selftest.ps1 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. Set-StrictMode -Version Latest
  2. $ErrorActionPreference = "Stop"
  3. $root = Split-Path -Parent $MyInvocation.MyCommand.Path
  4. $required = @(
  5. "fw.bit",
  6. "package-info.json",
  7. "evidence\td-primary-receipt.json",
  8. "evidence\td-confirmation-receipt.json",
  9. "evidence\validation-summary.json",
  10. "evidence\package-selftest-result.json",
  11. "config\host-contract.json",
  12. "schemas\host-contract.schema.json",
  13. "SEND_HEX.ps1",
  14. "INITIALIZE_DISABLE.ps1",
  15. "PROGRAM_SRAM.ps1",
  16. "run.ps1",
  17. "bootstrap.ps1",
  18. "README.zh-CN.md"
  19. )
  20. foreach ($relative in $required) {
  21. if (-not (Test-Path -LiteralPath (Join-Path $root $relative) -PathType Leaf)) {
  22. throw "required file missing: $relative"
  23. }
  24. }
  25. function Get-BoundSha256([string]$Path) {
  26. return (Get-FileHash -Algorithm SHA256 -LiteralPath $Path).Hash.ToLowerInvariant()
  27. }
  28. $info = Get-Content -LiteralPath (Join-Path $root "package-info.json") -Raw -Encoding UTF8 | ConvertFrom-Json
  29. if ([string]$info.schema -ne "prod2u-p4-normal-v3-folder-package-info-v3") {
  30. throw "package-info schema mismatch"
  31. }
  32. if ([string]$info.profile -ne "prod2u-p4-normal-actuator-binary921-v3") {
  33. throw "package profile mismatch"
  34. }
  35. $commit = [string]$info.commit
  36. if ($commit -notmatch '^[0-9a-f]{40}$') { throw "package commit is invalid" }
  37. $firmware = Join-Path $root "fw.bit"
  38. $bitstreamSha256 = Get-BoundSha256 $firmware
  39. if ([string]$info.bitstream_sha256 -ne $bitstreamSha256) {
  40. throw "bitstream hash mismatch"
  41. }
  42. $expectedName = "prod2u-p4-normal-$commit-$($bitstreamSha256.Substring(0, 16))"
  43. $currentName = Split-Path -Leaf $root
  44. if ($currentName -ne $expectedName -and -not $currentName.StartsWith(".prod2u-p4-normal-stage-")) {
  45. throw "package folder name mismatch"
  46. }
  47. if ((Split-Path -Leaf (Split-Path -Parent $root)) -ne $commit) {
  48. throw "package commit parent folder mismatch"
  49. }
  50. $primaryReceipt = Join-Path $root "evidence\td-primary-receipt.json"
  51. $confirmationReceipt = Join-Path $root "evidence\td-confirmation-receipt.json"
  52. $validationSummary = Join-Path $root "evidence\validation-summary.json"
  53. $selftestResult = Join-Path $root "evidence\package-selftest-result.json"
  54. if ([string]$info.td_receipt_sha256 -ne (Get-BoundSha256 $primaryReceipt)) {
  55. throw "primary TD receipt hash mismatch"
  56. }
  57. if ([string]$info.confirmation_td_receipt_sha256 -ne (Get-BoundSha256 $confirmationReceipt)) {
  58. throw "confirmation TD receipt hash mismatch"
  59. }
  60. if ([string]$info.validation_summary_sha256 -ne (Get-BoundSha256 $validationSummary)) {
  61. throw "validation summary hash mismatch"
  62. }
  63. if ([string]$info.package_selftest_result_sha256 -ne (Get-BoundSha256 $selftestResult)) {
  64. throw "package selftest result hash mismatch"
  65. }
  66. $primary = Get-Content -LiteralPath $primaryReceipt -Raw -Encoding UTF8 | ConvertFrom-Json
  67. $confirmation = Get-Content -LiteralPath $confirmationReceipt -Raw -Encoding UTF8 | ConvertFrom-Json
  68. if ([string]$primary.commit -ne $commit) { throw "primary TD receipt commit mismatch" }
  69. if ([string]$primary.bitstream_sha256 -ne $bitstreamSha256) {
  70. throw "primary TD receipt bitstream mismatch"
  71. }
  72. if ([string]$confirmation.commit -ne $commit) {
  73. throw "confirmation TD receipt commit mismatch"
  74. }
  75. if ([string]$info.synthesis_input_hash -ne [string]$primary.synthesis_input_hash) {
  76. throw "primary synthesis input hash mismatch"
  77. }
  78. if ([string]$info.confirmation_synthesis_input_hash -ne [string]$confirmation.synthesis_input_hash) {
  79. throw "confirmation synthesis input hash mismatch"
  80. }
  81. if ([string]$info.confirmation_bitstream_sha256 -ne [string]$confirmation.bitstream_sha256) {
  82. throw "confirmation bitstream hash binding mismatch"
  83. }
  84. $validation = Get-Content -LiteralPath $validationSummary -Raw -Encoding UTF8 | ConvertFrom-Json
  85. if ([string]$validation.schema -ne "prod2u-p4-normal-v3-validation-summary-v1") {
  86. throw "validation summary schema mismatch"
  87. }
  88. if ([string]$validation.commit -ne $commit -or [string]$validation.bitstream_sha256 -ne $bitstreamSha256) {
  89. throw "validation summary binding mismatch"
  90. }
  91. foreach ($property in @($validation.layers.PSObject.Properties)) {
  92. if ([string]$property.Value.status -notin @("pass", "fail", "not_run")) {
  93. throw "validation layer status is invalid: $($property.Name)"
  94. }
  95. }
  96. $expectedFiles = @{}
  97. foreach ($property in @($info.file_sha256.PSObject.Properties)) {
  98. $expectedFiles[[string]$property.Name] = [string]$property.Value
  99. }
  100. $observedFiles = @{}
  101. Get-ChildItem -LiteralPath $root -Recurse -File | ForEach-Object {
  102. $relative = $_.FullName.Substring($root.Length).TrimStart('\').Replace('\', '/')
  103. if ($relative -notin @("package-info.json", "evidence/package-selftest-result.json")) {
  104. $observedFiles[$relative] = Get-BoundSha256 $_.FullName
  105. }
  106. }
  107. if ($expectedFiles.Count -ne $observedFiles.Count) { throw "package file hash map is not closed" }
  108. foreach ($relative in $observedFiles.Keys) {
  109. if (-not $expectedFiles.ContainsKey($relative)) { throw "package file is not bound: $relative" }
  110. if ($expectedFiles[$relative] -ne $observedFiles[$relative]) {
  111. throw "package file hash mismatch: $relative"
  112. }
  113. }
  114. [ordered]@{
  115. schema = "prod2u-p4-normal-v3-folder-selftest-v3"
  116. status = "pass"
  117. commit = $commit
  118. bitstream_sha256 = $bitstreamSha256
  119. validation_status = if (@($validation.layers.PSObject.Properties | Where-Object { $_.Value.status -eq "fail" }).Count -eq 0) { "pass" } else { "fail" }
  120. } | ConvertTo-Json -Compress