Tom Rini | 70df9d6 | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 2 | /* |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 3 | * EFI_FILE_PROTOCOL |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 4 | * |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 5 | * Copyright (c) 2017 Rob Clark |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 6 | */ |
| 7 | |
Heinrich Schuchardt | 955a321 | 2025-01-16 20:26:59 +0100 | [diff] [blame] | 8 | #define LOG_CATEGORY LOGC_EFI |
| 9 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 10 | #include <charset.h> |
| 11 | #include <efi_loader.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 13 | #include <malloc.h> |
Alexander Graf | ab133c7 | 2018-08-08 03:54:32 -0600 | [diff] [blame] | 14 | #include <mapmem.h> |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 15 | #include <fs.h> |
Simon Glass | 655306c | 2020-05-10 11:39:58 -0600 | [diff] [blame] | 16 | #include <part.h> |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 17 | |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 18 | /* GUID for file system information */ |
| 19 | const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID; |
| 20 | |
Heinrich Schuchardt | 0c236c4 | 2019-09-08 10:32:54 +0200 | [diff] [blame] | 21 | /* GUID to obtain the volume label */ |
| 22 | const efi_guid_t efi_system_volume_label_id = EFI_FILE_SYSTEM_VOLUME_LABEL_ID; |
| 23 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 24 | struct file_system { |
| 25 | struct efi_simple_file_system_protocol base; |
| 26 | struct efi_device_path *dp; |
| 27 | struct blk_desc *desc; |
| 28 | int part; |
| 29 | }; |
| 30 | #define to_fs(x) container_of(x, struct file_system, base) |
| 31 | |
| 32 | struct file_handle { |
| 33 | struct efi_file_handle base; |
| 34 | struct file_system *fs; |
| 35 | loff_t offset; /* current file position/cursor */ |
| 36 | int isdir; |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 37 | u64 open_mode; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 38 | |
| 39 | /* for reading a directory: */ |
| 40 | struct fs_dir_stream *dirs; |
| 41 | struct fs_dirent *dent; |
| 42 | |
Gabriel Dalimonte | 7823d75 | 2025-02-17 13:26:46 -0500 | [diff] [blame] | 43 | char *path; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 44 | }; |
| 45 | #define to_fh(x) container_of(x, struct file_handle, base) |
| 46 | |
| 47 | static const struct efi_file_handle efi_file_handle_protocol; |
| 48 | |
| 49 | static char *basename(struct file_handle *fh) |
| 50 | { |
| 51 | char *s = strrchr(fh->path, '/'); |
| 52 | if (s) |
| 53 | return s + 1; |
| 54 | return fh->path; |
| 55 | } |
| 56 | |
| 57 | static int set_blk_dev(struct file_handle *fh) |
| 58 | { |
| 59 | return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part); |
| 60 | } |
| 61 | |
Heinrich Schuchardt | 80f834d | 2018-10-02 05:57:32 +0200 | [diff] [blame] | 62 | /** |
| 63 | * is_dir() - check if file handle points to directory |
| 64 | * |
| 65 | * We assume that set_blk_dev(fh) has been called already. |
| 66 | * |
| 67 | * @fh: file handle |
| 68 | * Return: true if file handle points to a directory |
| 69 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 70 | static int is_dir(struct file_handle *fh) |
| 71 | { |
| 72 | struct fs_dir_stream *dirs; |
| 73 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 74 | dirs = fs_opendir(fh->path); |
| 75 | if (!dirs) |
| 76 | return 0; |
| 77 | |
| 78 | fs_closedir(dirs); |
| 79 | |
| 80 | return 1; |
| 81 | } |
| 82 | |
| 83 | /* |
| 84 | * Normalize a path which may include either back or fwd slashes, |
| 85 | * double slashes, . or .. entries in the path, etc. |
| 86 | */ |
| 87 | static int sanitize_path(char *path) |
| 88 | { |
| 89 | char *p; |
| 90 | |
| 91 | /* backslash to slash: */ |
| 92 | p = path; |
| 93 | while ((p = strchr(p, '\\'))) |
| 94 | *p++ = '/'; |
| 95 | |
| 96 | /* handle double-slashes: */ |
| 97 | p = path; |
| 98 | while ((p = strstr(p, "//"))) { |
| 99 | char *src = p + 1; |
| 100 | memmove(p, src, strlen(src) + 1); |
| 101 | } |
| 102 | |
| 103 | /* handle extra /.'s */ |
| 104 | p = path; |
| 105 | while ((p = strstr(p, "/."))) { |
| 106 | /* |
| 107 | * You'd be tempted to do this *after* handling ".."s |
| 108 | * below to avoid having to check if "/." is start of |
| 109 | * a "/..", but that won't have the correct results.. |
| 110 | * for example, "/foo/./../bar" would get resolved to |
| 111 | * "/foo/bar" if you did these two passes in the other |
| 112 | * order |
| 113 | */ |
| 114 | if (p[2] == '.') { |
| 115 | p += 2; |
| 116 | continue; |
| 117 | } |
| 118 | char *src = p + 2; |
| 119 | memmove(p, src, strlen(src) + 1); |
| 120 | } |
| 121 | |
| 122 | /* handle extra /..'s: */ |
| 123 | p = path; |
| 124 | while ((p = strstr(p, "/.."))) { |
| 125 | char *src = p + 3; |
| 126 | |
| 127 | p--; |
| 128 | |
| 129 | /* find beginning of previous path entry: */ |
| 130 | while (true) { |
| 131 | if (p < path) |
| 132 | return -1; |
| 133 | if (*p == '/') |
| 134 | break; |
| 135 | p--; |
| 136 | } |
| 137 | |
| 138 | memmove(p, src, strlen(src) + 1); |
| 139 | } |
| 140 | |
| 141 | return 0; |
| 142 | } |
| 143 | |
Heinrich Schuchardt | 1af423b | 2018-09-12 19:00:02 +0200 | [diff] [blame] | 144 | /** |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 145 | * efi_create_file() - create file or directory |
| 146 | * |
| 147 | * @fh: file handle |
| 148 | * @attributes: attributes for newly created file |
| 149 | * Returns: 0 for success |
| 150 | */ |
| 151 | static int efi_create_file(struct file_handle *fh, u64 attributes) |
| 152 | { |
| 153 | loff_t actwrite; |
| 154 | void *buffer = &actwrite; |
| 155 | |
| 156 | if (attributes & EFI_FILE_DIRECTORY) |
| 157 | return fs_mkdir(fh->path); |
| 158 | else |
| 159 | return fs_write(fh->path, map_to_sysmem(buffer), 0, 0, |
| 160 | &actwrite); |
| 161 | } |
| 162 | |
| 163 | /** |
Heinrich Schuchardt | 1af423b | 2018-09-12 19:00:02 +0200 | [diff] [blame] | 164 | * file_open() - open a file handle |
| 165 | * |
| 166 | * @fs: file system |
| 167 | * @parent: directory relative to which the file is to be opened |
| 168 | * @file_name: path of the file to be opened. '\', '.', or '..' may |
| 169 | * be used as modifiers. A leading backslash indicates an |
| 170 | * absolute path. |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 171 | * @open_mode: bit mask indicating the access mode (read, write, |
Heinrich Schuchardt | 1af423b | 2018-09-12 19:00:02 +0200 | [diff] [blame] | 172 | * create) |
| 173 | * @attributes: attributes for newly created file |
| 174 | * Returns: handle to the opened file or NULL |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 175 | */ |
| 176 | static struct efi_file_handle *file_open(struct file_system *fs, |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 177 | struct file_handle *parent, u16 *file_name, u64 open_mode, |
AKASHI Takahiro | 0c672b0 | 2018-09-11 15:59:12 +0900 | [diff] [blame] | 178 | u64 attributes) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 179 | { |
| 180 | struct file_handle *fh; |
Gabriel Dalimonte | 7823d75 | 2025-02-17 13:26:46 -0500 | [diff] [blame] | 181 | char *path; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 182 | char f0[MAX_UTF8_PER_UTF16] = {0}; |
| 183 | int plen = 0; |
| 184 | int flen = 0; |
| 185 | |
| 186 | if (file_name) { |
Heinrich Schuchardt | db47c34 | 2019-01-12 12:02:33 +0100 | [diff] [blame] | 187 | utf16_to_utf8((u8 *)f0, file_name, 1); |
| 188 | flen = u16_strlen(file_name); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | /* we could have a parent, but also an absolute path: */ |
| 192 | if (f0[0] == '\\') { |
| 193 | plen = 0; |
| 194 | } else if (parent) { |
| 195 | plen = strlen(parent->path) + 1; |
| 196 | } |
| 197 | |
Gabriel Dalimonte | 7823d75 | 2025-02-17 13:26:46 -0500 | [diff] [blame] | 198 | fh = calloc(1, sizeof(*fh)); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 199 | /* +2 is for null and '/' */ |
Gabriel Dalimonte | 7823d75 | 2025-02-17 13:26:46 -0500 | [diff] [blame] | 200 | path = calloc(1, plen + (flen * MAX_UTF8_PER_UTF16) + 2); |
| 201 | if (!fh || !path) |
| 202 | goto error; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 203 | |
Gabriel Dalimonte | 7823d75 | 2025-02-17 13:26:46 -0500 | [diff] [blame] | 204 | fh->path = path; |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 205 | fh->open_mode = open_mode; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 206 | fh->base = efi_file_handle_protocol; |
| 207 | fh->fs = fs; |
| 208 | |
| 209 | if (parent) { |
| 210 | char *p = fh->path; |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 211 | int exists; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 212 | |
| 213 | if (plen > 0) { |
| 214 | strcpy(p, parent->path); |
| 215 | p += plen - 1; |
| 216 | *p++ = '/'; |
| 217 | } |
| 218 | |
Heinrich Schuchardt | db47c34 | 2019-01-12 12:02:33 +0100 | [diff] [blame] | 219 | utf16_to_utf8((u8 *)p, file_name, flen); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 220 | |
| 221 | if (sanitize_path(fh->path)) |
| 222 | goto error; |
| 223 | |
| 224 | /* check if file exists: */ |
| 225 | if (set_blk_dev(fh)) |
| 226 | goto error; |
| 227 | |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 228 | exists = fs_exists(fh->path); |
Heinrich Schuchardt | 3ab87ac | 2019-02-09 22:23:48 +0100 | [diff] [blame] | 229 | /* fs_exists() calls fs_close(), so open file system again */ |
| 230 | if (set_blk_dev(fh)) |
| 231 | goto error; |
| 232 | |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 233 | if (!exists) { |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 234 | if (!(open_mode & EFI_FILE_MODE_CREATE) || |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 235 | efi_create_file(fh, attributes)) |
| 236 | goto error; |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 237 | if (set_blk_dev(fh)) |
| 238 | goto error; |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 239 | } |
| 240 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 241 | /* figure out if file is a directory: */ |
| 242 | fh->isdir = is_dir(fh); |
| 243 | } else { |
| 244 | fh->isdir = 1; |
| 245 | strcpy(fh->path, ""); |
| 246 | } |
| 247 | |
| 248 | return &fh->base; |
| 249 | |
| 250 | error: |
Gabriel Dalimonte | 7823d75 | 2025-02-17 13:26:46 -0500 | [diff] [blame] | 251 | free(fh->path); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 252 | free(fh); |
| 253 | return NULL; |
| 254 | } |
| 255 | |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 256 | efi_status_t efi_file_open_int(struct efi_file_handle *this, |
| 257 | struct efi_file_handle **new_handle, |
| 258 | u16 *file_name, u64 open_mode, |
| 259 | u64 attributes) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 260 | { |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 261 | struct file_handle *fh = to_fh(this); |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 262 | efi_status_t ret; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 263 | |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 264 | /* Check parameters */ |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 265 | if (!this || !new_handle || !file_name) { |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 266 | ret = EFI_INVALID_PARAMETER; |
| 267 | goto out; |
| 268 | } |
| 269 | if (open_mode != EFI_FILE_MODE_READ && |
| 270 | open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) && |
| 271 | open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | |
| 272 | EFI_FILE_MODE_CREATE)) { |
| 273 | ret = EFI_INVALID_PARAMETER; |
| 274 | goto out; |
| 275 | } |
Heinrich Schuchardt | 662d0be | 2018-09-13 21:31:49 +0200 | [diff] [blame] | 276 | /* |
| 277 | * The UEFI spec requires that attributes are only set in create mode. |
| 278 | * The SCT does not care about this and sets EFI_FILE_DIRECTORY in |
| 279 | * read mode. EDK2 does not check that attributes are zero if not in |
| 280 | * create mode. |
| 281 | * |
| 282 | * So here we only check attributes in create mode and do not check |
| 283 | * that they are zero otherwise. |
| 284 | */ |
| 285 | if ((open_mode & EFI_FILE_MODE_CREATE) && |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 286 | (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) { |
| 287 | ret = EFI_INVALID_PARAMETER; |
| 288 | goto out; |
| 289 | } |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 290 | |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 291 | /* Open file */ |
| 292 | *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes); |
Heinrich Schuchardt | d4fe528 | 2019-04-06 16:36:16 +0200 | [diff] [blame] | 293 | if (*new_handle) { |
| 294 | EFI_PRINT("file handle %p\n", *new_handle); |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 295 | ret = EFI_SUCCESS; |
Heinrich Schuchardt | d4fe528 | 2019-04-06 16:36:16 +0200 | [diff] [blame] | 296 | } else { |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 297 | ret = EFI_NOT_FOUND; |
Heinrich Schuchardt | d4fe528 | 2019-04-06 16:36:16 +0200 | [diff] [blame] | 298 | } |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 299 | out: |
| 300 | return ret; |
| 301 | } |
| 302 | |
| 303 | /** |
Heinrich Schuchardt | b010250 | 2024-09-18 23:37:28 +0200 | [diff] [blame] | 304 | * efi_file_open() - open file synchronously |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 305 | * |
| 306 | * This function implements the Open service of the File Protocol. |
| 307 | * See the UEFI spec for details. |
| 308 | * |
| 309 | * @this: EFI_FILE_PROTOCOL instance |
| 310 | * @new_handle: on return pointer to file handle |
| 311 | * @file_name: file name |
| 312 | * @open_mode: mode to open the file (read, read/write, create/read/write) |
| 313 | * @attributes: attributes for newly created file |
| 314 | */ |
| 315 | static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *this, |
| 316 | struct efi_file_handle **new_handle, |
| 317 | u16 *file_name, u64 open_mode, |
| 318 | u64 attributes) |
| 319 | { |
| 320 | efi_status_t ret; |
| 321 | |
| 322 | EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", this, new_handle, |
| 323 | file_name, open_mode, attributes); |
| 324 | |
| 325 | ret = efi_file_open_int(this, new_handle, file_name, open_mode, |
| 326 | attributes); |
| 327 | |
| 328 | return EFI_EXIT(ret); |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * efi_file_open_ex() - open file asynchronously |
| 333 | * |
| 334 | * This function implements the OpenEx service of the File Protocol. |
| 335 | * See the UEFI spec for details. |
| 336 | * |
| 337 | * @this: EFI_FILE_PROTOCOL instance |
| 338 | * @new_handle: on return pointer to file handle |
| 339 | * @file_name: file name |
| 340 | * @open_mode: mode to open the file (read, read/write, create/read/write) |
| 341 | * @attributes: attributes for newly created file |
| 342 | * @token: transaction token |
| 343 | */ |
| 344 | static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *this, |
| 345 | struct efi_file_handle **new_handle, |
| 346 | u16 *file_name, u64 open_mode, |
| 347 | u64 attributes, |
| 348 | struct efi_file_io_token *token) |
| 349 | { |
| 350 | efi_status_t ret; |
| 351 | |
| 352 | EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu, %p", this, new_handle, |
| 353 | file_name, open_mode, attributes, token); |
| 354 | |
| 355 | if (!token) { |
| 356 | ret = EFI_INVALID_PARAMETER; |
| 357 | goto out; |
| 358 | } |
| 359 | |
| 360 | ret = efi_file_open_int(this, new_handle, file_name, open_mode, |
| 361 | attributes); |
| 362 | |
| 363 | if (ret == EFI_SUCCESS && token->event) { |
| 364 | token->status = EFI_SUCCESS; |
| 365 | efi_signal_event(token->event); |
| 366 | } |
| 367 | |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 368 | out: |
| 369 | return EFI_EXIT(ret); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | static efi_status_t file_close(struct file_handle *fh) |
| 373 | { |
| 374 | fs_closedir(fh->dirs); |
Gabriel Dalimonte | 7823d75 | 2025-02-17 13:26:46 -0500 | [diff] [blame] | 375 | free(fh->path); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 376 | free(fh); |
| 377 | return EFI_SUCCESS; |
| 378 | } |
| 379 | |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 380 | efi_status_t efi_file_close_int(struct efi_file_handle *file) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 381 | { |
| 382 | struct file_handle *fh = to_fh(file); |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 383 | |
| 384 | return file_close(fh); |
| 385 | } |
| 386 | |
| 387 | static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file) |
| 388 | { |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 389 | EFI_ENTRY("%p", file); |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 390 | return EFI_EXIT(efi_file_close_int(file)); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file) |
| 394 | { |
| 395 | struct file_handle *fh = to_fh(file); |
AKASHI Takahiro | 4dd5b9c | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 396 | efi_status_t ret = EFI_SUCCESS; |
| 397 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 398 | EFI_ENTRY("%p", file); |
AKASHI Takahiro | 4dd5b9c | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 399 | |
Heinrich Schuchardt | 646faf3 | 2019-06-17 22:00:13 +0200 | [diff] [blame] | 400 | if (set_blk_dev(fh) || fs_unlink(fh->path)) |
| 401 | ret = EFI_WARN_DELETE_FAILURE; |
AKASHI Takahiro | 4dd5b9c | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 402 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 403 | file_close(fh); |
AKASHI Takahiro | 4dd5b9c | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 404 | return EFI_EXIT(ret); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 405 | } |
| 406 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 407 | /** |
| 408 | * efi_get_file_size() - determine the size of a file |
| 409 | * |
| 410 | * @fh: file handle |
| 411 | * @file_size: pointer to receive file size |
| 412 | * Return: status code |
| 413 | */ |
| 414 | static efi_status_t efi_get_file_size(struct file_handle *fh, |
| 415 | loff_t *file_size) |
| 416 | { |
| 417 | if (set_blk_dev(fh)) |
| 418 | return EFI_DEVICE_ERROR; |
| 419 | |
| 420 | if (fs_size(fh->path, file_size)) |
| 421 | return EFI_DEVICE_ERROR; |
| 422 | |
| 423 | return EFI_SUCCESS; |
| 424 | } |
| 425 | |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 426 | /** |
| 427 | * efi_file_size() - Get the size of a file using an EFI file handle |
| 428 | * |
| 429 | * @fh: EFI file handle |
| 430 | * @size: buffer to fill in the discovered size |
| 431 | * |
| 432 | * Return: size of the file |
| 433 | */ |
| 434 | efi_status_t efi_file_size(struct efi_file_handle *fh, efi_uintn_t *size) |
| 435 | { |
| 436 | struct efi_file_info *info = NULL; |
| 437 | efi_uintn_t bs = 0; |
| 438 | efi_status_t ret; |
| 439 | |
| 440 | *size = 0; |
| 441 | ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs, |
| 442 | info)); |
| 443 | if (ret != EFI_BUFFER_TOO_SMALL) { |
| 444 | ret = EFI_DEVICE_ERROR; |
| 445 | goto out; |
| 446 | } |
| 447 | |
| 448 | info = malloc(bs); |
| 449 | if (!info) { |
| 450 | ret = EFI_OUT_OF_RESOURCES; |
| 451 | goto out; |
| 452 | } |
| 453 | ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs, |
| 454 | info)); |
| 455 | if (ret != EFI_SUCCESS) |
| 456 | goto out; |
| 457 | |
| 458 | *size = info->file_size; |
| 459 | |
| 460 | out: |
| 461 | free(info); |
| 462 | return ret; |
| 463 | } |
| 464 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 465 | static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size, |
| 466 | void *buffer) |
| 467 | { |
| 468 | loff_t actread; |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 469 | efi_status_t ret; |
| 470 | loff_t file_size; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 471 | |
Stefan Sørensen | 6b398d0 | 2020-07-22 09:43:31 +0200 | [diff] [blame] | 472 | if (!buffer) { |
| 473 | ret = EFI_INVALID_PARAMETER; |
| 474 | return ret; |
| 475 | } |
| 476 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 477 | ret = efi_get_file_size(fh, &file_size); |
| 478 | if (ret != EFI_SUCCESS) |
| 479 | return ret; |
| 480 | if (file_size < fh->offset) { |
| 481 | ret = EFI_DEVICE_ERROR; |
| 482 | return ret; |
| 483 | } |
| 484 | |
| 485 | if (set_blk_dev(fh)) |
| 486 | return EFI_DEVICE_ERROR; |
Simon Glass | 849688f | 2018-09-15 00:51:00 -0600 | [diff] [blame] | 487 | if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset, |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 488 | *buffer_size, &actread)) |
| 489 | return EFI_DEVICE_ERROR; |
| 490 | |
| 491 | *buffer_size = actread; |
| 492 | fh->offset += actread; |
| 493 | |
| 494 | return EFI_SUCCESS; |
| 495 | } |
| 496 | |
Heinrich Schuchardt | 69f722b | 2021-05-15 22:41:26 +0200 | [diff] [blame] | 497 | static void rtc2efi(struct efi_time *time, struct rtc_time *tm) |
| 498 | { |
| 499 | memset(time, 0, sizeof(struct efi_time)); |
| 500 | time->year = tm->tm_year; |
| 501 | time->month = tm->tm_mon; |
| 502 | time->day = tm->tm_mday; |
| 503 | time->hour = tm->tm_hour; |
| 504 | time->minute = tm->tm_min; |
| 505 | time->second = tm->tm_sec; |
| 506 | } |
| 507 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 508 | static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size, |
| 509 | void *buffer) |
| 510 | { |
| 511 | struct efi_file_info *info = buffer; |
| 512 | struct fs_dirent *dent; |
Heinrich Schuchardt | da06260 | 2019-09-07 22:34:07 +0200 | [diff] [blame] | 513 | u64 required_size; |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 514 | u16 *dst; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 515 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 516 | if (set_blk_dev(fh)) |
| 517 | return EFI_DEVICE_ERROR; |
| 518 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 519 | if (!fh->dirs) { |
| 520 | assert(fh->offset == 0); |
| 521 | fh->dirs = fs_opendir(fh->path); |
| 522 | if (!fh->dirs) |
| 523 | return EFI_DEVICE_ERROR; |
Heinrich Schuchardt | da06260 | 2019-09-07 22:34:07 +0200 | [diff] [blame] | 524 | fh->dent = NULL; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 525 | } |
| 526 | |
| 527 | /* |
| 528 | * So this is a bit awkward. Since fs layer is stateful and we |
| 529 | * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below |
| 530 | * we might have to return without consuming the dent.. so we |
| 531 | * have to stash it for next call. |
| 532 | */ |
| 533 | if (fh->dent) { |
| 534 | dent = fh->dent; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 535 | } else { |
| 536 | dent = fs_readdir(fh->dirs); |
| 537 | } |
| 538 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 539 | if (!dent) { |
Heinrich Schuchardt | da06260 | 2019-09-07 22:34:07 +0200 | [diff] [blame] | 540 | /* no more files in directory */ |
| 541 | *buffer_size = 0; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 542 | return EFI_SUCCESS; |
| 543 | } |
| 544 | |
| 545 | /* check buffer size: */ |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 546 | required_size = sizeof(*info) + |
| 547 | 2 * (utf8_utf16_strlen(dent->name) + 1); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 548 | if (*buffer_size < required_size) { |
| 549 | *buffer_size = required_size; |
| 550 | fh->dent = dent; |
| 551 | return EFI_BUFFER_TOO_SMALL; |
| 552 | } |
Stefan Sørensen | 6b398d0 | 2020-07-22 09:43:31 +0200 | [diff] [blame] | 553 | if (!buffer) |
| 554 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | da06260 | 2019-09-07 22:34:07 +0200 | [diff] [blame] | 555 | fh->dent = NULL; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 556 | |
| 557 | *buffer_size = required_size; |
| 558 | memset(info, 0, required_size); |
| 559 | |
| 560 | info->size = required_size; |
| 561 | info->file_size = dent->size; |
| 562 | info->physical_size = dent->size; |
Heinrich Schuchardt | 69f722b | 2021-05-15 22:41:26 +0200 | [diff] [blame] | 563 | info->attribute = dent->attr; |
| 564 | rtc2efi(&info->create_time, &dent->create_time); |
| 565 | rtc2efi(&info->modification_time, &dent->change_time); |
| 566 | rtc2efi(&info->last_access_time, &dent->access_time); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 567 | |
| 568 | if (dent->type == FS_DT_DIR) |
| 569 | info->attribute |= EFI_FILE_DIRECTORY; |
| 570 | |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 571 | dst = info->file_name; |
| 572 | utf8_utf16_strcpy(&dst, dent->name); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 573 | |
| 574 | fh->offset++; |
| 575 | |
| 576 | return EFI_SUCCESS; |
| 577 | } |
| 578 | |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 579 | efi_status_t efi_file_read_int(struct efi_file_handle *this, |
| 580 | efi_uintn_t *buffer_size, void *buffer) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 581 | { |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 582 | struct file_handle *fh = to_fh(this); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 583 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 584 | u64 bs; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 585 | |
Peng Fan | ca02db2 | 2021-04-28 21:54:01 +0800 | [diff] [blame] | 586 | if (!this || !buffer_size) |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 587 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 588 | |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 589 | bs = *buffer_size; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 590 | if (fh->isdir) |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 591 | ret = dir_read(fh, &bs, buffer); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 592 | else |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 593 | ret = file_read(fh, &bs, buffer); |
| 594 | if (bs <= SIZE_MAX) |
| 595 | *buffer_size = bs; |
| 596 | else |
| 597 | *buffer_size = SIZE_MAX; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 598 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 599 | return ret; |
| 600 | } |
| 601 | |
| 602 | /** |
| 603 | * efi_file_read() - read file |
| 604 | * |
| 605 | * This function implements the Read() service of the EFI_FILE_PROTOCOL. |
| 606 | * |
| 607 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 608 | * details. |
| 609 | * |
| 610 | * @this: file protocol instance |
| 611 | * @buffer_size: number of bytes to read |
| 612 | * @buffer: read buffer |
| 613 | * Return: status code |
| 614 | */ |
| 615 | static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *this, |
| 616 | efi_uintn_t *buffer_size, void *buffer) |
| 617 | { |
| 618 | efi_status_t ret; |
| 619 | |
| 620 | EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer); |
| 621 | |
| 622 | ret = efi_file_read_int(this, buffer_size, buffer); |
| 623 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 624 | return EFI_EXIT(ret); |
| 625 | } |
| 626 | |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 627 | /** |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 628 | * efi_file_read_ex() - read file asynchonously |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 629 | * |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 630 | * This function implements the ReadEx() service of the EFI_FILE_PROTOCOL. |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 631 | * |
| 632 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 633 | * details. |
| 634 | * |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 635 | * @this: file protocol instance |
| 636 | * @token: transaction token |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 637 | * Return: status code |
| 638 | */ |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 639 | static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *this, |
| 640 | struct efi_file_io_token *token) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 641 | { |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 642 | efi_status_t ret; |
| 643 | |
| 644 | EFI_ENTRY("%p, %p", this, token); |
| 645 | |
| 646 | if (!token) { |
| 647 | ret = EFI_INVALID_PARAMETER; |
| 648 | goto out; |
| 649 | } |
| 650 | |
| 651 | ret = efi_file_read_int(this, &token->buffer_size, token->buffer); |
| 652 | |
| 653 | if (ret == EFI_SUCCESS && token->event) { |
| 654 | token->status = EFI_SUCCESS; |
| 655 | efi_signal_event(token->event); |
| 656 | } |
| 657 | |
| 658 | out: |
| 659 | return EFI_EXIT(ret); |
| 660 | } |
| 661 | |
| 662 | static efi_status_t efi_file_write_int(struct efi_file_handle *this, |
| 663 | efi_uintn_t *buffer_size, void *buffer) |
| 664 | { |
| 665 | struct file_handle *fh = to_fh(this); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 666 | efi_status_t ret = EFI_SUCCESS; |
| 667 | loff_t actwrite; |
| 668 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 669 | if (!this || !buffer_size || !buffer) { |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 670 | ret = EFI_INVALID_PARAMETER; |
| 671 | goto out; |
| 672 | } |
| 673 | if (fh->isdir) { |
| 674 | ret = EFI_UNSUPPORTED; |
| 675 | goto out; |
| 676 | } |
| 677 | if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) { |
| 678 | ret = EFI_ACCESS_DENIED; |
| 679 | goto out; |
| 680 | } |
| 681 | |
| 682 | if (!*buffer_size) |
| 683 | goto out; |
| 684 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 685 | if (set_blk_dev(fh)) { |
| 686 | ret = EFI_DEVICE_ERROR; |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 687 | goto out; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 688 | } |
Simon Glass | 849688f | 2018-09-15 00:51:00 -0600 | [diff] [blame] | 689 | if (fs_write(fh->path, map_to_sysmem(buffer), fh->offset, *buffer_size, |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 690 | &actwrite)) { |
| 691 | ret = EFI_DEVICE_ERROR; |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 692 | goto out; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 693 | } |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 694 | *buffer_size = actwrite; |
| 695 | fh->offset += actwrite; |
| 696 | |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 697 | out: |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 698 | return ret; |
| 699 | } |
| 700 | |
| 701 | /** |
| 702 | * efi_file_write() - write to file |
| 703 | * |
| 704 | * This function implements the Write() service of the EFI_FILE_PROTOCOL. |
| 705 | * |
| 706 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 707 | * details. |
| 708 | * |
| 709 | * @this: file protocol instance |
| 710 | * @buffer_size: number of bytes to write |
| 711 | * @buffer: buffer with the bytes to write |
| 712 | * Return: status code |
| 713 | */ |
| 714 | static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *this, |
| 715 | efi_uintn_t *buffer_size, |
| 716 | void *buffer) |
| 717 | { |
| 718 | efi_status_t ret; |
| 719 | |
| 720 | EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer); |
| 721 | |
| 722 | ret = efi_file_write_int(this, buffer_size, buffer); |
| 723 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 724 | return EFI_EXIT(ret); |
| 725 | } |
| 726 | |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 727 | /** |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 728 | * efi_file_write_ex() - write to file |
| 729 | * |
| 730 | * This function implements the WriteEx() service of the EFI_FILE_PROTOCOL. |
| 731 | * |
| 732 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 733 | * details. |
| 734 | * |
| 735 | * @this: file protocol instance |
| 736 | * @token: transaction token |
| 737 | * Return: status code |
| 738 | */ |
| 739 | static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *this, |
| 740 | struct efi_file_io_token *token) |
| 741 | { |
| 742 | efi_status_t ret; |
| 743 | |
| 744 | EFI_ENTRY("%p, %p", this, token); |
| 745 | |
| 746 | if (!token) { |
| 747 | ret = EFI_INVALID_PARAMETER; |
| 748 | goto out; |
| 749 | } |
| 750 | |
| 751 | ret = efi_file_write_int(this, &token->buffer_size, token->buffer); |
| 752 | |
| 753 | if (ret == EFI_SUCCESS && token->event) { |
| 754 | token->status = EFI_SUCCESS; |
| 755 | efi_signal_event(token->event); |
| 756 | } |
| 757 | |
| 758 | out: |
| 759 | return EFI_EXIT(ret); |
| 760 | } |
| 761 | |
| 762 | /** |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 763 | * efi_file_getpos() - get current position in file |
| 764 | * |
| 765 | * This function implements the GetPosition service of the EFI file protocol. |
| 766 | * See the UEFI spec for details. |
| 767 | * |
| 768 | * @file: file handle |
| 769 | * @pos: pointer to file position |
| 770 | * Return: status code |
| 771 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 772 | static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file, |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 773 | u64 *pos) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 774 | { |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 775 | efi_status_t ret = EFI_SUCCESS; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 776 | struct file_handle *fh = to_fh(file); |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 777 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 778 | EFI_ENTRY("%p, %p", file, pos); |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 779 | |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 780 | if (fh->isdir) { |
| 781 | ret = EFI_UNSUPPORTED; |
| 782 | goto out; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 783 | } |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 784 | |
| 785 | *pos = fh->offset; |
| 786 | out: |
| 787 | return EFI_EXIT(ret); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 788 | } |
| 789 | |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 790 | efi_status_t efi_file_setpos_int(struct efi_file_handle *file, u64 pos) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 791 | { |
| 792 | struct file_handle *fh = to_fh(file); |
| 793 | efi_status_t ret = EFI_SUCCESS; |
| 794 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 795 | if (fh->isdir) { |
| 796 | if (pos != 0) { |
| 797 | ret = EFI_UNSUPPORTED; |
| 798 | goto error; |
| 799 | } |
| 800 | fs_closedir(fh->dirs); |
| 801 | fh->dirs = NULL; |
| 802 | } |
| 803 | |
| 804 | if (pos == ~0ULL) { |
| 805 | loff_t file_size; |
| 806 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 807 | ret = efi_get_file_size(fh, &file_size); |
| 808 | if (ret != EFI_SUCCESS) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 809 | goto error; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 810 | pos = file_size; |
| 811 | } |
| 812 | |
| 813 | fh->offset = pos; |
| 814 | |
| 815 | error: |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 816 | return ret; |
| 817 | } |
| 818 | |
| 819 | /** |
| 820 | * efi_file_setpos() - set current position in file |
| 821 | * |
| 822 | * This function implements the SetPosition service of the EFI file protocol. |
| 823 | * See the UEFI spec for details. |
| 824 | * |
| 825 | * @file: file handle |
| 826 | * @pos: new file position |
| 827 | * Return: status code |
| 828 | */ |
| 829 | static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file, |
| 830 | u64 pos) |
| 831 | { |
| 832 | efi_status_t ret = EFI_SUCCESS; |
| 833 | |
| 834 | EFI_ENTRY("%p, %llu", file, pos); |
| 835 | |
| 836 | ret = efi_file_setpos_int(file, pos); |
| 837 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 838 | return EFI_EXIT(ret); |
| 839 | } |
| 840 | |
| 841 | static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file, |
Heinrich Schuchardt | e86380d | 2018-04-04 15:42:09 +0200 | [diff] [blame] | 842 | const efi_guid_t *info_type, |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 843 | efi_uintn_t *buffer_size, |
| 844 | void *buffer) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 845 | { |
| 846 | struct file_handle *fh = to_fh(file); |
| 847 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 848 | u16 *dst; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 849 | |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 850 | EFI_ENTRY("%p, %pUs, %p, %p", file, info_type, buffer_size, buffer); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 851 | |
Heinrich Schuchardt | c540a0e | 2019-09-08 10:45:31 +0200 | [diff] [blame] | 852 | if (!file || !info_type || !buffer_size || |
| 853 | (*buffer_size && !buffer)) { |
| 854 | ret = EFI_INVALID_PARAMETER; |
| 855 | goto error; |
| 856 | } |
| 857 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 858 | if (!guidcmp(info_type, &efi_file_info_guid)) { |
| 859 | struct efi_file_info *info = buffer; |
| 860 | char *filename = basename(fh); |
| 861 | unsigned int required_size; |
| 862 | loff_t file_size; |
| 863 | |
| 864 | /* check buffer size: */ |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 865 | required_size = sizeof(*info) + |
| 866 | 2 * (utf8_utf16_strlen(filename) + 1); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 867 | if (*buffer_size < required_size) { |
| 868 | *buffer_size = required_size; |
| 869 | ret = EFI_BUFFER_TOO_SMALL; |
| 870 | goto error; |
| 871 | } |
| 872 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 873 | ret = efi_get_file_size(fh, &file_size); |
Heinrich Schuchardt | 6e3f70b | 2024-10-26 08:40:47 +0200 | [diff] [blame] | 874 | if (ret != EFI_SUCCESS) { |
| 875 | if (!fh->isdir) |
| 876 | goto error; |
| 877 | /* |
| 878 | * Some file drivers don't implement fs_size() for |
| 879 | * directories. Use a dummy non-zero value. |
| 880 | */ |
| 881 | file_size = 4096; |
| 882 | ret = EFI_SUCCESS; |
| 883 | } |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 884 | |
| 885 | memset(info, 0, required_size); |
| 886 | |
| 887 | info->size = required_size; |
| 888 | info->file_size = file_size; |
| 889 | info->physical_size = file_size; |
| 890 | |
| 891 | if (fh->isdir) |
| 892 | info->attribute |= EFI_FILE_DIRECTORY; |
| 893 | |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 894 | dst = info->file_name; |
| 895 | utf8_utf16_strcpy(&dst, filename); |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 896 | } else if (!guidcmp(info_type, &efi_file_system_info_guid)) { |
| 897 | struct efi_file_system_info *info = buffer; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 898 | struct disk_partition part; |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 899 | efi_uintn_t required_size; |
| 900 | int r; |
| 901 | |
| 902 | if (fh->fs->part >= 1) |
| 903 | r = part_get_info(fh->fs->desc, fh->fs->part, &part); |
| 904 | else |
| 905 | r = part_get_info_whole_disk(fh->fs->desc, &part); |
| 906 | if (r < 0) { |
| 907 | ret = EFI_DEVICE_ERROR; |
| 908 | goto error; |
| 909 | } |
Heinrich Schuchardt | 0c236c4 | 2019-09-08 10:32:54 +0200 | [diff] [blame] | 910 | required_size = sizeof(*info) + 2; |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 911 | if (*buffer_size < required_size) { |
| 912 | *buffer_size = required_size; |
| 913 | ret = EFI_BUFFER_TOO_SMALL; |
| 914 | goto error; |
| 915 | } |
| 916 | |
| 917 | memset(info, 0, required_size); |
| 918 | |
| 919 | info->size = required_size; |
Heinrich Schuchardt | a821b0c | 2019-12-08 10:02:37 +0100 | [diff] [blame] | 920 | /* |
| 921 | * TODO: We cannot determine if the volume can be written to. |
| 922 | */ |
| 923 | info->read_only = false; |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 924 | info->volume_size = part.size * part.blksz; |
Heinrich Schuchardt | a821b0c | 2019-12-08 10:02:37 +0100 | [diff] [blame] | 925 | /* |
| 926 | * TODO: We currently have no function to determine the free |
| 927 | * space. The volume size is the best upper bound we have. |
| 928 | */ |
| 929 | info->free_space = info->volume_size; |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 930 | info->block_size = part.blksz; |
| 931 | /* |
| 932 | * TODO: The volume label is not available in U-Boot. |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 933 | */ |
Heinrich Schuchardt | 0c236c4 | 2019-09-08 10:32:54 +0200 | [diff] [blame] | 934 | info->volume_label[0] = 0; |
| 935 | } else if (!guidcmp(info_type, &efi_system_volume_label_id)) { |
| 936 | if (*buffer_size < 2) { |
| 937 | *buffer_size = 2; |
| 938 | ret = EFI_BUFFER_TOO_SMALL; |
| 939 | goto error; |
| 940 | } |
| 941 | *(u16 *)buffer = 0; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 942 | } else { |
| 943 | ret = EFI_UNSUPPORTED; |
| 944 | } |
| 945 | |
| 946 | error: |
| 947 | return EFI_EXIT(ret); |
| 948 | } |
| 949 | |
| 950 | static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file, |
Heinrich Schuchardt | e86380d | 2018-04-04 15:42:09 +0200 | [diff] [blame] | 951 | const efi_guid_t *info_type, |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 952 | efi_uintn_t buffer_size, |
| 953 | void *buffer) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 954 | { |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 955 | struct file_handle *fh = to_fh(file); |
| 956 | efi_status_t ret = EFI_UNSUPPORTED; |
Gabriel Dalimonte | 7f77175 | 2025-02-17 13:26:47 -0500 | [diff] [blame] | 957 | char *new_file_name = NULL, *new_path = NULL; |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 958 | |
Heinrich Schuchardt | 282249d | 2022-01-16 14:15:31 +0100 | [diff] [blame] | 959 | EFI_ENTRY("%p, %pUs, %zu, %p", file, info_type, buffer_size, buffer); |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 960 | |
| 961 | if (!guidcmp(info_type, &efi_file_info_guid)) { |
| 962 | struct efi_file_info *info = (struct efi_file_info *)buffer; |
| 963 | char *filename = basename(fh); |
| 964 | char *new_file_name, *pos; |
| 965 | loff_t file_size; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 966 | |
Heinrich Schuchardt | dd106d0 | 2019-09-08 11:37:07 +0200 | [diff] [blame] | 967 | /* The buffer will always contain a file name. */ |
| 968 | if (buffer_size < sizeof(struct efi_file_info) + 2 || |
| 969 | buffer_size < info->size) { |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 970 | ret = EFI_BAD_BUFFER_SIZE; |
| 971 | goto out; |
| 972 | } |
| 973 | /* We cannot change the directory attribute */ |
| 974 | if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) { |
| 975 | ret = EFI_ACCESS_DENIED; |
| 976 | goto out; |
| 977 | } |
| 978 | /* Check for renaming */ |
Heinrich Schuchardt | 344bc07 | 2020-11-10 07:24:16 +0100 | [diff] [blame] | 979 | new_file_name = malloc(utf16_utf8_strlen(info->file_name) + 1); |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 980 | if (!new_file_name) { |
| 981 | ret = EFI_OUT_OF_RESOURCES; |
| 982 | goto out; |
| 983 | } |
| 984 | pos = new_file_name; |
| 985 | utf16_utf8_strcpy(&pos, info->file_name); |
| 986 | if (strcmp(new_file_name, filename)) { |
Gabriel Dalimonte | 7f77175 | 2025-02-17 13:26:47 -0500 | [diff] [blame] | 987 | int dlen; |
| 988 | int rv; |
| 989 | |
| 990 | if (set_blk_dev(fh)) { |
| 991 | ret = EFI_DEVICE_ERROR; |
| 992 | goto out; |
| 993 | } |
| 994 | dlen = filename - fh->path; |
| 995 | new_path = calloc(1, dlen + strlen(new_file_name) + 1); |
| 996 | if (!new_path) { |
| 997 | ret = EFI_OUT_OF_RESOURCES; |
| 998 | goto out; |
| 999 | } |
| 1000 | memcpy(new_path, fh->path, dlen); |
| 1001 | strcpy(new_path + dlen, new_file_name); |
| 1002 | sanitize_path(new_path); |
| 1003 | rv = fs_exists(new_path); |
| 1004 | if (rv) { |
| 1005 | ret = EFI_ACCESS_DENIED; |
| 1006 | goto out; |
| 1007 | } |
| 1008 | /* fs_exists() calls fs_close(), so open file system again */ |
| 1009 | if (set_blk_dev(fh)) { |
| 1010 | ret = EFI_DEVICE_ERROR; |
| 1011 | goto out; |
| 1012 | } |
| 1013 | rv = fs_rename(fh->path, new_path); |
| 1014 | if (rv) { |
| 1015 | ret = EFI_ACCESS_DENIED; |
| 1016 | goto out; |
| 1017 | } |
| 1018 | free(fh->path); |
| 1019 | fh->path = new_path; |
| 1020 | /* Prevent new_path from being freed on out */ |
| 1021 | new_path = NULL; |
| 1022 | ret = EFI_SUCCESS; |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 1023 | } |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 1024 | /* Check for truncation */ |
Heinrich Schuchardt | 6e3f70b | 2024-10-26 08:40:47 +0200 | [diff] [blame] | 1025 | if (!fh->isdir) { |
| 1026 | ret = efi_get_file_size(fh, &file_size); |
| 1027 | if (ret != EFI_SUCCESS) |
| 1028 | goto out; |
| 1029 | if (file_size != info->file_size) { |
| 1030 | /* TODO: we do not support truncation */ |
| 1031 | EFI_PRINT("Truncation not supported\n"); |
| 1032 | ret = EFI_ACCESS_DENIED; |
| 1033 | goto out; |
| 1034 | } |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 1035 | } |
| 1036 | /* |
| 1037 | * We do not care for the other attributes |
| 1038 | * TODO: Support read only |
| 1039 | */ |
| 1040 | ret = EFI_SUCCESS; |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 1041 | } else { |
Heinrich Schuchardt | dd106d0 | 2019-09-08 11:37:07 +0200 | [diff] [blame] | 1042 | /* TODO: We do not support changing the volume label */ |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 1043 | ret = EFI_UNSUPPORTED; |
| 1044 | } |
| 1045 | out: |
Gabriel Dalimonte | 7f77175 | 2025-02-17 13:26:47 -0500 | [diff] [blame] | 1046 | free(new_path); |
| 1047 | free(new_file_name); |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 1048 | return EFI_EXIT(ret); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1049 | } |
| 1050 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 1051 | /** |
| 1052 | * efi_file_flush_int() - flush file |
| 1053 | * |
| 1054 | * This is the internal implementation of the Flush() and FlushEx() services of |
| 1055 | * the EFI_FILE_PROTOCOL. |
| 1056 | * |
| 1057 | * @this: file protocol instance |
| 1058 | * Return: status code |
| 1059 | */ |
| 1060 | static efi_status_t efi_file_flush_int(struct efi_file_handle *this) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1061 | { |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 1062 | struct file_handle *fh = to_fh(this); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1063 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 1064 | if (!this) |
| 1065 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 1066 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 1067 | if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) |
| 1068 | return EFI_ACCESS_DENIED; |
| 1069 | |
| 1070 | /* TODO: flush for file position after end of file */ |
| 1071 | return EFI_SUCCESS; |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 1072 | } |
| 1073 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 1074 | /** |
| 1075 | * efi_file_flush() - flush file |
| 1076 | * |
| 1077 | * This function implements the Flush() service of the EFI_FILE_PROTOCOL. |
| 1078 | * |
| 1079 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1080 | * details. |
| 1081 | * |
| 1082 | * @this: file protocol instance |
| 1083 | * Return: status code |
| 1084 | */ |
| 1085 | static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *this) |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 1086 | { |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 1087 | efi_status_t ret; |
| 1088 | |
| 1089 | EFI_ENTRY("%p", this); |
| 1090 | |
| 1091 | ret = efi_file_flush_int(this); |
| 1092 | |
| 1093 | return EFI_EXIT(ret); |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 1094 | } |
| 1095 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 1096 | /** |
| 1097 | * efi_file_flush_ex() - flush file |
| 1098 | * |
| 1099 | * This function implements the FlushEx() service of the EFI_FILE_PROTOCOL. |
| 1100 | * |
| 1101 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1102 | * details. |
| 1103 | * |
| 1104 | * @this: file protocol instance |
| 1105 | * @token: transaction token |
| 1106 | * Return: status code |
| 1107 | */ |
| 1108 | static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *this, |
| 1109 | struct efi_file_io_token *token) |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 1110 | { |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 1111 | efi_status_t ret; |
| 1112 | |
| 1113 | EFI_ENTRY("%p, %p", this, token); |
| 1114 | |
| 1115 | if (!token) { |
| 1116 | ret = EFI_INVALID_PARAMETER; |
| 1117 | goto out; |
| 1118 | } |
| 1119 | |
| 1120 | ret = efi_file_flush_int(this); |
| 1121 | |
| 1122 | if (ret == EFI_SUCCESS && token->event) { |
| 1123 | token->status = EFI_SUCCESS; |
| 1124 | efi_signal_event(token->event); |
| 1125 | } |
| 1126 | |
| 1127 | out: |
| 1128 | return EFI_EXIT(ret); |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 1129 | } |
| 1130 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1131 | static const struct efi_file_handle efi_file_handle_protocol = { |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 1132 | .rev = EFI_FILE_PROTOCOL_REVISION2, |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1133 | .open = efi_file_open, |
| 1134 | .close = efi_file_close, |
| 1135 | .delete = efi_file_delete, |
| 1136 | .read = efi_file_read, |
| 1137 | .write = efi_file_write, |
| 1138 | .getpos = efi_file_getpos, |
| 1139 | .setpos = efi_file_setpos, |
| 1140 | .getinfo = efi_file_getinfo, |
| 1141 | .setinfo = efi_file_setinfo, |
| 1142 | .flush = efi_file_flush, |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 1143 | .open_ex = efi_file_open_ex, |
| 1144 | .read_ex = efi_file_read_ex, |
| 1145 | .write_ex = efi_file_write_ex, |
| 1146 | .flush_ex = efi_file_flush_ex, |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1147 | }; |
| 1148 | |
Heinrich Schuchardt | c1476c2 | 2019-02-04 21:24:35 +0100 | [diff] [blame] | 1149 | /** |
| 1150 | * efi_file_from_path() - open file via device path |
| 1151 | * |
Heinrich Schuchardt | 4395adb | 2022-11-10 14:14:06 +0100 | [diff] [blame] | 1152 | * The device path @fp consists of the device path of the handle with the |
| 1153 | * simple file system protocol and one or more file path device path nodes. |
| 1154 | * The concatenation of all file path names provides the total file path. |
| 1155 | * |
| 1156 | * The code starts at the first file path node and tries to open that file or |
| 1157 | * directory. If there is a succeding file path node, the code opens it relative |
| 1158 | * to this directory and continues iterating until reaching the last file path |
| 1159 | * node. |
| 1160 | * |
Heinrich Schuchardt | c1476c2 | 2019-02-04 21:24:35 +0100 | [diff] [blame] | 1161 | * @fp: device path |
Heinrich Schuchardt | fbe9021 | 2022-01-20 19:48:20 +0100 | [diff] [blame] | 1162 | * Return: EFI_FILE_PROTOCOL for the file or NULL |
Heinrich Schuchardt | c1476c2 | 2019-02-04 21:24:35 +0100 | [diff] [blame] | 1163 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1164 | struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp) |
| 1165 | { |
| 1166 | struct efi_simple_file_system_protocol *v; |
| 1167 | struct efi_file_handle *f; |
| 1168 | efi_status_t ret; |
| 1169 | |
| 1170 | v = efi_fs_from_path(fp); |
| 1171 | if (!v) |
| 1172 | return NULL; |
| 1173 | |
| 1174 | EFI_CALL(ret = v->open_volume(v, &f)); |
| 1175 | if (ret != EFI_SUCCESS) |
| 1176 | return NULL; |
| 1177 | |
Heinrich Schuchardt | c1476c2 | 2019-02-04 21:24:35 +0100 | [diff] [blame] | 1178 | /* Skip over device-path nodes before the file path. */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1179 | while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) |
| 1180 | fp = efi_dp_next(fp); |
| 1181 | |
Heinrich Schuchardt | c1476c2 | 2019-02-04 21:24:35 +0100 | [diff] [blame] | 1182 | /* |
| 1183 | * Step through the nodes of the directory path until the actual file |
| 1184 | * node is reached which is the final node in the device path. |
| 1185 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1186 | while (fp) { |
| 1187 | struct efi_device_path_file_path *fdp = |
| 1188 | container_of(fp, struct efi_device_path_file_path, dp); |
| 1189 | struct efi_file_handle *f2; |
Heinrich Schuchardt | 2cfcd99 | 2019-07-14 20:14:46 +0200 | [diff] [blame] | 1190 | u16 *filename; |
Ilias Apalodimas | 50279dc | 2022-11-11 20:04:31 +0200 | [diff] [blame] | 1191 | size_t filename_sz; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1192 | |
| 1193 | if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) { |
| 1194 | printf("bad file path!\n"); |
Ilias Apalodimas | 08d1af2 | 2022-11-11 18:20:37 +0200 | [diff] [blame] | 1195 | EFI_CALL(f->close(f)); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1196 | return NULL; |
| 1197 | } |
| 1198 | |
Ilias Apalodimas | 9124bc6 | 2022-11-10 15:31:30 +0200 | [diff] [blame] | 1199 | /* |
| 1200 | * UEFI specification requires pointers that are passed to |
| 1201 | * protocol member functions to be aligned. So memcpy it |
| 1202 | * unconditionally |
| 1203 | */ |
Ilias Apalodimas | 50279dc | 2022-11-11 20:04:31 +0200 | [diff] [blame] | 1204 | if (fdp->dp.length <= offsetof(struct efi_device_path_file_path, str)) |
| 1205 | return NULL; |
| 1206 | filename_sz = fdp->dp.length - |
| 1207 | offsetof(struct efi_device_path_file_path, str); |
| 1208 | filename = malloc(filename_sz); |
Heinrich Schuchardt | 2cfcd99 | 2019-07-14 20:14:46 +0200 | [diff] [blame] | 1209 | if (!filename) |
| 1210 | return NULL; |
Ilias Apalodimas | 50279dc | 2022-11-11 20:04:31 +0200 | [diff] [blame] | 1211 | memcpy(filename, fdp->str, filename_sz); |
Heinrich Schuchardt | 2cfcd99 | 2019-07-14 20:14:46 +0200 | [diff] [blame] | 1212 | EFI_CALL(ret = f->open(f, &f2, filename, |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1213 | EFI_FILE_MODE_READ, 0)); |
Heinrich Schuchardt | 2cfcd99 | 2019-07-14 20:14:46 +0200 | [diff] [blame] | 1214 | free(filename); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1215 | if (ret != EFI_SUCCESS) |
| 1216 | return NULL; |
| 1217 | |
| 1218 | fp = efi_dp_next(fp); |
| 1219 | |
| 1220 | EFI_CALL(f->close(f)); |
| 1221 | f = f2; |
| 1222 | } |
| 1223 | |
| 1224 | return f; |
| 1225 | } |
| 1226 | |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1227 | efi_status_t efi_open_volume_int(struct efi_simple_file_system_protocol *this, |
| 1228 | struct efi_file_handle **root) |
| 1229 | { |
| 1230 | struct file_system *fs = to_fs(this); |
| 1231 | |
| 1232 | *root = file_open(fs, NULL, NULL, 0, 0); |
| 1233 | |
| 1234 | return EFI_SUCCESS; |
| 1235 | } |
| 1236 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1237 | static efi_status_t EFIAPI |
| 1238 | efi_open_volume(struct efi_simple_file_system_protocol *this, |
| 1239 | struct efi_file_handle **root) |
| 1240 | { |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1241 | EFI_ENTRY("%p, %p", this, root); |
| 1242 | |
Masahisa Kojima | c5ff0a0 | 2022-09-12 17:33:50 +0900 | [diff] [blame] | 1243 | return EFI_EXIT(efi_open_volume_int(this, root)); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1244 | } |
| 1245 | |
Heinrich Schuchardt | f9e8ada | 2023-07-30 14:03:53 +0200 | [diff] [blame] | 1246 | efi_status_t |
| 1247 | efi_create_simple_file_system(struct blk_desc *desc, int part, |
| 1248 | struct efi_device_path *dp, |
| 1249 | struct efi_simple_file_system_protocol **fsp) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1250 | { |
| 1251 | struct file_system *fs; |
| 1252 | |
| 1253 | fs = calloc(1, sizeof(*fs)); |
Heinrich Schuchardt | f9e8ada | 2023-07-30 14:03:53 +0200 | [diff] [blame] | 1254 | if (!fs) |
| 1255 | return EFI_OUT_OF_RESOURCES; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1256 | fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION; |
| 1257 | fs->base.open_volume = efi_open_volume; |
| 1258 | fs->desc = desc; |
| 1259 | fs->part = part; |
| 1260 | fs->dp = dp; |
Heinrich Schuchardt | f9e8ada | 2023-07-30 14:03:53 +0200 | [diff] [blame] | 1261 | *fsp = &fs->base; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1262 | |
Heinrich Schuchardt | f9e8ada | 2023-07-30 14:03:53 +0200 | [diff] [blame] | 1263 | return EFI_SUCCESS; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1264 | } |