blob: 5f1b6b6d6737368dc03dbd24989389e88cc42574 [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>
Marian Balakowiczd45d5a12008-01-08 18:11:43 +010043#elif defined(CONFIG_OF_FLAT_TREE)
Wolfgang Denkf57f70a2005-10-13 01:45:54 +020044#include <ft_build.h>
45#endif
46
Wolfgang Denkd87080b2006-03-31 18:32:53 +020047DECLARE_GLOBAL_DATA_PTR;
48
Gerald Van Baren38eb5082007-05-12 09:45:46 -040049/*cmd_boot.c*/
50extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
wdenk8bde7f72003-06-27 21:31:46 +000051
Jon Loeligerbaa26db2007-07-08 17:51:39 -050052#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
wdenk47d1a6e2002-11-03 00:01:44 +000053#include <rtc.h>
54#endif
55
56#ifdef CFG_HUSH_PARSER
57#include <hush.h>
58#endif
59
wdenk2abbe072003-06-16 23:50:08 +000060#ifdef CONFIG_HAS_DATAFLASH
61#include <dataflash.h>
62#endif
63
wdenk47d1a6e2002-11-03 00:01:44 +000064/*
65 * Some systems (for example LWMON) have very short watchdog periods;
66 * we must make sure to split long operations like memmove() or
67 * crc32() into reasonable chunks.
68 */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010069#define CHUNKSZ (64 * 1024)
wdenk47d1a6e2002-11-03 00:01:44 +000070
wdenkeedcd072004-09-08 22:03:11 +000071int gunzip (void *, int, unsigned char *, unsigned long *);
wdenk47d1a6e2002-11-03 00:01:44 +000072
73static void *zalloc(void *, unsigned, unsigned);
74static void zfree(void *, void *, unsigned);
75
Jon Loeligerbaa26db2007-07-08 17:51:39 -050076#if defined(CONFIG_CMD_IMI)
wdenk47d1a6e2002-11-03 00:01:44 +000077static int image_info (unsigned long addr);
78#endif
wdenk27b207f2003-07-24 23:38:38 +000079
Jon Loeligerbaa26db2007-07-08 17:51:39 -050080#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +000081#include <flash.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020082extern flash_info_t flash_info[]; /* info for FLASH chips */
wdenk27b207f2003-07-24 23:38:38 +000083static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
84#endif
85
wdenk47d1a6e2002-11-03 00:01:44 +000086static void print_type (image_header_t *hdr);
87
wdenk2262cfe2002-11-18 00:14:45 +000088#ifdef __I386__
89image_header_t *fake_header(image_header_t *hdr, void *ptr, int size);
90#endif
91
wdenk47d1a6e2002-11-03 00:01:44 +000092/*
93 * Continue booting an OS image; caller already has:
94 * - copied image header to global variable `header'
95 * - checked header magic number, checksums (both header & image),
96 * - verified image architecture (PPC) and type (KERNEL or MULTI),
97 * - loaded (first part of) image to header load address,
98 * - disabled interrupts.
99 */
100typedef void boot_os_Fcn (cmd_tbl_t *cmdtp, int flag,
101 int argc, char *argv[],
102 ulong addr, /* of image to boot */
103 ulong *len_ptr, /* multi-file image length table */
104 int verify); /* getenv("verify")[0] != 'n' */
105
wdenk47d1a6e2002-11-03 00:01:44 +0000106extern boot_os_Fcn do_bootm_linux;
Marian Balakowicz5d3cc552008-01-08 18:11:43 +0100107
wdenkf72da342003-10-10 10:05:42 +0000108#ifdef CONFIG_SILENT_CONSOLE
109static void fixup_silent_linux (void);
110#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000111static boot_os_Fcn do_bootm_netbsd;
wdenkd791b1d2003-04-20 14:04:18 +0000112static boot_os_Fcn do_bootm_rtems;
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500113#if defined(CONFIG_CMD_ELF)
wdenk47d1a6e2002-11-03 00:01:44 +0000114static boot_os_Fcn do_bootm_vxworks;
115static boot_os_Fcn do_bootm_qnxelf;
116int do_bootvx ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
117int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[] );
Jon Loeliger90253172007-07-10 11:02:44 -0500118#endif
wdenk7f70e852003-05-20 14:25:27 +0000119#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
120static boot_os_Fcn do_bootm_artos;
121#endif
wdenk1f4bb372003-07-27 00:21:01 +0000122#ifdef CONFIG_LYNXKDI
123static boot_os_Fcn do_bootm_lynxkdi;
124extern void lynxkdi_boot( image_header_t * );
125#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000126
Stefan Roese15940c92006-03-13 11:16:36 +0100127#ifndef CFG_BOOTM_LEN
128#define CFG_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
129#endif
130
wdenk47d1a6e2002-11-03 00:01:44 +0000131image_header_t header;
132
133ulong load_addr = CFG_LOAD_ADDR; /* Default Load Address */
134
135int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
136{
137 ulong iflag;
138 ulong addr;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100139 ulong data, len;
wdenk47d1a6e2002-11-03 00:01:44 +0000140 ulong *len_ptr;
Stefan Roese15940c92006-03-13 11:16:36 +0100141 uint unc_len = CFG_BOOTM_LEN;
wdenk47d1a6e2002-11-03 00:01:44 +0000142 int i, verify;
143 char *name, *s;
wdenkb13fb012003-10-30 21:49:38 +0000144 int (*appl)(int, char *[]);
wdenk47d1a6e2002-11-03 00:01:44 +0000145 image_header_t *hdr = &header;
146
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100147 verify = getenv_verify ();
wdenk47d1a6e2002-11-03 00:01:44 +0000148
149 if (argc < 2) {
150 addr = load_addr;
151 } else {
152 addr = simple_strtoul(argv[1], NULL, 16);
153 }
154
Heiko Schocherfad63402007-07-13 09:54:17 +0200155 show_boot_progress (1);
wdenk47d1a6e2002-11-03 00:01:44 +0000156 printf ("## Booting image at %08lx ...\n", addr);
157
158 /* Copy header so we can blank CRC field for re-calculation */
wdenk2abbe072003-06-16 23:50:08 +0000159#ifdef CONFIG_HAS_DATAFLASH
160 if (addr_dataflash(addr)){
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100161 read_dataflash (addr, image_get_header_size (), (char *)&header);
wdenk2abbe072003-06-16 23:50:08 +0000162 } else
163#endif
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100164 memmove (&header, (char *)addr, image_get_header_size ());
wdenk47d1a6e2002-11-03 00:01:44 +0000165
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100166 if (!image_check_magic (hdr)) {
wdenk2262cfe2002-11-18 00:14:45 +0000167#ifdef __I386__ /* correct image format not implemented yet - fake it */
168 if (fake_header(hdr, (void*)addr, -1) != NULL) {
169 /* to compensate for the addition below */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100170 addr -= image_get_header_size ();
wdenk2262cfe2002-11-18 00:14:45 +0000171 /* turnof verify,
172 * fake_header() does not fake the data crc
173 */
174 verify = 0;
175 } else
176#endif /* __I386__ */
177 {
wdenk4b9206e2004-03-23 22:14:11 +0000178 puts ("Bad Magic Number\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200179 show_boot_progress (-1);
wdenk47d1a6e2002-11-03 00:01:44 +0000180 return 1;
wdenk2262cfe2002-11-18 00:14:45 +0000181 }
wdenk47d1a6e2002-11-03 00:01:44 +0000182 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200183 show_boot_progress (2);
wdenk47d1a6e2002-11-03 00:01:44 +0000184
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100185 if (!image_check_hcrc (hdr)) {
wdenk4b9206e2004-03-23 22:14:11 +0000186 puts ("Bad Header Checksum\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200187 show_boot_progress (-2);
wdenk47d1a6e2002-11-03 00:01:44 +0000188 return 1;
189 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200190 show_boot_progress (3);
wdenk47d1a6e2002-11-03 00:01:44 +0000191
Wolfgang Denkbccae902005-10-06 01:50:50 +0200192#ifdef CONFIG_HAS_DATAFLASH
193 if (addr_dataflash(addr)){
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100194 len = image_get_image_size (hdr);
Wolfgang Denkbccae902005-10-06 01:50:50 +0200195 read_dataflash(addr, len, (char *)CFG_LOAD_ADDR);
196 addr = CFG_LOAD_ADDR;
197 }
198#endif
199
200
wdenk47d1a6e2002-11-03 00:01:44 +0000201 /* for multi-file images we need the data part, too */
wdenka57a4962003-10-26 22:52:58 +0000202 print_image_hdr ((image_header_t *)addr);
wdenk47d1a6e2002-11-03 00:01:44 +0000203
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100204 len = image_get_data_size (hdr);
205 data = addr + image_get_header_size ();
206 len_ptr = (ulong *)data;
wdenk47d1a6e2002-11-03 00:01:44 +0000207
208 if (verify) {
wdenk4b9206e2004-03-23 22:14:11 +0000209 puts (" Verifying Checksum ... ");
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100210 if (!image_check_dcrc ((image_header_t *)addr)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000211 printf ("Bad Data CRC\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200212 show_boot_progress (-3);
wdenk47d1a6e2002-11-03 00:01:44 +0000213 return 1;
214 }
wdenk4b9206e2004-03-23 22:14:11 +0000215 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000216 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200217 show_boot_progress (4);
wdenk47d1a6e2002-11-03 00:01:44 +0000218
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100219 if (!image_check_target_arch (hdr)) {
220 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
Heiko Schocherfad63402007-07-13 09:54:17 +0200221 show_boot_progress (-4);
wdenk47d1a6e2002-11-03 00:01:44 +0000222 return 1;
223 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200224 show_boot_progress (5);
wdenk47d1a6e2002-11-03 00:01:44 +0000225
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100226 switch (image_get_type (hdr)) {
wdenkb13fb012003-10-30 21:49:38 +0000227 case IH_TYPE_STANDALONE:
228 name = "Standalone Application";
229 /* A second argument overwrites the load address */
230 if (argc > 2) {
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100231 image_set_load (hdr, simple_strtoul (argv[2], NULL, 16));
wdenkb13fb012003-10-30 21:49:38 +0000232 }
233 break;
234 case IH_TYPE_KERNEL:
235 name = "Kernel Image";
236 break;
wdenkd4ca31c2004-01-02 14:00:00 +0000237 case IH_TYPE_MULTI:
wdenkb13fb012003-10-30 21:49:38 +0000238 name = "Multi-File Image";
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100239 len = image_to_cpu (len_ptr[0]);
wdenkb13fb012003-10-30 21:49:38 +0000240 /* OS kernel is always the first image */
241 data += 8; /* kernel_len + terminator */
242 for (i=1; len_ptr[i]; ++i)
243 data += 4;
244 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000245 default: printf ("Wrong Image Type for %s command\n", cmdtp->name);
Heiko Schocherfad63402007-07-13 09:54:17 +0200246 show_boot_progress (-5);
wdenk47d1a6e2002-11-03 00:01:44 +0000247 return 1;
248 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200249 show_boot_progress (6);
wdenk47d1a6e2002-11-03 00:01:44 +0000250
251 /*
252 * We have reached the point of no return: we are going to
253 * overwrite all exception vector code, so we cannot easily
254 * recover from any failures any more...
255 */
256
257 iflag = disable_interrupts();
258
wdenkc7de8292002-11-19 11:04:11 +0000259#ifdef CONFIG_AMIGAONEG3SE
260 /*
wdenk8bde7f72003-06-27 21:31:46 +0000261 * We've possible left the caches enabled during
wdenkc7de8292002-11-19 11:04:11 +0000262 * bios emulation, so turn them off again
263 */
264 icache_disable();
265 invalidate_l1_instruction_cache();
266 flush_data_cache();
267 dcache_disable();
268#endif
269
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100270 switch (image_get_comp (hdr)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000271 case IH_COMP_NONE:
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100272 if (image_get_load (hdr) == addr) {
wdenk47d1a6e2002-11-03 00:01:44 +0000273 printf (" XIP %s ... ", name);
274 } else {
275#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
276 size_t l = len;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100277 void *to = (void *)image_get_load (hdr);
wdenk47d1a6e2002-11-03 00:01:44 +0000278 void *from = (void *)data;
279
280 printf (" Loading %s ... ", name);
281
282 while (l > 0) {
283 size_t tail = (l > CHUNKSZ) ? CHUNKSZ : l;
284 WATCHDOG_RESET();
285 memmove (to, from, tail);
286 to += tail;
287 from += tail;
288 l -= tail;
289 }
290#else /* !(CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG) */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100291 memmove ((void *)image_get_load (hdr), (uchar *)data, len);
wdenk47d1a6e2002-11-03 00:01:44 +0000292#endif /* CONFIG_HW_WATCHDOG || CONFIG_WATCHDOG */
293 }
294 break;
295 case IH_COMP_GZIP:
296 printf (" Uncompressing %s ... ", name);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100297 if (gunzip ((void *)image_get_load (hdr), unc_len,
wdenkeedcd072004-09-08 22:03:11 +0000298 (uchar *)data, &len) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000299 puts ("GUNZIP ERROR - must RESET board to recover\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200300 show_boot_progress (-6);
wdenk47d1a6e2002-11-03 00:01:44 +0000301 do_reset (cmdtp, flag, argc, argv);
302 }
303 break;
wdenkc29fdfc2003-08-29 20:57:53 +0000304#ifdef CONFIG_BZIP2
305 case IH_COMP_BZIP2:
306 printf (" Uncompressing %s ... ", name);
wdenk5653fc32004-02-08 22:55:38 +0000307 /*
308 * If we've got less than 4 MB of malloc() space,
309 * use slower decompression algorithm which requires
310 * at most 2300 KB of memory.
311 */
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100312 i = BZ2_bzBuffToBuffDecompress ((char*)image_get_load (hdr),
wdenk5653fc32004-02-08 22:55:38 +0000313 &unc_len, (char *)data, len,
314 CFG_MALLOC_LEN < (4096 * 1024), 0);
wdenkc29fdfc2003-08-29 20:57:53 +0000315 if (i != BZ_OK) {
316 printf ("BUNZIP2 ERROR %d - must RESET board to recover\n", i);
Heiko Schocherfad63402007-07-13 09:54:17 +0200317 show_boot_progress (-6);
wdenkc29fdfc2003-08-29 20:57:53 +0000318 do_reset (cmdtp, flag, argc, argv);
319 }
320 break;
321#endif /* CONFIG_BZIP2 */
wdenk47d1a6e2002-11-03 00:01:44 +0000322 default:
323 if (iflag)
324 enable_interrupts();
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100325 printf ("Unimplemented compression type %d\n", image_get_comp (hdr));
Heiko Schocherfad63402007-07-13 09:54:17 +0200326 show_boot_progress (-7);
wdenk47d1a6e2002-11-03 00:01:44 +0000327 return 1;
328 }
wdenk4b9206e2004-03-23 22:14:11 +0000329 puts ("OK\n");
Heiko Schocherfad63402007-07-13 09:54:17 +0200330 show_boot_progress (7);
wdenk47d1a6e2002-11-03 00:01:44 +0000331
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100332 switch (image_get_type (hdr)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000333 case IH_TYPE_STANDALONE:
wdenk47d1a6e2002-11-03 00:01:44 +0000334 if (iflag)
335 enable_interrupts();
336
wdenk4a6fd342003-04-12 23:38:12 +0000337 /* load (and uncompress), but don't start if "autostart"
338 * is set to "no"
339 */
wdenk4a551702003-10-08 23:26:14 +0000340 if (((s = getenv("autostart")) != NULL) && (strcmp(s,"no") == 0)) {
341 char buf[32];
342 sprintf(buf, "%lX", len);
343 setenv("filesize", buf);
wdenk4a6fd342003-04-12 23:38:12 +0000344 return 0;
wdenk4a551702003-10-08 23:26:14 +0000345 }
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100346 appl = (int (*)(int, char *[]))image_get_ep (hdr);
wdenkb13fb012003-10-30 21:49:38 +0000347 (*appl)(argc-1, &argv[1]);
wdenk4a6fd342003-04-12 23:38:12 +0000348 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000349 case IH_TYPE_KERNEL:
350 case IH_TYPE_MULTI:
351 /* handled below */
352 break;
353 default:
354 if (iflag)
355 enable_interrupts();
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100356 printf ("Can't boot image type %d\n", image_get_type (hdr));
Heiko Schocherfad63402007-07-13 09:54:17 +0200357 show_boot_progress (-8);
wdenk47d1a6e2002-11-03 00:01:44 +0000358 return 1;
359 }
Heiko Schocherfad63402007-07-13 09:54:17 +0200360 show_boot_progress (8);
wdenk47d1a6e2002-11-03 00:01:44 +0000361
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100362 switch (image_get_os (hdr)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000363 default: /* handled by (original) Linux case */
364 case IH_OS_LINUX:
wdenkf72da342003-10-10 10:05:42 +0000365#ifdef CONFIG_SILENT_CONSOLE
366 fixup_silent_linux();
367#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000368 do_bootm_linux (cmdtp, flag, argc, argv,
369 addr, len_ptr, verify);
370 break;
371 case IH_OS_NETBSD:
372 do_bootm_netbsd (cmdtp, flag, argc, argv,
373 addr, len_ptr, verify);
374 break;
wdenk8bde7f72003-06-27 21:31:46 +0000375
wdenk1f4bb372003-07-27 00:21:01 +0000376#ifdef CONFIG_LYNXKDI
377 case IH_OS_LYNXOS:
378 do_bootm_lynxkdi (cmdtp, flag, argc, argv,
379 addr, len_ptr, verify);
380 break;
381#endif
382
wdenkd791b1d2003-04-20 14:04:18 +0000383 case IH_OS_RTEMS:
384 do_bootm_rtems (cmdtp, flag, argc, argv,
385 addr, len_ptr, verify);
386 break;
wdenk8bde7f72003-06-27 21:31:46 +0000387
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500388#if defined(CONFIG_CMD_ELF)
wdenk47d1a6e2002-11-03 00:01:44 +0000389 case IH_OS_VXWORKS:
390 do_bootm_vxworks (cmdtp, flag, argc, argv,
391 addr, len_ptr, verify);
392 break;
393 case IH_OS_QNX:
394 do_bootm_qnxelf (cmdtp, flag, argc, argv,
395 addr, len_ptr, verify);
396 break;
Jon Loeliger90253172007-07-10 11:02:44 -0500397#endif
wdenk7f70e852003-05-20 14:25:27 +0000398#ifdef CONFIG_ARTOS
399 case IH_OS_ARTOS:
400 do_bootm_artos (cmdtp, flag, argc, argv,
401 addr, len_ptr, verify);
402 break;
403#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000404 }
405
Heiko Schocherfad63402007-07-13 09:54:17 +0200406 show_boot_progress (-9);
wdenk47d1a6e2002-11-03 00:01:44 +0000407#ifdef DEBUG
wdenk4b9206e2004-03-23 22:14:11 +0000408 puts ("\n## Control returned to monitor - resetting...\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000409 do_reset (cmdtp, flag, argc, argv);
410#endif
411 return 1;
412}
413
wdenk0d498392003-07-01 21:06:45 +0000414U_BOOT_CMD(
415 bootm, CFG_MAXARGS, 1, do_bootm,
wdenk8bde7f72003-06-27 21:31:46 +0000416 "bootm - boot application image from memory\n",
417 "[addr [arg ...]]\n - boot application image stored in memory\n"
wdenk9d5028c2004-11-21 00:06:33 +0000418 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
419 "\t'arg' can be the address of an initrd image\n"
Gerald Van Baren213bf8c2007-03-31 12:23:51 -0400420#if defined(CONFIG_OF_FLAT_TREE) || defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500421 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
Detlev Zundel5441f612007-10-19 16:47:26 +0200422 "\ta third argument is required which is the address of the\n"
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500423 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
424 "\tuse a '-' for the second argument. If you do not pass a third\n"
425 "\ta bd_info struct will be passed instead\n"
426#endif
wdenk8bde7f72003-06-27 21:31:46 +0000427);
428
wdenkf72da342003-10-10 10:05:42 +0000429#ifdef CONFIG_SILENT_CONSOLE
430static void
431fixup_silent_linux ()
432{
wdenkf72da342003-10-10 10:05:42 +0000433 char buf[256], *start, *end;
434 char *cmdline = getenv ("bootargs");
435
436 /* Only fix cmdline when requested */
437 if (!(gd->flags & GD_FLG_SILENT))
438 return;
439
440 debug ("before silent fix-up: %s\n", cmdline);
441 if (cmdline) {
442 if ((start = strstr (cmdline, "console=")) != NULL) {
443 end = strchr (start, ' ');
444 strncpy (buf, cmdline, (start - cmdline + 8));
445 if (end)
446 strcpy (buf + (start - cmdline + 8), end);
447 else
448 buf[start - cmdline + 8] = '\0';
449 } else {
450 strcpy (buf, cmdline);
451 strcat (buf, " console=");
452 }
453 } else {
454 strcpy (buf, "console=");
455 }
456
457 setenv ("bootargs", buf);
458 debug ("after silent fix-up: %s\n", buf);
459}
460#endif /* CONFIG_SILENT_CONSOLE */
461
wdenk47d1a6e2002-11-03 00:01:44 +0000462static void
463do_bootm_netbsd (cmd_tbl_t *cmdtp, int flag,
464 int argc, char *argv[],
465 ulong addr,
466 ulong *len_ptr,
467 int verify)
468{
wdenk47d1a6e2002-11-03 00:01:44 +0000469 image_header_t *hdr = &header;
470
471 void (*loader)(bd_t *, image_header_t *, char *, char *);
472 image_header_t *img_addr;
473 char *consdev;
474 char *cmdline;
475
476
477 /*
478 * Booting a (NetBSD) kernel image
479 *
480 * This process is pretty similar to a standalone application:
481 * The (first part of an multi-) image must be a stage-2 loader,
482 * which in turn is responsible for loading & invoking the actual
483 * kernel. The only differences are the parameters being passed:
484 * besides the board info strucure, the loader expects a command
485 * line, the name of the console device, and (optionally) the
486 * address of the original image header.
487 */
488
489 img_addr = 0;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100490 if ((image_check_type (hdr, IH_TYPE_MULTI)) && (len_ptr[1]))
wdenk47d1a6e2002-11-03 00:01:44 +0000491 img_addr = (image_header_t *) addr;
492
493
494 consdev = "";
495#if defined (CONFIG_8xx_CONS_SMC1)
496 consdev = "smc1";
497#elif defined (CONFIG_8xx_CONS_SMC2)
498 consdev = "smc2";
499#elif defined (CONFIG_8xx_CONS_SCC2)
500 consdev = "scc2";
501#elif defined (CONFIG_8xx_CONS_SCC3)
502 consdev = "scc3";
503#endif
504
505 if (argc > 2) {
506 ulong len;
507 int i;
508
509 for (i=2, len=0 ; i<argc ; i+=1)
510 len += strlen (argv[i]) + 1;
511 cmdline = malloc (len);
512
513 for (i=2, len=0 ; i<argc ; i+=1) {
514 if (i > 2)
515 cmdline[len++] = ' ';
516 strcpy (&cmdline[len], argv[i]);
517 len += strlen (argv[i]);
518 }
519 } else if ((cmdline = getenv("bootargs")) == NULL) {
520 cmdline = "";
521 }
522
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100523 loader = (void (*)(bd_t *, image_header_t *, char *, char *))image_get_ep (hdr);
wdenk47d1a6e2002-11-03 00:01:44 +0000524
525 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
526 (ulong)loader);
527
Heiko Schocherfad63402007-07-13 09:54:17 +0200528 show_boot_progress (15);
wdenk47d1a6e2002-11-03 00:01:44 +0000529
530 /*
531 * NetBSD Stage-2 Loader Parameters:
532 * r3: ptr to board info data
533 * r4: image address
534 * r5: console device
535 * r6: boot args string
536 */
537 (*loader) (gd->bd, img_addr, consdev, cmdline);
538}
539
wdenk7f70e852003-05-20 14:25:27 +0000540#if defined(CONFIG_ARTOS) && defined(CONFIG_PPC)
541
542/* Function that returns a character from the environment */
543extern uchar (*env_get_char)(int);
544
545static void
546do_bootm_artos (cmd_tbl_t *cmdtp, int flag,
547 int argc, char *argv[],
548 ulong addr,
549 ulong *len_ptr,
550 int verify)
551{
wdenk7f70e852003-05-20 14:25:27 +0000552 ulong top;
553 char *s, *cmdline;
554 char **fwenv, **ss;
555 int i, j, nxt, len, envno, envsz;
556 bd_t *kbd;
557 void (*entry)(bd_t *bd, char *cmdline, char **fwenv, ulong top);
558 image_header_t *hdr = &header;
559
560 /*
561 * Booting an ARTOS kernel image + application
562 */
563
564 /* this used to be the top of memory, but was wrong... */
565#ifdef CONFIG_PPC
566 /* get stack pointer */
567 asm volatile ("mr %0,1" : "=r"(top) );
568#endif
569 debug ("## Current stack ends at 0x%08lX ", top);
570
571 top -= 2048; /* just to be sure */
572 if (top > CFG_BOOTMAPSZ)
573 top = CFG_BOOTMAPSZ;
574 top &= ~0xF;
575
576 debug ("=> set upper limit to 0x%08lX\n", top);
577
578 /* first check the artos specific boot args, then the linux args*/
579 if ((s = getenv("abootargs")) == NULL && (s = getenv("bootargs")) == NULL)
580 s = "";
581
582 /* get length of cmdline, and place it */
583 len = strlen(s);
584 top = (top - (len + 1)) & ~0xF;
585 cmdline = (char *)top;
586 debug ("## cmdline at 0x%08lX ", top);
587 strcpy(cmdline, s);
588
589 /* copy bdinfo */
590 top = (top - sizeof(bd_t)) & ~0xF;
591 debug ("## bd at 0x%08lX ", top);
592 kbd = (bd_t *)top;
593 memcpy(kbd, gd->bd, sizeof(bd_t));
594
595 /* first find number of env entries, and their size */
596 envno = 0;
597 envsz = 0;
598 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
599 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
600 ;
601 envno++;
602 envsz += (nxt - i) + 1; /* plus trailing zero */
603 }
604 envno++; /* plus the terminating zero */
605 debug ("## %u envvars total size %u ", envno, envsz);
606
607 top = (top - sizeof(char **)*envno) & ~0xF;
608 fwenv = (char **)top;
609 debug ("## fwenv at 0x%08lX ", top);
610
611 top = (top - envsz) & ~0xF;
612 s = (char *)top;
613 ss = fwenv;
614
615 /* now copy them */
616 for (i = 0; env_get_char(i) != '\0'; i = nxt + 1) {
617 for (nxt = i; env_get_char(nxt) != '\0'; ++nxt)
618 ;
619 *ss++ = s;
620 for (j = i; j < nxt; ++j)
621 *s++ = env_get_char(j);
622 *s++ = '\0';
623 }
624 *ss++ = NULL; /* terminate */
625
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100626 entry = (void (*)(bd_t *, char *, char **, ulong))image_get_ep (hdr);
wdenk7f70e852003-05-20 14:25:27 +0000627 (*entry)(kbd, cmdline, fwenv, top);
628}
629#endif
630
631
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500632#if defined(CONFIG_CMD_BOOTD)
wdenk47d1a6e2002-11-03 00:01:44 +0000633int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
634{
635 int rcode = 0;
636#ifndef CFG_HUSH_PARSER
637 if (run_command (getenv ("bootcmd"), flag) < 0) rcode = 1;
638#else
639 if (parse_string_outer(getenv("bootcmd"),
640 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0 ) rcode = 1;
641#endif
642 return rcode;
643}
wdenk8bde7f72003-06-27 21:31:46 +0000644
wdenk0d498392003-07-01 21:06:45 +0000645U_BOOT_CMD(
646 boot, 1, 1, do_bootd,
wdenk9d2b18a2003-06-28 23:11:04 +0000647 "boot - boot default, i.e., run 'bootcmd'\n",
648 NULL
649);
650
651/* keep old command name "bootd" for backward compatibility */
wdenk0d498392003-07-01 21:06:45 +0000652U_BOOT_CMD(
653 bootd, 1, 1, do_bootd,
wdenk8bde7f72003-06-27 21:31:46 +0000654 "bootd - boot default, i.e., run 'bootcmd'\n",
655 NULL
656);
657
wdenk47d1a6e2002-11-03 00:01:44 +0000658#endif
659
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500660#if defined(CONFIG_CMD_IMI)
wdenk47d1a6e2002-11-03 00:01:44 +0000661int do_iminfo ( cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
662{
663 int arg;
664 ulong addr;
665 int rcode=0;
666
667 if (argc < 2) {
668 return image_info (load_addr);
669 }
670
671 for (arg=1; arg <argc; ++arg) {
672 addr = simple_strtoul(argv[arg], NULL, 16);
673 if (image_info (addr) != 0) rcode = 1;
674 }
675 return rcode;
676}
677
678static int image_info (ulong addr)
679{
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100680 image_header_t *hdr = (image_header_t *)addr;
wdenk47d1a6e2002-11-03 00:01:44 +0000681
682 printf ("\n## Checking Image at %08lx ...\n", addr);
683
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100684 if (!image_check_magic (hdr)) {
wdenk4b9206e2004-03-23 22:14:11 +0000685 puts (" Bad Magic Number\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000686 return 1;
687 }
688
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100689 if (!image_check_hcrc (hdr)) {
wdenk4b9206e2004-03-23 22:14:11 +0000690 puts (" Bad Header Checksum\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000691 return 1;
692 }
693
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100694 print_image_hdr (hdr);
wdenk47d1a6e2002-11-03 00:01:44 +0000695
wdenk4b9206e2004-03-23 22:14:11 +0000696 puts (" Verifying Checksum ... ");
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100697 if (!image_check_dcrc (hdr)) {
wdenk4b9206e2004-03-23 22:14:11 +0000698 puts (" Bad Data CRC\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000699 return 1;
700 }
wdenk4b9206e2004-03-23 22:14:11 +0000701 puts ("OK\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000702 return 0;
703}
wdenk0d498392003-07-01 21:06:45 +0000704
705U_BOOT_CMD(
706 iminfo, CFG_MAXARGS, 1, do_iminfo,
wdenk8bde7f72003-06-27 21:31:46 +0000707 "iminfo - print header information for application image\n",
708 "addr [addr ...]\n"
709 " - print header information for application image starting at\n"
710 " address 'addr' in memory; this includes verification of the\n"
711 " image contents (magic number, header and payload checksums)\n"
712);
713
Jon Loeliger90253172007-07-10 11:02:44 -0500714#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000715
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500716#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +0000717/*-----------------------------------------------------------------------
718 * List all images found in flash.
719 */
720int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
721{
722 flash_info_t *info;
723 int i, j;
724 image_header_t *hdr;
wdenk27b207f2003-07-24 23:38:38 +0000725
726 for (i=0, info=&flash_info[0]; i<CFG_MAX_FLASH_BANKS; ++i, ++info) {
727 if (info->flash_id == FLASH_UNKNOWN)
728 goto next_bank;
Marian Balakowicze6f2e902005-10-11 19:09:42 +0200729 for (j=0; j<info->sector_count; ++j) {
wdenk27b207f2003-07-24 23:38:38 +0000730
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100731 hdr = (image_header_t *)info->start[j];
732
733 if (!hdr || !image_check_magic (hdr))
wdenk27b207f2003-07-24 23:38:38 +0000734 goto next_sector;
735
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100736 if (!image_check_hcrc (hdr))
wdenk27b207f2003-07-24 23:38:38 +0000737 goto next_sector;
738
739 printf ("Image at %08lX:\n", (ulong)hdr);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100740 print_image_hdr (hdr);
wdenk5bb226e2003-11-17 21:14:37 +0000741
wdenk4b9206e2004-03-23 22:14:11 +0000742 puts (" Verifying Checksum ... ");
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100743 if (!image_check_dcrc (hdr)) {
744 puts ("Bad Data CRC\n");
745 } else {
746 puts ("OK\n");
wdenk5bb226e2003-11-17 21:14:37 +0000747 }
wdenkbdccc4f2003-08-05 17:43:17 +0000748next_sector: ;
wdenk27b207f2003-07-24 23:38:38 +0000749 }
wdenkbdccc4f2003-08-05 17:43:17 +0000750next_bank: ;
wdenk27b207f2003-07-24 23:38:38 +0000751 }
752
753 return (0);
754}
755
756U_BOOT_CMD(
757 imls, 1, 1, do_imls,
758 "imls - list all images found in flash\n",
759 "\n"
760 " - Prints information about all images found at sector\n"
761 " boundaries in flash.\n"
762);
Jon Loeliger90253172007-07-10 11:02:44 -0500763#endif
wdenk27b207f2003-07-24 23:38:38 +0000764
wdenk47d1a6e2002-11-03 00:01:44 +0000765void
766print_image_hdr (image_header_t *hdr)
767{
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500768#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100769 time_t timestamp = (time_t)image_get_time (hdr);
wdenk47d1a6e2002-11-03 00:01:44 +0000770 struct rtc_time tm;
771#endif
772
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100773 printf (" Image Name: %.*s\n", IH_NMLEN, image_get_name (hdr));
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500774#if defined(CONFIG_TIMESTAMP) || defined(CONFIG_CMD_DATE)
wdenk47d1a6e2002-11-03 00:01:44 +0000775 to_tm (timestamp, &tm);
776 printf (" Created: %4d-%02d-%02d %2d:%02d:%02d UTC\n",
777 tm.tm_year, tm.tm_mon, tm.tm_mday,
778 tm.tm_hour, tm.tm_min, tm.tm_sec);
Jon Loeliger90253172007-07-10 11:02:44 -0500779#endif
wdenk4b9206e2004-03-23 22:14:11 +0000780 puts (" Image Type: "); print_type(hdr);
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100781 printf ("\n Data Size: %d Bytes = ", image_get_data_size (hdr));
782 print_size (image_get_data_size (hdr), "\n");
wdenk4b9206e2004-03-23 22:14:11 +0000783 printf (" Load Address: %08x\n"
784 " Entry Point: %08x\n",
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100785 image_get_load (hdr), image_get_ep (hdr));
wdenk47d1a6e2002-11-03 00:01:44 +0000786
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100787 if (image_check_type (hdr, IH_TYPE_MULTI)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000788 int i;
789 ulong len;
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100790 ulong *len_ptr = (ulong *)((ulong)hdr + image_get_header_size ());
wdenk47d1a6e2002-11-03 00:01:44 +0000791
wdenk4b9206e2004-03-23 22:14:11 +0000792 puts (" Contents:\n");
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100793 for (i=0; (len = image_to_cpu (*len_ptr)); ++i, ++len_ptr) {
wdenk47d1a6e2002-11-03 00:01:44 +0000794 printf (" Image %d: %8ld Bytes = ", i, len);
795 print_size (len, "\n");
796 }
797 }
798}
799
800
801static void
802print_type (image_header_t *hdr)
803{
804 char *os, *arch, *type, *comp;
805
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100806 switch (image_get_os (hdr)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000807 case IH_OS_INVALID: os = "Invalid OS"; break;
808 case IH_OS_NETBSD: os = "NetBSD"; break;
809 case IH_OS_LINUX: os = "Linux"; break;
810 case IH_OS_VXWORKS: os = "VxWorks"; break;
811 case IH_OS_QNX: os = "QNX"; break;
812 case IH_OS_U_BOOT: os = "U-Boot"; break;
wdenkd791b1d2003-04-20 14:04:18 +0000813 case IH_OS_RTEMS: os = "RTEMS"; break;
wdenk7f70e852003-05-20 14:25:27 +0000814#ifdef CONFIG_ARTOS
815 case IH_OS_ARTOS: os = "ARTOS"; break;
816#endif
wdenk1f4bb372003-07-27 00:21:01 +0000817#ifdef CONFIG_LYNXKDI
818 case IH_OS_LYNXOS: os = "LynxOS"; break;
819#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000820 default: os = "Unknown OS"; break;
821 }
822
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100823 switch (image_get_arch (hdr)) {
824 case IH_ARCH_INVALID: arch = "Invalid CPU"; break;
825 case IH_ARCH_ALPHA: arch = "Alpha"; break;
826 case IH_ARCH_ARM: arch = "ARM"; break;
827 case IH_ARCH_AVR32: arch = "AVR32"; break;
828 case IH_ARCH_BLACKFIN: arch = "Blackfin"; break;
829 case IH_ARCH_I386: arch = "Intel x86"; break;
830 case IH_ARCH_IA64: arch = "IA64"; break;
831 case IH_ARCH_M68K: arch = "M68K"; break;
832 case IH_ARCH_MICROBLAZE:arch = "Microblaze"; break;
833 case IH_ARCH_MIPS64: arch = "MIPS 64 Bit"; break;
834 case IH_ARCH_MIPS: arch = "MIPS"; break;
835 case IH_ARCH_NIOS2: arch = "Nios-II"; break;
836 case IH_ARCH_NIOS: arch = "Nios"; break;
837 case IH_ARCH_PPC: arch = "PowerPC"; break;
838 case IH_ARCH_S390: arch = "IBM S390"; break;
839 case IH_ARCH_SH: arch = "SuperH"; break;
840 case IH_ARCH_SPARC64: arch = "SPARC 64 Bit"; break;
841 case IH_ARCH_SPARC: arch = "SPARC"; break;
wdenk47d1a6e2002-11-03 00:01:44 +0000842 default: arch = "Unknown Architecture"; break;
843 }
844
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100845 switch (image_get_type (hdr)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000846 case IH_TYPE_INVALID: type = "Invalid Image"; break;
847 case IH_TYPE_STANDALONE:type = "Standalone Program"; break;
848 case IH_TYPE_KERNEL: type = "Kernel Image"; break;
849 case IH_TYPE_RAMDISK: type = "RAMDisk Image"; break;
850 case IH_TYPE_MULTI: type = "Multi-File Image"; break;
851 case IH_TYPE_FIRMWARE: type = "Firmware"; break;
852 case IH_TYPE_SCRIPT: type = "Script"; break;
Matthew McClintock25c751e2006-08-16 10:54:09 -0500853 case IH_TYPE_FLATDT: type = "Flat Device Tree"; break;
wdenk47d1a6e2002-11-03 00:01:44 +0000854 default: type = "Unknown Image"; break;
855 }
856
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100857 switch (image_get_comp (hdr)) {
wdenk47d1a6e2002-11-03 00:01:44 +0000858 case IH_COMP_NONE: comp = "uncompressed"; break;
859 case IH_COMP_GZIP: comp = "gzip compressed"; break;
860 case IH_COMP_BZIP2: comp = "bzip2 compressed"; break;
861 default: comp = "unknown compression"; break;
862 }
863
864 printf ("%s %s %s (%s)", arch, os, type, comp);
865}
866
867#define ZALLOC_ALIGNMENT 16
868
869static void *zalloc(void *x, unsigned items, unsigned size)
870{
871 void *p;
872
873 size *= items;
874 size = (size + ZALLOC_ALIGNMENT - 1) & ~(ZALLOC_ALIGNMENT - 1);
875
876 p = malloc (size);
877
878 return (p);
879}
880
881static void zfree(void *x, void *addr, unsigned nb)
882{
883 free (addr);
884}
885
886#define HEAD_CRC 2
887#define EXTRA_FIELD 4
888#define ORIG_NAME 8
889#define COMMENT 0x10
890#define RESERVED 0xe0
891
892#define DEFLATED 8
893
wdenkeedcd072004-09-08 22:03:11 +0000894int gunzip(void *dst, int dstlen, unsigned char *src, unsigned long *lenp)
wdenk47d1a6e2002-11-03 00:01:44 +0000895{
896 z_stream s;
897 int r, i, flags;
898
899 /* skip header */
900 i = 10;
901 flags = src[3];
902 if (src[2] != DEFLATED || (flags & RESERVED) != 0) {
wdenk4b9206e2004-03-23 22:14:11 +0000903 puts ("Error: Bad gzipped data\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000904 return (-1);
905 }
906 if ((flags & EXTRA_FIELD) != 0)
907 i = 12 + src[10] + (src[11] << 8);
908 if ((flags & ORIG_NAME) != 0)
909 while (src[i++] != 0)
910 ;
911 if ((flags & COMMENT) != 0)
912 while (src[i++] != 0)
913 ;
914 if ((flags & HEAD_CRC) != 0)
915 i += 2;
916 if (i >= *lenp) {
wdenk4b9206e2004-03-23 22:14:11 +0000917 puts ("Error: gunzip out of data in header\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000918 return (-1);
919 }
920
921 s.zalloc = zalloc;
922 s.zfree = zfree;
923#if defined(CONFIG_HW_WATCHDOG) || defined(CONFIG_WATCHDOG)
924 s.outcb = (cb_func)WATCHDOG_RESET;
925#else
926 s.outcb = Z_NULL;
927#endif /* CONFIG_HW_WATCHDOG */
928
929 r = inflateInit2(&s, -MAX_WBITS);
930 if (r != Z_OK) {
931 printf ("Error: inflateInit2() returned %d\n", r);
932 return (-1);
933 }
934 s.next_in = src + i;
935 s.avail_in = *lenp - i;
936 s.next_out = dst;
937 s.avail_out = dstlen;
938 r = inflate(&s, Z_FINISH);
939 if (r != Z_OK && r != Z_STREAM_END) {
940 printf ("Error: inflate() returned %d\n", r);
941 return (-1);
942 }
943 *lenp = s.next_out - (unsigned char *) dst;
944 inflateEnd(&s);
945
946 return (0);
947}
948
wdenkc29fdfc2003-08-29 20:57:53 +0000949#ifdef CONFIG_BZIP2
950void bz_internal_error(int errcode)
951{
952 printf ("BZIP2 internal error %d\n", errcode);
953}
954#endif /* CONFIG_BZIP2 */
955
wdenkd791b1d2003-04-20 14:04:18 +0000956static void
957do_bootm_rtems (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
958 ulong addr, ulong *len_ptr, int verify)
959{
wdenkd791b1d2003-04-20 14:04:18 +0000960 image_header_t *hdr = &header;
961 void (*entry_point)(bd_t *);
962
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100963 entry_point = (void (*)(bd_t *))image_get_ep (hdr);
wdenkd791b1d2003-04-20 14:04:18 +0000964
965 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
966 (ulong)entry_point);
967
Heiko Schocherfad63402007-07-13 09:54:17 +0200968 show_boot_progress (15);
wdenkd791b1d2003-04-20 14:04:18 +0000969
970 /*
971 * RTEMS Parameters:
972 * r3: ptr to board info data
973 */
974
975 (*entry_point ) ( gd->bd );
976}
977
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500978#if defined(CONFIG_CMD_ELF)
wdenk47d1a6e2002-11-03 00:01:44 +0000979static void
980do_bootm_vxworks (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
981 ulong addr, ulong *len_ptr, int verify)
982{
983 image_header_t *hdr = &header;
984 char str[80];
985
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100986 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +0000987 setenv("loadaddr", str);
988 do_bootvx(cmdtp, 0, 0, NULL);
989}
990
991static void
992do_bootm_qnxelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
993 ulong addr, ulong *len_ptr, int verify)
994{
995 image_header_t *hdr = &header;
996 char *local_args[2];
997 char str[16];
998
Marian Balakowiczb97a2a02008-01-08 18:14:09 +0100999 sprintf(str, "%x", image_get_ep (hdr)); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001000 local_args[0] = argv[0];
1001 local_args[1] = str; /* and provide it via the arguments */
1002 do_bootelf(cmdtp, 0, 2, local_args);
1003}
Jon Loeliger90253172007-07-10 11:02:44 -05001004#endif
wdenk1f4bb372003-07-27 00:21:01 +00001005
1006#ifdef CONFIG_LYNXKDI
1007static void
1008do_bootm_lynxkdi (cmd_tbl_t *cmdtp, int flag,
1009 int argc, char *argv[],
1010 ulong addr,
1011 ulong *len_ptr,
1012 int verify)
1013{
1014 lynxkdi_boot( &header );
1015}
1016
1017#endif /* CONFIG_LYNXKDI */