# Build the dongle firmware: safeboot.bin (stage 1), app.bin (stage 2), and # dongle_combined.bin (safeboot padded to 4 KB + app) for the one-time install. # Uses the ARM toolchain PlatformIO installed (pio pkg install -g -p ststm32). $ErrorActionPreference = "Stop" $bin = "$env:USERPROFILE\.platformio\packages\toolchain-gccarmnoneeabi\bin" $gcc = (Get-ChildItem "$bin\arm-none-eabi-gcc*.exe" | Select-Object -First 1).FullName $objcopy = (Get-ChildItem "$bin\arm-none-eabi-objcopy*.exe" | Select-Object -First 1).FullName $size = (Get-ChildItem "$bin\arm-none-eabi-size*.exe" | Select-Object -First 1).FullName $cf = "-mcpu=cortex-m0 -mthumb -Os -ffreestanding -nostdlib -ffunction-sections -fdata-sections -Wall -Wextra" $here = $PSScriptRoot Set-Location $here $out = "$here\build"; New-Item -ItemType Directory -Force $out | Out-Null $lf = @("-mcpu=cortex-m0", "-mthumb", "-nostdlib", "-Wl,--gc-sections") # stage 1 & $gcc $cf.Split(' ') -c safeboot.c -o "$out\safeboot.o" & $gcc $lf -T safeboot.ld "$out\safeboot.o" -o "$out\safeboot.elf" & $objcopy -O binary "$out\safeboot.elf" "$out\safeboot.bin" # stage 2 & $gcc $cf.Split(' ') -c app.c -o "$out\app.o" & $gcc $cf.Split(' ') -c app_startup.c -o "$out\app_startup.o" & $gcc $lf -T app.ld "$out\app.o" "$out\app_startup.o" -o "$out\app.elf" & $objcopy -O binary "$out\app.elf" "$out\app.bin" & $size "$out\safeboot.elf" "$out\app.elf" # combined image: safeboot in [0,4KB), app at 4KB $sb = [System.IO.File]::ReadAllBytes("$out\safeboot.bin") $ap = [System.IO.File]::ReadAllBytes("$out\app.bin") if ($sb.Length -gt 4096) { throw "safeboot is $($sb.Length) bytes, over the 4 KB reserved" } $img = New-Object byte[] (4096 + $ap.Length) for ($i = 0; $i -lt $img.Length; $i++) { $img[$i] = 0xFF } [Array]::Copy($sb, 0, $img, 0, $sb.Length) [Array]::Copy($ap, 0, $img, 4096, $ap.Length) [System.IO.File]::WriteAllBytes("$out\dongle_combined.bin", $img) Write-Host "safeboot $($sb.Length) B, app $($ap.Length) B, combined $($img.Length) B -> build\dongle_combined.bin"