-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfirmware.js
More file actions
200 lines (170 loc) · 6.21 KB
/
firmware.js
File metadata and controls
200 lines (170 loc) · 6.21 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
const SETTINGS_VER = [0xAA, 0x03];
const LAYOUT_VER = [0xAA, 0x03];
const NUM_SHOWS = 20;
const FLASH_SIZE_MB = 2;
const EEPROM_START = (0x10000000 + (FLASH_SIZE_MB*0x100000) - 4096);
const SETTINGS_OFFSET = 0;
const LAYOUT_OFFSET_UF2 = 256;
const LAYOUT_OFFSET_HEX = (0x7E00 - 16);
const FIRMWARE_URL_UF2 = "firmware/firmware.uf2";
const FIRMWARE_URL_HEX = "firmware/firmware.hex";
const OFFSETS = {
magic1: 0,
magic2: 4,
flags: 8,
address: 12,
size: 16,
blockno: 20,
numblocks: 24,
family: 28,
data: 32,
magic3: 508,
}
class UF2Chunk {
constructor(data = null) {
this.header = new DataView(new ArrayBuffer(32));
this.trailer = new DataView(new ArrayBuffer(4));
if (data != null) {
let dataview = new DataView(data.buffer);
this.magic1 = dataview.getUint32(OFFSETS.magic1, true);
this.magic2 = dataview.getUint32(OFFSETS.magic2, true);
this.flags = dataview.getUint32(OFFSETS.flags, true);
this.address = dataview.getUint32(OFFSETS.address, true);
this.size = dataview.getUint32(OFFSETS.size, true);
this.blockno = dataview.getUint32(OFFSETS.blockno, true);
this.numblocks = dataview.getUint32(OFFSETS.numblocks, true);
this.family = dataview.getUint32(OFFSETS.family, true);
this.magic3 = dataview.getUint32(OFFSETS.magic3, true);
this.data = data.slice(OFFSETS.data, OFFSETS.data + 476);
} else {
this.magic1 = 0x0A324655;
this.magic2 = 0x9E5D5157;
this.flags = 0x2000;
this.address = 0x0;
this.size = 256;
this.blockno = 0;
this.numblocks = 1;
this.family = 0xE48BFF56;
this.magic3 = 0x0AB16F30;
this.data = new Uint8Array(476);
}
}
getBlobData() {
// offset value little endian
this.header.setUint32(OFFSETS.magic1, this.magic1, true);
this.header.setUint32(OFFSETS.magic2, this.magic2, true);
this.header.setUint32(OFFSETS.flags, this.flags, true);
this.header.setUint32(OFFSETS.address, this.address, true);
this.header.setUint32(OFFSETS.size, this.size, true);
this.header.setUint32(OFFSETS.blockno, this.blockno, true);
this.header.setUint32(OFFSETS.numblocks, this.numblocks, true);
this.header.setUint32(OFFSETS.family, this.family, true);
this.trailer.setUint32(0, this.magic3, true);
return [this.header.buffer, this.data, this.trailer.buffer];
}
}
class Layout {
constructor(
wing, nose, fuse, tail, nav,
wingRev, noseRev, fuseRev,
tailRev, noseFuseJoined
) {
this.data = new Uint8Array([
LAYOUT_VER[1], LAYOUT_VER[0],
wing, nose, fuse, tail, nav,
wingRev, noseRev, fuseRev,
tailRev, noseFuseJoined
]);
}
}
class Settings {
constructor(shows = null) {
this.data = new Uint8Array(NUM_SHOWS + 3);
this.data.set([SETTINGS_VER[1], SETTINGS_VER[0]], 0);
if (shows != null) {
this.data.set(shows, 2);
}
}
}
async function generateHex(layout, fullFirmware) {
firmware = "";
if (fullFirmware) {
let response = await fetch(FIRMWARE_URL_HEX);
let text = await response.text();
firmware = text.substring(0, text.lastIndexOf(":"));
}
let data = [layout.data.length, ((LAYOUT_OFFSET_HEX >> 8) & 0xff), (LAYOUT_OFFSET_HEX & 0xff), 0];
data = data.concat(Array.from(layout.data));
let sum = data.reduce((a, b) => a + b, 0);
data.push((~sum & 0xff) + 1);
let output = ":";
data.forEach((e) => output += e.toString(16).toUpperCase().padStart(2, "0"));
firmware += output + "\r\n:00000001FF\r\n";
return [firmware];
}
async function generateUF2(layout, settings, fullFirmware) {
let layoutChunk = new UF2Chunk();
layoutChunk.address = EEPROM_START + LAYOUT_OFFSET_UF2;
layoutChunk.data.set(layout.data, 0);
let settingsChunk = new UF2Chunk();
settingsChunk.address = EEPROM_START + SETTINGS_OFFSET;
settingsChunk.data.set(settings.data, 0);
let firmware = [];
let numBlocks = 2;
if (fullFirmware) {
let response = await fetch(FIRMWARE_URL_UF2);
let data = new Uint8Array(await response.arrayBuffer());
let chunk = new UF2Chunk(data.slice(0, 512));
numBlocks += chunk.numblocks;
chunk.numblocks = numBlocks;
firmware.push(...chunk.getBlobData());
for (let i = 512; i < data.length; i += 512) {
let chunk = new UF2Chunk(data.slice(i, i+512));
chunk.numblocks = numBlocks;
firmware.push(...chunk.getBlobData());
}
}
layoutChunk.blockno = numBlocks - 2;
layoutChunk.numblocks = numBlocks;
settingsChunk.blockno = numBlocks - 1;
settingsChunk.numblocks = numBlocks;
firmware.push(...layoutChunk.getBlobData());
firmware.push(...settingsChunk.getBlobData());
return firmware;
}
async function startDownload(layout, settings, firmwareType, version) {
let link = document.createElement("a");
let blob;
switch (firmwareType) {
case "v2Full":
link.download = "firmware-config_v" + version + ".uf2";
blob = new Blob(await generateUF2(layout, settings, true), {type: "application/octet-stream"});
link.href = URL.createObjectURL(blob);
break;
case "v2Config":
link.download = "config_v" + version + ".uf2";
blob = new Blob(await generateUF2(layout, settings, false), {type: "application/octet-stream"});
link.href = URL.createObjectURL(blob);
break;
case "v2Firmware":
link.download = "firmware_v" + version + ".uf2";
link.href = FIRMWARE_URL_UF2;
break;
case "v1Full":
link.download = "firmware-config_v" + version + ".hex";
blob = new Blob(await generateHex(layout, true), {type: "text/plain"});
link.href = URL.createObjectURL(blob);
break;
case "v1Config":
link.download = "config_v" + version + ".hex";
blob = new Blob(await generateHex(layout, false), {type: "text/plain"});
link.href = URL.createObjectURL(blob);
break;
case "v1Firmware":
link.download = "firmware_v" + version + ".hex";
link.href = FIRMWARE_URL_HEX;
break;
}
link.click();
URL.revokeObjectURL(blob);
}