blob: 7438469d091721a6dcba15ec2263ef7b086dc743 [file] [log] [blame]
wdenk47d1a6e2002-11-03 00:01:44 +00001/*
Detlev Zundelca95c9d2009-07-13 16:01:18 +02002 * (C) Copyright 2000-2009
wdenk47d1a6e2002-11-03 00:01:44 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
5 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010024
wdenk47d1a6e2002-11-03 00:01:44 +000025/*
26 * Boot support
27 */
28#include <common.h>
29#include <watchdog.h>
30#include <command.h>
wdenk47d1a6e2002-11-03 00:01:44 +000031#include <image.h>
32#include <malloc.h>
Jean-Christophe PLAGNIOL-VILLARDa31e0912009-04-04 12:49:11 +020033#include <u-boot/zlib.h>
wdenkc29fdfc2003-08-29 20:57:53 +000034#include <bzlib.h>
wdenk7f70e852003-05-20 14:25:27 +000035#include <environment.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060036#include <lmb.h>
Kumar Gala49c3a862008-10-21 17:25:45 -050037#include <linux/ctype.h>
wdenk47d1a6e2002-11-03 00:01:44 +000038#include <asm/byteorder.h>
Anatolij Gustschin5bf27662011-11-19 13:12:18 +000039#include <linux/compiler.h>
wdenk8bde7f72003-06-27 21:31:46 +000040
Stefan Roeseebb86c42008-07-30 09:59:51 +020041#if defined(CONFIG_CMD_USB)
Markus Klotzbücher3d71c812008-07-10 14:47:09 +020042#include <usb.h>
43#endif
44
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020045#ifdef CONFIG_SYS_HUSH_PARSER
wdenk47d1a6e2002-11-03 00:01:44 +000046#include <hush.h>
47#endif
48
Kumar Gala54f9c862008-08-15 08:24:39 -050049#if defined(CONFIG_OF_LIBFDT)
Kumar Gala54f9c862008-08-15 08:24:39 -050050#include <libfdt.h>
51#include <fdt_support.h>
52#endif
53
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +020054#ifdef CONFIG_LZMA
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +020055#include <lzma/LzmaTypes.h>
Luigi 'Comio' Mantellinicaf72ff2009-07-21 10:45:49 +020056#include <lzma/LzmaDec.h>
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +020057#include <lzma/LzmaTools.h>
58#endif /* CONFIG_LZMA */
59
Peter Korsgaard20dde482009-11-19 11:37:51 +010060#ifdef CONFIG_LZO
61#include <linux/lzo.h>
62#endif /* CONFIG_LZO */
63
Marian Balakowicz1ee11802008-01-08 18:17:10 +010064DECLARE_GLOBAL_DATA_PTR;
65
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +020066#ifndef CONFIG_SYS_BOOTM_LEN
67#define CONFIG_SYS_BOOTM_LEN 0x800000 /* use 8MByte as default max gunzip size */
wdenk47d1a6e2002-11-03 00:01:44 +000068#endif
69
Marian Balakowicz321359f2008-01-08 18:11:43 +010070#ifdef CONFIG_BZIP2
71extern void bz_internal_error(int);
wdenk228f29a2002-12-08 09:53:23 +000072#endif
73
Jon Loeligerbaa26db2007-07-08 17:51:39 -050074#if defined(CONFIG_CMD_IMI)
Stephen Warren712fbcf2011-10-18 11:11:49 +000075static int image_info(unsigned long addr);
wdenk47d1a6e2002-11-03 00:01:44 +000076#endif
wdenk27b207f2003-07-24 23:38:38 +000077
Jon Loeligerbaa26db2007-07-08 17:51:39 -050078#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +000079#include <flash.h>
Stefan Roeseca5def32010-08-31 10:00:10 +020080#include <mtd/cfi_flash.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020081extern flash_info_t flash_info[]; /* info for FLASH chips */
Vipin Kumar8fdf1e02012-12-16 22:32:48 +000082#endif
83
84#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
Stephen Warren712fbcf2011-10-18 11:11:49 +000085static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
wdenk27b207f2003-07-24 23:38:38 +000086#endif
87
Vipin Kumar8fdf1e02012-12-16 22:32:48 +000088#include <linux/err.h>
89#include <nand.h>
90
Joe Hershbergerbe2e5a02013-02-27 10:20:59 +000091#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
Stephen Warren712fbcf2011-10-18 11:11:49 +000092static void fixup_silent_linux(void);
wdenk2262cfe2002-11-18 00:14:45 +000093#endif
94
Stephen Warren712fbcf2011-10-18 11:11:49 +000095static image_header_t *image_get_kernel(ulong img_addr, int verify);
Marian Balakowicz6986a382008-03-12 10:01:05 +010096#if defined(CONFIG_FIT)
Stephen Warren712fbcf2011-10-18 11:11:49 +000097static int fit_check_kernel(const void *fit, int os_noffset, int verify);
Marian Balakowicz6986a382008-03-12 10:01:05 +010098#endif
99
Stephen Warren712fbcf2011-10-18 11:11:49 +0000100static void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
101 char * const argv[], bootm_headers_t *images,
102 ulong *os_data, ulong *os_len);
wdenk47d1a6e2002-11-03 00:01:44 +0000103
wdenk47d1a6e2002-11-03 00:01:44 +0000104/*
105 * Continue booting an OS image; caller already has:
106 * - copied image header to global variable `header'
107 * - checked header magic number, checksums (both header & image),
108 * - verified image architecture (PPC) and type (KERNEL or MULTI),
109 * - loaded (first part of) image to header load address,
110 * - disabled interrupts.
111 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000112typedef int boot_os_fn(int flag, int argc, char * const argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +0100113 bootm_headers_t *images); /* pointers to os/initrd/fdt */
wdenk47d1a6e2002-11-03 00:01:44 +0000114
Kumar Galab1d0db12008-10-21 17:25:47 -0500115#ifdef CONFIG_BOOTM_LINUX
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100116extern boot_os_fn do_bootm_linux;
Kumar Galab1d0db12008-10-21 17:25:47 -0500117#endif
118#ifdef CONFIG_BOOTM_NETBSD
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100119static boot_os_fn do_bootm_netbsd;
Kumar Galab1d0db12008-10-21 17:25:47 -0500120#endif
Marian Balakowiczf13e7b22008-01-08 18:12:17 +0100121#if defined(CONFIG_LYNXKDI)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100122static boot_os_fn do_bootm_lynxkdi;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000123extern void lynxkdi_boot(image_header_t *);
wdenk8bde7f72003-06-27 21:31:46 +0000124#endif
Kumar Galab1d0db12008-10-21 17:25:47 -0500125#ifdef CONFIG_BOOTM_RTEMS
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100126static boot_os_fn do_bootm_rtems;
Kumar Galab1d0db12008-10-21 17:25:47 -0500127#endif
Torkel Lundgren3df61952010-09-28 11:05:36 +0200128#if defined(CONFIG_BOOTM_OSE)
129static boot_os_fn do_bootm_ose;
130#endif
Steven Stallion04d41402013-03-20 06:31:35 +0000131#if defined(CONFIG_BOOTM_PLAN9)
132static boot_os_fn do_bootm_plan9;
133#endif
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500134#if defined(CONFIG_CMD_ELF)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100135static boot_os_fn do_bootm_vxworks;
136static boot_os_fn do_bootm_qnxelf;
Stephen Warren712fbcf2011-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 Loeliger90253172007-07-10 11:02:44 -0500139#endif
Peter Tyserf5ed9e32008-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 Frysinger00085552009-11-03 11:36:26 -0500144static boot_os_fn *boot_os[] = {
Kumar Galab1d0db12008-10-21 17:25:47 -0500145#ifdef CONFIG_BOOTM_LINUX
Kumar Galabe083152008-10-21 17:25:44 -0500146 [IH_OS_LINUX] = do_bootm_linux,
Kumar Galab1d0db12008-10-21 17:25:47 -0500147#endif
148#ifdef CONFIG_BOOTM_NETBSD
Kumar Galabe083152008-10-21 17:25:44 -0500149 [IH_OS_NETBSD] = do_bootm_netbsd,
Kumar Galab1d0db12008-10-21 17:25:47 -0500150#endif
Kumar Galabe083152008-10-21 17:25:44 -0500151#ifdef CONFIG_LYNXKDI
152 [IH_OS_LYNXOS] = do_bootm_lynxkdi,
153#endif
Kumar Galab1d0db12008-10-21 17:25:47 -0500154#ifdef CONFIG_BOOTM_RTEMS
Kumar Galabe083152008-10-21 17:25:44 -0500155 [IH_OS_RTEMS] = do_bootm_rtems,
Kumar Galab1d0db12008-10-21 17:25:47 -0500156#endif
Torkel Lundgren3df61952010-09-28 11:05:36 +0200157#if defined(CONFIG_BOOTM_OSE)
158 [IH_OS_OSE] = do_bootm_ose,
159#endif
Steven Stallion04d41402013-03-20 06:31:35 +0000160#if defined(CONFIG_BOOTM_PLAN9)
161 [IH_OS_PLAN9] = do_bootm_plan9,
162#endif
Kumar Galabe083152008-10-21 17:25:44 -0500163#if defined(CONFIG_CMD_ELF)
164 [IH_OS_VXWORKS] = do_bootm_vxworks,
165 [IH_OS_QNX] = do_bootm_qnxelf,
166#endif
167#ifdef CONFIG_INTEGRITY
168 [IH_OS_INTEGRITY] = do_bootm_integrity,
169#endif
170};
171
Simon Schwarzdee17762011-09-02 00:50:31 +0000172bootm_headers_t images; /* pointers to os/initrd/fdt images */
Stefan Roese15940c92006-03-13 11:16:36 +0100173
Kumar Gala3dfad402009-08-27 08:23:55 -0500174/* Allow for arch specific config before we boot */
Kim Phillips088f1b12012-10-29 13:34:31 +0000175static void __arch_preboot_os(void)
Kumar Gala3dfad402009-08-27 08:23:55 -0500176{
177 /* please define platform specific arch_preboot_os() */
178}
179void arch_preboot_os(void) __attribute__((weak, alias("__arch_preboot_os")));
180
Mike Frysinger476af292011-10-03 14:50:33 +0000181#define IH_INITRD_ARCH IH_ARCH_DEFAULT
wdenk47d1a6e2002-11-03 00:01:44 +0000182
Mike Frysingera16028d2009-11-03 11:35:59 -0500183#ifdef CONFIG_LMB
Marek Vasut44f074c2012-03-14 21:52:45 +0000184static void boot_start_lmb(bootm_headers_t *images)
185{
Becky Bruce391fd932008-06-09 20:37:18 -0500186 ulong mem_start;
187 phys_size_t mem_size;
wdenk47d1a6e2002-11-03 00:01:44 +0000188
Marek Vasut44f074c2012-03-14 21:52:45 +0000189 lmb_init(&images->lmb);
wdenk47d1a6e2002-11-03 00:01:44 +0000190
Kumar Galad3f2fa02008-02-27 21:51:50 -0600191 mem_start = getenv_bootm_low();
192 mem_size = getenv_bootm_size();
wdenk47d1a6e2002-11-03 00:01:44 +0000193
Marek Vasut44f074c2012-03-14 21:52:45 +0000194 lmb_add(&images->lmb, (phys_addr_t)mem_start, mem_size);
wdenk47d1a6e2002-11-03 00:01:44 +0000195
Marek Vasut44f074c2012-03-14 21:52:45 +0000196 arch_lmb_reserve(&images->lmb);
197 board_lmb_reserve(&images->lmb);
Mike Frysingera16028d2009-11-03 11:35:59 -0500198}
Marek Vasut44f074c2012-03-14 21:52:45 +0000199#else
Marek Vasut35cf5fe2012-04-01 03:59:46 +0000200#define lmb_reserve(lmb, base, size)
Marek Vasut44f074c2012-03-14 21:52:45 +0000201static inline void boot_start_lmb(bootm_headers_t *images) { }
202#endif
Mike Frysingera16028d2009-11-03 11:35:59 -0500203
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200204static int bootm_start(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Mike Frysingera16028d2009-11-03 11:35:59 -0500205{
206 void *os_hdr;
207 int ret;
208
Stephen Warren712fbcf2011-10-18 11:11:49 +0000209 memset((void *)&images, 0, sizeof(images));
210 images.verify = getenv_yesno("verify");
Mike Frysingera16028d2009-11-03 11:35:59 -0500211
Marek Vasut44f074c2012-03-14 21:52:45 +0000212 boot_start_lmb(&images);
wdenk47d1a6e2002-11-03 00:01:44 +0000213
Simon Glass573f14f2011-12-10 11:08:06 +0000214 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_START, "bootm_start");
215
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100216 /* get kernel image header, start address and length */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000217 os_hdr = boot_get_kernel(cmdtp, flag, argc, argv,
Kumar Gala396f6352008-08-15 08:24:41 -0500218 &images, &images.os.image_start, &images.os.image_len);
219 if (images.os.image_len == 0) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000220 puts("ERROR: can't get kernel image!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000221 return 1;
222 }
wdenk47d1a6e2002-11-03 00:01:44 +0000223
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100224 /* get image parameters */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000225 switch (genimg_get_format(os_hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100226 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000227 images.os.type = image_get_type(os_hdr);
228 images.os.comp = image_get_comp(os_hdr);
229 images.os.os = image_get_os(os_hdr);
Wolfgang Denkbccae902005-10-06 01:50:50 +0200230
Stephen Warren712fbcf2011-10-18 11:11:49 +0000231 images.os.end = image_get_image_end(os_hdr);
232 images.os.load = image_get_load(os_hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100233 break;
234#if defined(CONFIG_FIT)
235 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000236 if (fit_image_get_type(images.fit_hdr_os,
Kumar Gala396f6352008-08-15 08:24:41 -0500237 images.fit_noffset_os, &images.os.type)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000238 puts("Can't get image type!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000239 bootstage_error(BOOTSTAGE_ID_FIT_TYPE);
wdenk47d1a6e2002-11-03 00:01:44 +0000240 return 1;
241 }
wdenk47d1a6e2002-11-03 00:01:44 +0000242
Stephen Warren712fbcf2011-10-18 11:11:49 +0000243 if (fit_image_get_comp(images.fit_hdr_os,
Kumar Gala396f6352008-08-15 08:24:41 -0500244 images.fit_noffset_os, &images.os.comp)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000245 puts("Can't get image compression!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000246 bootstage_error(BOOTSTAGE_ID_FIT_COMPRESSION);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100247 return 1;
248 }
wdenk47d1a6e2002-11-03 00:01:44 +0000249
Stephen Warren712fbcf2011-10-18 11:11:49 +0000250 if (fit_image_get_os(images.fit_hdr_os,
Kumar Gala396f6352008-08-15 08:24:41 -0500251 images.fit_noffset_os, &images.os.os)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000252 puts("Can't get image OS!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000253 bootstage_error(BOOTSTAGE_ID_FIT_OS);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100254 return 1;
255 }
wdenk47d1a6e2002-11-03 00:01:44 +0000256
Stephen Warren712fbcf2011-10-18 11:11:49 +0000257 images.os.end = fit_get_end(images.fit_hdr_os);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100258
Stephen Warren712fbcf2011-10-18 11:11:49 +0000259 if (fit_image_get_load(images.fit_hdr_os, images.fit_noffset_os,
Kumar Gala396f6352008-08-15 08:24:41 -0500260 &images.os.load)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000261 puts("Can't get image load address!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000262 bootstage_error(BOOTSTAGE_ID_FIT_LOADADDR);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100263 return 1;
wdenkb13fb012003-10-30 21:49:38 +0000264 }
265 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100266#endif
267 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000268 puts("ERROR: unknown image format type!\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000269 return 1;
270 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100271
Kumar Galac160a952008-08-15 08:24:36 -0500272 /* find kernel entry point */
273 if (images.legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000274 images.ep = image_get_ep(&images.legacy_hdr_os_copy);
Kumar Galac160a952008-08-15 08:24:36 -0500275#if defined(CONFIG_FIT)
276 } else if (images.fit_uname_os) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000277 ret = fit_image_get_entry(images.fit_hdr_os,
Kumar Galac160a952008-08-15 08:24:36 -0500278 images.fit_noffset_os, &images.ep);
279 if (ret) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000280 puts("Can't get entry point property!\n");
Kumar Galac160a952008-08-15 08:24:36 -0500281 return 1;
282 }
283#endif
284 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000285 puts("Could not find kernel entry point!\n");
Kumar Galac160a952008-08-15 08:24:36 -0500286 return 1;
287 }
288
Stephen Warrenb9b50e892011-11-10 13:17:53 -0700289 if (images.os.type == IH_TYPE_KERNEL_NOLOAD) {
290 images.os.load = images.os.image_start;
291 images.ep += images.os.load;
292 }
293
Heiko Schocher24de2f42010-03-29 13:15:48 +0200294 if (((images.os.type == IH_TYPE_KERNEL) ||
Stephen Warrenb9b50e892011-11-10 13:17:53 -0700295 (images.os.type == IH_TYPE_KERNEL_NOLOAD) ||
Heiko Schocher24de2f42010-03-29 13:15:48 +0200296 (images.os.type == IH_TYPE_MULTI)) &&
Detlev Zundel8b828a82009-12-22 12:43:01 +0100297 (images.os.os == IH_OS_LINUX)) {
Kumar Galac4f94192008-08-15 08:24:37 -0500298 /* find ramdisk */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000299 ret = boot_get_ramdisk(argc, argv, &images, IH_INITRD_ARCH,
Kumar Galac4f94192008-08-15 08:24:37 -0500300 &images.rd_start, &images.rd_end);
301 if (ret) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000302 puts("Ramdisk image is corrupt or invalid\n");
Kumar Galac4f94192008-08-15 08:24:37 -0500303 return 1;
304 }
Kumar Gala06a09912008-08-15 08:24:38 -0500305
306#if defined(CONFIG_OF_LIBFDT)
307 /* find flattened device tree */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000308 ret = boot_get_fdt(flag, argc, argv, &images,
309 &images.ft_addr, &images.ft_len);
Kumar Gala06a09912008-08-15 08:24:38 -0500310 if (ret) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000311 puts("Could not find a valid device tree\n");
Kumar Gala06a09912008-08-15 08:24:38 -0500312 return 1;
313 }
Kumar Gala54f9c862008-08-15 08:24:39 -0500314
315 set_working_fdt_addr(images.ft_addr);
Kumar Gala06a09912008-08-15 08:24:38 -0500316#endif
Kumar Galac4f94192008-08-15 08:24:37 -0500317 }
318
Kumar Gala396f6352008-08-15 08:24:41 -0500319 images.os.start = (ulong)os_hdr;
Kumar Gala49c3a862008-10-21 17:25:45 -0500320 images.state = BOOTM_STATE_START;
Kumar Gala396f6352008-08-15 08:24:41 -0500321
322 return 0;
323}
324
325#define BOOTM_ERR_RESET -1
326#define BOOTM_ERR_OVERLAP -2
327#define BOOTM_ERR_UNIMPLEMENTED -3
328static int bootm_load_os(image_info_t os, ulong *load_end, int boot_progress)
329{
330 uint8_t comp = os.comp;
331 ulong load = os.load;
332 ulong blob_start = os.start;
333 ulong blob_end = os.end;
334 ulong image_start = os.image_start;
335 ulong image_len = os.image_len;
Anatolij Gustschin5bf27662011-11-19 13:12:18 +0000336 __maybe_unused uint unc_len = CONFIG_SYS_BOOTM_LEN;
Stephen Warrend5108592011-11-10 13:17:54 -0700337 int no_overlap = 0;
Matthias Weisser60fdc5f2010-08-05 13:17:30 +0200338#if defined(CONFIG_LZMA) || defined(CONFIG_LZO)
339 int ret;
340#endif /* defined(CONFIG_LZMA) || defined(CONFIG_LZO) */
Kumar Gala396f6352008-08-15 08:24:41 -0500341
Stephen Warren712fbcf2011-10-18 11:11:49 +0000342 const char *type_name = genimg_get_type_name(os.type);
Kumar Gala396f6352008-08-15 08:24:41 -0500343
344 switch (comp) {
345 case IH_COMP_NONE:
Lei Wen02cf3452011-01-10 18:21:15 +0800346 if (load == blob_start || load == image_start) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000347 printf(" XIP %s ... ", type_name);
Stephen Warrend5108592011-11-10 13:17:54 -0700348 no_overlap = 1;
Kumar Gala396f6352008-08-15 08:24:41 -0500349 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000350 printf(" Loading %s ... ", type_name);
351 memmove_wd((void *)load, (void *)image_start,
Larry Johnson54fa2c52010-04-20 08:09:43 -0400352 image_len, CHUNKSZ);
Kumar Gala396f6352008-08-15 08:24:41 -0500353 }
354 *load_end = load + image_len;
355 puts("OK\n");
356 break;
Mike Frysinger44431ca2010-01-21 19:30:36 -0500357#ifdef CONFIG_GZIP
Kumar Gala396f6352008-08-15 08:24:41 -0500358 case IH_COMP_GZIP:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000359 printf(" Uncompressing %s ... ", type_name);
360 if (gunzip((void *)load, unc_len,
361 (uchar *)image_start, &image_len) != 0) {
362 puts("GUNZIP: uncompress, out-of-mem or overwrite "
363 "error - must RESET board to recover\n");
Kumar Gala396f6352008-08-15 08:24:41 -0500364 if (boot_progress)
Simon Glass770605e2012-02-13 13:51:18 +0000365 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
Kumar Gala396f6352008-08-15 08:24:41 -0500366 return BOOTM_ERR_RESET;
367 }
368
369 *load_end = load + image_len;
370 break;
Mike Frysinger44431ca2010-01-21 19:30:36 -0500371#endif /* CONFIG_GZIP */
Kumar Gala396f6352008-08-15 08:24:41 -0500372#ifdef CONFIG_BZIP2
373 case IH_COMP_BZIP2:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000374 printf(" Uncompressing %s ... ", type_name);
Kumar Gala396f6352008-08-15 08:24:41 -0500375 /*
376 * If we've got less than 4 MB of malloc() space,
377 * use slower decompression algorithm which requires
378 * at most 2300 KB of memory.
379 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000380 int i = BZ2_bzBuffToBuffDecompress((char *)load,
Kumar Gala396f6352008-08-15 08:24:41 -0500381 &unc_len, (char *)image_start, image_len,
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200382 CONFIG_SYS_MALLOC_LEN < (4096 * 1024), 0);
Kumar Gala396f6352008-08-15 08:24:41 -0500383 if (i != BZ_OK) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000384 printf("BUNZIP2: uncompress or overwrite error %d "
Kumar Gala396f6352008-08-15 08:24:41 -0500385 "- must RESET board to recover\n", i);
386 if (boot_progress)
Simon Glass770605e2012-02-13 13:51:18 +0000387 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
Kumar Gala396f6352008-08-15 08:24:41 -0500388 return BOOTM_ERR_RESET;
389 }
390
391 *load_end = load + unc_len;
392 break;
393#endif /* CONFIG_BZIP2 */
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200394#ifdef CONFIG_LZMA
Mike Frysinger78e1e842010-07-25 15:54:17 -0400395 case IH_COMP_LZMA: {
396 SizeT lzma_len = unc_len;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000397 printf(" Uncompressing %s ... ", type_name);
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200398
Matthias Weisser60fdc5f2010-08-05 13:17:30 +0200399 ret = lzmaBuffToBuffDecompress(
Mike Frysinger78e1e842010-07-25 15:54:17 -0400400 (unsigned char *)load, &lzma_len,
Luigi 'Comio' Mantellinid977a572008-09-13 10:04:32 +0200401 (unsigned char *)image_start, image_len);
Mike Frysinger78e1e842010-07-25 15:54:17 -0400402 unc_len = lzma_len;
Luigi 'Comio' Mantellinicaf72ff2009-07-21 10:45:49 +0200403 if (ret != SZ_OK) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000404 printf("LZMA: uncompress or overwrite error %d "
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200405 "- must RESET board to recover\n", ret);
Simon Glass770605e2012-02-13 13:51:18 +0000406 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200407 return BOOTM_ERR_RESET;
408 }
409 *load_end = load + unc_len;
410 break;
Mike Frysinger78e1e842010-07-25 15:54:17 -0400411 }
Luigi 'Comio' Mantellinifc9c1722008-09-08 02:46:13 +0200412#endif /* CONFIG_LZMA */
Peter Korsgaard20dde482009-11-19 11:37:51 +0100413#ifdef CONFIG_LZO
414 case IH_COMP_LZO:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000415 printf(" Uncompressing %s ... ", type_name);
Peter Korsgaard20dde482009-11-19 11:37:51 +0100416
Matthias Weisser60fdc5f2010-08-05 13:17:30 +0200417 ret = lzop_decompress((const unsigned char *)image_start,
Peter Korsgaard20dde482009-11-19 11:37:51 +0100418 image_len, (unsigned char *)load,
419 &unc_len);
420 if (ret != LZO_E_OK) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000421 printf("LZO: uncompress or overwrite error %d "
Peter Korsgaard20dde482009-11-19 11:37:51 +0100422 "- must RESET board to recover\n", ret);
423 if (boot_progress)
Simon Glass770605e2012-02-13 13:51:18 +0000424 bootstage_error(BOOTSTAGE_ID_DECOMP_IMAGE);
Peter Korsgaard20dde482009-11-19 11:37:51 +0100425 return BOOTM_ERR_RESET;
426 }
427
428 *load_end = load + unc_len;
429 break;
430#endif /* CONFIG_LZO */
Kumar Gala396f6352008-08-15 08:24:41 -0500431 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000432 printf("Unimplemented compression type %d\n", comp);
Kumar Gala396f6352008-08-15 08:24:41 -0500433 return BOOTM_ERR_UNIMPLEMENTED;
434 }
Diana CRACIUN99ffccb2011-08-31 02:45:23 +0000435
436 flush_cache(load, (*load_end - load) * sizeof(ulong));
437
Stephen Warren712fbcf2011-10-18 11:11:49 +0000438 puts("OK\n");
439 debug(" kernel loaded at 0x%08lx, end = 0x%08lx\n", load, *load_end);
Simon Glass770605e2012-02-13 13:51:18 +0000440 bootstage_mark(BOOTSTAGE_ID_KERNEL_LOADED);
Kumar Gala396f6352008-08-15 08:24:41 -0500441
Stephen Warrend5108592011-11-10 13:17:54 -0700442 if (!no_overlap && (load < blob_end) && (*load_end > blob_start)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000443 debug("images.os.start = 0x%lX, images.os.end = 0x%lx\n",
444 blob_start, blob_end);
445 debug("images.os.load = 0x%lx, load_end = 0x%lx\n", load,
446 *load_end);
Kumar Gala396f6352008-08-15 08:24:41 -0500447
448 return BOOTM_ERR_OVERLAP;
449 }
450
451 return 0;
452}
453
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200454static int bootm_start_standalone(ulong iflag, int argc, char * const argv[])
Detlev Zundelf97ec302009-07-13 16:01:19 +0200455{
456 char *s;
Wolfgang Denk54841ab2010-06-28 22:00:46 +0200457 int (*appl)(int, char * const []);
Detlev Zundelf97ec302009-07-13 16:01:19 +0200458
459 /* Don't start if "autostart" is set to "no" */
460 if (((s = getenv("autostart")) != NULL) && (strcmp(s, "no") == 0)) {
Simon Glass41ef3722013-02-24 17:33:22 +0000461 setenv_hex("filesize", images.os.image_len);
Detlev Zundelf97ec302009-07-13 16:01:19 +0200462 return 0;
463 }
Simon Glass6d6f1232011-10-07 13:53:40 +0000464 appl = (int (*)(int, char * const []))(ulong)ntohl(images.ep);
Detlev Zundelf97ec302009-07-13 16:01:19 +0200465 (*appl)(argc-1, &argv[1]);
Detlev Zundelf97ec302009-07-13 16:01:19 +0200466 return 0;
467}
468
Kumar Gala49c3a862008-10-21 17:25:45 -0500469/* we overload the cmd field with our state machine info instead of a
470 * function pointer */
Frans Meulenbroeksf74d9bd2010-02-25 10:12:13 +0100471static cmd_tbl_t cmd_bootm_sub[] = {
Kumar Gala49c3a862008-10-21 17:25:45 -0500472 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
473 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
John Rigbyfca43cc2010-10-13 13:57:35 -0600474#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Kumar Gala49c3a862008-10-21 17:25:45 -0500475 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
476#endif
477#ifdef CONFIG_OF_LIBFDT
478 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
479#endif
Kumar Gala49c3a862008-10-21 17:25:45 -0500480 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
Peter Tyser224c90d2009-11-18 19:08:59 -0600481 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
Kumar Gala49c3a862008-10-21 17:25:45 -0500482 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
483 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
484};
485
Kim Phillips088f1b12012-10-29 13:34:31 +0000486static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
Stephen Warren712fbcf2011-10-18 11:11:49 +0000487 char * const argv[])
Kumar Gala49c3a862008-10-21 17:25:45 -0500488{
489 int ret = 0;
Simon Glass6d6f1232011-10-07 13:53:40 +0000490 long state;
Kumar Gala49c3a862008-10-21 17:25:45 -0500491 cmd_tbl_t *c;
492 boot_os_fn *boot_fn;
493
494 c = find_cmd_tbl(argv[1], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
495
496 if (c) {
Simon Glass6d6f1232011-10-07 13:53:40 +0000497 state = (long)c->cmd;
Kumar Gala49c3a862008-10-21 17:25:45 -0500498
499 /* treat start special since it resets the state machine */
500 if (state == BOOTM_STATE_START) {
501 argc--;
502 argv++;
503 return bootm_start(cmdtp, flag, argc, argv);
504 }
Wolfgang Denk47e26b12010-07-17 01:06:04 +0200505 } else {
506 /* Unrecognized command */
Simon Glass4c12eeb2011-12-10 08:44:01 +0000507 return CMD_RET_USAGE;
Kumar Gala49c3a862008-10-21 17:25:45 -0500508 }
509
Gabor Juhos0f307342013-01-06 23:04:25 +0000510 if (images.state < BOOTM_STATE_START ||
511 images.state >= state) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000512 printf("Trying to execute a command out of order\n");
Simon Glass4c12eeb2011-12-10 08:44:01 +0000513 return CMD_RET_USAGE;
Kumar Gala49c3a862008-10-21 17:25:45 -0500514 }
515
516 images.state |= state;
517 boot_fn = boot_os[images.os.os];
518
519 switch (state) {
520 ulong load_end;
521 case BOOTM_STATE_START:
522 /* should never occur */
523 break;
524 case BOOTM_STATE_LOADOS:
525 ret = bootm_load_os(images.os, &load_end, 0);
526 if (ret)
527 return ret;
528
529 lmb_reserve(&images.lmb, images.os.load,
530 (load_end - images.os.load));
531 break;
John Rigbyfca43cc2010-10-13 13:57:35 -0600532#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Kumar Gala49c3a862008-10-21 17:25:45 -0500533 case BOOTM_STATE_RAMDISK:
534 {
535 ulong rd_len = images.rd_end - images.rd_start;
Kumar Gala49c3a862008-10-21 17:25:45 -0500536
537 ret = boot_ramdisk_high(&images.lmb, images.rd_start,
538 rd_len, &images.initrd_start, &images.initrd_end);
539 if (ret)
540 return ret;
541
Simon Glass41ef3722013-02-24 17:33:22 +0000542 setenv_hex("initrd_start", images.initrd_start);
543 setenv_hex("initrd_end", images.initrd_end);
Kumar Gala49c3a862008-10-21 17:25:45 -0500544 }
545 break;
546#endif
Simon Glass75b3afc2012-12-05 14:46:31 +0000547#if defined(CONFIG_OF_LIBFDT) && defined(CONFIG_LMB)
Kumar Gala49c3a862008-10-21 17:25:45 -0500548 case BOOTM_STATE_FDT:
549 {
Grant Likely55b0a392011-03-28 09:59:01 +0000550 boot_fdt_add_mem_rsv_regions(&images.lmb,
551 images.ft_addr);
Grant Likely590d3ca2011-03-28 09:58:34 +0000552 ret = boot_relocate_fdt(&images.lmb,
Kumar Gala49c3a862008-10-21 17:25:45 -0500553 &images.ft_addr, &images.ft_len);
554 break;
555 }
556#endif
557 case BOOTM_STATE_OS_CMDLINE:
558 ret = boot_fn(BOOTM_STATE_OS_CMDLINE, argc, argv, &images);
559 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000560 printf("cmdline subcommand not supported\n");
Kumar Gala49c3a862008-10-21 17:25:45 -0500561 break;
562 case BOOTM_STATE_OS_BD_T:
563 ret = boot_fn(BOOTM_STATE_OS_BD_T, argc, argv, &images);
564 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000565 printf("bdt subcommand not supported\n");
Kumar Gala49c3a862008-10-21 17:25:45 -0500566 break;
567 case BOOTM_STATE_OS_PREP:
568 ret = boot_fn(BOOTM_STATE_OS_PREP, argc, argv, &images);
569 if (ret)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000570 printf("prep subcommand not supported\n");
Kumar Gala49c3a862008-10-21 17:25:45 -0500571 break;
572 case BOOTM_STATE_OS_GO:
573 disable_interrupts();
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000574#ifdef CONFIG_NETCONSOLE
575 /*
576 * Stop the ethernet stack if NetConsole could have
577 * left it up
578 */
579 eth_halt();
580#endif
Kumar Gala3dfad402009-08-27 08:23:55 -0500581 arch_preboot_os();
Kumar Gala49c3a862008-10-21 17:25:45 -0500582 boot_fn(BOOTM_STATE_OS_GO, argc, argv, &images);
583 break;
584 }
585
586 return ret;
587}
588
Kumar Gala396f6352008-08-15 08:24:41 -0500589/*******************************************************************/
590/* bootm - boot application image from image in memory */
591/*******************************************************************/
Kumar Galabe083152008-10-21 17:25:44 -0500592
Stephen Warren712fbcf2011-10-18 11:11:49 +0000593int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Kumar Gala396f6352008-08-15 08:24:41 -0500594{
Kumar Gala396f6352008-08-15 08:24:41 -0500595 ulong iflag;
596 ulong load_end = 0;
597 int ret;
Kumar Galabe083152008-10-21 17:25:44 -0500598 boot_os_fn *boot_fn;
Wolfgang Denk2e5167c2010-10-28 20:00:11 +0200599#ifdef CONFIG_NEEDS_MANUAL_RELOC
Peter Tyser521af042009-09-21 11:20:36 -0500600 static int relocated = 0;
Kumar Galabe083152008-10-21 17:25:44 -0500601
Kumar Galabe083152008-10-21 17:25:44 -0500602 if (!relocated) {
603 int i;
Daniel Schwierzeck58bd77d2013-01-07 06:54:52 +0000604
605 /* relocate boot function table */
Kumar Galabe083152008-10-21 17:25:44 -0500606 for (i = 0; i < ARRAY_SIZE(boot_os); i++)
Detlev Zundelca95c9d2009-07-13 16:01:18 +0200607 if (boot_os[i] != NULL)
608 boot_os[i] += gd->reloc_off;
Daniel Schwierzeck58bd77d2013-01-07 06:54:52 +0000609
610 /* relocate names of sub-command table */
611 for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
612 cmd_bootm_sub[i].name += gd->reloc_off;
613
Kumar Galabe083152008-10-21 17:25:44 -0500614 relocated = 1;
615 }
Peter Tyser521af042009-09-21 11:20:36 -0500616#endif
Kumar Gala396f6352008-08-15 08:24:41 -0500617
Kumar Gala49c3a862008-10-21 17:25:45 -0500618 /* determine if we have a sub command */
619 if (argc > 1) {
620 char *endp;
621
622 simple_strtoul(argv[1], &endp, 16);
623 /* endp pointing to NULL means that argv[1] was just a
624 * valid number, pass it along to the normal bootm processing
625 *
626 * If endp is ':' or '#' assume a FIT identifier so pass
627 * along for normal processing.
628 *
629 * Right now we assume the first arg should never be '-'
630 */
631 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
632 return do_bootm_subcommand(cmdtp, flag, argc, argv);
633 }
634
Anatolij Gustschin8e024942008-08-29 21:04:45 +0200635 if (bootm_start(cmdtp, flag, argc, argv))
636 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000637
638 /*
639 * We have reached the point of no return: we are going to
640 * overwrite all exception vector code, so we cannot easily
641 * recover from any failures any more...
642 */
wdenk47d1a6e2002-11-03 00:01:44 +0000643 iflag = disable_interrupts();
644
Joe Hershbergerf8be7d62012-08-03 10:59:08 +0000645#ifdef CONFIG_NETCONSOLE
646 /* Stop the ethernet stack if NetConsole could have left it up */
647 eth_halt();
648#endif
649
Stefan Roeseebb86c42008-07-30 09:59:51 +0200650#if defined(CONFIG_CMD_USB)
Wolfgang Denk699f0512008-07-15 22:22:44 +0200651 /*
652 * turn off USB to prevent the host controller from writing to the
653 * SDRAM while Linux is booting. This could happen (at least for OHCI
654 * controller), because the HCCA (Host Controller Communication Area)
655 * lies within the SDRAM and the host controller writes continously to
656 * this area (as busmaster!). The HccaFrameNumber is for example
657 * updated every 1 ms within the HCCA structure in SDRAM! For more
658 * details see the OpenHCI specification.
659 */
Markus Klotzbücher3d71c812008-07-10 14:47:09 +0200660 usb_stop();
661#endif
662
Kumar Gala396f6352008-08-15 08:24:41 -0500663 ret = bootm_load_os(images.os, &load_end, 1);
wdenk47d1a6e2002-11-03 00:01:44 +0000664
Kumar Gala396f6352008-08-15 08:24:41 -0500665 if (ret < 0) {
666 if (ret == BOOTM_ERR_RESET)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000667 do_reset(cmdtp, flag, argc, argv);
Kumar Gala396f6352008-08-15 08:24:41 -0500668 if (ret == BOOTM_ERR_OVERLAP) {
669 if (images.legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000670 image_header_t *hdr;
671 hdr = &images.legacy_hdr_os_copy;
672 if (image_get_type(hdr) == IH_TYPE_MULTI)
673 puts("WARNING: legacy format multi "
674 "component image "
675 "overwritten\n");
Kumar Gala396f6352008-08-15 08:24:41 -0500676 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000677 puts("ERROR: new format image overwritten - "
Kumar Gala396f6352008-08-15 08:24:41 -0500678 "must RESET the board to recover\n");
Simon Glass770605e2012-02-13 13:51:18 +0000679 bootstage_error(BOOTSTAGE_ID_OVERWRITTEN);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000680 do_reset(cmdtp, flag, argc, argv);
Kumar Gala396f6352008-08-15 08:24:41 -0500681 }
wdenk47d1a6e2002-11-03 00:01:44 +0000682 }
Kumar Gala396f6352008-08-15 08:24:41 -0500683 if (ret == BOOTM_ERR_UNIMPLEMENTED) {
684 if (iflag)
685 enable_interrupts();
Simon Glass770605e2012-02-13 13:51:18 +0000686 bootstage_error(BOOTSTAGE_ID_DECOMP_UNIMPL);
Kumar Gala396f6352008-08-15 08:24:41 -0500687 return 1;
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200688 }
wdenk47d1a6e2002-11-03 00:01:44 +0000689 }
Marian Balakowicz75824382008-01-31 13:20:06 +0100690
Kumar Gala396f6352008-08-15 08:24:41 -0500691 lmb_reserve(&images.lmb, images.os.load, (load_end - images.os.load));
692
Detlev Zundelf97ec302009-07-13 16:01:19 +0200693 if (images.os.type == IH_TYPE_STANDALONE) {
694 if (iflag)
695 enable_interrupts();
696 /* This may return when 'autostart' is 'no' */
697 bootm_start_standalone(iflag, argc, argv);
698 return 0;
699 }
700
Simon Glass770605e2012-02-13 13:51:18 +0000701 bootstage_mark(BOOTSTAGE_ID_CHECK_BOOT_OS);
wdenk47d1a6e2002-11-03 00:01:44 +0000702
Joe Hershbergerbe2e5a02013-02-27 10:20:59 +0000703#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
Kumar Galabe083152008-10-21 17:25:44 -0500704 if (images.os.os == IH_OS_LINUX)
705 fixup_silent_linux();
wdenk1f4bb372003-07-27 00:21:01 +0000706#endif
707
Kumar Galabe083152008-10-21 17:25:44 -0500708 boot_fn = boot_os[images.os.os];
Detlev Zundelca95c9d2009-07-13 16:01:18 +0200709
710 if (boot_fn == NULL) {
711 if (iflag)
712 enable_interrupts();
Stephen Warren712fbcf2011-10-18 11:11:49 +0000713 printf("ERROR: booting os '%s' (%d) is not supported\n",
Detlev Zundelca95c9d2009-07-13 16:01:18 +0200714 genimg_get_os_name(images.os.os), images.os.os);
Simon Glass770605e2012-02-13 13:51:18 +0000715 bootstage_error(BOOTSTAGE_ID_CHECK_BOOT_OS);
Detlev Zundelca95c9d2009-07-13 16:01:18 +0200716 return 1;
717 }
718
Kumar Gala3dfad402009-08-27 08:23:55 -0500719 arch_preboot_os();
720
Kumar Galabe083152008-10-21 17:25:44 -0500721 boot_fn(0, argc, argv, &images);
wdenk47d1a6e2002-11-03 00:01:44 +0000722
Simon Glass770605e2012-02-13 13:51:18 +0000723 bootstage_error(BOOTSTAGE_ID_BOOT_OS_RETURNED);
wdenk47d1a6e2002-11-03 00:01:44 +0000724#ifdef DEBUG
Stephen Warren712fbcf2011-10-18 11:11:49 +0000725 puts("\n## Control returned to monitor - resetting...\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000726#endif
Stephen Warren712fbcf2011-10-18 11:11:49 +0000727 do_reset(cmdtp, flag, argc, argv);
Marian Balakowicza44a2692008-03-12 10:14:57 +0100728
wdenk47d1a6e2002-11-03 00:01:44 +0000729 return 1;
730}
731
Mike Frysinger67d668b2011-06-05 13:43:02 +0000732int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
733{
734 const char *ep = getenv("autostart");
735
736 if (ep && !strcmp(ep, "yes")) {
737 char *local_args[2];
738 local_args[0] = (char *)cmd;
739 local_args[1] = NULL;
740 printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
741 return do_bootm(cmdtp, 0, 1, local_args);
742 }
743
744 return 0;
745}
746
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100747/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100748 * image_get_kernel - verify legacy format kernel image
749 * @img_addr: in RAM address of the legacy format image to be verified
750 * @verify: data CRC verification flag
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100751 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100752 * image_get_kernel() verifies legacy image integrity and returns pointer to
753 * legacy image header if image verification was completed successfully.
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100754 *
755 * returns:
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100756 * pointer to a legacy image header if valid image was found
757 * otherwise return NULL
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100758 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000759static image_header_t *image_get_kernel(ulong img_addr, int verify)
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100760{
761 image_header_t *hdr = (image_header_t *)img_addr;
762
763 if (!image_check_magic(hdr)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000764 puts("Bad Magic Number\n");
Simon Glass770605e2012-02-13 13:51:18 +0000765 bootstage_error(BOOTSTAGE_ID_CHECK_MAGIC);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100766 return NULL;
767 }
Simon Glass770605e2012-02-13 13:51:18 +0000768 bootstage_mark(BOOTSTAGE_ID_CHECK_HEADER);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100769
Stephen Warren712fbcf2011-10-18 11:11:49 +0000770 if (!image_check_hcrc(hdr)) {
771 puts("Bad Header Checksum\n");
Simon Glass770605e2012-02-13 13:51:18 +0000772 bootstage_error(BOOTSTAGE_ID_CHECK_HEADER);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100773 return NULL;
774 }
775
Simon Glass770605e2012-02-13 13:51:18 +0000776 bootstage_mark(BOOTSTAGE_ID_CHECK_CHECKSUM);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000777 image_print_contents(hdr);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100778
779 if (verify) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000780 puts(" Verifying Checksum ... ");
781 if (!image_check_dcrc(hdr)) {
782 printf("Bad Data CRC\n");
Simon Glass770605e2012-02-13 13:51:18 +0000783 bootstage_error(BOOTSTAGE_ID_CHECK_CHECKSUM);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100784 return NULL;
785 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000786 puts("OK\n");
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100787 }
Simon Glass770605e2012-02-13 13:51:18 +0000788 bootstage_mark(BOOTSTAGE_ID_CHECK_ARCH);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100789
Stephen Warren712fbcf2011-10-18 11:11:49 +0000790 if (!image_check_target_arch(hdr)) {
791 printf("Unsupported Architecture 0x%x\n", image_get_arch(hdr));
Simon Glass770605e2012-02-13 13:51:18 +0000792 bootstage_error(BOOTSTAGE_ID_CHECK_ARCH);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100793 return NULL;
794 }
795 return hdr;
796}
797
798/**
Marian Balakowicz6986a382008-03-12 10:01:05 +0100799 * fit_check_kernel - verify FIT format kernel subimage
800 * @fit_hdr: pointer to the FIT image header
801 * os_noffset: kernel subimage node offset within FIT image
802 * @verify: data CRC verification flag
803 *
804 * fit_check_kernel() verifies integrity of the kernel subimage and from
805 * specified FIT image.
806 *
807 * returns:
808 * 1, on success
809 * 0, on failure
810 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000811#if defined(CONFIG_FIT)
812static int fit_check_kernel(const void *fit, int os_noffset, int verify)
Marian Balakowicz6986a382008-03-12 10:01:05 +0100813{
Stephen Warren712fbcf2011-10-18 11:11:49 +0000814 fit_image_print(fit, os_noffset, " ");
Marian Balakowicz6986a382008-03-12 10:01:05 +0100815
816 if (verify) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000817 puts(" Verifying Hash Integrity ... ");
818 if (!fit_image_check_hashes(fit, os_noffset)) {
819 puts("Bad Data Hash\n");
Simon Glass770605e2012-02-13 13:51:18 +0000820 bootstage_error(BOOTSTAGE_ID_FIT_CHECK_HASH);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100821 return 0;
822 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000823 puts("OK\n");
Marian Balakowicz6986a382008-03-12 10:01:05 +0100824 }
Simon Glass770605e2012-02-13 13:51:18 +0000825 bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_ARCH);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100826
Stephen Warren712fbcf2011-10-18 11:11:49 +0000827 if (!fit_image_check_target_arch(fit, os_noffset)) {
828 puts("Unsupported Architecture\n");
Simon Glass770605e2012-02-13 13:51:18 +0000829 bootstage_error(BOOTSTAGE_ID_FIT_CHECK_ARCH);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100830 return 0;
831 }
832
Simon Glass770605e2012-02-13 13:51:18 +0000833 bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_KERNEL);
Stephen Warrenb9b50e892011-11-10 13:17:53 -0700834 if (!fit_image_check_type(fit, os_noffset, IH_TYPE_KERNEL) &&
835 !fit_image_check_type(fit, os_noffset, IH_TYPE_KERNEL_NOLOAD)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000836 puts("Not a kernel image\n");
Simon Glass770605e2012-02-13 13:51:18 +0000837 bootstage_error(BOOTSTAGE_ID_FIT_CHECK_KERNEL);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100838 return 0;
839 }
840
Simon Glass770605e2012-02-13 13:51:18 +0000841 bootstage_mark(BOOTSTAGE_ID_FIT_CHECKED);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100842 return 1;
843}
844#endif /* CONFIG_FIT */
845
846/**
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100847 * boot_get_kernel - find kernel image
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100848 * @os_data: pointer to a ulong variable, will hold os data start address
849 * @os_len: pointer to a ulong variable, will hold os data length
850 *
Marian Balakowicz9a4daad2008-02-29 14:58:34 +0100851 * boot_get_kernel() tries to find a kernel image, verifies its integrity
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100852 * and locates kernel data.
853 *
854 * returns:
855 * pointer to image header if valid image was found, plus kernel start
856 * address and length, otherwise NULL
857 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000858static void *boot_get_kernel(cmd_tbl_t *cmdtp, int flag, int argc,
859 char * const argv[], bootm_headers_t *images, ulong *os_data,
860 ulong *os_len)
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100861{
862 image_header_t *hdr;
863 ulong img_addr;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100864#if defined(CONFIG_FIT)
865 void *fit_hdr;
866 const char *fit_uname_config = NULL;
867 const char *fit_uname_kernel = NULL;
Marian Balakowicz6986a382008-03-12 10:01:05 +0100868 const void *data;
869 size_t len;
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100870 int cfg_noffset;
Marian Balakowicz6986a382008-03-12 10:01:05 +0100871 int os_noffset;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100872#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100873
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100874 /* find out kernel image address */
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100875 if (argc < 2) {
876 img_addr = load_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000877 debug("* kernel: default image load address = 0x%08lx\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100878 load_addr);
879#if defined(CONFIG_FIT)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000880 } else if (fit_parse_conf(argv[1], load_addr, &img_addr,
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100881 &fit_uname_config)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000882 debug("* kernel: config '%s' from image at 0x%08lx\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100883 fit_uname_config, img_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000884 } else if (fit_parse_subimage(argv[1], load_addr, &img_addr,
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100885 &fit_uname_kernel)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000886 debug("* kernel: subimage '%s' from image at 0x%08lx\n",
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100887 fit_uname_kernel, img_addr);
888#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100889 } else {
890 img_addr = simple_strtoul(argv[1], NULL, 16);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000891 debug("* kernel: cmdline image address = 0x%08lx\n", img_addr);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100892 }
893
Simon Glass770605e2012-02-13 13:51:18 +0000894 bootstage_mark(BOOTSTAGE_ID_CHECK_MAGIC);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100895
Marian Balakowiczfff888a12008-02-21 17:20:19 +0100896 /* copy from dataflash if needed */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000897 img_addr = genimg_get_image(img_addr);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100898
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100899 /* check image type, for FIT images get FIT kernel node */
Marian Balakowicz6986a382008-03-12 10:01:05 +0100900 *os_data = *os_len = 0;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000901 switch (genimg_get_format((void *)img_addr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100902 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000903 printf("## Booting kernel from Legacy Image at %08lx ...\n",
Marian Balakowicz6986a382008-03-12 10:01:05 +0100904 img_addr);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000905 hdr = image_get_kernel(img_addr, images->verify);
Marian Balakowicz1efd4362008-02-27 11:02:07 +0100906 if (!hdr)
Marian Balakowicz5cf746c2008-01-31 13:59:09 +0100907 return NULL;
Simon Glass770605e2012-02-13 13:51:18 +0000908 bootstage_mark(BOOTSTAGE_ID_CHECK_IMAGETYPE);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100909
Marian Balakowicz6986a382008-03-12 10:01:05 +0100910 /* get os_data and os_len */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000911 switch (image_get_type(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100912 case IH_TYPE_KERNEL:
Stephen Warrenb9b50e892011-11-10 13:17:53 -0700913 case IH_TYPE_KERNEL_NOLOAD:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000914 *os_data = image_get_data(hdr);
915 *os_len = image_get_data_size(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100916 break;
917 case IH_TYPE_MULTI:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000918 image_multi_getimg(hdr, 0, os_data, os_len);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100919 break;
Detlev Zundelf97ec302009-07-13 16:01:19 +0200920 case IH_TYPE_STANDALONE:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000921 *os_data = image_get_data(hdr);
922 *os_len = image_get_data_size(hdr);
Detlev Zundelf97ec302009-07-13 16:01:19 +0200923 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100924 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000925 printf("Wrong Image Type for %s command\n",
926 cmdtp->name);
Simon Glass770605e2012-02-13 13:51:18 +0000927 bootstage_error(BOOTSTAGE_ID_CHECK_IMAGETYPE);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100928 return NULL;
929 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100930
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200931 /*
Stephen Warren712fbcf2011-10-18 11:11:49 +0000932 * copy image header to allow for image overwrites during
933 * kernel decompression.
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200934 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000935 memmove(&images->legacy_hdr_os_copy, hdr,
936 sizeof(image_header_t));
Marian Balakowiczcb1c4892008-04-11 11:07:49 +0200937
938 /* save pointer to image header */
939 images->legacy_hdr_os = hdr;
940
941 images->legacy_hdr_valid = 1;
Simon Glass770605e2012-02-13 13:51:18 +0000942 bootstage_mark(BOOTSTAGE_ID_DECOMP_IMAGE);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100943 break;
944#if defined(CONFIG_FIT)
945 case IMAGE_FORMAT_FIT:
946 fit_hdr = (void *)img_addr;
Stephen Warren712fbcf2011-10-18 11:11:49 +0000947 printf("## Booting kernel from FIT Image at %08lx ...\n",
Marian Balakowicz6986a382008-03-12 10:01:05 +0100948 img_addr);
949
Stephen Warren712fbcf2011-10-18 11:11:49 +0000950 if (!fit_check_format(fit_hdr)) {
951 puts("Bad FIT kernel image format!\n");
Simon Glass770605e2012-02-13 13:51:18 +0000952 bootstage_error(BOOTSTAGE_ID_FIT_FORMAT);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100953 return NULL;
954 }
Simon Glass770605e2012-02-13 13:51:18 +0000955 bootstage_mark(BOOTSTAGE_ID_FIT_FORMAT);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100956
957 if (!fit_uname_kernel) {
958 /*
959 * no kernel image node unit name, try to get config
960 * node first. If config unit node name is NULL
Stephen Warren712fbcf2011-10-18 11:11:49 +0000961 * fit_conf_get_node() will try to find default config
962 * node
Marian Balakowicz6986a382008-03-12 10:01:05 +0100963 */
Simon Glass770605e2012-02-13 13:51:18 +0000964 bootstage_mark(BOOTSTAGE_ID_FIT_NO_UNIT_NAME);
Gabe Blackd95f6ec2012-10-25 16:31:10 +0000965#ifdef CONFIG_FIT_BEST_MATCH
966 if (fit_uname_config)
967 cfg_noffset =
968 fit_conf_get_node(fit_hdr,
969 fit_uname_config);
970 else
971 cfg_noffset =
972 fit_conf_find_compat(fit_hdr,
973 gd->fdt_blob);
974#else
Stephen Warren712fbcf2011-10-18 11:11:49 +0000975 cfg_noffset = fit_conf_get_node(fit_hdr,
976 fit_uname_config);
Gabe Blackd95f6ec2012-10-25 16:31:10 +0000977#endif
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100978 if (cfg_noffset < 0) {
Simon Glass770605e2012-02-13 13:51:18 +0000979 bootstage_error(BOOTSTAGE_ID_FIT_NO_UNIT_NAME);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100980 return NULL;
Marian Balakowicz1372cce2008-03-12 10:33:01 +0100981 }
Marian Balakowiczf773bea2008-03-12 10:35:46 +0100982 /* save configuration uname provided in the first
983 * bootm argument
984 */
Stephen Warren712fbcf2011-10-18 11:11:49 +0000985 images->fit_uname_cfg = fdt_get_name(fit_hdr,
986 cfg_noffset,
987 NULL);
988 printf(" Using '%s' configuration\n",
989 images->fit_uname_cfg);
Simon Glass770605e2012-02-13 13:51:18 +0000990 bootstage_mark(BOOTSTAGE_ID_FIT_CONFIG);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100991
Stephen Warren712fbcf2011-10-18 11:11:49 +0000992 os_noffset = fit_conf_get_kernel_node(fit_hdr,
993 cfg_noffset);
994 fit_uname_kernel = fit_get_name(fit_hdr, os_noffset,
995 NULL);
Marian Balakowicz6986a382008-03-12 10:01:05 +0100996 } else {
997 /* get kernel component image node offset */
Simon Glass770605e2012-02-13 13:51:18 +0000998 bootstage_mark(BOOTSTAGE_ID_FIT_UNIT_NAME);
Stephen Warren712fbcf2011-10-18 11:11:49 +0000999 os_noffset = fit_image_get_node(fit_hdr,
1000 fit_uname_kernel);
Marian Balakowicz6986a382008-03-12 10:01:05 +01001001 }
Marian Balakowicz1372cce2008-03-12 10:33:01 +01001002 if (os_noffset < 0) {
Simon Glass770605e2012-02-13 13:51:18 +00001003 bootstage_error(BOOTSTAGE_ID_FIT_CONFIG);
Marian Balakowicz6986a382008-03-12 10:01:05 +01001004 return NULL;
Marian Balakowicz1372cce2008-03-12 10:33:01 +01001005 }
Marian Balakowicz6986a382008-03-12 10:01:05 +01001006
Stephen Warren712fbcf2011-10-18 11:11:49 +00001007 printf(" Trying '%s' kernel subimage\n", fit_uname_kernel);
Marian Balakowicz6986a382008-03-12 10:01:05 +01001008
Simon Glass770605e2012-02-13 13:51:18 +00001009 bootstage_mark(BOOTSTAGE_ID_FIT_CHECK_SUBIMAGE);
Stephen Warren712fbcf2011-10-18 11:11:49 +00001010 if (!fit_check_kernel(fit_hdr, os_noffset, images->verify))
Marian Balakowicz6986a382008-03-12 10:01:05 +01001011 return NULL;
1012
1013 /* get kernel image data address and length */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001014 if (fit_image_get_data(fit_hdr, os_noffset, &data, &len)) {
1015 puts("Could not find kernel subimage data!\n");
Simon Glass770605e2012-02-13 13:51:18 +00001016 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO_ERR);
Marian Balakowicz6986a382008-03-12 10:01:05 +01001017 return NULL;
1018 }
Simon Glass770605e2012-02-13 13:51:18 +00001019 bootstage_mark(BOOTSTAGE_ID_FIT_KERNEL_INFO);
Marian Balakowicz6986a382008-03-12 10:01:05 +01001020
1021 *os_len = len;
1022 *os_data = (ulong)data;
1023 images->fit_hdr_os = fit_hdr;
1024 images->fit_uname_os = fit_uname_kernel;
Marian Balakowicz3dfe1102008-03-12 10:32:59 +01001025 images->fit_noffset_os = os_noffset;
Marian Balakowicz6986a382008-03-12 10:01:05 +01001026 break;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001027#endif
Marian Balakowicz5cf746c2008-01-31 13:59:09 +01001028 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001029 printf("Wrong Image Format for %s command\n", cmdtp->name);
Simon Glass770605e2012-02-13 13:51:18 +00001030 bootstage_error(BOOTSTAGE_ID_FIT_KERNEL_INFO);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +01001031 return NULL;
1032 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001033
Stephen Warren712fbcf2011-10-18 11:11:49 +00001034 debug(" kernel data at 0x%08lx, len = 0x%08lx (%ld)\n",
Marian Balakowicz6986a382008-03-12 10:01:05 +01001035 *os_data, *os_len, *os_len);
Marian Balakowicz5cf746c2008-01-31 13:59:09 +01001036
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001037 return (void *)img_addr;
Marian Balakowicz5cf746c2008-01-31 13:59:09 +01001038}
1039
Kim Phillips088f1b12012-10-29 13:34:31 +00001040#ifdef CONFIG_SYS_LONGHELP
1041static char bootm_help_text[] =
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001042 "[addr [arg ...]]\n - boot application image stored in memory\n"
1043 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
1044 "\t'arg' can be the address of an initrd image\n"
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +01001045#if defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -05001046 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
Detlev Zundel5441f612007-10-19 16:47:26 +02001047 "\ta third argument is required which is the address of the\n"
Matthew McClintock98a9c4d2006-06-28 10:41:37 -05001048 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1049 "\tuse a '-' for the second argument. If you do not pass a third\n"
1050 "\ta bd_info struct will be passed instead\n"
1051#endif
Marian Balakowicz6986a382008-03-12 10:01:05 +01001052#if defined(CONFIG_FIT)
1053 "\t\nFor the new multi component uImage format (FIT) addresses\n"
1054 "\tmust be extened to include component or configuration unit name:\n"
1055 "\taddr:<subimg_uname> - direct component image specification\n"
1056 "\taddr#<conf_uname> - configuration specification\n"
1057 "\tUse iminfo command to get the list of existing component\n"
1058 "\timages and configurations.\n"
1059#endif
Kumar Gala49c3a862008-10-21 17:25:45 -05001060 "\nSub-commands to do part of the bootm sequence. The sub-commands "
1061 "must be\n"
1062 "issued in the order below (it's ok to not issue all sub-commands):\n"
1063 "\tstart [addr [arg ...]]\n"
1064 "\tloados - load OS image\n"
Daniel Schwierzeck59af76d2013-02-26 04:54:19 +00001065#if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
Kumar Gala49c3a862008-10-21 17:25:45 -05001066 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
1067#endif
1068#if defined(CONFIG_OF_LIBFDT)
1069 "\tfdt - relocate flat device tree\n"
1070#endif
Kumar Gala49c3a862008-10-21 17:25:45 -05001071 "\tcmdline - OS specific command line processing/setup\n"
Peter Tyser224c90d2009-11-18 19:08:59 -06001072 "\tbdt - OS specific bd_t processing\n"
Kumar Gala49c3a862008-10-21 17:25:45 -05001073 "\tprep - OS specific prep before relocation or go\n"
Kim Phillips088f1b12012-10-29 13:34:31 +00001074 "\tgo - start OS";
1075#endif
1076
1077U_BOOT_CMD(
1078 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
1079 "boot application image from memory", bootm_help_text
wdenk8bde7f72003-06-27 21:31:46 +00001080);
1081
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001082/*******************************************************************/
1083/* bootd - boot default image */
1084/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001085#if defined(CONFIG_CMD_BOOTD)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001086int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk47d1a6e2002-11-03 00:01:44 +00001087{
1088 int rcode = 0;
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001089
Simon Glass53071532012-02-14 19:59:21 +00001090 if (run_command(getenv("bootcmd"), flag) < 0)
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001091 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001092 return rcode;
1093}
wdenk8bde7f72003-06-27 21:31:46 +00001094
wdenk0d498392003-07-01 21:06:45 +00001095U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001096 boot, 1, 1, do_bootd,
Peter Tyser2fb26042009-01-27 18:03:12 -06001097 "boot default, i.e., run 'bootcmd'",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001098 ""
wdenk9d2b18a2003-06-28 23:11:04 +00001099);
1100
1101/* keep old command name "bootd" for backward compatibility */
wdenk0d498392003-07-01 21:06:45 +00001102U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001103 bootd, 1, 1, do_bootd,
Peter Tyser2fb26042009-01-27 18:03:12 -06001104 "boot default, i.e., run 'bootcmd'",
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001105 ""
wdenk8bde7f72003-06-27 21:31:46 +00001106);
1107
wdenk47d1a6e2002-11-03 00:01:44 +00001108#endif
1109
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001110
1111/*******************************************************************/
1112/* iminfo - print header info for a requested image */
1113/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001114#if defined(CONFIG_CMD_IMI)
Kim Phillips088f1b12012-10-29 13:34:31 +00001115static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk47d1a6e2002-11-03 00:01:44 +00001116{
1117 int arg;
1118 ulong addr;
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001119 int rcode = 0;
wdenk47d1a6e2002-11-03 00:01:44 +00001120
1121 if (argc < 2) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001122 return image_info(load_addr);
wdenk47d1a6e2002-11-03 00:01:44 +00001123 }
1124
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001125 for (arg = 1; arg < argc; ++arg) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001126 addr = simple_strtoul(argv[arg], NULL, 16);
1127 if (image_info(addr) != 0)
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001128 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001129 }
1130 return rcode;
1131}
1132
Stephen Warren712fbcf2011-10-18 11:11:49 +00001133static int image_info(ulong addr)
wdenk47d1a6e2002-11-03 00:01:44 +00001134{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001135 void *hdr = (void *)addr;
wdenk47d1a6e2002-11-03 00:01:44 +00001136
Stephen Warren712fbcf2011-10-18 11:11:49 +00001137 printf("\n## Checking Image at %08lx ...\n", addr);
wdenk47d1a6e2002-11-03 00:01:44 +00001138
Stephen Warren712fbcf2011-10-18 11:11:49 +00001139 switch (genimg_get_format(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001140 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001141 puts(" Legacy image found\n");
1142 if (!image_check_magic(hdr)) {
1143 puts(" Bad Magic Number\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001144 return 1;
1145 }
1146
Stephen Warren712fbcf2011-10-18 11:11:49 +00001147 if (!image_check_hcrc(hdr)) {
1148 puts(" Bad Header Checksum\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001149 return 1;
1150 }
1151
Stephen Warren712fbcf2011-10-18 11:11:49 +00001152 image_print_contents(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001153
Stephen Warren712fbcf2011-10-18 11:11:49 +00001154 puts(" Verifying Checksum ... ");
1155 if (!image_check_dcrc(hdr)) {
1156 puts(" Bad Data CRC\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001157 return 1;
1158 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001159 puts("OK\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001160 return 0;
1161#if defined(CONFIG_FIT)
1162 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001163 puts(" FIT image found\n");
Marian Balakowicze32fea62008-03-11 12:35:20 +01001164
Stephen Warren712fbcf2011-10-18 11:11:49 +00001165 if (!fit_check_format(hdr)) {
1166 puts("Bad FIT image format!\n");
Marian Balakowicze32fea62008-03-11 12:35:20 +01001167 return 1;
1168 }
1169
Stephen Warren712fbcf2011-10-18 11:11:49 +00001170 fit_print_contents(hdr);
Bartlomiej Siekaa4f24342008-09-09 12:58:16 +02001171
Stephen Warren712fbcf2011-10-18 11:11:49 +00001172 if (!fit_all_image_check_hashes(hdr)) {
1173 puts("Bad hash in FIT image!\n");
Bartlomiej Siekaa4f24342008-09-09 12:58:16 +02001174 return 1;
1175 }
1176
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001177 return 0;
1178#endif
1179 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001180 puts("Unknown image format!\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001181 break;
wdenk47d1a6e2002-11-03 00:01:44 +00001182 }
1183
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001184 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001185}
wdenk0d498392003-07-01 21:06:45 +00001186
1187U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001188 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
Peter Tyser2fb26042009-01-27 18:03:12 -06001189 "print header information for application image",
wdenk8bde7f72003-06-27 21:31:46 +00001190 "addr [addr ...]\n"
1191 " - print header information for application image starting at\n"
1192 " address 'addr' in memory; this includes verification of the\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +02001193 " image contents (magic number, header and payload checksums)"
wdenk8bde7f72003-06-27 21:31:46 +00001194);
Jon Loeliger90253172007-07-10 11:02:44 -05001195#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001196
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001197
1198/*******************************************************************/
1199/* imls - list all images found in flash */
1200/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001201#if defined(CONFIG_CMD_IMLS)
Vipin Kumar8fdf1e02012-12-16 22:32:48 +00001202static int do_imls_nor(void)
wdenk27b207f2003-07-24 23:38:38 +00001203{
1204 flash_info_t *info;
1205 int i, j;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001206 void *hdr;
wdenk27b207f2003-07-24 23:38:38 +00001207
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001208 for (i = 0, info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +02001209 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001210
wdenk27b207f2003-07-24 23:38:38 +00001211 if (info->flash_id == FLASH_UNKNOWN)
1212 goto next_bank;
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001213 for (j = 0; j < info->sector_count; ++j) {
wdenk27b207f2003-07-24 23:38:38 +00001214
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001215 hdr = (void *)info->start[j];
1216 if (!hdr)
wdenk27b207f2003-07-24 23:38:38 +00001217 goto next_sector;
1218
Stephen Warren712fbcf2011-10-18 11:11:49 +00001219 switch (genimg_get_format(hdr)) {
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001220 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001221 if (!image_check_hcrc(hdr))
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001222 goto next_sector;
1223
Stephen Warren712fbcf2011-10-18 11:11:49 +00001224 printf("Legacy Image at %08lX:\n", (ulong)hdr);
1225 image_print_contents(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001226
Stephen Warren712fbcf2011-10-18 11:11:49 +00001227 puts(" Verifying Checksum ... ");
1228 if (!image_check_dcrc(hdr)) {
1229 puts("Bad Data CRC\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001230 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001231 puts("OK\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001232 }
1233 break;
1234#if defined(CONFIG_FIT)
1235 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +00001236 if (!fit_check_format(hdr))
Marian Balakowicze32fea62008-03-11 12:35:20 +01001237 goto next_sector;
1238
Stephen Warren712fbcf2011-10-18 11:11:49 +00001239 printf("FIT Image at %08lX:\n", (ulong)hdr);
1240 fit_print_contents(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001241 break;
1242#endif
1243 default:
wdenk27b207f2003-07-24 23:38:38 +00001244 goto next_sector;
wdenk5bb226e2003-11-17 21:14:37 +00001245 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001246
wdenkbdccc4f2003-08-05 17:43:17 +00001247next_sector: ;
wdenk27b207f2003-07-24 23:38:38 +00001248 }
wdenkbdccc4f2003-08-05 17:43:17 +00001249next_bank: ;
wdenk27b207f2003-07-24 23:38:38 +00001250 }
Vipin Kumar8fdf1e02012-12-16 22:32:48 +00001251 return 0;
1252}
1253#endif
1254
1255#if defined(CONFIG_CMD_IMLS_NAND)
1256static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
1257 size_t len)
1258{
1259 void *imgdata;
1260 int ret;
1261
1262 imgdata = malloc(len);
1263 if (!imgdata) {
1264 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
1265 nand_dev, off);
1266 printf(" Low memory(cannot allocate memory for image)\n");
1267 return -ENOMEM;
1268 }
1269
1270 ret = nand_read_skip_bad(nand, off, &len,
1271 imgdata);
1272 if (ret < 0 && ret != -EUCLEAN) {
1273 free(imgdata);
1274 return ret;
1275 }
1276
1277 if (!image_check_hcrc(imgdata)) {
1278 free(imgdata);
1279 return 0;
1280 }
1281
1282 printf("Legacy Image at NAND device %d offset %08llX:\n",
1283 nand_dev, off);
1284 image_print_contents(imgdata);
1285
1286 puts(" Verifying Checksum ... ");
1287 if (!image_check_dcrc(imgdata))
1288 puts("Bad Data CRC\n");
1289 else
1290 puts("OK\n");
1291
1292 free(imgdata);
1293
1294 return 0;
1295}
1296
1297static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
1298 size_t len)
1299{
1300 void *imgdata;
1301 int ret;
1302
1303 imgdata = malloc(len);
1304 if (!imgdata) {
1305 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
1306 nand_dev, off);
1307 printf(" Low memory(cannot allocate memory for image)\n");
1308 return -ENOMEM;
1309 }
1310
1311 ret = nand_read_skip_bad(nand, off, &len,
1312 imgdata);
1313 if (ret < 0 && ret != -EUCLEAN) {
1314 free(imgdata);
1315 return ret;
1316 }
1317
1318 if (!fit_check_format(imgdata)) {
1319 free(imgdata);
1320 return 0;
1321 }
1322
1323 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
1324
1325 fit_print_contents(imgdata);
1326 free(imgdata);
1327
1328 return 0;
1329}
1330
1331static int do_imls_nand(void)
1332{
1333 nand_info_t *nand;
1334 int nand_dev = nand_curr_device;
1335 size_t len;
1336 loff_t off;
1337 u32 buffer[16];
1338
1339 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
1340 puts("\nNo NAND devices available\n");
1341 return -ENODEV;
1342 }
1343
1344 printf("\n");
1345
1346 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
1347 nand = &nand_info[nand_dev];
1348 if (!nand->name || !nand->size)
1349 continue;
1350
1351 for (off = 0; off < nand->size; off += nand->erasesize) {
1352 const image_header_t *header;
1353 int ret;
1354
1355 if (nand_block_isbad(nand, off))
1356 continue;
1357
1358 len = sizeof(buffer);
1359
1360 ret = nand_read(nand, off, &len, (u8 *)buffer);
1361 if (ret < 0 && ret != -EUCLEAN) {
1362 printf("NAND read error %d at offset %08llX\n",
1363 ret, off);
1364 continue;
1365 }
1366
1367 switch (genimg_get_format(buffer)) {
1368 case IMAGE_FORMAT_LEGACY:
1369 header = (const image_header_t *)buffer;
1370
1371 len = image_get_image_size(header);
1372 nand_imls_legacyimage(nand, nand_dev, off, len);
1373 break;
1374#if defined(CONFIG_FIT)
1375 case IMAGE_FORMAT_FIT:
1376 len = fit_get_size(buffer);
1377 nand_imls_fitimage(nand, nand_dev, off, len);
1378 break;
1379#endif
1380 }
1381 }
1382 }
1383
1384 return 0;
1385}
1386#endif
1387
1388#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
1389static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1390{
1391 int ret_nor = 0, ret_nand = 0;
1392
1393#if defined(CONFIG_CMD_IMLS)
1394 ret_nor = do_imls_nor();
1395#endif
1396
1397#if defined(CONFIG_CMD_IMLS_NAND)
1398 ret_nand = do_imls_nand();
1399#endif
1400
1401 if (ret_nor)
1402 return ret_nor;
1403
1404 if (ret_nand)
1405 return ret_nand;
wdenk27b207f2003-07-24 23:38:38 +00001406
1407 return (0);
1408}
1409
1410U_BOOT_CMD(
1411 imls, 1, 1, do_imls,
Peter Tyser2fb26042009-01-27 18:03:12 -06001412 "list all images found in flash",
wdenk27b207f2003-07-24 23:38:38 +00001413 "\n"
Vipin Kumar8fdf1e02012-12-16 22:32:48 +00001414 " - Prints information about all images found at sector/block\n"
1415 " boundaries in nor/nand flash."
wdenk27b207f2003-07-24 23:38:38 +00001416);
Jon Loeliger90253172007-07-10 11:02:44 -05001417#endif
wdenk27b207f2003-07-24 23:38:38 +00001418
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001419/*******************************************************************/
Marian Balakowicz5cf746c2008-01-31 13:59:09 +01001420/* helper routines */
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001421/*******************************************************************/
Joe Hershbergerbe2e5a02013-02-27 10:20:59 +00001422#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
Doug Anderson3a8653b2011-10-19 12:30:57 +00001423static void fixup_silent_linux(void)
wdenk47d1a6e2002-11-03 00:01:44 +00001424{
1425 char buf[256], *start, *end;
Doug Anderson3a8653b2011-10-19 12:30:57 +00001426 char *cmdline = getenv("bootargs");
wdenk47d1a6e2002-11-03 00:01:44 +00001427
1428 /* Only fix cmdline when requested */
1429 if (!(gd->flags & GD_FLG_SILENT))
1430 return;
1431
Doug Anderson3a8653b2011-10-19 12:30:57 +00001432 debug("before silent fix-up: %s\n", cmdline);
wdenk47d1a6e2002-11-03 00:01:44 +00001433 if (cmdline) {
Doug Anderson3a8653b2011-10-19 12:30:57 +00001434 start = strstr(cmdline, "console=");
1435 if (start) {
1436 end = strchr(start, ' ');
1437 strncpy(buf, cmdline, (start - cmdline + 8));
wdenk47d1a6e2002-11-03 00:01:44 +00001438 if (end)
Doug Anderson3a8653b2011-10-19 12:30:57 +00001439 strcpy(buf + (start - cmdline + 8), end);
wdenk47d1a6e2002-11-03 00:01:44 +00001440 else
1441 buf[start - cmdline + 8] = '\0';
1442 } else {
Doug Anderson3a8653b2011-10-19 12:30:57 +00001443 strcpy(buf, cmdline);
1444 strcat(buf, " console=");
wdenk47d1a6e2002-11-03 00:01:44 +00001445 }
1446 } else {
Doug Anderson3a8653b2011-10-19 12:30:57 +00001447 strcpy(buf, "console=");
wdenk47d1a6e2002-11-03 00:01:44 +00001448 }
1449
Doug Anderson3a8653b2011-10-19 12:30:57 +00001450 setenv("bootargs", buf);
1451 debug("after silent fix-up: %s\n", buf);
wdenk47d1a6e2002-11-03 00:01:44 +00001452}
1453#endif /* CONFIG_SILENT_CONSOLE */
1454
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001455
1456/*******************************************************************/
1457/* OS booting routines */
1458/*******************************************************************/
1459
Kumar Galab1d0db12008-10-21 17:25:47 -05001460#ifdef CONFIG_BOOTM_NETBSD
Stephen Warren712fbcf2011-10-18 11:11:49 +00001461static int do_bootm_netbsd(int flag, int argc, char * const argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001462 bootm_headers_t *images)
wdenk47d1a6e2002-11-03 00:01:44 +00001463{
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001464 void (*loader)(bd_t *, image_header_t *, char *, char *);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001465 image_header_t *os_hdr, *hdr;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +01001466 ulong kernel_data, kernel_len;
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001467 char *consdev;
1468 char *cmdline;
wdenk47d1a6e2002-11-03 00:01:44 +00001469
Kumar Gala49c3a862008-10-21 17:25:45 -05001470 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1471 return 1;
1472
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001473#if defined(CONFIG_FIT)
1474 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001475 fit_unsupported_reset("NetBSD");
Kumar Gala40d7e992008-08-15 08:24:45 -05001476 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001477 }
1478#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001479 hdr = images->legacy_hdr_os;
wdenk47d1a6e2002-11-03 00:01:44 +00001480
1481 /*
1482 * Booting a (NetBSD) kernel image
1483 *
1484 * This process is pretty similar to a standalone application:
1485 * The (first part of an multi-) image must be a stage-2 loader,
1486 * which in turn is responsible for loading & invoking the actual
1487 * kernel. The only differences are the parameters being passed:
1488 * besides the board info strucure, the loader expects a command
1489 * line, the name of the console device, and (optionally) the
1490 * address of the original image header.
1491 */
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001492 os_hdr = NULL;
Stephen Warren712fbcf2011-10-18 11:11:49 +00001493 if (image_check_type(&images->legacy_hdr_os_copy, IH_TYPE_MULTI)) {
1494 image_multi_getimg(hdr, 1, &kernel_data, &kernel_len);
Marian Balakowiczf13e7b22008-01-08 18:12:17 +01001495 if (kernel_len)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001496 os_hdr = hdr;
Marian Balakowiczf13e7b22008-01-08 18:12:17 +01001497 }
wdenk47d1a6e2002-11-03 00:01:44 +00001498
1499 consdev = "";
Stephen Warren712fbcf2011-10-18 11:11:49 +00001500#if defined(CONFIG_8xx_CONS_SMC1)
wdenk47d1a6e2002-11-03 00:01:44 +00001501 consdev = "smc1";
Stephen Warren712fbcf2011-10-18 11:11:49 +00001502#elif defined(CONFIG_8xx_CONS_SMC2)
wdenk47d1a6e2002-11-03 00:01:44 +00001503 consdev = "smc2";
Stephen Warren712fbcf2011-10-18 11:11:49 +00001504#elif defined(CONFIG_8xx_CONS_SCC2)
wdenk47d1a6e2002-11-03 00:01:44 +00001505 consdev = "scc2";
Stephen Warren712fbcf2011-10-18 11:11:49 +00001506#elif defined(CONFIG_8xx_CONS_SCC3)
wdenk47d1a6e2002-11-03 00:01:44 +00001507 consdev = "scc3";
1508#endif
1509
1510 if (argc > 2) {
1511 ulong len;
1512 int i;
1513
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001514 for (i = 2, len = 0; i < argc; i += 1)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001515 len += strlen(argv[i]) + 1;
1516 cmdline = malloc(len);
wdenk47d1a6e2002-11-03 00:01:44 +00001517
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001518 for (i = 2, len = 0; i < argc; i += 1) {
wdenk47d1a6e2002-11-03 00:01:44 +00001519 if (i > 2)
1520 cmdline[len++] = ' ';
Stephen Warren712fbcf2011-10-18 11:11:49 +00001521 strcpy(&cmdline[len], argv[i]);
1522 len += strlen(argv[i]);
wdenk47d1a6e2002-11-03 00:01:44 +00001523 }
Stephen Warren712fbcf2011-10-18 11:11:49 +00001524 } else if ((cmdline = getenv("bootargs")) == NULL) {
wdenk47d1a6e2002-11-03 00:01:44 +00001525 cmdline = "";
1526 }
1527
Kumar Galac160a952008-08-15 08:24:36 -05001528 loader = (void (*)(bd_t *, image_header_t *, char *, char *))images->ep;
wdenk47d1a6e2002-11-03 00:01:44 +00001529
Stephen Warren712fbcf2011-10-18 11:11:49 +00001530 printf("## Transferring control to NetBSD stage-2 loader "
1531 "(at address %08lx) ...\n",
wdenk47d1a6e2002-11-03 00:01:44 +00001532 (ulong)loader);
1533
Simon Glass770605e2012-02-13 13:51:18 +00001534 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
wdenk47d1a6e2002-11-03 00:01:44 +00001535
1536 /*
1537 * NetBSD Stage-2 Loader Parameters:
1538 * r3: ptr to board info data
1539 * r4: image address
1540 * r5: console device
1541 * r6: boot args string
1542 */
Stephen Warren712fbcf2011-10-18 11:11:49 +00001543 (*loader)(gd->bd, os_hdr, consdev, cmdline);
Kumar Gala40d7e992008-08-15 08:24:45 -05001544
1545 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001546}
Kumar Galab1d0db12008-10-21 17:25:47 -05001547#endif /* CONFIG_BOOTM_NETBSD*/
wdenk47d1a6e2002-11-03 00:01:44 +00001548
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001549#ifdef CONFIG_LYNXKDI
Stephen Warren712fbcf2011-10-18 11:11:49 +00001550static int do_bootm_lynxkdi(int flag, int argc, char * const argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001551 bootm_headers_t *images)
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001552{
Marian Balakowiczcb1c4892008-04-11 11:07:49 +02001553 image_header_t *hdr = &images->legacy_hdr_os_copy;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001554
Kumar Gala49c3a862008-10-21 17:25:45 -05001555 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1556 return 1;
1557
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001558#if defined(CONFIG_FIT)
1559 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001560 fit_unsupported_reset("Lynx");
Kumar Gala40d7e992008-08-15 08:24:45 -05001561 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001562 }
1563#endif
1564
Stephen Warren712fbcf2011-10-18 11:11:49 +00001565 lynxkdi_boot((image_header_t *)hdr);
Kumar Gala40d7e992008-08-15 08:24:45 -05001566
1567 return 1;
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001568}
1569#endif /* CONFIG_LYNXKDI */
1570
Kumar Galab1d0db12008-10-21 17:25:47 -05001571#ifdef CONFIG_BOOTM_RTEMS
Stephen Warren712fbcf2011-10-18 11:11:49 +00001572static int do_bootm_rtems(int flag, int argc, char * const argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001573 bootm_headers_t *images)
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001574{
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001575 void (*entry_point)(bd_t *);
wdenkd791b1d2003-04-20 14:04:18 +00001576
Kumar Gala49c3a862008-10-21 17:25:45 -05001577 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1578 return 1;
1579
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001580#if defined(CONFIG_FIT)
1581 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001582 fit_unsupported_reset("RTEMS");
Kumar Gala40d7e992008-08-15 08:24:45 -05001583 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001584 }
1585#endif
1586
Kumar Galac160a952008-08-15 08:24:36 -05001587 entry_point = (void (*)(bd_t *))images->ep;
wdenkd791b1d2003-04-20 14:04:18 +00001588
Stephen Warren712fbcf2011-10-18 11:11:49 +00001589 printf("## Transferring control to RTEMS (at address %08lx) ...\n",
wdenkd791b1d2003-04-20 14:04:18 +00001590 (ulong)entry_point);
1591
Simon Glass770605e2012-02-13 13:51:18 +00001592 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
wdenkd791b1d2003-04-20 14:04:18 +00001593
1594 /*
1595 * RTEMS Parameters:
1596 * r3: ptr to board info data
1597 */
Marian Balakowicz1ee11802008-01-08 18:17:10 +01001598 (*entry_point)(gd->bd);
Kumar Gala40d7e992008-08-15 08:24:45 -05001599
1600 return 1;
wdenkd791b1d2003-04-20 14:04:18 +00001601}
Kumar Galab1d0db12008-10-21 17:25:47 -05001602#endif /* CONFIG_BOOTM_RTEMS */
wdenkd791b1d2003-04-20 14:04:18 +00001603
Torkel Lundgren3df61952010-09-28 11:05:36 +02001604#if defined(CONFIG_BOOTM_OSE)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001605static int do_bootm_ose(int flag, int argc, char * const argv[],
Torkel Lundgren3df61952010-09-28 11:05:36 +02001606 bootm_headers_t *images)
1607{
1608 void (*entry_point)(void);
1609
1610 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1611 return 1;
1612
1613#if defined(CONFIG_FIT)
1614 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001615 fit_unsupported_reset("OSE");
Torkel Lundgren3df61952010-09-28 11:05:36 +02001616 return 1;
1617 }
1618#endif
1619
1620 entry_point = (void (*)(void))images->ep;
1621
Stephen Warren712fbcf2011-10-18 11:11:49 +00001622 printf("## Transferring control to OSE (at address %08lx) ...\n",
Torkel Lundgren3df61952010-09-28 11:05:36 +02001623 (ulong)entry_point);
1624
Simon Glass770605e2012-02-13 13:51:18 +00001625 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Torkel Lundgren3df61952010-09-28 11:05:36 +02001626
1627 /*
1628 * OSE Parameters:
1629 * None
1630 */
1631 (*entry_point)();
1632
1633 return 1;
1634}
1635#endif /* CONFIG_BOOTM_OSE */
1636
Steven Stallion04d41402013-03-20 06:31:35 +00001637#if defined(CONFIG_BOOTM_PLAN9)
1638static int do_bootm_plan9(int flag, int argc, char * const argv[],
1639 bootm_headers_t *images)
1640{
1641 void (*entry_point)(void);
1642
1643 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1644 return 1;
1645
1646#if defined(CONFIG_FIT)
1647 if (!images->legacy_hdr_valid) {
1648 fit_unsupported_reset("Plan 9");
1649 return 1;
1650 }
1651#endif
1652
1653 entry_point = (void (*)(void))images->ep;
1654
1655 printf("## Transferring control to Plan 9 (at address %08lx) ...\n",
1656 (ulong)entry_point);
1657
1658 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
1659
1660 /*
1661 * Plan 9 Parameters:
1662 * None
1663 */
1664 (*entry_point)();
1665
1666 return 1;
1667}
1668#endif /* CONFIG_BOOTM_PLAN9 */
1669
Jon Loeligerbaa26db2007-07-08 17:51:39 -05001670#if defined(CONFIG_CMD_ELF)
Stephen Warren712fbcf2011-10-18 11:11:49 +00001671static int do_bootm_vxworks(int flag, int argc, char * const argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001672 bootm_headers_t *images)
wdenk47d1a6e2002-11-03 00:01:44 +00001673{
wdenk47d1a6e2002-11-03 00:01:44 +00001674 char str[80];
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001675
Kumar Gala49c3a862008-10-21 17:25:45 -05001676 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1677 return 1;
1678
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001679#if defined(CONFIG_FIT)
Marian Balakowiczcb1c4892008-04-11 11:07:49 +02001680 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001681 fit_unsupported_reset("VxWorks");
Kumar Gala40d7e992008-08-15 08:24:45 -05001682 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001683 }
1684#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001685
Kumar Galac160a952008-08-15 08:24:36 -05001686 sprintf(str, "%lx", images->ep); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001687 setenv("loadaddr", str);
Kumar Gala40d7e992008-08-15 08:24:45 -05001688 do_bootvx(NULL, 0, 0, NULL);
1689
1690 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001691}
1692
Wolfgang Denk54841ab2010-06-28 22:00:46 +02001693static int do_bootm_qnxelf(int flag, int argc, char * const argv[],
Marian Balakowicz8a5ea3e2008-02-27 11:01:04 +01001694 bootm_headers_t *images)
wdenk47d1a6e2002-11-03 00:01:44 +00001695{
wdenk47d1a6e2002-11-03 00:01:44 +00001696 char *local_args[2];
1697 char str[16];
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001698
Kumar Gala49c3a862008-10-21 17:25:45 -05001699 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1700 return 1;
1701
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001702#if defined(CONFIG_FIT)
1703 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001704 fit_unsupported_reset("QNX");
Kumar Gala40d7e992008-08-15 08:24:45 -05001705 return 1;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +01001706 }
1707#endif
wdenk47d1a6e2002-11-03 00:01:44 +00001708
Kumar Galac160a952008-08-15 08:24:36 -05001709 sprintf(str, "%lx", images->ep); /* write entry-point into string */
wdenk47d1a6e2002-11-03 00:01:44 +00001710 local_args[0] = argv[0];
1711 local_args[1] = str; /* and provide it via the arguments */
Kumar Gala40d7e992008-08-15 08:24:45 -05001712 do_bootelf(NULL, 0, 2, local_args);
1713
1714 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +00001715}
Jon Loeliger90253172007-07-10 11:02:44 -05001716#endif
Peter Tyserf5ed9e32008-09-08 14:56:49 -05001717
1718#ifdef CONFIG_INTEGRITY
Stephen Warren712fbcf2011-10-18 11:11:49 +00001719static int do_bootm_integrity(int flag, int argc, char * const argv[],
Peter Tyserf5ed9e32008-09-08 14:56:49 -05001720 bootm_headers_t *images)
1721{
1722 void (*entry_point)(void);
1723
Kumar Gala49c3a862008-10-21 17:25:45 -05001724 if ((flag != 0) && (flag != BOOTM_STATE_OS_GO))
1725 return 1;
1726
Peter Tyserf5ed9e32008-09-08 14:56:49 -05001727#if defined(CONFIG_FIT)
1728 if (!images->legacy_hdr_valid) {
Stephen Warren712fbcf2011-10-18 11:11:49 +00001729 fit_unsupported_reset("INTEGRITY");
Peter Tyserf5ed9e32008-09-08 14:56:49 -05001730 return 1;
1731 }
1732#endif
1733
1734 entry_point = (void (*)(void))images->ep;
1735
Stephen Warren712fbcf2011-10-18 11:11:49 +00001736 printf("## Transferring control to INTEGRITY (at address %08lx) ...\n",
Peter Tyserf5ed9e32008-09-08 14:56:49 -05001737 (ulong)entry_point);
1738
Simon Glass770605e2012-02-13 13:51:18 +00001739 bootstage_mark(BOOTSTAGE_ID_RUN_OS);
Peter Tyserf5ed9e32008-09-08 14:56:49 -05001740
1741 /*
1742 * INTEGRITY Parameters:
1743 * None
1744 */
1745 (*entry_point)();
1746
1747 return 1;
1748}
1749#endif
Marek Vasut44f074c2012-03-14 21:52:45 +00001750
1751#ifdef CONFIG_CMD_BOOTZ
1752
1753static int __bootz_setup(void *image, void **start, void **end)
1754{
1755 /* Please define bootz_setup() for your platform */
1756
1757 puts("Your platform's zImage format isn't supported yet!\n");
1758 return -1;
1759}
1760int bootz_setup(void *image, void **start, void **end)
1761 __attribute__((weak, alias("__bootz_setup")));
1762
1763/*
1764 * zImage booting support
1765 */
1766static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
1767 char * const argv[], bootm_headers_t *images)
1768{
1769 int ret;
1770 void *zi_start, *zi_end;
1771
1772 memset(images, 0, sizeof(bootm_headers_t));
1773
1774 boot_start_lmb(images);
1775
1776 /* Setup Linux kernel zImage entry point */
1777 if (argc < 2) {
1778 images->ep = load_addr;
1779 debug("* kernel: default image load address = 0x%08lx\n",
1780 load_addr);
1781 } else {
1782 images->ep = simple_strtoul(argv[1], NULL, 16);
1783 debug("* kernel: cmdline image address = 0x%08lx\n",
1784 images->ep);
1785 }
1786
1787 ret = bootz_setup((void *)images->ep, &zi_start, &zi_end);
1788 if (ret != 0)
1789 return 1;
1790
1791 lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
1792
1793 /* Find ramdisk */
1794 ret = boot_get_ramdisk(argc, argv, images, IH_INITRD_ARCH,
1795 &images->rd_start, &images->rd_end);
1796 if (ret) {
1797 puts("Ramdisk image is corrupt or invalid\n");
1798 return 1;
1799 }
1800
1801#if defined(CONFIG_OF_LIBFDT)
1802 /* find flattened device tree */
1803 ret = boot_get_fdt(flag, argc, argv, images,
1804 &images->ft_addr, &images->ft_len);
1805 if (ret) {
1806 puts("Could not find a valid device tree\n");
1807 return 1;
1808 }
1809
1810 set_working_fdt_addr(images->ft_addr);
1811#endif
1812
1813 return 0;
1814}
1815
1816static int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
1817{
Marek Vasut44f074c2012-03-14 21:52:45 +00001818 bootm_headers_t images;
1819
1820 if (bootz_start(cmdtp, flag, argc, argv, &images))
1821 return 1;
1822
1823 /*
1824 * We have reached the point of no return: we are going to
1825 * overwrite all exception vector code, so we cannot easily
1826 * recover from any failures any more...
1827 */
Anatolij Gustschina075a792012-05-19 05:38:19 +00001828 disable_interrupts();
Marek Vasut44f074c2012-03-14 21:52:45 +00001829
Joe Hershbergerf8be7d62012-08-03 10:59:08 +00001830#ifdef CONFIG_NETCONSOLE
1831 /* Stop the ethernet stack if NetConsole could have left it up */
1832 eth_halt();
1833#endif
1834
Marek Vasut44f074c2012-03-14 21:52:45 +00001835#if defined(CONFIG_CMD_USB)
1836 /*
1837 * turn off USB to prevent the host controller from writing to the
1838 * SDRAM while Linux is booting. This could happen (at least for OHCI
1839 * controller), because the HCCA (Host Controller Communication Area)
1840 * lies within the SDRAM and the host controller writes continously to
1841 * this area (as busmaster!). The HccaFrameNumber is for example
1842 * updated every 1 ms within the HCCA structure in SDRAM! For more
1843 * details see the OpenHCI specification.
1844 */
1845 usb_stop();
1846#endif
1847
Joe Hershbergerbe2e5a02013-02-27 10:20:59 +00001848#if defined(CONFIG_SILENT_CONSOLE) && !defined(CONFIG_SILENT_U_BOOT_ONLY)
Marek Vasut44f074c2012-03-14 21:52:45 +00001849 fixup_silent_linux();
1850#endif
1851 arch_preboot_os();
1852
1853 do_bootm_linux(0, argc, argv, &images);
1854#ifdef DEBUG
1855 puts("\n## Control returned to monitor - resetting...\n");
1856#endif
1857 do_reset(cmdtp, flag, argc, argv);
1858
1859 return 1;
1860}
1861
Kim Phillips088f1b12012-10-29 13:34:31 +00001862#ifdef CONFIG_SYS_LONGHELP
1863static char bootz_help_text[] =
Marek Vasut017e1f32012-03-18 11:47:58 +00001864 "[addr [initrd[:size]] [fdt]]\n"
1865 " - boot Linux zImage stored in memory\n"
Marek Vasut44f074c2012-03-14 21:52:45 +00001866 "\tThe argument 'initrd' is optional and specifies the address\n"
Marek Vasut017e1f32012-03-18 11:47:58 +00001867 "\tof the initrd in memory. The optional argument ':size' allows\n"
1868 "\tspecifying the size of RAW initrd.\n"
Marek Vasut44f074c2012-03-14 21:52:45 +00001869#if defined(CONFIG_OF_LIBFDT)
1870 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
1871 "\ta third argument is required which is the address of the\n"
1872 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
1873 "\tuse a '-' for the second argument. If you do not pass a third\n"
1874 "\ta bd_info struct will be passed instead\n"
1875#endif
Kim Phillips088f1b12012-10-29 13:34:31 +00001876 "";
1877#endif
1878
1879U_BOOT_CMD(
1880 bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
1881 "boot Linux zImage image from memory", bootz_help_text
Marek Vasut44f074c2012-03-14 21:52:45 +00001882);
1883#endif /* CONFIG_CMD_BOOTZ */