diff --git a/Makefile.standard_app b/Makefile.standard_app index 9ca4daf55..91afa720f 100644 --- a/Makefile.standard_app +++ b/Makefile.standard_app @@ -56,18 +56,14 @@ ifneq ($(DEBUG), 0) else DEFINES += PRINTF=mcu_usb_printf endif + ifneq ($(DISABLE_LEDGER_ASSERT), 1) + DEFINES += HAVE_LEDGER_ASSERT_DISPLAY + DEFINES += LEDGER_ASSERT_CONFIG_FILE_INFO + endif else DEFINES += PRINTF\(...\)= endif -##################################################################### -# LEDGER_ASSERT # -##################################################################### -ifneq ($(DISABLE_LEDGER_ASSERT), 1) - DEFINES += HAVE_LEDGER_ASSERT_DISPLAY - DEFINES += LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO -endif - ##################################################################### # IO SEPROXY BUFFER SIZE # ##################################################################### diff --git a/lib_standard_app/debug.h b/include/ledger_assert.h similarity index 55% rename from lib_standard_app/debug.h rename to include/ledger_assert.h index 83cdf17c9..502f693d6 100644 --- a/lib_standard_app/debug.h +++ b/include/ledger_assert.h @@ -1,13 +1,11 @@ #pragma once -#include "macros.h" -#include "os.h" -#ifdef HAVE_DEBUG_THROWS -WEAK void app_throw_info(unsigned int exception, unsigned int lr_val); -WEAK void __attribute__((noreturn)) debug_display_exit(int exception); -#endif +#include "appflags.h" +#include "bolos_target.h" +#include "decorators.h" +#include "errors.h" -void ledger_assert(void); +void __attribute__((noreturn)) assert_display_exit(void); #define ASSERT_ERROR_CODE 0xBAAD @@ -20,105 +18,86 @@ void ledger_assert(void); #define LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO 1 #endif -#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO -#endif - -#ifdef HAVE_LEDGER_ASSERT_DISPLAY -#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO +#if defined(LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO) && defined(HAVE_LEDGER_ASSERT_DISPLAY) #define LR_AND_PC_OFFSET 0 #define LR_AND_PC_SIZE 30 -WEAK void assert_display_lr_and_pc(int lr, int pc); +void assert_display_lr_and_pc(int lr, int pc); #define ASSERT_DISPLAY_LR_AND_PC(lr, pc) assert_display_lr_and_pc(lr, pc) #else #define LR_AND_PC_SIZE 0 +#define ASSERT_DISPLAY_LR_AND_PC(lr, pc) \ + do { \ + } while (0) #endif -#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO +#if defined(LEDGER_ASSERT_CONFIG_MESSAGE_INFO) && defined(HAVE_LEDGER_ASSERT_DISPLAY) #define MESSAGE_OFFSET LR_AND_PC_SIZE #define MESSAGE_SIZE 20 -WEAK void assert_display_message(const char *message); +void assert_display_message(const char *message); #define ASSERT_DISPLAY_MESSAGE(message) assert_display_message(message) #else #define MESSAGE_SIZE 0 +#define ASSERT_DISPLAY_MESSAGE(message) \ + do { \ + } while (0) #endif -#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO +#if defined(LEDGER_ASSERT_CONFIG_FILE_INFO) && defined(HAVE_LEDGER_ASSERT_DISPLAY) #define FILE_OFFSET MESSAGE_OFFSET + MESSAGE_SIZE #define FILE_SIZE 50 -WEAK void assert_display_file_info(const char *file, unsigned int line); +void assert_display_file_info(const char *file, unsigned int line); #define ASSERT_DISPLAY_FILE_INFO(file, line) assert_display_file_info(file, line) #else #define FILE_SIZE 0 -#endif - -#define ASSERT_BUFFER_LEN LR_AND_PC_SIZE + MESSAGE_SIZE + FILE_SIZE -#else -#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO -#define ASSERT_DISPLAY_LR_AND_PC(lr, pc) \ - do { \ - } while (0) -#endif - -#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO -#define ASSERT_DISPLAY_MESSAGE(message) \ - do { \ - } while (0) -#endif - -#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO #define ASSERT_DISPLAY_FILE_INFO(file, line) \ do { \ } while (0) #endif -#endif - -#ifdef HAVE_PRINTF -#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO -void print_lr_and_pc(int lr, int pc); -#define ASSERT_PRINT_LR_AND_PC(lr, pc) print_lr_and_pc(lr, pc) -#endif -#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO -void print_message(const char *message); -#define ASSERT_PRINT_MESSAGE(message) print_message(message) +#ifdef HAVE_LEDGER_ASSERT_DISPLAY +#define ASSERT_BUFFER_LEN LR_AND_PC_SIZE + MESSAGE_SIZE + FILE_SIZE +#define LEDGER_ASSERT_EXIT() assert_display_exit() +#else +#define LEDGER_ASSERT_EXIT() exit_app(true) #endif -#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO -void print_file_info(const char *file, int line); -#define ASSERT_PRINT_FILE_INFO(file, line) print_file_info(file, line) -#endif +#if defined(LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO) && defined(HAVE_PRINTF) +void assert_print_lr_and_pc(int lr, int pc); +#define ASSERT_PRINT_LR_AND_PC(lr, pc) assert_print_lr_and_pc(lr, pc) #else -#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO #define ASSERT_PRINT_LR_AND_PC(lr, pc) \ do { \ } while (0) #endif -#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO +#if defined(LEDGER_ASSERT_CONFIG_MESSAGE_INFO) && defined(HAVE_PRINTF) +void assert_print_message(const char *message); +#define ASSERT_PRINT_MESSAGE(message) assert_print_message(message) +#else #define ASSERT_PRINT_MESSAGE(message) \ do { \ } while (0) #endif -#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO +#if defined(LEDGER_ASSERT_CONFIG_FILE_INFO) && defined(HAVE_PRINTF) +void assert_print_file_info(const char *file, int line); +#define ASSERT_PRINT_FILE_INFO(file, line) assert_print_file_info(file, line) +#else #define ASSERT_PRINT_FILE_INFO(file, line) \ do { \ } while (0) #endif -#endif #ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO -#define LEDGER_ASSERT_LR_AND_PC() \ - do { \ - int _lr_address = 0; \ - int _pc_address = 0; \ - \ - __asm volatile("mov %0, lr" : "=r"(_lr_address)); \ - __asm volatile("mov %0, pc" : "=r"(_pc_address)); \ - _lr_address = compute_address_location(_lr_address); \ - _pc_address = compute_address_location(_pc_address); \ - ASSERT_PRINT_LR_AND_PC(_lr_address, _pc_address); \ - ASSERT_DISPLAY_LR_AND_PC(_lr_address, _pc_address); \ +#define LEDGER_ASSERT_LR_AND_PC() \ + do { \ + int _lr_address = 0; \ + int _pc_address = 0; \ + \ + __asm volatile("mov %0, lr" : "=r"(_lr_address)); \ + __asm volatile("mov %0, pc" : "=r"(_pc_address)); \ + ASSERT_PRINT_LR_AND_PC(_lr_address, _pc_address); \ + ASSERT_DISPLAY_LR_AND_PC(_lr_address, _pc_address); \ } while (0) #endif @@ -145,7 +124,7 @@ void print_file_info(const char *file, int line); LEDGER_ASSERT_LR_AND_PC(); \ LEDGER_ASSERT_MESSAGE(message); \ LEDGER_ASSERT_FILE_INFO(); \ - ledger_assert(); \ + LEDGER_ASSERT_EXIT(); \ } \ } while (0) #elif defined(LEDGER_ASSERT_CONFIG_MESSAGE_INFO) @@ -154,7 +133,7 @@ void print_file_info(const char *file, int line); if (!(test)) { \ LEDGER_ASSERT_LR_AND_PC(); \ LEDGER_ASSERT_MESSAGE(message); \ - ledger_assert(); \ + LEDGER_ASSERT_EXIT(); \ } \ } while (0) #elif defined(LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO) @@ -162,7 +141,19 @@ void print_file_info(const char *file, int line); do { \ if (!(test)) { \ LEDGER_ASSERT_LR_AND_PC(); \ - ledger_assert(); \ + LEDGER_ASSERT_EXIT(); \ } \ } while (0) #endif + +#if defined(HAVE_DEBUG_THROWS) +void throw_display_lr(int e, int lr); +void throw_print_lr(int lr); +#define DEBUG_THROW(e) \ + do { \ + unsigned int lr_val; \ + __asm volatile("mov %0, lr" : "=r"(lr_val)); \ + throw_display_lr(e, lr_val); \ + throw_print_lr(lr_val); \ + } while (0) +#endif diff --git a/include/os.h b/include/os.h index 7b5b3b53a..27633c383 100644 --- a/include/os.h +++ b/include/os.h @@ -132,10 +132,8 @@ int snprintf(char *str, size_t str_size, const char *format, ...); #endif // HAVE_SPRINTF #ifndef HAVE_BOLOS -#if defined(HAVE_PRINTF) || defined(HAVE_DEBUG_THROWS) int compute_address_location(int address); #endif -#endif // syscall test // SYSCALL void dummy_1(unsigned int* p PLENGTH(2+len+15+ len + 16 + sizeof(io_send_t) + 1 ), diff --git a/lib_standard_app/main.c b/lib_standard_app/main.c index 6a18693cb..6e499e36d 100644 --- a/lib_standard_app/main.c +++ b/lib_standard_app/main.c @@ -19,7 +19,7 @@ #include "os.h" #include "io.h" -#include "debug.h" +#include "ledger_assert.h" #ifdef HAVE_SWAP #include "swap.h" @@ -90,7 +90,7 @@ static void standalone_app_main(void) BLE_power(0, NULL); #endif // Display crash info on screen for debug purpose - debug_display_exit(e); + assert_display_exit(); #endif } FINALLY {} diff --git a/lib_standard_app/debug.c b/src/ledger_assert.c similarity index 59% rename from lib_standard_app/debug.c rename to src/ledger_assert.c index 2a20d190c..f221ab46c 100644 --- a/lib_standard_app/debug.c +++ b/src/ledger_assert.c @@ -1,5 +1,5 @@ -/***************************************************************************** - * (c) 2020 Ledger SAS. +/******************************************************************************* + * (c) 2023 Ledger * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -12,64 +12,84 @@ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. - *****************************************************************************/ - -#include // uint*_t -#include // memset, explicit_bzero + ********************************************************************************/ +#include "exceptions.h" #include "os.h" +#include +#include "ledger_assert.h" #include "io.h" -#include "debug.h" #ifdef HAVE_NBGL #include "nbgl_use_case.h" #endif -#if defined(HAVE_LEDGER_ASSERT_DISPLAY) || defined(HAVE_DEBUG_THROWS) - #ifndef ASSERT_BUFFER_LEN #define ASSERT_BUFFER_LEN 24 #endif -char assert_buffer[ASSERT_BUFFER_LEN]; -#ifdef HAVE_DEBUG_THROWS -WEAK void app_throw_info(unsigned int exception, unsigned int lr_val) -{ - snprintf(assert_buffer, sizeof(assert_buffer), "e=0x%04X\n LR=0x%08X", exception, lr_val); -} -#endif +#if defined(HAVE_LEDGER_ASSERT_DISPLAY) || defined(HAVE_DEBUG_THROWS) +static char assert_buffer[ASSERT_BUFFER_LEN]; #endif -#ifdef HAVE_LEDGER_ASSERT_DISPLAY -#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO -WEAK void assert_display_lr_and_pc(int lr, int pc) +void assert_display_lr_and_pc(int lr, int pc) { char buff[LR_AND_PC_SIZE]; + + lr = compute_address_location(lr); + pc = compute_address_location(pc); snprintf(buff, LR_AND_PC_SIZE, "LR=0x%08X\n PC=0x%08X\n", lr, pc); strncat(assert_buffer, buff, LR_AND_PC_SIZE); } -#endif -#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO -WEAK void assert_display_message(const char *message) +void assert_display_message(const char *message) { char buff[MESSAGE_SIZE]; + snprintf(buff, MESSAGE_SIZE, "%s\n", message); strncat(assert_buffer, buff, MESSAGE_SIZE); } -#endif -#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO -WEAK void assert_display_file_info(const char *file, unsigned int line) +void assert_display_file_info(const char *file, unsigned int line) { char buff[FILE_SIZE]; + snprintf(buff, FILE_SIZE, "%s::%d\n", file, line); strncat(assert_buffer, buff, FILE_SIZE); } -#endif -#endif -#if defined(HAVE_LEDGER_ASSERT_DISPLAY) || defined(HAVE_DEBUG_THROWS) +void assert_print_file_info(const char *file, int line) +{ + PRINTF("%s::%d \n", file, line); +} + +void assert_print_message(const char *message) +{ + if (message) { + PRINTF("%s\n", message); + } +} + +void assert_print_lr_and_pc(int lr, int pc) +{ + lr = compute_address_location(lr); + pc = compute_address_location(pc); + PRINTF("=> LR: 0x%08X \n", lr); + PRINTF("=> PC: 0x%08X \n", pc); +} + +void throw_display_lr(int e, int lr) +{ + lr = compute_address_location(lr); + snprintf(assert_buffer, ASSERT_BUFFER_LEN, "e=0x%04X\n LR=0x%08X\n", e, lr); +} + +void throw_print_lr(int lr) +{ + lr = compute_address_location(lr); + PRINTF("=> LR: 0x%08X \n", lr); +} + static void exit_app(bool confirm) { UNUSED(confirm); @@ -87,10 +107,8 @@ UX_STEP_CB(ux_error, UX_FLOW(ux_error_flow, &ux_error); #endif -WEAK void __attribute__((noreturn)) debug_display_exit(int exception) +void __attribute__((noreturn)) assert_display_exit(void) { - UNUSED(exception); - #ifdef HAVE_BAGL ux_flow_init(0, ux_error_flow, NULL); #endif @@ -105,41 +123,3 @@ WEAK void __attribute__((noreturn)) debug_display_exit(int exception) io_seproxyhal_io_heartbeat(); } } -#endif - -#ifdef HAVE_PRINTF - -#ifdef LEDGER_ASSERT_CONFIG_FILE_INFO -void print_file_info(const char *file, int line) -{ - PRINTF("%s::%d \n", file, line); -} -#endif - -#ifdef LEDGER_ASSERT_CONFIG_MESSAGE_INFO -void print_message(const char *message) -{ - if (message) { - PRINTF("%s\n", message); - } -} -#endif - -#ifdef LEDGER_ASSERT_CONFIG_LR_AND_PC_INFO -void print_lr_and_pc(int lr, int pc) -{ - PRINTF("=> LR: 0x%04X \n", lr); - PRINTF("=> PC: 0x%04X \n", pc); -} -#endif - -#endif - -void ledger_assert(void) -{ -#ifdef HAVE_LEDGER_ASSERT_DISPLAY - debug_display_exit(ASSERT_ERROR_CODE); -#else - exit_app(true); -#endif -} diff --git a/src/os.c b/src/os.c index a853cff49..014bec9f4 100644 --- a/src/os.c +++ b/src/os.c @@ -85,34 +85,21 @@ char os_secure_memcmp(const void *src1, const void *src2, size_t length) } #ifndef HAVE_BOLOS -#define MAIN_LINKER_SCRIPT_LOCATION 0xC0DE0000 -#include "lib_standard_app/debug.h" +#include "ledger_assert.h" int main(void); -#if defined(HAVE_PRINTF) || defined(HAVE_DEBUG_THROWS) +#define MAIN_LINKER_SCRIPT_LOCATION 0xC0DE0000 int compute_address_location(int address) { // Compute location before relocation (sort of anti PIC) return address - (unsigned int) main + MAIN_LINKER_SCRIPT_LOCATION; } -#endif void os_longjmp(unsigned int exception) { -#if defined(HAVE_PRINTF) || defined(HAVE_DEBUG_THROWS) - unsigned int lr_val; - __asm volatile("mov %0, lr" : "=r"(lr_val)); - - lr_val = compute_address_location(lr_val); -#endif - -#ifdef HAVE_PRINTF - PRINTF("exception[%d]: LR=0x%08X\n", exception, lr_val); -#endif // HAVE_PRINTF - #ifdef HAVE_DEBUG_THROWS // Send to the app the info of exception and LR for debug purpose - app_throw_info(exception, lr_val); + DEBUG_THROW(exception); #endif longjmp(try_context_get()->jmp_buf, exception);