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