blob: b17ee99d4a3e9d40fd0ec533e7dec88162c79b5e [file] [log] [blame]
Miklos Balint386b8b52017-11-29 13:12:32 +00001/*
Mate Toth-Pal65291f32018-02-23 14:35:22 +01002 * Copyright (c) 2017-2018, Arm Limited. All rights reserved.
Miklos Balint386b8b52017-11-29 13:12:32 +00003 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 *
6 */
7
Mate Toth-Pal349714a2018-02-23 15:30:24 +01008/* This file contains the APIs exported by the SPM to tfm core */
Miklos Balint386b8b52017-11-29 13:12:32 +00009
10#include <stdio.h>
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010011#include <string.h>
Miklos Balint386b8b52017-11-29 13:12:32 +000012#include "spm_api.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020013#include "platform/include/tfm_spm_hal.h"
14#include "spm_db_setup.h"
Miklos Balint6a139ae2018-04-04 19:44:37 +020015#include "tfm_internal.h"
Mate Toth-Pal65291f32018-02-23 14:35:22 +010016#include "tfm_api.h"
Mate Toth-Palce61afa2018-08-03 13:51:01 +020017#include "tfm_nspm.h"
Miklos Balint386b8b52017-11-29 13:12:32 +000018#include "secure_fw/core/tfm_core.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020019#include "platform_retarget.h"
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020020#include "tfm_peripherals_def.h"
Mate Toth-Pale1475332018-04-09 17:28:49 +020021#include "spm_partition_defs.h"
22
Miklos Balint386b8b52017-11-29 13:12:32 +000023
Mate Toth-Pal349714a2018-02-23 15:30:24 +010024struct spm_partition_db_t g_spm_partition_db = {0,};
Miklos Balint386b8b52017-11-29 13:12:32 +000025
Miklos Balint386b8b52017-11-29 13:12:32 +000026typedef enum {
27 TFM_INIT_FAILURE,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010028} sp_error_type_t;
Miklos Balint386b8b52017-11-29 13:12:32 +000029
30/*
Mate Toth-Pal349714a2018-02-23 15:30:24 +010031 * This function is called when a secure partition causes an error.
Mate Toth-Pal65291f32018-02-23 14:35:22 +010032 * In case of an error in the error handling, a non-zero value have to be
33 * returned.
Miklos Balint386b8b52017-11-29 13:12:32 +000034 */
Mate Toth-Pal349714a2018-02-23 15:30:24 +010035static void tfm_spm_partition_err_handler(
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020036 struct spm_partition_desc_t *partition,
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010037 sp_error_type_t err_type,
Mate Toth-Pal65291f32018-02-23 14:35:22 +010038 int32_t err_code)
Miklos Balint386b8b52017-11-29 13:12:32 +000039{
Miklos Balint386b8b52017-11-29 13:12:32 +000040#ifdef TFM_CORE_DEBUG
41 if (err_type == TFM_INIT_FAILURE) {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010042 printf("Partition init failed for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010043 partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000044 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +010045 printf("Unknown partition error %d for partition id 0x%08X\r\n",
Mate Toth-Pal18b83922018-02-26 17:58:18 +010046 err_type, partition->static_data.partition_id);
Miklos Balint386b8b52017-11-29 13:12:32 +000047 }
48#endif
Mate Toth-Pal18b83922018-02-26 17:58:18 +010049 tfm_spm_partition_set_state(partition->static_data.partition_id,
Mate Toth-Pal349714a2018-02-23 15:30:24 +010050 SPM_PARTITION_STATE_CLOSED);
Miklos Balint386b8b52017-11-29 13:12:32 +000051}
52
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010053uint32_t get_partition_idx(uint32_t partition_id)
54{
55 int i;
56
57 if (partition_id == INVALID_PARTITION_ID) {
58 return SPM_INVALID_PARTITION_IDX;
59 }
60
61 for (i = 0; i < g_spm_partition_db.partition_count; ++i) {
62 if (g_spm_partition_db.partitions[i].static_data.partition_id ==
63 partition_id) {
64 return i;
65 }
66 }
67 return SPM_INVALID_PARTITION_IDX;
68}
69
Miklos Balint386b8b52017-11-29 13:12:32 +000070enum spm_err_t tfm_spm_db_init(void)
71{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +020072 struct spm_partition_desc_t *part_ptr;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010073
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +010074 memset (&g_spm_partition_db, 0, sizeof(g_spm_partition_db));
75
Mate Toth-Pal349714a2018-02-23 15:30:24 +010076 /* This function initialises partition db */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010077 g_spm_partition_db.running_partition_idx = SPM_INVALID_PARTITION_IDX;
78 g_spm_partition_db.partition_count = 0;
Miklos Balint386b8b52017-11-29 13:12:32 +000079
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010080 /* There are a few partitions that are used by TF-M internally.
81 * These are explicitly added to the partition db here.
82 */
83
84 /* For the non secure Execution environment */
Miklos Balint6a139ae2018-04-04 19:44:37 +020085#if TFM_LVL != 1
Tamas Ban56ef3022018-09-13 23:49:16 +010086 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Base[];
87 extern uint32_t Image$$ARM_LIB_STACK$$ZI$$Limit[];
88 uint32_t psp_stack_bottom = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Base;
89 uint32_t psp_stack_top = (uint32_t)Image$$ARM_LIB_STACK$$ZI$$Limit;
Miklos Balint6a139ae2018-04-04 19:44:37 +020090#endif
Mate Toth-Pal52674ab2018-02-26 09:47:56 +010091 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
92 return SPM_ERR_INVALID_CONFIG;
93 }
94 part_ptr = &(g_spm_partition_db.partitions[
95 g_spm_partition_db.partition_count]);
96 part_ptr->static_data.partition_id = TFM_SP_NON_SECURE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +010097 part_ptr->static_data.partition_flags = 0;
Miklos Balint6a139ae2018-04-04 19:44:37 +020098
99#if TFM_LVL != 1
Tamas Ban56ef3022018-09-13 23:49:16 +0100100 part_ptr->memory_data.stack_bottom = psp_stack_bottom;
101 part_ptr->memory_data.stack_top = psp_stack_top;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200102 /* Since RW, ZI and stack are configured as one MPU region, configure
Tamas Ban56ef3022018-09-13 23:49:16 +0100103 * RW start address to psp_stack_bottom to get RW access to stack
Miklos Balint6a139ae2018-04-04 19:44:37 +0200104 */
Tamas Ban56ef3022018-09-13 23:49:16 +0100105 part_ptr->memory_data.rw_start = psp_stack_bottom;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200106#endif
107
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100108 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
Miklos Balint12735bc2018-08-01 15:45:18 +0200109 tfm_nspm_configure_clients();
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100110 ++g_spm_partition_db.partition_count;
111
112 /* For the TF-M core environment itself */
113 if (g_spm_partition_db.partition_count >= SPM_MAX_PARTITIONS) {
114 return SPM_ERR_INVALID_CONFIG;
115 }
116 part_ptr = &(g_spm_partition_db.partitions[
117 g_spm_partition_db.partition_count]);
118 part_ptr->static_data.partition_id = TFM_SP_CORE_ID;
Mate Toth-Pal59398712018-02-28 17:06:40 +0100119 part_ptr->static_data.partition_flags =
120 SPM_PART_FLAG_SECURE | SPM_PART_FLAG_TRUSTED;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100121 part_ptr->runtime_data.partition_state = SPM_PARTITION_STATE_UNINIT;
122 ++g_spm_partition_db.partition_count;
123
124 /* Add user-defined secure partitions */
Miklos Balintd306ab12018-05-18 16:58:18 +0200125 #include "secure_fw/services/tfm_partition_list.inc"
Miklos Balint386b8b52017-11-29 13:12:32 +0000126
Mate Toth-Pal7345a4b2018-03-08 16:10:28 +0100127 g_spm_partition_db.is_init = 1;
128
Miklos Balint386b8b52017-11-29 13:12:32 +0000129 return SPM_ERR_OK;
130}
131
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100132enum spm_err_t tfm_spm_partition_init(void)
Miklos Balint386b8b52017-11-29 13:12:32 +0000133{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200134 struct spm_partition_desc_t *part;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200135 struct tfm_sfn_req_s desc;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200136 int32_t args[4] = {0};
Miklos Balint386b8b52017-11-29 13:12:32 +0000137 int32_t fail_cnt = 0;
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100138 uint32_t idx;
Miklos Balint386b8b52017-11-29 13:12:32 +0000139
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100140 /* Call the init function for each partition */
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100141 for (idx = 0; idx < g_spm_partition_db.partition_count; ++idx) {
142 part = &g_spm_partition_db.partitions[idx];
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200143 tfm_spm_hal_configure_default_isolation(part->platform_data);
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100144 if (part->static_data.partition_init == NULL) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100145 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Miklos Balint6a139ae2018-04-04 19:44:37 +0200146 tfm_spm_partition_set_caller_partition_idx(idx,
147 SPM_INVALID_PARTITION_IDX);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100148 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200149 int32_t res;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200150
151 desc.args = args;
Miklos Balint6a139ae2018-04-04 19:44:37 +0200152 desc.ns_caller = 0;
153 desc.sfn = (sfn_t)part->static_data.partition_init;
154 desc.sp_id = part->static_data.partition_id;
Miklos Balintace4c3f2018-07-30 12:31:15 +0200155 res = tfm_core_sfn_request(&desc);
156 if (res == TFM_SUCCESS) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100157 tfm_spm_partition_set_state(idx, SPM_PARTITION_STATE_IDLE);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100158 } else {
Miklos Balintace4c3f2018-07-30 12:31:15 +0200159 tfm_spm_partition_err_handler(part, TFM_INIT_FAILURE, res);
Miklos Balint386b8b52017-11-29 13:12:32 +0000160 fail_cnt++;
161 }
162 }
163 }
164
Miklos Balint6a139ae2018-04-04 19:44:37 +0200165 tfm_secure_api_init_done();
166
Miklos Balint386b8b52017-11-29 13:12:32 +0000167 if (fail_cnt == 0) {
168 return SPM_ERR_OK;
169 } else {
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100170 return SPM_ERR_PARTITION_NOT_AVAILABLE;
Miklos Balint386b8b52017-11-29 13:12:32 +0000171 }
172}
173
174#if TFM_LVL != 1
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100175enum spm_err_t tfm_spm_partition_sandbox_config(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000176{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200177 struct spm_partition_desc_t *part;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100178 if (!g_spm_partition_db.is_init) {
179 return SPM_ERR_PARTITION_DB_NOT_INIT;
Miklos Balint386b8b52017-11-29 13:12:32 +0000180 }
181
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100182 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000183
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200184 return tfm_spm_hal_partition_sandbox_config(&(part->memory_data),
185 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000186
Miklos Balint386b8b52017-11-29 13:12:32 +0000187}
188
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100189enum spm_err_t tfm_spm_partition_sandbox_deconfig(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000190{
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100191 /* This function takes a partition id and disables the
192 * SPM partition for that partition
Miklos Balint386b8b52017-11-29 13:12:32 +0000193 */
194
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200195 struct spm_partition_desc_t *part;
Miklos Balint386b8b52017-11-29 13:12:32 +0000196
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100197 part = &g_spm_partition_db.partitions[partition_idx];
Miklos Balint386b8b52017-11-29 13:12:32 +0000198
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200199 return tfm_spm_hal_partition_sandbox_deconfig(&(part->memory_data),
200 part->platform_data);
Miklos Balint386b8b52017-11-29 13:12:32 +0000201}
202
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100203uint32_t tfm_spm_partition_get_stack_bottom(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000204{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100205 return g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200206 memory_data.stack_bottom;
Miklos Balint386b8b52017-11-29 13:12:32 +0000207}
208
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100209uint32_t tfm_spm_partition_get_stack_top(uint32_t partition_idx)
Miklos Balint386b8b52017-11-29 13:12:32 +0000210{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200211 return g_spm_partition_db.partitions[partition_idx].memory_data.stack_top;
Miklos Balint386b8b52017-11-29 13:12:32 +0000212}
213
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200214uint32_t tfm_spm_partition_get_zi_start(uint32_t partition_idx)
215{
216 return g_spm_partition_db.partitions[partition_idx].
217 memory_data.zi_start;
218}
219
220uint32_t tfm_spm_partition_get_zi_limit(uint32_t partition_idx)
221{
222 return g_spm_partition_db.partitions[partition_idx].
223 memory_data.zi_limit;
224}
225
226uint32_t tfm_spm_partition_get_rw_start(uint32_t partition_idx)
227{
228 return g_spm_partition_db.partitions[partition_idx].
229 memory_data.rw_start;
230}
231
232uint32_t tfm_spm_partition_get_rw_limit(uint32_t partition_idx)
233{
234 return g_spm_partition_db.partitions[partition_idx].
235 memory_data.rw_limit;
236}
237
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100238void tfm_spm_partition_set_stack(uint32_t partition_idx, uint32_t stack_ptr)
Miklos Balint386b8b52017-11-29 13:12:32 +0000239{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100240 g_spm_partition_db.partitions[partition_idx].
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100241 runtime_data.stack_ptr = stack_ptr;
Miklos Balint386b8b52017-11-29 13:12:32 +0000242}
243#endif
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100244
Miklos Balintace4c3f2018-07-30 12:31:15 +0200245void tfm_spm_partition_store_context(uint32_t partition_idx,
246 uint32_t stack_ptr, uint32_t lr)
247{
248 g_spm_partition_db.partitions[partition_idx].
249 runtime_data.stack_ptr = stack_ptr;
250 g_spm_partition_db.partitions[partition_idx].
251 runtime_data.lr = lr;
252}
253
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100254uint32_t tfm_spm_partition_get_partition_id(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100255{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100256 return g_spm_partition_db.partitions[partition_idx].static_data.
257 partition_id;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100258}
259
Mate Toth-Pal59398712018-02-28 17:06:40 +0100260uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx)
261{
262 return g_spm_partition_db.partitions[partition_idx].static_data.
263 partition_flags;
264}
265
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100266const struct spm_partition_runtime_data_t *
Mate Toth-Pal59398712018-02-28 17:06:40 +0100267 tfm_spm_partition_get_runtime_data(uint32_t partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100268{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100269 return &(g_spm_partition_db.partitions[partition_idx].runtime_data);
270}
271
272void tfm_spm_partition_set_state(uint32_t partition_idx, uint32_t state)
273{
274 g_spm_partition_db.partitions[partition_idx].runtime_data.partition_state =
275 state;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100276 if (state == SPM_PARTITION_STATE_RUNNING) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100277 g_spm_partition_db.running_partition_idx = partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100278 }
279}
280
Miklos Balint6a139ae2018-04-04 19:44:37 +0200281void tfm_spm_partition_set_caller_partition_idx(uint32_t partition_idx,
282 uint32_t caller_partition_idx)
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100283{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100284 g_spm_partition_db.partitions[partition_idx].runtime_data.
285 caller_partition_idx = caller_partition_idx;
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100286}
287
Mate Toth-Pal21a74c92018-04-13 14:05:41 +0200288void tfm_spm_partition_set_caller_client_id(uint32_t partition_idx,
289 int32_t caller_client_id)
290{
291 g_spm_partition_db.partitions[partition_idx].runtime_data.
292 caller_client_id = caller_client_id;
293}
294
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100295enum spm_err_t tfm_spm_partition_set_share(uint32_t partition_idx,
Mate Toth-Pal349714a2018-02-23 15:30:24 +0100296 uint32_t share)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100297{
298 enum spm_err_t ret = SPM_ERR_OK;
299
300#if TFM_LVL != 1
301 /* Only need to set configuration on levels higher than 1 */
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200302 ret = tfm_spm_hal_set_share_region(share);
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100303#endif
304
305 if (ret == SPM_ERR_OK) {
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100306 g_spm_partition_db.partitions[partition_idx].runtime_data.share = share;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100307 }
308 return ret;
309}
310
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100311uint32_t tfm_spm_partition_get_running_partition_idx(void)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100312{
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100313 return g_spm_partition_db.running_partition_idx;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100314}
315
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100316void tfm_spm_partition_cleanup_context(uint32_t partition_idx)
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100317{
Mate Toth-Pal936c33b2018-04-10 14:02:07 +0200318 struct spm_partition_desc_t *partition =
Mate Toth-Pal52674ab2018-02-26 09:47:56 +0100319 &(g_spm_partition_db.partitions[partition_idx]);
320 partition->runtime_data.caller_partition_idx = SPM_INVALID_PARTITION_IDX;
Mate Toth-Pal18b83922018-02-26 17:58:18 +0100321 partition->runtime_data.share = 0;
Mate Toth-Pal65291f32018-02-23 14:35:22 +0100322}