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