ra8-firmware 0.1.0
Bare-metal firmware for the Renesas RA8 family (RA8D2 / RA8P1)
Loading...
Searching...
No Matches
hil_convergence_safety_runtime_sources.py
Go to the documentation of this file.
1# SPDX-License-Identifier: MIT
2# Copyright (c) 2026 Brighton Sikarskie
3"""Publish digest-rebound image-supervisor sources for runtime proofs."""
4
5from __future__ import annotations
6
7import hashlib
8import re
9from pathlib import Path
10
11CASES_PIN_PATTERN = re.compile(r'CASES_RAW_SHA256 = "[0-9a-f]{64}"')
12PROCESS_PIN_PATTERN = re.compile(r'PROCESS_RAW_SHA256 = "[0-9a-f]{64}"')
13SOURCE_MODE = 0o644
14
15
16class RuntimeSourceError(ValueError):
17 """Report ambiguous runtime source binding or publication."""
18
19
20def _rebind_pin(source: str, payload: str, pattern: re.Pattern[str], name: str) -> str:
21 """Replace one exact embedded source digest or refuse ambiguous bytes."""
22 replacement = f'{name}_RAW_SHA256 = "{hashlib.sha256(payload.encode()).hexdigest()}"'
23 rebound, count = pattern.subn(replacement, source, count=1)
24 if count != 1 or len(pattern.findall(source)) != 1:
25 message = f"supervisor {name.lower()} digest assignment is not unique"
26 raise RuntimeSourceError(message)
27 return rebound
28
29
30def publish(
31 root: Path,
32 supervisor: str,
33 process_source: str,
34 cases_source: str,
35) -> tuple[Path, Path, Path]:
36 """Write one exact main/process/cases source bundle under a private root."""
37 rebound = _rebind_pin(supervisor, process_source, PROCESS_PIN_PATTERN, "PROCESS")
38 rebound = _rebind_pin(rebound, cases_source, CASES_PIN_PATTERN, "CASES")
39 main_path = root / "supervisor.py"
40 process_path = root / "supervisor_process.py"
41 cases_path = root / "supervisor_cases.py"
42 main_path.write_text(rebound, encoding="utf-8")
43 process_path.write_text(process_source, encoding="utf-8")
44 cases_path.write_text(cases_source, encoding="utf-8")
45 for path in (main_path, process_path, cases_path):
46 path.chmod(SOURCE_MODE)
47 return main_path, process_path, cases_path