TF-M Reference Manual  1.2.0
TrustedFirmware-M
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
main.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017-2020, Arm Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  *
6  */
7 
8 #include "arch.h"
9 #include "common/tfm_boot_data.h"
10 #include "region.h"
11 #include "spm_func.h"
12 #include "tfm_hal_platform.h"
13 #include "tfm_irq_list.h"
14 #include "tfm_nspm.h"
15 #include "tfm_spm_hal.h"
16 #include "tfm_spm_log.h"
17 #include "tfm_version.h"
18 
19 /*
20  * Avoids the semihosting issue
21  * FixMe: describe 'semihosting issue'
22  */
23 #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050)
24 __asm(" .global __ARM_use_no_argv\n");
25 #endif
26 
27 #ifndef TFM_LVL
28 #error TFM_LVL is not defined!
29 #elif (TFM_LVL != 1)
30 #error Only TFM_LVL 1 is supported for library model!
31 #endif
32 
33 REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base);
34 
35 static int32_t tfm_core_init(void)
36 {
37  size_t i;
38  enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC;
39  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;
41 
42  /* Enables fault handlers */
43  plat_err = tfm_spm_hal_enable_fault_handlers();
44  if (plat_err != TFM_PLAT_ERR_SUCCESS) {
45  return TFM_ERROR_GENERIC;
46  }
47 
48  /* Configures the system reset request properties */
49  plat_err = tfm_spm_hal_system_reset_cfg();
50  if (plat_err != TFM_PLAT_ERR_SUCCESS) {
51  return TFM_ERROR_GENERIC;
52  }
53 
54  /* Configures debug authentication */
55  plat_err = tfm_spm_hal_init_debug();
56  if (plat_err != TFM_PLAT_ERR_SUCCESS) {
57  return TFM_ERROR_GENERIC;
58  }
59 
60  /*
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 
69  /* Performs platform specific initialization */
70  hal_status = tfm_hal_platform_init();
71  if (hal_status != TFM_HAL_SUCCESS) {
72  return TFM_ERROR_GENERIC;
73  }
74 
75  /* Configures architecture-specific coprocessors */
77 
78  SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n");
79 
80  SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL);
81 
83 
85 
86  /* Configures all interrupts to retarget NS state, except for
87  * secure peripherals
88  */
89  plat_err = tfm_spm_hal_nvic_interrupt_target_state_cfg();
90  if (plat_err != TFM_PLAT_ERR_SUCCESS) {
91  return TFM_ERROR_GENERIC;
92  }
93 
94  for (i = 0; i < tfm_core_irq_signals_count; ++i) {
95  plat_err = tfm_spm_hal_set_secure_irq_priority(
96  tfm_core_irq_signals[i].irq_line,
97  tfm_core_irq_signals[i].irq_priority);
98  if (plat_err != TFM_PLAT_ERR_SUCCESS) {
99  return TFM_ERROR_GENERIC;
100  }
101  irq_target_state = tfm_spm_hal_set_irq_target_state(
102  tfm_core_irq_signals[i].irq_line,
103  TFM_IRQ_TARGET_STATE_SECURE);
104  if (irq_target_state != TFM_IRQ_TARGET_STATE_SECURE) {
105  return TFM_ERROR_GENERIC;
106  }
107  }
108 
109  /* Enable secure peripherals interrupts */
110  plat_err = tfm_spm_hal_nvic_interrupt_enable();
111  if (plat_err != TFM_PLAT_ERR_SUCCESS) {
112  return TFM_ERROR_GENERIC;
113  }
114 
115  return TFM_SUCCESS;
116 }
117 
118 int main(void)
119 {
120  /* set Main Stack Pointer limit */
121  tfm_arch_init_secure_msp((uint32_t)&REGION_NAME(Image$$,
122  ARM_LIB_STACK_MSP,
123  $$ZI$$Base));
124 
125  /* Seal the PSP stacks viz ARM_LIB_STACK and TFM_SECURE_STACK */
127 
128  if (tfm_core_init() != TFM_SUCCESS) {
129  tfm_core_panic();
130  }
131  /* Print the TF-M version */
132  SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n");
133 
134  if (tfm_spm_db_init() != SPM_ERR_OK) {
135  tfm_core_panic();
136  }
137 
138 #ifdef CONFIG_TFM_ENABLE_MEMORY_PROTECT
139  if (tfm_spm_hal_setup_isolation_hw() != TFM_PLAT_ERR_SUCCESS) {
140  tfm_core_panic();
141  }
142 #endif /* CONFIG_TFM_ENABLE_MEMORY_PROTECT */
143 
145 
146  REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base)[];
147  uint32_t psp_stack_bottom =
148  (uint32_t)REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base);
149 
150  tfm_arch_set_psplim(psp_stack_bottom);
151 
153  /* Certain systems might refuse to boot altogether if partitions fail
154  * to initialize. This is a placeholder for such an error handler
155  */
156  }
157 
158  /*
159  * Prioritise secure exceptions to avoid NS being able to pre-empt
160  * secure SVC or SecureFault. Do it before PSA API initialization.
161  */
163 
164  /* We close the TFM_SP_CORE_ID partition, because its only purpose is
165  * to be able to pass the state checks for the tests started from secure.
166  */
170 
171 #ifdef TFM_CORE_DEBUG
172  /* Jumps to non-secure code */
173  SPMLOG_DBGMSG("\033[1;34mJumping to non-secure code...\033[0m\r\n");
174 #endif
175 
176  jump_to_ns_code();
177 }
#define SPM_PARTITION_STATE_CLOSED
Definition: spm_func.h:22
enum spm_err_t tfm_spm_db_init(void)
Initialize partition database.
Definition: spm_func.c:1415
void jump_to_ns_code(void)
Jump to non-secure code.
Definition: arch.c:19
__STATIC_INLINE void tfm_arch_set_psplim(uint32_t psplim)
Set PSP limit value.
const struct tfm_core_irq_signal_data_t tfm_core_irq_signals[]
#define SPMLOG_DBGMSG(msg)
Definition: tfm_spm_log.h:42
void tfm_core_validate_boot_data(void)
Validate the content of shared memory area, which stores the shared data between bootloader and runti...
void configure_ns_code(void)
REGION_DECLARE(Image $$, ARM_LIB_STACK_MSP,$$ZI $$Base)
void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
Set the current state of a partition.
Definition: spm_func.c:1297
void tfm_arch_set_secure_exception_priorities(void)
__STATIC_INLINE void tfm_arch_init_secure_msp(uint32_t msplim)
Secure the MSP.
void tfm_core_panic(void)
Definition: utilities.c:11
#define SPMLOG_DBGMSGVAL(msg, val)
Definition: tfm_spm_log.h:41
int main(void)
Definition: main.c:118
#define VERSION_FULLSTR
Definition: tfm_version.h:22
const size_t tfm_core_irq_signals_count
#define SPMLOG_INFMSG(msg)
Definition: tfm_spm_log.h:50
#define TFM_SP_CORE_ID
void tfm_arch_configure_coprocessors(void)
Configure coprocessors.
Definition: arch.c:271
void tfm_spm_seal_psp_stacks(void)
Function to seal the PSP stacks for Function mode.
Definition: spm_func.c:51
enum spm_err_t tfm_spm_partition_init(void)
Execute partition init function.
Definition: spm_func.c:1168
#define TFM_SP_NON_SECURE_ID
#define SPM_PARTITION_STATE_RUNNING
Definition: spm_func.h:18