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