From da0e83c01161d0c216f3831d5be21df728691c64 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 11 May 2025 16:48:02 +0200 Subject: feat(kernel/hefs): Fixed the shortcomings and issues of HeFS. Signed-off-by: Amlal El Mahrouss --- .../HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc | 3 +- dev/kernel/HALKit/AMD64/HalDebugOutput.cc | 46 +++++++++++++++++----- dev/kernel/HALKit/AMD64/HalKernelMain.cc | 3 +- dev/kernel/HALKit/AMD64/Paging.h | 4 +- dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc | 6 ++- dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc | 10 +++-- dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc | 4 +- dev/kernel/HALKit/ARM64/HalApplicationProcessor.cc | 3 +- dev/kernel/HALKit/ARM64/HalKernelMain.cc | 3 +- dev/kernel/HALKit/ARM64/HalPagingMgrARM64.cc | 3 +- dev/kernel/HALKit/ARM64/Paging.h | 4 +- dev/kernel/HALKit/POWER/HalApplicationProcessor.cc | 3 +- dev/kernel/HALKit/RISCV/HalApplicationProcessor.cc | 3 +- 13 files changed, 65 insertions(+), 30 deletions(-) (limited to 'dev/kernel/HALKit') diff --git a/dev/kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc b/dev/kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc index 641fb8e4..84c35615 100644 --- a/dev/kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc +++ b/dev/kernel/HALKit/AMD64/HalCoreInterruptHandlerAMD64.cc @@ -128,7 +128,8 @@ EXTERN_C Kernel::Void idt_handle_breakpoint(Kernel::UIntPtr rip) { if (process.Leak().Status != Kernel::ProcessStatusKind::kRunning) { Kernel::kout << "Kernel: SIGTRAP\r"; - while (YES); + while (YES) + ; } kIsScheduling = NO; diff --git a/dev/kernel/HALKit/AMD64/HalDebugOutput.cc b/dev/kernel/HALKit/AMD64/HalDebugOutput.cc index bd8fe734..5d14f214 100644 --- a/dev/kernel/HALKit/AMD64/HalDebugOutput.cc +++ b/dev/kernel/HALKit/AMD64/HalDebugOutput.cc @@ -57,6 +57,8 @@ namespace Detail { TerminalDevice::~TerminalDevice() = default; +STATIC SizeT kX = kFontSizeX, kY = kFontSizeY; + EXTERN_C void ke_utf_io_write(IDeviceObject* obj, const Utf8Char* bytes) { NE_UNUSED(bytes); NE_UNUSED(obj); @@ -76,11 +78,36 @@ EXTERN_C void ke_utf_io_write(IDeviceObject* obj, const Utf8Cha index = 0; len = urt_string_len(bytes); + Char tmp_str[2]; + while (index < len) { if (bytes[index] == '\r') HAL::rt_out8(Detail::kPort, '\r'); HAL::rt_out8(Detail::kPort, bytes[index] == '\r' ? '\n' : bytes[index]); + tmp_str[0] = (bytes[index] > 127) ? '?' : bytes[index]; + tmp_str[1] = 0; + + fb_render_string(tmp_str, kY, kX, RGB(0xff, 0xff, 0xff)); + + if (bytes[index] == '\r') { + kY += kFontSizeY; + kX = kFontSizeX; + } + + kX += kFontSizeX; + + if (kX > kHandoverHeader->f_GOP.f_Width) { + kX = kFontSizeX; + } + + if (kY > kHandoverHeader->f_GOP.f_Height) { + kY = kFontSizeY; + + FBDrawInRegion(fb_get_clear_clr(), FB::FBAccessibilty::Height(), FB::FBAccessibilty::Width(), + 0, 0); + } + ++index; } @@ -107,32 +134,31 @@ EXTERN_C void ke_io_write(IDeviceObject* obj, const Char* bytes) { index = 0; len = rt_string_len(bytes); - STATIC SizeT x = kFontSizeX, y = kFontSizeY; + Char tmp_str[2]; while (index < len) { if (bytes[index] == '\r') HAL::rt_out8(Detail::kPort, '\r'); HAL::rt_out8(Detail::kPort, bytes[index] == '\r' ? '\n' : bytes[index]); - char tmp_str[2]; tmp_str[0] = bytes[index]; tmp_str[1] = 0; - fb_render_string(tmp_str, y, x, RGB(0xff, 0xff, 0xff)); + fb_render_string(tmp_str, kY, kX, RGB(0xff, 0xff, 0xff)); if (bytes[index] == '\r') { - y += kFontSizeY; - x = kFontSizeX; + kY += kFontSizeY; + kX = kFontSizeX; } - x += kFontSizeX; + kX += kFontSizeX; - if (x > kHandoverHeader->f_GOP.f_Width) { - x = kFontSizeX; + if (kX > kHandoverHeader->f_GOP.f_Width) { + kX = kFontSizeX; } - if (y > kHandoverHeader->f_GOP.f_Height) { - y = kFontSizeY; + if (kY > kHandoverHeader->f_GOP.f_Height) { + kY = kFontSizeY; FBDrawInRegion(fb_get_clear_clr(), FB::FBAccessibilty::Height(), FB::FBAccessibilty::Width(), 0, 0); diff --git a/dev/kernel/HALKit/AMD64/HalKernelMain.cc b/dev/kernel/HALKit/AMD64/HalKernelMain.cc index f41979bb..2747439a 100644 --- a/dev/kernel/HALKit/AMD64/HalKernelMain.cc +++ b/dev/kernel/HALKit/AMD64/HalKernelMain.cc @@ -146,7 +146,8 @@ EXTERN_C Kernel::Void hal_real_init(Kernel::Void) noexcept { kTeams[team_index].Id() = team_index; - while (!UserProcessScheduler::The().SwitchTeam(kTeams[team_index])); + while (!UserProcessScheduler::The().SwitchTeam(kTeams[team_index])) + ; timer.Wait(); diff --git a/dev/kernel/HALKit/AMD64/Paging.h b/dev/kernel/HALKit/AMD64/Paging.h index 3c7107bc..cfba232c 100644 --- a/dev/kernel/HALKit/AMD64/Paging.h +++ b/dev/kernel/HALKit/AMD64/Paging.h @@ -55,9 +55,7 @@ namespace Detail { PageEnable = 31, }; - inline UInt8 control_register_cast(ControlRegisterBits reg) { - return static_cast(reg); - } + inline UInt8 control_register_cast(ControlRegisterBits reg) { return static_cast(reg); } } // namespace Detail auto mm_alloc_bitmap(Boolean wr, Boolean user, SizeT size, Bool is_page, SizeT pad = 0) -> VoidPtr; diff --git a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc index 42242dbe..a3079e0c 100644 --- a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc @@ -265,6 +265,9 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz if (timeout > kTimeout) { kout << "Disk hangup!\r"; kSATAHba->Ports[kSATAIndex].Ci = 0; + err_global_get() = kErrorDiskIsCorrupted; + rtl_dma_free(size_buffer); + return; } @@ -293,7 +296,8 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz goto ahci_io_end; } else { kout << "Warning: Disk still busy after command completion!\r"; - while (kSATAHba->Ports[kSATAIndex].Tfd & (kSATASRBsy | kSATASRDrq)); + while (kSATAHba->Ports[kSATAIndex].Tfd & (kSATASRBsy | kSATASRDrq)) + ; } ahci_io_end: diff --git a/dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc index 4688203f..39efb7d3 100644 --- a/dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/DMA+Generic.cc @@ -105,7 +105,7 @@ Void drv_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz - 1) / SectorSz)); - rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF); + rt_out8(IO + ATA_REG_LBA0, (Lba) &0xFF); rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); @@ -123,7 +123,8 @@ Void drv_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorSz rt_out8(kATADevice.Bar(0x20) + 0x00, 0x09); // Start DMA engine - while (rt_in8(kATADevice.Bar(0x20) + ATA_REG_STATUS) & 0x01); + while (rt_in8(kATADevice.Bar(0x20) + ATA_REG_STATUS) & 0x01) + ; rt_out8(kATADevice.Bar(0x20) + 0x00, 0x00); // Stop DMA engine @@ -146,7 +147,7 @@ Void drv_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorS rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + (SectorSz - 1)) / SectorSz)); - rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF); + rt_out8(IO + ATA_REG_LBA0, (Lba) &0xFF); rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); @@ -162,7 +163,8 @@ Void drv_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT SectorS rt_out8(IO + 0x00, 0x09); // Start DMA engine - while (rt_in8(kATADevice.Bar(0x20) + ATA_REG_STATUS) & 0x01); + while (rt_in8(kATADevice.Bar(0x20) + ATA_REG_STATUS) & 0x01) + ; rt_out8(kATADevice.Bar(0x20) + 0x00, 0x00); // Stop DMA engine diff --git a/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc index b22330e4..aec21ee4 100644 --- a/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/PIO+Generic.cc @@ -118,7 +118,7 @@ Void drv_pio_std_read(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT Sect rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz) / SectorSz)); - rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF); + rt_out8(IO + ATA_REG_LBA0, (Lba) &0xFF); rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); @@ -143,7 +143,7 @@ Void drv_pio_std_write(UInt64 Lba, UInt16 IO, UInt8 Master, Char* Buf, SizeT Sec rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz) / SectorSz)); - rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF); + rt_out8(IO + ATA_REG_LBA0, (Lba) &0xFF); rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); diff --git a/dev/kernel/HALKit/ARM64/HalApplicationProcessor.cc b/dev/kernel/HALKit/ARM64/HalApplicationProcessor.cc index 14af1a16..7e55aa07 100644 --- a/dev/kernel/HALKit/ARM64/HalApplicationProcessor.cc +++ b/dev/kernel/HALKit/ARM64/HalApplicationProcessor.cc @@ -39,7 +39,8 @@ namespace Detail { STATIC BOOL kGICEnabled = NO; STATIC void mp_hang_fn(void) { - while (YES); + while (YES) + ; dbg_break_point(); } diff --git a/dev/kernel/HALKit/ARM64/HalKernelMain.cc b/dev/kernel/HALKit/ARM64/HalKernelMain.cc index 3e6701ea..bf5849ef 100644 --- a/dev/kernel/HALKit/ARM64/HalKernelMain.cc +++ b/dev/kernel/HALKit/ARM64/HalKernelMain.cc @@ -62,7 +62,8 @@ EXTERN_C void hal_init_platform(Kernel::HEL::BootInfoHeader* handover_hdr) { team_index = 0U; } - while (!UserProcessScheduler::The().SwitchTeam(kTeams[team_index])); + while (!UserProcessScheduler::The().SwitchTeam(kTeams[team_index])) + ; timer.Wait(); diff --git a/dev/kernel/HALKit/ARM64/HalPagingMgrARM64.cc b/dev/kernel/HALKit/ARM64/HalPagingMgrARM64.cc index 9fcee573..e0b67489 100644 --- a/dev/kernel/HALKit/ARM64/HalPagingMgrARM64.cc +++ b/dev/kernel/HALKit/ARM64/HalPagingMgrARM64.cc @@ -46,7 +46,8 @@ EXTERN_C Int32 mm_map_page(VoidPtr virtual_address, VoidPtr physical_address, UI NE_PAGE_STORE& page_store = NE_PAGE_STORE::The(); - while (page_store.fStoreOp); + while (page_store.fStoreOp) + ; page_store.fStoreOp = Yes; diff --git a/dev/kernel/HALKit/ARM64/Paging.h b/dev/kernel/HALKit/ARM64/Paging.h index e23c0538..2eb02bc1 100644 --- a/dev/kernel/HALKit/ARM64/Paging.h +++ b/dev/kernel/HALKit/ARM64/Paging.h @@ -84,9 +84,7 @@ namespace Detail { PageEnable = 31, }; - inline UInt8 control_register_cast(ControlRegisterBits reg) { - return static_cast(reg); - } + inline UInt8 control_register_cast(ControlRegisterBits reg) { return static_cast(reg); } } // namespace Detail struct PDE_4KB final { diff --git a/dev/kernel/HALKit/POWER/HalApplicationProcessor.cc b/dev/kernel/HALKit/POWER/HalApplicationProcessor.cc index eb44b72b..617b3dda 100644 --- a/dev/kernel/HALKit/POWER/HalApplicationProcessor.cc +++ b/dev/kernel/HALKit/POWER/HalApplicationProcessor.cc @@ -10,7 +10,8 @@ namespace Kernel::Detail { STATIC void mp_hang_fn(void) { - while (YES); + while (YES) + ; } } // namespace Kernel::Detail diff --git a/dev/kernel/HALKit/RISCV/HalApplicationProcessor.cc b/dev/kernel/HALKit/RISCV/HalApplicationProcessor.cc index 548167a4..80162b81 100644 --- a/dev/kernel/HALKit/RISCV/HalApplicationProcessor.cc +++ b/dev/kernel/HALKit/RISCV/HalApplicationProcessor.cc @@ -13,7 +13,8 @@ using namespace Kernel; namespace Kernel { namespace Detail { STATIC void mp_hang_fn(void) { - while (YES); + while (YES) + ; } } // namespace Detail -- cgit v1.2.3 From 152ab95f649d57521cbb90c257b11fa0c25b6b7e Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sun, 11 May 2025 23:23:51 +0200 Subject: dev(kernel:HeFS): Cleanup filesystem and AHCI driver. Signed-off-by: Amlal El Mahrouss --- dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc | 3 +- dev/kernel/src/FS/HeFS+FileSystemParser.cc | 68 ++++--------------------- 2 files changed, 12 insertions(+), 59 deletions(-) (limited to 'dev/kernel/HALKit') diff --git a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc index a3079e0c..21483560 100644 --- a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc @@ -264,8 +264,7 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz while (YES) { if (timeout > kTimeout) { kout << "Disk hangup!\r"; - kSATAHba->Ports[kSATAIndex].Ci = 0; - err_global_get() = kErrorDiskIsCorrupted; + err_global_get() = kErrorDiskIsCorrupted; rtl_dma_free(size_buffer); return; diff --git a/dev/kernel/src/FS/HeFS+FileSystemParser.cc b/dev/kernel/src/FS/HeFS+FileSystemParser.cc index f8208048..a285593e 100644 --- a/dev/kernel/src/FS/HeFS+FileSystemParser.cc +++ b/dev/kernel/src/FS/HeFS+FileSystemParser.cc @@ -489,6 +489,8 @@ namespace Detail { (Void)(kout << hex_number(hefsi_hash_64(dir_name)) << kendl); (Void)(kout << hex_number(dir->fHashPath) << kendl); + if (dir->fHashPath == 0) break; + if (hefsi_hash_64(dir_name) == dir->fHashPath) { for (SizeT inode_index = 0UL; inode_index < kHeFSSliceCount; ++inode_index) { mnt->fPacket.fPacketLba = dir->fINSlices[inode_index]; @@ -510,7 +512,7 @@ namespace Detail { } hefsi_traverse_tree(dir, mnt, root->fStartIND, start); - if (start == root->fStartIND || start == 0) break; + if (start == root->fStartIND || start == root->fStartIND) break; } delete node; @@ -536,6 +538,8 @@ namespace Detail { if (start > root->fEndIND) return NO; if (root->fStartIN > root->fEndIN) return NO; + ; + if (root->fStartBlock > root->fEndBlock) return NO; if (mnt) { HEFS_INDEX_NODE_DIRECTORY* dir = @@ -754,8 +758,8 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* mnt, _Input c } if (drv_std_get_size() < kHeFSMinimumDiskSize) { - err_global_get() = kErrorDiskIsTooTiny; - kout << "Error: Failed to allocate memory for boot node->\r"; + kout << "HeFS requires at least 128 GiB." << kendl; + err_global_get() = kErrorDisk; return NO; } @@ -812,10 +816,10 @@ _Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* mnt, _Input c MUST_PASS(root->fSectorSize); /// @note all HeFS strucutres are equal to 512, so here it's fine, unless fSectoSize is 2048. - const SizeT max_lba = drv_std_get_size() / root->fSectorSize; + const SizeT max_lba = (drv_std_get_size()) / root->fSectorSize; - const SizeT dir_max = max_lba / 5; // 5% for directory inodes - const SizeT inode_max = max_lba / 5; // 5% for inodes + const SizeT dir_max = max_lba / 300; // 5% for directory inodes + const SizeT inode_max = max_lba / 400; // 5% for inodes root->fStartIND = mnt->fLbaStart + kHeFSINDStartOffset; root->fEndIND = root->fStartIND + dir_max; @@ -1008,18 +1012,11 @@ _Output Bool HeFileSystemParser::INodeManip(_Input DriveTrait* mnt, VoidPtr bloc (Void)(kout << hex_number(start->fHashPath) << kendl); (Void)(kout << hex_number(start->fOffsetSliceLow) << kendl); - if (start->fOffsetSliceLow) { + if (start->fOffsetSliceLow && start->fHashPath) { mnt->fPacket.fPacketLba = ((UInt64) start->fOffsetSliceHigh << 32) | start->fOffsetSliceLow; mnt->fPacket.fPacketSize = block_sz; mnt->fPacket.fPacketContent = block; - if (mnt->fPacket.fPacketLba > root->fEndBlock) { - kout << "Error: Filesystem is full.\r"; - mm_delete_heap((VoidPtr) root); - delete start; - return NO; - } - if (is_input) { mnt->fInput(mnt->fPacket); } else { @@ -1157,49 +1154,6 @@ Boolean fs_init_hefs(Void) { parser.Format(&kMountPoint, kHeFSEncodingFlagsUTF8, kHeFSDefaultVolumeName); - MUST_PASS(parser.CreateINode(&kMountPoint, kHeFSEncodingFlagsUTF8, u8"/boot", u8"bootinfo.cfg~0", - kHeFSFileKindRegular)); - - MUST_PASS(parser.CreateINode(&kMountPoint, kHeFSEncodingFlagsUTF8, u8"/boot", u8"bootinfo.cfg~1", - kHeFSFileKindRegular)); - - MUST_PASS(parser.CreateINode(&kMountPoint, kHeFSEncodingFlagsUTF8, u8"/boot", u8"bootinfo.cfg~2", - kHeFSFileKindRegular)); - - Utf8Char contents_1[kHeFSBlockLen] = {0}; - - urt_set_memory(contents_1, 0, kHeFSBlockLen); - - MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", - u8"bootinfo.cfg~0", kHeFSFileKindRegular, YES)); - - if (*contents_1 != u'\0') { - (Void)(kout << "/boot/bootinfo.cfg~0:" << kendl); - (Void)(kout8 << contents_1 << kendl8); - - MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", - u8"bootinfo.cfg~1", kHeFSFileKindRegular, YES)); - - MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", - u8"bootinfo.cfg~2", kHeFSFileKindRegular, YES)); - } else { - auto src = - u8"[boot]\r" - u8"path=bootz.efi\r" - u8"name=BootZ\r" - u8"[kernel]\r" - u8"path=krnl.efi\r" - u8"name=NeKernel\r" - u8"[chk]\r" - u8"path=chk.efi\r" - u8"name=SysChk\r"; - - urt_copy_memory((VoidPtr) src, contents_1, urt_string_len(src)); - - MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", - u8"bootinfo.cfg~0", kHeFSFileKindRegular, NO)); - } - return YES; } } // namespace Kernel::HeFS -- cgit v1.2.3