optee_utils.c 6.55 KB
Newer Older
1
2
3
4
5
6
7
8
/*
 * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
 *
 * SPDX-License-Identifier: BSD-3-Clause
 */

#include <assert.h>
#include <errno.h>
9
10
11
12
13

#include <arch_helpers.h>
#include <common/debug.h>
#include <common/desc_image_load.h>
#include <lib/optee_utils.h>
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

/*
 * load_addr_hi and load_addr_lo: image load address.
 * image_id: 0 - pager, 1 - paged
 * size: image size in bytes.
 */
typedef struct optee_image {
	uint32_t load_addr_hi;
	uint32_t load_addr_lo;
	uint32_t image_id;
	uint32_t size;
} optee_image_t;

#define OPTEE_PAGER_IMAGE_ID		0
#define OPTEE_PAGED_IMAGE_ID		1
29
30

#define OPTEE_MAX_NUM_IMAGES		2u
31
32
33
34
35

#define TEE_MAGIC_NUM_OPTEE		0x4554504f
/*
 * magic: header magic number.
 * version: OPTEE header version:
36
37
 *		1 - not supported
 *		2 - supported
38
39
40
41
42
43
44
45
46
47
 * arch: OPTEE os architecture type: 0 - AARCH32, 1 - AARCH64.
 * flags: unused currently.
 * nb_images: number of images.
 */
typedef struct optee_header {
	uint32_t magic;
	uint8_t version;
	uint8_t arch;
	uint16_t flags;
	uint32_t nb_images;
Daniel Boulby's avatar
Daniel Boulby committed
48
	optee_image_t optee_image_list[];
49
50
51
52
53
54
55
} optee_header_t;

/*******************************************************************************
 * Check if it is a valid tee header
 * Return 1 if valid
 * Return 0 if invalid
 ******************************************************************************/
Daniel Boulby's avatar
Daniel Boulby committed
56
static inline int tee_validate_header(optee_header_t *header)
57
{
58
59
	int valid = 0;

Daniel Boulby's avatar
Daniel Boulby committed
60
	if ((header->magic == TEE_MAGIC_NUM_OPTEE) &&
61
62
63
64
65
66
67
68
		(header->version == 2u) &&
		(header->nb_images > 0u) &&
		(header->nb_images <= OPTEE_MAX_NUM_IMAGES)) {
		valid = 1;
	}

	else {
		WARN("Not a known TEE, use default loading options.\n");
69
70
	}

71
	return valid;
72
73
74
75
76
77
78
}

/*******************************************************************************
 * Parse the OPTEE image
 * Return 0 on success or a negative error code otherwise.
 ******************************************************************************/
static int parse_optee_image(image_info_t *image_info,
Daniel Boulby's avatar
Daniel Boulby committed
79
		optee_image_t *image)
80
81
82
83
{
	uintptr_t init_load_addr, free_end, requested_end;
	size_t init_size;

Daniel Boulby's avatar
Daniel Boulby committed
84
85
86
	init_load_addr = ((uint64_t)image->load_addr_hi << 32) |
					image->load_addr_lo;
	init_size = image->size;
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143

	/*
	 * -1 indicates loader decided address; take our pre-mapped area
	 * for current image since arm-tf could not allocate memory dynamically
	 */
	if (init_load_addr == -1)
		init_load_addr = image_info->image_base;

	/* Check that the default end address doesn't overflow */
	if (check_uptr_overflow(image_info->image_base,
				image_info->image_max_size - 1))
		return -1;
	free_end = image_info->image_base + (image_info->image_max_size - 1);

	/* Check that the image end address doesn't overflow */
	if (check_uptr_overflow(init_load_addr, init_size - 1))
		return -1;
	requested_end = init_load_addr + (init_size - 1);
	/*
	 * Check that the requested RAM location is within reserved
	 * space for OPTEE.
	 */
	if (!((init_load_addr >= image_info->image_base) &&
			(requested_end <= free_end))) {
		WARN("The load address in optee header %p - %p is not in reserved area: %p - %p.\n",
				(void *)init_load_addr,
				(void *)(init_load_addr + init_size),
				(void *)image_info->image_base,
				(void *)(image_info->image_base +
					image_info->image_max_size));
		return -1;
	}

	/*
	 * Remove the skip attr from image_info, the image will be loaded.
	 * The default attr in image_info is "IMAGE_ATTRIB_SKIP_LOADING", which
	 * mean the image will not be loaded. Here, we parse the header image to
	 * know that the extra image need to be loaded, so remove the skip attr.
	 */
	image_info->h.attr &= ~IMAGE_ATTRIB_SKIP_LOADING;

	/* Update image base and size of image_info */
	image_info->image_base = init_load_addr;
	image_info->image_size = init_size;

	return 0;
}

