arm_io_storage.c 7.79 KB
Newer Older
1
/*
2
 * Copyright (c) 2015-2018, 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
#include <io_driver.h>
#include <io_fip.h>
#include <io_memmap.h>
#include <io_storage.h>
Roberto Vargas's avatar
Roberto Vargas committed
13
14
#include <plat_arm.h>
#include <platform.h>
15
16
#include <platform_def.h>
#include <string.h>
17
#include <utils.h>
18
19
20
21
22
23
24
25
26
27
28
29

/* 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
};

30
31
static const io_uuid_spec_t bl2_uuid_spec = {
	.uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
32
33
};

34
35
static const io_uuid_spec_t scp_bl2_uuid_spec = {
	.uuid = UUID_SCP_FIRMWARE_SCP_BL2,
36
37
};

38
39
static const io_uuid_spec_t bl31_uuid_spec = {
	.uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
40
41
};

42
43
static const io_uuid_spec_t bl32_uuid_spec = {
	.uuid = UUID_SECURE_PAYLOAD_BL32,
44
45
};

46
47
48
49
50
51
52
53
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,
};

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

58
59
60
61
static const io_uuid_spec_t tb_fw_config_uuid_spec = {
	.uuid = UUID_TB_FW_CONFIG,
};

62
63
64
65
static const io_uuid_spec_t hw_config_uuid_spec = {
	.uuid = UUID_HW_CONFIG,
};

66
67
68
69
70
71
72
73
74
75
76
77
static const io_uuid_spec_t soc_fw_config_uuid_spec = {
	.uuid = UUID_SOC_FW_CONFIG,
};

static const io_uuid_spec_t tos_fw_config_uuid_spec = {
	.uuid = UUID_TOS_FW_CONFIG,
};

static const io_uuid_spec_t nt_fw_config_uuid_spec = {
	.uuid = UUID_NT_FW_CONFIG,
};

78
#if TRUSTED_BOARD_BOOT
79
80
static const io_uuid_spec_t tb_fw_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_BOOT_FW_CERT,
81
82
};

83
84
static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_KEY_CERT,
85
86
};

87
88
static const io_uuid_spec_t scp_fw_key_cert_uuid_spec = {
	.uuid = UUID_SCP_FW_KEY_CERT,
89
90
};

91
92
static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
	.uuid = UUID_SOC_FW_KEY_CERT,
93
94
};

95
96
static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
97
98
};

99
100
static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
	.uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
101
102
};

103
104
static const io_uuid_spec_t scp_fw_cert_uuid_spec = {
	.uuid = UUID_SCP_FW_CONTENT_CERT,
105
106
};

107
108
static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
	.uuid = UUID_SOC_FW_CONTENT_CERT,
109
110
};

111
112
static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
113
114
};

115
116
static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
	.uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
117
118
119
};
#endif /* TRUSTED_BOARD_BOOT */

120

121
122
123
124
125
126
127
128
129
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);
};

