From 70e95128b11dbb535e4679cbacb1cbeaa414c822 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 27 Jan 2024 10:26:31 +0100 Subject: Kernel: Improving AMD64 HAL, Add TIBInstall.asm, currently Working on SMPManager and Bootloader. Signed-off-by: Amlal El Mahrouss --- Private/HALKit/AMD64/DebugManager.asm | 7 +- Private/HALKit/AMD64/HalPageAlloc.cpp | 2 +- Private/HALKit/AMD64/HalPageAlloc.hpp | 2 + Private/HALKit/AMD64/SMPCoreManager.asm | 23 ++- Private/HALKit/AMD64/TIBInstall.asm | 18 +++ Private/KernelKit/FileManager.hpp | 10 +- Private/KernelKit/ThreadLocalStorage.hxx | 20 ++- Private/NetworkKit/IP.hpp | 25 +-- Private/NewBoot/BootKit/Boot.hpp | 77 +++++++++ Private/NewBoot/CxxKit/__cxxkit_unwind.cxx | 11 ++ Private/NewBoot/CxxKit/manifest.json | 3 + Private/NewBoot/NetBoot/.hgkeep | 0 Private/NewBoot/NetBoot/manifest.json | 4 + Private/NewBoot/NetBoot/module.cxx | 13 ++ Private/NewBoot/Source/.gitkeep | 0 Private/NewBoot/Source/BootNotes.txt | 9 ++ Private/NewBoot/Source/HEL/AMD64/.gitkeep | 0 Private/NewBoot/Source/HEL/AMD64/ATA.cxx | 172 +++++++++++++++++++++ Private/NewBoot/Source/HEL/AMD64/ATA.hxx | 106 +++++++++++++ Private/NewBoot/Source/HEL/AMD64/BIOSAllocApi.inc | 40 +++++ Private/NewBoot/Source/HEL/AMD64/BIOSApiGdt.inc | 36 +++++ Private/NewBoot/Source/HEL/AMD64/BIOSRuntime.cxx | 8 + Private/NewBoot/Source/HEL/AMD64/BIOSRuntime0.asm | 50 ++++++ Private/NewBoot/Source/HEL/AMD64/BootAMD64.cxx | 60 +++++++ Private/NewBoot/Source/HEL/AMD64/EFIApi.hxx | 25 +++ Private/NewBoot/Source/HEL/AMD64/EFIApiCrt0.cxx | 33 ++++ Private/NewBoot/Source/HEL/AMD64/Processor.cxx | 23 +++ Private/NewBoot/Source/HEL/AMD64/makefile | 37 +++++ Private/NewBoot/Source/HEL/PowerPC/.gitkeep | 0 Private/NewBoot/Source/MPT/.hgkeep | 0 Private/NewBoot/Source/MPT/API.cxx | 70 +++++++++ Private/NewBoot/Source/MPT/API.hxx | 17 ++ Private/NewBoot/Source/MPT/Detail.hxx | 52 +++++++ Private/NewBoot/Source/MPT/FileType.hxx | 36 +++++ Private/NewBoot/Source/MPT/MPT.hxx | 29 ++++ Private/NewBoot/Source/Start.cxx | 17 ++ Private/NewBoot/Source/makefile | 19 +++ Private/Source/FilesystemIndexer.cxx | 28 ---- Private/Source/IndexableProperty.cxx | 28 ++++ Private/Source/Network/IP.cpp | 11 +- Private/Source/NewFS-Journal.cxx | 1 - Private/Source/ThreadLocalStorage.cxx | 6 +- Private/newBoot/BootKit/Boot.hpp | 77 --------- Private/newBoot/Source/.gitkeep | 0 Private/newBoot/Source/Arch/AMD64/.gitkeep | 0 Private/newBoot/Source/Arch/AMD64/ATA.cxx | 172 --------------------- Private/newBoot/Source/Arch/AMD64/ATA.hxx | 106 ------------- Private/newBoot/Source/Arch/AMD64/BIOSAllocApi.inc | 40 ----- Private/newBoot/Source/Arch/AMD64/BIOSApiGdt.inc | 36 ----- Private/newBoot/Source/Arch/AMD64/BIOSRuntime.cxx | 8 - Private/newBoot/Source/Arch/AMD64/BIOSRuntime0.asm | 50 ------ Private/newBoot/Source/Arch/AMD64/BootAMD64.cxx | 60 ------- Private/newBoot/Source/Arch/AMD64/EFIApi.hxx | 25 --- Private/newBoot/Source/Arch/AMD64/EFIApiCrt0.cxx | 33 ---- Private/newBoot/Source/Arch/AMD64/Processor.cxx | 23 --- Private/newBoot/Source/Arch/AMD64/makefile | 33 ---- Private/newBoot/Source/Arch/PowerPC/.gitkeep | 0 Private/newBoot/Source/BootNotes.txt | 9 -- Private/newBoot/Source/MPT/.hgkeep | 0 Private/newBoot/Source/MPT/API.cxx | 70 --------- Private/newBoot/Source/MPT/API.hxx | 17 -- Private/newBoot/Source/MPT/Detail.hxx | 52 ------- Private/newBoot/Source/MPT/FileType.hxx | 36 ----- Private/newBoot/Source/MPT/MPT.hxx | 29 ---- Private/newBoot/Source/Start.cxx | 17 -- Private/newBoot/Source/makefile | 19 --- Private/newBoot/cxxKitModule/__cxxkit_unwind.cxx | 11 -- Private/newBoot/cxxKitModule/manifest.json | 3 - Private/newBoot/netBootModule/.hgkeep | 0 Private/newBoot/netBootModule/manifest.json | 4 - Private/newBoot/netBootModule/module.cxx | 13 -- Public/SDK/DriverKit/HW.hxx | 45 ++++++ Public/SDK/DriverKit/HWInterface.hpp | 44 ------ Public/SDK/DriverKit/TIB.hxx | 41 +++++ 74 files changed, 1153 insertions(+), 1048 deletions(-) create mode 100644 Private/HALKit/AMD64/TIBInstall.asm create mode 100644 Private/NewBoot/BootKit/Boot.hpp create mode 100644 Private/NewBoot/CxxKit/__cxxkit_unwind.cxx create mode 100644 Private/NewBoot/CxxKit/manifest.json create mode 100644 Private/NewBoot/NetBoot/.hgkeep create mode 100644 Private/NewBoot/NetBoot/manifest.json create mode 100644 Private/NewBoot/NetBoot/module.cxx create mode 100644 Private/NewBoot/Source/.gitkeep create mode 100644 Private/NewBoot/Source/BootNotes.txt create mode 100644 Private/NewBoot/Source/HEL/AMD64/.gitkeep create mode 100644 Private/NewBoot/Source/HEL/AMD64/ATA.cxx create mode 100644 Private/NewBoot/Source/HEL/AMD64/ATA.hxx create mode 100644 Private/NewBoot/Source/HEL/AMD64/BIOSAllocApi.inc create mode 100644 Private/NewBoot/Source/HEL/AMD64/BIOSApiGdt.inc create mode 100644 Private/NewBoot/Source/HEL/AMD64/BIOSRuntime.cxx create mode 100644 Private/NewBoot/Source/HEL/AMD64/BIOSRuntime0.asm create mode 100644 Private/NewBoot/Source/HEL/AMD64/BootAMD64.cxx create mode 100644 Private/NewBoot/Source/HEL/AMD64/EFIApi.hxx create mode 100644 Private/NewBoot/Source/HEL/AMD64/EFIApiCrt0.cxx create mode 100644 Private/NewBoot/Source/HEL/AMD64/Processor.cxx create mode 100644 Private/NewBoot/Source/HEL/AMD64/makefile create mode 100644 Private/NewBoot/Source/HEL/PowerPC/.gitkeep create mode 100644 Private/NewBoot/Source/MPT/.hgkeep create mode 100644 Private/NewBoot/Source/MPT/API.cxx create mode 100644 Private/NewBoot/Source/MPT/API.hxx create mode 100644 Private/NewBoot/Source/MPT/Detail.hxx create mode 100644 Private/NewBoot/Source/MPT/FileType.hxx create mode 100644 Private/NewBoot/Source/MPT/MPT.hxx create mode 100644 Private/NewBoot/Source/Start.cxx create mode 100644 Private/NewBoot/Source/makefile delete mode 100644 Private/Source/FilesystemIndexer.cxx create mode 100644 Private/Source/IndexableProperty.cxx delete mode 100644 Private/newBoot/BootKit/Boot.hpp delete mode 100644 Private/newBoot/Source/.gitkeep delete mode 100644 Private/newBoot/Source/Arch/AMD64/.gitkeep delete mode 100644 Private/newBoot/Source/Arch/AMD64/ATA.cxx delete mode 100644 Private/newBoot/Source/Arch/AMD64/ATA.hxx delete mode 100644 Private/newBoot/Source/Arch/AMD64/BIOSAllocApi.inc delete mode 100644 Private/newBoot/Source/Arch/AMD64/BIOSApiGdt.inc delete mode 100644 Private/newBoot/Source/Arch/AMD64/BIOSRuntime.cxx delete mode 100644 Private/newBoot/Source/Arch/AMD64/BIOSRuntime0.asm delete mode 100644 Private/newBoot/Source/Arch/AMD64/BootAMD64.cxx delete mode 100644 Private/newBoot/Source/Arch/AMD64/EFIApi.hxx delete mode 100644 Private/newBoot/Source/Arch/AMD64/EFIApiCrt0.cxx delete mode 100644 Private/newBoot/Source/Arch/AMD64/Processor.cxx delete mode 100644 Private/newBoot/Source/Arch/AMD64/makefile delete mode 100644 Private/newBoot/Source/Arch/PowerPC/.gitkeep delete mode 100644 Private/newBoot/Source/BootNotes.txt delete mode 100644 Private/newBoot/Source/MPT/.hgkeep delete mode 100644 Private/newBoot/Source/MPT/API.cxx delete mode 100644 Private/newBoot/Source/MPT/API.hxx delete mode 100644 Private/newBoot/Source/MPT/Detail.hxx delete mode 100644 Private/newBoot/Source/MPT/FileType.hxx delete mode 100644 Private/newBoot/Source/MPT/MPT.hxx delete mode 100644 Private/newBoot/Source/Start.cxx delete mode 100644 Private/newBoot/Source/makefile delete mode 100644 Private/newBoot/cxxKitModule/__cxxkit_unwind.cxx delete mode 100644 Private/newBoot/cxxKitModule/manifest.json delete mode 100644 Private/newBoot/netBootModule/.hgkeep delete mode 100644 Private/newBoot/netBootModule/manifest.json delete mode 100644 Private/newBoot/netBootModule/module.cxx create mode 100644 Public/SDK/DriverKit/HW.hxx delete mode 100644 Public/SDK/DriverKit/HWInterface.hpp create mode 100644 Public/SDK/DriverKit/TIB.hxx diff --git a/Private/HALKit/AMD64/DebugManager.asm b/Private/HALKit/AMD64/DebugManager.asm index 6c0bf149..0c717039 100644 --- a/Private/HALKit/AMD64/DebugManager.asm +++ b/Private/HALKit/AMD64/DebugManager.asm @@ -8,7 +8,6 @@ ;; */ [global rt_debug_fence] -[global rt_debug_fence_end] [global __rt_debug_int_3] ;; //////////////////////////////////////////////////// ;; @@ -25,8 +24,10 @@ L0: jmp $ rt_debug_fence: - push __rt_debug_record_table + mov [__rt_debug_record_table], rsi + push rsi jmp [rbx] -rt_debug_fence_end: + pop rsi + ret ;; //////////////////////////////////////////////////// ;; \ No newline at end of file diff --git a/Private/HALKit/AMD64/HalPageAlloc.cpp b/Private/HALKit/AMD64/HalPageAlloc.cpp index ccdfa8b6..07cfbecf 100644 --- a/Private/HALKit/AMD64/HalPageAlloc.cpp +++ b/Private/HALKit/AMD64/HalPageAlloc.cpp @@ -13,7 +13,7 @@ // this files handles paging. -static hCore::UIntPtr kPagePtr = 0x0900000; +static hCore::UIntPtr kPagePtr = kPagePtrAddress; static hCore::SizeT kPageCnt = 0UL; namespace hCore diff --git a/Private/HALKit/AMD64/HalPageAlloc.hpp b/Private/HALKit/AMD64/HalPageAlloc.hpp index 4f5f8592..06a883bf 100644 --- a/Private/HALKit/AMD64/HalPageAlloc.hpp +++ b/Private/HALKit/AMD64/HalPageAlloc.hpp @@ -19,6 +19,8 @@ #define PTE_ALIGN (4096) #endif //! PTE_ALIGN +#define kPagePtrAddress 0x0900000 + extern "C" void flush_tlb(hCore::UIntPtr VirtualAddr); extern "C" void write_cr3(hCore::UIntPtr pde); extern "C" void write_cr0(hCore::UIntPtr bit); diff --git a/Private/HALKit/AMD64/SMPCoreManager.asm b/Private/HALKit/AMD64/SMPCoreManager.asm index a528f847..ed56d7db 100644 --- a/Private/HALKit/AMD64/SMPCoreManager.asm +++ b/Private/HALKit/AMD64/SMPCoreManager.asm @@ -1,16 +1,25 @@ +;; /* +;; * ======================================================== +;; * +;; * hCore +;; * Copyright 2024 Mahrouss Logic, all rights reserved. +;; * +;; * ======================================================== +;; */ + [bits 64] [global rt_do_context_switch] [extern rt_debug_fence] -[extern rt_debug_fence_end] rt_do_context_switch: - mov rsi, [rt_do_context_switch] + mov rsi, [rt_do_context_switch_unprotected] call rt_debug_fence - mov rsi, rdi - mov rax, rsi - - call rt_debug_fence_end + iret - iret \ No newline at end of file +rt_do_context_switch_unprotected: + mov [rdi+0], rax + mov [rdi+8], rbx + mov [rdi+16], rcx + ret \ No newline at end of file diff --git a/Private/HALKit/AMD64/TIBInstall.asm b/Private/HALKit/AMD64/TIBInstall.asm new file mode 100644 index 00000000..69aa7503 --- /dev/null +++ b/Private/HALKit/AMD64/TIBInstall.asm @@ -0,0 +1,18 @@ +;; /* +;; * ======================================================== +;; * +;; * hCore +;; * Copyright 2024 Mahrouss Logic, all rights reserved. +;; * +;; * ======================================================== +;; */ + +[bits 64] + +[global rt_install_tib] + +rt_install_tib: + mov rsi, gs + ret + +;; //////////////////////////////////////////////////// ;; \ No newline at end of file diff --git a/Private/KernelKit/FileManager.hpp b/Private/KernelKit/FileManager.hpp index 52dfb267..311527f9 100644 --- a/Private/KernelKit/FileManager.hpp +++ b/Private/KernelKit/FileManager.hpp @@ -77,10 +77,13 @@ namespace hCore }; + /** + * @brief Child of IFilesystemManager, takes care of managing NewFS disks. + */ class NewFilesystemManager final : public IFilesystemManager { public: - NewFilesystemManager(); + explicit NewFilesystemManager(); ~NewFilesystemManager() override; public: @@ -113,6 +116,11 @@ namespace hCore }; + /** + * Usable FileStream + * @tparam Encoding file encoding (char, wchar_t...) + * @tparam FSClass Filesystem contract who takes care of it. + */ template class FileStream final { diff --git a/Private/KernelKit/ThreadLocalStorage.hxx b/Private/KernelKit/ThreadLocalStorage.hxx index 518adb32..8b3f2c9b 100644 --- a/Private/KernelKit/ThreadLocalStorage.hxx +++ b/Private/KernelKit/ThreadLocalStorage.hxx @@ -27,11 +27,25 @@ bool hcore_tls_delete_ptr(T* ptr); template T* hcore_tls_new_class(Args&&... args); -//! @brief Cookie Sanity check. -hCore::Boolean hcore_tls_check(hCore::VoidPtr ptr); - typedef char rt_cookie_type[3]; +/// @brief Thread Information Block for Local Storage. +/// Located in GS on AMD64, Virtual Address 0x10000 (64x0, 32x0, ARM64) +struct ThreadInformationBlock final +{ + hCore::Char Name[255]; // Module Name + hCore::UIntPtr StartCode; // Start Address + hCore::UIntPtr StartData; // Allocation Heap + hCore::UIntPtr StartStack; // Stack Pointer. + hCore::Int32 Arch; // Architecture and/or platform. + rt_cookie_type Cookie; // Not shown in public header, this is the way we tell something went wrong. +}; + +//! @brief Cookie Sanity check. +hCore::Boolean hcore_tls_check(ThreadInformationBlock* ptr); + #include "ThreadLocalStorage.inl" +// last rev 1/27/24 + #endif /* ifndef _KERNELKIT_TLS_HPP */ diff --git a/Private/NetworkKit/IP.hpp b/Private/NetworkKit/IP.hpp index 7323296f..13fa0c9d 100644 --- a/Private/NetworkKit/IP.hpp +++ b/Private/NetworkKit/IP.hpp @@ -18,22 +18,19 @@ namespace hCore { class RawIPAddress6; class RawIPAddress; -class NetworkManager; +class IPFactory; class RawIPAddress final { private: - RawIPAddress(char bytes[4]); + explicit RawIPAddress(char bytes[4]); ~RawIPAddress() = default; RawIPAddress &operator=(const RawIPAddress &) = delete; RawIPAddress(const RawIPAddress &) = default; public: - char *Address() - { - return m_Addr; - } + char *Address(); char &operator[](const Size &index); @@ -43,14 +40,17 @@ class RawIPAddress final private: char m_Addr[4]; - friend NetworkManager; // it is the one creating these addresses, thus this + friend IPFactory; // it is the one creating these addresses, thus this // is why the constructors are private. }; +/** + * @brief IPv6 address. + */ class RawIPAddress6 final { private: - RawIPAddress6(char Bytes[8]); + explicit RawIPAddress6(char Bytes[8]); ~RawIPAddress6() = default; RawIPAddress6 &operator=(const RawIPAddress6 &) = delete; @@ -70,15 +70,18 @@ class RawIPAddress6 final private: char m_Addr[8]; - friend NetworkManager; + friend IPFactory; }; -class IPHelper +/** + * @brief IP Creation helpers + */ +class IPFactory final { public: static ErrorOr ToStringView(Ref ipv6); static ErrorOr ToStringView(Ref ipv4); - static bool IpCheckV4(const char *ip); + static bool IpCheckVersion4(const char *ip); }; } // namespace hCore diff --git a/Private/NewBoot/BootKit/Boot.hpp b/Private/NewBoot/BootKit/Boot.hpp new file mode 100644 index 00000000..7323737e --- /dev/null +++ b/Private/NewBoot/BootKit/Boot.hpp @@ -0,0 +1,77 @@ +/* + * ======================================================== + * + * NewBoot + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include + +using namespace hCore; + +typedef void* PEFImage; + +enum +{ + kSegmentCode = 2, + kSegmentData = 4, + kSegmentBss = 6, +}; + +/** + * @brief BootKit Text Writer class + * Writes to VGA. + */ +class BKTextWriter final +{ + volatile UInt16* fWhere{ nullptr }; + +public: + void WriteString(const char* c, + unsigned char forecolour, + unsigned char backcolour, + int x, + int y); + + void WriteCharacter(char c, + unsigned char forecolour, + unsigned char backcolour, + int x, + int y); + +public: + BKTextWriter() = default; + ~BKTextWriter() = default; + +public: + BKTextWriter& operator=(const BKTextWriter&) = default; + BKTextWriter(const BKTextWriter&) = default; + +}; + +enum +{ + kBlack, + kBlue, + kGreen, + kCyan, + kRed, + kMagenta, + kBrown, + kLightGray, + kDarkGray, + kLightBlue, + kLightGreen, + kLightCyan, + kLightRed, + kLightMagenta, + kYellow, + kWhite, +}; + +#define BK_START_KERNEL (0x00080000) + diff --git a/Private/NewBoot/CxxKit/__cxxkit_unwind.cxx b/Private/NewBoot/CxxKit/__cxxkit_unwind.cxx new file mode 100644 index 00000000..477b6302 --- /dev/null +++ b/Private/NewBoot/CxxKit/__cxxkit_unwind.cxx @@ -0,0 +1,11 @@ +namespace cxxkit +{ + ///! @brief C++ ABI unwinding + ///! finis array (r1) + ///! n of finis (r2) + void __unwind(void(**finis)(void), int cnt) + { + for (int i = 0; i < cnt; ++i) + (finis[i])(); + } +} diff --git a/Private/NewBoot/CxxKit/manifest.json b/Private/NewBoot/CxxKit/manifest.json new file mode 100644 index 00000000..42b5d166 --- /dev/null +++ b/Private/NewBoot/CxxKit/manifest.json @@ -0,0 +1,3 @@ +{ + "pluginName": "C++Kit" +} \ No newline at end of file diff --git a/Private/NewBoot/NetBoot/.hgkeep b/Private/NewBoot/NetBoot/.hgkeep new file mode 100644 index 00000000..e69de29b diff --git a/Private/NewBoot/NetBoot/manifest.json b/Private/NewBoot/NetBoot/manifest.json new file mode 100644 index 00000000..1a177877 --- /dev/null +++ b/Private/NewBoot/NetBoot/manifest.json @@ -0,0 +1,4 @@ +{ + "pluginName": "internetBoot", + "description": "internetBoot your device from the network." +} \ No newline at end of file diff --git a/Private/NewBoot/NetBoot/module.cxx b/Private/NewBoot/NetBoot/module.cxx new file mode 100644 index 00000000..759e5cfe --- /dev/null +++ b/Private/NewBoot/NetBoot/module.cxx @@ -0,0 +1,13 @@ +/* + * ======================================================== + * + * NetBoot + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +extern "C" void __Module(const char* ip, long iplen) +{ + +} diff --git a/Private/NewBoot/Source/.gitkeep b/Private/NewBoot/Source/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Private/NewBoot/Source/BootNotes.txt b/Private/NewBoot/Source/BootNotes.txt new file mode 100644 index 00000000..e09e9cce --- /dev/null +++ b/Private/NewBoot/Source/BootNotes.txt @@ -0,0 +1,9 @@ +bootloader roadmap: + +- Rom is being mapped. +- Move boot code to link address (8M) +- Decompress kernel using newboot_decompress_pef() +- Find start image. +- Run image at 8M page zero. + +- Amlal diff --git a/Private/NewBoot/Source/HEL/AMD64/.gitkeep b/Private/NewBoot/Source/HEL/AMD64/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Private/NewBoot/Source/HEL/AMD64/ATA.cxx b/Private/NewBoot/Source/HEL/AMD64/ATA.cxx new file mode 100644 index 00000000..7bcd304a --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/ATA.cxx @@ -0,0 +1,172 @@ +/* + * Copyright 2024 Mahrouss Logic, all rights reserved + */ + +#include "ATA.hxx" +#include + +using namespace hCore::HAL; + +static Boolean kATADetected = false; + +void IDESelect(UInt8 Bus, Boolean isMaster) +{ + if (Bus == ATA_PRIMARY) + out8(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, + isMaster ? ATA_PRIMARY_SEL : ATA_SECONDARY_SEL); + else + out8(ATA_SECONDARY_IO + ATA_REG_HDDEVSEL, + isMaster ? ATA_PRIMARY_SEL : ATA_SECONDARY_SEL); +} + +Boolean ATAInitDriver(UInt8 Bus, UInt8 Drive) +{ + UInt16 IO = (Bus == ATA_PRIMARY) ? ATA_PRIMARY_IO : ATA_SECONDARY_IO; + + IDESelect(Bus, Drive); + + out8(IO + ATA_REG_SEC_COUNT0, 0); + out8(IO + ATA_REG_LBA0, 0); + out8(IO + ATA_REG_LBA1, 0); + out8(IO + ATA_REG_LBA2, 0); + + out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); + + UInt8 status = in8(IO + ATA_REG_STATUS); + + if (status) + { + while ((status = in8(IO + ATA_REG_STATUS) & ATA_SR_BSY)) + ; + + if (status & ATA_REG_ERROR) + { + return false; + } + + kATADetected = true; + return status; + } + + return false; +} + +void ATAWait(UInt16 IO) +{ + for (int i = 0; i < 4000; i++) + in8(IO + ATA_REG_ALT_STATUS); +} + +void IDEPoll(UInt16 IO) { ATAWait(IO); } + +UInt16 ATAReadLba(UInt32 lba, UInt8 bus, Boolean master) +{ + UInt16 IO = bus; + IDESelect(IO, master ? ATA_MASTER : ATA_SLAVE); + + out8(IO + ATA_REG_LBA5, (UInt8)(lba >> 24) & 0xF); + + out8(IO + ATA_REG_SEC_COUNT0, lba / 512); + + out8(IO + ATA_REG_LBA0, (UInt8)lba); + out8(IO + ATA_REG_LBA1, (UInt8)(lba >> 8)); + out8(IO + ATA_REG_LBA2, (UInt8)(lba >> 16)); + + out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); + + IDEPoll(IO); + + UInt16 data = in16(IO + ATA_REG_DATA); + + while ((in8(ATA_COMMAND(IO))) & 0x88) + ATAWait(IO); + + return data; +} + +Void ATAWriteLba(UInt16 Byte, UInt32 lba, UInt8 bus, Boolean master) +{ + UInt16 IO = bus; + IDESelect(IO, master ? ATA_MASTER : ATA_SLAVE); + + out8(IO + ATA_REG_LBA5, (UInt8)(lba >> 24) & 0xF); + + out8(IO + ATA_REG_SEC_COUNT0, lba / 512); + + out8(IO + ATA_REG_LBA0, (UInt8)lba); + out8(IO + ATA_REG_LBA1, (UInt8)(lba >> 8)); + out8(IO + ATA_REG_LBA2, (UInt8)(lba >> 16)); + + out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); // TODO: support DMA + + IDEPoll(IO); + + out32(IO + ATA_REG_DATA, Byte); + + while ((in8(ATA_COMMAND(IO))) & 0x88) + ATAWait(IO); +} + +Boolean ATAIsDetected(Void) { return kATADetected; } + +extern "C" void _start(void) +{ + ATAInitDriver(ATA_PRIMARY, true); + ATAInitDriver(ATA_PRIMARY, false); + + ATAInitDriver(ATA_SECONDARY, true); + ATAInitDriver(ATA_SECONDARY, false); +} + +extern "C" void out8(UInt16 port, UInt8 value) +{ + asm volatile("outb %%al, %1" : : "a"(value), "Nd"(port) : "memory"); +} + +extern "C" void out16(UInt16 port, UInt16 value) +{ + asm volatile("outw %%ax, %1" : : "a"(value), "Nd"(port) : "memory"); +} + +extern "C" void out32(UInt16 port, UInt32 value) +{ + asm volatile("outl %%eax, %1" : : "a"(value), "Nd"(port) : "memory"); +} + +extern "C" UInt8 in8(UInt16 port) +{ + UInt8 value = 0UL; + asm volatile("inb %1, %%al" : "=a"(value) : "Nd"(port) : "memory"); + + return value; +} + +extern "C" UInt16 in16(UInt16 port) +{ + UInt16 value = 0UL; + asm volatile("inw %1, %%ax" : "=a"(value) : "Nd"(port) : "memory"); + + return value; +} + +extern "C" UInt32 in32(UInt16 port) +{ + UInt32 value = 0UL; + asm volatile("inl %1, %%eax" : "=a"(value) : "Nd"(port) : "memory"); + + return value; +} + +extern "C" int init_ata_mpt(void) +{ + for (int i = 0; i < 255; ++i) + { + if (ATAInitDriver(i, ATA_MASTER)) + { + ATAInitDriver(i, ATA_SLAVE); + return 1; + } + } + + return 0; +} \ No newline at end of file diff --git a/Private/NewBoot/Source/HEL/AMD64/ATA.hxx b/Private/NewBoot/Source/HEL/AMD64/ATA.hxx new file mode 100644 index 00000000..09cc4007 --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/ATA.hxx @@ -0,0 +1,106 @@ +#pragma once + +#include + +using namespace hCore; + +// Status register +#define ATA_SR_BSY 0x80 +#define ATA_SR_DRDY 0x40 +#define ATA_SR_DF 0x20 +#define ATA_SR_DSC 0x10 +#define ATA_SR_DRQ 0x08 +#define ATA_SR_CORR 0x04 +#define ATA_SR_IDX 0x02 +#define ATA_SR_ERR 0x01 + +// Error register +#define ATA_ER_BBK 0x80 +#define ATA_ER_UNC 0x40 +#define ATA_ER_MC 0x20 +#define ATA_ER_IDNF 0x10 +#define ATA_ER_MCR 0x08 +#define ATA_ER_ABRT 0x04 +#define ATA_ER_TK0NF 0x02 +#define ATA_ER_AMNF 0x01 + +#define ATA_CMD_READ_PIO 0x20 +#define ATA_CMD_READ_PIO_EXT 0x24 +#define ATA_CMD_READ_DMA 0xC8 +#define ATA_CMD_READ_DMA_EXT 0x25 +#define ATA_CMD_WRITE_PIO 0x30 +#define ATA_CMD_WRITE_PIO_EXT 0x34 +#define ATA_CMD_WRITE_DMA 0xCA +#define ATA_CMD_WRITE_DMA_EXT 0x35 +#define ATA_CMD_CACHE_FLUSH 0xE7 +#define ATA_CMD_CACHE_FLUSH_EXT 0xEA +#define ATA_CMD_PACKET 0xA0 +#define ATA_CMD_IDENTIFY_PACKET 0xA1 +#define ATA_CMD_IDENTIFY 0xEC + +#define ATA_IDENT_DEVICE_TYPE 0 +#define ATA_IDENT_CYLINDERS 2 +#define ATA_IDENT_HEADS 6 +#define ATA_IDENT_SECTORS 12 +#define ATA_IDENT_SERIAL 20 +#define ATA_IDENT_MODEL 54 +#define ATA_IDENT_CAPABILITIES 98 +#define ATA_IDENT_FIELDVALID 106 +#define ATA_IDENT_MAX_LBA 120 +#define ATA_IDENT_COMMANDSETS 164 +#define ATA_IDENT_MAX_LBA_EXT 200 + + +#define ATA_MASTER 0x00 +#define ATA_SLAVE 0x01 + +// Register +#define ATA_REG_DATA 0x00 +#define ATA_REG_ERROR 0x01 +#define ATA_REG_FEATURES 0x01 +#define ATA_REG_SEC_COUNT0 0x02 +#define ATA_REG_LBA0 0x03 +#define ATA_REG_LBA1 0x04 +#define ATA_REG_LBA2 0x05 +#define ATA_REG_HDDEVSEL 0x06 +#define ATA_REG_COMMAND 0x07 +#define ATA_REG_STATUS 0x07 +#define ATA_REG_SEC_COUNT1 0x08 +#define ATA_REG_LBA3 0x09 +#define ATA_REG_LBA4 0x0A +#define ATA_REG_LBA5 0x0B +#define ATA_REG_CONTROL 0x0C +#define ATA_REG_ALT_STATUS 0x0C +#define ATA_REG_DEV_ADDRESS 0x0D + +#define ATA_PRIMARY_IO 0x1F0 +#define ATA_SECONDARY_IO 0x170 +#define ATA_PRIMARY_DCR_AS 0x3F6 +#define ATA_SECONDARY_DCR_AS 0x376 + +// Irq +#define ATA_PRIMARY_IRQ 14 +#define ATA_SECONDARY_IRQ 15 + +// Channels +#define ATA_PRIMARY 0x00 +#define ATA_SECONDARY 0x01 + +// IO Direction +#define ATA_READ 0x00 +#define ATA_WRITE 0x013 + +#define ATA_PRIMARY_SEL 0xA0 +#define ATA_SECONDARY_SEL 0xB0 + +// ATA Helpers +#define ATA_ADDRESS1(x) (x + 3) +#define ATA_ADDRESS2(x) (x + 4) +#define ATA_ADDRESS3(x) (x + 5) +#define ATA_COMMAND(x) (x + 7) + +Boolean ATAInitDriver(UInt8 bus, UInt8 drive); +Void ATAWait(UInt16 IO); +UInt16 ATAReadLba(UInt32 lba, UInt8 bus, Boolean master); +Void ATAWriteLba(UInt16 byte, UInt32 lba, UInt8 bus, Boolean master); +Boolean ATAIsDetected(Void); \ No newline at end of file diff --git a/Private/NewBoot/Source/HEL/AMD64/BIOSAllocApi.inc b/Private/NewBoot/Source/HEL/AMD64/BIOSAllocApi.inc new file mode 100644 index 00000000..5b007434 --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/BIOSAllocApi.inc @@ -0,0 +1,40 @@ +;; COPYRIGHT Mahrouss Logic, all rights reserved + +bits 32 + +[global __BIOSLoadRegister] +[global __BIOSAlloc] + +__BIOSLoadRegister: + cmp [ebp - 4], 0 + jmp __LoadGDT + ret +__LoadGDT: + lgdt [ebp - 8] + ret + +;; This memory resides in the BIOS Memory Region BMR (0x9000-0x9000 + 4096) +;; It contains bootloader data. + +__BIOSAlloc: + push ebp + mov ebp, esp + mov eax, __bios_lookup_table +__BIOSAllocLoop: + cmp eax, __end_bios_lookup_table + jne __BIOSAllocDone + add eax, 4 + jmp $ +__BIOSAllocDone: + cmp eax, 0 + je __BIOSAllocLoop + add esp, 12 + mov esp, ebp + pop ebp + ret + +;; Allocation table. + +__bios_lookup_table: + resb 8096 * 4 +__end_bios_lookup_table: \ No newline at end of file diff --git a/Private/NewBoot/Source/HEL/AMD64/BIOSApiGdt.inc b/Private/NewBoot/Source/HEL/AMD64/BIOSApiGdt.inc new file mode 100644 index 00000000..be7a97e3 --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/BIOSApiGdt.inc @@ -0,0 +1,36 @@ +[BITS 32] + +NewBootGdtRegister: + dw NewBootGdtEnd - NewBootGdt - 1 + dq NewBootGdt + +[BITS 32] + +NewBootGdt: + ;; Entry 0x0: Null descriptor + dd 0x0 + dd 0x0 + ;; Entry 0x8: Code segment +NewBootGdtCode: + dw 0xffff ; Limit + dw 0x0000 ; Base 15:00 + db 0x00 ; Base 23:16 + dw 0xcf9a ; Flags / Limit / Type [F,L,F,Type] + db 0x00 ; Base 32:24 + ;; Entry 0x10: Data segment +NewBootGdtData: + dw 0xffff ; Limit + dw 0x0000 ; Base 15:00 + db 0x00 ; Base 23:16 + dw 0xcf92 ; Flags / Limit / Type [F,L,F,Type] + db 0x00 ;Base 32:24 + ;; Entry 0x18: GS Data segment + dw 0x0100 ; Limit + dw 0x1000 ; Base 15:00 + db 0x00 ; Base 23:16 + dw 0x4092 ; Flags / Limit / Type [F,L,F,Type] + db 0x00 ; Base 32:24 +NewBootGdtEnd: + +NB_CODE: db 0x8 +NB_DATA: db 0x10 \ No newline at end of file diff --git a/Private/NewBoot/Source/HEL/AMD64/BIOSRuntime.cxx b/Private/NewBoot/Source/HEL/AMD64/BIOSRuntime.cxx new file mode 100644 index 00000000..a9cb4fe4 --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/BIOSRuntime.cxx @@ -0,0 +1,8 @@ +/* +* Copyright 2024 Mahrouss Logic, all rights reserved +*/ + +#include +#include "ATA.hxx" + +extern "C" char __runtime_stack[4096] = { 0 }; diff --git a/Private/NewBoot/Source/HEL/AMD64/BIOSRuntime0.asm b/Private/NewBoot/Source/HEL/AMD64/BIOSRuntime0.asm new file mode 100644 index 00000000..f5ba44ec --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/BIOSRuntime0.asm @@ -0,0 +1,50 @@ +;; Copyright 2024 Mahrouss Logic, all rights reserved + +extern __runtime_stack +global NBRuntimeZero + +bits 16 + +NBRuntimeZero: + cli + + lgdt [NewBootGdtRegister] + + mov ebx, cr0 + or ebx, 1 + mov cr0, ebx + + jmp 0x8:NBProtectedMode + +%include "BIOSApiGdt.inc" + +[bits 32] + +NBMasterPartitionTable: + resb 32 + resw 1 + resw 1 + resw 1 + resb 211 +NBMasterPartitionTableEnd: + +NBProtectedMode: + mov ax, 0x10 ; 0x10 is a stand-in for your data segment + mov ds, ax + mov es, ax + mov fs, ax + mov gs, ax + mov ss, ax + + mov esp, __runtime_stack + jmp 0x8000000 + +NBLoopOne: + cli + hlt + jmp $ + +NBMasterBootRecord: + times 510 - ($-$$) db 0 + dw 0xAA55 +NBEndMasterBootRecord: \ No newline at end of file diff --git a/Private/NewBoot/Source/HEL/AMD64/BootAMD64.cxx b/Private/NewBoot/Source/HEL/AMD64/BootAMD64.cxx new file mode 100644 index 00000000..daaa62ec --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/BootAMD64.cxx @@ -0,0 +1,60 @@ +/* + * ======================================================== + * + * NewBoot + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#include + +#define kVGABaseAddress 0xb8000 + +long long int BStrLen(const char* ptr) +{ + long long int cnt = 0; + while (*ptr != 0) + { + ++ptr; + ++cnt; + } + + return cnt; +} + +void BKTextWriter::WriteString( + const char* str, + unsigned char forecolour, + unsigned char backcolour, + int x, + int y) +{ + if (*str == 0 || + !str) + return; + + for (SizeT idx = 0; idx < BStrLen(str); ++idx) + { + this->WriteCharacter(str[idx], forecolour, backcolour, x, y); + ++x; + } +} + +void BKTextWriter::WriteCharacter( + char c, + unsigned char forecolour, + unsigned char backcolour, + int x, + int y) +{ + UInt16 attrib = (backcolour << 4) | (forecolour & 0x0F); + + // Video Graphics Array + // Reads at kVGABaseAddress + // Decodes UInt16, gets attributes (back colour, fore colour) + // Gets character, send it to video display with according colour in the registry. + + fWhere = (volatile UInt16*)kVGABaseAddress + (y * 80 + x); + *fWhere = c | (attrib << 8); +} diff --git a/Private/NewBoot/Source/HEL/AMD64/EFIApi.hxx b/Private/NewBoot/Source/HEL/AMD64/EFIApi.hxx new file mode 100644 index 00000000..a10efc38 --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/EFIApi.hxx @@ -0,0 +1,25 @@ +/* + * ======================================================== + * + * NewBoot + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include +#include + +/// @brief auto-mount and boots from a bootable drive. +/// does not return on success. +void newboot_mount_drive(const char* name); +void newboot_boot_file(const char* path); + +/// @brief initializes xpm library. +void newboot_init_epm(void); + +/// @brief frees the xpm library, called when newboot_auto_mount/newboot_boot_file +/// succeeds. +void newboot_fini_xpm(void); diff --git a/Private/NewBoot/Source/HEL/AMD64/EFIApiCrt0.cxx b/Private/NewBoot/Source/HEL/AMD64/EFIApiCrt0.cxx new file mode 100644 index 00000000..23357681 --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/EFIApiCrt0.cxx @@ -0,0 +1,33 @@ +/* + * ======================================================== + * + * NewBoot + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#include "EFIApi.hxx" + +#define main efi_main + +typedef EFI_STATUS(*EfiMainType)(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable); + +EFI_STATUS main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) +{ + InitializeLib(ImageHandle, SystemTable); + Print(L"NewBoot: Booting from EPM...\r\n"); + + newboot_init_epm(); + + //! these two should execute a program if any on it. + newboot_mount_drive("epm:///system/"); + newboot_mount_drive("epm:///efi/"); + + newboot_fini_xpm(); + + Print(L"NewBoot: No auto-mount found.\r\n"); + + return EFI_LOAD_ERROR; +} + diff --git a/Private/NewBoot/Source/HEL/AMD64/Processor.cxx b/Private/NewBoot/Source/HEL/AMD64/Processor.cxx new file mode 100644 index 00000000..d2a0c15f --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/Processor.cxx @@ -0,0 +1,23 @@ +/* +* ======================================================== +* +* NewBoot +* Copyright 2024 Mahrouss Logic, all rights reserved. +* +* ======================================================== +*/ + +/* + * + * @file Processor.cxx + * @brief Processor Specific Functions. + * + */ + +extern "C" void rt_halt(void) { asm volatile("hlt"); } + +extern "C" void rt_cli(void) { asm volatile("cli"); } + +extern "C" void rt_sti(void) { asm volatile("sti"); } + +extern "C" void rt_cld(void) { asm volatile("cld"); } diff --git a/Private/NewBoot/Source/HEL/AMD64/makefile b/Private/NewBoot/Source/HEL/AMD64/makefile new file mode 100644 index 00000000..576bb12b --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/makefile @@ -0,0 +1,37 @@ +ASM = nasm + +# The kernel entrypoint +ENTRY = NBRuntimeZero + +# Where the text segment is. +TEXT = 0x7c00 + +# we want a flat binary +FMT = binary + +ASMFLAGS = -f elf64 + +KERNEL = hBoot.bin +ATAMOD = ATA.bin + +LD = x86_64-elf-ld +CC = x86_64-elf-gcc +CCFLAGS = -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__hCore__ -I./ -I../../../ -I$(HOME)/hCore/ + +.PHONY: build-crt0-bios +build-crt0-bios: + $(CC) $(CCFLAGS) BIOSRuntime.cxx + $(CC) $(CCFLAGS) ATA.cxx + $(CC) $(CCFLAGS) Processor.cxx + $(ASM) $(ASMFLAGS) BIOSRuntime0.asm + + $(LD) --oformat $(FMT) ATA.o -o $(ATAMOD) + $(LD) -e $(ENTRY) -Ttext $(TEXT) --oformat $(FMT) BIOSRuntime.o BIOSRuntime0.o -o $(KERNEL) + +.PHONY: all +all: build-crt0-bios + @echo "Done (CRT-0)" + +.PHONY: clean +clean: + rm -f $(wildcard *.o) $(wildcard *.bin) \ No newline at end of file diff --git a/Private/NewBoot/Source/HEL/PowerPC/.gitkeep b/Private/NewBoot/Source/HEL/PowerPC/.gitkeep new file mode 100644 index 00000000..e69de29b diff --git a/Private/NewBoot/Source/MPT/.hgkeep b/Private/NewBoot/Source/MPT/.hgkeep new file mode 100644 index 00000000..e69de29b diff --git a/Private/NewBoot/Source/MPT/API.cxx b/Private/NewBoot/Source/MPT/API.cxx new file mode 100644 index 00000000..8c649024 --- /dev/null +++ b/Private/NewBoot/Source/MPT/API.cxx @@ -0,0 +1,70 @@ +/* + * ======================================================== + * + * NewBoot + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#include "API.hxx" +#include "Detail.hxx" + +#define kFilesR 0x01 /* read-only */ +#define kFilesH 0x02 /* hidden */ +#define kFilesS 0x04 /* system */ +#define kFilesL 0x08 /* volume label */ +#define kFilesD 0x10 /* directory */ +#define kFilesZ 0x20 /* archive */ + +// @brief Array of unused bits. +#define kFilesU { 0x40, 0x80 } + +namespace mpt::detail +{ + struct Files32FileHdr final + { + char Filename[32]; + char Ext[3]; + char Attr; + char Case; + char CreateMs; + unsigned short Create; + unsigned short CreateDate; + unsigned short LastAccess; + unsigned short Timestamp; + unsigned short Datestamp; + unsigned short StartLba; + unsigned int SizeFile; + }; + + struct Files32FileGroup final + { + Files32FileHdr* fHdr{ nullptr }; + + Files32FileGroup* fUpper{ nullptr }; + Files32FileGroup* fLower{ nullptr }; + Files32FileGroup* fPrev{ nullptr }; + Files32FileGroup* fNext{ nullptr }; + }; + + /* @brief external inits */ + extern "C" int init_ata_mpt(void); + extern "C" int init_mpt(void); + + Files32FileGroup* kRootGroup = nullptr; +} + +namespace mpt +{ + bool init_mpt() noexcept + { + detail::kRootGroup = detail::new_class(); + + assert(detail::kRootGroup != nullptr); + assert(detail::init_ata_mpt() == detail::okay); + assert(detail::init_mpt() == detail::okay); + + return true; + } +} \ No newline at end of file diff --git a/Private/NewBoot/Source/MPT/API.hxx b/Private/NewBoot/Source/MPT/API.hxx new file mode 100644 index 00000000..2ba9bc74 --- /dev/null +++ b/Private/NewBoot/Source/MPT/API.hxx @@ -0,0 +1,17 @@ +/* + * ======================================================== + * + * NewBoot + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +namespace mpt +{ + /// initializes the Master Partition Table and the Files32 filesystem. + /// \return status, assert(fail) is also triggered, use filesystem_hook_error if you want to catch it. + bool init_mpt() noexcept; +} \ No newline at end of file diff --git a/Private/NewBoot/Source/MPT/Detail.hxx b/Private/NewBoot/Source/MPT/Detail.hxx new file mode 100644 index 00000000..31636b90 --- /dev/null +++ b/Private/NewBoot/Source/MPT/Detail.hxx @@ -0,0 +1,52 @@ +/* +* ======================================================== +* +* NewBoot +* Copyright 2024 Mahrouss Logic, all rights reserved. +* +* ======================================================== +*/ + +#pragma once + +namespace detail +{ + inline void assert_expr(bool expr, const char* str_expr) const + { + if (!expr) + { + detail::panic("assertion failed!", str_expr); + detail::hang(); + } + } + + inline void hang() const + { + while (1) + {} + } + + extern "C" void* new_ptr(long sz); + + template + inline Cls* new_class() + { + Cls* cls = (Cls*)new_ptr(sizeof(Cls)); + *cls = Cls(); + + return cls; + } + + enum + { + okay = 1, + failed = 0, + }; +} + +#ifdef assert +# undef assert +# define assert(expr) detail::assert_expr(expr, #expr) +#endif // ifdef assert + + diff --git a/Private/NewBoot/Source/MPT/FileType.hxx b/Private/NewBoot/Source/MPT/FileType.hxx new file mode 100644 index 00000000..77408b25 --- /dev/null +++ b/Private/NewBoot/Source/MPT/FileType.hxx @@ -0,0 +1,36 @@ +/* + * ======================================================== + * + * NewBoot + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +// @brief this file purpose is to read/write files. + +#include + +/// \brief File buffer class +/// \tparam _Manager The disk manager +template +class FileType +{ +public: + hCore::SizeT DiskId{ 0 }; // identification number of the drive. + hCore::VoidPtr DiskSpace{ nullptr }; // the pointer containing the requested disk data. + hCore::SizeT DiskSize{ 0 }; // it's size + hCore::Int32 DiskError{ 0 }; // if it's going well. + + FileType* Read(const char* path) { return _Manager::Read(path); } + FileType* Write(FileType* path) { return _Manager::Write(path); } + + // little sanity test + operator bool() + { + return DiskError != 0 && DiskSize > 0; + } + +}; \ No newline at end of file diff --git a/Private/NewBoot/Source/MPT/MPT.hxx b/Private/NewBoot/Source/MPT/MPT.hxx new file mode 100644 index 00000000..c1b9c6a1 --- /dev/null +++ b/Private/NewBoot/Source/MPT/MPT.hxx @@ -0,0 +1,29 @@ +/* + * ======================================================== + * + * NewBoot + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +// @brief 255 size partition header. +// we use that to gather information about this hard drive. + +struct MasterPartitionTable final +{ + char fPartName[32]; + int fPartType; + int fPartSectorSz; + int fPartSectorCnt; + char fReserved[211]; +}; + +enum +{ + kPartEfi = 0x10, + kPartEpm = 0x11, + kPartEbr = 0x12, +}; \ No newline at end of file diff --git a/Private/NewBoot/Source/Start.cxx b/Private/NewBoot/Source/Start.cxx new file mode 100644 index 00000000..223fcc0f --- /dev/null +++ b/Private/NewBoot/Source/Start.cxx @@ -0,0 +1,17 @@ +/* + * ======================================================== + * + * NewBoot + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#include + +extern "C" void Main(void) +{ + BKTextWriter writer; + writer.WriteString("Booting Kernel...", kBlack, kWhite, 0, 0); + +} diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile new file mode 100644 index 00000000..3273b52f --- /dev/null +++ b/Private/NewBoot/Source/makefile @@ -0,0 +1,19 @@ +CC=gcc +CCFLAGS=-I../ -I../../ -std=c++20 -ffreestanding -nostdlib -c + +.PHONY: arch-arc +arch-arc: + ${CC} ${CCFLAGS} Start.cxx + +CC_GNU=x86_64-elf-gcc +LD_GNU=x86_64-elf-ld +FLAG_GNU=-I../ -I../../../efiSDK/inc -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__hCore__ -I./ -I$(HOME)/ + +.PHONY: arch-amd64 +arch-amd64: + $(CC_GNU) $(FLAG_GNU) HEL/AMD64/BootAMD64.cxx *.cxx + $(LD_GNU) *.o -e Main -Ttext 0x000 --oformat binary -o BootloaderStage2.bin + +.PHONY: clean +clean: + rm -f *.o diff --git a/Private/Source/FilesystemIndexer.cxx b/Private/Source/FilesystemIndexer.cxx deleted file mode 100644 index bb1393b5..00000000 --- a/Private/Source/FilesystemIndexer.cxx +++ /dev/null @@ -1,28 +0,0 @@ -/* - * ======================================================== - * - * hCore - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -//! @brief hCore NewFS Indexer. - -#include -#include -#include -#include - -#define kMaxLenIndexer 256 - -namespace hCore -{ - namespace Indexer - { - IndexProperty& IndexableProperty::LeakProperty() noexcept { return fIndex; } - - void IndexableProperty::AddFlag(Int16 flag) { fFlags |= flag; } - void IndexableProperty::RemoveFlag(Int16 flag) { fFlags &= flag; } - } -} diff --git a/Private/Source/IndexableProperty.cxx b/Private/Source/IndexableProperty.cxx new file mode 100644 index 00000000..bb1393b5 --- /dev/null +++ b/Private/Source/IndexableProperty.cxx @@ -0,0 +1,28 @@ +/* + * ======================================================== + * + * hCore + * Copyright 2024 Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +//! @brief hCore NewFS Indexer. + +#include +#include +#include +#include + +#define kMaxLenIndexer 256 + +namespace hCore +{ + namespace Indexer + { + IndexProperty& IndexableProperty::LeakProperty() noexcept { return fIndex; } + + void IndexableProperty::AddFlag(Int16 flag) { fFlags |= flag; } + void IndexableProperty::RemoveFlag(Int16 flag) { fFlags &= flag; } + } +} diff --git a/Private/Source/Network/IP.cpp b/Private/Source/Network/IP.cpp index 4a0b3011..ba6fd71f 100644 --- a/Private/Source/Network/IP.cpp +++ b/Private/Source/Network/IP.cpp @@ -12,6 +12,11 @@ namespace hCore { + char* RawIPAddress::Address() + { + return m_Addr; + } + RawIPAddress::RawIPAddress(char bytes[4]) { rt_copy_memory(bytes, m_Addr, 4); @@ -86,19 +91,19 @@ namespace hCore return true; } - ErrorOr IPHelper::ToStringView(Ref ipv6) + ErrorOr IPFactory::ToStringView(Ref ipv6) { auto str = StringBuilder::Construct(ipv6.Leak().Address()); return str; } - ErrorOr IPHelper::ToStringView(Ref ipv4) + ErrorOr IPFactory::ToStringView(Ref ipv4) { auto str = StringBuilder::Construct(ipv4.Leak().Address()); return str; } - bool IPHelper::IpCheckV4(const char *ip) + bool IPFactory::IpCheckVersion4(const char *ip) { int cnter = 0; diff --git a/Private/Source/NewFS-Journal.cxx b/Private/Source/NewFS-Journal.cxx index bc3a4345..9c96928d 100644 --- a/Private/Source/NewFS-Journal.cxx +++ b/Private/Source/NewFS-Journal.cxx @@ -7,7 +7,6 @@ * ======================================================== */ - #include #include diff --git a/Private/Source/ThreadLocalStorage.cxx b/Private/Source/ThreadLocalStorage.cxx index 5cde1e77..5d231f61 100644 --- a/Private/Source/ThreadLocalStorage.cxx +++ b/Private/Source/ThreadLocalStorage.cxx @@ -21,11 +21,11 @@ Boolean hcore_tls_check(VoidPtr ptr) return _ptr[0] == kRTLMag0 && _ptr[1] == kRTLMag1 && _ptr[2] == kRTLMag2; } -Void hcore_tls_check_syscall_impl(VoidPtr ptr) noexcept +Void hcore_tls_check_syscall_impl(ThreadInformationBlock ptr) noexcept { - if (!hcore_tls_check(ptr)) + if (!hcore_tls_check(ptr.Cookie)) { - kcout << "TLS: TLS check failure, crashing...\n"; + kcout << "TLS: Verification failure, crashing...\n"; ProcessManager::Shared().Leak().GetCurrent().Leak().Crash(); } } diff --git a/Private/newBoot/BootKit/Boot.hpp b/Private/newBoot/BootKit/Boot.hpp deleted file mode 100644 index af86ce4c..00000000 --- a/Private/newBoot/BootKit/Boot.hpp +++ /dev/null @@ -1,77 +0,0 @@ -/* - * ======================================================== - * - * newBoot - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#pragma once - -#include - -using namespace hCore; - -typedef void* PEFImage; - -enum -{ - kSegmentCode = 2, - kSegmentData = 4, - kSegmentBss = 6, -}; - -/** - * @brief BootKit Text Writer class - * Writes to VGA. - */ -class BKTextWriter final -{ - volatile UInt16* fWhere{ nullptr }; - -public: - void WriteString(const char* c, - unsigned char forecolour, - unsigned char backcolour, - int x, - int y); - - void WriteCharacter(char c, - unsigned char forecolour, - unsigned char backcolour, - int x, - int y); - -public: - BKTextWriter() = default; - ~BKTextWriter() = default; - -public: - BKTextWriter& operator=(const BKTextWriter&) = default; - BKTextWriter(const BKTextWriter&) = default; - -}; - -enum -{ - kBlack, - kBlue, - kGreen, - kCyan, - kRed, - kMagenta, - kBrown, - kLightGray, - kDarkGray, - kLightBlue, - kLightGreen, - kLightCyan, - kLightRed, - kLightMagenta, - kYellow, - kWhite, -}; - -#define BK_START_KERNEL (0x00080000) - diff --git a/Private/newBoot/Source/.gitkeep b/Private/newBoot/Source/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/Private/newBoot/Source/Arch/AMD64/.gitkeep b/Private/newBoot/Source/Arch/AMD64/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/Private/newBoot/Source/Arch/AMD64/ATA.cxx b/Private/newBoot/Source/Arch/AMD64/ATA.cxx deleted file mode 100644 index 7bcd304a..00000000 --- a/Private/newBoot/Source/Arch/AMD64/ATA.cxx +++ /dev/null @@ -1,172 +0,0 @@ -/* - * Copyright 2024 Mahrouss Logic, all rights reserved - */ - -#include "ATA.hxx" -#include - -using namespace hCore::HAL; - -static Boolean kATADetected = false; - -void IDESelect(UInt8 Bus, Boolean isMaster) -{ - if (Bus == ATA_PRIMARY) - out8(ATA_PRIMARY_IO + ATA_REG_HDDEVSEL, - isMaster ? ATA_PRIMARY_SEL : ATA_SECONDARY_SEL); - else - out8(ATA_SECONDARY_IO + ATA_REG_HDDEVSEL, - isMaster ? ATA_PRIMARY_SEL : ATA_SECONDARY_SEL); -} - -Boolean ATAInitDriver(UInt8 Bus, UInt8 Drive) -{ - UInt16 IO = (Bus == ATA_PRIMARY) ? ATA_PRIMARY_IO : ATA_SECONDARY_IO; - - IDESelect(Bus, Drive); - - out8(IO + ATA_REG_SEC_COUNT0, 0); - out8(IO + ATA_REG_LBA0, 0); - out8(IO + ATA_REG_LBA1, 0); - out8(IO + ATA_REG_LBA2, 0); - - out8(IO + ATA_REG_COMMAND, ATA_CMD_IDENTIFY); - - UInt8 status = in8(IO + ATA_REG_STATUS); - - if (status) - { - while ((status = in8(IO + ATA_REG_STATUS) & ATA_SR_BSY)) - ; - - if (status & ATA_REG_ERROR) - { - return false; - } - - kATADetected = true; - return status; - } - - return false; -} - -void ATAWait(UInt16 IO) -{ - for (int i = 0; i < 4000; i++) - in8(IO + ATA_REG_ALT_STATUS); -} - -void IDEPoll(UInt16 IO) { ATAWait(IO); } - -UInt16 ATAReadLba(UInt32 lba, UInt8 bus, Boolean master) -{ - UInt16 IO = bus; - IDESelect(IO, master ? ATA_MASTER : ATA_SLAVE); - - out8(IO + ATA_REG_LBA5, (UInt8)(lba >> 24) & 0xF); - - out8(IO + ATA_REG_SEC_COUNT0, lba / 512); - - out8(IO + ATA_REG_LBA0, (UInt8)lba); - out8(IO + ATA_REG_LBA1, (UInt8)(lba >> 8)); - out8(IO + ATA_REG_LBA2, (UInt8)(lba >> 16)); - - out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); - - IDEPoll(IO); - - UInt16 data = in16(IO + ATA_REG_DATA); - - while ((in8(ATA_COMMAND(IO))) & 0x88) - ATAWait(IO); - - return data; -} - -Void ATAWriteLba(UInt16 Byte, UInt32 lba, UInt8 bus, Boolean master) -{ - UInt16 IO = bus; - IDESelect(IO, master ? ATA_MASTER : ATA_SLAVE); - - out8(IO + ATA_REG_LBA5, (UInt8)(lba >> 24) & 0xF); - - out8(IO + ATA_REG_SEC_COUNT0, lba / 512); - - out8(IO + ATA_REG_LBA0, (UInt8)lba); - out8(IO + ATA_REG_LBA1, (UInt8)(lba >> 8)); - out8(IO + ATA_REG_LBA2, (UInt8)(lba >> 16)); - - out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); // TODO: support DMA - - IDEPoll(IO); - - out32(IO + ATA_REG_DATA, Byte); - - while ((in8(ATA_COMMAND(IO))) & 0x88) - ATAWait(IO); -} - -Boolean ATAIsDetected(Void) { return kATADetected; } - -extern "C" void _start(void) -{ - ATAInitDriver(ATA_PRIMARY, true); - ATAInitDriver(ATA_PRIMARY, false); - - ATAInitDriver(ATA_SECONDARY, true); - ATAInitDriver(ATA_SECONDARY, false); -} - -extern "C" void out8(UInt16 port, UInt8 value) -{ - asm volatile("outb %%al, %1" : : "a"(value), "Nd"(port) : "memory"); -} - -extern "C" void out16(UInt16 port, UInt16 value) -{ - asm volatile("outw %%ax, %1" : : "a"(value), "Nd"(port) : "memory"); -} - -extern "C" void out32(UInt16 port, UInt32 value) -{ - asm volatile("outl %%eax, %1" : : "a"(value), "Nd"(port) : "memory"); -} - -extern "C" UInt8 in8(UInt16 port) -{ - UInt8 value = 0UL; - asm volatile("inb %1, %%al" : "=a"(value) : "Nd"(port) : "memory"); - - return value; -} - -extern "C" UInt16 in16(UInt16 port) -{ - UInt16 value = 0UL; - asm volatile("inw %1, %%ax" : "=a"(value) : "Nd"(port) : "memory"); - - return value; -} - -extern "C" UInt32 in32(UInt16 port) -{ - UInt32 value = 0UL; - asm volatile("inl %1, %%eax" : "=a"(value) : "Nd"(port) : "memory"); - - return value; -} - -extern "C" int init_ata_mpt(void) -{ - for (int i = 0; i < 255; ++i) - { - if (ATAInitDriver(i, ATA_MASTER)) - { - ATAInitDriver(i, ATA_SLAVE); - return 1; - } - } - - return 0; -} \ No newline at end of file diff --git a/Private/newBoot/Source/Arch/AMD64/ATA.hxx b/Private/newBoot/Source/Arch/AMD64/ATA.hxx deleted file mode 100644 index 09cc4007..00000000 --- a/Private/newBoot/Source/Arch/AMD64/ATA.hxx +++ /dev/null @@ -1,106 +0,0 @@ -#pragma once - -#include - -using namespace hCore; - -// Status register -#define ATA_SR_BSY 0x80 -#define ATA_SR_DRDY 0x40 -#define ATA_SR_DF 0x20 -#define ATA_SR_DSC 0x10 -#define ATA_SR_DRQ 0x08 -#define ATA_SR_CORR 0x04 -#define ATA_SR_IDX 0x02 -#define ATA_SR_ERR 0x01 - -// Error register -#define ATA_ER_BBK 0x80 -#define ATA_ER_UNC 0x40 -#define ATA_ER_MC 0x20 -#define ATA_ER_IDNF 0x10 -#define ATA_ER_MCR 0x08 -#define ATA_ER_ABRT 0x04 -#define ATA_ER_TK0NF 0x02 -#define ATA_ER_AMNF 0x01 - -#define ATA_CMD_READ_PIO 0x20 -#define ATA_CMD_READ_PIO_EXT 0x24 -#define ATA_CMD_READ_DMA 0xC8 -#define ATA_CMD_READ_DMA_EXT 0x25 -#define ATA_CMD_WRITE_PIO 0x30 -#define ATA_CMD_WRITE_PIO_EXT 0x34 -#define ATA_CMD_WRITE_DMA 0xCA -#define ATA_CMD_WRITE_DMA_EXT 0x35 -#define ATA_CMD_CACHE_FLUSH 0xE7 -#define ATA_CMD_CACHE_FLUSH_EXT 0xEA -#define ATA_CMD_PACKET 0xA0 -#define ATA_CMD_IDENTIFY_PACKET 0xA1 -#define ATA_CMD_IDENTIFY 0xEC - -#define ATA_IDENT_DEVICE_TYPE 0 -#define ATA_IDENT_CYLINDERS 2 -#define ATA_IDENT_HEADS 6 -#define ATA_IDENT_SECTORS 12 -#define ATA_IDENT_SERIAL 20 -#define ATA_IDENT_MODEL 54 -#define ATA_IDENT_CAPABILITIES 98 -#define ATA_IDENT_FIELDVALID 106 -#define ATA_IDENT_MAX_LBA 120 -#define ATA_IDENT_COMMANDSETS 164 -#define ATA_IDENT_MAX_LBA_EXT 200 - - -#define ATA_MASTER 0x00 -#define ATA_SLAVE 0x01 - -// Register -#define ATA_REG_DATA 0x00 -#define ATA_REG_ERROR 0x01 -#define ATA_REG_FEATURES 0x01 -#define ATA_REG_SEC_COUNT0 0x02 -#define ATA_REG_LBA0 0x03 -#define ATA_REG_LBA1 0x04 -#define ATA_REG_LBA2 0x05 -#define ATA_REG_HDDEVSEL 0x06 -#define ATA_REG_COMMAND 0x07 -#define ATA_REG_STATUS 0x07 -#define ATA_REG_SEC_COUNT1 0x08 -#define ATA_REG_LBA3 0x09 -#define ATA_REG_LBA4 0x0A -#define ATA_REG_LBA5 0x0B -#define ATA_REG_CONTROL 0x0C -#define ATA_REG_ALT_STATUS 0x0C -#define ATA_REG_DEV_ADDRESS 0x0D - -#define ATA_PRIMARY_IO 0x1F0 -#define ATA_SECONDARY_IO 0x170 -#define ATA_PRIMARY_DCR_AS 0x3F6 -#define ATA_SECONDARY_DCR_AS 0x376 - -// Irq -#define ATA_PRIMARY_IRQ 14 -#define ATA_SECONDARY_IRQ 15 - -// Channels -#define ATA_PRIMARY 0x00 -#define ATA_SECONDARY 0x01 - -// IO Direction -#define ATA_READ 0x00 -#define ATA_WRITE 0x013 - -#define ATA_PRIMARY_SEL 0xA0 -#define ATA_SECONDARY_SEL 0xB0 - -// ATA Helpers -#define ATA_ADDRESS1(x) (x + 3) -#define ATA_ADDRESS2(x) (x + 4) -#define ATA_ADDRESS3(x) (x + 5) -#define ATA_COMMAND(x) (x + 7) - -Boolean ATAInitDriver(UInt8 bus, UInt8 drive); -Void ATAWait(UInt16 IO); -UInt16 ATAReadLba(UInt32 lba, UInt8 bus, Boolean master); -Void ATAWriteLba(UInt16 byte, UInt32 lba, UInt8 bus, Boolean master); -Boolean ATAIsDetected(Void); \ No newline at end of file diff --git a/Private/newBoot/Source/Arch/AMD64/BIOSAllocApi.inc b/Private/newBoot/Source/Arch/AMD64/BIOSAllocApi.inc deleted file mode 100644 index 5b007434..00000000 --- a/Private/newBoot/Source/Arch/AMD64/BIOSAllocApi.inc +++ /dev/null @@ -1,40 +0,0 @@ -;; COPYRIGHT Mahrouss Logic, all rights reserved - -bits 32 - -[global __BIOSLoadRegister] -[global __BIOSAlloc] - -__BIOSLoadRegister: - cmp [ebp - 4], 0 - jmp __LoadGDT - ret -__LoadGDT: - lgdt [ebp - 8] - ret - -;; This memory resides in the BIOS Memory Region BMR (0x9000-0x9000 + 4096) -;; It contains bootloader data. - -__BIOSAlloc: - push ebp - mov ebp, esp - mov eax, __bios_lookup_table -__BIOSAllocLoop: - cmp eax, __end_bios_lookup_table - jne __BIOSAllocDone - add eax, 4 - jmp $ -__BIOSAllocDone: - cmp eax, 0 - je __BIOSAllocLoop - add esp, 12 - mov esp, ebp - pop ebp - ret - -;; Allocation table. - -__bios_lookup_table: - resb 8096 * 4 -__end_bios_lookup_table: \ No newline at end of file diff --git a/Private/newBoot/Source/Arch/AMD64/BIOSApiGdt.inc b/Private/newBoot/Source/Arch/AMD64/BIOSApiGdt.inc deleted file mode 100644 index be7a97e3..00000000 --- a/Private/newBoot/Source/Arch/AMD64/BIOSApiGdt.inc +++ /dev/null @@ -1,36 +0,0 @@ -[BITS 32] - -NewBootGdtRegister: - dw NewBootGdtEnd - NewBootGdt - 1 - dq NewBootGdt - -[BITS 32] - -NewBootGdt: - ;; Entry 0x0: Null descriptor - dd 0x0 - dd 0x0 - ;; Entry 0x8: Code segment -NewBootGdtCode: - dw 0xffff ; Limit - dw 0x0000 ; Base 15:00 - db 0x00 ; Base 23:16 - dw 0xcf9a ; Flags / Limit / Type [F,L,F,Type] - db 0x00 ; Base 32:24 - ;; Entry 0x10: Data segment -NewBootGdtData: - dw 0xffff ; Limit - dw 0x0000 ; Base 15:00 - db 0x00 ; Base 23:16 - dw 0xcf92 ; Flags / Limit / Type [F,L,F,Type] - db 0x00 ;Base 32:24 - ;; Entry 0x18: GS Data segment - dw 0x0100 ; Limit - dw 0x1000 ; Base 15:00 - db 0x00 ; Base 23:16 - dw 0x4092 ; Flags / Limit / Type [F,L,F,Type] - db 0x00 ; Base 32:24 -NewBootGdtEnd: - -NB_CODE: db 0x8 -NB_DATA: db 0x10 \ No newline at end of file diff --git a/Private/newBoot/Source/Arch/AMD64/BIOSRuntime.cxx b/Private/newBoot/Source/Arch/AMD64/BIOSRuntime.cxx deleted file mode 100644 index a9cb4fe4..00000000 --- a/Private/newBoot/Source/Arch/AMD64/BIOSRuntime.cxx +++ /dev/null @@ -1,8 +0,0 @@ -/* -* Copyright 2024 Mahrouss Logic, all rights reserved -*/ - -#include -#include "ATA.hxx" - -extern "C" char __runtime_stack[4096] = { 0 }; diff --git a/Private/newBoot/Source/Arch/AMD64/BIOSRuntime0.asm b/Private/newBoot/Source/Arch/AMD64/BIOSRuntime0.asm deleted file mode 100644 index f5ba44ec..00000000 --- a/Private/newBoot/Source/Arch/AMD64/BIOSRuntime0.asm +++ /dev/null @@ -1,50 +0,0 @@ -;; Copyright 2024 Mahrouss Logic, all rights reserved - -extern __runtime_stack -global NBRuntimeZero - -bits 16 - -NBRuntimeZero: - cli - - lgdt [NewBootGdtRegister] - - mov ebx, cr0 - or ebx, 1 - mov cr0, ebx - - jmp 0x8:NBProtectedMode - -%include "BIOSApiGdt.inc" - -[bits 32] - -NBMasterPartitionTable: - resb 32 - resw 1 - resw 1 - resw 1 - resb 211 -NBMasterPartitionTableEnd: - -NBProtectedMode: - mov ax, 0x10 ; 0x10 is a stand-in for your data segment - mov ds, ax - mov es, ax - mov fs, ax - mov gs, ax - mov ss, ax - - mov esp, __runtime_stack - jmp 0x8000000 - -NBLoopOne: - cli - hlt - jmp $ - -NBMasterBootRecord: - times 510 - ($-$$) db 0 - dw 0xAA55 -NBEndMasterBootRecord: \ No newline at end of file diff --git a/Private/newBoot/Source/Arch/AMD64/BootAMD64.cxx b/Private/newBoot/Source/Arch/AMD64/BootAMD64.cxx deleted file mode 100644 index 0ed59ff0..00000000 --- a/Private/newBoot/Source/Arch/AMD64/BootAMD64.cxx +++ /dev/null @@ -1,60 +0,0 @@ -/* - * ======================================================== - * - * newBoot - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#include - -#define kVGABaseAddress 0xb8000 - -long long int BStrLen(const char* ptr) -{ - long long int cnt = 0; - while (*ptr != 0) - { - ++ptr; - ++cnt; - } - - return cnt; -} - -void BKTextWriter::WriteString( - const char* str, - unsigned char forecolour, - unsigned char backcolour, - int x, - int y) -{ - if (*str == 0 || - !str) - return; - - for (SizeT idx = 0; idx < BStrLen(str); ++idx) - { - this->WriteCharacter(str[idx], forecolour, backcolour, x, y); - ++x; - } -} - -void BKTextWriter::WriteCharacter( - char c, - unsigned char forecolour, - unsigned char backcolour, - int x, - int y) -{ - UInt16 attrib = (backcolour << 4) | (forecolour & 0x0F); - - // Video Graphics Array - // Reads at kVGABaseAddress - // Decodes UInt16, gets attributes (back colour, fore colour) - // Gets character, send it to video display with according colour in the registry. - - fWhere = (volatile UInt16*)kVGABaseAddress + (y * 80 + x); - *fWhere = c | (attrib << 8); -} diff --git a/Private/newBoot/Source/Arch/AMD64/EFIApi.hxx b/Private/newBoot/Source/Arch/AMD64/EFIApi.hxx deleted file mode 100644 index 23e7b088..00000000 --- a/Private/newBoot/Source/Arch/AMD64/EFIApi.hxx +++ /dev/null @@ -1,25 +0,0 @@ -/* - * ======================================================== - * - * newBoot - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#pragma once - -#include -#include - -/// @brief auto-mount and boots from a bootable drive. -/// does not return on success. -void newboot_mount_drive(const char* name); -void newboot_boot_file(const char* path); - -/// @brief initializes xpm library. -void newboot_init_xpm(void); - -/// @brief frees the xpm library, called when newboot_auto_mount/newboot_boot_file -/// succeeds. -void newboot_fini_xpm(void); diff --git a/Private/newBoot/Source/Arch/AMD64/EFIApiCrt0.cxx b/Private/newBoot/Source/Arch/AMD64/EFIApiCrt0.cxx deleted file mode 100644 index dcfe84c2..00000000 --- a/Private/newBoot/Source/Arch/AMD64/EFIApiCrt0.cxx +++ /dev/null @@ -1,33 +0,0 @@ -/* - * ======================================================== - * - * newBoot - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#include "EFIApi.hxx" - -#define main efi_main - -typedef EFI_STATUS(*EfiMainType)(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable); - -EFI_STATUS main(EFI_HANDLE ImageHandle, EFI_SYSTEM_TABLE *SystemTable) -{ - InitializeLib(ImageHandle, SystemTable); - Print(L"newBoot: Booting from XPM...\r\n"); - - newboot_init_xpm(); - - //! these two should execute a program if any on it. - newboot_mount_drive("xpm:///system/"); - newboot_mount_drive("xpm:///efi/"); - - newboot_fini_xpm(); - - Print(L"newBoot: No auto-mount found.\r\n"); - - return EFI_LOAD_ERROR; -} - diff --git a/Private/newBoot/Source/Arch/AMD64/Processor.cxx b/Private/newBoot/Source/Arch/AMD64/Processor.cxx deleted file mode 100644 index 50330e47..00000000 --- a/Private/newBoot/Source/Arch/AMD64/Processor.cxx +++ /dev/null @@ -1,23 +0,0 @@ -/* -* ======================================================== -* -* newBoot -* Copyright 2024 Mahrouss Logic, all rights reserved. -* -* ======================================================== -*/ - -/* - * - * @file Processor.cxx - * @brief Processor Specific Functions. - * - */ - -extern "C" void rt_halt(void) { asm volatile("hlt"); } - -extern "C" void rt_cli(void) { asm volatile("cli"); } - -extern "C" void rt_sti(void) { asm volatile("sti"); } - -extern "C" void rt_cld(void) { asm volatile("cld"); } diff --git a/Private/newBoot/Source/Arch/AMD64/makefile b/Private/newBoot/Source/Arch/AMD64/makefile deleted file mode 100644 index 21860551..00000000 --- a/Private/newBoot/Source/Arch/AMD64/makefile +++ /dev/null @@ -1,33 +0,0 @@ -ASM = nasm - -# The kernel entrypoint -ENTRY = NBRuntimeZero - -# Where the text segment is. -TEXT = 0x7c00 - -# we want a flat binary -FMT = binary - -ASMFLAGS = -f elf64 - -KERNEL = hBoot.bin -ATAMOD = ATA.bin - -LD = x86_64-elf-ld -CC = x86_64-elf-gcc -CCFLAGS = -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__hCore__ -I./ -I../../../ -I$(HOME)/hCore/ - -.PHONY: build-crt0-bios -build-crt0-bios: - $(CC) $(CCFLAGS) BIOSRuntime.cxx - $(CC) $(CCFLAGS) ATA.cxx - $(CC) $(CCFLAGS) Processor.cxx - $(ASM) $(ASMFLAGS) BIOSRuntime0.asm - - $(LD) --oformat $(FMT) ATA.o -o $(ATAMOD) - $(LD) -e $(ENTRY) -Ttext $(TEXT) --oformat $(FMT) BIOSRuntime.o BIOSRuntime0.o -o $(KERNEL) - -.PHONY: all -all: build-crt0-bios - @echo "Done (CRT-0)" \ No newline at end of file diff --git a/Private/newBoot/Source/Arch/PowerPC/.gitkeep b/Private/newBoot/Source/Arch/PowerPC/.gitkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/Private/newBoot/Source/BootNotes.txt b/Private/newBoot/Source/BootNotes.txt deleted file mode 100644 index e09e9cce..00000000 --- a/Private/newBoot/Source/BootNotes.txt +++ /dev/null @@ -1,9 +0,0 @@ -bootloader roadmap: - -- Rom is being mapped. -- Move boot code to link address (8M) -- Decompress kernel using newboot_decompress_pef() -- Find start image. -- Run image at 8M page zero. - -- Amlal diff --git a/Private/newBoot/Source/MPT/.hgkeep b/Private/newBoot/Source/MPT/.hgkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/Private/newBoot/Source/MPT/API.cxx b/Private/newBoot/Source/MPT/API.cxx deleted file mode 100644 index e607eecd..00000000 --- a/Private/newBoot/Source/MPT/API.cxx +++ /dev/null @@ -1,70 +0,0 @@ -/* - * ======================================================== - * - * newBoot - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#include "API.hxx" -#include "Detail.hxx" - -#define kFilesR 0x01 /* read-only */ -#define kFilesH 0x02 /* hidden */ -#define kFilesS 0x04 /* system */ -#define kFilesL 0x08 /* volume label */ -#define kFilesD 0x10 /* directory */ -#define kFilesZ 0x20 /* archive */ - -// @brief Array of unused bits. -#define kFilesU { 0x40, 0x80 } - -namespace mpt::detail -{ - struct Files32FileHdr final - { - char Filename[32]; - char Ext[3]; - char Attr; - char Case; - char CreateMs; - unsigned short Create; - unsigned short CreateDate; - unsigned short LastAccess; - unsigned short Timestamp; - unsigned short Datestamp; - unsigned short StartLba; - unsigned int SizeFile; - }; - - struct Files32FileGroup final - { - Files32FileHdr* fHdr{ nullptr }; - - Files32FileGroup* fUpper{ nullptr }; - Files32FileGroup* fLower{ nullptr }; - Files32FileGroup* fPrev{ nullptr }; - Files32FileGroup* fNext{ nullptr }; - }; - - /* @brief external inits */ - extern "C" int init_ata_mpt(void); - extern "C" int init_mpt(void); - - Files32FileGroup* kRootGroup = nullptr; -} - -namespace mpt -{ - bool init_mpt() noexcept - { - detail::kRootGroup = detail::new_class(); - - assert(detail::kRootGroup != nullptr); - assert(detail::init_ata_mpt() == detail::okay); - assert(detail::init_mpt() == detail::okay); - - return true; - } -} \ No newline at end of file diff --git a/Private/newBoot/Source/MPT/API.hxx b/Private/newBoot/Source/MPT/API.hxx deleted file mode 100644 index 3ce689e7..00000000 --- a/Private/newBoot/Source/MPT/API.hxx +++ /dev/null @@ -1,17 +0,0 @@ -/* - * ======================================================== - * - * newBoot - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#pragma once - -namespace mpt -{ - /// initializes the Master Partition Table and the Files32 filesystem. - /// \return status, assert(fail) is also triggered, use filesystem_hook_error if you want to catch it. - bool init_mpt() noexcept; -} \ No newline at end of file diff --git a/Private/newBoot/Source/MPT/Detail.hxx b/Private/newBoot/Source/MPT/Detail.hxx deleted file mode 100644 index 12212b01..00000000 --- a/Private/newBoot/Source/MPT/Detail.hxx +++ /dev/null @@ -1,52 +0,0 @@ -/* -* ======================================================== -* -* newBoot -* Copyright 2024 Mahrouss Logic, all rights reserved. -* -* ======================================================== -*/ - -#pragma once - -namespace detail -{ - inline void assert_expr(bool expr, const char* str_expr) const - { - if (!expr) - { - detail::panic("assertion failed!", str_expr); - detail::hang(); - } - } - - inline void hang() const - { - while (1) - {} - } - - extern "C" void* new_ptr(long sz); - - template - inline Cls* new_class() - { - Cls* cls = (Cls*)new_ptr(sizeof(Cls)); - *cls = Cls(); - - return cls; - } - - enum - { - okay = 1, - failed = 0, - }; -} - -#ifdef assert -# undef assert -# define assert(expr) detail::assert_expr(expr, #expr) -#endif // ifdef assert - - diff --git a/Private/newBoot/Source/MPT/FileType.hxx b/Private/newBoot/Source/MPT/FileType.hxx deleted file mode 100644 index fe0ff322..00000000 --- a/Private/newBoot/Source/MPT/FileType.hxx +++ /dev/null @@ -1,36 +0,0 @@ -/* - * ======================================================== - * - * newBoot - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#pragma once - -// @brief this file purpose is to read/write files. - -#include - -/// \brief File buffer class -/// \tparam _Manager The disk manager -template -class FileType -{ -public: - hCore::SizeT DiskId{ 0 }; // identification number of the drive. - hCore::VoidPtr DiskSpace{ nullptr }; // the pointer containing the requested disk data. - hCore::SizeT DiskSize{ 0 }; // it's size - hCore::Int32 DiskError{ 0 }; // if it's going well. - - FileType* Read(const char* path) { return _Manager::Read(path); } - FileType* Write(FileType* path) { return _Manager::Write(path); } - - // little sanity test - operator bool() - { - return DiskError != 0 && DiskSize > 0; - } - -}; \ No newline at end of file diff --git a/Private/newBoot/Source/MPT/MPT.hxx b/Private/newBoot/Source/MPT/MPT.hxx deleted file mode 100644 index e8bbe7cd..00000000 --- a/Private/newBoot/Source/MPT/MPT.hxx +++ /dev/null @@ -1,29 +0,0 @@ -/* - * ======================================================== - * - * newBoot - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#pragma once - -// @brief 255 size partition header. -// we use that to gather information about this hard drive. - -struct MasterPartitionTable final -{ - char fPartName[32]; - int fPartType; - int fPartSectorSz; - int fPartSectorCnt; - char fReserved[211]; -}; - -enum -{ - kPartEfi = 0x10, - kPartEpm = 0x11, - kPartEbr = 0x12, -}; \ No newline at end of file diff --git a/Private/newBoot/Source/Start.cxx b/Private/newBoot/Source/Start.cxx deleted file mode 100644 index 7c8b5e04..00000000 --- a/Private/newBoot/Source/Start.cxx +++ /dev/null @@ -1,17 +0,0 @@ -/* - * ======================================================== - * - * newBoot - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#include - -extern "C" void Main(void) -{ - BKTextWriter writer; - writer.WriteString("Booting Kernel...", kBlack, kWhite, 0, 0); - -} diff --git a/Private/newBoot/Source/makefile b/Private/newBoot/Source/makefile deleted file mode 100644 index 835e4afa..00000000 --- a/Private/newBoot/Source/makefile +++ /dev/null @@ -1,19 +0,0 @@ -CC=gcc -CCFLAGS=-I../ -I../../ -std=c++20 -ffreestanding -nostdlib -c - -.PHONY: arch-arc -arch-arc: - ${CC} ${CCFLAGS} Start.cxx - -CC_GNU=x86_64-elf-gcc -LD_GNU=x86_64-elf-ld -FLAG_GNU=-I../ -I../../../efiSDK/inc -c -ffreestanding -fno-rtti -fno-exceptions -std=c++20 -D__HAVE_HCORE_APIS__ -D__hCore__ -I./ -I$(HOME)/ - -.PHONY: arch-amd64 -arch-amd64: - $(CC_GNU) $(FLAG_GNU) Arch/AMD64/BootAMD64.cxx *.cxx - $(LD_GNU) *.o -e Main -Ttext 0x000 --oformat binary -o BootloaderStage2.bin - -.PHONY: clean -clean: - rm -f *.o diff --git a/Private/newBoot/cxxKitModule/__cxxkit_unwind.cxx b/Private/newBoot/cxxKitModule/__cxxkit_unwind.cxx deleted file mode 100644 index 477b6302..00000000 --- a/Private/newBoot/cxxKitModule/__cxxkit_unwind.cxx +++ /dev/null @@ -1,11 +0,0 @@ -namespace cxxkit -{ - ///! @brief C++ ABI unwinding - ///! finis array (r1) - ///! n of finis (r2) - void __unwind(void(**finis)(void), int cnt) - { - for (int i = 0; i < cnt; ++i) - (finis[i])(); - } -} diff --git a/Private/newBoot/cxxKitModule/manifest.json b/Private/newBoot/cxxKitModule/manifest.json deleted file mode 100644 index 42b5d166..00000000 --- a/Private/newBoot/cxxKitModule/manifest.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "pluginName": "C++Kit" -} \ No newline at end of file diff --git a/Private/newBoot/netBootModule/.hgkeep b/Private/newBoot/netBootModule/.hgkeep deleted file mode 100644 index e69de29b..00000000 diff --git a/Private/newBoot/netBootModule/manifest.json b/Private/newBoot/netBootModule/manifest.json deleted file mode 100644 index 1a177877..00000000 --- a/Private/newBoot/netBootModule/manifest.json +++ /dev/null @@ -1,4 +0,0 @@ -{ - "pluginName": "internetBoot", - "description": "internetBoot your device from the network." -} \ No newline at end of file diff --git a/Private/newBoot/netBootModule/module.cxx b/Private/newBoot/netBootModule/module.cxx deleted file mode 100644 index 759e5cfe..00000000 --- a/Private/newBoot/netBootModule/module.cxx +++ /dev/null @@ -1,13 +0,0 @@ -/* - * ======================================================== - * - * NetBoot - * Copyright 2024 Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -extern "C" void __Module(const char* ip, long iplen) -{ - -} diff --git a/Public/SDK/DriverKit/HW.hxx b/Public/SDK/DriverKit/HW.hxx new file mode 100644 index 00000000..14dd3cf2 --- /dev/null +++ b/Public/SDK/DriverKit/HW.hxx @@ -0,0 +1,45 @@ +/* + * ======================================================== + * + * h-core + * Copyright Mahrouss Logic, all rights reserved. + * + * ======================================================== + */ + +#pragma once + +#include +#include + +namespace h-core +{ + /// @brief Provides a gentle way to initialize, dispose, send and receive hardware data. + class HardwareInterface + { + public: + explicit HardwareInterface() ={ this->Initialize(); } + virtual ~HardwareInterface() { this->Dispose(); } + + public: + HCORE_COPY_DEFAULT(HardwareInterface); + + public: + virtual const char* Name() { return "Generic HW."; } + + public: + virtual void Initialize() {} + virtual void Dispose() {} + + public: + virtual void Send(voidPtr msg, SizeT sz) { (void)msg; (void)sz; } + virtual void Recv(voidPtr msg, SizeT sz) { (void)msg; (void)sz; } + + protected: + virtual void OnSend(voidPtr msg, SizeT sz) { (void)msg; (void)sz; } + virtual void OnRecv(voidPtr msg, SizeT sz) { (void)msg; (void)sz; } + + }; +} + +// last rev 1/27/24 \ No newline at end of file diff --git a/Public/SDK/DriverKit/HWInterface.hpp b/Public/SDK/DriverKit/HWInterface.hpp deleted file mode 100644 index d1824c67..00000000 --- a/Public/SDK/DriverKit/HWInterface.hpp +++ /dev/null @@ -1,44 +0,0 @@ -/* - * ======================================================== - * - * h-core - * Copyright Mahrouss Logic, all rights reserved. - * - * ======================================================== - */ - -#pragma once - -#include -#include - -namespace h-core -{ - // h-core HardWare Interface. - // Purpose: Provides a gentle way to initialize, dispose, send and receive hardware data. - class HWInterface - { - public: - HWInterface() = default; - virtual ~HWInterface() = default; - - public: - HCORE_COPY_DEFAULT(HWInterface); - - public: - virtual const char* Name() { return "Unimplemented Hardware."; } - - public: - virtual void Initialize() {} - virtual void Dispose() {} - - public: - virtual void Send(voidPtr msg, SizeT sz) { (void)msg; (void)sz; } - virtual void Recv(voidPtr msg, SizeT sz) { (void)msg; (void)sz; } - - protected: - virtual void OnSend(voidPtr msg, SizeT sz) { (void)msg; (void)sz; } - virtual void OnRecv(voidPtr msg, SizeT sz) { (void)msg; (void)sz; } - - }; -} diff --git a/Public/SDK/DriverKit/TIB.hxx b/Public/SDK/DriverKit/TIB.hxx new file mode 100644 index 00000000..3a1c038b --- /dev/null +++ b/Public/SDK/DriverKit/TIB.hxx @@ -0,0 +1,41 @@ +/* +* ======================================================== +* +* h-core +* Copyright Mahrouss Logic, all rights reserved. +* +* ======================================================== +*/ + +// +// Created by Amlal on 1/27/24. +// + +#ifndef HCORE_TIB_HXX +#define HCORE_TIB_HXX + +#include + +namespace hCore +{ + /// @brief Thread Information Block for Local Storage. + /// Located in GS on AMD64, Virtual Address 0x10000 (64x0, 32x0, ARM64) + struct ThreadInformationBlock final + { + const Char TIB_NAME[255]; // Module Name + const UIntPtr TIB_START; // Start Address + const UIntPtr TIB_ALLOC; // Allocation Heap + const UIntPtr TIB_STACK; // Stack Pointer. + const Int32 TIB_ARCH; // Architecture and/or platform. + }; + + enum + { + kPC_IA64, + kPC_ARM, + kMACS_64x0, + kMACS_32x0, + }; +} + +#endif // HCORE_TIB_HXX -- cgit v1.2.3