Skip to content

Commit

Permalink
OcMiscLib: Workaround firmware not specifying image handle at bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
vit9696 committed Feb 18, 2021
1 parent 583e563 commit b4449f4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 2 deletions.
7 changes: 7 additions & 0 deletions Application/OpenCore/OpenCore.c
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,13 @@ UefiMain (
return EFI_NOT_FOUND;
}

if (LoadedImage->DeviceHandle == NULL) {
DEBUG ((DEBUG_INFO, "OC: Missing boot device\n"));
//
// This is not critical as boot path may be complete.
//
}

if (LoadedImage->FilePath == NULL) {
DEBUG ((DEBUG_ERROR, "OC: Missing boot path\n"));
return EFI_INVALID_PARAMETER;
Expand Down
2 changes: 2 additions & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ OpenCore Changelog
- Improved CPU frequency calculation on Haswell and earlier
- Fixed issues when applying certain patches
- Added `SSN` (and `HW_SSN`) variable support
- Added onscreen early logging in DEBUG builds for legacy firmware
- Added workaround for firmware not specifying DeviceHandle at bootstrap

#### v0.6.6
- Added keyboard and pointer entry scroll support in OpenCanopy
Expand Down
51 changes: 49 additions & 2 deletions Library/OcMiscLib/ImageRunner.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,11 @@

#include <Uefi.h>
#include <Library/DebugLib.h>
#include <Library/DevicePathLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Protocol/LoadedImage.h>
#include <Protocol/SimpleFileSystem.h>

EFI_STATUS
OcLoadAndRunImage (
Expand All @@ -24,8 +28,9 @@ OcLoadAndRunImage (
OUT EFI_HANDLE *ImageHandle OPTIONAL
)
{
EFI_STATUS Status;
EFI_HANDLE NewHandle;
EFI_STATUS Status;
EFI_HANDLE NewHandle;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;

//
// Run OpenCore image
Expand All @@ -46,6 +51,48 @@ OcLoadAndRunImage (

DEBUG ((DEBUG_INFO, "OCM: Loaded image at %p handle\n", NewHandle));

Status = gBS->HandleProtocol (
NewHandle,
&gEfiLoadedImageProtocolGuid,
(VOID **) &LoadedImage
);
if (!EFI_ERROR (Status)) {
DEBUG ((
DEBUG_INFO,
"OCM: Loaded image has DeviceHandle %p FilePath %p ours DevicePath %p\n",
LoadedImage->DeviceHandle,
LoadedImage->FilePath,
DevicePath
));

//
// Some fragile firmware fail to properly set LoadedImage when buffer is provided.
// REF: https://github.com/acidanthera/bugtracker/issues/712
// REF: https://github.com/acidanthera/bugtracker/issues/1502
//
if (DevicePath != NULL && LoadedImage->DeviceHandle == NULL) {
Status = gBS->LocateDevicePath (
&gEfiSimpleFileSystemProtocolGuid,
&DevicePath,
&LoadedImage->DeviceHandle
);

DEBUG ((
DEBUG_INFO,
"OCM: LocateDevicePath on loaded handle %p - %r\n",
LoadedImage->DeviceHandle,
Status
));

if (!EFI_ERROR (Status)) {
if (LoadedImage->FilePath != NULL) {
FreePool (LoadedImage->FilePath);
}
LoadedImage->FilePath = DuplicateDevicePath (DevicePath);
}
}
}

Status = gBS->StartImage (
NewHandle,
NULL,
Expand Down
1 change: 1 addition & 0 deletions Library/OcMiscLib/OcMiscLib.inf
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
gEfiPciIoProtocolGuid
gEfiShellParametersProtocolGuid
gEfiLoadedImageProtocolGuid
gEfiSimpleFileSystemProtocolGuid

[LibraryClasses]
BaseLib
Expand Down

0 comments on commit b4449f4

Please sign in to comment.