From a2bc9eee9184a81d80ab54a09bd239ed26d5293e Mon Sep 17 00:00:00 2001 From: Amlal Date: Fri, 25 Apr 2025 15:24:52 +0200 Subject: dev, kernel: improve nefs format method, hefs format method. Signed-off-by: Amlal --- dev/kernel/FSKit/HeFS.h | 18 ++-- dev/kernel/FSKit/NeFS.h | 9 +- dev/kernel/HALKit/AMD64/HalKernelMain.cc | 2 +- dev/kernel/KernelKit/DriveMgr.h | 2 +- dev/kernel/NewKit/Utils.h | 4 +- dev/kernel/src/FS/HeFS.cc | 98 ++++++++++++++---- dev/kernel/src/FS/NeFS.cc | 167 ++++++++----------------------- dev/kernel/src/WideUtils.cc | 18 +++- 8 files changed, 152 insertions(+), 166 deletions(-) (limited to 'dev/kernel') diff --git a/dev/kernel/FSKit/HeFS.h b/dev/kernel/FSKit/HeFS.h index e4da9a47..63725f04 100644 --- a/dev/kernel/FSKit/HeFS.h +++ b/dev/kernel/FSKit/HeFS.h @@ -100,7 +100,7 @@ struct PACKED HEFS_BOOT_NODE final { Kernel::UInt64 fSectorCount; /// @brief Number of sectors in the filesystem. Kernel::UInt64 fSectorSize; /// @brief Size of the sector. Kernel::UInt32 fChecksum; /// @brief Checksum of the boot node. - Kernel::UInt8 fDriveKind; /// @brief Kind of the drive. (Hard Drive, Solid State Drive, Optical + Kernel::UInt8 fDiskKind; /// @brief Kind of the drive. (Hard Drive, Solid State Drive, Optical /// Drive, etc). Kernel::UInt8 fEncoding; /// @brief Encoding of the filesystem. (UTF-8, UTF-16, etc). Kernel::UInt64 fStartIND; /// @brief Start of the INode tree. @@ -332,7 +332,7 @@ inline const Char* hefs_file_flags_to_string(UInt32 flags) noexcept { } } // namespace Kernel::Detail -namespace Kernel { +namespace Kernel::HeFS { /// @brief HeFS filesystem parser class. /// @details This class is used to parse the HeFS filesystem. class HeFileSystemParser final { @@ -351,17 +351,13 @@ class HeFileSystemParser final { /// @brief Make a EPM+HeFS drive out of the disk. /// @param drive The drive to write on. /// @return If it was sucessful, see err_local_get(). - _Output Bool FormatEPM(_Input _Output DriveTrait* drive, _Input const Lba end_lba, - _Input const Int32 flags, const Char* part_name); - - /// @brief Make a GPT+HeFS drive out of the disk. - /// @param drive The drive to write on. - /// @return If it was sucessful, see err_local_get(). - _Output Bool FormatGPT(_Input _Output DriveTrait* drive, _Input const Lba end_lba, - _Input const Int32 flags, const Char* part_name); + _Output Bool Format(_Input _Output DriveTrait* drive, _Input const Int32 flags, + const Utf16Char* part_name); public: UInt32 mDriveIndex{MountpointInterface::kDriveIndexA}; /// @brief The drive index which this /// filesystem is mounted on. }; -} // namespace Kernel \ No newline at end of file + +Boolean fs_init_hefs(Void) noexcept; +} // namespace Kernel::HeFS \ No newline at end of file diff --git a/dev/kernel/FSKit/NeFS.h b/dev/kernel/FSKit/NeFS.h index 425ab051..2235ca4f 100644 --- a/dev/kernel/FSKit/NeFS.h +++ b/dev/kernel/FSKit/NeFS.h @@ -31,6 +31,8 @@ default. #define kNeFSInvalidCatalog (-1) #define kNeFSCatalogNameLen (256) +#define kNeFSVolumeName "NeFS Volume" + #define kNeFSMinimumDiskSize (mib_cast(8)) #define kNeFSSectorSz (512) @@ -305,11 +307,8 @@ class NeFileSystemParser final { /// @brief Make a EPM+NeFS drive out of the disk. /// @param drive The drive to write on. /// @return If it was sucessful, see err_local_get(). - _Output Bool FormatEPM(_Input _Output DriveTrait* drive, _Input const Lba end_lba, - _Input const Int32 flags, const Char* part_name); - - _Output Bool FormatGPT(_Input _Output DriveTrait* drive, _Input const Lba end_lba, - _Input const Int32 flags, const Char* part_name); + _Output Bool Format(_Input _Output DriveTrait* drive, _Input const Int32 flags, + const Char* part_name); public: UInt32 mDriveIndex{kNeFSSubDriveA}; diff --git a/dev/kernel/HALKit/AMD64/HalKernelMain.cc b/dev/kernel/HALKit/AMD64/HalKernelMain.cc index 82020520..8f4b55cb 100644 --- a/dev/kernel/HALKit/AMD64/HalKernelMain.cc +++ b/dev/kernel/HALKit/AMD64/HalKernelMain.cc @@ -105,7 +105,7 @@ EXTERN_C Int32 hal_init_platform(Kernel::HEL::BootInfoHeader* handover_hdr) { EXTERN_C Kernel::Void hal_real_init(Kernel::Void) noexcept { hal_pre_init_scheduler(); - Kernel::NeFS::fs_init_nefs(); + Kernel::HeFS::fs_init_hefs(); Kernel::HAL::mp_init_cores(kHandoverHeader->f_HardwareTables.f_VendorPtr); diff --git a/dev/kernel/KernelKit/DriveMgr.h b/dev/kernel/KernelKit/DriveMgr.h index e0bed8f5..263b728e 100644 --- a/dev/kernel/KernelKit/DriveMgr.h +++ b/dev/kernel/KernelKit/DriveMgr.h @@ -62,7 +62,7 @@ struct DriveTrait final { } fPacket; Lba fLbaStart{0}, fLbaEnd{0}; - SizeT fSectorSz{512}; + SizeT fSectorSz{kDriveSectorSz}; Void (*fInput)(DrivePacket packet); Void (*fOutput)(DrivePacket packet); diff --git a/dev/kernel/NewKit/Utils.h b/dev/kernel/NewKit/Utils.h index ca1c7d1c..2f0fc60b 100644 --- a/dev/kernel/NewKit/Utils.h +++ b/dev/kernel/NewKit/Utils.h @@ -17,7 +17,6 @@ void rt_zero_memory(voidPtr pointer, Size len); Int rt_string_cmp(const Char* src, const Char* cmp, Size len); const Char* rt_alloc_string(const Char* text); Size rt_string_len(const Char* str); -Size wrt_string_len(const Utf16Char* str); Size rt_string_len(const Char* str, SizeT _len); Boolean rt_to_string(Char* str_out, UInt64 base, Int32 limit); Boolean rt_is_newln(Char chr); @@ -27,4 +26,7 @@ Int rt_to_uppercase(Int c); Int rt_to_lower(Int c); voidPtr rt_string_in_string(const Char* in, const Char* needle); char* rt_string_has_char(Char* str, Char chr); + +Int wrt_copy_memory(const voidPtr src, voidPtr dst, Size len); +Size wrt_string_len(const Utf16Char* str); } // namespace Kernel diff --git a/dev/kernel/src/FS/HeFS.cc b/dev/kernel/src/FS/HeFS.cc index 88837273..5beb8b3f 100644 --- a/dev/kernel/src/FS/HeFS.cc +++ b/dev/kernel/src/FS/HeFS.cc @@ -4,6 +4,7 @@ ------------------------------------------- */ +#include "NewKit/Defines.h" #ifdef __FSKIT_INCLUDES_HEFS__ #include @@ -636,34 +637,95 @@ namespace Detail { /// real-time. /// @note This is certainly take longer to format a disk with it, but worth-it in the long run. -namespace Kernel { +namespace Kernel::HeFS { /// @brief Make a EPM+HeFS drive out of the disk. /// @param drive The drive to write on. /// @return If it was sucessful, see err_local_get(). -_Output Bool HeFileSystemParser::FormatEPM(_Input _Output DriveTrait* drive, - _Input const Lba end_lba, _Input const Int32 flags, - const Char* part_name) { +_Output Bool HeFileSystemParser::Format(_Input _Output DriveTrait* drive, _Input const Int32 flags, + const Utf16Char* part_name) { NE_UNUSED(drive); - NE_UNUSED(end_lba); NE_UNUSED(flags); NE_UNUSED(part_name); - return NO; + // verify disk. + drive->fVerify(drive->fPacket); + + // if disk isn't good, then error out. + if (false == drive->fPacket.fPacketGood) { + err_global_get() = kErrorDiskIsCorrupted; + return false; + } + + + HEFS_BOOT_NODE* root = new HEFS_BOOT_NODE(); + + if (!root) { + kout << "Error: Failed to allocate memory for boot node.\r"; + return NO; + } + + rt_set_memory(root, 0, sizeof(HEFS_BOOT_NODE)); + + rt_copy_memory((VoidPtr) "fs/hefs-packet", drive->fPacket.fPacketMime, + rt_string_len("fs/hefs-packet")); + + wrt_copy_memory((VoidPtr) part_name, root->fVolName, wrt_string_len(part_name)); + rt_copy_memory((VoidPtr) kHeFSMagic, root->fMagic, sizeof(kHeFSMagic)); + + root->fBadSectors = 0; + + root->fSectorCount = drv_get_sector_count(); + + root->fSectorSize = drive->fSectorSz; + + root->fStartIND = drive->fLbaStart + sizeof(HEFS_BOOT_NODE); + root->fEndIND = drive->fLbaEnd; + + root->fINDCount = root->fEndIND - root->fStartIND; + + root->fDiskSize = drv_get_size(); + root->fDiskStatus = kHeFSStatusUnlocked; + + root->fDiskFlags = flags; + + if (drive->fKind & kMassStorageDrive) { + } else if (drive->fKind & kHeFSOpticalDrive) { + root->fDiskKind = kHeFSOpticalDrive; + } else { + root->fDiskKind = kHeFSUnknown; + } + + root->fReserved = 0; + root->fReserved2 = 0; + root->fReserved3 = 0; + root->fReserved4 = 0; + + root->fChecksum = 0; + + root->fVID = kHeFSInvalidVID; + + drive->fPacket.fPacketLba = drive->fLbaStart; + drive->fPacket.fPacketSize = sizeof(HEFS_BOOT_NODE); + drive->fPacket.fPacketContent = root; + + drive->fOutput(drive->fPacket); + + return YES; } -/// @brief Make a EPM+HeFS drive out of the disk. -/// @param drive The drive to write on. -/// @return If it was sucessful, see err_local_get(). -_Output Bool HeFileSystemParser::FormatGPT(_Input _Output DriveTrait* drive, - _Input const Lba end_lba, _Input const Int32 flags, - const Char* part_name) { - NE_UNUSED(drive); - NE_UNUSED(end_lba); - NE_UNUSED(flags); - NE_UNUSED(part_name); +Boolean fs_init_hefs(Void) noexcept { + kout << "Creating main disk...\r"; - return NO; + auto drv = io_construct_main_drive(); + + if (drv.fPacket.fPacketReadOnly == YES) + ke_panic(RUNTIME_CHECK_FILESYSTEM, "Main filesystem cannot be mounted."); + + HeFileSystemParser parser; + parser.Format(&drv, kHeFSEncodingUTF16, kHeFSDefaultVoluneName); + + return YES; } -} // namespace Kernel +} // namespace Kernel::HeFS #endif // ifdef __FSKIT_INCLUDES_HEFS__ diff --git a/dev/kernel/src/FS/NeFS.cc b/dev/kernel/src/FS/NeFS.cc index d0934f94..e0cdc71b 100644 --- a/dev/kernel/src/FS/NeFS.cc +++ b/dev/kernel/src/FS/NeFS.cc @@ -415,25 +415,14 @@ _Output NEFS_CATALOG_STRUCT* NeFileSystemParser::CreateCatalog(_Input const Char return nullptr; } -_Output Bool NeFileSystemParser::FormatGPT(_Input _Output DriveTrait* drive, - _Input const Lba end_lba, _Input const Int32 flags, - const Char* part_name) { - NE_UNUSED(drive); - NE_UNUSED(end_lba); - NE_UNUSED(flags); - NE_UNUSED(part_name); - - (Void)(kout << "FormatGPT: Not implemented yet.\r"); - - return NO; -} - /// @brief Make a EPM+NeFS drive out of the disk. /// @param drive The drive to write on. /// @return If it was sucessful, see err_global_get(). -bool NeFileSystemParser::FormatEPM(_Input _Output DriveTrait* drive, _Input const Lba endLba, - _Input const Int32 flags, const Char* part_name) { - if (*part_name == 0 || endLba == 0) return false; +bool NeFileSystemParser::Format(_Input _Output DriveTrait* drive, _Input const Int32 flags, + const Char* part_name) { + if (*part_name == 0) return false; + + NE_UNUSED(flags); // verify disk. drive->fVerify(drive->fPacket); @@ -449,7 +438,7 @@ bool NeFileSystemParser::FormatEPM(_Input _Output DriveTrait* drive, _Input cons Char fs_buf[sizeof(NEFS_ROOT_PARTITION_BLOCK)] = {0}; - Lba start = kNeFSRootCatalogStartAddress; + Lba start = drive->fLbaStart; drive->fPacket.fPacketContent = fs_buf; drive->fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); @@ -457,129 +446,48 @@ bool NeFileSystemParser::FormatEPM(_Input _Output DriveTrait* drive, _Input cons drive->fInput(drive->fPacket); - if (flags & kNeFSPartitionTypeBoot) { - // make it bootable when needed. - Char buf_epm[kNeFSSectorSz] = {0}; - - EPM_PART_BLOCK* epm_boot = (EPM_PART_BLOCK*) buf_epm; - - // Write a new EPM entry. - - constexpr auto kFsName = "NeFS"; - constexpr auto kBlockName = "NeKernel:"; - - epm_boot->FsVersion = kNeFSVersionInteger; - epm_boot->LbaStart = start; - epm_boot->SectorSz = kNeFSSectorSz; - - rt_copy_memory(reinterpret_cast(const_cast(kFsName)), epm_boot->Fs, - rt_string_len(kFsName)); - rt_copy_memory(reinterpret_cast(const_cast(kBlockName)), epm_boot->Name, - rt_string_len(kBlockName)); - rt_copy_memory(reinterpret_cast(const_cast(kEPMMagic)), epm_boot->Magic, - rt_string_len(kEPMMagic)); - - Lba outEpmLba = kEPMBootBlockLba; - - Char buf[kNeFSSectorSz] = {0}; - - Lba prevStart = 0; - SizeT cnt = 0; - - while (drive->fPacket.fPacketGood) { - drive->fPacket.fPacketContent = buf; - drive->fPacket.fPacketSize = sizeof(EPM_PART_BLOCK); - drive->fPacket.fPacketLba = outEpmLba; - - drive->fInput(drive->fPacket); - - if (buf[0] == 0) { - epm_boot->LbaStart = prevStart; - - if (epm_boot->LbaStart) epm_boot->LbaStart = outEpmLba; - - epm_boot->LbaEnd = endLba; - epm_boot->NumBlocks = cnt; - - drive->fPacket.fPacketContent = buf_epm; - drive->fPacket.fPacketSize = sizeof(EPM_PART_BLOCK); - drive->fPacket.fPacketLba = outEpmLba; - - drive->fOutput(drive->fPacket); + NEFS_ROOT_PARTITION_BLOCK* part_block = (NEFS_ROOT_PARTITION_BLOCK*) fs_buf; - break; - } else { - prevStart = ((EPM_PART_BLOCK*) buf)->LbaStart + ((EPM_PART_BLOCK*) buf)->LbaEnd; - } - - outEpmLba += sizeof(EPM_PART_BLOCK); - ++cnt; - } - } - - // disk isnt faulty and data has been fetched. - while (drive->fPacket.fPacketGood) { - NEFS_ROOT_PARTITION_BLOCK* part_block = (NEFS_ROOT_PARTITION_BLOCK*) fs_buf; - - // check for an empty partition here. - if (part_block->PartitionName[0] == 0 && - rt_string_cmp(part_block->Ident, kNeFSIdent, kNeFSIdentLen)) { - // partition is free and valid. - - part_block->Version = kNeFSVersionInteger; - - const auto kNeFSUntitledHD = part_name; - - rt_copy_memory((VoidPtr) kNeFSIdent, (VoidPtr) part_block->Ident, kNeFSIdentLen); - - rt_copy_memory((VoidPtr) kNeFSUntitledHD, (VoidPtr) part_block->PartitionName, - rt_string_len(kNeFSUntitledHD)); + const auto kNeFSUntitledHD = part_name; - SizeT sectorCount = drv_get_sector_count(); - SizeT diskSize = drv_get_size(); + rt_copy_memory((VoidPtr) kNeFSIdent, (VoidPtr) part_block->Ident, kNeFSIdentLen); - part_block->Kind = kNeFSPartitionTypeStandard; - part_block->StartCatalog = kNeFSCatalogStartAddress; - part_block->Flags = kNeFSPartitionTypeStandard; - part_block->CatalogCount = sectorCount / sizeof(NEFS_CATALOG_STRUCT); - part_block->FreeSectors = sectorCount / sizeof(NEFS_CATALOG_STRUCT); - part_block->SectorCount = sectorCount; - part_block->DiskSize = diskSize; - part_block->FreeCatalog = sectorCount / sizeof(NEFS_CATALOG_STRUCT); + rt_copy_memory((VoidPtr) kNeFSUntitledHD, (VoidPtr) part_block->PartitionName, + rt_string_len(kNeFSUntitledHD)); - drive->fPacket.fPacketContent = fs_buf; - drive->fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); - drive->fPacket.fPacketLba = kNeFSRootCatalogStartAddress; + SizeT sectorCount = drv_get_sector_count(); + SizeT diskSize = drv_get_size(); - drive->fOutput(drive->fPacket); + part_block->Version = kNeFSVersionInteger; - (Void)(kout << "drive kind: " << drive->fProtocol() << kendl); + part_block->Kind = kNeFSPartitionTypeStandard; + part_block->StartCatalog = start + sizeof(NEFS_ROOT_PARTITION_BLOCK); + part_block->Flags = 0UL; + part_block->CatalogCount = sectorCount / sizeof(NEFS_CATALOG_STRUCT); + part_block->FreeSectors = sectorCount / sizeof(NEFS_CATALOG_STRUCT); + part_block->SectorCount = sectorCount; + part_block->DiskSize = diskSize; + part_block->FreeCatalog = sectorCount / sizeof(NEFS_CATALOG_STRUCT); - (Void)(kout << "partition name: " << part_block->PartitionName << kendl); - (Void)(kout << "start: " << hex_number(part_block->StartCatalog) << kendl); - (Void)(kout << "number of catalogs: " << hex_number(part_block->CatalogCount) << kendl); - (Void)(kout << "free catalog: " << hex_number(part_block->FreeCatalog) << kendl); - (Void)(kout << "free sectors: " << hex_number(part_block->FreeSectors) << kendl); - (Void)(kout << "sector size: " << hex_number(part_block->SectorSize) << kendl); - - // write the root catalog. - this->CreateCatalog(kNeFSRoot, 0, kNeFSCatalogKindDir); - - return true; - } + drive->fPacket.fPacketContent = fs_buf; + drive->fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); + drive->fPacket.fPacketLba = start; - kout << "partition block already exists.\r"; + drive->fOutput(drive->fPacket); - start += part_block->DiskSize; + (Void)(kout << "drive kind: " << drive->fProtocol() << kendl); - drive->fPacket.fPacketContent = fs_buf; - drive->fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); - drive->fPacket.fPacketLba = start; + (Void)(kout << "partition name: " << part_block->PartitionName << kendl); + (Void)(kout << "start: " << hex_number(part_block->StartCatalog) << kendl); + (Void)(kout << "number of catalogs: " << hex_number(part_block->CatalogCount) << kendl); + (Void)(kout << "free catalog: " << hex_number(part_block->FreeCatalog) << kendl); + (Void)(kout << "free sectors: " << hex_number(part_block->FreeSectors) << kendl); + (Void)(kout << "sector size: " << hex_number(part_block->SectorSize) << kendl); - drive->fInput(drive->fPacket); - } + // write the root catalog. + this->CreateCatalog(kNeFSRoot, 0, kNeFSCatalogKindDir); - return false; + return true; } /// @brief Writes the data fork into a specific catalog. @@ -971,6 +879,9 @@ Boolean fs_init_nefs(Void) noexcept { if (kMountpoint.A().fPacket.fPacketReadOnly == YES) ke_panic(RUNTIME_CHECK_FILESYSTEM, "Main filesystem cannot be mounted."); + NeFileSystemParser parser; + parser.Format(&kMountpoint.A(), 0, kNeFSVolumeName); + return YES; } } // namespace Kernel::NeFS diff --git a/dev/kernel/src/WideUtils.cc b/dev/kernel/src/WideUtils.cc index e6c5a533..0f628e00 100644 --- a/dev/kernel/src/WideUtils.cc +++ b/dev/kernel/src/WideUtils.cc @@ -10,8 +10,24 @@ namespace Kernel { Size wrt_string_len(const Utf16Char* str) { SizeT len{0}; - while (str[len] != 0) ++len; + while (str[len] != u'\0') ++len; return len; } + +Int wrt_copy_memory(const voidPtr src, voidPtr dst, Size len) { + Utf16Char* srcChr = reinterpret_cast(src); + Utf16Char* dstChar = reinterpret_cast(dst); + + Size index = 0; + + while (index < len) { + dstChar[index] = srcChr[index]; + ++index; + } + + dstChar[index] = 0; + + return index; +} } // namespace Kernel \ No newline at end of file -- cgit v1.2.3