1 /*
2 * Copyright (C) 2022 Huawei Technologies Co., Ltd.
3 * Licensed under the Mulan PSL v2.
4 * You can use this software according to the terms and conditions of the Mulan PSL v2.
5 * You may obtain a copy of Mulan PSL v2 at:
6 * http://license.coscl.org.cn/MulanPSL2
7 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
8 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
9 * PURPOSE.
10 * See the Mulan PSL v2 for more details.
11 */
12
13 #include "misc_work_agent.h"
14 #include <fcntl.h>
15 #include <securec.h>
16 #include <sys/ioctl.h>
17 #include <sys/time.h>
18 #include <sys/types.h>
19 #include <time.h>
20 #include "tc_ns_client.h"
21 #include "tee_agent.h"
22 #include "tee_log.h"
23
24 #ifdef LOG_TAG
25 #undef LOG_TAG
26 #endif
27 #define LOG_TAG "teecd_agent"
28
GetTimeWork(struct MiscControlType * transControl)29 static void GetTimeWork(struct MiscControlType *transControl)
30 {
31 struct timeval timeVal;
32
33 if (gettimeofday(&timeVal, NULL) == 0) {
34 transControl->ret = 0;
35 transControl->Args.GetTime.seconds = (uint32_t)timeVal.tv_sec;
36 transControl->Args.GetTime.millis = (uint32_t)(timeVal.tv_usec / 1000);
37 struct tm *tstruct = localtime(&(timeVal.tv_sec));
38 if (tstruct != NULL) {
39 /* year(from 1900) months(0~11) days hour min second */
40 errno_t rc = snprintf_s(transControl->Args.GetTime.timeStr, sizeof(transControl->Args.GetTime.timeStr),
41 sizeof(transControl->Args.GetTime.timeStr) - 1, "%04d-%02d-%02d %02d:%02d:%02d.%03d ",
42 tstruct->tm_year + 1900, tstruct->tm_mon + 1, tstruct->tm_mday, tstruct->tm_hour,
43 tstruct->tm_min, tstruct->tm_sec, (int)(timeVal.tv_usec / 1000));
44 if (rc == -1) {
45 transControl->ret = -1;
46 tloge("snprintf_s error %d\n", rc);
47 }
48 } else {
49 tloge("get localtime error\n");
50 }
51 } else {
52 transControl->ret = -1;
53 transControl->Args.GetTime.seconds = 0;
54 transControl->Args.GetTime.millis = 0;
55 }
56 }
57
MiscWorkThread(void * control)58 void *MiscWorkThread(void *control)
59 {
60 struct MiscControlType *transControl = NULL;
61 int miscFd;
62
63 if (control == NULL) {
64 return NULL;
65 }
66 transControl = (struct MiscControlType *)control;
67
68 miscFd = GetMiscFd();
69 if (miscFd == -1) {
70 tloge("misc file is not open\n");
71 return NULL;
72 }
73
74 transControl->magic = AGENT_MISC_ID;
75 while (1) {
76 tlogv("++ misc agent loop ++\n");
77 int ret = ioctl(miscFd, (int)TC_NS_CLIENT_IOCTL_WAIT_EVENT, AGENT_MISC_ID);
78 if (ret) {
79 tloge("misc agent wait event failed, errno = %d\n", errno);
80 break;
81 }
82
83 tlogv("misc agent wake up and working!!\n");
84 switch (transControl->cmd) {
85 case SEC_NV_INFO: /* bootloaderlock status in nv partition */
86 tlogv("sec nv info access\n");
87 break;
88 case SEC_GET_TIME:
89 tlogv("sec get time of day\n");
90 GetTimeWork(transControl);
91 break;
92 default:
93 tloge("misc agent error cmd\n");
94 break;
95 }
96
97 __asm__ volatile("isb");
98 __asm__ volatile("dsb sy");
99
100 transControl->magic = AGENT_MISC_ID;
101
102 __asm__ volatile("isb");
103 __asm__ volatile("dsb sy");
104
105 ret = ioctl(miscFd, (int)TC_NS_CLIENT_IOCTL_SEND_EVENT_RESPONSE, AGENT_MISC_ID);
106 if (ret) {
107 tloge("misc agent send reponse failed\n");
108 break;
109 }
110 tlogv("-- misc agent loop --\n");
111 }
112
113 return NULL;
114 }
115