Skip to content

Commit 07231d9

Browse files
committed
introduce new memory layout
firmware header is now stored with code, not within the storage sectors
1 parent fe39d10 commit 07231d9

24 files changed

+774
-483
lines changed

Makefile.include

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,8 +134,10 @@ endif
134134

135135
ifeq ($(MEMORY_PROTECT), 0)
136136
CFLAGS += -DMEMORY_PROTECT=0
137+
$(info MEMORY_PROTECT=0)
137138
else
138139
CFLAGS += -DMEMORY_PROTECT=1
140+
$(info MEMORY_PROTECT=1)
139141
endif
140142

141143
ifeq ($(DEBUG_RNG), 1)
@@ -159,7 +161,7 @@ flash: $(NAME).bin
159161
$(OPENOCD) -c "init; reset halt; flash write_image erase $(NAME).bin 0x8000000; exit"
160162

161163
upload: sign
162-
trezorctl firmware_update -f $(NAME).bin
164+
trezorctl firmware_update -f $(NAME).bin -s
163165

164166
sign: $(NAME).bin
165167
$(PYTHON) ../bootloader/firmware_sign.py -f $(NAME).bin

bootloader/bootloader.c

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <libopencm3/cm3/scb.h>
2525

2626
#include "bootloader.h"
27+
#include "signatures.h"
2728
#include "buttons.h"
2829
#include "setup.h"
2930
#include "usb.h"
@@ -33,8 +34,9 @@
3334
#include "layout.h"
3435
#include "rng.h"
3536
#include "timer.h"
37+
#include "memory.h"
3638

