|
|
@@ -0,0 +1,729 @@
|
|
|
+#!/usr/bin/env python3
|
|
|
+"""Collect J6 binary921 Host/Debug evidence.
|
|
|
+
|
|
|
+This collector uses explicit COM ports only. It opens HostPort for binary921
|
|
|
+host commands and reads DebugPort binary921 machine frames; DebugPort is never
|
|
|
+written.
|
|
|
+"""
|
|
|
+
|
|
|
+from __future__ import annotations
|
|
|
+
|
|
|
+import argparse
|
|
|
+import hashlib
|
|
|
+import json
|
|
|
+import sys
|
|
|
+import time
|
|
|
+from datetime import datetime, timezone
|
|
|
+from pathlib import Path
|
|
|
+from typing import Any
|
|
|
+
|
|
|
+sys.dont_write_bytecode = True
|
|
|
+ROOT = Path(__file__).resolve().parents[2]
|
|
|
+TOOLS = ROOT / "tools"
|
|
|
+if str(TOOLS) not in sys.path:
|
|
|
+ sys.path.insert(0, str(TOOLS))
|
|
|
+
|
|
|
+from binary921 import CommandId, FrameType, build_host_command, decode_stream, frame_to_dict, profile_id_for_name # noqa: E402
|
|
|
+
|
|
|
+COMMAND_SEQUENCE = [
|
|
|
+ ("SET_PTC", CommandId.SET_PTC, 1, 10),
|
|
|
+ ("SET_PTC", CommandId.SET_PTC, 24, 20),
|
|
|
+ ("SET_FAN", CommandId.SET_FAN, 1, 40),
|
|
|
+ ("SET_FAN", CommandId.SET_FAN, 4, 30),
|
|
|
+ ("SET_PID_REQUESTED", CommandId.SET_PID_REQUESTED, None, 1),
|
|
|
+ ("GET_STATUS", CommandId.GET_STATUS, None, None),
|
|
|
+ ("GET_MAP", CommandId.GET_MAP, None, None),
|
|
|
+ ("GET_SENSOR", CommandId.GET_SENSOR, None, None),
|
|
|
+ ("GET_SENSOR", CommandId.GET_SENSOR, None, None),
|
|
|
+ ("GET_SENSOR", CommandId.GET_SENSOR, None, None),
|
|
|
+ ("SET_PID_REQUESTED", CommandId.SET_PID_REQUESTED, None, 0),
|
|
|
+ ("DISABLE", CommandId.DISABLE, None, None),
|
|
|
+]
|
|
|
+COMBINED_UART_PROFILES = {"production-two-uart-bmc-replay-binary921"}
|
|
|
+SEPARATE_UART_PROFILES = {"debug-photo-bmc-replay-binary921", "formal-uart1-bmc-replay-binary921"}
|
|
|
+ALL_PROFILES = sorted(COMBINED_UART_PROFILES | SEPARATE_UART_PROFILES)
|
|
|
+
|
|
|
+
|
|
|
+def utc_now() -> str:
|
|
|
+ return datetime.now(timezone.utc).isoformat(timespec="seconds").replace("+00:00", "Z")
|
|
|
+
|
|
|
+
|
|
|
+def sha256_bytes(data: bytes) -> str:
|
|
|
+ return hashlib.sha256(data).hexdigest()
|
|
|
+
|
|
|
+
|
|
|
+def _profile_id(profile: str) -> int:
|
|
|
+ return profile_id_for_name(profile)
|
|
|
+
|
|
|
+
|
|
|
+def is_combined_uart_profile(profile: str) -> bool:
|
|
|
+ return profile in COMBINED_UART_PROFILES
|
|
|
+
|
|
|
+
|
|
|
+def transport_for_args(args: argparse.Namespace) -> str:
|
|
|
+ requested = getattr(args, "transport", "auto") or "auto"
|
|
|
+ if requested == "auto":
|
|
|
+ return "combined-single-com" if is_combined_uart_profile(args.profile) else "two-uart"
|
|
|
+ return str(requested)
|
|
|
+
|
|
|
+
|
|
|
+def _require_explicit_com(name: str, value: str) -> None:
|
|
|
+ if not value.upper().startswith("COM") or not value[3:].isdigit():
|
|
|
+ raise ValueError(f"{name} must be an explicit COM port such as COM7")
|
|
|
+
|
|
|
+
|
|
|
+def _open_serial(port: str, baud: int) -> Any:
|
|
|
+ try:
|
|
|
+ import serial # type: ignore
|
|
|
+ except ImportError as exc:
|
|
|
+ raise RuntimeError(
|
|
|
+ "pyserial is required for live binary921 collection. "
|
|
|
+ "Install it manually in the isolated lab before running live collection."
|
|
|
+ ) from exc
|
|
|
+ endpoint = serial.Serial()
|
|
|
+ endpoint.port = port
|
|
|
+ endpoint.baudrate = baud
|
|
|
+ endpoint.bytesize = 8
|
|
|
+ endpoint.parity = "N"
|
|
|
+ endpoint.stopbits = 1
|
|
|
+ endpoint.timeout = 0.02
|
|
|
+ endpoint.write_timeout = 1
|
|
|
+ endpoint.rtscts = False
|
|
|
+ endpoint.dsrdtr = False
|
|
|
+ endpoint.dtr = False
|
|
|
+ endpoint.rts = False
|
|
|
+ endpoint.open()
|
|
|
+ return endpoint
|
|
|
+
|
|
|
+
|
|
|
+def decode_accumulated_frames(buffer: bytearray) -> tuple[list[dict[str, Any]], bytearray]:
|
|
|
+ frames, remainder = decode_stream(bytes(buffer))
|
|
|
+ return [frame_to_dict(frame) for frame in frames], bytearray(remainder)
|
|
|
+
|
|
|
+
|
|
|
+def wait_for_ready(debug_serial: Any, *, timeout_sec: float) -> tuple[dict[str, Any], bytearray, list[dict[str, Any]]]:
|
|
|
+ deadline = time.monotonic() + timeout_sec
|
|
|
+ raw = bytearray()
|
|
|
+ decoded: list[dict[str, Any]] = []
|
|
|
+ remainder = bytearray()
|
|
|
+ while time.monotonic() < deadline:
|
|
|
+ chunk = debug_serial.read(4096)
|
|
|
+ if chunk:
|
|
|
+ raw += chunk
|
|
|
+ remainder += chunk
|
|
|
+ frames, remainder = decode_accumulated_frames(remainder)
|
|
|
+ decoded.extend(frames)
|
|
|
+ for frame in frames:
|
|
|
+ if int(frame.get("frame_type", -1)) == int(FrameType.READY):
|
|
|
+ return frame, raw, decoded
|
|
|
+ else:
|
|
|
+ time.sleep(0.005)
|
|
|
+ raise TimeoutError("blocked_waiting_for_binary921_ready")
|
|
|
+
|
|
|
+
|
|
|
+def drain_frames(debug_serial: Any, host_serial: Any, *, duration_sec: float) -> tuple[bytes, bytes, list[dict[str, Any]], list[dict[str, Any]]]:
|
|
|
+ deadline = time.monotonic() + duration_sec
|
|
|
+ debug_raw = bytearray()
|
|
|
+ host_raw = bytearray()
|
|
|
+ debug_decoded: list[dict[str, Any]] = []
|
|
|
+ host_decoded: list[dict[str, Any]] = []
|
|
|
+ debug_remainder = bytearray()
|
|
|
+ host_remainder = bytearray()
|
|
|
+ while time.monotonic() < deadline:
|
|
|
+ chunk = debug_serial.read(4096)
|
|
|
+ if chunk:
|
|
|
+ debug_raw += chunk
|
|
|
+ debug_remainder += chunk
|
|
|
+ frames, debug_remainder = decode_accumulated_frames(debug_remainder)
|
|
|
+ debug_decoded.extend(frames)
|
|
|
+ chunk = host_serial.read(4096)
|
|
|
+ if chunk:
|
|
|
+ host_raw += chunk
|
|
|
+ host_remainder += chunk
|
|
|
+ frames, host_remainder = decode_accumulated_frames(host_remainder)
|
|
|
+ host_decoded.extend(frames)
|
|
|
+ if not chunk:
|
|
|
+ time.sleep(0.005)
|
|
|
+ return bytes(debug_raw), bytes(host_raw), debug_decoded, host_decoded
|
|
|
+
|
|
|
+
|
|
|
+def drain_combined_frames(combined_serial: Any, *, duration_sec: float) -> tuple[bytes, list[dict[str, Any]]]:
|
|
|
+ deadline = time.monotonic() + duration_sec
|
|
|
+ combined_raw = bytearray()
|
|
|
+ combined_decoded: list[dict[str, Any]] = []
|
|
|
+ remainder = bytearray()
|
|
|
+ while time.monotonic() < deadline:
|
|
|
+ chunk = combined_serial.read(4096)
|
|
|
+ if chunk:
|
|
|
+ combined_raw += chunk
|
|
|
+ remainder += chunk
|
|
|
+ frames, remainder = decode_accumulated_frames(remainder)
|
|
|
+ combined_decoded.extend(frames)
|
|
|
+ else:
|
|
|
+ time.sleep(0.005)
|
|
|
+ return bytes(combined_raw), combined_decoded
|
|
|
+
|
|
|
+
|
|
|
+def _expected_ack(command: dict[str, Any]) -> dict[str, Any]:
|
|
|
+ return {
|
|
|
+ "seq": int(command["seq"]),
|
|
|
+ "cmd_id": int(command["command_id"]),
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def _ack_match(command: dict[str, Any], ack: dict[str, Any], *, profile_id: int) -> dict[str, Any]:
|
|
|
+ decoded = ack.get("decoded") if isinstance(ack.get("decoded"), dict) else {}
|
|
|
+ expected = _expected_ack(command)
|
|
|
+ return {
|
|
|
+ "seq": expected["seq"],
|
|
|
+ "cmd": command["name"],
|
|
|
+ "expected_cmd_id": expected["cmd_id"],
|
|
|
+ "ack_seq": ack.get("seq"),
|
|
|
+ "ack_cmd_id": decoded.get("cmd_id"),
|
|
|
+ "ack_status_code": decoded.get("status_code"),
|
|
|
+ "ack_ok": decoded.get("ok"),
|
|
|
+ "ack_profile_id": ack.get("profile_id"),
|
|
|
+ "seq_match": ack.get("seq") == expected["seq"],
|
|
|
+ "cmd_match": decoded.get("cmd_id") == expected["cmd_id"],
|
|
|
+ "status_match": decoded.get("status_code") == 0 and decoded.get("ok") is True,
|
|
|
+ "profile_match": ack.get("profile_id") == profile_id,
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def match_host_acks(host_commands: list[dict[str, Any]], host_frames: list[dict[str, Any]], *, profile_id: int) -> tuple[list[dict[str, Any]], int]:
|
|
|
+ ack_frames = [frame for frame in host_frames if int(frame.get("frame_type", -1)) == int(FrameType.HOST_ACK)]
|
|
|
+ matches: list[dict[str, Any]] = []
|
|
|
+ matched_count = 0
|
|
|
+ for index, command in enumerate(host_commands):
|
|
|
+ ack = ack_frames[index] if index < len(ack_frames) else {}
|
|
|
+ match = _ack_match(command, ack, profile_id=profile_id)
|
|
|
+ match["accepted"] = all(
|
|
|
+ bool(match[key]) for key in ["seq_match", "cmd_match", "status_match", "profile_match"]
|
|
|
+ )
|
|
|
+ if match["accepted"]:
|
|
|
+ matched_count += 1
|
|
|
+ matches.append(match)
|
|
|
+ return matches, matched_count
|
|
|
+
|
|
|
+
|
|
|
+def build_manifest_from_frames(
|
|
|
+ *,
|
|
|
+ profile: str,
|
|
|
+ startup_ready: dict[str, Any] | None,
|
|
|
+ host_commands: list[dict[str, Any]],
|
|
|
+ host_frames: list[dict[str, Any]],
|
|
|
+ debug_frames: list[dict[str, Any]],
|
|
|
+ debug_raw: bytes,
|
|
|
+ host_raw: bytes,
|
|
|
+ combined_raw: bytes | None = None,
|
|
|
+ bmc_replay: dict[str, Any] | None = None,
|
|
|
+) -> dict[str, Any]:
|
|
|
+ profile_id = _profile_id(profile)
|
|
|
+ transport = "combined_host_debug_uart" if is_combined_uart_profile(profile) or combined_raw is not None else "separate_host_debug_uart"
|
|
|
+ ack_frames = [frame for frame in host_frames if int(frame.get("frame_type", -1)) == int(FrameType.HOST_ACK)]
|
|
|
+ ack_matches, ack_matched_count = match_host_acks(host_commands, host_frames, profile_id=profile_id)
|
|
|
+ bmc_payload_frames = [
|
|
|
+ frame for frame in debug_frames if int(frame.get("frame_type", -1)) == int(FrameType.BMC_PAYLOAD)
|
|
|
+ ]
|
|
|
+ sensor_cache_frames = [
|
|
|
+ frame for frame in debug_frames if int(frame.get("frame_type", -1)) == int(FrameType.SENSOR_CACHE)
|
|
|
+ ]
|
|
|
+ module_snapshot_frames = [
|
|
|
+ frame for frame in debug_frames if int(frame.get("frame_type", -1)) == int(FrameType.MODULE_SNAPSHOT)
|
|
|
+ ]
|
|
|
+ latest_module_snapshot = (
|
|
|
+ module_snapshot_frames[-1].get("decoded")
|
|
|
+ if module_snapshot_frames and isinstance(module_snapshot_frames[-1].get("decoded"), dict)
|
|
|
+ else {}
|
|
|
+ )
|
|
|
+ host_command_coverage = {
|
|
|
+ "set_ptc_channels": sorted(
|
|
|
+ int(command["channel"])
|
|
|
+ for command in host_commands
|
|
|
+ if command.get("name") == "SET_PTC" and command.get("channel") is not None
|
|
|
+ ),
|
|
|
+ "set_fan_channels": sorted(
|
|
|
+ int(command["channel"])
|
|
|
+ for command in host_commands
|
|
|
+ if command.get("name") == "SET_FAN" and command.get("channel") is not None
|
|
|
+ ),
|
|
|
+ "pid_requested_values": [
|
|
|
+ int(command["percent"])
|
|
|
+ for command in host_commands
|
|
|
+ if command.get("name") == "SET_PID_REQUESTED" and command.get("percent") is not None
|
|
|
+ ],
|
|
|
+ "disable_seen": any(command.get("name") == "DISABLE" for command in host_commands),
|
|
|
+ }
|
|
|
+ pwm_logic_evidence = {
|
|
|
+ "status": "pass"
|
|
|
+ if host_command_coverage["set_ptc_channels"] == [1, 24]
|
|
|
+ and host_command_coverage["set_fan_channels"] == [1, 4]
|
|
|
+ and ack_matched_count == len(COMMAND_SEQUENCE)
|
|
|
+ else "blocked",
|
|
|
+ "requested_ptc_channels": host_command_coverage["set_ptc_channels"],
|
|
|
+ "requested_fan_channels": host_command_coverage["set_fan_channels"],
|
|
|
+ "host_ack_matched_count": ack_matched_count,
|
|
|
+ }
|
|
|
+ pid_evidence = {
|
|
|
+ "status": "pass"
|
|
|
+ if host_command_coverage["pid_requested_values"] == [1, 0]
|
|
|
+ and latest_module_snapshot.get("pid_requested") is False
|
|
|
+ else "blocked",
|
|
|
+ "requested_values": host_command_coverage["pid_requested_values"],
|
|
|
+ "latest_pid_requested": latest_module_snapshot.get("pid_requested"),
|
|
|
+ "policy": "pid_request_is_recorded_but_pid_control_remains_inactive_in_this_profile",
|
|
|
+ }
|
|
|
+ timeout_fallback_evidence = {
|
|
|
+ "status": "pass"
|
|
|
+ if latest_module_snapshot.get("timeout_active") is True
|
|
|
+ and latest_module_snapshot.get("ptc01_percent") == 0
|
|
|
+ and latest_module_snapshot.get("ptc24_percent") == 0
|
|
|
+ and latest_module_snapshot.get("fan01_percent") == 30
|
|
|
+ and latest_module_snapshot.get("fan04_percent") == 30
|
|
|
+ else "blocked",
|
|
|
+ "latest_timeout_active": latest_module_snapshot.get("timeout_active"),
|
|
|
+ "latest_ptc01_percent": latest_module_snapshot.get("ptc01_percent"),
|
|
|
+ "latest_ptc24_percent": latest_module_snapshot.get("ptc24_percent"),
|
|
|
+ "latest_fan01_percent": latest_module_snapshot.get("fan01_percent"),
|
|
|
+ "latest_fan04_percent": latest_module_snapshot.get("fan04_percent"),
|
|
|
+ }
|
|
|
+ blockers: list[str] = []
|
|
|
+ if len(host_commands) != len(COMMAND_SEQUENCE):
|
|
|
+ blockers.append("wrong_host_command_count")
|
|
|
+ if len(ack_frames) != len(COMMAND_SEQUENCE):
|
|
|
+ blockers.append("wrong_host_ack_count")
|
|
|
+ if ack_matched_count != len(COMMAND_SEQUENCE):
|
|
|
+ blockers.append("host_ack_mismatch")
|
|
|
+ if not bmc_payload_frames:
|
|
|
+ blockers.append("missing_bmc_payload")
|
|
|
+ if not sensor_cache_frames:
|
|
|
+ blockers.append("missing_sensor_cache")
|
|
|
+ if not isinstance(bmc_replay, dict) or bmc_replay.get("status") != "pass":
|
|
|
+ blockers.append("missing_or_blocked_bmc_replay")
|
|
|
+ if profile == "production-two-uart-bmc-replay-binary921":
|
|
|
+ if not module_snapshot_frames:
|
|
|
+ blockers.append("missing_module_snapshot")
|
|
|
+ if latest_module_snapshot.get("profile_id_payload") != profile_id:
|
|
|
+ blockers.append("module_snapshot_profile_mismatch")
|
|
|
+ if int(latest_module_snapshot.get("command_count_seen") or 0) < len(COMMAND_SEQUENCE):
|
|
|
+ blockers.append("module_snapshot_missing_command_coverage")
|
|
|
+ if latest_module_snapshot.get("host_err_count") not in (0, None):
|
|
|
+ blockers.append("module_snapshot_host_errors_nonzero")
|
|
|
+ if pwm_logic_evidence["status"] != "pass":
|
|
|
+ blockers.append("pwm_logic_evidence_incomplete")
|
|
|
+ if pid_evidence["status"] != "pass":
|
|
|
+ blockers.append("pid_inactive_evidence_incomplete")
|
|
|
+ if timeout_fallback_evidence["status"] != "pass":
|
|
|
+ blockers.append("timeout_fallback_evidence_incomplete")
|
|
|
+
|
|
|
+ status = "pass" if not blockers else ("blocked_host_ack" if any("ack" in item for item in blockers) else "blocked_evidence_incomplete")
|
|
|
+ artifact_hashes = {
|
|
|
+ "debug-uart-binary921.raw": sha256_bytes(debug_raw),
|
|
|
+ "host-port-binary921.raw": sha256_bytes(host_raw),
|
|
|
+ }
|
|
|
+ if combined_raw is not None:
|
|
|
+ artifact_hashes = {"combined-uart-binary921.raw": sha256_bytes(combined_raw)}
|
|
|
+
|
|
|
+ return {
|
|
|
+ "schema": "j6-binary921-evidence-manifest-v1",
|
|
|
+ "status": status,
|
|
|
+ "generated_at": utc_now(),
|
|
|
+ "protocol": "binary921",
|
|
|
+ "profile": profile,
|
|
|
+ "transport": transport,
|
|
|
+ "combined_uart_baud": 921600 if transport == "combined_host_debug_uart" else None,
|
|
|
+ "bmc_replay_uart_baud": 115200,
|
|
|
+ "bmc_source": "pc_replay_from_real_bmc_capture",
|
|
|
+ "live_bmc_link_status": "not_used",
|
|
|
+ "real_fan_ptc_hardware_status": "not_run",
|
|
|
+ "startup_ready": startup_ready or {},
|
|
|
+ "host_command_count": len(host_commands),
|
|
|
+ "host_ack_count": len(ack_frames),
|
|
|
+ "host_ack_matched_count": ack_matched_count,
|
|
|
+ "host_ack_matches": ack_matches,
|
|
|
+ "debug_frame_count": len(debug_frames),
|
|
|
+ "bmc_payload_frame_count": len(bmc_payload_frames),
|
|
|
+ "sensor_cache_frame_count": len(sensor_cache_frames),
|
|
|
+ "module_snapshot_count": len(module_snapshot_frames),
|
|
|
+ "latest_module_snapshot": latest_module_snapshot,
|
|
|
+ "host_command_coverage": host_command_coverage,
|
|
|
+ "pwm_logic_evidence": pwm_logic_evidence,
|
|
|
+ "pid_evidence": pid_evidence,
|
|
|
+ "timeout_fallback_evidence": timeout_fallback_evidence,
|
|
|
+ "bmc_replay": bmc_replay or {"status": "missing"},
|
|
|
+ "blockers": blockers,
|
|
|
+ "artifact_hashes": artifact_hashes,
|
|
|
+ "safety": {
|
|
|
+ "flash_attempted": False,
|
|
|
+ "driver_install_attempted": False,
|
|
|
+ "programs_nonvolatile": False,
|
|
|
+ "enumerates_com_ports": False,
|
|
|
+ "debug_port_writes_to_serial": False,
|
|
|
+ "toggles_dtr_rts": False,
|
|
|
+ },
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def write_blocked_manifest(
|
|
|
+ output_dir: Path,
|
|
|
+ *,
|
|
|
+ profile: str,
|
|
|
+ status: str,
|
|
|
+ startup_raw: bytes,
|
|
|
+ startup_frames: list[dict[str, Any]],
|
|
|
+ error: str,
|
|
|
+) -> dict[str, Any]:
|
|
|
+ output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
+ (output_dir / "debug-uart-binary921.raw").write_bytes(startup_raw)
|
|
|
+ (output_dir / "host-port-binary921.raw").write_bytes(b"")
|
|
|
+ (output_dir / "debug-binary921-frames.json").write_text(json.dumps(startup_frames, indent=2) + "\n", encoding="utf-8")
|
|
|
+ (output_dir / "host-binary921-frames.json").write_text("[]\n", encoding="utf-8")
|
|
|
+ (output_dir / "host-command-transcript.json").write_text("[]\n", encoding="utf-8")
|
|
|
+ (output_dir / "startup-preflight.json").write_text(
|
|
|
+ json.dumps(
|
|
|
+ {
|
|
|
+ "status": status,
|
|
|
+ "ready_seen": False,
|
|
|
+ "pre_sram_noise_sha256": sha256_bytes(startup_raw),
|
|
|
+ "error": error,
|
|
|
+ },
|
|
|
+ indent=2,
|
|
|
+ )
|
|
|
+ + "\n",
|
|
|
+ encoding="utf-8",
|
|
|
+ )
|
|
|
+ manifest = {
|
|
|
+ "schema": "j6-binary921-evidence-manifest-v1",
|
|
|
+ "status": status,
|
|
|
+ "generated_at": utc_now(),
|
|
|
+ "protocol": "binary921",
|
|
|
+ "profile": profile,
|
|
|
+ "transport": "combined_host_debug_uart" if is_combined_uart_profile(profile) else "separate_host_debug_uart",
|
|
|
+ "bmc_source": "pc_replay_from_real_bmc_capture",
|
|
|
+ "live_bmc_link_status": "not_used",
|
|
|
+ "real_fan_ptc_hardware_status": "not_run",
|
|
|
+ "startup_ready": {},
|
|
|
+ "startup_pre_sram_noise_sha256": sha256_bytes(startup_raw),
|
|
|
+ "host_command_count": 0,
|
|
|
+ "host_ack_count": 0,
|
|
|
+ "host_ack_matched_count": 0,
|
|
|
+ "debug_frame_count": len(startup_frames),
|
|
|
+ "bmc_payload_frame_count": 0,
|
|
|
+ "sensor_cache_frame_count": 0,
|
|
|
+ "bmc_replay": {"status": "not_run"},
|
|
|
+ "blockers": [status],
|
|
|
+ "error": error,
|
|
|
+ "artifact_hashes": {
|
|
|
+ "debug-uart-binary921.raw": sha256_bytes(startup_raw),
|
|
|
+ "host-port-binary921.raw": sha256_bytes(b""),
|
|
|
+ },
|
|
|
+ "safety": {
|
|
|
+ "flash_attempted": False,
|
|
|
+ "driver_install_attempted": False,
|
|
|
+ "programs_nonvolatile": False,
|
|
|
+ "enumerates_com_ports": False,
|
|
|
+ "debug_port_writes_to_serial": False,
|
|
|
+ "toggles_dtr_rts": False,
|
|
|
+ },
|
|
|
+ }
|
|
|
+ (output_dir / "binary921-evidence-manifest.json").write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")
|
|
|
+ return manifest
|
|
|
+
|
|
|
+
|
|
|
+def collect_live(args: argparse.Namespace) -> dict[str, Any]:
|
|
|
+ output_dir = Path(args.output_dir)
|
|
|
+ output_dir.mkdir(parents=True, exist_ok=True)
|
|
|
+ profile_id = _profile_id(args.profile)
|
|
|
+ transport = transport_for_args(args)
|
|
|
+
|
|
|
+ if transport == "combined-single-com":
|
|
|
+ combined_port = getattr(args, "combined_port", "") or args.host_port
|
|
|
+ _require_explicit_com("CombinedPort", combined_port)
|
|
|
+
|
|
|
+ combined_serial = _open_serial(combined_port, 921600)
|
|
|
+ host_commands: list[dict[str, Any]] = []
|
|
|
+ all_frames: list[dict[str, Any]] = []
|
|
|
+ combined_raw = bytearray()
|
|
|
+ ready: dict[str, Any] | None = None
|
|
|
+ try:
|
|
|
+ try:
|
|
|
+ ready, ready_raw, ready_frames = wait_for_ready(combined_serial, timeout_sec=args.ready_timeout_sec)
|
|
|
+ except TimeoutError as exc:
|
|
|
+ return write_blocked_manifest(
|
|
|
+ output_dir,
|
|
|
+ profile=args.profile,
|
|
|
+ status="blocked_waiting_for_binary921_ready",
|
|
|
+ startup_raw=bytes(combined_raw),
|
|
|
+ startup_frames=all_frames,
|
|
|
+ error=str(exc),
|
|
|
+ )
|
|
|
+ combined_raw += ready_raw
|
|
|
+ all_frames.extend(ready_frames)
|
|
|
+ for index, (name, command, channel, percent) in enumerate(COMMAND_SEQUENCE, start=1):
|
|
|
+ frame_bytes = build_host_command(
|
|
|
+ seq=index,
|
|
|
+ command=command,
|
|
|
+ channel=channel,
|
|
|
+ percent=percent,
|
|
|
+ profile_id=profile_id,
|
|
|
+ )
|
|
|
+ combined_serial.write(frame_bytes)
|
|
|
+ combined_serial.flush()
|
|
|
+ host_commands.append(
|
|
|
+ {
|
|
|
+ "seq": index,
|
|
|
+ "name": name,
|
|
|
+ "command_id": int(command),
|
|
|
+ "channel": channel,
|
|
|
+ "percent": percent,
|
|
|
+ "wire_sha256": sha256_bytes(frame_bytes),
|
|
|
+ }
|
|
|
+ )
|
|
|
+ c_raw, c_frames = drain_combined_frames(combined_serial, duration_sec=args.ack_window_sec)
|
|
|
+ combined_raw += c_raw
|
|
|
+ all_frames.extend(c_frames)
|
|
|
+ c_raw, c_frames = drain_combined_frames(combined_serial, duration_sec=max(0.0, args.duration_sec))
|
|
|
+ combined_raw += c_raw
|
|
|
+ all_frames.extend(c_frames)
|
|
|
+ finally:
|
|
|
+ combined_serial.close()
|
|
|
+
|
|
|
+ (output_dir / "combined-uart-binary921.raw").write_bytes(bytes(combined_raw))
|
|
|
+ (output_dir / "combined-binary921-frames.json").write_text(json.dumps(all_frames, indent=2) + "\n", encoding="utf-8")
|
|
|
+ (output_dir / "debug-uart-binary921.raw").write_bytes(bytes(combined_raw))
|
|
|
+ (output_dir / "host-port-binary921.raw").write_bytes(bytes(combined_raw))
|
|
|
+ (output_dir / "debug-binary921-frames.json").write_text(json.dumps(all_frames, indent=2) + "\n", encoding="utf-8")
|
|
|
+ (output_dir / "host-binary921-frames.json").write_text(json.dumps(all_frames, indent=2) + "\n", encoding="utf-8")
|
|
|
+ (output_dir / "host-command-transcript.json").write_text(json.dumps(host_commands, indent=2) + "\n", encoding="utf-8")
|
|
|
+ (output_dir / "startup-preflight.json").write_text(
|
|
|
+ json.dumps({"status": "pass", "ready": ready, "transport": "combined_host_debug_uart"}, indent=2) + "\n",
|
|
|
+ encoding="utf-8",
|
|
|
+ )
|
|
|
+
|
|
|
+ bmc_replay = {}
|
|
|
+ if args.bmc_replay_receipt:
|
|
|
+ try:
|
|
|
+ bmc_replay = json.loads(Path(args.bmc_replay_receipt).read_text(encoding="utf-8"))
|
|
|
+ except (OSError, json.JSONDecodeError) as exc:
|
|
|
+ bmc_replay = {"status": "blocked_unreadable_bmc_replay_receipt", "error": str(exc)}
|
|
|
+ manifest = build_manifest_from_frames(
|
|
|
+ profile=args.profile,
|
|
|
+ startup_ready=ready,
|
|
|
+ host_commands=host_commands,
|
|
|
+ host_frames=all_frames,
|
|
|
+ debug_frames=all_frames,
|
|
|
+ debug_raw=bytes(combined_raw),
|
|
|
+ host_raw=bytes(combined_raw),
|
|
|
+ combined_raw=bytes(combined_raw),
|
|
|
+ bmc_replay=bmc_replay,
|
|
|
+ )
|
|
|
+ (output_dir / "binary921-evidence-manifest.json").write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")
|
|
|
+ return manifest
|
|
|
+
|
|
|
+ if transport != "two-uart":
|
|
|
+ raise ValueError(f"unknown binary921 transport: {transport}")
|
|
|
+ _require_explicit_com("HostPort", args.host_port)
|
|
|
+ _require_explicit_com("DebugPort", args.debug_port)
|
|
|
+ if args.host_port.upper() == args.debug_port.upper():
|
|
|
+ raise ValueError("HostPort and DebugPort must be different; use --transport combined-single-com --combined-port for production combined UART")
|
|
|
+
|
|
|
+ host_serial = _open_serial(args.host_port, 115200)
|
|
|
+ debug_serial = _open_serial(args.debug_port, 921600)
|
|
|
+ host_commands: list[dict[str, Any]] = []
|
|
|
+ all_debug_frames: list[dict[str, Any]] = []
|
|
|
+ all_host_frames: list[dict[str, Any]] = []
|
|
|
+ debug_raw = bytearray()
|
|
|
+ host_raw = bytearray()
|
|
|
+ ready: dict[str, Any] | None = None
|
|
|
+ try:
|
|
|
+ try:
|
|
|
+ ready, ready_raw, ready_frames = wait_for_ready(debug_serial, timeout_sec=args.ready_timeout_sec)
|
|
|
+ except TimeoutError as exc:
|
|
|
+ return write_blocked_manifest(
|
|
|
+ output_dir,
|
|
|
+ profile=args.profile,
|
|
|
+ status="blocked_waiting_for_binary921_ready",
|
|
|
+ startup_raw=bytes(debug_raw),
|
|
|
+ startup_frames=all_debug_frames,
|
|
|
+ error=str(exc),
|
|
|
+ )
|
|
|
+ debug_raw += ready_raw
|
|
|
+ all_debug_frames.extend(ready_frames)
|
|
|
+ for index, (name, command, channel, percent) in enumerate(COMMAND_SEQUENCE, start=1):
|
|
|
+ frame_bytes = build_host_command(seq=index, command=command, channel=channel, percent=percent, profile_id=profile_id)
|
|
|
+ host_serial.write(frame_bytes)
|
|
|
+ host_serial.flush()
|
|
|
+ host_commands.append(
|
|
|
+ {
|
|
|
+ "seq": index,
|
|
|
+ "name": name,
|
|
|
+ "command_id": int(command),
|
|
|
+ "channel": channel,
|
|
|
+ "percent": percent,
|
|
|
+ "wire_sha256": sha256_bytes(frame_bytes),
|
|
|
+ }
|
|
|
+ )
|
|
|
+ d_raw, h_raw, d_frames, h_frames = drain_frames(debug_serial, host_serial, duration_sec=args.ack_window_sec)
|
|
|
+ debug_raw += d_raw
|
|
|
+ host_raw += h_raw
|
|
|
+ all_debug_frames.extend(d_frames)
|
|
|
+ all_host_frames.extend(h_frames)
|
|
|
+ d_raw, h_raw, d_frames, h_frames = drain_frames(debug_serial, host_serial, duration_sec=max(0.0, args.duration_sec))
|
|
|
+ debug_raw += d_raw
|
|
|
+ host_raw += h_raw
|
|
|
+ all_debug_frames.extend(d_frames)
|
|
|
+ all_host_frames.extend(h_frames)
|
|
|
+ finally:
|
|
|
+ debug_serial.close()
|
|
|
+ host_serial.close()
|
|
|
+
|
|
|
+ (output_dir / "debug-uart-binary921.raw").write_bytes(bytes(debug_raw))
|
|
|
+ (output_dir / "host-port-binary921.raw").write_bytes(bytes(host_raw))
|
|
|
+ (output_dir / "debug-binary921-frames.json").write_text(json.dumps(all_debug_frames, indent=2) + "\n", encoding="utf-8")
|
|
|
+ (output_dir / "host-binary921-frames.json").write_text(json.dumps(all_host_frames, indent=2) + "\n", encoding="utf-8")
|
|
|
+ (output_dir / "host-command-transcript.json").write_text(json.dumps(host_commands, indent=2) + "\n", encoding="utf-8")
|
|
|
+ (output_dir / "startup-preflight.json").write_text(json.dumps({"status": "pass", "ready": ready}, indent=2) + "\n", encoding="utf-8")
|
|
|
+
|
|
|
+ bmc_replay = {}
|
|
|
+ if args.bmc_replay_receipt:
|
|
|
+ try:
|
|
|
+ bmc_replay = json.loads(Path(args.bmc_replay_receipt).read_text(encoding="utf-8"))
|
|
|
+ except (OSError, json.JSONDecodeError) as exc:
|
|
|
+ bmc_replay = {"status": "blocked_unreadable_bmc_replay_receipt", "error": str(exc)}
|
|
|
+ manifest = build_manifest_from_frames(
|
|
|
+ profile=args.profile,
|
|
|
+ startup_ready=ready,
|
|
|
+ host_commands=host_commands,
|
|
|
+ host_frames=all_host_frames,
|
|
|
+ debug_frames=all_debug_frames,
|
|
|
+ debug_raw=bytes(debug_raw),
|
|
|
+ host_raw=bytes(host_raw),
|
|
|
+ bmc_replay=bmc_replay,
|
|
|
+ )
|
|
|
+ (output_dir / "binary921-evidence-manifest.json").write_text(json.dumps(manifest, indent=2) + "\n", encoding="utf-8")
|
|
|
+ return manifest
|
|
|
+
|
|
|
+
|
|
|
+def run_selftest() -> dict[str, Any]:
|
|
|
+ from binary921 import encode_frame, decode_frame
|
|
|
+
|
|
|
+ ready = frame_to_dict(decode_frame(encode_frame(profile_id=1, frame_type=FrameType.READY, seq=1, timestamp_tick=2, payload=b"\x01\x00\x00\x00")))
|
|
|
+ commands = [
|
|
|
+ frame_to_dict(decode_stream(build_host_command(seq=index, command=command, channel=channel, percent=percent))[0][0])
|
|
|
+ for index, (_, command, channel, percent) in enumerate(COMMAND_SEQUENCE, start=1)
|
|
|
+ ]
|
|
|
+ host_command_rows = [
|
|
|
+ {
|
|
|
+ "seq": int(frame["seq"]),
|
|
|
+ "name": COMMAND_SEQUENCE[index - 1][0],
|
|
|
+ "command_id": int(COMMAND_SEQUENCE[index - 1][1]),
|
|
|
+ "channel": COMMAND_SEQUENCE[index - 1][2],
|
|
|
+ "percent": COMMAND_SEQUENCE[index - 1][3],
|
|
|
+ }
|
|
|
+ for index, frame in enumerate(commands, start=1)
|
|
|
+ ]
|
|
|
+ ack_frames = [
|
|
|
+ frame_to_dict(
|
|
|
+ decode_frame(
|
|
|
+ encode_frame(
|
|
|
+ profile_id=1,
|
|
|
+ frame_type=FrameType.HOST_ACK,
|
|
|
+ seq=row["seq"],
|
|
|
+ timestamp_tick=0,
|
|
|
+ payload=bytes([1, row["command_id"], 0, 0, 0x34, 0x12]),
|
|
|
+ )
|
|
|
+ )
|
|
|
+ )
|
|
|
+ for row in host_command_rows
|
|
|
+ ]
|
|
|
+ bmc_payload = frame_to_dict(
|
|
|
+ decode_frame(
|
|
|
+ encode_frame(
|
|
|
+ profile_id=1,
|
|
|
+ frame_type=FrameType.BMC_PAYLOAD,
|
|
|
+ seq=2,
|
|
|
+ timestamp_tick=0,
|
|
|
+ payload=bytes([48, 48, 0, 1, 0x28, 0, 0x04, 0x14]),
|
|
|
+ )
|
|
|
+ )
|
|
|
+ )
|
|
|
+ sensor_cache = frame_to_dict(
|
|
|
+ decode_frame(
|
|
|
+ encode_frame(
|
|
|
+ profile_id=1,
|
|
|
+ frame_type=FrameType.SENSOR_CACHE,
|
|
|
+ seq=3,
|
|
|
+ timestamp_tick=0,
|
|
|
+ payload=bytes([1, 0, 1, 0, 0, 0, 24, 0]),
|
|
|
+ )
|
|
|
+ )
|
|
|
+ )
|
|
|
+ manifest = build_manifest_from_frames(
|
|
|
+ profile="debug-photo-bmc-replay-binary921",
|
|
|
+ startup_ready=ready,
|
|
|
+ host_commands=host_command_rows,
|
|
|
+ host_frames=ack_frames,
|
|
|
+ debug_frames=[ready, bmc_payload, sensor_cache],
|
|
|
+ debug_raw=b"debug",
|
|
|
+ host_raw=b"host",
|
|
|
+ bmc_replay={
|
|
|
+ "status": "pass",
|
|
|
+ "bmc_source": "pc_replay_from_real_bmc_capture",
|
|
|
+ "byte_count": 48,
|
|
|
+ "valid_count": 24,
|
|
|
+ "missing_count": 0,
|
|
|
+ "payload_sha256": "0" * 64,
|
|
|
+ },
|
|
|
+ )
|
|
|
+ if ready["decoded"]["kind"] != "ready":
|
|
|
+ raise AssertionError("READY decode failed")
|
|
|
+ if commands[0]["decoded"]["cmd"] != "SET_PTC":
|
|
|
+ raise AssertionError("SET_PTC build/decode failed")
|
|
|
+ if manifest["status"] != "pass":
|
|
|
+ raise AssertionError(f"manifest selftest did not pass: {manifest}")
|
|
|
+ return {
|
|
|
+ "schema": "j6-binary921-collector-selftest-v1",
|
|
|
+ "status": "pass",
|
|
|
+ "ready": ready,
|
|
|
+ "command_count": len(commands),
|
|
|
+ "first_command": commands[0],
|
|
|
+ "manifest": manifest,
|
|
|
+ "bmc_source": "pc_replay_from_real_bmc_capture",
|
|
|
+ "real_fan_ptc_hardware_status": "not_run",
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+def build_parser() -> argparse.ArgumentParser:
|
|
|
+ parser = argparse.ArgumentParser(description=__doc__)
|
|
|
+ parser.add_argument("--host-port", dest="host_port", default="")
|
|
|
+ parser.add_argument("--debug-port", dest="debug_port", default="")
|
|
|
+ parser.add_argument("--combined-port", dest="combined_port", default="")
|
|
|
+ parser.add_argument("--transport", choices=["auto", "two-uart", "combined-single-com"], default="auto")
|
|
|
+ parser.add_argument("--profile", choices=ALL_PROFILES, default="debug-photo-bmc-replay-binary921")
|
|
|
+ parser.add_argument("--output-dir", default="evidence-binary921")
|
|
|
+ parser.add_argument("--duration-sec", type=float, default=5.0)
|
|
|
+ parser.add_argument("--ready-timeout-sec", type=float, default=30.0)
|
|
|
+ parser.add_argument("--ack-window-sec", type=float, default=0.25)
|
|
|
+ parser.add_argument("--bitstream", default="")
|
|
|
+ parser.add_argument("--td-receipt", default="")
|
|
|
+ parser.add_argument("--bmc-replay-receipt", default="")
|
|
|
+ parser.add_argument("--selftest", action="store_true")
|
|
|
+ parser.add_argument("--json", action="store_true")
|
|
|
+ return parser
|
|
|
+
|
|
|
+
|
|
|
+def main(argv: list[str] | None = None) -> int:
|
|
|
+ args = build_parser().parse_args(argv)
|
|
|
+ if args.selftest:
|
|
|
+ payload = run_selftest()
|
|
|
+ else:
|
|
|
+ payload = collect_live(args)
|
|
|
+ if args.json or args.selftest:
|
|
|
+ print(json.dumps(payload, indent=2))
|
|
|
+ if args.selftest:
|
|
|
+ print("selftest=pass")
|
|
|
+ return 0 if payload["status"] == "pass" else 1
|
|
|
+
|
|
|
+
|
|
|
+if __name__ == "__main__":
|
|
|
+ raise SystemExit(main(sys.argv[1:]))
|