]> git.ipfire.org Git - people/ms/u-boot.git/blame - common/cmd_bootm.c
* Patches by Yuli Barcohen, 13 Jul 2003:
[people/ms/u-boot.git] / common / cmd_bootm.c
CommitLineData
47d1a6e1
WD
1/*
2 * (C) Copyright 2000-2002
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
24/*
25 * Boot support
26 */
27#include <common.h>
28#include <watchdog.h>
29#include <command.h>
47d1a6e1
WD
30#include <image.h>
31#include <malloc.h>
32#include <zlib.h>
7f70e853 33#include <environment.h>
47d1a6e1 34#include <asm/byteorder.h>
8bde7f77
WD
35
36 /*cmd_boot.c*/
37 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
38
47d1a6e1
WD
39#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
40#include <rtc.h>
41#endif
42
43#ifdef CFG_HUSH_PARSER
44#include <hush.h>
45#endif
46
47#ifdef CONFIG_SHOW_BOOT_PROGRESS
48# include <status_led.h>
49# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
50#else
51# define SHOW_BOOT_PROGRESS(arg)
52#endif
53
54#ifdef CFG_INIT_RAM_LOCK
55#include <asm/cache.h>
56#endif
57
228f29ac
WD
58#ifdef CONFIG_LOGBUFFER
59#include <logbuff.h>
60#endif
61
2abbe075
WD
62#ifdef CONFIG_HAS_DATAFLASH
63#include <dataflash.h>
64#endif
65
47d1a6e1
WD
66/*
67 * Some systems (for example LWMON) have very short watchdog periods;
68 * we must make sure to split long operations like memmove() or
69 * crc32() into reasonable chunks.
70 */
71#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
72# define CHUNKSZ (64 * 1024)
73#endif
74
75int gunzip (void *, int, unsigned char *, int *);
76
77static void *zalloc(void *, unsigned, unsigned);
78static void zfree(void *, void *, unsigned);
79
80#if (CONFIG_COMMANDS & CFG_CMD_IMI)
81static int image_info (unsigned long addr);
82#endif
83static void print_type (image_header_t *hdr);
84
2262cfee
WD
85#ifdef __I386__
86image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
87#endif
88
47d1a6e1
WD
89/*
90 * Continue booting an OS image; caller already has:
91 * - copied image header to global variable `header'
92 * - checked header magic number, checksums (both header & image),
93 * - verified image architecture (PPC) and type (KERNEL or MULTI),
94 * - loaded (first part of) image to header load address,
95 * - disabled interrupts.
96 */
97typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
98 int argc, char *argv[],
99 ulong addr, /* of image to boot */
100 ulong *len_ptr, /* multi-file image length table */
101 int verify); /* getenv("verify")[0] != 'n' */
102
8bde7f77
WD
103#ifdef DEBUG
104extern int do_bdinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
105#endif
106
2262cfee 107#ifdef CONFIG_PPC
47d1a6e1
WD
108static boot_os_Fcn do_bootm_linux;
109#else
110extern boot_os_Fcn do_bootm_linux;
111#endif
112static boot_os_Fcn do_bootm_netbsd;
d791b1dc 113static boot_os_Fcn do_bootm_rtems;
47d1a6e1
WD
114#if (CONFIG_COMMANDS & CFG_CMD_ELF)
115static boot_os_Fcn do_bootm_vxworks;
116static boot_os_Fcn do_bootm_qnxelf;
117int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
118int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
119#endif /* CFG_CMD_ELF */
7f70e853
WD
120#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
121static boot_os_Fcn do_bootm_artos;
122#endif
47d1a6e1
WD
123
124image_header_t header;
125
126ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
127
128int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
129{
130 ulong iflag;
131 ulong addr;
132 ulong data, len, checksum;
133 ulong *len_ptr;
134 int i, verify;
135 char *name, *s;
136 int (*appl)(cmd_tbl_t *, int, int, char *[]);
137 image_header_t *hdr = &header;
138
139 s = getenv ("verify");
140 verify = (s && (*s == 'n')) ? 0 : 1;
141
142 if (argc < 2) {
143 addr = load_addr;
144 } else {
145 addr = simple_strtoul(argv[1], NULL, 16);
146 }
147
148 SHOW_BOOT_PROGRESS (1);
149 printf ("## Booting image at %08lx ...\n", addr);
150
151 /* Copy header so we can blank CRC field for re-calculation */
2abbe075
WD
152#ifdef CONFIG_HAS_DATAFLASH
153 if (addr_dataflash(addr)){
154 read_dataflash(addr, sizeof(image_header_t), (char *)&header);
155 } else
156#endif
47d1a6e1
WD
157 memmove (&header, (char *)addr, sizeof(image_header_t));
158
159 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
2262cfee
WD
160#ifdef __I386__ /* correct image format not implemented yet - fake it */
161 if (fake_header(hdr, (void*)addr, -1) != NULL) {
162 /* to compensate for the addition below */
163 addr -= sizeof(image_header_t);
164 /* turnof verify,
165 * fake_header() does not fake the data crc
166 */
167 verify = 0;
168 } else
169#endif /* __I386__ */
170 {
47d1a6e1
WD
171 printf ("Bad Magic Number\n");
172 SHOW_BOOT_PROGRESS (-1);
173 return 1;
2262cfee 174 }
47d1a6e1
WD
175 }
176 SHOW_BOOT_PROGRESS (2);
177
178 data = (ulong)&header;
179 len = sizeof(image_header_t);
180
181 checksum = ntohl(hdr->ih_hcrc);
182 hdr->ih_hcrc = 0;
183
184 if (crc32 (0, (char *)data, len) != checksum) {
185 printf ("Bad Header Checksum\n");
186 SHOW_BOOT_PROGRESS (-2);
187 return 1;
188 }
189 SHOW_BOOT_PROGRESS (3);
190
191 /* for multi-file images we need the data part, too */
2262cfee 192 print_image_hdr (hdr);
47d1a6e1
WD
193
194 data = addr + sizeof(image_header_t);
195 len = ntohl(hdr->ih_size);
196
2abbe075
WD
197#ifdef CONFIG_HAS_DATAFLASH
198 if (addr_dataflash(addr)){
199 read_dataflash(data, len, (char *)CFG_LOAD_ADDR);
200 data = CFG_LOAD_ADDR;
201 }
8bde7f77 202#endif
2abbe075 203
47d1a6e1
WD
204 if (verify) {
205 printf (" Verifying Checksum ... ");
206 if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
207 printf ("Bad Data CRC\n");
208 SHOW_BOOT_PROGRESS (-3);
209 return 1;
210 }
211 printf ("OK\n");
212 }
213 SHOW_BOOT_PROGRESS (4);
214
215 len_ptr = (ulong *)data;
216
2262cfee
WD
217#if defined(__PPC__)
218 if (hdr->ih_arch != IH_CPU_PPC)
219#elif defined(__ARM__)
220 if (hdr->ih_arch != IH_CPU_ARM)
221#elif defined(__I386__)
222 if (hdr->ih_arch != IH_CPU_I386)
6069ff26 223#elif defined(__mips__)
8bde7f77 224 if (hdr->ih_arch != IH_CPU_MIPS)
2262cfee
WD
225#else
226# error Unknown CPU type
227#endif
228 {
229 printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch);
47d1a6e1
WD
230 SHOW_BOOT_PROGRESS (-4);
231 return 1;
232 }
233 SHOW_BOOT_PROGRESS (5);
234
235 switch (hdr->ih_type) {
236 case IH_TYPE_STANDALONE: name = "Standalone Application";
237 break;
238 case IH_TYPE_KERNEL: name = "Kernel Image";
239 break;
240 case IH_TYPE_MULTI: name = "Multi-File Image";
241 len = ntohl(len_ptr[0]);
242 /* OS kernel is always the first image */
243 data += 8; /* kernel_len + terminator */
244 for (i=1; len_ptr[i]; ++i)
245 data += 4;
246 break;
247 default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
248 SHOW_BOOT_PROGRESS (-5);
249 return 1;
250 }
251 SHOW_BOOT_PROGRESS (6);
252
253 /*
254 * We have reached the point of no return: we are going to
255 * overwrite all exception vector code, so we cannot easily
256 * recover from any failures any more...
257 */
258
259 iflag = disable_interrupts();
260
c7de829c
WD
261#ifdef CONFIG_AMIGAONEG3SE
262 /*
8bde7f77 263 * We've possible left the caches enabled during
c7de829c
WD
264 * bios emulation, so turn them off again
265 */
266 icache_disable();
267 invalidate_l1_instruction_cache();
268 flush_data_cache();
269 dcache_disable();
270#endif
271
47d1a6e1
WD
272 switch (hdr->ih_comp) {
273 case IH_COMP_NONE:
2262cfee 274 if(ntohl(hdr->ih_load) == addr) {
47d1a6e1
WD
275 printf (" XIP %s ... ", name);
276 } else {
277#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
278 size_t l = len;
279 void *to = (void *)ntohl(hdr->ih_load);
280 void *from = (void *)data;
281
282 printf (" Loading %s ... ", name);
283
284 while (l > 0) {
285 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
286 WATCHDOG_RESET();
287 memmove (to, from, tail);
288 to += tail;
289 from += tail;
290 l -= tail;
291 }
292#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
293 memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
294#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
295 }
296 break;
297 case IH_COMP_GZIP:
298 printf (" Uncompressing %s ... ", name);
299 if (gunzip ((void *)ntohl(hdr->ih_load), 0x400000,
300 (uchar *)data, (int *)&len) != 0) {
301 printf ("GUNZIP ERROR - must RESET board to recover\n");
302 SHOW_BOOT_PROGRESS (-6);
303 do_reset (cmdtp, flag, argc, argv);
304 }
305 break;
306 default:
307 if (iflag)
308 enable_interrupts();
309 printf ("Unimplemented compression type %d\n", hdr->ih_comp);
310 SHOW_BOOT_PROGRESS (-7);
311 return 1;
312 }
313 printf ("OK\n");
314 SHOW_BOOT_PROGRESS (7);
315
316 switch (hdr->ih_type) {
317 case IH_TYPE_STANDALONE:
47d1a6e1
WD
318 if (iflag)
319 enable_interrupts();
320
4a6fd34b
WD
321 /* load (and uncompress), but don't start if "autostart"
322 * is set to "no"
323 */
324 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0))
325 return 0;
326 appl = (int (*)(cmd_tbl_t *, int, int, char *[]))ntohl(hdr->ih_ep);
47d1a6e1 327 (*appl)(cmdtp, flag, argc-1, &argv[1]);
4a6fd34b 328 return 0;
47d1a6e1
WD
329 case IH_TYPE_KERNEL:
330 case IH_TYPE_MULTI:
331 /* handled below */
332 break;
333 default:
334 if (iflag)
335 enable_interrupts();
336 printf ("Can't boot image type %d\n", hdr->ih_type);
337 SHOW_BOOT_PROGRESS (-8);
338 return 1;
339 }
340 SHOW_BOOT_PROGRESS (8);
341
342 switch (hdr->ih_os) {
343 default: /* handled by (original) Linux case */
344 case IH_OS_LINUX:
345 do_bootm_linux (cmdtp, flag, argc, argv,
346 addr, len_ptr, verify);
347 break;
348 case IH_OS_NETBSD:
349 do_bootm_netbsd (cmdtp, flag, argc, argv,
350 addr, len_ptr, verify);
351 break;
8bde7f77 352
d791b1dc
WD
353 case IH_OS_RTEMS:
354 do_bootm_rtems (cmdtp, flag, argc, argv,
355 addr, len_ptr, verify);
356 break;
8bde7f77 357
47d1a6e1
WD
358#if (CONFIG_COMMANDS & CFG_CMD_ELF)
359 case IH_OS_VXWORKS:
360 do_bootm_vxworks (cmdtp, flag, argc, argv,
361 addr, len_ptr, verify);
362 break;
363 case IH_OS_QNX:
364 do_bootm_qnxelf (cmdtp, flag, argc, argv,
365 addr, len_ptr, verify);
366 break;
367#endif /* CFG_CMD_ELF */
7f70e853
WD
368#ifdef CONFIG_ARTOS
369 case IH_OS_ARTOS:
370 do_bootm_artos (cmdtp, flag, argc, argv,
371 addr, len_ptr, verify);
372 break;
373#endif
47d1a6e1
WD
374 }
375
376 SHOW_BOOT_PROGRESS (-9);
377#ifdef DEBUG
378 printf ("\n## Control returned to monitor - resetting...\n");
379 do_reset (cmdtp, flag, argc, argv);
380#endif
381 return 1;
382}
383
0d498393
WD
384U_BOOT_CMD(
385 bootm, CFG_MAXARGS, 1, do_bootm,
8bde7f77
WD
386 "bootm - boot application image from memory\n",
387 "[addr [arg ...]]\n - boot application image stored in memory\n"
388 " passing arguments 'arg ...'; when booting a Linux kernel,\n"
389 " 'arg' can be the address of an initrd image\n"
390);
391
2262cfee 392#ifdef CONFIG_PPC
47d1a6e1
WD
393static void
394do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
395 int argc, char *argv[],
396 ulong addr,
397 ulong *len_ptr,
398 int verify)
399{
400 DECLARE_GLOBAL_DATA_PTR;
401
402 ulong sp;
403 ulong len, checksum;
404 ulong initrd_start, initrd_end;
405 ulong cmd_start, cmd_end;
406 ulong initrd_high;
407 ulong data;
38b99261 408 int initrd_copy_to_ram = 1;
47d1a6e1
WD
409 char *cmdline;
410 char *s;
411 bd_t *kbd;
412 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
413 image_header_t *hdr = &header;
414
415 if ((s = getenv ("initrd_high")) != NULL) {
416 /* a value of "no" or a similar string will act like 0,
417 * turning the "load high" feature off. This is intentional.
418 */
419 initrd_high = simple_strtoul(s, NULL, 16);
38b99261
WD
420 if (initrd_high == ~0)
421 initrd_copy_to_ram = 0;
228f29ac 422 } else { /* not set, no restrictions to load high */
47d1a6e1
WD
423 initrd_high = ~0;
424 }
425
56f94be3 426#ifdef CONFIG_LOGBUFFER
6aff3115 427 kbd=gd->bd;
228f29ac
WD
428 /* Prevent initrd from overwriting logbuffer */
429 if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
430 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
431 debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
56f94be3
WD
432#endif
433
47d1a6e1
WD
434 /*
435 * Booting a (Linux) kernel image
436 *
437 * Allocate space for command line and board info - the
438 * address should be as high as possible within the reach of
439 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
440 * memory, which means far enough below the current stack
441 * pointer.
442 */
443
444 asm( "mr %0,1": "=r"(sp) : );
445
56f94be3
WD
446 debug ("## Current stack ends at 0x%08lX ", sp);
447
47d1a6e1
WD
448 sp -= 2048; /* just to be sure */
449 if (sp > CFG_BOOTMAPSZ)
450 sp = CFG_BOOTMAPSZ;
451 sp &= ~0xF;
452
56f94be3
WD
453 debug ("=> set upper limit to 0x%08lX\n", sp);
454
47d1a6e1
WD
455 cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
456 kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
457
458 if ((s = getenv("bootargs")) == NULL)
459 s = "";
460
461 strcpy (cmdline, s);
462
463 cmd_start = (ulong)&cmdline[0];
464 cmd_end = cmd_start + strlen(cmdline);
465
466 *kbd = *(gd->bd);
467
468#ifdef DEBUG
469 printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
470
471 do_bdinfo (NULL, 0, 0, NULL);
472#endif
473
474 if ((s = getenv ("clocks_in_mhz")) != NULL) {
475 /* convert all clock information to MHz */
476 kbd->bi_intfreq /= 1000000L;
477 kbd->bi_busfreq /= 1000000L;
478#if defined(CONFIG_8260)
479 kbd->bi_cpmfreq /= 1000000L;
480 kbd->bi_brgfreq /= 1000000L;
481 kbd->bi_sccfreq /= 1000000L;
482 kbd->bi_vco /= 1000000L;
483#endif /* CONFIG_8260 */
484 }
485
486 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))hdr->ih_ep;
487
488 /*
489 * Check if there is an initrd image
490 */
491 if (argc >= 3) {
492 SHOW_BOOT_PROGRESS (9);
493
494 addr = simple_strtoul(argv[2], NULL, 16);
495
496 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
497
498 /* Copy header so we can blank CRC field for re-calculation */
499 memmove (&header, (char *)addr, sizeof(image_header_t));
500
501 if (hdr->ih_magic != IH_MAGIC) {
502 printf ("Bad Magic Number\n");
503 SHOW_BOOT_PROGRESS (-10);
504 do_reset (cmdtp, flag, argc, argv);
505 }
506
507 data = (ulong)&header;
508 len = sizeof(image_header_t);
509
510 checksum = hdr->ih_hcrc;
511 hdr->ih_hcrc = 0;
512
513 if (crc32 (0, (char *)data, len) != checksum) {
514 printf ("Bad Header Checksum\n");
515 SHOW_BOOT_PROGRESS (-11);
516 do_reset (cmdtp, flag, argc, argv);
517 }
518
519 SHOW_BOOT_PROGRESS (10);
520
521 print_image_hdr (hdr);
522
523 data = addr + sizeof(image_header_t);
524 len = hdr->ih_size;
525
526 if (verify) {
527 ulong csum = 0;
528#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
529 ulong cdata = data, edata = cdata + len;
530#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
531
532 printf (" Verifying Checksum ... ");
533
534#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
535
536 while (cdata < edata) {
537 ulong chunk = edata - cdata;
538
539 if (chunk > CHUNKSZ)
540 chunk = CHUNKSZ;
541 csum = crc32 (csum, (char *)cdata, chunk);
542 cdata += chunk;
543
544 WATCHDOG_RESET();
545 }
546#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
547 csum = crc32 (0, (char *)data, len);
548#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
549
550 if (csum != hdr->ih_dcrc) {
551 printf ("Bad Data CRC\n");
552 SHOW_BOOT_PROGRESS (-12);
553 do_reset (cmdtp, flag, argc, argv);
554 }
555 printf ("OK\n");
556 }
557
558 SHOW_BOOT_PROGRESS (11);
559
560 if ((hdr->ih_os != IH_OS_LINUX) ||
561 (hdr->ih_arch != IH_CPU_PPC) ||
562 (hdr->ih_type != IH_TYPE_RAMDISK) ) {
563 printf ("No Linux PPC Ramdisk Image\n");
564 SHOW_BOOT_PROGRESS (-13);
565 do_reset (cmdtp, flag, argc, argv);
566 }
567
568 /*
569 * Now check if we have a multifile image
570 */
571 } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
572 u_long tail = ntohl(len_ptr[0]) % 4;
573 int i;
574
575 SHOW_BOOT_PROGRESS (13);
576
577 /* skip kernel length and terminator */
578 data = (ulong)(&len_ptr[2]);
579 /* skip any additional image length fields */
580 for (i=1; len_ptr[i]; ++i)
581 data += 4;
582 /* add kernel length, and align */
583 data += ntohl(len_ptr[0]);
584 if (tail) {
585 data += 4 - tail;
586 }
587
588 len = ntohl(len_ptr[1]);
589
590 } else {
591 /*
592 * no initrd image
593 */
594 SHOW_BOOT_PROGRESS (14);
595
596 len = data = 0;
597 }
598
47d1a6e1 599 if (!data) {
56f94be3 600 debug ("No initrd\n");
47d1a6e1 601 }
47d1a6e1
WD
602
603 if (data) {
38b99261
WD
604 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
605 initrd_start = data;
606 initrd_end = initrd_start + len;
607 } else {
47d1a6e1
WD
608 initrd_start = (ulong)kbd - len;
609 initrd_start &= ~(4096 - 1); /* align on page */
610
611 if (initrd_high) {
612 ulong nsp;
613
614 /*
615 * the inital ramdisk does not need to be within
616 * CFG_BOOTMAPSZ as it is not accessed until after
617 * the mm system is initialised.
618 *
619 * do the stack bottom calculation again and see if
620 * the initrd will fit just below the monitor stack
621 * bottom without overwriting the area allocated
622 * above for command line args and board info.
623 */
624 asm( "mr %0,1": "=r"(nsp) : );
625 nsp -= 2048; /* just to be sure */
626 nsp &= ~0xF;
627 if (nsp > initrd_high) /* limit as specified */
628 nsp = initrd_high;
629 nsp -= len;
630 nsp &= ~(4096 - 1); /* align on page */
631 if (nsp >= sp)
632 initrd_start = nsp;
633 }
634
635 SHOW_BOOT_PROGRESS (12);
56f94be3
WD
636
637 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
47d1a6e1 638 data, data + len - 1, len, len);
56f94be3 639
47d1a6e1
WD
640 initrd_end = initrd_start + len;
641 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
642 initrd_start, initrd_end);
643#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
644 {
645 size_t l = len;
646 void *to = (void *)initrd_start;
647 void *from = (void *)data;
648
649 while (l > 0) {
650 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
651 WATCHDOG_RESET();
652 memmove (to, from, tail);
653 to += tail;
654 from += tail;
655 l -= tail;
656 }
657 }
658#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
659 memmove ((void *)initrd_start, (void *)data, len);
660#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
661 printf ("OK\n");
38b99261 662 }
47d1a6e1
WD
663 } else {
664 initrd_start = 0;
665 initrd_end = 0;
666 }
667
56f94be3
WD
668
669 debug ("## Transferring control to Linux (at address %08lx) ...\n",
47d1a6e1 670 (ulong)kernel);
56f94be3 671
47d1a6e1
WD
672 SHOW_BOOT_PROGRESS (15);
673
674#ifdef CFG_INIT_RAM_LOCK
675 unlock_ram_in_cache();
676#endif
677 /*
678 * Linux Kernel Parameters:
679 * r3: ptr to board info data
680 * r4: initrd_start or 0 if no initrd
681 * r5: initrd_end - unused if r4 is 0
682 * r6: Start of command line string
683 * r7: End of command line string
684 */
685 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
686}
1cb8e980 687#endif /* CONFIG_PPC */
47d1a6e1
WD
688
689static void
690do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
691 int argc, char *argv[],
692 ulong addr,
693 ulong *len_ptr,
694 int verify)
695{
696 DECLARE_GLOBAL_DATA_PTR;
697
698 image_header_t *hdr = &header;
699
700 void (*loader)(bd_t *, image_header_t *, char *, char *);
701 image_header_t *img_addr;
702 char *consdev;
703 char *cmdline;
704
705
706 /*
707 * Booting a (NetBSD) kernel image
708 *
709 * This process is pretty similar to a standalone application:
710 * The (first part of an multi-) image must be a stage-2 loader,
711 * which in turn is responsible for loading & invoking the actual
712 * kernel. The only differences are the parameters being passed:
713 * besides the board info strucure, the loader expects a command
714 * line, the name of the console device, and (optionally) the
715 * address of the original image header.
716 */
717
718 img_addr = 0;
719 if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
720 img_addr = (image_header_t *) addr;
721
722
723 consdev = "";
724#if defined (CONFIG_8xx_CONS_SMC1)
725 consdev = "smc1";
726#elif defined (CONFIG_8xx_CONS_SMC2)
727 consdev = "smc2";
728#elif defined (CONFIG_8xx_CONS_SCC2)
729 consdev = "scc2";
730#elif defined (CONFIG_8xx_CONS_SCC3)
731 consdev = "scc3";
732#endif
733
734 if (argc > 2) {
735 ulong len;
736 int i;
737
738 for (i=2, len=0 ; i<argc ; i+=1)
739 len += strlen (argv[i]) + 1;
740 cmdline = malloc (len);
741
742 for (i=2, len=0 ; i<argc ; i+=1) {
743 if (i > 2)
744 cmdline[len++] = ' ';
745 strcpy (&cmdline[len], argv[i]);
746 len += strlen (argv[i]);
747 }
748 } else if ((cmdline = getenv("bootargs")) == NULL) {
749 cmdline = "";
750 }
751
752 loader = (void (*)(bd_t *, image_header_t *, char *, char *)) hdr->ih_ep;
753
754 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
755 (ulong)loader);
756
757 SHOW_BOOT_PROGRESS (15);
758
759 /*
760 * NetBSD Stage-2 Loader Parameters:
761 * r3: ptr to board info data
762 * r4: image address
763 * r5: console device
764 * r6: boot args string
765 */
766 (*loader) (gd->bd, img_addr, consdev, cmdline);
767}
768
7f70e853
WD
769#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
770
771/* Function that returns a character from the environment */
772extern uchar (*env_get_char)(int);
773
774static void
775do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
776 int argc, char *argv[],
777 ulong addr,
778 ulong *len_ptr,
779 int verify)
780{
781 DECLARE_GLOBAL_DATA_PTR;
782 ulong top;
783 char *s, *cmdline;
784 char **fwenv, **ss;
785 int i, j, nxt, len, envno, envsz;
786 bd_t *kbd;
787 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
788 image_header_t *hdr = &header;
789
790 /*
791 * Booting an ARTOS kernel image + application
792 */
793
794 /* this used to be the top of memory, but was wrong... */
795#ifdef CONFIG_PPC
796 /* get stack pointer */
797 asm volatile ("mr %0,1" : "=r"(top) );
798#endif
799 debug ("## Current stack ends at 0x%08lX ", top);
800
801 top -= 2048; /* just to be sure */
802 if (top > CFG_BOOTMAPSZ)
803 top = CFG_BOOTMAPSZ;
804 top &= ~0xF;
805
806 debug ("=> set upper limit to 0x%08lX\n", top);
807
808 /* first check the artos specific boot args, then the linux args*/
809 if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
810 s = "";
811
812 /* get length of cmdline, and place it */
813 len = strlen(s);
814 top = (top - (len + 1)) & ~0xF;
815 cmdline = (char *)top;
816 debug ("## cmdline at 0x%08lX ", top);
817 strcpy(cmdline, s);
818
819 /* copy bdinfo */
820 top = (top - sizeof(bd_t)) & ~0xF;
821 debug ("## bd at 0x%08lX ", top);
822 kbd = (bd_t *)top;
823 memcpy(kbd, gd->bd, sizeof(bd_t));
824
825 /* first find number of env entries, and their size */
826 envno = 0;
827 envsz = 0;
828 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
829 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
830 ;
831 envno++;
832 envsz += (nxt - i) + 1; /* plus trailing zero */
833 }
834 envno++; /* plus the terminating zero */
835 debug ("## %u envvars total size %u ", envno, envsz);
836
837 top = (top - sizeof(char **)*envno) & ~0xF;
838 fwenv = (char **)top;
839 debug ("## fwenv at 0x%08lX ", top);
840
841 top = (top - envsz) & ~0xF;
842 s = (char *)top;
843 ss = fwenv;
844
845 /* now copy them */
846 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
847 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
848 ;
849 *ss++ = s;
850 for (j = i; j < nxt; ++j)
851 *s++ = env_get_char(j);
852 *s++ = '\0';
853 }
854 *ss++ = NULL; /* terminate */
855
856 entry = (void (*)(bd_t *, char *, char **, ulong))ntohl(hdr->ih_ep);
857 (*entry)(kbd, cmdline, fwenv, top);
858}
859#endif
860
861
47d1a6e1
WD
862#if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
863int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
864{
865 int rcode = 0;
866#ifndef CFG_HUSH_PARSER
867 if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
868#else
869 if (parse_string_outer(getenv("bootcmd"),
870 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
871#endif
872 return rcode;
873}
8bde7f77 874
0d498393
WD
875U_BOOT_CMD(
876 boot, 1, 1, do_bootd,
9d2b18a0
WD
877 "boot - boot default, i.e., run 'bootcmd'\n",
878 NULL
879);
880
881/* keep old command name "bootd" for backward compatibility */
0d498393
WD
882U_BOOT_CMD(
883 bootd, 1, 1, do_bootd,
8bde7f77
WD
884 "bootd - boot default, i.e., run 'bootcmd'\n",
885 NULL
886);
887
47d1a6e1
WD
888#endif
889
890#if (CONFIG_COMMANDS & CFG_CMD_IMI)
891int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
892{
893 int arg;
894 ulong addr;
895 int rcode=0;
896
897 if (argc < 2) {
898 return image_info (load_addr);
899 }
900
901 for (arg=1; arg <argc; ++arg) {
902 addr = simple_strtoul(argv[arg], NULL, 16);
903 if (image_info (addr) != 0) rcode = 1;
904 }
905 return rcode;
906}
907
908static int image_info (ulong addr)
909{
910 ulong data, len, checksum;
911 image_header_t *hdr = &header;
912
913 printf ("\n## Checking Image at %08lx ...\n", addr);
914
915 /* Copy header so we can blank CRC field for re-calculation */
916 memmove (&header, (char *)addr, sizeof(image_header_t));
917
918 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
919 printf (" Bad Magic Number\n");
920 return 1;
921 }
922
923 data = (ulong)&header;
924 len = sizeof(image_header_t);
925
926 checksum = ntohl(hdr->ih_hcrc);
927 hdr->ih_hcrc = 0;
928
929 if (crc32 (0, (char *)data, len) != checksum) {
930 printf (" Bad Header Checksum\n");
931 return 1;
932 }
933
934 /* for multi-file images we need the data part, too */
935 print_image_hdr ((image_header_t *)addr);
936
937 data = addr + sizeof(image_header_t);
938 len = ntohl(hdr->ih_size);
939
940 printf (" Verifying Checksum ... ");
941 if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
942 printf (" Bad Data CRC\n");
943 return 1;
944 }
945 printf ("OK\n");
946 return 0;
947}
0d498393
WD
948
949U_BOOT_CMD(
950 iminfo, CFG_MAXARGS, 1, do_iminfo,
8bde7f77
WD
951 "iminfo - print header information for application image\n",
952 "addr [addr ...]\n"
953 " - print header information for application image starting at\n"
954 " address 'addr' in memory; this includes verification of the\n"
955 " image contents (magic number, header and payload checksums)\n"
956);
957
47d1a6e1
WD
958#endif /* CFG_CMD_IMI */
959
960void
961print_image_hdr (image_header_t *hdr)
962{
963#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
964 time_t timestamp = (time_t)ntohl(hdr->ih_time);
965 struct rtc_time tm;
966#endif
967
968 printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
969#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
970 to_tm (timestamp, &tm);
971 printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
972 tm.tm_year, tm.tm_mon, tm.tm_mday,
973 tm.tm_hour, tm.tm_min, tm.tm_sec);
974#endif /* CFG_CMD_DATE, CONFIG_TIMESTAMP */
975 printf (" Image Type: "); print_type(hdr); printf ("\n");
976 printf (" Data Size: %d Bytes = ", ntohl(hdr->ih_size));
977 print_size (ntohl(hdr->ih_size), "\n");
978 printf (" Load Address: %08x\n", ntohl(hdr->ih_load));
979 printf (" Entry Point: %08x\n", ntohl(hdr->ih_ep));
980
981 if (hdr->ih_type == IH_TYPE_MULTI) {
982 int i;
983 ulong len;
984 ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
985
986 printf (" Contents:\n");
987 for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
988 printf (" Image %d: %8ld Bytes = ", i, len);
989 print_size (len, "\n");
990 }
991 }
992}
993
994
995static void
996print_type (image_header_t *hdr)
997{
998 char *os, *arch, *type, *comp;
999
1000 switch (hdr->ih_os) {
1001 case IH_OS_INVALID: os = "Invalid OS"; break;
1002 case IH_OS_NETBSD: os = "NetBSD"; break;
1003 case IH_OS_LINUX: os = "Linux"; break;
1004 case IH_OS_VXWORKS: os = "VxWorks"; break;
1005 case IH_OS_QNX: os = "QNX"; break;
1006 case IH_OS_U_BOOT: os = "U-Boot"; break;
d791b1dc 1007 case IH_OS_RTEMS: os = "RTEMS"; break;
7f70e853
WD
1008#ifdef CONFIG_ARTOS
1009 case IH_OS_ARTOS: os = "ARTOS"; break;
1010#endif
47d1a6e1
WD
1011 default: os = "Unknown OS"; break;
1012 }
1013
1014 switch (hdr->ih_arch) {
1015 case IH_CPU_INVALID: arch = "Invalid CPU"; break;
1016 case IH_CPU_ALPHA: arch = "Alpha"; break;
1017 case IH_CPU_ARM: arch = "ARM"; break;
1018 case IH_CPU_I386: arch = "Intel x86"; break;
1019 case IH_CPU_IA64: arch = "IA64"; break;
1020 case IH_CPU_MIPS: arch = "MIPS"; break;
1021 case IH_CPU_MIPS64: arch = "MIPS 64 Bit"; break;
1022 case IH_CPU_PPC: arch = "PowerPC"; break;
1023 case IH_CPU_S390: arch = "IBM S390"; break;
1024 case IH_CPU_SH: arch = "SuperH"; break;
1025 case IH_CPU_SPARC: arch = "SPARC"; break;
1026 case IH_CPU_SPARC64: arch = "SPARC 64 Bit"; break;
8564acf9 1027 case IH_CPU_M68K: arch = "M68K"; break;
47d1a6e1
WD
1028 default: arch = "Unknown Architecture"; break;
1029 }
1030
1031 switch (hdr->ih_type) {
1032 case IH_TYPE_INVALID: type = "Invalid Image"; break;
1033 case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
1034 case IH_TYPE_KERNEL: type = "Kernel Image"; break;
1035 case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
1036 case IH_TYPE_MULTI: type = "Multi-File Image"; break;
1037 case IH_TYPE_FIRMWARE: type = "Firmware"; break;
1038 case IH_TYPE_SCRIPT: type = "Script"; break;
1039 default: type = "Unknown Image"; break;
1040 }
1041
1042 switch (hdr->ih_comp) {
1043 case IH_COMP_NONE: comp = "uncompressed"; break;
1044 case IH_COMP_GZIP: comp = "gzip compressed"; break;
1045 case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
1046 default: comp = "unknown compression"; break;
1047 }
1048
1049 printf ("%s %s %s (%s)", arch, os, type, comp);
1050}
1051
1052#define ZALLOC_ALIGNMENT 16
1053
1054static void *zalloc(void *x, unsigned items, unsigned size)
1055{
1056 void *p;
1057
1058 size *= items;
1059 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
1060
1061 p = malloc (size);
1062
1063 return (p);
1064}
1065
1066static void zfree(void *x, void *addr, unsigned nb)
1067{
1068 free (addr);
1069}
1070
1071#define HEAD_CRC 2
1072#define EXTRA_FIELD 4
1073#define ORIG_NAME 8
1074#define COMMENT 0x10
1075#define RESERVED 0xe0
1076
1077#define DEFLATED 8
1078
1079int gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
1080{
1081 z_stream s;
1082 int r, i, flags;
1083
1084 /* skip header */
1085 i = 10;
1086 flags = src[3];
1087 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
1088 printf ("Error: Bad gzipped data\n");
1089 return (-1);
1090 }
1091 if ((flags & EXTRA_FIELD) != 0)
1092 i = 12 + src[10] + (src[11] << 8);
1093 if ((flags & ORIG_NAME) != 0)
1094 while (src[i++] != 0)
1095 ;
1096 if ((flags & COMMENT) != 0)
1097 while (src[i++] != 0)
1098 ;
1099 if ((flags & HEAD_CRC) != 0)
1100 i += 2;
1101 if (i >= *lenp) {
1102 printf ("Error: gunzip out of data in header\n");
1103 return (-1);
1104 }
1105
1106 s.zalloc = zalloc;
1107 s.zfree = zfree;
1108#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
1109 s.outcb = (cb_func)WATCHDOG_RESET;
1110#else
1111 s.outcb = Z_NULL;
1112#endif /* CONFIG_HW_WATCHDOG */
1113
1114 r = inflateInit2(&s, -MAX_WBITS);
1115 if (r != Z_OK) {
1116 printf ("Error: inflateInit2() returned %d\n", r);
1117 return (-1);
1118 }
1119 s.next_in = src + i;
1120 s.avail_in = *lenp - i;
1121 s.next_out = dst;
1122 s.avail_out = dstlen;
1123 r = inflate(&s, Z_FINISH);
1124 if (r != Z_OK && r != Z_STREAM_END) {
1125 printf ("Error: inflate() returned %d\n", r);
1126 return (-1);
1127 }
1128 *lenp = s.next_out - (unsigned char *) dst;
1129 inflateEnd(&s);
1130
1131 return (0);
1132}
1133
d791b1dc
WD
1134static void
1135do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1136 ulong addr, ulong *len_ptr, int verify)
1137{
1138 DECLARE_GLOBAL_DATA_PTR;
1139 image_header_t *hdr = &header;
1140 void (*entry_point)(bd_t *);
1141
1142 entry_point = (void (*)(bd_t *)) hdr->ih_ep;
1143
1144 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
1145 (ulong)entry_point);
1146
1147 SHOW_BOOT_PROGRESS (15);
1148
1149 /*
1150 * RTEMS Parameters:
1151 * r3: ptr to board info data
1152 */
1153
1154 (*entry_point ) ( gd->bd );
1155}
1156
47d1a6e1
WD
1157#if (CONFIG_COMMANDS & CFG_CMD_ELF)
1158static void
1159do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1160 ulong addr, ulong *len_ptr, int verify)
1161{
1162 image_header_t *hdr = &header;
1163 char str[80];
1164
1165 sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
1166 setenv("loadaddr", str);
1167 do_bootvx(cmdtp, 0, 0, NULL);
1168}
1169
1170static void
1171do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1172 ulong addr, ulong *len_ptr, int verify)
1173{
1174 image_header_t *hdr = &header;
1175 char *local_args[2];
1176 char str[16];
1177
1178 sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
1179 local_args[0] = argv[0];
1180 local_args[1] = str; /* and provide it via the arguments */
1181 do_bootelf(cmdtp, 0, 2, local_args);
1182}
1183#endif /* CFG_CMD_ELF */