zynqmp: Run board_get_usable_ram_top() only on main U-Boot
[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 <env.h>
15 #include <fpga.h>
16 #include <image.h>
17 #include <init.h>
18 #include <mapmem.h>
19 #include <rtc.h>
20 #include <watchdog.h>
21 #include <asm/cache.h>
22 #include <asm/global_data.h>
23
24 DECLARE_GLOBAL_DATA_PTR;
25
26 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
27 /**
28  * image_get_ramdisk - get and verify ramdisk image
29  * @rd_addr: ramdisk image start address
30  * @arch: expected ramdisk architecture
31  * @verify: checksum verification flag
32  *
33  * image_get_ramdisk() returns a pointer to the verified ramdisk image
34  * header. Routine receives image start address and expected architecture
35  * flag. Verification done covers data and header integrity and os/type/arch
36  * fields checking.
37  *
38  * returns:
39  *     pointer to a ramdisk image header, if image was found and valid
40  *     otherwise, return NULL
41  */
42 static const image_header_t *image_get_ramdisk(ulong rd_addr, u8 arch,
43                                                int verify)
44 {
45         const image_header_t *rd_hdr = (const image_header_t *)rd_addr;
46
47         if (!image_check_magic(rd_hdr)) {
48                 puts("Bad Magic Number\n");
49                 bootstage_error(BOOTSTAGE_ID_RD_MAGIC);
50                 return NULL;
51         }
52
53         if (!image_check_hcrc(rd_hdr)) {
54                 puts("Bad Header Checksum\n");
55                 bootstage_error(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
56                 return NULL;
57         }
58
59         bootstage_mark(BOOTSTAGE_ID_RD_MAGIC);
60         image_print_contents(rd_hdr);
61
62         if (verify) {
63                 puts("   Verifying Checksum ... ");
64                 if (!image_check_dcrc(rd_hdr)) {
65                         puts("Bad Data CRC\n");
66                         bootstage_error(BOOTSTAGE_ID_RD_CHECKSUM);
67                         return NULL;
68                 }
69                 puts("OK\n");
70         }
71
72         bootstage_mark(BOOTSTAGE_ID_RD_HDR_CHECKSUM);
73
74         if (!image_check_os(rd_hdr, IH_OS_LINUX) ||
75             !image_check_arch(rd_hdr, arch) ||
76             !image_check_type(rd_hdr, IH_TYPE_RAMDISK)) {
77                 printf("No Linux %s Ramdisk Image\n",
78                        genimg_get_arch_name(arch));
79                 bootstage_error(BOOTSTAGE_ID_RAMDISK);
80                 return NULL;
81         }
82
83         return rd_hdr;
84 }
85 #endif
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 defined(CONFIG_HW_WATCHDOG) || defined(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                 WATCHDOG_RESET();
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   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
197         memmove(to, from, len);
198 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
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 image_header_t *hdr;
277
278                 hdr = (const image_header_t *)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(bootm_headers_t *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 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(bootm_headers_t *images, const char *select, u8 arch,
324                           ulong *rd_datap, ulong *rd_lenp)
325 {
326         ulong rd_addr;
327         char *buf;
328
329 #if CONFIG_IS_ENABLED(FIT)
330                 const char *fit_uname_config = images->fit_uname_cfg;
331                 const char *fit_uname_ramdisk = NULL;
332                 int rd_noffset;
333
334                 if (select) {
335                         ulong default_addr;
336                         /*
337                          * If the init ramdisk comes from the FIT image and
338                          * the FIT image address is omitted in the command
339                          * line argument, try to use os FIT image address or
340                          * default load address.
341                          */
342                         if (images->fit_uname_os)
343                                 default_addr = (ulong)images->fit_hdr_os;
344                         else
345                                 default_addr = image_load_addr;
346
347                         if (fit_parse_conf(select, default_addr,
348                                            &rd_addr, &fit_uname_config)) {
349                                 debug("*  ramdisk: config '%s' from image at 0x%08lx\n",
350                                       fit_uname_config, rd_addr);
351                         } else if (fit_parse_subimage(select, default_addr,
352                                                       &rd_addr,
353                                                       &fit_uname_ramdisk)) {
354                                 debug("*  ramdisk: subimage '%s' from image at 0x%08lx\n",
355                                       fit_uname_ramdisk, rd_addr);
356                         } else
357 #endif
358                         {
359                                 rd_addr = hextoul(select, NULL);
360                                 debug("*  ramdisk: cmdline image address = 0x%08lx\n",
361                                       rd_addr);
362                         }
363 #if CONFIG_IS_ENABLED(FIT)
364                 } else {
365                         /* use FIT configuration provided in first bootm
366                          * command argument. If the property is not defined,
367                          * quit silently (with -ENOPKG)
368                          */
369                         rd_addr = map_to_sysmem(images->fit_hdr_os);
370                         rd_noffset = fit_get_node_from_config(images,
371                                                               FIT_RAMDISK_PROP,
372                                                               rd_addr);
373                         if (rd_noffset == -ENOENT)
374                                 return -ENOPKG;
375                         else if (rd_noffset < 0)
376                                 return rd_noffset;
377                 }
378 #endif
379
380                 /*
381                  * Check if there is an initrd image at the
382                  * address provided in the second bootm argument
383                  * check image type, for FIT images get FIT node.
384                  */
385                 buf = map_sysmem(rd_addr, 0);
386                 switch (genimg_get_format(buf)) {
387 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
388                 case IMAGE_FORMAT_LEGACY: {
389                         const image_header_t *rd_hdr;
390
391                         printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
392                                rd_addr);
393
394                         bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
395                         rd_hdr = image_get_ramdisk(rd_addr, arch,
396                                                    images->verify);
397
398                         if (!rd_hdr)
399                                 return -ENOENT;
400
401                         *rd_datap = image_get_data(rd_hdr);
402                         *rd_lenp = image_get_data_size(rd_hdr);
403                         break;
404                 }
405 #endif
406 #if CONFIG_IS_ENABLED(FIT)
407                 case IMAGE_FORMAT_FIT:
408                         rd_noffset = fit_image_load(images,
409                                                     rd_addr, &fit_uname_ramdisk,
410                                                     &fit_uname_config, arch,
411                                                     IH_TYPE_RAMDISK,
412                                                     BOOTSTAGE_ID_FIT_RD_START,
413                                                     FIT_LOAD_OPTIONAL_NON_ZERO,
414                                                     rd_datap, rd_lenp);
415                         if (rd_noffset < 0)
416                                 return rd_noffset;
417
418                         images->fit_hdr_rd = map_sysmem(rd_addr, 0);
419                         images->fit_uname_rd = fit_uname_ramdisk;
420                         images->fit_noffset_rd = rd_noffset;
421                         break;
422 #endif
423 #ifdef CONFIG_ANDROID_BOOT_IMAGE
424                 case IMAGE_FORMAT_ANDROID:
425                         android_image_get_ramdisk((void *)images->os.start,
426                                                   rd_datap, rd_lenp);
427                         break;
428 #endif
429                 default:
430                         if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
431                                 char *end = NULL;
432
433                                 if (select)
434                                         end = strchr(select, ':');
435                                 if (end) {
436                                         *rd_lenp = hextoul(++end, NULL);
437                                         *rd_datap = rd_addr;
438                                         break;
439                                 }
440                         }
441                         puts("Wrong Ramdisk Image Format\n");
442                         return -EINVAL;
443                 }
444
445         return 0;
446 }
447
448 /**
449  * boot_get_ramdisk - main ramdisk handling routine
450  * @argc: command argument count
451  * @argv: command argument list
452  * @images: pointer to the bootm images structure
453  * @arch: expected ramdisk architecture
454  * @rd_start: pointer to a ulong variable, will hold ramdisk start address
455  * @rd_end: pointer to a ulong variable, will hold ramdisk end
456  *
457  * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
458  * Currently supported are the following ramdisk sources:
459  *      - multicomponent kernel/ramdisk image,
460  *      - commandline provided address of decicated ramdisk image.
461  *
462  * returns:
463  *     0, if ramdisk image was found and valid, or skiped
464  *     rd_start and rd_end are set to ramdisk start/end addresses if
465  *     ramdisk image is found and valid
466  *
467  *     1, if ramdisk image is found but corrupted, or invalid
468  *     rd_start and rd_end are set to 0 if no ramdisk exists
469  */
470 int boot_get_ramdisk(int argc, char *const argv[], bootm_headers_t *images,
471                      u8 arch, ulong *rd_start, ulong *rd_end)
472 {
473         ulong rd_data, rd_len;
474         const char *select = NULL;
475
476         *rd_start = 0;
477         *rd_end = 0;
478
479         if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
480                 char *buf;
481
482                 /* Look for an Android boot image */
483                 buf = map_sysmem(images->os.start, 0);
484                 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
485                         select = (argc == 0) ? env_get("loadaddr") : argv[0];
486         }
487
488         if (argc >= 2)
489                 select = argv[1];
490
491         /*
492          * Look for a '-' which indicates to ignore the
493          * ramdisk argument
494          */
495         if (select && strcmp(select, "-") ==  0) {
496                 debug("## Skipping init Ramdisk\n");
497                 rd_len = 0;
498                 rd_data = 0;
499         } else if (select || genimg_has_config(images)) {
500                 int ret;
501
502                 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
503                 if (ret == -ENOPKG)
504                         return 0;
505                 else if (ret)
506                         return ret;
507         } else if (images->legacy_hdr_valid &&
508                         image_check_type(&images->legacy_hdr_os_copy,
509                                          IH_TYPE_MULTI)) {
510                 /*
511                  * Now check if we have a legacy mult-component image,
512                  * get second entry data start address and len.
513                  */
514                 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
515                 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
516                        (ulong)images->legacy_hdr_os);
517
518                 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
519         } else {
520                 /*
521                  * no initrd image
522                  */
523                 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
524                 rd_len = 0;
525                 rd_data = 0;
526         }
527
528         if (!rd_data) {
529                 debug("## No init Ramdisk\n");
530         } else {
531                 *rd_start = rd_data;
532                 *rd_end = rd_data + rd_len;
533         }
534         debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
535               *rd_start, *rd_end);
536
537         return 0;
538 }
539
540 #if defined(CONFIG_LMB)
541 /**
542  * boot_ramdisk_high - relocate init ramdisk
543  * @lmb: pointer to lmb handle, will be used for memory mgmt
544  * @rd_data: ramdisk data start address
545  * @rd_len: ramdisk data length
546  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
547  *      start address (after possible relocation)
548  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
549  *      end address (after possible relocation)
550  *
551  * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
552  * variable and if requested ramdisk data is moved to a specified location.
553  *
554  * Initrd_start and initrd_end are set to final (after relocation) ramdisk
555  * start/end addresses if ramdisk image start and len were provided,
556  * otherwise set initrd_start and initrd_end set to zeros.
557  *
558  * returns:
559  *      0 - success
560  *     -1 - failure
561  */
562 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
563                       ulong *initrd_start, ulong *initrd_end)
564 {
565         char    *s;
566         ulong   initrd_high;
567         int     initrd_copy_to_ram = 1;
568
569         s = env_get("initrd_high");
570         if (s) {
571                 /* a value of "no" or a similar string will act like 0,
572                  * turning the "load high" feature off. This is intentional.
573                  */
574                 initrd_high = hextoul(s, NULL);
575                 if (initrd_high == ~0)
576                         initrd_copy_to_ram = 0;
577         } else {
578                 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
579         }
580
581         debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
582               initrd_high, initrd_copy_to_ram);
583
584         if (rd_data) {
585                 if (!initrd_copy_to_ram) {      /* zero-copy ramdisk support */
586                         debug("   in-place initrd\n");
587                         *initrd_start = rd_data;
588                         *initrd_end = rd_data + rd_len;
589                         lmb_reserve(lmb, rd_data, rd_len);
590                 } else {
591                         if (initrd_high)
592                                 *initrd_start = (ulong)lmb_alloc_base(lmb,
593                                                 rd_len, 0x1000, initrd_high);
594                         else
595                                 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
596                                                                  0x1000);
597
598                         if (*initrd_start == 0) {
599                                 puts("ramdisk - allocation error\n");
600                                 goto error;
601                         }
602                         bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
603
604                         *initrd_end = *initrd_start + rd_len;
605                         printf("   Loading Ramdisk to %08lx, end %08lx ... ",
606                                *initrd_start, *initrd_end);
607
608                         memmove_wd((void *)*initrd_start,
609                                    (void *)rd_data, rd_len, CHUNKSZ);
610
611                         /*
612                          * Ensure the image is flushed to memory to handle
613                          * AMP boot scenarios in which we might not be
614                          * HW cache coherent
615                          */
616                         if (IS_ENABLED(CONFIG_MP)) {
617                                 flush_cache((unsigned long)*initrd_start,
618                                             ALIGN(rd_len, ARCH_DMA_MINALIGN));
619                         }
620                         puts("OK\n");
621                 }
622         } else {
623                 *initrd_start = 0;
624                 *initrd_end = 0;
625         }
626         debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
627               *initrd_start, *initrd_end);
628
629         return 0;
630
631 error:
632         return -1;
633 }
634 #endif
635
636 int boot_get_setup(bootm_headers_t *images, u8 arch,
637                    ulong *setup_start, ulong *setup_len)
638 {
639         if (!CONFIG_IS_ENABLED(FIT))
640                 return -ENOENT;
641
642         return boot_get_setup_fit(images, arch, setup_start, setup_len);
643 }
644
645 int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
646                   u8 arch, const ulong *ld_start, ulong * const ld_len)
647 {
648         ulong tmp_img_addr, img_data, img_len;
649         void *buf;
650         int conf_noffset;
651         int fit_img_result;
652         const char *uname, *name;
653         int err;
654         int devnum = 0; /* TODO support multi fpga platforms */
655
656         if (!IS_ENABLED(CONFIG_FPGA))
657                 return -ENOSYS;
658
659         /* Check to see if the images struct has a FIT configuration */
660         if (!genimg_has_config(images)) {
661                 debug("## FIT configuration was not specified\n");
662                 return 0;
663         }
664
665         /*
666          * Obtain the os FIT header from the images struct
667          */
668         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
669         buf = map_sysmem(tmp_img_addr, 0);
670         /*
671          * Check image type. For FIT images get FIT node
672          * and attempt to locate a generic binary.
673          */
674         switch (genimg_get_format(buf)) {
675         case IMAGE_FORMAT_FIT:
676                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
677
678                 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
679                                            NULL);
680                 if (!uname) {
681                         debug("## FPGA image is not specified\n");
682                         return 0;
683                 }
684                 fit_img_result = fit_image_load(images,
685                                                 tmp_img_addr,
686                                                 (const char **)&uname,
687                                                 &images->fit_uname_cfg,
688                                                 arch,
689                                                 IH_TYPE_FPGA,
690                                                 BOOTSTAGE_ID_FPGA_INIT,
691                                                 FIT_LOAD_OPTIONAL_NON_ZERO,
692                                                 &img_data, &img_len);
693
694                 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
695                       uname, img_data, img_len);
696
697                 if (fit_img_result < 0) {
698                         /* Something went wrong! */
699                         return fit_img_result;
700                 }
701
702                 if (!fpga_is_partial_data(devnum, img_len)) {
703                         name = "full";
704                         err = fpga_loadbitstream(devnum, (char *)img_data,
705                                                  img_len, BIT_FULL);
706                         if (err)
707                                 err = fpga_load(devnum, (const void *)img_data,
708                                                 img_len, BIT_FULL);
709                 } else {
710                         name = "partial";
711                         err = fpga_loadbitstream(devnum, (char *)img_data,
712                                                  img_len, BIT_PARTIAL);
713                         if (err)
714                                 err = fpga_load(devnum, (const void *)img_data,
715                                                 img_len, BIT_PARTIAL);
716                 }
717
718                 if (err)
719                         return err;
720
721                 printf("   Programming %s bitstream... OK\n", name);
722                 break;
723         default:
724                 printf("The given image format is not supported (corrupt?)\n");
725                 return 1;
726         }
727
728         return 0;
729 }
730
731 static void fit_loadable_process(u8 img_type,
732                                  ulong img_data,
733                                  ulong img_len)
734 {
735         int i;
736         const unsigned int count =
737                         ll_entry_count(struct fit_loadable_tbl, fit_loadable);
738         struct fit_loadable_tbl *fit_loadable_handler =
739                         ll_entry_start(struct fit_loadable_tbl, fit_loadable);
740         /* For each loadable handler */
741         for (i = 0; i < count; i++, fit_loadable_handler++)
742                 /* matching this type */
743                 if (fit_loadable_handler->type == img_type)
744                         /* call that handler with this image data */
745                         fit_loadable_handler->handler(img_data, img_len);
746 }
747
748 int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
749                       u8 arch, const ulong *ld_start, ulong * const ld_len)
750 {
751         /*
752          * These variables are used to hold the current image location
753          * in system memory.
754          */
755         ulong tmp_img_addr;
756         /*
757          * These two variables are requirements for fit_image_load, but
758          * their values are not used
759          */
760         ulong img_data, img_len;
761         void *buf;
762         int loadables_index;
763         int conf_noffset;
764         int fit_img_result;
765         const char *uname;
766         u8 img_type;
767
768         /* Check to see if the images struct has a FIT configuration */
769         if (!genimg_has_config(images)) {
770                 debug("## FIT configuration was not specified\n");
771                 return 0;
772         }
773
774         /*
775          * Obtain the os FIT header from the images struct
776          */
777         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
778         buf = map_sysmem(tmp_img_addr, 0);
779         /*
780          * Check image type. For FIT images get FIT node
781          * and attempt to locate a generic binary.
782          */
783         switch (genimg_get_format(buf)) {
784         case IMAGE_FORMAT_FIT:
785                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
786
787                 for (loadables_index = 0;
788                      uname = fdt_stringlist_get(buf, conf_noffset,
789                                                 FIT_LOADABLE_PROP,
790                                                 loadables_index, NULL), uname;
791                      loadables_index++) {
792                         fit_img_result = fit_image_load(images, tmp_img_addr,
793                                                         &uname,
794                                                         &images->fit_uname_cfg,
795                                                         arch, IH_TYPE_LOADABLE,
796                                                         BOOTSTAGE_ID_FIT_LOADABLE_START,
797                                                         FIT_LOAD_OPTIONAL_NON_ZERO,
798                                                         &img_data, &img_len);
799                         if (fit_img_result < 0) {
800                                 /* Something went wrong! */
801                                 return fit_img_result;
802                         }
803
804                         fit_img_result = fit_image_get_node(buf, uname);
805                         if (fit_img_result < 0) {
806                                 /* Something went wrong! */
807                                 return fit_img_result;
808                         }
809                         fit_img_result = fit_image_get_type(buf,
810                                                             fit_img_result,
811                                                             &img_type);
812                         if (fit_img_result < 0) {
813                                 /* Something went wrong! */
814                                 return fit_img_result;
815                         }
816
817                         fit_loadable_process(img_type, img_data, img_len);
818                 }
819                 break;
820         default:
821                 printf("The given image format is not supported (corrupt?)\n");
822                 return 1;
823         }
824
825         return 0;
826 }
827
828 #if defined(CONFIG_LMB)
829 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
830 /**
831  * boot_get_cmdline - allocate and initialize kernel cmdline
832  * @lmb: pointer to lmb handle, will be used for memory mgmt
833  * @cmd_start: pointer to a ulong variable, will hold cmdline start
834  * @cmd_end: pointer to a ulong variable, will hold cmdline end
835  *
836  * boot_get_cmdline() allocates space for kernel command line below
837  * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
838  * variable is present its contents is copied to allocated kernel
839  * command line.
840  *
841  * returns:
842  *      0 - success
843  *     -1 - failure
844  */
845 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
846 {
847         char *cmdline;
848         char *s;
849
850         cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
851                                 env_get_bootm_mapsize() + env_get_bootm_low());
852         if (!cmdline)
853                 return -1;
854
855         s = env_get("bootargs");
856         if (!s)
857                 s = "";
858
859         strcpy(cmdline, s);
860
861         *cmd_start = (ulong)cmdline;
862         *cmd_end = *cmd_start + strlen(cmdline);
863
864         debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
865
866         return 0;
867 }
868
869 /**
870  * boot_get_kbd - allocate and initialize kernel copy of board info
871  * @lmb: pointer to lmb handle, will be used for memory mgmt
872  * @kbd: double pointer to board info data
873  *
874  * boot_get_kbd() allocates space for kernel copy of board info data below
875  * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
876  * with the current u-boot board info data.
877  *
878  * returns:
879  *      0 - success
880  *     -1 - failure
881  */
882 int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
883 {
884         *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
885                                                        sizeof(struct bd_info),
886                                                        0xf,
887                                                        env_get_bootm_mapsize() +
888                                                        env_get_bootm_low());
889         if (!*kbd)
890                 return -1;
891
892         **kbd = *gd->bd;
893
894         debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
895
896 #if defined(DEBUG)
897         if (IS_ENABLED(CONFIG_CMD_BDI))
898                 do_bdinfo(NULL, 0, 0, NULL);
899 #endif
900
901         return 0;
902 }
903 #endif
904
905 int image_setup_linux(bootm_headers_t *images)
906 {
907         ulong of_size = images->ft_len;
908         char **of_flat_tree = &images->ft_addr;
909         struct lmb *lmb = &images->lmb;
910         int ret;
911
912         if (CONFIG_IS_ENABLED(OF_LIBFDT))
913                 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
914
915         if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
916                 ret = boot_get_cmdline(lmb, &images->cmdline_start,
917                                        &images->cmdline_end);
918                 if (ret) {
919                         puts("ERROR with allocation of cmdline\n");
920                         return ret;
921                 }
922         }
923
924         if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
925                 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
926                 if (ret)
927                         return ret;
928         }
929
930         if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
931                 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
932                 if (ret)
933                         return ret;
934         }
935
936         return 0;
937 }
938 #endif
939
940 void genimg_print_size(uint32_t size)
941 {
942         printf("%d Bytes = ", size);
943         print_size(size, "\n");
944 }
945
946 void genimg_print_time(time_t timestamp)
947 {
948         struct rtc_time tm;
949
950         rtc_to_tm(timestamp, &tm);
951         printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
952                tm.tm_year, tm.tm_mon, tm.tm_mday,
953                tm.tm_hour, tm.tm_min, tm.tm_sec);
954 }