From 92e86a036219d31c56d12ba41adab51d62a26ecc Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Fri, 27 Feb 2026 05:41:16 +0100 Subject: chore: kernel, boot: updated sources copyright year and improve stability. Signed-off-by: Amlal El Mahrouss --- .../CoreFoundation.fwrk/headers/Property.h | 2 +- .../frameworks/CoreFoundation.fwrk/headers/Ref.h | 5 +- .../CoreFoundation.fwrk/src/Foundation.cc | 4 +- src/boot/amd64-ci.make | 6 +- src/boot/amd64-desktop.make | 6 +- src/boot/arm64-desktop.make | 4 +- src/boot/modules/BootNet/BootNet.cc | 129 ----------- src/boot/modules/BootNet/BootNet.cpp | 129 +++++++++++ src/boot/modules/BootNet/amd64.json | 6 +- src/boot/modules/SysChk/SysChk.cc | 36 --- src/boot/modules/SysChk/SysChk.cpp | 36 +++ src/boot/modules/SysChk/amd64-ahci-epm.json | 12 +- src/boot/modules/SysChk/amd64-ahci-gpt.json | 12 +- src/boot/modules/SysChk/amd64-pio-epm.json | 14 +- src/boot/modules/SysChk/amd64-pio-gpt.json | 14 +- src/boot/modules/SysChk/arm64.json | 2 +- src/boot/src/BootFileReader.cc | 171 -------------- src/boot/src/BootFileReader.cpp | 171 ++++++++++++++ src/boot/src/BootString.cc | 72 ------ src/boot/src/BootString.cpp | 72 ++++++ src/boot/src/BootSupport.cc | 131 ----------- src/boot/src/BootSupport.cpp | 131 +++++++++++ src/boot/src/BootTextWriter.cc | 148 ------------ src/boot/src/BootTextWriter.cpp | 148 ++++++++++++ src/boot/src/BootThread.cc | 224 ------------------ src/boot/src/BootThread.cpp | 224 ++++++++++++++++++ src/boot/src/HEL/AMD64/BootATA.cc | 253 -------------------- src/boot/src/HEL/AMD64/BootATA.cpp | 253 ++++++++++++++++++++ src/boot/src/HEL/AMD64/BootEFI.cc | 257 --------------------- src/boot/src/HEL/AMD64/BootEFI.cpp | 257 +++++++++++++++++++++ src/boot/src/HEL/AMD64/BootPlatform.cc | 33 --- src/boot/src/HEL/AMD64/BootPlatform.cpp | 33 +++ src/boot/src/HEL/AMD64/BootSATA.cc | 63 ----- src/boot/src/HEL/AMD64/BootSATA.cpp | 63 +++++ src/boot/src/HEL/ARM64/BootEFI.cc | 164 ------------- src/boot/src/HEL/ARM64/BootEFI.cpp | 164 +++++++++++++ src/boot/src/HEL/ARM64/BootPlatform.cc | 25 -- src/boot/src/HEL/ARM64/BootPlatform.cpp | 25 ++ src/boot/src/New+Delete.cc | 71 ------ src/boot/src/New+Delete.cpp | 71 ++++++ src/kernel/KernelKit/CoreProcessScheduler.h | 6 +- src/kernel/KernelKit/ILoader.h | 10 +- src/kernel/KernelKit/ZXD.h | 3 +- 43 files changed, 1830 insertions(+), 1830 deletions(-) delete mode 100644 src/boot/modules/BootNet/BootNet.cc create mode 100644 src/boot/modules/BootNet/BootNet.cpp delete mode 100644 src/boot/modules/SysChk/SysChk.cc create mode 100644 src/boot/modules/SysChk/SysChk.cpp delete mode 100644 src/boot/src/BootFileReader.cc create mode 100644 src/boot/src/BootFileReader.cpp delete mode 100644 src/boot/src/BootString.cc create mode 100644 src/boot/src/BootString.cpp delete mode 100644 src/boot/src/BootSupport.cc create mode 100644 src/boot/src/BootSupport.cpp delete mode 100644 src/boot/src/BootTextWriter.cc create mode 100644 src/boot/src/BootTextWriter.cpp delete mode 100644 src/boot/src/BootThread.cc create mode 100644 src/boot/src/BootThread.cpp delete mode 100644 src/boot/src/HEL/AMD64/BootATA.cc create mode 100644 src/boot/src/HEL/AMD64/BootATA.cpp delete mode 100644 src/boot/src/HEL/AMD64/BootEFI.cc create mode 100644 src/boot/src/HEL/AMD64/BootEFI.cpp delete mode 100644 src/boot/src/HEL/AMD64/BootPlatform.cc create mode 100644 src/boot/src/HEL/AMD64/BootPlatform.cpp delete mode 100644 src/boot/src/HEL/AMD64/BootSATA.cc create mode 100644 src/boot/src/HEL/AMD64/BootSATA.cpp delete mode 100644 src/boot/src/HEL/ARM64/BootEFI.cc create mode 100644 src/boot/src/HEL/ARM64/BootEFI.cpp delete mode 100644 src/boot/src/HEL/ARM64/BootPlatform.cc create mode 100644 src/boot/src/HEL/ARM64/BootPlatform.cpp delete mode 100644 src/boot/src/New+Delete.cc create mode 100644 src/boot/src/New+Delete.cpp diff --git a/public/frameworks/CoreFoundation.fwrk/headers/Property.h b/public/frameworks/CoreFoundation.fwrk/headers/Property.h index ddeb407a..55724def 100644 --- a/public/frameworks/CoreFoundation.fwrk/headers/Property.h +++ b/public/frameworks/CoreFoundation.fwrk/headers/Property.h @@ -1,4 +1,4 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) // Licensed under the Apache License, Version 2.0 (see LICENSE file) // Official repository: https://github.com/nekernel-org/nekernel diff --git a/public/frameworks/CoreFoundation.fwrk/headers/Ref.h b/public/frameworks/CoreFoundation.fwrk/headers/Ref.h index 10f1ef52..c7d9234c 100644 --- a/public/frameworks/CoreFoundation.fwrk/headers/Ref.h +++ b/public/frameworks/CoreFoundation.fwrk/headers/Ref.h @@ -1,5 +1,4 @@ - -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) // Licensed under the Apache License, Version 2.0 (see LICENSE file) // Official repository: https://github.com/nekernel-org/nekernel @@ -62,7 +61,7 @@ class CFRef final CF_OBJECT { /// @brief CFNonNullRef class for non-null reference counting. /// ================================================================================ template -class CFNonNullRef final { +class CFNonNullRef final CF_OBJECT { public: CFNonNullRef() = delete; CFNonNullRef(nullPtr) = delete; diff --git a/public/frameworks/CoreFoundation.fwrk/src/Foundation.cc b/public/frameworks/CoreFoundation.fwrk/src/Foundation.cc index 641e1bd0..cd3c809e 100644 --- a/public/frameworks/CoreFoundation.fwrk/src/Foundation.cc +++ b/public/frameworks/CoreFoundation.fwrk/src/Foundation.cc @@ -1,4 +1,4 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) // Licensed under the Apache License, Version 2.0 (see LICENSE file) // Official repository: https://github.com/nekernel-org/nekernel @@ -41,4 +41,4 @@ BOOL CF::CFPoint::IsWithin(CF::CFPoint& withinOf) { /***********************************************************************************/ CF::CFPoint::operator bool() { return x_1 > x_2 && y_1 > y_2; -} \ No newline at end of file +} diff --git a/src/boot/amd64-ci.make b/src/boot/amd64-ci.make index a6a1399e..845892fd 100644 --- a/src/boot/amd64-ci.make +++ b/src/boot/amd64-ci.make @@ -17,7 +17,7 @@ ifneq ($(findstring CYGWIN_NT-10.0,$(shell uname)), ) EMU=qemu-system-x86_64w.exe else # this for NT distributions -EMU=qemu-system-x86_64 +EMU=qemu-system-x86_64 endif ifeq ($(NEOS_MODEL), ) @@ -100,9 +100,9 @@ endif compile-amd64: $(WINDRES) src/boot_rsrc.rsrc -O coff -o boot_rsrc.o $(CC_GNU) $(NE_MODEL) $(STANDALONE_MACRO) $(FLAG_GNU) $(DEBUG) \ - $(wildcard src/HEL/AMD64/*.cc) \ + $(wildcard src/HEL/AMD64/*.cpp) \ $(wildcard src/HEL/AMD64/*.S) \ - $(wildcard src/*.cc) + $(wildcard src/*.cpp) mv *.o obj/ .PHONY: run-efi-amd64-ahci diff --git a/src/boot/amd64-desktop.make b/src/boot/amd64-desktop.make index 0c230f62..658ef516 100644 --- a/src/boot/amd64-desktop.make +++ b/src/boot/amd64-desktop.make @@ -17,7 +17,7 @@ ifneq ($(findstring CYGWIN_NT-10.0,$(shell uname)), ) EMU=qemu-system-x86_64w.exe else # this for NT distributions -EMU=qemu-system-x86_64 +EMU=qemu-system-x86_64 endif ifeq ($(NEOS_MODEL), ) @@ -110,9 +110,9 @@ endif compile-amd64: $(WINDRES) src/boot_rsrc.rsrc -O coff -o boot_rsrc.o $(CC_GNU) $(NE_MODEL) $(STANDALONE_MACRO) $(FLAG_GNU) $(DEBUG) \ - $(wildcard src/HEL/AMD64/*.cc) \ + $(wildcard src/HEL/AMD64/*.cpp) \ $(wildcard src/HEL/AMD64/*.S) \ - $(wildcard src/*.cc) + $(wildcard src/*.cpp) mv *.o obj/ .PHONY: run-efi-amd64-ahci diff --git a/src/boot/arm64-desktop.make b/src/boot/arm64-desktop.make index 3a418a14..bd380327 100644 --- a/src/boot/arm64-desktop.make +++ b/src/boot/arm64-desktop.make @@ -74,9 +74,9 @@ endif compile: $(RESCMD) $(CC_GNU) $(NE_MODEL) $(STANDALONE_MACRO) $(FLAG_GNU) $(DEBUG) \ - $(wildcard src/HEL/ARM64/*.cc) \ + $(wildcard src/HEL/ARM64/*.cpp) \ $(wildcard src/HEL/ARM64/*.S) \ - $(wildcard src/*.cc) + $(wildcard src/*.cpp) .PHONY: run run: diff --git a/src/boot/modules/BootNet/BootNet.cc b/src/boot/modules/BootNet/BootNet.cc deleted file mode 100644 index 353f5497..00000000 --- a/src/boot/modules/BootNet/BootNet.cc +++ /dev/null @@ -1,129 +0,0 @@ -// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include -#include - -STATIC EFI_GUID kEfiSimpleProtoGUID = EFI_SIMPLE_NETWORK_PROTOCOL_GUID; -STATIC EFI_SIMPLE_NETWORK_PROTOCOL* kEfiProtocol = nullptr; - -STATIC Void bootnet_read_ip_packet(BOOTNET_INTERNET_HEADER inet, - BOOTNET_INTERNET_HEADER** inet_out); - -EXTERN_C Int32 BootNetModuleMain(Kernel::HEL::BootInfoHeader* handover) { - fw_init_efi((EfiSystemTable*) handover->f_FirmwareCustomTables[Kernel::HEL::kHandoverTableST]); - - Boot::BootTextWriter writer; - - writer.Write("BootNet: Init BootNet...\r"); - - if (BS->LocateProtocol(&kEfiSimpleProtoGUID, nullptr, (VoidPtr*) &kEfiProtocol) != kEfiOk) { - writer.Write("BootNet: Not supported by firmware.\r"); - return kEfiFail; - } - - BOOTNET_INTERNET_HEADER inet{}; - BOOTNET_INTERNET_HEADER* inet_out = nullptr; - - SetMem(&inet, 0, sizeof(BOOTNET_INTERNET_HEADER)); - - writer.Write("BootNet: Downloading kernel...\r"); - - bootnet_read_ip_packet(inet, &inet_out); - - if (!inet_out) { - writer.Write("BootNet: Not a packet, aborting.\r"); - return kEfiFail; - } - - if (inet_out->NB1 != 'O' || inet_out->NB1 != 'N' || inet_out->NB1 != 'E' || - inet_out->NB1 != 'T') { - writer.Write("BootNet: Not a packet, aborting.\r"); - return kEfiFail; - } - - if (inet_out->Length < 1) { - writer.Write("BootNet: No executable attached to the packet, aborting.\r"); - return kEfiFail; - } - - if (inet_out->Version != kBootNetVersion) { - writer.Write("BootNet: The version clashes, not good.\r"); - return kEfiFail; - } - - if (!inet_out->ImpliesProgram) { - Boot::BootThread thread(inet_out->Data); - - if (thread.IsValid()) { - writer.Write("BootNet: Running NeKernel...\r"); - return thread.Start(handover, YES); - } - - return kEfiFail; - } else { - constexpr auto kROMSize = 0x200; - - if (inet_out->Length > kROMSize) { - writer.Write("BootNet: Not within 512K, won't flash EEPROM.\r"); - return kEfiFail; - } - - writer.Write("BootNet: Programming the flash...\r"); - - /// TODO: Program new firmware to EEPROM (if crc and size matches) - - const ATTRIBUTE(unused) UIntPtr kEEPROMStartAddress = 0; - const ATTRIBUTE(unused) UInt16 kEEPROMSize = inet_out->Length; - - return kEfiFail; - } - - return kEfiFail; -} - -STATIC Void bootnet_read_ip_packet(BOOTNET_INTERNET_HEADER inet, - BOOTNET_INTERNET_HEADER** inet_out) { - NE_UNUSED(inet); - - kEfiProtocol->Start(kEfiProtocol); - - UInt32 size_inet = sizeof(inet); - - /// Connect to the local BootNet server. - - auto len = inet.Length; - - /// And receive the handshake packet. - if (kEfiProtocol->Receive(kEfiProtocol, &size_inet, (UInt32*) &len, (VoidPtr) &inet, nullptr, - nullptr, nullptr) == kEfiOk) { - BOOTNET_INTERNET_HEADER* out = nullptr; - - BS->AllocatePool(EfiLoaderData, sizeof(BOOTNET_INTERNET_HEADER) + inet.Length, (VoidPtr*) &out); - - if (out == nullptr) { - kEfiProtocol->Stop(kEfiProtocol); - kEfiProtocol->Shutdown(kEfiProtocol); - return; - } - - SetMem(out, 0, sizeof(BOOTNET_INTERNET_HEADER)); - SetMem(out->Data, 0, inet.Length); - - size_inet = sizeof(BOOTNET_INTERNET_HEADER); - - auto full_size = size_inet + inet.Length; - - /// Ask for it again since we know the full_size variable now. - kEfiProtocol->Receive(kEfiProtocol, &size_inet, (UInt32*) &full_size, (VoidPtr) out, nullptr, - nullptr, nullptr); - - *inet_out = out; - } - - kEfiProtocol->Stop(kEfiProtocol); - kEfiProtocol->Shutdown(kEfiProtocol); -} diff --git a/src/boot/modules/BootNet/BootNet.cpp b/src/boot/modules/BootNet/BootNet.cpp new file mode 100644 index 00000000..28c72110 --- /dev/null +++ b/src/boot/modules/BootNet/BootNet.cpp @@ -0,0 +1,129 @@ +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include +#include + +STATIC EFI_GUID kEfiSimpleProtoGUID = EFI_SIMPLE_NETWORK_PROTOCOL_GUID; +STATIC EFI_SIMPLE_NETWORK_PROTOCOL* kEfiProtocol = nullptr; + +STATIC Void bootnet_read_ip_packet(BOOTNET_INTERNET_HEADER inet, + BOOTNET_INTERNET_HEADER** inet_out); + +EXTERN_C Int32 BootNetModuleMain(Kernel::HEL::BootInfoHeader* handover) { + fw_init_efi((EfiSystemTable*) handover->f_FirmwareCustomTables[Kernel::HEL::kHandoverTableST]); + + Boot::BootTextWriter writer; + + writer.Write("Net: Init BootNet...\r"); + + if (BS->LocateProtocol(&kEfiSimpleProtoGUID, nullptr, (VoidPtr*) &kEfiProtocol) != kEfiOk) { + writer.Write("Net: Not supported by the current firmware.\r"); + return kEfiFail; + } + + BOOTNET_INTERNET_HEADER inet{}; + BOOTNET_INTERNET_HEADER* inet_out = nullptr; + + SetMem(&inet, 0, sizeof(BOOTNET_INTERNET_HEADER)); + + writer.Write("Net: Downloading image...\r"); + + bootnet_read_ip_packet(inet, &inet_out); + + if (!inet_out) { + writer.Write("Net: Not a packet, aborting.\r"); + return kEfiFail; + } + + if (inet_out->NB1 != 'O' || inet_out->NB1 != 'N' || inet_out->NB1 != 'E' || + inet_out->NB1 != 'T') { + writer.Write("Net: Not a packet, aborting.\r"); + return kEfiFail; + } + + if (inet_out->Length < 1) { + writer.Write("Net: No executable attached to the packet, aborting.\r"); + return kEfiFail; + } + + if (inet_out->Version != kBootNetVersion) { + writer.Write("Net: The version clashes, not good.\r"); + return kEfiFail; + } + + if (!inet_out->ImpliesProgram) { + Boot::BootThread thread(inet_out->Data); + + if (thread.IsValid()) { + writer.Write("Net: Running image...\r"); + return thread.Start(handover, YES); + } + + return kEfiFail; + } else { + constexpr auto kROMSize = 0x200; + + if (inet_out->Length > kROMSize) { + writer.Write("Net: Not an image within 512K, we won't flash the EEPROM.\r"); + return kEfiFail; + } + + writer.Write("Net: Programming the EEPROM...\r"); + + /// TODO: Program new firmware to EEPROM (if crc and size matches) + + const ATTRIBUTE(unused) UIntPtr kEEPROMStartAddress = 0; + const ATTRIBUTE(unused) UInt16 kEEPROMSize = inet_out->Length; + + return kEfiFail; + } + + return kEfiFail; +} + +STATIC Void bootnet_read_ip_packet(BOOTNET_INTERNET_HEADER inet, + BOOTNET_INTERNET_HEADER** inet_out) { + NE_UNUSED(inet); + + kEfiProtocol->Start(kEfiProtocol); + + UInt32 size_inet = sizeof(inet); + + /// Connect to the local BootNet server. + + auto len = inet.Length; + + /// And receive the handshake packet. + if (kEfiProtocol->Receive(kEfiProtocol, &size_inet, (UInt32*) &len, (VoidPtr) &inet, nullptr, + nullptr, nullptr) == kEfiOk) { + BOOTNET_INTERNET_HEADER* out = nullptr; + + BS->AllocatePool(EfiLoaderData, sizeof(BOOTNET_INTERNET_HEADER) + inet.Length, (VoidPtr*) &out); + + if (out == nullptr) { + kEfiProtocol->Stop(kEfiProtocol); + kEfiProtocol->Shutdown(kEfiProtocol); + return; + } + + SetMem(out, 0, sizeof(BOOTNET_INTERNET_HEADER)); + SetMem(out->Data, 0, inet.Length); + + size_inet = sizeof(BOOTNET_INTERNET_HEADER); + + auto full_size = size_inet + inet.Length; + + /// Ask for it again since we know the full_size variable now. + kEfiProtocol->Receive(kEfiProtocol, &size_inet, (UInt32*) &full_size, (VoidPtr) out, nullptr, + nullptr, nullptr); + + *inet_out = out; + } + + kEfiProtocol->Stop(kEfiProtocol); + kEfiProtocol->Shutdown(kEfiProtocol); +} diff --git a/src/boot/modules/BootNet/amd64.json b/src/boot/modules/BootNet/amd64.json index 1f28e837..41ce26e1 100644 --- a/src/boot/modules/BootNet/amd64.json +++ b/src/boot/modules/BootNet/amd64.json @@ -9,11 +9,11 @@ "./" ], "sources_path": [ - "*.cc", + "*.cpp", "*.S", - "../../src/HEL/AMD64/*.cc", + "../../src/HEL/AMD64/*.cpp", "../../src/HEL/AMD64/*.S", - "../../src/*.cc" + "../../src/*.cpp" ], "output_name": "net.efi", "compiler_flags": [ diff --git a/src/boot/modules/SysChk/SysChk.cc b/src/boot/modules/SysChk/SysChk.cc deleted file mode 100644 index ebc2be68..00000000 --- a/src/boot/modules/SysChk/SysChk.cc +++ /dev/null @@ -1,36 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -// Makes the compiler shut up. -#ifndef kMachineModel -#define kMachineModel "OS" -#endif // !kMachineModel - -EXTERN_C Int32 SysChkModuleMain(Kernel::HEL::BootInfoHeader* handover) { - fw_init_efi((EfiSystemTable*) handover->f_FirmwareCustomTables[Kernel::HEL::kHandoverTableST]); - -#if defined(__ATA_PIO__) - Boot::BDiskFormatFactory partition_factory; -#elif defined(__AHCI__) - Boot::BDiskFormatFactory partition_factory; -#endif - - if (partition_factory.IsPartitionValid()) return kEfiOk; - - return partition_factory.Format(kMachineModel); -} diff --git a/src/boot/modules/SysChk/SysChk.cpp b/src/boot/modules/SysChk/SysChk.cpp new file mode 100644 index 00000000..ebc2be68 --- /dev/null +++ b/src/boot/modules/SysChk/SysChk.cpp @@ -0,0 +1,36 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +// Makes the compiler shut up. +#ifndef kMachineModel +#define kMachineModel "OS" +#endif // !kMachineModel + +EXTERN_C Int32 SysChkModuleMain(Kernel::HEL::BootInfoHeader* handover) { + fw_init_efi((EfiSystemTable*) handover->f_FirmwareCustomTables[Kernel::HEL::kHandoverTableST]); + +#if defined(__ATA_PIO__) + Boot::BDiskFormatFactory partition_factory; +#elif defined(__AHCI__) + Boot::BDiskFormatFactory partition_factory; +#endif + + if (partition_factory.IsPartitionValid()) return kEfiOk; + + return partition_factory.Format(kMachineModel); +} diff --git a/src/boot/modules/SysChk/amd64-ahci-epm.json b/src/boot/modules/SysChk/amd64-ahci-epm.json index 83282c3f..444003cf 100644 --- a/src/boot/modules/SysChk/amd64-ahci-epm.json +++ b/src/boot/modules/SysChk/amd64-ahci-epm.json @@ -3,14 +3,14 @@ "compiler_std": "c++20", "headers_path": ["../", "../../", "../../../kernel", "../../../", "./"], "sources_path": [ - "*.cc", + "*.cpp", "*.S", - "../../src/HEL/AMD64/BootSATA.cc", - "../../src/HEL/AMD64/BootPlatform.cc", + "../../src/HEL/AMD64/BootSATA.cpp", + "../../src/HEL/AMD64/BootPlatform.cpp", "../../src/HEL/AMD64/BootAPI.S", - "../../src/BootTextWriter.cc", - "../../src/BootSupport.cc", - "../../src/New+Delete.cc", + "../../src/BootTextWriter.cpp", + "../../src/BootSupport.cpp", + "../../src/New+Delete.cpp", "../../../kernel/HALKit/AMD64/PCI/*.cpp", "../../../kernel/HALKit/AMD64/Storage/*.cpp", "../../../kernel/src/Storage/*.cpp", diff --git a/src/boot/modules/SysChk/amd64-ahci-gpt.json b/src/boot/modules/SysChk/amd64-ahci-gpt.json index 8fcf2a57..9b0f4546 100644 --- a/src/boot/modules/SysChk/amd64-ahci-gpt.json +++ b/src/boot/modules/SysChk/amd64-ahci-gpt.json @@ -3,14 +3,14 @@ "compiler_std": "c++20", "headers_path": ["../", "../../", "../../../kernel", "../../../", "./"], "sources_path": [ - "*.cc", + "*.cpp", "*.S", - "../../src/HEL/AMD64/BootSATA.cc", - "../../src/HEL/AMD64/BootPlatform.cc", + "../../src/HEL/AMD64/BootSATA.cpp", + "../../src/HEL/AMD64/BootPlatform.cpp", "../../src/HEL/AMD64/BootAPI.S", - "../../src/BootTextWriter.cc", - "../../src/BootSupport.cc", - "../../src/New+Delete.cc", + "../../src/BootTextWriter.cpp", + "../../src/BootSupport.cpp", + "../../src/New+Delete.cpp", "../../../kernel/HALKit/AMD64/PCI/*.cpp", "../../../kernel/HALKit/AMD64/Storage/*.cpp", "../../../kernel/src/Storage/*.cpp", diff --git a/src/boot/modules/SysChk/amd64-pio-epm.json b/src/boot/modules/SysChk/amd64-pio-epm.json index 12d47119..0c4a1c59 100644 --- a/src/boot/modules/SysChk/amd64-pio-epm.json +++ b/src/boot/modules/SysChk/amd64-pio-epm.json @@ -9,14 +9,14 @@ "./" ], "sources_path": [ - "*.cc", + "*.cpp", "*.S", - "../../src/HEL/AMD64/BootATA.cc", - "../../src/HEL/AMD64/BootPlatform.cc", + "../../src/HEL/AMD64/BootATA.cpp", + "../../src/HEL/AMD64/BootPlatform.cpp", "../../src/HEL/AMD64/BootAPI.S", - "../../src/BootTextWriter.cc", - "../../src/BootSupport.cc", - "../../src/New+Delete.cc" + "../../src/BootTextWriter.cpp", + "../../src/BootSupport.cpp", + "../../src/New+Delete.cpp" ], "output_name": "chk.efi", "compiler_flags": [ @@ -42,4 +42,4 @@ "kChkVersionLowest=0x0100", "kChkVersion=0x0100" ] -} \ No newline at end of file +} diff --git a/src/boot/modules/SysChk/amd64-pio-gpt.json b/src/boot/modules/SysChk/amd64-pio-gpt.json index f68d5d36..ca564067 100644 --- a/src/boot/modules/SysChk/amd64-pio-gpt.json +++ b/src/boot/modules/SysChk/amd64-pio-gpt.json @@ -9,14 +9,14 @@ "./" ], "sources_path": [ - "*.cc", + "*.cpp", "*.S", - "../../src/HEL/AMD64/BootATA.cc", - "../../src/HEL/AMD64/BootPlatform.cc", + "../../src/HEL/AMD64/BootATA.cpp", + "../../src/HEL/AMD64/BootPlatform.cpp", "../../src/HEL/AMD64/BootAPI.S", - "../../src/BootTextWriter.cc", - "../../src/BootSupport.cc", - "../../src/New+Delete.cc" + "../../src/BootTextWriter.cpp", + "../../src/BootSupport.cpp", + "../../src/New+Delete.cpp" ], "output_name": "chk.efi", "compiler_flags": [ @@ -42,4 +42,4 @@ "kChkVersionLowest=0x0100", "kChkVersion=0x0100" ] -} \ No newline at end of file +} diff --git a/src/boot/modules/SysChk/arm64.json b/src/boot/modules/SysChk/arm64.json index ad5fde6e..2a04c772 100644 --- a/src/boot/modules/SysChk/arm64.json +++ b/src/boot/modules/SysChk/arm64.json @@ -2,7 +2,7 @@ "compiler_path": "clang++", "compiler_std": "c++20", "headers_path": ["../", "../../", "../../../kernel", "../../../", "./"], - "sources_path": ["*.cc", "*.S", "../../src/HEL/ARM64/*.cc", "../../src/HEL/ARM64/*.S", "../../src/*.cc"], + "sources_path": ["*.cpp", "*.S", "../../src/HEL/ARM64/*.cpp", "../../src/HEL/ARM64/*.S", "../../src/*.cpp"], "output_name": "chk.efi", "compiler_flags": [ "-ffreestanding", diff --git a/src/boot/src/BootFileReader.cc b/src/boot/src/BootFileReader.cc deleted file mode 100644 index 9deeeb4b..00000000 --- a/src/boot/src/BootFileReader.cc +++ /dev/null @@ -1,171 +0,0 @@ -// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include -#include -#include -#include - -/// @file BootFileReader -/// @brief Bootloader File reader. -/// BUGS: 0 - -//////////////////////////////////////////////////////////////////////////////////////////////////// -/// -/// -/// @name BootFileReader class -/// @brief Reads the file as a blob. -/// -/// -//////////////////////////////////////////////////////////////////////////////////////////////////// - -/*** - @brief File Reader constructor. -*/ -Boot::BootFileReader::BootFileReader(const CharacterTypeUTF16* path, EfiHandlePtr ImageHandle) { - if (path != nullptr) { - SizeT index = 0UL; - for (; path[index] != L'\0'; ++index) { - mPath[index] = path[index]; - } - - mPath[index] = 0; - } - - /// Load protocols with their GUIDs. - - EFI_GUID guidEfp = EFI_GUID(EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID); - - EfiSimpleFilesystemProtocol* efp = nullptr; - - EfiLoadImageProtocol* img = nullptr; - EFI_GUID guidImg = EFI_GUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); - - if (BS->HandleProtocol(ImageHandle, &guidImg, (void**) &img) != kEfiOk) { - mWriter.Write(L"BootZ: Handle-Protocol: No-Such-Protocol").Write(L"\r"); - this->mErrorCode = kNotSupported; - } - - if (BS->HandleProtocol(img->DeviceHandle, &guidEfp, (void**) &efp) != kEfiOk) { - mWriter.Write(L"BootZ: Handle-Protocol: No-Such-Protocol").Write(L"\r"); - this->mErrorCode = kNotSupported; - return; - } - - /// Start doing disk I/O - - if (efp->OpenVolume(efp, &mRootFs) != kEfiOk) { - mWriter.Write(L"BootZ: Fetch-Protocol: No-Such-Volume").Write(L"\r"); - this->mErrorCode = kNotSupported; - return; - } - - EfiFileProtocol* fileFs = nullptr; - - if (mRootFs->Open(mRootFs, &fileFs, mPath, kEFIFileRead, kEFIReadOnly) != kEfiOk) { - mWriter.Write(L"BootZ: Fetch-Protocol: No-Such-Path: ").Write(mPath).Write(L"\r"); - this->mErrorCode = kNotSupported; - - cg_render_string("BOOTZ: PLEASE RECOVER YOUR NEKERNEL INSTALL.", 40, 10, RGB(0xFF, 0xFF, 0xFF)); - - mRootFs->Close(mRootFs); - - return; - } - - mSizeFile = 0; - mFile = fileFs; - mErrorCode = kOperationOkay; -} - -Boot::BootFileReader::~BootFileReader() { - if (this->mFile) { - this->mFile->Close(this->mFile); - this->mFile = nullptr; - } - - if (this->mRootFs) { - this->mRootFs->Close(this->mRootFs); - this->mRootFs = nullptr; - } - - if (this->mBlob) { - BS->FreePool(this->mBlob); - this->mBlob = nullptr; - } - - BSetMem(this->mPath, 0, kPathLen); -} - -/** - @brief Reads all of the file into a buffer. - @param **readUntil** size of file - @param **chunkToRead** chunk to read each time. -*/ -Void Boot::BootFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead, UIntPtr out_address) { - UInt32 szInfo = sizeof(EfiFileInfo); - - EfiFileInfo newPtrInfo{}; - - EFI_GUID kFileInfoGUID = EFI_FILE_INFO_GUID; - - if (mFile->GetInfo(mFile, &kFileInfoGUID, &szInfo, &newPtrInfo) == kEfiOk) { - readUntil = newPtrInfo.FileSize; - mWriter.Write(L"BootZ: File-Size: ").Write(readUntil).Write("\r"); - } - - if (readUntil == 0) { - mErrorCode = kNotSupported; - return; - } - - if (mBlob == nullptr) { - if (!out_address) { - if (auto err = BS->AllocatePool(EfiLoaderCode, readUntil, (VoidPtr*) &mBlob) != kEfiOk) { - mWriter.Write(L"*** error: ").Write(err).Write(L" ***\r"); - Boot::ThrowError(L"OutOfMemory", L"Out of memory."); - } - } else { - mBlob = (VoidPtr) out_address; - } - } - - mWriter.Write(L"*** Bytes to read: ").Write(readUntil).Write(L" ***\r"); - - UInt64 bufSize = chunkToRead; - UInt64 szCnt = 0UL; - - while (szCnt < readUntil) { - auto res = mFile->Read(mFile, &bufSize, (VoidPtr) (&((Char*) mBlob)[szCnt])); - - szCnt += bufSize; - - if (res == kBufferTooSmall) { - bufSize = chunkToRead; - } - } - - mSizeFile = szCnt; - mErrorCode = kOperationOkay; -} - -/// @brief error code getter. -/// @return the error code. -Int32& Boot::BootFileReader::Error() { - return mErrorCode; -} - -/// @brief blob getter. -/// @return the blob. -VoidPtr Boot::BootFileReader::Blob() { - return mBlob; -} - -/// @breif Size getter. -/// @return the size of the file. -UInt64& Boot::BootFileReader::Size() { - return mSizeFile; -} diff --git a/src/boot/src/BootFileReader.cpp b/src/boot/src/BootFileReader.cpp new file mode 100644 index 00000000..9deeeb4b --- /dev/null +++ b/src/boot/src/BootFileReader.cpp @@ -0,0 +1,171 @@ +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include +#include +#include +#include + +/// @file BootFileReader +/// @brief Bootloader File reader. +/// BUGS: 0 + +//////////////////////////////////////////////////////////////////////////////////////////////////// +/// +/// +/// @name BootFileReader class +/// @brief Reads the file as a blob. +/// +/// +//////////////////////////////////////////////////////////////////////////////////////////////////// + +/*** + @brief File Reader constructor. +*/ +Boot::BootFileReader::BootFileReader(const CharacterTypeUTF16* path, EfiHandlePtr ImageHandle) { + if (path != nullptr) { + SizeT index = 0UL; + for (; path[index] != L'\0'; ++index) { + mPath[index] = path[index]; + } + + mPath[index] = 0; + } + + /// Load protocols with their GUIDs. + + EFI_GUID guidEfp = EFI_GUID(EFI_SIMPLE_FILE_SYSTEM_PROTOCOL_GUID); + + EfiSimpleFilesystemProtocol* efp = nullptr; + + EfiLoadImageProtocol* img = nullptr; + EFI_GUID guidImg = EFI_GUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); + + if (BS->HandleProtocol(ImageHandle, &guidImg, (void**) &img) != kEfiOk) { + mWriter.Write(L"BootZ: Handle-Protocol: No-Such-Protocol").Write(L"\r"); + this->mErrorCode = kNotSupported; + } + + if (BS->HandleProtocol(img->DeviceHandle, &guidEfp, (void**) &efp) != kEfiOk) { + mWriter.Write(L"BootZ: Handle-Protocol: No-Such-Protocol").Write(L"\r"); + this->mErrorCode = kNotSupported; + return; + } + + /// Start doing disk I/O + + if (efp->OpenVolume(efp, &mRootFs) != kEfiOk) { + mWriter.Write(L"BootZ: Fetch-Protocol: No-Such-Volume").Write(L"\r"); + this->mErrorCode = kNotSupported; + return; + } + + EfiFileProtocol* fileFs = nullptr; + + if (mRootFs->Open(mRootFs, &fileFs, mPath, kEFIFileRead, kEFIReadOnly) != kEfiOk) { + mWriter.Write(L"BootZ: Fetch-Protocol: No-Such-Path: ").Write(mPath).Write(L"\r"); + this->mErrorCode = kNotSupported; + + cg_render_string("BOOTZ: PLEASE RECOVER YOUR NEKERNEL INSTALL.", 40, 10, RGB(0xFF, 0xFF, 0xFF)); + + mRootFs->Close(mRootFs); + + return; + } + + mSizeFile = 0; + mFile = fileFs; + mErrorCode = kOperationOkay; +} + +Boot::BootFileReader::~BootFileReader() { + if (this->mFile) { + this->mFile->Close(this->mFile); + this->mFile = nullptr; + } + + if (this->mRootFs) { + this->mRootFs->Close(this->mRootFs); + this->mRootFs = nullptr; + } + + if (this->mBlob) { + BS->FreePool(this->mBlob); + this->mBlob = nullptr; + } + + BSetMem(this->mPath, 0, kPathLen); +} + +/** + @brief Reads all of the file into a buffer. + @param **readUntil** size of file + @param **chunkToRead** chunk to read each time. +*/ +Void Boot::BootFileReader::ReadAll(SizeT readUntil, SizeT chunkToRead, UIntPtr out_address) { + UInt32 szInfo = sizeof(EfiFileInfo); + + EfiFileInfo newPtrInfo{}; + + EFI_GUID kFileInfoGUID = EFI_FILE_INFO_GUID; + + if (mFile->GetInfo(mFile, &kFileInfoGUID, &szInfo, &newPtrInfo) == kEfiOk) { + readUntil = newPtrInfo.FileSize; + mWriter.Write(L"BootZ: File-Size: ").Write(readUntil).Write("\r"); + } + + if (readUntil == 0) { + mErrorCode = kNotSupported; + return; + } + + if (mBlob == nullptr) { + if (!out_address) { + if (auto err = BS->AllocatePool(EfiLoaderCode, readUntil, (VoidPtr*) &mBlob) != kEfiOk) { + mWriter.Write(L"*** error: ").Write(err).Write(L" ***\r"); + Boot::ThrowError(L"OutOfMemory", L"Out of memory."); + } + } else { + mBlob = (VoidPtr) out_address; + } + } + + mWriter.Write(L"*** Bytes to read: ").Write(readUntil).Write(L" ***\r"); + + UInt64 bufSize = chunkToRead; + UInt64 szCnt = 0UL; + + while (szCnt < readUntil) { + auto res = mFile->Read(mFile, &bufSize, (VoidPtr) (&((Char*) mBlob)[szCnt])); + + szCnt += bufSize; + + if (res == kBufferTooSmall) { + bufSize = chunkToRead; + } + } + + mSizeFile = szCnt; + mErrorCode = kOperationOkay; +} + +/// @brief error code getter. +/// @return the error code. +Int32& Boot::BootFileReader::Error() { + return mErrorCode; +} + +/// @brief blob getter. +/// @return the blob. +VoidPtr Boot::BootFileReader::Blob() { + return mBlob; +} + +/// @breif Size getter. +/// @return the size of the file. +UInt64& Boot::BootFileReader::Size() { + return mSizeFile; +} diff --git a/src/boot/src/BootString.cc b/src/boot/src/BootString.cc deleted file mode 100644 index 6cda5f24..00000000 --- a/src/boot/src/BootString.cc +++ /dev/null @@ -1,72 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include - -/// BUGS: 0 - -///////////////////////////////////////////////////////////////////////////////////////////////////////// - -Kernel::SizeT Boot::BCopyMem(CharacterTypeUTF16* dest, CharacterTypeUTF16* src, - const Kernel::SizeT len) { - if (!dest || !src) return 0; - - SizeT index = 0UL; - for (; index < len; ++index) { - dest[index] = src[index]; - } - - return index; -} - -Kernel::SizeT Boot::BStrLen(const CharacterTypeUTF16* ptr) { - if (!ptr) return 0; - - Kernel::SizeT cnt = 0; - - while (*ptr != (CharacterTypeUTF16) 0) { - ++ptr; - ++cnt; - } - - return cnt; -} - -Kernel::SizeT Boot::BSetMem(CharacterTypeUTF16* src, const CharacterTypeUTF16 byte, - const Kernel::SizeT len) { - if (!src) return 0; - - Kernel::SizeT cnt = 0UL; - - while (*src != 0) { - if (cnt > len) break; - - *src = byte; - ++src; - - ++cnt; - } - - return cnt; -} - -Kernel::SizeT Boot::BSetMem(CharacterTypeASCII* src, const CharacterTypeASCII byte, - const Kernel::SizeT len) { - if (!src) return 0; - - Kernel::SizeT cnt = 0UL; - - while (*src != 0) { - if (cnt > len) break; - - *src = byte; - ++src; - - ++cnt; - } - - return cnt; -} diff --git a/src/boot/src/BootString.cpp b/src/boot/src/BootString.cpp new file mode 100644 index 00000000..6cda5f24 --- /dev/null +++ b/src/boot/src/BootString.cpp @@ -0,0 +1,72 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include + +/// BUGS: 0 + +///////////////////////////////////////////////////////////////////////////////////////////////////////// + +Kernel::SizeT Boot::BCopyMem(CharacterTypeUTF16* dest, CharacterTypeUTF16* src, + const Kernel::SizeT len) { + if (!dest || !src) return 0; + + SizeT index = 0UL; + for (; index < len; ++index) { + dest[index] = src[index]; + } + + return index; +} + +Kernel::SizeT Boot::BStrLen(const CharacterTypeUTF16* ptr) { + if (!ptr) return 0; + + Kernel::SizeT cnt = 0; + + while (*ptr != (CharacterTypeUTF16) 0) { + ++ptr; + ++cnt; + } + + return cnt; +} + +Kernel::SizeT Boot::BSetMem(CharacterTypeUTF16* src, const CharacterTypeUTF16 byte, + const Kernel::SizeT len) { + if (!src) return 0; + + Kernel::SizeT cnt = 0UL; + + while (*src != 0) { + if (cnt > len) break; + + *src = byte; + ++src; + + ++cnt; + } + + return cnt; +} + +Kernel::SizeT Boot::BSetMem(CharacterTypeASCII* src, const CharacterTypeASCII byte, + const Kernel::SizeT len) { + if (!src) return 0; + + Kernel::SizeT cnt = 0UL; + + while (*src != 0) { + if (cnt > len) break; + + *src = byte; + ++src; + + ++cnt; + } + + return cnt; +} diff --git a/src/boot/src/BootSupport.cc b/src/boot/src/BootSupport.cc deleted file mode 100644 index cb6d9d0b..00000000 --- a/src/boot/src/BootSupport.cc +++ /dev/null @@ -1,131 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include -#include -#include -#include -#include - -#ifdef __BOOTZ_STANDALONE__ -/// @note This function is a stub, not implemented by the bootloader as of right now. (AMLALE) -EXTERN_C int atexit(void (*f)()) { - NE_UNUSED(f); - return 0; -} - -/// @brief memset definition in C++. -/// @param dst destination pointer. -/// @param byte value to fill in. -/// @param len length of of src. -EXTERN_C VoidPtr memnset(void* dst, int byte, long long unsigned int len, - long long unsigned int dst_size) { - if (!dst || len > dst_size) { - // For now, we return nullptr or an error status. - return nullptr; - } - unsigned char* p = (unsigned char*) dst; - unsigned char val = (unsigned char) byte; - for (size_t i = 0UL; i < len; ++i) { - p[i] = val; - } - return dst; -} - -/// @brief memcpy definition in C++. -/// @param dst destination pointer. -/// @param src source pointer. -/// @param len length of of src. -EXTERN_C VoidPtr memncpy(void* dst, const void* src, long long unsigned int len, - long long unsigned int dst_size) { - if (!dst || !src || len > dst_size) { - // Similar to memset, this is a critical failure. - return nullptr; - } - unsigned char* d = (unsigned char*) dst; - const unsigned char* s = (const unsigned char*) src; - for (size_t i = 0UL; i < len; ++i) { - d[i] = s[i]; - } - return dst; -} - -/// @brief strlen definition in C++. -EXTERN_C size_t strnlen(const char* whatToCheck, size_t max_len) { - size_t len = 0; - while (len < max_len && whatToCheck[len] != '\0') { - ++len; - } - return len; -} - -/// @brief strcmp definition in C++. -EXTERN_C int strncmp(const char* whatToCheck, const char* whatToCheckRight, size_t max_len) { - size_t i = 0; - while (i < max_len && whatToCheck[i] == whatToCheckRight[i]) { - if (whatToCheck[i] == '\0') return 0; - ++i; - } - if (i == max_len) { - return 0; - } - return (unsigned char) whatToCheck[i] - (unsigned char) whatToCheckRight[i]; -} - -/// @brief something specific to the Microsoft's ABI, When the stack grows too big. -EXTERN_C void ___chkstk_ms(void) {} - -/// @note GCC expects them to be here. - -/// @brief memset definition in C++. -/// @param dst destination pointer. -/// @param byte value to fill in. -/// @param len length of of src. -EXTERN_C VoidPtr memset(void* dst, int byte, long long unsigned int len) { - for (size_t i = 0UL; i < len; ++i) { - ((int*) dst)[i] = byte; - } - - return dst; -} - -/// @brief memcpy definition in C++. -/// @param dst destination pointer. -/// @param src source pointer. -/// @param len length of of src. -EXTERN_C VoidPtr memcpy(void* dst, const void* src, long long unsigned int len) { - for (size_t i = 0UL; i < len; ++i) { - ((int*) dst)[i] = ((int*) src)[i]; - } - - return dst; -} - -/// @brief strlen definition in C++. -EXTERN_C size_t strlen(const char* whatToCheck) { - SizeT len = 0; - - while (whatToCheck[len] != 0) { - ++len; - } - - return len; -} - -/// @brief strcmp definition in C++. -EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight) { - SizeT len = 0; - - while (whatToCheck[len] == whatToCheckRight[len]) { - if (whatToCheck[len] == 0) return 0; - - ++len; - } - - return len; -} - -#endif diff --git a/src/boot/src/BootSupport.cpp b/src/boot/src/BootSupport.cpp new file mode 100644 index 00000000..cb6d9d0b --- /dev/null +++ b/src/boot/src/BootSupport.cpp @@ -0,0 +1,131 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include +#include +#include +#include +#include + +#ifdef __BOOTZ_STANDALONE__ +/// @note This function is a stub, not implemented by the bootloader as of right now. (AMLALE) +EXTERN_C int atexit(void (*f)()) { + NE_UNUSED(f); + return 0; +} + +/// @brief memset definition in C++. +/// @param dst destination pointer. +/// @param byte value to fill in. +/// @param len length of of src. +EXTERN_C VoidPtr memnset(void* dst, int byte, long long unsigned int len, + long long unsigned int dst_size) { + if (!dst || len > dst_size) { + // For now, we return nullptr or an error status. + return nullptr; + } + unsigned char* p = (unsigned char*) dst; + unsigned char val = (unsigned char) byte; + for (size_t i = 0UL; i < len; ++i) { + p[i] = val; + } + return dst; +} + +/// @brief memcpy definition in C++. +/// @param dst destination pointer. +/// @param src source pointer. +/// @param len length of of src. +EXTERN_C VoidPtr memncpy(void* dst, const void* src, long long unsigned int len, + long long unsigned int dst_size) { + if (!dst || !src || len > dst_size) { + // Similar to memset, this is a critical failure. + return nullptr; + } + unsigned char* d = (unsigned char*) dst; + const unsigned char* s = (const unsigned char*) src; + for (size_t i = 0UL; i < len; ++i) { + d[i] = s[i]; + } + return dst; +} + +/// @brief strlen definition in C++. +EXTERN_C size_t strnlen(const char* whatToCheck, size_t max_len) { + size_t len = 0; + while (len < max_len && whatToCheck[len] != '\0') { + ++len; + } + return len; +} + +/// @brief strcmp definition in C++. +EXTERN_C int strncmp(const char* whatToCheck, const char* whatToCheckRight, size_t max_len) { + size_t i = 0; + while (i < max_len && whatToCheck[i] == whatToCheckRight[i]) { + if (whatToCheck[i] == '\0') return 0; + ++i; + } + if (i == max_len) { + return 0; + } + return (unsigned char) whatToCheck[i] - (unsigned char) whatToCheckRight[i]; +} + +/// @brief something specific to the Microsoft's ABI, When the stack grows too big. +EXTERN_C void ___chkstk_ms(void) {} + +/// @note GCC expects them to be here. + +/// @brief memset definition in C++. +/// @param dst destination pointer. +/// @param byte value to fill in. +/// @param len length of of src. +EXTERN_C VoidPtr memset(void* dst, int byte, long long unsigned int len) { + for (size_t i = 0UL; i < len; ++i) { + ((int*) dst)[i] = byte; + } + + return dst; +} + +/// @brief memcpy definition in C++. +/// @param dst destination pointer. +/// @param src source pointer. +/// @param len length of of src. +EXTERN_C VoidPtr memcpy(void* dst, const void* src, long long unsigned int len) { + for (size_t i = 0UL; i < len; ++i) { + ((int*) dst)[i] = ((int*) src)[i]; + } + + return dst; +} + +/// @brief strlen definition in C++. +EXTERN_C size_t strlen(const char* whatToCheck) { + SizeT len = 0; + + while (whatToCheck[len] != 0) { + ++len; + } + + return len; +} + +/// @brief strcmp definition in C++. +EXTERN_C int strcmp(const char* whatToCheck, const char* whatToCheckRight) { + SizeT len = 0; + + while (whatToCheck[len] == whatToCheckRight[len]) { + if (whatToCheck[len] == 0) return 0; + + ++len; + } + + return len; +} + +#endif diff --git a/src/boot/src/BootTextWriter.cc b/src/boot/src/BootTextWriter.cc deleted file mode 100644 index e980b0ae..00000000 --- a/src/boot/src/BootTextWriter.cc +++ /dev/null @@ -1,148 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include -#include - -///////////////////////////////////////////////////////////////////////////////////////////////////////// -/// BUGS: 0 /// -///////////////////////////////////////////////////////////////////////////////////////////////////////// - -/** -@brief puts wrapper over EFI ConOut. -*/ -Boot::BootTextWriter& Boot::BootTextWriter::Write(const CharacterTypeUTF16* str) { -#ifdef __DEBUG__ - if (!str || *str == 0) return *this; - - CharacterTypeUTF16 strTmp[2]; - strTmp[1] = 0; - - for (size_t i = 0; str[i] != 0; i++) { - if (str[i] == '\r') { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - - strTmp[0] = '\n'; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } else { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - } -#else - NE_UNUSED(str); -#endif // ifdef __DEBUG__ - - return *this; -} - -/// @brief UTF-8 equivalent of Write (UTF-16). -/// @param str the input string. -Boot::BootTextWriter& Boot::BootTextWriter::Write(const Char* str) { -#ifdef __DEBUG__ - if (!str || *str == 0) return *this; - - CharacterTypeUTF16 strTmp[2]; - strTmp[1] = 0; - - for (size_t i = 0; str[i] != 0; i++) { - if (str[i] == '\r') { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - - strTmp[0] = '\n'; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } else { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - } -#else - NE_UNUSED(str); -#endif // ifdef __DEBUG__ - - return *this; -} - -Boot::BootTextWriter& Boot::BootTextWriter::Write(const UChar* str) { -#ifdef __DEBUG__ - if (!str || *str == 0) return *this; - - CharacterTypeUTF16 strTmp[2]; - strTmp[1] = 0; - - for (size_t i = 0; str[i] != 0; i++) { - if (str[i] == '\r') { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - - strTmp[0] = '\n'; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } else { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); - } - } -#else - NE_UNUSED(str); -#endif // ifdef __DEBUG__ - - return *this; -} - -/** -@brief putc wrapper over EFI ConOut. -*/ -Boot::BootTextWriter& Boot::BootTextWriter::WriteCharacter(CharacterTypeUTF16 c) { -#ifdef __DEBUG__ - EfiCharType str[2]; - - str[0] = c; - str[1] = 0; - ST->ConOut->OutputString(ST->ConOut, str); -#else - NE_UNUSED(c); -#endif // ifdef __DEBUG__ - - return *this; -} - -Boot::BootTextWriter& Boot::BootTextWriter::Write(const UInt64& x) { -#ifdef __DEBUG__ - this->Write("0x"); - this->_Write(x); -#else - NE_UNUSED(x); -#endif // ifdef __DEBUG__ - - return *this; -} - -Boot::BootTextWriter& Boot::BootTextWriter::_Write(const UInt64& x) { -#ifdef __DEBUG__ - UInt64 y = (x > 0 ? x : -x) / 16; - UInt64 h = (x > 0 ? x : -x) % 16; - - if (y) this->_Write(y); - - /// @note Let 'X' be an invalid number of hexadecimal base. - if (h > 16) { - this->WriteCharacter('X'); - return *this; - } - - if (y == ~0UL) y = -y; - - constexpr const char kNumberList[] = "0123456789ABCDEF"; - - this->WriteCharacter(kNumberList[h]); -#else - NE_UNUSED(x); -#endif // ifdef __DEBUG__ - - return *this; -} diff --git a/src/boot/src/BootTextWriter.cpp b/src/boot/src/BootTextWriter.cpp new file mode 100644 index 00000000..e980b0ae --- /dev/null +++ b/src/boot/src/BootTextWriter.cpp @@ -0,0 +1,148 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include +#include + +///////////////////////////////////////////////////////////////////////////////////////////////////////// +/// BUGS: 0 /// +///////////////////////////////////////////////////////////////////////////////////////////////////////// + +/** +@brief puts wrapper over EFI ConOut. +*/ +Boot::BootTextWriter& Boot::BootTextWriter::Write(const CharacterTypeUTF16* str) { +#ifdef __DEBUG__ + if (!str || *str == 0) return *this; + + CharacterTypeUTF16 strTmp[2]; + strTmp[1] = 0; + + for (size_t i = 0; str[i] != 0; i++) { + if (str[i] == '\r') { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + + strTmp[0] = '\n'; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } else { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } + } +#else + NE_UNUSED(str); +#endif // ifdef __DEBUG__ + + return *this; +} + +/// @brief UTF-8 equivalent of Write (UTF-16). +/// @param str the input string. +Boot::BootTextWriter& Boot::BootTextWriter::Write(const Char* str) { +#ifdef __DEBUG__ + if (!str || *str == 0) return *this; + + CharacterTypeUTF16 strTmp[2]; + strTmp[1] = 0; + + for (size_t i = 0; str[i] != 0; i++) { + if (str[i] == '\r') { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + + strTmp[0] = '\n'; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } else { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } + } +#else + NE_UNUSED(str); +#endif // ifdef __DEBUG__ + + return *this; +} + +Boot::BootTextWriter& Boot::BootTextWriter::Write(const UChar* str) { +#ifdef __DEBUG__ + if (!str || *str == 0) return *this; + + CharacterTypeUTF16 strTmp[2]; + strTmp[1] = 0; + + for (size_t i = 0; str[i] != 0; i++) { + if (str[i] == '\r') { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + + strTmp[0] = '\n'; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } else { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } + } +#else + NE_UNUSED(str); +#endif // ifdef __DEBUG__ + + return *this; +} + +/** +@brief putc wrapper over EFI ConOut. +*/ +Boot::BootTextWriter& Boot::BootTextWriter::WriteCharacter(CharacterTypeUTF16 c) { +#ifdef __DEBUG__ + EfiCharType str[2]; + + str[0] = c; + str[1] = 0; + ST->ConOut->OutputString(ST->ConOut, str); +#else + NE_UNUSED(c); +#endif // ifdef __DEBUG__ + + return *this; +} + +Boot::BootTextWriter& Boot::BootTextWriter::Write(const UInt64& x) { +#ifdef __DEBUG__ + this->Write("0x"); + this->_Write(x); +#else + NE_UNUSED(x); +#endif // ifdef __DEBUG__ + + return *this; +} + +Boot::BootTextWriter& Boot::BootTextWriter::_Write(const UInt64& x) { +#ifdef __DEBUG__ + UInt64 y = (x > 0 ? x : -x) / 16; + UInt64 h = (x > 0 ? x : -x) % 16; + + if (y) this->_Write(y); + + /// @note Let 'X' be an invalid number of hexadecimal base. + if (h > 16) { + this->WriteCharacter('X'); + return *this; + } + + if (y == ~0UL) y = -y; + + constexpr const char kNumberList[] = "0123456789ABCDEF"; + + this->WriteCharacter(kNumberList[h]); +#else + NE_UNUSED(x); +#endif // ifdef __DEBUG__ + + return *this; +} diff --git a/src/boot/src/BootThread.cc b/src/boot/src/BootThread.cc deleted file mode 100644 index c75ff41d..00000000 --- a/src/boot/src/BootThread.cc +++ /dev/null @@ -1,224 +0,0 @@ -// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include -#include - -#include -#include -#include -#include -#include - -// \brief This macro defines the maximum size of a image's stack. -#define kBootThreadSz mib_cast(16) - -/// @brief External boot services symbol. -EXTERN EfiBootServices* BS; - -/// @note BootThread doesn't parse the symbols so doesn't nullify them, .bss is though. - -namespace Boot { -EXTERN_C Int32 rt_jump_to_address(VoidPtr code, HEL::BootInfoHeader* handover, UInt8* stack); - -BootThread::BootThread(VoidPtr blob) : fStartAddress(nullptr), fBlob(blob) { - // detect the image format (PEF, PE32, etc.) - const Char* blob_bytes = static_cast(fBlob); - BootTextWriter writer; - - if (!blob_bytes) { - // failed to provide a valid pointer. - return; - } - - if (blob_bytes[0] == kMagMz0 && blob_bytes[1] == kMagMz1) { - LDR_EXEC_HEADER_PTR header_ptr = CF::ldr_find_exec_header(blob_bytes); - LDR_OPTIONAL_HEADER_PTR opt_header_ptr = CF::ldr_find_opt_exec_header(blob_bytes); - - if (!header_ptr || !opt_header_ptr) return; - -#ifdef __NE_AMD64__ - 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->Machine != kPeMachineARM64 || header_ptr->Signature != kPeSignature) { - writer.Write("BootZ: Not a PE32+ executable.\r"); - return; - } -#endif // __NE_AMD64__ || __NE_ARM64__ - -#if !defined(__nekernel_allow_non_nekernel_pe) - if (opt_header_ptr->Subsystem != kNeKernelPESubsystem) { - writer.Write("BootZ: Not a NeKernel PE32+ executable.\r"); - return; - } -#endif - - writer.Write("BootZ: PE32+ executable detected. (NeKernel Subsystem)\r"); - - auto numSecs = header_ptr->NumberOfSections; - - 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"); - - EfiPhysicalAddress loadStartAddress = opt_header_ptr->ImageBase; - - writer.Write("BootZ: Image-Base: ").Write(loadStartAddress).Write("\r"); - - fStack = new UInt8[kBootThreadSz]; - - if (!fStack) { - writer.Write("BootZ: Unable to allocate the stack for the thread.\r"); - return; - } - - LDR_SECTION_HEADER_PTR sectPtr = - (LDR_SECTION_HEADER_PTR) (((Char*) opt_header_ptr) + header_ptr->SizeOfOptionalHeader); - - constexpr auto sectionForCode = ".text"; - constexpr auto sectionForBootZ = ".ldr"; - constexpr auto sectionForBootZAlt = ".botz"; - - for (SizeT sectIndex = 0; sectIndex < numSecs; ++sectIndex) { - LDR_SECTION_HEADER_PTR sect = §Ptr[sectIndex]; - - auto sz = sect->VirtualSize; - - if (sect->SizeOfRawData > 0) sz = sect->SizeOfRawData; - - SetMem((VoidPtr) (loadStartAddress + sect->VirtualAddress), 0, sz); - - if (sect->SizeOfRawData > 0) { - CopyMem((VoidPtr) (loadStartAddress + sect->VirtualAddress), - (VoidPtr) ((UIntPtr) fBlob + sect->PointerToRawData), sect->SizeOfRawData); - } - - if (StrCmp(sectionForCode, sect->Name) == 0) { - fStartAddress = - (VoidPtr) ((UIntPtr) loadStartAddress + opt_header_ptr->AddressOfEntryPoint); - writer.Write("BootZ: Executable entry address: ") - .Write((UIntPtr) fStartAddress) - .Write("\r"); - - /// @note .text region shall be marked as executable on ARM. - -#ifdef __NE_ARM64__ - -#endif - } else if (StrCmp(sectionForBootZ, sect->Name) == 0 || - StrCmp(sectionForBootZAlt, sect->Name) == 0) { - struct HANDOVER_INFORMATION_STUB { - UInt64 HandoverMagic; - UInt32 HandoverType; - UInt32 HandoverPad; - UInt32 HandoverArch; - }* handover_struc = - (struct HANDOVER_INFORMATION_STUB*) ((UIntPtr) fBlob + sect->PointerToRawData); - - if (handover_struc->HandoverMagic != kHandoverMagic) { -#ifdef __NE_AMD64__ - if (handover_struc->HandoverArch != HEL::kArchAMD64) { - writer.Write("BootZ: Not an handover header, bad CPU...\r"); - } -#elif defined(__NE_ARM64__) - if (handover_struc->HandoverArch != HEL::kArchARM64) { - writer.Write("BootZ: Not an handover header, bad CPU...\r"); - } -#endif - - writer.Write("BootZ: Not an handover header...\r"); - ::Boot::Stop(); - } - } - - writer.Write("BootZ: Raw-Offset: ") - .Write(sect->PointerToRawData) - .Write(" of ") - .Write(sect->Name) - .Write("\r"); - - CopyMem((VoidPtr) (loadStartAddress + sect->VirtualAddress), - (VoidPtr) ((UIntPtr) fBlob + sect->PointerToRawData), sect->SizeOfRawData); - } - } else if (blob_bytes[0] == kPefMagic[0] && blob_bytes[1] == kPefMagic[1] && - blob_bytes[2] == kPefMagic[2] && blob_bytes[3] == kPefMagic[3]) { - // ========================================= // - // PEF executable has been detected. - // This is stricly firmware level, by convention we only accept PE32+ here. - // ========================================= // - - fStartAddress = nullptr; - - writer.Write("BootZ: PEF executable detected, BootZ won't load it.\r"); - writer.Write("BootZ: note: PEF executables aren't supported for now.\r"); - } else { - writer.Write("BootZ: Invalid Executable.\r"); - } -} - -/// @note handover header has to be valid! -Int32 BootThread::Start(HEL::BootInfoHeader* handover, Bool own_stack) { - if (!fStartAddress) { - return kEfiFail; - } - - if (!handover) { - return kEfiFail; - } - - fHandover = handover; - - BootTextWriter writer; - - writer.Write("BootZ: Starting: ").Write(fBlobName).Write("\r"); - writer.Write("BootZ: Handover address: ").Write((UIntPtr) fHandover).Write("\r"); - - if (own_stack) { - writer.Write("BootZ: Using it's own stack.\r"); - writer.Write("BootZ: Stack address: ").Write((UIntPtr) &fStack[kBootThreadSz - 1]).Write("\r"); - writer.Write("BootZ: Stack size: ").Write(kBootThreadSz).Write("\r"); - - fHandover->f_StackTop = &fStack[kBootThreadSz - 1]; - fHandover->f_StackSz = kBootThreadSz; - - auto ret = rt_jump_to_address(fStartAddress, fHandover, &fStack[kBootThreadSz - 1]); - - // we don't need the stack anymore. - - delete[] fStack; - fStack = nullptr; - - return ret; - } else { - writer.Write("BootZ: Using the bootloader's stack.\r"); - - return reinterpret_cast(fStartAddress)(fHandover); - } - - return kEfiFail; -} - -const Char* BootThread::GetName() { - return fBlobName; -} - -Void BootThread::SetName(const Char* name) { - CopyMem(fBlobName, name, StrLen(name)); -} - -bool BootThread::IsValid() { - return fStartAddress != nullptr; -} -} // namespace Boot diff --git a/src/boot/src/BootThread.cpp b/src/boot/src/BootThread.cpp new file mode 100644 index 00000000..c75ff41d --- /dev/null +++ b/src/boot/src/BootThread.cpp @@ -0,0 +1,224 @@ +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include +#include + +#include +#include +#include +#include +#include + +// \brief This macro defines the maximum size of a image's stack. +#define kBootThreadSz mib_cast(16) + +/// @brief External boot services symbol. +EXTERN EfiBootServices* BS; + +/// @note BootThread doesn't parse the symbols so doesn't nullify them, .bss is though. + +namespace Boot { +EXTERN_C Int32 rt_jump_to_address(VoidPtr code, HEL::BootInfoHeader* handover, UInt8* stack); + +BootThread::BootThread(VoidPtr blob) : fStartAddress(nullptr), fBlob(blob) { + // detect the image format (PEF, PE32, etc.) + const Char* blob_bytes = static_cast(fBlob); + BootTextWriter writer; + + if (!blob_bytes) { + // failed to provide a valid pointer. + return; + } + + if (blob_bytes[0] == kMagMz0 && blob_bytes[1] == kMagMz1) { + LDR_EXEC_HEADER_PTR header_ptr = CF::ldr_find_exec_header(blob_bytes); + LDR_OPTIONAL_HEADER_PTR opt_header_ptr = CF::ldr_find_opt_exec_header(blob_bytes); + + if (!header_ptr || !opt_header_ptr) return; + +#ifdef __NE_AMD64__ + 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->Machine != kPeMachineARM64 || header_ptr->Signature != kPeSignature) { + writer.Write("BootZ: Not a PE32+ executable.\r"); + return; + } +#endif // __NE_AMD64__ || __NE_ARM64__ + +#if !defined(__nekernel_allow_non_nekernel_pe) + if (opt_header_ptr->Subsystem != kNeKernelPESubsystem) { + writer.Write("BootZ: Not a NeKernel PE32+ executable.\r"); + return; + } +#endif + + writer.Write("BootZ: PE32+ executable detected. (NeKernel Subsystem)\r"); + + auto numSecs = header_ptr->NumberOfSections; + + 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"); + + EfiPhysicalAddress loadStartAddress = opt_header_ptr->ImageBase; + + writer.Write("BootZ: Image-Base: ").Write(loadStartAddress).Write("\r"); + + fStack = new UInt8[kBootThreadSz]; + + if (!fStack) { + writer.Write("BootZ: Unable to allocate the stack for the thread.\r"); + return; + } + + LDR_SECTION_HEADER_PTR sectPtr = + (LDR_SECTION_HEADER_PTR) (((Char*) opt_header_ptr) + header_ptr->SizeOfOptionalHeader); + + constexpr auto sectionForCode = ".text"; + constexpr auto sectionForBootZ = ".ldr"; + constexpr auto sectionForBootZAlt = ".botz"; + + for (SizeT sectIndex = 0; sectIndex < numSecs; ++sectIndex) { + LDR_SECTION_HEADER_PTR sect = §Ptr[sectIndex]; + + auto sz = sect->VirtualSize; + + if (sect->SizeOfRawData > 0) sz = sect->SizeOfRawData; + + SetMem((VoidPtr) (loadStartAddress + sect->VirtualAddress), 0, sz); + + if (sect->SizeOfRawData > 0) { + CopyMem((VoidPtr) (loadStartAddress + sect->VirtualAddress), + (VoidPtr) ((UIntPtr) fBlob + sect->PointerToRawData), sect->SizeOfRawData); + } + + if (StrCmp(sectionForCode, sect->Name) == 0) { + fStartAddress = + (VoidPtr) ((UIntPtr) loadStartAddress + opt_header_ptr->AddressOfEntryPoint); + writer.Write("BootZ: Executable entry address: ") + .Write((UIntPtr) fStartAddress) + .Write("\r"); + + /// @note .text region shall be marked as executable on ARM. + +#ifdef __NE_ARM64__ + +#endif + } else if (StrCmp(sectionForBootZ, sect->Name) == 0 || + StrCmp(sectionForBootZAlt, sect->Name) == 0) { + struct HANDOVER_INFORMATION_STUB { + UInt64 HandoverMagic; + UInt32 HandoverType; + UInt32 HandoverPad; + UInt32 HandoverArch; + }* handover_struc = + (struct HANDOVER_INFORMATION_STUB*) ((UIntPtr) fBlob + sect->PointerToRawData); + + if (handover_struc->HandoverMagic != kHandoverMagic) { +#ifdef __NE_AMD64__ + if (handover_struc->HandoverArch != HEL::kArchAMD64) { + writer.Write("BootZ: Not an handover header, bad CPU...\r"); + } +#elif defined(__NE_ARM64__) + if (handover_struc->HandoverArch != HEL::kArchARM64) { + writer.Write("BootZ: Not an handover header, bad CPU...\r"); + } +#endif + + writer.Write("BootZ: Not an handover header...\r"); + ::Boot::Stop(); + } + } + + writer.Write("BootZ: Raw-Offset: ") + .Write(sect->PointerToRawData) + .Write(" of ") + .Write(sect->Name) + .Write("\r"); + + CopyMem((VoidPtr) (loadStartAddress + sect->VirtualAddress), + (VoidPtr) ((UIntPtr) fBlob + sect->PointerToRawData), sect->SizeOfRawData); + } + } else if (blob_bytes[0] == kPefMagic[0] && blob_bytes[1] == kPefMagic[1] && + blob_bytes[2] == kPefMagic[2] && blob_bytes[3] == kPefMagic[3]) { + // ========================================= // + // PEF executable has been detected. + // This is stricly firmware level, by convention we only accept PE32+ here. + // ========================================= // + + fStartAddress = nullptr; + + writer.Write("BootZ: PEF executable detected, BootZ won't load it.\r"); + writer.Write("BootZ: note: PEF executables aren't supported for now.\r"); + } else { + writer.Write("BootZ: Invalid Executable.\r"); + } +} + +/// @note handover header has to be valid! +Int32 BootThread::Start(HEL::BootInfoHeader* handover, Bool own_stack) { + if (!fStartAddress) { + return kEfiFail; + } + + if (!handover) { + return kEfiFail; + } + + fHandover = handover; + + BootTextWriter writer; + + writer.Write("BootZ: Starting: ").Write(fBlobName).Write("\r"); + writer.Write("BootZ: Handover address: ").Write((UIntPtr) fHandover).Write("\r"); + + if (own_stack) { + writer.Write("BootZ: Using it's own stack.\r"); + writer.Write("BootZ: Stack address: ").Write((UIntPtr) &fStack[kBootThreadSz - 1]).Write("\r"); + writer.Write("BootZ: Stack size: ").Write(kBootThreadSz).Write("\r"); + + fHandover->f_StackTop = &fStack[kBootThreadSz - 1]; + fHandover->f_StackSz = kBootThreadSz; + + auto ret = rt_jump_to_address(fStartAddress, fHandover, &fStack[kBootThreadSz - 1]); + + // we don't need the stack anymore. + + delete[] fStack; + fStack = nullptr; + + return ret; + } else { + writer.Write("BootZ: Using the bootloader's stack.\r"); + + return reinterpret_cast(fStartAddress)(fHandover); + } + + return kEfiFail; +} + +const Char* BootThread::GetName() { + return fBlobName; +} + +Void BootThread::SetName(const Char* name) { + CopyMem(fBlobName, name, StrLen(name)); +} + +bool BootThread::IsValid() { + return fStartAddress != nullptr; +} +} // namespace Boot diff --git a/src/boot/src/HEL/AMD64/BootATA.cc b/src/boot/src/HEL/AMD64/BootATA.cc deleted file mode 100644 index 423f4437..00000000 --- a/src/boot/src/HEL/AMD64/BootATA.cc +++ /dev/null @@ -1,253 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include - -#define kATADataLen (256) - -/// bugs: 0 - -using namespace Boot; - -static Boolean kATADetected = false; -static UInt16 kATAData[kATADataLen] = {0}; - -Boolean boot_ata_detected(Void); - -STATIC Boolean boot_ata_wait_io(UInt16 IO) { - for (int i = 0; i < 400; i++) rt_in8(IO + ATA_REG_STATUS); - -ATAWaitForIO_Retry: - auto status_rdy = rt_in8(IO + ATA_REG_STATUS); - - if ((status_rdy & ATA_SR_BSY)) goto ATAWaitForIO_Retry; - -ATAWaitForIO_Retry2: - status_rdy = rt_in8(IO + ATA_REG_STATUS); - - if (status_rdy & ATA_SR_ERR) return false; - - if (!(status_rdy & ATA_SR_DRDY)) goto ATAWaitForIO_Retry2; - - return true; -} - -Void boot_ata_select(UInt16 Bus) { - if (Bus == ATA_PRIMARY_IO) - rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_PRIMARY_SEL); - else - rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_SECONDARY_SEL); -} - -Boolean boot_ata_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) { - NE_UNUSED(Drive); - - if (boot_ata_detected()) return true; - - BootTextWriter writer; - - UInt16 IO = Bus; - - boot_ata_select(IO); - - // Bus init, NEIN bit. - rt_out8(IO + ATA_REG_NEIN, 1); - - // identify until it's good. -ATAInit_Retry: - auto status_rdy = rt_in8(IO + ATA_REG_STATUS); - - if (status_rdy & ATA_SR_ERR) { - writer.Write(L"VMBoot: ATA: Not an IDE based drive.\r"); - - return false; - } - - if ((status_rdy & ATA_SR_BSY)) goto ATAInit_Retry; - - boot_ata_select(IO); - - rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); - - /// fetch serial info - /// model, speed, number of sectors... - - while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)); - - for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) { - kATAData[indexData] = rt_in16(IO + ATA_REG_DATA); - } - - OutBus = (Bus == ATA_PRIMARY_IO) ? BootDeviceATA::kPrimary : BootDeviceATA::kSecondary; - - OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE; - - return true; -} - -Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeASCII* Buf, SizeT SectorSz, - SizeT Size) { - Lba /= SectorSz; - - UInt8 Command = ((!Master) ? 0xE0 : 0xF0); - - boot_ata_wait_io(IO); - boot_ata_select(IO); - - rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); - - rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz) / SectorSz)); - - rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF); - rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); - rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); - rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); - - rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); - - while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)); - - for (SizeT IndexOff = 0; IndexOff < Size; IndexOff += 2) { - boot_ata_wait_io(IO); - - auto in = rt_in16(IO + ATA_REG_DATA); - - Buf[IndexOff] = in & 0xFF; - Buf[IndexOff + 1] = (in >> 8) & 0xFF; - boot_ata_wait_io(IO); - } -} - -Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeASCII* Buf, SizeT SectorSz, - SizeT Size) { - Lba /= SectorSz; - - UInt8 Command = ((!Master) ? 0xE0 : 0xF0); - - boot_ata_wait_io(IO); - boot_ata_select(IO); - - rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); - - rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + (SectorSz)) / SectorSz)); - - rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF); - rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); - rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); - rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); - - rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); - - while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)); - - for (SizeT IndexOff = 0; IndexOff < Size; IndexOff += 2) { - boot_ata_wait_io(IO); - - UInt8 low = (UInt8) Buf[IndexOff]; - UInt8 high = (IndexOff + 1 < Size) ? (UInt8) Buf[IndexOff + 1] : 0; - UInt16 packed = (high << 8) | low; - - rt_out16(IO + ATA_REG_DATA, packed); - - boot_ata_wait_io(IO); - } - - boot_ata_wait_io(IO); -} - -/// @check is ATA detected? -Boolean boot_ata_detected(Void) { - return kATADetected; -} - -/*** - * - * - * @brief ATA Device class. - * - * - */ - -/** - * @brief ATA Device constructor. - * @param void none. - */ -BootDeviceATA::BootDeviceATA() { - if (boot_ata_init(ATA_PRIMARY_IO, true, this->Leak().mBus, this->Leak().mMaster) || - boot_ata_init(ATA_SECONDARY_IO, true, this->Leak().mBus, this->Leak().mMaster)) { - kATADetected = true; - } -} -/** - * @brief Is ATA detected? - */ -BootDeviceATA::operator bool() { - return boot_ata_detected(); -} - -/** - @brief Read Buf from disk - @param Sz Sector size - @param Buf buffer -*/ -BootDeviceATA& BootDeviceATA::Read(CharacterTypeASCII* Buf, SizeT SectorSz) { - if (!boot_ata_detected()) { - Leak().mErr = true; - return *this; - } - - this->Leak().mErr = false; - - if (!Buf || SectorSz < 1) return *this; - - boot_ata_read(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, Buf, SectorSz, - this->Leak().mSize); - - return *this; -} - -/** - @brief Write Buf into disk - @param Sz Sector size - @param Buf buffer -*/ -BootDeviceATA& BootDeviceATA::Write(CharacterTypeASCII* Buf, SizeT SectorSz) { - if (!boot_ata_detected()) { - Leak().mErr = true; - return *this; - } - - Leak().mErr = false; - - if (!Buf || SectorSz < 1 || this->Leak().mSize < 1) { - Leak().mErr = true; - return *this; - } - - boot_ata_write(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, Buf, SectorSz, - this->Leak().mSize); - - return *this; -} - -/** - * @brief ATA trait getter. - * @return BootDeviceATA::ATATrait& the drive config. - */ -BootDeviceATA::ATATrait& BootDeviceATA::Leak() { - return mTrait; -} - -/*** - @brief Getter, gets the number of sectors inside the drive. -*/ -SizeT BootDeviceATA::GetSectorsCount() { - return (kATAData[61] << 16) | kATAData[60]; -} - -SizeT BootDeviceATA::GetDiskSize() { - return this->GetSectorsCount() * BootDeviceATA::kSectorSize; -} diff --git a/src/boot/src/HEL/AMD64/BootATA.cpp b/src/boot/src/HEL/AMD64/BootATA.cpp new file mode 100644 index 00000000..423f4437 --- /dev/null +++ b/src/boot/src/HEL/AMD64/BootATA.cpp @@ -0,0 +1,253 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include + +#define kATADataLen (256) + +/// bugs: 0 + +using namespace Boot; + +static Boolean kATADetected = false; +static UInt16 kATAData[kATADataLen] = {0}; + +Boolean boot_ata_detected(Void); + +STATIC Boolean boot_ata_wait_io(UInt16 IO) { + for (int i = 0; i < 400; i++) rt_in8(IO + ATA_REG_STATUS); + +ATAWaitForIO_Retry: + auto status_rdy = rt_in8(IO + ATA_REG_STATUS); + + if ((status_rdy & ATA_SR_BSY)) goto ATAWaitForIO_Retry; + +ATAWaitForIO_Retry2: + status_rdy = rt_in8(IO + ATA_REG_STATUS); + + if (status_rdy & ATA_SR_ERR) return false; + + if (!(status_rdy & ATA_SR_DRDY)) goto ATAWaitForIO_Retry2; + + return true; +} + +Void boot_ata_select(UInt16 Bus) { + if (Bus == ATA_PRIMARY_IO) + rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_PRIMARY_SEL); + else + rt_out8(Bus + ATA_REG_HDDEVSEL, ATA_SECONDARY_SEL); +} + +Boolean boot_ata_init(UInt16 Bus, UInt8 Drive, UInt16& OutBus, UInt8& OutMaster) { + NE_UNUSED(Drive); + + if (boot_ata_detected()) return true; + + BootTextWriter writer; + + UInt16 IO = Bus; + + boot_ata_select(IO); + + // Bus init, NEIN bit. + rt_out8(IO + ATA_REG_NEIN, 1); + + // identify until it's good. +ATAInit_Retry: + auto status_rdy = rt_in8(IO + ATA_REG_STATUS); + + if (status_rdy & ATA_SR_ERR) { + writer.Write(L"VMBoot: ATA: Not an IDE based drive.\r"); + + return false; + } + + if ((status_rdy & ATA_SR_BSY)) goto ATAInit_Retry; + + boot_ata_select(IO); + + rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); + + /// fetch serial info + /// model, speed, number of sectors... + + while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)); + + for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) { + kATAData[indexData] = rt_in16(IO + ATA_REG_DATA); + } + + OutBus = (Bus == ATA_PRIMARY_IO) ? BootDeviceATA::kPrimary : BootDeviceATA::kSecondary; + + OutMaster = (Bus == ATA_PRIMARY_IO) ? ATA_MASTER : ATA_SLAVE; + + return true; +} + +Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeASCII* Buf, SizeT SectorSz, + SizeT Size) { + Lba /= SectorSz; + + UInt8 Command = ((!Master) ? 0xE0 : 0xF0); + + boot_ata_wait_io(IO); + boot_ata_select(IO); + + rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); + + rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz) / SectorSz)); + + rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF); + rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); + rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); + rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); + + rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); + + while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)); + + for (SizeT IndexOff = 0; IndexOff < Size; IndexOff += 2) { + boot_ata_wait_io(IO); + + auto in = rt_in16(IO + ATA_REG_DATA); + + Buf[IndexOff] = in & 0xFF; + Buf[IndexOff + 1] = (in >> 8) & 0xFF; + boot_ata_wait_io(IO); + } +} + +Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeASCII* Buf, SizeT SectorSz, + SizeT Size) { + Lba /= SectorSz; + + UInt8 Command = ((!Master) ? 0xE0 : 0xF0); + + boot_ata_wait_io(IO); + boot_ata_select(IO); + + rt_out8(IO + ATA_REG_HDDEVSEL, (Command) | (((Lba) >> 24) & 0x0F)); + + rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + (SectorSz)) / SectorSz)); + + rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF); + rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); + rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); + rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); + + rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); + + while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)); + + for (SizeT IndexOff = 0; IndexOff < Size; IndexOff += 2) { + boot_ata_wait_io(IO); + + UInt8 low = (UInt8) Buf[IndexOff]; + UInt8 high = (IndexOff + 1 < Size) ? (UInt8) Buf[IndexOff + 1] : 0; + UInt16 packed = (high << 8) | low; + + rt_out16(IO + ATA_REG_DATA, packed); + + boot_ata_wait_io(IO); + } + + boot_ata_wait_io(IO); +} + +/// @check is ATA detected? +Boolean boot_ata_detected(Void) { + return kATADetected; +} + +/*** + * + * + * @brief ATA Device class. + * + * + */ + +/** + * @brief ATA Device constructor. + * @param void none. + */ +BootDeviceATA::BootDeviceATA() { + if (boot_ata_init(ATA_PRIMARY_IO, true, this->Leak().mBus, this->Leak().mMaster) || + boot_ata_init(ATA_SECONDARY_IO, true, this->Leak().mBus, this->Leak().mMaster)) { + kATADetected = true; + } +} +/** + * @brief Is ATA detected? + */ +BootDeviceATA::operator bool() { + return boot_ata_detected(); +} + +/** + @brief Read Buf from disk + @param Sz Sector size + @param Buf buffer +*/ +BootDeviceATA& BootDeviceATA::Read(CharacterTypeASCII* Buf, SizeT SectorSz) { + if (!boot_ata_detected()) { + Leak().mErr = true; + return *this; + } + + this->Leak().mErr = false; + + if (!Buf || SectorSz < 1) return *this; + + boot_ata_read(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, Buf, SectorSz, + this->Leak().mSize); + + return *this; +} + +/** + @brief Write Buf into disk + @param Sz Sector size + @param Buf buffer +*/ +BootDeviceATA& BootDeviceATA::Write(CharacterTypeASCII* Buf, SizeT SectorSz) { + if (!boot_ata_detected()) { + Leak().mErr = true; + return *this; + } + + Leak().mErr = false; + + if (!Buf || SectorSz < 1 || this->Leak().mSize < 1) { + Leak().mErr = true; + return *this; + } + + boot_ata_write(this->Leak().mBase, this->Leak().mBus, this->Leak().mMaster, Buf, SectorSz, + this->Leak().mSize); + + return *this; +} + +/** + * @brief ATA trait getter. + * @return BootDeviceATA::ATATrait& the drive config. + */ +BootDeviceATA::ATATrait& BootDeviceATA::Leak() { + return mTrait; +} + +/*** + @brief Getter, gets the number of sectors inside the drive. +*/ +SizeT BootDeviceATA::GetSectorsCount() { + return (kATAData[61] << 16) | kATAData[60]; +} + +SizeT BootDeviceATA::GetDiskSize() { + return this->GetSectorsCount() * BootDeviceATA::kSectorSize; +} diff --git a/src/boot/src/HEL/AMD64/BootEFI.cc b/src/boot/src/HEL/AMD64/BootEFI.cc deleted file mode 100644 index 9ef6d1ec..00000000 --- a/src/boot/src/HEL/AMD64/BootEFI.cc +++ /dev/null @@ -1,257 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -/** Graphics related. */ - -STATIC EfiGraphicsOutputProtocol* kGop = nullptr; -STATIC UInt16 kGopStride = 0U; -STATIC EFI_GUID kGopGuid; - -/** Related to jumping to the reset vector. */ - -EXTERN_C Void rt_reset_hardware(); - -EXTERN_C Kernel::VoidPtr boot_read_cr3(); // @brief Page directory inside cr3 register. - -/** - @brief Finds and stores the GOP object. -*/ -STATIC Bool boot_init_fb() { - kGopGuid = EFI_GUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); - kGop = nullptr; - - if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*) &kGop) != kEfiOk) return No; - - // TODO: Ok that ain't great, open to fixes. - kGopStride = 4; - - return Yes; -} - -EFI_GUID kEfiGlobalNamespaceVarGUID = { - 0x8BE4DF61, 0x93CA, 0x11D2, {0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C}}; - -/// @brief BootloaderMain EFI entrypoint. -/// @param image_handle Handle of this image. -/// @param sys_table The system table of it. -/// @return nothing, never returns. -EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTable* sys_table) { - fw_init_efi(sys_table); ///! Init the EFI library. - - ST->ConOut->ClearScreen(sys_table->ConOut); - ST->ConOut->SetAttribute(sys_table->ConOut, kEFIYellow); - - ST->BootServices->SetWatchdogTimer(0, 0, 0, nullptr); - ST->ConOut->EnableCursor(ST->ConOut, false); - - HEL::BootInfoHeader* handover_hdr = new HEL::BootInfoHeader(); - - UInt32 map_key = 0; - UInt32 size_struct_ptr = sizeof(EfiMemoryDescriptor); - EfiMemoryDescriptor* struct_ptr = nullptr; - UInt32 sz_desc = sizeof(EfiMemoryDescriptor); - UInt32 rev_desc = 0; - - Boot::BootTextWriter writer; - - if (!boot_init_fb()) { - writer.Write("BootZ: Invalid Framebuffer, can't boot to NeKernel.\r"); - Boot::Stop(); - } - - writer.Write("BootZ: The NeKernel Bootloader. Copyright 2024-2026, Amlal El Mahrouss and al.\r"); - - for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; ++index_vt) { - Char* vendor_table = - reinterpret_cast(sys_table->ConfigurationTable[index_vt].VendorTable); - - // ACPI's 'RSD PTR', which contains the ACPI SDT (MADT, FACP...) - if (vendor_table[0] == 'R' && vendor_table[1] == 'S' && vendor_table[2] == 'D' && - vendor_table[3] == ' ' && vendor_table[4] == 'P' && vendor_table[5] == 'T' && - vendor_table[6] == 'R' && vendor_table[7] == ' ') { - handover_hdr->f_HardwareTables.f_VendorPtr = (VoidPtr) vendor_table; - break; - } - } - - // ------------------------------------------ // - // draw background color. - // ------------------------------------------ // - - handover_hdr->f_GOP.f_The = kGop->Mode->FrameBufferBase; - handover_hdr->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution; - handover_hdr->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution; - handover_hdr->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; - handover_hdr->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; - handover_hdr->f_GOP.f_Size = kGop->Mode->FrameBufferSize; - - // ------------------------------------------- // - // Grab MP services, extended to runtime. // - // ------------------------------------------- // - - EFI_GUID guid_mp = EFI_GUID(EFI_MP_SERVICES_PROTOCOL_GUID); - EfiMpServicesProtocol* mp = nullptr; - - BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast(&mp)); - - handover_hdr->f_HardwareTables.f_MpPtr = reinterpret_cast(mp); - - kHandoverHeader = handover_hdr; - - FB::cg_clear_video(); - - UInt32 cnt_enabled = 0; - UInt32 cnt_disabled = 0; - - if (mp) { - mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); - handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; - } else { - handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = NO; - } - - // Fill handover header now. - - handover_hdr->f_BitMapStart = nullptr; /* Start of bitmap. */ - handover_hdr->f_BitMapSize = 0UL; /* Size of bitmap in bytes. */ - - // Get memory map to determine available memory for bitmap allocation. - BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); - - // Allocate space for the memory descriptors. - BS->AllocatePool(EfiLoaderData, size_struct_ptr, (VoidPtr*) &struct_ptr); - BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); - - // Calculate initial bitmap size by summing all free memory pages. - UInt64 free_pages = 0; - VoidPtr first_free_page = (VoidPtr) 1024; - - for (UInt32 i = 0; i < size_struct_ptr / sz_desc; ++i) { - EfiMemoryDescriptor* desc = (EfiMemoryDescriptor*) ((UInt8*) struct_ptr + (i * sz_desc)); - if (desc->Kind == EfiConventionalMemory) { - if (first_free_page == nullptr) { - first_free_page = (VoidPtr) desc->PhysicalStart; - } - free_pages += desc->NumberOfPages; - } - } - - free_pages -= 1024; - - // Set bitmap to use the first free page region found. - handover_hdr->f_BitMapStart = first_free_page; - - // Convert pages to bytes (assuming 4K pages) for bitmap size. - handover_hdr->f_BitMapSize = free_pages * 4096; - - handover_hdr->f_FirmwareCustomTables[Kernel::HEL::kHandoverTableBS] = (VoidPtr) BS; - handover_hdr->f_FirmwareCustomTables[Kernel::HEL::kHandoverTableST] = (VoidPtr) ST; - - // ------------------------------------------ // - // If we succeed in reading the blob, then execute it. - // ------------------------------------------ // - - Boot::BootFileReader reader_syschk(L"chk.efi", image_handle); - reader_syschk.ReadAll(0); - - Boot::BootThread* syschk_thread = nullptr; - - if (reader_syschk.Blob()) { - syschk_thread = new Boot::BootThread(reader_syschk.Blob()); - syschk_thread->SetName("SysChk"); - - syschk_thread->Start(handover_hdr, NO); - } - - handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - - handover_hdr->f_Magic = kHandoverMagic; - handover_hdr->f_Version = kHandoverVersion; - - handover_hdr->f_HardwareTables.f_ImageKey = map_key; - handover_hdr->f_HardwareTables.f_ImageHandle = image_handle; - - // Provide fimware vendor name. - - Boot::BCopyMem(handover_hdr->f_FirmwareVendorName, sys_table->FirmwareVendor, - handover_hdr->f_FirmwareVendorLen); - - handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - // Assign to global 'kHandoverHeader'. - - WideChar kernel_path[256U] = L"ne_kernel"; - UInt32 kernel_path_sz = StrLen("ne_kernel"); - - UInt32 sz_ver = sizeof(UInt64); - UInt64 ver = KERNEL_VERSION_BCD; - - ST->RuntimeServices->GetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr, &sz_ver, - &ver); - - if (ver < KERNEL_VERSION_BCD) { - ver = KERNEL_VERSION_BCD; - - ST->RuntimeServices->SetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr, - &sz_ver, &ver); - - writer.Write("BootZ: Version has been updated: ").Write(ver).Write("\r"); - - if (ST->RuntimeServices->GetVariable(L"/props/kernel_path", kEfiGlobalNamespaceVarGUID, nullptr, - &kernel_path_sz, kernel_path) != kEfiOk) { - /// access attributes (in order) - /// EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS - UInt32 attr = 0x00000001 | 0x00000002 | 0x00000004; - - ST->RuntimeServices->SetVariable(L"/props/kernel_path", kEfiGlobalNamespaceVarGUID, &attr, - &kernel_path_sz, kernel_path); - } - } else { - writer.Write("BootZ: Version: ").Write(ver).Write("\r"); - } - - // boot to kernel, if not bootnet this. - - Boot::BootFileReader reader_kernel(kernel_path, image_handle); - reader_kernel.ReadAll(0); - - // ------------------------------------------ // - // If we succeed in reading the blob, then execute it. - // ------------------------------------------ // - - if (reader_kernel.Blob()) { - handover_hdr->f_PageStart = boot_read_cr3(); - - auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); - - kernel_thread.SetName("NeKernel"); - - handover_hdr->f_KernelImage = reader_kernel.Blob(); - handover_hdr->f_KernelSz = reader_kernel.Size(); - - kernel_thread.Start(handover_hdr, YES); - } - - Boot::BootFileReader reader_netboot(L"net.efi", image_handle); - reader_netboot.ReadAll(0); - - if (!reader_netboot.Blob()) return kEfiFail; - - auto netboot_thread = Boot::BootThread(reader_netboot.Blob()); - netboot_thread.SetName("BootNet"); - - return netboot_thread.Start(handover_hdr, NO); -} diff --git a/src/boot/src/HEL/AMD64/BootEFI.cpp b/src/boot/src/HEL/AMD64/BootEFI.cpp new file mode 100644 index 00000000..9ef6d1ec --- /dev/null +++ b/src/boot/src/HEL/AMD64/BootEFI.cpp @@ -0,0 +1,257 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +/** Graphics related. */ + +STATIC EfiGraphicsOutputProtocol* kGop = nullptr; +STATIC UInt16 kGopStride = 0U; +STATIC EFI_GUID kGopGuid; + +/** Related to jumping to the reset vector. */ + +EXTERN_C Void rt_reset_hardware(); + +EXTERN_C Kernel::VoidPtr boot_read_cr3(); // @brief Page directory inside cr3 register. + +/** + @brief Finds and stores the GOP object. +*/ +STATIC Bool boot_init_fb() { + kGopGuid = EFI_GUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); + kGop = nullptr; + + if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*) &kGop) != kEfiOk) return No; + + // TODO: Ok that ain't great, open to fixes. + kGopStride = 4; + + return Yes; +} + +EFI_GUID kEfiGlobalNamespaceVarGUID = { + 0x8BE4DF61, 0x93CA, 0x11D2, {0xAA, 0x0D, 0x00, 0xE0, 0x98, 0x03, 0x2B, 0x8C}}; + +/// @brief BootloaderMain EFI entrypoint. +/// @param image_handle Handle of this image. +/// @param sys_table The system table of it. +/// @return nothing, never returns. +EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTable* sys_table) { + fw_init_efi(sys_table); ///! Init the EFI library. + + ST->ConOut->ClearScreen(sys_table->ConOut); + ST->ConOut->SetAttribute(sys_table->ConOut, kEFIYellow); + + ST->BootServices->SetWatchdogTimer(0, 0, 0, nullptr); + ST->ConOut->EnableCursor(ST->ConOut, false); + + HEL::BootInfoHeader* handover_hdr = new HEL::BootInfoHeader(); + + UInt32 map_key = 0; + UInt32 size_struct_ptr = sizeof(EfiMemoryDescriptor); + EfiMemoryDescriptor* struct_ptr = nullptr; + UInt32 sz_desc = sizeof(EfiMemoryDescriptor); + UInt32 rev_desc = 0; + + Boot::BootTextWriter writer; + + if (!boot_init_fb()) { + writer.Write("BootZ: Invalid Framebuffer, can't boot to NeKernel.\r"); + Boot::Stop(); + } + + writer.Write("BootZ: The NeKernel Bootloader. Copyright 2024-2026, Amlal El Mahrouss and al.\r"); + + for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; ++index_vt) { + Char* vendor_table = + reinterpret_cast(sys_table->ConfigurationTable[index_vt].VendorTable); + + // ACPI's 'RSD PTR', which contains the ACPI SDT (MADT, FACP...) + if (vendor_table[0] == 'R' && vendor_table[1] == 'S' && vendor_table[2] == 'D' && + vendor_table[3] == ' ' && vendor_table[4] == 'P' && vendor_table[5] == 'T' && + vendor_table[6] == 'R' && vendor_table[7] == ' ') { + handover_hdr->f_HardwareTables.f_VendorPtr = (VoidPtr) vendor_table; + break; + } + } + + // ------------------------------------------ // + // draw background color. + // ------------------------------------------ // + + handover_hdr->f_GOP.f_The = kGop->Mode->FrameBufferBase; + handover_hdr->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution; + handover_hdr->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution; + handover_hdr->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; + handover_hdr->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; + handover_hdr->f_GOP.f_Size = kGop->Mode->FrameBufferSize; + + // ------------------------------------------- // + // Grab MP services, extended to runtime. // + // ------------------------------------------- // + + EFI_GUID guid_mp = EFI_GUID(EFI_MP_SERVICES_PROTOCOL_GUID); + EfiMpServicesProtocol* mp = nullptr; + + BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast(&mp)); + + handover_hdr->f_HardwareTables.f_MpPtr = reinterpret_cast(mp); + + kHandoverHeader = handover_hdr; + + FB::cg_clear_video(); + + UInt32 cnt_enabled = 0; + UInt32 cnt_disabled = 0; + + if (mp) { + mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); + handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; + } else { + handover_hdr->f_HardwareTables.f_MultiProcessingEnabled = NO; + } + + // Fill handover header now. + + handover_hdr->f_BitMapStart = nullptr; /* Start of bitmap. */ + handover_hdr->f_BitMapSize = 0UL; /* Size of bitmap in bytes. */ + + // Get memory map to determine available memory for bitmap allocation. + BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); + + // Allocate space for the memory descriptors. + BS->AllocatePool(EfiLoaderData, size_struct_ptr, (VoidPtr*) &struct_ptr); + BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); + + // Calculate initial bitmap size by summing all free memory pages. + UInt64 free_pages = 0; + VoidPtr first_free_page = (VoidPtr) 1024; + + for (UInt32 i = 0; i < size_struct_ptr / sz_desc; ++i) { + EfiMemoryDescriptor* desc = (EfiMemoryDescriptor*) ((UInt8*) struct_ptr + (i * sz_desc)); + if (desc->Kind == EfiConventionalMemory) { + if (first_free_page == nullptr) { + first_free_page = (VoidPtr) desc->PhysicalStart; + } + free_pages += desc->NumberOfPages; + } + } + + free_pages -= 1024; + + // Set bitmap to use the first free page region found. + handover_hdr->f_BitMapStart = first_free_page; + + // Convert pages to bytes (assuming 4K pages) for bitmap size. + handover_hdr->f_BitMapSize = free_pages * 4096; + + handover_hdr->f_FirmwareCustomTables[Kernel::HEL::kHandoverTableBS] = (VoidPtr) BS; + handover_hdr->f_FirmwareCustomTables[Kernel::HEL::kHandoverTableST] = (VoidPtr) ST; + + // ------------------------------------------ // + // If we succeed in reading the blob, then execute it. + // ------------------------------------------ // + + Boot::BootFileReader reader_syschk(L"chk.efi", image_handle); + reader_syschk.ReadAll(0); + + Boot::BootThread* syschk_thread = nullptr; + + if (reader_syschk.Blob()) { + syschk_thread = new Boot::BootThread(reader_syschk.Blob()); + syschk_thread->SetName("SysChk"); + + syschk_thread->Start(handover_hdr, NO); + } + + handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); + + handover_hdr->f_Magic = kHandoverMagic; + handover_hdr->f_Version = kHandoverVersion; + + handover_hdr->f_HardwareTables.f_ImageKey = map_key; + handover_hdr->f_HardwareTables.f_ImageHandle = image_handle; + + // Provide fimware vendor name. + + Boot::BCopyMem(handover_hdr->f_FirmwareVendorName, sys_table->FirmwareVendor, + handover_hdr->f_FirmwareVendorLen); + + handover_hdr->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); + // Assign to global 'kHandoverHeader'. + + WideChar kernel_path[256U] = L"ne_kernel"; + UInt32 kernel_path_sz = StrLen("ne_kernel"); + + UInt32 sz_ver = sizeof(UInt64); + UInt64 ver = KERNEL_VERSION_BCD; + + ST->RuntimeServices->GetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr, &sz_ver, + &ver); + + if (ver < KERNEL_VERSION_BCD) { + ver = KERNEL_VERSION_BCD; + + ST->RuntimeServices->SetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr, + &sz_ver, &ver); + + writer.Write("BootZ: Version has been updated: ").Write(ver).Write("\r"); + + if (ST->RuntimeServices->GetVariable(L"/props/kernel_path", kEfiGlobalNamespaceVarGUID, nullptr, + &kernel_path_sz, kernel_path) != kEfiOk) { + /// access attributes (in order) + /// EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS + UInt32 attr = 0x00000001 | 0x00000002 | 0x00000004; + + ST->RuntimeServices->SetVariable(L"/props/kernel_path", kEfiGlobalNamespaceVarGUID, &attr, + &kernel_path_sz, kernel_path); + } + } else { + writer.Write("BootZ: Version: ").Write(ver).Write("\r"); + } + + // boot to kernel, if not bootnet this. + + Boot::BootFileReader reader_kernel(kernel_path, image_handle); + reader_kernel.ReadAll(0); + + // ------------------------------------------ // + // If we succeed in reading the blob, then execute it. + // ------------------------------------------ // + + if (reader_kernel.Blob()) { + handover_hdr->f_PageStart = boot_read_cr3(); + + auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); + + kernel_thread.SetName("NeKernel"); + + handover_hdr->f_KernelImage = reader_kernel.Blob(); + handover_hdr->f_KernelSz = reader_kernel.Size(); + + kernel_thread.Start(handover_hdr, YES); + } + + Boot::BootFileReader reader_netboot(L"net.efi", image_handle); + reader_netboot.ReadAll(0); + + if (!reader_netboot.Blob()) return kEfiFail; + + auto netboot_thread = Boot::BootThread(reader_netboot.Blob()); + netboot_thread.SetName("BootNet"); + + return netboot_thread.Start(handover_hdr, NO); +} diff --git a/src/boot/src/HEL/AMD64/BootPlatform.cc b/src/boot/src/HEL/AMD64/BootPlatform.cc deleted file mode 100644 index acdf8258..00000000 --- a/src/boot/src/HEL/AMD64/BootPlatform.cc +++ /dev/null @@ -1,33 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include - -#ifdef __BOOTZ_STANDALONE__ - -using namespace Boot; - -EXTERN_C void rt_halt() { - asm volatile("hlt"); -} - -EXTERN_C void rt_cli() { - asm volatile("cli"); -} - -EXTERN_C void rt_sti() { - asm volatile("sti"); -} - -EXTERN_C void rt_cld() { - asm volatile("cld"); -} - -EXTERN_C void rt_std() { - asm volatile("std"); -} - -#endif // __BOOTZ_STANDALONE__ diff --git a/src/boot/src/HEL/AMD64/BootPlatform.cpp b/src/boot/src/HEL/AMD64/BootPlatform.cpp new file mode 100644 index 00000000..acdf8258 --- /dev/null +++ b/src/boot/src/HEL/AMD64/BootPlatform.cpp @@ -0,0 +1,33 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include + +#ifdef __BOOTZ_STANDALONE__ + +using namespace Boot; + +EXTERN_C void rt_halt() { + asm volatile("hlt"); +} + +EXTERN_C void rt_cli() { + asm volatile("cli"); +} + +EXTERN_C void rt_sti() { + asm volatile("sti"); +} + +EXTERN_C void rt_cld() { + asm volatile("cld"); +} + +EXTERN_C void rt_std() { + asm volatile("std"); +} + +#endif // __BOOTZ_STANDALONE__ diff --git a/src/boot/src/HEL/AMD64/BootSATA.cc b/src/boot/src/HEL/AMD64/BootSATA.cc deleted file mode 100644 index c093179b..00000000 --- a/src/boot/src/HEL/AMD64/BootSATA.cc +++ /dev/null @@ -1,63 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include - -#include -#include - -#if defined(__AHCI__) && defined(__SYSCHK__) - -using namespace Boot; - -/*** - * - * - * @brief SATA Device class. - * - * - */ - -/** - * @brief ATA Device constructor. - * @param void none. - */ -BootDeviceSATA::BootDeviceSATA() { - UInt16 pi = 0u; - drv_std_init(pi); -} - -/** - @brief Read Buf from disk - @param Sz Sector size - @param Buf buffer -*/ -BootDeviceSATA& BootDeviceSATA::Read(CharacterTypeASCII* Buf, SizeT SectorSz) { - drv_std_read(mTrait.mBase / SectorSz, Buf, SectorSz, mTrait.mSize); - - return *this; -} - -/** - @brief Write Buf into disk - @param Sz Sector size - @param Buf buffer -*/ -BootDeviceSATA& BootDeviceSATA::Write(CharacterTypeASCII* Buf, SizeT SectorSz) { - drv_std_write(mTrait.mBase / SectorSz, Buf, SectorSz, mTrait.mSize); - - return *this; -} - -/** - * @brief ATA trait getter. - * @return BootDeviceSATA::ATATrait& the drive config. - */ -BootDeviceSATA::SATATrait& BootDeviceSATA::Leak() { - return mTrait; -} - -#endif \ No newline at end of file diff --git a/src/boot/src/HEL/AMD64/BootSATA.cpp b/src/boot/src/HEL/AMD64/BootSATA.cpp new file mode 100644 index 00000000..c093179b --- /dev/null +++ b/src/boot/src/HEL/AMD64/BootSATA.cpp @@ -0,0 +1,63 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include + +#include +#include + +#if defined(__AHCI__) && defined(__SYSCHK__) + +using namespace Boot; + +/*** + * + * + * @brief SATA Device class. + * + * + */ + +/** + * @brief ATA Device constructor. + * @param void none. + */ +BootDeviceSATA::BootDeviceSATA() { + UInt16 pi = 0u; + drv_std_init(pi); +} + +/** + @brief Read Buf from disk + @param Sz Sector size + @param Buf buffer +*/ +BootDeviceSATA& BootDeviceSATA::Read(CharacterTypeASCII* Buf, SizeT SectorSz) { + drv_std_read(mTrait.mBase / SectorSz, Buf, SectorSz, mTrait.mSize); + + return *this; +} + +/** + @brief Write Buf into disk + @param Sz Sector size + @param Buf buffer +*/ +BootDeviceSATA& BootDeviceSATA::Write(CharacterTypeASCII* Buf, SizeT SectorSz) { + drv_std_write(mTrait.mBase / SectorSz, Buf, SectorSz, mTrait.mSize); + + return *this; +} + +/** + * @brief ATA trait getter. + * @return BootDeviceSATA::ATATrait& the drive config. + */ +BootDeviceSATA::SATATrait& BootDeviceSATA::Leak() { + return mTrait; +} + +#endif \ No newline at end of file diff --git a/src/boot/src/HEL/ARM64/BootEFI.cc b/src/boot/src/HEL/ARM64/BootEFI.cc deleted file mode 100644 index 6322d311..00000000 --- a/src/boot/src/HEL/ARM64/BootEFI.cc +++ /dev/null @@ -1,164 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#ifndef kExpectedWidth -#define kExpectedWidth (800) -#endif - -#ifndef kExpectedHeight -#define kExpectedHeight (600) -#endif - -/** Graphics related. */ - -STATIC EfiGraphicsOutputProtocol* kGop = nullptr; -STATIC UInt16 kGopStride = 0U; -STATIC EFI_GUID kGopGuid; - -EXTERN_C Void rt_reset_hardware(); - -EXTERN EfiBootServices* BS; - -/** - @brief Finds and stores the GOP object. -*/ -STATIC Bool boot_init_fb() { - kGopGuid = EFI_GUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); - kGop = nullptr; - - if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*) &kGop) != kEfiOk) return No; - - kGopStride = 4; - - for (SizeT i = 0; i < kGop->Mode->MaxMode; ++i) { - EfiGraphicsOutputProtocolModeInformation* infoPtr = nullptr; - UInt32 sz = 0U; - - kGop->QueryMode(kGop, i, &sz, &infoPtr); - - if (infoPtr->HorizontalResolution == kExpectedWidth && - infoPtr->VerticalResolution == kExpectedHeight) { - kGop->SetMode(kGop, i); - return Yes; - } - } - - return No; -} - -EXTERN EfiBootServices* BS; - -/// @brief BootloaderMain EFI entrypoint. -/// @param image_handle Handle of this image. -/// @param sys_table The system table of it. -/// @return nothing, never returns. -EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTable* sys_table) { - fw_init_efi(sys_table); ///! Init the EFI library. - - kHandoverHeader = new HEL::BootInfoHeader(); - -#ifdef ZBA_USE_FB - if (!boot_init_fb()) return kEfiFail; ///! Init the GOP. - - for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; ++index_vt) { - Char* vendor_table = - reinterpret_cast(sys_table->ConfigurationTable[index_vt].VendorTable); - - // ACPI's 'RSD PTR', which contains the ACPI SDT (MADT, FACP...) - if (vendor_table[0] == 'R' && vendor_table[1] == 'S' && vendor_table[2] == 'D' && - vendor_table[3] == ' ' && vendor_table[4] == 'P' && vendor_table[5] == 'T' && - vendor_table[6] == 'R' && vendor_table[7] == ' ') { - kHandoverHeader->f_HardwareTables.f_VendorPtr = (VoidPtr) vendor_table; - break; - } - } - - // ------------------------------------------ // - // draw background color. - // ------------------------------------------ // - - kHandoverHeader->f_GOP.f_The = kGop->Mode->FrameBufferBase; - kHandoverHeader->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution; - kHandoverHeader->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution; - kHandoverHeader->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; - kHandoverHeader->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; - kHandoverHeader->f_GOP.f_Size = kGop->Mode->FrameBufferSize; -#endif // ZBA_USE_FB - - // ------------------------------------------- // - // Grab MP services, extended to runtime. // - // ------------------------------------------- // - - EFI_GUID guid_mp = EFI_GUID(EFI_MP_SERVICES_PROTOCOL_GUID); - EfiMpServicesProtocol* mp = nullptr; - - BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast(&mp)); - kHandoverHeader->f_HardwareTables.f_MpPtr = reinterpret_cast(mp); - - UInt32 cnt_enabled = 0; - UInt32 cnt_disabled = 0; - - if (mp) { - mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); - kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; - } else { - kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled = NO; - } - - //-------------------------------------------------------------// - // TODO: Allocate heap. - //-------------------------------------------------------------// - - // ------------------------------------------ // - // null these fields, to avoid being reused later. - // ------------------------------------------ // - - kHandoverHeader->f_FirmwareCustomTables[0] = nullptr; - kHandoverHeader->f_FirmwareCustomTables[1] = nullptr; - - kHandoverHeader->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - - kHandoverHeader->f_Magic = kHandoverMagic; - kHandoverHeader->f_Version = kHandoverVersion; - - // Provide fimware vendor name. - - Boot::BCopyMem(kHandoverHeader->f_FirmwareVendorName, sys_table->FirmwareVendor, - kHandoverHeader->f_FirmwareVendorLen); - - kHandoverHeader->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); - - Boot::BootFileReader reader_kernel(L"ne_kernel", image_handle); - - reader_kernel.ReadAll(0); - - // ------------------------------------------ // - // If we succeed in reading the blob, then execute it. - // ------------------------------------------ // - - if (reader_kernel.Blob()) { - auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); - kernel_thread.SetName("NeKernel"); - - kHandoverHeader->f_KernelImage = reader_kernel.Blob(); - kHandoverHeader->f_KernelSz = reader_kernel.Size(); - - kernel_thread.Start(kHandoverHeader, YES); - } - - CANT_REACH(); -} diff --git a/src/boot/src/HEL/ARM64/BootEFI.cpp b/src/boot/src/HEL/ARM64/BootEFI.cpp new file mode 100644 index 00000000..6322d311 --- /dev/null +++ b/src/boot/src/HEL/ARM64/BootEFI.cpp @@ -0,0 +1,164 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#ifndef kExpectedWidth +#define kExpectedWidth (800) +#endif + +#ifndef kExpectedHeight +#define kExpectedHeight (600) +#endif + +/** Graphics related. */ + +STATIC EfiGraphicsOutputProtocol* kGop = nullptr; +STATIC UInt16 kGopStride = 0U; +STATIC EFI_GUID kGopGuid; + +EXTERN_C Void rt_reset_hardware(); + +EXTERN EfiBootServices* BS; + +/** + @brief Finds and stores the GOP object. +*/ +STATIC Bool boot_init_fb() { + kGopGuid = EFI_GUID(EFI_GRAPHICS_OUTPUT_PROTOCOL_GUID); + kGop = nullptr; + + if (BS->LocateProtocol(&kGopGuid, nullptr, (VoidPtr*) &kGop) != kEfiOk) return No; + + kGopStride = 4; + + for (SizeT i = 0; i < kGop->Mode->MaxMode; ++i) { + EfiGraphicsOutputProtocolModeInformation* infoPtr = nullptr; + UInt32 sz = 0U; + + kGop->QueryMode(kGop, i, &sz, &infoPtr); + + if (infoPtr->HorizontalResolution == kExpectedWidth && + infoPtr->VerticalResolution == kExpectedHeight) { + kGop->SetMode(kGop, i); + return Yes; + } + } + + return No; +} + +EXTERN EfiBootServices* BS; + +/// @brief BootloaderMain EFI entrypoint. +/// @param image_handle Handle of this image. +/// @param sys_table The system table of it. +/// @return nothing, never returns. +EFI_EXTERN_C EFI_API Int32 BootloaderMain(EfiHandlePtr image_handle, EfiSystemTable* sys_table) { + fw_init_efi(sys_table); ///! Init the EFI library. + + kHandoverHeader = new HEL::BootInfoHeader(); + +#ifdef ZBA_USE_FB + if (!boot_init_fb()) return kEfiFail; ///! Init the GOP. + + for (SizeT index_vt = 0; index_vt < sys_table->NumberOfTableEntries; ++index_vt) { + Char* vendor_table = + reinterpret_cast(sys_table->ConfigurationTable[index_vt].VendorTable); + + // ACPI's 'RSD PTR', which contains the ACPI SDT (MADT, FACP...) + if (vendor_table[0] == 'R' && vendor_table[1] == 'S' && vendor_table[2] == 'D' && + vendor_table[3] == ' ' && vendor_table[4] == 'P' && vendor_table[5] == 'T' && + vendor_table[6] == 'R' && vendor_table[7] == ' ') { + kHandoverHeader->f_HardwareTables.f_VendorPtr = (VoidPtr) vendor_table; + break; + } + } + + // ------------------------------------------ // + // draw background color. + // ------------------------------------------ // + + kHandoverHeader->f_GOP.f_The = kGop->Mode->FrameBufferBase; + kHandoverHeader->f_GOP.f_Width = kGop->Mode->Info->VerticalResolution; + kHandoverHeader->f_GOP.f_Height = kGop->Mode->Info->HorizontalResolution; + kHandoverHeader->f_GOP.f_PixelPerLine = kGop->Mode->Info->PixelsPerScanLine; + kHandoverHeader->f_GOP.f_PixelFormat = kGop->Mode->Info->PixelFormat; + kHandoverHeader->f_GOP.f_Size = kGop->Mode->FrameBufferSize; +#endif // ZBA_USE_FB + + // ------------------------------------------- // + // Grab MP services, extended to runtime. // + // ------------------------------------------- // + + EFI_GUID guid_mp = EFI_GUID(EFI_MP_SERVICES_PROTOCOL_GUID); + EfiMpServicesProtocol* mp = nullptr; + + BS->LocateProtocol(&guid_mp, nullptr, reinterpret_cast(&mp)); + kHandoverHeader->f_HardwareTables.f_MpPtr = reinterpret_cast(mp); + + UInt32 cnt_enabled = 0; + UInt32 cnt_disabled = 0; + + if (mp) { + mp->GetNumberOfProcessors(mp, &cnt_disabled, &cnt_enabled); + kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled = cnt_enabled > 1; + } else { + kHandoverHeader->f_HardwareTables.f_MultiProcessingEnabled = NO; + } + + //-------------------------------------------------------------// + // TODO: Allocate heap. + //-------------------------------------------------------------// + + // ------------------------------------------ // + // null these fields, to avoid being reused later. + // ------------------------------------------ // + + kHandoverHeader->f_FirmwareCustomTables[0] = nullptr; + kHandoverHeader->f_FirmwareCustomTables[1] = nullptr; + + kHandoverHeader->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); + + kHandoverHeader->f_Magic = kHandoverMagic; + kHandoverHeader->f_Version = kHandoverVersion; + + // Provide fimware vendor name. + + Boot::BCopyMem(kHandoverHeader->f_FirmwareVendorName, sys_table->FirmwareVendor, + kHandoverHeader->f_FirmwareVendorLen); + + kHandoverHeader->f_FirmwareVendorLen = Boot::BStrLen(sys_table->FirmwareVendor); + + Boot::BootFileReader reader_kernel(L"ne_kernel", image_handle); + + reader_kernel.ReadAll(0); + + // ------------------------------------------ // + // If we succeed in reading the blob, then execute it. + // ------------------------------------------ // + + if (reader_kernel.Blob()) { + auto kernel_thread = Boot::BootThread(reader_kernel.Blob()); + kernel_thread.SetName("NeKernel"); + + kHandoverHeader->f_KernelImage = reader_kernel.Blob(); + kHandoverHeader->f_KernelSz = reader_kernel.Size(); + + kernel_thread.Start(kHandoverHeader, YES); + } + + CANT_REACH(); +} diff --git a/src/boot/src/HEL/ARM64/BootPlatform.cc b/src/boot/src/HEL/ARM64/BootPlatform.cc deleted file mode 100644 index 8e45fa60..00000000 --- a/src/boot/src/HEL/ARM64/BootPlatform.cc +++ /dev/null @@ -1,25 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include - -#ifdef __BOOTZ_STANDALONE__ - -using namespace Boot; - -EXTERN_C void rt_halt() { - while (Yes); -} - -EXTERN_C void rt_cli() {} - -EXTERN_C void rt_sti() {} - -EXTERN_C void rt_cld() {} - -EXTERN_C void rt_std() {} - -#endif // __BOOTZ_STANDALONE__ diff --git a/src/boot/src/HEL/ARM64/BootPlatform.cpp b/src/boot/src/HEL/ARM64/BootPlatform.cpp new file mode 100644 index 00000000..8e45fa60 --- /dev/null +++ b/src/boot/src/HEL/ARM64/BootPlatform.cpp @@ -0,0 +1,25 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include + +#ifdef __BOOTZ_STANDALONE__ + +using namespace Boot; + +EXTERN_C void rt_halt() { + while (Yes); +} + +EXTERN_C void rt_cli() {} + +EXTERN_C void rt_sti() {} + +EXTERN_C void rt_cld() {} + +EXTERN_C void rt_std() {} + +#endif // __BOOTZ_STANDALONE__ diff --git a/src/boot/src/New+Delete.cc b/src/boot/src/New+Delete.cc deleted file mode 100644 index 20965cbe..00000000 --- a/src/boot/src/New+Delete.cc +++ /dev/null @@ -1,71 +0,0 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) -// Licensed under the Apache License, Version 2.0 (see LICENSE file) -// Official repository: https://github.com/nekernel-org/nekernel - -#include -#include -#include -#include - -#ifdef __BOOTZ_STANDALONE__ - -/// @brief Allocates a new object. -/// @param sz the size. -/// @return -void* operator new(size_t sz) { - void* buf = nullptr; - - while (BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf) != kEfiOk); - - return buf; -} - -/// @brief Allocates a new object. -/// @param sz the size. -/// @return -void* operator new[](size_t sz) { - void* buf = nullptr; - BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf); - - return buf; -} - -/// @brief Deletes the object. -/// @param buf the object. -void operator delete(void* buf) { - if (!buf) return; - - BS->FreePool(buf); -} - -/// @brief Deletes the object. -/// @param buf the object. -void operator delete[](void* buf) { - if (!buf) return; - - BS->FreePool(buf); -} - -/// @brief Deletes the object (array specific). -/// @param buf the object. -/// @param size it's size. -void operator delete(void* buf, size_t size) { - if (!buf) return; - - NE_UNUSED(size); - - BS->FreePool(buf); -} - -/// @brief Deletes the object (array specific). -/// @param buf the object. -/// @param size it's size. -void operator delete[](void* buf, size_t size) { - if (!buf) return; - - NE_UNUSED(size); - - BS->FreePool(buf); -} - -#endif // __BOOTZ_STANDALONE__ diff --git a/src/boot/src/New+Delete.cpp b/src/boot/src/New+Delete.cpp new file mode 100644 index 00000000..20965cbe --- /dev/null +++ b/src/boot/src/New+Delete.cpp @@ -0,0 +1,71 @@ +// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include +#include +#include +#include + +#ifdef __BOOTZ_STANDALONE__ + +/// @brief Allocates a new object. +/// @param sz the size. +/// @return +void* operator new(size_t sz) { + void* buf = nullptr; + + while (BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf) != kEfiOk); + + return buf; +} + +/// @brief Allocates a new object. +/// @param sz the size. +/// @return +void* operator new[](size_t sz) { + void* buf = nullptr; + BS->AllocatePool(EfiMemoryType::EfiLoaderData, sz, &buf); + + return buf; +} + +/// @brief Deletes the object. +/// @param buf the object. +void operator delete(void* buf) { + if (!buf) return; + + BS->FreePool(buf); +} + +/// @brief Deletes the object. +/// @param buf the object. +void operator delete[](void* buf) { + if (!buf) return; + + BS->FreePool(buf); +} + +/// @brief Deletes the object (array specific). +/// @param buf the object. +/// @param size it's size. +void operator delete(void* buf, size_t size) { + if (!buf) return; + + NE_UNUSED(size); + + BS->FreePool(buf); +} + +/// @brief Deletes the object (array specific). +/// @param buf the object. +/// @param size it's size. +void operator delete[](void* buf, size_t size) { + if (!buf) return; + + NE_UNUSED(size); + + BS->FreePool(buf); +} + +#endif // __BOOTZ_STANDALONE__ diff --git a/src/kernel/KernelKit/CoreProcessScheduler.h b/src/kernel/KernelKit/CoreProcessScheduler.h index a6f817f7..9b05d27d 100644 --- a/src/kernel/KernelKit/CoreProcessScheduler.h +++ b/src/kernel/KernelKit/CoreProcessScheduler.h @@ -48,7 +48,7 @@ enum struct TreeKind : UInt32 { }; template -struct ProcessHeapTree { +struct ProcessHeapTree final { static constexpr auto kHeap = true; static constexpr auto kFile = false; static constexpr auto kSpecial = false; @@ -67,7 +67,7 @@ struct ProcessHeapTree { }; template -struct ProcessFileTree { +struct ProcessFileTree final { static constexpr auto kHeap = false; static constexpr auto kFile = true; static constexpr auto kSpecial = false; @@ -88,7 +88,7 @@ struct ProcessFileTree { }; template -struct ProcessSpecialTree { +struct ProcessSpecialTree final { static constexpr auto kHeap = false; static constexpr auto kFile = false; static constexpr auto kSpecial = true; diff --git a/src/kernel/KernelKit/ILoader.h b/src/kernel/KernelKit/ILoader.h index 1cc0742d..a98046b5 100644 --- a/src/kernel/KernelKit/ILoader.h +++ b/src/kernel/KernelKit/ILoader.h @@ -1,4 +1,4 @@ -// Copyright 2024-2025, Amlal El Mahrouss (amlal@nekernel.org) +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) // Licensed under the Apache License, Version 2.0 (see LICENSE file) // Official repository: https://github.com/nekernel-org/nekernel @@ -14,7 +14,7 @@ namespace Kernel { -/// @brief This interface is used to make loader contracts (MSCOFF, PEF). +/// @brief The ILoader interface is used to make dynamic loader contracts (such as: MSCOFF, PEF). /// @author Amlal El Mahrouss class ILoader { public: @@ -25,9 +25,9 @@ class ILoader { public: virtual _Output ErrorOr GetBlob() { return ErrorOr{}; } - virtual _Output const Char* AsString() { return ""; } - virtual _Output const Char* MIME() { return ""; } - virtual _Output const Char* Path() { return ""; } + virtual _Output const Char* AsString() { return "(null)"; } + virtual _Output const Char* MIME() { return "*/*"; } + virtual _Output const Char* Path() { return "(null)"; } virtual _Output ErrorOr FindStart() { return ErrorOr{}; } virtual _Output ErrorOr FindSymbol(_Input const Char*, _Input Int32) { return ErrorOr{}; diff --git a/src/kernel/KernelKit/ZXD.h b/src/kernel/KernelKit/ZXD.h index e56299c9..9ac79af4 100644 --- a/src/kernel/KernelKit/ZXD.h +++ b/src/kernel/KernelKit/ZXD.h @@ -56,8 +56,9 @@ inline constexpr auto kDriverName = ".drvr"; /// @note This is ProcessSanitizer specific. inline constexpr auto kProsanName = ".pros"; +/// @brief Exec ptr alias. using ZxdExecPtr = ZxdExec*; - +/// @brief Exec stub alias. using ZxdStubPtr = ZxdStub*; } // namespace Kernel -- cgit v1.2.3