| 1 |
// SPDX-License-Identifier: BSD-2-Clause-Patent |
| 2 |
/* |
| 3 |
* Copyright 2012 <[email protected]> |
| 4 |
* Code Copyright 2012 Red Hat, Inc <[email protected]> |
| 5 |
*/ |
| 6 |
#include "shim.h" |
| 7 |
|
| 8 |
EFI_STATUS |
| 9 |
generate_path(CHAR16* name, EFI_LOADED_IMAGE *li, EFI_DEVICE_PATH **path, CHAR16 **PathName) |
| 10 |
{ |
| 11 |
unsigned int pathlen; |
| 12 |
EFI_STATUS efi_status = EFI_SUCCESS; |
| 13 |
CHAR16 *devpathstr = DevicePathToStr(li->FilePath), |
| 14 |
*found = NULL; |
| 15 |
unsigned int i; |
| 16 |
|
| 17 |
for (i = 0; i < StrLen(devpathstr); i++) { |
| 18 |
if (devpathstr[i] == '/') |
| 19 |
devpathstr[i] = '\\'; |
| 20 |
if (devpathstr[i] == '\\') |
| 21 |
found = &devpathstr[i]; |
| 22 |
} |
| 23 |
if (!found) { |
| 24 |
pathlen = 0; |
| 25 |
} else { |
| 26 |
while (*(found - 1) == '\\') |
| 27 |
--found; |
| 28 |
*found = '\0'; |
| 29 |
pathlen = StrLen(devpathstr); |
| 30 |
} |
| 31 |
|
| 32 |
if (name[0] != '\\') |
| 33 |
pathlen++; |
| 34 |
|
| 35 |
*PathName = AllocatePool((pathlen + 1 + StrLen(name))*sizeof(CHAR16)); |
| 36 |
|
| 37 |
if (!*PathName) { |
| 38 |
console_print(L"Failed to allocate path buffer\n"); |
| 39 |
efi_status = EFI_OUT_OF_RESOURCES; |
| 40 |
goto error; |
| 41 |
} |
| 42 |
|
| 43 |
StrCpy(*PathName, devpathstr); |
| 44 |
|
| 45 |
if (name[0] != '\\') |
| 46 |
StrCat(*PathName, L"\\"); |
| 47 |
StrCat(*PathName, name); |
| 48 |
|
| 49 |
*path = FileDevicePath(li->DeviceHandle, *PathName); |
| 50 |
|
| 51 |
error: |
| 52 |
FreePool(devpathstr); |
| 53 |
|
| 54 |
return efi_status; |
| 55 |
} |
| 56 |
|
| 57 |
EFI_STATUS |
| 58 |
execute(EFI_HANDLE image, CHAR16 *name) |
| 59 |
{ |
| 60 |
EFI_STATUS efi_status; |
| 61 |
EFI_HANDLE h; |
| 62 |
EFI_LOADED_IMAGE *li; |
| 63 |
EFI_DEVICE_PATH *devpath; |
| 64 |
CHAR16 *PathName; |
| 65 |
|
| 66 |
efi_status = BS->HandleProtocol(image, &IMAGE_PROTOCOL, |
| 67 |
(void **) &li); |
| 68 |
if (EFI_ERROR(efi_status)) |
| 69 |
return efi_status; |
| 70 |
|
| 71 |
efi_status = generate_path(name, li, &devpath, &PathName); |
| 72 |
if (EFI_ERROR(efi_status)) |
| 73 |
return efi_status; |
| 74 |
|
| 75 |
efi_status = BS->LoadImage(FALSE, image, devpath, NULL, 0, &h); |
| 76 |
if (EFI_ERROR(efi_status)) |
| 77 |
goto out; |
| 78 |
|
| 79 |
efi_status = BS->StartImage(h, NULL, NULL); |
| 80 |
BS->UnloadImage(h); |
| 81 |
|
| 82 |
out: |
| 83 |
FreePool(PathName); |
| 84 |
FreePool(devpath); |
| 85 |
return efi_status; |
| 86 |
} |