1
0
mirror of https://github.com/trezor/trezor-firmware.git synced 2024-12-16 03:18:09 +00:00

embed: introduce trassert (trezor assert)

This commit is contained in:
Pavol Rusnak 2017-10-12 00:32:46 +02:00
parent d003c250d8
commit 380d08f1d0
No known key found for this signature in database
GPG Key ID: 91F3B339B9A02A3D
9 changed files with 56 additions and 72 deletions

View File

@ -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;
}

View File

@ -1,6 +1,5 @@
#include <string.h>
#include <sys/types.h>
#include <assert.h>
#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;
}

View File

@ -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;
}

View File

@ -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) {

View File

@ -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) {
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
// wait until we read a byte
}
return c;
} else {
__fatal_error("vcp stdio is not configured", __FILE__, __LINE__, __FUNCTION__);
}
}
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

View File

@ -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

View File

@ -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);

View File

@ -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);
}

View File

@ -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