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