1 /* 2 * Copyright (c) 2017, Linaro Limited 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 11 * 2. Redistributions in binary form must reproduce the above copyright notice, 12 * this list of conditions and the following disclaimer in the documentation 13 * and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 16 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 17 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 18 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE 19 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 20 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 21 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 22 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 23 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 24 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 25 * POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #ifndef __TEE_FS_DIRFILE_H 29 #define __TEE_FS_DIRFILE_H 30 31 #include <tee/tee_fs.h> 32 #include <tee/fs_htree.h> 33 34 struct tee_fs_dirfile_dirh; 35 36 /** 37 * struct tee_fs_dirfile_fileh - file handle 38 * @file_number: sequence number of a file 39 * @hash: hash of file, to be supplied to tee_fs_htree_open() 40 * @idx: index of the file handle in the dirfile 41 */ 42 struct tee_fs_dirfile_fileh { 43 uint32_t file_number; 44 uint8_t hash[TEE_FS_HTREE_HASH_SIZE]; 45 int idx; 46 }; 47 48 /** 49 * struct tee_fs_dirfile_operations - file interface supplied by user of this 50 * interface 51 * @open: opens a file 52 * @close: closes a file, changes are discarded unless 53 * @commit_writes is called before 54 * @read: reads from an open file 55 * @write: writes to an open file 56 * @commit_writes: commits changes since the file was opened 57 */ 58 struct tee_fs_dirfile_operations { 59 TEE_Result (*open)(bool create, const TEE_UUID *uuid, 60 struct tee_fs_dirfile_fileh *dfh, 61 struct tee_file_handle **fh); 62 void (*close)(struct tee_file_handle *fh); 63 TEE_Result (*read)(struct tee_file_handle *fh, size_t pos, 64 void *buf, size_t *len); 65 TEE_Result (*write)(struct tee_file_handle *fh, size_t pos, 66 const void *buf, size_t len); 67 TEE_Result (*commit_writes)(struct tee_file_handle *fh, uint8_t *hash); 68 }; 69 70 /** 71 * tee_fs_dirfile_open() - opens a dirfile handle 72 * @uuid: uuid of requesting TA 73 * @fops: file interface 74 * @dirh: returned dirfile handle 75 */ 76 TEE_Result tee_fs_dirfile_open(const TEE_UUID *uuid, 77 const struct tee_fs_dirfile_operations *fops, 78 struct tee_fs_dirfile_dirh **dirh); 79 /** 80 * tee_fs_dirfile_close() - closes a dirfile handle 81 * @dirh: dirfile handle 82 * 83 * All changes since last call to tee_fs_dirfile_commit_writes() are 84 * discarded. 85 */ 86 void tee_fs_dirfile_close(struct tee_fs_dirfile_dirh *dirh); 87 88 /** 89 * tee_fs_dirfile_commit_writes() - commit updates of dirfile 90 * @dirh: dirfile handle 91 */ 92 TEE_Result tee_fs_dirfile_commit_writes(struct tee_fs_dirfile_dirh *dirh); 93 94 /** 95 * tee_fs_dirfile_get_tmp() - get a temporary file handle 96 * @dirh: dirfile handle 97 * @dfh: returned temporary file handle 98 * 99 * Note, nothing is queued up as changes to the dirfile with this function. 100 */ 101 TEE_Result tee_fs_dirfile_get_tmp(struct tee_fs_dirfile_dirh *dirh, 102 struct tee_fs_dirfile_fileh *dfh); 103 104 /** 105 * tee_fs_dirfile_find() - find a file handle 106 * @dirh: dirfile handle 107 * @oid: object id 108 * @oidlen: length of object id 109 * @dfh: returned file handle 110 */ 111 TEE_Result tee_fs_dirfile_find(struct tee_fs_dirfile_dirh *dirh, 112 const void *oid, size_t oidlen, 113 struct tee_fs_dirfile_fileh *dfh); 114 115 /** 116 * tee_fs_dirfile_fileh_to_fname() - get string representation of file handle 117 * @dfh: file handle 118 * @fname: buffer 119 * @fnlen: length of buffer, updated to used length 120 */ 121 TEE_Result tee_fs_dirfile_fileh_to_fname(const struct tee_fs_dirfile_fileh *dfh, 122 char *fname, size_t *fnlen); 123 124 /** 125 * tee_fs_dirfile_rename() - changes/supplies file handle object id 126 * @dirh: dirfile handle 127 * @dfh: file handle 128 * @oid: object id 129 * @oidlen: length of object id 130 * 131 * If the supplied object id already is used by another file is that file 132 * removed from the dirfile. 133 */ 134 TEE_Result tee_fs_dirfile_rename(struct tee_fs_dirfile_dirh *dirh, 135 struct tee_fs_dirfile_fileh *dfh, 136 const void *oid, size_t oidlen); 137 138 /** 139 * tee_fs_dirfile_remove() - remove file 140 * @dirh: dirfile handle 141 * @dfh: file handle 142 */ 143 TEE_Result tee_fs_dirfile_remove(struct tee_fs_dirfile_dirh *dirh, 144 const struct tee_fs_dirfile_fileh *dfh); 145 146 /** 147 * tee_fs_dirfile_update_hash() - update hash of file handle 148 * @dirh: filefile handle 149 * @dfh: file handle 150 */ 151 TEE_Result tee_fs_dirfile_update_hash(struct tee_fs_dirfile_dirh *dirh, 152 const struct tee_fs_dirfile_fileh *dfh); 153 154 /** 155 * tee_fs_dirfile_get_next() - get object id of next file 156 * @dirh: dirfile handle 157 * @idx: pointer to index 158 * @oid: object id 159 * @oidlen: length of object id 160 * 161 * If @idx contains -1 the first object id is returned, *@idx is updated 162 * with the index of the file. 163 */ 164 TEE_Result tee_fs_dirfile_get_next(struct tee_fs_dirfile_dirh *dirh, 165 int *idx, void *oid, size_t *oidlen); 166 167 #endif /*__TEE_FS_DIRFILE_H*/ 168