Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 1 | /* |
Yann Gautier | b3f7d75 | 2022-06-20 11:24:22 +0200 | [diff] [blame] | 2 | * Copyright (c) 2017-2022, ARM Limited and Contributors. All rights reserved. |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 3 | * |
| 4 | * SPDX-License-Identifier: BSD-3-Clause |
| 5 | */ |
| 6 | |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 7 | #include <assert.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 8 | |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 9 | #include <common/debug.h> |
Antonio Nino Diaz | e0f9063 | 2018-12-14 00:18:21 +0000 | [diff] [blame] | 10 | #include <lib/optee_utils.h> |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 11 | |
| 12 | /* |
| 13 | * load_addr_hi and load_addr_lo: image load address. |
| 14 | * image_id: 0 - pager, 1 - paged |
| 15 | * size: image size in bytes. |
| 16 | */ |
| 17 | typedef struct optee_image { |
| 18 | uint32_t load_addr_hi; |
| 19 | uint32_t load_addr_lo; |
| 20 | uint32_t image_id; |
| 21 | uint32_t size; |
| 22 | } optee_image_t; |
| 23 | |
| 24 | #define OPTEE_PAGER_IMAGE_ID 0 |
| 25 | #define OPTEE_PAGED_IMAGE_ID 1 |
nathan-menhorn | 9e845e5 | 2018-07-17 09:08:30 -0600 | [diff] [blame] | 26 | |
| 27 | #define OPTEE_MAX_NUM_IMAGES 2u |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 28 | |
| 29 | #define TEE_MAGIC_NUM_OPTEE 0x4554504f |
| 30 | /* |
| 31 | * magic: header magic number. |
| 32 | * version: OPTEE header version: |
nathan-menhorn | 9e845e5 | 2018-07-17 09:08:30 -0600 | [diff] [blame] | 33 | * 1 - not supported |
| 34 | * 2 - supported |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 35 | * arch: OPTEE os architecture type: 0 - AARCH32, 1 - AARCH64. |
| 36 | * flags: unused currently. |
| 37 | * nb_images: number of images. |
| 38 | */ |
| 39 | typedef struct optee_header { |
| 40 | uint32_t magic; |
| 41 | uint8_t version; |
| 42 | uint8_t arch; |
| 43 | uint16_t flags; |
| 44 | uint32_t nb_images; |
Daniel Boulby | c5259cc | 2018-05-15 11:41:55 +0100 | [diff] [blame] | 45 | optee_image_t optee_image_list[]; |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 46 | } optee_header_t; |
| 47 | |
| 48 | /******************************************************************************* |
| 49 | * Check if it is a valid tee header |
Etienne Carriere | d3e31e7 | 2019-04-15 18:01:29 +0200 | [diff] [blame] | 50 | * Return true if valid |
| 51 | * Return false if invalid |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 52 | ******************************************************************************/ |
Etienne Carriere | d3e31e7 | 2019-04-15 18:01:29 +0200 | [diff] [blame] | 53 | static bool tee_validate_header(optee_header_t *header) |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 54 | { |
Daniel Boulby | c5259cc | 2018-05-15 11:41:55 +0100 | [diff] [blame] | 55 | if ((header->magic == TEE_MAGIC_NUM_OPTEE) && |
nathan-menhorn | 9e845e5 | 2018-07-17 09:08:30 -0600 | [diff] [blame] | 56 | (header->version == 2u) && |
| 57 | (header->nb_images > 0u) && |
| 58 | (header->nb_images <= OPTEE_MAX_NUM_IMAGES)) { |
Etienne Carriere | d3e31e7 | 2019-04-15 18:01:29 +0200 | [diff] [blame] | 59 | return true; |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 60 | } |
| 61 | |
Etienne Carriere | d3e31e7 | 2019-04-15 18:01:29 +0200 | [diff] [blame] | 62 | return false; |
| 63 | } |
nathan-menhorn | 9e845e5 | 2018-07-17 09:08:30 -0600 | [diff] [blame] | 64 | |
Etienne Carriere | d3e31e7 | 2019-04-15 18:01:29 +0200 | [diff] [blame] | 65 | bool optee_header_is_valid(uintptr_t header_base) |
| 66 | { |
| 67 | return tee_validate_header((optee_header_t *)header_base); |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 68 | } |
| 69 | |
| 70 | /******************************************************************************* |
| 71 | * Parse the OPTEE image |
| 72 | * Return 0 on success or a negative error code otherwise. |
| 73 | ******************************************************************************/ |
| 74 | static int parse_optee_image(image_info_t *image_info, |
Daniel Boulby | c5259cc | 2018-05-15 11:41:55 +0100 | [diff] [blame] | 75 | optee_image_t *image) |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 76 | { |
| 77 | uintptr_t init_load_addr, free_end, requested_end; |
| 78 | size_t init_size; |
| 79 | |
Daniel Boulby | c5259cc | 2018-05-15 11:41:55 +0100 | [diff] [blame] | 80 | init_load_addr = ((uint64_t)image->load_addr_hi << 32) | |
| 81 | image->load_addr_lo; |
| 82 | init_size = image->size; |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 83 | |
| 84 | /* |
Yann Gautier | 1f6fd3d | 2020-11-10 14:47:04 +0100 | [diff] [blame] | 85 | * image->load_addr_hi & image->load_addr_lo set to UINT32_MAX indicate |
| 86 | * loader decided address; take our pre-mapped area for current image |
| 87 | * since arm-tf could not allocate memory dynamically |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 88 | */ |
Yann Gautier | 1f6fd3d | 2020-11-10 14:47:04 +0100 | [diff] [blame] | 89 | if ((image->load_addr_hi == UINT32_MAX) && |
| 90 | (image->load_addr_lo == UINT32_MAX)) { |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 91 | init_load_addr = image_info->image_base; |
Yann Gautier | 1f6fd3d | 2020-11-10 14:47:04 +0100 | [diff] [blame] | 92 | } |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 93 | |
| 94 | /* Check that the default end address doesn't overflow */ |
| 95 | if (check_uptr_overflow(image_info->image_base, |
| 96 | image_info->image_max_size - 1)) |
| 97 | return -1; |
| 98 | free_end = image_info->image_base + (image_info->image_max_size - 1); |
| 99 | |
| 100 | /* Check that the image end address doesn't overflow */ |
| 101 | if (check_uptr_overflow(init_load_addr, init_size - 1)) |
| 102 | return -1; |
| 103 | requested_end = init_load_addr + (init_size - 1); |
| 104 | /* |
| 105 | * Check that the requested RAM location is within reserved |
| 106 | * space for OPTEE. |
| 107 | */ |
| 108 | if (!((init_load_addr >= image_info->image_base) && |
| 109 | (requested_end <= free_end))) { |
| 110 | WARN("The load address in optee header %p - %p is not in reserved area: %p - %p.\n", |
| 111 | (void *)init_load_addr, |
| 112 | (void *)(init_load_addr + init_size), |
| 113 | (void *)image_info->image_base, |
| 114 | (void *)(image_info->image_base + |
| 115 | image_info->image_max_size)); |
| 116 | return -1; |
| 117 | } |
| 118 | |
| 119 | /* |
| 120 | * Remove the skip attr from image_info, the image will be loaded. |
| 121 | * The default attr in image_info is "IMAGE_ATTRIB_SKIP_LOADING", which |
| 122 | * mean the image will not be loaded. Here, we parse the header image to |
| 123 | * know that the extra image need to be loaded, so remove the skip attr. |
| 124 | */ |
| 125 | image_info->h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING; |
| 126 | |
| 127 | /* Update image base and size of image_info */ |
| 128 | image_info->image_base = init_load_addr; |
| 129 | image_info->image_size = init_size; |
| 130 | |
| 131 | return 0; |
| 132 | } |
| 133 | |
| 134 | /******************************************************************************* |
| 135 | * Parse the OPTEE header |
| 136 | * Return 0 on success or a negative error code otherwise. |
| 137 | ******************************************************************************/ |
| 138 | int parse_optee_header(entry_point_info_t *header_ep, |
| 139 | image_info_t *pager_image_info, |
| 140 | image_info_t *paged_image_info) |
| 141 | |
| 142 | { |
Daniel Boulby | c5259cc | 2018-05-15 11:41:55 +0100 | [diff] [blame] | 143 | optee_header_t *header; |
Yann Gautier | 1f6fd3d | 2020-11-10 14:47:04 +0100 | [diff] [blame] | 144 | uint32_t num; |
| 145 | int ret; |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 146 | |
| 147 | assert(header_ep); |
Daniel Boulby | c5259cc | 2018-05-15 11:41:55 +0100 | [diff] [blame] | 148 | header = (optee_header_t *)header_ep->pc; |
| 149 | assert(header); |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 150 | |
Victor Chong | eb3dcd6 | 2018-01-26 16:10:07 +0900 | [diff] [blame] | 151 | /* Print the OPTEE header information */ |
| 152 | INFO("OPTEE ep=0x%x\n", (unsigned int)header_ep->pc); |
| 153 | INFO("OPTEE header info:\n"); |
Daniel Boulby | c5259cc | 2018-05-15 11:41:55 +0100 | [diff] [blame] | 154 | INFO(" magic=0x%x\n", header->magic); |
| 155 | INFO(" version=0x%x\n", header->version); |
| 156 | INFO(" arch=0x%x\n", header->arch); |
| 157 | INFO(" flags=0x%x\n", header->flags); |
| 158 | INFO(" nb_images=0x%x\n", header->nb_images); |
Victor Chong | eb3dcd6 | 2018-01-26 16:10:07 +0900 | [diff] [blame] | 159 | |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 160 | /* |
| 161 | * OPTEE image has 3 types: |
| 162 | * |
| 163 | * 1. Plain OPTEE bin without header. |
| 164 | * Original bin without header, return directly, |
| 165 | * BL32_EXTRA1_IMAGE_ID and BL32_EXTRA2_IMAGE_ID will be skipped. |
| 166 | * |
| 167 | * 2. OPTEE bin with header bin, but no paging. |
| 168 | * Header available and nb_images = 1, remove skip attr for |
| 169 | * BL32_EXTRA1_IMAGE_ID. BL32_EXTRA1_IMAGE_ID will be loaded, |
| 170 | * and BL32_EXTRA2_IMAGE_ID be skipped. |
| 171 | * |
| 172 | * 3. OPTEE image with paging support. |
| 173 | * Header available and nb_images = 2, there are 3 bins: header, |
| 174 | * pager and pageable. Remove skip attr for BL32_EXTRA1_IMAGE_ID |
| 175 | * and BL32_EXTRA2_IMAGE_ID to load pager and paged bin. |
| 176 | */ |
Daniel Boulby | c5259cc | 2018-05-15 11:41:55 +0100 | [diff] [blame] | 177 | if (!tee_validate_header(header)) { |
Etienne Carriere | dc8bbb4 | 2018-02-05 10:42:42 +0100 | [diff] [blame] | 178 | INFO("Invalid OPTEE header, set legacy mode.\n"); |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 179 | #ifdef __aarch64__ |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 180 | header_ep->args.arg0 = MODE_RW_64; |
Etienne Carriere | dc8bbb4 | 2018-02-05 10:42:42 +0100 | [diff] [blame] | 181 | #else |
| 182 | header_ep->args.arg0 = MODE_RW_32; |
| 183 | #endif |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 184 | return 0; |
| 185 | } |
| 186 | |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 187 | /* Parse OPTEE image */ |
Yann Gautier | 1f6fd3d | 2020-11-10 14:47:04 +0100 | [diff] [blame] | 188 | for (num = 0U; num < header->nb_images; num++) { |
Daniel Boulby | c5259cc | 2018-05-15 11:41:55 +0100 | [diff] [blame] | 189 | if (header->optee_image_list[num].image_id == |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 190 | OPTEE_PAGER_IMAGE_ID) { |
| 191 | ret = parse_optee_image(pager_image_info, |
Daniel Boulby | c5259cc | 2018-05-15 11:41:55 +0100 | [diff] [blame] | 192 | &header->optee_image_list[num]); |
| 193 | } else if (header->optee_image_list[num].image_id == |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 194 | OPTEE_PAGED_IMAGE_ID) { |
Yann Gautier | b3f7d75 | 2022-06-20 11:24:22 +0200 | [diff] [blame] | 195 | if (paged_image_info == NULL) { |
| 196 | if (header->optee_image_list[num].size != 0U) { |
| 197 | ERROR("Paged image is not supported\n"); |
| 198 | return -1; |
| 199 | } |
| 200 | |
| 201 | continue; |
| 202 | } else { |
| 203 | ret = parse_optee_image(paged_image_info, |
| 204 | &header->optee_image_list[num]); |
| 205 | } |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 206 | } else { |
| 207 | ERROR("Parse optee image failed.\n"); |
| 208 | return -1; |
| 209 | } |
| 210 | |
| 211 | if (ret != 0) |
| 212 | return -1; |
| 213 | } |
| 214 | |
| 215 | /* |
| 216 | * Update "pc" value which should comes from pager image. After the |
| 217 | * header image is parsed, it will be unuseful, and the actual |
| 218 | * execution image after BL31 is pager image. |
| 219 | */ |
| 220 | header_ep->pc = pager_image_info->image_base; |
| 221 | |
| 222 | /* |
| 223 | * The paged load address and size are populated in |
| 224 | * header image arguments so that can be read by the |
| 225 | * BL32 SPD. |
| 226 | */ |
Yann Gautier | b3f7d75 | 2022-06-20 11:24:22 +0200 | [diff] [blame] | 227 | if (paged_image_info != NULL) { |
| 228 | header_ep->args.arg1 = paged_image_info->image_base; |
| 229 | header_ep->args.arg2 = paged_image_info->image_size; |
| 230 | } |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 231 | |
| 232 | /* Set OPTEE runtime arch - aarch32/aarch64 */ |
Daniel Boulby | c5259cc | 2018-05-15 11:41:55 +0100 | [diff] [blame] | 233 | if (header->arch == 0) { |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 234 | header_ep->args.arg0 = MODE_RW_32; |
Etienne Carriere | dc8bbb4 | 2018-02-05 10:42:42 +0100 | [diff] [blame] | 235 | } else { |
Julius Werner | 8e0ef0f | 2019-07-09 14:02:43 -0700 | [diff] [blame] | 236 | #ifdef __aarch64__ |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 237 | header_ep->args.arg0 = MODE_RW_64; |
Etienne Carriere | dc8bbb4 | 2018-02-05 10:42:42 +0100 | [diff] [blame] | 238 | #else |
| 239 | ERROR("Cannot boot an AArch64 OP-TEE\n"); |
| 240 | return -1; |
| 241 | #endif |
| 242 | } |
Summer Qin | 9db8f2e | 2017-04-24 16:49:28 +0100 | [diff] [blame] | 243 | |
| 244 | return 0; |
| 245 | } |