1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3 * Copyright (c) 2014, STMicroelectronics International N.V.
4 */
5
6 #ifndef __TEE_TEE_FS_H
7 #define __TEE_TEE_FS_H
8
9 #include <pta_stats.h>
10 #include <stdbool.h>
11 #include <stddef.h>
12 #include <stdint.h>
13 #include <tee_api_defines_extensions.h>
14 #include <tee_api_types.h>
15
16 #define TEE_FS_NAME_MAX U(350)
17
18 typedef int64_t tee_fs_off_t;
19 typedef uint32_t tee_fs_mode_t;
20
21 struct tee_fs_dirent {
22 uint8_t oid[TEE_OBJECT_ID_MAX_LEN];
23 size_t oidlen;
24 };
25
26 struct tee_fs_dir;
27 struct tee_file_handle;
28 struct tee_pobj;
29
30 /*
31 * tee_fs implements a POSIX like secure file system with GP extension
32 */
33 struct tee_file_operations {
34 TEE_Result (*open)(struct tee_pobj *po, size_t *size,
35 struct tee_file_handle **fh);
36 TEE_Result (*create)(struct tee_pobj *po, bool overwrite,
37 const void *head, size_t head_size,
38 const void *attr, size_t attr_size,
39 const void *data_core, const void *data_user,
40 size_t data_size, struct tee_file_handle **fh);
41 void (*close)(struct tee_file_handle **fh);
42 TEE_Result (*read)(struct tee_file_handle *fh, size_t pos,
43 void *buf_core, void *buf_user, size_t *len);
44 TEE_Result (*write)(struct tee_file_handle *fh, size_t pos,
45 const void *buf_core, const void *buf_user,
46 size_t len);
47 TEE_Result (*rename)(struct tee_pobj *old_po, struct tee_pobj *new_po,
48 bool overwrite);
49 TEE_Result (*remove)(struct tee_pobj *po);
50 TEE_Result (*truncate)(struct tee_file_handle *fh, size_t size);
51
52 TEE_Result (*opendir)(const TEE_UUID *uuid, struct tee_fs_dir **d);
53 TEE_Result (*readdir)(struct tee_fs_dir *d, struct tee_fs_dirent **ent);
54 void (*closedir)(struct tee_fs_dir *d);
55 };
56
57 #ifdef CFG_REE_FS
58 extern const struct tee_file_operations ree_fs_ops;
59 #endif
60 #ifdef CFG_RPMB_FS
61 extern const struct tee_file_operations rpmb_fs_ops;
62
63 TEE_Result tee_rpmb_fs_raw_open(const char *fname, bool create,
64 struct tee_file_handle **fh);
65 TEE_Result tee_rpmb_reinit(void);
66
67 /* Ger RPMB memory allocation statistics */
68 TEE_Result rpmb_mem_stats(struct pta_stats_alloc *stats, bool reset);
69
70 /**
71 * Weak function which can be overridden by platforms to indicate that the RPMB
72 * key is ready to be written. Defaults to true, platforms can return false to
73 * prevent a RPMB key write in the wrong state.
74 */
75 bool plat_rpmb_key_is_ready(void);
76 #else
tee_rpmb_reinit(void)77 static inline TEE_Result tee_rpmb_reinit(void)
78 {
79 return TEE_ERROR_STORAGE_NOT_AVAILABLE;
80 }
81
rpmb_mem_stats(struct pta_stats_alloc * stats __unused,bool reset __unused)82 static inline TEE_Result rpmb_mem_stats(struct pta_stats_alloc *stats __unused,
83 bool reset __unused)
84 {
85 return TEE_ERROR_STORAGE_NOT_AVAILABLE;
86 }
87 #endif
88
89 /*
90 * Returns the appropriate tee_file_operations for the specified storage ID.
91 * The value TEE_STORAGE_PRIVATE will select the REE FS if available, otherwise
92 * RPMB.
93 */
94 static inline const struct tee_file_operations *
tee_svc_storage_file_ops(uint32_t storage_id)95 tee_svc_storage_file_ops(uint32_t storage_id)
96 {
97 switch (storage_id) {
98 case TEE_STORAGE_PRIVATE:
99 #if defined(CFG_REE_FS)
100 return &ree_fs_ops;
101 #elif defined(CFG_RPMB_FS)
102 return &rpmb_fs_ops;
103 #else
104 return NULL;
105 #endif
106 #ifdef CFG_REE_FS
107 case TEE_STORAGE_PRIVATE_REE:
108 return &ree_fs_ops;
109 #endif
110 #ifdef CFG_RPMB_FS
111 case TEE_STORAGE_PRIVATE_RPMB:
112 return &rpmb_fs_ops;
113 #endif
114 default:
115 return NULL;
116 }
117 }
118
119 #endif /*__TEE_TEE_FS_H*/
120