blob: b02da3e34a692fb536703ff464ec1ca91939f1d0 [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
wdenk47d1a6e2002-11-03 00:01:44 +000025/*
26 * Boot support
27 */
28#include <common.h>
29#include <watchdog.h>
30#include <command.h>
wdenk47d1a6e2002-11-03 00:01:44 +000031#include <image.h>
32#include <malloc.h>
33#include <zlib.h>
wdenkc29fdfc2003-08-29 20:57:53 +000034#include <bzlib.h>
wdenk7f70e852003-05-20 14:25:27 +000035#include <environment.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060036#include <lmb.h>
wdenk47d1a6e2002-11-03 00:01:44 +000037#include <asm/byteorder.h>
wdenk8bde7f72003-06-27 21:31:46 +000038
Stefan Roeseebb86c42008-07-30 09:59:51 +020039#if defined(CONFIG_CMD_USB)
Markus Klotzbücher3d71c812008-07-10 14:47:09 +020040#include <usb.h>
41#endif
42
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020043#ifdef CONFIG_SYS_HUSH_PARSER
wdenk47d1a6e2002-11-03 00:01:44 +000044#include <hush.h>
45#endif
46
Kumar Gala54f9c862008-08-15 08:24:39 -050047#if defined(CONFIG_OF_LIBFDT)
48#include <fdt.h>
49#include <libfdt.h>
50#include <fdt_support.h>
51#endif
52
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +020053#ifdef CONFIG_LZMA
54#define _7ZIP_BYTE_DEFINED /* Byte already defined by zlib */
55#include <lzma/LzmaTypes.h>
56#include <lzma/LzmaDecode.h>
57#include <lzma/LzmaTools.h>
58#endif /* CONFIG_LZMA */
59
Marian Balakowicz1ee11802008-01-08 18:17:10 +010060DECLARE_GLOBAL_DATA_PTR;
61
62extern int gunzip (void *dst, int dstlen, unsigned char *src, unsigned long *lenp);
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020063#ifndef CONFIG_SYS_BOOTM_LEN
64#define CONFIG_SYS_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
wdenk47d1a6e2002-11-03 00:01:44 +000065#endif
66
Marian Balakowicz321359f2008-01-08 18:11:43 +010067#ifdef CONFIG_BZIP2
68extern void bz_internal_error(int);
wdenk228f29a2002-12-08 09:53:23 +000069#endif
70
Jon Loeligerbaa26db2007-07-08 17:51:39 -050071#if defined(CONFIG_CMD_IMI)
wdenk47d1a6e2002-11-03 00:01:44 +000072static int image_info (unsigned long addr);
73#endif
wdenk27b207f2003-07-24 23:38:38 +000074
Jon Loeligerbaa26db2007-07-08 17:51:39 -050075#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +000076#include <flash.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020077extern flash_info_t flash_info[]; /* info for FLASH chips */
wdenk27b207f2003-07-24 23:38:38 +000078static int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
79#endif
80
Marian Balakowicz1ee11802008-01-08 18:17:10 +010081#ifdef CONFIG_SILENT_CONSOLE
82static void fixup_silent_linux (void);
wdenk2262cfe2002-11-18 00:14:45 +000083#endif
84
Marian Balakowicz6986a382008-03-12 10:01:05 +010085static image_header_t *image_get_kernel (ulong img_addr, int verify);
86#if defined(CONFIG_FIT)
87static int fit_check_kernel (const void *fit, int os_noffset, int verify);
88#endif
89
Marian Balakowicz9a4daad2008-02-29 14:58:34 +010090static void *boot_get_kernel (cmd_tbl_t *cmdtp, int flag,int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +010091 bootm_headers_t *images, ulong *os_data, ulong *os_len);
Marian Balakowicz1ee11802008-01-08 18:17:10 +010092extern int do_reset (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
wdenk47d1a6e2002-11-03 00:01:44 +000093
wdenk47d1a6e2002-11-03 00:01:44 +000094/*
95 * Continue booting an OS image; caller already has:
96 * - copied image header to global variable `header'
97 * - checked header magic number, checksums (both header & image),
98 * - verified image architecture (PPC) and type (KERNEL or MULTI),
99 * - loaded (first part of) image to header load address,
100 * - disabled interrupts.
101 */
Kumar Gala40d7e992008-08-15 08:24:45 -0500102typedef int boot_os_fn (int flag, int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100103 bootm_headers_t *images); /* pointers to os/initrd/fdt */
wdenk47d1a6e2002-11-03 00:01:44 +0000104
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100105extern boot_os_fn do_bootm_linux;
106static boot_os_fn do_bootm_netbsd;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100107#if defined(CONFIG_LYNXKDI)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100108static boot_os_fn do_bootm_lynxkdi;
109extern void lynxkdi_boot (image_header_t *);
wdenk8bde7f72003-06-27 21:31:46 +0000110#endif
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100111static boot_os_fn do_bootm_rtems;
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500112#if defined(CONFIG_CMD_ELF)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100113static boot_os_fn do_bootm_vxworks;
114static boot_os_fn do_bootm_qnxelf;
115int do_bootvx (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
116int do_bootelf (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[]);
Jon Loeliger90253172007-07-10 11:02:44 -0500117#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500118#if defined(CONFIG_INTEGRITY)
119static boot_os_fn do_bootm_integrity;
120#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000121
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200122ulong load_addr = CONFIG_SYS_LOAD_ADDR; /* Default Load Address */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100123static bootm_headers_t images; /* pointers to os/initrd/fdt images */
Stefan Roese15940c92006-03-13 11:16:36 +0100124
Kumar Galae822d7f2008-02-27 21:51:49 -0600125void __board_lmb_reserve(struct lmb *lmb)
126{
127 /* please define platform specific board_lmb_reserve() */
128}
129void board_lmb_reserve(struct lmb *lmb) __attribute__((weak, alias("__board_lmb_reserve")));
wdenk47d1a6e2002-11-03 00:01:44 +0000130
Kumar Gala76da19d2008-10-16 21:52:08 -0500131void __arch_lmb_reserve(struct lmb *lmb)
132{
133 /* please define platform specific arch_lmb_reserve() */
134}
135void arch_lmb_reserve(struct lmb *lmb) __attribute__((weak, alias("__arch_lmb_reserve")));
136
Kumar Galac4f94192008-08-15 08:24:37 -0500137#if defined(__ARM__)
138 #define IH_INITRD_ARCH IH_ARCH_ARM
139#elif defined(__avr32__)
140 #define IH_INITRD_ARCH IH_ARCH_AVR32
141#elif defined(__bfin__)
142 #define IH_INITRD_ARCH IH_ARCH_BLACKFIN
143#elif defined(__I386__)
144 #define IH_INITRD_ARCH IH_ARCH_I386
145#elif defined(__M68K__)
146 #define IH_INITRD_ARCH IH_ARCH_M68K
147#elif defined(__microblaze__)
148 #define IH_INITRD_ARCH IH_ARCH_MICROBLAZE
149#elif defined(__mips__)
150 #define IH_INITRD_ARCH IH_ARCH_MIPS
151#elif defined(__nios__)
152 #define IH_INITRD_ARCH IH_ARCH_NIOS
153#elif defined(__nios2__)
154 #define IH_INITRD_ARCH IH_ARCH_NIOS2
155#elif defined(__PPC__)
156 #define IH_INITRD_ARCH IH_ARCH_PPC
157#elif defined(__sh__)
158 #define IH_INITRD_ARCH IH_ARCH_SH
159#elif defined(__sparc__)
160 #define IH_INITRD_ARCH IH_ARCH_SPARC
161#else
162# error Unknown CPU type
163#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000164
Kumar Gala396f6352008-08-15 08:24:41 -0500165static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000166{
Becky Bruce391fd932008-06-09 20:37:18 -0500167 ulong mem_start;
168 phys_size_t mem_size;
Kumar Gala396f6352008-08-15 08:24:41 -0500169 void *os_hdr;
Kumar Galac4f94192008-08-15 08:24:37 -0500170 int ret;
wdenk47d1a6e2002-11-03 00:01:44 +0000171
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100172 memset ((void *)&images, 0, sizeof (images));
Bartlomiej Siekaedbed242008-04-18 12:39:23 +0200173 images.verify = getenv_yesno ("verify");
wdenk47d1a6e2002-11-03 00:01:44 +0000174
Kumar Galae906cfa2008-08-15 08:24:40 -0500175 lmb_init(&images.lmb);
wdenk47d1a6e2002-11-03 00:01:44 +0000176
Kumar Galad3f2fa02008-02-27 21:51:50 -0600177 mem_start = getenv_bootm_low();
178 mem_size = getenv_bootm_size();
wdenk47d1a6e2002-11-03 00:01:44 +0000179
Kumar Galae906cfa2008-08-15 08:24:40 -0500180 lmb_add(&images.lmb, (phys_addr_t)mem_start, mem_size);
wdenk47d1a6e2002-11-03 00:01:44 +0000181
Kumar Gala76da19d2008-10-16 21:52:08 -0500182 arch_lmb_reserve(&images.lmb);
Kumar Galae906cfa2008-08-15 08:24:40 -0500183 board_lmb_reserve(&images.lmb);
wdenk47d1a6e2002-11-03 00:01:44 +0000184
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100185 /* get kernel image header, start address and length */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100186 os_hdr = boot_get_kernel (cmdtp, flag, argc, argv,
Kumar Gala396f6352008-08-15 08:24:41 -0500187 &images, &images.os.image_start, &images.os.image_len);
188 if (images.os.image_len == 0) {
Marian Balakowicz6986a382008-03-12 10:01:05 +0100189 puts ("ERROR: can't get kernel image!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000190 return 1;
191 }
wdenk47d1a6e2002-11-03 00:01:44 +0000192
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100193 /* get image parameters */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100194 switch (genimg_get_format (os_hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100195 case IMAGE_FORMAT_LEGACY:
Kumar Gala396f6352008-08-15 08:24:41 -0500196 images.os.type = image_get_type (os_hdr);
197 images.os.comp = image_get_comp (os_hdr);
198 images.os.os = image_get_os (os_hdr);
Wolfgang Denkbccae902005-10-06 01:50:50 +0200199
Kumar Gala396f6352008-08-15 08:24:41 -0500200 images.os.end = image_get_image_end (os_hdr);
201 images.os.load = image_get_load (os_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100202 break;
203#if defined(CONFIG_FIT)
204 case IMAGE_FORMAT_FIT:
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100205 if (fit_image_get_type (images.fit_hdr_os,
Kumar Gala396f6352008-08-15 08:24:41 -0500206 images.fit_noffset_os, &images.os.type)) {
Marian Balakowicz6986a382008-03-12 10:01:05 +0100207 puts ("Can't get image type!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100208 show_boot_progress (-109);
wdenk47d1a6e2002-11-03 00:01:44 +0000209 return 1;
210 }
wdenk47d1a6e2002-11-03 00:01:44 +0000211
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100212 if (fit_image_get_comp (images.fit_hdr_os,
Kumar Gala396f6352008-08-15 08:24:41 -0500213 images.fit_noffset_os, &images.os.comp)) {
Marian Balakowicz6986a382008-03-12 10:01:05 +0100214 puts ("Can't get image compression!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100215 show_boot_progress (-110);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100216 return 1;
217 }
wdenk47d1a6e2002-11-03 00:01:44 +0000218
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100219 if (fit_image_get_os (images.fit_hdr_os,
Kumar Gala396f6352008-08-15 08:24:41 -0500220 images.fit_noffset_os, &images.os.os)) {
Marian Balakowicz6986a382008-03-12 10:01:05 +0100221 puts ("Can't get image OS!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100222 show_boot_progress (-111);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100223 return 1;
224 }
wdenk47d1a6e2002-11-03 00:01:44 +0000225
Kumar Gala396f6352008-08-15 08:24:41 -0500226 images.os.end = fit_get_end (images.fit_hdr_os);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100227
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100228 if (fit_image_get_load (images.fit_hdr_os, images.fit_noffset_os,
Kumar Gala396f6352008-08-15 08:24:41 -0500229 &images.os.load)) {
Marian Balakowicz6986a382008-03-12 10:01:05 +0100230 puts ("Can't get image load address!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100231 show_boot_progress (-112);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100232 return 1;
wdenkb13fb012003-10-30 21:49:38 +0000233 }
234 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100235#endif
236 default:
237 puts ("ERROR: unknown image format type!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000238 return 1;
239 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100240
Kumar Galac160a952008-08-15 08:24:36 -0500241 /* find kernel entry point */
242 if (images.legacy_hdr_valid) {
243 images.ep = image_get_ep (&images.legacy_hdr_os_copy);
244#if defined(CONFIG_FIT)
245 } else if (images.fit_uname_os) {
246 ret = fit_image_get_entry (images.fit_hdr_os,
247 images.fit_noffset_os, &images.ep);
248 if (ret) {
249 puts ("Can't get entry point property!\n");
250 return 1;
251 }
252#endif
253 } else {
254 puts ("Could not find kernel entry point!\n");
255 return 1;
256 }
257
Kumar Gala396f6352008-08-15 08:24:41 -0500258 if (images.os.os == IH_OS_LINUX) {
Kumar Galac4f94192008-08-15 08:24:37 -0500259 /* find ramdisk */
260 ret = boot_get_ramdisk (argc, argv, &images, IH_INITRD_ARCH,
261 &images.rd_start, &images.rd_end);
262 if (ret) {
Kumar Galaea86b9e2008-08-29 19:08:29 -0500263 puts ("Ramdisk image is corrupt or invalid\n");
Kumar Galac4f94192008-08-15 08:24:37 -0500264 return 1;
265 }
Kumar Gala06a09912008-08-15 08:24:38 -0500266
267#if defined(CONFIG_OF_LIBFDT)
Jean-Christophe PLAGNIOL-VILLARD1d9af0b2008-09-09 22:18:23 +0200268#if defined(CONFIG_PPC) || defined(CONFIG_M68K) || defined(CONFIG_SPARC)
Kumar Gala06a09912008-08-15 08:24:38 -0500269 /* find flattened device tree */
270 ret = boot_get_fdt (flag, argc, argv, &images,
271 &images.ft_addr, &images.ft_len);
272 if (ret) {
273 puts ("Could not find a valid device tree\n");
274 return 1;
275 }
Kumar Gala54f9c862008-08-15 08:24:39 -0500276
277 set_working_fdt_addr(images.ft_addr);
Kumar Gala06a09912008-08-15 08:24:38 -0500278#endif
Jean-Christophe PLAGNIOL-VILLARD1d9af0b2008-09-09 22:18:23 +0200279#endif
Kumar Galac4f94192008-08-15 08:24:37 -0500280 }
281
Kumar Gala396f6352008-08-15 08:24:41 -0500282 images.os.start = (ulong)os_hdr;
283 images.valid = 1;
284
285 return 0;
286}
287
288#define BOOTM_ERR_RESET -1
289#define BOOTM_ERR_OVERLAP -2
290#define BOOTM_ERR_UNIMPLEMENTED -3
291static int bootm_load_os(image_info_t os, ulong *load_end, int boot_progress)
292{
293 uint8_t comp = os.comp;
294 ulong load = os.load;
295 ulong blob_start = os.start;
296 ulong blob_end = os.end;
297 ulong image_start = os.image_start;
298 ulong image_len = os.image_len;
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200299 uint unc_len = CONFIG_SYS_BOOTM_LEN;
Kumar Gala396f6352008-08-15 08:24:41 -0500300
301 const char *type_name = genimg_get_type_name (os.type);
302
303 switch (comp) {
304 case IH_COMP_NONE:
305 if (load == blob_start) {
306 printf (" XIP %s ... ", type_name);
307 } else {
308 printf (" Loading %s ... ", type_name);
309
310 memmove_wd ((void *)load,
311 (void *)image_start, image_len, CHUNKSZ);
312 }
313 *load_end = load + image_len;
314 puts("OK\n");
315 break;
316 case IH_COMP_GZIP:
317 printf (" Uncompressing %s ... ", type_name);
318 if (gunzip ((void *)load, unc_len,
319 (uchar *)image_start, &image_len) != 0) {
320 puts ("GUNZIP: uncompress or overwrite error "
321 "- must RESET board to recover\n");
322 if (boot_progress)
323 show_boot_progress (-6);
324 return BOOTM_ERR_RESET;
325 }
326
327 *load_end = load + image_len;
328 break;
329#ifdef CONFIG_BZIP2
330 case IH_COMP_BZIP2:
331 printf (" Uncompressing %s ... ", type_name);
332 /*
333 * If we've got less than 4 MB of malloc() space,
334 * use slower decompression algorithm which requires
335 * at most 2300 KB of memory.
336 */
337 int i = BZ2_bzBuffToBuffDecompress ((char*)load,
338 &unc_len, (char *)image_start, image_len,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200339 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
Kumar Gala396f6352008-08-15 08:24:41 -0500340 if (i != BZ_OK) {
341 printf ("BUNZIP2: uncompress or overwrite error %d "
342 "- must RESET board to recover\n", i);
343 if (boot_progress)
344 show_boot_progress (-6);
345 return BOOTM_ERR_RESET;
346 }
347
348 *load_end = load + unc_len;
349 break;
350#endif /* CONFIG_BZIP2 */
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200351#ifdef CONFIG_LZMA
352 case IH_COMP_LZMA:
353 printf (" Uncompressing %s ... ", type_name);
354
355 int ret = lzmaBuffToBuffDecompress(
356 (unsigned char *)load, &unc_len,
Luigi 'Comio' Mantellinid977a572008-09-13 10:04:32 +0200357 (unsigned char *)image_start, image_len);
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200358 if (ret != LZMA_RESULT_OK) {
359 printf ("LZMA: uncompress or overwrite error %d "
360 "- must RESET board to recover\n", ret);
361 show_boot_progress (-6);
362 return BOOTM_ERR_RESET;
363 }
364 *load_end = load + unc_len;
365 break;
366#endif /* CONFIG_LZMA */
Kumar Gala396f6352008-08-15 08:24:41 -0500367 default:
368 printf ("Unimplemented compression type %d\n", comp);
369 return BOOTM_ERR_UNIMPLEMENTED;
370 }
371 puts ("OK\n");
Jean-Christophe PLAGNIOL-VILLARD54b4ab32008-09-09 22:18:24 +0200372 debug (" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
Kumar Gala396f6352008-08-15 08:24:41 -0500373 if (boot_progress)
374 show_boot_progress (7);
375
376 if ((load < blob_end) && (*load_end > blob_start)) {
377 debug ("images.os.start = 0x%lX, images.os.end = 0x%lx\n", blob_start, blob_end);
Jean-Christophe PLAGNIOL-VILLARD54b4ab32008-09-09 22:18:24 +0200378 debug ("images.os.load = 0x%lx, load_end = 0x%lx\n", load, *load_end);
Kumar Gala396f6352008-08-15 08:24:41 -0500379
380 return BOOTM_ERR_OVERLAP;
381 }
382
383 return 0;
384}
385
386/*******************************************************************/
387/* bootm - boot application image from image in memory */
388/*******************************************************************/
389int do_bootm (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
390{
391
392 ulong iflag;
393 ulong load_end = 0;
394 int ret;
395
Anatolij Gustschin8e024942008-08-29 21:04:45 +0200396 if (bootm_start(cmdtp, flag, argc, argv))
397 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000398
399 /*
400 * We have reached the point of no return: we are going to
401 * overwrite all exception vector code, so we cannot easily
402 * recover from any failures any more...
403 */
wdenk47d1a6e2002-11-03 00:01:44 +0000404 iflag = disable_interrupts();
405
Stefan Roeseebb86c42008-07-30 09:59:51 +0200406#if defined(CONFIG_CMD_USB)
Wolfgang Denk699f0512008-07-15 22:22:44 +0200407 /*
408 * turn off USB to prevent the host controller from writing to the
409 * SDRAM while Linux is booting. This could happen (at least for OHCI
410 * controller), because the HCCA (Host Controller Communication Area)
411 * lies within the SDRAM and the host controller writes continously to
412 * this area (as busmaster!). The HccaFrameNumber is for example
413 * updated every 1 ms within the HCCA structure in SDRAM! For more
414 * details see the OpenHCI specification.
415 */
Markus Klotzbücher3d71c812008-07-10 14:47:09 +0200416 usb_stop();
417#endif
418
wdenkc7de8292002-11-19 11:04:11 +0000419#ifdef CONFIG_AMIGAONEG3SE
420 /*
wdenk8bde7f72003-06-27 21:31:46 +0000421 * We've possible left the caches enabled during
wdenkc7de8292002-11-19 11:04:11 +0000422 * bios emulation, so turn them off again
423 */
424 icache_disable();
wdenkc7de8292002-11-19 11:04:11 +0000425 dcache_disable();
426#endif
427
Kumar Gala396f6352008-08-15 08:24:41 -0500428 ret = bootm_load_os(images.os, &load_end, 1);
wdenk47d1a6e2002-11-03 00:01:44 +0000429
Kumar Gala396f6352008-08-15 08:24:41 -0500430 if (ret < 0) {
431 if (ret == BOOTM_ERR_RESET)
wdenk47d1a6e2002-11-03 00:01:44 +0000432 do_reset (cmdtp, flag, argc, argv);
Kumar Gala396f6352008-08-15 08:24:41 -0500433 if (ret == BOOTM_ERR_OVERLAP) {
434 if (images.legacy_hdr_valid) {
435 if (image_get_type (&images.legacy_hdr_os_copy) == IH_TYPE_MULTI)
436 puts ("WARNING: legacy format multi component "
437 "image overwritten\n");
438 } else {
439 puts ("ERROR: new format image overwritten - "
440 "must RESET the board to recover\n");
441 show_boot_progress (-113);
442 do_reset (cmdtp, flag, argc, argv);
443 }
wdenk47d1a6e2002-11-03 00:01:44 +0000444 }
Kumar Gala396f6352008-08-15 08:24:41 -0500445 if (ret == BOOTM_ERR_UNIMPLEMENTED) {
446 if (iflag)
447 enable_interrupts();
448 show_boot_progress (-7);
449 return 1;
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200450 }
wdenk47d1a6e2002-11-03 00:01:44 +0000451 }
Marian Balakowicz75824382008-01-31 13:20:06 +0100452
Kumar Gala396f6352008-08-15 08:24:41 -0500453 lmb_reserve(&images.lmb, images.os.load, (load_end - images.os.load));
454
Heiko Schocherfad63402007-07-13 09:54:17 +0200455 show_boot_progress (8);
wdenk47d1a6e2002-11-03 00:01:44 +0000456
Kumar Gala396f6352008-08-15 08:24:41 -0500457 switch (images.os.os) {
wdenk47d1a6e2002-11-03 00:01:44 +0000458 default: /* handled by (original) Linux case */
459 case IH_OS_LINUX:
wdenkf72da342003-10-10 10:05:42 +0000460#ifdef CONFIG_SILENT_CONSOLE
461 fixup_silent_linux();
462#endif
Kumar Gala40d7e992008-08-15 08:24:45 -0500463 do_bootm_linux (0, argc, argv, &images);
wdenk47d1a6e2002-11-03 00:01:44 +0000464 break;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100465
wdenk47d1a6e2002-11-03 00:01:44 +0000466 case IH_OS_NETBSD:
Kumar Gala40d7e992008-08-15 08:24:45 -0500467 do_bootm_netbsd (0, argc, argv, &images);
wdenk47d1a6e2002-11-03 00:01:44 +0000468 break;
wdenk8bde7f72003-06-27 21:31:46 +0000469
wdenk1f4bb372003-07-27 00:21:01 +0000470#ifdef CONFIG_LYNXKDI
471 case IH_OS_LYNXOS:
Kumar Gala40d7e992008-08-15 08:24:45 -0500472 do_bootm_lynxkdi (0, argc, argv, &images);
wdenk1f4bb372003-07-27 00:21:01 +0000473 break;
474#endif
475
wdenkd791b1d2003-04-20 14:04:18 +0000476 case IH_OS_RTEMS:
Kumar Gala40d7e992008-08-15 08:24:45 -0500477 do_bootm_rtems (0, argc, argv, &images);
wdenkd791b1d2003-04-20 14:04:18 +0000478 break;
wdenk8bde7f72003-06-27 21:31:46 +0000479
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500480#if defined(CONFIG_CMD_ELF)
wdenk47d1a6e2002-11-03 00:01:44 +0000481 case IH_OS_VXWORKS:
Kumar Gala40d7e992008-08-15 08:24:45 -0500482 do_bootm_vxworks (0, argc, argv, &images);
wdenk47d1a6e2002-11-03 00:01:44 +0000483 break;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100484
wdenk47d1a6e2002-11-03 00:01:44 +0000485 case IH_OS_QNX:
Kumar Gala40d7e992008-08-15 08:24:45 -0500486 do_bootm_qnxelf (0, argc, argv, &images);
wdenk47d1a6e2002-11-03 00:01:44 +0000487 break;
Jon Loeliger90253172007-07-10 11:02:44 -0500488#endif
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100489
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500490#ifdef CONFIG_INTEGRITY
491 case IH_OS_INTEGRITY:
492 do_bootm_integrity (0, argc, argv, &images);
493 break;
494#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000495 }
496
Heiko Schocherfad63402007-07-13 09:54:17 +0200497 show_boot_progress (-9);
wdenk47d1a6e2002-11-03 00:01:44 +0000498#ifdef DEBUG
wdenk4b9206e2004-03-23 22:14:11 +0000499 puts ("\n## Control returned to monitor - resetting...\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000500#endif
Kumar Gala40d7e992008-08-15 08:24:45 -0500501 do_reset (cmdtp, flag, argc, argv);
Marian Balakowicza44a2692008-03-12 10:14:57 +0100502
wdenk47d1a6e2002-11-03 00:01:44 +0000503 return 1;
504}
505
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100506/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100507 * image_get_kernel - verify legacy format kernel image
508 * @img_addr: in RAM address of the legacy format image to be verified
509 * @verify: data CRC verification flag
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100510 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100511 * image_get_kernel() verifies legacy image integrity and returns pointer to
512 * legacy image header if image verification was completed successfully.
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100513 *
514 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100515 * pointer to a legacy image header if valid image was found
516 * otherwise return NULL
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100517 */
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100518static image_header_t *image_get_kernel (ulong img_addr, int verify)
519{
520 image_header_t *hdr = (image_header_t *)img_addr;
521
522 if (!image_check_magic(hdr)) {
523 puts ("Bad Magic Number\n");
524 show_boot_progress (-1);
525 return NULL;
526 }
527 show_boot_progress (2);
528
529 if (!image_check_hcrc (hdr)) {
530 puts ("Bad Header Checksum\n");
531 show_boot_progress (-2);
532 return NULL;
533 }
534
535 show_boot_progress (3);
536 image_print_contents (hdr);
537
538 if (verify) {
539 puts (" Verifying Checksum ... ");
540 if (!image_check_dcrc (hdr)) {
541 printf ("Bad Data CRC\n");
542 show_boot_progress (-3);
543 return NULL;
544 }
545 puts ("OK\n");
546 }
547 show_boot_progress (4);
548
549 if (!image_check_target_arch (hdr)) {
550 printf ("Unsupported Architecture 0x%x\n", image_get_arch (hdr));
551 show_boot_progress (-4);
552 return NULL;
553 }
554 return hdr;
555}
556
557/**
Marian Balakowicz6986a382008-03-12 10:01:05 +0100558 * fit_check_kernel - verify FIT format kernel subimage
559 * @fit_hdr: pointer to the FIT image header
560 * os_noffset: kernel subimage node offset within FIT image
561 * @verify: data CRC verification flag
562 *
563 * fit_check_kernel() verifies integrity of the kernel subimage and from
564 * specified FIT image.
565 *
566 * returns:
567 * 1, on success
568 * 0, on failure
569 */
570#if defined (CONFIG_FIT)
571static int fit_check_kernel (const void *fit, int os_noffset, int verify)
572{
573 fit_image_print (fit, os_noffset, " ");
574
575 if (verify) {
576 puts (" Verifying Hash Integrity ... ");
577 if (!fit_image_check_hashes (fit, os_noffset)) {
578 puts ("Bad Data Hash\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100579 show_boot_progress (-104);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100580 return 0;
581 }
582 puts ("OK\n");
583 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100584 show_boot_progress (105);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100585
586 if (!fit_image_check_target_arch (fit, os_noffset)) {
587 puts ("Unsupported Architecture\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100588 show_boot_progress (-105);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100589 return 0;
590 }
591
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100592 show_boot_progress (106);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100593 if (!fit_image_check_type (fit, os_noffset, IH_TYPE_KERNEL)) {
594 puts ("Not a kernel image\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100595 show_boot_progress (-106);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100596 return 0;
597 }
598
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100599 show_boot_progress (107);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100600 return 1;
601}
602#endif /* CONFIG_FIT */
603
604/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100605 * boot_get_kernel - find kernel image
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100606 * @os_data: pointer to a ulong variable, will hold os data start address
607 * @os_len: pointer to a ulong variable, will hold os data length
608 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100609 * boot_get_kernel() tries to find a kernel image, verifies its integrity
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100610 * and locates kernel data.
611 *
612 * returns:
613 * pointer to image header if valid image was found, plus kernel start
614 * address and length, otherwise NULL
615 */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100616static void *boot_get_kernel (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100617 bootm_headers_t *images, ulong *os_data, ulong *os_len)
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100618{
619 image_header_t *hdr;
620 ulong img_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100621#if defined(CONFIG_FIT)
622 void *fit_hdr;
623 const char *fit_uname_config = NULL;
624 const char *fit_uname_kernel = NULL;
Marian Balakowicz6986a382008-03-12 10:01:05 +0100625 const void *data;
626 size_t len;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100627 int cfg_noffset;
Marian Balakowicz6986a382008-03-12 10:01:05 +0100628 int os_noffset;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100629#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100630
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100631 /* find out kernel image address */
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100632 if (argc < 2) {
633 img_addr = load_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100634 debug ("* kernel: default image load address = 0x%08lx\n",
635 load_addr);
636#if defined(CONFIG_FIT)
637 } else if (fit_parse_conf (argv[1], load_addr, &img_addr,
638 &fit_uname_config)) {
639 debug ("* kernel: config '%s' from image at 0x%08lx\n",
640 fit_uname_config, img_addr);
641 } else if (fit_parse_subimage (argv[1], load_addr, &img_addr,
642 &fit_uname_kernel)) {
643 debug ("* kernel: subimage '%s' from image at 0x%08lx\n",
644 fit_uname_kernel, img_addr);
645#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100646 } else {
647 img_addr = simple_strtoul(argv[1], NULL, 16);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100648 debug ("* kernel: cmdline image address = 0x%08lx\n", img_addr);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100649 }
650
651 show_boot_progress (1);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100652
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100653 /* copy from dataflash if needed */
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100654 img_addr = genimg_get_image (img_addr);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100655
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100656 /* check image type, for FIT images get FIT kernel node */
Marian Balakowicz6986a382008-03-12 10:01:05 +0100657 *os_data = *os_len = 0;
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100658 switch (genimg_get_format ((void *)img_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100659 case IMAGE_FORMAT_LEGACY:
Marian Balakowicz6986a382008-03-12 10:01:05 +0100660 printf ("## Booting kernel from Legacy Image at %08lx ...\n",
661 img_addr);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100662 hdr = image_get_kernel (img_addr, images->verify);
663 if (!hdr)
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100664 return NULL;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100665 show_boot_progress (5);
666
Marian Balakowicz6986a382008-03-12 10:01:05 +0100667 /* get os_data and os_len */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100668 switch (image_get_type (hdr)) {
669 case IH_TYPE_KERNEL:
670 *os_data = image_get_data (hdr);
671 *os_len = image_get_data_size (hdr);
672 break;
673 case IH_TYPE_MULTI:
674 image_multi_getimg (hdr, 0, os_data, os_len);
675 break;
676 default:
677 printf ("Wrong Image Type for %s command\n", cmdtp->name);
678 show_boot_progress (-5);
679 return NULL;
680 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100681
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200682 /*
683 * copy image header to allow for image overwrites during kernel
684 * decompression.
685 */
686 memmove (&images->legacy_hdr_os_copy, hdr, sizeof(image_header_t));
687
688 /* save pointer to image header */
689 images->legacy_hdr_os = hdr;
690
691 images->legacy_hdr_valid = 1;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100692 show_boot_progress (6);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100693 break;
694#if defined(CONFIG_FIT)
695 case IMAGE_FORMAT_FIT:
696 fit_hdr = (void *)img_addr;
Marian Balakowicz6986a382008-03-12 10:01:05 +0100697 printf ("## Booting kernel from FIT Image at %08lx ...\n",
698 img_addr);
699
700 if (!fit_check_format (fit_hdr)) {
701 puts ("Bad FIT kernel image format!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100702 show_boot_progress (-100);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100703 return NULL;
704 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100705 show_boot_progress (100);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100706
707 if (!fit_uname_kernel) {
708 /*
709 * no kernel image node unit name, try to get config
710 * node first. If config unit node name is NULL
711 * fit_conf_get_node() will try to find default config node
712 */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100713 show_boot_progress (101);
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100714 cfg_noffset = fit_conf_get_node (fit_hdr, fit_uname_config);
715 if (cfg_noffset < 0) {
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100716 show_boot_progress (-101);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100717 return NULL;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100718 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100719 /* save configuration uname provided in the first
720 * bootm argument
721 */
722 images->fit_uname_cfg = fdt_get_name (fit_hdr, cfg_noffset, NULL);
723 printf (" Using '%s' configuration\n", images->fit_uname_cfg);
724 show_boot_progress (103);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100725
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100726 os_noffset = fit_conf_get_kernel_node (fit_hdr, cfg_noffset);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100727 fit_uname_kernel = fit_get_name (fit_hdr, os_noffset, NULL);
728 } else {
729 /* get kernel component image node offset */
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100730 show_boot_progress (102);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100731 os_noffset = fit_image_get_node (fit_hdr, fit_uname_kernel);
732 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100733 if (os_noffset < 0) {
734 show_boot_progress (-103);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100735 return NULL;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100736 }
Marian Balakowicz6986a382008-03-12 10:01:05 +0100737
738 printf (" Trying '%s' kernel subimage\n", fit_uname_kernel);
739
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100740 show_boot_progress (104);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100741 if (!fit_check_kernel (fit_hdr, os_noffset, images->verify))
742 return NULL;
743
744 /* get kernel image data address and length */
745 if (fit_image_get_data (fit_hdr, os_noffset, &data, &len)) {
746 puts ("Could not find kernel subimage data!\n");
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100747 show_boot_progress (-107);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100748 return NULL;
749 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100750 show_boot_progress (108);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100751
752 *os_len = len;
753 *os_data = (ulong)data;
754 images->fit_hdr_os = fit_hdr;
755 images->fit_uname_os = fit_uname_kernel;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100756 images->fit_noffset_os = os_noffset;
Marian Balakowicz6986a382008-03-12 10:01:05 +0100757 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100758#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100759 default:
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100760 printf ("Wrong Image Format for %s command\n", cmdtp->name);
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100761 show_boot_progress (-108);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100762 return NULL;
763 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100764
Wolfgang Denk06c53be2008-07-10 13:16:09 +0200765 debug (" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
Marian Balakowicz6986a382008-03-12 10:01:05 +0100766 *os_data, *os_len, *os_len);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100767
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100768 return (void *)img_addr;
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100769}
770
wdenk0d498392003-07-01 21:06:45 +0000771U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200772 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100773 "bootm - boot application image from memory\n",
774 "[addr [arg ...]]\n - boot application image stored in memory\n"
775 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
776 "\t'arg' can be the address of an initrd image\n"
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100777#if defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500778 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
Detlev Zundel5441f612007-10-19 16:47:26 +0200779 "\ta third argument is required which is the address of the\n"
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500780 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
781 "\tuse a '-' for the second argument. If you do not pass a third\n"
782 "\ta bd_info struct will be passed instead\n"
783#endif
Marian Balakowicz6986a382008-03-12 10:01:05 +0100784#if defined(CONFIG_FIT)
785 "\t\nFor the new multi component uImage format (FIT) addresses\n"
786 "\tmust be extened to include component or configuration unit name:\n"
787 "\taddr:<subimg_uname> - direct component image specification\n"
788 "\taddr#<conf_uname> - configuration specification\n"
789 "\tUse iminfo command to get the list of existing component\n"
790 "\timages and configurations.\n"
791#endif
wdenk8bde7f72003-06-27 21:31:46 +0000792);
793
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100794/*******************************************************************/
795/* bootd - boot default image */
796/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500797#if defined(CONFIG_CMD_BOOTD)
wdenk47d1a6e2002-11-03 00:01:44 +0000798int do_bootd (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
799{
800 int rcode = 0;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100801
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200802#ifndef CONFIG_SYS_HUSH_PARSER
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100803 if (run_command (getenv ("bootcmd"), flag) < 0)
804 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000805#else
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100806 if (parse_string_outer (getenv ("bootcmd"),
807 FLAG_PARSE_SEMICOLON | FLAG_EXIT_FROM_LOOP) != 0)
808 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000809#endif
810 return rcode;
811}
wdenk8bde7f72003-06-27 21:31:46 +0000812
wdenk0d498392003-07-01 21:06:45 +0000813U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100814 boot, 1, 1, do_bootd,
815 "boot - boot default, i.e., run 'bootcmd'\n",
wdenk9d2b18a2003-06-28 23:11:04 +0000816 NULL
817);
818
819/* keep old command name "bootd" for backward compatibility */
wdenk0d498392003-07-01 21:06:45 +0000820U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100821 bootd, 1, 1, do_bootd,
822 "bootd - boot default, i.e., run 'bootcmd'\n",
wdenk8bde7f72003-06-27 21:31:46 +0000823 NULL
824);
825
wdenk47d1a6e2002-11-03 00:01:44 +0000826#endif
827
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100828
829/*******************************************************************/
830/* iminfo - print header info for a requested image */
831/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500832#if defined(CONFIG_CMD_IMI)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100833int do_iminfo (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000834{
835 int arg;
836 ulong addr;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100837 int rcode = 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000838
839 if (argc < 2) {
840 return image_info (load_addr);
841 }
842
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100843 for (arg = 1; arg < argc; ++arg) {
844 addr = simple_strtoul (argv[arg], NULL, 16);
845 if (image_info (addr) != 0)
846 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000847 }
848 return rcode;
849}
850
851static int image_info (ulong addr)
852{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100853 void *hdr = (void *)addr;
wdenk47d1a6e2002-11-03 00:01:44 +0000854
855 printf ("\n## Checking Image at %08lx ...\n", addr);
856
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100857 switch (genimg_get_format (hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100858 case IMAGE_FORMAT_LEGACY:
859 puts (" Legacy image found\n");
860 if (!image_check_magic (hdr)) {
861 puts (" Bad Magic Number\n");
862 return 1;
863 }
864
865 if (!image_check_hcrc (hdr)) {
866 puts (" Bad Header Checksum\n");
867 return 1;
868 }
869
870 image_print_contents (hdr);
871
872 puts (" Verifying Checksum ... ");
873 if (!image_check_dcrc (hdr)) {
874 puts (" Bad Data CRC\n");
875 return 1;
876 }
877 puts ("OK\n");
878 return 0;
879#if defined(CONFIG_FIT)
880 case IMAGE_FORMAT_FIT:
881 puts (" FIT image found\n");
Marian Balakowicze32fea62008-03-11 12:35:20 +0100882
883 if (!fit_check_format (hdr)) {
884 puts ("Bad FIT image format!\n");
885 return 1;
886 }
887
888 fit_print_contents (hdr);
Bartlomiej Siekaa4f24342008-09-09 12:58:16 +0200889
890 if (!fit_all_image_check_hashes (hdr)) {
891 puts ("Bad hash in FIT image!\n");
892 return 1;
893 }
894
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100895 return 0;
896#endif
897 default:
898 puts ("Unknown image format!\n");
899 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000900 }
901
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100902 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000903}
wdenk0d498392003-07-01 21:06:45 +0000904
905U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200906 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
wdenk8bde7f72003-06-27 21:31:46 +0000907 "iminfo - print header information for application image\n",
908 "addr [addr ...]\n"
909 " - print header information for application image starting at\n"
910 " address 'addr' in memory; this includes verification of the\n"
911 " image contents (magic number, header and payload checksums)\n"
912);
Jon Loeliger90253172007-07-10 11:02:44 -0500913#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000914
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100915
916/*******************************************************************/
917/* imls - list all images found in flash */
918/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500919#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +0000920int do_imls (cmd_tbl_t *cmdtp, int flag, int argc, char *argv[])
921{
922 flash_info_t *info;
923 int i, j;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100924 void *hdr;
wdenk27b207f2003-07-24 23:38:38 +0000925
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100926 for (i = 0, info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200927 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100928
wdenk27b207f2003-07-24 23:38:38 +0000929 if (info->flash_id == FLASH_UNKNOWN)
930 goto next_bank;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100931 for (j = 0; j < info->sector_count; ++j) {
wdenk27b207f2003-07-24 23:38:38 +0000932
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100933 hdr = (void *)info->start[j];
934 if (!hdr)
wdenk27b207f2003-07-24 23:38:38 +0000935 goto next_sector;
936
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100937 switch (genimg_get_format (hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100938 case IMAGE_FORMAT_LEGACY:
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100939 if (!image_check_hcrc (hdr))
940 goto next_sector;
941
942 printf ("Legacy Image at %08lX:\n", (ulong)hdr);
943 image_print_contents (hdr);
944
945 puts (" Verifying Checksum ... ");
946 if (!image_check_dcrc (hdr)) {
947 puts ("Bad Data CRC\n");
948 } else {
949 puts ("OK\n");
950 }
951 break;
952#if defined(CONFIG_FIT)
953 case IMAGE_FORMAT_FIT:
Marian Balakowicze32fea62008-03-11 12:35:20 +0100954 if (!fit_check_format (hdr))
955 goto next_sector;
956
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100957 printf ("FIT Image at %08lX:\n", (ulong)hdr);
Marian Balakowicze32fea62008-03-11 12:35:20 +0100958 fit_print_contents (hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100959 break;
960#endif
961 default:
wdenk27b207f2003-07-24 23:38:38 +0000962 goto next_sector;
wdenk5bb226e2003-11-17 21:14:37 +0000963 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100964
wdenkbdccc4f2003-08-05 17:43:17 +0000965next_sector: ;
wdenk27b207f2003-07-24 23:38:38 +0000966 }
wdenkbdccc4f2003-08-05 17:43:17 +0000967next_bank: ;
wdenk27b207f2003-07-24 23:38:38 +0000968 }
969
970 return (0);
971}
972
973U_BOOT_CMD(
974 imls, 1, 1, do_imls,
975 "imls - list all images found in flash\n",
976 "\n"
977 " - Prints information about all images found at sector\n"
978 " boundaries in flash.\n"
979);
Jon Loeliger90253172007-07-10 11:02:44 -0500980#endif
wdenk27b207f2003-07-24 23:38:38 +0000981
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100982/*******************************************************************/
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100983/* helper routines */
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100984/*******************************************************************/
wdenk47d1a6e2002-11-03 00:01:44 +0000985#ifdef CONFIG_SILENT_CONSOLE
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100986static void fixup_silent_linux ()
wdenk47d1a6e2002-11-03 00:01:44 +0000987{
988 char buf[256], *start, *end;
989 char *cmdline = getenv ("bootargs");
990
991 /* Only fix cmdline when requested */
992 if (!(gd->flags & GD_FLG_SILENT))
993 return;
994
995 debug ("before silent fix-up: %s\n", cmdline);
996 if (cmdline) {
997 if ((start = strstr (cmdline, "console=")) != NULL) {
998 end = strchr (start, ' ');
999 strncpy (buf, cmdline, (start - cmdline + 8));
1000 if (end)
1001 strcpy (buf + (start - cmdline + 8), end);
1002 else
1003 buf[start - cmdline + 8] = '\0';
1004 } else {
1005 strcpy (buf, cmdline);
1006 strcat (buf, " console=");
1007 }
1008 } else {
1009 strcpy (buf, "console=");
1010 }
1011
1012 setenv ("bootargs", buf);
1013 debug ("after silent fix-up: %s\n", buf);
1014}
1015#endif /* CONFIG_SILENT_CONSOLE */
1016
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001017
1018/*******************************************************************/
1019/* OS booting routines */
1020/*******************************************************************/
1021
Kumar Gala40d7e992008-08-15 08:24:45 -05001022static int do_bootm_netbsd (int flag, int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001023 bootm_headers_t *images)
wdenk47d1a6e2002-11-03 00:01:44 +00001024{
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001025 void (*loader)(bd_t *, image_header_t *, char *, char *);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001026 image_header_t *os_hdr, *hdr;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +01001027 ulong kernel_data, kernel_len;
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001028 char *consdev;
1029 char *cmdline;
wdenk47d1a6e2002-11-03 00:01:44 +00001030
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001031#if defined(CONFIG_FIT)
1032 if (!images->legacy_hdr_valid) {
1033 fit_unsupported_reset ("NetBSD");
Kumar Gala40d7e992008-08-15 08:24:45 -05001034 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001035 }
1036#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001037 hdr = images->legacy_hdr_os;
wdenk47d1a6e2002-11-03 00:01:44 +00001038
1039 /*
1040 * Booting a (NetBSD) kernel image
1041 *
1042 * This process is pretty similar to a standalone application:
1043 * The (first part of an multi-) image must be a stage-2 loader,
1044 * which in turn is responsible for loading & invoking the actual
1045 * kernel. The only differences are the parameters being passed:
1046 * besides the board info strucure, the loader expects a command
1047 * line, the name of the console device, and (optionally) the
1048 * address of the original image header.
1049 */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001050 os_hdr = NULL;
Marian Balakowiczcb1c4892008-04-11 11:07:49 +02001051 if (image_check_type (&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
Marian Balakowiczf13e7b22008-01-08 18:12:17 +01001052 image_multi_getimg (hdr, 1, &kernel_data, &kernel_len);
1053 if (kernel_len)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001054 os_hdr = hdr;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +01001055 }
wdenk47d1a6e2002-11-03 00:01:44 +00001056
1057 consdev = "";
1058#if defined (CONFIG_8xx_CONS_SMC1)
1059 consdev = "smc1";
1060#elif defined (CONFIG_8xx_CONS_SMC2)
1061 consdev = "smc2";
1062#elif defined (CONFIG_8xx_CONS_SCC2)
1063 consdev = "scc2";
1064#elif defined (CONFIG_8xx_CONS_SCC3)
1065 consdev = "scc3";
1066#endif
1067
1068 if (argc > 2) {
1069 ulong len;
1070 int i;
1071
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001072 for (i = 2, len = 0; i < argc; i += 1)
wdenk47d1a6e2002-11-03 00:01:44 +00001073 len += strlen (argv[i]) + 1;
1074 cmdline = malloc (len);
1075
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001076 for (i = 2, len = 0; i < argc; i += 1) {
wdenk47d1a6e2002-11-03 00:01:44 +00001077 if (i > 2)
1078 cmdline[len++] = ' ';
1079 strcpy (&cmdline[len], argv[i]);
1080 len += strlen (argv[i]);
1081 }
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001082 } else if ((cmdline = getenv ("bootargs")) == NULL) {
wdenk47d1a6e2002-11-03 00:01:44 +00001083 cmdline = "";
1084 }
1085
Kumar Galac160a952008-08-15 08:24:36 -05001086 loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
wdenk47d1a6e2002-11-03 00:01:44 +00001087
1088 printf ("## Transferring control to NetBSD stage-2 loader (at address %08lx) ...\n",
1089 (ulong)loader);
1090
1091 show_boot_progress (15);
1092
1093 /*
1094 * NetBSD Stage-2 Loader Parameters:
1095 * r3: ptr to board info data
1096 * r4: image address
1097 * r5: console device
1098 * r6: boot args string
1099 */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001100 (*loader) (gd->bd, os_hdr, consdev, cmdline);
Kumar Gala40d7e992008-08-15 08:24:45 -05001101
1102 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001103}
1104
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001105#ifdef CONFIG_LYNXKDI
Kumar Gala40d7e992008-08-15 08:24:45 -05001106static int do_bootm_lynxkdi (int flag, int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001107 bootm_headers_t *images)
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001108{
Marian Balakowiczcb1c4892008-04-11 11:07:49 +02001109 image_header_t *hdr = &images->legacy_hdr_os_copy;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001110
1111#if defined(CONFIG_FIT)
1112 if (!images->legacy_hdr_valid) {
1113 fit_unsupported_reset ("Lynx");
Kumar Gala40d7e992008-08-15 08:24:45 -05001114 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001115 }
1116#endif
1117
1118 lynxkdi_boot ((image_header_t *)hdr);
Kumar Gala40d7e992008-08-15 08:24:45 -05001119
1120 return 1;
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001121}
1122#endif /* CONFIG_LYNXKDI */
1123
Kumar Gala40d7e992008-08-15 08:24:45 -05001124static int do_bootm_rtems (int flag, int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001125 bootm_headers_t *images)
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001126{
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001127 void (*entry_point)(bd_t *);
wdenkd791b1d2003-04-20 14:04:18 +00001128
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001129#if defined(CONFIG_FIT)
1130 if (!images->legacy_hdr_valid) {
1131 fit_unsupported_reset ("RTEMS");
Kumar Gala40d7e992008-08-15 08:24:45 -05001132 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001133 }
1134#endif
1135
Kumar Galac160a952008-08-15 08:24:36 -05001136 entry_point = (void (*)(bd_t *))images->ep;
wdenkd791b1d2003-04-20 14:04:18 +00001137
1138 printf ("## Transferring control to RTEMS (at address %08lx) ...\n",
1139 (ulong)entry_point);
1140
Heiko Schocherfad63402007-07-13 09:54:17 +02001141 show_boot_progress (15);
wdenkd791b1d2003-04-20 14:04:18 +00001142
1143 /*
1144 * RTEMS Parameters:
1145 * r3: ptr to board info data
1146 */
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001147 (*entry_point)(gd->bd);
Kumar Gala40d7e992008-08-15 08:24:45 -05001148
1149 return 1;
wdenkd791b1d2003-04-20 14:04:18 +00001150}
1151
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001152#if defined(CONFIG_CMD_ELF)
Kumar Gala40d7e992008-08-15 08:24:45 -05001153static int do_bootm_vxworks (int flag, int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001154 bootm_headers_t *images)
wdenk47d1a6e2002-11-03 00:01:44 +00001155{
wdenk47d1a6e2002-11-03 00:01:44 +00001156 char str[80];
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001157
1158#if defined(CONFIG_FIT)
Marian Balakowiczcb1c4892008-04-11 11:07:49 +02001159 if (!images->legacy_hdr_valid) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001160 fit_unsupported_reset ("VxWorks");
Kumar Gala40d7e992008-08-15 08:24:45 -05001161 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001162 }
1163#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001164
Kumar Galac160a952008-08-15 08:24:36 -05001165 sprintf(str, "%lx", images->ep); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001166 setenv("loadaddr", str);
Kumar Gala40d7e992008-08-15 08:24:45 -05001167 do_bootvx(NULL, 0, 0, NULL);
1168
1169 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001170}
1171
Kumar Gala40d7e992008-08-15 08:24:45 -05001172static int do_bootm_qnxelf(int flag, int argc, char *argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001173 bootm_headers_t *images)
wdenk47d1a6e2002-11-03 00:01:44 +00001174{
wdenk47d1a6e2002-11-03 00:01:44 +00001175 char *local_args[2];
1176 char str[16];
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001177
1178#if defined(CONFIG_FIT)
1179 if (!images->legacy_hdr_valid) {
1180 fit_unsupported_reset ("QNX");
Kumar Gala40d7e992008-08-15 08:24:45 -05001181 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001182 }
1183#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001184
Kumar Galac160a952008-08-15 08:24:36 -05001185 sprintf(str, "%lx", images->ep); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001186 local_args[0] = argv[0];
1187 local_args[1] = str; /* and provide it via the arguments */
Kumar Gala40d7e992008-08-15 08:24:45 -05001188 do_bootelf(NULL, 0, 2, local_args);
1189
1190 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001191}
Jon Loeliger90253172007-07-10 11:02:44 -05001192#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -05001193
1194#ifdef CONFIG_INTEGRITY
1195static int do_bootm_integrity (int flag, int argc, char *argv[],
1196 bootm_headers_t *images)
1197{
1198 void (*entry_point)(void);
1199
1200#if defined(CONFIG_FIT)
1201 if (!images->legacy_hdr_valid) {
1202 fit_unsupported_reset ("INTEGRITY");
1203 return 1;
1204 }
1205#endif
1206
1207 entry_point = (void (*)(void))images->ep;
1208
1209 printf ("## Transferring control to INTEGRITY (at address %08lx) ...\n",
1210 (ulong)entry_point);
1211
1212 show_boot_progress (15);
1213
1214 /*
1215 * INTEGRITY Parameters:
1216 * None
1217 */
1218 (*entry_point)();
1219
1220 return 1;
1221}
1222#endif