mirror of
https://github.com/isod89/sloop-fm1.git
synced 2026-12-06 21:04:24 +01:00
45 lines
2.0 KiB
JavaScript
45 lines
2.0 KiB
JavaScript
// SPDX-License-Identifier: GPL-3.0-only
|
|
// Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
|
|
//
|
|
// FM-1 .fwsc packages in the browser: the package identity and the image the
|
|
// device reads during an update.
|
|
|
|
const BLOCKS = 20, BLK = 0x30, KEEP = 0x2F;
|
|
|
|
// the package identity ("FM-1_904"): 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;
|
|
}
|
|
|
|
// the image the device addresses during an update: the .fwsc without the 20 marker bytes
|
|
export function logicalImage(fwsc) {
|
|
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 official FM-1 V15 (M-VAVE Downloads -> PC Firmware -> FM-1 V15, FM-1.fwsc), for the return to the
|
|
// official firmware: only that exact file is accepted (its SHA-256 pins every byte: the loader, the app, the
|
|
// layout; the name of the file proves nothing). The digest is the one Felucca 1.0 pins.
|
|
export const STOCK_V15_SHA256 = "db1642b2b6fa5c2cccb11ffd13878068bb28601678d3644049f99dc40e7edb8a";
|
|
export const STOCK_V15_SIZE = 699956;
|
|
export const STOCK_V15_PRODUCT = "FM-1_015";
|
|
|
|
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("");
|
|
}
|
|
|
|
export async function validateStockPackage(bytes) {
|
|
if (bytes.length !== STOCK_V15_SIZE || await sha256hex(bytes) !== STOCK_V15_SHA256 || productOf(bytes) !== STOCK_V15_PRODUCT)
|
|
throw new Error("select the unmodified official FM-1 V15 file (FM-1.fwsc)");
|
|
return { product: STOCK_V15_PRODUCT, image: logicalImage(bytes) };
|
|
}
|