From e2bbec91d70847cc5a2ff67eb84ca4a3c2d03e85 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 4 Feb 2024 10:59:24 +0100 Subject: Kernel: Depend less on NewFS, add support for FileSystem protocol in NewBoot. Signed-off-by: Amlal El Mahrouss --- Private/NewBoot/BootKit/Arch/AHCI.hxx | 10 +-- Private/NewBoot/BootKit/BootKit.hxx | 5 +- Private/NewBoot/Source/Entrypoint.cxx | 19 ++-- Private/NewBoot/Source/FileReader.cxx | 118 +++++++++++++++++++++++-- Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx | 1 + Private/NewBoot/Source/String.cxx | 1 - Private/NewBoot/Source/TextWriter.cxx | 1 - Private/NewBoot/Source/makefile | 3 +- 8 files changed, 124 insertions(+), 34 deletions(-) (limited to 'Private/NewBoot') diff --git a/Private/NewBoot/BootKit/Arch/AHCI.hxx b/Private/NewBoot/BootKit/Arch/AHCI.hxx index 0735baad..1f36ca00 100644 --- a/Private/NewBoot/BootKit/Arch/AHCI.hxx +++ b/Private/NewBoot/BootKit/Arch/AHCI.hxx @@ -38,12 +38,4 @@ class BDeviceAHCI final { AHCITraits mTraits; }; -enum { - kATADevicePATA, - kATADeviceSATA, - kATADevicePATA_PI, - kATADeviceSATA_PI, - kATADeviceCount, -}; - -#define kATASectorSz 512 +#define kAHCISectorSz 512 diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index 45f54cb4..6fec1724 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -62,9 +62,9 @@ HCore::SizeT BSetMem(CharacterType *src, const CharacterType byte, class BFileReader final { public: explicit BFileReader(const CharacterType *path); - ~BFileReader() = default; + ~BFileReader(); - HCore::VoidPtr Fetch(SizeT &size); + HCore::VoidPtr Fetch(EfiHandlePtr ImageHandle); enum { kOperationOkay, @@ -83,6 +83,7 @@ class BFileReader final { private: Int32 mErrorCode{kOperationOkay}; + VoidPtr mBlob{nullptr}; CharacterType mPath[kPathLen]; BTextWriter mWriter; BDeviceATA mDevice; diff --git a/Private/NewBoot/Source/Entrypoint.cxx b/Private/NewBoot/Source/Entrypoint.cxx index 39769b87..4839ee1f 100644 --- a/Private/NewBoot/Source/Entrypoint.cxx +++ b/Private/NewBoot/Source/Entrypoint.cxx @@ -7,7 +7,6 @@ * ======================================================== */ -#include "BootKit/Arch/ATA.hxx" #define __BOOTLOADER__ 1 #include @@ -43,26 +42,18 @@ EFI_EXTERN_C Int EfiMain(EfiHandlePtr ImageHandle, UInt64 mapKey = 0; - BFileReader img(L"HCoreKrnl.exe"); + BFileReader img(L"\\EFI\\BOOT\\HCoreKrnl.exe"); - SizeT sz = 0UL; - PEImagePtr blob = (PEImagePtr)img.Fetch(sz); + PEImagePtr blob = (PEImagePtr)img.Fetch(ImageHandle); - if (!blob || sz < 1) + if (!blob) EFI::RaiseHardError(L"HCoreLdr_NoSuchKernel", L"Couldn't find HCoreKrnl.exe!"); - ExecHeaderPtr headerPtr = (ExecHeaderPtr)blob; + writer.WriteString(L"HCoreLdr: Running HCoreKrnl.exe...\r\n"); - if (blob[0] == kMagMz0 && blob[1] == kMagMz1) { - writer.WriteString(L"HCoreLdr: Running HCoreKrnl.exe...\r\n"); - /// Load Image here - } else { - EFI::RaiseHardError(L"HCoreLdr_NotPE", L"Not a PE file! Aborting..."); - } - - EFI::Stop(); EFI::ExitBootServices(mapKey, ImageHandle); + EFI::Stop(); return kEfiOk; } diff --git a/Private/NewBoot/Source/FileReader.cxx b/Private/NewBoot/Source/FileReader.cxx index 39884f9f..3edacb5a 100644 --- a/Private/NewBoot/Source/FileReader.cxx +++ b/Private/NewBoot/Source/FileReader.cxx @@ -14,7 +14,8 @@ #include #include -#include + +#include "EFIKit/EFI.hxx" //////////////////////////////////////////////////////////////////////////////////////////////////// // @@ -27,7 +28,7 @@ /*** @brief File Reader constructor. */ -BFileReader::BFileReader(const CharacterType *path) { +BFileReader::BFileReader(const CharacterType* path) { if (path != nullptr) { SizeT index = 0UL; for (; path[index] != L'\0'; ++index) { @@ -38,17 +39,122 @@ BFileReader::BFileReader(const CharacterType *path) { } } +BFileReader::~BFileReader() { + if (this->mBlob) { + BS->FreePool(this->mBlob); + } +} + /** @brief this reads all of the buffer. - @param size, new buffer size. + @param ImageHandle used internally. */ -HCore::VoidPtr BFileReader::Fetch(SizeT &size) { +HCore::VoidPtr BFileReader::Fetch(EfiHandlePtr ImageHandle) { mWriter.WriteString(L"HCoreLdr: Fetch-File: ") .WriteString(mPath) .WriteString(L"\r\n"); + /// Load protocols with their GUIDs. + + EfiGUID guidEfp = EfiGUID(EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID); + + EfiSimpleFilesystemProtocol* efp = nullptr; + EfiFileProtocol* rootFs = nullptr; + + EfiLoadImageProtocol* img = nullptr; + EfiGUID guidImg = EfiGUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); + + if (BS->OpenProtocol(ImageHandle, &guidImg, (void**)&img, ImageHandle, + nullptr, + EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) != kEfiOk) { + mWriter.WriteString(L"HCoreLdr: Fetch-Protocol: No-Such-Protocol") + .WriteString(L"\r\n"); + this->mErrorCode = kNotSupported; + } + + if (BS->OpenProtocol(img->DeviceHandle, &guidEfp, (void**)&efp, ImageHandle, + nullptr, + EFI_OPEN_PROTOCOL_BY_HANDLE_PROTOCOL) != kEfiOk) { + mWriter.WriteString(L"HCoreLdr: Fetch-Protocol: No-Such-Protocol") + .WriteString(L"\r\n"); + this->mErrorCode = kNotSupported; + return nullptr; + } + + /// Start doing disk I/O + + if (efp->OpenVolume(efp, &rootFs) != kEfiOk) { + mWriter.WriteString(L"HCoreLdr: Fetch-Protocol: No-Such-Volume") + .WriteString(L"\r\n"); + this->mErrorCode = kNotSupported; + return nullptr; + } + + /// Open kernel. + + EfiFileProtocol* kernelFile; + + if (rootFs->Open(rootFs, &kernelFile, mPath, kEFIFileRead, + kEFIReadOnly | kEFIHidden | kEFISystem) != kEfiOk) { + mWriter.WriteString(L"HCoreLdr: Fetch-Protocol: No-Such-Path: ") + .WriteString(mPath) + .WriteString(L"\r\n"); + this->mErrorCode = kNotSupported; + return nullptr; + } + + /// File FAT info. + + UInt32 szInfo = sizeof(EfiFileInfo); + EfiFileInfo info{0}; + + guidEfp = EfiGUID(EFI_FILE_INFO_GUID); + + if (kernelFile->GetInfo(kernelFile, &guidEfp, &szInfo, (void*)&info) != + kEfiOk) { + mWriter.WriteString(L"HCoreLdr: Fetch-Protocol: No-Such-Path: ") + .WriteString(mPath) + .WriteString(L"\r\n"); + this->mErrorCode = kNotSupported; + return nullptr; + } + + mWriter.WriteString(L"HCoreLdr: Fetch-Info: In-Progress...") + .WriteString(L"\r\n"); + + UInt8* blob = nullptr; + + mWriter.WriteString(L"HCoreLdr: Fetch-Info: OK...").WriteString(L"\r\n"); + + UInt64 sz = info.FileSize; + + if (BS->AllocatePool(EfiBootServicesData, sz, (VoidPtr*)&blob) != kEfiOk) { + mWriter + .WriteString( + L"HCoreLdr: Fetch: Failed to call AllocatePool " + L"correctly!") + .WriteString(L"\r\n"); + + kernelFile->Close(kernelFile); + + return nullptr; + } + + BSetMem((CharacterType*)blob, 0, sz); + + mWriter.WriteString(L"HCoreLdr: Fetch-File: In-Progress...") + .WriteString(L"\r\n"); + + kernelFile->Read(kernelFile, &sz, blob); + + mWriter.WriteString(L"HCoreLdr: Fetch-File: OK").WriteString(L"\r\n"); + + kernelFile->Close(kernelFile); + this->mCached = true; - this->mErrorCode = kNotSupported; + this->mErrorCode = kOperationOkay; + + this->mBlob = blob; - return nullptr; + return blob; } diff --git a/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx b/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx index 20af043d..46bec94c 100644 --- a/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/AMD64-ATA.cxx @@ -71,6 +71,7 @@ ATAInit_Retry: auto statRdy = In8(IO + ATA_REG_STATUS); if (statRdy & ATA_SR_ERR) { + writer.WriteString(L"HCoreLdr: Probe error.\r\n"); return false; } if ((statRdy & ATA_SR_BSY)) goto ATAInit_Retry; diff --git a/Private/NewBoot/Source/String.cxx b/Private/NewBoot/Source/String.cxx index e080a01d..c59e2bba 100644 --- a/Private/NewBoot/Source/String.cxx +++ b/Private/NewBoot/Source/String.cxx @@ -13,7 +13,6 @@ #include #include -#include /// bugs 0 diff --git a/Private/NewBoot/Source/TextWriter.cxx b/Private/NewBoot/Source/TextWriter.cxx index 77baa067..b6a8e3b8 100644 --- a/Private/NewBoot/Source/TextWriter.cxx +++ b/Private/NewBoot/Source/TextWriter.cxx @@ -13,7 +13,6 @@ #include #include -#include /// bugs 0 diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index 460b5dc1..3deef8f4 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -19,6 +19,7 @@ bootloader-amd64: $(CC_GNU) $(FLAG_GNU) HEL/AMD64/*.cxx *.cxx $(LD_GNU) *.o HEL/AMD64/*.obj -e efi_main -filealign:16 -shared --subsystem=10 -ffreestanding -o HCoreLdr.exe cp HCoreLdr.exe CDROM/EFI/BOOT/BOOTX64.EFI + cp ../../HCoreKrnl.exe CDROM/EFI/BOOT/HCoreKrnl.exe .PHONY: make-disk make-disk: @@ -27,7 +28,7 @@ make-disk: .PHONY: run-efi-debug run-efi-debug: wget https://retrage.github.io/edk2-nightly/bin/DEBUGX64_OVMF.fd -O OVMF.fd - qemu-system-x86_64 -M q35 -bios OVMF.fd -drive file=os.epm,index=0,if=ide,format=qcow2 -drive file=fat:rw:CDROM,index=1,format=raw -d int + qemu-system-x86_64 -m 8G -M q35 -bios OVMF.fd -drive file=os.epm,index=0,if=ide,format=qcow2 -drive file=fat:rw:CDROM,index=1,format=raw -d int .PHONY: clean clean: -- cgit v1.2.3