-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathfirehose.js
More file actions
348 lines (315 loc) · 10.4 KB
/
firehose.js
File metadata and controls
348 lines (315 loc) · 10.4 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
import * as Sparse from "./sparse"
import { concatUint8Array, containsBytes, compareStringToBytes, sleep, readBlobAsBuffer } from "./utils"
import { xmlParser } from "./xmlParser"
/**
* Progress callback
*
* @callback progressCallback
* @param {number} progress
* @returns {void}
*/
class response {
/**
* @param {boolean} resp
* @param {Uint8Array} data
* @param {string|undefined} [error]
* @param {string[]|undefined} [log]
*/
constructor(resp, data, error, log) {
this.resp = resp;
this.data = data;
this.error = error;
this.log = log;
}
}
class cfg {
constructor() {
this.ZLPAwareHost = 1;
this.SkipStorageInit = 0;
this.SkipWrite = 0;
this.MaxPayloadSizeToTargetInBytes = 1048576;
this.MaxPayloadSizeFromTargetInBytes = 4096;
this.MaxXMLSizeInBytes = 4096;
this.bit64 = true;
this.SECTOR_SIZE_IN_BYTES = 4096;
this.MemoryName = "UFS";
this.maxlun = 6;
}
}
export class Firehose {
/**
* @param {serialClass|usbClass} cdc
*/
constructor(cdc) {
this.cdc = cdc;
this.xml = new xmlParser();
this.cfg = new cfg();
/** @type {number[]} */
this.luns = [];
}
/**
* @param {string} command
* @param {boolean} [wait=true]
* @returns {Promise<response>}
*/
async xmlSend(command, wait = true) {
// FIXME: warn if command is shortened
const dataToSend = new TextEncoder().encode(command).slice(0, this.cfg.MaxXMLSizeInBytes);
await this.cdc.write(dataToSend, wait);
let rData = new Uint8Array();
let counter = 0;
const timeout = 3;
while (!(containsBytes("<response value", rData))) {
let tmp = await this.cdc.read();
if (compareStringToBytes("", tmp)) {
counter += 1;
await sleep(50);
if (counter > timeout) {
break;
}
}
rData = concatUint8Array([rData, tmp]);
}
const resp = this.xml.getResponse(rData);
const status = !("value" in resp) || resp.value === "ACK" || resp.value === "true";
if ("rawmode" in resp) {
if (resp.rawmode === "false") {
const log = this.xml.getLog(rData);
return new response(status, rData, "", log)
}
} else {
if (status) {
if (containsBytes("log value=", rData)) {
const log = this.xml.getLog(rData);
return new response(status, rData, "", log);
}
return new response(status, rData);
}
}
return new response(true, rData);
}
/**
* @returns {Promise<boolean>}
*/
async configure() {
const connectCmd = `<?xml version="1.0" encoding="UTF-8" ?><data>` +
`<configure MemoryName="${this.cfg.MemoryName}" ` +
`Verbose="0" ` +
`AlwaysValidate="0" ` +
`MaxDigestTableSizeInBytes="2048" ` +
`MaxPayloadSizeToTargetInBytes="${this.cfg.MaxPayloadSizeToTargetInBytes}" ` +
`ZLPAwareHost="${this.cfg.ZLPAwareHost}" ` +
`SkipStorageInit="${this.cfg.SkipStorageInit}" ` +
`SkipWrite="${this.cfg.SkipWrite}"/>` +
`</data>`
await this.xmlSend(connectCmd, false);
this.luns = Array.from({length: this.cfg.maxlun}, (x, i) => i);
return true;
}
/**
* @param {number} physicalPartitionNumber
* @param {number} startSector
* @param {number} numPartitionSectors
* @returns {Promise<response>}
*/
async cmdReadBuffer(physicalPartitionNumber, startSector, numPartitionSectors) {
const data = `<?xml version="1.0" ?><data><read SECTOR_SIZE_IN_BYTES="${this.cfg.SECTOR_SIZE_IN_BYTES}"` +
` num_partition_sectors="${numPartitionSectors}"` +
` physical_partition_number="${physicalPartitionNumber}"` +
` start_sector="${startSector}"/>\n</data>`
let rsp = await this.xmlSend(data);
let resData = new Uint8Array();
if (!rsp.resp) {
return rsp;
} else {
let bytesToRead = this.cfg.SECTOR_SIZE_IN_BYTES * numPartitionSectors;
while (bytesToRead > 0) {
let tmp = await this.cdc.read(Math.min(this.cdc.maxSize, bytesToRead));
const size = tmp.length;
bytesToRead -= size;
resData = concatUint8Array([resData, tmp]);
}
const wd = await this.waitForData();
const info = this.xml.getLog(wd);
rsp = this.xml.getResponse(wd);
if ("value" in rsp) {
if (rsp.value !== "ACK") {
return new response(false, resData, info);
} else if ("rawmode" in rsp) {
if (rsp.rawmode === "false") {
return new response(true, resData);
}
}
} else {
console.error("Failed read buffer");
return new response(false, resData, rsp[2]);
}
}
const resp = rsp.value === "ACK";
return new response(resp, resData, rsp[2]);
}
/**
* @returns {Promise<Uint8Array>}
*/
async waitForData() {
let tmp = new Uint8Array();
let timeout = 0;
while (!containsBytes("response value", tmp)) {
let res = await this.cdc.read();
if (compareStringToBytes("", res)) {
timeout += 1;
if (timeout === 4) {
break;
}
await sleep(20);
}
tmp = concatUint8Array([tmp, res]);
}
return tmp;
}
/**
* @param {number} physicalPartitionNumber
* @param {number} startSector
* @param {Blob} blob
* @param {progressCallback|undefined} [onProgress]
* @returns {Promise<boolean>}
*/
async cmdProgram(physicalPartitionNumber, startSector, blob, onProgress = undefined) {
let total = blob.size;
let sparseformat = false;
const sparseHeader = await Sparse.parseFileHeader(blob.slice(0, Sparse.FILE_HEADER_SIZE));
if (sparseHeader !== null) {
sparseformat = true;
total = await Sparse.getSparseRealSize(blob, sparseHeader);
}
let numPartitionSectors = Math.floor(total / this.cfg.SECTOR_SIZE_IN_BYTES);
if (total % this.cfg.SECTOR_SIZE_IN_BYTES !== 0) {
numPartitionSectors += 1;
}
const data = `<?xml version="1.0" ?><data>\n` +
`<program SECTOR_SIZE_IN_BYTES="${this.cfg.SECTOR_SIZE_IN_BYTES}"` +
` num_partition_sectors="${numPartitionSectors}"` +
` physical_partition_number="${physicalPartitionNumber}"` +
` start_sector="${startSector}" />\n</data>`;
let i = 0;
let bytesWritten = 0;
const rsp = await this.xmlSend(data);
if (rsp.resp) {
for await (let split of Sparse.splitBlob(blob)) {
let offset = 0;
let bytesToWriteSplit = split.size;
while (bytesToWriteSplit > 0) {
const wlen = Math.min(bytesToWriteSplit, this.cfg.MaxPayloadSizeToTargetInBytes);
let wdata = new Uint8Array(await readBlobAsBuffer(split.slice(offset, offset + wlen)));
if (wlen % this.cfg.SECTOR_SIZE_IN_BYTES !== 0) {
const fillLen = (Math.floor(wlen/this.cfg.SECTOR_SIZE_IN_BYTES) * this.cfg.SECTOR_SIZE_IN_BYTES) +
this.cfg.SECTOR_SIZE_IN_BYTES;
const fillArray = new Uint8Array(fillLen-wlen).fill(0x00);
wdata = concatUint8Array([wdata, fillArray]);
}
await this.cdc.write(wdata);
await this.cdc.write(new Uint8Array(0), true);
offset += wlen;
bytesWritten += wlen;
bytesToWriteSplit -= wlen;
// Need this for sparse image when the data.length < MaxPayloadSizeToTargetInBytes
// Add ~2.4s to total flash time
if (sparseformat && bytesWritten < total) {
await this.cdc.write(new Uint8Array(0), true);
}
if (i % 10 === 0) {
onProgress?.(bytesWritten / total);
}
i += 1;
}
}
const wd = await this.waitForData();
const response = this.xml.getResponse(wd);
if ("value" in response) {
if (response.value !== "ACK") {
return false;
}
} else {
return false;
}
}
onProgress?.(1.0);
return true;
}
/**
* @param {number} physicalPartitionNumber
* @param {number} startSector
* @param {number} numPartitionSectors
* @returns {Promise<boolean>}
*/
async cmdErase(physicalPartitionNumber, startSector, numPartitionSectors) {
const data = `<?xml version="1.0" ?><data>\n` +
`<program SECTOR_SIZE_IN_BYTES="${this.cfg.SECTOR_SIZE_IN_BYTES}"` +
` num_partition_sectors="${numPartitionSectors}"` +
` physical_partition_number="${physicalPartitionNumber}"` +
` start_sector="${startSector}" />\n</data>`;
const rsp = await this.xmlSend(data);
let bytesToWrite = this.cfg.SECTOR_SIZE_IN_BYTES * numPartitionSectors;
const empty = new Uint8Array(this.cfg.MaxPayloadSizeToTargetInBytes).fill(0);
if (rsp.resp) {
while (bytesToWrite > 0) {
let wlen = Math.min(bytesToWrite, this.cfg.MaxPayloadSizeToTargetInBytes);
await this.cdc.write(empty.slice(0, wlen));
bytesToWrite -= wlen;
await this.cdc.write(new Uint8Array(0));
}
const res = await this.waitForData();
const response = this.xml.getResponse(res);
if ("value" in response) {
if (response.value !== "ACK") {
throw "Failed to erase: NAK";
}
} else {
throw "Failed to erase no return value";
}
}
return true;
}
/**
* @param {number} lun
* @returns {Promise<boolean>}
*/
async cmdSetBootLunId(lun) {
const data = `<?xml version="1.0" ?><data>\n<setbootablestoragedrive value="${lun}" /></data>`;
const val = await this.xmlSend(data);
if (val.resp) {
console.log(`Successfully set bootID to lun ${lun}`);
return true;
} else {
throw `Firehose - Failed to set boot lun ${lun}`;
}
}
/**
* @returns {Promise<boolean>}
*/
async cmdReset() {
let data = '<?xml version="1.0" ?><data><power value="reset"/></data>';
let val = await this.xmlSend(data);
if (val.resp) {
console.log("Reset succeeded");
// Drain log buffer
try {
await this.waitForData();
} catch {
// Ignore any errors
}
return true;
} else {
throw "Firehose - Reset failed";
}
}
/**
* @returns {Promise<string[]>}
*/
async cmdGetStorageInfo() {
const data = '<?xml version="1.0" ?><data><getstorageinfo physical_partition_number="0" /></data>';
const resp = await this.xmlSend(data);
if (!resp.resp || !resp.log) throw new Error("Failed to get storage info", { cause: resp.error });
return resp.log;
}
}