diff options
| author | Amlal <amlal.elmahrouss@icloud.com> | 2025-01-24 21:18:46 +0100 |
|---|---|---|
| committer | Amlal <amlal.elmahrouss@icloud.com> | 2025-01-24 21:18:46 +0100 |
| commit | 105c5a085c9cacf5774511506ebe4ada8f325457 (patch) | |
| tree | 9c603e70918154f73e16c7a29a70ffba34a5270f /dev | |
| parent | 7ec22ed2ec82a72d691795bf12c158e1d58b09f7 (diff) | |
ADD: Missing components from private repository.
Signed-off-by: Amlal <amlal.elmahrouss@icloud.com>
Diffstat (limited to 'dev')
| -rw-r--r-- | dev/Kernel/FirmwareKit/CoreBoot/CoreBoot.h | 31 | ||||
| -rw-r--r-- | dev/Kernel/FirmwareKit/CoreBoot/NS.h | 10 | ||||
| -rw-r--r-- | dev/Kernel/FirmwareKit/CoreBoot/NetBoot.h | 25 | ||||
| -rw-r--r-- | dev/Kernel/FirmwareKit/EPM.h | 112 | ||||
| -rw-r--r-- | dev/Kernel/src/FS/NeFS+FileMgr.cc | 245 | ||||
| -rw-r--r-- | dev/Kernel/src/FS/NeFS.cc | 1051 |
6 files changed, 1474 insertions, 0 deletions
diff --git a/dev/Kernel/FirmwareKit/CoreBoot/CoreBoot.h b/dev/Kernel/FirmwareKit/CoreBoot/CoreBoot.h new file mode 100644 index 00000000..b30511f6 --- /dev/null +++ b/dev/Kernel/FirmwareKit/CoreBoot/CoreBoot.h @@ -0,0 +1,31 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.h> + +namespace Firmware::Detail::CoreBoot +{ + using namespace Kernel; + + struct LEHeader; + + /// @brief Linear Executable Header + /// @author Amlal EL Mahrouss + struct ATTRIBUTE(aligned(4)) LEHeader + { + const Char fMagic[2]; // magic number + const Char fName[10]; // operating system name + const UInt32 fRevision; // firmware revision + const UInt32 fStartAddress; // start address (master/slave(s) thread) + +#ifdef ZKA_IS_EXTENDED_COREBOOT + const UIntPtr fMasterStructure; // master structure for MP/PM and device tree and such (ARM) + const UIntPtr fMasterStructureVersion; // master structure version. +#endif + }; +} // namespace Firmware::Detail::CoreBoot diff --git a/dev/Kernel/FirmwareKit/CoreBoot/NS.h b/dev/Kernel/FirmwareKit/CoreBoot/NS.h new file mode 100644 index 00000000..efe7fa19 --- /dev/null +++ b/dev/Kernel/FirmwareKit/CoreBoot/NS.h @@ -0,0 +1,10 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include <FirmwareKit/CoreBoot/CoreBoot.h> +#include <FirmwareKit/CoreBoot/NetBoot.h>
\ No newline at end of file diff --git a/dev/Kernel/FirmwareKit/CoreBoot/NetBoot.h b/dev/Kernel/FirmwareKit/CoreBoot/NetBoot.h new file mode 100644 index 00000000..a6f10809 --- /dev/null +++ b/dev/Kernel/FirmwareKit/CoreBoot/NetBoot.h @@ -0,0 +1,25 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include <NewKit/Defines.h> + +/// @brief the internet header is used to download updates OTA. +typedef struct NetBootInternetHeader +{ + Kernel::Char NB1; /// magic char 1 'N' + Kernel::Char NB2; /// magic char 2 'E' + Kernel::Char NB3; /// magic char 3 'T' + Kernel::Char NB4; /// magic char 4 'B' + + Kernel::Char PatchName[255]; /// example: ColdChoco + Kernel::Int32 PatchLength; /// the patch length. + Kernel::Char PatchTarget[255]; /// the target file. + Kernel::Boolean ImpliesROM; /// does it imply an EEPROM reprogram? + Kernel::Boolean Preflight; /// is it a preflight packet. + Kernel::Char Patch[]; /// non preflight packet has a patch blob for a **PatchTarget** +} NetBootInternetHeader; diff --git a/dev/Kernel/FirmwareKit/EPM.h b/dev/Kernel/FirmwareKit/EPM.h new file mode 100644 index 00000000..244a44b8 --- /dev/null +++ b/dev/Kernel/FirmwareKit/EPM.h @@ -0,0 +1,112 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved. + +------------------------------------------- */ + +/** + @brief The Explicit Partition Map scheme. +*/ + +#ifndef FIRMWAREKIT_EPM_H +#define FIRMWAREKIT_EPM_H + +#include <NewKit/Defines.h> + +#define kEPMNameLength (32) +#define kEPMFilesystemLength (16) +#define kEPMMagicLength (5) + +/* @brief AMD64 magic for EPM */ +#define kEPMMagic86 "EPMAM" + +/* @brief RISC-V magic for EPM */ +#define kEPMMagicRISCV "EPMRV" + +/* @brief ARM magic for EPM */ +#define kEPMMagicARM "EPMAR" + +/* @brief 64x0 magic for EPM */ +#define kEPMMagic64k "EPM64" + +/* @brief 32x0 magic for EPM */ +#define kEPMMagic32k "EPM32" + +/* @brief POWER magic for EPM */ +#define kEPMMagicPPC "EPMPC" + +/* @brief Invalid magic for EPM */ +#define kEPMMagicError "EPM??" + +#ifdef __ZKA_AMD64__ +#define kEPMMagic kEPMMagic86 +#else +#ifdef __ZKA_ARM64__ +#define kEPMMagic kEPMMagicARM +#else +#define kEPMMagic kEPMMagicError +#endif +#endif + +///! @brief partition must start at this address. +///! Anything below is reserved for Data backup by the Main OS. +#define kEPMPartBlockLba (sizeof(EPM_PART_BLOCK)) + +///! @brief Current EPM revision. +#define kEPMRevisionBcd (0x0100) + +/// !@brief EPM boot block address. +#define kEPMBootBlockLba (0U) + +#define kEPMReserveLen (401) + +struct EPM_GUID; +struct EPM_PART_BLOCK; + +/* The first 0 > 128 addresses of a disk contains these headers. */ + +/// @brief EPM GUID structure. +typedef struct EPM_GUID +{ + Kernel::UInt32 Data1; + Kernel::UInt16 Data2; + Kernel::UInt16 Data3; + Kernel::UInt8 Data4[8]; +} EPM_GUID; + +/** + * @brief The EPM boot block. + * @note NumBlock and LbaStart are ignored on some platforms. + */ +struct PACKED EPM_PART_BLOCK +{ + Kernel::Char Magic[kEPMMagicLength]; + Kernel::Char Name[kEPMNameLength]; + EPM_GUID Guid; + Kernel::Int32 Version; + Kernel::Int64 NumBlocks; + Kernel::Int64 SectorSz; + Kernel::Int64 LbaStart; // base offset + Kernel::Int64 LbaEnd; // addition of lba_start to get the end of partition. + Kernel::Int16 Kind; + Kernel::Int32 FsVersion; + Kernel::Char Fs[kEPMFilesystemLength]; /* NeFS, ffs2... */ + Kernel::Char Reserved[kEPMReserveLen]; // to fill a full sector. +}; + +///! @brief Version kind enum. +///! @brief Use in boot block version field. + +enum +{ + kEPMInvalid = 0x00, + kEPMGenericOS = 0xcf, // Generic OS + kEPMLinux = 0x8f, // Linux on EPM + kEPMBSD = 0x9f, // Berkeley Soft. Distribution + kEPMZkaOS = 0x1f, // This OS. + kEPMInvalidOS = 0xff, +}; + +typedef struct EPM_PART_BLOCK BOOT_BLOCK_STRUCT; + +#endif // ifndef FIRMWAREKIT_EPM_H diff --git a/dev/Kernel/src/FS/NeFS+FileMgr.cc b/dev/Kernel/src/FS/NeFS+FileMgr.cc new file mode 100644 index 00000000..8d44f707 --- /dev/null +++ b/dev/Kernel/src/FS/NeFS+FileMgr.cc @@ -0,0 +1,245 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved. + +------------------------------------------- */ + +#include <KernelKit/FileMgr.h> +#include <KernelKit/Heap.h> + +#ifndef __ZKA_MINIMAL_OS__ +#ifdef __FSKIT_INCLUDES_NEFS__ + +/// @brief NeFS File manager. +/// BUGS: 0 + +namespace Kernel +{ + /// @brief C++ constructor + NeFileSystemMgr::NeFileSystemMgr() + { + NeFileSystemParser* mParser = new NeFileSystemParser(); + MUST_PASS(mParser); + + kcout << "We are done allocating NeFileSystemParser...\r"; + } + + NeFileSystemMgr::~NeFileSystemMgr() + { + if (mParser) + { + kcout << "Destroying NeFileSystemParser...\r"; + mm_delete_class(&mParser); + } + } + + /// @brief Removes a node from the filesystem. + /// @param path The filename + /// @return If it was deleted or not. + bool NeFileSystemMgr::Remove(_Input const Char* path) + { + if (path == nullptr || *path == 0) + return false; + + return mParser->RemoveCatalog(path); + } + + /// @brief Creates a node with the specified. + /// @param path The filename path. + /// @return The Node pointer. + NodePtr NeFileSystemMgr::Create(_Input const Char* path) + { + return node_cast(mParser->CreateCatalog(path)); + } + + /// @brief Creates a node with is a directory. + /// @param path The filename path. + /// @return The Node pointer. + NodePtr NeFileSystemMgr::CreateDirectory(const Char* path) + { + return node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindDir)); + } + + /// @brief Creates a node with is a alias. + /// @param path The filename path. + /// @return The Node pointer. + NodePtr NeFileSystemMgr::CreateAlias(const Char* path) + { + return node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindAlias)); + } + + /// @brief Creates a node with is a page file. + /// @param path The filename path. + /// @return The Node pointer. + NodePtr NeFileSystemMgr::CreateSwapFile(const Char* path) + { + return node_cast(mParser->CreateCatalog(path, 0, kNeFSCatalogKindPage)); + } + + /// @brief Gets the root directory. + /// @return + const Char* NeFileSystemHelper::Root() + { + return kNeFSRoot; + } + + /// @brief Gets the up-dir directory. + /// @return + const Char* NeFileSystemHelper::UpDir() + { + return kNeFSUpDir; + } + + /// @brief Gets the separator character. + /// @return + const Char NeFileSystemHelper::Separator() + { + return kNeFSSeparator; + } + + /// @brief Gets the metafile character. + /// @return + const Char NeFileSystemHelper::MetaFile() + { + return kNeFSMetaFilePrefix; + } + + /// @brief Opens a new file. + /// @param path + /// @param r + /// @return + _Output NodePtr NeFileSystemMgr::Open(_Input const Char* path, _Input const Char* r) + { + if (!path || *path == 0) + return nullptr; + + if (!r || *r == 0) + return nullptr; + + auto catalog = mParser->GetCatalog(path); + + return node_cast(catalog); + } + + /// @brief Writes to a catalog's fork. + /// @param node the node ptr. + /// @param data the data. + /// @param flags the size. + /// @return + Void NeFileSystemMgr::Write(_Input NodePtr node, _Input VoidPtr data, _Input Int32 flags, _Input SizeT size) + { + if (!node) + return; + if (!size) + return; + + constexpr auto kDataForkName = kNeFSDataFork; + this->Write(kDataForkName, node, data, flags, size); + } + + /// @brief Read from filesystem fork. + /// @param node the catalog node. + /// @param flags the flags with it. + /// @param sz the size to read. + /// @return + _Output VoidPtr NeFileSystemMgr::Read(_Input NodePtr node, _Input Int32 flags, _Input SizeT size) + { + if (!node) + return nullptr; + if (!size) + return nullptr; + + constexpr auto kDataForkName = kNeFSDataFork; + return this->Read(kDataForkName, node, flags, size); + } + + Void NeFileSystemMgr::Write(_Input const Char* name, + _Input NodePtr node, + _Input VoidPtr data, + _Input Int32 flags, + _Input SizeT size) + { + if (!size || + size > kNeFSForkSize) + return; + + if (!data) + return; + + ZKA_UNUSED(flags); + + if ((reinterpret_cast<NFS_CATALOG_STRUCT*>(node))->Kind == kNeFSCatalogKindFile) + mParser->WriteCatalog(reinterpret_cast<NFS_CATALOG_STRUCT*>(node)->Name, (flags & kFileFlagRsrc ? true : false), data, size, + name); + } + + _Output VoidPtr NeFileSystemMgr::Read(_Input const Char* name, + _Input NodePtr node, + _Input Int32 flags, + _Input SizeT sz) + { + if (sz > kNeFSForkSize) + return nullptr; + + if (!sz) + return nullptr; + + ZKA_UNUSED(flags); + + if ((reinterpret_cast<NFS_CATALOG_STRUCT*>(node))->Kind == kNeFSCatalogKindFile) + return mParser->ReadCatalog(reinterpret_cast<NFS_CATALOG_STRUCT*>(node), (flags & kFileFlagRsrc ? true : false), sz, + name); + + return nullptr; + } + + /// @brief Seek from Catalog. + /// @param node + /// @param off + /// @retval true always returns false, this is unimplemented. + /// @retval false always returns this, it is unimplemented. + + _Output Bool NeFileSystemMgr::Seek(NodePtr node, SizeT off) + { + if (!node || off == 0) + return false; + + return mParser->Seek(reinterpret_cast<NFS_CATALOG_STRUCT*>(node), off); + } + + /// @brief Tell where the catalog is. + /// @param node + /// @retval true always returns false, this is unimplemented. + /// @retval false always returns this, it is unimplemented. + + _Output SizeT NeFileSystemMgr::Tell(NodePtr node) + { + if (!node) + return kNPos; + + return mParser->Tell(reinterpret_cast<NFS_CATALOG_STRUCT*>(node)); + } + + /// @brief Rewinds the catalog. + /// @param node + /// @retval true always returns false, this is unimplemented. + /// @retval false always returns this, it is unimplemented. + + _Output Bool NeFileSystemMgr::Rewind(NodePtr node) + { + if (!node) + return false; + + return this->Seek(node, 0); + } + + /// @brief Returns the filesystem parser. + /// @return the Filesystem parser class. + _Output NeFileSystemParser* NeFileSystemMgr::GetParser() noexcept + { + return mParser; + } +} // namespace Kernel + +#endif // ifdef __FSKIT_INCLUDES_NEFS__ +#endif // ifndef __ZKA_MINIMAL_OS__ diff --git a/dev/Kernel/src/FS/NeFS.cc b/dev/Kernel/src/FS/NeFS.cc new file mode 100644 index 00000000..231cbfe1 --- /dev/null +++ b/dev/Kernel/src/FS/NeFS.cc @@ -0,0 +1,1051 @@ +/* ------------------------------------------- + + Copyright (C) 2024, Amlal EL Mahrouss, all rights reserved. + +------------------------------------------- */ + +#include "KernelKit/DebugOutput.h" +#ifdef __FSKIT_INCLUDES_NEFS__ + +#include <FSKit/NeFS.h> +#include <FirmwareKit/EPM.h> + +#include <Mod/AHCI/AHCI.h> +#include <Mod/ATA/ATA.h> +#include <Mod/MFlash/MFlash.h> +#include <KernelKit/LPC.h> +#include <NewKit/Crc32.h> +#include <NewKit/KernelPanic.h> +#include <NewKit/KString.h> +#include <NewKit/Utils.h> +#include <KernelKit/UserProcessScheduler.h> +#include <KernelKit/User.h> +#include <KernelKit/DriveMgr.h> + +using namespace Kernel; + +#ifdef __ZKA_NO_BUILTIN__ +/***********************************************************************************/ +/** + Define those external symbols, to make the editor shutup +*/ +/***********************************************************************************/ + +/***********************************************************************************/ +/// @brief get sector count. +/***********************************************************************************/ +Kernel::SizeT drv_get_sector_count(); + +/***********************************************************************************/ +/// @brief get device size. +/***********************************************************************************/ +Kernel::SizeT drv_get_size(); + +#endif + +///! BUGS: 0 + +/***********************************************************************************/ +/// This file implements the New extended File System. +/// New extended File System implements a B-Tree based algortihm. +/// / +/// /Path1/ /ath2/ +/// /readme.rtf /ListContents.pef /readme.lnk <-- symlink. +/// /Path1/readme.rtf +/***********************************************************************************/ + +STATIC MountpointInterface kMountpoint; + +/***********************************************************************************/ +/// @brief Creates a new fork inside the New filesystem partition. +/// @param catalog it's catalog +/// @param the_fork the fork itself. +/// @return the fork +/***********************************************************************************/ +_Output BOOL NeFileSystemParser::CreateFork(_Input NFS_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; + + kcout << "Fork LBA: " << hex_number(lba) << endl; + + if (lba < kNeFSCatalogStartAddress) + return NO; + + auto drv = kMountpoint.A(); + + Lba lba_prev = lba; + + NFS_FORK_STRUCT prev_fork; + NFS_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(NFS_FORK_STRUCT); + drv.fPacket.fPacketContent = &cur_fork; + + drv.fInput(drv.fPacket); + + kcout << "Next fork: " << hex_number(cur_fork.NextSibling) << endl; + + if (cur_fork.Flags & kNeFSFlagCreated) + { + kcout << "Fork already exists.\r"; + + /// sanity check. + if (StringBuilder::Equals(cur_fork.ForkName, the_fork.ForkName) && + StringBuilder::Equals(cur_fork.CatalogName, the_fork.CatalogName)) + break; + + kcout << "Next fork: " << hex_number(cur_fork.NextSibling) << endl; + + lba_prev = lba; + lba = cur_fork.NextSibling; + + 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(NFS_FORK_STRUCT); + drv.fPacket.fPacketContent = &prev_fork; + + prev_fork.NextSibling = lba; + + /// write to disk. + drv.fOutput(drv.fPacket); + } + + break; + } + } + + the_fork.Flags |= kNeFSFlagCreated; + the_fork.DataOffset = lba - sizeof(NFS_FORK_STRUCT); + the_fork.PreviousSibling = lba_prev; + the_fork.NextSibling = the_fork.DataOffset - the_fork.DataSize; + + drv.fPacket.fPacketLba = lba; + drv.fPacket.fPacketSize = sizeof(NFS_FORK_STRUCT); + drv.fPacket.fPacketContent = &the_fork; + + kcout << "Writing fork...\r"; + + // drv.fOutput(drv.fPacket); + + fs_ifs_write(&kMountpoint, drv, MountpointInterface::kDriveIndexA); + + /// log what we have now. + kcout << "Wrote fork data at: " << hex_number(the_fork.DataOffset) + << endl; + + kcout << "Wrote fork at: " << hex_number(lba) << endl; + + return YES; + } + + return NO; +} + +/***********************************************************************************/ +/// @brief Find fork inside New filesystem. +/// @param catalog the catalog. +/// @param name the fork name. +/// @return the fork. +/***********************************************************************************/ +_Output NFS_FORK_STRUCT* NeFileSystemParser::FindFork(_Input NFS_CATALOG_STRUCT* catalog, + _Input const Char* name, + Boolean isDataFork) +{ + auto drive = kMountpoint.A(); + NFS_FORK_STRUCT* the_fork = nullptr; + + Lba lba = isDataFork ? catalog->DataFork : catalog->ResourceFork; + + while (lba != 0) + { + drive.fPacket.fPacketLba = lba; + drive.fPacket.fPacketSize = sizeof(NFS_FORK_STRUCT); + drive.fPacket.fPacketContent = (VoidPtr)the_fork; + + rt_copy_memory((VoidPtr) "fs/nefs-packet", drive.fPacket.fPacketMime, 16); + + if (auto res = + fs_ifs_read(&kMountpoint, drive, this->mDriveIndex); + res) + { + switch (res) + { + case 1: + err_global_get() = kErrorDiskReadOnly; + break; + case 2: + err_global_get() = kErrorDiskIsFull; + break; + err_global_get() = kErrorNoSuchDisk; + break; + + default: + break; + } + return nullptr; + } + + if (StringBuilder::Equals(the_fork->ForkName, name)) + { + break; + } + + lba = the_fork->NextSibling; + } + + return the_fork; +} + +/***********************************************************************************/ +/// @brief Simpler factory to create a catalog (assumes you want to create a +/// file.) +/// @param name +/// @return catalog pointer. +/***********************************************************************************/ +_Output NFS_CATALOG_STRUCT* NeFileSystemParser::CreateCatalog(_Input const Char* name) +{ + return this->CreateCatalog(name, 0, kNeFSCatalogKindFile); +} + +/***********************************************************************************/ +/// @brief Creates a new catalog into the disk. +/// @param name the catalog name. +/// @param flags the flags of the catalog. +/// @param kind the catalog kind. +/// @return catalog pointer. +/***********************************************************************************/ +_Output NFS_CATALOG_STRUCT* NeFileSystemParser::CreateCatalog(_Input const Char* name, + _Input const Int32& flags, + _Input const Int32& kind) +{ + kcout << "CreateCatalog(...)\r"; + + Lba out_lba = 0UL; + + kcout << "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()) + return nullptr; + + /// a file shouldn't have a slash in the end. + if (kind != kNeFSCatalogKindDir && + name[rt_string_len(name) - 1] == NeFileSystemHelper::Separator()) + return nullptr; + + NFS_CATALOG_STRUCT* catalog_copy = this->FindCatalog(name, out_lba); + + if (catalog_copy) + { + kcout << "Catalog already exists: " << name << ".\r"; + err_global_get() = kErrorFileExists; + + delete catalog_copy; + catalog_copy = nullptr; + + return nullptr; + } + + Char parent_name[kNeFSNodeNameLen] = {0}; + + for (SizeT indexName = 0UL; indexName < rt_string_len(name); ++indexName) + { + parent_name[indexName] = name[indexName]; + } + + if (*parent_name == 0) + { + kcout << "Parent name is NUL.\r"; + err_global_get() = kErrorFileNotFound; + return nullptr; + } + + /// 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]; + } + + 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()) + { + parent_name[index_reverse_copy] = 0; + --index_reverse_copy; + } + + NFS_CATALOG_STRUCT* catalog = this->FindCatalog(parent_name, out_lba); + + auto drive = kMountpoint.A(); + + constexpr auto kNeFSCatalogPadding = 4; + + if (catalog && catalog->Kind == kNeFSCatalogKindFile) + { + kcout << "Parent is a file.\r"; + delete catalog; + + return nullptr; + } + else if (!catalog) + { + Char part_block[sizeof(NFS_ROOT_PARTITION_BLOCK)] = {0}; + + drive.fPacket.fPacketContent = part_block; + drive.fPacket.fPacketSize = sizeof(NFS_ROOT_PARTITION_BLOCK); + drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; + + drive.fInput(drive.fPacket); + + NFS_ROOT_PARTITION_BLOCK* blk_nefs = (NFS_ROOT_PARTITION_BLOCK*)part_block; + out_lba = blk_nefs->StartCatalog; + } + + NFS_CATALOG_STRUCT* child_catalog = new NFS_CATALOG_STRUCT(); + + 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; + + rt_copy_memory((VoidPtr)name, (VoidPtr)child_catalog->Name, + rt_string_len(name)); + + NFS_CATALOG_STRUCT temporary_catalog{}; + + 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(NFS_ROOT_PARTITION_BLOCK)] = {0}; + + drive.fPacket.fPacketContent = buf_part_block; + drive.fPacket.fPacketSize = sizeof(NFS_ROOT_PARTITION_BLOCK); + drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; + + drive.fInput(drive.fPacket); + + NFS_ROOT_PARTITION_BLOCK* part_block = (NFS_ROOT_PARTITION_BLOCK*)buf_part_block; + + drive.fPacket.fPacketContent = &temporary_catalog; + drive.fPacket.fPacketSize = sizeof(NFS_CATALOG_STRUCT); + drive.fPacket.fPacketLba = start_free; + + drive.fInput(drive.fPacket); + + if (part_block->FreeCatalog < 1) + { + delete child_catalog; + child_catalog = nullptr; + + return nullptr; + } + + while (start_free >= part_block->StartCatalog) + { + // ========================== // + // Allocate catalog now... + // ========================== // + if ((temporary_catalog.Flags & kNeFSFlagCreated) == 0) + { + child_catalog->NextSibling = + start_free + (sizeof(NFS_CATALOG_STRUCT) * kNeFSCatalogPadding); + + drive.fPacket.fPacketContent = &temporary_catalog; + drive.fPacket.fPacketSize = sizeof(NFS_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.fPacketSize = sizeof(NFS_CATALOG_STRUCT); + drive.fPacket.fPacketLba = start_free; + + drive.fOutput(drive.fPacket); + + // Get NeFS partition's block. + + drive.fPacket.fPacketContent = buf_part_block; + drive.fPacket.fPacketSize = sizeof(NFS_ROOT_PARTITION_BLOCK); + drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; + + drive.fInput(drive.fPacket); + + part_block->FreeSectors -= 1; + part_block->CatalogCount += 1; + part_block->FreeCatalog -= 1; + + drive.fOutput(drive.fPacket); + + kcout << "Create new catalog with flags: " + << hex_number(child_catalog->Flags) << endl; + kcout << "Create new catalog with name: " << child_catalog->Name + << endl; + + delete catalog; + catalog = nullptr; + + NFS_CATALOG_STRUCT* found_catalog = new NFS_CATALOG_STRUCT(); + rt_copy_memory(&temporary_catalog, found_catalog, sizeof(NFS_CATALOG_STRUCT)); + + return found_catalog; + } + else if ((temporary_catalog.Flags & kNeFSFlagCreated) && + StringBuilder::Equals(temporary_catalog.Name, name)) + { + rt_copy_memory(&temporary_catalog, child_catalog, sizeof(NFS_CATALOG_STRUCT)); + + return child_catalog; + } + + start_free = start_free + (sizeof(NFS_CATALOG_STRUCT) * kNeFSCatalogPadding); + + drive.fPacket.fPacketContent = &temporary_catalog; + drive.fPacket.fPacketSize = sizeof(NFS_CATALOG_STRUCT); + drive.fPacket.fPacketLba = start_free; + + drive.fInput(drive.fPacket); + } + + 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 Lba endLba, _Input const Int32 flags, const Char* part_name) +{ +#ifdef ZKA_EPM_SUPPORT + if (*part_name == 0 || + endLba == 0) + return false; + + // 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) + { + err_global_get() = kErrorDiskIsCorrupted; + return false; + } + + Char fs_buf[sizeof(NFS_ROOT_PARTITION_BLOCK)] = {0}; + + Lba start = kNeFSRootCatalogStartAddress; + + drive->fPacket.fPacketContent = fs_buf; + drive->fPacket.fPacketSize = sizeof(NFS_ROOT_PARTITION_BLOCK); + drive->fPacket.fPacketLba = start; + + drive->fInput(&drive->fPacket); + + if (flags & kNeFSPartitionTypeBoot) + { + // make it bootable when needed. + Char buf_epm[kNeFSSectorSz] = {0}; + + BOOT_BLOCK_STRUCT* epm_boot = (BOOT_BLOCK_STRUCT*)buf_epm; + + // Write a new EPM entry. + + constexpr auto kFsName = "NeFS"; + constexpr auto kBlockName = "ZkaOS:"; + + rt_copy_memory(reinterpret_cast<VoidPtr>(const_cast<Char*>(kFsName)), epm_boot->Fs, rt_string_len(kFsName)); + + epm_boot->FsVersion = kNeFSVersionInteger; + epm_boot->LbaStart = start; + epm_boot->SectorSz = kNeFSSectorSz; + + rt_copy_memory(reinterpret_cast<VoidPtr>(const_cast<Char*>(kBlockName)), epm_boot->Name, rt_string_len(kBlockName)); + rt_copy_memory(reinterpret_cast<VoidPtr>(const_cast<Char*>(kEPMMagic)), epm_boot->Magic, rt_string_len(kEPMMagic)); + + Lba outEpmLba = kEPMBootBlockLba; + + Char buf[kNeFSSectorSz]; + + 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); + + break; + } + else + { + prevStart = ((BOOT_BLOCK_STRUCT*)buf)->LbaStart + ((BOOT_BLOCK_STRUCT*)buf)->LbaEnd; + } + + outEpmLba += sizeof(BOOT_BLOCK_STRUCT); + ++cnt; + } + } + + // disk isnt faulty and data has been fetched. + while (drive->fPacket.fPacketGood) + { + NFS_ROOT_PARTITION_BLOCK* part_block = (NFS_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 cUntitledHD = part_name; + + rt_copy_memory((VoidPtr)kNeFSIdent, (VoidPtr)part_block->Ident, + kNeFSIdentLen); + + rt_copy_memory((VoidPtr)cUntitledHD, (VoidPtr)part_block->PartitionName, + rt_string_len(cUntitledHD)); + + SizeT catalogCount = 0UL; + + SizeT sectorCount = drv_get_sector_count(); + SizeT diskSize = drv_get_size(); + + part_block->Kind = kNeFSPartitionTypeStandard; + part_block->StartCatalog = kNeFSCatalogStartAddress; + part_block->Flags = kNeFSPartitionTypeStandard; + part_block->CatalogCount = sectorCount / sizeof(NFS_CATALOG_STRUCT); + part_block->SectorCount = sectorCount; + part_block->DiskSize = diskSize; + part_block->FreeCatalog = sectorCount / sizeof(NFS_CATALOG_STRUCT); + + drive->fPacket.fPacketContent = fs_buf; + drive->fPacket.fPacketSize = sizeof(NFS_ROOT_PARTITION_BLOCK); + drive->fPacket.fPacketLba = kNeFSRootCatalogStartAddress; + + drive->fOutput(&drive->fPacket); + + kcout << "drive kind: " << drive->fDriveKind() << endl; + + kcout << "partition name: " << part_block->PartitionName << endl; + kcout << "start: " << hex_number(part_block->StartCatalog) << endl; + kcout << "number of catalogs: " << hex_number(part_block->CatalogCount) << endl; + kcout << "free catalog: " << hex_number(part_block->FreeCatalog) << endl; + kcout << "free sectors: " << hex_number(part_block->FreeSectors) << endl; + kcout << "sector size: " << hex_number(part_block->SectorSize) << endl; + + // write the root catalog. + this->CreateCatalog(kNeFSRoot, 0, kNeFSCatalogKindDir); + + return true; + } + + kcout << "partition block already exists.\r"; + + start += part_block->DiskSize; + + drive->fPacket.fPacketContent = fs_buf; + drive->fPacket.fPacketSize = sizeof(NFS_ROOT_PARTITION_BLOCK); + drive->fPacket.fPacketLba = start; + + drive->fInput(&drive->fPacket); + } +#endif // ZKA_EPM_SUPPORT + + return false; +} + +/// @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. +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; + + auto buf = new UInt8[size_of_data]; + rt_set_memory(buf, 0, size_of_data); + + rt_copy_memory(data, buf, size_of_data); + + auto drive = kMountpoint.A(); + + rt_copy_memory((VoidPtr) "fs/nefs-packet", drive.fPacket.fPacketMime, + rt_string_len("fs/nefs-packet")); + + auto catalog = this->GetCatalog(catalog_name); + + if (!catalog) + { + delete[] buf; + buf = nullptr; + return NO; + } + + auto startFork = (!is_rsrc_fork) ? catalog->DataFork + : catalog->ResourceFork; + + delete catalog; + catalog = nullptr; + + NFS_FORK_STRUCT* fork_data_input = new NFS_FORK_STRUCT(); + NFS_FORK_STRUCT prev_fork{}; + + kcout << hex_number(startFork) << endl; + + // sanity check of the fork position as the condition to run the loop. + while (startFork >= kNeFSCatalogStartAddress) + { + drive.fPacket.fPacketContent = fork_data_input; + drive.fPacket.fPacketSize = sizeof(NFS_FORK_STRUCT); + drive.fPacket.fPacketLba = startFork; + + drive.fInput(drive.fPacket); + + kcout << hex_number(fork_data_input->DataSize) << endl; + kcout << hex_number(size_of_data) << endl; + kcout << hex_number(fork_data_input->Flags) << endl; + kcout << fork_name << endl; + kcout << fork_data_input->ForkName << endl; + kcout << fork_data_input->CatalogName << endl; + kcout << catalog_name << endl; + + if ((fork_data_input->Flags & kNeFSFlagCreated) && + StringBuilder::Equals(fork_data_input->ForkName, fork_name) && + StringBuilder::Equals(fork_data_input->CatalogName, catalog_name) && + fork_data_input->DataSize == size_of_data) + { + // ===================================================== // + // Store the blob now. + // ===================================================== // + + drive.fPacket.fPacketContent = buf; + drive.fPacket.fPacketSize = size_of_data; + drive.fPacket.fPacketLba = fork_data_input->DataOffset; + + kcout << "data offset: " << hex_number(fork_data_input->DataOffset) << endl; + + drive.fOutput(drive.fPacket); + + kcout << "wrote data at offset: " << hex_number(fork_data_input->DataOffset) << endl; + + delete fork_data_input; + delete buf; + + return true; + } + + // stumble upon a fork, store it. + + prev_fork = *fork_data_input; + + startFork = fork_data_input->NextSibling; + } + + delete buf; + delete fork_data_input; + + return false; +} + +/// @brief +/// @param catalog_name the catalog name. +/// @return the newly found catalog. +_Output NFS_CATALOG_STRUCT* NeFileSystemParser::FindCatalog(_Input const Char* catalog_name, + Lba& out_lba, + Bool search_hidden, + Bool local_search) +{ + if (!catalog_name || + *catalog_name == 0) + return nullptr; + + NFS_ROOT_PARTITION_BLOCK part{0}; + auto& drive = kMountpoint.A(); + + rt_copy_memory((VoidPtr) "fs/nefs-packet", drive.fPacket.fPacketMime, + rt_string_len("fs/nefs-packet")); + + drive.fPacket.fPacketContent = ∂ + drive.fPacket.fPacketSize = sizeof(NFS_ROOT_PARTITION_BLOCK); + drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; + + drive.fInput(drive.fPacket); + + auto start_catalog_lba = kNeFSCatalogStartAddress; + const auto kStartCatalogList = start_catalog_lba; + + if (!StringBuilder::Equals(catalog_name, NeFileSystemHelper::Root()) && local_search) + { + Char parent_name[kNeFSNodeNameLen] = {0}; + + for (SizeT indexFill = 0; indexFill < rt_string_len(catalog_name); ++indexFill) + { + parent_name[indexFill] = catalog_name[indexFill]; + } + + SizeT indexReverseCopy = rt_string_len(parent_name); + + // zero character. + parent_name[--indexReverseCopy] = 0; + + // mandatory '/' character. + parent_name[--indexReverseCopy] = 0; + + while (parent_name[indexReverseCopy] != NeFileSystemHelper::Separator()) + { + parent_name[indexReverseCopy] = 0; + --indexReverseCopy; + } + + NFS_CATALOG_STRUCT* parent_catalog = this->FindCatalog(parent_name, out_lba); + + if (parent_catalog && + !StringBuilder::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) + { + return nullptr; + } + } + + NFS_CATALOG_STRUCT temporary_catalog{}; + +kNeFSSearchThroughCatalogList: + while (YES) + { + drive.fPacket.fPacketLba = start_catalog_lba; + drive.fPacket.fPacketContent = &temporary_catalog; + drive.fPacket.fPacketSize = sizeof(NFS_CATALOG_STRUCT); + + drive.fInput(drive.fPacket); + + kcout << temporary_catalog.Name << endl; + + if (StringBuilder::Equals(catalog_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; + } + + kcout << "Found available catalog at: " << hex_number(start_catalog_lba) << endl; + kcout << "Found available catalog at: " << temporary_catalog.Name << endl; + + NFS_CATALOG_STRUCT* catalog_ptr = new NFS_CATALOG_STRUCT(); + rt_copy_memory(&temporary_catalog, catalog_ptr, sizeof(NFS_CATALOG_STRUCT)); + + out_lba = start_catalog_lba; + return catalog_ptr; + } + + 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; +} + +/// @brief Get catalog from filesystem. +/// @param name the catalog's name/ +/// @return +_Output NFS_CATALOG_STRUCT* NeFileSystemParser::GetCatalog(_Input const Char* name) +{ + Lba unused = 0; + return this->FindCatalog(name, unused, YES); +} + +/// @brief Closes a catalog, (frees it). +/// @param catalog the catalog to close. +/// @return +Boolean NeFileSystemParser::CloseCatalog(_Input _Output NFS_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. +Boolean NeFileSystemParser::RemoveCatalog(_Input const Char* catalog_name) +{ + if (!catalog_name || + StringBuilder::Equals(catalog_name, NeFileSystemHelper::Root())) + { + err_global_get() = kErrorInternal; + return false; + } + + Lba out_lba = 0; + auto catalog = this->FindCatalog(catalog_name, out_lba); + + 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(NFS_CATALOG_STRUCT); // size of catalog. roughly the sector size. + drive.fPacket.fPacketContent = catalog; // the catalog itself. + + drive.fOutput(drive.fPacket); // send packet. + + Char partitionBlockBuf[sizeof(NFS_ROOT_PARTITION_BLOCK)] = {0}; + + drive.fPacket.fPacketLba = kNeFSRootCatalogStartAddress; + drive.fPacket.fPacketContent = partitionBlockBuf; + drive.fPacket.fPacketSize = sizeof(NFS_ROOT_PARTITION_BLOCK); + + drive.fInput(drive.fPacket); + + NFS_ROOT_PARTITION_BLOCK* part_block = + reinterpret_cast<NFS_ROOT_PARTITION_BLOCK*>(partitionBlockBuf); + + --part_block->CatalogCount; + ++part_block->FreeSectors; + + drive.fOutput(drive.fPacket); + + return true; + } + + delete catalog; + catalog = nullptr; + + return false; +} + +/// ***************************************************************** /// +/// Reading,Seek,Tell are unimplemented on catalogs, refer to forks I/O instead. +/// ***************************************************************** /// + +/***********************************************************************************/ +/// @brief Read the catalog data fork. +/// @param catalog +/// @param dataSz +/// @return +/***********************************************************************************/ + +VoidPtr NeFileSystemParser::ReadCatalog(_Input _Output NFS_CATALOG_STRUCT* catalog, + _Input Bool is_rsrc_fork, + _Input SizeT dataSz, + _Input const Char* forkName) +{ + if (!catalog) + { + err_global_get() = kErrorFileNotFound; + return nullptr; + } + + Lba dataForkLba = (!is_rsrc_fork) ? catalog->DataFork : catalog->ResourceFork; + Size dataForkSize = (!is_rsrc_fork) ? catalog->DataForkSize : catalog->ResourceForkSize; + + kcout << "catalog " << catalog->Name + << ", fork: " << hex_number(dataForkLba) << endl; + + NFS_FORK_STRUCT* fs_buf = new NFS_FORK_STRUCT(); + auto drive = kMountpoint.A(); + + rt_copy_memory((VoidPtr) "fs/nefs-packet", drive.fPacket.fPacketMime, + rt_string_len("fs/nefs-packet")); + + NFS_FORK_STRUCT* fs_fork_data = nullptr; + + while (dataForkLba > kNeFSCatalogStartAddress) + { + drive.fPacket.fPacketLba = dataForkLba; + drive.fPacket.fPacketSize = sizeof(NFS_FORK_STRUCT); + drive.fPacket.fPacketContent = fs_buf; + + drive.fInput(drive.fPacket); + + fs_fork_data = fs_buf; + + kcout << "ForkName: " << fs_fork_data->ForkName << endl; + kcout << "CatalogName: " << fs_fork_data->CatalogName << endl; + + if (StringBuilder::Equals(forkName, fs_fork_data->ForkName) && + StringBuilder::Equals(catalog->Name, fs_fork_data->CatalogName)) + break; + + dataForkLba = fs_fork_data->NextSibling; + } + + if (dataForkLba < kNeFSCatalogStartAddress) + { + delete fs_buf; + return nullptr; + } + + return fs_fork_data; +} + +/***********************************************************************************/ +/// @brief Seek in the data fork. +/// @param catalog the catalog offset. +/// @param off where to seek. +/// @return if the seeking was successful. +/***********************************************************************************/ + +bool NeFileSystemParser::Seek(_Input _Output NFS_CATALOG_STRUCT* catalog, SizeT off) +{ + if (!catalog) + { + err_global_get() = kErrorFileNotFound; + return false; + } + + err_global_get() = kErrorUnimplemented; + return false; +} + +/***********************************************************************************/ +/// @brief Tell where we are inside the data fork. +/// @param catalog +/// @return The position on the file. +/***********************************************************************************/ + +SizeT NeFileSystemParser::Tell(_Input _Output NFS_CATALOG_STRUCT* catalog) +{ + if (!catalog) + { + err_global_get() = kErrorFileNotFound; + return 0; + } + + err_global_get() = kErrorUnimplemented; + return 0; +} + +namespace Kernel::NeFS +{ + /***********************************************************************************/ + /// @brief Construct NeFS drives. + /***********************************************************************************/ + Boolean fs_init_nefs(Void) noexcept + { + kcout << "Creating A:\r"; + + kMountpoint.A() = io_construct_main_drive(); + + kcout << "Creating A: [ OK ]\r"; + + return true; + } +} // namespace Kernel::NeFS + +#endif // ifdef __FSKIT_INCLUDES_NEFS__ |
