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