blob: 39e6c47748709697f8c4c4cc7d33470d7c583799 [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +05302 * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
Miklos Balint386b8b52017-11-29 13:12:32 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Summer Qin9c1fba12020-08-12 15:49:12 +08008#include "common/tfm_boot_data.h"
Summer Qinf993cd42020-08-12 16:55:17 +08009#include "log/tfm_log.h"
TTornblom83d96372019-11-19 12:53:16 +010010#include "region.h"
Summer Qinf993cd42020-08-12 16:55:17 +080011#include "spm_ipc.h"
Summer Qin0eb7c912020-08-19 16:08:50 +080012#include "tfm_hal_platform.h"
Summer Qin830c5542020-02-14 13:44:20 +080013#include "tfm_irq_list.h"
14#include "tfm_nspm.h"
15#include "tfm_spm_hal.h"
16#include "tfm_version.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000017
Miklos Balint386b8b52017-11-29 13:12:32 +000018/*
19 * Avoids the semihosting issue
20 * FixMe: describe 'semihosting issue'
21 */
22#if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
23__asm(" .global __ARM_use_no_argv\n");
24#endif
25
26#ifndef TFM_LVL
27#error TFM_LVL is not defined!
Summer Qinf993cd42020-08-12 16:55:17 +080028#elif (TFM_LVL != 1) && (TFM_LVL != 2)
Edison Aicb0ecf62019-07-10 18:43:51 +080029#error Only TFM_LVL 1 and 2 are supported for IPC model!
30#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000031
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +020032REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
33
Summer Qin830c5542020-02-14 13:44:20 +080034static int32_t tfm_core_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +000035{
Mate Toth-Pal4341de02018-10-02 12:55:47 +020036 size_t i;
Summer Qin0eb7c912020-08-19 16:08:50 +080037 enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC;
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020038 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
39 enum irq_target_state_t irq_target_state = TFM_IRQ_TARGET_STATE_SECURE;
Mate Toth-Pal4341de02018-10-02 12:55:47 +020040
Miklos Balint386b8b52017-11-29 13:12:32 +000041 /* Enables fault handlers */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020042 plat_err = tfm_spm_hal_enable_fault_handlers();
43 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
44 return TFM_ERROR_GENERIC;
45 }
Miklos Balint386b8b52017-11-29 13:12:32 +000046
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010047 /* Configures the system reset request properties */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020048 plat_err = tfm_spm_hal_system_reset_cfg();
49 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
50 return TFM_ERROR_GENERIC;
51 }
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010052
Marc Moreno Berengued584b612018-11-26 11:46:31 +000053 /* Configures debug authentication */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020054 plat_err = tfm_spm_hal_init_debug();
55 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
56 return TFM_ERROR_GENERIC;
57 }
Miklos Balint386b8b52017-11-29 13:12:32 +000058
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053059 /*
60 * Access to any peripheral should be performed after programming
61 * the necessary security components such as PPC/SAU.
62 */
63 plat_err = tfm_spm_hal_init_isolation_hw();
64 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
65 return TFM_ERROR_GENERIC;
66 }
67
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070068 /* Performs platform specific initialization */
Summer Qin0eb7c912020-08-19 16:08:50 +080069 hal_status = tfm_hal_platform_init();
70 if (hal_status != TFM_HAL_SUCCESS) {
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070071 return TFM_ERROR_GENERIC;
72 }
Miklos Balint386b8b52017-11-29 13:12:32 +000073
Jamie Fox45587672020-08-17 18:31:14 +010074 /* Configures architecture-specific coprocessors */
75 tfm_arch_configure_coprocessors();
76
Ken Liu81f2d5e2019-12-26 11:44:36 +080077 LOG_MSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
Miklos Balint6cbeba62018-04-12 17:31:34 +020078
Miklos Balint386b8b52017-11-29 13:12:32 +000079#ifdef TFM_CORE_DEBUG
Ken Liu81f2d5e2019-12-26 11:44:36 +080080 LOG_MSG("TF-M isolation level is: %d\r\n", TFM_LVL);
Miklos Balint386b8b52017-11-29 13:12:32 +000081#endif
82
Tamas Ban9ff535b2018-09-18 08:15:18 +010083 tfm_core_validate_boot_data();
84
Miklos Balint386b8b52017-11-29 13:12:32 +000085 configure_ns_code();
86
87 /* Configures all interrupts to retarget NS state, except for
88 * secure peripherals
89 */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020090 plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
91 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
92 return TFM_ERROR_GENERIC;
93 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +020094
95 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020096 plat_err = tfm_spm_hal_set_secure_irq_priority(
Mate Toth-Pal4341de02018-10-02 12:55:47 +020097 tfm_core_irq_signals[i].irq_line,
98 tfm_core_irq_signals[i].irq_priority);
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020099 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
100 return TFM_ERROR_GENERIC;
101 }
102 irq_target_state = tfm_spm_hal_set_irq_target_state(
103 tfm_core_irq_signals[i].irq_line,
104 TFM_IRQ_TARGET_STATE_SECURE);
105 if (irq_target_state != TFM_IRQ_TARGET_STATE_SECURE) {
106 return TFM_ERROR_GENERIC;
107 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200108 }
109
Miklos Balint386b8b52017-11-29 13:12:32 +0000110 /* Enable secure peripherals interrupts */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200111 plat_err = tfm_spm_hal_nvic_interrupt_enable();
112 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
113 return TFM_ERROR_GENERIC;
114 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000115
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200116 return TFM_SUCCESS;
Miklos Balint386b8b52017-11-29 13:12:32 +0000117}
118
Edison Aid87f07b2019-07-22 18:50:24 +0800119static int32_t tfm_core_set_secure_exception_priorities(void)
Miklos Balintace4c3f2018-07-30 12:31:15 +0200120{
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200121 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
122
David Hu4e165602019-06-12 18:38:31 +0800123 tfm_arch_prioritize_secure_exception();
Miklos Balintace4c3f2018-07-30 12:31:15 +0200124
Mate Toth-Pal3e2ebd02019-05-07 14:22:16 +0200125 /* Explicitly set Secure SVC priority to highest */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200126 plat_err = tfm_spm_hal_set_secure_irq_priority(SVCall_IRQn, 0);
127 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
128 return TFM_ERROR_GENERIC;
129 }
Miklos Balintace4c3f2018-07-30 12:31:15 +0200130
Summer Qin2b8ab7e2020-02-18 13:58:58 +0800131 tfm_arch_set_pendsv_priority();
Edison Aie5111d92019-07-22 16:08:27 +0800132
133 return TFM_SUCCESS;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200134}
135
Miklos Balint386b8b52017-11-29 13:12:32 +0000136int main(void)
137{
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200138 /* set Main Stack Pointer limit */
David Huf363fe92019-07-02 13:03:30 +0800139 tfm_arch_set_msplim((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK_MSP,
140 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200141
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200142 if (tfm_core_init() != TFM_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800143 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000144 }
Soby Mathewc64adbc2020-03-11 12:33:44 +0000145 /* Print the TF-M version */
146 LOG_MSG("\033[1;34mBooting TFM v%d.%d %s\033[0m\r\n",
147 VERSION_MAJOR, VERSION_MINOR, VERSION_STRING);
Miklos Balint386b8b52017-11-29 13:12:32 +0000148
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000149 if (tfm_spm_db_init() != SPM_ERR_OK) {
Edison Ai9059ea02019-11-28 13:46:14 +0800150 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000151 }
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200152
Edison Ai1dfd7b12020-02-23 14:16:08 +0800153#ifdef CONFIG_TFM_ENABLE_MEMORY_PROTECT
Edison Aic1b10902019-08-26 10:34:19 +0800154 if (tfm_spm_hal_setup_isolation_hw() != TFM_PLAT_ERR_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800155 tfm_core_panic();
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200156 }
Edison Ai1dfd7b12020-02-23 14:16:08 +0800157#endif /* CONFIG_TFM_ENABLE_MEMORY_PROTECT */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200158
Edison Ai4d66dc32019-02-18 17:58:49 +0800159 /*
160 * Prioritise secure exceptions to avoid NS being able to pre-empt
161 * secure SVC or SecureFault. Do it before PSA API initialization.
162 */
Edison Aic1b10902019-08-26 10:34:19 +0800163 if (tfm_core_set_secure_exception_priorities() != TFM_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800164 tfm_core_panic();
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200165 }
Ken Liu490281d2019-12-30 15:55:26 +0800166
167 /* Move to handler mode for further SPM initialization. */
168 tfm_core_handler_mode();
Miklos Balint386b8b52017-11-29 13:12:32 +0000169}