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