A truly modular keyboard
C
firmware: BOOTLOCK and BOOTCFG? to boot from flash only, from the app
Nathan2c40221a3b98
diff --git a/app/PROTOCOL.md b/app/PROTOCOL.mdindex 9de07db..e0ba5f4 100644--- a/app/PROTOCOL.md+++ b/app/PROTOCOL.md@@ -84,6 +84,8 @@ The Keyword lists the features it supports in `HELLO`'s `caps`. The app only sen| `ota` | `OTA` | `OK OTA`, then the Keyword restarts into the OTA bootloader after about 500 ms. `ERR no BLE stack on CPU2, use DFU` if it can't || (any) | `DFU` | `OK DFU`, then the Keyword restarts into the ROM USB DFU bootloader || (any) | `STACK?` | `STACK ble v=1.24.0 fus=2.2.0`, `STACK none fus=<x.y.z>` or `STACK unknown` |+| (any) | `BOOTCFG?` | `BOOTCFG nBOOT0=<0\|1> nSWBOOT0=<0\|1> nBOOT1=<0\|1> RDP=0x<2hex>` |+| (USB serial only) | `BOOTLOCK yes` | `OK BOOTLOCK, reloading option bytes`, then a reset that boots from flash only (BOOT0 ignored). `OK BOOTLOCK already`, or `ERR <reason>` before the app has run 10 s, without the BLE stack, or when RDP isn't level 0 |How each one behaves:diff --git a/firmware/Core/Inc/bootcfg.h b/firmware/Core/Inc/bootcfg.hnew file mode 100644index 0000000..4a680cb--- /dev/null+++ b/firmware/Core/Inc/bootcfg.h@@ -0,0 +1,59 @@+/*+ * Boot option bytes (FLASH_OPTR). BOOTLOCK makes the keyword always boot+ * from flash: nSWBOOT0 = 0 (BOOT0 pin ignored) and nBOOT0 = 1. It changes+ * only those two bits. The inline part has no HAL, so it runs in the host+ * test (test/bootcfg_test.c).+ */+#ifndef BOOTCFG_H+#define BOOTCFG_H++#include <stddef.h>+#include <stdint.h>+#include <stdio.h>++/* FLASH_OPTR bits, RM0434 (same values as stm32wb55xx.h) */+#define BOOTCFG_RDP 0x000000FFu+#define BOOTCFG_nBOOT1 (1u << 23)+#define BOOTCFG_nSWBOOT0 (1u << 26)+#define BOOTCFG_nBOOT0 (1u << 27)+#define BOOTCFG_RDP_LEVEL0 0xAAu+#define BOOTCFG_MIN_UPTIME 10000u /* ms, same as BOOT_HEALTHY_MS */++/* OPTR with only nSWBOOT0 cleared and nBOOT0 set. */+static inline uint32_t BOOTCFG_LockValue(uint32_t optr)+{+ return (optr & ~BOOTCFG_nSWBOOT0) | BOOTCFG_nBOOT0;+}++static inline int BOOTCFG_IsLocked(uint32_t optr)+{+ return BOOTCFG_LockValue(optr) == optr;+}++/* NULL when BOOTLOCK may write, else why not. */+static inline const char *BOOTCFG_Check(uint32_t optr, uint32_t uptime_ms, int stack_running)+{+ if (uptime_ms <= BOOTCFG_MIN_UPTIME) return "app has not run 10 s yet";+ if (!stack_running) return "BLE stack is not running";+ if ((optr & BOOTCFG_RDP) != BOOTCFG_RDP_LEVEL0) return "RDP is not level 0";+ return NULL;+}++static inline void BOOTCFG_Format(uint32_t optr, char *out, size_t n)+{+ snprintf(out, n, "nBOOT0=%u nSWBOOT0=%u nBOOT1=%u RDP=0x%02X",+ (unsigned)!!(optr & BOOTCFG_nBOOT0), (unsigned)!!(optr & BOOTCFG_nSWBOOT0),+ (unsigned)!!(optr & BOOTCFG_nBOOT1), (unsigned)(optr & BOOTCFG_RDP));+}++/* "nBOOT0=1 nSWBOOT0=1 nBOOT1=1 RDP=0xAA" from the live option bytes. */+void BOOTCFG_Info(char *out, size_t n);++/* 1 when the option bytes already boot from flash only. */+int BOOTCFG_Locked(void);++/* Writes nSWBOOT0 = 0, nBOOT0 = 1 and schedules the option byte reload+ * (a reset) in 500 ms. NULL on success, else why it didn't write. */+const char *BOOTCFG_Lock(void);++#endif /* BOOTCFG_H */diff --git a/firmware/Core/Inc/ota.h b/firmware/Core/Inc/ota.hindex 48889a0..b7275f3 100644--- a/firmware/Core/Inc/ota.h+++ b/firmware/Core/Inc/ota.h@@ -24,6 +24,9 @@ void OTA_RequestDfu(void);/* Plain reset back into the app in 500 ms. */void OTA_ResetSoon(void);+/* Option byte reload (OBL_LAUNCH, a reset) in 500 ms, after BOOTCFG_Lock. */+void OTA_ObLaunchSoon(void);+/* Call every main loop pass: feeds the bootloader's IWDG and marks the* app healthy (crash count zeroed) after it has run 10 s. */void OTA_Service(void);diff --git a/firmware/Core/Src/bootcfg.c b/firmware/Core/Src/bootcfg.cnew file mode 100644index 0000000..4034228--- /dev/null+++ b/firmware/Core/Src/bootcfg.c@@ -0,0 +1,73 @@+#include "app_common.h"+#include "main.h"+#include "shci.h"+#include "utilities_conf.h"+#include "ota.h"+#include "bootcfg.h"++void BOOTCFG_Info(char *out, size_t n)+{+ BOOTCFG_Format(FLASH->OPTR, out, n);+}++int BOOTCFG_Locked(void)+{+ return BOOTCFG_IsLocked(FLASH->OPTR);+}++/* OPTSTRT when neither CPU is blocking flash access, the same handshake+ * as keymap.c's Flash_Op (ST's BLE_RfWithFlash sequence). */+static HAL_StatusTypeDef Program(FLASH_OBProgramInitTypeDef *ob)+{+ for (int tries = 0; tries < 1000; tries++)+ {+ HAL_StatusTypeDef st = HAL_BUSY;+ while (LL_FLASH_IsActiveFlag_OperationSuspended());+ UTILS_ENTER_CRITICAL_SECTION();+ if (!LL_HSEM_GetStatus(HSEM, CFG_HW_BLOCK_FLASH_REQ_BY_CPU1_SEMID)+ && LL_HSEM_1StepLock(HSEM, CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID) == 0)+ {+ st = HAL_FLASHEx_OBProgram(ob);+ LL_HSEM_ReleaseLock(HSEM, CFG_HW_BLOCK_FLASH_REQ_BY_CPU2_SEMID, 0);+ }+ UTILS_EXIT_CRITICAL_SECTION();+ if (st != HAL_BUSY) return st;+ HAL_Delay(1); /* CPU2 is in a radio event, try again */+ }+ return HAL_TIMEOUT;+}++const char *BOOTCFG_Lock(void)+{+ uint32_t old = FLASH->OPTR;+ const char *why = BOOTCFG_Check(old, HAL_GetTick(), OTA_Available());+ if (why) return why;+ uint32_t want = BOOTCFG_LockValue(old);++ /* OPTIONBYTE_USER alone: HAL keeps the current RDP byte and every user+ * bit outside UserType (FLASH_OB_OptrConfig). */+ FLASH_OBProgramInitTypeDef ob = {+ .OptionType = OPTIONBYTE_USER,+ .UserType = OB_USER_nBOOT0 | OB_USER_nSWBOOT0,+ .UserConfig = want & (BOOTCFG_nBOOT0 | BOOTCFG_nSWBOOT0),+ };+ while (LL_HSEM_1StepLock(HSEM, CFG_HW_FLASH_SEMID));+ HAL_FLASH_Unlock();+ HAL_FLASH_OB_Unlock();+ __HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);+ SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_ON);+ HAL_StatusTypeDef st = Program(&ob);+ SHCI_C2_FLASH_EraseActivity(ERASE_ACTIVITY_OFF);+ uint32_t now = FLASH->OPTR;+ LL_HSEM_ReleaseLock(HSEM, CFG_HW_FLASH_SEMID, 0);++ if (st != HAL_OK || now != want)+ {+ /* Not reloaded: the old option bytes stay in force until a power cycle */+ HAL_FLASH_OB_Lock();+ HAL_FLASH_Lock();+ return "option byte write failed, send BOOTCFG? before any reset";+ }+ OTA_ObLaunchSoon(); /* flash stays unlocked: OBL_LAUNCH needs OPTLOCK clear */+ return NULL;+}diff --git a/firmware/Core/Src/ota.c b/firmware/Core/Src/ota.cindex 3941165..81be670 100644--- a/firmware/Core/Src/ota.c+++ b/firmware/Core/Src/ota.c@@ -14,7 +14,7 @@ PLACE_IN_SECTION("TAG_OTA_START") const uint32_t MagicKeywordAddress = (uint32_t#define RESET_DELAY_MS 500-enum { RESET_PLAIN, RESET_OTA, RESET_DFU };+enum { RESET_PLAIN, RESET_OTA, RESET_DFU, RESET_OBL };static uint8_t reset_timer_id;static uint8_t reset_mode;@@ -22,6 +22,15 @@ static uint8_t healthy;static void Reset_Now(void){+ if (reset_mode == RESET_OBL)+ {+ /* Option byte reload, a reset with the new option bytes. BOOTCFG_Lock+ * left the flash unlocked; the unlocks are no-ops then. */+ BOOT_REBOOT_VAL_MSG = BOOT_REBOOT_ON_FW_APP;+ HAL_FLASH_Unlock();+ HAL_FLASH_OB_Unlock();+ HAL_FLASH_OB_Launch();+ }if (reset_mode == RESET_DFU){BOOT_SYSDFU_REQ_WORD = BOOT_SYSDFU_REQ_MAGIC;@@ -69,6 +78,11 @@ void OTA_ResetSoon(void)Reset_Later(RESET_PLAIN);}+void OTA_ObLaunchSoon(void)+{+ Reset_Later(RESET_OBL);+}+/* Every main loop pass: feed the IWDG the bootloader started (no effect* if it didn't), and after BOOT_HEALTHY_MS of running tell the bootloader* this app is fine by zeroing its crash count. */diff --git a/firmware/Core/Src/proto.c b/firmware/Core/Src/proto.cindex 166f0db..d27f075 100644--- a/firmware/Core/Src/proto.c+++ b/firmware/Core/Src/proto.c@@ -6,6 +6,7 @@#include "keybit_image.h"#include "ota.h"#include "fus.h"+#include "bootcfg.h"#include "proto.h"/* Optional features the desktop app can use, only what's implemented.@@ -227,6 +228,35 @@ static void Handle_Line(proto_port_t *port, char *cmd)Reply(port, "OK DFU");OTA_RequestDfu();}+ else if (strcmp(cmd, "BOOTCFG?") == 0)+ {+ char info[48];+ BOOTCFG_Info(info, sizeof(info));+ Reply(port, "BOOTCFG %s", info);+ }+ else if (strcmp(cmd, "BOOTLOCK") == 0)+ {+ /* Boot from flash always, BOOT0 ignored. Option bytes: USB only, and+ * only once the app and the BLE stack have proven themselves. */+ if (!port->local)+ {+ Reply(port, "ERR BOOTLOCK is USB only");+ return;+ }+ if (strcmp(args, "yes") != 0)+ {+ Reply(port, "ERR send BOOTLOCK yes to always boot from flash (BOOT0 ignored)");+ return;+ }+ if (BOOTCFG_Locked())+ {+ Reply(port, "OK BOOTLOCK already");+ return;+ }+ const char *why = BOOTCFG_Lock();+ if (why) Reply(port, "ERR %s", why);+ else Reply(port, "OK BOOTLOCK, reloading option bytes");+ }else if (strcmp(cmd, "BATT?") == 0){const batt_t *b = BATT_Get();diff --git a/firmware/STM32Make.make b/firmware/STM32Make.makeindex 1d160af..2784333 100644--- a/firmware/STM32Make.make+++ b/firmware/STM32Make.make@@ -109,6 +109,7 @@ Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_rcc.c \Drivers/STM32WBxx_HAL_Driver/Src/stm32wbxx_ll_usb.c \Core/Src/app_entry.c \Core/Src/batt.c \+Core/Src/bootcfg.c \Core/Src/hw_timerserver.c \Core/Src/keyact.c \Core/Src/keymap.c \diff --git a/firmware/test/bootcfg_test.c b/firmware/test/bootcfg_test.cnew file mode 100644index 0000000..265ea87--- /dev/null+++ b/firmware/test/bootcfg_test.c@@ -0,0 +1,60 @@+/*+ * Host check for the option byte math in Core/Inc/bootcfg.h: BOOTLOCK+ * changes nSWBOOT0 and nBOOT0 and nothing else, RDP above all.+ * cc -std=c99 -ICore/Inc test/bootcfg_test.c -o /tmp/bootcfg_test && /tmp/bootcfg_test+ */+#include <assert.h>+#include <stdio.h>+#include <string.h>+#include "bootcfg.h"++/* An OPTR with RDP level 0 and BOOT0 from the pin (nSWBOOT0 = 1) */+#define FACTORY 0x3FFFF1AAu++static uint32_t rng = 1;+static uint32_t next(void)+{+ rng = rng * 1103515245u + 12345u;+ return (rng >> 16) | (rng << 16);+}++int main(void)+{+ const uint32_t two = BOOTCFG_nSWBOOT0 | BOOTCFG_nBOOT0;++ /* the bits match the CMSIS header (stm32wb55xx.h FLASH_OPTR_*) */+ assert(BOOTCFG_nBOOT1 == 0x00800000u && BOOTCFG_nSWBOOT0 == 0x04000000u);+ assert(BOOTCFG_nBOOT0 == 0x08000000u && BOOTCFG_RDP == 0xFFu);++ uint32_t v = BOOTCFG_LockValue(FACTORY);+ assert(!(v & BOOTCFG_nSWBOOT0) && (v & BOOTCFG_nBOOT0));+ assert((v & ~two) == (FACTORY & ~two));+ assert(BOOTCFG_IsLocked(v) && !BOOTCFG_IsLocked(FACTORY));+ assert(BOOTCFG_LockValue(v) == v); /* a second BOOTLOCK writes nothing new */++ /* every other bit, RDP included, comes through untouched for any OPTR */+ for (int i = 0; i < 100000; i++)+ {+ uint32_t o = next();+ uint32_t n = BOOTCFG_LockValue(o);+ assert((n & ~two) == (o & ~two));+ assert((n & BOOTCFG_RDP) == (o & BOOTCFG_RDP));+ assert((n & two) == BOOTCFG_nBOOT0);+ }++ /* refused unless 10 s up, stack running and RDP level 0 */+ assert(strcmp(BOOTCFG_Check(FACTORY, 10000, 1), "app has not run 10 s yet") == 0);+ assert(strcmp(BOOTCFG_Check(FACTORY, 20000, 0), "BLE stack is not running") == 0);+ assert(strcmp(BOOTCFG_Check((FACTORY & ~0xFFu) | 0xBB, 20000, 1), "RDP is not level 0") == 0);+ assert(strcmp(BOOTCFG_Check((FACTORY & ~0xFFu) | 0xCC, 20000, 1), "RDP is not level 0") == 0);+ assert(BOOTCFG_Check(FACTORY, 10001, 1) == NULL);++ char s[64];+ BOOTCFG_Format(FACTORY, s, sizeof(s));+ assert(strcmp(s, "nBOOT0=1 nSWBOOT0=1 nBOOT1=1 RDP=0xAA") == 0);+ BOOTCFG_Format(v, s, sizeof(s));+ assert(strcmp(s, "nBOOT0=1 nSWBOOT0=0 nBOOT1=1 RDP=0xAA") == 0);++ puts("bootcfg_test: ok");+ return 0;+}diff --git a/firmware/test/proto_test.c b/firmware/test/proto_test.cindex d4fe352..055927f 100644--- a/firmware/test/proto_test.c+++ b/firmware/test/proto_test.c@@ -32,6 +32,11 @@ static const char *fus_refuse;void FUS_Info(char *out, size_t n) { snprintf(out, n, "v=2.2.0 state=idle stack=none sfsa=0xF4 image=129460"); }const char *FUS_Install(void) { if (fus_refuse) return fus_refuse; fus_installs++; return NULL; }int FUS_Busy(void) { return 0; }+static int boot_locked, boot_writes;+static const char *boot_refuse;+void BOOTCFG_Info(char *out, size_t n) { snprintf(out, n, "nBOOT0=1 nSWBOOT0=%d nBOOT1=1 RDP=0xAA", !boot_locked); }+int BOOTCFG_Locked(void) { return boot_locked; }+const char *BOOTCFG_Lock(void) { if (boot_refuse) return boot_refuse; boot_writes++; boot_locked = 1; return NULL; }static batt_t batt_fake;const batt_t *BATT_Get(void) { return &batt_fake; }int KEYMAP_Save(void) { return 0; }@@ -218,6 +223,22 @@ int main(void)assert(ota_asked == 1);expect("DFU\n", "OK DFU\n");assert(dfu_asked == 1);+ /* BOOTCFG? and BOOTLOCK: USB only, exactly "BOOTLOCK yes" */+ expect("BOOTCFG?\n", "BOOTCFG nBOOT0=1 nSWBOOT0=1 nBOOT1=1 RDP=0xAA\n");+ expect("BOOTLOCK\n", "ERR send BOOTLOCK yes to always boot from flash (BOOT0 ignored)\n");+ expect("BOOTLOCK YES\n", "ERR send BOOTLOCK yes to always boot from flash (BOOT0 ignored)\n");+ expect("BOOTLOCK yes please\n", "ERR send BOOTLOCK yes to always boot from flash (BOOT0 ignored)\n");+ boot_refuse = "app has not run 10 s yet";+ expect("BOOTLOCK yes\n", "ERR app has not run 10 s yet\n");+ boot_refuse = NULL;+ out2[0] = 0;+ PROTO_Input(&port2, (const uint8_t *)"BOOTLOCK yes\n", 13); /* port2 is like NUS */+ assert(strcmp(out2, "ERR BOOTLOCK is USB only\n") == 0 && boot_writes == 0);+ expect("BOOTLOCK yes\n", "OK BOOTLOCK, reloading option bytes\n");+ expect("BOOTLOCK yes\n", "OK BOOTLOCK already\n");+ assert(boot_writes == 1);+ expect("BOOTCFG?\n", "BOOTCFG nBOOT0=1 nSWBOOT0=0 nBOOT1=1 RDP=0xAA\n");+/* FUS? and STACK INSTALL (USB only) */expect("FUS?\n", "FUS v=2.2.0 state=idle stack=none sfsa=0xF4 image=129460\n");expect("STACK INSTALL\n", "OK STACK INSTALL\n");