From f7492b792e5ef083a856787fde2f581f369fa200 Mon Sep 17 00:00:00 2001 From: Amlal Date: Wed, 7 May 2025 10:19:31 +0200 Subject: feat(modules/MBCI): Finalize MBCI base module. Signed-off-by: Amlal --- dev/modules/MBCI/MBCI.h | 27 ++++++++++++--------------- 1 file changed, 12 insertions(+), 15 deletions(-) (limited to 'dev/modules') diff --git a/dev/modules/MBCI/MBCI.h b/dev/modules/MBCI/MBCI.h index 1038f17c..f2bd7f71 100644 --- a/dev/modules/MBCI/MBCI.h +++ b/dev/modules/MBCI/MBCI.h @@ -96,27 +96,24 @@ enum MBCIHostState { /// @brief An AuthKey is a context used to tokenize data for an MBCI packet. typedef UInt32 MBCIAuthKeyType; +/// @internal +inline BOOL busi_test_mmio(_Input volatile struct IMBCIHost* host, const UInt32 test) { + host->MMIOTest = test; + while (host->MMIOTest == test); + + return host->MMIOTest == 0; +} + /// @brief Read Auth key for MBCI host. /// @param host the mbci host to get the key on. /// @return the 24-bit key. inline MBCIAuthKeyType mbci_read_auth_key(_Input volatile struct IMBCIHost* host) { - constexpr auto const kChallengeMBCI = 0xdeadbeef; - - host->MMIOTest = kChallengeMBCI; - - if (host->MMIOTest == kChallengeMBCI) { - return (host->Esb[kMBCIESBSz - 1] << 16) | (host->Esb[kMBCIESBSz - 2] << 8) | - (host->Esb[kMBCIESBSz - 3] & 0xFF); - } - - return kChallengeMBCI; -} + auto const kChallengeMBCI = 0x1; // MBCI Challenge test -inline BOOL mbci_test_mmio(_Input volatile struct IMBCIHost* host) { - constexpr auto const kChallengeMBCI = 0xdeadbeef; + if (!busi_test_mmio(host, kChallengeMBCI)) return ~0; - host->MMIOTest = kChallengeMBCI; - return host->MMIOTest == kChallengeMBCI; + return (host->Esb[kMBCIESBSz - 1] << 16) | (host->Esb[kMBCIESBSz - 2] << 8) | + (host->Esb[kMBCIESBSz - 3] & 0xFF); } } // namespace Kernel -- cgit v1.2.3 From a3ee1df87feddec339c710068e0922a40c6fd494 Mon Sep 17 00:00:00 2001 From: Amlal Date: Wed, 7 May 2025 10:25:43 +0200 Subject: feat(modules/MBCI): add tiemout for memory test. Signed-off-by: Amlal --- dev/modules/MBCI/MBCI.h | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) (limited to 'dev/modules') diff --git a/dev/modules/MBCI/MBCI.h b/dev/modules/MBCI/MBCI.h index f2bd7f71..37c18f79 100644 --- a/dev/modules/MBCI/MBCI.h +++ b/dev/modules/MBCI/MBCI.h @@ -99,7 +99,14 @@ typedef UInt32 MBCIAuthKeyType; /// @internal inline BOOL busi_test_mmio(_Input volatile struct IMBCIHost* host, const UInt32 test) { host->MMIOTest = test; - while (host->MMIOTest == test); + UInt16 timeout = 0UL; + + while (host->MMIOTest == test) { + ++timeout; + + if (timeout > 0x1000) + return NO; + } return host->MMIOTest == 0; } -- cgit v1.2.3 From 8acaf9b721973fdd852abc01fc44ba1152b8f72a Mon Sep 17 00:00:00 2001 From: Amlal Date: Wed, 7 May 2025 11:14:25 +0200 Subject: feat(kernel): HeFS fixes, AHCI improvements, and MBCI tweaks. why? - Some parts were causing issues on the filesystem. - The slot probe code was naive. - Made the current MBCI implementation clearer. Signed-off-by: Amlal --- dev/kernel/HALKit/AMD64/HalDebugOutput.cc | 64 +++++++++++++++++++------ dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc | 18 +++++-- dev/kernel/src/FS/HeFS+FileSystemParser.cc | 15 ++++-- dev/modules/MBCI/MBCI.h | 6 +-- docs/tex/mbci.tex | 2 +- 5 files changed, 79 insertions(+), 26 deletions(-) (limited to 'dev/modules') diff --git a/dev/kernel/HALKit/AMD64/HalDebugOutput.cc b/dev/kernel/HALKit/AMD64/HalDebugOutput.cc index 34b99ffe..a9759fc0 100644 --- a/dev/kernel/HALKit/AMD64/HalDebugOutput.cc +++ b/dev/kernel/HALKit/AMD64/HalDebugOutput.cc @@ -57,6 +57,54 @@ namespace Detail { TerminalDevice::~TerminalDevice() = default; +EXTERN_C void ke_utf_io_write(IDeviceObject* obj, const Utf8Char* bytes) { + NE_UNUSED(bytes); + NE_UNUSED(obj); + +#ifdef __DEBUG__ + Detail::hal_serial_init(); + + if (!bytes || Detail::kState != kStateReady) return; + + if (*bytes == 0) return; + + Detail::kState = kStateTransmit; + + SizeT index = 0; + SizeT len = 0; + + index = 0; + len = urt_string_len(bytes); + + static BOOL not_important = YES; + + 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; + + if (bytes[index] == '*') { + if (not_important) + not_important = NO; + else + not_important = YES; + + ++index; + + continue; + } + + ++index; + } + + Detail::kState = kStateReady; +#endif // __DEBUG__ +} + EXTERN_C void ke_io_write(IDeviceObject* obj, const Char* bytes) { NE_UNUSED(bytes); NE_UNUSED(obj); @@ -74,7 +122,7 @@ EXTERN_C void ke_io_write(IDeviceObject* obj, const Char* bytes) { SizeT len = 0; index = 0; - len = rt_string_len(bytes, 256U); + len = rt_string_len(bytes); static SizeT x = kFontSizeX, y = kFontSizeY; @@ -172,20 +220,8 @@ TerminalDevice TerminalDevice::The() noexcept { Utf8TerminalDevice::~Utf8TerminalDevice() = default; -STATIC Void ke_io_write_utf(IDeviceObject*, const Utf8Char* str) { - auto len = urt_string_len(str); - - for (auto size = 0ul; size < len; ++size) { - Char buf[2]; - buf[0] = str[size]; - buf[1] = 0; - - Kernel::ke_io_write(nullptr, buf); - } -} - Utf8TerminalDevice Utf8TerminalDevice::The() noexcept { - Utf8TerminalDevice out(Kernel::ke_io_write_utf, + Utf8TerminalDevice out(Kernel::ke_utf_io_write, [](IDeviceObject*, const Utf8Char*) -> Void {}); return out; } diff --git a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc index cf1841bd..4b0270ab 100644 --- a/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc +++ b/dev/kernel/HALKit/AMD64/Storage/AHCI+Generic.cc @@ -154,10 +154,18 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz UIntPtr slot = drv_find_cmd_slot_ahci(&kSATAHba->Ports[kSATAIndex]); - if (slot == ~0UL) { - kout << "No free command slot!\r"; - err_global_get() = kErrorDisk; - return; + UInt16 timeout = 0; + + while (slot == ~0UL) { + kout << "No free command slot found, AHCI disk is busy!\r"; + + if (timeout > 0x1000) { + err_global_get() = kErrorDisk; + return; + } + + slot = drv_find_cmd_slot_ahci(&kSATAHba->Ports[kSATAIndex]); + ++timeout; } volatile HbaCmdHeader* command_header = @@ -173,7 +181,7 @@ STATIC Void drv_std_input_output_ahci(UInt64 lba, UInt8* buffer, SizeT sector_sz rt_set_memory((VoidPtr) command_table, 0, sizeof(HbaCmdTbl)); - VoidPtr ptr = rtl_dma_alloc(size_buffer, 4096); + VoidPtr ptr = rtl_dma_alloc(size_buffer, kib_cast(4)); rtl_dma_flush(ptr, size_buffer); diff --git a/dev/kernel/src/FS/HeFS+FileSystemParser.cc b/dev/kernel/src/FS/HeFS+FileSystemParser.cc index 465cfc32..1cd7e61a 100644 --- a/dev/kernel/src/FS/HeFS+FileSystemParser.cc +++ b/dev/kernel/src/FS/HeFS+FileSystemParser.cc @@ -514,10 +514,14 @@ namespace Detail { if (start > root->fEndIND) break; } - node_arr[start_cnt + 1].fDeleted = 1UL; err_global_get() = kErrorSuccess; delete dir; + if (start_cnt == 0) { + delete[] node_arr; + node_arr = nullptr; + } + return node_arr; } @@ -577,7 +581,7 @@ namespace Detail { node->fOffsetSlices = root->fStartBlock; - auto offset = 0; + auto offset = kHeFSBlockLen; SizeT cnt = 0ULL; @@ -1031,6 +1035,10 @@ _Output Bool HeFileSystemParser::INodeManip(_Input DriveTrait* mnt, VoidPtr bloc mnt->fPacket.fPacketSize = kHeFSBlockLen; mnt->fPacket.fPacketContent = block; + if (mnt->fPacket.fPacketLba > root->fEndBlock) { + goto inode_manip_fail; + } + in ? mnt->fInput(mnt->fPacket) : mnt->fOutput(mnt->fPacket); sz_out += kHeFSBlockLen; @@ -1049,6 +1057,7 @@ _Output Bool HeFileSystemParser::INodeManip(_Input DriveTrait* mnt, VoidPtr bloc } } +inode_manip_fail: delete[] nodes; return NO; } @@ -1174,7 +1183,7 @@ Boolean fs_init_hefs(Void) { Utf8Char contents_1[kHeFSBlockLen] = u8"ロケットにはジエットエンジン\r"; MUST_PASS(parser.INodeManip(&kMountPoint, contents_1, kHeFSBlockLen, u8"/boot", - kHeFSFileKindRegular, u8"ジェット警察.txt", NO)); + kHeFSFileKindRegular, u8"ジェット警察.txt", YES)); return YES; } diff --git a/dev/modules/MBCI/MBCI.h b/dev/modules/MBCI/MBCI.h index 37c18f79..37f802ec 100644 --- a/dev/modules/MBCI/MBCI.h +++ b/dev/modules/MBCI/MBCI.h @@ -39,7 +39,7 @@ enum { }; /// @brief MBCI Host header. -struct PACKED IMBCIHost final { +volatile struct PACKED IMBCIHost final { UInt32 Magic; UInt32 HostId; UInt16 VendorId; @@ -97,7 +97,7 @@ enum MBCIHostState { typedef UInt32 MBCIAuthKeyType; /// @internal -inline BOOL busi_test_mmio(_Input volatile struct IMBCIHost* host, const UInt32 test) { +inline BOOL busi_test_mmio(_Input struct IMBCIHost* host, _Input const UInt32 test) { host->MMIOTest = test; UInt16 timeout = 0UL; @@ -114,7 +114,7 @@ inline BOOL busi_test_mmio(_Input volatile struct IMBCIHost* host, const UInt32 /// @brief Read Auth key for MBCI host. /// @param host the mbci host to get the key on. /// @return the 24-bit key. -inline MBCIAuthKeyType mbci_read_auth_key(_Input volatile struct IMBCIHost* host) { +inline MBCIAuthKeyType mbci_read_auth_key(_Input struct IMBCIHost* host) { auto const kChallengeMBCI = 0x1; // MBCI Challenge test if (!busi_test_mmio(host, kChallengeMBCI)) return ~0; diff --git a/docs/tex/mbci.tex b/docs/tex/mbci.tex index a23bc87b..99676803 100644 --- a/docs/tex/mbci.tex +++ b/docs/tex/mbci.tex @@ -33,7 +33,7 @@ The MBCI bus interface includes the following signal lines: \subsection*{IMBCIHost Structure} \begin{lstlisting}[language=C++,basicstyle=\ttfamily\footnotesize] -struct IMBCIHost { +volatile struct IMBCIHost { UInt32 Magic; UInt32 HostId; UInt16 VendorId; -- cgit v1.2.3 From a8782019a20f5487494e436f79b876b57f7229e1 Mon Sep 17 00:00:00 2001 From: Amlal Date: Thu, 8 May 2025 08:48:57 +0200 Subject: feat(tooling): update HeFS headers, and better HeFS tooling. Signed-off-by: Amlal --- dev/kernel/FSKit/HeFS.h | 8 +-- dev/kernel/src/FS/HeFS+FileSystemParser.cc | 20 ++----- dev/modules/MBCI/MBCI.h | 5 +- tooling/hefs.h | 4 +- tooling/mkfs.hefs.cc | 92 ++++++++++++++++++------------ 5 files changed, 68 insertions(+), 61 deletions(-) (limited to 'dev/modules') diff --git a/dev/kernel/FSKit/HeFS.h b/dev/kernel/FSKit/HeFS.h index f7e1d648..f5f42b47 100644 --- a/dev/kernel/FSKit/HeFS.h +++ b/dev/kernel/FSKit/HeFS.h @@ -125,10 +125,10 @@ struct PACKED HEFS_BOOT_NODE final { Kernel::UInt16 fDiskFlags; /// @brief Flags of the disk. (read-only, read-write, etc). Kernel::UInt16 fVID; /// @brief Virtual Identification Number within an EPM disk. (0xFFFF if not used). - Kernel::UInt64 fStartIN; /// @brief Reserved for future use. - Kernel::UInt64 fEndIN; /// @brief Reserved for future use. - Kernel::UInt64 fStartBlock; /// @brief Reserved for future use. - Kernel::UInt64 fEndBlock; /// @brief Reserved for future use. + Kernel::UInt64 fStartIN; /// @brief Start INodes range + Kernel::UInt64 fEndIN; /// @brief End INodes range + Kernel::UInt64 fStartBlock; /// @brief Start Blocks range + Kernel::UInt64 fEndBlock; /// @brief End Blocks range Kernel::Char fPad[272]; }; diff --git a/dev/kernel/src/FS/HeFS+FileSystemParser.cc b/dev/kernel/src/FS/HeFS+FileSystemParser.cc index 1cd7e61a..a324da2a 100644 --- a/dev/kernel/src/FS/HeFS+FileSystemParser.cc +++ b/dev/kernel/src/FS/HeFS+FileSystemParser.cc @@ -459,7 +459,7 @@ namespace Detail { const Utf8Char* dir_name, const Utf8Char* file_name, UInt8 kind, SizeT* cnt) { - if (mnt) { + if (mnt && cnt) { auto start = root->fStartIND; if (start > root->fEndIND) return nullptr; @@ -514,7 +514,7 @@ namespace Detail { if (start > root->fEndIND) break; } - err_global_get() = kErrorSuccess; + err_global_get() = kErrorSuccess; delete dir; if (start_cnt == 0) { @@ -535,7 +535,7 @@ namespace Detail { STATIC ATTRIBUTE(unused) _Output BOOL hefsi_update_in_status(HEFS_BOOT_NODE* root, DriveTrait* mnt, const Utf8Char* dir_name, HEFS_INDEX_NODE* node, BOOL delete_or_create) { - if (!root) return NO; + if (!root || !mnt) return NO; auto start = root->fStartIND; @@ -555,8 +555,6 @@ namespace Detail { mnt->fInput(mnt->fPacket); - kout8 << dir_name << u8"\r"; - (Void)(kout << hex_number(hefsi_hash_64(dir_name)) << kendl); (Void)(kout << hex_number(dir->fHashPath) << kendl); @@ -621,12 +619,6 @@ namespace Detail { mnt->fInput(mnt->fPacket); - kout8 << u8"HashPath: "; - (Void)(kout << hex_number(tmp_node.fHashPath) << kendl); - - kout8 << u8"HashPath: "; - (Void)(kout << hex_number(hash_file) << kendl); - if (tmp_node.fHashPath != hash_file) { continue; } @@ -690,7 +682,7 @@ namespace Detail { auto start = root->fStartIND; while (YES) { - if (start == 0 || start > root->fEndIND) break; + if (start == 0UL || start > root->fEndIND) break; mnt->fPacket.fPacketLba = start; mnt->fPacket.fPacketSize = sizeof(HEFS_INDEX_NODE_DIRECTORY); @@ -714,10 +706,10 @@ namespace Detail { mnt->fOutput(mnt->fPacket); } - if (dir->fColor == kHeFSBlack && dir->fChild != 0) { + if (dir->fColor == kHeFSBlack && dir->fChild != 0UL) { dir->fColor = kHeFSRed; hefsi_rotate_tree(start, mnt); - } else if (dir->fColor == kHeFSBlack && dir->fChild == 0) { + } else if (dir->fColor == kHeFSBlack && dir->fChild == 0UL) { dir->fColor = kHeFSBlack; mnt->fPacket.fPacketLba = start; diff --git a/dev/modules/MBCI/MBCI.h b/dev/modules/MBCI/MBCI.h index 37f802ec..99ecf802 100644 --- a/dev/modules/MBCI/MBCI.h +++ b/dev/modules/MBCI/MBCI.h @@ -100,12 +100,11 @@ typedef UInt32 MBCIAuthKeyType; inline BOOL busi_test_mmio(_Input struct IMBCIHost* host, _Input const UInt32 test) { host->MMIOTest = test; UInt16 timeout = 0UL; - + while (host->MMIOTest == test) { ++timeout; - if (timeout > 0x1000) - return NO; + if (timeout > 0x1000) return NO; } return host->MMIOTest == 0; diff --git a/tooling/hefs.h b/tooling/hefs.h index f281e7a3..68e0f906 100644 --- a/tooling/hefs.h +++ b/tooling/hefs.h @@ -102,8 +102,8 @@ struct __attribute__((packed)) BootNode { std::uint16_t vid{}; std::uint64_t startIN{}; std::uint64_t endIN{}; - std::uint64_t reserved3{}; - std::uint64_t reserved4{}; + std::uint64_t startBlock{}; + std::uint64_t endBlock{}; char pad[272]{}; }; } // namespace mkfs::hefs diff --git a/tooling/mkfs.hefs.cc b/tooling/mkfs.hefs.cc index 5a706646..2ddc7484 100644 --- a/tooling/mkfs.hefs.cc +++ b/tooling/mkfs.hefs.cc @@ -16,9 +16,10 @@ static size_t kSectorSize = 512; int main(int argc, char** argv) { if (argc < 2) { - mkfs::console_out() << "hefs: Usage: mkfs.hefs -L