# SPDX-License-Identifier: GPL-3.0-or-later """Qualify Disk BASIC stream writes and positioning against independently decoded volumes.""" import argparse import os import shutil from pathlib import Path import subprocess import tempfile SECTOR = 256 GRANULE = 9 * SECTOR FAT = (17 * 18 + 1) * SECTOR TEXT_RECORDS = b"X" * 255 + b"\r\nMARS\rEUROPA\nTITAN\r\n" SENTINEL = b"previously saved data\x00\r\xff" * 57 FILL = bytes((i * 3 + 11) & 255 for i in range(66 * GRANULE)) DISTANT = bytes((i * 7 + i // SECTOR) & 255 for i in range(65537)) def contents(raw): """Decode directory and FAT independently, rejecting leaks, cross-links and cycles.""" files = {} owners = set() for row in range(FAT + SECTOR, (17 + 1) * 18 * SECTOR, 32): entry = raw[row:row + 32] if entry[0] == 0xff: break if entry[0] == 0: continue name = entry[:8].decode().rstrip() + "." + entry[8:11].decode().rstrip() granule = entry[13] data = bytearray() while True: if granule >= 68 or granule in owners: raise AssertionError(f"{name} references missing or shared granule {granule}") owners.add(granule) addr = (granule + (2 if granule >= 34 else 0)) * GRANULE link = raw[FAT + granule] if link < 68: data.extend(raw[addr:addr + GRANULE]) granule = link continue count, last = link - 0xc0, int.from_bytes(entry[14:16], "big") if count not in range(10) or last > SECTOR or (not count and last): raise AssertionError(f"Invalid final size in {name}: FAT {link} / directory {last}") length = (count - 1) * SECTOR + last if count else 0 data.extend(raw[addr:addr + length]) break if name in files: raise AssertionError(f"Duplicate directory entry {name}") files[name] = bytes(data) occupied = {i for i in range(68) if raw[FAT + i] != 255} if owners != occupied: raise AssertionError(f"Leaked/missing granules {sorted(owners ^ occupied)}") return files def check_files(folder, stage, expected): actual = contents((folder / (stage + ".dsk")).read_bytes()) if actual != expected: names = {name for name in actual.keys() | expected.keys() if actual.get(name) != expected.get(name)} raise AssertionError(f"{stage}: mismatched {[(n, len(actual.get(n,b'')), len(expected.get(n,b''))) for n in names]}") def fragment_second_granule(raw, filename): """Relocate the second granule to the end of the disk without changing the file.""" directory = range(FAT + SECTOR, (17 + 1) * 18 * SECTOR, 32) entry = next(row for row in directory if raw[row:row + 11] == filename) head = raw[entry + 13] second = raw[FAT + head] unused = 67 assert head < 34 and second < 34 and raw[FAT + unused] == 255 old = second * GRANULE new = (unused + 2) * GRANULE # Skip track 17 on the way to the final track. raw[new:new + GRANULE] = raw[old:old + GRANULE] raw[old:old + GRANULE] = bytes(GRANULE) raw[FAT + unused] = raw[FAT + second] raw[FAT + head] = unused raw[FAT + second] = 255 def write_fixture(folder, tool, scenario): volume = folder / "VOL.DSK" volume.write_bytes(b"\xff" * (35 * 18 * SECTOR)) files = {"SAFE.DAT": SENTINEL} if scenario == "F": files["FILL.DAT"] = FILL elif scenario == "W": files["LOG.BIN"] = b"replace this old chain" * 150 files["DISTANT.DAT"] = DISTANT if scenario in ("S", "U"): files.update({"DISTANT.DAT": DISTANT, "RECORDS.TXT": TEXT_RECORDS}) if scenario == "U": files["RESET.DAT"] = b"old log" * 800 if scenario == "N": files.update({f"PAD{i}.DAT": bytes([i]) for i in range(7)}) files["DELETE.DAT"] = b"multi-granule" * 400 if scenario == "C": files["CROSS.DAT"] = b"aliased by fixture" for name, data in files.items(): (folder / name).write_bytes(data) subprocess.run([tool, "--binary", volume, name], cwd=folder, check=True) assert contents(volume.read_bytes()) == files if scenario in ("S", "U"): raw = bytearray(volume.read_bytes()) fragment_second_granule(raw, b"DISTANT DAT") assert contents(raw) == files volume.write_bytes(raw) if scenario == "C": raw = bytearray(volume.read_bytes()) directory = FAT + SECTOR assert raw[directory:directory + 4] == b"SAFE" and raw[directory + 32:directory + 37] == b"CROSS" raw[directory + 32 + 13] = raw[directory + 13] volume.write_bytes(raw) if scenario == "N": (folder / "SECOND.DSK").write_bytes(volume.read_bytes()) (folder / "initial.dsk").write_bytes(volume.read_bytes()) (folder / "startup.cfg").write_text("0=VOL.DSK\n") return volume def inspect(folder, scenario): base = {"SAFE.DAT": SENTINEL} if scenario == "N": expected = {f"PAD{i}.DAT": bytes([i]) for i in range(7)} expected.update({"OVERLAND.BAK": SENTINEL, "HOLD.TXT": b"still open"}) check_files(folder, "closed", expected) restored = {"SAFE.DAT": SENTINEL, "DELETE.DAT": b"multi-granule" * 400, "HOLD.TXT": b""} restored.update({f"PAD{i}.DAT": bytes([i]) for i in range(7)}) check_files(folder, "rollback", restored) if (folder / "drive1.dsk").exists(): second = contents((folder / "SECOND.DSK").read_bytes()) second["DRIVEONE.DAT"] = second.pop("SAFE.DAT") del second["DELETE.DAT"] check_files(folder, "drive1", second) elif scenario == "U": patched = bytearray(DISTANT) patched[255:259] = b"SECT" patched[2302:2306] = b"GRAN" expected = {**base, "DISTANT.DAT": patched + b"END!", "RECORDS.TXT": TEXT_RECORDS.replace(b"EUROPA", b"PHOBOS"), "TRIPS.DAT": b"\0\xffABC", "RESET.DAT": b"NOW", "EMPTY.TXT": b"", "CREATED.TXT": b"LOG\r", "FORMAT.TXT": b"HELLO 00042\r"} check_files(folder, "closed", expected) assert (folder / "protected.dsk").read_bytes() == (folder / "closed.dsk").read_bytes() elif scenario == "S": patched = bytearray((i + sector) & 255 for sector in range(10) for i in range(256)) patched.extend(b"\xa5" * 19) patched[255:259] = b"SECT" patched[2302:2306] = b"GRAN" patched[-3:] = b"TAIL++" patched[0] = ord("H") patched.extend(b"END!") check_files(folder, "closed", {**base, "DISTANT.DAT": DISTANT + b"!", "RECORDS.TXT": TEXT_RECORDS, "PATCH.BIN": patched}) elif scenario == "T": check_files(folder, "protected", {**base, "REPORT.BIN": b"ORBIT"}) assert (folder / "protected.dsk").read_bytes() == (folder / "refused.dsk").read_bytes() check_files(folder, "closed", {**base, "REPORT.BIN": b"OXBIT"}) elif scenario in ("C", "B", "M"): assert (folder / "closed.dsk").read_bytes() == (folder / "initial.dsk").read_bytes() elif scenario == "P": expected = dict(base, **{"REPORT.TXT": b"MARS RIDGE\r"}) check_files(folder, "protected", expected) assert (folder / "protected.dsk").read_bytes() == (folder / "closed.dsk").read_bytes() elif scenario == "F": expected = dict(base, **{"FILL.DAT": FILL, "LAST.DAT": b"abcdefghij" * 230}) check_files(folder, "full", expected) # The four remaining bytes physically fit; the buffered next sector does not. # Existing committed bytes and all unrelated data must survive. actual = contents((folder / "closed.dsk").read_bytes()) last = actual.pop("LAST.DAT") committed = expected.pop("LAST.DAT") assert actual == expected and last.startswith(committed) tail = last[len(committed):] assert tail == b"\xf3" * 4 else: binary = b"".join(bytes((i + sec) & 255 for i in range(256)) for sec in range(9)) first = {**base, "REPORT.TXT": b"MARS RIDGE\r", "LOG.BIN": binary[:256], "DISTANT.DAT": DISTANT} check_files(folder, "sector", first) first["LOG.BIN"] = binary check_files(folder, "granule", first) first["LOG.BIN"] += b"\xff\x00" first["DISTANT.DAT"] += b"\xfe" first["REPORT.TXT"] += b"SECOND\r" check_files(folder, "append", first) first.update({"LOG.BIN": b"", "ABSENT.TXT": b"A\r"}) check_files(folder, "closed", first) first["BASIC.TXT"] = b"FROM BASIC\r" check_files(folder, "basic", first) def main(): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument("runtime", type=Path) parser.add_argument("core", type=Path) parser.add_argument("roms", type=Path) parser.add_argument("--profile", action="append", choices=("disk", "rgb-hd", "rgb-fd", "sdc")) parser.add_argument("--suite", choices=("all", "writes", "position", "update", "directory"), default="all") args = parser.parse_args() runtime, core, roms = (p.resolve(strict=True) for p in (args.runtime, args.core, args.roms)) fixture = Path(__file__).resolve().parent env = dict(os.environ, VCC_CMOC_COMPAT="1", PATH=str(runtime / "bin") + os.pathsep + os.environ["PATH"]) with tempfile.TemporaryDirectory(prefix="vcc-stream-writes-") as tmp: work = Path(tmp) compiler = [runtime / "bin/cmoc", "--coco", "-Werror"] subprocess.run(["xcrun", "clang++", "-std=c++17", "-O2", "-mmacosx-version-min=14.0", "-I", fixture.parents[2] / "src", fixture / "run.cpp", core, "-o", "run"], cwd=work, check=True) # The installed archive uses -O2. Exercise both caller settings against # that archive and separately compile the stream implementation at -O0. builds = (("writes", "qualification", "-O2", False), ("position", "position", "-O2", False), ("update", "update", "-O0", False), ("update", "update", "-O2", False), ("update", "update", "-O0", True), ("directory", "directory", "-O0", False), ("directory", "directory", "-O2", False)) for suite, source, level, compile_streams in builds: if args.suite not in ("all", suite): continue implementation = "source" if compile_streams else "staged" objects = [] if suite == "position": for obj, defines in (("legacy-first.o", ["-DLEGACY_FIRST"]), ("stdio-first.o", [])): subprocess.run([*compiler, level, *defines, "-c", "-o", obj, fixture / "mixed-rewind.c"], cwd=work, env=env, check=True) objects.append(obj) if compile_streams: for unit in ("streams", "format", "fileformat"): source_path = fixture.parents[2] / "sdk/cmoc/compat" / (unit + ".c") subprocess.run([*compiler, level, "-c", "-o", unit + ".o", source_path], cwd=work, env=env, check=True) objects.append(unit + ".o") disk_library = "-ldecbfile" if suite == "directory": # Swap only the ROM I/O object in a disposable copy so a guest # can exercise one transient write failure without a production hook. shutil.copyfile(runtime / "share/cmoc/lib/libdecbfile.a", work / "libdiskfault.a") subprocess.run([*compiler, level, "-c", "-o", "dskcon.o", fixture / "refuse-fat.c"], cwd=work, env=env, check=True) subprocess.run([runtime / "bin/lwar", "--replace", "libdiskfault.a", "dskcon.o"], cwd=work, env=env, check=True) objects.append("-L.") disk_library = "-ldiskfault" if suite == "update": objects.append(fixture / "formatted.c") if not compile_streams: objects.append("-lvcccompat") binary = work / (source + ".bin") # The all-source -O0 qualification program needs another 2 KiB # below the guest stack and the ROM mapped at $8000. origin = "--org=2000" if compile_streams else "--org=2800" subprocess.run([*compiler, level, origin, "-o", binary, fixture / (source + ".c"), *objects, disk_library], cwd=work, env=env, check=True) for profile in args.profile or ("disk", "rgb-fd", "rgb-hd", "sdc"): if suite == "writes": cases = [("W", "streams and BASIC exchange"), ("F", "disk-full partial write"), ("P", "protected flush")] if profile == "disk": cases += [("C", "cross-linked FAT refusal"), ("B", "active Disk BASIC file refusal")] elif suite == "directory": cases = [("N", "rename without replacement and reclaim removed files")] elif suite == "position": cases = [("S", "seek, tell, rewind and in-place overwrite"), ("T", "protected seek and rewind recovery")] if profile == "disk": cases.append(("M", "stdio and legacy rewind in both header orders")) else: cases = [("U", "update directions, protected writes, unchanged neighbors and EOF growth")] for cpu in ("6809", "6309"): for scenario, description in cases: with tempfile.TemporaryDirectory(dir=work) as test: folder = Path(test) volume = write_fixture(folder, runtime / "bin/writecocofile", scenario) subprocess.run([work / "run", roms, volume, binary, profile, cpu, scenario], cwd=folder, check=True) inspect(folder, scenario) print(f"PASS {profile} {cpu} {level} {implementation}: {description}", flush=True) if __name__ == "__main__": main()