diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-03-31 22:14:55 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-03-31 22:14:55 +0200 |
| commit | 6849e75f2e95e88b43e2f8804abf1b862e3981cb (patch) | |
| tree | 2f517be98baf5b782c8f3718dc0beb4dc460d3ce | |
| parent | 33a94b84237afa61d2f15fa158664137724d60a4 (diff) | |
| parent | fd288fed29eff48503abf842676085701e04c38d (diff) | |
Merge pull request #3 from amlel-el-mahrouss/dev
pr/general: a set of patches to be pushed into master.
| -rw-r--r-- | dev/boot/BootKit/BootKit.h | 4 | ||||
| -rw-r--r-- | dev/boot/amd64-ci.make | 2 | ||||
| -rw-r--r-- | dev/boot/amd64-desktop.make | 16 | ||||
| -rw-r--r-- | dev/boot/arm64-desktop.make | 14 | ||||
| -rw-r--r-- | dev/boot/modules/BootNet/BootNet.cc | 4 | ||||
| -rw-r--r-- | dev/boot/modules/BootNet/BootNet.h | 10 | ||||
| -rw-r--r-- | dev/boot/modules/BootNet/amd64.json | 6 | ||||
| -rw-r--r-- | dev/boot/src/BootTextWriter.cc | 4 | ||||
| -rw-r--r-- | dev/boot/src/HEL/AMD64/BootEFI.cc | 43 | ||||
| -rw-r--r-- | dev/kernel/FirmwareKit/CoreBoot/BootNet.h | 30 | ||||
| -rw-r--r-- | dev/kernel/FirmwareKit/CoreBoot/NetBoot.h | 25 | ||||
| -rw-r--r-- | dev/kernel/FirmwareKit/EFI/EFI.h | 14 | ||||
| -rw-r--r-- | dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc | 31 | ||||
| -rw-r--r-- | dev/kernel/KernelKit/DebugOutput.h | 2 |
14 files changed, 113 insertions, 92 deletions
diff --git a/dev/boot/BootKit/BootKit.h b/dev/boot/BootKit/BootKit.h index e845e6ce..b5062eab 100644 --- a/dev/boot/BootKit/BootKit.h +++ b/dev/boot/BootKit/BootKit.h @@ -61,10 +61,10 @@ namespace Boot */ class BootTextWriter final { - BootTextWriter& _Write(const Long& num); + BootTextWriter& _Write(const UInt64& num); public: - BootTextWriter& Write(const Long& num); + BootTextWriter& Write(const UInt64& num); BootTextWriter& Write(const Char* str); BootTextWriter& Write(const CharacterTypeUTF16* str); BootTextWriter& WriteCharacter(CharacterTypeUTF16 c); diff --git a/dev/boot/amd64-ci.make b/dev/boot/amd64-ci.make index a77806c4..21c797f6 100644 --- a/dev/boot/amd64-ci.make +++ b/dev/boot/amd64-ci.make @@ -85,7 +85,7 @@ invalid-recipe: # CI doesn't do anything than build. .PHONY: all all: compile-amd64 - mkdir -p src/Root/EFI/BOOT + mkdir -p src/root/EFI/BOOT $(LD_GNU) $(OBJ) $(LD_FLAGS) -o src/$(BOOTLOADER) .PHONY: disk diff --git a/dev/boot/amd64-desktop.make b/dev/boot/amd64-desktop.make index 649e0925..95ccac2e 100644 --- a/dev/boot/amd64-desktop.make +++ b/dev/boot/amd64-desktop.make @@ -84,15 +84,15 @@ invalid-recipe: .PHONY: all all: compile-amd64 - mkdir -p src/Root/EFI/BOOT + mkdir -p src/root/EFI/BOOT $(LD_GNU) $(OBJ) $(LD_FLAGS) -o src/$(BOOTLOADER) - $(COPY) src/$(BOOTLOADER) src/Root/EFI/BOOT/BOOTX64.EFI - $(COPY) src/$(BOOTLOADER) src/Root/EFI/BOOT/BOOTZ.EFI - $(COPY) ../kernel/$(KERNEL) src/Root/$(KERNEL) - $(COPY) ./modules/SysChk/$(SYSCHK) src/Root/$(SYSCHK) - $(COPY) ./modules/BootNet/$(BOOTNET) src/Root/$(BOOTNET) - $(COPY) ../user/$(SCIKIT) src/Root/$(SCIKIT) - $(COPY) src/$(BOOTLOADER) src/Root/$(BOOTLOADER) + $(COPY) src/$(BOOTLOADER) src/root/EFI/BOOT/BOOTX64.EFI + $(COPY) src/$(BOOTLOADER) src/root/EFI/BOOT/BOOTZ.EFI + $(COPY) ../kernel/$(KERNEL) src/root/$(KERNEL) + $(COPY) ./modules/SysChk/$(SYSCHK) src/root/$(SYSCHK) + $(COPY) ./modules/BootNet/$(BOOTNET) src/root/$(BOOTNET) + $(COPY) ../user/$(SCIKIT) src/root/$(SCIKIT) + $(COPY) src/$(BOOTLOADER) src/root/$(BOOTLOADER) .PHONY: disk disk: diff --git a/dev/boot/arm64-desktop.make b/dev/boot/arm64-desktop.make index d17c2ae2..a862b5b8 100644 --- a/dev/boot/arm64-desktop.make +++ b/dev/boot/arm64-desktop.make @@ -31,7 +31,7 @@ EMU_FLAGS=-net none -smp 4 -m 8G -cpu max -M virt \ -bios $(BIOS) \ -drive id=disk,file=$(IMG),format=raw,if=none \ -drive \ - file=fat:rw:src/Root/,index=2,format=raw \ + file=fat:rw:src/root/,index=2,format=raw \ -no-shutdown -no-reboot -cpu cortex-a72 -device virtio-gpu-pci LD_FLAGS=-subsystem:efi_application -entry:Main /nodefaultlib @@ -58,13 +58,13 @@ invalid-recipe: .PHONY: all all: compile - mkdir -p src/Root/EFI/BOOT + mkdir -p src/root/EFI/BOOT $(LD_GNU) $(OBJ) $(LD_FLAGS) /out:src/$(BOOT_LOADER) - $(COPY) src/$(BOOT_LOADER) src/Root/EFI/BOOT/BOOTAA64.EFI - $(COPY) src/$(BOOT_LOADER) src/Root/EFI/BOOT/BootZ.EFI - $(COPY) ../kernel/$(KERNEL) src/Root/$(KERNEL) - $(COPY) ./modules/SysChk/$(SYSCHK) src/Root/$(SYSCHK) - $(COPY) src/$(BOOT_LOADER) src/Root/$(BOOT_LOADER) + $(COPY) src/$(BOOT_LOADER) src/root/EFI/BOOT/BOOTAA64.EFI + $(COPY) src/$(BOOT_LOADER) src/root/EFI/BOOT/BootZ.EFI + $(COPY) ../kernel/$(KERNEL) src/root/$(KERNEL) + $(COPY) ./modules/SysChk/$(SYSCHK) src/root/$(SYSCHK) + $(COPY) src/$(BOOT_LOADER) src/root/$(BOOT_LOADER) ifneq ($(DEBUG_SUPPORT), ) DEBUG = -D__DEBUG__ diff --git a/dev/boot/modules/BootNet/BootNet.cc b/dev/boot/modules/BootNet/BootNet.cc index e6c3d3e4..1129ef30 100644 --- a/dev/boot/modules/BootNet/BootNet.cc +++ b/dev/boot/modules/BootNet/BootNet.cc @@ -22,7 +22,7 @@ EXTERN_C Int32 ModuleMain(Kernel::HEL::BootInfoHeader* handover) if (inet.Length < 1) { Boot::BootTextWriter writer; - writer.Write("NetBootLauncher: No executable attached to the packet, aborting.\r"); + writer.Write("BootNetLauncher: No executable attached to the packet, aborting.\r"); return kEfiFail; } @@ -39,7 +39,7 @@ EXTERN_C Int32 ModuleMain(Kernel::HEL::BootInfoHeader* handover) else { Boot::BootTextWriter writer; - writer.Write("NetBootLauncher: EEPROM flash is not available as of right now.\r"); + writer.Write("BootNetLauncher: EEPROM flash is not available as of right now.\r"); /// TODO: Program new firmware to EEPROM (if crc and size matches) diff --git a/dev/boot/modules/BootNet/BootNet.h b/dev/boot/modules/BootNet/BootNet.h index 992cc7c3..2bf330df 100644 --- a/dev/boot/modules/BootNet/BootNet.h +++ b/dev/boot/modules/BootNet/BootNet.h @@ -11,10 +11,10 @@ #include <NewKit/Defines.h> -#define kNetBootINetMagic "NETB" -#define kNetBootINetMagicLength (4) +#define kBootNetINetMagic "NETB" +#define kBootNetINetMagicLength (4) -#define kNetBootNameLen (256U) +#define kBootNetNameLen (256U) /// @brief the internet header is used to download updates OTA. typedef struct BOOTNET_INTERNET_HEADER @@ -24,9 +24,9 @@ typedef struct BOOTNET_INTERNET_HEADER Kernel::Char NB3; /// magic char 3 'T' Kernel::Char NB4; /// magic char 4 'B' - Kernel::Char Name[kNetBootNameLen]; /// example: Modjo + Kernel::Char Name[kBootNetNameLen]; /// example: Modjo Kernel::Int32 Length; /// the patch length. - Kernel::Char Target[kNetBootNameLen]; /// the target file. + Kernel::Char Target[kBootNetNameLen]; /// the target file. Kernel::Boolean ImpliesEEPROM : 1; /// does it imply an EEPROM reprogram? Kernel::Boolean Preflight : 1; /// is it a preflight packet. Kernel::Char Data[]; /// non preflight packet has a patch blob for a **PatchTarget** diff --git a/dev/boot/modules/BootNet/amd64.json b/dev/boot/modules/BootNet/amd64.json index 664c617f..112cf9f1 100644 --- a/dev/boot/modules/BootNet/amd64.json +++ b/dev/boot/modules/BootNet/amd64.json @@ -17,8 +17,8 @@ "__BOOTZ__", "__BOOTZ_STANDALONE__", "__NE_AMD64__", - "kNetBootVersionHighest=0x0100", - "kNetBootVersionLowest=0x0100", - "kNetBootVersion=0x0100" + "kBootNetVersionHighest=0x0100", + "kBootNetVersionLowest=0x0100", + "kBootNetVersion=0x0100" ] } diff --git a/dev/boot/src/BootTextWriter.cc b/dev/boot/src/BootTextWriter.cc index bc93b133..0b132f83 100644 --- a/dev/boot/src/BootTextWriter.cc +++ b/dev/boot/src/BootTextWriter.cc @@ -131,7 +131,7 @@ Boot::BootTextWriter& Boot::BootTextWriter::WriteCharacter(CharacterTypeUTF16 c) return *this; } -Boot::BootTextWriter& Boot::BootTextWriter::Write(const Long& x) +Boot::BootTextWriter& Boot::BootTextWriter::Write(const UInt64& x) { #ifdef __DEBUG__ this->_Write(x); @@ -141,7 +141,7 @@ Boot::BootTextWriter& Boot::BootTextWriter::Write(const Long& x) return *this; } -Boot::BootTextWriter& Boot::BootTextWriter::_Write(const Long& x) +Boot::BootTextWriter& Boot::BootTextWriter::_Write(const UInt64& x) { #ifdef __DEBUG__ UInt64 y = (x > 0 ? x : -x) / 16; diff --git a/dev/boot/src/HEL/AMD64/BootEFI.cc b/dev/boot/src/HEL/AMD64/BootEFI.cc index 7b912025..5f89aae6 100644 --- a/dev/boot/src/HEL/AMD64/BootEFI.cc +++ b/dev/boot/src/HEL/AMD64/BootEFI.cc @@ -91,9 +91,9 @@ EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr image_handle, new HEL::BootInfoHeader(); UInt32 map_key = 0; - UInt32 size_struct_ptr = sizeof(EfiMemoryDescriptor); + UInt32 size_struct_ptr = 0; EfiMemoryDescriptor* struct_ptr = nullptr; - UInt32 sz_desc = sizeof(EfiMemoryDescriptor); + UInt32 sz_desc = 0; UInt32 rev_desc = 0; #ifdef ZBA_USE_FB @@ -176,33 +176,52 @@ EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr image_handle, // format the disk. // ---------------------------------------------------- // - BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); + Boot::BootTextWriter writer; + + auto ret = BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); - struct_ptr = new EfiMemoryDescriptor[sz_desc]; + if (ret == kEfiFail) + { + writer.Write("BootZ: GetMemoryMap failed (x1)\r"); + Boot::Stop(); + } - BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); + size_struct_ptr += sz_desc * 2; + BS->AllocatePool(EfiMemoryType::EfiBootServicesData, size_struct_ptr, reinterpret_cast<VoidPtr*>(&struct_ptr)); - auto kDefaultMemoryMap = 0; // Grab any usable entries. + ret = BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); + + if (ret == kEfiFail) + { + writer.Write("BootZ: GetMemoryMap failed (x2)\r"); + Boot::Stop(); + } //-----------------------------------------------------------// // A simple loop which finds a usable memory region for us. //-----------------------------------------------------------// SizeT lookup_index = 0UL; + SizeT entry_count = size_struct_ptr / sz_desc; - for (; struct_ptr[lookup_index].Kind != EfiMemoryType::EfiConventionalMemory; ++lookup_index) + for (; lookup_index < entry_count; ++lookup_index) { - NE_UNUSED(0); + if (struct_ptr[lookup_index].Kind == EfiMemoryType::EfiConventionalMemory) + break; } - kDefaultMemoryMap = lookup_index; + if (lookup_index > entry_count) + { + writer.Write("BootZ: No usable entries.\r"); + Boot::Stop(); + } //-------------------------------------------------------------// // Update handover file specific table and phyiscal start field. //-------------------------------------------------------------// - handover_hdr->f_BitMapStart = (VoidPtr)struct_ptr[kDefaultMemoryMap].VirtualStart; /* Start of bitmap. */ - handover_hdr->f_BitMapSize = struct_ptr[kDefaultMemoryMap].NumberOfPages * sizeof(UIntPtr); /* Size of bitmap. */ + handover_hdr->f_BitMapStart = (VoidPtr)struct_ptr[lookup_index].VirtualStart; /* Start of bitmap. */ + handover_hdr->f_BitMapSize = struct_ptr[lookup_index].NumberOfPages * kib_cast(4); /* Size of bitmap in bytes. */ handover_hdr->f_FirmwareCustomTables[0] = (VoidPtr)BS; handover_hdr->f_FirmwareCustomTables[1] = (VoidPtr)ST; @@ -287,8 +306,6 @@ EFI_EXTERN_C EFI_API Int32 Main(EfiHandlePtr image_handle, ST->RuntimeServices->GetVariable(L"/props/kern_ver", kEfiGlobalNamespaceVarGUID, nullptr, &sz_ver, &ver); - Boot::BootTextWriter writer; - if (ver != KERNEL_VERSION_BCD) { ver = KERNEL_VERSION_BCD; diff --git a/dev/kernel/FirmwareKit/CoreBoot/BootNet.h b/dev/kernel/FirmwareKit/CoreBoot/BootNet.h new file mode 100644 index 00000000..a86ea81a --- /dev/null +++ b/dev/kernel/FirmwareKit/CoreBoot/BootNet.h @@ -0,0 +1,30 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.h> + +#define kBootNetINetMagic "NETB" +#define kBootNetINetMagicLength (4) + +#define kBootNetNameLen (256U) + +/// @brief the internet header is used to download updates OTA. +typedef struct BOOTNET_INTERNET_HEADER +{ + Kernel::Char NB1; /// magic char 1 'N' + Kernel::Char NB2; /// magic char 2 'E' + Kernel::Char NB3; /// magic char 3 'T' + Kernel::Char NB4; /// magic char 4 'B' + + Kernel::Char Name[kBootNetNameLen]; /// example: Modjo + Kernel::Int32 Length; /// the patch length. + Kernel::Char Target[kBootNetNameLen]; /// the target file. + Kernel::Boolean ImpliesEEPROM : 1; /// does it imply an EEPROM reprogram? + Kernel::Boolean Preflight : 1; /// is it a preflight packet. + Kernel::Char Data[]; /// non preflight packet has a patch blob for a **PatchTarget** +} ATTRIBUTE(packed) BOOTNET_INTERNET_HEADER; diff --git a/dev/kernel/FirmwareKit/CoreBoot/NetBoot.h b/dev/kernel/FirmwareKit/CoreBoot/NetBoot.h deleted file mode 100644 index b1e9313c..00000000 --- a/dev/kernel/FirmwareKit/CoreBoot/NetBoot.h +++ /dev/null @@ -1,25 +0,0 @@ -/* ------------------------------------------- - - Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. - -------------------------------------------- */ - -#pragma once - -#include <NewKit/Defines.h> - -/// @brief the internet header is used to download updates OTA. -typedef struct BOOTNET_INTERNET_HEADER -{ - Kernel::Char NB1; /// magic char 1 'N' - Kernel::Char NB2; /// magic char 2 'E' - Kernel::Char NB3; /// magic char 3 'T' - Kernel::Char NB4; /// magic char 4 'B' - - Kernel::Char PatchName[256U]; /// example: ColdChoco - Kernel::Int32 PatchLength; /// the patch length. - Kernel::Char PatchTarget[256U]; /// the target file. - Kernel::Boolean ImpliesROM; /// does it imply an EEPROM reprogram? - Kernel::Boolean Preflight; /// is it a preflight packet. - Kernel::Char Patch[]; /// non preflight packet has a patch blob for a **PatchTarget** -} BOOTNET_INTERNET_HEADER; diff --git a/dev/kernel/FirmwareKit/EFI/EFI.h b/dev/kernel/FirmwareKit/EFI/EFI.h index 3c3ffa09..1e6de42c 100644 --- a/dev/kernel/FirmwareKit/EFI/EFI.h +++ b/dev/kernel/FirmwareKit/EFI/EFI.h @@ -201,18 +201,10 @@ typedef enum EfiAllocateType typedef struct EfiMemoryDescriptor { - /// - /// Kind of the memory region. - /// Kind EFI_MEMORY_TYPE is defined in the - /// AllocatePages() function description. - /// + /// @brief Kind of the memory region. UInt32 Kind; - /// - /// Physical address of the first byte in the memory region. PhysicalStart - /// must be aligned on a 4 KiB boundary, and must not be above - /// 0xfffffffffffff000. Kind EFI_PHYSICAL_ADDRESS is defined in the - /// AllocatePages() function description - /// + + /// @brief Physical address of the first byte in the memory region. PhysicalStart EfiPhysicalAddress PhysicalStart; /// /// Virtual address of the first byte in the memory region. diff --git a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc index 38a1742b..7dac018d 100644 --- a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc @@ -74,8 +74,10 @@ STATIC Void drv_compute_disk_ahci() noexcept const UInt16 kSzIdent = kib_cast(1); /// Push it to the stack - UInt8 identify_data[kSzIdent] = {0}; + static UInt8 identify_data[kSzIdent] ATTRIBUTE(aligned(4096)) = {0}; + HAL::mm_map_page((void*)mib_cast(1), (void*)mib_cast(1), HAL::kMMFlagsWr); + rt_set_memory(identify_data, 0, kSzIdent); /// Send AHCI command for identification. @@ -140,9 +142,11 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz MUST_PASS(command_header); + constexpr UInt32 kMaxPRDSize = 0x400000; + command_header->Cfl = sizeof(FisRegH2D) / sizeof(UInt32); command_header->Write = Write; - command_header->Prdtl = (UInt16)((size_buffer - 1) / 8); + command_header->Prdtl = (UInt16)((size_buffer + kMaxPRDSize - 1) / kMaxPRDSize); volatile HbaCmdTbl* command_table = (volatile HbaCmdTbl*)((VoidPtr)((UInt64)command_header->Ctba)); @@ -150,18 +154,21 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz MUST_PASS(command_table); - UIntPtr buffer_phys = HAL::hal_get_phys_address(buffer); - - UInt16 prd_i = 0; + UIntPtr buffer_phys = HAL::hal_get_phys_address(buffer); + SizeT bytes_remaining = size_buffer; + SizeT prdt_count = command_header->Prdtl; - for (; prd_i < (command_header->Prdtl - 1); ++prd_i) + for (UInt16 i = 0; i < prdt_count; ++i) { - command_table->Prdt[prd_i].Dbc = ((command_header->Prdtl - 1) / 8); - command_table->Prdt[prd_i].Dba = ((UInt32)(UInt64)buffer_phys); - command_table->Prdt[prd_i].Dbau = (((UInt64)(buffer_phys) >> 32)); - command_table->Prdt[prd_i].Ie = YES; + UInt32 chunk = (bytes_remaining > kMaxPRDSize) ? kMaxPRDSize : bytes_remaining; + + command_table->Prdt[i].Dba = (UInt32)(buffer_phys & 0xFFFFFFFF); + command_table->Prdt[i].Dbau = (UInt32)(buffer_phys >> 32); + command_table->Prdt[i].Dbc = chunk - 1; + command_table->Prdt[i].Ie = 1; - buffer_phys += command_table->Prdt[prd_i].Dbc; + buffer_phys += chunk; + bytes_remaining -= chunk; } volatile FisRegH2D* h2d_fis = (volatile FisRegH2D*)(&command_table->Cfis); @@ -176,7 +183,7 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz h2d_fis->Lba1 = (lba >> 8) & 0xFF; h2d_fis->Lba2 = (lba >> 16) & 0xFF; - h2d_fis->Device = Identify ? 0U : kSATALBAMode; + h2d_fis->Device = kSATALBAMode; h2d_fis->Lba3 = (lba >> 24) & 0xFF; h2d_fis->Lba4 = (lba >> 32) & 0xFF; diff --git a/dev/kernel/KernelKit/DebugOutput.h b/dev/kernel/KernelKit/DebugOutput.h index cb22ac6b..448e6208 100644 --- a/dev/kernel/KernelKit/DebugOutput.h +++ b/dev/kernel/KernelKit/DebugOutput.h @@ -208,6 +208,6 @@ namespace Kernel #undef kout #endif // ifdef kout -#define kout Kernel::TerminalDevice::The() << "[NeKernel] *" << __FILE__ << "*: " +#define kout Kernel::TerminalDevice::The() #define kendl Kernel::TerminalDevice::The() << Kernel::end_line() |
