Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 1 | /* |
Sherry Zhang | 07b4241 | 2021-01-07 14:19:41 +0800 | [diff] [blame] | 2 | * Copyright (c) 2018-2021, Arm Limited. All rights reserved. |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | * |
| 6 | */ |
| 7 | |
| 8 | #include <stdint.h> |
Xinyu Zhang | ade2e0a | 2021-03-18 16:20:54 +0800 | [diff] [blame] | 9 | #include "array.h" |
Mingyang Sun | 67a1c0e | 2020-06-04 17:18:16 +0800 | [diff] [blame] | 10 | #include "tfm_boot_status.h" |
David Hu | 1cb73db | 2019-09-06 13:29:22 +0800 | [diff] [blame] | 11 | #include "region_defs.h" |
Tamas Ban | 8bd24b7 | 2019-02-19 12:13:13 +0000 | [diff] [blame] | 12 | #include "tfm_memory_utils.h" |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 13 | #include "tfm_api.h" |
Mingyang Sun | 94b1b41 | 2019-09-20 15:11:14 +0800 | [diff] [blame] | 14 | #include "tfm_core_utils.h" |
Mingyang Sun | c3123ec | 2020-06-11 17:43:58 +0800 | [diff] [blame] | 15 | #include "spm_partition_defs.h" |
Edison Ai | 2e413a6 | 2019-05-16 13:46:00 +0800 | [diff] [blame] | 16 | #ifdef TFM_PSA_API |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 17 | #include "internal_errors.h" |
Summer Qin | 5fdcf63 | 2020-06-22 16:49:24 +0800 | [diff] [blame] | 18 | #include "utilities.h" |
Jamie Fox | cc31d40 | 2019-01-28 17:13:52 +0000 | [diff] [blame] | 19 | #include "psa/service.h" |
Edison Ai | 2e413a6 | 2019-05-16 13:46:00 +0800 | [diff] [blame] | 20 | #include "tfm_thread.h" |
| 21 | #include "tfm_wait.h" |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 22 | #include "tfm_spm_hal.h" |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 23 | #include "spm_ipc.h" |
Mingyang Sun | 2b35266 | 2021-04-21 11:35:43 +0800 | [diff] [blame] | 24 | #include "load/partition_defs.h" |
Mingyang Sun | 7397b4f | 2020-06-17 15:07:45 +0800 | [diff] [blame] | 25 | #else |
| 26 | #include "spm_func.h" |
Edison Ai | 2e413a6 | 2019-05-16 13:46:00 +0800 | [diff] [blame] | 27 | #endif |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 28 | |
| 29 | /*! |
| 30 | * \def BOOT_DATA_VALID |
| 31 | * |
| 32 | * \brief Indicates that shared data between bootloader and runtime firmware was |
| 33 | * passed the sanity check with success. |
| 34 | */ |
| 35 | #define BOOT_DATA_VALID (1u) |
| 36 | |
| 37 | /*! |
| 38 | * \def BOOT_DATA_INVALID |
| 39 | * |
| 40 | * \brief Indicates that shared data between bootloader and runtime firmware was |
| 41 | * failed on sanity check. |
| 42 | */ |
| 43 | #define BOOT_DATA_INVALID (0u) |
| 44 | |
| 45 | /*! |
| 46 | * \var is_boot_data_valid |
| 47 | * |
| 48 | * \brief Indicates the status of shared data between bootloader and runtime |
| 49 | * firmware |
| 50 | */ |
| 51 | static uint32_t is_boot_data_valid = BOOT_DATA_INVALID; |
| 52 | |
Tamas Ban | 6bd5b81 | 2019-09-25 09:34:11 +0100 | [diff] [blame] | 53 | /*! |
| 54 | * \struct boot_data_access_policy |
| 55 | * |
| 56 | * \brief Defines the access policy of secure partitions to data items in shared |
| 57 | * data area (between bootloader and runtime firmware). |
| 58 | */ |
| 59 | struct boot_data_access_policy { |
| 60 | uint32_t partition_id; |
| 61 | uint32_t major_type; |
| 62 | }; |
| 63 | |
| 64 | /*! |
| 65 | * \var access_policy_table |
| 66 | * |
| 67 | * \brief Contains the partition_id and major_type assignments. This describes |
| 68 | * which secure partition is allowed to access which data item |
| 69 | * (identified by major_type). |
| 70 | */ |
| 71 | static const struct boot_data_access_policy access_policy_table[] = { |
Edison Ai | b2134e6 | 2019-10-11 18:24:47 +0800 | [diff] [blame] | 72 | {TFM_SP_INITIAL_ATTESTATION, TLV_MAJOR_IAS}, |
Sherry Zhang | 07b4241 | 2021-01-07 14:19:41 +0800 | [diff] [blame] | 73 | {TFM_SP_FWU, TLV_MAJOR_FWU}, |
Tamas Ban | 6bd5b81 | 2019-09-25 09:34:11 +0100 | [diff] [blame] | 74 | }; |
| 75 | |
| 76 | /*! |
| 77 | * \brief Verify the access right of the active secure partition to the |
| 78 | * specified data type in the shared data area. |
| 79 | * |
| 80 | * \param[in] major_type Data type identifier. |
| 81 | * |
| 82 | * \return Returns 0 in case of success, otherwise -1. |
| 83 | */ |
| 84 | static int32_t tfm_core_check_boot_data_access_policy(uint8_t major_type) |
| 85 | { |
| 86 | uint32_t partition_id; |
| 87 | uint32_t i; |
| 88 | int32_t rc = -1; |
Xinyu Zhang | ade2e0a | 2021-03-18 16:20:54 +0800 | [diff] [blame] | 89 | const uint32_t array_size = ARRAY_SIZE(access_policy_table); |
Tamas Ban | 6bd5b81 | 2019-09-25 09:34:11 +0100 | [diff] [blame] | 90 | |
| 91 | #ifndef TFM_PSA_API |
| 92 | uint32_t partition_idx = tfm_spm_partition_get_running_partition_idx(); |
| 93 | |
| 94 | partition_id = tfm_spm_partition_get_partition_id(partition_idx); |
| 95 | #else |
| 96 | partition_id = tfm_spm_partition_get_running_partition_id(); |
| 97 | #endif |
| 98 | |
| 99 | for (i = 0; i < array_size; ++i) { |
| 100 | if (partition_id == access_policy_table[i].partition_id) { |
| 101 | if (major_type == access_policy_table[i].major_type) { |
| 102 | rc = 0; |
| 103 | break; |
| 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | return rc; |
| 109 | } |
| 110 | |
Tamas Ban | 6d2e430 | 2019-10-03 12:37:09 +0100 | [diff] [blame] | 111 | /* Compile time check to verify that shared data region is not overlapping with |
| 112 | * non-secure data area. |
| 113 | */ |
| 114 | #if ((BOOT_TFM_SHARED_DATA_BASE >= NS_DATA_START && \ |
| 115 | BOOT_TFM_SHARED_DATA_BASE <= NS_DATA_LIMIT) || \ |
| 116 | (BOOT_TFM_SHARED_DATA_LIMIT >= NS_DATA_START && \ |
| 117 | BOOT_TFM_SHARED_DATA_LIMIT <= NS_DATA_LIMIT)) |
| 118 | #error "Shared data area and non-secure data area is overlapping" |
| 119 | #endif |
| 120 | |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 121 | void tfm_core_validate_boot_data(void) |
| 122 | { |
David Vincze | e13a48b | 2020-01-08 17:42:30 +0100 | [diff] [blame] | 123 | #ifdef BOOT_DATA_AVAILABLE |
Tamas Ban | a24ce04 | 2019-02-20 11:50:22 +0000 | [diff] [blame] | 124 | struct tfm_boot_data *boot_data; |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 125 | |
Tamas Ban | a24ce04 | 2019-02-20 11:50:22 +0000 | [diff] [blame] | 126 | boot_data = (struct tfm_boot_data *)BOOT_TFM_SHARED_DATA_BASE; |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 127 | |
Tamas Ban | a24ce04 | 2019-02-20 11:50:22 +0000 | [diff] [blame] | 128 | if (boot_data->header.tlv_magic == SHARED_DATA_TLV_INFO_MAGIC) { |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 129 | is_boot_data_valid = BOOT_DATA_VALID; |
| 130 | } |
David Vincze | e13a48b | 2020-01-08 17:42:30 +0100 | [diff] [blame] | 131 | #else |
| 132 | is_boot_data_valid = BOOT_DATA_VALID; |
| 133 | #endif /* BOOT_DATA_AVAILABLE */ |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | void tfm_core_get_boot_data_handler(uint32_t args[]) |
| 137 | { |
| 138 | uint8_t tlv_major = (uint8_t)args[0]; |
Tamas Ban | c43181d | 2019-02-18 14:21:35 +0000 | [diff] [blame] | 139 | uint8_t *buf_start = (uint8_t *)args[1]; |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 140 | uint16_t buf_size = (uint16_t)args[2]; |
Tamas Ban | a24ce04 | 2019-02-20 11:50:22 +0000 | [diff] [blame] | 141 | struct tfm_boot_data *boot_data; |
David Vincze | e13a48b | 2020-01-08 17:42:30 +0100 | [diff] [blame] | 142 | #ifdef BOOT_DATA_AVAILABLE |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 143 | uint8_t *ptr; |
Tamas Ban | c43181d | 2019-02-18 14:21:35 +0000 | [diff] [blame] | 144 | struct shared_data_tlv_entry tlv_entry; |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 145 | uintptr_t tlv_end, offset; |
David Vincze | 5399803 | 2020-06-10 15:54:31 +0200 | [diff] [blame] | 146 | size_t next_tlv_offset; |
David Vincze | e13a48b | 2020-01-08 17:42:30 +0100 | [diff] [blame] | 147 | #endif /* BOOT_DATA_AVAILABLE */ |
Edison Ai | 2e413a6 | 2019-05-16 13:46:00 +0800 | [diff] [blame] | 148 | #ifndef TFM_PSA_API |
| 149 | uint32_t running_partition_idx = |
| 150 | tfm_spm_partition_get_running_partition_idx(); |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 151 | uint32_t res; |
Edison Ai | 2e413a6 | 2019-05-16 13:46:00 +0800 | [diff] [blame] | 152 | #else |
Mingyang Sun | ae70d8d | 2020-06-30 15:56:05 +0800 | [diff] [blame] | 153 | struct partition_t *partition = NULL; |
Edison Ai | 2e413a6 | 2019-05-16 13:46:00 +0800 | [diff] [blame] | 154 | uint32_t privileged; |
| 155 | #endif |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 156 | |
Edison Ai | 2e413a6 | 2019-05-16 13:46:00 +0800 | [diff] [blame] | 157 | #ifndef TFM_PSA_API |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 158 | /* |
| 159 | * Make sure that the output pointer points to a memory area that is owned |
| 160 | * by the partition. And check 4 bytes alignment. |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 161 | */ |
Mingyang Sun | abb1aab | 2020-02-18 13:49:08 +0800 | [diff] [blame] | 162 | res = tfm_spm_check_buffer_access(running_partition_idx, |
| 163 | (void *)buf_start, |
| 164 | buf_size, |
| 165 | 2); |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 166 | if (!res) { |
| 167 | /* Not in accessible range, return error */ |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 168 | args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER; |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 169 | return; |
| 170 | } |
Edison Ai | 2e413a6 | 2019-05-16 13:46:00 +0800 | [diff] [blame] | 171 | #else |
| 172 | partition = tfm_spm_get_running_partition(); |
| 173 | if (!partition) { |
Edison Ai | 9059ea0 | 2019-11-28 13:46:14 +0800 | [diff] [blame] | 174 | tfm_core_panic(); |
Edison Ai | 2e413a6 | 2019-05-16 13:46:00 +0800 | [diff] [blame] | 175 | } |
Mingyang Sun | 5e13aa7 | 2019-07-10 10:30:16 +0800 | [diff] [blame] | 176 | privileged = |
Ken Liu | acd2a57 | 2021-05-12 16:19:04 +0800 | [diff] [blame^] | 177 | tfm_spm_partition_get_privileged_mode(partition->p_ldinf->flags); |
Edison Ai | 2e413a6 | 2019-05-16 13:46:00 +0800 | [diff] [blame] | 178 | |
| 179 | if (tfm_memory_check(buf_start, buf_size, false, TFM_MEMORY_ACCESS_RW, |
Ken Liu | bcae38b | 2021-01-20 15:47:44 +0800 | [diff] [blame] | 180 | privileged) != SPM_SUCCESS) { |
Edison Ai | 2e413a6 | 2019-05-16 13:46:00 +0800 | [diff] [blame] | 181 | /* Not in accessible range, return error */ |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 182 | args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER; |
Edison Ai | 2e413a6 | 2019-05-16 13:46:00 +0800 | [diff] [blame] | 183 | return; |
| 184 | } |
| 185 | #endif |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 186 | |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 187 | if (is_boot_data_valid != BOOT_DATA_VALID) { |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 188 | args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER; |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 189 | return; |
| 190 | } |
| 191 | |
Tamas Ban | 6bd5b81 | 2019-09-25 09:34:11 +0100 | [diff] [blame] | 192 | /* Check whether caller has access right to given tlv_major_type */ |
| 193 | if (tfm_core_check_boot_data_access_policy(tlv_major)) { |
| 194 | args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER; |
| 195 | return; |
| 196 | } |
| 197 | |
David Vincze | e13a48b | 2020-01-08 17:42:30 +0100 | [diff] [blame] | 198 | #ifdef BOOT_DATA_AVAILABLE |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 199 | /* Get the boundaries of TLV section */ |
Tamas Ban | a24ce04 | 2019-02-20 11:50:22 +0000 | [diff] [blame] | 200 | boot_data = (struct tfm_boot_data *)BOOT_TFM_SHARED_DATA_BASE; |
| 201 | tlv_end = BOOT_TFM_SHARED_DATA_BASE + boot_data->header.tlv_tot_len; |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 202 | offset = BOOT_TFM_SHARED_DATA_BASE + SHARED_DATA_HEADER_SIZE; |
David Vincze | e13a48b | 2020-01-08 17:42:30 +0100 | [diff] [blame] | 203 | #endif /* BOOT_DATA_AVAILABLE */ |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 204 | |
| 205 | /* Add header to output buffer as well */ |
| 206 | if (buf_size < SHARED_DATA_HEADER_SIZE) { |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 207 | args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER; |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 208 | return; |
| 209 | } else { |
Tamas Ban | a24ce04 | 2019-02-20 11:50:22 +0000 | [diff] [blame] | 210 | boot_data = (struct tfm_boot_data *)buf_start; |
| 211 | boot_data->header.tlv_magic = SHARED_DATA_TLV_INFO_MAGIC; |
| 212 | boot_data->header.tlv_tot_len = SHARED_DATA_HEADER_SIZE; |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 213 | } |
| 214 | |
David Vincze | e13a48b | 2020-01-08 17:42:30 +0100 | [diff] [blame] | 215 | #ifdef BOOT_DATA_AVAILABLE |
TTornblom | faf74f5 | 2020-03-04 17:56:27 +0100 | [diff] [blame] | 216 | ptr = boot_data->data; |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 217 | /* Iterates over the TLV section and copy TLVs with requested major |
| 218 | * type to the provided buffer. |
| 219 | */ |
David Vincze | 5399803 | 2020-06-10 15:54:31 +0200 | [diff] [blame] | 220 | for (; offset < tlv_end; offset += next_tlv_offset) { |
Tamas Ban | c43181d | 2019-02-18 14:21:35 +0000 | [diff] [blame] | 221 | /* Create local copy to avoid unaligned access */ |
Summer Qin | f24dbb5 | 2020-07-23 14:53:54 +0800 | [diff] [blame] | 222 | (void)spm_memcpy(&tlv_entry, (const void *)offset, |
| 223 | SHARED_DATA_ENTRY_HEADER_SIZE); |
Balint Matyi | e5cadb2 | 2020-07-06 14:43:37 +0100 | [diff] [blame] | 224 | |
David Vincze | 5399803 | 2020-06-10 15:54:31 +0200 | [diff] [blame] | 225 | next_tlv_offset = SHARED_DATA_ENTRY_HEADER_SIZE + tlv_entry.tlv_len; |
Balint Matyi | e5cadb2 | 2020-07-06 14:43:37 +0100 | [diff] [blame] | 226 | |
Tamas Ban | c43181d | 2019-02-18 14:21:35 +0000 | [diff] [blame] | 227 | if (GET_MAJOR(tlv_entry.tlv_type) == tlv_major) { |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 228 | /* Check buffer overflow */ |
David Vincze | 5399803 | 2020-06-10 15:54:31 +0200 | [diff] [blame] | 229 | if (((ptr - buf_start) + next_tlv_offset) > buf_size) { |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 230 | args[0] = (uint32_t)TFM_ERROR_INVALID_PARAMETER; |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 231 | return; |
| 232 | } |
| 233 | |
Summer Qin | f24dbb5 | 2020-07-23 14:53:54 +0800 | [diff] [blame] | 234 | (void)spm_memcpy(ptr, (const void *)offset, next_tlv_offset); |
David Vincze | 5399803 | 2020-06-10 15:54:31 +0200 | [diff] [blame] | 235 | ptr += next_tlv_offset; |
| 236 | boot_data->header.tlv_tot_len += next_tlv_offset; |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 237 | } |
| 238 | } |
David Vincze | e13a48b | 2020-01-08 17:42:30 +0100 | [diff] [blame] | 239 | #endif /* BOOT_DATA_AVAILABLE */ |
| 240 | |
Hugues de Valon | 9957856 | 2019-06-18 16:08:51 +0100 | [diff] [blame] | 241 | args[0] = (uint32_t)TFM_SUCCESS; |
Tamas Ban | 9ff535b | 2018-09-18 08:15:18 +0100 | [diff] [blame] | 242 | return; |
| 243 | } |