| 1 |
// SPDX-License-Identifier: BSD-2-Clause-Patent |
| 2 |
/* |
| 3 |
* Copyright 2012 <[email protected]> |
| 4 |
*/ |
| 5 |
#include "shim.h" |
| 6 |
|
| 7 |
EFI_STATUS |
| 8 |
simple_file_open_by_handle(EFI_HANDLE device, CHAR16 *name, EFI_FILE **file, UINT64 mode) |
| 9 |
{ |
| 10 |
EFI_STATUS efi_status; |
| 11 |
EFI_FILE_IO_INTERFACE *drive; |
| 12 |
EFI_FILE *root; |
| 13 |
|
| 14 |
efi_status = BS->HandleProtocol(device, &EFI_SIMPLE_FILE_SYSTEM_GUID, |
| 15 |
(void **)&drive); |
| 16 |
if (EFI_ERROR(efi_status)) { |
| 17 |
console_print(L"Unable to find simple file protocol (%d)\n", |
| 18 |
efi_status); |
| 19 |
goto error; |
| 20 |
} |
| 21 |
|
| 22 |
efi_status = drive->OpenVolume(drive, &root); |
| 23 |
if (EFI_ERROR(efi_status)) { |
| 24 |
console_print(L"Failed to open drive volume (%d)\n", efi_status); |
| 25 |
goto error; |
| 26 |
} |
| 27 |
|
| 28 |
efi_status = root->Open(root, file, name, mode, 0); |
| 29 |
|
| 30 |
error: |
| 31 |
return efi_status; |
| 32 |
} |
| 33 |
|
| 34 |
EFI_STATUS |
| 35 |
simple_file_open(EFI_HANDLE image, CHAR16 *name, EFI_FILE **file, UINT64 mode) |
| 36 |
{ |
| 37 |
EFI_STATUS efi_status; |
| 38 |
EFI_HANDLE device; |
| 39 |
EFI_LOADED_IMAGE *li; |
| 40 |
EFI_DEVICE_PATH *loadpath = NULL; |
| 41 |
CHAR16 *PathName = NULL; |
| 42 |
|
| 43 |
efi_status = BS->HandleProtocol(image, &IMAGE_PROTOCOL, |
| 44 |
(void **) &li); |
| 45 |
if (EFI_ERROR(efi_status)) |
| 46 |
return simple_file_open_by_handle(image, name, file, mode); |
| 47 |
|
| 48 |
efi_status = generate_path(name, li, &loadpath, &PathName); |
| 49 |
if (EFI_ERROR(efi_status)) { |
| 50 |
console_print(L"Unable to generate load path for %s\n", name); |
| 51 |
return efi_status; |
| 52 |
} |
| 53 |
|
| 54 |
device = li->DeviceHandle; |
| 55 |
|
| 56 |
efi_status = simple_file_open_by_handle(device, PathName, file, mode); |
| 57 |
|
| 58 |
FreePool(PathName); |
| 59 |
FreePool(loadpath); |
| 60 |
|
| 61 |
return efi_status; |
| 62 |
} |
| 63 |
|
| 64 |
EFI_STATUS |
| 65 |
simple_dir_read_all_by_handle(EFI_HANDLE image UNUSED, EFI_FILE *file, |
| 66 |
CHAR16* name, EFI_FILE_INFO **entries, int *count) |
| 67 |
{ |
| 68 |
EFI_STATUS efi_status; |
| 69 |
char buf[4096]; |
| 70 |
UINTN size = sizeof(buf); |
| 71 |
EFI_FILE_INFO *fi = (void *)buf; |
| 72 |
|
| 73 |
efi_status = file->GetInfo(file, &EFI_FILE_INFO_GUID, &size, fi); |
| 74 |
if (EFI_ERROR(efi_status)) { |
| 75 |
console_print(L"Failed to get file info\n"); |
| 76 |
goto out; |
| 77 |
} |
| 78 |
if ((fi->Attribute & EFI_FILE_DIRECTORY) == 0) { |
| 79 |
console_print(L"Not a directory %s\n", name); |
| 80 |
efi_status = EFI_INVALID_PARAMETER; |
| 81 |
goto out; |
| 82 |
} |
| 83 |
size = 0; |
| 84 |
*count = 0; |
| 85 |
for (;;) { |
| 86 |
UINTN len = sizeof(buf); |
| 87 |
efi_status = file->Read(file, &len, buf); |
| 88 |
if (EFI_ERROR(efi_status) || len == 0) |
| 89 |
break; |
| 90 |
(*count)++; |
| 91 |
size += len; |
| 92 |
} |
| 93 |
file->SetPosition(file, 0); |
| 94 |
|
| 95 |
char *ptr = AllocatePool(size); |
| 96 |
*entries = (EFI_FILE_INFO *)ptr; |
| 97 |
if (!*entries) |
| 98 |
return EFI_OUT_OF_RESOURCES; |
| 99 |
int i; |
| 100 |
for (i = 0; i < *count; i++) { |
| 101 |
UINTN len = size; |
| 102 |
file->Read(file, &len, ptr); |
| 103 |
ptr += len; |
| 104 |
size -= len; |
| 105 |
} |
| 106 |
efi_status = EFI_SUCCESS; |
| 107 |
out: |
| 108 |
file->Close(file); |
| 109 |
if (EFI_ERROR(efi_status) && *entries) { |
| 110 |
FreePool(*entries); |
| 111 |
*entries = NULL; |
| 112 |
} |
| 113 |
return efi_status; |
| 114 |
} |
| 115 |
|
| 116 |
EFI_STATUS |
| 117 |
simple_dir_read_all(EFI_HANDLE image, CHAR16 *name, EFI_FILE_INFO **entries, |
| 118 |
int *count) |
| 119 |
{ |
| 120 |
EFI_FILE *file; |
| 121 |
EFI_STATUS efi_status; |
| 122 |
|
| 123 |
efi_status = simple_file_open(image, name, &file, EFI_FILE_MODE_READ); |
| 124 |
if (EFI_ERROR(efi_status)) { |
| 125 |
console_print(L"failed to open file %s: %d\n", name, efi_status); |
| 126 |
return efi_status; |
| 127 |
} |
| 128 |
|
| 129 |
return simple_dir_read_all_by_handle(image, file, name, entries, count); |
| 130 |
} |
| 131 |
|
| 132 |
EFI_STATUS |
| 133 |
simple_file_read_all(EFI_FILE *file, UINTN *size, void **buffer) |
| 134 |
{ |
| 135 |
EFI_STATUS efi_status; |
| 136 |
EFI_FILE_INFO *fi; |
| 137 |
char buf[1024]; |
| 138 |
|
| 139 |
*size = sizeof(buf); |
| 140 |
fi = (void *)buf; |
| 141 |
|
| 142 |
efi_status = file->GetInfo(file, &EFI_FILE_INFO_GUID, size, fi); |
| 143 |
if (EFI_ERROR(efi_status)) { |
| 144 |
console_print(L"Failed to get file info\n"); |
| 145 |
return efi_status; |
| 146 |
} |
| 147 |
|
| 148 |
*size = fi->FileSize; |
| 149 |
|
| 150 |
*buffer = AllocatePool(*size); |
| 151 |
if (!*buffer) { |
| 152 |
console_print(L"Failed to allocate buffer of size %d\n", *size); |
| 153 |
return EFI_OUT_OF_RESOURCES; |
| 154 |
} |
| 155 |
|
| 156 |
efi_status = file->Read(file, size, *buffer); |
| 157 |
return efi_status; |
| 158 |
} |
| 159 |
|
| 160 |
|
| 161 |
EFI_STATUS |
| 162 |
simple_file_write_all(EFI_FILE *file, UINTN size, void *buffer) |
| 163 |
{ |
| 164 |
EFI_STATUS efi_status; |
| 165 |
|
| 166 |
efi_status = file->Write(file, &size, buffer); |
| 167 |
return efi_status; |
| 168 |
} |
| 169 |
|
| 170 |
EFI_STATUS |
| 171 |
simple_volume_selector(CHAR16 **title, CHAR16 **selected, EFI_HANDLE *h) |
| 172 |
{ |
| 173 |
UINTN count, i, j; |
| 174 |
EFI_HANDLE *vol_handles = NULL; |
| 175 |
EFI_STATUS efi_status; |
| 176 |
CHAR16 **entries; |
| 177 |
int val; |
| 178 |
|
| 179 |
efi_status = BS->LocateHandleBuffer(ByProtocol, |
| 180 |
&EFI_SIMPLE_FILE_SYSTEM_GUID, |
| 181 |
NULL, &count, &vol_handles); |
| 182 |
if (EFI_ERROR(efi_status)) |
| 183 |
return efi_status; |
| 184 |
if (!count || !vol_handles) |
| 185 |
return EFI_NOT_FOUND; |
| 186 |
|
| 187 |
entries = AllocateZeroPool(sizeof(CHAR16 *) * (count+1)); |
| 188 |
if (!entries) |
| 189 |
return EFI_OUT_OF_RESOURCES; |
| 190 |
|
| 191 |
for (i = 0, j = 0; i < count; i++) { |
| 192 |
char buf[4096]; |
| 193 |
UINTN size = sizeof(buf); |
| 194 |
EFI_FILE_SYSTEM_INFO *fi = (void *)buf; |
| 195 |
EFI_FILE *root; |
| 196 |
CHAR16 *name; |
| 197 |
EFI_FILE_IO_INTERFACE *drive; |
| 198 |
|
| 199 |
efi_status = BS->HandleProtocol(vol_handles[i], |
| 200 |
&EFI_SIMPLE_FILE_SYSTEM_GUID, |
| 201 |
(void **) &drive); |
| 202 |
if (EFI_ERROR(efi_status) || !drive) |
| 203 |
continue; |
| 204 |
|
| 205 |
efi_status = drive->OpenVolume(drive, &root); |
| 206 |
if (EFI_ERROR(efi_status)) |
| 207 |
continue; |
| 208 |
|
| 209 |
efi_status = root->GetInfo(root, &EFI_FILE_SYSTEM_INFO_GUID, |
| 210 |
&size, fi); |
| 211 |
/* If GetInfo fails, try to form a name from DevicePath. */ |
| 212 |
if (EFI_ERROR(efi_status)){ |
| 213 |
name = NULL; |
| 214 |
} else { |
| 215 |
name = fi->VolumeLabel; |
| 216 |
} |
| 217 |
|
| 218 |
if (!name || StrLen(name) == 0 || StrCmp(name, L" ") == 0) |
| 219 |
name = DevicePathToStr(DevicePathFromHandle(vol_handles[i])); |
| 220 |
|
| 221 |
entries[j] = AllocatePool((StrLen(name) + 2) * sizeof(CHAR16)); |
| 222 |
if (!entries[j]) |
| 223 |
break; |
| 224 |
StrCpy(entries[j++], name); |
| 225 |
} |
| 226 |
entries[j] = NULL; |
| 227 |
|
| 228 |
val = console_select(title, entries, 0); |
| 229 |
|
| 230 |
if (val >= 0) { |
| 231 |
*selected = AllocatePool((StrLen(entries[val]) + 1) * sizeof(CHAR16)); |
| 232 |
if (*selected) { |
| 233 |
StrCpy(*selected , entries[val]); |
| 234 |
} |
| 235 |
*h = vol_handles[val]; |
| 236 |
} else { |
| 237 |
*selected = NULL; |
| 238 |
*h = 0; |
| 239 |
} |
| 240 |
|
| 241 |
for (i = 0; i < count; i++) { |
| 242 |
if (entries[i]) |
| 243 |
FreePool(entries[i]); |
| 244 |
} |
| 245 |
FreePool(entries); |
| 246 |
FreePool(vol_handles); |
| 247 |
|
| 248 |
return EFI_SUCCESS; |
| 249 |
} |
| 250 |
|
| 251 |
EFI_STATUS |
| 252 |
simple_dir_filter(EFI_HANDLE image, CHAR16 *name, CHAR16 *filter, |
| 253 |
CHAR16 ***result, int *count, EFI_FILE_INFO **entries) |
| 254 |
{ |
| 255 |
EFI_STATUS efi_status; |
| 256 |
int tot, offs = StrLen(filter), i, c, filtercount = 1; |
| 257 |
EFI_FILE_INFO *next; |
| 258 |
void *ptr; |
| 259 |
CHAR16 *newfilter = AllocatePool((StrLen(filter) + 1) * sizeof(CHAR16)), |
| 260 |
**filterarr; |
| 261 |
|
| 262 |
if (!newfilter) |
| 263 |
return EFI_OUT_OF_RESOURCES; |
| 264 |
|
| 265 |
/* just in case efi ever stops writeable strings */ |
| 266 |
StrCpy(newfilter, filter); |
| 267 |
|
| 268 |
for (i = 0; i < offs; i++) { |
| 269 |
if (filter[i] == '|') |
| 270 |
filtercount++; |
| 271 |
} |
| 272 |
filterarr = AllocatePool(filtercount * sizeof(void *)); |
| 273 |
if (!filterarr) |
| 274 |
return EFI_OUT_OF_RESOURCES; |
| 275 |
c = 0; |
| 276 |
filterarr[c++] = newfilter; |
| 277 |
for (i = 0; i < offs; i++) { |
| 278 |
if (filter[i] == '|') { |
| 279 |
newfilter[i] = '\0'; |
| 280 |
filterarr[c++] = &newfilter[i+1]; |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
*count = 0; |
| 285 |
|
| 286 |
efi_status = simple_dir_read_all(image, name, entries, &tot); |
| 287 |
if (EFI_ERROR(efi_status)) |
| 288 |
goto out; |
| 289 |
ptr = next = *entries; |
| 290 |
|
| 291 |
for (i = 0; next && i < tot; i++) { |
| 292 |
int len = StrLen(next->FileName); |
| 293 |
|
| 294 |
for (c = 0; c < filtercount; c++) { |
| 295 |
offs = StrLen(filterarr[c]); |
| 296 |
|
| 297 |
if (StrCmp(&next->FileName[len - offs], filterarr[c]) == 0 |
| 298 |
|| (next->Attribute & EFI_FILE_DIRECTORY)) { |
| 299 |
(*count)++; |
| 300 |
break; |
| 301 |
} |
| 302 |
} |
| 303 |
ptr += offsetof(EFI_FILE_INFO, FileName) + (len + 1)*sizeof(CHAR16); |
| 304 |
next = ptr; |
| 305 |
} |
| 306 |
if (*count) |
| 307 |
*result = AllocatePool(((*count) + 1) * sizeof(void *)); |
| 308 |
else |
| 309 |
*result = AllocatePool(2 * sizeof(void *)); |
| 310 |
|
| 311 |
*count = 0; |
| 312 |
ptr = next = *entries; |
| 313 |
|
| 314 |
for (i = 0; next && i < tot; i++) { |
| 315 |
int len = StrLen(next->FileName); |
| 316 |
|
| 317 |
if (StrCmp(next->FileName, L".") == 0) |
| 318 |
/* ignore . directory */ |
| 319 |
goto next; |
| 320 |
|
| 321 |
if (next->Attribute & EFI_FILE_DIRECTORY) { |
| 322 |
(*result)[(*count)] = PoolPrint(L"%s/", next->FileName); |
| 323 |
if (!(*result)[(*count)]) { |
| 324 |
console_print(L"Failed to allocate buffer"); |
| 325 |
return EFI_OUT_OF_RESOURCES; |
| 326 |
} |
| 327 |
(*count)++; |
| 328 |
goto next; |
| 329 |
} |
| 330 |
|
| 331 |
for (c = 0; c < filtercount; c++) { |
| 332 |
offs = StrLen(filterarr[c]); |
| 333 |
|
| 334 |
if (StrCmp(&next->FileName[len - offs], filterarr[c]) == 0) { |
| 335 |
(*result)[(*count)] = StrDuplicate(next->FileName); |
| 336 |
if (!(*result)[(*count)]) { |
| 337 |
console_print(L"Failed to allocate buffer"); |
| 338 |
return EFI_OUT_OF_RESOURCES; |
| 339 |
} |
| 340 |
(*count)++; |
| 341 |
} else { |
| 342 |
continue; |
| 343 |
} |
| 344 |
break; |
| 345 |
} |
| 346 |
|
| 347 |
next: |
| 348 |
if (StrCmp(next->FileName, L"..") == 0) { |
| 349 |
/* place .. directory first */ |
| 350 |
CHAR16 *tmp = (*result)[(*count) - 1]; |
| 351 |
|
| 352 |
(*result)[(*count) - 1] = (*result)[0]; |
| 353 |
(*result)[0] = tmp; |
| 354 |
} |
| 355 |
|
| 356 |
ptr += offsetof(EFI_FILE_INFO, FileName) + (len + 1)*sizeof(CHAR16); |
| 357 |
next = ptr; |
| 358 |
} |
| 359 |
if (*count == 0) { |
| 360 |
/* no entries at all ... can happen because top level dir has no . or .. */ |
| 361 |
(*result)[(*count)++] = L"./"; |
| 362 |
} |
| 363 |
(*result)[*count] = NULL; |
| 364 |
efi_status = EFI_SUCCESS; |
| 365 |
|
| 366 |
out: |
| 367 |
if (EFI_ERROR(efi_status)) { |
| 368 |
if (*entries) |
| 369 |
FreePool(*entries); |
| 370 |
*entries = NULL; |
| 371 |
if (*result) |
| 372 |
FreePool(*result); |
| 373 |
*result = NULL; |
| 374 |
} |
| 375 |
return efi_status; |
| 376 |
} |
| 377 |
|
| 378 |
static void |
| 379 |
free_entries(CHAR16 **entries, int count) |
| 380 |
{ |
| 381 |
int i; |
| 382 |
|
| 383 |
for (i = 0; i<count; i++) |
| 384 |
FreePool(entries[i]); |
| 385 |
} |
| 386 |
|
| 387 |
void |
| 388 |
simple_file_selector(EFI_HANDLE * im, CHAR16 ** title, CHAR16 * name, |
| 389 |
CHAR16 * filter, CHAR16 ** result) |
| 390 |
{ |
| 391 |
EFI_STATUS efi_status; |
| 392 |
CHAR16 **entries = NULL; |
| 393 |
EFI_FILE_INFO *dmp = NULL; |
| 394 |
int count, select, len; |
| 395 |
CHAR16 *newname, *selected; |
| 396 |
|
| 397 |
*result = NULL; |
| 398 |
if (!name) |
| 399 |
name = L"\\"; |
| 400 |
if (!filter) |
| 401 |
filter = L""; |
| 402 |
if (!*im) { |
| 403 |
EFI_HANDLE h; |
| 404 |
CHAR16 *volname = NULL; |
| 405 |
|
| 406 |
efi_status = simple_volume_selector(title, &volname, &h); |
| 407 |
if (EFI_ERROR(efi_status) || !volname) |
| 408 |
return; |
| 409 |
FreePool(volname); |
| 410 |
*im = h; |
| 411 |
} |
| 412 |
|
| 413 |
newname = AllocatePool((StrLen(name) + 1) * sizeof(CHAR16)); |
| 414 |
if (!newname) |
| 415 |
return; |
| 416 |
|
| 417 |
StrCpy(newname, name); |
| 418 |
name = newname; |
| 419 |
|
| 420 |
redo: |
| 421 |
efi_status = simple_dir_filter(*im, name, filter, &entries, &count, |
| 422 |
&dmp); |
| 423 |
if (EFI_ERROR(efi_status)) |
| 424 |
goto out_free; |
| 425 |
|
| 426 |
select = console_select(title, entries, 0); |
| 427 |
if (select < 0) |
| 428 |
/* ESC key */ |
| 429 |
goto out_free; |
| 430 |
selected = entries[select]; |
| 431 |
/* note that memory used by selected is valid until dmp is freed */ |
| 432 |
len = StrLen(selected); |
| 433 |
if (selected[len - 1] == '/') { |
| 434 |
CHAR16 *newname; |
| 435 |
|
| 436 |
/* stay where we are */ |
| 437 |
if (StrCmp(selected, L"./") == 0) { |
| 438 |
free_entries(entries, count); |
| 439 |
FreePool(entries); |
| 440 |
entries = NULL; |
| 441 |
FreePool(dmp); |
| 442 |
goto redo; |
| 443 |
} else if (StrCmp(selected, L"../") == 0) { |
| 444 |
int i; |
| 445 |
|
| 446 |
for (i = StrLen(name); i > 0; --i) { |
| 447 |
if (name[i] == '\\') |
| 448 |
break; |
| 449 |
} |
| 450 |
if (i == 0) |
| 451 |
i = 1; |
| 452 |
|
| 453 |
if (StrCmp(name, L"\\") != 0 |
| 454 |
&& StrCmp(&name[i], L"..") != 0) { |
| 455 |
name[i] = '\0'; |
| 456 |
free_entries(entries, count); |
| 457 |
FreePool(entries); |
| 458 |
entries = NULL; |
| 459 |
FreePool(dmp); |
| 460 |
goto redo; |
| 461 |
} |
| 462 |
} |
| 463 |
newname = |
| 464 |
AllocatePool((StrLen(name) + len + 2) * sizeof(CHAR16)); |
| 465 |
if (!newname) |
| 466 |
goto out_free; |
| 467 |
StrCpy(newname, name); |
| 468 |
|
| 469 |
if (name[StrLen(name) - 1] != '\\') |
| 470 |
StrCat(newname, L"\\"); |
| 471 |
StrCat(newname, selected); |
| 472 |
/* remove trailing / */ |
| 473 |
newname[StrLen(newname) - 1] = '\0'; |
| 474 |
|
| 475 |
free_entries(entries, count); |
| 476 |
FreePool(entries); |
| 477 |
entries = NULL; |
| 478 |
FreePool(dmp); |
| 479 |
FreePool(name); |
| 480 |
name = newname; |
| 481 |
|
| 482 |
goto redo; |
| 483 |
} |
| 484 |
*result = AllocatePool((StrLen(name) + len + 2) * sizeof(CHAR16)); |
| 485 |
if (*result) { |
| 486 |
StrCpy(*result, name); |
| 487 |
if (name[StrLen(name) - 1] != '\\') |
| 488 |
StrCat(*result, L"\\"); |
| 489 |
StrCat(*result, selected); |
| 490 |
} |
| 491 |
|
| 492 |
out_free: |
| 493 |
if (dmp) |
| 494 |
FreePool(dmp); |
| 495 |
if (entries) { |
| 496 |
free_entries(entries, count); |
| 497 |
FreePool(entries); |
| 498 |
} |
| 499 |
FreePool(name); |
| 500 |
} |