1/* 2 * teek_ns_client.h 3 * 4 * define structures and IOCTLs. 5 * 6 * Copyright (C) 2022 Huawei Technologies Co., Ltd. 7 * 8 * This software is licensed under the terms of the GNU General Public 9 * License version 2, as published by the Free Software Foundation, and 10 * may be copied, distributed, and modified under those terms. 11 * 12 * This program is distributed in the hope that it will be useful, 13 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15 * GNU General Public License for more details. 16 */ 17#ifndef TEEK_NS_CLIENT_H 18#define TEEK_NS_CLIENT_H 19 20#include <linux/mutex.h> 21#include <linux/list.h> 22#include <linux/slab.h> 23#include <linux/completion.h> 24#include "tc_ns_client.h" 25#include "tc_ns_log.h" 26 27#define TC_NS_CLIENT_IOC_MAGIC 't' 28#define TC_NS_CLIENT_DEV "tc_ns_client" 29#define TC_PRIV_DEV "tc_private" 30#define TC_NS_CLIENT_DEV_NAME "/dev/tc_ns_client" 31 32#define EXCEPTION_MEM_SIZE (8*1024) /* mem for exception handling */ 33#ifdef CONFIG_THIRDPARTY_COMPATIBLE 34#define TSP_REQUEST 0x32000008 35#define TSP_RESPONSE 0xBE000005 36#else 37#define TSP_REQUEST 0xB2000008 38#define TSP_RESPONSE 0xB2000009 39#endif 40#define TSP_REE_SIQ 0xB200000A 41#define TSP_CRASH 0xB200000B 42#define TSP_REBOOT 0xB200000E 43#define TSP_CPU_ON 0xB200000F 44#define TSP_REBOOT_DONE 0xB2000010 45#define TSP_PREEMPTED 0xB2000005 46#define TC_CALL_GLOBAL 0x01 47#define TC_CALL_SYNC 0x02 48#define TC_CALL_LOGIN 0x04 49#define TEE_REQ_FROM_USER_MODE 0U 50#define TEE_REQ_FROM_KERNEL_MODE 1U 51#define TEE_PARAM_NUM 4 52#define VMALLOC_TYPE 0 53#define RESERVED_TYPE 1 54 55/* Max sizes for login info buffer comming from teecd */ 56#define MAX_PACKAGE_NAME_LEN 255 57/* 58 * The apk certificate format is as follows: 59 * modulus_size(4 bytes) + modulus buffer(512 bytes) 60 * + exponent size(4 bytes) + exponent buffer(1 bytes) 61 */ 62#define MAX_PUBKEY_LEN 1024 63 64struct tc_ns_dev_list { 65 struct mutex dev_lock; /* for dev_file_list */ 66 struct list_head dev_file_list; 67}; 68 69struct tc_uuid { 70 uint32_t time_low; 71 uint16_t time_mid; 72 uint16_t timehi_and_version; 73 uint8_t clockseq_and_node[8]; /* clock len is 8 */ 74}; 75 76#define INVALID_MAP_ADDR ((void*)-1) 77struct tc_ns_shared_mem { 78 void *kernel_addr; 79 void *user_addr; 80 void *user_addr_ca; /* for ca alloc share mem */ 81 unsigned int len; 82 int mem_type; 83 struct list_head head; 84 atomic_t usage; 85 atomic_t offset; 86}; 87 88struct tc_ns_service { 89 unsigned char uuid[UUID_LEN]; 90 struct mutex session_lock; /* for session_list */ 91 struct list_head session_list; 92 struct list_head head; 93 struct mutex operation_lock; /* for session's open/close */ 94 atomic_t usage; 95}; 96 97#define SERVICES_MAX_COUNT 32 /* service limit can opened on 1 fd */ 98struct tc_ns_dev_file { 99 unsigned int dev_file_id; 100 struct mutex service_lock; /* for service_ref[], services[] */ 101 uint8_t service_ref[SERVICES_MAX_COUNT]; /* a judge if set services[i]=NULL */ 102 struct tc_ns_service *services[SERVICES_MAX_COUNT]; 103 struct mutex shared_mem_lock; /* for shared_mem_list */ 104 struct list_head shared_mem_list; 105 struct list_head head; 106 /* Device is linked to call from kernel */ 107 uint8_t kernel_api; 108 /* client login info provided by teecd, can be either package name and public 109 * key or uid(for non android services/daemons) 110 * login information can only be set once, dont' allow subsequent calls 111 */ 112 bool login_setup; 113 struct mutex login_setup_lock; /* for login_setup */ 114#ifdef CONFIG_AUTH_HASH 115 bool cainfo_hash_setup; 116 struct mutex cainfo_hash_setup_lock; 117#endif 118 uint32_t pkg_name_len; 119 uint8_t pkg_name[MAX_PACKAGE_NAME_LEN]; 120 uint32_t pub_key_len; 121 uint8_t pub_key[MAX_PUBKEY_LEN]; 122 int load_app_flag; 123 struct completion close_comp; /* for kthread close unclosed session */ 124}; 125 126union tc_ns_parameter { 127 struct { 128 unsigned int buffer; 129 unsigned int size; 130 } memref; 131 struct { 132 unsigned int a; 133 unsigned int b; 134 } value; 135}; 136 137struct tc_ns_login { 138 unsigned int method; 139 unsigned int mdata; 140}; 141 142struct tc_ns_operation { 143 unsigned int paramtypes; 144 union tc_ns_parameter params[TEE_PARAM_NUM]; 145 unsigned int buffer_h_addr[TEE_PARAM_NUM]; 146 struct tc_ns_shared_mem *sharemem[TEE_PARAM_NUM]; 147 void *mb_buffer[TEE_PARAM_NUM]; 148}; 149 150struct tc_ns_temp_buf { 151 void *temp_buffer; 152 unsigned int size; 153}; 154 155enum smc_cmd_type { 156 CMD_TYPE_GLOBAL, 157 CMD_TYPE_TA, 158 CMD_TYPE_TA_AGENT, 159 CMD_TYPE_TA2TA_AGENT, /* compatible with TA2TA2TA->AGENT etc. */ 160 CMD_TYPE_BUILDIN_AGENT, 161}; 162 163struct tc_ns_smc_cmd { 164 uint8_t uuid[sizeof(struct tc_uuid)]; 165 unsigned int cmd_type; 166 unsigned int cmd_id; 167 unsigned int dev_file_id; 168 unsigned int context_id; 169 unsigned int agent_id; 170 unsigned int operation_phys; 171 unsigned int operation_h_phys; 172 unsigned int login_method; 173 unsigned int login_data_phy; 174 unsigned int login_data_h_addr; 175 unsigned int login_data_len; 176 unsigned int err_origin; 177 int ret_val; 178 unsigned int event_nr; 179 unsigned int uid; 180 unsigned int ca_pid; /* pid */ 181 unsigned int pid; /* tgid */ 182 unsigned int eventindex; /* tee audit event index for upload */ 183 bool started; 184} __attribute__((__packed__)); 185 186/* 187 * @brief 188 */ 189struct tc_wait_data { 190 wait_queue_head_t send_cmd_wq; 191 int send_wait_flag; 192}; 193 194#define NUM_OF_SO 1 195#ifdef CONFIG_CMS_CAHASH_AUTH 196#define KIND_OF_SO 1 /* the number of libteecxxx.so library on MDC\DC\TI */ 197#else 198#define KIND_OF_SO 2 /* the number of libteecxxx.so library on OH\HO */ 199#endif 200struct tc_ns_session { 201 unsigned int session_id; 202 struct list_head head; 203 struct tc_wait_data wait_data; 204 struct mutex ta_session_lock; /* for open/close/invoke on 1 session */ 205 struct tc_ns_dev_file *owner; 206 uint8_t auth_hash_buf[MAX_SHA_256_SZ * NUM_OF_SO + MAX_SHA_256_SZ]; 207 atomic_t usage; 208}; 209 210struct mb_cmd_pack { 211 struct tc_ns_operation operation; 212 unsigned char login_data[MAX_SHA_256_SZ * NUM_OF_SO + MAX_SHA_256_SZ]; 213}; 214 215struct load_img_params { 216 struct tc_ns_dev_file *dev_file; 217 const char *file_buffer; 218 unsigned int file_size; 219 struct mb_cmd_pack *mb_pack; 220 char *mb_load_mem; 221 struct tc_uuid *uuid_return; 222 unsigned int mb_load_size; 223}; 224 225struct tc_call_params { 226 struct tc_ns_dev_file *dev; 227 struct tc_ns_client_context *context; 228 struct tc_ns_session *sess; 229 uint8_t flags; 230}; 231 232struct tc_op_params { 233 struct mb_cmd_pack *mb_pack; 234 struct tc_ns_smc_cmd *smc_cmd; 235 struct tc_ns_temp_buf local_tmpbuf[TEE_PARAM_NUM]; 236 uint32_t trans_paramtype[TEE_PARAM_NUM]; 237 bool op_inited; 238}; 239 240#endif 241