blob: a59ee95a698c014abe2a3ce7ce1cf16e98086088 [file] [log] [blame]
wdenk47d1a6e2002-11-03 00:01:44 +00001/*
Detlev Zundel77094472009-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 Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk47d1a6e2002-11-03 00:01:44 +00006 */
7
Marian Balakowicz41d71ed2008-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-VILLARD6bb94492009-04-04 12:49:11 +020017#include <u-boot/zlib.h>
wdenk710e3502003-08-29 20:57:53 +000018#include <bzlib.h>
wdenk0893c472003-05-20 14:25:27 +000019#include <environment.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060020#include <lmb.h>
Kumar Gala18178bc2008-10-21 17:25:45 -050021#include <linux/ctype.h>
wdenk47d1a6e2002-11-03 00:01:44 +000022#include <asm/byteorder.h>
Simon Glass49648062013-05-07 06:12:03 +000023#include <asm/io.h>
Anatolij Gustschin18e73d92011-11-19 13:12:18 +000024#include <linux/compiler.h>
wdenk57b2d802003-06-27 21:31:46 +000025
Miao Yan1bd54562013-11-28 17:51:38 +080026#if defined(CONFIG_BOOTM_VXWORKS) && \
27 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
28#include <vxworks.h>
29#endif
30
Stefan Roesea629a722008-07-30 09:59:51 +020031#if defined(CONFIG_CMD_USB)
Markus Klotzbücher09197cb2008-07-10 14:47:09 +020032#include <usb.h>
33#endif
34
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020035#ifdef CONFIG_SYS_HUSH_PARSER
wdenk47d1a6e2002-11-03 00:01:44 +000036#include <hush.h>
37#endif
38
Kumar Gala9f4e7e42008-08-15 08:24:39 -050039#if defined(CONFIG_OF_LIBFDT)
Kumar Gala9f4e7e42008-08-15 08:24:39 -050040#include <libfdt.h>
41#include <fdt_support.h>
42#endif
43
Luigi 'Comio' Mantellini35afc062008-09-08 02:46:13 +020044#ifdef CONFIG_LZMA
Luigi 'Comio' Mantellini35afc062008-09-08 02:46:13 +020045#include <lzma/LzmaTypes.h>
Luigi 'Comio' Mantellinid02bd742009-07-21 10:45:49 +020046#include <lzma/LzmaDec.h>
Luigi 'Comio' Mantellini35afc062008-09-08 02:46:13 +020047#include <lzma/LzmaTools.h>
48#endif /* CONFIG_LZMA */
49
Peter Korsgaardf7440cd2009-11-19 11:37:51 +010050#ifdef CONFIG_LZO
51#include <linux/lzo.h>
52#endif /* CONFIG_LZO */
53
Marian Balakowicz2df645e2008-01-08 18:17:10 +010054DECLARE_GLOBAL_DATA_PTR;
wdenk9dfa8d12002-12-08 09:53:23 +000055
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +020056#ifndef CONFIG_SYS_BOOTM_LEN
57#define CONFIG_SYS_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
wdenk381669a2003-06-16 23:50:08 +000058#endif
59
Marian Balakowiczbe946ba2008-01-08 18:11:43 +010060#ifdef CONFIG_BZIP2
61extern void bz_internal_error(int);
wdenk47d1a6e2002-11-03 00:01:44 +000062#endif
63
Jon Loeliger54324d02007-07-08 17:51:39 -050064#if defined(CONFIG_CMD_IMI)
Stephen Warren6904b6e2011-10-18 11:11:49 +000065static int image_info(unsigned long addr);
wdenk47d1a6e2002-11-03 00:01:44 +000066#endif
wdenk874ac262003-07-24 23:38:38 +000067
Jon Loeliger54324d02007-07-08 17:51:39 -050068#if defined(CONFIG_CMD_IMLS)
wdenk874ac262003-07-24 23:38:38 +000069#include <flash.h>
Stefan Roesefb9a7302010-08-31 10:00:10 +020070#include <mtd/cfi_flash.h>
Marian Balakowicz513b4a12005-10-11 19:09:42 +020071extern flash_info_t flash_info[]; /* info for FLASH chips */
Vipin Kumar3df41b12012-12-16 22:32:48 +000072#endif
73
74#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
Stephen Warren6904b6e2011-10-18 11:11:49 +000075static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
wdenk874ac262003-07-24 23:38:38 +000076#endif
77
Vipin Kumar3df41b12012-12-16 22:32:48 +000078#include <linux/err.h>
79#include <nand.h>
80
Joe Hershberger01c2d982013-02-27 10:20:59 +000081#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
Stephen Warren6904b6e2011-10-18 11:11:49 +000082static void fixup_silent_linux(void);
Marian Balakowicz2df645e2008-01-08 18:17:10 +010083#endif
wdenk47d1a6e2002-11-03 00:01:44 +000084
Simon Glassf7104ce2013-12-26 16:26:24 -070085static int do_bootm_standalone(int flag, int argc, char * const argv[],
86 bootm_headers_t *images);
87
Simon Glass49648062013-05-07 06:12:03 +000088static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
Stephen Warren6904b6e2011-10-18 11:11:49 +000089 char * const argv[], bootm_headers_t *images,
90 ulong *os_data, ulong *os_len);
wdenk47d1a6e2002-11-03 00:01:44 +000091
92/*
93 * Continue booting an OS image; caller already has:
94 * - copied image header to global variable `header'
95 * - checked header magic number, checksums (both header & image),
96 * - verified image architecture (PPC) and type (KERNEL or MULTI),
97 * - loaded (first part of) image to header load address,
98 * - disabled interrupts.
Simon Glass794a9212013-06-11 11:14:46 -070099 *
Simon Glass3081e2e2013-06-11 11:14:47 -0700100 * @flag: Flags indicating what to do (BOOTM_STATE_...)
Simon Glass794a9212013-06-11 11:14:46 -0700101 * @argc: Number of arguments. Note that the arguments are shifted down
102 * so that 0 is the first argument not processed by U-Boot, and
103 * argc is adjusted accordingly. This avoids confusion as to how
104 * many arguments are available for the OS.
105 * @images: Pointers to os/initrd/fdt
106 * @return 1 on error. On success the OS boots so this function does
107 * not return.
wdenk47d1a6e2002-11-03 00:01:44 +0000108 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000109typedef int boot_os_fn(int flag, int argc, char * const argv[],
Simon Glass794a9212013-06-11 11:14:46 -0700110 bootm_headers_t *images);
wdenk47d1a6e2002-11-03 00:01:44 +0000111
Kumar Gala697675e2008-10-21 17:25:47 -0500112#ifdef CONFIG_BOOTM_LINUX
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100113extern boot_os_fn do_bootm_linux;
Kumar Gala697675e2008-10-21 17:25:47 -0500114#endif
115#ifdef CONFIG_BOOTM_NETBSD
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100116static boot_os_fn do_bootm_netbsd;
Kumar Gala697675e2008-10-21 17:25:47 -0500117#endif
Marian Balakowiczb4a12a92008-01-08 18:12:17 +0100118#if defined(CONFIG_LYNXKDI)
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100119static boot_os_fn do_bootm_lynxkdi;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000120extern void lynxkdi_boot(image_header_t *);
wdenk47d1a6e2002-11-03 00:01:44 +0000121#endif
Kumar Gala697675e2008-10-21 17:25:47 -0500122#ifdef CONFIG_BOOTM_RTEMS
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100123static boot_os_fn do_bootm_rtems;
Kumar Gala697675e2008-10-21 17:25:47 -0500124#endif
Torkel Lundgren29a58662010-09-28 11:05:36 +0200125#if defined(CONFIG_BOOTM_OSE)
126static boot_os_fn do_bootm_ose;
127#endif
Steven Stallion36ec8ac2013-03-20 06:31:35 +0000128#if defined(CONFIG_BOOTM_PLAN9)
129static boot_os_fn do_bootm_plan9;
130#endif
Miao Yan1bd54562013-11-28 17:51:38 +0800131#if defined(CONFIG_BOOTM_VXWORKS) && \
132 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100133static boot_os_fn do_bootm_vxworks;
Miao Yanf8ba19f2013-11-28 17:51:36 +0800134#endif
135#if defined(CONFIG_CMD_ELF)
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100136static boot_os_fn do_bootm_qnxelf;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000137int do_bootvx(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
138int do_bootelf(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
Jon Loeligerd704d912007-07-10 11:02:44 -0500139#endif
Peter Tyser56b8dd12008-09-08 14:56:49 -0500140#if defined(CONFIG_INTEGRITY)
141static boot_os_fn do_bootm_integrity;
142#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000143
Mike Frysinger6c1616a2009-11-03 11:36:26 -0500144static boot_os_fn *boot_os[] = {
Simon Glassf7104ce2013-12-26 16:26:24 -0700145 [IH_OS_U_BOOT] = do_bootm_standalone,
Kumar Gala697675e2008-10-21 17:25:47 -0500146#ifdef CONFIG_BOOTM_LINUX
Kumar Galabf3954f2008-10-21 17:25:44 -0500147 [IH_OS_LINUX] = do_bootm_linux,
Kumar Gala697675e2008-10-21 17:25:47 -0500148#endif
149#ifdef CONFIG_BOOTM_NETBSD
Kumar Galabf3954f2008-10-21 17:25:44 -0500150 [IH_OS_NETBSD] = do_bootm_netbsd,
Kumar Gala697675e2008-10-21 17:25:47 -0500151#endif
Kumar Galabf3954f2008-10-21 17:25:44 -0500152#ifdef CONFIG_LYNXKDI
153 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
154#endif
Kumar Gala697675e2008-10-21 17:25:47 -0500155#ifdef CONFIG_BOOTM_RTEMS
Kumar Galabf3954f2008-10-21 17:25:44 -0500156 [IH_OS_RTEMS] = do_bootm_rtems,
Kumar Gala697675e2008-10-21 17:25:47 -0500157#endif
Torkel Lundgren29a58662010-09-28 11:05:36 +0200158#if defined(CONFIG_BOOTM_OSE)
159 [IH_OS_OSE] = do_bootm_ose,
160#endif
Steven Stallion36ec8ac2013-03-20 06:31:35 +0000161#if defined(CONFIG_BOOTM_PLAN9)
162 [IH_OS_PLAN9] = do_bootm_plan9,
163#endif
Miao Yan1bd54562013-11-28 17:51:38 +0800164#if defined(CONFIG_BOOTM_VXWORKS) && \
165 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
Kumar Galabf3954f2008-10-21 17:25:44 -0500166 [IH_OS_VXWORKS] = do_bootm_vxworks,
Miao Yanf8ba19f2013-11-28 17:51:36 +0800167#endif
168#if defined(CONFIG_CMD_ELF)
Kumar Galabf3954f2008-10-21 17:25:44 -0500169 [IH_OS_QNX] = do_bootm_qnxelf,
170#endif
171#ifdef CONFIG_INTEGRITY
172 [IH_OS_INTEGRITY] = do_bootm_integrity,
173#endif
174};
175
Simon Schwarz6519bbf2011-09-02 00:50:31 +0000176bootm_headers_t images; /* pointers to os/initrd/fdt images */
Stefan Roese5d5ce292006-03-13 11:16:36 +0100177
Kumar Gala5e726692009-08-27 08:23:55 -0500178/* Allow for arch specific config before we boot */
Kim Phillipsdc00a682012-10-29 13:34:31 +0000179static void __arch_preboot_os(void)
Kumar Gala5e726692009-08-27 08:23:55 -0500180{
181 /* please define platform specific arch_preboot_os() */
182}
183void arch_preboot_os(void) __attribute__((weak, alias("__arch_preboot_os")));
184
Mike Frysinger5da0bed2011-10-03 14:50:33 +0000185#define IH_INITRD_ARCH IH_ARCH_DEFAULT
wdenk47d1a6e2002-11-03 00:01:44 +0000186
Mike Frysingera0dadf82009-11-03 11:35:59 -0500187#ifdef CONFIG_LMB
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000188static void boot_start_lmb(bootm_headers_t *images)
189{
Becky Bruced26d67c2008-06-09 20:37:18 -0500190 ulong mem_start;
191 phys_size_t mem_size;
wdenk47d1a6e2002-11-03 00:01:44 +0000192
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000193 lmb_init(&images->lmb);
wdenk47d1a6e2002-11-03 00:01:44 +0000194
Kumar Galac5bacfd2008-02-27 21:51:50 -0600195 mem_start = getenv_bootm_low();
196 mem_size = getenv_bootm_size();
wdenk47d1a6e2002-11-03 00:01:44 +0000197
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000198 lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size);
wdenk47d1a6e2002-11-03 00:01:44 +0000199
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000200 arch_lmb_reserve(&images->lmb);
201 board_lmb_reserve(&images->lmb);
202}
Mike Frysingera0dadf82009-11-03 11:35:59 -0500203#else
Marek Vasutac15cad2012-04-01 03:59:46 +0000204#define lmb_reserve(lmb, base, size)
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000205static inline void boot_start_lmb(bootm_headers_t *images) { }
Mike Frysingera0dadf82009-11-03 11:35:59 -0500206#endif
Mike Frysingera0dadf82009-11-03 11:35:59 -0500207
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200208static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Mike Frysingera0dadf82009-11-03 11:35:59 -0500209{
Stephen Warren6904b6e2011-10-18 11:11:49 +0000210 memset((void *)&images, 0, sizeof(images));
211 images.verify = getenv_yesno("verify");
Mike Frysingera0dadf82009-11-03 11:35:59 -0500212
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000213 boot_start_lmb(&images);
wdenk47d1a6e2002-11-03 00:01:44 +0000214
Simon Glass768cbf02011-12-10 11:08:06 +0000215 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
Simon Glass3081e2e2013-06-11 11:14:47 -0700216 images.state = BOOTM_STATE_START;
217
218 return 0;
219}
220
221static int bootm_find_os(cmd_tbl_t *cmdtp, int flag, int argc,
222 char * const argv[])
223{
224 const void *os_hdr;
Simon Glass768cbf02011-12-10 11:08:06 +0000225
Marian Balakowiczc40032c2008-01-31 13:59:09 +0100226 /* get kernel image header, start address and length */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000227 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
Kumar Galabd70bbe2008-08-15 08:24:41 -0500228 &images, &images.os.image_start, &images.os.image_len);
229 if (images.os.image_len == 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000230 puts("ERROR: can't get kernel image!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000231 return 1;
232 }
Wolfgang Denk63e44ff2005-10-06 01:50:50 +0200233
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100234 /* get image parameters */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000235 switch (genimg_get_format(os_hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100236 case IMAGE_FORMAT_LEGACY:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000237 images.os.type = image_get_type(os_hdr);
238 images.os.comp = image_get_comp(os_hdr);
239 images.os.os = image_get_os(os_hdr);
Wolfgang Denk63e44ff2005-10-06 01:50:50 +0200240
Stephen Warren6904b6e2011-10-18 11:11:49 +0000241 images.os.end = image_get_image_end(os_hdr);
242 images.os.load = image_get_load(os_hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100243 break;
244#if defined(CONFIG_FIT)
245 case IMAGE_FORMAT_FIT:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000246 if (fit_image_get_type(images.fit_hdr_os,
Kumar Galabd70bbe2008-08-15 08:24:41 -0500247 images.fit_noffset_os, &images.os.type)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000248 puts("Can't get image type!\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000249 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +0100250 return 1;
251 }
wdenk47d1a6e2002-11-03 00:01:44 +0000252
Stephen Warren6904b6e2011-10-18 11:11:49 +0000253 if (fit_image_get_comp(images.fit_hdr_os,
Kumar Galabd70bbe2008-08-15 08:24:41 -0500254 images.fit_noffset_os, &images.os.comp)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000255 puts("Can't get image compression!\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000256 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
wdenk47d1a6e2002-11-03 00:01:44 +0000257 return 1;
258 }
wdenk47d1a6e2002-11-03 00:01:44 +0000259
Stephen Warren6904b6e2011-10-18 11:11:49 +0000260 if (fit_image_get_os(images.fit_hdr_os,
Kumar Galabd70bbe2008-08-15 08:24:41 -0500261 images.fit_noffset_os, &images.os.os)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000262 puts("Can't get image OS!\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000263 bootstage_error(BOOTSTAGE_ID_FIT_OS);
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +0100264 return 1;
265 }
wdenk47d1a6e2002-11-03 00:01:44 +0000266
Stephen Warren6904b6e2011-10-18 11:11:49 +0000267 images.os.end = fit_get_end(images.fit_hdr_os);
wdenk47d1a6e2002-11-03 00:01:44 +0000268
Stephen Warren6904b6e2011-10-18 11:11:49 +0000269 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
Kumar Galabd70bbe2008-08-15 08:24:41 -0500270 &images.os.load)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000271 puts("Can't get image load address!\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000272 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +0100273 return 1;
wdenk655a0f92003-10-30 21:49:38 +0000274 }
275 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100276#endif
277 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000278 puts("ERROR: unknown image format type!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000279 return 1;
280 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100281
Kumar Gala93467bc2008-08-15 08:24:36 -0500282 /* find kernel entry point */
283 if (images.legacy_hdr_valid) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000284 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
Kumar Gala93467bc2008-08-15 08:24:36 -0500285#if defined(CONFIG_FIT)
286 } else if (images.fit_uname_os) {
Simon Glass3081e2e2013-06-11 11:14:47 -0700287 int ret;
288
Stephen Warren6904b6e2011-10-18 11:11:49 +0000289 ret = fit_image_get_entry(images.fit_hdr_os,
Simon Glass50ad2212013-05-08 08:06:02 +0000290 images.fit_noffset_os, &images.ep);
Kumar Gala93467bc2008-08-15 08:24:36 -0500291 if (ret) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000292 puts("Can't get entry point property!\n");
Kumar Gala93467bc2008-08-15 08:24:36 -0500293 return 1;
294 }
295#endif
296 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000297 puts("Could not find kernel entry point!\n");
Kumar Gala93467bc2008-08-15 08:24:36 -0500298 return 1;
299 }
300
Stephen Warren8c87eba2011-11-10 13:17:53 -0700301 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
302 images.os.load = images.os.image_start;
303 images.ep += images.os.load;
304 }
305
Simon Glass3081e2e2013-06-11 11:14:47 -0700306 images.os.start = (ulong)os_hdr;
307
308 return 0;
309}
310
Tom Rini1ab61472013-07-09 15:33:25 -0400311static int bootm_find_ramdisk(int flag, int argc, char * const argv[])
Simon Glass3081e2e2013-06-11 11:14:47 -0700312{
313 int ret;
314
Tom Rini1ab61472013-07-09 15:33:25 -0400315 /* find ramdisk */
316 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
317 &images.rd_start, &images.rd_end);
318 if (ret) {
319 puts("Ramdisk image is corrupt or invalid\n");
320 return 1;
321 }
322
323 return 0;
324}
325
326#if defined(CONFIG_OF_LIBFDT)
327static int bootm_find_fdt(int flag, int argc, char * const argv[])
328{
329 int ret;
330
331 /* find flattened device tree */
332 ret = boot_get_fdt(flag, argc, argv, IH_ARCH_DEFAULT, &images,
333 &images.ft_addr, &images.ft_len);
334 if (ret) {
335 puts("Could not find a valid device tree\n");
336 return 1;
337 }
338
339 set_working_fdt_addr(images.ft_addr);
340
341 return 0;
342}
343#endif
344
345static int bootm_find_other(cmd_tbl_t *cmdtp, int flag, int argc,
346 char * const argv[])
347{
Heiko Schocher64ccc832010-03-29 13:15:48 +0200348 if (((images.os.type == IH_TYPE_KERNEL) ||
Stephen Warren8c87eba2011-11-10 13:17:53 -0700349 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
Heiko Schocher64ccc832010-03-29 13:15:48 +0200350 (images.os.type == IH_TYPE_MULTI)) &&
Miao Yan1bd54562013-11-28 17:51:38 +0800351 (images.os.os == IH_OS_LINUX ||
352 images.os.os == IH_OS_VXWORKS)) {
Tom Rini1ab61472013-07-09 15:33:25 -0400353 if (bootm_find_ramdisk(flag, argc, argv))
Kumar Galafffb1432008-08-15 08:24:37 -0500354 return 1;
Kumar Galaa5130ad2008-08-15 08:24:38 -0500355
356#if defined(CONFIG_OF_LIBFDT)
Tom Rini1ab61472013-07-09 15:33:25 -0400357 if (bootm_find_fdt(flag, argc, argv))
Kumar Galaa5130ad2008-08-15 08:24:38 -0500358 return 1;
Kumar Galaa5130ad2008-08-15 08:24:38 -0500359#endif
Kumar Galafffb1432008-08-15 08:24:37 -0500360 }
361
Kumar Galabd70bbe2008-08-15 08:24:41 -0500362 return 0;
363}
Markus Klotzbücher09197cb2008-07-10 14:47:09 +0200364
Kumar Galabd70bbe2008-08-15 08:24:41 -0500365#define BOOTM_ERR_RESET -1
366#define BOOTM_ERR_OVERLAP -2
367#define BOOTM_ERR_UNIMPLEMENTED -3
Tom Rinifec27172013-06-28 11:38:02 -0400368static int bootm_load_os(bootm_headers_t *images, unsigned long *load_end,
369 int boot_progress)
Kumar Galabd70bbe2008-08-15 08:24:41 -0500370{
Tom Rinifec27172013-06-28 11:38:02 -0400371 image_info_t os = images->os;
Kumar Galabd70bbe2008-08-15 08:24:41 -0500372 uint8_t comp = os.comp;
373 ulong load = os.load;
374 ulong blob_start = os.start;
375 ulong blob_end = os.end;
376 ulong image_start = os.image_start;
377 ulong image_len = os.image_len;
Anatolij Gustschin18e73d92011-11-19 13:12:18 +0000378 __maybe_unused uint unc_len = CONFIG_SYS_BOOTM_LEN;
Stephen Warrenf66131d2011-11-10 13:17:54 -0700379 int no_overlap = 0;
Simon Glass758409c2013-05-16 13:53:24 +0000380 void *load_buf, *image_buf;
Matthias Weisserc8bb9632010-08-05 13:17:30 +0200381#if defined(CONFIG_LZMA) || defined(CONFIG_LZO)
382 int ret;
383#endif /* defined(CONFIG_LZMA) || defined(CONFIG_LZO) */
Markus Klotzbücher09197cb2008-07-10 14:47:09 +0200384
Stephen Warren6904b6e2011-10-18 11:11:49 +0000385 const char *type_name = genimg_get_type_name(os.type);
wdenk452cfd62002-11-19 11:04:11 +0000386
Kees Cook81003da2013-08-16 07:59:16 -0700387 load_buf = map_sysmem(load, unc_len);
Simon Glass758409c2013-05-16 13:53:24 +0000388 image_buf = map_sysmem(image_start, image_len);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100389 switch (comp) {
wdenk47d1a6e2002-11-03 00:01:44 +0000390 case IH_COMP_NONE:
Lei Wen8790b9e2011-01-10 18:21:15 +0800391 if (load == blob_start || load == image_start) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000392 printf(" XIP %s ... ", type_name);
Stephen Warrenf66131d2011-11-10 13:17:54 -0700393 no_overlap = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000394 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000395 printf(" Loading %s ... ", type_name);
Simon Glass758409c2013-05-16 13:53:24 +0000396 memmove_wd(load_buf, image_buf, image_len, CHUNKSZ);
wdenk47d1a6e2002-11-03 00:01:44 +0000397 }
Kumar Galabd70bbe2008-08-15 08:24:41 -0500398 *load_end = load + image_len;
wdenk47d1a6e2002-11-03 00:01:44 +0000399 break;
Mike Frysingerab427ae2010-01-21 19:30:36 -0500400#ifdef CONFIG_GZIP
wdenk47d1a6e2002-11-03 00:01:44 +0000401 case IH_COMP_GZIP:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000402 printf(" Uncompressing %s ... ", type_name);
Simon Glass758409c2013-05-16 13:53:24 +0000403 if (gunzip(load_buf, unc_len, image_buf, &image_len) != 0) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000404 puts("GUNZIP: uncompress, out-of-mem or overwrite "
405 "error - must RESET board to recover\n");
Kumar Galabd70bbe2008-08-15 08:24:41 -0500406 if (boot_progress)
Simon Glass0169e6b2012-02-13 13:51:18 +0000407 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
Kumar Galabd70bbe2008-08-15 08:24:41 -0500408 return BOOTM_ERR_RESET;
wdenk47d1a6e2002-11-03 00:01:44 +0000409 }
Marian Balakowiczf92332b2008-01-31 13:20:06 +0100410
Kumar Galabd70bbe2008-08-15 08:24:41 -0500411 *load_end = load + image_len;
wdenk47d1a6e2002-11-03 00:01:44 +0000412 break;
Mike Frysingerab427ae2010-01-21 19:30:36 -0500413#endif /* CONFIG_GZIP */
wdenk710e3502003-08-29 20:57:53 +0000414#ifdef CONFIG_BZIP2
415 case IH_COMP_BZIP2:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000416 printf(" Uncompressing %s ... ", type_name);
wdenk2cefd152004-02-08 22:55:38 +0000417 /*
418 * If we've got less than 4 MB of malloc() space,
419 * use slower decompression algorithm which requires
420 * at most 2300 KB of memory.
421 */
Simon Glass758409c2013-05-16 13:53:24 +0000422 int i = BZ2_bzBuffToBuffDecompress(load_buf, &unc_len,
423 image_buf, image_len,
424 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
wdenk710e3502003-08-29 20:57:53 +0000425 if (i != BZ_OK) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000426 printf("BUNZIP2: uncompress or overwrite error %d "
Marian Balakowiczb5324a82008-03-12 10:33:01 +0100427 "- must RESET board to recover\n", i);
Kumar Galabd70bbe2008-08-15 08:24:41 -0500428 if (boot_progress)
Simon Glass0169e6b2012-02-13 13:51:18 +0000429 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
Kumar Galabd70bbe2008-08-15 08:24:41 -0500430 return BOOTM_ERR_RESET;
wdenk710e3502003-08-29 20:57:53 +0000431 }
Marian Balakowiczf92332b2008-01-31 13:20:06 +0100432
Kumar Galabd70bbe2008-08-15 08:24:41 -0500433 *load_end = load + unc_len;
wdenk710e3502003-08-29 20:57:53 +0000434 break;
435#endif /* CONFIG_BZIP2 */
Luigi 'Comio' Mantellini35afc062008-09-08 02:46:13 +0200436#ifdef CONFIG_LZMA
Mike Frysingerb41ad242010-07-25 15:54:17 -0400437 case IH_COMP_LZMA: {
438 SizeT lzma_len = unc_len;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000439 printf(" Uncompressing %s ... ", type_name);
Luigi 'Comio' Mantellini35afc062008-09-08 02:46:13 +0200440
Simon Glass758409c2013-05-16 13:53:24 +0000441 ret = lzmaBuffToBuffDecompress(load_buf, &lzma_len,
442 image_buf, image_len);
Mike Frysingerb41ad242010-07-25 15:54:17 -0400443 unc_len = lzma_len;
Luigi 'Comio' Mantellinid02bd742009-07-21 10:45:49 +0200444 if (ret != SZ_OK) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000445 printf("LZMA: uncompress or overwrite error %d "
Luigi 'Comio' Mantellini35afc062008-09-08 02:46:13 +0200446 "- must RESET board to recover\n", ret);
Simon Glass0169e6b2012-02-13 13:51:18 +0000447 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
Luigi 'Comio' Mantellini35afc062008-09-08 02:46:13 +0200448 return BOOTM_ERR_RESET;
449 }
450 *load_end = load + unc_len;
451 break;
Mike Frysingerb41ad242010-07-25 15:54:17 -0400452 }
Luigi 'Comio' Mantellini35afc062008-09-08 02:46:13 +0200453#endif /* CONFIG_LZMA */
Peter Korsgaardf7440cd2009-11-19 11:37:51 +0100454#ifdef CONFIG_LZO
Simon Glass208d8e82013-08-30 11:00:09 -0600455 case IH_COMP_LZO: {
456 size_t size;
457
Stephen Warren6904b6e2011-10-18 11:11:49 +0000458 printf(" Uncompressing %s ... ", type_name);
Peter Korsgaardf7440cd2009-11-19 11:37:51 +0100459
Simon Glass208d8e82013-08-30 11:00:09 -0600460 ret = lzop_decompress(image_buf, image_len, load_buf, &size);
Peter Korsgaardf7440cd2009-11-19 11:37:51 +0100461 if (ret != LZO_E_OK) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000462 printf("LZO: uncompress or overwrite error %d "
Peter Korsgaardf7440cd2009-11-19 11:37:51 +0100463 "- must RESET board to recover\n", ret);
464 if (boot_progress)
Simon Glass0169e6b2012-02-13 13:51:18 +0000465 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
Peter Korsgaardf7440cd2009-11-19 11:37:51 +0100466 return BOOTM_ERR_RESET;
467 }
468
Simon Glass208d8e82013-08-30 11:00:09 -0600469 *load_end = load + size;
Peter Korsgaardf7440cd2009-11-19 11:37:51 +0100470 break;
Simon Glass208d8e82013-08-30 11:00:09 -0600471 }
Peter Korsgaardf7440cd2009-11-19 11:37:51 +0100472#endif /* CONFIG_LZO */
wdenk47d1a6e2002-11-03 00:01:44 +0000473 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000474 printf("Unimplemented compression type %d\n", comp);
Kumar Galabd70bbe2008-08-15 08:24:41 -0500475 return BOOTM_ERR_UNIMPLEMENTED;
wdenk47d1a6e2002-11-03 00:01:44 +0000476 }
Diana CRACIUN4b2fbe42011-08-31 02:45:23 +0000477
478 flush_cache(load, (*load_end - load) * sizeof(ulong));
479
Stephen Warren6904b6e2011-10-18 11:11:49 +0000480 puts("OK\n");
481 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
Simon Glass0169e6b2012-02-13 13:51:18 +0000482 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
wdenk47d1a6e2002-11-03 00:01:44 +0000483
Stephen Warrenf66131d2011-11-10 13:17:54 -0700484 if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000485 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
486 blob_start, blob_end);
487 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
488 *load_end);
wdenk47d1a6e2002-11-03 00:01:44 +0000489
Tom Rinifec27172013-06-28 11:38:02 -0400490 /* Check what type of image this is. */
491 if (images->legacy_hdr_valid) {
492 if (image_get_type(&images->legacy_hdr_os_copy)
493 == IH_TYPE_MULTI)
494 puts("WARNING: legacy format multi component image overwritten\n");
495 return BOOTM_ERR_OVERLAP;
496 } else {
497 puts("ERROR: new format image overwritten - must RESET the board to recover\n");
498 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
499 return BOOTM_ERR_RESET;
500 }
Kumar Galabd70bbe2008-08-15 08:24:41 -0500501 }
502
503 return 0;
504}
505
Simon Glassf7104ce2013-12-26 16:26:24 -0700506static int do_bootm_standalone(int flag, int argc, char * const argv[],
507 bootm_headers_t *images)
Detlev Zundelea32b5e2009-07-13 16:01:19 +0200508{
509 char *s;
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200510 int (*appl)(int, char * const []);
Detlev Zundelea32b5e2009-07-13 16:01:19 +0200511
512 /* Don't start if "autostart" is set to "no" */
513 if (((s = getenv("autostart")) != NULL) && (strcmp(s, "no") == 0)) {
Simon Glassf7104ce2013-12-26 16:26:24 -0700514 setenv_hex("filesize", images->os.image_len);
Detlev Zundelea32b5e2009-07-13 16:01:19 +0200515 return 0;
516 }
Simon Glassf7104ce2013-12-26 16:26:24 -0700517 appl = (int (*)(int, char * const []))(ulong)ntohl(images->ep);
Simon Glass794a9212013-06-11 11:14:46 -0700518 (*appl)(argc, argv);
Detlev Zundelea32b5e2009-07-13 16:01:19 +0200519 return 0;
520}
521
Kumar Gala18178bc2008-10-21 17:25:45 -0500522/* we overload the cmd field with our state machine info instead of a
523 * function pointer */
Frans Meulenbroeks6b419fb2010-02-25 10:12:13 +0100524static cmd_tbl_t cmd_bootm_sub[] = {
Kumar Gala18178bc2008-10-21 17:25:45 -0500525 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
526 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
John Rigbyeea8e692010-10-13 13:57:35 -0600527#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Kumar Gala18178bc2008-10-21 17:25:45 -0500528 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
529#endif
530#ifdef CONFIG_OF_LIBFDT
531 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
532#endif
Kumar Gala18178bc2008-10-21 17:25:45 -0500533 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
Peter Tyser694a3932009-11-18 19:08:59 -0600534 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
Kumar Gala18178bc2008-10-21 17:25:45 -0500535 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
Simon Glass00134882013-06-11 11:14:48 -0700536 U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
Kumar Gala18178bc2008-10-21 17:25:45 -0500537 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
538};
539
Simon Glass3081e2e2013-06-11 11:14:47 -0700540static int boot_selected_os(int argc, char * const argv[], int state,
Simon Glass8f6e6232013-06-28 00:46:12 -0700541 bootm_headers_t *images, boot_os_fn *boot_fn)
Simon Glass3081e2e2013-06-11 11:14:47 -0700542{
Simon Glass3081e2e2013-06-11 11:14:47 -0700543 arch_preboot_os();
544 boot_fn(state, argc, argv, images);
Simon Glassf7104ce2013-12-26 16:26:24 -0700545
546 /* Stand-alone may return when 'autostart' is 'no' */
547 if (images->os.type == IH_TYPE_STANDALONE ||
548 state == BOOTM_STATE_OS_FAKE_GO) /* We expect to return */
Simon Glass00134882013-06-11 11:14:48 -0700549 return 0;
Simon Glass3081e2e2013-06-11 11:14:47 -0700550 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
551#ifdef DEBUG
552 puts("\n## Control returned to monitor - resetting...\n");
553#endif
554 return BOOTM_ERR_RESET;
555}
556
557/**
Simon Glass917f6842013-07-04 13:26:08 -0700558 * bootm_disable_interrupts() - Disable interrupts in preparation for load/boot
559 *
560 * @return interrupt flag (0 if interrupts were disabled, non-zero if they were
561 * enabled)
562 */
563static ulong bootm_disable_interrupts(void)
564{
565 ulong iflag;
566
567 /*
568 * We have reached the point of no return: we are going to
569 * overwrite all exception vector code, so we cannot easily
570 * recover from any failures any more...
571 */
572 iflag = disable_interrupts();
573#ifdef CONFIG_NETCONSOLE
574 /* Stop the ethernet stack if NetConsole could have left it up */
575 eth_halt();
Frederic Leroyd7ea47c2013-09-10 12:02:31 +0200576 eth_unregister(eth_get_dev());
Simon Glass917f6842013-07-04 13:26:08 -0700577#endif
578
579#if defined(CONFIG_CMD_USB)
580 /*
581 * turn off USB to prevent the host controller from writing to the
582 * SDRAM while Linux is booting. This could happen (at least for OHCI
583 * controller), because the HCCA (Host Controller Communication Area)
584 * lies within the SDRAM and the host controller writes continously to
585 * this area (as busmaster!). The HccaFrameNumber is for example
586 * updated every 1 ms within the HCCA structure in SDRAM! For more
587 * details see the OpenHCI specification.
588 */
589 usb_stop();
590#endif
591 return iflag;
592}
593
594/**
Simon Glass3081e2e2013-06-11 11:14:47 -0700595 * Execute selected states of the bootm command.
596 *
597 * Note the arguments to this state must be the first argument, Any 'bootm'
598 * or sub-command arguments must have already been taken.
599 *
600 * Note that if states contains more than one flag it MUST contain
601 * BOOTM_STATE_START, since this handles and consumes the command line args.
602 *
Tom Rinifec27172013-06-28 11:38:02 -0400603 * Also note that aside from boot_os_fn functions and bootm_load_os no other
604 * functions we store the return value of in 'ret' may use a negative return
605 * value, without special handling.
606 *
Simon Glass3081e2e2013-06-11 11:14:47 -0700607 * @param cmdtp Pointer to bootm command table entry
608 * @param flag Command flags (CMD_FLAG_...)
609 * @param argc Number of subcommand arguments (0 = no arguments)
610 * @param argv Arguments
611 * @param states Mask containing states to run (BOOTM_STATE_...)
612 * @param images Image header information
613 * @param boot_progress 1 to show boot progress, 0 to not do this
614 * @return 0 if ok, something else on error. Some errors will cause this
615 * function to perform a reboot! If states contains BOOTM_STATE_OS_GO
616 * then the intent is to boot an OS, so this function will not return
617 * unless the image type is standalone.
618 */
619static int do_bootm_states(cmd_tbl_t *cmdtp, int flag, int argc,
620 char * const argv[], int states, bootm_headers_t *images,
621 int boot_progress)
622{
623 boot_os_fn *boot_fn;
624 ulong iflag = 0;
Simon Glassfde683c2013-07-04 13:26:09 -0700625 int ret = 0, need_boot_fn;
Simon Glass3081e2e2013-06-11 11:14:47 -0700626
627 images->state |= states;
628
629 /*
630 * Work through the states and see how far we get. We stop on
631 * any error.
632 */
633 if (states & BOOTM_STATE_START)
634 ret = bootm_start(cmdtp, flag, argc, argv);
635
636 if (!ret && (states & BOOTM_STATE_FINDOS))
637 ret = bootm_find_os(cmdtp, flag, argc, argv);
638
639 if (!ret && (states & BOOTM_STATE_FINDOTHER)) {
640 ret = bootm_find_other(cmdtp, flag, argc, argv);
641 argc = 0; /* consume the args */
642 }
643
644 /* Load the OS */
645 if (!ret && (states & BOOTM_STATE_LOADOS)) {
646 ulong load_end;
647
Simon Glass917f6842013-07-04 13:26:08 -0700648 iflag = bootm_disable_interrupts();
Tom Rinifec27172013-06-28 11:38:02 -0400649 ret = bootm_load_os(images, &load_end, 0);
Tom Rinifec27172013-06-28 11:38:02 -0400650 if (ret == 0)
Simon Glass3081e2e2013-06-11 11:14:47 -0700651 lmb_reserve(&images->lmb, images->os.load,
652 (load_end - images->os.load));
Tom Rinia8961782013-07-11 09:35:23 -0400653 else if (ret && ret != BOOTM_ERR_OVERLAP)
654 goto err;
Tom Rinifec27172013-06-28 11:38:02 -0400655 else if (ret == BOOTM_ERR_OVERLAP)
656 ret = 0;
Paul B. Henson63f95252013-08-03 21:29:09 -0700657#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
Simon Glassb73b1f32013-07-16 20:09:59 -0700658 if (images->os.os == IH_OS_LINUX)
659 fixup_silent_linux();
660#endif
Simon Glass3081e2e2013-06-11 11:14:47 -0700661 }
662
663 /* Relocate the ramdisk */
664#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
665 if (!ret && (states & BOOTM_STATE_RAMDISK)) {
666 ulong rd_len = images->rd_end - images->rd_start;
667
668 ret = boot_ramdisk_high(&images->lmb, images->rd_start,
669 rd_len, &images->initrd_start, &images->initrd_end);
670 if (!ret) {
671 setenv_hex("initrd_start", images->initrd_start);
672 setenv_hex("initrd_end", images->initrd_end);
673 }
674 }
675#endif
676#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_LMB)
677 if (!ret && (states & BOOTM_STATE_FDT)) {
678 boot_fdt_add_mem_rsv_regions(&images->lmb, images->ft_addr);
679 ret = boot_relocate_fdt(&images->lmb, &images->ft_addr,
680 &images->ft_len);
681 }
682#endif
683
684 /* From now on, we need the OS boot function */
685 if (ret)
686 return ret;
687 boot_fn = boot_os[images->os.os];
Simon Glassfde683c2013-07-04 13:26:09 -0700688 need_boot_fn = states & (BOOTM_STATE_OS_CMDLINE |
689 BOOTM_STATE_OS_BD_T | BOOTM_STATE_OS_PREP |
690 BOOTM_STATE_OS_FAKE_GO | BOOTM_STATE_OS_GO);
691 if (boot_fn == NULL && need_boot_fn) {
Simon Glass3081e2e2013-06-11 11:14:47 -0700692 if (iflag)
693 enable_interrupts();
694 printf("ERROR: booting os '%s' (%d) is not supported\n",
695 genimg_get_os_name(images->os.os), images->os.os);
696 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
697 return 1;
698 }
699
700 /* Call various other states that are not generally used */
701 if (!ret && (states & BOOTM_STATE_OS_CMDLINE))
702 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, images);
703 if (!ret && (states & BOOTM_STATE_OS_BD_T))
704 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, images);
705 if (!ret && (states & BOOTM_STATE_OS_PREP))
706 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, images);
707
Simon Glass00134882013-06-11 11:14:48 -0700708#ifdef CONFIG_TRACE
709 /* Pretend to run the OS, then run a user command */
710 if (!ret && (states & BOOTM_STATE_OS_FAKE_GO)) {
711 char *cmd_list = getenv("fakegocmd");
712
713 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_FAKE_GO,
Simon Glass8f6e6232013-06-28 00:46:12 -0700714 images, boot_fn);
Simon Glass00134882013-06-11 11:14:48 -0700715 if (!ret && cmd_list)
716 ret = run_command_list(cmd_list, -1, flag);
717 }
718#endif
Simon Glassb8567132013-07-10 09:25:33 -0400719
720 /* Check for unsupported subcommand. */
721 if (ret) {
722 puts("subcommand not supported\n");
723 return ret;
724 }
725
Simon Glass3081e2e2013-06-11 11:14:47 -0700726 /* Now run the OS! We hope this doesn't return */
Simon Glassb8567132013-07-10 09:25:33 -0400727 if (!ret && (states & BOOTM_STATE_OS_GO))
Simon Glass3081e2e2013-06-11 11:14:47 -0700728 ret = boot_selected_os(argc, argv, BOOTM_STATE_OS_GO,
Simon Glass8f6e6232013-06-28 00:46:12 -0700729 images, boot_fn);
Simon Glass3081e2e2013-06-11 11:14:47 -0700730
731 /* Deal with any fallout */
Tom Rinifec27172013-06-28 11:38:02 -0400732err:
Simon Glass3081e2e2013-06-11 11:14:47 -0700733 if (iflag)
734 enable_interrupts();
Tom Rinifec27172013-06-28 11:38:02 -0400735
736 if (ret == BOOTM_ERR_UNIMPLEMENTED)
737 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
738 else if (ret == BOOTM_ERR_RESET)
739 do_reset(cmdtp, flag, argc, argv);
Simon Glass3081e2e2013-06-11 11:14:47 -0700740
741 return ret;
742}
743
Kim Phillipsdc00a682012-10-29 13:34:31 +0000744static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
Stephen Warren6904b6e2011-10-18 11:11:49 +0000745 char * const argv[])
Kumar Gala18178bc2008-10-21 17:25:45 -0500746{
747 int ret = 0;
Simon Glass81afc3d2011-10-07 13:53:40 +0000748 long state;
Kumar Gala18178bc2008-10-21 17:25:45 -0500749 cmd_tbl_t *c;
Kumar Gala18178bc2008-10-21 17:25:45 -0500750
Simon Glass794a9212013-06-11 11:14:46 -0700751 c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
752 argc--; argv++;
Kumar Gala18178bc2008-10-21 17:25:45 -0500753
754 if (c) {
Simon Glass81afc3d2011-10-07 13:53:40 +0000755 state = (long)c->cmd;
Simon Glass794a9212013-06-11 11:14:46 -0700756 if (state == BOOTM_STATE_START)
Simon Glass3081e2e2013-06-11 11:14:47 -0700757 state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
Wolfgang Denk3b683112010-07-17 01:06:04 +0200758 } else {
759 /* Unrecognized command */
Simon Glassa06dfc72011-12-10 08:44:01 +0000760 return CMD_RET_USAGE;
Kumar Gala18178bc2008-10-21 17:25:45 -0500761 }
762
Simon Glass3081e2e2013-06-11 11:14:47 -0700763 if (state != BOOTM_STATE_START && images.state >= state) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000764 printf("Trying to execute a command out of order\n");
Simon Glassa06dfc72011-12-10 08:44:01 +0000765 return CMD_RET_USAGE;
Kumar Gala18178bc2008-10-21 17:25:45 -0500766 }
767
Simon Glass3081e2e2013-06-11 11:14:47 -0700768 ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
Kumar Gala18178bc2008-10-21 17:25:45 -0500769
770 return ret;
771}
772
Kumar Galabd70bbe2008-08-15 08:24:41 -0500773/*******************************************************************/
774/* bootm - boot application image from image in memory */
775/*******************************************************************/
Kumar Galabf3954f2008-10-21 17:25:44 -0500776
Stephen Warren6904b6e2011-10-18 11:11:49 +0000777int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Kumar Galabd70bbe2008-08-15 08:24:41 -0500778{
Wolfgang Denkd0813e52010-10-28 20:00:11 +0200779#ifdef CONFIG_NEEDS_MANUAL_RELOC
Peter Tyser9057cbf2009-09-21 11:20:36 -0500780 static int relocated = 0;
Kumar Galabf3954f2008-10-21 17:25:44 -0500781
Kumar Galabf3954f2008-10-21 17:25:44 -0500782 if (!relocated) {
783 int i;
Daniel Schwierzecka2187092013-01-07 06:54:52 +0000784
785 /* relocate boot function table */
Kumar Galabf3954f2008-10-21 17:25:44 -0500786 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
Detlev Zundel77094472009-07-13 16:01:18 +0200787 if (boot_os[i] != NULL)
788 boot_os[i] += gd->reloc_off;
Daniel Schwierzecka2187092013-01-07 06:54:52 +0000789
790 /* relocate names of sub-command table */
791 for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
792 cmd_bootm_sub[i].name += gd->reloc_off;
793
Kumar Galabf3954f2008-10-21 17:25:44 -0500794 relocated = 1;
795 }
Peter Tyser9057cbf2009-09-21 11:20:36 -0500796#endif
Kumar Galabd70bbe2008-08-15 08:24:41 -0500797
Kumar Gala18178bc2008-10-21 17:25:45 -0500798 /* determine if we have a sub command */
Simon Glass794a9212013-06-11 11:14:46 -0700799 argc--; argv++;
800 if (argc > 0) {
Kumar Gala18178bc2008-10-21 17:25:45 -0500801 char *endp;
802
Simon Glass794a9212013-06-11 11:14:46 -0700803 simple_strtoul(argv[0], &endp, 16);
804 /* endp pointing to NULL means that argv[0] was just a
Kumar Gala18178bc2008-10-21 17:25:45 -0500805 * valid number, pass it along to the normal bootm processing
806 *
807 * If endp is ':' or '#' assume a FIT identifier so pass
808 * along for normal processing.
809 *
810 * Right now we assume the first arg should never be '-'
811 */
812 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
813 return do_bootm_subcommand(cmdtp, flag, argc, argv);
814 }
815
Simon Glass3081e2e2013-06-11 11:14:47 -0700816 return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
817 BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
Tom Rini1008dc22013-09-23 14:20:37 -0400818 BOOTM_STATE_LOADOS |
819#if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
820 BOOTM_STATE_OS_CMDLINE |
821#endif
Paul Burton8a39e8d2013-09-06 11:23:55 +0100822 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
823 BOOTM_STATE_OS_GO, &images, 1);
wdenk47d1a6e2002-11-03 00:01:44 +0000824}
825
Mike Frysinger194c2e82011-06-05 13:43:02 +0000826int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
827{
828 const char *ep = getenv("autostart");
829
830 if (ep && !strcmp(ep, "yes")) {
831 char *local_args[2];
832 local_args[0] = (char *)cmd;
833 local_args[1] = NULL;
834 printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
835 return do_bootm(cmdtp, 0, 1, local_args);
836 }
837
838 return 0;
839}
840
Marian Balakowiczc40032c2008-01-31 13:59:09 +0100841/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100842 * image_get_kernel - verify legacy format kernel image
843 * @img_addr: in RAM address of the legacy format image to be verified
844 * @verify: data CRC verification flag
Marian Balakowiczc40032c2008-01-31 13:59:09 +0100845 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100846 * image_get_kernel() verifies legacy image integrity and returns pointer to
847 * legacy image header if image verification was completed successfully.
Marian Balakowiczc40032c2008-01-31 13:59:09 +0100848 *
849 * returns:
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100850 * pointer to a legacy image header if valid image was found
851 * otherwise return NULL
Marian Balakowiczc40032c2008-01-31 13:59:09 +0100852 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000853static image_header_t *image_get_kernel(ulong img_addr, int verify)
wdenk808532a2003-10-10 10:05:42 +0000854{
Marian Balakowicz59504f62008-02-27 11:02:07 +0100855 image_header_t *hdr = (image_header_t *)img_addr;
wdenk808532a2003-10-10 10:05:42 +0000856
Marian Balakowicz59504f62008-02-27 11:02:07 +0100857 if (!image_check_magic(hdr)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000858 puts("Bad Magic Number\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000859 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
Marian Balakowicz59504f62008-02-27 11:02:07 +0100860 return NULL;
861 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000862 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
wdenk808532a2003-10-10 10:05:42 +0000863
Stephen Warren6904b6e2011-10-18 11:11:49 +0000864 if (!image_check_hcrc(hdr)) {
865 puts("Bad Header Checksum\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000866 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
Marian Balakowicz59504f62008-02-27 11:02:07 +0100867 return NULL;
868 }
869
Simon Glass0169e6b2012-02-13 13:51:18 +0000870 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000871 image_print_contents(hdr);
Marian Balakowicz59504f62008-02-27 11:02:07 +0100872
873 if (verify) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000874 puts(" Verifying Checksum ... ");
875 if (!image_check_dcrc(hdr)) {
876 printf("Bad Data CRC\n");
Simon Glass0169e6b2012-02-13 13:51:18 +0000877 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
Marian Balakowicz59504f62008-02-27 11:02:07 +0100878 return NULL;
wdenk808532a2003-10-10 10:05:42 +0000879 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000880 puts("OK\n");
wdenk808532a2003-10-10 10:05:42 +0000881 }
Simon Glass0169e6b2012-02-13 13:51:18 +0000882 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
wdenk808532a2003-10-10 10:05:42 +0000883
Stephen Warren6904b6e2011-10-18 11:11:49 +0000884 if (!image_check_target_arch(hdr)) {
885 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
Simon Glass0169e6b2012-02-13 13:51:18 +0000886 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
Marian Balakowicz59504f62008-02-27 11:02:07 +0100887 return NULL;
888 }
889 return hdr;
wdenk808532a2003-10-10 10:05:42 +0000890}
wdenk808532a2003-10-10 10:05:42 +0000891
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +0100892/**
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100893 * boot_get_kernel - find kernel image
Marian Balakowicz59504f62008-02-27 11:02:07 +0100894 * @os_data: pointer to a ulong variable, will hold os data start address
895 * @os_len: pointer to a ulong variable, will hold os data length
896 *
Marian Balakowiczd7c88a42008-02-29 14:58:34 +0100897 * boot_get_kernel() tries to find a kernel image, verifies its integrity
Marian Balakowicz59504f62008-02-27 11:02:07 +0100898 * and locates kernel data.
899 *
900 * returns:
901 * pointer to image header if valid image was found, plus kernel start
902 * address and length, otherwise NULL
903 */
Simon Glass49648062013-05-07 06:12:03 +0000904static const void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
Stephen Warren6904b6e2011-10-18 11:11:49 +0000905 char * const argv[], bootm_headers_t *images, ulong *os_data,
906 ulong *os_len)
Marian Balakowiczc40032c2008-01-31 13:59:09 +0100907{
908 image_header_t *hdr;
909 ulong img_addr;
Simon Glass49648062013-05-07 06:12:03 +0000910 const void *buf;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100911#if defined(CONFIG_FIT)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100912 const char *fit_uname_config = NULL;
913 const char *fit_uname_kernel = NULL;
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +0100914 int os_noffset;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100915#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000916
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100917 /* find out kernel image address */
Simon Glass794a9212013-06-11 11:14:46 -0700918 if (argc < 1) {
Marian Balakowiczc40032c2008-01-31 13:59:09 +0100919 img_addr = load_addr;
Stephen Warren6904b6e2011-10-18 11:11:49 +0000920 debug("* kernel: default image load address = 0x%08lx\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100921 load_addr);
922#if defined(CONFIG_FIT)
Simon Glass794a9212013-06-11 11:14:46 -0700923 } else if (fit_parse_conf(argv[0], load_addr, &img_addr,
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100924 &fit_uname_config)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000925 debug("* kernel: config '%s' from image at 0x%08lx\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100926 fit_uname_config, img_addr);
Simon Glass794a9212013-06-11 11:14:46 -0700927 } else if (fit_parse_subimage(argv[0], load_addr, &img_addr,
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100928 &fit_uname_kernel)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000929 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100930 fit_uname_kernel, img_addr);
931#endif
Marian Balakowiczc40032c2008-01-31 13:59:09 +0100932 } else {
Simon Glass794a9212013-06-11 11:14:46 -0700933 img_addr = simple_strtoul(argv[0], NULL, 16);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000934 debug("* kernel: cmdline image address = 0x%08lx\n", img_addr);
Marian Balakowiczc40032c2008-01-31 13:59:09 +0100935 }
wdenk56f94be2002-11-05 16:35:14 +0000936
Simon Glass0169e6b2012-02-13 13:51:18 +0000937 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
wdenk47d1a6e2002-11-03 00:01:44 +0000938
Marian Balakowiczc536d6f2008-02-21 17:20:19 +0100939 /* copy from dataflash if needed */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000940 img_addr = genimg_get_image(img_addr);
wdenk47d1a6e2002-11-03 00:01:44 +0000941
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100942 /* check image type, for FIT images get FIT kernel node */
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +0100943 *os_data = *os_len = 0;
Simon Glass49648062013-05-07 06:12:03 +0000944 buf = map_sysmem(img_addr, 0);
945 switch (genimg_get_format(buf)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100946 case IMAGE_FORMAT_LEGACY:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000947 printf("## Booting kernel from Legacy Image at %08lx ...\n",
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +0100948 img_addr);
Stephen Warren6904b6e2011-10-18 11:11:49 +0000949 hdr = image_get_kernel(img_addr, images->verify);
Marian Balakowicz59504f62008-02-27 11:02:07 +0100950 if (!hdr)
Marian Balakowiczc40032c2008-01-31 13:59:09 +0100951 return NULL;
Simon Glass0169e6b2012-02-13 13:51:18 +0000952 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
wdenk47d1a6e2002-11-03 00:01:44 +0000953
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +0100954 /* get os_data and os_len */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000955 switch (image_get_type(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100956 case IH_TYPE_KERNEL:
Stephen Warren8c87eba2011-11-10 13:17:53 -0700957 case IH_TYPE_KERNEL_NOLOAD:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000958 *os_data = image_get_data(hdr);
959 *os_len = image_get_data_size(hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100960 break;
961 case IH_TYPE_MULTI:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000962 image_multi_getimg(hdr, 0, os_data, os_len);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100963 break;
Detlev Zundelea32b5e2009-07-13 16:01:19 +0200964 case IH_TYPE_STANDALONE:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000965 *os_data = image_get_data(hdr);
966 *os_len = image_get_data_size(hdr);
Detlev Zundelea32b5e2009-07-13 16:01:19 +0200967 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100968 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000969 printf("Wrong Image Type for %s command\n",
970 cmdtp->name);
Simon Glass0169e6b2012-02-13 13:51:18 +0000971 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100972 return NULL;
973 }
Marian Balakowiczab00e022008-04-11 11:07:49 +0200974
975 /*
Stephen Warren6904b6e2011-10-18 11:11:49 +0000976 * copy image header to allow for image overwrites during
977 * kernel decompression.
Marian Balakowiczab00e022008-04-11 11:07:49 +0200978 */
Stephen Warren6904b6e2011-10-18 11:11:49 +0000979 memmove(&images->legacy_hdr_os_copy, hdr,
980 sizeof(image_header_t));
Marian Balakowiczab00e022008-04-11 11:07:49 +0200981
982 /* save pointer to image header */
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100983 images->legacy_hdr_os = hdr;
wdenk47d1a6e2002-11-03 00:01:44 +0000984
Marian Balakowiczab00e022008-04-11 11:07:49 +0200985 images->legacy_hdr_valid = 1;
Simon Glass0169e6b2012-02-13 13:51:18 +0000986 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
Marian Balakowiczc40032c2008-01-31 13:59:09 +0100987 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100988#if defined(CONFIG_FIT)
989 case IMAGE_FORMAT_FIT:
Simon Glasse6b127a2013-05-16 13:53:25 +0000990 os_noffset = fit_image_load(images, FIT_KERNEL_PROP,
991 img_addr,
Simon Glassad68fc32013-07-10 23:08:10 -0700992 &fit_uname_kernel, &fit_uname_config,
Simon Glasse6b127a2013-05-16 13:53:25 +0000993 IH_ARCH_DEFAULT, IH_TYPE_KERNEL,
994 BOOTSTAGE_ID_FIT_KERNEL_START,
995 FIT_LOAD_IGNORED, os_data, os_len);
996 if (os_noffset < 0)
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +0100997 return NULL;
wdenk47d1a6e2002-11-03 00:01:44 +0000998
Simon Glasse6b127a2013-05-16 13:53:25 +0000999 images->fit_hdr_os = map_sysmem(img_addr, 0);
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +01001000 images->fit_uname_os = fit_uname_kernel;
Simon Glassad68fc32013-07-10 23:08:10 -07001001 images->fit_uname_cfg = fit_uname_config;
Marian Balakowicz61c1ad52008-03-12 10:32:59 +01001002 images->fit_noffset_os = os_noffset;
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +01001003 break;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001004#endif
Marian Balakowiczc40032c2008-01-31 13:59:09 +01001005 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +00001006 printf("Wrong Image Format for %s command\n", cmdtp->name);
Simon Glass0169e6b2012-02-13 13:51:18 +00001007 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
Marian Balakowiczc40032c2008-01-31 13:59:09 +01001008 return NULL;
1009 }
wdenk47d1a6e2002-11-03 00:01:44 +00001010
Stephen Warren6904b6e2011-10-18 11:11:49 +00001011 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +01001012 *os_data, *os_len, *os_len);
wdenk47d1a6e2002-11-03 00:01:44 +00001013
Simon Glass49648062013-05-07 06:12:03 +00001014 return buf;
Marian Balakowiczc40032c2008-01-31 13:59:09 +01001015}
wdenk47d1a6e2002-11-03 00:01:44 +00001016
Kim Phillipsdc00a682012-10-29 13:34:31 +00001017#ifdef CONFIG_SYS_LONGHELP
1018static char bootm_help_text[] =
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001019 "[addr [arg ...]]\n - boot application image stored in memory\n"
1020 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
1021 "\t'arg' can be the address of an initrd image\n"
Marian Balakowiczd2160852008-01-31 13:20:07 +01001022#if defined(CONFIG_OF_LIBFDT)
wdenk47d1a6e2002-11-03 00:01:44 +00001023 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
1024 "\ta third argument is required which is the address of the\n"
1025 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1026 "\tuse a '-' for the second argument. If you do not pass a third\n"
1027 "\ta bd_info struct will be passed instead\n"
1028#endif
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +01001029#if defined(CONFIG_FIT)
1030 "\t\nFor the new multi component uImage format (FIT) addresses\n"
1031 "\tmust be extened to include component or configuration unit name:\n"
1032 "\taddr:<subimg_uname> - direct component image specification\n"
1033 "\taddr#<conf_uname> - configuration specification\n"
1034 "\tUse iminfo command to get the list of existing component\n"
1035 "\timages and configurations.\n"
1036#endif
Kumar Gala18178bc2008-10-21 17:25:45 -05001037 "\nSub-commands to do part of the bootm sequence. The sub-commands "
1038 "must be\n"
1039 "issued in the order below (it's ok to not issue all sub-commands):\n"
1040 "\tstart [addr [arg ...]]\n"
1041 "\tloados - load OS image\n"
Daniel Schwierzeckf2f2ec92013-02-26 04:54:19 +00001042#if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
Kumar Gala18178bc2008-10-21 17:25:45 -05001043 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
1044#endif
1045#if defined(CONFIG_OF_LIBFDT)
1046 "\tfdt - relocate flat device tree\n"
1047#endif
Kumar Gala18178bc2008-10-21 17:25:45 -05001048 "\tcmdline - OS specific command line processing/setup\n"
Peter Tyser694a3932009-11-18 19:08:59 -06001049 "\tbdt - OS specific bd_t processing\n"
Kumar Gala18178bc2008-10-21 17:25:45 -05001050 "\tprep - OS specific prep before relocation or go\n"
Kim Phillipsdc00a682012-10-29 13:34:31 +00001051 "\tgo - start OS";
1052#endif
1053
1054U_BOOT_CMD(
1055 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
1056 "boot application image from memory", bootm_help_text
wdenk47d1a6e2002-11-03 00:01:44 +00001057);
wdenk47d1a6e2002-11-03 00:01:44 +00001058
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001059/*******************************************************************/
1060/* bootd - boot default image */
1061/*******************************************************************/
Jon Loeliger54324d02007-07-08 17:51:39 -05001062#if defined(CONFIG_CMD_BOOTD)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001063int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk47d1a6e2002-11-03 00:01:44 +00001064{
1065 int rcode = 0;
wdenk47d1a6e2002-11-03 00:01:44 +00001066
Simon Glassbf8c5b02012-02-14 19:59:21 +00001067 if (run_command(getenv("bootcmd"), flag) < 0)
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001068 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001069 return rcode;
1070}
wdenk47d1a6e2002-11-03 00:01:44 +00001071
wdenkf287a242003-07-01 21:06:45 +00001072U_BOOT_CMD(
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001073 boot, 1, 1, do_bootd,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001074 "boot default, i.e., run 'bootcmd'",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001075 ""
wdenk3086a972003-06-28 23:11:04 +00001076);
wdenk47d1a6e2002-11-03 00:01:44 +00001077
wdenk3086a972003-06-28 23:11:04 +00001078/* keep old command name "bootd" for backward compatibility */
wdenkf287a242003-07-01 21:06:45 +00001079U_BOOT_CMD(
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001080 bootd, 1, 1, do_bootd,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001081 "boot default, i.e., run 'bootcmd'",
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001082 ""
wdenk57b2d802003-06-27 21:31:46 +00001083);
wdenk47d1a6e2002-11-03 00:01:44 +00001084
wdenk47d1a6e2002-11-03 00:01:44 +00001085#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001086
wdenk47d1a6e2002-11-03 00:01:44 +00001087
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001088/*******************************************************************/
1089/* iminfo - print header info for a requested image */
1090/*******************************************************************/
Jon Loeliger54324d02007-07-08 17:51:39 -05001091#if defined(CONFIG_CMD_IMI)
Kim Phillipsdc00a682012-10-29 13:34:31 +00001092static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk47d1a6e2002-11-03 00:01:44 +00001093{
1094 int arg;
1095 ulong addr;
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001096 int rcode = 0;
wdenk47d1a6e2002-11-03 00:01:44 +00001097
wdenk47d1a6e2002-11-03 00:01:44 +00001098 if (argc < 2) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001099 return image_info(load_addr);
wdenk47d1a6e2002-11-03 00:01:44 +00001100 }
wdenk47d1a6e2002-11-03 00:01:44 +00001101
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001102 for (arg = 1; arg < argc; ++arg) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001103 addr = simple_strtoul(argv[arg], NULL, 16);
1104 if (image_info(addr) != 0)
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001105 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001106 }
1107 return rcode;
1108}
wdenk47d1a6e2002-11-03 00:01:44 +00001109
Stephen Warren6904b6e2011-10-18 11:11:49 +00001110static int image_info(ulong addr)
wdenk47d1a6e2002-11-03 00:01:44 +00001111{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001112 void *hdr = (void *)addr;
wdenk47d1a6e2002-11-03 00:01:44 +00001113
Stephen Warren6904b6e2011-10-18 11:11:49 +00001114 printf("\n## Checking Image at %08lx ...\n", addr);
wdenk47d1a6e2002-11-03 00:01:44 +00001115
Stephen Warren6904b6e2011-10-18 11:11:49 +00001116 switch (genimg_get_format(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001117 case IMAGE_FORMAT_LEGACY:
Stephen Warren6904b6e2011-10-18 11:11:49 +00001118 puts(" Legacy image found\n");
1119 if (!image_check_magic(hdr)) {
1120 puts(" Bad Magic Number\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001121 return 1;
1122 }
wdenk47d1a6e2002-11-03 00:01:44 +00001123
Stephen Warren6904b6e2011-10-18 11:11:49 +00001124 if (!image_check_hcrc(hdr)) {
1125 puts(" Bad Header Checksum\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001126 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001127 }
1128
Stephen Warren6904b6e2011-10-18 11:11:49 +00001129 image_print_contents(hdr);
wdenk47d1a6e2002-11-03 00:01:44 +00001130
Stephen Warren6904b6e2011-10-18 11:11:49 +00001131 puts(" Verifying Checksum ... ");
1132 if (!image_check_dcrc(hdr)) {
1133 puts(" Bad Data CRC\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001134 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001135 }
Stephen Warren6904b6e2011-10-18 11:11:49 +00001136 puts("OK\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001137 return 0;
1138#if defined(CONFIG_FIT)
1139 case IMAGE_FORMAT_FIT:
Stephen Warren6904b6e2011-10-18 11:11:49 +00001140 puts(" FIT image found\n");
wdenk47d1a6e2002-11-03 00:01:44 +00001141
Stephen Warren6904b6e2011-10-18 11:11:49 +00001142 if (!fit_check_format(hdr)) {
1143 puts("Bad FIT image format!\n");
Marian Balakowiczcc1fd912008-03-11 12:35:20 +01001144 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001145 }
1146
Stephen Warren6904b6e2011-10-18 11:11:49 +00001147 fit_print_contents(hdr);
Bartlomiej Sieka03797f52008-09-09 12:58:16 +02001148
Simon Glass7428ad12013-05-07 06:11:57 +00001149 if (!fit_all_image_verify(hdr)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001150 puts("Bad hash in FIT image!\n");
Bartlomiej Sieka03797f52008-09-09 12:58:16 +02001151 return 1;
1152 }
1153
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001154 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +00001155#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001156 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +00001157 puts("Unknown image format!\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001158 break;
wdenk47d1a6e2002-11-03 00:01:44 +00001159 }
wdenk47d1a6e2002-11-03 00:01:44 +00001160
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001161 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001162}
wdenk47d1a6e2002-11-03 00:01:44 +00001163
wdenkf287a242003-07-01 21:06:45 +00001164U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001165 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001166 "print header information for application image",
wdenk57b2d802003-06-27 21:31:46 +00001167 "addr [addr ...]\n"
1168 " - print header information for application image starting at\n"
1169 " address 'addr' in memory; this includes verification of the\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +02001170 " image contents (magic number, header and payload checksums)"
wdenk57b2d802003-06-27 21:31:46 +00001171);
wdenk47d1a6e2002-11-03 00:01:44 +00001172#endif
1173
1174
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001175/*******************************************************************/
1176/* imls - list all images found in flash */
1177/*******************************************************************/
Jon Loeliger54324d02007-07-08 17:51:39 -05001178#if defined(CONFIG_CMD_IMLS)
Vipin Kumar3df41b12012-12-16 22:32:48 +00001179static int do_imls_nor(void)
wdenk874ac262003-07-24 23:38:38 +00001180{
1181 flash_info_t *info;
1182 int i, j;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001183 void *hdr;
wdenk47d1a6e2002-11-03 00:01:44 +00001184
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001185 for (i = 0, info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +02001186 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
wdenk47d1a6e2002-11-03 00:01:44 +00001187
wdenk874ac262003-07-24 23:38:38 +00001188 if (info->flash_id == FLASH_UNKNOWN)
1189 goto next_bank;
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001190 for (j = 0; j < info->sector_count; ++j) {
wdenk47d1a6e2002-11-03 00:01:44 +00001191
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001192 hdr = (void *)info->start[j];
1193 if (!hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +01001194 goto next_sector;
wdenk47d1a6e2002-11-03 00:01:44 +00001195
Stephen Warren6904b6e2011-10-18 11:11:49 +00001196 switch (genimg_get_format(hdr)) {
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001197 case IMAGE_FORMAT_LEGACY:
Stephen Warren6904b6e2011-10-18 11:11:49 +00001198 if (!image_check_hcrc(hdr))
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001199 goto next_sector;
wdenk47d1a6e2002-11-03 00:01:44 +00001200
Stephen Warren6904b6e2011-10-18 11:11:49 +00001201 printf("Legacy Image at %08lX:\n", (ulong)hdr);
1202 image_print_contents(hdr);
wdenk47d1a6e2002-11-03 00:01:44 +00001203
Stephen Warren6904b6e2011-10-18 11:11:49 +00001204 puts(" Verifying Checksum ... ");
1205 if (!image_check_dcrc(hdr)) {
1206 puts("Bad Data CRC\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001207 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001208 puts("OK\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001209 }
1210 break;
1211#if defined(CONFIG_FIT)
1212 case IMAGE_FORMAT_FIT:
Stephen Warren6904b6e2011-10-18 11:11:49 +00001213 if (!fit_check_format(hdr))
Marian Balakowiczcc1fd912008-03-11 12:35:20 +01001214 goto next_sector;
wdenk47d1a6e2002-11-03 00:01:44 +00001215
Stephen Warren6904b6e2011-10-18 11:11:49 +00001216 printf("FIT Image at %08lX:\n", (ulong)hdr);
1217 fit_print_contents(hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001218 break;
1219#endif
1220 default:
1221 goto next_sector;
wdenk47d1a6e2002-11-03 00:01:44 +00001222 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001223
wdenk0a658552003-08-05 17:43:17 +00001224next_sector: ;
wdenk47d1a6e2002-11-03 00:01:44 +00001225 }
wdenk0a658552003-08-05 17:43:17 +00001226next_bank: ;
wdenk47d1a6e2002-11-03 00:01:44 +00001227 }
Vipin Kumar3df41b12012-12-16 22:32:48 +00001228 return 0;
1229}
1230#endif
1231
1232#if defined(CONFIG_CMD_IMLS_NAND)
1233static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
1234 size_t len)
1235{
1236 void *imgdata;
1237 int ret;
1238
1239 imgdata = malloc(len);
1240 if (!imgdata) {
1241 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
1242 nand_dev, off);
1243 printf(" Low memory(cannot allocate memory for image)\n");
1244 return -ENOMEM;
1245 }
1246
1247 ret = nand_read_skip_bad(nand, off, &len,
1248 imgdata);
1249 if (ret < 0 && ret != -EUCLEAN) {
1250 free(imgdata);
1251 return ret;
1252 }
1253
1254 if (!image_check_hcrc(imgdata)) {
1255 free(imgdata);
1256 return 0;
1257 }
1258
1259 printf("Legacy Image at NAND device %d offset %08llX:\n",
1260 nand_dev, off);
1261 image_print_contents(imgdata);
1262
1263 puts(" Verifying Checksum ... ");
1264 if (!image_check_dcrc(imgdata))
1265 puts("Bad Data CRC\n");
1266 else
1267 puts("OK\n");
1268
1269 free(imgdata);
1270
1271 return 0;
1272}
1273
1274static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
1275 size_t len)
1276{
1277 void *imgdata;
1278 int ret;
1279
1280 imgdata = malloc(len);
1281 if (!imgdata) {
1282 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
1283 nand_dev, off);
1284 printf(" Low memory(cannot allocate memory for image)\n");
1285 return -ENOMEM;
1286 }
wdenk47d1a6e2002-11-03 00:01:44 +00001287
Vipin Kumar3df41b12012-12-16 22:32:48 +00001288 ret = nand_read_skip_bad(nand, off, &len,
1289 imgdata);
1290 if (ret < 0 && ret != -EUCLEAN) {
1291 free(imgdata);
1292 return ret;
1293 }
1294
1295 if (!fit_check_format(imgdata)) {
1296 free(imgdata);
1297 return 0;
1298 }
1299
1300 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
1301
1302 fit_print_contents(imgdata);
1303 free(imgdata);
1304
1305 return 0;
1306}
1307
1308static int do_imls_nand(void)
1309{
1310 nand_info_t *nand;
1311 int nand_dev = nand_curr_device;
1312 size_t len;
1313 loff_t off;
1314 u32 buffer[16];
1315
1316 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
1317 puts("\nNo NAND devices available\n");
1318 return -ENODEV;
1319 }
1320
1321 printf("\n");
1322
1323 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
1324 nand = &nand_info[nand_dev];
1325 if (!nand->name || !nand->size)
1326 continue;
1327
1328 for (off = 0; off < nand->size; off += nand->erasesize) {
1329 const image_header_t *header;
1330 int ret;
1331
1332 if (nand_block_isbad(nand, off))
1333 continue;
1334
1335 len = sizeof(buffer);
1336
1337 ret = nand_read(nand, off, &len, (u8 *)buffer);
1338 if (ret < 0 && ret != -EUCLEAN) {
1339 printf("NAND read error %d at offset %08llX\n",
1340 ret, off);
1341 continue;
1342 }
1343
1344 switch (genimg_get_format(buffer)) {
1345 case IMAGE_FORMAT_LEGACY:
1346 header = (const image_header_t *)buffer;
1347
1348 len = image_get_image_size(header);
1349 nand_imls_legacyimage(nand, nand_dev, off, len);
1350 break;
1351#if defined(CONFIG_FIT)
1352 case IMAGE_FORMAT_FIT:
1353 len = fit_get_size(buffer);
1354 nand_imls_fitimage(nand, nand_dev, off, len);
1355 break;
1356#endif
1357 }
1358 }
1359 }
1360
1361 return 0;
1362}
1363#endif
1364
1365#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
1366static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1367{
1368 int ret_nor = 0, ret_nand = 0;
1369
1370#if defined(CONFIG_CMD_IMLS)
1371 ret_nor = do_imls_nor();
1372#endif
1373
1374#if defined(CONFIG_CMD_IMLS_NAND)
1375 ret_nand = do_imls_nand();
1376#endif
1377
1378 if (ret_nor)
1379 return ret_nor;
1380
1381 if (ret_nand)
1382 return ret_nand;
1383
wdenk874ac262003-07-24 23:38:38 +00001384 return (0);
1385}
wdenk47d1a6e2002-11-03 00:01:44 +00001386
wdenk874ac262003-07-24 23:38:38 +00001387U_BOOT_CMD(
1388 imls, 1, 1, do_imls,
Peter Tyserdfb72b82009-01-27 18:03:12 -06001389 "list all images found in flash",
wdenk874ac262003-07-24 23:38:38 +00001390 "\n"
Vipin Kumar3df41b12012-12-16 22:32:48 +00001391 " - Prints information about all images found at sector/block\n"
1392 " boundaries in nor/nand flash."
wdenk874ac262003-07-24 23:38:38 +00001393);
wdenk47d1a6e2002-11-03 00:01:44 +00001394#endif
1395
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001396/*******************************************************************/
Marian Balakowiczc40032c2008-01-31 13:59:09 +01001397/* helper routines */
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001398/*******************************************************************/
Joe Hershberger01c2d982013-02-27 10:20:59 +00001399#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
Doug Anderson1658c112012-01-17 09:37:41 +00001400
1401#define CONSOLE_ARG "console="
1402#define CONSOLE_ARG_LEN (sizeof(CONSOLE_ARG) - 1)
1403
Doug Andersonc76b8342011-10-19 12:30:57 +00001404static void fixup_silent_linux(void)
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001405{
Doug Anderson1658c112012-01-17 09:37:41 +00001406 char *buf;
1407 const char *env_val;
Doug Andersonc76b8342011-10-19 12:30:57 +00001408 char *cmdline = getenv("bootargs");
Simon Glass5db3f932013-07-16 20:10:00 -07001409 int want_silent;
wdenk47d1a6e2002-11-03 00:01:44 +00001410
Simon Glass5db3f932013-07-16 20:10:00 -07001411 /*
1412 * Only fix cmdline when requested. The environment variable can be:
1413 *
1414 * no - we never fixup
1415 * yes - we always fixup
1416 * unset - we rely on the console silent flag
1417 */
1418 want_silent = getenv_yesno("silent_linux");
1419 if (want_silent == 0)
1420 return;
1421 else if (want_silent == -1 && !(gd->flags & GD_FLG_SILENT))
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001422 return;
wdenk47d1a6e2002-11-03 00:01:44 +00001423
Doug Andersonc76b8342011-10-19 12:30:57 +00001424 debug("before silent fix-up: %s\n", cmdline);
Doug Anderson1658c112012-01-17 09:37:41 +00001425 if (cmdline && (cmdline[0] != '\0')) {
1426 char *start = strstr(cmdline, CONSOLE_ARG);
1427
1428 /* Allocate space for maximum possible new command line */
1429 buf = malloc(strlen(cmdline) + 1 + CONSOLE_ARG_LEN + 1);
1430 if (!buf) {
1431 debug("%s: out of memory\n", __func__);
1432 return;
1433 }
1434
Doug Andersonc76b8342011-10-19 12:30:57 +00001435 if (start) {
Doug Anderson1658c112012-01-17 09:37:41 +00001436 char *end = strchr(start, ' ');
1437 int num_start_bytes = start - cmdline + CONSOLE_ARG_LEN;
1438
1439 strncpy(buf, cmdline, num_start_bytes);
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001440 if (end)
Doug Anderson1658c112012-01-17 09:37:41 +00001441 strcpy(buf + num_start_bytes, end);
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001442 else
Doug Anderson1658c112012-01-17 09:37:41 +00001443 buf[num_start_bytes] = '\0';
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001444 } else {
Doug Anderson1658c112012-01-17 09:37:41 +00001445 sprintf(buf, "%s %s", cmdline, CONSOLE_ARG);
wdenk47d1a6e2002-11-03 00:01:44 +00001446 }
Doug Anderson1658c112012-01-17 09:37:41 +00001447 env_val = buf;
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001448 } else {
Doug Anderson1658c112012-01-17 09:37:41 +00001449 buf = NULL;
1450 env_val = CONSOLE_ARG;
wdenk47d1a6e2002-11-03 00:01:44 +00001451 }
wdenk47d1a6e2002-11-03 00:01:44 +00001452
Doug Anderson1658c112012-01-17 09:37:41 +00001453 setenv("bootargs", env_val);
1454 debug("after silent fix-up: %s\n", env_val);
1455 free(buf);
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001456}
1457#endif /* CONFIG_SILENT_CONSOLE */
wdenk47d1a6e2002-11-03 00:01:44 +00001458
Steven Stalliona7386c42013-06-10 01:00:09 -07001459#if defined(CONFIG_BOOTM_NETBSD) || defined(CONFIG_BOOTM_PLAN9)
1460static void copy_args(char *dest, int argc, char * const argv[], char delim)
1461{
1462 int i;
1463
1464 for (i = 0; i < argc; i++) {
1465 if (i > 0)
1466 *dest++ = delim;
1467 strcpy(dest, argv[i]);
1468 dest += strlen(argv[i]);
1469 }
1470}
1471#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001472
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001473/*******************************************************************/
1474/* OS booting routines */
1475/*******************************************************************/
wdenk47d1a6e2002-11-03 00:01:44 +00001476
Kumar Gala697675e2008-10-21 17:25:47 -05001477#ifdef CONFIG_BOOTM_NETBSD
Stephen Warren6904b6e2011-10-18 11:11:49 +00001478static int do_bootm_netbsd(int flag, int argc, char * const argv[],
Marian Balakowicz61fde552008-02-27 11:01:04 +01001479 bootm_headers_t *images)
wdenk92bbe3f2003-04-20 14:04:18 +00001480{
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001481 void (*loader)(bd_t *, image_header_t *, char *, char *);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001482 image_header_t *os_hdr, *hdr;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +01001483 ulong kernel_data, kernel_len;
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001484 char *consdev;
1485 char *cmdline;
wdenk47d1a6e2002-11-03 00:01:44 +00001486
Miao Yanc4f63532013-11-04 11:18:14 +08001487 if (flag != BOOTM_STATE_OS_GO)
Simon Glassf059d0a2013-07-10 23:08:09 -07001488 return 0;
Kumar Gala18178bc2008-10-21 17:25:45 -05001489
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001490#if defined(CONFIG_FIT)
1491 if (!images->legacy_hdr_valid) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001492 fit_unsupported_reset("NetBSD");
Kumar Gala48626aa2008-08-15 08:24:45 -05001493 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001494 }
1495#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001496 hdr = images->legacy_hdr_os;
wdenk47d1a6e2002-11-03 00:01:44 +00001497
1498 /*
1499 * Booting a (NetBSD) kernel image
1500 *
1501 * This process is pretty similar to a standalone application:
1502 * The (first part of an multi-) image must be a stage-2 loader,
1503 * which in turn is responsible for loading & invoking the actual
1504 * kernel. The only differences are the parameters being passed:
1505 * besides the board info strucure, the loader expects a command
1506 * line, the name of the console device, and (optionally) the
1507 * address of the original image header.
1508 */
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001509 os_hdr = NULL;
Stephen Warren6904b6e2011-10-18 11:11:49 +00001510 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1511 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
Marian Balakowiczb4a12a92008-01-08 18:12:17 +01001512 if (kernel_len)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001513 os_hdr = hdr;
Marian Balakowiczb4a12a92008-01-08 18:12:17 +01001514 }
wdenk47d1a6e2002-11-03 00:01:44 +00001515
1516 consdev = "";
Stephen Warren6904b6e2011-10-18 11:11:49 +00001517#if defined(CONFIG_8xx_CONS_SMC1)
wdenk47d1a6e2002-11-03 00:01:44 +00001518 consdev = "smc1";
Stephen Warren6904b6e2011-10-18 11:11:49 +00001519#elif defined(CONFIG_8xx_CONS_SMC2)
wdenk47d1a6e2002-11-03 00:01:44 +00001520 consdev = "smc2";
Stephen Warren6904b6e2011-10-18 11:11:49 +00001521#elif defined(CONFIG_8xx_CONS_SCC2)
wdenk47d1a6e2002-11-03 00:01:44 +00001522 consdev = "scc2";
Stephen Warren6904b6e2011-10-18 11:11:49 +00001523#elif defined(CONFIG_8xx_CONS_SCC3)
wdenk47d1a6e2002-11-03 00:01:44 +00001524 consdev = "scc3";
1525#endif
1526
Simon Glass794a9212013-06-11 11:14:46 -07001527 if (argc > 0) {
wdenk47d1a6e2002-11-03 00:01:44 +00001528 ulong len;
1529 int i;
1530
Simon Glass794a9212013-06-11 11:14:46 -07001531 for (i = 0, len = 0; i < argc; i += 1)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001532 len += strlen(argv[i]) + 1;
1533 cmdline = malloc(len);
Steven Stalliona7386c42013-06-10 01:00:09 -07001534 copy_args(cmdline, argc, argv, ' ');
Stephen Warren6904b6e2011-10-18 11:11:49 +00001535 } else if ((cmdline = getenv("bootargs")) == NULL) {
wdenk47d1a6e2002-11-03 00:01:44 +00001536 cmdline = "";
1537 }
1538
Kumar Gala93467bc2008-08-15 08:24:36 -05001539 loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
wdenk47d1a6e2002-11-03 00:01:44 +00001540
Stephen Warren6904b6e2011-10-18 11:11:49 +00001541 printf("## Transferring control to NetBSD stage-2 loader "
1542 "(at address %08lx) ...\n",
wdenk47d1a6e2002-11-03 00:01:44 +00001543 (ulong)loader);
1544
Simon Glass0169e6b2012-02-13 13:51:18 +00001545 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
wdenk47d1a6e2002-11-03 00:01:44 +00001546
1547 /*
1548 * NetBSD Stage-2 Loader Parameters:
Kees Jongenburger22b13f92013-12-12 16:18:35 +01001549 * arg[0]: pointer to board info data
1550 * arg[1]: image load address
1551 * arg[2]: char pointer to the console device to use
1552 * arg[3]: char pointer to the boot arguments
wdenk47d1a6e2002-11-03 00:01:44 +00001553 */
Stephen Warren6904b6e2011-10-18 11:11:49 +00001554 (*loader)(gd->bd, os_hdr, consdev, cmdline);
Kumar Gala48626aa2008-08-15 08:24:45 -05001555
1556 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001557}
Kumar Gala697675e2008-10-21 17:25:47 -05001558#endif /* CONFIG_BOOTM_NETBSD*/
wdenk47d1a6e2002-11-03 00:01:44 +00001559
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001560#ifdef CONFIG_LYNXKDI
Stephen Warren6904b6e2011-10-18 11:11:49 +00001561static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
Marian Balakowicz61fde552008-02-27 11:01:04 +01001562 bootm_headers_t *images)
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001563{
Marian Balakowiczab00e022008-04-11 11:07:49 +02001564 image_header_t *hdr = &images->legacy_hdr_os_copy;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001565
Miao Yanc4f63532013-11-04 11:18:14 +08001566 if (flag != BOOTM_STATE_OS_GO)
Simon Glassf059d0a2013-07-10 23:08:09 -07001567 return 0;
Kumar Gala18178bc2008-10-21 17:25:45 -05001568
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001569#if defined(CONFIG_FIT)
1570 if (!images->legacy_hdr_valid) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001571 fit_unsupported_reset("Lynx");
Kumar Gala48626aa2008-08-15 08:24:45 -05001572 return 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001573 }
1574#endif
1575
Stephen Warren6904b6e2011-10-18 11:11:49 +00001576 lynxkdi_boot((image_header_t *)hdr);
Kumar Gala48626aa2008-08-15 08:24:45 -05001577
1578 return 1;
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001579}
1580#endif /* CONFIG_LYNXKDI */
1581
Kumar Gala697675e2008-10-21 17:25:47 -05001582#ifdef CONFIG_BOOTM_RTEMS
Stephen Warren6904b6e2011-10-18 11:11:49 +00001583static int do_bootm_rtems(int flag, int argc, char * const argv[],
Marian Balakowicz61fde552008-02-27 11:01:04 +01001584 bootm_headers_t *images)
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001585{
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001586 void (*entry_point)(bd_t *);
wdenk92bbe3f2003-04-20 14:04:18 +00001587
Miao Yanc4f63532013-11-04 11:18:14 +08001588 if (flag != BOOTM_STATE_OS_GO)
Simon Glassf059d0a2013-07-10 23:08:09 -07001589 return 0;
Kumar Gala18178bc2008-10-21 17:25:45 -05001590
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001591#if defined(CONFIG_FIT)
1592 if (!images->legacy_hdr_valid) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001593 fit_unsupported_reset("RTEMS");
Kumar Gala48626aa2008-08-15 08:24:45 -05001594 return 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001595 }
1596#endif
1597
Kumar Gala93467bc2008-08-15 08:24:36 -05001598 entry_point = (void (*)(bd_t *))images->ep;
wdenk47d1a6e2002-11-03 00:01:44 +00001599
Stephen Warren6904b6e2011-10-18 11:11:49 +00001600 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
wdenk92bbe3f2003-04-20 14:04:18 +00001601 (ulong)entry_point);
wdenk47d1a6e2002-11-03 00:01:44 +00001602
Simon Glass0169e6b2012-02-13 13:51:18 +00001603 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
wdenk92bbe3f2003-04-20 14:04:18 +00001604
1605 /*
1606 * RTEMS Parameters:
1607 * r3: ptr to board info data
1608 */
Marian Balakowicz2df645e2008-01-08 18:17:10 +01001609 (*entry_point)(gd->bd);
Kumar Gala48626aa2008-08-15 08:24:45 -05001610
1611 return 1;
wdenk92bbe3f2003-04-20 14:04:18 +00001612}
Kumar Gala697675e2008-10-21 17:25:47 -05001613#endif /* CONFIG_BOOTM_RTEMS */
wdenk92bbe3f2003-04-20 14:04:18 +00001614
Torkel Lundgren29a58662010-09-28 11:05:36 +02001615#if defined(CONFIG_BOOTM_OSE)
Stephen Warren6904b6e2011-10-18 11:11:49 +00001616static int do_bootm_ose(int flag, int argc, char * const argv[],
Torkel Lundgren29a58662010-09-28 11:05:36 +02001617 bootm_headers_t *images)
1618{
1619 void (*entry_point)(void);
1620
Miao Yanc4f63532013-11-04 11:18:14 +08001621 if (flag != BOOTM_STATE_OS_GO)
Simon Glassf059d0a2013-07-10 23:08:09 -07001622 return 0;
Torkel Lundgren29a58662010-09-28 11:05:36 +02001623
1624#if defined(CONFIG_FIT)
1625 if (!images->legacy_hdr_valid) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001626 fit_unsupported_reset("OSE");
Torkel Lundgren29a58662010-09-28 11:05:36 +02001627 return 1;
1628 }
1629#endif
1630
1631 entry_point = (void (*)(void))images->ep;
1632
Stephen Warren6904b6e2011-10-18 11:11:49 +00001633 printf("## Transferring control to OSE (at address %08lx) ...\n",
Torkel Lundgren29a58662010-09-28 11:05:36 +02001634 (ulong)entry_point);
1635
Simon Glass0169e6b2012-02-13 13:51:18 +00001636 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Torkel Lundgren29a58662010-09-28 11:05:36 +02001637
1638 /*
1639 * OSE Parameters:
1640 * None
1641 */
1642 (*entry_point)();
1643
1644 return 1;
1645}
1646#endif /* CONFIG_BOOTM_OSE */
1647
Steven Stallion36ec8ac2013-03-20 06:31:35 +00001648#if defined(CONFIG_BOOTM_PLAN9)
1649static int do_bootm_plan9(int flag, int argc, char * const argv[],
1650 bootm_headers_t *images)
1651{
1652 void (*entry_point)(void);
Steven Stalliona7386c42013-06-10 01:00:09 -07001653 char *s;
Steven Stallion36ec8ac2013-03-20 06:31:35 +00001654
Miao Yanc4f63532013-11-04 11:18:14 +08001655 if (flag != BOOTM_STATE_OS_GO)
Simon Glassf059d0a2013-07-10 23:08:09 -07001656 return 0;
Steven Stallion36ec8ac2013-03-20 06:31:35 +00001657
1658#if defined(CONFIG_FIT)
1659 if (!images->legacy_hdr_valid) {
1660 fit_unsupported_reset("Plan 9");
1661 return 1;
1662 }
1663#endif
1664
Steven Stalliona7386c42013-06-10 01:00:09 -07001665 /* See README.plan9 */
1666 s = getenv("confaddr");
1667 if (s != NULL) {
1668 char *confaddr = (char *)simple_strtoul(s, NULL, 16);
1669
1670 if (argc > 0) {
1671 copy_args(confaddr, argc, argv, '\n');
1672 } else {
1673 s = getenv("bootargs");
1674 if (s != NULL)
1675 strcpy(confaddr, s);
1676 }
1677 }
1678
Steven Stallion36ec8ac2013-03-20 06:31:35 +00001679 entry_point = (void (*)(void))images->ep;
1680
1681 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
1682 (ulong)entry_point);
1683
1684 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1685
1686 /*
1687 * Plan 9 Parameters:
1688 * None
1689 */
1690 (*entry_point)();
1691
1692 return 1;
1693}
1694#endif /* CONFIG_BOOTM_PLAN9 */
1695
Miao Yan1bd54562013-11-28 17:51:38 +08001696#if defined(CONFIG_BOOTM_VXWORKS) && \
1697 (defined(CONFIG_PPC) || defined(CONFIG_ARM))
1698
1699void do_bootvx_fdt(bootm_headers_t *images)
1700{
1701#if defined(CONFIG_OF_LIBFDT)
1702 int ret;
1703 char *bootline;
1704 ulong of_size = images->ft_len;
1705 char **of_flat_tree = &images->ft_addr;
1706 struct lmb *lmb = &images->lmb;
1707
1708 if (*of_flat_tree) {
1709 boot_fdt_add_mem_rsv_regions(lmb, *of_flat_tree);
1710
1711 ret = boot_relocate_fdt(lmb, of_flat_tree, &of_size);
1712 if (ret)
1713 return;
1714
1715 ret = fdt_add_subnode(*of_flat_tree, 0, "chosen");
1716 if ((ret >= 0 || ret == -FDT_ERR_EXISTS)) {
1717 bootline = getenv("bootargs");
1718 if (bootline) {
1719 ret = fdt_find_and_setprop(*of_flat_tree,
1720 "/chosen", "bootargs",
1721 bootline,
1722 strlen(bootline) + 1, 1);
1723 if (ret < 0) {
1724 printf("## ERROR: %s : %s\n", __func__,
1725 fdt_strerror(ret));
1726 return;
1727 }
1728 }
1729 } else {
1730 printf("## ERROR: %s : %s\n", __func__,
1731 fdt_strerror(ret));
1732 return;
1733 }
1734 }
1735#endif
1736
1737 boot_prep_vxworks(images);
1738
1739 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1740
1741#if defined(CONFIG_OF_LIBFDT)
1742 printf("## Starting vxWorks at 0x%08lx, device tree at 0x%08lx ...\n",
1743 (ulong)images->ep, (ulong)*of_flat_tree);
1744#else
1745 printf("## Starting vxWorks at 0x%08lx\n", (ulong)images->ep);
1746#endif
1747
1748 boot_jump_vxworks(images);
1749
1750 puts("## vxWorks terminated\n");
1751}
1752
Stephen Warren6904b6e2011-10-18 11:11:49 +00001753static int do_bootm_vxworks(int flag, int argc, char * const argv[],
Marian Balakowicz61fde552008-02-27 11:01:04 +01001754 bootm_headers_t *images)
wdenk47d1a6e2002-11-03 00:01:44 +00001755{
Miao Yanc4f63532013-11-04 11:18:14 +08001756 if (flag != BOOTM_STATE_OS_GO)
Simon Glassf059d0a2013-07-10 23:08:09 -07001757 return 0;
Kumar Gala18178bc2008-10-21 17:25:45 -05001758
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001759#if defined(CONFIG_FIT)
Marian Balakowiczab00e022008-04-11 11:07:49 +02001760 if (!images->legacy_hdr_valid) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001761 fit_unsupported_reset("VxWorks");
Kumar Gala48626aa2008-08-15 08:24:45 -05001762 return 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001763 }
1764#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001765
Miao Yan1bd54562013-11-28 17:51:38 +08001766 do_bootvx_fdt(images);
Kumar Gala48626aa2008-08-15 08:24:45 -05001767
1768 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001769}
Miao Yanf8ba19f2013-11-28 17:51:36 +08001770#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001771
Miao Yanf8ba19f2013-11-28 17:51:36 +08001772#if defined(CONFIG_CMD_ELF)
Wolfgang Denk6262d0212010-06-28 22:00:46 +02001773static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
Marian Balakowicz61fde552008-02-27 11:01:04 +01001774 bootm_headers_t *images)
wdenk47d1a6e2002-11-03 00:01:44 +00001775{
wdenk47d1a6e2002-11-03 00:01:44 +00001776 char *local_args[2];
1777 char str[16];
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001778
Miao Yanc4f63532013-11-04 11:18:14 +08001779 if (flag != BOOTM_STATE_OS_GO)
Simon Glassf059d0a2013-07-10 23:08:09 -07001780 return 0;
Kumar Gala18178bc2008-10-21 17:25:45 -05001781
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001782#if defined(CONFIG_FIT)
1783 if (!images->legacy_hdr_valid) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001784 fit_unsupported_reset("QNX");
Kumar Gala48626aa2008-08-15 08:24:45 -05001785 return 1;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +01001786 }
1787#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001788
Kumar Gala93467bc2008-08-15 08:24:36 -05001789 sprintf(str, "%lx", images->ep); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001790 local_args[0] = argv[0];
1791 local_args[1] = str; /* and provide it via the arguments */
Kumar Gala48626aa2008-08-15 08:24:45 -05001792 do_bootelf(NULL, 0, 2, local_args);
1793
1794 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001795}
Jon Loeligerd704d912007-07-10 11:02:44 -05001796#endif
Peter Tyser56b8dd12008-09-08 14:56:49 -05001797
1798#ifdef CONFIG_INTEGRITY
Stephen Warren6904b6e2011-10-18 11:11:49 +00001799static int do_bootm_integrity(int flag, int argc, char * const argv[],
Peter Tyser56b8dd12008-09-08 14:56:49 -05001800 bootm_headers_t *images)
1801{
1802 void (*entry_point)(void);
1803
Miao Yanc4f63532013-11-04 11:18:14 +08001804 if (flag != BOOTM_STATE_OS_GO)
Simon Glassf059d0a2013-07-10 23:08:09 -07001805 return 0;
Kumar Gala18178bc2008-10-21 17:25:45 -05001806
Peter Tyser56b8dd12008-09-08 14:56:49 -05001807#if defined(CONFIG_FIT)
1808 if (!images->legacy_hdr_valid) {
Stephen Warren6904b6e2011-10-18 11:11:49 +00001809 fit_unsupported_reset("INTEGRITY");
Peter Tyser56b8dd12008-09-08 14:56:49 -05001810 return 1;
1811 }
1812#endif
1813
1814 entry_point = (void (*)(void))images->ep;
1815
Stephen Warren6904b6e2011-10-18 11:11:49 +00001816 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
Peter Tyser56b8dd12008-09-08 14:56:49 -05001817 (ulong)entry_point);
1818
Simon Glass0169e6b2012-02-13 13:51:18 +00001819 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Peter Tyser56b8dd12008-09-08 14:56:49 -05001820
1821 /*
1822 * INTEGRITY Parameters:
1823 * None
1824 */
1825 (*entry_point)();
1826
1827 return 1;
1828}
1829#endif
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001830
1831#ifdef CONFIG_CMD_BOOTZ
1832
Simon Glass5f1afdd2013-07-04 13:26:10 -07001833int __weak bootz_setup(ulong image, ulong *start, ulong *end)
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001834{
1835 /* Please define bootz_setup() for your platform */
1836
1837 puts("Your platform's zImage format isn't supported yet!\n");
1838 return -1;
1839}
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001840
1841/*
1842 * zImage booting support
1843 */
1844static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
1845 char * const argv[], bootm_headers_t *images)
1846{
1847 int ret;
Simon Glass5f1afdd2013-07-04 13:26:10 -07001848 ulong zi_start, zi_end;
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001849
Simon Glass3081e2e2013-06-11 11:14:47 -07001850 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
1851 images, 1);
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001852
1853 /* Setup Linux kernel zImage entry point */
Tom Rini6e5dac42013-07-09 15:32:34 -04001854 if (!argc) {
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001855 images->ep = load_addr;
1856 debug("* kernel: default image load address = 0x%08lx\n",
1857 load_addr);
1858 } else {
Tom Rini6e5dac42013-07-09 15:32:34 -04001859 images->ep = simple_strtoul(argv[0], NULL, 16);
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001860 debug("* kernel: cmdline image address = 0x%08lx\n",
1861 images->ep);
1862 }
1863
Simon Glass5f1afdd2013-07-04 13:26:10 -07001864 ret = bootz_setup(images->ep, &zi_start, &zi_end);
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001865 if (ret != 0)
1866 return 1;
1867
1868 lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
1869
Tom Rini1ab61472013-07-09 15:33:25 -04001870 /*
1871 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
1872 * have a header that provide this informaiton.
1873 */
Tom Rini6e5dac42013-07-09 15:32:34 -04001874 if (bootm_find_ramdisk(flag, argc, argv))
1875 return 1;
1876
1877#if defined(CONFIG_OF_LIBFDT)
1878 if (bootm_find_fdt(flag, argc, argv))
1879 return 1;
1880#endif
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001881
Tom Rini6e5dac42013-07-09 15:32:34 -04001882 return 0;
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001883}
1884
Rob Herringc88e1bf2012-12-02 21:00:23 -06001885int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001886{
Simon Glass3081e2e2013-06-11 11:14:47 -07001887 int ret;
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001888
Tom Rini6e5dac42013-07-09 15:32:34 -04001889 /* Consume 'bootz' */
1890 argc--; argv++;
1891
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001892 if (bootz_start(cmdtp, flag, argc, argv, &images))
1893 return 1;
1894
Simon Glass917f6842013-07-04 13:26:08 -07001895 /*
1896 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
1897 * disable interrupts ourselves
1898 */
1899 bootm_disable_interrupts();
1900
Simon Glass42bca362013-07-04 13:26:11 -07001901 images.os.os = IH_OS_LINUX;
Simon Glass3081e2e2013-06-11 11:14:47 -07001902 ret = do_bootm_states(cmdtp, flag, argc, argv,
Simon Glass42bca362013-07-04 13:26:11 -07001903 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
1904 BOOTM_STATE_OS_GO,
Simon Glass00134882013-06-11 11:14:48 -07001905 &images, 1);
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001906
Simon Glass3081e2e2013-06-11 11:14:47 -07001907 return ret;
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001908}
1909
Kim Phillipsdc00a682012-10-29 13:34:31 +00001910#ifdef CONFIG_SYS_LONGHELP
1911static char bootz_help_text[] =
Marek Vasut28850d02012-03-18 11:47:58 +00001912 "[addr [initrd[:size]] [fdt]]\n"
1913 " - boot Linux zImage stored in memory\n"
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001914 "\tThe argument 'initrd' is optional and specifies the address\n"
Marek Vasut28850d02012-03-18 11:47:58 +00001915 "\tof the initrd in memory. The optional argument ':size' allows\n"
1916 "\tspecifying the size of RAW initrd.\n"
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001917#if defined(CONFIG_OF_LIBFDT)
1918 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
1919 "\ta third argument is required which is the address of the\n"
1920 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1921 "\tuse a '-' for the second argument. If you do not pass a third\n"
1922 "\ta bd_info struct will be passed instead\n"
1923#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +00001924 "";
1925#endif
1926
1927U_BOOT_CMD(
1928 bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
1929 "boot Linux zImage image from memory", bootz_help_text
Marek Vasutcf41a9b2012-03-14 21:52:45 +00001930);
1931#endif /* CONFIG_CMD_BOOTZ */