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