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