130
/* By default, ARM platforms load images from the FIP */
131
static const struct plat_io_policy policies[] = {
132
	[FIP_IMAGE_ID] = {
133
134
135
		&memmap_dev_handle,
		(uintptr_t)&fip_block_spec,
		open_memmap
136
137
	},
	[BL2_IMAGE_ID] = {
138
		&fip_dev_handle,
139
		(uintptr_t)&bl2_uuid_spec,
140
		open_fip
141
	},
142
	[SCP_BL2_IMAGE_ID] = {
143
		&fip_dev_handle,
144
		(uintptr_t)&scp_bl2_uuid_spec,
145
		open_fip
146
147
	},
	[BL31_IMAGE_ID] = {
148
		&fip_dev_handle,
149
		(uintptr_t)&bl31_uuid_spec,
150
		open_fip
151
152
	},
	[BL32_IMAGE_ID] = {
153
		&fip_dev_handle,
154
		(uintptr_t)&bl32_uuid_spec,
155
		open_fip
156
	},
157
158
159
160
161
162
163
164
165
166
	[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
	},
167
	[BL33_IMAGE_ID] = {
168
		&fip_dev_handle,
169
		(uintptr_t)&bl33_uuid_spec,
170
		open_fip
171
	},
172
173
174
175
176
	[TB_FW_CONFIG_ID] = {
		&fip_dev_handle,
		(uintptr_t)&tb_fw_config_uuid_spec,
		open_fip
	},
177
178
179
180
181
	[HW_CONFIG_ID] = {
		&fip_dev_handle,
		(uintptr_t)&hw_config_uuid_spec,
		open_fip
	},
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
	[SOC_FW_CONFIG_ID] = {
			&fip_dev_handle,
			(uintptr_t)&soc_fw_config_uuid_spec,
			open_fip
	},
	[TOS_FW_CONFIG_ID] = {
		&fip_dev_handle,
		(uintptr_t)&tos_fw_config_uuid_spec,
		open_fip
	},
	[NT_FW_CONFIG_ID] = {
		&fip_dev_handle,
		(uintptr_t)&nt_fw_config_uuid_spec,
		open_fip
	},
197
#if TRUSTED_BOARD_BOOT
198
	[TRUSTED_BOOT_FW_CERT_ID] = {
199
		&fip_dev_handle,
200
		(uintptr_t)&tb_fw_cert_uuid_spec,
201
		open_fip
202
203
	},
	[TRUSTED_KEY_CERT_ID] = {
204
		&fip_dev_handle,
205
		(uintptr_t)&trusted_key_cert_uuid_spec,
206
		open_fip
207
	},
208
	[SCP_FW_KEY_CERT_ID] = {
209
		&fip_dev_handle,
210
		(uintptr_t)&scp_fw_key_cert_uuid_spec,
211
		open_fip
212
	},
213
	[SOC_FW_KEY_CERT_ID] = {
214
		&fip_dev_handle,
215
		(uintptr_t)&soc_fw_key_cert_uuid_spec,
216
		open_fip
217
	},
218
	[TRUSTED_OS_FW_KEY_CERT_ID] = {
219
		&fip_dev_handle,
220
		(uintptr_t)&tos_fw_key_cert_uuid_spec,
221
		open_fip
222
	},
223
	[NON_TRUSTED_FW_KEY_CERT_ID] = {
224
		&fip_dev_handle,
225
		(uintptr_t)&nt_fw_key_cert_uuid_spec,
226
		open_fip
227
	},
228
	[SCP_FW_CONTENT_CERT_ID] = {
229
		&fip_dev_handle,
230
		(uintptr_t)&scp_fw_cert_uuid_spec,
231
		open_fip
232
	},
233
	[SOC_FW_CONTENT_CERT_ID] = {
234
		&fip_dev_handle,
235
		(uintptr_t)&soc_fw_cert_uuid_spec,
236
		open_fip
237
	},
238
	[TRUSTED_OS_FW_CONTENT_CERT_ID] = {
239
		&fip_dev_handle,
240
		(uintptr_t)&tos_fw_cert_uuid_spec,
241
		open_fip
242
	},
243
	[NON_TRUSTED_FW_CONTENT_CERT_ID] = {
244
		&fip_dev_handle,
245
		(uintptr_t)&nt_fw_cert_uuid_spec,
246
		open_fip
247
	},
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
#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 */
263
	result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
264
	if (result == 0) {
265
		result = io_open(fip_dev_handle, spec, &local_image_handle);
266
		if (result == 0) {
267
268
269
270
271
272
273
274
275
276
277
278
279
280
			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);
281
	if (result == 0) {
282
		result = io_open(memmap_dev_handle, spec, &local_image_handle);
283
		if (result == 0) {
284
285
286
287
288
289
290
291
292
293
294
295
296
			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);
297
	assert(io_result == 0);
298
299

	io_result = register_io_dev_memmap(&memmap_dev_con);
300
	assert(io_result == 0);
301
302
303
304

	/* Open connections to devices and cache the handles */
	io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
				&fip_dev_handle);
305
	assert(io_result == 0);
306
307
308

	io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
				&memmap_dev_handle);
309
	assert(io_result == 0);
310
311
312
313
314
315
316
317
318
319
320

	/* 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(
321
322
323
	unsigned int image_id __unused,
	uintptr_t *dev_handle __unused,
	uintptr_t *image_spec __unused)
324
325
{
	/* By default do not try an alternative */
326
	return -ENOENT;
327
328
329
330
}

/* Return an IO device handle and specification which can be used to access
 * an image. Use this to enforce platform load policy */
331
int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
332
333
			  uintptr_t *image_spec)
{
334
	int result;
335
336
	const struct plat_io_policy *policy;

337
338
339
340
	assert(image_id < ARRAY_SIZE(policies));

	policy = &policies[image_id];
	result = policy->check(policy->image_spec);
341
	if (result == 0) {
342
343
		*image_spec = policy->image_spec;
		*dev_handle = *(policy->dev_handle);
344
	} else {
345
346
347
		VERBOSE("Trying alternative IO\n");
		result = plat_arm_get_alt_image_source(image_id, dev_handle,
						       image_spec);
348
	}
349

350
351
	return result;
}
352
353
354
355
356
357
358
359
360
361
362
363
364
365

/*
 * 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);
}