arm_io_storage.c 7.89 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
8
#include <assert.h>
#include <string.h>
9
10
11
12
13
14
15
16
17

#include <platform_def.h>

#include <common/debug.h>
#include <drivers/io/io_driver.h>
#include <drivers/io/io_fip.h>
#include <drivers/io/io_memmap.h>
#include <drivers/io/io_storage.h>
#include <lib/utils.h>
18
#include <plat/arm/common/plat_arm.h>
19
20
21
#include <plat/common/platform.h>
#include <tools_share/firmware_image_package.h>

22
23
24
25
26
27
28
29
30
31
32
/* 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
};

33
34
static const io_uuid_spec_t bl2_uuid_spec = {
	.uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
35
36
};

37
38
static const io_uuid_spec_t scp_bl2_uuid_spec = {
	.uuid = UUID_SCP_FIRMWARE_SCP_BL2,
39
40
};

41
42
static const io_uuid_spec_t bl31_uuid_spec = {
	.uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
43
44
};

45
46
static const io_uuid_spec_t bl32_uuid_spec = {
	.uuid = UUID_SECURE_PAYLOAD_BL32,
47
48
};

49
50
51
52
53
54
55
56
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,
};

57
58
static const io_uuid_spec_t bl33_uuid_spec = {
	.uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
59
60
};

61
62
63
64
static const io_uuid_spec_t tb_fw_config_uuid_spec = {
	.uuid = UUID_TB_FW_CONFIG,
};

65
66
67
68
static const io_uuid_spec_t hw_config_uuid_spec = {
	.uuid = UUID_HW_CONFIG,
};

69
70
71
72
73
74
75
76
77
78
79
80
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,
};

81
#if TRUSTED_BOARD_BOOT
82
83
static const io_uuid_spec_t tb_fw_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_BOOT_FW_CERT,
84
85
};

86
87
static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_KEY_CERT,
88
89
};

90
91
static const io_uuid_spec_t scp_fw_key_cert_uuid_spec = {
	.uuid = UUID_SCP_FW_KEY_CERT,
92
93
};

94
95
static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
	.uuid = UUID_SOC_FW_KEY_CERT,
96
97
};

98
99
static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
100
101
};

102
103
static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
	.uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
104
105
};

106
107
static const io_uuid_spec_t scp_fw_cert_uuid_spec = {
	.uuid = UUID_SCP_FW_CONTENT_CERT,
108
109
};

110
111
static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
	.uuid = UUID_SOC_FW_CONTENT_CERT,
112
113
};

114
115
static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
116
117
};

118
119
static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
	.uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
120
121
122
};
#endif /* TRUSTED_BOARD_BOOT */

123

124
125
126
127
128
129
130
131
132
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);
};

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

	io_result = register_io_dev_memmap(&memmap_dev_con);
303
	assert(io_result == 0);
304
305
306
307

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

	io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
				&memmap_dev_handle);
312
	assert(io_result == 0);
313
314
315
316
317
318
319
320
321
322
323

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

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

340
341
342
343
	assert(image_id < ARRAY_SIZE(policies));

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

353
354
	return result;
}
355
356
357
358
359
360
361
362
363
364
365
366
367
368

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