diff --git a/firmware/hal/fm1_flash.h b/firmware/hal/fm1_flash.h index 36c9e58a..49e7a594 100644 --- a/firmware/hal/fm1_flash.h +++ b/firmware/hal/fm1_flash.h @@ -35,6 +35,11 @@ static inline void *fl_far(void *p) { void *volatile q = p; return q; } #define T4_CNT REG32(0x10804u) /* TIMER4, 24 MHz free-running */ #define FL_XIP(off) FM1_XIP(off) /* fm1_xip.h */ +#if BOARD_SMK37 +/* (SMK-37) the SMK's map, its windows and its guards (FL_STORE_OK, FL_RANGE_OK, FL_PROTECTED, FL_GUARD) */ +#include "fm1_flash_map.h" +#else +#define FL_PLAIN_LO 0x00093000u /* the plain XIP window: from the app area's end */ #define FL_DATA_LO 0x00097000u /* Felucca main store */ #define FL_DATA_HI 0x000E0000u #define FL_GLOB_LO 0x000FC000u /* Felucca superblock / globals */ @@ -57,6 +62,8 @@ static inline void *fl_far(void *p) { void *volatile q = p; return q; } #ifndef FL_RANGE_OK #define FL_RANGE_OK(off, n) (FL_STORE_OK(off, n) || FL_IN(off, n, FL_OTA_LO, FL_OTA_HI)) #endif +#define FL_GUARD(off, n) FL_RANGE_OK(off, n) /* (the SMK's adds FL_PROTECTED: fm1_flash_map.h) */ +#endif /* BOARD_SMK37 */ typedef struct { uint32_t sfc_con, spi_con; } fl_saved_t; @@ -171,7 +178,7 @@ static RAMFN uint32_t fl_status_ram(void) /* SR1 | SR2 << 8 */ static RAMFN int fl_erase4k_ram(uint32_t off, uint32_t *took_us) { fl_saved_t s; int rc; - if (!FL_RANGE_OK(off, 0x1000u) || (off & 0xFFFu)) + if (!FL_GUARD(off, 0x1000u) || (off & 0xFFFu)) return -1; if (fl_enter(&s)) return -2; fl_wren(); @@ -188,7 +195,7 @@ static RAMFN int fl_erase4k_ram(uint32_t off, uint32_t *took_us) static RAMFN int fl_prog_ram(uint32_t off, const uint8_t *src, uint32_t n, uint32_t *took_us) { fl_saved_t s; int rc; uint32_t i; - if (!FL_RANGE_OK(off, n) || + if (!FL_GUARD(off, n) || n == 0 || n > 256u || ((off & 0xFFu) + n) > 256u) return -1; /* one page, no wrap */ if (fl_enter(&s)) return -2; fl_wren(); @@ -222,11 +229,11 @@ static RAMFN int fl_read_ram(uint32_t off, uint8_t *dst, uint32_t n) /* 0x0B f static void fl_plain_window_init(void) { SFCENC_CON &= (uint8_t)~2u; - SFCENC_UNENC_L = FL_XIP(0x93000u); /* 0x0208F000 */ + SFCENC_UNENC_L = FL_XIP(FL_PLAIN_LO); /* 0x0208F000 (the SMK's: 0x02099000) */ SFCENC_UNENC_H = 0x07FFFFFFu; SFCENC_CON |= 2u; __asm__ volatile("cli" ::: "memory"); - fl_inval(0x93000u, 0x100000u - 0x93000u); /* drop any decrypted lines */ + fl_inval(FL_PLAIN_LO, 0x100000u - FL_PLAIN_LO); /* drop any decrypted lines */ __asm__ volatile("csync\n\tsti" ::: "memory"); } diff --git a/firmware/hal/fm1_flash_map.h b/firmware/hal/fm1_flash_map.h new file mode 100644 index 00000000..ae91fa4a --- /dev/null +++ b/firmware/hal/fm1_flash_map.h @@ -0,0 +1,102 @@ +/* SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) + * Adapted from Melodee's firmware/hal/fm1_flash_map.h: + * Copyright (C) 2026 Kerem Kilic (Ellic Studio) + * Portions Copyright (C) 2026 Leo Kuroshita (@kurogedelic), Hügelton Instruments + * SMK-37 port: Copyright (C) 2026 M. Geisler (genkigenki/makkuro) + * Hardware notes: https://github.com/genkigenki/fm1-to-smk37 */ +/* The SMK-37's flash map (BOARD_SMK37, fm1_cc.h): which ranges the RAM flash driver (fm1_flash.h) may erase / + * program, as macros and compile-time checks only, so the host tests include it (tests/smk_flash_test.c). The FM-1 + * keeps its own map in fm1_flash.h, unchanged. + * + * 0x00000-0x03FFF head (SPL, isd_config) NEVER + * 0x04000-0x9CFFF app area (the stock PRCT) the update loader only (FL_APP) + * 0x9D000-0xBAFFF VM: JIANT's store A (storage.c; the owner tag at 0xAF000) FL_DATA + * 0xBB000-0xBFFFF VM: M-UPGRADE loader staging + the update record at 0xBFF00 (ota.c) FL_OTA + * 0xC0000-0xC0FFF BTIF (BT pairing) NEVER + * 0xC1000-0xC1FFF USRTRIM (the keys' / pads' factory calibration "trimdat") NEVER + * 0xC2000-0xE9FFF USRFLASH: JIANT's store B (the stock mapping until JIANT first writes there) FL_UFL + * 0xEA000-0xEAFFF USRFLASH's tail: the stock end marker "usrdata\0" at 0xEAFF8; never data, only erased whole, + * once, before the first write into FL_UFL (FL_STOCKTAG; storage.c st_stock_release) + * 0xEB000-0xF3FFF unlisted: JIANT's store C FL_GAP + * 0xF4000-0xFDFFF USR (the stock resources; kept for a return to stock) NEVER + * 0xFE000-0xFEFFF free, unused NEVER (not in any window) + * 0xFF000-0xFFFFF key_mac NEVER + * Everything is an explicit allow-list; the asserts keep each window out of the protected ranges at compile time, + * and FL_PROTECTED is a second, independent net inside the RAM driver (FL_GUARD), also under the update loader's + * own FL_RANGE_OK. */ +#pragma once +#include +#include "fm1_cc.h" + +#if BOARD_SMK37 +/* [off, off + n) inside [lo, hi), without wrapping (as fm1_flash.h's): off + n can overflow, and the 1 MiB part + * ignores the high address bits, so a wrapped range lands low. */ +#define FL_IN(off, n, lo, hi) ((uint32_t)(off) >= (lo) && (uint32_t)(off) <= (hi) && \ + (uint32_t)(n) <= (hi) - (uint32_t)(off)) +/* The flash chip the SMK build stores on: the SMK-37's own 1 MiB part (0xEB6014) and nothing else; the driver uses + * only generic SPI NOR commands (9F, 05/35, 06, 20, 02, 0B). Any other id: RAM-only (project.c persist_boot). */ +#define FL_JEDEC_OK(id) ((uint32_t)(id) == 0xEB6014u) + +#define FL_HEAD_HI 0x00004000u /* protected: [0, FL_HEAD_HI) */ +#define FL_CAL_LO 0x000C0000u /* protected: BTIF + USRTRIM */ +#define FL_CAL_HI 0x000C2000u +#define FL_USRRES_LO 0x000F4000u /* protected: the stock USR resources ... */ +#define FL_USRRES_HI 0x000FF000u /* ... and the free sector 0xFE000 above them */ +#define FL_KEYMAC_LO 0x000FF000u /* protected: key_mac and above */ +#define FL_APP_LO 0x00004000u /* the update loader: the app area */ +#define FL_APP_HI 0x0009D000u +#define FL_PLAIN_LO 0x0009D000u /* the plain XIP window from the VM's start */ +#define FL_DATA_LO 0x0009D000u /* JIANT's store A */ +#define FL_DATA_HI 0x000BB000u +#define FL_OTA_LO 0x000BB000u /* M-UPGRADE loader staging, ota.c */ +#define FL_OTA_HI 0x000C0000u +#define FL_UFL_LO 0x000C2000u /* JIANT's store B: USRFLASH up to the marker's sector */ +#define FL_UFL_HI 0x000EA000u +#define FL_GAP_LO 0x000EB000u /* JIANT's store C: the unlisted sectors below USR */ +#define FL_GAP_HI 0x000F4000u +/* USRFLASH holds the stock firmware's key / knob mapping (global settings 0xC2000, 8 presets from 0xC3000) with its + * end marker "usrdata\0" at 0xEAFF8: while the marker is there the stock firmware trusts the area and never rebuilds + * it. So before the first write into USRFLASH the marker's sector is erased (storage.c st_stock_release), and a + * return to stock rebuilds the factory mapping. Exactly that one sector, whole, and never as data. */ +#define FL_STOCKTAG_LO 0x000EA000u +#define FL_STOCKTAG_HI 0x000EB000u +#define FL_STOCKTAG_OFF 0x000EAFF8u +#define FL_STOCKTAG_OK(off, n) ((uint32_t)(off) == FL_STOCKTAG_LO && (uint32_t)(n) == FL_STOCKTAG_HI - FL_STOCKTAG_LO) +/* a window [lo, hi) is allowed: sector aligned, non-empty, and clear of every protected range */ +#define FL_WIN_SAFE(lo, hi) ((lo) % 0x1000u == 0u && (hi) % 0x1000u == 0u && (lo) < (hi) && \ + (lo) >= FL_HEAD_HI && (hi) <= FL_KEYMAC_LO && \ + ((hi) <= FL_CAL_LO || (lo) >= FL_CAL_HI) && \ + ((hi) <= FL_USRRES_LO || (lo) >= FL_USRRES_HI)) +_Static_assert(FL_WIN_SAFE(FL_DATA_LO, FL_DATA_HI), "SMK: FL_DATA overlaps a protected range"); +_Static_assert(FL_WIN_SAFE(FL_OTA_LO, FL_OTA_HI), "SMK: FL_OTA overlaps a protected range"); +_Static_assert(FL_WIN_SAFE(FL_UFL_LO, FL_UFL_HI), "SMK: FL_UFL overlaps a protected range"); +_Static_assert(FL_WIN_SAFE(FL_GAP_LO, FL_GAP_HI), "SMK: FL_GAP overlaps a protected range"); +_Static_assert(FL_WIN_SAFE(FL_APP_LO, FL_APP_HI), "SMK: FL_APP overlaps a protected range"); +_Static_assert(FL_WIN_SAFE(FL_STOCKTAG_LO, FL_STOCKTAG_HI) && FL_STOCKTAG_LO == FL_UFL_HI && + FL_STOCKTAG_HI == FL_GAP_LO && FL_STOCKTAG_OFF + 8u == FL_STOCKTAG_HI, + "SMK: the stock marker's sector lies between FL_UFL and FL_GAP, in neither"); +_Static_assert(FL_UFL_LO == FL_CAL_HI && FL_GAP_HI == FL_USRRES_LO, "SMK: FL_UFL / FL_GAP bounds"); +_Static_assert(FL_OTA_HI == FL_CAL_LO && FL_DATA_HI == FL_OTA_LO && FL_APP_HI == FL_DATA_LO, + "SMK: the windows A, OTA and the app area meet, in that order, below BTIF"); +/* the update loader (loader.c): the app area, and the update record's sectors (the OTA area) only */ +#define FL_LDR_RANGE_OK(off, n) (FL_IN(off, n, FL_APP_LO, FL_APP_HI) || FL_IN(off, n, FL_OTA_LO, FL_OTA_HI)) +/* JIANT's own store: FL_DATA, FL_UFL and FL_GAP (never the marker's sector) */ +#define FL_STORE_OK(off, n) (FL_IN(off, n, FL_DATA_LO, FL_DATA_HI) || FL_IN(off, n, FL_UFL_LO, FL_UFL_HI) || \ + FL_IN(off, n, FL_GAP_LO, FL_GAP_HI)) +#ifndef FL_RANGE_OK +#define FL_RANGE_OK(off, n) (FL_STORE_OK(off, n) || FL_IN(off, n, FL_OTA_LO, FL_OTA_HI) || FL_STOCKTAG_OK(off, n)) +#endif +/* the second, independent net in the RAM driver: [off, off + n) touches a protected range (also when a caller + * supplies its own FL_RANGE_OK, as the update loader does) */ +#define FL_HITS(off, n, lo, hi) ((uint32_t)(off) < (hi) && \ + ((uint32_t)(off) >= (lo) || (uint32_t)(n) > (lo) - (uint32_t)(off))) +#define FL_PROTECTED(off, n) (FL_HITS(off, n, 0u, FL_HEAD_HI) || FL_HITS(off, n, FL_CAL_LO, FL_CAL_HI) || \ + FL_HITS(off, n, FL_USRRES_LO, FL_USRRES_HI) || (uint32_t)(off) >= FL_KEYMAC_LO || \ + (uint32_t)(n) > FL_KEYMAC_LO - (uint32_t)(off)) +#define FL_GUARD(off, n) (FL_RANGE_OK(off, n) && !FL_PROTECTED(off, n)) +/* the stock region table in flash.bin and in the device (app area offset 0x60: VM, PRCT, BTIF, USRTRIM, USRFLASH, + * USR, SFC-enciphered with the chip key): ota.c and ldr_core.c refuse a package whose table is not the device's */ +#define FL_RT_OFF 0x00004060u +#define FL_RT_LEN 0x000000C0u +#endif /* BOARD_SMK37 */ diff --git a/firmware/loader/ldr_core.c b/firmware/loader/ldr_core.c index 309af643..c77eb408 100644 --- a/firmware/loader/ldr_core.c +++ b/firmware/loader/ldr_core.c @@ -28,12 +28,26 @@ * 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 */ @@ -127,6 +141,14 @@ static int ldr_session(void) 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; diff --git a/firmware/loader/loader.c b/firmware/loader/loader.c index aca0f2d1..0d357e4a 100644 --- a/firmware/loader/loader.c +++ b/firmware/loader/loader.c @@ -21,9 +21,16 @@ #define RING_PUBLISH() __asm__ volatile("" ::: "memory") #include "fm1_time.h" #include "fm1_sys.h" +#if BOARD_SMK37 +/* (SMK-37) the loader may write the app area [0x4000, 0x9D000) and erase update records in the OTA area + * [0xBB000, 0xC0000) only (hal/fm1_flash_map.h FL_LDR_RANGE_OK); the RAM driver's FL_PROTECTED refuses the head, + * BTIF / USRTRIM, USR and key_mac on top of it */ +#define FL_RANGE_OK(off, n) FL_LDR_RANGE_OK(off, n) +#else /* the loader may write the app area and erase update records; never the head, nor Felucca's store * [0x97000, 0xE0000) (user data, never a record: ldr_core.c) */ #define FL_RANGE_OK(off, n) (FL_IN(off, n, 0x4000u, 0x97000u) || FL_IN(off, n, 0xE0000u, 0xFC000u)) +#endif #include "fm1_flash.h" #include "../src/libc.c" #include "../src/usb.c" diff --git a/firmware/src/felucca.c b/firmware/src/felucca.c index 78b06d99..398876f8 100644 --- a/firmware/src/felucca.c +++ b/firmware/src/felucca.c @@ -50,6 +50,13 @@ #ifndef FELUCCA_VERSION #define FELUCCA_VERSION "v0.9.18" /* JIANT's (based on Felucca 1.1.5.1): the splash, the menu, the console, the editor */ #endif +#ifndef FELUCCA_SMK_FLASH +#define FELUCCA_SMK_FLASH 0 /* (SMK-37, docs/SMK37.md) 1: settings / projects / updates on the SMK's flash (its + * own part 0xEB6014 only, the SMK map); 0: RAM-only, nothing is ever written */ +#endif +#if FELUCCA_SMK_FLASH && !BOARD_SMK37 +#error "FELUCCA_SMK_FLASH is the SMK-37's" +#endif #if FELUCCA_OTA && !FELUCCA_FLASH #error "FELUCCA_OTA needs FELUCCA_FLASH" #endif diff --git a/firmware/src/fm6_vbank.c b/firmware/src/fm6_vbank.c index efaec7e0..0309a24e 100644 --- a/firmware/src/fm6_vbank.c +++ b/firmware/src/fm6_vbank.c @@ -23,7 +23,11 @@ * fvb_boot finishes the copy at the next power-on, before any autosave can take that sector): never half of one. A * wrong CRC commits nothing. An autosave between two requests (it waits 10 s after the last, project.c) takes the * sector: WRITE and END find its commit record there and answer 5 (start again). Included by upreset.c. */ +#if BOARD_SMK37 +#define FVB_HOME ST_SMK_VBANK /* (SMK-37: store A, storage.c) */ +#else #define FVB_HOME 0xE7000u +#endif #define FVB_INFO 224u /* the bank's header; the voices from FVB_VOICES */ #define FVB_VOICES 256u #define FVB_MAGIC 0x56364D46u /* "FM6V" */ diff --git a/firmware/src/ota.c b/firmware/src/ota.c index f8766122..bf6746fa 100644 --- a/firmware/src/ota.c +++ b/firmware/src/ota.c @@ -26,12 +26,18 @@ /* (SMK-37, after Melodee's ota.c) the stock app's parm identity (Pro 18 B, Elite 20 B) */ #define OTA_PARM_ID "ota-" SMK_MODEL "_016" _Static_assert(sizeof OTA_PARM_ID - 1u <= 20u, "SMK: the parm identity ends before +0x1C"); +/* the last 5 sectors of the stock VM (0xBB000..0xBFFFF, hal/fm1_flash_map.h FL_OTA), below BTIF / USRTRIM + * 0xC0000..0xC1FFF; the update loader may erase records only there (ldr_core.c) */ +#define OTA_AREA 0x000BB000u /* staged loader, 5 x 4 KiB */ +#define OTA_AREA_LEN 0x5000u +#define OTA_RES (OTA_AREA + OTA_AREA_LEN - 0x100u) /* 0xBFF00 = 4K boundary - 256: SPL scan */ +_Static_assert(OTA_AREA + OTA_AREA_LEN <= 0xC0000u && OTA_AREA >= 0x9D000u, "SMK: OTA area outside the VM"); #else #define OTA_PARM_ID "ota-FM-1_015" -#endif #define OTA_AREA 0x000E0000u /* staged loader, 5 x 4 KiB */ #define OTA_AREA_LEN 0x5000u #define OTA_RES (OTA_AREA + OTA_AREA_LEN - 0x100u) /* 0xE4F00 = 4K boundary - 256: SPL scan */ +#endif #define OTA_LDR_HCRC 0xEBAAu /* official update loader (usb_hid_ota.bin) */ #define OTA_LDR_DCRC 0x5881u #define OTA_LDR_LEN 0x4DE1u @@ -262,6 +268,38 @@ static int ota_ufw(uint8_t *hdr, uint32_t *fl_off, uint32_t *fl_len, uint32_t *o return 0; } +#if BOARD_SMK37 +/* (SMK-37, after Melodee's ota.c) the board check, before anything is erased. The FM-1's official package has the + * same head and the same official update loader as the SMK-37's, so the head check alone lets it through; its loader + * then writes the FM-1's layout: the factory calibration (BTIF / USRTRIM 0xC0000-0xC1FFF) erased, the FM-1's USR + * image over the SMK's. So a package must be laid out for the SMK-37: + * -15 flash.bin is not the SMK's app area, 0x9D000 bytes (the FM-1's is 0x93000), either loader + * -16 the package's region table (flash 0x4060-0x411F: VM, PRCT, BTIF, USRTRIM, USRFLASH, USR) is not this + * keyboard's, either loader (JIANT's own loader checks it again, ldr_core.c). Both are SFC-enciphered at the + * same offset with the same chip key, so equal bytes mean an equal table, and any other key differs. + * -17 official loader: a USR image (UFW entry 50) for another place than 0xF4000 / 0xA000 + * The screen shows FAILED -15 / -16 / -17; the staging area is not touched. pk, dv: 0xC0 bytes of scratch each */ +#define OTA_E_FL_LEN (-15) +#define OTA_E_USR (-17) +static int ota_board_check(const uint8_t *hdr, uint32_t fl_off, uint32_t fl_len, uint32_t official, uint8_t *pk, + uint8_t *dv) +{ + uint32_t i, nent = ota_rd16(hdr + 8); /* hdr: entries deciphered by ota_ufw */ + if (fl_len != FL_APP_HI) + return OTA_E_FL_LEN; + if (ota_read(fl_off + FL_RT_OFF, pk, FL_RT_LEN) || ota_fread(FL_RT_OFF, dv, FL_RT_LEN)) + return -4; + if (!ota_memeq(pk, dv, FL_RT_LEN)) + return -16; + for (i = 0; official && i < nent && i < 11u; i++) { + const uint8_t *e = hdr + 0x40 + i * 0x50u; + if (ota_rd16(e) == 50u && (ota_rd32(e + 0x1C) != 0xF4000u || ota_rd32(e + 0x20) != 0xA000u)) + return OTA_E_USR; + } + return 0; +} +#endif + static int ota_stage(void) /* steps 1..6; 0 = host said success */ { static uint8_t hdr[0x400], b[512], mine[512]; @@ -287,6 +325,10 @@ static int ota_stage(void) /* steps 1..6; 0 = host said su own = ota_memeq(b + i, (const uint8_t *)"FELUCCA-LOADER-1", 16) && ota_rd32(b + 0x20 + 4) <= 0x14000u; if (!official && !own) return -7; +#if BOARD_SMK37 + if ((rc = ota_board_check(hdr, fl_off, fl_len, official, b, mine)) != 0) + return rc; /* another board's package: nothing staged, nothing erased */ +#endif if (official) { /* the official loader writes the head: it must equal ours */ ota_show(2, 0); for (i = 0; i < 0x4000u; i += 512u) { @@ -352,6 +394,9 @@ static int ota_session(void) ota_deadline = ota_now_ms() + 120000u; /* step 1 normally takes ~5 s; a stalled host gives up after 2 min */ rc = ota_stage(); if (rc) { +#if BOARD_SMK37 + if (rc > OTA_E_FL_LEN || rc < OTA_E_USR) /* (refused by the board check: nothing to erase) */ +#endif ota_unstage(); ota_show(9, rc); return rc; diff --git a/firmware/src/project.c b/firmware/src/project.c index 0b31f2fd..9c208f0c 100644 --- a/firmware/src/project.c +++ b/firmware/src/project.c @@ -2255,10 +2255,24 @@ static void persist_boot(void) /* before settings_init / pane #if FELUCCA_FLASH persist_t p; uint32_t f = irq_save(); +#if BOARD_SMK37 + /* (SMK-37, docs/SMK37.md) the store only with FELUCCA_SMK_FLASH=1 and only on the SMK's own part (0xEB6014, + * hal/fm1_flash_map.h FL_JEDEC_OK), in the SMK's windows; else RAM-only, as JIANT on any unknown part */ + uint32_t id = FL_FAR(fl_jedec_ram)(); + flash_ok = FELUCCA_SMK_FLASH && FL_JEDEC_OK(id); +#else flash_ok = FL_FAR(fl_jedec_ram)() == 0x856014u; /* the expected 1 MiB part, else stay RAM-only */ +#endif irq_restore(f); if (!flash_ok) return; +#if BOARD_SMK37 + if (st_owner_check() < 0) { /* never parse another ported firmware's store (storage.c): */ + flash_ok = 0; /* not cleared, so RAM-only until the next boot */ + st_foreign = 1; + return; + } +#endif { int n = st_load(OBJ_SETTINGS, &p, sizeof p); if (settings_import(&p, n)) { diff --git a/firmware/src/storage.c b/firmware/src/storage.c index 25f58f78..584ee6f8 100644 --- a/firmware/src/storage.c +++ b/firmware/src/storage.c @@ -68,6 +68,139 @@ static uint32_t st_crc32_add(uint32_t c, const void *p, uint32_t n) /* zlib CR } static uint32_t st_crc32(const void *p, uint32_t n) { return ~st_crc32_add(0xFFFFFFFFu, p, n); } +#if BOARD_SMK37 +/* (SMK-37, hal/fm1_flash_map.h) the stock region table keeps the head, BTIF / USRTRIM 0xC0000..0xC1FFF (the keys' + * factory calibration), USR 0xF4000..0xFDFFF and key_mac, and its app area runs to 0x9D000; JIANT's objects are the + * FM-1's, only their places differ, in three windows (after Melodee's store; 78 of its 79 sectors): + * store A, FL_DATA 0x9D000..0xBAFFF (the stock VM): 0x9D000 the settings pair; 0x9F000..0xA6FFF the four projects + * (song 1); 0xA7000 / 0xA8000 the user presets' FM6 patches (OBJ_UPFM6); 0xA9000 / 0xAA000 the autosave; + * 0xAB000 the FM6 voice bank (fm6_vbank.c FVB_HOME); 0xAC000 / 0xAD000 the song index; 0xAE000 (A) + 0xBA000 (B) + * song 2's section 4; 0xAF000 the store owner tag (st_owner_check); 0xB0000..0xB3FFF the user preset banks; + * 0xB4000..0xB9FFF song 2's sections 1..3 + * store B, FL_UFL 0xC2000..0xE9FFF (USRFLASH below its end marker): songs 3..7 (20 pairs) + * 0xEA000 the stock end marker's sector: never data (FL_STOCKTAG, only erased whole as the release below) + * store C, FL_GAP 0xEB000..0xF3FFF (unlisted, below USR): song 8 (4 pairs); 0xF3000 spare + * tests/smk_flash_test.c checks every object's pair against the windows, the tag and each other. */ +#define ST_SMK_TAG 0xAF000u +#define ST_SMK_VBANK 0xAB000u /* (fm6_vbank.c FVB_HOME) */ +static uint32_t st_sector(uint32_t obj, uint32_t copy) /* flash offset of copy A (0) / B (1) */ +{ + if (obj == OBJ_SETTINGS) + return 0x9D000u + copy * ST_SECTOR; + if (obj == OBJ_FM6BANK || obj == OBJ_UPFM6) /* (the same pair, as the FM-1's) */ + return 0xA7000u + copy * ST_SECTOR; + if (obj == OBJ_AUTOSAVE) + return 0xA9000u + copy * ST_SECTOR; + if (obj == OBJ_SONGIDX) + return 0xAC000u + copy * ST_SECTOR; + if (obj >= OBJ_SONG1) { + uint32_t j = obj - OBJ_SONG1; /* (song - 2) * 4 + section, 0..27 */ + if (j < 3u) + return 0xB4000u + j * 2u * ST_SECTOR + copy * ST_SECTOR; + if (j == 3u) + return copy ? 0xBA000u : 0xAE000u; + if (j < 24u) + return FL_UFL_LO + (j - 4u) * 2u * ST_SECTOR + copy * ST_SECTOR; + return FL_GAP_LO + (j - 24u) * 2u * ST_SECTOR + copy * ST_SECTOR; + } + if (obj >= OBJ_UPRESET0) + return 0xB0000u + (obj - OBJ_UPRESET0) * 2u * ST_SECTOR + copy * ST_SECTOR; + return 0x9F000u + (obj - OBJ_PROJECT0) * 2u * ST_SECTOR + copy * ST_SECTOR; +} +_Static_assert(OBJ_COUNT - OBJ_SONG1 == 28u && OBJ_UPRESET0 - OBJ_PROJECT0 == 4u && OBJ_FM6BANK - OBJ_UPRESET0 == 2u, + "SMK store: st_sector's table is laid out for 4 projects, 2 preset banks and 28 song objects"); +_Static_assert(FL_UFL_LO + 20u * 2u * 0x1000u == FL_UFL_HI && FL_GAP_LO + 4u * 2u * 0x1000u < FL_GAP_HI && + ST_SMK_TAG >= FL_DATA_LO && ST_SMK_TAG < FL_DATA_HI, "SMK store: the windows and the table disagree"); + +/* The owner of the store (after Melodee's storage.c st_owner_check). Several FM-1 firmwares are ported to the SMK-37 + * with the same store windows, each with its own formats: a firmware must never parse another's data. The sector + * 0xAF000 (the same in every port) holds "SMK37DATA" + the firmware's family (JIANT). Ours: load as ever. Erased or + * another family's: clear JIANT's whole store (A, B unless the stock mapping is intact there, and C) and take it. + * BTIF, USRTRIM, USR, key_mac and the head lie outside the three windows: never touched. */ +#define ST_DATA_FAMILY "JIANT" +#ifndef ST_IDLE +#define ST_IDLE() ((void)0) /* (the firmware: the watchdog, storage_hw.c) */ +#endif +/* The stock firmware's mapping in USRFLASH (global settings at 0xC2000, magic A3 3A; 8 presets from 0xC3000 every + * 0x5000, the rest erased; the end marker "usrdata\0" at 0xEAFF8, FL_STOCKTAG): the stock firmware trusts the area + * while the marker is there and never rebuilds it, so after a return to stock JIANT's store B would be read as its + * key mapping. So an intact stock mapping is kept (its sectors read as empty objects: no JIANT commit record), and + * the marker is released (its sector erased: the stock firmware rebuilds its factory mapping at its next start) + * before the first erase or program in USRFLASH (storage_hw.c) and at boot when it stands over anything else. */ +static int st_stock_tagged(void) +{ + static const uint8_t TAG[8] = {'u', 's', 'r', 'd', 'a', 't', 'a', 0}; + uint8_t t[8]; + uint32_t i; + if (st_read(FL_STOCKTAG_OFF, t, 8)) + return 0; + for (i = 0; i < 8u; i++) + if (t[i] != TAG[i]) + return 0; + return 1; +} +static int st_stock_intact(void) /* the marker over the stock layout */ +{ + uint8_t b[8]; + uint32_t n, k, i, ff; + if (!st_stock_tagged() || st_read(FL_UFL_LO, b, 2) || b[0] != 0xA3u || b[1] != 0x3Au) + return 0; + for (n = 0; n < 8u; n++) /* each preset: its head written, its last two sectors erased */ + for (k = 0; k < 3u; k++) { + if (st_read(FL_UFL_LO + 0x1000u + n * 0x5000u + (k ? 0x2000u + k * 0x1000u : 0u), b, 8)) + return 0; + for (ff = 1, i = 0; i < 8u; i++) + ff &= b[i] == 0xFFu; + if (ff != (k != 0u)) + return 0; + } + return 1; +} +#ifndef ST_ERASE_MARKER +#define ST_ERASE_MARKER() st_erase(FL_STOCKTAG_LO) /* (host tests; the firmware: storage_hw.c, the raw erase) */ +#endif +static void st_stock_release(void) /* erase the marker's sector, once: later calls find no marker */ +{ + if (st_stock_tagged()) + (void)ST_ERASE_MARKER(); +} +static int st_in_usrflash(uint32_t off) { return off >= FL_UFL_LO && off < FL_STOCKTAG_LO; } +static uint8_t st_foreign __attribute__((unused)); /* the store could not be cleared: RAM-only this boot (persist_boot) */ +static int st_owner_check(void) /* 0 ours; 1 another firmware's (or untagged) store was cleared; -1 an + * erase failed: not tagged as ours, so the next boot clears it again */ +{ + static const char FAM[] = ST_DATA_FAMILY; + uint8_t tag[16], want[16]; + uint32_t i, ours = 1, s, keep, bad = 0; + for (i = 0; i < 16u; i++) + want[i] = i < 9u ? (uint8_t)"SMK37DATA"[i] : i - 9u < sizeof FAM - 1u ? (uint8_t)FAM[i - 9u] : 0u; + if (!st_stock_intact()) + st_stock_release(); /* the marker over anything but the stock mapping: let it rebuild */ + if (st_read(ST_SMK_TAG, tag, 16)) + return -1; + for (i = 0; i < 16u; i++) + ours &= tag[i] == want[i]; + if (ours) + return 0; + for (s = FL_DATA_LO; s < FL_DATA_HI; s += ST_SECTOR) + if (s != ST_SMK_TAG) { + bad |= st_erase(s) != 0; + ST_IDLE(); + } + keep = st_stock_intact(); /* the stock mapping: kept until JIANT writes there */ + for (s = FL_UFL_LO; !keep && s < FL_UFL_HI; s += ST_SECTOR) { + bad |= st_erase(s) != 0; + ST_IDLE(); + } + for (s = FL_GAP_LO; s < FL_GAP_HI; s += ST_SECTOR) { + bad |= st_erase(s) != 0; + ST_IDLE(); + } + if (bad || st_erase(ST_SMK_TAG) || st_prog(ST_SMK_TAG, want, 16)) + return -1; /* the tag only over a store that is all ours */ + return 1; +} +#else static uint32_t st_sector(uint32_t obj, uint32_t copy) /* flash offset of copy A (0) / B (1) */ { if (obj == OBJ_SETTINGS) @@ -84,6 +217,7 @@ static uint32_t st_sector(uint32_t obj, uint32_t copy) /* flash offset of copy return 0xDC000u + (obj - OBJ_UPRESET0) * 2u * ST_SECTOR + copy * ST_SECTOR; return 0x97000u + (obj - OBJ_PROJECT0) * 2u * ST_SECTOR + copy * ST_SECTOR; } +#endif /* BOARD_SMK37 */ static uint8_t st_buf[ST_PAYLOAD_MAX] __attribute__((aligned(4), section(".lowbuf"))); /* (0.9.0: app.ld LOWBUF, was * .bss: main loop only, zeroed at boot as .bss) */ diff --git a/firmware/src/storage_hw.c b/firmware/src/storage_hw.c index 71915ac4..9b51e7b4 100644 --- a/firmware/src/storage_hw.c +++ b/firmware/src/storage_hw.c @@ -4,6 +4,11 @@ * audio keeps up, erases and writes only inside the storage areas, the audio buffer silenced * first. The host tests (storage_test.c) provide their own st_* on a simulated NOR. */ static uint8_t flash_ok; /* JEDEC id matched at boot (persist_boot) */ +#if BOARD_SMK37 +#define ST_IDLE() fm1_wdt_feed() /* (storage.c st_owner_check: a first boot clears ~78 sectors) */ +static void st_stock_release(void); /* storage.c: the stock mapping's end marker */ +static int st_in_usrflash(uint32_t off); +#endif static int st_read(uint32_t off, void *dst, uint32_t n) /* 256-byte IRQ-off windows: audio keeps up */ { uint8_t *d = dst; @@ -25,12 +30,31 @@ static void audio_silence(void) /* IRQs off: the DMA would loop for (i = 0; i < sizeof abuf / sizeof abuf[0]; i++) abuf[i] = 0; } +#if BOARD_SMK37 +static int st_erase_raw(uint32_t off); +#define ST_ERASE_MARKER() st_erase_raw(FL_STOCKTAG_LO) /* (storage.c st_stock_release: the marker's sector only) */ +#endif static int st_erase(uint32_t off) { - uint32_t took, f; - int rc; +#if BOARD_SMK37 if (!FL_STORE_OK(off, 0x1000u)) return -8; + if (st_in_usrflash(off)) + st_stock_release(); /* before the first erase in USRFLASH: see storage.c */ + return st_erase_raw(off); +} +static int st_erase_raw(uint32_t off) /* (the store windows, and the marker's sector: FL_GUARD) */ +{ +#endif + uint32_t took, f; + int rc; +#if BOARD_SMK37 + if (!FL_STORE_OK(off, 0x1000u) && !FL_STOCKTAG_OK(off, 0x1000u)) + return -8; +#else + if (!FL_STORE_OK(off, 0x1000u)) + return -8; +#endif f = irq_save(); audio_silence(); rc = FL_FAR(fl_erase4k_ram)(off, &took); @@ -41,5 +65,9 @@ static int st_prog(uint32_t off, const void *src, uint32_t n) { if (!FL_STORE_OK(off, n)) /* only inside the storage areas */ return -8; +#if BOARD_SMK37 + if (st_in_usrflash(off)) + st_stock_release(); /* before the first program in USRFLASH too */ +#endif return fl_write(off, src, n); } diff --git a/tests/ota_test.c b/tests/ota_test.c index 01c4ae3c..24179cb4 100644 --- a/tests/ota_test.c +++ b/tests/ota_test.c @@ -10,8 +10,18 @@ #include #include -#define FELUCCA_ID "FM-1_900" #define FELUCCA_OTA_DRYRUN 0 +/* (SMK-37) -DBOARD_SMK37=1 -Ifirmware/hal: the SMK build of ota.c (staging 0xBB000, the board check); + * ota_test SMK.fwsc FM1.fwsc */ +#if BOARD_SMK37 +#include "fm1_flash_map.h" +#define FELUCCA_ID SMK_MODEL "_900" +#define T_OTA_LO 0xBB000u +#else +#define FELUCCA_ID "FM-1_900" +#define T_OTA_LO 0xE0000u +#endif +#define T_OTA_HI (T_OTA_LO + 0x5000u) static uint8_t nor[0x100000]; static uint8_t *logical; @@ -96,7 +106,7 @@ static uint32_t ota_now_ms(void) { return now_ms; } static void ota_idle(void) { now_ms += 1; } static int ota_erase(uint32_t off) { - if (off < 0xE0000u || off >= 0xE5000u || (off & 0xFFFu)) { bad_erase++; return -8; } + if (off < T_OTA_LO || off >= T_OTA_HI || (off & 0xFFFu)) { bad_erase++; return -8; } memset(nor + off, 0xFF, 4096); return 0; } @@ -104,7 +114,7 @@ static int ota_prog(uint32_t off, const void *src, uint32_t n) { const uint8_t *s = src; uint32_t i; - if (off < 0xE0000u || off + n > 0xE5000u) { bad_erase++; return -8; } + if (off < T_OTA_LO || off + n > T_OTA_HI) { bad_erase++; return -8; } for (i = 0; i < n; i++) nor[off + i] &= s[i]; return 0; } @@ -138,7 +148,7 @@ static int check(const char *what, int ok) static int area_erased(void) { uint32_t i; - for (i = 0xE0000; i < 0xE5000; i++) if (nor[i] != 0xFF) return 0; + for (i = T_OTA_LO; i < T_OTA_HI; i++) if (nor[i] != 0xFF) return 0; return 1; } @@ -146,6 +156,10 @@ static void reset_dev(void) { memset(nor, 0xFF, sizeof nor); memcpy(nor, logical + 0x400, 0x4000); /* device flash head = package flash.bin head */ +#if BOARD_SMK37 + memcpy(nor + FL_RT_OFF, logical + 0x400 + FL_RT_OFF, FL_RT_LEN); /* the stock region table (smk_pkg_test.py: + * the package's is the stock v016's) */ +#endif rx_full = 0; requests = 0; ident_replies = 0; committed = 0; last_code = 0; mute_after = 0xFFFFFFFFu; corrupt_at = NULL; bad_erase = 0; } @@ -186,15 +200,18 @@ int main(int argc, char **argv) bad += check("session commits", rc == 0 && committed); bad += check("headers + loader + confirm, no head reads", requests >= 10 && requests <= 30); { - uint8_t *h = nor + 0xE0000, *p = committed_parm; - bad += check("staged body == the package's loader", !memcmp(nor + 0xE0020, logical + ota_body_off(), 64)); + uint8_t *h = nor + T_OTA_LO, *p = committed_parm; + bad += check("staged body == the package's loader", !memcmp(nor + T_OTA_LO + 0x20u, logical + ota_body_off(), 64)); bad += check("LOADER.BIN head CRC", (h[0] | h[1] << 8) == ota_crc16(h + 2, 30, 0) && !memcmp(h + 16, "LOADER.BIN", 10)); - bad += check("record at 0xE4F00 == RAM record", !memcmp(nor + 0xE4F00, p, 112)); + bad += check("record at the area's end - 256 == RAM record", !memcmp(nor + T_OTA_HI - 0x100u, p, 112)); bad += check("record CRC / type / magic / ota_addr", (p[0] | p[1] << 8) == ota_crc16(p + 2, 78, 0) && (p[2] | p[3] << 8) == 0x5A0D && - (p[6] | p[7] << 8) == 0x5441 && ota_rd32(p + 72) == 0xE0000); - bad += check("nothing written outside 0xE0000..0xE4FFF", bad_erase == 0); + (p[6] | p[7] << 8) == 0x5441 && ota_rd32(p + 72) == T_OTA_LO); + bad += check("record identity " OTA_PARM_ID ", zero after it", + !memcmp(p + 8, OTA_PARM_ID, sizeof OTA_PARM_ID - 1u) && p[8 + sizeof OTA_PARM_ID - 1u] == 0 && + p[0x27] == 0); + bad += check("nothing written outside the OTA area", bad_erase == 0); } /* boot cleanup erases it */ bad += check("boot cleanup erases the area", ota_boot_cleanup() == 1 && area_erased()); @@ -218,6 +235,36 @@ int main(int argc, char **argv) rc = ota_session(); bad += check("no 'success' -> -13, no commit, area clean", rc == -13 && !committed && area_erased()); } +#if BOARD_SMK37 + { /* the board check: refused before anything is erased or staged */ + uint8_t *own = logical; + size_t own_len = logical_len; + reset_dev(); + nor[FL_RT_OFF + 5] ^= 0x10u; /* the device's region table differs (an FM-1, ...) */ + memset(nor + T_OTA_LO, 0x5A, 16); + rc = ota_session(); + bad += check("SMK: a region table not the device's -> -16, nothing erased", + rc == -16 && !committed && nor[T_OTA_LO] == 0x5A && bad_erase == 0); + if (argc > 2 && (f = fopen(argv[2], "rb")) != NULL) { /* the FM-1's package (flash.bin 0x93000) */ + n = fread(raw, 1, 0x200000, f); + fclose(f); + logical = malloc(n); + for (i = 0; i < 20; i++) memcpy(logical + i * 47, raw + i * 48, 47); + memcpy(logical + 20 * 47, raw + 20 * 48, n - 20 * 48); + logical_len = n - 20; + memset(nor, 0xFF, sizeof nor); + memcpy(nor, own + 0x400, 0x4000); /* the SMK's own head and table */ + memset(nor + T_OTA_LO, 0x5A, 16); + rx_full = 0; requests = 0; committed = 0; bad_erase = 0; + rc = ota_session(); + bad += check("SMK: the FM-1's package (flash.bin 0x93000) -> -15, nothing erased", + rc == -15 && !committed && nor[T_OTA_LO] == 0x5A && bad_erase == 0); + logical = own; + logical_len = own_len; + } else + bad += check("SMK: the FM-1's package given (argv 2)", 0); + } +#endif printf("%s\n", bad ? "OTA TEST FAILED" : "ota test passed"); return bad != 0; diff --git a/tests/run_tests.sh b/tests/run_tests.sh index b2ab1c65..865dddd6 100755 --- a/tests/run_tests.sh +++ b/tests/run_tests.sh @@ -386,7 +386,27 @@ else echo "== skip CPU under valgrind (no valgrind, or not Linux x86-64)" fi -# (SMK-37, docs/SMK37.md) the SMK-37 packages, when built (./build.sh --board smk37 [--model elite]); a stock M-VAVE +# (SMK-37, docs/SMK37.md) the flash map and its guards, the store on it, the board check, the loader +$CC -DBOARD_SMK37=1 -Ifirmware/hal -o "$OUT/smk_flash_test" tests/smk_flash_test.c +run "SMK-37 flash map + store: never the head, BTIF/USRTRIM, USR, 0xFE000, key_mac; the stock marker only released" \ + "$OUT/smk_flash_test" +smk_driver_guard() { # the RAM driver checks FL_GUARD (FL_PROTECTED on the SMK) itself + [ "$(grep -c '!FL_GUARD(off, 0x1000u)\|!FL_GUARD(off, n)' firmware/hal/fm1_flash.h)" = 2 ] && + ! grep -q 'FL_RANGE_OK(off, 0x1000u) ||\|!FL_RANGE_OK(off, n) ||' firmware/hal/fm1_flash.h +} +run "SMK-37 flash driver: fl_erase4k_ram / fl_prog_ram refuse what FL_GUARD refuses" smk_driver_guard +if [ -f build/smk37-pro/felucca-smk37-pro.fwsc ]; then + $CC -w -DOWN_PKG=1 -DBOARD_SMK37=1 -Ifirmware/hal -o "$OUT/ota_test_smk" tests/ota_test.c + run "SMK-37 M-UPGRADE entry: staging 0xBB000, the model's record, the board check (-15 FM-1 package, -16 table)" \ + "$OUT/ota_test_smk" build/smk37-pro/felucca-smk37-pro.fwsc build/felucca.fwsc + head -c 200000 build/smk37-pro/felucca.bin > "$OUT/smk_old_app.bin" + python3 tools/fm1pkg_make.py --board smk37 --product "SMK-37 Pro_900" "$OUT/smk_old_app.bin" \ + build/smk37-pro/loader/ota.bin "$OUT/smk_old.fwsc" >/dev/null + $CC -DBOARD_SMK37=1 -Ifirmware/hal -o "$OUT/smk_ldr_test" tests/smk_ldr_test.c + run "SMK-37 update loader: the app area 0x4000-0x9CFFF only, the record at 0xBFF00, the FM-1's package refused" \ + "$OUT/smk_ldr_test" "$OUT/smk_old.fwsc" build/smk37-pro/felucca-smk37-pro.fwsc build/felucca.fwsc +fi +# the SMK-37 packages, when built (./build.sh --board smk37 [--model elite]); a stock M-VAVE # v016 package for the region table's bytes: SMK_STOCK=folder with SMK37-Pro-V16.fwsc / SMK37-Elite-V16.fwsc (read only) for m in pro elite; do P=build/smk37-$m/felucca-smk37-$m.fwsc diff --git a/tests/smk_flash_test.c b/tests/smk_flash_test.c new file mode 100644 index 00000000..a7706bcb --- /dev/null +++ b/tests/smk_flash_test.c @@ -0,0 +1,285 @@ +/* SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) + * (after Melodee's tests/smk_flash_test.c, Copyright (C) 2026 M. Geisler (genkigenki/makkuro), + * Copyright (C) 2026 Kerem Kilic (Ellic Studio)) */ +/* The SMK-37's flash map and JIANT's store on it (build with -DBOARD_SMK37=1 -Ifirmware/hal): + * - every 4 KiB sector of the 1 MiB part, and pages / bytes at every protected boundary, through the app's + * allow-list (FL_STORE_OK, FL_RANGE_OK), the RAM driver's net (FL_PROTECTED, FL_GUARD) and the update loader's + * (FL_LDR_RANGE_OK): none may reach the head 0x0-0x3FFF, BTIF / USRTRIM 0xC0000-0xC1FFF, USR 0xF4000-0xFDFFF, + * the free 0xFE000 or key_mac 0xFF000-; straddling and wrapping ranges refused; + * - storage.c's SMK table (st_sector): every object's pair inside the store windows, no two sharing a sector, + * never the owner tag, the voice bank, the marker's sector or the OTA area; + * - the store (storage.c + the storage_hw.c hooks, mirrored) on a simulated NOR whose protected ranges and stock + * data hold patterns: a first boot over another firmware's store, every object saved and loaded back, the voice + * bank's sector, the stock mapping in USRFLASH kept until the first write there, its marker released exactly + * once; erases and programs aimed at each forbidden range refused; afterwards every protected byte, the app area + * and the OTA area byte-identical; + * - FL_JEDEC_OK: the SMK's 0xEB6014 only. */ +#include +#include +#include +#include +#include "fm1_cc.h" +#if !BOARD_SMK37 +#error "build with -DBOARD_SMK37=1 -Ifirmware/hal" +#endif +#include "fm1_flash_map.h" + +static uint8_t nor[0x100000], ref[0x100000]; +static uint32_t refused, erases, tag_erases, fail_erase, net_refused; +static int st_read(uint32_t off, void *dst, uint32_t n) +{ + if (off > sizeof nor || n > sizeof nor - off) return -1; + memcpy(dst, nor + off, n); + return 0; +} +/* the RAM driver (fm1_flash.h fl_erase4k_ram / fl_prog_ram): FL_GUARD, then the part */ +static int drv_erase(uint32_t off) +{ + if (!FL_GUARD(off, 0x1000u) || (off & 0xFFFu)) { net_refused++; return -1; } + if (fail_erase && off == fail_erase) return -3; + if (off == FL_STOCKTAG_LO) tag_erases++; + memset(nor + off, 0xFF, 0x1000u); + erases++; + return 0; +} +static int drv_prog(uint32_t off, const uint8_t *s, uint32_t n) +{ + uint32_t i; + if (!FL_GUARD(off, n) || n == 0 || n > 256u || ((off & 0xFFu) + n) > 256u) { net_refused++; return -1; } + for (i = 0; i < n; i++) nor[off + i] &= s[i]; + return 0; +} +/* storage_hw.c's st_erase / st_erase_raw / st_prog, line for line, on the simulated driver */ +static void st_stock_release(void); +static int st_in_usrflash(uint32_t off); +static int st_erase_raw(uint32_t off); +#define ST_ERASE_MARKER() st_erase_raw(FL_STOCKTAG_LO) +static int st_erase(uint32_t off) +{ + if (!FL_STORE_OK(off, 0x1000u)) { refused++; return -8; } + if (st_in_usrflash(off)) st_stock_release(); + return st_erase_raw(off); +} +static int st_erase_raw(uint32_t off) +{ + if (!FL_STORE_OK(off, 0x1000u) && !FL_STOCKTAG_OK(off, 0x1000u)) { refused++; return -8; } + return drv_erase(off); +} +static int st_prog(uint32_t off, const void *src, uint32_t n) +{ + const uint8_t *s = src; + if (!FL_STORE_OK(off, n)) { refused++; return -8; } + if (st_in_usrflash(off)) st_stock_release(); + while (n) { /* fl_write: page by page */ + uint32_t k = 256u - (off & 0xFFu); + int rc; + if (k > n) k = n; + if ((rc = drv_prog(off, s, k)) != 0) return rc; + off += k; s += k; n -= k; + } + return 0; +} +#include "../firmware/src/storage.c" + +static int bad; +static void check(const char *what, int ok) { printf("smk flash: %-92s %s\n", what, ok ? "ok" : "FAIL"); bad += !ok; } + +static int protected_byte(uint32_t a) +{ + return a < 0x4000u || (a >= 0xC0000u && a < 0xC2000u) || (a >= 0xF4000u && a < 0xFF000u) || a >= 0xFF000u; +} +static int same(uint32_t lo, uint32_t hi) { return !memcmp(nor + lo, ref + lo, hi - lo); } +static int all_protected_same(void) +{ + return same(0, 0x4000u) && same(0xC0000u, 0xC2000u) && same(0xF4000u, 0x100000u); +} +static void stock(void) /* the stock mapping in USRFLASH, as V016 writes it */ +{ + uint32_t n; + memset(nor + 0xC2000, 0xFF, 0x29000); + memset(nor + 0xC2000, 0x07, 0x86); + nor[0xC2000] = 0xA3; nor[0xC2001] = 0x3A; + for (n = 0; n < 8u; n++) { + memset(nor + 0xC3000 + n * 0x5000, 0x01, 0x29E3); + nor[0xC3000 + n * 0x5000 + 4] = 0x7F; + } + memcpy(nor + 0xEAFF8, "usrdata", 8); +} +static void device(void) /* an SMK as M-VAVE ships it: every byte a pattern */ +{ + uint32_t i; + for (i = 0; i < sizeof nor; i++) nor[i] = (uint8_t)(i * 29u ^ i >> 11 ^ 0x5Au); + memset(nor + 0xC0000, 0x42, 0x1000); /* BTIF */ + for (i = 0; i < 0x22Cu; i++) nor[0xC1000 + i] = (uint8_t)(i * 3u + 1u); /* USRTRIM "trimdat" */ + stock(); + memcpy(nor + ST_SMK_TAG, "SMK37DATASLOOP", 15); /* another port's store */ +} + +static uint8_t payload[ST_PAYLOAD_MAX], back[ST_PAYLOAD_MAX]; + +int main(void) +{ + uint32_t s, o, c, k, leak = 0, leak_net = 0, leak_ldr = 0, allowed = 0; + /* ---- the map ---- */ + for (s = 0; s < 0x100000u; s += 0x1000u) { + int p = protected_byte(s); + allowed += FL_GUARD(s, 0x1000u); + if (!p) continue; + for (o = 0; o < 0x1000u; o += 0x100u) { + leak += FL_STORE_OK(s + o, 256u) || FL_RANGE_OK(s + o, 256u) || FL_GUARD(s + o, 256u) || FL_GUARD(s + o, 1u); + leak_net += !FL_PROTECTED(s + o, 256u) || !FL_PROTECTED(s + o, 1u); + leak_ldr += FL_LDR_RANGE_OK(s + o, 256u) && !FL_PROTECTED(s + o, 256u); + } + leak += FL_STORE_OK(s, 0x1000u) || FL_RANGE_OK(s, 0x1000u) || FL_GUARD(s, 0x1000u); + leak_net += !FL_PROTECTED(s, 0x1000u); + leak_ldr += FL_LDR_RANGE_OK(s, 0x1000u); + } + check("no protected sector, page or byte passes the app's allow-list (FL_STORE_OK / FL_RANGE_OK / FL_GUARD)", leak == 0); + check("the RAM driver's own net (FL_PROTECTED) covers every protected sector, page and byte", leak_net == 0); + check("no protected sector passes the update loader's allow-list (FL_LDR_RANGE_OK)", leak_ldr == 0); + { /* the boundaries, one byte either side */ + static const uint32_t B[] = {0x4000u, 0xC0000u, 0xC2000u, 0xF4000u}; /* (0xF4000.. protected to the end) */ + uint32_t ok = 1; + for (k = 0; k < sizeof B / sizeof B[0]; k++) { + ok &= FL_PROTECTED(B[k] - 1u, 1u) != FL_PROTECTED(B[k], 1u); + ok &= FL_PROTECTED(B[k] - 1u, 2u); /* any range touching a protected byte */ + } + check("each protected range starts / ends exactly at its boundary; a range touching it is refused", (int)ok); + } + check("straddling ranges refused: 0xBF000+0x2000, 0xE9000+0x2000, 0xEA000+0x2000, 0xF3000+0x2000, 0x3000+0x2000", + !FL_GUARD(0xBF000u, 0x2000u) && !FL_GUARD(0xE9000u, 0x2000u) && !FL_GUARD(0xEA000u, 0x2000u) && + !FL_GUARD(0xF3000u, 0x2000u) && !FL_LDR_RANGE_OK(0xBF000u, 0x2000u) && !FL_LDR_RANGE_OK(0x3000u, 0x2000u) && + FL_PROTECTED(0xBF000u, 0x2000u) && FL_PROTECTED(0x3F00u, 0x200u) && FL_PROTECTED(0xF3F00u, 0x200u)); + check("wrapping ranges refused (off + n past 4 GiB lands low on the 1 MiB part)", + !FL_GUARD(0xBB000u, 0xFFFFF000u) && !FL_LDR_RANGE_OK(0x9D000u, 0xFFFFFF00u) && FL_PROTECTED(0xBB000u, 0xFFFFF000u) && + !FL_GUARD(0xFFFFFF00u, 0x200u) && FL_PROTECTED(0xFFFFFF00u, 0x200u) && FL_PROTECTED(0x100000u, 0x100u)); + check("app windows: FL_DATA 0x9D000-0xBAFFF, FL_OTA 0xBB000-0xBFFFF, FL_UFL 0xC2000-0xE9FFF, FL_GAP 0xEB000-0xF3FFF", + FL_GUARD(0x9D000u, 0x1000u) && FL_GUARD(0xBA000u, 0x1000u) && FL_GUARD(0xBF000u, 0x1000u) && + FL_GUARD(0xC2000u, 0x1000u) && FL_GUARD(0xE9000u, 0x1000u) && FL_GUARD(0xEB000u, 0x1000u) && + FL_GUARD(0xF3000u, 0x1000u) && !FL_GUARD(0xF4000u, 0x1000u) && !FL_GUARD(0xFE000u, 0x1000u) && + !FL_GUARD(0x9C000u, 0x1000u) && + allowed == (0xC0000u - 0x9D000u + 0xEA000u - 0xC2000u + 0x1000u + 0xF4000u - 0xEB000u) / 0x1000u); + check("the stock marker's sector 0xEA000: its whole-sector erase only (the release), never a store window", + FL_GUARD(0xEA000u, 0x1000u) && !FL_STORE_OK(0xEA000u, 0x1000u) && !FL_GUARD(0xEA000u, 256u) && + !FL_GUARD(0xEAF00u, 256u) && !FL_STORE_OK(0xEAFF8u, 8u) && FL_STOCKTAG_OFF == 0xEAFF8u); + check("loader: the app area 0x4000-0x9CFFF and the OTA area 0xBB000-0xBFFFF only", + FL_LDR_RANGE_OK(0x4000u, 0x1000u) && FL_LDR_RANGE_OK(0x9C000u, 0x1000u) && FL_LDR_RANGE_OK(0xBF000u, 0x1000u) && + FL_LDR_RANGE_OK(0xBB000u, 0x1000u) && !FL_LDR_RANGE_OK(0x9D000u, 0x1000u) && !FL_LDR_RANGE_OK(0xBA000u, 0x1000u) && + !FL_LDR_RANGE_OK(0xC0000u, 0x1000u) && !FL_LDR_RANGE_OK(0xC2000u, 0x1000u) && !FL_LDR_RANGE_OK(0x3000u, 0x1000u)); + check("FL_JEDEC_OK: the SMK's 0xEB6014 only (not the FM-1's 0x856014 / 0xB36014, not 0, not 0xFFFFFF)", + FL_JEDEC_OK(0xEB6014u) && !FL_JEDEC_OK(0x856014u) && !FL_JEDEC_OK(0xB36014u) && !FL_JEDEC_OK(0u) && + !FL_JEDEC_OK(0xFFFFFFu) && !FL_JEDEC_OK(0x1EB6014u)); + check("FL_PLAIN_LO = the VM start 0x9D000; the region table at 0x4060, 0xC0 bytes", + FL_PLAIN_LO == 0x9D000u && FL_RT_OFF == 0x4060u && FL_RT_LEN == 0xC0u); + + /* ---- storage.c's table ---- */ + { + static uint8_t owner[256]; + uint32_t outside = 0, overlap = 0, n = 0; + memset(owner, 0xFF, sizeof owner); + owner[ST_SMK_TAG >> 12] = 0xF0; + owner[ST_SMK_VBANK >> 12] = 0xF1; + for (o = 0; o < OBJ_COUNT; o++) { + if (o == OBJ_FM6BANK) continue; /* (OBJ_UPFM6's pair: one object retired into the other) */ + for (c = 0; c < 2u; c++) { + uint32_t at = st_sector(o, c); + n++; + outside += !FL_STORE_OK(at, 0x1000u) || (at & 0xFFFu) || FL_PROTECTED(at, 0x1000u) || + (at >= FL_OTA_LO && at < FL_OTA_HI) || at == FL_STOCKTAG_LO; + if (owner[at >> 12] != 0xFF) overlap++; + owner[at >> 12] = (uint8_t)o; + } + } + check("storage.c: 76 copies (38 objects x A/B) inside FL_DATA / FL_UFL / FL_GAP, aligned, off the OTA area", + n == 76u && outside == 0); + check("storage.c: no two copies share a sector, nor the owner tag 0xAF000 or the voice bank 0xAB000", + overlap == 0 && owner[0xAF] == 0xF0 && owner[0xAB] == 0xF1); + check("storage.c: OBJ_FM6BANK and OBJ_UPFM6 one pair (as the FM-1's); the settings at 0x9D000 / 0x9E000", + st_sector(OBJ_FM6BANK, 0) == st_sector(OBJ_UPFM6, 0) && st_sector(OBJ_FM6BANK, 1) == st_sector(OBJ_UPFM6, 1) && + st_sector(OBJ_SETTINGS, 0) == 0x9D000u && st_sector(OBJ_SETTINGS, 1) == 0x9E000u); + for (s = 0, k = 0; s < 0x100u; s++) k += owner[s] == 0xFF && FL_STORE_OK(s << 12, 0x1000u); + check("storage.c: one store sector spare (0xF3000)", k == 1u && owner[0xF3] == 0xFF); + } + + /* ---- the store on a simulated SMK ---- */ + device(); + memcpy(ref, nor, sizeof nor); + refused = net_refused = erases = tag_erases = 0; + k = (uint32_t)st_owner_check(); + check("first boot over another port's store: cleared (A, C), the intact stock mapping in USRFLASH kept, tagged JIANT", + k == 1u && nor[0x9D000u] == 0xFF && nor[0xEB000u] == 0xFF && nor[0xF3FFFu] == 0xFF && + same(0xC2000u, 0xEB000u) && st_stock_intact() && tag_erases == 0 && + !memcmp(nor + ST_SMK_TAG, "SMK37DATAJIANT\0", 16) && refused == 0 && net_refused == 0); + check("... nothing outside the store windows touched: head, app area, OTA area, BTIF, USRTRIM, USR, key_mac", + all_protected_same() && same(0x4000u, 0x9D000u) && same(0xBB000u, 0xC0000u)); + check("... and at the next boot (ours now): nothing erased", (erases = 0, st_owner_check() == 0) && erases == 0); + for (o = 0; o < OBJ_SONG1 + 4u; o++) { /* everything outside USRFLASH first: the stock mapping stays */ + if (o == OBJ_FM6BANK) continue; + for (k = 0; k < sizeof payload; k++) payload[k] = (uint8_t)(k * 7u + o); + if (st_save(o, payload, sizeof payload) || st_save(o, payload, sizeof payload)) break; + } + check("every object outside USRFLASH saved twice (both copies): the stock mapping and its marker kept", + o == OBJ_SONG1 + 4u && st_stock_intact() && tag_erases == 0 && refused == 0 && net_refused == 0); + for (o = OBJ_SONG1 + 24u; o < OBJ_COUNT; o++) { /* song 8 (store C) */ + for (k = 0; k < sizeof payload; k++) payload[k] = (uint8_t)(k * 5u + o); + if (st_save(o, payload, sizeof payload) || st_save(o, payload, sizeof payload)) break; + } + check("song 8 (store C) saved: the stock mapping still kept", o == OBJ_COUNT && st_stock_intact() && tag_erases == 0); + for (o = OBJ_SONG1 + 4u; o < OBJ_SONG1 + 24u; o++) { /* songs 3..7: USRFLASH */ + for (k = 0; k < sizeof payload; k++) payload[k] = (uint8_t)(k * 3u + o); + if (st_save(o, payload, sizeof payload) || st_save(o, payload, sizeof payload)) break; + } + check("songs 3..7 into USRFLASH: the marker's sector erased first, exactly once", + o == OBJ_SONG1 + 24u && tag_erases == 1u && !st_stock_tagged() && nor[0xEAFF8u] == 0xFF && + refused == 0 && net_refused == 0); + { + uint32_t good = 1; + for (o = 0; o < OBJ_COUNT; o++) { + uint32_t mul = o < OBJ_SONG1 + 4u ? 7u : o < OBJ_SONG1 + 24u ? 3u : 5u; + if (o == OBJ_FM6BANK) continue; + for (k = 0; k < sizeof payload; k++) payload[k] = (uint8_t)(k * mul + o); + good &= st_load(o, back, sizeof back) == (int)sizeof back && !memcmp(back, payload, sizeof back); + } + check("every object loads back", (int)good); + } + check("the voice bank's sector (fm6_vbank.c FVB_HOME 0xAB000): erase + program pass the guards", + st_erase(ST_SMK_VBANK) == 0 && st_prog(ST_SMK_VBANK + 224u, payload, 600u) == 0 && net_refused == 0); + { /* aimed at every forbidden range: refused by st_erase / st_prog, and by the driver's own net */ + static const uint32_t BAD[] = {0x0u, 0x1000u, 0x3000u, 0x3F00u, 0x4000u, 0x50000u, 0x9C000u, 0x9CF00u, + 0xBB000u, 0xBFF00u, 0xC0000u, 0xC0F00u, 0xC1000u, 0xC1F00u, 0xEA000u, 0xEAF00u, + 0xF4000u, 0xF8000u, 0xFD000u, 0xFDF00u, 0xFE000u, 0xFF000u, 0xFFF00u, + 0x100000u, 0x1C0000u, 0xFFFFF000u}; + uint32_t ok = 1, before = refused, nb = net_refused; + for (k = 0; k < sizeof BAD / sizeof BAD[0]; k++) { + uint32_t a = BAD[k]; + ok &= st_prog(a, payload, 256u) != 0; + if (!(a & 0xFFFu)) ok &= st_erase(a) != 0 && (a == FL_STOCKTAG_LO || st_erase_raw(a) != 0); + if (protected_byte(a & 0xFFFFFu) || a >= 0x100000u) + ok &= drv_erase(a & ~0xFFFu) != 0 && drv_prog(a, payload, 16u) != 0; + } + check("erase / program of the head, the app area, OTA, BTIF, USRTRIM, the marker, USR, 0xFE000, key_mac, wraps: refused", + ok && refused > before && net_refused > nb); + } + check("after it all: head, BTIF, USRTRIM, USR, 0xFE000, key_mac byte-identical", all_protected_same()); + check("after it all: the app area 0x4000-0x9CFFF and the OTA area 0xBB000-0xBFFFF byte-identical", + same(0x4000u, 0x9D000u) && same(0xBB000u, 0xC0000u)); + { /* the owner check's other cases */ + device(); + memset(nor + 0xC2000u, 0x33, 0x2000u); /* USRFLASH holds something else under the marker */ + tag_erases = 0; + (void)st_owner_check(); + check("the marker over anything but the stock mapping: released at boot, USRFLASH cleared", + !st_stock_tagged() && tag_erases == 1u && nor[0xC2000u] == 0xFF); + device(); + memcpy(ref, nor, sizeof nor); + fail_erase = 0xB3000u; /* one sector of another port's store will not erase */ + check("an erase fails: -1, the other port's tag stays (cleared again at the next boot)", + st_owner_check() == -1 && !memcmp(nor + ST_SMK_TAG, "SMK37DATASLOOP", 15)); + fail_erase = 0; + check("... the next boot clears it and tags it", + st_owner_check() == 1 && !memcmp(nor + ST_SMK_TAG, "SMK37DATAJIANT", 14) && all_protected_same()); + } + printf("%s\n", bad ? "SMK FLASH TEST FAILED" : "smk flash test passed"); + return bad != 0; +} diff --git a/tests/smk_ldr_test.c b/tests/smk_ldr_test.c new file mode 100644 index 00000000..c4643df4 --- /dev/null +++ b/tests/smk_ldr_test.c @@ -0,0 +1,202 @@ +/* SPDX-License-Identifier: GPL-3.0-only + * Copyright (C) 2026 Juan Ignacio Antonucci (Juan Jiant) */ +/* Host test of the SMK-37 build of the update loader (firmware/loader/ldr_core.c, -DBOARD_SMK37=1 -Ifirmware/hal): + * a fake host serves a package over the SysEx protocol to a simulated SMK-37 whose every byte holds a pattern (the + * stock firmware's head and region table, BTIF, USRTRIM, USRFLASH with its marker, USR, key_mac, JIANT's store with + * a record look-alike in it, the stock update record at 0xBFF00). The flash hooks are the RAM driver's: FL_GUARD + * over the loader's FL_RANGE_OK (loader.c: FL_LDR_RANGE_OK), so a refused erase / program is counted, not done. + * smk_ldr_test OLD_SMK.fwsc NEW_SMK.fwsc FM1.fwsc + * Checks: the install writes the app area [0x4000, 0x9D000) only, clears the record in the OTA area, keeps every + * other byte; the FM-1's package (flash.bin 0x93000) and a package whose region table is not the device's are + * refused before anything is erased. */ +#include +#include +#include +#include +#include "fm1_cc.h" +#if !BOARD_SMK37 +#error "build with -DBOARD_SMK37=1 -Ifirmware/hal" +#endif +#define FL_RANGE_OK(off, n) FL_LDR_RANGE_OK(off, n) /* as loader.c, before the map */ +#include "fm1_flash_map.h" + +#define FELUCCA_ID "ota-" SMK_MODEL "_900" +#define FELUCCA_OTA_DRYRUN 0 + +static uint8_t nor[0x100000], ref[0x100000], *logical; +static size_t logical_len; +static uint8_t rx[1024]; +static uint32_t rx_len, rx_full, now_ms, erases, refused, record_cleared; + +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] = {0}, 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; + 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) { + 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); + } + 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) /* fm1_flash.h fl_erase4k_ram */ +{ + if (!FL_GUARD(off, 0x1000u) || (off & 0xFFFu)) { refused++; return -1; } + erases++; + memset(nor + off, 0xFF, 0x1000); + return 0; +} +static int ldr_prog(uint32_t off, const void *p, uint32_t n) /* fm1_flash.h fl_prog_ram */ +{ + const uint8_t *s = p; + uint32_t i; + if (!FL_GUARD(off, n) || n == 0 || n > 256u || ((off & 0xFFu) + n) > 256u) { refused++; return -1; } + 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) +{ + 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; +} +static int bad; +static void check(const char *what, int ok) { printf("smk ldr: %-90s %s\n", what, ok ? "ok" : "FAIL"); bad += !ok; } +static int same(uint32_t lo, uint32_t hi) { return !memcmp(nor + lo, ref + lo, hi - lo); } + +int main(int argc, char **argv) +{ + size_t olen, flen, own_len; + uint8_t *old, *fm1, *own; + uint32_t ofo, nfo, i; + int rc; + if (argc < 4) { printf("usage: smk_ldr_test OLD_SMK.fwsc NEW_SMK.fwsc FM1.fwsc\n"); return 2; } + old = load_logical(argv[1], &olen); + own = logical = load_logical(argv[2], &own_len); + logical_len = own_len; + fm1 = load_logical(argv[3], &flen); + ofo = flash_off(old); + nfo = flash_off(logical); + /* the device: OLD installed over an SMK whose other bytes are all patterns */ + for (i = 0; i < sizeof nor; i++) nor[i] = (uint8_t)(i * 13u ^ i >> 9 ^ 0xA5u); + memcpy(nor, old + ofo, 0x9D000); + memset(nor + 0xC0000, 0x42, 0x1000); /* BTIF */ + for (i = 0; i < 0x22Cu; i++) nor[0xC1000 + i] = (uint8_t)(i * 3u + 1u); /* USRTRIM */ + memcpy(nor + 0xEAFF8, "usrdata", 8); + { + 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 + 0xBFF00, r, sizeof r); /* the update record (ota.c OTA_RES) */ + memcpy(nor + 0xA0F00, r, sizeof r); /* user data in store A that looks like one */ + memcpy(nor + 0xC1F00, r, sizeof r); /* ... and in USRTRIM, BTIF, USRFLASH, USR */ + memcpy(nor + 0xC0F00, r, sizeof r); + memcpy(nor + 0xD5F00, r, sizeof r); + memcpy(nor + 0xF8F00, r, sizeof r); + } + memcpy(ref, nor, sizeof nor); + /* the FM-1's package: refused before anything is erased */ + logical = fm1; logical_len = flen; + erases = refused = 0; + rc = ldr_session(); + check("the FM-1's package (flash.bin 0x93000) refused, nothing erased", rc == -3 && erases == 0 && same(0, 0x100000u)); + logical = own; logical_len = own_len; + /* a package whose region table is not the device's (an SMK package on an FM-1, another layout) */ + nor[FL_RT_OFF + 7] ^= 0x01u; + erases = 0; + rc = ldr_session(); + check("a region table not the device's refused (-14), nothing erased", rc == -14 && erases == 0); + nor[FL_RT_OFF + 7] ^= 0x01u; + /* the install */ + erases = refused = record_cleared = 0; + rc = ldr_session(); + printf(" rc %d, %u sector erases\n", rc, erases); + check("install completes, no erase / program refused by the guards", rc == 0 && refused == 0); + check("app area 0x4000-0x9CFFF == the new package's flash.bin", !memcmp(nor + 0x4000, logical + nfo + 0x4000, 0x9D000 - 0x4000)); + check("the head [0, 0x4000) untouched", same(0, 0x4000u)); + check("the update record at 0xBFF00 cleared (RAM and flash)", record_cleared && nor[0xBFF06] == 0xFF); + check("store A (0x9D000-0xBAFFF), its record look-alike included, untouched", same(0x9D000u, 0xBB000u)); + check("BTIF, USRTRIM (their record look-alikes included), USRFLASH, the marker, store C, USR, key_mac untouched", + same(0xC0000u, 0x100000u)); + check("the OTA area: only the record's sector erased", same(0xBB000u, 0xBF000u)); + erases = 0; + rc = ldr_session(); + check("re-run with the same package erases nothing", rc == 0 && erases == 0); + printf("%s\n", bad ? "SMK LOADER TEST FAILED" : "smk loader test passed"); + return bad != 0; +}