From a45872967f07906297782cd04223706cfc326219 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 4 Apr 2024 19:46:31 +0200 Subject: NewBoot: Major bootloader improvements, use __EFI_x86_64__ on EFI platforms, add common device class. Meta: Upate specs and kernel-design. Signed-off-by: Amlal El Mahrouss --- .vscode/c_cpp_properties.json | 6 ++-- Private/NewBoot/BootKit/Arch/ATA.hxx | 44 ------------------------- Private/NewBoot/BootKit/Arch/SATA.hxx | 38 --------------------- Private/NewBoot/BootKit/BootKit.hxx | 27 ++++++++++----- Private/NewBoot/BootKit/Device.hxx | 34 +++++++++++++++++++ Private/NewBoot/BootKit/HW/ATA.hxx | 44 +++++++++++++++++++++++++ Private/NewBoot/BootKit/HW/SATA.hxx | 38 +++++++++++++++++++++ Private/NewBoot/NetBoot/EfiModule.cxx | 26 --------------- Private/NewBoot/NetBoot/Module.cxx | 19 +++++++++++ Private/NewBoot/Source/HEL/AMD64/BootAHCI.cxx | 2 +- Private/NewBoot/Source/HEL/AMD64/BootATA.cxx | 2 +- Private/NewBoot/Source/HEL/AMD64/BootEPM.cxx | 16 ++++----- Private/NewBoot/Source/HEL/AMD64/BootMain.cxx | 2 +- Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx | 33 +++++++++++++++++++ Private/NewBoot/Source/makefile | 6 +++- Public/Documentation/Spec.md | 7 ++-- Public/Documentation/kernel-design.drawio | 10 +++--- 17 files changed, 215 insertions(+), 139 deletions(-) delete mode 100644 Private/NewBoot/BootKit/Arch/ATA.hxx delete mode 100644 Private/NewBoot/BootKit/Arch/SATA.hxx create mode 100644 Private/NewBoot/BootKit/Device.hxx create mode 100644 Private/NewBoot/BootKit/HW/ATA.hxx create mode 100644 Private/NewBoot/BootKit/HW/SATA.hxx delete mode 100644 Private/NewBoot/NetBoot/EfiModule.cxx create mode 100644 Private/NewBoot/NetBoot/Module.cxx create mode 100644 Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json index 08cf70c4..6899e66e 100644 --- a/.vscode/c_cpp_properties.json +++ b/.vscode/c_cpp_properties.json @@ -13,7 +13,8 @@ "__KERNEL__", "Z_PREFIX", "__HAVE_MAHROUSS_APIS__", - "__FSKIT_NEWFS__" + "__FSKIT_NEWFS__", + "__EFI_x86_64__" ], "cStandard": "c17", "cppStandard": "c++20", @@ -43,7 +44,8 @@ "__KERNEL__", "Z_PREFIX", "__HAVE_MAHROUSS_APIS__", - "__FSKIT_NEWFS__" + "__FSKIT_NEWFS__", + "__EFI_x86_64__" ], "cStandard": "c17", "cppStandard": "c++20", diff --git a/Private/NewBoot/BootKit/Arch/ATA.hxx b/Private/NewBoot/BootKit/Arch/ATA.hxx deleted file mode 100644 index 5501f947..00000000 --- a/Private/NewBoot/BootKit/Arch/ATA.hxx +++ /dev/null @@ -1,44 +0,0 @@ -/* ------------------------------------------- - - Copyright Mahrouss Logic - -------------------------------------------- */ - -#pragma once - -#include - -using namespace NewOS; - -class BootDeviceATA final { - public: - enum { - kPrimary = ATA_PRIMARY_IO, - kSecondary = ATA_SECONDARY_IO, - }; - - explicit BootDeviceATA() noexcept; - ~BootDeviceATA() = default; - - HCORE_COPY_DEFAULT(BootDeviceATA); - - struct ATATrait final { - SizeT mBase{1024}; - SizeT mSize{1024}; - UInt16 mBus{kPrimary}; - UInt8 mMaster{0}; - Boolean mErr{false}; - - operator bool() { return !mErr; } - }; - - operator bool(); - - BootDeviceATA& Read(Char* Buf, const SizeT& SecCount); - BootDeviceATA& Write(Char* Buf, const SizeT& SecCount); - - ATATrait& Leak(); - - private: - ATATrait mTrait; -}; diff --git a/Private/NewBoot/BootKit/Arch/SATA.hxx b/Private/NewBoot/BootKit/Arch/SATA.hxx deleted file mode 100644 index cbaeb404..00000000 --- a/Private/NewBoot/BootKit/Arch/SATA.hxx +++ /dev/null @@ -1,38 +0,0 @@ -/* ------------------------------------------- - - Copyright Mahrouss Logic - -------------------------------------------- */ - -#pragma once - -#include -#include - -class BootDeviceSATA final { - public: - explicit BootDeviceSATA() noexcept; - ~BootDeviceSATA() = default; - - HCORE_COPY_DEFAULT(BootDeviceSATA); - - struct SATATrait final { - NewOS::SizeT mBase{1024}; - NewOS::Boolean mErr{false}; - NewOS::Boolean mDetected{false}; - - operator bool() { return !this->mErr; } - }; - - operator bool() { return this->Leak().mDetected; } - - BootDeviceSATA& Read(NewOS::WideChar* Buf, const NewOS::SizeT& SecCount); - BootDeviceSATA& Write(NewOS::WideChar* Buf, const NewOS::SizeT& SecCount); - - SATATrait& Leak(); - - private: - SATATrait mTrait; -}; - -#define kAHCISectorSz 4096 diff --git a/Private/NewBoot/BootKit/BootKit.hxx b/Private/NewBoot/BootKit/BootKit.hxx index be46f64c..66a9aa67 100644 --- a/Private/NewBoot/BootKit/BootKit.hxx +++ b/Private/NewBoot/BootKit/BootKit.hxx @@ -16,11 +16,13 @@ class BFileReader; class BFileRunner; class BVersionString; -#include +#include #include +#ifdef __EFI_x86_64__ #include -#include +#endif // ifdef __EFI_x86_64__ #include +#include using namespace NewOS; @@ -42,6 +44,7 @@ typedef Char CharacterTypeUTF8; */ class BTextWriter final { BTextWriter &_Write(const Long &num); + public: BTextWriter &Write(const Long &num); BTextWriter &Write(const UChar *str); @@ -76,7 +79,8 @@ NewOS::SizeT BSetMem(CharacterTypeUTF16 *src, const CharacterTypeUTF16 byte, */ class BFileReader final { public: - explicit BFileReader(const CharacterTypeUTF16 *path, EfiHandlePtr ImageHandle); + explicit BFileReader(const CharacterTypeUTF16 *path, + EfiHandlePtr ImageHandle); ~BFileReader(); Void ReadAll(); @@ -163,9 +167,7 @@ inline UInt32 In32(UInt16 port) { return value; } -inline Void rt_hlt() { - asm volatile("hlt"); -} +inline Void rt_hlt() { asm volatile("hlt"); } #endif // __EFI_x86_64__ @@ -181,7 +183,8 @@ const UInt32 kRgbBlue = 0x00FF0000; const UInt32 kRgbBlack = 0x00000000; const UInt32 kRgbWhite = 0x00FFFFFF; -/** QT GOP and related. */ +#ifdef __EFI_x86_64__ +/** GOP and related. */ inline EfiGraphicsOutputProtocol *kGop; inline UInt16 kStride; inline EfiGUID kGopGuid; @@ -199,10 +202,18 @@ inline Void InitGOP() noexcept { kStride = 4; } +#endif // __EFI_x86_64__ class BVersionString final { public: static const CharacterTypeUTF16 *Shared() { return BOOTLOADER_VERSION; } }; -EXTERN_C Boolean boot_write_epm_partition(const Char* namePart, SizeT namePartLength, BootDeviceATA* ataInterface); +/// @brief Writes an EPM partition on the main disk. +/// @param namePart the partition's name +/// @param namePartLength the partition name's length +/// @param bootDev the disk interface. +/// @return +EXTERN_C Boolean boot_write_epm_partition(const Char *namePart, + SizeT namePartLength, + BootDevice *bootDev); diff --git a/Private/NewBoot/BootKit/Device.hxx b/Private/NewBoot/BootKit/Device.hxx new file mode 100644 index 00000000..bc50a576 --- /dev/null +++ b/Private/NewBoot/BootKit/Device.hxx @@ -0,0 +1,34 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + +------------------------------------------- */ + +#pragma once + +#include + +using namespace NewOS; + +/// @brief Device type. +class Device { + public: + Device() = default; + virtual ~Device() = default; + + HCORE_MOVE_DEFAULT(Device); + + struct Trait { + SizeT mBase{1024}; + SizeT mSize{1024}; +}; + + virtual Trait& Leak() = 0; + + virtual Device& Read(Char* Buf, const SizeT& SecCount) = 0; + virtual Device& Write(Char* Buf, const SizeT& SecCount) = 0; +}; + +typedef Device BootDevice; +typedef Device NetworkDevice; +typedef Device DiskDevice; diff --git a/Private/NewBoot/BootKit/HW/ATA.hxx b/Private/NewBoot/BootKit/HW/ATA.hxx new file mode 100644 index 00000000..40402a86 --- /dev/null +++ b/Private/NewBoot/BootKit/HW/ATA.hxx @@ -0,0 +1,44 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + +------------------------------------------- */ + +#pragma once + +#include +#include + +using namespace NewOS; + +class BootDeviceATA final : public Device { + public: + enum { + kPrimary = ATA_PRIMARY_IO, + kSecondary = ATA_SECONDARY_IO, + }; + + explicit BootDeviceATA() noexcept; + ~BootDeviceATA() = default; + + HCORE_COPY_DEFAULT(BootDeviceATA); + + struct ATATrait final : public Device::Trait { + UInt16 mBus{kPrimary}; + UInt8 mMaster{0}; + Boolean mErr{false}; + + operator bool() { return !mErr; } + }; + + public: + operator bool(); + + BootDeviceATA& Read(Char* Buf, const SizeT& SecCount) override; + BootDeviceATA& Write(Char* Buf, const SizeT& SecCount) override; + + ATATrait& Leak() override; + + private: + ATATrait mTrait; +}; \ No newline at end of file diff --git a/Private/NewBoot/BootKit/HW/SATA.hxx b/Private/NewBoot/BootKit/HW/SATA.hxx new file mode 100644 index 00000000..cbaeb404 --- /dev/null +++ b/Private/NewBoot/BootKit/HW/SATA.hxx @@ -0,0 +1,38 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + +------------------------------------------- */ + +#pragma once + +#include +#include + +class BootDeviceSATA final { + public: + explicit BootDeviceSATA() noexcept; + ~BootDeviceSATA() = default; + + HCORE_COPY_DEFAULT(BootDeviceSATA); + + struct SATATrait final { + NewOS::SizeT mBase{1024}; + NewOS::Boolean mErr{false}; + NewOS::Boolean mDetected{false}; + + operator bool() { return !this->mErr; } + }; + + operator bool() { return this->Leak().mDetected; } + + BootDeviceSATA& Read(NewOS::WideChar* Buf, const NewOS::SizeT& SecCount); + BootDeviceSATA& Write(NewOS::WideChar* Buf, const NewOS::SizeT& SecCount); + + SATATrait& Leak(); + + private: + SATATrait mTrait; +}; + +#define kAHCISectorSz 4096 diff --git a/Private/NewBoot/NetBoot/EfiModule.cxx b/Private/NewBoot/NetBoot/EfiModule.cxx deleted file mode 100644 index e5df4a3e..00000000 --- a/Private/NewBoot/NetBoot/EfiModule.cxx +++ /dev/null @@ -1,26 +0,0 @@ -/* - * ======================================================== - * - * NetBoot - * Copyright Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#include -#include - -EXTERN_C Int32 EfiMain(EfiHandlePtr handle, EfiSystemTable* SystemTable) -{ - InitEFI(ST); - InitGOP(); - - /// - Find a network drive called "/OnlineInstall" - /// - Download our image - /// - Boot from it. - - BTextWriter writer; - writer.Write(L"NetBoot.exe: Updating from OTP...\r\n"); - - return kEfiOk; -} diff --git a/Private/NewBoot/NetBoot/Module.cxx b/Private/NewBoot/NetBoot/Module.cxx new file mode 100644 index 00000000..c89d0a5f --- /dev/null +++ b/Private/NewBoot/NetBoot/Module.cxx @@ -0,0 +1,19 @@ +/* + * ======================================================== + * + * NetBoot + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#include + +EXTERN_C Int32 EfiMain(Void) +{ + /// - Find a network drive called "/OnlineBoot" + /// - Download our image + /// - Boot from it. + + return kEfiOk; +} diff --git a/Private/NewBoot/Source/HEL/AMD64/BootAHCI.cxx b/Private/NewBoot/Source/HEL/AMD64/BootAHCI.cxx index be7010a8..d736ac59 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootAHCI.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootAHCI.cxx @@ -15,4 +15,4 @@ * */ -#include +#include diff --git a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx index 3f309f31..2a2852fb 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx @@ -15,7 +15,7 @@ * */ -#include +#include #include /// bugs: 0 diff --git a/Private/NewBoot/Source/HEL/AMD64/BootEPM.cxx b/Private/NewBoot/Source/HEL/AMD64/BootEPM.cxx index e767cb2d..cca9a6ca 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootEPM.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootEPM.cxx @@ -19,19 +19,19 @@ STATIC const BlockGUID kEPMGuid = { /// @brief Write epm partition to disk. /// @param namePart partition name /// @param namePartLength length of name -/// @param ataInterface disk interface, here ATA. +/// @param bootDev disk interface. /// @return EXTERN_C Boolean boot_write_epm_partition(const Char* namePart, SizeT namePartLength, - BootDeviceATA* ataInterface) { + BootDevice* bootDev) { if (namePartLength > kEPMNameLength || !namePart) return No; - if (!ataInterface) return No; + if (!bootDev) return No; - ataInterface->Leak().mBase = kEPMStartPartitionBlk; - ataInterface->Leak().mSize = kATASectorSize; + bootDev->Leak().mBase = kEPMStartPartitionBlk; + bootDev->Leak().mSize = kATASectorSize; - Char buf[512] = {0}; + Char buf[kATASectorSize] = {0}; - ataInterface->Read(buf, 1); + bootDev->Read(buf, 1); BTextWriter writer; @@ -106,7 +106,7 @@ EXTERN_C Boolean boot_write_epm_partition(const Char* namePart, SizeT namePartLe swapBlock->Kind = kNewFSPartitionTypePage; swapBlock->LbaEnd = kSwapSize; /// 4 MIB swap partition. - ataInterface->Write(buf, 1); + bootDev->Write(buf, 1); return No; } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx index f4eaee33..f2d893c2 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx @@ -173,7 +173,7 @@ EFI_EXTERN_C EFI_API Int EfiMain(EfiHandlePtr ImageHandle, handoverHdrPtr->f_Magic = kHandoverMagic; handoverHdrPtr->f_Version = kHandoverVersion; - writer.Write(L"NewOS: Running NewOS...\r\n"); + writer.Write(L"NewOS: Starting kernel...\r\n"); EFI::ExitBootServices(MapKey, ImageHandle); diff --git a/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx b/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx new file mode 100644 index 00000000..af7f2f00 --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/New+Delete.cxx @@ -0,0 +1,33 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + +------------------------------------------- */ + +#include + +/// @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) +{ + 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) +{ + BS->FreePool(buf); +} \ No newline at end of file diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index b9413bb4..4f076580 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -26,7 +26,7 @@ REM=rm REM_FLAG=-f FLAG_ASM=-f win64 -FLAG_GNU=-fshort-wchar -mgeneral-regs-only -mno-red-zone -D__KERNEL__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -D__BOOTLOADER__ -I./ +FLAG_GNU=-fshort-wchar -D__EFI_x86_64__ -mgeneral-regs-only -mno-red-zone -D__KERNEL__ -DEFI_FUNCTION_WRAPPER -I../ -I../../ -I./ -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_MAHROUSS_APIS__ -D__MAHROUSS__ -D__BOOTLOADER__ -I./ .PHONY: invalid-recipe invalid-recipe: @@ -38,6 +38,10 @@ all: compile-amd64 $(COPY) NewBoot.exe CDROM/EFI/BOOT/BOOTX64.EFI $(COPY) NewBoot.exe CDROM/EFI/BOOT/NEWBOOT.EFI +ifneq ($(DEBUG_SUPPORT), ) +DEBUG = -D__DEBUG__ +endif + .PHONY: compile-amd64 compile-amd64: windres BootloaderRsrc.rsrc -O coff -o BootloaderRsrc.o diff --git a/Public/Documentation/Spec.md b/Public/Documentation/Spec.md index f2b097a0..aafba3d2 100644 --- a/Public/Documentation/Spec.md +++ b/Public/Documentation/Spec.md @@ -28,7 +28,7 @@ # 2: The Filesystem =================================== -- Catalog based with forks. +- Catalog object with associated forks. - Large storage support. - Long file names. - UNIX path style. @@ -46,8 +46,7 @@ =================================== - Capable of booting from a network drive. -- Loads a PE file which is the kernel +- Loads a PE file which is the kernel. - Sanity checks, based on the number of sections. - Handover compliant. -- Does check for a valid invalid of NewOS (useful in the case of recovering) - +- Does check for a valid partition (useful in the case of recovering) diff --git a/Public/Documentation/kernel-design.drawio b/Public/Documentation/kernel-design.drawio index 90370ab0..d78ac170 100644 --- a/Public/Documentation/kernel-design.drawio +++ b/Public/Documentation/kernel-design.drawio @@ -25,7 +25,7 @@ - + @@ -49,19 +49,19 @@ - + - + - + - + -- cgit v1.2.3