xref: /rk3399_ARM-atf/plat/arm/common/arm_io_storage.c (revision e612e7250709d9b364002fda2366737954d8fe98)
1 /*
2  * Copyright (c) 2015-2025, ARM Limited. All rights reserved.
3  *
4  * SPDX-License-Identifier: BSD-3-Clause
5  */
6 
7 #include <common/debug.h>
8 #include <drivers/fwu/fwu_metadata.h>
9 #include <drivers/io/io_driver.h>
10 #include <drivers/io/io_encrypted.h>
11 #include <drivers/io/io_fip.h>
12 #include <drivers/io/io_memmap.h>
13 #include <drivers/io/io_storage.h>
14 #include <drivers/partition/partition.h>
15 #include <lib/utils.h>
16 
17 #include <plat/arm/common/arm_fconf_getter.h>
18 #include <plat/arm/common/arm_fconf_io_storage.h>
19 #include <plat/arm/common/plat_arm.h>
20 #include <plat/common/platform.h>
21 #include <platform_def.h>
22 
23 /* IO devices */
24 static const io_dev_connector_t *fip_dev_con;
25 uintptr_t fip_dev_handle;
26 static const io_dev_connector_t *memmap_dev_con;
27 uintptr_t memmap_dev_handle;
28 #ifndef DECRYPTION_SUPPORT_none
29 static const io_dev_connector_t *enc_dev_con;
30 uintptr_t enc_dev_handle;
31 #endif
32 
33 #if ARM_GPT_SUPPORT
34 /* fip partition names */
35 static const char * const fip_part_names[] = {"FIP_A", "FIP_B"};
36 CASSERT(sizeof(fip_part_names)/sizeof(char *) == NR_OF_FW_BANKS,
37 	assert_fip_partition_names_missing);
38 #endif /* ARM_GPT_SUPPORT */
39 
40 /* Weak definitions may be overridden in specific ARM standard platform */
41 #pragma weak plat_arm_io_setup
42 #pragma weak plat_arm_get_alt_image_source
43 
open_fip(const uintptr_t spec)44 int open_fip(const uintptr_t spec)
45 {
46 	int result;
47 	uintptr_t local_image_handle;
48 
49 	/* See if a Firmware Image Package is available */
50 	result = io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID);
51 	if (result == 0 && spec != (uintptr_t)NULL) {
52 		result = io_open(fip_dev_handle, spec, &local_image_handle);
53 		if (result == 0) {
54 			VERBOSE("Using FIP\n");
55 			io_close(local_image_handle);
56 		}
57 	}
58 	return result;
59 }
60 
open_memmap(const uintptr_t spec)61 int open_memmap(const uintptr_t spec)
62 {
63 	int result;
64 	uintptr_t local_image_handle;
65 
66 	result = io_dev_init(memmap_dev_handle, (uintptr_t)NULL);
67 	if (result == 0) {
68 		result = io_open(memmap_dev_handle, spec, &local_image_handle);
69 		if (result == 0) {
70 			VERBOSE("Using Memmap\n");
71 			io_close(local_image_handle);
72 		}
73 	}
74 	return result;
75 }
76 
arm_io_setup(void)77 int arm_io_setup(void)
78 {
79 	int io_result;
80 
81 	io_result = register_io_dev_fip(&fip_dev_con);
82 	if (io_result < 0) {
83 		return io_result;
84 	}
85 
86 	io_result = register_io_dev_memmap(&memmap_dev_con);
87 	if (io_result < 0) {
88 		return io_result;
89 	}
90 
91 	/* Open connections to devices and cache the handles */
92 	io_result = io_dev_open(fip_dev_con, (uintptr_t)NULL,
93 				&fip_dev_handle);
94 	if (io_result < 0) {
95 		return io_result;
96 	}
97 
98 	io_result = io_dev_open(memmap_dev_con, (uintptr_t)NULL,
99 				&memmap_dev_handle);
100 
101 	if (io_result < 0) {
102 		return io_result;
103 	}
104 
105 #ifndef DECRYPTION_SUPPORT_none
106 	io_result = register_io_dev_enc(&enc_dev_con);
107 	if (io_result < 0) {
108 		return io_result;
109 	}
110 
111 	io_result = io_dev_open(enc_dev_con, (uintptr_t)NULL,
112 			&enc_dev_handle);
113 	if (io_result < 0) {
114 		return io_result;
115 	}
116 #endif
117 
118 	return io_result;
119 }
120 
plat_arm_io_setup(void)121 void plat_arm_io_setup(void)
122 {
123 	int err;
124 
125 	err = arm_io_setup();
126 	if (err < 0) {
127 		panic();
128 	}
129 }
130 
plat_arm_get_alt_image_source(unsigned int image_id __unused,uintptr_t * dev_handle __unused,uintptr_t * image_spec __unused)131 int plat_arm_get_alt_image_source(
132 	unsigned int image_id __unused,
133 	uintptr_t *dev_handle __unused,
134 	uintptr_t *image_spec __unused)
135 {
136 	/* By default do not try an alternative */
137 	return -ENOENT;
138 }
139 
140 /* Return an IO device handle and specification which can be used to access
141  * an image. Use this to enforce platform load policy */
plat_get_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)142 int plat_get_image_source(unsigned int image_id, uintptr_t *dev_handle,
143 			  uintptr_t *image_spec)
144 {
145 	int result;
146 	const struct plat_io_policy *policy;
147 
148 	policy = FCONF_GET_PROPERTY(arm, io_policies, image_id);
149 	assert(policy->check != NULL);
150 	result = policy->check(policy->image_spec);
151 	if (result == 0) {
152 		*image_spec = policy->image_spec;
153 		*dev_handle = *(policy->dev_handle);
154 	} else {
155 		VERBOSE("Trying alternative IO\n");
156 		result = plat_arm_get_alt_image_source(image_id, dev_handle,
157 						       image_spec);
158 	}
159 
160 	return result;
161 }
162 
163 /*
164  * See if a Firmware Image Package is available,
165  * by checking if TOC is valid or not.
166  */
arm_io_is_toc_valid(void)167 bool arm_io_is_toc_valid(void)
168 {
169 	return (io_dev_init(fip_dev_handle, (uintptr_t)FIP_IMAGE_ID) == 0);
170 }
171 
172 #if ARM_GPT_SUPPORT
173 /******************************************************************************
174  * Retrieve partition entry details such as offset and length, and set these
175  * details in the I/O policy of the requested image.
176  *
177  * @image_id: image id whose I/O policy to be updated
178  *
179  * @part_name: partition name whose details to be retrieved
180  *
181  * Returns 0 on success, error otherwise
182  * Alongside, returns device handle and image specification of requested
183  * image.
184  ******************************************************************************/
arm_set_image_source(unsigned int image_id,const char * part_name,uintptr_t * dev_handle,uintptr_t * image_spec)185 int arm_set_image_source(unsigned int image_id, const char *part_name,
186 			 uintptr_t *dev_handle, uintptr_t *image_spec)
187 {
188 	const partition_entry_t *entry = get_partition_entry(part_name);
189 
190 	if (entry == NULL) {
191 		ERROR("Unable to find the %s partition\n", part_name);
192 		return -ENOENT;
193 	}
194 
195 	struct plat_io_policy *policy = FCONF_GET_PROPERTY(arm,
196 							   io_policies,
197 							   image_id);
198 
199 	assert(policy != NULL);
200 	assert(policy->image_spec != 0UL);
201 
202 	io_block_spec_t *spec = (io_block_spec_t *)policy->image_spec;
203 	/* set offset and length of the image */
204 	spec->offset = PLAT_ARM_FLASH_IMAGE_BASE + entry->start;
205 	spec->length = entry->length;
206 
207 	*dev_handle = *(policy->dev_handle);
208 	*image_spec = policy->image_spec;
209 
210 	return 0;
211 }
212 
213 /*******************************************************************************
214  * Set the source offset and length of the FIP image in its I/O policy.
215  *
216  * @active_fw_bank_idx: active firmware bank index gathered from FWU metadata.
217  ******************************************************************************/
arm_set_fip_addr(uint32_t active_fw_bank_idx)218 void arm_set_fip_addr(uint32_t active_fw_bank_idx)
219 {
220 	uintptr_t dev_handle __unused;
221 	uintptr_t image_spec __unused;
222 
223 	assert(active_fw_bank_idx < NR_OF_FW_BANKS);
224 
225 	INFO("Booting with partition %s\n", fip_part_names[active_fw_bank_idx]);
226 
227 	int result = arm_set_image_source(FIP_IMAGE_ID,
228 					  fip_part_names[active_fw_bank_idx],
229 					  &dev_handle,
230 					  &image_spec);
231 	if (result != 0) {
232 		panic();
233 	}
234 }
235 #endif /* ARM_GPT_SUPPORT */
236 
237 #if PSA_FWU_SUPPORT
238 /*******************************************************************************
239  * Read the FIP partition of the GPT image corresponding to the active firmware
240  * bank to get its offset and length, and update these details in the I/O policy
241  * of the FIP image.
242  ******************************************************************************/
plat_fwu_set_images_source(const struct fwu_metadata * metadata)243 void plat_fwu_set_images_source(const struct fwu_metadata *metadata)
244 {
245 	arm_set_fip_addr(metadata->active_index);
246 }
247 
248 /*******************************************************************************
249  * Read the requested FWU metadata partition of the GPT image to get its offset
250  * and length, and update these details in the I/O policy of the requested FWU
251  * metadata image.
252  ******************************************************************************/
plat_fwu_set_metadata_image_source(unsigned int image_id,uintptr_t * dev_handle,uintptr_t * image_spec)253 int plat_fwu_set_metadata_image_source(unsigned int image_id,
254 				       uintptr_t *dev_handle,
255 				       uintptr_t *image_spec)
256 {
257 	int result = -1;
258 
259 	if (image_id == FWU_METADATA_IMAGE_ID) {
260 		result = arm_set_image_source(FWU_METADATA_IMAGE_ID,
261 					      "FWU-Metadata",
262 					      dev_handle,
263 					      image_spec);
264 	} else if (image_id == BKUP_FWU_METADATA_IMAGE_ID) {
265 		result = arm_set_image_source(BKUP_FWU_METADATA_IMAGE_ID,
266 					      "Bkup-FWU-Metadata",
267 					      dev_handle,
268 					      image_spec);
269 	}
270 
271 	return result;
272 }
273 #endif /* PSA_FWU_SUPPORT */
274 
275 #ifndef DECRYPTION_SUPPORT_none
open_enc_fip(const uintptr_t spec)276 int open_enc_fip(const uintptr_t spec)
277 {
278 	int result;
279 	uintptr_t local_image_handle;
280 
281 	/* See if an encrypted FIP is available */
282 	result = io_dev_init(enc_dev_handle, (uintptr_t)ENC_IMAGE_ID);
283 	if (result == 0) {
284 		result = io_open(enc_dev_handle, spec, &local_image_handle);
285 		if (result == 0) {
286 			VERBOSE("Using encrypted FIP\n");
287 			io_close(local_image_handle);
288 		}
289 	}
290 	return result;
291 }
292 #endif
293