Files
JBao_Temp_Humi_Fan_FW/tools/pack_firmware.ps1
T

169 lines
5.6 KiB
PowerShell

# pack_firmware.ps1
# Extract JieLi burn files and zip as Project_Version_Date.
# ASCII only: Windows PowerShell 5.1 mis-parses UTF-8 without BOM.
#
# Usage:
# .\build.bat - build + download.bat + pack
# make pack - pack only
# tools\pack_firmware.bat
[CmdletBinding()]
param(
[string]$ProjectName = "JBao_Temp_Humi_Fan",
[string]$SdkRoot = ""
)
$ErrorActionPreference = "Stop"
$exitCode = 0
try {
if ([string]::IsNullOrWhiteSpace($SdkRoot)) {
$SdkRoot = Split-Path -Parent $PSScriptRoot
}
$VersionHeader = Join-Path $SdkRoot "apps\usr_jb_proto\Protocol\jb_product.h"
$DownloadDir = Join-Path $SdkRoot "cpu\br28\tools\download\earphone"
$ReleaseRoot = Join-Path $SdkRoot "release"
function Read-FwVersion {
param([string]$HeaderPath)
if (-not (Test-Path -LiteralPath $HeaderPath)) {
throw "version header not found: $HeaderPath"
}
$text = Get-Content -LiteralPath $HeaderPath -Raw -Encoding UTF8
$major = $null
$minor = $null
$patch = $null
if ($text -match '(?m)^\s*#define\s+JB_MCU_SW_VERSION_MAJOR\s+(\d+)') {
$major = $Matches[1]
}
if ($text -match '(?m)^\s*#define\s+JB_MCU_SW_VERSION_MINOR\s+(\d+)') {
$minor = $Matches[1]
}
if ($text -match '(?m)^\s*#define\s+JB_MCU_SW_VERSION_PATCH\s+(\d+)') {
$patch = $Matches[1]
}
if (($null -eq $major) -or ($null -eq $minor) -or ($null -eq $patch)) {
throw "cannot parse JB_MCU_SW_VERSION_* from $HeaderPath"
}
return "V$major.$minor.$patch"
}
$PackItems = @(
@{ Name = "jl_isd_nokey.fw"; Fallback = "jl_isd.fw"; Suffix = "_nokey_burner.fw"; Required = $true; NoteKey = "burner_nokey" },
@{ Name = "update_nokey.ufw"; Fallback = "update.ufw"; Suffix = "_nokey_app_ota.ufw"; Required = $false; NoteKey = "app_nokey" },
@{ Name = "db_update_data_nokey.bin"; Fallback = "db_update_data.bin"; Suffix = "_nokey_gateway_ota.bin"; Required = $false; NoteKey = "gw_nokey" },
@{ Name = "jl_isd_key.fw"; Fallback = ""; Suffix = "_key_burner.fw"; Required = $false; NoteKey = "burner_key" },
@{ Name = "update_key.ufw"; Fallback = ""; Suffix = "_key_app_ota.ufw"; Required = $false; NoteKey = "app_key" },
@{ Name = "db_update_data_key.bin"; Fallback = ""; Suffix = "_key_gateway_ota.bin"; Required = $false; NoteKey = "gw_key" }
)
$NoteZh = @{}
$notesFile = Join-Path $PSScriptRoot "pack_notes_zh.txt"
Get-Content -LiteralPath $notesFile -Encoding UTF8 | ForEach-Object {
if ($_ -match '^([^|]+)\|(.*)$') {
$NoteZh[$Matches[1].Trim()] = $Matches[2].Trim()
}
}
Write-Host "========================================"
Write-Host " pack firmware"
Write-Host "========================================"
$version = Read-FwVersion -HeaderPath $VersionHeader
$dateStr = Get-Date -Format "yyMMddHHmm"
$pkgName = "${ProjectName}_${version}_${dateStr}"
Write-Host "project : $ProjectName"
Write-Host "version : $version (from jb_product.h)"
Write-Host "date : $dateStr"
Write-Host "source : $DownloadDir"
Write-Host ""
if (-not (Test-Path -LiteralPath $DownloadDir)) {
throw "download dir not found, compile the project first: $DownloadDir"
}
$copied = @()
foreach ($item in $PackItems) {
$src = Join-Path $DownloadDir $item.Name
if (-not (Test-Path -LiteralPath $src) -and $item.Fallback) {
$src = Join-Path $DownloadDir $item.Fallback
}
if (Test-Path -LiteralPath $src) {
$item.Src = $src
$item.Dest = $pkgName + $item.Suffix
$copied += $item
continue
}
if ($item.Required) {
throw "missing required file: $($item.Name); please build the firmware first."
}
Write-Host "[skip] $($item.Name) not generated"
}
if ($copied.Count -eq 0) {
throw "no firmware files found in $DownloadDir"
}
$pkgDir = Join-Path $ReleaseRoot $pkgName
if (Test-Path -LiteralPath $pkgDir) {
Remove-Item -LiteralPath $pkgDir -Recurse -Force
}
New-Item -ItemType Directory -Path $pkgDir -Force | Out-Null
foreach ($item in $copied) {
Copy-Item -LiteralPath $item.Src -Destination (Join-Path $pkgDir $item.Dest) -Force
$size = (Get-Item -LiteralPath $item.Src).Length
Write-Host ("[copy] {0,-28} -> {1} ({2} bytes)" -f (Split-Path $item.Src -Leaf), $item.Dest, $size)
}
$fileList = New-Object System.Collections.Generic.List[string]
foreach ($item in $copied) {
$note = $NoteZh[$item.NoteKey]
[void]$fileList.Add((" {0}" -f $item.Dest))
[void]$fileList.Add((" {0}" -f $note))
}
$tplPath = Join-Path $PSScriptRoot "pack_readme_zh.txt"
$readme = Get-Content -LiteralPath $tplPath -Raw -Encoding UTF8
$readme = $readme.Replace("{{PKG_NAME}}", $pkgName)
$readme = $readme.Replace("{{PROJECT}}", $ProjectName)
$readme = $readme.Replace("{{VERSION}}", $version)
$readme = $readme.Replace("{{DATE}}", (Get-Date -Format "yyyy-MM-dd HH:mm:ss"))
$readme = $readme.Replace("{{FILE_LIST}}", ($fileList -join "`r`n"))
$readme = $readme -replace "`n", "`r`n"
$readme = $readme -replace "`r`r`n", "`r`n"
[System.IO.File]::WriteAllText(
(Join-Path $pkgDir "README.txt"),
$readme,
[System.Text.UTF8Encoding]::new($true)
)
$zipPath = Join-Path $ReleaseRoot "$pkgName.zip"
if (Test-Path -LiteralPath $zipPath) {
Remove-Item -LiteralPath $zipPath -Force
}
Compress-Archive -Path (Join-Path $pkgDir "*") -DestinationPath $zipPath -CompressionLevel Optimal
Write-Host ""
Write-Host "folder : $pkgDir"
Write-Host "zip : $zipPath"
Write-Host "done."
Write-Host "========================================"
} catch {
Write-Host ""
Write-Host "pack failed: $($_.Exception.Message)"
$exitCode = 1
}
exit $exitCode