Skip to content

Commit c018ffc

Browse files
committed
bootloader-fw-link: Use RAM to communicate
Instead of writing to non-volatile storage between mcu resets, write to a well-known location. RAM is random after hardware reset, but does not get cleared on MCU reset.
1 parent 42bda93 commit c018ffc

File tree

6 files changed

+21
-31
lines changed

6 files changed

+21
-31
lines changed

bootloader.ld

+1
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ SECTIONS
116116
{
117117
. = ALIGN(4);
118118
_srtt = .;
119+
*(.auto_enter);
119120
*(.segger_rtt);
120121
*(.segger_rtt_buf);
121122
_ertt = .;

firmware.ld

+1
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ SECTIONS
115115
{
116116
. = ALIGN(4);
117117
_srtt = .;
118+
*(.auto_enter);
118119
*(.segger_rtt);
119120
*(.segger_rtt_buf);
120121
_ertt = .;

src/bootloader/bootloader.c

+5-7
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,9 @@
3939

4040
#include <assert.h>
4141

42+
// Section is fixed in ram, so can be used to communicate between fw/bl
43+
volatile secbool_u32 auto_enter __attribute__((section(".auto_enter")));
44+
4245
#define BOOT_OP_LEN 2u // 1 byte op code and 1 byte parameter
4346
#define BOOTLOADER_CMD (HID_VENDOR_FIRST + 0x03) // Hardware wallet command
4447

@@ -758,12 +761,7 @@ static size_t _api_versions(uint8_t* output)
758761

759762
static void _api_reboot(void)
760763
{
761-
chunk_shared_t shared_data;
762-
memory_read_shared_bootdata(&shared_data);
763-
if (shared_data.fields.auto_enter == sectrue_u8) {
764-
shared_data.fields.auto_enter = secfalse_u8;
765-
_write_chunk(FLASH_SHARED_DATA_START, shared_data.bytes);
766-
}
764+
auto_enter = secfalse_u32;
767765
_reset_mcu();
768766
}
769767

@@ -977,7 +975,7 @@ void bootloader_jump(void)
977975

978976
UG_FontSelect(&font_font_a_9X9);
979977

980-
if (shared_data.fields.auto_enter != sectrue_u8) {
978+
if (auto_enter != sectrue_u32) {
981979
#ifdef BOOTLOADER_DEVDEVICE
982980
if (!_devdevice_enter(_firmware_verified_jump(&bootdata, secfalse_u32))) {
983981
_binary_exec();

src/factorysetup.c

+3-13
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "platform_init.h"
2121
#include "screen.h"
2222
#include "securechip/securechip.h"
23+
#include "system.h"
2324
#include "usb/usb.h"
2425
#include "usb/usb_packet.h"
2526
#include "usb/usb_processing.h"
@@ -312,19 +313,8 @@ int main(void)
312313
screen_splash();
313314
common_main();
314315

315-
{
316-
// Set to re-enter bootloader again, otherwise we are stuck with this
317-
// firmware forever.
318-
auto_enter_t auto_enter = {
319-
.value = sectrue_u8,
320-
};
321-
upside_down_t upside_down = {
322-
.value = false,
323-
};
324-
if (!memory_bootloader_set_flags(auto_enter, upside_down)) {
325-
// Not much we can do here.
326-
}
327-
}
316+
// After reset we prefer to stay in bootloader
317+
auto_enter = sectrue_u32;
328318

329319
SEGGER_RTT_Init();
330320

src/system.c

+4-10
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,16 @@
1515
#include "system.h"
1616
#include <memory/memory.h>
1717
#include <screen.h>
18+
#include <stdint.h>
1819
#ifndef TESTING
1920
#include <driver_init.h>
2021
#endif
2122

23+
volatile secbool_u32 auto_enter __attribute__((section(".auto_enter")));
24+
2225
void reboot(void)
2326
{
24-
auto_enter_t auto_enter = {
25-
.value = sectrue_u8,
26-
};
27-
upside_down_t upside_down = {
28-
.value = screen_is_upside_down(),
29-
};
30-
if (!memory_bootloader_set_flags(auto_enter, upside_down)) {
31-
// If this failed, we might not be able to reboot into the bootloader.
32-
// We will try anyway, no point in aborting here.
33-
}
27+
auto_enter = sectrue_u32;
3428
#ifndef TESTING
3529
_reset_mcu();
3630
#endif

src/system.h

+7-1
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,14 @@
1515
#ifndef _SYSTEM_H_
1616
#define _SYSTEM_H_
1717

18+
#include "util.h"
19+
20+
// Set this to `sectrue_u32` to stay in bootloader, or anything else to jump to firmware
21+
22+
extern volatile secbool_u32 auto_enter;
23+
1824
/**
19-
* Reboots the device.
25+
* Reboots the device into bootloader
2026
*/
2127
void reboot(void);
2228

0 commit comments

Comments
 (0)