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