|
19 | 19 |
|
20 | 20 | #include <string.h>
|
21 | 21 |
|
22 |
| -#include <libopencm3/stm32/rcc.h> |
23 |
| -#include <libopencm3/stm32/gpio.h> |
24 | 22 | #include <libopencm3/cm3/scb.h>
|
| 23 | +#include <libopencm3/stm32/gpio.h> |
| 24 | +#include <libopencm3/stm32/rcc.h> |
25 | 25 |
|
26 | 26 | #include "bootloader.h"
|
27 |
| -#include "signatures.h" |
28 | 27 | #include "buttons.h"
|
| 28 | +#include "layout.h" |
| 29 | +#include "memory.h" |
| 30 | +#include "oled.h" |
| 31 | +#include "rng.h" |
29 | 32 | #include "setup.h"
|
| 33 | +#include "signatures.h" |
30 | 34 | #include "usb.h"
|
31 |
| -#include "oled.h" |
32 | 35 | #include "util.h"
|
33 |
| -#include "signatures.h" |
34 |
| -#include "layout.h" |
35 |
| -#include "rng.h" |
36 |
| -#include "memory.h" |
37 | 36 |
|
38 |
| -void layoutFirmwareFingerprint(const uint8_t *hash) |
39 |
| -{ |
40 |
| - char str[4][17]; |
41 |
| - for (int i = 0; i < 4; i++) { |
42 |
| - data2hex(hash + i * 8, 8, str[i]); |
43 |
| - } |
44 |
| - layoutDialog(&bmp_icon_question, "Abort", "Continue", "Compare fingerprints", str[0], str[1], str[2], str[3], NULL, NULL); |
| 37 | +void layoutFirmwareFingerprint(const uint8_t *hash) { |
| 38 | + char str[4][17]; |
| 39 | + for (int i = 0; i < 4; i++) { |
| 40 | + data2hex(hash + i * 8, 8, str[i]); |
| 41 | + } |
| 42 | + layoutDialog(&bmp_icon_question, "Abort", "Continue", "Compare fingerprints", |
| 43 | + str[0], str[1], str[2], str[3], NULL, NULL); |
45 | 44 | }
|
46 | 45 |
|
47 |
| -bool get_button_response(void) |
48 |
| -{ |
49 |
| - do { |
50 |
| - delay(100000); |
51 |
| - buttonUpdate(); |
52 |
| - } while (!button.YesUp && !button.NoUp); |
53 |
| - return button.YesUp; |
| 46 | +bool get_button_response(void) { |
| 47 | + do { |
| 48 | + delay(100000); |
| 49 | + buttonUpdate(); |
| 50 | + } while (!button.YesUp && !button.NoUp); |
| 51 | + return button.YesUp; |
54 | 52 | }
|
55 | 53 |
|
56 |
| -void show_halt(const char *line1, const char *line2) |
57 |
| -{ |
58 |
| - layoutDialog(&bmp_icon_error, NULL, NULL, NULL, line1, line2, NULL, "Unplug your TREZOR,", "reinstall firmware.", NULL); |
59 |
| - shutdown(); |
| 54 | +void show_halt(const char *line1, const char *line2) { |
| 55 | + layoutDialog(&bmp_icon_error, NULL, NULL, NULL, line1, line2, NULL, |
| 56 | + "Unplug your TREZOR,", "reinstall firmware.", NULL); |
| 57 | + shutdown(); |
60 | 58 | }
|
61 | 59 |
|
62 |
| -static void show_unofficial_warning(const uint8_t *hash) |
63 |
| -{ |
64 |
| - layoutDialog(&bmp_icon_warning, "Abort", "I'll take the risk", NULL, "WARNING!", NULL, "Unofficial firmware", "detected.", NULL, NULL); |
| 60 | +static void show_unofficial_warning(const uint8_t *hash) { |
| 61 | + layoutDialog(&bmp_icon_warning, "Abort", "I'll take the risk", NULL, |
| 62 | + "WARNING!", NULL, "Unofficial firmware", "detected.", NULL, |
| 63 | + NULL); |
65 | 64 |
|
66 |
| - bool but = get_button_response(); |
67 |
| - if (!but) { // no button was pressed -> halt |
68 |
| - show_halt("Unofficial firmware", "aborted."); |
69 |
| - } |
| 65 | + bool but = get_button_response(); |
| 66 | + if (!but) { // no button was pressed -> halt |
| 67 | + show_halt("Unofficial firmware", "aborted."); |
| 68 | + } |
70 | 69 |
|
71 |
| - layoutFirmwareFingerprint(hash); |
| 70 | + layoutFirmwareFingerprint(hash); |
72 | 71 |
|
73 |
| - but = get_button_response(); |
74 |
| - if (!but) { // no button was pressed -> halt |
75 |
| - show_halt("Unofficial firmware", "aborted."); |
76 |
| - } |
| 72 | + but = get_button_response(); |
| 73 | + if (!but) { // no button was pressed -> halt |
| 74 | + show_halt("Unofficial firmware", "aborted."); |
| 75 | + } |
77 | 76 |
|
78 |
| - // everything is OK, user pressed 2x Continue -> continue program |
| 77 | + // everything is OK, user pressed 2x Continue -> continue program |
79 | 78 | }
|
80 | 79 |
|
81 |
| -static void __attribute__((noreturn)) load_app(int signed_firmware) |
82 |
| -{ |
83 |
| - // zero out SRAM |
84 |
| - memset_reg(_ram_start, _ram_end, 0); |
| 80 | +static void __attribute__((noreturn)) load_app(int signed_firmware) { |
| 81 | + // zero out SRAM |
| 82 | + memset_reg(_ram_start, _ram_end, 0); |
85 | 83 |
|
86 |
| - jump_to_firmware((const vector_table_t *) FLASH_PTR(FLASH_APP_START), signed_firmware); |
| 84 | + jump_to_firmware((const vector_table_t *)FLASH_PTR(FLASH_APP_START), |
| 85 | + signed_firmware); |
87 | 86 | }
|
88 | 87 |
|
89 |
| -static void bootloader_loop(void) |
90 |
| -{ |
91 |
| - oledClear(); |
92 |
| - oledDrawBitmap(0, 0, &bmp_logo64); |
93 |
| - if (firmware_present_new()) { |
94 |
| - oledDrawStringCenter(90, 10, "TREZOR", FONT_STANDARD); |
95 |
| - oledDrawStringCenter(90, 30, "Bootloader", FONT_STANDARD); |
96 |
| - oledDrawStringCenter(90, 50, VERSTR(VERSION_MAJOR) "." VERSTR(VERSION_MINOR) "." VERSTR(VERSION_PATCH), FONT_STANDARD); |
97 |
| - } else { |
98 |
| - oledDrawStringCenter(90, 10, "Welcome!", FONT_STANDARD); |
99 |
| - oledDrawStringCenter(90, 30, "Please visit", FONT_STANDARD); |
100 |
| - oledDrawStringCenter(90, 50, "trezor.io/start", FONT_STANDARD); |
101 |
| - } |
102 |
| - oledRefresh(); |
103 |
| - |
104 |
| - usbLoop(); |
| 88 | +static void bootloader_loop(void) { |
| 89 | + oledClear(); |
| 90 | + oledDrawBitmap(0, 0, &bmp_logo64); |
| 91 | + if (firmware_present_new()) { |
| 92 | + oledDrawStringCenter(90, 10, "TREZOR", FONT_STANDARD); |
| 93 | + oledDrawStringCenter(90, 30, "Bootloader", FONT_STANDARD); |
| 94 | + oledDrawStringCenter(90, 50, |
| 95 | + VERSTR(VERSION_MAJOR) "." VERSTR( |
| 96 | + VERSION_MINOR) "." VERSTR(VERSION_PATCH), |
| 97 | + FONT_STANDARD); |
| 98 | + } else { |
| 99 | + oledDrawStringCenter(90, 10, "Welcome!", FONT_STANDARD); |
| 100 | + oledDrawStringCenter(90, 30, "Please visit", FONT_STANDARD); |
| 101 | + oledDrawStringCenter(90, 50, "trezor.io/start", FONT_STANDARD); |
| 102 | + } |
| 103 | + oledRefresh(); |
| 104 | + |
| 105 | + usbLoop(); |
105 | 106 | }
|
106 | 107 |
|
107 |
| -int main(void) |
108 |
| -{ |
| 108 | +int main(void) { |
109 | 109 | #ifndef APPVER
|
110 |
| - setup(); |
| 110 | + setup(); |
111 | 111 | #endif
|
112 |
| - __stack_chk_guard = random32(); // this supports compiler provided unpredictable stack protection checks |
| 112 | + __stack_chk_guard = random32(); // this supports compiler provided |
| 113 | + // unpredictable stack protection checks |
113 | 114 | #ifndef APPVER
|
114 |
| - memory_protect(); |
115 |
| - oledInit(); |
| 115 | + memory_protect(); |
| 116 | + oledInit(); |
116 | 117 | #endif
|
117 | 118 |
|
118 |
| - mpu_config_bootloader(); |
| 119 | + mpu_config_bootloader(); |
119 | 120 |
|
120 | 121 | #ifndef APPVER
|
121 |
| - bool left_pressed = (buttonRead() & BTN_PIN_NO) == 0; |
122 |
| - |
123 |
| - if (firmware_present_new() && !left_pressed) { |
124 |
| - |
125 |
| - oledClear(); |
126 |
| - oledDrawBitmap(40, 0, &bmp_logo64_empty); |
127 |
| - oledRefresh(); |
128 |
| - |
129 |
| - const image_header *hdr = (const image_header *)FLASH_PTR(FLASH_FWHEADER_START); |
130 |
| - |
131 |
| - uint8_t fingerprint[32]; |
132 |
| - int signed_firmware = signatures_new_ok(hdr, fingerprint); |
133 |
| - if (SIG_OK != signed_firmware) { |
134 |
| - show_unofficial_warning(fingerprint); |
135 |
| - } |
136 |
| - |
137 |
| - if (SIG_OK != check_firmware_hashes(hdr)) { |
138 |
| - layoutDialog(&bmp_icon_error, NULL, NULL, NULL, "Broken firmware", "detected.", NULL, "Unplug your TREZOR,", "reinstall firmware.", NULL); |
139 |
| - shutdown(); |
140 |
| - } |
141 |
| - |
142 |
| - mpu_config_off(); |
143 |
| - load_app(signed_firmware); |
144 |
| - } |
| 122 | + bool left_pressed = (buttonRead() & BTN_PIN_NO) == 0; |
| 123 | + |
| 124 | + if (firmware_present_new() && !left_pressed) { |
| 125 | + oledClear(); |
| 126 | + oledDrawBitmap(40, 0, &bmp_logo64_empty); |
| 127 | + oledRefresh(); |
| 128 | + |
| 129 | + const image_header *hdr = |
| 130 | + (const image_header *)FLASH_PTR(FLASH_FWHEADER_START); |
| 131 | + |
| 132 | + uint8_t fingerprint[32]; |
| 133 | + int signed_firmware = signatures_new_ok(hdr, fingerprint); |
| 134 | + if (SIG_OK != signed_firmware) { |
| 135 | + show_unofficial_warning(fingerprint); |
| 136 | + } |
| 137 | + |
| 138 | + if (SIG_OK != check_firmware_hashes(hdr)) { |
| 139 | + layoutDialog(&bmp_icon_error, NULL, NULL, NULL, "Broken firmware", |
| 140 | + "detected.", NULL, "Unplug your TREZOR,", |
| 141 | + "reinstall firmware.", NULL); |
| 142 | + shutdown(); |
| 143 | + } |
| 144 | + |
| 145 | + mpu_config_off(); |
| 146 | + load_app(signed_firmware); |
| 147 | + } |
145 | 148 | #endif
|
146 | 149 |
|
147 |
| - bootloader_loop(); |
| 150 | + bootloader_loop(); |
148 | 151 |
|
149 |
| - return 0; |
| 152 | + return 0; |
150 | 153 | }
|
0 commit comments