1 /*
2 * Copyright (c) 2017-2020, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <assert.h>
8
9 #include <common/debug.h>
10 #include <lib/optee_utils.h>
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
26
27 #define OPTEE_MAX_NUM_IMAGES 2u
28
29 #define TEE_MAGIC_NUM_OPTEE 0x4554504f
30 /*
31 * magic: header magic number.
32 * version: OPTEE header version:
33 * 1 - not supported
34 * 2 - supported
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;
45 optee_image_t optee_image_list[];
46 } optee_header_t;
47
48 /*******************************************************************************
49 * Check if it is a valid tee header
50 * Return 1 if valid
51 * Return 0 if invalid
52 ******************************************************************************/
tee_validate_header(optee_header_t * header)53 static inline int tee_validate_header(optee_header_t *header)
54 {
55 int valid = 0;
56
57 if ((header->magic == TEE_MAGIC_NUM_OPTEE) &&
58 (header->version == 2u) &&
59 (header->nb_images > 0u) &&
60 (header->nb_images <= OPTEE_MAX_NUM_IMAGES)) {
61 valid = 1;
62 }
63
64 else {
65 WARN("Not a known TEE, use default loading options.\n");
66 }
67
68 return valid;
69 }
70
71 /*******************************************************************************
72 * Parse the OPTEE image
73 * Return 0 on success or a negative error code otherwise.
74 ******************************************************************************/
parse_optee_image(image_info_t * image_info,optee_image_t * image)75 static int parse_optee_image(image_info_t *image_info,
76 optee_image_t *image)
77 {
78 uintptr_t init_load_addr, free_end, requested_end;
79 size_t init_size;
80
81 init_load_addr = ((uint64_t)image->load_addr_hi << 32) |
82 image->load_addr_lo;
83 init_size = image->size;
84
85 /*
86 * -1 indicates loader decided address; take our pre-mapped area
87 * for current image since arm-tf could not allocate memory dynamically
88 */
89 if (init_load_addr == -1)
90 init_load_addr = image_info->image_base;
91
92 /* Check that the default end address doesn't overflow */
93 if (check_uptr_overflow(image_info->image_base,
94 image_info->image_max_size - 1))
95 return -1;
96 free_end = image_info->image_base + (image_info->image_max_size - 1);
97
98 /* Check that the image end address doesn't overflow */
99 if (check_uptr_overflow(init_load_addr, init_size - 1))
100 return -1;
101 requested_end = init_load_addr + (init_size - 1);
102 /*
103 * Check that the requested RAM location is within reserved
104 * space for OPTEE.
105 */
106 if (!((init_load_addr >= image_info->image_base) &&
107 (requested_end <= free_end))) {
108 WARN("The load address in optee header %p - %p is not in reserved area: %p - %p.\n",
109 (void *)init_load_addr,
110 (void *)(init_load_addr + init_size),
111 (void *)image_info->image_base,
112 (void *)(image_info->image_base +
113 image_info->image_max_size));
114 return -1;
115 }
116
117 /*
118 * Remove the skip attr from image_info, the image will be loaded.
119 * The default attr in image_info is "IMAGE_ATTRIB_SKIP_LOADING", which
120 * mean the image will not be loaded. Here, we parse the header image to
121 * know that the extra image need to be loaded, so remove the skip attr.
122 */
123 image_info->h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;
124
125 /* Update image base and size of image_info */
126 image_info->image_base = init_load_addr;
127 image_info->image_size = init_size;
128
129 return 0;
130 }
131
132 /*******************************************************************************
133 * Parse the OPTEE header
134 * Return 0 on success or a negative error code otherwise.
135 ******************************************************************************/
parse_optee_header(entry_point_info_t * header_ep,image_info_t * pager_image_info,image_info_t * paged_image_info)136 int parse_optee_header(entry_point_info_t *header_ep,
137 image_info_t *pager_image_info,
138 image_info_t *paged_image_info)
139
140 {
141 optee_header_t *header;
142 int num, ret;
143
144 assert(header_ep);
145 header = (optee_header_t *)header_ep->pc;
146 assert(header);
147
148 /* Print the OPTEE header information */
149 INFO("OPTEE ep=0x%x\n", (unsigned int)header_ep->pc);
150 INFO("OPTEE header info:\n");
151 INFO(" magic=0x%x\n", header->magic);
152 INFO(" version=0x%x\n", header->version);
153 INFO(" arch=0x%x\n", header->arch);
154 INFO(" flags=0x%x\n", header->flags);
155 INFO(" nb_images=0x%x\n", header->nb_images);
156
157 /*
158 * OPTEE image has 3 types:
159 *
160 * 1. Plain OPTEE bin without header.
161 * Original bin without header, return directly,
162 * BL32_EXTRA1_IMAGE_ID and BL32_EXTRA2_IMAGE_ID will be skipped.
163 *
164 * 2. OPTEE bin with header bin, but no paging.
165 * Header available and nb_images = 1, remove skip attr for
166 * BL32_EXTRA1_IMAGE_ID. BL32_EXTRA1_IMAGE_ID will be loaded,
167 * and BL32_EXTRA2_IMAGE_ID be skipped.
168 *
169 * 3. OPTEE image with paging support.
170 * Header available and nb_images = 2, there are 3 bins: header,
171 * pager and pageable. Remove skip attr for BL32_EXTRA1_IMAGE_ID
172 * and BL32_EXTRA2_IMAGE_ID to load pager and paged bin.
173 */
174 if (!tee_validate_header(header)) {
175 INFO("Invalid OPTEE header, set legacy mode.\n");
176 #ifdef __aarch64__
177 header_ep->args.arg0 = MODE_RW_64;
178 #else
179 header_ep->args.arg0 = MODE_RW_32;
180 #endif
181 return 0;
182 }
183
184 /* Parse OPTEE image */
185 for (num = 0; num < header->nb_images; num++) {
186 if (header->optee_image_list[num].image_id ==
187 OPTEE_PAGER_IMAGE_ID) {
188 ret = parse_optee_image(pager_image_info,
189 &header->optee_image_list[num]);
190 } else if (header->optee_image_list[num].image_id ==
191 OPTEE_PAGED_IMAGE_ID) {
192 ret = parse_optee_image(paged_image_info,
193 &header->optee_image_list[num]);
194 } else {
195 ERROR("Parse optee image failed.\n");
196 return -1;
197 }
198
199 if (ret != 0)
200 return -1;
201 }
202
203 /*
204 * Update "pc" value which should comes from pager image. After the
205 * header image is parsed, it will be unuseful, and the actual
206 * execution image after BL31 is pager image.
207 */
208 header_ep->pc = pager_image_info->image_base;
209
210 /*
211 * The paged load address and size are populated in
212 * header image arguments so that can be read by the
213 * BL32 SPD.
214 */
215 header_ep->args.arg1 = paged_image_info->image_base;
216 header_ep->args.arg2 = paged_image_info->image_size;
217
218 /* Set OPTEE runtime arch - aarch32/aarch64 */
219 if (header->arch == 0) {
220 header_ep->args.arg0 = MODE_RW_32;
221 } else {
222 #ifdef __aarch64__
223 header_ep->args.arg0 = MODE_RW_64;
224 #else
225 ERROR("Cannot boot an AArch64 OP-TEE\n");
226 return -1;
227 #endif
228 }
229
230 return 0;
231 }
232