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