diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2026-03-02 14:59:58 +0100 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2026-03-02 15:01:10 +0100 |
| commit | 941e0dfbb41ac539178503351afc0f09de442720 (patch) | |
| tree | 76f56d71783d57a8c90457b28515cb10ad5019fb /tools/chk.hefs.cpp | |
| parent | bb83a254036ec708a0840313854d0ff5b0f84eb9 (diff) | |
chore: OpenHeFS and MKFS tweaks.
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
Diffstat (limited to 'tools/chk.hefs.cpp')
| -rw-r--r-- | tools/chk.hefs.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
diff --git a/tools/chk.hefs.cpp b/tools/chk.hefs.cpp new file mode 100644 index 00000000..d9141f1f --- /dev/null +++ b/tools/chk.hefs.cpp @@ -0,0 +1,73 @@ +// Copyright 2024-2026, Amlal El Mahrouss (amlal@nekernel.org) +// Licensed under the Apache License, Version 2.0 (see LICENSE file) +// Official repository: https://github.com/nekernel-org/nekernel + +#include <tools/libmkfs/mkfs.hpp> +#include <tools/libmkfs/openhefs.hpp> +#include <cstdlib> +#include <fstream> + +/// @note decimal base. +static uint16_t kNumericalBase = 10; +static auto kMinArgs = 2; + +int main(int argc, char** argv) { + if (argc < kMinArgs) { + mkfs::console_out() << "chk: hefs: usage: chk.hefs -in=<input_device> -b=<origin>" + << "\n"; + return EXIT_FAILURE; + } + + auto args = mkfs::detail::build_args(argc, argv); + auto opt_disk = mkfs::get_option<char>(args, "in"); + auto origin = mkfs::get_option<char>(args, "b"); + + if (opt_disk.empty()) { + mkfs::console_out() << "chk: hefs: error: OpenHeFS partition is empty! Exiting..." + << "\n"; + return EXIT_FAILURE; + } + + auto out_origin = 0UL; + + if (!mkfs::detail::parse_signed(origin, out_origin, kNumericalBase)) { + mkfs::console_out() << "chk: hefs: error: Invalid -org=<dec> argument.\n"; + return EXIT_FAILURE; + } + + std::ifstream output_device(opt_disk, std::ios::binary); + + if (!output_device.good()) { + mkfs::console_out() << "chk: 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() << "chk: hefs: error: Failed seek to origin.\n"; + return EXIT_FAILURE; + } + + // @note use modern intializer list here to empty out the fields according to the struct layout. + mkfs::hefs::BootNode boot_node{}; + + output_device.read(reinterpret_cast<char*>(&boot_node), sizeof(boot_node)); + + if (strncmp(boot_node.magic, kOpenHeFSMagic, kOpenHeFSMagicLen) != 0 || + boot_node.sectorCount < 1 || boot_node.sectorSize < kMkFsSectorSz) { + mkfs::console_out() << "chk: hefs: error: Device does not contain an OpenHeFS disk: " << opt_disk + << "\n"; + return EXIT_FAILURE; + } + + if (boot_node.badSectors > kMkFsMaxBadSectors) { + mkfs::console_out() << "chk: hefs: error: OpenHeFS disk has too much bad sectors: " << opt_disk + << "\n"; + return EXIT_FAILURE; + } + + mkfs::console_out() << "chk: hefs: OpenHeFS partition is healthy. Exiting...\n"; + + return EXIT_SUCCESS; +} |
