#!/usr/bin/env python3 """Fail-closed validation of the active 6400 lock against the final protocol. The retired Host-I2C lock is gone. This validator now fails closed on the NEW rule: one 8-bit-length frame, no PID / temperature protection, no Host I2C and no retired protocol field anywhere in the active 6400 closure. """ from __future__ import annotations import json import re import sys from pathlib import Path try: import yaml except Exception as exc: # pragma: no cover print(f"LOCK_FAIL yaml unavailable: {exc}", file=sys.stderr) raise SystemExit(2) ROOT = Path(__file__).resolve().parents[1] LOCK = ROOT / "ACTIVE_6400_MANUAL_I2C_LOCK.yaml" EXPECTED = { "product": "6400-front-rear", "board": "FCB V1.1", "device": "EF2L45BG256B", "clock_hz": 25_000_000, "pll": "forbidden", "firmware_count": 1, "gui_count": 1, "watchdog_seconds": 30, "host_i2c": "REMOVED_NOT_PRESENT", } EXPECTED_COMMANDS = {"SET_ALL": 0x01, "GET_ALL": 0x02} EXPECTED_RESPONSES = {"SET_ACK": 0x81, "GET_ACK": 0x82, "GET_DATA": 0x42, "NACK": 0xC0} EXPECTED_REASONS = {"UNSUPPORTED": 1, "BAD_LENGTH": 2, "BAD_VALUE": 3, "QUEUE_FULL": 4, "BUSY": 5} FORBIDDEN_HEADER_MACROS = ("FB_SNAPSHOT", "FB_TEMP", "FB_STATUS_", "FB_OWNER_", "FB_HOST_I2C", "FB_PID") def fail(message: str) -> int: print(f"LOCK_FAIL {message}", file=sys.stderr) return 2 def main() -> int: if not LOCK.is_file(): return fail("missing ACTIVE_6400_MANUAL_I2C_LOCK.yaml") data = yaml.safe_load(LOCK.read_text(encoding="utf-8")) for key, value in EXPECTED.items(): if data.get(key) != value: return fail(f"{key}: expected {value!r}, got {data.get(key)!r}") frame = data.get("frame", {}) if frame.get("fields") != [{"type": "u8"}, {"length": "u8"}, {"payload": "bytes"}, {"crc_l": "u8"}, {"crc_h": "u8"}]: return fail("frame fields must be TYPE:u8, LENGTH:u8, PAYLOAD, CRC_L, CRC_H") if frame.get("coverage") != "type_length_payload" or frame.get("wire_order") != "little": return fail("CRC coverage/order") if data.get("commands") != EXPECTED_COMMANDS: return fail(f"commands: {data.get('commands')!r}") if data.get("responses") != EXPECTED_RESPONSES: return fail(f"responses: {data.get('responses')!r}") if data.get("nack_reasons") != EXPECTED_REASONS: return fail(f"NACK reasons: {data.get('nack_reasons')!r}") if data.get("payload_bytes") != {"set_all": 32, "get_data": 83}: return fail(f"payload sizes: {data.get('payload_bytes')!r}") outputs = data.get("outputs", {}) if (outputs.get("ptc_count"), outputs.get("front_fan_count"), outputs.get("rear_fan_count")) != (24, 4, 4): return fail("actuator counts") if outputs.get("ptc_pwm_hz") != 20 or outputs.get("fan_pwm_hz") != 20: return fail("PWM frequency must be 20 Hz for PTC and FAN") host = data.get("host_uart", {}) if host.get("baud") != 921600 or host.get("format") != "8N1" or host.get("connector") != "CON78": return fail("host UART") if data.get("temperature", {}).get("role") != "acquisition_only": return fail("temperature must be acquisition only") deleted = data.get("deleted_functionality", {}) for key in ("pid_control", "temperature_target_70c", "temperature_protection_76c_77c", "ct_warmup", "position_percent", "global_snapshot_lock", "external_age_epoch_stale", "command_tag", "sequence_number", "event_history", "group_synchronised_start", "unbounded_seek_continuous_hold", "host_i2c"): if deleted.get(key) != "ABSENT": return fail(f"deleted functionality {key} must be ABSENT") for relative, message in ( ("contracts/6400-wire.yaml", "6400 wire contract"), ("contracts/6400-manual.yaml", "6400 manual contract"), ("contracts/6400-snapshot.yaml", "6400 GET_DATA contract"), ): path = ROOT / relative if not path.is_file(): return fail(f"missing {relative}") text = path.read_text(encoding="utf-8") if "length_width_bits: 16" in text or "length: u16le" in text: return fail(f"{message} still declares a 16-bit length") for token in ("ACK_NACK", "ALL_STATUS_DATA", "MOTOR_QUEUE_WRITE", "MOTOR_CONTROL", "OUTPUT_KEEPALIVE", "LENGTH16", "register_address_width"): if token in text: return fail(f"{message} still declares {token}") if (ROOT / "contracts/host-i2c-pin-evidence.json").exists(): return fail("retired host I2C pin evidence is present") manual = yaml.safe_load((ROOT / "contracts/6400-manual.yaml").read_text(encoding="utf-8")) if manual.get("host_i2c") != "REMOVED_NOT_PRESENT": return fail("6400 manual must declare host_i2c REMOVED_NOT_PRESENT") if manual.get("sensor_i2c", {}).get("host_register_interface", False): return fail("the CT7428 bus must stay sensor-only") header = ROOT / "generated/6400/prod2u_6400_v1_params.vh" if not header.is_file(): return fail("generated 6400 header is missing") text = header.read_text(encoding="utf-8") if not re.search(r"`define FB_FRAME_LENGTH_WIDTH\s+8\b", text): return fail("FB_FRAME_LENGTH_WIDTH must be 8") if not re.search(r"`define FB_GET_DATA_BYTES\s+83\b", text): return fail("FB_GET_DATA_BYTES must be 83") for token in FORBIDDEN_HEADER_MACROS: if token in text: return fail(f"generated header still defines {token}") if (ROOT / "generated/6400/host-i2c-register-map.json").exists(): return fail("retired generated host I2C register map is present") host_contract = ROOT / "generated/6400/host-contract.json" if not host_contract.is_file(): return fail("generated 6400 host contract is missing") payload = json.loads(host_contract.read_text(encoding="utf-8")) if payload.get("wire", {}).get("length_width_bits", 8) != 8: return fail("generated host contract declares a non-8-bit length") if "host_i2c" in payload.get("wire", {}): return fail("generated host contract still carries a host I2C block") print("LOCK_PASS") return 0 if __name__ == "__main__": raise SystemExit(main())