From 422b8029eba71b6fbb6b3dcb386b8e115bbd08ef Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 6 Apr 2024 09:31:44 +0200 Subject: NewFS, Implementing the NewFSParser class, added CreateFork method. --- Private/Source/FS/NewFS.cxx | 92 ++++++++++++++++++++++++++++++++++++ Private/Source/KernelHeap.cxx | 54 ++++++++++----------- Private/Source/NewFS+FileManager.cxx | 4 +- Private/Source/NewFS+IO.cxx | 20 ++++---- Private/Source/NewFS+Journal.cxx | 55 ++------------------- 5 files changed, 137 insertions(+), 88 deletions(-) create mode 100644 Private/Source/FS/NewFS.cxx (limited to 'Private/Source') diff --git a/Private/Source/FS/NewFS.cxx b/Private/Source/FS/NewFS.cxx new file mode 100644 index 00000000..f497c2af --- /dev/null +++ b/Private/Source/FS/NewFS.cxx @@ -0,0 +1,92 @@ +/* ------------------------------------------- + + Copyright Mahrouss Logic + +------------------------------------------- */ + +#ifdef __FSKIT_NEWFS__ + +#include +#include + +using namespace NewOS; + +STATIC Lba ke_find_fork(SizeT sz); +STATIC Lba ke_find_catalog(SizeT sz); +STATIC Lba ke_find_data(SizeT sz); + +STATIC MountpointInterface sMountpointInterface; + +/// @brief Creates a new fork inside the New filesystem partition. +/// @param catalog it's catalog +/// @param theFork the fork itself. +/// @return the fork +_Output NewFork* NewFSParser::CreateFork(_Input NewCatalog* catalog, _Input NewFork& theFork) +{ + if (catalog && + theFork.Name[0] != 0 && + theFork.DataSize > 0) { + Lba whereFork = 0; + + theFork.DataOffset = ke_find_fork(theFork.DataSize); + theFork.Flags |= kNewFSFlagCreated; + + if (catalog->FirstFork == 0) { + catalog->FirstFork = whereFork; + } else { + if (catalog->LastFork == 0) { + theFork.PreviousSibling = catalog->FirstFork; + } + } + + if (catalog->LastFork == 0) { + catalog->LastFork = whereFork; + } else { + theFork.PreviousSibling = catalog->LastFork; + } + + if (!sMountpointInterface.GetAddressOf(this->fDriveIndex) || + !*sMountpointInterface.GetAddressOf(this->fDriveIndex)) + return nullptr; + + auto drv = *sMountpointInterface.GetAddressOf(this->fDriveIndex); + + drv->fPacket.fLba = whereFork; + drv->fPacket.fPacketSize = theFork.DataSize; + drv->fPacket.fPacketContent = (VoidPtr)&theFork; + + rt_copy_memory((VoidPtr)"fs/newfs-packet", drv->fPacket.fPacketMime, 16); + + if (auto res = fs_newfs_write(&sMountpointInterface, *drv, this->fDriveIndex); + res) { + switch (res) + { + case 1: + DbgLastError() = kErrorDiskReadOnly; + break; + case 2: + DbgLastError() = kErrorDiskIsFull; + break; + DbgLastError() = kErrorNoSuchDisk; + break; + + default: + break; + } + return nullptr; + } + + /// Also update catalog. + this->WriteCatalog(catalog, nullptr); + + return &theFork; + } + + return nullptr; +} + +STATIC Lba ke_find_fork(SizeT sz) {} +STATIC Lba ke_find_catalog(SizeT sz) {} +STATIC Lba ke_find_data(SizeT sz) {} + +#endif // ifdef __FSKIT_NEWFS__ \ No newline at end of file diff --git a/Private/Source/KernelHeap.cxx b/Private/Source/KernelHeap.cxx index d47b6931..0f2f57dd 100644 --- a/Private/Source/KernelHeap.cxx +++ b/Private/Source/KernelHeap.cxx @@ -13,8 +13,8 @@ //! @file KernelHeap.cxx //! @brief Kernel allocator. -#define kHeapMagic (0xD4D7D5) -#define kHeapHeaderPaddingSz (16U) +#define kKernelHeapMagic (0xD4D7D5) +#define kKernelHeapHeaderPaddingSz (16U) namespace NewOS { STATIC SizeT kHeapCount = 0UL; @@ -26,16 +26,16 @@ namespace Detail { /// | HIB | ADDRESS | struct PACKED HeapInformationBlock final { ///! @brief 32-bit value which contains the magic number of the executable. - UInt32 hMagic; + UInt32 fMagic; ///! @brief Boolean value which tells if the pointer is allocated. - Boolean hPresent; + Boolean fPresent; ///! @brief 32-bit CRC checksum - UInt32 hCRC32; + UInt32 fCRC32; /// @brief 64-bit pointer size. - SizeT hSizePtr; + SizeT fTargetPtrSize; /// @brief 64-bit target pointer. - UIntPtr hTargetPtr; - UInt8 hPadding[kHeapHeaderPaddingSz]; + UIntPtr fTargetPtr; + UInt8 fPadding[kKernelHeapHeaderPaddingSz]; }; typedef HeapInformationBlock *HeapInformationBlockPtr; @@ -55,10 +55,10 @@ VoidPtr ke_new_ke_heap(SizeT sz, const bool rw, const bool user) { reinterpret_cast( wrapper.VirtualAddress()); - heapInfo->hSizePtr = sz; - heapInfo->hMagic = kHeapMagic; - heapInfo->hCRC32 = 0; // dont fill it for now. - heapInfo->hTargetPtr = wrapper.VirtualAddress(); + heapInfo->fTargetPtrSize = sz; + heapInfo->fMagic = kKernelHeapMagic; + heapInfo->fCRC32 = 0; // dont fill it for now. + heapInfo->fTargetPtr = wrapper.VirtualAddress(); ++kHeapCount; @@ -76,24 +76,24 @@ Int32 ke_delete_ke_heap(VoidPtr heapPtr) { reinterpret_cast( (UIntPtr)heapPtr - sizeof(Detail::HeapInformationBlock)); - if (virtualAddress && virtualAddress->hMagic == kHeapMagic) { - if (!virtualAddress->hPresent) { + if (virtualAddress && virtualAddress->fMagic == kKernelHeapMagic) { + if (!virtualAddress->fPresent) { return -kErrorHeapNotPresent; } - if (virtualAddress->hCRC32 != 0) { - if (virtualAddress->hCRC32 != - ke_calculate_crc32((Char *)virtualAddress->hTargetPtr, - virtualAddress->hSizePtr)) { + if (virtualAddress->fCRC32 != 0) { + if (virtualAddress->fCRC32 != + ke_calculate_crc32((Char *)virtualAddress->fTargetPtr, + virtualAddress->fTargetPtrSize)) { ke_stop(RUNTIME_CHECK_POINTER); } } - virtualAddress->hSizePtr = 0UL; - virtualAddress->hPresent = false; - virtualAddress->hTargetPtr = 0; - virtualAddress->hCRC32 = 0; - virtualAddress->hMagic = 0; + virtualAddress->fTargetPtrSize = 0UL; + virtualAddress->fPresent = false; + virtualAddress->fTargetPtr = 0; + virtualAddress->fCRC32 = 0; + virtualAddress->fMagic = 0; PTEWrapper pageWrapper(false, false, false, (UIntPtr)virtualAddress); Ref pteAddress{ &pageWrapper }; @@ -118,7 +118,7 @@ Boolean ke_is_valid_heap(VoidPtr heapPtr) { reinterpret_cast( (UIntPtr)heapPtr - sizeof(Detail::HeapInformationBlock)); - if (virtualAddress->hPresent && virtualAddress->hMagic == kHeapMagic) { + if (virtualAddress->fPresent && virtualAddress->fMagic == kKernelHeapMagic) { return true; } } @@ -135,9 +135,9 @@ Boolean ke_protect_ke_heap(VoidPtr heapPtr) { reinterpret_cast( (UIntPtr)heapPtr - sizeof(Detail::HeapInformationBlock)); - if (virtualAddress->hPresent && virtualAddress->hMagic == kHeapMagic) { - virtualAddress->hCRC32 = - ke_calculate_crc32((Char *)heapPtr, virtualAddress->hSizePtr); + if (virtualAddress->fPresent && virtualAddress->fMagic == kKernelHeapMagic) { + virtualAddress->fCRC32 = + ke_calculate_crc32((Char *)heapPtr, virtualAddress->fTargetPtrSize); return true; } } diff --git a/Private/Source/NewFS+FileManager.cxx b/Private/Source/NewFS+FileManager.cxx index 4ba95d89..7227c2e2 100644 --- a/Private/Source/NewFS+FileManager.cxx +++ b/Private/Source/NewFS+FileManager.cxx @@ -13,7 +13,9 @@ namespace NewOS { /// @brief C++ constructor -NewFilesystemManager::NewFilesystemManager() = default; +NewFilesystemManager::NewFilesystemManager() { + MUST_PASS(Detail::fs_init_newfs()); +} NewFilesystemManager::~NewFilesystemManager() = default; diff --git a/Private/Source/NewFS+IO.cxx b/Private/Source/NewFS+IO.cxx index 63db9b1a..a7acb63b 100644 --- a/Private/Source/NewFS+IO.cxx +++ b/Private/Source/NewFS+IO.cxx @@ -34,25 +34,25 @@ using namespace NewOS; /// @param DrvTrait drive info /// @param DrvIndex drive index. /// @return -Int32 fs_newfs_read_raw(MountpointInterface* Mnt, DriveTrait& DrvTrait, Int32 DrvIndex) { +Int32 fs_newfs_read(MountpointInterface* Mnt, DriveTrait& DrvTrait, Int32 DrvIndex) { if (!Mnt) return -1; DrvTrait.fPacket.fPacketGood = false; switch (DrvIndex) { - case kHCFSSubDriveA: { + case kNewFSSubDriveA: { NEWFS_READ(A, DrvTrait.fPacket, Mnt); break; } - case kHCFSSubDriveB: { + case kNewFSSubDriveB: { NEWFS_READ(B, DrvTrait.fPacket, Mnt); break; } - case kHCFSSubDriveC: { + case kNewFSSubDriveC: { NEWFS_READ(C, DrvTrait.fPacket, Mnt); break; } - case kHCFSSubDriveD: { + case kNewFSSubDriveD: { NEWFS_READ(D, DrvTrait.fPacket, Mnt); break; } @@ -66,25 +66,25 @@ Int32 fs_newfs_read_raw(MountpointInterface* Mnt, DriveTrait& DrvTrait, Int32 Dr /// @param DrvTrait drive info /// @param DrvIndex drive index. /// @return -Int32 fs_newfs_write_raw(MountpointInterface* Mnt, DriveTrait& DrvTrait, Int32 DrvIndex) { +Int32 fs_newfs_write(MountpointInterface* Mnt, DriveTrait& DrvTrait, Int32 DrvIndex) { if (!Mnt) return -1; DrvTrait.fPacket.fPacketGood = false; switch (DrvIndex) { - case kHCFSSubDriveA: { + case kNewFSSubDriveA: { NEWFS_WRITE(A, DrvTrait.fPacket, Mnt); break; } - case kHCFSSubDriveB: { + case kNewFSSubDriveB: { NEWFS_WRITE(B, DrvTrait.fPacket, Mnt); break; } - case kHCFSSubDriveC: { + case kNewFSSubDriveC: { NEWFS_WRITE(C, DrvTrait.fPacket, Mnt); break; } - case kHCFSSubDriveD: { + case kNewFSSubDriveD: { NEWFS_WRITE(D, DrvTrait.fPacket, Mnt); break; } diff --git a/Private/Source/NewFS+Journal.cxx b/Private/Source/NewFS+Journal.cxx index ce04785e..bb44f7cd 100644 --- a/Private/Source/NewFS+Journal.cxx +++ b/Private/Source/NewFS+Journal.cxx @@ -9,57 +9,12 @@ #ifdef __FSKIT_NEWFS__ -//! BUGS: 0 -//! @brief Journaling for NewFS. +///! BUGS: 0 +///! @file NewFS+Journal.cxx +///! @brief Journaling for NewFS. -#define kNewFSOpLog (4) - -namespace NewOS::Detail { -typedef Boolean (*NewFSRunnerType)(VoidPtr delegate); - -/// @brief Journal thread class. -class NewFSJournalRunner final { - private: - NewFSRunnerType fLoadRoutine{nullptr}; - NewFSRunnerType fCacheRoutine{nullptr}; - NewFSRunnerType fUnloadRoutine{nullptr}; - - public: - explicit NewFSJournalRunner(NewFSRunnerType load_runner) - : fLoadRoutine(load_runner) { - MUST_PASS(fLoadRoutine); - - // load runner - if (fLoadRoutine) fLoadRoutine(this); - } - - /// @brief Unload journal runner - ~NewFSJournalRunner() noexcept { - if (fUnloadRoutine) fUnloadRoutine(this); - } - - NEWOS_COPY_DEFAULT(NewFSJournalRunner); - - public: - Boolean Run(const Int32& operation, VoidPtr classPtr) { - switch (operation) { - case kNewFSOpLog: { - if (!classPtr) { - kcout << "NewOS: Miss for classPtr at " - "NewFSJournalManager::Run(classPtr) " - << __FILE__ << "\n"; - return false; - } - - MUST_PASS(fCacheRoutine); - return fCacheRoutine(classPtr); - } - }; - - return false; - } -}; -} // namespace NewOS::Detail +namespace NewOS::Journal { +} // namespace NewOS::Journal using namespace NewOS; -- cgit v1.2.3