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 <errno.h>
12 #include <firmware_image_package.h>
13 #include <io_block.h>
14 #include <io_driver.h>
15 #include <io_fip.h>
16 #include <io_memmap.h>
17 #include <io_storage.h>
18 #include <mmio.h>
19 #include <platform_def.h>
20 #include <semihosting.h>	/* For FOPEN_MODE_... */
21 #include <string.h>
22 #include "hikey_private.h"
23 
24 #define EMMC_BLOCK_SHIFT			9
25 
26 /* Page 1024, since only a few pages before 2048 are used as partition table */
27 #define SERIALNO_EMMC_OFFSET			(1024 * 512)
28 
29 struct plat_io_policy {
30 	uintptr_t *dev_handle;
31 	uintptr_t image_spec;
32 	int (*check)(const uintptr_t spec);
33 };
34 
35 static const io_dev_connector_t *emmc_dev_con;
36 static uintptr_t emmc_dev_handle;
37 static const io_dev_connector_t *fip_dev_con;
38 static uintptr_t fip_dev_handle;
39 
40 static int check_emmc(const uintptr_t spec);
41 static int check_fip(const uintptr_t spec);
42 
43 static const io_block_spec_t emmc_fip_spec = {
44 	.offset		= HIKEY_FIP_BASE,
45 	.length		= HIKEY_FIP_MAX_SIZE,
46 };
47 
48 static const io_block_dev_spec_t emmc_dev_spec = {
49 	/* It's used as temp buffer in block driver. */
50 #ifdef IMAGE_BL1
51 	.buffer		= {
52 		.offset	= HIKEY_BL1_MMC_DATA_BASE,
53 		.length	= HIKEY_BL1_MMC_DATA_SIZE,
54 	},
55 #else
56 	.buffer		= {
57 		.offset	= HIKEY_MMC_DATA_BASE,
58 		.length	= HIKEY_MMC_DATA_SIZE,
59 	},
60 #endif
61 	.ops		= {
62 		.read	= emmc_read_blocks,
63 		.write	= emmc_write_blocks,
64 	},
65 	.block_size	= EMMC_BLOCK_SIZE,
66 };
67 
68 static const io_uuid_spec_t bl2_uuid_spec = {
69 	.uuid = UUID_TRUSTED_BOOT_FIRMWARE_BL2,
70 };
71 
72 static const io_uuid_spec_t bl31_uuid_spec = {
73 	.uuid = UUID_EL3_RUNTIME_FIRMWARE_BL31,
74 };
75 
76 static const io_uuid_spec_t bl32_uuid_spec = {
77 	.uuid = UUID_SECURE_PAYLOAD_BL32,
78 };
79 
80 static const io_uuid_spec_t bl32_extra1_uuid_spec = {
81 	.uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA1,
82 };
83 
84 static const io_uuid_spec_t bl32_extra2_uuid_spec = {
85 	.uuid = UUID_SECURE_PAYLOAD_BL32_EXTRA2,
86 };
87 
88 static const io_uuid_spec_t bl33_uuid_spec = {
89 	.uuid = UUID_NON_TRUSTED_FIRMWARE_BL33,
90 };
91 
92 static const io_uuid_spec_t scp_bl2_uuid_spec = {
93 	.uuid = UUID_SCP_FIRMWARE_SCP_BL2,
94 };
95 
96 static const struct plat_io_policy policies[] = {
97 	[FIP_IMAGE_ID] = {
98 		&emmc_dev_handle,
99 		(uintptr_t)&emmc_fip_spec,
100 		check_emmc
101 	},
102 	[BL2_IMAGE_ID] = {
103 		&fip_dev_handle,
104 		(uintptr_t)&bl2_uuid_spec,
105 		check_fip
106 	},
107 	[SCP_BL2_IMAGE_ID] = {
108 		&fip_dev_handle,
109 		(uintptr_t)&scp_bl2_uuid_spec,
110 		check_fip
111 	},
112 	[BL31_IMAGE_ID] = {
113 		&fip_dev_handle,
114 		(uintptr_t)&bl31_uuid_spec,
115 		check_fip
116 	},
117 	[BL32_IMAGE_ID] = {
118 		&fip_dev_handle,
119 		(uintptr_t)&bl32_uuid_spec,
120 		check_fip
121 	},
122 	[BL32_EXTRA1_IMAGE_ID] = {
123 		&fip_dev_handle,
124 		(uintptr_t)&bl32_extra1_uuid_spec,
125 		check_fip
126 	},
127 	[BL32_EXTRA2_IMAGE_ID] = {
128 		&fip_dev_handle,
129 		(uintptr_t)&bl32_extra2_uuid_spec,
130 		check_fip
131 	},
132 	[BL33_IMAGE_ID] = {
133 		&fip_dev_handle,
134 		(uintptr_t)&bl33_uuid_spec,
135 		check_fip
136 	}
137 };
138 
check_emmc(const uintptr_t spec)139 static int check_emmc(const uintptr_t spec)
140 {
141 	int result;
142 	uintptr_t local_handle;
143 
144 	result = io_dev_init(emmc_dev_handle, (uintptr_t)NULL);
145 	if (result == 0) {
146 		result = io_open(emmc_dev_handle, spec, &local_handle);
147 		if (result == 0)
148 			io_close(local_handle);
149 	}
150 	return result;
151 }
152 
check_fip(const uintptr_t spec)153 static int check_fip(const uintptr_t spec)
154 {
155 	int result;
156 	uintptr_t local_image_handle;
157 
158 	/* See if a Firmware Image Package is available */
159 	result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
160 	if (result == 0) {
161 		result = io_open(fip_dev_handle, spec, &local_image_handle);
162 		if (result == 0) {
163 			VERBOSE("Using FIP\n");
164 			io_close(local_image_handle);
165 		}
166 	}
167 	return result;
168 }
169 
hikey_io_setup(void)170 void hikey_io_setup(void)
171 {
172 	int result;
173 
174 	result = register_io_dev_block(&emmc_dev_con);
175 	assert(result == 0);
176 
177 	result = register_io_dev_fip(&fip_dev_con);
178 	assert(result == 0);
179 
180 	result = io_dev_open(emmc_dev_con, (uintptr_t)&emmc_dev_spec,
181 			     &emmc_dev_handle);
182 	assert(result == 0);
183 
184 	result = io_dev_open(fip_dev_con, (uintptr_t)NULL, &fip_dev_handle);
185 	assert(result == 0);
186 
187 	/* Ignore improbable errors in release builds */
188 	(void)result;
189 }
190 
191 /* Return an IO device handle and specification which can be used to access
192  * an image. Use this to enforce platform load policy
193  */
plat_get_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)194 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
195 			  uintptr_t *image_spec)
196 {
197 	int result;
198 	const struct plat_io_policy *policy;
199 
200 	assert(image_id < ARRAY_SIZE(policies));
201 
202 	policy = &policies[image_id];
203 	result = policy->check(policy->image_spec);
204 	assert(result == 0);
205 
206 	*image_spec = policy->image_spec;
207 	*dev_handle = *(policy->dev_handle);
208 
209 	return result;
210 }
211