Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 1 | /* |
Ken Liu | 55ba01f | 2021-01-20 17:34:50 +0800 | [diff] [blame^] | 2 | * Copyright (c) 2017-2021, Arm Limited. All rights reserved. |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
Summer Qin | 9c1fba1 | 2020-08-12 15:49:12 +0800 | [diff] [blame] | 8 | #include "arch.h" |
Ken Liu | 55ba01f | 2021-01-20 17:34:50 +0800 | [diff] [blame^] | 9 | #include "ffm/tfm_boot_data.h" |
TTornblom | 83d9637 | 2019-11-19 12:53:16 +0100 | [diff] [blame] | 10 | #include "region.h" |
Summer Qin | f993cd4 | 2020-08-12 16:55:17 +0800 | [diff] [blame] | 11 | #include "spm_func.h" |
Mingyang Sun | 9763dee | 2020-12-07 10:45:17 +0800 | [diff] [blame] | 12 | #include "tfm_hal_defs.h" |
Summer Qin | 0eb7c91 | 2020-08-19 16:08:50 +0800 | [diff] [blame] | 13 | #include "tfm_hal_platform.h" |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 14 | #include "tfm_irq_list.h" |
| 15 | #include "tfm_nspm.h" |
| 16 | #include "tfm_spm_hal.h" |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 17 | #include "tfm_spm_log.h" |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 18 | #include "tfm_version.h" |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 19 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 20 | /* |
| 21 | * Avoids the semihosting issue |
| 22 | * FixMe: describe 'semihosting issue' |
| 23 | */ |
| 24 | #if defined(__ARMCC_VERSION) && (__ARMCC_VERSION >= 6010050) |
| 25 | __asm(" .global __ARM_use_no_argv\n"); |
| 26 | #endif |
| 27 | |
| 28 | #ifndef TFM_LVL |
| 29 | #error TFM_LVL is not defined! |
Summer Qin | f993cd4 | 2020-08-12 16:55:17 +0800 | [diff] [blame] | 30 | #elif (TFM_LVL != 1) |
Edison Ai | cb0ecf6 | 2019-07-10 18:43:51 +0800 | [diff] [blame] | 31 | #error Only TFM_LVL 1 is supported for library model! |
| 32 | #endif |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 33 | |
Mate Toth-Pal | 6bb416a | 2019-05-07 16:23:55 +0200 | [diff] [blame] | 34 | REGION_DECLARE(Image$$, ARM_LIB_STACK_MSP, $$ZI$$Base); |
| 35 | |
Summer Qin | 830c554 | 2020-02-14 13:44:20 +0800 | [diff] [blame] | 36 | static int32_t tfm_core_init(void) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 37 | { |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 38 | size_t i; |
Summer Qin | 0eb7c91 | 2020-08-19 16:08:50 +0800 | [diff] [blame] | 39 | enum tfm_hal_status_t hal_status = TFM_HAL_ERROR_GENERIC; |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 40 | enum tfm_plat_err_t plat_err = TFM_PLAT_ERR_SYSTEM_ERR; |
| 41 | enum irq_target_state_t irq_target_state = TFM_IRQ_TARGET_STATE_SECURE; |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 42 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 43 | /* Enables fault handlers */ |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 44 | plat_err = tfm_spm_hal_enable_fault_handlers(); |
| 45 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 46 | return TFM_ERROR_GENERIC; |
| 47 | } |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 48 | |
Marc Moreno Berengue | 8e0fa7a | 2018-10-04 18:25:13 +0100 | [diff] [blame] | 49 | /* Configures the system reset request properties */ |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 50 | plat_err = tfm_spm_hal_system_reset_cfg(); |
| 51 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 52 | return TFM_ERROR_GENERIC; |
| 53 | } |
Marc Moreno Berengue | 8e0fa7a | 2018-10-04 18:25:13 +0100 | [diff] [blame] | 54 | |
Marc Moreno Berengue | d584b61 | 2018-11-26 11:46:31 +0000 | [diff] [blame] | 55 | /* Configures debug authentication */ |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 56 | plat_err = tfm_spm_hal_init_debug(); |
| 57 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 58 | return TFM_ERROR_GENERIC; |
| 59 | } |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 60 | |
Jaykumar Pitambarbhai Patel | 98e6ce4 | 2020-01-06 12:42:42 +0530 | [diff] [blame] | 61 | /* |
| 62 | * Access to any peripheral should be performed after programming |
| 63 | * the necessary security components such as PPC/SAU. |
| 64 | */ |
Mingyang Sun | 9763dee | 2020-12-07 10:45:17 +0800 | [diff] [blame] | 65 | hal_status = tfm_hal_set_up_static_boundaries(); |
| 66 | if (hal_status != TFM_HAL_SUCCESS) { |
Jaykumar Pitambarbhai Patel | 98e6ce4 | 2020-01-06 12:42:42 +0530 | [diff] [blame] | 67 | return TFM_ERROR_GENERIC; |
| 68 | } |
| 69 | |
Andrei Narkevitch | 5bba54c | 2019-09-23 14:09:13 -0700 | [diff] [blame] | 70 | /* Performs platform specific initialization */ |
Summer Qin | 0eb7c91 | 2020-08-19 16:08:50 +0800 | [diff] [blame] | 71 | hal_status = tfm_hal_platform_init(); |
| 72 | if (hal_status != TFM_HAL_SUCCESS) { |
Andrei Narkevitch | 5bba54c | 2019-09-23 14:09:13 -0700 | [diff] [blame] | 73 | return TFM_ERROR_GENERIC; |
| 74 | } |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 75 | |
Jamie Fox | 4558767 | 2020-08-17 18:31:14 +0100 | [diff] [blame] | 76 | /* Configures architecture-specific coprocessors */ |
| 77 | tfm_arch_configure_coprocessors(); |
| 78 | |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 79 | SPMLOG_INFMSG("\033[1;34m[Sec Thread] Secure image initializing!\033[0m\r\n"); |
Miklos Balint | 6cbeba6 | 2018-04-12 17:31:34 +0200 | [diff] [blame] | 80 | |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 81 | SPMLOG_DBGMSGVAL("TF-M isolation level is: ", TFM_LVL); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 82 | |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 83 | tfm_core_validate_boot_data(); |
| 84 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 85 | configure_ns_code(); |
| 86 | |
| 87 | /* Configures all interrupts to retarget NS state, except for |
| 88 | * secure peripherals |
| 89 | */ |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 90 | 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-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 94 | |
| 95 | for (i = 0; i < tfm_core_irq_signals_count; ++i) { |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 96 | plat_err = tfm_spm_hal_set_secure_irq_priority( |
Mate Toth-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 97 | tfm_core_irq_signals[i].irq_line, |
| 98 | tfm_core_irq_signals[i].irq_priority); |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 99 | 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-Pal | 4341de0 | 2018-10-02 12:55:47 +0200 | [diff] [blame] | 108 | } |
| 109 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 110 | /* Enable secure peripherals interrupts */ |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 111 | plat_err = tfm_spm_hal_nvic_interrupt_enable(); |
| 112 | if (plat_err != TFM_PLAT_ERR_SUCCESS) { |
| 113 | return TFM_ERROR_GENERIC; |
| 114 | } |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 115 | |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 116 | return TFM_SUCCESS; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | int main(void) |
| 120 | { |
Mate Toth-Pal | 6bb416a | 2019-05-07 16:23:55 +0200 | [diff] [blame] | 121 | /* set Main Stack Pointer limit */ |
Ken Liu | 05e13ba | 2020-07-25 10:31:33 +0800 | [diff] [blame] | 122 | tfm_arch_init_secure_msp((uint32_t)®ION_NAME(Image$$, |
| 123 | ARM_LIB_STACK_MSP, |
| 124 | $$ZI$$Base)); |
Mate Toth-Pal | 6bb416a | 2019-05-07 16:23:55 +0200 | [diff] [blame] | 125 | |
Soby Mathew | 960521a | 2020-09-29 12:48:50 +0100 | [diff] [blame] | 126 | /* Seal the PSP stacks viz ARM_LIB_STACK and TFM_SECURE_STACK */ |
| 127 | tfm_spm_seal_psp_stacks(); |
| 128 | |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 129 | if (tfm_core_init() != TFM_SUCCESS) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 130 | tfm_core_panic(); |
Hugues de Valon | 4bf875b | 2019-02-19 14:53:49 +0000 | [diff] [blame] | 131 | } |
Soby Mathew | c64adbc | 2020-03-11 12:33:44 +0000 | [diff] [blame] | 132 | /* Print the TF-M version */ |
Shawn Shan | 45578e9 | 2020-10-19 17:50:02 +0800 | [diff] [blame] | 133 | SPMLOG_INFMSG("\033[1;34mBooting TFM v"VERSION_FULLSTR"\033[0m\r\n"); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 134 | |
Hugues de Valon | 4bf875b | 2019-02-19 14:53:49 +0000 | [diff] [blame] | 135 | if (tfm_spm_db_init() != SPM_ERR_OK) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 136 | tfm_core_panic(); |
Hugues de Valon | 4bf875b | 2019-02-19 14:53:49 +0000 | [diff] [blame] | 137 | } |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 138 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 139 | tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_RUNNING); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 140 | |
TTornblom | c640e07 | 2019-06-14 14:33:51 +0200 | [diff] [blame] | 141 | REGION_DECLARE(Image$$, ARM_LIB_STACK, $$ZI$$Base)[]; |
Mate Toth-Pal | 5d3ae08 | 2019-07-10 16:14:14 +0200 | [diff] [blame] | 142 | uint32_t psp_stack_bottom = |
| 143 | (uint32_t)REGION_NAME(Image$$, ARM_LIB_STACK, $$ZI$$Base); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 144 | |
David Hu | e05b6a6 | 2019-06-12 18:45:28 +0800 | [diff] [blame] | 145 | tfm_arch_set_psplim(psp_stack_bottom); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 146 | |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 147 | if (tfm_spm_partition_init() != SPM_ERR_OK) { |
| 148 | /* Certain systems might refuse to boot altogether if partitions fail |
| 149 | * to initialize. This is a placeholder for such an error handler |
| 150 | */ |
| 151 | } |
| 152 | |
Ken Liu | 96714b3 | 2019-04-08 15:10:39 +0800 | [diff] [blame] | 153 | /* |
| 154 | * Prioritise secure exceptions to avoid NS being able to pre-empt |
| 155 | * secure SVC or SecureFault. Do it before PSA API initialization. |
| 156 | */ |
Ken Liu | 50e2109 | 2020-10-14 16:42:15 +0800 | [diff] [blame] | 157 | tfm_arch_set_secure_exception_priorities(); |
Ken Liu | 96714b3 | 2019-04-08 15:10:39 +0800 | [diff] [blame] | 158 | |
Edison Ai | 4d66dc3 | 2019-02-18 17:58:49 +0800 | [diff] [blame] | 159 | /* We close the TFM_SP_CORE_ID partition, because its only purpose is |
| 160 | * to be able to pass the state checks for the tests started from secure. |
| 161 | */ |
| 162 | tfm_spm_partition_set_state(TFM_SP_CORE_ID, SPM_PARTITION_STATE_CLOSED); |
| 163 | tfm_spm_partition_set_state(TFM_SP_NON_SECURE_ID, |
| 164 | SPM_PARTITION_STATE_RUNNING); |
Edison Ai | 4dcae6f | 2019-03-18 10:13:47 +0800 | [diff] [blame] | 165 | |
| 166 | #ifdef TFM_CORE_DEBUG |
| 167 | /* Jumps to non-secure code */ |
Shawn Shan | f5471ba | 2020-09-17 17:34:50 +0800 | [diff] [blame] | 168 | SPMLOG_DBGMSG("\033[1;34mJumping to non-secure code...\033[0m\r\n"); |
Edison Ai | 4dcae6f | 2019-03-18 10:13:47 +0800 | [diff] [blame] | 169 | #endif |
| 170 | |
| 171 | jump_to_ns_code(); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 172 | } |