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>
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
#include "lib/utils/pyexec.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"
|
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"
|
2017-03-21 00:41:49 +00:00
|
|
|
#include "common.h"
|
|
|
|
#include "display.h"
|
2017-03-07 14:52:19 +00:00
|
|
|
#include "flash.h"
|
2018-02-23 18:08:34 +00:00
|
|
|
#include "mpu.h"
|
2020-02-26 16:41:40 +00:00
|
|
|
#ifdef RDI
|
2021-02-12 11:12:20 +00:00
|
|
|
#include "random_delays.h"
|
2020-02-26 16:41:40 +00:00
|
|
|
#endif
|
2020-11-27 10:32:29 +00:00
|
|
|
#ifdef SYSTEM_VIEW
|
|
|
|
#include "systemview.h"
|
|
|
|
#endif
|
2017-03-07 14:52:19 +00:00
|
|
|
#include "rng.h"
|
|
|
|
#include "sdcard.h"
|
2019-12-10 19:12:55 +00:00
|
|
|
#include "supervise.h"
|
2017-03-07 14:52:19 +00:00
|
|
|
#include "touch.h"
|
2017-03-24 15:38:46 +00:00
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
int main(void) {
|
2021-03-25 18:33:21 +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
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
// reinitialize HAL for Trezor One
|
2018-09-26 17:34:05 +00:00
|
|
|
#if TREZOR_MODEL == 1
|
2019-03-29 15:26:02 +00:00
|
|
|
HAL_Init();
|
2018-09-26 17:34:05 +00:00
|
|
|
#endif
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
collect_hw_entropy();
|
2019-01-25 17:33:25 +00:00
|
|
|
|
2020-11-27 10:32:29 +00:00
|
|
|
#ifdef SYSTEM_VIEW
|
|
|
|
enable_systemview();
|
|
|
|
#endif
|
|
|
|
|
2018-09-26 17:34:05 +00:00
|
|
|
#if TREZOR_MODEL == T
|
2020-07-13 08:30:58 +00:00
|
|
|
#if PRODUCTION
|
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
|
|
|
|
|
|
|
#if TREZOR_MODEL == 1
|
2019-03-29 15:26:02 +00:00
|
|
|
display_init();
|
|
|
|
touch_init();
|
2018-09-26 17:34:05 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TREZOR_MODEL == T
|
2021-02-25 19:13:30 +00:00
|
|
|
// display_init_seq();
|
2019-03-29 15:26:02 +00:00
|
|
|
sdcard_init();
|
|
|
|
touch_init();
|
|
|
|
touch_power_on();
|
2017-03-07 14:52:19 +00:00
|
|
|
|
2020-01-24 08:59:57 +00:00
|
|
|
// jump to unprivileged mode
|
|
|
|
// http://infocenter.arm.com/help/topic/com.arm.doc.dui0552a/CHDBIBGJ.html
|
|
|
|
__asm__ volatile("msr control, %0" ::"r"(0x1));
|
|
|
|
__asm__ volatile("isb");
|
|
|
|
|
2019-03-29 15:26:02 +00:00
|
|
|
display_clear();
|
2018-09-26 17:34:05 +00:00
|
|
|
#endif
|
2018-02-22 19:40:04 +00:00
|
|
|
|
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);
|
|
|
|
mp_stack_set_limit((char *)&_estack - (char *)&_heap_end - 1024);
|
|
|
|
|
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);
|
|
|
|
mp_obj_list_append(
|
|
|
|
mp_sys_path,
|
|
|
|
MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
|
|
|
|
|
|
|
|
// 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();
|
|
|
|
|
|
|
|
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) {
|
2019-12-11 15:08:42 +00:00
|
|
|
error_shutdown("Internal error", "(UE)", NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
// interrupt handlers
|
|
|
|
|
|
|
|
void NMI_Handler(void) {
|
|
|
|
// Clock Security System triggered NMI
|
|
|
|
if ((RCC->CIR & RCC_CIR_CSSF) != 0) {
|
|
|
|
error_shutdown("Internal error", "(CS)", NULL, NULL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HardFault_Handler(void) {
|
|
|
|
error_shutdown("Internal error", "(HF)", NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemManage_Handler(void) {
|
|
|
|
error_shutdown("Internal error", "(MM)", NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void BusFault_Handler(void) {
|
|
|
|
error_shutdown("Internal error", "(BF)", NULL, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
void UsageFault_Handler(void) {
|
|
|
|
error_shutdown("Internal error", "(UF)", NULL, NULL);
|
2017-10-11 18:01:22 +00:00
|
|
|
}
|
|
|
|
|
2019-12-10 19:12:55 +00:00
|
|
|
void SVC_C_Handler(uint32_t *stack) {
|
|
|
|
uint8_t svc_number = ((uint8_t *)stack[6])[-2];
|
|
|
|
switch (svc_number) {
|
|
|
|
case SVC_ENABLE_IRQ:
|
|
|
|
HAL_NVIC_EnableIRQ(stack[0]);
|
|
|
|
break;
|
|
|
|
case SVC_DISABLE_IRQ:
|
|
|
|
HAL_NVIC_DisableIRQ(stack[0]);
|
|
|
|
break;
|
|
|
|
case SVC_SET_PRIORITY:
|
|
|
|
NVIC_SetPriority(stack[0], stack[1]);
|
|
|
|
break;
|
2020-12-07 03:45:29 +00:00
|
|
|
#ifdef SYSTEM_VIEW
|
|
|
|
case SVC_GET_DWT_CYCCNT:
|
|
|
|
cyccnt_cycles = *DWT_CYCCNT_ADDR;
|
|
|
|
break;
|
|
|
|
#endif
|
2019-12-10 19:12:55 +00:00
|
|
|
default:
|
|
|
|
stack[0] = 0xffffffff;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
__attribute__((naked)) void SVC_Handler(void) {
|
|
|
|
__asm volatile(
|
|
|
|
" tst lr, #4 \n" // Test Bit 3 to see which stack pointer we should
|
|
|
|
// use.
|
|
|
|
" ite eq \n" // Tell the assembler that the nest 2 instructions
|
|
|
|
// are if-then-else
|
|
|
|
" mrseq r0, msp \n" // Make R0 point to main stack pointer
|
|
|
|
" mrsne r0, psp \n" // Make R0 point to process stack pointer
|
|
|
|
" b SVC_C_Handler \n" // Off to C land
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
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);
|