blob: be8589d78f215db7022886e96d3d26a84a879db4 [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
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010024#define DEBUG
25
wdenk47d1a6e2002-11-03 00:01:44 +000026/*
27 * Boot support
28 */
29#include <common.h>
30#include <watchdog.h>
31#include <command.h>
wdenk47d1a6e2002-11-03 00:01:44 +000032#include <image.h>
33#include <malloc.h>
34#include <zlib.h>
wdenkc29fdfc2003-08-29 20:57:53 +000035#include <bzlib.h>
wdenk7f70e852003-05-20 14:25:27 +000036#include <environment.h>
wdenk47d1a6e2002-11-03 00:01:44 +000037#include <asm/byteorder.h>
wdenk8bde7f72003-06-27 21:31:46 +000038
Gerald Van Baren213bf8c2007-03-31 12:23:51 -040039#if defined(CONFIG_OF_LIBFDT)
40#include <fdt.h>
41#include <libfdt.h>
Gerald Van Barenc28abb92007-04-14 22:51:24 -040042#include <fdt_support.h>
Gerald Van Baren213bf8c2007-03-31 12:23:51 -040043#endif
Gerald Van Barenaea03c42007-03-31 14:30:53 -040044#if defined(CONFIG_OF_FLAT_TREE)
Wolfgang Denkf57f70a2005-10-13 01:45:54 +020045#include <ft_build.h>
46#endif
47
Wolfgang Denkd87080b2006-03-31 18:32:53 +020048DECLARE_GLOBAL_DATA_PTR;
49
Gerald Van Baren38eb5082007-05-12 09:45:46 -040050/*cmd_boot.c*/
51extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
wdenk8bde7f72003-06-27 21:31:46 +000052
Jon Loeligerbaa26db2007-07-08 17:51:39 -050053#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
wdenk47d1a6e2002-11-03 00:01:44 +000054#include <rtc.h>
55#endif
56
57#ifdef CFG_HUSH_PARSER
58#include <hush.h>
59#endif
60
wdenk47d1a6e2002-11-03 00:01:44 +000061#ifdef CFG_INIT_RAM_LOCK
62#include <asm/cache.h>
63#endif
64
wdenk228f29a2002-12-08 09:53:23 +000065#ifdef CONFIG_LOGBUFFER
66#include <logbuff.h>
67#endif
68
wdenk2abbe072003-06-16 23:50:08 +000069#ifdef CONFIG_HAS_DATAFLASH
70#include <dataflash.h>
71#endif
72
wdenk47d1a6e2002-11-03 00:01:44 +000073/*
74 * Some systems (for example LWMON) have very short watchdog periods;
75 * we must make sure to split long operations like memmove() or
76 * crc32() into reasonable chunks.
77 */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010078#define CHUNKSZ (64 * 1024)
wdenk47d1a6e2002-11-03 00:01:44 +000079
wdenkeedcd072004-09-08 22:03:11 +000080int gunzip (void *, int, unsigned char *, unsigned long *);
wdenk47d1a6e2002-11-03 00:01:44 +000081
82static void *zalloc(void *, unsigned, unsigned);
83static void zfree(void *, void *, unsigned);
84
Jon Loeligerbaa26db2007-07-08 17:51:39 -050085#if defined(CONFIG_CMD_IMI)
wdenk47d1a6e2002-11-03 00:01:44 +000086static int image_info (unsigned long addr);
87#endif
wdenk27b207f2003-07-24 23:38:38 +000088
Jon Loeligerbaa26db2007-07-08 17:51:39 -050089#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +000090#include <flash.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020091extern flash_info_t flash_info[]; /* info for FLASH chips */
wdenk27b207f2003-07-24 23:38:38 +000092static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
93#endif
94
wdenk47d1a6e2002-11-03 00:01:44 +000095static void print_type (image_header_t *hdr);
96
wdenk2262cfe2002-11-18 00:14:45 +000097#ifdef __I386__
98image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
99#endif
100
wdenk47d1a6e2002-11-03 00:01:44 +0000101/*
102 * Continue booting an OS image; caller already has:
103 * - copied image header to global variable `header'
104 * - checked header magic number, checksums (both header & image),
105 * - verified image architecture (PPC) and type (KERNEL or MULTI),
106 * - loaded (first part of) image to header load address,
107 * - disabled interrupts.
108 */
109typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
110 int argc, char *argv[],
111 ulong addr, /* of image to boot */
112 ulong *len_ptr, /* multi-file image length table */
113 int verify); /* getenv("verify")[0] != 'n' */
114
wdenk8bde7f72003-06-27 21:31:46 +0000115#ifdef DEBUG
116extern int do_bdinfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
117#endif
118
wdenk2262cfe2002-11-18 00:14:45 +0000119#ifdef CONFIG_PPC
wdenk47d1a6e2002-11-03 00:01:44 +0000120static boot_os_Fcn do_bootm_linux;
121#else
122extern boot_os_Fcn do_bootm_linux;
123#endif
wdenkf72da342003-10-10 10:05:42 +0000124#ifdef CONFIG_SILENT_CONSOLE
125static void fixup_silent_linux (void);
126#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000127static boot_os_Fcn do_bootm_netbsd;
wdenkd791b1d2003-04-20 14:04:18 +0000128static boot_os_Fcn do_bootm_rtems;
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500129#if defined(CONFIG_CMD_ELF)
wdenk47d1a6e2002-11-03 00:01:44 +0000130static boot_os_Fcn do_bootm_vxworks;
131static boot_os_Fcn do_bootm_qnxelf;
132int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
133int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
Jon Loeliger90253172007-07-10 11:02:44 -0500134#endif
wdenk7f70e852003-05-20 14:25:27 +0000135#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
136static boot_os_Fcn do_bootm_artos;
137#endif
wdenk1f4bb372003-07-27 00:21:01 +0000138#ifdef CONFIG_LYNXKDI
139static boot_os_Fcn do_bootm_lynxkdi;
140extern void lynxkdi_boot( image_header_t * );
141#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000142
Stefan Roese15940c92006-03-13 11:16:36 +0100143#ifndef CFG_BOOTM_LEN
144#define CFG_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
145#endif
146
wdenk47d1a6e2002-11-03 00:01:44 +0000147image_header_t header;
148
149ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
150
151int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
152{
153 ulong iflag;
154 ulong addr;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100155 ulong data, len;
wdenk47d1a6e2002-11-03 00:01:44 +0000156 ulong *len_ptr;
Stefan Roese15940c92006-03-13 11:16:36 +0100157 uint unc_len = CFG_BOOTM_LEN;
wdenk47d1a6e2002-11-03 00:01:44 +0000158 int i, verify;
159 char *name, *s;
wdenkb13fb012003-10-30 21:49:38 +0000160 int (*appl)(int, char *[]);
wdenk47d1a6e2002-11-03 00:01:44 +0000161 image_header_t *hdr = &header;
162
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100163 verify = getenv_verify ();
wdenk47d1a6e2002-11-03 00:01:44 +0000164
165 if (argc < 2) {
166 addr = load_addr;
167 } else {
168 addr = simple_strtoul(argv[1], NULL, 16);
169 }
170
Heiko Schocherfad63402007-07-13 09:54:17 +0200171 show_boot_progress (1);
wdenk47d1a6e2002-11-03 00:01:44 +0000172 printf ("## Booting image at %08lx ...\n", addr);
173
174 /* Copy header so we can blank CRC field for re-calculation */
wdenk2abbe072003-06-16 23:50:08 +0000175#ifdef CONFIG_HAS_DATAFLASH
176 if (addr_dataflash(addr)){
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100177 read_dataflash (addr, image_get_header_size (), (char *)&header);
wdenk2abbe072003-06-16 23:50:08 +0000178 } else
179#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100180 memmove (&header, (char *)addr, image_get_header_size ());
wdenk47d1a6e2002-11-03 00:01:44 +0000181
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100182 if (!image_check_magic (hdr)) {
wdenk2262cfe2002-11-18 00:14:45 +0000183#ifdef __I386__ /* correct image format not implemented yet - fake it */
184 if (fake_header(hdr, (void*)addr, -1) != NULL) {
185 /* to compensate for the addition below */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100186 addr -= image_get_header_size ();
wdenk2262cfe2002-11-18 00:14:45 +0000187 /* turnof verify,
188 * fake_header() does not fake the data crc
189 */
190 verify = 0;
191 } else
192#endif /* __I386__ */
193 {
wdenk4b9206e2004-03-23 22:14:11 +0000194 puts ("Bad Magic Number\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200195 show_boot_progress (-1);
wdenk47d1a6e2002-11-03 00:01:44 +0000196 return 1;
wdenk2262cfe2002-11-18 00:14:45 +0000197 }
wdenk47d1a6e2002-11-03 00:01:44 +0000198 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200199 show_boot_progress (2);
wdenk47d1a6e2002-11-03 00:01:44 +0000200
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100201 if (!image_check_hcrc (hdr)) {
wdenk4b9206e2004-03-23 22:14:11 +0000202 puts ("Bad Header Checksum\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200203 show_boot_progress (-2);
wdenk47d1a6e2002-11-03 00:01:44 +0000204 return 1;
205 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200206 show_boot_progress (3);
wdenk47d1a6e2002-11-03 00:01:44 +0000207
Wolfgang Denkbccae902005-10-06 01:50:50 +0200208#ifdef CONFIG_HAS_DATAFLASH
209 if (addr_dataflash(addr)){
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100210 len = image_get_image_size (hdr);
Wolfgang Denkbccae902005-10-06 01:50:50 +0200211 read_dataflash(addr, len, (char *)CFG_LOAD_ADDR);
212 addr = CFG_LOAD_ADDR;
213 }
214#endif
215
216
wdenk47d1a6e2002-11-03 00:01:44 +0000217 /* for multi-file images we need the data part, too */
wdenka57a4962003-10-26 22:52:58 +0000218 print_image_hdr ((image_header_t *)addr);
wdenk47d1a6e2002-11-03 00:01:44 +0000219
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100220 len = image_get_data_size (hdr);
221 data = addr + image_get_header_size ();
222 len_ptr = (ulong *)data;
wdenk47d1a6e2002-11-03 00:01:44 +0000223
224 if (verify) {
wdenk4b9206e2004-03-23 22:14:11 +0000225 puts (" Verifying Checksum ... ");
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100226 if (!image_check_dcrc ((image_header_t *)addr)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000227 printf ("Bad Data CRC\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200228 show_boot_progress (-3);
wdenk47d1a6e2002-11-03 00:01:44 +0000229 return 1;
230 }
wdenk4b9206e2004-03-23 22:14:11 +0000231 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000232 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200233 show_boot_progress (4);
wdenk47d1a6e2002-11-03 00:01:44 +0000234
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100235 if (!image_check_target_arch (hdr)) {
236 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
Heiko Schocherfad63402007-07-13 09:54:17 +0200237 show_boot_progress (-4);
wdenk47d1a6e2002-11-03 00:01:44 +0000238 return 1;
239 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200240 show_boot_progress (5);
wdenk47d1a6e2002-11-03 00:01:44 +0000241
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100242 switch (image_get_type (hdr)) {
wdenkb13fb012003-10-30 21:49:38 +0000243 case IH_TYPE_STANDALONE:
244 name = "Standalone Application";
245 /* A second argument overwrites the load address */
246 if (argc > 2) {
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100247 image_set_load (hdr, simple_strtoul (argv[2], NULL, 16));
wdenkb13fb012003-10-30 21:49:38 +0000248 }
249 break;
250 case IH_TYPE_KERNEL:
251 name = "Kernel Image";
252 break;
wdenkd4ca31c2004-01-02 14:00:00 +0000253 case IH_TYPE_MULTI:
wdenkb13fb012003-10-30 21:49:38 +0000254 name = "Multi-File Image";
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100255 len = image_to_cpu (len_ptr[0]);
wdenkb13fb012003-10-30 21:49:38 +0000256 /* OS kernel is always the first image */
257 data += 8; /* kernel_len + terminator */
258 for (i=1; len_ptr[i]; ++i)
259 data += 4;
260 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000261 default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
Heiko Schocherfad63402007-07-13 09:54:17 +0200262 show_boot_progress (-5);
wdenk47d1a6e2002-11-03 00:01:44 +0000263 return 1;
264 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200265 show_boot_progress (6);
wdenk47d1a6e2002-11-03 00:01:44 +0000266
267 /*
268 * We have reached the point of no return: we are going to
269 * overwrite all exception vector code, so we cannot easily
270 * recover from any failures any more...
271 */
272
273 iflag = disable_interrupts();
274
wdenkc7de8292002-11-19 11:04:11 +0000275#ifdef CONFIG_AMIGAONEG3SE
276 /*
wdenk8bde7f72003-06-27 21:31:46 +0000277 * We've possible left the caches enabled during
wdenkc7de8292002-11-19 11:04:11 +0000278 * bios emulation, so turn them off again
279 */
280 icache_disable();
281 invalidate_l1_instruction_cache();
282 flush_data_cache();
283 dcache_disable();
284#endif
285
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100286 switch (image_get_comp (hdr)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000287 case IH_COMP_NONE:
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100288 if (image_get_load (hdr) == addr) {
wdenk47d1a6e2002-11-03 00:01:44 +0000289 printf (" XIP %s ... ", name);
290 } else {
291#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
292 size_t l = len;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100293 void *to = (void *)image_get_load (hdr);
wdenk47d1a6e2002-11-03 00:01:44 +0000294 void *from = (void *)data;
295
296 printf (" Loading %s ... ", name);
297
298 while (l > 0) {
299 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
300 WATCHDOG_RESET();
301 memmove (to, from, tail);
302 to += tail;
303 from += tail;
304 l -= tail;
305 }
306#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100307 memmove ((void *)image_get_load (hdr), (uchar *)data, len);
wdenk47d1a6e2002-11-03 00:01:44 +0000308#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
309 }
310 break;
311 case IH_COMP_GZIP:
312 printf (" Uncompressing %s ... ", name);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100313 if (gunzip ((void *)image_get_load (hdr), unc_len,
wdenkeedcd072004-09-08 22:03:11 +0000314 (uchar *)data, &len) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000315 puts ("GUNZIP ERROR - must RESET board to recover\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200316 show_boot_progress (-6);
wdenk47d1a6e2002-11-03 00:01:44 +0000317 do_reset (cmdtp, flag, argc, argv);
318 }
319 break;
wdenkc29fdfc2003-08-29 20:57:53 +0000320#ifdef CONFIG_BZIP2
321 case IH_COMP_BZIP2:
322 printf (" Uncompressing %s ... ", name);
wdenk5653fc32004-02-08 22:55:38 +0000323 /*
324 * If we've got less than 4 MB of malloc() space,
325 * use slower decompression algorithm which requires
326 * at most 2300 KB of memory.
327 */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100328 i = BZ2_bzBuffToBuffDecompress ((char*)image_get_load (hdr),
wdenk5653fc32004-02-08 22:55:38 +0000329 &unc_len, (char *)data, len,
330 CFG_MALLOC_LEN < (4096 * 1024), 0);
wdenkc29fdfc2003-08-29 20:57:53 +0000331 if (i != BZ_OK) {
332 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
Heiko Schocherfad63402007-07-13 09:54:17 +0200333 show_boot_progress (-6);
wdenkc29fdfc2003-08-29 20:57:53 +0000334 do_reset (cmdtp, flag, argc, argv);
335 }
336 break;
337#endif /* CONFIG_BZIP2 */
wdenk47d1a6e2002-11-03 00:01:44 +0000338 default:
339 if (iflag)
340 enable_interrupts();
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100341 printf ("Unimplemented compression type %d\n", image_get_comp (hdr));
Heiko Schocherfad63402007-07-13 09:54:17 +0200342 show_boot_progress (-7);
wdenk47d1a6e2002-11-03 00:01:44 +0000343 return 1;
344 }
wdenk4b9206e2004-03-23 22:14:11 +0000345 puts ("OK\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200346 show_boot_progress (7);
wdenk47d1a6e2002-11-03 00:01:44 +0000347
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100348 switch (image_get_type (hdr)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000349 case IH_TYPE_STANDALONE:
wdenk47d1a6e2002-11-03 00:01:44 +0000350 if (iflag)
351 enable_interrupts();
352
wdenk4a6fd342003-04-12 23:38:12 +0000353 /* load (and uncompress), but don't start if "autostart"
354 * is set to "no"
355 */
wdenk4a551702003-10-08 23:26:14 +0000356 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
357 char buf[32];
358 sprintf(buf, "%lX", len);
359 setenv("filesize", buf);
wdenk4a6fd342003-04-12 23:38:12 +0000360 return 0;
wdenk4a551702003-10-08 23:26:14 +0000361 }
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100362 appl = (int (*)(int, char *[]))image_get_ep (hdr);
wdenkb13fb012003-10-30 21:49:38 +0000363 (*appl)(argc-1, &argv[1]);
wdenk4a6fd342003-04-12 23:38:12 +0000364 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000365 case IH_TYPE_KERNEL:
366 case IH_TYPE_MULTI:
367 /* handled below */
368 break;
369 default:
370 if (iflag)
371 enable_interrupts();
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100372 printf ("Can't boot image type %d\n", image_get_type (hdr));
Heiko Schocherfad63402007-07-13 09:54:17 +0200373 show_boot_progress (-8);
wdenk47d1a6e2002-11-03 00:01:44 +0000374 return 1;
375 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200376 show_boot_progress (8);
wdenk47d1a6e2002-11-03 00:01:44 +0000377
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100378 switch (image_get_os (hdr)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000379 default: /* handled by (original) Linux case */
380 case IH_OS_LINUX:
wdenkf72da342003-10-10 10:05:42 +0000381#ifdef CONFIG_SILENT_CONSOLE
382 fixup_silent_linux();
383#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000384 do_bootm_linux (cmdtp, flag, argc, argv,
385 addr, len_ptr, verify);
386 break;
387 case IH_OS_NETBSD:
388 do_bootm_netbsd (cmdtp, flag, argc, argv,
389 addr, len_ptr, verify);
390 break;
wdenk8bde7f72003-06-27 21:31:46 +0000391
wdenk1f4bb372003-07-27 00:21:01 +0000392#ifdef CONFIG_LYNXKDI
393 case IH_OS_LYNXOS:
394 do_bootm_lynxkdi (cmdtp, flag, argc, argv,
395 addr, len_ptr, verify);
396 break;
397#endif
398
wdenkd791b1d2003-04-20 14:04:18 +0000399 case IH_OS_RTEMS:
400 do_bootm_rtems (cmdtp, flag, argc, argv,
401 addr, len_ptr, verify);
402 break;
wdenk8bde7f72003-06-27 21:31:46 +0000403
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500404#if defined(CONFIG_CMD_ELF)
wdenk47d1a6e2002-11-03 00:01:44 +0000405 case IH_OS_VXWORKS:
406 do_bootm_vxworks (cmdtp, flag, argc, argv,
407 addr, len_ptr, verify);
408 break;
409 case IH_OS_QNX:
410 do_bootm_qnxelf (cmdtp, flag, argc, argv,
411 addr, len_ptr, verify);
412 break;
Jon Loeliger90253172007-07-10 11:02:44 -0500413#endif
wdenk7f70e852003-05-20 14:25:27 +0000414#ifdef CONFIG_ARTOS
415 case IH_OS_ARTOS:
416 do_bootm_artos (cmdtp, flag, argc, argv,
417 addr, len_ptr, verify);
418 break;
419#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000420 }
421
Heiko Schocherfad63402007-07-13 09:54:17 +0200422 show_boot_progress (-9);
wdenk47d1a6e2002-11-03 00:01:44 +0000423#ifdef DEBUG
wdenk4b9206e2004-03-23 22:14:11 +0000424 puts ("\n## Control returned to monitor - resetting...\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000425 do_reset (cmdtp, flag, argc, argv);
426#endif
427 return 1;
428}
429
wdenk0d498392003-07-01 21:06:45 +0000430U_BOOT_CMD(
431 bootm, CFG_MAXARGS, 1, do_bootm,
wdenk8bde7f72003-06-27 21:31:46 +0000432 "bootm - boot application image from memory\n",
433 "[addr [arg ...]]\n - boot application image stored in memory\n"
wdenk9d5028c2004-11-21 00:06:33 +0000434 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
435 "\t'arg' can be the address of an initrd image\n"
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400436#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500437 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
Detlev Zundel5441f612007-10-19 16:47:26 +0200438 "\ta third argument is required which is the address of the\n"
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500439 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
440 "\tuse a '-' for the second argument. If you do not pass a third\n"
441 "\ta bd_info struct will be passed instead\n"
442#endif
wdenk8bde7f72003-06-27 21:31:46 +0000443);
444
wdenkf72da342003-10-10 10:05:42 +0000445#ifdef CONFIG_SILENT_CONSOLE
446static void
447fixup_silent_linux ()
448{
wdenkf72da342003-10-10 10:05:42 +0000449 char buf[256], *start, *end;
450 char *cmdline = getenv ("bootargs");
451
452 /* Only fix cmdline when requested */
453 if (!(gd->flags & GD_FLG_SILENT))
454 return;
455
456 debug ("before silent fix-up: %s\n", cmdline);
457 if (cmdline) {
458 if ((start = strstr (cmdline, "console=")) != NULL) {
459 end = strchr (start, ' ');
460 strncpy (buf, cmdline, (start - cmdline + 8));
461 if (end)
462 strcpy (buf + (start - cmdline + 8), end);
463 else
464 buf[start - cmdline + 8] = '\0';
465 } else {
466 strcpy (buf, cmdline);
467 strcat (buf, " console=");
468 }
469 } else {
470 strcpy (buf, "console=");
471 }
472
473 setenv ("bootargs", buf);
474 debug ("after silent fix-up: %s\n", buf);
475}
476#endif /* CONFIG_SILENT_CONSOLE */
477
wdenk2262cfe2002-11-18 00:14:45 +0000478#ifdef CONFIG_PPC
Matthew McClintockb2b78422006-08-23 13:32:45 -0500479static void __attribute__((noinline))
wdenk47d1a6e2002-11-03 00:01:44 +0000480do_bootm_linux (cmd_tbl_t *cmdtp, int flag,
481 int argc, char *argv[],
482 ulong addr,
483 ulong *len_ptr,
484 int verify)
485{
wdenk47d1a6e2002-11-03 00:01:44 +0000486 ulong sp;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100487 ulong len;
wdenk47d1a6e2002-11-03 00:01:44 +0000488 ulong initrd_start, initrd_end;
489 ulong cmd_start, cmd_end;
490 ulong initrd_high;
491 ulong data;
wdenk38b99262003-05-23 23:18:21 +0000492 int initrd_copy_to_ram = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000493 char *cmdline;
494 char *s;
495 bd_t *kbd;
496 void (*kernel)(bd_t *, ulong, ulong, ulong, ulong);
497 image_header_t *hdr = &header;
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400498#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock25c751e2006-08-16 10:54:09 -0500499 char *of_flat_tree = NULL;
Kumar Galac76f9512006-10-24 23:47:37 -0500500 ulong of_data = 0;
Wolfgang Denkf57f70a2005-10-13 01:45:54 +0200501#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000502
503 if ((s = getenv ("initrd_high")) != NULL) {
504 /* a value of "no" or a similar string will act like 0,
505 * turning the "load high" feature off. This is intentional.
506 */
507 initrd_high = simple_strtoul(s, NULL, 16);
wdenk38b99262003-05-23 23:18:21 +0000508 if (initrd_high == ~0)
509 initrd_copy_to_ram = 0;
wdenk228f29a2002-12-08 09:53:23 +0000510 } else { /* not set, no restrictions to load high */
wdenk47d1a6e2002-11-03 00:01:44 +0000511 initrd_high = ~0;
512 }
513
wdenk56f94be2002-11-05 16:35:14 +0000514#ifdef CONFIG_LOGBUFFER
wdenk6aff3112002-12-17 01:51:00 +0000515 kbd=gd->bd;
wdenk228f29a2002-12-08 09:53:23 +0000516 /* Prevent initrd from overwriting logbuffer */
517 if (initrd_high < (kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD))
518 initrd_high = kbd->bi_memsize-LOGBUFF_LEN-LOGBUFF_OVERHEAD;
519 debug ("## Logbuffer at 0x%08lX ", kbd->bi_memsize-LOGBUFF_LEN);
wdenk56f94be2002-11-05 16:35:14 +0000520#endif
521
wdenk47d1a6e2002-11-03 00:01:44 +0000522 /*
523 * Booting a (Linux) kernel image
524 *
525 * Allocate space for command line and board info - the
526 * address should be as high as possible within the reach of
527 * the kernel (see CFG_BOOTMAPSZ settings), but in unused
528 * memory, which means far enough below the current stack
529 * pointer.
530 */
531
532 asm( "mr %0,1": "=r"(sp) : );
533
wdenk56f94be2002-11-05 16:35:14 +0000534 debug ("## Current stack ends at 0x%08lX ", sp);
535
wdenk47d1a6e2002-11-03 00:01:44 +0000536 sp -= 2048; /* just to be sure */
537 if (sp > CFG_BOOTMAPSZ)
538 sp = CFG_BOOTMAPSZ;
539 sp &= ~0xF;
540
wdenk56f94be2002-11-05 16:35:14 +0000541 debug ("=> set upper limit to 0x%08lX\n", sp);
542
wdenk47d1a6e2002-11-03 00:01:44 +0000543 cmdline = (char *)((sp - CFG_BARGSIZE) & ~0xF);
544 kbd = (bd_t *)(((ulong)cmdline - sizeof(bd_t)) & ~0xF);
545
546 if ((s = getenv("bootargs")) == NULL)
547 s = "";
548
549 strcpy (cmdline, s);
550
551 cmd_start = (ulong)&cmdline[0];
552 cmd_end = cmd_start + strlen(cmdline);
553
554 *kbd = *(gd->bd);
555
556#ifdef DEBUG
557 printf ("## cmdline at 0x%08lX ... 0x%08lX\n", cmd_start, cmd_end);
558
559 do_bdinfo (NULL, 0, 0, NULL);
560#endif
561
562 if ((s = getenv ("clocks_in_mhz")) != NULL) {
563 /* convert all clock information to MHz */
564 kbd->bi_intfreq /= 1000000L;
565 kbd->bi_busfreq /= 1000000L;
wdenk983fda82004-10-28 00:09:35 +0000566#if defined(CONFIG_MPC8220)
wdenk9d5028c2004-11-21 00:06:33 +0000567 kbd->bi_inpfreq /= 1000000L;
568 kbd->bi_pcifreq /= 1000000L;
569 kbd->bi_pevfreq /= 1000000L;
570 kbd->bi_flbfreq /= 1000000L;
571 kbd->bi_vcofreq /= 1000000L;
wdenk983fda82004-10-28 00:09:35 +0000572#endif
Jon Loeliger9c4c5ae2005-07-23 10:37:35 -0500573#if defined(CONFIG_CPM2)
wdenk47d1a6e2002-11-03 00:01:44 +0000574 kbd->bi_cpmfreq /= 1000000L;
575 kbd->bi_brgfreq /= 1000000L;
576 kbd->bi_sccfreq /= 1000000L;
577 kbd->bi_vco /= 1000000L;
Jon Loeliger9c4c5ae2005-07-23 10:37:35 -0500578#endif
wdenkcbd8a352004-02-24 02:00:03 +0000579#if defined(CONFIG_MPC5xxx)
wdenk945af8d2003-07-16 21:53:01 +0000580 kbd->bi_ipbfreq /= 1000000L;
581 kbd->bi_pcifreq /= 1000000L;
wdenkcbd8a352004-02-24 02:00:03 +0000582#endif /* CONFIG_MPC5xxx */
wdenk47d1a6e2002-11-03 00:01:44 +0000583 }
584
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100585 kernel = (void (*)(bd_t *, ulong, ulong, ulong, ulong))image_get_ep (hdr);
wdenk47d1a6e2002-11-03 00:01:44 +0000586
587 /*
588 * Check if there is an initrd image
589 */
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500590
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400591#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500592 /* Look for a '-' which indicates to ignore the ramdisk argument */
593 if (argc >= 3 && strcmp(argv[2], "-") == 0) {
594 debug ("Skipping initrd\n");
Matthew McClintock7376eb82006-10-11 15:13:01 -0500595 len = data = 0;
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500596 }
597 else
598#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000599 if (argc >= 3) {
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500600 debug ("Not skipping initrd\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200601 show_boot_progress (9);
wdenk47d1a6e2002-11-03 00:01:44 +0000602
603 addr = simple_strtoul(argv[2], NULL, 16);
604
605 printf ("## Loading RAMDisk Image at %08lx ...\n", addr);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100606 hdr = (image_header_t *)addr;
wdenk47d1a6e2002-11-03 00:01:44 +0000607
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100608 if (!image_check_magic (hdr)) {
wdenk4b9206e2004-03-23 22:14:11 +0000609 puts ("Bad Magic Number\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200610 show_boot_progress (-10);
wdenk47d1a6e2002-11-03 00:01:44 +0000611 do_reset (cmdtp, flag, argc, argv);
612 }
613
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100614 if (!image_check_hcrc (hdr)) {
wdenk4b9206e2004-03-23 22:14:11 +0000615 puts ("Bad Header Checksum\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200616 show_boot_progress (-11);
wdenk47d1a6e2002-11-03 00:01:44 +0000617 do_reset (cmdtp, flag, argc, argv);
618 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200619 show_boot_progress (10);
wdenk47d1a6e2002-11-03 00:01:44 +0000620
621 print_image_hdr (hdr);
622
wdenk47d1a6e2002-11-03 00:01:44 +0000623 if (verify) {
wdenk4b9206e2004-03-23 22:14:11 +0000624 puts (" Verifying Checksum ... ");
wdenk47d1a6e2002-11-03 00:01:44 +0000625
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100626 if (!image_check_dcrc_wd (hdr, CHUNKSZ)) {
wdenk4b9206e2004-03-23 22:14:11 +0000627 puts ("Bad Data CRC\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200628 show_boot_progress (-12);
wdenk47d1a6e2002-11-03 00:01:44 +0000629 do_reset (cmdtp, flag, argc, argv);
630 }
wdenk4b9206e2004-03-23 22:14:11 +0000631 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000632 }
633
Heiko Schocherfad63402007-07-13 09:54:17 +0200634 show_boot_progress (11);
wdenk47d1a6e2002-11-03 00:01:44 +0000635
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100636 if (!image_check_os (hdr, IH_OS_LINUX) ||
637 !image_check_arch (hdr, IH_ARCH_PPC) ||
638 !image_check_type (hdr, IH_TYPE_RAMDISK)) {
wdenk4b9206e2004-03-23 22:14:11 +0000639 puts ("No Linux PPC Ramdisk Image\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200640 show_boot_progress (-13);
wdenk47d1a6e2002-11-03 00:01:44 +0000641 do_reset (cmdtp, flag, argc, argv);
642 }
643
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100644 data = image_get_data (hdr);
645 len = image_get_data_size (hdr);
646
wdenk47d1a6e2002-11-03 00:01:44 +0000647 /*
648 * Now check if we have a multifile image
649 */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100650 } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1])) {
651 u_long tail = image_to_cpu (len_ptr[0]) % 4;
wdenk47d1a6e2002-11-03 00:01:44 +0000652 int i;
653
Heiko Schocherfad63402007-07-13 09:54:17 +0200654 show_boot_progress (13);
wdenk47d1a6e2002-11-03 00:01:44 +0000655
656 /* skip kernel length and terminator */
657 data = (ulong)(&len_ptr[2]);
658 /* skip any additional image length fields */
659 for (i=1; len_ptr[i]; ++i)
660 data += 4;
661 /* add kernel length, and align */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100662 data += image_to_cpu (len_ptr[0]);
wdenk47d1a6e2002-11-03 00:01:44 +0000663 if (tail) {
664 data += 4 - tail;
665 }
666
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100667 len = image_to_cpu (len_ptr[1]);
wdenk47d1a6e2002-11-03 00:01:44 +0000668
669 } else {
670 /*
671 * no initrd image
672 */
Heiko Schocherfad63402007-07-13 09:54:17 +0200673 show_boot_progress (14);
wdenk47d1a6e2002-11-03 00:01:44 +0000674
675 len = data = 0;
676 }
677
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400678#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock5de62c42006-08-22 09:31:59 -0500679 if(argc > 3) {
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500680 of_flat_tree = (char *) simple_strtoul(argv[3], NULL, 16);
Matthew McClintock25c751e2006-08-16 10:54:09 -0500681 hdr = (image_header_t *)of_flat_tree;
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400682#if defined(CONFIG_OF_FLAT_TREE)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100683 if (*((ulong *)(of_flat_tree + image_get_header_size ())) != OF_DT_HEADER) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400684#else
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100685 if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400686#endif
Matthew McClintock25c751e2006-08-16 10:54:09 -0500687#ifndef CFG_NO_FLASH
Kumar Galac76f9512006-10-24 23:47:37 -0500688 if (addr2info((ulong)of_flat_tree) != NULL)
689 of_data = (ulong)of_flat_tree;
Matthew McClintock25c751e2006-08-16 10:54:09 -0500690#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100691 } else if (image_check_magic (hdr)) {
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400692 printf("## Flat Device Tree at %08lX\n", hdr);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100693 print_image_hdr (hdr);
Matthew McClintock25c751e2006-08-16 10:54:09 -0500694
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100695 if ((image_get_load (hdr) < ((unsigned long)hdr + image_get_image_size (hdr))) &&
696 ((image_get_load (hdr) + image_get_data_size (hdr)) > (unsigned long)hdr)) {
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400697 puts ("ERROR: fdt overwritten - "
698 "must RESET the board to recover.\n");
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400699 do_reset (cmdtp, flag, argc, argv);
Matthew McClintock25c751e2006-08-16 10:54:09 -0500700 }
Matthew McClintock87a449c2006-08-22 09:23:55 -0500701
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400702 puts (" Verifying Checksum ... ");
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100703 if (!image_check_hcrc (hdr)) {
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400704 puts ("ERROR: fdt header checksum invalid - "
705 "must RESET the board to recover.\n");
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400706 do_reset (cmdtp, flag, argc, argv);
Matthew McClintock25c751e2006-08-16 10:54:09 -0500707 }
708
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100709 if (!image_check_dcrc (hdr)) {
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400710 puts ("ERROR: fdt checksum invalid - "
711 "must RESET the board to recover.\n");
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400712 do_reset (cmdtp, flag, argc, argv);
Matthew McClintock25c751e2006-08-16 10:54:09 -0500713 }
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400714 puts ("OK\n");
Matthew McClintock25c751e2006-08-16 10:54:09 -0500715
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100716 if (!image_check_type (hdr, IH_TYPE_FLATDT)) {
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400717 puts ("ERROR: uImage is not a fdt - "
718 "must RESET the board to recover.\n");
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400719 do_reset (cmdtp, flag, argc, argv);
Matthew McClintock25c751e2006-08-16 10:54:09 -0500720 }
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100721 if (image_get_comp (hdr) != IH_COMP_NONE) {
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400722 puts ("ERROR: uImage is compressed - "
723 "must RESET the board to recover.\n");
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400724 do_reset (cmdtp, flag, argc, argv);
Matthew McClintock25c751e2006-08-16 10:54:09 -0500725 }
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400726#if defined(CONFIG_OF_FLAT_TREE)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100727 if (*((ulong *)(of_flat_tree + image_get_header_size ())) != OF_DT_HEADER) {
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400728#else
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100729 if (fdt_check_header (of_flat_tree + image_get_header_size ()) != 0) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400730#endif
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400731 puts ("ERROR: uImage data is not a fdt - "
732 "must RESET the board to recover.\n");
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400733 do_reset (cmdtp, flag, argc, argv);
Matthew McClintock25c751e2006-08-16 10:54:09 -0500734 }
Matthew McClintock87a449c2006-08-22 09:23:55 -0500735
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100736 memmove ((void *)image_get_load (hdr),
737 (void *)(of_flat_tree + image_get_header_size ()),
738 image_get_data_size (hdr));
739
740 of_flat_tree = (char *)image_get_load (hdr);
Matthew McClintock25c751e2006-08-16 10:54:09 -0500741 } else {
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400742 puts ("Did not find a flat Flat Device Tree.\n"
743 "Must RESET the board to recover.\n");
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400744 do_reset (cmdtp, flag, argc, argv);
Matthew McClintock25c751e2006-08-16 10:54:09 -0500745 }
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400746 printf (" Booting using the fdt at 0x%x\n",
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500747 of_flat_tree);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100748 } else if (image_check_type (hdr, IH_TYPE_MULTI) && (len_ptr[1]) && (len_ptr[2])) {
749 u_long tail = image_to_cpu (len_ptr[0]) % 4;
Kumar Galac76f9512006-10-24 23:47:37 -0500750 int i;
751
752 /* skip kernel length, initrd length, and terminator */
Bartlomiej Sieka5b729fb2007-09-04 17:31:22 +0200753 of_flat_tree = (char *)(&len_ptr[3]);
Kumar Galac76f9512006-10-24 23:47:37 -0500754 /* skip any additional image length fields */
755 for (i=2; len_ptr[i]; ++i)
Bartlomiej Sieka5b729fb2007-09-04 17:31:22 +0200756 of_flat_tree += 4;
Kumar Galac76f9512006-10-24 23:47:37 -0500757 /* add kernel length, and align */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100758 of_flat_tree += image_to_cpu (len_ptr[0]);
Kumar Galac76f9512006-10-24 23:47:37 -0500759 if (tail) {
Bartlomiej Sieka5b729fb2007-09-04 17:31:22 +0200760 of_flat_tree += 4 - tail;
Kumar Galac76f9512006-10-24 23:47:37 -0500761 }
762
763 /* add initrd length, and align */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100764 tail = image_to_cpu (len_ptr[1]) % 4;
765 of_flat_tree += image_to_cpu (len_ptr[1]);
Kumar Galac76f9512006-10-24 23:47:37 -0500766 if (tail) {
Bartlomiej Sieka5b729fb2007-09-04 17:31:22 +0200767 of_flat_tree += 4 - tail;
Kumar Galac76f9512006-10-24 23:47:37 -0500768 }
769
Grant Likelya7d7eca2007-09-07 09:25:07 -0600770#ifndef CFG_NO_FLASH
Bartlomiej Sieka5b729fb2007-09-04 17:31:22 +0200771 /* move the blob if it is in flash (set of_data to !null) */
772 if (addr2info ((ulong)of_flat_tree) != NULL)
773 of_data = (ulong)of_flat_tree;
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400774#endif
Bartlomiej Sieka5b729fb2007-09-04 17:31:22 +0200775
776
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400777#if defined(CONFIG_OF_FLAT_TREE)
Bartlomiej Sieka5b729fb2007-09-04 17:31:22 +0200778 if (*((ulong *)(of_flat_tree)) != OF_DT_HEADER) {
Kumar Galac76f9512006-10-24 23:47:37 -0500779#else
Bartlomiej Sieka5b729fb2007-09-04 17:31:22 +0200780 if (fdt_check_header (of_flat_tree) != 0) {
Kumar Galac76f9512006-10-24 23:47:37 -0500781#endif
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400782 puts ("ERROR: image is not a fdt - "
783 "must RESET the board to recover.\n");
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400784 do_reset (cmdtp, flag, argc, argv);
Kumar Galac76f9512006-10-24 23:47:37 -0500785 }
786
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400787#if defined(CONFIG_OF_FLAT_TREE)
Bartlomiej Sieka5b729fb2007-09-04 17:31:22 +0200788 if (((struct boot_param_header *)of_flat_tree)->totalsize !=
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100789 image_to_cpu (len_ptr[2])) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400790#else
Bartlomiej Sieka5b729fb2007-09-04 17:31:22 +0200791 if (be32_to_cpu (fdt_totalsize (of_flat_tree)) !=
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100792 image_to_cpu (len_ptr[2])) {
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400793#endif
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400794 puts ("ERROR: fdt size != image size - "
795 "must RESET the board to recover.\n");
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400796 do_reset (cmdtp, flag, argc, argv);
Kumar Galac76f9512006-10-24 23:47:37 -0500797 }
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500798 }
799#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000800 if (!data) {
wdenk56f94be2002-11-05 16:35:14 +0000801 debug ("No initrd\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000802 }
wdenk47d1a6e2002-11-03 00:01:44 +0000803
804 if (data) {
wdenk38b99262003-05-23 23:18:21 +0000805 if (!initrd_copy_to_ram) { /* zero-copy ramdisk support */
806 initrd_start = data;
807 initrd_end = initrd_start + len;
808 } else {
wdenk47d1a6e2002-11-03 00:01:44 +0000809 initrd_start = (ulong)kbd - len;
810 initrd_start &= ~(4096 - 1); /* align on page */
811
812 if (initrd_high) {
813 ulong nsp;
814
815 /*
816 * the inital ramdisk does not need to be within
817 * CFG_BOOTMAPSZ as it is not accessed until after
818 * the mm system is initialised.
819 *
820 * do the stack bottom calculation again and see if
821 * the initrd will fit just below the monitor stack
822 * bottom without overwriting the area allocated
823 * above for command line args and board info.
824 */
825 asm( "mr %0,1": "=r"(nsp) : );
826 nsp -= 2048; /* just to be sure */
827 nsp &= ~0xF;
828 if (nsp > initrd_high) /* limit as specified */
829 nsp = initrd_high;
830 nsp -= len;
831 nsp &= ~(4096 - 1); /* align on page */
832 if (nsp >= sp)
833 initrd_start = nsp;
834 }
835
Heiko Schocherfad63402007-07-13 09:54:17 +0200836 show_boot_progress (12);
wdenk56f94be2002-11-05 16:35:14 +0000837
838 debug ("## initrd at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
wdenk47d1a6e2002-11-03 00:01:44 +0000839 data, data + len - 1, len, len);
wdenk56f94be2002-11-05 16:35:14 +0000840
wdenk47d1a6e2002-11-03 00:01:44 +0000841 initrd_end = initrd_start + len;
842 printf (" Loading Ramdisk to %08lx, end %08lx ... ",
843 initrd_start, initrd_end);
844#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
845 {
846 size_t l = len;
847 void *to = (void *)initrd_start;
848 void *from = (void *)data;
849
850 while (l > 0) {
851 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
852 WATCHDOG_RESET();
853 memmove (to, from, tail);
854 to += tail;
855 from += tail;
856 l -= tail;
857 }
858 }
859#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
860 memmove ((void *)initrd_start, (void *)data, len);
861#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
wdenk4b9206e2004-03-23 22:14:11 +0000862 puts ("OK\n");
wdenk38b99262003-05-23 23:18:21 +0000863 }
wdenk47d1a6e2002-11-03 00:01:44 +0000864 } else {
865 initrd_start = 0;
866 initrd_end = 0;
867 }
868
Jerry Van Baren210f4632007-08-15 11:13:15 -0400869#if defined(CONFIG_OF_LIBFDT)
wdenk56f94be2002-11-05 16:35:14 +0000870
Andy Fleming073e1b52007-08-14 10:32:59 -0500871#ifdef CFG_BOOTMAPSZ
872 /*
873 * The blob must be within CFG_BOOTMAPSZ,
Jerry Van Baren210f4632007-08-15 11:13:15 -0400874 * so we flag it to be copied if it is not.
Andy Fleming073e1b52007-08-14 10:32:59 -0500875 */
876 if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
Andy Fleming82bd9ee2007-08-15 20:06:50 -0500877 of_data = (ulong)of_flat_tree;
wdenk47d1a6e2002-11-03 00:01:44 +0000878#endif
Wolfgang Denkf57f70a2005-10-13 01:45:54 +0200879
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400880 /* move of_flat_tree if needed */
881 if (of_data) {
882 int err;
883 ulong of_start, of_len;
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400884
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400885 of_len = be32_to_cpu(fdt_totalsize(of_data));
Andy Fleming073e1b52007-08-14 10:32:59 -0500886
887 /* position on a 4K boundary before the kbd */
888 of_start = (ulong)kbd - of_len;
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400889 of_start &= ~(4096 - 1); /* align on page */
890 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
891 of_data, of_data + of_len - 1, of_len, of_len);
892
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400893 of_flat_tree = (char *)of_start;
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400894 printf (" Loading Device Tree to %08lx, end %08lx ... ",
895 of_start, of_start + of_len - 1);
Gerald Van Baren89c87572007-05-08 21:27:35 -0400896 err = fdt_open_into((void *)of_data, (void *)of_start, of_len);
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400897 if (err != 0) {
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400898 puts ("ERROR: fdt move failed - "
899 "must RESET the board to recover.\n");
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400900 do_reset (cmdtp, flag, argc, argv);
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400901 }
Bartlomiej Sieka5b729fb2007-09-04 17:31:22 +0200902 puts ("OK\n");
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400903 }
904 /*
905 * Add the chosen node if it doesn't exist, add the env and bd_t
906 * if the user wants it (the logic is in the subroutines).
907 */
Grant Likelye7902122007-09-06 09:47:40 -0600908 if (of_flat_tree) {
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400909 if (fdt_chosen(of_flat_tree, initrd_start, initrd_end, 0) < 0) {
Grant Likelye7902122007-09-06 09:47:40 -0600910 puts ("ERROR: /chosen node create failed - "
911 "must RESET the board to recover.\n");
912 do_reset (cmdtp, flag, argc, argv);
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400913 }
914#ifdef CONFIG_OF_HAS_UBOOT_ENV
915 if (fdt_env(of_flat_tree) < 0) {
Grant Likelye7902122007-09-06 09:47:40 -0600916 puts ("ERROR: /u-boot-env node create failed - "
917 "must RESET the board to recover.\n");
918 do_reset (cmdtp, flag, argc, argv);
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400919 }
920#endif
921#ifdef CONFIG_OF_HAS_BD_T
922 if (fdt_bd_t(of_flat_tree) < 0) {
Grant Likelye7902122007-09-06 09:47:40 -0600923 puts ("ERROR: /bd_t node create failed - "
924 "must RESET the board to recover.\n");
925 do_reset (cmdtp, flag, argc, argv);
Gerald Van Barenc28abb92007-04-14 22:51:24 -0400926 }
927#endif
Gerald Van Barene125a2f2007-07-10 20:40:39 -0400928#ifdef CONFIG_OF_BOARD_SETUP
Grant Likelye7902122007-09-06 09:47:40 -0600929 /* Call the board-specific fixup routine */
930 ft_board_setup(of_flat_tree, gd->bd);
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400931#endif
Grant Likelye7902122007-09-06 09:47:40 -0600932 }
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400933#endif /* CONFIG_OF_LIBFDT */
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400934#if defined(CONFIG_OF_FLAT_TREE)
Andy Fleming10aaf712007-08-15 17:30:56 -0500935#ifdef CFG_BOOTMAPSZ
936 /*
937 * The blob must be within CFG_BOOTMAPSZ,
938 * so we flag it to be copied if it is not.
939 */
940 if (of_flat_tree >= (char *)CFG_BOOTMAPSZ)
Andy Fleming82bd9ee2007-08-15 20:06:50 -0500941 of_data = (ulong)of_flat_tree;
Andy Fleming10aaf712007-08-15 17:30:56 -0500942#endif
943
Kumar Galac76f9512006-10-24 23:47:37 -0500944 /* move of_flat_tree if needed */
945 if (of_data) {
946 ulong of_start, of_len;
947 of_len = ((struct boot_param_header *)of_data)->totalsize;
Andy Fleming073e1b52007-08-14 10:32:59 -0500948
Kumar Galac76f9512006-10-24 23:47:37 -0500949 /* provide extra 8k pad */
Andy Fleming073e1b52007-08-14 10:32:59 -0500950 of_start = (ulong)kbd - of_len - 8192;
Kumar Galac76f9512006-10-24 23:47:37 -0500951 of_start &= ~(4096 - 1); /* align on page */
952 debug ("## device tree at 0x%08lX ... 0x%08lX (len=%ld=0x%lX)\n",
953 of_data, of_data + of_len - 1, of_len, of_len);
954
955 of_flat_tree = (char *)of_start;
956 printf (" Loading Device Tree to %08lx, end %08lx ... ",
957 of_start, of_start + of_len - 1);
958 memmove ((void *)of_start, (void *)of_data, of_len);
Bartlomiej Sieka5b729fb2007-09-04 17:31:22 +0200959 puts ("OK\n");
Kumar Galac76f9512006-10-24 23:47:37 -0500960 }
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400961 /*
962 * Create the /chosen node and modify the blob with board specific
963 * values as needed.
964 */
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400965 ft_setup(of_flat_tree, kbd, initrd_start, initrd_end);
966 /* ft_dump_blob(of_flat_tree); */
967#endif
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400968 debug ("## Transferring control to Linux (at address %08lx) ...\n",
969 (ulong)kernel);
970
971 show_boot_progress (15);
972
973#if defined(CFG_INIT_RAM_LOCK) && !defined(CONFIG_E500)
974 unlock_ram_in_cache();
Wolfgang Denkf57f70a2005-10-13 01:45:54 +0200975#endif
Stefan Roeseda5553b2006-11-27 17:04:06 +0100976
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400977#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
978 if (of_flat_tree) { /* device tree; boot new style */
979 /*
980 * Linux Kernel Parameters (passing device tree):
Gerald Van Barenc45874b2007-06-25 19:52:23 -0400981 * r3: pointer to the fdt, followed by the board info data
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400982 * r4: physical pointer to the kernel itself
983 * r5: NULL
984 * r6: NULL
985 * r7: NULL
986 */
987 (*kernel) ((bd_t *)of_flat_tree, (ulong)kernel, 0, 0, 0);
988 /* does not return */
989 }
990#endif
Wolfgang Denkf57f70a2005-10-13 01:45:54 +0200991 /*
Stefan Roeseda5553b2006-11-27 17:04:06 +0100992 * Linux Kernel Parameters (passing board info data):
993 * r3: ptr to board info data
994 * r4: initrd_start or 0 if no initrd
995 * r5: initrd_end - unused if r4 is 0
996 * r6: Start of command line string
997 * r7: End of command line string
998 */
Gerald Van Baren38eb5082007-05-12 09:45:46 -0400999 (*kernel) (kbd, initrd_start, initrd_end, cmd_start, cmd_end);
1000 /* does not return */
wdenk47d1a6e2002-11-03 00:01:44 +00001001}
wdenk1cb8e982003-03-06 21:55:29 +00001002#endif /* CONFIG_PPC */
wdenk47d1a6e2002-11-03 00:01:44 +00001003
1004static void
1005do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
1006 int argc, char *argv[],
1007 ulong addr,
1008 ulong *len_ptr,
1009 int verify)
1010{
wdenk47d1a6e2002-11-03 00:01:44 +00001011 image_header_t *hdr = &header;
1012
1013 void (*loader)(bd_t *, image_header_t *, char *, char *);
1014 image_header_t *img_addr;
1015 char *consdev;
1016 char *cmdline;
1017
1018
1019 /*
1020 * Booting a (NetBSD) kernel image
1021 *
1022 * This process is pretty similar to a standalone application:
1023 * The (first part of an multi-) image must be a stage-2 loader,
1024 * which in turn is responsible for loading & invoking the actual
1025 * kernel. The only differences are the parameters being passed:
1026 * besides the board info strucure, the loader expects a command
1027 * line, the name of the console device, and (optionally) the
1028 * address of the original image header.
1029 */
1030
1031 img_addr = 0;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001032 if ((image_check_type (hdr, IH_TYPE_MULTI)) && (len_ptr[1]))
wdenk47d1a6e2002-11-03 00:01:44 +00001033 img_addr = (image_header_t *) addr;
1034
1035
1036 consdev = "";
1037#if defined (CONFIG_8xx_CONS_SMC1)
1038 consdev = "smc1";
1039#elif defined (CONFIG_8xx_CONS_SMC2)
1040 consdev = "smc2";
1041#elif defined (CONFIG_8xx_CONS_SCC2)
1042 consdev = "scc2";
1043#elif defined (CONFIG_8xx_CONS_SCC3)
1044 consdev = "scc3";
1045#endif
1046
1047 if (argc > 2) {
1048 ulong len;
1049 int i;
1050
1051 for (i=2, len=0 ; i<argc ; i+=1)
1052 len += strlen (argv[i]) + 1;
1053 cmdline = malloc (len);
1054
1055 for (i=2, len=0 ; i<argc ; i+=1) {
1056 if (i > 2)
1057 cmdline[len++] = ' ';
1058 strcpy (&cmdline[len], argv[i]);
1059 len += strlen (argv[i]);
1060 }
1061 } else if ((cmdline = getenv("bootargs")) == NULL) {
1062 cmdline = "";
1063 }
1064
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001065 loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
wdenk47d1a6e2002-11-03 00:01:44 +00001066
1067 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
1068 (ulong)loader);
1069
Heiko Schocherfad63402007-07-13 09:54:17 +02001070 show_boot_progress (15);
wdenk47d1a6e2002-11-03 00:01:44 +00001071
1072 /*
1073 * NetBSD Stage-2 Loader Parameters:
1074 * r3: ptr to board info data
1075 * r4: image address
1076 * r5: console device
1077 * r6: boot args string
1078 */
1079 (*loader) (gd->bd, img_addr, consdev, cmdline);
1080}
1081
wdenk7f70e852003-05-20 14:25:27 +00001082#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
1083
1084/* Function that returns a character from the environment */
1085extern uchar (*env_get_char)(int);
1086
1087static void
1088do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
1089 int argc, char *argv[],
1090 ulong addr,
1091 ulong *len_ptr,
1092 int verify)
1093{
wdenk7f70e852003-05-20 14:25:27 +00001094 ulong top;
1095 char *s, *cmdline;
1096 char **fwenv, **ss;
1097 int i, j, nxt, len, envno, envsz;
1098 bd_t *kbd;
1099 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
1100 image_header_t *hdr = &header;
1101
1102 /*
1103 * Booting an ARTOS kernel image + application
1104 */
1105
1106 /* this used to be the top of memory, but was wrong... */
1107#ifdef CONFIG_PPC
1108 /* get stack pointer */
1109 asm volatile ("mr %0,1" : "=r"(top) );
1110#endif
1111 debug ("## Current stack ends at 0x%08lX ", top);
1112
1113 top -= 2048; /* just to be sure */
1114 if (top > CFG_BOOTMAPSZ)
1115 top = CFG_BOOTMAPSZ;
1116 top &= ~0xF;
1117
1118 debug ("=> set upper limit to 0x%08lX\n", top);
1119
1120 /* first check the artos specific boot args, then the linux args*/
1121 if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
1122 s = "";
1123
1124 /* get length of cmdline, and place it */
1125 len = strlen(s);
1126 top = (top - (len + 1)) & ~0xF;
1127 cmdline = (char *)top;
1128 debug ("## cmdline at 0x%08lX ", top);
1129 strcpy(cmdline, s);
1130
1131 /* copy bdinfo */
1132 top = (top - sizeof(bd_t)) & ~0xF;
1133 debug ("## bd at 0x%08lX ", top);
1134 kbd = (bd_t *)top;
1135 memcpy(kbd, gd->bd, sizeof(bd_t));
1136
1137 /* first find number of env entries, and their size */
1138 envno = 0;
1139 envsz = 0;
1140 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1141 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1142 ;
1143 envno++;
1144 envsz += (nxt - i) + 1; /* plus trailing zero */
1145 }
1146 envno++; /* plus the terminating zero */
1147 debug ("## %u envvars total size %u ", envno, envsz);
1148
1149 top = (top - sizeof(char **)*envno) & ~0xF;
1150 fwenv = (char **)top;
1151 debug ("## fwenv at 0x%08lX ", top);
1152
1153 top = (top - envsz) & ~0xF;
1154 s = (char *)top;
1155 ss = fwenv;
1156
1157 /* now copy them */
1158 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
1159 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
1160 ;
1161 *ss++ = s;
1162 for (j = i; j < nxt; ++j)
1163 *s++ = env_get_char(j);
1164 *s++ = '\0';
1165 }
1166 *ss++ = NULL; /* terminate */
1167
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001168 entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
wdenk7f70e852003-05-20 14:25:27 +00001169 (*entry)(kbd, cmdline, fwenv, top);
1170}
1171#endif
1172
1173
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001174#if defined(CONFIG_CMD_BOOTD)
wdenk47d1a6e2002-11-03 00:01:44 +00001175int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1176{
1177 int rcode = 0;
1178#ifndef CFG_HUSH_PARSER
1179 if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
1180#else
1181 if (parse_string_outer(getenv("bootcmd"),
1182 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
1183#endif
1184 return rcode;
1185}
wdenk8bde7f72003-06-27 21:31:46 +00001186
wdenk0d498392003-07-01 21:06:45 +00001187U_BOOT_CMD(
1188 boot, 1, 1, do_bootd,
wdenk9d2b18a2003-06-28 23:11:04 +00001189 "boot - boot default, i.e., run 'bootcmd'\n",
1190 NULL
1191);
1192
1193/* keep old command name "bootd" for backward compatibility */
wdenk0d498392003-07-01 21:06:45 +00001194U_BOOT_CMD(
1195 bootd, 1, 1, do_bootd,
wdenk8bde7f72003-06-27 21:31:46 +00001196 "bootd - boot default, i.e., run 'bootcmd'\n",
1197 NULL
1198);
1199
wdenk47d1a6e2002-11-03 00:01:44 +00001200#endif
1201
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001202#if defined(CONFIG_CMD_IMI)
wdenk47d1a6e2002-11-03 00:01:44 +00001203int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1204{
1205 int arg;
1206 ulong addr;
1207 int rcode=0;
1208
1209 if (argc < 2) {
1210 return image_info (load_addr);
1211 }
1212
1213 for (arg=1; arg <argc; ++arg) {
1214 addr = simple_strtoul(argv[arg], NULL, 16);
1215 if (image_info (addr) != 0) rcode = 1;
1216 }
1217 return rcode;
1218}
1219
1220static int image_info (ulong addr)
1221{
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001222 image_header_t *hdr = (image_header_t *)addr;
wdenk47d1a6e2002-11-03 00:01:44 +00001223
1224 printf ("\n## Checking Image at %08lx ...\n", addr);
1225
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001226 if (!image_check_magic (hdr)) {
wdenk4b9206e2004-03-23 22:14:11 +00001227 puts (" Bad Magic Number\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001228 return 1;
1229 }
1230
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001231 if (!image_check_hcrc (hdr)) {
wdenk4b9206e2004-03-23 22:14:11 +00001232 puts (" Bad Header Checksum\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001233 return 1;
1234 }
1235
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001236 print_image_hdr (hdr);
wdenk47d1a6e2002-11-03 00:01:44 +00001237
wdenk4b9206e2004-03-23 22:14:11 +00001238 puts (" Verifying Checksum ... ");
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001239 if (!image_check_dcrc (hdr)) {
wdenk4b9206e2004-03-23 22:14:11 +00001240 puts (" Bad Data CRC\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001241 return 1;
1242 }
wdenk4b9206e2004-03-23 22:14:11 +00001243 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001244 return 0;
1245}
wdenk0d498392003-07-01 21:06:45 +00001246
1247U_BOOT_CMD(
1248 iminfo, CFG_MAXARGS, 1, do_iminfo,
wdenk8bde7f72003-06-27 21:31:46 +00001249 "iminfo - print header information for application image\n",
1250 "addr [addr ...]\n"
1251 " - print header information for application image starting at\n"
1252 " address 'addr' in memory; this includes verification of the\n"
1253 " image contents (magic number, header and payload checksums)\n"
1254);
1255
Jon Loeliger90253172007-07-10 11:02:44 -05001256#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001257
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001258#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +00001259/*-----------------------------------------------------------------------
1260 * List all images found in flash.
1261 */
1262int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
1263{
1264 flash_info_t *info;
1265 int i, j;
1266 image_header_t *hdr;
wdenk27b207f2003-07-24 23:38:38 +00001267
1268 for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
1269 if (info->flash_id == FLASH_UNKNOWN)
1270 goto next_bank;
Marian Balakowicze6f2e902005-10-11 19:09:42 +02001271 for (j=0; j<info->sector_count; ++j) {
wdenk27b207f2003-07-24 23:38:38 +00001272
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001273 hdr = (image_header_t *)info->start[j];
1274
1275 if (!hdr || !image_check_magic (hdr))
wdenk27b207f2003-07-24 23:38:38 +00001276 goto next_sector;
1277
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001278 if (!image_check_hcrc (hdr))
wdenk27b207f2003-07-24 23:38:38 +00001279 goto next_sector;
1280
1281 printf ("Image at %08lX:\n", (ulong)hdr);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001282 print_image_hdr (hdr);
wdenk5bb226e2003-11-17 21:14:37 +00001283
wdenk4b9206e2004-03-23 22:14:11 +00001284 puts (" Verifying Checksum ... ");
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001285 if (!image_check_dcrc (hdr)) {
1286 puts ("Bad Data CRC\n");
1287 } else {
1288 puts ("OK\n");
wdenk5bb226e2003-11-17 21:14:37 +00001289 }
wdenkbdccc4f2003-08-05 17:43:17 +00001290next_sector: ;
wdenk27b207f2003-07-24 23:38:38 +00001291 }
wdenkbdccc4f2003-08-05 17:43:17 +00001292next_bank: ;
wdenk27b207f2003-07-24 23:38:38 +00001293 }
1294
1295 return (0);
1296}
1297
1298U_BOOT_CMD(
1299 imls, 1, 1, do_imls,
1300 "imls - list all images found in flash\n",
1301 "\n"
1302 " - Prints information about all images found at sector\n"
1303 " boundaries in flash.\n"
1304);
Jon Loeliger90253172007-07-10 11:02:44 -05001305#endif
wdenk27b207f2003-07-24 23:38:38 +00001306
wdenk47d1a6e2002-11-03 00:01:44 +00001307void
1308print_image_hdr (image_header_t *hdr)
1309{
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001310#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001311 time_t timestamp = (time_t)image_get_time (hdr);
wdenk47d1a6e2002-11-03 00:01:44 +00001312 struct rtc_time tm;
1313#endif
1314
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001315 printf (" Image Name: %.*s\n", IH_NMLEN, image_get_name (hdr));
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001316#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
wdenk47d1a6e2002-11-03 00:01:44 +00001317 to_tm (timestamp, &tm);
1318 printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
1319 tm.tm_year, tm.tm_mon, tm.tm_mday,
1320 tm.tm_hour, tm.tm_min, tm.tm_sec);
Jon Loeliger90253172007-07-10 11:02:44 -05001321#endif
wdenk4b9206e2004-03-23 22:14:11 +00001322 puts (" Image Type: "); print_type(hdr);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001323 printf ("\n Data Size: %d Bytes = ", image_get_data_size (hdr));
1324 print_size (image_get_data_size (hdr), "\n");
wdenk4b9206e2004-03-23 22:14:11 +00001325 printf (" Load Address: %08x\n"
1326 " Entry Point: %08x\n",
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001327 image_get_load (hdr), image_get_ep (hdr));
wdenk47d1a6e2002-11-03 00:01:44 +00001328
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001329 if (image_check_type (hdr, IH_TYPE_MULTI)) {
wdenk47d1a6e2002-11-03 00:01:44 +00001330 int i;
1331 ulong len;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001332 ulong *len_ptr = (ulong *)((ulong)hdr + image_get_header_size ());
wdenk47d1a6e2002-11-03 00:01:44 +00001333
wdenk4b9206e2004-03-23 22:14:11 +00001334 puts (" Contents:\n");
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001335 for (i=0; (len = image_to_cpu (*len_ptr)); ++i, ++len_ptr) {
wdenk47d1a6e2002-11-03 00:01:44 +00001336 printf (" Image %d: %8ld Bytes = ", i, len);
1337 print_size (len, "\n");
1338 }
1339 }
1340}
1341
1342
1343static void
1344print_type (image_header_t *hdr)
1345{
1346 char *os, *arch, *type, *comp;
1347
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001348 switch (image_get_os (hdr)) {
wdenk47d1a6e2002-11-03 00:01:44 +00001349 case IH_OS_INVALID: os = "Invalid OS"; break;
1350 case IH_OS_NETBSD: os = "NetBSD"; break;
1351 case IH_OS_LINUX: os = "Linux"; break;
1352 case IH_OS_VXWORKS: os = "VxWorks"; break;
1353 case IH_OS_QNX: os = "QNX"; break;
1354 case IH_OS_U_BOOT: os = "U-Boot"; break;
wdenkd791b1d2003-04-20 14:04:18 +00001355 case IH_OS_RTEMS: os = "RTEMS"; break;
wdenk7f70e852003-05-20 14:25:27 +00001356#ifdef CONFIG_ARTOS
1357 case IH_OS_ARTOS: os = "ARTOS"; break;
1358#endif
wdenk1f4bb372003-07-27 00:21:01 +00001359#ifdef CONFIG_LYNXKDI
1360 case IH_OS_LYNXOS: os = "LynxOS"; break;
1361#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001362 default: os = "Unknown OS"; break;
1363 }
1364
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001365 switch (image_get_arch (hdr)) {
1366 case IH_ARCH_INVALID: arch = "Invalid CPU"; break;
1367 case IH_ARCH_ALPHA: arch = "Alpha"; break;
1368 case IH_ARCH_ARM: arch = "ARM"; break;
1369 case IH_ARCH_AVR32: arch = "AVR32"; break;
1370 case IH_ARCH_BLACKFIN: arch = "Blackfin"; break;
1371 case IH_ARCH_I386: arch = "Intel x86"; break;
1372 case IH_ARCH_IA64: arch = "IA64"; break;
1373 case IH_ARCH_M68K: arch = "M68K"; break;
1374 case IH_ARCH_MICROBLAZE:arch = "Microblaze"; break;
1375 case IH_ARCH_MIPS64: arch = "MIPS 64 Bit"; break;
1376 case IH_ARCH_MIPS: arch = "MIPS"; break;
1377 case IH_ARCH_NIOS2: arch = "Nios-II"; break;
1378 case IH_ARCH_NIOS: arch = "Nios"; break;
1379 case IH_ARCH_PPC: arch = "PowerPC"; break;
1380 case IH_ARCH_S390: arch = "IBM S390"; break;
1381 case IH_ARCH_SH: arch = "SuperH"; break;
1382 case IH_ARCH_SPARC64: arch = "SPARC 64 Bit"; break;
1383 case IH_ARCH_SPARC: arch = "SPARC"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001384 default: arch = "Unknown Architecture"; break;
1385 }
1386
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001387 switch (image_get_type (hdr)) {
wdenk47d1a6e2002-11-03 00:01:44 +00001388 case IH_TYPE_INVALID: type = "Invalid Image"; break;
1389 case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
1390 case IH_TYPE_KERNEL: type = "Kernel Image"; break;
1391 case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
1392 case IH_TYPE_MULTI: type = "Multi-File Image"; break;
1393 case IH_TYPE_FIRMWARE: type = "Firmware"; break;
1394 case IH_TYPE_SCRIPT: type = "Script"; break;
Matthew McClintock25c751e2006-08-16 10:54:09 -05001395 case IH_TYPE_FLATDT: type = "Flat Device Tree"; break;
wdenk47d1a6e2002-11-03 00:01:44 +00001396 default: type = "Unknown Image"; break;
1397 }
1398
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001399 switch (image_get_comp (hdr)) {
wdenk47d1a6e2002-11-03 00:01:44 +00001400 case IH_COMP_NONE: comp = "uncompressed"; break;
1401 case IH_COMP_GZIP: comp = "gzip compressed"; break;
1402 case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
1403 default: comp = "unknown compression"; break;
1404 }
1405
1406 printf ("%s %s %s (%s)", arch, os, type, comp);
1407}
1408
1409#define ZALLOC_ALIGNMENT 16
1410
1411static void *zalloc(void *x, unsigned items, unsigned size)
1412{
1413 void *p;
1414
1415 size *= items;
1416 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
1417
1418 p = malloc (size);
1419
1420 return (p);
1421}
1422
1423static void zfree(void *x, void *addr, unsigned nb)
1424{
1425 free (addr);
1426}
1427
1428#define HEAD_CRC 2
1429#define EXTRA_FIELD 4
1430#define ORIG_NAME 8
1431#define COMMENT 0x10
1432#define RESERVED 0xe0
1433
1434#define DEFLATED 8
1435
wdenkeedcd072004-09-08 22:03:11 +00001436int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
wdenk47d1a6e2002-11-03 00:01:44 +00001437{
1438 z_stream s;
1439 int r, i, flags;
1440
1441 /* skip header */
1442 i = 10;
1443 flags = src[3];
1444 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +00001445 puts ("Error: Bad gzipped data\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001446 return (-1);
1447 }
1448 if ((flags & EXTRA_FIELD) != 0)
1449 i = 12 + src[10] + (src[11] << 8);
1450 if ((flags & ORIG_NAME) != 0)
1451 while (src[i++] != 0)
1452 ;
1453 if ((flags & COMMENT) != 0)
1454 while (src[i++] != 0)
1455 ;
1456 if ((flags & HEAD_CRC) != 0)
1457 i += 2;
1458 if (i >= *lenp) {
wdenk4b9206e2004-03-23 22:14:11 +00001459 puts ("Error: gunzip out of data in header\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001460 return (-1);
1461 }
1462
1463 s.zalloc = zalloc;
1464 s.zfree = zfree;
1465#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
1466 s.outcb = (cb_func)WATCHDOG_RESET;
1467#else
1468 s.outcb = Z_NULL;
1469#endif /* CONFIG_HW_WATCHDOG */
1470
1471 r = inflateInit2(&s, -MAX_WBITS);
1472 if (r != Z_OK) {
1473 printf ("Error: inflateInit2() returned %d\n", r);
1474 return (-1);
1475 }
1476 s.next_in = src + i;
1477 s.avail_in = *lenp - i;
1478 s.next_out = dst;
1479 s.avail_out = dstlen;
1480 r = inflate(&s, Z_FINISH);
1481 if (r != Z_OK && r != Z_STREAM_END) {
1482 printf ("Error: inflate() returned %d\n", r);
1483 return (-1);
1484 }
1485 *lenp = s.next_out - (unsigned char *) dst;
1486 inflateEnd(&s);
1487
1488 return (0);
1489}
1490
wdenkc29fdfc2003-08-29 20:57:53 +00001491#ifdef CONFIG_BZIP2
1492void bz_internal_error(int errcode)
1493{
1494 printf ("BZIP2 internal error %d\n", errcode);
1495}
1496#endif /* CONFIG_BZIP2 */
1497
wdenkd791b1d2003-04-20 14:04:18 +00001498static void
1499do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1500 ulong addr, ulong *len_ptr, int verify)
1501{
wdenkd791b1d2003-04-20 14:04:18 +00001502 image_header_t *hdr = &header;
1503 void (*entry_point)(bd_t *);
1504
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001505 entry_point = (void (*)(bd_t *))image_get_ep (hdr);
wdenkd791b1d2003-04-20 14:04:18 +00001506
1507 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
1508 (ulong)entry_point);
1509
Heiko Schocherfad63402007-07-13 09:54:17 +02001510 show_boot_progress (15);
wdenkd791b1d2003-04-20 14:04:18 +00001511
1512 /*
1513 * RTEMS Parameters:
1514 * r3: ptr to board info data
1515 */
1516
1517 (*entry_point ) ( gd->bd );
1518}
1519
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001520#if defined(CONFIG_CMD_ELF)
wdenk47d1a6e2002-11-03 00:01:44 +00001521static void
1522do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1523 ulong addr, ulong *len_ptr, int verify)
1524{
1525 image_header_t *hdr = &header;
1526 char str[80];
1527
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001528 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001529 setenv("loadaddr", str);
1530 do_bootvx(cmdtp, 0, 0, NULL);
1531}
1532
1533static void
1534do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
1535 ulong addr, ulong *len_ptr, int verify)
1536{
1537 image_header_t *hdr = &header;
1538 char *local_args[2];
1539 char str[16];
1540
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01001541 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001542 local_args[0] = argv[0];
1543 local_args[1] = str; /* and provide it via the arguments */
1544 do_bootelf(cmdtp, 0, 2, local_args);
1545}
Jon Loeliger90253172007-07-10 11:02:44 -05001546#endif
wdenk1f4bb372003-07-27 00:21:01 +00001547
1548#ifdef CONFIG_LYNXKDI
1549static void
1550do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
1551 int argc, char *argv[],
1552 ulong addr,
1553 ulong *len_ptr,
1554 int verify)
1555{
1556 lynxkdi_boot( &header );
1557}
1558
1559#endif /* CONFIG_LYNXKDI */