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