未验证 提交 27905dd7 编写于 作者: P PMheart 提交者: GitHub

Utilities: Introduce TestProcessKernel tool for testing kext...

Utilities: Introduce TestProcessKernel tool for testing kext injection/patching from config, by @mhaeuser (#369)
上级 44cc14d3
......@@ -47,8 +47,10 @@ prelinkedkernel.unpack
*.nam
*.til
*.aml
*.kext
Debug/QemuRun
Doxy
BootKernelExtensions.kc
Legacy/BinDrivers/X64
Legacy/BinDrivers/IA32
Legacy/BootLoader/bin
......@@ -74,13 +76,14 @@ Utilities/TestImg4/Img4
Utilities/TestMp3/Mp3
Utilities/TestMp3/test.bin
Utilities/TestKernelCollection2/KernelCollection2
Utilities/TestKextInject/BootKernelExtensions.kc
Utilities/TestKextInject/*.kext
Utilities/TestKextInject/KextInject
Utilities/TestKextInject/Result.xml
Utilities/TestMacho/Macho
Utilities/TestRsaPreprocess/RsaPreprocess
Utilities/TestSmbios/Smbios
Utilities/TestPeCoff/PeCoff
Utilities/TestProcessKernel/EFI/*
Utilities/TestProcessKernel/OC/*
Utilities/TestProcessKernel/ProcessKernel
Utilities/TestNtfsDxe/TestNtfsDxe
Utilities/TestExt4Dxe/TestExt4Dxe
......@@ -137,6 +137,20 @@ OcKernelApplyQuirk (
IN OUT PATCHER_CONTEXT *KernelPatcher
);
/**
Inject kexts.
**/
VOID
OcKernelInjectKexts (
IN OC_GLOBAL_CONFIG *Config,
IN KERNEL_CACHE_TYPE CacheType,
IN VOID *Context,
IN UINT32 DarwinVersion,
IN BOOLEAN Is32Bit,
IN UINT32 LinkedExpansion,
IN UINT32 ReservedExeSize
);
/**
Apply kernel patch.
**/
......@@ -164,6 +178,21 @@ OcKernelBlockKexts (
IN VOID *Context
);
/**
Process prelinked.
**/
EFI_STATUS
OcKernelProcessPrelinked (
IN OC_GLOBAL_CONFIG *Config,
IN UINT32 DarwinVersion,
IN BOOLEAN Is32Bit,
IN OUT UINT8 *Kernel,
IN UINT32 *KernelSize,
IN UINT32 AllocatedSize,
IN UINT32 LinkedExpansion,
IN UINT32 ReservedExeSize
);
/**
Cleanup Kernel compatibility support on failure.
**/
......
......@@ -908,6 +908,8 @@ PatchKernelCpuId (
sizeof (mKernelCpuidFindMcRel) - sizeof (INTERNAL_MICROCODE_PATCH),
0x90
);
DEBUG ((DEBUG_INFO, "OCAK: Patch success CPUID release\n"));
return EFI_SUCCESS;
}
}
......@@ -939,6 +941,7 @@ PatchKernelCpuId (
FnPatch->AppleFamily1 = FnPatch->AppleFamily2 = OcCpuModelToAppleFamily (Eax);
DEBUG ((DEBUG_INFO, "OCAK: Patch success CPUID debug\n"));
return EFI_SUCCESS;
}
......
......@@ -681,7 +681,6 @@ OcKernelInjectKext (
DEBUG_CODE_END ();
}
STATIC
VOID
OcKernelInjectKexts (
IN OC_GLOBAL_CONFIG *Config,
......@@ -764,7 +763,6 @@ OcKernelInjectKexts (
}
}
STATIC
EFI_STATUS
OcKernelProcessPrelinked (
IN OC_GLOBAL_CONFIG *Config,
......
......@@ -60,6 +60,7 @@ extern BOOLEAN _gPcd_FixedAtBuild_PcdImageLoaderTolerantLoad;
extern BOOLEAN _gPcd_FixedAtBuild_PcdImageLoaderSupportDebug;
extern UINT8 _gPcd_FixedAtBuild_PcdUefiVariableDefaultLang[4];
extern UINT8 _gPcd_FixedAtBuild_PcdUefiVariableDefaultPlatformLang[6];
extern UINT32 _gPcd_BinaryPatch_PcdSerialRegisterStride;
#define _PCD_GET_MODE_32_PcdUefiLibMaxPrintBufferSize _gPcd_FixedAtBuild_PcdUefiLibMaxPrintBufferSize
#define _PCD_GET_MODE_BOOL_PcdUgaConsumeSupport _gPcd_FixedAtBuild_PcdUgaConsumeSupport
......@@ -78,6 +79,7 @@ extern UINT8 _gPcd_FixedAtBuild_PcdUefiVariableDefaultPlatformLang[6];
#define _PCD_GET_MODE_PTR_PcdUefiVariableDefaultLang _gPcd_FixedAtBuild_PcdUefiVariableDefaultLang
#define _PCD_GET_MODE_PTR_PcdUefiVariableDefaultPlatformLang _gPcd_FixedAtBuild_PcdUefiVariableDefaultPlatformLang
#define _PCD_GET_MODE_BOOL_PcdValidateOrderedCollection ((BOOLEAN)0U)
#define _PCD_GET_MODE_32_PcdSerialRegisterStride _gPcd_BinaryPatch_PcdSerialRegisterStride
//
// This will not be of any effect at userspace.
//
......
......@@ -695,3 +695,11 @@ OcLocateRootVolume (
{
return NULL;
}
UINTN
GetSerialRegisterBase (
VOID
)
{
return 0;
}
......@@ -3,7 +3,13 @@
SPDX-License-Identifier: BSD-3-Clause
**/
#include <Library/DebugLib.h>
#include <Library/OcAppleKernelLib.h>
#include <Library/OcBootManagementLib.h>
#include <Library/OcDevicePathLib.h>
#include <Library/OcFileLib.h>
#include <Protocol/LoadedImage.h>
EFI_DEVICE_PATH_PROTOCOL *
OcGetNextLoadOptionDevicePath (
......@@ -11,5 +17,363 @@ OcGetNextLoadOptionDevicePath (
IN EFI_DEVICE_PATH_PROTOCOL *FullPath
)
{
ASSERT (FALSE);
return NULL;
}
EFI_STATUS
CachelessContextAddKext (
IN OUT CACHELESS_CONTEXT *Context,
IN CONST CHAR8 *InfoPlist,
IN UINT32 InfoPlistSize,
IN UINT8 *Executable OPTIONAL,
IN UINT32 ExecutableSize OPTIONAL,
OUT CHAR8 BundleVersion[MAX_INFO_BUNDLE_VERSION_KEY_SIZE] OPTIONAL
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
EFI_STATUS
CachelessContextAddPatch (
IN OUT CACHELESS_CONTEXT *Context,
IN CONST CHAR8 *Identifier,
IN PATCHER_GENERIC_PATCH *Patch
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
EFI_STATUS
CachelessContextAddQuirk (
IN OUT CACHELESS_CONTEXT *Context,
IN KERNEL_QUIRK_NAME Quirk
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
EFI_STATUS
CachelessContextBlock (
IN OUT CACHELESS_CONTEXT *Context,
IN CONST CHAR8 *Identifier,
IN BOOLEAN Exclude
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
EFI_STATUS
CachelessContextForceKext (
IN OUT CACHELESS_CONTEXT *Context,
IN CONST CHAR8 *Identifier
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
VOID
CachelessContextFree (
IN OUT CACHELESS_CONTEXT *Context
)
{
ASSERT (FALSE);
}
EFI_STATUS
CachelessContextHookBuiltin (
IN OUT CACHELESS_CONTEXT *Context,
IN CONST CHAR16 *FileName,
IN EFI_FILE_PROTOCOL *File,
OUT EFI_FILE_PROTOCOL **VirtualFile
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
EFI_STATUS
CachelessContextInit (
IN OUT CACHELESS_CONTEXT *Context,
IN CONST CHAR16 *FileName,
IN EFI_FILE_PROTOCOL *ExtensionsDir,
IN UINT32 KernelVersion,
IN BOOLEAN Is32Bit
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
EFI_STATUS
CachelessContextOverlayExtensionsDir (
IN OUT CACHELESS_CONTEXT *Context,
OUT EFI_FILE_PROTOCOL **File
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
EFI_STATUS
CachelessContextPerformInject (
IN OUT CACHELESS_CONTEXT *Context,
IN CONST CHAR16 *FileName,
OUT EFI_FILE_PROTOCOL **File
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
EFI_STATUS
CreateRealFile (
IN EFI_FILE_PROTOCOL *OriginalFile OPTIONAL,
IN EFI_FILE_OPEN OpenCallback OPTIONAL,
IN BOOLEAN CloseOnFailure,
OUT EFI_FILE_PROTOCOL **File
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
EFI_STATUS
CreateVirtualFileFileNameCopy (
IN CONST CHAR16 *FileName,
IN VOID *FileBuffer,
IN UINT64 FileSize,
IN CONST EFI_TIME *ModificationTime OPTIONAL,
OUT EFI_FILE_PROTOCOL **File
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
EFI_STATUS
DisableVirtualFs (
IN OUT EFI_BOOT_SERVICES *BootServices
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
EFI_STATUS
EnableVirtualFs (
IN OUT EFI_BOOT_SERVICES *BootServices,
IN EFI_FILE_OPEN OpenCallback
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
BOOLEAN
OcAppendArgumentsToLoadedImage (
IN OUT EFI_LOADED_IMAGE_PROTOCOL *LoadedImage,
IN CONST CHAR8 **Arguments,
IN UINT32 ArgumentCount,
IN BOOLEAN Replace
)
{
ASSERT (FALSE);
return FALSE;
}
BOOLEAN
OcCheckArgumentFromEnv (
IN EFI_LOADED_IMAGE *LoadedImage OPTIONAL,
IN EFI_GET_VARIABLE GetVariable OPTIONAL,
IN CONST CHAR8 *Argument,
IN CONST UINTN ArgumentLength,
IN OUT CHAR8 **Value OPTIONAL
)
{
ASSERT (FALSE);
return FALSE;
}
VOID
OcDirectorySeachContextInit (
IN OUT DIRECTORY_SEARCH_CONTEXT *Context
)
{
ASSERT (FALSE);
}
EFI_STATUS
OcFindWritableOcFileSystem (
OUT EFI_FILE_PROTOCOL **FileSystem
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
OC_BOOT_ENTRY_TYPE
OcGetBootDevicePathType (
IN EFI_DEVICE_PATH_PROTOCOL *DevicePath,
OUT BOOLEAN *IsFolder OPTIONAL,
OUT BOOLEAN *IsGeneric OPTIONAL
)
{
ASSERT (FALSE);
return 0;
}
EFI_STATUS
OcGetFileModificationTime (
IN EFI_FILE_PROTOCOL *File,
OUT EFI_TIME *Time
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
EFI_STATUS
OcGetNewestFileFromDirectory (
IN OUT DIRECTORY_SEARCH_CONTEXT *Context,
IN EFI_FILE_PROTOCOL *Directory,
IN CHAR16 *FileNameStartsWith OPTIONAL,
OUT EFI_FILE_INFO **FileInfo
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
VOID
OcImageLoaderRegisterConfigure (
IN OC_IMAGE_LOADER_CONFIGURE Configure OPTIONAL
)
{
ASSERT (FALSE);
}
BOOLEAN
OcPlatformIs64BitSupported (
IN UINT32 KernelVersion
)
{
ASSERT (FALSE);
return FALSE;
}
EFI_STATUS
OcSafeFileOpen (
IN CONST EFI_FILE_PROTOCOL *Protocol,
OUT EFI_FILE_PROTOCOL **NewHandle,
IN CONST CHAR16 *FileName,
IN CONST UINT64 OpenMode,
IN CONST UINT64 Attributes
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
VOID *
OcStorageReadFileUnicode (
IN OC_STORAGE_CONTEXT *Context,
IN CONST CHAR16 *FilePath,
OUT UINT32 *FileSize OPTIONAL
)
{
ASSERT (FALSE);
return NULL;
}
VOID *
OcReadFileFromDirectory (
IN CONST EFI_FILE_PROTOCOL *RootDirectory,
IN CONST CHAR16 *FilePath,
OUT UINT32 *FileSize OPTIONAL,
IN UINT32 MaxFileSize OPTIONAL
)
{
ASSERT (FALSE);
return NULL;
}
EFI_MEMORY_DESCRIPTOR *
OcGetCurrentMemoryMap (
OUT UINTN *MemoryMapSize,
OUT UINTN *DescriptorSize,
OUT UINTN *MapKey OPTIONAL,
OUT UINT32 *DescriptorVersion OPTIONAL,
OUT UINTN *OriginalMemoryMapSize OPTIONAL,
IN BOOLEAN IncludeSplitSpace
)
{
ASSERT (FALSE);
return NULL;
}
VOID *
OcGetFileInfo (
IN EFI_FILE_PROTOCOL *File,
IN EFI_GUID *InformationType,
IN UINTN MinFileInfoSize,
OUT UINTN *RealFileInfoSize OPTIONAL
)
{
ASSERT (FALSE);
return NULL;
}
CHAR16 *
OcGetVolumeLabel (
IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem
)
{
ASSERT (FALSE);
return NULL;
}
VOID *
OcReadFile (
IN CONST EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem,
IN CONST CHAR16 *FilePath,
OUT UINT32 *FileSize OPTIONAL,
IN CONST UINT32 MaxFileSize OPTIONAL
)
{
ASSERT (FALSE);
return NULL;
}
......@@ -37,3 +37,4 @@ BOOLEAN _gPcd_FixedAtBuild_PcdImageLoaderSupportArmThumb = FALSE;
BOOLEAN _gPcd_FixedAtBuild_PcdImageLoaderForceLoadDebug = FALSE;
BOOLEAN _gPcd_FixedAtBuild_PcdImageLoaderTolerantLoad = TRUE;
BOOLEAN _gPcd_FixedAtBuild_PcdImageLoaderSupportDebug = FALSE;
UINT32 _gPcd_BinaryPatch_PcdSerialRegisterStride = 0;
/** @file
This file is part of OpenCanopy, OpenCore GUI.
Copyright (c) 2018-2019, Download-Fritz. All rights reserved.<BR>
SPDX-License-Identifier: BSD-3-Clause
**/
#include <Library/DebugLib.h>
EFI_STATUS
OcGetFileData (
IN EFI_FILE_PROTOCOL *File,
IN UINT32 Position,
IN UINT32 Size,
OUT UINT8 *Buffer
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
EFI_STATUS
OcReadFileSize (
IN EFI_SIMPLE_FILE_SYSTEM_PROTOCOL *FileSystem,
IN CONST CHAR16 *FilePath,
OUT UINT32 *Size
)
{
ASSERT (FALSE);
return EFI_UNSUPPORTED;
}
......@@ -5,7 +5,7 @@
PROJECT = Bmf
PRODUCT = $(PROJECT)$(INFIX)$(SUFFIX)
OBJS = $(PROJECT).o
OBJS = $(PROJECT).o BmfDummy.o
#
# From OpenCanopy.
#
......@@ -13,7 +13,7 @@ OBJS += BitmapFont.o Images.o Blending.o
#
# From OpenCore.
#
OBJS += OcPng.o lodepng.o OcCompressionLib.o OcTimerLib.o OcAppleKeyMapLib.o UpDownDetection.o OcTypingLib.o ConsoleUtils.o BootEntryInfo.o OcAppleBootPolicyLib.o OcDevicePathLib.o DebugPrint.o GetFileInfo.o GetVolumeLabel.o ReadFile.o OpenFile.o FileProtocol.o OcStorageLib.o BootAudio.o
OBJS += OcPng.o lodepng.o OcCompressionLib.o OcTimerLib.o OcAppleKeyMapLib.o UpDownDetection.o OcTypingLib.o ConsoleUtils.o BootEntryInfo.o OcAppleBootPolicyLib.o OcDevicePathLib.o DebugPrint.o BootAudio.o
VPATH = ../../Platform/OpenCanopy:$\
../../Platform/OpenCanopy/Input:$\
......@@ -28,7 +28,6 @@ VPATH = ../../Platform/OpenCanopy:$\
../../Library/OcDevicePathLib:$\
../../Library/OcDebugLibProtocol:$\
../../Library/OcStringLib:$\
../../Library/OcFileLib:$\
../../Library/OcStorageLib:$\
../../Library/OcTemplateLib:$\
../../Library/OcTypingLib:$\
......
......@@ -7,20 +7,6 @@
#include <Library/OcMemoryLib.h>
#include <Library/OcFileLib.h>
EFI_MEMORY_DESCRIPTOR *
OcGetCurrentMemoryMap (
OUT UINTN *MemoryMapSize,
OUT UINTN *DescriptorSize,
OUT UINTN *MapKey OPTIONAL,
OUT UINT32 *DescriptorVersion OPTIONAL,
OUT UINTN *OriginalMemoryMapSize OPTIONAL,
IN BOOLEAN IncludeSplitSpace
)
{
ASSERT (FALSE);
return NULL;
}
EFI_STATUS
OcGetFileSize (
IN EFI_FILE_PROTOCOL *File,
......
## @file
# Copyright (c) 2020, PMheart. All rights reserved.
# SPDX-License-Identifier: BSD-3-Clause
##
PROJECT = ProcessKernel
PRODUCT = $(PROJECT)$(INFIX)$(SUFFIX)
OBJS = $(PROJECT).o \
ProcessKernelDummy.o \
CommonPatches.o \
CpuidPatches.o \
KextPatcher.o \
KxldState.o \
PrelinkedKext.o \
PrelinkedContext.o \
MkextContext.o \
Vtables.o \
Link.o \
KernelReader.o \
KernelCollection.o \
lzss.o \
lzvn.o \
adler32.o \
compress.o \
crc32.o \
deflate.o \
infback.o \
inffast.o \
inflate.o \
inftrees.o \
trees.o \
uncompr.o \
zlib_uefi.o
#
# OcConfigurationLib targets.
#
OBJS += OcConfigurationLib.o
#
# OpenCoreKernel targets.
#
OBJS += OpenCoreKernel.o
OBJS += OpenCoreKernelPatch.o
VPATH = ../../Library/OcConfigurationLib:$\
../../Library/OcAppleKernelLib:$\
../../Library/OcMainLib:$\
../../Library/OcCompressionLib/lzss:$\
../../Library/OcCompressionLib/lzvn:$\
../../Library/OcCompressionLib/zlib
include ../../User/Makefile
/** @file
Copyright (C) 2022, Marvin Haeuser. All rights reserved.
Copyright (C) 2022, PMheart. 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 <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/OcTemplateLib.h>
#include <Library/OcSerializeLib.h>
#include <Library/OcMiscLib.h>
#include <Library/OcAppleKernelLib.h>
#include <Library/OcConfigurationLib.h>
#include <Library/OcMainLib.h>
#include <UserFile.h>
#define OC_USER_FULL_PATH_MAX_SIZE 256
STATIC CHAR8 mFullPath[OC_USER_FULL_PATH_MAX_SIZE] = { 0 };
STATIC UINTN mRootPathLen = 0;
STATIC
BOOLEAN
UserSetRootPath (
IN CONST CHAR8 *RootPath
)
{
UINTN RootPathLen;
RootPathLen = AsciiStrLen (RootPath);
//
// Preserve 2 bytes for '/' and '\0'.
//
if (RootPathLen > OC_USER_FULL_PATH_MAX_SIZE - 2) {
DEBUG ((DEBUG_ERROR, "RootPath is too long!\n"));
return FALSE;
}
AsciiStrCpyS (mFullPath, sizeof (mFullPath) - 1, RootPath);
//
// If passed without '/' in the end, append it.
//
if (mFullPath[RootPathLen - 1] != '/') {
mFullPath[RootPathLen] = '/';
mFullPath[RootPathLen + 1] = '\0';
mRootPathLen = RootPathLen + 1;
} else {
mRootPathLen = RootPathLen;
}
DEBUG ((DEBUG_ERROR, "Root Path: %a\n", mFullPath));
return TRUE;
}
STATIC
UINT8 *
UserReadFileFromRoot (
IN CONST CHAR8 *FileName,
OUT UINT32 *Size
)
{
AsciiStrCpyS (&mFullPath[mRootPathLen], sizeof (mFullPath) - mRootPathLen - 1, FileName);
DEBUG ((DEBUG_ERROR, "Full path: %a\n", mFullPath));
return UserReadFile (mFullPath, Size);
}
STATIC BOOLEAN FailedToProcess = FALSE;
STATIC UINT32 KernelVersion = 0;
STATIC EFI_FILE_PROTOCOL NilFileProtocol;
STATIC UINT8 *mPrelinked = NULL;
STATIC UINT32 mPrelinkedSize = 0;
//
// TODO: Windows portability.
//
STATIC
VOID
AsciiHostSlashes (
IN OUT CHAR8 *String
)
{
CHAR8 *Needle;
Needle = String;
while ((Needle = AsciiStrStr (Needle, "\\")) != NULL) {
*Needle++ = '/';
}
}
STATIC
EFI_STATUS
UserOcKernelLoadAndReserveKext (
IN OC_KERNEL_ADD_ENTRY *Kext,
IN UINT32 Index,
IN OC_GLOBAL_CONFIG *Config,
IN BOOLEAN Is32Bit,
IN OUT UINT32 *ReservedExeSize,
IN OUT UINT32 *ReservedInfoSize,
IN OUT UINT32 *NumReservedKexts
)
{
EFI_STATUS Status;
CHAR8 *BundlePath;
CHAR8 *Comment;
CONST CHAR8 *Arch;
CHAR8 *PlistPath;
CHAR8 *ExecutablePath;
CHAR8 FullPath[OC_STORAGE_SAFE_PATH_MAX];
if (!Kext->Enabled) {
return EFI_SUCCESS;
}
BundlePath = OC_BLOB_GET (&Kext->BundlePath);
Comment = OC_BLOB_GET (&Kext->Comment);
Arch = OC_BLOB_GET (&Kext->Arch);
PlistPath = OC_BLOB_GET (&Kext->PlistPath);
if ((BundlePath[0] == '\0') || (PlistPath[0] == '\0')) {
DEBUG ((
DEBUG_ERROR,
"OC: Injected kext %u (%a) has invalid info\n",
Index,
Comment
));
Kext->Enabled = FALSE;
return EFI_INVALID_PARAMETER;
}
if (AsciiStrCmp (Arch, Is32Bit ? "x86_64" : "i386") == 0) {
DEBUG ((
DEBUG_INFO,
"OC: Injected kext %a (%a) at %u skipped due to arch %a != %a\n",
BundlePath,
Comment,
Index,
Arch,
Is32Bit ? "i386" : "x86_64"
));
return EFI_SUCCESS;
}
//
// Required for possible cacheless force injection later on.
//
AsciiHostSlashes (BundlePath);
//
// Get plist path and data.
//
Status = OcAsciiSafeSPrint (
FullPath,
sizeof (FullPath),
"%s%a\\%a",
OPEN_CORE_KEXT_PATH,
BundlePath,
PlistPath
);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_WARN,
"OC: Failed to fit injected kext path %s%a\\%a",
OPEN_CORE_KEXT_PATH,
BundlePath,
PlistPath
));
Kext->Enabled = FALSE;
return Status;
}
AsciiHostSlashes (FullPath);
Kext->PlistData = (CHAR8 *)UserReadFileFromRoot (
FullPath,
&Kext->PlistDataSize
);
if (Kext->PlistData == NULL) {
DEBUG ((
DEBUG_ERROR,
"OC: Plist injected is missing for %s kext %a (%a)\n",
FullPath,
BundlePath,
Comment
));
Kext->Enabled = FALSE;
return EFI_UNSUPPORTED;
}
//
// Get executable path and data, if present.
//
ExecutablePath = OC_BLOB_GET (&Kext->ExecutablePath);
if (ExecutablePath[0] != '\0') {
Status = OcAsciiSafeSPrint (
FullPath,
sizeof (FullPath),
"%s%a\\%a",
OPEN_CORE_KEXT_PATH,
BundlePath,
ExecutablePath
);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_WARN,
"OC: Failed to fit injected kext path %s%a\\%a",
OPEN_CORE_KEXT_PATH,
BundlePath,
ExecutablePath
));
Kext->Enabled = FALSE;
FreePool (Kext->PlistData);
Kext->PlistData = NULL;
return Status;
}
AsciiHostSlashes (FullPath);
Kext->ImageData = UserReadFileFromRoot (
FullPath,
&Kext->ImageDataSize
);
if (Kext->ImageData == NULL) {
DEBUG ((
DEBUG_ERROR,
"OC: Image injected is missing for %a kext %a (%a)\n",
FullPath,
BundlePath,
Comment
));
Kext->Enabled = FALSE;
FreePool (Kext->PlistData);
Kext->PlistData = NULL;
return EFI_UNSUPPORTED;
}
}
Status = PrelinkedReserveKextSize (
ReservedInfoSize,
ReservedExeSize,
Kext->PlistDataSize,
Kext->ImageData,
Kext->ImageDataSize,
Is32Bit
);
if (EFI_ERROR (Status)) {
DEBUG ((
DEBUG_INFO,
"OC: Failed to fit %s kext %a (%a) - %r\n",
Is32Bit ? L"32-bit" : L"64-bit",
BundlePath,
Comment,
Status
));
if (Kext->ImageData != NULL) {
FreePool (Kext->ImageData);
Kext->ImageData = NULL;
}
FreePool (Kext->PlistData);
Kext->PlistData = NULL;
return Status;
}
(*NumReservedKexts)++;
return EFI_SUCCESS;
}
EFI_STATUS
OcGetFileData (
IN EFI_FILE_PROTOCOL *File,
IN UINT32 Position,
IN UINT32 Size,
OUT UINT8 *Buffer
)
{
ASSERT (File == &NilFileProtocol);
if ((UINT64)Position + Size > mPrelinkedSize) {
return EFI_INVALID_PARAMETER;
}
CopyMem (&Buffer[0], &mPrelinked[Position], Size);
return EFI_SUCCESS;
}
EFI_STATUS
OcGetFileSize (
IN EFI_FILE_PROTOCOL *File,
OUT UINT32 *Size
)
{
ASSERT (File == &NilFileProtocol);
*Size = mPrelinkedSize;
return EFI_SUCCESS;
}
int
WrapMain (
int argc,
char *argv[]
)
{
UINT8 *ConfigFileBuffer;
UINT32 ConfigFileSize;
OC_GLOBAL_CONFIG Config;
EFI_STATUS Status;
UINT32 ErrorCount;
UINT32 Index;
UINT32 AllocSize;
EFI_STATUS PrelinkedStatus;
CONST CHAR8 *FileName;
BOOLEAN mUse32BitKernel;
UINT32 ReservedInfoSize;
UINT32 ReservedExeSize;
UINT32 NumReservedKexts;
UINT32 LinkedExpansion;
UINT8 *NewPrelinked;
UINT32 NewPrelinkedSize;
UINT8 Sha384[48];
BOOLEAN Is32Bit;
OC_CPU_INFO DummyCpuInfo;
OC_KERNEL_ADD_ENTRY *Kext;
if (argc < 2) {
DEBUG ((DEBUG_ERROR, "Usage: %a <path/to/OC/folder/> [path/to/kernel]\n\n", argv[0]));
return -1;
}
FileName = argc > 2 ? argv[2] : "/System/Library/PrelinkedKernels/prelinkedkernel";
if ((mPrelinked = UserReadFile (FileName, &mPrelinkedSize)) == NULL) {
DEBUG ((DEBUG_ERROR, "Read fail %a\n", FileName));
return -1;
}
if (!UserSetRootPath (argv[1])) {
return -1;
}
//
// Read config file (Only one single config is supported).
//
CHAR8 AsciiOcConfig[16];
UnicodeStrToAsciiStrS (OPEN_CORE_CONFIG_PATH, AsciiOcConfig, L_STR_SIZE (OPEN_CORE_CONFIG_PATH));
ConfigFileBuffer = UserReadFileFromRoot (AsciiOcConfig, &ConfigFileSize);
if (ConfigFileBuffer == NULL) {
DEBUG ((DEBUG_ERROR, "Failed to read %s\n", OPEN_CORE_CONFIG_PATH));
return -1;
}
//
// Initialise config structure to be checked, and exit on error.
//
ErrorCount = 0;
Status = OcConfigurationInit (&Config, ConfigFileBuffer, ConfigFileSize, &ErrorCount);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Invalid config\n"));
return -1;
}
if (ErrorCount > 0) {
DEBUG ((DEBUG_ERROR, "Serialisation returns %u %a!\n", ErrorCount, ErrorCount > 1 ? "errors" : "error"));
}
PcdGet32 (PcdFixedDebugPrintErrorLevel) |= DEBUG_INFO;
PcdGet32 (PcdDebugPrintErrorLevel) |= DEBUG_INFO;
PcdGet8 (PcdDebugPropertyMask) |= DEBUG_PROPERTY_DEBUG_CODE_ENABLED;
mUse32BitKernel = FALSE;
ReservedInfoSize = PRELINK_INFO_RESERVE_SIZE;
ReservedExeSize = 0;
NumReservedKexts = 0;
//
// Process kexts to be injected.
//
for (Index = 0; Index < Config.Kernel.Add.Count; ++Index) {
Kext = Config.Kernel.Add.Values[Index];
Status = UserOcKernelLoadAndReserveKext (
Kext,
Index,
&Config,
mUse32BitKernel,
&ReservedExeSize,
&ReservedInfoSize,
&NumReservedKexts
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_WARN, "[FAIL] Kernel load and reserve - %r\n", Status));
FailedToProcess = TRUE;
return -1;
}
}
LinkedExpansion = KcGetSegmentFixupChainsSize (ReservedExeSize);
if (LinkedExpansion == 0) {
FailedToProcess = TRUE;
return -1;
}
Status = ReadAppleKernel (
&NilFileProtocol,
FALSE,
&Is32Bit,
&NewPrelinked,
&NewPrelinkedSize,
&AllocSize,
ReservedInfoSize + ReservedExeSize + LinkedExpansion,
Sha384
);
if (!EFI_ERROR (Status)) {
FreePool (mPrelinked);
mPrelinked = NewPrelinked;
mPrelinkedSize = NewPrelinkedSize;
DEBUG ((DEBUG_WARN, "[OK] Sha384 is %02X%02X%02X%02X\n", Sha384[0], Sha384[1], Sha384[2], Sha384[3]));
} else {
DEBUG ((DEBUG_WARN, "[FAIL] Kernel unpack failure - %r\n", Status));
FailedToProcess = TRUE;
return -1;
}
KernelVersion = OcKernelReadDarwinVersion (mPrelinked, mPrelinkedSize);
if (KernelVersion != 0) {
DEBUG ((DEBUG_WARN, "[OK] Got version %u\n", KernelVersion));
} else {
DEBUG ((DEBUG_WARN, "[FAIL] Failed to detect version\n"));
FailedToProcess = TRUE;
}
ZeroMem (&DummyCpuInfo, sizeof (DummyCpuInfo));
//
// Disable ProvideCurrentCpuInfo patch, as there is no CpuInfo available on userspace.
//
Config.Kernel.Quirks.ProvideCurrentCpuInfo = FALSE;
ASSERT (Config.Kernel.Quirks.ProvideCurrentCpuInfo == FALSE);
ZeroMem (Config.Kernel.Emulate.Cpuid1Data, sizeof (Config.Kernel.Emulate.Cpuid1Data));
Config.Kernel.Emulate.Cpuid1Data[0] = 0x000306A9;
ZeroMem (Config.Kernel.Emulate.Cpuid1Mask, sizeof (Config.Kernel.Emulate.Cpuid1Mask));
Config.Kernel.Emulate.Cpuid1Mask[0] = 0xFFFFFFFF;
ASSERT (Config.Kernel.Force.Count == 0);
//
// Apply patches to kernel itself, and then process prelinked.
//
OcKernelApplyPatches (
&Config,
&DummyCpuInfo,
KernelVersion,
FALSE,
CacheTypeNone,
NULL,
NewPrelinked,
NewPrelinkedSize
);
PrelinkedStatus = OcKernelProcessPrelinked (
&Config,
KernelVersion,
FALSE,
NewPrelinked,
&NewPrelinkedSize,
AllocSize,
LinkedExpansion,
ReservedExeSize
);
if (EFI_ERROR (PrelinkedStatus)) {
DEBUG ((DEBUG_WARN, "[FAIL] Kernel process - %r\n", PrelinkedStatus));
FailedToProcess = TRUE;
return -1;
}
DEBUG ((DEBUG_INFO, "OC: Prelinked status - %r\n", PrelinkedStatus));
UserWriteFile ("out.bin", NewPrelinked, NewPrelinkedSize);
FreePool (mPrelinked);
return 0;
}
int
main (
int argc,
char *argv[]
)
{
int code;
code = WrapMain (argc, argv);
if (FailedToProcess) {
code = -1;
}
return code;
}
/** @file
Copyright (C) 2022, PMheart. 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 <Library/DebugLib.h>
VOID
OcAppleImg4RegisterOverride (
IN CONST UINT8 *OriginalDigest,
IN CONST UINT8 *Image,
IN UINT32 ImageSize
)
{
ASSERT (FALSE);
}
......@@ -28,6 +28,7 @@ buildutil() {
"TestExt4Dxe"
"TestNtfsDxe"
"TestPeCoff"
"TestProcessKernel"
"TestRsaPreprocess"
"TestSmbios"
)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册