xref: /optee_os/core/include/tee/tee_fs.h (revision ef3bc69c72b8d46493eab724eab6e018423088e1)
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 <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 TEE_Result tee_rpmb_reinit(void);
64 
65 /**
66  * Weak function which can be overridden by platforms to indicate that the RPMB
67  * key is ready to be written. Defaults to true, platforms can return false to
68  * prevent a RPMB key write in the wrong state.
69  */
70 bool plat_rpmb_key_is_ready(void);
71 #else
72 static inline TEE_Result tee_rpmb_reinit(void)
73 {
74 	return TEE_ERROR_STORAGE_NOT_AVAILABLE;
75 }
76 #endif
77 
78 /*
79  * Returns the appropriate tee_file_operations for the specified storage ID.
80  * The value TEE_STORAGE_PRIVATE will select the REE FS if available, otherwise
81  * RPMB.
82  */
83 static inline const struct tee_file_operations *
84 tee_svc_storage_file_ops(uint32_t storage_id)
85 {
86 	switch (storage_id) {
87 	case TEE_STORAGE_PRIVATE:
88 #if defined(CFG_REE_FS)
89 		return &ree_fs_ops;
90 #elif defined(CFG_RPMB_FS)
91 		return &rpmb_fs_ops;
92 #else
93 		return NULL;
94 #endif
95 #ifdef CFG_REE_FS
96 	case TEE_STORAGE_PRIVATE_REE:
97 		return &ree_fs_ops;
98 #endif
99 #ifdef CFG_RPMB_FS
100 	case TEE_STORAGE_PRIVATE_RPMB:
101 		return &rpmb_fs_ops;
102 #endif
103 	default:
104 		return NULL;
105 	}
106 }
107 
108 #endif /*__TEE_TEE_FS_H*/
109