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