Files
Claude c4e04dd749 The second code segment (flash 0xC8000..0xCFFFF), ported from JIANT BYTE (e807224d): 31.8 KB out of the app slot
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
2026-12-04 22:33:34 +00:00

289 lines
14 KiB
C

/* 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) */
/* Felucca update loader, the portable part (step 2 of the M-UPGRADE update
* protocol).
*
* The host serves the package ("logical image") with cmd 0x30 reads (ota.c).
* The loader:
* 1. reads the UFW header and finds flash.bin;
* 2. checks that the package's app area decrypts with THIS chip's key
* (taken from isd_config.ini in the device's own flash head), so a
* package for another key is refused before anything is erased;
* 2b. reads the whole flash.bin once and checks it against the CRC16 of its
* UFW entry (the vendor tools' check), keeping each app sector's CRC16:
* a damaged or truncated package is refused before anything is erased;
* 3. writes ONLY the app area [0x4000, 0x93000), sector by sector, skipping
* sectors that are already equal, and verifies each one; a sector that
* comes in different from the checked pass is read again, never written;
* the flash head (SPL, isd_config) is never written;
* 3b. (V10.12) with a "bt.bin" entry (type 200, tools/fm1pkg_make.py): the Bluetooth segment, checked in 2b
* as a whole (its entry's CRC16, its header) and by sector like the app area, written the same way at
* [0xEA000, 0xEA000 + its length) (plain: the XIP window from 0x93000 up is unencrypted), the last sector's
* tail 0xFF; a package without one leaves the region as it is (the app then finds no segment of its build);
* 3c. (seg2) with a "c2.bin" entry (type 201): the second code segment, checked in 2b as the Bluetooth
* segment is and written the same way at [0xC8000, 0xD0000): the whole 32 KiB, its sectors past the segment's
* length 0xFF (an older JIANT's song 7 objects there go); [0xD0000, 0xD8000) and the rest of Felucca's store are
* never written; a package without one leaves the region as it is (the app then finds no segment of its build
* and keeps GEN, the editor and the rest behind bt_hw_seg() off);
* 4. asks 0xF0000000 ("success"), then invalidates the update record (RAM
* and flash: 4K boundary - 256 between 0x93000 and 0xFC000, outside
* Felucca's store, where only user data lives) so the SPL boots the new
* app, and resets.
* Power loss during 3: the record is still there, the SPL runs the loader
* again on the next power-on and the host can resume.
*
* Hooks from the platform (loader.c, ldr_test.c):
* ldr_fread(off, p, n) ldr_erase(off) ldr_prog(off, p, n) flash, 0 = ok
* ldr_record_clear() forget the RAM update record
* ldr_progress(done, total)
* plus the ota.c hooks (frames, time, idle). */
#define LDR_APP_LO 0x4000u
#define LDR_APP_HI 0x93000u
#define LDR_REC_LO 0x93000u /* update records live above the app ... */
#define LDR_REC_HI 0xFC000u /* ... and below Felucca's globals (Felucca's records: 0xE4F00, */
#define LDR_DATA_LO 0x97000u /* the stock firmware's: 0xE8F00), never in Felucca's store, whose */
#define LDR_DATA_HI 0xE0000u /* bytes (the store) the host writes */
#define LDR_NSEC ((LDR_APP_HI - LDR_APP_LO) >> 12)
#define LDR_FL_MAX 0x100000u /* flash.bin: at most the 1 MiB part */
#define LDR_BT_LO 0xEA000u /* (V10.12) the Bluetooth segment (hal/fm1_flash.h FL_EXT_*) */
#define LDR_BT_HI 0xFC000u
#define LDR_BT_TYPE 200u /* its UFW entry, "bt.bin" */
#define LDR_BT_MAGIC 0x3154424Au /* "JBT1": its header's first word (firmware/bt/bt_seg.c bt_head) */
#define LDR_BT_NSEC ((LDR_BT_HI - LDR_BT_LO) >> 12)
#define LDR_C2_LO 0xC8000u /* (seg2) the second code segment (hal/fm1_flash.h FL_SEG2_*) */
#define LDR_C2_HI 0xD0000u
#define LDR_C2_TYPE 201u /* its UFW entry, "c2.bin" */
#define LDR_C2_MAGIC 0x3243424Au /* "JBC2": its header's first word (src/seg2.c c2_head) */
#define LDR_C2_NSEC ((LDR_C2_HI - LDR_C2_LO) >> 12)
static int ldr_fread(uint32_t off, void *p, uint32_t n);
static int ldr_erase(uint32_t off);
static int ldr_prog(uint32_t off, const void *p, uint32_t n);
static void ldr_record_clear(void);
static void ldr_progress(uint32_t done, uint32_t total);
/* SFC cipher: per 32-byte block, key ^ (block offset >> 2) */
static void ldr_sfc(uint8_t *p, uint32_t n, uint32_t base_off, uint32_t key)
{
uint32_t i, j;
for (i = 0; i < n; i += 32u) {
uint32_t k = key ^ ((base_off + i) >> 2);
for (j = 0; j < 32u && i + j < n; j++) {
p[i + j] ^= (uint8_t)k;
k = ((k << 1) ^ (k & 0x8000u ? 0x1021u : 0u)) & 0xFFFFu;
}
}
}
/* chip key from the device's flash head: JLFS top entries (ENC 0xFFFF) -> isd_config.ini blob */
static int ldr_chip_key(uint32_t *key)
{
uint8_t e[32], blob[34];
uint32_t off, i, sum, k;
for (off = 32; off < 0x400u; off += 32u) {
if (ldr_fread(off, e, 32))
return -1;
ota_jl_enc(e, 32);
if (ota_crc16(e + 2, 30, 0) != ota_rd16(e))
return -2;
if (!ota_memeq(e + 16, (const uint8_t *)"isd_config.ini", 15)) {
if (ota_rd16(e + 14))
return -3; /* last entry, not found */
continue;
}
if (ldr_fread(ota_rd32(e + 4), blob, 34) || ota_crc16(blob, 32, 0) != ota_rd16(blob + 32))
return -4;
for (i = 0, sum = 0; i < 16u; i++)
sum += blob[i];
sum &= 0xFFu;
sum = sum >= 0xE0u ? 0xAAu : sum <= 0x10u ? 0x55u : sum;
for (i = 0, k = 0; i < 16u; i++)
if ((uint32_t)(blob[16 + i] ^ blob[15 - i]) < sum)
k |= 1u << i;
*key = k;
return 0;
}
return -3;
}
/* 2b. flash.bin [0, fl_size) from the host, before any erase: its CRC16 must be the UFW entry's (want); sc gets
* the CRC16 of each app sector as it came in */
static int ldr_check(uint32_t fl_off, uint32_t fl_size, uint32_t want, uint16_t *sc, uint8_t *b)
{
uint32_t i, n, crc = 0;
for (i = 0; i < fl_size; i += n) {
n = fl_size - i > 512u ? 512u : fl_size - i;
if (ota_read(fl_off + i, b, n))
return -7;
crc = ota_crc16(b, n, crc);
if (i >= LDR_APP_LO && i < LDR_APP_HI) { /* (512-byte reads from 0: never across a sector) */
uint32_t k = (i - LDR_APP_LO) >> 12;
sc[k] = (uint16_t)ota_crc16(b, n, (i & 0xFFFu) ? sc[k] : 0u);
}
}
return crc == want ? 0 : -12;
}
/* (V10.12) 2b for the Bluetooth segment: [off, off + len) of the package, its CRC16 the entry's (want), its header's
* magic; sc gets each sector's CRC16 with the last one's tail 0xFF, as it is written. (seg2) The second code
* segment's too (magic LDR_C2_MAGIC): end, the bytes written (whole sectors), its sectors past len all 0xFF */
static int ldr_check_bt(uint32_t off, uint32_t len, uint32_t want, uint16_t *sc, uint8_t *b, uint32_t magic, uint32_t end)
{
uint32_t i, n, crc = 0;
for (i = 0; i < len; i += n) {
n = len - i > 512u ? 512u : len - i;
if (ota_read(off + i, b, n))
return -7;
if (!i && ota_rd32(b) != magic)
return magic == LDR_BT_MAGIC ? -15 : -17; /* not a segment */
crc = ota_crc16(b, n, crc);
sc[i >> 12] = (uint16_t)ota_crc16(b, n, (i & 0xFFFu) ? sc[i >> 12] : 0u);
}
for (i = 0; i < 512u; i++)
b[i] = 0xFFu;
for (i = len; i < end; i += n) { /* the last sector's tail (and any sector after) as written: 0xFF */
n = 512u - (i & 511u);
sc[i >> 12] = (uint16_t)ota_crc16(b, n, (i & 0xFFFu) ? sc[i >> 12] : 0u);
}
return crc == want ? 0 : magic == LDR_BT_MAGIC ? -16 : -18;
}
/* 3. one sector: [src, src + n) of the package (n <= 4 KiB, the rest 0xFF) as checked in 2b (want), else read again;
* written at dst unless it is there already, and verified */
static int ldr_sector(uint32_t dst, uint32_t src, uint32_t n, uint32_t want, uint8_t *sec, uint8_t *cur)
{
uint32_t again, k;
for (again = 0;; again++) { /* as checked in 2b, else read it again */
for (k = 0; k < 0x1000u; k += 512u) {
uint32_t m = k >= n ? 0u : n - k > 512u ? 512u : n - k, j;
if (m && ota_read(src + k, sec + k, m))
return -7;
for (j = m; j < 512u; j++)
sec[k + j] = 0xFFu;
}
if (ota_crc16(sec, 0x1000u, 0) == want)
break;
if (again == 2u)
return -13;
}
if (ldr_fread(dst, cur, 0x1000u))
return -8;
if (!ota_memeq(sec, cur, 0x1000u)) {
uint32_t tries;
for (tries = 0; tries < 2u; tries++) {
if (ldr_erase(dst))
return -9;
for (k = 0; k < 0x1000u; k += 256u)
if (ldr_prog(dst + k, sec + k, 256))
return -10;
if (ldr_fread(dst, cur, 0x1000u))
return -8;
if (ota_memeq(sec, cur, 0x1000u))
break;
}
if (tries == 2u)
return -11; /* verify failed twice */
}
return 0;
}
static int ldr_session(void)
{
static uint8_t hdr[0x400], sec[0x1000], cur[0x1000];
static uint16_t sc[LDR_NSEC], bsc[LDR_BT_NSEC], csc[LDR_C2_NSEC];
uint32_t i, k, fl_off, fl_size, ota_off, ota_len, key, s, want = ~0u, done = 0; /* (no CRC16: no flash.bin entry) */
uint32_t bt_off = 0, bt_len = 0, bt_want = 0, c2_off = 0, c2_len = 0, c2_want = 0, total;
int rc;
/* 1. UFW header + entry list (ota.c; it leaves the entries decoded in hdr) */
if ((rc = ota_ufw(hdr, &fl_off, &fl_size, &ota_off, &ota_len)) != 0)
return rc;
if (!fl_off || fl_size < LDR_APP_HI || fl_size > LDR_FL_MAX)
return -3;
for (i = 0; i < ota_rd16(hdr + 8); i++) {
const uint8_t *e = hdr + 0x40 + i * 0x50u;
if (ota_rd16(e) == 0)
want = ota_rd16(e + 4u); /* flash.bin's data CRC16 */
if (ota_rd16(e) == LDR_BT_TYPE && ota_memeq(e + 0x40, (const uint8_t *)"bt.bin", 7)) {
bt_off = ota_rd32(e + 8); /* (V10.12) the Bluetooth segment */
bt_len = ota_rd32(e + 12);
bt_want = ota_rd16(e + 4u);
}
if (ota_rd16(e) == LDR_C2_TYPE && ota_memeq(e + 0x40, (const uint8_t *)"c2.bin", 7)) {
c2_off = ota_rd32(e + 8); /* (seg2) the second code segment */
c2_len = ota_rd32(e + 12);
c2_want = ota_rd16(e + 4u);
}
}
if (bt_off && (bt_len <= 32u || bt_len > LDR_BT_HI - LDR_BT_LO))
return -14;
if (c2_off && (c2_len <= 32u || c2_len > LDR_C2_HI - LDR_C2_LO))
return -19;
total = LDR_APP_HI - LDR_APP_LO + ((bt_len + 0xFFFu) & ~0xFFFu) + (c2_off ? LDR_C2_HI - LDR_C2_LO : 0u);
/* 2. the package's app area must decrypt with this chip's key */
if ((rc = ldr_chip_key(&key)) != 0)
return -40 + rc;
if (ota_read(fl_off + LDR_APP_LO, sec, 32))
return -5;
ldr_sfc(sec, 32, 0, key);
if (ota_crc16(sec + 2, 30, 0) != ota_rd16(sec))
return -6; /* package for another chip key */
/* 2b. the whole package first: nothing is erased for a damaged one */
if ((rc = ldr_check(fl_off, fl_size, want, sc, sec)) != 0)
return rc;
if (bt_off && (rc = ldr_check_bt(bt_off, bt_len, bt_want, bsc, sec, LDR_BT_MAGIC, (bt_len + 0xFFFu) & ~0xFFFu)) != 0)
return rc;
if (c2_off && (rc = ldr_check_bt(c2_off, c2_len, c2_want, csc, sec, LDR_C2_MAGIC, LDR_C2_HI - LDR_C2_LO)) != 0)
return rc;
/* 3. app area, sector by sector */
for (s = LDR_APP_LO; s < LDR_APP_HI; s += 0x1000u) {
if ((rc = ldr_sector(s, fl_off + s, 0x1000u, sc[(s - LDR_APP_LO) >> 12], sec, cur)) != 0)
return rc;
done += 0x1000u;
ldr_progress(done, total);
}
/* 3b. (V10.12) the Bluetooth segment, after the app (a segment that does not match the app is never called) */
for (k = 0; bt_off && k < bt_len; k += 0x1000u) {
if ((rc = ldr_sector(LDR_BT_LO + k, bt_off + k, bt_len - k > 0x1000u ? 0x1000u : bt_len - k, bsc[k >> 12], sec,
cur)) != 0)
return rc;
done += 0x1000u;
ldr_progress(done, total);
}
/* 3c. (seg2) the second code segment, all of its 32 KiB (past its length: erased, 0xFF), after the app */
for (k = 0; c2_off && k < LDR_C2_HI - LDR_C2_LO; k += 0x1000u) {
if ((rc = ldr_sector(LDR_C2_LO + k, c2_off + k, k >= c2_len ? 0u : c2_len - k > 0x1000u ? 0x1000u : c2_len - k,
csc[k >> 12], sec, cur)) != 0)
return rc;
done += 0x1000u;
ldr_progress(done, total);
}
/* 4. finish: the host confirms, the record goes, the SPL boots the app */
for (i = 0; i < 4u; i++)
if (!ota_read(0xF0000000u, sec, 8))
break;
ldr_record_clear();
for (s = LDR_REC_HI; s > LDR_REC_LO; s -= 0x1000u) { /* flash records: 4K boundary - 256 */
uint8_t r[80];
if (s - 0x1000u >= LDR_DATA_LO && s - 0x1000u < LDR_DATA_HI)
continue; /* Felucca's store: user data that may look like one */
if (s - 0x1000u >= 0xEA000u && s - 0x1000u < 0xFC000u)
continue; /* (V10.11) the Bluetooth segment: code, never a record */
if (ldr_fread(s - 0x100u, r, 80))
break;
if (ota_rd16(r + 6) == 0x5441u && ota_rd16(r) && ota_rd16(r) == ota_crc16(r + 2, 78, 0)) {
if (s - 0x1000u >= 0xE7000u && s - 0x1000u < 0xEA000u) { /* (V10.10) the stock VM: the radio's RF trims
* and BT address, the record at its tail (0xE8F00): the sector
* kept but those 256 bytes (was: erased whole, the trims lost) */
if (ldr_fread(s - 0x1000u, cur, 0xF00u))
continue;
if (!ldr_erase(s - 0x1000u))
for (k = 0; k < 0xF00u; k += 256u) /* (a page at a time) */
(void)ldr_prog(s - 0x1000u + k, cur + k, 256);
} else
ldr_erase(s - 0x1000u);
}
}
return 0;
}