mirror of
https://github.com/trezor/trezor-firmware.git
synced 2024-12-19 12:58:13 +00:00
refactor(core): optimize assert/fatal_error for reduced flash footprint
[no changelog]
This commit is contained in:
parent
06faae8f82
commit
0f1cac9695
55
core/embed/lib/assert.h
Normal file
55
core/embed/lib/assert.h
Normal file
@ -0,0 +1,55 @@
|
|||||||
|
/*
|
||||||
|
* This file is part of the Trezor project, https://trezor.io/
|
||||||
|
*
|
||||||
|
* Copyright (c) SatoshiLabs
|
||||||
|
*
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
*
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
*
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef LIB_ASSERT_H
|
||||||
|
#define LIB_ASSERT_H
|
||||||
|
|
||||||
|
// This file overrides the standard `assert` macro to
|
||||||
|
// save space in flash memory.
|
||||||
|
//
|
||||||
|
// This file will be included instead of the standard assert.h
|
||||||
|
// as it is passed to the compiler with -include before the
|
||||||
|
// paths to the standard libraries.
|
||||||
|
//
|
||||||
|
// Our custom assert macro eliminates printing of the
|
||||||
|
// expression and prints only a short file name and line number.
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifndef NDEBUG
|
||||||
|
|
||||||
|
void __attribute__((noreturn))
|
||||||
|
__fatal_error(const char *msg, const char *file, int line);
|
||||||
|
|
||||||
|
#define assert(expr) \
|
||||||
|
((expr) ? (void)0 : __fatal_error("Assert", __FILE_NAME__, __LINE__))
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
#define assert(expr) ((void)0)
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
}
|
||||||
|
#endif
|
||||||
|
|
||||||
|
#endif // LIB_ASSERT_H
|
@ -64,28 +64,24 @@ void __attribute__((noreturn)) error_shutdown(const char *message) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void __attribute__((noreturn))
|
void __attribute__((noreturn))
|
||||||
__fatal_error(const char *expr, const char *msg, const char *file, int line,
|
__fatal_error(const char *msg, const char *file, int line) {
|
||||||
const char *func) {
|
|
||||||
#ifdef FANCY_FATAL_ERROR
|
#ifdef FANCY_FATAL_ERROR
|
||||||
char buf[256] = {0};
|
if (msg == NULL) {
|
||||||
mini_snprintf(buf, sizeof(buf), "%s: %d", file, line);
|
char buf[128] = {0};
|
||||||
error_shutdown(msg != NULL ? msg : buf);
|
mini_snprintf(buf, sizeof(buf), "%s:%d", file, line);
|
||||||
|
msg = buf;
|
||||||
|
}
|
||||||
|
error_shutdown(msg);
|
||||||
#else
|
#else
|
||||||
display_orientation(0);
|
display_orientation(0);
|
||||||
term_set_color(COLOR_WHITE, COLOR_FATAL_ERROR);
|
term_set_color(COLOR_WHITE, COLOR_FATAL_ERROR);
|
||||||
term_printf("\nINTERNAL ERROR:\n");
|
term_printf("\nINTERNAL ERROR:\n");
|
||||||
if (expr) {
|
|
||||||
term_printf("expr: %s\n", expr);
|
|
||||||
}
|
|
||||||
if (msg) {
|
if (msg) {
|
||||||
term_printf("msg : %s\n", msg);
|
term_printf("msg : %s\n", msg);
|
||||||
}
|
}
|
||||||
if (file) {
|
if (file) {
|
||||||
term_printf("file: %s:%d\n", file, line);
|
term_printf("file: %s:%d\n", file, line);
|
||||||
}
|
}
|
||||||
if (func) {
|
|
||||||
term_printf("func: %s\n", func);
|
|
||||||
}
|
|
||||||
#ifdef SCM_REVISION
|
#ifdef SCM_REVISION
|
||||||
const uint8_t *rev = (const uint8_t *)SCM_REVISION;
|
const uint8_t *rev = (const uint8_t *)SCM_REVISION;
|
||||||
term_printf("rev : %02x%02x%02x%02x%02x\n", rev[0], rev[1], rev[2], rev[3],
|
term_printf("rev : %02x%02x%02x%02x%02x\n", rev[0], rev[1], rev[2], rev[3],
|
||||||
@ -97,13 +93,6 @@ __fatal_error(const char *expr, const char *msg, const char *file, int line,
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
#ifndef NDEBUG
|
|
||||||
void __assert_func(const char *file, int line, const char *func,
|
|
||||||
const char *expr) {
|
|
||||||
__fatal_error(expr, "assert failed", file, line, func);
|
|
||||||
}
|
|
||||||
#endif
|
|
||||||
|
|
||||||
void __attribute__((noreturn)) show_wipe_code_screen(void) {
|
void __attribute__((noreturn)) show_wipe_code_screen(void) {
|
||||||
error_shutdown_ex("WIPE CODE ENTERED",
|
error_shutdown_ex("WIPE CODE ENTERED",
|
||||||
"All data has been erased from the device",
|
"All data has been erased from the device",
|
||||||
|
@ -35,15 +35,12 @@ void __attribute__((noreturn)) error_shutdown(const char *message);
|
|||||||
|
|
||||||
// Do not use this function directly, use the `ensure()` macro instead.
|
// Do not use this function directly, use the `ensure()` macro instead.
|
||||||
void __attribute__((noreturn))
|
void __attribute__((noreturn))
|
||||||
__fatal_error(const char *expr, const char *msg, const char *file, int line,
|
__fatal_error(const char *msg, const char *file, int line);
|
||||||
const char *func);
|
|
||||||
|
|
||||||
// Checks for an expression and if it is false, shows an error message
|
// Checks for an expression and if it is false, shows an error message
|
||||||
// and shuts down the device.
|
// and shuts down the device.
|
||||||
#define ensure(expr, msg) \
|
#define ensure(expr, msg) \
|
||||||
(((expr) == sectrue) \
|
(((expr) == sectrue) ? (void)0 : __fatal_error(msg, __FILE_NAME__, __LINE__))
|
||||||
? (void)0 \
|
|
||||||
: __fatal_error(#expr, msg, __FILE__, __LINE__, __func__))
|
|
||||||
|
|
||||||
// Shows WIPE CODE ENTERED screeen and shuts down the device.
|
// Shows WIPE CODE ENTERED screeen and shuts down the device.
|
||||||
void __attribute__((noreturn)) show_wipe_code_screen(void);
|
void __attribute__((noreturn)) show_wipe_code_screen(void);
|
||||||
|
@ -53,9 +53,9 @@ fn panic_debug(panic_info: &core::panic::PanicInfo) -> ! {
|
|||||||
// Filling at least the file and line information, if available.
|
// Filling at least the file and line information, if available.
|
||||||
// TODO: find out how to display message from panic_info.message()
|
// TODO: find out how to display message from panic_info.message()
|
||||||
if let Some(location) = panic_info.location() {
|
if let Some(location) = panic_info.location() {
|
||||||
trezorhal::fatal_error::__fatal_error("", "rs", location.file(), location.line(), "");
|
trezorhal::fatal_error::__fatal_error("rs", location.file(), location.line());
|
||||||
} else {
|
} else {
|
||||||
trezorhal::fatal_error::__fatal_error("", "rs", "", 0, "");
|
trezorhal::fatal_error::__fatal_error("rs", "", 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,7 +34,7 @@ pub fn error_shutdown(title: &str, msg: &str, footer: &str) -> ! {
|
|||||||
/// Shows an error message on the screen and shuts down the device.
|
/// Shows an error message on the screen and shuts down the device.
|
||||||
/// In debug mode, also prints the error message to the console.
|
/// In debug mode, also prints the error message to the console.
|
||||||
#[inline(never)] // saves few kilobytes of flash
|
#[inline(never)] // saves few kilobytes of flash
|
||||||
pub fn __fatal_error(_expr: &str, msg: &str, _file: &str, _line: u32, _func: &str) -> ! {
|
pub fn __fatal_error(msg: &str, _file: &str, _line: u32) -> ! {
|
||||||
#[cfg(feature = "debug")]
|
#[cfg(feature = "debug")]
|
||||||
{
|
{
|
||||||
dbg_println!("=== FATAL ERROR");
|
dbg_println!("=== FATAL ERROR");
|
||||||
@ -42,9 +42,6 @@ pub fn __fatal_error(_expr: &str, msg: &str, _file: &str, _line: u32, _func: &st
|
|||||||
if _line != 0 {
|
if _line != 0 {
|
||||||
dbg_println!("Location: {}:{}", _file, _line);
|
dbg_println!("Location: {}:{}", _file, _line);
|
||||||
}
|
}
|
||||||
if !_expr.is_empty() {
|
|
||||||
dbg_println!("Expression: {}", _expr);
|
|
||||||
}
|
|
||||||
if !msg.is_empty() {
|
if !msg.is_empty() {
|
||||||
dbg_println!("Message: {}", msg);
|
dbg_println!("Message: {}", msg);
|
||||||
}
|
}
|
||||||
@ -56,49 +53,31 @@ pub fn __fatal_error(_expr: &str, msg: &str, _file: &str, _line: u32, _func: &st
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub trait UnwrapOrFatalError<T> {
|
pub trait UnwrapOrFatalError<T> {
|
||||||
fn unwrap_or_fatal_error(self, expr: &str, msg: &str, file: &str, line: u32, func: &str) -> T;
|
fn unwrap_or_fatal_error(self, msg: &str, file: &str, line: u32) -> T;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T> UnwrapOrFatalError<T> for Option<T> {
|
impl<T> UnwrapOrFatalError<T> for Option<T> {
|
||||||
fn unwrap_or_fatal_error(self, expr: &str, msg: &str, file: &str, line: u32, func: &str) -> T {
|
fn unwrap_or_fatal_error(self, msg: &str, file: &str, line: u32) -> T {
|
||||||
match self {
|
match self {
|
||||||
Some(x) => x,
|
Some(x) => x,
|
||||||
None => __fatal_error(expr, msg, file, line, func),
|
None => __fatal_error(msg, file, line),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<T, E> UnwrapOrFatalError<T> for Result<T, E> {
|
impl<T, E> UnwrapOrFatalError<T> for Result<T, E> {
|
||||||
fn unwrap_or_fatal_error(self, expr: &str, msg: &str, file: &str, line: u32, func: &str) -> T {
|
fn unwrap_or_fatal_error(self, msg: &str, file: &str, line: u32) -> T {
|
||||||
match self {
|
match self {
|
||||||
Ok(x) => x,
|
Ok(x) => x,
|
||||||
Err(_) => __fatal_error(expr, msg, file, line, func),
|
Err(_) => __fatal_error(msg, file, line),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! function_name {
|
|
||||||
() => {{
|
|
||||||
#[cfg(not(feature = "bootloader"))]
|
|
||||||
{
|
|
||||||
fn f() {}
|
|
||||||
fn type_name_of<T>(_: T) -> &'static str {
|
|
||||||
core::any::type_name::<T>()
|
|
||||||
}
|
|
||||||
let name = type_name_of(f);
|
|
||||||
name.get(..name.len() - 3).unwrap_or("")
|
|
||||||
}
|
|
||||||
#[cfg(feature = "bootloader")]
|
|
||||||
{
|
|
||||||
""
|
|
||||||
}
|
|
||||||
}};
|
|
||||||
}
|
|
||||||
|
|
||||||
macro_rules! unwrap {
|
macro_rules! unwrap {
|
||||||
($e:expr, $msg:expr) => {{
|
($e:expr, $msg:expr) => {{
|
||||||
use crate::trezorhal::fatal_error::UnwrapOrFatalError;
|
use crate::trezorhal::fatal_error::UnwrapOrFatalError;
|
||||||
$e.unwrap_or_fatal_error(stringify!($e), $msg, file!(), line!(), function_name!())
|
$e.unwrap_or_fatal_error($msg, file!(), line!())
|
||||||
}};
|
}};
|
||||||
($expr:expr) => {
|
($expr:expr) => {
|
||||||
unwrap!($expr, "unwrap failed")
|
unwrap!($expr, "unwrap failed")
|
||||||
@ -108,19 +87,13 @@ macro_rules! unwrap {
|
|||||||
macro_rules! ensure {
|
macro_rules! ensure {
|
||||||
($what:expr, $error:expr) => {
|
($what:expr, $error:expr) => {
|
||||||
if !($what) {
|
if !($what) {
|
||||||
crate::trezorhal::fatal_error::__fatal_error(
|
crate::trezorhal::fatal_error::__fatal_error($error, file!(), line!());
|
||||||
stringify!($what),
|
|
||||||
$error,
|
|
||||||
file!(),
|
|
||||||
line!(),
|
|
||||||
function_name!(),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
macro_rules! fatal_error {
|
macro_rules! fatal_error {
|
||||||
($msg:expr) => {{
|
($msg:expr) => {{
|
||||||
crate::trezorhal::fatal_error::__fatal_error("", $msg, file!(), line!(), function_name!());
|
crate::trezorhal::fatal_error::__fatal_error($msg, file!(), line!());
|
||||||
}};
|
}};
|
||||||
}
|
}
|
||||||
|
@ -33,8 +33,7 @@ uint8_t HW_ENTROPY_DATA[HW_ENTROPY_LEN];
|
|||||||
static HMAC_DRBG_CTX drbg_ctx;
|
static HMAC_DRBG_CTX drbg_ctx;
|
||||||
|
|
||||||
void __attribute__((noreturn))
|
void __attribute__((noreturn))
|
||||||
__fatal_error(const char *expr, const char *msg, const char *file, int line_num,
|
__fatal_error(const char *msg, const char *file, int line_num) {
|
||||||
const char *func) {
|
|
||||||
const BITMAP *icon = &bmp_icon_error;
|
const BITMAP *icon = &bmp_icon_error;
|
||||||
char line[128] = {0};
|
char line[128] = {0};
|
||||||
int y = icon->height + 3;
|
int y = icon->height + 3;
|
||||||
@ -44,7 +43,7 @@ __fatal_error(const char *expr, const char *msg, const char *file, int line_num,
|
|||||||
oledDrawStringCenter(OLED_WIDTH / 2, (icon->height - FONT_HEIGHT) / 2 + 1,
|
oledDrawStringCenter(OLED_WIDTH / 2, (icon->height - FONT_HEIGHT) / 2 + 1,
|
||||||
"FATAL ERROR", FONT_STANDARD);
|
"FATAL ERROR", FONT_STANDARD);
|
||||||
|
|
||||||
snprintf(line, sizeof(line), "Expr: %s", expr ? expr : "(null)");
|
snprintf(line, sizeof(line), "Expr: %s", "(null)");
|
||||||
oledDrawString(0, y, line, FONT_STANDARD);
|
oledDrawString(0, y, line, FONT_STANDARD);
|
||||||
y += FONT_HEIGHT + 1;
|
y += FONT_HEIGHT + 1;
|
||||||
|
|
||||||
@ -59,7 +58,7 @@ __fatal_error(const char *expr, const char *msg, const char *file, int line_num,
|
|||||||
oledDrawString(0, y, label, FONT_STANDARD);
|
oledDrawString(0, y, label, FONT_STANDARD);
|
||||||
y += FONT_HEIGHT + 1;
|
y += FONT_HEIGHT + 1;
|
||||||
|
|
||||||
snprintf(line, sizeof(line), "Func: %s", func ? func : "(null)");
|
snprintf(line, sizeof(line), "Func: %s", "(null)");
|
||||||
oledDrawString(0, y, line, FONT_STANDARD);
|
oledDrawString(0, y, line, FONT_STANDARD);
|
||||||
y += FONT_HEIGHT + 1;
|
y += FONT_HEIGHT + 1;
|
||||||
|
|
||||||
@ -80,7 +79,10 @@ error_shutdown(const char *line1, const char *line2, const char *line3,
|
|||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
void __assert_func(const char *file, int line, const char *func,
|
void __assert_func(const char *file, int line, const char *func,
|
||||||
const char *expr) {
|
const char *expr) {
|
||||||
__fatal_error(expr, "assert failed", file, line, func);
|
(void)func;
|
||||||
|
(void)expr;
|
||||||
|
|
||||||
|
__fatal_error("assert failed", file, line);
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
@ -28,8 +28,7 @@
|
|||||||
extern uint8_t HW_ENTROPY_DATA[HW_ENTROPY_LEN];
|
extern uint8_t HW_ENTROPY_DATA[HW_ENTROPY_LEN];
|
||||||
|
|
||||||
void __attribute__((noreturn))
|
void __attribute__((noreturn))
|
||||||
__fatal_error(const char *expr, const char *msg, const char *file, int line,
|
__fatal_error(const char *msg, const char *file, int line);
|
||||||
const char *func);
|
|
||||||
void __attribute__((noreturn))
|
void __attribute__((noreturn))
|
||||||
error_shutdown(const char *line1, const char *line2, const char *line3,
|
error_shutdown(const char *line1, const char *line2, const char *line3,
|
||||||
const char *line4);
|
const char *line4);
|
||||||
@ -37,9 +36,7 @@ void show_wipe_code_screen(void);
|
|||||||
void show_pin_too_many_screen(void);
|
void show_pin_too_many_screen(void);
|
||||||
|
|
||||||
#define ensure(expr, msg) \
|
#define ensure(expr, msg) \
|
||||||
(((expr) == sectrue) \
|
(((expr) == sectrue) ? (void)0 : __fatal_error(msg, __FILE__, __LINE__))
|
||||||
? (void)0 \
|
|
||||||
: __fatal_error(#expr, msg, __FILE__, __LINE__, __func__))
|
|
||||||
|
|
||||||
void hal_delay(uint32_t ms);
|
void hal_delay(uint32_t ms);
|
||||||
|
|
||||||
|
@ -45,13 +45,13 @@
|
|||||||
#ifdef USE_SECP256K1_ZKP
|
#ifdef USE_SECP256K1_ZKP
|
||||||
void secp256k1_default_illegal_callback_fn(const char *str, void *data) {
|
void secp256k1_default_illegal_callback_fn(const char *str, void *data) {
|
||||||
(void)data;
|
(void)data;
|
||||||
__fatal_error(NULL, str, __FILE__, __LINE__, __func__);
|
__fatal_error(str, __FILE__, __LINE__);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
void secp256k1_default_error_callback_fn(const char *str, void *data) {
|
void secp256k1_default_error_callback_fn(const char *str, void *data) {
|
||||||
(void)data;
|
(void)data;
|
||||||
__fatal_error(NULL, str, __FILE__, __LINE__, __func__);
|
__fatal_error(str, __FILE__, __LINE__);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
@ -158,9 +158,8 @@ static const uint8_t FALSE_BYTE = 0x00;
|
|||||||
static const uint32_t TRUE_WORD = 0xC35A69A5;
|
static const uint32_t TRUE_WORD = 0xC35A69A5;
|
||||||
static const uint32_t FALSE_WORD = 0x3CA5965A;
|
static const uint32_t FALSE_WORD = 0x3CA5965A;
|
||||||
|
|
||||||
static void __handle_fault(const char *msg, const char *file, int line,
|
static void __handle_fault(const char *msg, const char *file, int line);
|
||||||
const char *func);
|
#define handle_fault(msg) (__handle_fault(msg, __FILE_NAME__, __LINE__))
|
||||||
#define handle_fault(msg) (__handle_fault(msg, __FILE__, __LINE__, __func__))
|
|
||||||
|
|
||||||
static uint32_t pin_to_int(const uint8_t *pin, size_t pin_len);
|
static uint32_t pin_to_int(const uint8_t *pin, size_t pin_len);
|
||||||
static secbool storage_upgrade(void);
|
static secbool storage_upgrade(void);
|
||||||
@ -1356,15 +1355,14 @@ void storage_wipe(void) {
|
|||||||
init_wiped_storage();
|
init_wiped_storage();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void __handle_fault(const char *msg, const char *file, int line,
|
static void __handle_fault(const char *msg, const char *file, int line) {
|
||||||
const char *func) {
|
|
||||||
CONFIDENTIAL static secbool in_progress = secfalse;
|
CONFIDENTIAL static secbool in_progress = secfalse;
|
||||||
|
|
||||||
// If fault handling is already in progress, then we are probably facing a
|
// If fault handling is already in progress, then we are probably facing a
|
||||||
// fault injection attack, so wipe.
|
// fault injection attack, so wipe.
|
||||||
if (secfalse != in_progress) {
|
if (secfalse != in_progress) {
|
||||||
storage_wipe();
|
storage_wipe();
|
||||||
__fatal_error("Fault detected", msg, file, line, func);
|
__fatal_error(msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
// We use the PIN fail counter as a fault counter. Increment the counter,
|
// We use the PIN fail counter as a fault counter. Increment the counter,
|
||||||
@ -1373,19 +1371,19 @@ static void __handle_fault(const char *msg, const char *file, int line,
|
|||||||
uint32_t ctr = 0;
|
uint32_t ctr = 0;
|
||||||
if (sectrue != pin_get_fails(&ctr)) {
|
if (sectrue != pin_get_fails(&ctr)) {
|
||||||
storage_wipe();
|
storage_wipe();
|
||||||
__fatal_error("Fault detected", msg, file, line, func);
|
__fatal_error(msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sectrue != storage_pin_fails_increase()) {
|
if (sectrue != storage_pin_fails_increase()) {
|
||||||
storage_wipe();
|
storage_wipe();
|
||||||
__fatal_error("Fault detected", msg, file, line, func);
|
__fatal_error(msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t ctr_new = 0;
|
uint32_t ctr_new = 0;
|
||||||
if (sectrue != pin_get_fails(&ctr_new) || ctr + 1 != ctr_new) {
|
if (sectrue != pin_get_fails(&ctr_new) || ctr + 1 != ctr_new) {
|
||||||
storage_wipe();
|
storage_wipe();
|
||||||
}
|
}
|
||||||
__fatal_error("Fault detected", msg, file, line, func);
|
__fatal_error(msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -28,21 +28,14 @@ void __shutdown(void) {
|
|||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void __fatal_error(const char *expr, const char *msg, const char *file,
|
void __fatal_error(const char *msg, const char *file, int line) {
|
||||||
int line, const char *func) {
|
|
||||||
printf("\nFATAL ERROR:\n");
|
printf("\nFATAL ERROR:\n");
|
||||||
if (expr) {
|
|
||||||
printf("expr: %s\n", expr);
|
|
||||||
}
|
|
||||||
if (msg) {
|
if (msg) {
|
||||||
printf("msg : %s\n", msg);
|
printf("msg : %s\n", msg);
|
||||||
}
|
}
|
||||||
if (file) {
|
if (file) {
|
||||||
printf("file: %s:%d\n", file, line);
|
printf("file: %s:%d\n", file, line);
|
||||||
}
|
}
|
||||||
if (func) {
|
|
||||||
printf("func: %s\n", func);
|
|
||||||
}
|
|
||||||
__shutdown();
|
__shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,16 +22,13 @@
|
|||||||
|
|
||||||
#include "secbool.h"
|
#include "secbool.h"
|
||||||
|
|
||||||
void __fatal_error(const char *expr, const char *msg, const char *file,
|
void __fatal_error(const char *msg, const char *file, int line);
|
||||||
int line, const char *func);
|
|
||||||
|
|
||||||
void show_wipe_code_screen(void);
|
void show_wipe_code_screen(void);
|
||||||
void show_pin_too_many_screen(void);
|
void show_pin_too_many_screen(void);
|
||||||
|
|
||||||
#define ensure(expr, msg) \
|
#define ensure(expr, msg) \
|
||||||
(((expr) == sectrue) \
|
(((expr) == sectrue) ? (void)0 : __fatal_error(msg, __FILE__, __LINE__))
|
||||||
? (void)0 \
|
|
||||||
: __fatal_error(#expr, msg, __FILE__, __LINE__, __func__))
|
|
||||||
|
|
||||||
#define hal_delay(ms) (void)ms;
|
#define hal_delay(ms) (void)ms;
|
||||||
|
|
||||||
|
@ -28,21 +28,14 @@ void __shutdown(void) {
|
|||||||
exit(3);
|
exit(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
void __fatal_error(const char *expr, const char *msg, const char *file,
|
void __fatal_error(const char *msg, const char *file, int line) {
|
||||||
int line, const char *func) {
|
|
||||||
printf("\nFATAL ERROR:\n");
|
printf("\nFATAL ERROR:\n");
|
||||||
if (expr) {
|
|
||||||
printf("expr: %s\n", expr);
|
|
||||||
}
|
|
||||||
if (msg) {
|
if (msg) {
|
||||||
printf("msg : %s\n", msg);
|
printf("msg : %s\n", msg);
|
||||||
}
|
}
|
||||||
if (file) {
|
if (file) {
|
||||||
printf("file: %s:%d\n", file, line);
|
printf("file: %s:%d\n", file, line);
|
||||||
}
|
}
|
||||||
if (func) {
|
|
||||||
printf("func: %s\n", func);
|
|
||||||
}
|
|
||||||
__shutdown();
|
__shutdown();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -22,16 +22,13 @@
|
|||||||
|
|
||||||
#include "secbool.h"
|
#include "secbool.h"
|
||||||
|
|
||||||
void __fatal_error(const char *expr, const char *msg, const char *file,
|
void __fatal_error(const char *msg, const char *file, int line);
|
||||||
int line, const char *func);
|
|
||||||
|
|
||||||
void show_wipe_code_screen(void);
|
void show_wipe_code_screen(void);
|
||||||
void show_pin_too_many_screen(void);
|
void show_pin_too_many_screen(void);
|
||||||
|
|
||||||
#define ensure(expr, msg) \
|
#define ensure(expr, msg) \
|
||||||
(((expr) == sectrue) \
|
(((expr) == sectrue) ? (void)0 : __fatal_error(msg, __FILE__, __LINE__))
|
||||||
? (void)0 \
|
|
||||||
: __fatal_error(#expr, msg, __FILE__, __LINE__, __func__))
|
|
||||||
|
|
||||||
#define hal_delay(ms) (void)ms;
|
#define hal_delay(ms) (void)ms;
|
||||||
|
|
||||||
|
@ -171,9 +171,8 @@ static const uint8_t FALSE_BYTE = 0x00;
|
|||||||
static const uint32_t TRUE_WORD = 0xC35A69A5;
|
static const uint32_t TRUE_WORD = 0xC35A69A5;
|
||||||
static const uint32_t FALSE_WORD = 0x3CA5965A;
|
static const uint32_t FALSE_WORD = 0x3CA5965A;
|
||||||
|
|
||||||
static void __handle_fault(const char *msg, const char *file, int line,
|
static void __handle_fault(const char *msg, const char *file, int line);
|
||||||
const char *func);
|
#define handle_fault(msg) (__handle_fault(msg, __FILE__, __LINE__))
|
||||||
#define handle_fault(msg) (__handle_fault(msg, __FILE__, __LINE__, __func__))
|
|
||||||
|
|
||||||
static uint32_t pin_to_int(const uint8_t *pin, size_t pin_len);
|
static uint32_t pin_to_int(const uint8_t *pin, size_t pin_len);
|
||||||
static secbool storage_upgrade(void);
|
static secbool storage_upgrade(void);
|
||||||
@ -1634,15 +1633,14 @@ void storage_wipe(void) {
|
|||||||
init_wiped_storage();
|
init_wiped_storage();
|
||||||
}
|
}
|
||||||
|
|
||||||
static void __handle_fault(const char *msg, const char *file, int line,
|
static void __handle_fault(const char *msg, const char *file, int line) {
|
||||||
const char *func) {
|
|
||||||
static secbool in_progress = secfalse;
|
static secbool in_progress = secfalse;
|
||||||
|
|
||||||
// If fault handling is already in progress, then we are probably facing a
|
// If fault handling is already in progress, then we are probably facing a
|
||||||
// fault injection attack, so wipe.
|
// fault injection attack, so wipe.
|
||||||
if (secfalse != in_progress) {
|
if (secfalse != in_progress) {
|
||||||
storage_wipe();
|
storage_wipe();
|
||||||
__fatal_error("Fault detected", msg, file, line, func);
|
__fatal_error(msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
// We use the PIN fail counter as a fault counter. Increment the counter,
|
// We use the PIN fail counter as a fault counter. Increment the counter,
|
||||||
@ -1651,19 +1649,19 @@ static void __handle_fault(const char *msg, const char *file, int line,
|
|||||||
uint32_t ctr = 0;
|
uint32_t ctr = 0;
|
||||||
if (sectrue != pin_get_fails(&ctr)) {
|
if (sectrue != pin_get_fails(&ctr)) {
|
||||||
storage_wipe();
|
storage_wipe();
|
||||||
__fatal_error("Fault detected", msg, file, line, func);
|
__fatal_error(msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (sectrue != storage_pin_fails_increase()) {
|
if (sectrue != storage_pin_fails_increase()) {
|
||||||
storage_wipe();
|
storage_wipe();
|
||||||
__fatal_error("Fault detected", msg, file, line, func);
|
__fatal_error(msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
uint32_t ctr_new = 0;
|
uint32_t ctr_new = 0;
|
||||||
if (sectrue != pin_get_fails(&ctr_new) || ctr + 1 != ctr_new) {
|
if (sectrue != pin_get_fails(&ctr_new) || ctr + 1 != ctr_new) {
|
||||||
storage_wipe();
|
storage_wipe();
|
||||||
}
|
}
|
||||||
__fatal_error("Fault detected", msg, file, line, func);
|
__fatal_error(msg, file, line);
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
Loading…
Reference in New Issue
Block a user