arm_io_storage.c 6.86 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2017, ARM Limited and Contributors. All rights reserved.
3
 *
dp-arm's avatar
dp-arm committed
4
 * SPDX-License-Identifier: BSD-3-Clause
5
6
7
 */
#include <assert.h>
#include <debug.h>
8
#include <firmware_image_package.h>
9
10
11
12
13
14
#include <io_driver.h>
#include <io_fip.h>
#include <io_memmap.h>
#include <io_storage.h>
#include <platform_def.h>
#include <string.h>
15
#include <utils.h>
16
17
18
19
20
21
22
23
24
25
26
27

/* IO devices */
static const io_dev_connector_t *fip_dev_con;
static uintptr_t fip_dev_handle;
static const io_dev_connector_t *memmap_dev_con;
static uintptr_t memmap_dev_handle;

static const io_block_spec_t fip_block_spec = {
	.offset = PLAT_ARM_FIP_BASE,
	.length = PLAT_ARM_FIP_MAX_SIZE
};

28
29
static const io_uuid_spec_t bl2_uuid_spec = {
	.uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
30
31
};

32
33
static const io_uuid_spec_t scp_bl2_uuid_spec = {
	.uuid = UUID_SCP_FIRMWARE_SCP_BL2,
34
35
};

36
37
static const io_uuid_spec_t bl31_uuid_spec = {
	.uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
38
39
};

40
41
static const io_uuid_spec_t bl32_uuid_spec = {
	.uuid = UUID_SECURE_PAYLOAD_BL32,
42
43
};

44
45
46
47
48
49
50
51
static const io_uuid_spec_t bl32_extra1_uuid_spec = {
	.uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
};

static const io_uuid_spec_t bl32_extra2_uuid_spec = {
	.uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
};

52
53
static const io_uuid_spec_t bl33_uuid_spec = {
	.uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
54
55
56
};

#if TRUSTED_BOARD_BOOT
57
58
static const io_uuid_spec_t tb_fw_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_BOOT_FW_CERT,
59
60
};

61
62
static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_KEY_CERT,
63
64
};

65
66
static const io_uuid_spec_t scp_fw_key_cert_uuid_spec = {
	.uuid = UUID_SCP_FW_KEY_CERT,
67
68
};

69
70
static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
	.uuid = UUID_SOC_FW_KEY_CERT,
71
72
};

73
74
static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
75
76
};

77
78
static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
	.uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
79
80
};

81
82
static const io_uuid_spec_t scp_fw_cert_uuid_spec = {
	.uuid = UUID_SCP_FW_CONTENT_CERT,
83
84
};

85
86
static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
	.uuid = UUID_SOC_FW_CONTENT_CERT,
87
88
};

89
90
static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
91
92
};

93
94
static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
	.uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
95
96
97
};
#endif /* TRUSTED_BOARD_BOOT */

98

99
100
101
102
103
104
105
106
107
static int open_fip(const uintptr_t spec);
static int open_memmap(const uintptr_t spec);

struct plat_io_policy {
	uintptr_t *dev_handle;
	uintptr_t image_spec;
	int (*check)(const uintptr_t spec);
};

