summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-02-06 11:30:56 +0100
committerAmlal El Mahrouss <amlalelmahrouss@icloud.com>2024-02-06 11:30:56 +0100
commit61492dc648412818e1f0e96dbc9522514b1fed2a (patch)
tree068fd8cca5e27cbe48990dae2e10cc618e6ae155
parent55eb89dde91318a6b7f37e824d383e0625cd53f1 (diff)
HCR-15 : Load kernel into memory.
Progess have been done regarding PE/MZ support, also updated PEF impl to not rely on compiler feature __attribute__ Signed-off-by: Amlal El Mahrouss <amlalelmahrouss@icloud.com>
-rw-r--r--Private/ArchKit/ArchKit.hpp2
-rw-r--r--Private/EFIKit/Api.hxx6
-rw-r--r--Private/EFIKit/Handover.hxx6
-rw-r--r--Private/KernelKit/MSDOS.hpp2
-rw-r--r--Private/KernelKit/PE.hpp15
-rw-r--r--Private/KernelKit/PEF.hpp4
-rw-r--r--Private/KernelKit/ProcessManager.hpp6
-rw-r--r--Private/NewBoot/Source/FileReader.cxx22
-rw-r--r--Private/NewBoot/Source/RuntimeMain.cxx36
-rw-r--r--Private/NewKit/Defines.hpp3
-rw-r--r--Private/Source/IndexableProperty.cxx2
-rw-r--r--Private/Source/NewFS+IO.cxx (renamed from Private/Source/NewFS-IO.cxx)0
-rw-r--r--Private/Source/NewFS+Journal.cxx (renamed from Private/Source/NewFS-Journal.cxx)0
-rw-r--r--Private/Source/PEFSharedObjectRT.cxx4
-rw-r--r--Private/Source/Pmm.cxx2
-rw-r--r--Private/Source/SMPManager.cxx7
16 files changed, 76 insertions, 41 deletions
diff --git a/Private/ArchKit/ArchKit.hpp b/Private/ArchKit/ArchKit.hpp
index 1b3d7884..ae63c2a1 100644
--- a/Private/ArchKit/ArchKit.hpp
+++ b/Private/ArchKit/ArchKit.hpp
@@ -65,7 +65,7 @@ extern HCore::Array<void (*)(HCore::Int32 id, HCore::HAL::StackFrame *),
extern "C" HCore::Void rt_wait_for_io();
extern "C" HCore::Void rt_syscall_handle(HCore::HAL::StackFramePtr stackFrame);
extern "C" HCore::HAL::StackFramePtr rt_get_current_context();
-extern "C" HCore::Int32 rt_do_context_switch(
+extern "C" HCore::Void rt_do_context_switch(
HCore::HAL::StackFramePtr stackFrame);
inline HCore::VoidPtr kKernelVirtualStart;
diff --git a/Private/EFIKit/Api.hxx b/Private/EFIKit/Api.hxx
index 89e5e0a3..4a23c747 100644
--- a/Private/EFIKit/Api.hxx
+++ b/Private/EFIKit/Api.hxx
@@ -11,6 +11,8 @@
#define __EFI_API__
#include <EFIKit/EFI.hxx>
+#include <KernelKit/MSDOS.hpp>
+#include <KernelKit/PE.hpp>
inline EfiSystemTable *ST = nullptr;
inline EfiBootServices *BS = nullptr;
@@ -28,7 +30,7 @@ Bascially frees everything we have in the EFI side.
inline void ExitBootServices(UInt64 MapKey, EfiHandlePtr ImageHandle) noexcept {
if (!ST) return;
- ST->ConOut->OutputString(ST->ConOut, L"EFI: Exit BootServices...\r\n");
+ ST->ConOut->OutputString(ST->ConOut, L"HCoreLdr: Exit BootServices...\r\n");
ST->BootServices->ExitBootServices(ImageHandle, MapKey);
}
@@ -39,6 +41,8 @@ enum {
kPartCnt,
};
+inline UInt32 Platform() noexcept { return kPEMachineAMD64; }
+
/***
* @brief Raise Hard kernel error.
*/
diff --git a/Private/EFIKit/Handover.hxx b/Private/EFIKit/Handover.hxx
index 93877dfb..eb79e934 100644
--- a/Private/EFIKit/Handover.hxx
+++ b/Private/EFIKit/Handover.hxx
@@ -53,15 +53,15 @@ enum {
@brief The first struct that we read when inspecting The executable
it tells us more about it and IS format independent.
*/
-struct __attribute__((packed)) HandoverHeader final {
+typedef struct HandoverHeader final {
UInt64 f_TargetMagic;
Int32 f_TargetType;
Int32 f_TargetArch;
UIntPtr f_TargetStartAddress;
-};
+} __attribute__((packed)) HandoverHeader, *HandoverHeaderPtr;
struct HandoverInformationHeader {
- HandoverHeader* f_Header;
+ HandoverHeaderPtr f_Header;
voidPtr f_VirtualStart;
SizeT f_VirtualSize;
voidPtr f_PhysicalStart;
diff --git a/Private/KernelKit/MSDOS.hpp b/Private/KernelKit/MSDOS.hpp
index 021d62aa..4f098249 100644
--- a/Private/KernelKit/MSDOS.hpp
+++ b/Private/KernelKit/MSDOS.hpp
@@ -20,7 +20,7 @@ typedef HCore::UInt32 DosWord;
typedef HCore::Long DosLong;
typedef struct _DosHeader {
- DosWord eMagic;
+ HCore::UInt8 eMagic[2];
DosWord eMagLen;
DosWord ePagesCount;
DosWord eCrlc;
diff --git a/Private/KernelKit/PE.hpp b/Private/KernelKit/PE.hpp
index 6cc0ff60..99d6724f 100644
--- a/Private/KernelKit/PE.hpp
+++ b/Private/KernelKit/PE.hpp
@@ -27,7 +27,7 @@ typedef char CHAR;
#define kPeMagic 0x00004550
typedef struct ExecHeader final {
- U8 mMagic[4]; // PE\0\0 or 0x00004550
+ U32 mMagic; // PE\0\0 or 0x00004550
U16 mMachine;
U16 mNumberOfSections;
U32 mTimeDateStamp;
@@ -35,11 +35,14 @@ typedef struct ExecHeader final {
U32 mNumberOfSymbols;
U16 mSizeOfOptionalHeader;
U16 mCharacteristics;
-} PACKED ExecHeader, *ExecHeaderPtr;
+} ALIGN(8) ExecHeader, *ExecHeaderPtr;
#define kMagPE32 0x010b
#define kMagPE64 0x020b
+#define kPEMachineAMD64 0x8664
+#define kPEMachineARM64 0xaa64
+
typedef struct ExecOptionalHeader final {
U16 mMagic; // 0x010b - PE32, 0x020b - PE32+ (64 bit)
U8 mMajorLinkerVersion;
@@ -71,7 +74,7 @@ typedef struct ExecOptionalHeader final {
U32 mSizeOfHeapCommit;
U32 mLoaderFlags;
U32 mNumberOfRvaAndSizes;
-} PACKED ExecOptionalHeader, *ExecOptionalHeaderPtr;
+} ExecOptionalHeader, *ExecOptionalHeaderPtr;
typedef struct ExecSectionHeader final {
CHAR mName[8];
@@ -84,7 +87,7 @@ typedef struct ExecSectionHeader final {
U16 mNumberOfRelocations;
U16 mNumberOfLinenumbers;
U32 mCharacteristics;
-} PACKED ExecSectionHeader, *ExecSectionHeaderPtr;
+} ExecSectionHeader, *ExecSectionHeaderPtr;
enum kExecDataDirParams {
kExecExport,
@@ -104,7 +107,7 @@ typedef struct ExecExportDirectory {
U32 mAddressOfFunctions; // export table rva
U32 mAddressOfNames;
U32 mAddressOfNameOrdinal; // ordinal table rva
-} PACKED ExecExportDirectory, *ExecExportDirectoryPtr;
+} ExecExportDirectory, *ExecExportDirectoryPtr;
typedef struct ExecImportDirectory {
union {
@@ -115,6 +118,6 @@ typedef struct ExecImportDirectory {
U32 mForwarderChain;
U32 mNameRva;
U32 mThunkTableRva;
-} PACKED ExecImportDirectory, *ExecImportDirectoryPtr;
+} ExecImportDirectory, *ExecImportDirectoryPtr;
#endif /* ifndef __PE__ */
diff --git a/Private/KernelKit/PEF.hpp b/Private/KernelKit/PEF.hpp
index 2ffc057f..69919f93 100644
--- a/Private/KernelKit/PEF.hpp
+++ b/Private/KernelKit/PEF.hpp
@@ -63,7 +63,7 @@ typedef struct PEFContainer final {
UIntPtr Start;
SizeT HdrSz; /* Size of header */
SizeT Count; /* container header count */
-} __attribute__((packed)) PEFContainer;
+} PACKED PEFContainer;
/* First PEFCommandHeader starts after PEFContainer */
/* Last container is __exec_end */
@@ -76,7 +76,7 @@ typedef struct PEFCommandHeader final {
UInt16 Kind; /* container kind */
UIntPtr Offset; /* content offset */
SizeT Size; /* content Size */
-} __attribute__((packed)) PEFCommandHeader;
+} PACKED PEFCommandHeader;
enum {
kPefCode = 0xC,
diff --git a/Private/KernelKit/ProcessManager.hpp b/Private/KernelKit/ProcessManager.hpp
index ddde3d3c..a459dbbd 100644
--- a/Private/KernelKit/ProcessManager.hpp
+++ b/Private/KernelKit/ProcessManager.hpp
@@ -100,12 +100,16 @@ enum class ProcessSelector : Int {
using ImagePtr = VoidPtr;
using HeapPtr = VoidPtr;
-// @brief Process header structure.
+// @name Process
+// @brief Process Information Header (PIH)
+// Holds information about the running process.
+// Thread execution is being abstracted away.
class Process final {
public:
explicit Process(VoidPtr startImage = nullptr) : Image(startImage) {
MUST_PASS(startImage);
}
+
~Process() = default;
HCORE_COPY_DEFAULT(Process)
diff --git a/Private/NewBoot/Source/FileReader.cxx b/Private/NewBoot/Source/FileReader.cxx
index efd53020..2bed60ef 100644
--- a/Private/NewBoot/Source/FileReader.cxx
+++ b/Private/NewBoot/Source/FileReader.cxx
@@ -92,10 +92,6 @@ BFileReader::BFileReader(const CharacterType* path, EfiHandlePtr ImageHandle) {
}
BFileReader::~BFileReader() {
- if (this->mBlob) {
- BS->FreePool(this->mBlob);
- }
-
if (this->mFile) {
this->mFile->Close(this->mFile);
this->mFile = nullptr;
@@ -115,17 +111,21 @@ Void BFileReader::ReadAll() {
/// Allocate Handover page.
- UInt8* blob = (UInt8*)kHandoverStartKernel;
+ if (mBlob == nullptr) {
+ UInt8* blob = (UInt8*)kHandoverStartKernel;
+
+ if (BS->AllocatePages(AllocateAnyPages, EfiLoaderData, 1,
+ (EfiPhysicalAddress*)&blob) != kEfiOk) {
+ EFI::RaiseHardError(L"HCoreLdr_PageError", L"Allocation error.");
+ }
- if (BS->AllocatePages(AllocateAnyPages, EfiLoaderData, 1,
- (EfiPhysicalAddress*)&blob) != kEfiOk) {
- EFI::RaiseHardError(L"HCoreLdr_PageError", L"Allocation error.");
+ mBlob = blob;
}
- mBlob = blob;
- mSizeFile = KIB(kMaxReadSize);
+ mErrorCode = kNotSupported;
- if (mFile->Read(mFile, &mSizeFile, mBlob) != kEfiOk) return;
+ if (mFile->Read(mFile, &mSizeFile, (VoidPtr)((UIntPtr)mBlob)) != kEfiOk)
+ return;
mErrorCode = kOperationOkay;
}
diff --git a/Private/NewBoot/Source/RuntimeMain.cxx b/Private/NewBoot/Source/RuntimeMain.cxx
index 6feefea5..ff6eb730 100644
--- a/Private/NewBoot/Source/RuntimeMain.cxx
+++ b/Private/NewBoot/Source/RuntimeMain.cxx
@@ -15,6 +15,16 @@
#include <KernelKit/PE.hpp>
#include <NewKit/Ref.hpp>
+namespace Detail {
+constexpr Int32 kReadSz = 2048;
+
+auto FindPEHeader(DosHeaderPtr ptrDos) -> ExecHeaderPtr {
+ if (!ptrDos) return nullptr;
+
+ return (ExecHeaderPtr)(&ptrDos->eLfanew + 1);
+}
+} // namespace Detail
+
EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle,
EfiSystemTable* SystemTable) {
InitEFI(SystemTable);
@@ -39,21 +49,33 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle,
.WriteString(L"\r\n");
BFileReader img(L"HCOREKRNL.EXE", ImageHandle);
+
+ img.Size() = Detail::kReadSz;
img.ReadAll();
if (img.Error() == BFileReader::kOperationOkay) {
- VoidPtr blob = img.Blob();
+ UInt8* blob = (UInt8*)img.Blob();
- UInt64 MapKey = 0;
+ DosHeaderPtr ptrDos = reinterpret_cast<DosHeaderPtr>(blob);
+ ExecHeaderPtr ptrHdr = Detail::FindPEHeader(ptrDos);
- EFI::ExitBootServices(MapKey, ImageHandle);
- EFI::Stop();
+ if (ptrDos->eMagic[0] == kMagMz0 && ptrDos->eMagic[1] == kMagMz1 &&
+ ptrHdr->mMachine == EFI::Platform() && ptrHdr->mMagic == kPeMagic) {
+ UInt64 MapKey = 0;
- return kEfiOk;
+ writer.WriteString(L"HCoreLdr: Booting...").WriteString(L"\r\n");
+
+ EFI::ExitBootServices(MapKey, ImageHandle);
+
+ // Launch PE app.
+
+ EFI::Stop();
+
+ return kEfiOk;
+ }
}
- writer.WriteString(
- L"HCoreLdr: Missing HCOREKRNL.EXE! Your system is damaged.\r\n");
+ writer.WriteString(L"HCoreLdr: Error! HCOREKRNL.EXE missing or invalid!\r\n");
EFI::Stop();
diff --git a/Private/NewKit/Defines.hpp b/Private/NewKit/Defines.hpp
index f46dd5bd..3c07c1e4 100644
--- a/Private/NewKit/Defines.hpp
+++ b/Private/NewKit/Defines.hpp
@@ -57,6 +57,9 @@ using IntFast = __INT_FAST32_TYPE__;
using IntFast64 = __INT_FAST64_TYPE__;
using PtrDiff = __PTRDIFF_TYPE__;
+typedef UIntPtr *Ptr64;
+typedef UInt32 *Ptr32;
+
using Utf8Char = char8_t;
using Utf16Char = char16_t;
using WideChar = wchar_t;
diff --git a/Private/Source/IndexableProperty.cxx b/Private/Source/IndexableProperty.cxx
index d3a9fdd0..674db861 100644
--- a/Private/Source/IndexableProperty.cxx
+++ b/Private/Source/IndexableProperty.cxx
@@ -7,7 +7,7 @@
* ========================================================
*/
-//! @brief HCore NewFS Indexer.
+//! @brief Filesystem Indexer.
#include <CompilerKit/CompilerKit.hpp>
#include <FSKit/IndexableProperty.hxx>
diff --git a/Private/Source/NewFS-IO.cxx b/Private/Source/NewFS+IO.cxx
index e90ed1a4..e90ed1a4 100644
--- a/Private/Source/NewFS-IO.cxx
+++ b/Private/Source/NewFS+IO.cxx
diff --git a/Private/Source/NewFS-Journal.cxx b/Private/Source/NewFS+Journal.cxx
index 72812b6e..72812b6e 100644
--- a/Private/Source/NewFS-Journal.cxx
+++ b/Private/Source/NewFS+Journal.cxx
diff --git a/Private/Source/PEFSharedObjectRT.cxx b/Private/Source/PEFSharedObjectRT.cxx
index c13314df..c6bec404 100644
--- a/Private/Source/PEFSharedObjectRT.cxx
+++ b/Private/Source/PEFSharedObjectRT.cxx
@@ -66,7 +66,7 @@ extern "C" SharedObject *__LibInit() {
library->Get()->fImageEntrypointOffset =
library->Load<VoidPtr>(kPefStart, string_length(kPefStart, 0), kPefCode);
- kcout << "__LibInit: Task was successful!\n";
+ kcout << "__LibInit: Task is successful!\n";
return library;
}
@@ -94,7 +94,7 @@ extern "C" Void __LibFini(SharedObjectPtr lib, bool *successful) {
lib = nullptr;
- kcout << "__LibFini: Task was successful!\n";
+ kcout << "__LibFini: Task is successful!\n";
*successful = true;
}
diff --git a/Private/Source/Pmm.cxx b/Private/Source/Pmm.cxx
index 76191fdd..7eb6875e 100644
--- a/Private/Source/Pmm.cxx
+++ b/Private/Source/Pmm.cxx
@@ -23,8 +23,6 @@ Ref<PTEWrapper *> Pmm::RequestPage(Boolean user, Boolean readWrite) {
PTEWrapper *pt = m_PageManager.Leak().Request(user, readWrite, true);
if (pt) return Ref<PTEWrapper *>(pt);
-
- return {};
}
kcout << "[Pmm::RequestPage] Ref<PTEWrapper*> could not be created! "
diff --git a/Private/Source/SMPManager.cxx b/Private/Source/SMPManager.cxx
index 9b976412..487eda51 100644
--- a/Private/Source/SMPManager.cxx
+++ b/Private/Source/SMPManager.cxx
@@ -67,7 +67,8 @@ bool HardwareThread::Switch(HAL::StackFrame* stack) {
m_Stack = stack;
- return rt_do_context_switch(m_Stack) == 0;
+ rt_do_context_switch(m_Stack);
+ return true;
}
///! @brief Tells if processor is waked up.
@@ -114,11 +115,11 @@ bool SMPManager::Switch(HAL::StackFrame* stack) {
m_ThreadList[idx].Leak().Leak().Busy(true);
- Boolean ret = (rt_do_context_switch(stack) == 0);
+ rt_do_context_switch(stack);
m_ThreadList[idx].Leak().Leak().Busy(false);
- return ret;
+ return true;
}
return false;