2018-02-26 13:06:10 +00:00
|
|
|
/*
|
2019-06-17 18:27:55 +00:00
|
|
|
* This file is part of the Trezor project, https://trezor.io/
|
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/>.
|
|
|
|
*/
|
|
|
|
|
2019-12-15 08:41:36 +00:00
|
|
|
#include STM32_HAL_H
|
|
|
|
|
2017-02-24 12:19:00 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
2023-01-24 20:57:27 +00:00
|
|
|
#include "py/builtin.h"
|
2017-02-24 12:19:00 +00:00
|
|
|
#include "py/compile.h"
|
|
|
|
#include "py/gc.h"
|
2017-03-26 21:11:57 +00:00
|
|
|
#include "py/mperrno.h"
|
2019-03-29 15:26:02 +00:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/repl.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/stackctrl.h"
|
2021-09-09 18:26:10 +00:00
|
|
|
#include "shared/runtime/pyexec.h"
|
2017-02-24 12:19:00 +00:00
|
|
|
|
2019-01-24 18:30:16 +00:00
|
|
|
#include "ports/stm32/gccollect.h"
|
|
|
|
#include "ports/stm32/pendsv.h"
|
2017-02-24 12:19:00 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
#include "bl_check.h"
|
2022-05-31 11:29:04 +00:00
|
|
|
#include "board_capabilities.h"
|
2017-03-21 00:41:49 +00:00
|
|
|
#include "common.h"
|
2021-06-03 11:11:58 +00:00
|
|
|
#include "compiler_traits.h"
|
2017-03-21 00:41:49 +00:00
|
|
|
#include "display.h"
|
2023-11-14 10:53:24 +00:00
|
|
|
#include "fault_handlers.h"
|
2017-03-07 14:52:19 +00:00
|
|
|
#include "flash.h"
|
2022-04-11 11:27:48 +00:00
|
|
|
#include "image.h"
|
2023-08-23 13:42:16 +00:00
|
|
|
#include "memzero.h"
|
2023-06-30 10:12:55 +00:00
|
|
|
#include "model.h"
|
2018-02-23 18:08:34 +00:00
|
|
|
#include "mpu.h"
|
2021-02-12 11:12:20 +00:00
|
|
|
#include "random_delays.h"
|
2023-11-29 13:51:25 +00:00
|
|
|
#include "rust_ui.h"
|
2023-06-28 08:51:30 +00:00
|
|
|
#include "secure_aes.h"
|
2023-06-30 10:12:55 +00:00
|
|
|
|
2023-07-24 21:26:07 +00:00
|
|
|
#include TREZOR_BOARD
|
|
|
|
|
2023-02-15 12:57:54 +00:00
|
|
|
#ifdef USE_RGB_LED
|
2022-05-31 07:31:32 +00:00
|
|
|
#include "rgb_led.h"
|
|
|
|
#endif
|
2023-06-22 19:56:48 +00:00
|
|
|
#ifdef USE_CONSUMPTION_MASK
|
|
|
|
#include "consumption_mask.h"
|
|
|
|
#endif
|
2023-02-15 12:57:54 +00:00
|
|
|
#ifdef USE_DMA2D
|
2022-08-16 14:51:10 +00:00
|
|
|
#include "dma2d.h"
|
|
|
|
#endif
|
2023-02-15 12:57:54 +00:00
|
|
|
#ifdef USE_BUTTON
|
2022-05-31 07:31:32 +00:00
|
|
|
#include "button.h"
|
|
|
|
#endif
|
2023-03-27 15:52:59 +00:00
|
|
|
#ifdef USE_I2C
|
|
|
|
#include "i2c.h"
|
|
|
|
#endif
|
2023-02-15 12:57:54 +00:00
|
|
|
#ifdef USE_TOUCH
|
2023-06-23 14:50:13 +00:00
|
|
|
#include "touch.h"
|
2023-02-15 12:57:54 +00:00
|
|
|
#endif
|
|
|
|
#ifdef USE_SD_CARD
|
|
|
|
#include "sdcard.h"
|
|
|
|
#endif
|
2023-12-15 22:50:33 +00:00
|
|
|
#ifdef USE_HASH_PROCESSOR
|
|
|
|
#include "hash_processor.h"
|
|
|
|
#endif
|
|
|
|
|
2023-07-24 21:26:07 +00:00
|
|
|
#ifdef USE_OPTIGA
|
2023-08-23 13:42:16 +00:00
|
|
|
#include "optiga_commands.h"
|
2023-07-24 21:26:07 +00:00
|
|
|
#include "optiga_transport.h"
|
2023-06-28 08:51:30 +00:00
|
|
|
#endif
|
|
|
|
#if defined USE_OPTIGA | defined STM32U5
|
2023-08-23 13:42:16 +00:00
|
|
|
#include "secret.h"
|
2023-07-24 21:26:07 +00:00
|
|
|
#endif
|
2023-06-28 08:51:30 +00:00
|
|
|
|
2023-04-14 15:18:03 +00:00
|
|
|
#include "unit_variant.h"
|
2022-05-31 07:31:32 +00:00
|
|
|
|
2020-11-27 10:32:29 +00:00
|
|
|
#ifdef SYSTEM_VIEW
|
|
|
|
#include "systemview.h"
|
|
|
|
#endif
|
2023-06-23 14:50:13 +00:00
|
|
|
#include "platform.h"
|
2017-03-07 14:52:19 +00:00
|
|
|
#include "rng.h"
|
2019-12-10 19:12:55 +00:00
|
|
|
#include "supervise.h"
|
2021-05-12 13:50:25 +00:00
|
|
|
#ifdef USE_SECP256K1_ZKP
|
|
|
|
#include "zkp_context.h"
|
|
|
|
#endif
|
2023-08-24 09:30:29 +00:00
|
|
|
#ifdef USE_HAPTIC
|
|
|
|
#include "haptic.h"
|
|
|
|
#endif
|
2017-03-24 15:38:46 +00:00
|
|
|
|
2021-06-14 11:28:25 +00:00
|
|
|
// from util.s
|
|
|
|
extern void shutdown_privileged(void);
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
int main(void) {
|
2021-06-24 15:12:19 +00:00
|
|
|
random_delays_init();
|
|
|
|
|
2020-02-26 16:41:40 +00:00
|
|
|
#ifdef RDI
|
|
|
|
rdi_start();
|
|
|
|
#endif
|
2019-06-07 18:16:40 +00:00
|
|
|
|
2023-06-28 08:51:30 +00:00
|
|
|
#ifdef STM32U5
|
|
|
|
check_oem_keys();
|
|
|
|
#endif
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
// reinitialize HAL for Trezor One
|
2022-07-26 13:09:43 +00:00
|
|
|
#if defined TREZOR_MODEL_1
|
2019-03-29 15:26:02 +00:00
|
|
|
HAL_Init();
|
2018-09-26 17:34:05 +00:00
|
|
|
#endif
|
|
|
|
|
2020-11-27 10:32:29 +00:00
|
|
|
#ifdef SYSTEM_VIEW
|
|
|
|
enable_systemview();
|
|
|
|
#endif
|
|
|
|
|
2023-12-15 22:50:33 +00:00
|
|
|
#ifdef USE_HASH_PROCESSOR
|
|
|
|
hash_processor_init();
|
|
|
|
#endif
|
|
|
|
|
2023-12-12 12:34:33 +00:00
|
|
|
#ifdef USE_DMA2D
|
|
|
|
dma2d_init();
|
|
|
|
#endif
|
|
|
|
|
2023-01-04 12:51:33 +00:00
|
|
|
display_reinit();
|
|
|
|
|
2023-11-29 13:51:25 +00:00
|
|
|
screen_boot_full();
|
|
|
|
|
2022-05-31 11:29:04 +00:00
|
|
|
#if !defined TREZOR_MODEL_1
|
|
|
|
parse_boardloader_capabilities();
|
|
|
|
|
2023-04-14 15:18:03 +00:00
|
|
|
unit_variant_init();
|
|
|
|
|
2024-03-06 14:53:50 +00:00
|
|
|
#ifdef STM32U5
|
|
|
|
secure_aes_init();
|
|
|
|
#endif
|
|
|
|
|
2023-09-14 14:52:45 +00:00
|
|
|
#ifdef USE_OPTIGA
|
|
|
|
uint8_t secret[SECRET_OPTIGA_KEY_LEN] = {0};
|
2024-03-06 14:53:50 +00:00
|
|
|
secbool secret_ok = secret_optiga_get(secret);
|
2023-09-14 14:52:45 +00:00
|
|
|
#endif
|
|
|
|
|
2023-11-16 22:34:09 +00:00
|
|
|
mpu_config_firmware_initial();
|
|
|
|
|
2023-11-23 11:50:02 +00:00
|
|
|
collect_hw_entropy();
|
|
|
|
|
2023-03-02 15:31:25 +00:00
|
|
|
#if PRODUCTION || BOOTLOADER_QA
|
2019-03-29 15:26:02 +00:00
|
|
|
check_and_replace_bootloader();
|
2020-07-13 08:30:58 +00:00
|
|
|
#endif
|
2019-03-29 15:26:02 +00:00
|
|
|
// Enable MPU
|
|
|
|
mpu_config_firmware();
|
2018-09-26 17:34:05 +00:00
|
|
|
#endif
|
2018-02-23 18:08:34 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
// Init peripherals
|
|
|
|
pendsv_init();
|
2018-09-26 17:34:05 +00:00
|
|
|
|
2023-11-14 10:53:24 +00:00
|
|
|
fault_handlers_init();
|
2022-09-05 10:12:33 +00:00
|
|
|
|
2023-02-15 12:57:54 +00:00
|
|
|
#if defined TREZOR_MODEL_T
|
|
|
|
set_core_clock(CLOCK_180_MHZ);
|
2018-09-26 17:34:05 +00:00
|
|
|
#endif
|
|
|
|
|
2023-02-15 12:57:54 +00:00
|
|
|
#ifdef USE_BUTTON
|
2022-04-25 10:05:19 +00:00
|
|
|
button_init();
|
2023-02-15 12:57:54 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_RGB_LED
|
2022-05-31 07:31:32 +00:00
|
|
|
rgb_led_init();
|
2023-06-22 19:56:48 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_CONSUMPTION_MASK
|
|
|
|
consumption_mask_init();
|
2022-04-25 10:05:19 +00:00
|
|
|
#endif
|
2023-03-27 15:52:59 +00:00
|
|
|
|
|
|
|
#ifdef USE_I2C
|
|
|
|
i2c_init();
|
|
|
|
#endif
|
2022-04-25 10:05:19 +00:00
|
|
|
|
2023-02-15 12:57:54 +00:00
|
|
|
#ifdef USE_TOUCH
|
2022-07-26 13:09:43 +00:00
|
|
|
touch_init();
|
2023-02-15 12:57:54 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef USE_SD_CARD
|
2019-03-29 15:26:02 +00:00
|
|
|
sdcard_init();
|
2022-07-26 13:09:43 +00:00
|
|
|
#endif
|
2017-03-07 14:52:19 +00:00
|
|
|
|
2023-08-24 09:30:29 +00:00
|
|
|
#ifdef USE_HAPTIC
|
|
|
|
haptic_init();
|
|
|
|
#endif
|
|
|
|
|
2023-07-24 21:26:07 +00:00
|
|
|
#ifdef USE_OPTIGA
|
|
|
|
optiga_init();
|
2023-08-23 13:42:16 +00:00
|
|
|
optiga_open_application();
|
2023-09-14 14:52:45 +00:00
|
|
|
if (sectrue == secret_ok) {
|
2023-08-23 13:42:16 +00:00
|
|
|
optiga_sec_chan_handshake(secret, sizeof(secret));
|
|
|
|
}
|
|
|
|
memzero(secret, sizeof(secret));
|
2023-07-24 21:26:07 +00:00
|
|
|
#endif
|
|
|
|
|
2022-07-26 13:09:43 +00:00
|
|
|
#if !defined TREZOR_MODEL_1
|
2023-07-20 11:20:50 +00:00
|
|
|
drop_privileges();
|
2018-09-26 17:34:05 +00:00
|
|
|
#endif
|
2018-02-22 19:40:04 +00:00
|
|
|
|
2021-11-03 21:25:16 +00:00
|
|
|
#ifdef USE_SECP256K1_ZKP
|
|
|
|
ensure(sectrue * (zkp_context_init() == 0), NULL);
|
|
|
|
#endif
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
printf("CORE: Preparing stack\n");
|
|
|
|
// Stack limit should be less than real stack size, so we have a chance
|
|
|
|
// to recover from limit hit.
|
|
|
|
mp_stack_set_top(&_estack);
|
2022-08-09 17:16:54 +00:00
|
|
|
mp_stack_set_limit((char *)&_estack - (char *)&_sstack - 1024);
|
2019-03-29 15:26:02 +00:00
|
|
|
|
2020-06-30 12:42:29 +00:00
|
|
|
#if MICROPY_ENABLE_PYSTACK
|
|
|
|
static mp_obj_t pystack[1024];
|
|
|
|
mp_pystack_init(pystack, &pystack[MP_ARRAY_SIZE(pystack)]);
|
|
|
|
#endif
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
// GC init
|
|
|
|
printf("CORE: Starting GC\n");
|
|
|
|
gc_init(&_heap_start, &_heap_end);
|
|
|
|
|
|
|
|
// Interpreter init
|
|
|
|
printf("CORE: Starting interpreter\n");
|
|
|
|
mp_init();
|
|
|
|
mp_obj_list_init(mp_sys_argv, 0);
|
|
|
|
mp_obj_list_init(mp_sys_path, 0);
|
2023-01-24 20:57:27 +00:00
|
|
|
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__dot_frozen));
|
2019-03-29 15:26:02 +00:00
|
|
|
|
|
|
|
// Execute the main script
|
|
|
|
printf("CORE: Executing main script\n");
|
|
|
|
pyexec_frozen_module("main.py");
|
|
|
|
|
|
|
|
// Clean up
|
|
|
|
printf("CORE: Main script finished, cleaning up\n");
|
|
|
|
mp_deinit();
|
|
|
|
|
2024-01-24 09:44:12 +00:00
|
|
|
// Python code shouldn't ever exit, avoid black screen if it does
|
|
|
|
error_shutdown("INTERNAL ERROR", "(PE)");
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
return 0;
|
2017-02-24 12:19:00 +00:00
|
|
|
}
|
|
|
|
|
2017-10-11 18:01:22 +00:00
|
|
|
// MicroPython default exception handler
|
|
|
|
|
|
|
|
void __attribute__((noreturn)) nlr_jump_fail(void *val) {
|
2023-04-03 12:32:08 +00:00
|
|
|
error_shutdown("INTERNAL ERROR", "(UE)");
|
2019-12-11 15:08:42 +00:00
|
|
|
}
|
|
|
|
|
2017-11-21 13:33:36 +00:00
|
|
|
// MicroPython builtin stubs
|
2017-02-24 12:19:00 +00:00
|
|
|
|
|
|
|
mp_import_stat_t mp_import_stat(const char *path) {
|
2019-03-29 15:26:02 +00:00
|
|
|
return MP_IMPORT_STAT_NO_EXIST;
|
2017-02-24 12:19:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t mp_builtin_open(uint n_args, const mp_obj_t *args, mp_map_t *kwargs) {
|
2019-03-29 15:26:02 +00:00
|
|
|
return mp_const_none;
|
2017-02-24 12:19:00 +00:00
|
|
|
}
|
|
|
|
MP_DEFINE_CONST_FUN_OBJ_KW(mp_builtin_open_obj, 1, mp_builtin_open);
|