mirror of
https://github.com/juanjiant-bit/JIANT.git
synced 2026-12-05 12:24:42 +01:00
FM's reserve 0xC8000..0xD7FFF (storage.c FL_SAMPLES, written by nothing) now holds, in its first half, cold main-loop
code run in place through the plain XIP window as the Bluetooth segment is. 0xD0000.. stays reserved; JAM's takes at
0xDA000 / 0xDB000 untouched.
- Build: app.ld C2SEG (XIP 0x020C4000), section .c2 (c2_head first, then .c2.text / .c2.rodata); core.h C2_FN / C2_IN /
C2_RO; src/seg2.c (header, c2_want, c2_seg_ok). tools/build.py writes build/c2.bin (JBC2, length, CRC-32) and patches
c2_want as bt_want; c2_check: nothing an interrupt, core 1 or RAM code reaches is in it, every call in from the slot
comes from a hand-checked gate (C2_GATES), no segment address stored as data.
- Moved: the editor protocol's commands and pushes (ed_command, ed_sync, their helpers, FM's ed_fm6_handle; INFO, the
backup and the project library stay in the slot), GEN's generators (gen_make, gen_parts, gen_pitched, gcpu_fit,
gen_sound, gen_kit, gen_mix, gl_write, gsong, gplan, grich, gls_prep, gen_arp, trackgen_run ...). FM differs from
BYTE: gen_make / gen_parts are slot code here (BYTE: Bluetooth segment), so they moved too, gen_go a gate;
gen_evolve and gx_comps stay in the slot (gen_post calls them); gen_rs_apply was already in the Bluetooth segment;
no dice_seq. Slot: 583660 -> 551828 B (676 -> 32508 B free). Segment: 31660 of 32768 B (1108 B free).
- Sound: every pinned / recorded audio function and table at its address (layout unchanged, no repin); audio flash
code identical but for immediates; RAM code +36 B (the flash driver's range check, FL_RANGE_OK). The merged
constants' floor (build/layout_cst.ld) is the first pool's start as laid out (FM's pools have UI constants before
the sound's first). seq_steps always inline (no out-of-line copy for the segment's calls).
- Runtime: bt_hw_seg() checks both segments once at power-on; missing / torn / another build's: FLASH PART MISSING /
REINSTALL at power-on and on GEN; the editor answers INFO ('X' 2), the backup and the project library only.
- Package / loader: fourth UFW entry c2.bin (type 201), checked before anything is erased, all of [0xC8000, 0xD0000)
written (0xFF past the code), never 0xD0000 and up. FL_STORE_OK / FL_RANGE_OK leave the segment out (static asserts).
- Tests: ldr_test (c2 written, rest erased, damaged c2 refused, 0xD0000.. untouched), editor_test (segments missing),
storage map assert, layout packing test. Manuals and EDITOR_PROTOCOL: FLASH PART MISSING.
Needs a hardware test on the FM-1 before any release.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KMHk6yH4XbP1xxHYKCo9Mh
270 lines
12 KiB
Python
Executable File
270 lines
12 KiB
Python
Executable File
#!/usr/bin/env python3
|
|
# SPDX-License-Identifier: GPL-3.0-only
|
|
# Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments
|
|
# Portions Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant)
|
|
"""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)
|
|
bt.bin (V10.12, optional) the Bluetooth segment, written by that loader at 0xEA000
|
|
c2.bin (seg2, optional) the second code segment, written by that loader at 0xC8000 (all of
|
|
[0xC8000, 0xD0000): its sectors past the segment erased)
|
|
|
|
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] [--bt bt.bin] [--c2 c2.bin]
|
|
"""
|
|
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
|
|
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("<HIIBBH16s", dcrc, offset & 0xFFFFFFFF, size & 0xFFFFFFFF, flags, resvd, index, nm)
|
|
return struct.pack("<H", crc16(body)) + body
|
|
|
|
|
|
def key_blob(key):
|
|
"""32 bytes that chipkey_decode() turns into `key` (+ CRC16)"""
|
|
d = bytearray([0x10] * 16) + bytearray(16) # sum 0 -> 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("<H", crc16(d))
|
|
|
|
|
|
def sdk_file(rel):
|
|
root = Path(SDK or os.environ.get("AC79_SDK", ""))
|
|
path = root / "cpu" / "wl82" / "tools" / rel
|
|
try:
|
|
return path.read_bytes()
|
|
except OSError as e:
|
|
raise SystemExit(f"fm1pkg_make: {path}: {e.strerror}. Set AC79_SDK to a JieLi AC79 SDK checkout.")
|
|
|
|
|
|
def flash_image(app, key):
|
|
spl = sdk_file("uboot.boot") # SDK SPL, BANKCB-encoded as in the SDK
|
|
cfg_tool = sdk_file("cfg_tool.bin")
|
|
eq = sdk_file("cfg/eq_cfg_hw.bin")
|
|
isd = key_blob(key) + b"[FELUCCA]\r\n"
|
|
f = bytearray(b"\xFF" * FLASH_SIZE)
|
|
# ---- head
|
|
spl_off = 0xA0
|
|
isd_off = spl_off + len(spl)
|
|
assert isd_off + len(isd) <= 0x4000
|
|
f[spl_off:spl_off + len(spl)] = spl
|
|
f[isd_off:isd_off + len(isd)] = isd
|
|
top = (entry(crc16(spl), spl_off, len(spl), 0x00, 0x00, 0, "uboot.boot") +
|
|
entry(crc16(isd), isd_off, len(isd), 0x02, 0x80, 0, "isd_config.ini") +
|
|
entry(0xFFFF, 0x4000, 0xFFFFFFFF, 0x81, 0xFF, 0, "app_dir_head") +
|
|
entry(0xFFFF, 0xFF000, 0x1000, 0x12, 0x01, 0xFFFF, "key_mac"))
|
|
hdr = struct.pack("<H", crc16(FLASH_HDR)) + FLASH_HDR
|
|
head = bytearray(hdr + top)
|
|
for o in range(0, len(head), 32): # header and each JLFS entry: ENC 0xFFFF
|
|
enc(head, o, 32)
|
|
f[0:len(head)] = head
|
|
# ---- app area (plain first, then SFC)
|
|
if len(app) > 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, 0x93000, 352256, 0x12, 0x80, 0, bytes.fromhex("564d00ffffffffffffffffff00800000")),
|
|
entry(0xFFFF, 0, FLASH_SIZE, 0x92, 0x82, 0, bytes.fromhex("5052435400ffffffffffffff434f4445")),
|
|
entry(0xFFFF, 0xE9000, 0x1000, 0x92, 0x80, 0, bytes.fromhex("4254494600ffffffffffffff4155544f")),
|
|
entry(0xFFFF, 0xEA000, 0x12000, 0x92, 0x81, 1, "USR")]
|
|
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("<H", r)[0] == crc16(r[2:80]):
|
|
raise SystemExit(f"the image has an update-record look-alike at {s - 0x100:#x}")
|
|
return bytes(f)
|
|
|
|
|
|
# (V10.12) the Bluetooth segment (firmware/bt/bt_seg.c): a third entry, "bt.bin", after ota.bin (whatever reads the first
|
|
# two finds them where they were). Felucca's update loader (firmware/loader/ldr_core.c) writes it at flash 0xEA000
|
|
# (FL_EXT_LO, plain: the XIP window from 0x93000 up is unencrypted) after the app area; the app checks it before use
|
|
BT_TYPE = 200
|
|
BT_BASE, BT_END, BT_MAGIC = 0xEA000, 0xFC000, 0x3154424A
|
|
|
|
|
|
def FM1_XIP(off):
|
|
return 0x02000000 + off - 0x4000
|
|
|
|
|
|
# (seg2) the second code segment (src/seg2.c, app.ld C2SEG): a fourth entry, "c2.bin", written by the loader at flash
|
|
# 0xC8000 (FL_SEG2_LO, plain as the Bluetooth segment), the rest of [0xC8000, 0xD0000) erased; never 0xD0000 and up
|
|
C2_TYPE = 201
|
|
C2_BASE, C2_END, C2_MAGIC = 0xC8000, 0xD0000, 0x3243424A
|
|
|
|
|
|
def bt_check(bt, name="bt.bin", base=BT_BASE, end=BT_END, magic=BT_MAGIC):
|
|
"""the segment as the loader writes it: its header, its size, and no 4K-256 slot that reads as an update record"""
|
|
if len(bt) <= 32 or struct.unpack_from("<I", bt)[0] != magic or len(bt) > end - base:
|
|
raise SystemExit(f"{name}: {len(bt)} B, not a segment for [{base:#x}, {end:#x})")
|
|
if struct.unpack_from("<I", bt, 4)[0] != len(bt) - 32:
|
|
raise SystemExit(f"{name}: the header's length is not the payload's")
|
|
f = bt + b"\xFF" * (-len(bt) % 0x1000)
|
|
for s in range(0x1000, len(f) + 1, 0x1000):
|
|
r = f[s - 0x100:s - 0x100 + 80]
|
|
if r[6] == 0x41 and r[7] == 0x54 and (r[0] | r[1]) and struct.unpack_from("<H", r)[0] == crc16(r[2:80]):
|
|
raise SystemExit(f"{name} has an update-record look-alike at {base + s - 0x100:#x}")
|
|
|
|
|
|
def c2_check(c2):
|
|
bt_check(c2, "c2.bin", C2_BASE, C2_END, C2_MAGIC)
|
|
|
|
|
|
def ufw(flash, ota, product, bt=None, c2=None):
|
|
files = [(0, "flash.bin", flash), (100, "ota.bin", ota)]
|
|
if bt:
|
|
bt_check(bt)
|
|
files.append((BT_TYPE, "bt.bin", bt))
|
|
if c2:
|
|
c2_check(c2)
|
|
files.append((C2_TYPE, "c2.bin", c2))
|
|
off, ents, data = 0x400, [], bytearray()
|
|
for i, (typ, name, d) in enumerate(files):
|
|
off = (off + 0xFF) & ~0xFF
|
|
e = bytearray(0x50)
|
|
struct.pack_into("<HHHHIII", e, 0, typ, i, crc16(d), 0, off, len(d), (len(d) + 31) & ~31)
|
|
e[0x40:0x40 + len(name)] = name.encode()
|
|
ents.append((e, off, d))
|
|
off += len(d)
|
|
total = off
|
|
hdr = bytearray(0x40)
|
|
struct.pack_into("<IH", hdr, 4, total, len(files))
|
|
struct.pack_into("<HHH", hdr, 10, 0x0004, 0x0200, 0) # header fields as written by the SDK packer
|
|
hdr[16:16 + len(UFW_CHIP)] = UFW_CHIP
|
|
lst = bytearray()
|
|
for e, _, _ in ents:
|
|
x = bytearray(e)
|
|
enc(x, 0, 0x50)
|
|
lst += x
|
|
struct.pack_into("<H", hdr, 2, crc16(lst))
|
|
struct.pack_into("<H", hdr, 0, crc16(hdr[2:0x40]))
|
|
enc(hdr, 0, 0x40)
|
|
logical = bytearray(b"\xFF" * total)
|
|
logical[0:0x40] = hdr
|
|
logical[0x40:0x40 + len(lst)] = lst
|
|
for e, o, d in ents:
|
|
logical[o:o + len(d)] = d
|
|
# .fwsc: one marker byte after each of the first 20 0x2F-byte blocks (the identity)
|
|
if len(product) > 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("--bt", type=Path, help="the Bluetooth segment (build/bt.bin), a third entry")
|
|
ap.add_argument("--c2", type=Path, help="the second code segment (build/c2.bin), a fourth entry")
|
|
a = ap.parse_args()
|
|
global SDK
|
|
SDK = a.sdk
|
|
pkg = ufw(flash_image(a.app.read_bytes(), a.key), a.ota.read_bytes(), a.product, a.bt.read_bytes() if a.bt else None,
|
|
a.c2.read_bytes() if a.c2 else None)
|
|
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()
|