blob: 34a74ce2c5a6fd0fcdfdb3acfc107b74f5ffdb08 [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>
wdenk47d1a6e2002-11-03 00:01:44 +000030#include <image.h>
31#include <malloc.h>
32#include <zlib.h>
wdenkc29fdfc2003-08-29 20:57:53 +000033#include <bzlib.h>
wdenk7f70e852003-05-20 14:25:27 +000034#include <environment.h>
wdenk47d1a6e2002-11-03 00:01:44 +000035#include <asm/byteorder.h>
wdenk8bde7f72003-06-27 21:31:46 +000036
37 /*cmd_boot.c*/
38 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
39
wdenk47d1a6e2002-11-03 00:01:44 +000040#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
41#include <rtc.h>
42#endif
43
44#ifdef CFG_HUSH_PARSER
45#include <hush.h>
46#endif
47
48#ifdef CONFIG_SHOW_BOOT_PROGRESS
49# include <status_led.h>
50# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
51#else
52# define SHOW_BOOT_PROGRESS(arg)
53#endif
54
55#ifdef CFG_INIT_RAM_LOCK
56#include <asm/cache.h>
57#endif
58
wdenk228f29a2002-12-08 09:53:23 +000059#ifdef CONFIG_LOGBUFFER
60#include <logbuff.h>
61#endif
62
wdenk2abbe072003-06-16 23:50:08 +000063#ifdef CONFIG_HAS_DATAFLASH
64#include <dataflash.h>
65#endif
66
wdenk47d1a6e2002-11-03 00:01:44 +000067/*
68 * Some systems (for example LWMON) have very short watchdog periods;
69 * we must make sure to split long operations like memmove() or
70 * crc32() into reasonable chunks.
71 */
72#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
73# define CHUNKSZ (64 * 1024)
74#endif
75
76int gunzip (void *, int, unsigned char *, int *);
77
78static void *zalloc(void *, unsigned, unsigned);
79static void zfree(void *, void *, unsigned);
80
81#if (CONFIG_COMMANDS & CFG_CMD_IMI)
82static int image_info (unsigned long addr);
83#endif
wdenk27b207f2003-07-24 23:38:38 +000084
85#if (CONFIG_COMMANDS & CFG_CMD_IMLS)
86#include <flash.h>
87extern flash_info_t flash_info[CFG_MAX_FLASH_BANKS]; /* info for FLASH chips */
88static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
89#endif
90
wdenk47d1a6e2002-11-03 00:01:44 +000091static void print_type (image_header_t *hdr);
92
wdenk2262cfe2002-11-18 00:14:45 +000093#ifdef __I386__
94image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
95#endif
96
wdenk47d1a6e2002-11-03 00:01:44 +000097/*
98 * Continue booting an OS image; caller already has:
99 * - copied image header to global variable `header'
100 * - checked header magic number, checksums (both header & image),
101 * - verified image architecture (PPC) and type (KERNEL or MULTI),
102 * - loaded (first part of) image to header load address,
103 * - disabled interrupts.
104 */
105typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
106 int argc, char *argv[],
107 ulong addr, /* of image to boot */
108 ulong *len_ptr, /* multi-file image length table */
109 int verify); /* getenv("verify")[0] != 'n' */
110
wdenk8bde7f72003-06-27 21:31:46 +0000111#ifdef DEBUG
112extern int do_bdinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
113#endif
114
wdenk2262cfe2002-11-18 00:14:45 +0000115#ifdef CONFIG_PPC
wdenk47d1a6e2002-11-03 00:01:44 +0000116static boot_os_Fcn do_bootm_linux;
117#else
118extern boot_os_Fcn do_bootm_linux;
119#endif
wdenkf72da342003-10-10 10:05:42 +0000120#ifdef CONFIG_SILENT_CONSOLE
121static void fixup_silent_linux (void);
122#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000123static boot_os_Fcn do_bootm_netbsd;
wdenkd791b1d2003-04-20 14:04:18 +0000124static boot_os_Fcn do_bootm_rtems;
wdenk47d1a6e2002-11-03 00:01:44 +0000125#if (CONFIG_COMMANDS & CFG_CMD_ELF)
126static boot_os_Fcn do_bootm_vxworks;
127static boot_os_Fcn do_bootm_qnxelf;
128int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
129int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
130#endif /* CFG_CMD_ELF */
wdenk7f70e852003-05-20 14:25:27 +0000131#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
132static boot_os_Fcn do_bootm_artos;
133#endif
wdenk1f4bb372003-07-27 00:21:01 +0000134#ifdef CONFIG_LYNXKDI
135static boot_os_Fcn do_bootm_lynxkdi;
136extern void lynxkdi_boot( image_header_t * );
137#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000138
139image_header_t header;
140
141ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
142
143int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
144{
145 ulong iflag;
146 ulong addr;
147 ulong data, len, checksum;
148 ulong *len_ptr;
wdenkc29fdfc2003-08-29 20:57:53 +0000149 uint unc_len = 0x400000;
wdenk47d1a6e2002-11-03 00:01:44 +0000150 int i, verify;
151 char *name, *s;
wdenkb13fb012003-10-30 21:49:38 +0000152 int (*appl)(int, char *[]);
wdenk47d1a6e2002-11-03 00:01:44 +0000153 image_header_t *hdr = &header;
154
155 s = getenv ("verify");
156 verify = (s && (*s == 'n')) ? 0 : 1;
157
158 if (argc < 2) {
159 addr = load_addr;
160 } else {
161 addr = simple_strtoul(argv[1], NULL, 16);
162 }
163
164 SHOW_BOOT_PROGRESS (1);
165 printf ("## Booting image at %08lx ...\n", addr);
166
167 /* Copy header so we can blank CRC field for re-calculation */
wdenk2abbe072003-06-16 23:50:08 +0000168#ifdef CONFIG_HAS_DATAFLASH
169 if (addr_dataflash(addr)){
170 read_dataflash(addr, sizeof(image_header_t), (char *)&header);
171 } else
172#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000173 memmove (&header, (char *)addr, sizeof(image_header_t));
174
175 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
wdenk2262cfe2002-11-18 00:14:45 +0000176#ifdef __I386__ /* correct image format not implemented yet - fake it */
177 if (fake_header(hdr, (void*)addr, -1) != NULL) {
178 /* to compensate for the addition below */
179 addr -= sizeof(image_header_t);
180 /* turnof verify,
181 * fake_header() does not fake the data crc
182 */
183 verify = 0;
184 } else
185#endif /* __I386__ */
186 {
wdenk47d1a6e2002-11-03 00:01:44 +0000187 printf ("Bad Magic Number\n");
188 SHOW_BOOT_PROGRESS (-1);
189 return 1;
wdenk2262cfe2002-11-18 00:14:45 +0000190 }
wdenk47d1a6e2002-11-03 00:01:44 +0000191 }
192 SHOW_BOOT_PROGRESS (2);
193
194 data = (ulong)&header;
195 len = sizeof(image_header_t);
196
197 checksum = ntohl(hdr->ih_hcrc);
198 hdr->ih_hcrc = 0;
199
200 if (crc32 (0, (char *)data, len) != checksum) {
201 printf ("Bad Header Checksum\n");
202 SHOW_BOOT_PROGRESS (-2);
203 return 1;
204 }
205 SHOW_BOOT_PROGRESS (3);
206
207 /* for multi-file images we need the data part, too */
wdenka57a4962003-10-26 22:52:58 +0000208 print_image_hdr ((image_header_t *)addr);
wdenk47d1a6e2002-11-03 00:01:44 +0000209
210 data = addr + sizeof(image_header_t);
211 len = ntohl(hdr->ih_size);
212
wdenk2abbe072003-06-16 23:50:08 +0000213#ifdef CONFIG_HAS_DATAFLASH
214 if (addr_dataflash(addr)){
215 read_dataflash(data, len, (char *)CFG_LOAD_ADDR);
216 data = CFG_LOAD_ADDR;
217 }
wdenk8bde7f72003-06-27 21:31:46 +0000218#endif
wdenk2abbe072003-06-16 23:50:08 +0000219
wdenk47d1a6e2002-11-03 00:01:44 +0000220 if (verify) {
221 printf (" Verifying Checksum ... ");
222 if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
223 printf ("Bad Data CRC\n");
224 SHOW_BOOT_PROGRESS (-3);
225 return 1;
226 }
227 printf ("OK\n");
228 }
229 SHOW_BOOT_PROGRESS (4);
230
231 len_ptr = (ulong *)data;
232
wdenk2262cfe2002-11-18 00:14:45 +0000233#if defined(__PPC__)
234 if (hdr->ih_arch != IH_CPU_PPC)
235#elif defined(__ARM__)
236 if (hdr->ih_arch != IH_CPU_ARM)
237#elif defined(__I386__)
238 if (hdr->ih_arch != IH_CPU_I386)
wdenk6069ff22003-02-28 00:49:47 +0000239#elif defined(__mips__)
wdenk8bde7f72003-06-27 21:31:46 +0000240 if (hdr->ih_arch != IH_CPU_MIPS)
wdenk4a551702003-10-08 23:26:14 +0000241#elif defined(__nios__)
242 if (hdr->ih_arch != IH_CPU_NIOS)
wdenk4e5ca3e2003-12-08 01:34:36 +0000243#elif defined(__M68K__)
244 if (hdr->ih_arch != IH_CPU_M68K)
wdenk2262cfe2002-11-18 00:14:45 +0000245#else
246# error Unknown CPU type
247#endif
248 {
249 printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch);
wdenk47d1a6e2002-11-03 00:01:44 +0000250 SHOW_BOOT_PROGRESS (-4);
251 return 1;
252 }
253 SHOW_BOOT_PROGRESS (5);
254
255 switch (hdr->ih_type) {
wdenkb13fb012003-10-30 21:49:38 +0000256 case IH_TYPE_STANDALONE:
257 name = "Standalone Application";
258 /* A second argument overwrites the load address */
259 if (argc > 2) {
260 hdr->ih_load = simple_strtoul(argv[2], NULL, 16);
261 }
262 break;
263 case IH_TYPE_KERNEL:
264 name = "Kernel Image";
265 break;
wdenkd4ca31c2004-01-02 14:00:00 +0000266 case IH_TYPE_MULTI:
wdenkb13fb012003-10-30 21:49:38 +0000267 name = "Multi-File Image";
268 len = ntohl(len_ptr[0]);
269 /* OS kernel is always the first image */
270 data += 8; /* kernel_len + terminator */
271 for (i=1; len_ptr[i]; ++i)
272 data += 4;
273 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000274 default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
275 SHOW_BOOT_PROGRESS (-5);
276 return 1;
277 }
278 SHOW_BOOT_PROGRESS (6);
279
280 /*
281 * We have reached the point of no return: we are going to
282 * overwrite all exception vector code, so we cannot easily
283 * recover from any failures any more...
284 */
285
286 iflag = disable_interrupts();
287
wdenkc7de8292002-11-19 11:04:11 +0000288#ifdef CONFIG_AMIGAONEG3SE
289 /*
wdenk8bde7f72003-06-27 21:31:46 +0000290 * We've possible left the caches enabled during
wdenkc7de8292002-11-19 11:04:11 +0000291 * bios emulation, so turn them off again
292 */
293 icache_disable();
294 invalidate_l1_instruction_cache();
295 flush_data_cache();
296 dcache_disable();
297#endif
298
wdenk47d1a6e2002-11-03 00:01:44 +0000299 switch (hdr->ih_comp) {
300 case IH_COMP_NONE:
wdenk2262cfe2002-11-18 00:14:45 +0000301 if(ntohl(hdr->ih_load) == addr) {
wdenk47d1a6e2002-11-03 00:01:44 +0000302 printf (" XIP %s ... ", name);
303 } else {
304#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
305 size_t l = len;
306 void *to = (void *)ntohl(hdr->ih_load);
307 void *from = (void *)data;
308
309 printf (" Loading %s ... ", name);
310
311 while (l > 0) {
312 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
313 WATCHDOG_RESET();
314 memmove (to, from, tail);
315 to += tail;
316 from += tail;
317 l -= tail;
318 }
319#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
320 memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
321#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
322 }
323 break;
324 case IH_COMP_GZIP:
325 printf (" Uncompressing %s ... ", name);
wdenkc29fdfc2003-08-29 20:57:53 +0000326 if (gunzip ((void *)ntohl(hdr->ih_load), unc_len,
wdenk47d1a6e2002-11-03 00:01:44 +0000327 (uchar *)data, (int *)&len) != 0) {
328 printf ("GUNZIP ERROR - must RESET board to recover\n");
329 SHOW_BOOT_PROGRESS (-6);
330 do_reset (cmdtp, flag, argc, argv);
331 }
332 break;
wdenkc29fdfc2003-08-29 20:57:53 +0000333#ifdef CONFIG_BZIP2
334 case IH_COMP_BZIP2:
335 printf (" Uncompressing %s ... ", name);
336 i = BZ2_bzBuffToBuffDecompress ((char*)ntohl(hdr->ih_load),
337 &unc_len, (char *)data, len, 0, 0);
338 if (i != BZ_OK) {
339 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
340 SHOW_BOOT_PROGRESS (-6);
341 udelay(100000);
342 do_reset (cmdtp, flag, argc, argv);
343 }
344 break;
345#endif /* CONFIG_BZIP2 */
wdenk47d1a6e2002-11-03 00:01:44 +0000346 default:
347 if (iflag)
348 enable_interrupts();
349 printf ("Unimplemented compression type %d\n", hdr->ih_comp);
350 SHOW_BOOT_PROGRESS (-7);
351 return 1;
352 }
353 printf ("OK\n");
354 SHOW_BOOT_PROGRESS (7);
355
356 switch (hdr->ih_type) {
357 case IH_TYPE_STANDALONE:
wdenk47d1a6e2002-11-03 00:01:44 +0000358 if (iflag)
359 enable_interrupts();
360
wdenk4a6fd342003-04-12 23:38:12 +0000361 /* load (and uncompress), but don't start if "autostart"
362 * is set to "no"
363 */
wdenk4a551702003-10-08 23:26:14 +0000364 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
365 char buf[32];
366 sprintf(buf, "%lX", len);
367 setenv("filesize", buf);
wdenk4a6fd342003-04-12 23:38:12 +0000368 return 0;
wdenk4a551702003-10-08 23:26:14 +0000369 }
wdenkb13fb012003-10-30 21:49:38 +0000370 appl = (int (*)(int, char *[]))ntohl(hdr->ih_ep);
371 (*appl)(argc-1, &argv[1]);
wdenk4a6fd342003-04-12 23:38:12 +0000372 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000373 case IH_TYPE_KERNEL:
374 case IH_TYPE_MULTI:
375 /* handled below */
376 break;
377 default:
378 if (iflag)
379 enable_interrupts();
380 printf ("Can't boot image type %d\n", hdr->ih_type);
381 SHOW_BOOT_PROGRESS (-8);
382 return 1;
383 }
384 SHOW_BOOT_PROGRESS (8);
385
386 switch (hdr->ih_os) {
387 default: /* handled by (original) Linux case */
388 case IH_OS_LINUX:
wdenkf72da342003-10-10 10:05:42 +0000389#ifdef CONFIG_SILENT_CONSOLE
390 fixup_silent_linux();
391#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000392 do_bootm_linux (cmdtp, flag, argc, argv,
393 addr, len_ptr, verify);
394 break;
395 case IH_OS_NETBSD:
396 do_bootm_netbsd (cmdtp, flag, argc, argv,
397 addr, len_ptr, verify);
398 break;
wdenk8bde7f72003-06-27 21:31:46 +0000399
wdenk1f4bb372003-07-27 00:21:01 +0000400#ifdef CONFIG_LYNXKDI
401 case IH_OS_LYNXOS:
402 do_bootm_lynxkdi (cmdtp, flag, argc, argv,
403 addr, len_ptr, verify);
404 break;
405#endif
406
wdenkd791b1d2003-04-20 14:04:18 +0000407 case IH_OS_RTEMS:
408 do_bootm_rtems (cmdtp, flag, argc, argv,
409 addr, len_ptr, verify);
410 break;
wdenk8bde7f72003-06-27 21:31:46 +0000411
wdenk47d1a6e2002-11-03 00:01:44 +0000412#if (CONFIG_COMMANDS & CFG_CMD_ELF)
413 case IH_OS_VXWORKS:
414 do_bootm_vxworks (cmdtp, flag, argc, argv,
415 addr, len_ptr, verify);
416 break;
417 case IH_OS_QNX:
418 do_bootm_qnxelf (cmdtp, flag, argc, argv,
419 addr, len_ptr, verify);
420 break;
421#endif /* CFG_CMD_ELF */
wdenk7f70e852003-05-20 14:25:27 +0000422#ifdef CONFIG_ARTOS
423 case IH_OS_ARTOS:
424 do_bootm_artos (cmdtp, flag, argc, argv,
425 addr, len_ptr, verify);
426 break;
427#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000428 }
429
430 SHOW_BOOT_PROGRESS (-9);
431#ifdef DEBUG
432 printf ("\n## Control returned to monitor - resetting...\n");
433 do_reset (cmdtp, flag, argc, argv);
434#endif
435 return 1;
436}
437
wdenk0d498392003-07-01 21:06:45 +0000438U_BOOT_CMD(
439 bootm, CFG_MAXARGS, 1, do_bootm,
wdenk8bde7f72003-06-27 21:31:46 +0000440 "bootm - boot application image from memory\n",
441 "[addr [arg ...]]\n - boot application image stored in memory\n"
442 " passing arguments 'arg ...'; when booting a Linux kernel,\n"
443 " 'arg' can be the address of an initrd image\n"
444);
445
wdenkf72da342003-10-10 10:05:42 +0000446#ifdef CONFIG_SILENT_CONSOLE
447static void
448fixup_silent_linux ()
449{
450 DECLARE_GLOBAL_DATA_PTR;
451 char buf[256], *start, *end;
452 char *cmdline = getenv ("bootargs");
453
454 /* Only fix cmdline when requested */
455 if (!(gd->flags & GD_FLG_SILENT))
456 return;
457
458 debug ("before silent fix-up: %s\n", cmdline);
459 if (cmdline) {
460 if ((start = strstr (cmdline, "console=")) != NULL) {
461 end = strchr (start, ' ');
462 strncpy (buf, cmdline, (start - cmdline + 8));
463 if (end)
464 strcpy (buf + (start - cmdline + 8), end);
465 else
466 buf[start - cmdline + 8] = '\0';
467 } else {
468 strcpy (buf, cmdline);
469 strcat (buf, " console=");
470 }
471 } else {
472 strcpy (buf, "console=");
473 }
474
475 setenv ("bootargs", buf);
476 debug ("after silent fix-up: %s\n", buf);
477}
478#endif /* CONFIG_SILENT_CONSOLE */
479
wdenk2262cfe2002-11-18 00:14:45 +0000480#ifdef CONFIG_PPC
wdenk47d1a6e2002-11-03 00:01:44 +0000481static void
482do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
483 int argc, char *argv[],
484 ulong addr,
485 ulong *len_ptr,
486 int verify)
487{
488 DECLARE_GLOBAL_DATA_PTR;
489
490 ulong sp;
491 ulong len, checksum;
492 ulong initrd_start, initrd_end;
493 ulong cmd_start, cmd_end;
494 ulong initrd_high;
495 ulong data;
wdenk38b99262003-05-23 23:18:21 +0000496 int initrd_copy_to_ram = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000497 char *cmdline;
498 char *s;
499 bd_t *kbd;
500 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
501 image_header_t *hdr = &header;
502
503 if ((s = getenv ("initrd_high")) != NULL) {
504 /* a value of "no" or a similar string will act like 0,
505 * turning the "load high" feature off. This is intentional.
506 */
507 initrd_high = simple_strtoul(s, NULL, 16);
wdenk38b99262003-05-23 23:18:21 +0000508 if (initrd_high == ~0)
509 initrd_copy_to_ram = 0;
wdenk228f29a2002-12-08 09:53:23 +0000510 } else { /* not set, no restrictions to load high */
wdenk47d1a6e2002-11-03 00:01:44 +0000511 initrd_high = ~0;
512 }
513
wdenk56f94be2002-11-05 16:35:14 +0000514#ifdef CONFIG_LOGBUFFER
wdenk6aff3112002-12-17 01:51:00 +0000515 kbd=gd->bd;
wdenk228f29a2002-12-08 09:53:23 +0000516 /* Prevent initrd from overwriting logbuffer */
517 if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
518 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
519 debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
wdenk56f94be2002-11-05 16:35:14 +0000520#endif
521
wdenk47d1a6e2002-11-03 00:01:44 +0000522 /*
523 * Booting a (Linux) kernel image
524 *
525 * Allocate space for command line and board info - the
526 * address should be as high as possible within the reach of
527 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
528 * memory, which means far enough below the current stack
529 * pointer.
530 */
531
532 asm( "mr %0,1": "=r"(sp) : );
533
wdenk56f94be2002-11-05 16:35:14 +0000534 debug ("## Current stack ends at 0x%08lX ", sp);
535
wdenk47d1a6e2002-11-03 00:01:44 +0000536 sp -= 2048; /* just to be sure */
537 if (sp > CFG_BOOTMAPSZ)
538 sp = CFG_BOOTMAPSZ;
539 sp &= ~0xF;
540
wdenk56f94be2002-11-05 16:35:14 +0000541 debug ("=> set upper limit to 0x%08lX\n", sp);
542
wdenk47d1a6e2002-11-03 00:01:44 +0000543 cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
544 kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
545
546 if ((s = getenv("bootargs")) == NULL)
547 s = "";
548
549 strcpy (cmdline, s);
550
551 cmd_start = (ulong)&cmdline[0];
552 cmd_end = cmd_start + strlen(cmdline);
553
554 *kbd = *(gd->bd);
555
556#ifdef DEBUG
557 printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
558
559 do_bdinfo (NULL, 0, 0, NULL);
560#endif
561
562 if ((s = getenv ("clocks_in_mhz")) != NULL) {
563 /* convert all clock information to MHz */
564 kbd->bi_intfreq /= 1000000L;
565 kbd->bi_busfreq /= 1000000L;
wdenk42d1f032003-10-15 23:53:47 +0000566#if defined(CONFIG_8260) || defined(CONFIG_MPC8560)
wdenk47d1a6e2002-11-03 00:01:44 +0000567 kbd->bi_cpmfreq /= 1000000L;
568 kbd->bi_brgfreq /= 1000000L;
569 kbd->bi_sccfreq /= 1000000L;
570 kbd->bi_vco /= 1000000L;
571#endif /* CONFIG_8260 */
wdenkb96619a2003-12-05 21:08:38 +0000572#if defined(CONFIG_MPC5XXX)
wdenk945af8d2003-07-16 21:53:01 +0000573 kbd->bi_ipbfreq /= 1000000L;
574 kbd->bi_pcifreq /= 1000000L;
wdenkb96619a2003-12-05 21:08:38 +0000575#endif /* CONFIG_MPC5XXX */
wdenk47d1a6e2002-11-03 00:01:44 +0000576 }
577
578 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))hdr->ih_ep;
579
580 /*
581 * Check if there is an initrd image
582 */
583 if (argc >= 3) {
584 SHOW_BOOT_PROGRESS (9);
585
586 addr = simple_strtoul(argv[2], NULL, 16);
587
588 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
589
590 /* Copy header so we can blank CRC field for re-calculation */
591 memmove (&header, (char *)addr, sizeof(image_header_t));
592
593 if (hdr->ih_magic != IH_MAGIC) {
594 printf ("Bad Magic Number\n");
595 SHOW_BOOT_PROGRESS (-10);
596 do_reset (cmdtp, flag, argc, argv);
597 }
598
599 data = (ulong)&header;
600 len = sizeof(image_header_t);
601
602 checksum = hdr->ih_hcrc;
603 hdr->ih_hcrc = 0;
604
605 if (crc32 (0, (char *)data, len) != checksum) {
606 printf ("Bad Header Checksum\n");
607 SHOW_BOOT_PROGRESS (-11);
608 do_reset (cmdtp, flag, argc, argv);
609 }
610
611 SHOW_BOOT_PROGRESS (10);
612
613 print_image_hdr (hdr);
614
615 data = addr + sizeof(image_header_t);
616 len = hdr->ih_size;
617
618 if (verify) {
619 ulong csum = 0;
620#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
621 ulong cdata = data, edata = cdata + len;
622#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
623
624 printf (" Verifying Checksum ... ");
625
626#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
627
628 while (cdata < edata) {
629 ulong chunk = edata - cdata;
630
631 if (chunk > CHUNKSZ)
632 chunk = CHUNKSZ;
633 csum = crc32 (csum, (char *)cdata, chunk);
634 cdata += chunk;
635
636 WATCHDOG_RESET();
637 }
638#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
639 csum = crc32 (0, (char *)data, len);
640#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
641
642 if (csum != hdr->ih_dcrc) {
643 printf ("Bad Data CRC\n");
644 SHOW_BOOT_PROGRESS (-12);
645 do_reset (cmdtp, flag, argc, argv);
646 }
647 printf ("OK\n");
648 }
649
650 SHOW_BOOT_PROGRESS (11);
651
652 if ((hdr->ih_os != IH_OS_LINUX) ||
653 (hdr->ih_arch != IH_CPU_PPC) ||
654 (hdr->ih_type != IH_TYPE_RAMDISK) ) {
655 printf ("No Linux PPC Ramdisk Image\n");
656 SHOW_BOOT_PROGRESS (-13);
657 do_reset (cmdtp, flag, argc, argv);
658 }
659
660 /*
661 * Now check if we have a multifile image
662 */
663 } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
664 u_long tail = ntohl(len_ptr[0]) % 4;
665 int i;
666
667 SHOW_BOOT_PROGRESS (13);
668
669 /* skip kernel length and terminator */
670 data = (ulong)(&len_ptr[2]);
671 /* skip any additional image length fields */
672 for (i=1; len_ptr[i]; ++i)
673 data += 4;
674 /* add kernel length, and align */
675 data += ntohl(len_ptr[0]);
676 if (tail) {
677 data += 4 - tail;
678 }
679
680 len = ntohl(len_ptr[1]);
681
682 } else {
683 /*
684 * no initrd image
685 */
686 SHOW_BOOT_PROGRESS (14);
687
688 len = data = 0;
689 }
690
wdenk47d1a6e2002-11-03 00:01:44 +0000691 if (!data) {
wdenk56f94be2002-11-05 16:35:14 +0000692 debug ("No initrd\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000693 }
wdenk47d1a6e2002-11-03 00:01:44 +0000694
695 if (data) {
wdenk38b99262003-05-23 23:18:21 +0000696 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
697 initrd_start = data;
698 initrd_end = initrd_start + len;
699 } else {
wdenk47d1a6e2002-11-03 00:01:44 +0000700 initrd_start = (ulong)kbd - len;
701 initrd_start &= ~(4096 - 1); /* align on page */
702
703 if (initrd_high) {
704 ulong nsp;
705
706 /*
707 * the inital ramdisk does not need to be within
708 * CFG_BOOTMAPSZ as it is not accessed until after
709 * the mm system is initialised.
710 *
711 * do the stack bottom calculation again and see if
712 * the initrd will fit just below the monitor stack
713 * bottom without overwriting the area allocated
714 * above for command line args and board info.
715 */
716 asm( "mr %0,1": "=r"(nsp) : );
717 nsp -= 2048; /* just to be sure */
718 nsp &= ~0xF;
719 if (nsp > initrd_high) /* limit as specified */
720 nsp = initrd_high;
721 nsp -= len;
722 nsp &= ~(4096 - 1); /* align on page */
723 if (nsp >= sp)
724 initrd_start = nsp;
725 }
726
727 SHOW_BOOT_PROGRESS (12);
wdenk56f94be2002-11-05 16:35:14 +0000728
729 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
wdenk47d1a6e2002-11-03 00:01:44 +0000730 data, data + len - 1, len, len);
wdenk56f94be2002-11-05 16:35:14 +0000731
wdenk47d1a6e2002-11-03 00:01:44 +0000732 initrd_end = initrd_start + len;
733 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
734 initrd_start, initrd_end);
735#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
736 {
737 size_t l = len;
738 void *to = (void *)initrd_start;
739 void *from = (void *)data;
740
741 while (l > 0) {
742 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
743 WATCHDOG_RESET();
744 memmove (to, from, tail);
745 to += tail;
746 from += tail;
747 l -= tail;
748 }
749 }
750#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
751 memmove ((void *)initrd_start, (void *)data, len);
752#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
753 printf ("OK\n");
wdenk38b99262003-05-23 23:18:21 +0000754 }
wdenk47d1a6e2002-11-03 00:01:44 +0000755 } else {
756 initrd_start = 0;
757 initrd_end = 0;
758 }
759
wdenk56f94be2002-11-05 16:35:14 +0000760
761 debug ("## Transferring control to Linux (at address %08lx) ...\n",
wdenk47d1a6e2002-11-03 00:01:44 +0000762 (ulong)kernel);
wdenk56f94be2002-11-05 16:35:14 +0000763
wdenk47d1a6e2002-11-03 00:01:44 +0000764 SHOW_BOOT_PROGRESS (15);
765
wdenk42d1f032003-10-15 23:53:47 +0000766#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
wdenk47d1a6e2002-11-03 00:01:44 +0000767 unlock_ram_in_cache();
768#endif
769 /*
770 * Linux Kernel Parameters:
771 * r3: ptr to board info data
772 * r4: initrd_start or 0 if no initrd
773 * r5: initrd_end - unused if r4 is 0
774 * r6: Start of command line string
775 * r7: End of command line string
776 */
777 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
778}
wdenk1cb8e982003-03-06 21:55:29 +0000779#endif /* CONFIG_PPC */
wdenk47d1a6e2002-11-03 00:01:44 +0000780
781static void
782do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
783 int argc, char *argv[],
784 ulong addr,
785 ulong *len_ptr,
786 int verify)
787{
788 DECLARE_GLOBAL_DATA_PTR;
789
790 image_header_t *hdr = &header;
791
792 void (*loader)(bd_t *, image_header_t *, char *, char *);
793 image_header_t *img_addr;
794 char *consdev;
795 char *cmdline;
796
797
798 /*
799 * Booting a (NetBSD) kernel image
800 *
801 * This process is pretty similar to a standalone application:
802 * The (first part of an multi-) image must be a stage-2 loader,
803 * which in turn is responsible for loading & invoking the actual
804 * kernel. The only differences are the parameters being passed:
805 * besides the board info strucure, the loader expects a command
806 * line, the name of the console device, and (optionally) the
807 * address of the original image header.
808 */
809
810 img_addr = 0;
811 if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
812 img_addr = (image_header_t *) addr;
813
814
815 consdev = "";
816#if defined (CONFIG_8xx_CONS_SMC1)
817 consdev = "smc1";
818#elif defined (CONFIG_8xx_CONS_SMC2)
819 consdev = "smc2";
820#elif defined (CONFIG_8xx_CONS_SCC2)
821 consdev = "scc2";
822#elif defined (CONFIG_8xx_CONS_SCC3)
823 consdev = "scc3";
824#endif
825
826 if (argc > 2) {
827 ulong len;
828 int i;
829
830 for (i=2, len=0 ; i<argc ; i+=1)
831 len += strlen (argv[i]) + 1;
832 cmdline = malloc (len);
833
834 for (i=2, len=0 ; i<argc ; i+=1) {
835 if (i > 2)
836 cmdline[len++] = ' ';
837 strcpy (&cmdline[len], argv[i]);
838 len += strlen (argv[i]);
839 }
840 } else if ((cmdline = getenv("bootargs")) == NULL) {
841 cmdline = "";
842 }
843
844 loader = (void (*)(bd_t *, image_header_t *, char *, char *)) hdr->ih_ep;
845
846 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
847 (ulong)loader);
848
849 SHOW_BOOT_PROGRESS (15);
850
851 /*
852 * NetBSD Stage-2 Loader Parameters:
853 * r3: ptr to board info data
854 * r4: image address
855 * r5: console device
856 * r6: boot args string
857 */
858 (*loader) (gd->bd, img_addr, consdev, cmdline);
859}
860
wdenk7f70e852003-05-20 14:25:27 +0000861#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
862
863/* Function that returns a character from the environment */
864extern uchar (*env_get_char)(int);
865
866static void
867do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
868 int argc, char *argv[],
869 ulong addr,
870 ulong *len_ptr,
871 int verify)
872{
873 DECLARE_GLOBAL_DATA_PTR;
874 ulong top;
875 char *s, *cmdline;
876 char **fwenv, **ss;
877 int i, j, nxt, len, envno, envsz;
878 bd_t *kbd;
879 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
880 image_header_t *hdr = &header;
881
882 /*
883 * Booting an ARTOS kernel image + application
884 */
885
886 /* this used to be the top of memory, but was wrong... */
887#ifdef CONFIG_PPC
888 /* get stack pointer */
889 asm volatile ("mr %0,1" : "=r"(top) );
890#endif
891 debug ("## Current stack ends at 0x%08lX ", top);
892
893 top -= 2048; /* just to be sure */
894 if (top > CFG_BOOTMAPSZ)
895 top = CFG_BOOTMAPSZ;
896 top &= ~0xF;
897
898 debug ("=> set upper limit to 0x%08lX\n", top);
899
900 /* first check the artos specific boot args, then the linux args*/
901 if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
902 s = "";
903
904 /* get length of cmdline, and place it */
905 len = strlen(s);
906 top = (top - (len + 1)) & ~0xF;
907 cmdline = (char *)top;
908 debug ("## cmdline at 0x%08lX ", top);
909 strcpy(cmdline, s);
910
911 /* copy bdinfo */
912 top = (top - sizeof(bd_t)) & ~0xF;
913 debug ("## bd at 0x%08lX ", top);
914 kbd = (bd_t *)top;
915 memcpy(kbd, gd->bd, sizeof(bd_t));
916
917 /* first find number of env entries, and their size */
918 envno = 0;
919 envsz = 0;
920 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
921 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
922 ;
923 envno++;
924 envsz += (nxt - i) + 1; /* plus trailing zero */
925 }
926 envno++; /* plus the terminating zero */
927 debug ("## %u envvars total size %u ", envno, envsz);
928
929 top = (top - sizeof(char **)*envno) & ~0xF;
930 fwenv = (char **)top;
931 debug ("## fwenv at 0x%08lX ", top);
932
933 top = (top - envsz) & ~0xF;
934 s = (char *)top;
935 ss = fwenv;
936
937 /* now copy them */
938 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
939 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
940 ;
941 *ss++ = s;
942 for (j = i; j < nxt; ++j)
943 *s++ = env_get_char(j);
944 *s++ = '\0';
945 }
946 *ss++ = NULL; /* terminate */
947
948 entry = (void (*)(bd_t *, char *, char **, ulong))ntohl(hdr->ih_ep);
949 (*entry)(kbd, cmdline, fwenv, top);
950}
951#endif
952
953
wdenk47d1a6e2002-11-03 00:01:44 +0000954#if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
955int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
956{
957 int rcode = 0;
958#ifndef CFG_HUSH_PARSER
959 if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
960#else
961 if (parse_string_outer(getenv("bootcmd"),
962 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
963#endif
964 return rcode;
965}
wdenk8bde7f72003-06-27 21:31:46 +0000966
wdenk0d498392003-07-01 21:06:45 +0000967U_BOOT_CMD(
968 boot, 1, 1, do_bootd,
wdenk9d2b18a2003-06-28 23:11:04 +0000969 "boot - boot default, i.e., run 'bootcmd'\n",
970 NULL
971);
972
973/* keep old command name "bootd" for backward compatibility */
wdenk0d498392003-07-01 21:06:45 +0000974U_BOOT_CMD(
975 bootd, 1, 1, do_bootd,
wdenk8bde7f72003-06-27 21:31:46 +0000976 "bootd - boot default, i.e., run 'bootcmd'\n",
977 NULL
978);
979
wdenk47d1a6e2002-11-03 00:01:44 +0000980#endif
981
982#if (CONFIG_COMMANDS & CFG_CMD_IMI)
983int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
984{
985 int arg;
986 ulong addr;
987 int rcode=0;
988
989 if (argc < 2) {
990 return image_info (load_addr);
991 }
992
993 for (arg=1; arg <argc; ++arg) {
994 addr = simple_strtoul(argv[arg], NULL, 16);
995 if (image_info (addr) != 0) rcode = 1;
996 }
997 return rcode;
998}
999
1000static int image_info (ulong addr)
1001{
1002 ulong data, len, checksum;
1003 image_header_t *hdr = &header;
1004
1005 printf ("\n## Checking Image at %08lx ...\n", addr);
1006
1007 /* Copy header so we can blank CRC field for re-calculation */
1008 memmove (&header, (char *)addr, sizeof(image_header_t));
1009
1010 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
1011 printf (" Bad Magic Number\n");
1012 return 1;
1013 }
1014
1015 data = (ulong)&header;
1016 len = sizeof(image_header_t);
1017
1018 checksum = ntohl(hdr->ih_hcrc);
1019 hdr->ih_hcrc = 0;
1020
1021 if (crc32 (0, (char *)data, len) != checksum) {
1022 printf (" Bad Header Checksum\n");
1023 return 1;
1024 }
1025
1026 /* for multi-file images we need the data part, too */
1027 print_image_hdr ((image_header_t *)addr);
1028
1029 data = addr + sizeof(image_header_t);
1030 len = ntohl(hdr->ih_size);
1031
1032 printf (" Verifying Checksum ... ");
1033 if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
1034 printf (" Bad Data CRC\n");
1035 return 1;
1036 }
1037 printf ("OK\n");
1038 return 0;
1039}
wdenk0d498392003-07-01 21:06:45 +00001040
1041U_BOOT_CMD(
1042 iminfo, CFG_MAXARGS, 1, do_iminfo,
wdenk8bde7f72003-06-27 21:31:46 +00001043 "iminfo - print header information for application image\n",
1044 "addr [addr ...]\n"
1045 " - print header information for application image starting at\n"
1046 " address 'addr' in memory; this includes verification of the\n"
1047 " image contents (magic number, header and payload checksums)\n"
1048);
1049
wdenk47d1a6e2002-11-03 00:01:44 +00001050#endif /* CFG_CMD_IMI */
1051
wdenk27b207f2003-07-24 23:38:38 +00001052#if (CONFIG_COMMANDS & CFG_CMD_IMLS)
1053/*-----------------------------------------------------------------------
1054 * List all images found in flash.
1055 */
1056int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1057{
1058 flash_info_t *info;
1059 int i, j;
1060 image_header_t *hdr;
wdenk5bb226e2003-11-17 21:14:37 +00001061 ulong data, len, checksum;
wdenk27b207f2003-07-24 23:38:38 +00001062
1063 for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
1064 if (info->flash_id == FLASH_UNKNOWN)
1065 goto next_bank;
1066 for (j=0; j<CFG_MAX_FLASH_SECT; ++j) {
1067
1068 if (!(hdr=(image_header_t *)info->start[j]) ||
1069 (ntohl(hdr->ih_magic) != IH_MAGIC))
1070 goto next_sector;
1071
1072 /* Copy header so we can blank CRC field for re-calculation */
1073 memmove (&header, (char *)hdr, sizeof(image_header_t));
1074
1075 checksum = ntohl(header.ih_hcrc);
1076 header.ih_hcrc = 0;
1077
1078 if (crc32 (0, (char *)&header, sizeof(image_header_t))
1079 != checksum)
1080 goto next_sector;
1081
1082 printf ("Image at %08lX:\n", (ulong)hdr);
1083 print_image_hdr( hdr );
wdenk5bb226e2003-11-17 21:14:37 +00001084
1085 data = (ulong)hdr + sizeof(image_header_t);
1086 len = ntohl(hdr->ih_size);
1087
1088 printf (" Verifying Checksum ... ");
1089 if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
1090 printf (" Bad Data CRC\n");
1091 }
1092 printf ("OK\n");
wdenkbdccc4f2003-08-05 17:43:17 +00001093next_sector: ;
wdenk27b207f2003-07-24 23:38:38 +00001094 }
wdenkbdccc4f2003-08-05 17:43:17 +00001095next_bank: ;
wdenk27b207f2003-07-24 23:38:38 +00001096 }
1097
1098 return (0);
1099}
1100
1101U_BOOT_CMD(
1102 imls, 1, 1, do_imls,
1103 "imls - list all images found in flash\n",
1104 "\n"
1105 " - Prints information about all images found at sector\n"
1106 " boundaries in flash.\n"
1107);
1108#endif /* CFG_CMD_IMLS */
1109
wdenk47d1a6e2002-11-03 00:01:44 +00001110void
1111print_image_hdr (image_header_t *hdr)
1112{
1113#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
1114 time_t timestamp = (time_t)ntohl(hdr->ih_time);
1115 struct rtc_time tm;
1116#endif
1117
1118 printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
1119#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
1120 to_tm (timestamp, &tm);
1121 printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
1122 tm.tm_year, tm.tm_mon, tm.tm_mday,
1123 tm.tm_hour, tm.tm_min, tm.tm_sec);
1124#endif /* CFG_CMD_DATE, CONFIG_TIMESTAMP */
1125 printf (" Image Type: "); print_type(hdr); printf ("\n");
1126 printf (" Data Size: %d Bytes = ", ntohl(hdr->ih_size));
1127 print_size (ntohl(hdr->ih_size), "\n");
1128 printf (" Load Address: %08x\n", ntohl(hdr->ih_load));
1129 printf (" Entry Point: %08x\n", ntohl(hdr->ih_ep));
1130
1131 if (hdr->ih_type == IH_TYPE_MULTI) {
1132 int i;
1133 ulong len;
1134 ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
1135
1136 printf (" Contents:\n");
1137 for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
1138 printf (" Image %d: %8ld Bytes = ", i, len);
1139 print_size (len, "\n");
1140 }
1141 }
1142}
1143
1144
1145static void
1146print_type (image_header_t *hdr)
1147{
1148 char *os, *arch, *type, *comp;
1149
1150 switch (hdr->ih_os) {
1151 case IH_OS_INVALID: os = "Invalid OS"; break;
1152 case IH_OS_NETBSD: os = "NetBSD"; break;
1153 case IH_OS_LINUX: os = "Linux"; break;
1154 case IH_OS_VXWORKS: os = "VxWorks"; break;
1155 case IH_OS_QNX: os = "QNX"; break;
1156 case IH_OS_U_BOOT: os = "U-Boot"; break;
wdenkd791b1d2003-04-20 14:04:18 +00001157 case IH_OS_RTEMS: os = "RTEMS"; break;
wdenk7f70e852003-05-20 14:25:27 +00001158#ifdef CONFIG_ARTOS
1159 case IH_OS_ARTOS: os = "ARTOS"; break;
1160#endif
wdenk1f4bb372003-07-27 00:21:01 +00001161#ifdef CONFIG_LYNXKDI
1162 case IH_OS_LYNXOS: os = "LynxOS"; break;
1163#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001164 default: os = "Unknown OS"; break;
1165 }
1166
1167 switch (hdr->ih_arch) {
1168 case IH_CPU_INVALID: arch = "Invalid CPU"; break;
1169 case IH_CPU_ALPHA: arch = "Alpha"; break;
1170 case IH_CPU_ARM: arch = "ARM"; break;
1171 case IH_CPU_I386: arch = "Intel x86"; break;
1172 case IH_CPU_IA64: arch = "IA64"; break;
1173 case IH_CPU_MIPS: arch = "MIPS"; break;
1174 case IH_CPU_MIPS64: arch = "MIPS 64 Bit"; break;
1175 case IH_CPU_PPC: arch = "PowerPC"; break;
1176 case IH_CPU_S390: arch = "IBM S390"; break;
1177 case IH_CPU_SH: arch = "SuperH"; break;
1178 case IH_CPU_SPARC: arch = "SPARC"; break;
1179 case IH_CPU_SPARC64: arch = "SPARC 64 Bit"; break;
wdenk8564acf2003-07-14 22:13:32 +00001180 case IH_CPU_M68K: arch = "M68K"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001181 default: arch = "Unknown Architecture"; break;
1182 }
1183
1184 switch (hdr->ih_type) {
1185 case IH_TYPE_INVALID: type = "Invalid Image"; break;
1186 case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
1187 case IH_TYPE_KERNEL: type = "Kernel Image"; break;
1188 case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
1189 case IH_TYPE_MULTI: type = "Multi-File Image"; break;
1190 case IH_TYPE_FIRMWARE: type = "Firmware"; break;
1191 case IH_TYPE_SCRIPT: type = "Script"; break;
1192 default: type = "Unknown Image"; break;
1193 }
1194
1195 switch (hdr->ih_comp) {
1196 case IH_COMP_NONE: comp = "uncompressed"; break;
1197 case IH_COMP_GZIP: comp = "gzip compressed"; break;
1198 case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
1199 default: comp = "unknown compression"; break;
1200 }
1201
1202 printf ("%s %s %s (%s)", arch, os, type, comp);
1203}
1204
1205#define ZALLOC_ALIGNMENT 16
1206
1207static void *zalloc(void *x, unsigned items, unsigned size)
1208{
1209 void *p;
1210
1211 size *= items;
1212 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
1213
1214 p = malloc (size);
1215
1216 return (p);
1217}
1218
1219static void zfree(void *x, void *addr, unsigned nb)
1220{
1221 free (addr);
1222}
1223
1224#define HEAD_CRC 2
1225#define EXTRA_FIELD 4
1226#define ORIG_NAME 8
1227#define COMMENT 0x10
1228#define RESERVED 0xe0
1229
1230#define DEFLATED 8
1231
1232int gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
1233{
1234 z_stream s;
1235 int r, i, flags;
1236
1237 /* skip header */
1238 i = 10;
1239 flags = src[3];
1240 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
1241 printf ("Error: Bad gzipped data\n");
1242 return (-1);
1243 }
1244 if ((flags & EXTRA_FIELD) != 0)
1245 i = 12 + src[10] + (src[11] << 8);
1246 if ((flags & ORIG_NAME) != 0)
1247 while (src[i++] != 0)
1248 ;
1249 if ((flags & COMMENT) != 0)
1250 while (src[i++] != 0)
1251 ;
1252 if ((flags & HEAD_CRC) != 0)
1253 i += 2;
1254 if (i >= *lenp) {
1255 printf ("Error: gunzip out of data in header\n");
1256 return (-1);
1257 }
1258
1259 s.zalloc = zalloc;
1260 s.zfree = zfree;
1261#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
1262 s.outcb = (cb_func)WATCHDOG_RESET;
1263#else
1264 s.outcb = Z_NULL;
1265#endif /* CONFIG_HW_WATCHDOG */
1266
1267 r = inflateInit2(&s, -MAX_WBITS);
1268 if (r != Z_OK) {
1269 printf ("Error: inflateInit2() returned %d\n", r);
1270 return (-1);
1271 }
1272 s.next_in = src + i;
1273 s.avail_in = *lenp - i;
1274 s.next_out = dst;
1275 s.avail_out = dstlen;
1276 r = inflate(&s, Z_FINISH);
1277 if (r != Z_OK && r != Z_STREAM_END) {
1278 printf ("Error: inflate() returned %d\n", r);
1279 return (-1);
1280 }
1281 *lenp = s.next_out - (unsigned char *) dst;
1282 inflateEnd(&s);
1283
1284 return (0);
1285}
1286
wdenkc29fdfc2003-08-29 20:57:53 +00001287#ifdef CONFIG_BZIP2
1288void bz_internal_error(int errcode)
1289{
1290 printf ("BZIP2 internal error %d\n", errcode);
1291}
1292#endif /* CONFIG_BZIP2 */
1293
wdenkd791b1d2003-04-20 14:04:18 +00001294static void
1295do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1296 ulong addr, ulong *len_ptr, int verify)
1297{
1298 DECLARE_GLOBAL_DATA_PTR;
1299 image_header_t *hdr = &header;
1300 void (*entry_point)(bd_t *);
1301
1302 entry_point = (void (*)(bd_t *)) hdr->ih_ep;
1303
1304 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
1305 (ulong)entry_point);
1306
1307 SHOW_BOOT_PROGRESS (15);
1308
1309 /*
1310 * RTEMS Parameters:
1311 * r3: ptr to board info data
1312 */
1313
1314 (*entry_point ) ( gd->bd );
1315}
1316
wdenk47d1a6e2002-11-03 00:01:44 +00001317#if (CONFIG_COMMANDS & CFG_CMD_ELF)
1318static void
1319do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1320 ulong addr, ulong *len_ptr, int verify)
1321{
1322 image_header_t *hdr = &header;
1323 char str[80];
1324
1325 sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
1326 setenv("loadaddr", str);
1327 do_bootvx(cmdtp, 0, 0, NULL);
1328}
1329
1330static void
1331do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1332 ulong addr, ulong *len_ptr, int verify)
1333{
1334 image_header_t *hdr = &header;
1335 char *local_args[2];
1336 char str[16];
1337
1338 sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
1339 local_args[0] = argv[0];
1340 local_args[1] = str; /* and provide it via the arguments */
1341 do_bootelf(cmdtp, 0, 2, local_args);
1342}
1343#endif /* CFG_CMD_ELF */
wdenk1f4bb372003-07-27 00:21:01 +00001344
1345#ifdef CONFIG_LYNXKDI
1346static void
1347do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
1348 int argc, char *argv[],
1349 ulong addr,
1350 ulong *len_ptr,
1351 int verify)
1352{
1353 lynxkdi_boot( &header );
1354}
1355
1356#endif /* CONFIG_LYNXKDI */