diff options
| author | Amlal El Mahrouss <amlal@nekernel.org> | 2025-06-04 14:41:29 +0200 |
|---|---|---|
| committer | Amlal El Mahrouss <amlal@nekernel.org> | 2025-06-04 14:41:29 +0200 |
| commit | e4c4041182480e96d52d289204d7f5363e6be3e4 (patch) | |
| tree | 3c63aaddd4f8a828ea86ce5cd6cacc1faea773a0 | |
| parent | b5add3bcf5580a2b2384f414b2df7350f4ded786 (diff) | |
meta: Ran `./format.sh`
Signed-off-by: Amlal El Mahrouss <amlal@nekernel.org>
| -rw-r--r-- | dev/boot/BootKit/HW/SATA.h | 2 | ||||
| -rw-r--r-- | dev/boot/src/HEL/AMD64/BootATA.cc | 13 | ||||
| -rw-r--r-- | dev/kernel/KernelKit/ThreadLocalStorage.h | 2 | ||||
| -rw-r--r-- | dev/kernel/src/AsciiUtils.cc | 55 | ||||
| -rw-r--r-- | dev/kernel/src/CxxAbi-ARM64.cc | 2 | ||||
| -rw-r--r-- | dev/kernel/src/ThreadLocalStorage.cc | 3 | ||||
| -rw-r--r-- | tooling/fsck.hefs.cc | 2 | ||||
| -rw-r--r-- | tooling/mkfs.hefs.cc | 383 |
8 files changed, 219 insertions, 243 deletions
diff --git a/dev/boot/BootKit/HW/SATA.h b/dev/boot/BootKit/HW/SATA.h index ebb1151e..d880c3af 100644 --- a/dev/boot/BootKit/HW/SATA.h +++ b/dev/boot/BootKit/HW/SATA.h @@ -19,7 +19,7 @@ class BootDeviceSATA final { NE_COPY_DEFAULT(BootDeviceSATA) - struct SATATrait final : public Device::Trait { + struct SATATrait final : public Device::Trait { Kernel::Boolean mErr{false}; Kernel::Boolean mDetected{false}; diff --git a/dev/boot/src/HEL/AMD64/BootATA.cc b/dev/boot/src/HEL/AMD64/BootATA.cc index e5e0d8c2..25810222 100644 --- a/dev/boot/src/HEL/AMD64/BootATA.cc +++ b/dev/boot/src/HEL/AMD64/BootATA.cc @@ -88,7 +88,8 @@ ATAInit_Retry: /// fetch serial info /// model, speed, number of sectors... - while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)); + while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)) + ; for (SizeT indexData = 0ul; indexData < kATADataLen; ++indexData) { kATAData[indexData] = rt_in16(IO + ATA_REG_DATA); @@ -114,14 +115,15 @@ Void boot_ata_read(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeASCII* Buf, rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + SectorSz) / SectorSz)); - rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF); + rt_out8(IO + ATA_REG_LBA0, (Lba) &0xFF); rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_READ_PIO); - while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)); + while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)) + ; for (SizeT IndexOff = 0; IndexOff < Size; IndexOff += 2) { boot_ata_wait_io(IO); @@ -147,14 +149,15 @@ Void boot_ata_write(UInt64 Lba, UInt16 IO, UInt8 Master, CharacterTypeASCII* Buf rt_out8(IO + ATA_REG_SEC_COUNT0, ((Size + (SectorSz)) / SectorSz)); - rt_out8(IO + ATA_REG_LBA0, (Lba) & 0xFF); + rt_out8(IO + ATA_REG_LBA0, (Lba) &0xFF); rt_out8(IO + ATA_REG_LBA1, (Lba) >> 8); rt_out8(IO + ATA_REG_LBA2, (Lba) >> 16); rt_out8(IO + ATA_REG_LBA3, (Lba) >> 24); rt_out8(IO + ATA_REG_COMMAND, ATA_CMD_WRITE_PIO); - while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)); + while (!(rt_in8(IO + ATA_REG_STATUS) & ATA_SR_DRQ)) + ; for (SizeT IndexOff = 0; IndexOff < Size; IndexOff += 2) { boot_ata_wait_io(IO); diff --git a/dev/kernel/KernelKit/ThreadLocalStorage.h b/dev/kernel/KernelKit/ThreadLocalStorage.h index 35c17db0..1b8e4821 100644 --- a/dev/kernel/KernelKit/ThreadLocalStorage.h +++ b/dev/kernel/KernelKit/ThreadLocalStorage.h @@ -28,7 +28,7 @@ struct THREAD_INFORMATION_BLOCK; /// Located in GS on AMD64, other architectures have their own stuff. (64x0, 32x0, ARM64) struct PACKED THREAD_INFORMATION_BLOCK final { Kernel::Char Cookie[kTLSCookieLen]{0}; //! Thread Magic Number. - Kernel::VoidPtr UserData{nullptr}; //! Thread Information Record (User defined canary structure) + Kernel::VoidPtr UserData{nullptr}; //! Thread Information Record (User defined canary structure) }; ///! @brief Cookie Sanity check. diff --git a/dev/kernel/src/AsciiUtils.cc b/dev/kernel/src/AsciiUtils.cc index 087b6d5f..bfc56aec 100644 --- a/dev/kernel/src/AsciiUtils.cc +++ b/dev/kernel/src/AsciiUtils.cc @@ -8,40 +8,35 @@ namespace Kernel { -STATIC Int rt_copy_memory_safe(const voidPtr src, voidPtr dst, Size len, Size dst_size); +STATIC Int rt_copy_memory_safe(const voidPtr src, voidPtr dst, Size len, Size dst_size); STATIC voidPtr rt_set_memory_safe(voidPtr dst, UInt32 value, Size len, Size dst_size); Int32 rt_string_cmp(const Char* src, const Char* cmp, Size size) { for (Size i = 0; i < size; ++i) { - if (src[i] != cmp[i]) - return static_cast<Int32>(src[i]) - static_cast<Int32>(cmp[i]); + if (src[i] != cmp[i]) return static_cast<Int32>(src[i]) - static_cast<Int32>(cmp[i]); } return 0; } SizeT rt_string_len(const Char* str, SizeT max_len) { SizeT len = 0; - while (len < max_len && str[len] != '\0') - ++len; + while (len < max_len && str[len] != '\0') ++len; return len; } Size rt_string_len(const Char* ptr) { Size cnt = 0; - while (ptr[cnt] != '\0') - ++cnt; + while (ptr[cnt] != '\0') ++cnt; return cnt; } const Char* rt_alloc_string(const Char* src) { - SizeT slen = rt_string_len(src); + SizeT slen = rt_string_len(src); Char* buffer = new Char[slen + 1]; if (!buffer) return nullptr; if (rt_copy_memory_safe(reinterpret_cast<voidPtr>(const_cast<Char*>(src)), - reinterpret_cast<voidPtr>(buffer), - slen, - slen + 1) < 0) { + reinterpret_cast<voidPtr>(buffer), slen, slen + 1) < 0) { delete[] buffer; return nullptr; } @@ -59,17 +54,15 @@ STATIC Int rt_copy_memory_safe(const voidPtr src, voidPtr dst, Size len, Size ds } auto s = reinterpret_cast<const UInt8*>(src); auto d = reinterpret_cast<UInt8*>(dst); - for (Size i = 0; i < len; ++i) - d[i] = s[i]; + for (Size i = 0; i < len; ++i) d[i] = s[i]; return static_cast<Int>(len); } STATIC voidPtr rt_set_memory_safe(voidPtr dst, UInt32 value, Size len, Size dst_size) { if (!dst || len > dst_size) return nullptr; - auto p = reinterpret_cast<UInt8*>(dst); + auto p = reinterpret_cast<UInt8*>(dst); UInt8 v = static_cast<UInt8>(value & 0xFF); - for (Size i = 0; i < len; ++i) - p[i] = v; + for (Size i = 0; i < len; ++i) p[i] = v; return dst; } @@ -80,12 +73,12 @@ Void rt_zero_memory(voidPtr pointer, Size len) { #ifdef __NE_ENFORCE_DEPRECATED_WARNINGS [[deprecated("Use rt_set_memory_safe instead")]] #endif -voidPtr rt_set_memory(voidPtr src, UInt32 value, Size len) { +voidPtr +rt_set_memory(voidPtr src, UInt32 value, Size len) { if (!src) return nullptr; - auto p = reinterpret_cast<UInt8*>(src); + auto p = reinterpret_cast<UInt8*>(src); UInt8 v = static_cast<UInt8>(value & 0xFF); - for (Size i = 0; i < len; ++i) - p[i] = v; + for (Size i = 0; i < len; ++i) p[i] = v; return src; } @@ -97,13 +90,11 @@ Int rt_copy_memory(const voidPtr src, voidPtr dst, Size len) { auto s = reinterpret_cast<const UInt8*>(src); auto d = reinterpret_cast<UInt8*>(dst); - for (Size i = 0; i < len; ++i) - d[i] = s[i]; + for (Size i = 0; i < len; ++i) d[i] = s[i]; return static_cast<Int>(len); } - Int32 rt_to_uppercase(Int32 ch) { return (ch >= 'a' && ch <= 'z') ? ch - 0x20 : ch; } @@ -113,9 +104,7 @@ Int32 rt_to_lower(Int32 ch) { } Int32 rt_is_alnum(Int32 ch) { - return (ch >= 'a' && ch <= 'z') || - (ch >= 'A' && ch <= 'Z') || - (ch >= '0' && ch <= '9'); + return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z') || (ch >= '0' && ch <= '9'); } Boolean rt_is_space(Char ch) { @@ -141,18 +130,17 @@ Bool rt_to_string(Char* str, UInt64 value, Int32 base) { str[i] = '\0'; // in-place for (Int j = 0; j < i / 2; ++j) { - Char tmp = str[j]; - str[j] = str[i - j - 1]; + Char tmp = str[j]; + str[j] = str[i - j - 1]; str[i - j - 1] = tmp; } #endif return true; } - VoidPtr rt_string_in_string(const Char* haystack, const Char* needle) { SizeT needle_len = rt_string_len(needle); - SizeT hay_len = rt_string_len(haystack); + SizeT hay_len = rt_string_len(haystack); if (needle_len > hay_len) return nullptr; for (SizeT i = 0; i <= hay_len - needle_len; ++i) { @@ -173,11 +161,10 @@ Int32 rt_strcmp(const Char* a, const Char* b) { while (a[i] != '\0' && b[i] != '\0' && a[i] == b[i]) { ++i; } - return static_cast<Int32>(static_cast<UInt8>(a[i]) - - static_cast<UInt8>(b[i])); + return static_cast<Int32>(static_cast<UInt8>(a[i]) - static_cast<UInt8>(b[i])); } - // @uses the deprecated version callers should ensure 'len' is valid. +// @uses the deprecated version callers should ensure 'len' is valid. extern "C" void* memset(void* dst, int c, long long unsigned int len) { return Kernel::rt_set_memory(dst, c, static_cast<Size>(len)); } @@ -191,4 +178,4 @@ extern "C" Kernel::Int32 strcmp(const char* a, const char* b) { return Kernel::rt_strcmp(a, b); } -} +} // namespace Kernel diff --git a/dev/kernel/src/CxxAbi-ARM64.cc b/dev/kernel/src/CxxAbi-ARM64.cc index d351b024..e91eb958 100644 --- a/dev/kernel/src/CxxAbi-ARM64.cc +++ b/dev/kernel/src/CxxAbi-ARM64.cc @@ -37,7 +37,7 @@ EXTERN_C void __cxa_finalize(void* f) { while (i--) { if (__atexit_funcs[i].destructor_func) { (*__atexit_funcs[i].destructor_func)(); - __atexit_funcs[i].destructor_func = 0; + __atexit_funcs[i].destructor_func = 0; }; } diff --git a/dev/kernel/src/ThreadLocalStorage.cc b/dev/kernel/src/ThreadLocalStorage.cc index 88fefee4..03a71f1a 100644 --- a/dev/kernel/src/ThreadLocalStorage.cc +++ b/dev/kernel/src/ThreadLocalStorage.cc @@ -30,7 +30,8 @@ Boolean tls_check_tib(THREAD_INFORMATION_BLOCK* tib_ptr) { if (!tib_ptr) return false; return tib_ptr->Cookie[kCookieMag0Idx] == kCookieMag0 && - tib_ptr->Cookie[kCookieMag1Idx] == kCookieMag1 && tib_ptr->Cookie[kCookieMag2Idx] == kCookieMag2; + tib_ptr->Cookie[kCookieMag1Idx] == kCookieMag1 && + tib_ptr->Cookie[kCookieMag2Idx] == kCookieMag2; } /** diff --git a/tooling/fsck.hefs.cc b/tooling/fsck.hefs.cc index 2898f09b..37dfbdd7 100644 --- a/tooling/fsck.hefs.cc +++ b/tooling/fsck.hefs.cc @@ -15,7 +15,7 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - (void)(argv); + (void) (argv); return EXIT_SUCCESS; }
\ No newline at end of file diff --git a/tooling/mkfs.hefs.cc b/tooling/mkfs.hefs.cc index ef6ceb32..3960fa5e 100644 --- a/tooling/mkfs.hefs.cc +++ b/tooling/mkfs.hefs.cc @@ -6,228 +6,213 @@ #include <tooling/hefs.h> #include <tooling/mkfs.h> +#include <algorithm> #include <cstdlib> #include <cstring> #include <fstream> #include <limits> -#include <algorithm> namespace detail { /// @internal /// @brief GB‐to‐byte conversion (use multiplication, not XOR). static constexpr size_t gib_cast(uint32_t gb) { - return static_cast<size_t>(gb) * 1024ULL * 1024ULL * 1024ULL; + return static_cast<size_t>(gb) * 1024ULL * 1024ULL * 1024ULL; } } // namespace detail -static size_t kDiskSize = detail::gib_cast(4UL); -static uint16_t kVersion = kHeFSVersion; -static std::u8string kLabel; +static size_t kDiskSize = detail::gib_cast(4UL); +static uint16_t kVersion = kHeFSVersion; +static std::u8string kLabel; static size_t kSectorSize = 512; static 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; + 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; } static bool parse_signed(const std::string& opt, long& out, int base = 10) { - if (opt.empty()) return false; - char* endptr = nullptr; - long val = std::strtol(opt.c_str(), &endptr, base); - if (endptr == opt.c_str() || *endptr != '\0' || val < 0) return false; - out = val; - return true; + if (opt.empty()) return false; + char* endptr = nullptr; + long val = std::strtol(opt.c_str(), &endptr, base); + if (endptr == opt.c_str() || *endptr != '\0' || val < 0) return false; + out = val; + return true; } static std::string build_args(int argc, char** argv) { - std::string combined; - for (int i = 1; i < argc; ++i) { - combined += argv[i]; - combined += ' '; - } - return combined; + std::string combined; + for (int i = 1; i < argc; ++i) { + combined += argv[i]; + combined += ' '; + } + return combined; } int main(int argc, char** argv) { - if (argc < 2) { - mkfs::console_out() - << "hefs: usage: mkfs.hefs -L=<label> -s=<sector_size> -b=<ind_start> -e=<ind_end> " - "-bs=<block_start> -be=<block_end> -is=<in_start> -ie=<in_end> " - "-S=<disk_size_GB> -o=<output_device>\n"; - - return EXIT_FAILURE; - } - - std::string args = build_args(argc, argv); - - auto output_path = mkfs::get_option<char>(args, "-o"); - if (output_path.empty()) { - mkfs::console_out() << "hefs: error: Missing -o <output_device> argument.\n"; - return EXIT_FAILURE; - } - - auto opt_s = mkfs::get_option<char>(args, "-s"); - long parsed_s = 0; - if (!parse_signed(opt_s, parsed_s, 10) || parsed_s == 0) { - mkfs::console_out() - << "hefs: error: Invalid sector size \"" << opt_s - << "\". Must be a positive integer.\n"; - return EXIT_FAILURE; - } - - if ((parsed_s & (parsed_s - 1)) != 0) { - mkfs::console_out() - << "hefs: error: Sector size \"" << parsed_s - << "\" is not a power of two.\n"; - return EXIT_FAILURE; - } - kSectorSize = static_cast<size_t>(parsed_s); - - auto opt_L = mkfs::get_option<char>(args, "-L"); - if (!opt_L.empty()) { - kLabel.clear(); - for (char c : opt_L) kLabel.push_back(static_cast<char8_t>(c)); - } else { - kLabel.clear(); - for (size_t i = 0; i < kHeFSPartNameLen && kHeFSDefaultVolumeName[i] != u'\0'; ++i) { - kLabel.push_back(static_cast<char8_t>(kHeFSDefaultVolumeName[i])); - } - } - - auto opt_S = mkfs::get_option<char>(args, "-S"); - unsigned long long gb = 0; - if (!parse_decimal(opt_S, gb) || gb == 0ULL) { - mkfs::console_out() - << "hefs: error: Invalid disk size \"" << opt_S - << "\". Must be a positive integer.\n"; - return EXIT_FAILURE; - } - unsigned long long max_gb = - std::numeric_limits<uint64_t>::max() / (1024ULL * 1024ULL * 1024ULL); - if (gb > max_gb) { - mkfs::console_out() - << "hefs: error: Disk size \"" << gb << "GB\" is too large.\n"; - return EXIT_FAILURE; - } - kDiskSize = static_cast<size_t>(gb * 1024ULL * 1024ULL * 1024ULL); - - auto opt_b = mkfs::get_option<char>(args, "-b"); - auto opt_e = mkfs::get_option<char>(args, "-e"); - auto opt_bs = mkfs::get_option<char>(args, "-bs"); - auto opt_be = mkfs::get_option<char>(args, "-be"); - auto opt_is = mkfs::get_option<char>(args, "-is"); - auto opt_ie = mkfs::get_option<char>(args, "-ie"); - - long start_ind = 0, end_ind = 0; - long start_block = 0, end_block = 0; - long start_in = 0, end_in = 0; - - if (!parse_signed(opt_b, start_ind, 16)) { - mkfs::console_out() << "hefs: error: Invalid -b <hex> argument.\n"; - return EXIT_FAILURE; - } - if (!parse_signed(opt_e, end_ind, 16) || end_ind <= start_ind) { - mkfs::console_out() - << "hefs: error: Invalid or out-of-range -e <hex> argument.\n"; - return EXIT_FAILURE; - } - if (!parse_signed(opt_bs, start_block, 16)) { - mkfs::console_out() << "hefs: error: Invalid -bs <hex> argument.\n"; - return EXIT_FAILURE; - } - if (!parse_signed(opt_be, end_block, 16) || end_block <= start_block) { - mkfs::console_out() - << "hefs: error: Invalid or out-of-range -be <hex> argument.\n"; - return EXIT_FAILURE; - } - if (!parse_signed(opt_is, start_in, 16)) { - mkfs::console_out() << "hefs: error: Invalid -is <hex> argument.\n"; - return EXIT_FAILURE; - } - if (!parse_signed(opt_ie, end_in, 16) || end_in <= start_in) { - mkfs::console_out() - << "hefs: error: Invalid or out-of-range -ie <hex> argument.\n"; - return EXIT_FAILURE; - } - - if (static_cast<size_t>(end_block) * kSectorSize > kDiskSize || - static_cast<size_t>(end_ind) > kDiskSize || - static_cast<size_t>(end_in) > kDiskSize) { - mkfs::console_out() << "hefs: error: One or more ranges exceed disk size.\n"; - return EXIT_FAILURE; - } - - std::ofstream output_device(output_path, std::ios::binary); - if (!output_device.good()) { - mkfs::console_out() - << "hefs: error: Unable to open output device: " << output_path << "\n"; - return EXIT_FAILURE; - } - - mkfs::hefs::BootNode boot_node; - std::memset(&boot_node, 0, sizeof(boot_node)); - - boot_node.version = kVersion; - boot_node.diskKind = mkfs::hefs::kHeFSHardDrive; - boot_node.encoding = mkfs::hefs::kHeFSEncodingFlagsUTF8; - boot_node.diskSize = kDiskSize; - boot_node.sectorSize = kSectorSize; - boot_node.startIND = static_cast<size_t>(start_ind) + sizeof(mkfs::hefs::BootNode); - boot_node.endIND = static_cast<size_t>(end_ind); - boot_node.startIN = static_cast<size_t>(start_in); - boot_node.endIN = static_cast<size_t>(end_in); - boot_node.startBlock = static_cast<size_t>(start_block); - boot_node.endBlock = static_cast<size_t>(end_block); - boot_node.indCount = 0UL; - boot_node.diskStatus = mkfs::hefs::kHeFSStatusUnlocked; - - 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); - std::memcpy(boot_node.magic, kHeFSMagic, magic_copy); - boot_node.magic[magic_copy] = 0; - - constexpr size_t vol_slots = kHeFSPartNameLen; - std::memset(boot_node.volumeName, 0, sizeof(boot_node.volumeName)); - size_t label_units = std::min(kLabel.size(), vol_slots - 1); - for (size_t i = 0; i < label_units; ++i) { - boot_node.volumeName[i] = static_cast<char16_t>(kLabel[i]); - } - boot_node.volumeName[label_units] = 0; - - output_device.seekp(static_cast<std::streamoff>(start_ind)); - if (!output_device.good()) { - mkfs::console_out() << "hefs: error: Failed seek to index start.\n"; - return EXIT_FAILURE; - } - - output_device.write(reinterpret_cast<const char*>(&boot_node), - sizeof(boot_node)); - if (!output_device.good()) { - mkfs::console_out() - << "hefs: error: Unable to write BootNode to output device: " - << output_path << "\n"; - return EXIT_FAILURE; - } - - output_device.seekp(static_cast<std::streamoff>(boot_node.startIND)); - if (!output_device.good()) { - mkfs::console_out() << "hefs: error: Failed seek to startIND.\n"; - return EXIT_FAILURE; - } - - output_device.flush(); - output_device.close(); - + if (argc < 2) { mkfs::console_out() - << "hefs: info: Wrote filesystem to output device: " << output_path << "\n"; - return EXIT_SUCCESS; + << "hefs: usage: mkfs.hefs -L=<label> -s=<sector_size> -b=<ind_start> -e=<ind_end> " + "-bs=<block_start> -be=<block_end> -is=<in_start> -ie=<in_end> " + "-S=<disk_size_GB> -o=<output_device>\n"; + + return EXIT_FAILURE; + } + + std::string args = build_args(argc, argv); + + auto output_path = mkfs::get_option<char>(args, "-o"); + if (output_path.empty()) { + mkfs::console_out() << "hefs: error: Missing -o <output_device> argument.\n"; + return EXIT_FAILURE; + } + + auto opt_s = mkfs::get_option<char>(args, "-s"); + long parsed_s = 0; + if (!parse_signed(opt_s, parsed_s, 10) || parsed_s == 0) { + mkfs::console_out() << "hefs: error: Invalid sector size \"" << opt_s + << "\". Must be a positive integer.\n"; + return EXIT_FAILURE; + } + + if ((parsed_s & (parsed_s - 1)) != 0) { + mkfs::console_out() << "hefs: error: Sector size \"" << parsed_s + << "\" is not a power of two.\n"; + return EXIT_FAILURE; + } + kSectorSize = static_cast<size_t>(parsed_s); + + auto opt_L = mkfs::get_option<char>(args, "-L"); + if (!opt_L.empty()) { + kLabel.clear(); + for (char c : opt_L) kLabel.push_back(static_cast<char8_t>(c)); + } else { + kLabel.clear(); + for (size_t i = 0; i < kHeFSPartNameLen && kHeFSDefaultVolumeName[i] != u'\0'; ++i) { + kLabel.push_back(static_cast<char8_t>(kHeFSDefaultVolumeName[i])); + } + } + + auto opt_S = mkfs::get_option<char>(args, "-S"); + unsigned long long gb = 0; + if (!parse_decimal(opt_S, gb) || gb == 0ULL) { + mkfs::console_out() << "hefs: error: Invalid disk size \"" << opt_S + << "\". Must be a positive integer.\n"; + return EXIT_FAILURE; + } + unsigned long long max_gb = std::numeric_limits<uint64_t>::max() / (1024ULL * 1024ULL * 1024ULL); + if (gb > max_gb) { + mkfs::console_out() << "hefs: error: Disk size \"" << gb << "GB\" is too large.\n"; + return EXIT_FAILURE; + } + kDiskSize = static_cast<size_t>(gb * 1024ULL * 1024ULL * 1024ULL); + + auto opt_b = mkfs::get_option<char>(args, "-b"); + auto opt_e = mkfs::get_option<char>(args, "-e"); + auto opt_bs = mkfs::get_option<char>(args, "-bs"); + auto opt_be = mkfs::get_option<char>(args, "-be"); + auto opt_is = mkfs::get_option<char>(args, "-is"); + auto opt_ie = mkfs::get_option<char>(args, "-ie"); + + long start_ind = 0, end_ind = 0; + long start_block = 0, end_block = 0; + long start_in = 0, end_in = 0; + + if (!parse_signed(opt_b, start_ind, 16)) { + mkfs::console_out() << "hefs: error: Invalid -b <hex> argument.\n"; + return EXIT_FAILURE; + } + if (!parse_signed(opt_e, end_ind, 16) || end_ind <= start_ind) { + mkfs::console_out() << "hefs: error: Invalid or out-of-range -e <hex> argument.\n"; + return EXIT_FAILURE; + } + if (!parse_signed(opt_bs, start_block, 16)) { + mkfs::console_out() << "hefs: error: Invalid -bs <hex> argument.\n"; + return EXIT_FAILURE; + } + if (!parse_signed(opt_be, end_block, 16) || end_block <= start_block) { + mkfs::console_out() << "hefs: error: Invalid or out-of-range -be <hex> argument.\n"; + return EXIT_FAILURE; + } + if (!parse_signed(opt_is, start_in, 16)) { + mkfs::console_out() << "hefs: error: Invalid -is <hex> argument.\n"; + return EXIT_FAILURE; + } + if (!parse_signed(opt_ie, end_in, 16) || end_in <= start_in) { + mkfs::console_out() << "hefs: error: Invalid or out-of-range -ie <hex> argument.\n"; + return EXIT_FAILURE; + } + + if (static_cast<size_t>(end_block) * kSectorSize > kDiskSize || + static_cast<size_t>(end_ind) > kDiskSize || static_cast<size_t>(end_in) > kDiskSize) { + mkfs::console_out() << "hefs: error: One or more ranges exceed disk size.\n"; + return EXIT_FAILURE; + } + + std::ofstream output_device(output_path, std::ios::binary); + if (!output_device.good()) { + mkfs::console_out() << "hefs: error: Unable to open output device: " << output_path << "\n"; + return EXIT_FAILURE; + } + + mkfs::hefs::BootNode boot_node; + std::memset(&boot_node, 0, sizeof(boot_node)); + + boot_node.version = kVersion; + boot_node.diskKind = mkfs::hefs::kHeFSHardDrive; + boot_node.encoding = mkfs::hefs::kHeFSEncodingFlagsUTF8; + boot_node.diskSize = kDiskSize; + boot_node.sectorSize = kSectorSize; + boot_node.startIND = static_cast<size_t>(start_ind) + sizeof(mkfs::hefs::BootNode); + boot_node.endIND = static_cast<size_t>(end_ind); + boot_node.startIN = static_cast<size_t>(start_in); + boot_node.endIN = static_cast<size_t>(end_in); + boot_node.startBlock = static_cast<size_t>(start_block); + boot_node.endBlock = static_cast<size_t>(end_block); + boot_node.indCount = 0UL; + boot_node.diskStatus = mkfs::hefs::kHeFSStatusUnlocked; + + 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); + std::memcpy(boot_node.magic, kHeFSMagic, magic_copy); + boot_node.magic[magic_copy] = 0; + + constexpr size_t vol_slots = kHeFSPartNameLen; + std::memset(boot_node.volumeName, 0, sizeof(boot_node.volumeName)); + size_t label_units = std::min(kLabel.size(), vol_slots - 1); + for (size_t i = 0; i < label_units; ++i) { + boot_node.volumeName[i] = static_cast<char16_t>(kLabel[i]); + } + boot_node.volumeName[label_units] = 0; + + output_device.seekp(static_cast<std::streamoff>(start_ind)); + if (!output_device.good()) { + mkfs::console_out() << "hefs: error: Failed seek to index start.\n"; + return EXIT_FAILURE; + } + + output_device.write(reinterpret_cast<const char*>(&boot_node), sizeof(boot_node)); + if (!output_device.good()) { + mkfs::console_out() << "hefs: error: Unable to write BootNode to output device: " << output_path + << "\n"; + return EXIT_FAILURE; + } + + output_device.seekp(static_cast<std::streamoff>(boot_node.startIND)); + if (!output_device.good()) { + mkfs::console_out() << "hefs: error: Failed seek to startIND.\n"; + return EXIT_FAILURE; + } + + output_device.flush(); + output_device.close(); + + mkfs::console_out() << "hefs: info: Wrote filesystem to output device: " << output_path << "\n"; + return EXIT_SUCCESS; } |
