Skip to content

Commit

Permalink
OcMiscLib: Add OcLoadAndRunImage and use it in Bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
vit9696 committed Jan 17, 2021
1 parent 2b7ed55 commit 9a11244
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 26 deletions.
29 changes: 3 additions & 26 deletions Application/Bootstrap/Bootstrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ EFI_STATUS
LoadOpenCore (
IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem,
IN EFI_DEVICE_PATH_PROTOCOL *LoaderDevicePath,
IN EFI_HANDLE ParentImageHandle,
OUT EFI_HANDLE *ImageHandle,
OUT EFI_DEVICE_PATH_PROTOCOL **ImagePath
)
Expand All @@ -50,7 +49,6 @@ LoadOpenCore (
UINT32 BufferSize;

ASSERT (FileSystem != NULL);
ASSERT (ParentImageHandle != NULL);
ASSERT (ImageHandle != NULL);
ASSERT (ImagePath != NULL);

Expand Down Expand Up @@ -122,41 +120,20 @@ LoadOpenCore (
// Run OpenCore image
//
*ImageHandle = NULL;
Status = gBS->LoadImage (
FALSE,
ParentImageHandle,
Status = OcLoadAndRunImage (
NULL,
Buffer,
BufferSize,
ImageHandle
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "BS: Failed to load OpenCore image - %r\n", Status));
DEBUG ((DEBUG_ERROR, "BS: Failed to start OpenCore image - %r\n", Status));
FreePool (Buffer);
if (*ImagePath != NULL) {
FreePool (*ImagePath);
}
return Status;
}

DEBUG ((DEBUG_INFO, "BS: Loaded OpenCore image at %p handle\n", *ImageHandle));

Status = gBS->StartImage (
*ImageHandle,
NULL,
NULL
);

if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "BS: Failed to start OpenCore image - %r\n", Status));
gBS->UnloadImage (*ImageHandle);
if (*ImagePath != NULL) {
FreePool (*ImagePath);
}
}

FreePool (Buffer);

return Status;
}

Expand Down Expand Up @@ -267,7 +244,7 @@ UefiMain (
}

DEBUG ((DEBUG_INFO, "BS: Trying to load OpenCore image...\n"));
Status = LoadOpenCore (FileSystem, LoadedImage->FilePath, ImageHandle, &OcImageHandle, &OcImagePath);
Status = LoadOpenCore (FileSystem, LoadedImage->FilePath, &OcImageHandle, &OcImagePath);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "BS: Failed to load OpenCore from disk - %r\n", Status));
return EFI_NOT_FOUND;
Expand Down
16 changes: 16 additions & 0 deletions Include/Acidanthera/Library/OcMiscLib.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,22 @@ OcHandleProtocolFallback (
OUT VOID **Interface
);

/**
Run and execute image file from buffer.
@param[in] DevicePath Image device path, optional.
@param[in] Buffer Buffer with image data, optional when DP is given.
@param[in] BufferSize Image data size in the buffer.
@param[out] ImageHandle Loaded image handle for drivers, optional.
**/
EFI_STATUS
OcLoadAndRunImage (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL,
IN VOID *Buffer OPTIONAL,
IN UINTN BufferSize,
OUT EFI_HANDLE *ImageHandle OPTIONAL
);

/**
Release UEFI ownership from USB controllers at booting.
**/
Expand Down
63 changes: 63 additions & 0 deletions Library/OcMiscLib/ImageRunner.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/** @file
Copyright (C) 2021, vit9696. All rights reserved.
All rights reserved.
This program and the accompanying materials
are licensed and made available under the terms and conditions of the BSD License
which accompanies this distribution. The full text of the license may be found at
http://opensource.org/licenses/bsd-license.php
THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
**/

#include <Uefi.h>
#include <Library/DebugLib.h>
#include <Library/UefiBootServicesTableLib.h>

EFI_STATUS
OcLoadAndRunImage (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath OPTIONAL,
IN VOID *Buffer OPTIONAL,
IN UINTN BufferSize,
OUT EFI_HANDLE *ImageHandle OPTIONAL
)
{
EFI_STATUS Status;
EFI_HANDLE NewHandle;

//
// Run OpenCore image
//
NewHandle = NULL;
Status = gBS->LoadImage (
FALSE,
gImageHandle,
DevicePath,
Buffer,
BufferSize,
&NewHandle
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "OCM: Failed to load image - %r\n", Status));
return Status;
}

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

Status = gBS->StartImage (
NewHandle,
NULL,
NULL
);

if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "OCM: Failed to start image - %r\n", Status));
gBS->UnloadImage (NewHandle);
} else if (ImageHandle != NULL) {
*ImageHandle = NewHandle;
}

return Status;
}
1 change: 1 addition & 0 deletions Library/OcMiscLib/OcMiscLib.inf
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
[Sources]
DataPatcher.c
DirectReset.c
ImageRunner.c
ReleaseUsbOwnership.c
ProtocolSupport.c
Math.c

0 comments on commit 9a11244

Please sign in to comment.