xref: /optee_os/core/pta/device.c (revision 12fc37711783247b0d05fdc271ef007f4930767b)
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/stmm_sp.h>
16 #include <kernel/tee_ta_manager.h>
17 #include <pta_device.h>
18 #include <string.h>
19 #include <tee/uuid.h>
20 #include <user_ta_header.h>
21 
22 #define PTA_NAME "device.pta"
23 
24 static void add_ta(uint32_t flags, const TEE_UUID *uuid, uint8_t *buf,
25 		   uint32_t blen, uint32_t *pos, uint32_t rflags)
26 {
27 	if ((flags & TA_FLAG_DEVICE_ENUM) &&
28 	    (flags & TA_FLAG_DEVICE_ENUM_SUPP)) {
29 		EMSG(PTA_NAME ": skipping TA %pUl, inconsistent flags", uuid);
30 		return;
31 	}
32 
33 	if (flags & rflags) {
34 		if (*pos + sizeof(*uuid) <= blen)
35 			tee_uuid_to_octets(buf + *pos, uuid);
36 
37 		*pos += sizeof(*uuid);
38 	}
39 }
40 
41 static TEE_Result get_devices(uint32_t types,
42 			      TEE_Param params[TEE_NUM_PARAMS],
43 			      uint32_t rflags)
44 {
45 	const struct pseudo_ta_head *ta = NULL;
46 	const struct embedded_ts *eta = NULL;
47 	void *buf = NULL;
48 	uint32_t blen = 0;
49 	uint32_t pos = 0;
50 
51 	if (types != TEE_PARAM_TYPES(TEE_PARAM_TYPE_MEMREF_OUTPUT,
52 				     TEE_PARAM_TYPE_NONE,
53 				     TEE_PARAM_TYPE_NONE,
54 				     TEE_PARAM_TYPE_NONE))
55 		return TEE_ERROR_BAD_PARAMETERS;
56 
57 	if (!params[0].memref.buffer && (params[0].memref.size > 0))
58 		return TEE_ERROR_BAD_PARAMETERS;
59 
60 	buf =  params[0].memref.buffer;
61 	blen = params[0].memref.size;
62 
63 	SCATTERED_ARRAY_FOREACH(ta, pseudo_tas, struct pseudo_ta_head)
64 		add_ta(ta->flags, &ta->uuid, buf, blen, &pos, rflags);
65 
66 	if (stmm_get_uuid())
67 		add_ta(TA_FLAG_DEVICE_ENUM_SUPP, stmm_get_uuid(), buf, blen,
68 		       &pos, rflags);
69 
70 	if (IS_ENABLED(CFG_EARLY_TA))
71 		for_each_early_ta(eta)
72 			add_ta(eta->flags, &eta->uuid, buf, blen, &pos,
73 			       rflags);
74 
75 	params[0].memref.size = pos;
76 	if (pos > blen)
77 		return TEE_ERROR_SHORT_BUFFER;
78 
79 	return TEE_SUCCESS;
80 }
81 
82 static TEE_Result invoke_command(void *pSessionContext __unused,
83 				 uint32_t nCommandID, uint32_t nParamTypes,
84 				 TEE_Param pParams[TEE_NUM_PARAMS])
85 {
86 	switch (nCommandID) {
87 	case PTA_CMD_GET_DEVICES:
88 		return get_devices(nParamTypes, pParams,
89 				   TA_FLAG_DEVICE_ENUM);
90 	case PTA_CMD_GET_DEVICES_SUPP:
91 		return get_devices(nParamTypes, pParams,
92 				   TA_FLAG_DEVICE_ENUM_SUPP);
93 	default:
94 		break;
95 	}
96 
97 	return TEE_ERROR_NOT_IMPLEMENTED;
98 }
99 
100 pseudo_ta_register(.uuid = PTA_DEVICE_UUID, .name = PTA_NAME,
101 		   .flags = PTA_DEFAULT_FLAGS,
102 		   .invoke_command_entry_point = invoke_command);
103