-
Notifications
You must be signed in to change notification settings - Fork 0
/
wchisp.js
492 lines (430 loc) · 15.7 KB
/
wchisp.js
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
/*******************************************************************************
*
* WCH RISC-V Microcontroller Web Serial ISP
* Copyright (c) 2024 Basil Hussain
*
* This program is free software: you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License as published by the Free
* Software Foundation, either version 3 of the License, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
* FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
* details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*
******************************************************************************/
import { IntelHexParser } from "./modules/parsers/intelhex.js";
import { SRecordParser } from "./modules/parsers/srecord.js";
import { ElfRiscVParser } from "./modules/parsers/elf.js";
import { Firmware } from "./modules/firmware.js";
import { Session } from "./modules/session.js";
import { DevicesDatabase } from "./modules/devices.js";
import { Logger } from "./modules/logger.js";
import { Formatter } from "./modules/util.js";
/******************************************************************************/
function clearHexListing() {
document.getElementById("fw_hex").replaceChildren();
}
function createHexListing(bytes, fileName) {
const container = document.getElementById("fw_hex");
const offset_max_digits = bytes.length.toString(16).length;
const groups_count = 8;
const row_size = groups_count * 2;
// console.time("createHexListing");
for(let i = 0; i < bytes.length; i += row_size) {
let data = "", text = "";
for(let j = 0; j < groups_count; j++) {
const n = i + (j * 2);
if(n < bytes.length) {
data += Formatter.hex(bytes[n], 2);
text += Formatter.printableText(bytes[n], "·");
}
if(n + 1 < bytes.length) {
data += Formatter.hex(bytes[n + 1], 2);
text += Formatter.printableText(bytes[n + 1], "·");
}
data += " ";
}
const offset = document.createElement("span");
offset.classList.add("o");
offset.textContent = "0x" + Formatter.hex(i, offset_max_digits);
const printable = document.createElement("span");
printable.classList.add("p");
printable.textContent = text;
const br = document.createElement("br");
container.append(offset, data.trimEnd(), printable, br);
}
// console.timeEnd("createHexListing");
document.getElementById("fw_name_val").textContent = fileName;
document.getElementById("fw_size_val").textContent = bytes.length.toLocaleString();
}
function configInputIds() {
const inputIds = [
"cfg_rdpr", "cfg_user", "cfg_data0", "cfg_data1",
"cfg_wrpr0", "cfg_wrpr1", "cfg_wrpr2", "cfg_wrpr3"
];
return inputIds;
}
function populateConfig(config) {
if("optionBytes" in config) {
configInputIds().forEach((id, idx) => {
document.getElementById(id).value = "0x" + Formatter.hex(config.optionBytes[idx], 2)
});
}
}
function getConfigIsValid() {
// Return whether all config input fields are valid according to their
// constraints, and that their value parses successfully as a hex string.
return configInputIds().every((id) => {
const input = document.getElementById(id);
return input.checkValidity() && !Number.isNaN(Number.parseInt(input.value, 16));
});
}
function getConfigBytes() {
if(!getConfigIsValid()) {
throw new Error("One or more configuration option byte values is missing or invalid");
}
return configInputIds().map((id) => Number.parseInt(document.getElementById(id).value, 16));
}
function setActionButtonsEnabled(enable, ids = null) {
const buttons = document.querySelectorAll("#actions > button");
const prevState = new Array(buttons.length);
buttons.forEach((btn, idx) => {
prevState[idx] = btn.disabled;
// Enable/disable either all buttons if only boolean arg given, or if
// additional arg array given, only buttons with those given ids.
if(!Array.isArray(ids) || ids.includes(btn.id)) {
btn.disabled = !enable;
}
});
return prevState;
}
function restoreActionButtonsEnabled(prevState) {
document.querySelectorAll("#actions > button").forEach((btn, idx) => {
btn.disabled = prevState[idx];
});
}
function checkFirmwareSize(fwSize, flashSize) {
if(fwSize > flashSize) {
window.alert(
"Currently loaded firmware file size is LARGER than device flash size!\n\n" +
"Firmware size: " + Formatter.byteSize(fwSize) + "\n" +
"Device flash size: " + Formatter.byteSize(flashSize)
);
setActionButtonsEnabled(false, ["flash_write", "flash_verify"]);
} else {
setActionButtonsEnabled(true, ["flash_write", "flash_verify"]);
}
}
function updateProgress(event) {
const bar = document.getElementById("progress_bar");
const pct = document.getElementById("progress_pct");
const icon = document.getElementById("progress_result");
// When either event value is null, make the progress bar 'indeterminate'
// by removing its 'value' attribute. Otherwise, set its value accordingly.
if(event.detail.increment === null || event.detail.total === null) {
bar.removeAttribute("value");
pct.textContent = "∞";
} else {
const val = Math.min(event.detail.increment / event.detail.total, 1.0);
bar.setAttribute("value", val.toString());
pct.textContent = Math.floor(val * 100).toString() + "%";
}
icon.classList.remove("failure", "success");
icon.removeAttribute("title");
}
function updateResult(success) {
const icon = document.getElementById("progress_result");
icon.classList.remove("failure", "success");
icon.classList.add(success ? "success" : "failure");
icon.setAttribute("title", "Operation " + (success ? "succeeded" : "failed"));
if(!success) window.alert(
"Operation failed!\n\n" +
"See log for details."
);
}
function logMessage(msg, date, levelName, levelShortName) {
const log = document.getElementById("log");
const debug = document.getElementById("log_debug");
if(levelName !== "Debug" || (debug.checked && levelName === "Debug")) {
const line = document.createElement("p");
const time = document.createElement("span");
time.classList.add("time");
time.textContent = date.toLocaleTimeString([], {
hour: "2-digit",
minute: "2-digit",
second: "2-digit",
fractionalSecondDigits: 3
});
const level = document.createElement("span");
level.classList.add("level", levelName.toLowerCase());
level.textContent = levelShortName;
line.append("[", time, "][", level, "] ", msg);
log.appendChild(line);
log.scrollTop = log.scrollHeight;
}
}
function clearLog() {
document.getElementById("log").replaceChildren();
}
/******************************************************************************/
Firmware.addParser(["hex", "ihx"], IntelHexParser);
Firmware.addParser(["srec", "s19", "s28", "s37"], SRecordParser);
Firmware.addParser(["elf"], ElfRiscVParser);
const logger = new Logger(logMessage);
const devices = new DevicesDatabase();
const params = new URLSearchParams(window.location.search);
// Create promises that resolve when devices JSON and DOM content have finished
// loading.
const devicesLoaded = devices.fetchDevicesData("devices.json");
const contentLoaded = new Promise((resolve) => document.addEventListener("DOMContentLoaded", resolve, false));
Promise.all([contentLoaded, devicesLoaded])
.then(() => {
const deviceList = document.getElementById("device_list");
const fwUrl = document.getElementById("fw_url");
const fwUrlLoad = document.getElementById("fw_url_load");
const fwFile = document.getElementById("fw_file");
const fwHex = document.getElementById("fw_hex");
const configRead = document.getElementById("config_read");
const configWrite = document.getElementById("config_write");
const flashWrite = document.getElementById("flash_write");
const flashVerify = document.getElementById("flash_verify");
const flashErase = document.getElementById("flash_erase");
const logClear = document.getElementById("log_clear");
let device, firmware;
devices.populateDeviceList(deviceList);
device = devices.findDeviceByIndex(deviceList.value);
fwUrl.addEventListener("input", (event) => {
fwUrlLoad.disabled = !event.target.validity.valid;
});
fwUrl.addEventListener("keydown", (event) => {
if(event.key == "Enter" && event.target.validity.valid) {
fwUrlLoad.dispatchEvent(new Event("click"));
}
});
fwUrlLoad.addEventListener("click", (event) => {
clearHexListing();
Firmware.fromUrl(fwUrl.value)
.then((fw) => {
logger.info("Loaded " + fw.format + " firmware file from \"" + fwUrl.value + "\"");
fw.fillToEndOfSegment(1024);
createHexListing(fw.bytes, fw.fileName);
checkFirmwareSize(fw.size, device["flash"]["size"]);
firmware = fw;
})
.catch((err) => {
logger.error("Failed to load firmware from URL \"" + fwUrl.value + "\"");
logger.error(err.message);
window.alert(
"Failed to load firmware from URL \"" + fwUrl.value + "\".\n\n" +
"See log for details."
);
firmware = undefined;
setActionButtonsEnabled(false, ["flash_write", "flash_verify"]);
});
});
fwFile.addEventListener("change", (event) => {
if(fwFile.files.length > 0) {
clearHexListing();
Firmware.fromFile(fwFile.files[0])
.then((fw) => {
logger.info("Loaded " + fw.format + " firmware file from \"" + fwFile.files[0].name + "\"");
fw.fillToEndOfSegment(1024);
createHexListing(fw.bytes, fw.fileName);
checkFirmwareSize(fw.size, device["flash"]["size"]);
firmware = fw;
})
.catch((err) => {
logger.error("Failed to load firmware from file \"" + fwFile.files[0].name + "\"");
logger.error(err.message);
window.alert(
"Failed to load firmware from file \"" + fwFile.files[0].name + "\".\n\n" +
"See log for details."
);
firmware = undefined;
setActionButtonsEnabled(false, ["flash_write", "flash_verify"]);
});
}
});
fwHex.addEventListener("dragover", (event) => {
event.dataTransfer.dropEffect = (event.dataTransfer.types.includes("Files") ? "copy" : "none");
event.preventDefault();
});
fwHex.addEventListener("drop", (event) => {
if(event.dataTransfer.types.includes("Files") && event.dataTransfer.files.length > 0) {
// Assign the dropped file(s) to file input and manually trigger
// its change event.
fwFile.files = event.dataTransfer.files;
fwFile.dispatchEvent(new Event("change"));
}
event.preventDefault();
});
deviceList.addEventListener("change", (event) => {
device = devices.findDeviceByIndex(deviceList.value);
logger.info(
"Selected device changed to: " +
device["name"] + " (" + device["package"] + ", " + Formatter.byteSize(device["flash"]["size"]) + " flash)"
);
if(firmware !== undefined) {
checkFirmwareSize(firmware.size, device["flash"]["size"]);
}
});
[
"cfg_rdpr", "cfg_user", "cfg_data0", "cfg_data1",
"cfg_wrpr0", "cfg_wrpr1", "cfg_wrpr2", "cfg_wrpr3"
].forEach((id) => {
document.getElementById(id).addEventListener("input", (event) => {
setActionButtonsEnabled(getConfigIsValid(), ["config_write"]);
});
});
configRead.addEventListener("click", (event) => {
const btnState = setActionButtonsEnabled(false);
let success = true;
const sess = new Session(device["variant"], device["type"]);
sess.setLogger(logger);
sess.addEventListener("progress", updateProgress);
sess.start()
.then(() => sess.identify())
.then(() => sess.configRead())
.then((config) => {
populateConfig(config);
return sess.reset(true);
})
.catch((err) => {
logger.error(err.message);
success = false;
})
.finally(() => {
sess.end();
updateResult(success);
restoreActionButtonsEnabled(btnState);
setActionButtonsEnabled(getConfigIsValid(), ["config_write"]);
});
});
configWrite.addEventListener("click", (event) => {
const btnState = setActionButtonsEnabled(false);
let success = true;
const sess = new Session(device["variant"], device["type"]);
sess.setLogger(logger);
sess.addEventListener("progress", updateProgress);
sess.start()
.then(() => sess.identify())
.then(() => sess.configRead())
.then(() => sess.configWrite(getConfigBytes()))
.then(() => sess.reset(true))
.catch((err) => {
logger.error(err.message);
success = false;
})
.finally(() => {
sess.end();
updateResult(success);
restoreActionButtonsEnabled(btnState);
});
});
flashWrite.addEventListener("click", (event) => {
const btnState = setActionButtonsEnabled(false);
let success = true;
const sess = new Session(device["variant"], device["type"]);
sess.setLogger(logger);
sess.addEventListener("progress", updateProgress);
sess.start()
.then(() => sess.identify())
.then(() => sess.configRead())
.then(() => sess.keyGenerate())
.then(() => sess.flashErase(firmware.getSectorCount(1024)))
.then(() => sess.flashWrite(firmware.bytes))
.then(() => sess.keyGenerate())
.then(() => sess.flashVerify(firmware.bytes))
.then(() => sess.reset(true))
.catch((err) => {
logger.error(err.message);
success = false;
})
.finally(() => {
sess.end();
updateResult(success);
restoreActionButtonsEnabled(btnState);
});
});
flashVerify.addEventListener("click", (event) => {
const btnState = setActionButtonsEnabled(false);
let success = true;
const sess = new Session(device["variant"], device["type"]);
sess.setLogger(logger);
sess.addEventListener("progress", updateProgress);
sess.start()
.then(() => sess.identify())
.then(() => sess.configRead())
.then(() => sess.keyGenerate())
.then(() => sess.flashVerify(firmware.bytes))
.then(() => sess.reset(true))
.catch((err) => {
logger.error(err.message);
success = false;
})
.finally(() => {
sess.end();
updateResult(success);
restoreActionButtonsEnabled(btnState);
});
});
flashErase.addEventListener("click", (event) => {
if(window.confirm(
"Are you sure you want to ERASE the device?\n\n" +
"This will destroy ALL data in the user application flash!"
)) {
const btnState = setActionButtonsEnabled(false);
let success = true;
const sess = new Session(device["variant"], device["type"]);
sess.setLogger(logger);
sess.addEventListener("progress", updateProgress);
sess.start()
.then(() => sess.identify())
.then(() => sess.configRead())
.then(() => sess.flashErase(Math.ceil(device["flash"]["size"] / 1024)))
.then(() => sess.reset(true))
.catch((err) => {
logger.error(err.message);
success = false;
})
.finally(() => {
sess.end();
updateResult(success);
restoreActionButtonsEnabled(btnState);
});
}
});
logClear.addEventListener("click", (event) => {
clearLog();
});
// When a device name was given in URL parameter, find it and
// automatically select it in the list.
if(params.has("dev")) {
const idx = devices.findDeviceIndexByName(params.get("dev"));
if(idx) {
deviceList.value = idx;
deviceList.dispatchEvent(new Event("change"));
} else {
throw new Error("Couldn't find device with name \"" + params.get("dev") + "\"");
}
}
// When a firmware file URL was given in URL parameter, load it.
if(params.has("fw")) {
document.getElementById("fw_tab_url").checked = true;
fwUrl.value = params.get("fw");
fwUrlLoad.disabled = false;
fwUrlLoad.dispatchEvent(new Event("click"));
}
})
.catch((err) => {
console.error(err);
logger.error(err.message);
if(err.cause instanceof Error) {
logger.error(err.cause.message);
}
});