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