From 408be791647c015c99963cc1b6d710f58d729dec Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Sat, 9 Aug 2025 08:56:53 +0100 Subject: refactor! rename `tooling` to `tools` feat: BenchKit improvements and libMsg authorship refactors. --- tools/dist/.keep | 0 tools/fsck.hefs.cc | 76 +++++++++ tools/fsck.hefs.json | 16 ++ tools/libmkfs/hefs.h | 116 +++++++++++++ tools/libmkfs/mkfs.h | 83 +++++++++ tools/mk_app.py | 80 +++++++++ tools/mk_fwrk.py | 92 ++++++++++ tools/mk_htman.py | 41 +++++ tools/mk_img.py | 41 +++++ tools/mkfs.hefs.cc | 197 ++++++++++++++++++++++ tools/mkfs.hefs.json | 16 ++ tools/rang.h | 467 +++++++++++++++++++++++++++++++++++++++++++++++++++ 12 files changed, 1225 insertions(+) create mode 100644 tools/dist/.keep create mode 100644 tools/fsck.hefs.cc create mode 100644 tools/fsck.hefs.json create mode 100644 tools/libmkfs/hefs.h create mode 100644 tools/libmkfs/mkfs.h create mode 100644 tools/mk_app.py create mode 100644 tools/mk_fwrk.py create mode 100644 tools/mk_htman.py create mode 100644 tools/mk_img.py create mode 100644 tools/mkfs.hefs.cc create mode 100644 tools/mkfs.hefs.json create mode 100644 tools/rang.h (limited to 'tools') diff --git a/tools/dist/.keep b/tools/dist/.keep new file mode 100644 index 00000000..e69de29b diff --git a/tools/fsck.hefs.cc b/tools/fsck.hefs.cc new file mode 100644 index 00000000..e3837b8a --- /dev/null +++ b/tools/fsck.hefs.cc @@ -0,0 +1,76 @@ +/* ------------------------------------------- + + Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#include +#include +#include +#include + +static uint16_t kNumericalBase = 10; + +int main(int argc, char** argv) { + if (argc < 2) { + mkfs::console_out() << "fsck: hefs: usage: fsck.hefs -i=" + << "\n"; + return EXIT_FAILURE; + } + + auto args = mkfs::detail::build_args(argc, argv); + + auto opt_disk = mkfs::get_option(args, "-i"); + + auto origin = mkfs::get_option(args, "-o"); + + if (opt_disk.empty()) { + mkfs::console_out() << "fsck: hefs: error: HeFS is empty! Exiting..." + << "\n"; + return EXIT_FAILURE; + } + + auto out_origin = 0L; + + if (!mkfs::detail::parse_signed(origin, out_origin, kNumericalBase)) { + mkfs::console_out() << "hefs: error: Invalid -o argument.\n"; + return EXIT_FAILURE; + } + + std::ifstream output_device(opt_disk, std::ios::binary); + + if (!output_device.good()) { + mkfs::console_out() << "hefs: error: Unable to open output device: " << opt_disk << "\n"; + return EXIT_FAILURE; + } + + output_device.seekg(out_origin); + + if (!output_device.good()) { + mkfs::console_out() << "hefs: error: Failed seek to origin.\n"; + return EXIT_FAILURE; + } + + mkfs::hefs::BootNode boot_node; + + std::memset(&boot_node, 0, sizeof(boot_node)); + + output_device.read(reinterpret_cast(&boot_node), sizeof(boot_node)); + + if (strncmp(boot_node.magic, kHeFSMagic, kHeFSMagicLen) != 0 || boot_node.sectorCount < 1 || + boot_node.sectorSize < kMkFsSectorSz) { + mkfs::console_out() << "hefs: error: Device is not an HeFS disk: " << opt_disk << "\n"; + return EXIT_FAILURE; + } + + if (boot_node.badSectors > kMkFsMaxBadSectors) { + mkfs::console_out() << "hefs: error: HeFS disk has too much bad sectors: " << opt_disk << "\n"; + return EXIT_FAILURE; + } + + mkfs::console_out() << "hefs: HeFS partition is is healthy, exiting...\n"; + + output_device.close(); + + return EXIT_SUCCESS; +} \ No newline at end of file diff --git a/tools/fsck.hefs.json b/tools/fsck.hefs.json new file mode 100644 index 00000000..d6ff4b0f --- /dev/null +++ b/tools/fsck.hefs.json @@ -0,0 +1,16 @@ +{ + "compiler_path": "g++", + "compiler_std": "c++20", + "headers_path": [ + "../" + ], + "sources_path": [ + "fsck.hefs.cc" + ], + "output_name": "./dist/fsck.hefs", + "cpp_macros": [ + "kFSCKHEFSVersion=0x0100", + "kFSCKHEFSVersionHighest=0x0100", + "kFSCKHEFSVersionLowest=0x0100" + ] +} \ No newline at end of file diff --git a/tools/libmkfs/hefs.h b/tools/libmkfs/hefs.h new file mode 100644 index 00000000..52bb3086 --- /dev/null +++ b/tools/libmkfs/hefs.h @@ -0,0 +1,116 @@ +/* ------------------------------------------- + + Copyright (C) 2024-2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include +#include + +#define kHeFSVersion (0x0101) +#define kHeFSMagic " HeFS" +#define kHeFSMagicLen (8) + +#define kHeFSFileNameLen (256U) +#define kHeFSPartNameLen (128U) + +#define kHeFSDefaultVolumeName u8"HeFS Volume" + +namespace mkfs::hefs { + +// Drive kinds +enum { + kHeFSHardDrive = 0xC0, // Hard Drive + kHeFSSolidStateDrive = 0xC1, // Solid State Drive + kHeFSOpticalDrive = 0x0C, // Blu-Ray/DVD + kHeFSMassStorageDevice = 0xCC, // USB + kHeFSScsiDrive = 0xC4, // SCSI Hard Drive + kHeFSFlashDrive = 0xC6, + kHeFSUnknown = 0xFF, // Unknown device. + kHeFSDriveCount = 8, +}; + +// Disk status +enum { + kHeFSStatusUnlocked = 0x18, + kHeFSStatusLocked, + kHeFSStatusError, + kHeFSStatusInvalid, + kHeFSStatusCount, +}; + +// Encodings +enum { + kHeFSEncodingFlagsUTF8 = 0x50, + kHeFSEncodingFlagsUTF16, + kHeFSEncodingFlagsUTF32, + kHeFSEncodingFlagsUTF16BE, + kHeFSEncodingFlagsUTF16LE, + kHeFSEncodingFlagsUTF32BE, + kHeFSEncodingFlagsUTF32LE, + kHeFSEncodingFlagsUTF8BE, + kHeFSEncodingFlagsUTF8LE, + kHeFSEncodingFlagsBinary, + kHeFSEncodingFlagsCount = 11, + kHeFSFlagsNone = 0, + kHeFSFlagsReadOnly = 0x100, + kHeFSFlagsHidden, + kHeFSFlagsSystem, + kHeFSFlagsArchive, + kHeFSFlagsDevice, + kHeFSFlagsCount = 7 +}; + +// Time type +using ATime = std::uint64_t; + +// File kinds +inline constexpr uint16_t kHeFSFileKindRegular = 0x00; +inline constexpr uint16_t kHeFSFileKindDirectory = 0x01; +inline constexpr uint16_t kHeFSFileKindBlock = 0x02; +inline constexpr uint16_t kHeFSFileKindCharacter = 0x03; +inline constexpr uint16_t kHeFSFileKindFIFO = 0x04; +inline constexpr uint16_t kHeFSFileKindSocket = 0x05; +inline constexpr uint16_t kHeFSFileKindSymbolicLink = 0x06; +inline constexpr uint16_t kHeFSFileKindUnknown = 0x07; +inline constexpr uint16_t kHeFSFileKindCount = 0x08; + +// Red-black tree colors +enum { + kHeFSInvalidColor = 0, + kHeFSRed = 100, + kHeFSBlack, + kHeFSColorCount, +}; + +// Time constants +inline constexpr ATime kHeFSTimeInvalid = 0x0000000000000000; +inline constexpr ATime kHeFSTimeMax = 0xFFFFFFFFFFFFFFFF - 1; + +// Boot Node +struct __attribute__((packed)) BootNode { + char magic[kHeFSMagicLen]{}; + char8_t volumeName[kHeFSPartNameLen]{}; + std::uint32_t version{}; + std::uint64_t badSectors{}; + std::uint64_t sectorCount{}; + std::uint64_t sectorSize{}; + std::uint32_t checksum{}; + std::uint8_t diskKind{}; + std::uint8_t encoding{}; + std::uint64_t startIND{}; + std::uint64_t endIND{}; + std::uint64_t indCount{}; + std::uint64_t diskSize{}; + std::uint16_t diskStatus{}; + std::uint16_t diskFlags{}; + std::uint16_t vid{}; + std::uint64_t startIN{}; + std::uint64_t endIN{}; + std::uint64_t startBlock{}; + std::uint64_t endBlock{}; + char pad[272]{}; +}; +} // namespace mkfs::hefs diff --git a/tools/libmkfs/mkfs.h b/tools/libmkfs/mkfs.h new file mode 100644 index 00000000..e729c29b --- /dev/null +++ b/tools/libmkfs/mkfs.h @@ -0,0 +1,83 @@ +/* ------------------------------------------- + + Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#pragma once + +#include +#include +#include +#include + +#define kMkFsSectorSz (512U) +#define kMkFsMaxBadSectors (128U) + +/// @internal +namespace mkfs { + +namespace detail { + /// @internal + /// @brief GB‐to‐byte conversion (use multiplication, not XOR). + inline constexpr size_t gib_cast(uint32_t gb) { + return static_cast(gb) * 1024ULL * 1024ULL * 1024ULL; + } + + inline bool parse_decimal(const std::string& opt, unsigned long long& out) { + if (opt.empty()) return false; + char* endptr = nullptr; + unsigned long long val = std::strtoull(opt.c_str(), &endptr, 10); + if (endptr == opt.c_str() || *endptr != '\0') return false; + out = val; + return true; + } + + inline bool parse_signed(const std::string& opt, long& out, int base = 10) { + out = 0L; + + if (opt.empty()) return true; + + char* endptr = nullptr; + long val = std::strtol(opt.c_str(), &endptr, base); + auto err = errno; + + if (err == ERANGE || err == EINVAL) return false; + if (endptr == opt.c_str() || *endptr != '\0') return false; + + out = val; + return true; + } + + inline std::string build_args(int argc, char** argv) { + std::string combined; + for (int i = 1; i < argc; ++i) { + combined += argv[i]; + combined += ' '; + } + return combined; + } +} // namespace detail + +/// @brief Helper function to get the option value from command line arguments. +template +inline std::basic_string get_option(const std::basic_string& args, + const std::basic_string& option) { + size_t pos = args.find(option + CharType('=')); + + if (pos != std::string::npos) { + size_t start = pos + option.length() + 1; + size_t end = args.find(' ', start); + return args.substr(start, end - start); + } + + return std::basic_string{}; +} + +inline auto console_out() -> std::ostream& { + std::ostream& conout = std::cout; + conout << rang::fg::red << "mkfs: " << rang::style::reset; + + return conout; +} +} // namespace mkfs \ No newline at end of file diff --git a/tools/mk_app.py b/tools/mk_app.py new file mode 100644 index 00000000..7f7cef17 --- /dev/null +++ b/tools/mk_app.py @@ -0,0 +1,80 @@ +#! /usr/bin/env python3 +# -*- coding: utf-8 -*- + +import os +import json +import sys + +def create_directory_structure(base_path, project_name): + # Define the directory structure + structure = { + project_name: { + "dist": { + ".keep": None + }, + "src": { + ".keep": None, + "CommandLine.cc": None, + }, + "vendor": { + ".keep": None + }, + ".keep": None, + f"{project_name}.json": {} + } + } + + def create_structure(path, structure): + for name, content in structure.items(): + current_path = os.path.join(path, name) + # Create directories or files based on the content type + if isinstance(content, dict) and current_path.endswith(".json") == False: + os.makedirs(current_path, exist_ok=True) + create_structure(current_path, content) + elif content is None: + # Create an empty file + with open(current_path, 'w') as f: + pass + + # Create the base directory + os.makedirs(base_path, exist_ok=True) + create_structure(base_path, structure) + + # Create the JSON file + proj_json_path = os.path.join(base_path, project_name, f"{project_name}.json") + + manifest = { + "compiler_path": "clang++", + "compiler_std": "c++20", + "headers_path": ["./", "../../../dev/kernel", "../../../public/frameworks/", "../../../dev/", "./"], + "sources_path": [ + + ], + "output_name": f"./dist/{project_name}", + "cpp_macros": [ + "kSampleFWVersion=0x0100", + "kSampleFWVersionHighest=0x0100", + "kSampleFWVersionLowest=0x0100", + "__NE_SDK__" + ] + } + + with open(proj_json_path, 'w') as json_file: + json.dump(manifest, json_file, indent=4) + + proj_cpp_path = os.path.join(base_path, project_name, f"src/CommandLine.cc") + + cpp_file = "#include \n\nSInt32 _NeMain(SInt32 argc, Char* argv[]) {\n\treturn EXIT_FAILURE;\n}" + + with open(proj_cpp_path, 'w') as cpp_file_io: + cpp_file_io.write(cpp_file) + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("HELP: mk_app.py ") + sys.exit(os.EX_CONFIG) + + base_path = os.getcwd() # Use the current working directory as the base path + create_directory_structure(base_path, sys.argv[1]) + + print("INFO: Application created successfully.") diff --git a/tools/mk_fwrk.py b/tools/mk_fwrk.py new file mode 100644 index 00000000..b2ef99ff --- /dev/null +++ b/tools/mk_fwrk.py @@ -0,0 +1,92 @@ +#! /usr/bin/env python3 +# -*- coding: utf-8 -*- + +import os +import json +import sys + +""" + Create directory structure for the framework. +""" +def create_directory_structure(base_path_fwrk, project_file_name, project_name): + # Define the directory structure + structure = { + project_name: { + "headers": { + ".keep": None + }, + "dist": { + ".keep": None + }, + "src": { + ".keep": None, + "DylibMain.cc": None, + }, + "xml": { + ".keep": None + }, + ".keep": None, + f"{project_file_name}.json": {} + } + } + + def create_structure(path, structure_in): + for name, content in structure_in.items(): + current_path = os.path.join(path, name) + # Create directories or files based on the content type + if isinstance(content, dict) and current_path.endswith(".json") == False: + os.makedirs(current_path, exist_ok=True) + create_structure(current_path, content) + elif content is None: + # Create an empty file + with open(current_path, 'w') as f: + pass + + # Create the base directory + os.makedirs(base_path_fwrk, exist_ok=True) + create_structure(base_path_fwrk, structure) + + # Create the JSON file + proj_json_path = os.path.join(base_path_fwrk, project_name, f"{project_file_name}.json") + + manifest = { + "compiler_path": "clang++", + "compiler_std": "c++20", + "headers_path": ["./", "../../../dev/kernel", "../../../public/frameworks/", "../../../dev/", "./"], + "sources_path": [ + + ], + "output_name": f"./dist/lib{project_name}.dylib", + "cpp_macros": [ + "kSampleFWVersion=0x0100", + "kSampleFWVersionHighest=0x0100", + "kSampleFWVersionLowest=0x0100", + "__NE_SDK__" + ] + } + + with open(proj_json_path, 'w') as json_file: + json.dump(manifest, json_file, indent=4) + + proj_cpp_path = os.path.join(base_path_fwrk, project_name, f"src/DylibMain.cc") + + cpp_file = "#include \n\nSInt32 _DylibAttach(SInt32 argc, Char* argv[]) {\n\treturn EXIT_FAILURE;\n}" + + with open(proj_cpp_path, 'w') as cpp_file_io: + cpp_file_io.write(cpp_file) + + xml_blob = f"\n" + proj_xml_path = os.path.join(base_path_fwrk, project_name, f"xml/app.xml") + + with open(proj_xml_path, 'w') as cpp_file_io: + cpp_file_io.write(xml_blob) + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("HELP: mk_fwrk.py ") + sys.exit(os.EX_CONFIG) + + base_path = os.getcwd() # Use the current working directory as the base path + create_directory_structure(base_path, sys.argv[1], sys.argv[1] + '.fwrk') + + print("INFO: Framework created successfully.") diff --git a/tools/mk_htman.py b/tools/mk_htman.py new file mode 100644 index 00000000..e865f7c5 --- /dev/null +++ b/tools/mk_htman.py @@ -0,0 +1,41 @@ +#! /usr/bin/env python3 +# -*- coding: utf-8 -*- + +import sys, os + +if __name__ == "__main__": + if len(sys.argv) != 2: + print("INFO: mk_htman.py ") + sys.exit(os.EX_CONFIG) + + manual_path = sys.argv[1] + if not os.path.exists(manual_path): + print(f"ERROR: Manual path '{manual_path}' does not exist.") + sys.exit(os.EX_NOINPUT) + + if os.path.isdir(manual_path): + print(f"ERROR: Manual path '{manual_path}' is a directory.") + sys.exit(os.EX_NOTDIR) + + if not manual_path.endswith('.man'): + print(f"ERROR: Manual path '{manual_path}' must end with '.man'") + sys.exit(os.EX_DATAERR) + + try: + with open(manual_path, 'r') as file: + content = file.read() + if not content.strip(): + print(f"ERROR: Manual file '{manual_path}' is empty.") + sys.exit(os.EX_DATAERR) + html_content = f"NeKernel Manual: {manual_path}
{content}
" + + html_path = manual_path.replace('.man', '.html') + + with open(html_path, 'w') as html_file: + html_file.write(html_content) + except IOError as e: + print(f"ERROR: Could not read manual file '{manual_path}': {e}") + sys.exit(os.EX_IOERR) + + print(f"INFO: Wrote manual '{manual_path}' to HTML.") + sys.exit(os.EX_OK) diff --git a/tools/mk_img.py b/tools/mk_img.py new file mode 100644 index 00000000..f0fa0609 --- /dev/null +++ b/tools/mk_img.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 +# -*- coding: utf-8 -*- + +import os +import sys +import subprocess +import glob as file_glob + +def copy_to_fat(image_path, source_dir): + if not os.path.isfile(image_path): + print(f"Error: FAT32 image {image_path} does not exist.") + sys.exit(1) + + if not os.path.isdir(source_dir): + print(f"Error: {source_dir} is not a valid directory.") + sys.exit(1) + + try: + files_to_copy = file_glob.glob(os.path.join(source_dir, "*")) + + if not files_to_copy: + print(f"Warning: No files found in {source_dir}, nothing to copy.") + sys.exit(1) + + command = ["mcopy", "-spm", "-i", image_path] + files_to_copy + ["::"] + subprocess.run(command, check=True) + except Exception as e: + print(f"Error: failed: {e}") + sys.exit(1) + +if __name__ == "__main__": + if len(sys.argv) != 3: + print("HELP: mk_img.py ") + sys.exit(1) + + image_path = sys.argv[1] + source_dir = sys.argv[2] + + copy_to_fat(image_path, source_dir) + + print("INFO: Image created successfully.") diff --git a/tools/mkfs.hefs.cc b/tools/mkfs.hefs.cc new file mode 100644 index 00000000..bf790598 --- /dev/null +++ b/tools/mkfs.hefs.cc @@ -0,0 +1,197 @@ +/* ------------------------------------------- + + Copyright (C) 2025, Amlal El Mahrouss, all rights reserved. + +------------------------------------------- */ + +#include +#include +#include +#include +#include +#include +#include + +static size_t kDiskSize = mkfs::detail::gib_cast(4UL); +static uint16_t kVersion = kHeFSVersion; +static std::u8string kLabel; +static size_t kSectorSize = 512; +static uint16_t kNumericalBase = 10; + +int main(int argc, char** argv) { + if (argc < 2) { + mkfs::console_out() + << "hefs: usage: mkfs.hefs -L=