/* Hexapod dongle - stage 1 "safeboot" (permanent recovery bootloader). * * Lives in the first 4 KB of flash (pages 0-3) and runs first on every reset. * It speaks a tiny fixed-baud (115200 8N1) flashing protocol over USB and can * always erase/rewrite the application (pages 4-15). It is flashed ONCE and the * host flasher never erases it, so no application update can remove the * wire-free recovery path. See ../README.md. * * Entry decisions: * - soft reset with MAGIC_FLASH set -> enter flash mode immediately * - else print banner, open a knock window; host 'S' -> flash mode * - else if the app looks valid -> jump to it * - else (no valid app) -> stay in flash mode (recovery) * * Flash-mode protocol (host -> safeboot, each replies one byte 'K' ok / 'E' err * unless noted): * 'P' ping -> 'K' * 'E' erase app region (pages 4-15) * 'W' a0 a1 a2 a3 n d[n] write n bytes (n even, <=128) at little-endian addr * 'R' a0 a1 a2 a3 n read n bytes at addr -> sends n bytes (no K) * 'G' 'K', then boot the app (or reset) * 'V' -> prints "SBV1\n" (identify) */ #include "common.h" #define KNOCK_MS 1500 /* ---- startup (custom: read the magic word before touching RAM) ---- */ extern uint32_t _sidata, _sdata, _edata, _sbss, _ebss, _estack; int sb_main(int forced); void Reset_Handler(void) { uint32_t m = REG(MAGIC_ADDR); REG(MAGIC_ADDR) = 0; uint32_t *src = &_sidata, *dst = &_sdata; while (dst < &_edata) *dst++ = *src++; for (dst = &_sbss; dst < &_ebss;) *dst++ = 0; sb_main(m == MAGIC_FLASH); for (;;) {} } void Default_Handler(void) { for (;;) {} } __attribute__((section(".isr_vector"), used)) void (*const g_vectors[])(void) = { (void (*)(void))&_estack, Reset_Handler, Default_Handler, Default_Handler, 0, 0, 0, 0, 0, 0, 0, Default_Handler, 0, 0, Default_Handler, Default_Handler, }; /* ---- flash programming ---- */ static void flash_unlock(void) { if (FLASH_CR & (1u << 7)) { FLASH_KEYR = 0x45670123; FLASH_KEYR = 0xCDEF89AB; } } static int flash_wait(void) { while (FLASH_SR & (1u << 0)) {} /* BSY */ uint32_t sr = FLASH_SR; FLASH_SR = (1u << 5) | (1u << 2) | (1u << 4); /* clear EOP/PGERR/WRPRT */ return !(sr & ((1u << 2) | (1u << 4))); /* ok if no error bits */ } static int flash_erase_page(uint32_t addr) { flash_wait(); FLASH_CR |= (1u << 1); /* PER */ FLASH_AR = addr; FLASH_CR |= (1u << 6); /* STRT */ int ok = flash_wait(); FLASH_CR &= ~(1u << 1); return ok; } static int flash_prog_hw(uint32_t addr, uint16_t hw) { flash_wait(); FLASH_CR |= (1u << 0); /* PG */ *(volatile uint16_t *)addr = hw; int ok = flash_wait(); FLASH_CR &= ~(1u << 0); return ok && (*(volatile uint16_t *)addr == hw); } static int erase_app(void) { flash_unlock(); for (int p = APP_PAGE_LO; p <= APP_PAGE_HI; p++) if (!flash_erase_page(0x08000000u + p * PAGE_SIZE)) return 0; return 1; } static uint32_t rd4(void) { uint32_t v = uart_getc_block(); v |= (uint32_t)uart_getc_block() << 8; v |= (uint32_t)uart_getc_block() << 16; v |= (uint32_t)uart_getc_block() << 24; return v; } static void boot_app(void) { uint32_t sp = *(volatile uint32_t *)APP_BASE; uint32_t pc = *(volatile uint32_t *)(APP_BASE + 4); if (sp >= 0x20000000u && sp <= 0x20001000u) { __asm volatile("msr msp, %0" : : "r"(sp)); ((void (*)(void))pc)(); } } static void flash_loop(void) { flash_unlock(); for (;;) { int c = uart_getc_block(); if (c == 'P') uart_putc('K'); else if (c == 'V') uart_puts("SBV1\r\n"); else if (c == 'E') uart_putc(erase_app() ? 'K' : 'E'); else if (c == 'W') { uint32_t a = rd4(); uint8_t n = uart_getc_block(); uint8_t buf[128]; for (uint8_t i = 0; i < n; i++) buf[i] = uart_getc_block(); int ok = (n <= 128) && !(n & 1) && a >= APP_BASE && (a + n) <= 0x08004000u; for (uint8_t i = 0; ok && i < n; i += 2) ok = flash_prog_hw(a + i, (uint16_t)(buf[i] | (buf[i + 1] << 8))); uart_putc(ok ? 'K' : 'E'); } else if (c == 'R') { uint32_t a = rd4(); uint8_t n = uart_getc_block(); for (uint8_t i = 0; i < n; i++) uart_putc(*(volatile uint8_t *)(a + i)); } else if (c == 'G') { uart_putc('K'); boot_app(); /* if it returns, stay */ } } } int sb_main(int forced) { uart_init8n1(); if (forced) { uart_puts("SBF\r\n"); flash_loop(); } uart_puts("SB1\r\n"); /* cold boot: offer a knock window */ systick_setup(); uint32_t ms = 0; while (ms < KNOCK_MS) { int c = uart_getc(); if (c == 'S') { uart_putc('K'); flash_loop(); } if (STK_CTRL & 0x10000) ms++; } STK_CTRL = 0; boot_app(); /* valid app -> never returns */ uart_puts("SBR\r\n"); /* no app -> recovery flash mode */ flash_loop(); return 0; }