From 989092c04649ff07bbb552b2ccc7c9f44569b75c Mon Sep 17 00:00:00 2001 From: Amlal El Mahrouss Date: Thu, 24 Jul 2025 10:31:18 +0100 Subject: fix: tooling: fix 'fsck.hefs', 'mkfs.hefs', and 'mk_img.py' tools. Signed-off-by: Amlal El Mahrouss --- tooling/fsck.hefs.cc | 27 +++++++++++++++++++++++++-- tooling/libmkfs/mkfs.h | 11 +++++++++-- tooling/mk_img.py | 10 +++++----- tooling/mkfs.hefs.cc | 35 ++++++++++++++++++++++------------- 4 files changed, 61 insertions(+), 22 deletions(-) diff --git a/tooling/fsck.hefs.cc b/tooling/fsck.hefs.cc index e59cacc0..9d0b3e03 100644 --- a/tooling/fsck.hefs.cc +++ b/tooling/fsck.hefs.cc @@ -9,6 +9,8 @@ #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=" @@ -20,12 +22,21 @@ int main(int argc, char** 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()) { @@ -33,9 +44,18 @@ int main(int argc, char** argv) { 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) { @@ -48,6 +68,9 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - mkfs::console_out() << "hefs: HeFS partition is is healthy, exiting...\r"; + 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/tooling/libmkfs/mkfs.h b/tooling/libmkfs/mkfs.h index 1765e1d4..31f7a26a 100644 --- a/tooling/libmkfs/mkfs.h +++ b/tooling/libmkfs/mkfs.h @@ -34,10 +34,17 @@ namespace detail { } inline bool parse_signed(const std::string& opt, long& out, int base = 10) { - if (opt.empty()) return false; + out = 0L; + + if (opt.empty()) return true; + char* endptr = nullptr; long val = std::strtol(opt.c_str(), &endptr, base); - if (endptr == opt.c_str() || *endptr != '\0' || val < 0) return false; + auto err = errno; + + if (err == ERANGE || err == EINVAL) return false; + if (endptr == opt.c_str() || *endptr != '\0') return false; + out = val; return true; } diff --git a/tooling/mk_img.py b/tooling/mk_img.py index 28af22e3..f0fa0609 100755 --- a/tooling/mk_img.py +++ b/tooling/mk_img.py @@ -4,7 +4,7 @@ import os import sys import subprocess -import glob +import glob as file_glob def copy_to_fat(image_path, source_dir): if not os.path.isfile(image_path): @@ -16,11 +16,11 @@ def copy_to_fat(image_path, source_dir): sys.exit(1) try: - files_to_copy = glob.glob(os.path.join(source_dir, "*")) - + 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.") - return + 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) diff --git a/tooling/mkfs.hefs.cc b/tooling/mkfs.hefs.cc index 3c2727fd..b90b0fc5 100644 --- a/tooling/mkfs.hefs.cc +++ b/tooling/mkfs.hefs.cc @@ -16,6 +16,7 @@ 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) { @@ -37,7 +38,7 @@ int main(int argc, char** argv) { auto opt_s = mkfs::get_option(args, "-s"); long parsed_s = 0; - if (!mkfs::detail::parse_signed(opt_s, parsed_s, 10) || parsed_s == 0) { + if (!mkfs::detail::parse_signed(opt_s, parsed_s, kNumericalBase) || parsed_s == 0) { mkfs::console_out() << "hefs: error: Invalid sector size \"" << opt_s << "\". Must be a positive integer.\n"; return EXIT_FAILURE; @@ -86,28 +87,33 @@ int main(int argc, char** argv) { long start_block = 0, end_block = 0; long start_in = 0, end_in = 0; - if (!mkfs::detail::parse_signed(opt_b, start_ind, 16)) { - mkfs::console_out() << "hefs: error: Invalid -b argument.\n"; + if (!mkfs::detail::parse_signed(opt_b, start_ind, kNumericalBase)) { + mkfs::console_out() << "hefs: error: Invalid -b argument.\n"; return EXIT_FAILURE; } - if (!mkfs::detail::parse_signed(opt_e, end_ind, 16) || end_ind <= start_ind) { - mkfs::console_out() << "hefs: error: Invalid or out-of-range -e argument.\n"; + + if (!mkfs::detail::parse_signed(opt_e, end_ind, kNumericalBase) || end_ind <= start_ind) { + mkfs::console_out() << "hefs: error: Invalid or out-of-range -e argument.\n"; return EXIT_FAILURE; } - if (!mkfs::detail::parse_signed(opt_bs, start_block, 16)) { - mkfs::console_out() << "hefs: error: Invalid -bs argument.\n"; + + if (!mkfs::detail::parse_signed(opt_bs, start_block, kNumericalBase)) { + mkfs::console_out() << "hefs: error: Invalid -bs argument.\n"; return EXIT_FAILURE; } - if (!mkfs::detail::parse_signed(opt_be, end_block, 16) || end_block <= start_block) { - mkfs::console_out() << "hefs: error: Invalid or out-of-range -be argument.\n"; + + if (!mkfs::detail::parse_signed(opt_be, end_block, kNumericalBase) || end_block <= start_block) { + mkfs::console_out() << "hefs: error: Invalid or out-of-range -be argument.\n"; return EXIT_FAILURE; } - if (!mkfs::detail::parse_signed(opt_is, start_in, 16)) { - mkfs::console_out() << "hefs: error: Invalid -is argument.\n"; + + if (!mkfs::detail::parse_signed(opt_is, start_in, kNumericalBase)) { + mkfs::console_out() << "hefs: error: Invalid -is argument.\n"; return EXIT_FAILURE; } - if (!mkfs::detail::parse_signed(opt_ie, end_in, 16) || end_in <= start_in) { - mkfs::console_out() << "hefs: error: Invalid or out-of-range -ie argument.\n"; + + if (!mkfs::detail::parse_signed(opt_ie, end_in, kNumericalBase) || end_in <= start_in) { + mkfs::console_out() << "hefs: error: Invalid or out-of-range -ie argument.\n"; return EXIT_FAILURE; } @@ -132,6 +138,7 @@ int main(int argc, char** argv) { boot_node.encoding = mkfs::hefs::kHeFSEncodingFlagsUTF8; boot_node.diskSize = kDiskSize; boot_node.sectorSize = kSectorSize; + boot_node.sectorCount = kDiskSize / kSectorSize; boot_node.startIND = static_cast(start_ind) + sizeof(mkfs::hefs::BootNode); boot_node.endIND = static_cast(end_ind); boot_node.startIN = static_cast(start_in); @@ -143,6 +150,7 @@ int main(int argc, char** argv) { static_assert(sizeof(boot_node.magic) >= kHeFSMagicLen, "BootNode::magic too small to hold kHeFSMagicLen"); + std::memset(boot_node.magic, 0, sizeof(boot_node.magic)); size_t magic_copy = (sizeof(boot_node.magic) < kHeFSMagicLen - 1) ? sizeof(boot_node.magic) : (kHeFSMagicLen - 1); @@ -184,5 +192,6 @@ int main(int argc, char** argv) { output_device.close(); mkfs::console_out() << "hefs: info: Wrote filesystem to output device: " << output_path << "\n"; + return EXIT_SUCCESS; } -- cgit v1.2.3