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, uint32_t min_counter,
38 			   const TEE_UUID *uuid,
39 			   struct tee_fs_dirfile_fileh *dfh,
40 			   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, void *buf,
43 			   size_t *len);
44 	TEE_Result (*write)(struct tee_file_handle *fh, size_t pos,
45 			    const void *buf, size_t len);
46 	TEE_Result (*commit_writes)(struct tee_file_handle *fh, uint8_t *hash,
47 				    uint32_t *counter);
48 };
49 
50 /**
51  * tee_fs_dirfile_open() - opens a dirfile handle
52  * @create:	true if a new dirfile is to be created, else the dirfile
53  *		is read opened and verified
54  * @hash:	hash of underlying file
55  * @min_counter: the smallest accepted value in struct htree_image.counter
56  * @fops:	file interface
57  * @dirh:	returned dirfile handle
58  */
59 TEE_Result tee_fs_dirfile_open(bool create, uint8_t *hash, uint32_t min_counter,
60 			       const struct tee_fs_dirfile_operations *fops,
61 			       struct tee_fs_dirfile_dirh **dirh);
62 /**
63  * tee_fs_dirfile_close() - closes a dirfile handle
64  * @dirh:	dirfile handle
65  *
66  * All changes since last call to tee_fs_dirfile_commit_writes() are
67  * discarded.
68  */
69 void tee_fs_dirfile_close(struct tee_fs_dirfile_dirh *dirh);
70 
71 /**
72  * tee_fs_dirfile_commit_writes() - commit updates of dirfile
73  * @dirh:	dirfile handle
74  * @hash:	hash of underlying file is copied here if not NULL
75  * @counter:	version counter of underlying file is copied here if not NULL
76  */
77 TEE_Result tee_fs_dirfile_commit_writes(struct tee_fs_dirfile_dirh *dirh,
78 					uint8_t *hash, uint32_t *counter);
79 
80 /**
81  * tee_fs_dirfile_get_tmp() - get a temporary file handle
82  * @dirh:	dirfile handle
83  * @dfh:	returned temporary file handle
84  *
85  * Note, nothing is queued up as changes to the dirfile with this function.
86  */
87 TEE_Result tee_fs_dirfile_get_tmp(struct tee_fs_dirfile_dirh *dirh,
88 				  struct tee_fs_dirfile_fileh *dfh);
89 
90 /**
91  * tee_fs_dirfile_find() - find a file handle
92  * @dirh:	dirfile handle
93  * @uuid:	uuid of requesting TA
94  * @oid:	object id
95  * @oidlen:	length of object id
96  * @dfh:	returned file handle
97  */
98 TEE_Result tee_fs_dirfile_find(struct tee_fs_dirfile_dirh *dirh,
99 			       const TEE_UUID *uuid, const void *oid,
100 			       size_t oidlen, struct tee_fs_dirfile_fileh *dfh);
101 
102 /**
103  * tee_fs_dirfile_fileh_to_fname() - get string representation of file handle
104  * @dfh:	file handle
105  * @fname:	buffer
106  * @fnlen:	length of buffer, updated to used length
107  */
108 TEE_Result tee_fs_dirfile_fileh_to_fname(const struct tee_fs_dirfile_fileh *dfh,
109 					 char *fname, size_t *fnlen);
110 
111 /**
112  * tee_fs_dirfile_rename() - changes/supplies file handle object id
113  * @dirh:	dirfile handle
114  * @uuid:	uuid of requesting TA
115  * @dfh:	file handle
116  * @oid:	object id
117  * @oidlen:	length of object id
118  *
119  * If the supplied object id already is used by another file is that file
120  * removed from the dirfile.
121  */
122 TEE_Result tee_fs_dirfile_rename(struct tee_fs_dirfile_dirh *dirh,
123 				 const TEE_UUID *uuid,
124 				 struct tee_fs_dirfile_fileh *dfh,
125 				 const void *oid, size_t oidlen);
126 
127 /**
128  * tee_fs_dirfile_remove() - remove file
129  * @dirh:	dirfile handle
130  * @dfh:	file handle
131  */
132 TEE_Result tee_fs_dirfile_remove(struct tee_fs_dirfile_dirh *dirh,
133 				 const struct tee_fs_dirfile_fileh *dfh);
134 
135 /**
136  * tee_fs_dirfile_update_hash() - update hash of file handle
137  * @dirh:	filefile handle
138  * @dfh:	file handle
139  */
140 TEE_Result tee_fs_dirfile_update_hash(struct tee_fs_dirfile_dirh *dirh,
141 				      const struct tee_fs_dirfile_fileh *dfh);
142 
143 /**
144  * tee_fs_dirfile_get_next() - get object id of next file
145  * @dirh:	dirfile handle
146  * @uuid:	uuid of requesting TA
147  * @idx:	pointer to index
148  * @oid:	object id
149  * @oidlen:	length of object id
150  *
151  * If @idx contains -1 the first object id is returned, *@idx is updated
152  * with the index of the file.
153  */
154 TEE_Result tee_fs_dirfile_get_next(struct tee_fs_dirfile_dirh *dirh,
155 				   const TEE_UUID *uuid, int *idx, void *oid,
156 				   size_t *oidlen);
157 
158 #endif /*__TEE_FS_DIRFILE_H*/
159