package_prod2u_p4_normal_v3.ps1 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. param(
  2. [Parameter(Mandatory = $true)][string]$OutputDir,
  3. [Parameter(Mandatory = $true)][string]$NormalBitstream,
  4. [Parameter(Mandatory = $true)][string]$TdReceipt,
  5. [Parameter(Mandatory = $true)][string]$ConfirmationTdReceipt,
  6. [string[]]$ValidationReceipt = @(),
  7. [string]$HostContract = "",
  8. [string]$Commit = "",
  9. [string]$WheelhouseRoot = ""
  10. )
  11. Set-StrictMode -Version Latest
  12. $ErrorActionPreference = "Stop"
  13. $repo = Split-Path -Parent (Split-Path -Parent $MyInvocation.MyCommand.Path)
  14. $packageScriptRelative = "tools/package_prod2u_p4_normal_v3.ps1"
  15. $readmeSourceRelative = "docs/prod2u-p4-normal-v3-runbook.zh-CN.md"
  16. $hostContractSchemaRelative = "schemas/prod2u_p4_normal_host_contract_v4.schema.json"
  17. $templateRelatives = @(
  18. "tools/package_templates/bootstrap.ps1",
  19. "tools/package_templates/selftest.ps1",
  20. "tools/package_templates/run.ps1",
  21. "tools/package_templates/SEND_HEX.ps1",
  22. "tools/package_templates/INITIALIZE_DISABLE.ps1",
  23. "tools/package_templates/PROGRAM_SRAM.ps1"
  24. )
  25. if (-not $HostContract) {
  26. $HostContract = Join-Path $repo "config\prod2u_p4_normal_host_contract_v4.json"
  27. }
  28. function Assert-File([string]$Path, [string]$Label) {
  29. if (-not (Test-Path -LiteralPath $Path -PathType Leaf)) {
  30. throw "$Label is missing: $Path"
  31. }
  32. }
  33. function Resolve-Git {
  34. $git = Get-Command git.exe -ErrorAction SilentlyContinue
  35. if (-not $git) { $git = Get-Command git -ErrorAction SilentlyContinue }
  36. if (-not $git) { throw "Git is required to bind the package commit" }
  37. return $git.Source
  38. }
  39. function Resolve-Commit([string]$Requested) {
  40. $git = Resolve-Git
  41. $head = (& $git -C $repo rev-parse HEAD).Trim().ToLowerInvariant()
  42. if ($LASTEXITCODE -ne 0 -or $head -notmatch '^[0-9a-f]{40}$') {
  43. throw "Unable to resolve package commit"
  44. }
  45. if ($Requested) {
  46. $requestedCommit = $Requested.Trim().ToLowerInvariant()
  47. if ($requestedCommit -notmatch '^[0-9a-f]{40}$') {
  48. throw "Commit must be a 40-digit lowercase Git object id"
  49. }
  50. if ($requestedCommit -ne $head) {
  51. throw "Explicit Commit does not match repository HEAD"
  52. }
  53. }
  54. return $head
  55. }
  56. function Resolve-RepoRelativePath([string]$Path, [string]$Label) {
  57. $repoFull = [System.IO.Path]::GetFullPath($repo).TrimEnd('\') + '\'
  58. $pathFull = [System.IO.Path]::GetFullPath($Path)
  59. if (-not $pathFull.StartsWith($repoFull, [System.StringComparison]::OrdinalIgnoreCase)) {
  60. throw "$Label must be inside the repository and tracked by HEAD"
  61. }
  62. return $pathFull.Substring($repoFull.Length).Replace('\', '/')
  63. }
  64. function Assert-CleanCommittedSources(
  65. [string]$Git,
  66. [string]$ResolvedCommit,
  67. [string[]]$Relatives
  68. ) {
  69. & $Git -C $repo diff --quiet --exit-code $ResolvedCommit -- . ":(exclude).planning/**"
  70. $diffExit = $LASTEXITCODE
  71. if ($diffExit -eq 1) { throw "repository has tracked source changes outside .planning/" }
  72. if ($diffExit -ne 0) { throw "unable to verify repository tracked source state" }
  73. foreach ($relative in $Relatives) {
  74. $saved = $ErrorActionPreference
  75. $ErrorActionPreference = "SilentlyContinue"
  76. & $Git -C $repo cat-file -e "$ResolvedCommit`:$relative" 2>$null
  77. $objectExit = $LASTEXITCODE
  78. $ErrorActionPreference = $saved
  79. if ($objectExit -ne 0) {
  80. throw "package source file is not tracked by HEAD: $relative"
  81. }
  82. }
  83. }
  84. function New-CommitSourceSnapshot(
  85. [string]$Git,
  86. [string]$ResolvedCommit,
  87. [string[]]$Relatives,
  88. [string]$Destination
  89. ) {
  90. $archive = "$Destination.zip"
  91. $arguments = @(
  92. "-C", $repo,
  93. "archive", "--format=zip", "--output=$archive",
  94. $ResolvedCommit, "--"
  95. ) + @($Relatives)
  96. & $Git @arguments
  97. if ($LASTEXITCODE -ne 0) { throw "unable to materialize package sources from HEAD" }
  98. New-Item -ItemType Directory -Force -Path $Destination | Out-Null
  99. Expand-Archive -LiteralPath $archive -DestinationPath $Destination -Force
  100. Remove-Item -LiteralPath $archive -Force
  101. }
  102. function Get-Sha256([string]$Path) {
  103. return (Get-FileHash -Algorithm SHA256 -LiteralPath $Path).Hash.ToLowerInvariant()
  104. }
  105. function Write-Utf8NoBom([string]$Path, [string]$Text) {
  106. $parent = Split-Path -Parent $Path
  107. if ($parent) { New-Item -ItemType Directory -Force -Path $parent | Out-Null }
  108. [System.IO.File]::WriteAllText($Path, $Text, [System.Text.UTF8Encoding]::new($false))
  109. }
  110. function Write-Json([string]$Path, [object]$Value) {
  111. $text = $Value | ConvertTo-Json -Depth 20
  112. Write-Utf8NoBom $Path ($text + "`n")
  113. }
  114. function Read-Json([string]$Path, [string]$Label) {
  115. Assert-File $Path $Label
  116. try {
  117. return Get-Content -LiteralPath $Path -Raw -Encoding UTF8 | ConvertFrom-Json
  118. } catch {
  119. throw "$Label is not valid JSON: $Path"
  120. }
  121. }
  122. function Get-PropertyValue([object]$Value, [string]$Name) {
  123. if ($null -eq $Value) { return $null }
  124. $property = $Value.PSObject.Properties[$Name]
  125. if ($null -eq $property) { return $null }
  126. return $property.Value
  127. }
  128. function Normalize-ValidationStatus([object]$Value) {
  129. $status = [string]$Value
  130. if ($status -eq "pass") { return "pass" }
  131. if ($status -eq "not_run" -or $status -eq "skipped" -or $status.StartsWith("blocked")) {
  132. return "not_run"
  133. }
  134. return "fail"
  135. }
  136. function New-ValidationLayer([string]$Status, [string]$Source, [object]$OriginalStatus) {
  137. return [ordered]@{
  138. status = $Status
  139. source = $Source
  140. original_status = if ($null -eq $OriginalStatus) { $null } else { [string]$OriginalStatus }
  141. }
  142. }
  143. function Set-ValidationLayer(
  144. [System.Collections.IDictionary]$Layers,
  145. [string]$Name,
  146. [object]$Status,
  147. [string]$Source
  148. ) {
  149. $Layers[$Name] = New-ValidationLayer (Normalize-ValidationStatus $Status) $Source $Status
  150. }
  151. function Merge-ValidationReceipt(
  152. [System.Collections.IDictionary]$Layers,
  153. [object]$Receipt,
  154. [string]$Source
  155. ) {
  156. $schema = [string](Get-PropertyValue $Receipt "schema")
  157. if ($schema -eq "prod2u-p4-normal-v3-strict-offline-gate-v1") {
  158. $children = Get-PropertyValue $Receipt "layers"
  159. if ($null -ne $children) {
  160. $unit = Get-PropertyValue $children "unit"
  161. if ($null -ne $unit) {
  162. Set-ValidationLayer $Layers "python" (Get-PropertyValue $unit "status") $Source
  163. }
  164. $formal = Get-PropertyValue $children "formal"
  165. if ($null -ne $formal) {
  166. Set-ValidationLayer $Layers "formal" (Get-PropertyValue $formal "status") $Source
  167. }
  168. $hdl = Get-PropertyValue $children "hdl_sim"
  169. if ($null -ne $hdl) {
  170. $backendResults = Get-PropertyValue $hdl "backend_results"
  171. foreach ($backend in @("icarus", "verilator", "cocotb")) {
  172. $child = Get-PropertyValue $backendResults $backend
  173. if ($null -ne $child) {
  174. Set-ValidationLayer $Layers $backend (Get-PropertyValue $child "status") $Source
  175. }
  176. }
  177. }
  178. }
  179. return
  180. }
  181. $layer = [string](Get-PropertyValue $Receipt "verification_layer")
  182. $status = Get-PropertyValue $Receipt "status"
  183. if ($layer -eq "formal") {
  184. Set-ValidationLayer $Layers "formal" $status $Source
  185. } elseif ($layer -eq "unit") {
  186. Set-ValidationLayer $Layers "python" $status $Source
  187. } elseif ($layer -eq "hdl_sim") {
  188. $backend = [string](Get-PropertyValue $Receipt "backend")
  189. if ($backend -in @("icarus", "verilator", "cocotb")) {
  190. Set-ValidationLayer $Layers $backend $status $Source
  191. } elseif ($backend -eq "all") {
  192. $backendResults = Get-PropertyValue $Receipt "backend_results"
  193. foreach ($name in @("icarus", "verilator", "cocotb")) {
  194. $child = Get-PropertyValue $backendResults $name
  195. if ($null -ne $child) {
  196. Set-ValidationLayer $Layers $name (Get-PropertyValue $child "status") $Source
  197. }
  198. }
  199. }
  200. } elseif ($schema -eq "prod2u-p4-normal-v3-package-selftest-v1") {
  201. Set-ValidationLayer $Layers "package_selftest" $status $Source
  202. }
  203. }
  204. function Get-ResourceStatus([object]$Receipt) {
  205. $structural = Get-PropertyValue $Receipt "structural_gate"
  206. $resources = Get-PropertyValue $structural "resources"
  207. $status = Get-PropertyValue $resources "status"
  208. if ($null -ne $status) { return $status }
  209. $resourceGate = Get-PropertyValue $Receipt "resource_gate"
  210. $status = Get-PropertyValue $resourceGate "status"
  211. if ($null -ne $status) { return $status }
  212. return "not_run"
  213. }
  214. function Get-ComparisonStatus([object]$Observed, [object]$Expected) {
  215. if ($null -eq $Observed -or [string]$Observed -eq "") { return "not_run" }
  216. if ([string]$Observed -eq [string]$Expected) { return "pass" }
  217. return "fail"
  218. }
  219. function Get-TdBitstreamBindingStatus([object]$Receipt) {
  220. $digest = [string](Get-PropertyValue $Receipt "bitstream_sha256")
  221. if (-not $digest) { return "not_run" }
  222. if ($digest -notmatch '^[0-9a-f]{64}$') { return "fail" }
  223. $candidates = @()
  224. $direct = Get-PropertyValue $Receipt "bitstream"
  225. if ($null -ne $direct) { $candidates += $direct }
  226. $measurements = Get-PropertyValue $Receipt "measurement_artifacts"
  227. $measured = Get-PropertyValue $measurements "bitstream"
  228. if ($null -ne $measured) { $candidates += $measured }
  229. foreach ($candidate in $candidates) {
  230. $path = [string](Get-PropertyValue $candidate "path")
  231. $expected = [string](Get-PropertyValue $candidate "sha256")
  232. if ($expected -ne $digest) { continue }
  233. if (Test-Path -LiteralPath $path -PathType Leaf) {
  234. if ((Get-Sha256 $path) -eq $digest) { return "pass" }
  235. }
  236. }
  237. return "fail"
  238. }
  239. function Copy-TdArtifacts(
  240. [object]$Receipt,
  241. [string]$Destination,
  242. [string]$PackageRelativeRoot
  243. ) {
  244. New-Item -ItemType Directory -Force -Path $Destination | Out-Null
  245. $records = [ordered]@{}
  246. $artifacts = Get-PropertyValue $Receipt "measurement_artifacts"
  247. if ($null -eq $artifacts) { return $records }
  248. foreach ($property in @($artifacts.PSObject.Properties)) {
  249. $name = [string]$property.Name
  250. if ($name -eq "bitstream") { continue }
  251. $metadata = $property.Value
  252. $source = [string](Get-PropertyValue $metadata "path")
  253. $expectedHash = [string](Get-PropertyValue $metadata "sha256")
  254. $record = [ordered]@{
  255. status = "fail"
  256. source_path = $source
  257. expected_sha256 = $expectedHash
  258. package_path = $null
  259. observed_sha256 = $null
  260. }
  261. if (Test-Path -LiteralPath $source -PathType Leaf) {
  262. $observed = Get-Sha256 $source
  263. $record.observed_sha256 = $observed
  264. if ($expectedHash -match '^[0-9a-f]{64}$' -and $observed -eq $expectedHash) {
  265. $safeName = ($name -replace '[^A-Za-z0-9_.-]', '_')
  266. $extension = [System.IO.Path]::GetExtension($source)
  267. if (-not $extension) { $extension = ".bin" }
  268. $fileName = "$safeName$extension"
  269. $target = Join-Path $Destination $fileName
  270. Copy-Item -LiteralPath $source -Destination $target
  271. $record.status = "pass"
  272. $record.package_path = "$PackageRelativeRoot/$fileName"
  273. }
  274. }
  275. $records[$name] = $record
  276. }
  277. return $records
  278. }
  279. function Get-ArtifactValidationStatus([System.Collections.IDictionary]$Records) {
  280. if ($Records.Count -eq 0) { return "not_run" }
  281. foreach ($record in $Records.Values) {
  282. if ([string]$record.status -ne "pass") { return "fail" }
  283. }
  284. return "pass"
  285. }
  286. function Copy-TdPinGate(
  287. [object]$Receipt,
  288. [string]$Destination,
  289. [string]$PackageRelativePath
  290. ) {
  291. $pinGate = Get-PropertyValue $Receipt "post_route_pin_gate"
  292. if ($null -eq $pinGate) {
  293. return [ordered]@{ status = "not_run"; package_path = $null; sha256 = $null }
  294. }
  295. Write-Json $Destination $pinGate
  296. return [ordered]@{
  297. status = "pass"
  298. package_path = $PackageRelativePath
  299. sha256 = Get-Sha256 $Destination
  300. }
  301. }
  302. function Get-PackageFileHashes([string]$Root) {
  303. $result = [ordered]@{}
  304. Get-ChildItem -LiteralPath $Root -Recurse -File | Sort-Object FullName | ForEach-Object {
  305. $relative = $_.FullName.Substring($Root.Length).TrimStart('\').Replace('\', '/')
  306. if ($relative -notin @("package-info.json", "evidence/package-selftest-result.json")) {
  307. $result[$relative] = Get-Sha256 $_.FullName
  308. }
  309. }
  310. return $result
  311. }
  312. function Invoke-FolderSelftest([string]$Root) {
  313. $savedErrorActionPreference = $ErrorActionPreference
  314. $ErrorActionPreference = "Continue"
  315. try {
  316. $output = & powershell.exe -NoProfile -ExecutionPolicy Bypass -File (Join-Path $Root "selftest.ps1") 2>&1 | Out-String
  317. $returnCode = $LASTEXITCODE
  318. } catch {
  319. $output = $_ | Out-String
  320. $returnCode = if ($LASTEXITCODE) { $LASTEXITCODE } else { 1 }
  321. } finally {
  322. $ErrorActionPreference = $savedErrorActionPreference
  323. }
  324. return [ordered]@{
  325. schema = "prod2u-p4-normal-v3-package-selftest-result-v1"
  326. status = if ($returnCode -eq 0) { "pass" } else { "fail" }
  327. returncode = $returnCode
  328. output = $output.Trim()
  329. }
  330. }
  331. Assert-File $NormalBitstream "Normal bitstream"
  332. Assert-File $TdReceipt "Primary TD build receipt"
  333. Assert-File $ConfirmationTdReceipt "Confirmation TD build receipt"
  334. Assert-File $HostContract "Normal v3 host contract"
  335. foreach ($receipt in $ValidationReceipt) {
  336. Assert-File $receipt "Validation receipt"
  337. }
  338. $hostContractRelative = Resolve-RepoRelativePath $HostContract "Normal v3 host contract"
  339. $sourceRelatives = @(
  340. $packageScriptRelative,
  341. $readmeSourceRelative,
  342. $hostContractSchemaRelative,
  343. $hostContractRelative
  344. ) + $templateRelatives | Sort-Object -Unique
  345. foreach ($relative in $sourceRelatives) {
  346. Assert-File (Join-Path $repo $relative) "Package source $relative"
  347. }
  348. $resolvedCommit = Resolve-Commit $Commit
  349. $git = Resolve-Git
  350. Assert-CleanCommittedSources $git $resolvedCommit $sourceRelatives
  351. $bitstreamSha256 = Get-Sha256 $NormalBitstream
  352. $tdPrimary = Read-Json $TdReceipt "Primary TD build receipt"
  353. $tdConfirmation = Read-Json $ConfirmationTdReceipt "Confirmation TD build receipt"
  354. if ([string](Get-PropertyValue $tdPrimary "commit") -ne $resolvedCommit) {
  355. throw "Primary TD receipt commit does not match package commit"
  356. }
  357. if ([string](Get-PropertyValue $tdPrimary "bitstream_sha256") -ne $bitstreamSha256) {
  358. throw "Primary TD receipt bitstream hash does not match package bitstream"
  359. }
  360. $synthesisInputHash = [string](Get-PropertyValue $tdPrimary "synthesis_input_hash")
  361. $confirmationCommit = [string](Get-PropertyValue $tdConfirmation "commit")
  362. if ($confirmationCommit -ne $resolvedCommit) {
  363. throw "Confirmation TD receipt commit does not match package commit"
  364. }
  365. $confirmationBitstreamSha256 = Get-PropertyValue $tdConfirmation "bitstream_sha256"
  366. $confirmationSynthesisInputHash = Get-PropertyValue $tdConfirmation "synthesis_input_hash"
  367. $primaryManifest = Get-PropertyValue $tdPrimary "synthesis_input_manifest"
  368. $confirmationManifest = Get-PropertyValue $tdConfirmation "synthesis_input_manifest"
  369. $primaryManifestSha256 = Get-PropertyValue $primaryManifest "sha256"
  370. $confirmationManifestSha256 = Get-PropertyValue $confirmationManifest "sha256"
  371. $outFull = [System.IO.Path]::GetFullPath($OutputDir)
  372. $commitRoot = Join-Path $outFull $resolvedCommit
  373. New-Item -ItemType Directory -Force -Path $commitRoot | Out-Null
  374. $packageName = "prod2u-p4-normal-$resolvedCommit-$($bitstreamSha256.Substring(0, 16))"
  375. $final = Join-Path $commitRoot $packageName
  376. if (Test-Path -LiteralPath $final) { throw "Package folder already exists: $final" }
  377. $stage = Join-Path $commitRoot (".prod2u-p4-normal-stage-" + [Guid]::NewGuid().ToString("N"))
  378. New-Item -ItemType Directory -Path $stage | Out-Null
  379. try {
  380. $sourceSnapshot = Join-Path $stage ".source-snapshot"
  381. New-CommitSourceSnapshot $git $resolvedCommit $sourceRelatives $sourceSnapshot
  382. $configDir = Join-Path $stage "config"
  383. $evidenceDir = Join-Path $stage "evidence"
  384. $validationDir = Join-Path $evidenceDir "validation"
  385. $schemaDir = Join-Path $stage "schemas"
  386. foreach ($directory in @($configDir, $evidenceDir, $validationDir, $schemaDir)) {
  387. New-Item -ItemType Directory -Force -Path $directory | Out-Null
  388. }
  389. $packagedBitstream = Join-Path $stage "fw.bit"
  390. $packagedPrimaryReceipt = Join-Path $evidenceDir "td-primary-receipt.json"
  391. $packagedConfirmationReceipt = Join-Path $evidenceDir "td-confirmation-receipt.json"
  392. Copy-Item -LiteralPath $NormalBitstream -Destination $packagedBitstream
  393. Copy-Item -LiteralPath $TdReceipt -Destination $packagedPrimaryReceipt
  394. Copy-Item -LiteralPath $ConfirmationTdReceipt -Destination $packagedConfirmationReceipt
  395. Copy-Item -LiteralPath (Join-Path $sourceSnapshot $hostContractRelative) -Destination (Join-Path $configDir "host-contract.json")
  396. Copy-Item -LiteralPath (Join-Path $sourceSnapshot $hostContractSchemaRelative) -Destination (Join-Path $schemaDir "host-contract.schema.json")
  397. $templateRoot = Join-Path $sourceSnapshot "tools\package_templates"
  398. foreach ($template in @(
  399. "bootstrap.ps1",
  400. "selftest.ps1",
  401. "run.ps1",
  402. "SEND_HEX.ps1",
  403. "INITIALIZE_DISABLE.ps1",
  404. "PROGRAM_SRAM.ps1"
  405. )) {
  406. Copy-Item -LiteralPath (Join-Path $templateRoot $template) -Destination (Join-Path $stage $template)
  407. }
  408. $readme = [System.IO.File]::ReadAllText(
  409. (Join-Path $sourceSnapshot $readmeSourceRelative),
  410. [System.Text.Encoding]::UTF8
  411. )
  412. Write-Utf8NoBom (Join-Path $stage "README.zh-CN.md") ($readme + "`r`n")
  413. Remove-Item -LiteralPath $sourceSnapshot -Recurse -Force
  414. $layers = [ordered]@{}
  415. foreach ($name in @(
  416. "python", "icarus", "verilator", "cocotb", "formal",
  417. "td_primary", "td_confirmation", "td_primary_artifacts",
  418. "td_confirmation_artifacts", "td_confirmation_bitstream",
  419. "td_confirmation_synthesis", "td_confirmation_manifest",
  420. "resource_primary", "resource_confirmation", "package_selftest"
  421. )) {
  422. $layers[$name] = New-ValidationLayer "not_run" "not_provided" "not_run"
  423. }
  424. Set-ValidationLayer $layers "td_primary" (Get-PropertyValue $tdPrimary "status") "evidence/td-primary-receipt.json"
  425. Set-ValidationLayer $layers "td_confirmation" (Get-PropertyValue $tdConfirmation "status") "evidence/td-confirmation-receipt.json"
  426. Set-ValidationLayer $layers "resource_primary" (Get-ResourceStatus $tdPrimary) "evidence/td-primary-receipt.json"
  427. Set-ValidationLayer $layers "resource_confirmation" (Get-ResourceStatus $tdConfirmation) "evidence/td-confirmation-receipt.json"
  428. Set-ValidationLayer $layers "td_confirmation_bitstream" (Get-TdBitstreamBindingStatus $tdConfirmation) "evidence/td-confirmation-receipt.json"
  429. Set-ValidationLayer $layers "td_confirmation_synthesis" (Get-ComparisonStatus $confirmationSynthesisInputHash $synthesisInputHash) "evidence/td-confirmation-receipt.json"
  430. Set-ValidationLayer $layers "td_confirmation_manifest" (Get-ComparisonStatus $confirmationManifestSha256 $primaryManifestSha256) "evidence/td-confirmation-receipt.json"
  431. $primaryArtifacts = Copy-TdArtifacts $tdPrimary (Join-Path $evidenceDir "td-primary") "evidence/td-primary"
  432. $confirmationArtifacts = Copy-TdArtifacts $tdConfirmation (Join-Path $evidenceDir "td-confirmation") "evidence/td-confirmation"
  433. $primaryPinGate = Copy-TdPinGate $tdPrimary (Join-Path $evidenceDir "td-primary\pin-gate.json") "evidence/td-primary/pin-gate.json"
  434. $confirmationPinGate = Copy-TdPinGate $tdConfirmation (Join-Path $evidenceDir "td-confirmation\pin-gate.json") "evidence/td-confirmation/pin-gate.json"
  435. $primaryArtifacts["pin_gate"] = $primaryPinGate
  436. $confirmationArtifacts["pin_gate"] = $confirmationPinGate
  437. $layers["td_primary"]["artifacts"] = $primaryArtifacts
  438. $layers["td_confirmation"]["artifacts"] = $confirmationArtifacts
  439. Set-ValidationLayer $layers "td_primary_artifacts" (Get-ArtifactValidationStatus $primaryArtifacts) "evidence/td-primary"
  440. Set-ValidationLayer $layers "td_confirmation_artifacts" (Get-ArtifactValidationStatus $confirmationArtifacts) "evidence/td-confirmation"
  441. $validationSources = @()
  442. $validationIndex = 0
  443. foreach ($receiptPath in $ValidationReceipt) {
  444. $validationIndex += 1
  445. $receipt = Read-Json $receiptPath "Validation receipt"
  446. $base = [System.IO.Path]::GetFileNameWithoutExtension($receiptPath) -replace '[^A-Za-z0-9_.-]', '_'
  447. $fileName = ("{0:D2}-{1}.json" -f $validationIndex, $base)
  448. $target = Join-Path $validationDir $fileName
  449. Copy-Item -LiteralPath $receiptPath -Destination $target
  450. $relative = "evidence/validation/$fileName"
  451. $validationSources += [ordered]@{
  452. path = $relative
  453. sha256 = Get-Sha256 $target
  454. schema = Get-PropertyValue $receipt "schema"
  455. status = Get-PropertyValue $receipt "status"
  456. }
  457. Merge-ValidationReceipt $layers $receipt $relative
  458. }
  459. $validationSummaryPath = Join-Path $evidenceDir "validation-summary.json"
  460. $validationSummary = [ordered]@{
  461. schema = "prod2u-p4-normal-v3-validation-summary-v1"
  462. commit = $resolvedCommit
  463. bitstream_sha256 = $bitstreamSha256
  464. layers = $layers
  465. sources = @($validationSources)
  466. policy = [ordered]@{
  467. image_created_when_bitstream_nonempty = $true
  468. validation_failures_block_package = $false
  469. allowed_statuses = @("pass", "fail", "not_run")
  470. }
  471. }
  472. Write-Json $validationSummaryPath $validationSummary
  473. $selftestResultPath = Join-Path $evidenceDir "package-selftest-result.json"
  474. $provisionalSelftest = [ordered]@{
  475. schema = "prod2u-p4-normal-v3-package-selftest-result-v1"
  476. status = "not_run"
  477. returncode = $null
  478. output = ""
  479. }
  480. Write-Json $selftestResultPath $provisionalSelftest
  481. $packageInfoPath = Join-Path $stage "package-info.json"
  482. $packageInfo = [ordered]@{
  483. schema = "prod2u-p4-normal-v3-folder-package-info-v3"
  484. profile = "prod2u-p4-normal-actuator-binary921-v3"
  485. commit = $resolvedCommit
  486. bitstream_sha256 = $bitstreamSha256
  487. confirmation_bitstream_sha256 = $confirmationBitstreamSha256
  488. td_receipt_sha256 = Get-Sha256 $packagedPrimaryReceipt
  489. confirmation_td_receipt_sha256 = Get-Sha256 $packagedConfirmationReceipt
  490. validation_summary_sha256 = Get-Sha256 $validationSummaryPath
  491. package_selftest_result_sha256 = Get-Sha256 $selftestResultPath
  492. synthesis_input_hash = $synthesisInputHash
  493. confirmation_synthesis_input_hash = $confirmationSynthesisInputHash
  494. file_sha256 = Get-PackageFileHashes $stage
  495. }
  496. Write-Json $packageInfoPath $packageInfo
  497. $selftestResult = Invoke-FolderSelftest $stage
  498. Set-ValidationLayer $layers "package_selftest" $selftestResult.status "evidence/package-selftest-result.json"
  499. Write-Json $validationSummaryPath $validationSummary
  500. Write-Json $selftestResultPath $selftestResult
  501. $packageInfo.validation_summary_sha256 = Get-Sha256 $validationSummaryPath
  502. $packageInfo.package_selftest_result_sha256 = Get-Sha256 $selftestResultPath
  503. $packageInfo.file_sha256 = Get-PackageFileHashes $stage
  504. Write-Json $packageInfoPath $packageInfo
  505. $finalSelftest = Invoke-FolderSelftest $stage
  506. if ($finalSelftest.status -ne "pass") {
  507. Set-ValidationLayer $layers "package_selftest" "fail" "evidence/package-selftest-result.json"
  508. Write-Json $validationSummaryPath $validationSummary
  509. Write-Json $selftestResultPath $finalSelftest
  510. $packageInfo.validation_summary_sha256 = Get-Sha256 $validationSummaryPath
  511. $packageInfo.package_selftest_result_sha256 = Get-Sha256 $selftestResultPath
  512. $packageInfo.file_sha256 = Get-PackageFileHashes $stage
  513. Write-Json $packageInfoPath $packageInfo
  514. $selftestResult = $finalSelftest
  515. }
  516. Move-Item -LiteralPath $stage -Destination $final
  517. $stage = $null
  518. [ordered]@{
  519. schema = "prod2u-p4-normal-v3-folder-package-v3"
  520. status = "pass"
  521. package = $final
  522. commit = $resolvedCommit
  523. bitstream_sha256 = $bitstreamSha256
  524. validation_status = if (@($layers.Values | Where-Object { $_.status -eq "fail" }).Count -eq 0) { "pass" } else { "fail" }
  525. package_selftest_status = $selftestResult.status
  526. } | ConvertTo-Json -Compress
  527. } finally {
  528. if ($stage -and (Test-Path -LiteralPath $stage)) {
  529. Remove-Item -LiteralPath $stage -Recurse -Force
  530. }
  531. }