From 1c43fb19cab6eb1121a6d41f4bbe180229a3ae9e Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 20 Mar 2025 20:30:18 +0100 Subject: Refactored BootKit and DiskImage framework, minor kernel fixes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Refactored BootKit classes: - Renamed `BTextWriter` → `BootTextWriter` - Renamed `BFileReader` → `BootFileReader` - Improved consistency across `BootKit.h`, `BootThread.cc`, and related files. - Updated NetBoot module: - Standardized text writer usage. - Improved error handling for missing patches and EEPROM flash. - DiskImage framework improvements: - Introduced `DI_DISK_IMAGE` struct. - Added new `DIFormatDisk()` and `DIFormatNeFS()` functions. - Improved error handling and structure alignment. - Kernel enhancements: - Updated PE loader structures for better readability. - Renamed PE header fields for consistency. - Improved SwapDisk API structure (`SwapDiskHdr` → `SWAP_DISK_HEADER`). Signed-off-by: Amlal El Mahrouss --- dev/Boot/src/BootFileReader.cc | 16 +++++++------- dev/Boot/src/BootTextWriter.cc | 12 +++++------ dev/Boot/src/BootThread.cc | 44 +++++++++++++++++++-------------------- dev/Boot/src/HEL/AMD64/BootATA.cc | 2 +- dev/Boot/src/HEL/AMD64/BootEFI.cc | 4 ++-- dev/Boot/src/HEL/ARM64/BootEFI.cc | 2 +- 6 files changed, 40 insertions(+), 40 deletions(-) (limited to 'dev/Boot/src') diff --git a/dev/Boot/src/BootFileReader.cc b/dev/Boot/src/BootFileReader.cc index 80ae46a2..4712374e 100644 --- a/dev/Boot/src/BootFileReader.cc +++ b/dev/Boot/src/BootFileReader.cc @@ -22,7 +22,7 @@ //////////////////////////////////////////////////////////////////////////////////////////////////// /// /// -/// @name BFileReader class +/// @name BootFileReader class /// @brief Reads the file as a blob. /// /// @@ -31,8 +31,8 @@ /*** @brief File Reader constructor. */ -Boot::BFileReader::BFileReader(const CharacterTypeUTF16* path, - EfiHandlePtr ImageHandle) +Boot::BootFileReader::BootFileReader(const CharacterTypeUTF16* path, + EfiHandlePtr ImageHandle) { if (path != nullptr) { @@ -98,7 +98,7 @@ Boot::BFileReader::BFileReader(const CharacterTypeUTF16* path, mErrorCode = kOperationOkay; } -Boot::BFileReader::~BFileReader() +Boot::BootFileReader::~BootFileReader() { if (this->mFile) { @@ -126,7 +126,7 @@ Boot::BFileReader::~BFileReader() @param **readUntil** size of file @param **chunkToRead** chunk to read each time. */ -Void Boot::BFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead, UIntPtr out_address) +Void Boot::BootFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead, UIntPtr out_address) { UInt32 szInfo = sizeof(EfiFileInfo); @@ -186,21 +186,21 @@ Void Boot::BFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead, UIntPtr out_ /// @brief error code getter. /// @return the error code. -Int32& Boot::BFileReader::Error() +Int32& Boot::BootFileReader::Error() { return mErrorCode; } /// @brief blob getter. /// @return the blob. -VoidPtr Boot::BFileReader::Blob() +VoidPtr Boot::BootFileReader::Blob() { return mBlob; } /// @breif Size getter. /// @return the size of the file. -UInt64& Boot::BFileReader::Size() +UInt64& Boot::BootFileReader::Size() { return mSizeFile; } diff --git a/dev/Boot/src/BootTextWriter.cc b/dev/Boot/src/BootTextWriter.cc index 5bebdb10..b58d3429 100644 --- a/dev/Boot/src/BootTextWriter.cc +++ b/dev/Boot/src/BootTextWriter.cc @@ -23,7 +23,7 @@ /** @brief puts wrapper over EFI ConOut. */ -Boot::BTextWriter& Boot::BTextWriter::Write(const CharacterTypeUTF16* str) +Boot::BootTextWriter& Boot::BootTextWriter::Write(const CharacterTypeUTF16* str) { #ifdef __DEBUG__ if (!str || *str == 0) @@ -55,7 +55,7 @@ Boot::BTextWriter& Boot::BTextWriter::Write(const CharacterTypeUTF16* str) /// @brief UTF-8 equivalent of Write (UTF-16). /// @param str the input string. -Boot::BTextWriter& Boot::BTextWriter::Write(const Char* str) +Boot::BootTextWriter& Boot::BootTextWriter::Write(const Char* str) { #ifdef __DEBUG__ if (!str || *str == 0) @@ -85,7 +85,7 @@ Boot::BTextWriter& Boot::BTextWriter::Write(const Char* str) return *this; } -Boot::BTextWriter& Boot::BTextWriter::Write(const UChar* str) +Boot::BootTextWriter& Boot::BootTextWriter::Write(const UChar* str) { #ifdef __DEBUG__ if (!str || *str == 0) @@ -118,7 +118,7 @@ Boot::BTextWriter& Boot::BTextWriter::Write(const UChar* str) /** @brief putc wrapper over EFI ConOut. */ -Boot::BTextWriter& Boot::BTextWriter::WriteCharacter(CharacterTypeUTF16 c) +Boot::BootTextWriter& Boot::BootTextWriter::WriteCharacter(CharacterTypeUTF16 c) { #ifdef __DEBUG__ EfiCharType str[2]; @@ -131,7 +131,7 @@ Boot::BTextWriter& Boot::BTextWriter::WriteCharacter(CharacterTypeUTF16 c) return *this; } -Boot::BTextWriter& Boot::BTextWriter::Write(const Long& x) +Boot::BootTextWriter& Boot::BootTextWriter::Write(const Long& x) { #ifdef __DEBUG__ this->_Write(x); @@ -141,7 +141,7 @@ Boot::BTextWriter& Boot::BTextWriter::Write(const Long& x) return *this; } -Boot::BTextWriter& Boot::BTextWriter::_Write(const Long& x) +Boot::BootTextWriter& Boot::BootTextWriter::_Write(const Long& x) { #ifdef __DEBUG__ UInt64 y = (x > 0 ? x : -x) / 16; diff --git a/dev/Boot/src/BootThread.cc b/dev/Boot/src/BootThread.cc index 8b136ba3..ff094f72 100644 --- a/dev/Boot/src/BootThread.cc +++ b/dev/Boot/src/BootThread.cc @@ -30,7 +30,7 @@ namespace Boot // detect the format. const Char* blob_bytes = reinterpret_cast(fBlob); - BTextWriter writer; + BootTextWriter writer; if (!blob_bytes) { @@ -48,15 +48,15 @@ namespace Boot return; #ifdef __NE_AMD64__ - if (header_ptr->mMachine != kPeMachineAMD64 || - header_ptr->mSignature != kPeSignature) + if (header_ptr->Machine != kPeMachineAMD64 || + header_ptr->Signature != kPeSignature) { writer.Write("BootZ: Not a PE32+ executable.\r"); return; } #elif defined(__NE_ARM64__) - if (header_ptr->mMachine != kPeMachineARM64 || - header_ptr->mSignature != kPeSignature) + if (header_ptr->Machine != kPeMachineARM64 || + header_ptr->Signature != kPeSignature) { writer.Write("BootZ: Not a PE32+ executable.\r"); return; @@ -65,25 +65,25 @@ namespace Boot writer.Write("BootZ: PE32+ executable detected (NeKernel Subsystem).\r"); - auto numSecs = header_ptr->mNumberOfSections; + auto numSecs = header_ptr->NumberOfSections; - writer.Write("BootZ: Major Linker Ver: ").Write(opt_header_ptr->mMajorLinkerVersion).Write("\r"); - writer.Write("BootZ: Minor Linker Ver: ").Write(opt_header_ptr->mMinorLinkerVersion).Write("\r"); - writer.Write("BootZ: Major Subsystem Ver: ").Write(opt_header_ptr->mMajorSubsystemVersion).Write("\r"); - writer.Write("BootZ: Minor Subsystem Ver: ").Write(opt_header_ptr->mMinorSubsystemVersion).Write("\r"); - writer.Write("BootZ: Magic: ").Write(header_ptr->mSignature).Write("\r"); + writer.Write("BootZ: Major Linker Ver: ").Write(opt_header_ptr->MajorLinkerVersion).Write("\r"); + writer.Write("BootZ: Minor Linker Ver: ").Write(opt_header_ptr->MinorLinkerVersion).Write("\r"); + writer.Write("BootZ: Major Subsystem Ver: ").Write(opt_header_ptr->MajorSubsystemVersion).Write("\r"); + writer.Write("BootZ: Minor Subsystem Ver: ").Write(opt_header_ptr->MinorSubsystemVersion).Write("\r"); + writer.Write("BootZ: Magic: ").Write(header_ptr->Signature).Write("\r"); constexpr auto cPageSize = 512; - EfiPhysicalAddress loadStartAddress = opt_header_ptr->mImageBase; - loadStartAddress += opt_header_ptr->mBaseOfData; + EfiPhysicalAddress loadStartAddress = opt_header_ptr->ImageBase; + loadStartAddress += opt_header_ptr->BaseOfData; writer.Write("BootZ: Image base: ").Write(loadStartAddress).Write("\r"); - auto numPages = opt_header_ptr->mSizeOfImage / cPageSize; + auto numPages = opt_header_ptr->SizeOfImage / cPageSize; BS->AllocatePages(AllocateAddress, EfiLoaderData, numPages, &loadStartAddress); - LDR_SECTION_HEADER_PTR sectPtr = (LDR_SECTION_HEADER_PTR)(((Char*)opt_header_ptr) + header_ptr->mSizeOfOptionalHeader); + LDR_SECTION_HEADER_PTR sectPtr = (LDR_SECTION_HEADER_PTR)(((Char*)opt_header_ptr) + header_ptr->SizeOfOptionalHeader); constexpr auto sectionForCode = ".text"; constexpr auto sectionForNewLdr = ".ldr"; @@ -93,14 +93,14 @@ namespace Boot { LDR_SECTION_HEADER_PTR sect = §Ptr[sectIndex]; - SetMem((VoidPtr)(loadStartAddress + sect->mVirtualAddress), 0, sect->mSizeOfRawData); + SetMem((VoidPtr)(loadStartAddress + sect->VirtualAddress), 0, sect->SizeOfRawData); - if (StrCmp(sectionForCode, sect->mName) == 0) + if (StrCmp(sectionForCode, sect->Name) == 0) { - fStartAddress = (VoidPtr)((UIntPtr)loadStartAddress + opt_header_ptr->mAddressOfEntryPoint); + fStartAddress = (VoidPtr)((UIntPtr)loadStartAddress + opt_header_ptr->AddressOfEntryPoint); writer.Write("BootZ: Executable entry address: ").Write((UIntPtr)fStartAddress).Write("\r"); } - else if (StrCmp(sectionForNewLdr, sect->mName) == 0) + else if (StrCmp(sectionForNewLdr, sect->Name) == 0) { struct HANDOVER_INFORMATION_STUB { @@ -108,7 +108,7 @@ namespace Boot UInt32 HandoverType; UInt32 HandoverPad; UInt32 HandoverArch; - }* handover_struc = (struct HANDOVER_INFORMATION_STUB*)((UIntPtr)fBlob + sect->mPointerToRawData); + }* handover_struc = (struct HANDOVER_INFORMATION_STUB*)((UIntPtr)fBlob + sect->PointerToRawData); if (handover_struc->HandoverMagic != kHandoverMagic && handover_struc->HandoverType != HEL::kTypeKernel) @@ -134,9 +134,9 @@ namespace Boot } } - writer.Write("BootZ: Raw offset: ").Write(sect->mPointerToRawData).Write(" of ").Write(sect->mName).Write("\r"); + writer.Write("BootZ: Raw offset: ").Write(sect->PointerToRawData).Write(" of ").Write(sect->Name).Write("\r"); - CopyMem((VoidPtr)(loadStartAddress + sect->mVirtualAddress), (VoidPtr)((UIntPtr)fBlob + sect->mPointerToRawData), sect->mSizeOfRawData); + CopyMem((VoidPtr)(loadStartAddress + sect->VirtualAddress), (VoidPtr)((UIntPtr)fBlob + sect->PointerToRawData), sect->SizeOfRawData); } } else if (blob_bytes[0] == kPefMagic[0] && diff --git a/dev/Boot/src/HEL/AMD64/BootATA.cc b/dev/Boot/src/HEL/AMD64/BootATA.cc index 0cebdb9d..0ec6ab18 100644 --- a/dev/Boot/src/HEL/AMD64/BootATA.cc +++ b/dev/Boot/src/HEL/AMD64/BootATA.cc @@ -67,7 +67,7 @@ Boolean boot_ata_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) if (boot_ata_detected()) return true; - BTextWriter writer; + BootTextWriter writer; UInt16 IO = Bus; diff --git a/dev/Boot/src/HEL/AMD64/BootEFI.cc b/dev/Boot/src/HEL/AMD64/BootEFI.cc index 56315475..11b70b81 100644 --- a/dev/Boot/src/HEL/AMD64/BootEFI.cc +++ b/dev/Boot/src/HEL/AMD64/BootEFI.cc @@ -202,7 +202,7 @@ EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr image_handle, handover_hdr->f_FirmwareCustomTables[0] = (VoidPtr)BS; handover_hdr->f_FirmwareCustomTables[1] = (VoidPtr)ST; - Boot::BFileReader reader_syschk(L"syschk.sys", image_handle); + Boot::BootFileReader reader_syschk(L"syschk.sys", image_handle); reader_syschk.ReadAll(0); Boot::BootThread* syschk_thread = nullptr; @@ -279,7 +279,7 @@ EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr image_handle, ST->RuntimeServices->SetVariable(L"/props/boot_path", kEfiGlobalNamespaceVarGUID, &attr, &kernel_path_sz, kernel_path); } - Boot::BFileReader reader_kernel(kernel_path, image_handle); + Boot::BootFileReader reader_kernel(kernel_path, image_handle); reader_kernel.ReadAll(0); diff --git a/dev/Boot/src/HEL/ARM64/BootEFI.cc b/dev/Boot/src/HEL/ARM64/BootEFI.cc index d3d72ff6..9df1b2d4 100644 --- a/dev/Boot/src/HEL/ARM64/BootEFI.cc +++ b/dev/Boot/src/HEL/ARM64/BootEFI.cc @@ -196,7 +196,7 @@ EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr image_handle, // Assign to global 'kHandoverHeader'. - Boot::BFileReader reader_kernel(L"neoskrnl.exe", image_handle); + Boot::BootFileReader reader_kernel(L"neoskrnl.exe", image_handle); reader_kernel.ReadAll(0); -- cgit v1.2.3