From 8b0336862f11be587f8b482d2f9d99759b860e92 Mon Sep 17 00:00:00 2001 From: Pavol Rusnak Date: Fri, 28 Apr 2017 15:39:22 +0200 Subject: [PATCH] make __fatal_error more useful, add make sizecheck target --- .travis.yml | 11 ++++---- Makefile | 17 ++++++++----- micropython/boardloader/main.c | 12 ++++----- micropython/bootloader/main.c | 25 +++++++------------ .../extmod/modtrezorutils/modtrezorutils.c | 6 ++--- micropython/firmware/main.c | 17 ++++--------- micropython/trezorhal/common.c | 23 ++++++++++++++--- micropython/trezorhal/common.h | 4 +-- micropython/trezorhal/stm32_it.c | 11 ++++---- micropython/trezorhal/stm32_system.c | 10 ++++---- micropython/unix/common.c | 11 +++++--- micropython/unix/common.h | 2 +- 12 files changed, 79 insertions(+), 70 deletions(-) diff --git a/.travis.yml b/.travis.yml index 31df1f8f4..1a182dee2 100644 --- a/.travis.yml +++ b/.travis.yml @@ -28,11 +28,12 @@ install: - pip2 install ecdsa mnemonic protobuf requests script: - - test "$GOAL" != "stm32" || test "$CC" != "gcc" || make vendorheader - - test "$GOAL" != "stm32" || test "$CC" != "gcc" || make build_cross - - test "$GOAL" != "stm32" || test "$CC" != "gcc" || make build_boardloader - - test "$GOAL" != "stm32" || test "$CC" != "gcc" || make build_bootloader - - test "$GOAL" != "stm32" || test "$CC" != "gcc" || make build_firmware + - test "$GOAL" != "stm32" || make vendorheader + - test "$GOAL" != "stm32" || make build_cross + - test "$GOAL" != "stm32" || make build_boardloader + - test "$GOAL" != "stm32" || make build_bootloader + - test "$GOAL" != "stm32" || make build_firmware + - test "$GOAL" != "stm32" || make sizecheck - test "$GOAL" != "unix" || make build_unix TREZOR_NOUI=1 - test "$GOAL" != "unix" || make test diff --git a/Makefile b/Makefile index 819464fb7..8c4b43bda 100644 --- a/Makefile +++ b/Makefile @@ -56,11 +56,11 @@ build_boardloader: ## build boardloader build_bootloader: ## build bootloader $(MAKE) -f Makefile.bootloader $(TREZORHAL_PORT_OPTS) - ./tools/binctl micropython/bootloader/build/bootloader.bin -s 1 4141414141414141414141414141414141414141414141414141414141414141 + ./tools/binctl $(BOOTLOADER_BUILD_DIR)/bootloader.bin -s 1 4141414141414141414141414141414141414141414141414141414141414141 build_firmware: res build_cross ## build firmware with frozen modules $(MAKE) -f Makefile.firmware $(TREZORHAL_PORT_OPTS) - ./tools/binctl micropython/firmware/build/firmware.bin -s 1 4141414141414141414141414141414141414141414141414141414141414141 + ./tools/binctl $(FIRMWARE_BUILD_DIR)/firmware.bin -s 1 4141414141414141414141414141414141414141414141414141414141414141 build_unix: ## build unix port $(MAKE) -f ../../../micropython/unix/Makefile -C vendor/micropython/unix $(UNIX_PORT_OPTS) @@ -117,10 +117,15 @@ vendorheader: ## construct default vendor header ./tools/binctl micropython/firmware/vendorheader.bin -s 1 4141414141414141414141414141414141414141414141414141414141414141 binctl: ## print info about binary files - ./tools/binctl micropython/bootloader/build/bootloader.bin + ./tools/binctl $(BOOTLOADER_BUILD_DIR)/bootloader.bin ./tools/binctl micropython/firmware/vendorheader.bin - ./tools/binctl micropython/firmware/build/firmware.bin + ./tools/binctl $(FIRMWARE_BUILD_DIR)/firmware.bin bloaty: ## run bloaty size profiler - bloaty -d symbols -n 0 -s file micropython/firmware/build/firmware.elf | less - bloaty -d compileunits -n 0 -s file micropython/firmware/build/firmware.elf | less + bloaty -d symbols -n 0 -s file $(FIRMWARE_BUILD_DIR)/firmware.elf | less + bloaty -d compileunits -n 0 -s file $(FIRMWARE_BUILD_DIR)/firmware.elf | less + +sizecheck: ## check sizes of binary files + test 32768 -ge $(shell stat -c%s $(BOARDLOADER_BUILD_DIR)/boardloader.bin) + test 65536 -ge $(shell stat -c%s $(BOOTLOADER_BUILD_DIR)/bootloader.bin) + test 917504 -ge $(shell stat -c%s $(FIRMWARE_BUILD_DIR)/firmware.bin) diff --git a/micropython/boardloader/main.c b/micropython/boardloader/main.c index 13863a866..c0d290c82 100644 --- a/micropython/boardloader/main.c +++ b/micropython/boardloader/main.c @@ -13,7 +13,7 @@ #define IMAGE_MAXSIZE (1 * 64 * 1024 + 7 * 128 * 1024) void pendsv_isr_handler(void) { - __fatal_error("pendsv"); + __fatal_error("pendsv", __FILE__, __LINE__, __FUNCTION__); } bool check_sdcard(void) @@ -146,15 +146,15 @@ int main(void) periph_init(); if (0 != display_init()) { - __fatal_error("display_init failed"); + __fatal_error("display_init", __FILE__, __LINE__, __FUNCTION__); } if (0 != flash_init()) { - __fatal_error("flash_init failed"); + __fatal_error("flash_init", __FILE__, __LINE__, __FUNCTION__); } if (0 != sdcard_init()) { - __fatal_error("sdcard_init failed"); + __fatal_error("sdcard_init", __FILE__, __LINE__, __FUNCTION__); } display_clear(); @@ -166,13 +166,13 @@ int main(void) if (check_sdcard()) { if (!copy_sdcard()) { - __fatal_error("halt"); + __fatal_error("HALT", __FILE__, __LINE__, __FUNCTION__); } } check_and_jump(); - __fatal_error("halt"); + __fatal_error("HALT", __FILE__, __LINE__, __FUNCTION__); return 0; } diff --git a/micropython/bootloader/main.c b/micropython/bootloader/main.c index 0dc94ed79..4f4150589 100644 --- a/micropython/bootloader/main.c +++ b/micropython/bootloader/main.c @@ -19,7 +19,7 @@ #define IMAGE_MAXSIZE (7 * 128 * 1024) void pendsv_isr_handler(void) { - __fatal_error("pendsv"); + __fatal_error("pendsv", __FILE__, __LINE__, __FUNCTION__); } void display_vendor(const uint8_t *vimg, const char *vstr, uint32_t vstr_len, uint32_t fw_version) @@ -132,13 +132,13 @@ int usb_init_all(void) { }; if (0 != usb_init(&dev_info)) { - __fatal_error("usb_init failed"); + __fatal_error("usb_init", __FILE__, __LINE__, __FUNCTION__); } if (0 != usb_hid_add(&hid_info)) { - __fatal_error("usb_hid_add failed"); + __fatal_error("usb_hid_add", __FILE__, __LINE__, __FUNCTION__); } if (0 != usb_start()) { - __fatal_error("usb_start failed"); + __fatal_error("usb_start", __FILE__, __LINE__, __FUNCTION__); } return 0; @@ -182,11 +182,11 @@ uint32_t process_upload_message(uint32_t msg_size, const uint8_t *initbuf, uint3 void mainloop(void) { if (0 != flash_init()) { - __fatal_error("flash_init failed"); + __fatal_error("flash_init", __FILE__, __LINE__, __FUNCTION__); } if (0 != usb_init_all()) { - __fatal_error("usb_init_all failed"); + __fatal_error("usb_init_all", __FILE__, __LINE__, __FUNCTION__); } uint8_t buf[USB_PACKET_SIZE]; @@ -238,11 +238,11 @@ int main(void) periph_init(); if (0 != display_init()) { - __fatal_error("display_init failed"); + __fatal_error("display_init", __FILE__, __LINE__, __FUNCTION__); } if (0 != touch_init()) { - __fatal_error("touch_init failed"); + __fatal_error("touch_init", __FILE__, __LINE__, __FUNCTION__); } display_clear(); @@ -258,14 +258,7 @@ int main(void) check_and_jump(); } - __fatal_error("halt"); + __fatal_error("HALT", __FILE__, __LINE__, __FUNCTION__); return 0; } - -#ifndef NDEBUG -void __assert_func(const char *file, int line, const char *func, const char *expr) { - // printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); - __fatal_error("Assertion failed"); -} -#endif diff --git a/micropython/extmod/modtrezorutils/modtrezorutils.c b/micropython/extmod/modtrezorutils/modtrezorutils.c index b27f52a04..c70df6051 100644 --- a/micropython/extmod/modtrezorutils/modtrezorutils.c +++ b/micropython/extmod/modtrezorutils/modtrezorutils.c @@ -60,12 +60,10 @@ 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)) { - printf("HALT! %s\n", (const char *)msg.buf); + __fatal_error(msg.buf, __FILE__, __LINE__, __FUNCTION__); } else { - printf("HALT!\n"); + __fatal_error("HALT", __FILE__, __LINE__, __FUNCTION__); } - // TODO: is this the best we can do? - __fatal_error("HALT"); return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mod_TrezorUtils_halt_obj, 0, 1, mod_TrezorUtils_halt); diff --git a/micropython/firmware/main.c b/micropython/firmware/main.c index 8b1e3efef..3590e77d7 100644 --- a/micropython/firmware/main.c +++ b/micropython/firmware/main.c @@ -28,23 +28,23 @@ int main(void) { pendsv_init(); if (0 != display_init()) { - __fatal_error("display_init failed"); + __fatal_error("display_init", __FILE__, __LINE__, __FUNCTION__); } if (0 != flash_init()) { - __fatal_error("flash_init failed"); + __fatal_error("flash_init", __FILE__, __LINE__, __FUNCTION__); } if (0 != rng_init()) { - __fatal_error("rng_init failed"); + __fatal_error("rng_init", __FILE__, __LINE__, __FUNCTION__); } if (0 != sdcard_init()) { - __fatal_error("sdcard_init failed"); + __fatal_error("sdcard_init", __FILE__, __LINE__, __FUNCTION__); } if (0 != touch_init()) { - __fatal_error("touch_init failed"); + __fatal_error("touch_init", __FILE__, __LINE__, __FUNCTION__); } for (;;) { @@ -72,13 +72,6 @@ int main(void) { return 0; } -#ifndef NDEBUG -void __assert_func(const char *file, int line, const char *func, const char *expr) { - printf("Assertion '%s' failed, at file %s:%d\n", expr, file, line); - __fatal_error("Assertion failed"); -} -#endif - // Micropython file I/O stubs mp_lexer_t *mp_lexer_new_from_file(const char *filename) { diff --git a/micropython/trezorhal/common.c b/micropython/trezorhal/common.c index 15056857b..cbabcc90f 100644 --- a/micropython/trezorhal/common.c +++ b/micropython/trezorhal/common.c @@ -6,12 +6,18 @@ #define FATAL_FGCOLOR 0xFFFF #define FATAL_BGCOLOR 0x7800 -void __attribute__((noreturn)) __fatal_error(const char *msg) { +void __attribute__((noreturn)) __fatal_error(const char *msg, const char *file, int line, const char *func) { for (volatile uint32_t delay = 0; delay < 10000000; delay++) { } - display_print("FATAL ERROR:\n", -1); + display_print("\nFATAL ERROR:\n", -1); display_print(msg, -1); - display_print("\n", -1); + if (file) { + display_print("\nFile: ", -1); display_print(file, -1); (void)line; + } + if (func) { + display_print("\nFunc: ", -1); display_print(func, -1); + } + display_print("\n", 1); display_print_out(FATAL_FGCOLOR, FATAL_BGCOLOR); for (;;) { display_backlight(255); @@ -21,8 +27,17 @@ void __attribute__((noreturn)) __fatal_error(const char *msg) { } } +#ifndef NDEBUG +void __assert_func(const char *file, int line, const char *func, const char *expr) { + display_print("\nassert(", -1); + display_print(expr, -1); + display_print(")\n", 2); + __fatal_error("Assertion failed", file, line, func); +} +#endif + void __attribute__((noreturn)) nlr_jump_fail(void *val) { - __fatal_error("uncaught exception"); + __fatal_error("uncaught exception", __FILE__, __LINE__, __FUNCTION__); } extern void SystemClock_Config(void); diff --git a/micropython/trezorhal/common.h b/micropython/trezorhal/common.h index 5fedace72..c052ec61d 100644 --- a/micropython/trezorhal/common.h +++ b/micropython/trezorhal/common.h @@ -10,9 +10,9 @@ void periph_init(void); -void __attribute__((noreturn)) nlr_jump_fail(void *val); +void __attribute__((noreturn)) __fatal_error(const char *msg, const char *file, int line, const char *func); -void __attribute__((noreturn)) __fatal_error(const char *msg); +void __attribute__((noreturn)) nlr_jump_fail(void *val); void jump_to(uint32_t address); diff --git a/micropython/trezorhal/stm32_it.c b/micropython/trezorhal/stm32_it.c index 8727ebbb3..4984bc88d 100644 --- a/micropython/trezorhal/stm32_it.c +++ b/micropython/trezorhal/stm32_it.c @@ -71,12 +71,11 @@ #include "gccollect.h" #include "display.h" +#include "common.h" #define IRQ_ENTER(irq) #define IRQ_EXIT(irq) -extern void __fatal_error(const char*); - /******************************************************************************/ /* Cortex-M4 Processor Exceptions Handlers */ /******************************************************************************/ @@ -174,7 +173,7 @@ void HardFault_C_Handler(ExceptionRegisters_t *regs) { /* Go to infinite loop when Hard Fault exception occurs */ while (1) { - __fatal_error("HardFault"); + __fatal_error("HardFault", __FILE__, __LINE__, __FUNCTION__); } } @@ -216,7 +215,7 @@ void NMI_Handler(void) { void MemManage_Handler(void) { /* Go to infinite loop when Memory Manage exception occurs */ while (1) { - __fatal_error("MemManage"); + __fatal_error("MemManage", __FILE__, __LINE__, __FUNCTION__); } } @@ -228,7 +227,7 @@ void MemManage_Handler(void) { void BusFault_Handler(void) { /* Go to infinite loop when Bus Fault exception occurs */ while (1) { - __fatal_error("BusFault"); + __fatal_error("BusFault", __FILE__, __LINE__, __FUNCTION__); } } @@ -240,7 +239,7 @@ void BusFault_Handler(void) { void UsageFault_Handler(void) { /* Go to infinite loop when Usage Fault exception occurs */ while (1) { - __fatal_error("UsageFault"); + __fatal_error("UsageFault", __FILE__, __LINE__, __FUNCTION__); } } diff --git a/micropython/trezorhal/stm32_system.c b/micropython/trezorhal/stm32_system.c index ce21091ac..3f1aa59ee 100644 --- a/micropython/trezorhal/stm32_system.c +++ b/micropython/trezorhal/stm32_system.c @@ -95,7 +95,7 @@ #define MICROPY_HW_CLK_PLLQ (7) #define MICROPY_HW_CLK_LAST_FREQ (1) -void __fatal_error(const char *msg); +#include "common.h" /** * @} @@ -324,7 +324,7 @@ void SystemClock_Config(void) RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_LSE; RCC_OscInitStruct.LSEState = RCC_LSE_ON; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - __fatal_error("HAL_RCC_OscConfig"); + __fatal_error("HAL_RCC_OscConfig", __FILE__, __LINE__, __FUNCTION__); } #endif @@ -409,14 +409,14 @@ void SystemClock_Config(void) #endif if(HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { - __fatal_error("HAL_RCC_OscConfig"); + __fatal_error("HAL_RCC_OscConfig", __FILE__, __LINE__, __FUNCTION__); } #if defined(MCU_SERIES_F7) /* Activate the OverDrive to reach the 200 MHz Frequency */ if (HAL_PWREx_EnableOverDrive() != HAL_OK) { - __fatal_error("HAL_PWREx_EnableOverDrive"); + __fatal_error("HAL_PWREx_EnableOverDrive", __FILE__, __LINE__, __FUNCTION__); } #endif @@ -426,7 +426,7 @@ void SystemClock_Config(void) if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, MICROPY_HW_FLASH_LATENCY) != HAL_OK) { - __fatal_error("HAL_RCC_ClockConfig"); + __fatal_error("HAL_RCC_ClockConfig", __FILE__, __LINE__, __FUNCTION__); } #if defined(MCU_SERIES_F7) diff --git a/micropython/unix/common.c b/micropython/unix/common.c index 418a77150..c5c5a94c7 100644 --- a/micropython/unix/common.c +++ b/micropython/unix/common.c @@ -3,8 +3,13 @@ #include "common.h" -void __attribute__((noreturn)) __fatal_error(const char *msg) { - printf("FATAL ERROR:\n"); - printf("%s\n", msg); +void __attribute__((noreturn)) __fatal_error(const char *msg, const char *file, int line, const char *func) { + printf("\nFATAL ERROR:\n%s\n", msg); + if (file) { + printf("File: %s:%d\n", file, line); + } + if (func) { + printf("Func: %s\n", func); + } exit(1); } diff --git a/micropython/unix/common.h b/micropython/unix/common.h index 24cc6b527..7b2586ec3 100644 --- a/micropython/unix/common.h +++ b/micropython/unix/common.h @@ -1,6 +1,6 @@ #ifndef __TREZORUNIX_COMMON_H__ #define __TREZORUNIX_COMMON_H__ -void __attribute__((noreturn)) __fatal_error(const char *msg); +void __attribute__((noreturn)) __fatal_error(const char *msg, const char *file, int line, const char *func); #endif