configs: starfive: add starfive_visionfive2_defconfig
[platform/kernel/u-boot.git] / boot / bootm.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2009
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6
7 #ifndef USE_HOSTCC
8 #include <common.h>
9 #include <bootstage.h>
10 #include <cli.h>
11 #include <cpu_func.h>
12 #include <env.h>
13 #include <errno.h>
14 #include <fdt_support.h>
15 #include <irq_func.h>
16 #include <lmb.h>
17 #include <log.h>
18 #include <malloc.h>
19 #include <mapmem.h>
20 #include <net.h>
21 #include <asm/cache.h>
22 #include <asm/global_data.h>
23 #include <asm/io.h>
24 #include <linux/sizes.h>
25 #if defined(CONFIG_CMD_USB)
26 #include <usb.h>
27 #endif
28 #else
29 #include "mkimage.h"
30 #endif
31
32 #include <command.h>
33 #include <bootm.h>
34 #include <image.h>
35
36 #define MAX_CMDLINE_SIZE        SZ_4K
37
38 #define IH_INITRD_ARCH IH_ARCH_DEFAULT
39
40 #ifndef USE_HOSTCC
41
42 DECLARE_GLOBAL_DATA_PTR;
43
44 struct bootm_headers images;            /* pointers to os/initrd/fdt images */
45
46 static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
47                                    char *const argv[], struct bootm_headers *images,
48                                    ulong *os_data, ulong *os_len);
49
50 __weak void board_quiesce_devices(void)
51 {
52 }
53
54 #ifdef CONFIG_LMB
55 static void boot_start_lmb(struct bootm_headers *images)
56 {
57         ulong           mem_start;
58         phys_size_t     mem_size;
59
60         mem_start = env_get_bootm_low();
61         mem_size = env_get_bootm_size();
62
63         lmb_init_and_reserve_range(&images->lmb, (phys_addr_t)mem_start,
64                                    mem_size, NULL);
65 }
66 #else
67 #define lmb_reserve(lmb, base, size)
68 static inline void boot_start_lmb(struct bootm_headers *images) { }
69 #endif
70
71 static int bootm_start(struct cmd_tbl *cmdtp, int flag, int argc,
72                        char *const argv[])
73 {
74         memset((void *)&images, 0, sizeof(images));
75         images.verify = env_get_yesno("verify");
76
77         boot_start_lmb(&images);
78
79         bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
80         images.state = BOOTM_STATE_START;
81
82         return 0;
83 }
84
85 static ulong bootm_data_addr(int argc, char *const argv[])
86 {
87         ulong addr;
88
89         if (argc > 0)
90                 addr = simple_strtoul(argv[0], NULL, 16);
91         else
92                 addr = image_load_addr;
93
94         return addr;
95 }
96
97 static int bootm_pre_load(struct cmd_tbl *cmdtp, int flag, int argc,
98                           char *const argv[])
99 {
100         ulong data_addr = bootm_data_addr(argc, argv);
101         int ret = 0;
102
103         if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
104                 ret = image_pre_load(data_addr);
105
106         if (ret)
107                 ret = CMD_RET_FAILURE;
108
109         return ret;
110 }
111
112 static int bootm_find_os(struct cmd_tbl *cmdtp, int flag, int argc,
113                          char *const argv[])
114 {
115         const void *os_hdr;
116 #ifdef CONFIG_ANDROID_BOOT_IMAGE
117         const void *vendor_boot_img;
118         const void *boot_img;
119 #endif
120         bool ep_found = false;
121         int ret;
122
123         /* get kernel image header, start address and length */
124         os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
125                         &images, &images.os.image_start, &images.os.image_len);
126         if (images.os.image_len == 0) {
127                 puts("ERROR: can't get kernel image!\n");
128                 return 1;
129         }
130
131         /* get image parameters */
132         switch (genimg_get_format(os_hdr)) {
133 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
134         case IMAGE_FORMAT_LEGACY:
135                 images.os.type = image_get_type(os_hdr);
136                 images.os.comp = image_get_comp(os_hdr);
137                 images.os.os = image_get_os(os_hdr);
138
139                 images.os.end = image_get_image_end(os_hdr);
140                 images.os.load = image_get_load(os_hdr);
141                 images.os.arch = image_get_arch(os_hdr);
142                 break;
143 #endif
144 #if CONFIG_IS_ENABLED(FIT)
145         case IMAGE_FORMAT_FIT:
146                 if (fit_image_get_type(images.fit_hdr_os,
147                                        images.fit_noffset_os,
148                                        &images.os.type)) {
149                         puts("Can't get image type!\n");
150                         bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
151                         return 1;
152                 }
153
154                 if (fit_image_get_comp(images.fit_hdr_os,
155                                        images.fit_noffset_os,
156                                        &images.os.comp)) {
157                         puts("Can't get image compression!\n");
158                         bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
159                         return 1;
160                 }
161
162                 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
163                                      &images.os.os)) {
164                         puts("Can't get image OS!\n");
165                         bootstage_error(BOOTSTAGE_ID_FIT_OS);
166                         return 1;
167                 }
168
169                 if (fit_image_get_arch(images.fit_hdr_os,
170                                        images.fit_noffset_os,
171                                        &images.os.arch)) {
172                         puts("Can't get image ARCH!\n");
173                         return 1;
174                 }
175
176                 images.os.end = fit_get_end(images.fit_hdr_os);
177
178                 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
179                                        &images.os.load)) {
180                         puts("Can't get image load address!\n");
181                         bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
182                         return 1;
183                 }
184                 break;
185 #endif
186 #ifdef CONFIG_ANDROID_BOOT_IMAGE
187         case IMAGE_FORMAT_ANDROID:
188                 boot_img = os_hdr;
189                 vendor_boot_img = NULL;
190                 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
191                         boot_img = map_sysmem(get_abootimg_addr(), 0);
192                         vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
193                 }
194                 images.os.type = IH_TYPE_KERNEL;
195                 images.os.comp = android_image_get_kcomp(boot_img, vendor_boot_img);
196                 images.os.os = IH_OS_LINUX;
197                 images.os.end = android_image_get_end(boot_img, vendor_boot_img);
198                 images.os.load = android_image_get_kload(boot_img, vendor_boot_img);
199                 images.ep = images.os.load;
200                 ep_found = true;
201                 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
202                         unmap_sysmem(vendor_boot_img);
203                         unmap_sysmem(boot_img);
204                 }
205                 break;
206 #endif
207         default:
208                 puts("ERROR: unknown image format type!\n");
209                 return 1;
210         }
211
212         /* If we have a valid setup.bin, we will use that for entry (x86) */
213         if (images.os.arch == IH_ARCH_I386 ||
214             images.os.arch == IH_ARCH_X86_64) {
215                 ulong len;
216
217                 ret = boot_get_setup(&images, IH_ARCH_I386, &images.ep, &len);
218                 if (ret < 0 && ret != -ENOENT) {
219                         puts("Could not find a valid setup.bin for x86\n");
220                         return 1;
221                 }
222                 /* Kernel entry point is the setup.bin */
223         } else if (images.legacy_hdr_valid) {
224                 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
225 #if CONFIG_IS_ENABLED(FIT)
226         } else if (images.fit_uname_os) {
227                 int ret;
228
229                 ret = fit_image_get_entry(images.fit_hdr_os,
230                                           images.fit_noffset_os, &images.ep);
231                 if (ret) {
232                         puts("Can't get entry point property!\n");
233                         return 1;
234                 }
235 #endif
236         } else if (!ep_found) {
237                 puts("Could not find kernel entry point!\n");
238                 return 1;
239         }
240
241         if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
242                 if (IS_ENABLED(CONFIG_CMD_BOOTI) &&
243                     images.os.arch == IH_ARCH_ARM64) {
244                         ulong image_addr;
245                         ulong image_size;
246
247                         ret = booti_setup(images.os.image_start, &image_addr,
248                                           &image_size, true);
249                         if (ret != 0)
250                                 return 1;
251
252                         images.os.type = IH_TYPE_KERNEL;
253                         images.os.load = image_addr;
254                         images.ep = image_addr;
255                 } else {
256                         images.os.load = images.os.image_start;
257                         images.ep += images.os.image_start;
258                 }
259         }
260
261         images.os.start = map_to_sysmem(os_hdr);
262
263         return 0;
264 }
265
266 /**
267  * bootm_find_images - wrapper to find and locate various images
268  * @flag: Ignored Argument
269  * @argc: command argument count
270  * @argv: command argument list
271  * @start: OS image start address
272  * @size: OS image size
273  *
274  * boot_find_images() will attempt to load an available ramdisk,
275  * flattened device tree, as well as specifically marked
276  * "loadable" images (loadables are FIT only)
277  *
278  * Note: bootm_find_images will skip an image if it is not found
279  *
280  * @return:
281  *     0, if all existing images were loaded correctly
282  *     1, if an image is found but corrupted, or invalid
283  */
284 int bootm_find_images(int flag, int argc, char *const argv[], ulong start,
285                       ulong size)
286 {
287         int ret;
288
289         /* find ramdisk */
290         ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
291                                &images.rd_start, &images.rd_end);
292         if (ret) {
293                 puts("Ramdisk image is corrupt or invalid\n");
294                 return 1;
295         }
296
297         /* check if ramdisk overlaps OS image */
298         if (images.rd_start && (((ulong)images.rd_start >= start &&
299                                  (ulong)images.rd_start < start + size) ||
300                                 ((ulong)images.rd_end > start &&
301                                  (ulong)images.rd_end <= start + size) ||
302                                 ((ulong)images.rd_start < start &&
303                                  (ulong)images.rd_end >= start + size))) {
304                 printf("ERROR: RD image overlaps OS image (OS=0x%lx..0x%lx)\n",
305                        start, start + size);
306                 return 1;
307         }
308
309 #if CONFIG_IS_ENABLED(OF_LIBFDT)
310         /* find flattened device tree */
311         ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
312                            &images.ft_addr, &images.ft_len);
313         if (ret) {
314                 puts("Could not find a valid device tree\n");
315                 return 1;
316         }
317
318         /* check if FDT overlaps OS image */
319         if (images.ft_addr &&
320             (((ulong)images.ft_addr >= start &&
321               (ulong)images.ft_addr < start + size) ||
322              ((ulong)images.ft_addr + images.ft_len >= start &&
323               (ulong)images.ft_addr + images.ft_len < start + size))) {
324                 printf("ERROR: FDT image overlaps OS image (OS=0x%lx..0x%lx)\n",
325                        start, start + size);
326                 return 1;
327         }
328
329         if (IS_ENABLED(CONFIG_CMD_FDT))
330                 set_working_fdt_addr(map_to_sysmem(images.ft_addr));
331 #endif
332
333 #if CONFIG_IS_ENABLED(FIT)
334         if (IS_ENABLED(CONFIG_FPGA)) {
335                 /* find bitstreams */
336                 ret = boot_get_fpga(argc, argv, &images, IH_ARCH_DEFAULT,
337                                     NULL, NULL);
338                 if (ret) {
339                         printf("FPGA image is corrupted or invalid\n");
340                         return 1;
341                 }
342         }
343
344         /* find all of the loadables */
345         ret = boot_get_loadable(argc, argv, &images, IH_ARCH_DEFAULT,
346                                NULL, NULL);
347         if (ret) {
348                 printf("Loadable(s) is corrupt or invalid\n");
349                 return 1;
350         }
351 #endif
352
353         return 0;
354 }
355
356 static int bootm_find_other(struct cmd_tbl *cmdtp, int flag, int argc,
357                             char *const argv[])
358 {
359         if (((images.os.type == IH_TYPE_KERNEL) ||
360              (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
361              (images.os.type == IH_TYPE_MULTI)) &&
362             (images.os.os == IH_OS_LINUX ||
363                  images.os.os == IH_OS_VXWORKS))
364                 return bootm_find_images(flag, argc, argv, 0, 0);
365
366         return 0;
367 }
368 #endif /* USE_HOSTC */
369
370 #if !defined(USE_HOSTCC) || defined(CONFIG_FIT_SIGNATURE)
371 /**
372  * handle_decomp_error() - display a decompression error
373  *
374  * This function tries to produce a useful message. In the case where the
375  * uncompressed size is the same as the available space, we can assume that
376  * the image is too large for the buffer.
377  *
378  * @comp_type:          Compression type being used (IH_COMP_...)
379  * @uncomp_size:        Number of bytes uncompressed
380  * @buf_size:           Number of bytes the decompresion buffer was
381  * @ret:                errno error code received from compression library
382  * Return: Appropriate BOOTM_ERR_ error code
383  */
384 static int handle_decomp_error(int comp_type, size_t uncomp_size,
385                                size_t buf_size, int ret)
386 {
387         const char *name = genimg_get_comp_name(comp_type);
388
389         /* ENOSYS means unimplemented compression type, don't reset. */
390         if (ret == -ENOSYS)
391                 return BOOTM_ERR_UNIMPLEMENTED;
392
393         if (uncomp_size >= buf_size)
394                 printf("Image too large: increase CONFIG_SYS_BOOTM_LEN\n");
395         else
396                 printf("%s: uncompress error %d\n", name, ret);
397
398         /*
399          * The decompression routines are now safe, so will not write beyond
400          * their bounds. Probably it is not necessary to reset, but maintain
401          * the current behaviour for now.
402          */
403         printf("Must RESET board to recover\n");
404 #ifndef USE_HOSTCC
405         bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
406 #endif
407
408         return BOOTM_ERR_RESET;
409 }
410 #endif
411
412 #ifndef USE_HOSTCC
413 static int bootm_load_os(struct bootm_headers *images, int boot_progress)
414 {
415         struct image_info os = images->os;
416         ulong load = os.load;
417         ulong load_end;
418         ulong blob_start = os.start;
419         ulong blob_end = os.end;
420         ulong image_start = os.image_start;
421         ulong image_len = os.image_len;
422         ulong flush_start = ALIGN_DOWN(load, ARCH_DMA_MINALIGN);
423         bool no_overlap;
424         void *load_buf, *image_buf;
425         int err;
426
427         load_buf = map_sysmem(load, 0);
428         image_buf = map_sysmem(os.image_start, image_len);
429         err = image_decomp(os.comp, load, os.image_start, os.type,
430                            load_buf, image_buf, image_len,
431                            CONFIG_SYS_BOOTM_LEN, &load_end);
432         if (err) {
433                 err = handle_decomp_error(os.comp, load_end - load,
434                                           CONFIG_SYS_BOOTM_LEN, err);
435                 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
436                 return err;
437         }
438         /* We need the decompressed image size in the next steps */
439         images->os.image_len = load_end - load;
440
441         flush_cache(flush_start, ALIGN(load_end, ARCH_DMA_MINALIGN) - flush_start);
442
443         debug("   kernel loaded at 0x%08lx, end = 0x%08lx\n", load, load_end);
444         bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
445
446         no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
447
448         if (!no_overlap && load < blob_end && load_end > blob_start) {
449                 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
450                       blob_start, blob_end);
451                 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
452                       load_end);
453
454                 /* Check what type of image this is. */
455                 if (images->legacy_hdr_valid) {
456                         if (image_get_type(&images->legacy_hdr_os_copy)
457                                         == IH_TYPE_MULTI)
458                                 puts("WARNING: legacy format multi component image overwritten\n");
459                         return BOOTM_ERR_OVERLAP;
460                 } else {
461                         puts("ERROR: new format image overwritten - must RESET the board to recover\n");
462                         bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
463                         return BOOTM_ERR_RESET;
464                 }
465         }
466
467         lmb_reserve(&images->lmb, images->os.load, (load_end -
468                                                     images->os.load));
469         return 0;
470 }
471
472 /**
473  * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
474  *
475  * Return: interrupt flag (0 if interrupts were disabled, non-zero if they were
476  *      enabled)
477  */
478 ulong bootm_disable_interrupts(void)
479 {
480         ulong iflag;
481
482         /*
483          * We have reached the point of no return: we are going to
484          * overwrite all exception vector code, so we cannot easily
485          * recover from any failures any more...
486          */
487         iflag = disable_interrupts();
488 #ifdef CONFIG_NETCONSOLE
489         /* Stop the ethernet stack if NetConsole could have left it up */
490         eth_halt();
491 #endif
492
493 #if defined(CONFIG_CMD_USB)
494         /*
495          * turn off USB to prevent the host controller from writing to the
496          * SDRAM while Linux is booting. This could happen (at least for OHCI
497          * controller), because the HCCA (Host Controller Communication Area)
498          * lies within the SDRAM and the host controller writes continously to
499          * this area (as busmaster!). The HccaFrameNumber is for example
500          * updated every 1 ms within the HCCA structure in SDRAM! For more
501          * details see the OpenHCI specification.
502          */
503         usb_stop();
504 #endif
505         return iflag;
506 }
507
508 #define CONSOLE_ARG             "console="
509 #define NULL_CONSOLE            (CONSOLE_ARG "ttynull")
510 #define CONSOLE_ARG_SIZE        sizeof(NULL_CONSOLE)
511
512 /**
513  * fixup_silent_linux() - Handle silencing the linux boot if required
514  *
515  * This uses the silent_linux envvar to control whether to add/set a "console="
516  * parameter to the command line
517  *
518  * @buf: Buffer containing the string to process
519  * @maxlen: Maximum length of buffer
520  * Return: 0 if OK, -ENOSPC if @maxlen is too small
521  */
522 static int fixup_silent_linux(char *buf, int maxlen)
523 {
524         int want_silent;
525         char *cmdline;
526         int size;
527
528         /*
529          * Move the input string to the end of buffer. The output string will be
530          * built up at the start.
531          */
532         size = strlen(buf) + 1;
533         if (size * 2 > maxlen)
534                 return -ENOSPC;
535         cmdline = buf + maxlen - size;
536         memmove(cmdline, buf, size);
537         /*
538          * Only fix cmdline when requested. The environment variable can be:
539          *
540          *      no - we never fixup
541          *      yes - we always fixup
542          *      unset - we rely on the console silent flag
543          */
544         want_silent = env_get_yesno("silent_linux");
545         if (want_silent == 0)
546                 return 0;
547         else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
548                 return 0;
549
550         debug("before silent fix-up: %s\n", cmdline);
551         if (*cmdline) {
552                 char *start = strstr(cmdline, CONSOLE_ARG);
553
554                 /* Check space for maximum possible new command line */
555                 if (size + CONSOLE_ARG_SIZE > maxlen)
556                         return -ENOSPC;
557
558                 if (start) {
559                         char *end = strchr(start, ' ');
560                         int start_bytes;
561
562                         start_bytes = start - cmdline;
563                         strncpy(buf, cmdline, start_bytes);
564                         strncpy(buf + start_bytes, NULL_CONSOLE, CONSOLE_ARG_SIZE);
565                         if (end)
566                                 strcpy(buf + start_bytes + CONSOLE_ARG_SIZE - 1, end);
567                         else
568                                 buf[start_bytes + CONSOLE_ARG_SIZE] = '\0';
569                 } else {
570                         sprintf(buf, "%s %s", cmdline, NULL_CONSOLE);
571                 }
572                 if (buf + strlen(buf) >= cmdline)
573                         return -ENOSPC;
574         } else {
575                 if (maxlen < CONSOLE_ARG_SIZE)
576                         return -ENOSPC;
577                 strcpy(buf, NULL_CONSOLE);
578         }
579         debug("after silent fix-up: %s\n", buf);
580
581         return 0;
582 }
583
584 /**
585  * process_subst() - Handle substitution of ${...} fields in the environment
586  *
587  * Handle variable substitution in the provided buffer
588  *
589  * @buf: Buffer containing the string to process
590  * @maxlen: Maximum length of buffer
591  * Return: 0 if OK, -ENOSPC if @maxlen is too small
592  */
593 static int process_subst(char *buf, int maxlen)
594 {
595         char *cmdline;
596         int size;
597         int ret;
598
599         /* Move to end of buffer */
600         size = strlen(buf) + 1;
601         cmdline = buf + maxlen - size;
602         if (buf + size > cmdline)
603                 return -ENOSPC;
604         memmove(cmdline, buf, size);
605
606         ret = cli_simple_process_macros(cmdline, buf, cmdline - buf);
607
608         return ret;
609 }
610
611 int bootm_process_cmdline(char *buf, int maxlen, int flags)
612 {
613         int ret;
614
615         /* Check config first to enable compiler to eliminate code */
616         if (IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
617             !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) &&
618             (flags & BOOTM_CL_SILENT)) {
619                 ret = fixup_silent_linux(buf, maxlen);
620                 if (ret)
621                         return log_msg_ret("silent", ret);
622         }
623         if (IS_ENABLED(CONFIG_BOOTARGS_SUBST) && IS_ENABLED(CONFIG_CMDLINE) &&
624             (flags & BOOTM_CL_SUBST)) {
625                 ret = process_subst(buf, maxlen);
626                 if (ret)
627                         return log_msg_ret("subst", ret);
628         }
629
630         return 0;
631 }
632
633 int bootm_process_cmdline_env(int flags)
634 {
635         const int maxlen = MAX_CMDLINE_SIZE;
636         bool do_silent;
637         const char *env;
638         char *buf;
639         int ret;
640
641         /* First check if any action is needed */
642         do_silent = IS_ENABLED(CONFIG_SILENT_CONSOLE) &&
643             !IS_ENABLED(CONFIG_SILENT_U_BOOT_ONLY) && (flags & BOOTM_CL_SILENT);
644         if (!do_silent && !IS_ENABLED(CONFIG_BOOTARGS_SUBST))
645                 return 0;
646
647         env = env_get("bootargs");
648         if (env && strlen(env) >= maxlen)
649                 return -E2BIG;
650         buf = malloc(maxlen);
651         if (!buf)
652                 return -ENOMEM;
653         if (env)
654                 strcpy(buf, env);
655         else
656                 *buf = '\0';
657         ret = bootm_process_cmdline(buf, maxlen, flags);
658         if (!ret) {
659                 ret = env_set("bootargs", buf);
660
661                 /*
662                  * If buf is "" and bootargs does not exist, this will produce
663                  * an error trying to delete bootargs. Ignore it
664                  */
665                 if (ret == -ENOENT)
666                         ret = 0;
667         }
668         free(buf);
669         if (ret)
670                 return log_msg_ret("env", ret);
671
672         return 0;
673 }
674
675 /**
676  * Execute selected states of the bootm command.
677  *
678  * Note the arguments to this state must be the first argument, Any 'bootm'
679  * or sub-command arguments must have already been taken.
680  *
681  * Note that if states contains more than one flag it MUST contain
682  * BOOTM_STATE_START, since this handles and consumes the command line args.
683  *
684  * Also note that aside from boot_os_fn functions and bootm_load_os no other
685  * functions we store the return value of in 'ret' may use a negative return
686  * value, without special handling.
687  *
688  * @param cmdtp         Pointer to bootm command table entry
689  * @param flag          Command flags (CMD_FLAG_...)
690  * @param argc          Number of subcommand arguments (0 = no arguments)
691  * @param argv          Arguments
692  * @param states        Mask containing states to run (BOOTM_STATE_...)
693  * @param images        Image header information
694  * @param boot_progress 1 to show boot progress, 0 to not do this
695  * Return: 0 if ok, something else on error. Some errors will cause this
696  *      function to perform a reboot! If states contains BOOTM_STATE_OS_GO
697  *      then the intent is to boot an OS, so this function will not return
698  *      unless the image type is standalone.
699  */
700 int do_bootm_states(struct cmd_tbl *cmdtp, int flag, int argc,
701                     char *const argv[], int states, struct bootm_headers *images,
702                     int boot_progress)
703 {
704         boot_os_fn *boot_fn;
705         ulong iflag = 0;
706         int ret = 0, need_boot_fn;
707
708         images->state |= states;
709
710         /*
711          * Work through the states and see how far we get. We stop on
712          * any error.
713          */
714         if (states & BOOTM_STATE_START)
715                 ret = bootm_start(cmdtp, flag, argc, argv);
716
717         if (!ret && (states & BOOTM_STATE_PRE_LOAD))
718                 ret = bootm_pre_load(cmdtp, flag, argc, argv);
719
720         if (!ret && (states & BOOTM_STATE_FINDOS))
721                 ret = bootm_find_os(cmdtp, flag, argc, argv);
722
723         if (!ret && (states & BOOTM_STATE_FINDOTHER))
724                 ret = bootm_find_other(cmdtp, flag, argc, argv);
725
726         /* Load the OS */
727         if (!ret && (states & BOOTM_STATE_LOADOS)) {
728                 iflag = bootm_disable_interrupts();
729                 ret = bootm_load_os(images, 0);
730                 if (ret && ret != BOOTM_ERR_OVERLAP)
731                         goto err;
732                 else if (ret == BOOTM_ERR_OVERLAP)
733                         ret = 0;
734         }
735
736         /* Relocate the ramdisk */
737 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
738         if (!ret && (states & BOOTM_STATE_RAMDISK)) {
739                 ulong rd_len = images->rd_end - images->rd_start;
740
741                 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
742                         rd_len, &images->initrd_start, &images->initrd_end);
743                 if (!ret) {
744                         env_set_hex("initrd_start", images->initrd_start);
745                         env_set_hex("initrd_end", images->initrd_end);
746                 }
747         }
748 #endif
749 #if CONFIG_IS_ENABLED(OF_LIBFDT) && defined(CONFIG_LMB)
750         if (!ret && (states & BOOTM_STATE_FDT)) {
751                 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
752                 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
753                                         &images->ft_len);
754         }
755 #endif
756
757         /* From now on, we need the OS boot function */
758         if (ret)
759                 return ret;
760         boot_fn = bootm_os_get_boot_func(images->os.os);
761         need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
762                         BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
763                         BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
764         if (boot_fn == NULL && need_boot_fn) {
765                 if (iflag)
766                         enable_interrupts();
767                 printf("ERROR: booting os '%s' (%d) is not supported\n",
768                        genimg_get_os_name(images->os.os), images->os.os);
769                 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
770                 return 1;
771         }
772
773
774         /* Call various other states that are not generally used */
775         if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
776                 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
777         if (!ret && (states & BOOTM_STATE_OS_BD_T))
778                 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
779         if (!ret && (states & BOOTM_STATE_OS_PREP)) {
780                 ret = bootm_process_cmdline_env(images->os.os == IH_OS_LINUX);
781                 if (ret) {
782                         printf("Cmdline setup failed (err=%d)\n", ret);
783                         ret = CMD_RET_FAILURE;
784                         goto err;
785                 }
786                 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
787         }
788
789 #ifdef CONFIG_TRACE
790         /* Pretend to run the OS, then run a user command */
791         if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
792                 char *cmd_list = env_get("fakegocmd");
793
794                 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
795                                 images, boot_fn);
796                 if (!ret && cmd_list)
797                         ret = run_command_list(cmd_list, -1, flag);
798         }
799 #endif
800
801         /* Check for unsupported subcommand. */
802         if (ret) {
803                 printf("subcommand failed (err=%d)\n", ret);
804                 return ret;
805         }
806
807         /* Now run the OS! We hope this doesn't return */
808         if (!ret && (states & BOOTM_STATE_OS_GO))
809                 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
810                                 images, boot_fn);
811
812         /* Deal with any fallout */
813 err:
814         if (iflag)
815                 enable_interrupts();
816
817         if (ret == BOOTM_ERR_UNIMPLEMENTED)
818                 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
819         else if (ret == BOOTM_ERR_RESET)
820                 do_reset(cmdtp, flag, argc, argv);
821
822         return ret;
823 }
824
825 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
826 /**
827  * image_get_kernel - verify legacy format kernel image
828  * @img_addr: in RAM address of the legacy format image to be verified
829  * @verify: data CRC verification flag
830  *
831  * image_get_kernel() verifies legacy image integrity and returns pointer to
832  * legacy image header if image verification was completed successfully.
833  *
834  * returns:
835  *     pointer to a legacy image header if valid image was found
836  *     otherwise return NULL
837  */
838 static struct legacy_img_hdr *image_get_kernel(ulong img_addr, int verify)
839 {
840         struct legacy_img_hdr *hdr = (struct legacy_img_hdr *)img_addr;
841
842         if (!image_check_magic(hdr)) {
843                 puts("Bad Magic Number\n");
844                 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
845                 return NULL;
846         }
847         bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
848
849         if (!image_check_hcrc(hdr)) {
850                 puts("Bad Header Checksum\n");
851                 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
852                 return NULL;
853         }
854
855         bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
856         image_print_contents(hdr);
857
858         if (verify) {
859                 puts("   Verifying Checksum ... ");
860                 if (!image_check_dcrc(hdr)) {
861                         printf("Bad Data CRC\n");
862                         bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
863                         return NULL;
864                 }
865                 puts("OK\n");
866         }
867         bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
868
869         if (!image_check_target_arch(hdr)) {
870                 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
871                 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
872                 return NULL;
873         }
874         return hdr;
875 }
876 #endif
877
878 /**
879  * boot_get_kernel - find kernel image
880  * @os_data: pointer to a ulong variable, will hold os data start address
881  * @os_len: pointer to a ulong variable, will hold os data length
882  *
883  * boot_get_kernel() tries to find a kernel image, verifies its integrity
884  * and locates kernel data.
885  *
886  * returns:
887  *     pointer to image header if valid image was found, plus kernel start
888  *     address and length, otherwise NULL
889  */
890 static const void *boot_get_kernel(struct cmd_tbl *cmdtp, int flag, int argc,
891                                    char *const argv[], struct bootm_headers *images,
892                                    ulong *os_data, ulong *os_len)
893 {
894 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
895         struct legacy_img_hdr   *hdr;
896 #endif
897         ulong           img_addr;
898         const void *buf;
899         const char      *fit_uname_config = NULL;
900         const char      *fit_uname_kernel = NULL;
901 #if CONFIG_IS_ENABLED(FIT)
902         int             os_noffset;
903 #endif
904
905 #ifdef CONFIG_ANDROID_BOOT_IMAGE
906         const void *boot_img;
907         const void *vendor_boot_img;
908 #endif
909         img_addr = genimg_get_kernel_addr_fit(argc < 1 ? NULL : argv[0],
910                                               &fit_uname_config,
911                                               &fit_uname_kernel);
912
913         if (IS_ENABLED(CONFIG_CMD_BOOTM_PRE_LOAD))
914                 img_addr += image_load_offset;
915
916         bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
917
918         /* check image type, for FIT images get FIT kernel node */
919         *os_data = *os_len = 0;
920         buf = map_sysmem(img_addr, 0);
921         switch (genimg_get_format(buf)) {
922 #if CONFIG_IS_ENABLED(LEGACY_IMAGE_FORMAT)
923         case IMAGE_FORMAT_LEGACY:
924                 printf("## Booting kernel from Legacy Image at %08lx ...\n",
925                        img_addr);
926                 hdr = image_get_kernel(img_addr, images->verify);
927                 if (!hdr)
928                         return NULL;
929                 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
930
931                 /* get os_data and os_len */
932                 switch (image_get_type(hdr)) {
933                 case IH_TYPE_KERNEL:
934                 case IH_TYPE_KERNEL_NOLOAD:
935                         *os_data = image_get_data(hdr);
936                         *os_len = image_get_data_size(hdr);
937                         break;
938                 case IH_TYPE_MULTI:
939                         image_multi_getimg(hdr, 0, os_data, os_len);
940                         break;
941                 case IH_TYPE_STANDALONE:
942                         *os_data = image_get_data(hdr);
943                         *os_len = image_get_data_size(hdr);
944                         break;
945                 default:
946                         printf("Wrong Image Type for %s command\n",
947                                cmdtp->name);
948                         bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
949                         return NULL;
950                 }
951
952                 /*
953                  * copy image header to allow for image overwrites during
954                  * kernel decompression.
955                  */
956                 memmove(&images->legacy_hdr_os_copy, hdr,
957                         sizeof(struct legacy_img_hdr));
958
959                 /* save pointer to image header */
960                 images->legacy_hdr_os = hdr;
961
962                 images->legacy_hdr_valid = 1;
963                 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
964                 break;
965 #endif
966 #if CONFIG_IS_ENABLED(FIT)
967         case IMAGE_FORMAT_FIT:
968                 os_noffset = fit_image_load(images, img_addr,
969                                 &fit_uname_kernel, &fit_uname_config,
970                                 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
971                                 BOOTSTAGE_ID_FIT_KERNEL_START,
972                                 FIT_LOAD_IGNORED, os_data, os_len);
973                 if (os_noffset < 0)
974                         return NULL;
975
976                 images->fit_hdr_os = map_sysmem(img_addr, 0);
977                 images->fit_uname_os = fit_uname_kernel;
978                 images->fit_uname_cfg = fit_uname_config;
979                 images->fit_noffset_os = os_noffset;
980                 break;
981 #endif
982 #ifdef CONFIG_ANDROID_BOOT_IMAGE
983         case IMAGE_FORMAT_ANDROID:
984                 boot_img = buf;
985                 vendor_boot_img = NULL;
986                 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
987                         boot_img = map_sysmem(get_abootimg_addr(), 0);
988                         vendor_boot_img = map_sysmem(get_avendor_bootimg_addr(), 0);
989                 }
990                 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
991                 if (android_image_get_kernel(boot_img, vendor_boot_img, images->verify,
992                                              os_data, os_len))
993                         return NULL;
994                 if (IS_ENABLED(CONFIG_CMD_ABOOTIMG)) {
995                         unmap_sysmem(vendor_boot_img);
996                         unmap_sysmem(boot_img);
997                 }
998                 break;
999 #endif
1000         default:
1001                 printf("Wrong Image Format for %s command\n", cmdtp->name);
1002                 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
1003                 return NULL;
1004         }
1005
1006         debug("   kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
1007               *os_data, *os_len, *os_len);
1008
1009         return buf;
1010 }
1011
1012 /**
1013  * switch_to_non_secure_mode() - switch to non-secure mode
1014  *
1015  * This routine is overridden by architectures requiring this feature.
1016  */
1017 void __weak switch_to_non_secure_mode(void)
1018 {
1019 }
1020
1021 #else /* USE_HOSTCC */
1022
1023 #if defined(CONFIG_FIT_SIGNATURE)
1024 static int bootm_host_load_image(const void *fit, int req_image_type,
1025                                  int cfg_noffset)
1026 {
1027         const char *fit_uname_config = NULL;
1028         ulong data, len;
1029         struct bootm_headers images;
1030         int noffset;
1031         ulong load_end, buf_size;
1032         uint8_t image_type;
1033         uint8_t image_comp;
1034         void *load_buf;
1035         int ret;
1036
1037         fit_uname_config = fdt_get_name(fit, cfg_noffset, NULL);
1038         memset(&images, '\0', sizeof(images));
1039         images.verify = 1;
1040         noffset = fit_image_load(&images, (ulong)fit,
1041                 NULL, &fit_uname_config,
1042                 IH_ARCH_DEFAULT, req_image_type, -1,
1043                 FIT_LOAD_IGNORED, &data, &len);
1044         if (noffset < 0)
1045                 return noffset;
1046         if (fit_image_get_type(fit, noffset, &image_type)) {
1047                 puts("Can't get image type!\n");
1048                 return -EINVAL;
1049         }
1050
1051         if (fit_image_get_comp(fit, noffset, &image_comp))
1052                 image_comp = IH_COMP_NONE;
1053
1054         /* Allow the image to expand by a factor of 4, should be safe */
1055         buf_size = (1 << 20) + len * 4;
1056         load_buf = malloc(buf_size);
1057         ret = image_decomp(image_comp, 0, data, image_type, load_buf,
1058                            (void *)data, len, buf_size, &load_end);
1059         free(load_buf);
1060
1061         if (ret) {
1062                 ret = handle_decomp_error(image_comp, load_end - 0, buf_size, ret);
1063                 if (ret != BOOTM_ERR_UNIMPLEMENTED)
1064                         return ret;
1065         }
1066
1067         return 0;
1068 }
1069
1070 int bootm_host_load_images(const void *fit, int cfg_noffset)
1071 {
1072         static uint8_t image_types[] = {
1073                 IH_TYPE_KERNEL,
1074                 IH_TYPE_FLATDT,
1075                 IH_TYPE_RAMDISK,
1076         };
1077         int err = 0;
1078         int i;
1079
1080         for (i = 0; i < ARRAY_SIZE(image_types); i++) {
1081                 int ret;
1082
1083                 ret = bootm_host_load_image(fit, image_types[i], cfg_noffset);
1084                 if (!err && ret && ret != -ENOENT)
1085                         err = ret;
1086         }
1087
1088         /* Return the first error we found */
1089         return err;
1090 }
1091 #endif
1092
1093 #endif /* ndef USE_HOSTCC */