Technician tool v0.6

Imported from the Google Drive folder 004-technician_tool/v0.6.
This commit is contained in:
kkallas committed 2026-09-28 17:31:03 +02:00
1 parent f5b32e1554
commit 22bd853ecf
30 files changed
+1521 -568

No files matched your search

+103
View File
@@ -0,0 +1,103 @@
"""FAT32/MBR layout: field-level checks against the FAT spec on an in-memory
disk, plus an independent FAT implementation (pyfatfs) mounting the result."""
import struct
import pytest
from kota_tech.sd import fat32
MiB, GiB = 1024**2, 1024**3
def make_disk(size: int, label="KOTA") -> tuple[bytearray, fat32.Fat32Layout]:
lay = fat32.plan(size)
disk = bytearray(b"\xAA" * size) # old garbage everywhere
zeros, writes = fat32.build(lay, label, 0x12345678, 0xCAFEBABE, size)
for off, n in zeros:
disk[off:off + n] = bytes(n)
for off, data in writes:
disk[off:off + len(data)] = data
return disk, lay
@pytest.fixture(scope="module")
def small():
return make_disk(96 * MiB)
def test_mbr_has_one_fat32_lba_partition(small):
disk, lay = small
assert disk[510:512] == b"\x55\xAA"
status, ptype, lba, count = struct.unpack_from("<B3xB3xII", disk, 0x1BE)
assert (status, ptype, lba) == (0, 0x0C, fat32.PART_START // 512)
assert count == lay.part_sectors
assert disk[0x1CE:0x1FE] == bytes(48) # other entries empty
assert struct.unpack_from("<I", disk, 0x1B8)[0] == 0xCAFEBABE
def test_boot_sector_fields(small):
disk, lay = small
b = disk[fat32.PART_START:fat32.PART_START + 512]
bps, spc, rsvd, nfats, root_ent, tot16, media, fat16 = struct.unpack_from("<HBHBHHBH", b, 11)
assert (bps, spc, rsvd, nfats, root_ent, tot16, media, fat16) == (512, lay.sectors_per_cluster, 32, 2, 0, 0, 0xF8, 0)
hidden, tot32, fatsz, _, _, root_clus, fsinfo, bkboot = struct.unpack_from("<IIIHHIHH", b, 28)
assert hidden == fat32.PART_START // 512 and tot32 == lay.part_sectors and fatsz == lay.fat_sectors
assert (root_clus, fsinfo, bkboot) == (2, 1, 6)
assert b[66] == 0x29 and b[71:82] == b"KOTA " and b[82:90] == b"FAT32 " and b[510:] == b"\x55\xAA"
assert disk[fat32.PART_START + 6 * 512:fat32.PART_START + 7 * 512] == b # backup boot sector
def test_fat_is_large_enough_and_clusters_valid(small):
_, lay = small
assert fat32.MIN_CLUSTERS <= lay.clusters
assert lay.fat_sectors * 512 // 4 >= lay.clusters + 2
assert lay.data_start + lay.clusters * lay.sectors_per_cluster <= lay.part_sectors
def test_fsinfo_and_fat_entries(small):
disk, lay = small
info = disk[fat32.PART_START + 512:fat32.PART_START + 1024]
assert struct.unpack_from("<I", info, 0)[0] == 0x41615252
assert struct.unpack_from("<III", info, 484) == (0x61417272, lay.clusters - 1, 3)
assert struct.unpack_from("<I", info, 508)[0] == 0xAA550000
for fat in range(2):
off = fat32.PART_START + (32 + fat * lay.fat_sectors) * 512
assert struct.unpack_from("<III", disk, off) == (0x0FFFFFF8, 0x0FFFFFFF, 0x0FFFFFFF)
assert disk[off + 12:off + lay.fat_sectors * 512] == bytes(lay.fat_sectors * 512 - 12)
def test_root_holds_only_the_label(small):
disk, lay = small
root = fat32.PART_START + lay.data_start * 512
assert disk[root:root + 11] == b"KOTA " and disk[root + 11] == 0x08
assert disk[root + 32:root + lay.sectors_per_cluster * 512] == bytes(lay.sectors_per_cluster * 512 - 32)
@pytest.mark.parametrize("size,spc", [(4 * GiB, 8), (15 * GiB, 16), (31_457_280_000, 32), (64 * GiB, 32)])
def test_real_card_sizes(size, spc):
lay = fat32.plan(size)
assert lay.sectors_per_cluster == spc
assert lay.part_sectors * 512 <= min(size - fat32.PART_START, 32 * GiB)
assert fat32.MIN_CLUSTERS <= lay.clusters < fat32.MAX_CLUSTERS
def test_too_small_card_refused():
with pytest.raises(ValueError):
fat32.plan(20 * MiB)
def test_independent_fat_implementation_mounts_it(small, tmp_path):
pyfat = pytest.importorskip("pyfatfs.PyFat")
disk, lay = small
img = tmp_path / "part.img"
img.write_bytes(bytes(disk[fat32.PART_START:fat32.PART_START + lay.part_sectors * 512]))
fs = pyfat.PyFat()
fs.open(str(img))
assert fs.fat_type == pyfat.PyFat.FAT_TYPE_FAT32
assert fs.bpb_header["BPB_TotSec32"] == lay.part_sectors
fs.close()
from pyfatfs.PyFatFS import PyFatFS
with PyFatFS(str(img)) as ffs: # write + read back a file like ENV.TXT
ffs.writebytes("/ENV.TXT", b"INFLUX_URL=x\n")
assert ffs.readbytes("/ENV.TXT") == b"INFLUX_URL=x\n"