-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathesp32.ts
More file actions
370 lines (311 loc) · 12.5 KB
/
Copy pathesp32.ts
File metadata and controls
370 lines (311 loc) · 12.5 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
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
import { bstrToUi8, ESP_CHECKSUM_MAGIC } from "../util";
import { alignFilePosition, BaseFirmwareImage, ELFSection, ESP_IMAGE_MAGIC, ImageSegment } from "./base";
import { ESPError } from "../types/error";
import { ROM } from "../targets/rom";
import { ESP32ROM } from "../targets/esp32";
export class ESP32FirmwareImage extends BaseFirmwareImage {
securePad: string | null = null;
flashMode = 0;
flashSizeFreq = 0;
version = 1;
ramOnlyHeader: boolean;
// ROM bootloader will read the wp_pin field if SPI flash
// pins are remapped via flash. IDF actually enables QIO only
// from software bootloader, so this can be ignored. But needs
// to be set to this value so ROM bootloader will skip it.
WP_PIN_DISABLED = 0xee;
wpPin: number = this.WP_PIN_DISABLED;
// Extended header fields
clkDrv = 0;
qDrv = 0;
dDrv = 0;
csDrv = 0;
hdDrv = 0;
wpDrv = 0;
chipId = 0;
minRev = 0;
minRevFull = 0;
maxRevFull = 0;
appendDigest: boolean;
storedDigest: Uint8Array | null = null;
calcDigest: Uint8Array | null = null;
dataLength = 0;
IROM_ALIGN = 65536;
ROM_LOADER: ESP32ROM;
constructor(rom: ROM, loadFile: Uint8Array | string | null = null, appendDigest = true, ramOnlyHeader = false) {
super(rom);
this.ROM_LOADER = rom as ESP32ROM;
this.appendDigest = appendDigest;
this.ramOnlyHeader = ramOnlyHeader;
if (loadFile !== null) {
this.loadFromFile(loadFile);
}
}
async loadFromFile(loadFile: Uint8Array | string): Promise<void> {
const start = 0;
const binaryData = loadFile instanceof Uint8Array ? loadFile : bstrToUi8(loadFile);
let offset = 0;
const segments = this.loadCommonHeader(binaryData, offset, ESP_IMAGE_MAGIC);
offset += 8;
this.loadExtendedHeader(binaryData, offset);
offset += 16; // Extended header is 16 bytes
// Load segments
for (let i = 0; i < segments; i++) {
const segment = this.loadSegment(binaryData, offset);
offset += 8 + segment.data.length;
}
// Read checksum
this.checksum = this.readChecksum(binaryData, offset);
offset = alignFilePosition(offset, 16);
if (this.appendDigest) {
const end = offset;
this.storedDigest = binaryData.slice(offset, offset + this.SHA256_DIGEST_LEN);
const shaDigest = await crypto.subtle.digest("SHA-256", binaryData.slice(start, end));
this.calcDigest = new Uint8Array(shaDigest);
this.dataLength = end - start;
}
this.verify();
}
isFlashAddr(addr: number): boolean {
return (
(this.ROM_LOADER.IROM_MAP_START <= addr && addr < this.ROM_LOADER.IROM_MAP_END) ||
(this.ROM_LOADER.DROM_MAP_START <= addr && addr < this.ROM_LOADER.DROM_MAP_END)
);
}
async save() {
let totalSegments = 0;
const output = new Uint8Array(1024 * 1024); // Start with 1MB buffer, will grow if needed
let offset = 0;
// Write common header
this.writeCommonHeader(output, offset, this.segments.length);
offset += 8;
// Write extended header
this.saveExtendedHeader(output, offset);
offset += 16;
let checksum = ESP_CHECKSUM_MAGIC;
// Split segments into flash-mapped vs ram-loaded
const flashSegments = this.segments.filter((s) => this.isFlashAddr(s.addr)).sort((a, b) => a.addr - b.addr);
const ramSegments = this.segments.filter((s) => !this.isFlashAddr(s.addr)).sort((a, b) => a.addr - b.addr);
// Patch to support ESP32-C6 union bus memmap
// move ".flash.appdesc" segment to the top of the flash segment
for (let i = 0; i < flashSegments.length; i++) {
const segment = flashSegments[i];
if (segment instanceof ELFSection && segment.name === ".flash.appdesc") {
flashSegments.splice(i, 1);
flashSegments.unshift(segment);
break;
}
}
// For the bootloader image
// move ".dram0.bootdesc" segment to the top of the ram segment
// So bootdesc will be at the very top of the binary at 0x20 offset
// (in the first segment).
for (let i = 0; i < ramSegments.length; i++) {
const segment = ramSegments[i];
if (segment instanceof ELFSection && segment.name === ".dram0.bootdesc") {
ramSegments.splice(i, 1);
ramSegments.unshift(segment);
break;
}
}
// Check for multiple ELF sections in same flash mapping region
if (flashSegments.length > 0) {
let lastAddr = flashSegments[0].addr;
for (const segment of flashSegments.slice(1)) {
if (Math.floor(segment.addr / this.IROM_ALIGN) === Math.floor(lastAddr / this.IROM_ALIGN)) {
throw new ESPError(
`Segment loaded at 0x${segment.addr.toString(16)} lands in same 64KB flash mapping ` +
`as segment loaded at 0x${lastAddr.toString(16)}. Can't generate binary. ` +
"Suggest changing linker script or ELF to merge sections.",
);
}
lastAddr = segment.addr;
}
}
if (this.ramOnlyHeader) {
// Write RAM segments first
for (const segment of ramSegments) {
checksum = this.saveSegment(output, offset, segment, checksum);
offset += 8 + segment.data.length;
totalSegments++;
}
this.appendChecksum(output, offset, checksum);
offset = alignFilePosition(offset, 16);
// Write flash segments
for (const segment of flashSegments.reverse()) {
let padLen = this.getAlignmentDataNeeded(segment, offset);
if (padLen > 0) {
const align_min = this.ROM_LOADER.BOOTLOADER_FLASH_OFFSET - this.SEG_HEADER_LEN;
if (padLen < align_min) {
// in case pad_len does not fit minimum alignment,
// pad it to next aligned boundary
padLen += this.IROM_ALIGN;
}
padLen -= this.ROM_LOADER.BOOTLOADER_FLASH_OFFSET;
const padSegment = new ImageSegment(0, new Uint8Array(padLen).fill(0), offset);
checksum = this.saveSegment(output, offset, padSegment, checksum);
offset += 8 + padLen;
totalSegments++;
}
this.saveFlashSegment(output, offset, segment);
offset += 8 + segment.data.length;
totalSegments++;
}
} else {
// Write flash segments with padding
while (flashSegments.length > 0) {
const segment = flashSegments[0];
const padLen = this.getAlignmentDataNeeded(segment, offset);
if (padLen > 0) {
// need to pad
if (ramSegments.length > 0 && padLen > this.SEG_HEADER_LEN) {
// Split a part of the first RAM segment to use as padding
const padSegment = ramSegments[0].splitImage(padLen);
if (ramSegments[0].data.length === 0) {
ramSegments.shift();
}
checksum = this.saveSegment(output, offset, padSegment, checksum);
} else {
// Use zero padding
const padSegment = new ImageSegment(0, new Uint8Array(padLen).fill(0), offset);
checksum = this.saveSegment(output, offset, padSegment, checksum);
}
offset += 8 + padLen;
totalSegments++;
} else {
// write the flash segment
if ((offset + 8) % this.IROM_ALIGN !== segment.addr % this.IROM_ALIGN) {
throw new Error("Flash segment alignment mismatch");
}
checksum = this.saveFlashSegment(output, offset, segment, checksum);
flashSegments.shift();
offset += 8 + segment.data.length;
totalSegments++;
}
}
// Write remaining RAM segments
for (const segment of ramSegments) {
checksum = this.saveSegment(output, offset, segment, checksum);
offset += 8 + segment.data.length;
totalSegments++;
}
}
// Handle secure padding if needed
if (this.securePad) {
if (!this.appendDigest) {
throw new Error("secure_pad only applies if a SHA-256 digest is also appended to the image");
}
const alignPast = (offset + this.SEG_HEADER_LEN) % this.IROM_ALIGN;
const checksumSpace = 16; // 16 byte aligned checksum
let spaceAfterChecksum = 0;
if (this.securePad === "1") {
// Secure Boot V1: SHA-256 digest + version + signature + 12 trailing bytes
spaceAfterChecksum = 32 + 4 + 64 + 12;
} else if (this.securePad === "2") {
// Secure Boot V2: SHA-256 digest + signature sector (placed after 64KB boundary)
spaceAfterChecksum = 32;
}
const padLen = (this.IROM_ALIGN - alignPast - checksumSpace - spaceAfterChecksum) % this.IROM_ALIGN;
const padSegment = new ImageSegment(0, new Uint8Array(padLen).fill(0), offset);
checksum = this.saveSegment(output, offset, padSegment, checksum);
offset += 8 + padLen;
totalSegments++;
}
// Append checksum after all segments are written
if (!this.ramOnlyHeader) {
this.appendChecksum(output, offset, checksum);
offset = alignFilePosition(offset, 16);
}
const imageLength = offset;
// Go back to the initial header and write the new segment count
// This header is not checksummed
if (this.ramOnlyHeader) {
// Update the header with the RAM segments quantity as it should be
// visible by the ROM bootloader
output[1] = ramSegments.length;
} else {
output[1] = totalSegments;
}
if (this.appendDigest) {
// calculate the SHA256 of the whole file and append it
const shaDigest = await crypto.subtle.digest("SHA-256", output.slice(0, imageLength));
const digest = new Uint8Array(shaDigest);
output.set(digest, imageLength);
offset += 32;
}
if (this.padToSize) {
if (offset % this.padToSize !== 0) {
const padBy = this.padToSize - (offset % this.padToSize);
const padding = new Uint8Array(padBy);
padding.fill(0xff);
output.set(padding, offset);
offset += padBy;
}
}
return output;
}
loadExtendedHeader(data: Uint8Array, offset: number): void {
const view = new DataView(data.buffer, offset);
this.wpPin = view.getUint8(0);
const driveConfig = view.getUint8(1);
[this.clkDrv, this.qDrv] = this.splitByte(driveConfig);
const dConfig = view.getUint8(2);
[this.dDrv, this.csDrv] = this.splitByte(dConfig);
const hdConfig = view.getUint8(3);
[this.hdDrv, this.wpDrv] = this.splitByte(hdConfig);
this.chipId = view.getUint8(4);
if (this.ROM_LOADER.IMAGE_CHIP_ID !== undefined && this.chipId !== this.ROM_LOADER.IMAGE_CHIP_ID) {
console.warn(
`Unexpected chip id in image. Expected ${this.ROM_LOADER.IMAGE_CHIP_ID} but value was ${this.chipId}. ` +
"Is this image for a different chip model?",
);
}
this.minRev = view.getUint8(5);
this.minRevFull = view.getUint16(6, true);
this.maxRevFull = view.getUint16(8, true);
// Last byte is append_digest validation
const appendDigest = view.getUint8(15);
if (appendDigest === 0 || appendDigest === 1) {
this.appendDigest = appendDigest === 1;
} else {
throw new Error(`Invalid value for append_digest field (0x${appendDigest.toString(16)}). Should be 0 or 1.`);
}
}
saveExtendedHeader(output: Uint8Array, offset: number): void {
const headerBuffer = new ArrayBuffer(16);
const view = new DataView(headerBuffer);
view.setUint8(0, this.wpPin);
view.setUint8(1, this.joinByte(this.clkDrv, this.qDrv));
view.setUint8(2, this.joinByte(this.dDrv, this.csDrv));
view.setUint8(3, this.joinByte(this.hdDrv, this.wpDrv));
view.setUint8(4, this.ROM_LOADER.IMAGE_CHIP_ID ?? 0);
view.setUint8(5, this.minRev);
view.setUint16(6, this.minRevFull, true);
view.setUint16(8, this.maxRevFull, true);
for (let i = 9; i < 15; i++) {
view.setUint8(i, 0);
}
view.setUint8(15, this.appendDigest ? 1 : 0);
// Copy the header buffer to the output
output.set(new Uint8Array(headerBuffer), offset);
}
private splitByte(n: number): [number, number] {
return [n & 0x0f, (n >> 4) & 0x0f];
}
private joinByte(ln: number, hn: number): number {
return (ln & 0x0f) | ((hn & 0x0f) << 4);
}
private getAlignmentDataNeeded(segment: ImageSegment, currentOffset: number): number {
// Calculate alignment needed for segment
const alignPast = (segment.addr % this.IROM_ALIGN) - this.SEG_HEADER_LEN;
let padLen = this.IROM_ALIGN - (currentOffset % this.IROM_ALIGN) + alignPast;
if (padLen === 0 || padLen === this.IROM_ALIGN) {
return 0; // already aligned
}
padLen -= this.SEG_HEADER_LEN;
if (padLen < 0) {
padLen += this.IROM_ALIGN;
}
return padLen;
}
}