blob: a17e0f1a22429326a59430e4ad8d25d73d58ef8f [file] [log] [blame]
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +02001/*
2 * Copyright (c) 2017, ARM Limited and Contributors. All rights reserved.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +02007#include <assert.h>
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +02008#include <string.h>
Antonio Nino Diaze0f90632018-12-14 00:18:21 +00009
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>
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +020026
Victor Chongf0c7c612018-01-16 00:29:47 +090027#if !POPLAR_RECOVERY
Victor Chong539408d2018-01-03 01:53:08 +090028static const io_dev_connector_t *emmc_dev_con;
Victor Chong539408d2018-01-03 01:53:08 +090029static uintptr_t emmc_dev_handle;
Victor Chong539408d2018-01-03 01:53:08 +090030static int open_emmc(const uintptr_t spec);
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +020031
Victor Chong539408d2018-01-03 01:53:08 +090032static const io_block_spec_t emmc_fip_spec = {
33 .offset = FIP_BASE_EMMC,
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +020034 .length = FIP_SIZE
35};
36
Victor Chong539408d2018-01-03 01:53:08 +090037static 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 = {
Haojian Zhuang3eff4092018-08-04 18:07:26 +080043 .read = mmc_read_blocks,
44 .write = mmc_write_blocks,
Victor Chong539408d2018-01-03 01:53:08 +090045 },
Haojian Zhuang3eff4092018-08-04 18:07:26 +080046 .block_size = MMC_BLOCK_SIZE,
Victor Chong539408d2018-01-03 01:53:08 +090047};
Victor Chongf0c7c612018-01-16 00:29:47 +090048#else
49static const io_dev_connector_t *mmap_dev_con;
50static uintptr_t mmap_dev_handle;
51static int open_mmap(const uintptr_t spec);
52
53static const io_block_spec_t loader_fip_spec = {
54 .offset = FIP_BASE,
55 .length = FIP_SIZE
56};
57#endif
58
59static const io_dev_connector_t *fip_dev_con;
60static uintptr_t fip_dev_handle;
61static int open_fip(const uintptr_t spec);
Victor Chong539408d2018-01-03 01:53:08 +090062
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +020063static const io_uuid_spec_t bl2_uuid_spec = {
64 .uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
65};
66
67static const io_uuid_spec_t bl31_uuid_spec = {
68 .uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
69};
70
Victor Chong662556a2017-10-28 01:59:41 +090071static const io_uuid_spec_t bl32_uuid_spec = {
72 .uuid = UUID_SECURE_PAYLOAD_BL32,
73};
74
Victor Chongaa033472018-02-01 00:35:39 +090075static const io_uuid_spec_t bl32_extra1_uuid_spec = {
76 .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
77};
78
79static const io_uuid_spec_t bl32_extra2_uuid_spec = {
80 .uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
81};
82
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +020083static const io_uuid_spec_t bl33_uuid_spec = {
84 .uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
85};
86
87struct plat_io_policy {
88 uintptr_t *dev_handle;
89 uintptr_t image_spec;
90 int (*check)(const uintptr_t spec);
91};
92
93static const struct plat_io_policy policies[] = {
Victor Chongf0c7c612018-01-16 00:29:47 +090094#if !POPLAR_RECOVERY
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +020095 [FIP_IMAGE_ID] = {
Victor Chong539408d2018-01-03 01:53:08 +090096 &emmc_dev_handle,
97 (uintptr_t)&emmc_fip_spec,
98 open_emmc
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +020099 },
Victor Chongf0c7c612018-01-16 00:29:47 +0900100#else
101 [FIP_IMAGE_ID] = {
102 &mmap_dev_handle,
103 (uintptr_t)&loader_fip_spec,
104 open_mmap
105 },
106#endif
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200107 [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 },
Victor Chong662556a2017-10-28 01:59:41 +0900117 [BL32_IMAGE_ID] = {
118 &fip_dev_handle,
119 (uintptr_t)&bl32_uuid_spec,
120 open_fip
121 },
Victor Chongaa033472018-02-01 00:35:39 +0900122 [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 },
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200132 [BL33_IMAGE_ID] = {
133 &fip_dev_handle,
134 (uintptr_t)&bl33_uuid_spec,
135 open_fip
136 },
137};
138
Victor Chongf0c7c612018-01-16 00:29:47 +0900139#if !POPLAR_RECOVERY
Victor Chong539408d2018-01-03 01:53:08 +0900140static int open_emmc(const uintptr_t spec)
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200141{
142 int result;
143 uintptr_t local_image_handle;
144
Victor Chong539408d2018-01-03 01:53:08 +0900145 result = io_dev_init(emmc_dev_handle, (uintptr_t)NULL);
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200146 if (result == 0) {
Victor Chong539408d2018-01-03 01:53:08 +0900147 result = io_open(emmc_dev_handle, spec, &local_image_handle);
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200148 if (result == 0) {
Victor Chong539408d2018-01-03 01:53:08 +0900149 INFO("Using eMMC\n");
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200150 io_close(local_image_handle);
Victor Chong539408d2018-01-03 01:53:08 +0900151 } else {
152 ERROR("error opening emmc\n");
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200153 }
Victor Chong539408d2018-01-03 01:53:08 +0900154 } else {
155 ERROR("error initializing emmc\n");
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200156 }
Victor Chong539408d2018-01-03 01:53:08 +0900157
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200158 return result;
159}
Victor Chongf0c7c612018-01-16 00:29:47 +0900160#else
161static 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
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200182
183static 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) {
Victor Chong539408d2018-01-03 01:53:08 +0900192 INFO("Using FIP\n");
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200193 io_close(local_image_handle);
194 } else {
Victor Chong539408d2018-01-03 01:53:08 +0900195 ERROR("error opening fip\n");
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200196 }
197 } else {
Victor Chong539408d2018-01-03 01:53:08 +0900198 ERROR("error initializing fip\n");
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200199 }
200
201 return result;
202}
203
204int 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
222void plat_io_setup(void)
223{
224 int result;
225
Victor Chongf0c7c612018-01-16 00:29:47 +0900226#if !POPLAR_RECOVERY
Victor Chong539408d2018-01-03 01:53:08 +0900227 result = register_io_dev_block(&emmc_dev_con);
Victor Chongf0c7c612018-01-16 00:29:47 +0900228#else
229 result = register_io_dev_memmap(&mmap_dev_con);
230#endif
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200231 assert(result == 0);
232
233 result = register_io_dev_fip(&fip_dev_con);
234 assert(result == 0);
235
Victor Chongf0c7c612018-01-16 00:29:47 +0900236#if !POPLAR_RECOVERY
Victor Chong539408d2018-01-03 01:53:08 +0900237 result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200238 &fip_dev_handle);
Victor Chongf0c7c612018-01-16 00:29:47 +0900239#else
240 result = io_dev_open(fip_dev_con, (uintptr_t)&loader_fip_spec,
241 &fip_dev_handle);
242#endif
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200243 assert(result == 0);
244
Victor Chongf0c7c612018-01-16 00:29:47 +0900245#if !POPLAR_RECOVERY
Victor Chong539408d2018-01-03 01:53:08 +0900246 result = io_dev_open(emmc_dev_con, (uintptr_t)&emmc_dev_spec,
247 &emmc_dev_handle);
Victor Chongf0c7c612018-01-16 00:29:47 +0900248#else
249 result = io_dev_open(mmap_dev_con, (uintptr_t)NULL, &mmap_dev_handle);
250#endif
Jorge Ramirez-Ortiza29d9a62017-06-28 10:11:31 +0200251 assert(result == 0);
252
253 (void) result;
254}