blob: d93d3f3f104cd48da66649ef8201181a7a17e22d [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
Simon Glassea51a622014-06-12 07:24:51 -06008#ifndef USE_HOSTCC
Simon Glassb6396402014-06-12 07:24:46 -06009#include <common.h>
Simon Glassea51a622014-06-12 07:24:51 -060010#include <bootstage.h>
Simon Glassb6396402014-06-12 07:24:46 -060011#include <bzlib.h>
Simon Glassb6396402014-06-12 07:24:46 -060012#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>
Simon Glassb6396402014-06-12 07:24:46 -060020#if defined(CONFIG_CMD_USB)
21#include <usb.h>
22#endif
Simon Glassea51a622014-06-12 07:24:51 -060023#else
24#include "mkimage.h"
25#endif
Simon Glassb6396402014-06-12 07:24:46 -060026
Simon Glassea51a622014-06-12 07:24:51 -060027#include <command.h>
28#include <bootm.h>
29#include <image.h>
Simon Glassb6396402014-06-12 07:24:46 -060030
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
Simon Glassea51a622014-06-12 07:24:51 -060038#ifndef USE_HOSTCC
39
40DECLARE_GLOBAL_DATA_PTR;
41
Simon Glassb6396402014-06-12 07:24:46 -060042static 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
47static 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)
64static inline void boot_start_lmb(bootm_headers_t *images) { }
65#endif
66
67static 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
81static 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
188static 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)
204static 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
222int 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
235static 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
Simon Glass2b164f12014-06-12 07:24:52 -0600248/**
249 * decomp_image() - decompress the operating system
250 *
251 * @comp: Compression algorithm that is used (IH_COMP_...)
252 * @load: Destination load address in U-Boot memory
253 * @image_start Image start address (where we are decompressing from)
254 * @type: OS type (IH_OS_...)
255 * @load_bug: Place to decompress to
256 * @image_buf: Address to decompress from
257 * @return 0 if OK, -ve on error (BOOTM_ERR_...)
258 */
259static int decomp_image(int comp, ulong load, ulong image_start, int type,
260 void *load_buf, void *image_buf, ulong image_len,
261 ulong *load_end)
Simon Glassb6396402014-06-12 07:24:46 -0600262{
Simon Glass2b164f12014-06-12 07:24:52 -0600263 const char *type_name = genimg_get_type_name(type);
264 __attribute__((unused)) uint unc_len = CONFIG_SYS_BOOTM_LEN;
Simon Glassb6396402014-06-12 07:24:46 -0600265
Simon Glass2b164f12014-06-12 07:24:52 -0600266 *load_end = load;
Simon Glassb6396402014-06-12 07:24:46 -0600267 switch (comp) {
268 case IH_COMP_NONE:
269 if (load == image_start) {
270 printf(" XIP %s ... ", type_name);
Simon Glassb6396402014-06-12 07:24:46 -0600271 } else {
272 printf(" Loading %s ... ", type_name);
273 memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
274 }
275 *load_end = load + image_len;
276 break;
277#ifdef CONFIG_GZIP
278 case IH_COMP_GZIP:
279 printf(" Uncompressing %s ... ", type_name);
280 if (gunzip(load_buf, unc_len, image_buf, &image_len) != 0) {
281 puts("GUNZIP: uncompress, out-of-mem or overwrite error - must RESET board to recover\n");
Simon Glassb6396402014-06-12 07:24:46 -0600282 return BOOTM_ERR_RESET;
283 }
284
285 *load_end = load + image_len;
286 break;
287#endif /* CONFIG_GZIP */
288#ifdef CONFIG_BZIP2
289 case IH_COMP_BZIP2:
290 printf(" Uncompressing %s ... ", type_name);
291 /*
292 * If we've got less than 4 MB of malloc() space,
293 * use slower decompression algorithm which requires
294 * at most 2300 KB of memory.
295 */
296 int i = BZ2_bzBuffToBuffDecompress(load_buf, &unc_len,
297 image_buf, image_len,
298 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
299 if (i != BZ_OK) {
300 printf("BUNZIP2: uncompress or overwrite error %d - must RESET board to recover\n",
301 i);
Simon Glassb6396402014-06-12 07:24:46 -0600302 return BOOTM_ERR_RESET;
303 }
304
305 *load_end = load + unc_len;
306 break;
307#endif /* CONFIG_BZIP2 */
308#ifdef CONFIG_LZMA
309 case IH_COMP_LZMA: {
310 SizeT lzma_len = unc_len;
Simon Glass2b164f12014-06-12 07:24:52 -0600311 int ret;
312
Simon Glassb6396402014-06-12 07:24:46 -0600313 printf(" Uncompressing %s ... ", type_name);
314
315 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
316 image_buf, image_len);
317 unc_len = lzma_len;
318 if (ret != SZ_OK) {
319 printf("LZMA: uncompress or overwrite error %d - must RESET board to recover\n",
320 ret);
321 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
322 return BOOTM_ERR_RESET;
323 }
324 *load_end = load + unc_len;
325 break;
326 }
327#endif /* CONFIG_LZMA */
328#ifdef CONFIG_LZO
329 case IH_COMP_LZO: {
330 size_t size = unc_len;
Simon Glass2b164f12014-06-12 07:24:52 -0600331 int ret;
Simon Glassb6396402014-06-12 07:24:46 -0600332
333 printf(" Uncompressing %s ... ", type_name);
334
335 ret = lzop_decompress(image_buf, image_len, load_buf, &size);
336 if (ret != LZO_E_OK) {
337 printf("LZO: uncompress or overwrite error %d - must RESET board to recover\n",
338 ret);
Simon Glassb6396402014-06-12 07:24:46 -0600339 return BOOTM_ERR_RESET;
340 }
341
342 *load_end = load + size;
343 break;
344 }
345#endif /* CONFIG_LZO */
346 default:
347 printf("Unimplemented compression type %d\n", comp);
348 return BOOTM_ERR_UNIMPLEMENTED;
349 }
350
Simon Glass2b164f12014-06-12 07:24:52 -0600351 puts("OK\n");
352
353 return 0;
354}
355
356static int bootm_load_os(bootm_headers_t *images, unsigned long *load_end,
357 int boot_progress)
358{
359 image_info_t os = images->os;
360 ulong load = os.load;
361 ulong blob_start = os.start;
362 ulong blob_end = os.end;
363 ulong image_start = os.image_start;
364 ulong image_len = os.image_len;
365 bool no_overlap;
366 void *load_buf, *image_buf;
367 int err;
368
369 load_buf = map_sysmem(load, 0);
370 image_buf = map_sysmem(os.image_start, image_len);
371 err = decomp_image(os.comp, load, os.image_start, os.type, load_buf,
372 image_buf, image_len, load_end);
373 if (err) {
374 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
375 return err;
376 }
Simon Glassb6396402014-06-12 07:24:46 -0600377 flush_cache(load, (*load_end - load) * sizeof(ulong));
378
Simon Glassb6396402014-06-12 07:24:46 -0600379 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
380 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
381
Simon Glass2b164f12014-06-12 07:24:52 -0600382 no_overlap = (os.comp == IH_COMP_NONE && load == image_start);
383
Simon Glassb6396402014-06-12 07:24:46 -0600384 if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
385 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
386 blob_start, blob_end);
387 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
388 *load_end);
389
390 /* Check what type of image this is. */
391 if (images->legacy_hdr_valid) {
392 if (image_get_type(&images->legacy_hdr_os_copy)
393 == IH_TYPE_MULTI)
394 puts("WARNING: legacy format multi component image overwritten\n");
395 return BOOTM_ERR_OVERLAP;
396 } else {
397 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
398 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
399 return BOOTM_ERR_RESET;
400 }
401 }
402
403 return 0;
404}
405
406/**
407 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
408 *
409 * @return interrupt flag (0 if interrupts were disabled, non-zero if they were
410 * enabled)
411 */
412ulong bootm_disable_interrupts(void)
413{
414 ulong iflag;
415
416 /*
417 * We have reached the point of no return: we are going to
418 * overwrite all exception vector code, so we cannot easily
419 * recover from any failures any more...
420 */
421 iflag = disable_interrupts();
422#ifdef CONFIG_NETCONSOLE
423 /* Stop the ethernet stack if NetConsole could have left it up */
424 eth_halt();
425 eth_unregister(eth_get_dev());
426#endif
427
428#if defined(CONFIG_CMD_USB)
429 /*
430 * turn off USB to prevent the host controller from writing to the
431 * SDRAM while Linux is booting. This could happen (at least for OHCI
432 * controller), because the HCCA (Host Controller Communication Area)
433 * lies within the SDRAM and the host controller writes continously to
434 * this area (as busmaster!). The HccaFrameNumber is for example
435 * updated every 1 ms within the HCCA structure in SDRAM! For more
436 * details see the OpenHCI specification.
437 */
438 usb_stop();
439#endif
440 return iflag;
441}
442
443#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
444
445#define CONSOLE_ARG "console="
446#define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
447
448static void fixup_silent_linux(void)
449{
450 char *buf;
451 const char *env_val;
452 char *cmdline = getenv("bootargs");
453 int want_silent;
454
455 /*
456 * Only fix cmdline when requested. The environment variable can be:
457 *
458 * no - we never fixup
459 * yes - we always fixup
460 * unset - we rely on the console silent flag
461 */
462 want_silent = getenv_yesno("silent_linux");
463 if (want_silent == 0)
464 return;
465 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
466 return;
467
468 debug("before silent fix-up: %s\n", cmdline);
469 if (cmdline && (cmdline[0] != '\0')) {
470 char *start = strstr(cmdline, CONSOLE_ARG);
471
472 /* Allocate space for maximum possible new command line */
473 buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1);
474 if (!buf) {
475 debug("%s: out of memory\n", __func__);
476 return;
477 }
478
479 if (start) {
480 char *end = strchr(start, ' ');
481 int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN;
482
483 strncpy(buf, cmdline, num_start_bytes);
484 if (end)
485 strcpy(buf + num_start_bytes, end);
486 else
487 buf[num_start_bytes] = '\0';
488 } else {
489 sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
490 }
491 env_val = buf;
492 } else {
493 buf = NULL;
494 env_val = CONSOLE_ARG;
495 }
496
497 setenv("bootargs", env_val);
498 debug("after silent fix-up: %s\n", env_val);
499 free(buf);
500}
501#endif /* CONFIG_SILENT_CONSOLE */
502
503/**
504 * Execute selected states of the bootm command.
505 *
506 * Note the arguments to this state must be the first argument, Any 'bootm'
507 * or sub-command arguments must have already been taken.
508 *
509 * Note that if states contains more than one flag it MUST contain
510 * BOOTM_STATE_START, since this handles and consumes the command line args.
511 *
512 * Also note that aside from boot_os_fn functions and bootm_load_os no other
513 * functions we store the return value of in 'ret' may use a negative return
514 * value, without special handling.
515 *
516 * @param cmdtp Pointer to bootm command table entry
517 * @param flag Command flags (CMD_FLAG_...)
518 * @param argc Number of subcommand arguments (0 = no arguments)
519 * @param argv Arguments
520 * @param states Mask containing states to run (BOOTM_STATE_...)
521 * @param images Image header information
522 * @param boot_progress 1 to show boot progress, 0 to not do this
523 * @return 0 if ok, something else on error. Some errors will cause this
524 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
525 * then the intent is to boot an OS, so this function will not return
526 * unless the image type is standalone.
527 */
528int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
529 int states, bootm_headers_t *images, int boot_progress)
530{
531 boot_os_fn *boot_fn;
532 ulong iflag = 0;
533 int ret = 0, need_boot_fn;
534
535 images->state |= states;
536
537 /*
538 * Work through the states and see how far we get. We stop on
539 * any error.
540 */
541 if (states & BOOTM_STATE_START)
542 ret = bootm_start(cmdtp, flag, argc, argv);
543
544 if (!ret && (states & BOOTM_STATE_FINDOS))
545 ret = bootm_find_os(cmdtp, flag, argc, argv);
546
547 if (!ret && (states & BOOTM_STATE_FINDOTHER)) {
548 ret = bootm_find_other(cmdtp, flag, argc, argv);
549 argc = 0; /* consume the args */
550 }
551
552 /* Load the OS */
553 if (!ret && (states & BOOTM_STATE_LOADOS)) {
554 ulong load_end;
555
556 iflag = bootm_disable_interrupts();
557 ret = bootm_load_os(images, &load_end, 0);
558 if (ret == 0)
559 lmb_reserve(&images->lmb, images->os.load,
560 (load_end - images->os.load));
561 else if (ret && ret != BOOTM_ERR_OVERLAP)
562 goto err;
563 else if (ret == BOOTM_ERR_OVERLAP)
564 ret = 0;
565#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
566 if (images->os.os == IH_OS_LINUX)
567 fixup_silent_linux();
568#endif
569 }
570
571 /* Relocate the ramdisk */
572#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
573 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
574 ulong rd_len = images->rd_end - images->rd_start;
575
576 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
577 rd_len, &images->initrd_start, &images->initrd_end);
578 if (!ret) {
579 setenv_hex("initrd_start", images->initrd_start);
580 setenv_hex("initrd_end", images->initrd_end);
581 }
582 }
583#endif
584#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_LMB)
585 if (!ret && (states & BOOTM_STATE_FDT)) {
586 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
587 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
588 &images->ft_len);
589 }
590#endif
591
592 /* From now on, we need the OS boot function */
593 if (ret)
594 return ret;
595 boot_fn = bootm_os_get_boot_func(images->os.os);
596 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
597 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
598 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
599 if (boot_fn == NULL && need_boot_fn) {
600 if (iflag)
601 enable_interrupts();
602 printf("ERROR: booting os '%s' (%d) is not supported\n",
603 genimg_get_os_name(images->os.os), images->os.os);
604 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
605 return 1;
606 }
607
608 /* Call various other states that are not generally used */
609 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
610 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
611 if (!ret && (states & BOOTM_STATE_OS_BD_T))
612 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
613 if (!ret && (states & BOOTM_STATE_OS_PREP))
614 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
615
616#ifdef CONFIG_TRACE
617 /* Pretend to run the OS, then run a user command */
618 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
619 char *cmd_list = getenv("fakegocmd");
620
621 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
622 images, boot_fn);
623 if (!ret && cmd_list)
624 ret = run_command_list(cmd_list, -1, flag);
625 }
626#endif
627
628 /* Check for unsupported subcommand. */
629 if (ret) {
630 puts("subcommand not supported\n");
631 return ret;
632 }
633
634 /* Now run the OS! We hope this doesn't return */
635 if (!ret && (states & BOOTM_STATE_OS_GO))
636 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
637 images, boot_fn);
638
639 /* Deal with any fallout */
640err:
641 if (iflag)
642 enable_interrupts();
643
644 if (ret == BOOTM_ERR_UNIMPLEMENTED)
645 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
646 else if (ret == BOOTM_ERR_RESET)
647 do_reset(cmdtp, flag, argc, argv);
648
649 return ret;
650}
651
652#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
653/**
654 * image_get_kernel - verify legacy format kernel image
655 * @img_addr: in RAM address of the legacy format image to be verified
656 * @verify: data CRC verification flag
657 *
658 * image_get_kernel() verifies legacy image integrity and returns pointer to
659 * legacy image header if image verification was completed successfully.
660 *
661 * returns:
662 * pointer to a legacy image header if valid image was found
663 * otherwise return NULL
664 */
665static image_header_t *image_get_kernel(ulong img_addr, int verify)
666{
667 image_header_t *hdr = (image_header_t *)img_addr;
668
669 if (!image_check_magic(hdr)) {
670 puts("Bad Magic Number\n");
671 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
672 return NULL;
673 }
674 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
675
676 if (!image_check_hcrc(hdr)) {
677 puts("Bad Header Checksum\n");
678 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
679 return NULL;
680 }
681
682 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
683 image_print_contents(hdr);
684
685 if (verify) {
686 puts(" Verifying Checksum ... ");
687 if (!image_check_dcrc(hdr)) {
688 printf("Bad Data CRC\n");
689 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
690 return NULL;
691 }
692 puts("OK\n");
693 }
694 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
695
696 if (!image_check_target_arch(hdr)) {
697 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
698 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
699 return NULL;
700 }
701 return hdr;
702}
703#endif
704
705/**
706 * boot_get_kernel - find kernel image
707 * @os_data: pointer to a ulong variable, will hold os data start address
708 * @os_len: pointer to a ulong variable, will hold os data length
709 *
710 * boot_get_kernel() tries to find a kernel image, verifies its integrity
711 * and locates kernel data.
712 *
713 * returns:
714 * pointer to image header if valid image was found, plus kernel start
715 * address and length, otherwise NULL
716 */
717static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
718 char * const argv[], bootm_headers_t *images,
719 ulong *os_data, ulong *os_len)
720{
721#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
722 image_header_t *hdr;
723#endif
724 ulong img_addr;
725 const void *buf;
726#if defined(CONFIG_FIT)
727 const char *fit_uname_config = NULL;
728 const char *fit_uname_kernel = NULL;
729 int os_noffset;
730#endif
731
732 /* find out kernel image address */
733 if (argc < 1) {
734 img_addr = load_addr;
735 debug("* kernel: default image load address = 0x%08lx\n",
736 load_addr);
737#if defined(CONFIG_FIT)
738 } else if (fit_parse_conf(argv[0], load_addr, &img_addr,
739 &fit_uname_config)) {
740 debug("* kernel: config '%s' from image at 0x%08lx\n",
741 fit_uname_config, img_addr);
742 } else if (fit_parse_subimage(argv[0], load_addr, &img_addr,
743 &fit_uname_kernel)) {
744 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
745 fit_uname_kernel, img_addr);
746#endif
747 } else {
748 img_addr = simple_strtoul(argv[0], NULL, 16);
749 debug("* kernel: cmdline image address = 0x%08lx\n",
750 img_addr);
751 }
752
753 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
754
755 /* copy from dataflash if needed */
756 img_addr = genimg_get_image(img_addr);
757
758 /* check image type, for FIT images get FIT kernel node */
759 *os_data = *os_len = 0;
760 buf = map_sysmem(img_addr, 0);
761 switch (genimg_get_format(buf)) {
762#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
763 case IMAGE_FORMAT_LEGACY:
764 printf("## Booting kernel from Legacy Image at %08lx ...\n",
765 img_addr);
766 hdr = image_get_kernel(img_addr, images->verify);
767 if (!hdr)
768 return NULL;
769 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
770
771 /* get os_data and os_len */
772 switch (image_get_type(hdr)) {
773 case IH_TYPE_KERNEL:
774 case IH_TYPE_KERNEL_NOLOAD:
775 *os_data = image_get_data(hdr);
776 *os_len = image_get_data_size(hdr);
777 break;
778 case IH_TYPE_MULTI:
779 image_multi_getimg(hdr, 0, os_data, os_len);
780 break;
781 case IH_TYPE_STANDALONE:
782 *os_data = image_get_data(hdr);
783 *os_len = image_get_data_size(hdr);
784 break;
785 default:
786 printf("Wrong Image Type for %s command\n",
787 cmdtp->name);
788 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
789 return NULL;
790 }
791
792 /*
793 * copy image header to allow for image overwrites during
794 * kernel decompression.
795 */
796 memmove(&images->legacy_hdr_os_copy, hdr,
797 sizeof(image_header_t));
798
799 /* save pointer to image header */
800 images->legacy_hdr_os = hdr;
801
802 images->legacy_hdr_valid = 1;
803 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
804 break;
805#endif
806#if defined(CONFIG_FIT)
807 case IMAGE_FORMAT_FIT:
Simon Glass126cc862014-06-12 07:24:47 -0600808 os_noffset = fit_image_load(images, img_addr,
Simon Glassb6396402014-06-12 07:24:46 -0600809 &fit_uname_kernel, &fit_uname_config,
810 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
811 BOOTSTAGE_ID_FIT_KERNEL_START,
812 FIT_LOAD_IGNORED, os_data, os_len);
813 if (os_noffset < 0)
814 return NULL;
815
816 images->fit_hdr_os = map_sysmem(img_addr, 0);
817 images->fit_uname_os = fit_uname_kernel;
818 images->fit_uname_cfg = fit_uname_config;
819 images->fit_noffset_os = os_noffset;
820 break;
821#endif
822#ifdef CONFIG_ANDROID_BOOT_IMAGE
823 case IMAGE_FORMAT_ANDROID:
824 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
Simon Glass07c0cd72014-06-12 07:24:48 -0600825 if (android_image_get_kernel(buf, images->verify,
Simon Glassb6396402014-06-12 07:24:46 -0600826 os_data, os_len))
827 return NULL;
828 break;
829#endif
830 default:
831 printf("Wrong Image Format for %s command\n", cmdtp->name);
832 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
833 return NULL;
834 }
835
836 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
837 *os_data, *os_len, *os_len);
838
839 return buf;
840}
Simon Glassea51a622014-06-12 07:24:51 -0600841
842#endif /* ndef USE_HOSTCC */