Skip to content

Commit 689564b

Browse files
committed
fatal error handling optimize
Signed-off-by: Adam BZH <adam@onekey.so>
1 parent 782a157 commit 689564b

File tree

3 files changed

+103
-357
lines changed

3 files changed

+103
-357
lines changed

core/embed/trezorhal/common.c

Lines changed: 96 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -81,54 +81,97 @@ void reboot_to_boot(void) {
8181
svc_reset_system();
8282
}
8383

84+
// print line hight (DISPLAY_CHAR_HIGHT + 2)
85+
// up and down padding 8
86+
// which is (800-(8*2))/(26+2)=28 lines max
87+
#define DISP_LINE_TO_Y(LINE) (8 + (DISPLAY_CHAR_HIGHT + 2) * (LINE))
88+
8489
void __attribute__((noreturn))
85-
__fatal_error(const char *expr, const char *msg, const char *file, int line,
86-
const char *func) {
87-
display_orientation(0);
88-
display_backlight(255);
89-
display_printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
90-
display_print_color(RGB16(0x69, 0x69, 0x69), COLOR_BLACK);
91-
display_printf("\nFATAL ERROR:\n");
90+
__fatal_error(const char* expr, const char* msg, const char* file, int line,
91+
const char* func) {
92+
static bool triggered = false;
93+
static int print_y;
94+
95+
if (!triggered) {
96+
display_orientation(0);
97+
display_backlight(255);
98+
display_print_clear();
99+
display_clear();
100+
display_print_color(RGB16(0x69, 0x69, 0x69), COLOR_BLACK);
101+
102+
display_image(9, 50, 46, 40, toi_icon_warning + 12,
103+
sizeof(toi_icon_warning) - 12);
104+
display_text(8, 140, "System error occurred", -1, FONT_NORMAL, COLOR_WHITE,
105+
COLOR_BLACK);
106+
107+
#ifdef SCM_REVISION
108+
const uint8_t* rev = (const uint8_t*)SCM_REVISION;
109+
display_text_printf(8, DISP_LINE_TO_Y(26), "rev: %02x%02x%02x%02x%02x",
110+
rev[0], rev[1], rev[2], rev[3], rev[4]);
111+
print_y += (DISPLAY_CHAR_HIGHT + 2);
112+
#endif
113+
#ifdef BUILD_ID
114+
const uint8_t* id = (const uint8_t*)BUILD_ID;
115+
display_text_printf(8, DISP_LINE_TO_Y(27), "build id: %s", id);
116+
print_y += (DISPLAY_CHAR_HIGHT + 2);
117+
#endif
118+
119+
print_y = DISP_LINE_TO_Y(20);
120+
display_text_printf(8, print_y, "FATAL ERROR:");
121+
print_y += (DISPLAY_CHAR_HIGHT + 2);
122+
} else {
123+
print_y = DISP_LINE_TO_Y(14);
124+
125+
display_text_printf(8, print_y, "FATAL ERROR (while handling):");
126+
print_y += (DISPLAY_CHAR_HIGHT + 2);
127+
}
128+
92129
if (expr) {
93-
display_printf("expr: %s\n", expr);
130+
display_text_printf(8, print_y, "expr: %s", expr);
131+
print_y += (DISPLAY_CHAR_HIGHT + 2);
94132
}
95133
if (msg) {
96-
display_printf("msg : %s\n", msg);
134+
display_text_printf(8, print_y, "msg: %s", msg);
135+
print_y += (DISPLAY_CHAR_HIGHT + 2);
97136
}
98137
if (file) {
99-
display_printf("file: %s:%d\n", file, line);
138+
display_text_printf(8, print_y, "file: %s:%d", file, line);
139+
print_y += (DISPLAY_CHAR_HIGHT + 2);
100140
}
101141
if (func) {
102-
display_printf("func: %s\n", func);
142+
display_text_printf(8, print_y, "func: %s", func);
143+
print_y += (DISPLAY_CHAR_HIGHT + 2);
103144
}
104-
#ifdef SCM_REVISION
105-
const uint8_t *rev = (const uint8_t *)SCM_REVISION;
106-
display_printf("rev : %02x%02x%02x%02x%02x\n", rev[0], rev[1], rev[2], rev[3],
107-
rev[4]);
108-
#endif
109-
#ifdef BUILD_ID
110-
const uint8_t *id = (const uint8_t *)BUILD_ID;
111-
display_printf("build id: %s\n", id);
112-
#endif
113-
display_printf("\n\n");
114-
display_image(9, 50, 46, 40, toi_icon_warning + 12,
115-
sizeof(toi_icon_warning) - 12);
116-
display_text(8, 140, "System problem detected.", -1, FONT_NORMAL, COLOR_WHITE,
117-
COLOR_BLACK);
118-
display_text(8, 784, "Tap to restart ...", -1, FONT_NORMAL, COLOR_WHITE,
119-
COLOR_BLACK);
120-
// shutdown();
121-
while (!touch_click()) {
145+
146+
display_text_printf(8, print_y, "---------------------------------------");
147+
print_y += (DISPLAY_CHAR_HIGHT + 2);
148+
149+
if (!triggered) {
150+
triggered = true;
151+
152+
if (touch_is_inited()) {
153+
display_text(8, DISP_LINE_TO_Y(28), "Tap to restart ...", -1, FONT_NORMAL,
154+
COLOR_WHITE, COLOR_BLACK);
155+
while (!touch_click()) {
156+
}
157+
restart();
158+
}
122159
}
123-
restart();
160+
161+
display_bar(0, DISP_LINE_TO_Y(28) - (DISPLAY_CHAR_HIGHT + 2), DISPLAY_RESX,
162+
DISPLAY_RESY, COLOR_BLACK);
163+
display_text(8, DISP_LINE_TO_Y(28), "Hold power button to shutdown ...", -1,
164+
FONT_NORMAL, COLOR_WHITE, COLOR_BLACK);
165+
124166
for (;;)
125167
;
126168
}
127169

128170
void __attribute__((noreturn))
129-
error_shutdown(const char *line1, const char *line2, const char *line3,
130-
const char *line4) {
171+
error_shutdown(const char* line1, const char* line2, const char* line3,
172+
const char* line4) {
131173
display_orientation(0);
174+
display_backlight(255);
132175
#ifdef TREZOR_FONT_NORMAL_ENABLE
133176
uint16_t font_color = RGB16(0x69, 0x69, 0x69);
134177
display_clear();
@@ -174,17 +217,24 @@ error_shutdown(const char *line1, const char *line2, const char *line3,
174217
}
175218
display_printf("\nPlease unplug the device.\n");
176219
#endif
177-
display_backlight(255);
178-
// shutdown();
179-
while (!touch_click()) {
220+
221+
if (touch_is_inited()) {
222+
display_text(8, 784, "Tap to restart ...", -1, FONT_NORMAL, COLOR_WHITE,
223+
COLOR_BLACK);
224+
while (!touch_click()) {
225+
}
226+
restart();
227+
} else {
228+
display_text(8, 784, "Hold power button to shutdown ...", -1, FONT_NORMAL,
229+
COLOR_WHITE, COLOR_BLACK);
180230
}
181-
restart();
231+
182232
for (;;)
183233
;
184234
}
185235

186-
void error_reset(const char *line1, const char *line2, const char *line3,
187-
const char *line4) {
236+
void error_reset(const char* line1, const char* line2, const char* line3,
237+
const char* line4) {
188238
display_orientation(0);
189239
display_printf("\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n\n");
190240
display_print_color(RGB16(0x69, 0x69, 0x69), COLOR_BLACK);
@@ -233,8 +283,8 @@ void error_pin_max_prompt(void) {
233283
}
234284

235285
#ifndef NDEBUG
236-
void __assert_func(const char *file, int line, const char *func,
237-
const char *expr) {
286+
void __assert_func(const char* file, int line, const char* func,
287+
const char* expr) {
238288
__fatal_error(expr, "assert failed", file, line, func);
239289
}
240290
#endif
@@ -252,8 +302,8 @@ void clear_otg_hs_memory(void) {
252302
// that the peripheral memory is
253303
// accessible
254304
memset_reg(
255-
(volatile void *)USB_OTG_HS_DATA_FIFO_RAM,
256-
(volatile void *)(USB_OTG_HS_DATA_FIFO_RAM + USB_OTG_HS_DATA_FIFO_SIZE),
305+
(volatile void*)USB_OTG_HS_DATA_FIFO_RAM,
306+
(volatile void*)(USB_OTG_HS_DATA_FIFO_RAM + USB_OTG_HS_DATA_FIFO_SIZE),
257307
0);
258308
__HAL_RCC_USB_OTG_HS_CLK_DISABLE(); // disable USB OTG_HS peripheral clock as
259309
// the peripheral is not needed right now
@@ -290,10 +340,10 @@ void collect_hw_entropy(void) {
290340
NULL);
291341
}
292342

293-
bool check_all_ones(const void *data, int len) {
343+
bool check_all_ones(const void* data, int len) {
294344
if (!data) return false;
295345
uint8_t result = 0xff;
296-
const uint8_t *ptr = (const uint8_t *)data;
346+
const uint8_t* ptr = (const uint8_t*)data;
297347

298348
for (; len; len--, ptr++) {
299349
result &= *ptr;
@@ -303,10 +353,10 @@ bool check_all_ones(const void *data, int len) {
303353
return (result == 0xff);
304354
}
305355

306-
bool check_all_zeros(const void *data, int len) {
356+
bool check_all_zeros(const void* data, int len) {
307357
if (!data) return false;
308358
uint8_t result = 0x0;
309-
const uint8_t *ptr = (const uint8_t *)data;
359+
const uint8_t* ptr = (const uint8_t*)data;
310360

311361
for (; len; len--, ptr++) {
312362
result |= *ptr;

0 commit comments

Comments
 (0)