diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-04-19 08:36:30 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-04-19 08:36:30 +0200 |
| commit | 1740a0dff822d7666b8c1f056b6c411ef6b0f9fd (patch) | |
| tree | 646ae3f61ebcd3f83c888912c5f72efc0a8c25b0 /dev | |
| parent | 1dfc636e87c289d4091a9008a0beae0ff3d4e926 (diff) | |
kernel, mod, boot: extend GPT support.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'dev')
| -rw-r--r-- | dev/boot/BootKit/BootKit.h | 79 | ||||
| -rw-r--r-- | dev/boot/modules/SysChk/SysChk.cc | 2 | ||||
| -rw-r--r-- | dev/boot/modules/SysChk/amd64-ahci-epm.json (renamed from dev/boot/modules/SysChk/amd64-ahci.json) | 1 | ||||
| -rw-r--r-- | dev/boot/modules/SysChk/amd64-ahci-gpt.json | 25 | ||||
| -rw-r--r-- | dev/boot/modules/SysChk/amd64-pio-epm.json (renamed from dev/boot/modules/SysChk/amd64-pio.json) | 0 | ||||
| -rw-r--r-- | dev/boot/modules/SysChk/amd64-pio-gpt.json | 25 | ||||
| -rw-r--r-- | dev/kernel/FirmwareKit/EFI.h | 1 | ||||
| -rw-r--r-- | dev/kernel/FirmwareKit/GPT.h | 8 | ||||
| -rw-r--r-- | dev/kernel/src/DriveMgr.cc | 36 |
9 files changed, 159 insertions, 18 deletions
diff --git a/dev/boot/BootKit/BootKit.h b/dev/boot/BootKit/BootKit.h index d7f4f4d5..c781876c 100644 --- a/dev/boot/BootKit/BootKit.h +++ b/dev/boot/BootKit/BootKit.h @@ -298,7 +298,7 @@ namespace Boot fDiskDev.Write((Char*)&catalogKind, sizeof(NEFS_CATALOG_STRUCT)); - writer.Write(L"BootZ: Wrote directory: ").Write(blob->fFileName).Write(L"\r"); + writer.Write(L"BootZ: wrote root directory: ").Write(blob->fFileName).Write(L"\r"); return true; } @@ -349,10 +349,12 @@ namespace Boot part.DiskSize = fDiskDev.GetDiskSize(); part.Flags = kNeFSPartitionTypeBoot | kNeFSPartitionTypeStandard; +#if defined(BOOTZ_EPM_SUPPORT) fDiskDev.Leak().mBase = kNeFSRootCatalogStartAddress; fDiskDev.Leak().mSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); fDiskDev.Write((Char*)&part, sizeof(NEFS_ROOT_PARTITION_BLOCK)); +#endif // defined(BOOTZ_EPM_SUPPORT) BootTextWriter writer; @@ -363,14 +365,12 @@ namespace Boot writer << "BootZ: Free sectors: " << part.FreeSectors << "\r"; writer << "BootZ: Sector size: " << part.SectorSize << "\r"; -#ifdef BOOTZ_EPM_SUPPORT +#if defined(BOOTZ_EPM_SUPPORT) EPM_PART_BLOCK epm_boot{0}; const auto kFsName = "NeFS"; const auto kBlockName = "OS"; - CopyMem(epm_boot.Fs, reinterpret_cast<VoidPtr>(const_cast<Char*>(kFsName)), StrLen(kFsName)); - epm_boot.FsVersion = kNeFSVersionInteger; epm_boot.LbaStart = kNeFSRootCatalogStartAddress; epm_boot.LbaEnd = fDiskDev.GetDiskSize(); @@ -378,6 +378,13 @@ namespace Boot epm_boot.Kind = kEPMNeKernel; epm_boot.NumBlocks = part.CatalogCount; + epm_boot.Guid.Data1 = 0x00000000; + epm_boot.Guid.Data2 = 0x0000; + epm_boot.Guid.Data3 = 0x0000; + + SetMem(epm_boot.Guid.Data4, 0, 8); + + CopyMem(epm_boot.Fs, reinterpret_cast<VoidPtr>(const_cast<Char*>(kFsName)), StrLen(kFsName)); CopyMem(epm_boot.Name, reinterpret_cast<VoidPtr>(const_cast<Char*>(kBlockName)), StrLen(kBlockName)); CopyMem(epm_boot.Magic, reinterpret_cast<VoidPtr>(const_cast<Char*>(kEPMMagic)), StrLen(kEPMMagic)); @@ -386,7 +393,69 @@ namespace Boot fDiskDev.Write((Char*)&epm_boot, sizeof(EPM_PART_BLOCK)); - writer.Write(L"BootZ: Drive has been formatted Successfully.\r"); + writer.Write(L"BootZ: Drive is EPM formatted.\r"); +#elif defined(BOOTZ_GPT_SUPPORT) + const auto kBlockName = "NeFS_OS"; + + GPT_PARTITION_TABLE gpt_part{0}; + + CopyMem(gpt_part.Signature, reinterpret_cast<VoidPtr>(const_cast<Char*>(kMagicGPT)), StrLen(kMagicGPT)); + + gpt_part.Revision = 0x00010000; + gpt_part.HeaderSize = sizeof(GPT_PARTITION_TABLE); + + gpt_part.CRC32 = 0x00000000; + + gpt_part.Reserved1 = 0x00000000; + gpt_part.LBAHeader = 0x00000000; + gpt_part.LBAAltHeader = 0x00000000; + gpt_part.FirstGPTEntry = 0x00000000; + gpt_part.LastGPTEntry = 0x00000000; + gpt_part.Guid.Data1 = 0x00000000; + gpt_part.Guid.Data2 = 0x0000; + gpt_part.Guid.Data3 = 0x0000; + + gpt_part.Revision = 0x00010000; + + SetMem(gpt_part.Guid.Data4, 0, 8); + + gpt_part.StartingLBA = 0x00000000; + gpt_part.NumPartitionEntries = 0x00000000; + gpt_part.SizeOfEntries = 0x00000000; + gpt_part.CRC32PartEntry = 0x00000000; + + SetMem(gpt_part.Reserved2, 0, kSectorAlignGPT_PartTbl); + + fDiskDev.Leak().mBase = kGPTPartitionTableLBA; // always always resies at zero block. + fDiskDev.Leak().mSize = sizeof(EPM_PART_BLOCK); + + fDiskDev.Write((Char*)&gpt_part, sizeof(GPT_PARTITION_TABLE)); + + GPT_PARTITION_ENTRY gpt_part_entry{0}; + + gpt_part_entry.StartLBA = kNeFSRootCatalogStartAddress; + gpt_part_entry.EndLBA = fDiskDev.GetDiskSize(); + gpt_part_entry.Attributes = 0x00000000; + + gpt_part_entry.PartitionTypeGUID.Data1 = 0x00000000; + gpt_part_entry.PartitionTypeGUID.Data2 = 0x0000; + gpt_part_entry.PartitionTypeGUID.Data3 = 0x0000; + + CopyMem(gpt_part_entry.Name, reinterpret_cast<VoidPtr>(const_cast<Char*>(kBlockName)), StrLen(kBlockName)); + + SetMem(gpt_part_entry.PartitionTypeGUID.Data4, 0, 8); + + fDiskDev.Leak().mBase = kGPTPartitionTableLBA + sizeof(GPT_PARTITION_TABLE); + fDiskDev.Leak().mSize = sizeof(GPT_PARTITION_ENTRY); + fDiskDev.Write((Char*)&gpt_part_entry, sizeof(GPT_PARTITION_ENTRY)); + + fDiskDev.Leak().mBase = gpt_part_entry.StartLBA; + fDiskDev.Leak().mSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); + + fDiskDev.Write((Char*)&part, sizeof(NEFS_ROOT_PARTITION_BLOCK)); + + writer.Write(L"BootZ: Drive is GPT formatted.\r"); + #endif return YES; diff --git a/dev/boot/modules/SysChk/SysChk.cc b/dev/boot/modules/SysChk/SysChk.cc index dec4e11a..3979714d 100644 --- a/dev/boot/modules/SysChk/SysChk.cc +++ b/dev/boot/modules/SysChk/SysChk.cc @@ -30,7 +30,7 @@ EXTERN_C Int32 SysChkModuleMain(Kernel::HEL::BootInfoHeader* handover) { NE_UNUSED(handover); -#ifdef __ATA_PIO__ +#if defined(__ATA_PIO__) Boot::BDiskFormatFactory<BootDeviceATA> partition_factory; if (partition_factory.IsPartitionValid()) diff --git a/dev/boot/modules/SysChk/amd64-ahci.json b/dev/boot/modules/SysChk/amd64-ahci-epm.json index d13e6c4a..5cff74de 100644 --- a/dev/boot/modules/SysChk/amd64-ahci.json +++ b/dev/boot/modules/SysChk/amd64-ahci-epm.json @@ -17,6 +17,7 @@ "__BOOTZ_STANDALONE__", "__NE_AMD64__", "__AHCI__", + "BOOTZ_EPM_SUPPORT", "kChkVersionHighest=0x0100", "kChkVersionLowest=0x0100", "kChkVersion=0x0100" diff --git a/dev/boot/modules/SysChk/amd64-ahci-gpt.json b/dev/boot/modules/SysChk/amd64-ahci-gpt.json new file mode 100644 index 00000000..52864969 --- /dev/null +++ b/dev/boot/modules/SysChk/amd64-ahci-gpt.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", + "-fno-rtti", + "-fno-exceptions", + "-Wl,--subsystem=17,--image-base,0x10000000,-e,SysChkModuleMain" + ], + "cpp_macros": [ + "__NEOSKRNL__", + "__BOOTZ__", + "__BOOTZ_STANDALONE__", + "__NE_AMD64__", + "__AHCI__", + "BOOTZ_GPT_SUPPORT", + "kChkVersionHighest=0x0100", + "kChkVersionLowest=0x0100", + "kChkVersion=0x0100" + ] +} diff --git a/dev/boot/modules/SysChk/amd64-pio.json b/dev/boot/modules/SysChk/amd64-pio-epm.json index 3848a611..3848a611 100644 --- a/dev/boot/modules/SysChk/amd64-pio.json +++ b/dev/boot/modules/SysChk/amd64-pio-epm.json diff --git a/dev/boot/modules/SysChk/amd64-pio-gpt.json b/dev/boot/modules/SysChk/amd64-pio-gpt.json new file mode 100644 index 00000000..2a4c4efe --- /dev/null +++ b/dev/boot/modules/SysChk/amd64-pio-gpt.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", + "-fno-rtti", + "-fno-exceptions", + "-Wl,--subsystem=17,--image-base,0x10000000,-e,SysChkModuleMain" + ], + "cpp_macros": [ + "__NEOSKRNL__", + "__BOOTZ__", + "__BOOTZ_STANDALONE__", + "__NE_AMD64__", + "__ATA_PIO__", + "BOOTZ_GPT_SUPPORT", + "kChkVersionHighest=0x0100", + "kChkVersionLowest=0x0100", + "kChkVersion=0x0100" + ] +} diff --git a/dev/kernel/FirmwareKit/EFI.h b/dev/kernel/FirmwareKit/EFI.h index e4e78720..ca4360b1 100644 --- a/dev/kernel/FirmwareKit/EFI.h +++ b/dev/kernel/FirmwareKit/EFI.h @@ -7,5 +7,6 @@ #pragma once #include <FirmwareKit/EFI/EFI.h> +#include <FirmwareKit/GPT.h> /// @note this header is used to reference the EFI/EFI.h diff --git a/dev/kernel/FirmwareKit/GPT.h b/dev/kernel/FirmwareKit/GPT.h index 7ee5d10e..dc2b5ce3 100644 --- a/dev/kernel/FirmwareKit/GPT.h +++ b/dev/kernel/FirmwareKit/GPT.h @@ -11,7 +11,9 @@ #define kSectorAlignGPT_PartTbl (420U) #define kSectorAlignGPT_PartEntry (72U) -#define kPartNameGPT (8U) +#define kMagicLenGPT (8U) +#define kMagicGPT ("EFI PART") // "EFI PART" +#define kGPTPartitionTableLBA (512U + sizeof(GPT_PARTITION_TABLE)) namespace Kernel { @@ -30,10 +32,10 @@ namespace Kernel struct PACKED GPT_PARTITION_TABLE final { - Char PartitionName[kPartNameGPT]; + Char Signature[kMagicLenGPT]; UInt32 Revision; UInt32 HeaderSize; - UInt32 ChecksumCRC32; + UInt32 CRC32; UInt32 Reserved1; UInt64 LBAHeader; UInt64 LBAAltHeader; diff --git a/dev/kernel/src/DriveMgr.cc b/dev/kernel/src/DriveMgr.cc index a379ea43..f958a33f 100644 --- a/dev/kernel/src/DriveMgr.cc +++ b/dev/kernel/src/DriveMgr.cc @@ -8,6 +8,7 @@ #include <KernelKit/DriveMgr.h> #include <NewKit/Utils.h> #include <FirmwareKit/EPM.h> +#include <FirmwareKit/GPT.h> #include <modules/ATA/ATA.h> #include <modules/AHCI/AHCI.h> #include <modules/NVME/NVME.h> @@ -170,12 +171,12 @@ namespace Kernel trait.fInput(trait.fPacket); - if (rt_string_cmp(((EPM_PART_BLOCK*)trait.fPacket.fPacketContent)->Magic, kEPMMagic, kEPMMagicLength) == 0) + if (rt_string_cmp(block_struct.Magic, kEPMMagic, kEPMMagicLength) == 0) { trait.fPacket.fPacketReadOnly = NO; trait.fKind = kMassStorageDrive | kEPMDrive; - kout << "Disk is EPM.\r"; + kout << "Disk is EPM formatted.\r"; trait.fSectorSz = block_struct.SectorSz; trait.fLbaEnd = block_struct.LbaEnd; @@ -183,17 +184,34 @@ namespace Kernel } else { - trait.fPacket.fPacketReadOnly = YES; - trait.fKind = kMassStorageDrive | kUnformattedDrive | kReadOnlyDrive; + GPT_PARTITION_TABLE gpt_struct; - if (block_struct.Name[0] == 0 || - !rt_is_alnum(block_struct.Name[0])) + trait.fPacket.fPacketLba = kEPMBootBlockLba; + trait.fPacket.fPacketSize = sizeof(GPT_PARTITION_TABLE); + trait.fPacket.fPacketContent = &gpt_struct; + + rt_copy_memory((VoidPtr) "fs/detect-packet", trait.fPacket.fPacketMime, + rt_string_len("fs/detect-packet")); + + trait.fInput(trait.fPacket); + + if (rt_string_cmp(gpt_struct.Signature, kMagicGPT, kMagicLenGPT) == 0) { - kout << "Disk partition is empty (Read Only)\r"; + trait.fPacket.fPacketReadOnly = NO; + trait.fKind = kMassStorageDrive | kGPTDrive; + + kout << "Disk is GPT formatted.\r"; + + trait.fSectorSz = gpt_struct.SizeOfEntries; + trait.fLbaEnd = gpt_struct.LastGPTEntry; + trait.fLbaStart = gpt_struct.FirstGPTEntry; } else { - (void)(kout << "Scheme Found: " << block_struct.Name << kendl); + kout << "Disk is unformatted.\r"; + + trait.fPacket.fPacketReadOnly = YES; + trait.fKind = kMassStorageDrive | kUnformattedDrive | kReadOnlyDrive; } } @@ -212,7 +230,7 @@ namespace Kernel { DriveTrait trait; - constexpr auto kMainDrive = "/media/sda/"; + constexpr auto kMainDrive = "/media/main/"; rt_copy_memory((VoidPtr)kMainDrive, trait.fName, rt_string_len(kMainDrive)); |
