summaryrefslogtreecommitdiffhomepage
path: root/Private/NewBoot
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-03-01 09:37:23 +0100
committerAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-03-01 09:37:23 +0100
commit374d341bf0216294b58f0198dbe4ba986464563f (patch)
treead9fb1a0b02f40f566f09d36531060780457d249 /Private/NewBoot
parentc3e8cddf8c689807097f6f956d45e3ba96afec20 (diff)
:boom: See below.
HCoreLdr: Start effort to load HCoreKrnl in case of a non-installer scenario. HCoreKrnl: Start working on disk abstraction (AHCI) and rework PE format header file. Signed-off-by: Amlal El Mahrouss <amlalelmahrouss@icloud.com>
Diffstat (limited to 'Private/NewBoot')
-rw-r--r--Private/NewBoot/BootKit/BootKit.hxx2
-rw-r--r--Private/NewBoot/CxxRuntime/unwind.cxx4
-rw-r--r--Private/NewBoot/Source/BootMain.cxx122
-rw-r--r--Private/NewBoot/Source/FileReader.cxx9
-rw-r--r--Private/NewBoot/Source/TextWriter.cxx23
-rw-r--r--Private/NewBoot/Source/makefile2
6 files changed, 133 insertions, 29 deletions
diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx
index e9b52bf2..fa173f25 100644
--- a/Private/NewBoot/BootKit/BootKit.hxx
+++ b/Private/NewBoot/BootKit/BootKit.hxx
@@ -42,8 +42,10 @@ typedef WideChar CharacterType;
* Writes to UEFI StdOut.
*/
class BTextWriter final {
+ BTextWriter &_Write(const Long &num);
public:
BTextWriter &Write(const Long &num);
+ BTextWriter &Write(const UChar *str);
BTextWriter &Write(const CharacterType *str);
BTextWriter &WriteCharacter(CharacterType c);
diff --git a/Private/NewBoot/CxxRuntime/unwind.cxx b/Private/NewBoot/CxxRuntime/unwind.cxx
index f679bdd5..4f8807cd 100644
--- a/Private/NewBoot/CxxRuntime/unwind.cxx
+++ b/Private/NewBoot/CxxRuntime/unwind.cxx
@@ -1,7 +1,7 @@
namespace cxxkit {
///! @brief C++ ABI unwinding
-///! finis array (r1)
-///! n of finis (r2)
+///! Fini array (r1)
+///! Numbers of Fini (r2)
extern "C" void __unwind(void (**finis)(void), int cnt) {
for (int i = 0; i < cnt; ++i) (finis[i])();
}
diff --git a/Private/NewBoot/Source/BootMain.cxx b/Private/NewBoot/Source/BootMain.cxx
index a9f3babd..53a217b6 100644
--- a/Private/NewBoot/Source/BootMain.cxx
+++ b/Private/NewBoot/Source/BootMain.cxx
@@ -10,7 +10,6 @@
#define __BOOTLOADER__ 1
#include <BootKit/BootKit.hxx>
-#include <HALKit/AMD64/ACPI/ACPI.hpp>
#include <KernelKit/MSDOS.hpp>
#include <KernelKit/PE.hpp>
#include <NewKit/Ref.hpp>
@@ -21,11 +20,17 @@
#error This CPU is unknown.
#endif // ifdef __x86_64__
+#ifndef kBootKrnlSections
+#error Please provide the amount of sections the kernel has.
+#endif // !kBootKrnlSections
+
#define kBootReadSize \
(sizeof(DosHeader) + sizeof(ExecHeader) + sizeof(ExecOptionalHeader))
EXTERN_C void Main(HEL::HandoverInformationHeader* HIH);
+typedef void (*bt_main_type)(HEL::HandoverInformationHeader* HIH);
+
EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle,
EfiSystemTable* SystemTable) {
InitEFI(SystemTable);
@@ -47,13 +52,13 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle,
.Write(SystemTable->FirmwareVendor)
.Write(L"\r\n");
- BFileReader img(L"HCOREKRNL.EXE", ImageHandle);
+ BFileReader kernelImg(L"HCOREKRNL.EXE", ImageHandle);
- img.Size(kBootReadSize);
- img.ReadAll();
+ kernelImg.Size(kBootReadSize);
+ kernelImg.ReadAll();
- if (img.Error() == BFileReader::kOperationOkay) {
- BlobType blob = (BlobType)img.Blob();
+ if (kernelImg.Error() == BFileReader::kOperationOkay) {
+ BlobType blob = (BlobType)kernelImg.Blob();
ExecHeaderPtr ptrHdr = reinterpret_cast<ExecHeaderPtr>(
HCore::rt_find_exec_header(reinterpret_cast<DosHeaderPtr>(blob)));
@@ -61,9 +66,78 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle,
if (ptrHdr && ptrHdr->mMachine == EFI::Platform() &&
ptrHdr->mMagic == kPeMagic) {
/// sections must be at least 3.
- if (ptrHdr->mNumberOfSections >= 3) {
+ if (ptrHdr->mNumberOfSections == kBootKrnlSections) {
ExecOptionalHeaderPtr optHdr = reinterpret_cast<ExecOptionalHeaderPtr>(
- ptrHdr + sizeof(ExecHeader));
+ (UIntPtr)&ptrHdr->mCharacteristics + 1);
+
+ BFileReader systemIni(L"KERNEL.CFG", ImageHandle);
+
+ systemIni.Size(1);
+ systemIni.ReadAll();
+
+ bt_main_type kernelMain =
+ (bt_main_type)(UIntPtr)optHdr->mAddressOfEntryPoint;
+
+ SizeT heapSize = optHdr->mSizeOfHeapReserve;
+ SizeT stackSize = optHdr->mSizeOfStackReserve;
+
+ UInt64 posSeek = 0;
+
+ /****
+ *
+ * LOAD KERNEL CODE
+ *
+ *
+ *
+ *
+ *
+ *
+ *
+ * LOAD KERNEL CODE
+ */
+
+ kernelImg.File()->SetPosition(kernelImg.File(), &posSeek);
+ kernelImg.Size(kBootReadSize +
+ ptrHdr->mNumberOfSections * sizeof(ExecSectionHeader));
+
+ kernelImg.ReadAll();
+
+ writer.Write(L"HCoreLdr: Number of sections: ")
+ .Write(ptrHdr->mNumberOfSections)
+ .Write(L"\r\n");
+ writer.Write(L"HCoreLdr: Size of sections: ")
+ .Write(ptrHdr->mNumberOfSections * sizeof(ExecSectionHeader))
+ .Write(L"\r\n");
+
+ volatile ALIGN(kPTEAlign) ExecSectionHeader* blobKrnl =
+ (ExecSectionHeader*)(reinterpret_cast<DosHeaderPtr>(blob) +
+ reinterpret_cast<DosHeaderPtr>(blob)->eLfanew +
+ ptrHdr->mSizeOfOptionalHeader +
+ (sizeof(ExecHeader) +
+ sizeof(ExecOptionalHeader) + sizeof(U32)));
+
+ while (blobKrnl->mCharacteristics != 0x00000020) {
+ blobKrnl = blobKrnl + sizeof(ExecSectionHeader);
+ }
+
+ writer.Write(L"HCoreLdr: Exec Timestamp: ")
+ .Write(ptrHdr->mTimeDateStamp)
+ .Write(L"\r\n");
+
+ for (size_t i = 0; i < ptrHdr->mNumberOfSections; i++) {
+ writer.Write(L"HCoreLdr: Virtual-Size: ")
+ .Write(blobKrnl[i].mVirtualSize)
+ .Write(L"\r\n");
+ writer.Write(L"HCoreLdr: Virtual-Address: ")
+ .Write(blobKrnl[i].mVirtualAddress)
+ .Write(L"\r\n");
+ writer.Write(L"HCoreLdr: Raw-Address: ")
+ .Write(blobKrnl[i].mPointerToRawData)
+ .Write(L"\r\n");
+ writer.Write(L"HCoreLdr: Raw-Size: ")
+ .Write(blobKrnl[i].mSizeOfRawData)
+ .Write(L"\r\n");
+ }
UInt32 MapKey = 0;
UInt32* Size;
@@ -78,6 +152,12 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle,
L"The bootloader ran out of memory! Please check your specs.");
}
+ /****
+ *
+ * LOAD KERNEL CODE
+ *
+ */
+
*Size = sizeof(EfiMemoryDescriptor);
if (BS->AllocatePool(EfiLoaderData, sizeof(EfiMemoryDescriptor),
@@ -87,6 +167,12 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle,
L"The bootloader ran out of memory! Please check your specs.");
}
+ /****
+ *
+ * GET MEMORY MAP OF COMPUTER.
+ *
+ */
+
if (BS->GetMemoryMap(Size, Descriptor, &MapKey, &SzDesc, &RevDesc) !=
kEfiOk) {
EFI::RaiseHardError(
@@ -94,6 +180,10 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle,
L"GetMemoryMap returned a value which isn't kEfiOk!");
}
+#ifndef __DEBUG__
+ ST->ConOut->ClearScreen(ST->ConOut);
+#endif
+
HEL::HandoverInformationHeader* handoverHdrPtr = nullptr;
BS->AllocatePool(EfiLoaderData, sizeof(HEL::HandoverInformationHeader),
@@ -124,30 +214,24 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle,
SystemTable->FirmwareVendor,
handoverHdrPtr->f_FirmwareVendorLen);
- BFileReader systemIni(L"SYSTEM.INI", ImageHandle);
-
- systemIni.Size(1);
- systemIni.ReadAll();
-
- ST->ConOut->ClearScreen(ST->ConOut);
-
EFI::ExitBootServices(MapKey, ImageHandle);
bool isIniNotFound = (systemIni.Blob() == nullptr);
if (isIniNotFound) {
- handoverHdrPtr->f_Magic = 0x55DDFF;
+ handoverHdrPtr->f_Magic = kHandoverMagic;
handoverHdrPtr->f_Version = 0x1011;
handoverHdrPtr->f_Bootloader = 0x11; // Installer
Main(handoverHdrPtr);
-
} else {
- handoverHdrPtr->f_Magic = 0xFF55DD;
+ handoverHdrPtr->f_Magic = kHandoverMagic;
handoverHdrPtr->f_Version = 0x1011;
handoverHdrPtr->f_Bootloader = 0xDD; // System present
- // TODO: read .NewBoot section.
+ MUST_PASS(kernelMain);
+
+ kernelMain(handoverHdrPtr);
}
EFI::Stop();
diff --git a/Private/NewBoot/Source/FileReader.cxx b/Private/NewBoot/Source/FileReader.cxx
index 92b35eff..158f9ab4 100644
--- a/Private/NewBoot/Source/FileReader.cxx
+++ b/Private/NewBoot/Source/FileReader.cxx
@@ -107,14 +107,11 @@ Void BFileReader::ReadAll() {
if (this->mErrorCode != kOperationOkay) return;
if (mBlob == nullptr) {
- UInt8* blob = (UInt8*)hTransferBufferAddress;
-
- if (BS->AllocatePages(AllocateAnyPages, EfiLoaderData, 1,
- (EfiPhysicalAddress*)&blob) != kEfiOk) {
+ if (auto err = BS->AllocatePool(EfiLoaderCode, mSizeFile,
+ (VoidPtr*)&mBlob) != kEfiOk) {
+ mWriter.Write(L"*** EFI-Code: ").Write(err).Write(L" ***\r\n");
EFI::RaiseHardError(L"HCoreLdr_PageError", L"Allocation error.");
}
-
- mBlob = blob;
}
mErrorCode = kNotSupported;
diff --git a/Private/NewBoot/Source/TextWriter.cxx b/Private/NewBoot/Source/TextWriter.cxx
index 127b2293..b363d80b 100644
--- a/Private/NewBoot/Source/TextWriter.cxx
+++ b/Private/NewBoot/Source/TextWriter.cxx
@@ -28,6 +28,20 @@ BTextWriter &BTextWriter::Write(const CharacterType *str) {
return *this;
}
+BTextWriter &BTextWriter::Write(const UChar *str) {
+ if (*str == 0 || !str) return *this;
+
+ CharacterType strTmp[2];
+ strTmp[1] = 0;
+
+ for (size_t i = 0; str[i] != 0; i++) {
+ strTmp[0] = str[i];
+ ST->ConOut->OutputString(ST->ConOut, strTmp);
+ }
+
+ return *this;
+}
+
/**
@brief putc wrapper over EFI ConOut.
*/
@@ -41,10 +55,17 @@ BTextWriter &BTextWriter::WriteCharacter(CharacterType c) {
}
BTextWriter &BTextWriter::Write(const Long &x) {
+ this->Write(L"0x");
+ this->_Write(x);
+
+ return *this;
+}
+
+BTextWriter &BTextWriter::_Write(const Long &x) {
int y = x / 16;
int h = x % 16;
- if (y) this->Write(y);
+ if (y) this->_Write(y);
/* fail if the hex number is not base-16 */
if (h > 15) {
diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile
index 40996aa4..23680c09 100644
--- a/Private/NewBoot/Source/makefile
+++ b/Private/NewBoot/Source/makefile
@@ -10,7 +10,7 @@ ASM=nasm
OBJ=$(wildcard *.o) $(wildcard ../../Obj/*.obj) $(wildcard HEL/AMD64/*.obj)
FLAG_ASM=-f win64
-FLAG_GNU=-fshort-wchar -D__DEBUG__ -mgeneral-regs-only -mno-red-zone -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I./ -I$(HOME)/
+FLAG_GNU=-fshort-wchar -DkBootKrnlSections=9 -D__DEBUG__ -mgeneral-regs-only -mno-red-zone -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__HCORE__ -I./ -I$(HOME)/
.PHONY: invalid-recipe
invalid-recipe: