@sensebox/flash-tool 1.1.0 → 1.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.cjs CHANGED
@@ -9,6 +9,132 @@ var _proptypes = require('prop-types'); var _proptypes2 = _interopRequireDefault
9
9
  // src/react/useFlashDevice.js
10
10
 
11
11
 
12
+ // src/core/md5.js
13
+ var S = [
14
+ 7,
15
+ 12,
16
+ 17,
17
+ 22,
18
+ 7,
19
+ 12,
20
+ 17,
21
+ 22,
22
+ 7,
23
+ 12,
24
+ 17,
25
+ 22,
26
+ 7,
27
+ 12,
28
+ 17,
29
+ 22,
30
+ 5,
31
+ 9,
32
+ 14,
33
+ 20,
34
+ 5,
35
+ 9,
36
+ 14,
37
+ 20,
38
+ 5,
39
+ 9,
40
+ 14,
41
+ 20,
42
+ 5,
43
+ 9,
44
+ 14,
45
+ 20,
46
+ 4,
47
+ 11,
48
+ 16,
49
+ 23,
50
+ 4,
51
+ 11,
52
+ 16,
53
+ 23,
54
+ 4,
55
+ 11,
56
+ 16,
57
+ 23,
58
+ 4,
59
+ 11,
60
+ 16,
61
+ 23,
62
+ 6,
63
+ 10,
64
+ 15,
65
+ 21,
66
+ 6,
67
+ 10,
68
+ 15,
69
+ 21,
70
+ 6,
71
+ 10,
72
+ 15,
73
+ 21,
74
+ 6,
75
+ 10,
76
+ 15,
77
+ 21
78
+ ];
79
+ var K = Array.from(
80
+ { length: 64 },
81
+ (_, i) => Math.floor(Math.abs(Math.sin(i + 1)) * 2 ** 32) >>> 0
82
+ );
83
+ function md5Hex(data) {
84
+ const length = data.length;
85
+ const paddedLength = (length + 8 >> 6) + 1 << 6;
86
+ const buffer = new Uint8Array(paddedLength);
87
+ buffer.set(data);
88
+ buffer[length] = 128;
89
+ const view = new DataView(buffer.buffer);
90
+ view.setUint32(paddedLength - 8, length << 3 >>> 0, true);
91
+ view.setUint32(paddedLength - 4, Math.floor(length / 536870912), true);
92
+ let a0 = 1732584193;
93
+ let b0 = 4023233417;
94
+ let c0 = 2562383102;
95
+ let d0 = 271733878;
96
+ const M = new Uint32Array(16);
97
+ for (let offset = 0; offset < paddedLength; offset += 64) {
98
+ for (let j = 0; j < 16; j++) M[j] = view.getUint32(offset + j * 4, true);
99
+ let a = a0;
100
+ let b = b0;
101
+ let c = c0;
102
+ let d = d0;
103
+ for (let i = 0; i < 64; i++) {
104
+ let f;
105
+ let g;
106
+ if (i < 16) {
107
+ f = b & c | ~b & d;
108
+ g = i;
109
+ } else if (i < 32) {
110
+ f = d & b | ~d & c;
111
+ g = 5 * i + 1 & 15;
112
+ } else if (i < 48) {
113
+ f = b ^ c ^ d;
114
+ g = 3 * i + 5 & 15;
115
+ } else {
116
+ f = c ^ (b | ~d);
117
+ g = 7 * i & 15;
118
+ }
119
+ const sum = a + f + K[i] + M[g] >>> 0;
120
+ a = d;
121
+ d = c;
122
+ c = b;
123
+ b = b + (sum << S[i] | sum >>> 32 - S[i]) >>> 0;
124
+ }
125
+ a0 = a0 + a >>> 0;
126
+ b0 = b0 + b >>> 0;
127
+ c0 = c0 + c >>> 0;
128
+ d0 = d0 + d >>> 0;
129
+ }
130
+ const out = new DataView(new ArrayBuffer(16));
131
+ [a0, b0, c0, d0].forEach((word, i) => out.setUint32(i * 4, word, true));
132
+ return Array.from(
133
+ new Uint8Array(out.buffer),
134
+ (byte) => byte.toString(16).padStart(2, "0")
135
+ ).join("");
136
+ }
137
+
12
138
  // src/core/esptool.js
