-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpush_rpi.ps1
More file actions
239 lines (208 loc) · 7.34 KB
/
push_rpi.ps1
File metadata and controls
239 lines (208 loc) · 7.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#!/usr/bin/env pwsh
# =============================================================================
# TensorOS — Push OTA Update to Raspberry Pi over Bluetooth Serial
#
# Usage:
# .\push_rpi.ps1 -ComPort COM5 # Chain-load (RAM, fast dev)
# .\push_rpi.ps1 -ComPort COM5 -Flash # Persistent SD write
# .\push_rpi.ps1 -ComPort COM5 -Kernel .\custom.img # Custom binary
# .\push_rpi.ps1 -ComPort COM5 -Build # Build first, then push
#
# The script:
# 1. (Optionally) builds the ARM64 kernel with build_rpi.ps1
# 2. Opens the BT COM port
# 3. Sends the "ota" or "flash" shell command
# 4. Waits for "RDY" response
# 5. Sends: "OTA!" + uint32(size) + raw_bytes + uint32(crc32)
# 6. Waits for "OK!" then "BOOT"
# =============================================================================
param(
[Parameter(Mandatory = $true)]
[string]$ComPort, # e.g. COM5
[string]$Kernel = "", # Path to kernel8.img (default: auto-detect)
[switch]$Flash, # Use persistent SD flash instead of RAM chain-load
[switch]$Build, # Run build_rpi.ps1 before pushing
[int]$BaudRate = 115200,
[int]$TimeoutSec = 60
)
$ErrorActionPreference = "Stop"
# ---- CRC-32 (IEEE) ----
function Get-CRC32([byte[]]$Data) {
[uint32]$crc = 0xFFFFFFFF
foreach ($b in $Data) {
$crc = $crc -bxor $b
for ($j = 0; $j -lt 8; $j++) {
if ($crc -band 1) {
$crc = ($crc -shr 1) -bxor 0xEDB88320
} else {
$crc = $crc -shr 1
}
}
}
return $crc -bxor 0xFFFFFFFF
}
# ---- Step 1: Optionally build ----
if ($Build) {
Write-Host "[BUILD] Running build_rpi.ps1..." -ForegroundColor Cyan
& "$PSScriptRoot\build_rpi.ps1"
if ($LASTEXITCODE -ne 0) {
Write-Host "[BUILD] Build failed!" -ForegroundColor Red
exit 1
}
Write-Host "[BUILD] Build succeeded." -ForegroundColor Green
}
# ---- Step 2: Find kernel binary ----
if ($Kernel -eq "") {
# Auto-detect: look for build output
$candidates = @(
"$PSScriptRoot\build\arm64\kernel8.img",
"$PSScriptRoot\kernel8.img"
)
foreach ($c in $candidates) {
if (Test-Path $c) {
$Kernel = $c
break
}
}
if ($Kernel -eq "") {
Write-Host "[ERROR] No kernel8.img found. Specify -Kernel or use -Build." -ForegroundColor Red
exit 1
}
}
if (-not (Test-Path $Kernel)) {
Write-Host "[ERROR] Kernel not found: $Kernel" -ForegroundColor Red
exit 1
}
$kernelBytes = [System.IO.File]::ReadAllBytes($Kernel)
$kernelSize = $kernelBytes.Length
$crc = Get-CRC32 $kernelBytes
Write-Host "[INFO] Kernel: $Kernel ($kernelSize bytes, CRC32=0x$($crc.ToString('X8')))" -ForegroundColor Cyan
# ---- Step 3: Open COM port ----
Write-Host "[COMM] Opening $ComPort at $BaudRate baud..." -ForegroundColor Yellow
$port = New-Object System.IO.Ports.SerialPort $ComPort, $BaudRate, "None", 8, "One"
$port.ReadTimeout = $TimeoutSec * 1000
$port.WriteTimeout = 10000
$port.DtrEnable = $true
$port.RtsEnable = $true
$port.Open()
Write-Host "[COMM] Port opened." -ForegroundColor Green
# Helper: read a line (until \n)
function Read-Line {
$buf = ""
$deadline = (Get-Date).AddSeconds($TimeoutSec)
while ((Get-Date) -lt $deadline) {
if ($port.BytesToRead -gt 0) {
$ch = [char]$port.ReadByte()
if ($ch -eq "`n") { return $buf.Trim() }
$buf += $ch
} else {
Start-Sleep -Milliseconds 10
}
}
return $null # timeout
}
# Helper: drain any pending input
function Drain-Input {
Start-Sleep -Milliseconds 200
while ($port.BytesToRead -gt 0) {
[void]$port.ReadByte()
}
}
try {
# ---- Step 4: Send shell command ----
Drain-Input
$cmd = if ($Flash) { "flash" } else { "ota" }
Write-Host "[COMM] Sending '$cmd' command to TensorOS shell..." -ForegroundColor Yellow
$port.Write("$cmd`r")
Start-Sleep -Milliseconds 500 # Let shell process + echo
# ---- Step 5: Wait for RDY ----
Write-Host "[COMM] Waiting for RDY..." -ForegroundColor Yellow
$ready = $false
$deadline = (Get-Date).AddSeconds($TimeoutSec)
while ((Get-Date) -lt $deadline) {
if ($port.BytesToRead -gt 0) {
$line = Read-Line
Write-Host " < $line" -ForegroundColor DarkGray
if ($line -match "RDY") {
$ready = $true
break
}
if ($line -match "ERR") {
Write-Host "[ERROR] Device reported error: $line" -ForegroundColor Red
exit 1
}
}
Start-Sleep -Milliseconds 50
}
if (-not $ready) {
Write-Host "[ERROR] Timeout waiting for RDY." -ForegroundColor Red
exit 1
}
Write-Host "[COMM] Device ready. Sending kernel..." -ForegroundColor Green
# ---- Step 6: Send OTA protocol packet ----
# Magic: "OTA!"
$magic = [System.Text.Encoding]::ASCII.GetBytes("OTA!")
$port.Write($magic, 0, 4)
# Size: uint32 LE
$sizeBytes = [System.BitConverter]::GetBytes([uint32]$kernelSize)
$port.Write($sizeBytes, 0, 4)
# Data: raw kernel binary (in chunks for flow control)
$chunkSize = 1024
$sent = 0
while ($sent -lt $kernelSize) {
$remaining = $kernelSize - $sent
$chunk = [Math]::Min($chunkSize, $remaining)
$port.Write($kernelBytes, $sent, $chunk)
$sent += $chunk
# Progress
$pct = [int]($sent * 100 / $kernelSize)
Write-Host "`r[XFER] $sent / $kernelSize bytes ($pct%)" -NoNewline -ForegroundColor Cyan
# Small delay for flow control (BT SPP can buffer but let's be safe)
if ($sent % 8192 -eq 0) {
Start-Sleep -Milliseconds 10
}
}
Write-Host "" # newline after progress
# CRC32: uint32 LE
$crcBytes = [System.BitConverter]::GetBytes([uint32]$crc)
$port.Write($crcBytes, 0, 4)
Write-Host "[XFER] Transfer complete. Waiting for verification..." -ForegroundColor Yellow
# ---- Step 7: Wait for OK! ----
$verified = $false
$deadline = (Get-Date).AddSeconds(30)
while ((Get-Date) -lt $deadline) {
$line = Read-Line
if ($line -ne $null) {
Write-Host " < $line" -ForegroundColor DarkGray
if ($line -match "OK!") {
$verified = $true
Write-Host "[VERIFY] CRC verified!" -ForegroundColor Green
}
if ($line -match "BOOT") {
Write-Host "[BOOT] Device is booting new kernel!" -ForegroundColor Green
break
}
if ($line -match "ERR") {
Write-Host "[ERROR] $line" -ForegroundColor Red
exit 1
}
if ($line -match "WARN") {
Write-Host "[WARN] $line" -ForegroundColor Yellow
}
}
}
if (-not $verified) {
Write-Host "[ERROR] Did not receive verification." -ForegroundColor Red
exit 1
}
Write-Host ""
if ($Flash) {
Write-Host "[DONE] Kernel flashed to SD card. Device is rebooting." -ForegroundColor Green
} else {
Write-Host "[DONE] Kernel chain-loaded into RAM. (Not persisted to SD)" -ForegroundColor Green
Write-Host " Use -Flash to write permanently." -ForegroundColor DarkGray
}
}
finally {
if ($port.IsOpen) { $port.Close() }
}