image: Drop another #ifdef for FIT
[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 <mapmem.h>
20 #include <rtc.h>
21 #include <watchdog.h>
22 #include <asm/cache.h>
23 #include <asm/global_data.h>
24
25 DECLARE_GLOBAL_DATA_PTR;
26
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
86 /*****************************************************************************/
87 /* Shared dual-format routines */
88 /*****************************************************************************/
89 ulong image_load_addr = CONFIG_SYS_LOAD_ADDR;   /* Default Load Address */
90 ulong image_save_addr;                  /* Default Save Address */
91 ulong image_save_size;                  /* Default Save Size (in bytes) */
92
93 static int on_loadaddr(const char *name, const char *value, enum env_op op,
94                        int flags)
95 {
96         switch (op) {
97         case env_op_create:
98         case env_op_overwrite:
99                 image_load_addr = hextoul(value, NULL);
100                 break;
101         default:
102                 break;
103         }
104
105         return 0;
106 }
107 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
108
109 ulong env_get_bootm_low(void)
110 {
111         char *s = env_get("bootm_low");
112
113         if (s) {
114                 ulong tmp = hextoul(s, NULL);
115                 return tmp;
116         }
117
118 #if defined(CONFIG_SYS_SDRAM_BASE)
119         return CONFIG_SYS_SDRAM_BASE;
120 #elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
121         return gd->bd->bi_dram[0].start;
122 #else
123         return 0;
124 #endif
125 }
126
127 phys_size_t env_get_bootm_size(void)
128 {
129         phys_size_t tmp, size;
130         phys_addr_t start;
131         char *s = env_get("bootm_size");
132
133         if (s) {
134                 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
135                 return tmp;
136         }
137
138         start = gd->ram_base;
139         size = gd->ram_size;
140
141         if (start + size > gd->ram_top)
142                 size = gd->ram_top - start;
143
144         s = env_get("bootm_low");
145         if (s)
146                 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
147         else
148                 tmp = start;
149
150         return size - (tmp - start);
151 }
152
153 phys_size_t env_get_bootm_mapsize(void)
154 {
155         phys_size_t tmp;
156         char *s = env_get("bootm_mapsize");
157
158         if (s) {
159                 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
160                 return tmp;
161         }
162
163 #if defined(CONFIG_SYS_BOOTMAPSZ)
164         return CONFIG_SYS_BOOTMAPSZ;
165 #else
166         return env_get_bootm_size();
167 #endif
168 }
169
170 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
171 {
172         if (to == from)
173                 return;
174
175 #if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
176         if (to > from) {
177                 from += len;
178                 to += len;
179         }
180         while (len > 0) {
181                 size_t tail = (len > chunksz) ? chunksz : len;
182
183                 WATCHDOG_RESET();
184                 if (to > from) {
185                         to -= tail;
186                         from -= tail;
187                 }
188                 memmove(to, from, tail);
189                 if (to < from) {
190                         to += tail;
191                         from += tail;
192                 }
193                 len -= tail;
194         }
195 #else   /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
196         memmove(to, from, len);
197 #endif  /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
198 }
199
200 /**
201  * genimg_get_kernel_addr_fit - get the real kernel address and return 2
202  *                              FIT strings
203  * @img_addr: a string might contain real image address
204  * @fit_uname_config: double pointer to a char, will hold pointer to a
205  *                    configuration unit name
206  * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
207  *                    name
208  *
209  * genimg_get_kernel_addr_fit get the real kernel start address from a string
210  * which is normally the first argv of bootm/bootz
211  *
212  * returns:
213  *     kernel start address
214  */
215 ulong genimg_get_kernel_addr_fit(char * const img_addr,
216                                  const char **fit_uname_config,
217                                  const char **fit_uname_kernel)
218 {
219         ulong kernel_addr;
220
221         /* find out kernel image address */
222         if (!img_addr) {
223                 kernel_addr = image_load_addr;
224                 debug("*  kernel: default image load address = 0x%08lx\n",
225                       image_load_addr);
226         } else if (CONFIG_IS_ENABLED(FIT) &&
227                    fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
228                                   fit_uname_config)) {
229                 debug("*  kernel: config '%s' from image at 0x%08lx\n",
230                       *fit_uname_config, kernel_addr);
231         } else if (CONFIG_IS_ENABLED(FIT) &&
232                    fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
233                                       fit_uname_kernel)) {
234                 debug("*  kernel: subimage '%s' from image at 0x%08lx\n",
235                       *fit_uname_kernel, kernel_addr);
236         } else {
237                 kernel_addr = hextoul(img_addr, NULL);
238                 debug("*  kernel: cmdline image address = 0x%08lx\n",
239                       kernel_addr);
240         }
241
242         return kernel_addr;
243 }
244
245 /**
246  * genimg_get_kernel_addr() is the simple version of
247  * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
248  */
249 ulong genimg_get_kernel_addr(char * const img_addr)
250 {
251         const char *fit_uname_config = NULL;
252         const char *fit_uname_kernel = NULL;
253
254         return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
255                                           &fit_uname_kernel);
256 }
257
258 /**
259  * genimg_get_format - get image format type
260  * @img_addr: image start address
261  *
262  * genimg_get_format() checks whether provided address points to a valid
263  * legacy or FIT image.
264  *
265  * New uImage format and FDT blob are based on a libfdt. FDT blob
266  * may be passed directly or embedded in a FIT image. In both situations
267  * genimg_get_format() must be able to dectect libfdt header.
268  *
269  * returns:
270  *     image format type or IMAGE_FORMAT_INVALID if no image is present
271  */
272 int genimg_get_format(const void *img_addr)
273 {
274         if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
275                 const image_header_t *hdr;
276
277                 hdr = (const image_header_t *)img_addr;
278                 if (image_check_magic(hdr))
279                         return IMAGE_FORMAT_LEGACY;
280         }
281         if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
282                 if (!fdt_check_header(img_addr))
283                         return IMAGE_FORMAT_FIT;
284         }
285         if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
286             !android_image_check_header(img_addr))
287                 return IMAGE_FORMAT_ANDROID;
288
289         return IMAGE_FORMAT_INVALID;
290 }
291
292 /**
293  * fit_has_config - check if there is a valid FIT configuration
294  * @images: pointer to the bootm command headers structure
295  *
296  * fit_has_config() checks if there is a FIT configuration in use
297  * (if FTI support is present).
298  *
299  * returns:
300  *     0, no FIT support or no configuration found
301  *     1, configuration found
302  */
303 int genimg_has_config(bootm_headers_t *images)
304 {
305         if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
306                 return 1;
307
308         return 0;
309 }
310
311 /**
312  * select_ramdisk() - Select and locate the ramdisk to use
313  *
314  * @images: pointer to the bootm images structure
315  * @select: name of ramdisk to select, or hex address, NULL for any
316  * @arch: expected ramdisk architecture
317  * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
318  * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
319  * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
320  *      other -ve value on other error
321  */
322 static int select_ramdisk(bootm_headers_t *images, const char *select, u8 arch,
323                           ulong *rd_datap, ulong *rd_lenp)
324 {
325         const char *fit_uname_config;
326         const char *fit_uname_ramdisk;
327         bool done = false;
328         int rd_noffset;
329         ulong rd_addr;
330         char *buf;
331
332 #if CONFIG_IS_ENABLED(FIT)
333                 fit_uname_config = images->fit_uname_cfg;
334                 fit_uname_ramdisk = NULL;
335
336                 if (select) {
337                         ulong default_addr;
338                         /*
339                          * If the init ramdisk comes from the FIT image and
340                          * the FIT image address is omitted in the command
341                          * line argument, try to use os FIT image address or
342                          * default load address.
343                          */
344                         if (images->fit_uname_os)
345                                 default_addr = (ulong)images->fit_hdr_os;
346                         else
347                                 default_addr = image_load_addr;
348
349                         if (fit_parse_conf(select, default_addr,
350                                            &rd_addr, &fit_uname_config)) {
351                                 debug("*  ramdisk: config '%s' from image at 0x%08lx\n",
352                                       fit_uname_config, rd_addr);
353                         } else if (fit_parse_subimage(select, default_addr,
354                                                       &rd_addr,
355                                                       &fit_uname_ramdisk)) {
356                                 debug("*  ramdisk: subimage '%s' from image at 0x%08lx\n",
357                                       fit_uname_ramdisk, rd_addr);
358                         } else
359 #endif
360                         {
361                                 rd_addr = hextoul(select, NULL);
362                                 debug("*  ramdisk: cmdline image address = 0x%08lx\n",
363                                       rd_addr);
364                         }
365 #if CONFIG_IS_ENABLED(FIT)
366                 }
367 #endif
368                 if (CONFIG_IS_ENABLED(FIT) && !select) {
369                         /* use FIT configuration provided in first bootm
370                          * command argument. If the property is not defined,
371                          * quit silently (with -ENOPKG)
372                          */
373                         rd_addr = map_to_sysmem(images->fit_hdr_os);
374                         rd_noffset = fit_get_node_from_config(images,
375                                                               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 image_header_t *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, &fit_uname_config,
413                                         arch, IH_TYPE_RAMDISK,
414                                         BOOTSTAGE_ID_FIT_RD_START,
415                                         FIT_LOAD_OPTIONAL_NON_ZERO,
416                                         rd_datap, rd_lenp);
417                                 if (rd_noffset < 0)
418                                         return rd_noffset;
419
420                                 images->fit_hdr_rd = map_sysmem(rd_addr, 0);
421                                 images->fit_uname_rd = fit_uname_ramdisk;
422                                 images->fit_noffset_rd = rd_noffset;
423                                 done = true;
424                         }
425                         break;
426                 case IMAGE_FORMAT_ANDROID:
427                         if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
428                                 void *ptr = map_sysmem(images->os.start, 0);
429                                 int ret;
430
431                                 ret = android_image_get_ramdisk(ptr, rd_datap,
432                                                                 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[], bootm_headers_t *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 #if defined(CONFIG_LMB)
556 /**
557  * boot_ramdisk_high - relocate init ramdisk
558  * @lmb: pointer to lmb handle, will be used for memory mgmt
559  * @rd_data: ramdisk data start address
560  * @rd_len: ramdisk data length
561  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
562  *      start address (after possible relocation)
563  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
564  *      end address (after possible relocation)
565  *
566  * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
567  * variable and if requested ramdisk data is moved to a specified location.
568  *
569  * Initrd_start and initrd_end are set to final (after relocation) ramdisk
570  * start/end addresses if ramdisk image start and len were provided,
571  * otherwise set initrd_start and initrd_end set to zeros.
572  *
573  * returns:
574  *      0 - success
575  *     -1 - failure
576  */
577 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
578                       ulong *initrd_start, ulong *initrd_end)
579 {
580         char    *s;
581         ulong   initrd_high;
582         int     initrd_copy_to_ram = 1;
583
584         s = env_get("initrd_high");
585         if (s) {
586                 /* a value of "no" or a similar string will act like 0,
587                  * turning the "load high" feature off. This is intentional.
588                  */
589                 initrd_high = hextoul(s, NULL);
590                 if (initrd_high == ~0)
591                         initrd_copy_to_ram = 0;
592         } else {
593                 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
594         }
595
596         debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
597               initrd_high, initrd_copy_to_ram);
598
599         if (rd_data) {
600                 if (!initrd_copy_to_ram) {      /* zero-copy ramdisk support */
601                         debug("   in-place initrd\n");
602                         *initrd_start = rd_data;
603                         *initrd_end = rd_data + rd_len;
604                         lmb_reserve(lmb, rd_data, rd_len);
605                 } else {
606                         if (initrd_high)
607                                 *initrd_start = (ulong)lmb_alloc_base(lmb,
608                                                 rd_len, 0x1000, initrd_high);
609                         else
610                                 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
611                                                                  0x1000);
612
613                         if (*initrd_start == 0) {
614                                 puts("ramdisk - allocation error\n");
615                                 goto error;
616                         }
617                         bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
618
619                         *initrd_end = *initrd_start + rd_len;
620                         printf("   Loading Ramdisk to %08lx, end %08lx ... ",
621                                *initrd_start, *initrd_end);
622
623                         memmove_wd((void *)*initrd_start,
624                                    (void *)rd_data, rd_len, CHUNKSZ);
625
626                         /*
627                          * Ensure the image is flushed to memory to handle
628                          * AMP boot scenarios in which we might not be
629                          * HW cache coherent
630                          */
631                         if (IS_ENABLED(CONFIG_MP)) {
632                                 flush_cache((unsigned long)*initrd_start,
633                                             ALIGN(rd_len, ARCH_DMA_MINALIGN));
634                         }
635                         puts("OK\n");
636                 }
637         } else {
638                 *initrd_start = 0;
639                 *initrd_end = 0;
640         }
641         debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
642               *initrd_start, *initrd_end);
643
644         return 0;
645
646 error:
647         return -1;
648 }
649 #endif
650
651 int boot_get_setup(bootm_headers_t *images, u8 arch,
652                    ulong *setup_start, ulong *setup_len)
653 {
654         if (!CONFIG_IS_ENABLED(FIT))
655                 return -ENOENT;
656
657         return boot_get_setup_fit(images, arch, setup_start, setup_len);
658 }
659
660 int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
661                   u8 arch, const ulong *ld_start, ulong * const ld_len)
662 {
663         ulong tmp_img_addr, img_data, img_len;
664         void *buf;
665         int conf_noffset;
666         int fit_img_result;
667         const char *uname, *name;
668         int err;
669         int devnum = 0; /* TODO support multi fpga platforms */
670
671         if (!IS_ENABLED(CONFIG_FPGA))
672                 return -ENOSYS;
673
674         /* Check to see if the images struct has a FIT configuration */
675         if (!genimg_has_config(images)) {
676                 debug("## FIT configuration was not specified\n");
677                 return 0;
678         }
679
680         /*
681          * Obtain the os FIT header from the images struct
682          */
683         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
684         buf = map_sysmem(tmp_img_addr, 0);
685         /*
686          * Check image type. For FIT images get FIT node
687          * and attempt to locate a generic binary.
688          */
689         switch (genimg_get_format(buf)) {
690         case IMAGE_FORMAT_FIT:
691                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
692
693                 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
694                                            NULL);
695                 if (!uname) {
696                         debug("## FPGA image is not specified\n");
697                         return 0;
698                 }
699                 fit_img_result = fit_image_load(images,
700                                                 tmp_img_addr,
701                                                 (const char **)&uname,
702                                                 &images->fit_uname_cfg,
703                                                 arch,
704                                                 IH_TYPE_FPGA,
705                                                 BOOTSTAGE_ID_FPGA_INIT,
706                                                 FIT_LOAD_OPTIONAL_NON_ZERO,
707                                                 &img_data, &img_len);
708
709                 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
710                       uname, img_data, img_len);
711
712                 if (fit_img_result < 0) {
713                         /* Something went wrong! */
714                         return fit_img_result;
715                 }
716
717                 if (!fpga_is_partial_data(devnum, img_len)) {
718                         name = "full";
719                         err = fpga_loadbitstream(devnum, (char *)img_data,
720                                                  img_len, BIT_FULL);
721                         if (err)
722                                 err = fpga_load(devnum, (const void *)img_data,
723                                                 img_len, BIT_FULL, 0);
724                 } else {
725                         name = "partial";
726                         err = fpga_loadbitstream(devnum, (char *)img_data,
727                                                  img_len, BIT_PARTIAL);
728                         if (err)
729                                 err = fpga_load(devnum, (const void *)img_data,
730                                                 img_len, BIT_PARTIAL, 0);
731                 }
732
733                 if (err)
734                         return err;
735
736                 printf("   Programming %s bitstream... OK\n", name);
737                 break;
738         default:
739                 printf("The given image format is not supported (corrupt?)\n");
740                 return 1;
741         }
742
743         return 0;
744 }
745
746 static void fit_loadable_process(u8 img_type,
747                                  ulong img_data,
748                                  ulong img_len)
749 {
750         int i;
751         const unsigned int count =
752                         ll_entry_count(struct fit_loadable_tbl, fit_loadable);
753         struct fit_loadable_tbl *fit_loadable_handler =
754                         ll_entry_start(struct fit_loadable_tbl, fit_loadable);
755         /* For each loadable handler */
756         for (i = 0; i < count; i++, fit_loadable_handler++)
757                 /* matching this type */
758                 if (fit_loadable_handler->type == img_type)
759                         /* call that handler with this image data */
760                         fit_loadable_handler->handler(img_data, img_len);
761 }
762
763 int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
764                       u8 arch, const ulong *ld_start, ulong * const ld_len)
765 {
766         /*
767          * These variables are used to hold the current image location
768          * in system memory.
769          */
770         ulong tmp_img_addr;
771         /*
772          * These two variables are requirements for fit_image_load, but
773          * their values are not used
774          */
775         ulong img_data, img_len;
776         void *buf;
777         int loadables_index;
778         int conf_noffset;
779         int fit_img_result;
780         const char *uname;
781         u8 img_type;
782
783         /* Check to see if the images struct has a FIT configuration */
784         if (!genimg_has_config(images)) {
785                 debug("## FIT configuration was not specified\n");
786                 return 0;
787         }
788
789         /*
790          * Obtain the os FIT header from the images struct
791          */
792         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
793         buf = map_sysmem(tmp_img_addr, 0);
794         /*
795          * Check image type. For FIT images get FIT node
796          * and attempt to locate a generic binary.
797          */
798         switch (genimg_get_format(buf)) {
799         case IMAGE_FORMAT_FIT:
800                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
801
802                 for (loadables_index = 0;
803                      uname = fdt_stringlist_get(buf, conf_noffset,
804                                                 FIT_LOADABLE_PROP,
805                                                 loadables_index, NULL), uname;
806                      loadables_index++) {
807                         fit_img_result = fit_image_load(images, tmp_img_addr,
808                                                         &uname,
809                                                         &images->fit_uname_cfg,
810                                                         arch, IH_TYPE_LOADABLE,
811                                                         BOOTSTAGE_ID_FIT_LOADABLE_START,
812                                                         FIT_LOAD_OPTIONAL_NON_ZERO,
813                                                         &img_data, &img_len);
814                         if (fit_img_result < 0) {
815                                 /* Something went wrong! */
816                                 return fit_img_result;
817                         }
818
819                         fit_img_result = fit_image_get_node(buf, uname);
820                         if (fit_img_result < 0) {
821                                 /* Something went wrong! */
822                                 return fit_img_result;
823                         }
824                         fit_img_result = fit_image_get_type(buf,
825                                                             fit_img_result,
826                                                             &img_type);
827                         if (fit_img_result < 0) {
828                                 /* Something went wrong! */
829                                 return fit_img_result;
830                         }
831
832                         fit_loadable_process(img_type, img_data, img_len);
833                 }
834                 break;
835         default:
836                 printf("The given image format is not supported (corrupt?)\n");
837                 return 1;
838         }
839
840         return 0;
841 }
842
843 #if defined(CONFIG_LMB)
844 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
845 /**
846  * boot_get_cmdline - allocate and initialize kernel cmdline
847  * @lmb: pointer to lmb handle, will be used for memory mgmt
848  * @cmd_start: pointer to a ulong variable, will hold cmdline start
849  * @cmd_end: pointer to a ulong variable, will hold cmdline end
850  *
851  * boot_get_cmdline() allocates space for kernel command line below
852  * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
853  * variable is present its contents is copied to allocated kernel
854  * command line.
855  *
856  * returns:
857  *      0 - success
858  *     -1 - failure
859  */
860 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
861 {
862         char *cmdline;
863         char *s;
864
865         cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
866                                 env_get_bootm_mapsize() + env_get_bootm_low());
867         if (!cmdline)
868                 return -1;
869
870         s = env_get("bootargs");
871         if (!s)
872                 s = "";
873
874         strcpy(cmdline, s);
875
876         *cmd_start = (ulong)cmdline;
877         *cmd_end = *cmd_start + strlen(cmdline);
878
879         debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
880
881         return 0;
882 }
883
884 /**
885  * boot_get_kbd - allocate and initialize kernel copy of board info
886  * @lmb: pointer to lmb handle, will be used for memory mgmt
887  * @kbd: double pointer to board info data
888  *
889  * boot_get_kbd() allocates space for kernel copy of board info data below
890  * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
891  * with the current u-boot board info data.
892  *
893  * returns:
894  *      0 - success
895  *     -1 - failure
896  */
897 int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
898 {
899         *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
900                                                        sizeof(struct bd_info),
901                                                        0xf,
902                                                        env_get_bootm_mapsize() +
903                                                        env_get_bootm_low());
904         if (!*kbd)
905                 return -1;
906
907         **kbd = *gd->bd;
908
909         debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
910
911 #if defined(DEBUG)
912         if (IS_ENABLED(CONFIG_CMD_BDI))
913                 do_bdinfo(NULL, 0, 0, NULL);
914 #endif
915
916         return 0;
917 }
918 #endif
919
920 int image_setup_linux(bootm_headers_t *images)
921 {
922         ulong of_size = images->ft_len;
923         char **of_flat_tree = &images->ft_addr;
924         struct lmb *lmb = &images->lmb;
925         int ret;
926
927         if (CONFIG_IS_ENABLED(OF_LIBFDT))
928                 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
929
930         if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
931                 ret = boot_get_cmdline(lmb, &images->cmdline_start,
932                                        &images->cmdline_end);
933                 if (ret) {
934                         puts("ERROR with allocation of cmdline\n");
935                         return ret;
936                 }
937         }
938
939         if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
940                 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
941                 if (ret)
942                         return ret;
943         }
944
945         if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
946                 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
947                 if (ret)
948                         return ret;
949         }
950
951         return 0;
952 }
953 #endif
954
955 void genimg_print_size(uint32_t size)
956 {
957         printf("%d Bytes = ", size);
958         print_size(size, "\n");
959 }
960
961 void genimg_print_time(time_t timestamp)
962 {
963         struct rtc_time tm;
964
965         rtc_to_tm(timestamp, &tm);
966         printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
967                tm.tm_year, tm.tm_mon, tm.tm_mday,
968                tm.tm_hour, tm.tm_min, tm.tm_sec);
969 }