13
139
  var sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
14
140
  var DEFAULT_USB_VENDOR_ID = 12346;
@@ -189,6 +315,9 @@ async function flashBinary({
189
315
  `Expected ${expectedChip}, but connected to ${loader.chip.CHIP_NAME}. Check the selected board.`
190
316
  );
191
317
  }
318
+ const sizes = fileArray.map((file) => file.data.length);
319
+ const totalSize = sizes.reduce((sum, size) => sum + size, 0);
320
+ const doneBefore = (index) => sizes.slice(0, index).reduce((sum, size) => sum + size, 0);
192
321
  await loader.writeFlash({
193
322
  fileArray,
194
323
  flashSize,
@@ -196,9 +325,14 @@ async function flashBinary({
196
325
  flashFreq,
197
326
  eraseAll,
198
327
  compress: true,
199
- reportProgress: (_fileIndex, written, total) => {
200
- if (total > 0) {
201
- onProgress == null ? void 0 : onProgress(Math.round(written / total * 100));
328
+ // Read the MD5 back from the chip after each file, so a write that
329
+ // did not stick fails the upload instead of reporting success. Only
330
+ // with the stub: esptool-js parses the ROM's MD5 reply differently.
331
+ calculateMD5Hash: loader.IS_STUB ? md5Hex : void 0,
332
+ reportProgress: (fileIndex, written, total) => {
333
+ if (total > 0 && totalSize > 0) {
334
+ const done = doneBefore(fileIndex) + written / total * sizes[fileIndex];
335
+ onProgress == null ? void 0 : onProgress(Math.round(done / totalSize * 100));
202
336
  }
203
337
  }
204
338
  });
@@ -218,6 +352,9 @@ var esp = {
218
352
  vendorId: ESPRESSIF_VENDOR_ID,
219
353
  bootloaderProductId: 2,
220
354
  address: 65536,
355
+ // otadata partition in the senseBox TinyUF2 partition tables (MCU-S2 and
356
+ // MCU Eye). Rewritten on every sketch upload to select ota_0, see flash.js.
357
+ otadataAddress: 57344,
221
358
  flashMode: "dio",
222
359
  flashFreq: "80m",
223
360
  backend: "esp"
@@ -471,13 +608,28 @@ async function flashSamdBinary({ port, firmware, onLog, onProgress }) {
471
608
  }
472
609
 
473
610
  // src/core/flash.js
611
+ var OTADATA_SIZE = 8192;
612
+ function createOtadataBootOta0() {
613
+ const otadata = new Uint8Array(OTADATA_SIZE).fill(255);
614
+ otadata.set([1, 0, 0, 0], 0);
615
+ otadata.set([154, 152, 67, 71], 28);
616
+ return otadata;
617
+ }
474
618
  function flashWithProfile({ profile, port, binary, options = {}, onLog, onProgress }, deps = { flashBinary, flashSamdBinary }) {
475
619
  if (profile.backend === "samd") {
476
620
  return deps.flashSamdBinary({ port, firmware: binary, onLog, onProgress });
477
621
  }
622
+ const address = _nullishCoalesce(options.address, () => ( profile.address));
623
+ const fileArray = [{ data: binary, address }];
624
+ if (options.writeOtadata !== false && profile.otadataAddress !== void 0 && address === profile.address) {
625
+ fileArray.push({
626
+ data: createOtadataBootOta0(),
627
+ address: profile.otadataAddress
628
+ });
629
+ }
478
630
  return deps.flashBinary({
479
631
  port,
480
- fileArray: [{ data: binary, address: _nullishCoalesce(options.address, () => ( profile.address)) }],
632
+ fileArray,
481
633
  baudrate: options.baudrate,
482
634
  flashMode: options.flashMode,
483
635
  flashFreq: options.flashFreq,