diff --git a/src/nipbox.nim b/src/nipbox.nim index b41f8d4..aa9da09 100644 --- a/src/nipbox.nim +++ b/src/nipbox.nim @@ -81,9 +81,10 @@ proc print_raw(s: string) = if s.len > 0: discard write(1, unsafeAddr s[0], csize_t(s.len)) -# Helper: Swap Bytes 16 -proc swap16(x: uint16): uint16 = - return (x shl 8) or (x shr 8) +# Forward declarations for functions used before their definition +proc parseIntSimple(s: string): uint64 +proc toHexChar(b: byte): char +proc do_mkfs() # Calculate Checksum (Standard Internet Checksum) proc calc_checksum(buf: cptr, len: int): uint16 = @@ -346,8 +347,44 @@ proc main() = elif cmd == "ls": do_ls() elif cmd == "exec": do_exec(arg) elif cmd == "dd": do_dd(arg) + elif cmd == "mkfs": do_mkfs() elif cmd == "help": do_help() else: print("Unknown command: " & cmd) +proc do_mkfs() = + print("[mkfs] Formatting disk as Sovereign Filesystem (SFS v1)...") + + # 1. Superblock (Sector 0) + var sb: array[512, byte] + # Magic: S (0x53), F (0x46), S (0x53), 1 (0x31) -> Little Endian? String is byte order. + sb[0] = 0x53 + sb[1] = 0x46 + sb[2] = 0x53 + sb[3] = 0x31 + # Disk Size (u64 at offset 4) - 32MB = 33554432 = 0x02000000 + # Little Endian + sb[4] = 0x00 + sb[5] = 0x00 + sb[6] = 0x00 + sb[7] = 0x02 + sb[8] = 0x00 + sb[9] = 0x00 + sb[10] = 0x00 + sb[11] = 0x00 + + # Root Dir Sector (u64 at offset 12) -> 1 + sb[12] = 0x01 + + nexus_blk_write(0, addr sb[0], 512) + print("[mkfs] Superblock written.") + + # 2. Directory Table (Sector 1) - Zero it + var zero: array[512, byte] # Implicitly zeroed? In Nim, yes if global/stack? Better be safe. + for i in 0 ..< 512: zero[i] = 0 + nexus_blk_write(1, addr zero[0], 512) + print("[mkfs] Directory Table initialized.") + + print("[mkfs] Format Complete. The Ledger is structured.") + when isMainModule: main()