blob: 32c29e55a37bc829d16226590d94f63adc934ef0 [file] [log] [blame]
wdenk47d1a6e2002-11-03 00:01:44 +00001/*
Stefan Roese5d5ce292006-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>
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
Gerald Van Barend6abef42007-03-31 12:23:51 -040037#if defined(CONFIG_OF_LIBFDT)
38#include <fdt.h>
39#include <libfdt.h>
Gerald Van Barenc9502b92007-04-14 22:51:24 -040040#include <fdt_support.h>
Gerald Van Barend6abef42007-03-31 12:23:51 -040041#endif
Gerald Van Baren32480892007-03-31 14:30:53 -040042#if defined(CONFIG_OF_FLAT_TREE)
Wolfgang Denk27a5b0b2005-10-13 01:45:54 +020043#include <ft_build.h>
44#endif
45
Wolfgang Denk6405a152006-03-31 18:32:53 +020046DECLARE_GLOBAL_DATA_PTR;
47
wdenk57b2d802003-06-27 21:31:46 +000048 /*cmd_boot.c*/
49 extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
50
wdenk47d1a6e2002-11-03 00:01:44 +000051#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
52#include <rtc.h>
53#endif
54
55#ifdef CFG_HUSH_PARSER
56#include <hush.h>
57#endif
58
59#ifdef CONFIG_SHOW_BOOT_PROGRESS
60# include <status_led.h>
61# define SHOW_BOOT_PROGRESS(arg) show_boot_progress(arg)
62#else
63# define SHOW_BOOT_PROGRESS(arg)
64#endif
65
66#ifdef CFG_INIT_RAM_LOCK
67#include <asm/cache.h>
68#endif
69
wdenk9dfa8d12002-12-08 09:53:23 +000070#ifdef CONFIG_LOGBUFFER
71#include <logbuff.h>
72#endif
73
wdenk381669a2003-06-16 23:50:08 +000074#ifdef CONFIG_HAS_DATAFLASH
75#include <dataflash.h>
76#endif
77
wdenk47d1a6e2002-11-03 00:01:44 +000078/*
79 * Some systems (for example LWMON) have very short watchdog periods;
80 * we must make sure to split long operations like memmove() or
81 * crc32() into reasonable chunks.
82 */
83#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
84# define CHUNKSZ (64 * 1024)
85#endif
86
wdenka0ebde52004-09-08 22:03:11 +000087int gunzip (void *, int, unsigned char *, unsigned long *);
wdenk47d1a6e2002-11-03 00:01:44 +000088
89static void *zalloc(void *, unsigned, unsigned);
90static void zfree(void *, void *, unsigned);
91
92#if (CONFIG_COMMANDS & CFG_CMD_IMI)
93static int image_info (unsigned long addr);
94#endif
wdenk874ac262003-07-24 23:38:38 +000095
96#if (CONFIG_COMMANDS & CFG_CMD_IMLS)
97#include <flash.h>
Marian Balakowicz513b4a12005-10-11 19:09:42 +020098extern flash_info_t flash_info[]; /* info for FLASH chips */
wdenk874ac262003-07-24 23:38:38 +000099static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
100#endif
101
wdenk47d1a6e2002-11-03 00:01:44 +0000102static void print_type (image_header_t *hdr);
103
wdenk591dda52002-11-18 00:14:45 +0000104#ifdef __I386__
105image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
106#endif
107
wdenk47d1a6e2002-11-03 00:01:44 +0000108/*
109 * Continue booting an OS image; caller already has:
110 * - copied image header to global variable `header'
111 * - checked header magic number, checksums (both header & image),
112 * - verified image architecture (PPC) and type (KERNEL or MULTI),
113 * - loaded (first part of) image to header load address,
114 * - disabled interrupts.
115 */
116typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
117 int argc, char *argv[],
118 ulong addr, /* of image to boot */
119 ulong *len_ptr, /* multi-file image length table */
120 int verify); /* getenv("verify")[0] != 'n' */
121
wdenk57b2d802003-06-27 21:31:46 +0000122#ifdef DEBUG
123extern int do_bdinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
124#endif
125
wdenk591dda52002-11-18 00:14:45 +0000126#ifdef CONFIG_PPC
wdenk47d1a6e2002-11-03 00:01:44 +0000127static boot_os_Fcn do_bootm_linux;
128#else
129extern boot_os_Fcn do_bootm_linux;
130#endif
wdenk808532a2003-10-10 10:05:42 +0000131#ifdef CONFIG_SILENT_CONSOLE
132static void fixup_silent_linux (void);
133#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000134static boot_os_Fcn do_bootm_netbsd;
wdenk92bbe3f2003-04-20 14:04:18 +0000135static boot_os_Fcn do_bootm_rtems;
wdenk47d1a6e2002-11-03 00:01:44 +0000136#if (CONFIG_COMMANDS & CFG_CMD_ELF)
137static boot_os_Fcn do_bootm_vxworks;
138static boot_os_Fcn do_bootm_qnxelf;
139int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
140int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
141#endif /* CFG_CMD_ELF */
wdenk0893c472003-05-20 14:25:27 +0000142#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
143static boot_os_Fcn do_bootm_artos;
144#endif
wdenke58b0dc2003-07-27 00:21:01 +0000145#ifdef CONFIG_LYNXKDI
146static boot_os_Fcn do_bootm_lynxkdi;
147extern void lynxkdi_boot( image_header_t * );
148#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000149
Stefan Roese5d5ce292006-03-13 11:16:36 +0100150#ifndef CFG_BOOTM_LEN
151#define CFG_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
152#endif
153
wdenk47d1a6e2002-11-03 00:01:44 +0000154image_header_t header;
155
156ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
157
158int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
159{
160 ulong iflag;
161 ulong addr;
162 ulong data, len, checksum;
163 ulong *len_ptr;
Stefan Roese5d5ce292006-03-13 11:16:36 +0100164 uint unc_len = CFG_BOOTM_LEN;
wdenk47d1a6e2002-11-03 00:01:44 +0000165 int i, verify;
166 char *name, *s;
wdenk655a0f92003-10-30 21:49:38 +0000167 int (*appl)(int, char *[]);
wdenk47d1a6e2002-11-03 00:01:44 +0000168 image_header_t *hdr = &header;
169
170 s = getenv ("verify");
171 verify = (s && (*s == 'n')) ? 0 : 1;
172
173 if (argc < 2) {
174 addr = load_addr;
175 } else {
176 addr = simple_strtoul(argv[1], NULL, 16);
177 }
178
179 SHOW_BOOT_PROGRESS (1);
180 printf ("## Booting image at %08lx ...\n", addr);
181
182 /* Copy header so we can blank CRC field for re-calculation */
wdenk381669a2003-06-16 23:50:08 +0000183#ifdef CONFIG_HAS_DATAFLASH
184 if (addr_dataflash(addr)){
185 read_dataflash(addr, sizeof(image_header_t), (char *)&header);
186 } else
187#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000188 memmove (&header, (char *)addr, sizeof(image_header_t));
189
190 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
wdenk591dda52002-11-18 00:14:45 +0000191#ifdef __I386__ /* correct image format not implemented yet - fake it */
192 if (fake_header(hdr, (void*)addr, -1) != NULL) {
193 /* to compensate for the addition below */
194 addr -= sizeof(image_header_t);
195 /* turnof verify,
196 * fake_header() does not fake the data crc
197 */
198 verify = 0;
199 } else
200#endif /* __I386__ */
201 {
wdenk42c05472004-03-23 22:14:11 +0000202 puts ("Bad Magic Number\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000203 SHOW_BOOT_PROGRESS (-1);
204 return 1;
wdenk591dda52002-11-18 00:14:45 +0000205 }
wdenk47d1a6e2002-11-03 00:01:44 +0000206 }
207 SHOW_BOOT_PROGRESS (2);
208
209 data = (ulong)&header;
210 len = sizeof(image_header_t);
211
212 checksum = ntohl(hdr->ih_hcrc);
213 hdr->ih_hcrc = 0;
214
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200215 if (crc32 (0, (uchar *)data, len) != checksum) {
wdenk42c05472004-03-23 22:14:11 +0000216 puts ("Bad Header Checksum\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000217 SHOW_BOOT_PROGRESS (-2);
218 return 1;
219 }
220 SHOW_BOOT_PROGRESS (3);
221
Wolfgang Denk63e44ff2005-10-06 01:50:50 +0200222#ifdef CONFIG_HAS_DATAFLASH
223 if (addr_dataflash(addr)){
224 len = ntohl(hdr->ih_size) + sizeof(image_header_t);
225 read_dataflash(addr, len, (char *)CFG_LOAD_ADDR);
226 addr = CFG_LOAD_ADDR;
227 }
228#endif
229
230
wdenk47d1a6e2002-11-03 00:01:44 +0000231 /* for multi-file images we need the data part, too */
wdenka983cc22003-10-26 22:52:58 +0000232 print_image_hdr ((image_header_t *)addr);
wdenk47d1a6e2002-11-03 00:01:44 +0000233
234 data = addr + sizeof(image_header_t);
235 len = ntohl(hdr->ih_size);
236
237 if (verify) {
wdenk42c05472004-03-23 22:14:11 +0000238 puts (" Verifying Checksum ... ");
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200239 if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000240 printf ("Bad Data CRC\n");
241 SHOW_BOOT_PROGRESS (-3);
242 return 1;
243 }
wdenk42c05472004-03-23 22:14:11 +0000244 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000245 }
246 SHOW_BOOT_PROGRESS (4);
247
248 len_ptr = (ulong *)data;
249
Wolfgang Denkb2846792007-03-22 00:13:12 +0100250#if defined(__ARM__)
wdenk591dda52002-11-18 00:14:45 +0000251 if (hdr->ih_arch != IH_CPU_ARM)
Wolfgang Denkb2846792007-03-22 00:13:12 +0100252#elif defined(__avr32__)
253 if (hdr->ih_arch != IH_CPU_AVR32)
254#elif defined(__bfin__)
255 if (hdr->ih_arch != IH_CPU_BLACKFIN)
wdenk591dda52002-11-18 00:14:45 +0000256#elif defined(__I386__)
257 if (hdr->ih_arch != IH_CPU_I386)
wdenkabf7a7c2003-12-08 01:34:36 +0000258#elif defined(__M68K__)
259 if (hdr->ih_arch != IH_CPU_M68K)
wdenk12490652004-04-18 21:13:41 +0000260#elif defined(__microblaze__)
261 if (hdr->ih_arch != IH_CPU_MICROBLAZE)
Wolfgang Denkb2846792007-03-22 00:13:12 +0100262#elif defined(__mips__)
263 if (hdr->ih_arch != IH_CPU_MIPS)
264#elif defined(__nios__)
265 if (hdr->ih_arch != IH_CPU_NIOS)
wdenkef3386f2004-10-10 21:27:30 +0000266#elif defined(__nios2__)
267 if (hdr->ih_arch != IH_CPU_NIOS2)
Wolfgang Denkb2846792007-03-22 00:13:12 +0100268#elif defined(__PPC__)
269 if (hdr->ih_arch != IH_CPU_PPC)
wdenk591dda52002-11-18 00:14:45 +0000270#else
271# error Unknown CPU type
272#endif
273 {
274 printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch);
wdenk47d1a6e2002-11-03 00:01:44 +0000275 SHOW_BOOT_PROGRESS (-4);
276 return 1;
277 }
278 SHOW_BOOT_PROGRESS (5);
279
280 switch (hdr->ih_type) {
wdenk655a0f92003-10-30 21:49:38 +0000281 case IH_TYPE_STANDALONE:
282 name = "Standalone Application";
283 /* A second argument overwrites the load address */
284 if (argc > 2) {
wdenkd1894de2005-06-20 10:17:34 +0000285 hdr->ih_load = htonl(simple_strtoul(argv[2], NULL, 16));
wdenk655a0f92003-10-30 21:49:38 +0000286 }
287 break;
288 case IH_TYPE_KERNEL:
289 name = "Kernel Image";
290 break;
wdenk1ebf41e2004-01-02 14:00:00 +0000291 case IH_TYPE_MULTI:
wdenk655a0f92003-10-30 21:49:38 +0000292 name = "Multi-File Image";
293 len = ntohl(len_ptr[0]);
294 /* OS kernel is always the first image */
295 data += 8; /* kernel_len + terminator */
296 for (i=1; len_ptr[i]; ++i)
297 data += 4;
298 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000299 default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
300 SHOW_BOOT_PROGRESS (-5);
301 return 1;
302 }
303 SHOW_BOOT_PROGRESS (6);
304
305 /*
306 * We have reached the point of no return: we are going to
307 * overwrite all exception vector code, so we cannot easily
308 * recover from any failures any more...
309 */
310
311 iflag = disable_interrupts();
312
wdenk452cfd62002-11-19 11:04:11 +0000313#ifdef CONFIG_AMIGAONEG3SE
314 /*
wdenk57b2d802003-06-27 21:31:46 +0000315 * We've possible left the caches enabled during
wdenk452cfd62002-11-19 11:04:11 +0000316 * bios emulation, so turn them off again
317 */
318 icache_disable();
319 invalidate_l1_instruction_cache();
320 flush_data_cache();
321 dcache_disable();
322#endif
323
wdenk47d1a6e2002-11-03 00:01:44 +0000324 switch (hdr->ih_comp) {
325 case IH_COMP_NONE:
wdenk591dda52002-11-18 00:14:45 +0000326 if(ntohl(hdr->ih_load) == addr) {
wdenk47d1a6e2002-11-03 00:01:44 +0000327 printf (" XIP %s ... ", name);
328 } else {
329#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
330 size_t l = len;
331 void *to = (void *)ntohl(hdr->ih_load);
332 void *from = (void *)data;
333
334 printf (" Loading %s ... ", name);
335
336 while (l > 0) {
337 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
338 WATCHDOG_RESET();
339 memmove (to, from, tail);
340 to += tail;
341 from += tail;
342 l -= tail;
343 }
344#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
345 memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
346#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
347 }
348 break;
349 case IH_COMP_GZIP:
350 printf (" Uncompressing %s ... ", name);
wdenk710e3502003-08-29 20:57:53 +0000351 if (gunzip ((void *)ntohl(hdr->ih_load), unc_len,
wdenka0ebde52004-09-08 22:03:11 +0000352 (uchar *)data, &len) != 0) {
wdenk42c05472004-03-23 22:14:11 +0000353 puts ("GUNZIP ERROR - must RESET board to recover\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000354 SHOW_BOOT_PROGRESS (-6);
355 do_reset (cmdtp, flag, argc, argv);
356 }
357 break;
wdenk710e3502003-08-29 20:57:53 +0000358#ifdef CONFIG_BZIP2
359 case IH_COMP_BZIP2:
360 printf (" Uncompressing %s ... ", name);
wdenk2cefd152004-02-08 22:55:38 +0000361 /*
362 * If we've got less than 4 MB of malloc() space,
363 * use slower decompression algorithm which requires
364 * at most 2300 KB of memory.
365 */
wdenk710e3502003-08-29 20:57:53 +0000366 i = BZ2_bzBuffToBuffDecompress ((char*)ntohl(hdr->ih_load),
wdenk2cefd152004-02-08 22:55:38 +0000367 &unc_len, (char *)data, len,
368 CFG_MALLOC_LEN < (4096 * 1024), 0);
wdenk710e3502003-08-29 20:57:53 +0000369 if (i != BZ_OK) {
370 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
371 SHOW_BOOT_PROGRESS (-6);
372 udelay(100000);
373 do_reset (cmdtp, flag, argc, argv);
374 }
375 break;
376#endif /* CONFIG_BZIP2 */
wdenk47d1a6e2002-11-03 00:01:44 +0000377 default:
378 if (iflag)
379 enable_interrupts();
380 printf ("Unimplemented compression type %d\n", hdr->ih_comp);
381 SHOW_BOOT_PROGRESS (-7);
382 return 1;
383 }
wdenk42c05472004-03-23 22:14:11 +0000384 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000385 SHOW_BOOT_PROGRESS (7);
386
387 switch (hdr->ih_type) {
388 case IH_TYPE_STANDALONE:
wdenk47d1a6e2002-11-03 00:01:44 +0000389 if (iflag)
390 enable_interrupts();
391
wdenk3f9ab982003-04-12 23:38:12 +0000392 /* load (and uncompress), but don't start if "autostart"
393 * is set to "no"
394 */
wdenk60164a82003-10-08 23:26:14 +0000395 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
396 char buf[32];
397 sprintf(buf, "%lX", len);
398 setenv("filesize", buf);
wdenk3f9ab982003-04-12 23:38:12 +0000399 return 0;
wdenk60164a82003-10-08 23:26:14 +0000400 }
wdenk655a0f92003-10-30 21:49:38 +0000401 appl = (int (*)(int, char *[]))ntohl(hdr->ih_ep);
402 (*appl)(argc-1, &argv[1]);
wdenk3f9ab982003-04-12 23:38:12 +0000403 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000404 case IH_TYPE_KERNEL:
405 case IH_TYPE_MULTI:
406 /* handled below */
407 break;
408 default:
409 if (iflag)
410 enable_interrupts();
411 printf ("Can't boot image type %d\n", hdr->ih_type);
412 SHOW_BOOT_PROGRESS (-8);
413 return 1;
414 }
415 SHOW_BOOT_PROGRESS (8);
416
417 switch (hdr->ih_os) {
418 default: /* handled by (original) Linux case */
419 case IH_OS_LINUX:
wdenk808532a2003-10-10 10:05:42 +0000420#ifdef CONFIG_SILENT_CONSOLE
421 fixup_silent_linux();
422#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000423 do_bootm_linux (cmdtp, flag, argc, argv,
424 addr, len_ptr, verify);
425 break;
426 case IH_OS_NETBSD:
427 do_bootm_netbsd (cmdtp, flag, argc, argv,
428 addr, len_ptr, verify);
429 break;
wdenke58b0dc2003-07-27 00:21:01 +0000430
431#ifdef CONFIG_LYNXKDI
432 case IH_OS_LYNXOS:
433 do_bootm_lynxkdi (cmdtp, flag, argc, argv,
434 addr, len_ptr, verify);
435 break;
436#endif
wdenk57b2d802003-06-27 21:31:46 +0000437
wdenk92bbe3f2003-04-20 14:04:18 +0000438 case IH_OS_RTEMS:
439 do_bootm_rtems (cmdtp, flag, argc, argv,
440 addr, len_ptr, verify);
441 break;
wdenk57b2d802003-06-27 21:31:46 +0000442
wdenk47d1a6e2002-11-03 00:01:44 +0000443#if (CONFIG_COMMANDS & CFG_CMD_ELF)
444 case IH_OS_VXWORKS:
445 do_bootm_vxworks (cmdtp, flag, argc, argv,
446 addr, len_ptr, verify);
447 break;
448 case IH_OS_QNX:
449 do_bootm_qnxelf (cmdtp, flag, argc, argv,
450 addr, len_ptr, verify);
451 break;
452#endif /* CFG_CMD_ELF */
wdenk0893c472003-05-20 14:25:27 +0000453#ifdef CONFIG_ARTOS
454 case IH_OS_ARTOS:
455 do_bootm_artos (cmdtp, flag, argc, argv,
456 addr, len_ptr, verify);
457 break;
458#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000459 }
460
461 SHOW_BOOT_PROGRESS (-9);
462#ifdef DEBUG
wdenk42c05472004-03-23 22:14:11 +0000463 puts ("\n## Control returned to monitor - resetting...\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000464 do_reset (cmdtp, flag, argc, argv);
465#endif
466 return 1;
467}
468
wdenkf287a242003-07-01 21:06:45 +0000469U_BOOT_CMD(
470 bootm, CFG_MAXARGS, 1, do_bootm,
wdenk57b2d802003-06-27 21:31:46 +0000471 "bootm - boot application image from memory\n",
472 "[addr [arg ...]]\n - boot application image stored in memory\n"
wdenk20dd2fa2004-11-21 00:06:33 +0000473 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
474 "\t'arg' can be the address of an initrd image\n"
Gerald Van Barend6abef42007-03-31 12:23:51 -0400475#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock6fb7c812006-06-28 10:41:37 -0500476 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
477 "\ta third argument is required which is the address of the of the\n"
478 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
479 "\tuse a '-' for the second argument. If you do not pass a third\n"
480 "\ta bd_info struct will be passed instead\n"
481#endif
wdenk57b2d802003-06-27 21:31:46 +0000482);
483
wdenk808532a2003-10-10 10:05:42 +0000484#ifdef CONFIG_SILENT_CONSOLE
485static void
486fixup_silent_linux ()
487{
wdenk808532a2003-10-10 10:05:42 +0000488 char buf[256], *start, *end;
489 char *cmdline = getenv ("bootargs");
490
491 /* Only fix cmdline when requested */
492 if (!(gd->flags & GD_FLG_SILENT))
493 return;
494
495 debug ("before silent fix-up: %s\n", cmdline);
496 if (cmdline) {
497 if ((start = strstr (cmdline, "console=")) != NULL) {
498 end = strchr (start, ' ');
499 strncpy (buf, cmdline, (start - cmdline + 8));
500 if (end)
501 strcpy (buf + (start - cmdline + 8), end);
502 else
503 buf[start - cmdline + 8] = '\0';
504 } else {
505 strcpy (buf, cmdline);
506 strcat (buf, " console=");
507 }
508 } else {
509 strcpy (buf, "console=");
510 }
511
512 setenv ("bootargs", buf);
513 debug ("after silent fix-up: %s\n", buf);
514}
515#endif /* CONFIG_SILENT_CONSOLE */
516
wdenk591dda52002-11-18 00:14:45 +0000517#ifdef CONFIG_PPC
Matthew McClintock4db69372006-08-23 13:32:45 -0500518static void __attribute__((noinline))
wdenk47d1a6e2002-11-03 00:01:44 +0000519do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
520 int argc, char *argv[],
521 ulong addr,
522 ulong *len_ptr,
523 int verify)
524{
wdenk47d1a6e2002-11-03 00:01:44 +0000525 ulong sp;
526 ulong len, checksum;
527 ulong initrd_start, initrd_end;
528 ulong cmd_start, cmd_end;
529 ulong initrd_high;
530 ulong data;
wdenk6f770ed2003-05-23 23:18:21 +0000531 int initrd_copy_to_ram = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000532 char *cmdline;
533 char *s;
534 bd_t *kbd;
535 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
536 image_header_t *hdr = &header;
Gerald Van Barend6abef42007-03-31 12:23:51 -0400537#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock16485a62006-08-16 10:54:09 -0500538 char *of_flat_tree = NULL;
Kumar Gala665915b2006-10-24 23:47:37 -0500539 ulong of_data = 0;
Wolfgang Denk27a5b0b2005-10-13 01:45:54 +0200540#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000541
542 if ((s = getenv ("initrd_high")) != NULL) {
543 /* a value of "no" or a similar string will act like 0,
544 * turning the "load high" feature off. This is intentional.
545 */
546 initrd_high = simple_strtoul(s, NULL, 16);
wdenk6f770ed2003-05-23 23:18:21 +0000547 if (initrd_high == ~0)
548 initrd_copy_to_ram = 0;
wdenk9dfa8d12002-12-08 09:53:23 +0000549 } else { /* not set, no restrictions to load high */
wdenk47d1a6e2002-11-03 00:01:44 +0000550 initrd_high = ~0;
551 }
552
wdenk56f94be2002-11-05 16:35:14 +0000553#ifdef CONFIG_LOGBUFFER
wdenk34b613e2002-12-17 01:51:00 +0000554 kbd=gd->bd;
wdenk9dfa8d12002-12-08 09:53:23 +0000555 /* Prevent initrd from overwriting logbuffer */
556 if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
557 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
558 debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
wdenk56f94be2002-11-05 16:35:14 +0000559#endif
560
wdenk47d1a6e2002-11-03 00:01:44 +0000561 /*
562 * Booting a (Linux) kernel image
563 *
564 * Allocate space for command line and board info - the
565 * address should be as high as possible within the reach of
566 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
567 * memory, which means far enough below the current stack
568 * pointer.
569 */
570
571 asm( "mr %0,1": "=r"(sp) : );
572
wdenk56f94be2002-11-05 16:35:14 +0000573 debug ("## Current stack ends at 0x%08lX ", sp);
574
wdenk47d1a6e2002-11-03 00:01:44 +0000575 sp -= 2048; /* just to be sure */
576 if (sp > CFG_BOOTMAPSZ)
577 sp = CFG_BOOTMAPSZ;
578 sp &= ~0xF;
579
wdenk56f94be2002-11-05 16:35:14 +0000580 debug ("=> set upper limit to 0x%08lX\n", sp);
581
wdenk47d1a6e2002-11-03 00:01:44 +0000582 cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
583 kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
584
585 if ((s = getenv("bootargs")) == NULL)
586 s = "";
587
588 strcpy (cmdline, s);
589
590 cmd_start = (ulong)&cmdline[0];
591 cmd_end = cmd_start + strlen(cmdline);
592
593 *kbd = *(gd->bd);
594
595#ifdef DEBUG
596 printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
597
598 do_bdinfo (NULL, 0, 0, NULL);
599#endif
600
601 if ((s = getenv ("clocks_in_mhz")) != NULL) {
602 /* convert all clock information to MHz */
603 kbd->bi_intfreq /= 1000000L;
604 kbd->bi_busfreq /= 1000000L;
wdenk337f5652004-10-28 00:09:35 +0000605#if defined(CONFIG_MPC8220)
wdenk20dd2fa2004-11-21 00:06:33 +0000606 kbd->bi_inpfreq /= 1000000L;
607 kbd->bi_pcifreq /= 1000000L;
608 kbd->bi_pevfreq /= 1000000L;
609 kbd->bi_flbfreq /= 1000000L;
610 kbd->bi_vcofreq /= 1000000L;
wdenk337f5652004-10-28 00:09:35 +0000611#endif
Jon Loeligerf5ad3782005-07-23 10:37:35 -0500612#if defined(CONFIG_CPM2)
wdenk47d1a6e2002-11-03 00:01:44 +0000613 kbd->bi_cpmfreq /= 1000000L;
614 kbd->bi_brgfreq /= 1000000L;
615 kbd->bi_sccfreq /= 1000000L;
616 kbd->bi_vco /= 1000000L;
Jon Loeligerf5ad3782005-07-23 10:37:35 -0500617#endif
wdenkbe9c1cb2004-02-24 02:00:03 +0000618#if defined(CONFIG_MPC5xxx)
wdenk21136db2003-07-16 21:53:01 +0000619 kbd->bi_ipbfreq /= 1000000L;
620 kbd->bi_pcifreq /= 1000000L;
wdenkbe9c1cb2004-02-24 02:00:03 +0000621#endif /* CONFIG_MPC5xxx */
wdenk47d1a6e2002-11-03 00:01:44 +0000622 }
623
Wolfgang Denkb6929f92006-03-12 01:59:35 +0100624 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong)) ntohl(hdr->ih_ep);
wdenk47d1a6e2002-11-03 00:01:44 +0000625
626 /*
627 * Check if there is an initrd image
628 */
Matthew McClintock6fb7c812006-06-28 10:41:37 -0500629
Gerald Van Barend6abef42007-03-31 12:23:51 -0400630#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock6fb7c812006-06-28 10:41:37 -0500631 /* Look for a '-' which indicates to ignore the ramdisk argument */
632 if (argc >= 3 && strcmp(argv[2], "-") == 0) {
633 debug ("Skipping initrd\n");
Matthew McClintock5b948822006-10-11 15:13:01 -0500634 len = data = 0;
Matthew McClintock6fb7c812006-06-28 10:41:37 -0500635 }
636 else
637#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000638 if (argc >= 3) {
Matthew McClintock6fb7c812006-06-28 10:41:37 -0500639 debug ("Not skipping initrd\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000640 SHOW_BOOT_PROGRESS (9);
641
642 addr = simple_strtoul(argv[2], NULL, 16);
643
644 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
645
646 /* Copy header so we can blank CRC field for re-calculation */
647 memmove (&header, (char *)addr, sizeof(image_header_t));
648
Wolfgang Denkb6929f92006-03-12 01:59:35 +0100649 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
wdenk42c05472004-03-23 22:14:11 +0000650 puts ("Bad Magic Number\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000651 SHOW_BOOT_PROGRESS (-10);
652 do_reset (cmdtp, flag, argc, argv);
653 }
654
655 data = (ulong)&header;
656 len = sizeof(image_header_t);
657
Wolfgang Denkb6929f92006-03-12 01:59:35 +0100658 checksum = ntohl(hdr->ih_hcrc);
wdenk47d1a6e2002-11-03 00:01:44 +0000659 hdr->ih_hcrc = 0;
660
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200661 if (crc32 (0, (uchar *)data, len) != checksum) {
wdenk42c05472004-03-23 22:14:11 +0000662 puts ("Bad Header Checksum\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000663 SHOW_BOOT_PROGRESS (-11);
664 do_reset (cmdtp, flag, argc, argv);
665 }
666
667 SHOW_BOOT_PROGRESS (10);
668
669 print_image_hdr (hdr);
670
671 data = addr + sizeof(image_header_t);
Wolfgang Denkb6929f92006-03-12 01:59:35 +0100672 len = ntohl(hdr->ih_size);
wdenk47d1a6e2002-11-03 00:01:44 +0000673
674 if (verify) {
675 ulong csum = 0;
676#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
677 ulong cdata = data, edata = cdata + len;
678#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
679
wdenk42c05472004-03-23 22:14:11 +0000680 puts (" Verifying Checksum ... ");
wdenk47d1a6e2002-11-03 00:01:44 +0000681
682#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
683
684 while (cdata < edata) {
685 ulong chunk = edata - cdata;
686
687 if (chunk > CHUNKSZ)
688 chunk = CHUNKSZ;
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200689 csum = crc32 (csum, (uchar *)cdata, chunk);
wdenk47d1a6e2002-11-03 00:01:44 +0000690 cdata += chunk;
691
692 WATCHDOG_RESET();
693 }
694#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Wolfgang Denk7fb52662005-10-13 16:45:02 +0200695 csum = crc32 (0, (uchar *)data, len);
wdenk47d1a6e2002-11-03 00:01:44 +0000696#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
697
Wolfgang Denkb6929f92006-03-12 01:59:35 +0100698 if (csum != ntohl(hdr->ih_dcrc)) {
wdenk42c05472004-03-23 22:14:11 +0000699 puts ("Bad Data CRC\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000700 SHOW_BOOT_PROGRESS (-12);
701 do_reset (cmdtp, flag, argc, argv);
702 }
wdenk42c05472004-03-23 22:14:11 +0000703 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000704 }
705
706 SHOW_BOOT_PROGRESS (11);
707
708 if ((hdr->ih_os != IH_OS_LINUX) ||
709 (hdr->ih_arch != IH_CPU_PPC) ||
710 (hdr->ih_type != IH_TYPE_RAMDISK) ) {
wdenk42c05472004-03-23 22:14:11 +0000711 puts ("No Linux PPC Ramdisk Image\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000712 SHOW_BOOT_PROGRESS (-13);
713 do_reset (cmdtp, flag, argc, argv);
714 }
715
716 /*
717 * Now check if we have a multifile image
718 */
719 } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
720 u_long tail = ntohl(len_ptr[0]) % 4;
721 int i;
722
723 SHOW_BOOT_PROGRESS (13);
724
725 /* skip kernel length and terminator */
726 data = (ulong)(&len_ptr[2]);
727 /* skip any additional image length fields */
728 for (i=1; len_ptr[i]; ++i)
729 data += 4;
730 /* add kernel length, and align */
731 data += ntohl(len_ptr[0]);
732 if (tail) {
733 data += 4 - tail;
734 }
735
736 len = ntohl(len_ptr[1]);
737
738 } else {
739 /*
740 * no initrd image
741 */
742 SHOW_BOOT_PROGRESS (14);
743
744 len = data = 0;
745 }
746
Gerald Van Barend6abef42007-03-31 12:23:51 -0400747#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock20453d62006-08-22 09:31:59 -0500748 if(argc > 3) {
Matthew McClintock6fb7c812006-06-28 10:41:37 -0500749 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
Matthew McClintock16485a62006-08-16 10:54:09 -0500750 hdr = (image_header_t *)of_flat_tree;
Gerald Van Barend6abef42007-03-31 12:23:51 -0400751#if defined(CONFIG_OF_LIBFDT)
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400752 if (fdt_check_header(of_flat_tree) == 0) {
Gerald Van Barend6abef42007-03-31 12:23:51 -0400753#else
754 if (*(ulong *)of_flat_tree == OF_DT_HEADER) {
755#endif
Matthew McClintock16485a62006-08-16 10:54:09 -0500756#ifndef CFG_NO_FLASH
Kumar Gala665915b2006-10-24 23:47:37 -0500757 if (addr2info((ulong)of_flat_tree) != NULL)
758 of_data = (ulong)of_flat_tree;
Matthew McClintock16485a62006-08-16 10:54:09 -0500759#endif
760 } else if (ntohl(hdr->ih_magic) == IH_MAGIC) {
761 printf("## Flat Device Tree Image at %08lX\n", hdr);
762 print_image_hdr(hdr);
763
764 if ((ntohl(hdr->ih_load) < ((unsigned long)hdr + ntohl(hdr->ih_size) + sizeof(hdr))) &&
765 ((ntohl(hdr->ih_load) + ntohl(hdr->ih_size)) > (unsigned long)hdr)) {
766 printf ("ERROR: Load address overwrites Flat Device Tree uImage\n");
767 return;
768 }
Matthew McClintock27ec2062006-08-22 09:23:55 -0500769
Matthew McClintock16485a62006-08-16 10:54:09 -0500770 printf(" Verifying Checksum ... ");
771 memmove (&header, (char *)hdr, sizeof(image_header_t));
772 checksum = ntohl(header.ih_hcrc);
773 header.ih_hcrc = 0;
774
775 if(checksum != crc32(0, (uchar *)&header, sizeof(image_header_t))) {
776 printf("ERROR: Flat Device Tree header checksum is invalid\n");
777 return;
778 }
779
780 checksum = ntohl(hdr->ih_dcrc);
781 addr = (ulong)((uchar *)(hdr) + sizeof(image_header_t));
782 len = ntohl(hdr->ih_size);
783
784 if(checksum != crc32(0, (uchar *)addr, len)) {
785 printf("ERROR: Flat Device Tree checksum is invalid\n");
786 return;
787 }
788 printf("OK\n");
789
790 if (ntohl(hdr->ih_type) != IH_TYPE_FLATDT) {
791 printf ("ERROR: uImage not Flat Device Tree type\n");
792 return;
793 }
794 if (ntohl(hdr->ih_comp) != IH_COMP_NONE) {
795 printf("ERROR: uImage is not uncompressed\n");
796 return;
797 }
Gerald Van Barend6abef42007-03-31 12:23:51 -0400798#if defined(CONFIG_OF_LIBFDT)
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400799 if (fdt_check_header(of_flat_tree + sizeof(image_header_t)) == 0) {
Gerald Van Barend6abef42007-03-31 12:23:51 -0400800#else
Matthew McClintock16485a62006-08-16 10:54:09 -0500801 if (*((ulong *)(of_flat_tree + sizeof(image_header_t))) != OF_DT_HEADER) {
Gerald Van Barend6abef42007-03-31 12:23:51 -0400802#endif
Matthew McClintock16485a62006-08-16 10:54:09 -0500803 printf ("ERROR: uImage data is not a flat device tree\n");
804 return;
805 }
Matthew McClintock27ec2062006-08-22 09:23:55 -0500806
807 memmove((void *)ntohl(hdr->ih_load),
Matthew McClintock16485a62006-08-16 10:54:09 -0500808 (void *)(of_flat_tree + sizeof(image_header_t)),
809 ntohl(hdr->ih_size));
810 of_flat_tree = (char *)ntohl(hdr->ih_load);
811 } else {
812 printf ("Did not find a flat flat device tree at address %08lX\n", of_flat_tree);
813 return;
814 }
815 printf (" Booting using flat device tree at 0x%x\n",
Matthew McClintock6fb7c812006-06-28 10:41:37 -0500816 of_flat_tree);
Kumar Gala665915b2006-10-24 23:47:37 -0500817 } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]) && (len_ptr[2])) {
818 u_long tail = ntohl(len_ptr[0]) % 4;
819 int i;
820
821 /* skip kernel length, initrd length, and terminator */
822 of_data = (ulong)(&len_ptr[3]);
823 /* skip any additional image length fields */
824 for (i=2; len_ptr[i]; ++i)
825 of_data += 4;
826 /* add kernel length, and align */
827 of_data += ntohl(len_ptr[0]);
828 if (tail) {
829 of_data += 4 - tail;
830 }
831
832 /* add initrd length, and align */
833 tail = ntohl(len_ptr[1]) % 4;
834 of_data += ntohl(len_ptr[1]);
835 if (tail) {
836 of_data += 4 - tail;
837 }
838
Gerald Van Barend6abef42007-03-31 12:23:51 -0400839#if defined(CONFIG_OF_LIBFDT)
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400840 if (fdt_check_header((void *)of_data) != 0) {
Gerald Van Barend6abef42007-03-31 12:23:51 -0400841#else
Kumar Gala665915b2006-10-24 23:47:37 -0500842 if (((struct boot_param_header *)of_data)->magic != OF_DT_HEADER) {
Gerald Van Barend6abef42007-03-31 12:23:51 -0400843#endif
Kumar Gala665915b2006-10-24 23:47:37 -0500844 printf ("ERROR: image is not a flat device tree\n");
845 return;
846 }
847
Gerald Van Barend6abef42007-03-31 12:23:51 -0400848#if defined(CONFIG_OF_LIBFDT)
849 if (be32_to_cpu(fdt_totalsize(of_data)) != ntohl(len_ptr[2])) {
850#else
Kumar Gala665915b2006-10-24 23:47:37 -0500851 if (((struct boot_param_header *)of_data)->totalsize != ntohl(len_ptr[2])) {
Gerald Van Barend6abef42007-03-31 12:23:51 -0400852#endif
Kumar Gala665915b2006-10-24 23:47:37 -0500853 printf ("ERROR: flat device tree size does not agree with image\n");
854 return;
855 }
Matthew McClintock6fb7c812006-06-28 10:41:37 -0500856 }
857#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000858 if (!data) {
wdenk56f94be2002-11-05 16:35:14 +0000859 debug ("No initrd\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000860 }
wdenk47d1a6e2002-11-03 00:01:44 +0000861
862 if (data) {
wdenk6f770ed2003-05-23 23:18:21 +0000863 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
864 initrd_start = data;
865 initrd_end = initrd_start + len;
866 } else {
wdenk47d1a6e2002-11-03 00:01:44 +0000867 initrd_start = (ulong)kbd - len;
868 initrd_start &= ~(4096 - 1); /* align on page */
869
870 if (initrd_high) {
871 ulong nsp;
872
873 /*
874 * the inital ramdisk does not need to be within
875 * CFG_BOOTMAPSZ as it is not accessed until after
876 * the mm system is initialised.
877 *
878 * do the stack bottom calculation again and see if
879 * the initrd will fit just below the monitor stack
880 * bottom without overwriting the area allocated
881 * above for command line args and board info.
882 */
883 asm( "mr %0,1": "=r"(nsp) : );
884 nsp -= 2048; /* just to be sure */
885 nsp &= ~0xF;
886 if (nsp > initrd_high) /* limit as specified */
887 nsp = initrd_high;
888 nsp -= len;
889 nsp &= ~(4096 - 1); /* align on page */
890 if (nsp >= sp)
891 initrd_start = nsp;
892 }
893
894 SHOW_BOOT_PROGRESS (12);
wdenk56f94be2002-11-05 16:35:14 +0000895
896 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
wdenk47d1a6e2002-11-03 00:01:44 +0000897 data, data + len - 1, len, len);
wdenk56f94be2002-11-05 16:35:14 +0000898
wdenk47d1a6e2002-11-03 00:01:44 +0000899 initrd_end = initrd_start + len;
900 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
901 initrd_start, initrd_end);
902#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
903 {
904 size_t l = len;
905 void *to = (void *)initrd_start;
906 void *from = (void *)data;
907
908 while (l > 0) {
909 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
910 WATCHDOG_RESET();
911 memmove (to, from, tail);
912 to += tail;
913 from += tail;
914 l -= tail;
915 }
916 }
917#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
918 memmove ((void *)initrd_start, (void *)data, len);
919#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
wdenk42c05472004-03-23 22:14:11 +0000920 puts ("OK\n");
wdenk6f770ed2003-05-23 23:18:21 +0000921 }
wdenk47d1a6e2002-11-03 00:01:44 +0000922 } else {
923 initrd_start = 0;
924 initrd_end = 0;
925 }
926
wdenk56f94be2002-11-05 16:35:14 +0000927 debug ("## Transferring control to Linux (at address %08lx) ...\n",
wdenk47d1a6e2002-11-03 00:01:44 +0000928 (ulong)kernel);
wdenk56f94be2002-11-05 16:35:14 +0000929
wdenk47d1a6e2002-11-03 00:01:44 +0000930 SHOW_BOOT_PROGRESS (15);
931
wdenk9c53f402003-10-15 23:53:47 +0000932#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
wdenk47d1a6e2002-11-03 00:01:44 +0000933 unlock_ram_in_cache();
934#endif
Wolfgang Denk27a5b0b2005-10-13 01:45:54 +0200935
Gerald Van Barend6abef42007-03-31 12:23:51 -0400936#if defined(CONFIG_OF_LIBFDT)
Kumar Gala665915b2006-10-24 23:47:37 -0500937 /* move of_flat_tree if needed */
938 if (of_data) {
Gerald Van Barend6abef42007-03-31 12:23:51 -0400939 int err;
940 ulong of_start, of_len;
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400941
Gerald Van Barend6abef42007-03-31 12:23:51 -0400942 of_len = be32_to_cpu(fdt_totalsize(of_data));
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400943 /* position on a 4K boundary before the initrd/kbd */
Gerald Van Barend6abef42007-03-31 12:23:51 -0400944 if (initrd_start)
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400945 of_start = initrd_start - of_len;
Gerald Van Barend6abef42007-03-31 12:23:51 -0400946 else
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400947 of_start = (ulong)kbd - of_len;
Gerald Van Barend6abef42007-03-31 12:23:51 -0400948 of_start &= ~(4096 - 1); /* align on page */
949 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
950 of_data, of_data + of_len - 1, of_len, of_len);
951
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400952 of_flat_tree = (char *)of_start;
Gerald Van Barend6abef42007-03-31 12:23:51 -0400953 printf (" Loading Device Tree to %08lx, end %08lx ... ",
954 of_start, of_start + of_len - 1);
Gerald Van Barenc4a57ea2007-04-06 14:19:43 -0400955 err = fdt_open_into((void *)of_start, (void *)of_data, of_len);
Gerald Van Barend6abef42007-03-31 12:23:51 -0400956 if (err != 0) {
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400957 printf ("libfdt: %s " __FILE__ " %d\n", fdt_strerror(err), __LINE__);
Gerald Van Barend6abef42007-03-31 12:23:51 -0400958 }
Gerald Van Barenc9502b92007-04-14 22:51:24 -0400959 /*
960 * Add the chosen node if it doesn't exist, add the env and bd_t
961 * if the user wants it (the logic is in the subroutines).
962 */
963 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
964 printf("Failed creating the /chosen node (0x%08X), aborting.\n", of_flat_tree);
965 return;
966 }
967#ifdef CONFIG_OF_HAS_UBOOT_ENV
968 if (fdt_env(of_flat_tree) < 0) {
969 printf("Failed creating the /u-boot-env node, aborting.\n");
970 return;
971 }
972#endif
973#ifdef CONFIG_OF_HAS_BD_T
974 if (fdt_bd_t(of_flat_tree) < 0) {
975 printf("Failed creating the /bd_t node, aborting.\n");
976 return;
977 }
978#endif
Gerald Van Barend6abef42007-03-31 12:23:51 -0400979 }
980#endif
981#if defined(CONFIG_OF_FLAT_TREE)
982 /* move of_flat_tree if needed */
983 if (of_data) {
Kumar Gala665915b2006-10-24 23:47:37 -0500984 ulong of_start, of_len;
985 of_len = ((struct boot_param_header *)of_data)->totalsize;
986 /* provide extra 8k pad */
987 if (initrd_start)
988 of_start = initrd_start - of_len - 8192;
989 else
990 of_start = (ulong)kbd - of_len - 8192;
991 of_start &= ~(4096 - 1); /* align on page */
992 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
993 of_data, of_data + of_len - 1, of_len, of_len);
994
995 of_flat_tree = (char *)of_start;
996 printf (" Loading Device Tree to %08lx, end %08lx ... ",
997 of_start, of_start + of_len - 1);
998 memmove ((void *)of_start, (void *)of_data, of_len);
999 }
Stefan Roese208d38a2006-11-27 17:04:06 +01001000#endif
Wolfgang Denk27a5b0b2005-10-13 01:45:54 +02001001
Stefan Roese208d38a2006-11-27 17:04:06 +01001002 /*
1003 * Linux Kernel Parameters (passing board info data):
1004 * r3: ptr to board info data
1005 * r4: initrd_start or 0 if no initrd
1006 * r5: initrd_end - unused if r4 is 0
1007 * r6: Start of command line string
1008 * r7: End of command line string
1009 */
Gerald Van Barend6abef42007-03-31 12:23:51 -04001010#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Stefan Roese208d38a2006-11-27 17:04:06 +01001011 if (!of_flat_tree) /* no device tree; boot old style */
Wolfgang Denk27a5b0b2005-10-13 01:45:54 +02001012#endif
Stefan Roese208d38a2006-11-27 17:04:06 +01001013 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
1014 /* does not return */
1015
Gerald Van Barend6abef42007-03-31 12:23:51 -04001016#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Wolfgang Denk27a5b0b2005-10-13 01:45:54 +02001017 /*
Stefan Roese208d38a2006-11-27 17:04:06 +01001018 * Linux Kernel Parameters (passing device tree):
Wolfgang Denk27a5b0b2005-10-13 01:45:54 +02001019 * r3: ptr to OF flat tree, followed by the board info data
Kumar Galaa8b76de2006-01-11 16:41:35 -06001020 * r4: physical pointer to the kernel itself
1021 * r5: NULL
1022 * r6: NULL
1023 * r7: NULL
Wolfgang Denk27a5b0b2005-10-13 01:45:54 +02001024 */
Gerald Van Barend6abef42007-03-31 12:23:51 -04001025#if defined(CONFIG_OF_FLAT_TREE)
Stefan Roese208d38a2006-11-27 17:04:06 +01001026 ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
1027 /* ft_dump_blob(of_flat_tree); */
Gerald Van Barend6abef42007-03-31 12:23:51 -04001028#endif
Gerald Van Barenc9502b92007-04-14 22:51:24 -04001029#if defined(CONFIG_OF_LIBFDT)
1030 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
1031 printf("Failed creating the /chosen node (0x%08X), aborting.\n", of_flat_tree);
1032 return;
1033 }
1034#ifdef CONFIG_OF_HAS_UBOOT_ENV
1035 if (fdt_env(of_flat_tree) < 0) {
1036 printf("Failed creating the /u-boot-env node, aborting.\n");
1037 return;
1038 }
1039#endif
1040#ifdef CONFIG_OF_HAS_BD_T
1041 if (fdt_bd_t(of_flat_tree) < 0) {
1042 printf("Failed creating the /bd_t node, aborting.\n");
1043 return;
1044 }
1045#endif
1046#endif /* if defined(CONFIG_OF_LIBFDT) */
Stefan Roese208d38a2006-11-27 17:04:06 +01001047
1048 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
1049#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001050}
wdenk1fe2c702003-03-06 21:55:29 +00001051#endif /* CONFIG_PPC */
wdenk47d1a6e2002-11-03 00:01:44 +00001052
1053static void
1054do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
1055 int argc, char *argv[],
1056 ulong addr,
1057 ulong *len_ptr,
1058 int verify)
1059{
wdenk47d1a6e2002-11-03 00:01:44 +00001060 image_header_t *hdr = &header;
1061
1062 void (*loader)(bd_t *, image_header_t *, char *, char *);
1063 image_header_t *img_addr;
1064 char *consdev;
1065 char *cmdline;
1066
1067
1068 /*
1069 * Booting a (NetBSD) kernel image
1070 *
1071 * This process is pretty similar to a standalone application:
1072 * The (first part of an multi-) image must be a stage-2 loader,
1073 * which in turn is responsible for loading & invoking the actual
1074 * kernel. The only differences are the parameters being passed:
1075 * besides the board info strucure, the loader expects a command
1076 * line, the name of the console device, and (optionally) the
1077 * address of the original image header.
1078 */
1079
1080 img_addr = 0;
1081 if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
1082 img_addr = (image_header_t *) addr;
1083
1084
1085 consdev = "";
1086#if defined (CONFIG_8xx_CONS_SMC1)
1087 consdev = "smc1";
1088#elif defined (CONFIG_8xx_CONS_SMC2)
1089 consdev = "smc2";
1090#elif defined (CONFIG_8xx_CONS_SCC2)
1091 consdev = "scc2";
1092#elif defined (CONFIG_8xx_CONS_SCC3)
1093 consdev = "scc3";
1094#endif
1095
1096 if (argc > 2) {
1097 ulong len;
1098 int i;
1099
1100 for (i=2, len=0 ; i<argc ; i+=1)
1101 len += strlen (argv[i]) + 1;
1102 cmdline = malloc (len);
1103
1104 for (i=2, len=0 ; i<argc ; i+=1) {
1105 if (i > 2)
1106 cmdline[len++] = ' ';
1107 strcpy (&cmdline[len], argv[i]);
1108 len += strlen (argv[i]);
1109 }
1110 } else if ((cmdline = getenv("bootargs")) == NULL) {
1111 cmdline = "";
1112 }
1113
Wolfgang Denkb6929f92006-03-12 01:59:35 +01001114 loader = (void (*)(bd_t *, image_header_t *, char *, char *)) ntohl(hdr->ih_ep);
wdenk47d1a6e2002-11-03 00:01:44 +00001115
1116 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
1117 (ulong)loader);
1118
1119 SHOW_BOOT_PROGRESS (15);
1120
1121 /*
1122 * NetBSD Stage-2 Loader Parameters:
1123 * r3: ptr to board info data
1124 * r4: image address
1125 * r5: console device
1126 * r6: boot args string
1127 */
1128 (*loader) (gd->bd, img_addr, consdev, cmdline);
1129}
wdenk0893c472003-05-20 14:25:27 +00001130
1131#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
1132
1133/* Function that returns a character from the environment */
1134extern uchar (*env_get_char)(int);
1135
1136static void
1137do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
1138 int argc, char *argv[],
1139 ulong addr,
1140 ulong *len_ptr,
1141 int verify)
1142{
wdenk0893c472003-05-20 14:25:27 +00001143 ulong top;
1144 char *s, *cmdline;
1145 char **fwenv, **ss;
1146 int i, j, nxt, len, envno, envsz;
1147 bd_t *kbd;
1148 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
1149 image_header_t *hdr = &header;
1150
1151 /*
1152 * Booting an ARTOS kernel image + application
1153 */
1154
1155 /* this used to be the top of memory, but was wrong... */
1156#ifdef CONFIG_PPC
1157 /* get stack pointer */
1158 asm volatile ("mr %0,1" : "=r"(top) );
1159#endif
1160 debug ("## Current stack ends at 0x%08lX ", top);
1161
1162 top -= 2048; /* just to be sure */
1163 if (top > CFG_BOOTMAPSZ)
1164 top = CFG_BOOTMAPSZ;
1165 top &= ~0xF;
1166
1167 debug ("=> set upper limit to 0x%08lX\n", top);
1168
1169 /* first check the artos specific boot args, then the linux args*/
1170 if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
1171 s = "";
1172
1173 /* get length of cmdline, and place it */
1174 len = strlen(s);
1175 top = (top - (len + 1)) & ~0xF;
1176 cmdline = (char *)top;
1177 debug ("## cmdline at 0x%08lX ", top);
1178 strcpy(cmdline, s);
1179
1180 /* copy bdinfo */
1181 top = (top - sizeof(bd_t)) & ~0xF;
1182 debug ("## bd at 0x%08lX ", top);
1183 kbd = (bd_t *)top;
1184 memcpy(kbd, gd->bd, sizeof(bd_t));
1185
1186 /* first find number of env entries, and their size */
1187 envno = 0;
1188 envsz = 0;
1189 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1190 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1191 ;
1192 envno++;
1193 envsz += (nxt - i) + 1; /* plus trailing zero */
1194 }
1195 envno++; /* plus the terminating zero */
1196 debug ("## %u envvars total size %u ", envno, envsz);
1197
1198 top = (top - sizeof(char **)*envno) & ~0xF;
1199 fwenv = (char **)top;
1200 debug ("## fwenv at 0x%08lX ", top);
1201
1202 top = (top - envsz) & ~0xF;
1203 s = (char *)top;
1204 ss = fwenv;
1205
1206 /* now copy them */
1207 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1208 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1209 ;
1210 *ss++ = s;
1211 for (j = i; j < nxt; ++j)
1212 *s++ = env_get_char(j);
1213 *s++ = '\0';
1214 }
1215 *ss++ = NULL; /* terminate */
1216
1217 entry = (void (*)(bd_t *, char *, char **, ulong))ntohl(hdr->ih_ep);
1218 (*entry)(kbd, cmdline, fwenv, top);
1219}
1220#endif
1221
wdenk47d1a6e2002-11-03 00:01:44 +00001222
1223#if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
1224int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1225{
1226 int rcode = 0;
1227#ifndef CFG_HUSH_PARSER
1228 if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
1229#else
1230 if (parse_string_outer(getenv("bootcmd"),
1231 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
1232#endif
1233 return rcode;
1234}
wdenk57b2d802003-06-27 21:31:46 +00001235
wdenkf287a242003-07-01 21:06:45 +00001236U_BOOT_CMD(
1237 boot, 1, 1, do_bootd,
wdenk3086a972003-06-28 23:11:04 +00001238 "boot - boot default, i.e., run 'bootcmd'\n",
1239 NULL
1240);
1241
1242/* keep old command name "bootd" for backward compatibility */
wdenkf287a242003-07-01 21:06:45 +00001243U_BOOT_CMD(
1244 bootd, 1, 1, do_bootd,
wdenk57b2d802003-06-27 21:31:46 +00001245 "bootd - boot default, i.e., run 'bootcmd'\n",
1246 NULL
1247);
1248
wdenk47d1a6e2002-11-03 00:01:44 +00001249#endif
1250
1251#if (CONFIG_COMMANDS & CFG_CMD_IMI)
1252int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1253{
1254 int arg;
1255 ulong addr;
1256 int rcode=0;
1257
1258 if (argc < 2) {
1259 return image_info (load_addr);
1260 }
1261
1262 for (arg=1; arg <argc; ++arg) {
1263 addr = simple_strtoul(argv[arg], NULL, 16);
1264 if (image_info (addr) != 0) rcode = 1;
1265 }
1266 return rcode;
1267}
1268
1269static int image_info (ulong addr)
1270{
1271 ulong data, len, checksum;
1272 image_header_t *hdr = &header;
1273
1274 printf ("\n## Checking Image at %08lx ...\n", addr);
1275
1276 /* Copy header so we can blank CRC field for re-calculation */
1277 memmove (&header, (char *)addr, sizeof(image_header_t));
1278
1279 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
wdenk42c05472004-03-23 22:14:11 +00001280 puts (" Bad Magic Number\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001281 return 1;
1282 }
1283
1284 data = (ulong)&header;
1285 len = sizeof(image_header_t);
1286
1287 checksum = ntohl(hdr->ih_hcrc);
1288 hdr->ih_hcrc = 0;
1289
Wolfgang Denk7fb52662005-10-13 16:45:02 +02001290 if (crc32 (0, (uchar *)data, len) != checksum) {
wdenk42c05472004-03-23 22:14:11 +00001291 puts (" Bad Header Checksum\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001292 return 1;
1293 }
1294
1295 /* for multi-file images we need the data part, too */
1296 print_image_hdr ((image_header_t *)addr);
1297
1298 data = addr + sizeof(image_header_t);
1299 len = ntohl(hdr->ih_size);
1300
wdenk42c05472004-03-23 22:14:11 +00001301 puts (" Verifying Checksum ... ");
Wolfgang Denk7fb52662005-10-13 16:45:02 +02001302 if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
wdenk42c05472004-03-23 22:14:11 +00001303 puts (" Bad Data CRC\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001304 return 1;
1305 }
wdenk42c05472004-03-23 22:14:11 +00001306 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001307 return 0;
1308}
wdenkf287a242003-07-01 21:06:45 +00001309
1310U_BOOT_CMD(
1311 iminfo, CFG_MAXARGS, 1, do_iminfo,
wdenk57b2d802003-06-27 21:31:46 +00001312 "iminfo - print header information for application image\n",
1313 "addr [addr ...]\n"
1314 " - print header information for application image starting at\n"
1315 " address 'addr' in memory; this includes verification of the\n"
1316 " image contents (magic number, header and payload checksums)\n"
1317);
1318
wdenk47d1a6e2002-11-03 00:01:44 +00001319#endif /* CFG_CMD_IMI */
1320
wdenk874ac262003-07-24 23:38:38 +00001321#if (CONFIG_COMMANDS & CFG_CMD_IMLS)
1322/*-----------------------------------------------------------------------
1323 * List all images found in flash.
1324 */
1325int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1326{
1327 flash_info_t *info;
1328 int i, j;
1329 image_header_t *hdr;
wdenkc6abb7e2003-11-17 21:14:37 +00001330 ulong data, len, checksum;
wdenk874ac262003-07-24 23:38:38 +00001331
1332 for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
1333 if (info->flash_id == FLASH_UNKNOWN)
1334 goto next_bank;
Marian Balakowicz513b4a12005-10-11 19:09:42 +02001335 for (j=0; j<info->sector_count; ++j) {
wdenk874ac262003-07-24 23:38:38 +00001336
1337 if (!(hdr=(image_header_t *)info->start[j]) ||
1338 (ntohl(hdr->ih_magic) != IH_MAGIC))
1339 goto next_sector;
1340
1341 /* Copy header so we can blank CRC field for re-calculation */
1342 memmove (&header, (char *)hdr, sizeof(image_header_t));
1343
1344 checksum = ntohl(header.ih_hcrc);
1345 header.ih_hcrc = 0;
1346
Wolfgang Denk7fb52662005-10-13 16:45:02 +02001347 if (crc32 (0, (uchar *)&header, sizeof(image_header_t))
wdenk874ac262003-07-24 23:38:38 +00001348 != checksum)
1349 goto next_sector;
1350
1351 printf ("Image at %08lX:\n", (ulong)hdr);
1352 print_image_hdr( hdr );
wdenkc6abb7e2003-11-17 21:14:37 +00001353
1354 data = (ulong)hdr + sizeof(image_header_t);
1355 len = ntohl(hdr->ih_size);
1356
wdenk42c05472004-03-23 22:14:11 +00001357 puts (" Verifying Checksum ... ");
Wolfgang Denk7fb52662005-10-13 16:45:02 +02001358 if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
wdenk42c05472004-03-23 22:14:11 +00001359 puts (" Bad Data CRC\n");
wdenkc6abb7e2003-11-17 21:14:37 +00001360 }
wdenk42c05472004-03-23 22:14:11 +00001361 puts ("OK\n");
wdenk0a658552003-08-05 17:43:17 +00001362next_sector: ;
wdenk874ac262003-07-24 23:38:38 +00001363 }
wdenk0a658552003-08-05 17:43:17 +00001364next_bank: ;
wdenk874ac262003-07-24 23:38:38 +00001365 }
1366
1367 return (0);
1368}
1369
1370U_BOOT_CMD(
1371 imls, 1, 1, do_imls,
1372 "imls - list all images found in flash\n",
1373 "\n"
1374 " - Prints information about all images found at sector\n"
1375 " boundaries in flash.\n"
1376);
1377#endif /* CFG_CMD_IMLS */
1378
wdenk47d1a6e2002-11-03 00:01:44 +00001379void
1380print_image_hdr (image_header_t *hdr)
1381{
1382#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
1383 time_t timestamp = (time_t)ntohl(hdr->ih_time);
1384 struct rtc_time tm;
1385#endif
1386
1387 printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
1388#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
1389 to_tm (timestamp, &tm);
1390 printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
1391 tm.tm_year, tm.tm_mon, tm.tm_mday,
1392 tm.tm_hour, tm.tm_min, tm.tm_sec);
1393#endif /* CFG_CMD_DATE, CONFIG_TIMESTAMP */
wdenk42c05472004-03-23 22:14:11 +00001394 puts (" Image Type: "); print_type(hdr);
1395 printf ("\n Data Size: %d Bytes = ", ntohl(hdr->ih_size));
wdenk47d1a6e2002-11-03 00:01:44 +00001396 print_size (ntohl(hdr->ih_size), "\n");
wdenk42c05472004-03-23 22:14:11 +00001397 printf (" Load Address: %08x\n"
1398 " Entry Point: %08x\n",
1399 ntohl(hdr->ih_load), ntohl(hdr->ih_ep));
wdenk47d1a6e2002-11-03 00:01:44 +00001400
1401 if (hdr->ih_type == IH_TYPE_MULTI) {
1402 int i;
1403 ulong len;
1404 ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
1405
wdenk42c05472004-03-23 22:14:11 +00001406 puts (" Contents:\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001407 for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
1408 printf (" Image %d: %8ld Bytes = ", i, len);
1409 print_size (len, "\n");
1410 }
1411 }
1412}
1413
1414
1415static void
1416print_type (image_header_t *hdr)
1417{
1418 char *os, *arch, *type, *comp;
1419
1420 switch (hdr->ih_os) {
1421 case IH_OS_INVALID: os = "Invalid OS"; break;
1422 case IH_OS_NETBSD: os = "NetBSD"; break;
1423 case IH_OS_LINUX: os = "Linux"; break;
1424 case IH_OS_VXWORKS: os = "VxWorks"; break;
1425 case IH_OS_QNX: os = "QNX"; break;
1426 case IH_OS_U_BOOT: os = "U-Boot"; break;
wdenk92bbe3f2003-04-20 14:04:18 +00001427 case IH_OS_RTEMS: os = "RTEMS"; break;
wdenk0893c472003-05-20 14:25:27 +00001428#ifdef CONFIG_ARTOS
1429 case IH_OS_ARTOS: os = "ARTOS"; break;
1430#endif
wdenke58b0dc2003-07-27 00:21:01 +00001431#ifdef CONFIG_LYNXKDI
1432 case IH_OS_LYNXOS: os = "LynxOS"; break;
1433#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001434 default: os = "Unknown OS"; break;
1435 }
1436
1437 switch (hdr->ih_arch) {
1438 case IH_CPU_INVALID: arch = "Invalid CPU"; break;
1439 case IH_CPU_ALPHA: arch = "Alpha"; break;
1440 case IH_CPU_ARM: arch = "ARM"; break;
Stefan Roesedf86f152006-10-09 12:55:38 +02001441 case IH_CPU_AVR32: arch = "AVR32"; break;
Wolfgang Denkb2846792007-03-22 00:13:12 +01001442 case IH_CPU_BLACKFIN: arch = "Blackfin"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001443 case IH_CPU_I386: arch = "Intel x86"; break;
1444 case IH_CPU_IA64: arch = "IA64"; break;
Wolfgang Denkb2846792007-03-22 00:13:12 +01001445 case IH_CPU_M68K: arch = "M68K"; break;
1446 case IH_CPU_MICROBLAZE: arch = "Microblaze"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001447 case IH_CPU_MIPS64: arch = "MIPS 64 Bit"; break;
Wolfgang Denkb2846792007-03-22 00:13:12 +01001448 case IH_CPU_MIPS: arch = "MIPS"; break;
1449 case IH_CPU_NIOS2: arch = "Nios-II"; break;
1450 case IH_CPU_NIOS: arch = "Nios"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001451 case IH_CPU_PPC: arch = "PowerPC"; break;
1452 case IH_CPU_S390: arch = "IBM S390"; break;
1453 case IH_CPU_SH: arch = "SuperH"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001454 case IH_CPU_SPARC64: arch = "SPARC 64 Bit"; break;
Wolfgang Denkb2846792007-03-22 00:13:12 +01001455 case IH_CPU_SPARC: arch = "SPARC"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001456 default: arch = "Unknown Architecture"; break;
1457 }
1458
1459 switch (hdr->ih_type) {
1460 case IH_TYPE_INVALID: type = "Invalid Image"; break;
1461 case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
1462 case IH_TYPE_KERNEL: type = "Kernel Image"; break;
1463 case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
1464 case IH_TYPE_MULTI: type = "Multi-File Image"; break;
1465 case IH_TYPE_FIRMWARE: type = "Firmware"; break;
1466 case IH_TYPE_SCRIPT: type = "Script"; break;
Matthew McClintock16485a62006-08-16 10:54:09 -05001467 case IH_TYPE_FLATDT: type = "Flat Device Tree"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001468 default: type = "Unknown Image"; break;
1469 }
1470
1471 switch (hdr->ih_comp) {
1472 case IH_COMP_NONE: comp = "uncompressed"; break;
1473 case IH_COMP_GZIP: comp = "gzip compressed"; break;
1474 case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
1475 default: comp = "unknown compression"; break;
1476 }
1477
1478 printf ("%s %s %s (%s)", arch, os, type, comp);
1479}
1480
1481#define ZALLOC_ALIGNMENT 16
1482
1483static void *zalloc(void *x, unsigned items, unsigned size)
1484{
1485 void *p;
1486
1487 size *= items;
1488 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
1489
1490 p = malloc (size);
1491
1492 return (p);
1493}
1494
1495static void zfree(void *x, void *addr, unsigned nb)
1496{
1497 free (addr);
1498}
1499
1500#define HEAD_CRC 2
1501#define EXTRA_FIELD 4
1502#define ORIG_NAME 8
1503#define COMMENT 0x10
1504#define RESERVED 0xe0
1505
1506#define DEFLATED 8
1507
wdenka0ebde52004-09-08 22:03:11 +00001508int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
wdenk47d1a6e2002-11-03 00:01:44 +00001509{
1510 z_stream s;
1511 int r, i, flags;
1512
1513 /* skip header */
1514 i = 10;
1515 flags = src[3];
1516 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
wdenk42c05472004-03-23 22:14:11 +00001517 puts ("Error: Bad gzipped data\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001518 return (-1);
1519 }
1520 if ((flags & EXTRA_FIELD) != 0)
1521 i = 12 + src[10] + (src[11] << 8);
1522 if ((flags & ORIG_NAME) != 0)
1523 while (src[i++] != 0)
1524 ;
1525 if ((flags & COMMENT) != 0)
1526 while (src[i++] != 0)
1527 ;
1528 if ((flags & HEAD_CRC) != 0)
1529 i += 2;
1530 if (i >= *lenp) {
wdenk42c05472004-03-23 22:14:11 +00001531 puts ("Error: gunzip out of data in header\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001532 return (-1);
1533 }
1534
1535 s.zalloc = zalloc;
1536 s.zfree = zfree;
1537#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
1538 s.outcb = (cb_func)WATCHDOG_RESET;
1539#else
1540 s.outcb = Z_NULL;
1541#endif /* CONFIG_HW_WATCHDOG */
1542
1543 r = inflateInit2(&s, -MAX_WBITS);
1544 if (r != Z_OK) {
1545 printf ("Error: inflateInit2() returned %d\n", r);
1546 return (-1);
1547 }
1548 s.next_in = src + i;
1549 s.avail_in = *lenp - i;
1550 s.next_out = dst;
1551 s.avail_out = dstlen;
1552 r = inflate(&s, Z_FINISH);
1553 if (r != Z_OK && r != Z_STREAM_END) {
1554 printf ("Error: inflate() returned %d\n", r);
1555 return (-1);
1556 }
1557 *lenp = s.next_out - (unsigned char *) dst;
1558 inflateEnd(&s);
1559
1560 return (0);
1561}
wdenk710e3502003-08-29 20:57:53 +00001562
1563#ifdef CONFIG_BZIP2
1564void bz_internal_error(int errcode)
1565{
1566 printf ("BZIP2 internal error %d\n", errcode);
1567}
1568#endif /* CONFIG_BZIP2 */
wdenk47d1a6e2002-11-03 00:01:44 +00001569
wdenk92bbe3f2003-04-20 14:04:18 +00001570static void
1571do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1572 ulong addr, ulong *len_ptr, int verify)
1573{
wdenk92bbe3f2003-04-20 14:04:18 +00001574 image_header_t *hdr = &header;
1575 void (*entry_point)(bd_t *);
1576
Wolfgang Denkb6929f92006-03-12 01:59:35 +01001577 entry_point = (void (*)(bd_t *)) ntohl(hdr->ih_ep);
wdenk92bbe3f2003-04-20 14:04:18 +00001578
1579 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
1580 (ulong)entry_point);
1581
1582 SHOW_BOOT_PROGRESS (15);
1583
1584 /*
1585 * RTEMS Parameters:
1586 * r3: ptr to board info data
1587 */
1588
1589 (*entry_point ) ( gd->bd );
1590}
1591
wdenk47d1a6e2002-11-03 00:01:44 +00001592#if (CONFIG_COMMANDS & CFG_CMD_ELF)
1593static void
1594do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1595 ulong addr, ulong *len_ptr, int verify)
1596{
1597 image_header_t *hdr = &header;
1598 char str[80];
1599
Wolfgang Denkb6929f92006-03-12 01:59:35 +01001600 sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001601 setenv("loadaddr", str);
1602 do_bootvx(cmdtp, 0, 0, NULL);
1603}
1604
1605static void
1606do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1607 ulong addr, ulong *len_ptr, int verify)
1608{
1609 image_header_t *hdr = &header;
1610 char *local_args[2];
1611 char str[16];
1612
Wolfgang Denkb6929f92006-03-12 01:59:35 +01001613 sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001614 local_args[0] = argv[0];
1615 local_args[1] = str; /* and provide it via the arguments */
1616 do_bootelf(cmdtp, 0, 2, local_args);
1617}
1618#endif /* CFG_CMD_ELF */
wdenke58b0dc2003-07-27 00:21:01 +00001619
1620#ifdef CONFIG_LYNXKDI
1621static void
1622do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
1623 int argc, char *argv[],
1624 ulong addr,
1625 ulong *len_ptr,
1626 int verify)
1627{
1628 lynxkdi_boot( &header );
1629}
1630
1631#endif /* CONFIG_LYNXKDI */