]> git.kernelconcepts.de Git - karo-tx-uboot.git/blob - common/bootm.c
Allow compiling common/bootm.c on with HOSTCC
[karo-tx-uboot.git] / common / bootm.c
1 /*
2  * (C) Copyright 2000-2009
3  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4  *
5  * SPDX-License-Identifier:     GPL-2.0+
6  */
7
8 #ifndef USE_HOSTCC
9 #include <common.h>
10 #include <bootstage.h>
11 #include <bzlib.h>
12 #include <fdt_support.h>
13 #include <lmb.h>
14 #include <malloc.h>
15 #include <asm/io.h>
16 #include <linux/lzo.h>
17 #include <lzma/LzmaTypes.h>
18 #include <lzma/LzmaDec.h>
19 #include <lzma/LzmaTools.h>
20 #if defined(CONFIG_CMD_USB)
21 #include <usb.h>
22 #endif
23 #else
24 #include "mkimage.h"
25 #endif
26
27 #include <command.h>
28 #include <bootm.h>
29 #include <image.h>
30
31 #ifndef CONFIG_SYS_BOOTM_LEN
32 /* use 8MByte as default max gunzip size */
33 #define CONFIG_SYS_BOOTM_LEN    0x800000
34 #endif
35
36 #define IH_INITRD_ARCH IH_ARCH_DEFAULT
37
38 #ifndef USE_HOSTCC
39
40 DECLARE_GLOBAL_DATA_PTR;
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 #ifdef CONFIG_LMB
47 static void boot_start_lmb(bootm_headers_t *images)
48 {
49         ulong           mem_start;
50         phys_size_t     mem_size;
51
52         lmb_init(&images->lmb);
53
54         mem_start = getenv_bootm_low();
55         mem_size = getenv_bootm_size();
56
57         lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size);
58
59         arch_lmb_reserve(&images->lmb);
60         board_lmb_reserve(&images->lmb);
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 = getenv_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
87         /* get kernel image header, start address and length */
88         os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
89                         &images, &images.os.image_start, &images.os.image_len);
90         if (images.os.image_len == 0) {
91                 puts("ERROR: can't get kernel image!\n");
92                 return 1;
93         }
94
95         /* get image parameters */
96         switch (genimg_get_format(os_hdr)) {
97 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
98         case IMAGE_FORMAT_LEGACY:
99                 images.os.type = image_get_type(os_hdr);
100                 images.os.comp = image_get_comp(os_hdr);
101                 images.os.os = image_get_os(os_hdr);
102
103                 images.os.end = image_get_image_end(os_hdr);
104                 images.os.load = image_get_load(os_hdr);
105                 break;
106 #endif
107 #if defined(CONFIG_FIT)
108         case IMAGE_FORMAT_FIT:
109                 if (fit_image_get_type(images.fit_hdr_os,
110                                        images.fit_noffset_os,
111                                        &images.os.type)) {
112                         puts("Can't get image type!\n");
113                         bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
114                         return 1;
115                 }
116
117                 if (fit_image_get_comp(images.fit_hdr_os,
118                                        images.fit_noffset_os,
119                                        &images.os.comp)) {
120                         puts("Can't get image compression!\n");
121                         bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
122                         return 1;
123                 }
124
125                 if (fit_image_get_os(images.fit_hdr_os, images.fit_noffset_os,
126                                      &images.os.os)) {
127                         puts("Can't get image OS!\n");
128                         bootstage_error(BOOTSTAGE_ID_FIT_OS);
129                         return 1;
130                 }
131
132                 images.os.end = fit_get_end(images.fit_hdr_os);
133
134                 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
135                                        &images.os.load)) {
136                         puts("Can't get image load address!\n");
137                         bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
138                         return 1;
139                 }
140                 break;
141 #endif
142 #ifdef CONFIG_ANDROID_BOOT_IMAGE
143         case IMAGE_FORMAT_ANDROID:
144                 images.os.type = IH_TYPE_KERNEL;
145                 images.os.comp = IH_COMP_NONE;
146                 images.os.os = IH_OS_LINUX;
147                 images.ep = images.os.load;
148                 ep_found = true;
149
150                 images.os.end = android_image_get_end(os_hdr);
151                 images.os.load = android_image_get_kload(os_hdr);
152                 break;
153 #endif
154         default:
155                 puts("ERROR: unknown image format type!\n");
156                 return 1;
157         }
158
159         /* find kernel entry point */
160         if (images.legacy_hdr_valid) {
161                 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
162 #if defined(CONFIG_FIT)
163         } else if (images.fit_uname_os) {
164                 int ret;
165
166                 ret = fit_image_get_entry(images.fit_hdr_os,
167                                           images.fit_noffset_os, &images.ep);
168                 if (ret) {
169                         puts("Can't get entry point property!\n");
170                         return 1;
171                 }
172 #endif
173         } else if (!ep_found) {
174                 puts("Could not find kernel entry point!\n");
175                 return 1;
176         }
177
178         if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
179                 images.os.load = images.os.image_start;
180                 images.ep += images.os.load;
181         }
182
183         images.os.start = (ulong)os_hdr;
184
185         return 0;
186 }
187
188 static int bootm_find_ramdisk(int flag, int argc, char * const argv[])
189 {
190         int ret;
191
192         /* find ramdisk */
193         ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
194                                &images.rd_start, &images.rd_end);
195         if (ret) {
196                 puts("Ramdisk image is corrupt or invalid\n");
197                 return 1;
198         }
199
200         return 0;
201 }
202
203 #if defined(CONFIG_OF_LIBFDT)
204 static int bootm_find_fdt(int flag, int argc, char * const argv[])
205 {
206         int ret;
207
208         /* find flattened device tree */
209         ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
210                            &images.ft_addr, &images.ft_len);
211         if (ret) {
212                 puts("Could not find a valid device tree\n");
213                 return 1;
214         }
215
216         set_working_fdt_addr(images.ft_addr);
217
218         return 0;
219 }
220 #endif
221
222 int bootm_find_ramdisk_fdt(int flag, int argc, char * const argv[])
223 {
224         if (bootm_find_ramdisk(flag, argc, argv))
225                 return 1;
226
227 #if defined(CONFIG_OF_LIBFDT)
228         if (bootm_find_fdt(flag, argc, argv))
229                 return 1;
230 #endif
231
232         return 0;
233 }
234
235 static int bootm_find_other(cmd_tbl_t *cmdtp, int flag, int argc,
236                             char * const argv[])
237 {
238         if (((images.os.type == IH_TYPE_KERNEL) ||
239              (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
240              (images.os.type == IH_TYPE_MULTI)) &&
241             (images.os.os == IH_OS_LINUX ||
242                  images.os.os == IH_OS_VXWORKS))
243                 return bootm_find_ramdisk_fdt(flag, argc, argv);
244
245         return 0;
246 }
247
248 static int bootm_load_os(bootm_headers_t *images, unsigned long *load_end,
249                          int boot_progress)
250 {
251         image_info_t os = images->os;
252         uint8_t comp = os.comp;
253         ulong load = os.load;
254         ulong blob_start = os.start;
255         ulong blob_end = os.end;
256         ulong image_start = os.image_start;
257         ulong image_len = os.image_len;
258         __maybe_unused uint unc_len = CONFIG_SYS_BOOTM_LEN;
259         int no_overlap = 0;
260         void *load_buf, *image_buf;
261 #if defined(CONFIG_LZMA) || defined(CONFIG_LZO)
262         int ret;
263 #endif /* defined(CONFIG_LZMA) || defined(CONFIG_LZO) */
264
265         const char *type_name = genimg_get_type_name(os.type);
266
267         load_buf = map_sysmem(load, unc_len);
268         image_buf = map_sysmem(image_start, image_len);
269         switch (comp) {
270         case IH_COMP_NONE:
271                 if (load == image_start) {
272                         printf("   XIP %s ... ", type_name);
273                         no_overlap = 1;
274                 } else {
275                         printf("   Loading %s ... ", type_name);
276                         memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
277                 }
278                 *load_end = load + image_len;
279                 break;
280 #ifdef CONFIG_GZIP
281         case IH_COMP_GZIP:
282                 printf("   Uncompressing %s ... ", type_name);
283                 if (gunzip(load_buf, unc_len, image_buf, &image_len) != 0) {
284                         puts("GUNZIP: uncompress, out-of-mem or overwrite error - must RESET board to recover\n");
285                         if (boot_progress)
286                                 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
287                         return BOOTM_ERR_RESET;
288                 }
289
290                 *load_end = load + image_len;
291                 break;
292 #endif /* CONFIG_GZIP */
293 #ifdef CONFIG_BZIP2
294         case IH_COMP_BZIP2:
295                 printf("   Uncompressing %s ... ", type_name);
296                 /*
297                  * If we've got less than 4 MB of malloc() space,
298                  * use slower decompression algorithm which requires
299                  * at most 2300 KB of memory.
300                  */
301                 int i = BZ2_bzBuffToBuffDecompress(load_buf, &unc_len,
302                         image_buf, image_len,
303                         CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
304                 if (i != BZ_OK) {
305                         printf("BUNZIP2: uncompress or overwrite error %d - must RESET board to recover\n",
306                                i);
307                         if (boot_progress)
308                                 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
309                         return BOOTM_ERR_RESET;
310                 }
311
312                 *load_end = load + unc_len;
313                 break;
314 #endif /* CONFIG_BZIP2 */
315 #ifdef CONFIG_LZMA
316         case IH_COMP_LZMA: {
317                 SizeT lzma_len = unc_len;
318                 printf("   Uncompressing %s ... ", type_name);
319
320                 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
321                                                image_buf, image_len);
322                 unc_len = lzma_len;
323                 if (ret != SZ_OK) {
324                         printf("LZMA: uncompress or overwrite error %d - must RESET board to recover\n",
325                                ret);
326                         bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
327                         return BOOTM_ERR_RESET;
328                 }
329                 *load_end = load + unc_len;
330                 break;
331         }
332 #endif /* CONFIG_LZMA */
333 #ifdef CONFIG_LZO
334         case IH_COMP_LZO: {
335                 size_t size = unc_len;
336
337                 printf("   Uncompressing %s ... ", type_name);
338
339                 ret = lzop_decompress(image_buf, image_len, load_buf, &size);
340                 if (ret != LZO_E_OK) {
341                         printf("LZO: uncompress or overwrite error %d - must RESET board to recover\n",
342                                ret);
343                         if (boot_progress)
344                                 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
345                         return BOOTM_ERR_RESET;
346                 }
347
348                 *load_end = load + size;
349                 break;
350         }
351 #endif /* CONFIG_LZO */
352         default:
353                 printf("Unimplemented compression type %d\n", comp);
354                 return BOOTM_ERR_UNIMPLEMENTED;
355         }
356
357         flush_cache(load, (*load_end - load) * sizeof(ulong));
358
359         puts("OK\n");
360         debug("   kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
361         bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
362
363         if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
364                 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
365                       blob_start, blob_end);
366                 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
367                       *load_end);
368
369                 /* Check what type of image this is. */
370                 if (images->legacy_hdr_valid) {
371                         if (image_get_type(&images->legacy_hdr_os_copy)
372                                         == IH_TYPE_MULTI)
373                                 puts("WARNING: legacy format multi component image overwritten\n");
374                         return BOOTM_ERR_OVERLAP;
375                 } else {
376                         puts("ERROR: new format image overwritten - must RESET the board to recover\n");
377                         bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
378                         return BOOTM_ERR_RESET;
379                 }
380         }
381
382         return 0;
383 }
384
385 /**
386  * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
387  *
388  * @return interrupt flag (0 if interrupts were disabled, non-zero if they were
389  *      enabled)
390  */
391 ulong bootm_disable_interrupts(void)
392 {
393         ulong iflag;
394
395         /*
396          * We have reached the point of no return: we are going to
397          * overwrite all exception vector code, so we cannot easily
398          * recover from any failures any more...
399          */
400         iflag = disable_interrupts();
401 #ifdef CONFIG_NETCONSOLE
402         /* Stop the ethernet stack if NetConsole could have left it up */
403         eth_halt();
404         eth_unregister(eth_get_dev());
405 #endif
406
407 #if defined(CONFIG_CMD_USB)
408         /*
409          * turn off USB to prevent the host controller from writing to the
410          * SDRAM while Linux is booting. This could happen (at least for OHCI
411          * controller), because the HCCA (Host Controller Communication Area)
412          * lies within the SDRAM and the host controller writes continously to
413          * this area (as busmaster!). The HccaFrameNumber is for example
414          * updated every 1 ms within the HCCA structure in SDRAM! For more
415          * details see the OpenHCI specification.
416          */
417         usb_stop();
418 #endif
419         return iflag;
420 }
421
422 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
423
424 #define CONSOLE_ARG     "console="
425 #define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
426
427 static void fixup_silent_linux(void)
428 {
429         char *buf;
430         const char *env_val;
431         char *cmdline = getenv("bootargs");
432         int want_silent;
433
434         /*
435          * Only fix cmdline when requested. The environment variable can be:
436          *
437          *      no - we never fixup
438          *      yes - we always fixup
439          *      unset - we rely on the console silent flag
440          */
441         want_silent = getenv_yesno("silent_linux");
442         if (want_silent == 0)
443                 return;
444         else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
445                 return;
446
447         debug("before silent fix-up: %s\n", cmdline);
448         if (cmdline && (cmdline[0] != '\0')) {
449                 char *start = strstr(cmdline, CONSOLE_ARG);
450
451                 /* Allocate space for maximum possible new command line */
452                 buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1);
453                 if (!buf) {
454                         debug("%s: out of memory\n", __func__);
455                         return;
456                 }
457
458                 if (start) {
459                         char *end = strchr(start, ' ');
460                         int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN;
461
462                         strncpy(buf, cmdline, num_start_bytes);
463                         if (end)
464                                 strcpy(buf + num_start_bytes, end);
465                         else
466                                 buf[num_start_bytes] = '\0';
467                 } else {
468                         sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
469                 }
470                 env_val = buf;
471         } else {
472                 buf = NULL;
473                 env_val = CONSOLE_ARG;
474         }
475
476         setenv("bootargs", env_val);
477         debug("after silent fix-up: %s\n", env_val);
478         free(buf);
479 }
480 #endif /* CONFIG_SILENT_CONSOLE */
481
482 /**
483  * Execute selected states of the bootm command.
484  *
485  * Note the arguments to this state must be the first argument, Any 'bootm'
486  * or sub-command arguments must have already been taken.
487  *
488  * Note that if states contains more than one flag it MUST contain
489  * BOOTM_STATE_START, since this handles and consumes the command line args.
490  *
491  * Also note that aside from boot_os_fn functions and bootm_load_os no other
492  * functions we store the return value of in 'ret' may use a negative return
493  * value, without special handling.
494  *
495  * @param cmdtp         Pointer to bootm command table entry
496  * @param flag          Command flags (CMD_FLAG_...)
497  * @param argc          Number of subcommand arguments (0 = no arguments)
498  * @param argv          Arguments
499  * @param states        Mask containing states to run (BOOTM_STATE_...)
500  * @param images        Image header information
501  * @param boot_progress 1 to show boot progress, 0 to not do this
502  * @return 0 if ok, something else on error. Some errors will cause this
503  *      function to perform a reboot! If states contains BOOTM_STATE_OS_GO
504  *      then the intent is to boot an OS, so this function will not return
505  *      unless the image type is standalone.
506  */
507 int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
508                     int states, bootm_headers_t *images, int boot_progress)
509 {
510         boot_os_fn *boot_fn;
511         ulong iflag = 0;
512         int ret = 0, need_boot_fn;
513
514         images->state |= states;
515
516         /*
517          * Work through the states and see how far we get. We stop on
518          * any error.
519          */
520         if (states & BOOTM_STATE_START)
521                 ret = bootm_start(cmdtp, flag, argc, argv);
522
523         if (!ret && (states & BOOTM_STATE_FINDOS))
524                 ret = bootm_find_os(cmdtp, flag, argc, argv);
525
526         if (!ret && (states & BOOTM_STATE_FINDOTHER)) {
527                 ret = bootm_find_other(cmdtp, flag, argc, argv);
528                 argc = 0;       /* consume the args */
529         }
530
531         /* Load the OS */
532         if (!ret && (states & BOOTM_STATE_LOADOS)) {
533                 ulong load_end;
534
535                 iflag = bootm_disable_interrupts();
536                 ret = bootm_load_os(images, &load_end, 0);
537                 if (ret == 0)
538                         lmb_reserve(&images->lmb, images->os.load,
539                                     (load_end - images->os.load));
540                 else if (ret && ret != BOOTM_ERR_OVERLAP)
541                         goto err;
542                 else if (ret == BOOTM_ERR_OVERLAP)
543                         ret = 0;
544 #if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
545                 if (images->os.os == IH_OS_LINUX)
546                         fixup_silent_linux();
547 #endif
548         }
549
550         /* Relocate the ramdisk */
551 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
552         if (!ret && (states & BOOTM_STATE_RAMDISK)) {
553                 ulong rd_len = images->rd_end - images->rd_start;
554
555                 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
556                         rd_len, &images->initrd_start, &images->initrd_end);
557                 if (!ret) {
558                         setenv_hex("initrd_start", images->initrd_start);
559                         setenv_hex("initrd_end", images->initrd_end);
560                 }
561         }
562 #endif
563 #if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_LMB)
564         if (!ret && (states & BOOTM_STATE_FDT)) {
565                 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
566                 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
567                                         &images->ft_len);
568         }
569 #endif
570
571         /* From now on, we need the OS boot function */
572         if (ret)
573                 return ret;
574         boot_fn = bootm_os_get_boot_func(images->os.os);
575         need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
576                         BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
577                         BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
578         if (boot_fn == NULL && need_boot_fn) {
579                 if (iflag)
580                         enable_interrupts();
581                 printf("ERROR: booting os '%s' (%d) is not supported\n",
582                        genimg_get_os_name(images->os.os), images->os.os);
583                 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
584                 return 1;
585         }
586
587         /* Call various other states that are not generally used */
588         if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
589                 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
590         if (!ret && (states & BOOTM_STATE_OS_BD_T))
591                 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
592         if (!ret && (states & BOOTM_STATE_OS_PREP))
593                 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
594
595 #ifdef CONFIG_TRACE
596         /* Pretend to run the OS, then run a user command */
597         if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
598                 char *cmd_list = getenv("fakegocmd");
599
600                 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
601                                 images, boot_fn);
602                 if (!ret && cmd_list)
603                         ret = run_command_list(cmd_list, -1, flag);
604         }
605 #endif
606
607         /* Check for unsupported subcommand. */
608         if (ret) {
609                 puts("subcommand not supported\n");
610                 return ret;
611         }
612
613         /* Now run the OS! We hope this doesn't return */
614         if (!ret && (states & BOOTM_STATE_OS_GO))
615                 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
616                                 images, boot_fn);
617
618         /* Deal with any fallout */
619 err:
620         if (iflag)
621                 enable_interrupts();
622
623         if (ret == BOOTM_ERR_UNIMPLEMENTED)
624                 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
625         else if (ret == BOOTM_ERR_RESET)
626                 do_reset(cmdtp, flag, argc, argv);
627
628         return ret;
629 }
630
631 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
632 /**
633  * image_get_kernel - verify legacy format kernel image
634  * @img_addr: in RAM address of the legacy format image to be verified
635  * @verify: data CRC verification flag
636  *
637  * image_get_kernel() verifies legacy image integrity and returns pointer to
638  * legacy image header if image verification was completed successfully.
639  *
640  * returns:
641  *     pointer to a legacy image header if valid image was found
642  *     otherwise return NULL
643  */
644 static image_header_t *image_get_kernel(ulong img_addr, int verify)
645 {
646         image_header_t *hdr = (image_header_t *)img_addr;
647
648         if (!image_check_magic(hdr)) {
649                 puts("Bad Magic Number\n");
650                 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
651                 return NULL;
652         }
653         bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
654
655         if (!image_check_hcrc(hdr)) {
656                 puts("Bad Header Checksum\n");
657                 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
658                 return NULL;
659         }
660
661         bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
662         image_print_contents(hdr);
663
664         if (verify) {
665                 puts("   Verifying Checksum ... ");
666                 if (!image_check_dcrc(hdr)) {
667                         printf("Bad Data CRC\n");
668                         bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
669                         return NULL;
670                 }
671                 puts("OK\n");
672         }
673         bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
674
675         if (!image_check_target_arch(hdr)) {
676                 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
677                 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
678                 return NULL;
679         }
680         return hdr;
681 }
682 #endif
683
684 /**
685  * boot_get_kernel - find kernel image
686  * @os_data: pointer to a ulong variable, will hold os data start address
687  * @os_len: pointer to a ulong variable, will hold os data length
688  *
689  * boot_get_kernel() tries to find a kernel image, verifies its integrity
690  * and locates kernel data.
691  *
692  * returns:
693  *     pointer to image header if valid image was found, plus kernel start
694  *     address and length, otherwise NULL
695  */
696 static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
697                                    char * const argv[], bootm_headers_t *images,
698                                    ulong *os_data, ulong *os_len)
699 {
700 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
701         image_header_t  *hdr;
702 #endif
703         ulong           img_addr;
704         const void *buf;
705 #if defined(CONFIG_FIT)
706         const char      *fit_uname_config = NULL;
707         const char      *fit_uname_kernel = NULL;
708         int             os_noffset;
709 #endif
710
711         /* find out kernel image address */
712         if (argc < 1) {
713                 img_addr = load_addr;
714                 debug("*  kernel: default image load address = 0x%08lx\n",
715                       load_addr);
716 #if defined(CONFIG_FIT)
717         } else if (fit_parse_conf(argv[0], load_addr, &img_addr,
718                                   &fit_uname_config)) {
719                 debug("*  kernel: config '%s' from image at 0x%08lx\n",
720                       fit_uname_config, img_addr);
721         } else if (fit_parse_subimage(argv[0], load_addr, &img_addr,
722                                      &fit_uname_kernel)) {
723                 debug("*  kernel: subimage '%s' from image at 0x%08lx\n",
724                       fit_uname_kernel, img_addr);
725 #endif
726         } else {
727                 img_addr = simple_strtoul(argv[0], NULL, 16);
728                 debug("*  kernel: cmdline image address = 0x%08lx\n",
729                       img_addr);
730         }
731
732         bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
733
734         /* copy from dataflash if needed */
735         img_addr = genimg_get_image(img_addr);
736
737         /* check image type, for FIT images get FIT kernel node */
738         *os_data = *os_len = 0;
739         buf = map_sysmem(img_addr, 0);
740         switch (genimg_get_format(buf)) {
741 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
742         case IMAGE_FORMAT_LEGACY:
743                 printf("## Booting kernel from Legacy Image at %08lx ...\n",
744                        img_addr);
745                 hdr = image_get_kernel(img_addr, images->verify);
746                 if (!hdr)
747                         return NULL;
748                 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
749
750                 /* get os_data and os_len */
751                 switch (image_get_type(hdr)) {
752                 case IH_TYPE_KERNEL:
753                 case IH_TYPE_KERNEL_NOLOAD:
754                         *os_data = image_get_data(hdr);
755                         *os_len = image_get_data_size(hdr);
756                         break;
757                 case IH_TYPE_MULTI:
758                         image_multi_getimg(hdr, 0, os_data, os_len);
759                         break;
760                 case IH_TYPE_STANDALONE:
761                         *os_data = image_get_data(hdr);
762                         *os_len = image_get_data_size(hdr);
763                         break;
764                 default:
765                         printf("Wrong Image Type for %s command\n",
766                                cmdtp->name);
767                         bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
768                         return NULL;
769                 }
770
771                 /*
772                  * copy image header to allow for image overwrites during
773                  * kernel decompression.
774                  */
775                 memmove(&images->legacy_hdr_os_copy, hdr,
776                         sizeof(image_header_t));
777
778                 /* save pointer to image header */
779                 images->legacy_hdr_os = hdr;
780
781                 images->legacy_hdr_valid = 1;
782                 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
783                 break;
784 #endif
785 #if defined(CONFIG_FIT)
786         case IMAGE_FORMAT_FIT:
787                 os_noffset = fit_image_load(images, img_addr,
788                                 &fit_uname_kernel, &fit_uname_config,
789                                 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
790                                 BOOTSTAGE_ID_FIT_KERNEL_START,
791                                 FIT_LOAD_IGNORED, os_data, os_len);
792                 if (os_noffset < 0)
793                         return NULL;
794
795                 images->fit_hdr_os = map_sysmem(img_addr, 0);
796                 images->fit_uname_os = fit_uname_kernel;
797                 images->fit_uname_cfg = fit_uname_config;
798                 images->fit_noffset_os = os_noffset;
799                 break;
800 #endif
801 #ifdef CONFIG_ANDROID_BOOT_IMAGE
802         case IMAGE_FORMAT_ANDROID:
803                 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
804                 if (android_image_get_kernel(buf, images->verify,
805                                              os_data, os_len))
806                         return NULL;
807                 break;
808 #endif
809         default:
810                 printf("Wrong Image Format for %s command\n", cmdtp->name);
811                 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
812                 return NULL;
813         }
814
815         debug("   kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
816               *os_data, *os_len, *os_len);
817
818         return buf;
819 }
820
821 #endif /* ndef USE_HOSTCC */