summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-02-13 20:29:28 +0100
committerAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-02-13 20:29:28 +0100
commit9b2686756dee0e55b7860e7ea18d4bf92a365065 (patch)
treecbfbfdf508d2e1634c4f1a5ae841600d589bffcf
parent544d0cadfc371bcfb54d9f7ec15464bc6a79af21 (diff)
HCR-18: First commit, bringing HCoreKrnl.exe into memory.
Signed-off-by: Amlal El Mahrouss <amlalelmahrouss@icloud.com>
-rw-r--r--Private/EFIKit/Api.hxx7
-rw-r--r--Private/EFIKit/EFI.hxx9
-rw-r--r--Private/EFIKit/Handover.hxx8
-rw-r--r--Private/KernelKit/MSDOS.hpp2
-rw-r--r--Private/NewBoot/Source/RuntimeMain.cxx89
-rw-r--r--Private/NewBoot/Source/makefile2
6 files changed, 102 insertions, 15 deletions
diff --git a/Private/EFIKit/Api.hxx b/Private/EFIKit/Api.hxx
index 7338235a..e91e983d 100644
--- a/Private/EFIKit/Api.hxx
+++ b/Private/EFIKit/Api.hxx
@@ -11,6 +11,7 @@
#define __EFI_API__
#include <EFIKit/EFI.hxx>
+#include <EFIKit/Handover.hxx>
#include <KernelKit/MSDOS.hpp>
#include <KernelKit/PE.hpp>
@@ -27,7 +28,9 @@ 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(UInt64 MapKey, EfiHandlePtr ImageHandle) noexcept {
+inline void ExitBootServices(UInt64 MapKey, EfiHandlePtr ImageHandle,
+ HEL::HandoverProc &handOver,
+ HEL::HandoverInformationHeader *hdr) noexcept {
if (!ST) return;
/// The MapKey may be invalid.
@@ -35,6 +38,8 @@ inline void ExitBootServices(UInt64 MapKey, EfiHandlePtr ImageHandle) noexcept {
if (ST->BootServices->ExitBootServices(ImageHandle, MapKey) != kEfiOk) {
EFI::Stop();
}
+
+ return handOver(hdr);
}
enum {
diff --git a/Private/EFIKit/EFI.hxx b/Private/EFIKit/EFI.hxx
index dfaf68eb..3358da44 100644
--- a/Private/EFIKit/EFI.hxx
+++ b/Private/EFIKit/EFI.hxx
@@ -416,6 +416,11 @@ typedef UInt64(EFI_API *EfiAllocatePages)(EfiAllocateType AllocType,
typedef UInt64(EFI_API *EfiFreePages)(EfiPhysicalAddress *Memory, UInt32 Pages);
+typedef UInt64(EFI_API *EfiGetMemoryMap)(UInt64 *MapSize,
+ EfiMemoryDescriptor *DescPtr,
+ UInt64 *MapKey, UInt64 *DescSize,
+ UInt64 *DescVersion);
+
/**
* @brief GUID type, something you can also find in CFKit.
*/
@@ -459,7 +464,7 @@ typedef struct EfiBootServices {
UIntPtr RestoreTPL;
EfiAllocatePages AllocatePages;
EfiFreePages FreePages;
- UIntPtr GetMemoryMap;
+ EfiGetMemoryMap GetMemoryMap;
EfiAllocatePool AllocatePool;
EfiFreePool FreePool;
UIntPtr CreateEvent;
@@ -720,4 +725,6 @@ struct EfiFileInfo final {
#define EFI_FILE_PROTOCOL_REVISION2 0x00020000
#define EFI_FILE_PROTOCOL_LATEST_REVISION EFI_FILE_PROTOCOL_REVISION2
+#define EFI_EXTRA_DESCRIPTOR_SIZE 8
+
#endif // ifndef __EFI__
diff --git a/Private/EFIKit/Handover.hxx b/Private/EFIKit/Handover.hxx
index eb79e934..840af2ec 100644
--- a/Private/EFIKit/Handover.hxx
+++ b/Private/EFIKit/Handover.hxx
@@ -61,22 +61,22 @@ typedef struct HandoverHeader final {
} __attribute__((packed)) HandoverHeader, *HandoverHeaderPtr;
struct HandoverInformationHeader {
- HandoverHeaderPtr f_Header;
+ HandoverHeader f_Header;
voidPtr f_VirtualStart;
SizeT f_VirtualSize;
voidPtr f_PhysicalStart;
SizeT f_PhysicalSize;
- Char f_FirmwareVendorName[32];
+ WideChar f_FirmwareVendorName[32];
SizeT f_FirmwareVendorLen;
voidPtr f_RsdPtr;
voidPtr f_SmBIOS;
voidPtr f_RTC;
voidPtr f_GOP;
- voidPtr f_GOPSize;
+ SizeT f_GOPSize;
};
/**
@brief Handover Jump Proc
*/
-typedef UInt64 (*HandoverProc)(HandoverInformationHeader* pHandover);
+typedef void (*HandoverProc)(HandoverInformationHeader* pHandover);
} // namespace HCore::HEL
diff --git a/Private/KernelKit/MSDOS.hpp b/Private/KernelKit/MSDOS.hpp
index 2276f3cb..1adf6082 100644
--- a/Private/KernelKit/MSDOS.hpp
+++ b/Private/KernelKit/MSDOS.hpp
@@ -48,7 +48,7 @@ namespace HCore {
inline auto rt_find_exec_header(DosHeaderPtr ptrDos) -> VoidPtr {
if (!ptrDos) return nullptr;
if (ptrDos->eMagic[0] != kMagMz0) return nullptr;
- if (ptrDos->eMagic[0] != kMagMz1) return nullptr;
+ if (ptrDos->eMagic[1] != kMagMz1) return nullptr;
return (VoidPtr)(&ptrDos->eLfanew + 1);
}
diff --git a/Private/NewBoot/Source/RuntimeMain.cxx b/Private/NewBoot/Source/RuntimeMain.cxx
index 3c55c06b..cf1051fd 100644
--- a/Private/NewBoot/Source/RuntimeMain.cxx
+++ b/Private/NewBoot/Source/RuntimeMain.cxx
@@ -26,19 +26,17 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle,
#ifndef __DEBUG__
- writer.WriteString(
- L"MahroussLogic (R) HCore Version 1.00 (Release Channel)\r\n");
+ writer.WriteString(L"HCoreLdr: Version 1.00 (Release Channel)\r\n");
#else
- writer.WriteString(
- L"MahroussLogic (R) HCore Version 1.00 (Insiders Channel)\r\n");
+ writer.WriteString(L"HCoreLdr: Version 1.00 (Insiders Channel)\r\n");
#endif
const char strDate[] = __DATE__;
- writer.WriteString(L"Build Date: ");
+ writer.WriteString(L"HCoreLdr: Build-Date");
for (auto& ch : strDate) {
writer.WriteCharacter(ch);
@@ -66,9 +64,84 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle,
if (ptrHdr && ptrHdr->mMachine == EFI::Platform() &&
ptrHdr->mMagic == kPeMagic) {
if (ptrHdr->mNumberOfSections > 1) {
+ ExecOptionalHeaderPtr optHdr = reinterpret_cast<ExecOptionalHeaderPtr>(
+ ptrHdr + sizeof(ExecHeader));
+
+ UInt64 baseCode = optHdr->mBaseOfCode;
+ UInt64 codeSz = optHdr->mSizeOfCode;
+ VoidPtr codePtr = nullptr;
+
+ img.File()->SetPosition(img.File(), &baseCode);
+
+ BS->AllocatePool(EfiLoaderCode, codeSz, &codePtr);
+
+ writer.WriteString(L"HCoreLdr: Init [0/2]...\r\n");
+
+ if (!codePtr) {
+ EFI::RaiseHardError(L"HCoreLdr-BadAlloc", L"Bad alloc!");
+ }
+
+ img.File()->Read(img.File(), &codeSz, codePtr);
+
+ /// GET DATA
+
+ UInt64 baseData = optHdr->mBaseOfData;
+ UInt64 sizeofData =
+ optHdr->mSizeOfInitializedData + optHdr->mSizeOfUninitializedData;
+ VoidPtr dataPtr = nullptr;
+
+ BS->AllocatePool(EfiLoaderCode, sizeofData, &dataPtr);
+
+ if (!dataPtr) {
+ EFI::RaiseHardError(L"HCoreLdr-BadAlloc", L"Bad alloc!");
+ }
+
+ writer.WriteString(L"HCoreLdr: Init [1/2]...\r\n");
+
+ img.File()->SetPosition(img.File(), &baseData);
+
+ img.File()->Read(img.File(), &sizeofData, dataPtr);
+
UInt64 MapKey = 0;
+ UInt64 Size = 0;
+ EfiMemoryDescriptor* Descriptor = nullptr;
+ UInt64 SzDesc = 0;
+ UInt64 RevDesc = 0;
+
+ BS->AllocatePool(EfiLoaderData, sizeof(EfiMemoryDescriptor),
+ (VoidPtr*)&Descriptor);
+
+ if (BS->GetMemoryMap(&Size, Descriptor, &MapKey, &SzDesc, &RevDesc) !=
+ kEfiOk) {
+ EFI::RaiseHardError(L"HCoreLdr: Bad-Call",
+ L"Bad call! (GetMemoryMap)");
+ }
+
+ writer.WriteString(L"HCoreLdr: Init [2/2]...\r\n");
+
+ HEL::HandoverInformationHeader* handoverHdrPtr = nullptr;
+
+ BS->AllocatePool(EfiLoaderData, sizeof(HEL::HandoverInformationHeader),
+ (VoidPtr*)&handoverHdrPtr);
+
+ HEL::HandoverProc fn = reinterpret_cast<HEL::HandoverProc>(
+ (UIntPtr)optHdr->mAddressOfEntryPoint);
+
+ handoverHdrPtr->f_GOP = (voidPtr)kGop->Mode->FrameBufferBase;
+ handoverHdrPtr->f_GOPSize = kGop->Mode->FrameBufferSize;
+
+ handoverHdrPtr->f_PhysicalStart = (voidPtr)Descriptor->PhysicalStart;
+ handoverHdrPtr->f_PhysicalSize = Descriptor->NumberOfPages;
+ handoverHdrPtr->f_VirtualStart = (voidPtr)Descriptor->VirtualStart;
+ handoverHdrPtr->f_VirtualSize = 0; /* not known */
+ handoverHdrPtr->f_FirmwareVendorLen =
+ BStrLen(SystemTable->FirmwareVendor);
+
+ BCopyMem(handoverHdrPtr->f_FirmwareVendorName,
+ SystemTable->FirmwareVendor,
+ handoverHdrPtr->f_FirmwareVendorLen);
- EFI::ExitBootServices(MapKey, ImageHandle);
+ EFI::ExitBootServices(MapKey, ImageHandle, fn, handoverHdrPtr);
// Launch PE app.
@@ -78,9 +151,11 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle,
} else {
writer.WriteString(L"HCoreLdr: Error-Code: HLDR-0001\r\n");
}
+ } else {
+ writer.WriteString(L"HCoreLdr: Error-Code: HLDR-0002\r\n");
}
} else {
- writer.WriteString(L"HCoreLdr: Error-Code: HLDR-0002\r\n");
+ writer.WriteString(L"HCoreLdr: Error-Code: HLDR-0003\r\n");
}
EFI::Stop();
diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile
index fab7453d..d17f9671 100644
--- a/Private/NewBoot/Source/makefile
+++ b/Private/NewBoot/Source/makefile
@@ -27,7 +27,7 @@ bootloader-amd64:
.PHONY: run-efi-amd64
run-efi-amd64:
wget https://retrage.github.io/edk2-nightly/bin/DEBUGX64_OVMF.fd -O OVMF.fd
- qemu-system-x86_64 -net none -smp 2 -m 4G -M q35 -bios OVMF.fd -drive file=fat:rw:CDROM,index=1,format=raw -serial stdio
+ qemu-system-x86_64 -net none -smp 2 -m 4G -M q35 -bios OVMF.fd -drive file=fat:rw:CDROM,index=1,format=raw -d int
.PHONY: clean
clean: