From 275c162c7c270499408ee4cbdd8f24b6d0240117 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 2 Feb 2024 22:19:08 +0100 Subject: Bootloader: Fix code that isn't supposed to be there, EFI wrapper improvements. Signed-off-by: Amlal El Mahrouss --- Private/EFIKit/Api.hxx | 16 +++-- Private/EFIKit/BootProtocol.hxx | 82 ---------------------- Private/EFIKit/EFI.hxx | 10 ++- Private/EFIKit/Handover.hxx | 82 ++++++++++++++++++++++ Private/KernelKit/PE.hpp | 20 +++--- Private/NewBoot/BootKit/Protocol.hxx | 2 +- Private/NewBoot/Source/HEL/AMD64/AMD64-BootKit.cxx | 28 +++++--- .../NewBoot/Source/HEL/AMD64/AMD64-Platform.cxx | 3 +- 8 files changed, 129 insertions(+), 114 deletions(-) delete mode 100644 Private/EFIKit/BootProtocol.hxx create mode 100644 Private/EFIKit/Handover.hxx diff --git a/Private/EFIKit/Api.hxx b/Private/EFIKit/Api.hxx index dea09425..031c38e7 100644 --- a/Private/EFIKit/Api.hxx +++ b/Private/EFIKit/Api.hxx @@ -12,8 +12,8 @@ #include -inline EfiSystemTable* ST = nullptr; -inline EfiBootServices* BS = nullptr; +inline EfiSystemTable *ST = nullptr; +inline EfiBootServices *BS = nullptr; namespace EFI { /** @@ -30,7 +30,7 @@ inline Void Stop() noexcept { @brief Exit EFI API to let the OS load correctly. Bascially frees everything we have in the EFI side. */ -inline void ExitBootServices(EfiSystemTable* SystemTable, UInt64 MapKey, +inline void ExitBootServices(EfiSystemTable *SystemTable, UInt64 MapKey, EfiHandlePtr ImageHandle) noexcept { if (!SystemTable) return; @@ -38,7 +38,7 @@ inline void ExitBootServices(EfiSystemTable* SystemTable, UInt64 MapKey, } } // namespace EFI -inline void KeInitEFI(EfiSystemTable* SystemTable) noexcept { +inline void KeInitEFI(EfiSystemTable *SystemTable) noexcept { if (!SystemTable) return; ST = SystemTable; @@ -48,8 +48,8 @@ inline void KeInitEFI(EfiSystemTable* SystemTable) noexcept { ST->ConOut->SetAttribute(SystemTable->ConOut, kEFIYellow); } -inline void KeRuntimeStop(const EfiCharType* ErrorCode, - const EfiCharType* Reason) noexcept { +inline void KeRuntimeStop(const EfiCharType *ErrorCode, + const EfiCharType *Reason) noexcept { ST->ConOut->OutputString(ST->ConOut, L"*** STOP ***\r\n"); ST->ConOut->OutputString(ST->ConOut, L"*** ErrorCode: "); @@ -69,7 +69,9 @@ enum { }; #ifdef __BOOTLOADER__ + #include -#endif // IF TARGET=BOOTLOADER + +#endif // ifdef __BOOTLOADER__ #endif /* ifndef __EFI_API__ */ diff --git a/Private/EFIKit/BootProtocol.hxx b/Private/EFIKit/BootProtocol.hxx deleted file mode 100644 index 1acfcc18..00000000 --- a/Private/EFIKit/BootProtocol.hxx +++ /dev/null @@ -1,82 +0,0 @@ -/* - * ======================================================== - * - * NewBoot - * Copyright Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -/** - * @file BootProtocol.hxx - * @author Amlal El Mahrouss (amlalelmahrouss@icloud.com) - * @brief Handover protocol. - * @version 0.1 - * @date 2024-02-02 - * - * @copyright Copyright (c) 2024, Mahrouss Logic - * - */ - -#pragma once - -#include - -/* useful macros */ - -#define kHandoverMagic 0xBAD55 - -#define kBaseHandoverStruct 0x80000000 -#define kHandoverStructSz sizeof(HEL::HandoverHeader) - -namespace HCore::HEL { -/** - @brief the kind of executable we're loading. -*/ -enum { - kTypeKernel = 100, - kTypeKernelDriver = 101, - kTypeRsrc = 102, - kTypeCount = 3, -}; - -/** - @brief The executable architecture. -*/ - -enum { - kArchAmd64 = 122, - kArchCount = 2, -}; - -/** -@brief The first struct that we read when inspecting The executable -it tells us more about it and IS format independent. -*/ -struct __attribute__((packed)) HandoverHeader final { - Int32 f_TargetMagic; - Int32 f_TargetType; - Int32 f_TargetArch; - UIntPtr f_TargetStartAddress; -}; - -struct HandoverInformationHeader { - HandoverHeader* f_Header; - voidPtr f_VirtualStart; - SizeT f_VirtualSize; - voidPtr f_PhysicalStart; - SizeT f_PhysicalSize; - Char f_FirmwareVendorName[32]; - SizeT f_FirmwareVendorLen; - voidPtr f_RsdPtr; - voidPtr f_SmBIOS; - voidPtr f_RTC; - voidPtr f_GOP; - voidPtr f_GOPSize; -}; - -/** - @brief Handover Jump Proc -*/ -typedef UInt64 (*HandoverProc)(HandoverInformationHeader* pHandover); -} // namespace HCore::HEL diff --git a/Private/EFIKit/EFI.hxx b/Private/EFIKit/EFI.hxx index 243211e9..f59917f2 100644 --- a/Private/EFIKit/EFI.hxx +++ b/Private/EFIKit/EFI.hxx @@ -59,8 +59,8 @@ typedef UInt64(EFI_API *EfiTextClear)(struct EfiSimpleTextOutputProtocol *This); typedef UInt64(EFI_API *EfiLoadFile)(EfiLoadFileProtocol *This, EfiFileDevicePathProtocol *FilePath, - Boolean BootPolicy, UInt32 **BufferSize, - VoidPtr *Buffer); + Boolean BootPolicy, UInt32 *BufferSize, + VoidPtr Buffer); typedef UInt64(EFI_API *EfiCopyMem)(VoidPtr DstBuf, VoidPtr SrcBuf, SizeT Length); @@ -472,4 +472,10 @@ enum { kEFICount = 6, }; +#define END_DEVICE_PATH_TYPE 0x7f +#define END_ENTIRE_DEVICE_PATH_SUBTYPE 0xFF +#define END_INSTANCE_DEVICE_PATH_SUBTYPE 0x01 + +#define kEfiOffsetOf(T, F) __builtin_offsetof(T, F) + #endif // __EFI__ diff --git a/Private/EFIKit/Handover.hxx b/Private/EFIKit/Handover.hxx new file mode 100644 index 00000000..07a3bc51 --- /dev/null +++ b/Private/EFIKit/Handover.hxx @@ -0,0 +1,82 @@ +/* + * ======================================================== + * + * NewBoot + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +/** + * @file Handover.hxx + * @author Amlal El Mahrouss (amlalelmahrouss@icloud.com) + * @brief Handover protocol. + * @version 0.1 + * @date 2024-02-02 + * + * @copyright Copyright (c) 2024, Mahrouss Logic + * + */ + +#pragma once + +#include + +/* useful macros */ + +#define kHandoverMagic 0xBAD55 + +#define kBaseHandoverStruct 0x80000000 +#define kHandoverStructSz sizeof(HEL::HandoverHeader) + +namespace HCore::HEL { +/** + @brief the kind of executable we're loading. +*/ +enum { + kTypeKernel = 100, + kTypeKernelDriver = 101, + kTypeRsrc = 102, + kTypeCount = 3, +}; + +/** + @brief The executable architecture. +*/ + +enum { + kArchAmd64 = 122, + kArchCount = 2, +}; + +/** +@brief The first struct that we read when inspecting The executable +it tells us more about it and IS format independent. +*/ +struct __attribute__((packed)) HandoverHeader final { + Int32 f_TargetMagic; + Int32 f_TargetType; + Int32 f_TargetArch; + UIntPtr f_TargetStartAddress; +}; + +struct HandoverInformationHeader { + HandoverHeader* f_Header; + voidPtr f_VirtualStart; + SizeT f_VirtualSize; + voidPtr f_PhysicalStart; + SizeT f_PhysicalSize; + Char f_FirmwareVendorName[32]; + SizeT f_FirmwareVendorLen; + voidPtr f_RsdPtr; + voidPtr f_SmBIOS; + voidPtr f_RTC; + voidPtr f_GOP; + voidPtr f_GOPSize; +}; + +/** + @brief Handover Jump Proc +*/ +typedef UInt64 (*HandoverProc)(HandoverInformationHeader* pHandover); +} // namespace HCore::HEL diff --git a/Private/KernelKit/PE.hpp b/Private/KernelKit/PE.hpp index 9c975108..88ee319a 100644 --- a/Private/KernelKit/PE.hpp +++ b/Private/KernelKit/PE.hpp @@ -26,7 +26,7 @@ typedef char CHAR; #define kPeMagic 0x00004550 -typedef struct PACKED ExecHeader final { +typedef struct ExecHeader final { U32 mMagic; // PE\0\0 or 0x00004550 U16 mMachine; U16 mNumberOfSections; @@ -35,12 +35,12 @@ typedef struct PACKED ExecHeader final { U32 mNumberOfSymbols; U16 mSizeOfOptionalHeader; U16 mCharacteristics; -} ExecHeader, *ExecHeaderPtr; +} PACKED ExecHeader, *ExecHeaderPtr; #define kMagPE32 0x010b #define kMagPE64 0x020b -typedef PACKED struct ExecOptionalHeader final { +typedef struct ExecOptionalHeader final { U16 mMagic; // 0x010b - PE32, 0x020b - PE32+ (64 bit) U8 mMajorLinkerVersion; U8 mMinorLinkerVersion; @@ -71,9 +71,9 @@ typedef PACKED struct ExecOptionalHeader final { U32 mSizeOfHeapCommit; U32 mLoaderFlags; U32 mNumberOfRvaAndSizes; -} ExecOptionalHeader, *ExecOptionalHeaderPtr; +} PACKED ExecOptionalHeader, *ExecOptionalHeaderPtr; -typedef PACKED struct ExecSectionHeader final { +typedef struct ExecSectionHeader final { CHAR mName[8]; U32 mVirtualSize; U32 mVirtualAddress; @@ -84,7 +84,7 @@ typedef PACKED struct ExecSectionHeader final { U16 mNumberOfRelocations; U16 mNumberOfLinenumbers; U32 mCharacteristics; -} ExecSectionHeader, *ExecSectionHeaderPtr; +} PACKED ExecSectionHeader, *ExecSectionHeaderPtr; enum kExecDataDirParams { kExecExport, @@ -92,7 +92,7 @@ enum kExecDataDirParams { kExecCnt, }; -typedef PACKED struct ExecExportDirectory { +typedef struct ExecExportDirectory { U32 mCharacteristics; U32 mTimeDateStamp; U16 mMajorVersion; @@ -104,9 +104,9 @@ typedef PACKED struct ExecExportDirectory { U32 mAddressOfFunctions; // export table rva U32 mAddressOfNames; U32 mAddressOfNameOrdinal; // ordinal table rva -} ExecExportDirectory, *ExecExportDirectoryPtr; +} PACKED ExecExportDirectory, *ExecExportDirectoryPtr; -typedef PACKED struct ExecImportDirectory { +typedef struct ExecImportDirectory { union { U32 mCharacteristics; U32 mOriginalFirstThunk; @@ -115,6 +115,6 @@ typedef PACKED struct ExecImportDirectory { U32 mForwarderChain; U32 mNameRva; U32 mThunkTableRva; -} ExecImportDirectory, *ExecImportDirectoryPtr; +} PACKED ExecImportDirectory, *ExecImportDirectoryPtr; #endif /* ifndef __PE__ */ diff --git a/Private/NewBoot/BootKit/Protocol.hxx b/Private/NewBoot/BootKit/Protocol.hxx index b2725794..055858a8 100644 --- a/Private/NewBoot/BootKit/Protocol.hxx +++ b/Private/NewBoot/BootKit/Protocol.hxx @@ -9,5 +9,5 @@ #pragma once -#include #include +#include diff --git a/Private/NewBoot/Source/HEL/AMD64/AMD64-BootKit.cxx b/Private/NewBoot/Source/HEL/AMD64/AMD64-BootKit.cxx index 8ca7737d..328e7bc9 100644 --- a/Private/NewBoot/Source/HEL/AMD64/AMD64-BootKit.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/AMD64-BootKit.cxx @@ -11,9 +11,6 @@ #include #include -#include "EFIKit/EFI.hxx" -#include "NewKit/Macros.hpp" - /// bugs 0 ///////////////////////////////////////////////////////////////////////////////////////////////////////// @@ -113,7 +110,7 @@ HCore::VoidPtr BFileReader::ReadAll(SizeT &size) { EfiHandlePtr handleFile = nullptr; EfiLoadFileProtocol *loadFile = nullptr; - EfiGUID loadFileGUID = EfiGUID(EFI_LOAD_FILE2_PROTOCOL_GUID); + EfiGUID loadFileGUID = EfiGUID(EFI_LOAD_FILE_PROTOCOL_GUID); BS->LocateProtocol(&loadFileGUID, nullptr, (VoidPtr *)&loadFile); @@ -128,21 +125,30 @@ HCore::VoidPtr BFileReader::ReadAll(SizeT &size) { BS->AllocatePool(EfiLoaderCode, sizeof(UInt32), (VoidPtr *)&bufSz); *bufSz = KIB(324); + if (!bufSz) { + return nullptr; + } + BS->AllocatePool(EfiLoaderCode, *bufSz, &buf); - if (!buf) return nullptr; + if (!buf) { + BS->FreePool(bufSz); + bufSz = nullptr; + + return nullptr; + } EfiFileDevicePathProtocol filePath{0}; - filePath.Proto.Length[0] = sizeof(EfiDevicePathProtocol); - filePath.Proto.Length[1] = BStrLen(mPath); + filePath.Proto.Length[0] = sizeof(EfiDevicePathProtocol) + BStrLen(mPath); + filePath.Proto.Length[1] = 0; filePath.Proto.Type = kEFIMediaDevicePath; filePath.Proto.SubType = kEFIMediaDevicePath; // from all drives. BCopyMem(filePath.Path, mPath, BStrLen(mPath)); - auto err = loadFile->LoadFile(loadFile, &filePath, false, &bufSz, &buf); + auto err = loadFile->LoadFile(loadFile, &filePath, true, bufSz, buf); size = *bufSz; @@ -158,7 +164,7 @@ HCore::VoidPtr BFileReader::ReadAll(SizeT &size) { case 2: { writer.WriteString(L"HCoreLdr: Error: ") .WriteString(mPath) - .WriteString(L", Code: Invalid-Parameter") + .WriteString(L", EFI-Code: Invalid-Parameter") .WriteString(L"\r\n"); break; @@ -166,7 +172,7 @@ HCore::VoidPtr BFileReader::ReadAll(SizeT &size) { case 14: { writer.WriteString(L"HCoreLdr: Error: ") .WriteString(mPath) - .WriteString(L" , EFI-Code: Not-Found") + .WriteString(L", EFI-Code: Not-Found") .WriteString(L"\r\n"); break; @@ -174,7 +180,7 @@ HCore::VoidPtr BFileReader::ReadAll(SizeT &size) { default: { writer.WriteString(L"HCoreLdr: Error: ") .WriteString(mPath) - .WriteString(L" , EFI-Code: Unknown-Error") + .WriteString(L", EFI-Code: Unknown-Error") .WriteString(L"\r\n"); break; diff --git a/Private/NewBoot/Source/HEL/AMD64/AMD64-Platform.cxx b/Private/NewBoot/Source/HEL/AMD64/AMD64-Platform.cxx index 663d4bcb..20dd3125 100644 --- a/Private/NewBoot/Source/HEL/AMD64/AMD64-Platform.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/AMD64-Platform.cxx @@ -14,6 +14,7 @@ * */ +#include #include #include @@ -27,6 +28,6 @@ extern "C" void rt_cld() { asm volatile("cld"); } extern "C" void rt_std() { asm volatile("std"); } -/// @brief Stack check +/// @brief Stack Checker, leave empty. extern "C" void ___chkstk_ms(void) {} -- cgit v1.2.3