Tom Rini | f739fcd | 2018-05-07 17:02:21 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 2 | /* |
| 3 | * EFI utils |
| 4 | * |
| 5 | * Copyright (c) 2017 Rob Clark |
Rob Clark | 2a92080 | 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 | d5a5a5a | 2018-08-08 03:54:32 -0600 | [diff] [blame] | 12 | #include <mapmem.h> |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 13 | #include <fs.h> |
| 14 | |
Heinrich Schuchardt | 9e6835e | 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 | 2a92080 | 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 | |
| 55 | static int is_dir(struct file_handle *fh) |
| 56 | { |
| 57 | struct fs_dir_stream *dirs; |
| 58 | |
| 59 | set_blk_dev(fh); |
| 60 | dirs = fs_opendir(fh->path); |
| 61 | if (!dirs) |
| 62 | return 0; |
| 63 | |
| 64 | fs_closedir(dirs); |
| 65 | |
| 66 | return 1; |
| 67 | } |
| 68 | |
| 69 | /* |
| 70 | * Normalize a path which may include either back or fwd slashes, |
| 71 | * double slashes, . or .. entries in the path, etc. |
| 72 | */ |
| 73 | static int sanitize_path(char *path) |
| 74 | { |
| 75 | char *p; |
| 76 | |
| 77 | /* backslash to slash: */ |
| 78 | p = path; |
| 79 | while ((p = strchr(p, '\\'))) |
| 80 | *p++ = '/'; |
| 81 | |
| 82 | /* handle double-slashes: */ |
| 83 | p = path; |
| 84 | while ((p = strstr(p, "//"))) { |
| 85 | char *src = p + 1; |
| 86 | memmove(p, src, strlen(src) + 1); |
| 87 | } |
| 88 | |
| 89 | /* handle extra /.'s */ |
| 90 | p = path; |
| 91 | while ((p = strstr(p, "/."))) { |
| 92 | /* |
| 93 | * You'd be tempted to do this *after* handling ".."s |
| 94 | * below to avoid having to check if "/." is start of |
| 95 | * a "/..", but that won't have the correct results.. |
| 96 | * for example, "/foo/./../bar" would get resolved to |
| 97 | * "/foo/bar" if you did these two passes in the other |
| 98 | * order |
| 99 | */ |
| 100 | if (p[2] == '.') { |
| 101 | p += 2; |
| 102 | continue; |
| 103 | } |
| 104 | char *src = p + 2; |
| 105 | memmove(p, src, strlen(src) + 1); |
| 106 | } |
| 107 | |
| 108 | /* handle extra /..'s: */ |
| 109 | p = path; |
| 110 | while ((p = strstr(p, "/.."))) { |
| 111 | char *src = p + 3; |
| 112 | |
| 113 | p--; |
| 114 | |
| 115 | /* find beginning of previous path entry: */ |
| 116 | while (true) { |
| 117 | if (p < path) |
| 118 | return -1; |
| 119 | if (*p == '/') |
| 120 | break; |
| 121 | p--; |
| 122 | } |
| 123 | |
| 124 | memmove(p, src, strlen(src) + 1); |
| 125 | } |
| 126 | |
| 127 | return 0; |
| 128 | } |
| 129 | |
| 130 | /* NOTE: despite what you would expect, 'file_name' is actually a path. |
| 131 | * With windoze style backlashes, ofc. |
| 132 | */ |
| 133 | static struct efi_file_handle *file_open(struct file_system *fs, |
AKASHI Takahiro | 5bc84a1 | 2018-09-11 15:59:12 +0900 | [diff] [blame] | 134 | struct file_handle *parent, s16 *file_name, u64 mode, |
| 135 | u64 attributes) |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 136 | { |
| 137 | struct file_handle *fh; |
| 138 | char f0[MAX_UTF8_PER_UTF16] = {0}; |
| 139 | int plen = 0; |
| 140 | int flen = 0; |
| 141 | |
| 142 | if (file_name) { |
| 143 | utf16_to_utf8((u8 *)f0, (u16 *)file_name, 1); |
Heinrich Schuchardt | 1dde0d5 | 2018-08-31 21:31:26 +0200 | [diff] [blame] | 144 | flen = u16_strlen((u16 *)file_name); |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | /* we could have a parent, but also an absolute path: */ |
| 148 | if (f0[0] == '\\') { |
| 149 | plen = 0; |
| 150 | } else if (parent) { |
| 151 | plen = strlen(parent->path) + 1; |
| 152 | } |
| 153 | |
| 154 | /* +2 is for null and '/' */ |
| 155 | fh = calloc(1, sizeof(*fh) + plen + (flen * MAX_UTF8_PER_UTF16) + 2); |
| 156 | |
| 157 | fh->base = efi_file_handle_protocol; |
| 158 | fh->fs = fs; |
| 159 | |
| 160 | if (parent) { |
| 161 | char *p = fh->path; |
| 162 | |
| 163 | if (plen > 0) { |
| 164 | strcpy(p, parent->path); |
| 165 | p += plen - 1; |
| 166 | *p++ = '/'; |
| 167 | } |
| 168 | |
| 169 | utf16_to_utf8((u8 *)p, (u16 *)file_name, flen); |
| 170 | |
| 171 | if (sanitize_path(fh->path)) |
| 172 | goto error; |
| 173 | |
| 174 | /* check if file exists: */ |
| 175 | if (set_blk_dev(fh)) |
| 176 | goto error; |
| 177 | |
AKASHI Takahiro | 5bc84a1 | 2018-09-11 15:59:12 +0900 | [diff] [blame] | 178 | if ((mode & EFI_FILE_MODE_CREATE) && |
| 179 | (attributes & EFI_FILE_DIRECTORY)) { |
| 180 | if (fs_mkdir(fh->path)) |
| 181 | goto error; |
| 182 | } else if (!((mode & EFI_FILE_MODE_CREATE) || |
| 183 | fs_exists(fh->path))) |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 184 | goto error; |
| 185 | |
| 186 | /* figure out if file is a directory: */ |
| 187 | fh->isdir = is_dir(fh); |
| 188 | } else { |
| 189 | fh->isdir = 1; |
| 190 | strcpy(fh->path, ""); |
| 191 | } |
| 192 | |
| 193 | return &fh->base; |
| 194 | |
| 195 | error: |
| 196 | free(fh); |
| 197 | return NULL; |
| 198 | } |
| 199 | |
| 200 | static efi_status_t EFIAPI efi_file_open(struct efi_file_handle *file, |
| 201 | struct efi_file_handle **new_handle, |
| 202 | s16 *file_name, u64 open_mode, u64 attributes) |
| 203 | { |
| 204 | struct file_handle *fh = to_fh(file); |
Heinrich Schuchardt | 143acd1 | 2018-09-12 18:43:58 +0200 | [diff] [blame^] | 205 | efi_status_t ret; |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 206 | |
| 207 | EFI_ENTRY("%p, %p, \"%ls\", %llx, %llu", file, new_handle, file_name, |
| 208 | open_mode, attributes); |
| 209 | |
Heinrich Schuchardt | 143acd1 | 2018-09-12 18:43:58 +0200 | [diff] [blame^] | 210 | /* Check parameters */ |
| 211 | if (!file || !file || !file_name) { |
| 212 | ret = EFI_INVALID_PARAMETER; |
| 213 | goto out; |
| 214 | } |
| 215 | if (open_mode != EFI_FILE_MODE_READ && |
| 216 | open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE) && |
| 217 | open_mode != (EFI_FILE_MODE_READ | EFI_FILE_MODE_WRITE | |
| 218 | EFI_FILE_MODE_CREATE)) { |
| 219 | ret = EFI_INVALID_PARAMETER; |
| 220 | goto out; |
| 221 | } |
| 222 | if ((!(open_mode & EFI_FILE_MODE_CREATE) && attributes) || |
| 223 | (attributes & (EFI_FILE_READ_ONLY | ~EFI_FILE_VALID_ATTR))) { |
| 224 | ret = EFI_INVALID_PARAMETER; |
| 225 | goto out; |
| 226 | } |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 227 | |
Heinrich Schuchardt | 143acd1 | 2018-09-12 18:43:58 +0200 | [diff] [blame^] | 228 | /* Open file */ |
| 229 | *new_handle = file_open(fh->fs, fh, file_name, open_mode, attributes); |
| 230 | if (*new_handle) |
| 231 | ret = EFI_SUCCESS; |
| 232 | else |
| 233 | ret = EFI_NOT_FOUND; |
| 234 | out: |
| 235 | return EFI_EXIT(ret); |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | static efi_status_t file_close(struct file_handle *fh) |
| 239 | { |
| 240 | fs_closedir(fh->dirs); |
| 241 | free(fh); |
| 242 | return EFI_SUCCESS; |
| 243 | } |
| 244 | |
| 245 | static efi_status_t EFIAPI efi_file_close(struct efi_file_handle *file) |
| 246 | { |
| 247 | struct file_handle *fh = to_fh(file); |
| 248 | EFI_ENTRY("%p", file); |
| 249 | return EFI_EXIT(file_close(fh)); |
| 250 | } |
| 251 | |
| 252 | static efi_status_t EFIAPI efi_file_delete(struct efi_file_handle *file) |
| 253 | { |
| 254 | struct file_handle *fh = to_fh(file); |
AKASHI Takahiro | d39f6a6 | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 255 | efi_status_t ret = EFI_SUCCESS; |
| 256 | |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 257 | EFI_ENTRY("%p", file); |
AKASHI Takahiro | d39f6a6 | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 258 | |
| 259 | if (set_blk_dev(fh)) { |
| 260 | ret = EFI_DEVICE_ERROR; |
| 261 | goto error; |
| 262 | } |
| 263 | |
| 264 | if (fs_unlink(fh->path)) |
| 265 | ret = EFI_DEVICE_ERROR; |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 266 | file_close(fh); |
AKASHI Takahiro | d39f6a6 | 2018-09-11 15:59:16 +0900 | [diff] [blame] | 267 | |
| 268 | error: |
| 269 | return EFI_EXIT(ret); |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | static efi_status_t file_read(struct file_handle *fh, u64 *buffer_size, |
| 273 | void *buffer) |
| 274 | { |
| 275 | loff_t actread; |
Alexander Graf | d5a5a5a | 2018-08-08 03:54:32 -0600 | [diff] [blame] | 276 | /* fs_read expects buffer address, not pointer */ |
| 277 | uintptr_t buffer_addr = (uintptr_t)map_to_sysmem(buffer); |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 278 | |
Alexander Graf | d5a5a5a | 2018-08-08 03:54:32 -0600 | [diff] [blame] | 279 | if (fs_read(fh->path, buffer_addr, fh->offset, |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 280 | *buffer_size, &actread)) |
| 281 | return EFI_DEVICE_ERROR; |
| 282 | |
| 283 | *buffer_size = actread; |
| 284 | fh->offset += actread; |
| 285 | |
| 286 | return EFI_SUCCESS; |
| 287 | } |
| 288 | |
| 289 | static efi_status_t dir_read(struct file_handle *fh, u64 *buffer_size, |
| 290 | void *buffer) |
| 291 | { |
| 292 | struct efi_file_info *info = buffer; |
| 293 | struct fs_dirent *dent; |
| 294 | unsigned int required_size; |
| 295 | |
| 296 | if (!fh->dirs) { |
| 297 | assert(fh->offset == 0); |
| 298 | fh->dirs = fs_opendir(fh->path); |
| 299 | if (!fh->dirs) |
| 300 | return EFI_DEVICE_ERROR; |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * So this is a bit awkward. Since fs layer is stateful and we |
| 305 | * can't rewind an entry, in the EFI_BUFFER_TOO_SMALL case below |
| 306 | * we might have to return without consuming the dent.. so we |
| 307 | * have to stash it for next call. |
| 308 | */ |
| 309 | if (fh->dent) { |
| 310 | dent = fh->dent; |
| 311 | fh->dent = NULL; |
| 312 | } else { |
| 313 | dent = fs_readdir(fh->dirs); |
| 314 | } |
| 315 | |
| 316 | |
| 317 | if (!dent) { |
| 318 | /* no more files in directory: */ |
| 319 | /* workaround shim.efi bug/quirk.. as find_boot_csv() |
| 320 | * loops through directory contents, it initially calls |
| 321 | * read w/ zero length buffer to find out how much mem |
| 322 | * to allocate for the EFI_FILE_INFO, then allocates, |
| 323 | * and then calls a 2nd time. If we return size of |
| 324 | * zero the first time, it happily passes that to |
| 325 | * AllocateZeroPool(), and when that returns NULL it |
| 326 | * thinks it is EFI_OUT_OF_RESOURCES. So on first |
| 327 | * call return a non-zero size: |
| 328 | */ |
| 329 | if (*buffer_size == 0) |
| 330 | *buffer_size = sizeof(*info); |
| 331 | else |
| 332 | *buffer_size = 0; |
| 333 | return EFI_SUCCESS; |
| 334 | } |
| 335 | |
| 336 | /* check buffer size: */ |
| 337 | required_size = sizeof(*info) + 2 * (strlen(dent->name) + 1); |
| 338 | if (*buffer_size < required_size) { |
| 339 | *buffer_size = required_size; |
| 340 | fh->dent = dent; |
| 341 | return EFI_BUFFER_TOO_SMALL; |
| 342 | } |
| 343 | |
| 344 | *buffer_size = required_size; |
| 345 | memset(info, 0, required_size); |
| 346 | |
| 347 | info->size = required_size; |
| 348 | info->file_size = dent->size; |
| 349 | info->physical_size = dent->size; |
| 350 | |
| 351 | if (dent->type == FS_DT_DIR) |
| 352 | info->attribute |= EFI_FILE_DIRECTORY; |
| 353 | |
| 354 | ascii2unicode((u16 *)info->file_name, dent->name); |
| 355 | |
| 356 | fh->offset++; |
| 357 | |
| 358 | return EFI_SUCCESS; |
| 359 | } |
| 360 | |
| 361 | static efi_status_t EFIAPI efi_file_read(struct efi_file_handle *file, |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 362 | efi_uintn_t *buffer_size, void *buffer) |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 363 | { |
| 364 | struct file_handle *fh = to_fh(file); |
| 365 | efi_status_t ret = EFI_SUCCESS; |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 366 | u64 bs; |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 367 | |
| 368 | EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer); |
| 369 | |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 370 | if (!buffer_size || !buffer) { |
| 371 | ret = EFI_INVALID_PARAMETER; |
| 372 | goto error; |
| 373 | } |
| 374 | |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 375 | if (set_blk_dev(fh)) { |
| 376 | ret = EFI_DEVICE_ERROR; |
| 377 | goto error; |
| 378 | } |
| 379 | |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 380 | bs = *buffer_size; |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 381 | if (fh->isdir) |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 382 | ret = dir_read(fh, &bs, buffer); |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 383 | else |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 384 | ret = file_read(fh, &bs, buffer); |
| 385 | if (bs <= SIZE_MAX) |
| 386 | *buffer_size = bs; |
| 387 | else |
| 388 | *buffer_size = SIZE_MAX; |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 389 | |
| 390 | error: |
| 391 | return EFI_EXIT(ret); |
| 392 | } |
| 393 | |
| 394 | static efi_status_t EFIAPI efi_file_write(struct efi_file_handle *file, |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 395 | efi_uintn_t *buffer_size, |
| 396 | void *buffer) |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 397 | { |
| 398 | struct file_handle *fh = to_fh(file); |
| 399 | efi_status_t ret = EFI_SUCCESS; |
| 400 | loff_t actwrite; |
| 401 | |
| 402 | EFI_ENTRY("%p, %p, %p", file, buffer_size, buffer); |
| 403 | |
| 404 | if (set_blk_dev(fh)) { |
| 405 | ret = EFI_DEVICE_ERROR; |
| 406 | goto error; |
| 407 | } |
| 408 | |
| 409 | if (fs_write(fh->path, (ulong)buffer, fh->offset, *buffer_size, |
| 410 | &actwrite)) { |
| 411 | ret = EFI_DEVICE_ERROR; |
| 412 | goto error; |
| 413 | } |
| 414 | |
| 415 | *buffer_size = actwrite; |
| 416 | fh->offset += actwrite; |
| 417 | |
| 418 | error: |
| 419 | return EFI_EXIT(ret); |
| 420 | } |
| 421 | |
| 422 | static efi_status_t EFIAPI efi_file_getpos(struct efi_file_handle *file, |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 423 | efi_uintn_t *pos) |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 424 | { |
| 425 | struct file_handle *fh = to_fh(file); |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 426 | |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 427 | EFI_ENTRY("%p, %p", file, pos); |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 428 | |
| 429 | if (fh->offset <= SIZE_MAX) { |
| 430 | *pos = fh->offset; |
| 431 | return EFI_EXIT(EFI_SUCCESS); |
| 432 | } else { |
| 433 | return EFI_EXIT(EFI_DEVICE_ERROR); |
| 434 | } |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static efi_status_t EFIAPI efi_file_setpos(struct efi_file_handle *file, |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 438 | efi_uintn_t pos) |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 439 | { |
| 440 | struct file_handle *fh = to_fh(file); |
| 441 | efi_status_t ret = EFI_SUCCESS; |
| 442 | |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 443 | EFI_ENTRY("%p, %zu", file, pos); |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 444 | |
| 445 | if (fh->isdir) { |
| 446 | if (pos != 0) { |
| 447 | ret = EFI_UNSUPPORTED; |
| 448 | goto error; |
| 449 | } |
| 450 | fs_closedir(fh->dirs); |
| 451 | fh->dirs = NULL; |
| 452 | } |
| 453 | |
| 454 | if (pos == ~0ULL) { |
| 455 | loff_t file_size; |
| 456 | |
| 457 | if (set_blk_dev(fh)) { |
| 458 | ret = EFI_DEVICE_ERROR; |
| 459 | goto error; |
| 460 | } |
| 461 | |
| 462 | if (fs_size(fh->path, &file_size)) { |
| 463 | ret = EFI_DEVICE_ERROR; |
| 464 | goto error; |
| 465 | } |
| 466 | |
| 467 | pos = file_size; |
| 468 | } |
| 469 | |
| 470 | fh->offset = pos; |
| 471 | |
| 472 | error: |
| 473 | return EFI_EXIT(ret); |
| 474 | } |
| 475 | |
| 476 | static efi_status_t EFIAPI efi_file_getinfo(struct efi_file_handle *file, |
Heinrich Schuchardt | 9c9021e | 2018-04-04 15:42:09 +0200 | [diff] [blame] | 477 | const efi_guid_t *info_type, |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 478 | efi_uintn_t *buffer_size, |
| 479 | void *buffer) |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 480 | { |
| 481 | struct file_handle *fh = to_fh(file); |
| 482 | efi_status_t ret = EFI_SUCCESS; |
| 483 | |
| 484 | EFI_ENTRY("%p, %p, %p, %p", file, info_type, buffer_size, buffer); |
| 485 | |
| 486 | if (!guidcmp(info_type, &efi_file_info_guid)) { |
| 487 | struct efi_file_info *info = buffer; |
| 488 | char *filename = basename(fh); |
| 489 | unsigned int required_size; |
| 490 | loff_t file_size; |
| 491 | |
| 492 | /* check buffer size: */ |
| 493 | required_size = sizeof(*info) + 2 * (strlen(filename) + 1); |
| 494 | if (*buffer_size < required_size) { |
| 495 | *buffer_size = required_size; |
| 496 | ret = EFI_BUFFER_TOO_SMALL; |
| 497 | goto error; |
| 498 | } |
| 499 | |
| 500 | if (set_blk_dev(fh)) { |
| 501 | ret = EFI_DEVICE_ERROR; |
| 502 | goto error; |
| 503 | } |
| 504 | |
| 505 | if (fs_size(fh->path, &file_size)) { |
| 506 | ret = EFI_DEVICE_ERROR; |
| 507 | goto error; |
| 508 | } |
| 509 | |
| 510 | memset(info, 0, required_size); |
| 511 | |
| 512 | info->size = required_size; |
| 513 | info->file_size = file_size; |
| 514 | info->physical_size = file_size; |
| 515 | |
| 516 | if (fh->isdir) |
| 517 | info->attribute |= EFI_FILE_DIRECTORY; |
| 518 | |
| 519 | ascii2unicode((u16 *)info->file_name, filename); |
Heinrich Schuchardt | 9e6835e | 2018-04-04 15:42:11 +0200 | [diff] [blame] | 520 | } else if (!guidcmp(info_type, &efi_file_system_info_guid)) { |
| 521 | struct efi_file_system_info *info = buffer; |
| 522 | disk_partition_t part; |
| 523 | efi_uintn_t required_size; |
| 524 | int r; |
| 525 | |
| 526 | if (fh->fs->part >= 1) |
| 527 | r = part_get_info(fh->fs->desc, fh->fs->part, &part); |
| 528 | else |
| 529 | r = part_get_info_whole_disk(fh->fs->desc, &part); |
| 530 | if (r < 0) { |
| 531 | ret = EFI_DEVICE_ERROR; |
| 532 | goto error; |
| 533 | } |
| 534 | required_size = sizeof(info) + 2 * |
| 535 | (strlen((const char *)part.name) + 1); |
| 536 | if (*buffer_size < required_size) { |
| 537 | *buffer_size = required_size; |
| 538 | ret = EFI_BUFFER_TOO_SMALL; |
| 539 | goto error; |
| 540 | } |
| 541 | |
| 542 | memset(info, 0, required_size); |
| 543 | |
| 544 | info->size = required_size; |
| 545 | info->read_only = true; |
| 546 | info->volume_size = part.size * part.blksz; |
| 547 | info->free_space = 0; |
| 548 | info->block_size = part.blksz; |
| 549 | /* |
| 550 | * TODO: The volume label is not available in U-Boot. |
| 551 | * Use the partition name as substitute. |
| 552 | */ |
| 553 | ascii2unicode((u16 *)info->volume_label, |
| 554 | (const char *)part.name); |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 555 | } else { |
| 556 | ret = EFI_UNSUPPORTED; |
| 557 | } |
| 558 | |
| 559 | error: |
| 560 | return EFI_EXIT(ret); |
| 561 | } |
| 562 | |
| 563 | static efi_status_t EFIAPI efi_file_setinfo(struct efi_file_handle *file, |
Heinrich Schuchardt | 9c9021e | 2018-04-04 15:42:09 +0200 | [diff] [blame] | 564 | const efi_guid_t *info_type, |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 565 | efi_uintn_t buffer_size, |
| 566 | void *buffer) |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 567 | { |
Heinrich Schuchardt | b6dd577 | 2018-04-03 22:37:11 +0200 | [diff] [blame] | 568 | EFI_ENTRY("%p, %p, %zu, %p", file, info_type, buffer_size, buffer); |
| 569 | |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 570 | return EFI_EXIT(EFI_UNSUPPORTED); |
| 571 | } |
| 572 | |
| 573 | static efi_status_t EFIAPI efi_file_flush(struct efi_file_handle *file) |
| 574 | { |
| 575 | EFI_ENTRY("%p", file); |
| 576 | return EFI_EXIT(EFI_SUCCESS); |
| 577 | } |
| 578 | |
| 579 | static const struct efi_file_handle efi_file_handle_protocol = { |
| 580 | .rev = EFI_FILE_PROTOCOL_REVISION, |
| 581 | .open = efi_file_open, |
| 582 | .close = efi_file_close, |
| 583 | .delete = efi_file_delete, |
| 584 | .read = efi_file_read, |
| 585 | .write = efi_file_write, |
| 586 | .getpos = efi_file_getpos, |
| 587 | .setpos = efi_file_setpos, |
| 588 | .getinfo = efi_file_getinfo, |
| 589 | .setinfo = efi_file_setinfo, |
| 590 | .flush = efi_file_flush, |
| 591 | }; |
| 592 | |
| 593 | struct efi_file_handle *efi_file_from_path(struct efi_device_path *fp) |
| 594 | { |
| 595 | struct efi_simple_file_system_protocol *v; |
| 596 | struct efi_file_handle *f; |
| 597 | efi_status_t ret; |
| 598 | |
| 599 | v = efi_fs_from_path(fp); |
| 600 | if (!v) |
| 601 | return NULL; |
| 602 | |
| 603 | EFI_CALL(ret = v->open_volume(v, &f)); |
| 604 | if (ret != EFI_SUCCESS) |
| 605 | return NULL; |
| 606 | |
| 607 | /* skip over device-path nodes before the file path: */ |
| 608 | while (fp && !EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) |
| 609 | fp = efi_dp_next(fp); |
| 610 | |
| 611 | while (fp) { |
| 612 | struct efi_device_path_file_path *fdp = |
| 613 | container_of(fp, struct efi_device_path_file_path, dp); |
| 614 | struct efi_file_handle *f2; |
| 615 | |
| 616 | if (!EFI_DP_TYPE(fp, MEDIA_DEVICE, FILE_PATH)) { |
| 617 | printf("bad file path!\n"); |
| 618 | f->close(f); |
| 619 | return NULL; |
| 620 | } |
| 621 | |
| 622 | EFI_CALL(ret = f->open(f, &f2, (s16 *)fdp->str, |
| 623 | EFI_FILE_MODE_READ, 0)); |
| 624 | if (ret != EFI_SUCCESS) |
| 625 | return NULL; |
| 626 | |
| 627 | fp = efi_dp_next(fp); |
| 628 | |
| 629 | EFI_CALL(f->close(f)); |
| 630 | f = f2; |
| 631 | } |
| 632 | |
| 633 | return f; |
| 634 | } |
| 635 | |
| 636 | static efi_status_t EFIAPI |
| 637 | efi_open_volume(struct efi_simple_file_system_protocol *this, |
| 638 | struct efi_file_handle **root) |
| 639 | { |
| 640 | struct file_system *fs = to_fs(this); |
| 641 | |
| 642 | EFI_ENTRY("%p, %p", this, root); |
| 643 | |
AKASHI Takahiro | 5bc84a1 | 2018-09-11 15:59:12 +0900 | [diff] [blame] | 644 | *root = file_open(fs, NULL, NULL, 0, 0); |
Rob Clark | 2a92080 | 2017-09-13 18:05:34 -0400 | [diff] [blame] | 645 | |
| 646 | return EFI_EXIT(EFI_SUCCESS); |
| 647 | } |
| 648 | |
| 649 | struct efi_simple_file_system_protocol * |
| 650 | efi_simple_file_system(struct blk_desc *desc, int part, |
| 651 | struct efi_device_path *dp) |
| 652 | { |
| 653 | struct file_system *fs; |
| 654 | |
| 655 | fs = calloc(1, sizeof(*fs)); |
| 656 | fs->base.rev = EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_REVISION; |
| 657 | fs->base.open_volume = efi_open_volume; |
| 658 | fs->desc = desc; |
| 659 | fs->part = part; |
| 660 | fs->dp = dp; |
| 661 | |
| 662 | return &fs->base; |
| 663 | } |