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