1 /* 2 * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 */ 6 7 #include <arch_helpers.h> 8 #include <assert.h> 9 #include <debug.h> 10 #include <desc_image_load.h> 11 #include <errno.h> 12 #include <optee_utils.h> 13 14 /* 15 * load_addr_hi and load_addr_lo: image load address. 16 * image_id: 0 - pager, 1 - paged 17 * size: image size in bytes. 18 */ 19 typedef struct optee_image { 20 uint32_t load_addr_hi; 21 uint32_t load_addr_lo; 22 uint32_t image_id; 23 uint32_t size; 24 } optee_image_t; 25 26 #define OPTEE_PAGER_IMAGE_ID 0 27 #define OPTEE_PAGED_IMAGE_ID 1 28 29 #define OPTEE_MAX_NUM_IMAGES 2u 30 31 #define TEE_MAGIC_NUM_OPTEE 0x4554504f 32 /* 33 * magic: header magic number. 34 * version: OPTEE header version: 35 * 1 - not supported 36 * 2 - supported 37 * arch: OPTEE os architecture type: 0 - AARCH32, 1 - AARCH64. 38 * flags: unused currently. 39 * nb_images: number of images. 40 */ 41 typedef struct optee_header { 42 uint32_t magic; 43 uint8_t version; 44 uint8_t arch; 45 uint16_t flags; 46 uint32_t nb_images; 47 optee_image_t optee_image_list[]; 48 } optee_header_t; 49 50 /******************************************************************************* 51 * Check if it is a valid tee header 52 * Return 1 if valid 53 * Return 0 if invalid 54 ******************************************************************************/ 55 static inline int tee_validate_header(optee_header_t *header) 56 { 57 int valid = 0; 58 59 if ((header->magic == TEE_MAGIC_NUM_OPTEE) && 60 (header->version == 2u) && 61 (header->nb_images > 0u) && 62 (header->nb_images <= OPTEE_MAX_NUM_IMAGES)) { 63 valid = 1; 64 } 65 66 else { 67 WARN("Not a known TEE, use default loading options.\n"); 68 } 69 70 return valid; 71 } 72 73 /******************************************************************************* 74 * Parse the OPTEE image 75 * Return 0 on success or a negative error code otherwise. 76 ******************************************************************************/ 77 static int parse_optee_image(image_info_t *image_info, 78 optee_image_t *image) 79 { 80 uintptr_t init_load_addr, free_end, requested_end; 81 size_t init_size; 82 83 init_load_addr = ((uint64_t)image->load_addr_hi << 32) | 84 image->load_addr_lo; 85 init_size = image->size; 86 87 /* 88 * -1 indicates loader decided address; take our pre-mapped area 89 * for current image since arm-tf could not allocate memory dynamically 90 */ 91 if (init_load_addr == -1) 92 init_load_addr = image_info->image_base; 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 { 143 optee_header_t *header; 144 int num, ret; 145 146 assert(header_ep); 147 header = (optee_header_t *)header_ep->pc; 148 assert(header); 149 150 /* Print the OPTEE header information */ 151 INFO("OPTEE ep=0x%x\n", (unsigned int)header_ep->pc); 152 INFO("OPTEE header info:\n"); 153 INFO(" magic=0x%x\n", header->magic); 154 INFO(" version=0x%x\n", header->version); 155 INFO(" arch=0x%x\n", header->arch); 156 INFO(" flags=0x%x\n", header->flags); 157 INFO(" nb_images=0x%x\n", header->nb_images); 158 159 /* 160 * OPTEE image has 3 types: 161 * 162 * 1. Plain OPTEE bin without header. 163 * Original bin without header, return directly, 164 * BL32_EXTRA1_IMAGE_ID and BL32_EXTRA2_IMAGE_ID will be skipped. 165 * 166 * 2. OPTEE bin with header bin, but no paging. 167 * Header available and nb_images = 1, remove skip attr for 168 * BL32_EXTRA1_IMAGE_ID. BL32_EXTRA1_IMAGE_ID will be loaded, 169 * and BL32_EXTRA2_IMAGE_ID be skipped. 170 * 171 * 3. OPTEE image with paging support. 172 * Header available and nb_images = 2, there are 3 bins: header, 173 * pager and pageable. Remove skip attr for BL32_EXTRA1_IMAGE_ID 174 * and BL32_EXTRA2_IMAGE_ID to load pager and paged bin. 175 */ 176 if (!tee_validate_header(header)) { 177 INFO("Invalid OPTEE header, set legacy mode.\n"); 178 #ifdef AARCH64 179 header_ep->args.arg0 = MODE_RW_64; 180 #else 181 header_ep->args.arg0 = MODE_RW_32; 182 #endif 183 return 0; 184 } 185 186 /* Parse OPTEE image */ 187 for (num = 0; num < header->nb_images; num++) { 188 if (header->optee_image_list[num].image_id == 189 OPTEE_PAGER_IMAGE_ID) { 190 ret = parse_optee_image(pager_image_info, 191 &header->optee_image_list[num]); 192 } else if (header->optee_image_list[num].image_id == 193 OPTEE_PAGED_IMAGE_ID) { 194 ret = parse_optee_image(paged_image_info, 195 &header->optee_image_list[num]); 196 } else { 197 ERROR("Parse optee image failed.\n"); 198 return -1; 199 } 200 201 if (ret != 0) 202 return -1; 203 } 204 205 /* 206 * Update "pc" value which should comes from pager image. After the 207 * header image is parsed, it will be unuseful, and the actual 208 * execution image after BL31 is pager image. 209 */ 210 header_ep->pc = pager_image_info->image_base; 211 212 /* 213 * The paged load address and size are populated in 214 * header image arguments so that can be read by the 215 * BL32 SPD. 216 */ 217 header_ep->args.arg1 = paged_image_info->image_base; 218 header_ep->args.arg2 = paged_image_info->image_size; 219 220 /* Set OPTEE runtime arch - aarch32/aarch64 */ 221 if (header->arch == 0) { 222 header_ep->args.arg0 = MODE_RW_32; 223 } else { 224 #ifdef AARCH64 225 header_ep->args.arg0 = MODE_RW_64; 226 #else 227 ERROR("Cannot boot an AArch64 OP-TEE\n"); 228 return -1; 229 #endif 230 } 231 232 return 0; 233 } 234