fw_update.cpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. // Staged firmware update: WebUSB streams the new app image into unused upper app-region flash; a verified
  2. // meta-page commit arms it; the next boot applies it from RAM. See fw_update.h for the protocol and the
  3. // corruption-safety invariant every step here preserves.
  4. #include "fw_update.h"
  5. #include "rf_journal_layout.h"
  6. #include "fault_diag.h"
  7. #include <Arduino.h>
  8. #include <string.h>
  9. // nRF52840 flash map (Adafruit UF2 bootloader layout): MBR 0x0-0x1000, SoftDevice to 0x26000, app region
  10. // 0x26000-0xED000, internal LittleFS 0xED000-0xF4000, bootloader 0xF4000+, its settings page at 0xFF000.
  11. // We manage the TOP of the app region: the meta/commit page at 0xEC000 and the staged image right below it,
  12. // growing downward. Nothing outside [APP_BASE, 0xED000) is ever written except FWUP_BL_SETTINGS (see apply).
  13. #define FWUP_APP_BASE 0x26000UL
  14. #define FWUP_APP_END 0xED000UL
  15. #define FWUP_META 0xEC000UL
  16. // internal LittleFS (cfg.bin + bonds.bin) sits between the app region and the bootloader; wiped whole by the
  17. // full-board erase (config.cpp's factoryErase() only reformats it -- this leaves it raw-erased).
  18. #define FWUP_FS_BASE 0xED000UL
  19. #define FWUP_FS_END 0xF4000UL
  20. #define FWUP_BL_SETTINGS 0xFF000UL
  21. #define FWUP_PAGE 4096UL
  22. // The normal application ends immediately before the fixed RF-journal window.
  23. // The largest staged image starts at 0x8C000, leaving 16 KiB between the
  24. // journal end (0x88000) and staging. This keeps both update source and target
  25. // disjoint from learned RF history.
  26. #define FWUP_MAX_IMG (RF_CHANNEL_JOURNAL_BASE - FWUP_APP_BASE)
  27. static_assert(FWUP_MAX_IMG == 0x60000UL,
  28. "RF journal layout changed the WebUSB application cap");
  29. static_assert(RF_CHANNEL_JOURNAL_END <=
  30. ((FWUP_META - FWUP_MAX_IMG) & ~(FWUP_PAGE - 1UL)),
  31. "RF journal overlaps staged WebUSB firmware storage");
  32. // Capability tag, searched for BY THE PANEL inside any .uf2 it is about to flash: an image without this
  33. // exact string predates panel updates, so flashing it silently locks future updates back to UF2-DFU
  34. // drag-and-drop -- the panel warns before letting that happen. Bump the suffix only on a breaking protocol
  35. // change (the panel searches for this exact value). The asm reference below keeps it through --gc-sections.
  36. static const char FWUP_TAG[] = "OPK-FWUP-v1";
  37. #define FWUP_META_MAGIC 0x32465055UL // "UPF2"
  38. // Full-board-wipe arm marker, written to the meta page instead of a FwupMeta. Distinct magic + its bitwise
  39. // complement in the next word so neither an erased page (0xFFFFFFFF) nor a stray FwupMeta can spoof the arm.
  40. #define FWUP_WIPE_MAGIC 0x57495045UL // "WIPE"
  41. struct FwupMeta {
  42. uint32_t magic, size, crc, staged;
  43. uint32_t metaCrc; // CRC32 over the four fields above
  44. };
  45. static_assert(sizeof(FwupMeta) == 20, "FwupMeta layout");
  46. // end of the flash the RUNNING image occupies: code/rodata (__etext) plus the .data init image loaded right
  47. // after it (.data is the last flash-loaded section in the core's linker script). Staging must stay above it.
  48. extern "C" {
  49. extern uint32_t __etext[];
  50. extern uint32_t __data_start__[];
  51. extern uint32_t __data_end__[];
  52. }
  53. static uint32_t flashUsedEnd(void)
  54. {
  55. return (uint32_t)__etext +
  56. ((uint32_t)__data_end__ - (uint32_t)__data_start__);
  57. }
  58. // ---- CRC32 (IEEE, reflected, init/xorout 0xFFFFFFFF) -- must match the panel's JS implementation ----------
  59. static uint32_t crc32Step(uint32_t crc, const volatile uint8_t *p, uint32_t n)
  60. {
  61. while (n--) {
  62. crc ^= *p++;
  63. for (int k = 0; k < 8; k++)
  64. crc = (crc >> 1) ^ (0xEDB88320UL & (0UL - (crc & 1)));
  65. }
  66. return crc;
  67. }
  68. // CRC a flash range. Beats the loop-heartbeat between blocks: verifying a full-size image takes ~200 ms of
  69. // CPU, and without beats the fault_diag live-wedge reporter (>300 ms stall) would push an unsolicited status
  70. // blob into the IN pipe right when the panel is waiting for the 0x22 ack.
  71. static uint32_t crc32Flash(uint32_t addr, uint32_t len)
  72. {
  73. uint32_t crc = 0xFFFFFFFFUL;
  74. while (len) {
  75. uint32_t blk = len > 16384 ? 16384 : len;
  76. crc = crc32Step(crc, (const volatile uint8_t *)addr, blk);
  77. addr += blk;
  78. len -= blk;
  79. faultDiagBeat();
  80. }
  81. return ~crc;
  82. }
  83. // ---- NVMC helpers (flash-resident; used for staging + meta only, never on the live app region) ------------
  84. // NVMC ops halt the CPU (page erase ~85 ms) -- callers keep it to at most one erase per loop() pass so the
  85. // 8 s watchdog and the RF link only see brief hiccups.
  86. static void nvmcErasePage(uint32_t page)
  87. {
  88. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een;
  89. while (!NRF_NVMC->READY) {
  90. }
  91. NRF_NVMC->ERASEPAGE = page;
  92. while (!NRF_NVMC->READY) {
  93. }
  94. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
  95. while (!NRF_NVMC->READY) {
  96. }
  97. }
  98. static void nvmcWriteWords(uint32_t addr, const uint32_t *w, uint32_t nWords)
  99. {
  100. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
  101. while (!NRF_NVMC->READY) {
  102. }
  103. for (uint32_t i = 0; i < nWords; i++) {
  104. ((volatile uint32_t *)addr)[i] = w[i];
  105. while (!NRF_NVMC->READY) {
  106. }
  107. }
  108. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
  109. while (!NRF_NVMC->READY) {
  110. }
  111. }
  112. // staged image must start with a plausible Cortex-M vector table: initial SP word-aligned in RAM (256 KiB on
  113. // the nRF52840), reset vector Thumb within the app region. Last line of defense against arming a non-app blob.
  114. static bool vectorsPlausible(uint32_t imgAddr, uint32_t size)
  115. {
  116. if (size < 8)
  117. return false;
  118. uint32_t sp = ((const volatile uint32_t *)imgAddr)[0];
  119. uint32_t rv = ((const volatile uint32_t *)imgAddr)[1];
  120. return sp >= 0x20000000UL && sp <= 0x20040000UL && (sp & 3) == 0 &&
  121. (rv & 1) && rv >= FWUP_APP_BASE && rv < FWUP_APP_END;
  122. }
  123. // ---- staging state (transfer is strictly sequential; every ack carries s_off so the panel can resync) -----
  124. static bool s_active = false;
  125. static uint32_t s_size, s_crc, s_base, s_off, s_erased;
  126. uint32_t fwupNextOff(void)
  127. {
  128. return s_off;
  129. }
  130. uint8_t fwupBegin(uint32_t size, uint32_t crc32)
  131. {
  132. s_active = false;
  133. // any BEGIN first disarms a previously staged update -- a restarted transfer must never leave a stale
  134. // commit behind (the staged bytes it points at are about to be overwritten).
  135. nvmcErasePage(FWUP_META);
  136. if (size == 0 || size > FWUP_MAX_IMG || (size & 3))
  137. return FWUP_ERR_BOUNDS;
  138. uint32_t base = (FWUP_META - size) & ~(FWUP_PAGE - 1);
  139. if (base < RF_CHANNEL_JOURNAL_END)
  140. return FWUP_ERR_BOUNDS;
  141. // keep a page of slack above the running image so staging can NEVER touch live code/rodata/.data-init
  142. if (base < flashUsedEnd() + FWUP_PAGE)
  143. return FWUP_ERR_BOUNDS;
  144. s_size = size;
  145. s_crc = crc32;
  146. s_base = base;
  147. s_off = 0;
  148. s_erased =
  149. base; // pages erased lazily, one per chunk at most (85 ms CPU stall each)
  150. s_active = true;
  151. return FWUP_OK;
  152. }
  153. uint8_t fwupChunk(uint32_t off, const uint8_t *d, uint8_t len)
  154. {
  155. if (!s_active)
  156. return FWUP_ERR_STATE;
  157. if (len == 0 || len > 128 || (len & 3) || off + len > s_size)
  158. return FWUP_ERR_BOUNDS;
  159. if (off != s_off) {
  160. // duplicate of already-written data (panel retried a chunk whose ack got lost): don't rewrite
  161. // the words (nRF52 flash allows only 2 writes per word between erases) -- just re-ack.
  162. if (off < s_off && off + len <= s_off)
  163. return FWUP_OK;
  164. return FWUP_ERR_OFFSET; // gap/desync: ack carries s_off, panel resends from there
  165. }
  166. while (s_erased < s_base + off + len) {
  167. nvmcErasePage(s_erased);
  168. s_erased += FWUP_PAGE;
  169. }
  170. uint32_t w[32]; // chunk arrives unaligned in the command buffer; NVMC wants word writes
  171. memcpy(w, d, len);
  172. nvmcWriteWords(s_base + off, w, len / 4);
  173. s_off = off + len;
  174. return FWUP_OK;
  175. }
  176. uint8_t fwupEnd(void)
  177. {
  178. if (!s_active) {
  179. // duplicate END (the panel retried because our ack got lost): if the commit it is asking about
  180. // is exactly the one already on the meta page, re-report success instead of ERR_STATE.
  181. const volatile FwupMeta *m =
  182. (const volatile FwupMeta *)FWUP_META;
  183. if (s_size && m->magic == FWUP_META_MAGIC &&
  184. m->size == s_size && m->crc == s_crc)
  185. return FWUP_OK;
  186. return FWUP_ERR_STATE;
  187. }
  188. if (s_off != s_size)
  189. return FWUP_ERR_STATE;
  190. s_active = false;
  191. // verify what actually landed in flash, not what we were sent
  192. if (crc32Flash(s_base, s_size) != s_crc)
  193. return FWUP_ERR_CRC;
  194. if (!vectorsPlausible(s_base, s_size))
  195. return FWUP_ERR_VECTOR;
  196. // COMMIT: this meta page write is the single arming action of the whole protocol
  197. FwupMeta m = { FWUP_META_MAGIC, s_size, s_crc, s_base, 0 };
  198. m.metaCrc = ~crc32Step(0xFFFFFFFFUL, (const uint8_t *)&m, 16);
  199. nvmcErasePage(FWUP_META);
  200. nvmcWriteWords(FWUP_META, (const uint32_t *)&m, 5);
  201. return FWUP_OK;
  202. }
  203. void fwupAbort(void)
  204. {
  205. s_active = false;
  206. nvmcErasePage(FWUP_META);
  207. }
  208. // Arm a full-board wipe: erase the meta page and stamp the wipe marker (self-complementary pair). Runs at
  209. // RUNTIME on the meta page only -- the same NVMC path fwupBegin()/fwupChunk() already use during a live
  210. // transfer, never the app region we execute from. Any staged update is dropped (its bytes are about to be
  211. // erased anyway). The caller reboots; fwupWipeIfArmed() does the erase from RAM at the next boot.
  212. void fwupArmFullWipe(void)
  213. {
  214. s_active = false;
  215. uint32_t w[2] = { FWUP_WIPE_MAGIC, ~FWUP_WIPE_MAGIC };
  216. nvmcErasePage(FWUP_META);
  217. nvmcWriteWords(FWUP_META, w, 2);
  218. }
  219. // ---- boot-time apply ---------------------------------------------------------------------------------------
  220. // Runs entirely from RAM (.data section => copied out of flash by the startup code) with interrupts off: it
  221. // erases and rewrites the very flash the rest of the firmware executes from, so from the first erase to the
  222. // reset it must not touch ANY flash-resident code or data -- only registers, its arguments, and the two
  223. // flash regions it copies between. No calls, no libc, volatile pointers so the compiler can't lift the loops
  224. // into memcpy/memcmp libcalls.
  225. //
  226. // Power-cut safety is in the ordering: all target pages are erased up front (the vector page first), pages
  227. // 1..N-1 are written forward, then the vector page is written BACKWARD so word 0 -- the app-validity marker
  228. // the bootloader checks -- is the final word written anywhere in the app. Interrupt the sequence at any
  229. // point and the board reads as app-less: the UF2 bootloader keeps it, drag-and-drop recovers it.
  230. #define FWUP_RAMFUNC \
  231. __attribute__((used, noinline, section(".data.opk_ramfunc")))
  232. FWUP_RAMFUNC static void ramApply(uint32_t dst, uint32_t src, uint32_t size,
  233. uint32_t metaPage, uint32_t blSettings)
  234. {
  235. volatile uint32_t *d = (volatile uint32_t *)dst;
  236. const volatile uint32_t *s = (const volatile uint32_t *)src;
  237. const uint32_t words = size / 4;
  238. const uint32_t pg0w = words < FWUP_PAGE / 4 ? words : FWUP_PAGE / 4;
  239. // 1) erase every target page; dst itself (the vector page) goes first
  240. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een;
  241. while (!NRF_NVMC->READY) {
  242. }
  243. for (uint32_t a = dst; a < dst + size; a += FWUP_PAGE) {
  244. NRF_WDT->RR[0] = WDT_RR_RR_Reload;
  245. NRF_NVMC->ERASEPAGE = a;
  246. while (!NRF_NVMC->READY) {
  247. }
  248. }
  249. // 2) write pages 1..N-1 forward, then the vector page backward (word 0 dead-last)
  250. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
  251. while (!NRF_NVMC->READY) {
  252. }
  253. for (uint32_t w = FWUP_PAGE / 4; w < words; w++) {
  254. if ((w & 1023) == 0)
  255. NRF_WDT->RR[0] = WDT_RR_RR_Reload;
  256. d[w] = s[w];
  257. while (!NRF_NVMC->READY) {
  258. }
  259. }
  260. for (uint32_t w = pg0w; w-- > 0;) {
  261. d[w] = s[w];
  262. while (!NRF_NVMC->READY) {
  263. }
  264. }
  265. // 3) verify the copy word-for-word against the staged source
  266. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
  267. while (!NRF_NVMC->READY) {
  268. }
  269. uint32_t bad = 0;
  270. for (uint32_t w = 0; w < words; w++) {
  271. if ((w & 4095) == 0)
  272. NRF_WDT->RR[0] = WDT_RR_RR_Reload;
  273. if (d[w] != s[w]) {
  274. bad = 1;
  275. break;
  276. }
  277. }
  278. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een;
  279. while (!NRF_NVMC->READY) {
  280. }
  281. if (bad) {
  282. // flash cell failure: make the app unambiguously invalid (bootloader keeps the board) instead
  283. // of resetting into a half-good image. Meta is erased below so this can't loop.
  284. NRF_NVMC->ERASEPAGE = dst;
  285. while (!NRF_NVMC->READY) {
  286. }
  287. } else {
  288. // 4) bootloader settings: leave the exact "valid app, CRC check disabled" state drag-and-drop
  289. // (MSC) flashing leaves. A previous adafruit-nrfutil serial-DFU flash records a real CRC16 here,
  290. // and the bootloader re-checks it EVERY boot -- stale, it would fail our new image straight into
  291. // DFU mode. Layout = SDK11 bootloader_settings_t; word 0 ([bank_0=BANK_VALID_APP][bank_0_crc=0])
  292. // is written last so an interrupted settings write falls back to the erased-settings path.
  293. NRF_WDT->RR[0] = WDT_RR_RR_Reload;
  294. NRF_NVMC->ERASEPAGE = blSettings;
  295. while (!NRF_NVMC->READY) {
  296. }
  297. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Wen;
  298. while (!NRF_NVMC->READY) {
  299. }
  300. volatile uint32_t *bs = (volatile uint32_t *)blSettings;
  301. bs[6] = 0; // sd_image_start
  302. while (!NRF_NVMC->READY) {
  303. }
  304. bs[5] = 0; // app_image_size (bank_0_size is the authoritative one)
  305. while (!NRF_NVMC->READY) {
  306. }
  307. bs[4] = 0; // bl_image_size
  308. while (!NRF_NVMC->READY) {
  309. }
  310. bs[3] = 0; // sd_image_size
  311. while (!NRF_NVMC->READY) {
  312. }
  313. bs[2] = size; // bank_0_size
  314. while (!NRF_NVMC->READY) {
  315. }
  316. bs[1] = 0x000000FEUL; // bank_1 = BANK_ERASED
  317. while (!NRF_NVMC->READY) {
  318. }
  319. bs[0] = 0x00000001UL; // bank_0 = BANK_VALID_APP, bank_0_crc = 0 ("not used")
  320. while (!NRF_NVMC->READY) {
  321. }
  322. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een;
  323. while (!NRF_NVMC->READY) {
  324. }
  325. }
  326. // 5) disarm: erase the meta page so the (old or new) firmware boots normally from here on
  327. NRF_NVMC->ERASEPAGE = metaPage;
  328. while (!NRF_NVMC->READY) {
  329. }
  330. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
  331. while (!NRF_NVMC->READY) {
  332. }
  333. // 6) reset -- raw AIRCR write; NVIC_SystemReset() is inline but keep even its helpers out of the path
  334. __asm volatile("dsb 0xF" ::: "memory");
  335. *(volatile uint32_t *)0xE000ED0CUL = (0x5FAUL << 16) | (1UL << 2);
  336. for (;;) {
  337. }
  338. }
  339. void fwupApplyIfArmed(void)
  340. {
  341. // materialize a reference to the capability tag so the linker's --gc-sections keeps it in the image
  342. __asm volatile("" ::"r"(FWUP_TAG));
  343. const volatile FwupMeta *m = (const volatile FwupMeta *)FWUP_META;
  344. if (m->magic != FWUP_META_MAGIC)
  345. return; // erased page (0xFFFFFFFF) = nothing armed: the common path, zero boot cost
  346. // snapshot, then validate EVERYTHING before touching any flash; anything off -> disarm and boot normally
  347. FwupMeta v = { m->magic, m->size, m->crc, m->staged, m->metaCrc };
  348. bool ok = v.metaCrc ==
  349. (uint32_t)~crc32Step(0xFFFFFFFFUL, (const uint8_t *)&v, 16);
  350. ok = ok && v.size && v.size <= FWUP_MAX_IMG && (v.size & 3) == 0;
  351. ok = ok && v.staged >= FWUP_APP_BASE + FWUP_MAX_IMG &&
  352. v.staged + v.size <= FWUP_META &&
  353. (v.staged & (FWUP_PAGE - 1)) == 0;
  354. ok = ok && crc32Flash(v.staged, v.size) ==
  355. v.crc; // staged bytes still intact?
  356. ok = ok && vectorsPlausible(v.staged, v.size);
  357. if (!ok) {
  358. nvmcErasePage(FWUP_META);
  359. return;
  360. }
  361. // Point of no return. Freeze the system: nothing may run from app flash once the copy starts. The WDT
  362. // is forced on first (it survives soft reset if the previous boot armed it, but not power-on), so even
  363. // a fault inside the copier resets into the safe app-less/bootloader state instead of hanging forever.
  364. __disable_irq();
  365. if (!NRF_WDT->RUNSTATUS) {
  366. NRF_WDT->CONFIG =
  367. (WDT_CONFIG_HALT_Pause << WDT_CONFIG_HALT_Pos) |
  368. (WDT_CONFIG_SLEEP_Run << WDT_CONFIG_SLEEP_Pos);
  369. NRF_WDT->CRV = 8UL * 32768UL - 1; // ~8 s, same as the app's own
  370. NRF_WDT->RREN = WDT_RREN_RR0_Msk;
  371. NRF_WDT->TASKS_START = 1;
  372. }
  373. NRF_WDT->RR[0] = WDT_RR_RR_Reload;
  374. // call through a volatile pointer: the compiler must materialize the RAM address and may not inline the
  375. // copier back into flash-resident code
  376. void (*volatile apply)(uint32_t, uint32_t, uint32_t, uint32_t,
  377. uint32_t) = ramApply;
  378. apply(FWUP_APP_BASE, v.staged, v.size, FWUP_META, FWUP_BL_SETTINGS);
  379. for (;;) {
  380. } // unreachable (ramApply resets)
  381. }
  382. // ---- full-board wipe (debug-only "erase everything") -------------------------------------------------------
  383. // Like ramApply, runs entirely from RAM with interrupts off -- it erases the very flash it would otherwise
  384. // execute from, so no calls/libc/flash constants, only registers + arguments + NVMC. It erases the ENTIRE app
  385. // region, the LittleFS config/bond region, and the bootloader settings page; the MBR, SoftDevice, and the
  386. // bootloader itself are left intact so the board still enumerates as the UF2 mass-storage drive. Afterwards no
  387. // valid app exists, so the Adafruit bootloader stays in UF2 mode on EVERY boot until firmware is flashed again.
  388. // Power-cut safe by the same ordering as ramApply: the app vector page is erased FIRST, so the board reads
  389. // app-less from that instant on -- an interrupted wipe still lands in the safe UF2-bootloader state.
  390. FWUP_RAMFUNC static void ramWipe(uint32_t appBase, uint32_t appEnd,
  391. uint32_t fsBase, uint32_t fsEnd,
  392. uint32_t blSettings)
  393. {
  394. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Een;
  395. while (!NRF_NVMC->READY) {
  396. }
  397. // app vector page first (word 0 = the bootloader's app-validity marker)
  398. NRF_WDT->RR[0] = WDT_RR_RR_Reload;
  399. NRF_NVMC->ERASEPAGE = appBase;
  400. while (!NRF_NVMC->READY) {
  401. }
  402. // rest of the app region (code, rodata, .data-init, staged-image scratch, the wipe marker itself)
  403. for (uint32_t a = appBase + FWUP_PAGE; a < appEnd; a += FWUP_PAGE) {
  404. NRF_WDT->RR[0] = WDT_RR_RR_Reload;
  405. NRF_NVMC->ERASEPAGE = a;
  406. while (!NRF_NVMC->READY) {
  407. }
  408. }
  409. // LittleFS: cfg.bin (settings) + bonds.bin (pairings) -- no user data survives
  410. for (uint32_t a = fsBase; a < fsEnd; a += FWUP_PAGE) {
  411. NRF_WDT->RR[0] = WDT_RR_RR_Reload;
  412. NRF_NVMC->ERASEPAGE = a;
  413. while (!NRF_NVMC->READY) {
  414. }
  415. }
  416. // bootloader settings: drop any "valid app" record so the boot check relies only on the (now erased)
  417. // app vector -> UF2 mode every boot.
  418. NRF_WDT->RR[0] = WDT_RR_RR_Reload;
  419. NRF_NVMC->ERASEPAGE = blSettings;
  420. while (!NRF_NVMC->READY) {
  421. }
  422. NRF_NVMC->CONFIG = NVMC_CONFIG_WEN_Ren;
  423. while (!NRF_NVMC->READY) {
  424. }
  425. // reset -- raw AIRCR write (keep NVIC_SystemReset's helpers out of the now-erased flash)
  426. __asm volatile("dsb 0xF" ::: "memory");
  427. *(volatile uint32_t *)0xE000ED0CUL = (0x5FAUL << 16) | (1UL << 2);
  428. for (;;) {
  429. }
  430. }
  431. void fwupWipeIfArmed(void)
  432. {
  433. const volatile uint32_t *m = (const volatile uint32_t *)FWUP_META;
  434. if (m[0] != FWUP_WIPE_MAGIC || m[1] != (uint32_t)~FWUP_WIPE_MAGIC)
  435. return; // not armed (the common path): erased page or a FwupMeta -> nothing to do
  436. // Point of no return -- identical freeze discipline to fwupApplyIfArmed: IRQs off, WDT forced on so a
  437. // fault mid-erase still resets into the safe app-less state. The erase (~200 pages) drops USB the instant
  438. // it starts; the WDT is reloaded per page so the ~18 s run never trips it.
  439. __disable_irq();
  440. if (!NRF_WDT->RUNSTATUS) {
  441. NRF_WDT->CONFIG =
  442. (WDT_CONFIG_HALT_Pause << WDT_CONFIG_HALT_Pos) |
  443. (WDT_CONFIG_SLEEP_Run << WDT_CONFIG_SLEEP_Pos);
  444. NRF_WDT->CRV = 8UL * 32768UL - 1; // ~8 s
  445. NRF_WDT->RREN = WDT_RREN_RR0_Msk;
  446. NRF_WDT->TASKS_START = 1;
  447. }
  448. NRF_WDT->RR[0] = WDT_RR_RR_Reload;
  449. void (*volatile wipe)(uint32_t, uint32_t, uint32_t, uint32_t,
  450. uint32_t) = ramWipe;
  451. wipe(FWUP_APP_BASE, FWUP_APP_END, FWUP_FS_BASE, FWUP_FS_END,
  452. FWUP_BL_SETTINGS);
  453. for (;;) {
  454. } // unreachable (ramWipe resets)
  455. }