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); |
| 198 | |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 199 | fh->open_mode = open_mode; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 200 | fh->base = efi_file_handle_protocol; |
| 201 | fh->fs = fs; |
| 202 | |
| 203 | if (parent) { |
| 204 | char *p = fh->path; |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 205 | int exists; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 206 | |
| 207 | if (plen > 0) { |
| 208 | strcpy(p, parent->path); |
| 209 | p += plen - 1; |
| 210 | *p++ = '/'; |
| 211 | } |
| 212 | |
Heinrich Schuchardt | db47c34 | 2019-01-12 12:02:33 +0100 | [diff] [blame] | 213 | utf16_to_utf8((u8 *)p, file_name, flen); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 214 | |
| 215 | if (sanitize_path(fh->path)) |
| 216 | goto error; |
| 217 | |
| 218 | /* check if file exists: */ |
| 219 | if (set_blk_dev(fh)) |
| 220 | goto error; |
| 221 | |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 222 | exists = fs_exists(fh->path); |
Heinrich Schuchardt | 3ab87ac | 2019-02-09 22:23:48 +0100 | [diff] [blame] | 223 | /* fs_exists() calls fs_close(), so open file system again */ |
| 224 | if (set_blk_dev(fh)) |
| 225 | goto error; |
| 226 | |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 227 | if (!exists) { |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 228 | if (!(open_mode & EFI_FILE_MODE_CREATE) || |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 229 | efi_create_file(fh, attributes)) |
| 230 | goto error; |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 231 | if (set_blk_dev(fh)) |
| 232 | goto error; |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 233 | } |
| 234 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 235 | /* figure out if file is a directory: */ |
| 236 | fh->isdir = is_dir(fh); |
| 237 | } else { |
| 238 | fh->isdir = 1; |
| 239 | strcpy(fh->path, ""); |
| 240 | } |
| 241 | |
| 242 | return &fh->base; |
| 243 | |
| 244 | error: |
| 245 | free(fh); |
| 246 | return NULL; |
| 247 | } |
| 248 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 249 | static efi_status_t efi_file_open_int(struct efi_file_handle *this, |
| 250 | struct efi_file_handle **new_handle, |
| 251 | u16 *file_name, u64 open_mode, |
| 252 | u64 attributes) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 253 | { |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 254 | struct file_handle *fh = to_fh(this); |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 255 | efi_status_t ret; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 256 | |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 257 | /* Check parameters */ |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 258 | if (!this || !new_handle || !file_name) { |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 259 | ret = EFI_INVALID_PARAMETER; |
| 260 | goto out; |
| 261 | } |
| 262 | if (open_mode != EFI_FILE_MODE_READ && |
| 263 | open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) && |
| 264 | open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | |
| 265 | EFI_FILE_MODE_CREATE)) { |
| 266 | ret = EFI_INVALID_PARAMETER; |
| 267 | goto out; |
| 268 | } |
Heinrich Schuchardt | 662d0be | 2018-09-13 21:31:49 +0200 | [diff] [blame] | 269 | /* |
| 270 | * The UEFI spec requires that attributes are only set in create mode. |
| 271 | * The SCT does not care about this and sets EFI_FILE_DIRECTORY in |
| 272 | * read mode. EDK2 does not check that attributes are zero if not in |
| 273 | * create mode. |
| 274 | * |
| 275 | * So here we only check attributes in create mode and do not check |
| 276 | * that they are zero otherwise. |
| 277 | */ |
| 278 | if ((open_mode & EFI_FILE_MODE_CREATE) && |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 279 | (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) { |
| 280 | ret = EFI_INVALID_PARAMETER; |
| 281 | goto out; |
| 282 | } |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 283 | |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 284 | /* Open file */ |
| 285 | *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes); |
Heinrich Schuchardt | d4fe528 | 2019-04-06 16:36:16 +0200 | [diff] [blame] | 286 | if (*new_handle) { |
| 287 | EFI_PRINT("file handle %p\n", *new_handle); |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 288 | ret = EFI_SUCCESS; |
Heinrich Schuchardt | d4fe528 | 2019-04-06 16:36:16 +0200 | [diff] [blame] | 289 | } else { |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 290 | ret = EFI_NOT_FOUND; |
Heinrich Schuchardt | d4fe528 | 2019-04-06 16:36:16 +0200 | [diff] [blame] | 291 | } |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 292 | out: |
| 293 | return ret; |
| 294 | } |
| 295 | |
| 296 | /** |
| 297 | * efi_file_open_() |
| 298 | * |
| 299 | * This function implements the Open service of the File Protocol. |
| 300 | * See the UEFI spec for details. |
| 301 | * |
| 302 | * @this: EFI_FILE_PROTOCOL instance |
| 303 | * @new_handle: on return pointer to file handle |
| 304 | * @file_name: file name |
| 305 | * @open_mode: mode to open the file (read, read/write, create/read/write) |
| 306 | * @attributes: attributes for newly created file |
| 307 | */ |
| 308 | static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *this, |
| 309 | struct efi_file_handle **new_handle, |
| 310 | u16 *file_name, u64 open_mode, |
| 311 | u64 attributes) |
| 312 | { |
| 313 | efi_status_t ret; |
| 314 | |
| 315 | EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", this, new_handle, |
| 316 | file_name, open_mode, attributes); |
| 317 | |
| 318 | ret = efi_file_open_int(this, new_handle, file_name, open_mode, |
| 319 | attributes); |
| 320 | |
| 321 | return EFI_EXIT(ret); |
| 322 | } |
| 323 | |
| 324 | /** |
| 325 | * efi_file_open_ex() - open file asynchronously |
| 326 | * |
| 327 | * This function implements the OpenEx service of the File Protocol. |
| 328 | * See the UEFI spec for details. |
| 329 | * |
| 330 | * @this: EFI_FILE_PROTOCOL instance |
| 331 | * @new_handle: on return pointer to file handle |
| 332 | * @file_name: file name |
| 333 | * @open_mode: mode to open the file (read, read/write, create/read/write) |
| 334 | * @attributes: attributes for newly created file |
| 335 | * @token: transaction token |
| 336 | */ |
| 337 | static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *this, |
| 338 | struct efi_file_handle **new_handle, |
| 339 | u16 *file_name, u64 open_mode, |
| 340 | u64 attributes, |
| 341 | struct efi_file_io_token *token) |
| 342 | { |
| 343 | efi_status_t ret; |
| 344 | |
| 345 | EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu, %p", this, new_handle, |
| 346 | file_name, open_mode, attributes, token); |
| 347 | |
| 348 | if (!token) { |
| 349 | ret = EFI_INVALID_PARAMETER; |
| 350 | goto out; |
| 351 | } |
| 352 | |
| 353 | ret = efi_file_open_int(this, new_handle, file_name, open_mode, |
| 354 | attributes); |
| 355 | |
| 356 | if (ret == EFI_SUCCESS && token->event) { |
| 357 | token->status = EFI_SUCCESS; |
| 358 | efi_signal_event(token->event); |
| 359 | } |
| 360 | |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 361 | out: |
| 362 | return EFI_EXIT(ret); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 363 | } |
| 364 | |
| 365 | static efi_status_t file_close(struct file_handle *fh) |
| 366 | { |
| 367 | fs_closedir(fh->dirs); |
| 368 | free(fh); |
| 369 | return EFI_SUCCESS; |
| 370 | } |
| 371 | |
| 372 | static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file) |
| 373 | { |
| 374 | struct file_handle *fh = to_fh(file); |
| 375 | EFI_ENTRY("%p", file); |
| 376 | return EFI_EXIT(file_close(fh)); |
| 377 | } |
| 378 | |
| 379 | static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file) |
| 380 | { |
| 381 | struct file_handle *fh = to_fh(file); |
AKASHI Takahiro | 4dd5b9c | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 382 | efi_status_t ret = EFI_SUCCESS; |
| 383 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 384 | EFI_ENTRY("%p", file); |
AKASHI Takahiro | 4dd5b9c | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 385 | |
Heinrich Schuchardt | 646faf3 | 2019-06-17 22:00:13 +0200 | [diff] [blame] | 386 | if (set_blk_dev(fh) || fs_unlink(fh->path)) |
| 387 | ret = EFI_WARN_DELETE_FAILURE; |
AKASHI Takahiro | 4dd5b9c | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 388 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 389 | file_close(fh); |
AKASHI Takahiro | 4dd5b9c | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 390 | return EFI_EXIT(ret); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 391 | } |
| 392 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 393 | /** |
| 394 | * efi_get_file_size() - determine the size of a file |
| 395 | * |
| 396 | * @fh: file handle |
| 397 | * @file_size: pointer to receive file size |
| 398 | * Return: status code |
| 399 | */ |
| 400 | static efi_status_t efi_get_file_size(struct file_handle *fh, |
| 401 | loff_t *file_size) |
| 402 | { |
| 403 | if (set_blk_dev(fh)) |
| 404 | return EFI_DEVICE_ERROR; |
| 405 | |
| 406 | if (fs_size(fh->path, file_size)) |
| 407 | return EFI_DEVICE_ERROR; |
| 408 | |
| 409 | return EFI_SUCCESS; |
| 410 | } |
| 411 | |
Ilias Apalodimas | aa0f755 | 2021-03-17 21:54:59 +0200 | [diff] [blame] | 412 | /** |
| 413 | * efi_file_size() - Get the size of a file using an EFI file handle |
| 414 | * |
| 415 | * @fh: EFI file handle |
| 416 | * @size: buffer to fill in the discovered size |
| 417 | * |
| 418 | * Return: size of the file |
| 419 | */ |
| 420 | efi_status_t efi_file_size(struct efi_file_handle *fh, efi_uintn_t *size) |
| 421 | { |
| 422 | struct efi_file_info *info = NULL; |
| 423 | efi_uintn_t bs = 0; |
| 424 | efi_status_t ret; |
| 425 | |
| 426 | *size = 0; |
| 427 | ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs, |
| 428 | info)); |
| 429 | if (ret != EFI_BUFFER_TOO_SMALL) { |
| 430 | ret = EFI_DEVICE_ERROR; |
| 431 | goto out; |
| 432 | } |
| 433 | |
| 434 | info = malloc(bs); |
| 435 | if (!info) { |
| 436 | ret = EFI_OUT_OF_RESOURCES; |
| 437 | goto out; |
| 438 | } |
| 439 | ret = EFI_CALL(fh->getinfo(fh, (efi_guid_t *)&efi_file_info_guid, &bs, |
| 440 | info)); |
| 441 | if (ret != EFI_SUCCESS) |
| 442 | goto out; |
| 443 | |
| 444 | *size = info->file_size; |
| 445 | |
| 446 | out: |
| 447 | free(info); |
| 448 | return ret; |
| 449 | } |
| 450 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 451 | static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size, |
| 452 | void *buffer) |
| 453 | { |
| 454 | loff_t actread; |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 455 | efi_status_t ret; |
| 456 | loff_t file_size; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 457 | |
Stefan Sørensen | 6b398d0 | 2020-07-22 09:43:31 +0200 | [diff] [blame] | 458 | if (!buffer) { |
| 459 | ret = EFI_INVALID_PARAMETER; |
| 460 | return ret; |
| 461 | } |
| 462 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 463 | ret = efi_get_file_size(fh, &file_size); |
| 464 | if (ret != EFI_SUCCESS) |
| 465 | return ret; |
| 466 | if (file_size < fh->offset) { |
| 467 | ret = EFI_DEVICE_ERROR; |
| 468 | return ret; |
| 469 | } |
| 470 | |
| 471 | if (set_blk_dev(fh)) |
| 472 | return EFI_DEVICE_ERROR; |
Simon Glass | 849688f | 2018-09-15 00:51:00 -0600 | [diff] [blame] | 473 | if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset, |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 474 | *buffer_size, &actread)) |
| 475 | return EFI_DEVICE_ERROR; |
| 476 | |
| 477 | *buffer_size = actread; |
| 478 | fh->offset += actread; |
| 479 | |
| 480 | return EFI_SUCCESS; |
| 481 | } |
| 482 | |
| 483 | static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size, |
| 484 | void *buffer) |
| 485 | { |
| 486 | struct efi_file_info *info = buffer; |
| 487 | struct fs_dirent *dent; |
Heinrich Schuchardt | da06260 | 2019-09-07 22:34:07 +0200 | [diff] [blame] | 488 | u64 required_size; |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 489 | u16 *dst; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 490 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 491 | if (set_blk_dev(fh)) |
| 492 | return EFI_DEVICE_ERROR; |
| 493 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 494 | if (!fh->dirs) { |
| 495 | assert(fh->offset == 0); |
| 496 | fh->dirs = fs_opendir(fh->path); |
| 497 | if (!fh->dirs) |
| 498 | return EFI_DEVICE_ERROR; |
Heinrich Schuchardt | da06260 | 2019-09-07 22:34:07 +0200 | [diff] [blame] | 499 | fh->dent = NULL; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 500 | } |
| 501 | |
| 502 | /* |
| 503 | * So this is a bit awkward. Since fs layer is stateful and we |
| 504 | * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below |
| 505 | * we might have to return without consuming the dent.. so we |
| 506 | * have to stash it for next call. |
| 507 | */ |
| 508 | if (fh->dent) { |
| 509 | dent = fh->dent; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 510 | } else { |
| 511 | dent = fs_readdir(fh->dirs); |
| 512 | } |
| 513 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 514 | if (!dent) { |
Heinrich Schuchardt | da06260 | 2019-09-07 22:34:07 +0200 | [diff] [blame] | 515 | /* no more files in directory */ |
| 516 | *buffer_size = 0; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 517 | return EFI_SUCCESS; |
| 518 | } |
| 519 | |
| 520 | /* check buffer size: */ |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 521 | required_size = sizeof(*info) + |
| 522 | 2 * (utf8_utf16_strlen(dent->name) + 1); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 523 | if (*buffer_size < required_size) { |
| 524 | *buffer_size = required_size; |
| 525 | fh->dent = dent; |
| 526 | return EFI_BUFFER_TOO_SMALL; |
| 527 | } |
Stefan Sørensen | 6b398d0 | 2020-07-22 09:43:31 +0200 | [diff] [blame] | 528 | if (!buffer) |
| 529 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | da06260 | 2019-09-07 22:34:07 +0200 | [diff] [blame] | 530 | fh->dent = NULL; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 531 | |
| 532 | *buffer_size = required_size; |
| 533 | memset(info, 0, required_size); |
| 534 | |
| 535 | info->size = required_size; |
| 536 | info->file_size = dent->size; |
| 537 | info->physical_size = dent->size; |
| 538 | |
| 539 | if (dent->type == FS_DT_DIR) |
| 540 | info->attribute |= EFI_FILE_DIRECTORY; |
| 541 | |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 542 | dst = info->file_name; |
| 543 | utf8_utf16_strcpy(&dst, dent->name); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 544 | |
| 545 | fh->offset++; |
| 546 | |
| 547 | return EFI_SUCCESS; |
| 548 | } |
| 549 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 550 | static efi_status_t efi_file_read_int(struct efi_file_handle *this, |
| 551 | efi_uintn_t *buffer_size, void *buffer) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 552 | { |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 553 | struct file_handle *fh = to_fh(this); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 554 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 555 | u64 bs; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 556 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 557 | if (!this || !buffer_size || !buffer) |
| 558 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 559 | |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 560 | bs = *buffer_size; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 561 | if (fh->isdir) |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 562 | ret = dir_read(fh, &bs, buffer); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 563 | else |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 564 | ret = file_read(fh, &bs, buffer); |
| 565 | if (bs <= SIZE_MAX) |
| 566 | *buffer_size = bs; |
| 567 | else |
| 568 | *buffer_size = SIZE_MAX; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 569 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 570 | return ret; |
| 571 | } |
| 572 | |
| 573 | /** |
| 574 | * efi_file_read() - read file |
| 575 | * |
| 576 | * This function implements the Read() service of the EFI_FILE_PROTOCOL. |
| 577 | * |
| 578 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 579 | * details. |
| 580 | * |
| 581 | * @this: file protocol instance |
| 582 | * @buffer_size: number of bytes to read |
| 583 | * @buffer: read buffer |
| 584 | * Return: status code |
| 585 | */ |
| 586 | static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *this, |
| 587 | efi_uintn_t *buffer_size, void *buffer) |
| 588 | { |
| 589 | efi_status_t ret; |
| 590 | |
| 591 | EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer); |
| 592 | |
| 593 | ret = efi_file_read_int(this, buffer_size, buffer); |
| 594 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 595 | return EFI_EXIT(ret); |
| 596 | } |
| 597 | |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 598 | /** |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 599 | * efi_file_read_ex() - read file asynchonously |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 600 | * |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 601 | * This function implements the ReadEx() service of the EFI_FILE_PROTOCOL. |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 602 | * |
| 603 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 604 | * details. |
| 605 | * |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 606 | * @this: file protocol instance |
| 607 | * @token: transaction token |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 608 | * Return: status code |
| 609 | */ |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 610 | static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *this, |
| 611 | struct efi_file_io_token *token) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 612 | { |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 613 | efi_status_t ret; |
| 614 | |
| 615 | EFI_ENTRY("%p, %p", this, token); |
| 616 | |
| 617 | if (!token) { |
| 618 | ret = EFI_INVALID_PARAMETER; |
| 619 | goto out; |
| 620 | } |
| 621 | |
| 622 | ret = efi_file_read_int(this, &token->buffer_size, token->buffer); |
| 623 | |
| 624 | if (ret == EFI_SUCCESS && token->event) { |
| 625 | token->status = EFI_SUCCESS; |
| 626 | efi_signal_event(token->event); |
| 627 | } |
| 628 | |
| 629 | out: |
| 630 | return EFI_EXIT(ret); |
| 631 | } |
| 632 | |
| 633 | static efi_status_t efi_file_write_int(struct efi_file_handle *this, |
| 634 | efi_uintn_t *buffer_size, void *buffer) |
| 635 | { |
| 636 | struct file_handle *fh = to_fh(this); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 637 | efi_status_t ret = EFI_SUCCESS; |
| 638 | loff_t actwrite; |
| 639 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 640 | if (!this || !buffer_size || !buffer) { |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 641 | ret = EFI_INVALID_PARAMETER; |
| 642 | goto out; |
| 643 | } |
| 644 | if (fh->isdir) { |
| 645 | ret = EFI_UNSUPPORTED; |
| 646 | goto out; |
| 647 | } |
| 648 | if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) { |
| 649 | ret = EFI_ACCESS_DENIED; |
| 650 | goto out; |
| 651 | } |
| 652 | |
| 653 | if (!*buffer_size) |
| 654 | goto out; |
| 655 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 656 | if (set_blk_dev(fh)) { |
| 657 | ret = EFI_DEVICE_ERROR; |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 658 | goto out; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 659 | } |
Simon Glass | 849688f | 2018-09-15 00:51:00 -0600 | [diff] [blame] | 660 | 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] | 661 | &actwrite)) { |
| 662 | ret = EFI_DEVICE_ERROR; |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 663 | goto out; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 664 | } |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 665 | *buffer_size = actwrite; |
| 666 | fh->offset += actwrite; |
| 667 | |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 668 | out: |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 669 | return ret; |
| 670 | } |
| 671 | |
| 672 | /** |
| 673 | * efi_file_write() - write to file |
| 674 | * |
| 675 | * This function implements the Write() service of the EFI_FILE_PROTOCOL. |
| 676 | * |
| 677 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 678 | * details. |
| 679 | * |
| 680 | * @this: file protocol instance |
| 681 | * @buffer_size: number of bytes to write |
| 682 | * @buffer: buffer with the bytes to write |
| 683 | * Return: status code |
| 684 | */ |
| 685 | static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *this, |
| 686 | efi_uintn_t *buffer_size, |
| 687 | void *buffer) |
| 688 | { |
| 689 | efi_status_t ret; |
| 690 | |
| 691 | EFI_ENTRY("%p, %p, %p", this, buffer_size, buffer); |
| 692 | |
| 693 | ret = efi_file_write_int(this, buffer_size, buffer); |
| 694 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 695 | return EFI_EXIT(ret); |
| 696 | } |
| 697 | |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 698 | /** |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 699 | * efi_file_write_ex() - write to file |
| 700 | * |
| 701 | * This function implements the WriteEx() service of the EFI_FILE_PROTOCOL. |
| 702 | * |
| 703 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 704 | * details. |
| 705 | * |
| 706 | * @this: file protocol instance |
| 707 | * @token: transaction token |
| 708 | * Return: status code |
| 709 | */ |
| 710 | static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *this, |
| 711 | struct efi_file_io_token *token) |
| 712 | { |
| 713 | efi_status_t ret; |
| 714 | |
| 715 | EFI_ENTRY("%p, %p", this, token); |
| 716 | |
| 717 | if (!token) { |
| 718 | ret = EFI_INVALID_PARAMETER; |
| 719 | goto out; |
| 720 | } |
| 721 | |
| 722 | ret = efi_file_write_int(this, &token->buffer_size, token->buffer); |
| 723 | |
| 724 | if (ret == EFI_SUCCESS && token->event) { |
| 725 | token->status = EFI_SUCCESS; |
| 726 | efi_signal_event(token->event); |
| 727 | } |
| 728 | |
| 729 | out: |
| 730 | return EFI_EXIT(ret); |
| 731 | } |
| 732 | |
| 733 | /** |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 734 | * efi_file_getpos() - get current position in file |
| 735 | * |
| 736 | * This function implements the GetPosition service of the EFI file protocol. |
| 737 | * See the UEFI spec for details. |
| 738 | * |
| 739 | * @file: file handle |
| 740 | * @pos: pointer to file position |
| 741 | * Return: status code |
| 742 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 743 | 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] | 744 | u64 *pos) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 745 | { |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 746 | efi_status_t ret = EFI_SUCCESS; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 747 | struct file_handle *fh = to_fh(file); |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 748 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 749 | EFI_ENTRY("%p, %p", file, pos); |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 750 | |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 751 | if (fh->isdir) { |
| 752 | ret = EFI_UNSUPPORTED; |
| 753 | goto out; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 754 | } |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 755 | |
| 756 | *pos = fh->offset; |
| 757 | out: |
| 758 | return EFI_EXIT(ret); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 759 | } |
| 760 | |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 761 | /** |
| 762 | * efi_file_setpos() - set current position in file |
| 763 | * |
| 764 | * This function implements the SetPosition service of the EFI file protocol. |
| 765 | * See the UEFI spec for details. |
| 766 | * |
| 767 | * @file: file handle |
| 768 | * @pos: new file position |
| 769 | * Return: status code |
| 770 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 771 | static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file, |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 772 | u64 pos) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 773 | { |
| 774 | struct file_handle *fh = to_fh(file); |
| 775 | efi_status_t ret = EFI_SUCCESS; |
| 776 | |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 777 | EFI_ENTRY("%p, %llu", file, pos); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 778 | |
| 779 | if (fh->isdir) { |
| 780 | if (pos != 0) { |
| 781 | ret = EFI_UNSUPPORTED; |
| 782 | goto error; |
| 783 | } |
| 784 | fs_closedir(fh->dirs); |
| 785 | fh->dirs = NULL; |
| 786 | } |
| 787 | |
| 788 | if (pos == ~0ULL) { |
| 789 | loff_t file_size; |
| 790 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 791 | ret = efi_get_file_size(fh, &file_size); |
| 792 | if (ret != EFI_SUCCESS) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 793 | goto error; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 794 | pos = file_size; |
| 795 | } |
| 796 | |
| 797 | fh->offset = pos; |
| 798 | |
| 799 | error: |
| 800 | return EFI_EXIT(ret); |
| 801 | } |
| 802 | |
| 803 | 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] | 804 | const efi_guid_t *info_type, |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 805 | efi_uintn_t *buffer_size, |
| 806 | void *buffer) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 807 | { |
| 808 | struct file_handle *fh = to_fh(file); |
| 809 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 810 | u16 *dst; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 811 | |
Heinrich Schuchardt | e8c99b0 | 2018-09-14 22:46:34 +0200 | [diff] [blame] | 812 | EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 813 | |
Heinrich Schuchardt | c540a0e | 2019-09-08 10:45:31 +0200 | [diff] [blame] | 814 | if (!file || !info_type || !buffer_size || |
| 815 | (*buffer_size && !buffer)) { |
| 816 | ret = EFI_INVALID_PARAMETER; |
| 817 | goto error; |
| 818 | } |
| 819 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 820 | if (!guidcmp(info_type, &efi_file_info_guid)) { |
| 821 | struct efi_file_info *info = buffer; |
| 822 | char *filename = basename(fh); |
| 823 | unsigned int required_size; |
| 824 | loff_t file_size; |
| 825 | |
| 826 | /* check buffer size: */ |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 827 | required_size = sizeof(*info) + |
| 828 | 2 * (utf8_utf16_strlen(filename) + 1); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 829 | if (*buffer_size < required_size) { |
| 830 | *buffer_size = required_size; |
| 831 | ret = EFI_BUFFER_TOO_SMALL; |
| 832 | goto error; |
| 833 | } |
| 834 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 835 | ret = efi_get_file_size(fh, &file_size); |
| 836 | if (ret != EFI_SUCCESS) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 837 | goto error; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 838 | |
| 839 | memset(info, 0, required_size); |
| 840 | |
| 841 | info->size = required_size; |
| 842 | info->file_size = file_size; |
| 843 | info->physical_size = file_size; |
| 844 | |
| 845 | if (fh->isdir) |
| 846 | info->attribute |= EFI_FILE_DIRECTORY; |
| 847 | |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 848 | dst = info->file_name; |
| 849 | utf8_utf16_strcpy(&dst, filename); |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 850 | } else if (!guidcmp(info_type, &efi_file_system_info_guid)) { |
| 851 | struct efi_file_system_info *info = buffer; |
Simon Glass | c1c4a8f | 2020-05-10 11:39:57 -0600 | [diff] [blame] | 852 | struct disk_partition part; |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 853 | efi_uintn_t required_size; |
| 854 | int r; |
| 855 | |
| 856 | if (fh->fs->part >= 1) |
| 857 | r = part_get_info(fh->fs->desc, fh->fs->part, &part); |
| 858 | else |
| 859 | r = part_get_info_whole_disk(fh->fs->desc, &part); |
| 860 | if (r < 0) { |
| 861 | ret = EFI_DEVICE_ERROR; |
| 862 | goto error; |
| 863 | } |
Heinrich Schuchardt | 0c236c4 | 2019-09-08 10:32:54 +0200 | [diff] [blame] | 864 | required_size = sizeof(*info) + 2; |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 865 | if (*buffer_size < required_size) { |
| 866 | *buffer_size = required_size; |
| 867 | ret = EFI_BUFFER_TOO_SMALL; |
| 868 | goto error; |
| 869 | } |
| 870 | |
| 871 | memset(info, 0, required_size); |
| 872 | |
| 873 | info->size = required_size; |
Heinrich Schuchardt | a821b0c | 2019-12-08 10:02:37 +0100 | [diff] [blame] | 874 | /* |
| 875 | * TODO: We cannot determine if the volume can be written to. |
| 876 | */ |
| 877 | info->read_only = false; |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 878 | info->volume_size = part.size * part.blksz; |
Heinrich Schuchardt | a821b0c | 2019-12-08 10:02:37 +0100 | [diff] [blame] | 879 | /* |
| 880 | * TODO: We currently have no function to determine the free |
| 881 | * space. The volume size is the best upper bound we have. |
| 882 | */ |
| 883 | info->free_space = info->volume_size; |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 884 | info->block_size = part.blksz; |
| 885 | /* |
| 886 | * TODO: The volume label is not available in U-Boot. |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 887 | */ |
Heinrich Schuchardt | 0c236c4 | 2019-09-08 10:32:54 +0200 | [diff] [blame] | 888 | info->volume_label[0] = 0; |
| 889 | } else if (!guidcmp(info_type, &efi_system_volume_label_id)) { |
| 890 | if (*buffer_size < 2) { |
| 891 | *buffer_size = 2; |
| 892 | ret = EFI_BUFFER_TOO_SMALL; |
| 893 | goto error; |
| 894 | } |
| 895 | *(u16 *)buffer = 0; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 896 | } else { |
| 897 | ret = EFI_UNSUPPORTED; |
| 898 | } |
| 899 | |
| 900 | error: |
| 901 | return EFI_EXIT(ret); |
| 902 | } |
| 903 | |
| 904 | 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] | 905 | const efi_guid_t *info_type, |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 906 | efi_uintn_t buffer_size, |
| 907 | void *buffer) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 908 | { |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 909 | struct file_handle *fh = to_fh(file); |
| 910 | efi_status_t ret = EFI_UNSUPPORTED; |
| 911 | |
| 912 | EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer); |
| 913 | |
| 914 | if (!guidcmp(info_type, &efi_file_info_guid)) { |
| 915 | struct efi_file_info *info = (struct efi_file_info *)buffer; |
| 916 | char *filename = basename(fh); |
| 917 | char *new_file_name, *pos; |
| 918 | loff_t file_size; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 919 | |
Heinrich Schuchardt | dd106d0 | 2019-09-08 11:37:07 +0200 | [diff] [blame] | 920 | /* The buffer will always contain a file name. */ |
| 921 | if (buffer_size < sizeof(struct efi_file_info) + 2 || |
| 922 | buffer_size < info->size) { |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 923 | ret = EFI_BAD_BUFFER_SIZE; |
| 924 | goto out; |
| 925 | } |
| 926 | /* We cannot change the directory attribute */ |
| 927 | if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) { |
| 928 | ret = EFI_ACCESS_DENIED; |
| 929 | goto out; |
| 930 | } |
| 931 | /* Check for renaming */ |
Heinrich Schuchardt | 344bc07 | 2020-11-10 07:24:16 +0100 | [diff] [blame] | 932 | new_file_name = malloc(utf16_utf8_strlen(info->file_name) + 1); |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 933 | if (!new_file_name) { |
| 934 | ret = EFI_OUT_OF_RESOURCES; |
| 935 | goto out; |
| 936 | } |
| 937 | pos = new_file_name; |
| 938 | utf16_utf8_strcpy(&pos, info->file_name); |
| 939 | if (strcmp(new_file_name, filename)) { |
| 940 | /* TODO: we do not support renaming */ |
| 941 | EFI_PRINT("Renaming not supported\n"); |
| 942 | free(new_file_name); |
| 943 | ret = EFI_ACCESS_DENIED; |
| 944 | goto out; |
| 945 | } |
| 946 | free(new_file_name); |
| 947 | /* Check for truncation */ |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 948 | ret = efi_get_file_size(fh, &file_size); |
| 949 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 950 | goto out; |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 951 | if (file_size != info->file_size) { |
| 952 | /* TODO: we do not support truncation */ |
| 953 | EFI_PRINT("Truncation not supported\n"); |
| 954 | ret = EFI_ACCESS_DENIED; |
| 955 | goto out; |
| 956 | } |
| 957 | /* |
| 958 | * We do not care for the other attributes |
| 959 | * TODO: Support read only |
| 960 | */ |
| 961 | ret = EFI_SUCCESS; |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 962 | } else { |
Heinrich Schuchardt | dd106d0 | 2019-09-08 11:37:07 +0200 | [diff] [blame] | 963 | /* TODO: We do not support changing the volume label */ |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 964 | ret = EFI_UNSUPPORTED; |
| 965 | } |
| 966 | out: |
| 967 | return EFI_EXIT(ret); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 968 | } |
| 969 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 970 | /** |
| 971 | * efi_file_flush_int() - flush file |
| 972 | * |
| 973 | * This is the internal implementation of the Flush() and FlushEx() services of |
| 974 | * the EFI_FILE_PROTOCOL. |
| 975 | * |
| 976 | * @this: file protocol instance |
| 977 | * Return: status code |
| 978 | */ |
| 979 | 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] | 980 | { |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 981 | struct file_handle *fh = to_fh(this); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 982 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 983 | if (!this) |
| 984 | return EFI_INVALID_PARAMETER; |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 985 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 986 | if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) |
| 987 | return EFI_ACCESS_DENIED; |
| 988 | |
| 989 | /* TODO: flush for file position after end of file */ |
| 990 | return EFI_SUCCESS; |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 991 | } |
| 992 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 993 | /** |
| 994 | * efi_file_flush() - flush file |
| 995 | * |
| 996 | * This function implements the Flush() service of the EFI_FILE_PROTOCOL. |
| 997 | * |
| 998 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 999 | * details. |
| 1000 | * |
| 1001 | * @this: file protocol instance |
| 1002 | * Return: status code |
| 1003 | */ |
| 1004 | 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] | 1005 | { |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 1006 | efi_status_t ret; |
| 1007 | |
| 1008 | EFI_ENTRY("%p", this); |
| 1009 | |
| 1010 | ret = efi_file_flush_int(this); |
| 1011 | |
| 1012 | return EFI_EXIT(ret); |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 1013 | } |
| 1014 | |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 1015 | /** |
| 1016 | * efi_file_flush_ex() - flush file |
| 1017 | * |
| 1018 | * This function implements the FlushEx() service of the EFI_FILE_PROTOCOL. |
| 1019 | * |
| 1020 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 1021 | * details. |
| 1022 | * |
| 1023 | * @this: file protocol instance |
| 1024 | * @token: transaction token |
| 1025 | * Return: status code |
| 1026 | */ |
| 1027 | static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *this, |
| 1028 | struct efi_file_io_token *token) |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 1029 | { |
Heinrich Schuchardt | e759b57 | 2021-01-01 08:39:43 +0100 | [diff] [blame] | 1030 | efi_status_t ret; |
| 1031 | |
| 1032 | EFI_ENTRY("%p, %p", this, token); |
| 1033 | |
| 1034 | if (!token) { |
| 1035 | ret = EFI_INVALID_PARAMETER; |
| 1036 | goto out; |
| 1037 | } |
| 1038 | |
| 1039 | ret = efi_file_flush_int(this); |
| 1040 | |
| 1041 | if (ret == EFI_SUCCESS && token->event) { |
| 1042 | token->status = EFI_SUCCESS; |
| 1043 | efi_signal_event(token->event); |
| 1044 | } |
| 1045 | |
| 1046 | out: |
| 1047 | return EFI_EXIT(ret); |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 1048 | } |
| 1049 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1050 | static const struct efi_file_handle efi_file_handle_protocol = { |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 1051 | .rev = EFI_FILE_PROTOCOL_REVISION2, |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1052 | .open = efi_file_open, |
| 1053 | .close = efi_file_close, |
| 1054 | .delete = efi_file_delete, |
| 1055 | .read = efi_file_read, |
| 1056 | .write = efi_file_write, |
| 1057 | .getpos = efi_file_getpos, |
| 1058 | .setpos = efi_file_setpos, |
| 1059 | .getinfo = efi_file_getinfo, |
| 1060 | .setinfo = efi_file_setinfo, |
| 1061 | .flush = efi_file_flush, |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame] | 1062 | .open_ex = efi_file_open_ex, |
| 1063 | .read_ex = efi_file_read_ex, |
| 1064 | .write_ex = efi_file_write_ex, |
| 1065 | .flush_ex = efi_file_flush_ex, |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1066 | }; |
| 1067 | |
Heinrich Schuchardt | c1476c2 | 2019-02-04 21:24:35 +0100 | [diff] [blame] | 1068 | /** |
| 1069 | * efi_file_from_path() - open file via device path |
| 1070 | * |
| 1071 | * @fp: device path |
| 1072 | * @return: EFI_FILE_PROTOCOL for the file or NULL |
| 1073 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1074 | struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp) |
| 1075 | { |
| 1076 | struct efi_simple_file_system_protocol *v; |
| 1077 | struct efi_file_handle *f; |
| 1078 | efi_status_t ret; |
| 1079 | |
| 1080 | v = efi_fs_from_path(fp); |
| 1081 | if (!v) |
| 1082 | return NULL; |
| 1083 | |
| 1084 | EFI_CALL(ret = v->open_volume(v, &f)); |
| 1085 | if (ret != EFI_SUCCESS) |
| 1086 | return NULL; |
| 1087 | |
Heinrich Schuchardt | c1476c2 | 2019-02-04 21:24:35 +0100 | [diff] [blame] | 1088 | /* Skip over device-path nodes before the file path. */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1089 | while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) |
| 1090 | fp = efi_dp_next(fp); |
| 1091 | |
Heinrich Schuchardt | c1476c2 | 2019-02-04 21:24:35 +0100 | [diff] [blame] | 1092 | /* |
| 1093 | * Step through the nodes of the directory path until the actual file |
| 1094 | * node is reached which is the final node in the device path. |
| 1095 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1096 | while (fp) { |
| 1097 | struct efi_device_path_file_path *fdp = |
| 1098 | container_of(fp, struct efi_device_path_file_path, dp); |
| 1099 | struct efi_file_handle *f2; |
Heinrich Schuchardt | 2cfcd99 | 2019-07-14 20:14:46 +0200 | [diff] [blame] | 1100 | u16 *filename; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1101 | |
| 1102 | if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) { |
| 1103 | printf("bad file path!\n"); |
| 1104 | f->close(f); |
| 1105 | return NULL; |
| 1106 | } |
| 1107 | |
Heinrich Schuchardt | 2cfcd99 | 2019-07-14 20:14:46 +0200 | [diff] [blame] | 1108 | filename = u16_strdup(fdp->str); |
| 1109 | if (!filename) |
| 1110 | return NULL; |
| 1111 | EFI_CALL(ret = f->open(f, &f2, filename, |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1112 | EFI_FILE_MODE_READ, 0)); |
Heinrich Schuchardt | 2cfcd99 | 2019-07-14 20:14:46 +0200 | [diff] [blame] | 1113 | free(filename); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1114 | if (ret != EFI_SUCCESS) |
| 1115 | return NULL; |
| 1116 | |
| 1117 | fp = efi_dp_next(fp); |
| 1118 | |
| 1119 | EFI_CALL(f->close(f)); |
| 1120 | f = f2; |
| 1121 | } |
| 1122 | |
| 1123 | return f; |
| 1124 | } |
| 1125 | |
| 1126 | static efi_status_t EFIAPI |
| 1127 | efi_open_volume(struct efi_simple_file_system_protocol *this, |
| 1128 | struct efi_file_handle **root) |
| 1129 | { |
| 1130 | struct file_system *fs = to_fs(this); |
| 1131 | |
| 1132 | EFI_ENTRY("%p, %p", this, root); |
| 1133 | |
AKASHI Takahiro | 0c672b0 | 2018-09-11 15:59:12 +0900 | [diff] [blame] | 1134 | *root = file_open(fs, NULL, NULL, 0, 0); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 1135 | |
| 1136 | return EFI_EXIT(EFI_SUCCESS); |
| 1137 | } |
| 1138 | |
| 1139 | struct efi_simple_file_system_protocol * |
| 1140 | efi_simple_file_system(struct blk_desc *desc, int part, |
| 1141 | struct efi_device_path *dp) |
| 1142 | { |
| 1143 | struct file_system *fs; |
| 1144 | |
| 1145 | fs = calloc(1, sizeof(*fs)); |
| 1146 | fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION; |
| 1147 | fs->base.open_volume = efi_open_volume; |
| 1148 | fs->desc = desc; |
| 1149 | fs->part = part; |
| 1150 | fs->dp = dp; |
| 1151 | |
| 1152 | return &fs->base; |
| 1153 | } |