dsh-stm32 0.1.4 → 0.1.6

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.
Files changed (38) hide show
  1. package/README.md +517 -459
  2. package/README_EN.md +510 -453
  3. package/assets/device/h7/chips.json +831 -140
  4. package/assets/device/u5/chips.json +375 -64
  5. package/assets/manifest.json +4 -4
  6. package/docs/reading-guide.md +114 -16
  7. package/lib/commands/register.js +1 -1
  8. package/lib/data/cmsis-sources.js +136 -4
  9. package/lib/data/device-facts.js +14 -0
  10. package/lib/data/family.js +14 -14
  11. package/lib/data/firmware-packages.js +35 -14
  12. package/lib/data/firmware-pins.js +128 -0
  13. package/lib/data/search-roots.js +17 -3
  14. package/lib/data/serial-config.js +430 -0
  15. package/lib/engine/doctor.js +85 -8
  16. package/lib/engine/download.js +242 -0
  17. package/lib/engine/fetch.js +1 -1
  18. package/lib/engine/firmware-install.js +158 -35
  19. package/lib/engine/firmware-official.js +113 -65
  20. package/lib/engine/ioc-plan.js +28 -6
  21. package/lib/engine/msys2-install.js +237 -88
  22. package/lib/engine/probe.js +4 -2
  23. package/lib/engine/register-gpio.js +18 -1
  24. package/lib/engine/register-linker.js +25 -6
  25. package/lib/engine/register-template.js +25 -1
  26. package/lib/engine/serial.js +1696 -0
  27. package/lib/engine/tar.js +235 -3
  28. package/lib/index.js +12 -0
  29. package/lib/skill/stm32-workflow.js +2 -2
  30. package/lib/tools/assets.js +8 -3
  31. package/lib/tools/build.js +0 -1
  32. package/lib/tools/doctor.js +9 -3
  33. package/lib/tools/download.js +303 -0
  34. package/lib/tools/ioc-plan.js +17 -7
  35. package/lib/tools/project-create.js +1 -1
  36. package/lib/tools/serial.js +737 -0
  37. package/package.json +70 -70
  38. package/templates/serial/bridge.ps1 +477 -0
@@ -17,7 +17,7 @@
17
17
  * https://sw-center.st.com/packs/resource/firmware/stm32cube_fw_<库名后缀小写>_v<版本去点>.zip
18
18
  * 例:STM32Cube_FW_F1 + 1.8.7 → stm32cube_fw_f1_v187.zip(56,220,914 B)
19
19
  */
