]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_bootm.c
am33xx/omap: Add a new board to enable verified boot
[people/ms/u-boot.git] / common / cmd_bootm.c
CommitLineData
47d1a6e1 1/*
ca95c9df 2 * (C) Copyright 2000-2009
47d1a6e1
WD
3 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
1a459660 5 * SPDX-License-Identifier: GPL-2.0+
47d1a6e1
WD
6 */
7
b97a2a0a 8
47d1a6e1
WD
9/*
10 * Boot support
11 */
12#include <common.h>
13#include <watchdog.h>
14#include <command.h>
47d1a6e1
WD
15#include <image.h>
16#include <malloc.h>
a31e091a 17#include <u-boot/zlib.h>
c29fdfc1 18#include <bzlib.h>
7f70e853 19#include <environment.h>
4ed6552f 20#include <lmb.h>
49c3a861 21#include <linux/ctype.h>
47d1a6e1 22#include <asm/byteorder.h>
35e7b0f1 23#include <asm/io.h>
5bf2766b 24#include <linux/compiler.h>
8bde7f77 25
871a57bb
MY
26#if defined(CONFIG_BOOTM_VXWORKS) && \
27 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
28#include <vxworks.h>
29#endif
30
ebb86c4e 31#if defined(CONFIG_CMD_USB)
3d71c81a
MK
32#include <usb.h>
33#endif
34
54f9c866 35#if defined(CONFIG_OF_LIBFDT)
54f9c866
KG
36#include <libfdt.h>
37#include <fdt_support.h>
38#endif
39
fc9c1727 40#ifdef CONFIG_LZMA
fc9c1727 41#include <lzma/LzmaTypes.h>
caf72ff3 42#include <lzma/LzmaDec.h>
fc9c1727
LCM
43#include <lzma/LzmaTools.h>
44#endif /* CONFIG_LZMA */
45
20dde48b
PK
46#ifdef CONFIG_LZO
47#include <linux/lzo.h>
48#endif /* CONFIG_LZO */
49
1ee1180b 50DECLARE_GLOBAL_DATA_PTR;
228f29ac 51
6d0f6bcf
JCPV
52#ifndef CONFIG_SYS_BOOTM_LEN
53#define CONFIG_SYS_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
2abbe075
WD
54#endif
55
321359f2
MB
56#ifdef CONFIG_BZIP2
57extern void bz_internal_error(int);
47d1a6e1
WD
58#endif
59
baa26db4 60#if defined(CONFIG_CMD_IMI)
712fbcf3 61static int image_info(unsigned long addr);
47d1a6e1 62#endif
27b207fd 63
baa26db4 64#if defined(CONFIG_CMD_IMLS)
27b207fd 65#include <flash.h>
ca5def3f 66#include <mtd/cfi_flash.h>
e6f2e902 67extern flash_info_t flash_info[]; /* info for FLASH chips */
8fdf1e0f
VK
68#endif
69
70#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
712fbcf3 71static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
27b207fd
WD
72#endif
73
8fdf1e0f
VK
74#include <linux/err.h>
75#include <nand.h>
76
be2e5a09 77#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
712fbcf3 78static void fixup_silent_linux(void);
1ee1180b 79#endif
47d1a6e1 80
c5cbe1e2
SG
81static int do_bootm_standalone(int flag, int argc, char * const argv[],
82 bootm_headers_t *images);
83
35e7b0f1 84static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
712fbcf3
SW
85 char * const argv[], bootm_headers_t *images,
86 ulong *os_data, ulong *os_len);
47d1a6e1
WD
87
88/*
89 * Continue booting an OS image; caller already has:
90 * - copied image header to global variable `header'
91 * - checked header magic number, checksums (both header & image),
92 * - verified image architecture (PPC) and type (KERNEL or MULTI),
93 * - loaded (first part of) image to header load address,
94 * - disabled interrupts.
983c72f4 95 *
35fc84fa 96 * @flag: Flags indicating what to do (BOOTM_STATE_...)
983c72f4
SG
97 * @argc: Number of arguments. Note that the arguments are shifted down
98 * so that 0 is the first argument not processed by U-Boot, and
99 * argc is adjusted accordingly. This avoids confusion as to how
100 * many arguments are available for the OS.
101 * @images: Pointers to os/initrd/fdt
102 * @return 1 on error. On success the OS boots so this function does
103 * not return.
47d1a6e1 104 */
712fbcf3 105typedef int boot_os_fn(int flag, int argc, char * const argv[],
983c72f4 106 bootm_headers_t *images);
1ee1180b 107
b1d0db18 108#ifdef CONFIG_BOOTM_LINUX
1ee1180b 109extern boot_os_fn do_bootm_linux;
b1d0db18
KG
110#endif
111#ifdef CONFIG_BOOTM_NETBSD
1ee1180b 112static boot_os_fn do_bootm_netbsd;
b1d0db18 113#endif
f13e7b2e 114#if defined(CONFIG_LYNXKDI)
1ee1180b 115static boot_os_fn do_bootm_lynxkdi;
712fbcf3 116extern void lynxkdi_boot(image_header_t *);
47d1a6e1 117#endif
b1d0db18 118#ifdef CONFIG_BOOTM_RTEMS
1ee1180b 119static boot_os_fn do_bootm_rtems;
b1d0db18 120#endif
3df61957
TL
121#if defined(CONFIG_BOOTM_OSE)
122static boot_os_fn do_bootm_ose;
123#endif
04d41409
SS
124#if defined(CONFIG_BOOTM_PLAN9)
125static boot_os_fn do_bootm_plan9;
126#endif
871a57bb
MY
127#if defined(CONFIG_BOOTM_VXWORKS) && \
128 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
1ee1180b 129static boot_os_fn do_bootm_vxworks;
17ab52fe
MY
130#endif
131#if defined(CONFIG_CMD_ELF)
1ee1180b 132static boot_os_fn do_bootm_qnxelf;
712fbcf3
SW
133int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
134int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
90253178 135#endif
f5ed9e39
PT
136#if defined(CONFIG_INTEGRITY)
137static boot_os_fn do_bootm_integrity;
138#endif
47d1a6e1 139
0008555f 140static boot_os_fn *boot_os[] = {
c5cbe1e2 141 [IH_OS_U_BOOT] = do_bootm_standalone,
b1d0db18 142#ifdef CONFIG_BOOTM_LINUX
be083159 143 [IH_OS_LINUX] = do_bootm_linux,
b1d0db18
KG
144#endif
145#ifdef CONFIG_BOOTM_NETBSD
be083159 146 [IH_OS_NETBSD] = do_bootm_netbsd,
b1d0db18 147#endif
be083159
KG
148#ifdef CONFIG_LYNXKDI
149 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
150#endif
b1d0db18 151#ifdef CONFIG_BOOTM_RTEMS
be083159 152 [IH_OS_RTEMS] = do_bootm_rtems,
b1d0db18 153#endif
3df61957
TL
154#if defined(CONFIG_BOOTM_OSE)
155 [IH_OS_OSE] = do_bootm_ose,
156#endif
04d41409
SS
157#if defined(CONFIG_BOOTM_PLAN9)
158 [IH_OS_PLAN9] = do_bootm_plan9,
159#endif
871a57bb
MY
160#if defined(CONFIG_BOOTM_VXWORKS) && \
161 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
be083159 162 [IH_OS_VXWORKS] = do_bootm_vxworks,
17ab52fe
MY
163#endif
164#if defined(CONFIG_CMD_ELF)
be083159
KG
165 [IH_OS_QNX] = do_bootm_qnxelf,
166#endif
167#ifdef CONFIG_INTEGRITY
168 [IH_OS_INTEGRITY] = do_bootm_integrity,
169#endif
170};
171
dee17768 172bootm_headers_t images; /* pointers to os/initrd/fdt images */
15940c9a 173
3dfad40a 174/* Allow for arch specific config before we boot */
088f1b19 175static void __arch_preboot_os(void)
3dfad40a
KG
176{
177 /* please define platform specific arch_preboot_os() */
178}
179void arch_preboot_os(void) __attribute__((weak, alias("__arch_preboot_os")));
180
476af299 181#define IH_INITRD_ARCH IH_ARCH_DEFAULT
47d1a6e1 182
a16028da 183#ifdef CONFIG_LMB
44f074c7
MV
184static void boot_start_lmb(bootm_headers_t *images)
185{
391fd93a
BB
186 ulong mem_start;
187 phys_size_t mem_size;
47d1a6e1 188
44f074c7 189 lmb_init(&images->lmb);
47d1a6e1 190
d3f2fa0d
KG
191 mem_start = getenv_bootm_low();
192 mem_size = getenv_bootm_size();
47d1a6e1 193
44f074c7 194 lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size);
47d1a6e1 195
44f074c7
MV
196 arch_lmb_reserve(&images->lmb);
197 board_lmb_reserve(&images->lmb);
198}
a16028da 199#else
35cf5fe5 200#define lmb_reserve(lmb, base, size)
44f074c7 201static inline void boot_start_lmb(bootm_headers_t *images) { }
a16028da 202#endif
a16028da 203
54841ab5 204static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a16028da 205{
712fbcf3
SW
206 memset((void *)&images, 0, sizeof(images));
207 images.verify = getenv_yesno("verify");
a16028da 208
44f074c7 209 boot_start_lmb(&images);
47d1a6e1 210
573f14fe 211 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
35fc84fa
SG
212 images.state = BOOTM_STATE_START;
213
214 return 0;
215}
216
217static int bootm_find_os(cmd_tbl_t *cmdtp, int flag, int argc,
218 char * const argv[])
219{
220 const void *os_hdr;
9ace3fc8 221 bool ep_found = false;
573f14fe 222
5cf746c3 223 /* get kernel image header, start address and length */
712fbcf3 224 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
396f635b
KG
225 &images, &images.os.image_start, &images.os.image_len);
226 if (images.os.image_len == 0) {
712fbcf3 227 puts("ERROR: can't get kernel image!\n");
47d1a6e1 228 return 1;
bccae903 229 }
bccae903 230
d5934ad7 231 /* get image parameters */
712fbcf3 232 switch (genimg_get_format(os_hdr)) {
21d29f7f 233#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
d5934ad7 234 case IMAGE_FORMAT_LEGACY:
712fbcf3
SW
235 images.os.type = image_get_type(os_hdr);
236 images.os.comp = image_get_comp(os_hdr);
237 images.os.os = image_get_os(os_hdr);
bccae903 238
712fbcf3
SW
239 images.os.end = image_get_image_end(os_hdr);
240 images.os.load = image_get_load(os_hdr);
d5934ad7 241 break;
21d29f7f 242#endif
d5934ad7
MB
243#if defined(CONFIG_FIT)
244 case IMAGE_FORMAT_FIT:
712fbcf3 245 if (fit_image_get_type(images.fit_hdr_os,
396f635b 246 images.fit_noffset_os, &images.os.type)) {
712fbcf3 247 puts("Can't get image type!\n");
770605e4 248 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
6986a385
MB
249 return 1;
250 }
47d1a6e1 251
712fbcf3 252 if (fit_image_get_comp(images.fit_hdr_os,
396f635b 253 images.fit_noffset_os, &images.os.comp)) {
712fbcf3 254 puts("Can't get image compression!\n");
770605e4 255 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
6986a385
MB
256 return 1;
257 }
47d1a6e1 258
712fbcf3 259 if (fit_image_get_os(images.fit_hdr_os,
396f635b 260 images.fit_noffset_os, &images.os.os)) {
712fbcf3 261 puts("Can't get image OS!\n");
770605e4 262 bootstage_error(BOOTSTAGE_ID_FIT_OS);
47d1a6e1
WD
263 return 1;
264 }
47d1a6e1 265
712fbcf3 266 images.os.end = fit_get_end(images.fit_hdr_os);
6986a385 267
712fbcf3 268 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
396f635b 269 &images.os.load)) {
712fbcf3 270 puts("Can't get image load address!\n");
770605e4 271 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
6986a385 272 return 1;
b13fb01a
WD
273 }
274 break;
9ace3fc8
SS
275#endif
276#ifdef CONFIG_ANDROID_BOOT_IMAGE
277 case IMAGE_FORMAT_ANDROID:
278 images.os.type = IH_TYPE_KERNEL;
279 images.os.comp = IH_COMP_NONE;
280 images.os.os = IH_OS_LINUX;
281 images.ep = images.os.load;
282 ep_found = true;
283
284 images.os.end = android_image_get_end(os_hdr);
285 images.os.load = android_image_get_kload(os_hdr);
286 break;
d5934ad7
MB
287#endif
288 default:
712fbcf3 289 puts("ERROR: unknown image format type!\n");
47d1a6e1
WD
290 return 1;
291 }
d5934ad7 292
c160a954
KG
293 /* find kernel entry point */
294 if (images.legacy_hdr_valid) {
712fbcf3 295 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
c160a954
KG
296#if defined(CONFIG_FIT)
297 } else if (images.fit_uname_os) {
35fc84fa
SG
298 int ret;
299
712fbcf3 300 ret = fit_image_get_entry(images.fit_hdr_os,
c19d13b0 301 images.fit_noffset_os, &images.ep);
c160a954 302 if (ret) {
712fbcf3 303 puts("Can't get entry point property!\n");
c160a954
KG
304 return 1;
305 }
306#endif
9ace3fc8 307 } else if (!ep_found) {
712fbcf3 308 puts("Could not find kernel entry point!\n");
c160a954
KG
309 return 1;
310 }
311
b9b50e89
SW
312 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
313 images.os.load = images.os.image_start;
314 images.ep += images.os.load;
315 }
316
35fc84fa
SG
317 images.os.start = (ulong)os_hdr;
318
319 return 0;
320}
321
225fd8c5
TR
322static int bootm_find_ramdisk(int flag, int argc, char * const argv[])
323{
324 int ret;
325
326 /* find ramdisk */
327 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
328 &images.rd_start, &images.rd_end);
329 if (ret) {
330 puts("Ramdisk image is corrupt or invalid\n");
331 return 1;
332 }
333
334 return 0;
335}
336
337#if defined(CONFIG_OF_LIBFDT)
338static int bootm_find_fdt(int flag, int argc, char * const argv[])
35fc84fa
SG
339{
340 int ret;
341
225fd8c5
TR
342 /* find flattened device tree */
343 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
344 &images.ft_addr, &images.ft_len);
345 if (ret) {
346 puts("Could not find a valid device tree\n");
347 return 1;
348 }
349
350 set_working_fdt_addr(images.ft_addr);
351
352 return 0;
353}
354#endif
355
356static int bootm_find_other(cmd_tbl_t *cmdtp, int flag, int argc,
357 char * const argv[])
358{
24de2f4b 359 if (((images.os.type == IH_TYPE_KERNEL) ||
b9b50e89 360 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
24de2f4b 361 (images.os.type == IH_TYPE_MULTI)) &&
871a57bb
MY
362 (images.os.os == IH_OS_LINUX ||
363 images.os.os == IH_OS_VXWORKS)) {
225fd8c5 364 if (bootm_find_ramdisk(flag, argc, argv))
c4f9419c 365 return 1;
06a09918
KG
366
367#if defined(CONFIG_OF_LIBFDT)
225fd8c5 368 if (bootm_find_fdt(flag, argc, argv))
06a09918 369 return 1;
06a09918 370#endif
c4f9419c
KG
371 }
372
396f635b
KG
373 return 0;
374}
3d71c81a 375
396f635b
KG
376#define BOOTM_ERR_RESET -1
377#define BOOTM_ERR_OVERLAP -2
378#define BOOTM_ERR_UNIMPLEMENTED -3
d366438d
TR
379static int bootm_load_os(bootm_headers_t *images, unsigned long *load_end,
380 int boot_progress)
396f635b 381{
d366438d 382 image_info_t os = images->os;
396f635b
KG
383 uint8_t comp = os.comp;
384 ulong load = os.load;
385 ulong blob_start = os.start;
386 ulong blob_end = os.end;
387 ulong image_start = os.image_start;
388 ulong image_len = os.image_len;
5bf2766b 389 __maybe_unused uint unc_len = CONFIG_SYS_BOOTM_LEN;
d510859b 390 int no_overlap = 0;
aed161e5 391 void *load_buf, *image_buf;
60fdc5f2
MW
392#if defined(CONFIG_LZMA) || defined(CONFIG_LZO)
393 int ret;
394#endif /* defined(CONFIG_LZMA) || defined(CONFIG_LZO) */
3d71c81a 395
712fbcf3 396 const char *type_name = genimg_get_type_name(os.type);
c7de829c 397
315c0ace 398 load_buf = map_sysmem(load, unc_len);
aed161e5 399 image_buf = map_sysmem(image_start, image_len);
d5934ad7 400 switch (comp) {
47d1a6e1 401 case IH_COMP_NONE:
717ccc1d 402 if (load == image_start) {
712fbcf3 403 printf(" XIP %s ... ", type_name);
d510859b 404 no_overlap = 1;
47d1a6e1 405 } else {
712fbcf3 406 printf(" Loading %s ... ", type_name);
aed161e5 407 memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
47d1a6e1 408 }
396f635b 409 *load_end = load + image_len;
47d1a6e1 410 break;
44431cab 411#ifdef CONFIG_GZIP
47d1a6e1 412 case IH_COMP_GZIP:
712fbcf3 413 printf(" Uncompressing %s ... ", type_name);
aed161e5 414 if (gunzip(load_buf, unc_len, image_buf, &image_len) != 0) {
712fbcf3
SW
415 puts("GUNZIP: uncompress, out-of-mem or overwrite "
416 "error - must RESET board to recover\n");
396f635b 417 if (boot_progress)
770605e4 418 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
396f635b 419 return BOOTM_ERR_RESET;
47d1a6e1 420 }
7582438c 421
396f635b 422 *load_end = load + image_len;
47d1a6e1 423 break;
44431cab 424#endif /* CONFIG_GZIP */
c29fdfc1
WD
425#ifdef CONFIG_BZIP2
426 case IH_COMP_BZIP2:
712fbcf3 427 printf(" Uncompressing %s ... ", type_name);
5653fc33
WD
428 /*
429 * If we've got less than 4 MB of malloc() space,
430 * use slower decompression algorithm which requires
431 * at most 2300 KB of memory.
432 */
aed161e5
SG
433 int i = BZ2_bzBuffToBuffDecompress(load_buf, &unc_len,
434 image_buf, image_len,
435 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
c29fdfc1 436 if (i != BZ_OK) {
712fbcf3 437 printf("BUNZIP2: uncompress or overwrite error %d "
2682ce8a 438 "- must RESET board to recover\n", i);
396f635b 439 if (boot_progress)
770605e4 440 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
396f635b 441 return BOOTM_ERR_RESET;
c29fdfc1 442 }
7582438c 443
396f635b 444 *load_end = load + unc_len;
c29fdfc1
WD
445 break;
446#endif /* CONFIG_BZIP2 */
fc9c1727 447#ifdef CONFIG_LZMA
78e1e846
MF
448 case IH_COMP_LZMA: {
449 SizeT lzma_len = unc_len;
712fbcf3 450 printf(" Uncompressing %s ... ", type_name);
fc9c1727 451
aed161e5
SG
452 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
453 image_buf, image_len);
78e1e846 454 unc_len = lzma_len;
caf72ff3 455 if (ret != SZ_OK) {
712fbcf3 456 printf("LZMA: uncompress or overwrite error %d "
fc9c1727 457 "- must RESET board to recover\n", ret);
770605e4 458 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
fc9c1727
LCM
459 return BOOTM_ERR_RESET;
460 }
461 *load_end = load + unc_len;
462 break;
78e1e846 463 }
fc9c1727 464#endif /* CONFIG_LZMA */
20dde48b 465#ifdef CONFIG_LZO
628af179 466 case IH_COMP_LZO: {
77cc8902 467 size_t size = unc_len;
628af179 468
712fbcf3 469 printf(" Uncompressing %s ... ", type_name);
20dde48b 470
628af179 471 ret = lzop_decompress(image_buf, image_len, load_buf, &size);
20dde48b 472 if (ret != LZO_E_OK) {
712fbcf3 473 printf("LZO: uncompress or overwrite error %d "
20dde48b
PK
474 "- must RESET board to recover\n", ret);
475 if (boot_progress)
770605e4 476 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
20dde48b
PK
477 return BOOTM_ERR_RESET;
478 }
479
628af179 480 *load_end = load + size;
20dde48b 481 break;
628af179 482 }
20dde48b 483#endif /* CONFIG_LZO */
47d1a6e1 484 default:
712fbcf3 485 printf("Unimplemented compression type %d\n", comp);
396f635b 486 return BOOTM_ERR_UNIMPLEMENTED;
47d1a6e1 487 }
99ffccbd
DC
488
489 flush_cache(load, (*load_end - load) * sizeof(ulong));
490
712fbcf3
SW
491 puts("OK\n");
492 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
770605e4 493 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
47d1a6e1 494
d510859b 495 if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
712fbcf3
SW
496 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
497 blob_start, blob_end);
498 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
499 *load_end);
47d1a6e1 500
d366438d
TR
501 /* Check what type of image this is. */
502 if (images->legacy_hdr_valid) {
503 if (image_get_type(&images->legacy_hdr_os_copy)
504 == IH_TYPE_MULTI)
505 puts("WARNING: legacy format multi component image overwritten\n");
506 return BOOTM_ERR_OVERLAP;
507 } else {
508 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
509 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
510 return BOOTM_ERR_RESET;
511 }
396f635b
KG
512 }
513
514 return 0;
515}
516
c5cbe1e2
SG
517static int do_bootm_standalone(int flag, int argc, char * const argv[],
518 bootm_headers_t *images)
f97ec30b
DZ
519{
520 char *s;
54841ab5 521 int (*appl)(int, char * const []);
f97ec30b
DZ
522
523 /* Don't start if "autostart" is set to "no" */
524 if (((s = getenv("autostart")) != NULL) && (strcmp(s, "no") == 0)) {
c5cbe1e2 525 setenv_hex("filesize", images->os.image_len);
f97ec30b
DZ
526 return 0;
527 }
9c89614d
CE
528 appl = (int (*)(int, char * const []))images->ep;
529 appl(argc, argv);
f97ec30b
DZ
530 return 0;
531}
532
49c3a861
KG
533/* we overload the cmd field with our state machine info instead of a
534 * function pointer */
f74d9bd2 535static cmd_tbl_t cmd_bootm_sub[] = {
49c3a861
KG
536 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
537 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
fca43cc8 538#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
49c3a861
KG
539 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
540#endif
541#ifdef CONFIG_OF_LIBFDT
542 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
543#endif
49c3a861 544 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
224c90d1 545 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
49c3a861 546 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
d0ae31eb 547 U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
49c3a861
KG
548 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
549};
550
35fc84fa 551static int boot_selected_os(int argc, char * const argv[], int state,
5ff0d083 552 bootm_headers_t *images, boot_os_fn *boot_fn)
35fc84fa 553{
35fc84fa
SG
554 arch_preboot_os();
555 boot_fn(state, argc, argv, images);
c5cbe1e2
SG
556
557 /* Stand-alone may return when 'autostart' is 'no' */
558 if (images->os.type == IH_TYPE_STANDALONE ||
559 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
d0ae31eb 560 return 0;
35fc84fa
SG
561 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
562#ifdef DEBUG
563 puts("\n## Control returned to monitor - resetting...\n");
564#endif
565 return BOOTM_ERR_RESET;
566}
567
385501d3
SG
568/**
569 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
570 *
571 * @return interrupt flag (0 if interrupts were disabled, non-zero if they were
572 * enabled)
573 */
574static ulong bootm_disable_interrupts(void)
575{
576 ulong iflag;
577
578 /*
579 * We have reached the point of no return: we are going to
580 * overwrite all exception vector code, so we cannot easily
581 * recover from any failures any more...
582 */
583 iflag = disable_interrupts();
584#ifdef CONFIG_NETCONSOLE
585 /* Stop the ethernet stack if NetConsole could have left it up */
586 eth_halt();
8094972d 587 eth_unregister(eth_get_dev());
385501d3
SG
588#endif
589
590#if defined(CONFIG_CMD_USB)
591 /*
592 * turn off USB to prevent the host controller from writing to the
593 * SDRAM while Linux is booting. This could happen (at least for OHCI
594 * controller), because the HCCA (Host Controller Communication Area)
595 * lies within the SDRAM and the host controller writes continously to
596 * this area (as busmaster!). The HccaFrameNumber is for example
597 * updated every 1 ms within the HCCA structure in SDRAM! For more
598 * details see the OpenHCI specification.
599 */
600 usb_stop();
601#endif
602 return iflag;
603}
604
35fc84fa
SG
605/**
606 * Execute selected states of the bootm command.
607 *
608 * Note the arguments to this state must be the first argument, Any 'bootm'
609 * or sub-command arguments must have already been taken.
610 *
611 * Note that if states contains more than one flag it MUST contain
612 * BOOTM_STATE_START, since this handles and consumes the command line args.
613 *
d366438d
TR
614 * Also note that aside from boot_os_fn functions and bootm_load_os no other
615 * functions we store the return value of in 'ret' may use a negative return
616 * value, without special handling.
617 *
35fc84fa
SG
618 * @param cmdtp Pointer to bootm command table entry
619 * @param flag Command flags (CMD_FLAG_...)
620 * @param argc Number of subcommand arguments (0 = no arguments)
621 * @param argv Arguments
622 * @param states Mask containing states to run (BOOTM_STATE_...)
623 * @param images Image header information
624 * @param boot_progress 1 to show boot progress, 0 to not do this
625 * @return 0 if ok, something else on error. Some errors will cause this
626 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
627 * then the intent is to boot an OS, so this function will not return
628 * unless the image type is standalone.
629 */
630static int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc,
631 char * const argv[], int states, bootm_headers_t *images,
632 int boot_progress)
633{
634 boot_os_fn *boot_fn;
635 ulong iflag = 0;
a26913f3 636 int ret = 0, need_boot_fn;
35fc84fa
SG
637
638 images->state |= states;
639
640 /*
641 * Work through the states and see how far we get. We stop on
642 * any error.
643 */
644 if (states & BOOTM_STATE_START)
645 ret = bootm_start(cmdtp, flag, argc, argv);
646
647 if (!ret && (states & BOOTM_STATE_FINDOS))
648 ret = bootm_find_os(cmdtp, flag, argc, argv);
649
650 if (!ret && (states & BOOTM_STATE_FINDOTHER)) {
651 ret = bootm_find_other(cmdtp, flag, argc, argv);
652 argc = 0; /* consume the args */
653 }
654
655 /* Load the OS */
656 if (!ret && (states & BOOTM_STATE_LOADOS)) {
657 ulong load_end;
658
385501d3 659 iflag = bootm_disable_interrupts();
d366438d 660 ret = bootm_load_os(images, &load_end, 0);
d366438d 661 if (ret == 0)
35fc84fa
SG
662 lmb_reserve(&images->lmb, images->os.load,
663 (load_end - images->os.load));
970150a1
TR
664 else if (ret && ret != BOOTM_ERR_OVERLAP)
665 goto err;
d366438d
TR
666 else if (ret == BOOTM_ERR_OVERLAP)
667 ret = 0;
bd4a3997 668#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
576aacdb
SG
669 if (images->os.os == IH_OS_LINUX)
670 fixup_silent_linux();
671#endif
35fc84fa
SG
672 }
673
674 /* Relocate the ramdisk */
675#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
676 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
677 ulong rd_len = images->rd_end - images->rd_start;
678
679 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
680 rd_len, &images->initrd_start, &images->initrd_end);
681 if (!ret) {
682 setenv_hex("initrd_start", images->initrd_start);
683 setenv_hex("initrd_end", images->initrd_end);
684 }
685 }
686#endif
687#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_LMB)
688 if (!ret && (states & BOOTM_STATE_FDT)) {
689 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
690 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
691 &images->ft_len);
692 }
693#endif
694
695 /* From now on, we need the OS boot function */
696 if (ret)
697 return ret;
698 boot_fn = boot_os[images->os.os];
a26913f3
SG
699 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
700 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
701 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
702 if (boot_fn == NULL && need_boot_fn) {
35fc84fa
SG
703 if (iflag)
704 enable_interrupts();
705 printf("ERROR: booting os '%s' (%d) is not supported\n",
706 genimg_get_os_name(images->os.os), images->os.os);
707 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
708 return 1;
709 }
710
711 /* Call various other states that are not generally used */
712 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
713 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
714 if (!ret && (states & BOOTM_STATE_OS_BD_T))
715 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
716 if (!ret && (states & BOOTM_STATE_OS_PREP))
717 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
718
d0ae31eb
SG
719#ifdef CONFIG_TRACE
720 /* Pretend to run the OS, then run a user command */
721 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
722 char *cmd_list = getenv("fakegocmd");
723
724 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
5ff0d083 725 images, boot_fn);
d0ae31eb
SG
726 if (!ret && cmd_list)
727 ret = run_command_list(cmd_list, -1, flag);
728 }
729#endif
b7a1d134
SG
730
731 /* Check for unsupported subcommand. */
732 if (ret) {
733 puts("subcommand not supported\n");
734 return ret;
735 }
736
35fc84fa 737 /* Now run the OS! We hope this doesn't return */
b7a1d134 738 if (!ret && (states & BOOTM_STATE_OS_GO))
35fc84fa 739 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
5ff0d083 740 images, boot_fn);
35fc84fa
SG
741
742 /* Deal with any fallout */
d366438d 743err:
35fc84fa
SG
744 if (iflag)
745 enable_interrupts();
d366438d
TR
746
747 if (ret == BOOTM_ERR_UNIMPLEMENTED)
748 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
749 else if (ret == BOOTM_ERR_RESET)
750 do_reset(cmdtp, flag, argc, argv);
35fc84fa
SG
751
752 return ret;
753}
754
088f1b19 755static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
712fbcf3 756 char * const argv[])
49c3a861
KG
757{
758 int ret = 0;
6d6f1236 759 long state;
49c3a861 760 cmd_tbl_t *c;
49c3a861 761
983c72f4
SG
762 c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
763 argc--; argv++;
49c3a861
KG
764
765 if (c) {
6d6f1236 766 state = (long)c->cmd;
983c72f4 767 if (state == BOOTM_STATE_START)
35fc84fa 768 state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
47e26b1b
WD
769 } else {
770 /* Unrecognized command */
4c12eeb8 771 return CMD_RET_USAGE;
49c3a861
KG
772 }
773
35fc84fa 774 if (state != BOOTM_STATE_START && images.state >= state) {
712fbcf3 775 printf("Trying to execute a command out of order\n");
4c12eeb8 776 return CMD_RET_USAGE;
49c3a861
KG
777 }
778
35fc84fa 779 ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
49c3a861
KG
780
781 return ret;
782}
783
396f635b
KG
784/*******************************************************************/
785/* bootm - boot application image from image in memory */
786/*******************************************************************/
be083159 787
712fbcf3 788int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
396f635b 789{
2e5167cc 790#ifdef CONFIG_NEEDS_MANUAL_RELOC
521af04d 791 static int relocated = 0;
be083159 792
be083159
KG
793 if (!relocated) {
794 int i;
58bd77db
DS
795
796 /* relocate boot function table */
be083159 797 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
ca95c9df
DZ
798 if (boot_os[i] != NULL)
799 boot_os[i] += gd->reloc_off;
58bd77db
DS
800
801 /* relocate names of sub-command table */
802 for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
803 cmd_bootm_sub[i].name += gd->reloc_off;
804
be083159
KG
805 relocated = 1;
806 }
521af04d 807#endif
396f635b 808
49c3a861 809 /* determine if we have a sub command */
983c72f4
SG
810 argc--; argv++;
811 if (argc > 0) {
49c3a861
KG
812 char *endp;
813
983c72f4
SG
814 simple_strtoul(argv[0], &endp, 16);
815 /* endp pointing to NULL means that argv[0] was just a
49c3a861
KG
816 * valid number, pass it along to the normal bootm processing
817 *
818 * If endp is ':' or '#' assume a FIT identifier so pass
819 * along for normal processing.
820 *
821 * Right now we assume the first arg should never be '-'
822 */
823 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
824 return do_bootm_subcommand(cmdtp, flag, argc, argv);
825 }
826
35fc84fa
SG
827 return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
828 BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
3d187b39
TR
829 BOOTM_STATE_LOADOS |
830#if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
831 BOOTM_STATE_OS_CMDLINE |
832#endif
5c427e49
PB
833 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
834 BOOTM_STATE_OS_GO, &images, 1);
47d1a6e1
WD
835}
836
67d668bf
MF
837int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
838{
839 const char *ep = getenv("autostart");
840
841 if (ep && !strcmp(ep, "yes")) {
842 char *local_args[2];
843 local_args[0] = (char *)cmd;
844 local_args[1] = NULL;
845 printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
846 return do_bootm(cmdtp, 0, 1, local_args);
847 }
848
849 return 0;
850}
851
21d29f7f 852#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
1efd4360 853/**
9a4daad0
MB
854 * image_get_kernel - verify legacy format kernel image
855 * @img_addr: in RAM address of the legacy format image to be verified
856 * @verify: data CRC verification flag
1efd4360 857 *
9a4daad0
MB
858 * image_get_kernel() verifies legacy image integrity and returns pointer to
859 * legacy image header if image verification was completed successfully.
1efd4360
MB
860 *
861 * returns:
9a4daad0
MB
862 * pointer to a legacy image header if valid image was found
863 * otherwise return NULL
1efd4360 864 */
712fbcf3 865static image_header_t *image_get_kernel(ulong img_addr, int verify)
f72da340 866{
1efd4360 867 image_header_t *hdr = (image_header_t *)img_addr;
f72da340 868
1efd4360 869 if (!image_check_magic(hdr)) {
712fbcf3 870 puts("Bad Magic Number\n");
770605e4 871 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
1efd4360
MB
872 return NULL;
873 }
770605e4 874 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
f72da340 875
712fbcf3
SW
876 if (!image_check_hcrc(hdr)) {
877 puts("Bad Header Checksum\n");
770605e4 878 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
1efd4360
MB
879 return NULL;
880 }
881
770605e4 882 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
712fbcf3 883 image_print_contents(hdr);
1efd4360
MB
884
885 if (verify) {
712fbcf3
SW
886 puts(" Verifying Checksum ... ");
887 if (!image_check_dcrc(hdr)) {
888 printf("Bad Data CRC\n");
770605e4 889 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
1efd4360 890 return NULL;
f72da340 891 }
712fbcf3 892 puts("OK\n");
f72da340 893 }
770605e4 894 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
f72da340 895
712fbcf3
SW
896 if (!image_check_target_arch(hdr)) {
897 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
770605e4 898 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
1efd4360
MB
899 return NULL;
900 }
901 return hdr;
f72da340 902}
21d29f7f 903#endif
f72da340 904
5cf746c3 905/**
9a4daad0 906 * boot_get_kernel - find kernel image
5cf746c3
MB
907 * @os_data: pointer to a ulong variable, will hold os data start address
908 * @os_len: pointer to a ulong variable, will hold os data length
909 *
9a4daad0 910 * boot_get_kernel() tries to find a kernel image, verifies its integrity
5cf746c3
MB
911 * and locates kernel data.
912 *
913 * returns:
914 * pointer to image header if valid image was found, plus kernel start
915 * address and length, otherwise NULL
916 */
35e7b0f1 917static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
712fbcf3
SW
918 char * const argv[], bootm_headers_t *images, ulong *os_data,
919 ulong *os_len)
5cf746c3 920{
21d29f7f 921#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
5cf746c3 922 image_header_t *hdr;
21d29f7f 923#endif
5cf746c3 924 ulong img_addr;
35e7b0f1 925 const void *buf;
d5934ad7 926#if defined(CONFIG_FIT)
d5934ad7
MB
927 const char *fit_uname_config = NULL;
928 const char *fit_uname_kernel = NULL;
6986a385 929 int os_noffset;
d5934ad7 930#endif
56f94be3 931
d5934ad7 932 /* find out kernel image address */
983c72f4 933 if (argc < 1) {
5cf746c3 934 img_addr = load_addr;
712fbcf3 935 debug("* kernel: default image load address = 0x%08lx\n",
d5934ad7
MB
936 load_addr);
937#if defined(CONFIG_FIT)
983c72f4 938 } else if (fit_parse_conf(argv[0], load_addr, &img_addr,
d5934ad7 939 &fit_uname_config)) {
712fbcf3 940 debug("* kernel: config '%s' from image at 0x%08lx\n",
d5934ad7 941 fit_uname_config, img_addr);
983c72f4 942 } else if (fit_parse_subimage(argv[0], load_addr, &img_addr,
d5934ad7 943 &fit_uname_kernel)) {
712fbcf3 944 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
d5934ad7
MB
945 fit_uname_kernel, img_addr);
946#endif
5cf746c3 947 } else {
983c72f4 948 img_addr = simple_strtoul(argv[0], NULL, 16);
712fbcf3 949 debug("* kernel: cmdline image address = 0x%08lx\n", img_addr);
5cf746c3 950 }
47d1a6e1 951
770605e4 952 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
56f94be3 953
fff888a1 954 /* copy from dataflash if needed */
712fbcf3 955 img_addr = genimg_get_image(img_addr);
5cf746c3 956
d5934ad7 957 /* check image type, for FIT images get FIT kernel node */
6986a385 958 *os_data = *os_len = 0;
35e7b0f1
SG
959 buf = map_sysmem(img_addr, 0);
960 switch (genimg_get_format(buf)) {
21d29f7f 961#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
d5934ad7 962 case IMAGE_FORMAT_LEGACY:
712fbcf3 963 printf("## Booting kernel from Legacy Image at %08lx ...\n",
6986a385 964 img_addr);
712fbcf3 965 hdr = image_get_kernel(img_addr, images->verify);
1efd4360 966 if (!hdr)
d5934ad7 967 return NULL;
770605e4 968 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
d5934ad7 969
6986a385 970 /* get os_data and os_len */
712fbcf3 971 switch (image_get_type(hdr)) {
d5934ad7 972 case IH_TYPE_KERNEL:
b9b50e89 973 case IH_TYPE_KERNEL_NOLOAD:
712fbcf3
SW
974 *os_data = image_get_data(hdr);
975 *os_len = image_get_data_size(hdr);
d5934ad7
MB
976 break;
977 case IH_TYPE_MULTI:
712fbcf3 978 image_multi_getimg(hdr, 0, os_data, os_len);
d5934ad7 979 break;
f97ec30b 980 case IH_TYPE_STANDALONE:
712fbcf3
SW
981 *os_data = image_get_data(hdr);
982 *os_len = image_get_data_size(hdr);
f97ec30b 983 break;
d5934ad7 984 default:
712fbcf3
SW
985 printf("Wrong Image Type for %s command\n",
986 cmdtp->name);
770605e4 987 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
d5934ad7
MB
988 return NULL;
989 }
cb1c4896
MB
990
991 /*
712fbcf3
SW
992 * copy image header to allow for image overwrites during
993 * kernel decompression.
cb1c4896 994 */
712fbcf3
SW
995 memmove(&images->legacy_hdr_os_copy, hdr,
996 sizeof(image_header_t));
cb1c4896
MB
997
998 /* save pointer to image header */
d5934ad7 999 images->legacy_hdr_os = hdr;
47d1a6e1 1000
cb1c4896 1001 images->legacy_hdr_valid = 1;
770605e4 1002 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
5cf746c3 1003 break;
21d29f7f 1004#endif
d5934ad7
MB
1005#if defined(CONFIG_FIT)
1006 case IMAGE_FORMAT_FIT:
4651800d
SG
1007 os_noffset = fit_image_load(images, FIT_KERNEL_PROP,
1008 img_addr,
f320a4d8 1009 &fit_uname_kernel, &fit_uname_config,
4651800d
SG
1010 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
1011 BOOTSTAGE_ID_FIT_KERNEL_START,
1012 FIT_LOAD_IGNORED, os_data, os_len);
1013 if (os_noffset < 0)
6986a385 1014 return NULL;
47d1a6e1 1015
4651800d 1016 images->fit_hdr_os = map_sysmem(img_addr, 0);
6986a385 1017 images->fit_uname_os = fit_uname_kernel;
f320a4d8 1018 images->fit_uname_cfg = fit_uname_config;
3dfe1101 1019 images->fit_noffset_os = os_noffset;
6986a385 1020 break;
9ace3fc8
SS
1021#endif
1022#ifdef CONFIG_ANDROID_BOOT_IMAGE
1023 case IMAGE_FORMAT_ANDROID:
1024 printf("## Booting Android Image at 0x%08lx ...\n", img_addr);
1025 if (android_image_get_kernel((void *)img_addr, images->verify,
1026 os_data, os_len))
1027 return NULL;
1028 break;
9c4c5ae3 1029#endif
5cf746c3 1030 default:
712fbcf3 1031 printf("Wrong Image Format for %s command\n", cmdtp->name);
770605e4 1032 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
5cf746c3 1033 return NULL;
47d1a6e1
WD
1034 }
1035
712fbcf3 1036 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
6986a385 1037 *os_data, *os_len, *os_len);
47d1a6e1 1038
35e7b0f1 1039 return buf;
5cf746c3 1040}
98a9c4d4 1041
088f1b19
KP
1042#ifdef CONFIG_SYS_LONGHELP
1043static char bootm_help_text[] =
1ee1180b
MB
1044 "[addr [arg ...]]\n - boot application image stored in memory\n"
1045 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
1046 "\t'arg' can be the address of an initrd image\n"
4a2ad5ff 1047#if defined(CONFIG_OF_LIBFDT)
98a9c4d4 1048 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
5441f61a 1049 "\ta third argument is required which is the address of the\n"
98a9c4d4
MM
1050 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1051 "\tuse a '-' for the second argument. If you do not pass a third\n"
1052 "\ta bd_info struct will be passed instead\n"
98a9c4d4 1053#endif
6986a385
MB
1054#if defined(CONFIG_FIT)
1055 "\t\nFor the new multi component uImage format (FIT) addresses\n"
1056 "\tmust be extened to include component or configuration unit name:\n"
1057 "\taddr:<subimg_uname> - direct component image specification\n"
1058 "\taddr#<conf_uname> - configuration specification\n"
1059 "\tUse iminfo command to get the list of existing component\n"
1060 "\timages and configurations.\n"
1061#endif
49c3a861
KG
1062 "\nSub-commands to do part of the bootm sequence. The sub-commands "
1063 "must be\n"
1064 "issued in the order below (it's ok to not issue all sub-commands):\n"
1065 "\tstart [addr [arg ...]]\n"
1066 "\tloados - load OS image\n"
59af76d9 1067#if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
49c3a861
KG
1068 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
1069#endif
1070#if defined(CONFIG_OF_LIBFDT)
1071 "\tfdt - relocate flat device tree\n"
1072#endif
49c3a861 1073 "\tcmdline - OS specific command line processing/setup\n"
224c90d1 1074 "\tbdt - OS specific bd_t processing\n"
49c3a861 1075 "\tprep - OS specific prep before relocation or go\n"
088f1b19
KP
1076 "\tgo - start OS";
1077#endif
1078
1079U_BOOT_CMD(
1080 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
1081 "boot application image from memory", bootm_help_text
8bde7f77 1082);
47d1a6e1 1083
1ee1180b
MB
1084/*******************************************************************/
1085/* bootd - boot default image */
1086/*******************************************************************/
baa26db4 1087#if defined(CONFIG_CMD_BOOTD)
712fbcf3 1088int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
47d1a6e1
WD
1089{
1090 int rcode = 0;
47d1a6e1 1091
53071532 1092 if (run_command(getenv("bootcmd"), flag) < 0)
1ee1180b 1093 rcode = 1;
47d1a6e1
WD
1094 return rcode;
1095}
47d1a6e1 1096
0d498393 1097U_BOOT_CMD(
1ee1180b 1098 boot, 1, 1, do_bootd,
2fb2604d 1099 "boot default, i.e., run 'bootcmd'",
a89c33db 1100 ""
9d2b18a0 1101);
47d1a6e1 1102
9d2b18a0 1103/* keep old command name "bootd" for backward compatibility */
0d498393 1104U_BOOT_CMD(
1ee1180b 1105 bootd, 1, 1, do_bootd,
2fb2604d 1106 "boot default, i.e., run 'bootcmd'",
a89c33db 1107 ""
8bde7f77 1108);
47d1a6e1 1109
47d1a6e1 1110#endif
47d1a6e1 1111
47d1a6e1 1112
1ee1180b
MB
1113/*******************************************************************/
1114/* iminfo - print header info for a requested image */
1115/*******************************************************************/
baa26db4 1116#if defined(CONFIG_CMD_IMI)
088f1b19 1117static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
47d1a6e1
WD
1118{
1119 int arg;
1120 ulong addr;
1ee1180b 1121 int rcode = 0;
47d1a6e1 1122
47d1a6e1 1123 if (argc < 2) {
712fbcf3 1124 return image_info(load_addr);
47d1a6e1 1125 }
47d1a6e1 1126
1ee1180b 1127 for (arg = 1; arg < argc; ++arg) {
712fbcf3
SW
1128 addr = simple_strtoul(argv[arg], NULL, 16);
1129 if (image_info(addr) != 0)
1ee1180b 1130 rcode = 1;
47d1a6e1
WD
1131 }
1132 return rcode;
1133}
47d1a6e1 1134
712fbcf3 1135static int image_info(ulong addr)
47d1a6e1 1136{
d5934ad7 1137 void *hdr = (void *)addr;
47d1a6e1 1138
712fbcf3 1139 printf("\n## Checking Image at %08lx ...\n", addr);
47d1a6e1 1140
712fbcf3 1141 switch (genimg_get_format(hdr)) {
21d29f7f 1142#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
d5934ad7 1143 case IMAGE_FORMAT_LEGACY:
712fbcf3
SW
1144 puts(" Legacy image found\n");
1145 if (!image_check_magic(hdr)) {
1146 puts(" Bad Magic Number\n");
d5934ad7
MB
1147 return 1;
1148 }
47d1a6e1 1149
712fbcf3
SW
1150 if (!image_check_hcrc(hdr)) {
1151 puts(" Bad Header Checksum\n");
d5934ad7 1152 return 1;
47d1a6e1
WD
1153 }
1154
712fbcf3 1155 image_print_contents(hdr);
47d1a6e1 1156
712fbcf3
SW
1157 puts(" Verifying Checksum ... ");
1158 if (!image_check_dcrc(hdr)) {
1159 puts(" Bad Data CRC\n");
d5934ad7 1160 return 1;
47d1a6e1 1161 }
712fbcf3 1162 puts("OK\n");
d5934ad7 1163 return 0;
21d29f7f 1164#endif
d5934ad7
MB
1165#if defined(CONFIG_FIT)
1166 case IMAGE_FORMAT_FIT:
712fbcf3 1167 puts(" FIT image found\n");
47d1a6e1 1168
712fbcf3
SW
1169 if (!fit_check_format(hdr)) {
1170 puts("Bad FIT image format!\n");
e32fea6a 1171 return 1;
47d1a6e1
WD
1172 }
1173
712fbcf3 1174 fit_print_contents(hdr);
a4f24345 1175
b8da8366 1176 if (!fit_all_image_verify(hdr)) {
712fbcf3 1177 puts("Bad hash in FIT image!\n");
a4f24345
BS
1178 return 1;
1179 }
1180
d5934ad7
MB
1181 return 0;
1182#endif
1183 default:
712fbcf3 1184 puts("Unknown image format!\n");
d5934ad7 1185 break;
47d1a6e1
WD
1186 }
1187
d5934ad7 1188 return 1;
47d1a6e1 1189}
0d498393
WD
1190
1191U_BOOT_CMD(
6d0f6bcf 1192 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
2fb2604d 1193 "print header information for application image",
8bde7f77
WD
1194 "addr [addr ...]\n"
1195 " - print header information for application image starting at\n"
1196 " address 'addr' in memory; this includes verification of the\n"
a89c33db 1197 " image contents (magic number, header and payload checksums)"
8bde7f77 1198);
25c751e9 1199#endif
87a449c8 1200
25c751e9 1201
1ee1180b
MB
1202/*******************************************************************/
1203/* imls - list all images found in flash */
1204/*******************************************************************/
baa26db4 1205#if defined(CONFIG_CMD_IMLS)
8fdf1e0f 1206static int do_imls_nor(void)
27b207fd
WD
1207{
1208 flash_info_t *info;
1209 int i, j;
d5934ad7 1210 void *hdr;
25c751e9 1211
1ee1180b 1212 for (i = 0, info = &flash_info[0];
6d0f6bcf 1213 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
25c751e9 1214
27b207fd
WD
1215 if (info->flash_id == FLASH_UNKNOWN)
1216 goto next_bank;
1ee1180b 1217 for (j = 0; j < info->sector_count; ++j) {
25c751e9 1218
d5934ad7
MB
1219 hdr = (void *)info->start[j];
1220 if (!hdr)
b97a2a0a 1221 goto next_sector;
27b207fd 1222
712fbcf3 1223 switch (genimg_get_format(hdr)) {
21d29f7f 1224#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
d5934ad7 1225 case IMAGE_FORMAT_LEGACY:
712fbcf3 1226 if (!image_check_hcrc(hdr))
d5934ad7
MB
1227 goto next_sector;
1228
712fbcf3
SW
1229 printf("Legacy Image at %08lX:\n", (ulong)hdr);
1230 image_print_contents(hdr);
d5934ad7 1231
712fbcf3
SW
1232 puts(" Verifying Checksum ... ");
1233 if (!image_check_dcrc(hdr)) {
1234 puts("Bad Data CRC\n");
d5934ad7 1235 } else {
712fbcf3 1236 puts("OK\n");
d5934ad7
MB
1237 }
1238 break;
21d29f7f 1239#endif
d5934ad7
MB
1240#if defined(CONFIG_FIT)
1241 case IMAGE_FORMAT_FIT:
712fbcf3 1242 if (!fit_check_format(hdr))
e32fea6a
MB
1243 goto next_sector;
1244
712fbcf3
SW
1245 printf("FIT Image at %08lX:\n", (ulong)hdr);
1246 fit_print_contents(hdr);
d5934ad7 1247 break;
213bf8c8 1248#endif
d5934ad7 1249 default:
27b207fd 1250 goto next_sector;
25c751e9 1251 }
87a449c8 1252
bdccc4fe 1253next_sector: ;
c76f951a 1254 }
bdccc4fe 1255next_bank: ;
27b207fd 1256 }
8fdf1e0f
VK
1257 return 0;
1258}
1259#endif
1260
1261#if defined(CONFIG_CMD_IMLS_NAND)
1262static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
1263 size_t len)
1264{
1265 void *imgdata;
1266 int ret;
1267
1268 imgdata = malloc(len);
1269 if (!imgdata) {
1270 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
1271 nand_dev, off);
1272 printf(" Low memory(cannot allocate memory for image)\n");
1273 return -ENOMEM;
1274 }
1275
1276 ret = nand_read_skip_bad(nand, off, &len,
1277 imgdata);
1278 if (ret < 0 && ret != -EUCLEAN) {
1279 free(imgdata);
1280 return ret;
1281 }
1282
1283 if (!image_check_hcrc(imgdata)) {
1284 free(imgdata);
1285 return 0;
1286 }
1287
1288 printf("Legacy Image at NAND device %d offset %08llX:\n",
1289 nand_dev, off);
1290 image_print_contents(imgdata);
1291
1292 puts(" Verifying Checksum ... ");
1293 if (!image_check_dcrc(imgdata))
1294 puts("Bad Data CRC\n");
1295 else
1296 puts("OK\n");
1297
1298 free(imgdata);
1299
1300 return 0;
1301}
1302
1303static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
1304 size_t len)
1305{
1306 void *imgdata;
1307 int ret;
1308
1309 imgdata = malloc(len);
1310 if (!imgdata) {
1311 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
1312 nand_dev, off);
1313 printf(" Low memory(cannot allocate memory for image)\n");
1314 return -ENOMEM;
1315 }
1316
1317 ret = nand_read_skip_bad(nand, off, &len,
1318 imgdata);
1319 if (ret < 0 && ret != -EUCLEAN) {
1320 free(imgdata);
1321 return ret;
1322 }
1323
1324 if (!fit_check_format(imgdata)) {
1325 free(imgdata);
1326 return 0;
1327 }
1328
1329 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
1330
1331 fit_print_contents(imgdata);
1332 free(imgdata);
1333
1334 return 0;
1335}
1336
1337static int do_imls_nand(void)
1338{
1339 nand_info_t *nand;
1340 int nand_dev = nand_curr_device;
1341 size_t len;
1342 loff_t off;
1343 u32 buffer[16];
1344
1345 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
1346 puts("\nNo NAND devices available\n");
1347 return -ENODEV;
1348 }
1349
1350 printf("\n");
1351
1352 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
1353 nand = &nand_info[nand_dev];
1354 if (!nand->name || !nand->size)
1355 continue;
1356
1357 for (off = 0; off < nand->size; off += nand->erasesize) {
1358 const image_header_t *header;
1359 int ret;
1360
1361 if (nand_block_isbad(nand, off))
1362 continue;
1363
1364 len = sizeof(buffer);
1365
1366 ret = nand_read(nand, off, &len, (u8 *)buffer);
1367 if (ret < 0 && ret != -EUCLEAN) {
1368 printf("NAND read error %d at offset %08llX\n",
1369 ret, off);
1370 continue;
1371 }
1372
1373 switch (genimg_get_format(buffer)) {
21d29f7f 1374#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
8fdf1e0f
VK
1375 case IMAGE_FORMAT_LEGACY:
1376 header = (const image_header_t *)buffer;
1377
1378 len = image_get_image_size(header);
1379 nand_imls_legacyimage(nand, nand_dev, off, len);
1380 break;
21d29f7f 1381#endif
8fdf1e0f
VK
1382#if defined(CONFIG_FIT)
1383 case IMAGE_FORMAT_FIT:
1384 len = fit_get_size(buffer);
1385 nand_imls_fitimage(nand, nand_dev, off, len);
1386 break;
1387#endif
1388 }
1389 }
1390 }
1391
1392 return 0;
1393}
1394#endif
1395
1396#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
1397static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1398{
1399 int ret_nor = 0, ret_nand = 0;
1400
1401#if defined(CONFIG_CMD_IMLS)
1402 ret_nor = do_imls_nor();
1403#endif
1404
1405#if defined(CONFIG_CMD_IMLS_NAND)
1406 ret_nand = do_imls_nand();
1407#endif
1408
1409 if (ret_nor)
1410 return ret_nor;
1411
1412 if (ret_nand)
1413 return ret_nand;
c76f951a 1414
27b207fd
WD
1415 return (0);
1416}
c76f951a 1417
27b207fd
WD
1418U_BOOT_CMD(
1419 imls, 1, 1, do_imls,
2fb2604d 1420 "list all images found in flash",
27b207fd 1421 "\n"
8fdf1e0f
VK
1422 " - Prints information about all images found at sector/block\n"
1423 " boundaries in nor/nand flash."
27b207fd 1424);
98a9c4d4 1425#endif
47d1a6e1 1426
1ee1180b 1427/*******************************************************************/
5cf746c3 1428/* helper routines */
1ee1180b 1429/*******************************************************************/
be2e5a09 1430#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
a558ad71
DA
1431
1432#define CONSOLE_ARG "console="
1433#define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
1434
3a8653b3 1435static void fixup_silent_linux(void)
1ee1180b 1436{
a558ad71
DA
1437 char *buf;
1438 const char *env_val;
3a8653b3 1439 char *cmdline = getenv("bootargs");
8d51aacd 1440 int want_silent;
47d1a6e1 1441
8d51aacd
SG
1442 /*
1443 * Only fix cmdline when requested. The environment variable can be:
1444 *
1445 * no - we never fixup
1446 * yes - we always fixup
1447 * unset - we rely on the console silent flag
1448 */
1449 want_silent = getenv_yesno("silent_linux");
1450 if (want_silent == 0)
1451 return;
1452 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
1ee1180b 1453 return;
47d1a6e1 1454
3a8653b3 1455 debug("before silent fix-up: %s\n", cmdline);
a558ad71
DA
1456 if (cmdline && (cmdline[0] != '\0')) {
1457 char *start = strstr(cmdline, CONSOLE_ARG);
1458
1459 /* Allocate space for maximum possible new command line */
1460 buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1);
1461 if (!buf) {
1462 debug("%s: out of memory\n", __func__);
1463 return;
1464 }
1465
3a8653b3 1466 if (start) {
a558ad71
DA
1467 char *end = strchr(start, ' ');
1468 int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN;
1469
1470 strncpy(buf, cmdline, num_start_bytes);
1ee1180b 1471 if (end)
a558ad71 1472 strcpy(buf + num_start_bytes, end);
1ee1180b 1473 else
a558ad71 1474 buf[num_start_bytes] = '\0';
1ee1180b 1475 } else {
a558ad71 1476 sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
47d1a6e1 1477 }
a558ad71 1478 env_val = buf;
47d1a6e1 1479 } else {
a558ad71
DA
1480 buf = NULL;
1481 env_val = CONSOLE_ARG;
e7902122 1482 }
10aaf716 1483
a558ad71
DA
1484 setenv("bootargs", env_val);
1485 debug("after silent fix-up: %s\n", env_val);
1486 free(buf);
1ee1180b
MB
1487}
1488#endif /* CONFIG_SILENT_CONSOLE */
38eb508e 1489
eeaef5e4
SS
1490#if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
1491static void copy_args(char *dest, int argc, char * const argv[], char delim)
1492{
1493 int i;
1494
1495 for (i = 0; i < argc; i++) {
1496 if (i > 0)
1497 *dest++ = delim;
1498 strcpy(dest, argv[i]);
1499 dest += strlen(argv[i]);
1500 }
1501}
1502#endif
38eb508e 1503
1ee1180b
MB
1504/*******************************************************************/
1505/* OS booting routines */
1506/*******************************************************************/
c76f951a 1507
b1d0db18 1508#ifdef CONFIG_BOOTM_NETBSD
712fbcf3 1509static int do_bootm_netbsd(int flag, int argc, char * const argv[],
8a5ea3e6 1510 bootm_headers_t *images)
d791b1dc 1511{
1ee1180b 1512 void (*loader)(bd_t *, image_header_t *, char *, char *);
d5934ad7 1513 image_header_t *os_hdr, *hdr;
f13e7b2e 1514 ulong kernel_data, kernel_len;
1ee1180b
MB
1515 char *consdev;
1516 char *cmdline;
1517
5b629319 1518 if (flag != BOOTM_STATE_OS_GO)
7af26b16 1519 return 0;
49c3a861 1520
d5934ad7
MB
1521#if defined(CONFIG_FIT)
1522 if (!images->legacy_hdr_valid) {
712fbcf3 1523 fit_unsupported_reset("NetBSD");
40d7e99d 1524 return 1;
38eb508e
GVB
1525 }
1526#endif
d5934ad7 1527 hdr = images->legacy_hdr_os;
47d1a6e1
WD
1528
1529 /*
1530 * Booting a (NetBSD) kernel image
1531 *
1532 * This process is pretty similar to a standalone application:
1533 * The (first part of an multi-) image must be a stage-2 loader,
1534 * which in turn is responsible for loading & invoking the actual
1535 * kernel. The only differences are the parameters being passed:
1536 * besides the board info strucure, the loader expects a command
1537 * line, the name of the console device, and (optionally) the
1538 * address of the original image header.
1539 */
d5934ad7 1540 os_hdr = NULL;
712fbcf3
SW
1541 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1542 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
f13e7b2e 1543 if (kernel_len)
d5934ad7 1544 os_hdr = hdr;
f13e7b2e 1545 }
47d1a6e1
WD
1546
1547 consdev = "";
712fbcf3 1548#if defined(CONFIG_8xx_CONS_SMC1)
47d1a6e1 1549 consdev = "smc1";
712fbcf3 1550#elif defined(CONFIG_8xx_CONS_SMC2)
47d1a6e1 1551 consdev = "smc2";
712fbcf3 1552#elif defined(CONFIG_8xx_CONS_SCC2)
47d1a6e1 1553 consdev = "scc2";
712fbcf3 1554#elif defined(CONFIG_8xx_CONS_SCC3)
47d1a6e1
WD
1555 consdev = "scc3";
1556#endif
1557
983c72f4 1558 if (argc > 0) {
47d1a6e1
WD
1559 ulong len;
1560 int i;
1561
983c72f4 1562 for (i = 0, len = 0; i < argc; i += 1)
712fbcf3
SW
1563 len += strlen(argv[i]) + 1;
1564 cmdline = malloc(len);
eeaef5e4 1565 copy_args(cmdline, argc, argv, ' ');
712fbcf3 1566 } else if ((cmdline = getenv("bootargs")) == NULL) {
47d1a6e1
WD
1567 cmdline = "";
1568 }
1569
c160a954 1570 loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
47d1a6e1 1571
712fbcf3
SW
1572 printf("## Transferring control to NetBSD stage-2 loader "
1573 "(at address %08lx) ...\n",
47d1a6e1
WD
1574 (ulong)loader);
1575
770605e4 1576 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
47d1a6e1
WD
1577
1578 /*
1579 * NetBSD Stage-2 Loader Parameters:
e2ce8169
KJ
1580 * arg[0]: pointer to board info data
1581 * arg[1]: image load address
1582 * arg[2]: char pointer to the console device to use
1583 * arg[3]: char pointer to the boot arguments
47d1a6e1 1584 */
712fbcf3 1585 (*loader)(gd->bd, os_hdr, consdev, cmdline);
40d7e99d
KG
1586
1587 return 1;
47d1a6e1 1588}
b1d0db18 1589#endif /* CONFIG_BOOTM_NETBSD*/
47d1a6e1 1590
1ee1180b 1591#ifdef CONFIG_LYNXKDI
712fbcf3 1592static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
8a5ea3e6 1593 bootm_headers_t *images)
1ee1180b 1594{
cb1c4896 1595 image_header_t *hdr = &images->legacy_hdr_os_copy;
d5934ad7 1596
5b629319 1597 if (flag != BOOTM_STATE_OS_GO)
7af26b16 1598 return 0;
49c3a861 1599
d5934ad7
MB
1600#if defined(CONFIG_FIT)
1601 if (!images->legacy_hdr_valid) {
712fbcf3 1602 fit_unsupported_reset("Lynx");
40d7e99d 1603 return 1;
d5934ad7
MB
1604 }
1605#endif
1606
712fbcf3 1607 lynxkdi_boot((image_header_t *)hdr);
40d7e99d
KG
1608
1609 return 1;
1ee1180b
MB
1610}
1611#endif /* CONFIG_LYNXKDI */
1612
b1d0db18 1613#ifdef CONFIG_BOOTM_RTEMS
712fbcf3 1614static int do_bootm_rtems(int flag, int argc, char * const argv[],
8a5ea3e6 1615 bootm_headers_t *images)
1ee1180b 1616{
1ee1180b 1617 void (*entry_point)(bd_t *);
d791b1dc 1618
5b629319 1619 if (flag != BOOTM_STATE_OS_GO)
7af26b16 1620 return 0;
49c3a861 1621
d5934ad7
MB
1622#if defined(CONFIG_FIT)
1623 if (!images->legacy_hdr_valid) {
712fbcf3 1624 fit_unsupported_reset("RTEMS");
40d7e99d 1625 return 1;
d5934ad7
MB
1626 }
1627#endif
1628
c160a954 1629 entry_point = (void (*)(bd_t *))images->ep;
d791b1dc 1630
712fbcf3 1631 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
d791b1dc
WD
1632 (ulong)entry_point);
1633
770605e4 1634 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
d791b1dc
WD
1635
1636 /*
1637 * RTEMS Parameters:
1638 * r3: ptr to board info data
1639 */
1ee1180b 1640 (*entry_point)(gd->bd);
40d7e99d
KG
1641
1642 return 1;
d791b1dc 1643}
b1d0db18 1644#endif /* CONFIG_BOOTM_RTEMS */
7f70e853 1645
3df61957 1646#if defined(CONFIG_BOOTM_OSE)
712fbcf3 1647static int do_bootm_ose(int flag, int argc, char * const argv[],
3df61957
TL
1648 bootm_headers_t *images)
1649{
1650 void (*entry_point)(void);
1651
5b629319 1652 if (flag != BOOTM_STATE_OS_GO)
7af26b16 1653 return 0;
3df61957
TL
1654
1655#if defined(CONFIG_FIT)
1656 if (!images->legacy_hdr_valid) {
712fbcf3 1657 fit_unsupported_reset("OSE");
3df61957
TL
1658 return 1;
1659 }
1660#endif
1661
1662 entry_point = (void (*)(void))images->ep;
1663
712fbcf3 1664 printf("## Transferring control to OSE (at address %08lx) ...\n",
3df61957
TL
1665 (ulong)entry_point);
1666
770605e4 1667 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
3df61957
TL
1668
1669 /*
1670 * OSE Parameters:
1671 * None
1672 */
1673 (*entry_point)();
1674
1675 return 1;
1676}
1677#endif /* CONFIG_BOOTM_OSE */
1678
04d41409
SS
1679#if defined(CONFIG_BOOTM_PLAN9)
1680static int do_bootm_plan9(int flag, int argc, char * const argv[],
1681 bootm_headers_t *images)
1682{
1683 void (*entry_point)(void);
eeaef5e4 1684 char *s;
04d41409 1685
5b629319 1686 if (flag != BOOTM_STATE_OS_GO)
7af26b16 1687 return 0;
04d41409
SS
1688
1689#if defined(CONFIG_FIT)
1690 if (!images->legacy_hdr_valid) {
1691 fit_unsupported_reset("Plan 9");
1692 return 1;
1693 }
1694#endif
1695
eeaef5e4
SS
1696 /* See README.plan9 */
1697 s = getenv("confaddr");
1698 if (s != NULL) {
1699 char *confaddr = (char *)simple_strtoul(s, NULL, 16);
1700
1701 if (argc > 0) {
1702 copy_args(confaddr, argc, argv, '\n');
1703 } else {
1704 s = getenv("bootargs");
1705 if (s != NULL)
1706 strcpy(confaddr, s);
1707 }
1708 }
1709
04d41409
SS
1710 entry_point = (void (*)(void))images->ep;
1711
1712 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
1713 (ulong)entry_point);
1714
1715 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1716
1717 /*
1718 * Plan 9 Parameters:
1719 * None
1720 */
1721 (*entry_point)();
1722
1723 return 1;
1724}
1725#endif /* CONFIG_BOOTM_PLAN9 */
1726
871a57bb
MY
1727#if defined(CONFIG_BOOTM_VXWORKS) && \
1728 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
1729
1730void do_bootvx_fdt(bootm_headers_t *images)
1731{
1732#if defined(CONFIG_OF_LIBFDT)
1733 int ret;
1734 char *bootline;
1735 ulong of_size = images->ft_len;
1736 char **of_flat_tree = &images->ft_addr;
1737 struct lmb *lmb = &images->lmb;
1738
1739 if (*of_flat_tree) {
1740 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
1741
1742 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
1743 if (ret)
1744 return;
1745
1746 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
1747 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
1748 bootline = getenv("bootargs");
1749 if (bootline) {
1750 ret = fdt_find_and_setprop(*of_flat_tree,
1751 "/chosen", "bootargs",
1752 bootline,
1753 strlen(bootline) + 1, 1);
1754 if (ret < 0) {
1755 printf("## ERROR: %s : %s\n", __func__,
1756 fdt_strerror(ret));
1757 return;
1758 }
1759 }
1760 } else {
1761 printf("## ERROR: %s : %s\n", __func__,
1762 fdt_strerror(ret));
1763 return;
1764 }
1765 }
1766#endif
1767
1768 boot_prep_vxworks(images);
1769
1770 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1771
1772#if defined(CONFIG_OF_LIBFDT)
1773 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
1774 (ulong)images->ep, (ulong)*of_flat_tree);
1775#else
1776 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
1777#endif
1778
1779 boot_jump_vxworks(images);
1780
1781 puts("## vxWorks terminated\n");
1782}
1783
712fbcf3 1784static int do_bootm_vxworks(int flag, int argc, char * const argv[],
8a5ea3e6 1785 bootm_headers_t *images)
47d1a6e1 1786{
5b629319 1787 if (flag != BOOTM_STATE_OS_GO)
7af26b16 1788 return 0;
49c3a861 1789
d5934ad7 1790#if defined(CONFIG_FIT)
cb1c4896 1791 if (!images->legacy_hdr_valid) {
712fbcf3 1792 fit_unsupported_reset("VxWorks");
40d7e99d 1793 return 1;
d5934ad7
MB
1794 }
1795#endif
47d1a6e1 1796
871a57bb 1797 do_bootvx_fdt(images);
40d7e99d
KG
1798
1799 return 1;
47d1a6e1 1800}
17ab52fe 1801#endif
47d1a6e1 1802
17ab52fe 1803#if defined(CONFIG_CMD_ELF)
54841ab5 1804static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
8a5ea3e6 1805 bootm_headers_t *images)
47d1a6e1 1806{
47d1a6e1
WD
1807 char *local_args[2];
1808 char str[16];
d5934ad7 1809
5b629319 1810 if (flag != BOOTM_STATE_OS_GO)
7af26b16 1811 return 0;
49c3a861 1812
d5934ad7
MB
1813#if defined(CONFIG_FIT)
1814 if (!images->legacy_hdr_valid) {
712fbcf3 1815 fit_unsupported_reset("QNX");
40d7e99d 1816 return 1;
d5934ad7
MB
1817 }
1818#endif
47d1a6e1 1819
c160a954 1820 sprintf(str, "%lx", images->ep); /* write entry-point into string */
47d1a6e1
WD
1821 local_args[0] = argv[0];
1822 local_args[1] = str; /* and provide it via the arguments */
40d7e99d
KG
1823 do_bootelf(NULL, 0, 2, local_args);
1824
1825 return 1;
47d1a6e1 1826}
90253178 1827#endif
f5ed9e39
PT
1828
1829#ifdef CONFIG_INTEGRITY
712fbcf3 1830static int do_bootm_integrity(int flag, int argc, char * const argv[],
f5ed9e39
PT
1831 bootm_headers_t *images)
1832{
1833 void (*entry_point)(void);
1834
5b629319 1835 if (flag != BOOTM_STATE_OS_GO)
7af26b16 1836 return 0;
49c3a861 1837
f5ed9e39
PT
1838#if defined(CONFIG_FIT)
1839 if (!images->legacy_hdr_valid) {
712fbcf3 1840 fit_unsupported_reset("INTEGRITY");
f5ed9e39
PT
1841 return 1;
1842 }
1843#endif
1844
1845 entry_point = (void (*)(void))images->ep;
1846
712fbcf3 1847 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
f5ed9e39
PT
1848 (ulong)entry_point);
1849
770605e4 1850 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
f5ed9e39
PT
1851
1852 /*
1853 * INTEGRITY Parameters:
1854 * None
1855 */
1856 (*entry_point)();
1857
1858 return 1;
1859}
1860#endif
44f074c7
MV
1861
1862#ifdef CONFIG_CMD_BOOTZ
1863
a5266d6b 1864int __weak bootz_setup(ulong image, ulong *start, ulong *end)
44f074c7
MV
1865{
1866 /* Please define bootz_setup() for your platform */
1867
1868 puts("Your platform's zImage format isn't supported yet!\n");
1869 return -1;
1870}
44f074c7
MV
1871
1872/*
1873 * zImage booting support
1874 */
1875static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
1876 char * const argv[], bootm_headers_t *images)
1877{
1878 int ret;
a5266d6b 1879 ulong zi_start, zi_end;
44f074c7 1880
35fc84fa
SG
1881 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
1882 images, 1);
44f074c7
MV
1883
1884 /* Setup Linux kernel zImage entry point */
2b9599e0 1885 if (!argc) {
44f074c7
MV
1886 images->ep = load_addr;
1887 debug("* kernel: default image load address = 0x%08lx\n",
1888 load_addr);
1889 } else {
2b9599e0 1890 images->ep = simple_strtoul(argv[0], NULL, 16);
44f074c7
MV
1891 debug("* kernel: cmdline image address = 0x%08lx\n",
1892 images->ep);
1893 }
1894
a5266d6b 1895 ret = bootz_setup(images->ep, &zi_start, &zi_end);
44f074c7
MV
1896 if (ret != 0)
1897 return 1;
1898
1899 lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
1900
225fd8c5
TR
1901 /*
1902 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
1903 * have a header that provide this informaiton.
1904 */
2b9599e0
TR
1905 if (bootm_find_ramdisk(flag, argc, argv))
1906 return 1;
44f074c7 1907
2b9599e0
TR
1908#if defined(CONFIG_OF_LIBFDT)
1909 if (bootm_find_fdt(flag, argc, argv))
1910 return 1;
1911#endif
1912
1913 return 0;
44f074c7
MV
1914}
1915
da620222 1916int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
44f074c7 1917{
35fc84fa 1918 int ret;
44f074c7 1919
2b9599e0
TR
1920 /* Consume 'bootz' */
1921 argc--; argv++;
1922
44f074c7
MV
1923 if (bootz_start(cmdtp, flag, argc, argv, &images))
1924 return 1;
1925
385501d3
SG
1926 /*
1927 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
1928 * disable interrupts ourselves
1929 */
1930 bootm_disable_interrupts();
1931
fb1b139b 1932 images.os.os = IH_OS_LINUX;
35fc84fa 1933 ret = do_bootm_states(cmdtp, flag, argc, argv,
fb1b139b
SG
1934 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
1935 BOOTM_STATE_OS_GO,
d0ae31eb 1936 &images, 1);
f8be7d65 1937
35fc84fa 1938 return ret;
44f074c7
MV
1939}
1940
088f1b19
KP
1941#ifdef CONFIG_SYS_LONGHELP
1942static char bootz_help_text[] =
017e1f3f
MV
1943 "[addr [initrd[:size]] [fdt]]\n"
1944 " - boot Linux zImage stored in memory\n"
44f074c7 1945 "\tThe argument 'initrd' is optional and specifies the address\n"
017e1f3f
MV
1946 "\tof the initrd in memory. The optional argument ':size' allows\n"
1947 "\tspecifying the size of RAW initrd.\n"
44f074c7
MV
1948#if defined(CONFIG_OF_LIBFDT)
1949 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
1950 "\ta third argument is required which is the address of the\n"
1951 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1952 "\tuse a '-' for the second argument. If you do not pass a third\n"
1953 "\ta bd_info struct will be passed instead\n"
1954#endif
088f1b19
KP
1955 "";
1956#endif
1957
1958U_BOOT_CMD(
1959 bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
1960 "boot Linux zImage image from memory", bootz_help_text
44f074c7
MV
1961);
1962#endif /* CONFIG_CMD_BOOTZ */