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"
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
121108int 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 }
0 commit comments