]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_bootm.c
cmd_usage(): simplify return code handling
[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>
8bde7f77 39
ebb86c4e 40#if defined(CONFIG_CMD_USB)
3d71c81a
MK
41#include <usb.h>
42#endif
43
6d0f6bcf 44#ifdef CONFIG_SYS_HUSH_PARSER
47d1a6e1
WD
45#include <hush.h>
46#endif
47
54f9c866
KG
48#if defined(CONFIG_OF_LIBFDT)
49#include <fdt.h>
50#include <libfdt.h>
51#include <fdt_support.h>
52#endif
53
fc9c1727 54#ifdef CONFIG_LZMA
fc9c1727 55#include <lzma/LzmaTypes.h>
caf72ff3 56#include <lzma/LzmaDec.h>
fc9c1727
LCM
57#include <lzma/LzmaTools.h>
58#endif /* CONFIG_LZMA */
59
20dde48b
PK
60#ifdef CONFIG_LZO
61#include <linux/lzo.h>
62#endif /* CONFIG_LZO */
63
1ee1180b 64DECLARE_GLOBAL_DATA_PTR;
228f29ac 65
6d0f6bcf
JCPV
66#ifndef CONFIG_SYS_BOOTM_LEN
67#define CONFIG_SYS_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
2abbe075
WD
68#endif
69
321359f2
MB
70#ifdef CONFIG_BZIP2
71extern void bz_internal_error(int);
47d1a6e1
WD
72#endif
73
baa26db4 74#if defined(CONFIG_CMD_IMI)
47d1a6e1
WD
75static int image_info (unsigned long addr);
76#endif
27b207fd 77
baa26db4 78#if defined(CONFIG_CMD_IMLS)
27b207fd 79#include <flash.h>
e6f2e902 80extern flash_info_t flash_info[]; /* info for FLASH chips */
54841ab5 81static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
27b207fd
WD
82#endif
83
1ee1180b
MB
84#ifdef CONFIG_SILENT_CONSOLE
85static void fixup_silent_linux (void);
86#endif
47d1a6e1 87
6986a385
MB
88static image_header_t *image_get_kernel (ulong img_addr, int verify);
89#if defined(CONFIG_FIT)
90static int fit_check_kernel (const void *fit, int os_noffset, int verify);
2262cfee
WD
91#endif
92
54841ab5 93static void *boot_get_kernel (cmd_tbl_t *cmdtp, int flag,int argc, char * const argv[],
8a5ea3e6 94 bootm_headers_t *images, ulong *os_data, ulong *os_len);
54841ab5 95extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
47d1a6e1
WD
96
97/*
98 * Continue booting an OS image; caller already has:
99 * - copied image header to global variable `header'
100 * - checked header magic number, checksums (both header & image),
101 * - verified image architecture (PPC) and type (KERNEL or MULTI),
102 * - loaded (first part of) image to header load address,
103 * - disabled interrupts.
104 */
54841ab5 105typedef int boot_os_fn (int flag, int argc, char * const argv[],
8a5ea3e6 106 bootm_headers_t *images); /* pointers to os/initrd/fdt */
1ee1180b 107
b1d0db18 108#ifdef CONFIG_BOOTM_LINUX
1ee1180b 109extern boot_os_fn do_bootm_linux;
b1d0db18
KG
110#endif
111#ifdef CONFIG_BOOTM_NETBSD
1ee1180b 112static boot_os_fn do_bootm_netbsd;
b1d0db18 113#endif
f13e7b2e 114#if defined(CONFIG_LYNXKDI)
1ee1180b
MB
115static boot_os_fn do_bootm_lynxkdi;
116extern void lynxkdi_boot (image_header_t *);
47d1a6e1 117#endif
b1d0db18 118#ifdef CONFIG_BOOTM_RTEMS
1ee1180b 119static boot_os_fn do_bootm_rtems;
b1d0db18 120#endif
baa26db4 121#if defined(CONFIG_CMD_ELF)
1ee1180b
MB
122static boot_os_fn do_bootm_vxworks;
123static boot_os_fn do_bootm_qnxelf;
54841ab5
WD
124int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
125int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
90253178 126#endif
f5ed9e39
PT
127#if defined(CONFIG_INTEGRITY)
128static boot_os_fn do_bootm_integrity;
129#endif
47d1a6e1 130
0008555f 131static boot_os_fn *boot_os[] = {
b1d0db18 132#ifdef CONFIG_BOOTM_LINUX
be083159 133 [IH_OS_LINUX] = do_bootm_linux,
b1d0db18
KG
134#endif
135#ifdef CONFIG_BOOTM_NETBSD
be083159 136 [IH_OS_NETBSD] = do_bootm_netbsd,
b1d0db18 137#endif
be083159
KG
138#ifdef CONFIG_LYNXKDI
139 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
140#endif
b1d0db18 141#ifdef CONFIG_BOOTM_RTEMS
be083159 142 [IH_OS_RTEMS] = do_bootm_rtems,
b1d0db18 143#endif
be083159
KG
144#if defined(CONFIG_CMD_ELF)
145 [IH_OS_VXWORKS] = do_bootm_vxworks,
146 [IH_OS_QNX] = do_bootm_qnxelf,
147#endif
148#ifdef CONFIG_INTEGRITY
149 [IH_OS_INTEGRITY] = do_bootm_integrity,
150#endif
151};
152
6d0f6bcf 153ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
d5934ad7 154static bootm_headers_t images; /* pointers to os/initrd/fdt images */
15940c9a 155
3dfad40a
KG
156/* Allow for arch specific config before we boot */
157void __arch_preboot_os(void)
158{
159 /* please define platform specific arch_preboot_os() */
160}
161void arch_preboot_os(void) __attribute__((weak, alias("__arch_preboot_os")));
162
c4f9419c
KG
163#if defined(__ARM__)
164 #define IH_INITRD_ARCH IH_ARCH_ARM
165#elif defined(__avr32__)
166 #define IH_INITRD_ARCH IH_ARCH_AVR32
167#elif defined(__bfin__)
168 #define IH_INITRD_ARCH IH_ARCH_BLACKFIN
169#elif defined(__I386__)
170 #define IH_INITRD_ARCH IH_ARCH_I386
171#elif defined(__M68K__)
172 #define IH_INITRD_ARCH IH_ARCH_M68K
173#elif defined(__microblaze__)
174 #define IH_INITRD_ARCH IH_ARCH_MICROBLAZE
175#elif defined(__mips__)
176 #define IH_INITRD_ARCH IH_ARCH_MIPS
c4f9419c
KG
177#elif defined(__nios2__)
178 #define IH_INITRD_ARCH IH_ARCH_NIOS2
179#elif defined(__PPC__)
180 #define IH_INITRD_ARCH IH_ARCH_PPC
181#elif defined(__sh__)
182 #define IH_INITRD_ARCH IH_ARCH_SH
183#elif defined(__sparc__)
184 #define IH_INITRD_ARCH IH_ARCH_SPARC
185#else
186# error Unknown CPU type
187#endif
47d1a6e1 188
a16028da 189static void bootm_start_lmb(void)
47d1a6e1 190{
a16028da 191#ifdef CONFIG_LMB
391fd93a
BB
192 ulong mem_start;
193 phys_size_t mem_size;
47d1a6e1 194
e906cfae 195 lmb_init(&images.lmb);
47d1a6e1 196
d3f2fa0d
KG
197 mem_start = getenv_bootm_low();
198 mem_size = getenv_bootm_size();
47d1a6e1 199
e906cfae 200 lmb_add(&images.lmb, (phys_addr_t)mem_start, mem_size);
47d1a6e1 201
76da19df 202 arch_lmb_reserve(&images.lmb);
e906cfae 203 board_lmb_reserve(&images.lmb);
a16028da
MF
204#else
205# define lmb_reserve(lmb, base, size)
206#endif
207}
208
54841ab5 209static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
a16028da
MF
210{
211 void *os_hdr;
212 int ret;
213
214 memset ((void *)&images, 0, sizeof (images));
215 images.verify = getenv_yesno ("verify");
216
217 bootm_start_lmb();
47d1a6e1 218
5cf746c3 219 /* get kernel image header, start address and length */
9a4daad0 220 os_hdr = boot_get_kernel (cmdtp, flag, argc, argv,
396f635b
KG
221 &images, &images.os.image_start, &images.os.image_len);
222 if (images.os.image_len == 0) {
6986a385 223 puts ("ERROR: can't get kernel image!\n");
47d1a6e1 224 return 1;
bccae903 225 }
bccae903 226
d5934ad7 227 /* get image parameters */
9a4daad0 228 switch (genimg_get_format (os_hdr)) {
d5934ad7 229 case IMAGE_FORMAT_LEGACY:
396f635b
KG
230 images.os.type = image_get_type (os_hdr);
231 images.os.comp = image_get_comp (os_hdr);
232 images.os.os = image_get_os (os_hdr);
bccae903 233
396f635b
KG
234 images.os.end = image_get_image_end (os_hdr);
235 images.os.load = image_get_load (os_hdr);
d5934ad7
MB
236 break;
237#if defined(CONFIG_FIT)
238 case IMAGE_FORMAT_FIT:
3dfe1101 239 if (fit_image_get_type (images.fit_hdr_os,
396f635b 240 images.fit_noffset_os, &images.os.type)) {
6986a385 241 puts ("Can't get image type!\n");
1372cce2 242 show_boot_progress (-109);
6986a385
MB
243 return 1;
244 }
47d1a6e1 245
3dfe1101 246 if (fit_image_get_comp (images.fit_hdr_os,
396f635b 247 images.fit_noffset_os, &images.os.comp)) {
6986a385 248 puts ("Can't get image compression!\n");
1372cce2 249 show_boot_progress (-110);
6986a385
MB
250 return 1;
251 }
47d1a6e1 252
3dfe1101 253 if (fit_image_get_os (images.fit_hdr_os,
396f635b 254 images.fit_noffset_os, &images.os.os)) {
6986a385 255 puts ("Can't get image OS!\n");
1372cce2 256 show_boot_progress (-111);
47d1a6e1
WD
257 return 1;
258 }
47d1a6e1 259
396f635b 260 images.os.end = fit_get_end (images.fit_hdr_os);
6986a385 261
3dfe1101 262 if (fit_image_get_load (images.fit_hdr_os, images.fit_noffset_os,
396f635b 263 &images.os.load)) {
6986a385 264 puts ("Can't get image load address!\n");
1372cce2 265 show_boot_progress (-112);
6986a385 266 return 1;
b13fb01a
WD
267 }
268 break;
d5934ad7
MB
269#endif
270 default:
271 puts ("ERROR: unknown image format type!\n");
47d1a6e1
WD
272 return 1;
273 }
d5934ad7 274
c160a954
KG
275 /* find kernel entry point */
276 if (images.legacy_hdr_valid) {
277 images.ep = image_get_ep (&images.legacy_hdr_os_copy);
278#if defined(CONFIG_FIT)
279 } else if (images.fit_uname_os) {
280 ret = fit_image_get_entry (images.fit_hdr_os,
281 images.fit_noffset_os, &images.ep);
282 if (ret) {
283 puts ("Can't get entry point property!\n");
284 return 1;
285 }
286#endif
287 } else {
288 puts ("Could not find kernel entry point!\n");
289 return 1;
290 }
291
24de2f4b
HS
292 if (((images.os.type == IH_TYPE_KERNEL) ||
293 (images.os.type == IH_TYPE_MULTI)) &&
8b828a8f 294 (images.os.os == IH_OS_LINUX)) {
c4f9419c
KG
295 /* find ramdisk */
296 ret = boot_get_ramdisk (argc, argv, &images, IH_INITRD_ARCH,
297 &images.rd_start, &images.rd_end);
298 if (ret) {
ea86b9e6 299 puts ("Ramdisk image is corrupt or invalid\n");
c4f9419c
KG
300 return 1;
301 }
06a09918
KG
302
303#if defined(CONFIG_OF_LIBFDT)
1d9af0be 304#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
06a09918
KG
305 /* find flattened device tree */
306 ret = boot_get_fdt (flag, argc, argv, &images,
307 &images.ft_addr, &images.ft_len);
308 if (ret) {
309 puts ("Could not find a valid device tree\n");
310 return 1;
311 }
54f9c866
KG
312
313 set_working_fdt_addr(images.ft_addr);
1d9af0be 314#endif
06a09918 315#endif
c4f9419c
KG
316 }
317
396f635b 318 images.os.start = (ulong)os_hdr;
49c3a861 319 images.state = BOOTM_STATE_START;
47d1a6e1 320
396f635b
KG
321 return 0;
322}
3d71c81a 323
396f635b
KG
324#define BOOTM_ERR_RESET -1
325#define BOOTM_ERR_OVERLAP -2
326#define BOOTM_ERR_UNIMPLEMENTED -3
327static int bootm_load_os(image_info_t os, ulong *load_end, int boot_progress)
328{
329 uint8_t comp = os.comp;
330 ulong load = os.load;
331 ulong blob_start = os.start;
332 ulong blob_end = os.end;
333 ulong image_start = os.image_start;
334 ulong image_len = os.image_len;
6d0f6bcf 335 uint unc_len = CONFIG_SYS_BOOTM_LEN;
3d71c81a 336
396f635b 337 const char *type_name = genimg_get_type_name (os.type);
c7de829c 338
d5934ad7 339 switch (comp) {
47d1a6e1 340 case IH_COMP_NONE:
396f635b 341 if (load == blob_start) {
42b73e8e 342 printf (" XIP %s ... ", type_name);
47d1a6e1 343 } else {
42b73e8e 344 printf (" Loading %s ... ", type_name);
54fa2c5b
LJ
345 memmove_wd ((void *)load, (void *)image_start,
346 image_len, CHUNKSZ);
47d1a6e1 347 }
396f635b 348 *load_end = load + image_len;
2e752be3 349 puts("OK\n");
47d1a6e1 350 break;
44431cab 351#ifdef CONFIG_GZIP
47d1a6e1 352 case IH_COMP_GZIP:
42b73e8e 353 printf (" Uncompressing %s ... ", type_name);
396f635b
KG
354 if (gunzip ((void *)load, unc_len,
355 (uchar *)image_start, &image_len) != 0) {
107b801c 356 puts ("GUNZIP: uncompress, out-of-mem or overwrite error "
2682ce8a 357 "- must RESET board to recover\n");
396f635b
KG
358 if (boot_progress)
359 show_boot_progress (-6);
360 return BOOTM_ERR_RESET;
47d1a6e1 361 }
7582438c 362
396f635b 363 *load_end = load + image_len;
47d1a6e1 364 break;
44431cab 365#endif /* CONFIG_GZIP */
c29fdfc1
WD
366#ifdef CONFIG_BZIP2
367 case IH_COMP_BZIP2:
42b73e8e 368 printf (" Uncompressing %s ... ", type_name);
5653fc33
WD
369 /*
370 * If we've got less than 4 MB of malloc() space,
371 * use slower decompression algorithm which requires
372 * at most 2300 KB of memory.
373 */
396f635b
KG
374 int i = BZ2_bzBuffToBuffDecompress ((char*)load,
375 &unc_len, (char *)image_start, image_len,
6d0f6bcf 376 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
c29fdfc1 377 if (i != BZ_OK) {
2682ce8a
MB
378 printf ("BUNZIP2: uncompress or overwrite error %d "
379 "- must RESET board to recover\n", i);
396f635b
KG
380 if (boot_progress)
381 show_boot_progress (-6);
382 return BOOTM_ERR_RESET;
c29fdfc1 383 }
7582438c 384
396f635b 385 *load_end = load + unc_len;
c29fdfc1
WD
386 break;
387#endif /* CONFIG_BZIP2 */
fc9c1727
LCM
388#ifdef CONFIG_LZMA
389 case IH_COMP_LZMA:
390 printf (" Uncompressing %s ... ", type_name);
391
392 int ret = lzmaBuffToBuffDecompress(
393 (unsigned char *)load, &unc_len,
d977a573 394 (unsigned char *)image_start, image_len);
caf72ff3 395 if (ret != SZ_OK) {
fc9c1727
LCM
396 printf ("LZMA: uncompress or overwrite error %d "
397 "- must RESET board to recover\n", ret);
398 show_boot_progress (-6);
399 return BOOTM_ERR_RESET;
400 }
401 *load_end = load + unc_len;
402 break;
403#endif /* CONFIG_LZMA */
20dde48b
PK
404#ifdef CONFIG_LZO
405 case IH_COMP_LZO:
406 printf (" Uncompressing %s ... ", type_name);
407
408 int ret = lzop_decompress((const unsigned char *)image_start,
409 image_len, (unsigned char *)load,
410 &unc_len);
411 if (ret != LZO_E_OK) {
412 printf ("LZO: uncompress or overwrite error %d "
413 "- must RESET board to recover\n", ret);
414 if (boot_progress)
415 show_boot_progress (-6);
416 return BOOTM_ERR_RESET;
417 }
418
419 *load_end = load + unc_len;
420 break;
421#endif /* CONFIG_LZO */
47d1a6e1 422 default:
d5934ad7 423 printf ("Unimplemented compression type %d\n", comp);
396f635b 424 return BOOTM_ERR_UNIMPLEMENTED;
47d1a6e1 425 }
4b9206ed 426 puts ("OK\n");
54b4ab3c 427 debug (" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
396f635b
KG
428 if (boot_progress)
429 show_boot_progress (7);
47d1a6e1 430
396f635b
KG
431 if ((load < blob_end) && (*load_end > blob_start)) {
432 debug ("images.os.start = 0x%lX, images.os.end = 0x%lx\n", blob_start, blob_end);
54b4ab3c 433 debug ("images.os.load = 0x%lx, load_end = 0x%lx\n", load, *load_end);
47d1a6e1 434
396f635b
KG
435 return BOOTM_ERR_OVERLAP;
436 }
437
438 return 0;
439}
440
54841ab5 441static int bootm_start_standalone(ulong iflag, int argc, char * const argv[])
f97ec30b
DZ
442{
443 char *s;
54841ab5 444 int (*appl)(int, char * const []);
f97ec30b
DZ
445
446 /* Don't start if "autostart" is set to "no" */
447 if (((s = getenv("autostart")) != NULL) && (strcmp(s, "no") == 0)) {
448 char buf[32];
449 sprintf(buf, "%lX", images.os.image_len);
450 setenv("filesize", buf);
451 return 0;
452 }
54841ab5 453 appl = (int (*)(int, char * const []))ntohl(images.ep);
f97ec30b
DZ
454 (*appl)(argc-1, &argv[1]);
455
456 return 0;
457}
458
49c3a861
KG
459/* we overload the cmd field with our state machine info instead of a
460 * function pointer */
f74d9bd2 461static cmd_tbl_t cmd_bootm_sub[] = {
49c3a861
KG
462 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
463 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
464#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
465 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
466#endif
467#ifdef CONFIG_OF_LIBFDT
468 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
469#endif
49c3a861 470 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
224c90d1 471 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
49c3a861
KG
472 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
473 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
474};
475
54841ab5 476int do_bootm_subcommand (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
49c3a861
KG
477{
478 int ret = 0;
479 int state;
480 cmd_tbl_t *c;
481 boot_os_fn *boot_fn;
482
483 c = find_cmd_tbl(argv[1], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
484
485 if (c) {
486 state = (int)c->cmd;
487
488 /* treat start special since it resets the state machine */
489 if (state == BOOTM_STATE_START) {
490 argc--;
491 argv++;
492 return bootm_start(cmdtp, flag, argc, argv);
493 }
47e26b1b
WD
494 } else {
495 /* Unrecognized command */
496 return cmd_usage(cmdtp);
49c3a861
KG
497 }
498
499 if (images.state >= state) {
500 printf ("Trying to execute a command out of order\n");
47e26b1b 501 return cmd_usage(cmdtp);
49c3a861
KG
502 }
503
504 images.state |= state;
505 boot_fn = boot_os[images.os.os];
506
507 switch (state) {
508 ulong load_end;
509 case BOOTM_STATE_START:
510 /* should never occur */
511 break;
512 case BOOTM_STATE_LOADOS:
513 ret = bootm_load_os(images.os, &load_end, 0);
514 if (ret)
515 return ret;
516
517 lmb_reserve(&images.lmb, images.os.load,
518 (load_end - images.os.load));
519 break;
520#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
521 case BOOTM_STATE_RAMDISK:
522 {
523 ulong rd_len = images.rd_end - images.rd_start;
524 char str[17];
525
526 ret = boot_ramdisk_high(&images.lmb, images.rd_start,
527 rd_len, &images.initrd_start, &images.initrd_end);
528 if (ret)
529 return ret;
530
531 sprintf(str, "%lx", images.initrd_start);
532 setenv("initrd_start", str);
533 sprintf(str, "%lx", images.initrd_end);
534 setenv("initrd_end", str);
535 }
536 break;
537#endif
b25e38fc 538#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_SYS_BOOTMAPSZ)
49c3a861
KG
539 case BOOTM_STATE_FDT:
540 {
541 ulong bootmap_base = getenv_bootm_low();
542 ret = boot_relocate_fdt(&images.lmb, bootmap_base,
543 &images.ft_addr, &images.ft_len);
544 break;
545 }
546#endif
547 case BOOTM_STATE_OS_CMDLINE:
548 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, &images);
549 if (ret)
550 printf ("cmdline subcommand not supported\n");
551 break;
552 case BOOTM_STATE_OS_BD_T:
553 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, &images);
554 if (ret)
555 printf ("bdt subcommand not supported\n");
556 break;
557 case BOOTM_STATE_OS_PREP:
558 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, &images);
559 if (ret)
560 printf ("prep subcommand not supported\n");
561 break;
562 case BOOTM_STATE_OS_GO:
563 disable_interrupts();
3dfad40a 564 arch_preboot_os();
49c3a861
KG
565 boot_fn(BOOTM_STATE_OS_GO, argc, argv, &images);
566 break;
567 }
568
569 return ret;
570}
571
396f635b
KG
572/*******************************************************************/
573/* bootm - boot application image from image in memory */
574/*******************************************************************/
be083159 575
54841ab5 576int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
396f635b 577{
396f635b
KG
578 ulong iflag;
579 ulong load_end = 0;
580 int ret;
be083159 581 boot_os_fn *boot_fn;
521af04d
PT
582#ifndef CONFIG_RELOC_FIXUP_WORKS
583 static int relocated = 0;
be083159
KG
584
585 /* relocate boot function table */
586 if (!relocated) {
587 int i;
588 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
ca95c9df
DZ
589 if (boot_os[i] != NULL)
590 boot_os[i] += gd->reloc_off;
be083159
KG
591 relocated = 1;
592 }
521af04d 593#endif
396f635b 594
49c3a861
KG
595 /* determine if we have a sub command */
596 if (argc > 1) {
597 char *endp;
598
599 simple_strtoul(argv[1], &endp, 16);
600 /* endp pointing to NULL means that argv[1] was just a
601 * valid number, pass it along to the normal bootm processing
602 *
603 * If endp is ':' or '#' assume a FIT identifier so pass
604 * along for normal processing.
605 *
606 * Right now we assume the first arg should never be '-'
607 */
608 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
609 return do_bootm_subcommand(cmdtp, flag, argc, argv);
610 }
611
8e02494e
AG
612 if (bootm_start(cmdtp, flag, argc, argv))
613 return 1;
396f635b
KG
614
615 /*
616 * We have reached the point of no return: we are going to
617 * overwrite all exception vector code, so we cannot easily
618 * recover from any failures any more...
619 */
620 iflag = disable_interrupts();
621
622#if defined(CONFIG_CMD_USB)
623 /*
624 * turn off USB to prevent the host controller from writing to the
625 * SDRAM while Linux is booting. This could happen (at least for OHCI
626 * controller), because the HCCA (Host Controller Communication Area)
627 * lies within the SDRAM and the host controller writes continously to
628 * this area (as busmaster!). The HccaFrameNumber is for example
629 * updated every 1 ms within the HCCA structure in SDRAM! For more
630 * details see the OpenHCI specification.
631 */
632 usb_stop();
633#endif
634
396f635b
KG
635 ret = bootm_load_os(images.os, &load_end, 1);
636
637 if (ret < 0) {
638 if (ret == BOOTM_ERR_RESET)
cb1c4896 639 do_reset (cmdtp, flag, argc, argv);
396f635b
KG
640 if (ret == BOOTM_ERR_OVERLAP) {
641 if (images.legacy_hdr_valid) {
642 if (image_get_type (&images.legacy_hdr_os_copy) == IH_TYPE_MULTI)
643 puts ("WARNING: legacy format multi component "
644 "image overwritten\n");
645 } else {
646 puts ("ERROR: new format image overwritten - "
647 "must RESET the board to recover\n");
648 show_boot_progress (-113);
649 do_reset (cmdtp, flag, argc, argv);
650 }
651 }
652 if (ret == BOOTM_ERR_UNIMPLEMENTED) {
653 if (iflag)
654 enable_interrupts();
655 show_boot_progress (-7);
656 return 1;
cb1c4896 657 }
47d1a6e1 658 }
7582438c 659
396f635b 660 lmb_reserve(&images.lmb, images.os.load, (load_end - images.os.load));
47d1a6e1 661
f97ec30b
DZ
662 if (images.os.type == IH_TYPE_STANDALONE) {
663 if (iflag)
664 enable_interrupts();
665 /* This may return when 'autostart' is 'no' */
666 bootm_start_standalone(iflag, argc, argv);
667 return 0;
668 }
669
396f635b 670 show_boot_progress (8);
4ed6552f 671
f72da340 672#ifdef CONFIG_SILENT_CONSOLE
be083159
KG
673 if (images.os.os == IH_OS_LINUX)
674 fixup_silent_linux();
1f4bb37d
WD
675#endif
676
be083159 677 boot_fn = boot_os[images.os.os];
ca95c9df
DZ
678
679 if (boot_fn == NULL) {
680 if (iflag)
681 enable_interrupts();
682 printf ("ERROR: booting os '%s' (%d) is not supported\n",
683 genimg_get_os_name(images.os.os), images.os.os);
684 show_boot_progress (-8);
685 return 1;
686 }
687
3dfad40a
KG
688 arch_preboot_os();
689
be083159 690 boot_fn(0, argc, argv, &images);
47d1a6e1 691
fad63407 692 show_boot_progress (-9);
47d1a6e1 693#ifdef DEBUG
4b9206ed 694 puts ("\n## Control returned to monitor - resetting...\n");
47d1a6e1 695#endif
40d7e99d 696 do_reset (cmdtp, flag, argc, argv);
a44a269a 697
47d1a6e1
WD
698 return 1;
699}
700
1efd4360 701/**
9a4daad0
MB
702 * image_get_kernel - verify legacy format kernel image
703 * @img_addr: in RAM address of the legacy format image to be verified
704 * @verify: data CRC verification flag
1efd4360 705 *
9a4daad0
MB
706 * image_get_kernel() verifies legacy image integrity and returns pointer to
707 * legacy image header if image verification was completed successfully.
1efd4360
MB
708 *
709 * returns:
9a4daad0
MB
710 * pointer to a legacy image header if valid image was found
711 * otherwise return NULL
1efd4360
MB
712 */
713static image_header_t *image_get_kernel (ulong img_addr, int verify)
f72da340 714{
1efd4360 715 image_header_t *hdr = (image_header_t *)img_addr;
f72da340 716
1efd4360
MB
717 if (!image_check_magic(hdr)) {
718 puts ("Bad Magic Number\n");
719 show_boot_progress (-1);
720 return NULL;
721 }
722 show_boot_progress (2);
f72da340 723
1efd4360
MB
724 if (!image_check_hcrc (hdr)) {
725 puts ("Bad Header Checksum\n");
726 show_boot_progress (-2);
727 return NULL;
728 }
729
730 show_boot_progress (3);
731 image_print_contents (hdr);
732
733 if (verify) {
734 puts (" Verifying Checksum ... ");
735 if (!image_check_dcrc (hdr)) {
736 printf ("Bad Data CRC\n");
737 show_boot_progress (-3);
738 return NULL;
f72da340 739 }
1efd4360 740 puts ("OK\n");
f72da340 741 }
1efd4360 742 show_boot_progress (4);
f72da340 743
1efd4360
MB
744 if (!image_check_target_arch (hdr)) {
745 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
746 show_boot_progress (-4);
747 return NULL;
748 }
749 return hdr;
f72da340 750}
f72da340 751
6986a385
MB
752/**
753 * fit_check_kernel - verify FIT format kernel subimage
754 * @fit_hdr: pointer to the FIT image header
755 * os_noffset: kernel subimage node offset within FIT image
756 * @verify: data CRC verification flag
757 *
758 * fit_check_kernel() verifies integrity of the kernel subimage and from
759 * specified FIT image.
760 *
761 * returns:
762 * 1, on success
763 * 0, on failure
764 */
765#if defined (CONFIG_FIT)
766static int fit_check_kernel (const void *fit, int os_noffset, int verify)
47d1a6e1 767{
6986a385 768 fit_image_print (fit, os_noffset, " ");
47d1a6e1 769
6986a385
MB
770 if (verify) {
771 puts (" Verifying Hash Integrity ... ");
772 if (!fit_image_check_hashes (fit, os_noffset)) {
773 puts ("Bad Data Hash\n");
1372cce2 774 show_boot_progress (-104);
6986a385
MB
775 return 0;
776 }
777 puts ("OK\n");
47d1a6e1 778 }
1372cce2 779 show_boot_progress (105);
47d1a6e1 780
6986a385
MB
781 if (!fit_image_check_target_arch (fit, os_noffset)) {
782 puts ("Unsupported Architecture\n");
1372cce2 783 show_boot_progress (-105);
6986a385
MB
784 return 0;
785 }
56f94be3 786
1372cce2 787 show_boot_progress (106);
6986a385
MB
788 if (!fit_image_check_type (fit, os_noffset, IH_TYPE_KERNEL)) {
789 puts ("Not a kernel image\n");
1372cce2 790 show_boot_progress (-106);
6986a385
MB
791 return 0;
792 }
47d1a6e1 793
1372cce2 794 show_boot_progress (107);
6986a385
MB
795 return 1;
796}
797#endif /* CONFIG_FIT */
47d1a6e1 798
5cf746c3 799/**
9a4daad0 800 * boot_get_kernel - find kernel image
5cf746c3
MB
801 * @os_data: pointer to a ulong variable, will hold os data start address
802 * @os_len: pointer to a ulong variable, will hold os data length
803 *
9a4daad0 804 * boot_get_kernel() tries to find a kernel image, verifies its integrity
5cf746c3
MB
805 * and locates kernel data.
806 *
807 * returns:
808 * pointer to image header if valid image was found, plus kernel start
809 * address and length, otherwise NULL
810 */
54841ab5 811static void *boot_get_kernel (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[],
8a5ea3e6 812 bootm_headers_t *images, ulong *os_data, ulong *os_len)
5cf746c3
MB
813{
814 image_header_t *hdr;
815 ulong img_addr;
d5934ad7
MB
816#if defined(CONFIG_FIT)
817 void *fit_hdr;
818 const char *fit_uname_config = NULL;
819 const char *fit_uname_kernel = NULL;
6986a385
MB
820 const void *data;
821 size_t len;
f773bea8 822 int cfg_noffset;
6986a385 823 int os_noffset;
d5934ad7 824#endif
56f94be3 825
d5934ad7 826 /* find out kernel image address */
5cf746c3
MB
827 if (argc < 2) {
828 img_addr = load_addr;
d5934ad7
MB
829 debug ("* kernel: default image load address = 0x%08lx\n",
830 load_addr);
831#if defined(CONFIG_FIT)
832 } else if (fit_parse_conf (argv[1], load_addr, &img_addr,
833 &fit_uname_config)) {
834 debug ("* kernel: config '%s' from image at 0x%08lx\n",
835 fit_uname_config, img_addr);
836 } else if (fit_parse_subimage (argv[1], load_addr, &img_addr,
837 &fit_uname_kernel)) {
838 debug ("* kernel: subimage '%s' from image at 0x%08lx\n",
839 fit_uname_kernel, img_addr);
840#endif
5cf746c3
MB
841 } else {
842 img_addr = simple_strtoul(argv[1], NULL, 16);
d5934ad7 843 debug ("* kernel: cmdline image address = 0x%08lx\n", img_addr);
5cf746c3 844 }
47d1a6e1 845
5cf746c3 846 show_boot_progress (1);
56f94be3 847
fff888a1 848 /* copy from dataflash if needed */
9a4daad0 849 img_addr = genimg_get_image (img_addr);
5cf746c3 850
d5934ad7 851 /* check image type, for FIT images get FIT kernel node */
6986a385 852 *os_data = *os_len = 0;
9a4daad0 853 switch (genimg_get_format ((void *)img_addr)) {
d5934ad7 854 case IMAGE_FORMAT_LEGACY:
6986a385
MB
855 printf ("## Booting kernel from Legacy Image at %08lx ...\n",
856 img_addr);
1efd4360
MB
857 hdr = image_get_kernel (img_addr, images->verify);
858 if (!hdr)
d5934ad7 859 return NULL;
d5934ad7
MB
860 show_boot_progress (5);
861
6986a385 862 /* get os_data and os_len */
d5934ad7
MB
863 switch (image_get_type (hdr)) {
864 case IH_TYPE_KERNEL:
865 *os_data = image_get_data (hdr);
866 *os_len = image_get_data_size (hdr);
867 break;
868 case IH_TYPE_MULTI:
869 image_multi_getimg (hdr, 0, os_data, os_len);
870 break;
f97ec30b 871 case IH_TYPE_STANDALONE:
f97ec30b
DZ
872 *os_data = image_get_data (hdr);
873 *os_len = image_get_data_size (hdr);
874 break;
d5934ad7
MB
875 default:
876 printf ("Wrong Image Type for %s command\n", cmdtp->name);
877 show_boot_progress (-5);
878 return NULL;
879 }
cb1c4896
MB
880
881 /*
882 * copy image header to allow for image overwrites during kernel
883 * decompression.
884 */
885 memmove (&images->legacy_hdr_os_copy, hdr, sizeof(image_header_t));
886
887 /* save pointer to image header */
d5934ad7 888 images->legacy_hdr_os = hdr;
47d1a6e1 889
cb1c4896 890 images->legacy_hdr_valid = 1;
1372cce2 891 show_boot_progress (6);
5cf746c3 892 break;
d5934ad7
MB
893#if defined(CONFIG_FIT)
894 case IMAGE_FORMAT_FIT:
895 fit_hdr = (void *)img_addr;
6986a385
MB
896 printf ("## Booting kernel from FIT Image at %08lx ...\n",
897 img_addr);
898
899 if (!fit_check_format (fit_hdr)) {
900 puts ("Bad FIT kernel image format!\n");
1372cce2 901 show_boot_progress (-100);
6986a385
MB
902 return NULL;
903 }
1372cce2 904 show_boot_progress (100);
47d1a6e1 905
6986a385
MB
906 if (!fit_uname_kernel) {
907 /*
908 * no kernel image node unit name, try to get config
909 * node first. If config unit node name is NULL
910 * fit_conf_get_node() will try to find default config node
911 */
1372cce2 912 show_boot_progress (101);
f773bea8
MB
913 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
914 if (cfg_noffset < 0) {
1372cce2 915 show_boot_progress (-101);
6986a385 916 return NULL;
1372cce2 917 }
f773bea8
MB
918 /* save configuration uname provided in the first
919 * bootm argument
920 */
921 images->fit_uname_cfg = fdt_get_name (fit_hdr, cfg_noffset, NULL);
922 printf (" Using '%s' configuration\n", images->fit_uname_cfg);
923 show_boot_progress (103);
47d1a6e1 924
f773bea8 925 os_noffset = fit_conf_get_kernel_node (fit_hdr, cfg_noffset);
6986a385
MB
926 fit_uname_kernel = fit_get_name (fit_hdr, os_noffset, NULL);
927 } else {
928 /* get kernel component image node offset */
1372cce2 929 show_boot_progress (102);
6986a385
MB
930 os_noffset = fit_image_get_node (fit_hdr, fit_uname_kernel);
931 }
1372cce2
MB
932 if (os_noffset < 0) {
933 show_boot_progress (-103);
6986a385 934 return NULL;
1372cce2 935 }
47d1a6e1 936
6986a385 937 printf (" Trying '%s' kernel subimage\n", fit_uname_kernel);
47d1a6e1 938
1372cce2 939 show_boot_progress (104);
6986a385
MB
940 if (!fit_check_kernel (fit_hdr, os_noffset, images->verify))
941 return NULL;
47d1a6e1 942
6986a385
MB
943 /* get kernel image data address and length */
944 if (fit_image_get_data (fit_hdr, os_noffset, &data, &len)) {
945 puts ("Could not find kernel subimage data!\n");
1372cce2 946 show_boot_progress (-107);
6986a385
MB
947 return NULL;
948 }
1372cce2 949 show_boot_progress (108);
47d1a6e1 950
6986a385
MB
951 *os_len = len;
952 *os_data = (ulong)data;
953 images->fit_hdr_os = fit_hdr;
954 images->fit_uname_os = fit_uname_kernel;
3dfe1101 955 images->fit_noffset_os = os_noffset;
6986a385 956 break;
9c4c5ae3 957#endif
5cf746c3 958 default:
d5934ad7 959 printf ("Wrong Image Format for %s command\n", cmdtp->name);
1372cce2 960 show_boot_progress (-108);
5cf746c3 961 return NULL;
47d1a6e1
WD
962 }
963
06c53bea 964 debug (" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
6986a385 965 *os_data, *os_len, *os_len);
47d1a6e1 966
d5934ad7 967 return (void *)img_addr;
5cf746c3 968}
98a9c4d4 969
0d498393 970U_BOOT_CMD(
6d0f6bcf 971 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
2fb2604d 972 "boot application image from memory",
1ee1180b
MB
973 "[addr [arg ...]]\n - boot application image stored in memory\n"
974 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
975 "\t'arg' can be the address of an initrd image\n"
4a2ad5ff 976#if defined(CONFIG_OF_LIBFDT)
98a9c4d4 977 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
5441f61a 978 "\ta third argument is required which is the address of the\n"
98a9c4d4
MM
979 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
980 "\tuse a '-' for the second argument. If you do not pass a third\n"
981 "\ta bd_info struct will be passed instead\n"
98a9c4d4 982#endif
6986a385
MB
983#if defined(CONFIG_FIT)
984 "\t\nFor the new multi component uImage format (FIT) addresses\n"
985 "\tmust be extened to include component or configuration unit name:\n"
986 "\taddr:<subimg_uname> - direct component image specification\n"
987 "\taddr#<conf_uname> - configuration specification\n"
988 "\tUse iminfo command to get the list of existing component\n"
989 "\timages and configurations.\n"
990#endif
49c3a861
KG
991 "\nSub-commands to do part of the bootm sequence. The sub-commands "
992 "must be\n"
993 "issued in the order below (it's ok to not issue all sub-commands):\n"
994 "\tstart [addr [arg ...]]\n"
995 "\tloados - load OS image\n"
996#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
997 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
998#endif
999#if defined(CONFIG_OF_LIBFDT)
1000 "\tfdt - relocate flat device tree\n"
1001#endif
49c3a861 1002 "\tcmdline - OS specific command line processing/setup\n"
224c90d1 1003 "\tbdt - OS specific bd_t processing\n"
49c3a861 1004 "\tprep - OS specific prep before relocation or go\n"
a89c33db 1005 "\tgo - start OS"
8bde7f77 1006);
47d1a6e1 1007
1ee1180b
MB
1008/*******************************************************************/
1009/* bootd - boot default image */
1010/*******************************************************************/
baa26db4 1011#if defined(CONFIG_CMD_BOOTD)
54841ab5 1012int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
47d1a6e1
WD
1013{
1014 int rcode = 0;
47d1a6e1 1015
6d0f6bcf 1016#ifndef CONFIG_SYS_HUSH_PARSER
1ee1180b
MB
1017 if (run_command (getenv ("bootcmd"), flag) < 0)
1018 rcode = 1;
47d1a6e1 1019#else
1ee1180b
MB
1020 if (parse_string_outer (getenv ("bootcmd"),
1021 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
1022 rcode = 1;
47d1a6e1
WD
1023#endif
1024 return rcode;
1025}
47d1a6e1 1026
0d498393 1027U_BOOT_CMD(
1ee1180b 1028 boot, 1, 1, do_bootd,
2fb2604d 1029 "boot default, i.e., run 'bootcmd'",
a89c33db 1030 ""
9d2b18a0 1031);
47d1a6e1 1032
9d2b18a0 1033/* keep old command name "bootd" for backward compatibility */
0d498393 1034U_BOOT_CMD(
1ee1180b 1035 bootd, 1, 1, do_bootd,
2fb2604d 1036 "boot default, i.e., run 'bootcmd'",
a89c33db 1037 ""
8bde7f77 1038);
47d1a6e1 1039
47d1a6e1 1040#endif
47d1a6e1 1041
47d1a6e1 1042
1ee1180b
MB
1043/*******************************************************************/
1044/* iminfo - print header info for a requested image */
1045/*******************************************************************/
baa26db4 1046#if defined(CONFIG_CMD_IMI)
54841ab5 1047int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
47d1a6e1
WD
1048{
1049 int arg;
1050 ulong addr;
1ee1180b 1051 int rcode = 0;
47d1a6e1 1052
47d1a6e1
WD
1053 if (argc < 2) {
1054 return image_info (load_addr);
1055 }
47d1a6e1 1056
1ee1180b
MB
1057 for (arg = 1; arg < argc; ++arg) {
1058 addr = simple_strtoul (argv[arg], NULL, 16);
1059 if (image_info (addr) != 0)
1060 rcode = 1;
47d1a6e1
WD
1061 }
1062 return rcode;
1063}
47d1a6e1 1064
47d1a6e1
WD
1065static int image_info (ulong addr)
1066{
d5934ad7 1067 void *hdr = (void *)addr;
47d1a6e1 1068
47d1a6e1 1069 printf ("\n## Checking Image at %08lx ...\n", addr);
47d1a6e1 1070
9a4daad0 1071 switch (genimg_get_format (hdr)) {
d5934ad7
MB
1072 case IMAGE_FORMAT_LEGACY:
1073 puts (" Legacy image found\n");
1074 if (!image_check_magic (hdr)) {
1075 puts (" Bad Magic Number\n");
1076 return 1;
1077 }
47d1a6e1 1078
d5934ad7
MB
1079 if (!image_check_hcrc (hdr)) {
1080 puts (" Bad Header Checksum\n");
1081 return 1;
47d1a6e1
WD
1082 }
1083
d5934ad7 1084 image_print_contents (hdr);
47d1a6e1 1085
d5934ad7
MB
1086 puts (" Verifying Checksum ... ");
1087 if (!image_check_dcrc (hdr)) {
1088 puts (" Bad Data CRC\n");
1089 return 1;
47d1a6e1 1090 }
d5934ad7
MB
1091 puts ("OK\n");
1092 return 0;
1093#if defined(CONFIG_FIT)
1094 case IMAGE_FORMAT_FIT:
1095 puts (" FIT image found\n");
47d1a6e1 1096
e32fea6a
MB
1097 if (!fit_check_format (hdr)) {
1098 puts ("Bad FIT image format!\n");
1099 return 1;
47d1a6e1
WD
1100 }
1101
e32fea6a 1102 fit_print_contents (hdr);
a4f24345
BS
1103
1104 if (!fit_all_image_check_hashes (hdr)) {
1105 puts ("Bad hash in FIT image!\n");
1106 return 1;
1107 }
1108
d5934ad7
MB
1109 return 0;
1110#endif
1111 default:
1112 puts ("Unknown image format!\n");
1113 break;
47d1a6e1
WD
1114 }
1115
d5934ad7 1116 return 1;
47d1a6e1 1117}
0d498393
WD
1118
1119U_BOOT_CMD(
6d0f6bcf 1120 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
2fb2604d 1121 "print header information for application image",
8bde7f77
WD
1122 "addr [addr ...]\n"
1123 " - print header information for application image starting at\n"
1124 " address 'addr' in memory; this includes verification of the\n"
a89c33db 1125 " image contents (magic number, header and payload checksums)"
8bde7f77 1126);
25c751e9 1127#endif
87a449c8 1128
25c751e9 1129
1ee1180b
MB
1130/*******************************************************************/
1131/* imls - list all images found in flash */
1132/*******************************************************************/
baa26db4 1133#if defined(CONFIG_CMD_IMLS)
54841ab5 1134int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
27b207fd
WD
1135{
1136 flash_info_t *info;
1137 int i, j;
d5934ad7 1138 void *hdr;
25c751e9 1139
1ee1180b 1140 for (i = 0, info = &flash_info[0];
6d0f6bcf 1141 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
25c751e9 1142
27b207fd
WD
1143 if (info->flash_id == FLASH_UNKNOWN)
1144 goto next_bank;
1ee1180b 1145 for (j = 0; j < info->sector_count; ++j) {
25c751e9 1146
d5934ad7
MB
1147 hdr = (void *)info->start[j];
1148 if (!hdr)
b97a2a0a 1149 goto next_sector;
27b207fd 1150
9a4daad0 1151 switch (genimg_get_format (hdr)) {
d5934ad7 1152 case IMAGE_FORMAT_LEGACY:
d5934ad7
MB
1153 if (!image_check_hcrc (hdr))
1154 goto next_sector;
1155
1156 printf ("Legacy Image at %08lX:\n", (ulong)hdr);
1157 image_print_contents (hdr);
1158
1159 puts (" Verifying Checksum ... ");
1160 if (!image_check_dcrc (hdr)) {
1161 puts ("Bad Data CRC\n");
1162 } else {
1163 puts ("OK\n");
1164 }
1165 break;
1166#if defined(CONFIG_FIT)
1167 case IMAGE_FORMAT_FIT:
e32fea6a
MB
1168 if (!fit_check_format (hdr))
1169 goto next_sector;
1170
d5934ad7 1171 printf ("FIT Image at %08lX:\n", (ulong)hdr);
e32fea6a 1172 fit_print_contents (hdr);
d5934ad7 1173 break;
213bf8c8 1174#endif
d5934ad7 1175 default:
27b207fd 1176 goto next_sector;
25c751e9 1177 }
87a449c8 1178
bdccc4fe 1179next_sector: ;
c76f951a 1180 }
bdccc4fe 1181next_bank: ;
27b207fd 1182 }
c76f951a 1183
27b207fd
WD
1184 return (0);
1185}
c76f951a 1186
27b207fd
WD
1187U_BOOT_CMD(
1188 imls, 1, 1, do_imls,
2fb2604d 1189 "list all images found in flash",
27b207fd
WD
1190 "\n"
1191 " - Prints information about all images found at sector\n"
a89c33db 1192 " boundaries in flash."
27b207fd 1193);
98a9c4d4 1194#endif
47d1a6e1 1195
1ee1180b 1196/*******************************************************************/
5cf746c3 1197/* helper routines */
1ee1180b 1198/*******************************************************************/
1ee1180b
MB
1199#ifdef CONFIG_SILENT_CONSOLE
1200static void fixup_silent_linux ()
1201{
1202 char buf[256], *start, *end;
1203 char *cmdline = getenv ("bootargs");
47d1a6e1 1204
1ee1180b
MB
1205 /* Only fix cmdline when requested */
1206 if (!(gd->flags & GD_FLG_SILENT))
1207 return;
47d1a6e1 1208
1ee1180b
MB
1209 debug ("before silent fix-up: %s\n", cmdline);
1210 if (cmdline) {
1211 if ((start = strstr (cmdline, "console=")) != NULL) {
1212 end = strchr (start, ' ');
1213 strncpy (buf, cmdline, (start - cmdline + 8));
1214 if (end)
1215 strcpy (buf + (start - cmdline + 8), end);
1216 else
1217 buf[start - cmdline + 8] = '\0';
1218 } else {
1219 strcpy (buf, cmdline);
1220 strcat (buf, " console=");
47d1a6e1 1221 }
47d1a6e1 1222 } else {
1ee1180b 1223 strcpy (buf, "console=");
e7902122 1224 }
10aaf716 1225
1ee1180b
MB
1226 setenv ("bootargs", buf);
1227 debug ("after silent fix-up: %s\n", buf);
1228}
1229#endif /* CONFIG_SILENT_CONSOLE */
38eb508e 1230
38eb508e 1231
1ee1180b
MB
1232/*******************************************************************/
1233/* OS booting routines */
1234/*******************************************************************/
c76f951a 1235
b1d0db18 1236#ifdef CONFIG_BOOTM_NETBSD
54841ab5 1237static int do_bootm_netbsd (int flag, int argc, char * const argv[],
8a5ea3e6 1238 bootm_headers_t *images)
d791b1dc 1239{
1ee1180b 1240 void (*loader)(bd_t *, image_header_t *, char *, char *);
d5934ad7 1241 image_header_t *os_hdr, *hdr;
f13e7b2e 1242 ulong kernel_data, kernel_len;
1ee1180b
MB
1243 char *consdev;
1244 char *cmdline;
1245
49c3a861
KG
1246 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1247 return 1;
1248
d5934ad7
MB
1249#if defined(CONFIG_FIT)
1250 if (!images->legacy_hdr_valid) {
1251 fit_unsupported_reset ("NetBSD");
40d7e99d 1252 return 1;
38eb508e
GVB
1253 }
1254#endif
d5934ad7 1255 hdr = images->legacy_hdr_os;
47d1a6e1
WD
1256
1257 /*
1258 * Booting a (NetBSD) kernel image
1259 *
1260 * This process is pretty similar to a standalone application:
1261 * The (first part of an multi-) image must be a stage-2 loader,
1262 * which in turn is responsible for loading & invoking the actual
1263 * kernel. The only differences are the parameters being passed:
1264 * besides the board info strucure, the loader expects a command
1265 * line, the name of the console device, and (optionally) the
1266 * address of the original image header.
1267 */
d5934ad7 1268 os_hdr = NULL;
cb1c4896 1269 if (image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
f13e7b2e
MB
1270 image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
1271 if (kernel_len)
d5934ad7 1272 os_hdr = hdr;
f13e7b2e 1273 }
47d1a6e1
WD
1274
1275 consdev = "";
1276#if defined (CONFIG_8xx_CONS_SMC1)
1277 consdev = "smc1";
1278#elif defined (CONFIG_8xx_CONS_SMC2)
1279 consdev = "smc2";
1280#elif defined (CONFIG_8xx_CONS_SCC2)
1281 consdev = "scc2";
1282#elif defined (CONFIG_8xx_CONS_SCC3)
1283 consdev = "scc3";
1284#endif
1285
1286 if (argc > 2) {
1287 ulong len;
1288 int i;
1289
1ee1180b 1290 for (i = 2, len = 0; i < argc; i += 1)
47d1a6e1
WD
1291 len += strlen (argv[i]) + 1;
1292 cmdline = malloc (len);
1293
1ee1180b 1294 for (i = 2, len = 0; i < argc; i += 1) {
47d1a6e1
WD
1295 if (i > 2)
1296 cmdline[len++] = ' ';
1297 strcpy (&cmdline[len], argv[i]);
1298 len += strlen (argv[i]);
1299 }
1ee1180b 1300 } else if ((cmdline = getenv ("bootargs")) == NULL) {
47d1a6e1
WD
1301 cmdline = "";
1302 }
1303
c160a954 1304 loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
47d1a6e1
WD
1305
1306 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
1307 (ulong)loader);
1308
fad63407 1309 show_boot_progress (15);
47d1a6e1
WD
1310
1311 /*
1312 * NetBSD Stage-2 Loader Parameters:
1313 * r3: ptr to board info data
1314 * r4: image address
1315 * r5: console device
1316 * r6: boot args string
1317 */
d5934ad7 1318 (*loader) (gd->bd, os_hdr, consdev, cmdline);
40d7e99d
KG
1319
1320 return 1;
47d1a6e1 1321}
b1d0db18 1322#endif /* CONFIG_BOOTM_NETBSD*/
47d1a6e1 1323
1ee1180b 1324#ifdef CONFIG_LYNXKDI
54841ab5 1325static int do_bootm_lynxkdi (int flag, int argc, char * const argv[],
8a5ea3e6 1326 bootm_headers_t *images)
1ee1180b 1327{
cb1c4896 1328 image_header_t *hdr = &images->legacy_hdr_os_copy;
d5934ad7 1329
49c3a861
KG
1330 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1331 return 1;
1332
d5934ad7
MB
1333#if defined(CONFIG_FIT)
1334 if (!images->legacy_hdr_valid) {
1335 fit_unsupported_reset ("Lynx");
40d7e99d 1336 return 1;
d5934ad7
MB
1337 }
1338#endif
1339
1340 lynxkdi_boot ((image_header_t *)hdr);
40d7e99d
KG
1341
1342 return 1;
1ee1180b
MB
1343}
1344#endif /* CONFIG_LYNXKDI */
1345
b1d0db18 1346#ifdef CONFIG_BOOTM_RTEMS
54841ab5 1347static int do_bootm_rtems (int flag, int argc, char * const argv[],
8a5ea3e6 1348 bootm_headers_t *images)
1ee1180b 1349{
1ee1180b 1350 void (*entry_point)(bd_t *);
d791b1dc 1351
49c3a861
KG
1352 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1353 return 1;
1354
d5934ad7
MB
1355#if defined(CONFIG_FIT)
1356 if (!images->legacy_hdr_valid) {
1357 fit_unsupported_reset ("RTEMS");
40d7e99d 1358 return 1;
d5934ad7
MB
1359 }
1360#endif
1361
c160a954 1362 entry_point = (void (*)(bd_t *))images->ep;
d791b1dc
WD
1363
1364 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
1365 (ulong)entry_point);
1366
fad63407 1367 show_boot_progress (15);
d791b1dc
WD
1368
1369 /*
1370 * RTEMS Parameters:
1371 * r3: ptr to board info data
1372 */
1ee1180b 1373 (*entry_point)(gd->bd);
40d7e99d
KG
1374
1375 return 1;
d791b1dc 1376}
b1d0db18 1377#endif /* CONFIG_BOOTM_RTEMS */
7f70e853 1378
baa26db4 1379#if defined(CONFIG_CMD_ELF)
54841ab5 1380static int do_bootm_vxworks (int flag, int argc, char * const argv[],
8a5ea3e6 1381 bootm_headers_t *images)
47d1a6e1 1382{
47d1a6e1 1383 char str[80];
7f70e853 1384
49c3a861
KG
1385 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1386 return 1;
1387
d5934ad7 1388#if defined(CONFIG_FIT)
cb1c4896 1389 if (!images->legacy_hdr_valid) {
d5934ad7 1390 fit_unsupported_reset ("VxWorks");
40d7e99d 1391 return 1;
d5934ad7
MB
1392 }
1393#endif
47d1a6e1 1394
c160a954 1395 sprintf(str, "%lx", images->ep); /* write entry-point into string */
47d1a6e1 1396 setenv("loadaddr", str);
40d7e99d
KG
1397 do_bootvx(NULL, 0, 0, NULL);
1398
1399 return 1;
47d1a6e1
WD
1400}
1401
54841ab5 1402static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
8a5ea3e6 1403 bootm_headers_t *images)
47d1a6e1 1404{
47d1a6e1
WD
1405 char *local_args[2];
1406 char str[16];
d5934ad7 1407
49c3a861
KG
1408 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1409 return 1;
1410
d5934ad7
MB
1411#if defined(CONFIG_FIT)
1412 if (!images->legacy_hdr_valid) {
1413 fit_unsupported_reset ("QNX");
40d7e99d 1414 return 1;
d5934ad7
MB
1415 }
1416#endif
47d1a6e1 1417
c160a954 1418 sprintf(str, "%lx", images->ep); /* write entry-point into string */
47d1a6e1
WD
1419 local_args[0] = argv[0];
1420 local_args[1] = str; /* and provide it via the arguments */
40d7e99d
KG
1421 do_bootelf(NULL, 0, 2, local_args);
1422
1423 return 1;
47d1a6e1 1424}
90253178 1425#endif
f5ed9e39
PT
1426
1427#ifdef CONFIG_INTEGRITY
54841ab5 1428static int do_bootm_integrity (int flag, int argc, char * const argv[],
f5ed9e39
PT
1429 bootm_headers_t *images)
1430{
1431 void (*entry_point)(void);
1432
49c3a861
KG
1433 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1434 return 1;
1435
f5ed9e39
PT
1436#if defined(CONFIG_FIT)
1437 if (!images->legacy_hdr_valid) {
1438 fit_unsupported_reset ("INTEGRITY");
1439 return 1;
1440 }
1441#endif
1442
1443 entry_point = (void (*)(void))images->ep;
1444
1445 printf ("## Transferring control to INTEGRITY (at address %08lx) ...\n",
1446 (ulong)entry_point);
1447
1448 show_boot_progress (15);
1449
1450 /*
1451 * INTEGRITY Parameters:
1452 * None
1453 */
1454 (*entry_point)();
1455
1456 return 1;
1457}
1458#endif