20
- import { closeSync, existsSync, mkdirSync, openSync, readFileSync, readSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
20
+ import { closeSync, existsSync, mkdirSync, openSync, readSync, readdirSync, rmSync, statSync, writeFileSync } from "node:fs";
21
21
  import { createWriteStream } from "node:fs";
22
22
  import path from "node:path";
23
23
  import { inflateRawSync } from "node:zlib";
@@ -142,12 +142,33 @@ export function crc32Of(bytes, seed = 0) {
142
142
  function hex32(value) {
143
143
  return "0x" + (value >>> 0).toString(16).padStart(8, "0");
144
144
  }
145
+ /** zip 结构签名与单条解压上限。 */
146
+ const ZIP_EOCD_SIG = 0x06054b50;
147
+ const ZIP_CENTRAL_SIG = 0x02014b50;
148
+ const ZIP_LOCAL_SIG = 0x04034b50;
149
+ /** 单条目解压上限:官方包里最大的 C 源远小于此,超过只可能是坏 zip / 压缩炸弹。 */
150
+ const ZIP_MAX_ENTRY_BYTES = 256 * 1024 * 1024;
151
+ /** 从 fd 的指定位置读满 len 字节;短读返回实际读到的(调用方判错)。 */
152
+ function readAt(fd, position, len) {
153
+ const out = Buffer.allocUnsafe(len);
154
+ let done = 0;
155
+ while (done < len) {
156
+ const n = readSync(fd, out, done, len - done, position + done);
157
+ if (n <= 0)
158
+ break;
159
+ done += n;
160
+ }
161
+ return done === len ? out : out.subarray(0, done);
162
+ }
145
163
  /**
146
164
  * 校验 zip:读中央目录 → 逐条解压 → 比 CRC-32。
147
165
  *
148
166
  * 为什么要全量比一遍而不是"能解开就行":ST 的 zip 有 300+ MB(WBA 388,603,994 B),
149
167
  * 代理/断流/磁盘满都能留一个"结构完好但内容缺一块"的文件,swmgr 解包时才炸,
150
168
  * 而那时用户已经等了几分钟、包目录还是半截。这里先自证一遍,坏在哪一条也说得清。
169
+ *
170
+ * 内存边界:只把中央目录(通常几百 KB)读进内存,压缩数据按条目一条条 readSync +
171
+ * inflate;峰值≈最大单条,不再整包 388 MB 进 Buffer。
151
172
  */
152
173
  export function verifyZipCrc(file, onProgress) {
153
174
  const base = { ok: false, entries: 0, checked: 0, mismatches: [], error: "" };
@@ -160,77 +181,104 @@ export function verifyZipCrc(file, onProgress) {
160
181
  }
161
182
  if (size < 22)
162
183
  return { ...base, error: "不是 zip:文件只有 " + String(size) + " B" };
163
- const buffer = readFileSync(file);
164
- // EOCD 在文件尾部,后面最多跟 65535 字节注释。
165
- const tailStart = Math.max(0, size - 65558);
166
- let eocd = -1;
167
- for (let i = size - 22; i >= tailStart; i -= 1) {
168
- if (buffer.readUInt32LE(i) === 0x06054b50) {
169
- const commentLen = buffer.readUInt16LE(i + 20);
170
- if (i + 22 + commentLen === size) {
171
- eocd = i;
172
- break;
173
- }
174
- }
184
+ let fd;
185
+ try {
186
+ fd = openSync(file, "r");
175
187
  }
176
- if (eocd === -1)
177
- return { ...base, error: "不是 zip:找不到中央目录结尾(EOCD)" };
178
- const total = buffer.readUInt16LE(eocd + 10);
179
- const cdOffset = buffer.readUInt32LE(eocd + 16);
180
- const mismatches = [];
181
- let offset = cdOffset;
182
- let checked = 0;
183
- for (let index = 0; index < total; index += 1) {
184
- if (buffer.readUInt32LE(offset) !== 0x02014b50) {
185
- return { ...base, entries: total, checked, mismatches, error: "中央目录第 " + String(index + 1) + " 条签名不对" };
186
- }
187
- const method = buffer.readUInt16LE(offset + 10);
188
- const expectedCrc = buffer.readUInt32LE(offset + 16);
189
- const compressedSize = buffer.readUInt32LE(offset + 20);
190
- const nameLen = buffer.readUInt16LE(offset + 28);
191
- const extraLen = buffer.readUInt16LE(offset + 30);
192
- const commentLen = buffer.readUInt16LE(offset + 32);
193
- const localOffset = buffer.readUInt32LE(offset + 42);
194
- const name = buffer.subarray(offset + 46, offset + 46 + nameLen).toString("utf8");
195
- offset += 46 + nameLen + extraLen + commentLen;
196
- if (buffer.readUInt32LE(localOffset) !== 0x04034b50) {
197
- return { ...base, entries: total, checked, mismatches, error: "条目 " + name + " 的本地头签名不对" };
188
+ catch (error) {
189
+ return { ...base, error: "打不开 zip:" + (error instanceof Error ? error.message : String(error)) };
190
+ }
191
+ try {
192
+ // EOCD 在文件尾部,后面最多跟 65535 字节注释。
193
+ const tailStart = Math.max(0, size - 65558);
194
+ const tail = readAt(fd, tailStart, size - tailStart);
195
+ let eocd = -1;
196
+ for (let i = tail.length - 22; i >= 0; i -= 1) {
197
+ if (tail.readUInt32LE(i) === ZIP_EOCD_SIG) {
198
+ const commentLen = tail.readUInt16LE(i + 20);
199
+ if (tailStart + i + 22 + commentLen === size) {
200
+ eocd = i;
201
+ break;
202
+ }
203
+ }
198
204
  }
199
- const localNameLen = buffer.readUInt16LE(localOffset + 26);
200
- const localExtraLen = buffer.readUInt16LE(localOffset + 28);
201
- const dataStart = localOffset + 30 + localNameLen + localExtraLen;
202
- const raw = buffer.subarray(dataStart, dataStart + compressedSize);
203
- let data;
204
- try {
205
- if (method === 0)
206
- data = raw;
207
- else if (method === 8)
208
- data = inflateRawSync(raw);
209
- else {
205
+ if (eocd === -1)
206
+ return { ...base, error: "不是 zip:找不到中央目录结尾(EOCD)" };
207
+ const total = tail.readUInt16LE(eocd + 10);
208
+ const cdSize = tail.readUInt32LE(eocd + 12);
209
+ const cdOffset = tail.readUInt32LE(eocd + 16);
210
+ if (cdOffset + cdSize > size)
211
+ return { ...base, entries: total, error: "中央目录越界:offset " + String(cdOffset) + " + " + String(cdSize) + " > " + String(size) };
212
+ const cd = readAt(fd, cdOffset, cdSize);
213
+ if (cd.length !== cdSize)
214
+ return { ...base, entries: total, error: "中央目录读不全:" + String(cd.length) + "/" + String(cdSize) + " B" };
215
+ const mismatches = [];
216
+ let offset = 0;
217
+ let checked = 0;
218
+ for (let index = 0; index < total; index += 1) {
219
+ if (offset + 46 > cd.length)
220
+ return { ...base, entries: total, checked, mismatches, error: "中央目录第 " + String(index + 1) + " 条越界" };
221
+ if (cd.readUInt32LE(offset) !== ZIP_CENTRAL_SIG) {
222
+ return { ...base, entries: total, checked, mismatches, error: "中央目录第 " + String(index + 1) + " 条签名不对" };
223
+ }
224
+ const method = cd.readUInt16LE(offset + 10);
225
+ const expectedCrc = cd.readUInt32LE(offset + 16);
226
+ const compressedSize = cd.readUInt32LE(offset + 20);
227
+ const nameLen = cd.readUInt16LE(offset + 28);
228
+ const extraLen = cd.readUInt16LE(offset + 30);
229
+ const commentLen = cd.readUInt16LE(offset + 32);
230
+ const localOffset = cd.readUInt32LE(offset + 42);
231
+ const name = cd.subarray(offset + 46, offset + 46 + nameLen).toString("utf8");
232
+ offset += 46 + nameLen + extraLen + commentLen;
233
+ if (method !== 0 && method !== 8) {
210
234
  // 其它算法(bzip2/lzma)官方包不用;不认识的条目跳过而不是判坏。
211
235
  continue;
212
236
  }
237
+ if (localOffset + 30 > size)
238
+ return { ...base, entries: total, checked, mismatches, error: "条目 " + name + " 的本地头越界" };
239
+ const localHeader = readAt(fd, localOffset, 30);
240
+ if (localHeader.length !== 30 || localHeader.readUInt32LE(0) !== ZIP_LOCAL_SIG) {
241
+ return { ...base, entries: total, checked, mismatches, error: "条目 " + name + " 的本地头签名不对" };
242
+ }
243
+ const localNameLen = localHeader.readUInt16LE(26);
244
+ const localExtraLen = localHeader.readUInt16LE(28);
245
+ const dataStart = localOffset + 30 + localNameLen + localExtraLen;
246
+ if (dataStart + compressedSize > size) {
247
+ return { ...base, entries: total, checked, mismatches, error: "条目 " + name + " 的数据越界:" + String(dataStart) + " + " + String(compressedSize) + " > " + String(size) };
248
+ }
249
+ // 先按 central 里的压缩长度读;解压上限再兜一层压缩炸弹。
250
+ const raw = readAt(fd, dataStart, compressedSize);
251
+ if (raw.length !== compressedSize) {
252
+ return { ...base, entries: total, checked, mismatches, error: "条目 " + name + " 的数据读不全:" + String(raw.length) + "/" + String(compressedSize) + " B" };
253
+ }
254
+ let data;
255
+ try {
256
+ data = method === 0 ? raw : inflateRawSync(raw, { maxOutputLength: ZIP_MAX_ENTRY_BYTES });
257
+ }
258
+ catch (error) {
259
+ return {
260
+ ...base,
261
+ entries: total,
262
+ checked,
263
+ mismatches,
264
+ error: "条目 " + name + " 解压失败:" + (error instanceof Error ? error.message : String(error)),
265
+ };
266
+ }
267
+ const actual = crc32Of(data);
268
+ checked += 1;
269
+ if (actual !== (expectedCrc >>> 0)) {
270
+ mismatches.push({ name, expected: hex32(expectedCrc), actual: hex32(actual) });
271
+ if (mismatches.length > 5)
272
+ mismatches.pop();
273
+ }
274
+ if (onProgress !== undefined && checked % 200 === 0)
275
+ onProgress(checked, total);
213
276
  }
214
- catch (error) {
215
- return {
216
- ...base,
217
- entries: total,
218
- checked,
219
- mismatches,
220
- error: "条目 " + name + " 解压失败:" + (error instanceof Error ? error.message : String(error)),
221
- };
222
- }
223
- const actual = crc32Of(data);
224
- checked += 1;
225
- if (actual !== (expectedCrc >>> 0)) {
226
- mismatches.push({ name, expected: hex32(expectedCrc), actual: hex32(actual) });
227
- if (mismatches.length > 5)
228
- mismatches.pop();
229
- }
230
- if (onProgress !== undefined && checked % 200 === 0)
231
- onProgress(checked, total);
277
+ return { ok: mismatches.length === 0, entries: total, checked, mismatches, error: "" };
278
+ }
279
+ finally {
280
+ closeSync(fd);
232
281
  }
233
- return { ok: mismatches.length === 0, entries: total, checked, mismatches, error: "" };
234
282
  }
235
283
  /**
236
284
  * 下载官方 zip。判据两条:HTTP 200 + 落盘字节数等于 Content-Length。
@@ -29,6 +29,26 @@ import { planClockDirect } from "./clock-direct.js";
29
29
  import { followDerivedClocks } from "./clock-derived.js";
30
30
  import { isKnownPin } from "../data/chips.js";
31
31
  import { capabilityGap, familyProfileOf, isF1 } from "../data/family.js";
32
+ /** 把 [{key,value}] 折成查找表:空 key 与重复 key 记错误,重复时后值覆盖前值。 */
33
+ function keyValueMap(pairs, label, errors) {
34
+ const map = new Map();
35
+ for (const [index, pair] of (pairs ?? []).entries()) {
36
+ if (pair === null || typeof pair !== "object") {
37
+ errors.push(label + "[" + String(index) + "] 不是 {key,value} 对象");
38
+ continue;
39
+ }
40
+ const key = typeof pair.key === "string" ? pair.key.trim() : "";
41
+ const value = typeof pair.value === "string" ? pair.value : "";
42
+ if (key === "") {
43
+ errors.push(label + "[" + String(index) + "] 的 key 是空串");
44
+ continue;
45
+ }
46
+ if (map.has(key))
47
+ errors.push(label + " 里的 key " + JSON.stringify(key) + " 重复,后面的值覆盖前面的");
48
+ map.set(key, value);
49
+ }
50
+ return map;
51
+ }
32
52
  export function sha256Hex(text) {
33
53
  return createHash("sha256").update(text, "utf8").digest("hex");
34
54
  }
@@ -908,9 +928,11 @@ function enablePeripheral(table, request, pinNames, notes, familyId) {
908
928
  }
909
929
  table.entries.set(renderIocKey("mcu.ip_nb"), String(slots));
910
930
  // 引脚:Mode + Signal + Locked。
911
- const overrodePins = request.pins !== undefined && Object.keys(request.pins).length > 0;
931
+ const pinOverrides = keyValueMap(request.pins, preset.name + ".pins", table.errors);
932
+ const paramOverrides = keyValueMap(request.parameters, preset.name + ".parameters", table.errors);
933
+ const overrodePins = pinOverrides.size > 0;
912
934
  for (const role of preset.pins) {
913
- const logical = (request.pins?.[role.role] ?? role.defaultPin).trim().toUpperCase();
935
+ const logical = (pinOverrides.get(role.role) ?? role.defaultPin).trim().toUpperCase();
914
936
  if (logical === "") {
915
937
  table.errors.push(preset.name + " 的引脚 " + role.role + " 被指定成空串");
916
938
  continue;
@@ -942,7 +964,7 @@ function enablePeripheral(table, request, pinNames, notes, familyId) {
942
964
  }
943
965
  // 逐字键(DMA 这类键名不受 <ip>.<名字> 约束):先写它们,再统一重算 Dma.RequestsNb。
944
966
  for (const raw of preset.rawEntries ?? []) {
945
- table.entries.set(raw.key, request.parameters?.[raw.key.replace("Dma.", "").replace(/\.0\./u, ".")] ?? raw.value);
967
+ table.entries.set(raw.key, paramOverrides.get(raw.key.replace("Dma.", "").replace(/\.0\./u, ".")) ?? raw.value);
946
968
  }
947
969
  // G1/G2:DMA 三件套。CubeMX 自己写的是
948
970
  // Dma.<IP>.<n>.RequestParameters=Instance,Direction,...,Priority(白名单,与 IPParameters 同机制:
@@ -1004,15 +1026,15 @@ function enablePeripheral(table, request, pinNames, notes, familyId) {
1004
1026
  const paramsKey = renderIocKey("periph.ip_parameters", { ip: preset.ip });
1005
1027
  const names = (table.entries.get(paramsKey) ?? "").split(",").map((item) => item.trim()).filter((item) => item !== "");
1006
1028
  for (const param of preset.parameters) {
1007
- table.entries.set(renderIocKey("periph.parameter", { ip: preset.ip, name: param.name }), request.parameters?.[param.name] ?? param.value);
1029
+ table.entries.set(renderIocKey("periph.parameter", { ip: preset.ip, name: param.name }), paramOverrides.get(param.name) ?? param.value);
1008
1030
  if (!names.includes(param.name))
1009
1031
  names.push(param.name);
1010
- if (overrodePins && request.parameters?.[param.name] === undefined && param.name.startsWith("Channel-")) {
1032
+ if (overrodePins && !paramOverrides.has(param.name) && param.name.startsWith("Channel-")) {
1011
1033
  table.warnings.push(preset.name + " 换了引脚,但 " + param.name + " 仍是 " + param.value +
1012
1034
  ":通道号必须与引脚一致,请一并用 parameters 改");
1013
1035
  }
1014
1036
  }
1015
- for (const [name, value] of Object.entries(request.parameters ?? {})) {
1037
+ for (const [name, value] of paramOverrides) {
1016
1038
  if (names.includes(name))
1017
1039
  continue;
1018
1040
  names.push(name);