Merge https://source.denx.de/u-boot/custodians/u-boot-usb
[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 /**
541  * boot_ramdisk_high - relocate init ramdisk
542  * @lmb: pointer to lmb handle, will be used for memory mgmt
543  * @rd_data: ramdisk data start address
544  * @rd_len: ramdisk data length
545  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
546  *      start address (after possible relocation)
547  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
548  *      end address (after possible relocation)
549  *
550  * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
551  * variable and if requested ramdisk data is moved to a specified location.
552  *
553  * Initrd_start and initrd_end are set to final (after relocation) ramdisk
554  * start/end addresses if ramdisk image start and len were provided,
555  * otherwise set initrd_start and initrd_end set to zeros.
556  *
557  * returns:
558  *      0 - success
559  *     -1 - failure
560  */
561 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
562                       ulong *initrd_start, ulong *initrd_end)
563 {
564         char    *s;
565         ulong   initrd_high;
566         int     initrd_copy_to_ram = 1;
567
568         s = env_get("initrd_high");
569         if (s) {
570                 /* a value of "no" or a similar string will act like 0,
571                  * turning the "load high" feature off. This is intentional.
572                  */
573                 initrd_high = hextoul(s, NULL);
574                 if (initrd_high == ~0)
575                         initrd_copy_to_ram = 0;
576         } else {
577                 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
578         }
579
580         debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
581               initrd_high, initrd_copy_to_ram);
582
583         if (rd_data) {
584                 if (!initrd_copy_to_ram) {      /* zero-copy ramdisk support */
585                         debug("   in-place initrd\n");
586                         *initrd_start = rd_data;
587                         *initrd_end = rd_data + rd_len;
588                         lmb_reserve(lmb, rd_data, rd_len);
589                 } else {
590                         if (initrd_high)
591                                 *initrd_start = (ulong)lmb_alloc_base(lmb,
592                                                 rd_len, 0x1000, initrd_high);
593                         else
594                                 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
595                                                                  0x1000);
596
597                         if (*initrd_start == 0) {
598                                 puts("ramdisk - allocation error\n");
599                                 goto error;
600                         }
601                         bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
602
603                         *initrd_end = *initrd_start + rd_len;
604                         printf("   Loading Ramdisk to %08lx, end %08lx ... ",
605                                *initrd_start, *initrd_end);
606
607                         memmove_wd((void *)*initrd_start,
608                                    (void *)rd_data, rd_len, CHUNKSZ);
609
610                         /*
611                          * Ensure the image is flushed to memory to handle
612                          * AMP boot scenarios in which we might not be
613                          * HW cache coherent
614                          */
615                         if (IS_ENABLED(CONFIG_MP)) {
616                                 flush_cache((unsigned long)*initrd_start,
617                                             ALIGN(rd_len, ARCH_DMA_MINALIGN));
618                         }
619                         puts("OK\n");
620                 }
621         } else {
622                 *initrd_start = 0;
623                 *initrd_end = 0;
624         }
625         debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
626               *initrd_start, *initrd_end);
627
628         return 0;
629
630 error:
631         return -1;
632 }
633
634 int boot_get_setup(bootm_headers_t *images, u8 arch,
635                    ulong *setup_start, ulong *setup_len)
636 {
637         if (!CONFIG_IS_ENABLED(FIT))
638                 return -ENOENT;
639
640         return boot_get_setup_fit(images, arch, setup_start, setup_len);
641 }
642
643 int boot_get_fpga(int argc, char *const argv[], bootm_headers_t *images,
644                   u8 arch, const ulong *ld_start, ulong * const ld_len)
645 {
646         ulong tmp_img_addr, img_data, img_len;
647         void *buf;
648         int conf_noffset;
649         int fit_img_result;
650         const char *uname, *name;
651         int err;
652         int devnum = 0; /* TODO support multi fpga platforms */
653
654         if (!IS_ENABLED(CONFIG_FPGA))
655                 return -ENOSYS;
656
657         /* Check to see if the images struct has a FIT configuration */
658         if (!genimg_has_config(images)) {
659                 debug("## FIT configuration was not specified\n");
660                 return 0;
661         }
662
663         /*
664          * Obtain the os FIT header from the images struct
665          */
666         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
667         buf = map_sysmem(tmp_img_addr, 0);
668         /*
669          * Check image type. For FIT images get FIT node
670          * and attempt to locate a generic binary.
671          */
672         switch (genimg_get_format(buf)) {
673         case IMAGE_FORMAT_FIT:
674                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
675
676                 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
677                                            NULL);
678                 if (!uname) {
679                         debug("## FPGA image is not specified\n");
680                         return 0;
681                 }
682                 fit_img_result = fit_image_load(images,
683                                                 tmp_img_addr,
684                                                 (const char **)&uname,
685                                                 &images->fit_uname_cfg,
686                                                 arch,
687                                                 IH_TYPE_FPGA,
688                                                 BOOTSTAGE_ID_FPGA_INIT,
689                                                 FIT_LOAD_OPTIONAL_NON_ZERO,
690                                                 &img_data, &img_len);
691
692                 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
693                       uname, img_data, img_len);
694
695                 if (fit_img_result < 0) {
696                         /* Something went wrong! */
697                         return fit_img_result;
698                 }
699
700                 if (!fpga_is_partial_data(devnum, img_len)) {
701                         name = "full";
702                         err = fpga_loadbitstream(devnum, (char *)img_data,
703                                                  img_len, BIT_FULL);
704                         if (err)
705                                 err = fpga_load(devnum, (const void *)img_data,
706                                                 img_len, BIT_FULL);
707                 } else {
708                         name = "partial";
709                         err = fpga_loadbitstream(devnum, (char *)img_data,
710                                                  img_len, BIT_PARTIAL);
711                         if (err)
712                                 err = fpga_load(devnum, (const void *)img_data,
713                                                 img_len, BIT_PARTIAL);
714                 }
715
716                 if (err)
717                         return err;
718
719                 printf("   Programming %s bitstream... OK\n", name);
720                 break;
721         default:
722                 printf("The given image format is not supported (corrupt?)\n");
723                 return 1;
724         }
725
726         return 0;
727 }
728
729 static void fit_loadable_process(u8 img_type,
730                                  ulong img_data,
731                                  ulong img_len)
732 {
733         int i;
734         const unsigned int count =
735                         ll_entry_count(struct fit_loadable_tbl, fit_loadable);
736         struct fit_loadable_tbl *fit_loadable_handler =
737                         ll_entry_start(struct fit_loadable_tbl, fit_loadable);
738         /* For each loadable handler */
739         for (i = 0; i < count; i++, fit_loadable_handler++)
740                 /* matching this type */
741                 if (fit_loadable_handler->type == img_type)
742                         /* call that handler with this image data */
743                         fit_loadable_handler->handler(img_data, img_len);
744 }
745
746 int boot_get_loadable(int argc, char *const argv[], bootm_headers_t *images,
747                       u8 arch, const ulong *ld_start, ulong * const ld_len)
748 {
749         /*
750          * These variables are used to hold the current image location
751          * in system memory.
752          */
753         ulong tmp_img_addr;
754         /*
755          * These two variables are requirements for fit_image_load, but
756          * their values are not used
757          */
758         ulong img_data, img_len;
759         void *buf;
760         int loadables_index;
761         int conf_noffset;
762         int fit_img_result;
763         const char *uname;
764         u8 img_type;
765
766         /* Check to see if the images struct has a FIT configuration */
767         if (!genimg_has_config(images)) {
768                 debug("## FIT configuration was not specified\n");
769                 return 0;
770         }
771
772         /*
773          * Obtain the os FIT header from the images struct
774          */
775         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
776         buf = map_sysmem(tmp_img_addr, 0);
777         /*
778          * Check image type. For FIT images get FIT node
779          * and attempt to locate a generic binary.
780          */
781         switch (genimg_get_format(buf)) {
782         case IMAGE_FORMAT_FIT:
783                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
784
785                 for (loadables_index = 0;
786                      uname = fdt_stringlist_get(buf, conf_noffset,
787                                                 FIT_LOADABLE_PROP,
788                                                 loadables_index, NULL), uname;
789                      loadables_index++) {
790                         fit_img_result = fit_image_load(images, tmp_img_addr,
791                                                         &uname,
792                                                         &images->fit_uname_cfg,
793                                                         arch, IH_TYPE_LOADABLE,
794                                                         BOOTSTAGE_ID_FIT_LOADABLE_START,
795                                                         FIT_LOAD_OPTIONAL_NON_ZERO,
796                                                         &img_data, &img_len);
797                         if (fit_img_result < 0) {
798                                 /* Something went wrong! */
799                                 return fit_img_result;
800                         }
801
802                         fit_img_result = fit_image_get_node(buf, uname);
803                         if (fit_img_result < 0) {
804                                 /* Something went wrong! */
805                                 return fit_img_result;
806                         }
807                         fit_img_result = fit_image_get_type(buf,
808                                                             fit_img_result,
809                                                             &img_type);
810                         if (fit_img_result < 0) {
811                                 /* Something went wrong! */
812                                 return fit_img_result;
813                         }
814
815                         fit_loadable_process(img_type, img_data, img_len);
816                 }
817                 break;
818         default:
819                 printf("The given image format is not supported (corrupt?)\n");
820                 return 1;
821         }
822
823         return 0;
824 }
825
826 #ifdef CONFIG_SYS_BOOT_GET_CMDLINE
827 /**
828  * boot_get_cmdline - allocate and initialize kernel cmdline
829  * @lmb: pointer to lmb handle, will be used for memory mgmt
830  * @cmd_start: pointer to a ulong variable, will hold cmdline start
831  * @cmd_end: pointer to a ulong variable, will hold cmdline end
832  *
833  * boot_get_cmdline() allocates space for kernel command line below
834  * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
835  * variable is present its contents is copied to allocated kernel
836  * command line.
837  *
838  * returns:
839  *      0 - success
840  *     -1 - failure
841  */
842 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
843 {
844         char *cmdline;
845         char *s;
846
847         cmdline = (char *)(ulong)lmb_alloc_base(lmb, CONFIG_SYS_BARGSIZE, 0xf,
848                                 env_get_bootm_mapsize() + env_get_bootm_low());
849         if (!cmdline)
850                 return -1;
851
852         s = env_get("bootargs");
853         if (!s)
854                 s = "";
855
856         strcpy(cmdline, s);
857
858         *cmd_start = (ulong)cmdline;
859         *cmd_end = *cmd_start + strlen(cmdline);
860
861         debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
862
863         return 0;
864 }
865
866 /**
867  * boot_get_kbd - allocate and initialize kernel copy of board info
868  * @lmb: pointer to lmb handle, will be used for memory mgmt
869  * @kbd: double pointer to board info data
870  *
871  * boot_get_kbd() allocates space for kernel copy of board info data below
872  * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
873  * with the current u-boot board info data.
874  *
875  * returns:
876  *      0 - success
877  *     -1 - failure
878  */
879 int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
880 {
881         *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
882                                                        sizeof(struct bd_info),
883                                                        0xf,
884                                                        env_get_bootm_mapsize() +
885                                                        env_get_bootm_low());
886         if (!*kbd)
887                 return -1;
888
889         **kbd = *gd->bd;
890
891         debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
892
893 #if defined(DEBUG)
894         if (IS_ENABLED(CONFIG_CMD_BDI))
895                 do_bdinfo(NULL, 0, 0, NULL);
896 #endif
897
898         return 0;
899 }
900 #endif
901
902 int image_setup_linux(bootm_headers_t *images)
903 {
904         ulong of_size = images->ft_len;
905         char **of_flat_tree = &images->ft_addr;
906         struct lmb *lmb = &images->lmb;
907         int ret;
908
909         if (CONFIG_IS_ENABLED(OF_LIBFDT))
910                 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
911
912         if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
913                 ret = boot_get_cmdline(lmb, &images->cmdline_start,
914                                        &images->cmdline_end);
915                 if (ret) {
916                         puts("ERROR with allocation of cmdline\n");
917                         return ret;
918                 }
919         }
920
921         if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
922                 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
923                 if (ret)
924                         return ret;
925         }
926
927         if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
928                 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
929                 if (ret)
930                         return ret;
931         }
932
933         return 0;
934 }
935
936 void genimg_print_size(uint32_t size)
937 {
938         printf("%d Bytes = ", size);
939         print_size(size, "\n");
940 }
941
942 void genimg_print_time(time_t timestamp)
943 {
944         struct rtc_time tm;
945
946         rtc_to_tm(timestamp, &tm);
947         printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
948                tm.tm_year, tm.tm_mon, tm.tm_mday,
949                tm.tm_hour, tm.tm_min, tm.tm_sec);
950 }