67 lines
4.4 KiB
JavaScript
67 lines
4.4 KiB
JavaScript
/* 模块职责:把已有非空负载封装成示例使用的 I104 工程包,供基线登记或210上传。
|
|
* 调用方式:node scripts/make_upgrade_package.cjs --version N --input payload --output package。
|
|
* 版本、长度和CRC按小端写入16字节头,CRC只覆盖负载;此格式不提供签名或机型验证。
|
|
* 只创建新输出,不覆盖已有文件、不执行负载。写入失败可留下未完成包,不能继续上传。
|
|
* 只依赖Node内置模块;固定块读写,不把整个映像放入内存。 */
|
|
'use strict';
|
|
const fs = require('node:fs');
|
|
const path = require('node:path');
|
|
const MAX_PACKAGE = 16 * 1024 * 1024;
|
|
|
|
/* 参数按键值对处理,缺项、重复键和未知键都在创建输出前拒绝。
|
|
* 任一步抛出异常交末尾入口设置非零退出码;只有头、负载及fsync均成功才打印Created。 */
|
|
function main() {
|
|
const options = {};
|
|
for (let i = 2; i < process.argv.length; i += 2) {
|
|
const key = process.argv[i];
|
|
if (!['--version', '--input', '--output'].includes(key) || options[key] !== undefined || !process.argv[i + 1])
|
|
throw Error('Usage: node scripts/make_upgrade_package.cjs --version <1..4294967295> --input <payload> --output <new-package>');
|
|
options[key] = process.argv[i + 1];
|
|
}
|
|
if (!options['--input'] || !options['--output'] || !/^[1-9][0-9]*$/.test(options['--version'] || ''))
|
|
throw Error('Provide --version, --input and --output');
|
|
/* 先限制十进制正整数文本,再检查uint32范围,避免NaN、负数或精度损失进入包头。
|
|
* 该工具只验证字段范围;上传版本是否高于设备当前版本由接收端判断。 */
|
|
const version = Number(options['--version']);
|
|
if (!Number.isSafeInteger(version) || version > 0xffffffff) throw Error('Version must fit uint32');
|
|
const input = path.resolve(options['--input']), output = path.resolve(options['--output']);
|
|
if (input === output) throw Error('Input and output must differ');
|
|
/* 源文件先只读打开;目的文件稍后用wx独占新建,已有输出即使路径写法不同也不覆盖。
|
|
* 源句柄不锁定外部写入者,调用方应提供稳定负载;下方短读及末次尺寸检查仅作防御。 */
|
|
const source = fs.openSync(input, 'r');
|
|
let destination;
|
|
try {
|
|
const size = fs.fstatSync(source).size;
|
|
if (!Number.isSafeInteger(size) || size < 1 || size > MAX_PACKAGE - 16) throw Error('Payload size outside example limits');
|
|
// 整包上限包含16字节头;先从偏移16写负载,最后才填入完整有效头。
|
|
// CRC直接累计本轮实际写出的块,避免先算一遍源文件、再读另一版本的数据。
|
|
destination = fs.openSync(output, 'wx');
|
|
const chunk = Buffer.alloc(4096), header = Buffer.alloc(16);
|
|
let offset = 0, crc = 0xffffffff;
|
|
while (offset < size) {
|
|
const wanted = Math.min(chunk.length, size - offset);
|
|
if (fs.readSync(source, chunk, 0, wanted, offset) !== wanted) throw Error('Payload changed or short read');
|
|
for (let i = 0; i < wanted; ++i) {
|
|
crc ^= chunk[i];
|
|
for (let bit = 0; bit < 8; ++bit) crc = (crc >>> 1) ^ ((crc & 1) ? 0xedb88320 : 0);
|
|
}
|
|
if (fs.writeSync(destination, chunk, 0, wanted, offset + 16) !== wanted) throw Error('Short package write');
|
|
offset += wanted;
|
|
}
|
|
if (fs.fstatSync(source).size !== size) throw Error('Payload size changed');
|
|
/* CRC使用反射多项式0xEDB88320、全1初值和最终取反;>>>0转成无符号32位值。
|
|
* fsync发生在包头回填之后,打印成功前确保已请求同步整个输出文件。 */
|
|
header.write('I104', 0, 'ascii'); header.writeUInt32LE(version, 4); header.writeUInt32LE(size, 8);
|
|
header.writeUInt32LE((crc ^ 0xffffffff) >>> 0, 12);
|
|
if (fs.writeSync(destination, header, 0, header.length, 0) !== header.length) throw Error('Short header write');
|
|
fs.fsyncSync(destination);
|
|
console.log(`Created ${output}: version=${version} payload=${size} crc=${header.readUInt32LE(12)}`);
|
|
} finally {
|
|
/* 正常与异常共用关闭路径,仅关闭已取得的句柄;不删除残留输出,也不尝试原地续写。 */
|
|
if (destination !== undefined) fs.closeSync(destination);
|
|
fs.closeSync(source);
|
|
}
|
|
}
|
|
/* 命令行边界只打印原因并保留失败退出码;不把部分包当作成功产物。 */
|
|
try { main(); } catch (error) { console.error(error.message); process.exitCode = 1; }
|