108
/* By default, ARM platforms load images from the FIP */
109
static const struct plat_io_policy policies[] = {
110
	[FIP_IMAGE_ID] = {
111
112
113
		&memmap_dev_handle,
		(uintptr_t)&fip_block_spec,
		open_memmap
114
115
	},
	[BL2_IMAGE_ID] = {
116
		&fip_dev_handle,
117
		(uintptr_t)&bl2_uuid_spec,
118
		open_fip
119
	},
120
	[SCP_BL2_IMAGE_ID] = {
121
		&fip_dev_handle,
122
		(uintptr_t)&scp_bl2_uuid_spec,
123
		open_fip
124
125
	},
	[BL31_IMAGE_ID] = {
126
		&fip_dev_handle,
127
		(uintptr_t)&bl31_uuid_spec,
128
		open_fip
129
130
	},
	[BL32_IMAGE_ID] = {
131
		&fip_dev_handle,
132
		(uintptr_t)&bl32_uuid_spec,
133
		open_fip
134
	},
135
136
137
138
139
140
141
142
143
144
	[BL32_EXTRA1_IMAGE_ID] = {
		&fip_dev_handle,
		(uintptr_t)&bl32_extra1_uuid_spec,
		open_fip
	},
	[BL32_EXTRA2_IMAGE_ID] = {
		&fip_dev_handle,
		(uintptr_t)&bl32_extra2_uuid_spec,
		open_fip
	},
145
	[BL33_IMAGE_ID] = {
146
		&fip_dev_handle,
147
		(uintptr_t)&bl33_uuid_spec,
148
		open_fip
149
	},
150
#if TRUSTED_BOARD_BOOT
151
	[TRUSTED_BOOT_FW_CERT_ID] = {
152
		&fip_dev_handle,
153
		(uintptr_t)&tb_fw_cert_uuid_spec,
154
		open_fip
155
156
	},
	[TRUSTED_KEY_CERT_ID] = {
157
		&fip_dev_handle,
158
		(uintptr_t)&trusted_key_cert_uuid_spec,
159
		open_fip
160
	},
161
	[SCP_FW_KEY_CERT_ID] = {
162
		&fip_dev_handle,
163
		(uintptr_t)&scp_fw_key_cert_uuid_spec,
164
		open_fip
165
	},
166
	[SOC_FW_KEY_CERT_ID] = {
167
		&fip_dev_handle,
168
		(uintptr_t)&soc_fw_key_cert_uuid_spec,
169
		open_fip
170
	},
171
	[TRUSTED_OS_FW_KEY_CERT_ID] = {
172
		&fip_dev_handle,
173
		(uintptr_t)&tos_fw_key_cert_uuid_spec,
174
		open_fip
175
	},
176
	[NON_TRUSTED_FW_KEY_CERT_ID] = {
177
		&fip_dev_handle,
178
		(uintptr_t)&nt_fw_key_cert_uuid_spec,
179
		open_fip
180
	},
181
	[SCP_FW_CONTENT_CERT_ID] = {
182
		&fip_dev_handle,
183
		(uintptr_t)&scp_fw_cert_uuid_spec,
184
		open_fip
185
	},
186
	[SOC_FW_CONTENT_CERT_ID] = {
187
		&fip_dev_handle,
188
		(uintptr_t)&soc_fw_cert_uuid_spec,
189
		open_fip
190
	},
191
	[TRUSTED_OS_FW_CONTENT_CERT_ID] = {
192
		&fip_dev_handle,
193
		(uintptr_t)&tos_fw_cert_uuid_spec,
194
		open_fip
195
	},
196
	[NON_TRUSTED_FW_CONTENT_CERT_ID] = {
197
		&fip_dev_handle,
198
		(uintptr_t)&nt_fw_cert_uuid_spec,
199
		open_fip
200
	},
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#endif /* TRUSTED_BOARD_BOOT */
};


/* Weak definitions may be overridden in specific ARM standard platform */
#pragma weak plat_arm_io_setup
#pragma weak plat_arm_get_alt_image_source


static int open_fip(const uintptr_t spec)
{
	int result;
	uintptr_t local_image_handle;

	/* See if a Firmware Image Package is available */
216
	result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
217
	if (result == 0) {
218
		result = io_open(fip_dev_handle, spec, &local_image_handle);
219
		if (result == 0) {
220
221
222
223
224
225
226
227
228
229
230
231
232
233
			VERBOSE("Using FIP\n");
			io_close(local_image_handle);
		}
	}
	return result;
}


static int open_memmap(const uintptr_t spec)
{
	int result;
	uintptr_t local_image_handle;

	result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL);
234
	if (result == 0) {
235
		result = io_open(memmap_dev_handle, spec, &local_image_handle);
236
		if (result == 0) {
237
238
239
240
241
242
243
244
245
246
247
248
249
			VERBOSE("Using Memmap\n");
			io_close(local_image_handle);
		}
	}
	return result;
}


void arm_io_setup(void)
{
	int io_result;

	io_result = register_io_dev_fip(&fip_dev_con);
250
	assert(io_result == 0);
251
252

	io_result = register_io_dev_memmap(&memmap_dev_con);
253
	assert(io_result == 0);
254
255
256
257

	/* Open connections to devices and cache the handles */
	io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
				&fip_dev_handle);
258
	assert(io_result == 0);
259
260
261

	io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
				&memmap_dev_handle);
262
	assert(io_result == 0);
263
264
265
266
267
268
269
270
271
272
273

	/* Ignore improbable errors in release builds */
	(void)io_result;
}

void plat_arm_io_setup(void)
{
	arm_io_setup();
}

int plat_arm_get_alt_image_source(
274
275
276
	unsigned int image_id __unused,
	uintptr_t *dev_handle __unused,
	uintptr_t *image_spec __unused)
277
278
{
	/* By default do not try an alternative */
279
	return -ENOENT;
280
281
282
283
}

/* Return an IO device handle and specification which can be used to access
 * an image. Use this to enforce platform load policy */
284
int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
285
286
			  uintptr_t *image_spec)
{
287
	int result;
288
289
	const struct plat_io_policy *policy;

290
291
292
293
	assert(image_id < ARRAY_SIZE(policies));

	policy = &policies[image_id];
	result = policy->check(policy->image_spec);
294
	if (result == 0) {
295
296
		*image_spec = policy->image_spec;
		*dev_handle = *(policy->dev_handle);
297
	} else {
298
299
300
		VERBOSE("Trying alternative IO\n");
		result = plat_arm_get_alt_image_source(image_id, dev_handle,
						       image_spec);
301
	}
302

303
304
	return result;
}
305
306
307
308
309
310
311
312
313
314
315
316
317
318

/*
 * See if a Firmware Image Package is available,
 * by checking if TOC is valid or not.
 */
int arm_io_is_toc_valid(void)
{
	int result;

	result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);

	return (result == 0);
}