blob: 48199bfff3ed75a232ba3905cb4fe8662b09e321 [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
8/*
9 * Boot support
10 */
11#include <common.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060012#include <bootm.h>
wdenk47d1a6e2002-11-03 00:01:44 +000013#include <command.h>
wdenk0893c472003-05-20 14:25:27 +000014#include <environment.h>
Simon Glass0129b522014-10-19 21:11:24 -060015#include <errno.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060016#include <image.h>
Kumar Gala6d7bfa82008-02-27 21:51:47 -060017#include <lmb.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060018#include <malloc.h>
19#include <nand.h>
wdenk47d1a6e2002-11-03 00:01:44 +000020#include <asm/byteorder.h>
Anatolij Gustschin18e73d92011-11-19 13:12:18 +000021#include <linux/compiler.h>
Simon Glass3b5866d2014-06-12 07:24:46 -060022#include <linux/ctype.h>
23#include <linux/err.h>
24#include <u-boot/zlib.h>
Peter Korsgaardf7440cd2009-11-19 11:37:51 +010025
Marian Balakowicz2df645e2008-01-08 18:17:10 +010026DECLARE_GLOBAL_DATA_PTR;
wdenk9dfa8d12002-12-08 09:53:23 +000027
Jon Loeliger54324d02007-07-08 17:51:39 -050028#if defined(CONFIG_CMD_IMI)
Stephen Warren6904b6e2011-10-18 11:11:49 +000029static int image_info(unsigned long addr);
wdenk47d1a6e2002-11-03 00:01:44 +000030#endif
wdenk874ac262003-07-24 23:38:38 +000031
Jon Loeliger54324d02007-07-08 17:51:39 -050032#if defined(CONFIG_CMD_IMLS)
wdenk874ac262003-07-24 23:38:38 +000033#include <flash.h>
Stefan Roesefb9a7302010-08-31 10:00:10 +020034#include <mtd/cfi_flash.h>
Marian Balakowicz513b4a12005-10-11 19:09:42 +020035extern flash_info_t flash_info[]; /* info for FLASH chips */
Vipin Kumar3df41b12012-12-16 22:32:48 +000036#endif
37
38#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
Stephen Warren6904b6e2011-10-18 11:11:49 +000039static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
wdenk874ac262003-07-24 23:38:38 +000040#endif
Kumar Galabf3954f2008-10-21 17:25:44 -050041
Simon Schwarz6519bbf2011-09-02 00:50:31 +000042bootm_headers_t images; /* pointers to os/initrd/fdt images */
Stefan Roese5d5ce292006-03-13 11:16:36 +010043
Kumar Gala18178bc2008-10-21 17:25:45 -050044/* we overload the cmd field with our state machine info instead of a
45 * function pointer */
Frans Meulenbroeks6b419fb2010-02-25 10:12:13 +010046static cmd_tbl_t cmd_bootm_sub[] = {
Kumar Gala18178bc2008-10-21 17:25:45 -050047 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
48 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
John Rigbyeea8e692010-10-13 13:57:35 -060049#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Kumar Gala18178bc2008-10-21 17:25:45 -050050 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
51#endif
52#ifdef CONFIG_OF_LIBFDT
53 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
54#endif
Kumar Gala18178bc2008-10-21 17:25:45 -050055 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
Peter Tyser694a3932009-11-18 19:08:59 -060056 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
Kumar Gala18178bc2008-10-21 17:25:45 -050057 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
Simon Glass00134882013-06-11 11:14:48 -070058 U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
Kumar Gala18178bc2008-10-21 17:25:45 -050059 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
60};
61
Kim Phillipsdc00a682012-10-29 13:34:31 +000062static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
Stephen Warren6904b6e2011-10-18 11:11:49 +000063 char * const argv[])
Kumar Gala18178bc2008-10-21 17:25:45 -050064{
65 int ret = 0;
Simon Glass81afc3d2011-10-07 13:53:40 +000066 long state;
Kumar Gala18178bc2008-10-21 17:25:45 -050067 cmd_tbl_t *c;
Kumar Gala18178bc2008-10-21 17:25:45 -050068
Simon Glass794a9212013-06-11 11:14:46 -070069 c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
70 argc--; argv++;
Kumar Gala18178bc2008-10-21 17:25:45 -050071
72 if (c) {
Simon Glass81afc3d2011-10-07 13:53:40 +000073 state = (long)c->cmd;
Simon Glass794a9212013-06-11 11:14:46 -070074 if (state == BOOTM_STATE_START)
Simon Glass3081e2e2013-06-11 11:14:47 -070075 state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
Wolfgang Denk3b683112010-07-17 01:06:04 +020076 } else {
77 /* Unrecognized command */
Simon Glassa06dfc72011-12-10 08:44:01 +000078 return CMD_RET_USAGE;
Kumar Gala18178bc2008-10-21 17:25:45 -050079 }
80
Simon Glass3081e2e2013-06-11 11:14:47 -070081 if (state != BOOTM_STATE_START && images.state >= state) {
Stephen Warren6904b6e2011-10-18 11:11:49 +000082 printf("Trying to execute a command out of order\n");
Simon Glassa06dfc72011-12-10 08:44:01 +000083 return CMD_RET_USAGE;
Kumar Gala18178bc2008-10-21 17:25:45 -050084 }
85
Simon Glass3081e2e2013-06-11 11:14:47 -070086 ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
Kumar Gala18178bc2008-10-21 17:25:45 -050087
88 return ret;
89}
90
Kumar Galabd70bbe2008-08-15 08:24:41 -050091/*******************************************************************/
92/* bootm - boot application image from image in memory */
93/*******************************************************************/
Kumar Galabf3954f2008-10-21 17:25:44 -050094
Stephen Warren6904b6e2011-10-18 11:11:49 +000095int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Kumar Galabd70bbe2008-08-15 08:24:41 -050096{
Wolfgang Denkd0813e52010-10-28 20:00:11 +020097#ifdef CONFIG_NEEDS_MANUAL_RELOC
Peter Tyser9057cbf2009-09-21 11:20:36 -050098 static int relocated = 0;
Kumar Galabf3954f2008-10-21 17:25:44 -050099
Kumar Galabf3954f2008-10-21 17:25:44 -0500100 if (!relocated) {
101 int i;
Daniel Schwierzecka2187092013-01-07 06:54:52 +0000102
Daniel Schwierzecka2187092013-01-07 06:54:52 +0000103 /* relocate names of sub-command table */
104 for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
105 cmd_bootm_sub[i].name += gd->reloc_off;
106
Kumar Galabf3954f2008-10-21 17:25:44 -0500107 relocated = 1;
108 }
Peter Tyser9057cbf2009-09-21 11:20:36 -0500109#endif
Kumar Galabd70bbe2008-08-15 08:24:41 -0500110
Kumar Gala18178bc2008-10-21 17:25:45 -0500111 /* determine if we have a sub command */
Simon Glass794a9212013-06-11 11:14:46 -0700112 argc--; argv++;
113 if (argc > 0) {
Kumar Gala18178bc2008-10-21 17:25:45 -0500114 char *endp;
115
Simon Glass794a9212013-06-11 11:14:46 -0700116 simple_strtoul(argv[0], &endp, 16);
117 /* endp pointing to NULL means that argv[0] was just a
Kumar Gala18178bc2008-10-21 17:25:45 -0500118 * valid number, pass it along to the normal bootm processing
119 *
120 * If endp is ':' or '#' assume a FIT identifier so pass
121 * along for normal processing.
122 *
123 * Right now we assume the first arg should never be '-'
124 */
125 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
126 return do_bootm_subcommand(cmdtp, flag, argc, argv);
127 }
128
Simon Glass3081e2e2013-06-11 11:14:47 -0700129 return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
130 BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
Tom Rini1008dc22013-09-23 14:20:37 -0400131 BOOTM_STATE_LOADOS |
132#if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
133 BOOTM_STATE_OS_CMDLINE |
134#endif
Paul Burton8a39e8d2013-09-06 11:23:55 +0100135 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
136 BOOTM_STATE_OS_GO, &images, 1);
wdenk47d1a6e2002-11-03 00:01:44 +0000137}
138
Mike Frysinger194c2e82011-06-05 13:43:02 +0000139int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
140{
141 const char *ep = getenv("autostart");
142
143 if (ep && !strcmp(ep, "yes")) {
144 char *local_args[2];
145 local_args[0] = (char *)cmd;
146 local_args[1] = NULL;
147 printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
148 return do_bootm(cmdtp, 0, 1, local_args);
149 }
150
151 return 0;
152}
153
Kim Phillipsdc00a682012-10-29 13:34:31 +0000154#ifdef CONFIG_SYS_LONGHELP
155static char bootm_help_text[] =
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100156 "[addr [arg ...]]\n - boot application image stored in memory\n"
157 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
158 "\t'arg' can be the address of an initrd image\n"
Marian Balakowiczd2160852008-01-31 13:20:07 +0100159#if defined(CONFIG_OF_LIBFDT)
Matthew McClintock6fb7c812006-06-28 10:41:37 -0500160 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
Detlev Zundele6099162007-10-19 16:47:26 +0200161 "\ta third argument is required which is the address of the\n"
Matthew McClintock6fb7c812006-06-28 10:41:37 -0500162 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
163 "\tuse a '-' for the second argument. If you do not pass a third\n"
164 "\ta bd_info struct will be passed instead\n"
165#endif
Marian Balakowicz9f8e4aa2008-03-12 10:01:05 +0100166#if defined(CONFIG_FIT)
167 "\t\nFor the new multi component uImage format (FIT) addresses\n"
168 "\tmust be extened to include component or configuration unit name:\n"
169 "\taddr:<subimg_uname> - direct component image specification\n"
170 "\taddr#<conf_uname> - configuration specification\n"
171 "\tUse iminfo command to get the list of existing component\n"
172 "\timages and configurations.\n"
173#endif
Kumar Gala18178bc2008-10-21 17:25:45 -0500174 "\nSub-commands to do part of the bootm sequence. The sub-commands "
175 "must be\n"
176 "issued in the order below (it's ok to not issue all sub-commands):\n"
177 "\tstart [addr [arg ...]]\n"
178 "\tloados - load OS image\n"
Daniel Schwierzeckf2f2ec92013-02-26 04:54:19 +0000179#if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
Kumar Gala18178bc2008-10-21 17:25:45 -0500180 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
181#endif
182#if defined(CONFIG_OF_LIBFDT)
183 "\tfdt - relocate flat device tree\n"
184#endif
Kumar Gala18178bc2008-10-21 17:25:45 -0500185 "\tcmdline - OS specific command line processing/setup\n"
Peter Tyser694a3932009-11-18 19:08:59 -0600186 "\tbdt - OS specific bd_t processing\n"
Kumar Gala18178bc2008-10-21 17:25:45 -0500187 "\tprep - OS specific prep before relocation or go\n"
Michal Simek5ce8e422015-01-15 09:53:13 +0100188#if defined(CONFIG_TRACE)
189 "\tfake - OS specific fake start without go\n"
190#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000191 "\tgo - start OS";
192#endif
193
194U_BOOT_CMD(
195 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
196 "boot application image from memory", bootm_help_text
wdenk57b2d802003-06-27 21:31:46 +0000197);
wdenk47d1a6e2002-11-03 00:01:44 +0000198
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100199/*******************************************************************/
200/* bootd - boot default image */
201/*******************************************************************/
Jon Loeliger54324d02007-07-08 17:51:39 -0500202#if defined(CONFIG_CMD_BOOTD)
Stephen Warren6904b6e2011-10-18 11:11:49 +0000203int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000204{
Thomas Betker5ac00262014-06-05 20:07:56 +0200205 return run_command(getenv("bootcmd"), flag);
wdenk47d1a6e2002-11-03 00:01:44 +0000206}
wdenk47d1a6e2002-11-03 00:01:44 +0000207
wdenkf287a242003-07-01 21:06:45 +0000208U_BOOT_CMD(
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100209 boot, 1, 1, do_bootd,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600210 "boot default, i.e., run 'bootcmd'",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200211 ""
wdenk3086a972003-06-28 23:11:04 +0000212);
wdenk47d1a6e2002-11-03 00:01:44 +0000213
wdenk3086a972003-06-28 23:11:04 +0000214/* keep old command name "bootd" for backward compatibility */
wdenkf287a242003-07-01 21:06:45 +0000215U_BOOT_CMD(
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100216 bootd, 1, 1, do_bootd,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600217 "boot default, i.e., run 'bootcmd'",
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200218 ""
wdenk57b2d802003-06-27 21:31:46 +0000219);
wdenk47d1a6e2002-11-03 00:01:44 +0000220
wdenk47d1a6e2002-11-03 00:01:44 +0000221#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000222
wdenk47d1a6e2002-11-03 00:01:44 +0000223
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100224/*******************************************************************/
225/* iminfo - print header info for a requested image */
226/*******************************************************************/
Jon Loeliger54324d02007-07-08 17:51:39 -0500227#if defined(CONFIG_CMD_IMI)
Kim Phillipsdc00a682012-10-29 13:34:31 +0000228static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000229{
230 int arg;
231 ulong addr;
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100232 int rcode = 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000233
wdenk47d1a6e2002-11-03 00:01:44 +0000234 if (argc < 2) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000235 return image_info(load_addr);
wdenk47d1a6e2002-11-03 00:01:44 +0000236 }
wdenk47d1a6e2002-11-03 00:01:44 +0000237
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100238 for (arg = 1; arg < argc; ++arg) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000239 addr = simple_strtoul(argv[arg], NULL, 16);
240 if (image_info(addr) != 0)
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100241 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000242 }
243 return rcode;
244}
wdenk47d1a6e2002-11-03 00:01:44 +0000245
Stephen Warren6904b6e2011-10-18 11:11:49 +0000246static int image_info(ulong addr)
wdenk47d1a6e2002-11-03 00:01:44 +0000247{
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100248 void *hdr = (void *)addr;
wdenk47d1a6e2002-11-03 00:01:44 +0000249
Stephen Warren6904b6e2011-10-18 11:11:49 +0000250 printf("\n## Checking Image at %08lx ...\n", addr);
wdenk47d1a6e2002-11-03 00:01:44 +0000251
Stephen Warren6904b6e2011-10-18 11:11:49 +0000252 switch (genimg_get_format(hdr)) {
Heiko Schocher515eb122014-05-28 11:33:33 +0200253#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100254 case IMAGE_FORMAT_LEGACY:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000255 puts(" Legacy image found\n");
256 if (!image_check_magic(hdr)) {
257 puts(" Bad Magic Number\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100258 return 1;
259 }
wdenk47d1a6e2002-11-03 00:01:44 +0000260
Stephen Warren6904b6e2011-10-18 11:11:49 +0000261 if (!image_check_hcrc(hdr)) {
262 puts(" Bad Header Checksum\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100263 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000264 }
265
Stephen Warren6904b6e2011-10-18 11:11:49 +0000266 image_print_contents(hdr);
wdenk47d1a6e2002-11-03 00:01:44 +0000267
Stephen Warren6904b6e2011-10-18 11:11:49 +0000268 puts(" Verifying Checksum ... ");
269 if (!image_check_dcrc(hdr)) {
270 puts(" Bad Data CRC\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100271 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000272 }
Stephen Warren6904b6e2011-10-18 11:11:49 +0000273 puts("OK\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100274 return 0;
Heiko Schocher515eb122014-05-28 11:33:33 +0200275#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100276#if defined(CONFIG_FIT)
277 case IMAGE_FORMAT_FIT:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000278 puts(" FIT image found\n");
wdenk47d1a6e2002-11-03 00:01:44 +0000279
Stephen Warren6904b6e2011-10-18 11:11:49 +0000280 if (!fit_check_format(hdr)) {
281 puts("Bad FIT image format!\n");
Marian Balakowiczcc1fd912008-03-11 12:35:20 +0100282 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000283 }
284
Stephen Warren6904b6e2011-10-18 11:11:49 +0000285 fit_print_contents(hdr);
Bartlomiej Sieka03797f52008-09-09 12:58:16 +0200286
Simon Glass7428ad12013-05-07 06:11:57 +0000287 if (!fit_all_image_verify(hdr)) {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000288 puts("Bad hash in FIT image!\n");
Bartlomiej Sieka03797f52008-09-09 12:58:16 +0200289 return 1;
290 }
291
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100292 return 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000293#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100294 default:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000295 puts("Unknown image format!\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100296 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000297 }
wdenk47d1a6e2002-11-03 00:01:44 +0000298
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100299 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000300}
wdenk47d1a6e2002-11-03 00:01:44 +0000301
wdenkf287a242003-07-01 21:06:45 +0000302U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200303 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600304 "print header information for application image",
wdenk57b2d802003-06-27 21:31:46 +0000305 "addr [addr ...]\n"
306 " - print header information for application image starting at\n"
307 " address 'addr' in memory; this includes verification of the\n"
Wolfgang Denkc54781c2009-05-24 17:06:54 +0200308 " image contents (magic number, header and payload checksums)"
wdenk57b2d802003-06-27 21:31:46 +0000309);
wdenk47d1a6e2002-11-03 00:01:44 +0000310#endif
311
312
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100313/*******************************************************************/
314/* imls - list all images found in flash */
315/*******************************************************************/
Jon Loeliger54324d02007-07-08 17:51:39 -0500316#if defined(CONFIG_CMD_IMLS)
Vipin Kumar3df41b12012-12-16 22:32:48 +0000317static int do_imls_nor(void)
wdenk874ac262003-07-24 23:38:38 +0000318{
319 flash_info_t *info;
320 int i, j;
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100321 void *hdr;
wdenk47d1a6e2002-11-03 00:01:44 +0000322
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100323 for (i = 0, info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD03836942008-10-16 15:01:15 +0200324 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
wdenk47d1a6e2002-11-03 00:01:44 +0000325
wdenk874ac262003-07-24 23:38:38 +0000326 if (info->flash_id == FLASH_UNKNOWN)
327 goto next_bank;
Marian Balakowicz2df645e2008-01-08 18:17:10 +0100328 for (j = 0; j < info->sector_count; ++j) {
wdenk47d1a6e2002-11-03 00:01:44 +0000329
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100330 hdr = (void *)info->start[j];
331 if (!hdr)
Marian Balakowicz41d71ed2008-01-08 18:14:09 +0100332 goto next_sector;
wdenk47d1a6e2002-11-03 00:01:44 +0000333
Stephen Warren6904b6e2011-10-18 11:11:49 +0000334 switch (genimg_get_format(hdr)) {
Heiko Schocher515eb122014-05-28 11:33:33 +0200335#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100336 case IMAGE_FORMAT_LEGACY:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000337 if (!image_check_hcrc(hdr))
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100338 goto next_sector;
wdenk47d1a6e2002-11-03 00:01:44 +0000339
Stephen Warren6904b6e2011-10-18 11:11:49 +0000340 printf("Legacy Image at %08lX:\n", (ulong)hdr);
341 image_print_contents(hdr);
wdenk47d1a6e2002-11-03 00:01:44 +0000342
Stephen Warren6904b6e2011-10-18 11:11:49 +0000343 puts(" Verifying Checksum ... ");
344 if (!image_check_dcrc(hdr)) {
345 puts("Bad Data CRC\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100346 } else {
Stephen Warren6904b6e2011-10-18 11:11:49 +0000347 puts("OK\n");
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100348 }
349 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200350#endif
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100351#if defined(CONFIG_FIT)
352 case IMAGE_FORMAT_FIT:
Stephen Warren6904b6e2011-10-18 11:11:49 +0000353 if (!fit_check_format(hdr))
Marian Balakowiczcc1fd912008-03-11 12:35:20 +0100354 goto next_sector;
wdenk47d1a6e2002-11-03 00:01:44 +0000355
Stephen Warren6904b6e2011-10-18 11:11:49 +0000356 printf("FIT Image at %08lX:\n", (ulong)hdr);
357 fit_print_contents(hdr);
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100358 break;
359#endif
360 default:
361 goto next_sector;
wdenk47d1a6e2002-11-03 00:01:44 +0000362 }
Marian Balakowiczdbdd16a2008-02-04 08:28:09 +0100363
wdenk0a658552003-08-05 17:43:17 +0000364next_sector: ;
wdenk47d1a6e2002-11-03 00:01:44 +0000365 }
wdenk0a658552003-08-05 17:43:17 +0000366next_bank: ;
wdenk47d1a6e2002-11-03 00:01:44 +0000367 }
Vipin Kumar3df41b12012-12-16 22:32:48 +0000368 return 0;
369}
370#endif
371
372#if defined(CONFIG_CMD_IMLS_NAND)
373static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
374 size_t len)
375{
376 void *imgdata;
377 int ret;
378
379 imgdata = malloc(len);
380 if (!imgdata) {
381 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
382 nand_dev, off);
383 printf(" Low memory(cannot allocate memory for image)\n");
384 return -ENOMEM;
385 }
386
387 ret = nand_read_skip_bad(nand, off, &len,
388 imgdata);
389 if (ret < 0 && ret != -EUCLEAN) {
390 free(imgdata);
391 return ret;
392 }
393
394 if (!image_check_hcrc(imgdata)) {
395 free(imgdata);
396 return 0;
397 }
398
399 printf("Legacy Image at NAND device %d offset %08llX:\n",
400 nand_dev, off);
401 image_print_contents(imgdata);
402
403 puts(" Verifying Checksum ... ");
404 if (!image_check_dcrc(imgdata))
405 puts("Bad Data CRC\n");
406 else
407 puts("OK\n");
408
409 free(imgdata);
410
411 return 0;
412}
413
414static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
415 size_t len)
416{
417 void *imgdata;
418 int ret;
419
420 imgdata = malloc(len);
421 if (!imgdata) {
422 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
423 nand_dev, off);
424 printf(" Low memory(cannot allocate memory for image)\n");
425 return -ENOMEM;
426 }
wdenk47d1a6e2002-11-03 00:01:44 +0000427
Vipin Kumar3df41b12012-12-16 22:32:48 +0000428 ret = nand_read_skip_bad(nand, off, &len,
429 imgdata);
430 if (ret < 0 && ret != -EUCLEAN) {
431 free(imgdata);
432 return ret;
433 }
434
435 if (!fit_check_format(imgdata)) {
436 free(imgdata);
437 return 0;
438 }
439
440 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
441
442 fit_print_contents(imgdata);
443 free(imgdata);
444
445 return 0;
446}
447
448static int do_imls_nand(void)
449{
450 nand_info_t *nand;
451 int nand_dev = nand_curr_device;
452 size_t len;
453 loff_t off;
454 u32 buffer[16];
455
456 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
457 puts("\nNo NAND devices available\n");
458 return -ENODEV;
459 }
460
461 printf("\n");
462
463 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
464 nand = &nand_info[nand_dev];
465 if (!nand->name || !nand->size)
466 continue;
467
468 for (off = 0; off < nand->size; off += nand->erasesize) {
469 const image_header_t *header;
470 int ret;
471
472 if (nand_block_isbad(nand, off))
473 continue;
474
475 len = sizeof(buffer);
476
477 ret = nand_read(nand, off, &len, (u8 *)buffer);
478 if (ret < 0 && ret != -EUCLEAN) {
479 printf("NAND read error %d at offset %08llX\n",
480 ret, off);
481 continue;
482 }
483
484 switch (genimg_get_format(buffer)) {
Heiko Schocher515eb122014-05-28 11:33:33 +0200485#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Vipin Kumar3df41b12012-12-16 22:32:48 +0000486 case IMAGE_FORMAT_LEGACY:
487 header = (const image_header_t *)buffer;
488
489 len = image_get_image_size(header);
490 nand_imls_legacyimage(nand, nand_dev, off, len);
491 break;
Heiko Schocher515eb122014-05-28 11:33:33 +0200492#endif
Vipin Kumar3df41b12012-12-16 22:32:48 +0000493#if defined(CONFIG_FIT)
494 case IMAGE_FORMAT_FIT:
495 len = fit_get_size(buffer);
496 nand_imls_fitimage(nand, nand_dev, off, len);
497 break;
498#endif
499 }
500 }
501 }
502
503 return 0;
504}
505#endif
506
507#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
508static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
509{
510 int ret_nor = 0, ret_nand = 0;
511
512#if defined(CONFIG_CMD_IMLS)
513 ret_nor = do_imls_nor();
514#endif
515
516#if defined(CONFIG_CMD_IMLS_NAND)
517 ret_nand = do_imls_nand();
518#endif
519
520 if (ret_nor)
521 return ret_nor;
522
523 if (ret_nand)
524 return ret_nand;
525
wdenk874ac262003-07-24 23:38:38 +0000526 return (0);
527}
wdenk47d1a6e2002-11-03 00:01:44 +0000528
wdenk874ac262003-07-24 23:38:38 +0000529U_BOOT_CMD(
530 imls, 1, 1, do_imls,
Peter Tyserdfb72b82009-01-27 18:03:12 -0600531 "list all images found in flash",
wdenk874ac262003-07-24 23:38:38 +0000532 "\n"
Vipin Kumar3df41b12012-12-16 22:32:48 +0000533 " - Prints information about all images found at sector/block\n"
534 " boundaries in nor/nand flash."
wdenk874ac262003-07-24 23:38:38 +0000535);
wdenk47d1a6e2002-11-03 00:01:44 +0000536#endif
537
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000538#ifdef CONFIG_CMD_BOOTZ
539
Simon Glass5f1afdd2013-07-04 13:26:10 -0700540int __weak bootz_setup(ulong image, ulong *start, ulong *end)
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000541{
542 /* Please define bootz_setup() for your platform */
543
544 puts("Your platform's zImage format isn't supported yet!\n");
545 return -1;
546}
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000547
548/*
549 * zImage booting support
550 */
551static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
552 char * const argv[], bootm_headers_t *images)
553{
554 int ret;
Simon Glass5f1afdd2013-07-04 13:26:10 -0700555 ulong zi_start, zi_end;
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000556
Simon Glass3081e2e2013-06-11 11:14:47 -0700557 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
558 images, 1);
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000559
560 /* Setup Linux kernel zImage entry point */
Tom Rini6e5dac42013-07-09 15:32:34 -0400561 if (!argc) {
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000562 images->ep = load_addr;
563 debug("* kernel: default image load address = 0x%08lx\n",
564 load_addr);
565 } else {
Tom Rini6e5dac42013-07-09 15:32:34 -0400566 images->ep = simple_strtoul(argv[0], NULL, 16);
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000567 debug("* kernel: cmdline image address = 0x%08lx\n",
568 images->ep);
569 }
570
Simon Glass5f1afdd2013-07-04 13:26:10 -0700571 ret = bootz_setup(images->ep, &zi_start, &zi_end);
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000572 if (ret != 0)
573 return 1;
574
575 lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
576
Tom Rini1ab61472013-07-09 15:33:25 -0400577 /*
578 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
579 * have a header that provide this informaiton.
580 */
Simon Glass3b5866d2014-06-12 07:24:46 -0600581 if (bootm_find_ramdisk_fdt(flag, argc, argv))
Tom Rini6e5dac42013-07-09 15:32:34 -0400582 return 1;
583
Tom Rini6e5dac42013-07-09 15:32:34 -0400584 return 0;
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000585}
586
Rob Herringc88e1bf2012-12-02 21:00:23 -0600587int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000588{
Simon Glass3081e2e2013-06-11 11:14:47 -0700589 int ret;
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000590
Tom Rini6e5dac42013-07-09 15:32:34 -0400591 /* Consume 'bootz' */
592 argc--; argv++;
593
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000594 if (bootz_start(cmdtp, flag, argc, argv, &images))
595 return 1;
596
Simon Glass917f6842013-07-04 13:26:08 -0700597 /*
598 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
599 * disable interrupts ourselves
600 */
601 bootm_disable_interrupts();
602
Simon Glass42bca362013-07-04 13:26:11 -0700603 images.os.os = IH_OS_LINUX;
Simon Glass3081e2e2013-06-11 11:14:47 -0700604 ret = do_bootm_states(cmdtp, flag, argc, argv,
Simon Glass42bca362013-07-04 13:26:11 -0700605 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
606 BOOTM_STATE_OS_GO,
Simon Glass00134882013-06-11 11:14:48 -0700607 &images, 1);
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000608
Simon Glass3081e2e2013-06-11 11:14:47 -0700609 return ret;
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000610}
611
Kim Phillipsdc00a682012-10-29 13:34:31 +0000612#ifdef CONFIG_SYS_LONGHELP
613static char bootz_help_text[] =
Marek Vasut28850d02012-03-18 11:47:58 +0000614 "[addr [initrd[:size]] [fdt]]\n"
615 " - boot Linux zImage stored in memory\n"
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000616 "\tThe argument 'initrd' is optional and specifies the address\n"
Marek Vasut28850d02012-03-18 11:47:58 +0000617 "\tof the initrd in memory. The optional argument ':size' allows\n"
618 "\tspecifying the size of RAW initrd.\n"
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000619#if defined(CONFIG_OF_LIBFDT)
620 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
621 "\ta third argument is required which is the address of the\n"
622 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
623 "\tuse a '-' for the second argument. If you do not pass a third\n"
624 "\ta bd_info struct will be passed instead\n"
625#endif
Kim Phillipsdc00a682012-10-29 13:34:31 +0000626 "";
627#endif
628
629U_BOOT_CMD(
630 bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
631 "boot Linux zImage image from memory", bootz_help_text
Marek Vasutcf41a9b2012-03-14 21:52:45 +0000632);
633#endif /* CONFIG_CMD_BOOTZ */
Tom Rini5ce62cd2014-08-14 06:42:36 -0400634
635#ifdef CONFIG_CMD_BOOTI
636/* See Documentation/arm64/booting.txt in the Linux kernel */
637struct Image_header {
638 uint32_t code0; /* Executable code */
639 uint32_t code1; /* Executable code */
640 uint64_t text_offset; /* Image load offset, LE */
641 uint64_t image_size; /* Effective Image size, LE */
642 uint64_t res1; /* reserved */
643 uint64_t res2; /* reserved */
644 uint64_t res3; /* reserved */
645 uint64_t res4; /* reserved */
646 uint32_t magic; /* Magic number */
647 uint32_t res5;
648};
649
650#define LINUX_ARM64_IMAGE_MAGIC 0x644d5241
651
652static int booti_setup(bootm_headers_t *images)
653{
654 struct Image_header *ih;
655 uint64_t dst;
656
657 ih = (struct Image_header *)map_sysmem(images->ep, 0);
658
659 if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
660 puts("Bad Linux ARM64 Image magic!\n");
661 return 1;
662 }
663
664 if (ih->image_size == 0) {
665 puts("Image lacks image_size field, assuming 16MiB\n");
666 ih->image_size = (16 << 20);
667 }
668
669 /*
670 * If we are not at the correct run-time location, set the new
671 * correct location and then move the image there.
672 */
673 dst = gd->bd->bi_dram[0].start + le32_to_cpu(ih->text_offset);
674 if (images->ep != dst) {
675 void *src;
676
677 debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
678
679 src = (void *)images->ep;
680 images->ep = dst;
681 memmove((void *)dst, src, le32_to_cpu(ih->image_size));
682 }
683
684 return 0;
685}
686
687/*
688 * Image booting support
689 */
690static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
691 char * const argv[], bootm_headers_t *images)
692{
693 int ret;
694 struct Image_header *ih;
695
696 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
697 images, 1);
698
699 /* Setup Linux kernel Image entry point */
700 if (!argc) {
701 images->ep = load_addr;
702 debug("* kernel: default image load address = 0x%08lx\n",
703 load_addr);
704 } else {
705 images->ep = simple_strtoul(argv[0], NULL, 16);
706 debug("* kernel: cmdline image address = 0x%08lx\n",
707 images->ep);
708 }
709
710 ret = booti_setup(images);
711 if (ret != 0)
712 return 1;
713
714 ih = (struct Image_header *)map_sysmem(images->ep, 0);
715
716 lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
717
718 /*
719 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
720 * have a header that provide this informaiton.
721 */
722 if (bootm_find_ramdisk_fdt(flag, argc, argv))
723 return 1;
724
725 return 0;
726}
727
728int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
729{
730 int ret;
731
732 /* Consume 'booti' */
733 argc--; argv++;
734
735 if (booti_start(cmdtp, flag, argc, argv, &images))
736 return 1;
737
738 /*
739 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
740 * disable interrupts ourselves
741 */
742 bootm_disable_interrupts();
743
744 images.os.os = IH_OS_LINUX;
745 ret = do_bootm_states(cmdtp, flag, argc, argv,
746 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
747 BOOTM_STATE_OS_GO,
748 &images, 1);
749
750 return ret;
751}
752
753#ifdef CONFIG_SYS_LONGHELP
754static char booti_help_text[] =
755 "[addr [initrd[:size]] [fdt]]\n"
756 " - boot Linux Image stored in memory\n"
757 "\tThe argument 'initrd' is optional and specifies the address\n"
758 "\tof the initrd in memory. The optional argument ':size' allows\n"
759 "\tspecifying the size of RAW initrd.\n"
760#if defined(CONFIG_OF_LIBFDT)
761 "\tSince booting a Linux kernelrequires a flat device-tree\n"
762 "\ta third argument is required which is the address of the\n"
763 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
764 "\tuse a '-' for the second argument.\n"
765#endif
766 "";
767#endif
768
769U_BOOT_CMD(
770 booti, CONFIG_SYS_MAXARGS, 1, do_booti,
771 "boot arm64 Linux Image image from memory", booti_help_text
772);
773#endif /* CONFIG_CMD_BOOTI */