blob: 72553aa0614030e951ec1e752b6a56f6de874103 [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>
wdenk710e3502003-08-29 20:57:53 +000033#include <bzlib.h>
wdenk0893c472003-05-20 14:25:27 +000034#include <environment.h>
wdenk47d1a6e2002-11-03 00:01:44 +000035#include <asm/byteorder.h>
wdenk57b2d802003-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
wdenk9dfa8d12002-12-08 09:53:23 +000059#ifdef CONFIG_LOGBUFFER
60#include <logbuff.h>
61#endif
62
wdenk381669a2003-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
wdenk874ac262003-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
wdenk591dda52002-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
wdenk57b2d802003-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
wdenk591dda52002-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
wdenk808532a2003-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;
wdenk92bbe3f2003-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 */
wdenk0893c472003-05-20 14:25:27 +0000131#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
132static boot_os_Fcn do_bootm_artos;
133#endif
wdenke58b0dc2003-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;
wdenk710e3502003-08-29 20:57:53 +0000149 uint unc_len = 0x400000;
wdenk47d1a6e2002-11-03 00:01:44 +0000150 int i, verify;
151 char *name, *s;
wdenk655a0f92003-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 */
wdenk381669a2003-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) {
wdenk591dda52002-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;
wdenk591dda52002-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 */
wdenka983cc22003-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
wdenk381669a2003-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 }
wdenk57b2d802003-06-27 21:31:46 +0000218#endif
wdenk381669a2003-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
wdenk591dda52002-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)
wdenk4fc95692003-02-28 00:49:47 +0000239#elif defined(__mips__)
wdenk57b2d802003-06-27 21:31:46 +0000240 if (hdr->ih_arch != IH_CPU_MIPS)
wdenk60164a82003-10-08 23:26:14 +0000241#elif defined(__nios__)
242 if (hdr->ih_arch != IH_CPU_NIOS)
wdenkabf7a7c2003-12-08 01:34:36 +0000243#elif defined(__M68K__)
244 if (hdr->ih_arch != IH_CPU_M68K)
wdenk591dda52002-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) {
wdenk655a0f92003-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;
wdenk1ebf41e2004-01-02 14:00:00 +0000266 case IH_TYPE_MULTI:
wdenk655a0f92003-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
wdenk452cfd62002-11-19 11:04:11 +0000288#ifdef CONFIG_AMIGAONEG3SE
289 /*
wdenk57b2d802003-06-27 21:31:46 +0000290 * We've possible left the caches enabled during
wdenk452cfd62002-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:
wdenk591dda52002-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);
wdenk710e3502003-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;
wdenk710e3502003-08-29 20:57:53 +0000333#ifdef CONFIG_BZIP2
334 case IH_COMP_BZIP2:
335 printf (" Uncompressing %s ... ", name);
wdenk2cefd152004-02-08 22:55:38 +0000336 /*
337 * If we've got less than 4 MB of malloc() space,
338 * use slower decompression algorithm which requires
339 * at most 2300 KB of memory.
340 */
wdenk710e3502003-08-29 20:57:53 +0000341 i = BZ2_bzBuffToBuffDecompress ((char*)ntohl(hdr->ih_load),
wdenk2cefd152004-02-08 22:55:38 +0000342 &unc_len, (char *)data, len,
343 CFG_MALLOC_LEN < (4096 * 1024), 0);
wdenk710e3502003-08-29 20:57:53 +0000344 if (i != BZ_OK) {
345 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
346 SHOW_BOOT_PROGRESS (-6);
347 udelay(100000);
348 do_reset (cmdtp, flag, argc, argv);
349 }
350 break;
351#endif /* CONFIG_BZIP2 */
wdenk47d1a6e2002-11-03 00:01:44 +0000352 default:
353 if (iflag)
354 enable_interrupts();
355 printf ("Unimplemented compression type %d\n", hdr->ih_comp);
356 SHOW_BOOT_PROGRESS (-7);
357 return 1;
358 }
359 printf ("OK\n");
360 SHOW_BOOT_PROGRESS (7);
361
362 switch (hdr->ih_type) {
363 case IH_TYPE_STANDALONE:
wdenk47d1a6e2002-11-03 00:01:44 +0000364 if (iflag)
365 enable_interrupts();
366
wdenk3f9ab982003-04-12 23:38:12 +0000367 /* load (and uncompress), but don't start if "autostart"
368 * is set to "no"
369 */
wdenk60164a82003-10-08 23:26:14 +0000370 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
371 char buf[32];
372 sprintf(buf, "%lX", len);
373 setenv("filesize", buf);
wdenk3f9ab982003-04-12 23:38:12 +0000374 return 0;
wdenk60164a82003-10-08 23:26:14 +0000375 }
wdenk655a0f92003-10-30 21:49:38 +0000376 appl = (int (*)(int, char *[]))ntohl(hdr->ih_ep);
377 (*appl)(argc-1, &argv[1]);
wdenk3f9ab982003-04-12 23:38:12 +0000378 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000379 case IH_TYPE_KERNEL:
380 case IH_TYPE_MULTI:
381 /* handled below */
382 break;
383 default:
384 if (iflag)
385 enable_interrupts();
386 printf ("Can't boot image type %d\n", hdr->ih_type);
387 SHOW_BOOT_PROGRESS (-8);
388 return 1;
389 }
390 SHOW_BOOT_PROGRESS (8);
391
392 switch (hdr->ih_os) {
393 default: /* handled by (original) Linux case */
394 case IH_OS_LINUX:
wdenk808532a2003-10-10 10:05:42 +0000395#ifdef CONFIG_SILENT_CONSOLE
396 fixup_silent_linux();
397#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000398 do_bootm_linux (cmdtp, flag, argc, argv,
399 addr, len_ptr, verify);
400 break;
401 case IH_OS_NETBSD:
402 do_bootm_netbsd (cmdtp, flag, argc, argv,
403 addr, len_ptr, verify);
404 break;
wdenke58b0dc2003-07-27 00:21:01 +0000405
406#ifdef CONFIG_LYNXKDI
407 case IH_OS_LYNXOS:
408 do_bootm_lynxkdi (cmdtp, flag, argc, argv,
409 addr, len_ptr, verify);
410 break;
411#endif
wdenk57b2d802003-06-27 21:31:46 +0000412
wdenk92bbe3f2003-04-20 14:04:18 +0000413 case IH_OS_RTEMS:
414 do_bootm_rtems (cmdtp, flag, argc, argv,
415 addr, len_ptr, verify);
416 break;
wdenk57b2d802003-06-27 21:31:46 +0000417
wdenk47d1a6e2002-11-03 00:01:44 +0000418#if (CONFIG_COMMANDS & CFG_CMD_ELF)
419 case IH_OS_VXWORKS:
420 do_bootm_vxworks (cmdtp, flag, argc, argv,
421 addr, len_ptr, verify);
422 break;
423 case IH_OS_QNX:
424 do_bootm_qnxelf (cmdtp, flag, argc, argv,
425 addr, len_ptr, verify);
426 break;
427#endif /* CFG_CMD_ELF */
wdenk0893c472003-05-20 14:25:27 +0000428#ifdef CONFIG_ARTOS
429 case IH_OS_ARTOS:
430 do_bootm_artos (cmdtp, flag, argc, argv,
431 addr, len_ptr, verify);
432 break;
433#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000434 }
435
436 SHOW_BOOT_PROGRESS (-9);
437#ifdef DEBUG
438 printf ("\n## Control returned to monitor - resetting...\n");
439 do_reset (cmdtp, flag, argc, argv);
440#endif
441 return 1;
442}
443
wdenkf287a242003-07-01 21:06:45 +0000444U_BOOT_CMD(
445 bootm, CFG_MAXARGS, 1, do_bootm,
wdenk57b2d802003-06-27 21:31:46 +0000446 "bootm - boot application image from memory\n",
447 "[addr [arg ...]]\n - boot application image stored in memory\n"
448 " passing arguments 'arg ...'; when booting a Linux kernel,\n"
449 " 'arg' can be the address of an initrd image\n"
450);
451
wdenk808532a2003-10-10 10:05:42 +0000452#ifdef CONFIG_SILENT_CONSOLE
453static void
454fixup_silent_linux ()
455{
456 DECLARE_GLOBAL_DATA_PTR;
457 char buf[256], *start, *end;
458 char *cmdline = getenv ("bootargs");
459
460 /* Only fix cmdline when requested */
461 if (!(gd->flags & GD_FLG_SILENT))
462 return;
463
464 debug ("before silent fix-up: %s\n", cmdline);
465 if (cmdline) {
466 if ((start = strstr (cmdline, "console=")) != NULL) {
467 end = strchr (start, ' ');
468 strncpy (buf, cmdline, (start - cmdline + 8));
469 if (end)
470 strcpy (buf + (start - cmdline + 8), end);
471 else
472 buf[start - cmdline + 8] = '\0';
473 } else {
474 strcpy (buf, cmdline);
475 strcat (buf, " console=");
476 }
477 } else {
478 strcpy (buf, "console=");
479 }
480
481 setenv ("bootargs", buf);
482 debug ("after silent fix-up: %s\n", buf);
483}
484#endif /* CONFIG_SILENT_CONSOLE */
485
wdenk591dda52002-11-18 00:14:45 +0000486#ifdef CONFIG_PPC
wdenk47d1a6e2002-11-03 00:01:44 +0000487static void
488do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
489 int argc, char *argv[],
490 ulong addr,
491 ulong *len_ptr,
492 int verify)
493{
494 DECLARE_GLOBAL_DATA_PTR;
495
496 ulong sp;
497 ulong len, checksum;
498 ulong initrd_start, initrd_end;
499 ulong cmd_start, cmd_end;
500 ulong initrd_high;
501 ulong data;
wdenk6f770ed2003-05-23 23:18:21 +0000502 int initrd_copy_to_ram = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000503 char *cmdline;
504 char *s;
505 bd_t *kbd;
506 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
507 image_header_t *hdr = &header;
508
509 if ((s = getenv ("initrd_high")) != NULL) {
510 /* a value of "no" or a similar string will act like 0,
511 * turning the "load high" feature off. This is intentional.
512 */
513 initrd_high = simple_strtoul(s, NULL, 16);
wdenk6f770ed2003-05-23 23:18:21 +0000514 if (initrd_high == ~0)
515 initrd_copy_to_ram = 0;
wdenk9dfa8d12002-12-08 09:53:23 +0000516 } else { /* not set, no restrictions to load high */
wdenk47d1a6e2002-11-03 00:01:44 +0000517 initrd_high = ~0;
518 }
519
wdenk56f94be2002-11-05 16:35:14 +0000520#ifdef CONFIG_LOGBUFFER
wdenk34b613e2002-12-17 01:51:00 +0000521 kbd=gd->bd;
wdenk9dfa8d12002-12-08 09:53:23 +0000522 /* Prevent initrd from overwriting logbuffer */
523 if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
524 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
525 debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
wdenk56f94be2002-11-05 16:35:14 +0000526#endif
527
wdenk47d1a6e2002-11-03 00:01:44 +0000528 /*
529 * Booting a (Linux) kernel image
530 *
531 * Allocate space for command line and board info - the
532 * address should be as high as possible within the reach of
533 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
534 * memory, which means far enough below the current stack
535 * pointer.
536 */
537
538 asm( "mr %0,1": "=r"(sp) : );
539
wdenk56f94be2002-11-05 16:35:14 +0000540 debug ("## Current stack ends at 0x%08lX ", sp);
541
wdenk47d1a6e2002-11-03 00:01:44 +0000542 sp -= 2048; /* just to be sure */
543 if (sp > CFG_BOOTMAPSZ)
544 sp = CFG_BOOTMAPSZ;
545 sp &= ~0xF;
546
wdenk56f94be2002-11-05 16:35:14 +0000547 debug ("=> set upper limit to 0x%08lX\n", sp);
548
wdenk47d1a6e2002-11-03 00:01:44 +0000549 cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
550 kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
551
552 if ((s = getenv("bootargs")) == NULL)
553 s = "";
554
555 strcpy (cmdline, s);
556
557 cmd_start = (ulong)&cmdline[0];
558 cmd_end = cmd_start + strlen(cmdline);
559
560 *kbd = *(gd->bd);
561
562#ifdef DEBUG
563 printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
564
565 do_bdinfo (NULL, 0, 0, NULL);
566#endif
567
568 if ((s = getenv ("clocks_in_mhz")) != NULL) {
569 /* convert all clock information to MHz */
570 kbd->bi_intfreq /= 1000000L;
571 kbd->bi_busfreq /= 1000000L;
wdenk9c53f402003-10-15 23:53:47 +0000572#if defined(CONFIG_8260) || defined(CONFIG_MPC8560)
wdenk47d1a6e2002-11-03 00:01:44 +0000573 kbd->bi_cpmfreq /= 1000000L;
574 kbd->bi_brgfreq /= 1000000L;
575 kbd->bi_sccfreq /= 1000000L;
576 kbd->bi_vco /= 1000000L;
577#endif /* CONFIG_8260 */
wdenkbe9c1cb2004-02-24 02:00:03 +0000578#if defined(CONFIG_MPC5xxx)
wdenk21136db2003-07-16 21:53:01 +0000579 kbd->bi_ipbfreq /= 1000000L;
580 kbd->bi_pcifreq /= 1000000L;
wdenkbe9c1cb2004-02-24 02:00:03 +0000581#endif /* CONFIG_MPC5xxx */
wdenk47d1a6e2002-11-03 00:01:44 +0000582 }
583
584 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))hdr->ih_ep;
585
586 /*
587 * Check if there is an initrd image
588 */
589 if (argc >= 3) {
590 SHOW_BOOT_PROGRESS (9);
591
592 addr = simple_strtoul(argv[2], NULL, 16);
593
594 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
595
596 /* Copy header so we can blank CRC field for re-calculation */
597 memmove (&header, (char *)addr, sizeof(image_header_t));
598
599 if (hdr->ih_magic != IH_MAGIC) {
600 printf ("Bad Magic Number\n");
601 SHOW_BOOT_PROGRESS (-10);
602 do_reset (cmdtp, flag, argc, argv);
603 }
604
605 data = (ulong)&header;
606 len = sizeof(image_header_t);
607
608 checksum = hdr->ih_hcrc;
609 hdr->ih_hcrc = 0;
610
611 if (crc32 (0, (char *)data, len) != checksum) {
612 printf ("Bad Header Checksum\n");
613 SHOW_BOOT_PROGRESS (-11);
614 do_reset (cmdtp, flag, argc, argv);
615 }
616
617 SHOW_BOOT_PROGRESS (10);
618
619 print_image_hdr (hdr);
620
621 data = addr + sizeof(image_header_t);
622 len = hdr->ih_size;
623
624 if (verify) {
625 ulong csum = 0;
626#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
627 ulong cdata = data, edata = cdata + len;
628#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
629
630 printf (" Verifying Checksum ... ");
631
632#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
633
634 while (cdata < edata) {
635 ulong chunk = edata - cdata;
636
637 if (chunk > CHUNKSZ)
638 chunk = CHUNKSZ;
639 csum = crc32 (csum, (char *)cdata, chunk);
640 cdata += chunk;
641
642 WATCHDOG_RESET();
643 }
644#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
645 csum = crc32 (0, (char *)data, len);
646#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
647
648 if (csum != hdr->ih_dcrc) {
649 printf ("Bad Data CRC\n");
650 SHOW_BOOT_PROGRESS (-12);
651 do_reset (cmdtp, flag, argc, argv);
652 }
653 printf ("OK\n");
654 }
655
656 SHOW_BOOT_PROGRESS (11);
657
658 if ((hdr->ih_os != IH_OS_LINUX) ||
659 (hdr->ih_arch != IH_CPU_PPC) ||
660 (hdr->ih_type != IH_TYPE_RAMDISK) ) {
661 printf ("No Linux PPC Ramdisk Image\n");
662 SHOW_BOOT_PROGRESS (-13);
663 do_reset (cmdtp, flag, argc, argv);
664 }
665
666 /*
667 * Now check if we have a multifile image
668 */
669 } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
670 u_long tail = ntohl(len_ptr[0]) % 4;
671 int i;
672
673 SHOW_BOOT_PROGRESS (13);
674
675 /* skip kernel length and terminator */
676 data = (ulong)(&len_ptr[2]);
677 /* skip any additional image length fields */
678 for (i=1; len_ptr[i]; ++i)
679 data += 4;
680 /* add kernel length, and align */
681 data += ntohl(len_ptr[0]);
682 if (tail) {
683 data += 4 - tail;
684 }
685
686 len = ntohl(len_ptr[1]);
687
688 } else {
689 /*
690 * no initrd image
691 */
692 SHOW_BOOT_PROGRESS (14);
693
694 len = data = 0;
695 }
696
wdenk47d1a6e2002-11-03 00:01:44 +0000697 if (!data) {
wdenk56f94be2002-11-05 16:35:14 +0000698 debug ("No initrd\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000699 }
wdenk47d1a6e2002-11-03 00:01:44 +0000700
701 if (data) {
wdenk6f770ed2003-05-23 23:18:21 +0000702 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
703 initrd_start = data;
704 initrd_end = initrd_start + len;
705 } else {
wdenk47d1a6e2002-11-03 00:01:44 +0000706 initrd_start = (ulong)kbd - len;
707 initrd_start &= ~(4096 - 1); /* align on page */
708
709 if (initrd_high) {
710 ulong nsp;
711
712 /*
713 * the inital ramdisk does not need to be within
714 * CFG_BOOTMAPSZ as it is not accessed until after
715 * the mm system is initialised.
716 *
717 * do the stack bottom calculation again and see if
718 * the initrd will fit just below the monitor stack
719 * bottom without overwriting the area allocated
720 * above for command line args and board info.
721 */
722 asm( "mr %0,1": "=r"(nsp) : );
723 nsp -= 2048; /* just to be sure */
724 nsp &= ~0xF;
725 if (nsp > initrd_high) /* limit as specified */
726 nsp = initrd_high;
727 nsp -= len;
728 nsp &= ~(4096 - 1); /* align on page */
729 if (nsp >= sp)
730 initrd_start = nsp;
731 }
732
733 SHOW_BOOT_PROGRESS (12);
wdenk56f94be2002-11-05 16:35:14 +0000734
735 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
wdenk47d1a6e2002-11-03 00:01:44 +0000736 data, data + len - 1, len, len);
wdenk56f94be2002-11-05 16:35:14 +0000737
wdenk47d1a6e2002-11-03 00:01:44 +0000738 initrd_end = initrd_start + len;
739 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
740 initrd_start, initrd_end);
741#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
742 {
743 size_t l = len;
744 void *to = (void *)initrd_start;
745 void *from = (void *)data;
746
747 while (l > 0) {
748 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
749 WATCHDOG_RESET();
750 memmove (to, from, tail);
751 to += tail;
752 from += tail;
753 l -= tail;
754 }
755 }
756#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
757 memmove ((void *)initrd_start, (void *)data, len);
758#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
759 printf ("OK\n");
wdenk6f770ed2003-05-23 23:18:21 +0000760 }
wdenk47d1a6e2002-11-03 00:01:44 +0000761 } else {
762 initrd_start = 0;
763 initrd_end = 0;
764 }
765
wdenk56f94be2002-11-05 16:35:14 +0000766
767 debug ("## Transferring control to Linux (at address %08lx) ...\n",
wdenk47d1a6e2002-11-03 00:01:44 +0000768 (ulong)kernel);
wdenk56f94be2002-11-05 16:35:14 +0000769
wdenk47d1a6e2002-11-03 00:01:44 +0000770 SHOW_BOOT_PROGRESS (15);
771
wdenk9c53f402003-10-15 23:53:47 +0000772#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
wdenk47d1a6e2002-11-03 00:01:44 +0000773 unlock_ram_in_cache();
774#endif
775 /*
776 * Linux Kernel Parameters:
777 * r3: ptr to board info data
778 * r4: initrd_start or 0 if no initrd
779 * r5: initrd_end - unused if r4 is 0
780 * r6: Start of command line string
781 * r7: End of command line string
782 */
783 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
784}
wdenk1fe2c702003-03-06 21:55:29 +0000785#endif /* CONFIG_PPC */
wdenk47d1a6e2002-11-03 00:01:44 +0000786
787static void
788do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
789 int argc, char *argv[],
790 ulong addr,
791 ulong *len_ptr,
792 int verify)
793{
794 DECLARE_GLOBAL_DATA_PTR;
795
796 image_header_t *hdr = &header;
797
798 void (*loader)(bd_t *, image_header_t *, char *, char *);
799 image_header_t *img_addr;
800 char *consdev;
801 char *cmdline;
802
803
804 /*
805 * Booting a (NetBSD) kernel image
806 *
807 * This process is pretty similar to a standalone application:
808 * The (first part of an multi-) image must be a stage-2 loader,
809 * which in turn is responsible for loading & invoking the actual
810 * kernel. The only differences are the parameters being passed:
811 * besides the board info strucure, the loader expects a command
812 * line, the name of the console device, and (optionally) the
813 * address of the original image header.
814 */
815
816 img_addr = 0;
817 if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
818 img_addr = (image_header_t *) addr;
819
820
821 consdev = "";
822#if defined (CONFIG_8xx_CONS_SMC1)
823 consdev = "smc1";
824#elif defined (CONFIG_8xx_CONS_SMC2)
825 consdev = "smc2";
826#elif defined (CONFIG_8xx_CONS_SCC2)
827 consdev = "scc2";
828#elif defined (CONFIG_8xx_CONS_SCC3)
829 consdev = "scc3";
830#endif
831
832 if (argc > 2) {
833 ulong len;
834 int i;
835
836 for (i=2, len=0 ; i<argc ; i+=1)
837 len += strlen (argv[i]) + 1;
838 cmdline = malloc (len);
839
840 for (i=2, len=0 ; i<argc ; i+=1) {
841 if (i > 2)
842 cmdline[len++] = ' ';
843 strcpy (&cmdline[len], argv[i]);
844 len += strlen (argv[i]);
845 }
846 } else if ((cmdline = getenv("bootargs")) == NULL) {
847 cmdline = "";
848 }
849
850 loader = (void (*)(bd_t *, image_header_t *, char *, char *)) hdr->ih_ep;
851
852 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
853 (ulong)loader);
854
855 SHOW_BOOT_PROGRESS (15);
856
857 /*
858 * NetBSD Stage-2 Loader Parameters:
859 * r3: ptr to board info data
860 * r4: image address
861 * r5: console device
862 * r6: boot args string
863 */
864 (*loader) (gd->bd, img_addr, consdev, cmdline);
865}
wdenk0893c472003-05-20 14:25:27 +0000866
867#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
868
869/* Function that returns a character from the environment */
870extern uchar (*env_get_char)(int);
871
872static void
873do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
874 int argc, char *argv[],
875 ulong addr,
876 ulong *len_ptr,
877 int verify)
878{
879 DECLARE_GLOBAL_DATA_PTR;
880 ulong top;
881 char *s, *cmdline;
882 char **fwenv, **ss;
883 int i, j, nxt, len, envno, envsz;
884 bd_t *kbd;
885 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
886 image_header_t *hdr = &header;
887
888 /*
889 * Booting an ARTOS kernel image + application
890 */
891
892 /* this used to be the top of memory, but was wrong... */
893#ifdef CONFIG_PPC
894 /* get stack pointer */
895 asm volatile ("mr %0,1" : "=r"(top) );
896#endif
897 debug ("## Current stack ends at 0x%08lX ", top);
898
899 top -= 2048; /* just to be sure */
900 if (top > CFG_BOOTMAPSZ)
901 top = CFG_BOOTMAPSZ;
902 top &= ~0xF;
903
904 debug ("=> set upper limit to 0x%08lX\n", top);
905
906 /* first check the artos specific boot args, then the linux args*/
907 if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
908 s = "";
909
910 /* get length of cmdline, and place it */
911 len = strlen(s);
912 top = (top - (len + 1)) & ~0xF;
913 cmdline = (char *)top;
914 debug ("## cmdline at 0x%08lX ", top);
915 strcpy(cmdline, s);
916
917 /* copy bdinfo */
918 top = (top - sizeof(bd_t)) & ~0xF;
919 debug ("## bd at 0x%08lX ", top);
920 kbd = (bd_t *)top;
921 memcpy(kbd, gd->bd, sizeof(bd_t));
922
923 /* first find number of env entries, and their size */
924 envno = 0;
925 envsz = 0;
926 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
927 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
928 ;
929 envno++;
930 envsz += (nxt - i) + 1; /* plus trailing zero */
931 }
932 envno++; /* plus the terminating zero */
933 debug ("## %u envvars total size %u ", envno, envsz);
934
935 top = (top - sizeof(char **)*envno) & ~0xF;
936 fwenv = (char **)top;
937 debug ("## fwenv at 0x%08lX ", top);
938
939 top = (top - envsz) & ~0xF;
940 s = (char *)top;
941 ss = fwenv;
942
943 /* now copy them */
944 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
945 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
946 ;
947 *ss++ = s;
948 for (j = i; j < nxt; ++j)
949 *s++ = env_get_char(j);
950 *s++ = '\0';
951 }
952 *ss++ = NULL; /* terminate */
953
954 entry = (void (*)(bd_t *, char *, char **, ulong))ntohl(hdr->ih_ep);
955 (*entry)(kbd, cmdline, fwenv, top);
956}
957#endif
958
wdenk47d1a6e2002-11-03 00:01:44 +0000959
960#if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
961int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
962{
963 int rcode = 0;
964#ifndef CFG_HUSH_PARSER
965 if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
966#else
967 if (parse_string_outer(getenv("bootcmd"),
968 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
969#endif
970 return rcode;
971}
wdenk57b2d802003-06-27 21:31:46 +0000972
wdenkf287a242003-07-01 21:06:45 +0000973U_BOOT_CMD(
974 boot, 1, 1, do_bootd,
wdenk3086a972003-06-28 23:11:04 +0000975 "boot - boot default, i.e., run 'bootcmd'\n",
976 NULL
977);
978
979/* keep old command name "bootd" for backward compatibility */
wdenkf287a242003-07-01 21:06:45 +0000980U_BOOT_CMD(
981 bootd, 1, 1, do_bootd,
wdenk57b2d802003-06-27 21:31:46 +0000982 "bootd - boot default, i.e., run 'bootcmd'\n",
983 NULL
984);
985
wdenk47d1a6e2002-11-03 00:01:44 +0000986#endif
987
988#if (CONFIG_COMMANDS & CFG_CMD_IMI)
989int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
990{
991 int arg;
992 ulong addr;
993 int rcode=0;
994
995 if (argc < 2) {
996 return image_info (load_addr);
997 }
998
999 for (arg=1; arg <argc; ++arg) {
1000 addr = simple_strtoul(argv[arg], NULL, 16);
1001 if (image_info (addr) != 0) rcode = 1;
1002 }
1003 return rcode;
1004}
1005
1006static int image_info (ulong addr)
1007{
1008 ulong data, len, checksum;
1009 image_header_t *hdr = &header;
1010
1011 printf ("\n## Checking Image at %08lx ...\n", addr);
1012
1013 /* Copy header so we can blank CRC field for re-calculation */
1014 memmove (&header, (char *)addr, sizeof(image_header_t));
1015
1016 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
1017 printf (" Bad Magic Number\n");
1018 return 1;
1019 }
1020
1021 data = (ulong)&header;
1022 len = sizeof(image_header_t);
1023
1024 checksum = ntohl(hdr->ih_hcrc);
1025 hdr->ih_hcrc = 0;
1026
1027 if (crc32 (0, (char *)data, len) != checksum) {
1028 printf (" Bad Header Checksum\n");
1029 return 1;
1030 }
1031
1032 /* for multi-file images we need the data part, too */
1033 print_image_hdr ((image_header_t *)addr);
1034
1035 data = addr + sizeof(image_header_t);
1036 len = ntohl(hdr->ih_size);
1037
1038 printf (" Verifying Checksum ... ");
1039 if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
1040 printf (" Bad Data CRC\n");
1041 return 1;
1042 }
1043 printf ("OK\n");
1044 return 0;
1045}
wdenkf287a242003-07-01 21:06:45 +00001046
1047U_BOOT_CMD(
1048 iminfo, CFG_MAXARGS, 1, do_iminfo,
wdenk57b2d802003-06-27 21:31:46 +00001049 "iminfo - print header information for application image\n",
1050 "addr [addr ...]\n"
1051 " - print header information for application image starting at\n"
1052 " address 'addr' in memory; this includes verification of the\n"
1053 " image contents (magic number, header and payload checksums)\n"
1054);
1055
wdenk47d1a6e2002-11-03 00:01:44 +00001056#endif /* CFG_CMD_IMI */
1057
wdenk874ac262003-07-24 23:38:38 +00001058#if (CONFIG_COMMANDS & CFG_CMD_IMLS)
1059/*-----------------------------------------------------------------------
1060 * List all images found in flash.
1061 */
1062int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1063{
1064 flash_info_t *info;
1065 int i, j;
1066 image_header_t *hdr;
wdenkc6abb7e2003-11-17 21:14:37 +00001067 ulong data, len, checksum;
wdenk874ac262003-07-24 23:38:38 +00001068
1069 for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
1070 if (info->flash_id == FLASH_UNKNOWN)
1071 goto next_bank;
1072 for (j=0; j<CFG_MAX_FLASH_SECT; ++j) {
1073
1074 if (!(hdr=(image_header_t *)info->start[j]) ||
1075 (ntohl(hdr->ih_magic) != IH_MAGIC))
1076 goto next_sector;
1077
1078 /* Copy header so we can blank CRC field for re-calculation */
1079 memmove (&header, (char *)hdr, sizeof(image_header_t));
1080
1081 checksum = ntohl(header.ih_hcrc);
1082 header.ih_hcrc = 0;
1083
1084 if (crc32 (0, (char *)&header, sizeof(image_header_t))
1085 != checksum)
1086 goto next_sector;
1087
1088 printf ("Image at %08lX:\n", (ulong)hdr);
1089 print_image_hdr( hdr );
wdenkc6abb7e2003-11-17 21:14:37 +00001090
1091 data = (ulong)hdr + sizeof(image_header_t);
1092 len = ntohl(hdr->ih_size);
1093
1094 printf (" Verifying Checksum ... ");
1095 if (crc32 (0, (char *)data, len) != ntohl(hdr->ih_dcrc)) {
1096 printf (" Bad Data CRC\n");
1097 }
1098 printf ("OK\n");
wdenk0a658552003-08-05 17:43:17 +00001099next_sector: ;
wdenk874ac262003-07-24 23:38:38 +00001100 }
wdenk0a658552003-08-05 17:43:17 +00001101next_bank: ;
wdenk874ac262003-07-24 23:38:38 +00001102 }
1103
1104 return (0);
1105}
1106
1107U_BOOT_CMD(
1108 imls, 1, 1, do_imls,
1109 "imls - list all images found in flash\n",
1110 "\n"
1111 " - Prints information about all images found at sector\n"
1112 " boundaries in flash.\n"
1113);
1114#endif /* CFG_CMD_IMLS */
1115
wdenk47d1a6e2002-11-03 00:01:44 +00001116void
1117print_image_hdr (image_header_t *hdr)
1118{
1119#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
1120 time_t timestamp = (time_t)ntohl(hdr->ih_time);
1121 struct rtc_time tm;
1122#endif
1123
1124 printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
1125#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
1126 to_tm (timestamp, &tm);
1127 printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
1128 tm.tm_year, tm.tm_mon, tm.tm_mday,
1129 tm.tm_hour, tm.tm_min, tm.tm_sec);
1130#endif /* CFG_CMD_DATE, CONFIG_TIMESTAMP */
1131 printf (" Image Type: "); print_type(hdr); printf ("\n");
1132 printf (" Data Size: %d Bytes = ", ntohl(hdr->ih_size));
1133 print_size (ntohl(hdr->ih_size), "\n");
1134 printf (" Load Address: %08x\n", ntohl(hdr->ih_load));
1135 printf (" Entry Point: %08x\n", ntohl(hdr->ih_ep));
1136
1137 if (hdr->ih_type == IH_TYPE_MULTI) {
1138 int i;
1139 ulong len;
1140 ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
1141
1142 printf (" Contents:\n");
1143 for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
1144 printf (" Image %d: %8ld Bytes = ", i, len);
1145 print_size (len, "\n");
1146 }
1147 }
1148}
1149
1150
1151static void
1152print_type (image_header_t *hdr)
1153{
1154 char *os, *arch, *type, *comp;
1155
1156 switch (hdr->ih_os) {
1157 case IH_OS_INVALID: os = "Invalid OS"; break;
1158 case IH_OS_NETBSD: os = "NetBSD"; break;
1159 case IH_OS_LINUX: os = "Linux"; break;
1160 case IH_OS_VXWORKS: os = "VxWorks"; break;
1161 case IH_OS_QNX: os = "QNX"; break;
1162 case IH_OS_U_BOOT: os = "U-Boot"; break;
wdenk92bbe3f2003-04-20 14:04:18 +00001163 case IH_OS_RTEMS: os = "RTEMS"; break;
wdenk0893c472003-05-20 14:25:27 +00001164#ifdef CONFIG_ARTOS
1165 case IH_OS_ARTOS: os = "ARTOS"; break;
1166#endif
wdenke58b0dc2003-07-27 00:21:01 +00001167#ifdef CONFIG_LYNXKDI
1168 case IH_OS_LYNXOS: os = "LynxOS"; break;
1169#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001170 default: os = "Unknown OS"; break;
1171 }
1172
1173 switch (hdr->ih_arch) {
1174 case IH_CPU_INVALID: arch = "Invalid CPU"; break;
1175 case IH_CPU_ALPHA: arch = "Alpha"; break;
1176 case IH_CPU_ARM: arch = "ARM"; break;
1177 case IH_CPU_I386: arch = "Intel x86"; break;
1178 case IH_CPU_IA64: arch = "IA64"; break;
1179 case IH_CPU_MIPS: arch = "MIPS"; break;
1180 case IH_CPU_MIPS64: arch = "MIPS 64 Bit"; break;
1181 case IH_CPU_PPC: arch = "PowerPC"; break;
1182 case IH_CPU_S390: arch = "IBM S390"; break;
1183 case IH_CPU_SH: arch = "SuperH"; break;
1184 case IH_CPU_SPARC: arch = "SPARC"; break;
1185 case IH_CPU_SPARC64: arch = "SPARC 64 Bit"; break;
wdenkdccbda02003-07-14 22:13:32 +00001186 case IH_CPU_M68K: arch = "M68K"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001187 default: arch = "Unknown Architecture"; break;
1188 }
1189
1190 switch (hdr->ih_type) {
1191 case IH_TYPE_INVALID: type = "Invalid Image"; break;
1192 case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
1193 case IH_TYPE_KERNEL: type = "Kernel Image"; break;
1194 case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
1195 case IH_TYPE_MULTI: type = "Multi-File Image"; break;
1196 case IH_TYPE_FIRMWARE: type = "Firmware"; break;
1197 case IH_TYPE_SCRIPT: type = "Script"; break;
1198 default: type = "Unknown Image"; break;
1199 }
1200
1201 switch (hdr->ih_comp) {
1202 case IH_COMP_NONE: comp = "uncompressed"; break;
1203 case IH_COMP_GZIP: comp = "gzip compressed"; break;
1204 case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
1205 default: comp = "unknown compression"; break;
1206 }
1207
1208 printf ("%s %s %s (%s)", arch, os, type, comp);
1209}
1210
1211#define ZALLOC_ALIGNMENT 16
1212
1213static void *zalloc(void *x, unsigned items, unsigned size)
1214{
1215 void *p;
1216
1217 size *= items;
1218 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
1219
1220 p = malloc (size);
1221
1222 return (p);
1223}
1224
1225static void zfree(void *x, void *addr, unsigned nb)
1226{
1227 free (addr);
1228}
1229
1230#define HEAD_CRC 2
1231#define EXTRA_FIELD 4
1232#define ORIG_NAME 8
1233#define COMMENT 0x10
1234#define RESERVED 0xe0
1235
1236#define DEFLATED 8
1237
1238int gunzip(void *dst, int dstlen, unsigned char *src, int *lenp)
1239{
1240 z_stream s;
1241 int r, i, flags;
1242
1243 /* skip header */
1244 i = 10;
1245 flags = src[3];
1246 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
1247 printf ("Error: Bad gzipped data\n");
1248 return (-1);
1249 }
1250 if ((flags & EXTRA_FIELD) != 0)
1251 i = 12 + src[10] + (src[11] << 8);
1252 if ((flags & ORIG_NAME) != 0)
1253 while (src[i++] != 0)
1254 ;
1255 if ((flags & COMMENT) != 0)
1256 while (src[i++] != 0)
1257 ;
1258 if ((flags & HEAD_CRC) != 0)
1259 i += 2;
1260 if (i >= *lenp) {
1261 printf ("Error: gunzip out of data in header\n");
1262 return (-1);
1263 }
1264
1265 s.zalloc = zalloc;
1266 s.zfree = zfree;
1267#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
1268 s.outcb = (cb_func)WATCHDOG_RESET;
1269#else
1270 s.outcb = Z_NULL;
1271#endif /* CONFIG_HW_WATCHDOG */
1272
1273 r = inflateInit2(&s, -MAX_WBITS);
1274 if (r != Z_OK) {
1275 printf ("Error: inflateInit2() returned %d\n", r);
1276 return (-1);
1277 }
1278 s.next_in = src + i;
1279 s.avail_in = *lenp - i;
1280 s.next_out = dst;
1281 s.avail_out = dstlen;
1282 r = inflate(&s, Z_FINISH);
1283 if (r != Z_OK && r != Z_STREAM_END) {
1284 printf ("Error: inflate() returned %d\n", r);
1285 return (-1);
1286 }
1287 *lenp = s.next_out - (unsigned char *) dst;
1288 inflateEnd(&s);
1289
1290 return (0);
1291}
wdenk710e3502003-08-29 20:57:53 +00001292
1293#ifdef CONFIG_BZIP2
1294void bz_internal_error(int errcode)
1295{
1296 printf ("BZIP2 internal error %d\n", errcode);
1297}
1298#endif /* CONFIG_BZIP2 */
wdenk47d1a6e2002-11-03 00:01:44 +00001299
wdenk92bbe3f2003-04-20 14:04:18 +00001300static void
1301do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1302 ulong addr, ulong *len_ptr, int verify)
1303{
1304 DECLARE_GLOBAL_DATA_PTR;
1305 image_header_t *hdr = &header;
1306 void (*entry_point)(bd_t *);
1307
1308 entry_point = (void (*)(bd_t *)) hdr->ih_ep;
1309
1310 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
1311 (ulong)entry_point);
1312
1313 SHOW_BOOT_PROGRESS (15);
1314
1315 /*
1316 * RTEMS Parameters:
1317 * r3: ptr to board info data
1318 */
1319
1320 (*entry_point ) ( gd->bd );
1321}
1322
wdenk47d1a6e2002-11-03 00:01:44 +00001323#if (CONFIG_COMMANDS & CFG_CMD_ELF)
1324static void
1325do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1326 ulong addr, ulong *len_ptr, int verify)
1327{
1328 image_header_t *hdr = &header;
1329 char str[80];
1330
1331 sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
1332 setenv("loadaddr", str);
1333 do_bootvx(cmdtp, 0, 0, NULL);
1334}
1335
1336static void
1337do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1338 ulong addr, ulong *len_ptr, int verify)
1339{
1340 image_header_t *hdr = &header;
1341 char *local_args[2];
1342 char str[16];
1343
1344 sprintf(str, "%x", hdr->ih_ep); /* write entry-point into string */
1345 local_args[0] = argv[0];
1346 local_args[1] = str; /* and provide it via the arguments */
1347 do_bootelf(cmdtp, 0, 2, local_args);
1348}
1349#endif /* CFG_CMD_ELF */
wdenke58b0dc2003-07-27 00:21:01 +00001350
1351#ifdef CONFIG_LYNXKDI
1352static void
1353do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
1354 int argc, char *argv[],
1355 ulong addr,
1356 ulong *len_ptr,
1357 int verify)
1358{
1359 lynxkdi_boot( &header );
1360}
1361
1362#endif /* CONFIG_LYNXKDI */