1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (C) 2019, Linaro Limited 4 */ 5 6 /* 7 * This pseudo TA is used by normal world OS TEE driver to fetch pseudo TA's 8 * UUIDs which can act as TEE bus devices. 9 */ 10 11 #include <config.h> 12 #include <kernel/early_ta.h> 13 #include <kernel/linker.h> 14 #include <kernel/pseudo_ta.h> 15 #include <kernel/tee_ta_manager.h> 16 #include <pta_device.h> 17 #include <string.h> 18 #include <tee/uuid.h> 19 #include <user_ta_header.h> 20 21 #define PTA_NAME "device.pta" 22 23 static void add_ta(uint32_t flags, const TEE_UUID *uuid, uint8_t *buf, 24 uint32_t blen, uint32_t *pos) 25 { 26 if (flags & TA_FLAG_DEVICE_ENUM) { 27 if (*pos + sizeof(*uuid) <= blen) 28 tee_uuid_to_octets(buf + *pos, uuid); 29 30 *pos += sizeof(*uuid); 31 } 32 } 33 34 static TEE_Result get_devices(uint32_t types, 35 TEE_Param params[TEE_NUM_PARAMS]) 36 { 37 const struct pseudo_ta_head *ta = NULL; 38 const struct early_ta *eta = NULL; 39 void *buf = NULL; 40 uint32_t blen = 0; 41 uint32_t pos = 0; 42 43 if (types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_OUTPUT, 44 TEE_PARAM_TYPE_NONE, 45 TEE_PARAM_TYPE_NONE, 46 TEE_PARAM_TYPE_NONE)) 47 return TEE_ERROR_BAD_PARAMETERS; 48 49 if (!params[0].memref.buffer && (params[0].memref.size > 0)) 50 return TEE_ERROR_BAD_PARAMETERS; 51 52 buf = params[0].memref.buffer; 53 blen = params[0].memref.size; 54 55 SCATTERED_ARRAY_FOREACH(ta, pseudo_tas, struct pseudo_ta_head) 56 add_ta(ta->flags, &ta->uuid, buf, blen, &pos); 57 58 if (IS_ENABLED(CFG_EARLY_TA)) 59 for_each_early_ta(eta) 60 add_ta(eta->flags, &eta->uuid, buf, blen, &pos); 61 62 params[0].memref.size = pos; 63 if (pos > blen) 64 return TEE_ERROR_SHORT_BUFFER; 65 66 return TEE_SUCCESS; 67 } 68 69 static TEE_Result invoke_command(void *pSessionContext __unused, 70 uint32_t nCommandID, uint32_t nParamTypes, 71 TEE_Param pParams[TEE_NUM_PARAMS]) 72 { 73 switch (nCommandID) { 74 case PTA_CMD_GET_DEVICES: 75 return get_devices(nParamTypes, pParams); 76 default: 77 break; 78 } 79 80 return TEE_ERROR_NOT_IMPLEMENTED; 81 } 82 83 pseudo_ta_register(.uuid = PTA_DEVICE_UUID, .name = PTA_NAME, 84 .flags = PTA_DEFAULT_FLAGS, 85 .invoke_command_entry_point = invoke_command); 86