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