Skip to content

Commit

Permalink
Bootstrap: Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
vit9696 committed Apr 3, 2019
0 parents commit 0d6a4c1
Show file tree
Hide file tree
Showing 8 changed files with 474 additions and 0 deletions.
14 changes: 14 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
.DS_Store
UDK
Binaries
build
DerivedData
xcshareddata
xcuserdata
project.xcworkspace
*.dSYM
DICT
fuzz-*.log
crash-*
oom-*
slow-unit-*
183 changes: 183 additions & 0 deletions Application/Bootstrap/Bootstrap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
/** @file
Bootstrap OpenCore driver.
Copyright (c) 2018, vit9696. All rights reserved.<BR>
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 <Protocol/DevicePath.h>
#include <Protocol/LoadedImage.h>
#include <Protocol/OcBootstrap.h>

#include <Library/UefiLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/DebugLib.h>
#include <Library/DevicePathLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/OcDevicePathLib.h>
#include <Library/OcFileLib.h>
#include <Library/UefiBootServicesTableLib.h>
#include <Library/UefiRuntimeServicesTableLib.h>
#include <Library/UefiApplicationEntryPoint.h>

STATIC
EFI_STATUS
LoadOpenCore (
IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem,
IN EFI_HANDLE ParentImageHandle,
OUT EFI_HANDLE *ImageHandle
)
{
EFI_STATUS Status;
VOID *Buffer;
UINTN BufferSize;

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

BufferSize = 0;
Buffer = ReadFile (FileSystem, L"EFI\\OC\\OpenCore.efi", &BufferSize);
if (Buffer == NULL) {
DEBUG ((DEBUG_ERROR, "BS: Failed to locate valid OpenCore image - %p!\n", Buffer));
return EFI_NOT_FOUND;
}

DEBUG ((DEBUG_INFO, "BS: Read OpenCore image of %Lu bytes\n", (UINT64) BufferSize));

//
// Run OpenCore image
//
*ImageHandle = NULL;
Status = gBS->LoadImage (
FALSE,
ParentImageHandle,
NULL,
Buffer,
BufferSize,
ImageHandle
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "BS: Failed to load OpenCore image - %r\n", Status));
FreePool (Buffer);
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_ERROR, "BS: Failed to start OpenCore image - %r\n", Status));
gBS->UnloadImage (*ImageHandle);
}

FreePool (Buffer);

return Status;
}

STATIC
VOID
StartOpenCore (
IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem
)
{
EFI_STATUS Status;
OC_BOOTSTRAP_PROTOCOL *Bootstrap;

Bootstrap = NULL;
Status = gBS->LocateProtocol (
&gOcBootstrapProtocolGuid,
NULL,
(VOID **) &Bootstrap
);

if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "BS: Failed to locate bootstrap protocol - %r\n", Status));
return;
}

Bootstrap->ReRun (Bootstrap, FileSystem);
}

EFI_STATUS
EFIAPI
UefiMain (
IN EFI_HANDLE ImageHandle,
IN EFI_SYSTEM_TABLE *SystemTable
)
{
EFI_STATUS Status;
EFI_LOADED_IMAGE_PROTOCOL *LoadedImage;
EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem;
EFI_HANDLE OcImageHandle;


DEBUG ((DEBUG_INFO, "BS: Starting OpenCore...\n"));

//
// We have just started at EFI/BOOT/BOOTx64.efi.
// We need to run OpenCore on this partition as it failed automatically.
// The image is optionally located at EFI/OC/OpenCore.efi file.
//

LoadedImage = NULL;
Status = gBS->HandleProtocol (
ImageHandle,
&gEfiLoadedImageProtocolGuid,
(VOID **) &LoadedImage
);

if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "BS: Failed to locate loaded image - %r\n", Status));
return EFI_NOT_FOUND;
}

DebugPrintDevicePath (DEBUG_INFO, "BS: Booter path", LoadedImage->FilePath);

//
// Obtain the file system device path
//
FileSystem = LocateFileSystem (
LoadedImage->DeviceHandle,
LoadedImage->FilePath
);

if (FileSystem == NULL) {
DEBUG ((DEBUG_ERROR, "BS: Failed to obtain own file system\n"));
return EFI_NOT_FOUND;
}

//
// Try to start previously loaded OpenCore
//

DEBUG ((DEBUG_INFO, "BS: Trying to start loaded OpenCore image...\n"));
StartOpenCore (FileSystem);

DEBUG ((DEBUG_INFO, "BS: Trying to load OpenCore image...\n"));
Status = LoadOpenCore (FileSystem, ImageHandle, &OcImageHandle);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "BS: Failed to load OpenCore from disk - %r\n", Status));
return EFI_NOT_FOUND;
}

StartOpenCore (FileSystem);
DEBUG ((DEBUG_ERROR, "BS: Failed to start OpenCore image...\n"));

return EFI_NOT_FOUND;
}
57 changes: 57 additions & 0 deletions Application/Bootstrap/Bootstrap.inf
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
## @file
# OpenCore bootstrap image.
#
# Copyright (c) 2018, vit9696. All rights reserved.<BR>
#
# 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.
#
##

[Defines]
INF_VERSION = 0x00010005
BASE_NAME = BOOTx64
FILE_GUID = 1E55CC26-6036-43AB-867C-213FFB426812
MODULE_TYPE = UEFI_APPLICATION
VERSION_STRING = 1.0
ENTRY_POINT = UefiMain

#
# This flag specifies whether HII resource section is generated into PE image.
#
UEFI_HII_RESOURCE_SECTION = TRUE

#
# The following information is for reference only and not required by the build tools.
#
# VALID_ARCHITECTURES = IA32 X64 IPF EBC
#

[Sources]
Bootstrap.c

[Packages]
OpenCorePkg/OpenCorePkg.dec
OcSupportPkg/OcSupportPkg.dec
MdePkg/MdePkg.dec
MdeModulePkg/MdeModulePkg.dec

[Protocols]
gEfiDevicePathProtocolGuid ## CONSUMES
gEfiLoadedImageProtocolGuid ## CONSUMES
gEfiSimpleFileSystemProtocolGuid ## CONSUMES
gOcBootstrapProtocolGuid ## CONSUMES

[LibraryClasses]
UefiApplicationEntryPoint
UefiLib
PrintLib
DebugLib
DevicePathLib
MemoryAllocationLib
OcDevicePathLib
OcFileLib
56 changes: 56 additions & 0 deletions Include/Protocol/OcBootstrap.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/** @file
Copyright (C) 2018, 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.
**/

