1 /*
2  * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <assert.h>
8 #include <string.h>
9 
10 #include <platform_def.h>
11 
12 #include <arch_helpers.h>
13 #include <common/debug.h>
14 #include <common/tbbr/tbbr_img_def.h>
15 #include <drivers/io/io_block.h>
16 #include <drivers/io/io_driver.h>
17 #include <drivers/io/io_fip.h>
18 #include <drivers/io/io_memmap.h>
19 #include <drivers/io/io_storage.h>
20 #include <drivers/mmc.h>
21 #include <drivers/partition/partition.h>
22 #include <lib/mmio.h>
23 #include <lib/semihosting.h>
24 #include <lib/utils.h>
25 #include <tools_share/firmware_image_package.h>
26 
27 #if !POPLAR_RECOVERY
28 static const io_dev_connector_t *emmc_dev_con;
29 static uintptr_t emmc_dev_handle;
30 static int open_emmc(const uintptr_t spec);
31 
32 static const io_block_spec_t emmc_fip_spec = {
33 	.offset		= FIP_BASE_EMMC,
34 	.length		= FIP_SIZE
35 };
36 
37 static const io_block_dev_spec_t emmc_dev_spec = {
38 	.buffer		= {
39 		.offset	= POPLAR_EMMC_DATA_BASE,
40 		.length	= POPLAR_EMMC_DATA_SIZE,
41 	},
42 	.ops		= {
43 		.read	= mmc_read_blocks,
44 		.write	= mmc_write_blocks,
45 	},
46 	.block_size	= MMC_BLOCK_SIZE,
47 };
48 #else
49 static const io_dev_connector_t *mmap_dev_con;
50 static uintptr_t mmap_dev_handle;
51 static int open_mmap(const uintptr_t spec);
52 
53 static const io_block_spec_t loader_fip_spec = {
54 	.offset		= FIP_BASE,
55 	.length		= FIP_SIZE
56 };
57 #endif
58 
59 static const io_dev_connector_t *fip_dev_con;
60 static uintptr_t fip_dev_handle;
61 static int open_fip(const uintptr_t spec);
62 
63 static const io_uuid_spec_t bl2_uuid_spec = {
64 	.uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
65 };
66 
67 static const io_uuid_spec_t bl31_uuid_spec = {
68 	.uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
69 };
70 
71 static const io_uuid_spec_t bl32_uuid_spec = {
72 	.uuid = UUID_SECURE_PAYLOAD_BL32,
73 };
74 
75 static const io_uuid_spec_t bl32_extra1_uuid_spec = {
76 	.uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
77 };
78 
79 static const io_uuid_spec_t bl32_extra2_uuid_spec = {
80 	.uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
81 };
82 
83 static const io_uuid_spec_t bl33_uuid_spec = {
84 	.uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
85 };
86 
87 struct plat_io_policy {
88 	uintptr_t	*dev_handle;
89 	uintptr_t	image_spec;
90 	int		(*check)(const uintptr_t spec);
91 };
92 
93 static const struct plat_io_policy policies[] = {
94 #if !POPLAR_RECOVERY
95 	[FIP_IMAGE_ID] = {
96 		&emmc_dev_handle,
97 		(uintptr_t)&emmc_fip_spec,
98 		open_emmc
99 	},
100 #else
101 	[FIP_IMAGE_ID] = {
102 		&mmap_dev_handle,
103 		(uintptr_t)&loader_fip_spec,
104 		open_mmap
105 	},
106 #endif
107 	[BL2_IMAGE_ID] = {
108 		&fip_dev_handle,
109 		(uintptr_t)&bl2_uuid_spec,
110 		open_fip
111 	},
112 	[BL31_IMAGE_ID] = {
113 		&fip_dev_handle,
114 		(uintptr_t)&bl31_uuid_spec,
115 		open_fip
116 	},
117 	[BL32_IMAGE_ID] = {
118 		&fip_dev_handle,
119 		(uintptr_t)&bl32_uuid_spec,
120 		open_fip
121 	},
122 	[BL32_EXTRA1_IMAGE_ID] = {
123 		&fip_dev_handle,
124 		(uintptr_t)&bl32_extra1_uuid_spec,
125 		open_fip
126 	},
127 	[BL32_EXTRA2_IMAGE_ID] = {
128 		&fip_dev_handle,
129 		(uintptr_t)&bl32_extra2_uuid_spec,
130 		open_fip
131 	},
132 	[BL33_IMAGE_ID] = {
133 		&fip_dev_handle,
134 		(uintptr_t)&bl33_uuid_spec,
135 		open_fip
136 	},
137 };
138 
139 #if !POPLAR_RECOVERY
open_emmc(const uintptr_t spec)140 static int open_emmc(const uintptr_t spec)
141 {
142 	int result;
143 	uintptr_t local_image_handle;
144 
145 	result = io_dev_init(emmc_dev_handle, (uintptr_t)NULL);
146 	if (result == 0) {
147 		result = io_open(emmc_dev_handle, spec, &local_image_handle);
148 		if (result == 0) {
149 			INFO("Using eMMC\n");
150 			io_close(local_image_handle);
151 		} else {
152 			ERROR("error opening emmc\n");
153 		}
154 	} else {
155 		ERROR("error initializing emmc\n");
156 	}
157 
158 	return result;
159 }
160 #else
open_mmap(const uintptr_t spec)161 static int open_mmap(const uintptr_t spec)
162 {
163 	int result;
164 	uintptr_t local_image_handle;
165 
166 	result = io_dev_init(mmap_dev_handle, (uintptr_t)NULL);
167 	if (result == 0) {
168 		result = io_open(mmap_dev_handle, spec, &local_image_handle);
169 		if (result == 0) {
170 			INFO("Using mmap\n");
171 			io_close(local_image_handle);
172 		} else {
173 			ERROR("error opening mmap\n");
174 		}
175 	} else {
176 		ERROR("error initializing mmap\n");
177 	}
178 
179 	return result;
180 }
181 #endif
182 
open_fip(const uintptr_t spec)183 static int open_fip(const uintptr_t spec)
184 {
185 	uintptr_t local_image_handle;
186 	int result;
187 
188 	result = io_dev_init(fip_dev_handle, (uintptr_t) FIP_IMAGE_ID);
189 	if (result == 0) {
190 		result = io_open(fip_dev_handle, spec, &local_image_handle);
191 		if (result == 0) {
192 			INFO("Using FIP\n");
193 			io_close(local_image_handle);
194 		} else {
195 			ERROR("error opening fip\n");
196 		}
197 	} else {
198 		ERROR("error initializing fip\n");
199 	}
200 
201 	return result;
202 }
203 
plat_get_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)204 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
205 			  uintptr_t *image_spec)
206 {
207 	const struct plat_io_policy *policy;
208 	int result;
209 
210 	assert(image_id < ARRAY_SIZE(policies));
211 
212 	policy = &policies[image_id];
213 	result = policy->check(policy->image_spec);
214 	assert(result == 0);
215 
216 	*image_spec = policy->image_spec;
217 	*dev_handle = *(policy->dev_handle);
218 
219 	return result;
220 }
221 
plat_io_setup(void)222 void plat_io_setup(void)
223 {
224 	int result;
225 
226 #if !POPLAR_RECOVERY
227 	result = register_io_dev_block(&emmc_dev_con);
228 #else
229 	result = register_io_dev_memmap(&mmap_dev_con);
230 #endif
231 	assert(result == 0);
232 
233 	result = register_io_dev_fip(&fip_dev_con);
234 	assert(result == 0);
235 
236 #if !POPLAR_RECOVERY
237 	result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
238 				&fip_dev_handle);
239 #else
240 	result = io_dev_open(fip_dev_con, (uintptr_t)&loader_fip_spec,
241 				&fip_dev_handle);
242 #endif
243 	assert(result == 0);
244 
245 #if !POPLAR_RECOVERY
246 	result = io_dev_open(emmc_dev_con, (uintptr_t)&emmc_dev_spec,
247 				&emmc_dev_handle);
248 #else
249 	result = io_dev_open(mmap_dev_con, (uintptr_t)NULL, &mmap_dev_handle);
250 #endif
251 	assert(result == 0);
252 
253 	(void) result;
254 }
255