| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294 |
- Set-StrictMode -Version Latest
- $ErrorActionPreference = "Stop"
- $root = Split-Path -Parent $MyInvocation.MyCommand.Path
- $expectedLayerNames = @(
- "python", "icarus", "verilator", "cocotb", "formal",
- "td_primary", "td_confirmation", "td_primary_artifacts",
- "td_confirmation_artifacts", "td_confirmation_bitstream",
- "td_confirmation_synthesis", "td_confirmation_manifest",
- "resource_primary", "resource_confirmation", "package_selftest"
- )
- $verificationEvidenceLayers = @(
- "python", "icarus", "verilator", "cocotb", "formal",
- "td_primary", "td_confirmation", "td_primary_artifacts",
- "td_confirmation_artifacts", "td_confirmation_bitstream",
- "td_confirmation_synthesis", "td_confirmation_manifest",
- "resource_primary", "resource_confirmation"
- )
- $required = @(
- "fw.bit",
- "package-info.json",
- "evidence\td-primary-receipt.json",
- "evidence\validation-summary.json",
- "evidence\package-selftest-result.json",
- "config\host-contract.json",
- "config\prod2u_p4_normal_v3_hw_binding.json",
- "config\prod2u_p4_verification_targets_v1.json",
- "schemas\host-contract.schema.json",
- "SEND_HEX.ps1",
- "INITIALIZE_DISABLE.ps1",
- "PROGRAM_SRAM.ps1",
- "run.ps1",
- "bootstrap.ps1",
- "README.zh-CN.md"
- )
- foreach ($relative in $required) {
- if (-not (Test-Path -LiteralPath (Join-Path $root $relative) -PathType Leaf)) {
- throw "required file missing: $relative"
- }
- }
- function Get-BoundSha256([string]$Path) {
- return (Get-FileHash -Algorithm SHA256 -LiteralPath $Path).Hash.ToLowerInvariant()
- }
- function Assert-ValidJson([string]$Relative) {
- try {
- Get-Content -LiteralPath (Join-Path $root $Relative) -Raw -Encoding UTF8 |
- ConvertFrom-Json | Out-Null
- } catch {
- throw "$Relative is not valid JSON"
- }
- }
- function Get-ValidationSummaryStatus([object]$Layers) {
- $statuses = @($Layers.PSObject.Properties | ForEach-Object { [string]$_.Value.status })
- if ($statuses -contains "fail") { return "fail" }
- if ($statuses -contains "running") { return "running" }
- if ($statuses -contains "not_run") { return "not_run" }
- return "pass"
- }
- foreach ($relative in @(
- "config\prod2u_p4_normal_v3_hw_binding.json",
- "config\prod2u_p4_verification_targets_v1.json"
- )) {
- Assert-ValidJson $relative
- }
- $info = Get-Content -LiteralPath (Join-Path $root "package-info.json") -Raw -Encoding UTF8 | ConvertFrom-Json
- if ([string]$info.schema -ne "prod2u-p4-normal-v3-folder-package-info-v3") {
- throw "package-info schema mismatch"
- }
- $verificationRunId = [string]$info.verification_run_id
- if ($verificationRunId -notmatch '^[0-9a-f]{32}$') {
- throw "package verification_run_id is invalid"
- }
- if ([string]$info.profile -ne "prod2u-p4-normal-actuator-binary921-v3") {
- throw "package profile mismatch"
- }
- $deliveryMode = [string]$info.delivery_mode
- if (-not ($info.verification_complete -is [bool])) {
- throw "package verification_complete must be boolean"
- }
- if (-not ($info.confirmation_td_present -is [bool])) {
- throw "package confirmation_td_present must be boolean"
- }
- $verificationComplete = $info.verification_complete
- $confirmationTdPresent = $info.confirmation_td_present
- if ($deliveryMode -notin @("normal_dual_td", "urgent_single_td")) {
- throw "package delivery mode is invalid"
- }
- if ($deliveryMode -eq "normal_dual_td") {
- if (-not $confirmationTdPresent) {
- throw "normal dual-TD package must contain a confirmation receipt"
- }
- } else {
- if ($verificationComplete -or $confirmationTdPresent) {
- throw "urgent single-TD package completeness fields disagree"
- }
- }
- $commit = [string]$info.commit
- if ($commit -cnotmatch '^[0-9a-f]{8}$') { throw "package commit is invalid" }
- $firmware = Join-Path $root "fw.bit"
- $bitstreamSha256 = Get-BoundSha256 $firmware
- if ([string]$info.bitstream_sha256 -ne $bitstreamSha256) {
- throw "bitstream hash mismatch"
- }
- $expectedName = "prod2u-p4-normal-$commit-$($bitstreamSha256.Substring(0, 16))"
- $currentName = Split-Path -Leaf $root
- if ($currentName -ne $expectedName -and -not $currentName.StartsWith(".prod2u-p4-normal-stage-")) {
- throw "package folder name mismatch"
- }
- if ((Split-Path -Leaf (Split-Path -Parent $root)) -ne $commit) {
- throw "package commit parent folder mismatch"
- }
- $primaryReceipt = Join-Path $root "evidence\td-primary-receipt.json"
- $confirmationReceipt = Join-Path $root "evidence\td-confirmation-receipt.json"
- $validationSummary = Join-Path $root "evidence\validation-summary.json"
- $selftestResult = Join-Path $root "evidence\package-selftest-result.json"
- if ([string]$info.td_receipt_sha256 -ne (Get-BoundSha256 $primaryReceipt)) {
- throw "primary TD receipt hash mismatch"
- }
- if ($confirmationTdPresent) {
- if (-not (Test-Path -LiteralPath $confirmationReceipt -PathType Leaf)) {
- throw "confirmation TD receipt is missing"
- }
- if ([string]$info.confirmation_td_receipt_sha256 -ne (Get-BoundSha256 $confirmationReceipt)) {
- throw "confirmation TD receipt hash mismatch"
- }
- } else {
- if (Test-Path -LiteralPath $confirmationReceipt -PathType Leaf) {
- throw "urgent single-TD package unexpectedly contains a confirmation receipt"
- }
- if ($null -ne $info.confirmation_td_receipt_sha256) {
- throw "urgent single-TD confirmation receipt hash must be null"
- }
- }
- if ([string]$info.validation_summary_sha256 -ne (Get-BoundSha256 $validationSummary)) {
- throw "validation summary hash mismatch"
- }
- if ([string]$info.package_selftest_result_sha256 -ne (Get-BoundSha256 $selftestResult)) {
- throw "package selftest result hash mismatch"
- }
- $primary = Get-Content -LiteralPath $primaryReceipt -Raw -Encoding UTF8 | ConvertFrom-Json
- if ([string]$primary.commit -ne $commit) { throw "primary TD receipt commit mismatch" }
- if ([string]$primary.verification_run_id -ne $verificationRunId) {
- throw "primary TD receipt verification_run_id mismatch"
- }
- if ([string]$primary.bitstream_sha256 -ne $bitstreamSha256) {
- throw "primary TD receipt bitstream mismatch"
- }
- if ([string]$info.synthesis_input_hash -ne [string]$primary.synthesis_input_hash) {
- throw "primary synthesis input hash mismatch"
- }
- if ($confirmationTdPresent) {
- $confirmation = Get-Content -LiteralPath $confirmationReceipt -Raw -Encoding UTF8 | ConvertFrom-Json
- if ([string]$confirmation.commit -ne $commit) {
- throw "confirmation TD receipt commit mismatch"
- }
- if ([string]$confirmation.verification_run_id -ne $verificationRunId) {
- throw "confirmation TD receipt verification_run_id mismatch"
- }
- if ($null -ne $confirmation.bitstream_sha256 -and
- [string]$confirmation.bitstream_sha256 -ne $bitstreamSha256) {
- throw "confirmation TD receipt bitstream mismatch"
- }
- if ([string]$info.confirmation_synthesis_input_hash -ne [string]$confirmation.synthesis_input_hash) {
- throw "confirmation synthesis input hash mismatch"
- }
- if ([string]$info.confirmation_bitstream_sha256 -ne [string]$confirmation.bitstream_sha256) {
- throw "confirmation bitstream hash binding mismatch"
- }
- if ([string]$primary.run_id -cnotmatch '^[0-9a-f]{32}$' -or
- [string]$confirmation.run_id -cnotmatch '^[0-9a-f]{32}$' -or
- [string]$primary.run_id -eq [string]$confirmation.run_id) {
- throw "Primary and confirmation TD run_id must differ"
- }
- if ([string]::IsNullOrWhiteSpace([string]$primary.work_root) -or
- [string]::IsNullOrWhiteSpace([string]$confirmation.work_root) -or
- [string]$primary.work_root -cnotmatch '^[A-Za-z]:[\\/]' -or
- [string]$confirmation.work_root -cnotmatch '^[A-Za-z]:[\\/]' -or
- [string]::Equals(
- [System.IO.Path]::GetFullPath([string]$primary.work_root).TrimEnd('\'),
- [System.IO.Path]::GetFullPath([string]$confirmation.work_root).TrimEnd('\'),
- [System.StringComparison]::OrdinalIgnoreCase
- )) {
- throw "Primary and confirmation TD work_root must differ"
- }
- } else {
- if ($null -ne $info.confirmation_synthesis_input_hash -or
- $null -ne $info.confirmation_bitstream_sha256) {
- throw "urgent single-TD confirmation bindings must be null"
- }
- }
- $validation = Get-Content -LiteralPath $validationSummary -Raw -Encoding UTF8 | ConvertFrom-Json
- if ([string]$validation.schema -ne "prod2u-p4-normal-v3-validation-summary-v1") {
- throw "validation summary schema mismatch"
- }
- if ([string]$validation.verification_run_id -ne $verificationRunId) {
- throw "validation summary verification_run_id mismatch"
- }
- if ([string]$validation.commit -ne $commit -or [string]$validation.bitstream_sha256 -ne $bitstreamSha256) {
- throw "validation summary binding mismatch"
- }
- if ([string]$validation.delivery_mode -ne $deliveryMode -or
- -not ($validation.verification_complete -is [bool]) -or
- -not ($validation.confirmation_td_present -is [bool]) -or
- $validation.verification_complete -ne $verificationComplete -or
- $validation.confirmation_td_present -ne $confirmationTdPresent) {
- throw "validation summary delivery fields mismatch"
- }
- $observedLayerNames = @($validation.layers.PSObject.Properties.Name | Sort-Object)
- $expectedSortedLayerNames = @($expectedLayerNames | Sort-Object)
- if (@(Compare-Object $expectedSortedLayerNames $observedLayerNames).Count -ne 0) {
- throw "validation layer set mismatch"
- }
- foreach ($property in @($validation.layers.PSObject.Properties)) {
- if ([string]$property.Value.status -notin @("pass", "fail", "running", "not_run")) {
- throw "validation layer status is invalid: $($property.Name)"
- }
- }
- if ($deliveryMode -eq "urgent_single_td") {
- if ([string]$validation.layers.td_confirmation.status -notin @("not_run", "running")) {
- throw "urgent td_confirmation must be not_run or running"
- }
- foreach ($name in @(
- "td_confirmation_artifacts", "td_confirmation_bitstream",
- "td_confirmation_synthesis", "td_confirmation_manifest",
- "resource_confirmation"
- )) {
- if ([string]$validation.layers.$name.status -ne "not_run") {
- throw "urgent confirmation layer must be not_run: $name"
- }
- }
- }
- $expectedVerificationComplete = $deliveryMode -eq "normal_dual_td"
- foreach ($name in $verificationEvidenceLayers) {
- if ([string]$validation.layers.$name.status -ne "pass") {
- $expectedVerificationComplete = $false
- }
- }
- if ($verificationComplete -ne $expectedVerificationComplete) {
- throw "verification_complete does not match validation evidence"
- }
- foreach ($source in @($validation.sources)) {
- $relative = [string]$source.path
- $sourcePath = Join-Path $root $relative
- if (-not (Test-Path -LiteralPath $sourcePath -PathType Leaf)) {
- throw "validation receipt is missing: $relative"
- }
- if ([string]$source.sha256 -ne (Get-BoundSha256 $sourcePath)) {
- throw "validation receipt hash mismatch: $relative"
- }
- if ([string]$source.verification_run_id -ne $verificationRunId) {
- throw "validation source verification_run_id mismatch: $relative"
- }
- $sourceReceipt = Get-Content -LiteralPath $sourcePath -Raw -Encoding UTF8 | ConvertFrom-Json
- if ([string]$sourceReceipt.verification_run_id -ne $verificationRunId) {
- throw "validation receipt verification_run_id mismatch: $relative"
- }
- }
- $expectedFiles = @{}
- foreach ($property in @($info.file_sha256.PSObject.Properties)) {
- $expectedFiles[[string]$property.Name] = [string]$property.Value
- }
- $observedFiles = @{}
- Get-ChildItem -LiteralPath $root -Recurse -File | ForEach-Object {
- $relative = $_.FullName.Substring($root.Length).TrimStart('\').Replace('\', '/')
- if ($relative -notin @("package-info.json", "evidence/package-selftest-result.json")) {
- $observedFiles[$relative] = Get-BoundSha256 $_.FullName
- }
- }
- if ($expectedFiles.Count -ne $observedFiles.Count) { throw "package file hash map is not closed" }
- foreach ($relative in $observedFiles.Keys) {
- if (-not $expectedFiles.ContainsKey($relative)) { throw "package file is not bound: $relative" }
- if ($expectedFiles[$relative] -ne $observedFiles[$relative]) {
- throw "package file hash mismatch: $relative"
- }
- }
- [ordered]@{
- schema = "prod2u-p4-normal-v3-folder-selftest-v3"
- status = "pass"
- verification_run_id = $verificationRunId
- commit = $commit
- bitstream_sha256 = $bitstreamSha256
- delivery_mode = $deliveryMode
- verification_complete = $verificationComplete
- confirmation_td_present = $confirmationTdPresent
- validation_status = Get-ValidationSummaryStatus $validation.layers
- } | ConvertTo-Json -Compress
|