diff --git a/embed/boardloader/main.c b/embed/boardloader/main.c index 053ba9cb8..cf4f24055 100644 --- a/embed/boardloader/main.c +++ b/embed/boardloader/main.c @@ -147,21 +147,13 @@ int main(void) clear_otg_hs_memory(); periph_init(); - if (0 != display_init()) { - __fatal_error("display_init", __FILE__, __LINE__, __FUNCTION__); - } - - if (0 != flash_init()) { - __fatal_error("flash_init", __FILE__, __LINE__, __FUNCTION__); - } - - if (0 != sdcard_init()) { - __fatal_error("sdcard_init", __FILE__, __LINE__, __FUNCTION__); - } + trassert(0 == display_init(), NULL); + trassert(0 == flash_init(), NULL); + trassert(0 == sdcard_init(), NULL); if (check_sdcard()) { if (!copy_sdcard()) { - __fatal_error("HALT", __FILE__, __LINE__, __FUNCTION__); + trassert(true == copy_sdcard(), NULL); } else { for (;;); } @@ -169,7 +161,7 @@ int main(void) check_and_jump(); - __fatal_error("HALT", __FILE__, __LINE__, __FUNCTION__); + trassert(0, "halt"); return 0; } diff --git a/embed/bootloader/main.c b/embed/bootloader/main.c index db4e99fc9..4869640b3 100644 --- a/embed/bootloader/main.c +++ b/embed/bootloader/main.c @@ -1,6 +1,5 @@ #include #include -#include #include "common.h" #include "display.h" @@ -129,28 +128,17 @@ int usb_init_all(void) { .report_desc = hid_report_desc, }; - if (0 != usb_init(&dev_info)) { - __fatal_error("usb_init", __FILE__, __LINE__, __FUNCTION__); - } - if (0 != usb_hid_add(&hid_info)) { - __fatal_error("usb_hid_add", __FILE__, __LINE__, __FUNCTION__); - } - if (0 != usb_start()) { - __fatal_error("usb_start", __FILE__, __LINE__, __FUNCTION__); - } + trassert(0 == usb_init(&dev_info), NULL); + trassert(0 == usb_hid_add(&hid_info), NULL); + trassert(0 == usb_start(), NULL); return 0; } void mainloop(void) { - if (0 != flash_init()) { - __fatal_error("flash_init", __FILE__, __LINE__, __FUNCTION__); - } - - if (0 != usb_init_all()) { - __fatal_error("usb_init_all", __FILE__, __LINE__, __FUNCTION__); - } + trassert(0 == flash_init(), NULL); + trassert(0 == usb_init_all(), NULL); display_clear(); @@ -161,7 +149,7 @@ void mainloop(void) if (r != USB_PACKET_SIZE) { continue; } - assert(r == USB_PACKET_SIZE); + trassert(r == USB_PACKET_SIZE, NULL); uint16_t msg_id; uint32_t msg_size; if (!msg_parse_header(buf, &msg_id, &msg_size)) { @@ -198,9 +186,7 @@ int main(void) display_orientation(0); display_backlight(255); - if (0 != touch_init()) { - __fatal_error("touch_init", __FILE__, __LINE__, __FUNCTION__); - } + trassert(0 == touch_init(), NULL); uint32_t touched = 0; for (int i = 0; i < 10; i++) { @@ -213,7 +199,7 @@ int main(void) check_and_jump(); } - __fatal_error("HALT", __FILE__, __LINE__, __FUNCTION__); + trassert(0, "halt"); return 0; } diff --git a/embed/extmod/modtrezorutils/modtrezorutils.c b/embed/extmod/modtrezorutils/modtrezorutils.c index 6ff7036c6..2e809c844 100644 --- a/embed/extmod/modtrezorutils/modtrezorutils.c +++ b/embed/extmod/modtrezorutils/modtrezorutils.c @@ -86,9 +86,9 @@ STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_trezorutils_memcpy_obj, 5, 5, mod STATIC mp_obj_t mod_trezorutils_halt(size_t n_args, const mp_obj_t *args) { mp_buffer_info_t msg; if (n_args > 0 && mp_get_buffer(args[0], &msg, MP_BUFFER_READ)) { - __fatal_error(msg.buf, __FILE__, __LINE__, __FUNCTION__); + trassert(0, msg.buf); } else { - __fatal_error("HALT", __FILE__, __LINE__, __FUNCTION__); + trassert(0, "halt"); } return mp_const_none; } diff --git a/embed/firmware/main.c b/embed/firmware/main.c index 93cf59be7..b62d45fea 100644 --- a/embed/firmware/main.c +++ b/embed/firmware/main.c @@ -33,17 +33,9 @@ int main(void) display_orientation(0); display_backlight(255); - if (0 != flash_init()) { - __fatal_error("flash_init", __FILE__, __LINE__, __FUNCTION__); - } - - if (0 != sdcard_init()) { - __fatal_error("sdcard_init", __FILE__, __LINE__, __FUNCTION__); - } - - if (0 != touch_init()) { - __fatal_error("touch_init", __FILE__, __LINE__, __FUNCTION__); - } + trassert(0 == flash_init(), NULL); + trassert(0 == sdcard_init(), NULL); + trassert(0 == touch_init(), NULL); for (;;) { printf("CORE: Starting main loop\n"); @@ -83,7 +75,7 @@ int main(void) // MicroPython default exception handler void __attribute__((noreturn)) nlr_jump_fail(void *val) { - __fatal_error("uncaught exception", NULL, 0, NULL); + trassert(0, "uncaught exception"); } void PendSV_Handler(void) { diff --git a/embed/firmware/mphalport.c b/embed/firmware/mphalport.c index 93553d643..979843835 100644 --- a/embed/firmware/mphalport.c +++ b/embed/firmware/mphalport.c @@ -5,23 +5,21 @@ static int vcp_iface_num = -1; int mp_hal_stdin_rx_chr(void) { - #define VCP_READ_TIMEOUT 25 - if (vcp_iface_num != -1) { - uint8_t c = 0; - while (usb_vcp_read_blocking(vcp_iface_num, &c, 1, VCP_READ_TIMEOUT) < 1) { - // Wait until we read a byte - } - return c; - } else { - __fatal_error("vcp stdio is not configured", __FILE__, __LINE__, __FUNCTION__); + trassert(vcp_iface_num >= 0, "vcp stdio is not configured"); + +#define VCP_READ_TIMEOUT 25 + uint8_t c = 0; + while (usb_vcp_read_blocking(vcp_iface_num, &c, 1, VCP_READ_TIMEOUT) < 1) { + // wait until we read a byte } + return c; } void mp_hal_stdout_tx_strn(const char *str, size_t len) { #define VCP_WRITE_TIMEOUT 0 - if (vcp_iface_num != -1) { + if (vcp_iface_num >= 0) { usb_vcp_write_blocking(vcp_iface_num, (const uint8_t *)str, len, VCP_WRITE_TIMEOUT); } else { // no-op diff --git a/embed/trezorhal/common.c b/embed/trezorhal/common.c index 6d3ff827f..bb64f05a2 100644 --- a/embed/trezorhal/common.c +++ b/embed/trezorhal/common.c @@ -4,17 +4,23 @@ #include "display.h" #include "rng.h" -void __attribute__((noreturn)) __fatal_error(const char *msg, const char *file, int line, const char *func) { +void __attribute__((noreturn)) __fatal_error(const char *expr, const char *msg, const char *file, int line, const char *func) { for (volatile uint32_t delay = 0; delay < 10000000; delay++) {} display_orientation(0); display_backlight(255); display_print_color(COLOR_WHITE, COLOR_RED128); - display_printf("\nFATAL ERROR:\n%s\n", msg); + display_printf("\nFATAL ERROR:\n"); + if (expr) { + display_printf("expr: %s\n", expr); + } + if (msg) { + display_printf("msg : %s\n", msg); + } if (file) { - display_printf("File: %s:%d\n", file, line); + display_printf("file: %s:%d\n", file, line); } if (func) { - display_printf("Func: %s\n", func); + display_printf("func: %s\n", func); } for (;;); } @@ -23,13 +29,12 @@ uint32_t __stack_chk_guard; void __attribute__((noreturn)) __stack_chk_fail(void) { - __fatal_error("Stack smashing detected.", NULL, 0, NULL); + trassert(0, "Stack smashing detected"); } #ifndef NDEBUG void __assert_func(const char *file, int line, const char *func, const char *expr) { - display_printf("\nassert(%s)\n", expr); - __fatal_error("Assertion failed", file, line, func); + __fatal_error(expr, "assert failed", file, line, func); } #endif diff --git a/embed/trezorhal/common.h b/embed/trezorhal/common.h index 42457a744..bab22eabe 100644 --- a/embed/trezorhal/common.h +++ b/embed/trezorhal/common.h @@ -12,7 +12,9 @@ extern void memset_reg(volatile void *start, volatile void *stop, uint32_t val); void periph_init(void); -void __attribute__((noreturn)) __fatal_error(const char *msg, const char *file, int line, const char *func); +void __attribute__((noreturn)) __fatal_error(const char *expr, const char *msg, const char *file, int line, const char *func); + +#define trassert(expr, msg) ((expr) ? (void)0 : __fatal_error(#expr, msg, __FILE__, __LINE__, __func__)) void jump_to(uint32_t address); diff --git a/embed/unix/common.c b/embed/unix/common.c index c5c5a94c7..7b1e98131 100644 --- a/embed/unix/common.c +++ b/embed/unix/common.c @@ -3,13 +3,20 @@ #include "common.h" -void __attribute__((noreturn)) __fatal_error(const char *msg, const char *file, int line, const char *func) { - printf("\nFATAL ERROR:\n%s\n", msg); +void __attribute__((noreturn)) __fatal_error(const char *expr, const char *msg, const char *file, int line, const char *func) +{ + printf("\nFATAL ERROR:\n"); + if (expr) { + printf("expr: %s\n", expr); + } + if (msg) { + printf("msg : %s\n", msg); + } if (file) { - printf("File: %s:%d\n", file, line); + printf("file: %s:%d\n", file, line); } if (func) { - printf("Func: %s\n", func); + printf("func: %s\n", func); } exit(1); } diff --git a/embed/unix/common.h b/embed/unix/common.h index 7b2586ec3..43c219d84 100644 --- a/embed/unix/common.h +++ b/embed/unix/common.h @@ -1,6 +1,8 @@ #ifndef __TREZORUNIX_COMMON_H__ #define __TREZORUNIX_COMMON_H__ -void __attribute__((noreturn)) __fatal_error(const char *msg, const char *file, int line, const char *func); +void __attribute__((noreturn)) __fatal_error(const char *expr, const char *msg, const char *file, int line, const char *func); + +#define trassert(expr, msg) ((expr) ? (void)0 : __fatal_error(#expr, msg, __FILE__, __LINE__, __func__)) #endif