Skip to content

Commit

Permalink
Extract is_removable_media_path() out of should_use_fallback()
Browse files Browse the repository at this point in the history
Simple refactoring that extracts the path checking on the given
loaded image. This will be useful to check if we were booted via
removable media path in other places.
  • Loading branch information
julian-klode authored and vathpela committed Oct 12, 2021
1 parent 885feaf commit 9dca459
Showing 1 changed file with 31 additions and 17 deletions.
48 changes: 31 additions & 17 deletions shim.c
Original file line number Diff line number Diff line change
Expand Up @@ -690,25 +690,12 @@ verify_buffer (char *data, int datasize,
}

static int
should_use_fallback(EFI_HANDLE image_handle)
is_removable_media_path(EFI_LOADED_IMAGE *li)
{
EFI_LOADED_IMAGE *li;
unsigned int pathlen = 0;
CHAR16 *bootpath = NULL;
EFI_FILE_IO_INTERFACE *fio = NULL;
EFI_FILE *vh = NULL;
EFI_FILE *fh = NULL;
EFI_STATUS efi_status;
int ret = 0;

efi_status = BS->HandleProtocol(image_handle, &EFI_LOADED_IMAGE_GUID,
(void **)&li);
if (EFI_ERROR(efi_status)) {
perror(L"Could not get image for boot" EFI_ARCH L".efi: %r\n",
efi_status);
return 0;
}

bootpath = DevicePathToStr(li->FilePath);

/* Check the beginning of the string and the end, to avoid
Expand All @@ -726,6 +713,36 @@ should_use_fallback(EFI_HANDLE image_handle)
if (pathlen < 5 || StrCaseCmp(bootpath + pathlen - 4, L".EFI"))
goto error;

ret = 1;

error:
if (bootpath)
FreePool(bootpath);

return ret;
}

static int
should_use_fallback(EFI_HANDLE image_handle)
{
EFI_LOADED_IMAGE *li;
EFI_FILE_IO_INTERFACE *fio = NULL;
EFI_FILE *vh = NULL;
EFI_FILE *fh = NULL;
EFI_STATUS efi_status;
int ret = 0;

efi_status = BS->HandleProtocol(image_handle, &EFI_LOADED_IMAGE_GUID,
(void **)&li);
if (EFI_ERROR(efi_status)) {
perror(L"Could not get image for boot" EFI_ARCH L".efi: %r\n",
efi_status);
return 0;
}

if (!is_removable_media_path(li))
goto error;

efi_status = BS->HandleProtocol(li->DeviceHandle, &FileSystemProtocol,
(void **) &fio);
if (EFI_ERROR(efi_status)) {
Expand Down Expand Up @@ -758,12 +775,9 @@ should_use_fallback(EFI_HANDLE image_handle)
fh->Close(fh);
if (vh)
vh->Close(vh);
if (bootpath)
FreePool(bootpath);

return ret;
}

/*
* Open the second stage bootloader and read it into a buffer
*/
Expand Down

0 comments on commit 9dca459

Please sign in to comment.