Commit | Line | Data |
---|---|---|
f1dcee59 SG |
1 | /* |
2 | * Copyright (C) 2016 Google, Inc | |
3 | * Written by Simon Glass <sjg@chromium.org> | |
4 | * | |
a616c783 | 5 | * SPDX-License-Identifier: GPL-2.0+ |
f1dcee59 SG |
6 | */ |
7 | ||
8 | #include <common.h> | |
9 | #include <errno.h> | |
10 | #include <image.h> | |
11 | #include <libfdt.h> | |
12 | #include <spl.h> | |
13 | ||
7264f292 YS |
14 | #ifndef CONFIG_SYS_BOOTM_LEN |
15 | #define CONFIG_SYS_BOOTM_LEN (64 << 20) | |
16 | #endif | |
17 | ||
736806fb | 18 | /** |
a616c783 | 19 | * spl_fit_get_image_name(): By using the matching configuration subnode, |
736806fb AP |
20 | * retrieve the name of an image, specified by a property name and an index |
21 | * into that. | |
22 | * @fit: Pointer to the FDT blob. | |
23 | * @images: Offset of the /images subnode. | |
24 | * @type: Name of the property within the configuration subnode. | |
25 | * @index: Index into the list of strings in this property. | |
a616c783 | 26 | * @outname: Name of the image |
736806fb | 27 | * |
a616c783 | 28 | * Return: 0 on success, or a negative error number |
736806fb | 29 | */ |
a616c783 PT |
30 | static int spl_fit_get_image_name(const void *fit, int images, |
31 | const char *type, int index, | |
32 | char **outname) | |
4b9340ab AP |
33 | { |
34 | const char *name, *str; | |
a616c783 PT |
35 | __maybe_unused int node; |
36 | int conf_node; | |
4b9340ab AP |
37 | int len, i; |
38 | ||
3863f840 | 39 | conf_node = fit_find_config_node(fit); |
4b9340ab AP |
40 | if (conf_node < 0) { |
41 | #ifdef CONFIG_SPL_LIBCOMMON_SUPPORT | |
42 | printf("No matching DT out of these options:\n"); | |
43 | for (node = fdt_first_subnode(fit, conf_node); | |
44 | node >= 0; | |
45 | node = fdt_next_subnode(fit, node)) { | |
46 | name = fdt_getprop(fit, node, "description", &len); | |
47 | printf(" %s\n", name); | |
f1dcee59 | 48 | } |
4b9340ab AP |
49 | #endif |
50 | return conf_node; | |
51 | } | |
f1dcee59 | 52 | |
4b9340ab AP |
53 | name = fdt_getprop(fit, conf_node, type, &len); |
54 | if (!name) { | |
55 | debug("cannot find property '%s': %d\n", type, len); | |
56 | return -EINVAL; | |
57 | } | |
f1dcee59 | 58 | |
4b9340ab AP |
59 | str = name; |
60 | for (i = 0; i < index; i++) { | |
61 | str = strchr(str, '\0') + 1; | |
62 | if (!str || (str - name >= len)) { | |
63 | debug("no string for index %d\n", index); | |
64 | return -E2BIG; | |
65 | } | |
f1dcee59 SG |
66 | } |
67 | ||
a616c783 PT |
68 | *outname = (char *)str; |
69 | return 0; | |
70 | } | |
71 | ||
72 | /** | |
73 | * spl_fit_get_image_node(): By using the matching configuration subnode, | |
74 | * retrieve the name of an image, specified by a property name and an index | |
75 | * into that. | |
76 | * @fit: Pointer to the FDT blob. | |
77 | * @images: Offset of the /images subnode. | |
78 | * @type: Name of the property within the configuration subnode. | |
79 | * @index: Index into the list of strings in this property. | |
80 | * | |
81 | * Return: the node offset of the respective image node or a negative | |
82 | * error number. | |
83 | */ | |
84 | static int spl_fit_get_image_node(const void *fit, int images, | |
85 | const char *type, int index) | |
86 | { | |
87 | char *str; | |
88 | int err; | |
89 | int node; | |
90 | ||
91 | err = spl_fit_get_image_name(fit, images, type, index, &str); | |
92 | if (err) | |
93 | return err; | |
94 | ||
4b9340ab | 95 | debug("%s: '%s'\n", type, str); |
a616c783 | 96 | |
4b9340ab AP |
97 | node = fdt_subnode_offset(fit, images, str); |
98 | if (node < 0) { | |
99 | debug("cannot find image node '%s': %d\n", str, node); | |
100 | return -EINVAL; | |
f1dcee59 | 101 | } |
f1dcee59 | 102 | |
736806fb | 103 | return node; |
f1dcee59 SG |
104 | } |
105 | ||
eafd5410 LV |
106 | static int get_aligned_image_offset(struct spl_load_info *info, int offset) |
107 | { | |
108 | /* | |
109 | * If it is a FS read, get the first address before offset which is | |
110 | * aligned to ARCH_DMA_MINALIGN. If it is raw read return the | |
111 | * block number to which offset belongs. | |
112 | */ | |
113 | if (info->filename) | |
114 | return offset & ~(ARCH_DMA_MINALIGN - 1); | |
115 | ||
116 | return offset / info->bl_len; | |
117 | } | |
118 | ||
119 | static int get_aligned_image_overhead(struct spl_load_info *info, int offset) | |
120 | { | |
121 | /* | |
122 | * If it is a FS read, get the difference between the offset and | |
123 | * the first address before offset which is aligned to | |
124 | * ARCH_DMA_MINALIGN. If it is raw read return the offset within the | |
125 | * block. | |
126 | */ | |
127 | if (info->filename) | |
128 | return offset & (ARCH_DMA_MINALIGN - 1); | |
129 | ||
130 | return offset % info->bl_len; | |
131 | } | |
132 | ||
133 | static int get_aligned_image_size(struct spl_load_info *info, int data_size, | |
134 | int offset) | |
135 | { | |
3cc1f380 LV |
136 | data_size = data_size + get_aligned_image_overhead(info, offset); |
137 | ||
eafd5410 | 138 | if (info->filename) |
3cc1f380 | 139 | return data_size; |
eafd5410 LV |
140 | |
141 | return (data_size + info->bl_len - 1) / info->bl_len; | |
142 | } | |
143 | ||
8baa3818 AP |
144 | /** |
145 | * spl_load_fit_image(): load the image described in a certain FIT node | |
146 | * @info: points to information about the device to load data from | |
147 | * @sector: the start sector of the FIT image on the device | |
148 | * @fit: points to the flattened device tree blob describing the FIT | |
a616c783 | 149 | * image |
8baa3818 AP |
150 | * @base_offset: the beginning of the data area containing the actual |
151 | * image data, relative to the beginning of the FIT | |
152 | * @node: offset of the DT node describing the image to load (relative | |
a616c783 | 153 | * to @fit) |
8baa3818 | 154 | * @image_info: will be filled with information about the loaded image |
a616c783 PT |
155 | * If the FIT node does not contain a "load" (address) property, |
156 | * the image gets loaded to the address pointed to by the | |
157 | * load_addr member in this struct. | |
8baa3818 AP |
158 | * |
159 | * Return: 0 on success or a negative error number. | |
160 | */ | |
161 | static int spl_load_fit_image(struct spl_load_info *info, ulong sector, | |
162 | void *fit, ulong base_offset, int node, | |
163 | struct spl_image_info *image_info) | |
164 | { | |
5fd13d97 | 165 | int offset; |
8baa3818 | 166 | size_t length; |
5fd13d97 | 167 | int len; |
933f67aa | 168 | ulong size; |
8baa3818 AP |
169 | ulong load_addr, load_ptr; |
170 | void *src; | |
171 | ulong overhead; | |
172 | int nr_sectors; | |
173 | int align_len = ARCH_DMA_MINALIGN - 1; | |
7264f292 | 174 | uint8_t image_comp = -1, type = -1; |
5fd13d97 | 175 | const void *data; |
7264f292 YS |
176 | |
177 | if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && IS_ENABLED(CONFIG_SPL_GZIP)) { | |
178 | if (fit_image_get_comp(fit, node, &image_comp)) | |
179 | puts("Cannot get image compression format.\n"); | |
180 | else | |
181 | debug("%s ", genimg_get_comp_name(image_comp)); | |
182 | ||
183 | if (fit_image_get_type(fit, node, &type)) | |
184 | puts("Cannot get image type.\n"); | |
185 | else | |
186 | debug("%s ", genimg_get_type_name(type)); | |
187 | } | |
8baa3818 | 188 | |
5fd13d97 | 189 | if (fit_image_get_load(fit, node, &load_addr)) |
8baa3818 | 190 | load_addr = image_info->load_addr; |
8baa3818 | 191 | |
5fd13d97 YS |
192 | if (!fit_image_get_data_offset(fit, node, &offset)) { |
193 | /* External data */ | |
194 | offset += base_offset; | |
195 | if (fit_image_get_data_size(fit, node, &len)) | |
196 | return -ENOENT; | |
197 | ||
198 | load_ptr = (load_addr + align_len) & ~align_len; | |
199 | length = len; | |
200 | ||
201 | overhead = get_aligned_image_overhead(info, offset); | |
202 | nr_sectors = get_aligned_image_size(info, length, offset); | |
203 | ||
204 | if (info->read(info, | |
205 | sector + get_aligned_image_offset(info, offset), | |
206 | nr_sectors, (void *)load_ptr) != nr_sectors) | |
207 | return -EIO; | |
208 | ||
209 | debug("External data: dst=%lx, offset=%x, size=%lx\n", | |
210 | load_ptr, offset, (unsigned long)length); | |
211 | src = (void *)load_ptr + overhead; | |
212 | } else { | |
213 | /* Embedded data */ | |
214 | if (fit_image_get_data(fit, node, &data, &length)) { | |
215 | puts("Cannot get image data/size\n"); | |
216 | return -ENOENT; | |
217 | } | |
218 | debug("Embedded data: dst=%lx, size=%lx\n", load_addr, | |
219 | (unsigned long)length); | |
220 | src = (void *)data; | |
221 | } | |
8baa3818 | 222 | |
8baa3818 AP |
223 | #ifdef CONFIG_SPL_FIT_IMAGE_POST_PROCESS |
224 | board_fit_image_post_process(&src, &length); | |
225 | #endif | |
226 | ||
7264f292 YS |
227 | if (IS_ENABLED(CONFIG_SPL_OS_BOOT) && |
228 | IS_ENABLED(CONFIG_SPL_GZIP) && | |
229 | image_comp == IH_COMP_GZIP && | |
230 | type == IH_TYPE_KERNEL) { | |
933f67aa | 231 | size = length; |
7264f292 | 232 | if (gunzip((void *)load_addr, CONFIG_SYS_BOOTM_LEN, |
933f67aa | 233 | src, &size)) { |
7264f292 YS |
234 | puts("Uncompressing error\n"); |
235 | return -EIO; | |
236 | } | |
933f67aa | 237 | length = size; |
7264f292 YS |
238 | } else { |
239 | memcpy((void *)load_addr, src, length); | |
240 | } | |
8baa3818 AP |
241 | |
242 | if (image_info) { | |
243 | image_info->load_addr = load_addr; | |
244 | image_info->size = length; | |
245 | image_info->entry_point = fdt_getprop_u32(fit, node, "entry"); | |
246 | } | |
247 | ||
248 | return 0; | |
249 | } | |
250 | ||
d879616e PT |
251 | static int spl_fit_append_fdt(struct spl_image_info *spl_image, |
252 | struct spl_load_info *info, ulong sector, | |
253 | void *fit, int images, ulong base_offset) | |
254 | { | |
255 | struct spl_image_info image_info; | |
256 | int node, ret; | |
257 | ||
258 | /* Figure out which device tree the board wants to use */ | |
259 | node = spl_fit_get_image_node(fit, images, FIT_FDT_PROP, 0); | |
260 | if (node < 0) { | |
261 | debug("%s: cannot find FDT node\n", __func__); | |
262 | return node; | |
263 | } | |
264 | ||
265 | /* | |
266 | * Read the device tree and place it after the image. | |
267 | * Align the destination address to ARCH_DMA_MINALIGN. | |
268 | */ | |
269 | image_info.load_addr = spl_image->load_addr + spl_image->size; | |
270 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, | |
271 | &image_info); | |
a616c783 PT |
272 | |
273 | if (ret < 0) | |
274 | return ret; | |
275 | ||
276 | /* Make the load-address of the FDT available for the SPL framework */ | |
277 | spl_image->fdt_addr = (void *)image_info.load_addr; | |
337bbb62 | 278 | #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY) |
a616c783 PT |
279 | /* Try to make space, so we can inject details on the loadables */ |
280 | ret = fdt_shrink_to_minimum(spl_image->fdt_addr, 8192); | |
337bbb62 | 281 | #endif |
a616c783 PT |
282 | |
283 | return ret; | |
284 | } | |
285 | ||
286 | static int spl_fit_record_loadable(const void *fit, int images, int index, | |
287 | void *blob, struct spl_image_info *image) | |
288 | { | |
337bbb62 PT |
289 | int ret = 0; |
290 | #if !CONFIG_IS_ENABLED(FIT_IMAGE_TINY) | |
a616c783 | 291 | char *name; |
337bbb62 | 292 | int node; |
a616c783 PT |
293 | |
294 | ret = spl_fit_get_image_name(fit, images, "loadables", | |
295 | index, &name); | |
296 | if (ret < 0) | |
297 | return ret; | |
298 | ||
299 | node = spl_fit_get_image_node(fit, images, "loadables", index); | |
300 | ||
301 | ret = fdt_record_loadable(blob, index, name, image->load_addr, | |
302 | image->size, image->entry_point, | |
303 | fdt_getprop(fit, node, "type", NULL), | |
304 | fdt_getprop(fit, node, "os", NULL)); | |
337bbb62 | 305 | #endif |
d879616e PT |
306 | return ret; |
307 | } | |
308 | ||
337bbb62 PT |
309 | static int spl_fit_image_get_os(const void *fit, int noffset, uint8_t *os) |
310 | { | |
311 | #if CONFIG_IS_ENABLED(FIT_IMAGE_TINY) | |
312 | return -ENOTSUPP; | |
313 | #else | |
314 | return fit_image_get_os(fit, noffset, os); | |
315 | #endif | |
316 | } | |
317 | ||
f4d7d859 SG |
318 | int spl_load_simple_fit(struct spl_image_info *spl_image, |
319 | struct spl_load_info *info, ulong sector, void *fit) | |
f1dcee59 SG |
320 | { |
321 | int sectors; | |
8baa3818 | 322 | ulong size; |
f1dcee59 | 323 | unsigned long count; |
8baa3818 | 324 | struct spl_image_info image_info; |
c8bc3c0c YS |
325 | int node = -1; |
326 | int images, ret; | |
eafd5410 | 327 | int base_offset, align_len = ARCH_DMA_MINALIGN - 1; |
411cf32d | 328 | int index = 0; |
f1dcee59 SG |
329 | |
330 | /* | |
c8bc3c0c YS |
331 | * For FIT with external data, figure out where the external images |
332 | * start. This is the base for the data-offset properties in each | |
333 | * image. | |
f1dcee59 SG |
334 | */ |
335 | size = fdt_totalsize(fit); | |
336 | size = (size + 3) & ~3; | |
337 | base_offset = (size + 3) & ~3; | |
338 | ||
339 | /* | |
340 | * So far we only have one block of data from the FIT. Read the entire | |
341 | * thing, including that first block, placing it so it finishes before | |
342 | * where we will load the image. | |
343 | * | |
344 | * Note that we will load the image such that its first byte will be | |
345 | * at the load address. Since that byte may be part-way through a | |
346 | * block, we may load the image up to one block before the load | |
347 | * address. So take account of that here by subtracting an addition | |
348 | * block length from the FIT start position. | |
349 | * | |
350 | * In fact the FIT has its own load address, but we assume it cannot | |
351 | * be before CONFIG_SYS_TEXT_BASE. | |
c8bc3c0c YS |
352 | * |
353 | * For FIT with data embedded, data is loaded as part of FIT image. | |
354 | * For FIT with external data, data is not loaded in this step. | |
f1dcee59 | 355 | */ |
8b528709 LV |
356 | fit = (void *)((CONFIG_SYS_TEXT_BASE - size - info->bl_len - |
357 | align_len) & ~align_len); | |
eafd5410 | 358 | sectors = get_aligned_image_size(info, size, 0); |
f1dcee59 SG |
359 | count = info->read(info, sector, sectors, fit); |
360 | debug("fit read sector %lx, sectors=%d, dst=%p, count=%lu\n", | |
361 | sector, sectors, fit, count); | |
362 | if (count == 0) | |
363 | return -EIO; | |
364 | ||
736806fb | 365 | /* find the node holding the images information */ |
f1dcee59 SG |
366 | images = fdt_path_offset(fit, FIT_IMAGES_PATH); |
367 | if (images < 0) { | |
368 | debug("%s: Cannot find /images node: %d\n", __func__, images); | |
369 | return -1; | |
370 | } | |
736806fb | 371 | |
d879616e PT |
372 | /* |
373 | * Find the U-Boot image using the following search order: | |
374 | * - start at 'firmware' (e.g. an ARM Trusted Firmware) | |
375 | * - fall back 'kernel' (e.g. a Falcon-mode OS boot | |
376 | * - fall back to using the first 'loadables' entry | |
377 | */ | |
378 | if (node < 0) | |
379 | node = spl_fit_get_image_node(fit, images, "firmware", 0); | |
c8bc3c0c | 380 | #ifdef CONFIG_SPL_OS_BOOT |
c8bc3c0c | 381 | if (node < 0) |
d879616e | 382 | node = spl_fit_get_image_node(fit, images, FIT_KERNEL_PROP, 0); |
c8bc3c0c | 383 | #endif |
f1dcee59 | 384 | if (node < 0) { |
736806fb AP |
385 | debug("could not find firmware image, trying loadables...\n"); |
386 | node = spl_fit_get_image_node(fit, images, "loadables", 0); | |
411cf32d AP |
387 | /* |
388 | * If we pick the U-Boot image from "loadables", start at | |
389 | * the second image when later loading additional images. | |
390 | */ | |
391 | index = 1; | |
736806fb AP |
392 | } |
393 | if (node < 0) { | |
394 | debug("%s: Cannot find u-boot image node: %d\n", | |
395 | __func__, node); | |
f1dcee59 SG |
396 | return -1; |
397 | } | |
398 | ||
8baa3818 AP |
399 | /* Load the image and set up the spl_image structure */ |
400 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, | |
401 | spl_image); | |
402 | if (ret) | |
403 | return ret; | |
da74d1f3 | 404 | |
d879616e PT |
405 | /* |
406 | * For backward compatibility, we treat the first node that is | |
407 | * as a U-Boot image, if no OS-type has been declared. | |
408 | */ | |
337bbb62 | 409 | if (!spl_fit_image_get_os(fit, node, &spl_image->os)) |
c8bc3c0c | 410 | debug("Image OS is %s\n", genimg_get_os_name(spl_image->os)); |
d879616e PT |
411 | #if !defined(CONFIG_SPL_OS_BOOT) |
412 | else | |
413 | spl_image->os = IH_OS_U_BOOT; | |
c8bc3c0c | 414 | #endif |
f1dcee59 | 415 | |
d879616e PT |
416 | /* |
417 | * Booting a next-stage U-Boot may require us to append the FDT. | |
418 | * We allow this to fail, as the U-Boot image might embed its FDT. | |
419 | */ | |
420 | if (spl_image->os == IH_OS_U_BOOT) | |
421 | spl_fit_append_fdt(spl_image, info, sector, fit, | |
422 | images, base_offset); | |
411cf32d AP |
423 | |
424 | /* Now check if there are more images for us to load */ | |
425 | for (; ; index++) { | |
d879616e PT |
426 | uint8_t os_type = IH_OS_INVALID; |
427 | ||
411cf32d AP |
428 | node = spl_fit_get_image_node(fit, images, "loadables", index); |
429 | if (node < 0) | |
430 | break; | |
431 | ||
432 | ret = spl_load_fit_image(info, sector, fit, base_offset, node, | |
433 | &image_info); | |
434 | if (ret < 0) | |
435 | continue; | |
436 | ||
337bbb62 | 437 | if (!spl_fit_image_get_os(fit, node, &os_type)) |
d879616e PT |
438 | debug("Loadable is %s\n", genimg_get_os_name(os_type)); |
439 | ||
a616c783 PT |
440 | if (os_type == IH_OS_U_BOOT) { |
441 | spl_fit_append_fdt(&image_info, info, sector, | |
d879616e | 442 | fit, images, base_offset); |
a616c783 PT |
443 | spl_image->fdt_addr = image_info.fdt_addr; |
444 | } | |
d879616e | 445 | |
411cf32d AP |
446 | /* |
447 | * If the "firmware" image did not provide an entry point, | |
448 | * use the first valid entry point from the loadables. | |
449 | */ | |
450 | if (spl_image->entry_point == FDT_ERROR && | |
451 | image_info.entry_point != FDT_ERROR) | |
452 | spl_image->entry_point = image_info.entry_point; | |
a616c783 PT |
453 | |
454 | /* Record our loadables into the FDT */ | |
455 | if (spl_image->fdt_addr) | |
456 | spl_fit_record_loadable(fit, images, index, | |
457 | spl_image->fdt_addr, | |
458 | &image_info); | |
411cf32d AP |
459 | } |
460 | ||
461 | /* | |
462 | * If a platform does not provide CONFIG_SYS_UBOOT_START, U-Boot's | |
463 | * Makefile will set it to 0 and it will end up as the entry point | |
464 | * here. What it actually means is: use the load address. | |
465 | */ | |
466 | if (spl_image->entry_point == FDT_ERROR || spl_image->entry_point == 0) | |
467 | spl_image->entry_point = spl_image->load_addr; | |
468 | ||
469 | return 0; | |
f1dcee59 | 470 | } |