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