]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_bootm.c
[new uImage] ppc: Add new uImage format support to FDT handling routines
[people/ms/u-boot.git] / common / cmd_bootm.c
CommitLineData
47d1a6e1 1/*
15940c9a 2 * (C) Copyright 2000-2006
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
MB
24#define DEBUG
25
47d1a6e1
WD
26/*
27 * Boot support
28 */
29#include <common.h>
30#include <watchdog.h>
31#include <command.h>
47d1a6e1
WD
32#include <image.h>
33#include <malloc.h>
34#include <zlib.h>
c29fdfc1 35#include <bzlib.h>
7f70e853 36#include <environment.h>
4ed6552f 37#include <lmb.h>
47d1a6e1 38#include <asm/byteorder.h>
8bde7f77 39
47d1a6e1
WD
40#ifdef CFG_HUSH_PARSER
41#include <hush.h>
42#endif
43
1ee1180b
MB
44DECLARE_GLOBAL_DATA_PTR;
45
46extern int gunzip (void *dst, int dstlen, unsigned char *src, unsigned long *lenp);
47#ifndef CFG_BOOTM_LEN
48#define CFG_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
49#endif
47d1a6e1 50
321359f2
MB
51#ifdef CONFIG_BZIP2
52extern void bz_internal_error(int);
53#endif
47d1a6e1 54
baa26db4 55#if defined(CONFIG_CMD_IMI)
47d1a6e1
WD
56static int image_info (unsigned long addr);
57#endif
27b207fd 58
baa26db4 59#if defined(CONFIG_CMD_IMLS)
27b207fd 60#include <flash.h>
e6f2e902 61extern flash_info_t flash_info[]; /* info for FLASH chips */
27b207fd
WD
62static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
63#endif
64
1ee1180b
MB
65#ifdef CONFIG_SILENT_CONSOLE
66static void fixup_silent_linux (void);
67#endif
68
6986a385
MB
69static image_header_t *image_get_kernel (ulong img_addr, int verify);
70#if defined(CONFIG_FIT)
71static int fit_check_kernel (const void *fit, int os_noffset, int verify);
72#endif
73
9a4daad0 74static void *boot_get_kernel (cmd_tbl_t *cmdtp, int flag,int argc, char *argv[],
8a5ea3e6 75 bootm_headers_t *images, ulong *os_data, ulong *os_len);
1ee1180b 76extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
47d1a6e1
WD
77
78/*
79 * Continue booting an OS image; caller already has:
80 * - copied image header to global variable `header'
81 * - checked header magic number, checksums (both header & image),
82 * - verified image architecture (PPC) and type (KERNEL or MULTI),
83 * - loaded (first part of) image to header load address,
84 * - disabled interrupts.
85 */
1ee1180b 86typedef void boot_os_fn (cmd_tbl_t *cmdtp, int flag,
f13e7b2e 87 int argc, char *argv[],
8a5ea3e6 88 bootm_headers_t *images); /* pointers to os/initrd/fdt */
1ee1180b
MB
89
90extern boot_os_fn do_bootm_linux;
91static boot_os_fn do_bootm_netbsd;
f13e7b2e 92#if defined(CONFIG_LYNXKDI)
1ee1180b
MB
93static boot_os_fn do_bootm_lynxkdi;
94extern void lynxkdi_boot (image_header_t *);
f72da340 95#endif
1ee1180b 96static boot_os_fn do_bootm_rtems;
baa26db4 97#if defined(CONFIG_CMD_ELF)
1ee1180b
MB
98static boot_os_fn do_bootm_vxworks;
99static boot_os_fn do_bootm_qnxelf;
100int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
101int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
90253178 102#endif
7f70e853 103#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
1ee1180b
MB
104extern uchar (*env_get_char)(int); /* Returns a character from the environment */
105static boot_os_fn do_bootm_artos;
15940c9a
SR
106#endif
107
1ee1180b 108ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
d5934ad7 109static bootm_headers_t images; /* pointers to os/initrd/fdt images */
1ee1180b 110
e822d7fc
KG
111void __board_lmb_reserve(struct lmb *lmb)
112{
113 /* please define platform specific board_lmb_reserve() */
114}
115void board_lmb_reserve(struct lmb *lmb) __attribute__((weak, alias("__board_lmb_reserve")));
116
47d1a6e1 117
1ee1180b
MB
118/*******************************************************************/
119/* bootm - boot application image from image in memory */
120/*******************************************************************/
47d1a6e1
WD
121int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
122{
f13e7b2e 123 ulong iflag;
42b73e8e 124 const char *type_name;
f13e7b2e 125 uint unc_len = CFG_BOOTM_LEN;
d5934ad7 126 uint8_t comp, type, os;
47d1a6e1 127
d5934ad7 128 void *os_hdr;
f13e7b2e 129 ulong os_data, os_len;
7582438c
MB
130 ulong image_start, image_end;
131 ulong load_start, load_end;
d3f2fa0d 132 ulong mem_start, mem_size;
6986a385
MB
133#if defined(CONFIG_FIT)
134 int os_noffset;
135#endif
7582438c 136
4ed6552f
KG
137 struct lmb lmb;
138
d5934ad7 139 memset ((void *)&images, 0, sizeof (images));
8a5ea3e6 140 images.verify = getenv_verify();
f5614e79 141 images.autostart = getenv_autostart();
4ed6552f
KG
142 images.lmb = &lmb;
143
144 lmb_init(&lmb);
145
d3f2fa0d
KG
146 mem_start = getenv_bootm_low();
147 mem_size = getenv_bootm_size();
148
149 lmb_add(&lmb, mem_start, mem_size);
d5934ad7 150
e822d7fc
KG
151 board_lmb_reserve(&lmb);
152
5cf746c3 153 /* get kernel image header, start address and length */
9a4daad0 154 os_hdr = boot_get_kernel (cmdtp, flag, argc, argv,
d5934ad7 155 &images, &os_data, &os_len);
6986a385
MB
156 if (os_len == 0) {
157 puts ("ERROR: can't get kernel image!\n");
47d1a6e1 158 return 1;
6986a385 159 }
47d1a6e1 160
fad63407 161 show_boot_progress (6);
47d1a6e1 162
d5934ad7 163 /* get image parameters */
9a4daad0 164 switch (genimg_get_format (os_hdr)) {
d5934ad7
MB
165 case IMAGE_FORMAT_LEGACY:
166 type = image_get_type (os_hdr);
167 comp = image_get_comp (os_hdr);
168 os = image_get_os (os_hdr);
169
170 image_end = image_get_image_end (os_hdr);
171 load_start = image_get_load (os_hdr);
172 break;
173#if defined(CONFIG_FIT)
174 case IMAGE_FORMAT_FIT:
6986a385
MB
175 os_noffset = fit_image_get_node (images.fit_hdr_os,
176 images.fit_uname_os);
177 if (os_noffset < 0) {
178 printf ("Can't get image node for '%s'!\n",
179 images.fit_uname_os);
180 return 1;
181 }
182
183 if (fit_image_get_type (images.fit_hdr_os, os_noffset, &type)) {
184 puts ("Can't get image type!\n");
185 return 1;
186 }
187
188 if (fit_image_get_comp (images.fit_hdr_os, os_noffset, &comp)) {
189 puts ("Can't get image compression!\n");
190 return 1;
191 }
192
193 if (fit_image_get_os (images.fit_hdr_os, os_noffset, &os)) {
194 puts ("Can't get image OS!\n");
195 return 1;
196 }
197
198 image_end = fit_get_end (images.fit_hdr_os);
199
200 if (fit_image_get_load (images.fit_hdr_os, os_noffset,
201 &load_start)) {
202 puts ("Can't get image load address!\n");
203 return 1;
204 }
205 break;
d5934ad7
MB
206#endif
207 default:
208 puts ("ERROR: unknown image format type!\n");
209 return 1;
210 }
211
212 image_start = (ulong)os_hdr;
213 load_end = 0;
9a4daad0 214 type_name = genimg_get_type_name (type);
d5934ad7 215
47d1a6e1
WD
216 /*
217 * We have reached the point of no return: we are going to
218 * overwrite all exception vector code, so we cannot easily
219 * recover from any failures any more...
220 */
47d1a6e1
WD
221 iflag = disable_interrupts();
222
c7de829c
WD
223#ifdef CONFIG_AMIGAONEG3SE
224 /*
8bde7f77 225 * We've possible left the caches enabled during
c7de829c
WD
226 * bios emulation, so turn them off again
227 */
228 icache_disable();
229 invalidate_l1_instruction_cache();
230 flush_data_cache();
231 dcache_disable();
232#endif
233
d5934ad7 234 switch (comp) {
47d1a6e1 235 case IH_COMP_NONE:
d5934ad7 236 if (load_start == (ulong)os_hdr) {
42b73e8e 237 printf (" XIP %s ... ", type_name);
47d1a6e1 238 } else {
42b73e8e 239 printf (" Loading %s ... ", type_name);
47d1a6e1 240
d5934ad7 241 memmove_wd ((void *)load_start,
f13e7b2e 242 (void *)os_data, os_len, CHUNKSZ);
af13cdbc 243
7582438c 244 load_end = load_start + os_len;
af13cdbc 245 puts("OK\n");
47d1a6e1
WD
246 }
247 break;
248 case IH_COMP_GZIP:
42b73e8e 249 printf (" Uncompressing %s ... ", type_name);
d5934ad7 250 if (gunzip ((void *)load_start, unc_len,
f13e7b2e 251 (uchar *)os_data, &os_len) != 0) {
4b9206ed 252 puts ("GUNZIP ERROR - must RESET board to recover\n");
fad63407 253 show_boot_progress (-6);
47d1a6e1
WD
254 do_reset (cmdtp, flag, argc, argv);
255 }
7582438c
MB
256
257 load_end = load_start + os_len;
47d1a6e1 258 break;
c29fdfc1
WD
259#ifdef CONFIG_BZIP2
260 case IH_COMP_BZIP2:
42b73e8e 261 printf (" Uncompressing %s ... ", type_name);
5653fc33
WD
262 /*
263 * If we've got less than 4 MB of malloc() space,
264 * use slower decompression algorithm which requires
265 * at most 2300 KB of memory.
266 */
d5934ad7 267 int i = BZ2_bzBuffToBuffDecompress ((char*)load_start,
f13e7b2e
MB
268 &unc_len, (char *)os_data, os_len,
269 CFG_MALLOC_LEN < (4096 * 1024), 0);
c29fdfc1
WD
270 if (i != BZ_OK) {
271 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
fad63407 272 show_boot_progress (-6);
c29fdfc1
WD
273 do_reset (cmdtp, flag, argc, argv);
274 }
7582438c
MB
275
276 load_end = load_start + unc_len;
c29fdfc1
WD
277 break;
278#endif /* CONFIG_BZIP2 */
47d1a6e1
WD
279 default:
280 if (iflag)
281 enable_interrupts();
d5934ad7 282 printf ("Unimplemented compression type %d\n", comp);
fad63407 283 show_boot_progress (-7);
47d1a6e1
WD
284 return 1;
285 }
4b9206ed 286 puts ("OK\n");
5cf746c3 287 debug (" kernel loaded at 0x%08lx, end = 0x%08lx\n", load_start, load_end);
fad63407 288 show_boot_progress (7);
47d1a6e1 289
7582438c
MB
290 if ((load_start < image_end) && (load_end > image_start)) {
291 debug ("image_start = 0x%lX, image_end = 0x%lx\n", image_start, image_end);
292 debug ("load_start = 0x%lx, load_end = 0x%lx\n", load_start, load_end);
293
294 puts ("ERROR: image overwritten - must RESET the board to recover.\n");
295 do_reset (cmdtp, flag, argc, argv);
296 }
297
fad63407 298 show_boot_progress (8);
47d1a6e1 299
4ed6552f
KG
300 lmb_reserve(&lmb, load_start, (load_end - load_start));
301
d5934ad7 302 switch (os) {
47d1a6e1
WD
303 default: /* handled by (original) Linux case */
304 case IH_OS_LINUX:
f72da340
WD
305#ifdef CONFIG_SILENT_CONSOLE
306 fixup_silent_linux();
307#endif
8a5ea3e6 308 do_bootm_linux (cmdtp, flag, argc, argv, &images);
47d1a6e1 309 break;
1ee1180b 310
47d1a6e1 311 case IH_OS_NETBSD:
8a5ea3e6 312 do_bootm_netbsd (cmdtp, flag, argc, argv, &images);
47d1a6e1 313 break;
8bde7f77 314
1f4bb37d
WD
315#ifdef CONFIG_LYNXKDI
316 case IH_OS_LYNXOS:
8a5ea3e6 317 do_bootm_lynxkdi (cmdtp, flag, argc, argv, &images);
1f4bb37d
WD
318 break;
319#endif
320
d791b1dc 321 case IH_OS_RTEMS:
8a5ea3e6 322 do_bootm_rtems (cmdtp, flag, argc, argv, &images);
d791b1dc 323 break;
8bde7f77 324
baa26db4 325#if defined(CONFIG_CMD_ELF)
47d1a6e1 326 case IH_OS_VXWORKS:
8a5ea3e6 327 do_bootm_vxworks (cmdtp, flag, argc, argv, &images);
47d1a6e1 328 break;
f13e7b2e 329
47d1a6e1 330 case IH_OS_QNX:
8a5ea3e6 331 do_bootm_qnxelf (cmdtp, flag, argc, argv, &images);
47d1a6e1 332 break;
90253178 333#endif
1ee1180b 334
7f70e853
WD
335#ifdef CONFIG_ARTOS
336 case IH_OS_ARTOS:
8a5ea3e6 337 do_bootm_artos (cmdtp, flag, argc, argv, &images);
7f70e853
WD
338 break;
339#endif
47d1a6e1
WD
340 }
341
fad63407 342 show_boot_progress (-9);
47d1a6e1 343#ifdef DEBUG
4b9206ed 344 puts ("\n## Control returned to monitor - resetting...\n");
a44a269a
MB
345 if (images.autostart)
346 do_reset (cmdtp, flag, argc, argv);
47d1a6e1 347#endif
a44a269a
MB
348 if (!images.autostart && iflag)
349 enable_interrupts();
350
47d1a6e1
WD
351 return 1;
352}
353
1efd4360 354/**
9a4daad0
MB
355 * image_get_kernel - verify legacy format kernel image
356 * @img_addr: in RAM address of the legacy format image to be verified
357 * @verify: data CRC verification flag
1efd4360 358 *
9a4daad0
MB
359 * image_get_kernel() verifies legacy image integrity and returns pointer to
360 * legacy image header if image verification was completed successfully.
1efd4360
MB
361 *
362 * returns:
9a4daad0
MB
363 * pointer to a legacy image header if valid image was found
364 * otherwise return NULL
1efd4360
MB
365 */
366static image_header_t *image_get_kernel (ulong img_addr, int verify)
367{
368 image_header_t *hdr = (image_header_t *)img_addr;
369
370 if (!image_check_magic(hdr)) {
371 puts ("Bad Magic Number\n");
372 show_boot_progress (-1);
373 return NULL;
374 }
375 show_boot_progress (2);
376
377 if (!image_check_hcrc (hdr)) {
378 puts ("Bad Header Checksum\n");
379 show_boot_progress (-2);
380 return NULL;
381 }
382
383 show_boot_progress (3);
384 image_print_contents (hdr);
385
386 if (verify) {
387 puts (" Verifying Checksum ... ");
388 if (!image_check_dcrc (hdr)) {
389 printf ("Bad Data CRC\n");
390 show_boot_progress (-3);
391 return NULL;
392 }
393 puts ("OK\n");
394 }
395 show_boot_progress (4);
396
397 if (!image_check_target_arch (hdr)) {
398 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
399 show_boot_progress (-4);
400 return NULL;
401 }
402 return hdr;
403}
404
6986a385
MB
405/**
406 * fit_check_kernel - verify FIT format kernel subimage
407 * @fit_hdr: pointer to the FIT image header
408 * os_noffset: kernel subimage node offset within FIT image
409 * @verify: data CRC verification flag
410 *
411 * fit_check_kernel() verifies integrity of the kernel subimage and from
412 * specified FIT image.
413 *
414 * returns:
415 * 1, on success
416 * 0, on failure
417 */
418#if defined (CONFIG_FIT)
419static int fit_check_kernel (const void *fit, int os_noffset, int verify)
420{
421 fit_image_print (fit, os_noffset, " ");
422
423 if (verify) {
424 puts (" Verifying Hash Integrity ... ");
425 if (!fit_image_check_hashes (fit, os_noffset)) {
426 puts ("Bad Data Hash\n");
427 return 0;
428 }
429 puts ("OK\n");
430 }
431
432 if (!fit_image_check_target_arch (fit, os_noffset)) {
433 puts ("Unsupported Architecture\n");
434 return 0;
435 }
436
437 if (!fit_image_check_type (fit, os_noffset, IH_TYPE_KERNEL)) {
438 puts ("Not a kernel image\n");
439 return 0;
440 }
441
442 return 1;
443}
444#endif /* CONFIG_FIT */
445
5cf746c3 446/**
9a4daad0 447 * boot_get_kernel - find kernel image
5cf746c3
MB
448 * @os_data: pointer to a ulong variable, will hold os data start address
449 * @os_len: pointer to a ulong variable, will hold os data length
450 *
9a4daad0 451 * boot_get_kernel() tries to find a kernel image, verifies its integrity
5cf746c3
MB
452 * and locates kernel data.
453 *
454 * returns:
455 * pointer to image header if valid image was found, plus kernel start
456 * address and length, otherwise NULL
457 */
9a4daad0 458static void *boot_get_kernel (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
8a5ea3e6 459 bootm_headers_t *images, ulong *os_data, ulong *os_len)
5cf746c3
MB
460{
461 image_header_t *hdr;
462 ulong img_addr;
d5934ad7
MB
463#if defined(CONFIG_FIT)
464 void *fit_hdr;
465 const char *fit_uname_config = NULL;
466 const char *fit_uname_kernel = NULL;
6986a385
MB
467 const void *data;
468 size_t len;
469 int conf_noffset;
470 int os_noffset;
d5934ad7 471#endif
5cf746c3 472
d5934ad7 473 /* find out kernel image address */
5cf746c3
MB
474 if (argc < 2) {
475 img_addr = load_addr;
d5934ad7
MB
476 debug ("* kernel: default image load address = 0x%08lx\n",
477 load_addr);
478#if defined(CONFIG_FIT)
479 } else if (fit_parse_conf (argv[1], load_addr, &img_addr,
480 &fit_uname_config)) {
481 debug ("* kernel: config '%s' from image at 0x%08lx\n",
482 fit_uname_config, img_addr);
483 } else if (fit_parse_subimage (argv[1], load_addr, &img_addr,
484 &fit_uname_kernel)) {
485 debug ("* kernel: subimage '%s' from image at 0x%08lx\n",
486 fit_uname_kernel, img_addr);
487#endif
5cf746c3
MB
488 } else {
489 img_addr = simple_strtoul(argv[1], NULL, 16);
d5934ad7 490 debug ("* kernel: cmdline image address = 0x%08lx\n", img_addr);
5cf746c3
MB
491 }
492
493 show_boot_progress (1);
5cf746c3 494
fff888a1 495 /* copy from dataflash if needed */
9a4daad0 496 img_addr = genimg_get_image (img_addr);
5cf746c3 497
d5934ad7 498 /* check image type, for FIT images get FIT kernel node */
6986a385 499 *os_data = *os_len = 0;
9a4daad0 500 switch (genimg_get_format ((void *)img_addr)) {
d5934ad7 501 case IMAGE_FORMAT_LEGACY:
6986a385
MB
502 printf ("## Booting kernel from Legacy Image at %08lx ...\n",
503 img_addr);
1efd4360
MB
504 hdr = image_get_kernel (img_addr, images->verify);
505 if (!hdr)
d5934ad7 506 return NULL;
d5934ad7
MB
507 show_boot_progress (5);
508
6986a385 509 /* get os_data and os_len */
d5934ad7
MB
510 switch (image_get_type (hdr)) {
511 case IH_TYPE_KERNEL:
512 *os_data = image_get_data (hdr);
513 *os_len = image_get_data_size (hdr);
514 break;
515 case IH_TYPE_MULTI:
516 image_multi_getimg (hdr, 0, os_data, os_len);
517 break;
518 default:
519 printf ("Wrong Image Type for %s command\n", cmdtp->name);
520 show_boot_progress (-5);
521 return NULL;
522 }
523 images->legacy_hdr_os = hdr;
524 images->legacy_hdr_valid = 1;
5cf746c3 525
5cf746c3 526 break;
d5934ad7
MB
527#if defined(CONFIG_FIT)
528 case IMAGE_FORMAT_FIT:
529 fit_hdr = (void *)img_addr;
6986a385
MB
530 printf ("## Booting kernel from FIT Image at %08lx ...\n",
531 img_addr);
532
533 if (!fit_check_format (fit_hdr)) {
534 puts ("Bad FIT kernel image format!\n");
535 return NULL;
536 }
537
538 if (!fit_uname_kernel) {
539 /*
540 * no kernel image node unit name, try to get config
541 * node first. If config unit node name is NULL
542 * fit_conf_get_node() will try to find default config node
543 */
544 conf_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
545 if (conf_noffset < 0)
546 return NULL;
547
548 os_noffset = fit_conf_get_kernel_node (fit_hdr, conf_noffset);
549 fit_uname_kernel = fit_get_name (fit_hdr, os_noffset, NULL);
550 } else {
551 /* get kernel component image node offset */
552 os_noffset = fit_image_get_node (fit_hdr, fit_uname_kernel);
553 }
554 if (os_noffset < 0)
555 return NULL;
556
557 printf (" Trying '%s' kernel subimage\n", fit_uname_kernel);
558
559 if (!fit_check_kernel (fit_hdr, os_noffset, images->verify))
560 return NULL;
561
562 /* get kernel image data address and length */
563 if (fit_image_get_data (fit_hdr, os_noffset, &data, &len)) {
564 puts ("Could not find kernel subimage data!\n");
565 return NULL;
566 }
567
568 *os_len = len;
569 *os_data = (ulong)data;
570 images->fit_hdr_os = fit_hdr;
571 images->fit_uname_os = fit_uname_kernel;
572 break;
d5934ad7 573#endif
5cf746c3 574 default:
d5934ad7 575 printf ("Wrong Image Format for %s command\n", cmdtp->name);
5cf746c3
MB
576 return NULL;
577 }
d5934ad7 578
6986a385
MB
579 debug (" kernel data at 0x%08lx, len = 0x%08lx (%d)\n",
580 *os_data, *os_len, *os_len);
5cf746c3 581
d5934ad7 582 return (void *)img_addr;
5cf746c3
MB
583}
584
0d498393 585U_BOOT_CMD(
1ee1180b
MB
586 bootm, CFG_MAXARGS, 1, do_bootm,
587 "bootm - boot application image from memory\n",
588 "[addr [arg ...]]\n - boot application image stored in memory\n"
589 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
590 "\t'arg' can be the address of an initrd image\n"
4a2ad5ff 591#if defined(CONFIG_OF_LIBFDT)
98a9c4d4 592 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
5441f61a 593 "\ta third argument is required which is the address of the\n"
98a9c4d4
MM
594 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
595 "\tuse a '-' for the second argument. If you do not pass a third\n"
596 "\ta bd_info struct will be passed instead\n"
597#endif
6986a385
MB
598#if defined(CONFIG_FIT)
599 "\t\nFor the new multi component uImage format (FIT) addresses\n"
600 "\tmust be extened to include component or configuration unit name:\n"
601 "\taddr:<subimg_uname> - direct component image specification\n"
602 "\taddr#<conf_uname> - configuration specification\n"
603 "\tUse iminfo command to get the list of existing component\n"
604 "\timages and configurations.\n"
605#endif
8bde7f77
WD
606);
607
1ee1180b
MB
608/*******************************************************************/
609/* bootd - boot default image */
610/*******************************************************************/
baa26db4 611#if defined(CONFIG_CMD_BOOTD)
47d1a6e1
WD
612int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
613{
614 int rcode = 0;
1ee1180b 615
47d1a6e1 616#ifndef CFG_HUSH_PARSER
1ee1180b
MB
617 if (run_command (getenv ("bootcmd"), flag) < 0)
618 rcode = 1;
47d1a6e1 619#else
1ee1180b
MB
620 if (parse_string_outer (getenv ("bootcmd"),
621 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
622 rcode = 1;
47d1a6e1
WD
623#endif
624 return rcode;
625}
8bde7f77 626
0d498393 627U_BOOT_CMD(
1ee1180b
MB
628 boot, 1, 1, do_bootd,
629 "boot - boot default, i.e., run 'bootcmd'\n",
9d2b18a0
WD
630 NULL
631);
632
633/* keep old command name "bootd" for backward compatibility */
0d498393 634U_BOOT_CMD(
1ee1180b
MB
635 bootd, 1, 1, do_bootd,
636 "bootd - boot default, i.e., run 'bootcmd'\n",
8bde7f77
WD
637 NULL
638);
639
47d1a6e1
WD
640#endif
641
1ee1180b
MB
642
643/*******************************************************************/
644/* iminfo - print header info for a requested image */
645/*******************************************************************/
baa26db4 646#if defined(CONFIG_CMD_IMI)
1ee1180b 647int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
47d1a6e1
WD
648{
649 int arg;
650 ulong addr;
1ee1180b 651 int rcode = 0;
47d1a6e1
WD
652
653 if (argc < 2) {
654 return image_info (load_addr);
655 }
656
1ee1180b
MB
657 for (arg = 1; arg < argc; ++arg) {
658 addr = simple_strtoul (argv[arg], NULL, 16);
659 if (image_info (addr) != 0)
660 rcode = 1;
47d1a6e1
WD
661 }
662 return rcode;
663}
664
665static int image_info (ulong addr)
666{
d5934ad7 667 void *hdr = (void *)addr;
47d1a6e1
WD
668
669 printf ("\n## Checking Image at %08lx ...\n", addr);
670
9a4daad0 671 switch (genimg_get_format (hdr)) {
d5934ad7
MB
672 case IMAGE_FORMAT_LEGACY:
673 puts (" Legacy image found\n");
674 if (!image_check_magic (hdr)) {
675 puts (" Bad Magic Number\n");
676 return 1;
677 }
47d1a6e1 678
d5934ad7
MB
679 if (!image_check_hcrc (hdr)) {
680 puts (" Bad Header Checksum\n");
681 return 1;
682 }
47d1a6e1 683
d5934ad7 684 image_print_contents (hdr);
47d1a6e1 685
d5934ad7
MB
686 puts (" Verifying Checksum ... ");
687 if (!image_check_dcrc (hdr)) {
688 puts (" Bad Data CRC\n");
689 return 1;
690 }
691 puts ("OK\n");
692 return 0;
693#if defined(CONFIG_FIT)
694 case IMAGE_FORMAT_FIT:
695 puts (" FIT image found\n");
e32fea6a
MB
696
697 if (!fit_check_format (hdr)) {
698 puts ("Bad FIT image format!\n");
699 return 1;
700 }
701
702 fit_print_contents (hdr);
d5934ad7
MB
703 return 0;
704#endif
705 default:
706 puts ("Unknown image format!\n");
707 break;
47d1a6e1 708 }
d5934ad7
MB
709
710 return 1;
47d1a6e1 711}
0d498393
WD
712
713U_BOOT_CMD(
714 iminfo, CFG_MAXARGS, 1, do_iminfo,
8bde7f77
WD
715 "iminfo - print header information for application image\n",
716 "addr [addr ...]\n"
717 " - print header information for application image starting at\n"
718 " address 'addr' in memory; this includes verification of the\n"
719 " image contents (magic number, header and payload checksums)\n"
720);
90253178 721#endif
47d1a6e1 722
1ee1180b
MB
723
724/*******************************************************************/
725/* imls - list all images found in flash */
726/*******************************************************************/
baa26db4 727#if defined(CONFIG_CMD_IMLS)
27b207fd
WD
728int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
729{
730 flash_info_t *info;
731 int i, j;
d5934ad7 732 void *hdr;
27b207fd 733
1ee1180b
MB
734 for (i = 0, info = &flash_info[0];
735 i < CFG_MAX_FLASH_BANKS; ++i, ++info) {
736
27b207fd
WD
737 if (info->flash_id == FLASH_UNKNOWN)
738 goto next_bank;
1ee1180b 739 for (j = 0; j < info->sector_count; ++j) {
27b207fd 740
d5934ad7
MB
741 hdr = (void *)info->start[j];
742 if (!hdr)
b97a2a0a 743 goto next_sector;
27b207fd 744
9a4daad0 745 switch (genimg_get_format (hdr)) {
d5934ad7 746 case IMAGE_FORMAT_LEGACY:
d5934ad7
MB
747 if (!image_check_hcrc (hdr))
748 goto next_sector;
749
750 printf ("Legacy Image at %08lX:\n", (ulong)hdr);
751 image_print_contents (hdr);
752
753 puts (" Verifying Checksum ... ");
754 if (!image_check_dcrc (hdr)) {
755 puts ("Bad Data CRC\n");
756 } else {
757 puts ("OK\n");
758 }
759 break;
760#if defined(CONFIG_FIT)
761 case IMAGE_FORMAT_FIT:
e32fea6a
MB
762 if (!fit_check_format (hdr))
763 goto next_sector;
764
d5934ad7 765 printf ("FIT Image at %08lX:\n", (ulong)hdr);
e32fea6a 766 fit_print_contents (hdr);
d5934ad7
MB
767 break;
768#endif
769 default:
27b207fd 770 goto next_sector;
5bb226e8 771 }
d5934ad7 772
bdccc4fe 773next_sector: ;
27b207fd 774 }
bdccc4fe 775next_bank: ;
27b207fd
WD
776 }
777
778 return (0);
779}
780
781U_BOOT_CMD(
782 imls, 1, 1, do_imls,
783 "imls - list all images found in flash\n",
784 "\n"
785 " - Prints information about all images found at sector\n"
786 " boundaries in flash.\n"
787);
90253178 788#endif
27b207fd 789
1ee1180b 790/*******************************************************************/
5cf746c3 791/* helper routines */
1ee1180b 792/*******************************************************************/
1ee1180b
MB
793#ifdef CONFIG_SILENT_CONSOLE
794static void fixup_silent_linux ()
795{
796 char buf[256], *start, *end;
797 char *cmdline = getenv ("bootargs");
798
799 /* Only fix cmdline when requested */
800 if (!(gd->flags & GD_FLG_SILENT))
801 return;
802
803 debug ("before silent fix-up: %s\n", cmdline);
804 if (cmdline) {
805 if ((start = strstr (cmdline, "console=")) != NULL) {
806 end = strchr (start, ' ');
807 strncpy (buf, cmdline, (start - cmdline + 8));
808 if (end)
809 strcpy (buf + (start - cmdline + 8), end);
810 else
811 buf[start - cmdline + 8] = '\0';
812 } else {
813 strcpy (buf, cmdline);
814 strcat (buf, " console=");
815 }
816 } else {
817 strcpy (buf, "console=");
818 }
819
820 setenv ("bootargs", buf);
821 debug ("after silent fix-up: %s\n", buf);
822}
823#endif /* CONFIG_SILENT_CONSOLE */
824
825
826/*******************************************************************/
827/* OS booting routines */
828/*******************************************************************/
829
830static void do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
f13e7b2e 831 int argc, char *argv[],
8a5ea3e6 832 bootm_headers_t *images)
d791b1dc 833{
1ee1180b 834 void (*loader)(bd_t *, image_header_t *, char *, char *);
d5934ad7 835 image_header_t *os_hdr, *hdr;
f13e7b2e 836 ulong kernel_data, kernel_len;
1ee1180b
MB
837 char *consdev;
838 char *cmdline;
839
d5934ad7
MB
840#if defined(CONFIG_FIT)
841 if (!images->legacy_hdr_valid) {
842 fit_unsupported_reset ("NetBSD");
843 do_reset (cmdtp, flag, argc, argv);
844 }
845#endif
846 hdr = images->legacy_hdr_os;
847
1ee1180b
MB
848 /*
849 * Booting a (NetBSD) kernel image
850 *
851 * This process is pretty similar to a standalone application:
852 * The (first part of an multi-) image must be a stage-2 loader,
853 * which in turn is responsible for loading & invoking the actual
854 * kernel. The only differences are the parameters being passed:
855 * besides the board info strucure, the loader expects a command
856 * line, the name of the console device, and (optionally) the
857 * address of the original image header.
858 */
d5934ad7 859 os_hdr = NULL;
f13e7b2e
MB
860 if (image_check_type (hdr, IH_TYPE_MULTI)) {
861 image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
862 if (kernel_len)
d5934ad7 863 os_hdr = hdr;
f13e7b2e 864 }
1ee1180b
MB
865
866 consdev = "";
867#if defined (CONFIG_8xx_CONS_SMC1)
868 consdev = "smc1";
869#elif defined (CONFIG_8xx_CONS_SMC2)
870 consdev = "smc2";
871#elif defined (CONFIG_8xx_CONS_SCC2)
872 consdev = "scc2";
873#elif defined (CONFIG_8xx_CONS_SCC3)
874 consdev = "scc3";
875#endif
876
877 if (argc > 2) {
878 ulong len;
879 int i;
880
881 for (i = 2, len = 0; i < argc; i += 1)
882 len += strlen (argv[i]) + 1;
883 cmdline = malloc (len);
884
885 for (i = 2, len = 0; i < argc; i += 1) {
886 if (i > 2)
887 cmdline[len++] = ' ';
888 strcpy (&cmdline[len], argv[i]);
889 len += strlen (argv[i]);
890 }
891 } else if ((cmdline = getenv ("bootargs")) == NULL) {
892 cmdline = "";
893 }
894
895 loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
896
897 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
898 (ulong)loader);
899
900 show_boot_progress (15);
901
902 /*
903 * NetBSD Stage-2 Loader Parameters:
904 * r3: ptr to board info data
905 * r4: image address
906 * r5: console device
907 * r6: boot args string
908 */
d5934ad7 909 (*loader) (gd->bd, os_hdr, consdev, cmdline);
1ee1180b
MB
910}
911
912#ifdef CONFIG_LYNXKDI
913static void do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
914 int argc, char *argv[],
8a5ea3e6 915 bootm_headers_t *images)
1ee1180b 916{
d5934ad7
MB
917 image_header_t *hdr = images->legacy_hdr_os;
918
919#if defined(CONFIG_FIT)
920 if (!images->legacy_hdr_valid) {
921 fit_unsupported_reset ("Lynx");
922 do_reset (cmdtp, flag, argc, argv);
923 }
924#endif
925
926 lynxkdi_boot ((image_header_t *)hdr);
1ee1180b
MB
927}
928#endif /* CONFIG_LYNXKDI */
929
930static void do_bootm_rtems (cmd_tbl_t *cmdtp, int flag,
931 int argc, char *argv[],
8a5ea3e6 932 bootm_headers_t *images)
1ee1180b 933{
d5934ad7 934 image_header_t *hdr = images->legacy_hdr_os;
1ee1180b 935 void (*entry_point)(bd_t *);
d791b1dc 936
d5934ad7
MB
937#if defined(CONFIG_FIT)
938 if (!images->legacy_hdr_valid) {
939 fit_unsupported_reset ("RTEMS");
940 do_reset (cmdtp, flag, argc, argv);
941 }
942#endif
943
b97a2a0a 944 entry_point = (void (*)(bd_t *))image_get_ep (hdr);
d791b1dc
WD
945
946 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
947 (ulong)entry_point);
948
fad63407 949 show_boot_progress (15);
d791b1dc
WD
950
951 /*
952 * RTEMS Parameters:
953 * r3: ptr to board info data
954 */
1ee1180b 955 (*entry_point)(gd->bd);
d791b1dc
WD
956}
957
baa26db4 958#if defined(CONFIG_CMD_ELF)
1ee1180b
MB
959static void do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag,
960 int argc, char *argv[],
8a5ea3e6 961 bootm_headers_t *images)
47d1a6e1 962{
47d1a6e1 963 char str[80];
d5934ad7
MB
964 image_header_t *hdr = images->legacy_hdr_os;
965
966#if defined(CONFIG_FIT)
967 if (hdr == NULL) {
968 fit_unsupported_reset ("VxWorks");
969 do_reset (cmdtp, flag, argc, argv);
970 }
971#endif
47d1a6e1 972
b97a2a0a 973 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
47d1a6e1
WD
974 setenv("loadaddr", str);
975 do_bootvx(cmdtp, 0, 0, NULL);
976}
977
f13e7b2e
MB
978static void do_bootm_qnxelf(cmd_tbl_t *cmdtp, int flag,
979 int argc, char *argv[],
8a5ea3e6 980 bootm_headers_t *images)
47d1a6e1 981{
47d1a6e1
WD
982 char *local_args[2];
983 char str[16];
d5934ad7
MB
984 image_header_t *hdr = images->legacy_hdr_os;
985
986#if defined(CONFIG_FIT)
987 if (!images->legacy_hdr_valid) {
988 fit_unsupported_reset ("QNX");
989 do_reset (cmdtp, flag, argc, argv);
990 }
991#endif
47d1a6e1 992
b97a2a0a 993 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
47d1a6e1
WD
994 local_args[0] = argv[0];
995 local_args[1] = str; /* and provide it via the arguments */
996 do_bootelf(cmdtp, 0, 2, local_args);
997}
90253178 998#endif
1f4bb37d 999
1ee1180b
MB
1000#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
1001static void do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
1002 int argc, char *argv[],
8a5ea3e6 1003 bootm_headers_t *images)
1f4bb37d 1004{
1ee1180b
MB
1005 ulong top;
1006 char *s, *cmdline;
1007 char **fwenv, **ss;
1008 int i, j, nxt, len, envno, envsz;
1009 bd_t *kbd;
1010 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
d5934ad7
MB
1011 image_header_t *hdr = images->legacy_hdr_os;
1012
1013#if defined(CONFIG_FIT)
1014 if (!images->legacy_hdr_valid) {
1015 fit_unsupported_reset ("ARTOS");
1016 do_reset (cmdtp, flag, argc, argv);
1017 }
1018#endif
1f4bb37d 1019
1ee1180b
MB
1020 /*
1021 * Booting an ARTOS kernel image + application
1022 */
1023
1024 /* this used to be the top of memory, but was wrong... */
1025#ifdef CONFIG_PPC
1026 /* get stack pointer */
1027 asm volatile ("mr %0,1" : "=r"(top) );
1028#endif
1029 debug ("## Current stack ends at 0x%08lX ", top);
1030
1031 top -= 2048; /* just to be sure */
1032 if (top > CFG_BOOTMAPSZ)
1033 top = CFG_BOOTMAPSZ;
1034 top &= ~0xF;
1035
1036 debug ("=> set upper limit to 0x%08lX\n", top);
1037
1038 /* first check the artos specific boot args, then the linux args*/
1039 if ((s = getenv( "abootargs")) == NULL && (s = getenv ("bootargs")) == NULL)
1040 s = "";
1041
1042 /* get length of cmdline, and place it */
1043 len = strlen (s);
1044 top = (top - (len + 1)) & ~0xF;
1045 cmdline = (char *)top;
1046 debug ("## cmdline at 0x%08lX ", top);
1047 strcpy (cmdline, s);
1048
1049 /* copy bdinfo */
1050 top = (top - sizeof (bd_t)) & ~0xF;
1051 debug ("## bd at 0x%08lX ", top);
1052 kbd = (bd_t *)top;
1053 memcpy (kbd, gd->bd, sizeof (bd_t));
1054
1055 /* first find number of env entries, and their size */
1056 envno = 0;
1057 envsz = 0;
1058 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
1059 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
1060 ;
1061 envno++;
1062 envsz += (nxt - i) + 1; /* plus trailing zero */
1063 }
1064 envno++; /* plus the terminating zero */
1065 debug ("## %u envvars total size %u ", envno, envsz);
1066
1067 top = (top - sizeof (char **) * envno) & ~0xF;
1068 fwenv = (char **)top;
1069 debug ("## fwenv at 0x%08lX ", top);
1070
1071 top = (top - envsz) & ~0xF;
1072 s = (char *)top;
1073 ss = fwenv;
1074
1075 /* now copy them */
1076 for (i = 0; env_get_char (i) != '\0'; i = nxt + 1) {
1077 for (nxt = i; env_get_char (nxt) != '\0'; ++nxt)
1078 ;
1079 *ss++ = s;
1080 for (j = i; j < nxt; ++j)
1081 *s++ = env_get_char (j);
1082 *s++ = '\0';
1083 }
1084 *ss++ = NULL; /* terminate */
1085
1086 entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
1087 (*entry) (kbd, cmdline, fwenv, top);
1088}
1089#endif