1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
|
#include "tz_secure_clock.h"
#include <linux/earlysuspend.h>
#ifdef TZ_SECURETIME_SUPPORT
uint32_t TEE_update_gb_time_intee(KREE_SESSION_HANDLE session, KREE_SESSION_HANDLE mem_session)
{
MTEEC_PARAM param[4];
uint32_t paramTypes;
TZ_RESULT ret = TZ_RESULT_SUCCESS;
uint32_t file_result = GB_TIME_FILE_OK_SIGN;
struct file *file = NULL;
UINT64 u8Offset = 0;
int i = 0;
int err = -ENODEV;
struct rtc_time tm;
DRM_UINT64 time_count64;
unsigned long time_count;
struct rtc_device *rtc;
struct TZ_GB_SECURETIME_INFO secure_time;
DRM_UINT64 cur_counter;
pr_err("enter TEE_update_gb_time_intee \n");
rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
if (rtc == NULL) {
pr_err("%s: unable to open rtc device (%s)\n",
__FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
goto err_open;
}
paramTypes = TZ_ParamTypes3(TZPT_MEM_INPUT, TZPT_MEM_INPUT, TZPT_VALUE_OUTPUT);
param[0].mem.buffer = (void *) &secure_time;
param[0].mem.size = sizeof(struct TZ_GB_SECURETIME_INFO);
param[1].mem.buffer = (void *) &cur_counter;
param[1].mem.size = sizeof(DRM_UINT64);
file = FILE_Open(GB_TIME_FILE_SAVE_PATH, O_RDWR, S_IRWXU);
if (file) {
FILE_Read(file, (void *) &secure_time, sizeof(struct TZ_GB_SECURETIME_INFO), &u8Offset);
filp_close(file, NULL);
} else {
file_result = GB_NO_SECURETD_FILE;
goto err_read;
}
for (i=0;i< sizeof(struct TZ_GB_SECURETIME_INFO);i++)
pr_err("%02x",((char*)&secure_time)[i]);
pr_err("\n");
err = rtc_read_time(rtc, &tm);
if (err) {
dev_err(rtc->dev.parent,
"hctosys: unable to read the hardware clock\n");
goto err_read;
}
err = rtc_valid_tm(&tm);
if (err) {
dev_err(rtc->dev.parent, "hctosys: invalid date/time\n");
goto err_invalid;
}
rtc_tm_to_time(&tm, &time_count);
time_count64 = (DRM_UINT64)time_count;
memcpy((char *) &cur_counter, (char *) &time_count64, sizeof(DRM_UINT64));
ret = KREE_TeeServiceCall(session, TZCMD_SECURETIME_SET_CURRENT_COUNTER, paramTypes, param);
if (ret != TZ_RESULT_SUCCESS)
pr_info("ServiceCall TZCMD_SECURETIME_SET_CURRENT_COUNTER error %d\n", ret);
if (param[2].value.a == GB_TIME_FILE_ERROR_SIGN) {
file_result = GB_TIME_FILE_ERROR_SIGN;
pr_info("ServiceCall TZCMD_SECURETIME_SET_CURRENT_COUNTER file_result %d\n", file_result);
} else if (param[2].value.a == GB_TIME_FILE_OK_SIGN) {
file_result = GB_TIME_FILE_OK_SIGN;
pr_info("ServiceCall TZCMD_SECURETIME_SET_CURRENT_COUNTER file_result %d\n", file_result);
}
err_invalid:
err_read:
rtc_class_close(rtc);
err_open:
if (file_result == GB_TIME_FILE_OK_SIGN)
return ret;
else
return file_result;
}
uint32_t TEE_update_gb_time_infile(KREE_SESSION_HANDLE session, KREE_SESSION_HANDLE mem_session)
{
uint32_t *shm_p;
MTEEC_PARAM param[4];
uint32_t paramTypes;
TZ_RESULT ret = TZ_RESULT_SUCCESS;
struct file *file = NULL;
UINT64 u8Offset = 0;
pr_err("enter TEE_update_gb_time_infile \n");
shm_p = kmalloc(sizeof(struct TZ_GB_SECURETIME_INFO), GFP_KERNEL);
paramTypes = TZ_ParamTypes3(TZPT_MEM_OUTPUT, TZPT_VALUE_INPUT, TZPT_VALUE_OUTPUT);
param[0].mem.buffer = shm_p;
param[0].mem.size = sizeof(struct TZ_GB_SECURETIME_INFO);
param[1].value.a = 0;
ret = KREE_TeeServiceCall(session, TZCMD_SECURETIME_GET_CURRENT_COUNTER, paramTypes, param);
if (ret != TZ_RESULT_SUCCESS) {
pr_err("ServiceCall error %d\n", ret);
goto tz_error;
}
file = FILE_Open(GB_TIME_FILE_SAVE_PATH, O_RDWR|O_CREAT, S_IRWXU);
if (file) {
FILE_Write(file, (void *)shm_p, sizeof(struct TZ_GB_SECURETIME_INFO), &u8Offset);
filp_close(file, NULL);
} else {
pr_err("FILE_Open GB_TIME_FILE_SAVE_PATH return NULL\n");
}
tz_error:
if (shm_p != NULL)
kfree(shm_p);
return ret;
}
uint32_t TEE_Icnt_time(KREE_SESSION_HANDLE session, KREE_SESSION_HANDLE mem_session)
{
uint32_t *shm_p;
//KREE_SHAREDMEM_PARAM shm_param;
//KREE_SHAREDMEM_HANDLE shm_handle;
MTEEC_PARAM param[4];
uint32_t paramTypes;
TZ_RESULT ret;
unsigned long time_count = 1392967151;
struct rtc_device *rtc = NULL;
struct rtc_time tm;
int err = -ENODEV;
ret = err;
pr_err("enter TEE_Icnt_time \n");
shm_p = kmalloc(sizeof(struct TM_GB), GFP_KERNEL);
param[1].mem.buffer = shm_p;
param[1].mem.size = sizeof(struct TM_GB);
paramTypes = TZ_ParamTypes2(TZPT_VALUE_INPUT, TZPT_MEM_OUTPUT);
rtc = rtc_class_open(CONFIG_RTC_HCTOSYS_DEVICE);
if (rtc == NULL) {
pr_err("%s: unable to open rtc device (%s)\n",
__FILE__, CONFIG_RTC_HCTOSYS_DEVICE);
goto err_open;
}
err = rtc_read_time(rtc, &tm);
if (err) {
dev_err(rtc->dev.parent,
"hctosys: unable to read the hardware clock\n");
goto err_read;
}
err = rtc_valid_tm(&tm);
if (err) {
dev_err(rtc->dev.parent,
"hctosys: invalid date/time\n");
goto err_invalid;
}
rtc_tm_to_time(&tm, &time_count);
#if 1
pr_info("securetime increase result: %d %d %d %d %d %d %d\n", tm.tm_yday, tm.tm_year, tm.tm_mon
, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec);
#endif
param[0].value.a = time_count;
ret = KREE_TeeServiceCall(session, TZCMD_SECURETIME_INC_CURRENT_COUNTER, paramTypes, param);
if (ret != TZ_RESULT_SUCCESS)
pr_err("ServiceCall error %d\n", ret);
#if 1
pr_info("securetime increase result: %d %d %d %d %d %d %d\n", ((struct TM_GB *) shm_p)->tm_yday
, ((struct TM_GB *) shm_p)->tm_year, ((struct TM_GB *) shm_p)->tm_mon, ((struct TM_GB *) shm_p)->tm_mday
, ((struct TM_GB *) shm_p)->tm_hour, ((struct TM_GB *) shm_p)->tm_min, ((struct TM_GB *) shm_p)->tm_sec);
#endif
err_read:
err_invalid:
rtc_class_close(rtc);
err_open:
if (shm_p != NULL)
kfree(shm_p);
return ret;
}
#define THREAD_COUNT_FREQ 5
#define THREAD_SAVEFILE_VALUE (10*60) // store secure time per minute
static int check_count;
static KREE_SESSION_HANDLE icnt_session;
static KREE_SESSION_HANDLE mem_session;
static int securetime_savefile_gb(void)
{
int ret = 0;
KREE_SESSION_HANDLE securetime_session = 0;
KREE_SESSION_HANDLE mem_session = 0;
ret = KREE_CreateSession(TZ_TA_SECURETIME_UUID, &securetime_session);
if (ret != TZ_RESULT_SUCCESS) {
pr_info("[securetime]CreateSession error %d\n", ret);
} else {
ret = KREE_CreateSession(TZ_TA_MEM_UUID, &mem_session);
TEE_update_gb_time_infile(securetime_session, mem_session);
ret = KREE_CloseSession(securetime_session);
if (ret != TZ_RESULT_SUCCESS)
pr_info("CloseSession error %d\n", ret);
ret = KREE_CloseSession(mem_session);
if (ret != TZ_RESULT_SUCCESS)
pr_info("[securetime]CloseMEMSession error %d\n", ret);
}
return ret;
}
#ifdef CONFIG_HAS_EARLYSUSPEND_GB
static void st_early_suspend_gb(struct early_suspend *h)
{
pr_info("[securetime]st_early_suspend_gb error \n");
//pr_info("st_early_suspend: start\n");
securetime_savefile_gb();
}
static void st_late_resume_gb(struct early_suspend *h)
{
int ret = 0;
KREE_SESSION_HANDLE securetime_session = 0;
KREE_SESSION_HANDLE mem_session = 0;
ret = KREE_CreateSession(TZ_TA_SECURETIME_UUID, &securetime_session);
if (ret != TZ_RESULT_SUCCESS) {
pr_info("[securetime]CreateSession error %d\n", ret);
} else {
ret = KREE_CreateSession(TZ_TA_MEM_UUID, &mem_session);
TEE_update_gb_time_intee(securetime_session, mem_session);
ret = KREE_CloseSession(securetime_session);
if (ret != TZ_RESULT_SUCCESS)
pr_info("[securetime]CloseSession error %d\n", ret);
ret = KREE_CloseSession(mem_session);
if (ret != TZ_RESULT_SUCCESS)
pr_info("[securetime]CloseMEMSession error %d\n", ret);
}
}
static struct early_suspend securetime_early_suspend_gb = {
.level = 258,
.suspend = st_early_suspend_gb,
.resume = st_late_resume_gb,
};
#endif
int update_securetime_thread_gb(void *data)
{
TZ_RESULT ret;
unsigned int update_ret = 0;
uint32_t nsec = THREAD_COUNT_FREQ;
ret = KREE_CreateSession(TZ_TA_SECURETIME_UUID, &icnt_session);
if (ret != TZ_RESULT_SUCCESS) {
pr_err("update_securetime_thread_gb CreateSession error %d\n", ret);
return 1;
}
ret = KREE_CreateSession(TZ_TA_MEM_UUID, &mem_session);
if (ret != TZ_RESULT_SUCCESS) {
pr_err("update_securetime_thread_gb Create memory session error %d\n", ret);
ret = KREE_CloseSession(icnt_session);
return ret;
}
set_freezable();
schedule_timeout_interruptible(HZ * nsec * 6 * 2); // 2 minutes
update_ret = TEE_update_gb_time_intee(icnt_session, mem_session);
if (update_ret == GB_NO_SECURETD_FILE || update_ret == GB_TIME_FILE_ERROR_SIGN) {
TEE_update_gb_time_infile(icnt_session, mem_session);
TEE_update_gb_time_intee(icnt_session, mem_session);
}
#ifdef CONFIG_HAS_EARLYSUSPEND_GB
register_early_suspend(&securetime_early_suspend_gb);
#endif
for (;;) {
if (kthread_should_stop())
break;
if (try_to_freeze())
continue;
schedule_timeout_interruptible(HZ * nsec);
if (icnt_session && mem_session) {
TEE_Icnt_time(icnt_session, mem_session);
check_count += THREAD_COUNT_FREQ;
if ((check_count < 0) || (check_count > THREAD_SAVEFILE_VALUE)) {
TEE_update_gb_time_infile(icnt_session, mem_session);
check_count = 0;
}
}
pr_err("update_securetime_thread_gb updata inc count\n");
}
ret = KREE_CloseSession(icnt_session);
if (ret != TZ_RESULT_SUCCESS)
pr_err("update_securetime_thread_gb CloseSession error %d\n", ret);
ret = KREE_CloseSession(mem_session);
if (ret != TZ_RESULT_SUCCESS)
pr_err("update_securetime_thread_gb Close memory session error %d\n", ret);
return 0;
}
#endif
|