/
flashing.ts
631 lines (583 loc) · 18.5 KB
/
flashing.ts
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
import { bufferToArray, NumberFormat, getNumber } from "./buffer"
import { JDBus } from "./bus"
import { Packet } from "./packet"
import { JDDevice } from "./device"
import {
BootloaderCmd,
ControlCmd,
SRV_BOOTLOADER,
CMD_ADVERTISEMENT_DATA,
PACKET_REPORT,
PROGRESS,
CHANGE,
} from "./constants"
import {
assert,
bufferConcat,
bufferToString,
readBlobToUint8Array,
} from "./utils"
import { jdpack, jdunpack } from "./pack"
import { BootloaderError } from "./constants"
import { prettySize } from "./pretty"
import { Flags } from "./flags"
import { dualDeviceId, isDualDeviceId } from "./spec"
import { JDEventSource } from "./eventsource"
const BL_SUBPAGE_SIZE = 208
const BL_RETRIES = 15
const BL_SESSION_DELAY = 5
const BL_PAGE_DELAY = 5
let _startTime = 0
const uf2ExtTags: Record<string, number> = {
version: -0x9fc7bc,
name: -0x650d9d,
pageSize: 0x0be9f7,
productIdentifier: 0xc8a729,
}
/**
* UF2 page data structure
* @category Firmware
* @internal
*/
export interface FirmwarePage {
data: Uint8Array
targetAddress: number
}
/**
* Data structure representing a firmware binary
* @category Firmware
* @internal
*/
export interface FirmwareBlob {
pages: FirmwarePage[]
productIdentifier: number
pageSize: number
name: string
version: string
// name of the file or repo
store: string
}
function timestamp() {
if (!_startTime) _startTime = Date.now()
return Date.now() - _startTime
}
function log(msg: string) {
if (Flags.diagnostics) console.debug(`BL [${timestamp()}ms]: ${msg}`)
}
export class FirmwareUpdater extends JDEventSource {
constructor(
readonly bus: JDBus,
readonly blob: FirmwareBlob,
) {
super()
}
/**
* Flash firmware blob onto device
* @param bus
* @param blob
* @param updateCandidates
* @param ignoreFirmwareCheck
* @param progress
* @returns
* @category Firmware
*/
async flash(
updateCandidates: FirmwareInfo[],
ignoreFirmwareCheck: boolean,
) {
if (!updateCandidates?.length) return
const { bus, blob } = this
_startTime = Date.now()
log(`resetting ${updateCandidates.length} device(s)`)
const bootloaderDeviceIds: string[] = []
for (const d of updateCandidates) {
const device = bus.device(d.deviceId, true)
if (!device) {
log("device not found")
return
}
if (!device.bootloader) {
log(`resetting ${device}`)
bootloaderDeviceIds.push(dualDeviceId(device.deviceId))
await device.sendCtrlCommand(ControlCmd.Reset)
} else {
bootloaderDeviceIds.push(device.deviceId)
}
}
const allFlashers = await createFlashers(this, bootloaderDeviceIds)
const flashers = allFlashers
.filter(
f =>
!!ignoreFirmwareCheck ||
f.dev_class == blob.productIdentifier,
)
.filter(f =>
updateCandidates.find(
uc =>
uc.deviceId === f.device.deviceId ||
isDualDeviceId(uc.deviceId, f.device.deviceId),
),
)
if (!flashers.length) {
log(`no devices to flash`)
return
}
if (flashers.length != updateCandidates.length) {
console.error(
`expected ${updateCandidates.length} flashers, got ${flashers.length}`,
)
return
}
log(`flashing ${blob.name}`)
flashers[0].classClients = flashers
const unmounts: (() => void)[] = []
try {
flashers.forEach(f => {
f.device.firmwareUpdater = this
const dualDevice = bus.device(
dualDeviceId(f.device.deviceId),
true,
)
if (dualDevice) dualDevice.firmwareUpdater = this
unmounts.push(() => {
f.device.firmwareUpdater = undefined
if (dualDevice) dualDevice.firmwareUpdater = undefined
})
})
await flashers[0].flashFirmwareBlob(blob)
} finally {
unmounts.forEach(u => u())
}
}
}
class FlashClient {
private pageSize: number
private flashSize: number
private sessionId: number
classClients: FlashClient[]
private lastStatus: Packet
private pending: boolean
public dev_class: number
public device: JDDevice
private progressTotal: number
private progressIndex: number
constructor(
readonly updater: FirmwareUpdater,
adpkt: Packet,
) {
const d = bufferToArray(adpkt.data, NumberFormat.UInt32LE)
this.pageSize = d[1]
this.flashSize = d[2]
this.dev_class = d[3]
this.device = adpkt.device
this.handlePacket = this.handlePacket.bind(this)
}
get bus() {
return this.updater.bus
}
get progress() {
return this.progressTotal ? this.progressIndex / this.progressTotal : 0
}
private handlePacket(pkt: Packet) {
if (pkt.serviceCommand == BootloaderCmd.PageData) this.lastStatus = pkt
}
private start() {
this.device.on(PACKET_REPORT, this.handlePacket)
}
private stop() {
this.device.off(PACKET_REPORT, this.handlePacket)
}
private async sendCommandAsync(p: Packet) {
p.serviceIndex = 1
await p.sendCmdAsync(this.device)
}
private async startFlashAsync() {
this.sessionId = (Math.random() * 0x10000000) | 0
for (const d of this.classClients) {
d.start()
log(
`flashing ${d.device.shortId}; available flash=${
d.flashSize / 1024
}kb; page=${d.pageSize}b`,
)
}
const setsession = Packet.jdpacked<[number]>(
BootloaderCmd.SetSession,
"u32",
[this.sessionId],
)
this.allPending()
for (let i = 0; i < BL_RETRIES; ++i) {
for (const d of this.classClients) {
if (d.pending) {
if (
d.lastStatus &&
d.lastStatus.getNumber(NumberFormat.UInt32LE, 0) ==
this.sessionId
) {
d.pending = false
} else {
d.lastStatus = null
log(`set session ${this.sessionId} on ${d.device}`)
await d.sendCommandAsync(setsession)
}
await this.bus.delay(BL_SESSION_DELAY)
}
}
if (this.numPending() == 0) break
await this.waitForStatusAsync()
}
if (this.numPending()) throw new Error("Can't set session id")
}
private async endFlashAsync() {
for (const f of this.classClients) {
await this.bus.delay(10)
await f.device.reset()
}
}
private allPending() {
for (const c of this.classClients) {
c.pending = true
c.lastStatus = null
}
}
private numPending() {
let num = 0
for (const c of this.classClients) if (c.pending) num++
return num
}
private async waitForStatusAsync() {
for (let i = 0; i < 100; ++i) {
if (this.classClients.every(c => c.lastStatus != null)) break
await this.bus.delay(5)
}
}
private async flashPage(page: FirmwarePage) {
const pageAddr = page.targetAddress
const pageSize = this.pageSize
const numSubpage =
((pageSize + BL_SUBPAGE_SIZE - 1) / BL_SUBPAGE_SIZE) | 0
log(
`flash ${prettySize(this.pageSize)} at ${(
pageAddr & 0xffffff
).toString(16)}`,
)
if (page.data.length != this.pageSize)
throw new Error("invalid page size")
for (const f of this.classClients) f.lastStatus = null
this.allPending()
for (let i = 0; i < BL_RETRIES; ++i) {
log(` attempt ${i}`)
let currSubpage = 0
for (let suboff = 0; suboff < pageSize; suboff += BL_SUBPAGE_SIZE) {
let sz = BL_SUBPAGE_SIZE
if (suboff + sz > pageSize) sz = pageSize - suboff
log(
`send sub page ${currSubpage}/${
numSubpage - 1
} at ${suboff.toString(16)}[${sz}]`,
)
const hd = jdpack("u32 u16 u8 u8 u32 u32 u32 u32 u32", [
pageAddr,
suboff,
currSubpage++,
numSubpage - 1,
this.sessionId,
0,
0,
0,
0,
])
assert(hd.length == 4 * 7)
const p = Packet.from(
BootloaderCmd.PageData,
bufferConcat(hd, page.data.slice(suboff, suboff + sz)),
)
// in first round, just broadcast everything
// in other rounds, broadcast everything except for last packet
if (
this.classClients.length > 1 &&
(i == 0 || currSubpage < numSubpage)
)
await p.sendAsMultiCommandAsync(this.bus, SRV_BOOTLOADER)
else {
for (const f of this.classClients)
if (f.pending) {
f.lastStatus = null
await f.sendCommandAsync(p)
}
}
await this.bus.delay(BL_PAGE_DELAY)
}
await this.waitForStatusAsync()
for (const f of this.classClients) {
if (f.pending) {
let err = ""
if (f.lastStatus) {
const [session_id, page_error, pageAddrR] = jdunpack<
[number, BootloaderError, number]
>(f.lastStatus.data, "u32 u32 u32")
if (session_id != this.sessionId)
err = "invalid session_id"
else if (pageAddrR != pageAddr)
err = "invalid page address"
else if (page_error)
err =
"err: " +
(BootloaderError[page_error] || page_error)
} else {
err = "timeout"
}
if (err) {
f.lastStatus = null
log(`retry ${f.device}: ${err}`)
} else {
f.pending = false
}
}
}
if (this.numPending() == 0) {
log(
`page ${
pageAddr & 0xffffff
} done, ${i}/${BL_RETRIES} retries`,
)
return
}
}
throw new Error("too many retries")
}
public async flashFirmwareBlob(fw: FirmwareBlob) {
const waitCycles = 15
this.progressTotal = fw.pages.length + waitCycles + 3
this.progressIndex = 0
const prog = () => {
this.updater.emit(PROGRESS, this.progress)
this.progressIndex++
}
try {
this.updater.emit(CHANGE)
prog()
await this.startFlashAsync()
prog()
for (const page of fw.pages) {
await this.flashPage(page)
prog()
}
} finally {
try {
// even if something failed, try to reset everyone
await this.endFlashAsync()
prog()
// wait until we're out of bootloader mode; otherwise the subsequent scan will keep devices in BL mode
for (let i = 0; i < waitCycles; ++i) {
await this.bus.delay(150)
prog()
}
} finally {
// even if resetting failed, unregister event listeners
for (const d of this.classClients) {
d.stop()
}
}
this.updater.emit(CHANGE)
}
}
}
const UF2_MAGIC_START0 = 0x0a324655
const UF2_MAGIC_START1 = 0x9e5d5157
const UF2_MAGIC_END = 0x0ab16f30
/**
* Parses a UF2 firmware binary into firmware blobs
* @param uf2
* @param store
* @returns
* @category Firmware
*/
export function parseUF2Firmware(
uf2: Uint8Array,
store: string,
): FirmwareBlob[] {
const blobs: FirmwareBlob[] = []
let currBlob: FirmwareBlob
for (let off = 0; off < uf2.length; off += 512) {
const header = uf2.slice(off, off + 32)
const [
magic0,
magic1,
flags,
trgaddr,
payloadSize,
blkNo,
numBlocks,
familyID,
] = bufferToArray(header, NumberFormat.UInt32LE)
if (
magic0 != UF2_MAGIC_START0 ||
magic1 != UF2_MAGIC_START1 ||
getNumber(uf2, NumberFormat.UInt32LE, off + 512 - 4) !=
UF2_MAGIC_END
)
throw new Error("invalid UF2")
if (blkNo == 0) {
flush()
currBlob = {
pages: [],
productIdentifier: familyID,
version: "",
pageSize: 1024,
name: "FW " + familyID.toString(16),
store,
}
}
if (flags & 0x8000)
parseExtTags(uf2.slice(off + 32 + payloadSize, off + 512))
const pageSize = currBlob.pageSize || 1024
let currPage = currBlob.pages[currBlob.pages.length - 1]
if (
!currPage ||
!(
currPage.targetAddress <= trgaddr &&
trgaddr < currPage.targetAddress + pageSize
)
) {
currPage = {
targetAddress: trgaddr & ~(pageSize - 1),
data: new Uint8Array(pageSize),
}
currPage.data.fill(0xff)
currBlob.pages.push(currPage)
}
currPage.data.set(
uf2.slice(off + 32, off + 32 + payloadSize),
trgaddr - currPage.targetAddress,
)
}
flush()
return blobs
function flush() {
if (currBlob) blobs.push(currBlob)
}
function parseExtTags(buf: Uint8Array) {
let sz = 0
for (let i = 0; i < buf.length; i += sz) {
sz = buf[i]
if (sz == 0) break
const desig = getNumber(buf, NumberFormat.UInt32LE, i) >>> 8
for (const key of Object.keys(uf2ExtTags)) {
const tg = uf2ExtTags[key]
if (desig == Math.abs(tg)) {
let v: any
if (tg < 0) {
v = bufferToString(buf.slice(i + 4, i + sz))
} else {
v = getNumber(buf, NumberFormat.UInt32LE, i + 4)
}
const cbany = currBlob as any
cbany[key] = v
break
}
}
sz = (sz + 3) & ~3
}
}
}
/**
* Firmware information
* @internal
*/
export interface FirmwareInfo {
deviceId: string
version: string
name: string
productIdentifier: number
bootloaderProductIdentifier: number
}
/**
* Parse a UF2 firmware file and extracts firmware blobs
* @param blob
* @param store
* @returns
* @category Firmware
*/
export async function parseFirmwareFile(
blob: Blob,
store?: string,
): Promise<FirmwareBlob[]> {
const data = await readBlobToUint8Array(blob)
const buf = new Uint8Array(data)
const uf2Blobs = parseUF2Firmware(buf, store)
return uf2Blobs
}
async function createFlashers(
updater: FirmwareUpdater,
bootloaderDeviceIds?: string[],
) {
const bus = updater.bus
const flashers: FlashClient[] = []
const numTries = 10
const tryDelay = 10
const handlePkt = (p: Packet) => {
// note that we may get this even if recovery==false due to someone else asking
// (eg when the user set the recovery mode toggle)
if (
p.serviceIndex == 1 &&
p.serviceCommand == CMD_ADVERTISEMENT_DATA &&
p.getNumber(NumberFormat.UInt32LE, 0) == SRV_BOOTLOADER
) {
if (!flashers.find(f => f.device.deviceId == p.deviceIdentifier)) {
log(`new flasher`)
flashers.push(new FlashClient(updater, p))
}
}
}
try {
bus.on(PACKET_REPORT, handlePkt)
for (let i = 0; i < numTries; ++i) {
if (bootloaderDeviceIds?.length > 1) {
// also ask BL services if any
const bl_announce = Packet.onlyHeader(CMD_ADVERTISEMENT_DATA)
await bl_announce.sendAsMultiCommandAsync(bus, SRV_BOOTLOADER)
await bus.delay(tryDelay)
} else {
for (const id of bootloaderDeviceIds) {
const bl_announce = Packet.onlyHeader(
CMD_ADVERTISEMENT_DATA,
)
bl_announce.serviceIndex = 1
bl_announce.deviceIdentifier = id
bl_announce.isCommand = true
await bus.sendPacketAsync(bl_announce)
}
}
await bus.delay(tryDelay)
}
} finally {
bus.off(PACKET_REPORT, handlePkt)
}
return flashers
}
/**
* Indicates if a firmware blob is applicated to the device information
* @param dev
* @param blob
* @returns
* @category Firmware
*/
export function updateApplicable(dev: FirmwareInfo, blob: FirmwareBlob) {
return (
dev &&
blob &&
dev.bootloaderProductIdentifier == blob.productIdentifier &&
dev.version !== blob.version
)
}
/**
* This command can be sent every 50ms to keep devices in bootloader mode
* @param bus
* @category Firmware
*/
export async function sendStayInBootloaderCommand(bus: JDBus) {
const bl_announce = Packet.onlyHeader(BootloaderCmd.Info)
await bl_announce.sendAsMultiCommandAsync(bus, SRV_BOOTLOADER)
}