param( [string]$Output = "dist\factory_firmware" ) $ErrorActionPreference = "Stop" $toolRoot = Split-Path -Parent $MyInvocation.MyCommand.Path $projectRoot = Split-Path -Parent $toolRoot $audioImage = Join-Path $toolRoot "dist\factory_audio\customer_fs.bin" $configuredImage = Join-Path $projectRoot "ql-config\config\EG800AKCN_11LG\customer_fs.bin" $flashPackage = Join-Path $projectRoot "target\EG800AKCN_11LG\EG800AKCN_11LG.zip" $outDir = Join-Path $toolRoot $Output $backup = Join-Path $outDir "customer_fs.business-backup.bin" $export = Join-Path $outDir "EG800AKCN_11LG-FT1-factory-test.zip" if (-not (Test-Path -LiteralPath $audioImage)) { throw "Factory audio image is missing. Run build_factory_audio_package.py first." } if ((Get-Item -LiteralPath $audioImage).Length -ne 262144) { throw "Factory customer_fs.bin is not 262144 bytes." } New-Item -ItemType Directory -Force -Path $outDir | Out-Null Copy-Item -LiteralPath $configuredImage -Destination $backup -Force $originalHash = (Get-FileHash -LiteralPath $configuredImage -Algorithm SHA256).Hash $audioHash = (Get-FileHash -LiteralPath $audioImage -Algorithm SHA256).Hash try { Copy-Item -LiteralPath $audioImage -Destination $configuredImage -Force if ((Get-FileHash -LiteralPath $configuredImage -Algorithm SHA256).Hash -ne $audioHash) { throw "Temporary customer_fs.bin hash verification failed." } & cmd.exe /d /c "build.bat app" if ($LASTEXITCODE -ne 0) { throw "build.bat app failed with exit code $LASTEXITCODE" } & cmd.exe /d /c "build.bat firmware" if ($LASTEXITCODE -ne 0) { throw "build.bat firmware failed with exit code $LASTEXITCODE" } if (-not (Test-Path -LiteralPath $flashPackage)) { throw "Flashable inner package was not generated: $flashPackage" } Copy-Item -LiteralPath $flashPackage -Destination $export -Force $exportHash = (Get-FileHash -LiteralPath $export -Algorithm SHA256).Hash @{ package = (Split-Path -Leaf $export) bytes = (Get-Item -LiteralPath $export).Length sha256 = $exportHash customer_fs_sha256 = $audioHash git_revision = (& git -C $projectRoot rev-parse HEAD).Trim() generated_at = (Get-Date).ToString("o") } | ConvertTo-Json | Set-Content -LiteralPath (Join-Path $outDir "firmware_manifest.json") -Encoding UTF8 Write-Host "Factory firmware: $export" Write-Host "SHA256: $exportHash" } finally { Copy-Item -LiteralPath $backup -Destination $configuredImage -Force $restoredHash = (Get-FileHash -LiteralPath $configuredImage -Algorithm SHA256).Hash if ($restoredHash -ne $originalHash) { throw "Business customer_fs.bin restore verification failed. Backup: $backup" } Write-Host "Business customer_fs.bin restored: $restoredHash" }