1 /* SPDX-License-Identifier: BSD-2-Clause */
2 /*
3  * Copyright (c) 2017, Linaro Limited
4  */
5 
6 #ifndef __TEE_FS_DIRFILE_H
7 #define __TEE_FS_DIRFILE_H
8 
9 #include <tee/tee_fs.h>
10 #include <tee/fs_htree.h>
11 
12 struct tee_fs_dirfile_dirh;
13 
14 /**
15  * struct tee_fs_dirfile_fileh - file handle
16  * @file_number:	sequence number of a file
17  * @hash:		hash of file, to be supplied to tee_fs_htree_open()
18  * @idx:		index of the file handle in the dirfile
19  */
20 struct tee_fs_dirfile_fileh {
21 	uint32_t file_number;
22 	uint8_t hash[TEE_FS_HTREE_HASH_SIZE];
23 	int idx;
24 };
25 
26 /**
27  * struct tee_fs_dirfile_operations - file interface supplied by user of this
28  * interface
29  * @open:		opens a file
30  * @close:		closes a file, changes are discarded unless
31  *			@commit_writes is called before
32  * @read:		reads from an open file
33  * @write:		writes to an open file
34  * @commit_writes:	commits changes since the file was opened
35  */
36 struct tee_fs_dirfile_operations {
37 	TEE_Result (*open)(bool create, uint8_t *hash, const TEE_UUID *uuid,
38 			   struct tee_fs_dirfile_fileh *dfh,
39 			   struct tee_file_handle **fh);
40 	void (*close)(struct tee_file_handle *fh);
41 	TEE_Result (*read)(struct tee_file_handle *fh, size_t pos,
42 			   void *buf, size_t *len);
43 	TEE_Result (*write)(struct tee_file_handle *fh, size_t pos,
44 			    const void *buf, size_t len);
45 	TEE_Result (*commit_writes)(struct tee_file_handle *fh, uint8_t *hash);
46 };
47 
48 /**
49  * tee_fs_dirfile_open() - opens a dirfile handle
50  * @create:	true if a new dirfile is to be created, else the dirfile
51  *		is read opened and verified
52  * @hash:	hash of underlying file
53  * @fops:	file interface
54  * @dirh:	returned dirfile handle
55  */
56 TEE_Result tee_fs_dirfile_open(bool create, uint8_t *hash,
57 			       const struct tee_fs_dirfile_operations *fops,
58 			       struct tee_fs_dirfile_dirh **dirh);
59 /**
60  * tee_fs_dirfile_close() - closes a dirfile handle
61  * @dirh:	dirfile handle
62  *
63  * All changes since last call to tee_fs_dirfile_commit_writes() are
64  * discarded.
65  */
66 void tee_fs_dirfile_close(struct tee_fs_dirfile_dirh *dirh);
67 
68 /**
69  * tee_fs_dirfile_commit_writes() - commit updates of dirfile
70  * @dirh:	dirfile handle
71  * @hash:	hash of underlying file is copied here if not NULL
72  */
73 TEE_Result tee_fs_dirfile_commit_writes(struct tee_fs_dirfile_dirh *dirh,
74 					uint8_t *hash);
75 
76 /**
77  * tee_fs_dirfile_get_tmp() - get a temporary file handle
78  * @dirh:	dirfile handle
79  * @dfh:	returned temporary file handle
80  *
81  * Note, nothing is queued up as changes to the dirfile with this function.
82  */
83 TEE_Result tee_fs_dirfile_get_tmp(struct tee_fs_dirfile_dirh *dirh,
84 				  struct tee_fs_dirfile_fileh *dfh);
85 
86 /**
87  * tee_fs_dirfile_find() - find a file handle
88  * @dirh:	dirfile handle
89  * @uuid:	uuid of requesting TA
90  * @oid:	object id
91  * @oidlen:	length of object id
92  * @dfh:	returned file handle
93  */
94 TEE_Result tee_fs_dirfile_find(struct tee_fs_dirfile_dirh *dirh,
95 			       const TEE_UUID *uuid, const void *oid,
96 			       size_t oidlen, struct tee_fs_dirfile_fileh *dfh);
97 
98 /**
99  * tee_fs_dirfile_fileh_to_fname() - get string representation of file handle
100  * @dfh:	file handle
101  * @fname:	buffer
102  * @fnlen:	length of buffer, updated to used length
103  */
104 TEE_Result tee_fs_dirfile_fileh_to_fname(const struct tee_fs_dirfile_fileh *dfh,
105 					 char *fname, size_t *fnlen);
106 
107 /**
108  * tee_fs_dirfile_rename() - changes/supplies file handle object id
109  * @dirh:	dirfile handle
110  * @uuid:	uuid of requesting TA
111  * @dfh:	file handle
112  * @oid:	object id
113  * @oidlen:	length of object id
114  *
115  * If the supplied object id already is used by another file is that file
116  * removed from the dirfile.
117  */
118 TEE_Result tee_fs_dirfile_rename(struct tee_fs_dirfile_dirh *dirh,
119 				 const TEE_UUID *uuid,
120 				 struct tee_fs_dirfile_fileh *dfh,
121 				 const void *oid, size_t oidlen);
122 
123 /**
124  * tee_fs_dirfile_remove() - remove file
125  * @dirh:	dirfile handle
126  * @dfh:	file handle
127  */
128 TEE_Result tee_fs_dirfile_remove(struct tee_fs_dirfile_dirh *dirh,
129 				 const struct tee_fs_dirfile_fileh *dfh);
130 
131 /**
132  * tee_fs_dirfile_update_hash() - update hash of file handle
133  * @dirh:	filefile handle
134  * @dfh:	file handle
135  */
136 TEE_Result tee_fs_dirfile_update_hash(struct tee_fs_dirfile_dirh *dirh,
137 				      const struct tee_fs_dirfile_fileh *dfh);
138 
139 /**
140  * tee_fs_dirfile_get_next() - get object id of next file
141  * @dirh:	dirfile handle
142  * @uuid:	uuid of requesting TA
143  * @idx:	pointer to index
144  * @oid:	object id
145  * @oidlen:	length of object id
146  *
147  * If @idx contains -1 the first object id is returned, *@idx is updated
148  * with the index of the file.
149  */
150 TEE_Result tee_fs_dirfile_get_next(struct tee_fs_dirfile_dirh *dirh,
151 				   const TEE_UUID *uuid, int *idx, void *oid,
152 				   size_t *oidlen);
153 
154 #endif /*__TEE_FS_DIRFILE_H*/
155