From e50f871e6852beacb53986f930ed2d5dead84838 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 10 Apr 2025 09:41:33 +0200 Subject: kernel, boot: a lot of improvements regarding memory probing. Signed-off-by: Amlal El Mahrouss --- dev/boot/modules/BootNet/amd64.json | 2 +- dev/boot/modules/SysChk/SysChk.cc | 6 +-- dev/boot/modules/SysChk/amd64-ahci.json | 25 +++++++++++++ dev/boot/modules/SysChk/amd64-pio.json | 25 +++++++++++++ dev/boot/modules/SysChk/amd64.json | 24 ------------ dev/boot/src/HEL/AMD64/BootEFI.cc | 65 ++++++++++++--------------------- 6 files changed, 77 insertions(+), 70 deletions(-) create mode 100644 dev/boot/modules/SysChk/amd64-ahci.json create mode 100644 dev/boot/modules/SysChk/amd64-pio.json delete mode 100644 dev/boot/modules/SysChk/amd64.json (limited to 'dev/boot') diff --git a/dev/boot/modules/BootNet/amd64.json b/dev/boot/modules/BootNet/amd64.json index 86143e6a..da4a15f0 100644 --- a/dev/boot/modules/BootNet/amd64.json +++ b/dev/boot/modules/BootNet/amd64.json @@ -10,7 +10,7 @@ "-fPIC", "-fno-rtti", "-fno-exceptions", - "-Wl,--subsystem=17,--image-base,0x1000000,-e,BootNetModuleMain" + "-Wl,--subsystem=17,--image-base,0x10000000,-e,BootNetModuleMain" ], "cpp_macros": [ "__NEOSKRNL__", diff --git a/dev/boot/modules/SysChk/SysChk.cc b/dev/boot/modules/SysChk/SysChk.cc index ed14d2cd..3086cdc6 100644 --- a/dev/boot/modules/SysChk/SysChk.cc +++ b/dev/boot/modules/SysChk/SysChk.cc @@ -23,12 +23,12 @@ // Makes the compiler shut up. #ifndef kMachineModel -#define kMachineModel "NeKrnl" +#define kMachineModel "Ne" #endif // !kMachineModel EXTERN_C Int32 SysChkModuleMain(Kernel::HEL::BootInfoHeader* handover) { -#ifdef __NE_AMD64__ +#ifdef __ATA_PIO__ Boot::BDiskFormatFactory partition_factory; if (partition_factory.IsPartitionValid()) @@ -40,7 +40,7 @@ EXTERN_C Int32 SysChkModuleMain(Kernel::HEL::BootInfoHeader* handover) desc.fFileName[1] = 0; desc.fKind = kNeFSCatalogKindDir; - partition_factory.Format(kMachineModel, &desc, sizeof(Boot::BDiskFormatFactory::BFileDescriptor)); + partition_factory.Format(kMachineModel, &desc, 1); if (partition_factory.IsPartitionValid()) return kEfiOk; diff --git a/dev/boot/modules/SysChk/amd64-ahci.json b/dev/boot/modules/SysChk/amd64-ahci.json new file mode 100644 index 00000000..527e79f9 --- /dev/null +++ b/dev/boot/modules/SysChk/amd64-ahci.json @@ -0,0 +1,25 @@ +{ + "compiler_path": "x86_64-w64-mingw32-g++", + "compiler_std": "c++20", + "headers_path": ["../", "../../", "../../../kernel", "../../../", "./"], + "sources_path": ["*.cc", "*.S", "../../src/HEL/AMD64/*.cc", "../../src/HEL/AMD64/*.S", "../../src/*.cc"], + "output_name": "chk.efi", + "compiler_flags": [ + "-nostdlib", + "-std=c++20", + "-fPIC", + "-fno-rtti", + "-fno-exceptions", + "-Wl,--subsystem=17,--image-base,0x10000000,-e,SysChkModuleMain" + ], + "cpp_macros": [ + "__NEOSKRNL__", + "__BOOTZ__", + "__BOOTZ_STANDALONE__", + "__NE_AMD64__", + "__AHCI__", + "kChkVersionHighest=0x0100", + "kChkVersionLowest=0x0100", + "kChkVersion=0x0100" + ] +} diff --git a/dev/boot/modules/SysChk/amd64-pio.json b/dev/boot/modules/SysChk/amd64-pio.json new file mode 100644 index 00000000..de3195ce --- /dev/null +++ b/dev/boot/modules/SysChk/amd64-pio.json @@ -0,0 +1,25 @@ +{ + "compiler_path": "x86_64-w64-mingw32-g++", + "compiler_std": "c++20", + "headers_path": ["../", "../../", "../../../kernel", "../../../", "./"], + "sources_path": ["*.cc", "*.S", "../../src/HEL/AMD64/*.cc", "../../src/HEL/AMD64/*.S", "../../src/*.cc"], + "output_name": "chk.efi", + "compiler_flags": [ + "-nostdlib", + "-std=c++20", + "-fPIC", + "-fno-rtti", + "-fno-exceptions", + "-Wl,--subsystem=17,--image-base,0x10000000,-e,SysChkModuleMain" + ], + "cpp_macros": [ + "__NEOSKRNL__", + "__BOOTZ__", + "__BOOTZ_STANDALONE__", + "__NE_AMD64__", + "__ATA_PIO__", + "kChkVersionHighest=0x0100", + "kChkVersionLowest=0x0100", + "kChkVersion=0x0100" + ] +} diff --git a/dev/boot/modules/SysChk/amd64.json b/dev/boot/modules/SysChk/amd64.json deleted file mode 100644 index 3b5bab6f..00000000 --- a/dev/boot/modules/SysChk/amd64.json +++ /dev/null @@ -1,24 +0,0 @@ -{ - "compiler_path": "x86_64-w64-mingw32-g++", - "compiler_std": "c++20", - "headers_path": ["../", "../../", "../../../kernel", "../../../", "./"], - "sources_path": ["*.cc", "*.S", "../../src/HEL/AMD64/*.cc", "../../src/HEL/AMD64/*.S", "../../src/*.cc"], - "output_name": "chk.efi", - "compiler_flags": [ - "-nostdlib", - "-std=c++20", - "-fPIC", - "-fno-rtti", - "-fno-exceptions", - "-Wl,--subsystem=17,--image-base,0x1000000,-e,SysChkModuleMain" - ], - "cpp_macros": [ - "__NEOSKRNL__", - "__BOOTZ__", - "__BOOTZ_STANDALONE__", - "__NE_AMD64__", - "kChkVersionHighest=0x0100", - "kChkVersionLowest=0x0100", - "kChkVersion=0x0100" - ] -} diff --git a/dev/boot/src/HEL/AMD64/BootEFI.cc b/dev/boot/src/HEL/AMD64/BootEFI.cc index e07ad6d6..57651016 100644 --- a/dev/boot/src/HEL/AMD64/BootEFI.cc +++ b/dev/boot/src/HEL/AMD64/BootEFI.cc @@ -165,58 +165,39 @@ EFI_EXTERN_C EFI_API Int32 ModuleMain(EfiHandlePtr image_handle, // Fill handover header now. - // ---------------------------------------------------- // - // The following checks for an exisiting partition - // inside the disk, if it doesn't have one, - // format the disk. - // ---------------------------------------------------- // - Boot::BootTextWriter writer; - auto ret = BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); - - if (ret == kEfiFail) - { - writer.Write("BootZ: GetMemoryMap failed (x1)\r"); - Boot::Stop(); - } - - size_struct_ptr += sz_desc * 2; - BS->AllocatePool(EfiMemoryType::EfiBootServicesData, size_struct_ptr, reinterpret_cast(&struct_ptr)); + //-------------------------------------------------------------// + // Update handover file specific table and phyiscal start field. + //-------------------------------------------------------------// - ret = BS->GetMemoryMap(&size_struct_ptr, struct_ptr, &map_key, &sz_desc, &rev_desc); + handover_hdr->f_BitMapSize = gib_cast(4); /* Size of bitmap in bytes. */ + Int32 trials = 5 * 10000000; - if (ret == kEfiFail) + while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) { - writer.Write("BootZ: GetMemoryMap failed (x2)\r"); - Boot::Stop(); - } + --trials; - //-----------------------------------------------------------// - // A simple loop which finds a usable memory region for us. - //-----------------------------------------------------------// + if (!trials) + { + writer.Write("BootZ: Unable to allocate sufficent memory, trying again with 2GB...\r"); - SizeT lookup_index = 0UL; - SizeT entry_count = size_struct_ptr / sz_desc; + trials = 3 * 10000000; - for (; lookup_index < entry_count; ++lookup_index) - { - if (struct_ptr[lookup_index].Kind == EfiMemoryType::EfiConventionalMemory) - break; - } + handover_hdr->f_BitMapSize = gib_cast(2); /* Size of bitmap in bytes. */ - if (lookup_index > entry_count) - { - writer.Write("BootZ: No usable entries.\r"); - Boot::Stop(); - } + while (BS->AllocatePool(EfiLoaderData, handover_hdr->f_BitMapSize, &handover_hdr->f_BitMapStart) != kEfiOk) + { + --trials; - //-------------------------------------------------------------// - // Update handover file specific table and phyiscal start field. - //-------------------------------------------------------------// - - 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. */ + if (!trials) + { + writer.Write("BootZ: Unable to allocate sufficent memory, aborting...\r"); + Boot::Stop(); + } + } + } + } handover_hdr->f_FirmwareCustomTables[0] = (VoidPtr)BS; handover_hdr->f_FirmwareCustomTables[1] = (VoidPtr)ST; -- cgit v1.2.3