/*******************************************************************************
 * Parse the OPTEE header
 * Return 0 on success or a negative error code otherwise.
 ******************************************************************************/
int parse_optee_header(entry_point_info_t *header_ep,
		image_info_t *pager_image_info,
		image_info_t *paged_image_info)

{
Daniel Boulby's avatar
Daniel Boulby committed
144
	optee_header_t *header;
145
146
147
	int num, ret;

	assert(header_ep);
Daniel Boulby's avatar
Daniel Boulby committed
148
149
	header = (optee_header_t *)header_ep->pc;
	assert(header);
150

151
152
153
	/* Print the OPTEE header information */
	INFO("OPTEE ep=0x%x\n", (unsigned int)header_ep->pc);
	INFO("OPTEE header info:\n");
Daniel Boulby's avatar
Daniel Boulby committed
154
155
156
157
158
	INFO("      magic=0x%x\n", header->magic);
	INFO("      version=0x%x\n", header->version);
	INFO("      arch=0x%x\n", header->arch);
	INFO("      flags=0x%x\n", header->flags);
	INFO("      nb_images=0x%x\n", header->nb_images);
159

160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
	/*
	 * OPTEE image has 3 types:
	 *
	 * 1. Plain OPTEE bin without header.
	 *	Original bin without header, return directly,
	 *	BL32_EXTRA1_IMAGE_ID and BL32_EXTRA2_IMAGE_ID will be skipped.
	 *
	 * 2. OPTEE bin with header bin, but no paging.
	 *	Header available and nb_images = 1, remove skip attr for
	 *	BL32_EXTRA1_IMAGE_ID. BL32_EXTRA1_IMAGE_ID will be loaded,
	 *	and BL32_EXTRA2_IMAGE_ID be skipped.
	 *
	 * 3. OPTEE image with paging support.
	 *	Header available and nb_images = 2, there are 3 bins: header,
	 *	pager and pageable. Remove skip attr for BL32_EXTRA1_IMAGE_ID
	 *	and BL32_EXTRA2_IMAGE_ID to load pager and paged bin.
	 */
Daniel Boulby's avatar
Daniel Boulby committed
177
	if (!tee_validate_header(header)) {
178
179
		INFO("Invalid OPTEE header, set legacy mode.\n");
#ifdef AARCH64
180
		header_ep->args.arg0 = MODE_RW_64;
181
182
183
#else
		header_ep->args.arg0 = MODE_RW_32;
#endif
184
185
186
187
		return 0;
	}

	/* Parse OPTEE image */
Daniel Boulby's avatar
Daniel Boulby committed
188
189
	for (num = 0; num < header->nb_images; num++) {
		if (header->optee_image_list[num].image_id ==
190
191
				OPTEE_PAGER_IMAGE_ID) {
			ret = parse_optee_image(pager_image_info,
Daniel Boulby's avatar
Daniel Boulby committed
192
193
				&header->optee_image_list[num]);
		} else if (header->optee_image_list[num].image_id ==
194
195
				OPTEE_PAGED_IMAGE_ID) {
			ret = parse_optee_image(paged_image_info,
Daniel Boulby's avatar
Daniel Boulby committed
196
				&header->optee_image_list[num]);
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
		} else {
			ERROR("Parse optee image failed.\n");
			return -1;
		}

		if (ret != 0)
			return -1;
	}

	/*
	 * Update "pc" value which should comes from pager image. After the
	 * header image is parsed, it will be unuseful, and the actual
	 * execution image after BL31 is pager image.
	 */
	header_ep->pc =	pager_image_info->image_base;

	/*
	 * The paged load address and size are populated in
	 * header image arguments so that can be read by the
	 * BL32 SPD.
	 */
	header_ep->args.arg1 = paged_image_info->image_base;
	header_ep->args.arg2 = paged_image_info->image_size;

	/* Set OPTEE runtime arch - aarch32/aarch64 */
Daniel Boulby's avatar
Daniel Boulby committed
222
	if (header->arch == 0) {
223
		header_ep->args.arg0 = MODE_RW_32;
224
225
	} else {
#ifdef AARCH64
226
		header_ep->args.arg0 = MODE_RW_64;
227
228
229
230
231
#else
		ERROR("Cannot boot an AArch64 OP-TEE\n");
		return -1;
#endif
	}
232
233
234

	return 0;
}