summaryrefslogtreecommitdiffhomepage
path: root/Private/NewBoot/Source/FileReader.cxx
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-02-04 10:59:24 +0100
committerAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-02-04 10:59:24 +0100
commite2bbec91d70847cc5a2ff67eb84ca4a3c2d03e85 (patch)
tree1e7fec7c232b0c750157007113ce8f86d43b7e76 /Private/NewBoot/Source/FileReader.cxx
parentaff54fd3dc6855b62c047f126c6fe20ca717ee0f (diff)
Kernel: Depend less on NewFS, add support for FileSystem protocol in
NewBoot. Signed-off-by: Amlal El Mahrouss <amlalelmahrouss@icloud.com>
Diffstat (limited to 'Private/NewBoot/Source/FileReader.cxx')
-rw-r--r--Private/NewBoot/Source/FileReader.cxx118
1 files changed, 112 insertions, 6 deletions
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 <BootKit/BootKit.hxx>
#include <EFIKit/Api.hxx>
-#include <FSKit/NewFS.hxx>
+
+#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;
}