blob: e138d78b9b406c347ed64b3bbe401a666e923f03 [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>
34#include <asm/byteorder.h>
35#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
36#include <rtc.h>
37#endif
38
39#ifdef CFG_HUSH_PARSER
40#include <hush.h>
41#endif
42
43#ifdef CONFIG_SHOW_BOOT_PROGRESS
44# include <status_led.h>
45# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
46#else
47# define SHOW_BOOT_PROGRESS(arg)
48#endif
49
50#ifdef CFG_INIT_RAM_LOCK
51#include <asm/cache.h>
52#endif
53
wdenk228f29a2002-12-08 09:53:23 +000054#ifdef CONFIG_LOGBUFFER
55#include <logbuff.h>
56#endif
57
wdenk47d1a6e2002-11-03 00:01:44 +000058/*
59 * Some systems (for example LWMON) have very short watchdog periods;
60 * we must make sure to split long operations like memmove() or
61 * crc32() into reasonable chunks.
62 */
63#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
64# define CHUNKSZ (64 * 1024)
65#endif
66
67int gunzip (void *, int, unsigned char *, int *);
68
69static void *zalloc(void *, unsigned, unsigned);
70static void zfree(void *, void *, unsigned);
71
72#if (CONFIG_COMMANDS & CFG_CMD_IMI)
73static int image_info (unsigned long addr);
74#endif
75static void print_type (image_header_t *hdr);
76
wdenk2262cfe2002-11-18 00:14:45 +000077#ifdef __I386__
78image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
79#endif
80
wdenk47d1a6e2002-11-03 00:01:44 +000081/*
82 * Continue booting an OS image; caller already has:
83 * - copied image header to global variable `header'
84 * - checked header magic number, checksums (both header & image),
85 * - verified image architecture (PPC) and type (KERNEL or MULTI),
86 * - loaded (first part of) image to header load address,
87 * - disabled interrupts.
88 */
89typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
90 int argc, char *argv[],
91 ulong addr, /* of image to boot */
92 ulong *len_ptr, /* multi-file image length table */
93 int verify); /* getenv("verify")[0] != 'n' */
94
wdenk2262cfe2002-11-18 00:14:45 +000095#ifdef CONFIG_PPC
wdenk47d1a6e2002-11-03 00:01:44 +000096static boot_os_Fcn do_bootm_linux;
97#else
98extern boot_os_Fcn do_bootm_linux;
99#endif
100static boot_os_Fcn do_bootm_netbsd;
101#if (CONFIG_COMMANDS & CFG_CMD_ELF)
102static boot_os_Fcn do_bootm_vxworks;
103static boot_os_Fcn do_bootm_qnxelf;
104int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
105int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
106#endif /* CFG_CMD_ELF */
107
108image_header_t header;
109
110ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
111
112int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
113{
114 ulong iflag;
115 ulong addr;
116 ulong data, len, checksum;
117 ulong *len_ptr;
118 int i, verify;
119 char *name, *s;
120 int (*appl)(cmd_tbl_t *, int, int, char *[]);
121 image_header_t *hdr = &header;
122
123 s = getenv ("verify");
124 verify = (s && (*s == 'n')) ? 0 : 1;
125
126 if (argc < 2) {
127 addr = load_addr;
128 } else {
129 addr = simple_strtoul(argv[1], NULL, 16);
130 }
131
132 SHOW_BOOT_PROGRESS (1);
133 printf ("## Booting image at %08lx ...\n", addr);
134
135 /* Copy header so we can blank CRC field for re-calculation */
136 memmove (&header, (char *)addr, sizeof(image_header_t));
137
138 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
wdenk2262cfe2002-11-18 00:14:45 +0000139#ifdef __I386__ /* correct image format not implemented yet - fake it */
140 if (fake_header(hdr, (void*)addr, -1) != NULL) {
141 /* to compensate for the addition below */
142 addr -= sizeof(image_header_t);
143 /* turnof verify,
144 * fake_header() does not fake the data crc
145 */
146 verify = 0;
147 } else
148#endif /* __I386__ */
149 {
wdenk47d1a6e2002-11-03 00:01:44 +0000150 printf ("Bad Magic Number\n");
151 SHOW_BOOT_PROGRESS (-1);
152 return 1;
wdenk2262cfe2002-11-18 00:14:45 +0000153 }
wdenk47d1a6e2002-11-03 00:01:44 +0000154 }
155 SHOW_BOOT_PROGRESS (2);
156
157 data = (ulong)&header;
158 len = sizeof(image_header_t);
159
160 checksum = ntohl(hdr->ih_hcrc);
161 hdr->ih_hcrc = 0;
162
163 if (crc32 (0, (char *)data, len) != checksum) {
164 printf ("Bad Header Checksum\n");
165 SHOW_BOOT_PROGRESS (-2);
166 return 1;
167 }
168 SHOW_BOOT_PROGRESS (3);
169
170 /* for multi-file images we need the data part, too */
wdenk2262cfe2002-11-18 00:14:45 +0000171 print_image_hdr (hdr);
wdenk47d1a6e2002-11-03 00:01:44 +0000172
173 data = addr + sizeof(image_header_t);
174 len = ntohl(hdr->ih_size);
175
176 if (verify) {
177 printf (" Verifying Checksum ... ");
178 if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
179 printf ("Bad Data CRC\n");
180 SHOW_BOOT_PROGRESS (-3);
181 return 1;
182 }
183 printf ("OK\n");
184 }
185 SHOW_BOOT_PROGRESS (4);
186
187 len_ptr = (ulong *)data;
188
wdenk2262cfe2002-11-18 00:14:45 +0000189#if defined(__PPC__)
190 if (hdr->ih_arch != IH_CPU_PPC)
191#elif defined(__ARM__)
192 if (hdr->ih_arch != IH_CPU_ARM)
193#elif defined(__I386__)
194 if (hdr->ih_arch != IH_CPU_I386)
195#else
196# error Unknown CPU type
197#endif
198 {
199 printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch);
wdenk47d1a6e2002-11-03 00:01:44 +0000200 SHOW_BOOT_PROGRESS (-4);
201 return 1;
202 }
203 SHOW_BOOT_PROGRESS (5);
204
205 switch (hdr->ih_type) {
206 case IH_TYPE_STANDALONE: name = "Standalone Application";
207 break;
208 case IH_TYPE_KERNEL: name = "Kernel Image";
209 break;
210 case IH_TYPE_MULTI: name = "Multi-File Image";
211 len = ntohl(len_ptr[0]);
212 /* OS kernel is always the first image */
213 data += 8; /* kernel_len + terminator */
214 for (i=1; len_ptr[i]; ++i)
215 data += 4;
216 break;
217 default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
218 SHOW_BOOT_PROGRESS (-5);
219 return 1;
220 }
221 SHOW_BOOT_PROGRESS (6);
222
223 /*
224 * We have reached the point of no return: we are going to
225 * overwrite all exception vector code, so we cannot easily
226 * recover from any failures any more...
227 */
228
229 iflag = disable_interrupts();
230
wdenkc7de8292002-11-19 11:04:11 +0000231#ifdef CONFIG_AMIGAONEG3SE
232 /*
233 * We've possible left the caches enabled during
234 * bios emulation, so turn them off again
235 */
236 icache_disable();
237 invalidate_l1_instruction_cache();
238 flush_data_cache();
239 dcache_disable();
240#endif
241
wdenk47d1a6e2002-11-03 00:01:44 +0000242 switch (hdr->ih_comp) {
243 case IH_COMP_NONE:
wdenk2262cfe2002-11-18 00:14:45 +0000244 if(ntohl(hdr->ih_load) == addr) {
wdenk47d1a6e2002-11-03 00:01:44 +0000245 printf (" XIP %s ... ", name);
246 } else {
247#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
248 size_t l = len;
249 void *to = (void *)ntohl(hdr->ih_load);
250 void *from = (void *)data;
251
252 printf (" Loading %s ... ", name);
253
254 while (l > 0) {
255 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
256 WATCHDOG_RESET();
257 memmove (to, from, tail);
258 to += tail;
259 from += tail;
260 l -= tail;
261 }
262#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
263 memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
264#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
265 }
266 break;
267 case IH_COMP_GZIP:
268 printf (" Uncompressing %s ... ", name);
269 if (gunzip ((void *)ntohl(hdr->ih_load), 0x400000,
270 (uchar *)data, (int *)&len) != 0) {
271 printf ("GUNZIP ERROR - must RESET board to recover\n");
272 SHOW_BOOT_PROGRESS (-6);
273 do_reset (cmdtp, flag, argc, argv);
274 }
275 break;
276 default:
277 if (iflag)
278 enable_interrupts();
279 printf ("Unimplemented compression type %d\n", hdr->ih_comp);
280 SHOW_BOOT_PROGRESS (-7);
281 return 1;
282 }
283 printf ("OK\n");
284 SHOW_BOOT_PROGRESS (7);
285
286 switch (hdr->ih_type) {
287 case IH_TYPE_STANDALONE:
288 appl = (int (*)(cmd_tbl_t *, int, int, char *[]))ntohl(hdr->ih_ep);
289 if (iflag)
290 enable_interrupts();
291
292 (*appl)(cmdtp, flag, argc-1, &argv[1]);
293 break;
294 case IH_TYPE_KERNEL:
295 case IH_TYPE_MULTI:
296 /* handled below */
297 break;
298 default:
299 if (iflag)
300 enable_interrupts();
301 printf ("Can't boot image type %d\n", hdr->ih_type);
302 SHOW_BOOT_PROGRESS (-8);
303 return 1;
304 }
305 SHOW_BOOT_PROGRESS (8);
306
307 switch (hdr->ih_os) {
308 default: /* handled by (original) Linux case */
309 case IH_OS_LINUX:
310 do_bootm_linux (cmdtp, flag, argc, argv,
311 addr, len_ptr, verify);
312 break;
313 case IH_OS_NETBSD:
314 do_bootm_netbsd (cmdtp, flag, argc, argv,
315 addr, len_ptr, verify);
316 break;
317#if (CONFIG_COMMANDS & CFG_CMD_ELF)
318 case IH_OS_VXWORKS:
319 do_bootm_vxworks (cmdtp, flag, argc, argv,
320 addr, len_ptr, verify);
321 break;
322 case IH_OS_QNX:
323 do_bootm_qnxelf (cmdtp, flag, argc, argv,
324 addr, len_ptr, verify);
325 break;
326#endif /* CFG_CMD_ELF */
327 }
328
329 SHOW_BOOT_PROGRESS (-9);
330#ifdef DEBUG
331 printf ("\n## Control returned to monitor - resetting...\n");
332 do_reset (cmdtp, flag, argc, argv);
333#endif
334 return 1;
335}
336
wdenk2262cfe2002-11-18 00:14:45 +0000337#ifdef CONFIG_PPC
wdenk47d1a6e2002-11-03 00:01:44 +0000338static void
339do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
340 int argc, char *argv[],
341 ulong addr,
342 ulong *len_ptr,
343 int verify)
344{
345 DECLARE_GLOBAL_DATA_PTR;
346
347 ulong sp;
348 ulong len, checksum;
349 ulong initrd_start, initrd_end;
350 ulong cmd_start, cmd_end;
351 ulong initrd_high;
352 ulong data;
353 char *cmdline;
354 char *s;
355 bd_t *kbd;
356 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
357 image_header_t *hdr = &header;
358
359 if ((s = getenv ("initrd_high")) != NULL) {
360 /* a value of "no" or a similar string will act like 0,
361 * turning the "load high" feature off. This is intentional.
362 */
363 initrd_high = simple_strtoul(s, NULL, 16);
wdenk228f29a2002-12-08 09:53:23 +0000364 } else { /* not set, no restrictions to load high */
wdenk47d1a6e2002-11-03 00:01:44 +0000365 initrd_high = ~0;
366 }
367
wdenk56f94be2002-11-05 16:35:14 +0000368#ifdef CONFIG_LOGBUFFER
wdenk6aff3112002-12-17 01:51:00 +0000369 kbd=gd->bd;
wdenk228f29a2002-12-08 09:53:23 +0000370 /* Prevent initrd from overwriting logbuffer */
371 if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
372 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
373 debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
wdenk56f94be2002-11-05 16:35:14 +0000374#endif
375
wdenk47d1a6e2002-11-03 00:01:44 +0000376 /*
377 * Booting a (Linux) kernel image
378 *
379 * Allocate space for command line and board info - the
380 * address should be as high as possible within the reach of
381 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
382 * memory, which means far enough below the current stack
383 * pointer.
384 */
385
386 asm( "mr %0,1": "=r"(sp) : );
387
wdenk56f94be2002-11-05 16:35:14 +0000388 debug ("## Current stack ends at 0x%08lX ", sp);
389
wdenk47d1a6e2002-11-03 00:01:44 +0000390 sp -= 2048; /* just to be sure */
391 if (sp > CFG_BOOTMAPSZ)
392 sp = CFG_BOOTMAPSZ;
393 sp &= ~0xF;
394
wdenk56f94be2002-11-05 16:35:14 +0000395 debug ("=> set upper limit to 0x%08lX\n", sp);
396
wdenk47d1a6e2002-11-03 00:01:44 +0000397 cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
398 kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
399
400 if ((s = getenv("bootargs")) == NULL)
401 s = "";
402
403 strcpy (cmdline, s);
404
405 cmd_start = (ulong)&cmdline[0];
406 cmd_end = cmd_start + strlen(cmdline);
407
408 *kbd = *(gd->bd);
409
410#ifdef DEBUG
411 printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
412
413 do_bdinfo (NULL, 0, 0, NULL);
414#endif
415
416 if ((s = getenv ("clocks_in_mhz")) != NULL) {
417 /* convert all clock information to MHz */
418 kbd->bi_intfreq /= 1000000L;
419 kbd->bi_busfreq /= 1000000L;
420#if defined(CONFIG_8260)
421 kbd->bi_cpmfreq /= 1000000L;
422 kbd->bi_brgfreq /= 1000000L;
423 kbd->bi_sccfreq /= 1000000L;
424 kbd->bi_vco /= 1000000L;
425#endif /* CONFIG_8260 */
426 }
427
428 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))hdr->ih_ep;
429
430 /*
431 * Check if there is an initrd image
432 */
433 if (argc >= 3) {
434 SHOW_BOOT_PROGRESS (9);
435
436 addr = simple_strtoul(argv[2], NULL, 16);
437
438 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
439
440 /* Copy header so we can blank CRC field for re-calculation */
441 memmove (&header, (char *)addr, sizeof(image_header_t));
442
443 if (hdr->ih_magic != IH_MAGIC) {
444 printf ("Bad Magic Number\n");
445 SHOW_BOOT_PROGRESS (-10);
446 do_reset (cmdtp, flag, argc, argv);
447 }
448
449 data = (ulong)&header;
450 len = sizeof(image_header_t);
451
452 checksum = hdr->ih_hcrc;
453 hdr->ih_hcrc = 0;
454
455 if (crc32 (0, (char *)data, len) != checksum) {
456 printf ("Bad Header Checksum\n");
457 SHOW_BOOT_PROGRESS (-11);
458 do_reset (cmdtp, flag, argc, argv);
459 }
460
461 SHOW_BOOT_PROGRESS (10);
462
463 print_image_hdr (hdr);
464
465 data = addr + sizeof(image_header_t);
466 len = hdr->ih_size;
467
468 if (verify) {
469 ulong csum = 0;
470#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
471 ulong cdata = data, edata = cdata + len;
472#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
473
474 printf (" Verifying Checksum ... ");
475
476#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
477
478 while (cdata < edata) {
479 ulong chunk = edata - cdata;
480
481 if (chunk > CHUNKSZ)
482 chunk = CHUNKSZ;
483 csum = crc32 (csum, (char *)cdata, chunk);
484 cdata += chunk;
485
486 WATCHDOG_RESET();
487 }
488#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
489 csum = crc32 (0, (char *)data, len);
490#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
491
492 if (csum != hdr->ih_dcrc) {
493 printf ("Bad Data CRC\n");
494 SHOW_BOOT_PROGRESS (-12);
495 do_reset (cmdtp, flag, argc, argv);
496 }
497 printf ("OK\n");
498 }
499
500 SHOW_BOOT_PROGRESS (11);
501
502 if ((hdr->ih_os != IH_OS_LINUX) ||
503 (hdr->ih_arch != IH_CPU_PPC) ||
504 (hdr->ih_type != IH_TYPE_RAMDISK) ) {
505 printf ("No Linux PPC Ramdisk Image\n");
506 SHOW_BOOT_PROGRESS (-13);
507 do_reset (cmdtp, flag, argc, argv);
508 }
509
510 /*
511 * Now check if we have a multifile image
512 */
513 } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
514 u_long tail = ntohl(len_ptr[0]) % 4;
515 int i;
516
517 SHOW_BOOT_PROGRESS (13);
518
519 /* skip kernel length and terminator */
520 data = (ulong)(&len_ptr[2]);
521 /* skip any additional image length fields */
522 for (i=1; len_ptr[i]; ++i)
523 data += 4;
524 /* add kernel length, and align */
525 data += ntohl(len_ptr[0]);
526 if (tail) {
527 data += 4 - tail;
528 }
529
530 len = ntohl(len_ptr[1]);
531
532 } else {
533 /*
534 * no initrd image
535 */
536 SHOW_BOOT_PROGRESS (14);
537
538 len = data = 0;
539 }
540
wdenk47d1a6e2002-11-03 00:01:44 +0000541 if (!data) {
wdenk56f94be2002-11-05 16:35:14 +0000542 debug ("No initrd\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000543 }
wdenk47d1a6e2002-11-03 00:01:44 +0000544
545 if (data) {
546 initrd_start = (ulong)kbd - len;
547 initrd_start &= ~(4096 - 1); /* align on page */
548
549 if (initrd_high) {
550 ulong nsp;
551
552 /*
553 * the inital ramdisk does not need to be within
554 * CFG_BOOTMAPSZ as it is not accessed until after
555 * the mm system is initialised.
556 *
557 * do the stack bottom calculation again and see if
558 * the initrd will fit just below the monitor stack
559 * bottom without overwriting the area allocated
560 * above for command line args and board info.
561 */
562 asm( "mr %0,1": "=r"(nsp) : );
563 nsp -= 2048; /* just to be sure */
564 nsp &= ~0xF;
565 if (nsp > initrd_high) /* limit as specified */
566 nsp = initrd_high;
567 nsp -= len;
568 nsp &= ~(4096 - 1); /* align on page */
569 if (nsp >= sp)
570 initrd_start = nsp;
571 }
572
573 SHOW_BOOT_PROGRESS (12);
wdenk56f94be2002-11-05 16:35:14 +0000574
575 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
wdenk47d1a6e2002-11-03 00:01:44 +0000576 data, data + len - 1, len, len);
wdenk56f94be2002-11-05 16:35:14 +0000577
wdenk47d1a6e2002-11-03 00:01:44 +0000578 initrd_end = initrd_start + len;
579 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
580 initrd_start, initrd_end);
581#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
582 {
583 size_t l = len;
584 void *to = (void *)initrd_start;
585 void *from = (void *)data;
586
587 while (l > 0) {
588 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
589 WATCHDOG_RESET();
590 memmove (to, from, tail);
591 to += tail;
592 from += tail;
593 l -= tail;
594 }
595 }
596#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
597 memmove ((void *)initrd_start, (void *)data, len);
598#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
599 printf ("OK\n");
600 } else {
601 initrd_start = 0;
602 initrd_end = 0;
603 }
604
wdenk56f94be2002-11-05 16:35:14 +0000605
606 debug ("## Transferring control to Linux (at address %08lx) ...\n",
wdenk47d1a6e2002-11-03 00:01:44 +0000607 (ulong)kernel);
wdenk56f94be2002-11-05 16:35:14 +0000608
wdenk47d1a6e2002-11-03 00:01:44 +0000609 SHOW_BOOT_PROGRESS (15);
610
611#ifdef CFG_INIT_RAM_LOCK
612 unlock_ram_in_cache();
613#endif
614 /*
615 * Linux Kernel Parameters:
616 * r3: ptr to board info data
617 * r4: initrd_start or 0 if no initrd
618 * r5: initrd_end - unused if r4 is 0
619 * r6: Start of command line string
620 * r7: End of command line string
621 */
622 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
623}
624#endif /* CONFIG_ARM */
625
626static void
627do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
628 int argc, char *argv[],
629 ulong addr,
630 ulong *len_ptr,
631 int verify)
632{
633 DECLARE_GLOBAL_DATA_PTR;
634
635 image_header_t *hdr = &header;
636
637 void (*loader)(bd_t *, image_header_t *, char *, char *);
638 image_header_t *img_addr;
639 char *consdev;
640 char *cmdline;
641
642
643 /*
644 * Booting a (NetBSD) kernel image
645 *
646 * This process is pretty similar to a standalone application:
647 * The (first part of an multi-) image must be a stage-2 loader,
648 * which in turn is responsible for loading & invoking the actual
649 * kernel. The only differences are the parameters being passed:
650 * besides the board info strucure, the loader expects a command
651 * line, the name of the console device, and (optionally) the
652 * address of the original image header.
653 */
654
655 img_addr = 0;
656 if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
657 img_addr = (image_header_t *) addr;
658
659
660 consdev = "";
661#if defined (CONFIG_8xx_CONS_SMC1)
662 consdev = "smc1";
663#elif defined (CONFIG_8xx_CONS_SMC2)
664 consdev = "smc2";
665#elif defined (CONFIG_8xx_CONS_SCC2)
666 consdev = "scc2";
667#elif defined (CONFIG_8xx_CONS_SCC3)
668 consdev = "scc3";
669#endif
670
671 if (argc > 2) {
672 ulong len;
673 int i;
674
675 for (i=2, len=0 ; i<argc ; i+=1)
676 len += strlen (argv[i]) + 1;
677 cmdline = malloc (len);
678
679 for (i=2, len=0 ; i<argc ; i+=1) {
680 if (i > 2)
681 cmdline[len++] = ' ';
682 strcpy (&cmdline[len], argv[i]);
683 len += strlen (argv[i]);
684 }
685 } else if ((cmdline = getenv("bootargs")) == NULL) {
686 cmdline = "";
687 }
688
689 loader = (void (*)(bd_t *, image_header_t *, char *, char *)) hdr->ih_ep;
690
691 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
692 (ulong)loader);
693
694 SHOW_BOOT_PROGRESS (15);
695
696 /*
697 * NetBSD Stage-2 Loader Parameters:
698 * r3: ptr to board info data
699 * r4: image address
700 * r5: console device
701 * r6: boot args string
702 */
703 (*loader) (gd->bd, img_addr, consdev, cmdline);
704}
705
706#if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
707int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
708{
709 int rcode = 0;
710#ifndef CFG_HUSH_PARSER
711 if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
712#else
713 if (parse_string_outer(getenv("bootcmd"),
714 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
715#endif
716 return rcode;
717}
718#endif
719
720#if (CONFIG_COMMANDS & CFG_CMD_IMI)
721int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
722{
723 int arg;
724 ulong addr;
725 int rcode=0;
726
727 if (argc < 2) {
728 return image_info (load_addr);
729 }
730
731 for (arg=1; arg <argc; ++arg) {
732 addr = simple_strtoul(argv[arg], NULL, 16);
733 if (image_info (addr) != 0) rcode = 1;
734 }
735 return rcode;
736}
737
738static int image_info (ulong addr)
739{
740 ulong data, len, checksum;
741 image_header_t *hdr = &header;
742
743 printf ("\n## Checking Image at %08lx ...\n", addr);
744
745 /* Copy header so we can blank CRC field for re-calculation */
746 memmove (&header, (char *)addr, sizeof(image_header_t));
747
748 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
749 printf (" Bad Magic Number\n");
750 return 1;
751 }
752
753 data = (ulong)&header;
754 len = sizeof(image_header_t);
755
756 checksum = ntohl(hdr->ih_hcrc);
757 hdr->ih_hcrc = 0;
758
759 if (crc32 (0, (char *)data, len) != checksum) {
760 printf (" Bad Header Checksum\n");
761 return 1;
762 }
763
764 /* for multi-file images we need the data part, too */
765 print_image_hdr ((image_header_t *)addr);
766
767 data = addr + sizeof(image_header_t);
768 len = ntohl(hdr->ih_size);
769
770 printf (" Verifying Checksum ... ");
771 if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
772 printf (" Bad Data CRC\n");
773 return 1;
774 }
775 printf ("OK\n");
776 return 0;
777}
778#endif /* CFG_CMD_IMI */
779
780void
781print_image_hdr (image_header_t *hdr)
782{
783#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
784 time_t timestamp = (time_t)ntohl(hdr->ih_time);
785 struct rtc_time tm;
786#endif
787
788 printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
789#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
790 to_tm (timestamp, &tm);
791 printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
792 tm.tm_year, tm.tm_mon, tm.tm_mday,
793 tm.tm_hour, tm.tm_min, tm.tm_sec);
794#endif /* CFG_CMD_DATE, CONFIG_TIMESTAMP */
795 printf (" Image Type: "); print_type(hdr); printf ("\n");
796 printf (" Data Size: %d Bytes = ", ntohl(hdr->ih_size));
797 print_size (ntohl(hdr->ih_size), "\n");
798 printf (" Load Address: %08x\n", ntohl(hdr->ih_load));
799 printf (" Entry Point: %08x\n", ntohl(hdr->ih_ep));
800
801 if (hdr->ih_type == IH_TYPE_MULTI) {
802 int i;
803 ulong len;
804 ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
805
806 printf (" Contents:\n");
807 for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
808 printf (" Image %d: %8ld Bytes = ", i, len);
809 print_size (len, "\n");
810 }
811 }
812}
813
814
815static void
816print_type (image_header_t *hdr)
817{
818 char *os, *arch, *type, *comp;
819
820 switch (hdr->ih_os) {
821 case IH_OS_INVALID: os = "Invalid OS"; break;
822 case IH_OS_NETBSD: os = "NetBSD"; break;
823 case IH_OS_LINUX: os = "Linux"; break;
824 case IH_OS_VXWORKS: os = "VxWorks"; break;
825 case IH_OS_QNX: os = "QNX"; break;
826 case IH_OS_U_BOOT: os = "U-Boot"; break;
827 default: os = "Unknown OS"; break;
828 }
829
830 switch (hdr->ih_arch) {
831 case IH_CPU_INVALID: arch = "Invalid CPU"; break;
832 case IH_CPU_ALPHA: arch = "Alpha"; break;
833 case IH_CPU_ARM: arch = "ARM"; break;
834 case IH_CPU_I386: arch = "Intel x86"; break;
835 case IH_CPU_IA64: arch = "IA64"; break;
836 case IH_CPU_MIPS: arch = "MIPS"; break;
837 case IH_CPU_MIPS64: arch = "MIPS 64 Bit"; break;
838 case IH_CPU_PPC: arch = "PowerPC"; break;
839 case IH_CPU_S390: arch = "IBM S390"; break;
840 case IH_CPU_SH: arch = "SuperH"; break;
841 case IH_CPU_SPARC: arch = "SPARC"; break;
842 case IH_CPU_SPARC64: arch = "SPARC 64 Bit"; break;
843 default: arch = "Unknown Architecture"; break;
844 }
845
846 switch (hdr->ih_type) {
847 case IH_TYPE_INVALID: type = "Invalid Image"; break;
848 case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
849 case IH_TYPE_KERNEL: type = "Kernel Image"; break;
850 case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
851 case IH_TYPE_MULTI: type = "Multi-File Image"; break;
852 case IH_TYPE_FIRMWARE: type = "Firmware"; break;
853 case IH_TYPE_SCRIPT: type = "Script"; break;
854 default: type = "Unknown Image"; break;
855 }
856
857 switch (hdr->ih_comp) {
858 case IH_COMP_NONE: comp = "uncompressed"; break;
859 case IH_COMP_GZIP: comp = "gzip compressed"; break;
860 case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
861 default: comp = "unknown compression"; break;
862 }
863
864 printf ("%s %s %s (%s)", arch, os, type, comp);
865}
866
867#define ZALLOC_ALIGNMENT 16
868
869static void *zalloc(void *x, unsigned items, unsigned size)
870{
871 void *p;
872
873 size *= items;
874 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
875
876 p = malloc (size);
877
878 return (p);
879}
880
881static void zfree(void *x, void *addr, unsigned nb)
882{
883 free (addr);
884}
885
886#define HEAD_CRC 2
887#define EXTRA_FIELD 4
888#define ORIG_NAME 8
889#define COMMENT 0x10
890#define RESERVED 0xe0
891
892#define DEFLATED 8
893
894int gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
895{
896 z_stream s;
897 int r, i, flags;
898
899 /* skip header */
900 i = 10;
901 flags = src[3];
902 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
903 printf ("Error: Bad gzipped data\n");
904 return (-1);
905 }
906 if ((flags & EXTRA_FIELD) != 0)
907 i = 12 + src[10] + (src[11] << 8);
908 if ((flags & ORIG_NAME) != 0)
909 while (src[i++] != 0)
910 ;
911 if ((flags & COMMENT) != 0)
912 while (src[i++] != 0)
913 ;
914 if ((flags & HEAD_CRC) != 0)
915 i += 2;
916 if (i >= *lenp) {
917 printf ("Error: gunzip out of data in header\n");
918 return (-1);
919 }
920
921 s.zalloc = zalloc;
922 s.zfree = zfree;
923#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
924 s.outcb = (cb_func)WATCHDOG_RESET;
925#else
926 s.outcb = Z_NULL;
927#endif /* CONFIG_HW_WATCHDOG */
928
929 r = inflateInit2(&s, -MAX_WBITS);
930 if (r != Z_OK) {
931 printf ("Error: inflateInit2() returned %d\n", r);
932 return (-1);
933 }
934 s.next_in = src + i;
935 s.avail_in = *lenp - i;
936 s.next_out = dst;
937 s.avail_out = dstlen;
938 r = inflate(&s, Z_FINISH);
939 if (r != Z_OK && r != Z_STREAM_END) {
940 printf ("Error: inflate() returned %d\n", r);
941 return (-1);
942 }
943 *lenp = s.next_out - (unsigned char *) dst;
944 inflateEnd(&s);
945
946 return (0);
947}
948
949#if (CONFIG_COMMANDS & CFG_CMD_ELF)
950static void
951do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
952 ulong addr, ulong *len_ptr, int verify)
953{
954 image_header_t *hdr = &header;
955 char str[80];
956
957 sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
958 setenv("loadaddr", str);
959 do_bootvx(cmdtp, 0, 0, NULL);
960}
961
962static void
963do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
964 ulong addr, ulong *len_ptr, int verify)
965{
966 image_header_t *hdr = &header;
967 char *local_args[2];
968 char str[16];
969
970 sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
971 local_args[0] = argv[0];
972 local_args[1] = str; /* and provide it via the arguments */
973 do_bootelf(cmdtp, 0, 2, local_args);
974}
975#endif /* CFG_CMD_ELF */