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