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