Enable network on VF2 board
[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 <log.h>
20 #include <mapmem.h>
21 #include <rtc.h>
22 #include <watchdog.h>
23 #include <asm/cache.h>
24 #include <asm/global_data.h>
25
26 DECLARE_GLOBAL_DATA_PTR;
27
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 struct legacy_img_hdr *image_get_ramdisk(ulong rd_addr, u8 arch,
44                                                       int verify)
45 {
46         const struct legacy_img_hdr *rd_hdr = (const struct legacy_img_hdr *)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
87 /*****************************************************************************/
88 /* Shared dual-format routines */
89 /*****************************************************************************/
90 ulong image_load_addr = CONFIG_SYS_LOAD_ADDR;   /* Default Load Address */
91 ulong image_save_addr;                  /* Default Save Address */
92 ulong image_save_size;                  /* Default Save Size (in bytes) */
93
94 static int on_loadaddr(const char *name, const char *value, enum env_op op,
95                        int flags)
96 {
97         switch (op) {
98         case env_op_create:
99         case env_op_overwrite:
100                 image_load_addr = hextoul(value, NULL);
101                 break;
102         default:
103                 break;
104         }
105
106         return 0;
107 }
108 U_BOOT_ENV_CALLBACK(loadaddr, on_loadaddr);
109
110 ulong env_get_bootm_low(void)
111 {
112         char *s = env_get("bootm_low");
113
114         if (s) {
115                 ulong tmp = hextoul(s, NULL);
116                 return tmp;
117         }
118
119 #if defined(CFG_SYS_SDRAM_BASE)
120         return CFG_SYS_SDRAM_BASE;
121 #elif defined(CONFIG_ARM) || defined(CONFIG_MICROBLAZE) || defined(CONFIG_RISCV)
122         return gd->bd->bi_dram[0].start;
123 #else
124         return 0;
125 #endif
126 }
127
128 phys_size_t env_get_bootm_size(void)
129 {
130         phys_size_t tmp, size;
131         phys_addr_t start;
132         char *s = env_get("bootm_size");
133
134         if (s) {
135                 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
136                 return tmp;
137         }
138
139         start = gd->ram_base;
140         size = gd->ram_size;
141
142         if (start + size > gd->ram_top)
143                 size = gd->ram_top - start;
144
145         s = env_get("bootm_low");
146         if (s)
147                 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
148         else
149                 tmp = start;
150
151         return size - (tmp - start);
152 }
153
154 phys_size_t env_get_bootm_mapsize(void)
155 {
156         phys_size_t tmp;
157         char *s = env_get("bootm_mapsize");
158
159         if (s) {
160                 tmp = (phys_size_t)simple_strtoull(s, NULL, 16);
161                 return tmp;
162         }
163
164 #if defined(CFG_SYS_BOOTMAPSZ)
165         return CFG_SYS_BOOTMAPSZ;
166 #else
167         return env_get_bootm_size();
168 #endif
169 }
170
171 void memmove_wd(void *to, void *from, size_t len, ulong chunksz)
172 {
173         if (to == from)
174                 return;
175
176         if (IS_ENABLED(CONFIG_HW_WATCHDOG) || IS_ENABLED(CONFIG_WATCHDOG)) {
177                 if (to > from) {
178                         from += len;
179                         to += len;
180                 }
181                 while (len > 0) {
182                         size_t tail = (len > chunksz) ? chunksz : len;
183
184                         schedule();
185                         if (to > from) {
186                                 to -= tail;
187                                 from -= tail;
188                         }
189                         memmove(to, from, tail);
190                         if (to < from) {
191                                 to += tail;
192                                 from += tail;
193                         }
194                         len -= tail;
195                 }
196         } else {
197                 memmove(to, from, len);
198         }
199 }
200
201 /**
202  * genimg_get_kernel_addr_fit - get the real kernel address and return 2
203  *                              FIT strings
204  * @img_addr: a string might contain real image address
205  * @fit_uname_config: double pointer to a char, will hold pointer to a
206  *                    configuration unit name
207  * @fit_uname_kernel: double pointer to a char, will hold pointer to a subimage
208  *                    name
209  *
210  * genimg_get_kernel_addr_fit get the real kernel start address from a string
211  * which is normally the first argv of bootm/bootz
212  *
213  * returns:
214  *     kernel start address
215  */
216 ulong genimg_get_kernel_addr_fit(char * const img_addr,
217                                  const char **fit_uname_config,
218                                  const char **fit_uname_kernel)
219 {
220         ulong kernel_addr;
221
222         /* find out kernel image address */
223         if (!img_addr) {
224                 kernel_addr = image_load_addr;
225                 debug("*  kernel: default image load address = 0x%08lx\n",
226                       image_load_addr);
227         } else if (CONFIG_IS_ENABLED(FIT) &&
228                    fit_parse_conf(img_addr, image_load_addr, &kernel_addr,
229                                   fit_uname_config)) {
230                 debug("*  kernel: config '%s' from image at 0x%08lx\n",
231                       *fit_uname_config, kernel_addr);
232         } else if (CONFIG_IS_ENABLED(FIT) &&
233                    fit_parse_subimage(img_addr, image_load_addr, &kernel_addr,
234                                       fit_uname_kernel)) {
235                 debug("*  kernel: subimage '%s' from image at 0x%08lx\n",
236                       *fit_uname_kernel, kernel_addr);
237         } else {
238                 kernel_addr = hextoul(img_addr, NULL);
239                 debug("*  kernel: cmdline image address = 0x%08lx\n",
240                       kernel_addr);
241         }
242
243         return kernel_addr;
244 }
245
246 /**
247  * genimg_get_kernel_addr() is the simple version of
248  * genimg_get_kernel_addr_fit(). It ignores those return FIT strings
249  */
250 ulong genimg_get_kernel_addr(char * const img_addr)
251 {
252         const char *fit_uname_config = NULL;
253         const char *fit_uname_kernel = NULL;
254
255         return genimg_get_kernel_addr_fit(img_addr, &fit_uname_config,
256                                           &fit_uname_kernel);
257 }
258
259 /**
260  * genimg_get_format - get image format type
261  * @img_addr: image start address
262  *
263  * genimg_get_format() checks whether provided address points to a valid
264  * legacy or FIT image.
265  *
266  * New uImage format and FDT blob are based on a libfdt. FDT blob
267  * may be passed directly or embedded in a FIT image. In both situations
268  * genimg_get_format() must be able to dectect libfdt header.
269  *
270  * returns:
271  *     image format type or IMAGE_FORMAT_INVALID if no image is present
272  */
273 int genimg_get_format(const void *img_addr)
274 {
275         if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
276                 const struct legacy_img_hdr *hdr;
277
278                 hdr = (const struct legacy_img_hdr *)img_addr;
279                 if (image_check_magic(hdr))
280                         return IMAGE_FORMAT_LEGACY;
281         }
282         if (CONFIG_IS_ENABLED(FIT) || CONFIG_IS_ENABLED(OF_LIBFDT)) {
283                 if (!fdt_check_header(img_addr))
284                         return IMAGE_FORMAT_FIT;
285         }
286         if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE) &&
287             is_android_boot_image_header(img_addr))
288                 return IMAGE_FORMAT_ANDROID;
289
290         return IMAGE_FORMAT_INVALID;
291 }
292
293 /**
294  * fit_has_config - check if there is a valid FIT configuration
295  * @images: pointer to the bootm command headers structure
296  *
297  * fit_has_config() checks if there is a FIT configuration in use
298  * (if FTI support is present).
299  *
300  * returns:
301  *     0, no FIT support or no configuration found
302  *     1, configuration found
303  */
304 int genimg_has_config(struct bootm_headers *images)
305 {
306         if (CONFIG_IS_ENABLED(FIT) && images->fit_uname_cfg)
307                 return 1;
308
309         return 0;
310 }
311
312 /**
313  * select_ramdisk() - Select and locate the ramdisk to use
314  *
315  * @images: pointer to the bootm images structure
316  * @select: name of ramdisk to select, or hex address, NULL for any
317  * @arch: expected ramdisk architecture
318  * @rd_datap: pointer to a ulong variable, will hold ramdisk pointer
319  * @rd_lenp: pointer to a ulong variable, will hold ramdisk length
320  * Return: 0 if OK, -ENOPKG if no ramdisk (but an error should not be reported),
321  *      other -ve value on other error
322  */
323 static int select_ramdisk(struct bootm_headers *images, const char *select, u8 arch,
324                           ulong *rd_datap, ulong *rd_lenp)
325 {
326         const char *fit_uname_config;
327         const char *fit_uname_ramdisk;
328         bool done_select = !select;
329         bool done = false;
330         int rd_noffset;
331         ulong rd_addr = 0;
332         char *buf;
333
334         if (CONFIG_IS_ENABLED(FIT)) {
335                 fit_uname_config = images->fit_uname_cfg;
336                 fit_uname_ramdisk = NULL;
337
338                 if (select) {
339                         ulong default_addr;
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                                 done_select = true;
356                         } else if (fit_parse_subimage(select, default_addr,
357                                                       &rd_addr,
358                                                       &fit_uname_ramdisk)) {
359                                 debug("*  ramdisk: subimage '%s' from image at 0x%08lx\n",
360                                       fit_uname_ramdisk, rd_addr);
361                                 done_select = true;
362                         }
363                 }
364         }
365         if (!done_select) {
366                 rd_addr = hextoul(select, NULL);
367                 debug("*  ramdisk: cmdline image address = 0x%08lx\n", rd_addr);
368         }
369         if (CONFIG_IS_ENABLED(FIT) && !select) {
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         switch (genimg_get_format(buf)) {
390         case IMAGE_FORMAT_LEGACY:
391                 if (CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)) {
392                         const struct legacy_img_hdr *rd_hdr;
393
394                         printf("## Loading init Ramdisk from Legacy Image at %08lx ...\n",
395                                rd_addr);
396
397                         bootstage_mark(BOOTSTAGE_ID_CHECK_RAMDISK);
398                         rd_hdr = image_get_ramdisk(rd_addr, arch,
399                                                    images->verify);
400
401                         if (!rd_hdr)
402                                 return -ENOENT;
403
404                         *rd_datap = image_get_data(rd_hdr);
405                         *rd_lenp = image_get_data_size(rd_hdr);
406                         done = true;
407                 }
408                 break;
409         case IMAGE_FORMAT_FIT:
410                 if (CONFIG_IS_ENABLED(FIT)) {
411                         rd_noffset = fit_image_load(images, rd_addr,
412                                                     &fit_uname_ramdisk,
413                                                     &fit_uname_config,
414                                                     arch, IH_TYPE_RAMDISK,
415                                                     BOOTSTAGE_ID_FIT_RD_START,
416                                                     FIT_LOAD_OPTIONAL_NON_ZERO,
417                                                     rd_datap, rd_lenp);
418                         if (rd_noffset < 0)
419                                 return rd_noffset;
420
421                         images->fit_hdr_rd = map_sysmem(rd_addr, 0);
422                         images->fit_uname_rd = fit_uname_ramdisk;
423                         images->fit_noffset_rd = rd_noffset;
424                         done = true;
425                 }
426                 break;
427         case IMAGE_FORMAT_ANDROID:
428                 if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
429                         int ret;
430                         if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
431                                 void *boot_img = map_sysmem(get_abootimg_addr(), 0);
432                                 void *vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
433
434                                 ret = android_image_get_ramdisk(boot_img, vendor_boot_img,
435                                                                 rd_datap, rd_lenp);
436                                 unmap_sysmem(vendor_boot_img);
437                                 unmap_sysmem(boot_img);
438                         } else {
439                                 void *ptr = map_sysmem(images->os.start, 0);
440
441                                 ret = android_image_get_ramdisk(ptr, NULL, rd_datap, rd_lenp);
442                                 unmap_sysmem(ptr);
443                         }
444
445                         if (ret)
446                                 return ret;
447                         done = true;
448                 }
449                 break;
450         }
451
452         if (!done) {
453                 if (IS_ENABLED(CONFIG_SUPPORT_RAW_INITRD)) {
454                         char *end = NULL;
455
456                         if (select)
457                                 end = strchr(select, ':');
458                         if (end) {
459                                 *rd_lenp = hextoul(++end, NULL);
460                                 *rd_datap = rd_addr;
461                                 done = true;
462                         }
463                 }
464
465                 if (!done) {
466                         puts("Wrong Ramdisk Image Format\n");
467                         return -EINVAL;
468                 }
469         }
470
471         return 0;
472 }
473
474 /**
475  * boot_get_ramdisk - main ramdisk handling routine
476  * @argc: command argument count
477  * @argv: command argument list
478  * @images: pointer to the bootm images structure
479  * @arch: expected ramdisk architecture
480  * @rd_start: pointer to a ulong variable, will hold ramdisk start address
481  * @rd_end: pointer to a ulong variable, will hold ramdisk end
482  *
483  * boot_get_ramdisk() is responsible for finding a valid ramdisk image.
484  * Currently supported are the following ramdisk sources:
485  *      - multicomponent kernel/ramdisk image,
486  *      - commandline provided address of decicated ramdisk image.
487  *
488  * returns:
489  *     0, if ramdisk image was found and valid, or skiped
490  *     rd_start and rd_end are set to ramdisk start/end addresses if
491  *     ramdisk image is found and valid
492  *
493  *     1, if ramdisk image is found but corrupted, or invalid
494  *     rd_start and rd_end are set to 0 if no ramdisk exists
495  */
496 int boot_get_ramdisk(int argc, char *const argv[], struct bootm_headers *images,
497                      u8 arch, ulong *rd_start, ulong *rd_end)
498 {
499         ulong rd_data, rd_len;
500         const char *select = NULL;
501
502         *rd_start = 0;
503         *rd_end = 0;
504
505         if (IS_ENABLED(CONFIG_ANDROID_BOOT_IMAGE)) {
506                 char *buf;
507
508                 /* Look for an Android boot image */
509                 buf = map_sysmem(images->os.start, 0);
510                 if (buf && genimg_get_format(buf) == IMAGE_FORMAT_ANDROID)
511                         select = (argc == 0) ? env_get("loadaddr") : argv[0];
512         }
513
514         if (argc >= 2)
515                 select = argv[1];
516
517         /*
518          * Look for a '-' which indicates to ignore the
519          * ramdisk argument
520          */
521         if (select && strcmp(select, "-") ==  0) {
522                 debug("## Skipping init Ramdisk\n");
523                 rd_len = 0;
524                 rd_data = 0;
525         } else if (select || genimg_has_config(images)) {
526                 int ret;
527
528                 ret = select_ramdisk(images, select, arch, &rd_data, &rd_len);
529                 if (ret == -ENOPKG)
530                         return 0;
531                 else if (ret)
532                         return ret;
533         } else if (images->legacy_hdr_valid &&
534                         image_check_type(&images->legacy_hdr_os_copy,
535                                          IH_TYPE_MULTI)) {
536                 /*
537                  * Now check if we have a legacy mult-component image,
538                  * get second entry data start address and len.
539                  */
540                 bootstage_mark(BOOTSTAGE_ID_RAMDISK);
541                 printf("## Loading init Ramdisk from multi component Legacy Image at %08lx ...\n",
542                        (ulong)images->legacy_hdr_os);
543
544                 image_multi_getimg(images->legacy_hdr_os, 1, &rd_data, &rd_len);
545         } else {
546                 /*
547                  * no initrd image
548                  */
549                 bootstage_mark(BOOTSTAGE_ID_NO_RAMDISK);
550                 rd_len = 0;
551                 rd_data = 0;
552         }
553
554         if (!rd_data) {
555                 debug("## No init Ramdisk\n");
556         } else {
557                 *rd_start = rd_data;
558                 *rd_end = rd_data + rd_len;
559         }
560         debug("   ramdisk start = 0x%08lx, ramdisk end = 0x%08lx\n",
561               *rd_start, *rd_end);
562
563         return 0;
564 }
565
566 /**
567  * boot_ramdisk_high - relocate init ramdisk
568  * @lmb: pointer to lmb handle, will be used for memory mgmt
569  * @rd_data: ramdisk data start address
570  * @rd_len: ramdisk data length
571  * @initrd_start: pointer to a ulong variable, will hold final init ramdisk
572  *      start address (after possible relocation)
573  * @initrd_end: pointer to a ulong variable, will hold final init ramdisk
574  *      end address (after possible relocation)
575  *
576  * boot_ramdisk_high() takes a relocation hint from "initrd_high" environment
577  * variable and if requested ramdisk data is moved to a specified location.
578  *
579  * Initrd_start and initrd_end are set to final (after relocation) ramdisk
580  * start/end addresses if ramdisk image start and len were provided,
581  * otherwise set initrd_start and initrd_end set to zeros.
582  *
583  * returns:
584  *      0 - success
585  *     -1 - failure
586  */
587 int boot_ramdisk_high(struct lmb *lmb, ulong rd_data, ulong rd_len,
588                       ulong *initrd_start, ulong *initrd_end)
589 {
590         char    *s;
591         ulong   initrd_high;
592         int     initrd_copy_to_ram = 1;
593
594         s = env_get("initrd_high");
595         if (s) {
596                 /* a value of "no" or a similar string will act like 0,
597                  * turning the "load high" feature off. This is intentional.
598                  */
599                 initrd_high = hextoul(s, NULL);
600                 if (initrd_high == ~0)
601                         initrd_copy_to_ram = 0;
602         } else {
603                 initrd_high = env_get_bootm_mapsize() + env_get_bootm_low();
604         }
605
606         debug("## initrd_high = 0x%08lx, copy_to_ram = %d\n",
607               initrd_high, initrd_copy_to_ram);
608
609         if (rd_data) {
610                 if (!initrd_copy_to_ram) {      /* zero-copy ramdisk support */
611                         debug("   in-place initrd\n");
612                         *initrd_start = rd_data;
613                         *initrd_end = rd_data + rd_len;
614                         lmb_reserve(lmb, rd_data, rd_len);
615                 } else {
616                         if (initrd_high)
617                                 *initrd_start = (ulong)lmb_alloc_base(lmb,
618                                                 rd_len, 0x1000, initrd_high);
619                         else
620                                 *initrd_start = (ulong)lmb_alloc(lmb, rd_len,
621                                                                  0x1000);
622
623                         if (*initrd_start == 0) {
624                                 puts("ramdisk - allocation error\n");
625                                 goto error;
626                         }
627                         bootstage_mark(BOOTSTAGE_ID_COPY_RAMDISK);
628
629                         *initrd_end = *initrd_start + rd_len;
630                         printf("   Loading Ramdisk to %08lx, end %08lx ... ",
631                                *initrd_start, *initrd_end);
632
633                         memmove_wd((void *)*initrd_start,
634                                    (void *)rd_data, rd_len, CHUNKSZ);
635
636                         /*
637                          * Ensure the image is flushed to memory to handle
638                          * AMP boot scenarios in which we might not be
639                          * HW cache coherent
640                          */
641                         if (IS_ENABLED(CONFIG_MP)) {
642                                 flush_cache((unsigned long)*initrd_start,
643                                             ALIGN(rd_len, ARCH_DMA_MINALIGN));
644                         }
645                         puts("OK\n");
646                 }
647         } else {
648                 *initrd_start = 0;
649                 *initrd_end = 0;
650         }
651         debug("   ramdisk load start = 0x%08lx, ramdisk load end = 0x%08lx\n",
652               *initrd_start, *initrd_end);
653
654         return 0;
655
656 error:
657         return -1;
658 }
659
660 int boot_get_setup(struct bootm_headers *images, u8 arch,
661                    ulong *setup_start, ulong *setup_len)
662 {
663         if (!CONFIG_IS_ENABLED(FIT))
664                 return -ENOENT;
665
666         return boot_get_setup_fit(images, arch, setup_start, setup_len);
667 }
668
669 int boot_get_fpga(int argc, char *const argv[], struct bootm_headers *images,
670                   u8 arch, const ulong *ld_start, ulong * const ld_len)
671 {
672         ulong tmp_img_addr, img_data, img_len;
673         void *buf;
674         int conf_noffset;
675         int fit_img_result;
676         const char *uname, *name;
677         int err;
678         int devnum = 0; /* TODO support multi fpga platforms */
679
680         if (!IS_ENABLED(CONFIG_FPGA))
681                 return -ENOSYS;
682
683         /* Check to see if the images struct has a FIT configuration */
684         if (!genimg_has_config(images)) {
685                 debug("## FIT configuration was not specified\n");
686                 return 0;
687         }
688
689         /*
690          * Obtain the os FIT header from the images struct
691          */
692         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
693         buf = map_sysmem(tmp_img_addr, 0);
694         /*
695          * Check image type. For FIT images get FIT node
696          * and attempt to locate a generic binary.
697          */
698         switch (genimg_get_format(buf)) {
699         case IMAGE_FORMAT_FIT:
700                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
701
702                 uname = fdt_stringlist_get(buf, conf_noffset, FIT_FPGA_PROP, 0,
703                                            NULL);
704                 if (!uname) {
705                         debug("## FPGA image is not specified\n");
706                         return 0;
707                 }
708                 fit_img_result = fit_image_load(images,
709                                                 tmp_img_addr,
710                                                 (const char **)&uname,
711                                                 &images->fit_uname_cfg,
712                                                 arch,
713                                                 IH_TYPE_FPGA,
714                                                 BOOTSTAGE_ID_FPGA_INIT,
715                                                 FIT_LOAD_OPTIONAL_NON_ZERO,
716                                                 &img_data, &img_len);
717
718                 debug("FPGA image (%s) loaded to 0x%lx/size 0x%lx\n",
719                       uname, img_data, img_len);
720
721                 if (fit_img_result < 0) {
722                         /* Something went wrong! */
723                         return fit_img_result;
724                 }
725
726                 if (!fpga_is_partial_data(devnum, img_len)) {
727                         name = "full";
728                         err = fpga_loadbitstream(devnum, (char *)img_data,
729                                                  img_len, BIT_FULL);
730                         if (err)
731                                 err = fpga_load(devnum, (const void *)img_data,
732                                                 img_len, BIT_FULL, 0);
733                 } else {
734                         name = "partial";
735                         err = fpga_loadbitstream(devnum, (char *)img_data,
736                                                  img_len, BIT_PARTIAL);
737                         if (err)
738                                 err = fpga_load(devnum, (const void *)img_data,
739                                                 img_len, BIT_PARTIAL, 0);
740                 }
741
742                 if (err)
743                         return err;
744
745                 printf("   Programming %s bitstream... OK\n", name);
746                 break;
747         default:
748                 printf("The given image format is not supported (corrupt?)\n");
749                 return 1;
750         }
751
752         return 0;
753 }
754
755 static void fit_loadable_process(u8 img_type,
756                                  ulong img_data,
757                                  ulong img_len)
758 {
759         int i;
760         const unsigned int count =
761                         ll_entry_count(struct fit_loadable_tbl, fit_loadable);
762         struct fit_loadable_tbl *fit_loadable_handler =
763                         ll_entry_start(struct fit_loadable_tbl, fit_loadable);
764         /* For each loadable handler */
765         for (i = 0; i < count; i++, fit_loadable_handler++)
766                 /* matching this type */
767                 if (fit_loadable_handler->type == img_type)
768                         /* call that handler with this image data */
769                         fit_loadable_handler->handler(img_data, img_len);
770 }
771
772 int boot_get_loadable(int argc, char *const argv[], struct bootm_headers *images,
773                       u8 arch, const ulong *ld_start, ulong * const ld_len)
774 {
775         /*
776          * These variables are used to hold the current image location
777          * in system memory.
778          */
779         ulong tmp_img_addr;
780         /*
781          * These two variables are requirements for fit_image_load, but
782          * their values are not used
783          */
784         ulong img_data, img_len;
785         void *buf;
786         int loadables_index;
787         int conf_noffset;
788         int fit_img_result;
789         const char *uname;
790         u8 img_type;
791
792         /* Check to see if the images struct has a FIT configuration */
793         if (!genimg_has_config(images)) {
794                 debug("## FIT configuration was not specified\n");
795                 return 0;
796         }
797
798         /*
799          * Obtain the os FIT header from the images struct
800          */
801         tmp_img_addr = map_to_sysmem(images->fit_hdr_os);
802         buf = map_sysmem(tmp_img_addr, 0);
803         /*
804          * Check image type. For FIT images get FIT node
805          * and attempt to locate a generic binary.
806          */
807         switch (genimg_get_format(buf)) {
808         case IMAGE_FORMAT_FIT:
809                 conf_noffset = fit_conf_get_node(buf, images->fit_uname_cfg);
810
811                 for (loadables_index = 0;
812                      uname = fdt_stringlist_get(buf, conf_noffset,
813                                                 FIT_LOADABLE_PROP,
814                                                 loadables_index, NULL), uname;
815                      loadables_index++) {
816                         fit_img_result = fit_image_load(images, tmp_img_addr,
817                                                         &uname,
818                                                         &images->fit_uname_cfg,
819                                                         arch, IH_TYPE_LOADABLE,
820                                                         BOOTSTAGE_ID_FIT_LOADABLE_START,
821                                                         FIT_LOAD_OPTIONAL_NON_ZERO,
822                                                         &img_data, &img_len);
823                         if (fit_img_result < 0) {
824                                 /* Something went wrong! */
825                                 return fit_img_result;
826                         }
827
828                         fit_img_result = fit_image_get_node(buf, uname);
829                         if (fit_img_result < 0) {
830                                 /* Something went wrong! */
831                                 return fit_img_result;
832                         }
833                         fit_img_result = fit_image_get_type(buf,
834                                                             fit_img_result,
835                                                             &img_type);
836                         if (fit_img_result < 0) {
837                                 /* Something went wrong! */
838                                 return fit_img_result;
839                         }
840
841                         fit_loadable_process(img_type, img_data, img_len);
842                 }
843                 break;
844         default:
845                 printf("The given image format is not supported (corrupt?)\n");
846                 return 1;
847         }
848
849         return 0;
850 }
851
852 /**
853  * boot_get_cmdline - allocate and initialize kernel cmdline
854  * @lmb: pointer to lmb handle, will be used for memory mgmt
855  * @cmd_start: pointer to a ulong variable, will hold cmdline start
856  * @cmd_end: pointer to a ulong variable, will hold cmdline end
857  *
858  * This allocates space for kernel command line below
859  * BOOTMAPSZ + env_get_bootm_low() address. If "bootargs" U-Boot environment
860  * variable is present its contents is copied to allocated kernel
861  * command line.
862  *
863  * returns:
864  *      0 - success
865  *     -1 - failure
866  */
867 int boot_get_cmdline(struct lmb *lmb, ulong *cmd_start, ulong *cmd_end)
868 {
869         int barg;
870         char *cmdline;
871         char *s;
872
873         /*
874          * Help the compiler detect that this function is only called when
875          * CONFIG_SYS_BOOT_GET_CMDLINE is enabled
876          */
877         if (!IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE))
878                 return 0;
879
880         barg = IF_ENABLED_INT(CONFIG_SYS_BOOT_GET_CMDLINE, CONFIG_SYS_BARGSIZE);
881         cmdline = (char *)(ulong)lmb_alloc_base(lmb, barg, 0xf,
882                                 env_get_bootm_mapsize() + env_get_bootm_low());
883         if (!cmdline)
884                 return -1;
885
886         s = env_get("bootargs");
887         if (!s)
888                 s = "";
889
890         strcpy(cmdline, s);
891
892         *cmd_start = (ulong)cmdline;
893         *cmd_end = *cmd_start + strlen(cmdline);
894
895         debug("## cmdline at 0x%08lx ... 0x%08lx\n", *cmd_start, *cmd_end);
896
897         return 0;
898 }
899
900 /**
901  * boot_get_kbd - allocate and initialize kernel copy of board info
902  * @lmb: pointer to lmb handle, will be used for memory mgmt
903  * @kbd: double pointer to board info data
904  *
905  * boot_get_kbd() allocates space for kernel copy of board info data below
906  * BOOTMAPSZ + env_get_bootm_low() address and kernel board info is initialized
907  * with the current u-boot board info data.
908  *
909  * returns:
910  *      0 - success
911  *     -1 - failure
912  */
913 int boot_get_kbd(struct lmb *lmb, struct bd_info **kbd)
914 {
915         *kbd = (struct bd_info *)(ulong)lmb_alloc_base(lmb,
916                                                        sizeof(struct bd_info),
917                                                        0xf,
918                                                        env_get_bootm_mapsize() +
919                                                        env_get_bootm_low());
920         if (!*kbd)
921                 return -1;
922
923         **kbd = *gd->bd;
924
925         debug("## kernel board info at 0x%08lx\n", (ulong)*kbd);
926
927         if (_DEBUG && IS_ENABLED(CONFIG_CMD_BDI))
928                 do_bdinfo(NULL, 0, 0, NULL);
929
930         return 0;
931 }
932
933 int image_setup_linux(struct bootm_headers *images)
934 {
935         ulong of_size = images->ft_len;
936         char **of_flat_tree = &images->ft_addr;
937         struct lmb *lmb = images_lmb(images);
938         int ret;
939
940         /* This function cannot be called without lmb support */
941         if (!IS_ENABLED(CONFIG_LMB))
942                 return -EFAULT;
943         if (CONFIG_IS_ENABLED(OF_LIBFDT))
944                 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
945
946         if (IS_ENABLED(CONFIG_SYS_BOOT_GET_CMDLINE)) {
947                 ret = boot_get_cmdline(lmb, &images->cmdline_start,
948                                        &images->cmdline_end);
949                 if (ret) {
950                         puts("ERROR with allocation of cmdline\n");
951                         return ret;
952                 }
953         }
954
955         if (CONFIG_IS_ENABLED(OF_LIBFDT)) {
956                 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
957                 if (ret)
958                         return ret;
959         }
960
961         if (CONFIG_IS_ENABLED(OF_LIBFDT) && of_size) {
962                 ret = image_setup_libfdt(images, *of_flat_tree, of_size, lmb);
963                 if (ret)
964                         return ret;
965         }
966
967         return 0;
968 }
969
970 void genimg_print_size(uint32_t size)
971 {
972         printf("%d Bytes = ", size);
973         print_size(size, "\n");
974 }
975
976 void genimg_print_time(time_t timestamp)
977 {
978         struct rtc_time tm;
979
980         rtc_to_tm(timestamp, &tm);
981         printf("%4d-%02d-%02d  %2d:%02d:%02d UTC\n",
982                tm.tm_year, tm.tm_mon, tm.tm_mday,
983                tm.tm_hour, tm.tm_min, tm.tm_sec);
984 }
985
986 /**
987  * get_default_image() - Return default property from /images
988  *
989  * Return: Pointer to value of default property (or NULL)
990  */
991 static const char *get_default_image(const void *fit)
992 {
993         int images_noffset;
994
995         images_noffset = fdt_path_offset(fit, FIT_IMAGES_PATH);
996         if (images_noffset < 0)
997                 return NULL;
998
999         return fdt_getprop(fit, images_noffset, FIT_DEFAULT_PROP, NULL);
1000 }
1001
1002 int image_locate_script(void *buf, int size, const char *fit_uname,
1003                         const char *confname, char **datap, uint *lenp)
1004 {
1005         const struct legacy_img_hdr *hdr;
1006         const void *fit_data;
1007         const void *fit_hdr;
1008         size_t fit_len;
1009         int noffset;
1010         int verify;
1011         ulong len;
1012         u32 *data;
1013
1014         verify = env_get_yesno("verify");
1015
1016         switch (genimg_get_format(buf)) {
1017         case IMAGE_FORMAT_LEGACY:
1018                 if (!IS_ENABLED(CONFIG_LEGACY_IMAGE_FORMAT)) {
1019                         goto exit_image_format;
1020                 } else {
1021                         hdr = buf;
1022
1023                         if (!image_check_magic(hdr)) {
1024                                 puts("Bad magic number\n");
1025                                 return 1;
1026                         }
1027
1028                         if (!image_check_hcrc(hdr)) {
1029                                 puts("Bad header crc\n");
1030                                 return 1;
1031                         }
1032
1033                         if (verify) {
1034                                 if (!image_check_dcrc(hdr)) {
1035                                         puts("Bad data crc\n");
1036                                         return 1;
1037                                 }
1038                         }
1039
1040                         if (!image_check_type(hdr, IH_TYPE_SCRIPT)) {
1041                                 puts("Bad image type\n");
1042                                 return 1;
1043                         }
1044
1045                         /* get length of script */
1046                         data = (u32 *)image_get_data(hdr);
1047
1048                         len = uimage_to_cpu(*data);
1049                         if (!len) {
1050                                 puts("Empty Script\n");
1051                                 return 1;
1052                         }
1053
1054                         /*
1055                          * scripts are just multi-image files with one
1056                          * component, so seek past the zero-terminated sequence
1057                          * of image lengths to get to the actual image data
1058                          */
1059                         while (*data++);
1060                 }
1061                 break;
1062         case IMAGE_FORMAT_FIT:
1063                 if (!IS_ENABLED(CONFIG_FIT)) {
1064                         goto exit_image_format;
1065                 } else {
1066                         fit_hdr = buf;
1067                         if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
1068                                 puts("Bad FIT image format\n");
1069                                 return 1;
1070                         }
1071
1072                         if (!fit_uname) {
1073                                 /* If confname is empty, use the default */
1074                                 if (confname && *confname)
1075                                         noffset = fit_conf_get_node(fit_hdr, confname);
1076                                 else
1077                                         noffset = fit_conf_get_node(fit_hdr, NULL);
1078                                 if (noffset < 0) {
1079                                         if (!confname)
1080                                                 goto fallback;
1081                                         printf("Could not find config %s\n", confname);
1082                                         return 1;
1083                                 }
1084
1085                                 if (verify && fit_config_verify(fit_hdr, noffset))
1086                                         return 1;
1087
1088                                 noffset = fit_conf_get_prop_node(fit_hdr,
1089                                                                  noffset,
1090                                                                  FIT_SCRIPT_PROP,
1091                                                                  IH_PHASE_NONE);
1092                                 if (noffset < 0) {
1093                                         if (!confname)
1094                                                 goto fallback;
1095                                         printf("Could not find script in %s\n", confname);
1096                                         return 1;
1097                                 }
1098                         } else {
1099 fallback:
1100                                 if (!fit_uname || !*fit_uname)
1101                                         fit_uname = get_default_image(fit_hdr);
1102                                 if (!fit_uname) {
1103                                         puts("No FIT subimage unit name\n");
1104                                         return 1;
1105                                 }
1106
1107                                 /* get script component image node offset */
1108                                 noffset = fit_image_get_node(fit_hdr, fit_uname);
1109                                 if (noffset < 0) {
1110                                         printf("Can't find '%s' FIT subimage\n",
1111                                                fit_uname);
1112                                         return 1;
1113                                 }
1114                         }
1115
1116                         if (!fit_image_check_type(fit_hdr, noffset,
1117                                                   IH_TYPE_SCRIPT)) {
1118                                 puts("Not a image image\n");
1119                                 return 1;
1120                         }
1121
1122                         /* verify integrity */
1123                         if (verify && !fit_image_verify(fit_hdr, noffset)) {
1124                                 puts("Bad Data Hash\n");
1125                                 return 1;
1126                         }
1127
1128                         /* get script subimage data address and length */
1129                         if (fit_image_get_data_and_size(fit_hdr, noffset,
1130                                                         &fit_data, &fit_len)) {
1131                                 puts("Could not find script subimage data\n");
1132                                 return 1;
1133                         }
1134
1135                         data = (u32 *)fit_data;
1136                         len = (ulong)fit_len;
1137                 }
1138                 break;
1139         default:
1140                 goto exit_image_format;
1141         }
1142
1143         *datap = (char *)data;
1144         *lenp = len;
1145
1146         return 0;
1147
1148 exit_image_format:
1149         puts("Wrong image format for \"source\" command\n");
1150         return -EPERM;
1151 }