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