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