Files
illuminus5 4382e9cf5e Skiff 2.0.2: notes up to 128 steps, the way back takes V16 too, fixes from your issues
- #58: LENGTH on NOTES (and PRESETS while entering steps) up to 128 steps: up to 64 within a pattern, past its end on
  through all of a chain's next part.
- #64: the way back to the official firmware takes M-VAVE's V16 too: an FM-1 that ran V16 goes back to V16, one that
  only ran V15 to V15 (its start-up part decides); the installer says which one a unit needs, or to update Skiff
  first. Tried in the emulator only so far.
- #57: an FX key held as the layer locks stays on, latched; its next press or closing the layer turns it off.
- #61: MOD's route 8 lights its own row only.
- #56: SEQ's pages no longer decide which knobs a held step locks (after PATTERN a drum step still offers KIT, PITCH,
  TONE and DECAY).
- ARCADE (NOISE) goes on rising past middle F: the shift register steps several times a sample.
- A NEW pattern and an empty entry start notes at 1 STEP, not at a pattern from before 2.0's 50 %.
2026-10-26 21:47:08 -05:00

332 lines
14 KiB
JavaScript

// SPDX-License-Identifier: GPL-3.0-only
// Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
//
// FM-1 (and SMK-37 Pro) .fwsc packages in the browser: the package identity and the image the
// device reads during an update.
const BLOCKS = 20, BLK = 0x30, KEEP = 0x2F, MAX_BLOCKS = 64;
export const STOCK_V15_SHA256 = "db1642b2b6fa5c2cccb11ffd13878068bb28601678d3644049f99dc40e7edb8a";
export const STOCK_APP_SIZE = 0x8DFBC;
export const STOCK_V15_SIZE = 699956;
// (2.0.2, #64) the unmodified official FM-1 V16 (M-VAVE's FM-1.fwsc since 2026-10-21; FM-1_016): the way back of a unit
// that ran V16 (its head is V16's: Skiff's ota.c stages a package whose head is the unit's only)
export const STOCK_V16_SHA256 = "d017fb65e70aca97ee29d944470c3515a74581df5c0576bf70de917c7b2b61d5";
export const STOCK_V16_SIZE = 718196;
// each official FM-1 file's layout as pinned: identity, flash.bin's size, the app's offset and size
const FM1_STOCK = {
[STOCK_V15_SHA256]: { product: "FM-1_015", flash: 602112, appOff: 16672, app: STOCK_APP_SIZE },
[STOCK_V16_SHA256]: { product: "FM-1_016", flash: 692224, appOff: 16704, app: 0x91FA8 },
};
// the unmodified official SMK-37 Pro V016 (SMK-37_Pro_016.fwsc, 36 interleave blocks): the return of an SMK-37 Pro
// (the same pin as tools/fm1_install.py)
export const STOCK_SMK016_SHA256 = "2d98ce72530e71d617384963423820536f094501299e2cfec0a8d3c3fb6bcfb0";
export const STOCK_SMK016_SIZE = 705252;
const T = new Uint16Array(256);
for (let i = 0; i < 256; i++) {
let c = i << 8;
for (let k = 0; k < 8; k++) c = c & 0x8000 ? (c << 1) ^ 0x1021 : c << 1;
T[i] = c & 0xFFFF;
}
export function crc16(d, crc = 0) {
for (let i = 0; i < d.length; i++) crc = ((crc << 8) & 0xFFFF) ^ T[((crc >> 8) ^ d[i]) & 0xFF];
return crc;
}
function enc(buf, off, size, key = 0xFFFF) {
for (let i = 0; i < size; i++) {
buf[off + i] ^= key & 0xFF;
key = ((key * 2) ^ (key & 0x8000 ? 0x1021 : 0)) & 0xFFFF;
}
}
function sfc(buf, off, size, base, key) {
for (let i = 0; i < size; i += 32) enc(buf, off + i, Math.min(size - i, 32), key ^ ((off + i - base) >> 2));
}
function chipkeyDecode(d) {
let sum = 0;
for (let i = 0; i < 16; i++) sum += d[i];
sum &= 0xFF;
sum = sum >= 0xE0 ? 0xAA : sum <= 0x10 ? 0x55 : sum;
let k = 0;
for (let i = 0; i < 16; i++) if ((d[16 + i] ^ d[15 - i]) < sum) k |= 1 << i;
return k;
}
const align32 = (v) => (v + 31) & ~31;
const u16 = (b, o) => b[o] | (b[o + 1] << 8);
const u32 = (b, o) => (b[o] | (b[o + 1] << 8) | (b[o + 2] << 16) | (b[o + 3] << 24)) >>> 0;
function w16(b, o, v) { b[o] = v & 0xFF; b[o + 1] = (v >> 8) & 0xFF; }
function w32(b, o, v) { w16(b, o, v & 0xFFFF); w16(b, o + 2, (v >>> 16) & 0xFFFF); }
class Entry { // JLFS head, 32 bytes
constructor(raw, off) {
this.hdrOff = off;
this.hcrc = u16(raw, off);
this.dcrc = u16(raw, off + 2);
this.offset = u32(raw, off + 4);
this.size = u32(raw, off + 8);
this.flags = raw[off + 12];
this.resvd = raw[off + 13];
this.index = u16(raw, off + 14);
this.rawName = raw.slice(off + 16, off + 32);
const z = this.rawName.indexOf(0);
this.name = String.fromCharCode(...this.rawName.slice(0, z < 0 ? 16 : z));
if (crc16(raw.subarray(off + 2, off + 32)) !== this.hcrc) throw new Error(`JLFS header CRC mismatch @${off.toString(16)}`);
}
packInto(buf) {
const o = this.hdrOff;
w16(buf, o + 2, this.dcrc); w32(buf, o + 4, this.offset); w32(buf, o + 8, this.size);
buf[o + 12] = this.flags; buf[o + 13] = this.resvd; w16(buf, o + 14, this.index);
buf.set(this.rawName, o + 16);
this.hcrc = crc16(buf.subarray(o + 2, o + 32));
w16(buf, o, this.hcrc);
}
}
export class Package {
constructor(bytes) {
this.raw = new Uint8Array(bytes);
this.parseUfw();
this.flash = this.raw.slice(this.flashFileOff, this.flashFileOff + this.flashSize);
this.parseFlash();
}
get product() { return productOf(this.raw); }
setProduct(name) {
if (name.length > BLOCKS) throw new Error("product name too long");
for (let i = 0; i < BLOCKS; i++) {
const m = i < name.length ? (name.charCodeAt(i) + i + 1) & 0xFF : 0x7D;
if (i < name.length && m === 0x7D) throw new Error("product name encodes to the unused marker");
this.raw[i * BLK + KEEP] = m;
}
}
headerBytes() {
const h = new Uint8Array(BLOCKS * KEEP);
for (let i = 0; i < BLOCKS; i++) h.set(this.raw.subarray(i * BLK, i * BLK + KEEP), i * KEEP);
return h;
}
hdrFileOff(i) { return Math.floor(i / KEEP) * BLK + (i % KEEP); }
parseUfw() {
const h = this.headerBytes();
enc(h, 0, 0x40);
const hcrc = u16(h, 0), lcrc = u16(h, 2), nent = u16(h, 8);
if (crc16(h.subarray(2, 0x40)) !== hcrc) throw new Error("not an FM-1 .fwsc (UFW header CRC mismatch)");
this.skew = BLOCKS * BLK - h.length;
this.ufw = h;
this.nent = nent;
const hsize = 0x40 + nent * 0x50;
if (crc16(h.subarray(0x40, hsize)) !== lcrc) throw new Error("UFW entry list CRC mismatch");
this.entries = [];
for (let off = 0x40; off < hsize; off += 0x50) {
const e = h.slice(off, off + 0x50);
enc(e, 0, 0x50);
const z = e.subarray(0x40, 0x50).indexOf(0);
this.entries.push({ off, type: u16(e, 0), dcrc: u16(e, 4), dataOff: u32(e, 8), size: u32(e, 12),
name: String.fromCharCode(...e.subarray(0x40, z < 0 ? 0x50 : 0x40 + z)), plain: e });
}
const fl = this.entries.find((e) => e.type === 0);
if (!fl) throw new Error("no flash.bin in the package");
this.flashEntry = fl;
this.flashFileOff = fl.dataOff + this.skew;
this.flashSize = fl.size;
if (crc16(this.raw.subarray(this.flashFileOff, this.flashFileOff + fl.size)) !== fl.dcrc) throw new Error("flash.bin CRC mismatch");
}
parseFlash() {
const dec = this.flash.slice();
const h = dec.slice(0, 32);
enc(h, 0, 32);
if (crc16(h.subarray(2, 32)) !== u16(h, 0)) throw new Error("flash header CRC mismatch");
this.chipKey = null; this.appBase = null;
for (let off = 32; ; off += 32) {
enc(dec, off, 32);
const e = new Entry(dec, off);
if (e.name === "isd_config.ini") {
const blob = dec.subarray(e.offset, e.offset + 34);
if (crc16(blob.subarray(0, 32)) === u16(blob, 32)) this.chipKey = chipkeyDecode(blob.subarray(0, 32));
}
if (e.flags === 0x81 && e.name === "app_dir_head" && this.appBase === null) this.appBase = e.offset;
if (e.index) break;
}
if (this.chipKey === null || this.appBase === null) throw new Error("chip key or app_dir_head not found");
const b = this.appBase, k = this.chipKey, end = align32(b + 32);
sfc(dec, b, end - b, b, k);
const head = new Entry(dec, b);
const blkEnd = b + head.size;
if (blkEnd > end) sfc(dec, end, blkEnd - end, b, k);
this.sfcEnd = Math.max(end, blkEnd);
this.cfgLen = 0;
const probe = this.flash.slice(b, align32(blkEnd) + 32);
sfc(probe, 0, probe.length, 0, k);
try {
const cfg = new Entry(probe, blkEnd - b);
if (cfg.name === "cfg" && blkEnd + cfg.size <= this.flash.length) this.cfgLen = cfg.size;
} catch (_) { /* no cfg block */ }
if (this.cfgLen) {
const region = this.flash.slice(b, blkEnd + this.cfgLen);
sfc(region, 0, region.length, 0, k);
dec.set(region, b);
this.sfcEnd = blkEnd + this.cfgLen;
}
this.head = head;
this.app = null;
for (let off = b + 32; ; off += 32) {
const e = new Entry(dec, off);
if (e.flags === 0x82 && e.name === "app.bin") this.app = e;
if (e.index) break;
}
if (!this.app) throw new Error("app.bin not found in app area");
this.appOff = b + this.app.offset;
this.dec = dec;
}
// the new app, padded to the stock size (Felucca never changes the layout)
setApp(app) {
if (app.length > this.app.size) throw new Error(`app is ${app.length} B, the slot is ${this.app.size} B`);
const nw = new Uint8Array(this.app.size).fill(0xFF);
nw.set(app);
const a = this.app, h = this.head, b = this.appBase;
this.dec.set(nw, this.appOff);
a.dcrc = crc16(nw);
a.packInto(this.dec);
h.dcrc = crc16(this.dec.subarray(h.hdrOff + 32, h.hdrOff + h.size));
h.packInto(this.dec);
this.sfcEnd = Math.max(align32(b + 32), b + h.size + this.cfgLen);
const region = this.dec.slice(b, this.sfcEnd);
sfc(region, 0, region.length, 0, this.chipKey);
this.flash.set(region, b);
}
serialize() {
const out = this.raw.slice();
out.set(this.flash, this.flashFileOff);
const fl = this.flashEntry, e = fl.plain.slice();
w16(e, 4, crc16(this.flash));
enc(e, 0, 0x50);
const h = this.ufw.slice();
h.set(e, fl.off);
const hsize = 0x40 + this.nent * 0x50;
w16(h, 2, crc16(h.subarray(0x40, hsize)));
w16(h, 0, crc16(h.subarray(2, 0x40)));
enc(h, 0, 0x40);
for (let i = 0; i < hsize; i++) out[this.hdrFileOff(i)] = h[i];
return out;
}
}
// the package identity ("FM-1_904", "SMK-37 Pro_016"): one marker byte after each of the first 20 blocks
export function productOf(fwsc) {
if (fwsc.length < BLOCKS * BLK) throw new Error("not an FM-1 package (too short)");
let s = "";
for (let i = 0; i < BLOCKS; i++) {
const m = fwsc[i * BLK + KEEP];
if (m !== 0x7D) s += String.fromCharCode((m - i - 1) & 0xFF);
}
return s;
}
// interleave blocks: 20, then more while the marker is the unused 0x7D (the stock SMK-37 Pro v016 has 36)
export function blockCount(fwsc) {
let n = BLOCKS;
while (n < MAX_BLOCKS && (n + 1) * BLK <= fwsc.length && fwsc[n * BLK + KEEP] === 0x7D) n++;
return n;
}
function deinterleave(fwsc, blocks) {
const out = new Uint8Array(fwsc.length - blocks);
for (let i = 0; i < blocks; i++) out.set(fwsc.subarray(i * BLK, i * BLK + KEEP), i * KEEP);
out.set(fwsc.subarray(blocks * BLK), blocks * KEEP);
return out;
}
// the logical image's flash.bin (type 0) and ota.bin (type 100) match their CRCs (as fm1_install.py files_ok)
function filesOk(img) {
if (img.length < 0x40) return false;
const rd = (a, o, n) => a.slice(o, o + n).reduce((v, x, i) => v + x * 2 ** (8 * i), 0);
const h = Uint8Array.from(img.subarray(0, 0x40));
enc(h, 0, 0x40);
if (crc16(h.subarray(2, 0x40)) !== rd(h, 0, 2)) return false;
const count = rd(h, 8, 2);
if (count < 1 || count > 8 || img.length < 0x40 + 0x50 * count) return false;
for (let i = 0; i < count; i++) {
const e = Uint8Array.from(img.subarray(0x40 + i * 0x50, 0x90 + i * 0x50));
enc(e, 0, 0x50);
const typ = rd(e, 0, 2), off = rd(e, 8, 4), size = rd(e, 12, 4);
if (off + size > img.length) return false;
if ((typ === 0 || typ === 100) && crc16(img.subarray(off, off + size)) !== rd(e, 4, 2)) return false;
}
return true;
}
// where flash.bin (type 0) lies in a logical image: { off, size }, or null (no file list). For the write's
// progress (fm1ota.js WriteMeter), as tools/fm1_install.py flash_span
export function flashSpan(img) {
if (img.length < 0x40) return null;
const rd = (a, o, n) => a.slice(o, o + n).reduce((v, x, i) => v + x * 2 ** (8 * i), 0);
const h = Uint8Array.from(img.subarray(0, 0x40));
enc(h, 0, 0x40);
const count = rd(h, 8, 2);
for (let i = 0; i < Math.min(count, 8) && 0x90 + i * 0x50 <= img.length; i++) {
const e = Uint8Array.from(img.subarray(0x40 + i * 0x50, 0x90 + i * 0x50));
enc(e, 0, 0x50);
const off = rd(e, 8, 4), size = rd(e, 12, 4);
if (rd(e, 0, 2) === 0 && size && off + size <= img.length) return { off, size };
}
return null;
}
// the image the device addresses during an update: the .fwsc without the marker bytes (20 blocks, or the
// detected count when the files' CRCs agree: a 20-block package can have 0x7D after block 20 by chance)
export function logicalImage(fwsc) {
const n = blockCount(fwsc);
if (n !== BLOCKS) {
const img = deinterleave(fwsc, n);
if (filesOk(img) || !filesOk(deinterleave(fwsc, BLOCKS))) return img;
}
return deinterleave(fwsc, BLOCKS);
}
export async function sha256hex(bytes) {
const d = await crypto.subtle.digest("SHA-256", bytes);
return [...new Uint8Array(d)].map((x) => x.toString(16).padStart(2, "0")).join("");
}
// Only the user's unmodified official firmware is accepted for a return to stock: FM-1 V15 or V16 (FM-1.fwsc) or
// SMK-37 Pro V016 (SMK-37_Pro_016.fwsc). The digest pins every file, loader and flash layout; filenames are
// not evidence. (On an SMK, Skiff stages its own loader for it: M-VAVE's must not run from Skiff's staging.)
export async function validateStockPackage(bytes) {
const sha = await sha256hex(bytes);
if (bytes.length === STOCK_SMK016_SIZE && sha === STOCK_SMK016_SHA256) {
const image = logicalImage(bytes);
if (productOf(bytes) !== "SMK-37 Pro_016" || !filesOk(image))
throw new Error("The official firmware has an unsupported SMK-37 layout.");
return { product: "SMK-37 Pro_016", image, sha256: sha };
}
const L = FM1_STOCK[sha];
if (!L || bytes.length !== (L.product === "FM-1_016" ? STOCK_V16_SIZE : STOCK_V15_SIZE))
throw new Error("Select the unmodified official firmware: FM-1.fwsc (FM-1 V15 or V16) or SMK-37_Pro_016.fwsc (SMK-37 Pro V016).");
const pkg = new Package(bytes);
if (pkg.product !== L.product || pkg.flashFileOff !== 1044 || pkg.flashSize !== L.flash ||
pkg.appBase !== 16384 || pkg.appOff !== L.appOff || pkg.app.size !== L.app ||
!pkg.entries.some((e) => e.type === 100 && e.name === "ota.bin" && e.size === 19969))
throw new Error("The official firmware has an unsupported FM-1 layout.");
return { product: pkg.product, image: logicalImage(bytes), sha256: sha };
}
// official V15 + Felucca app -> installable package (Uint8Array)
export async function buildFeluccaPackage(stockBytes, appBytes, product) {
const sha = await sha256hex(stockBytes);
if (sha !== STOCK_V15_SHA256) throw new Error("this is not the official FM-1 V15 firmware file (FM-1.fwsc)");
const pkg = new Package(stockBytes);
pkg.setApp(appBytes);
pkg.setProduct(product);
return pkg.serialize();
}