2018-02-23 18:04:46 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
2018-02-23 18:04:46 +00:00
|
|
|
*
|
2018-02-26 13:06:10 +00:00
|
|
|
* 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/>.
|
2018-02-23 18:04:46 +00:00
|
|
|
*/
|
|
|
|
|
2017-03-21 00:41:49 +00:00
|
|
|
#include STM32_HAL_H
|
|
|
|
|
2019-01-25 17:33:25 +00:00
|
|
|
#include <string.h>
|
|
|
|
|
2017-04-17 16:07:34 +00:00
|
|
|
#include "common.h"
|
2017-03-21 00:41:49 +00:00
|
|
|
#include "display.h"
|
2022-05-05 11:47:19 +00:00
|
|
|
#ifdef FANCY_FATAL_ERROR
|
|
|
|
#include "rust_ui.h"
|
|
|
|
#endif
|
2019-02-18 16:27:31 +00:00
|
|
|
#include "flash.h"
|
2019-06-08 11:53:17 +00:00
|
|
|
#include "rand.h"
|
2022-10-26 17:22:18 +00:00
|
|
|
#include "stm32.h"
|
2021-06-21 14:19:25 +00:00
|
|
|
#include "supervise.h"
|
2017-03-21 00:41:49 +00:00
|
|
|
|
2022-05-05 11:47:19 +00:00
|
|
|
#include "mini_printf.h"
|
2019-01-25 17:33:25 +00:00
|
|
|
#include "stm32f4xx_ll_utils.h"
|
|
|
|
|
2022-04-29 08:38:30 +00:00
|
|
|
#ifdef RGB16
|
2017-12-17 00:09:45 +00:00
|
|
|
#define COLOR_FATAL_ERROR RGB16(0x7F, 0x00, 0x00)
|
2022-04-29 08:38:30 +00:00
|
|
|
#else
|
|
|
|
#define COLOR_FATAL_ERROR COLOR_BLACK
|
|
|
|
#endif
|
2017-12-17 00:09:45 +00:00
|
|
|
|
2021-06-21 14:19:25 +00:00
|
|
|
// from util.s
|
|
|
|
extern void shutdown_privileged(void);
|
|
|
|
|
2022-05-05 11:47:19 +00:00
|
|
|
void __attribute__((noreturn)) shutdown(void) {
|
2021-06-21 14:19:25 +00:00
|
|
|
#ifdef USE_SVC_SHUTDOWN
|
|
|
|
svc_shutdown();
|
|
|
|
#else
|
|
|
|
// It won't work properly unless called from the privileged mode
|
|
|
|
shutdown_privileged();
|
|
|
|
#endif
|
2022-05-05 11:47:19 +00:00
|
|
|
|
|
|
|
for (;;)
|
|
|
|
;
|
2021-06-21 14:19:25 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
void __attribute__((noreturn))
|
|
|
|
__fatal_error(const char *expr, const char *msg, const char *file, int line,
|
|
|
|
const char *func) {
|
|
|
|
display_orientation(0);
|
|
|
|
display_backlight(255);
|
2022-05-05 11:47:19 +00:00
|
|
|
|
|
|
|
#ifdef FANCY_FATAL_ERROR
|
|
|
|
char buf[256] = {0};
|
|
|
|
mini_snprintf(buf, sizeof(buf), "%s: %d", file, line);
|
|
|
|
screen_fatal_error_c(msg, buf);
|
|
|
|
display_refresh();
|
|
|
|
#else
|
2019-03-29 15:26:02 +00:00
|
|
|
display_print_color(COLOR_WHITE, COLOR_FATAL_ERROR);
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
if (func) {
|
|
|
|
display_printf("func: %s\n", func);
|
|
|
|
}
|
2021-05-21 09:35:43 +00:00
|
|
|
#ifdef SCM_REVISION
|
|
|
|
const uint8_t *rev = (const uint8_t *)SCM_REVISION;
|
|
|
|
display_printf("rev : %02x%02x%02x%02x%02x\n", rev[0], rev[1], rev[2], rev[3],
|
|
|
|
rev[4]);
|
2017-10-12 14:02:40 +00:00
|
|
|
#endif
|
2019-06-17 18:27:55 +00:00
|
|
|
display_printf("\nPlease contact Trezor support.\n");
|
2022-05-05 11:47:19 +00:00
|
|
|
#endif
|
2021-06-14 14:37:45 +00:00
|
|
|
shutdown();
|
2019-02-07 11:02:41 +00:00
|
|
|
}
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
void __attribute__((noreturn))
|
2022-05-05 11:47:19 +00:00
|
|
|
error_shutdown(const char *label, const char *msg) {
|
2019-03-29 15:26:02 +00:00
|
|
|
display_orientation(0);
|
2022-05-05 11:47:19 +00:00
|
|
|
#ifdef FANCY_FATAL_ERROR
|
|
|
|
screen_error_shutdown_c(label, msg);
|
|
|
|
display_refresh();
|
2019-02-07 11:02:41 +00:00
|
|
|
#else
|
2019-03-29 15:26:02 +00:00
|
|
|
display_print_color(COLOR_WHITE, COLOR_FATAL_ERROR);
|
2022-05-05 11:47:19 +00:00
|
|
|
if (label) {
|
|
|
|
display_printf("%s\n", label);
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
2022-05-05 11:47:19 +00:00
|
|
|
if (msg) {
|
|
|
|
display_printf("%s\n", msg);
|
2019-03-29 15:26:02 +00:00
|
|
|
}
|
|
|
|
display_printf("\nPlease unplug the device.\n");
|
2019-02-07 11:02:41 +00:00
|
|
|
#endif
|
2019-03-29 15:26:02 +00:00
|
|
|
display_backlight(255);
|
2021-06-14 14:37:45 +00:00
|
|
|
shutdown();
|
2017-03-21 00:41:49 +00:00
|
|
|
}
|
|
|
|
|
2017-04-28 13:39:22 +00:00
|
|
|
#ifndef NDEBUG
|
2019-03-29 15:26:02 +00:00
|
|
|
void __assert_func(const char *file, int line, const char *func,
|
|
|
|
const char *expr) {
|
|
|
|
__fatal_error(expr, "assert failed", file, line, func);
|
2017-04-28 13:39:22 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
void hal_delay(uint32_t ms) { HAL_Delay(ms); }
|
2022-06-08 08:31:23 +00:00
|
|
|
uint32_t hal_ticks_ms() { return HAL_GetTick(); }
|
2017-10-12 22:36:12 +00:00
|
|
|
|
|
|
|
// reference RM0090 section 35.12.1 Figure 413
|
2019-03-29 15:26:02 +00:00
|
|
|
#define USB_OTG_HS_DATA_FIFO_RAM (USB_OTG_HS_PERIPH_BASE + 0x20000U)
|
2017-10-12 22:36:12 +00:00
|
|
|
#define USB_OTG_HS_DATA_FIFO_SIZE (4096U)
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
void clear_otg_hs_memory(void) {
|
|
|
|
// use the HAL version due to section 2.1.6 of STM32F42xx Errata sheet
|
|
|
|
__HAL_RCC_USB_OTG_HS_CLK_ENABLE(); // enable USB_OTG_HS peripheral clock so
|
|
|
|
// that the peripheral memory is
|
|
|
|
// accessible
|
|
|
|
memset_reg(
|
|
|
|
(volatile void *)USB_OTG_HS_DATA_FIFO_RAM,
|
|
|
|
(volatile void *)(USB_OTG_HS_DATA_FIFO_RAM + USB_OTG_HS_DATA_FIFO_SIZE),
|
|
|
|
0);
|
|
|
|
__HAL_RCC_USB_OTG_HS_CLK_DISABLE(); // disable USB OTG_HS peripheral clock as
|
|
|
|
// the peripheral is not needed right now
|
2017-10-12 22:36:12 +00:00
|
|
|
}
|
2017-11-07 19:41:16 +00:00
|
|
|
|
|
|
|
uint32_t __stack_chk_guard = 0;
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
void __attribute__((noreturn)) __stack_chk_fail(void) {
|
2022-05-05 11:47:19 +00:00
|
|
|
error_shutdown("Internal error", "(SS)");
|
2017-11-07 19:41:16 +00:00
|
|
|
}
|
2019-01-25 17:33:25 +00:00
|
|
|
|
|
|
|
uint8_t HW_ENTROPY_DATA[HW_ENTROPY_LEN];
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
void collect_hw_entropy(void) {
|
|
|
|
// collect entropy from UUID
|
|
|
|
uint32_t w = LL_GetUID_Word0();
|
|
|
|
memcpy(HW_ENTROPY_DATA, &w, 4);
|
|
|
|
w = LL_GetUID_Word1();
|
|
|
|
memcpy(HW_ENTROPY_DATA + 4, &w, 4);
|
|
|
|
w = LL_GetUID_Word2();
|
|
|
|
memcpy(HW_ENTROPY_DATA + 8, &w, 4);
|
|
|
|
|
|
|
|
// set entropy in the OTP randomness block
|
|
|
|
if (secfalse == flash_otp_is_locked(FLASH_OTP_BLOCK_RANDOMNESS)) {
|
|
|
|
uint8_t entropy[FLASH_OTP_BLOCK_SIZE];
|
|
|
|
random_buffer(entropy, FLASH_OTP_BLOCK_SIZE);
|
|
|
|
ensure(flash_otp_write(FLASH_OTP_BLOCK_RANDOMNESS, 0, entropy,
|
|
|
|
FLASH_OTP_BLOCK_SIZE),
|
|
|
|
NULL);
|
|
|
|
ensure(flash_otp_lock(FLASH_OTP_BLOCK_RANDOMNESS), NULL);
|
|
|
|
}
|
|
|
|
// collect entropy from OTP randomness block
|
|
|
|
ensure(flash_otp_read(FLASH_OTP_BLOCK_RANDOMNESS, 0, HW_ENTROPY_DATA + 12,
|
|
|
|
FLASH_OTP_BLOCK_SIZE),
|
|
|
|
NULL);
|
2019-01-25 17:33:25 +00:00
|
|
|
}
|
2022-08-16 14:51:10 +00:00
|
|
|
|
|
|
|
// this function resets settings changed in one layer (bootloader/firmware),
|
|
|
|
// which might be incompatible with the other layers older versions,
|
|
|
|
// where this setting might be unknown
|
|
|
|
void ensure_compatible_settings(void) {
|
|
|
|
#ifdef TREZOR_MODEL_T
|
|
|
|
display_set_big_endian();
|
2022-12-13 11:40:02 +00:00
|
|
|
display_orientation(0);
|
2022-10-26 17:22:18 +00:00
|
|
|
set_core_clock(CLOCK_168_MHZ);
|
2022-11-01 11:05:00 +00:00
|
|
|
display_set_slow_pwm();
|
2022-08-16 14:51:10 +00:00
|
|
|
#endif
|
|
|
|
}
|
2022-05-05 11:47:19 +00:00
|
|
|
|
|
|
|
void show_wipe_code_screen(void) {
|
|
|
|
error_shutdown(
|
|
|
|
"DEVICE WIPED!",
|
|
|
|
"You have entered the wipe code. All private data has been erased.");
|
|
|
|
}
|
|
|
|
void show_pin_too_many_screen(void) {
|
|
|
|
error_shutdown("DEVICE WIPED!",
|
|
|
|
"Too many wrong PIN attempts. Storage has been wiped.");
|
|
|
|
}
|