1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2015-2019, Linaro Limited
4  * Copyright (c) 2020, Arm Limited.
5  */
6 #ifndef __KERNEL_TS_STORE_H
7 #define __KERNEL_TS_STORE_H
8 
9 #include <tee_api_types.h>
10 
11 struct ts_store_handle;
12 struct ts_store_ops {
13 	/*
14 	 * Human-readable string to describe where the TS comes from.
15 	 * For debug purposes only.
16 	 */
17 	const char *description;
18 	/*
19 	 * Open a TS. Does not guarantee that the TS is valid or even exists.
20 	 */
21 	TEE_Result (*open)(const TEE_UUID *uuid,
22 			   struct ts_store_handle **h);
23 	/*
24 	 * Return the size of the unencrypted TS binary, that is: the TS
25 	 * header (struct ta_head or sp_head) plus the ELF data.
26 	 */
27 	TEE_Result (*get_size)(const struct ts_store_handle *h,
28 			       size_t *size);
29 
30 	/*
31 	 * Return the tag or hash of the TS binary. Used to uniquely
32 	 * identify the binary also if the binary happens to be updated.
33 	 */
34 	TEE_Result (*get_tag)(const struct ts_store_handle *h,
35 			      uint8_t *tag, unsigned int *tag_len);
36 	/*
37 	 * Read the TS sequentially, from the start of the TS header (struct
38 	 * ta_head or sp_head) up to the end of the ELF.
39 	 * The TEE core is expected to read *exactly* get_size() bytes in total
40 	 * unless an error occurs. Therefore, an implementation may rely on the
41 	 * condition (current offset == total size) to detect the last call to
42 	 * this function.
43 	 * @data: pointer to secure memory where the TS bytes should be copied.
44 	 * If @data == NULL and @len != 0, the function should just skip @len
45 	 * bytes.
46 	 */
47 	TEE_Result (*read)(struct ts_store_handle *h, void *data,
48 			   size_t len);
49 	/*
50 	 * Close a TS handle. Do nothing if @h == NULL.
51 	 */
52 	void (*close)(struct ts_store_handle *h);
53 };
54 
55 /*
56  * Registers a TA storage.
57  *
58  * A TA is loaded from the first TA storage in which the TA can be found.
59  * TA storage is searched in order of priority, where lower values are
60  * tried first.
61  *
62  * Note prio must be unique per storage in order to avoid dependency on
63  * registration order. This is enforced by a deliberate linker error in
64  * case of conflict.
65  *
66  * Also note that TA storage is sorted lexicographically instead of
67  * numerically.
68  */
69 #define REGISTER_TA_STORE(prio) \
70 	int __tee_ta_store_##prio __unused; \
71 	SCATTERED_ARRAY_DEFINE_PG_ITEM_ORDERED(ta_stores, prio, \
72 					       struct ts_store_ops)
73 
74 /*
75  * Registers a SP storage.
76  *
77  * The SP store is separate from the TA store. The user of the stores knows if
78  * it needs to access the TA store or if it needs to access the SP one.
79  */
80 #define REGISTER_SP_STORE(prio) \
81 	int __tee_sp_store_##prio __unused; \
82 	SCATTERED_ARRAY_DEFINE_PG_ITEM_ORDERED(sp_stores, prio, \
83 					       struct ts_store_ops)
84 #endif /*__KERNEL_TS_STORE_H*/
85