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, uint8_t *hash, 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 * @create: true if a new dirfile is to be created, else the dirfile 73 * is read opened and verified 74 * @hash: hash of underlying file 75 * @fops: file interface 76 * @dirh: returned dirfile handle 77 */ 78 TEE_Result tee_fs_dirfile_open(bool create, uint8_t *hash, 79 const struct tee_fs_dirfile_operations *fops, 80 struct tee_fs_dirfile_dirh **dirh); 81 /** 82 * tee_fs_dirfile_close() - closes a dirfile handle 83 * @dirh: dirfile handle 84 * 85 * All changes since last call to tee_fs_dirfile_commit_writes() are 86 * discarded. 87 */ 88 void tee_fs_dirfile_close(struct tee_fs_dirfile_dirh *dirh); 89 90 /** 91 * tee_fs_dirfile_commit_writes() - commit updates of dirfile 92 * @dirh: dirfile handle 93 * @hash: hash of underlying file is copied here if not NULL 94 */ 95 TEE_Result tee_fs_dirfile_commit_writes(struct tee_fs_dirfile_dirh *dirh, 96 uint8_t *hash); 97 98 /** 99 * tee_fs_dirfile_get_tmp() - get a temporary file handle 100 * @dirh: dirfile handle 101 * @dfh: returned temporary file handle 102 * 103 * Note, nothing is queued up as changes to the dirfile with this function. 104 */ 105 TEE_Result tee_fs_dirfile_get_tmp(struct tee_fs_dirfile_dirh *dirh, 106 struct tee_fs_dirfile_fileh *dfh); 107 108 /** 109 * tee_fs_dirfile_find() - find a file handle 110 * @dirh: dirfile handle 111 * @uuid: uuid of requesting TA 112 * @oid: object id 113 * @oidlen: length of object id 114 * @dfh: returned file handle 115 */ 116 TEE_Result tee_fs_dirfile_find(struct tee_fs_dirfile_dirh *dirh, 117 const TEE_UUID *uuid, const void *oid, 118 size_t oidlen, struct tee_fs_dirfile_fileh *dfh); 119 120 /** 121 * tee_fs_dirfile_fileh_to_fname() - get string representation of file handle 122 * @dfh: file handle 123 * @fname: buffer 124 * @fnlen: length of buffer, updated to used length 125 */ 126 TEE_Result tee_fs_dirfile_fileh_to_fname(const struct tee_fs_dirfile_fileh *dfh, 127 char *fname, size_t *fnlen); 128 129 /** 130 * tee_fs_dirfile_rename() - changes/supplies file handle object id 131 * @dirh: dirfile handle 132 * @uuid: uuid of requesting TA 133 * @dfh: file handle 134 * @oid: object id 135 * @oidlen: length of object id 136 * 137 * If the supplied object id already is used by another file is that file 138 * removed from the dirfile. 139 */ 140 TEE_Result tee_fs_dirfile_rename(struct tee_fs_dirfile_dirh *dirh, 141 const TEE_UUID *uuid, 142 struct tee_fs_dirfile_fileh *dfh, 143 const void *oid, size_t oidlen); 144 145 /** 146 * tee_fs_dirfile_remove() - remove file 147 * @dirh: dirfile handle 148 * @dfh: file handle 149 */ 150 TEE_Result tee_fs_dirfile_remove(struct tee_fs_dirfile_dirh *dirh, 151 const struct tee_fs_dirfile_fileh *dfh); 152 153 /** 154 * tee_fs_dirfile_update_hash() - update hash of file handle 155 * @dirh: filefile handle 156 * @dfh: file handle 157 */ 158 TEE_Result tee_fs_dirfile_update_hash(struct tee_fs_dirfile_dirh *dirh, 159 const struct tee_fs_dirfile_fileh *dfh); 160 161 /** 162 * tee_fs_dirfile_get_next() - get object id of next file 163 * @dirh: dirfile handle 164 * @uuid: uuid of requesting TA 165 * @idx: pointer to index 166 * @oid: object id 167 * @oidlen: length of object id 168 * 169 * If @idx contains -1 the first object id is returned, *@idx is updated 170 * with the index of the file. 171 */ 172 TEE_Result tee_fs_dirfile_get_next(struct tee_fs_dirfile_dirh *dirh, 173 const TEE_UUID *uuid, int *idx, void *oid, 174 size_t *oidlen); 175 176 #endif /*__TEE_FS_DIRFILE_H*/ 177