Merge tag 'u-boot-at91-fixes-2023.01-a' of https://source.denx.de/u-boot/custodians...
[platform/kernel/u-boot.git] / boot / image-board.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Image code used by boards (and not host tools)
4  *
5  * (C) Copyright 2008 Semihalf
6  *
7  * (C) Copyright 2000-2006
8  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
9  */
10
11 #include <common.h>
12 #include <bootstage.h>
13 #include <cpu_func.h>
14 #include <display_options.h>
15 #include <env.h>
16 #include <fpga.h>
17 #include <image.h>
18 #include <init.h>
19 #include <log.h>
20 #include <mapmem.h>
21 #include <rtc.h>
22 #include <watchdog.h>
23 #include <asm/cache.h>
24 #include <asm/global_data.h>
25
26 DECLARE_GLOBAL_DATA_PTR;
27
28 /**
29  * image_get_ramdisk - get and verify ramdisk image
30  * @rd_addr: ramdisk image start address
31  * @arch: expected ramdisk architecture
32  * @verify: checksum verification flag
33  *
34  * image_get_ramdisk() returns a pointer to the verified ramdisk image
35  * header. Routine receives image start address and expected architecture
36  * flag. Verification done covers data and header integrity and os/type/arch
37  * fields checking.
38  *
39  * returns:
40  *     pointer to a ramdisk image header, if image was found and valid
41  *     otherwise, return NULL
42  */
43 static const struct legacy_img_hdr *image_get_ramdisk(ulong rd_addr, u8 arch,
44                                                       int verify)
45 {
46         const struct legacy_img_hdr *rd_hdr = (const struct legacy_img_hdr *)rd_addr;
47
48         if (!image_check_magic(rd_hdr)) {
49                 puts("Bad Magic Number\n");
50                 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
51                 return NULL;
52         }
53
54         if (!image_check_hcrc(rd_hdr)) {
55                 puts("Bad Header Checksum\n");
56                 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
57                 return NULL;
58         }
59
60         bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
61         image_print_contents(rd_hdr);
62
63         if (verify) {
64                 puts("   Verifying Checksum ... ");
65                 if (!image_check_dcrc(rd_hdr)) {
66                         puts("Bad Data CRC\n");
67                         bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
68                         return NULL;
69                 }
70                 puts("OK\n");
71         }
72
73         bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
74
75         if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
76             !image_check_arch(rd_hdr, arch) ||
77             !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
78                 printf("No Linux %s Ramdisk Image\n",
79                        genimg_get_arch_name(arch));
80                 bootstage_error(BOOTSTAGE_ID_RAMDISK);
81                 return NULL;
82         }
83
84         return rd_hdr;
85 }
86
87 /*****************************************************************************/
88 /* Shared dual-format routines */
89 /*****************************************************************************/
90 ulong image_load_addr = CONFIG_SYS_LOAD_ADDR;   /* Default Load Address */
91 ulong image_save_addr;                  /* Default Save Address */
92 ulong image_save_size;                  /* Default Save Size (in bytes) */
93
94 static int on_loadaddr(const char *name, const char *value, enum env_op op,
95                        int flags)
96 {
97         switch (op) {
98         case env_op_create:
99         case env_op_overwrite:
100                 image_load_addr = hextoul(value, NULL);
101                 break;
102         default:
103                 break;
104         }
105
106         return 0;
107 }
108 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
109
110 ulong env_get_bootm_low(void)
111 {
112         char *s = env_get("bootm_low");
113
114         if (s) {
115                 ulong tmp = hextoul(s, NULL);
116                 return tmp;
117         }
118
119 #if defined(CONFIG_SYS_SDRAM_BASE)
120         return CONFIG_SYS_SDRAM_BASE;
121 #elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
122         return gd->bd->bi_dram[0].start;
123 #else
124         return 0;
125 #endif
126 }
127
128 phys_size_t env_get_bootm_size(void)
129 {
130         phys_size_t tmp, size;
131         phys_addr_t start;
132         char *s = env_get("bootm_size");
133
134         if (s) {
135                 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
136                 return tmp;
137         }
138
139         start = gd->ram_base;
140         size = gd->ram_size;
141
142         if (start + size > gd->ram_top)
143                 size = gd->ram_top - start;
144
145         s = env_get("bootm_low");
146         if (s)
147                 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
148         else
149                 tmp = start;
150
151         return size - (tmp - start);
152 }
153
154 phys_size_t env_get_bootm_mapsize(void)
155 {
156         phys_size_t tmp;
157         char *s = env_get("bootm_mapsize");
158
159         if (s) {
160                 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
161                 return tmp;
162         }
163
164 #if defined(CONFIG_SYS_BOOTMAPSZ)
165         return CONFIG_SYS_BOOTMAPSZ;
166 #else
167         return env_get_bootm_size();
168 #endif
169 }
170
171 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
172 {
173         if (to == from)
174                 return;
175
176         if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
177                 if (to > from) {
178                         from += len;
179                         to += len;
180                 }
181                 while (len > 0) {
182                         size_t tail = (len > chunksz) ? chunksz : len;
183
184                         schedule();
185                         if (to > from) {
186                                 to -= tail;
187                                 from -= tail;
188                         }
189                         memmove(to, from, tail);
190                         if (to < from) {
191                                 to += tail;
192                                 from += tail;
193                         }
194                         len -= tail;
195                 }
196         } else {
197                 memmove(to, from, len);
198         }
199 }
200
201 /**
202  * genimg_get_kernel_addr_fit - get the real kernel address and return 2
203  *                              FIT strings
204  * @img_addr: a string might contain real image address
205  * @fit_uname_config: double pointer to a char, will hold pointer to a
206  *                    configuration unit name
207  * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
208  *                    name
209  *
210  * genimg_get_kernel_addr_fit get the real kernel start address from a string
211  * which is normally the first argv of bootm/bootz
212  *
213  * returns:
214  *     kernel start address
215  */
216 ulong genimg_get_kernel_addr_fit(char * const img_addr,
217                                  const char **fit_uname_config,
218                                  const char **fit_uname_kernel)
219 {
220         ulong kernel_addr;
221
222         /* find out kernel image address */
223         if (!img_addr) {
224                 kernel_addr = image_load_addr;
225                 debug("*  kernel: default image load address = 0x%08lx\n",
226                       image_load_addr);
227         } else if (CONFIG_IS_ENABLED(FIT) &&
228                    fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
229                                   fit_uname_config)) {
230                 debug("*  kernel: config '%s' from image at 0x%08lx\n",
231                       *fit_uname_config, kernel_addr);
232         } else if (CONFIG_IS_ENABLED(FIT) &&
233                    fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
234                                       fit_uname_kernel)) {
235                 debug("*  kernel: subimage '%s' from image at 0x%08lx\n",
236                       *fit_uname_kernel, kernel_addr);
237         } else {
238                 kernel_addr = hextoul(img_addr, NULL);
239                 debug("*  kernel: cmdline image address = 0x%08lx\n",
240                       kernel_addr);
241         }
242
243         return kernel_addr;
244 }
245
246 /**
247  * genimg_get_kernel_addr() is the simple version of
248  * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
249  */
250 ulong genimg_get_kernel_addr(char * const img_addr)
251 {
252         const char *fit_uname_config = NULL;
253         const char *fit_uname_kernel = NULL;
254
255         return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
256                                           &fit_uname_kernel);
257 }
258
259 /**
260  * genimg_get_format - get image format type
261  * @img_addr: image start address
262  *
263  * genimg_get_format() checks whether provided address points to a valid
264  * legacy or FIT image.
265  *
266  * New uImage format and FDT blob are based on a libfdt. FDT blob
267  * may be passed directly or embedded in a FIT image. In both situations
268  * genimg_get_format() must be able to dectect libfdt header.
269  *
270  * returns:
271  *     image format type or IMAGE_FORMAT_INVALID if no image is present
272  */
273 int genimg_get_format(const void *img_addr)
274 {
275         if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
276                 const struct legacy_img_hdr *hdr;
277
278                 hdr = (const struct legacy_img_hdr *)img_addr;
279                 if (image_check_magic(hdr))
280                         return IMAGE_FORMAT_LEGACY;
281         }
282         if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
283                 if (!fdt_check_header(img_addr))
284                         return IMAGE_FORMAT_FIT;
285         }
286         if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
287             !android_image_check_header(img_addr))
288                 return IMAGE_FORMAT_ANDROID;
289
290         return IMAGE_FORMAT_INVALID;
291 }
292
293 /**
294  * fit_has_config - check if there is a valid FIT configuration
295  * @images: pointer to the bootm command headers structure
296  *
297  * fit_has_config() checks if there is a FIT configuration in use
298  * (if FTI support is present).
299  *
300  * returns:
301  *     0, no FIT support or no configuration found
302  *     1, configuration found
303  */
304 int genimg_has_config(struct bootm_headers *images)
305 {
306         if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
307                 return 1;
308
309         return 0;
310 }
311
312 /**
313  * select_ramdisk() - Select and locate the ramdisk to use
314  *
315  * @images: pointer to the bootm images structure
316  * @select: name of ramdisk to select, or hex address, NULL for any
317  * @arch: expected ramdisk architecture
318  * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
319  * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
320  * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
321  *      other -ve value on other error
322  */
323 static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch,
324                           ulong *rd_datap, ulong *rd_lenp)
325 {
326         const char *fit_uname_config;
327         const char *fit_uname_ramdisk;
328         bool done_select = !select;
329         bool done = false;
330         int rd_noffset;
331         ulong rd_addr;
332         char *buf;
333
334         if (CONFIG_IS_ENABLED(FIT)) {
335                 fit_uname_config = images->fit_uname_cfg;
336                 fit_uname_ramdisk = NULL;
337
338                 if (select) {
339                         ulong default_addr;
340                         /*
341                          * If the init ramdisk comes from the FIT image and
342                          * the FIT image address is omitted in the command
343                          * line argument, try to use os FIT image address or
344                          * default load address.
345                          */
346                         if (images->fit_uname_os)
347                                 default_addr = (ulong)images->fit_hdr_os;
348                         else
349                                 default_addr = image_load_addr;
350
351                         if (fit_parse_conf(select, default_addr, &rd_addr,
352                                            &fit_uname_config)) {
353                                 debug("*  ramdisk: config '%s' from image at 0x%08lx\n",
354                                       fit_uname_config, rd_addr);
355                                 done_select = true;
356                         } else if (fit_parse_subimage(select, default_addr,
357                                                       &rd_addr,
358                                                       &fit_uname_ramdisk)) {
359                                 debug("*  ramdisk: subimage '%s' from image at 0x%08lx\n",
360                                       fit_uname_ramdisk, rd_addr);
361                                 done_select = true;
362                         }
363                 }
364         }
365         if (!done_select) {
366                 rd_addr = hextoul(select, NULL);
367                 debug("*  ramdisk: cmdline image address = 0x%08lx\n", rd_addr);
368         }
369         if (CONFIG_IS_ENABLED(FIT) && !select) {
370                 /* use FIT configuration provided in first bootm
371                  * command argument. If the property is not defined,
372                  * quit silently (with -ENOPKG)
373                  */
374                 rd_addr = map_to_sysmem(images->fit_hdr_os);
375                 rd_noffset = fit_get_node_from_config(images, FIT_RAMDISK_PROP,
376                                                       rd_addr);
377                 if (rd_noffset == -ENOENT)
378                         return -ENOPKG;
379                 else if (rd_noffset < 0)
380                         return rd_noffset;
381         }
382
383         /*
384          * Check if there is an initrd image at the
385          * address provided in the second bootm argument
386          * check image type, for FIT images get FIT node.
387          */
388         buf = map_sysmem(rd_addr, 0);
389         switch (genimg_get_format(buf)) {
390         case IMAGE_FORMAT_LEGACY:
391                 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
392                         const struct legacy_img_hdr *rd_hdr;
393
394                         printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
395                                rd_addr);
396
397                         bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
398                         rd_hdr = image_get_ramdisk(rd_addr, arch,
399                                                    images->verify);
400
401                         if (!rd_hdr)
402                                 return -ENOENT;
403
404                         *rd_datap = image_get_data(rd_hdr);
405                         *rd_lenp = image_get_data_size(rd_hdr);
406                         done = true;
407                 }
408                 break;
409         case IMAGE_FORMAT_FIT:
410                 if (CONFIG_IS_ENABLED(FIT)) {
411                         rd_noffset = fit_image_load(images, rd_addr,
412                                                     &fit_uname_ramdisk,
413                                                     &fit_uname_config,
414                                                     arch, IH_TYPE_RAMDISK,
415                                                     BOOTSTAGE_ID_FIT_RD_START,
416                                                     FIT_LOAD_OPTIONAL_NON_ZERO,
417                                                     rd_datap, rd_lenp);
418                         if (rd_noffset < 0)
419                                 return rd_noffset;
420
421                         images->fit_hdr_rd = map_sysmem(rd_addr, 0);
422                         images->fit_uname_rd = fit_uname_ramdisk;
423                         images->fit_noffset_rd = rd_noffset;
424                         done = true;
425                 }
426                 break;
427         case IMAGE_FORMAT_ANDROID:
428                 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
429                         void *ptr = map_sysmem(images->os.start, 0);
430                         int ret;
431
432                         ret = android_image_get_ramdisk(ptr, rd_datap, rd_lenp);
433                         unmap_sysmem(ptr);
434                         if (ret)
435                                 return ret;
436                         done = true;
437                 }
438                 break;
439         }
440
441         if (!done) {
442                 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
443                         char *end = NULL;
444
445                         if (select)
446                                 end = strchr(select, ':');
447                         if (end) {
448                                 *rd_lenp = hextoul(++end, NULL);
449                                 *rd_datap = rd_addr;
450                                 done = true;
451                         }
452                 }
453
454                 if (!done) {
455                         puts("Wrong Ramdisk Image Format\n");
456                         return -EINVAL;
457                 }
458         }
459
460         return 0;
461 }
462
463 /**
464  * boot_get_ramdisk - main ramdisk handling routine
465  * @argc: command argument count
466  * @argv: command argument list
467  * @images: pointer to the bootm images structure
468  * @arch: expected ramdisk architecture
469  * @rd_start: pointer to a ulong variable, will hold ramdisk start address
470  * @rd_end: pointer to a ulong variable, will hold ramdisk end
471  *
472  * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
473  * Currently supported are the following ramdisk sources:
474  *      - multicomponent kernel/ramdisk image,
475  *      - commandline provided address of decicated ramdisk image.
476  *
477  * returns:
478  *     0, if ramdisk image was found and valid, or skiped
479  *     rd_start and rd_end are set to ramdisk start/end addresses if
480  *     ramdisk image is found and valid
481  *
482  *     1, if ramdisk image is found but corrupted, or invalid
483  *     rd_start and rd_end are set to 0 if no ramdisk exists
484  */
485 int boot_get_ramdisk(int argc, char *const argv[], struct bootm_headers *images,
486                      u8 arch, ulong *rd_start, ulong *rd_end)
487 {
488         ulong rd_data, rd_len;
489         const char *select = NULL;
490
491         *rd_start = 0;
492         *rd_end = 0;
493
494         if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
495                 char *buf;
496
497                 /* Look for an Android boot image */
498                 buf = map_sysmem(images->os.start, 0);
499                 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
500                         select = (argc == 0) ? env_get("loadaddr") : argv[0];
501         }
502
503         if (argc >= 2)
504                 select = argv[1];
505
506         /*
507          * Look for a '-' which indicates to ignore the
508          * ramdisk argument
509          */
510         if (select && strcmp(select, "-") ==  0) {
511                 debug("## Skipping init Ramdisk\n");
512                 rd_len = 0;
513                 rd_data = 0;
514         } else if (select || genimg_has_config(images)) {
515                 int ret;
516
517                 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
518                 if (ret == -ENOPKG)
519                         return 0;
520                 else if (ret)
521                         return ret;
522         } else if (images->legacy_hdr_valid &&
523                         image_check_type(&images->legacy_hdr_os_copy,
524                                          IH_TYPE_MULTI)) {
525                 /*
526                  * Now check if we have a legacy mult-component image,
527                  * get second entry data start address and len.
528                  */
529                 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
530                 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
531                        (ulong)images->legacy_hdr_os);
532
533                 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
534         } else {
535                 /*
536                  * no initrd image
537                  */
538                 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
539                 rd_len = 0;
540                 rd_data = 0;
541         }
542
543         if (!rd_data) {
544                 debug("## No init Ramdisk\n");
545         } else {
546                 *rd_start = rd_data;
547                 *rd_end = rd_data + rd_len;
548         }
549         debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
550               *rd_start, *rd_end);
551
552         return 0;
553 }
554
555 /**
556  * boot_ramdisk_high - relocate init ramdisk
557  * @lmb: pointer to lmb handle, will be used for memory mgmt
558  * @rd_data: ramdisk data start address
559  * @rd_len: ramdisk data length
560  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
561  *      start address (after possible relocation)
562  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
563  *      end address (after possible relocation)
564  *
565  * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
566  * variable and if requested ramdisk data is moved to a specified location.
567  *
568  * Initrd_start and initrd_end are set to final (after relocation) ramdisk
569  * start/end addresses if ramdisk image start and len were provided,
570  * otherwise set initrd_start and initrd_end set to zeros.
571  *
572  * returns:
573  *      0 - success
574  *     -1 - failure
575  */
576 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
577                       ulong *initrd_start, ulong *initrd_end)
578 {
579         char    *s;
580         ulong   initrd_high;
581         int     initrd_copy_to_ram = 1;
582
583         s = env_get("initrd_high");
584         if (s) {
585                 /* a value of "no" or a similar string will act like 0,
586                  * turning the "load high" feature off. This is intentional.
587                  */
588                 initrd_high = hextoul(s, NULL);
589                 if (initrd_high == ~0)
590                         initrd_copy_to_ram = 0;
591         } else {
592                 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
593         }
594
595         debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
596               initrd_high, initrd_copy_to_ram);
597
598         if (rd_data) {
599                 if (!initrd_copy_to_ram) {      /* zero-copy ramdisk support */
600                         debug("   in-place initrd\n");
601                         *initrd_start = rd_data;
602                         *initrd_end = rd_data + rd_len;
603                         lmb_reserve(lmb, rd_data, rd_len);
604                 } else {
605                         if (initrd_high)
606                                 *initrd_start = (ulong)lmb_alloc_base(lmb,
607                                                 rd_len, 0x1000, initrd_high);
608                         else
609                                 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
610                                                                  0x1000);
611
612                         if (*initrd_start == 0) {
613                                 puts("ramdisk - allocation error\n");
614                                 goto error;
615                         }
616                         bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
617
618                         *initrd_end = *initrd_start + rd_len;
619                         printf("   Loading Ramdisk to %08lx, end %08lx ... ",
620                                *initrd_start, *initrd_end);
621
622                         memmove_wd((void *)*initrd_start,
623                                    (void *)rd_data, rd_len, CHUNKSZ);
624
625                         /*
626                          * Ensure the image is flushed to memory to handle
627                          * AMP boot scenarios in which we might not be
628                          * HW cache coherent
629                          */
630                         if (IS_ENABLED(CONFIG_MP)) {
631                                 flush_cache((unsigned long)*initrd_start,
632                                             ALIGN(rd_len, ARCH_DMA_MINALIGN));
633                         }
634                         puts("OK\n");
635                 }
636         } else {
637                 *initrd_start = 0;
638                 *initrd_end = 0;
639         }
640         debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
641               *initrd_start, *initrd_end);
642
643         return 0;
644
645 error:
646         return -1;
647 }
648
649 int boot_get_setup(struct bootm_headers *images, u8 arch,
650                    ulong *setup_start, ulong *setup_len)
651 {
652         if (!CONFIG_IS_ENABLED(FIT))
653                 return -ENOENT;
654
655         return boot_get_setup_fit(images, arch, setup_start, setup_len);
656 }
657
658 int boot_get_fpga(int argc, char *const argv[], struct bootm_headers *images,
659                   u8 arch, const ulong *ld_start, ulong * const ld_len)
660 {
661         ulong tmp_img_addr, img_data, img_len;
662         void *buf;
663         int conf_noffset;
664         int fit_img_result;
665         const char *uname, *name;
666         int err;
667         int devnum = 0; /* TODO support multi fpga platforms */
668
669         if (!IS_ENABLED(CONFIG_FPGA))
670                 return -ENOSYS;
671
672         /* Check to see if the images struct has a FIT configuration */
673         if (!genimg_has_config(images)) {
674                 debug("## FIT configuration was not specified\n");
675                 return 0;
676         }
677
678         /*
679          * Obtain the os FIT header from the images struct
680          */
681         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
682         buf = map_sysmem(tmp_img_addr, 0);
683         /*
684          * Check image type. For FIT images get FIT node
685          * and attempt to locate a generic binary.
686          */
687         switch (genimg_get_format(buf)) {
688         case IMAGE_FORMAT_FIT:
689                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
690
691                 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
692                                            NULL);
693                 if (!uname) {
694                         debug("## FPGA image is not specified\n");
695                         return 0;
696                 }
697                 fit_img_result = fit_image_load(images,
698                                                 tmp_img_addr,
699                                                 (const char **)&uname,
700                                                 &images->fit_uname_cfg,
701                                                 arch,
702                                                 IH_TYPE_FPGA,
703                                                 BOOTSTAGE_ID_FPGA_INIT,
704                                                 FIT_LOAD_OPTIONAL_NON_ZERO,
705                                                 &img_data, &img_len);
706
707                 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
708                       uname, img_data, img_len);
709
710                 if (fit_img_result < 0) {
711                         /* Something went wrong! */
712                         return fit_img_result;
713                 }
714
715                 if (!fpga_is_partial_data(devnum, img_len)) {
716                         name = "full";
717                         err = fpga_loadbitstream(devnum, (char *)img_data,
718                                                  img_len, BIT_FULL);
719                         if (err)
720                                 err = fpga_load(devnum, (const void *)img_data,
721                                                 img_len, BIT_FULL, 0);
722                 } else {
723                         name = "partial";
724                         err = fpga_loadbitstream(devnum, (char *)img_data,
725                                                  img_len, BIT_PARTIAL);
726                         if (err)
727                                 err = fpga_load(devnum, (const void *)img_data,
728                                                 img_len, BIT_PARTIAL, 0);
729                 }
730
731                 if (err)
732                         return err;
733
734                 printf("   Programming %s bitstream... OK\n", name);
735                 break;
736         default:
737                 printf("The given image format is not supported (corrupt?)\n");
738                 return 1;
739         }
740
741         return 0;
742 }
743
744 static void fit_loadable_process(u8 img_type,
745                                  ulong img_data,
746                                  ulong img_len)
747 {
748         int i;
749         const unsigned int count =
750                         ll_entry_count(struct fit_loadable_tbl, fit_loadable);
751         struct fit_loadable_tbl *fit_loadable_handler =
752                         ll_entry_start(struct fit_loadable_tbl, fit_loadable);
753         /* For each loadable handler */
754         for (i = 0; i < count; i++, fit_loadable_handler++)
755                 /* matching this type */
756                 if (fit_loadable_handler->type == img_type)
757                         /* call that handler with this image data */
758                         fit_loadable_handler->handler(img_data, img_len);
759 }
760
761 int boot_get_loadable(int argc, char *const argv[], struct bootm_headers *images,
762                       u8 arch, const ulong *ld_start, ulong * const ld_len)
763 {
764         /*
765          * These variables are used to hold the current image location
766          * in system memory.
767          */
768         ulong tmp_img_addr;
769         /*
770          * These two variables are requirements for fit_image_load, but
771          * their values are not used
772          */
773         ulong img_data, img_len;
774         void *buf;
775         int loadables_index;
776         int conf_noffset;
777         int fit_img_result;
778         const char *uname;
779         u8 img_type;
780
781         /* Check to see if the images struct has a FIT configuration */
782         if (!genimg_has_config(images)) {
783                 debug("## FIT configuration was not specified\n");
784                 return 0;
785         }
786
787         /*
788          * Obtain the os FIT header from the images struct
789          */
790         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
791         buf = map_sysmem(tmp_img_addr, 0);
792         /*
793          * Check image type. For FIT images get FIT node
794          * and attempt to locate a generic binary.
795          */
796         switch (genimg_get_format(buf)) {
797         case IMAGE_FORMAT_FIT:
798                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
799
800                 for (loadables_index = 0;
801                      uname = fdt_stringlist_get(buf, conf_noffset,
802                                                 FIT_LOADABLE_PROP,
803                                                 loadables_index, NULL), uname;
804                      loadables_index++) {
805                         fit_img_result = fit_image_load(images, tmp_img_addr,
806                                                         &uname,
807                                                         &images->fit_uname_cfg,
808                                                         arch, IH_TYPE_LOADABLE,
809                                                         BOOTSTAGE_ID_FIT_LOADABLE_START,
810                                                         FIT_LOAD_OPTIONAL_NON_ZERO,
811                                                         &img_data, &img_len);
812                         if (fit_img_result < 0) {
813                                 /* Something went wrong! */
814                                 return fit_img_result;
815                         }
816
817                         fit_img_result = fit_image_get_node(buf, uname);
818                         if (fit_img_result < 0) {
819                                 /* Something went wrong! */
820                                 return fit_img_result;
821                         }
822                         fit_img_result = fit_image_get_type(buf,
823                                                             fit_img_result,
824                                                             &img_type);
825                         if (fit_img_result < 0) {
826                                 /* Something went wrong! */
827                                 return fit_img_result;
828                         }
829
830                         fit_loadable_process(img_type, img_data, img_len);
831                 }
832                 break;
833         default:
834                 printf("The given image format is not supported (corrupt?)\n");
835                 return 1;
836         }
837
838         return 0;
839 }
840
841 /**
842  * boot_get_cmdline - allocate and initialize kernel cmdline
843  * @lmb: pointer to lmb handle, will be used for memory mgmt
844  * @cmd_start: pointer to a ulong variable, will hold cmdline start
845  * @cmd_end: pointer to a ulong variable, will hold cmdline end
846  *
847  * This allocates space for kernel command line below
848  * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
849  * variable is present its contents is copied to allocated kernel
850  * command line.
851  *
852  * returns:
853  *      0 - success
854  *     -1 - failure
855  */
856 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
857 {
858         int barg;
859         char *cmdline;
860         char *s;
861
862         /*
863          * Help the compiler detect that this function is only called when
864          * CONFIG_SYS_BOOT_GET_CMDLINE is enabled
865          */
866         if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE))
867                 return 0;
868
869         barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE);
870         cmdline = (char *)(ulong)lmb_alloc_base(lmb, barg, 0xf,
871                                 env_get_bootm_mapsize() + env_get_bootm_low());
872         if (!cmdline)
873                 return -1;
874
875         s = env_get("bootargs");
876         if (!s)
877                 s = "";
878
879         strcpy(cmdline, s);
880
881         *cmd_start = (ulong)cmdline;
882         *cmd_end = *cmd_start + strlen(cmdline);
883
884         debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
885
886         return 0;
887 }
888
889 /**
890  * boot_get_kbd - allocate and initialize kernel copy of board info
891  * @lmb: pointer to lmb handle, will be used for memory mgmt
892  * @kbd: double pointer to board info data
893  *
894  * boot_get_kbd() allocates space for kernel copy of board info data below
895  * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
896  * with the current u-boot board info data.
897  *
898  * returns:
899  *      0 - success
900  *     -1 - failure
901  */
902 int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
903 {
904         *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
905                                                        sizeof(struct bd_info),
906                                                        0xf,
907                                                        env_get_bootm_mapsize() +
908                                                        env_get_bootm_low());
909         if (!*kbd)
910                 return -1;
911
912         **kbd = *gd->bd;
913
914         debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
915
916         if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI))
917                 do_bdinfo(NULL, 0, 0, NULL);
918
919         return 0;
920 }
921
922 int image_setup_linux(struct bootm_headers *images)
923 {
924         ulong of_size = images->ft_len;
925         char **of_flat_tree = &images->ft_addr;
926         struct lmb *lmb = images_lmb(images);
927         int ret;
928
929         /* This function cannot be called without lmb support */
930         if (!CONFIG_IS_ENABLED(LMB))
931                 return -EFAULT;
932         if (CONFIG_IS_ENABLED(OF_LIBFDT))
933                 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
934
935         if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
936                 ret = boot_get_cmdline(lmb, &images->cmdline_start,
937                                        &images->cmdline_end);
938                 if (ret) {
939                         puts("ERROR with allocation of cmdline\n");
940                         return ret;
941                 }
942         }
943
944         if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
945                 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
946                 if (ret)
947                         return ret;
948         }
949
950         if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
951                 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
952                 if (ret)
953                         return ret;
954         }
955
956         return 0;
957 }
958
959 void genimg_print_size(uint32_t size)
960 {
961         printf("%d Bytes = ", size);
962         print_size(size, "\n");
963 }
964
965 void genimg_print_time(time_t timestamp)
966 {
967         struct rtc_time tm;
968
969         rtc_to_tm(timestamp, &tm);
970         printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
971                tm.tm_year, tm.tm_mon, tm.tm_mday,
972                tm.tm_hour, tm.tm_min, tm.tm_sec);
973 }