diff options
Diffstat (limited to 'Private/NewBoot/Source')
23 files changed, 839 insertions, 0 deletions
diff --git a/Private/NewBoot/Source/.gitkeep b/Private/NewBoot/Source/.gitkeep new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/Private/NewBoot/Source/.gitkeep 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 --- /dev/null +++ b/Private/NewBoot/Source/HEL/AMD64/.gitkeep 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 <ArchKit/Arch.hpp> + +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 <NewKit/Defines.hpp> + +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 <BootKit/Boot.hpp> +#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 <BootKit/Boot.hpp> + +#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 <efi.h> +#include <efilib.h> + +/// @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 --- /dev/null +++ b/Private/NewBoot/Source/HEL/PowerPC/.gitkeep diff --git a/Private/NewBoot/Source/MPT/.hgkeep b/Private/NewBoot/Source/MPT/.hgkeep new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/Private/NewBoot/Source/MPT/.hgkeep 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<detail::Files32FileGroup>(); + + 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 <typename Cls> + 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 <NewKit/Defines.hpp> + +/// \brief File buffer class +/// \tparam _Manager The disk manager +template <typename _Manager> +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 <BootKit/Boot.hpp> + +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 |
