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 #ifndef TEE_CLIENT_INNER_API_H
14 #define TEE_CLIENT_INNER_API_H
15 
16 #include <pthread.h>
17 #include <stdint.h>
18 #include <stdio.h>
19 #include <unistd.h>
20 #include "tee_auth_common.h"
21 #include "tee_client_constants.h"
22 #include "tee_client_inner.h"
23 #include "tee_client_type.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 /* TEE GLOBAL CMD */
30 enum SVC_GLOBAL_CMD_ID {
31     GLOBAL_CMD_ID_INVALID                   = 0x0,  /* Global Task invalid cmd ID */
32     GLOBAL_CMD_ID_BOOT_ACK                  = 0x1,  /* Global Task boot ack */
33     GLOBAL_CMD_ID_OPEN_SESSION              = 0x2,  /* Global Task open Session */
34     GLOBAL_CMD_ID_CLOSE_SESSION             = 0x3,  /* Global Task close Session */
35     GLOBAL_CMD_ID_LOAD_SECURE_APP           = 0x4,  /* Global Task load dyn ta */
36     GLOBAL_CMD_ID_NEED_LOAD_APP             = 0x5,  /* Global Task judge if need load ta */
37     GLOBAL_CMD_ID_REGISTER_AGENT            = 0x6,  /* Global Task register agent */
38     GLOBAL_CMD_ID_UNREGISTER_AGENT          = 0x7,  /* Global Task unregister agent */
39     GLOBAL_CMD_ID_REGISTER_NOTIFY_MEMORY    = 0x8,  /* Global Task register notify memory */
40     GLOBAL_CMD_ID_UNREGISTER_NOTIFY_MEMORY  = 0x9,  /* Global Task unregister notify memory */
41     GLOBAL_CMD_ID_INIT_CONTENT_PATH         = 0xa,  /* Global Task init content path */
42     GLOBAL_CMD_ID_TERMINATE_CONTENT_PATH    = 0xb,  /* Global Task terminate content path */
43     GLOBAL_CMD_ID_ALLOC_EXCEPTION_MEM       = 0xc,  /* Global Task alloc exception memory */
44     GLOBAL_CMD_ID_TEE_TIME                  = 0xd,  /* Global Task get tee secure time */
45     GLOBAL_CMD_ID_TEE_INFO                  = 0xe,  /* Global Task tlogcat get tee info */
46     GLOBAL_CMD_ID_MAX,
47 };
48 
49 void SetBit(uint32_t i, uint32_t byteMax, uint8_t *bitMap);
50 void ClearBit(uint32_t i, uint32_t byteMax, uint8_t *bitMap);
51 int32_t GetAndSetBit(uint8_t *bitMap, uint32_t byteMax);
52 int32_t GetAndCleartBit(uint8_t *bitMap, uint32_t byteMax);
53 TEEC_Result TEEC_InitializeContextInner(TEEC_ContextInner *context, const CaAuthInfo *caInfo);
54 TEEC_Result TEEC_OpenSessionInner(int callingPid, const TaFileInfo *taFile, TEEC_ContextInner *context,
55     TEEC_Session *session, const TEEC_UUID *destination, uint32_t connectionMethod,
56     const void *connectionData, TEEC_Operation *operation, uint32_t *returnOrigin);
57 TEEC_Result TEEC_InvokeCommandInner(TEEC_ContextInner *context, const TEEC_Session *session,
58     uint32_t commandID, const TEEC_Operation *operation, uint32_t *returnOrigin);
59 void TEEC_CloseSessionInner(TEEC_Session *session, const TEEC_ContextInner *context);
60 void TEEC_FinalizeContextInner(TEEC_ContextInner *context);
61 TEEC_Result TEEC_RegisterSharedMemoryInner(TEEC_ContextInner *context, TEEC_SharedMemoryInner *sharedMem);
62 TEEC_Result TEEC_AllocateSharedMemoryInner(TEEC_ContextInner *context, TEEC_SharedMemoryInner *sharedMem);
63 void TEEC_ReleaseSharedMemoryInner(TEEC_SharedMemoryInner *sharedMem);
64 TEEC_ContextInner *GetBnContext(const TEEC_Context *context);
65 bool PutBnContext(TEEC_ContextInner *context);
66 TEEC_ContextInner *FindAndRemoveBnContext(const TEEC_Context *context);
67 TEEC_Session *GetBnSession(const TEEC_Session *session, TEEC_ContextInner *context);
68 void PutBnSession(TEEC_Session *session);
69 TEEC_Session *FindAndRemoveSession(const TEEC_Session *session, TEEC_ContextInner *context);
70 TEEC_SharedMemoryInner *GetBnShmByOffset(uint32_t shmOffset, TEEC_ContextInner *context);
71 void PutBnShrMem(TEEC_SharedMemoryInner *shrMem);
72 TEEC_Result TEEC_SendSecfileInner(const char *path, int tzFd, FILE *fp);
73 
74 #ifdef __cplusplus
75 }
76 #endif
77 #endif
78