fw_update.cpp 18 KB

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