summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAmlal EL Mahrouss <amlalelmahrouss@icloud.com>2024-08-08 14:19:42 +0200
committerAmlal EL Mahrouss <amlalelmahrouss@icloud.com>2024-08-08 14:19:42 +0200
commitdf8393cebbae61ed7686be17a28d80c657f49b7e (patch)
tree96a67cbbe80a69f2b3c59a94a517741b6583a88b
parent209ebb8caa774c8d73ead8e0eba3bd65d138930f (diff)
[IMP] Code cleanup and improvements of the bootloader, still trying to
figure what is going wrong on the kernel's DLL. Signed-off-by: Amlal EL Mahrouss <amlalelmahrouss@icloud.com>
-rw-r--r--Boot/BootKit/KernelLoader.hxx (renamed from Boot/BootKit/ProgramLoader.hxx)16
-rw-r--r--Boot/Sources/HEL/AMD64/BootMain.cxx6
-rw-r--r--Boot/Sources/HEL/AMD64/New+Delete.cxx7
-rw-r--r--Boot/Sources/KernelLoader.cxx (renamed from Boot/Sources/ProgramLoader.cxx)60
-rw-r--r--Boot/Sources/Root/bootloader.json5
-rw-r--r--Kernel/HALKit/AMD64/HalBoot.asm13
-rw-r--r--Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cxx18
-rw-r--r--Kernel/HALKit/AMD64/HalKernelMain.cxx29
-rw-r--r--Kernel/Sources/KernelCheck.cxx14
-rw-r--r--Kernel/amd64-efi.make2
-rw-r--r--Subsys/NKLogOn/Start.cxx11
11 files changed, 89 insertions, 92 deletions
diff --git a/Boot/BootKit/ProgramLoader.hxx b/Boot/BootKit/KernelLoader.hxx
index 09fff89b..3c98a238 100644
--- a/Boot/BootKit/ProgramLoader.hxx
+++ b/Boot/BootKit/KernelLoader.hxx
@@ -14,20 +14,20 @@ namespace Boot
{
using namespace Kernel;
- class ProgramLoader;
+ class KernelLoader;
/// @brief Program loader class
/// @package nl.zeta.boot.api
- class ProgramLoader final
+ class KernelLoader final
{
public:
- explicit ProgramLoader() = delete;
- ~ProgramLoader() = default;
+ explicit KernelLoader() = delete;
+ ~KernelLoader() = default;
- explicit ProgramLoader(Kernel::VoidPtr blob);
+ explicit KernelLoader(Kernel::VoidPtr blob);
- ProgramLoader& operator=(const ProgramLoader&) = default;
- ProgramLoader(const ProgramLoader&) = default;
+ KernelLoader& operator=(const KernelLoader&) = default;
+ KernelLoader(const KernelLoader&) = default;
void Start(HEL::HandoverInformationHeader* handover);
const char* GetName();
@@ -36,7 +36,7 @@ namespace Boot
private:
Char fBlobName[255];
- Char* fStackPtr{nullptr};
+ Char* fHeapForProgram{nullptr};
VoidPtr fStartAddress{nullptr};
VoidPtr fBlob{nullptr};
};
diff --git a/Boot/Sources/HEL/AMD64/BootMain.cxx b/Boot/Sources/HEL/AMD64/BootMain.cxx
index 8647d00e..0e2be7ac 100644
--- a/Boot/Sources/HEL/AMD64/BootMain.cxx
+++ b/Boot/Sources/HEL/AMD64/BootMain.cxx
@@ -16,7 +16,7 @@
#include <KernelKit/PEF.hxx>
#include <NewKit/Macros.hxx>
#include <NewKit/Ref.hxx>
-#include <BootKit/ProgramLoader.hxx>
+#include <BootKit/KernelLoader.hxx>
#include <cstring>
// make the compiler shut up.
@@ -205,7 +205,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle,
readerKernel.ReadAll(0);
- Boot::ProgramLoader* loader = nullptr;
+ Boot::KernelLoader* loader = nullptr;
// ------------------------------------------ //
// If we succeed in reading the blob, then execute it.
@@ -213,7 +213,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle,
if (readerKernel.Blob())
{
- loader = new Boot::ProgramLoader(readerKernel.Blob());
+ loader = new Boot::KernelLoader(readerKernel.Blob());
loader->SetName("\"newoskrnl.dll\" (64-bit SMP DLL)");
}
diff --git a/Boot/Sources/HEL/AMD64/New+Delete.cxx b/Boot/Sources/HEL/AMD64/New+Delete.cxx
index b651f091..7039f478 100644
--- a/Boot/Sources/HEL/AMD64/New+Delete.cxx
+++ b/Boot/Sources/HEL/AMD64/New+Delete.cxx
@@ -44,6 +44,13 @@ void operator delete(void* buf)
BS->FreePool(buf);
}
+/// @brief Deletes the object.
+/// @param buf the object.
+void operator delete[](void* buf)
+{
+ BS->FreePool(buf);
+}
+
/// @brief Deletes the object (array specific).
/// @param buf the object.
/// @param size it's size.
diff --git a/Boot/Sources/ProgramLoader.cxx b/Boot/Sources/KernelLoader.cxx
index 3b24d97b..02fa97e4 100644
--- a/Boot/Sources/ProgramLoader.cxx
+++ b/Boot/Sources/KernelLoader.cxx
@@ -4,13 +4,15 @@
------------------------------------------- */
-#include <BootKit/ProgramLoader.hxx>
+#include <BootKit/KernelLoader.hxx>
#include <BootKit/Support.hxx>
#include <BootKit/BootKit.hxx>
+#include <FirmwareKit/EFI/API.hxx>
#include <KernelKit/PEF.hxx>
#include <KernelKit/PE.hxx>
#include <KernelKit/MSDOS.hxx>
+#include <Modules/CoreCG/TextRenderer.hxx>
#include <CFKit/LoaderUtils.hxx>
EXTERN_C
@@ -18,13 +20,13 @@ EXTERN_C
#include <string.h>
}
-extern EfiBootServices* BS;
+#define kHOTypeKernel 100
+
+EXTERN EfiBootServices* BS;
namespace Boot
{
- EXTERN_C Int32 rt_jump_to_address(HEL::HandoverProc baseCode, HEL::HandoverInformationHeader* handover, Char* stackPointer);
-
- ProgramLoader::ProgramLoader(VoidPtr blob)
+ KernelLoader::KernelLoader(VoidPtr blob)
: fBlob(blob), fStartAddress(nullptr)
{
// detect the format.
@@ -46,6 +48,12 @@ namespace Boot
ExecHeaderPtr hdrPtr = ldr_find_exec_header(firstBytes);
ExecOptionalHeaderPtr optHdr = ldr_find_opt_exec_header(firstBytes);
+ if (hdrPtr->mMachine != 0x8664 &&
+ hdrPtr->mSignature != 0x20b)
+ {
+ return;
+ }
+
auto numSecs = hdrPtr->mNumberOfSections;
writer.Write("newosldr: Major Linker Ver: ").Write(optHdr->mMajorLinkerVersion).Write("\r");
@@ -66,7 +74,8 @@ namespace Boot
ExecSectionHeaderPtr sectPtr = (ExecSectionHeaderPtr)(((Char*)optHdr) + hdrPtr->mSizeOfOptionalHeader);
- constexpr auto sectionForCode = ".start";
+ constexpr auto sectionForCode = ".text";
+ constexpr auto sectionForNewLdr = ".ldr";
for (SizeT sectIndex = 0; sectIndex < numSecs; ++sectIndex)
{
@@ -74,20 +83,31 @@ namespace Boot
if (StrCmp(sectionForCode, sect->mName) == 0)
{
- fStartAddress = (VoidPtr)((UIntPtr)loadStartAddress + sect->mVirtualAddress);
+ fStartAddress = (VoidPtr)((UIntPtr)loadStartAddress + optHdr->mAddressOfEntryPoint);
writer.Write("newosldr: Start Address: ").Write((UIntPtr)fStartAddress).Write("\r");
}
+ else if (StrCmp(sectionForNewLdr, sect->mName) == 0)
+ {
+ struct HANDOVER_INFORMATION_STUB
+ {
+ UInt64 HandoverMagic;
+ UInt32 HandoverType;
+
+ }* structHandover = (struct HANDOVER_INFORMATION_STUB*)(fBlob + sect->mPointerToRawData);
+
+ if (structHandover->HandoverMagic != kHandoverMagic ||
+ structHandover->HandoverType != kHOTypeKernel)
+ {
+ cg_write_text("NEWOSLDR: INVALID HANDOVER IMAGE! ABORTING...", 40, 10, RGB(0x00, 0x00, 0x00));
+ EFI::Stop();
+ }
+ }
- writer.Write("newosldr: offset ").Write(sect->mPointerToRawData).Write(" of ").Write(sect->mName).Write(".\r");
+ writer.Write("newosldr: offset ").Write(sect->mPointerToRawData).Write(" of ").Write(sect->mName).Write("\r");
SetMem((VoidPtr)(loadStartAddress + sect->mVirtualAddress), 0, sect->mSizeOfRawData);
CopyMem((VoidPtr)(loadStartAddress + sect->mVirtualAddress), (VoidPtr)((UIntPtr)fBlob + sect->mPointerToRawData), sect->mSizeOfRawData);
}
-
- // ================================ //
- // Allocate stack.
- // ================================ //
- fStackPtr = new Char[optHdr->mSizeOfStackReserve];
}
else if (firstBytes[0] == kPefMagic[0] &&
firstBytes[1] == kPefMagic[1] &&
@@ -108,7 +128,7 @@ namespace Boot
}
/// @note handover header has to be valid!
- Void ProgramLoader::Start(HEL::HandoverInformationHeader* handover)
+ Void KernelLoader::Start(HEL::HandoverInformationHeader* handover)
{
BTextWriter writer;
@@ -119,10 +139,8 @@ namespace Boot
}
HEL::HandoverProc err_fn = [](HEL::HandoverInformationHeader* rcx) -> void {
- BTextWriter writer;
- writer.Write("newosldr: Exec format error, Thread has been aborted.\r");
-
- EFI::ThrowError(L"Exec-Format-Error", L"Format doesn't match (Thread aborted).");
+ cg_write_text("NEWOSLDR: INVALID IMAGE! ABORTING...", 40, 10, RGB(0x00, 0x00, 0x00));
+ EFI::Stop();
};
if (!fStartAddress)
@@ -134,17 +152,17 @@ namespace Boot
err_fn(handover);
}
- const Char* ProgramLoader::GetName()
+ const Char* KernelLoader::GetName()
{
return fBlobName;
}
- Void ProgramLoader::SetName(const Char* name)
+ Void KernelLoader::SetName(const Char* name)
{
CopyMem(fBlobName, name, StrLen(name));
}
- bool ProgramLoader::IsValid()
+ bool KernelLoader::IsValid()
{
return fStartAddress != nullptr;
}
diff --git a/Boot/Sources/Root/bootloader.json b/Boot/Sources/Root/bootloader.json
deleted file mode 100644
index ebfcf320..00000000
--- a/Boot/Sources/Root/bootloader.json
+++ /dev/null
@@ -1,5 +0,0 @@
-{
- "newoskrnl.dll": "krnldll",
- "ZKA/fonts.json": "rsrc",
- "ZKA/cmd.json": "cmdline"
-}
diff --git a/Kernel/HALKit/AMD64/HalBoot.asm b/Kernel/HALKit/AMD64/HalBoot.asm
index 7bda9635..aee837a0 100644
--- a/Kernel/HALKit/AMD64/HalBoot.asm
+++ b/Kernel/HALKit/AMD64/HalBoot.asm
@@ -20,16 +20,3 @@ section .ldr
HandoverMagic: dq kHandoverMagic
HandoverType: dw kTypeKernel
-HandoverArch: dw kArchAmd64
-;; This NewBootStart points to Main.
-HandoverStart: dq hal_init_platform
-
-section .start
-
-[global ke_startup_platform]
-
-ke_startup_platform:
- push rax
- jmp hal_init_platform
- pop rax
- ret \ No newline at end of file
diff --git a/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cxx b/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cxx
index dd912d2b..5c1a4323 100644
--- a/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cxx
+++ b/Kernel/HALKit/AMD64/HalCoreMultiProcessingAMD64.cxx
@@ -183,8 +183,8 @@ namespace Kernel::HAL
struct PROCESS_CONTROL_BLOCK final
{
- PROCESS_HEADER_BLOCK* f_Header;
- HAL::StackFramePtr f_StackFrame;
+ PROCESS_HEADER_BLOCK* f_Header;
+ HAL::StackFramePtr f_StackFrame;
} fBlocks[cMaxPCBBlocks] = {0};
STATIC Void hal_switch_context(HAL::StackFramePtr stackFrame)
@@ -198,7 +198,7 @@ namespace Kernel::HAL
cFramePtr = stackFrame;
- fBlocks[ProcessScheduler::The().Leak().TheCurrent().Leak().ProcessId % cMaxPCBBlocks].f_Header = &ProcessScheduler::The().Leak().TheCurrent().Leak();
+ fBlocks[ProcessScheduler::The().Leak().TheCurrent().Leak().ProcessId % cMaxPCBBlocks].f_Header = &ProcessScheduler::The().Leak().TheCurrent().Leak();
fBlocks[ProcessScheduler::The().Leak().TheCurrent().Leak().ProcessId % cMaxPCBBlocks].f_StackFrame = stackFrame;
sem.Unlock();
@@ -234,15 +234,9 @@ namespace Kernel::HAL
auto acpi = ACPIFactoryInterface(rsdPtr);
kApicMadt = acpi.Find(kApicSignature).Leak().Leak();
- if (kApicMadt != nullptr)
- {
- kApicInfoBlock = (MadtType*)kApicMadt;
- }
- else
- {
- kcout << "newoskrnl: APIC is not present! it is a vital component to enable SMP.\r";
- ke_stop(RUNTIME_CHECK_FAILED);
- }
+ MUST_PASS(kApicMadt);
+
+ kApicInfoBlock = (MadtType*)kApicMadt;
}
} // namespace Kernel::HAL
diff --git a/Kernel/HALKit/AMD64/HalKernelMain.cxx b/Kernel/HALKit/AMD64/HalKernelMain.cxx
index 3cefe1d3..bf63b388 100644
--- a/Kernel/HALKit/AMD64/HalKernelMain.cxx
+++ b/Kernel/HALKit/AMD64/HalKernelMain.cxx
@@ -89,11 +89,16 @@ EXTERN_C void hal_init_platform(
void hal_real_init(void)
{
+ // get page size.
kKernelVirtualSize = kHandoverHeader->f_VirtualSize;
+
+ // get virtual address start.
kKernelVirtualStart = reinterpret_cast<Kernel::VoidPtr>(
- reinterpret_cast<Kernel::UIntPtr>(kHandoverHeader->f_VirtualStart) + cHeapStartOffset);
+ reinterpret_cast<Kernel::UIntPtr>(kHandoverHeader->f_VirtualStart));
- kKernelPhysicalStart = kHandoverHeader->f_PhysicalStart;
+ // get physical address start.
+ kKernelPhysicalStart = reinterpret_cast<Kernel::VoidPtr>(
+ reinterpret_cast<Kernel::UIntPtr>(kHandoverHeader->f_PhysicalStart));
kTextOffsetY += 10;
cg_write_text("LOADING INTERRUPTS...", kTextOffsetY, 10, RGB(0x00, 0x00, 0x00));
@@ -116,10 +121,7 @@ void hal_real_init(void)
CONST Kernel::HAL::IDTLoader cIDT;
cIDT.Load(idtBase);
- // Register the basic SCI functions.
-
- kTextOffsetY += 10;
- cg_write_text("LOADING SYSCALLS...", kTextOffsetY, 10, RGB(0x00, 0x00, 0x00));
+ // Register the basic system calls.
constexpr auto cVGAWrite = 0x10;
constexpr auto cTlsInterrupt = 0x11;
@@ -230,20 +232,7 @@ void hal_real_init(void)
kSyscalls[cRebootInterrupt].Leak().Leak()->fHooked = true;
kSyscalls[cLPCSanitizeMsg].Leak().Leak()->fHooked = true;
- kTextOffsetY += 10;
- cg_write_text("LOADING SMP...", kTextOffsetY, 10, RGB(0x00, 0x00, 0x00));
-
- for (Kernel::SizeT i = 0; i < kHandoverMaxCmdLine; i++)
- {
- if (Kernel::rt_string_cmp(kHandoverHeader->f_CommandLine[i], "/AutoFormat", Kernel::rt_string_len("/AutoFormat")) == 0)
- {
- cAutoFormatDisk.GetValue() = Yes;
- break;
- }
- }
-
- kTextOffsetY += 10;
- cg_write_text("LOADING SECURITY SUBSYSTEM...", kTextOffsetY, 10, RGB(0x00, 0x00, 0x00));
+ Kernel::HAL::hal_system_get_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr);
Kernel::ke_stop(RUNTIME_CHECK_BOOTSTRAP);
}
diff --git a/Kernel/Sources/KernelCheck.cxx b/Kernel/Sources/KernelCheck.cxx
index b9843cd0..4752024d 100644
--- a/Kernel/Sources/KernelCheck.cxx
+++ b/Kernel/Sources/KernelCheck.cxx
@@ -19,7 +19,7 @@
#define CopyMem(dst, src, sz) Kernel::rt_copy_memory((Kernel::VoidPtr)src, (Kernel::VoidPtr)dst, sz)
#define MoveMem(dst, src, sz) Kernel::rt_copy_memory((Kernel::VoidPtr)src, (Kernel::VoidPtr)dst, sz)
-#define cWebsiteMacro "https://zka-tech.nl/hulp"
+#define cWebsiteMacro "https://zka.ma/help"
#include <BootKit/Vendor/Qr.hxx>
@@ -134,8 +134,10 @@ namespace Kernel
Void RecoveryFactory::Recover() noexcept
{
- PowerFactoryInterface powerInterface(kHandoverHeader->f_HardwareTables.f_VendorPtr);
- powerInterface.Shutdown();
+ while (true)
+ {
+ asm volatile ("cli; hlt");
+ }
}
@@ -143,12 +145,6 @@ namespace Kernel
{
if (!expr)
{
-#ifdef __DEBUG__
- kcout << "newoskrnl: FILE: " << file << "\r";
- kcout << "newoskrnl: LINE: " << line << "\r";
-
-#endif // __DEBUG__
-
ke_stop(RUNTIME_CHECK_FAILED); // Runtime Check failed
}
}
diff --git a/Kernel/amd64-efi.make b/Kernel/amd64-efi.make
index 52532223..55517b2f 100644
--- a/Kernel/amd64-efi.make
+++ b/Kernel/amd64-efi.make
@@ -35,7 +35,7 @@ COPY = cp
ASMFLAGS = -f win64
# Kernel subsystem is 17 and entrypoint is __ImageStart
-LDFLAGS = -e ke_startup_platform --subsystem=17 --image-base 0x10000000
+LDFLAGS = -e hal_init_platform --subsystem=17 --image-base 0x10000000
LDOBJ = Objects/*.obj
# This file is the kernel, responsible of task management and memory.
diff --git a/Subsys/NKLogOn/Start.cxx b/Subsys/NKLogOn/Start.cxx
new file mode 100644
index 00000000..9b335020
--- /dev/null
+++ b/Subsys/NKLogOn/Start.cxx
@@ -0,0 +1,11 @@
+/***
+ *
+ * (C) ZKA Technologies.
+ *
+ */
+
+int main(int argc, char const *argv[])
+{
+ /* code */
+ return 0;
+}