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> |
| 11 | #include <malloc.h> |
Alexander Graf | ab133c7 | 2018-08-08 03:54:32 -0600 | [diff] [blame] | 12 | #include <mapmem.h> |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 13 | #include <fs.h> |
| 14 | |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 15 | /* GUID for file system information */ |
| 16 | const efi_guid_t efi_file_system_info_guid = EFI_FILE_SYSTEM_INFO_GUID; |
| 17 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 18 | struct file_system { |
| 19 | struct efi_simple_file_system_protocol base; |
| 20 | struct efi_device_path *dp; |
| 21 | struct blk_desc *desc; |
| 22 | int part; |
| 23 | }; |
| 24 | #define to_fs(x) container_of(x, struct file_system, base) |
| 25 | |
| 26 | struct file_handle { |
| 27 | struct efi_file_handle base; |
| 28 | struct file_system *fs; |
| 29 | loff_t offset; /* current file position/cursor */ |
| 30 | int isdir; |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 31 | u64 open_mode; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 32 | |
| 33 | /* for reading a directory: */ |
| 34 | struct fs_dir_stream *dirs; |
| 35 | struct fs_dirent *dent; |
| 36 | |
| 37 | char path[0]; |
| 38 | }; |
| 39 | #define to_fh(x) container_of(x, struct file_handle, base) |
| 40 | |
| 41 | static const struct efi_file_handle efi_file_handle_protocol; |
| 42 | |
| 43 | static char *basename(struct file_handle *fh) |
| 44 | { |
| 45 | char *s = strrchr(fh->path, '/'); |
| 46 | if (s) |
| 47 | return s + 1; |
| 48 | return fh->path; |
| 49 | } |
| 50 | |
| 51 | static int set_blk_dev(struct file_handle *fh) |
| 52 | { |
| 53 | return fs_set_blk_dev_with_part(fh->fs->desc, fh->fs->part); |
| 54 | } |
| 55 | |
Heinrich Schuchardt | 80f834d | 2018-10-02 05:57:32 +0200 | [diff] [blame] | 56 | /** |
| 57 | * is_dir() - check if file handle points to directory |
| 58 | * |
| 59 | * We assume that set_blk_dev(fh) has been called already. |
| 60 | * |
| 61 | * @fh: file handle |
| 62 | * Return: true if file handle points to a directory |
| 63 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 64 | static int is_dir(struct file_handle *fh) |
| 65 | { |
| 66 | struct fs_dir_stream *dirs; |
| 67 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 68 | dirs = fs_opendir(fh->path); |
| 69 | if (!dirs) |
| 70 | return 0; |
| 71 | |
| 72 | fs_closedir(dirs); |
| 73 | |
| 74 | return 1; |
| 75 | } |
| 76 | |
| 77 | /* |
| 78 | * Normalize a path which may include either back or fwd slashes, |
| 79 | * double slashes, . or .. entries in the path, etc. |
| 80 | */ |
| 81 | static int sanitize_path(char *path) |
| 82 | { |
| 83 | char *p; |
| 84 | |
| 85 | /* backslash to slash: */ |
| 86 | p = path; |
| 87 | while ((p = strchr(p, '\\'))) |
| 88 | *p++ = '/'; |
| 89 | |
| 90 | /* handle double-slashes: */ |
| 91 | p = path; |
| 92 | while ((p = strstr(p, "//"))) { |
| 93 | char *src = p + 1; |
| 94 | memmove(p, src, strlen(src) + 1); |
| 95 | } |
| 96 | |
| 97 | /* handle extra /.'s */ |
| 98 | p = path; |
| 99 | while ((p = strstr(p, "/."))) { |
| 100 | /* |
| 101 | * You'd be tempted to do this *after* handling ".."s |
| 102 | * below to avoid having to check if "/." is start of |
| 103 | * a "/..", but that won't have the correct results.. |
| 104 | * for example, "/foo/./../bar" would get resolved to |
| 105 | * "/foo/bar" if you did these two passes in the other |
| 106 | * order |
| 107 | */ |
| 108 | if (p[2] == '.') { |
| 109 | p += 2; |
| 110 | continue; |
| 111 | } |
| 112 | char *src = p + 2; |
| 113 | memmove(p, src, strlen(src) + 1); |
| 114 | } |
| 115 | |
| 116 | /* handle extra /..'s: */ |
| 117 | p = path; |
| 118 | while ((p = strstr(p, "/.."))) { |
| 119 | char *src = p + 3; |
| 120 | |
| 121 | p--; |
| 122 | |
| 123 | /* find beginning of previous path entry: */ |
| 124 | while (true) { |
| 125 | if (p < path) |
| 126 | return -1; |
| 127 | if (*p == '/') |
| 128 | break; |
| 129 | p--; |
| 130 | } |
| 131 | |
| 132 | memmove(p, src, strlen(src) + 1); |
| 133 | } |
| 134 | |
| 135 | return 0; |
| 136 | } |
| 137 | |
Heinrich Schuchardt | 1af423b | 2018-09-12 19:00:02 +0200 | [diff] [blame] | 138 | /** |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 139 | * efi_create_file() - create file or directory |
| 140 | * |
| 141 | * @fh: file handle |
| 142 | * @attributes: attributes for newly created file |
| 143 | * Returns: 0 for success |
| 144 | */ |
| 145 | static int efi_create_file(struct file_handle *fh, u64 attributes) |
| 146 | { |
| 147 | loff_t actwrite; |
| 148 | void *buffer = &actwrite; |
| 149 | |
| 150 | if (attributes & EFI_FILE_DIRECTORY) |
| 151 | return fs_mkdir(fh->path); |
| 152 | else |
| 153 | return fs_write(fh->path, map_to_sysmem(buffer), 0, 0, |
| 154 | &actwrite); |
| 155 | } |
| 156 | |
| 157 | /** |
Heinrich Schuchardt | 1af423b | 2018-09-12 19:00:02 +0200 | [diff] [blame] | 158 | * file_open() - open a file handle |
| 159 | * |
| 160 | * @fs: file system |
| 161 | * @parent: directory relative to which the file is to be opened |
| 162 | * @file_name: path of the file to be opened. '\', '.', or '..' may |
| 163 | * be used as modifiers. A leading backslash indicates an |
| 164 | * absolute path. |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 165 | * @open_mode: bit mask indicating the access mode (read, write, |
Heinrich Schuchardt | 1af423b | 2018-09-12 19:00:02 +0200 | [diff] [blame] | 166 | * create) |
| 167 | * @attributes: attributes for newly created file |
| 168 | * Returns: handle to the opened file or NULL |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 169 | */ |
| 170 | static struct efi_file_handle *file_open(struct file_system *fs, |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 171 | struct file_handle *parent, u16 *file_name, u64 open_mode, |
AKASHI Takahiro | 0c672b0 | 2018-09-11 15:59:12 +0900 | [diff] [blame] | 172 | u64 attributes) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 173 | { |
| 174 | struct file_handle *fh; |
| 175 | char f0[MAX_UTF8_PER_UTF16] = {0}; |
| 176 | int plen = 0; |
| 177 | int flen = 0; |
| 178 | |
| 179 | if (file_name) { |
Heinrich Schuchardt | db47c34 | 2019-01-12 12:02:33 +0100 | [diff] [blame] | 180 | utf16_to_utf8((u8 *)f0, file_name, 1); |
| 181 | flen = u16_strlen(file_name); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | /* we could have a parent, but also an absolute path: */ |
| 185 | if (f0[0] == '\\') { |
| 186 | plen = 0; |
| 187 | } else if (parent) { |
| 188 | plen = strlen(parent->path) + 1; |
| 189 | } |
| 190 | |
| 191 | /* +2 is for null and '/' */ |
| 192 | fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2); |
| 193 | |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 194 | fh->open_mode = open_mode; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 195 | fh->base = efi_file_handle_protocol; |
| 196 | fh->fs = fs; |
| 197 | |
| 198 | if (parent) { |
| 199 | char *p = fh->path; |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 200 | int exists; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 201 | |
| 202 | if (plen > 0) { |
| 203 | strcpy(p, parent->path); |
| 204 | p += plen - 1; |
| 205 | *p++ = '/'; |
| 206 | } |
| 207 | |
Heinrich Schuchardt | db47c34 | 2019-01-12 12:02:33 +0100 | [diff] [blame] | 208 | utf16_to_utf8((u8 *)p, file_name, flen); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 209 | |
| 210 | if (sanitize_path(fh->path)) |
| 211 | goto error; |
| 212 | |
| 213 | /* check if file exists: */ |
| 214 | if (set_blk_dev(fh)) |
| 215 | goto error; |
| 216 | |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 217 | exists = fs_exists(fh->path); |
Heinrich Schuchardt | 3ab87ac | 2019-02-09 22:23:48 +0100 | [diff] [blame] | 218 | /* fs_exists() calls fs_close(), so open file system again */ |
| 219 | if (set_blk_dev(fh)) |
| 220 | goto error; |
| 221 | |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 222 | if (!exists) { |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 223 | if (!(open_mode & EFI_FILE_MODE_CREATE) || |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 224 | efi_create_file(fh, attributes)) |
| 225 | goto error; |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 226 | if (set_blk_dev(fh)) |
| 227 | goto error; |
Heinrich Schuchardt | 34c303b | 2019-04-06 16:27:34 +0200 | [diff] [blame] | 228 | } |
| 229 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 230 | /* figure out if file is a directory: */ |
| 231 | fh->isdir = is_dir(fh); |
| 232 | } else { |
| 233 | fh->isdir = 1; |
| 234 | strcpy(fh->path, ""); |
| 235 | } |
| 236 | |
| 237 | return &fh->base; |
| 238 | |
| 239 | error: |
| 240 | free(fh); |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file, |
| 245 | struct efi_file_handle **new_handle, |
Heinrich Schuchardt | db47c34 | 2019-01-12 12:02:33 +0100 | [diff] [blame] | 246 | u16 *file_name, u64 open_mode, u64 attributes) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 247 | { |
| 248 | struct file_handle *fh = to_fh(file); |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 249 | efi_status_t ret; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 250 | |
Simon Glass | 60209e6 | 2019-01-07 16:44:18 -0700 | [diff] [blame] | 251 | EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, |
Heinrich Schuchardt | 9de5c4c | 2019-03-19 19:16:23 +0100 | [diff] [blame] | 252 | file_name, open_mode, attributes); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 253 | |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 254 | /* Check parameters */ |
Heinrich Schuchardt | b63f9fc | 2018-09-15 20:43:46 +0200 | [diff] [blame] | 255 | if (!file || !new_handle || !file_name) { |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 256 | ret = EFI_INVALID_PARAMETER; |
| 257 | goto out; |
| 258 | } |
| 259 | if (open_mode != EFI_FILE_MODE_READ && |
| 260 | open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) && |
| 261 | open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | |
| 262 | EFI_FILE_MODE_CREATE)) { |
| 263 | ret = EFI_INVALID_PARAMETER; |
| 264 | goto out; |
| 265 | } |
Heinrich Schuchardt | 662d0be | 2018-09-13 21:31:49 +0200 | [diff] [blame] | 266 | /* |
| 267 | * The UEFI spec requires that attributes are only set in create mode. |
| 268 | * The SCT does not care about this and sets EFI_FILE_DIRECTORY in |
| 269 | * read mode. EDK2 does not check that attributes are zero if not in |
| 270 | * create mode. |
| 271 | * |
| 272 | * So here we only check attributes in create mode and do not check |
| 273 | * that they are zero otherwise. |
| 274 | */ |
| 275 | if ((open_mode & EFI_FILE_MODE_CREATE) && |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 276 | (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) { |
| 277 | ret = EFI_INVALID_PARAMETER; |
| 278 | goto out; |
| 279 | } |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 280 | |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 281 | /* Open file */ |
| 282 | *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes); |
Heinrich Schuchardt | d4fe528 | 2019-04-06 16:36:16 +0200 | [diff] [blame] | 283 | if (*new_handle) { |
| 284 | EFI_PRINT("file handle %p\n", *new_handle); |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 285 | ret = EFI_SUCCESS; |
Heinrich Schuchardt | d4fe528 | 2019-04-06 16:36:16 +0200 | [diff] [blame] | 286 | } else { |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 287 | ret = EFI_NOT_FOUND; |
Heinrich Schuchardt | d4fe528 | 2019-04-06 16:36:16 +0200 | [diff] [blame] | 288 | } |
Heinrich Schuchardt | d6932c0 | 2018-09-12 18:43:58 +0200 | [diff] [blame] | 289 | out: |
| 290 | return EFI_EXIT(ret); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static efi_status_t file_close(struct file_handle *fh) |
| 294 | { |
| 295 | fs_closedir(fh->dirs); |
| 296 | free(fh); |
| 297 | return EFI_SUCCESS; |
| 298 | } |
| 299 | |
| 300 | static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file) |
| 301 | { |
| 302 | struct file_handle *fh = to_fh(file); |
| 303 | EFI_ENTRY("%p", file); |
| 304 | return EFI_EXIT(file_close(fh)); |
| 305 | } |
| 306 | |
| 307 | static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file) |
| 308 | { |
| 309 | struct file_handle *fh = to_fh(file); |
AKASHI Takahiro | 4dd5b9c | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 310 | efi_status_t ret = EFI_SUCCESS; |
| 311 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 312 | EFI_ENTRY("%p", file); |
AKASHI Takahiro | 4dd5b9c | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 313 | |
Heinrich Schuchardt | 646faf3 | 2019-06-17 22:00:13 +0200 | [diff] [blame] | 314 | if (set_blk_dev(fh) || fs_unlink(fh->path)) |
| 315 | ret = EFI_WARN_DELETE_FAILURE; |
AKASHI Takahiro | 4dd5b9c | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 316 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 317 | file_close(fh); |
AKASHI Takahiro | 4dd5b9c | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 318 | return EFI_EXIT(ret); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 319 | } |
| 320 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 321 | /** |
| 322 | * efi_get_file_size() - determine the size of a file |
| 323 | * |
| 324 | * @fh: file handle |
| 325 | * @file_size: pointer to receive file size |
| 326 | * Return: status code |
| 327 | */ |
| 328 | static efi_status_t efi_get_file_size(struct file_handle *fh, |
| 329 | loff_t *file_size) |
| 330 | { |
| 331 | if (set_blk_dev(fh)) |
| 332 | return EFI_DEVICE_ERROR; |
| 333 | |
| 334 | if (fs_size(fh->path, file_size)) |
| 335 | return EFI_DEVICE_ERROR; |
| 336 | |
| 337 | return EFI_SUCCESS; |
| 338 | } |
| 339 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 340 | static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size, |
| 341 | void *buffer) |
| 342 | { |
| 343 | loff_t actread; |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 344 | efi_status_t ret; |
| 345 | loff_t file_size; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 346 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 347 | ret = efi_get_file_size(fh, &file_size); |
| 348 | if (ret != EFI_SUCCESS) |
| 349 | return ret; |
| 350 | if (file_size < fh->offset) { |
| 351 | ret = EFI_DEVICE_ERROR; |
| 352 | return ret; |
| 353 | } |
| 354 | |
| 355 | if (set_blk_dev(fh)) |
| 356 | return EFI_DEVICE_ERROR; |
Simon Glass | 849688f | 2018-09-15 00:51:00 -0600 | [diff] [blame] | 357 | if (fs_read(fh->path, map_to_sysmem(buffer), fh->offset, |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 358 | *buffer_size, &actread)) |
| 359 | return EFI_DEVICE_ERROR; |
| 360 | |
| 361 | *buffer_size = actread; |
| 362 | fh->offset += actread; |
| 363 | |
| 364 | return EFI_SUCCESS; |
| 365 | } |
| 366 | |
| 367 | static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size, |
| 368 | void *buffer) |
| 369 | { |
| 370 | struct efi_file_info *info = buffer; |
| 371 | struct fs_dirent *dent; |
Heinrich Schuchardt | da06260 | 2019-09-07 22:34:07 +0200 | [diff] [blame] | 372 | u64 required_size; |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 373 | u16 *dst; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 374 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 375 | if (set_blk_dev(fh)) |
| 376 | return EFI_DEVICE_ERROR; |
| 377 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 378 | if (!fh->dirs) { |
| 379 | assert(fh->offset == 0); |
| 380 | fh->dirs = fs_opendir(fh->path); |
| 381 | if (!fh->dirs) |
| 382 | return EFI_DEVICE_ERROR; |
Heinrich Schuchardt | da06260 | 2019-09-07 22:34:07 +0200 | [diff] [blame] | 383 | fh->dent = NULL; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* |
| 387 | * So this is a bit awkward. Since fs layer is stateful and we |
| 388 | * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below |
| 389 | * we might have to return without consuming the dent.. so we |
| 390 | * have to stash it for next call. |
| 391 | */ |
| 392 | if (fh->dent) { |
| 393 | dent = fh->dent; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 394 | } else { |
| 395 | dent = fs_readdir(fh->dirs); |
| 396 | } |
| 397 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 398 | if (!dent) { |
Heinrich Schuchardt | da06260 | 2019-09-07 22:34:07 +0200 | [diff] [blame] | 399 | /* no more files in directory */ |
| 400 | *buffer_size = 0; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 401 | return EFI_SUCCESS; |
| 402 | } |
| 403 | |
| 404 | /* check buffer size: */ |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 405 | required_size = sizeof(*info) + |
| 406 | 2 * (utf8_utf16_strlen(dent->name) + 1); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 407 | if (*buffer_size < required_size) { |
| 408 | *buffer_size = required_size; |
| 409 | fh->dent = dent; |
| 410 | return EFI_BUFFER_TOO_SMALL; |
| 411 | } |
Heinrich Schuchardt | da06260 | 2019-09-07 22:34:07 +0200 | [diff] [blame] | 412 | fh->dent = NULL; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 413 | |
| 414 | *buffer_size = required_size; |
| 415 | memset(info, 0, required_size); |
| 416 | |
| 417 | info->size = required_size; |
| 418 | info->file_size = dent->size; |
| 419 | info->physical_size = dent->size; |
| 420 | |
| 421 | if (dent->type == FS_DT_DIR) |
| 422 | info->attribute |= EFI_FILE_DIRECTORY; |
| 423 | |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 424 | dst = info->file_name; |
| 425 | utf8_utf16_strcpy(&dst, dent->name); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 426 | |
| 427 | fh->offset++; |
| 428 | |
| 429 | return EFI_SUCCESS; |
| 430 | } |
| 431 | |
| 432 | static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file, |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 433 | efi_uintn_t *buffer_size, void *buffer) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 434 | { |
| 435 | struct file_handle *fh = to_fh(file); |
| 436 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 437 | u64 bs; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 438 | |
| 439 | EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer); |
| 440 | |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 441 | if (!buffer_size || !buffer) { |
| 442 | ret = EFI_INVALID_PARAMETER; |
| 443 | goto error; |
| 444 | } |
| 445 | |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 446 | bs = *buffer_size; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 447 | if (fh->isdir) |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 448 | ret = dir_read(fh, &bs, buffer); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 449 | else |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 450 | ret = file_read(fh, &bs, buffer); |
| 451 | if (bs <= SIZE_MAX) |
| 452 | *buffer_size = bs; |
| 453 | else |
| 454 | *buffer_size = SIZE_MAX; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 455 | |
| 456 | error: |
| 457 | return EFI_EXIT(ret); |
| 458 | } |
| 459 | |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 460 | /** |
| 461 | * efi_file_write() - write to file |
| 462 | * |
| 463 | * This function implements the Write() service of the EFI_FILE_PROTOCOL. |
| 464 | * |
| 465 | * See the Unified Extensible Firmware Interface (UEFI) specification for |
| 466 | * details. |
| 467 | * |
| 468 | * @file: file handle |
| 469 | * @buffer_size: number of bytes to write |
| 470 | * @buffer: buffer with the bytes to write |
| 471 | * Return: status code |
| 472 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 473 | static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file, |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 474 | efi_uintn_t *buffer_size, |
| 475 | void *buffer) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 476 | { |
| 477 | struct file_handle *fh = to_fh(file); |
| 478 | efi_status_t ret = EFI_SUCCESS; |
| 479 | loff_t actwrite; |
| 480 | |
| 481 | EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer); |
| 482 | |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 483 | if (!file || !buffer_size || !buffer) { |
| 484 | ret = EFI_INVALID_PARAMETER; |
| 485 | goto out; |
| 486 | } |
| 487 | if (fh->isdir) { |
| 488 | ret = EFI_UNSUPPORTED; |
| 489 | goto out; |
| 490 | } |
| 491 | if (!(fh->open_mode & EFI_FILE_MODE_WRITE)) { |
| 492 | ret = EFI_ACCESS_DENIED; |
| 493 | goto out; |
| 494 | } |
| 495 | |
| 496 | if (!*buffer_size) |
| 497 | goto out; |
| 498 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 499 | if (set_blk_dev(fh)) { |
| 500 | ret = EFI_DEVICE_ERROR; |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 501 | goto out; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 502 | } |
Simon Glass | 849688f | 2018-09-15 00:51:00 -0600 | [diff] [blame] | 503 | 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] | 504 | &actwrite)) { |
| 505 | ret = EFI_DEVICE_ERROR; |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 506 | goto out; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 507 | } |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 508 | *buffer_size = actwrite; |
| 509 | fh->offset += actwrite; |
| 510 | |
Heinrich Schuchardt | b7e87a2 | 2019-09-06 21:37:21 +0200 | [diff] [blame] | 511 | out: |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 512 | return EFI_EXIT(ret); |
| 513 | } |
| 514 | |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 515 | /** |
| 516 | * efi_file_getpos() - get current position in file |
| 517 | * |
| 518 | * This function implements the GetPosition service of the EFI file protocol. |
| 519 | * See the UEFI spec for details. |
| 520 | * |
| 521 | * @file: file handle |
| 522 | * @pos: pointer to file position |
| 523 | * Return: status code |
| 524 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 525 | 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] | 526 | u64 *pos) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 527 | { |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 528 | efi_status_t ret = EFI_SUCCESS; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 529 | struct file_handle *fh = to_fh(file); |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 530 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 531 | EFI_ENTRY("%p, %p", file, pos); |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 532 | |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 533 | if (fh->isdir) { |
| 534 | ret = EFI_UNSUPPORTED; |
| 535 | goto out; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 536 | } |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 537 | |
| 538 | *pos = fh->offset; |
| 539 | out: |
| 540 | return EFI_EXIT(ret); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 541 | } |
| 542 | |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 543 | /** |
| 544 | * efi_file_setpos() - set current position in file |
| 545 | * |
| 546 | * This function implements the SetPosition service of the EFI file protocol. |
| 547 | * See the UEFI spec for details. |
| 548 | * |
| 549 | * @file: file handle |
| 550 | * @pos: new file position |
| 551 | * Return: status code |
| 552 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 553 | 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] | 554 | u64 pos) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 555 | { |
| 556 | struct file_handle *fh = to_fh(file); |
| 557 | efi_status_t ret = EFI_SUCCESS; |
| 558 | |
Heinrich Schuchardt | 21ae057 | 2018-10-07 05:26:26 +0200 | [diff] [blame] | 559 | EFI_ENTRY("%p, %llu", file, pos); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 560 | |
| 561 | if (fh->isdir) { |
| 562 | if (pos != 0) { |
| 563 | ret = EFI_UNSUPPORTED; |
| 564 | goto error; |
| 565 | } |
| 566 | fs_closedir(fh->dirs); |
| 567 | fh->dirs = NULL; |
| 568 | } |
| 569 | |
| 570 | if (pos == ~0ULL) { |
| 571 | loff_t file_size; |
| 572 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 573 | ret = efi_get_file_size(fh, &file_size); |
| 574 | if (ret != EFI_SUCCESS) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 575 | goto error; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 576 | pos = file_size; |
| 577 | } |
| 578 | |
| 579 | fh->offset = pos; |
| 580 | |
| 581 | error: |
| 582 | return EFI_EXIT(ret); |
| 583 | } |
| 584 | |
| 585 | 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] | 586 | const efi_guid_t *info_type, |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 587 | efi_uintn_t *buffer_size, |
| 588 | void *buffer) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 589 | { |
| 590 | struct file_handle *fh = to_fh(file); |
| 591 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 592 | u16 *dst; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 593 | |
Heinrich Schuchardt | e8c99b0 | 2018-09-14 22:46:34 +0200 | [diff] [blame] | 594 | EFI_ENTRY("%p, %pUl, %p, %p", file, info_type, buffer_size, buffer); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 595 | |
| 596 | if (!guidcmp(info_type, &efi_file_info_guid)) { |
| 597 | struct efi_file_info *info = buffer; |
| 598 | char *filename = basename(fh); |
| 599 | unsigned int required_size; |
| 600 | loff_t file_size; |
| 601 | |
| 602 | /* check buffer size: */ |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 603 | required_size = sizeof(*info) + |
| 604 | 2 * (utf8_utf16_strlen(filename) + 1); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 605 | if (*buffer_size < required_size) { |
| 606 | *buffer_size = required_size; |
| 607 | ret = EFI_BUFFER_TOO_SMALL; |
| 608 | goto error; |
| 609 | } |
| 610 | |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 611 | ret = efi_get_file_size(fh, &file_size); |
| 612 | if (ret != EFI_SUCCESS) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 613 | goto error; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 614 | |
| 615 | memset(info, 0, required_size); |
| 616 | |
| 617 | info->size = required_size; |
| 618 | info->file_size = file_size; |
| 619 | info->physical_size = file_size; |
| 620 | |
| 621 | if (fh->isdir) |
| 622 | info->attribute |= EFI_FILE_DIRECTORY; |
| 623 | |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 624 | dst = info->file_name; |
| 625 | utf8_utf16_strcpy(&dst, filename); |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 626 | } else if (!guidcmp(info_type, &efi_file_system_info_guid)) { |
| 627 | struct efi_file_system_info *info = buffer; |
| 628 | disk_partition_t part; |
| 629 | efi_uintn_t required_size; |
| 630 | int r; |
| 631 | |
| 632 | if (fh->fs->part >= 1) |
| 633 | r = part_get_info(fh->fs->desc, fh->fs->part, &part); |
| 634 | else |
| 635 | r = part_get_info_whole_disk(fh->fs->desc, &part); |
| 636 | if (r < 0) { |
| 637 | ret = EFI_DEVICE_ERROR; |
| 638 | goto error; |
| 639 | } |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 640 | required_size = sizeof(*info) + 2 * |
| 641 | (utf8_utf16_strlen((const char *)part.name) + |
| 642 | 1); |
Heinrich Schuchardt | 37fb4b9 | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 643 | if (*buffer_size < required_size) { |
| 644 | *buffer_size = required_size; |
| 645 | ret = EFI_BUFFER_TOO_SMALL; |
| 646 | goto error; |
| 647 | } |
| 648 | |
| 649 | memset(info, 0, required_size); |
| 650 | |
| 651 | info->size = required_size; |
| 652 | info->read_only = true; |
| 653 | info->volume_size = part.size * part.blksz; |
| 654 | info->free_space = 0; |
| 655 | info->block_size = part.blksz; |
| 656 | /* |
| 657 | * TODO: The volume label is not available in U-Boot. |
| 658 | * Use the partition name as substitute. |
| 659 | */ |
Heinrich Schuchardt | 2ad17f9 | 2019-09-07 21:05:45 +0200 | [diff] [blame] | 660 | dst = info->volume_label; |
| 661 | utf8_utf16_strcpy(&dst, (const char *)part.name); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 662 | } else { |
| 663 | ret = EFI_UNSUPPORTED; |
| 664 | } |
| 665 | |
| 666 | error: |
| 667 | return EFI_EXIT(ret); |
| 668 | } |
| 669 | |
| 670 | 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] | 671 | const efi_guid_t *info_type, |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 672 | efi_uintn_t buffer_size, |
| 673 | void *buffer) |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 674 | { |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 675 | struct file_handle *fh = to_fh(file); |
| 676 | efi_status_t ret = EFI_UNSUPPORTED; |
| 677 | |
| 678 | EFI_ENTRY("%p, %pUl, %zu, %p", file, info_type, buffer_size, buffer); |
| 679 | |
| 680 | if (!guidcmp(info_type, &efi_file_info_guid)) { |
| 681 | struct efi_file_info *info = (struct efi_file_info *)buffer; |
| 682 | char *filename = basename(fh); |
| 683 | char *new_file_name, *pos; |
| 684 | loff_t file_size; |
Heinrich Schuchardt | 6b06e0d | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 685 | |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 686 | if (buffer_size < sizeof(struct efi_file_info)) { |
| 687 | ret = EFI_BAD_BUFFER_SIZE; |
| 688 | goto out; |
| 689 | } |
| 690 | /* We cannot change the directory attribute */ |
| 691 | if (!fh->isdir != !(info->attribute & EFI_FILE_DIRECTORY)) { |
| 692 | ret = EFI_ACCESS_DENIED; |
| 693 | goto out; |
| 694 | } |
| 695 | /* Check for renaming */ |
| 696 | new_file_name = malloc(utf16_utf8_strlen(info->file_name)); |
| 697 | if (!new_file_name) { |
| 698 | ret = EFI_OUT_OF_RESOURCES; |
| 699 | goto out; |
| 700 | } |
| 701 | pos = new_file_name; |
| 702 | utf16_utf8_strcpy(&pos, info->file_name); |
| 703 | if (strcmp(new_file_name, filename)) { |
| 704 | /* TODO: we do not support renaming */ |
| 705 | EFI_PRINT("Renaming not supported\n"); |
| 706 | free(new_file_name); |
| 707 | ret = EFI_ACCESS_DENIED; |
| 708 | goto out; |
| 709 | } |
| 710 | free(new_file_name); |
| 711 | /* Check for truncation */ |
Heinrich Schuchardt | df86279 | 2019-09-07 23:28:04 +0200 | [diff] [blame] | 712 | ret = efi_get_file_size(fh, &file_size); |
| 713 | if (ret != EFI_SUCCESS) |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 714 | goto out; |
Heinrich Schuchardt | f1fe17e | 2019-04-06 18:17:39 +0200 | [diff] [blame] | 715 | if (file_size != info->file_size) { |
| 716 | /* TODO: we do not support truncation */ |
| 717 | EFI_PRINT("Truncation not supported\n"); |
| 718 | ret = EFI_ACCESS_DENIED; |
| 719 | goto out; |
| 720 | } |
| 721 | /* |
| 722 | * We do not care for the other attributes |
| 723 | * TODO: Support read only |
| 724 | */ |
| 725 | ret = EFI_SUCCESS; |
| 726 | } else if (!guidcmp(info_type, &efi_file_system_info_guid)) { |
| 727 | if (buffer_size < sizeof(struct efi_file_system_info)) { |
| 728 | ret = EFI_BAD_BUFFER_SIZE; |
| 729 | goto out; |
| 730 | } |
| 731 | } else { |
| 732 | ret = EFI_UNSUPPORTED; |
| 733 | } |
| 734 | out: |
| 735 | return EFI_EXIT(ret); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file) |
| 739 | { |
| 740 | EFI_ENTRY("%p", file); |
| 741 | return EFI_EXIT(EFI_SUCCESS); |
| 742 | } |
| 743 | |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame^] | 744 | static efi_status_t EFIAPI efi_file_open_ex(struct efi_file_handle *file, |
| 745 | struct efi_file_handle **new_handle, |
| 746 | u16 *file_name, u64 open_mode, u64 attributes, |
| 747 | struct efi_file_io_token *token) |
| 748 | { |
| 749 | return EFI_UNSUPPORTED; |
| 750 | } |
| 751 | |
| 752 | static efi_status_t EFIAPI efi_file_read_ex(struct efi_file_handle *file, |
| 753 | struct efi_file_io_token *token) |
| 754 | { |
| 755 | return EFI_UNSUPPORTED; |
| 756 | } |
| 757 | |
| 758 | static efi_status_t EFIAPI efi_file_write_ex(struct efi_file_handle *file, |
| 759 | struct efi_file_io_token *token) |
| 760 | { |
| 761 | return EFI_UNSUPPORTED; |
| 762 | } |
| 763 | |
| 764 | static efi_status_t EFIAPI efi_file_flush_ex(struct efi_file_handle *file, |
| 765 | struct efi_file_io_token *token) |
| 766 | { |
| 767 | return EFI_UNSUPPORTED; |
| 768 | } |
| 769 | |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 770 | static const struct efi_file_handle efi_file_handle_protocol = { |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame^] | 771 | .rev = EFI_FILE_PROTOCOL_REVISION2, |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 772 | .open = efi_file_open, |
| 773 | .close = efi_file_close, |
| 774 | .delete = efi_file_delete, |
| 775 | .read = efi_file_read, |
| 776 | .write = efi_file_write, |
| 777 | .getpos = efi_file_getpos, |
| 778 | .setpos = efi_file_setpos, |
| 779 | .getinfo = efi_file_getinfo, |
| 780 | .setinfo = efi_file_setinfo, |
| 781 | .flush = efi_file_flush, |
Heinrich Schuchardt | f2dcbac | 2019-09-08 09:35:32 +0200 | [diff] [blame^] | 782 | .open_ex = efi_file_open_ex, |
| 783 | .read_ex = efi_file_read_ex, |
| 784 | .write_ex = efi_file_write_ex, |
| 785 | .flush_ex = efi_file_flush_ex, |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 786 | }; |
| 787 | |
Heinrich Schuchardt | c1476c2 | 2019-02-04 21:24:35 +0100 | [diff] [blame] | 788 | /** |
| 789 | * efi_file_from_path() - open file via device path |
| 790 | * |
| 791 | * @fp: device path |
| 792 | * @return: EFI_FILE_PROTOCOL for the file or NULL |
| 793 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 794 | struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp) |
| 795 | { |
| 796 | struct efi_simple_file_system_protocol *v; |
| 797 | struct efi_file_handle *f; |
| 798 | efi_status_t ret; |
| 799 | |
| 800 | v = efi_fs_from_path(fp); |
| 801 | if (!v) |
| 802 | return NULL; |
| 803 | |
| 804 | EFI_CALL(ret = v->open_volume(v, &f)); |
| 805 | if (ret != EFI_SUCCESS) |
| 806 | return NULL; |
| 807 | |
Heinrich Schuchardt | c1476c2 | 2019-02-04 21:24:35 +0100 | [diff] [blame] | 808 | /* Skip over device-path nodes before the file path. */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 809 | while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) |
| 810 | fp = efi_dp_next(fp); |
| 811 | |
Heinrich Schuchardt | c1476c2 | 2019-02-04 21:24:35 +0100 | [diff] [blame] | 812 | /* |
| 813 | * Step through the nodes of the directory path until the actual file |
| 814 | * node is reached which is the final node in the device path. |
| 815 | */ |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 816 | while (fp) { |
| 817 | struct efi_device_path_file_path *fdp = |
| 818 | container_of(fp, struct efi_device_path_file_path, dp); |
| 819 | struct efi_file_handle *f2; |
Heinrich Schuchardt | 2cfcd99 | 2019-07-14 20:14:46 +0200 | [diff] [blame] | 820 | u16 *filename; |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 821 | |
| 822 | if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) { |
| 823 | printf("bad file path!\n"); |
| 824 | f->close(f); |
| 825 | return NULL; |
| 826 | } |
| 827 | |
Heinrich Schuchardt | 2cfcd99 | 2019-07-14 20:14:46 +0200 | [diff] [blame] | 828 | filename = u16_strdup(fdp->str); |
| 829 | if (!filename) |
| 830 | return NULL; |
| 831 | EFI_CALL(ret = f->open(f, &f2, filename, |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 832 | EFI_FILE_MODE_READ, 0)); |
Heinrich Schuchardt | 2cfcd99 | 2019-07-14 20:14:46 +0200 | [diff] [blame] | 833 | free(filename); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 834 | if (ret != EFI_SUCCESS) |
| 835 | return NULL; |
| 836 | |
| 837 | fp = efi_dp_next(fp); |
| 838 | |
| 839 | EFI_CALL(f->close(f)); |
| 840 | f = f2; |
| 841 | } |
| 842 | |
| 843 | return f; |
| 844 | } |
| 845 | |
| 846 | static efi_status_t EFIAPI |
| 847 | efi_open_volume(struct efi_simple_file_system_protocol *this, |
| 848 | struct efi_file_handle **root) |
| 849 | { |
| 850 | struct file_system *fs = to_fs(this); |
| 851 | |
| 852 | EFI_ENTRY("%p, %p", this, root); |
| 853 | |
AKASHI Takahiro | 0c672b0 | 2018-09-11 15:59:12 +0900 | [diff] [blame] | 854 | *root = file_open(fs, NULL, NULL, 0, 0); |
Rob Clark | 5314203 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 855 | |
| 856 | return EFI_EXIT(EFI_SUCCESS); |
| 857 | } |
| 858 | |
| 859 | struct efi_simple_file_system_protocol * |
| 860 | efi_simple_file_system(struct blk_desc *desc, int part, |
| 861 | struct efi_device_path *dp) |
| 862 | { |
| 863 | struct file_system *fs; |
| 864 | |
| 865 | fs = calloc(1, sizeof(*fs)); |
| 866 | fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION; |
| 867 | fs->base.open_volume = efi_open_volume; |
| 868 | fs->desc = desc; |
| 869 | fs->part = part; |
| 870 | fs->dp = dp; |
| 871 | |
| 872 | return &fs->base; |
| 873 | } |