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