#!/usr/bin/env python3 # SPDX-License-Identifier: GPL-3.0-only # Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments """Build a complete FM-1 update package (.fwsc) from scratch, with no vendor material in it: flash.bin head [0, 0x4000): flash header (JieLi SDK default values), JLFS entries, the SDK SPL (uboot.boot, Apache-2.0), an isd_config blob of our own that decodes to the chip key; the head is never written by Felucca's loader, it only has to look like a package head app area 0x4000..: app_area_head, Felucca app.bin, the SDK cfg_tool.bin and eq_cfg_hw.bin, the region descriptors, SFC-encrypted with the chip key ota.bin Felucca's own update loader (firmware/loader) The layout values (offsets, flags, descriptor addresses, header fields) are facts of the FM-1 flash layout and the SDK file formats. The three SDK files (uboot.boot, cfg_tool.bin, eq_cfg_hw.bin) are Apache-2.0, not GPL: ship LICENSES/Apache-2.0.txt with every package (LICENSING.md; make_site.py and build.py --release do). fm1pkg_make.py APP.bin OTA.bin OUT.fwsc [--product FM-1_9XY] [--key 0x980F] [--board fm1|smk37] --board smk37 (the M-VAVE SMK-37, same AC791N, SPL and loader; after Melodee's fm1pkg_make.py) selects the SMK's flash layout: flash.bin is the stock PRCT, 0x9D000 bytes, and the app area carries the stock v016 region table (VM, PRCT, BTIF, USRTRIM, USRFLASH, USR) byte for byte, so a later return to stock sees the same regions. app.bin's slot (APP_SLOT) is the FM-1's: the encrypted region still ends at 0x92FFE. """ import argparse import os import struct from pathlib import Path # (JIANT 0.9.13) the app slot grows from the stock 0x8DFBC to 0x8EA90 (+2772 B): the encrypted region (app_area_head # block + cfg_tool.bin + the cfg directory with eq_cfg_hw.bin) now ends at 0x92FFE, inside flash.bin [0, 0x93000) # that every Felucca/JIANT loader already writes whole (0xFF padding up to 0x93000 until 0.9.12): no loader change. # firmware/app.ld's XIP LENGTH is this value (tools/build.py checks it). APP_SLOT = 0x8EA90 FLASH_SIZE = 0x93000 BOARD = "fm1" # (SMK-37, after Melodee's fm1pkg_make.py) the region descriptors after app.bin / cfg_tool.bin, per board: (offset, # size or None for FLASH_SIZE, flags, resvd, index, 16 name bytes or a name). The bytes after the names are SDK packer # fields (VM size, "CODE", "AUTO"). The FM-1's are the four this file always wrote. REGIONS = { "fm1": [(0x93000, 352256, 0x12, 0x80, 0, bytes.fromhex("564d00ffffffffffffffffff00800000")), # VM (0, None, 0x92, 0x82, 0, bytes.fromhex("5052435400ffffffffffffff434f4445")), # PRCT (0xE9000, 0x1000, 0x92, 0x80, 0, bytes.fromhex("4254494600ffffffffffffff4155544f")), # BTIF (0xEA000, 0x12000, 0x92, 0x81, 1, "USR")], # the SMK-37's stock v016 package's region table "smk37": [(0x9D000, 0x23000, 0x12, 0x81, 0, bytes.fromhex("564d00ffffffffffffffffff00800000")), # VM (0, None, 0x92, 0x82, 0, bytes.fromhex("5052435400ffffffffffffff434f4445")), # PRCT (0xC0000, 0x1000, 0x92, 0x82, 0, bytes.fromhex("4254494600ffffffffffffff4155544f")), # BTIF (0xC1000, 0x1000, 0x92, 0x81, 0, bytes.fromhex("5553525452494d00ffffffffffffffff")), # USRTRIM (0xC2000, 0x29000, 0x92, 0x81, 0, bytes.fromhex("555352464c41534800ffffffffffffff")), # USRFLASH (0xF4000, 0xA000, 0x92, 0x81, 1, "USR")], } BOARDS = {"fm1": 0x93000, "smk37": 0x9D000} # FLASH_SIZE = PRCT = the end of what the update loader writes # smk37: one board under two model names; each build carries its model's name (identity, loader, update record) MODELS = {"pro": "SMK-37 Pro", "elite": "SMK-37 Elite"} def set_board(board): """select the flash layout of a board ("fm1", the default, or "smk37")""" global BOARD, FLASH_SIZE if board not in BOARDS: raise SystemExit(f"fm1pkg_make: unknown board {board!r} (fm1, smk37)") BOARD = board FLASH_SIZE = BOARDS[board] KEY = 0x980F # flash header: JieLi SDK defaults (burner_size 544, VID "0.01", flash_size 0xFF000, # fs_ver 0x10, PID "AC791N_STORY"), plain, without its CRC FLASH_HDR = bytes.fromhex("20022f104cc900f00f001010a4ff4e5d0b41e0cd781dc856417fb367cf9f") UFW_CHIP = b"AC791N" SDK = None def _crc_table(): t = [] for i in range(256): c = i << 8 for _ in range(8): c = ((c << 1) ^ 0x1021) if c & 0x8000 else (c << 1) t.append(c & 0xFFFF) return t _T = _crc_table() def crc16(data, crc=0): for b in data: crc = ((crc << 8) & 0xFFFF) ^ _T[((crc >> 8) ^ b) & 0xFF] return crc def enc(buf, off, size, key=0xFFFF): """JieLi ENC stream cipher (in place)""" for i in range(size): buf[off + i] ^= key & 0xFF key = ((key << 1) ^ (0x1021 if key & 0x8000 else 0)) & 0xFFFF def sfc(buf, off, size, base, key): """SFC cipher: ENC per 32-byte block, key mixed with the block address""" for i in range(0, size, 32): enc(buf, off + i, min(size - i, 32), key ^ ((off + i - base) >> 2)) def chipkey_decode(d): """chip key from the 32-byte isd_config blob""" thesum = sum(d[:16]) & 0xFF thesum = 0xAA if thesum >= 0xE0 else 0x55 if thesum <= 0x10 else thesum return sum(1 << i for i in range(16) if (d[16 + i] ^ d[15 - i]) < thesum) def entry(dcrc, offset, size, flags, resvd, index, name, fill=0xFF): if isinstance(name, bytes): # 16 raw bytes: name + the packer's trailing fields nm = name else: nm = name.encode() + b"\0" nm = nm + bytes([fill]) * (16 - len(nm)) body = struct.pack(" threshold 0x55 for i in range(16): d[16 + i] = d[15 - i] ^ (0x00 if key >> i & 1 else 0xFF) assert chipkey_decode(bytes(d)) == key return bytes(d) + struct.pack(" APP_SLOT: raise SystemExit(f"app is {len(app)} B, the slot is {APP_SLOT} B") app = app + b"\xFF" * (APP_SLOT - len(app)) blk = 0x120 + APP_SLOT + len(cfg_tool) # app_area_head block size area = bytearray(b"\xFF" * blk) area[0x120:0x120 + APP_SLOT] = app area[0x120 + APP_SLOT:blk] = cfg_tool ents = [entry(crc16(app), 0x120, APP_SLOT, 0x82, 0xFF, 0, "app.bin"), entry(crc16(cfg_tool), 0x120 + APP_SLOT, len(cfg_tool), 0x82, 0xFF, 0, "cfg_tool.bin"), # region descriptors; the bytes after the names are SDK packer fields (VM size, "CODE", "AUTO") *(entry(0xFFFF, o, FLASH_SIZE if sz is None else sz, fl, rs, ix, nm) for o, sz, fl, rs, ix, nm in REGIONS[BOARD])] assert 32 * (1 + len(ents)) <= 0x120, "the entries overrun app.bin (app_area_head + 0x120)" area[32:32 + len(b"".join(ents))] = b"".join(ents) area[0:32] = entry(crc16(area[32:blk]), 0x02000120, blk, 0x83, 0xFF, 0, "app_area_head") # nested cfg directory right after the block: SDK default EQ table eq_e = entry(crc16(eq), 0x40, len(eq), 0x82, 0xFF, 1, "eq_cfg_hw.bin") cfg_body = eq_e + eq cfg = entry(crc16(cfg_body), 0x20, 0x20 + len(cfg_body), 0x83, 0xFF, 1, "cfg") + cfg_body region = bytearray(area + cfg) if 0x4000 + len(region) > FLASH_SIZE: # the loaders write flash.bin [0x4000, FLASH_SIZE) only raise SystemExit(f"app region ends at {0x4000 + len(region):#x}, past flash.bin ({FLASH_SIZE:#x})") sfc(region, 0, len(region), 0, key) f[0x4000:0x4000 + len(region)] = region for s in range(0x5000, FLASH_SIZE + 1, 0x1000): # no 4K-256 slot of the image may read as an update record r = f[s - 0x100:s - 0x100 + 80] # (what the SPL and ldr_core.c look for: magic + CRC16) if r[6] == 0x41 and r[7] == 0x54 and (r[0] | r[1]) and struct.unpack_from(" 20 or any((ord(c) + i + 1) & 0xFF == 0x7D for i, c in enumerate(product)): raise SystemExit(f"product {product!r}: at most 20 characters, none encoding to the unused marker") raw = bytearray() for i in range(20): ch = product[i] if i < len(product) else None m = (ord(ch) + i + 1) & 0xFF if ch else 0x7D raw += logical[i * 0x2F:(i + 1) * 0x2F] + bytes([m]) raw += logical[20 * 0x2F:] return bytes(raw) def main(): ap = argparse.ArgumentParser() ap.add_argument("app", type=Path) ap.add_argument("ota", type=Path) ap.add_argument("out", type=Path) ap.add_argument("--product", default="FM-1_900") ap.add_argument("--key", type=lambda s: int(s, 0), default=KEY) ap.add_argument("--sdk", type=Path, help="JieLi AC79 SDK checkout (default: $AC79_SDK)") ap.add_argument("--board", choices=sorted(BOARDS), default="fm1") a = ap.parse_args() global SDK SDK = a.sdk set_board(a.board) pkg = ufw(flash_image(a.app.read_bytes(), a.key), a.ota.read_bytes(), a.product) a.out.write_bytes(pkg) print(f"package {a.out}: {len(pkg)} B, identity {a.product}, chip key {a.key:#06x}") if __name__ == "__main__": main()