selftest.ps1 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. Set-StrictMode -Version Latest
  2. $ErrorActionPreference = "Stop"
  3. $root = Split-Path -Parent $MyInvocation.MyCommand.Path
  4. $expectedLayerNames = @(
  5. "python", "icarus", "verilator", "cocotb", "formal",
  6. "td_primary", "td_confirmation", "td_primary_artifacts",
  7. "td_confirmation_artifacts", "td_confirmation_bitstream",
  8. "td_confirmation_synthesis", "td_confirmation_manifest",
  9. "resource_primary", "resource_confirmation", "package_selftest"
  10. )
  11. $verificationEvidenceLayers = @(
  12. "python", "icarus", "verilator", "cocotb", "formal",
  13. "td_primary", "td_confirmation", "td_primary_artifacts",
  14. "td_confirmation_artifacts", "td_confirmation_bitstream",
  15. "td_confirmation_synthesis", "td_confirmation_manifest",
  16. "resource_primary", "resource_confirmation"
  17. )
  18. $required = @(
  19. "fw.bit",
  20. "package-info.json",
  21. "evidence\td-primary-receipt.json",
  22. "evidence\validation-summary.json",
  23. "evidence\package-selftest-result.json",
  24. "config\host-contract.json",
  25. "config\prod2u_p4_normal_v3_hw_binding.json",
  26. "config\prod2u_p4_verification_targets_v1.json",
  27. "schemas\host-contract.schema.json",
  28. "SEND_HEX.ps1",
  29. "INITIALIZE_DISABLE.ps1",
  30. "PROGRAM_SRAM.ps1",
  31. "run.ps1",
  32. "bootstrap.ps1",
  33. "README.zh-CN.md"
  34. )
  35. foreach ($relative in $required) {
  36. if (-not (Test-Path -LiteralPath (Join-Path $root $relative) -PathType Leaf)) {
  37. throw "required file missing: $relative"
  38. }
  39. }
  40. function Get-BoundSha256([string]$Path) {
  41. return (Get-FileHash -Algorithm SHA256 -LiteralPath $Path).Hash.ToLowerInvariant()
  42. }
  43. function Assert-ValidJson([string]$Relative) {
  44. try {
  45. Get-Content -LiteralPath (Join-Path $root $Relative) -Raw -Encoding UTF8 |
  46. ConvertFrom-Json | Out-Null
  47. } catch {
  48. throw "$Relative is not valid JSON"
  49. }
  50. }
  51. function Get-ValidationSummaryStatus([object]$Layers) {
  52. $statuses = @($Layers.PSObject.Properties | ForEach-Object { [string]$_.Value.status })
  53. if ($statuses -contains "fail") { return "fail" }
  54. if ($statuses -contains "running") { return "running" }
  55. if ($statuses -contains "not_run") { return "not_run" }
  56. return "pass"
  57. }
  58. foreach ($relative in @(
  59. "config\prod2u_p4_normal_v3_hw_binding.json",
  60. "config\prod2u_p4_verification_targets_v1.json"
  61. )) {
  62. Assert-ValidJson $relative
  63. }
  64. $info = Get-Content -LiteralPath (Join-Path $root "package-info.json") -Raw -Encoding UTF8 | ConvertFrom-Json
  65. if ([string]$info.schema -ne "prod2u-p4-normal-v3-folder-package-info-v3") {
  66. throw "package-info schema mismatch"
  67. }
  68. $verificationRunId = [string]$info.verification_run_id
  69. if ($verificationRunId -notmatch '^[0-9a-f]{32}$') {
  70. throw "package verification_run_id is invalid"
  71. }
  72. if ([string]$info.profile -ne "prod2u-p4-normal-actuator-binary921-v3") {
  73. throw "package profile mismatch"
  74. }
  75. $deliveryMode = [string]$info.delivery_mode
  76. if (-not ($info.verification_complete -is [bool])) {
  77. throw "package verification_complete must be boolean"
  78. }
  79. if (-not ($info.confirmation_td_present -is [bool])) {
  80. throw "package confirmation_td_present must be boolean"
  81. }
  82. $verificationComplete = $info.verification_complete
  83. $confirmationTdPresent = $info.confirmation_td_present
  84. if ($deliveryMode -notin @("normal_dual_td", "urgent_single_td")) {
  85. throw "package delivery mode is invalid"
  86. }
  87. if ($deliveryMode -eq "normal_dual_td") {
  88. if (-not $confirmationTdPresent) {
  89. throw "normal dual-TD package must contain a confirmation receipt"
  90. }
  91. } else {
  92. if ($verificationComplete -or $confirmationTdPresent) {
  93. throw "urgent single-TD package completeness fields disagree"
  94. }
  95. }
  96. $commit = [string]$info.commit
  97. if ($commit -cnotmatch '^[0-9a-f]{8}$') { throw "package commit is invalid" }
  98. $firmware = Join-Path $root "fw.bit"
  99. $bitstreamSha256 = Get-BoundSha256 $firmware
  100. if ([string]$info.bitstream_sha256 -ne $bitstreamSha256) {
  101. throw "bitstream hash mismatch"
  102. }
  103. $expectedName = "prod2u-p4-normal-$commit-$($bitstreamSha256.Substring(0, 16))"
  104. $currentName = Split-Path -Leaf $root
  105. if ($currentName -ne $expectedName -and -not $currentName.StartsWith(".prod2u-p4-normal-stage-")) {
  106. throw "package folder name mismatch"
  107. }
  108. if ((Split-Path -Leaf (Split-Path -Parent $root)) -ne $commit) {
  109. throw "package commit parent folder mismatch"
  110. }
  111. $primaryReceipt = Join-Path $root "evidence\td-primary-receipt.json"
  112. $confirmationReceipt = Join-Path $root "evidence\td-confirmation-receipt.json"
  113. $validationSummary = Join-Path $root "evidence\validation-summary.json"
  114. $selftestResult = Join-Path $root "evidence\package-selftest-result.json"
  115. if ([string]$info.td_receipt_sha256 -ne (Get-BoundSha256 $primaryReceipt)) {
  116. throw "primary TD receipt hash mismatch"
  117. }
  118. if ($confirmationTdPresent) {
  119. if (-not (Test-Path -LiteralPath $confirmationReceipt -PathType Leaf)) {
  120. throw "confirmation TD receipt is missing"
  121. }
  122. if ([string]$info.confirmation_td_receipt_sha256 -ne (Get-BoundSha256 $confirmationReceipt)) {
  123. throw "confirmation TD receipt hash mismatch"
  124. }
  125. } else {
  126. if (Test-Path -LiteralPath $confirmationReceipt -PathType Leaf) {
  127. throw "urgent single-TD package unexpectedly contains a confirmation receipt"
  128. }
  129. if ($null -ne $info.confirmation_td_receipt_sha256) {
  130. throw "urgent single-TD confirmation receipt hash must be null"
  131. }
  132. }
  133. if ([string]$info.validation_summary_sha256 -ne (Get-BoundSha256 $validationSummary)) {
  134. throw "validation summary hash mismatch"
  135. }
  136. if ([string]$info.package_selftest_result_sha256 -ne (Get-BoundSha256 $selftestResult)) {
  137. throw "package selftest result hash mismatch"
  138. }
  139. $primary = Get-Content -LiteralPath $primaryReceipt -Raw -Encoding UTF8 | ConvertFrom-Json
  140. if ([string]$primary.commit -ne $commit) { throw "primary TD receipt commit mismatch" }
  141. if ([string]$primary.verification_run_id -ne $verificationRunId) {
  142. throw "primary TD receipt verification_run_id mismatch"
  143. }
  144. if ([string]$primary.bitstream_sha256 -ne $bitstreamSha256) {
  145. throw "primary TD receipt bitstream mismatch"
  146. }
  147. if ([string]$info.synthesis_input_hash -ne [string]$primary.synthesis_input_hash) {
  148. throw "primary synthesis input hash mismatch"
  149. }
  150. if ($confirmationTdPresent) {
  151. $confirmation = Get-Content -LiteralPath $confirmationReceipt -Raw -Encoding UTF8 | ConvertFrom-Json
  152. if ([string]$confirmation.commit -ne $commit) {
  153. throw "confirmation TD receipt commit mismatch"
  154. }
  155. if ([string]$confirmation.verification_run_id -ne $verificationRunId) {
  156. throw "confirmation TD receipt verification_run_id mismatch"
  157. }
  158. if ($null -ne $confirmation.bitstream_sha256 -and
  159. [string]$confirmation.bitstream_sha256 -ne $bitstreamSha256) {
  160. throw "confirmation TD receipt bitstream mismatch"
  161. }
  162. if ([string]$info.confirmation_synthesis_input_hash -ne [string]$confirmation.synthesis_input_hash) {
  163. throw "confirmation synthesis input hash mismatch"
  164. }
  165. if ([string]$info.confirmation_bitstream_sha256 -ne [string]$confirmation.bitstream_sha256) {
  166. throw "confirmation bitstream hash binding mismatch"
  167. }
  168. if ([string]$primary.run_id -cnotmatch '^[0-9a-f]{32}$' -or
  169. [string]$confirmation.run_id -cnotmatch '^[0-9a-f]{32}$' -or
  170. [string]$primary.run_id -eq [string]$confirmation.run_id) {
  171. throw "Primary and confirmation TD run_id must differ"
  172. }
  173. if ([string]::IsNullOrWhiteSpace([string]$primary.work_root) -or
  174. [string]::IsNullOrWhiteSpace([string]$confirmation.work_root) -or
  175. [string]$primary.work_root -cnotmatch '^[A-Za-z]:[\\/]' -or
  176. [string]$confirmation.work_root -cnotmatch '^[A-Za-z]:[\\/]' -or
  177. [string]::Equals(
  178. [System.IO.Path]::GetFullPath([string]$primary.work_root).TrimEnd('\'),
  179. [System.IO.Path]::GetFullPath([string]$confirmation.work_root).TrimEnd('\'),
  180. [System.StringComparison]::OrdinalIgnoreCase
  181. )) {
  182. throw "Primary and confirmation TD work_root must differ"
  183. }
  184. } else {
  185. if ($null -ne $info.confirmation_synthesis_input_hash -or
  186. $null -ne $info.confirmation_bitstream_sha256) {
  187. throw "urgent single-TD confirmation bindings must be null"
  188. }
  189. }
  190. $validation = Get-Content -LiteralPath $validationSummary -Raw -Encoding UTF8 | ConvertFrom-Json
  191. if ([string]$validation.schema -ne "prod2u-p4-normal-v3-validation-summary-v1") {
  192. throw "validation summary schema mismatch"
  193. }
  194. if ([string]$validation.verification_run_id -ne $verificationRunId) {
  195. throw "validation summary verification_run_id mismatch"
  196. }
  197. if ([string]$validation.commit -ne $commit -or [string]$validation.bitstream_sha256 -ne $bitstreamSha256) {
  198. throw "validation summary binding mismatch"
  199. }
  200. if ([string]$validation.delivery_mode -ne $deliveryMode -or
  201. -not ($validation.verification_complete -is [bool]) -or
  202. -not ($validation.confirmation_td_present -is [bool]) -or
  203. $validation.verification_complete -ne $verificationComplete -or
  204. $validation.confirmation_td_present -ne $confirmationTdPresent) {
  205. throw "validation summary delivery fields mismatch"
  206. }
  207. $observedLayerNames = @($validation.layers.PSObject.Properties.Name | Sort-Object)
  208. $expectedSortedLayerNames = @($expectedLayerNames | Sort-Object)
  209. if (@(Compare-Object $expectedSortedLayerNames $observedLayerNames).Count -ne 0) {
  210. throw "validation layer set mismatch"
  211. }
  212. foreach ($property in @($validation.layers.PSObject.Properties)) {
  213. if ([string]$property.Value.status -notin @("pass", "fail", "running", "not_run")) {
  214. throw "validation layer status is invalid: $($property.Name)"
  215. }
  216. }
  217. if ($deliveryMode -eq "urgent_single_td") {
  218. if ([string]$validation.layers.td_confirmation.status -notin @("not_run", "running")) {
  219. throw "urgent td_confirmation must be not_run or running"
  220. }
  221. foreach ($name in @(
  222. "td_confirmation_artifacts", "td_confirmation_bitstream",
  223. "td_confirmation_synthesis", "td_confirmation_manifest",
  224. "resource_confirmation"
  225. )) {
  226. if ([string]$validation.layers.$name.status -ne "not_run") {
  227. throw "urgent confirmation layer must be not_run: $name"
  228. }
  229. }
  230. }
  231. $expectedVerificationComplete = $deliveryMode -eq "normal_dual_td"
  232. foreach ($name in $verificationEvidenceLayers) {
  233. if ([string]$validation.layers.$name.status -ne "pass") {
  234. $expectedVerificationComplete = $false
  235. }
  236. }
  237. if ($verificationComplete -ne $expectedVerificationComplete) {
  238. throw "verification_complete does not match validation evidence"
  239. }
  240. foreach ($source in @($validation.sources)) {
  241. $relative = [string]$source.path
  242. $sourcePath = Join-Path $root $relative
  243. if (-not (Test-Path -LiteralPath $sourcePath -PathType Leaf)) {
  244. throw "validation receipt is missing: $relative"
  245. }
  246. if ([string]$source.sha256 -ne (Get-BoundSha256 $sourcePath)) {
  247. throw "validation receipt hash mismatch: $relative"
  248. }
  249. if ([string]$source.verification_run_id -ne $verificationRunId) {
  250. throw "validation source verification_run_id mismatch: $relative"
  251. }
  252. $sourceReceipt = Get-Content -LiteralPath $sourcePath -Raw -Encoding UTF8 | ConvertFrom-Json
  253. if ([string]$sourceReceipt.verification_run_id -ne $verificationRunId) {
  254. throw "validation receipt verification_run_id mismatch: $relative"
  255. }
  256. }
  257. $expectedFiles = @{}
  258. foreach ($property in @($info.file_sha256.PSObject.Properties)) {
  259. $expectedFiles[[string]$property.Name] = [string]$property.Value
  260. }
  261. $observedFiles = @{}
  262. Get-ChildItem -LiteralPath $root -Recurse -File | ForEach-Object {
  263. $relative = $_.FullName.Substring($root.Length).TrimStart('\').Replace('\', '/')
  264. if ($relative -notin @("package-info.json", "evidence/package-selftest-result.json")) {
  265. $observedFiles[$relative] = Get-BoundSha256 $_.FullName
  266. }
  267. }
  268. if ($expectedFiles.Count -ne $observedFiles.Count) { throw "package file hash map is not closed" }
  269. foreach ($relative in $observedFiles.Keys) {
  270. if (-not $expectedFiles.ContainsKey($relative)) { throw "package file is not bound: $relative" }
  271. if ($expectedFiles[$relative] -ne $observedFiles[$relative]) {
  272. throw "package file hash mismatch: $relative"
  273. }
  274. }
  275. [ordered]@{
  276. schema = "prod2u-p4-normal-v3-folder-selftest-v3"
  277. status = "pass"
  278. verification_run_id = $verificationRunId
  279. commit = $commit
  280. bitstream_sha256 = $bitstreamSha256
  281. delivery_mode = $deliveryMode
  282. verification_complete = $verificationComplete
  283. confirmation_td_present = $confirmationTdPresent
  284. validation_status = Get-ValidationSummaryStatus $validation.layers
  285. } | ConvertTo-Json -Compress