Bootloader: always treat initial state as tainted. Firmware: mark boot state as tainted on boot. (#558)

This commit is contained in:
あく 2021-07-02 16:02:36 +03:00 committed by GitHub
parent df9a6673da
commit b6d5b5cb74
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 39 additions and 20 deletions

View File

@ -13,9 +13,9 @@
#include <api-hal.h> #include <api-hal.h>
// Boot request enum // Boot request enum
#define BOOT_REQUEST_NONE 0x00000000 #define BOOT_REQUEST_TAINTED 0x00000000
#define BOOT_REQUEST_CLEAN 0xDADEDADE
#define BOOT_REQUEST_DFU 0xDF00B000 #define BOOT_REQUEST_DFU 0xDF00B000
#define BOOT_REQUEST_TAINTED 0xDF00F000
// Boot to DFU pin // Boot to DFU pin
#define BOOT_DFU_PORT GPIOB #define BOOT_DFU_PORT GPIOB
#define BOOT_DFU_PIN LL_GPIO_PIN_11 #define BOOT_DFU_PIN LL_GPIO_PIN_11
@ -136,13 +136,13 @@ void target_init() {
} }
int target_is_dfu_requested() { int target_is_dfu_requested() {
if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_DFU) { if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_TAINTED) {
return 1; // Default system state is tainted
} else if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_TAINTED) { // We must ensure that MCU is cleanly booted
// We came here directly from STM bootloader and chip is unusable LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_CLEAN);
// One more reset required to fix it
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_NONE);
NVIC_SystemReset(); NVIC_SystemReset();
} else if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_DFU) {
return 1;
} }
LL_mDelay(100); LL_mDelay(100);
if(!LL_GPIO_IsInputPinSet(BOOT_DFU_PORT, BOOT_DFU_PIN)) { if(!LL_GPIO_IsInputPinSet(BOOT_DFU_PORT, BOOT_DFU_PIN)) {

View File

@ -13,9 +13,9 @@
#include <api-hal.h> #include <api-hal.h>
// Boot request enum // Boot request enum
#define BOOT_REQUEST_NONE 0x00000000 #define BOOT_REQUEST_TAINTED 0x00000000
#define BOOT_REQUEST_CLEAN 0xDADEDADE
#define BOOT_REQUEST_DFU 0xDF00B000 #define BOOT_REQUEST_DFU 0xDF00B000
#define BOOT_REQUEST_TAINTED 0xDF00F000
// Boot to DFU pin // Boot to DFU pin
#define BOOT_DFU_PORT GPIOB #define BOOT_DFU_PORT GPIOB
#define BOOT_DFU_PIN LL_GPIO_PIN_11 #define BOOT_DFU_PIN LL_GPIO_PIN_11
@ -136,13 +136,13 @@ void target_init() {
} }
int target_is_dfu_requested() { int target_is_dfu_requested() {
if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_DFU) { if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_TAINTED) {
return 1; // Default system state is tainted
} else if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_TAINTED) { // We must ensure that MCU is cleanly booted
// We came here directly from STM bootloader and chip is unusable LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_CLEAN);
// One more reset required to fix it
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_NONE);
NVIC_SystemReset(); NVIC_SystemReset();
} else if(LL_RTC_BAK_GetRegister(RTC, LL_RTC_BKP_DR0) == BOOT_REQUEST_DFU) {
return 1;
} }
LL_mDelay(100); LL_mDelay(100);
if(!LL_GPIO_IsInputPinSet(BOOT_DFU_PORT, BOOT_DFU_PIN)) { if(!LL_GPIO_IsInputPinSet(BOOT_DFU_PORT, BOOT_DFU_PIN)) {

View File

@ -17,6 +17,9 @@ typedef enum {
ApiHalBootFlagFactoryReset=1, ApiHalBootFlagFactoryReset=1,
} ApiHalBootFlag; } ApiHalBootFlag;
/** Initialize boot subsystem */
void api_hal_boot_init();
/** Set boot mode */ /** Set boot mode */
void api_hal_boot_set_mode(ApiHalBootMode mode); void api_hal_boot_set_mode(ApiHalBootMode mode);

View File

@ -1,12 +1,18 @@
#include <api-hal-boot.h> #include <api-hal-boot.h>
#include <stm32wbxx_ll_rtc.h> #include <stm32wbxx_ll_rtc.h>
#define BOOT_REQUEST_NONE 0x00000000 // Boot request enum
#define BOOT_REQUEST_TAINTED 0x00000000
#define BOOT_REQUEST_CLEAN 0xDADEDADE
#define BOOT_REQUEST_DFU 0xDF00B000 #define BOOT_REQUEST_DFU 0xDF00B000
void api_hal_boot_init() {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_TAINTED);
}
void api_hal_boot_set_mode(ApiHalBootMode mode) { void api_hal_boot_set_mode(ApiHalBootMode mode) {
if (mode == ApiHalBootModeNormal) { if (mode == ApiHalBootModeNormal) {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_NONE); LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_CLEAN);
} else if (mode == ApiHalBootModeDFU) { } else if (mode == ApiHalBootModeDFU) {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_DFU); LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_DFU);
} }

View File

@ -1,6 +1,8 @@
#include <api-hal.h> #include <api-hal.h>
void api_hal_init() { void api_hal_init() {
api_hal_boot_init();
FURI_LOG_I("FURI_HAL", "BOOT OK");
api_hal_version_init(); api_hal_version_init();
FURI_LOG_I("FURI_HAL", "VERSION OK"); FURI_LOG_I("FURI_HAL", "VERSION OK");
api_hal_delay_init(); api_hal_delay_init();

View File

@ -1,12 +1,18 @@
#include <api-hal-boot.h> #include <api-hal-boot.h>
#include <stm32wbxx_ll_rtc.h> #include <stm32wbxx_ll_rtc.h>
#define BOOT_REQUEST_NONE 0x00000000 // Boot request enum
#define BOOT_REQUEST_TAINTED 0x00000000
#define BOOT_REQUEST_CLEAN 0xDADEDADE
#define BOOT_REQUEST_DFU 0xDF00B000 #define BOOT_REQUEST_DFU 0xDF00B000
void api_hal_boot_init() {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_TAINTED);
}
void api_hal_boot_set_mode(ApiHalBootMode mode) { void api_hal_boot_set_mode(ApiHalBootMode mode) {
if (mode == ApiHalBootModeNormal) { if (mode == ApiHalBootModeNormal) {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_NONE); LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_CLEAN);
} else if (mode == ApiHalBootModeDFU) { } else if (mode == ApiHalBootModeDFU) {
LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_DFU); LL_RTC_BAK_SetRegister(RTC, LL_RTC_BKP_DR0, BOOT_REQUEST_DFU);
} }

View File

@ -1,6 +1,8 @@
#include <api-hal.h> #include <api-hal.h>
void api_hal_init() { void api_hal_init() {
api_hal_boot_init();
FURI_LOG_I("FURI_HAL", "BOOT OK");
api_hal_version_init(); api_hal_version_init();
FURI_LOG_I("FURI_HAL", "VERSION OK"); FURI_LOG_I("FURI_HAL", "VERSION OK");
api_hal_delay_init(); api_hal_delay_init();