1 /*
2 * Copyright (c) 2018-2019, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #include <assert.h>
8
9 #include <platform_def.h>
10
11 #include <common/debug.h>
12 #include <drivers/io/io_block.h>
13 #include <drivers/io/io_driver.h>
14 #include <drivers/io/io_fip.h>
15 #include <drivers/io/io_memmap.h>
16 #include <drivers/mmc.h>
17 #include <tools_share/firmware_image_package.h>
18
19 static const io_dev_connector_t *fip_dev_con;
20 static uintptr_t fip_dev_handle;
21
22 #ifndef IMX7_FIP_MMAP
23 static const io_dev_connector_t *mmc_dev_con;
24 static uintptr_t mmc_dev_handle;
25
26 static const io_block_spec_t mmc_fip_spec = {
27 .offset = IMX7_FIP_MMC_BASE,
28 .length = IMX7_FIP_SIZE
29 };
30
31 static const io_block_dev_spec_t mmc_dev_spec = {
32 /* It's used as temp buffer in block driver. */
33 .buffer = {
34 .offset = IMX7_FIP_BASE,
35 /* do we need a new value? */
36 .length = IMX7_FIP_SIZE
37 },
38 .ops = {
39 .read = mmc_read_blocks,
40 .write = mmc_write_blocks,
41 },
42 .block_size = MMC_BLOCK_SIZE,
43 };
44
45 static int open_mmc(const uintptr_t spec);
46
47 #else
48 static const io_dev_connector_t *memmap_dev_con;
49 static uintptr_t memmap_dev_handle;
50
51 static const io_block_spec_t fip_block_spec = {
52 .offset = IMX7_FIP_BASE,
53 .length = IMX7_FIP_SIZE
54 };
55 static int open_memmap(const uintptr_t spec);
56 #endif
57 static int open_fip(const uintptr_t spec);
58
59 static const io_uuid_spec_t bl32_uuid_spec = {
60 .uuid = UUID_SECURE_PAYLOAD_BL32,
61 };
62
63 static const io_uuid_spec_t bl32_extra1_uuid_spec = {
64 .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
65 };
66
67 static const io_uuid_spec_t bl32_extra2_uuid_spec = {
68 .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
69 };
70
71 static const io_uuid_spec_t bl33_uuid_spec = {
72 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
73 };
74
75 #if TRUSTED_BOARD_BOOT
76 static const io_uuid_spec_t tb_fw_cert_uuid_spec = {
77 .uuid = UUID_TRUSTED_BOOT_FW_CERT,
78 };
79
80 static const io_uuid_spec_t trusted_key_cert_uuid_spec = {
81 .uuid = UUID_TRUSTED_KEY_CERT,
82 };
83
84 static const io_uuid_spec_t tos_fw_key_cert_uuid_spec = {
85 .uuid = UUID_TRUSTED_OS_FW_KEY_CERT,
86 };
87
88 static const io_uuid_spec_t tos_fw_cert_uuid_spec = {
89 .uuid = UUID_TRUSTED_OS_FW_CONTENT_CERT,
90 };
91
92 static const io_uuid_spec_t nt_fw_key_cert_uuid_spec = {
93 .uuid = UUID_NON_TRUSTED_FW_KEY_CERT,
94 };
95
96 static const io_uuid_spec_t nt_fw_cert_uuid_spec = {
97 .uuid = UUID_NON_TRUSTED_FW_CONTENT_CERT,
98 };
99 #endif /* TRUSTED_BOARD_BOOT */
100
101 /* TODO: this structure is replicated multiple times. rationalize it ! */
102 struct plat_io_policy {
103 uintptr_t *dev_handle;
104 uintptr_t image_spec;
105 int (*check)(const uintptr_t spec);
106 };
107
108 static const struct plat_io_policy policies[] = {
109 #ifndef IMX7_FIP_MMAP
110 [FIP_IMAGE_ID] = {
111 &mmc_dev_handle,
112 (uintptr_t)&mmc_fip_spec,
113 open_mmc
114 },
115 #else
116 [FIP_IMAGE_ID] = {
117 &memmap_dev_handle,
118 (uintptr_t)&fip_block_spec,
119 open_memmap
120 },
121 #endif
122 [BL32_IMAGE_ID] = {
123 &fip_dev_handle,
124 (uintptr_t)&bl32_uuid_spec,
125 open_fip
126 },
127 [BL32_EXTRA1_IMAGE_ID] = {
128 &fip_dev_handle,
129 (uintptr_t)&bl32_extra1_uuid_spec,
130 open_fip
131 },
132 [BL32_EXTRA2_IMAGE_ID] = {
133 &fip_dev_handle,
134 (uintptr_t)&bl32_extra2_uuid_spec,
135 open_fip
136 },
137 [BL33_IMAGE_ID] = {
138 &fip_dev_handle,
139 (uintptr_t)&bl33_uuid_spec,
140 open_fip
141 },
142 #if TRUSTED_BOARD_BOOT
143 [TRUSTED_BOOT_FW_CERT_ID] = {
144 &fip_dev_handle,
145 (uintptr_t)&tb_fw_cert_uuid_spec,
146 open_fip
147 },
148 [TRUSTED_KEY_CERT_ID] = {
149 &fip_dev_handle,
150 (uintptr_t)&trusted_key_cert_uuid_spec,
151 open_fip
152 },
153 [TRUSTED_OS_FW_KEY_CERT_ID] = {
154 &fip_dev_handle,
155 (uintptr_t)&tos_fw_key_cert_uuid_spec,
156 open_fip
157 },
158 [NON_TRUSTED_FW_KEY_CERT_ID] = {
159 &fip_dev_handle,
160 (uintptr_t)&nt_fw_key_cert_uuid_spec,
161 open_fip
162 },
163 [TRUSTED_OS_FW_CONTENT_CERT_ID] = {
164 &fip_dev_handle,
165 (uintptr_t)&tos_fw_cert_uuid_spec,
166 open_fip
167 },
168 [NON_TRUSTED_FW_CONTENT_CERT_ID] = {
169 &fip_dev_handle,
170 (uintptr_t)&nt_fw_cert_uuid_spec,
171 open_fip
172 },
173 #endif /* TRUSTED_BOARD_BOOT */
174 };
175
open_fip(const uintptr_t spec)176 static int open_fip(const uintptr_t spec)
177 {
178 int result;
179 uintptr_t local_image_handle;
180
181 /* See if a Firmware Image Package is available */
182 result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
183 if (result == 0) {
184 result = io_open(fip_dev_handle, spec, &local_image_handle);
185 if (result == 0) {
186 VERBOSE("Using FIP\n");
187 io_close(local_image_handle);
188 }
189 }
190 return result;
191 }
192
193 #ifndef IMX7_FIP_MMAP
open_mmc(const uintptr_t spec)194 static int open_mmc(const uintptr_t spec)
195 {
196 int result;
197 uintptr_t local_handle;
198
199 result = io_dev_init(mmc_dev_handle, (uintptr_t)NULL);
200 if (result == 0) {
201 result = io_open(mmc_dev_handle, spec, &local_handle);
202 if (result == 0)
203 io_close(local_handle);
204 }
205 return result;
206 }
207 #else
open_memmap(const uintptr_t spec)208 static int open_memmap(const uintptr_t spec)
209 {
210 int result;
211 uintptr_t local_image_handle;
212
213 result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL);
214 if (result == 0) {
215 result = io_open(memmap_dev_handle, spec, &local_image_handle);
216 if (result == 0) {
217 VERBOSE("Using Memmap\n");
218 io_close(local_image_handle);
219 }
220 }
221 return result;
222 }
223 #endif
224
plat_get_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)225 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
226 uintptr_t *image_spec)
227 {
228 int result;
229 const struct plat_io_policy *policy;
230
231 assert(image_id < ARRAY_SIZE(policies));
232
233 policy = &policies[image_id];
234 result = policy->check(policy->image_spec);
235 assert(result == 0);
236
237 *image_spec = policy->image_spec;
238 *dev_handle = *policy->dev_handle;
239
240 return result;
241 }
242
plat_imx7_io_setup(void)243 void plat_imx7_io_setup(void)
244 {
245 int result __unused;
246
247 #ifndef IMX7_FIP_MMAP
248 result = register_io_dev_block(&mmc_dev_con);
249 assert(result == 0);
250
251 result = io_dev_open(mmc_dev_con, (uintptr_t)&mmc_dev_spec,
252 &mmc_dev_handle);
253 assert(result == 0);
254
255 #else
256 result = register_io_dev_memmap(&memmap_dev_con);
257 assert(result == 0);
258
259 result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
260 &memmap_dev_handle);
261 assert(result == 0);
262
263 #endif
264 result = register_io_dev_fip(&fip_dev_con);
265 assert(result == 0);
266
267 result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
268 &fip_dev_handle);
269 assert(result == 0);
270 }
271