From 77423c4fa55a060c5cf710bbe5c4de02fce43e97 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Mon, 29 Apr 2024 20:20:57 +0200 Subject: MHR-18: Fixing last bugs... Signed-off-by: Amlal El Mahrouss --- Private/NewBoot/Source/HEL/AMD64/BootATA.cxx | 2 +- .../NewBoot/Source/HEL/AMD64/BootFileReader.cxx | 10 ++++---- Private/NewBoot/Source/HEL/AMD64/BootMain.cxx | 6 ++--- .../NewBoot/Source/HEL/AMD64/BootTextWriter.cxx | 30 +++++++++++++++++++--- 4 files changed, 36 insertions(+), 12 deletions(-) (limited to 'Private/NewBoot/Source') diff --git a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx index d6b5542c..edfbaed8 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootATA.cxx @@ -73,7 +73,7 @@ ATAInit_Retry: if (statRdy & ATA_SR_ERR) { writer.Write( - L"New Boot: ATA: Select error, not an IDE based hard-drive.\r\n"); + L"New Boot: ATA: Select error, not an IDE based hard-drive.\r"); return false; } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootFileReader.cxx b/Private/NewBoot/Source/HEL/AMD64/BootFileReader.cxx index b2d728ae..e01d0b96 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootFileReader.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootFileReader.cxx @@ -52,12 +52,12 @@ BFileReader::BFileReader(const CharacterTypeUTF16* path, EfiGUID guidImg = EfiGUID(EFI_LOADED_IMAGE_PROTOCOL_GUID); if (BS->HandleProtocol(ImageHandle, &guidImg, (void**)&img) != kEfiOk) { - mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Protocol").Write(L"\r\n"); + mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Protocol").Write(L"\r"); this->mErrorCode = kNotSupported; } if (BS->HandleProtocol(img->DeviceHandle, &guidEfp, (void**)&efp) != kEfiOk) { - mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Protocol").Write(L"\r\n"); + mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Protocol").Write(L"\r"); this->mErrorCode = kNotSupported; return; } @@ -65,7 +65,7 @@ BFileReader::BFileReader(const CharacterTypeUTF16* path, /// Start doing disk I/O if (efp->OpenVolume(efp, &rootFs) != kEfiOk) { - mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Volume").Write(L"\r\n"); + mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Volume").Write(L"\r"); EFI::ThrowError(L"NoSuchVolume", L"No Such volume."); this->mErrorCode = kNotSupported; return; @@ -77,7 +77,7 @@ BFileReader::BFileReader(const CharacterTypeUTF16* path, kEfiOk) { mWriter.Write(L"New Boot: Fetch-Protocol: No-Such-Path: ") .Write(mPath) - .Write(L"\r\n"); + .Write(L"\r"); EFI::ThrowError(L"NoSuchPath", L"No Such file on filesystem."); this->mErrorCode = kNotSupported; return; @@ -109,7 +109,7 @@ Void BFileReader::ReadAll(SizeT until, SizeT chunk) { if (mBlob == nullptr) { if (auto err = BS->AllocatePool(EfiLoaderCode, until, (VoidPtr*)&mBlob) != kEfiOk) { - mWriter.Write(L"*** EFI-Code: ").Write(err).Write(L" ***\r\n"); + mWriter.Write(L"*** EFI-Code: ").Write(err).Write(L" ***\r"); EFI::ThrowError(L"OutOfMemory", L"Allocation error."); } } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx index 96999df8..1462115a 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootMain.cxx @@ -65,9 +65,9 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, writer.Write(L"Mahrouss-Logic (R) New Boot: ") .Write(BVersionString::Shared()); - writer.Write(L"\r\nNew Boot: Firmware Vendor: ") + writer.Write(L"\rNew Boot: Firmware Vendor: ") .Write(SystemTable->FirmwareVendor) - .Write(L"\r\n"); + .Write(L"\r"); UInt32* MapKey = new UInt32(); UInt32* SizePtr = new UInt32(); @@ -151,7 +151,7 @@ EFI_EXTERN_C EFI_API Int Main(EfiHandlePtr ImageHandle, writer.WriteCharacter(buf[i]); } } else - writer.Write(L"\r\n"); + writer.Write(L"\r"); } } diff --git a/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx b/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx index fc91a02a..2a92ce6d 100644 --- a/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx +++ b/Private/NewBoot/Source/HEL/AMD64/BootTextWriter.cxx @@ -26,12 +26,28 @@ BTextWriter &BTextWriter::Write(const CharacterTypeUTF16 *str) { #ifdef __DEBUG__ if (!str || *str == 0) return *this; - ST->ConOut->OutputString(ST->ConOut, str); + CharacterTypeUTF16 strTmp[2]; + strTmp[1] = 0; + + for (size_t i = 0; str[i] != 0; i++) { + if (str[i] == '\r') { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + + strTmp[0] = '\n'; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } else { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } + } #endif // ifdef __DEBUG__ return *this; } +/// @brief UTF-8 equivalent of Write (UTF-16). +/// @param str the input string. BTextWriter &BTextWriter::Write(const Char *str) { #ifdef __DEBUG__ if (!str || *str == 0) return *this; @@ -40,8 +56,16 @@ BTextWriter &BTextWriter::Write(const Char *str) { strTmp[1] = 0; for (size_t i = 0; str[i] != 0; i++) { - strTmp[0] = str[i]; - ST->ConOut->OutputString(ST->ConOut, strTmp); + if (str[i] == '\r') { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + + strTmp[0] = '\n'; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } else { + strTmp[0] = str[i]; + ST->ConOut->OutputString(ST->ConOut, strTmp); + } } #endif // ifdef __DEBUG__ -- cgit v1.2.3 From f86125cf437fb49587d588413d69646959a58541 Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Tue, 30 Apr 2024 08:15:54 +0200 Subject: MHR-18: Quality assurance of the filesystem. - File/Directory removal and creation. - Read/Write support. - Sanitu check and catalog validation. Signed-off-by: Amlal El Mahrouss --- Private/FSKit/NewFS.hxx | 10 ++- Private/NewBoot/Source/makefile | 2 +- Private/NewKit/Utils.hpp | 2 +- Private/Source/AppMain.cxx | 61 +++++--------- Private/Source/FS/NewFS.cxx | 176 ++++++++++++++++++++++------------------ Private/Source/FileManager.cxx | 2 +- Private/Source/Utils.cxx | 2 +- 7 files changed, 130 insertions(+), 125 deletions(-) (limited to 'Private/NewBoot/Source') diff --git a/Private/FSKit/NewFS.hxx b/Private/FSKit/NewFS.hxx index 2a499ab7..b461666d 100644 --- a/Private/FSKit/NewFS.hxx +++ b/Private/FSKit/NewFS.hxx @@ -43,8 +43,8 @@ default. /// @brief Partition GUID on EPM and GPT disks. #define kNewFSUUID "@{DD997393-9CCE-4288-A8D5-C0FDE3908DBE}" -#define kNewFSVersionInteger 0x123 -#define kNewFSVerionString "1.23" +#define kNewFSVersionInteger 0x124 +#define kNewFSVerionString "1.24" /// @brief Standard fork types. #define kNewFSDataFork "data" @@ -115,7 +115,9 @@ struct PACKED NewCatalog final { NewCharType Mime[kNewFSMimeNameLen]; /// Catalog status flag. - NewOS::Int32 Flags; + NewOS::UInt16 Flags; + /// Custom catalog flags. + NewOS::UInt16 FileFlags; /// Catalog kind. NewOS::Int32 Kind; @@ -237,7 +239,7 @@ class NewFSParser final { bool WriteCatalog(_Input _Output NewCatalog* catalog, voidPtr data, SizeT sizeOfData); - VoidPtr ReadCatalog(_Input _Output const char* catalogName, + VoidPtr ReadCatalog(_Input _Output NewCatalog* catalog, SizeT dataSz); bool Seek(_Input _Output NewCatalog* catalog, SizeT off); diff --git a/Private/NewBoot/Source/makefile b/Private/NewBoot/Source/makefile index 9018623f..df913031 100644 --- a/Private/NewBoot/Source/makefile +++ b/Private/NewBoot/Source/makefile @@ -75,7 +75,7 @@ run-efi-amd64: .PHONY: epm-img epm-img: - qemu-img create -f raw $(IMG) 1G + qemu-img create -f raw $(IMG) 4G qemu-img create -f raw $(IMG_2) 512M .PHONY: download-edk diff --git a/Private/NewKit/Utils.hpp b/Private/NewKit/Utils.hpp index 749e15ba..c9503479 100644 --- a/Private/NewKit/Utils.hpp +++ b/Private/NewKit/Utils.hpp @@ -24,5 +24,5 @@ Boolean is_space(Char chr); 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_frofchar(char *str, const char chr); +char *rt_string_has_char(char *str, const char chr); } // namespace NewOS diff --git a/Private/Source/AppMain.cxx b/Private/Source/AppMain.cxx index 8fb9a4ba..966172df 100644 --- a/Private/Source/AppMain.cxx +++ b/Private/Source/AppMain.cxx @@ -26,55 +26,36 @@ EXTERN_C NewOS::Void AppMain(NewOS::Void) { NewOS::FilesystemManagerInterface::Mount(newFS); if (newFS->GetImpl()) { - NewCatalog* mountCatalog = newFS->GetImpl()->GetCatalog("/Boot/"); + NewCatalog* textCatalog = nullptr; - if (mountCatalog) { - delete newFS->GetImpl()->CreateCatalog("/Boot/System/", 0, - kNewFSCatalogKindDir); - NewCatalog* newKernelCatalog = - newFS->GetImpl()->CreateCatalog("/Boot/System/ExampleTextFile"); + if (!newFS->GetImpl()->GetCatalog("/EditableText")) { + constexpr auto cDataSz = 512; + NewOS::Char theData[cDataSz] = { + "About NewKernel...\rNewKernel is the System behind " + "NewOS.\rFeaturing modern common features, yet innovative.\r"}; - NewOS::kcout << NewOS::hex_number(newKernelCatalog->Flags) << NewOS::endl; + NewFork theFork{0}; - if (newKernelCatalog) - NewOS::kcout << "Catalog-Path-Name: " << newKernelCatalog->Name - << NewOS::endl; + NewOS::rt_copy_memory((NewOS::VoidPtr) "EditableText", + (NewOS::VoidPtr)theFork.Name, + NewOS::rt_string_len("EditableText")); - NewOS::kcout << "Catalog-Path-Name: " << mountCatalog->Name - << NewOS::endl; + theFork.Kind = NewOS::kNewFSDataForkKind; + theFork.DataSize = cDataSz; - constexpr auto cDataSz = 512; - NewOS::Char theData[cDataSz] = { - "About NewKernel...\rNewKernel is the System behind " - "NewOS.\rFeaturing modern common features, yet innovative.\r"}; + textCatalog = newFS->GetImpl()->CreateCatalog("/EditableText"); - NewFork theFork{0}; - NewOS::rt_copy_memory((NewOS::VoidPtr) "EditableText", - (NewOS::VoidPtr)theFork.Name, - NewOS::rt_string_len("EditableText")); - - theFork.Kind = NewOS::kNewFSDataForkKind; - theFork.DataSize = cDataSz; - - newFS->GetImpl()->CreateFork(newKernelCatalog, theFork); - newFS->GetImpl()->WriteCatalog(newKernelCatalog, theData, cDataSz); - - //newFS->GetImpl()->RemoveCatalog("/Boot/System/ExampleTextFile"); - - - - char* buf = nullptr; + newFS->GetImpl()->CreateFork(textCatalog, theFork); + newFS->GetImpl()->WriteCatalog(textCatalog, theData, cDataSz); + } else { + NewOS::kcout << "Catalog already exists.\r"; + } - buf = - (NewOS::Char*)newFS->GetImpl()->ReadCatalog("/Boot/System/ExampleTextFile", 512); + char* buf = nullptr; - NewOS::kcout << buf << NewOS::endl; + buf = (NewOS::Char*)newFS->GetImpl()->ReadCatalog(newFS->GetImpl()->GetCatalog("/EditableText"), 512); - delete newKernelCatalog; - delete mountCatalog; - } else { - delete newFS->GetImpl()->CreateCatalog("/Boot/", 0, kNewFSCatalogKindDir); - } + NewOS::kcout << buf << NewOS::endl; } while (NewOS::ProcessScheduler::Shared().Leak().Run() > 0) diff --git a/Private/Source/FS/NewFS.cxx b/Private/Source/FS/NewFS.cxx index fe208389..2a85d394 100644 --- a/Private/Source/FS/NewFS.cxx +++ b/Private/Source/FS/NewFS.cxx @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -24,41 +25,32 @@ STATIC MountpointInterface sMountpointInterface; /// @return the fork _Output NewFork* NewFSParser::CreateFork(_Input NewCatalog* catalog, _Input NewFork& theFork) { + if (!sMountpointInterface.GetAddressOf(this->fDriveIndex)) return nullptr; + if (catalog && theFork.Name[0] != 0 && theFork.DataSize > 0) { - theFork.Flags = kNewFSFlagCreated; Lba lba = (theFork.Kind == kNewFSDataForkKind) ? catalog->DataFork : catalog->ResourceFork; - if (lba <= kNewFSCatalogStartAddress) return nullptr; + kcout << "Data-Fork-LBA: " << hex_number(lba) << endl; - theFork.DataOffset = lba + sizeof(NewCatalog) + sizeof(NewFork); + if (lba <= kNewFSCatalogStartAddress) return nullptr; - if (!sMountpointInterface.GetAddressOf(this->fDriveIndex)) return nullptr; + theFork.DataOffset = lba - sizeof(NewFork); auto drv = sMountpointInterface.GetAddressOf(this->fDriveIndex); - Char cpyFork[sizeof(NewFork)] = {0}; - - drv->fPacket.fLba = lba + sizeof(NewCatalog); - drv->fPacket.fPacketSize = sizeof(NewFork); - drv->fPacket.fPacketContent = (VoidPtr)cpyFork; - - drv->fInput(&drv->fPacket); - - NewFork* cpyForkStruct = (NewFork*)cpyFork; - - if (cpyForkStruct->Flags == kNewFSFlagCreated) return nullptr; - /// special treatment. rt_copy_memory((VoidPtr) "fs/newfs-packet", drv->fPacket.fPacketMime, rt_string_len("fs/newfs-packet")); drv->fPacket.fLba = lba; drv->fPacket.fPacketSize = sizeof(NewFork); - drv->fPacket.fPacketContent = (VoidPtr)&theFork; + drv->fPacket.fPacketContent = &theFork; drv->fOutput(&drv->fPacket); + kcout << "New OS: Wrote fork data at: " << hex_number(theFork.DataOffset) << endl; + return &theFork; } @@ -74,6 +66,7 @@ _Output NewFork* NewFSParser::FindFork(_Input NewCatalog* catalog, Boolean isDataFork) { auto drv = sMountpointInterface.GetAddressOf(this->fDriveIndex); NewFork* theFork = nullptr; + Lba lba = isDataFork ? catalog->DataFork : catalog->ResourceFork; while (lba != 0) { @@ -147,35 +140,35 @@ _Output NewCatalog* NewFSParser::CreateCatalog(_Input const char* name, return checkForCpy; } - char copyName[kNewFSNodeNameLen] = {0}; + char parentName[kNewFSNodeNameLen] = {0}; for (SizeT indexName = 0UL; indexName < rt_string_len(name); ++indexName) { - copyName[indexName] = name[indexName]; + parentName[indexName] = name[indexName]; } - if (*copyName == 0) { + if (*parentName == 0) { DbgLastError() = kErrorFileNotFound; return nullptr; } for (SizeT indexFill = 0; indexFill < rt_string_len(name); ++indexFill) { - copyName[indexFill] = name[indexFill]; + parentName[indexFill] = name[indexFill]; } - SizeT indexReverseCopy = rt_string_len(copyName); + SizeT indexReverseCopy = rt_string_len(parentName); // zero character. - copyName[--indexReverseCopy] = 0; + parentName[--indexReverseCopy] = 0; // mandatory / character. - copyName[--indexReverseCopy] = 0; + parentName[--indexReverseCopy] = 0; - while (copyName[indexReverseCopy] != '/') { - copyName[indexReverseCopy] = 0; + while (parentName[indexReverseCopy] != '/') { + parentName[indexReverseCopy] = 0; --indexReverseCopy; } - NewCatalog* catalog = this->FindCatalog(copyName, outLba); + NewCatalog* catalog = this->FindCatalog(parentName, outLba); if (!catalog) { return nullptr; @@ -186,7 +179,7 @@ _Output NewCatalog* NewFSParser::CreateCatalog(_Input const char* name, return nullptr; } - constexpr SizeT cDefaultForkSize = 8192; + constexpr SizeT cDefaultForkSize = 8196; NewCatalog* catalogChild = new NewCatalog(); @@ -201,22 +194,27 @@ _Output NewCatalog* NewFSParser::CreateCatalog(_Input const char* name, rt_copy_memory((VoidPtr)name, (VoidPtr)catalogChild->Name, rt_string_len(name)); - UInt16 sectorBuf[kNewFSMinimumSectorSz] = {0}; + UInt16 catalogBuf[kNewFSMinimumSectorSz] = {0}; auto drive = sMountpointInterface.GetAddressOf(this->fDriveIndex); Lba startFree = catalogChild->PrevSibling + catalog->NextSibling; - kcout << "Next-Catalog: " << hex_number(startFree) << endl; + kcout << "Free-Catalog: " << hex_number(startFree) << endl; + kcout << "New-Catalog: " << name << endl; + kcout << "Parent-Catalog: " << parentName << endl; - drive->fPacket.fPacketContent = sectorBuf; + rt_copy_memory((VoidPtr) "fs/newfs-packet", drive->fPacket.fPacketMime, + rt_string_len("fs/newfs-packet")); + + drive->fPacket.fPacketContent = catalogBuf; drive->fPacket.fPacketSize = kNewFSMinimumSectorSz; drive->fPacket.fLba = startFree; drive->fInput(&drive->fPacket); while (drive->fPacket.fPacketGood) { - NewCatalog* nextSibling = (NewCatalog*)sectorBuf; + NewCatalog* nextSibling = (NewCatalog*)catalogBuf; if (startFree <= kNewFSAddressAsLba) { delete catalogChild; @@ -226,10 +224,23 @@ _Output NewCatalog* NewFSParser::CreateCatalog(_Input const char* name, } /// allocation or reallocation or catalog... - if (nextSibling->Name[0] == 0 && nextSibling->Flags != kNewFSFlagCreated) { - catalogChild->DataFork = startFree + sizeof(NewCatalog); - catalogChild->ResourceFork = - startFree + sizeof(NewCatalog) + catalogChild->DataForkSize; + if (nextSibling->Flags != kNewFSFlagCreated) { + Char sectorBufPartBlock[kNewFSMinimumSectorSz] = {0}; + + drive->fPacket.fPacketContent = sectorBufPartBlock; + drive->fPacket.fPacketSize = kNewFSMinimumSectorSz; + drive->fPacket.fLba = kNewFSAddressAsLba; + + drive->fInput(&drive->fPacket); + + NewPartitionBlock* partBlock = (NewPartitionBlock*)sectorBufPartBlock; + + catalogChild->DataFork = + partBlock->DiskSize - startFree - sizeof(NewCatalog); + + kcout << "DataFork: " << hex_number(catalogChild->DataFork) << endl; + + catalogChild->ResourceFork = catalogChild->DataFork; if (catalogChild->Kind == kNewFSCatalogKindFile) { rt_copy_memory((VoidPtr) "x-kind/file", (VoidPtr)catalogChild->Mime, @@ -243,13 +254,16 @@ _Output NewCatalog* NewFSParser::CreateCatalog(_Input const char* name, catalogChild->DataForkSize + catalogChild->ResourceForkOverallSize; - drive->fPacket.fPacketContent = catalogChild; + rt_copy_memory(catalogChild, catalogBuf, sizeof(NewCatalog)); + + drive->fPacket.fPacketContent = catalogBuf; drive->fPacket.fPacketSize = sizeof(NewCatalog); drive->fPacket.fLba = startFree; drive->fOutput(&drive->fPacket); - kcout << "New OS: Create new catalog successfully!\r"; + kcout << "New OS: Create new catalog, status: " + << hex_number(nextSibling->Flags) << endl; Char sectBuf[sizeof(NewCatalog)] = {0}; @@ -266,16 +280,12 @@ _Output NewCatalog* NewFSParser::CreateCatalog(_Input const char* name, kcout << "Edit-Catalog: " << prevCatalog->Name << endl; - Char sectorBufPartBlock[kNewFSMinimumSectorSz] = {0}; - drive->fPacket.fPacketContent = sectorBufPartBlock; drive->fPacket.fPacketSize = kNewFSMinimumSectorSz; drive->fPacket.fLba = kNewFSAddressAsLba; drive->fInput(&drive->fPacket); - NewPartitionBlock* partBlock = (NewPartitionBlock*)sectorBufPartBlock; - partBlock->SectorCount -= 1; partBlock->CatalogCount += 1; partBlock->FreeCatalog -= 1; @@ -287,11 +297,9 @@ _Output NewCatalog* NewFSParser::CreateCatalog(_Input const char* name, } //// @note that's how we find the next catalog in the partition block. - startFree += sizeof(NewFork) + sizeof(NewCatalog) + - nextSibling->DataForkSize + - nextSibling->ResourceForkOverallSize; + startFree += sizeof(NewFork) + sizeof(NewCatalog) + cDefaultForkSize; - drive->fPacket.fPacketContent = sectorBuf; + drive->fPacket.fPacketContent = catalogBuf; drive->fPacket.fPacketSize = kNewFSMinimumSectorSz; drive->fPacket.fLba = startFree; @@ -309,6 +317,9 @@ bool NewFSParser::Format(_Input _Output DriveTrait* drive) { /// verify disk. drive->fVerify(&drive->fPacket); + rt_copy_memory((VoidPtr) "fs/newfs-packet", drive->fPacket.fPacketMime, + rt_string_len("fs/newfs-packet")); + /// if disk isn't good, then error out. if (false == drive->fPacket.fPacketGood) { DbgLastError() = kErrorDiskIsCorrupted; @@ -348,7 +359,7 @@ bool NewFSParser::Format(_Input _Output DriveTrait* drive) { partBlock->Kind = kNewFSPartitionTypeStandard; partBlock->StartCatalog = kNewFSCatalogStartAddress; - partBlock->Flags |= kNewFSPartitionTypeStandard; + partBlock->Flags = kNewFSPartitionTypeStandard; partBlock->CatalogCount = sectorCount / sizeof(NewCatalog); partBlock->SectorCount = sectorCount; partBlock->DiskSize = diskSize; @@ -363,11 +374,11 @@ bool NewFSParser::Format(_Input _Output DriveTrait* drive) { kcout << "Drive-Kind: " << drive->fDriveKind() << endl; kcout << "Partition-Name: " << partBlock->PartitionName << endl; - kcout << "Start-Catalog: " << number(partBlock->StartCatalog) << endl; - kcout << "Catalog-Count: " << number(partBlock->CatalogCount) << endl; - kcout << "Free-Catalog: " << number(partBlock->FreeCatalog) << endl; - kcout << "Free-Sectors: " << number(partBlock->FreeSectors) << endl; - kcout << "Sector-Size: " << number(partBlock->SectorSize) << endl; + kcout << "Start-Catalog: " << hex_number(partBlock->StartCatalog) << endl; + kcout << "Catalog-Count: " << hex_number(partBlock->CatalogCount) << endl; + kcout << "Free-Catalog: " << hex_number(partBlock->FreeCatalog) << endl; + kcout << "Free-Sectors: " << hex_number(partBlock->FreeSectors) << endl; + kcout << "Sector-Size: " << hex_number(partBlock->SectorSize) << endl; return true; } @@ -391,9 +402,13 @@ bool NewFSParser::WriteCatalog(_Input _Output NewCatalog* catalog, voidPtr data, if (!sMountpointInterface.GetAddressOf(this->fDriveIndex)) return false; NewFork* forkData = new NewFork(); + rt_set_memory(forkData, 0, sizeof(NewFork)); auto drive = sMountpointInterface.GetAddressOf(this->fDriveIndex); + rt_copy_memory((VoidPtr) "fs/newfs-packet", drive->fPacket.fPacketMime, + rt_string_len("fs/newfs-packet")); + auto startFork = catalog->DataFork; /// sanity check of the fork position as the condition to run the loop. @@ -404,18 +419,12 @@ bool NewFSParser::WriteCatalog(_Input _Output NewCatalog* catalog, voidPtr data, drive->fInput(&drive->fPacket); - /// check if fork even exists. - if (forkData->Flags != kNewFSFlagCreated) { - DbgLastError() = kErrorDiskIsCorrupted; - - delete forkData; - return false; - } - /// sanity check the fork. if (forkData->DataOffset <= kNewFSCatalogStartAddress) { DbgLastError() = kErrorDiskIsCorrupted; + kcout << "New OS: Invalid fork offset.\r"; + delete forkData; return false; } @@ -433,8 +442,8 @@ bool NewFSParser::WriteCatalog(_Input _Output NewCatalog* catalog, voidPtr data, } /// @brief -/// @param catalogName -/// @return +/// @param catalogName the catalog name. +/// @return the newly found catalog. _Output NewCatalog* NewFSParser::FindCatalog(_Input const char* catalogName, Lba& outLba) { if (!sMountpointInterface.GetAddressOf(this->fDriveIndex)) return nullptr; @@ -442,6 +451,9 @@ _Output NewCatalog* NewFSParser::FindCatalog(_Input const char* catalogName, Char* sectorBuf = new Char[sizeof(NewPartitionBlock)]; auto drive = sMountpointInterface.GetAddressOf(this->fDriveIndex); + rt_copy_memory((VoidPtr) "fs/newfs-packet", drive->fPacket.fPacketMime, + rt_string_len("fs/newfs-packet")); + drive->fPacket.fPacketContent = sectorBuf; drive->fPacket.fPacketSize = sizeof(NewPartitionBlock); drive->fPacket.fLba = kNewFSAddressAsLba; @@ -467,13 +479,14 @@ _Output NewCatalog* NewFSParser::FindCatalog(_Input const char* catalogName, if (StringBuilder::Equals(catalogName, catalog->Name)) { /// ignore unallocated catalog, break if (catalog->Flags != kNewFSFlagCreated) { - goto _NewFSContinueSearch; } NewCatalog* catalogPtr = new NewCatalog(); rt_copy_memory(catalog, catalogPtr, sizeof(NewCatalog)); + kcout << "New OS: Found catalog at: " << hex_number(start) << endl; + outLba = start; delete[] sectorBuf; return catalogPtr; @@ -482,7 +495,7 @@ _Output NewCatalog* NewFSParser::FindCatalog(_Input const char* catalogName, _NewFSContinueSearch: start = catalog->NextSibling; - kcout << "Catalog-Next: " << number(start) << endl; + kcout << "Catalog-Next: " << hex_number(start) << endl; if (start <= kNewFSAddressAsLba) break; @@ -511,14 +524,12 @@ _Output NewCatalog* NewFSParser::GetCatalog(_Input const char* name) { /// @param catalog /// @return Boolean NewFSParser::CloseCatalog(_Input _Output NewCatalog* catalog) { - if (this->WriteCatalog(catalog, nullptr, 0)) { - delete catalog; - catalog = nullptr; + if (!catalog) return false; - return true; - } + delete catalog; + catalog = nullptr; - return false; + return true; } /// @brief Mark catalog as removed. @@ -539,6 +550,9 @@ Boolean NewFSParser::RemoveCatalog(_Input const Char* catalogName) { auto drive = sMountpointInterface.GetAddressOf(this->fDriveIndex); + rt_copy_memory((VoidPtr) "fs/newfs-packet", drive->fPacket.fPacketMime, + rt_string_len("fs/newfs-packet")); + drive->fPacket.fLba = outLba; // the catalog position. drive->fPacket.fPacketSize = sizeof(NewCatalog); // size of catalog. roughly the sector size. @@ -561,23 +575,28 @@ Boolean NewFSParser::RemoveCatalog(_Input const Char* catalogName) { /// @param catalog /// @param dataSz /// @return -VoidPtr NewFSParser::ReadCatalog(_Input _Output const char* catalogName, +VoidPtr NewFSParser::ReadCatalog(_Input _Output NewCatalog* catalog, SizeT dataSz) { - if (!catalogName) { + if (!catalog) { DbgLastError() = kErrorFileNotFound; return nullptr; } if (!sMountpointInterface.GetAddressOf(this->fDriveIndex)) return nullptr; - NewCatalog* catalog = this->GetCatalog(catalogName); + Lba dataForkLba = catalog->DataFork; + Size dataForkSize = catalog->DataForkSize; - if (!catalog) return nullptr; + kcout << "Found-Catalog: " << catalog->Name + << ", Data-Fork: " << hex_number(dataForkLba) << endl; Char* sectorBuf = new Char[sizeof(NewFork)]; auto drive = sMountpointInterface.GetAddressOf(this->fDriveIndex); - drive->fPacket.fLba = catalog->DataFork; + rt_copy_memory((VoidPtr) "fs/newfs-packet", drive->fPacket.fPacketMime, + rt_string_len("fs/newfs-packet")); + + drive->fPacket.fLba = dataForkLba; drive->fPacket.fPacketSize = sizeof(NewFork); drive->fPacket.fPacketContent = sectorBuf; @@ -585,11 +604,14 @@ VoidPtr NewFSParser::ReadCatalog(_Input _Output const char* catalogName, NewFork* forkData = (NewFork*)sectorBuf; - kcout << forkData->Name << " " << hex_number(forkData->DataOffset) << endl; + kcout << "Fork-Name: " << forkData->Name << endl; - if (dataSz > forkData->DataSize) { + if (forkData->DataOffset <= kNewFSCatalogStartAddress || + dataSz > forkData->DataSize) { delete[] sectorBuf; + kcout << "Fail-Data-Offset: " << hex_number(forkData->DataOffset) << endl; + return nullptr; } diff --git a/Private/Source/FileManager.cxx b/Private/Source/FileManager.cxx index c7d0bf41..aab3cff0 100644 --- a/Private/Source/FileManager.cxx +++ b/Private/Source/FileManager.cxx @@ -88,7 +88,7 @@ Void NewFilesystemManager::Write(NodePtr node, VoidPtr data, /// @return VoidPtr NewFilesystemManager::Read(NodePtr node, Int32 flags, SizeT sz) { if ((reinterpret_cast(node))->Kind == kNewFSCatalogKindFile) - return fImpl->ReadCatalog(reinterpret_cast(node)->Name, sz); + return fImpl->ReadCatalog(reinterpret_cast(node), sz); return nullptr; } diff --git a/Private/Source/Utils.cxx b/Private/Source/Utils.cxx index 11b7129a..8eeb8cea 100644 --- a/Private/Source/Utils.cxx +++ b/Private/Source/Utils.cxx @@ -157,7 +157,7 @@ voidPtr rt_string_in_string(const char *in, const char *needle) { // @brief Checks for a string start at the character. -char *rt_string_frofchar(char *str, const char chr) { +char *rt_string_has_char(char *str, const char chr) { while (*str != chr) { ++str; -- cgit v1.2.3