mirror of
https://github.com/juanjiant-bit/JIANT.git
synced 2026-12-05 20:34:27 +01:00
hal/fm1_flash_map.h (after Melodee's): the SMK's windows (store A 0x9D000-0xBAFFF, OTA 0xBB000-0xBFFFF, store B USRFLASH 0xC2000-0xE9FFF, store C 0xEB000-0xF3FFF), the marker sector 0xEA000 (whole-sector erase only), and FL_PROTECTED: the head, BTIF / USRTRIM 0xC0000-0xC1FFF, USR + 0xFE000, key_mac. The RAM driver (fm1_flash.h) refuses on FL_GUARD = FL_RANGE_OK && !FL_PROTECTED, also under the loader's own range; on the FM-1 FL_GUARD is FL_RANGE_OK (byte-identical build). storage.c: an SMK st_sector table (78 sectors: 38 A/B pairs, the voice bank, the owner tag 'SMK37DATAJIANT' at 0xAF000), st_owner_check and the stock-mapping marker release (after Melodee's); storage_hw.c releases the marker before the first write into USRFLASH. ota.c: staging 0xBB000 / record 0xBFF00 and a board check (-15 not an SMK flash.bin, -16 region table not the device's, -17 USR place). Loader: app area [0x4000, 0x9D000) and the OTA area only; the record scan never leaves the OTA area; a package whose region table is not the device's is refused (-14). project.c: on the SMK, flash only with FELUCCA_SMK_FLASH=1 and only for JEDEC 0xEB6014 (FL_JEDEC_OK); the default SMK build stays RAM-only. Tests: tests/smk_flash_test.c (every protected sector / page / byte refused by every allow-list and the driver net; straddles, wraps; the table; a simulated SMK: first boot, every object saved and loaded, the marker released once, forbidden erases and programs refused, protected bytes identical), tests/smk_ldr_test.c, ota_test.c SMK build; wired into run_tests.sh. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01UVqsRpEnNJfjWWrH3C4opq
204 lines
9.0 KiB
C
204 lines
9.0 KiB
C
/* SPDX-License-Identifier: GPL-3.0-only
|
|
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
|
|
/* 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;
|
|
* 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). */
|
|
#if BOARD_SMK37
|
|
/* (SMK-37, after Melodee's ldr_core.c; hal/fm1_flash_map.h) the app area is the stock PRCT [0x4000, 0x9D000); the
|
|
* record scan covers only the VM below BTIF [0x9D000, 0xC0000) minus JIANT's store A [0x9D000, 0xBB000): the OTA
|
|
* area. It never reaches BTIF / USRTRIM, USRFLASH, USR or key_mac */
|
|
#define LDR_APP_LO FL_APP_LO
|
|
#define LDR_APP_HI FL_APP_HI
|
|
#define LDR_REC_LO FL_APP_HI
|
|
#define LDR_REC_HI FL_CAL_LO
|
|
#define LDR_DATA_LO FL_DATA_LO
|
|
#define LDR_DATA_HI FL_DATA_HI
|
|
_Static_assert(LDR_APP_HI == 0x9D000u && LDR_REC_HI == 0xC0000u && LDR_DATA_HI == FL_OTA_LO,
|
|
"SMK loader: the app area, and records in the OTA area only");
|
|
#else
|
|
#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 */
|
|
#endif
|
|
#define LDR_NSEC ((LDR_APP_HI - LDR_APP_LO) >> 12)
|
|
#define LDR_FL_MAX 0x100000u /* flash.bin: at most the 1 MiB part */
|
|
|
|
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;
|
|
}
|
|
|
|
static int ldr_session(void)
|
|
{
|
|
static uint8_t hdr[0x400], sec[0x1000], cur[0x1000];
|
|
static uint16_t sc[LDR_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) */
|
|
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++)
|
|
if (ota_rd16(hdr + 0x40 + i * 0x50u) == 0)
|
|
want = ota_rd16(hdr + 0x40 + i * 0x50u + 4u); /* flash.bin's data CRC16 */
|
|
/* 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 */
|
|
#if BOARD_SMK37
|
|
/* (SMK-37) the package's region table (flash 0x4060..0x411F, enciphered) must be this device's: an SMK
|
|
* package on an FM-1, or a package with another layout, is refused before anything is erased */
|
|
if (ota_read(fl_off + FL_RT_OFF, sec, FL_RT_LEN) || ldr_fread(FL_RT_OFF, cur, FL_RT_LEN))
|
|
return -5;
|
|
if (!ota_memeq(sec, cur, FL_RT_LEN))
|
|
return -14;
|
|
#endif
|
|
/* 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;
|
|
/* 3. app area, sector by sector */
|
|
for (s = LDR_APP_LO; s < LDR_APP_HI; s += 0x1000u) {
|
|
uint32_t again;
|
|
for (again = 0;; again++) { /* as checked in 2b, else read it again */
|
|
for (k = 0; k < 0x1000u; k += 512u)
|
|
if (ota_read(fl_off + s + k, sec + k, 512))
|
|
return -7;
|
|
if (ota_crc16(sec, 0x1000u, 0) == sc[(s - LDR_APP_LO) >> 12])
|
|
break;
|
|
if (again == 2u)
|
|
return -13;
|
|
}
|
|
if (ldr_fread(s, cur, 0x1000u))
|
|
return -8;
|
|
if (!ota_memeq(sec, cur, 0x1000u)) {
|
|
uint32_t tries;
|
|
for (tries = 0; tries < 2u; tries++) {
|
|
if (ldr_erase(s))
|
|
return -9;
|
|
for (k = 0; k < 0x1000u; k += 256u)
|
|
if (ldr_prog(s + k, sec + k, 256))
|
|
return -10;
|
|
if (ldr_fread(s, cur, 0x1000u))
|
|
return -8;
|
|
if (ota_memeq(sec, cur, 0x1000u))
|
|
break;
|
|
}
|
|
if (tries == 2u)
|
|
return -11; /* verify failed twice */
|
|
}
|
|
done += 0x1000u;
|
|
ldr_progress(done, LDR_APP_HI - LDR_APP_LO);
|
|
}
|
|
/* 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 (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))
|
|
ldr_erase(s - 0x1000u);
|
|
}
|
|
return 0;
|
|
}
|