blob: 56d53b15c05877a30912dc18267137728e3bf3a2 [file] [log] [blame]
wdenk47d1a6e2002-11-03 00:01:44 +00001/*
Detlev Zundelca95c9d2009-07-13 16:01:18 +02002 * (C) Copyright 2000-2009
wdenk47d1a6e2002-11-03 00:01:44 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk47d1a6e2002-11-03 00:01:44 +00006 */
7
Marian Balakowiczb97a2a02008-01-08 18:14:09 +01008
wdenk47d1a6e2002-11-03 00:01:44 +00009/*
10 * Boot support
11 */
12#include <common.h>
13#include <watchdog.h>
14#include <command.h>
wdenk47d1a6e2002-11-03 00:01:44 +000015#include <image.h>
16#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARDa31e0912009-04-04 12:49:11 +020017#include <u-boot/zlib.h>
wdenkc29fdfc2003-08-29 20:57:53 +000018#include <bzlib.h>
wdenk7f70e852003-05-20 14:25:27 +000019#include <environment.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060020#include <lmb.h>
Kumar Gala49c3a862008-10-21 17:25:45 -050021#include <linux/ctype.h>
wdenk47d1a6e2002-11-03 00:01:44 +000022#include <asm/byteorder.h>
Simon Glass35e7b0f2013-05-07 06:12:03 +000023#include <asm/io.h>
Anatolij Gustschin5bf27662011-11-19 13:12:18 +000024#include <linux/compiler.h>
wdenk8bde7f72003-06-27 21:31:46 +000025
Stefan Roeseebb86c42008-07-30 09:59:51 +020026#if defined(CONFIG_CMD_USB)
Markus Klotzbücher3d71c812008-07-10 14:47:09 +020027#include <usb.h>
28#endif
29
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020030#ifdef CONFIG_SYS_HUSH_PARSER
wdenk47d1a6e2002-11-03 00:01:44 +000031#include <hush.h>
32#endif
33
Kumar Gala54f9c862008-08-15 08:24:39 -050034#if defined(CONFIG_OF_LIBFDT)
Kumar Gala54f9c862008-08-15 08:24:39 -050035#include <libfdt.h>
36#include <fdt_support.h>
37#endif
38
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +020039#ifdef CONFIG_LZMA
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +020040#include <lzma/LzmaTypes.h>
Luigi 'Comio' Mantellinicaf72ff2009-07-21 10:45:49 +020041#include <lzma/LzmaDec.h>
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +020042#include <lzma/LzmaTools.h>
43#endif /* CONFIG_LZMA */
44
Peter Korsgaard20dde482009-11-19 11:37:51 +010045#ifdef CONFIG_LZO
46#include <linux/lzo.h>
47#endif /* CONFIG_LZO */
48
Marian Balakowicz1ee11802008-01-08 18:17:10 +010049DECLARE_GLOBAL_DATA_PTR;
50
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020051#ifndef CONFIG_SYS_BOOTM_LEN
52#define CONFIG_SYS_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
wdenk47d1a6e2002-11-03 00:01:44 +000053#endif
54
Marian Balakowicz321359f2008-01-08 18:11:43 +010055#ifdef CONFIG_BZIP2
56extern void bz_internal_error(int);
wdenk228f29a2002-12-08 09:53:23 +000057#endif
58
Jon Loeligerbaa26db2007-07-08 17:51:39 -050059#if defined(CONFIG_CMD_IMI)
Stephen Warren712fbcf2011-10-18 11:11:49 +000060static int image_info(unsigned long addr);
wdenk47d1a6e2002-11-03 00:01:44 +000061#endif
wdenk27b207f2003-07-24 23:38:38 +000062
Jon Loeligerbaa26db2007-07-08 17:51:39 -050063#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +000064#include <flash.h>
Stefan Roeseca5def32010-08-31 10:00:10 +020065#include <mtd/cfi_flash.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020066extern flash_info_t flash_info[]; /* info for FLASH chips */
Vipin Kumar8fdf1e02012-12-16 22:32:48 +000067#endif
68
69#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
Stephen Warren712fbcf2011-10-18 11:11:49 +000070static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
wdenk27b207f2003-07-24 23:38:38 +000071#endif
72
Vipin Kumar8fdf1e02012-12-16 22:32:48 +000073#include <linux/err.h>
74#include <nand.h>
75
Joe Hershbergerbe2e5a02013-02-27 10:20:59 +000076#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
Stephen Warren712fbcf2011-10-18 11:11:49 +000077static void fixup_silent_linux(void);
wdenk2262cfe2002-11-18 00:14:45 +000078#endif
79
Simon Glass35e7b0f2013-05-07 06:12:03 +000080static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
Stephen Warren712fbcf2011-10-18 11:11:49 +000081 char * const argv[], bootm_headers_t *images,
82 ulong *os_data, ulong *os_len);
wdenk47d1a6e2002-11-03 00:01:44 +000083
wdenk47d1a6e2002-11-03 00:01:44 +000084/*
85 * Continue booting an OS image; caller already has:
86 * - copied image header to global variable `header'
87 * - checked header magic number, checksums (both header & image),
88 * - verified image architecture (PPC) and type (KERNEL or MULTI),
89 * - loaded (first part of) image to header load address,
90 * - disabled interrupts.
Simon Glass983c72f2013-06-11 11:14:46 -070091 *
Simon Glass35fc84f2013-06-11 11:14:47 -070092 * @flag: Flags indicating what to do (BOOTM_STATE_...)
Simon Glass983c72f2013-06-11 11:14:46 -070093 * @argc: Number of arguments. Note that the arguments are shifted down
94 * so that 0 is the first argument not processed by U-Boot, and
95 * argc is adjusted accordingly. This avoids confusion as to how
96 * many arguments are available for the OS.
97 * @images: Pointers to os/initrd/fdt
98 * @return 1 on error. On success the OS boots so this function does
99 * not return.
wdenk47d1a6e2002-11-03 00:01:44 +0000100 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000101typedef int boot_os_fn(int flag, int argc, char * const argv[],
Simon Glass983c72f2013-06-11 11:14:46 -0700102 bootm_headers_t *images);
wdenk47d1a6e2002-11-03 00:01:44 +0000103
Kumar Galab1d0db12008-10-21 17:25:47 -0500104#ifdef CONFIG_BOOTM_LINUX
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100105extern boot_os_fn do_bootm_linux;
Kumar Galab1d0db12008-10-21 17:25:47 -0500106#endif
107#ifdef CONFIG_BOOTM_NETBSD
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100108static boot_os_fn do_bootm_netbsd;
Kumar Galab1d0db12008-10-21 17:25:47 -0500109#endif
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100110#if defined(CONFIG_LYNXKDI)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100111static boot_os_fn do_bootm_lynxkdi;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000112extern void lynxkdi_boot(image_header_t *);
wdenk8bde7f72003-06-27 21:31:46 +0000113#endif
Kumar Galab1d0db12008-10-21 17:25:47 -0500114#ifdef CONFIG_BOOTM_RTEMS
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100115static boot_os_fn do_bootm_rtems;
Kumar Galab1d0db12008-10-21 17:25:47 -0500116#endif
Torkel Lundgren3df61952010-09-28 11:05:36 +0200117#if defined(CONFIG_BOOTM_OSE)
118static boot_os_fn do_bootm_ose;
119#endif
Steven Stallion04d41402013-03-20 06:31:35 +0000120#if defined(CONFIG_BOOTM_PLAN9)
121static boot_os_fn do_bootm_plan9;
122#endif
Miao Yan17ab52f2013-11-28 17:51:36 +0800123#if defined(CONFIG_BOOTM_VXWORKS)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100124static boot_os_fn do_bootm_vxworks;
Miao Yan17ab52f2013-11-28 17:51:36 +0800125#endif
126#if defined(CONFIG_CMD_ELF)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100127static boot_os_fn do_bootm_qnxelf;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000128int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
129int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Jon Loeliger90253172007-07-10 11:02:44 -0500130#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -0500131#if defined(CONFIG_INTEGRITY)
132static boot_os_fn do_bootm_integrity;
133#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000134
Mike Frysinger00085552009-11-03 11:36:26 -0500135static boot_os_fn *boot_os[] = {
Kumar Galab1d0db12008-10-21 17:25:47 -0500136#ifdef CONFIG_BOOTM_LINUX
Kumar Galabe083152008-10-21 17:25:44 -0500137 [IH_OS_LINUX] = do_bootm_linux,
Kumar Galab1d0db12008-10-21 17:25:47 -0500138#endif
139#ifdef CONFIG_BOOTM_NETBSD
Kumar Galabe083152008-10-21 17:25:44 -0500140 [IH_OS_NETBSD] = do_bootm_netbsd,
Kumar Galab1d0db12008-10-21 17:25:47 -0500141#endif
Kumar Galabe083152008-10-21 17:25:44 -0500142#ifdef CONFIG_LYNXKDI
143 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
144#endif
Kumar Galab1d0db12008-10-21 17:25:47 -0500145#ifdef CONFIG_BOOTM_RTEMS
Kumar Galabe083152008-10-21 17:25:44 -0500146 [IH_OS_RTEMS] = do_bootm_rtems,
Kumar Galab1d0db12008-10-21 17:25:47 -0500147#endif
Torkel Lundgren3df61952010-09-28 11:05:36 +0200148#if defined(CONFIG_BOOTM_OSE)
149 [IH_OS_OSE] = do_bootm_ose,
150#endif
Steven Stallion04d41402013-03-20 06:31:35 +0000151#if defined(CONFIG_BOOTM_PLAN9)
152 [IH_OS_PLAN9] = do_bootm_plan9,
153#endif
Miao Yan17ab52f2013-11-28 17:51:36 +0800154#if defined(CONFIG_BOOTM_VXWORKS)
Kumar Galabe083152008-10-21 17:25:44 -0500155 [IH_OS_VXWORKS] = do_bootm_vxworks,
Miao Yan17ab52f2013-11-28 17:51:36 +0800156#endif
157#if defined(CONFIG_CMD_ELF)
Kumar Galabe083152008-10-21 17:25:44 -0500158 [IH_OS_QNX] = do_bootm_qnxelf,
159#endif
160#ifdef CONFIG_INTEGRITY
161 [IH_OS_INTEGRITY] = do_bootm_integrity,
162#endif
163};
164
Simon Schwarzdee17762011-09-02 00:50:31 +0000165bootm_headers_t images; /* pointers to os/initrd/fdt images */
Stefan Roese15940c92006-03-13 11:16:36 +0100166
Kumar Gala3dfad402009-08-27 08:23:55 -0500167/* Allow for arch specific config before we boot */
Kim Phillips088f1b12012-10-29 13:34:31 +0000168static void __arch_preboot_os(void)
Kumar Gala3dfad402009-08-27 08:23:55 -0500169{
170 /* please define platform specific arch_preboot_os() */
171}
172void arch_preboot_os(void) __attribute__((weak, alias("__arch_preboot_os")));
173
Mike Frysinger476af292011-10-03 14:50:33 +0000174#define IH_INITRD_ARCH IH_ARCH_DEFAULT
wdenk47d1a6e2002-11-03 00:01:44 +0000175
Mike Frysingera16028d2009-11-03 11:35:59 -0500176#ifdef CONFIG_LMB
Marek Vasut44f074c2012-03-14 21:52:45 +0000177static void boot_start_lmb(bootm_headers_t *images)
178{
Becky Bruce391fd932008-06-09 20:37:18 -0500179 ulong mem_start;
180 phys_size_t mem_size;
wdenk47d1a6e2002-11-03 00:01:44 +0000181
Marek Vasut44f074c2012-03-14 21:52:45 +0000182 lmb_init(&images->lmb);
wdenk47d1a6e2002-11-03 00:01:44 +0000183
Kumar Galad3f2fa02008-02-27 21:51:50 -0600184 mem_start = getenv_bootm_low();
185 mem_size = getenv_bootm_size();
wdenk47d1a6e2002-11-03 00:01:44 +0000186
Marek Vasut44f074c2012-03-14 21:52:45 +0000187 lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size);
wdenk47d1a6e2002-11-03 00:01:44 +0000188
Marek Vasut44f074c2012-03-14 21:52:45 +0000189 arch_lmb_reserve(&images->lmb);
190 board_lmb_reserve(&images->lmb);
Mike Frysingera16028d2009-11-03 11:35:59 -0500191}
Marek Vasut44f074c2012-03-14 21:52:45 +0000192#else
Marek Vasut35cf5fe2012-04-01 03:59:46 +0000193#define lmb_reserve(lmb, base, size)
Marek Vasut44f074c2012-03-14 21:52:45 +0000194static inline void boot_start_lmb(bootm_headers_t *images) { }
195#endif
Mike Frysingera16028d2009-11-03 11:35:59 -0500196
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200197static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Mike Frysingera16028d2009-11-03 11:35:59 -0500198{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000199 memset((void *)&images, 0, sizeof(images));
200 images.verify = getenv_yesno("verify");
Mike Frysingera16028d2009-11-03 11:35:59 -0500201
Marek Vasut44f074c2012-03-14 21:52:45 +0000202 boot_start_lmb(&images);
wdenk47d1a6e2002-11-03 00:01:44 +0000203
Simon Glass573f14f2011-12-10 11:08:06 +0000204 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
Simon Glass35fc84f2013-06-11 11:14:47 -0700205 images.state = BOOTM_STATE_START;
206
207 return 0;
208}
209
210static int bootm_find_os(cmd_tbl_t *cmdtp, int flag, int argc,
211 char * const argv[])
212{
213 const void *os_hdr;
Simon Glass573f14f2011-12-10 11:08:06 +0000214
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100215 /* get kernel image header, start address and length */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000216 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
Kumar Gala396f6352008-08-15 08:24:41 -0500217 &images, &images.os.image_start, &images.os.image_len);
218 if (images.os.image_len == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000219 puts("ERROR: can't get kernel image!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000220 return 1;
221 }
wdenk47d1a6e2002-11-03 00:01:44 +0000222
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100223 /* get image parameters */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000224 switch (genimg_get_format(os_hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100225 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000226 images.os.type = image_get_type(os_hdr);
227 images.os.comp = image_get_comp(os_hdr);
228 images.os.os = image_get_os(os_hdr);
Wolfgang Denkbccae902005-10-06 01:50:50 +0200229
Stephen Warren712fbcf2011-10-18 11:11:49 +0000230 images.os.end = image_get_image_end(os_hdr);
231 images.os.load = image_get_load(os_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100232 break;
233#if defined(CONFIG_FIT)
234 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000235 if (fit_image_get_type(images.fit_hdr_os,
Kumar Gala396f6352008-08-15 08:24:41 -0500236 images.fit_noffset_os, &images.os.type)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000237 puts("Can't get image type!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000238 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
wdenk47d1a6e2002-11-03 00:01:44 +0000239 return 1;
240 }
wdenk47d1a6e2002-11-03 00:01:44 +0000241
Stephen Warren712fbcf2011-10-18 11:11:49 +0000242 if (fit_image_get_comp(images.fit_hdr_os,
Kumar Gala396f6352008-08-15 08:24:41 -0500243 images.fit_noffset_os, &images.os.comp)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000244 puts("Can't get image compression!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000245 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100246 return 1;
247 }
wdenk47d1a6e2002-11-03 00:01:44 +0000248
Stephen Warren712fbcf2011-10-18 11:11:49 +0000249 if (fit_image_get_os(images.fit_hdr_os,
Kumar Gala396f6352008-08-15 08:24:41 -0500250 images.fit_noffset_os, &images.os.os)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000251 puts("Can't get image OS!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000252 bootstage_error(BOOTSTAGE_ID_FIT_OS);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100253 return 1;
254 }
wdenk47d1a6e2002-11-03 00:01:44 +0000255
Stephen Warren712fbcf2011-10-18 11:11:49 +0000256 images.os.end = fit_get_end(images.fit_hdr_os);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100257
Stephen Warren712fbcf2011-10-18 11:11:49 +0000258 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
Kumar Gala396f6352008-08-15 08:24:41 -0500259 &images.os.load)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000260 puts("Can't get image load address!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000261 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100262 return 1;
wdenkb13fb012003-10-30 21:49:38 +0000263 }
264 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100265#endif
266 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000267 puts("ERROR: unknown image format type!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000268 return 1;
269 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100270
Kumar Galac160a952008-08-15 08:24:36 -0500271 /* find kernel entry point */
272 if (images.legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000273 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
Kumar Galac160a952008-08-15 08:24:36 -0500274#if defined(CONFIG_FIT)
275 } else if (images.fit_uname_os) {
Simon Glass35fc84f2013-06-11 11:14:47 -0700276 int ret;
277
Stephen Warren712fbcf2011-10-18 11:11:49 +0000278 ret = fit_image_get_entry(images.fit_hdr_os,
Simon Glassc19d13b2013-05-08 08:06:02 +0000279 images.fit_noffset_os, &images.ep);
Kumar Galac160a952008-08-15 08:24:36 -0500280 if (ret) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000281 puts("Can't get entry point property!\n");
Kumar Galac160a952008-08-15 08:24:36 -0500282 return 1;
283 }
284#endif
285 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000286 puts("Could not find kernel entry point!\n");
Kumar Galac160a952008-08-15 08:24:36 -0500287 return 1;
288 }
289
Stephen Warrenb9b50e892011-11-10 13:17:53 -0700290 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
291 images.os.load = images.os.image_start;
292 images.ep += images.os.load;
293 }
294
Simon Glass35fc84f2013-06-11 11:14:47 -0700295 images.os.start = (ulong)os_hdr;
296
297 return 0;
298}
299
Tom Rini225fd8c2013-07-09 15:33:25 -0400300static int bootm_find_ramdisk(int flag, int argc, char * const argv[])
Simon Glass35fc84f2013-06-11 11:14:47 -0700301{
302 int ret;
303
Tom Rini225fd8c2013-07-09 15:33:25 -0400304 /* find ramdisk */
305 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
306 &images.rd_start, &images.rd_end);
307 if (ret) {
308 puts("Ramdisk image is corrupt or invalid\n");
309 return 1;
310 }
311
312 return 0;
313}
314
315#if defined(CONFIG_OF_LIBFDT)
316static int bootm_find_fdt(int flag, int argc, char * const argv[])
317{
318 int ret;
319
320 /* find flattened device tree */
321 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
322 &images.ft_addr, &images.ft_len);
323 if (ret) {
324 puts("Could not find a valid device tree\n");
325 return 1;
326 }
327
328 set_working_fdt_addr(images.ft_addr);
329
330 return 0;
331}
332#endif
333
334static int bootm_find_other(cmd_tbl_t *cmdtp, int flag, int argc,
335 char * const argv[])
336{
Heiko Schocher24de2f42010-03-29 13:15:48 +0200337 if (((images.os.type == IH_TYPE_KERNEL) ||
Stephen Warrenb9b50e892011-11-10 13:17:53 -0700338 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
Heiko Schocher24de2f42010-03-29 13:15:48 +0200339 (images.os.type == IH_TYPE_MULTI)) &&
Detlev Zundel8b828a82009-12-22 12:43:01 +0100340 (images.os.os == IH_OS_LINUX)) {
Tom Rini225fd8c2013-07-09 15:33:25 -0400341 if (bootm_find_ramdisk(flag, argc, argv))
Kumar Galac4f94192008-08-15 08:24:37 -0500342 return 1;
Kumar Gala06a09912008-08-15 08:24:38 -0500343
344#if defined(CONFIG_OF_LIBFDT)
Tom Rini225fd8c2013-07-09 15:33:25 -0400345 if (bootm_find_fdt(flag, argc, argv))
Kumar Gala06a09912008-08-15 08:24:38 -0500346 return 1;
Kumar Gala06a09912008-08-15 08:24:38 -0500347#endif
Kumar Galac4f94192008-08-15 08:24:37 -0500348 }
349
Kumar Gala396f6352008-08-15 08:24:41 -0500350 return 0;
351}
352
353#define BOOTM_ERR_RESET -1
354#define BOOTM_ERR_OVERLAP -2
355#define BOOTM_ERR_UNIMPLEMENTED -3
Tom Rinid3664382013-06-28 11:38:02 -0400356static int bootm_load_os(bootm_headers_t *images, unsigned long *load_end,
357 int boot_progress)
Kumar Gala396f6352008-08-15 08:24:41 -0500358{
Tom Rinid3664382013-06-28 11:38:02 -0400359 image_info_t os = images->os;
Kumar Gala396f6352008-08-15 08:24:41 -0500360 uint8_t comp = os.comp;
361 ulong load = os.load;
362 ulong blob_start = os.start;
363 ulong blob_end = os.end;
364 ulong image_start = os.image_start;
365 ulong image_len = os.image_len;
Anatolij Gustschin5bf27662011-11-19 13:12:18 +0000366 __maybe_unused uint unc_len = CONFIG_SYS_BOOTM_LEN;
Stephen Warrend5108592011-11-10 13:17:54 -0700367 int no_overlap = 0;
Simon Glassaed161e2013-05-16 13:53:24 +0000368 void *load_buf, *image_buf;
Matthias Weisser60fdc5f2010-08-05 13:17:30 +0200369#if defined(CONFIG_LZMA) || defined(CONFIG_LZO)
370 int ret;
371#endif /* defined(CONFIG_LZMA) || defined(CONFIG_LZO) */
Kumar Gala396f6352008-08-15 08:24:41 -0500372
Stephen Warren712fbcf2011-10-18 11:11:49 +0000373 const char *type_name = genimg_get_type_name(os.type);
Kumar Gala396f6352008-08-15 08:24:41 -0500374
Kees Cook315c0ac2013-08-16 07:59:16 -0700375 load_buf = map_sysmem(load, unc_len);
Simon Glassaed161e2013-05-16 13:53:24 +0000376 image_buf = map_sysmem(image_start, image_len);
Kumar Gala396f6352008-08-15 08:24:41 -0500377 switch (comp) {
378 case IH_COMP_NONE:
Lei Wen02cf3452011-01-10 18:21:15 +0800379 if (load == blob_start || load == image_start) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000380 printf(" XIP %s ... ", type_name);
Stephen Warrend5108592011-11-10 13:17:54 -0700381 no_overlap = 1;
Kumar Gala396f6352008-08-15 08:24:41 -0500382 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000383 printf(" Loading %s ... ", type_name);
Simon Glassaed161e2013-05-16 13:53:24 +0000384 memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
Kumar Gala396f6352008-08-15 08:24:41 -0500385 }
386 *load_end = load + image_len;
Kumar Gala396f6352008-08-15 08:24:41 -0500387 break;
Mike Frysinger44431ca2010-01-21 19:30:36 -0500388#ifdef CONFIG_GZIP
Kumar Gala396f6352008-08-15 08:24:41 -0500389 case IH_COMP_GZIP:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000390 printf(" Uncompressing %s ... ", type_name);
Simon Glassaed161e2013-05-16 13:53:24 +0000391 if (gunzip(load_buf, unc_len, image_buf, &image_len) != 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000392 puts("GUNZIP: uncompress, out-of-mem or overwrite "
393 "error - must RESET board to recover\n");
Kumar Gala396f6352008-08-15 08:24:41 -0500394 if (boot_progress)
Simon Glass770605e2012-02-13 13:51:18 +0000395 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
Kumar Gala396f6352008-08-15 08:24:41 -0500396 return BOOTM_ERR_RESET;
397 }
398
399 *load_end = load + image_len;
400 break;
Mike Frysinger44431ca2010-01-21 19:30:36 -0500401#endif /* CONFIG_GZIP */
Kumar Gala396f6352008-08-15 08:24:41 -0500402#ifdef CONFIG_BZIP2
403 case IH_COMP_BZIP2:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000404 printf(" Uncompressing %s ... ", type_name);
Kumar Gala396f6352008-08-15 08:24:41 -0500405 /*
406 * If we've got less than 4 MB of malloc() space,
407 * use slower decompression algorithm which requires
408 * at most 2300 KB of memory.
409 */
Simon Glassaed161e2013-05-16 13:53:24 +0000410 int i = BZ2_bzBuffToBuffDecompress(load_buf, &unc_len,
411 image_buf, image_len,
412 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
Kumar Gala396f6352008-08-15 08:24:41 -0500413 if (i != BZ_OK) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000414 printf("BUNZIP2: uncompress or overwrite error %d "
Kumar Gala396f6352008-08-15 08:24:41 -0500415 "- must RESET board to recover\n", i);
416 if (boot_progress)
Simon Glass770605e2012-02-13 13:51:18 +0000417 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
Kumar Gala396f6352008-08-15 08:24:41 -0500418 return BOOTM_ERR_RESET;
419 }
420
421 *load_end = load + unc_len;
422 break;
423#endif /* CONFIG_BZIP2 */
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200424#ifdef CONFIG_LZMA
Mike Frysinger78e1e842010-07-25 15:54:17 -0400425 case IH_COMP_LZMA: {
426 SizeT lzma_len = unc_len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000427 printf(" Uncompressing %s ... ", type_name);
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200428
Simon Glassaed161e2013-05-16 13:53:24 +0000429 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
430 image_buf, image_len);
Mike Frysinger78e1e842010-07-25 15:54:17 -0400431 unc_len = lzma_len;
Luigi 'Comio' Mantellinicaf72ff2009-07-21 10:45:49 +0200432 if (ret != SZ_OK) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000433 printf("LZMA: uncompress or overwrite error %d "
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200434 "- must RESET board to recover\n", ret);
Simon Glass770605e2012-02-13 13:51:18 +0000435 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200436 return BOOTM_ERR_RESET;
437 }
438 *load_end = load + unc_len;
439 break;
Mike Frysinger78e1e842010-07-25 15:54:17 -0400440 }
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200441#endif /* CONFIG_LZMA */
Peter Korsgaard20dde482009-11-19 11:37:51 +0100442#ifdef CONFIG_LZO
Simon Glass628af172013-08-30 11:00:09 -0600443 case IH_COMP_LZO: {
444 size_t size;
445
Stephen Warren712fbcf2011-10-18 11:11:49 +0000446 printf(" Uncompressing %s ... ", type_name);
Peter Korsgaard20dde482009-11-19 11:37:51 +0100447
Simon Glass628af172013-08-30 11:00:09 -0600448 ret = lzop_decompress(image_buf, image_len, load_buf, &size);
Peter Korsgaard20dde482009-11-19 11:37:51 +0100449 if (ret != LZO_E_OK) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000450 printf("LZO: uncompress or overwrite error %d "
Peter Korsgaard20dde482009-11-19 11:37:51 +0100451 "- must RESET board to recover\n", ret);
452 if (boot_progress)
Simon Glass770605e2012-02-13 13:51:18 +0000453 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
Peter Korsgaard20dde482009-11-19 11:37:51 +0100454 return BOOTM_ERR_RESET;
455 }
456
Simon Glass628af172013-08-30 11:00:09 -0600457 *load_end = load + size;
Peter Korsgaard20dde482009-11-19 11:37:51 +0100458 break;
Simon Glass628af172013-08-30 11:00:09 -0600459 }
Peter Korsgaard20dde482009-11-19 11:37:51 +0100460#endif /* CONFIG_LZO */
Kumar Gala396f6352008-08-15 08:24:41 -0500461 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000462 printf("Unimplemented compression type %d\n", comp);
Kumar Gala396f6352008-08-15 08:24:41 -0500463 return BOOTM_ERR_UNIMPLEMENTED;
464 }
Diana CRACIUN99ffccb2011-08-31 02:45:23 +0000465
466 flush_cache(load, (*load_end - load) * sizeof(ulong));
467
Stephen Warren712fbcf2011-10-18 11:11:49 +0000468 puts("OK\n");
469 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
Simon Glass770605e2012-02-13 13:51:18 +0000470 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
Kumar Gala396f6352008-08-15 08:24:41 -0500471
Stephen Warrend5108592011-11-10 13:17:54 -0700472 if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000473 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
474 blob_start, blob_end);
475 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
476 *load_end);
Kumar Gala396f6352008-08-15 08:24:41 -0500477
Tom Rinid3664382013-06-28 11:38:02 -0400478 /* Check what type of image this is. */
479 if (images->legacy_hdr_valid) {
480 if (image_get_type(&images->legacy_hdr_os_copy)
481 == IH_TYPE_MULTI)
482 puts("WARNING: legacy format multi component image overwritten\n");
483 return BOOTM_ERR_OVERLAP;
484 } else {
485 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
486 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
487 return BOOTM_ERR_RESET;
488 }
Kumar Gala396f6352008-08-15 08:24:41 -0500489 }
490
491 return 0;
492}
493
Simon Glass35fc84f2013-06-11 11:14:47 -0700494static int bootm_start_standalone(int argc, char * const argv[])
Detlev Zundelf97ec302009-07-13 16:01:19 +0200495{
496 char *s;
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200497 int (*appl)(int, char * const []);
Detlev Zundelf97ec302009-07-13 16:01:19 +0200498
499 /* Don't start if "autostart" is set to "no" */
500 if (((s = getenv("autostart")) != NULL) && (strcmp(s, "no") == 0)) {
Simon Glass41ef3722013-02-24 17:33:22 +0000501 setenv_hex("filesize", images.os.image_len);
Detlev Zundelf97ec302009-07-13 16:01:19 +0200502 return 0;
503 }
Simon Glass6d6f1232011-10-07 13:53:40 +0000504 appl = (int (*)(int, char * const []))(ulong)ntohl(images.ep);
Simon Glass983c72f2013-06-11 11:14:46 -0700505 (*appl)(argc, argv);
Detlev Zundelf97ec302009-07-13 16:01:19 +0200506 return 0;
507}
508
Kumar Gala49c3a862008-10-21 17:25:45 -0500509/* we overload the cmd field with our state machine info instead of a
510 * function pointer */
Frans Meulenbroeksf74d9bd2010-02-25 10:12:13 +0100511static cmd_tbl_t cmd_bootm_sub[] = {
Kumar Gala49c3a862008-10-21 17:25:45 -0500512 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
513 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
John Rigbyfca43cc2010-10-13 13:57:35 -0600514#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Kumar Gala49c3a862008-10-21 17:25:45 -0500515 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
516#endif
517#ifdef CONFIG_OF_LIBFDT
518 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
519#endif
Kumar Gala49c3a862008-10-21 17:25:45 -0500520 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
Peter Tyser224c90d2009-11-18 19:08:59 -0600521 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
Kumar Gala49c3a862008-10-21 17:25:45 -0500522 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
Simon Glassd0ae31e2013-06-11 11:14:48 -0700523 U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
Kumar Gala49c3a862008-10-21 17:25:45 -0500524 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
525};
526
Simon Glass35fc84f2013-06-11 11:14:47 -0700527static int boot_selected_os(int argc, char * const argv[], int state,
Simon Glass5ff0d082013-06-28 00:46:12 -0700528 bootm_headers_t *images, boot_os_fn *boot_fn)
Simon Glass35fc84f2013-06-11 11:14:47 -0700529{
530 if (images->os.type == IH_TYPE_STANDALONE) {
531 /* This may return when 'autostart' is 'no' */
532 bootm_start_standalone(argc, argv);
533 return 0;
534 }
Simon Glass35fc84f2013-06-11 11:14:47 -0700535 arch_preboot_os();
536 boot_fn(state, argc, argv, images);
Simon Glassd0ae31e2013-06-11 11:14:48 -0700537 if (state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
538 return 0;
Simon Glass35fc84f2013-06-11 11:14:47 -0700539 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
540#ifdef DEBUG
541 puts("\n## Control returned to monitor - resetting...\n");
542#endif
543 return BOOTM_ERR_RESET;
544}
545
546/**
Simon Glass385501d2013-07-04 13:26:08 -0700547 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
548 *
549 * @return interrupt flag (0 if interrupts were disabled, non-zero if they were
550 * enabled)
551 */
552static ulong bootm_disable_interrupts(void)
553{
554 ulong iflag;
555
556 /*
557 * We have reached the point of no return: we are going to
558 * overwrite all exception vector code, so we cannot easily
559 * recover from any failures any more...
560 */
561 iflag = disable_interrupts();
562#ifdef CONFIG_NETCONSOLE
563 /* Stop the ethernet stack if NetConsole could have left it up */
564 eth_halt();
Frederic Leroy80949722013-09-10 12:02:31 +0200565 eth_unregister(eth_get_dev());
Simon Glass385501d2013-07-04 13:26:08 -0700566#endif
567
568#if defined(CONFIG_CMD_USB)
569 /*
570 * turn off USB to prevent the host controller from writing to the
571 * SDRAM while Linux is booting. This could happen (at least for OHCI
572 * controller), because the HCCA (Host Controller Communication Area)
573 * lies within the SDRAM and the host controller writes continously to
574 * this area (as busmaster!). The HccaFrameNumber is for example
575 * updated every 1 ms within the HCCA structure in SDRAM! For more
576 * details see the OpenHCI specification.
577 */
578 usb_stop();
579#endif
580 return iflag;
581}
582
583/**
Simon Glass35fc84f2013-06-11 11:14:47 -0700584 * Execute selected states of the bootm command.
585 *
586 * Note the arguments to this state must be the first argument, Any 'bootm'
587 * or sub-command arguments must have already been taken.
588 *
589 * Note that if states contains more than one flag it MUST contain
590 * BOOTM_STATE_START, since this handles and consumes the command line args.
591 *
Tom Rinid3664382013-06-28 11:38:02 -0400592 * Also note that aside from boot_os_fn functions and bootm_load_os no other
593 * functions we store the return value of in 'ret' may use a negative return
594 * value, without special handling.
595 *
Simon Glass35fc84f2013-06-11 11:14:47 -0700596 * @param cmdtp Pointer to bootm command table entry
597 * @param flag Command flags (CMD_FLAG_...)
598 * @param argc Number of subcommand arguments (0 = no arguments)
599 * @param argv Arguments
600 * @param states Mask containing states to run (BOOTM_STATE_...)
601 * @param images Image header information
602 * @param boot_progress 1 to show boot progress, 0 to not do this
603 * @return 0 if ok, something else on error. Some errors will cause this
604 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
605 * then the intent is to boot an OS, so this function will not return
606 * unless the image type is standalone.
607 */
608static int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc,
609 char * const argv[], int states, bootm_headers_t *images,
610 int boot_progress)
611{
612 boot_os_fn *boot_fn;
613 ulong iflag = 0;
Simon Glassa26913f2013-07-04 13:26:09 -0700614 int ret = 0, need_boot_fn;
Simon Glass35fc84f2013-06-11 11:14:47 -0700615
616 images->state |= states;
617
618 /*
619 * Work through the states and see how far we get. We stop on
620 * any error.
621 */
622 if (states & BOOTM_STATE_START)
623 ret = bootm_start(cmdtp, flag, argc, argv);
624
625 if (!ret && (states & BOOTM_STATE_FINDOS))
626 ret = bootm_find_os(cmdtp, flag, argc, argv);
627
628 if (!ret && (states & BOOTM_STATE_FINDOTHER)) {
629 ret = bootm_find_other(cmdtp, flag, argc, argv);
630 argc = 0; /* consume the args */
631 }
632
633 /* Load the OS */
634 if (!ret && (states & BOOTM_STATE_LOADOS)) {
635 ulong load_end;
636
Simon Glass385501d2013-07-04 13:26:08 -0700637 iflag = bootm_disable_interrupts();
Tom Rinid3664382013-06-28 11:38:02 -0400638 ret = bootm_load_os(images, &load_end, 0);
Tom Rinid3664382013-06-28 11:38:02 -0400639 if (ret == 0)
Simon Glass35fc84f2013-06-11 11:14:47 -0700640 lmb_reserve(&images->lmb, images->os.load,
641 (load_end - images->os.load));
Tom Rini970150a2013-07-11 09:35:23 -0400642 else if (ret && ret != BOOTM_ERR_OVERLAP)
643 goto err;
Tom Rinid3664382013-06-28 11:38:02 -0400644 else if (ret == BOOTM_ERR_OVERLAP)
645 ret = 0;
Paul B. Hensonbd4a3992013-08-03 21:29:09 -0700646#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
Simon Glass576aacd2013-07-16 20:09:59 -0700647 if (images->os.os == IH_OS_LINUX)
648 fixup_silent_linux();
649#endif
Simon Glass35fc84f2013-06-11 11:14:47 -0700650 }
651
652 /* Relocate the ramdisk */
653#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
654 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
655 ulong rd_len = images->rd_end - images->rd_start;
656
657 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
658 rd_len, &images->initrd_start, &images->initrd_end);
659 if (!ret) {
660 setenv_hex("initrd_start", images->initrd_start);
661 setenv_hex("initrd_end", images->initrd_end);
662 }
663 }
664#endif
665#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_LMB)
666 if (!ret && (states & BOOTM_STATE_FDT)) {
667 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
668 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
669 &images->ft_len);
670 }
671#endif
672
673 /* From now on, we need the OS boot function */
674 if (ret)
675 return ret;
676 boot_fn = boot_os[images->os.os];
Simon Glassa26913f2013-07-04 13:26:09 -0700677 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
678 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
679 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
680 if (boot_fn == NULL && need_boot_fn) {
Simon Glass35fc84f2013-06-11 11:14:47 -0700681 if (iflag)
682 enable_interrupts();
683 printf("ERROR: booting os '%s' (%d) is not supported\n",
684 genimg_get_os_name(images->os.os), images->os.os);
685 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
686 return 1;
687 }
688
689 /* Call various other states that are not generally used */
690 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
691 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
692 if (!ret && (states & BOOTM_STATE_OS_BD_T))
693 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
694 if (!ret && (states & BOOTM_STATE_OS_PREP))
695 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
696
Simon Glassd0ae31e2013-06-11 11:14:48 -0700697#ifdef CONFIG_TRACE
698 /* Pretend to run the OS, then run a user command */
699 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
700 char *cmd_list = getenv("fakegocmd");
701
702 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
Simon Glass5ff0d082013-06-28 00:46:12 -0700703 images, boot_fn);
Simon Glassd0ae31e2013-06-11 11:14:48 -0700704 if (!ret && cmd_list)
705 ret = run_command_list(cmd_list, -1, flag);
706 }
707#endif
Simon Glassb7a1d132013-07-10 09:25:33 -0400708
709 /* Check for unsupported subcommand. */
710 if (ret) {
711 puts("subcommand not supported\n");
712 return ret;
Tom Rinid3664382013-06-28 11:38:02 -0400713 }
714
Simon Glassb7a1d132013-07-10 09:25:33 -0400715 /* Now run the OS! We hope this doesn't return */
716 if (!ret && (states & BOOTM_STATE_OS_GO))
717 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
718 images, boot_fn);
Simon Glass35fc84f2013-06-11 11:14:47 -0700719
720 /* Deal with any fallout */
Tom Rinid3664382013-06-28 11:38:02 -0400721err:
Simon Glass35fc84f2013-06-11 11:14:47 -0700722 if (iflag)
723 enable_interrupts();
Tom Rinid3664382013-06-28 11:38:02 -0400724
725 if (ret == BOOTM_ERR_UNIMPLEMENTED)
726 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
727 else if (ret == BOOTM_ERR_RESET)
728 do_reset(cmdtp, flag, argc, argv);
Simon Glass35fc84f2013-06-11 11:14:47 -0700729
730 return ret;
731}
732
Kim Phillips088f1b12012-10-29 13:34:31 +0000733static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
Stephen Warren712fbcf2011-10-18 11:11:49 +0000734 char * const argv[])
Kumar Gala49c3a862008-10-21 17:25:45 -0500735{
736 int ret = 0;
Simon Glass6d6f1232011-10-07 13:53:40 +0000737 long state;
Kumar Gala49c3a862008-10-21 17:25:45 -0500738 cmd_tbl_t *c;
Kumar Gala49c3a862008-10-21 17:25:45 -0500739
Simon Glass983c72f2013-06-11 11:14:46 -0700740 c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
741 argc--; argv++;
Kumar Gala49c3a862008-10-21 17:25:45 -0500742
743 if (c) {
Simon Glass6d6f1232011-10-07 13:53:40 +0000744 state = (long)c->cmd;
Simon Glass983c72f2013-06-11 11:14:46 -0700745 if (state == BOOTM_STATE_START)
Simon Glass35fc84f2013-06-11 11:14:47 -0700746 state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200747 } else {
748 /* Unrecognized command */
Simon Glass4c12eeb2011-12-10 08:44:01 +0000749 return CMD_RET_USAGE;
Kumar Gala49c3a862008-10-21 17:25:45 -0500750 }
751
Simon Glass35fc84f2013-06-11 11:14:47 -0700752 if (state != BOOTM_STATE_START && images.state >= state) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000753 printf("Trying to execute a command out of order\n");
Simon Glass4c12eeb2011-12-10 08:44:01 +0000754 return CMD_RET_USAGE;
Kumar Gala49c3a862008-10-21 17:25:45 -0500755 }
756
Simon Glass35fc84f2013-06-11 11:14:47 -0700757 ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
Kumar Gala49c3a862008-10-21 17:25:45 -0500758
759 return ret;
760}
761
Kumar Gala396f6352008-08-15 08:24:41 -0500762/*******************************************************************/
763/* bootm - boot application image from image in memory */
764/*******************************************************************/
Kumar Galabe083152008-10-21 17:25:44 -0500765
Stephen Warren712fbcf2011-10-18 11:11:49 +0000766int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Kumar Gala396f6352008-08-15 08:24:41 -0500767{
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200768#ifdef CONFIG_NEEDS_MANUAL_RELOC
Peter Tyser521af042009-09-21 11:20:36 -0500769 static int relocated = 0;
Kumar Galabe083152008-10-21 17:25:44 -0500770
Kumar Galabe083152008-10-21 17:25:44 -0500771 if (!relocated) {
772 int i;
Daniel Schwierzeck58bd77d2013-01-07 06:54:52 +0000773
774 /* relocate boot function table */
Kumar Galabe083152008-10-21 17:25:44 -0500775 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
Detlev Zundelca95c9d2009-07-13 16:01:18 +0200776 if (boot_os[i] != NULL)
777 boot_os[i] += gd->reloc_off;
Daniel Schwierzeck58bd77d2013-01-07 06:54:52 +0000778
779 /* relocate names of sub-command table */
780 for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
781 cmd_bootm_sub[i].name += gd->reloc_off;
782
Kumar Galabe083152008-10-21 17:25:44 -0500783 relocated = 1;
784 }
Peter Tyser521af042009-09-21 11:20:36 -0500785#endif
Kumar Gala396f6352008-08-15 08:24:41 -0500786
Kumar Gala49c3a862008-10-21 17:25:45 -0500787 /* determine if we have a sub command */
Simon Glass983c72f2013-06-11 11:14:46 -0700788 argc--; argv++;
789 if (argc > 0) {
Kumar Gala49c3a862008-10-21 17:25:45 -0500790 char *endp;
791
Simon Glass983c72f2013-06-11 11:14:46 -0700792 simple_strtoul(argv[0], &endp, 16);
793 /* endp pointing to NULL means that argv[0] was just a
Kumar Gala49c3a862008-10-21 17:25:45 -0500794 * valid number, pass it along to the normal bootm processing
795 *
796 * If endp is ':' or '#' assume a FIT identifier so pass
797 * along for normal processing.
798 *
799 * Right now we assume the first arg should never be '-'
800 */
801 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
802 return do_bootm_subcommand(cmdtp, flag, argc, argv);
803 }
804
Simon Glass35fc84f2013-06-11 11:14:47 -0700805 return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
806 BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
Tom Rini3d187b32013-09-23 14:20:37 -0400807 BOOTM_STATE_LOADOS |
808#if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
809 BOOTM_STATE_OS_CMDLINE |
810#endif
Paul Burton5c427e42013-09-06 11:23:55 +0100811 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
812 BOOTM_STATE_OS_GO, &images, 1);
wdenk47d1a6e2002-11-03 00:01:44 +0000813}
814
Mike Frysinger67d668b2011-06-05 13:43:02 +0000815int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
816{
817 const char *ep = getenv("autostart");
818
819 if (ep && !strcmp(ep, "yes")) {
820 char *local_args[2];
821 local_args[0] = (char *)cmd;
822 local_args[1] = NULL;
823 printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
824 return do_bootm(cmdtp, 0, 1, local_args);
825 }
826
827 return 0;
828}
829
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100830/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100831 * image_get_kernel - verify legacy format kernel image
832 * @img_addr: in RAM address of the legacy format image to be verified
833 * @verify: data CRC verification flag
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100834 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100835 * image_get_kernel() verifies legacy image integrity and returns pointer to
836 * legacy image header if image verification was completed successfully.
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100837 *
838 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100839 * pointer to a legacy image header if valid image was found
840 * otherwise return NULL
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100841 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000842static image_header_t *image_get_kernel(ulong img_addr, int verify)
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100843{
844 image_header_t *hdr = (image_header_t *)img_addr;
845
846 if (!image_check_magic(hdr)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000847 puts("Bad Magic Number\n");
Simon Glass770605e2012-02-13 13:51:18 +0000848 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100849 return NULL;
850 }
Simon Glass770605e2012-02-13 13:51:18 +0000851 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100852
Stephen Warren712fbcf2011-10-18 11:11:49 +0000853 if (!image_check_hcrc(hdr)) {
854 puts("Bad Header Checksum\n");
Simon Glass770605e2012-02-13 13:51:18 +0000855 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100856 return NULL;
857 }
858
Simon Glass770605e2012-02-13 13:51:18 +0000859 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000860 image_print_contents(hdr);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100861
862 if (verify) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000863 puts(" Verifying Checksum ... ");
864 if (!image_check_dcrc(hdr)) {
865 printf("Bad Data CRC\n");
Simon Glass770605e2012-02-13 13:51:18 +0000866 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100867 return NULL;
868 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000869 puts("OK\n");
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100870 }
Simon Glass770605e2012-02-13 13:51:18 +0000871 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100872
Stephen Warren712fbcf2011-10-18 11:11:49 +0000873 if (!image_check_target_arch(hdr)) {
874 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
Simon Glass770605e2012-02-13 13:51:18 +0000875 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100876 return NULL;
877 }
878 return hdr;
879}
880
881/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100882 * boot_get_kernel - find kernel image
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100883 * @os_data: pointer to a ulong variable, will hold os data start address
884 * @os_len: pointer to a ulong variable, will hold os data length
885 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100886 * boot_get_kernel() tries to find a kernel image, verifies its integrity
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100887 * and locates kernel data.
888 *
889 * returns:
890 * pointer to image header if valid image was found, plus kernel start
891 * address and length, otherwise NULL
892 */
Simon Glass35e7b0f2013-05-07 06:12:03 +0000893static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
Stephen Warren712fbcf2011-10-18 11:11:49 +0000894 char * const argv[], bootm_headers_t *images, ulong *os_data,
895 ulong *os_len)
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100896{
897 image_header_t *hdr;
898 ulong img_addr;
Simon Glass35e7b0f2013-05-07 06:12:03 +0000899 const void *buf;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100900#if defined(CONFIG_FIT)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100901 const char *fit_uname_config = NULL;
902 const char *fit_uname_kernel = NULL;
Marian Balakowicz6986a382008-03-12 10:01:05 +0100903 int os_noffset;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100904#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100905
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100906 /* find out kernel image address */
Simon Glass983c72f2013-06-11 11:14:46 -0700907 if (argc < 1) {
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100908 img_addr = load_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000909 debug("* kernel: default image load address = 0x%08lx\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100910 load_addr);
911#if defined(CONFIG_FIT)
Simon Glass983c72f2013-06-11 11:14:46 -0700912 } else if (fit_parse_conf(argv[0], load_addr, &img_addr,
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100913 &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000914 debug("* kernel: config '%s' from image at 0x%08lx\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100915 fit_uname_config, img_addr);
Simon Glass983c72f2013-06-11 11:14:46 -0700916 } else if (fit_parse_subimage(argv[0], load_addr, &img_addr,
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100917 &fit_uname_kernel)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000918 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100919 fit_uname_kernel, img_addr);
920#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100921 } else {
Simon Glass983c72f2013-06-11 11:14:46 -0700922 img_addr = simple_strtoul(argv[0], NULL, 16);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000923 debug("* kernel: cmdline image address = 0x%08lx\n", img_addr);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100924 }
925
Simon Glass770605e2012-02-13 13:51:18 +0000926 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100927
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100928 /* copy from dataflash if needed */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000929 img_addr = genimg_get_image(img_addr);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100930
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100931 /* check image type, for FIT images get FIT kernel node */
Marian Balakowicz6986a382008-03-12 10:01:05 +0100932 *os_data = *os_len = 0;
Simon Glass35e7b0f2013-05-07 06:12:03 +0000933 buf = map_sysmem(img_addr, 0);
934 switch (genimg_get_format(buf)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100935 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000936 printf("## Booting kernel from Legacy Image at %08lx ...\n",
Marian Balakowicz6986a382008-03-12 10:01:05 +0100937 img_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000938 hdr = image_get_kernel(img_addr, images->verify);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100939 if (!hdr)
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100940 return NULL;
Simon Glass770605e2012-02-13 13:51:18 +0000941 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100942
Marian Balakowicz6986a382008-03-12 10:01:05 +0100943 /* get os_data and os_len */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000944 switch (image_get_type(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100945 case IH_TYPE_KERNEL:
Stephen Warrenb9b50e892011-11-10 13:17:53 -0700946 case IH_TYPE_KERNEL_NOLOAD:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000947 *os_data = image_get_data(hdr);
948 *os_len = image_get_data_size(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100949 break;
950 case IH_TYPE_MULTI:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000951 image_multi_getimg(hdr, 0, os_data, os_len);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100952 break;
Detlev Zundelf97ec302009-07-13 16:01:19 +0200953 case IH_TYPE_STANDALONE:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000954 *os_data = image_get_data(hdr);
955 *os_len = image_get_data_size(hdr);
Detlev Zundelf97ec302009-07-13 16:01:19 +0200956 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100957 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000958 printf("Wrong Image Type for %s command\n",
959 cmdtp->name);
Simon Glass770605e2012-02-13 13:51:18 +0000960 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100961 return NULL;
962 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100963
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200964 /*
Stephen Warren712fbcf2011-10-18 11:11:49 +0000965 * copy image header to allow for image overwrites during
966 * kernel decompression.
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200967 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000968 memmove(&images->legacy_hdr_os_copy, hdr,
969 sizeof(image_header_t));
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200970
971 /* save pointer to image header */
972 images->legacy_hdr_os = hdr;
973
974 images->legacy_hdr_valid = 1;
Simon Glass770605e2012-02-13 13:51:18 +0000975 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100976 break;
977#if defined(CONFIG_FIT)
978 case IMAGE_FORMAT_FIT:
Simon Glass46518002013-05-16 13:53:25 +0000979 os_noffset = fit_image_load(images, FIT_KERNEL_PROP,
980 img_addr,
Simon Glassf320a4d2013-07-10 23:08:10 -0700981 &fit_uname_kernel, &fit_uname_config,
Simon Glass46518002013-05-16 13:53:25 +0000982 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
983 BOOTSTAGE_ID_FIT_KERNEL_START,
984 FIT_LOAD_IGNORED, os_data, os_len);
985 if (os_noffset < 0)
Marian Balakowicz6986a382008-03-12 10:01:05 +0100986 return NULL;
987
Simon Glass46518002013-05-16 13:53:25 +0000988 images->fit_hdr_os = map_sysmem(img_addr, 0);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100989 images->fit_uname_os = fit_uname_kernel;
Simon Glassf320a4d2013-07-10 23:08:10 -0700990 images->fit_uname_cfg = fit_uname_config;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +0100991 images->fit_noffset_os = os_noffset;
Marian Balakowicz6986a382008-03-12 10:01:05 +0100992 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100993#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100994 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000995 printf("Wrong Image Format for %s command\n", cmdtp->name);
Simon Glass770605e2012-02-13 13:51:18 +0000996 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100997 return NULL;
998 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100999
Stephen Warren712fbcf2011-10-18 11:11:49 +00001000 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
Marian Balakowicz6986a382008-03-12 10:01:05 +01001001 *os_data, *os_len, *os_len);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +01001002
Simon Glass35e7b0f2013-05-07 06:12:03 +00001003 return buf;
Marian Balakowicz5cf746c2008-01-31 13:59:09 +01001004}
1005
Kim Phillips088f1b12012-10-29 13:34:31 +00001006#ifdef CONFIG_SYS_LONGHELP
1007static char bootm_help_text[] =
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001008 "[addr [arg ...]]\n - boot application image stored in memory\n"
1009 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
1010 "\t'arg' can be the address of an initrd image\n"
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +01001011#if defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -05001012 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
Detlev Zundel5441f612007-10-19 16:47:26 +02001013 "\ta third argument is required which is the address of the\n"
Matthew McClintock98a9c4d2006-06-28 10:41:37 -05001014 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1015 "\tuse a '-' for the second argument. If you do not pass a third\n"
1016 "\ta bd_info struct will be passed instead\n"
1017#endif
Marian Balakowicz6986a382008-03-12 10:01:05 +01001018#if defined(CONFIG_FIT)
1019 "\t\nFor the new multi component uImage format (FIT) addresses\n"
1020 "\tmust be extened to include component or configuration unit name:\n"
1021 "\taddr:<subimg_uname> - direct component image specification\n"
1022 "\taddr#<conf_uname> - configuration specification\n"
1023 "\tUse iminfo command to get the list of existing component\n"
1024 "\timages and configurations.\n"
1025#endif
Kumar Gala49c3a862008-10-21 17:25:45 -05001026 "\nSub-commands to do part of the bootm sequence. The sub-commands "
1027 "must be\n"
1028 "issued in the order below (it's ok to not issue all sub-commands):\n"
1029 "\tstart [addr [arg ...]]\n"
1030 "\tloados - load OS image\n"
Daniel Schwierzeck59af76d2013-02-26 04:54:19 +00001031#if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
Kumar Gala49c3a862008-10-21 17:25:45 -05001032 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
1033#endif
1034#if defined(CONFIG_OF_LIBFDT)
1035 "\tfdt - relocate flat device tree\n"
1036#endif
Kumar Gala49c3a862008-10-21 17:25:45 -05001037 "\tcmdline - OS specific command line processing/setup\n"
Peter Tyser224c90d2009-11-18 19:08:59 -06001038 "\tbdt - OS specific bd_t processing\n"
Kumar Gala49c3a862008-10-21 17:25:45 -05001039 "\tprep - OS specific prep before relocation or go\n"
Kim Phillips088f1b12012-10-29 13:34:31 +00001040 "\tgo - start OS";
1041#endif
1042
1043U_BOOT_CMD(
1044 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
1045 "boot application image from memory", bootm_help_text
wdenk8bde7f72003-06-27 21:31:46 +00001046);
1047
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001048/*******************************************************************/
1049/* bootd - boot default image */
1050/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001051#if defined(CONFIG_CMD_BOOTD)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001052int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk47d1a6e2002-11-03 00:01:44 +00001053{
1054 int rcode = 0;
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001055
Simon Glass53071532012-02-14 19:59:21 +00001056 if (run_command(getenv("bootcmd"), flag) < 0)
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001057 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001058 return rcode;
1059}
wdenk8bde7f72003-06-27 21:31:46 +00001060
wdenk0d498392003-07-01 21:06:45 +00001061U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001062 boot, 1, 1, do_bootd,
Peter Tyser2fb26042009-01-27 18:03:12 -06001063 "boot default, i.e., run 'bootcmd'",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001064 ""
wdenk9d2b18a2003-06-28 23:11:04 +00001065);
1066
1067/* keep old command name "bootd" for backward compatibility */
wdenk0d498392003-07-01 21:06:45 +00001068U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001069 bootd, 1, 1, do_bootd,
Peter Tyser2fb26042009-01-27 18:03:12 -06001070 "boot default, i.e., run 'bootcmd'",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001071 ""
wdenk8bde7f72003-06-27 21:31:46 +00001072);
1073
wdenk47d1a6e2002-11-03 00:01:44 +00001074#endif
1075
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001076
1077/*******************************************************************/
1078/* iminfo - print header info for a requested image */
1079/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001080#if defined(CONFIG_CMD_IMI)
Kim Phillips088f1b12012-10-29 13:34:31 +00001081static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk47d1a6e2002-11-03 00:01:44 +00001082{
1083 int arg;
1084 ulong addr;
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001085 int rcode = 0;
wdenk47d1a6e2002-11-03 00:01:44 +00001086
1087 if (argc < 2) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001088 return image_info(load_addr);
wdenk47d1a6e2002-11-03 00:01:44 +00001089 }
1090
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001091 for (arg = 1; arg < argc; ++arg) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001092 addr = simple_strtoul(argv[arg], NULL, 16);
1093 if (image_info(addr) != 0)
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001094 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001095 }
1096 return rcode;
1097}
1098
Stephen Warren712fbcf2011-10-18 11:11:49 +00001099static int image_info(ulong addr)
wdenk47d1a6e2002-11-03 00:01:44 +00001100{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001101 void *hdr = (void *)addr;
wdenk47d1a6e2002-11-03 00:01:44 +00001102
Stephen Warren712fbcf2011-10-18 11:11:49 +00001103 printf("\n## Checking Image at %08lx ...\n", addr);
wdenk47d1a6e2002-11-03 00:01:44 +00001104
Stephen Warren712fbcf2011-10-18 11:11:49 +00001105 switch (genimg_get_format(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001106 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001107 puts(" Legacy image found\n");
1108 if (!image_check_magic(hdr)) {
1109 puts(" Bad Magic Number\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001110 return 1;
1111 }
1112
Stephen Warren712fbcf2011-10-18 11:11:49 +00001113 if (!image_check_hcrc(hdr)) {
1114 puts(" Bad Header Checksum\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001115 return 1;
1116 }
1117
Stephen Warren712fbcf2011-10-18 11:11:49 +00001118 image_print_contents(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001119
Stephen Warren712fbcf2011-10-18 11:11:49 +00001120 puts(" Verifying Checksum ... ");
1121 if (!image_check_dcrc(hdr)) {
1122 puts(" Bad Data CRC\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001123 return 1;
1124 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001125 puts("OK\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001126 return 0;
1127#if defined(CONFIG_FIT)
1128 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001129 puts(" FIT image found\n");
Marian Balakowicze32fea62008-03-11 12:35:20 +01001130
Stephen Warren712fbcf2011-10-18 11:11:49 +00001131 if (!fit_check_format(hdr)) {
1132 puts("Bad FIT image format!\n");
Marian Balakowicze32fea62008-03-11 12:35:20 +01001133 return 1;
1134 }
1135
Stephen Warren712fbcf2011-10-18 11:11:49 +00001136 fit_print_contents(hdr);
Bartlomiej Siekaa4f24342008-09-09 12:58:16 +02001137
Simon Glassb8da8362013-05-07 06:11:57 +00001138 if (!fit_all_image_verify(hdr)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001139 puts("Bad hash in FIT image!\n");
Bartlomiej Siekaa4f24342008-09-09 12:58:16 +02001140 return 1;
1141 }
1142
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001143 return 0;
1144#endif
1145 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001146 puts("Unknown image format!\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001147 break;
wdenk47d1a6e2002-11-03 00:01:44 +00001148 }
1149
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001150 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001151}
wdenk0d498392003-07-01 21:06:45 +00001152
1153U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001154 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
Peter Tyser2fb26042009-01-27 18:03:12 -06001155 "print header information for application image",
wdenk8bde7f72003-06-27 21:31:46 +00001156 "addr [addr ...]\n"
1157 " - print header information for application image starting at\n"
1158 " address 'addr' in memory; this includes verification of the\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001159 " image contents (magic number, header and payload checksums)"
wdenk8bde7f72003-06-27 21:31:46 +00001160);
Jon Loeliger90253172007-07-10 11:02:44 -05001161#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001162
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001163
1164/*******************************************************************/
1165/* imls - list all images found in flash */
1166/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001167#if defined(CONFIG_CMD_IMLS)
Vipin Kumar8fdf1e02012-12-16 22:32:48 +00001168static int do_imls_nor(void)
wdenk27b207f2003-07-24 23:38:38 +00001169{
1170 flash_info_t *info;
1171 int i, j;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001172 void *hdr;
wdenk27b207f2003-07-24 23:38:38 +00001173
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001174 for (i = 0, info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001175 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001176
wdenk27b207f2003-07-24 23:38:38 +00001177 if (info->flash_id == FLASH_UNKNOWN)
1178 goto next_bank;
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001179 for (j = 0; j < info->sector_count; ++j) {
wdenk27b207f2003-07-24 23:38:38 +00001180
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001181 hdr = (void *)info->start[j];
1182 if (!hdr)
wdenk27b207f2003-07-24 23:38:38 +00001183 goto next_sector;
1184
Stephen Warren712fbcf2011-10-18 11:11:49 +00001185 switch (genimg_get_format(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001186 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001187 if (!image_check_hcrc(hdr))
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001188 goto next_sector;
1189
Stephen Warren712fbcf2011-10-18 11:11:49 +00001190 printf("Legacy Image at %08lX:\n", (ulong)hdr);
1191 image_print_contents(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001192
Stephen Warren712fbcf2011-10-18 11:11:49 +00001193 puts(" Verifying Checksum ... ");
1194 if (!image_check_dcrc(hdr)) {
1195 puts("Bad Data CRC\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001196 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001197 puts("OK\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001198 }
1199 break;
1200#if defined(CONFIG_FIT)
1201 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001202 if (!fit_check_format(hdr))
Marian Balakowicze32fea62008-03-11 12:35:20 +01001203 goto next_sector;
1204
Stephen Warren712fbcf2011-10-18 11:11:49 +00001205 printf("FIT Image at %08lX:\n", (ulong)hdr);
1206 fit_print_contents(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001207 break;
1208#endif
1209 default:
wdenk27b207f2003-07-24 23:38:38 +00001210 goto next_sector;
wdenk5bb226e2003-11-17 21:14:37 +00001211 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001212
wdenkbdccc4f2003-08-05 17:43:17 +00001213next_sector: ;
wdenk27b207f2003-07-24 23:38:38 +00001214 }
wdenkbdccc4f2003-08-05 17:43:17 +00001215next_bank: ;
wdenk27b207f2003-07-24 23:38:38 +00001216 }
Vipin Kumar8fdf1e02012-12-16 22:32:48 +00001217 return 0;
1218}
1219#endif
1220
1221#if defined(CONFIG_CMD_IMLS_NAND)
1222static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
1223 size_t len)
1224{
1225 void *imgdata;
1226 int ret;
1227
1228 imgdata = malloc(len);
1229 if (!imgdata) {
1230 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
1231 nand_dev, off);
1232 printf(" Low memory(cannot allocate memory for image)\n");
1233 return -ENOMEM;
1234 }
1235
1236 ret = nand_read_skip_bad(nand, off, &len,
1237 imgdata);
1238 if (ret < 0 && ret != -EUCLEAN) {
1239 free(imgdata);
1240 return ret;
1241 }
1242
1243 if (!image_check_hcrc(imgdata)) {
1244 free(imgdata);
1245 return 0;
1246 }
1247
1248 printf("Legacy Image at NAND device %d offset %08llX:\n",
1249 nand_dev, off);
1250 image_print_contents(imgdata);
1251
1252 puts(" Verifying Checksum ... ");
1253 if (!image_check_dcrc(imgdata))
1254 puts("Bad Data CRC\n");
1255 else
1256 puts("OK\n");
1257
1258 free(imgdata);
1259
1260 return 0;
1261}
1262
1263static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
1264 size_t len)
1265{
1266 void *imgdata;
1267 int ret;
1268
1269 imgdata = malloc(len);
1270 if (!imgdata) {
1271 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
1272 nand_dev, off);
1273 printf(" Low memory(cannot allocate memory for image)\n");
1274 return -ENOMEM;
1275 }
1276
1277 ret = nand_read_skip_bad(nand, off, &len,
1278 imgdata);
1279 if (ret < 0 && ret != -EUCLEAN) {
1280 free(imgdata);
1281 return ret;
1282 }
1283
1284 if (!fit_check_format(imgdata)) {
1285 free(imgdata);
1286 return 0;
1287 }
1288
1289 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
1290
1291 fit_print_contents(imgdata);
1292 free(imgdata);
1293
1294 return 0;
1295}
1296
1297static int do_imls_nand(void)
1298{
1299 nand_info_t *nand;
1300 int nand_dev = nand_curr_device;
1301 size_t len;
1302 loff_t off;
1303 u32 buffer[16];
1304
1305 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
1306 puts("\nNo NAND devices available\n");
1307 return -ENODEV;
1308 }
1309
1310 printf("\n");
1311
1312 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
1313 nand = &nand_info[nand_dev];
1314 if (!nand->name || !nand->size)
1315 continue;
1316
1317 for (off = 0; off < nand->size; off += nand->erasesize) {
1318 const image_header_t *header;
1319 int ret;
1320
1321 if (nand_block_isbad(nand, off))
1322 continue;
1323
1324 len = sizeof(buffer);
1325
1326 ret = nand_read(nand, off, &len, (u8 *)buffer);
1327 if (ret < 0 && ret != -EUCLEAN) {
1328 printf("NAND read error %d at offset %08llX\n",
1329 ret, off);
1330 continue;
1331 }
1332
1333 switch (genimg_get_format(buffer)) {
1334 case IMAGE_FORMAT_LEGACY:
1335 header = (const image_header_t *)buffer;
1336
1337 len = image_get_image_size(header);
1338 nand_imls_legacyimage(nand, nand_dev, off, len);
1339 break;
1340#if defined(CONFIG_FIT)
1341 case IMAGE_FORMAT_FIT:
1342 len = fit_get_size(buffer);
1343 nand_imls_fitimage(nand, nand_dev, off, len);
1344 break;
1345#endif
1346 }
1347 }
1348 }
1349
1350 return 0;
1351}
1352#endif
1353
1354#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
1355static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1356{
1357 int ret_nor = 0, ret_nand = 0;
1358
1359#if defined(CONFIG_CMD_IMLS)
1360 ret_nor = do_imls_nor();
1361#endif
1362
1363#if defined(CONFIG_CMD_IMLS_NAND)
1364 ret_nand = do_imls_nand();
1365#endif
1366
1367 if (ret_nor)
1368 return ret_nor;
1369
1370 if (ret_nand)
1371 return ret_nand;
wdenk27b207f2003-07-24 23:38:38 +00001372
1373 return (0);
1374}
1375
1376U_BOOT_CMD(
1377 imls, 1, 1, do_imls,
Peter Tyser2fb26042009-01-27 18:03:12 -06001378 "list all images found in flash",
wdenk27b207f2003-07-24 23:38:38 +00001379 "\n"
Vipin Kumar8fdf1e02012-12-16 22:32:48 +00001380 " - Prints information about all images found at sector/block\n"
1381 " boundaries in nor/nand flash."
wdenk27b207f2003-07-24 23:38:38 +00001382);
Jon Loeliger90253172007-07-10 11:02:44 -05001383#endif
wdenk27b207f2003-07-24 23:38:38 +00001384
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001385/*******************************************************************/
Marian Balakowicz5cf746c2008-01-31 13:59:09 +01001386/* helper routines */
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001387/*******************************************************************/
Joe Hershbergerbe2e5a02013-02-27 10:20:59 +00001388#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
Doug Andersona558ad72012-01-17 09:37:41 +00001389
1390#define CONSOLE_ARG "console="
1391#define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
1392
Doug Anderson3a8653b2011-10-19 12:30:57 +00001393static void fixup_silent_linux(void)
wdenk47d1a6e2002-11-03 00:01:44 +00001394{
Doug Andersona558ad72012-01-17 09:37:41 +00001395 char *buf;
1396 const char *env_val;
Doug Anderson3a8653b2011-10-19 12:30:57 +00001397 char *cmdline = getenv("bootargs");
Simon Glass8d51aac2013-07-16 20:10:00 -07001398 int want_silent;
wdenk47d1a6e2002-11-03 00:01:44 +00001399
Simon Glass8d51aac2013-07-16 20:10:00 -07001400 /*
1401 * Only fix cmdline when requested. The environment variable can be:
1402 *
1403 * no - we never fixup
1404 * yes - we always fixup
1405 * unset - we rely on the console silent flag
1406 */
1407 want_silent = getenv_yesno("silent_linux");
1408 if (want_silent == 0)
1409 return;
1410 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
wdenk47d1a6e2002-11-03 00:01:44 +00001411 return;
1412
Doug Anderson3a8653b2011-10-19 12:30:57 +00001413 debug("before silent fix-up: %s\n", cmdline);
Doug Andersona558ad72012-01-17 09:37:41 +00001414 if (cmdline && (cmdline[0] != '\0')) {
1415 char *start = strstr(cmdline, CONSOLE_ARG);
1416
1417 /* Allocate space for maximum possible new command line */
1418 buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1);
1419 if (!buf) {
1420 debug("%s: out of memory\n", __func__);
1421 return;
wdenk47d1a6e2002-11-03 00:01:44 +00001422 }
Doug Andersona558ad72012-01-17 09:37:41 +00001423
1424 if (start) {
1425 char *end = strchr(start, ' ');
1426 int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN;
1427
1428 strncpy(buf, cmdline, num_start_bytes);
1429 if (end)
1430 strcpy(buf + num_start_bytes, end);
1431 else
1432 buf[num_start_bytes] = '\0';
1433 } else {
1434 sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
1435 }
1436 env_val = buf;
wdenk47d1a6e2002-11-03 00:01:44 +00001437 } else {
Doug Andersona558ad72012-01-17 09:37:41 +00001438 buf = NULL;
1439 env_val = CONSOLE_ARG;
wdenk47d1a6e2002-11-03 00:01:44 +00001440 }
1441
Doug Andersona558ad72012-01-17 09:37:41 +00001442 setenv("bootargs", env_val);
1443 debug("after silent fix-up: %s\n", env_val);
1444 free(buf);
wdenk47d1a6e2002-11-03 00:01:44 +00001445}
1446#endif /* CONFIG_SILENT_CONSOLE */
1447
Steven Stallioneeaef5e2013-06-10 01:00:09 -07001448#if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
1449static void copy_args(char *dest, int argc, char * const argv[], char delim)
1450{
1451 int i;
1452
1453 for (i = 0; i < argc; i++) {
1454 if (i > 0)
1455 *dest++ = delim;
1456 strcpy(dest, argv[i]);
1457 dest += strlen(argv[i]);
1458 }
1459}
1460#endif
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001461
1462/*******************************************************************/
1463/* OS booting routines */
1464/*******************************************************************/
1465
Kumar Galab1d0db12008-10-21 17:25:47 -05001466#ifdef CONFIG_BOOTM_NETBSD
Stephen Warren712fbcf2011-10-18 11:11:49 +00001467static int do_bootm_netbsd(int flag, int argc, char * const argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001468 bootm_headers_t *images)
wdenk47d1a6e2002-11-03 00:01:44 +00001469{
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001470 void (*loader)(bd_t *, image_header_t *, char *, char *);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001471 image_header_t *os_hdr, *hdr;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +01001472 ulong kernel_data, kernel_len;
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001473 char *consdev;
1474 char *cmdline;
wdenk47d1a6e2002-11-03 00:01:44 +00001475
Miao Yan5b629312013-11-04 11:18:14 +08001476 if (flag != BOOTM_STATE_OS_GO)
Simon Glass7af26b12013-07-10 23:08:09 -07001477 return 0;
Kumar Gala49c3a862008-10-21 17:25:45 -05001478
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001479#if defined(CONFIG_FIT)
1480 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001481 fit_unsupported_reset("NetBSD");
Kumar Gala40d7e992008-08-15 08:24:45 -05001482 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001483 }
1484#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001485 hdr = images->legacy_hdr_os;
wdenk47d1a6e2002-11-03 00:01:44 +00001486
1487 /*
1488 * Booting a (NetBSD) kernel image
1489 *
1490 * This process is pretty similar to a standalone application:
1491 * The (first part of an multi-) image must be a stage-2 loader,
1492 * which in turn is responsible for loading & invoking the actual
1493 * kernel. The only differences are the parameters being passed:
1494 * besides the board info strucure, the loader expects a command
1495 * line, the name of the console device, and (optionally) the
1496 * address of the original image header.
1497 */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001498 os_hdr = NULL;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001499 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1500 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +01001501 if (kernel_len)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001502 os_hdr = hdr;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +01001503 }
wdenk47d1a6e2002-11-03 00:01:44 +00001504
1505 consdev = "";
Stephen Warren712fbcf2011-10-18 11:11:49 +00001506#if defined(CONFIG_8xx_CONS_SMC1)
wdenk47d1a6e2002-11-03 00:01:44 +00001507 consdev = "smc1";
Stephen Warren712fbcf2011-10-18 11:11:49 +00001508#elif defined(CONFIG_8xx_CONS_SMC2)
wdenk47d1a6e2002-11-03 00:01:44 +00001509 consdev = "smc2";
Stephen Warren712fbcf2011-10-18 11:11:49 +00001510#elif defined(CONFIG_8xx_CONS_SCC2)
wdenk47d1a6e2002-11-03 00:01:44 +00001511 consdev = "scc2";
Stephen Warren712fbcf2011-10-18 11:11:49 +00001512#elif defined(CONFIG_8xx_CONS_SCC3)
wdenk47d1a6e2002-11-03 00:01:44 +00001513 consdev = "scc3";
1514#endif
1515
Simon Glass983c72f2013-06-11 11:14:46 -07001516 if (argc > 0) {
wdenk47d1a6e2002-11-03 00:01:44 +00001517 ulong len;
1518 int i;
1519
Simon Glass983c72f2013-06-11 11:14:46 -07001520 for (i = 0, len = 0; i < argc; i += 1)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001521 len += strlen(argv[i]) + 1;
1522 cmdline = malloc(len);
Steven Stallioneeaef5e2013-06-10 01:00:09 -07001523 copy_args(cmdline, argc, argv, ' ');
Stephen Warren712fbcf2011-10-18 11:11:49 +00001524 } else if ((cmdline = getenv("bootargs")) == NULL) {
wdenk47d1a6e2002-11-03 00:01:44 +00001525 cmdline = "";
1526 }
1527
Kumar Galac160a952008-08-15 08:24:36 -05001528 loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
wdenk47d1a6e2002-11-03 00:01:44 +00001529
Stephen Warren712fbcf2011-10-18 11:11:49 +00001530 printf("## Transferring control to NetBSD stage-2 loader "
1531 "(at address %08lx) ...\n",
wdenk47d1a6e2002-11-03 00:01:44 +00001532 (ulong)loader);
1533
Simon Glass770605e2012-02-13 13:51:18 +00001534 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
wdenk47d1a6e2002-11-03 00:01:44 +00001535
1536 /*
1537 * NetBSD Stage-2 Loader Parameters:
Kees Jongenburgere2ce8162013-12-12 16:18:35 +01001538 * arg[0]: pointer to board info data
1539 * arg[1]: image load address
1540 * arg[2]: char pointer to the console device to use
1541 * arg[3]: char pointer to the boot arguments
wdenk47d1a6e2002-11-03 00:01:44 +00001542 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001543 (*loader)(gd->bd, os_hdr, consdev, cmdline);
Kumar Gala40d7e992008-08-15 08:24:45 -05001544
1545 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001546}
Kumar Galab1d0db12008-10-21 17:25:47 -05001547#endif /* CONFIG_BOOTM_NETBSD*/
wdenk47d1a6e2002-11-03 00:01:44 +00001548
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001549#ifdef CONFIG_LYNXKDI
Stephen Warren712fbcf2011-10-18 11:11:49 +00001550static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001551 bootm_headers_t *images)
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001552{
Marian Balakowiczcb1c4892008-04-11 11:07:49 +02001553 image_header_t *hdr = &images->legacy_hdr_os_copy;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001554
Miao Yan5b629312013-11-04 11:18:14 +08001555 if (flag != BOOTM_STATE_OS_GO)
Simon Glass7af26b12013-07-10 23:08:09 -07001556 return 0;
Kumar Gala49c3a862008-10-21 17:25:45 -05001557
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001558#if defined(CONFIG_FIT)
1559 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001560 fit_unsupported_reset("Lynx");
Kumar Gala40d7e992008-08-15 08:24:45 -05001561 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001562 }
1563#endif
1564
Stephen Warren712fbcf2011-10-18 11:11:49 +00001565 lynxkdi_boot((image_header_t *)hdr);
Kumar Gala40d7e992008-08-15 08:24:45 -05001566
1567 return 1;
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001568}
1569#endif /* CONFIG_LYNXKDI */
1570
Kumar Galab1d0db12008-10-21 17:25:47 -05001571#ifdef CONFIG_BOOTM_RTEMS
Stephen Warren712fbcf2011-10-18 11:11:49 +00001572static int do_bootm_rtems(int flag, int argc, char * const argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001573 bootm_headers_t *images)
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001574{
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001575 void (*entry_point)(bd_t *);
wdenkd791b1d2003-04-20 14:04:18 +00001576
Miao Yan5b629312013-11-04 11:18:14 +08001577 if (flag != BOOTM_STATE_OS_GO)
Simon Glass7af26b12013-07-10 23:08:09 -07001578 return 0;
Kumar Gala49c3a862008-10-21 17:25:45 -05001579
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001580#if defined(CONFIG_FIT)
1581 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001582 fit_unsupported_reset("RTEMS");
Kumar Gala40d7e992008-08-15 08:24:45 -05001583 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001584 }
1585#endif
1586
Kumar Galac160a952008-08-15 08:24:36 -05001587 entry_point = (void (*)(bd_t *))images->ep;
wdenkd791b1d2003-04-20 14:04:18 +00001588
Stephen Warren712fbcf2011-10-18 11:11:49 +00001589 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
wdenkd791b1d2003-04-20 14:04:18 +00001590 (ulong)entry_point);
1591
Simon Glass770605e2012-02-13 13:51:18 +00001592 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
wdenkd791b1d2003-04-20 14:04:18 +00001593
1594 /*
1595 * RTEMS Parameters:
1596 * r3: ptr to board info data
1597 */
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001598 (*entry_point)(gd->bd);
Kumar Gala40d7e992008-08-15 08:24:45 -05001599
1600 return 1;
wdenkd791b1d2003-04-20 14:04:18 +00001601}
Kumar Galab1d0db12008-10-21 17:25:47 -05001602#endif /* CONFIG_BOOTM_RTEMS */
wdenkd791b1d2003-04-20 14:04:18 +00001603
Torkel Lundgren3df61952010-09-28 11:05:36 +02001604#if defined(CONFIG_BOOTM_OSE)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001605static int do_bootm_ose(int flag, int argc, char * const argv[],
Torkel Lundgren3df61952010-09-28 11:05:36 +02001606 bootm_headers_t *images)
1607{
1608 void (*entry_point)(void);
1609
Miao Yan5b629312013-11-04 11:18:14 +08001610 if (flag != BOOTM_STATE_OS_GO)
Simon Glass7af26b12013-07-10 23:08:09 -07001611 return 0;
Torkel Lundgren3df61952010-09-28 11:05:36 +02001612
1613#if defined(CONFIG_FIT)
1614 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001615 fit_unsupported_reset("OSE");
Torkel Lundgren3df61952010-09-28 11:05:36 +02001616 return 1;
1617 }
1618#endif
1619
1620 entry_point = (void (*)(void))images->ep;
1621
Stephen Warren712fbcf2011-10-18 11:11:49 +00001622 printf("## Transferring control to OSE (at address %08lx) ...\n",
Torkel Lundgren3df61952010-09-28 11:05:36 +02001623 (ulong)entry_point);
1624
Simon Glass770605e2012-02-13 13:51:18 +00001625 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Torkel Lundgren3df61952010-09-28 11:05:36 +02001626
1627 /*
1628 * OSE Parameters:
1629 * None
1630 */
1631 (*entry_point)();
1632
1633 return 1;
1634}
1635#endif /* CONFIG_BOOTM_OSE */
1636
Steven Stallion04d41402013-03-20 06:31:35 +00001637#if defined(CONFIG_BOOTM_PLAN9)
1638static int do_bootm_plan9(int flag, int argc, char * const argv[],
1639 bootm_headers_t *images)
1640{
1641 void (*entry_point)(void);
Steven Stallioneeaef5e2013-06-10 01:00:09 -07001642 char *s;
Steven Stallion04d41402013-03-20 06:31:35 +00001643
Miao Yan5b629312013-11-04 11:18:14 +08001644 if (flag != BOOTM_STATE_OS_GO)
Simon Glass7af26b12013-07-10 23:08:09 -07001645 return 0;
Steven Stallion04d41402013-03-20 06:31:35 +00001646
1647#if defined(CONFIG_FIT)
1648 if (!images->legacy_hdr_valid) {
1649 fit_unsupported_reset("Plan 9");
1650 return 1;
1651 }
1652#endif
1653
Steven Stallioneeaef5e2013-06-10 01:00:09 -07001654 /* See README.plan9 */
1655 s = getenv("confaddr");
1656 if (s != NULL) {
1657 char *confaddr = (char *)simple_strtoul(s, NULL, 16);
1658
1659 if (argc > 0) {
1660 copy_args(confaddr, argc, argv, '\n');
1661 } else {
1662 s = getenv("bootargs");
1663 if (s != NULL)
1664 strcpy(confaddr, s);
1665 }
1666 }
1667
Steven Stallion04d41402013-03-20 06:31:35 +00001668 entry_point = (void (*)(void))images->ep;
1669
1670 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
1671 (ulong)entry_point);
1672
1673 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1674
1675 /*
1676 * Plan 9 Parameters:
1677 * None
1678 */
1679 (*entry_point)();
1680
1681 return 1;
1682}
1683#endif /* CONFIG_BOOTM_PLAN9 */
1684
Miao Yan17ab52f2013-11-28 17:51:36 +08001685#if defined(CONFIG_BOOTM_VXWORKS)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001686static int do_bootm_vxworks(int flag, int argc, char * const argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001687 bootm_headers_t *images)
wdenk47d1a6e2002-11-03 00:01:44 +00001688{
wdenk47d1a6e2002-11-03 00:01:44 +00001689 char str[80];
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001690
Miao Yan5b629312013-11-04 11:18:14 +08001691 if (flag != BOOTM_STATE_OS_GO)
Simon Glass7af26b12013-07-10 23:08:09 -07001692 return 0;
Kumar Gala49c3a862008-10-21 17:25:45 -05001693
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001694#if defined(CONFIG_FIT)
Marian Balakowiczcb1c4892008-04-11 11:07:49 +02001695 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001696 fit_unsupported_reset("VxWorks");
Kumar Gala40d7e992008-08-15 08:24:45 -05001697 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001698 }
1699#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001700
Kumar Galac160a952008-08-15 08:24:36 -05001701 sprintf(str, "%lx", images->ep); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001702 setenv("loadaddr", str);
Miao Yan17ab52f2013-11-28 17:51:36 +08001703
1704#if defined(CONFIG_CMD_ELF)
Kumar Gala40d7e992008-08-15 08:24:45 -05001705 do_bootvx(NULL, 0, 0, NULL);
Miao Yan17ab52f2013-11-28 17:51:36 +08001706#endif
Kumar Gala40d7e992008-08-15 08:24:45 -05001707
1708 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001709}
Miao Yan17ab52f2013-11-28 17:51:36 +08001710#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001711
Miao Yan17ab52f2013-11-28 17:51:36 +08001712#if defined(CONFIG_CMD_ELF)
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001713static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001714 bootm_headers_t *images)
wdenk47d1a6e2002-11-03 00:01:44 +00001715{
wdenk47d1a6e2002-11-03 00:01:44 +00001716 char *local_args[2];
1717 char str[16];
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001718
Miao Yan5b629312013-11-04 11:18:14 +08001719 if (flag != BOOTM_STATE_OS_GO)
Simon Glass7af26b12013-07-10 23:08:09 -07001720 return 0;
Kumar Gala49c3a862008-10-21 17:25:45 -05001721
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001722#if defined(CONFIG_FIT)
1723 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001724 fit_unsupported_reset("QNX");
Kumar Gala40d7e992008-08-15 08:24:45 -05001725 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001726 }
1727#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001728
Kumar Galac160a952008-08-15 08:24:36 -05001729 sprintf(str, "%lx", images->ep); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001730 local_args[0] = argv[0];
1731 local_args[1] = str; /* and provide it via the arguments */
Kumar Gala40d7e992008-08-15 08:24:45 -05001732 do_bootelf(NULL, 0, 2, local_args);
1733
1734 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001735}
Jon Loeliger90253172007-07-10 11:02:44 -05001736#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -05001737
1738#ifdef CONFIG_INTEGRITY
Stephen Warren712fbcf2011-10-18 11:11:49 +00001739static int do_bootm_integrity(int flag, int argc, char * const argv[],
Peter Tyserf5ed9e32008-09-08 14:56:49 -05001740 bootm_headers_t *images)
1741{
1742 void (*entry_point)(void);
1743
Miao Yan5b629312013-11-04 11:18:14 +08001744 if (flag != BOOTM_STATE_OS_GO)
Simon Glass7af26b12013-07-10 23:08:09 -07001745 return 0;
Kumar Gala49c3a862008-10-21 17:25:45 -05001746
Peter Tyserf5ed9e32008-09-08 14:56:49 -05001747#if defined(CONFIG_FIT)
1748 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001749 fit_unsupported_reset("INTEGRITY");
Peter Tyserf5ed9e32008-09-08 14:56:49 -05001750 return 1;
1751 }
1752#endif
1753
1754 entry_point = (void (*)(void))images->ep;
1755
Stephen Warren712fbcf2011-10-18 11:11:49 +00001756 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
Peter Tyserf5ed9e32008-09-08 14:56:49 -05001757 (ulong)entry_point);
1758
Simon Glass770605e2012-02-13 13:51:18 +00001759 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Peter Tyserf5ed9e32008-09-08 14:56:49 -05001760
1761 /*
1762 * INTEGRITY Parameters:
1763 * None
1764 */
1765 (*entry_point)();
1766
1767 return 1;
1768}
1769#endif
Marek Vasut44f074c2012-03-14 21:52:45 +00001770
1771#ifdef CONFIG_CMD_BOOTZ
1772
Simon Glassa5266d62013-07-04 13:26:10 -07001773int __weak bootz_setup(ulong image, ulong *start, ulong *end)
Marek Vasut44f074c2012-03-14 21:52:45 +00001774{
1775 /* Please define bootz_setup() for your platform */
1776
1777 puts("Your platform's zImage format isn't supported yet!\n");
1778 return -1;
1779}
Marek Vasut44f074c2012-03-14 21:52:45 +00001780
1781/*
1782 * zImage booting support
1783 */
1784static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
1785 char * const argv[], bootm_headers_t *images)
1786{
1787 int ret;
Simon Glassa5266d62013-07-04 13:26:10 -07001788 ulong zi_start, zi_end;
Marek Vasut44f074c2012-03-14 21:52:45 +00001789
Simon Glass35fc84f2013-06-11 11:14:47 -07001790 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
1791 images, 1);
Marek Vasut44f074c2012-03-14 21:52:45 +00001792
1793 /* Setup Linux kernel zImage entry point */
Tom Rini2b9599e2013-07-09 15:32:34 -04001794 if (!argc) {
Marek Vasut44f074c2012-03-14 21:52:45 +00001795 images->ep = load_addr;
1796 debug("* kernel: default image load address = 0x%08lx\n",
1797 load_addr);
1798 } else {
Tom Rini2b9599e2013-07-09 15:32:34 -04001799 images->ep = simple_strtoul(argv[0], NULL, 16);
Marek Vasut44f074c2012-03-14 21:52:45 +00001800 debug("* kernel: cmdline image address = 0x%08lx\n",
1801 images->ep);
1802 }
1803
Simon Glassa5266d62013-07-04 13:26:10 -07001804 ret = bootz_setup(images->ep, &zi_start, &zi_end);
Marek Vasut44f074c2012-03-14 21:52:45 +00001805 if (ret != 0)
1806 return 1;
1807
1808 lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
1809
Tom Rini225fd8c2013-07-09 15:33:25 -04001810 /*
1811 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
1812 * have a header that provide this informaiton.
1813 */
Tom Rini2b9599e2013-07-09 15:32:34 -04001814 if (bootm_find_ramdisk(flag, argc, argv))
1815 return 1;
Marek Vasut44f074c2012-03-14 21:52:45 +00001816
Tom Rini2b9599e2013-07-09 15:32:34 -04001817#if defined(CONFIG_OF_LIBFDT)
1818 if (bootm_find_fdt(flag, argc, argv))
1819 return 1;
1820#endif
1821
1822 return 0;
Marek Vasut44f074c2012-03-14 21:52:45 +00001823}
1824
Rob Herringda620222012-12-02 21:00:23 -06001825int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Marek Vasut44f074c2012-03-14 21:52:45 +00001826{
Simon Glass35fc84f2013-06-11 11:14:47 -07001827 int ret;
Marek Vasut44f074c2012-03-14 21:52:45 +00001828
Tom Rini2b9599e2013-07-09 15:32:34 -04001829 /* Consume 'bootz' */
1830 argc--; argv++;
1831
Marek Vasut44f074c2012-03-14 21:52:45 +00001832 if (bootz_start(cmdtp, flag, argc, argv, &images))
1833 return 1;
1834
Simon Glass385501d2013-07-04 13:26:08 -07001835 /*
1836 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
1837 * disable interrupts ourselves
1838 */
1839 bootm_disable_interrupts();
1840
Simon Glassfb1b1392013-07-04 13:26:11 -07001841 images.os.os = IH_OS_LINUX;
Simon Glass35fc84f2013-06-11 11:14:47 -07001842 ret = do_bootm_states(cmdtp, flag, argc, argv,
Simon Glassfb1b1392013-07-04 13:26:11 -07001843 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
1844 BOOTM_STATE_OS_GO,
Simon Glassd0ae31e2013-06-11 11:14:48 -07001845 &images, 1);
Marek Vasut44f074c2012-03-14 21:52:45 +00001846
Simon Glass35fc84f2013-06-11 11:14:47 -07001847 return ret;
Marek Vasut44f074c2012-03-14 21:52:45 +00001848}
1849
Kim Phillips088f1b12012-10-29 13:34:31 +00001850#ifdef CONFIG_SYS_LONGHELP
1851static char bootz_help_text[] =
Marek Vasut017e1f32012-03-18 11:47:58 +00001852 "[addr [initrd[:size]] [fdt]]\n"
1853 " - boot Linux zImage stored in memory\n"
Marek Vasut44f074c2012-03-14 21:52:45 +00001854 "\tThe argument 'initrd' is optional and specifies the address\n"
Marek Vasut017e1f32012-03-18 11:47:58 +00001855 "\tof the initrd in memory. The optional argument ':size' allows\n"
1856 "\tspecifying the size of RAW initrd.\n"
Marek Vasut44f074c2012-03-14 21:52:45 +00001857#if defined(CONFIG_OF_LIBFDT)
1858 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
1859 "\ta third argument is required which is the address of the\n"
1860 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1861 "\tuse a '-' for the second argument. If you do not pass a third\n"
1862 "\ta bd_info struct will be passed instead\n"
1863#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00001864 "";
1865#endif
1866
1867U_BOOT_CMD(
1868 bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
1869 "boot Linux zImage image from memory", bootz_help_text
Marek Vasut44f074c2012-03-14 21:52:45 +00001870);
1871#endif /* CONFIG_CMD_BOOTZ */