Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 1 | /* |
Tamas Ban | 8bd24b7 | 2019-02-19 12:13:13 +0000 | [diff] [blame] | 2 | * Copyright (c) 2017-2019, 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 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 8 | /* This file contains the APIs exported by the SPM to tfm core */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 9 | |
| 10 | #include <stdio.h> |
Mate Toth-Pal | 7345a4b | 2018-03-08 16:10:28 +0100 | [diff] [blame] | 11 | #include <string.h> |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 12 | #include "spm_api.h" |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 13 | #include "platform/include/tfm_spm_hal.h" |
Tamas Ban | 8bd24b7 | 2019-02-19 12:13:13 +0000 | [diff] [blame] | 14 | #include "tfm_memory_utils.h" |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 15 | #include "spm_db_setup.h" |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 16 | #include "tfm_internal.h" |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 17 | #include "tfm_api.h" |
Mate Toth-Pal | ce61afa | 2018-08-03 13:51:01 +0200 | [diff] [blame] | 18 | #include "tfm_nspm.h" |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 19 | #include "secure_fw/core/tfm_core.h" |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 20 | #include "tfm_peripherals_def.h" |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 21 | #include "spm_partition_defs.h" |
| 22 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 23 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 24 | struct spm_partition_db_t g_spm_partition_db = {0,}; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 25 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 26 | typedef enum { |
| 27 | TFM_INIT_FAILURE, |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 28 | } sp_error_type_t; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 29 | |
| 30 | /* |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 31 | * This function is called when a secure partition causes an error. |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 32 | * In case of an error in the error handling, a non-zero value have to be |
| 33 | * returned. |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 34 | */ |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 35 | #ifndef TFM_PSA_API |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 36 | static void tfm_spm_partition_err_handler( |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 37 | const struct spm_partition_desc_t *partition, |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 38 | sp_error_type_t err_type, |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 39 | int32_t err_code) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 40 | { |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 41 | #ifdef TFM_CORE_DEBUG |
| 42 | if (err_type == TFM_INIT_FAILURE) { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 43 | printf("Partition init failed for partition id 0x%08X\r\n", |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 44 | partition->static_data.partition_id); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 45 | } else { |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 46 | printf( |
| 47 | "Unknown partition error %d (code: %d) for partition id 0x%08X\r\n", |
| 48 | err_type, err_code, partition->static_data.partition_id); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 49 | } |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 50 | #else |
| 51 | (void)err_type; |
| 52 | (void)err_code; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 53 | #endif |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 54 | tfm_spm_partition_set_state(partition->static_data.partition_id, |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 55 | SPM_PARTITION_STATE_CLOSED); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 56 | } |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 57 | #endif /* !defined(TFM_PSA_API) */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 58 | |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 59 | /* |
| 60 | * This function prevents name clashes between the variable names accessibles in |
| 61 | * the scope of where tfm_partition_list.inc is included and the varaible names |
| 62 | * defined inside tfm_partition_list.inc file. |
| 63 | */ |
| 64 | static inline enum spm_err_t add_user_defined_partitions(void) { |
| 65 | #include "secure_fw/services/tfm_partition_list.inc" |
| 66 | |
| 67 | return SPM_ERR_OK; |
| 68 | } |
| 69 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 70 | uint32_t get_partition_idx(uint32_t partition_id) |
| 71 | { |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 72 | uint32_t i; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 73 | |
| 74 | if (partition_id == INVALID_PARTITION_ID) { |
| 75 | return SPM_INVALID_PARTITION_IDX; |
| 76 | } |
| 77 | |
| 78 | for (i = 0; i < g_spm_partition_db.partition_count; ++i) { |
| 79 | if (g_spm_partition_db.partitions[i].static_data.partition_id == |
| 80 | partition_id) { |
| 81 | return i; |
| 82 | } |
| 83 | } |
| 84 | return SPM_INVALID_PARTITION_IDX; |
| 85 | } |
| 86 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 87 | enum spm_err_t tfm_spm_db_init(void) |
| 88 | { |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 89 | struct spm_partition_desc_t *part_ptr; |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 90 | enum spm_err_t err; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 91 | |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 92 | (void)tfm_memset (&g_spm_partition_db, 0, sizeof(g_spm_partition_db)); |
Mate Toth-Pal | 7345a4b | 2018-03-08 16:10:28 +0100 | [diff] [blame] | 93 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 94 | /* This function initialises partition db */ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 95 | g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX; |
| 96 | g_spm_partition_db.partition_count = 0; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 97 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 98 | /* There are a few partitions that are used by TF-M internally. |
| 99 | * These are explicitly added to the partition db here. |
| 100 | */ |
| 101 | |
| 102 | /* For the non secure Execution environment */ |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 103 | #if (TFM_LVL != 1) || defined(TFM_PSA_API) |
Tamas Ban | 56ef302 | 2018-09-13 23:49:16 +0100 | [diff] [blame] | 104 | extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[]; |
| 105 | extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit[]; |
| 106 | uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base; |
| 107 | uint32_t psp_stack_top = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Limit; |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 108 | #endif |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 109 | if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) { |
| 110 | return SPM_ERR_INVALID_CONFIG; |
| 111 | } |
| 112 | part_ptr = &(g_spm_partition_db.partitions[ |
| 113 | g_spm_partition_db.partition_count]); |
| 114 | part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID; |
Edison Ai | 4dcae6f | 2019-03-18 10:13:47 +0800 | [diff] [blame] | 115 | #if TFM_PSA_API |
| 116 | part_ptr->static_data.partition_flags = SPM_PART_FLAG_APP_ROT | |
| 117 | SPM_PART_FLAG_IPC; |
| 118 | part_ptr->static_data.partition_priority = TFM_PRIORITY_LOW; |
| 119 | part_ptr->static_data.partition_init = tfm_nspm_thread_entry; |
| 120 | #else |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 121 | part_ptr->static_data.partition_flags = 0; |
Edison Ai | 4dcae6f | 2019-03-18 10:13:47 +0800 | [diff] [blame] | 122 | #endif |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 123 | |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 124 | #if (TFM_LVL != 1) || defined(TFM_PSA_API) |
Tamas Ban | 56ef302 | 2018-09-13 23:49:16 +0100 | [diff] [blame] | 125 | part_ptr->memory_data.stack_bottom = psp_stack_bottom; |
| 126 | part_ptr->memory_data.stack_top = psp_stack_top; |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 127 | /* Since RW, ZI and stack are configured as one MPU region, configure |
Tamas Ban | 56ef302 | 2018-09-13 23:49:16 +0100 | [diff] [blame] | 128 | * RW start address to psp_stack_bottom to get RW access to stack |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 129 | */ |
Tamas Ban | 56ef302 | 2018-09-13 23:49:16 +0100 | [diff] [blame] | 130 | part_ptr->memory_data.rw_start = psp_stack_bottom; |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 131 | #endif |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 132 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 133 | part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT; |
Miklos Balint | 12735bc | 2018-08-01 15:45:18 +0200 | [diff] [blame] | 134 | tfm_nspm_configure_clients(); |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 135 | ++g_spm_partition_db.partition_count; |
| 136 | |
| 137 | /* For the TF-M core environment itself */ |
| 138 | if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) { |
| 139 | return SPM_ERR_INVALID_CONFIG; |
| 140 | } |
| 141 | part_ptr = &(g_spm_partition_db.partitions[ |
| 142 | g_spm_partition_db.partition_count]); |
| 143 | part_ptr->static_data.partition_id = TFM_SP_CORE_ID; |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 144 | part_ptr->static_data.partition_flags = |
Edison Ai | bb614aa | 2018-11-21 15:15:00 +0800 | [diff] [blame] | 145 | SPM_PART_FLAG_APP_ROT | SPM_PART_FLAG_PSA_ROT; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 146 | part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT; |
| 147 | ++g_spm_partition_db.partition_count; |
| 148 | |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 149 | err = add_user_defined_partitions(); |
| 150 | if (err != SPM_ERR_OK) { |
| 151 | return err; |
| 152 | } |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 153 | |
Mate Toth-Pal | 7345a4b | 2018-03-08 16:10:28 +0100 | [diff] [blame] | 154 | g_spm_partition_db.is_init = 1; |
| 155 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 156 | return SPM_ERR_OK; |
| 157 | } |
| 158 | |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 159 | #ifndef TFM_PSA_API |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 160 | enum spm_err_t tfm_spm_partition_init(void) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 161 | { |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 162 | struct spm_partition_desc_t *part; |
Miklos Balint | ace4c3f | 2018-07-30 12:31:15 +0200 | [diff] [blame] | 163 | struct tfm_sfn_req_s desc; |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 164 | int32_t args[4] = {0}; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 165 | int32_t fail_cnt = 0; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 166 | uint32_t idx; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 167 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 168 | /* Call the init function for each partition */ |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 169 | for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) { |
| 170 | part = &g_spm_partition_db.partitions[idx]; |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 171 | tfm_spm_hal_configure_default_isolation(part->platform_data); |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 172 | if (part->static_data.partition_init == NULL) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 173 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 174 | tfm_spm_partition_set_caller_partition_idx(idx, |
| 175 | SPM_INVALID_PARTITION_IDX); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 176 | } else { |
Miklos Balint | ace4c3f | 2018-07-30 12:31:15 +0200 | [diff] [blame] | 177 | int32_t res; |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 178 | |
| 179 | desc.args = args; |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 180 | desc.ns_caller = 0U; |
Mate Toth-Pal | b8ce0dd | 2018-07-25 10:18:34 +0200 | [diff] [blame] | 181 | desc.iovec_api = TFM_SFN_API_IOVEC; |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 182 | desc.sfn = (sfn_t)part->static_data.partition_init; |
| 183 | desc.sp_id = part->static_data.partition_id; |
Miklos Balint | ace4c3f | 2018-07-30 12:31:15 +0200 | [diff] [blame] | 184 | res = tfm_core_sfn_request(&desc); |
| 185 | if (res == TFM_SUCCESS) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 186 | tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 187 | } else { |
Miklos Balint | ace4c3f | 2018-07-30 12:31:15 +0200 | [diff] [blame] | 188 | tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 189 | fail_cnt++; |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 194 | tfm_secure_api_init_done(); |
| 195 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 196 | if (fail_cnt == 0) { |
| 197 | return SPM_ERR_OK; |
| 198 | } else { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 199 | return SPM_ERR_PARTITION_NOT_AVAILABLE; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 200 | } |
| 201 | } |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 202 | #endif /* !defined(TFM_PSA_API) */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 203 | |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 204 | #if (TFM_LVL != 1) || defined(TFM_PSA_API) |
Summer Qin | d00e4db | 2019-05-09 18:03:52 +0800 | [diff] [blame] | 205 | uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx) |
| 206 | { |
| 207 | return g_spm_partition_db.partitions[partition_idx]. |
| 208 | memory_data.stack_bottom; |
| 209 | } |
| 210 | |
| 211 | uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx) |
| 212 | { |
| 213 | return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top; |
| 214 | } |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 215 | #endif |
Summer Qin | d00e4db | 2019-05-09 18:03:52 +0800 | [diff] [blame] | 216 | |
Miklos Balint | dd02bb3 | 2019-05-26 21:13:12 +0200 | [diff] [blame] | 217 | #if (TFM_LVL != 1) && !defined(TFM_PSA_API) |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 218 | enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 219 | { |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 220 | struct spm_partition_desc_t *part; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 221 | if (!g_spm_partition_db.is_init) { |
| 222 | return SPM_ERR_PARTITION_DB_NOT_INIT; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 225 | part = &g_spm_partition_db.partitions[partition_idx]; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 226 | |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 227 | return tfm_spm_hal_partition_sandbox_config(&(part->memory_data), |
| 228 | part->platform_data); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 229 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 230 | } |
| 231 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 232 | enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 233 | { |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 234 | /* This function takes a partition id and disables the |
| 235 | * SPM partition for that partition |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 236 | */ |
| 237 | |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 238 | struct spm_partition_desc_t *part; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 239 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 240 | part = &g_spm_partition_db.partitions[partition_idx]; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 241 | |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 242 | return tfm_spm_hal_partition_sandbox_deconfig(&(part->memory_data), |
| 243 | part->platform_data); |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Mate Toth-Pal | 21a74c9 | 2018-04-13 14:05:41 +0200 | [diff] [blame] | 246 | uint32_t tfm_spm_partition_get_zi_start(uint32_t partition_idx) |
| 247 | { |
| 248 | return g_spm_partition_db.partitions[partition_idx]. |
| 249 | memory_data.zi_start; |
| 250 | } |
| 251 | |
| 252 | uint32_t tfm_spm_partition_get_zi_limit(uint32_t partition_idx) |
| 253 | { |
| 254 | return g_spm_partition_db.partitions[partition_idx]. |
| 255 | memory_data.zi_limit; |
| 256 | } |
| 257 | |
| 258 | uint32_t tfm_spm_partition_get_rw_start(uint32_t partition_idx) |
| 259 | { |
| 260 | return g_spm_partition_db.partitions[partition_idx]. |
| 261 | memory_data.rw_start; |
| 262 | } |
| 263 | |
| 264 | uint32_t tfm_spm_partition_get_rw_limit(uint32_t partition_idx) |
| 265 | { |
| 266 | return g_spm_partition_db.partitions[partition_idx]. |
| 267 | memory_data.rw_limit; |
| 268 | } |
| 269 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 270 | void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr) |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 271 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 272 | g_spm_partition_db.partitions[partition_idx]. |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 273 | runtime_data.stack_ptr = stack_ptr; |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 274 | } |
| 275 | #endif |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 276 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 277 | uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 278 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 279 | return g_spm_partition_db.partitions[partition_idx].static_data. |
| 280 | partition_id; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 281 | } |
| 282 | |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 283 | uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx) |
| 284 | { |
| 285 | return g_spm_partition_db.partitions[partition_idx].static_data. |
| 286 | partition_flags; |
| 287 | } |
| 288 | |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 289 | #ifndef TFM_PSA_API |
| 290 | void tfm_spm_partition_store_context(uint32_t partition_idx, |
| 291 | uint32_t stack_ptr, uint32_t lr) |
| 292 | { |
| 293 | g_spm_partition_db.partitions[partition_idx]. |
| 294 | runtime_data.stack_ptr = stack_ptr; |
| 295 | g_spm_partition_db.partitions[partition_idx]. |
| 296 | runtime_data.lr = lr; |
| 297 | } |
| 298 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 299 | const struct spm_partition_runtime_data_t * |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 300 | tfm_spm_partition_get_runtime_data(uint32_t partition_idx) |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 301 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 302 | return &(g_spm_partition_db.partitions[partition_idx].runtime_data); |
| 303 | } |
| 304 | |
| 305 | void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state) |
| 306 | { |
| 307 | g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state = |
| 308 | state; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 309 | if (state == SPM_PARTITION_STATE_RUNNING) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 310 | g_spm_partition_db.running_partition_idx = partition_idx; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 311 | } |
| 312 | } |
| 313 | |
Miklos Balint | 6a139ae | 2018-04-04 19:44:37 +0200 | [diff] [blame] | 314 | void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx, |
| 315 | uint32_t caller_partition_idx) |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 316 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 317 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 318 | caller_partition_idx = caller_partition_idx; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 319 | } |
| 320 | |
Mate Toth-Pal | 21a74c9 | 2018-04-13 14:05:41 +0200 | [diff] [blame] | 321 | void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx, |
| 322 | int32_t caller_client_id) |
| 323 | { |
| 324 | g_spm_partition_db.partitions[partition_idx].runtime_data. |
| 325 | caller_client_id = caller_client_id; |
| 326 | } |
| 327 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 328 | enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx, |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 329 | uint32_t share) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 330 | { |
| 331 | enum spm_err_t ret = SPM_ERR_OK; |
| 332 | |
| 333 | #if TFM_LVL != 1 |
| 334 | /* Only need to set configuration on levels higher than 1 */ |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 335 | ret = tfm_spm_hal_set_share_region(share); |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 336 | #endif |
| 337 | |
| 338 | if (ret == SPM_ERR_OK) { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 339 | g_spm_partition_db.partitions[partition_idx].runtime_data.share = share; |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 340 | } |
| 341 | return ret; |
| 342 | } |
| 343 | |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 344 | enum spm_err_t tfm_spm_partition_set_iovec(uint32_t partition_idx, |
| 345 | const int32_t *args) |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 346 | { |
| 347 | struct spm_partition_runtime_data_t *runtime_data = |
| 348 | &g_spm_partition_db.partitions[partition_idx].runtime_data; |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 349 | size_t i; |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 350 | |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 351 | if ((args[1] < 0) || (args[3] < 0)) { |
| 352 | return SPM_ERR_INVALID_PARAMETER; |
| 353 | } |
| 354 | |
| 355 | runtime_data->iovec_args.in_len = (size_t)args[1]; |
| 356 | for (i = 0U; i < runtime_data->iovec_args.in_len; ++i) { |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 357 | runtime_data->iovec_args.in_vec[i].base = |
| 358 | ((psa_invec *)args[0])[i].base; |
| 359 | runtime_data->iovec_args.in_vec[i].len = ((psa_invec *)args[0])[i].len; |
| 360 | } |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 361 | runtime_data->iovec_args.out_len = (size_t)args[3]; |
| 362 | for (i = 0U; i < runtime_data->iovec_args.out_len; ++i) { |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 363 | runtime_data->iovec_args.out_vec[i].base = |
| 364 | ((psa_outvec *)args[2])[i].base; |
| 365 | runtime_data->iovec_args.out_vec[i].len = |
| 366 | ((psa_outvec *)args[2])[i].len; |
| 367 | } |
| 368 | runtime_data->orig_outvec = (psa_outvec *)args[2]; |
Mate Toth-Pal | 2a6f8c2 | 2018-12-13 16:37:17 +0100 | [diff] [blame] | 369 | runtime_data->iovec_api = 1; |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 370 | |
| 371 | return SPM_ERR_OK; |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 372 | } |
| 373 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 374 | uint32_t tfm_spm_partition_get_running_partition_idx(void) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 375 | { |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 376 | return g_spm_partition_db.running_partition_idx; |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 377 | } |
| 378 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 379 | void tfm_spm_partition_cleanup_context(uint32_t partition_idx) |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 380 | { |
Mate Toth-Pal | 936c33b | 2018-04-10 14:02:07 +0200 | [diff] [blame] | 381 | struct spm_partition_desc_t *partition = |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 382 | &(g_spm_partition_db.partitions[partition_idx]); |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 383 | int32_t i; |
| 384 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 385 | partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX; |
Mate Toth-Pal | 18b8392 | 2018-02-26 17:58:18 +0100 | [diff] [blame] | 386 | partition->runtime_data.share = 0; |
Mate Toth-Pal | 3db437a | 2018-06-22 16:15:13 +0200 | [diff] [blame] | 387 | partition->runtime_data.iovec_args.in_len = 0; |
| 388 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 389 | partition->runtime_data.iovec_args.in_vec[i].base = 0; |
| 390 | partition->runtime_data.iovec_args.in_vec[i].len = 0; |
| 391 | } |
| 392 | partition->runtime_data.iovec_args.out_len = 0; |
| 393 | for (i = 0; i < PSA_MAX_IOVEC; ++i) { |
| 394 | partition->runtime_data.iovec_args.out_vec[i].base = 0; |
| 395 | partition->runtime_data.iovec_args.out_vec[i].len = 0; |
| 396 | } |
| 397 | partition->runtime_data.orig_outvec = 0; |
Mate Toth-Pal | 2a6f8c2 | 2018-12-13 16:37:17 +0100 | [diff] [blame] | 398 | partition->runtime_data.iovec_api = 0; |
Mate Toth-Pal | 65291f3 | 2018-02-23 14:35:22 +0100 | [diff] [blame] | 399 | } |
Summer Qin | b4a854d | 2019-05-29 15:31:22 +0800 | [diff] [blame] | 400 | #endif /* !defined(TFM_PSA_API) */ |
Edison Ai | b557135 | 2019-03-22 10:49:52 +0800 | [diff] [blame] | 401 | |
| 402 | __attribute__((section("SFN"))) |
| 403 | void tfm_spm_partition_change_privilege(uint32_t privileged) |
| 404 | { |
| 405 | CONTROL_Type ctrl; |
| 406 | |
| 407 | ctrl.w = __get_CONTROL(); |
| 408 | |
| 409 | if (privileged == TFM_PARTITION_PRIVILEGED_MODE) { |
| 410 | ctrl.b.nPRIV = 0; |
| 411 | } else { |
| 412 | ctrl.b.nPRIV = 1; |
| 413 | } |
| 414 | |
| 415 | __set_CONTROL(ctrl.w); |
| 416 | } |