summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-02-02 17:32:49 +0100
committerAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-02-02 17:33:32 +0100
commit5613b2627a4f9e74296ec2dcfb9079516d4f41e0 (patch)
tree76431c66bed92b6441f48f9cf338de9d4e6cd80d
parente1e6032cfa5d0c55424d5badd65bfd186a44ab93 (diff)
Bootloader: Working on BFileReader::ReadAll() implementation.
Signed-off-by: Amlal El Mahrouss <amlalelmahrouss@icloud.com>
-rw-r--r--Private/KernelKit/PE.hpp15
-rw-r--r--Private/NewBoot/BootKit/BootKit.hxx5
-rw-r--r--Private/NewBoot/Source/HEL/AMD64/AMD64-BootKit.cxx45
-rw-r--r--Private/NewBoot/Source/HEL/AMD64/AMD64-Main.cxx17
4 files changed, 55 insertions, 27 deletions
diff --git a/Private/KernelKit/PE.hpp b/Private/KernelKit/PE.hpp
index 5d8ac9f8..9c975108 100644
--- a/Private/KernelKit/PE.hpp
+++ b/Private/KernelKit/PE.hpp
@@ -21,9 +21,12 @@ typedef HCore::UInt16 U16;
typedef HCore::UInt8 U8;
typedef char CHAR;
+#define kMagMz0 'M'
+#define kMagMz1 'Z'
+
#define kPeMagic 0x00004550
-struct ExecHeader final {
+typedef struct PACKED ExecHeader final {
U32 mMagic; // PE\0\0 or 0x00004550
U16 mMachine;
U16 mNumberOfSections;
@@ -32,12 +35,12 @@ struct ExecHeader final {
U32 mNumberOfSymbols;
U16 mSizeOfOptionalHeader;
U16 mCharacteristics;
-};
+} ExecHeader, *ExecHeaderPtr;
#define kMagPE32 0x010b
#define kMagPE64 0x020b
-typedef struct ExecOptionalHeader final {
+typedef PACKED struct ExecOptionalHeader final {
U16 mMagic; // 0x010b - PE32, 0x020b - PE32+ (64 bit)
U8 mMajorLinkerVersion;
U8 mMinorLinkerVersion;
@@ -70,7 +73,7 @@ typedef struct ExecOptionalHeader final {
U32 mNumberOfRvaAndSizes;
} ExecOptionalHeader, *ExecOptionalHeaderPtr;
-typedef struct ExecSectionHeader final {
+typedef PACKED struct ExecSectionHeader final {
CHAR mName[8];
U32 mVirtualSize;
U32 mVirtualAddress;
@@ -89,7 +92,7 @@ enum kExecDataDirParams {
kExecCnt,
};
-typedef struct ExecExportDirectory {
+typedef PACKED struct ExecExportDirectory {
U32 mCharacteristics;
U32 mTimeDateStamp;
U16 mMajorVersion;
@@ -103,7 +106,7 @@ typedef struct ExecExportDirectory {
U32 mAddressOfNameOrdinal; // ordinal table rva
} ExecExportDirectory, *ExecExportDirectoryPtr;
-typedef struct ExecImportDirectory {
+typedef PACKED struct ExecImportDirectory {
union {
U32 mCharacteristics;
U32 mOriginalFirstThunk;
diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx
index bbe2332e..50b14928 100644
--- a/Private/NewBoot/BootKit/BootKit.hxx
+++ b/Private/NewBoot/BootKit/BootKit.hxx
@@ -20,8 +20,8 @@
using namespace HCore;
-typedef VoidPtr PEFImagePtr;
-typedef VoidPtr PEImagePtr;
+typedef Char *PEFImagePtr;
+typedef Char *PEImagePtr;
enum {
kSegmentCode = 2,
@@ -84,6 +84,7 @@ class BFileReader final {
private:
Int32 mErrorCode{kOperationOkay};
CharacterType mPath[kPathLen];
+ VoidPtr mHandle{nullptr};
};
/***********************************************************************************/
diff --git a/Private/NewBoot/Source/HEL/AMD64/AMD64-BootKit.cxx b/Private/NewBoot/Source/HEL/AMD64/AMD64-BootKit.cxx
index b45e661f..1533194b 100644
--- a/Private/NewBoot/Source/HEL/AMD64/AMD64-BootKit.cxx
+++ b/Private/NewBoot/Source/HEL/AMD64/AMD64-BootKit.cxx
@@ -122,39 +122,58 @@ HCore::VoidPtr BFileReader::ReadAll(SizeT &size) {
.WriteString(mPath)
.WriteString(L"\r\n");
- UInt32 bufSz = KIB(350);
+ UInt32 *bufSz = nullptr;
VoidPtr buf = nullptr;
- BS->AllocatePool(EfiLoaderCode, bufSz, &buf);
+ BS->AllocatePool(EfiLoaderCode, sizeof(UInt32), (VoidPtr *)&bufSz);
+ *bufSz = 0;
+
+ BS->AllocatePool(EfiLoaderCode, *bufSz, &buf);
if (!buf) return nullptr;
EfiFileDevicePathProtocol filePath{0};
- filePath.Proto.Length[0] = (sizeof(EfiDevicePathProtocol));
- filePath.Proto.Length[1] = (sizeof(EfiDevicePathProtocol) + kPathLen) >> 8;
filePath.Proto.Type = kEFIMediaDevicePath;
filePath.Proto.SubType = kEFIMediaDevicePath; // from all drives.
BCopyMem(filePath.Path, mPath, kPathLen);
- auto err = loadFile->LoadFile(loadFile, &filePath, false, (UInt32 *)&bufSz,
- (VoidPtr *)&buf);
+ auto err = loadFile->LoadFile(loadFile, &filePath, true, bufSz, buf);
- size = bufSz;
+ size = *bufSz;
- if (buf) {
+ if (err == kEfiOk) {
writer.WriteString(L"HCoreLdr: Loaded: ")
.WriteString(mPath)
.WriteString(L"\r\n");
} else {
- writer.WriteString(L"HCoreLdr: Error: ")
- .WriteString(mPath)
- .WriteString(L" , EFI-Code: ")
- .WriteCharacter(err + 48)
- .WriteString(L"\r\n");
+ BS->FreePool(buf);
+ buf = nullptr;
+
+ switch (err) {
+ case 2: {
+ writer.WriteString(L"HCoreLdr: Error: ")
+ .WriteString(mPath)
+ .WriteString(L", Code: Invalid-Parameter")
+ .WriteString(L"\r\n");
+
+ break;
+ }
+ case 14: {
+ writer.WriteString(L"HCoreLdr: Error: ")
+ .WriteString(mPath)
+ .WriteString(L" , EFI-Code: Not-Found")
+ .WriteString(L"\r\n");
+
+ break;
+ }
+ }
}
+ BS->FreePool(bufSz);
+ bufSz = nullptr;
+
return buf;
}
diff --git a/Private/NewBoot/Source/HEL/AMD64/AMD64-Main.cxx b/Private/NewBoot/Source/HEL/AMD64/AMD64-Main.cxx
index 7d179e42..d7583873 100644
--- a/Private/NewBoot/Source/HEL/AMD64/AMD64-Main.cxx
+++ b/Private/NewBoot/Source/HEL/AMD64/AMD64-Main.cxx
@@ -7,11 +7,11 @@
* ========================================================
*/
-#include "EFIKit/EFI.hxx"
#define __BOOTLOADER__ 1
#include <BootKit/BootKit.hxx>
#include <EFIKit/Api.hxx>
+#include <KernelKit/PE.hpp>
// don't remove EfiGUID, it will call initializer_list!
@@ -27,17 +27,22 @@ EFI_EXTERN_C int EfiMain(EfiHandlePtr ImageHandle,
UInt64 mapKey = 0;
- BFileReader reader(L"EFI\\BOOT\\HCoreKrnl.exe");
+ BFileReader reader(L"HCoreKrnl.exe");
SizeT sz = 0UL;
- auto blob = reader.ReadAll(sz);
+ PEImagePtr blob = (PEImagePtr)reader.ReadAll(sz);
if (!blob || sz < 1)
- KeRuntimeStop(L"HCoreLdr_NoSuchKernel",
- L"Couldn't find HCoreKrnl.exe! Aborting...");
+ KeRuntimeStop(L"HCoreLdr_NoSuchKernel", L"Couldn't find HCoreKrnl.exe!");
- writer.WriteString(L"HCoreLdr: Running HCoreKrnl.exe...\r\n");
+ ExecHeaderPtr headerPtr = (ExecHeaderPtr)blob;
+
+ if (blob[0] == kMagMz0 && blob[1] == kMagMz1) {
+ writer.WriteString(L"HCoreLdr: Running HCoreKrnl.exe...\r\n");
+ } else {
+ KeRuntimeStop(L"HCoreLdr_NotPE", L"Not a PE file! Aborting...");
+ }
EFI::ExitBootServices(SystemTable, mapKey, ImageHandle);
EFI::Stop();