#ifndef OC_BOOTSTRAP_PROTOCOL_H
#define OC_BOOTSTRAP_PROTOCOL_H

#include <Protocol/SimpleFileSystem.h>

///
/// BA1EB455-B182-4F14-8521-E422C325DEF6
///
#define OC_BOOTSTRAP_PROTOCOL_GUID \
{ \
0xBA1EB455, 0xB182, 0x4F14, { 0x85, 0x21, 0xE4, 0x22, 0xC3, 0x25, 0xDE, 0xF6 } \
}

///
/// Forward declaration of OC_BOOTSTRAP_PROTOCOL structure.
///
typedef struct OC_BOOTSTRAP_PROTOCOL_ OC_BOOTSTRAP_PROTOCOL;

/**
Restart OpenCore at specified file system, does not return.
@param[in] This This protocol.
@param[in] FileSystem File system to bootstrap in.
**/
typedef
VOID
(EFIAPI *OC_BOOTSTRAP_RERUN) (
IN OC_BOOTSTRAP_PROTOCOL *This,
IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem
);

///
/// The structure exposed by the OC_BOOTSTRAP_PROTOCOL.
///
struct OC_BOOTSTRAP_PROTOCOL_ {
UINTN Revision;
OC_BOOTSTRAP_RERUN ReRun;
};

extern EFI_GUID gOcBootstrapProtocolGuid;

#endif // OC_BOOTSTRAP_PROTOCOL_H
16 changes: 16 additions & 0 deletions LICENSE.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
Copyright (c) 2016-2017, The HermitCrabs Lab
Copyright (c) 2016-2019, Download-Fritz
Copyright (c) 2017-2019, savvas
Copyright (c) 2016-2019, vit9696

All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 changes: 26 additions & 0 deletions OpenCorePkg.dec
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
## @file
# Copyright (C) 2018, vit9696. All rights reserved.<BR>
#
# Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
##

[Defines]
PACKAGE_NAME = OpenCorePkg
PACKAGE_GUID = 6B1D3AB4-5C85-462D-9DC5-480F8B17D5CB
PACKAGE_VERSION = 1.0
DEC_SPECIFICATION = 0x00010005

[Includes]
Include

[Protocols]
gOcBootstrapProtocolGuid = { 0xBA1EB455, 0xB182, 0x4F14, { 0x85, 0x21, 0xE4, 0x22, 0xC3, 0x25, 0xDE, 0xF6 }}
Loading

0 comments on commit 0d6a4c1

Please sign in to comment.