Просмотр исходного кода

fix(package): mark unfrozen 6400 firmware as review candidate

root 3 месяцев назад
Родитель
Сommit
4e4ffc7d1b
2 измененных файлов с 136 добавлено и 19 удалено
  1. 43 8
      tests/unit/test_6400_profile_package.py
  2. 93 11
      tools/package_target.py

+ 43 - 8
tests/unit/test_6400_profile_package.py

@@ -35,6 +35,8 @@ def _receipt(
         "source_closure_sha256": package_target._sha256(
             ROOT / str(profile["closure"])
         ),
+        "known_unbound_ports": ["host_ttl_rx", "host_ttl_tx"],
+        "deployment_status": "UNSAFE_UNTIL_HOST_TTL_PINS_ARE_FROZEN_AND_HARDWARE_VALIDATED",
         "bitstream": str(bitstream),
         "bitstream_sha256": package_target._sha256(bitstream),
     }
@@ -99,23 +101,56 @@ def test_control_source_package_contains_only_active_grouped_closure(tmp_path: P
     assert not any("prod2u_6400_front_back_top" in name for name in names)
 
 
-def test_identification_firmware_package_is_distinct_and_receipt_bound(
+@pytest.mark.parametrize(
+    ("profile_name", "expected_bit", "role", "production", "stimulus", "contract_text"),
+    [
+        (
+            "6400-control",
+            "prod2u_6400_front_back.bit",
+            "production",
+            True,
+            False,
+            "stimulus=false",
+        ),
+        (
+            "6400-identification",
+            "prod2u_6400_identification.bit",
+            "identification",
+            False,
+            True,
+            "不得作为 Production",
+        ),
+    ],
+)
+def test_firmware_package_is_fail_closed_and_profile_bound(
     tmp_path: Path,
     monkeypatch: pytest.MonkeyPatch,
+    profile_name: str,
+    expected_bit: str,
+    role: str,
+    production: bool,
+    stimulus: bool,
+    contract_text: str,
 ) -> None:
     monkeypatch.setattr(package_target, "_git", lambda *_args: TEST_COMMIT)
-    receipt = _receipt(tmp_path, "6400-identification")
+    receipt = _receipt(tmp_path, profile_name)
     archive = package_target.build_target_package(
-        "6400-identification", "firmware", tmp_path / "out", td_receipt=receipt
+        profile_name, "firmware", tmp_path / "out", td_receipt=receipt
     )
     with zipfile.ZipFile(archive) as zipf:
         names = set(zipf.namelist())
         manifest = json.loads(zipf.read("manifest.json"))
         instructions = zipf.read("FLASH-INSTRUCTIONS.zh-CN.txt").decode("utf-8")
-    assert manifest["profile"] == "6400-identification"
-    assert manifest["firmware_role"] == "identification"
-    assert "prod2u_6400_identification.bit" in names
-    assert "prod2u_6400_front_back.bit" not in names
+    assert manifest["profile"] == profile_name
+    assert manifest["firmware_role"] == role
+    assert manifest["production"] is production
+    assert manifest["stimulus"] is stimulus
+    assert expected_bit in names
     assert "921600 8N1" in instructions
     assert "独立 ACK" in instructions
-    assert "不得作为 Production" in instructions
+    assert contract_text in instructions
+    assert instructions.startswith(package_target.DO_NOT_PROGRAM_LINE)
+    assert manifest["release_blocked"] is True
+    assert manifest["deployable"] is False
+    assert manifest["known_unbound_ports"] == ["host_ttl_rx", "host_ttl_tx"]
+    assert "CANDIDATE-NOTICE.zh-CN.txt" in names

+ 93 - 11
tools/package_target.py

@@ -26,6 +26,10 @@ from tools.run_6400_td import HARDWARE_PROFILES, source_closure as td_source_clo
 from tools.target_registry import ROOT, load_source_closure, release_blocked, resolve_target
 
 
+CANDIDATE_NOTICE_ZH_CN = "候选,仅供审核;未冻结TTL Pin/未做硬件验证前不要烧录"
+DO_NOT_PROGRAM_LINE = "DO NOT PROGRAM / 不要烧录"
+
+
 def _sha256(path: Path) -> str:
     digest = hashlib.sha256()
     with path.open("rb") as handle:
@@ -103,6 +107,23 @@ def _validated_td_bitstream(
     ]
     if mismatches:
         raise SystemExit("6400 firmware package blocked: TD receipt mismatch: " + "; ".join(mismatches))
+    known_unbound_ports = receipt.get("known_unbound_ports")
+    if (
+        not isinstance(known_unbound_ports, list)
+        or any(
+            not isinstance(port, str) or not port
+            for port in known_unbound_ports
+        )
+        or len(set(known_unbound_ports)) != len(known_unbound_ports)
+    ):
+        raise SystemExit(
+            "6400 firmware package blocked: TD receipt has invalid known_unbound_ports"
+        )
+    deployment_status = receipt.get("deployment_status")
+    if not isinstance(deployment_status, str) or not deployment_status:
+        raise SystemExit(
+            "6400 firmware package blocked: TD receipt has invalid deployment_status"
+        )
     bitstream_value = receipt.get("bitstream")
     if not isinstance(bitstream_value, str) or not bitstream_value:
         raise SystemExit("6400 firmware package blocked: TD receipt has no bitstream")
@@ -129,6 +150,9 @@ def build_target_package(
     is_6400 = target_name == "6400" or target_name.startswith("6400-")
     receipt_payload: dict[str, object] | None = None
     expanded_closure: dict[str, object] | None = None
+    hardware_validation = "NOT RUN"
+    known_unbound_ports: tuple[str, ...] = ()
+    deployment_status: str | None = None
     if is_6400:
         selected_profile = _resolve_6400_profile(target_name, profile_name)
         profile = HARDWARE_PROFILES[selected_profile]
@@ -175,6 +199,15 @@ def build_target_package(
         bitstream_path, receipt_payload = _validated_td_bitstream(
             selected_profile, td_receipt, commit
         )
+        ports_value = receipt_payload["known_unbound_ports"]
+        status_value = receipt_payload["deployment_status"]
+        assert isinstance(ports_value, list)
+        assert all(isinstance(port, str) for port in ports_value)
+        assert isinstance(status_value, str)
+        known_unbound_ports = tuple(ports_value)
+        deployment_status = status_value
+        blocked = bool(known_unbound_ports) or hardware_validation != "PASS"
+        pins = known_unbound_ports
     run_id = datetime.now(timezone.utc).strftime("%Y%m%dT%H%M%SZ")
     name = f"prod2u-{package_label}-{kind}-{commit[:12]}-{run_id}"
     output_dir.mkdir(parents=True, exist_ok=True)
@@ -196,14 +229,42 @@ def build_target_package(
         "project_config_sha256": _sha256(project_path),
         "source_manifest_sha256": _sha256(closure_path),
         "bitstream_sha256": _sha256(bitstream_path) if bitstream_path else None,
-        "hardware_validation": "NOT RUN",
+        "hardware_validation": hardware_validation,
         "release_blocked": blocked,
         "unresolved_pins": list(pins),
         "include_reference": include_reference,
     }
+    if is_6400:
+        manifest.update(
+            {
+                "production": bool(profile["production"]),
+                "stimulus": bool(profile["stimulus"]),
+                "simulation_only": False,
+            }
+        )
+    if is_6400 and kind == "firmware":
+        manifest.update(
+            {
+                "candidate_notice_zh_CN": CANDIDATE_NOTICE_ZH_CN,
+                "candidate_only": blocked,
+                "deployable": not blocked,
+                "deployment_status": deployment_status,
+                "known_unbound_ports": list(known_unbound_ports),
+                "unresolved_ports": list(known_unbound_ports),
+            }
+        )
     with tempfile.TemporaryDirectory() as tmp:
         stage = Path(tmp)
         (stage / "manifest.json").write_text(json.dumps(manifest, indent=2, sort_keys=True) + "\n", encoding="utf-8")
+        if is_6400 and kind == "firmware" and blocked:
+            (stage / "CANDIDATE-NOTICE.zh-CN.txt").write_text(
+                DO_NOT_PROGRAM_LINE + "\n"
+                + CANDIDATE_NOTICE_ZH_CN + "\n"
+                + f"未冻结端口:{', '.join(known_unbound_ports)}\n"
+                + f"部署状态:{deployment_status}\n"
+                + f"硬件验证:{hardware_validation}\n",
+                encoding="utf-8",
+            )
         shutil.copy2(project_path, stage / "project.json")
         shutil.copy2(closure_path, stage / "source-closure.json")
         if expanded_closure is not None:
@@ -229,20 +290,19 @@ def build_target_package(
             )
             role = "生产" if selected_profile == "6400-control" else "Identification 专用"
             (stage / "FLASH-INSTRUCTIONS.zh-CN.txt").write_text(
-                f"6400 {role}固件说明\n"
+                DO_NOT_PROGRAM_LINE + "\n"
+                + CANDIDATE_NOTICE_ZH_CN + "\n\n"
+                + f"6400 {role}固件审核说明\n"
                 "========================\n"
                 "目标器件:EF2L45BG256B(6400 前后独立温控)\n"
                 f"固件文件:{bitstream_name}\n"
-                "\n"
-                "1. 关闭目标板电源,连接板卡厂商支持的 JTAG 下载器。\n"
-                "2. 在 Anlogic TD 2026.1 SP2 中选择 EF2L45BG256B,打开该 .bit 文件。\n"
-                "3. 先执行 Erase/Program,再执行 Verify;确认校验通过后断电重启。\n"
-                f"4. Host 串口使用 921600 8N1;每条合法命令先独立 ACK;上位机选择 {selected_profile}。\n"
-                "5. 本包只包含 6400 固件,不能用于 MRDIMM 门控板。\n"
+                "请审核 BITSTREAM-SHA256.txt 与 TD-BUILD-RECEIPT.json 后再冻结制品。\n"
+                f"Host 串口使用 921600 8N1;每条合法命令先独立 ACK;Profile={selected_profile}。\n"
+                "本包只包含 6400 固件,不能用于 MRDIMM 门控板。\n"
                 + (
-                    "6. 本固件含 SET_STIMULUS,仅限 Identification,不得作为 Production。\n"
+                    "本固件含 SET_STIMULUS,仅限 Identification,不得作为 Production。\n"
                     if selected_profile == "6400-identification"
-                    else "6. Production 固件不包含 SET_STIMULUS。\n"
+                    else "Production 固件不包含 SET_STIMULUS(stimulus=false)。\n"
                 )
                 + "\n"
                 "注意:本次交付未执行真实 JTAG/硬件烧录,硬件验证状态为 NOT RUN。\n",
@@ -316,10 +376,32 @@ def _selftest(zip_path: Path) -> None:
                 "bitstream_sha256"
             ) != manifest["bitstream_sha256"]:
                 raise SystemExit("package selftest: TD receipt/profile identity mismatch")
+            receipt_ports = receipt.get("known_unbound_ports")
+            if (
+                manifest.get("release_blocked") is not True
+                or manifest.get("deployable") is not False
+                or manifest.get("candidate_only") is not True
+                or manifest.get("known_unbound_ports") != receipt_ports
+                or manifest.get("unresolved_ports") != receipt_ports
+                or manifest.get("unresolved_pins") != receipt_ports
+                or manifest.get("deployment_status") != receipt.get("deployment_status")
+            ):
+                raise SystemExit("package selftest: unsafe deployment gate mismatch")
+            if not (dest / "CANDIDATE-NOTICE.zh-CN.txt").is_file():
+                raise SystemExit("package selftest: candidate notice missing")
+            production = manifest["profile"] == "6400-control"
+            if manifest.get("production") is not production or manifest.get(
+                "stimulus"
+            ) is production:
+                raise SystemExit("package selftest: firmware profile semantics mismatch")
             instructions = (dest / "FLASH-INSTRUCTIONS.zh-CN.txt").read_text(
                 encoding="utf-8"
             )
-            if "921600 8N1" not in instructions or "独立 ACK" not in instructions:
+            if (
+                not instructions.startswith(DO_NOT_PROGRAM_LINE)
+                or "921600 8N1" not in instructions
+                or "独立 ACK" not in instructions
+            ):
                 raise SystemExit("package selftest: Host UART/ACK contract missing")