blob: 2721216bf35eba2418efa60874703c7214eb3e05 [file] [log] [blame]
wdenk47d1a6e2002-11-03 00:01:44 +00001/*
Stefan Roese15940c92006-03-13 11:16:36 +01002 * (C) Copyright 2000-2006
wdenk47d1a6e2002-11-03 00:01:44 +00003 * 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
Gerald Van Baren213bf8c2007-03-31 12:23:51 -040037#if defined(CONFIG_OF_LIBFDT)
38#include <fdt.h>
39#include <libfdt.h>
40#endif
Gerald Van Barenaea03c42007-03-31 14:30:53 -040041#if defined(CONFIG_OF_FLAT_TREE)
Wolfgang Denkf57f70a2005-10-13 01:45:54 +020042#include <ft_build.h>
43#endif
44
Wolfgang Denkd87080b2006-03-31 18:32:53 +020045DECLARE_GLOBAL_DATA_PTR;
46
wdenk8bde7f72003-06-27 21:31:46 +000047 /*cmd_boot.c*/
48 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
49
wdenk47d1a6e2002-11-03 00:01:44 +000050#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
51#include <rtc.h>
52#endif
53
54#ifdef CFG_HUSH_PARSER
55#include <hush.h>
56#endif
57
58#ifdef CONFIG_SHOW_BOOT_PROGRESS
59# include <status_led.h>
60# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
61#else
62# define SHOW_BOOT_PROGRESS(arg)
63#endif
64
65#ifdef CFG_INIT_RAM_LOCK
66#include <asm/cache.h>
67#endif
68
wdenk228f29a2002-12-08 09:53:23 +000069#ifdef CONFIG_LOGBUFFER
70#include <logbuff.h>
71#endif
72
wdenk2abbe072003-06-16 23:50:08 +000073#ifdef CONFIG_HAS_DATAFLASH
74#include <dataflash.h>
75#endif
76
wdenk47d1a6e2002-11-03 00:01:44 +000077/*
78 * Some systems (for example LWMON) have very short watchdog periods;
79 * we must make sure to split long operations like memmove() or
80 * crc32() into reasonable chunks.
81 */
82#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
83# define CHUNKSZ (64 * 1024)
84#endif
85
wdenkeedcd072004-09-08 22:03:11 +000086int gunzip (void *, int, unsigned char *, unsigned long *);
wdenk47d1a6e2002-11-03 00:01:44 +000087
88static void *zalloc(void *, unsigned, unsigned);
89static void zfree(void *, void *, unsigned);
90
91#if (CONFIG_COMMANDS & CFG_CMD_IMI)
92static int image_info (unsigned long addr);
93#endif
wdenk27b207f2003-07-24 23:38:38 +000094
95#if (CONFIG_COMMANDS & CFG_CMD_IMLS)
96#include <flash.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020097extern flash_info_t flash_info[]; /* info for FLASH chips */
wdenk27b207f2003-07-24 23:38:38 +000098static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
99#endif
100
wdenk47d1a6e2002-11-03 00:01:44 +0000101static void print_type (image_header_t *hdr);
102
wdenk2262cfe2002-11-18 00:14:45 +0000103#ifdef __I386__
104image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
105#endif
106
wdenk47d1a6e2002-11-03 00:01:44 +0000107/*
108 * Continue booting an OS image; caller already has:
109 * - copied image header to global variable `header'
110 * - checked header magic number, checksums (both header & image),
111 * - verified image architecture (PPC) and type (KERNEL or MULTI),
112 * - loaded (first part of) image to header load address,
113 * - disabled interrupts.
114 */
115typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
116 int argc, char *argv[],
117 ulong addr, /* of image to boot */
118 ulong *len_ptr, /* multi-file image length table */
119 int verify); /* getenv("verify")[0] != 'n' */
120
wdenk8bde7f72003-06-27 21:31:46 +0000121#ifdef DEBUG
122extern int do_bdinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
123#endif
124
wdenk2262cfe2002-11-18 00:14:45 +0000125#ifdef CONFIG_PPC
wdenk47d1a6e2002-11-03 00:01:44 +0000126static boot_os_Fcn do_bootm_linux;
127#else
128extern boot_os_Fcn do_bootm_linux;
129#endif
wdenkf72da342003-10-10 10:05:42 +0000130#ifdef CONFIG_SILENT_CONSOLE
131static void fixup_silent_linux (void);
132#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000133static boot_os_Fcn do_bootm_netbsd;
wdenkd791b1d2003-04-20 14:04:18 +0000134static boot_os_Fcn do_bootm_rtems;
wdenk47d1a6e2002-11-03 00:01:44 +0000135#if (CONFIG_COMMANDS & CFG_CMD_ELF)
136static boot_os_Fcn do_bootm_vxworks;
137static boot_os_Fcn do_bootm_qnxelf;
138int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
139int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
140#endif /* CFG_CMD_ELF */
wdenk7f70e852003-05-20 14:25:27 +0000141#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
142static boot_os_Fcn do_bootm_artos;
143#endif
wdenk1f4bb372003-07-27 00:21:01 +0000144#ifdef CONFIG_LYNXKDI
145static boot_os_Fcn do_bootm_lynxkdi;
146extern void lynxkdi_boot( image_header_t * );
147#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000148
Stefan Roese15940c92006-03-13 11:16:36 +0100149#ifndef CFG_BOOTM_LEN
150#define CFG_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
151#endif
152
wdenk47d1a6e2002-11-03 00:01:44 +0000153image_header_t header;
154
155ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
156
157int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
158{
159 ulong iflag;
160 ulong addr;
161 ulong data, len, checksum;
162 ulong *len_ptr;
Stefan Roese15940c92006-03-13 11:16:36 +0100163 uint unc_len = CFG_BOOTM_LEN;
wdenk47d1a6e2002-11-03 00:01:44 +0000164 int i, verify;
165 char *name, *s;
wdenkb13fb012003-10-30 21:49:38 +0000166 int (*appl)(int, char *[]);
wdenk47d1a6e2002-11-03 00:01:44 +0000167 image_header_t *hdr = &header;
168
169 s = getenv ("verify");
170 verify = (s && (*s == 'n')) ? 0 : 1;
171
172 if (argc < 2) {
173 addr = load_addr;
174 } else {
175 addr = simple_strtoul(argv[1], NULL, 16);
176 }
177
178 SHOW_BOOT_PROGRESS (1);
179 printf ("## Booting image at %08lx ...\n", addr);
180
181 /* Copy header so we can blank CRC field for re-calculation */
wdenk2abbe072003-06-16 23:50:08 +0000182#ifdef CONFIG_HAS_DATAFLASH
183 if (addr_dataflash(addr)){
184 read_dataflash(addr, sizeof(image_header_t), (char *)&header);
185 } else
186#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000187 memmove (&header, (char *)addr, sizeof(image_header_t));
188
189 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
wdenk2262cfe2002-11-18 00:14:45 +0000190#ifdef __I386__ /* correct image format not implemented yet - fake it */
191 if (fake_header(hdr, (void*)addr, -1) != NULL) {
192 /* to compensate for the addition below */
193 addr -= sizeof(image_header_t);
194 /* turnof verify,
195 * fake_header() does not fake the data crc
196 */
197 verify = 0;
198 } else
199#endif /* __I386__ */
200 {
wdenk4b9206e2004-03-23 22:14:11 +0000201 puts ("Bad Magic Number\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000202 SHOW_BOOT_PROGRESS (-1);
203 return 1;
wdenk2262cfe2002-11-18 00:14:45 +0000204 }
wdenk47d1a6e2002-11-03 00:01:44 +0000205 }
206 SHOW_BOOT_PROGRESS (2);
207
208 data = (ulong)&header;
209 len = sizeof(image_header_t);
210
211 checksum = ntohl(hdr->ih_hcrc);
212 hdr->ih_hcrc = 0;
213
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200214 if (crc32 (0, (uchar *)data, len) != checksum) {
wdenk4b9206e2004-03-23 22:14:11 +0000215 puts ("Bad Header Checksum\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000216 SHOW_BOOT_PROGRESS (-2);
217 return 1;
218 }
219 SHOW_BOOT_PROGRESS (3);
220
Wolfgang Denkbccae902005-10-06 01:50:50 +0200221#ifdef CONFIG_HAS_DATAFLASH
222 if (addr_dataflash(addr)){
223 len = ntohl(hdr->ih_size) + sizeof(image_header_t);
224 read_dataflash(addr, len, (char *)CFG_LOAD_ADDR);
225 addr = CFG_LOAD_ADDR;
226 }
227#endif
228
229
wdenk47d1a6e2002-11-03 00:01:44 +0000230 /* for multi-file images we need the data part, too */
wdenka57a4962003-10-26 22:52:58 +0000231 print_image_hdr ((image_header_t *)addr);
wdenk47d1a6e2002-11-03 00:01:44 +0000232
233 data = addr + sizeof(image_header_t);
234 len = ntohl(hdr->ih_size);
235
236 if (verify) {
wdenk4b9206e2004-03-23 22:14:11 +0000237 puts (" Verifying Checksum ... ");
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200238 if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000239 printf ("Bad Data CRC\n");
240 SHOW_BOOT_PROGRESS (-3);
241 return 1;
242 }
wdenk4b9206e2004-03-23 22:14:11 +0000243 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000244 }
245 SHOW_BOOT_PROGRESS (4);
246
247 len_ptr = (ulong *)data;
248
Wolfgang Denk44ba4642007-03-22 00:13:12 +0100249#if defined(__ARM__)
wdenk2262cfe2002-11-18 00:14:45 +0000250 if (hdr->ih_arch != IH_CPU_ARM)
Wolfgang Denk44ba4642007-03-22 00:13:12 +0100251#elif defined(__avr32__)
252 if (hdr->ih_arch != IH_CPU_AVR32)
253#elif defined(__bfin__)
254 if (hdr->ih_arch != IH_CPU_BLACKFIN)
wdenk2262cfe2002-11-18 00:14:45 +0000255#elif defined(__I386__)
256 if (hdr->ih_arch != IH_CPU_I386)
wdenk4e5ca3e2003-12-08 01:34:36 +0000257#elif defined(__M68K__)
258 if (hdr->ih_arch != IH_CPU_M68K)
wdenk507bbe32004-04-18 21:13:41 +0000259#elif defined(__microblaze__)
260 if (hdr->ih_arch != IH_CPU_MICROBLAZE)
Wolfgang Denk44ba4642007-03-22 00:13:12 +0100261#elif defined(__mips__)
262 if (hdr->ih_arch != IH_CPU_MIPS)
263#elif defined(__nios__)
264 if (hdr->ih_arch != IH_CPU_NIOS)
wdenk5c952cf2004-10-10 21:27:30 +0000265#elif defined(__nios2__)
266 if (hdr->ih_arch != IH_CPU_NIOS2)
Wolfgang Denk44ba4642007-03-22 00:13:12 +0100267#elif defined(__PPC__)
268 if (hdr->ih_arch != IH_CPU_PPC)
wdenk2262cfe2002-11-18 00:14:45 +0000269#else
270# error Unknown CPU type
271#endif
272 {
273 printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch);
wdenk47d1a6e2002-11-03 00:01:44 +0000274 SHOW_BOOT_PROGRESS (-4);
275 return 1;
276 }
277 SHOW_BOOT_PROGRESS (5);
278
279 switch (hdr->ih_type) {
wdenkb13fb012003-10-30 21:49:38 +0000280 case IH_TYPE_STANDALONE:
281 name = "Standalone Application";
282 /* A second argument overwrites the load address */
283 if (argc > 2) {
wdenkb2532ef2005-06-20 10:17:34 +0000284 hdr->ih_load = htonl(simple_strtoul(argv[2], NULL, 16));
wdenkb13fb012003-10-30 21:49:38 +0000285 }
286 break;
287 case IH_TYPE_KERNEL:
288 name = "Kernel Image";
289 break;
wdenkd4ca31c2004-01-02 14:00:00 +0000290 case IH_TYPE_MULTI:
wdenkb13fb012003-10-30 21:49:38 +0000291 name = "Multi-File Image";
292 len = ntohl(len_ptr[0]);
293 /* OS kernel is always the first image */
294 data += 8; /* kernel_len + terminator */
295 for (i=1; len_ptr[i]; ++i)
296 data += 4;
297 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000298 default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
299 SHOW_BOOT_PROGRESS (-5);
300 return 1;
301 }
302 SHOW_BOOT_PROGRESS (6);
303
304 /*
305 * We have reached the point of no return: we are going to
306 * overwrite all exception vector code, so we cannot easily
307 * recover from any failures any more...
308 */
309
310 iflag = disable_interrupts();
311
wdenkc7de8292002-11-19 11:04:11 +0000312#ifdef CONFIG_AMIGAONEG3SE
313 /*
wdenk8bde7f72003-06-27 21:31:46 +0000314 * We've possible left the caches enabled during
wdenkc7de8292002-11-19 11:04:11 +0000315 * bios emulation, so turn them off again
316 */
317 icache_disable();
318 invalidate_l1_instruction_cache();
319 flush_data_cache();
320 dcache_disable();
321#endif
322
wdenk47d1a6e2002-11-03 00:01:44 +0000323 switch (hdr->ih_comp) {
324 case IH_COMP_NONE:
wdenk2262cfe2002-11-18 00:14:45 +0000325 if(ntohl(hdr->ih_load) == addr) {
wdenk47d1a6e2002-11-03 00:01:44 +0000326 printf (" XIP %s ... ", name);
327 } else {
328#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
329 size_t l = len;
330 void *to = (void *)ntohl(hdr->ih_load);
331 void *from = (void *)data;
332
333 printf (" Loading %s ... ", name);
334
335 while (l > 0) {
336 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
337 WATCHDOG_RESET();
338 memmove (to, from, tail);
339 to += tail;
340 from += tail;
341 l -= tail;
342 }
343#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
344 memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
345#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
346 }
347 break;
348 case IH_COMP_GZIP:
349 printf (" Uncompressing %s ... ", name);
wdenkc29fdfc2003-08-29 20:57:53 +0000350 if (gunzip ((void *)ntohl(hdr->ih_load), unc_len,
wdenkeedcd072004-09-08 22:03:11 +0000351 (uchar *)data, &len) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000352 puts ("GUNZIP ERROR - must RESET board to recover\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000353 SHOW_BOOT_PROGRESS (-6);
354 do_reset (cmdtp, flag, argc, argv);
355 }
356 break;
wdenkc29fdfc2003-08-29 20:57:53 +0000357#ifdef CONFIG_BZIP2
358 case IH_COMP_BZIP2:
359 printf (" Uncompressing %s ... ", name);
wdenk5653fc32004-02-08 22:55:38 +0000360 /*
361 * If we've got less than 4 MB of malloc() space,
362 * use slower decompression algorithm which requires
363 * at most 2300 KB of memory.
364 */
wdenkc29fdfc2003-08-29 20:57:53 +0000365 i = BZ2_bzBuffToBuffDecompress ((char*)ntohl(hdr->ih_load),
wdenk5653fc32004-02-08 22:55:38 +0000366 &unc_len, (char *)data, len,
367 CFG_MALLOC_LEN < (4096 * 1024), 0);
wdenkc29fdfc2003-08-29 20:57:53 +0000368 if (i != BZ_OK) {
369 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
370 SHOW_BOOT_PROGRESS (-6);
371 udelay(100000);
372 do_reset (cmdtp, flag, argc, argv);
373 }
374 break;
375#endif /* CONFIG_BZIP2 */
wdenk47d1a6e2002-11-03 00:01:44 +0000376 default:
377 if (iflag)
378 enable_interrupts();
379 printf ("Unimplemented compression type %d\n", hdr->ih_comp);
380 SHOW_BOOT_PROGRESS (-7);
381 return 1;
382 }
wdenk4b9206e2004-03-23 22:14:11 +0000383 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000384 SHOW_BOOT_PROGRESS (7);
385
386 switch (hdr->ih_type) {
387 case IH_TYPE_STANDALONE:
wdenk47d1a6e2002-11-03 00:01:44 +0000388 if (iflag)
389 enable_interrupts();
390
wdenk4a6fd342003-04-12 23:38:12 +0000391 /* load (and uncompress), but don't start if "autostart"
392 * is set to "no"
393 */
wdenk4a551702003-10-08 23:26:14 +0000394 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
395 char buf[32];
396 sprintf(buf, "%lX", len);
397 setenv("filesize", buf);
wdenk4a6fd342003-04-12 23:38:12 +0000398 return 0;
wdenk4a551702003-10-08 23:26:14 +0000399 }
wdenkb13fb012003-10-30 21:49:38 +0000400 appl = (int (*)(int, char *[]))ntohl(hdr->ih_ep);
401 (*appl)(argc-1, &argv[1]);
wdenk4a6fd342003-04-12 23:38:12 +0000402 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000403 case IH_TYPE_KERNEL:
404 case IH_TYPE_MULTI:
405 /* handled below */
406 break;
407 default:
408 if (iflag)
409 enable_interrupts();
410 printf ("Can't boot image type %d\n", hdr->ih_type);
411 SHOW_BOOT_PROGRESS (-8);
412 return 1;
413 }
414 SHOW_BOOT_PROGRESS (8);
415
416 switch (hdr->ih_os) {
417 default: /* handled by (original) Linux case */
418 case IH_OS_LINUX:
wdenkf72da342003-10-10 10:05:42 +0000419#ifdef CONFIG_SILENT_CONSOLE
420 fixup_silent_linux();
421#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000422 do_bootm_linux (cmdtp, flag, argc, argv,
423 addr, len_ptr, verify);
424 break;
425 case IH_OS_NETBSD:
426 do_bootm_netbsd (cmdtp, flag, argc, argv,
427 addr, len_ptr, verify);
428 break;
wdenk8bde7f72003-06-27 21:31:46 +0000429
wdenk1f4bb372003-07-27 00:21:01 +0000430#ifdef CONFIG_LYNXKDI
431 case IH_OS_LYNXOS:
432 do_bootm_lynxkdi (cmdtp, flag, argc, argv,
433 addr, len_ptr, verify);
434 break;
435#endif
436
wdenkd791b1d2003-04-20 14:04:18 +0000437 case IH_OS_RTEMS:
438 do_bootm_rtems (cmdtp, flag, argc, argv,
439 addr, len_ptr, verify);
440 break;
wdenk8bde7f72003-06-27 21:31:46 +0000441
wdenk47d1a6e2002-11-03 00:01:44 +0000442#if (CONFIG_COMMANDS & CFG_CMD_ELF)
443 case IH_OS_VXWORKS:
444 do_bootm_vxworks (cmdtp, flag, argc, argv,
445 addr, len_ptr, verify);
446 break;
447 case IH_OS_QNX:
448 do_bootm_qnxelf (cmdtp, flag, argc, argv,
449 addr, len_ptr, verify);
450 break;
451#endif /* CFG_CMD_ELF */
wdenk7f70e852003-05-20 14:25:27 +0000452#ifdef CONFIG_ARTOS
453 case IH_OS_ARTOS:
454 do_bootm_artos (cmdtp, flag, argc, argv,
455 addr, len_ptr, verify);
456 break;
457#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000458 }
459
460 SHOW_BOOT_PROGRESS (-9);
461#ifdef DEBUG
wdenk4b9206e2004-03-23 22:14:11 +0000462 puts ("\n## Control returned to monitor - resetting...\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000463 do_reset (cmdtp, flag, argc, argv);
464#endif
465 return 1;
466}
467
wdenk0d498392003-07-01 21:06:45 +0000468U_BOOT_CMD(
469 bootm, CFG_MAXARGS, 1, do_bootm,
wdenk8bde7f72003-06-27 21:31:46 +0000470 "bootm - boot application image from memory\n",
471 "[addr [arg ...]]\n - boot application image stored in memory\n"
wdenk9d5028c2004-11-21 00:06:33 +0000472 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
473 "\t'arg' can be the address of an initrd image\n"
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400474#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500475 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
476 "\ta third argument is required which is the address of the of the\n"
477 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
478 "\tuse a '-' for the second argument. If you do not pass a third\n"
479 "\ta bd_info struct will be passed instead\n"
480#endif
wdenk8bde7f72003-06-27 21:31:46 +0000481);
482
wdenkf72da342003-10-10 10:05:42 +0000483#ifdef CONFIG_SILENT_CONSOLE
484static void
485fixup_silent_linux ()
486{
wdenkf72da342003-10-10 10:05:42 +0000487 char buf[256], *start, *end;
488 char *cmdline = getenv ("bootargs");
489
490 /* Only fix cmdline when requested */
491 if (!(gd->flags & GD_FLG_SILENT))
492 return;
493
494 debug ("before silent fix-up: %s\n", cmdline);
495 if (cmdline) {
496 if ((start = strstr (cmdline, "console=")) != NULL) {
497 end = strchr (start, ' ');
498 strncpy (buf, cmdline, (start - cmdline + 8));
499 if (end)
500 strcpy (buf + (start - cmdline + 8), end);
501 else
502 buf[start - cmdline + 8] = '\0';
503 } else {
504 strcpy (buf, cmdline);
505 strcat (buf, " console=");
506 }
507 } else {
508 strcpy (buf, "console=");
509 }
510
511 setenv ("bootargs", buf);
512 debug ("after silent fix-up: %s\n", buf);
513}
514#endif /* CONFIG_SILENT_CONSOLE */
515
wdenk2262cfe2002-11-18 00:14:45 +0000516#ifdef CONFIG_PPC
Matthew McClintockb2b78422006-08-23 13:32:45 -0500517static void __attribute__((noinline))
wdenk47d1a6e2002-11-03 00:01:44 +0000518do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
519 int argc, char *argv[],
520 ulong addr,
521 ulong *len_ptr,
522 int verify)
523{
wdenk47d1a6e2002-11-03 00:01:44 +0000524 ulong sp;
525 ulong len, checksum;
526 ulong initrd_start, initrd_end;
527 ulong cmd_start, cmd_end;
528 ulong initrd_high;
529 ulong data;
wdenk38b99262003-05-23 23:18:21 +0000530 int initrd_copy_to_ram = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000531 char *cmdline;
532 char *s;
533 bd_t *kbd;
534 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
535 image_header_t *hdr = &header;
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400536#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock25c751e2006-08-16 10:54:09 -0500537 char *of_flat_tree = NULL;
Kumar Galac76f9512006-10-24 23:47:37 -0500538 ulong of_data = 0;
Wolfgang Denkf57f70a2005-10-13 01:45:54 +0200539#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000540
541 if ((s = getenv ("initrd_high")) != NULL) {
542 /* a value of "no" or a similar string will act like 0,
543 * turning the "load high" feature off. This is intentional.
544 */
545 initrd_high = simple_strtoul(s, NULL, 16);
wdenk38b99262003-05-23 23:18:21 +0000546 if (initrd_high == ~0)
547 initrd_copy_to_ram = 0;
wdenk228f29a2002-12-08 09:53:23 +0000548 } else { /* not set, no restrictions to load high */
wdenk47d1a6e2002-11-03 00:01:44 +0000549 initrd_high = ~0;
550 }
551
wdenk56f94be2002-11-05 16:35:14 +0000552#ifdef CONFIG_LOGBUFFER
wdenk6aff3112002-12-17 01:51:00 +0000553 kbd=gd->bd;
wdenk228f29a2002-12-08 09:53:23 +0000554 /* Prevent initrd from overwriting logbuffer */
555 if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
556 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
557 debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
wdenk56f94be2002-11-05 16:35:14 +0000558#endif
559
wdenk47d1a6e2002-11-03 00:01:44 +0000560 /*
561 * Booting a (Linux) kernel image
562 *
563 * Allocate space for command line and board info - the
564 * address should be as high as possible within the reach of
565 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
566 * memory, which means far enough below the current stack
567 * pointer.
568 */
569
570 asm( "mr %0,1": "=r"(sp) : );
571
wdenk56f94be2002-11-05 16:35:14 +0000572 debug ("## Current stack ends at 0x%08lX ", sp);
573
wdenk47d1a6e2002-11-03 00:01:44 +0000574 sp -= 2048; /* just to be sure */
575 if (sp > CFG_BOOTMAPSZ)
576 sp = CFG_BOOTMAPSZ;
577 sp &= ~0xF;
578
wdenk56f94be2002-11-05 16:35:14 +0000579 debug ("=> set upper limit to 0x%08lX\n", sp);
580
wdenk47d1a6e2002-11-03 00:01:44 +0000581 cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
582 kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
583
584 if ((s = getenv("bootargs")) == NULL)
585 s = "";
586
587 strcpy (cmdline, s);
588
589 cmd_start = (ulong)&cmdline[0];
590 cmd_end = cmd_start + strlen(cmdline);
591
592 *kbd = *(gd->bd);
593
594#ifdef DEBUG
595 printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
596
597 do_bdinfo (NULL, 0, 0, NULL);
598#endif
599
600 if ((s = getenv ("clocks_in_mhz")) != NULL) {
601 /* convert all clock information to MHz */
602 kbd->bi_intfreq /= 1000000L;
603 kbd->bi_busfreq /= 1000000L;
wdenk983fda82004-10-28 00:09:35 +0000604#if defined(CONFIG_MPC8220)
wdenk9d5028c2004-11-21 00:06:33 +0000605 kbd->bi_inpfreq /= 1000000L;
606 kbd->bi_pcifreq /= 1000000L;
607 kbd->bi_pevfreq /= 1000000L;
608 kbd->bi_flbfreq /= 1000000L;
609 kbd->bi_vcofreq /= 1000000L;
wdenk983fda82004-10-28 00:09:35 +0000610#endif
Jon Loeliger9c4c5ae2005-07-23 10:37:35 -0500611#if defined(CONFIG_CPM2)
wdenk47d1a6e2002-11-03 00:01:44 +0000612 kbd->bi_cpmfreq /= 1000000L;
613 kbd->bi_brgfreq /= 1000000L;
614 kbd->bi_sccfreq /= 1000000L;
615 kbd->bi_vco /= 1000000L;
Jon Loeliger9c4c5ae2005-07-23 10:37:35 -0500616#endif
wdenkcbd8a352004-02-24 02:00:03 +0000617#if defined(CONFIG_MPC5xxx)
wdenk945af8d2003-07-16 21:53:01 +0000618 kbd->bi_ipbfreq /= 1000000L;
619 kbd->bi_pcifreq /= 1000000L;
wdenkcbd8a352004-02-24 02:00:03 +0000620#endif /* CONFIG_MPC5xxx */
wdenk47d1a6e2002-11-03 00:01:44 +0000621 }
622
Wolfgang Denkdc013d42006-03-12 01:59:35 +0100623 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong)) ntohl(hdr->ih_ep);
wdenk47d1a6e2002-11-03 00:01:44 +0000624
625 /*
626 * Check if there is an initrd image
627 */
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500628
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400629#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500630 /* Look for a '-' which indicates to ignore the ramdisk argument */
631 if (argc >= 3 && strcmp(argv[2], "-") == 0) {
632 debug ("Skipping initrd\n");
Matthew McClintock7376eb82006-10-11 15:13:01 -0500633 len = data = 0;
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500634 }
635 else
636#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000637 if (argc >= 3) {
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500638 debug ("Not skipping initrd\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000639 SHOW_BOOT_PROGRESS (9);
640
641 addr = simple_strtoul(argv[2], NULL, 16);
642
643 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
644
645 /* Copy header so we can blank CRC field for re-calculation */
646 memmove (&header, (char *)addr, sizeof(image_header_t));
647
Wolfgang Denkdc013d42006-03-12 01:59:35 +0100648 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
wdenk4b9206e2004-03-23 22:14:11 +0000649 puts ("Bad Magic Number\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000650 SHOW_BOOT_PROGRESS (-10);
651 do_reset (cmdtp, flag, argc, argv);
652 }
653
654 data = (ulong)&header;
655 len = sizeof(image_header_t);
656
Wolfgang Denkdc013d42006-03-12 01:59:35 +0100657 checksum = ntohl(hdr->ih_hcrc);
wdenk47d1a6e2002-11-03 00:01:44 +0000658 hdr->ih_hcrc = 0;
659
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200660 if (crc32 (0, (uchar *)data, len) != checksum) {
wdenk4b9206e2004-03-23 22:14:11 +0000661 puts ("Bad Header Checksum\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000662 SHOW_BOOT_PROGRESS (-11);
663 do_reset (cmdtp, flag, argc, argv);
664 }
665
666 SHOW_BOOT_PROGRESS (10);
667
668 print_image_hdr (hdr);
669
670 data = addr + sizeof(image_header_t);
Wolfgang Denkdc013d42006-03-12 01:59:35 +0100671 len = ntohl(hdr->ih_size);
wdenk47d1a6e2002-11-03 00:01:44 +0000672
673 if (verify) {
674 ulong csum = 0;
675#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
676 ulong cdata = data, edata = cdata + len;
677#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
678
wdenk4b9206e2004-03-23 22:14:11 +0000679 puts (" Verifying Checksum ... ");
wdenk47d1a6e2002-11-03 00:01:44 +0000680
681#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
682
683 while (cdata < edata) {
684 ulong chunk = edata - cdata;
685
686 if (chunk > CHUNKSZ)
687 chunk = CHUNKSZ;
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200688 csum = crc32 (csum, (uchar *)cdata, chunk);
wdenk47d1a6e2002-11-03 00:01:44 +0000689 cdata += chunk;
690
691 WATCHDOG_RESET();
692 }
693#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200694 csum = crc32 (0, (uchar *)data, len);
wdenk47d1a6e2002-11-03 00:01:44 +0000695#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
696
Wolfgang Denkdc013d42006-03-12 01:59:35 +0100697 if (csum != ntohl(hdr->ih_dcrc)) {
wdenk4b9206e2004-03-23 22:14:11 +0000698 puts ("Bad Data CRC\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000699 SHOW_BOOT_PROGRESS (-12);
700 do_reset (cmdtp, flag, argc, argv);
701 }
wdenk4b9206e2004-03-23 22:14:11 +0000702 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000703 }
704
705 SHOW_BOOT_PROGRESS (11);
706
707 if ((hdr->ih_os != IH_OS_LINUX) ||
708 (hdr->ih_arch != IH_CPU_PPC) ||
709 (hdr->ih_type != IH_TYPE_RAMDISK) ) {
wdenk4b9206e2004-03-23 22:14:11 +0000710 puts ("No Linux PPC Ramdisk Image\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000711 SHOW_BOOT_PROGRESS (-13);
712 do_reset (cmdtp, flag, argc, argv);
713 }
714
715 /*
716 * Now check if we have a multifile image
717 */
718 } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
719 u_long tail = ntohl(len_ptr[0]) % 4;
720 int i;
721
722 SHOW_BOOT_PROGRESS (13);
723
724 /* skip kernel length and terminator */
725 data = (ulong)(&len_ptr[2]);
726 /* skip any additional image length fields */
727 for (i=1; len_ptr[i]; ++i)
728 data += 4;
729 /* add kernel length, and align */
730 data += ntohl(len_ptr[0]);
731 if (tail) {
732 data += 4 - tail;
733 }
734
735 len = ntohl(len_ptr[1]);
736
737 } else {
738 /*
739 * no initrd image
740 */
741 SHOW_BOOT_PROGRESS (14);
742
743 len = data = 0;
744 }
745
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400746#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock5de62c42006-08-22 09:31:59 -0500747 if(argc > 3) {
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500748 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
Matthew McClintock25c751e2006-08-16 10:54:09 -0500749 hdr = (image_header_t *)of_flat_tree;
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400750#if defined(CONFIG_OF_LIBFDT)
751 if (be32_to_cpu(fdt_magic(of_flat_tree)) == FDT_MAGIC) {
752#else
753 if (*(ulong *)of_flat_tree == OF_DT_HEADER) {
754#endif
Matthew McClintock25c751e2006-08-16 10:54:09 -0500755#ifndef CFG_NO_FLASH
Kumar Galac76f9512006-10-24 23:47:37 -0500756 if (addr2info((ulong)of_flat_tree) != NULL)
757 of_data = (ulong)of_flat_tree;
Matthew McClintock25c751e2006-08-16 10:54:09 -0500758#endif
759 } else if (ntohl(hdr->ih_magic) == IH_MAGIC) {
760 printf("## Flat Device Tree Image at %08lX\n", hdr);
761 print_image_hdr(hdr);
762
763 if ((ntohl(hdr->ih_load) < ((unsigned long)hdr + ntohl(hdr->ih_size) + sizeof(hdr))) &&
764 ((ntohl(hdr->ih_load) + ntohl(hdr->ih_size)) > (unsigned long)hdr)) {
765 printf ("ERROR: Load address overwrites Flat Device Tree uImage\n");
766 return;
767 }
Matthew McClintock87a449c2006-08-22 09:23:55 -0500768
Matthew McClintock25c751e2006-08-16 10:54:09 -0500769 printf(" Verifying Checksum ... ");
770 memmove (&header, (char *)hdr, sizeof(image_header_t));
771 checksum = ntohl(header.ih_hcrc);
772 header.ih_hcrc = 0;
773
774 if(checksum != crc32(0, (uchar *)&header, sizeof(image_header_t))) {
775 printf("ERROR: Flat Device Tree header checksum is invalid\n");
776 return;
777 }
778
779 checksum = ntohl(hdr->ih_dcrc);
780 addr = (ulong)((uchar *)(hdr) + sizeof(image_header_t));
781 len = ntohl(hdr->ih_size);
782
783 if(checksum != crc32(0, (uchar *)addr, len)) {
784 printf("ERROR: Flat Device Tree checksum is invalid\n");
785 return;
786 }
787 printf("OK\n");
788
789 if (ntohl(hdr->ih_type) != IH_TYPE_FLATDT) {
790 printf ("ERROR: uImage not Flat Device Tree type\n");
791 return;
792 }
793 if (ntohl(hdr->ih_comp) != IH_COMP_NONE) {
794 printf("ERROR: uImage is not uncompressed\n");
795 return;
796 }
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400797#if defined(CONFIG_OF_LIBFDT)
798 if (be32_to_cpu(fdt_magic(of_flat_tree + sizeof(image_header_t))) != FDT_MAGIC) {
799#else
Matthew McClintock25c751e2006-08-16 10:54:09 -0500800 if (*((ulong *)(of_flat_tree + sizeof(image_header_t))) != OF_DT_HEADER) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400801#endif
Matthew McClintock25c751e2006-08-16 10:54:09 -0500802 printf ("ERROR: uImage data is not a flat device tree\n");
803 return;
804 }
Matthew McClintock87a449c2006-08-22 09:23:55 -0500805
806 memmove((void *)ntohl(hdr->ih_load),
Matthew McClintock25c751e2006-08-16 10:54:09 -0500807 (void *)(of_flat_tree + sizeof(image_header_t)),
808 ntohl(hdr->ih_size));
809 of_flat_tree = (char *)ntohl(hdr->ih_load);
810 } else {
811 printf ("Did not find a flat flat device tree at address %08lX\n", of_flat_tree);
812 return;
813 }
814 printf (" Booting using flat device tree at 0x%x\n",
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500815 of_flat_tree);
Kumar Galac76f9512006-10-24 23:47:37 -0500816 } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]) && (len_ptr[2])) {
817 u_long tail = ntohl(len_ptr[0]) % 4;
818 int i;
819
820 /* skip kernel length, initrd length, and terminator */
821 of_data = (ulong)(&len_ptr[3]);
822 /* skip any additional image length fields */
823 for (i=2; len_ptr[i]; ++i)
824 of_data += 4;
825 /* add kernel length, and align */
826 of_data += ntohl(len_ptr[0]);
827 if (tail) {
828 of_data += 4 - tail;
829 }
830
831 /* add initrd length, and align */
832 tail = ntohl(len_ptr[1]) % 4;
833 of_data += ntohl(len_ptr[1]);
834 if (tail) {
835 of_data += 4 - tail;
836 }
837
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400838#if defined(CONFIG_OF_LIBFDT)
839 if (be32_to_cpu(fdt_magic(of_data)) != FDT_MAGIC) {
840#else
Kumar Galac76f9512006-10-24 23:47:37 -0500841 if (((struct boot_param_header *)of_data)->magic != OF_DT_HEADER) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400842#endif
Kumar Galac76f9512006-10-24 23:47:37 -0500843 printf ("ERROR: image is not a flat device tree\n");
844 return;
845 }
846
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400847#if defined(CONFIG_OF_LIBFDT)
848 if (be32_to_cpu(fdt_totalsize(of_data)) != ntohl(len_ptr[2])) {
849#else
Kumar Galac76f9512006-10-24 23:47:37 -0500850 if (((struct boot_param_header *)of_data)->totalsize != ntohl(len_ptr[2])) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400851#endif
Kumar Galac76f9512006-10-24 23:47:37 -0500852 printf ("ERROR: flat device tree size does not agree with image\n");
853 return;
854 }
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500855 }
856#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000857 if (!data) {
wdenk56f94be2002-11-05 16:35:14 +0000858 debug ("No initrd\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000859 }
wdenk47d1a6e2002-11-03 00:01:44 +0000860
861 if (data) {
wdenk38b99262003-05-23 23:18:21 +0000862 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
863 initrd_start = data;
864 initrd_end = initrd_start + len;
865 } else {
wdenk47d1a6e2002-11-03 00:01:44 +0000866 initrd_start = (ulong)kbd - len;
867 initrd_start &= ~(4096 - 1); /* align on page */
868
869 if (initrd_high) {
870 ulong nsp;
871
872 /*
873 * the inital ramdisk does not need to be within
874 * CFG_BOOTMAPSZ as it is not accessed until after
875 * the mm system is initialised.
876 *
877 * do the stack bottom calculation again and see if
878 * the initrd will fit just below the monitor stack
879 * bottom without overwriting the area allocated
880 * above for command line args and board info.
881 */
882 asm( "mr %0,1": "=r"(nsp) : );
883 nsp -= 2048; /* just to be sure */
884 nsp &= ~0xF;
885 if (nsp > initrd_high) /* limit as specified */
886 nsp = initrd_high;
887 nsp -= len;
888 nsp &= ~(4096 - 1); /* align on page */
889 if (nsp >= sp)
890 initrd_start = nsp;
891 }
892
893 SHOW_BOOT_PROGRESS (12);
wdenk56f94be2002-11-05 16:35:14 +0000894
895 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
wdenk47d1a6e2002-11-03 00:01:44 +0000896 data, data + len - 1, len, len);
wdenk56f94be2002-11-05 16:35:14 +0000897
wdenk47d1a6e2002-11-03 00:01:44 +0000898 initrd_end = initrd_start + len;
899 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
900 initrd_start, initrd_end);
901#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
902 {
903 size_t l = len;
904 void *to = (void *)initrd_start;
905 void *from = (void *)data;
906
907 while (l > 0) {
908 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
909 WATCHDOG_RESET();
910 memmove (to, from, tail);
911 to += tail;
912 from += tail;
913 l -= tail;
914 }
915 }
916#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
917 memmove ((void *)initrd_start, (void *)data, len);
918#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
wdenk4b9206e2004-03-23 22:14:11 +0000919 puts ("OK\n");
wdenk38b99262003-05-23 23:18:21 +0000920 }
wdenk47d1a6e2002-11-03 00:01:44 +0000921 } else {
922 initrd_start = 0;
923 initrd_end = 0;
924 }
925
wdenk56f94be2002-11-05 16:35:14 +0000926 debug ("## Transferring control to Linux (at address %08lx) ...\n",
wdenk47d1a6e2002-11-03 00:01:44 +0000927 (ulong)kernel);
wdenk56f94be2002-11-05 16:35:14 +0000928
wdenk47d1a6e2002-11-03 00:01:44 +0000929 SHOW_BOOT_PROGRESS (15);
930
wdenk42d1f032003-10-15 23:53:47 +0000931#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
wdenk47d1a6e2002-11-03 00:01:44 +0000932 unlock_ram_in_cache();
933#endif
Wolfgang Denkf57f70a2005-10-13 01:45:54 +0200934
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400935#if defined(CONFIG_OF_LIBFDT)
936 /* move of_flat_tree if needed */
937 if (of_data) {
938 int err;
939 ulong of_start, of_len;
940 of_len = be32_to_cpu(fdt_totalsize(of_data));
941 /* provide extra 8k pad */
942 if (initrd_start)
943 of_start = initrd_start - of_len - 8192;
944 else
945 of_start = (ulong)kbd - of_len - 8192;
946 of_start &= ~(4096 - 1); /* align on page */
947 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
948 of_data, of_data + of_len - 1, of_len, of_len);
949
950
951 printf (" Loading Device Tree to %08lx, end %08lx ... ",
952 of_start, of_start + of_len - 1);
953 err = fdt_open_into(of_start, of_data, of_len);
954 if (err != 0) {
955 printf ("libfdt: %s\n", fdt_strerror(err));
956 }
957 }
958#endif
959#if defined(CONFIG_OF_FLAT_TREE)
Kumar Galac76f9512006-10-24 23:47:37 -0500960 /* move of_flat_tree if needed */
961 if (of_data) {
962 ulong of_start, of_len;
963 of_len = ((struct boot_param_header *)of_data)->totalsize;
964 /* provide extra 8k pad */
965 if (initrd_start)
966 of_start = initrd_start - of_len - 8192;
967 else
968 of_start = (ulong)kbd - of_len - 8192;
969 of_start &= ~(4096 - 1); /* align on page */
970 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
971 of_data, of_data + of_len - 1, of_len, of_len);
972
973 of_flat_tree = (char *)of_start;
974 printf (" Loading Device Tree to %08lx, end %08lx ... ",
975 of_start, of_start + of_len - 1);
976 memmove ((void *)of_start, (void *)of_data, of_len);
977 }
Wolfgang Denkf57f70a2005-10-13 01:45:54 +0200978#endif
Stefan Roeseda5553b2006-11-27 17:04:06 +0100979
Wolfgang Denkf57f70a2005-10-13 01:45:54 +0200980 /*
Stefan Roeseda5553b2006-11-27 17:04:06 +0100981 * Linux Kernel Parameters (passing board info data):
982 * r3: ptr to board info data
983 * r4: initrd_start or 0 if no initrd
984 * r5: initrd_end - unused if r4 is 0
985 * r6: Start of command line string
986 * r7: End of command line string
987 */
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400988#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Stefan Roeseda5553b2006-11-27 17:04:06 +0100989 if (!of_flat_tree) /* no device tree; boot old style */
990#endif
991 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
992 /* does not return */
993
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400994#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Stefan Roeseda5553b2006-11-27 17:04:06 +0100995 /*
996 * Linux Kernel Parameters (passing device tree):
Wolfgang Denkf57f70a2005-10-13 01:45:54 +0200997 * r3: ptr to OF flat tree, followed by the board info data
Kumar Galae559a692006-01-11 16:41:35 -0600998 * r4: physical pointer to the kernel itself
999 * r5: NULL
1000 * r6: NULL
1001 * r7: NULL
Wolfgang Denkf57f70a2005-10-13 01:45:54 +02001002 */
Gerald Van Baren213bf8c2007-03-31 12:23:51 -04001003#if defined(CONFIG_OF_FLAT_TREE)
Stefan Roeseda5553b2006-11-27 17:04:06 +01001004 ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
1005 /* ft_dump_blob(of_flat_tree); */
Gerald Van Baren213bf8c2007-03-31 12:23:51 -04001006#endif
Stefan Roeseda5553b2006-11-27 17:04:06 +01001007
1008 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
1009#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001010}
wdenk1cb8e982003-03-06 21:55:29 +00001011#endif /* CONFIG_PPC */
wdenk47d1a6e2002-11-03 00:01:44 +00001012
1013static void
1014do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
1015 int argc, char *argv[],
1016 ulong addr,
1017 ulong *len_ptr,
1018 int verify)
1019{
wdenk47d1a6e2002-11-03 00:01:44 +00001020 image_header_t *hdr = &header;
1021
1022 void (*loader)(bd_t *, image_header_t *, char *, char *);
1023 image_header_t *img_addr;
1024 char *consdev;
1025 char *cmdline;
1026
1027
1028 /*
1029 * Booting a (NetBSD) kernel image
1030 *
1031 * This process is pretty similar to a standalone application:
1032 * The (first part of an multi-) image must be a stage-2 loader,
1033 * which in turn is responsible for loading & invoking the actual
1034 * kernel. The only differences are the parameters being passed:
1035 * besides the board info strucure, the loader expects a command
1036 * line, the name of the console device, and (optionally) the
1037 * address of the original image header.
1038 */
1039
1040 img_addr = 0;
1041 if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
1042 img_addr = (image_header_t *) addr;
1043
1044
1045 consdev = "";
1046#if defined (CONFIG_8xx_CONS_SMC1)
1047 consdev = "smc1";
1048#elif defined (CONFIG_8xx_CONS_SMC2)
1049 consdev = "smc2";
1050#elif defined (CONFIG_8xx_CONS_SCC2)
1051 consdev = "scc2";
1052#elif defined (CONFIG_8xx_CONS_SCC3)
1053 consdev = "scc3";
1054#endif
1055
1056 if (argc > 2) {
1057 ulong len;
1058 int i;
1059
1060 for (i=2, len=0 ; i<argc ; i+=1)
1061 len += strlen (argv[i]) + 1;
1062 cmdline = malloc (len);
1063
1064 for (i=2, len=0 ; i<argc ; i+=1) {
1065 if (i > 2)
1066 cmdline[len++] = ' ';
1067 strcpy (&cmdline[len], argv[i]);
1068 len += strlen (argv[i]);
1069 }
1070 } else if ((cmdline = getenv("bootargs")) == NULL) {
1071 cmdline = "";
1072 }
1073
Wolfgang Denkdc013d42006-03-12 01:59:35 +01001074 loader = (void (*)(bd_t *, image_header_t *, char *, char *)) ntohl(hdr->ih_ep);
wdenk47d1a6e2002-11-03 00:01:44 +00001075
1076 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
1077 (ulong)loader);
1078
1079 SHOW_BOOT_PROGRESS (15);
1080
1081 /*
1082 * NetBSD Stage-2 Loader Parameters:
1083 * r3: ptr to board info data
1084 * r4: image address
1085 * r5: console device
1086 * r6: boot args string
1087 */
1088 (*loader) (gd->bd, img_addr, consdev, cmdline);
1089}
1090
wdenk7f70e852003-05-20 14:25:27 +00001091#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
1092
1093/* Function that returns a character from the environment */
1094extern uchar (*env_get_char)(int);
1095
1096static void
1097do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
1098 int argc, char *argv[],
1099 ulong addr,
1100 ulong *len_ptr,
1101 int verify)
1102{
wdenk7f70e852003-05-20 14:25:27 +00001103 ulong top;
1104 char *s, *cmdline;
1105 char **fwenv, **ss;
1106 int i, j, nxt, len, envno, envsz;
1107 bd_t *kbd;
1108 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
1109 image_header_t *hdr = &header;
1110
1111 /*
1112 * Booting an ARTOS kernel image + application
1113 */
1114
1115 /* this used to be the top of memory, but was wrong... */
1116#ifdef CONFIG_PPC
1117 /* get stack pointer */
1118 asm volatile ("mr %0,1" : "=r"(top) );
1119#endif
1120 debug ("## Current stack ends at 0x%08lX ", top);
1121
1122 top -= 2048; /* just to be sure */
1123 if (top > CFG_BOOTMAPSZ)
1124 top = CFG_BOOTMAPSZ;
1125 top &= ~0xF;
1126
1127 debug ("=> set upper limit to 0x%08lX\n", top);
1128
1129 /* first check the artos specific boot args, then the linux args*/
1130 if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
1131 s = "";
1132
1133 /* get length of cmdline, and place it */
1134 len = strlen(s);
1135 top = (top - (len + 1)) & ~0xF;
1136 cmdline = (char *)top;
1137 debug ("## cmdline at 0x%08lX ", top);
1138 strcpy(cmdline, s);
1139
1140 /* copy bdinfo */
1141 top = (top - sizeof(bd_t)) & ~0xF;
1142 debug ("## bd at 0x%08lX ", top);
1143 kbd = (bd_t *)top;
1144 memcpy(kbd, gd->bd, sizeof(bd_t));
1145
1146 /* first find number of env entries, and their size */
1147 envno = 0;
1148 envsz = 0;
1149 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1150 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1151 ;
1152 envno++;
1153 envsz += (nxt - i) + 1; /* plus trailing zero */
1154 }
1155 envno++; /* plus the terminating zero */
1156 debug ("## %u envvars total size %u ", envno, envsz);
1157
1158 top = (top - sizeof(char **)*envno) & ~0xF;
1159 fwenv = (char **)top;
1160 debug ("## fwenv at 0x%08lX ", top);
1161
1162 top = (top - envsz) & ~0xF;
1163 s = (char *)top;
1164 ss = fwenv;
1165
1166 /* now copy them */
1167 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1168 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1169 ;
1170 *ss++ = s;
1171 for (j = i; j < nxt; ++j)
1172 *s++ = env_get_char(j);
1173 *s++ = '\0';
1174 }
1175 *ss++ = NULL; /* terminate */
1176
1177 entry = (void (*)(bd_t *, char *, char **, ulong))ntohl(hdr->ih_ep);
1178 (*entry)(kbd, cmdline, fwenv, top);
1179}
1180#endif
1181
1182
wdenk47d1a6e2002-11-03 00:01:44 +00001183#if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
1184int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1185{
1186 int rcode = 0;
1187#ifndef CFG_HUSH_PARSER
1188 if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
1189#else
1190 if (parse_string_outer(getenv("bootcmd"),
1191 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
1192#endif
1193 return rcode;
1194}
wdenk8bde7f72003-06-27 21:31:46 +00001195
wdenk0d498392003-07-01 21:06:45 +00001196U_BOOT_CMD(
1197 boot, 1, 1, do_bootd,
wdenk9d2b18a2003-06-28 23:11:04 +00001198 "boot - boot default, i.e., run 'bootcmd'\n",
1199 NULL
1200);
1201
1202/* keep old command name "bootd" for backward compatibility */
wdenk0d498392003-07-01 21:06:45 +00001203U_BOOT_CMD(
1204 bootd, 1, 1, do_bootd,
wdenk8bde7f72003-06-27 21:31:46 +00001205 "bootd - boot default, i.e., run 'bootcmd'\n",
1206 NULL
1207);
1208
wdenk47d1a6e2002-11-03 00:01:44 +00001209#endif
1210
1211#if (CONFIG_COMMANDS & CFG_CMD_IMI)
1212int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1213{
1214 int arg;
1215 ulong addr;
1216 int rcode=0;
1217
1218 if (argc < 2) {
1219 return image_info (load_addr);
1220 }
1221
1222 for (arg=1; arg <argc; ++arg) {
1223 addr = simple_strtoul(argv[arg], NULL, 16);
1224 if (image_info (addr) != 0) rcode = 1;
1225 }
1226 return rcode;
1227}
1228
1229static int image_info (ulong addr)
1230{
1231 ulong data, len, checksum;
1232 image_header_t *hdr = &header;
1233
1234 printf ("\n## Checking Image at %08lx ...\n", addr);
1235
1236 /* Copy header so we can blank CRC field for re-calculation */
1237 memmove (&header, (char *)addr, sizeof(image_header_t));
1238
1239 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
wdenk4b9206e2004-03-23 22:14:11 +00001240 puts (" Bad Magic Number\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001241 return 1;
1242 }
1243
1244 data = (ulong)&header;
1245 len = sizeof(image_header_t);
1246
1247 checksum = ntohl(hdr->ih_hcrc);
1248 hdr->ih_hcrc = 0;
1249
Wolfgang Denk77ddac92005-10-13 16:45:02 +02001250 if (crc32 (0, (uchar *)data, len) != checksum) {
wdenk4b9206e2004-03-23 22:14:11 +00001251 puts (" Bad Header Checksum\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001252 return 1;
1253 }
1254
1255 /* for multi-file images we need the data part, too */
1256 print_image_hdr ((image_header_t *)addr);
1257
1258 data = addr + sizeof(image_header_t);
1259 len = ntohl(hdr->ih_size);
1260
wdenk4b9206e2004-03-23 22:14:11 +00001261 puts (" Verifying Checksum ... ");
Wolfgang Denk77ddac92005-10-13 16:45:02 +02001262 if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
wdenk4b9206e2004-03-23 22:14:11 +00001263 puts (" Bad Data CRC\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001264 return 1;
1265 }
wdenk4b9206e2004-03-23 22:14:11 +00001266 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001267 return 0;
1268}
wdenk0d498392003-07-01 21:06:45 +00001269
1270U_BOOT_CMD(
1271 iminfo, CFG_MAXARGS, 1, do_iminfo,
wdenk8bde7f72003-06-27 21:31:46 +00001272 "iminfo - print header information for application image\n",
1273 "addr [addr ...]\n"
1274 " - print header information for application image starting at\n"
1275 " address 'addr' in memory; this includes verification of the\n"
1276 " image contents (magic number, header and payload checksums)\n"
1277);
1278
wdenk47d1a6e2002-11-03 00:01:44 +00001279#endif /* CFG_CMD_IMI */
1280
wdenk27b207f2003-07-24 23:38:38 +00001281#if (CONFIG_COMMANDS & CFG_CMD_IMLS)
1282/*-----------------------------------------------------------------------
1283 * List all images found in flash.
1284 */
1285int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1286{
1287 flash_info_t *info;
1288 int i, j;
1289 image_header_t *hdr;
wdenk5bb226e2003-11-17 21:14:37 +00001290 ulong data, len, checksum;
wdenk27b207f2003-07-24 23:38:38 +00001291
1292 for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
1293 if (info->flash_id == FLASH_UNKNOWN)
1294 goto next_bank;
Marian Balakowicze6f2e902005-10-11 19:09:42 +02001295 for (j=0; j<info->sector_count; ++j) {
wdenk27b207f2003-07-24 23:38:38 +00001296
1297 if (!(hdr=(image_header_t *)info->start[j]) ||
1298 (ntohl(hdr->ih_magic) != IH_MAGIC))
1299 goto next_sector;
1300
1301 /* Copy header so we can blank CRC field for re-calculation */
1302 memmove (&header, (char *)hdr, sizeof(image_header_t));
1303
1304 checksum = ntohl(header.ih_hcrc);
1305 header.ih_hcrc = 0;
1306
Wolfgang Denk77ddac92005-10-13 16:45:02 +02001307 if (crc32 (0, (uchar *)&header, sizeof(image_header_t))
wdenk27b207f2003-07-24 23:38:38 +00001308 != checksum)
1309 goto next_sector;
1310
1311 printf ("Image at %08lX:\n", (ulong)hdr);
1312 print_image_hdr( hdr );
wdenk5bb226e2003-11-17 21:14:37 +00001313
1314 data = (ulong)hdr + sizeof(image_header_t);
1315 len = ntohl(hdr->ih_size);
1316
wdenk4b9206e2004-03-23 22:14:11 +00001317 puts (" Verifying Checksum ... ");
Wolfgang Denk77ddac92005-10-13 16:45:02 +02001318 if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
wdenk4b9206e2004-03-23 22:14:11 +00001319 puts (" Bad Data CRC\n");
wdenk5bb226e2003-11-17 21:14:37 +00001320 }
wdenk4b9206e2004-03-23 22:14:11 +00001321 puts ("OK\n");
wdenkbdccc4f2003-08-05 17:43:17 +00001322next_sector: ;
wdenk27b207f2003-07-24 23:38:38 +00001323 }
wdenkbdccc4f2003-08-05 17:43:17 +00001324next_bank: ;
wdenk27b207f2003-07-24 23:38:38 +00001325 }
1326
1327 return (0);
1328}
1329
1330U_BOOT_CMD(
1331 imls, 1, 1, do_imls,
1332 "imls - list all images found in flash\n",
1333 "\n"
1334 " - Prints information about all images found at sector\n"
1335 " boundaries in flash.\n"
1336);
1337#endif /* CFG_CMD_IMLS */
1338
wdenk47d1a6e2002-11-03 00:01:44 +00001339void
1340print_image_hdr (image_header_t *hdr)
1341{
1342#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
1343 time_t timestamp = (time_t)ntohl(hdr->ih_time);
1344 struct rtc_time tm;
1345#endif
1346
1347 printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
1348#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
1349 to_tm (timestamp, &tm);
1350 printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
1351 tm.tm_year, tm.tm_mon, tm.tm_mday,
1352 tm.tm_hour, tm.tm_min, tm.tm_sec);
1353#endif /* CFG_CMD_DATE, CONFIG_TIMESTAMP */
wdenk4b9206e2004-03-23 22:14:11 +00001354 puts (" Image Type: "); print_type(hdr);
1355 printf ("\n Data Size: %d Bytes = ", ntohl(hdr->ih_size));
wdenk47d1a6e2002-11-03 00:01:44 +00001356 print_size (ntohl(hdr->ih_size), "\n");
wdenk4b9206e2004-03-23 22:14:11 +00001357 printf (" Load Address: %08x\n"
1358 " Entry Point: %08x\n",
1359 ntohl(hdr->ih_load), ntohl(hdr->ih_ep));
wdenk47d1a6e2002-11-03 00:01:44 +00001360
1361 if (hdr->ih_type == IH_TYPE_MULTI) {
1362 int i;
1363 ulong len;
1364 ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
1365
wdenk4b9206e2004-03-23 22:14:11 +00001366 puts (" Contents:\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001367 for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
1368 printf (" Image %d: %8ld Bytes = ", i, len);
1369 print_size (len, "\n");
1370 }
1371 }
1372}
1373
1374
1375static void
1376print_type (image_header_t *hdr)
1377{
1378 char *os, *arch, *type, *comp;
1379
1380 switch (hdr->ih_os) {
1381 case IH_OS_INVALID: os = "Invalid OS"; break;
1382 case IH_OS_NETBSD: os = "NetBSD"; break;
1383 case IH_OS_LINUX: os = "Linux"; break;
1384 case IH_OS_VXWORKS: os = "VxWorks"; break;
1385 case IH_OS_QNX: os = "QNX"; break;
1386 case IH_OS_U_BOOT: os = "U-Boot"; break;
wdenkd791b1d2003-04-20 14:04:18 +00001387 case IH_OS_RTEMS: os = "RTEMS"; break;
wdenk7f70e852003-05-20 14:25:27 +00001388#ifdef CONFIG_ARTOS
1389 case IH_OS_ARTOS: os = "ARTOS"; break;
1390#endif
wdenk1f4bb372003-07-27 00:21:01 +00001391#ifdef CONFIG_LYNXKDI
1392 case IH_OS_LYNXOS: os = "LynxOS"; break;
1393#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001394 default: os = "Unknown OS"; break;
1395 }
1396
1397 switch (hdr->ih_arch) {
1398 case IH_CPU_INVALID: arch = "Invalid CPU"; break;
1399 case IH_CPU_ALPHA: arch = "Alpha"; break;
1400 case IH_CPU_ARM: arch = "ARM"; break;
Stefan Roese1a1b7372006-10-09 12:55:38 +02001401 case IH_CPU_AVR32: arch = "AVR32"; break;
Wolfgang Denk44ba4642007-03-22 00:13:12 +01001402 case IH_CPU_BLACKFIN: arch = "Blackfin"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001403 case IH_CPU_I386: arch = "Intel x86"; break;
1404 case IH_CPU_IA64: arch = "IA64"; break;
Wolfgang Denk44ba4642007-03-22 00:13:12 +01001405 case IH_CPU_M68K: arch = "M68K"; break;
1406 case IH_CPU_MICROBLAZE: arch = "Microblaze"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001407 case IH_CPU_MIPS64: arch = "MIPS 64 Bit"; break;
Wolfgang Denk44ba4642007-03-22 00:13:12 +01001408 case IH_CPU_MIPS: arch = "MIPS"; break;
1409 case IH_CPU_NIOS2: arch = "Nios-II"; break;
1410 case IH_CPU_NIOS: arch = "Nios"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001411 case IH_CPU_PPC: arch = "PowerPC"; break;
1412 case IH_CPU_S390: arch = "IBM S390"; break;
1413 case IH_CPU_SH: arch = "SuperH"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001414 case IH_CPU_SPARC64: arch = "SPARC 64 Bit"; break;
Wolfgang Denk44ba4642007-03-22 00:13:12 +01001415 case IH_CPU_SPARC: arch = "SPARC"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001416 default: arch = "Unknown Architecture"; break;
1417 }
1418
1419 switch (hdr->ih_type) {
1420 case IH_TYPE_INVALID: type = "Invalid Image"; break;
1421 case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
1422 case IH_TYPE_KERNEL: type = "Kernel Image"; break;
1423 case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
1424 case IH_TYPE_MULTI: type = "Multi-File Image"; break;
1425 case IH_TYPE_FIRMWARE: type = "Firmware"; break;
1426 case IH_TYPE_SCRIPT: type = "Script"; break;
Matthew McClintock25c751e2006-08-16 10:54:09 -05001427 case IH_TYPE_FLATDT: type = "Flat Device Tree"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001428 default: type = "Unknown Image"; break;
1429 }
1430
1431 switch (hdr->ih_comp) {
1432 case IH_COMP_NONE: comp = "uncompressed"; break;
1433 case IH_COMP_GZIP: comp = "gzip compressed"; break;
1434 case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
1435 default: comp = "unknown compression"; break;
1436 }
1437
1438 printf ("%s %s %s (%s)", arch, os, type, comp);
1439}
1440
1441#define ZALLOC_ALIGNMENT 16
1442
1443static void *zalloc(void *x, unsigned items, unsigned size)
1444{
1445 void *p;
1446
1447 size *= items;
1448 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
1449
1450 p = malloc (size);
1451
1452 return (p);
1453}
1454
1455static void zfree(void *x, void *addr, unsigned nb)
1456{
1457 free (addr);
1458}
1459
1460#define HEAD_CRC 2
1461#define EXTRA_FIELD 4
1462#define ORIG_NAME 8
1463#define COMMENT 0x10
1464#define RESERVED 0xe0
1465
1466#define DEFLATED 8
1467
wdenkeedcd072004-09-08 22:03:11 +00001468int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
wdenk47d1a6e2002-11-03 00:01:44 +00001469{
1470 z_stream s;
1471 int r, i, flags;
1472
1473 /* skip header */
1474 i = 10;
1475 flags = src[3];
1476 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +00001477 puts ("Error: Bad gzipped data\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001478 return (-1);
1479 }
1480 if ((flags & EXTRA_FIELD) != 0)
1481 i = 12 + src[10] + (src[11] << 8);
1482 if ((flags & ORIG_NAME) != 0)
1483 while (src[i++] != 0)
1484 ;
1485 if ((flags & COMMENT) != 0)
1486 while (src[i++] != 0)
1487 ;
1488 if ((flags & HEAD_CRC) != 0)
1489 i += 2;
1490 if (i >= *lenp) {
wdenk4b9206e2004-03-23 22:14:11 +00001491 puts ("Error: gunzip out of data in header\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001492 return (-1);
1493 }
1494
1495 s.zalloc = zalloc;
1496 s.zfree = zfree;
1497#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
1498 s.outcb = (cb_func)WATCHDOG_RESET;
1499#else
1500 s.outcb = Z_NULL;
1501#endif /* CONFIG_HW_WATCHDOG */
1502
1503 r = inflateInit2(&s, -MAX_WBITS);
1504 if (r != Z_OK) {
1505 printf ("Error: inflateInit2() returned %d\n", r);
1506 return (-1);
1507 }
1508 s.next_in = src + i;
1509 s.avail_in = *lenp - i;
1510 s.next_out = dst;
1511 s.avail_out = dstlen;
1512 r = inflate(&s, Z_FINISH);
1513 if (r != Z_OK && r != Z_STREAM_END) {
1514 printf ("Error: inflate() returned %d\n", r);
1515 return (-1);
1516 }
1517 *lenp = s.next_out - (unsigned char *) dst;
1518 inflateEnd(&s);
1519
1520 return (0);
1521}
1522
wdenkc29fdfc2003-08-29 20:57:53 +00001523#ifdef CONFIG_BZIP2
1524void bz_internal_error(int errcode)
1525{
1526 printf ("BZIP2 internal error %d\n", errcode);
1527}
1528#endif /* CONFIG_BZIP2 */
1529
wdenkd791b1d2003-04-20 14:04:18 +00001530static void
1531do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1532 ulong addr, ulong *len_ptr, int verify)
1533{
wdenkd791b1d2003-04-20 14:04:18 +00001534 image_header_t *hdr = &header;
1535 void (*entry_point)(bd_t *);
1536
Wolfgang Denkdc013d42006-03-12 01:59:35 +01001537 entry_point = (void (*)(bd_t *)) ntohl(hdr->ih_ep);
wdenkd791b1d2003-04-20 14:04:18 +00001538
1539 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
1540 (ulong)entry_point);
1541
1542 SHOW_BOOT_PROGRESS (15);
1543
1544 /*
1545 * RTEMS Parameters:
1546 * r3: ptr to board info data
1547 */
1548
1549 (*entry_point ) ( gd->bd );
1550}
1551
wdenk47d1a6e2002-11-03 00:01:44 +00001552#if (CONFIG_COMMANDS & CFG_CMD_ELF)
1553static void
1554do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1555 ulong addr, ulong *len_ptr, int verify)
1556{
1557 image_header_t *hdr = &header;
1558 char str[80];
1559
Wolfgang Denkdc013d42006-03-12 01:59:35 +01001560 sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001561 setenv("loadaddr", str);
1562 do_bootvx(cmdtp, 0, 0, NULL);
1563}
1564
1565static void
1566do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1567 ulong addr, ulong *len_ptr, int verify)
1568{
1569 image_header_t *hdr = &header;
1570 char *local_args[2];
1571 char str[16];
1572
Wolfgang Denkdc013d42006-03-12 01:59:35 +01001573 sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001574 local_args[0] = argv[0];
1575 local_args[1] = str; /* and provide it via the arguments */
1576 do_bootelf(cmdtp, 0, 2, local_args);
1577}
1578#endif /* CFG_CMD_ELF */
wdenk1f4bb372003-07-27 00:21:01 +00001579
1580#ifdef CONFIG_LYNXKDI
1581static void
1582do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
1583 int argc, char *argv[],
1584 ulong addr,
1585 ulong *len_ptr,
1586 int verify)
1587{
1588 lynxkdi_boot( &header );
1589}
1590
1591#endif /* CONFIG_LYNXKDI */