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