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