37-
void layoutFirmwareHash(const uint8_t *hash)
39+
void layoutFirmwareFingerprint(const uint8_t *hash)
3840
{
3941
char str[4][17];
4042
for (int i = 0; i < 4; i++) {
@@ -43,68 +45,53 @@ void layoutFirmwareHash(const uint8_t *hash)
4345
layoutDialog(&bmp_icon_question, "Abort", "Continue", "Compare fingerprints", str[0], str[1], str[2], str[3], NULL, NULL);
4446
}
4547

46-
void show_halt(void)
48+
bool get_button_response(void)
49+
{
50+
do {
51+
delay(100000);
52+
buttonUpdate();
53+
} while (!button.YesUp && !button.NoUp);
54+
return button.YesUp;
55+
}
56+
57+
static void show_halt(void)
4758
{
48-
layoutDialog(&bmp_icon_error, NULL, NULL, NULL, "Unofficial firmware", "aborted.", NULL, "Unplug your TREZOR", "contact our support.", NULL);
59+
layoutDialog(&bmp_icon_error, NULL, NULL, NULL, "Unofficial firmware", "aborted.", NULL, "Unplug your TREZOR,", "reinstall firmware.", NULL);
4960
shutdown();
5061
}
5162

52-
void show_unofficial_warning(const uint8_t *hash)
63+
static void show_unofficial_warning(const uint8_t *hash)
5364
{
5465
layoutDialog(&bmp_icon_warning, "Abort", "I'll take the risk", NULL, "WARNING!", NULL, "Unofficial firmware", "detected.", NULL, NULL);
5566

56-
do {
57-
delay(100000);
58-
buttonUpdate();
59-
} while (!button.YesUp && !button.NoUp);
60-
61-
if (button.NoUp) {
62-
show_halt(); // no button was pressed -> halt
67+
bool but = get_button_response();
68+
if (!but) { // no button was pressed -> halt
69+
show_halt();
6370
}
6471

65-
layoutFirmwareHash(hash);
72+
layoutFirmwareFingerprint(hash);
6673

67-
do {
68-
delay(100000);
69-
buttonUpdate();
70-
} while (!button.YesUp && !button.NoUp);
71-
72-
if (button.NoUp) {
73-
show_halt(); // no button was pressed -> halt
74+
but = get_button_response();
75+
if (!but) { // no button was pressed -> halt
76+
show_halt();
7477
}
7578

7679
// everything is OK, user pressed 2x Continue -> continue program
7780
}
7881

79-
void __attribute__((noreturn)) load_app(int signed_firmware)
82+
static void __attribute__((noreturn)) load_app(int signed_firmware)
8083
{
8184
// zero out SRAM
8285
memset_reg(_ram_start, _ram_end, 0);
8386

8487
jump_to_firmware((const vector_table_t *) FLASH_PTR(FLASH_APP_START), signed_firmware);
8588
}
8689

87-
bool firmware_present(void)
88-
{
89-
#ifndef APPVER
90-
if (memcmp(FLASH_PTR(FLASH_META_MAGIC), "TRZR", 4)) { // magic does not match
91-
return false;
92-
}
93-
if (*((const uint32_t *)FLASH_PTR(FLASH_META_CODELEN)) < 4096) { // firmware reports smaller size than 4kB
94-
return false;
95-
}
96-
if (*((const uint32_t *)FLASH_PTR(FLASH_META_CODELEN)) > FLASH_TOTAL_SIZE - (FLASH_APP_START - FLASH_ORIGIN)) { // firmware reports bigger size than flash size
97-
return false;
98-
}
99-
#endif
100-
return true;
101-
}
102-
103-
void bootloader_loop(void)
90+
static void bootloader_loop(void)
10491
{
10592
oledClear();
10693
oledDrawBitmap(0, 0, &bmp_logo64);
107-
if (firmware_present()) {
94+
if (firmware_present_new()) {
10895
oledDrawStringCenter(90, 10, "TREZOR", FONT_STANDARD);
10996
oledDrawStringCenter(90, 30, "Bootloader", FONT_STANDARD);
11097
oledDrawStringCenter(90, 50, VERSTR(VERSION_MAJOR) "." VERSTR(VERSION_MINOR) "." VERSTR(VERSION_PATCH), FONT_STANDARD);
@@ -115,7 +102,7 @@ void bootloader_loop(void)
115102
}
116103
oledRefresh();
117104

118-
usbLoop(firmware_present());
105+
usbLoop();
119106
}
120107

121108
int main(void)
@@ -136,19 +123,26 @@ int main(void)
136123
uint16_t state = gpio_port_read(BTN_PORT);
137124
int unpressed = ((state & BTN_PIN_YES) == BTN_PIN_YES || (state & BTN_PIN_NO) == BTN_PIN_NO);
138125

139-
if (firmware_present() && unpressed) {
126+
if (firmware_present_new() && unpressed) {
140127

141128
oledClear();
142129
oledDrawBitmap(40, 0, &bmp_logo64_empty);
143130
oledRefresh();
144131

145-
uint8_t hash[32];
146-
int signed_firmware = signatures_ok(hash);
132+
const image_header *hdr = (const image_header *)FLASH_PTR(FLASH_FWHEADER_START);
133+
134+
uint8_t fingerprint[32];
135+
int signed_firmware = signatures_new_ok(hdr, fingerprint);
147136
if (SIG_OK != signed_firmware) {
148-
show_unofficial_warning(hash);
137+
show_unofficial_warning(fingerprint);
149138
timer_init();
150139
}
151140

141+
if (SIG_OK != check_firmware_hashes(hdr)) {
142+
layoutDialog(&bmp_icon_error, NULL, NULL, NULL, "Broken firmware", "detected.", NULL, "Unplug your TREZOR,", "reinstall firmware.", NULL);
143+
shutdown();
144+
}
145+
152146
mpu_config_off();
153147
load_app(signed_firmware);
154148
}

bootloader/bootloader.h

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,20 @@
2121
#define __BOOTLOADER_H__
2222

2323
#define VERSION_MAJOR 1
24-
#define VERSION_MINOR 6
25-
#define VERSION_PATCH 1
24+
#define VERSION_MINOR 8
25+
#define VERSION_PATCH 0
2626

2727
#define STR(X) #X
2828
#define VERSTR(X) STR(X)
2929

3030
#define VERSION_MAJOR_CHAR "\x01"
31-
#define VERSION_MINOR_CHAR "\x06"
32-
#define VERSION_PATCH_CHAR "\x01"
31+
#define VERSION_MINOR_CHAR "\x08"
32+
#define VERSION_PATCH_CHAR "\x00"
3333

34+
#include <stdint.h>
3435
#include <stdbool.h>
35-
#include "memory.h"
3636

37-
void layoutFirmwareHash(const uint8_t *hash);
38-
bool firmware_present(void);
37+
void layoutFirmwareFingerprint(const uint8_t *hash);
38+
bool get_button_response(void);
3939

4040
#endif

bootloader/firmware_align.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env python3
22
import sys
33
import os
44

0 commit comments

Comments
 (0)