arm_io_storage.c 7.89 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
/*
 * Copyright (c) 2015, ARM Limited and Contributors. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 * Redistributions of source code must retain the above copyright notice, this
 * list of conditions and the following disclaimer.
 *
 * Redistributions in binary form must reproduce the above copyright notice,
 * this list of conditions and the following disclaimer in the documentation
 * and/or other materials provided with the distribution.
 *
 * Neither the name of ARM nor the names of its contributors may be used
 * to endorse or promote products derived from this software without specific
 * prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */
#include <assert.h>
31
#include <bl_common.h>		/* For ARRAY_SIZE */
32
#include <debug.h>
33
#include <firmware_image_package.h>
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <io_driver.h>
#include <io_fip.h>
#include <io_memmap.h>
#include <io_storage.h>
#include <platform_def.h>
#include <string.h>

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

52
53
static const io_uuid_spec_t bl2_uuid_spec = {
	.uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
54
55
};

56
57
static const io_uuid_spec_t scp_bl2_uuid_spec = {
	.uuid = UUID_SCP_FIRMWARE_SCP_BL2,
58
59
};

60
61
static const io_uuid_spec_t bl31_uuid_spec = {
	.uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
62
63
};

64
65
static const io_uuid_spec_t bl32_uuid_spec = {
	.uuid = UUID_SECURE_PAYLOAD_BL32,
66
67
};

68
69
static const io_uuid_spec_t bl33_uuid_spec = {
	.uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
70
71
72
};

#if TRUSTED_BOARD_BOOT
73
74
static const io_uuid_spec_t tb_fw_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_BOOT_FW_CERT,
75
76
};

77
78
static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_KEY_CERT,
79
80
};

81
82
static const io_uuid_spec_t scp_fw_key_cert_uuid_spec = {
	.uuid = UUID_SCP_FW_KEY_CERT,
83
84
};

85
86
static const io_uuid_spec_t soc_fw_key_cert_uuid_spec = {
	.uuid = UUID_SOC_FW_KEY_CERT,
87
88
};

89
90
static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
91
92
};

93
94
static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
	.uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
95
96
};

97
98
static const io_uuid_spec_t scp_fw_cert_uuid_spec = {
	.uuid = UUID_SCP_FW_CONTENT_CERT,
99
100
};

101
102
static const io_uuid_spec_t soc_fw_cert_uuid_spec = {
	.uuid = UUID_SOC_FW_CONTENT_CERT,
103
104
};

105
106
static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
	.uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
107
108
};

109
110
static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
	.uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
111
112
113
};
#endif /* TRUSTED_BOARD_BOOT */

114

115
116
117
118
119
120
121
122
123
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);
};

