Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 1 | /* |
Mate Toth-Pal | 5e6d034 | 2019-11-22 11:43:20 +0100 | [diff] [blame] | 2 | * Copyright (c) 2017-2020, 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 | |
Mingyang Sun | da01a97 | 2019-07-12 17:32:59 +0800 | [diff] [blame] | 8 | /* All the APIs defined in this file are common for library and IPC model. */ |
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" |
Mingyang Sun | f3d2989 | 2019-07-10 17:50:23 +0800 | [diff] [blame] | 13 | #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 | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 15 | #include "spm_db.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" |
David Hu | 49a28eb | 2019-08-14 18:18:15 +0800 | [diff] [blame] | 19 | #include "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" |
Shawn Shan | 6e7be07 | 2019-11-29 17:56:29 +0800 | [diff] [blame] | 22 | #include "psa/lifecycle.h" |
TTornblom | 83d9637 | 2019-11-19 12:53:16 +0100 | [diff] [blame] | 23 | #include "region.h" |
Mate Toth-Pal | e147533 | 2018-04-09 17:28:49 +0200 | [diff] [blame] | 24 | |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 25 | #define NON_SECURE_INTERNAL_PARTITION_DB_IDX 0 |
| 26 | #define TFM_CORE_INTERNAL_PARTITION_DB_IDX 1 |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 27 | |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 28 | /* Define SPM DB structure */ |
Edison Ai | a8e751e | 2019-10-12 14:08:15 +0800 | [diff] [blame] | 29 | #include "tfm_spm_db.inc" |
Mate Toth-Pal | 3ad2e3e | 2019-07-11 21:43:37 +0200 | [diff] [blame] | 30 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 31 | uint32_t get_partition_idx(uint32_t partition_id) |
| 32 | { |
Hugues de Valon | f704c80 | 2019-02-19 14:51:41 +0000 | [diff] [blame] | 33 | uint32_t i; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 34 | |
| 35 | if (partition_id == INVALID_PARTITION_ID) { |
| 36 | return SPM_INVALID_PARTITION_IDX; |
| 37 | } |
| 38 | |
| 39 | for (i = 0; i < g_spm_partition_db.partition_count; ++i) { |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 40 | if (g_spm_partition_db.partitions[i].static_data->partition_id == |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 41 | partition_id) { |
| 42 | return i; |
| 43 | } |
| 44 | } |
| 45 | return SPM_INVALID_PARTITION_IDX; |
| 46 | } |
| 47 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 48 | enum spm_err_t tfm_spm_db_init(void) |
| 49 | { |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 50 | uint32_t i; |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 51 | |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 52 | /* This function initialises partition db */ |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 53 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 54 | /* For the non secure Execution environment */ |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 55 | #ifndef TFM_PSA_API |
Miklos Balint | 12735bc | 2018-08-01 15:45:18 +0200 | [diff] [blame] | 56 | tfm_nspm_configure_clients(); |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 57 | #endif |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 58 | |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 59 | for (i = 0; i < g_spm_partition_db.partition_count; i++) { |
| 60 | #ifndef TFM_PSA_API |
| 61 | g_spm_partition_db.partitions[i].runtime_data.partition_state = |
| 62 | SPM_PARTITION_STATE_UNINIT; |
| 63 | g_spm_partition_db.partitions[i].runtime_data.caller_partition_idx = |
| 64 | SPM_INVALID_PARTITION_IDX; |
| 65 | g_spm_partition_db.partitions[i].runtime_data.caller_client_id = |
| 66 | TFM_INVALID_CLIENT_ID; |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 67 | g_spm_partition_db.partitions[i].runtime_data.ctx_stack_ptr = |
| 68 | ctx_stack_list[i]; |
Edison Ai | 66fbdf1 | 2019-07-08 16:05:07 +0800 | [diff] [blame] | 69 | #endif /* !defined(TFM_PSA_API) */ |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 70 | g_spm_partition_db.partitions[i].static_data = &static_data_list[i]; |
Mate Toth-Pal | 8ac98a7 | 2019-11-21 17:30:10 +0100 | [diff] [blame] | 71 | g_spm_partition_db.partitions[i].platform_data_list = |
| 72 | platform_data_list_list[i]; |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 73 | #ifdef TFM_PSA_API |
| 74 | g_spm_partition_db.partitions[i].memory_data = &memory_data_list[i]; |
| 75 | #endif |
| 76 | } |
Mate Toth-Pal | 7345a4b | 2018-03-08 16:10:28 +0100 | [diff] [blame] | 77 | g_spm_partition_db.is_init = 1; |
| 78 | |
Miklos Balint | 386b8b5 | 2017-11-29 13:12:32 +0000 | [diff] [blame] | 79 | return SPM_ERR_OK; |
| 80 | } |
| 81 | |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 82 | 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] | 83 | { |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 84 | return g_spm_partition_db.partitions[partition_idx].static_data-> |
Mate Toth-Pal | 52674ab | 2018-02-26 09:47:56 +0100 | [diff] [blame] | 85 | partition_id; |
Mate Toth-Pal | 349714a | 2018-02-23 15:30:24 +0100 | [diff] [blame] | 86 | } |
| 87 | |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 88 | uint32_t tfm_spm_partition_get_flags(uint32_t partition_idx) |
| 89 | { |
Summer Qin | 423dbef | 2019-08-22 15:59:35 +0800 | [diff] [blame] | 90 | return g_spm_partition_db.partitions[partition_idx].static_data-> |
Mate Toth-Pal | 5939871 | 2018-02-28 17:06:40 +0100 | [diff] [blame] | 91 | partition_flags; |
| 92 | } |
| 93 | |
Mate Toth-Pal | 5e6d034 | 2019-11-22 11:43:20 +0100 | [diff] [blame] | 94 | uint32_t tfm_spm_partition_get_privileged_mode(uint32_t partition_flags) |
| 95 | { |
| 96 | if (partition_flags & SPM_PART_FLAG_PSA_ROT) { |
| 97 | return TFM_PARTITION_PRIVILEGED_MODE; |
| 98 | } else { |
| 99 | return TFM_PARTITION_UNPRIVILEGED_MODE; |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | bool tfm_is_partition_privileged(uint32_t partition_idx) |
| 104 | { |
| 105 | uint32_t flags = tfm_spm_partition_get_flags(partition_idx); |
| 106 | |
| 107 | return tfm_spm_partition_get_privileged_mode(flags) == |
| 108 | TFM_PARTITION_PRIVILEGED_MODE; |
| 109 | } |
| 110 | |
Shawn Shan | 6e7be07 | 2019-11-29 17:56:29 +0800 | [diff] [blame] | 111 | uint32_t tfm_spm_get_lifecycle_state(void) |
| 112 | { |
| 113 | /* |
| 114 | * FixMe: return PSA_LIFECYCLE_UNKNOWN to the caller directly. It will be |
| 115 | * implemented in the future. |
| 116 | */ |
| 117 | return PSA_LIFECYCLE_UNKNOWN; |
| 118 | } |
| 119 | |
Edison Ai | b557135 | 2019-03-22 10:49:52 +0800 | [diff] [blame] | 120 | __attribute__((section("SFN"))) |
| 121 | void tfm_spm_partition_change_privilege(uint32_t privileged) |
| 122 | { |
| 123 | CONTROL_Type ctrl; |
| 124 | |
| 125 | ctrl.w = __get_CONTROL(); |
| 126 | |
| 127 | if (privileged == TFM_PARTITION_PRIVILEGED_MODE) { |
| 128 | ctrl.b.nPRIV = 0; |
| 129 | } else { |
| 130 | ctrl.b.nPRIV = 1; |
| 131 | } |
| 132 | |
| 133 | __set_CONTROL(ctrl.w); |
| 134 | } |