blob: f52ff643797f6228bc55e270c250f74e8677c16f [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
TTornblom83d96372019-11-19 12:53:16 +01008#include "region.h"
Summer Qin830c5542020-02-14 13:44:20 +08009#include "tfm_internal.h"
10#include "tfm_irq_list.h"
11#include "tfm_nspm.h"
12#include "tfm_spm_hal.h"
13#include "tfm_version.h"
14#include "log/tfm_log.h"
Mingyang Sun7397b4f2020-06-17 15:07:45 +080015#include "spm_func.h"
16#include "spm_partition_defs.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!
28#endif
Edison Aicb0ecf62019-07-10 18:43:51 +080029
Edison Aicb0ecf62019-07-10 18:43:51 +080030#if (TFM_LVL != 1)
31#error Only TFM_LVL 1 is supported for library model!
32#endif
Miklos Balint386b8b52017-11-29 13:12:32 +000033
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +020034REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
35
Summer Qin830c5542020-02-14 13:44:20 +080036static int32_t tfm_core_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +000037{
Mate Toth-Pal4341de02018-10-02 12:55:47 +020038 size_t i;
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020039 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
40 enum irq_target_state_t irq_target_state = TFM_IRQ_TARGET_STATE_SECURE;
Mate Toth-Pal4341de02018-10-02 12:55:47 +020041
Miklos Balint386b8b52017-11-29 13:12:32 +000042 /* Enables fault handlers */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020043 plat_err = tfm_spm_hal_enable_fault_handlers();
44 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
45 return TFM_ERROR_GENERIC;
46 }
Miklos Balint386b8b52017-11-29 13:12:32 +000047
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010048 /* Configures the system reset request properties */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020049 plat_err = tfm_spm_hal_system_reset_cfg();
50 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
51 return TFM_ERROR_GENERIC;
52 }
Marc Moreno Berengue8e0fa7a2018-10-04 18:25:13 +010053
Marc Moreno Berengued584b612018-11-26 11:46:31 +000054 /* Configures debug authentication */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020055 plat_err = tfm_spm_hal_init_debug();
56 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
57 return TFM_ERROR_GENERIC;
58 }
Miklos Balint386b8b52017-11-29 13:12:32 +000059
Jaykumar Pitambarbhai Patel98e6ce42020-01-06 12:42:42 +053060 /*
61 * Access to any peripheral should be performed after programming
62 * the necessary security components such as PPC/SAU.
63 */
64 plat_err = tfm_spm_hal_init_isolation_hw();
65 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
66 return TFM_ERROR_GENERIC;
67 }
68
Andrei Narkevitch5bba54c2019-09-23 14:09:13 -070069 /* Performs platform specific initialization */
70 plat_err = tfm_spm_hal_post_init();
71 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
72 return TFM_ERROR_GENERIC;
73 }
Miklos Balint386b8b52017-11-29 13:12:32 +000074
Ken Liu81f2d5e2019-12-26 11:44:36 +080075 LOG_MSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
Miklos Balint6cbeba62018-04-12 17:31:34 +020076
Miklos Balint386b8b52017-11-29 13:12:32 +000077#ifdef TFM_CORE_DEBUG
Ken Liu81f2d5e2019-12-26 11:44:36 +080078 LOG_MSG("TF-M isolation level is: %d\r\n", TFM_LVL);
Miklos Balint386b8b52017-11-29 13:12:32 +000079#endif
80
Tamas Ban9ff535b2018-09-18 08:15:18 +010081 tfm_core_validate_boot_data();
82
Miklos Balint386b8b52017-11-29 13:12:32 +000083 configure_ns_code();
84
85 /* Configures all interrupts to retarget NS state, except for
86 * secure peripherals
87 */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020088 plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
89 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
90 return TFM_ERROR_GENERIC;
91 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +020092
93 for (i = 0; i < tfm_core_irq_signals_count; ++i) {
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020094 plat_err = tfm_spm_hal_set_secure_irq_priority(
Mate Toth-Pal4341de02018-10-02 12:55:47 +020095 tfm_core_irq_signals[i].irq_line,
96 tfm_core_irq_signals[i].irq_priority);
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +020097 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
98 return TFM_ERROR_GENERIC;
99 }
100 irq_target_state = tfm_spm_hal_set_irq_target_state(
101 tfm_core_irq_signals[i].irq_line,
102 TFM_IRQ_TARGET_STATE_SECURE);
103 if (irq_target_state != TFM_IRQ_TARGET_STATE_SECURE) {
104 return TFM_ERROR_GENERIC;
105 }
Mate Toth-Pal4341de02018-10-02 12:55:47 +0200106 }
107
Miklos Balint386b8b52017-11-29 13:12:32 +0000108 /* Enable secure peripherals interrupts */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200109 plat_err = tfm_spm_hal_nvic_interrupt_enable();
110 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
111 return TFM_ERROR_GENERIC;
112 }
Miklos Balint386b8b52017-11-29 13:12:32 +0000113
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200114 return TFM_SUCCESS;
Miklos Balint386b8b52017-11-29 13:12:32 +0000115}
116
Edison Aid87f07b2019-07-22 18:50:24 +0800117static int32_t tfm_core_set_secure_exception_priorities(void)
Miklos Balintace4c3f2018-07-30 12:31:15 +0200118{
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200119 enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR;
120
David Hu4e165602019-06-12 18:38:31 +0800121 tfm_arch_prioritize_secure_exception();
Miklos Balintace4c3f2018-07-30 12:31:15 +0200122
Mate Toth-Pal3e2ebd02019-05-07 14:22:16 +0200123 /* Explicitly set Secure SVC priority to highest */
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200124 plat_err = tfm_spm_hal_set_secure_irq_priority(SVCall_IRQn, 0);
125 if (plat_err != TFM_PLAT_ERR_SUCCESS) {
126 return TFM_ERROR_GENERIC;
127 }
Miklos Balintace4c3f2018-07-30 12:31:15 +0200128
Summer Qin2b8ab7e2020-02-18 13:58:58 +0800129 tfm_arch_set_pendsv_priority();
Edison Aie5111d92019-07-22 16:08:27 +0800130
131 return TFM_SUCCESS;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200132}
133
Miklos Balint386b8b52017-11-29 13:12:32 +0000134int main(void)
135{
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200136 /* set Main Stack Pointer limit */
David Huf363fe92019-07-02 13:03:30 +0800137 tfm_arch_set_msplim((uint32_t)&REGION_NAME(Image$$, ARM_LIB_STACK_MSP,
138 $$ZI$$Base));
Mate Toth-Pal6bb416a2019-05-07 16:23:55 +0200139
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200140 if (tfm_core_init() != TFM_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800141 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000142 }
Soby Mathewc64adbc2020-03-11 12:33:44 +0000143 /* Print the TF-M version */
144 LOG_MSG("\033[1;34mBooting TFM v%d.%d %s\033[0m\r\n",
145 VERSION_MAJOR, VERSION_MINOR, VERSION_STRING);
Miklos Balint386b8b52017-11-29 13:12:32 +0000146
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000147 if (tfm_spm_db_init() != SPM_ERR_OK) {
Edison Ai9059ea02019-11-28 13:46:14 +0800148 tfm_core_panic();
Hugues de Valon4bf875b2019-02-19 14:53:49 +0000149 }
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200150
Edison Ai1dfd7b12020-02-23 14:16:08 +0800151#ifdef CONFIG_TFM_ENABLE_MEMORY_PROTECT
Edison Aic1b10902019-08-26 10:34:19 +0800152 if (tfm_spm_hal_setup_isolation_hw() != TFM_PLAT_ERR_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800153 tfm_core_panic();
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200154 }
Edison Ai1dfd7b12020-02-23 14:16:08 +0800155#endif /* CONFIG_TFM_ENABLE_MEMORY_PROTECT */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200156
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100157 tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_RUNNING);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100158
TTornblomc640e072019-06-14 14:33:51 +0200159 REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base)[];
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200160 uint32_t psp_stack_bottom =
161 (uint32_t)REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
Miklos Balint386b8b52017-11-29 13:12:32 +0000162
David Hue05b6a62019-06-12 18:45:28 +0800163 tfm_arch_set_psplim(psp_stack_bottom);
Miklos Balint386b8b52017-11-29 13:12:32 +0000164
Miklos Balint6a139ae2018-04-04 19:44:37 +0200165 if (tfm_spm_partition_init() != SPM_ERR_OK) {
166 /* Certain systems might refuse to boot altogether if partitions fail
167 * to initialize. This is a placeholder for such an error handler
168 */
169 }
170
Ken Liu96714b32019-04-08 15:10:39 +0800171 /*
172 * Prioritise secure exceptions to avoid NS being able to pre-empt
173 * secure SVC or SecureFault. Do it before PSA API initialization.
174 */
Edison Aic1b10902019-08-26 10:34:19 +0800175 if (tfm_core_set_secure_exception_priorities() != TFM_SUCCESS) {
Edison Ai9059ea02019-11-28 13:46:14 +0800176 tfm_core_panic();
Mate Toth-Pal5d3ae082019-07-10 16:14:14 +0200177 }
Ken Liu96714b32019-04-08 15:10:39 +0800178
Edison Ai4d66dc32019-02-18 17:58:49 +0800179 /* We close the TFM_SP_CORE_ID partition, because its only purpose is
180 * to be able to pass the state checks for the tests started from secure.
181 */
182 tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_CLOSED);
183 tfm_spm_partition_set_state(TFM_SP_NON_SECURE_ID,
184 SPM_PARTITION_STATE_RUNNING);
Edison Ai4dcae6f2019-03-18 10:13:47 +0800185
186#ifdef TFM_CORE_DEBUG
187 /* Jumps to non-secure code */
Ken Liu81f2d5e2019-12-26 11:44:36 +0800188 LOG_MSG("\033[1;34mJumping to non-secure code...\033[0m\r\n");
Edison Ai4dcae6f2019-03-18 10:13:47 +0800189#endif
190
191 jump_to_ns_code();
Miklos Balint386b8b52017-11-29 13:12:32 +0000192}