124
/* By default, ARM platforms load images from the FIP */
125
static const struct plat_io_policy policies[] = {
126
	[FIP_IMAGE_ID] = {
127
128
129
		&memmap_dev_handle,
		(uintptr_t)&fip_block_spec,
		open_memmap
130
131
	},
	[BL2_IMAGE_ID] = {
132
		&fip_dev_handle,
133
		(uintptr_t)&bl2_uuid_spec,
134
		open_fip
135
	},
136
	[SCP_BL2_IMAGE_ID] = {
137
		&fip_dev_handle,
138
		(uintptr_t)&scp_bl2_uuid_spec,
139
		open_fip
140
141
	},
	[BL31_IMAGE_ID] = {
142
		&fip_dev_handle,
143
		(uintptr_t)&bl31_uuid_spec,
144
		open_fip
145
146
	},
	[BL32_IMAGE_ID] = {
147
		&fip_dev_handle,
148
		(uintptr_t)&bl32_uuid_spec,
149
		open_fip
150
151
	},
	[BL33_IMAGE_ID] = {
152
		&fip_dev_handle,
153
		(uintptr_t)&bl33_uuid_spec,
154
		open_fip
155
	},
156
#if TRUSTED_BOARD_BOOT
157
	[TRUSTED_BOOT_FW_CERT_ID] = {
158
		&fip_dev_handle,
159
		(uintptr_t)&tb_fw_cert_uuid_spec,
160
		open_fip
161
162
	},
	[TRUSTED_KEY_CERT_ID] = {
163
		&fip_dev_handle,
164
		(uintptr_t)&trusted_key_cert_uuid_spec,
165
		open_fip
166
	},
167
	[SCP_FW_KEY_CERT_ID] = {
168
		&fip_dev_handle,
169
		(uintptr_t)&scp_fw_key_cert_uuid_spec,
170
		open_fip
171
	},
172
	[SOC_FW_KEY_CERT_ID] = {
173
		&fip_dev_handle,
174
		(uintptr_t)&soc_fw_key_cert_uuid_spec,
175
		open_fip
176
	},
177
	[TRUSTED_OS_FW_KEY_CERT_ID] = {
178
		&fip_dev_handle,
179
		(uintptr_t)&tos_fw_key_cert_uuid_spec,
180
		open_fip
181
	},
182
	[NON_TRUSTED_FW_KEY_CERT_ID] = {
183
		&fip_dev_handle,
184
		(uintptr_t)&nt_fw_key_cert_uuid_spec,
185
		open_fip
186
	},
187
	[SCP_FW_CONTENT_CERT_ID] = {
188
		&fip_dev_handle,
189
		(uintptr_t)&scp_fw_cert_uuid_spec,
190
		open_fip
191
	},
192
	[SOC_FW_CONTENT_CERT_ID] = {
193
		&fip_dev_handle,
194
		(uintptr_t)&soc_fw_cert_uuid_spec,
195
		open_fip
196
	},
197
	[TRUSTED_OS_FW_CONTENT_CERT_ID] = {
198
		&fip_dev_handle,
199
		(uintptr_t)&tos_fw_cert_uuid_spec,
200
		open_fip
201
	},
202
	[NON_TRUSTED_FW_CONTENT_CERT_ID] = {
203
		&fip_dev_handle,
204
		(uintptr_t)&nt_fw_cert_uuid_spec,
205
		open_fip
206
	},
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#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 */
222
	result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
223
	if (result == 0) {
224
		result = io_open(fip_dev_handle, spec, &local_image_handle);
225
		if (result == 0) {
226
227
228
229
230
231
232
233
234
235
236
237
238
239
			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);
240
	if (result == 0) {
241
		result = io_open(memmap_dev_handle, spec, &local_image_handle);
242
		if (result == 0) {
243
244
245
246
247
248
249
250
251
252
253
254
255
			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);
256
	assert(io_result == 0);
257
258

	io_result = register_io_dev_memmap(&memmap_dev_con);
259
	assert(io_result == 0);
260
261
262
263

	/* Open connections to devices and cache the handles */
	io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
				&fip_dev_handle);
264
	assert(io_result == 0);
265
266
267

	io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
				&memmap_dev_handle);
268
	assert(io_result == 0);
269
270
271
272
273
274
275
276
277
278
279

	/* 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(
280
281
282
	unsigned int image_id __unused,
	uintptr_t *dev_handle __unused,
	uintptr_t *image_spec __unused)
283
284
{
	/* By default do not try an alternative */
285
	return -ENOENT;
286
287
288
289
}

/* Return an IO device handle and specification which can be used to access
 * an image. Use this to enforce platform load policy */
290
int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
291
292
			  uintptr_t *image_spec)
{
293
	int result;
294
295
	const struct plat_io_policy *policy;

296
297
298
299
	assert(image_id < ARRAY_SIZE(policies));

	policy = &policies[image_id];
	result = policy->check(policy->image_spec);
300
	if (result == 0) {
301
302
		*image_spec = policy->image_spec;
		*dev_handle = *(policy->dev_handle);
303
	} else {
304
305
306
		VERBOSE("Trying alternative IO\n");
		result = plat_arm_get_alt_image_source(image_id, dev_handle,
						       image_spec);
307
	}
308

309
310
	return result;
}
311
312
313
314
315
316
317
318
319
320
321
322
323
324

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