From f14ef700de0304947d8e60e14fca7d13be376e01 Mon Sep 17 00:00:00 2001 From: Vladislav Yaroshchuk Date: Wed, 30 Jun 2021 18:04:16 +0300 Subject: [PATCH] Rebase onto edk2-stable202105: restore AppleSupportLib Signed-off-by: Vladislav Yaroshchuk --- DarwinComponents.dsc.inc | 5 + DarwinComponents.fdf.inc | 5 + DarwinLibs.dsc.inc | 8 + Include/Guid/Darwin/AppleDataHub.h | 28 +++ Include/Guid/Darwin/AppleVariable.h | 21 ++ Include/Library/Darwin/AppleSupportLib.h | 29 +++ Include/Protocol/Darwin/AppleBootPath.h | 49 +++++ Include/Protocol/Darwin/ConsoleControl.h | 122 +++++++++++ Library/AppleSupportLib/AppleSupport.c | 115 +++++++++++ Library/AppleSupportLib/AppleSupportLib.inf | 62 ++++++ Library/AppleSupportLib/Boot.c | 194 ++++++++++++++++++ Library/AppleSupportLib/Boot.h | 45 ++++ Library/AppleSupportLib/Common.h | 30 +++ Library/AppleSupportLib/Console.c | 89 ++++++++ Library/AppleSupportLib/Console.h | 28 +++ Library/AppleSupportLib/Datahub.c | 100 +++++++++ Library/AppleSupportLib/Datahub.h | 33 +++ Library/AppleSupportLib/SmBios.c | 115 +++++++++++ Library/AppleSupportLib/SmBios.h | 31 +++ Library/PlatformBootManagerLib/BdsPlatform.c | 6 + .../PlatformBootManagerLib.inf | 1 + OvmfDarwinPkg.dec | 12 ++ OvmfDarwinPkgX64.dsc | 2 + 23 files changed, 1130 insertions(+) create mode 100644 DarwinLibs.dsc.inc create mode 100644 Include/Guid/Darwin/AppleDataHub.h create mode 100644 Include/Guid/Darwin/AppleVariable.h create mode 100644 Include/Library/Darwin/AppleSupportLib.h create mode 100644 Include/Protocol/Darwin/AppleBootPath.h create mode 100644 Include/Protocol/Darwin/ConsoleControl.h create mode 100644 Library/AppleSupportLib/AppleSupport.c create mode 100644 Library/AppleSupportLib/AppleSupportLib.inf create mode 100644 Library/AppleSupportLib/Boot.c create mode 100644 Library/AppleSupportLib/Boot.h create mode 100644 Library/AppleSupportLib/Common.h create mode 100644 Library/AppleSupportLib/Console.c create mode 100644 Library/AppleSupportLib/Console.h create mode 100644 Library/AppleSupportLib/Datahub.c create mode 100644 Library/AppleSupportLib/Datahub.h create mode 100644 Library/AppleSupportLib/SmBios.c create mode 100644 Library/AppleSupportLib/SmBios.h diff --git a/DarwinComponents.dsc.inc b/DarwinComponents.dsc.inc index 51b1992..55cd01d 100644 --- a/DarwinComponents.dsc.inc +++ b/DarwinComponents.dsc.inc @@ -13,3 +13,8 @@ OvmfDarwinPkg/OpenHfsPlus/OpenHfsPlus.inf # APFS Support # OvmfDarwinPkg/ApfsDriverLoader/ApfsDriverLoader.inf + +# +# AppleSupportLib requirement +# +IntelFrameworkModulePkg/Universal/DataHubDxe/DataHubDxe.inf \ No newline at end of file diff --git a/DarwinComponents.fdf.inc b/DarwinComponents.fdf.inc index cb49c66..5c9a895 100644 --- a/DarwinComponents.fdf.inc +++ b/DarwinComponents.fdf.inc @@ -13,3 +13,8 @@ INF OvmfDarwinPkg/OpenHfsPlus/OpenHfsPlus.inf # APFS Support # INF OvmfDarwinPkg/ApfsDriverLoader/ApfsDriverLoader.inf + +# +# AppleSupportLib requirement +# +INF IntelFrameworkModulePkg/Universal/DataHubDxe/DataHubDxe.inf diff --git a/DarwinLibs.dsc.inc b/DarwinLibs.dsc.inc new file mode 100644 index 0000000..2919394 --- /dev/null +++ b/DarwinLibs.dsc.inc @@ -0,0 +1,8 @@ +# +# Copyright (c) 2021, Vladislav Yaroshchuk . All rights reserved. + + SPDX-License-Identifier: BSD-2-Clause-Patent +**/ + +#ifndef _APPLE_VARIABLE_GUID_H_ +#define _APPLE_VARIABLE_GUID_H_ + +#define APPLE_NVRAM_VARIABLE_GUID \ + {0x7C436110, 0xAB2A, 0x4BBB, {0xA8, 0x80, 0xFE, 0x41, 0x99, 0x5C, 0x9F, 0x82}} + +extern EFI_GUID gAppleNVRAMVariableGuid; + +#define APPLE_FIRMWARE_VARIABLE_GUID \ + {0x4D1EDE05, 0x38C7, 0x4A6A, {0x9C, 0xC6, 0x4B, 0xCC, 0xA8, 0xB3, 0x8C, 0x14}} + +extern EFI_GUID gAppleFirmwareVariableGuid; + + +#endif //_APPLE_VARIABLE_GUID_H_ diff --git a/Include/Library/Darwin/AppleSupportLib.h b/Include/Library/Darwin/AppleSupportLib.h new file mode 100644 index 0000000..2678a48 --- /dev/null +++ b/Include/Library/Darwin/AppleSupportLib.h @@ -0,0 +1,29 @@ +/** @file +* +* Copyright (c) 2020, Vladislav Yaroshchuk +* Copyright (c) 2014, Reza Jelveh. 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 _APPLESUPPORT_LIB_INCLUDED_ +#define _APPLESUPPORT_LIB_INCLUDED_ + + +#include + +EFI_STATUS +EFIAPI +InitializeAppleSupport ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ); + +#endif diff --git a/Include/Protocol/Darwin/AppleBootPath.h b/Include/Protocol/Darwin/AppleBootPath.h new file mode 100644 index 0000000..8392a65 --- /dev/null +++ b/Include/Protocol/Darwin/AppleBootPath.h @@ -0,0 +1,49 @@ +/** @file +* +* Copyright (c) 2021 Vladislav Yaroshchuk. 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. +* +* Module Name: +* +* AppleBootPath.h +* +* Abstract: +* +* Abstraction of Apple's boot.efi bootloader path retrieval +**/ + +#ifndef _APPLE_BOOT_PATH_PROTOCOL_H_ +#define _APPLE_BOOT_PATH_PROTOCOL_H_ + +#define EFI_APPLE_BOOT_PATH_PROTOCOL_GUID \ + {0x85e1deb6, 0xd97a, 0x4bea, {0xa0, 0xa0, 0xe6, 0xa9, 0x43, 0x32, 0x85, 0x09}} + + +typedef struct _EFI_APPLE_BOOT_PATH_PROTOCOL EFI_APPLE_BOOT_PATH_PROTOCOL; + +/** + * Attempts to find Apple's boot.efi bootloader expanding media device path. + * Returns expanded device path if bootloader found, otherwise NULL + */ +typedef +EFI_DEVICE_PATH_PROTOCOL * +(EFIAPI *EFI_APPLE_BOOT_PATH_GET_BOOT_LOADER_PATH) ( + IN EFI_APPLE_BOOT_PATH_PROTOCOL *This, + IN EFI_HANDLE Device + ); + + +struct _EFI_APPLE_BOOT_PATH_PROTOCOL { + EFI_APPLE_BOOT_PATH_GET_BOOT_LOADER_PATH GetBootLoaderPath; +}; + +extern EFI_GUID gEfiAppleBootPathProtocolGuid; + +#endif //_APPLE_BOOT_PATH_PROTOCOL_H_ diff --git a/Include/Protocol/Darwin/ConsoleControl.h b/Include/Protocol/Darwin/ConsoleControl.h new file mode 100644 index 0000000..14ab566 --- /dev/null +++ b/Include/Protocol/Darwin/ConsoleControl.h @@ -0,0 +1,122 @@ +/*++ + +Copyright (c) 2004 - 2010, Intel Corporation. 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. + +Module Name: + + ConsoleControl.h + +Abstract: + + Abstraction of a Text mode or GOP/UGA screen + +--*/ + +#ifndef __CONSOLE_CONTROL_H__ +#define __CONSOLE_CONTROL_H__ + +#define EFI_CONSOLE_CONTROL_PROTOCOL_GUID \ + { 0xf42f7782, 0x12e, 0x4c12, {0x99, 0x56, 0x49, 0xf9, 0x43, 0x4, 0xf7, 0x21} } + +typedef struct _EFI_CONSOLE_CONTROL_PROTOCOL EFI_CONSOLE_CONTROL_PROTOCOL; + + +typedef enum { + EfiConsoleControlScreenText, + EfiConsoleControlScreenGraphics, + EfiConsoleControlScreenMaxValue +} EFI_CONSOLE_CONTROL_SCREEN_MODE; + + +typedef +EFI_STATUS +(EFIAPI *EFI_CONSOLE_CONTROL_PROTOCOL_GET_MODE) ( + IN EFI_CONSOLE_CONTROL_PROTOCOL *This, + OUT EFI_CONSOLE_CONTROL_SCREEN_MODE *Mode, + OUT BOOLEAN *GopUgaExists, OPTIONAL + OUT BOOLEAN *StdInLocked OPTIONAL + ) +/*++ + + Routine Description: + Return the current video mode information. Also returns info about existence + of Graphics Output devices or UGA Draw devices in system, and if the Std In + device is locked. All the arguments are optional and only returned if a non + NULL pointer is passed in. + + Arguments: + This - Protocol instance pointer. + Mode - Are we in text of grahics mode. + GopUgaExists - TRUE if Console Spliter has found a GOP or UGA device + StdInLocked - TRUE if StdIn device is keyboard locked + + Returns: + EFI_SUCCESS - Mode information returned. + +--*/ +; + + +typedef +EFI_STATUS +(EFIAPI *EFI_CONSOLE_CONTROL_PROTOCOL_SET_MODE) ( + IN EFI_CONSOLE_CONTROL_PROTOCOL *This, + IN EFI_CONSOLE_CONTROL_SCREEN_MODE Mode + ) +/*++ + + Routine Description: + Set the current mode to either text or graphics. Graphics is + for Quiet Boot. + + Arguments: + This - Protocol instance pointer. + Mode - Mode to set the + + Returns: + EFI_SUCCESS - Mode information returned. + +--*/ +; + + +typedef +EFI_STATUS +(EFIAPI *EFI_CONSOLE_CONTROL_PROTOCOL_LOCK_STD_IN) ( + IN EFI_CONSOLE_CONTROL_PROTOCOL *This, + IN CHAR16 *Password + ) +/*++ + + Routine Description: + Lock Std In devices until Password is typed. + + Arguments: + This - Protocol instance pointer. + Password - Password needed to unlock screen. NULL means unlock keyboard + + Returns: + EFI_SUCCESS - Mode information returned. + EFI_DEVICE_ERROR - Std In not locked + +--*/ +; + + + +struct _EFI_CONSOLE_CONTROL_PROTOCOL { + EFI_CONSOLE_CONTROL_PROTOCOL_GET_MODE GetMode; + EFI_CONSOLE_CONTROL_PROTOCOL_SET_MODE SetMode; + EFI_CONSOLE_CONTROL_PROTOCOL_LOCK_STD_IN LockStdIn; +}; + +extern EFI_GUID gEfiConsoleControlProtocolGuid; + +#endif diff --git a/Library/AppleSupportLib/AppleSupport.c b/Library/AppleSupportLib/AppleSupport.c new file mode 100644 index 0000000..d52fceb --- /dev/null +++ b/Library/AppleSupportLib/AppleSupport.c @@ -0,0 +1,115 @@ +/** @file +* +* Copyright (c) 2020, Vladislav Yaroshchuk +* Copyright (c) 2014, Reza Jelveh. 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 "Console.h" +#include "Datahub.h" +#include "Boot.h" +#include "SmBios.h" + +/** + Register Handler for the specified interrupt source. + + @param This Instance pointer for this protocol + @param Source Hardware source of the interrupt + @param Handler Callback for interrupt. NULL to unregister + + @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as + defined by the Attributes. + @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the + DataSize exceeds the maximum allowed. + @retval EFI_INVALID_PARAMETER VariableName is an empty Unicode string. + @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data. + @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure. + @retval EFI_WRITE_PROTECTED The variable in question is read-only. + @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted. + @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS + set but the AuthInfo does NOT pass the validation check carried + out by the firmware. + +**/ +EFI_STATUS +InitializeFirmware () +{ + EFI_STATUS Status; + + UINT32 BackgroundClear = 0x00000000; + UINT32 FwFeatures = 0x80000015; + UINT32 FwFeaturesMask = 0x800003ff; + CHAR8 BootArgs[] = "-v debug=0x14e keepsyms=1"; + + Status = gRT->SetVariable(L"BackgroundClear", + &gAppleFirmwareVariableGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, + sizeof(BackgroundClear), &BackgroundClear); + ASSERT_EFI_ERROR (Status); + + Status = gRT->SetVariable(L"FirmwareFeatures", + &gAppleFirmwareVariableGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, + sizeof(FwFeatures), &FwFeatures); + ASSERT_EFI_ERROR (Status); + + Status = gRT->SetVariable(L"FirmwareFeaturesMask", + &gAppleFirmwareVariableGuid, + EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, + sizeof(FwFeaturesMask), &FwFeaturesMask); + ASSERT_EFI_ERROR (Status); + + DEBUG_CODE_BEGIN (); + Status = gRT->SetVariable(L"boot-args", + &gAppleNVRAMVariableGuid, + EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS, + sizeof(BootArgs), &BootArgs); + ASSERT_EFI_ERROR (Status); + DEBUG_CODE_END (); + + return EFI_SUCCESS; +} + +/** + Register driver. + + @param ImageHandle of the loaded driver + @param SystemTable Pointer to the System Table + + @retval EFI_SUCCESS Driver registered + +**/ +EFI_STATUS +EFIAPI +InitializeAppleSupport ( + IN EFI_HANDLE ImageHandle, + IN EFI_SYSTEM_TABLE *SystemTable + ) +{ + EFI_STATUS Status; + + Status = InitializeConsoleControl(ImageHandle); + ASSERT_EFI_ERROR (Status); + + Status = InitializeDatahub(ImageHandle); + ASSERT_EFI_ERROR(Status); + + Status = InitializeFirmware(); + ASSERT_EFI_ERROR(Status); + + Status = InitializeAppleBoot(ImageHandle); + ASSERT_EFI_ERROR(Status); + + Status = InitializeSmBios (); + ASSERT_EFI_ERROR (Status); + + return Status; +} diff --git a/Library/AppleSupportLib/AppleSupportLib.inf b/Library/AppleSupportLib/AppleSupportLib.inf new file mode 100644 index 0000000..dd73af3 --- /dev/null +++ b/Library/AppleSupportLib/AppleSupportLib.inf @@ -0,0 +1,62 @@ +#/* @file +# +# Copyright (c) 2020, Vladislav Yaroshchuk +# Copyright (c) 2014, Reza Jelveh. 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. +# +#*/ + +[Defines] + INF_VERSION = 0x00010005 + BASE_NAME = AppleSupportLib + FILE_GUID = CEC880AF-68DF-4CDF-BBA5-FF9B202382AB + MODULE_TYPE = BASE + VERSION_STRING = 1.0 + LIBRARY_CLASS = AppleSupportLib + +# +# The following information is for reference only and not required by the build tools. +# +# VALID_ARCHITECTURES = IA32 X64 +# + +[Sources] + AppleSupport.c + Console.c + Datahub.c + Boot.c + SmBios.c + +[Packages] + MdePkg/MdePkg.dec + OvmfDarwinPkg/OvmfDarwinPkg.dec + IntelFrameworkPkg/IntelFrameworkPkg.dec + IntelFrameworkModulePkg/IntelFrameworkModulePkg.dec + +[LibraryClasses] + UefiBootServicesTableLib + BaseLib + DebugLib + MemoryAllocationLib + BaseMemoryLib + +[Protocols] + gEfiConsoleControlProtocolGuid ## SOMETIMES_PRODUCES + gEfiDataHubProtocolGuid ## SOMETIMES_PRODUCES + gEfiAppleBootPathProtocolGuid ## SOMETIMES_PRODUCES + +[Guids] + gAppleFirmwareVariableGuid ## SOMETIMES_CONSUMES + gAppleNVRAMVariableGuid ## SOMETIMES_CONSUMES + gAppleBlessedSystemFileInfoGuid ## SOMETIMES_CONSUMES + gEfiSmbiosTableGuid ## SOMETIMES_CONSUMES + gAppleSystemInfoProducerNameGuid ## SOMETIMES_CONSUMES + gAppleFsbFrequencyPropertyGuid ## SOMETIMES_CONSUMES + gAppleDevicePathsSupportedGuid ## SOMETIMES_CONSUMES diff --git a/Library/AppleSupportLib/Boot.c b/Library/AppleSupportLib/Boot.c new file mode 100644 index 0000000..c268b4b --- /dev/null +++ b/Library/AppleSupportLib/Boot.c @@ -0,0 +1,194 @@ +/** @file +* +* Copyright (C) 2021, Vladislav Yaroshchuk +* +* 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 "Boot.h" + +/** + * Try to find Blessed file on Device and return its path into BlessedFilePath + * @param Device Device to lookup + * @param BlessedFilePath Found blessed file path + * @return EFI_SUCCESS if blessed file found, error code on fail + */ +STATIC +EFI_STATUS +GetBlessedFilePath ( + IN EFI_HANDLE Device, + OUT EFI_DEVICE_PATH_PROTOCOL **BlessedFilePath + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *SimpleFsProtocol; + EFI_FILE_PROTOCOL *Root; + UINTN Size; + + Status = gBS->HandleProtocol (Device, &gEfiSimpleFileSystemProtocolGuid, (VOID **)&SimpleFsProtocol); + if (EFI_ERROR (Status)) { + return Status; + } + + Status = SimpleFsProtocol->OpenVolume (SimpleFsProtocol, &Root); + if (EFI_ERROR (Status)) { + return Status; + } + + Size = 0; + Status = Root->GetInfo (Root, &gAppleBlessedSystemFileInfoGuid, &Size, NULL); + if (Status != EFI_BUFFER_TOO_SMALL && EFI_ERROR (Status)) { + return Status; + } + + *BlessedFilePath = AllocatePool (Size); + if (BlessedFilePath == NULL) { + } + + Status = Root->GetInfo (Root, &gAppleBlessedSystemFileInfoGuid, &Size, *BlessedFilePath); + if (EFI_ERROR (Status)) { + return Status; + } + + if (!IsDevicePathValid (*BlessedFilePath, Size)) { + return EFI_NOT_FOUND; + } + + return EFI_SUCCESS; +} + +/** + * Check if given path exists + * @param Device Device to lookup + * @param Path Path in format '\Dir0\Dir1' to be checked + * @return TRUE if exists, otherwise FALSE + */ +STATIC +BOOLEAN +IsPathExists( + IN EFI_HANDLE Device, + IN CHAR16 *Path + ) +{ + EFI_STATUS Status; + EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem; + EFI_FILE_PROTOCOL *Root; + EFI_FILE_PROTOCOL *Target; + BOOLEAN Result; + + Result = FALSE; + + Status = gBS->HandleProtocol (Device, &gEfiSimpleFileSystemProtocolGuid, (VOID **)&FileSystem); + + if (EFI_ERROR (Status)) { + goto done; + } + + Status = FileSystem->OpenVolume (FileSystem, &Root); + if (EFI_ERROR (Status)) { + goto done; + } + + Status = Root->Open (Root, &Target, Path, EFI_FILE_MODE_READ, 0); + if (EFI_ERROR (Status)) { + goto close_root; + } + + Result = TRUE; + Target->Close(Target); + +close_root: + Root->Close (Root); + +done: + return Result; +} + +/** + * Get boot.efi bootloader path from predefined APPLE_BOOTLOADER_DEFAULT_PATH + * @param Device Device to lookup + * @param BootloaderFilePath Found predefined file path + * @return EFI_SUCCESS if APPLE_BOOTLOADER_DEFAULT_PATH file exists, error code on fail + */ +STATIC +EFI_STATUS +GetPredefinedFilePath( + IN EFI_HANDLE Device, + OUT EFI_DEVICE_PATH_PROTOCOL **BootloaderFilePath + ) +{ + STATIC CHAR16 *DefaultPath = APPLE_BOOTLOADER_DEFAULT_PATH; + + if (!IsPathExists(Device, DefaultPath)) { + return EFI_NOT_FOUND; + } + + *BootloaderFilePath = FileDevicePath (Device, DefaultPath); + return EFI_SUCCESS; +} + +/** + * Attempts to find Apple's boot.efi bootloader expanding media device path. + * Returns expanded device path if bootloader found, otherwise NULL + * + * We try to expand device path into three steps: + * 1. Request blessed file (bootloader) from device handle. Use it if found. + * 2. If blessed file not found, lookup predefined path, check for existence and use. + * 3. If noting found or an error occurred, return NULL + */ +STATIC +EFI_DEVICE_PATH_PROTOCOL * +EFIAPI +GetBootLoaderPathImpl ( + IN EFI_APPLE_BOOT_PATH_PROTOCOL *This, + IN EFI_HANDLE Device + ) +{ + EFI_STATUS Status; + EFI_DEVICE_PATH_PROTOCOL *DevicePath; + + DevicePath = NULL; + // Blessed file check + Status = GetBlessedFilePath (Device, &DevicePath); + if (!EFI_ERROR (Status)) { + return DevicePath; + } + + // If blessed file not found attempt to find bootloader from default path + Status = GetPredefinedFilePath(Device, &DevicePath); + if (!EFI_ERROR (Status)) { + return DevicePath; + } + return NULL; +} + +STATIC EFI_APPLE_BOOT_PATH_PROTOCOL mAppleBootPathProtocolImpl = { + GetBootLoaderPathImpl, +}; + +/** + * Installs AppleBootPathProtocol to be used by UefiBootManagerLib BDS phase + * @param ImageHandle Image handle + * @return EFI_SUCCESS on success, otherwise InstallMultipleProtocolInterfaces error code + */ +EFI_STATUS +InitializeAppleBoot ( + IN EFI_HANDLE ImageHandle + ) +{ + EFI_STATUS Status; + Status = gBS->InstallMultipleProtocolInterfaces ( + &ImageHandle, + &gEfiAppleBootPathProtocolGuid, + &mAppleBootPathProtocolImpl, + NULL + ); + return Status; +} \ No newline at end of file diff --git a/Library/AppleSupportLib/Boot.h b/Library/AppleSupportLib/Boot.h new file mode 100644 index 0000000..d730bbc --- /dev/null +++ b/Library/AppleSupportLib/Boot.h @@ -0,0 +1,45 @@ +/** @file +* +* Copyright (c) 2021, Vladislav Yaroshchuk. 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 _APPLESUPPORT_BOOT_H_INCLUDED_ +#define _APPLESUPPORT_BOOT_H_INCLUDED_ + +#include "Common.h" + +#include + +#include + +#include +#include + +#include +#include + +#ifndef APPLE_BOOTLOADER_DEFAULT_PATH + #define APPLE_BOOTLOADER_DEFAULT_PATH L"System\\Library\\CoreServices\\boot.efi" +#endif + +/** + * Installs AppleBootPathProtocol to be used by UefiBootManagerLib BDS phase + * @param ImageHandle Image handle + * @return EFI_SUCCESS on success, otherwise InstallMultipleProtocolInterfaces error code + */ +EFI_STATUS +InitializeAppleBoot ( + IN EFI_HANDLE ImageHandle + ); + +#endif diff --git a/Library/AppleSupportLib/Common.h b/Library/AppleSupportLib/Common.h new file mode 100644 index 0000000..605d5a8 --- /dev/null +++ b/Library/AppleSupportLib/Common.h @@ -0,0 +1,30 @@ +/** @file +* +* Copyright (c) 2014, Reza Jelveh. 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 _APPLESUPPORT_COMMON_H_INCLUDED_ +#define _APPLESUPPORT_COMMON_H_INCLUDED_ + +#include + +#include +#include +#include +#include + +#include +#include +#include + +#endif diff --git a/Library/AppleSupportLib/Console.c b/Library/AppleSupportLib/Console.c new file mode 100644 index 0000000..2dbb65f --- /dev/null +++ b/Library/AppleSupportLib/Console.c @@ -0,0 +1,89 @@ +/** @file +* +* Copyright (c) 2014, Reza Jelveh. 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 "Console.h" + +EFI_STATUS +EFIAPI +GetModeImpl ( + IN EFI_CONSOLE_CONTROL_PROTOCOL *This, + OUT EFI_CONSOLE_CONTROL_SCREEN_MODE *Mode, + OUT BOOLEAN *GopUgaExists, OPTIONAL + OUT BOOLEAN *StdInLocked OPTIONAL + ) +{ + *Mode = EfiConsoleControlScreenGraphics; + + if (GopUgaExists) + *GopUgaExists = TRUE; + if (StdInLocked) + *StdInLocked = FALSE; + return EFI_SUCCESS; +} + +EFI_STATUS +EFIAPI +SetModeImpl ( + IN EFI_CONSOLE_CONTROL_PROTOCOL *This, + IN EFI_CONSOLE_CONTROL_SCREEN_MODE Mode + ) +{ + return EFI_SUCCESS; +} + +EFI_STATUS +EFIAPI +LockStdInImpl ( + IN EFI_CONSOLE_CONTROL_PROTOCOL *This, + IN CHAR16 *Password + ) +{ + return EFI_SUCCESS; +} + + + +EFI_CONSOLE_CONTROL_PROTOCOL gConsoleController = +{ + GetModeImpl, + SetModeImpl, +}; + +/** + Install ConsoleControl protocol, which is needed for Apple's + boot.efi + + @param ImageHandle of the loaded driver + + @retval EFI_SUCCESS Successfully installed protocol handler + @retval EFI_DEVICE_ERROR ConsoleProtocol could not be installed + +**/ +EFI_STATUS +EFIAPI +InitializeConsoleControl ( + IN EFI_HANDLE ImageHandle + ) +{ + EFI_STATUS Status; + + Status = gBS->InstallMultipleProtocolInterfaces ( + &ImageHandle, + &gEfiConsoleControlProtocolGuid, + &gConsoleController, + NULL + ); + + return Status; +} diff --git a/Library/AppleSupportLib/Console.h b/Library/AppleSupportLib/Console.h new file mode 100644 index 0000000..fa20995 --- /dev/null +++ b/Library/AppleSupportLib/Console.h @@ -0,0 +1,28 @@ +/** @file +* +* Copyright (c) 2014, Reza Jelveh. 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 _APPLESUPPORT_CONSOLE_H_INCLUDED_ +#define _APPLESUPPORT_CONSOLE_H_INCLUDED_ + +#include "Common.h" + +#include + +EFI_STATUS +EFIAPI +InitializeConsoleControl ( + IN EFI_HANDLE ImageHandle + ); + +#endif diff --git a/Library/AppleSupportLib/Datahub.c b/Library/AppleSupportLib/Datahub.c new file mode 100644 index 0000000..295c0c4 --- /dev/null +++ b/Library/AppleSupportLib/Datahub.c @@ -0,0 +1,100 @@ +/** @file +* +* Copyright (c) 2011, Reza Jelveh. 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 "Datahub.h" + +typedef struct { + UINT32 DataNameSize; + UINT32 DataSize; +} EFI_PROPERTY_SUBCLASS_RECORD; + +typedef struct { + EFI_SUBCLASS_TYPE1_HEADER Header; + EFI_PROPERTY_SUBCLASS_RECORD Record; +} EFI_PROPERTY_SUBCLASS_DATA; + + +EFI_STATUS +SetEfiPlatformProperty ( + IN EFI_DATA_HUB_PROTOCOL *DataHub, + IN CONST EFI_STRING Name, + EFI_GUID EfiPropertyGuid, + VOID *Data, + UINT32 DataSize + ) +{ + EFI_STATUS Status; + UINT32 DataNameSize; + EFI_PROPERTY_SUBCLASS_DATA *DataRecord; + + DataNameSize = (UINT32)StrSize(Name); + + DataRecord = AllocateZeroPool (sizeof (EFI_PROPERTY_SUBCLASS_DATA) + DataNameSize + DataSize); + ASSERT (DataRecord != NULL); + + DataRecord->Header.Version = EFI_DATA_RECORD_HEADER_VERSION; + DataRecord->Header.HeaderSize = sizeof(EFI_SUBCLASS_TYPE1_HEADER); + DataRecord->Header.Instance = 0xFFFF; + DataRecord->Header.SubInstance = 0xFFFF; + DataRecord->Header.RecordType = 0xFFFFFFFF; + DataRecord->Record.DataNameSize = DataNameSize; + DataRecord->Record.DataSize = DataSize; + + + CopyMem((UINT8 *)DataRecord + sizeof(EFI_PROPERTY_SUBCLASS_DATA), Name, DataNameSize); + CopyMem((UINT8 *)DataRecord + sizeof(EFI_PROPERTY_SUBCLASS_DATA) + DataNameSize, Data, DataSize); + + Status = DataHub->LogData(DataHub, &EfiPropertyGuid, &gAppleSystemInfoProducerNameGuid, + EFI_DATA_RECORD_CLASS_DATA, + DataRecord, + sizeof(EFI_PROPERTY_SUBCLASS_DATA) + DataNameSize + DataSize); + + if (DataRecord) { + gBS->FreePool(DataRecord); + } + + return Status; +} + +/** + Initialize the DataHub protocols data for the xnu kernel to + detect an EFI boot. + + @param ImageHandle of the loaded driver + + @retval EFI_SUCCESS Source was updated to support Handler. + @retval EFI_DEVICE_ERROR Hardware could not be programmed. + +**/ +EFI_STATUS +EFIAPI +InitializeDatahub ( + IN EFI_HANDLE ImageHandle + ) +{ + EFI_STATUS Status; + EFI_DATA_HUB_PROTOCOL *DataHub; + + Status = gBS->LocateProtocol(&gEfiDataHubProtocolGuid, NULL, (VOID **)&DataHub); + ASSERT_EFI_ERROR(Status); + + UINT64 FsbFrequency = 667000000; + UINT32 DevicePathsSupported = 1; + + SetEfiPlatformProperty(DataHub, L"FSBFrequency", gAppleFsbFrequencyPropertyGuid, &FsbFrequency, sizeof(UINT64)); + SetEfiPlatformProperty(DataHub, L"DevicePathsSupported", gAppleDevicePathsSupportedGuid, &DevicePathsSupported, sizeof(UINT32)); + ASSERT_EFI_ERROR(Status); + + return Status; +} diff --git a/Library/AppleSupportLib/Datahub.h b/Library/AppleSupportLib/Datahub.h new file mode 100644 index 0000000..4a40eac --- /dev/null +++ b/Library/AppleSupportLib/Datahub.h @@ -0,0 +1,33 @@ +/** @file +* +* Copyright (c) 2014, Reza Jelveh. 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 _APPLESUPPORT_DATAHUB_H_INCLUDED_ +#define _APPLESUPPORT_DATAHUB_H_INCLUDED_ + +#include "Common.h" + +#include + +#include +#include + +#include + +EFI_STATUS +EFIAPI +InitializeDatahub ( + IN EFI_HANDLE ImageHandle + ); + +#endif diff --git a/Library/AppleSupportLib/SmBios.c b/Library/AppleSupportLib/SmBios.c new file mode 100644 index 0000000..9daaea5 --- /dev/null +++ b/Library/AppleSupportLib/SmBios.c @@ -0,0 +1,115 @@ +/** @file +* +* Copyright (c) 2021, Vladislav Yaroshchuk +* Copyright (c) 2006 - 2009, Intel Corporation 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 "SmBios.h" + +/** + * Reallocate Smbios table with Memory type EfiReservedMemoryType + * And placing in E/F Seg + * + * @param Table Pointer to the table + * @return EFI_SUCCESS - Relocate Table successfully, other - failed + */ +STATIC +EFI_STATUS +ReallocSmbiosTable ( + IN OUT SMBIOS_TABLE_ENTRY_POINT **Table +) +{ + SMBIOS_TABLE_ENTRY_POINT *SmbiosTableNew; + SMBIOS_TABLE_ENTRY_POINT *SmbiosTableOri; + UINT32 SmbiosEntryLen; + UINT32 BufferLen; + EFI_PHYSICAL_ADDRESS BufferPtr; + EFI_STATUS Status; + + SmbiosTableNew = NULL; + SmbiosTableOri = *Table; + + // + // Relocate the Smibos memory + // + BufferPtr = EFI_SYSTEM_TABLE_MAX_ADDRESS; + if (SmbiosTableOri->SmbiosBcdRevision != 0x21) { + SmbiosEntryLen = SmbiosTableOri->EntryPointLength; + } else { + // + // According to Smbios Spec 2.4, we should set entry point length as 0x1F if version is 2.1 + // + SmbiosEntryLen = 0x1F; + } + BufferLen = SmbiosEntryLen + SYS_TABLE_PAD(SmbiosEntryLen) + SmbiosTableOri->TableLength; + Status = gBS->AllocatePages ( + AllocateMaxAddress, + EfiReservedMemoryType, + EFI_SIZE_TO_PAGES(BufferLen), + &BufferPtr + ); + ASSERT_EFI_ERROR (Status); + + SmbiosTableNew = (SMBIOS_TABLE_ENTRY_POINT *)(UINTN)BufferPtr; + CopyMem ( + SmbiosTableNew, + SmbiosTableOri, + SmbiosEntryLen + ); + // + // Get Smbios Structure table address, and make sure the start address is 32-bit align + // + BufferPtr += SmbiosEntryLen + SYS_TABLE_PAD(SmbiosEntryLen); + CopyMem ( + (VOID *)(UINTN)BufferPtr, + (VOID *)(UINTN)(SmbiosTableOri->TableAddress), + SmbiosTableOri->TableLength + ); + + SmbiosTableNew->TableAddress = (UINT32)BufferPtr; + SmbiosTableNew->IntermediateChecksum = 0; + SmbiosTableNew->IntermediateChecksum = + CalculateCheckSum8 ((UINT8*)SmbiosTableNew + 0x10, SmbiosEntryLen -0x10); + + // + // Change the SMBIOS pointer + // + *Table = SmbiosTableNew; + + return EFI_SUCCESS; +} + +/** + * Reinstall Smbios table. Makes Smbios can be read by the guest + * + * @return EFI_SUCCESS - done successfully, other - failed + */ +EFI_STATUS +InitializeSmBios ( +) +{ + EFI_STATUS Status; + SMBIOS_TABLE_ENTRY_POINT *mOriginalSmbios; + + Status = EfiGetSystemConfigurationTable ( + &gEfiSmbiosTableGuid, + (VOID **) &mOriginalSmbios + ); + ASSERT_EFI_ERROR (Status); + + Status = ReallocSmbiosTable (&mOriginalSmbios); + ASSERT_EFI_ERROR (Status); + + Status = gBS->InstallConfigurationTable (&gEfiSmbiosTableGuid, mOriginalSmbios); + ASSERT_EFI_ERROR (Status); + + return Status; +} \ No newline at end of file diff --git a/Library/AppleSupportLib/SmBios.h b/Library/AppleSupportLib/SmBios.h new file mode 100644 index 0000000..fc92a9f --- /dev/null +++ b/Library/AppleSupportLib/SmBios.h @@ -0,0 +1,31 @@ +/** @file +* +* Copyright (c) 2021, Vladislav Yaroshchuk +* +* 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 _APPLESUPPORT_SMBIOS_H_INCLUDED_ +#define _APPLESUPPORT_SMBIOS_H_INCLUDED_ + +#include "Common.h" + +#include + +#include + +#define EFI_SYSTEM_TABLE_MAX_ADDRESS 0xFFFFFFFF +#define SYS_TABLE_PAD(ptr) (((~ptr) +1) & 0x07 ) + +EFI_STATUS +InitializeSmBios ( +); + +#endif //_APPLESUPPORT_SMBIOS_H_INCLUDED_ diff --git a/Library/PlatformBootManagerLib/BdsPlatform.c b/Library/PlatformBootManagerLib/BdsPlatform.c index cab8905..3ff0a82 100644 --- a/Library/PlatformBootManagerLib/BdsPlatform.c +++ b/Library/PlatformBootManagerLib/BdsPlatform.c @@ -12,6 +12,7 @@ #include #include #include +#include // @@ -365,6 +366,11 @@ PlatformBootManagerBeforeConsole ( VisitAllInstancesOfProtocol (&gEfiPciRootBridgeIoProtocolGuid, ConnectRootBridge, NULL); + // + // Initialize AppleSupportLib + // + InitializeAppleSupport (gImageHandle, gST); + // // Signal the ACPI platform driver that it can download QEMU ACPI tables. // diff --git a/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf b/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf index a6526f9..25894cc 100644 --- a/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf +++ b/Library/PlatformBootManagerLib/PlatformBootManagerLib.inf @@ -35,6 +35,7 @@ ShellPkg/ShellPkg.dec [LibraryClasses] + AppleSupportLib BaseLib MemoryAllocationLib UefiBootServicesTableLib diff --git a/OvmfDarwinPkg.dec b/OvmfDarwinPkg.dec index 450264a..65fe1e6 100644 --- a/OvmfDarwinPkg.dec +++ b/OvmfDarwinPkg.dec @@ -134,6 +134,14 @@ gAppleApfsContainerInfoGuid = {0x3533CF0D, 0x685F, 0x5EBF, {0x8D, 0xC6, 0x73, 0x93, 0x48, 0x5B, 0xAF, 0xA2}} gAppleApfsVolumeInfoGuid = {0x900C7693, 0x8C14, 0x58BA, {0xB4, 0x4E, 0x97, 0x45, 0x15, 0xD2, 0x7C, 0x78}} + # AppleSupportLib + gAppleFirmwareVariableGuid = {0x4D1EDE05, 0x38C7, 0x4A6A, {0x9C, 0xC6, 0x4B, 0xCC, 0xA8, 0xB3, 0x8C, 0x14}} + gAppleNVRAMVariableGuid = {0x7C436110, 0xAB2A, 0x4BBB, {0xA8, 0x80, 0xFE, 0x41, 0x99, 0x5C, 0x9F, 0x82}} + gEfiSmbiosTableGuid = {0xeb9d2d31, 0x2d88, 0x11d3, {0x9a, 0x16, 0x0, 0x90, 0x27, 0x3f, 0xc1, 0x4d}} + gAppleSystemInfoProducerNameGuid = {0x64517CC8, 0x6561, 0x4051, {0xB0, 0x3C, 0x59, 0x64, 0xB6, 0x0F, 0x4C, 0x7A}} + gAppleFsbFrequencyPropertyGuid = {0xD1A04D55, 0x75B9, 0x41A3, {0x90, 0x36, 0x8F, 0x4A, 0x26, 0x1C, 0xBB, 0xA2}} + gAppleDevicePathsSupportedGuid = {0x5BB91CF7, 0xD816, 0x404B, {0x86, 0x72, 0x68, 0xF2, 0x7F, 0x78, 0x31, 0xDC}} + [Ppis] # PPI whose presence in the PPI database signals that the TPM base address # has been discovered and recorded @@ -162,6 +170,10 @@ gApplePartitionInfoProtocolGuid = {0x68425EE5, 0x1C43, 0x4BAA, {0x84, 0xF7, 0x9A, 0xA8, 0xA4, 0xD8, 0xE1, 0x1E}} gApfsEfiBootRecordInfoProtocolGuid = {0x03B8D751, 0xA02F, 0x4FF8, {0x9B, 0x1A, 0x55, 0x24, 0xAF, 0xA3, 0x94, 0x5F}} + # AppleSupportLib + gEfiConsoleControlProtocolGuid = {0xf42f7782, 0x012e, 0x4c12, {0x99, 0x56, 0x49, 0xf9, 0x43, 0x04, 0xf7, 0x21}} + gEfiAppleBootPathProtocolGuid = {0x85e1deb6, 0xd97a, 0x4bea, {0xa0, 0xa0, 0xe6, 0xa9, 0x43, 0x32, 0x85, 0x09}} + [PcdsFixedAtBuild] gUefiOvmfPkgTokenSpaceGuid.PcdOvmfPeiMemFvBase|0x0|UINT32|0 gUefiOvmfPkgTokenSpaceGuid.PcdOvmfPeiMemFvSize|0x0|UINT32|1 diff --git a/OvmfDarwinPkgX64.dsc b/OvmfDarwinPkgX64.dsc index 01ee7e4..e6d6fbc 100644 --- a/OvmfDarwinPkgX64.dsc +++ b/OvmfDarwinPkgX64.dsc @@ -125,6 +125,8 @@ !include MdePkg/MdeLibs.dsc.inc +!include OvmfDarwinPkg/DarwinLibs.dsc.inc + [LibraryClasses] PcdLib|MdePkg/Library/BasePcdLibNull/BasePcdLibNull.inf TimerLib|OvmfDarwinPkg/Library/AcpiTimerLib/BaseAcpiTimerLib.inf