From 77571f02bcd83ca9a158e9ce8592b101039878e1 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 22 Feb 2024 18:31:09 +0100 Subject: Bootloader: refactoring source file names. Signed-off-by: Amlal El Mahrouss --- Private/NewBoot/Source/BootMain.cxx | 178 +++++++++++++++++++++++++ Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx | 5 +- Private/NewBoot/Source/RuntimeMain.cxx | 178 ------------------------- 3 files changed, 181 insertions(+), 180 deletions(-) create mode 100644 Private/NewBoot/Source/BootMain.cxx delete mode 100644 Private/NewBoot/Source/RuntimeMain.cxx diff --git a/Private/NewBoot/Source/BootMain.cxx b/Private/NewBoot/Source/BootMain.cxx new file mode 100644 index 00000000..0db90e6f --- /dev/null +++ b/Private/NewBoot/Source/BootMain.cxx @@ -0,0 +1,178 @@ +/* + * ======================================================== + * + * NewBoot + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#define __BOOTLOADER__ 1 + +#include +#include +#include +#include +#include + +#ifdef __x86_64__ +#include +#else +#error This CPU is unknown. +#endif // ifdef __x86_64__ + +#define kHeadersSz \ + (sizeof(DosHeader) + sizeof(ExecHeader) + sizeof(ExecOptionalHeader)) + +#ifdef __BUNDLE_KERNEL__ +EXTERN_C EFI_API void RuntimeMain(HEL::HandoverInformationHeader* HIH); +#endif + +EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, + EfiSystemTable* SystemTable) { + InitEFI(SystemTable); + InitQT(); + + BTextWriter writer; + +#ifdef __BUNDLE_KERNEL__ + writer.WriteString(L"HCoreLite: "); +#else + writer.WriteString(L"HCoreLdr:"); +#endif + +#ifndef __DEBUG__ + + writer.WriteString(L"Version 1.00 (Release Channel)\r\n"); + +#else + + writer.WriteString(L"Version 1.00 (Insiders Channel)\r\n"); + +#endif + + const char strDate[] = __DATE__; + +#ifdef __BUNDLE_KERNEL__ + writer.WriteString(L"HCoreLite: Build: "); +#else + writer.WriteString(L"HCoreLdr: Build: "); +#endif + + for (auto& ch : strDate) writer.WriteCharacter(ch); + +#ifdef __BUNDLE_KERNEL__ + writer.WriteString(L"\r\nHCoreLite: Firmware Vendor: ") + .WriteString(SystemTable->FirmwareVendor) + .WriteString(L"\r\n"); +#else + writer.WriteString(L"\r\nHCoreLdr: Firmware Vendor: ") + .WriteString(SystemTable->FirmwareVendor) + .WriteString(L"\r\n"); +#endif + + BFileReader img(L"HCOREKRNL.EXE", ImageHandle); + + img.Size(kHeadersSz); + img.ReadAll(); + + if (img.Error() == BFileReader::kOperationOkay) { + BlobType blob = (BlobType)img.Blob(); + + ExecHeaderPtr ptrHdr = reinterpret_cast( + HCore::rt_find_exec_header(reinterpret_cast(blob))); + + if (ptrHdr && ptrHdr->mMachine == EFI::Platform() && + ptrHdr->mMagic == kPeMagic) { + /// sections must be at least 3. + if (ptrHdr->mNumberOfSections >= 3) { + ExecOptionalHeaderPtr optHdr = reinterpret_cast( + ptrHdr + sizeof(ExecHeader)); + + UInt32 MapKey = 0; + UInt32* Size; + EfiMemoryDescriptor* Descriptor; + UInt32 SzDesc = 0; + UInt32 RevDesc = 0; + + if (BS->AllocatePool(EfiLoaderData, sizeof(UInt32), (VoidPtr*)&Size) != + kEfiOk) { + EFI::RaiseHardError( + L"HCoreLdr-BadAlloc", + L"The bootloader ran out of memory! Please check your specs."); + } + + *Size = sizeof(EfiMemoryDescriptor); + + if (BS->AllocatePool(EfiLoaderData, sizeof(EfiMemoryDescriptor), + (VoidPtr*)&Descriptor) != kEfiOk) { + EFI::RaiseHardError( + L"HCoreLdr-BadAlloc", + L"The bootloader ran out of memory! Please check your specs."); + } + + if (BS->GetMemoryMap(Size, Descriptor, &MapKey, &SzDesc, &RevDesc) != + kEfiOk) { + EFI::RaiseHardError( + L"HCoreLdr-GetMemoryMap", + L"GetMemoryMap returned a value which isn't kEfiOk!"); + } + + HEL::HandoverInformationHeader* handoverHdrPtr = nullptr; + + BS->AllocatePool(EfiLoaderData, sizeof(HEL::HandoverInformationHeader), + (VoidPtr*)&handoverHdrPtr); + + handoverHdrPtr->f_GOP = (voidPtr)kGop->Mode->FrameBufferBase; + handoverHdrPtr->f_GOPSize = kGop->Mode->FrameBufferSize; + + handoverHdrPtr->f_PhysicalStart = + reinterpret_cast(Descriptor->PhysicalStart); + handoverHdrPtr->f_PhysicalSize = Descriptor->NumberOfPages; + + handoverHdrPtr->f_VirtualStart = + reinterpret_cast(Descriptor->VirtualStart); + + handoverHdrPtr->f_VirtualSize = + Descriptor->NumberOfPages; /* # of pages */ + + handoverHdrPtr->f_FirmwareVendorLen = + BStrLen(SystemTable->FirmwareVendor); + + BCopyMem(handoverHdrPtr->f_FirmwareVendorName, + SystemTable->FirmwareVendor, + handoverHdrPtr->f_FirmwareVendorLen); + +#ifdef __BUNDLE_KERNEL__ + writer.WriteString(L"HCoreLite: Exit Boot...").WriteString(L"\r\n"); +#else + writer.WriteString(L"HCoreLdr: Load File succeeded, running it...") + .WriteString(L"\r\n"); +#endif + + EFI::ExitBootServices(MapKey, ImageHandle); + +#ifdef __BUNDLE_KERNEL__ + RuntimeMain(handoverHdrPtr); +#else + // Load HCoreKrnl.exe (TODO) + +#endif // ifdef __BUNDLE_KERNEL__ + + EFI::Stop(); + + return kEfiOk; + } 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-0003\r\n"); + } + + EFI::Stop(); + + return kEfiFail; +} diff --git a/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx b/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx index 5382fd30..04755dcf 100644 --- a/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx @@ -85,14 +85,15 @@ ATAInit_Retry: BSetMem(kATAData, 0, kATADataLen); + /// fetch serial info + /// model, speed, number of sectors... + for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) { kATAData[indexData] = In16(IO + ATA_REG_DATA); } writer.WriteString(L"HCoreLdr: Model: "); - /// fetch drive info - for (SizeT indexData = 0; indexData < kATADataLen; indexData += 1) { writer.WriteCharacter(kATAData[indexData + ATA_IDENT_MODEL + 1]) .WriteCharacter(kATAData[indexData + ATA_IDENT_MODEL]); diff --git a/Private/NewBoot/Source/RuntimeMain.cxx b/Private/NewBoot/Source/RuntimeMain.cxx deleted file mode 100644 index 0db90e6f..00000000 --- a/Private/NewBoot/Source/RuntimeMain.cxx +++ /dev/null @@ -1,178 +0,0 @@ -/* - * ======================================================== - * - * NewBoot - * Copyright Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#define __BOOTLOADER__ 1 - -#include -#include -#include -#include -#include - -#ifdef __x86_64__ -#include -#else -#error This CPU is unknown. -#endif // ifdef __x86_64__ - -#define kHeadersSz \ - (sizeof(DosHeader) + sizeof(ExecHeader) + sizeof(ExecOptionalHeader)) - -#ifdef __BUNDLE_KERNEL__ -EXTERN_C EFI_API void RuntimeMain(HEL::HandoverInformationHeader* HIH); -#endif - -EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, - EfiSystemTable* SystemTable) { - InitEFI(SystemTable); - InitQT(); - - BTextWriter writer; - -#ifdef __BUNDLE_KERNEL__ - writer.WriteString(L"HCoreLite: "); -#else - writer.WriteString(L"HCoreLdr:"); -#endif - -#ifndef __DEBUG__ - - writer.WriteString(L"Version 1.00 (Release Channel)\r\n"); - -#else - - writer.WriteString(L"Version 1.00 (Insiders Channel)\r\n"); - -#endif - - const char strDate[] = __DATE__; - -#ifdef __BUNDLE_KERNEL__ - writer.WriteString(L"HCoreLite: Build: "); -#else - writer.WriteString(L"HCoreLdr: Build: "); -#endif - - for (auto& ch : strDate) writer.WriteCharacter(ch); - -#ifdef __BUNDLE_KERNEL__ - writer.WriteString(L"\r\nHCoreLite: Firmware Vendor: ") - .WriteString(SystemTable->FirmwareVendor) - .WriteString(L"\r\n"); -#else - writer.WriteString(L"\r\nHCoreLdr: Firmware Vendor: ") - .WriteString(SystemTable->FirmwareVendor) - .WriteString(L"\r\n"); -#endif - - BFileReader img(L"HCOREKRNL.EXE", ImageHandle); - - img.Size(kHeadersSz); - img.ReadAll(); - - if (img.Error() == BFileReader::kOperationOkay) { - BlobType blob = (BlobType)img.Blob(); - - ExecHeaderPtr ptrHdr = reinterpret_cast( - HCore::rt_find_exec_header(reinterpret_cast(blob))); - - if (ptrHdr && ptrHdr->mMachine == EFI::Platform() && - ptrHdr->mMagic == kPeMagic) { - /// sections must be at least 3. - if (ptrHdr->mNumberOfSections >= 3) { - ExecOptionalHeaderPtr optHdr = reinterpret_cast( - ptrHdr + sizeof(ExecHeader)); - - UInt32 MapKey = 0; - UInt32* Size; - EfiMemoryDescriptor* Descriptor; - UInt32 SzDesc = 0; - UInt32 RevDesc = 0; - - if (BS->AllocatePool(EfiLoaderData, sizeof(UInt32), (VoidPtr*)&Size) != - kEfiOk) { - EFI::RaiseHardError( - L"HCoreLdr-BadAlloc", - L"The bootloader ran out of memory! Please check your specs."); - } - - *Size = sizeof(EfiMemoryDescriptor); - - if (BS->AllocatePool(EfiLoaderData, sizeof(EfiMemoryDescriptor), - (VoidPtr*)&Descriptor) != kEfiOk) { - EFI::RaiseHardError( - L"HCoreLdr-BadAlloc", - L"The bootloader ran out of memory! Please check your specs."); - } - - if (BS->GetMemoryMap(Size, Descriptor, &MapKey, &SzDesc, &RevDesc) != - kEfiOk) { - EFI::RaiseHardError( - L"HCoreLdr-GetMemoryMap", - L"GetMemoryMap returned a value which isn't kEfiOk!"); - } - - HEL::HandoverInformationHeader* handoverHdrPtr = nullptr; - - BS->AllocatePool(EfiLoaderData, sizeof(HEL::HandoverInformationHeader), - (VoidPtr*)&handoverHdrPtr); - - handoverHdrPtr->f_GOP = (voidPtr)kGop->Mode->FrameBufferBase; - handoverHdrPtr->f_GOPSize = kGop->Mode->FrameBufferSize; - - handoverHdrPtr->f_PhysicalStart = - reinterpret_cast(Descriptor->PhysicalStart); - handoverHdrPtr->f_PhysicalSize = Descriptor->NumberOfPages; - - handoverHdrPtr->f_VirtualStart = - reinterpret_cast(Descriptor->VirtualStart); - - handoverHdrPtr->f_VirtualSize = - Descriptor->NumberOfPages; /* # of pages */ - - handoverHdrPtr->f_FirmwareVendorLen = - BStrLen(SystemTable->FirmwareVendor); - - BCopyMem(handoverHdrPtr->f_FirmwareVendorName, - SystemTable->FirmwareVendor, - handoverHdrPtr->f_FirmwareVendorLen); - -#ifdef __BUNDLE_KERNEL__ - writer.WriteString(L"HCoreLite: Exit Boot...").WriteString(L"\r\n"); -#else - writer.WriteString(L"HCoreLdr: Load File succeeded, running it...") - .WriteString(L"\r\n"); -#endif - - EFI::ExitBootServices(MapKey, ImageHandle); - -#ifdef __BUNDLE_KERNEL__ - RuntimeMain(handoverHdrPtr); -#else - // Load HCoreKrnl.exe (TODO) - -#endif // ifdef __BUNDLE_KERNEL__ - - EFI::Stop(); - - return kEfiOk; - } 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-0003\r\n"); - } - - EFI::Stop(); - - return kEfiFail; -} -- cgit v1.2.3