1 /*
2 * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3 * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without modification,
6 * are permitted provided that the following conditions are met:
7 *
8 * 1. Redistributions of source code must retain the above copyright notice, this list of
9 * conditions and the following disclaimer.
10 *
11 * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 * of conditions and the following disclaimer in the documentation and/or other materials
13 * provided with the distribution.
14 *
15 * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16 * to endorse or promote products derived from this software without specific prior written
17 * permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 #ifndef AGENT_H
33 #define AGENT_H
34 #include "teek_ns_client.h"
35
36 #define AGENT_FS_ID 0x46536673 /* FSfs */
37 #define AGENT_MISC_ID 0x4d495343 /* MISC */
38 #define TEE_RPMB_AGENT_ID 0x4abe6198 /* RPMB */
39 #define AGENT_SOCKET_ID 0x69e85664 /* socket */
40 #define SECFILE_LOAD_AGENT_ID 0x4c4f4144 /* SECFILE-LOAD-AGENT */
41 #define TEE_SECE_AGENT_ID 0x53656345 /* test */
42
43 typedef enum {
44 AGENT_FALSE = -1,
45 AGENT_SUCCESS = 0,
46 } AgentErrCode;
47
48 typedef enum {
49 AGENT_ALIVE = 1,
50 AGENT_DEAD = 0,
51 } AgentStatusCode;
52
53 enum AgentStateType {
54 AGENT_CRASHED = 0,
55 AGENT_REGISTERED,
56 AGENT_READY,
57 };
58
59 struct SmcEventData *FindEventControl(unsigned int agentId);
60
61 /* for secure agent */
62 struct SmcEventData {
63 unsigned int agentId;
64 atomic_t agentReady;
65 wait_queue_head_t waitEventWq;
66 int retFlag; /* indicate whether agent is returned from TEE */
67 wait_queue_head_t sendResponseWq;
68 struct list_head head;
69 TcNsSmcCmd cmd;
70 TcNsDevFile *owner;
71 void *agentBuffKernel;
72 void *agentBuffUser; /* used for unmap */
73 unsigned int agentBuffSize;
74 atomic_t usage;
75 #ifdef CONFIG_TEE_SMP
76 wait_queue_head_t caPendingWq;
77 atomic_t caRun; /* indicate whether agent is allowed to return to TEE */
78 #endif
79 };
80
81 struct TeeAgentKernelOps {
82 const char *agentName; /* MUST NOT be NULL */
83 unsigned int agentId; /* MUST NOT be zero */
84 int (*teeAgentInit)(struct TeeAgentKernelOps *agentInstance);
85 int (*teeAgentRun)(struct TeeAgentKernelOps *agentInstance);
86 /* MUST NOT be NULL */
87 int (*teeAgentWork)(struct TeeAgentKernelOps *agentInstance);
88 int (*teeAgentStop)(struct TeeAgentKernelOps *agentInstance);
89 int (*teeAgentExit)(struct TeeAgentKernelOps *agentInstance);
90 int (*teeAgentCrashWork)(
91 struct TeeAgentKernelOps *agentInstance,
92 TcNsClientContext *context,
93 unsigned int devFileId);
94 LosTaskCB *agentThread;
95 void *agentData;
96 void *agentBuff;
97 unsigned int agentBuffSize;
98 struct list_head list;
99 };
100
GetAgentEvent(struct SmcEventData * eventData)101 static inline void GetAgentEvent(struct SmcEventData *eventData)
102 {
103 if (eventData != NULL) {
104 atomic_inc(&eventData->usage);
105 }
106 }
107
PutAgentEvent(struct SmcEventData * eventData)108 static inline void PutAgentEvent(struct SmcEventData *eventData)
109 {
110 if (eventData != NULL) {
111 if (atomic_dec_and_test(&eventData->usage)) {
112 free(eventData);
113 }
114 }
115 }
116
117 void AgentInit(void);
118 int AgentExit(void);
119 void SendEventResponse(unsigned int agentId);
120 int AgentProcessWork(const TcNsSmcCmd *smcCmd, unsigned int agentId);
121 int IsAgentAlive(unsigned int agentId);
122 int TcNsSetNativeHash(unsigned long arg, unsigned int cmdId);
123 int TcNsLateInit(unsigned long arg);
124 int TcNsRegisterAgent(TcNsDevFile *devFile, unsigned int agentId,
125 unsigned int bufferSize, void **buffer, bool userAgent);
126 int TcNsUnregisterAgent(unsigned int agentId);
127 void SendCrashedEventResponseAll(const TcNsDevFile *devFile);
128 int TcNsWaitEvent(unsigned int agentId);
129 int TcNsSendEventResponse(unsigned int agentId);
130 void SendEventResponseSingle(const TcNsDevFile *devFile);
131 int TcNsSyncSysTime(const TcNsClientTime *tcNsTime);
132 int TeeAgentClearWork(TcNsClientContext *context,
133 unsigned int devFileId);
134 int TeeAgentKernelRegister(struct TeeAgentKernelOps *newAgent);
135 bool IsSystemAgent(const TcNsDevFile *devFile);
136 void TeeAgentClearDevOwner(const TcNsDevFile *devFile);
137 extern int checkExtAgentAccess(LosTaskCB *caTask);
138
139 #endif /* AGENT_H */
140