diff options
| author | 0xf00sec <159052166+0xf00sec@users.noreply.github.com> | 2025-06-04 21:20:00 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2025-06-04 21:20:00 +0000 |
| commit | a0b360130206bc72741005b1a81c00ad8f75fa8b (patch) | |
| tree | 862af5c1e2356d147ceab9655dab421f772d872b /dev | |
| parent | 9828f432fe1ec678a5a78ad841b4f4d6beb3795b (diff) | |
Fx NeFS
Diffstat (limited to 'dev')
| -rw-r--r-- | dev/kernel/src/FS/NeFS+FileSystemParser.cc | 751 |
1 files changed, 360 insertions, 391 deletions
diff --git a/dev/kernel/src/FS/NeFS+FileSystemParser.cc b/dev/kernel/src/FS/NeFS+FileSystemParser.cc index dd0a1d9a..18595e9f 100644 --- a/dev/kernel/src/FS/NeFS+FileSystemParser.cc +++ b/dev/kernel/src/FS/NeFS+FileSystemParser.cc @@ -46,15 +46,35 @@ Kernel::SizeT drv_std_get_size(); /***********************************************************************************/ /// This file implements the New extended File System. -/// New extended File System implements a flat linked-list based algortihm. -/// / -/// /Path1/ /Path2/ -/// /readme.rtf /ListContents.pef /readme.lnk <-- symlink. -/// /Path1/readme.rtf +/// New extended File System implements a flat linked-list based algorithm. +/// / +/// /Path1/ /Path2/ +/// /readme.rtf /ListContents.pef /readme.lnk <-- symlink. +/// /Path1/readme.rtf /***********************************************************************************/ +static inline bool is_valid_size(SizeT size, SizeT max_size) { + return size > 0 && size <= max_size; +} -STATIC MountpointInterface kMountpoint; +static inline bool is_valid_lba(Lba lba, DriveTrait& drive) { + NEFS_ROOT_PARTITION_BLOCK part_block; + drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; + drive.fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&part_block); + drive.fInput(drive.fPacket); + + if (!drive.fPacket.fPacketGood) { + return false; + } + + // Compute the maximum LBA (DiskSize / sector size) + SizeT sectorSize = drive.fSectorSz; + Lba maxLba = part_block.DiskSize / sectorSize; + return (lba >= part_block.StartCatalog) && (lba < maxLba); +} + +STATIC MountpointInterface kMountpoint; /***********************************************************************************/ /// @brief Creates a new fork inside the New filesystem partition. /// @param catalog it's catalog @@ -62,86 +82,69 @@ STATIC MountpointInterface kMountpoint; /// @return the fork /***********************************************************************************/ _Output BOOL NeFileSystemParser::CreateFork(_Input NEFS_FORK_STRUCT& the_fork) { - if (the_fork.ForkName[0] != 0 && the_fork.CatalogName[0] != 0 && the_fork.DataSize > 0) { - auto catalog = this->GetCatalog(the_fork.CatalogName); - - if (!catalog) return NO; - - Lba lba = catalog->DataFork; - - (Void)(kout << "Fork LBA: " << hex_number(lba) << kendl); - - if (lba < kNeFSCatalogStartAddress) return NO; - - auto& drv = kMountpoint.A(); - - Lba lba_prev = lba; - - NEFS_FORK_STRUCT prev_fork; - NEFS_FORK_STRUCT cur_fork; - - /// do not check for anything. Loop until we get what we want, that is a free fork zone. - while (drv.fPacket.fPacketGood) { - drv.fPacket.fPacketLba = lba; - drv.fPacket.fPacketSize = sizeof(NEFS_FORK_STRUCT); - drv.fPacket.fPacketContent = &cur_fork; - - drv.fInput(drv.fPacket); - - (Void)(kout << "Next fork: " << hex_number(cur_fork.NextSibling) << kendl); - - if (cur_fork.Flags & kNeFSFlagCreated) { - kout << "Error: Fork does exists, not overwriting this one.\r"; - - /// sanity check. - if (KStringBuilder::Equals(cur_fork.ForkName, the_fork.ForkName) && - KStringBuilder::Equals(cur_fork.CatalogName, the_fork.CatalogName)) - break; + if (the_fork.ForkName[0] == 0 || the_fork.CatalogName[0] == 0 || the_fork.DataSize == 0) { + return NO; + } - lba_prev = lba; - lba = cur_fork.NextSibling; + auto catalog = this->GetCatalog(the_fork.CatalogName); + if (!catalog) return NO; - prev_fork = cur_fork; - } else { - /// This is a check that we have, in order to link the previous fork - /// entry. - if (lba >= kNeFSCatalogStartAddress) { - drv.fPacket.fPacketLba = lba_prev; - drv.fPacket.fPacketSize = sizeof(NEFS_FORK_STRUCT); - drv.fPacket.fPacketContent = &prev_fork; + Lba lba = catalog->DataFork; + if (lba < kNeFSCatalogStartAddress) { + delete catalog; + return NO; + } - prev_fork.NextSibling = lba; + auto& drv = kMountpoint.A(); + Lba lba_prev = lba; + NEFS_FORK_STRUCT prev_fork{}; + NEFS_FORK_STRUCT cur_fork{}; - /// write to disk. - drv.fOutput(drv.fPacket); - } + while (true) { + drv.fPacket.fPacketLba = lba; + drv.fPacket.fPacketSize = sizeof(NEFS_FORK_STRUCT); + drv.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&cur_fork); + drv.fInput(drv.fPacket); + if (!drv.fPacket.fPacketGood) { + delete catalog; + return NO; + } + if (cur_fork.Flags & kNeFSFlagCreated) { + if (KStringBuilder::Equals(cur_fork.ForkName, the_fork.ForkName) && + KStringBuilder::Equals(cur_fork.CatalogName, the_fork.CatalogName)) { break; } + lba_prev = lba; + lba = cur_fork.NextSibling; + prev_fork = cur_fork; + } else { + if (lba >= kNeFSCatalogStartAddress) { + prev_fork.NextSibling = lba; + drv.fPacket.fPacketLba = lba_prev; + drv.fPacket.fPacketSize = sizeof(NEFS_FORK_STRUCT); + drv.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&prev_fork); + drv.fOutput(drv.fPacket); + } + break; } + } - the_fork.Flags |= kNeFSFlagCreated; - the_fork.DataOffset = lba - sizeof(NEFS_FORK_STRUCT); - the_fork.PreviousSibling = lba_prev; - the_fork.NextSibling = the_fork.DataOffset - the_fork.DataSize; - - drv.fPacket.fPacketLba = lba; - drv.fPacket.fPacketSize = sizeof(NEFS_FORK_STRUCT); - drv.fPacket.fPacketContent = &the_fork; - - drv.fOutput(drv.fPacket); - - fs_ifs_write(&kMountpoint, drv, MountpointInterface::kDriveIndexA); - - /// log what we have now. - (Void)(kout << "Fork offset is at: " << hex_number(the_fork.DataOffset) << kendl); + SizeT sectorCount = (the_fork.DataSize + kNeFSSectorSz - 1) / kNeFSSectorSz; + the_fork.DataOffset = lba + 1; + the_fork.PreviousSibling = lba_prev; + the_fork.NextSibling = lba + 1 + sectorCount; + the_fork.Flags |= kNeFSFlagCreated; - (Void)(kout << "Wrote fork metadata at: " << hex_number(lba) << kendl); + drv.fPacket.fPacketLba = lba; + drv.fPacket.fPacketSize = sizeof(NEFS_FORK_STRUCT); + drv.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&the_fork); + drv.fOutput(drv.fPacket); - return YES; - } + fs_ifs_write(&kMountpoint, drv, MountpointInterface::kDriveIndexA); - return NO; + delete catalog; + return YES; } /***********************************************************************************/ @@ -153,17 +156,19 @@ _Output BOOL NeFileSystemParser::CreateFork(_Input NEFS_FORK_STRUCT& the_fork) { _Output NEFS_FORK_STRUCT* NeFileSystemParser::FindFork(_Input NEFS_CATALOG_STRUCT* catalog, _Input const Char* name, _Input Boolean is_data) { - auto& drive = kMountpoint.A(); - NEFS_FORK_STRUCT* the_fork = nullptr; + if (!catalog || !name) return nullptr; - Lba lba = is_data ? catalog->DataFork : catalog->ResourceFork; + auto& drive = kMountpoint.A(); + Lba lba = is_data ? catalog->DataFork : catalog->ResourceFork; + NEFS_FORK_STRUCT local_buf{}; - while (lba != 0) { + while (lba >= kNeFSCatalogStartAddress) { drive.fPacket.fPacketLba = lba; drive.fPacket.fPacketSize = sizeof(NEFS_FORK_STRUCT); - drive.fPacket.fPacketContent = (VoidPtr) the_fork; + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&local_buf); - rt_copy_memory((VoidPtr) "fs/nefs-packet", drive.fPacket.fPacketMime, 16); + rt_copy_memory((VoidPtr) "fs/nefs-packet", drive.fPacket.fPacketMime, + rt_string_len("fs/nefs-packet")); if (auto res = fs_ifs_read(&kMountpoint, drive, this->mDriveIndex); res) { switch (res) { @@ -176,21 +181,23 @@ _Output NEFS_FORK_STRUCT* NeFileSystemParser::FindFork(_Input NEFS_CATALOG_STRUC case 3: err_global_get() = kErrorNoSuchDisk; break; - default: break; } return nullptr; } - if (KStringBuilder::Equals(the_fork->ForkName, name)) { - break; + if (KStringBuilder::Equals(local_buf.ForkName, name) && + KStringBuilder::Equals(local_buf.CatalogName, catalog->Name)) { + auto result = new NEFS_FORK_STRUCT(); + rt_copy_memory(&local_buf, result, sizeof(NEFS_FORK_STRUCT)); + return result; } - lba = the_fork->NextSibling; + lba = local_buf.NextSibling; } - return the_fork; + return nullptr; } /***********************************************************************************/ @@ -215,18 +222,20 @@ _Output NEFS_CATALOG_STRUCT* NeFileSystemParser::CreateCatalog(_Input const Char _Input const Int32& kind) { kout << "CreateCatalog(*...*)\r"; + if (!name) return nullptr; + SizeT nameLen = rt_string_len(name); + if (nameLen == 0) return nullptr; + Lba out_lba = 0UL; kout << "Checking for path separator...\r"; /// a directory should have a slash in the end. - if (kind == kNeFSCatalogKindDir && - name[rt_string_len(name) - 1] != NeFileSystemHelper::Separator()) + if (kind == kNeFSCatalogKindDir && name[nameLen - 1] != NeFileSystemHelper::Separator()) return nullptr; /// a file shouldn't have a slash in the end. - if (kind != kNeFSCatalogKindDir && - name[rt_string_len(name) - 1] == NeFileSystemHelper::Separator()) + if (kind != kNeFSCatalogKindDir && name[nameLen - 1] == NeFileSystemHelper::Separator()) return nullptr; NEFS_CATALOG_STRUCT* catalog_copy = this->FindCatalog(name, out_lba); @@ -234,245 +243,217 @@ _Output NEFS_CATALOG_STRUCT* NeFileSystemParser::CreateCatalog(_Input const Char if (catalog_copy) { kout << "Catalog already exists: " << name << ".\r"; err_global_get() = kErrorFileExists; - delete catalog_copy; catalog_copy = nullptr; - return nullptr; } - Char* parent_name = (Char*) mm_alloc_ptr(sizeof(Char) * rt_string_len(name), Yes, No); + Char* parent_name = (Char*) mm_alloc_ptr(nameLen + 1, Yes, No); + rt_copy_memory(const_cast<Char*>(name), parent_name, nameLen + 1); - /// Locate parent catalog, to then allocate right after it. - - for (SizeT index_fill = 0; index_fill < rt_string_len(name); ++index_fill) { - parent_name[index_fill] = name[index_fill]; - } - - if (*parent_name == 0) { - kout << "Parent name is NUL.\r"; + if (nameLen < 2) { + mm_free_ptr(parent_name); err_global_get() = kErrorFileNotFound; return nullptr; } - SizeT index_reverse_copy = rt_string_len(parent_name); - - // zero character it. - parent_name[--index_reverse_copy] = 0; - - // mandatory / character, zero it. - parent_name[--index_reverse_copy] = 0; - - while (parent_name[index_reverse_copy] != NeFileSystemHelper::Separator()) { + SizeT index_reverse_copy = nameLen - 1; + if (parent_name[index_reverse_copy] == NeFileSystemHelper::Separator()) { + parent_name[index_reverse_copy] = 0; + --index_reverse_copy; + } + while (index_reverse_copy > 0 && + parent_name[index_reverse_copy] != NeFileSystemHelper::Separator()) { parent_name[index_reverse_copy] = 0; --index_reverse_copy; } + if (index_reverse_copy == 0 && parent_name[0] != NeFileSystemHelper::Separator()) { + mm_free_ptr(parent_name); + err_global_get() = kErrorFileNotFound; + return nullptr; + } NEFS_CATALOG_STRUCT* catalog = this->FindCatalog(parent_name, out_lba); - mm_free_ptr(parent_name); auto& drive = kMountpoint.A(); - if (catalog && catalog->Kind == kNeFSCatalogKindFile) { kout << "Parent is a file.\r"; delete catalog; - return nullptr; } else if (!catalog) { Char part_block[sizeof(NEFS_ROOT_PARTITION_BLOCK)] = {0}; - - drive.fPacket.fPacketContent = part_block; - drive.fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); - drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; - + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(part_block); + drive.fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); + drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; drive.fInput(drive.fPacket); NEFS_ROOT_PARTITION_BLOCK* blk_nefs = (NEFS_ROOT_PARTITION_BLOCK*) part_block; out_lba = blk_nefs->StartCatalog; } - if (drive.fPacket.fPacketReadOnly) return nullptr; + if (drive.fPacket.fPacketReadOnly) { + delete catalog; + return nullptr; + } NEFS_CATALOG_STRUCT* child_catalog = new NEFS_CATALOG_STRUCT(); - - child_catalog->Checksum = 0; - child_catalog->ResourceForkSize = 0UL; - child_catalog->DataForkSize = 0UL; - child_catalog->CatalogFlags = kNeFSStatusUnlocked; - child_catalog->NextSibling = out_lba; - child_catalog->PrevSibling = out_lba; - child_catalog->Kind = kind; + child_catalog->Checksum = 0; + child_catalog->ResourceForkSize = 0UL; + child_catalog->DataForkSize = 0UL; + child_catalog->CatalogFlags = kNeFSStatusUnlocked; + child_catalog->NextSibling = out_lba; + child_catalog->PrevSibling = out_lba; + child_catalog->Kind = kind; child_catalog->Flags |= kNeFSFlagCreated; child_catalog->CatalogFlags = flags; - SizeT i = rt_string_len(name); - - // get rid pf \0 + SizeT i = nameLen; --i; - if (kind == kNeFSCatalogKindDir) --i; - while (name[i] != '/') --i; - - rt_copy_memory((VoidPtr) (name + i), (VoidPtr) child_catalog->Name, rt_string_len(name)); + rt_copy_memory((VoidPtr) (name + i), (VoidPtr) child_catalog->Name, rt_string_len(name) - i); NEFS_CATALOG_STRUCT temporary_catalog{}; - - Lba start_free = out_lba; + Lba start_free = out_lba; rt_copy_memory((VoidPtr) "fs/nefs-packet", drive.fPacket.fPacketMime, rt_string_len("fs/nefs-packet")); Char buf_part_block[sizeof(NEFS_ROOT_PARTITION_BLOCK)] = {0}; - - drive.fPacket.fPacketContent = buf_part_block; + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(buf_part_block); drive.fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; - drive.fInput(drive.fPacket); NEFS_ROOT_PARTITION_BLOCK* part_block = (NEFS_ROOT_PARTITION_BLOCK*) buf_part_block; - - drive.fPacket.fPacketContent = &temporary_catalog; - drive.fPacket.fPacketSize = sizeof(NEFS_CATALOG_STRUCT); - drive.fPacket.fPacketLba = start_free; - + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&temporary_catalog); + drive.fPacket.fPacketSize = sizeof(NEFS_CATALOG_STRUCT); + drive.fPacket.fPacketLba = start_free; drive.fInput(drive.fPacket); if (part_block->FreeCatalog < 1) { delete child_catalog; - child_catalog = nullptr; - + delete catalog; return nullptr; } kout << "Start finding catalog to allocate or empty space...\r"; - while (start_free >= part_block->StartCatalog) { - // ========================== // - // Allocate catalog now... - // ========================== // + SizeT catalogSectors = (sizeof(NEFS_CATALOG_STRUCT) + drive.fSectorSz - 1) / drive.fSectorSz; + while (start_free < part_block->StartCatalog + (part_block->CatalogCount * catalogSectors)) { + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&temporary_catalog); + drive.fPacket.fPacketSize = sizeof(NEFS_CATALOG_STRUCT); + drive.fPacket.fPacketLba = start_free; + drive.fInput(drive.fPacket); + if ((temporary_catalog.Flags & kNeFSFlagCreated) == 0) { - child_catalog->NextSibling = start_free + sizeof(NEFS_CATALOG_STRUCT); + child_catalog->NextSibling = start_free + catalogSectors; - drive.fPacket.fPacketContent = &temporary_catalog; + NEFS_CATALOG_STRUCT placeholder{}; + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&placeholder); drive.fPacket.fPacketSize = sizeof(NEFS_CATALOG_STRUCT); drive.fPacket.fPacketLba = start_free; - drive.fOutput(drive.fPacket); child_catalog->DataFork = part_block->DiskSize - start_free; child_catalog->ResourceFork = child_catalog->DataFork; - drive.fPacket.fPacketContent = child_catalog; + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(child_catalog); drive.fPacket.fPacketSize = sizeof(NEFS_CATALOG_STRUCT); drive.fPacket.fPacketLba = start_free; - drive.fOutput(drive.fPacket); - // Get NeFS partition's block. - - drive.fPacket.fPacketContent = buf_part_block; + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(buf_part_block); drive.fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; - drive.fInput(drive.fPacket); - part_block->FreeSectors -= 1; + part_block->FreeSectors -= catalogSectors; part_block->CatalogCount += 1; part_block->FreeCatalog -= 1; + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(part_block); + drive.fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); + drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; drive.fOutput(drive.fPacket); delete catalog; - catalog = nullptr; - NEFS_CATALOG_STRUCT* found_catalog = new NEFS_CATALOG_STRUCT(); rt_copy_memory(&temporary_catalog, found_catalog, sizeof(NEFS_CATALOG_STRUCT)); + delete child_catalog; return found_catalog; } else if ((temporary_catalog.Flags & kNeFSFlagCreated) && KStringBuilder::Equals(temporary_catalog.Name, name)) { rt_copy_memory(&temporary_catalog, child_catalog, sizeof(NEFS_CATALOG_STRUCT)); - + delete catalog; return child_catalog; } - start_free = start_free + sizeof(NEFS_CATALOG_STRUCT); - - drive.fPacket.fPacketContent = &temporary_catalog; - drive.fPacket.fPacketSize = sizeof(NEFS_CATALOG_STRUCT); - drive.fPacket.fPacketLba = start_free; - - drive.fInput(drive.fPacket); + start_free += catalogSectors; } + delete child_catalog; delete catalog; return nullptr; } +/***********************************************************************************/ /// @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::Format(_Input _Output DriveTrait* drive, _Input const Int32 flags, const Char* part_name) { - if (*part_name == 0) return false; - + if (!part_name || *part_name == 0) return false; NE_UNUSED(flags); // verify disk. drive->fVerify(drive->fPacket); - rt_copy_memory((VoidPtr) "fs/nefs-packet", drive->fPacket.fPacketMime, rt_string_len("fs/nefs-packet")); - - // if disk isn't good, then error out. - if (false == drive->fPacket.fPacketGood) { + if (!drive->fPacket.fPacketGood) { err_global_get() = kErrorDiskIsCorrupted; return false; } Char fs_buf[sizeof(NEFS_ROOT_PARTITION_BLOCK)] = {0}; + Lba start = drive->fLbaStart; - Lba start = drive->fLbaStart; - - drive->fPacket.fPacketContent = fs_buf; + drive->fPacket.fPacketContent = reinterpret_cast<VoidPtr>(fs_buf); drive->fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); drive->fPacket.fPacketLba = start; - drive->fInput(drive->fPacket); NEFS_ROOT_PARTITION_BLOCK* part_block = (NEFS_ROOT_PARTITION_BLOCK*) fs_buf; - if (rt_string_cmp(kNeFSIdent, part_block->Ident, kNeFSIdentLen) == 0) return true; 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)); - SizeT sectorCount = drv_std_get_sector_count(); - SizeT diskSize = drv_std_get_size(); - - part_block->Version = kNeFSVersionInteger; + SizeT sectorCount = drv_std_get_sector_count(); + SizeT sectorSize = drive->fSectorSz; + SizeT totalBytes = sectorCount * sectorSize; + SizeT catalogEntries = totalBytes / sizeof(NEFS_CATALOG_STRUCT); + SizeT catalogSectors = (sizeof(NEFS_CATALOG_STRUCT) + sectorSize - 1) / sectorSize; + part_block->Version = kNeFSVersionInteger; part_block->Kind = kNeFSPartitionTypeStandard; - part_block->StartCatalog = start + sizeof(NEFS_CATALOG_STRUCT); + part_block->StartCatalog = start + catalogSectors; part_block->Flags = 0UL; - part_block->CatalogCount = sectorCount / sizeof(NEFS_CATALOG_STRUCT); - part_block->FreeSectors = sectorCount / sizeof(NEFS_CATALOG_STRUCT) - 1; + part_block->CatalogCount = catalogEntries; + part_block->FreeCatalog = catalogEntries - 1; part_block->SectorCount = sectorCount; - part_block->DiskSize = diskSize; - part_block->SectorSize = drive->fSectorSz; - part_block->FreeCatalog = sectorCount / sizeof(NEFS_CATALOG_STRUCT) - 1; + part_block->DiskSize = totalBytes; + part_block->SectorSize = sectorSize; + part_block->FreeSectors = sectorCount - catalogSectors; - drive->fPacket.fPacketContent = fs_buf; + drive->fPacket.fPacketContent = reinterpret_cast<VoidPtr>(fs_buf); drive->fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); drive->fPacket.fPacketLba = start; - drive->fOutput(drive->fPacket); (Void)(kout << "Drive kind: " << drive->fProtocol() << kendl); @@ -486,110 +467,108 @@ bool NeFileSystemParser::Format(_Input _Output DriveTrait* drive, _Input const I return true; } +/***********************************************************************************/ /// @brief Writes the data fork into a specific catalog. /// @param catalog the catalog itself /// @param data the data. -/// @return if the catalog w rote the contents successfully. +/// @return if the catalog wrote the contents successfully. +/***********************************************************************************/ bool NeFileSystemParser::WriteCatalog(_Input const Char* catalog_name, Bool is_rsrc_fork, _Input VoidPtr data, _Input SizeT size_of_data, _Input const Char* fork_name) { - if (size_of_data < 1) return No; + if (size_of_data < 1) return NO; - auto buf = new UInt8[size_of_data]; - rt_set_memory(buf, 0, size_of_data); + auto catalog = this->GetCatalog(catalog_name); + if (!catalog) { + kout << "NeFS: WriteCatalog failed to find catalog: " << catalog_name << "\n"; + return false; + } - rt_copy_memory(data, buf, size_of_data); + SizeT maxSize = is_rsrc_fork ? catalog->ResourceForkSize : catalog->DataForkSize; - auto& drive = kMountpoint.A(); + if (!is_valid_size(size_of_data, maxSize)) { + (Void)(kout << "NeFS: WriteCatalog called with invalid size: " << hex_number(size_of_data)); + kout << "\n"; - rt_copy_memory((VoidPtr) "fs/nefs-packet", drive.fPacket.fPacketMime, - rt_string_len("fs/nefs-packet")); + delete catalog; + return false; + } - auto catalog = this->GetCatalog(catalog_name); + Lba startFork = is_rsrc_fork ? catalog->ResourceFork : catalog->DataFork; + auto& drive = kMountpoint.A(); - if (!catalog) { - delete[] buf; - buf = nullptr; - return NO; + if (!is_valid_lba(startFork, drive)) { + (Void)(kout << "NeFS: WriteCatalog called with invalid LBA: " << hex_number(startFork)); + kout << "\n"; + + delete catalog; + return false; } - auto startFork = (!is_rsrc_fork) ? catalog->DataFork : catalog->ResourceFork; + NEFS_ROOT_PARTITION_BLOCK part_block; + drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; + drive.fPacket.fPacketSize = sizeof(part_block); + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&part_block); + drive.fInput(drive.fPacket); - delete catalog; - catalog = nullptr; + auto buf = new UInt8[size_of_data]; + rt_set_memory(buf, 0, size_of_data); + rt_copy_memory(data, buf, size_of_data); + rt_copy_memory((VoidPtr) "fs/nefs-packet", drive.fPacket.fPacketMime, + rt_string_len("fs/nefs-packet")); NEFS_FORK_STRUCT* fork_data_input = new NEFS_FORK_STRUCT(); NEFS_FORK_STRUCT prev_fork{}; - (Void)(kout << hex_number(startFork) << kendl); - - // sanity check of the fork position as the condition to run the loop. - while (startFork >= kNeFSCatalogStartAddress) { - drive.fPacket.fPacketContent = fork_data_input; + while (startFork >= part_block.StartCatalog && drive.fPacket.fPacketGood) { + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(fork_data_input); drive.fPacket.fPacketSize = sizeof(NEFS_FORK_STRUCT); drive.fPacket.fPacketLba = startFork; - drive.fInput(drive.fPacket); - (Void)(kout << hex_number(fork_data_input->DataSize) << kendl); - (Void)(kout << hex_number(size_of_data) << kendl); - (Void)(kout << hex_number(fork_data_input->Flags) << kendl); - (Void)(kout << fork_name << kendl); - (Void)(kout << fork_data_input->ForkName << kendl); - (Void)(kout << fork_data_input->CatalogName << kendl); - (Void)(kout << catalog_name << kendl); + if (!drive.fPacket.fPacketGood) { + break; + } if ((fork_data_input->Flags & kNeFSFlagCreated) && KStringBuilder::Equals(fork_data_input->ForkName, fork_name) && KStringBuilder::Equals(fork_data_input->CatalogName, catalog_name) && fork_data_input->DataSize == size_of_data) { - // ===================================================== // - // Store the blob now, into chunks. - // ===================================================== // - - auto cnt = size_of_data / kNeFSSectorSz; - auto cnter = 0UL; - auto compute_sz = kNeFSSectorSz; - - if (cnt < 1) break; - - while (compute_sz) { - drive.fPacket.fPacketContent = buf + (cnter * kNeFSSectorSz); - drive.fPacket.fPacketSize = compute_sz; - drive.fPacket.fPacketLba = fork_data_input->DataOffset; - - (Void)(kout << "data offset: " << hex_number(cnt * kNeFSSectorSz) << kendl); - + SizeT bytes_left = size_of_data; + SizeT offset = 0; + Lba base_lba = fork_data_input->DataOffset; + + while (bytes_left > 0) { + SizeT chunk = (bytes_left > kNeFSSectorSz) ? kNeFSSectorSz : bytes_left; + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(buf + offset); + drive.fPacket.fPacketSize = chunk; + drive.fPacket.fPacketLba = base_lba + (offset / kNeFSSectorSz); drive.fOutput(drive.fPacket); - - compute_sz /= (size_of_data / cnt); - ++cnter; + offset += chunk; + bytes_left -= chunk; } - (Void)(kout << "wrote data at offset: " << hex_number(fork_data_input->DataOffset) << kendl); - delete fork_data_input; delete[] buf; - + delete catalog; return true; } - // stumble upon a fork, store it. - prev_fork = *fork_data_input; - startFork = fork_data_input->NextSibling; } - delete[] buf; delete fork_data_input; - + delete[] buf; + delete catalog; return false; } +/***********************************************************************************/ /// @brief /// @param catalog_name the catalog name. /// @return the newly found catalog. +/***********************************************************************************/ _Output NEFS_CATALOG_STRUCT* NeFileSystemParser::FindCatalog(_Input const Char* catalog_name, Lba& out_lba, Bool search_hidden, Bool local_search) { @@ -600,144 +579,102 @@ _Output NEFS_CATALOG_STRUCT* NeFileSystemParser::FindCatalog(_Input const Char* rt_copy_memory((VoidPtr) "fs/nefs-packet", drive.fPacket.fPacketMime, rt_string_len("fs/nefs-packet")); - - drive.fPacket.fPacketContent = ∂ + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&part); drive.fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; - drive.fInput(drive.fPacket); auto start_catalog_lba = kNeFSCatalogStartAddress; - if (!KStringBuilder::Equals(catalog_name, NeFileSystemHelper::Root()) && local_search) { - Char parent_name[kNeFSCatalogNameLen] = {0}; + // Helper lambda to scan from a given LBA + auto scan_from = [&](Lba lba_start, Bool allow_hidden) -> NEFS_CATALOG_STRUCT* { + Lba cursor = lba_start; + NEFS_CATALOG_STRUCT tmp{}; + while (cursor >= part.StartCatalog && drive.fPacket.fPacketGood) { + drive.fPacket.fPacketLba = cursor; + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&tmp); + drive.fPacket.fPacketSize = sizeof(NEFS_CATALOG_STRUCT); + drive.fInput(drive.fPacket); - for (SizeT indexFill = 0; indexFill < rt_string_len(catalog_name); ++indexFill) { - parent_name[indexFill] = catalog_name[indexFill]; + if (KStringBuilder::Equals( + tmp.Name, catalog_name + (rt_string_len(catalog_name) - rt_string_len(tmp.Name)))) { + if (tmp.Status == kNeFSStatusLocked && !allow_hidden) { + err_global_get() = kErrorFileLocked; + return nullptr; + } + if (!(tmp.Flags & kNeFSFlagCreated)) { + err_global_get() = kErrorFileNotFound; + return nullptr; + } + NEFS_CATALOG_STRUCT* catalog_ptr = new NEFS_CATALOG_STRUCT(); + rt_copy_memory(&tmp, catalog_ptr, sizeof(NEFS_CATALOG_STRUCT)); + out_lba = cursor; + return catalog_ptr; + } + cursor = tmp.NextSibling; } + return nullptr; + }; - SizeT indexReverseCopy = rt_string_len(parent_name); - - // zero character. - parent_name[--indexReverseCopy] = 0; - - // mandatory '/' character. - parent_name[--indexReverseCopy] = 0; + if (!KStringBuilder::Equals(catalog_name, NeFileSystemHelper::Root()) && local_search) { + Char parent_name[kNeFSCatalogNameLen] = {0}; + SizeT nameLen = rt_string_len(catalog_name); + rt_copy_memory(const_cast<Char*>(catalog_name), parent_name, nameLen + 1); - while (parent_name[indexReverseCopy] != NeFileSystemHelper::Separator()) { + SizeT indexReverseCopy = nameLen - 1; + if (parent_name[indexReverseCopy] == NeFileSystemHelper::Separator()) { parent_name[indexReverseCopy] = 0; --indexReverseCopy; } - - NEFS_CATALOG_STRUCT* parent_catalog = this->FindCatalog(parent_name, out_lba); - - if (parent_catalog && !KStringBuilder::Equals(parent_name, NeFileSystemHelper::Root())) { - start_catalog_lba = parent_catalog->NextSibling; - - delete parent_catalog; - parent_catalog = nullptr; - - local_search = YES; - } else if (parent_catalog) { - start_catalog_lba = parent_catalog->NextSibling; - - local_search = YES; - - delete parent_catalog; - parent_catalog = nullptr; - } else if (!parent_catalog) { + while (indexReverseCopy > 0 && + parent_name[indexReverseCopy] != NeFileSystemHelper::Separator()) { + parent_name[indexReverseCopy] = 0; + --indexReverseCopy; + } + if (indexReverseCopy == 0 && parent_name[0] != NeFileSystemHelper::Separator()) { return nullptr; } - } - NEFS_CATALOG_STRUCT temporary_catalog{}; - - SizeT i = rt_string_len(catalog_name); - - // get rid of \0 - --i; - - if (catalog_name[i] == '/') --i; - - while (catalog_name[i] != '/') --i; - - const Char* tmp_name = (catalog_name + i); - -kNeFSSearchThroughCatalogList: - while (drive.fPacket.fPacketGood) { - drive.fPacket.fPacketLba = start_catalog_lba; - drive.fPacket.fPacketContent = &temporary_catalog; - drive.fPacket.fPacketSize = sizeof(NEFS_CATALOG_STRUCT); - - drive.fInput(drive.fPacket); - - if (KStringBuilder::Equals(tmp_name, temporary_catalog.Name)) { - if (temporary_catalog.Status == kNeFSStatusLocked && !search_hidden) { - err_global_get() = kErrorFileLocked; - - goto NeFSContinueSearch; - } - - /// ignore unallocated catalog, break - if (!(temporary_catalog.Flags & kNeFSFlagCreated)) { - err_global_get() = kErrorFileNotFound; - - goto NeFSContinueSearch; - } - - (Void)(kout << "Found available catalog at: " << hex_number(start_catalog_lba) << kendl); - (Void)(kout << "Found available catalog at: " << temporary_catalog.Name << kendl); - - NEFS_CATALOG_STRUCT* catalog_ptr = new NEFS_CATALOG_STRUCT(); - rt_copy_memory(&temporary_catalog, catalog_ptr, sizeof(NEFS_CATALOG_STRUCT)); - - out_lba = start_catalog_lba; - return catalog_ptr; + NEFS_CATALOG_STRUCT* parent_catalog = + this->FindCatalog(parent_name, out_lba, search_hidden, NO); + if (parent_catalog) { + start_catalog_lba = parent_catalog->NextSibling; + delete parent_catalog; + NEFS_CATALOG_STRUCT* found = scan_from(start_catalog_lba, search_hidden); + if (found) return found; } - - NeFSContinueSearch: - start_catalog_lba = temporary_catalog.NextSibling; - - if (start_catalog_lba < part.StartCatalog) break; - } - - if (local_search) { - local_search = false; - start_catalog_lba = part.StartCatalog; - - goto kNeFSSearchThroughCatalogList; } - err_global_get() = kErrorFileNotFound; - - out_lba = 0UL; - - return nullptr; + return scan_from(part.StartCatalog, search_hidden); } +/***********************************************************************************/ /// @brief Get catalog from filesystem. /// @param name the catalog's name/ /// @return +/***********************************************************************************/ _Output NEFS_CATALOG_STRUCT* NeFileSystemParser::GetCatalog(_Input const Char* name) { Lba unused = 0; - return this->FindCatalog(name, unused, YES); + return this->FindCatalog(name, unused, YES, YES); } +/***********************************************************************************/ /// @brief Closes a catalog, (frees it). /// @param catalog the catalog to close. /// @return +/***********************************************************************************/ _Output Boolean NeFileSystemParser::CloseCatalog(_Input _Output NEFS_CATALOG_STRUCT* catalog) { if (!catalog) return false; - delete catalog; catalog = nullptr; - return true; } +/***********************************************************************************/ /// @brief Mark catalog as removed. /// @param catalog The catalog structure. /// @return if the catalog was removed or not. +/***********************************************************************************/ _Output Boolean NeFileSystemParser::RemoveCatalog(_Input const Char* catalog_name) { if (!catalog_name || KStringBuilder::Equals(catalog_name, NeFileSystemHelper::Root())) { err_global_get() = kErrorInternal; @@ -745,46 +682,80 @@ _Output Boolean NeFileSystemParser::RemoveCatalog(_Input const Char* catalog_nam } Lba out_lba = 0; - auto catalog = this->FindCatalog(catalog_name, out_lba); + auto catalog = this->FindCatalog(catalog_name, out_lba, YES, YES); + if (!catalog) return false; + + auto& drive = kMountpoint.A(); + NEFS_FORK_STRUCT fork_buf{}; + Lba fork_lba = catalog->DataFork; + while (fork_lba >= kNeFSCatalogStartAddress) { + drive.fPacket.fPacketLba = fork_lba; + drive.fPacket.fPacketSize = sizeof(fork_buf); + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&fork_buf); + drive.fInput(drive.fPacket); + + fork_buf.Flags &= (~kNeFSFlagCreated); + fork_buf.Flags |= kNeFSFlagDeleted; + + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&fork_buf); + drive.fPacket.fPacketSize = sizeof(fork_buf); + drive.fPacket.fPacketLba = fork_lba; + drive.fOutput(drive.fPacket); + + fork_lba = fork_buf.NextSibling; + } + + fork_lba = catalog->ResourceFork; + while (fork_lba >= kNeFSCatalogStartAddress) { + drive.fPacket.fPacketLba = fork_lba; + drive.fPacket.fPacketSize = sizeof(fork_buf); + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&fork_buf); + drive.fInput(drive.fPacket); + + fork_buf.Flags &= (~kNeFSFlagCreated); + fork_buf.Flags |= kNeFSFlagDeleted; + + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(&fork_buf); + drive.fPacket.fPacketSize = sizeof(fork_buf); + drive.fPacket.fPacketLba = fork_lba; + drive.fOutput(drive.fPacket); + + fork_lba = fork_buf.NextSibling; + } - if (out_lba >= kNeFSCatalogStartAddress || catalog->Flags & kNeFSFlagCreated) { + if (out_lba >= kNeFSCatalogStartAddress || (catalog->Flags & kNeFSFlagCreated)) { catalog->Flags &= (~kNeFSFlagCreated); catalog->Flags |= kNeFSFlagDeleted; - auto& drive = kMountpoint.A(); - rt_copy_memory((VoidPtr) "fs/nefs-packet", drive.fPacket.fPacketMime, rt_string_len("fs/nefs-packet")); - - drive.fPacket.fPacketLba = out_lba; // the catalog position. - drive.fPacket.fPacketSize = - sizeof(NEFS_CATALOG_STRUCT); // size of catalog. roughly the sector size. - drive.fPacket.fPacketContent = catalog; // the catalog itself. - - drive.fOutput(drive.fPacket); // send packet. + drive.fPacket.fPacketLba = out_lba; + drive.fPacket.fPacketSize = sizeof(NEFS_CATALOG_STRUCT); + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(catalog); + drive.fOutput(drive.fPacket); Char partitionBlockBuf[sizeof(NEFS_ROOT_PARTITION_BLOCK)] = {0}; - - drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; - drive.fPacket.fPacketContent = partitionBlockBuf; + drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(partitionBlockBuf); drive.fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); - drive.fInput(drive.fPacket); - NEFS_ROOT_PARTITION_BLOCK* part_block = - reinterpret_cast<NEFS_ROOT_PARTITION_BLOCK*>(partitionBlockBuf); - - --part_block->CatalogCount; + NEFS_ROOT_PARTITION_BLOCK* part_block = (NEFS_ROOT_PARTITION_BLOCK*) partitionBlockBuf; + if (part_block->CatalogCount > 0) --part_block->CatalogCount; ++part_block->FreeSectors; + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(part_block); + drive.fPacket.fPacketSize = sizeof(NEFS_ROOT_PARTITION_BLOCK); + drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; drive.fOutput(drive.fPacket); + delete catalog; + catalog = nullptr; return true; } delete catalog; catalog = nullptr; - return false; } @@ -798,7 +769,6 @@ _Output Boolean NeFileSystemParser::RemoveCatalog(_Input const Char* catalog_nam /// @param dataSz /// @return /***********************************************************************************/ - VoidPtr NeFileSystemParser::ReadCatalog(_Input _Output NEFS_CATALOG_STRUCT* catalog, _Input Bool is_rsrc_fork, _Input SizeT dataSz, _Input const Char* forkName) { @@ -806,35 +776,43 @@ VoidPtr NeFileSystemParser::ReadCatalog(_Input _Output NEFS_CATALOG_STRUCT* cata err_global_get() = kErrorInvalidData; return nullptr; } + // Validate size against fork size + SizeT maxSize = is_rsrc_fork ? catalog->ResourceForkSize : catalog->DataForkSize; + if (!is_valid_size(dataSz, maxSize)) { + kout << "NeFS: ReadCatalog called with invalid size: "; + hex_number(dataSz); + kout << "\n"; + return nullptr; + } - NE_UNUSED(dataSz); - - Lba dataForkLba = (!is_rsrc_fork) ? catalog->DataFork : catalog->ResourceFork; - - NEFS_FORK_STRUCT* fs_buf = new NEFS_FORK_STRUCT(); - auto& drive = kMountpoint.A(); + Lba dataForkLba = is_rsrc_fork ? catalog->ResourceFork : catalog->DataFork; + auto& drive = kMountpoint.A(); + if (!is_valid_lba(dataForkLba, drive)) { + kout << "NeFS: ReadCatalog called with invalid LBA: "; + hex_number(dataForkLba); + kout << "\n"; + return nullptr; + } + auto* fs_buf = new NEFS_FORK_STRUCT(); rt_copy_memory((VoidPtr) "fs/nefs-packet", drive.fPacket.fPacketMime, rt_string_len("fs/nefs-packet")); NEFS_FORK_STRUCT* fs_fork_data = nullptr; - - while (dataForkLba > kNeFSCatalogStartAddress) { + while (dataForkLba >= kNeFSCatalogStartAddress) { drive.fPacket.fPacketLba = dataForkLba; drive.fPacket.fPacketSize = sizeof(NEFS_FORK_STRUCT); - drive.fPacket.fPacketContent = fs_buf; - + drive.fPacket.fPacketContent = reinterpret_cast<VoidPtr>(fs_buf); drive.fInput(drive.fPacket); fs_fork_data = fs_buf; - (Void)(kout << "ForkName: " << fs_fork_data->ForkName << kendl); (Void)(kout << "CatalogName: " << fs_fork_data->CatalogName << kendl); if (KStringBuilder::Equals(forkName, fs_fork_data->ForkName) && - KStringBuilder::Equals(catalog->Name, fs_fork_data->CatalogName)) + KStringBuilder::Equals(catalog->Name, fs_fork_data->CatalogName)) { break; - + } dataForkLba = fs_fork_data->NextSibling; } @@ -842,7 +820,6 @@ VoidPtr NeFileSystemParser::ReadCatalog(_Input _Output NEFS_CATALOG_STRUCT* cata delete fs_buf; return nullptr; } - return fs_fork_data; } @@ -852,11 +829,9 @@ VoidPtr NeFileSystemParser::ReadCatalog(_Input _Output NEFS_CATALOG_STRUCT* cata /// @param off where to seek. /// @return if the seeking was successful. /***********************************************************************************/ - bool NeFileSystemParser::Seek(_Input _Output NEFS_CATALOG_STRUCT* catalog, SizeT off) { NE_UNUSED(catalog); NE_UNUSED(off); - err_global_get() = kErrorUnimplemented; return false; } @@ -866,10 +841,8 @@ bool NeFileSystemParser::Seek(_Input _Output NEFS_CATALOG_STRUCT* catalog, SizeT /// @param catalog /// @return The position on the file. /***********************************************************************************/ - SizeT NeFileSystemParser::Tell(_Input _Output NEFS_CATALOG_STRUCT* catalog) { NE_UNUSED(catalog); - err_global_get() = kErrorUnimplemented; return 0; } @@ -880,14 +853,10 @@ namespace Kernel::NeFS { /***********************************************************************************/ Boolean fs_init_nefs(Void) noexcept { kout << "Creating HeFS disk...\r"; - kMountpoint.A() = io_construct_main_drive(); - if (kMountpoint.A().fPacket.fPacketReadOnly == YES) ke_panic(RUNTIME_CHECK_FILESYSTEM, "Main disk cannot be mounted."); - NeFileSystemParser parser; - return parser.Format(&kMountpoint.A(), 0, kNeFSVolumeName); } } // namespace Kernel::NeFS |
