mirror of
https://github.com/ruppert/fmini-1.git
synced 2026-12-06 04:44:27 +01:00
The SMK-37 target from github.com/genkigenki/Felucca branch smk37 (commit b67312c on Felucca 1.6.1, = hugelton/Felucca#237; the fm1-to-smk37 kit's Felucca layer), applied to FMini-1, whose firmware/hal and firmware/loader are Felucca 1.6.1's: - BOARD=smk37 / MODEL=pro|elite|mke (tools/build.py, build.sh), the SMK flash map with its protected ranges (fm1_flash_map.h), the update path and its size checks (ota.c, ldr_core.c), the SMK input HAL, LCD and pad LEDs, the owner tag of the store, installers and tests. - Conflicts resolved for FMini-1: packages fmini-1-fm1-<version>.fwsc and fmini-1-smk37[-elite|-mke]-<version>.fwsc; FL_JEDEC_OK keeps the UC25HQ80 (0xB36014) on the FM-1 and accepts it on the SMK too; the official V16 length stays FM-1 only; the sample slots stay gone (FMini-1 has no SAMPLE engine: the layer's eng_sample.c and editor sample hunks are not taken); main.c keeps FMini-1's UI_* hooks with the SMK calls around them. - The FM-1 build is byte-identical to 2.5.0 (app, package and loader hashes). The SMK build does not link yet: FMini-1's preset objects need places in the SMK store, and fmini_app.c's LED code is FM-1 sized. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01QUF6pQ1NkTwmBC8SR8AK94
310 lines
13 KiB
C
310 lines
13 KiB
C
/* SPDX-License-Identifier: GPL-3.0-only
|
|
* Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments */
|
|
/* Host test of the Felucca update loader (firmware/loader/ldr_core.c): a fake
|
|
* host serves a package over the SysEx protocol, the "device" flash starts as
|
|
* another package's flash.bin (as if that firmware were installed).
|
|
* ldr_test OLD.fwsc NEW.fwsc
|
|
* With -DBOARD_SMK37=1 (packages from fm1pkg_make.py --board smk37): the SMK-37 Pro windows (app area
|
|
* up to 0x9D000, records 0x9D000..0xBFFFF) and the protected ranges BTIF / USRTRIM 0xC0000..0xC1FFF,
|
|
* USR 0xF4000..0xFDFFF and key_mac 0xFF000 never touched, even with a valid-looking record in them. */
|
|
#include <stdint.h>
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#define __attribute__(x) /* (the sources' .pool sections: no Mach-O section names) */
|
|
|
|
#if BOARD_SMK37
|
|
#define FELUCCA_ID "ota-SMK-37 Pro_900"
|
|
#define T_APP_HI 0x9D000u /* end of the app area the loader writes */
|
|
#define T_REC 0xBFF00u /* the SMK build's update record (ota.c OTA_RES) */
|
|
#define T_REC2 0xAEF00u /* a record elsewhere in the VM (the stock app's staging is not fixed) */
|
|
#define T_ERASE_LO 0x9D000u /* records: the VM only */
|
|
#define T_ERASE_HI 0xC0000u /* nothing erased at or above (BTIF, USRTRIM, ...) */
|
|
#define T_DATA_LO 0u /* no store range skipped (user samples are in USRFLASH) */
|
|
#define T_DATA_HI 0u
|
|
#define T_USER 0xC2000u /* user sample data (USR1), outside the record window */
|
|
#define T_FL_OTHER 0x93000u /* another board's (the FM-1's) flash.bin size */
|
|
#define T_FL_OTHER_NAME "0x93000"
|
|
#define T_FL_NAME "0x9D000"
|
|
#else
|
|
#define FELUCCA_ID "ota-FM-1_900"
|
|
#define T_APP_HI 0x93000u
|
|
#define T_REC 0xE4F00u
|
|
#define T_REC2 0xE8F00u /* the stock firmware's */
|
|
#define T_ERASE_LO 0x4000u
|
|
#define T_ERASE_HI 0xFC000u
|
|
#define T_DATA_LO 0x97000u
|
|
#define T_DATA_HI 0xE0000u
|
|
#define T_USER 0xA0000u
|
|
#define T_FL_OTHER 0x9D000u /* an SMK-37 Pro package's flash.bin size */
|
|
#define T_FL_OTHER_NAME "0x9D000"
|
|
#define T_FL_NAME "0x93000"
|
|
#endif
|
|
#define FELUCCA_OTA_DRYRUN 0
|
|
|
|
static uint8_t nor[0x100000], *logical;
|
|
static size_t logical_len;
|
|
static uint8_t rx[1024];
|
|
static uint32_t rx_len, rx_full, now_ms, requests, erases, bad_range, record_cleared, f0_asked;
|
|
static uint32_t flip_addr = 0xFFFFFFFFu, flip_seen, flip_on; /* serve addr's flip_on-th reply with a byte changed */
|
|
|
|
static uint32_t pack7(const uint8_t *in, uint32_t n, uint8_t *out)
|
|
{
|
|
uint32_t acc = 0, nb = 0, o = 0;
|
|
while (n--) {
|
|
acc |= (uint32_t)*in++ << nb;
|
|
nb += 8;
|
|
while (nb >= 7) { out[o++] = acc & 0x7F; acc >>= 7; nb -= 7; }
|
|
}
|
|
if (nb) out[o++] = acc & 0x7F;
|
|
return o;
|
|
}
|
|
static uint32_t unpack7h(const uint8_t *in, uint32_t n, uint8_t *out)
|
|
{
|
|
uint32_t acc = 0, nb = 0, o = 0;
|
|
while (n--) {
|
|
acc |= (uint32_t)*in++ << nb;
|
|
nb += 7;
|
|
if (nb >= 8) { out[o++] = (uint8_t)acc; acc >>= 8; nb -= 8; }
|
|
}
|
|
return o;
|
|
}
|
|
|
|
static int ota_wire_send(const uint8_t *p, uint32_t n)
|
|
{
|
|
uint8_t u[64], m[600];
|
|
uint32_t addr, len, i, s = 0;
|
|
unpack7h(p + 1, n - 2, u);
|
|
if (u[2] == 0x11) return 0;
|
|
addr = u[7] | u[8] << 8 | u[9] << 16 | (uint32_t)u[10] << 24;
|
|
len = u[11] | u[12] << 8 | u[13] << 16;
|
|
requests++;
|
|
memcpy(m, "\x00\x59\x30", 3);
|
|
m[3] = (uint8_t)(len + 8); m[4] = (uint8_t)((len + 8) >> 8); m[5] = 0; m[6] = 0;
|
|
memcpy(m + 7, &addr, 4);
|
|
m[11] = (uint8_t)len; m[12] = (uint8_t)(len >> 8); m[13] = 0;
|
|
if (addr >= 0xE0000000u) {
|
|
f0_asked += addr == 0xF0000000u;
|
|
memset(m + 14, 0, len);
|
|
memcpy(m + 14, "success", 8);
|
|
} else {
|
|
if (addr + len > logical_len) { printf("read past the package %#x\n", addr); exit(1); }
|
|
memcpy(m + 14, logical + addr, len);
|
|
if (addr == flip_addr && ++flip_seen == flip_on)
|
|
m[14 + len / 2u] ^= 0x10; /* a transfer error the frame checksum does not see */
|
|
}
|
|
for (i = 6; i < 14 + len; i++) s += m[i];
|
|
m[14 + len] = (uint8_t)~s;
|
|
rx_len = pack7(m, 15 + len, rx);
|
|
rx_full = 1;
|
|
return 0;
|
|
}
|
|
static int ota_frame_get(const uint8_t **p, uint32_t *n) { if (!rx_full) return 0; *p = rx; *n = rx_len; return 1; }
|
|
static void ota_frame_done(void) { rx_full = 0; }
|
|
static uint32_t ota_now_ms(void) { return now_ms; }
|
|
static void ota_idle(void) { now_ms++; }
|
|
static int ota_erase(uint32_t off) { (void)off; return -1; }
|
|
static int ota_prog(uint32_t off, const void *p, uint32_t n) { (void)off; (void)p; (void)n; return -1; }
|
|
static int ota_fread(uint32_t off, void *p, uint32_t n) { memcpy(p, nor + off, n); return 0; }
|
|
static void ota_show(uint32_t step, int32_t code) { (void)step; (void)code; }
|
|
static void ota_commit(const uint8_t *parm) { (void)parm; }
|
|
#include "../firmware/src/ota.c"
|
|
|
|
static int ldr_fread(uint32_t off, void *p, uint32_t n) { memcpy(p, nor + off, n); return 0; }
|
|
static int ldr_erase(uint32_t off)
|
|
{
|
|
if (off < 0x4000u || off >= T_ERASE_HI || (off & 0xFFFu) || (off >= T_DATA_LO && off < T_DATA_HI) ||
|
|
(off >= T_APP_HI && off < T_ERASE_LO)) bad_range++;
|
|
if (off < 0x4000u) { printf("ERASE IN THE HEAD %#x\n", off); exit(1); }
|
|
erases++;
|
|
memset(nor + off, 0xFF, 0x1000);
|
|
return 0;
|
|
}
|
|
static int ldr_prog(uint32_t off, const void *p, uint32_t n)
|
|
{
|
|
const uint8_t *s = p;
|
|
uint32_t i;
|
|
if (off < 0x4000u || off + n > T_APP_HI || ((off & 0xFFu) + n) > 256u) bad_range++;
|
|
for (i = 0; i < n; i++) nor[off + i] &= s[i];
|
|
return 0;
|
|
}
|
|
static void ldr_record_clear(void) { record_cleared++; }
|
|
static void ldr_progress(uint32_t done, uint32_t total) { (void)done; (void)total; }
|
|
#include "../firmware/loader/ldr_core.c"
|
|
|
|
static uint8_t *load_logical(const char *path, size_t *len)
|
|
{
|
|
FILE *f = fopen(path, "rb");
|
|
uint8_t *raw = malloc(0x200000), *lg;
|
|
size_t n, i;
|
|
if (!f) { perror(path); exit(2); }
|
|
n = fread(raw, 1, 0x200000, f);
|
|
fclose(f);
|
|
if (n < 20 * 48 + 0x400) { printf("%s: too short for a package\n", path); exit(2); }
|
|
lg = malloc(n);
|
|
for (i = 0; i < 20; i++) memcpy(lg + i * 47, raw + i * 48, 47);
|
|
memcpy(lg + 20 * 47, raw + 20 * 48, n - 20 * 48);
|
|
*len = n - 20;
|
|
free(raw);
|
|
return lg;
|
|
}
|
|
|
|
static uint32_t flash_off(const uint8_t *lg) /* flash.bin offset in the logical image */
|
|
{
|
|
uint8_t h[0x400];
|
|
uint32_t i, n;
|
|
memcpy(h, lg, sizeof h);
|
|
ota_jl_enc(h, 0x40);
|
|
n = ota_rd16(h + 8);
|
|
for (i = 0; i < n; i++) {
|
|
uint8_t *e = h + 0x40 + i * 0x50u;
|
|
ota_jl_enc(e, 0x50);
|
|
if (ota_rd16(e) == 0) return ota_rd32(e + 8);
|
|
}
|
|
return 0;
|
|
}
|
|
|
|
/* rewrite flash.bin's size in the logical image's UFW entry list (the entries' and the header's CRCs too): a package
|
|
* of the same chip whose app area is another board's size */
|
|
static void set_fl_size(uint8_t *lg, uint32_t size)
|
|
{
|
|
uint8_t h[0x40 + 11 * 0x50];
|
|
uint32_t i, n;
|
|
memcpy(h, lg, sizeof h);
|
|
ota_jl_enc(h, 0x40);
|
|
n = ota_rd16(h + 8);
|
|
for (i = 0; i < n; i++) {
|
|
uint8_t *e = h + 0x40 + i * 0x50u;
|
|
ota_jl_enc(e, 0x50);
|
|
if (ota_rd16(e) == 0)
|
|
ota_wr32(e + 12, size);
|
|
ota_jl_enc(e, 0x50);
|
|
}
|
|
ota_wr16(h + 2, ota_crc16(h + 0x40, n * 0x50u, 0)); /* (over the entries as stored) */
|
|
ota_wr16(h, ota_crc16(h + 2, 0x3E, 0));
|
|
ota_jl_enc(h, 0x40);
|
|
memcpy(lg, h, 0x40 + n * 0x50u);
|
|
}
|
|
|
|
static int check(const char *what, int ok) { printf("%-56s %s\n", what, ok ? "ok" : "FAIL"); return ok ? 0 : 1; }
|
|
|
|
static void put_record_at(uint32_t at) /* a valid update record (the SPL runs the loader) */
|
|
{
|
|
uint8_t r[112] = {0};
|
|
r[2] = 0x0D; r[3] = 0x5A; r[4] = 0x01; r[5] = 0x5A; r[6] = 0x41; r[7] = 0x54;
|
|
ota_wr16(r, ota_crc16(r + 2, 78, 0));
|
|
memcpy(nor + at, r, sizeof r);
|
|
}
|
|
|
|
#if BOARD_SMK37
|
|
/* the SMK's protected ranges: a pattern, and a valid-looking record in BTIF, USRTRIM and USR */
|
|
static const uint32_t PROT[][2] = {{0xC0000u, 0xC2000u}, {0xF4000u, 0xFE000u}, {0xFF000u, 0x100000u}};
|
|
static uint8_t prot_img[sizeof nor];
|
|
static void protect_fill(void)
|
|
{
|
|
uint32_t i, k;
|
|
for (k = 0; k < sizeof PROT / sizeof PROT[0]; k++)
|
|
for (i = PROT[k][0]; i < PROT[k][1]; i++)
|
|
nor[i] = (uint8_t)(i * 13u + 5u);
|
|
memcpy(nor + 0xC1224u, "trimdat", 8);
|
|
put_record_at(0xC1F00u); /* USRTRIM sector: must never be erased */
|
|
put_record_at(0xC0F00u); /* BTIF */
|
|
put_record_at(0xF4F00u); /* USR */
|
|
memcpy(prot_img, nor, sizeof nor);
|
|
}
|
|
static int protect_ok(void)
|
|
{
|
|
uint32_t k;
|
|
for (k = 0; k < sizeof PROT / sizeof PROT[0]; k++)
|
|
if (memcmp(nor + PROT[k][0], prot_img + PROT[k][0], PROT[k][1] - PROT[k][0]))
|
|
return 0;
|
|
return 1;
|
|
}
|
|
#else
|
|
static void protect_fill(void) {}
|
|
static int protect_ok(void) { return 1; }
|
|
#endif
|
|
|
|
int main(int argc, char **argv)
|
|
{
|
|
size_t olen;
|
|
static uint8_t user[0x1000];
|
|
uint8_t *old, head[0x4000];
|
|
uint32_t ofo, nfo, i;
|
|
int bad = 0, rc;
|
|
if (argc < 3) { printf("usage: ldr_test OLD.fwsc NEW.fwsc\n"); return 2; }
|
|
old = load_logical(argv[1], &olen);
|
|
logical = load_logical(argv[2], &logical_len);
|
|
ofo = flash_off(old);
|
|
nfo = flash_off(logical);
|
|
/* device: OLD installed, plus a valid update record at T_REC (Felucca's step 1; FM-1 0xE4F00) and T_REC2 (FM-1:
|
|
* the stock firmware's 0xE8F00, a first install from it); user sample data that looks like a record */
|
|
memset(nor, 0xFF, sizeof nor);
|
|
memcpy(nor, old + ofo, T_APP_HI);
|
|
protect_fill();
|
|
memcpy(head, nor, sizeof head);
|
|
put_record_at(T_REC);
|
|
put_record_at(T_REC2);
|
|
for (i = 0; i < 0x1000u; i++) nor[T_USER + i] = (uint8_t)(i * 7u);
|
|
put_record_at(T_USER + 0xF00u);
|
|
memcpy(user, nor + T_USER, sizeof user);
|
|
/* 1.6.1: flash.bin must be exactly this board's [0, app area end): a package with another board's size (an
|
|
* SMK-37's app area ends at 0x9D000, the FM-1's at 0x93000; one byte short too) is refused before any erase */
|
|
{
|
|
static const uint32_t SZ[2] = {T_FL_OTHER, LDR_APP_HI - 1u};
|
|
uint32_t k;
|
|
for (k = 0; k < 2u; k++) {
|
|
set_fl_size(logical, SZ[k]);
|
|
erases = 0;
|
|
rc = ldr_session();
|
|
bad += check(k ? "flash.bin a byte short of the app area: refused, nothing erased" :
|
|
"another board's size (" T_FL_OTHER_NAME "): refused (-14), nothing erased",
|
|
rc == -14 && erases == 0 && !memcmp(nor + 0x4000, old + ofo + 0x4000, T_APP_HI - 0x4000));
|
|
}
|
|
set_fl_size(logical, LDR_APP_HI); /* back: this board's size, the install below takes it */
|
|
requests = 0;
|
|
}
|
|
/* a damaged package (one byte deep in the app area): refused before anything is erased */
|
|
{
|
|
size_t at = nfo + 0x50123u;
|
|
logical[at] ^= 0x01;
|
|
erases = 0;
|
|
rc = ldr_session();
|
|
bad += check("damaged package (flash.bin CRC) refused, nothing erased", rc == -12 && erases == 0 &&
|
|
!memcmp(nor + 0x4000, old + ofo + 0x4000, T_APP_HI - 0x4000) && nor[T_REC + 6u] == 0x41);
|
|
logical[at] ^= 0x01;
|
|
requests = 0;
|
|
}
|
|
/* a sector that comes in different the second time (a transfer error after the check) is read again */
|
|
flip_addr = nfo + 0x20000u; flip_on = 2; flip_seen = 0;
|
|
rc = ldr_session();
|
|
printf(" rc %d, %u requests, %u sector erases\n", rc, requests, erases);
|
|
bad += check("install completes (flash.bin exactly the app area, " T_FL_NAME ")", rc == 0);
|
|
bad += check("a sector changed in transfer after the check is read again", flip_seen == 3);
|
|
flip_addr = 0xFFFFFFFFu;
|
|
bad += check("app area == the new package's flash.bin", !memcmp(nor + 0x4000, logical + nfo + 0x4000, T_APP_HI - 0x4000));
|
|
bad += check("flash head [0, 0x4000) untouched", !memcmp(nor, head, sizeof head));
|
|
bad += check("finish asked (0xF0000000)", f0_asked >= 1);
|
|
bad += check("update records cleared (RAM + flash: ours, the other one)",
|
|
record_cleared && nor[T_REC + 6u] == 0xFF && nor[T_REC2 + 6u] == 0xFF);
|
|
bad += check("user sample data that looks like a record is kept", !memcmp(nor + T_USER, user, sizeof user));
|
|
bad += check("no write outside the allowed windows", bad_range == 0);
|
|
/* same package again: nothing to erase */
|
|
erases = 0; requests = 0;
|
|
rc = ldr_session();
|
|
bad += check("re-run with the same package erases nothing", rc == 0 && erases == 0);
|
|
/* a package for another chip key is refused before any erase */
|
|
{
|
|
uint8_t save = logical[nfo + 0x4000 + 5];
|
|
logical[nfo + 0x4000 + 5] ^= 0x5A; /* app area head no longer decrypts */
|
|
memcpy(nor, old + ofo, T_APP_HI);
|
|
erases = 0;
|
|
rc = ldr_session();
|
|
bad += check("foreign key / damaged app head refused, nothing erased", rc == -6 && erases == 0);
|
|
logical[nfo + 0x4000 + 5] = save;
|
|
}
|
|
bad += check("protected ranges untouched (SMK: BTIF, USRTRIM, USR, key_mac)", protect_ok());
|
|
printf("%s\n", bad ? "LOADER TEST FAILED" : "loader test passed");
|
|
return bad != 0;
|
|
}
|