blob: 8af0589d9a12bc0b92e699bdf9fbb66c59ed9fb6 [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>
Gerald Van Barenc28abb92007-04-14 22:51:24 -040040#include <fdt_support.h>
Gerald Van Baren213bf8c2007-03-31 12:23:51 -040041#endif
Gerald Van Barenaea03c42007-03-31 14:30:53 -040042#if defined(CONFIG_OF_FLAT_TREE)
Wolfgang Denkf57f70a2005-10-13 01:45:54 +020043#include <ft_build.h>
44#endif
45
Wolfgang Denkd87080b2006-03-31 18:32:53 +020046DECLARE_GLOBAL_DATA_PTR;
47
wdenk8bde7f72003-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
wdenk228f29a2002-12-08 09:53:23 +000070#ifdef CONFIG_LOGBUFFER
71#include <logbuff.h>
72#endif
73
wdenk2abbe072003-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
wdenkeedcd072004-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
wdenk27b207f2003-07-24 23:38:38 +000095
96#if (CONFIG_COMMANDS & CFG_CMD_IMLS)
97#include <flash.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020098extern flash_info_t flash_info[]; /* info for FLASH chips */
wdenk27b207f2003-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
wdenk2262cfe2002-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
wdenk8bde7f72003-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
wdenk2262cfe2002-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
wdenkf72da342003-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;
wdenkd791b1d2003-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 */
wdenk7f70e852003-05-20 14:25:27 +0000142#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
143static boot_os_Fcn do_bootm_artos;
144#endif
wdenk1f4bb372003-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 Roese15940c92006-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 Roese15940c92006-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;
wdenkb13fb012003-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 */
wdenk2abbe072003-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) {
wdenk2262cfe2002-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 {
wdenk4b9206e2004-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;
wdenk2262cfe2002-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 Denk77ddac92005-10-13 16:45:02 +0200215 if (crc32 (0, (uchar *)data, len) != checksum) {
wdenk4b9206e2004-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 Denkbccae902005-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 */
wdenka57a4962003-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) {
wdenk4b9206e2004-03-23 22:14:11 +0000238 puts (" Verifying Checksum ... ");
Wolfgang Denk77ddac92005-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 }
wdenk4b9206e2004-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 Denk44ba4642007-03-22 00:13:12 +0100250#if defined(__ARM__)
wdenk2262cfe2002-11-18 00:14:45 +0000251 if (hdr->ih_arch != IH_CPU_ARM)
Wolfgang Denk44ba4642007-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)
wdenk2262cfe2002-11-18 00:14:45 +0000256#elif defined(__I386__)
257 if (hdr->ih_arch != IH_CPU_I386)
wdenk4e5ca3e2003-12-08 01:34:36 +0000258#elif defined(__M68K__)
259 if (hdr->ih_arch != IH_CPU_M68K)
wdenk507bbe32004-04-18 21:13:41 +0000260#elif defined(__microblaze__)
261 if (hdr->ih_arch != IH_CPU_MICROBLAZE)
Wolfgang Denk44ba4642007-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)
wdenk5c952cf2004-10-10 21:27:30 +0000266#elif defined(__nios2__)
267 if (hdr->ih_arch != IH_CPU_NIOS2)
Wolfgang Denk44ba4642007-03-22 00:13:12 +0100268#elif defined(__PPC__)
269 if (hdr->ih_arch != IH_CPU_PPC)
Nobuhiro Iwamatsu0b135cf2007-05-13 20:58:00 +0900270#elif defined(__sh__)
271 if (hdr->ih_arch != IH_CPU_SH)
wdenk2262cfe2002-11-18 00:14:45 +0000272#else
273# error Unknown CPU type
274#endif
275 {
276 printf ("Unsupported Architecture 0x%x\n", hdr->ih_arch);
wdenk47d1a6e2002-11-03 00:01:44 +0000277 SHOW_BOOT_PROGRESS (-4);
278 return 1;
279 }
280 SHOW_BOOT_PROGRESS (5);
281
282 switch (hdr->ih_type) {
wdenkb13fb012003-10-30 21:49:38 +0000283 case IH_TYPE_STANDALONE:
284 name = "Standalone Application";
285 /* A second argument overwrites the load address */
286 if (argc > 2) {
wdenkb2532ef2005-06-20 10:17:34 +0000287 hdr->ih_load = htonl(simple_strtoul(argv[2], NULL, 16));
wdenkb13fb012003-10-30 21:49:38 +0000288 }
289 break;
290 case IH_TYPE_KERNEL:
291 name = "Kernel Image";
292 break;
wdenkd4ca31c2004-01-02 14:00:00 +0000293 case IH_TYPE_MULTI:
wdenkb13fb012003-10-30 21:49:38 +0000294 name = "Multi-File Image";
295 len = ntohl(len_ptr[0]);
296 /* OS kernel is always the first image */
297 data += 8; /* kernel_len + terminator */
298 for (i=1; len_ptr[i]; ++i)
299 data += 4;
300 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000301 default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
302 SHOW_BOOT_PROGRESS (-5);
303 return 1;
304 }
305 SHOW_BOOT_PROGRESS (6);
306
307 /*
308 * We have reached the point of no return: we are going to
309 * overwrite all exception vector code, so we cannot easily
310 * recover from any failures any more...
311 */
312
313 iflag = disable_interrupts();
314
wdenkc7de8292002-11-19 11:04:11 +0000315#ifdef CONFIG_AMIGAONEG3SE
316 /*
wdenk8bde7f72003-06-27 21:31:46 +0000317 * We've possible left the caches enabled during
wdenkc7de8292002-11-19 11:04:11 +0000318 * bios emulation, so turn them off again
319 */
320 icache_disable();
321 invalidate_l1_instruction_cache();
322 flush_data_cache();
323 dcache_disable();
324#endif
325
wdenk47d1a6e2002-11-03 00:01:44 +0000326 switch (hdr->ih_comp) {
327 case IH_COMP_NONE:
wdenk2262cfe2002-11-18 00:14:45 +0000328 if(ntohl(hdr->ih_load) == addr) {
wdenk47d1a6e2002-11-03 00:01:44 +0000329 printf (" XIP %s ... ", name);
330 } else {
331#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
332 size_t l = len;
333 void *to = (void *)ntohl(hdr->ih_load);
334 void *from = (void *)data;
335
336 printf (" Loading %s ... ", name);
337
338 while (l > 0) {
339 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
340 WATCHDOG_RESET();
341 memmove (to, from, tail);
342 to += tail;
343 from += tail;
344 l -= tail;
345 }
346#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
347 memmove ((void *) ntohl(hdr->ih_load), (uchar *)data, len);
348#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
349 }
350 break;
351 case IH_COMP_GZIP:
352 printf (" Uncompressing %s ... ", name);
wdenkc29fdfc2003-08-29 20:57:53 +0000353 if (gunzip ((void *)ntohl(hdr->ih_load), unc_len,
wdenkeedcd072004-09-08 22:03:11 +0000354 (uchar *)data, &len) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000355 puts ("GUNZIP ERROR - must RESET board to recover\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000356 SHOW_BOOT_PROGRESS (-6);
357 do_reset (cmdtp, flag, argc, argv);
358 }
359 break;
wdenkc29fdfc2003-08-29 20:57:53 +0000360#ifdef CONFIG_BZIP2
361 case IH_COMP_BZIP2:
362 printf (" Uncompressing %s ... ", name);
wdenk5653fc32004-02-08 22:55:38 +0000363 /*
364 * If we've got less than 4 MB of malloc() space,
365 * use slower decompression algorithm which requires
366 * at most 2300 KB of memory.
367 */
wdenkc29fdfc2003-08-29 20:57:53 +0000368 i = BZ2_bzBuffToBuffDecompress ((char*)ntohl(hdr->ih_load),
wdenk5653fc32004-02-08 22:55:38 +0000369 &unc_len, (char *)data, len,
370 CFG_MALLOC_LEN < (4096 * 1024), 0);
wdenkc29fdfc2003-08-29 20:57:53 +0000371 if (i != BZ_OK) {
372 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
373 SHOW_BOOT_PROGRESS (-6);
374 udelay(100000);
375 do_reset (cmdtp, flag, argc, argv);
376 }
377 break;
378#endif /* CONFIG_BZIP2 */
wdenk47d1a6e2002-11-03 00:01:44 +0000379 default:
380 if (iflag)
381 enable_interrupts();
382 printf ("Unimplemented compression type %d\n", hdr->ih_comp);
383 SHOW_BOOT_PROGRESS (-7);
384 return 1;
385 }
wdenk4b9206e2004-03-23 22:14:11 +0000386 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000387 SHOW_BOOT_PROGRESS (7);
388
389 switch (hdr->ih_type) {
390 case IH_TYPE_STANDALONE:
wdenk47d1a6e2002-11-03 00:01:44 +0000391 if (iflag)
392 enable_interrupts();
393
wdenk4a6fd342003-04-12 23:38:12 +0000394 /* load (and uncompress), but don't start if "autostart"
395 * is set to "no"
396 */
wdenk4a551702003-10-08 23:26:14 +0000397 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
398 char buf[32];
399 sprintf(buf, "%lX", len);
400 setenv("filesize", buf);
wdenk4a6fd342003-04-12 23:38:12 +0000401 return 0;
wdenk4a551702003-10-08 23:26:14 +0000402 }
wdenkb13fb012003-10-30 21:49:38 +0000403 appl = (int (*)(int, char *[]))ntohl(hdr->ih_ep);
404 (*appl)(argc-1, &argv[1]);
wdenk4a6fd342003-04-12 23:38:12 +0000405 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000406 case IH_TYPE_KERNEL:
407 case IH_TYPE_MULTI:
408 /* handled below */
409 break;
410 default:
411 if (iflag)
412 enable_interrupts();
413 printf ("Can't boot image type %d\n", hdr->ih_type);
414 SHOW_BOOT_PROGRESS (-8);
415 return 1;
416 }
417 SHOW_BOOT_PROGRESS (8);
418
419 switch (hdr->ih_os) {
420 default: /* handled by (original) Linux case */
421 case IH_OS_LINUX:
wdenkf72da342003-10-10 10:05:42 +0000422#ifdef CONFIG_SILENT_CONSOLE
423 fixup_silent_linux();
424#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000425 do_bootm_linux (cmdtp, flag, argc, argv,
426 addr, len_ptr, verify);
427 break;
428 case IH_OS_NETBSD:
429 do_bootm_netbsd (cmdtp, flag, argc, argv,
430 addr, len_ptr, verify);
431 break;
wdenk8bde7f72003-06-27 21:31:46 +0000432
wdenk1f4bb372003-07-27 00:21:01 +0000433#ifdef CONFIG_LYNXKDI
434 case IH_OS_LYNXOS:
435 do_bootm_lynxkdi (cmdtp, flag, argc, argv,
436 addr, len_ptr, verify);
437 break;
438#endif
439
wdenkd791b1d2003-04-20 14:04:18 +0000440 case IH_OS_RTEMS:
441 do_bootm_rtems (cmdtp, flag, argc, argv,
442 addr, len_ptr, verify);
443 break;
wdenk8bde7f72003-06-27 21:31:46 +0000444
wdenk47d1a6e2002-11-03 00:01:44 +0000445#if (CONFIG_COMMANDS & CFG_CMD_ELF)
446 case IH_OS_VXWORKS:
447 do_bootm_vxworks (cmdtp, flag, argc, argv,
448 addr, len_ptr, verify);
449 break;
450 case IH_OS_QNX:
451 do_bootm_qnxelf (cmdtp, flag, argc, argv,
452 addr, len_ptr, verify);
453 break;
454#endif /* CFG_CMD_ELF */
wdenk7f70e852003-05-20 14:25:27 +0000455#ifdef CONFIG_ARTOS
456 case IH_OS_ARTOS:
457 do_bootm_artos (cmdtp, flag, argc, argv,
458 addr, len_ptr, verify);
459 break;
460#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000461 }
462
463 SHOW_BOOT_PROGRESS (-9);
464#ifdef DEBUG
wdenk4b9206e2004-03-23 22:14:11 +0000465 puts ("\n## Control returned to monitor - resetting...\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000466 do_reset (cmdtp, flag, argc, argv);
467#endif
468 return 1;
469}
470
wdenk0d498392003-07-01 21:06:45 +0000471U_BOOT_CMD(
472 bootm, CFG_MAXARGS, 1, do_bootm,
wdenk8bde7f72003-06-27 21:31:46 +0000473 "bootm - boot application image from memory\n",
474 "[addr [arg ...]]\n - boot application image stored in memory\n"
wdenk9d5028c2004-11-21 00:06:33 +0000475 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
476 "\t'arg' can be the address of an initrd image\n"
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400477#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500478 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
479 "\ta third argument is required which is the address of the of the\n"
480 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
481 "\tuse a '-' for the second argument. If you do not pass a third\n"
482 "\ta bd_info struct will be passed instead\n"
483#endif
wdenk8bde7f72003-06-27 21:31:46 +0000484);
485
wdenkf72da342003-10-10 10:05:42 +0000486#ifdef CONFIG_SILENT_CONSOLE
487static void
488fixup_silent_linux ()
489{
wdenkf72da342003-10-10 10:05:42 +0000490 char buf[256], *start, *end;
491 char *cmdline = getenv ("bootargs");
492
493 /* Only fix cmdline when requested */
494 if (!(gd->flags & GD_FLG_SILENT))
495 return;
496
497 debug ("before silent fix-up: %s\n", cmdline);
498 if (cmdline) {
499 if ((start = strstr (cmdline, "console=")) != NULL) {
500 end = strchr (start, ' ');
501 strncpy (buf, cmdline, (start - cmdline + 8));
502 if (end)
503 strcpy (buf + (start - cmdline + 8), end);
504 else
505 buf[start - cmdline + 8] = '\0';
506 } else {
507 strcpy (buf, cmdline);
508 strcat (buf, " console=");
509 }
510 } else {
511 strcpy (buf, "console=");
512 }
513
514 setenv ("bootargs", buf);
515 debug ("after silent fix-up: %s\n", buf);
516}
517#endif /* CONFIG_SILENT_CONSOLE */
518
wdenk2262cfe2002-11-18 00:14:45 +0000519#ifdef CONFIG_PPC
Matthew McClintockb2b78422006-08-23 13:32:45 -0500520static void __attribute__((noinline))
wdenk47d1a6e2002-11-03 00:01:44 +0000521do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
522 int argc, char *argv[],
523 ulong addr,
524 ulong *len_ptr,
525 int verify)
526{
wdenk47d1a6e2002-11-03 00:01:44 +0000527 ulong sp;
528 ulong len, checksum;
529 ulong initrd_start, initrd_end;
530 ulong cmd_start, cmd_end;
531 ulong initrd_high;
532 ulong data;
wdenk38b99262003-05-23 23:18:21 +0000533 int initrd_copy_to_ram = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000534 char *cmdline;
535 char *s;
536 bd_t *kbd;
537 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
538 image_header_t *hdr = &header;
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400539#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock25c751e2006-08-16 10:54:09 -0500540 char *of_flat_tree = NULL;
Kumar Galac76f9512006-10-24 23:47:37 -0500541 ulong of_data = 0;
Wolfgang Denkf57f70a2005-10-13 01:45:54 +0200542#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000543
544 if ((s = getenv ("initrd_high")) != NULL) {
545 /* a value of "no" or a similar string will act like 0,
546 * turning the "load high" feature off. This is intentional.
547 */
548 initrd_high = simple_strtoul(s, NULL, 16);
wdenk38b99262003-05-23 23:18:21 +0000549 if (initrd_high == ~0)
550 initrd_copy_to_ram = 0;
wdenk228f29a2002-12-08 09:53:23 +0000551 } else { /* not set, no restrictions to load high */
wdenk47d1a6e2002-11-03 00:01:44 +0000552 initrd_high = ~0;
553 }
554
wdenk56f94be2002-11-05 16:35:14 +0000555#ifdef CONFIG_LOGBUFFER
wdenk6aff3112002-12-17 01:51:00 +0000556 kbd=gd->bd;
wdenk228f29a2002-12-08 09:53:23 +0000557 /* Prevent initrd from overwriting logbuffer */
558 if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
559 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
560 debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
wdenk56f94be2002-11-05 16:35:14 +0000561#endif
562
wdenk47d1a6e2002-11-03 00:01:44 +0000563 /*
564 * Booting a (Linux) kernel image
565 *
566 * Allocate space for command line and board info - the
567 * address should be as high as possible within the reach of
568 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
569 * memory, which means far enough below the current stack
570 * pointer.
571 */
572
573 asm( "mr %0,1": "=r"(sp) : );
574
wdenk56f94be2002-11-05 16:35:14 +0000575 debug ("## Current stack ends at 0x%08lX ", sp);
576
wdenk47d1a6e2002-11-03 00:01:44 +0000577 sp -= 2048; /* just to be sure */
578 if (sp > CFG_BOOTMAPSZ)
579 sp = CFG_BOOTMAPSZ;
580 sp &= ~0xF;
581
wdenk56f94be2002-11-05 16:35:14 +0000582 debug ("=> set upper limit to 0x%08lX\n", sp);
583
wdenk47d1a6e2002-11-03 00:01:44 +0000584 cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
585 kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
586
587 if ((s = getenv("bootargs")) == NULL)
588 s = "";
589
590 strcpy (cmdline, s);
591
592 cmd_start = (ulong)&cmdline[0];
593 cmd_end = cmd_start + strlen(cmdline);
594
595 *kbd = *(gd->bd);
596
597#ifdef DEBUG
598 printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
599
600 do_bdinfo (NULL, 0, 0, NULL);
601#endif
602
603 if ((s = getenv ("clocks_in_mhz")) != NULL) {
604 /* convert all clock information to MHz */
605 kbd->bi_intfreq /= 1000000L;
606 kbd->bi_busfreq /= 1000000L;
wdenk983fda82004-10-28 00:09:35 +0000607#if defined(CONFIG_MPC8220)
wdenk9d5028c2004-11-21 00:06:33 +0000608 kbd->bi_inpfreq /= 1000000L;
609 kbd->bi_pcifreq /= 1000000L;
610 kbd->bi_pevfreq /= 1000000L;
611 kbd->bi_flbfreq /= 1000000L;
612 kbd->bi_vcofreq /= 1000000L;
wdenk983fda82004-10-28 00:09:35 +0000613#endif
Jon Loeliger9c4c5ae2005-07-23 10:37:35 -0500614#if defined(CONFIG_CPM2)
wdenk47d1a6e2002-11-03 00:01:44 +0000615 kbd->bi_cpmfreq /= 1000000L;
616 kbd->bi_brgfreq /= 1000000L;
617 kbd->bi_sccfreq /= 1000000L;
618 kbd->bi_vco /= 1000000L;
Jon Loeliger9c4c5ae2005-07-23 10:37:35 -0500619#endif
wdenkcbd8a352004-02-24 02:00:03 +0000620#if defined(CONFIG_MPC5xxx)
wdenk945af8d2003-07-16 21:53:01 +0000621 kbd->bi_ipbfreq /= 1000000L;
622 kbd->bi_pcifreq /= 1000000L;
wdenkcbd8a352004-02-24 02:00:03 +0000623#endif /* CONFIG_MPC5xxx */
wdenk47d1a6e2002-11-03 00:01:44 +0000624 }
625
Wolfgang Denkdc013d42006-03-12 01:59:35 +0100626 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong)) ntohl(hdr->ih_ep);
wdenk47d1a6e2002-11-03 00:01:44 +0000627
628 /*
629 * Check if there is an initrd image
630 */
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500631
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400632#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500633 /* Look for a '-' which indicates to ignore the ramdisk argument */
634 if (argc >= 3 && strcmp(argv[2], "-") == 0) {
635 debug ("Skipping initrd\n");
Matthew McClintock7376eb82006-10-11 15:13:01 -0500636 len = data = 0;
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500637 }
638 else
639#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000640 if (argc >= 3) {
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500641 debug ("Not skipping initrd\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000642 SHOW_BOOT_PROGRESS (9);
643
644 addr = simple_strtoul(argv[2], NULL, 16);
645
646 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
647
648 /* Copy header so we can blank CRC field for re-calculation */
649 memmove (&header, (char *)addr, sizeof(image_header_t));
650
Wolfgang Denkdc013d42006-03-12 01:59:35 +0100651 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
wdenk4b9206e2004-03-23 22:14:11 +0000652 puts ("Bad Magic Number\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000653 SHOW_BOOT_PROGRESS (-10);
654 do_reset (cmdtp, flag, argc, argv);
655 }
656
657 data = (ulong)&header;
658 len = sizeof(image_header_t);
659
Wolfgang Denkdc013d42006-03-12 01:59:35 +0100660 checksum = ntohl(hdr->ih_hcrc);
wdenk47d1a6e2002-11-03 00:01:44 +0000661 hdr->ih_hcrc = 0;
662
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200663 if (crc32 (0, (uchar *)data, len) != checksum) {
wdenk4b9206e2004-03-23 22:14:11 +0000664 puts ("Bad Header Checksum\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000665 SHOW_BOOT_PROGRESS (-11);
666 do_reset (cmdtp, flag, argc, argv);
667 }
668
669 SHOW_BOOT_PROGRESS (10);
670
671 print_image_hdr (hdr);
672
673 data = addr + sizeof(image_header_t);
Wolfgang Denkdc013d42006-03-12 01:59:35 +0100674 len = ntohl(hdr->ih_size);
wdenk47d1a6e2002-11-03 00:01:44 +0000675
676 if (verify) {
677 ulong csum = 0;
678#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
679 ulong cdata = data, edata = cdata + len;
680#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
681
wdenk4b9206e2004-03-23 22:14:11 +0000682 puts (" Verifying Checksum ... ");
wdenk47d1a6e2002-11-03 00:01:44 +0000683
684#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
685
686 while (cdata < edata) {
687 ulong chunk = edata - cdata;
688
689 if (chunk > CHUNKSZ)
690 chunk = CHUNKSZ;
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200691 csum = crc32 (csum, (uchar *)cdata, chunk);
wdenk47d1a6e2002-11-03 00:01:44 +0000692 cdata += chunk;
693
694 WATCHDOG_RESET();
695 }
696#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Wolfgang Denk77ddac92005-10-13 16:45:02 +0200697 csum = crc32 (0, (uchar *)data, len);
wdenk47d1a6e2002-11-03 00:01:44 +0000698#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
699
Wolfgang Denkdc013d42006-03-12 01:59:35 +0100700 if (csum != ntohl(hdr->ih_dcrc)) {
wdenk4b9206e2004-03-23 22:14:11 +0000701 puts ("Bad Data CRC\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000702 SHOW_BOOT_PROGRESS (-12);
703 do_reset (cmdtp, flag, argc, argv);
704 }
wdenk4b9206e2004-03-23 22:14:11 +0000705 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000706 }
707
708 SHOW_BOOT_PROGRESS (11);
709
710 if ((hdr->ih_os != IH_OS_LINUX) ||
711 (hdr->ih_arch != IH_CPU_PPC) ||
712 (hdr->ih_type != IH_TYPE_RAMDISK) ) {
wdenk4b9206e2004-03-23 22:14:11 +0000713 puts ("No Linux PPC Ramdisk Image\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000714 SHOW_BOOT_PROGRESS (-13);
715 do_reset (cmdtp, flag, argc, argv);
716 }
717
718 /*
719 * Now check if we have a multifile image
720 */
721 } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1])) {
722 u_long tail = ntohl(len_ptr[0]) % 4;
723 int i;
724
725 SHOW_BOOT_PROGRESS (13);
726
727 /* skip kernel length and terminator */
728 data = (ulong)(&len_ptr[2]);
729 /* skip any additional image length fields */
730 for (i=1; len_ptr[i]; ++i)
731 data += 4;
732 /* add kernel length, and align */
733 data += ntohl(len_ptr[0]);
734 if (tail) {
735 data += 4 - tail;
736 }
737
738 len = ntohl(len_ptr[1]);
739
740 } else {
741 /*
742 * no initrd image
743 */
744 SHOW_BOOT_PROGRESS (14);
745
746 len = data = 0;
747 }
748
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400749#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock5de62c42006-08-22 09:31:59 -0500750 if(argc > 3) {
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500751 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
Matthew McClintock25c751e2006-08-16 10:54:09 -0500752 hdr = (image_header_t *)of_flat_tree;
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400753#if defined(CONFIG_OF_LIBFDT)
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400754 if (fdt_check_header(of_flat_tree) == 0) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400755#else
756 if (*(ulong *)of_flat_tree == OF_DT_HEADER) {
757#endif
Matthew McClintock25c751e2006-08-16 10:54:09 -0500758#ifndef CFG_NO_FLASH
Kumar Galac76f9512006-10-24 23:47:37 -0500759 if (addr2info((ulong)of_flat_tree) != NULL)
760 of_data = (ulong)of_flat_tree;
Matthew McClintock25c751e2006-08-16 10:54:09 -0500761#endif
762 } else if (ntohl(hdr->ih_magic) == IH_MAGIC) {
763 printf("## Flat Device Tree Image at %08lX\n", hdr);
764 print_image_hdr(hdr);
765
766 if ((ntohl(hdr->ih_load) < ((unsigned long)hdr + ntohl(hdr->ih_size) + sizeof(hdr))) &&
767 ((ntohl(hdr->ih_load) + ntohl(hdr->ih_size)) > (unsigned long)hdr)) {
768 printf ("ERROR: Load address overwrites Flat Device Tree uImage\n");
769 return;
770 }
Matthew McClintock87a449c2006-08-22 09:23:55 -0500771
Matthew McClintock25c751e2006-08-16 10:54:09 -0500772 printf(" Verifying Checksum ... ");
773 memmove (&header, (char *)hdr, sizeof(image_header_t));
774 checksum = ntohl(header.ih_hcrc);
775 header.ih_hcrc = 0;
776
777 if(checksum != crc32(0, (uchar *)&header, sizeof(image_header_t))) {
778 printf("ERROR: Flat Device Tree header checksum is invalid\n");
779 return;
780 }
781
782 checksum = ntohl(hdr->ih_dcrc);
783 addr = (ulong)((uchar *)(hdr) + sizeof(image_header_t));
Matthew McClintock25c751e2006-08-16 10:54:09 -0500784
Wolfgang Denk9877d7d2007-05-04 10:02:33 +0200785 if(checksum != crc32(0, (uchar *)addr, ntohl(hdr->ih_size))) {
Matthew McClintock25c751e2006-08-16 10:54:09 -0500786 printf("ERROR: Flat Device Tree checksum is invalid\n");
787 return;
788 }
789 printf("OK\n");
790
791 if (ntohl(hdr->ih_type) != IH_TYPE_FLATDT) {
792 printf ("ERROR: uImage not Flat Device Tree type\n");
793 return;
794 }
795 if (ntohl(hdr->ih_comp) != IH_COMP_NONE) {
796 printf("ERROR: uImage is not uncompressed\n");
797 return;
798 }
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400799#if defined(CONFIG_OF_LIBFDT)
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400800 if (fdt_check_header(of_flat_tree + sizeof(image_header_t)) == 0) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400801#else
Matthew McClintock25c751e2006-08-16 10:54:09 -0500802 if (*((ulong *)(of_flat_tree + sizeof(image_header_t))) != OF_DT_HEADER) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400803#endif
Matthew McClintock25c751e2006-08-16 10:54:09 -0500804 printf ("ERROR: uImage data is not a flat device tree\n");
805 return;
806 }
Matthew McClintock87a449c2006-08-22 09:23:55 -0500807
808 memmove((void *)ntohl(hdr->ih_load),
Matthew McClintock25c751e2006-08-16 10:54:09 -0500809 (void *)(of_flat_tree + sizeof(image_header_t)),
810 ntohl(hdr->ih_size));
811 of_flat_tree = (char *)ntohl(hdr->ih_load);
812 } else {
813 printf ("Did not find a flat flat device tree at address %08lX\n", of_flat_tree);
814 return;
815 }
816 printf (" Booting using flat device tree at 0x%x\n",
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500817 of_flat_tree);
Kumar Galac76f9512006-10-24 23:47:37 -0500818 } else if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]) && (len_ptr[2])) {
819 u_long tail = ntohl(len_ptr[0]) % 4;
820 int i;
821
822 /* skip kernel length, initrd length, and terminator */
823 of_data = (ulong)(&len_ptr[3]);
824 /* skip any additional image length fields */
825 for (i=2; len_ptr[i]; ++i)
826 of_data += 4;
827 /* add kernel length, and align */
828 of_data += ntohl(len_ptr[0]);
829 if (tail) {
830 of_data += 4 - tail;
831 }
832
833 /* add initrd length, and align */
834 tail = ntohl(len_ptr[1]) % 4;
835 of_data += ntohl(len_ptr[1]);
836 if (tail) {
837 of_data += 4 - tail;
838 }
839
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400840#if defined(CONFIG_OF_LIBFDT)
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400841 if (fdt_check_header((void *)of_data) != 0) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400842#else
Kumar Galac76f9512006-10-24 23:47:37 -0500843 if (((struct boot_param_header *)of_data)->magic != OF_DT_HEADER) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400844#endif
Kumar Galac76f9512006-10-24 23:47:37 -0500845 printf ("ERROR: image is not a flat device tree\n");
846 return;
847 }
848
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400849#if defined(CONFIG_OF_LIBFDT)
850 if (be32_to_cpu(fdt_totalsize(of_data)) != ntohl(len_ptr[2])) {
851#else
Kumar Galac76f9512006-10-24 23:47:37 -0500852 if (((struct boot_param_header *)of_data)->totalsize != ntohl(len_ptr[2])) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400853#endif
Kumar Galac76f9512006-10-24 23:47:37 -0500854 printf ("ERROR: flat device tree size does not agree with image\n");
855 return;
856 }
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500857 }
858#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000859 if (!data) {
wdenk56f94be2002-11-05 16:35:14 +0000860 debug ("No initrd\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000861 }
wdenk47d1a6e2002-11-03 00:01:44 +0000862
863 if (data) {
wdenk38b99262003-05-23 23:18:21 +0000864 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
865 initrd_start = data;
866 initrd_end = initrd_start + len;
867 } else {
wdenk47d1a6e2002-11-03 00:01:44 +0000868 initrd_start = (ulong)kbd - len;
869 initrd_start &= ~(4096 - 1); /* align on page */
870
871 if (initrd_high) {
872 ulong nsp;
873
874 /*
875 * the inital ramdisk does not need to be within
876 * CFG_BOOTMAPSZ as it is not accessed until after
877 * the mm system is initialised.
878 *
879 * do the stack bottom calculation again and see if
880 * the initrd will fit just below the monitor stack
881 * bottom without overwriting the area allocated
882 * above for command line args and board info.
883 */
884 asm( "mr %0,1": "=r"(nsp) : );
885 nsp -= 2048; /* just to be sure */
886 nsp &= ~0xF;
887 if (nsp > initrd_high) /* limit as specified */
888 nsp = initrd_high;
889 nsp -= len;
890 nsp &= ~(4096 - 1); /* align on page */
891 if (nsp >= sp)
892 initrd_start = nsp;
893 }
894
895 SHOW_BOOT_PROGRESS (12);
wdenk56f94be2002-11-05 16:35:14 +0000896
897 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
wdenk47d1a6e2002-11-03 00:01:44 +0000898 data, data + len - 1, len, len);
wdenk56f94be2002-11-05 16:35:14 +0000899
wdenk47d1a6e2002-11-03 00:01:44 +0000900 initrd_end = initrd_start + len;
901 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
902 initrd_start, initrd_end);
903#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
904 {
905 size_t l = len;
906 void *to = (void *)initrd_start;
907 void *from = (void *)data;
908
909 while (l > 0) {
910 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
911 WATCHDOG_RESET();
912 memmove (to, from, tail);
913 to += tail;
914 from += tail;
915 l -= tail;
916 }
917 }
918#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
919 memmove ((void *)initrd_start, (void *)data, len);
920#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
wdenk4b9206e2004-03-23 22:14:11 +0000921 puts ("OK\n");
wdenk38b99262003-05-23 23:18:21 +0000922 }
wdenk47d1a6e2002-11-03 00:01:44 +0000923 } else {
924 initrd_start = 0;
925 initrd_end = 0;
926 }
927
wdenk56f94be2002-11-05 16:35:14 +0000928 debug ("## Transferring control to Linux (at address %08lx) ...\n",
wdenk47d1a6e2002-11-03 00:01:44 +0000929 (ulong)kernel);
wdenk56f94be2002-11-05 16:35:14 +0000930
wdenk47d1a6e2002-11-03 00:01:44 +0000931 SHOW_BOOT_PROGRESS (15);
932
wdenk42d1f032003-10-15 23:53:47 +0000933#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
wdenk47d1a6e2002-11-03 00:01:44 +0000934 unlock_ram_in_cache();
935#endif
Wolfgang Denkf57f70a2005-10-13 01:45:54 +0200936
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400937#if defined(CONFIG_OF_LIBFDT)
938 /* move of_flat_tree if needed */
939 if (of_data) {
940 int err;
941 ulong of_start, of_len;
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400942
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400943 of_len = be32_to_cpu(fdt_totalsize(of_data));
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400944 /* position on a 4K boundary before the initrd/kbd */
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400945 if (initrd_start)
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400946 of_start = initrd_start - of_len;
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400947 else
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400948 of_start = (ulong)kbd - of_len;
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400949 of_start &= ~(4096 - 1); /* align on page */
950 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
951 of_data, of_data + of_len - 1, of_len, of_len);
952
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400953 of_flat_tree = (char *)of_start;
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400954 printf (" Loading Device Tree to %08lx, end %08lx ... ",
955 of_start, of_start + of_len - 1);
Gerald Van Baren64dbbd42007-04-06 14:19:43 -0400956 err = fdt_open_into((void *)of_start, (void *)of_data, of_len);
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400957 if (err != 0) {
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400958 printf ("libfdt: %s " __FILE__ " %d\n", fdt_strerror(err), __LINE__);
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400959 }
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400960 /*
961 * Add the chosen node if it doesn't exist, add the env and bd_t
962 * if the user wants it (the logic is in the subroutines).
963 */
964 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
965 printf("Failed creating the /chosen node (0x%08X), aborting.\n", of_flat_tree);
966 return;
967 }
968#ifdef CONFIG_OF_HAS_UBOOT_ENV
969 if (fdt_env(of_flat_tree) < 0) {
970 printf("Failed creating the /u-boot-env node, aborting.\n");
971 return;
972 }
973#endif
974#ifdef CONFIG_OF_HAS_BD_T
975 if (fdt_bd_t(of_flat_tree) < 0) {
976 printf("Failed creating the /bd_t node, aborting.\n");
977 return;
978 }
979#endif
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400980 }
981#endif
982#if defined(CONFIG_OF_FLAT_TREE)
Kumar Galac76f9512006-10-24 23:47:37 -0500983 /* move of_flat_tree if needed */
984 if (of_data) {
985 ulong of_start, of_len;
986 of_len = ((struct boot_param_header *)of_data)->totalsize;
987 /* provide extra 8k pad */
988 if (initrd_start)
989 of_start = initrd_start - of_len - 8192;
990 else
991 of_start = (ulong)kbd - of_len - 8192;
992 of_start &= ~(4096 - 1); /* align on page */
993 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
994 of_data, of_data + of_len - 1, of_len, of_len);
995
996 of_flat_tree = (char *)of_start;
997 printf (" Loading Device Tree to %08lx, end %08lx ... ",
998 of_start, of_start + of_len - 1);
999 memmove ((void *)of_start, (void *)of_data, of_len);
1000 }
Wolfgang Denkf57f70a2005-10-13 01:45:54 +02001001#endif
Stefan Roeseda5553b2006-11-27 17:04:06 +01001002
Wolfgang Denkf57f70a2005-10-13 01:45:54 +02001003 /*
Stefan Roeseda5553b2006-11-27 17:04:06 +01001004 * Linux Kernel Parameters (passing board info data):
1005 * r3: ptr to board info data
1006 * r4: initrd_start or 0 if no initrd
1007 * r5: initrd_end - unused if r4 is 0
1008 * r6: Start of command line string
1009 * r7: End of command line string
1010 */
Gerald Van Baren213bf8c2007-03-31 12:23:51 -04001011#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Stefan Roeseda5553b2006-11-27 17:04:06 +01001012 if (!of_flat_tree) /* no device tree; boot old style */
1013#endif
1014 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
1015 /* does not return */
1016
Gerald Van Baren213bf8c2007-03-31 12:23:51 -04001017#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Stefan Roeseda5553b2006-11-27 17:04:06 +01001018 /*
1019 * Linux Kernel Parameters (passing device tree):
Wolfgang Denkf57f70a2005-10-13 01:45:54 +02001020 * r3: ptr to OF flat tree, followed by the board info data
Kumar Galae559a692006-01-11 16:41:35 -06001021 * r4: physical pointer to the kernel itself
1022 * r5: NULL
1023 * r6: NULL
1024 * r7: NULL
Wolfgang Denkf57f70a2005-10-13 01:45:54 +02001025 */
Gerald Van Baren213bf8c2007-03-31 12:23:51 -04001026#if defined(CONFIG_OF_FLAT_TREE)
Stefan Roeseda5553b2006-11-27 17:04:06 +01001027 ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
1028 /* ft_dump_blob(of_flat_tree); */
Gerald Van Baren213bf8c2007-03-31 12:23:51 -04001029#endif
Gerald Van Barenc28abb92007-04-14 22:51:24 -04001030#if defined(CONFIG_OF_LIBFDT)
1031 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
1032 printf("Failed creating the /chosen node (0x%08X), aborting.\n", of_flat_tree);
1033 return;
1034 }
1035#ifdef CONFIG_OF_HAS_UBOOT_ENV
1036 if (fdt_env(of_flat_tree) < 0) {
1037 printf("Failed creating the /u-boot-env node, aborting.\n");
1038 return;
1039 }
1040#endif
1041#ifdef CONFIG_OF_HAS_BD_T
1042 if (fdt_bd_t(of_flat_tree) < 0) {
1043 printf("Failed creating the /bd_t node, aborting.\n");
1044 return;
1045 }
1046#endif
1047#endif /* if defined(CONFIG_OF_LIBFDT) */
Stefan Roeseda5553b2006-11-27 17:04:06 +01001048
1049 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
1050#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001051}
wdenk1cb8e982003-03-06 21:55:29 +00001052#endif /* CONFIG_PPC */
wdenk47d1a6e2002-11-03 00:01:44 +00001053
1054static void
1055do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
1056 int argc, char *argv[],
1057 ulong addr,
1058 ulong *len_ptr,
1059 int verify)
1060{
wdenk47d1a6e2002-11-03 00:01:44 +00001061 image_header_t *hdr = &header;
1062
1063 void (*loader)(bd_t *, image_header_t *, char *, char *);
1064 image_header_t *img_addr;
1065 char *consdev;
1066 char *cmdline;
1067
1068
1069 /*
1070 * Booting a (NetBSD) kernel image
1071 *
1072 * This process is pretty similar to a standalone application:
1073 * The (first part of an multi-) image must be a stage-2 loader,
1074 * which in turn is responsible for loading & invoking the actual
1075 * kernel. The only differences are the parameters being passed:
1076 * besides the board info strucure, the loader expects a command
1077 * line, the name of the console device, and (optionally) the
1078 * address of the original image header.
1079 */
1080
1081 img_addr = 0;
1082 if ((hdr->ih_type==IH_TYPE_MULTI) && (len_ptr[1]))
1083 img_addr = (image_header_t *) addr;
1084
1085
1086 consdev = "";
1087#if defined (CONFIG_8xx_CONS_SMC1)
1088 consdev = "smc1";
1089#elif defined (CONFIG_8xx_CONS_SMC2)
1090 consdev = "smc2";
1091#elif defined (CONFIG_8xx_CONS_SCC2)
1092 consdev = "scc2";
1093#elif defined (CONFIG_8xx_CONS_SCC3)
1094 consdev = "scc3";
1095#endif
1096
1097 if (argc > 2) {
1098 ulong len;
1099 int i;
1100
1101 for (i=2, len=0 ; i<argc ; i+=1)
1102 len += strlen (argv[i]) + 1;
1103 cmdline = malloc (len);
1104
1105 for (i=2, len=0 ; i<argc ; i+=1) {
1106 if (i > 2)
1107 cmdline[len++] = ' ';
1108 strcpy (&cmdline[len], argv[i]);
1109 len += strlen (argv[i]);
1110 }
1111 } else if ((cmdline = getenv("bootargs")) == NULL) {
1112 cmdline = "";
1113 }
1114
Wolfgang Denkdc013d42006-03-12 01:59:35 +01001115 loader = (void (*)(bd_t *, image_header_t *, char *, char *)) ntohl(hdr->ih_ep);
wdenk47d1a6e2002-11-03 00:01:44 +00001116
1117 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
1118 (ulong)loader);
1119
1120 SHOW_BOOT_PROGRESS (15);
1121
1122 /*
1123 * NetBSD Stage-2 Loader Parameters:
1124 * r3: ptr to board info data
1125 * r4: image address
1126 * r5: console device
1127 * r6: boot args string
1128 */
1129 (*loader) (gd->bd, img_addr, consdev, cmdline);
1130}
1131
wdenk7f70e852003-05-20 14:25:27 +00001132#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
1133
1134/* Function that returns a character from the environment */
1135extern uchar (*env_get_char)(int);
1136
1137static void
1138do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
1139 int argc, char *argv[],
1140 ulong addr,
1141 ulong *len_ptr,
1142 int verify)
1143{
wdenk7f70e852003-05-20 14:25:27 +00001144 ulong top;
1145 char *s, *cmdline;
1146 char **fwenv, **ss;
1147 int i, j, nxt, len, envno, envsz;
1148 bd_t *kbd;
1149 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
1150 image_header_t *hdr = &header;
1151
1152 /*
1153 * Booting an ARTOS kernel image + application
1154 */
1155
1156 /* this used to be the top of memory, but was wrong... */
1157#ifdef CONFIG_PPC
1158 /* get stack pointer */
1159 asm volatile ("mr %0,1" : "=r"(top) );
1160#endif
1161 debug ("## Current stack ends at 0x%08lX ", top);
1162
1163 top -= 2048; /* just to be sure */
1164 if (top > CFG_BOOTMAPSZ)
1165 top = CFG_BOOTMAPSZ;
1166 top &= ~0xF;
1167
1168 debug ("=> set upper limit to 0x%08lX\n", top);
1169
1170 /* first check the artos specific boot args, then the linux args*/
1171 if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
1172 s = "";
1173
1174 /* get length of cmdline, and place it */
1175 len = strlen(s);
1176 top = (top - (len + 1)) & ~0xF;
1177 cmdline = (char *)top;
1178 debug ("## cmdline at 0x%08lX ", top);
1179 strcpy(cmdline, s);
1180
1181 /* copy bdinfo */
1182 top = (top - sizeof(bd_t)) & ~0xF;
1183 debug ("## bd at 0x%08lX ", top);
1184 kbd = (bd_t *)top;
1185 memcpy(kbd, gd->bd, sizeof(bd_t));
1186
1187 /* first find number of env entries, and their size */
1188 envno = 0;
1189 envsz = 0;
1190 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1191 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1192 ;
1193 envno++;
1194 envsz += (nxt - i) + 1; /* plus trailing zero */
1195 }
1196 envno++; /* plus the terminating zero */
1197 debug ("## %u envvars total size %u ", envno, envsz);
1198
1199 top = (top - sizeof(char **)*envno) & ~0xF;
1200 fwenv = (char **)top;
1201 debug ("## fwenv at 0x%08lX ", top);
1202
1203 top = (top - envsz) & ~0xF;
1204 s = (char *)top;
1205 ss = fwenv;
1206
1207 /* now copy them */
1208 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1209 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1210 ;
1211 *ss++ = s;
1212 for (j = i; j < nxt; ++j)
1213 *s++ = env_get_char(j);
1214 *s++ = '\0';
1215 }
1216 *ss++ = NULL; /* terminate */
1217
1218 entry = (void (*)(bd_t *, char *, char **, ulong))ntohl(hdr->ih_ep);
1219 (*entry)(kbd, cmdline, fwenv, top);
1220}
1221#endif
1222
1223
wdenk47d1a6e2002-11-03 00:01:44 +00001224#if (CONFIG_COMMANDS & CFG_CMD_BOOTD)
1225int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1226{
1227 int rcode = 0;
1228#ifndef CFG_HUSH_PARSER
1229 if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
1230#else
1231 if (parse_string_outer(getenv("bootcmd"),
1232 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
1233#endif
1234 return rcode;
1235}
wdenk8bde7f72003-06-27 21:31:46 +00001236
wdenk0d498392003-07-01 21:06:45 +00001237U_BOOT_CMD(
1238 boot, 1, 1, do_bootd,
wdenk9d2b18a2003-06-28 23:11:04 +00001239 "boot - boot default, i.e., run 'bootcmd'\n",
1240 NULL
1241);
1242
1243/* keep old command name "bootd" for backward compatibility */
wdenk0d498392003-07-01 21:06:45 +00001244U_BOOT_CMD(
1245 bootd, 1, 1, do_bootd,
wdenk8bde7f72003-06-27 21:31:46 +00001246 "bootd - boot default, i.e., run 'bootcmd'\n",
1247 NULL
1248);
1249
wdenk47d1a6e2002-11-03 00:01:44 +00001250#endif
1251
1252#if (CONFIG_COMMANDS & CFG_CMD_IMI)
1253int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1254{
1255 int arg;
1256 ulong addr;
1257 int rcode=0;
1258
1259 if (argc < 2) {
1260 return image_info (load_addr);
1261 }
1262
1263 for (arg=1; arg <argc; ++arg) {
1264 addr = simple_strtoul(argv[arg], NULL, 16);
1265 if (image_info (addr) != 0) rcode = 1;
1266 }
1267 return rcode;
1268}
1269
1270static int image_info (ulong addr)
1271{
1272 ulong data, len, checksum;
1273 image_header_t *hdr = &header;
1274
1275 printf ("\n## Checking Image at %08lx ...\n", addr);
1276
1277 /* Copy header so we can blank CRC field for re-calculation */
1278 memmove (&header, (char *)addr, sizeof(image_header_t));
1279
1280 if (ntohl(hdr->ih_magic) != IH_MAGIC) {
wdenk4b9206e2004-03-23 22:14:11 +00001281 puts (" Bad Magic Number\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001282 return 1;
1283 }
1284
1285 data = (ulong)&header;
1286 len = sizeof(image_header_t);
1287
1288 checksum = ntohl(hdr->ih_hcrc);
1289 hdr->ih_hcrc = 0;
1290
Wolfgang Denk77ddac92005-10-13 16:45:02 +02001291 if (crc32 (0, (uchar *)data, len) != checksum) {
wdenk4b9206e2004-03-23 22:14:11 +00001292 puts (" Bad Header Checksum\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001293 return 1;
1294 }
1295
1296 /* for multi-file images we need the data part, too */
1297 print_image_hdr ((image_header_t *)addr);
1298
1299 data = addr + sizeof(image_header_t);
1300 len = ntohl(hdr->ih_size);
1301
wdenk4b9206e2004-03-23 22:14:11 +00001302 puts (" Verifying Checksum ... ");
Wolfgang Denk77ddac92005-10-13 16:45:02 +02001303 if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
wdenk4b9206e2004-03-23 22:14:11 +00001304 puts (" Bad Data CRC\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001305 return 1;
1306 }
wdenk4b9206e2004-03-23 22:14:11 +00001307 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001308 return 0;
1309}
wdenk0d498392003-07-01 21:06:45 +00001310
1311U_BOOT_CMD(
1312 iminfo, CFG_MAXARGS, 1, do_iminfo,
wdenk8bde7f72003-06-27 21:31:46 +00001313 "iminfo - print header information for application image\n",
1314 "addr [addr ...]\n"
1315 " - print header information for application image starting at\n"
1316 " address 'addr' in memory; this includes verification of the\n"
1317 " image contents (magic number, header and payload checksums)\n"
1318);
1319
wdenk47d1a6e2002-11-03 00:01:44 +00001320#endif /* CFG_CMD_IMI */
1321
wdenk27b207f2003-07-24 23:38:38 +00001322#if (CONFIG_COMMANDS & CFG_CMD_IMLS)
1323/*-----------------------------------------------------------------------
1324 * List all images found in flash.
1325 */
1326int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1327{
1328 flash_info_t *info;
1329 int i, j;
1330 image_header_t *hdr;
wdenk5bb226e2003-11-17 21:14:37 +00001331 ulong data, len, checksum;
wdenk27b207f2003-07-24 23:38:38 +00001332
1333 for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
1334 if (info->flash_id == FLASH_UNKNOWN)
1335 goto next_bank;
Marian Balakowicze6f2e902005-10-11 19:09:42 +02001336 for (j=0; j<info->sector_count; ++j) {
wdenk27b207f2003-07-24 23:38:38 +00001337
1338 if (!(hdr=(image_header_t *)info->start[j]) ||
1339 (ntohl(hdr->ih_magic) != IH_MAGIC))
1340 goto next_sector;
1341
1342 /* Copy header so we can blank CRC field for re-calculation */
1343 memmove (&header, (char *)hdr, sizeof(image_header_t));
1344
1345 checksum = ntohl(header.ih_hcrc);
1346 header.ih_hcrc = 0;
1347
Wolfgang Denk77ddac92005-10-13 16:45:02 +02001348 if (crc32 (0, (uchar *)&header, sizeof(image_header_t))
wdenk27b207f2003-07-24 23:38:38 +00001349 != checksum)
1350 goto next_sector;
1351
1352 printf ("Image at %08lX:\n", (ulong)hdr);
1353 print_image_hdr( hdr );
wdenk5bb226e2003-11-17 21:14:37 +00001354
1355 data = (ulong)hdr + sizeof(image_header_t);
1356 len = ntohl(hdr->ih_size);
1357
wdenk4b9206e2004-03-23 22:14:11 +00001358 puts (" Verifying Checksum ... ");
Wolfgang Denk77ddac92005-10-13 16:45:02 +02001359 if (crc32 (0, (uchar *)data, len) != ntohl(hdr->ih_dcrc)) {
wdenk4b9206e2004-03-23 22:14:11 +00001360 puts (" Bad Data CRC\n");
wdenk5bb226e2003-11-17 21:14:37 +00001361 }
wdenk4b9206e2004-03-23 22:14:11 +00001362 puts ("OK\n");
wdenkbdccc4f2003-08-05 17:43:17 +00001363next_sector: ;
wdenk27b207f2003-07-24 23:38:38 +00001364 }
wdenkbdccc4f2003-08-05 17:43:17 +00001365next_bank: ;
wdenk27b207f2003-07-24 23:38:38 +00001366 }
1367
1368 return (0);
1369}
1370
1371U_BOOT_CMD(
1372 imls, 1, 1, do_imls,
1373 "imls - list all images found in flash\n",
1374 "\n"
1375 " - Prints information about all images found at sector\n"
1376 " boundaries in flash.\n"
1377);
1378#endif /* CFG_CMD_IMLS */
1379
wdenk47d1a6e2002-11-03 00:01:44 +00001380void
1381print_image_hdr (image_header_t *hdr)
1382{
1383#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
1384 time_t timestamp = (time_t)ntohl(hdr->ih_time);
1385 struct rtc_time tm;
1386#endif
1387
1388 printf (" Image Name: %.*s\n", IH_NMLEN, hdr->ih_name);
1389#if (CONFIG_COMMANDS & CFG_CMD_DATE) || defined(CONFIG_TIMESTAMP)
1390 to_tm (timestamp, &tm);
1391 printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
1392 tm.tm_year, tm.tm_mon, tm.tm_mday,
1393 tm.tm_hour, tm.tm_min, tm.tm_sec);
1394#endif /* CFG_CMD_DATE, CONFIG_TIMESTAMP */
wdenk4b9206e2004-03-23 22:14:11 +00001395 puts (" Image Type: "); print_type(hdr);
1396 printf ("\n Data Size: %d Bytes = ", ntohl(hdr->ih_size));
wdenk47d1a6e2002-11-03 00:01:44 +00001397 print_size (ntohl(hdr->ih_size), "\n");
wdenk4b9206e2004-03-23 22:14:11 +00001398 printf (" Load Address: %08x\n"
1399 " Entry Point: %08x\n",
1400 ntohl(hdr->ih_load), ntohl(hdr->ih_ep));
wdenk47d1a6e2002-11-03 00:01:44 +00001401
1402 if (hdr->ih_type == IH_TYPE_MULTI) {
1403 int i;
1404 ulong len;
1405 ulong *len_ptr = (ulong *)((ulong)hdr + sizeof(image_header_t));
1406
wdenk4b9206e2004-03-23 22:14:11 +00001407 puts (" Contents:\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001408 for (i=0; (len = ntohl(*len_ptr)); ++i, ++len_ptr) {
1409 printf (" Image %d: %8ld Bytes = ", i, len);
1410 print_size (len, "\n");
1411 }
1412 }
1413}
1414
1415
1416static void
1417print_type (image_header_t *hdr)
1418{
1419 char *os, *arch, *type, *comp;
1420
1421 switch (hdr->ih_os) {
1422 case IH_OS_INVALID: os = "Invalid OS"; break;
1423 case IH_OS_NETBSD: os = "NetBSD"; break;
1424 case IH_OS_LINUX: os = "Linux"; break;
1425 case IH_OS_VXWORKS: os = "VxWorks"; break;
1426 case IH_OS_QNX: os = "QNX"; break;
1427 case IH_OS_U_BOOT: os = "U-Boot"; break;
wdenkd791b1d2003-04-20 14:04:18 +00001428 case IH_OS_RTEMS: os = "RTEMS"; break;
wdenk7f70e852003-05-20 14:25:27 +00001429#ifdef CONFIG_ARTOS
1430 case IH_OS_ARTOS: os = "ARTOS"; break;
1431#endif
wdenk1f4bb372003-07-27 00:21:01 +00001432#ifdef CONFIG_LYNXKDI
1433 case IH_OS_LYNXOS: os = "LynxOS"; break;
1434#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001435 default: os = "Unknown OS"; break;
1436 }
1437
1438 switch (hdr->ih_arch) {
1439 case IH_CPU_INVALID: arch = "Invalid CPU"; break;
1440 case IH_CPU_ALPHA: arch = "Alpha"; break;
1441 case IH_CPU_ARM: arch = "ARM"; break;
Stefan Roese1a1b7372006-10-09 12:55:38 +02001442 case IH_CPU_AVR32: arch = "AVR32"; break;
Wolfgang Denk44ba4642007-03-22 00:13:12 +01001443 case IH_CPU_BLACKFIN: arch = "Blackfin"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001444 case IH_CPU_I386: arch = "Intel x86"; break;
1445 case IH_CPU_IA64: arch = "IA64"; break;
Wolfgang Denk44ba4642007-03-22 00:13:12 +01001446 case IH_CPU_M68K: arch = "M68K"; break;
1447 case IH_CPU_MICROBLAZE: arch = "Microblaze"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001448 case IH_CPU_MIPS64: arch = "MIPS 64 Bit"; break;
Wolfgang Denk44ba4642007-03-22 00:13:12 +01001449 case IH_CPU_MIPS: arch = "MIPS"; break;
1450 case IH_CPU_NIOS2: arch = "Nios-II"; break;
1451 case IH_CPU_NIOS: arch = "Nios"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001452 case IH_CPU_PPC: arch = "PowerPC"; break;
1453 case IH_CPU_S390: arch = "IBM S390"; break;
1454 case IH_CPU_SH: arch = "SuperH"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001455 case IH_CPU_SPARC64: arch = "SPARC 64 Bit"; break;
Wolfgang Denk44ba4642007-03-22 00:13:12 +01001456 case IH_CPU_SPARC: arch = "SPARC"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001457 default: arch = "Unknown Architecture"; break;
1458 }
1459
1460 switch (hdr->ih_type) {
1461 case IH_TYPE_INVALID: type = "Invalid Image"; break;
1462 case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
1463 case IH_TYPE_KERNEL: type = "Kernel Image"; break;
1464 case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
1465 case IH_TYPE_MULTI: type = "Multi-File Image"; break;
1466 case IH_TYPE_FIRMWARE: type = "Firmware"; break;
1467 case IH_TYPE_SCRIPT: type = "Script"; break;
Matthew McClintock25c751e2006-08-16 10:54:09 -05001468 case IH_TYPE_FLATDT: type = "Flat Device Tree"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001469 default: type = "Unknown Image"; break;
1470 }
1471
1472 switch (hdr->ih_comp) {
1473 case IH_COMP_NONE: comp = "uncompressed"; break;
1474 case IH_COMP_GZIP: comp = "gzip compressed"; break;
1475 case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
1476 default: comp = "unknown compression"; break;
1477 }
1478
1479 printf ("%s %s %s (%s)", arch, os, type, comp);
1480}
1481
1482#define ZALLOC_ALIGNMENT 16
1483
1484static void *zalloc(void *x, unsigned items, unsigned size)
1485{
1486 void *p;
1487
1488 size *= items;
1489 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
1490
1491 p = malloc (size);
1492
1493 return (p);
1494}
1495
1496static void zfree(void *x, void *addr, unsigned nb)
1497{
1498 free (addr);
1499}
1500
1501#define HEAD_CRC 2
1502#define EXTRA_FIELD 4
1503#define ORIG_NAME 8
1504#define COMMENT 0x10
1505#define RESERVED 0xe0
1506
1507#define DEFLATED 8
1508
wdenkeedcd072004-09-08 22:03:11 +00001509int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
wdenk47d1a6e2002-11-03 00:01:44 +00001510{
1511 z_stream s;
1512 int r, i, flags;
1513
1514 /* skip header */
1515 i = 10;
1516 flags = src[3];
1517 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +00001518 puts ("Error: Bad gzipped data\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001519 return (-1);
1520 }
1521 if ((flags & EXTRA_FIELD) != 0)
1522 i = 12 + src[10] + (src[11] << 8);
1523 if ((flags & ORIG_NAME) != 0)
1524 while (src[i++] != 0)
1525 ;
1526 if ((flags & COMMENT) != 0)
1527 while (src[i++] != 0)
1528 ;
1529 if ((flags & HEAD_CRC) != 0)
1530 i += 2;
1531 if (i >= *lenp) {
wdenk4b9206e2004-03-23 22:14:11 +00001532 puts ("Error: gunzip out of data in header\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001533 return (-1);
1534 }
1535
1536 s.zalloc = zalloc;
1537 s.zfree = zfree;
1538#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
1539 s.outcb = (cb_func)WATCHDOG_RESET;
1540#else
1541 s.outcb = Z_NULL;
1542#endif /* CONFIG_HW_WATCHDOG */
1543
1544 r = inflateInit2(&s, -MAX_WBITS);
1545 if (r != Z_OK) {
1546 printf ("Error: inflateInit2() returned %d\n", r);
1547 return (-1);
1548 }
1549 s.next_in = src + i;
1550 s.avail_in = *lenp - i;
1551 s.next_out = dst;
1552 s.avail_out = dstlen;
1553 r = inflate(&s, Z_FINISH);
1554 if (r != Z_OK && r != Z_STREAM_END) {
1555 printf ("Error: inflate() returned %d\n", r);
1556 return (-1);
1557 }
1558 *lenp = s.next_out - (unsigned char *) dst;
1559 inflateEnd(&s);
1560
1561 return (0);
1562}
1563
wdenkc29fdfc2003-08-29 20:57:53 +00001564#ifdef CONFIG_BZIP2
1565void bz_internal_error(int errcode)
1566{
1567 printf ("BZIP2 internal error %d\n", errcode);
1568}
1569#endif /* CONFIG_BZIP2 */
1570
wdenkd791b1d2003-04-20 14:04:18 +00001571static void
1572do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1573 ulong addr, ulong *len_ptr, int verify)
1574{
wdenkd791b1d2003-04-20 14:04:18 +00001575 image_header_t *hdr = &header;
1576 void (*entry_point)(bd_t *);
1577
Wolfgang Denkdc013d42006-03-12 01:59:35 +01001578 entry_point = (void (*)(bd_t *)) ntohl(hdr->ih_ep);
wdenkd791b1d2003-04-20 14:04:18 +00001579
1580 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
1581 (ulong)entry_point);
1582
1583 SHOW_BOOT_PROGRESS (15);
1584
1585 /*
1586 * RTEMS Parameters:
1587 * r3: ptr to board info data
1588 */
1589
1590 (*entry_point ) ( gd->bd );
1591}
1592
wdenk47d1a6e2002-11-03 00:01:44 +00001593#if (CONFIG_COMMANDS & CFG_CMD_ELF)
1594static void
1595do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1596 ulong addr, ulong *len_ptr, int verify)
1597{
1598 image_header_t *hdr = &header;
1599 char str[80];
1600
Wolfgang Denkdc013d42006-03-12 01:59:35 +01001601 sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001602 setenv("loadaddr", str);
1603 do_bootvx(cmdtp, 0, 0, NULL);
1604}
1605
1606static void
1607do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1608 ulong addr, ulong *len_ptr, int verify)
1609{
1610 image_header_t *hdr = &header;
1611 char *local_args[2];
1612 char str[16];
1613
Wolfgang Denkdc013d42006-03-12 01:59:35 +01001614 sprintf(str, "%x", ntohl(hdr->ih_ep)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001615 local_args[0] = argv[0];
1616 local_args[1] = str; /* and provide it via the arguments */
1617 do_bootelf(cmdtp, 0, 2, local_args);
1618}
1619#endif /* CFG_CMD_ELF */
wdenk1f4bb372003-07-27 00:21:01 +00001620
1621#ifdef CONFIG_LYNXKDI
1622static void
1623do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
1624 int argc, char *argv[],
1625 ulong addr,
1626 ulong *len_ptr,
1627 int verify)
1628{
1629 lynxkdi_boot( &header );
1630}
1631
1632#endif /* CONFIG_LYNXKDI */