Files
fm1-x0x/tests/host/installer_hs_test.mjs
T
Charles Vestal 502b2c28c1 M-UPGRADE 3.11 finds X0X again: ota.c answers its handshake with the identity's name, '_' and three digits, and the installers ask for the whole identity with the handshake's body 01
M-VAVE's M-UPGRADE 3.11.0318 (built 2026-11-03) checks the identity a device answers to its 0x11 handshake
against [^_\t\r\n]+_[0-9]{1,3} and refuses a longer one ("Invalid device identity", then "A USB MIDI device is
present but did not answer"). Every X0X release is FM-1_9 + XXYYZZ (1.2.0: FM-1_9010200), so M-UPGRADE found
none of them, and nobody could go back to the stock firmware with it.

firmware/src/ota.c, as felucca-bsp's chip/ota.c (41a6e86): ota_reply_identity(full) cuts the plain answer
("FM-1_9010200" -> "FM-1_901"; three digits or fewer stay) and ota_hs tells the plain query from the one with
the one-byte body 01, which gets the whole identity; ota_read answers both between reads as ota_service does.
The update loader (FELUCCA_LOADER) answers the plain query only, with its whole identity ("ota-FM-1_900" passes).

tools/fm1_install.py, web/fm1ota.js, web/backup/index.html, as fm1-base's (3c4a57e): HS_FULL; the handshake asks
with it when the plain answer is NAME_ and three digits (not a loader), and keeps the plain answer when nothing
answers in 0.6 s (M-VAVE's firmware, older builds), so the check after an install and the backup page's X0X
check still see the whole identity. The identity's text is read up to its first zero (felucca-bsp#29 puts the
flash's JEDEC id in bytes 24..26). The installer page, the landing page and the manual: M-UPGRADE 3.11 finds
this version, not older ones (docs/manual.html regenerated, with the PING WIDTH sentence it lacked).

Tests: tests/ota_hs_test.c (felucca-bsp's tests/ota_test.c: a release's identity, a dev build's, the loader;
run_tests.sh's ota-hs-*), ota_test.c (a release's identity: FM-1_901 to the plain query, the whole to 01),
install_test.py, tests/host/installer_hs_test.mjs (fm1-base's: the web handshake against releases and dev
builds with and without the fix, M-VAVE's firmware, the loader, #29's bytes), backup_page_test.mjs,
x0x_backup_test.py's delays. LICENSING.md: the two test files, MPL-2.0.

The scheme is PERF-1's (deamonSLINKY/PERF-1 775106b), so its installers and ours read each other's firmwares.
2026-11-05 19:59:57 +01:00

68 lines
3.9 KiB
JavaScript

// SPDX-License-Identifier: MPL-2.0
// Copyright (C) 2026 Charles Vestal
// From fm1-base's tests/installer_hs_test.mjs (3c4a57e).
// The web installer's handshake (web/fm1ota.js) against fake FM-1s: what each firmware answers. A firmware on
// firmware/src/ota.c from M-UPGRADE 3.11's fix on answers the plain query with its name, '_' and three digits
// ("FM-1_901": M-UPGRADE 3.11 takes NAME_ and 1-3 digits only) and its whole identity to the query with the body 01
// (PERF-1's scheme, deamonSLINKY/PERF-1 775106b); the installer has to see the whole one, and every other firmware as
// before.
import { Updater, HS_FULL, pack7, unpack7 } from "../../web/fm1ota.js";
const HS_QUERY = [0xF0, 0x00, 0x32, 0x45, 0x00, 0x00, 0x00, 0x40, 0x7F, 0xF7];
const same = (a, b) => a.length === b.length && a.every((x, i) => x === b[i]);
// an identity reply: the text, zeros, felucca-bsp#29's flash JEDEC id in bytes 24..26 (jedec), the checksum
const idPkt = (text, jedec) => {
const t = [...text].map((c) => c.charCodeAt(0)).concat(new Array(27 - text.length).fill(0));
if (jedec) t[24] = 0xB3, t[25] = 0x60, t[26] = 0x14;
return [0xF0, ...pack7([0, 0x59, 0x11, 27, 0, 0, ...t, ~t.reduce((a, v) => a + v, 0) & 0xFF]), 0xF7];
};
// one port called FM-1: answer(query) -> the identity text to send back, or null
function access(answer, jedec) {
const input = { name: "FM-1", state: "connected", onmidimessage: null, open: async () => {} };
const output = {
name: "FM-1", state: "connected", open: async () => {}, sent: [],
send(b) {
this.sent.push(Array.from(b));
const t = answer(Array.from(b));
if (t !== null) setTimeout(() => input.onmidimessage && input.onmidimessage({ data: idPkt(t, jedec) }), 5);
},
};
return { inputs: new Map([[1, input]]), outputs: new Map([[1, output]]), output };
}
let bad = 0;
function check(what, cond) { console.log((cond ? "ok " : "FAIL ") + what); if (!cond) bad++; }
const cut = (t) => t.slice(0, t.indexOf("_") + 4);
const fixed = (t) => (q) => same(q, HS_QUERY) ? cut(t) : same(q, HS_FULL) ? t : null; // chip/ota.c with the fix
const before = (t) => (q) => same(q, HS_QUERY) ? t : null; // everything else
const cases = [
["a release with the fix: FM-1_901, then its whole identity", fixed("FM-1_9010200"), "FM-1_9010200", true, true],
["a release before it: its whole identity to the plain query", before("FM-1_9010200"), "FM-1_9010200", false, false],
["a dev build with the fix: FM-1_900", fixed("FM-1_900"), "FM-1_900", true, false],
["a dev build before it: FM-1_900, no answer to 01", before("FM-1_900"), "FM-1_900", true, false],
["M-VAVE's firmware: FM-1_015, no answer to 01", before("FM-1_015"), "FM-1_015", true, false],
["the update loader: ota-FM-1_900, never asked with 01", before("ota-FM-1_900"), "ota-FM-1_900", false, false],
];
for (const [what, answer, want, asksFull, jedec] of cases) {
const a = access(answer, jedec);
const dev = await new Updater(a).find();
if (dev) dev.link.close();
check(`${what}: ${want}`, dev && dev.id.text === want);
check(` ${asksFull ? "asked with 01" : "never asked with 01"}`, a.output.sent.some((q) => same(q, HS_FULL)) === asksFull);
}
{
const a = access(fixed("FM-1_9010200"), true);
const dev = await new Updater(a).find();
if (dev) dev.link.close();
check("felucca-bsp#29's bytes after the text: the text up to its first zero", dev && dev.id.text === "FM-1_9010200" &&
dev.id.model === "FM-1" && dev.id.version === 9010200);
}
// the bytes of the query with 01, as chip/ota.c checks them (ota_take: header, length, checksum)
check("HS_FULL is 00 59 11, length 1, body 01, checksum FE",
same(HS_FULL, [0xF0, ...pack7([0, 0x59, 0x11, 1, 0, 0, 1, 0xFE]), 0xF7]) &&
same(unpack7(HS_FULL.slice(1, -1)).slice(0, 8), [0, 0x59, 0x11, 1, 0, 0, 1, 0xFE]));
if (bad) { console.log(`${bad} FAILED`); process.exit(1); }
console.log("installer handshake tests passed");