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