blob: f5e91f40a9bf0a2a167d52ab7a215fbbd6b7ab23 [file] [log] [blame]
wdenk47d1a6e2002-11-03 00:01:44 +00001/*
Detlev Zundelca95c9d2009-07-13 16:01:18 +02002 * (C) Copyright 2000-2009
wdenk47d1a6e2002-11-03 00:01:44 +00003 * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
4 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenk47d1a6e2002-11-03 00:01:44 +00006 */
7
8/*
9 * Boot support
10 */
11#include <common.h>
Simon Glassb6396402014-06-12 07:24:46 -060012#include <bootm.h>
wdenk47d1a6e2002-11-03 00:01:44 +000013#include <command.h>
wdenk7f70e852003-05-20 14:25:27 +000014#include <environment.h>
Simon Glass90268b82014-10-19 21:11:24 -060015#include <errno.h>
Simon Glassb6396402014-06-12 07:24:46 -060016#include <image.h>
Kumar Gala4ed65522008-02-27 21:51:47 -060017#include <lmb.h>
Simon Glassb6396402014-06-12 07:24:46 -060018#include <malloc.h>
Joe Hershberger0eb25b62015-03-22 17:08:59 -050019#include <mapmem.h>
Simon Glassb6396402014-06-12 07:24:46 -060020#include <nand.h>
wdenk47d1a6e2002-11-03 00:01:44 +000021#include <asm/byteorder.h>
Anatolij Gustschin5bf27662011-11-19 13:12:18 +000022#include <linux/compiler.h>
Simon Glassb6396402014-06-12 07:24:46 -060023#include <linux/ctype.h>
24#include <linux/err.h>
25#include <u-boot/zlib.h>
Peter Korsgaard20dde482009-11-19 11:37:51 +010026
Marian Balakowicz1ee11802008-01-08 18:17:10 +010027DECLARE_GLOBAL_DATA_PTR;
28
Jon Loeligerbaa26db2007-07-08 17:51:39 -050029#if defined(CONFIG_CMD_IMI)
Stephen Warren712fbcf2011-10-18 11:11:49 +000030static int image_info(unsigned long addr);
wdenk47d1a6e2002-11-03 00:01:44 +000031#endif
wdenk27b207f2003-07-24 23:38:38 +000032
Jon Loeligerbaa26db2007-07-08 17:51:39 -050033#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +000034#include <flash.h>
Stefan Roeseca5def32010-08-31 10:00:10 +020035#include <mtd/cfi_flash.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020036extern flash_info_t flash_info[]; /* info for FLASH chips */
Vipin Kumar8fdf1e02012-12-16 22:32:48 +000037#endif
38
39#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
Stephen Warren712fbcf2011-10-18 11:11:49 +000040static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
wdenk27b207f2003-07-24 23:38:38 +000041#endif
42
Simon Schwarzdee17762011-09-02 00:50:31 +000043bootm_headers_t images; /* pointers to os/initrd/fdt images */
Stefan Roese15940c92006-03-13 11:16:36 +010044
Kumar Gala49c3a862008-10-21 17:25:45 -050045/* we overload the cmd field with our state machine info instead of a
46 * function pointer */
Frans Meulenbroeksf74d9bd2010-02-25 10:12:13 +010047static cmd_tbl_t cmd_bootm_sub[] = {
Kumar Gala49c3a862008-10-21 17:25:45 -050048 U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
49 U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
John Rigbyfca43cc2010-10-13 13:57:35 -060050#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Kumar Gala49c3a862008-10-21 17:25:45 -050051 U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
52#endif
53#ifdef CONFIG_OF_LIBFDT
54 U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
55#endif
Kumar Gala49c3a862008-10-21 17:25:45 -050056 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
Peter Tyser224c90d2009-11-18 19:08:59 -060057 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
Kumar Gala49c3a862008-10-21 17:25:45 -050058 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
Simon Glassd0ae31e2013-06-11 11:14:48 -070059 U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
Kumar Gala49c3a862008-10-21 17:25:45 -050060 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
61};
62
Kim Phillips088f1b12012-10-29 13:34:31 +000063static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
Stephen Warren712fbcf2011-10-18 11:11:49 +000064 char * const argv[])
Kumar Gala49c3a862008-10-21 17:25:45 -050065{
66 int ret = 0;
Simon Glass6d6f1232011-10-07 13:53:40 +000067 long state;
Kumar Gala49c3a862008-10-21 17:25:45 -050068 cmd_tbl_t *c;
Kumar Gala49c3a862008-10-21 17:25:45 -050069
Simon Glass983c72f2013-06-11 11:14:46 -070070 c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
71 argc--; argv++;
Kumar Gala49c3a862008-10-21 17:25:45 -050072
73 if (c) {
Simon Glass6d6f1232011-10-07 13:53:40 +000074 state = (long)c->cmd;
Simon Glass983c72f2013-06-11 11:14:46 -070075 if (state == BOOTM_STATE_START)
Simon Glass35fc84f2013-06-11 11:14:47 -070076 state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
Wolfgang Denk47e26b12010-07-17 01:06:04 +020077 } else {
78 /* Unrecognized command */
Simon Glass4c12eeb2011-12-10 08:44:01 +000079 return CMD_RET_USAGE;
Kumar Gala49c3a862008-10-21 17:25:45 -050080 }
81
Heiko Schocherff6c0322015-02-24 07:04:38 +010082 if (((state & BOOTM_STATE_START) != BOOTM_STATE_START) &&
83 images.state >= state) {
Stephen Warren712fbcf2011-10-18 11:11:49 +000084 printf("Trying to execute a command out of order\n");
Simon Glass4c12eeb2011-12-10 08:44:01 +000085 return CMD_RET_USAGE;
Kumar Gala49c3a862008-10-21 17:25:45 -050086 }
87
Simon Glass35fc84f2013-06-11 11:14:47 -070088 ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
Kumar Gala49c3a862008-10-21 17:25:45 -050089
90 return ret;
91}
92
Kumar Gala396f6352008-08-15 08:24:41 -050093/*******************************************************************/
94/* bootm - boot application image from image in memory */
95/*******************************************************************/
Kumar Galabe083152008-10-21 17:25:44 -050096
Stephen Warren712fbcf2011-10-18 11:11:49 +000097int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Kumar Gala396f6352008-08-15 08:24:41 -050098{
Wolfgang Denk2e5167c2010-10-28 20:00:11 +020099#ifdef CONFIG_NEEDS_MANUAL_RELOC
Peter Tyser521af042009-09-21 11:20:36 -0500100 static int relocated = 0;
Kumar Galabe083152008-10-21 17:25:44 -0500101
Kumar Galabe083152008-10-21 17:25:44 -0500102 if (!relocated) {
103 int i;
Daniel Schwierzeck58bd77d2013-01-07 06:54:52 +0000104
Daniel Schwierzeck58bd77d2013-01-07 06:54:52 +0000105 /* relocate names of sub-command table */
106 for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
107 cmd_bootm_sub[i].name += gd->reloc_off;
108
Kumar Galabe083152008-10-21 17:25:44 -0500109 relocated = 1;
110 }
Peter Tyser521af042009-09-21 11:20:36 -0500111#endif
Kumar Gala396f6352008-08-15 08:24:41 -0500112
Kumar Gala49c3a862008-10-21 17:25:45 -0500113 /* determine if we have a sub command */
Simon Glass983c72f2013-06-11 11:14:46 -0700114 argc--; argv++;
115 if (argc > 0) {
Kumar Gala49c3a862008-10-21 17:25:45 -0500116 char *endp;
117
Simon Glass983c72f2013-06-11 11:14:46 -0700118 simple_strtoul(argv[0], &endp, 16);
119 /* endp pointing to NULL means that argv[0] was just a
Kumar Gala49c3a862008-10-21 17:25:45 -0500120 * valid number, pass it along to the normal bootm processing
121 *
122 * If endp is ':' or '#' assume a FIT identifier so pass
123 * along for normal processing.
124 *
125 * Right now we assume the first arg should never be '-'
126 */
127 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
128 return do_bootm_subcommand(cmdtp, flag, argc, argv);
129 }
130
Simon Glass35fc84f2013-06-11 11:14:47 -0700131 return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
132 BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
Tom Rini3d187b32013-09-23 14:20:37 -0400133 BOOTM_STATE_LOADOS |
134#if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
135 BOOTM_STATE_OS_CMDLINE |
136#endif
Paul Burton5c427e42013-09-06 11:23:55 +0100137 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
138 BOOTM_STATE_OS_GO, &images, 1);
wdenk47d1a6e2002-11-03 00:01:44 +0000139}
140
Mike Frysinger67d668b2011-06-05 13:43:02 +0000141int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
142{
143 const char *ep = getenv("autostart");
144
145 if (ep && !strcmp(ep, "yes")) {
146 char *local_args[2];
147 local_args[0] = (char *)cmd;
148 local_args[1] = NULL;
149 printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
150 return do_bootm(cmdtp, 0, 1, local_args);
151 }
152
153 return 0;
154}
155
Kim Phillips088f1b12012-10-29 13:34:31 +0000156#ifdef CONFIG_SYS_LONGHELP
157static char bootm_help_text[] =
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100158 "[addr [arg ...]]\n - boot application image stored in memory\n"
159 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
160 "\t'arg' can be the address of an initrd image\n"
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100161#if defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500162 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
Detlev Zundel5441f612007-10-19 16:47:26 +0200163 "\ta third argument is required which is the address of the\n"
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500164 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
165 "\tuse a '-' for the second argument. If you do not pass a third\n"
166 "\ta bd_info struct will be passed instead\n"
167#endif
Marian Balakowicz6986a382008-03-12 10:01:05 +0100168#if defined(CONFIG_FIT)
169 "\t\nFor the new multi component uImage format (FIT) addresses\n"
170 "\tmust be extened to include component or configuration unit name:\n"
171 "\taddr:<subimg_uname> - direct component image specification\n"
172 "\taddr#<conf_uname> - configuration specification\n"
173 "\tUse iminfo command to get the list of existing component\n"
174 "\timages and configurations.\n"
175#endif
Kumar Gala49c3a862008-10-21 17:25:45 -0500176 "\nSub-commands to do part of the bootm sequence. The sub-commands "
177 "must be\n"
178 "issued in the order below (it's ok to not issue all sub-commands):\n"
179 "\tstart [addr [arg ...]]\n"
180 "\tloados - load OS image\n"
Daniel Schwierzeck59af76d2013-02-26 04:54:19 +0000181#if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
Kumar Gala49c3a862008-10-21 17:25:45 -0500182 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
183#endif
184#if defined(CONFIG_OF_LIBFDT)
185 "\tfdt - relocate flat device tree\n"
186#endif
Kumar Gala49c3a862008-10-21 17:25:45 -0500187 "\tcmdline - OS specific command line processing/setup\n"
Peter Tyser224c90d2009-11-18 19:08:59 -0600188 "\tbdt - OS specific bd_t processing\n"
Kumar Gala49c3a862008-10-21 17:25:45 -0500189 "\tprep - OS specific prep before relocation or go\n"
Michal Simeke3046ba2015-01-15 09:53:13 +0100190#if defined(CONFIG_TRACE)
191 "\tfake - OS specific fake start without go\n"
192#endif
Kim Phillips088f1b12012-10-29 13:34:31 +0000193 "\tgo - start OS";
194#endif
195
196U_BOOT_CMD(
197 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
198 "boot application image from memory", bootm_help_text
wdenk8bde7f72003-06-27 21:31:46 +0000199);
200
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100201/*******************************************************************/
202/* bootd - boot default image */
203/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500204#if defined(CONFIG_CMD_BOOTD)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000205int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000206{
Thomas Betker73671da2014-06-05 20:07:56 +0200207 return run_command(getenv("bootcmd"), flag);
wdenk47d1a6e2002-11-03 00:01:44 +0000208}
wdenk8bde7f72003-06-27 21:31:46 +0000209
wdenk0d498392003-07-01 21:06:45 +0000210U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100211 boot, 1, 1, do_bootd,
Peter Tyser2fb26042009-01-27 18:03:12 -0600212 "boot default, i.e., run 'bootcmd'",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200213 ""
wdenk9d2b18a2003-06-28 23:11:04 +0000214);
215
216/* keep old command name "bootd" for backward compatibility */
wdenk0d498392003-07-01 21:06:45 +0000217U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100218 bootd, 1, 1, do_bootd,
Peter Tyser2fb26042009-01-27 18:03:12 -0600219 "boot default, i.e., run 'bootcmd'",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200220 ""
wdenk8bde7f72003-06-27 21:31:46 +0000221);
222
wdenk47d1a6e2002-11-03 00:01:44 +0000223#endif
224
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100225
226/*******************************************************************/
227/* iminfo - print header info for a requested image */
228/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500229#if defined(CONFIG_CMD_IMI)
Kim Phillips088f1b12012-10-29 13:34:31 +0000230static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000231{
232 int arg;
233 ulong addr;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100234 int rcode = 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000235
236 if (argc < 2) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000237 return image_info(load_addr);
wdenk47d1a6e2002-11-03 00:01:44 +0000238 }
239
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100240 for (arg = 1; arg < argc; ++arg) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000241 addr = simple_strtoul(argv[arg], NULL, 16);
242 if (image_info(addr) != 0)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100243 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000244 }
245 return rcode;
246}
247
Stephen Warren712fbcf2011-10-18 11:11:49 +0000248static int image_info(ulong addr)
wdenk47d1a6e2002-11-03 00:01:44 +0000249{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100250 void *hdr = (void *)addr;
wdenk47d1a6e2002-11-03 00:01:44 +0000251
Stephen Warren712fbcf2011-10-18 11:11:49 +0000252 printf("\n## Checking Image at %08lx ...\n", addr);
wdenk47d1a6e2002-11-03 00:01:44 +0000253
Stephen Warren712fbcf2011-10-18 11:11:49 +0000254 switch (genimg_get_format(hdr)) {
Heiko Schocher21d29f72014-05-28 11:33:33 +0200255#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100256 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000257 puts(" Legacy image found\n");
258 if (!image_check_magic(hdr)) {
259 puts(" Bad Magic Number\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100260 return 1;
261 }
262
Stephen Warren712fbcf2011-10-18 11:11:49 +0000263 if (!image_check_hcrc(hdr)) {
264 puts(" Bad Header Checksum\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100265 return 1;
266 }
267
Stephen Warren712fbcf2011-10-18 11:11:49 +0000268 image_print_contents(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100269
Stephen Warren712fbcf2011-10-18 11:11:49 +0000270 puts(" Verifying Checksum ... ");
271 if (!image_check_dcrc(hdr)) {
272 puts(" Bad Data CRC\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100273 return 1;
274 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000275 puts("OK\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100276 return 0;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200277#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100278#if defined(CONFIG_FIT)
279 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000280 puts(" FIT image found\n");
Marian Balakowicze32fea62008-03-11 12:35:20 +0100281
Stephen Warren712fbcf2011-10-18 11:11:49 +0000282 if (!fit_check_format(hdr)) {
283 puts("Bad FIT image format!\n");
Marian Balakowicze32fea62008-03-11 12:35:20 +0100284 return 1;
285 }
286
Stephen Warren712fbcf2011-10-18 11:11:49 +0000287 fit_print_contents(hdr);
Bartlomiej Siekaa4f24342008-09-09 12:58:16 +0200288
Simon Glassb8da8362013-05-07 06:11:57 +0000289 if (!fit_all_image_verify(hdr)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000290 puts("Bad hash in FIT image!\n");
Bartlomiej Siekaa4f24342008-09-09 12:58:16 +0200291 return 1;
292 }
293
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100294 return 0;
295#endif
296 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000297 puts("Unknown image format!\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100298 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000299 }
300
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100301 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000302}
wdenk0d498392003-07-01 21:06:45 +0000303
304U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200305 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
Peter Tyser2fb26042009-01-27 18:03:12 -0600306 "print header information for application image",
wdenk8bde7f72003-06-27 21:31:46 +0000307 "addr [addr ...]\n"
308 " - print header information for application image starting at\n"
309 " address 'addr' in memory; this includes verification of the\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200310 " image contents (magic number, header and payload checksums)"
wdenk8bde7f72003-06-27 21:31:46 +0000311);
Jon Loeliger90253172007-07-10 11:02:44 -0500312#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000313
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100314
315/*******************************************************************/
316/* imls - list all images found in flash */
317/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500318#if defined(CONFIG_CMD_IMLS)
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000319static int do_imls_nor(void)
wdenk27b207f2003-07-24 23:38:38 +0000320{
321 flash_info_t *info;
322 int i, j;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100323 void *hdr;
wdenk27b207f2003-07-24 23:38:38 +0000324
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100325 for (i = 0, info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200326 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100327
wdenk27b207f2003-07-24 23:38:38 +0000328 if (info->flash_id == FLASH_UNKNOWN)
329 goto next_bank;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100330 for (j = 0; j < info->sector_count; ++j) {
wdenk27b207f2003-07-24 23:38:38 +0000331
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100332 hdr = (void *)info->start[j];
333 if (!hdr)
wdenk27b207f2003-07-24 23:38:38 +0000334 goto next_sector;
335
Stephen Warren712fbcf2011-10-18 11:11:49 +0000336 switch (genimg_get_format(hdr)) {
Heiko Schocher21d29f72014-05-28 11:33:33 +0200337#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100338 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000339 if (!image_check_hcrc(hdr))
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100340 goto next_sector;
341
Stephen Warren712fbcf2011-10-18 11:11:49 +0000342 printf("Legacy Image at %08lX:\n", (ulong)hdr);
343 image_print_contents(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100344
Stephen Warren712fbcf2011-10-18 11:11:49 +0000345 puts(" Verifying Checksum ... ");
346 if (!image_check_dcrc(hdr)) {
347 puts("Bad Data CRC\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100348 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000349 puts("OK\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100350 }
351 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200352#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100353#if defined(CONFIG_FIT)
354 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000355 if (!fit_check_format(hdr))
Marian Balakowicze32fea62008-03-11 12:35:20 +0100356 goto next_sector;
357
Stephen Warren712fbcf2011-10-18 11:11:49 +0000358 printf("FIT Image at %08lX:\n", (ulong)hdr);
359 fit_print_contents(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100360 break;
361#endif
362 default:
wdenk27b207f2003-07-24 23:38:38 +0000363 goto next_sector;
wdenk5bb226e2003-11-17 21:14:37 +0000364 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100365
wdenkbdccc4f2003-08-05 17:43:17 +0000366next_sector: ;
wdenk27b207f2003-07-24 23:38:38 +0000367 }
wdenkbdccc4f2003-08-05 17:43:17 +0000368next_bank: ;
wdenk27b207f2003-07-24 23:38:38 +0000369 }
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000370 return 0;
371}
372#endif
373
374#if defined(CONFIG_CMD_IMLS_NAND)
Scott Wood151c06e2016-05-30 13:57:54 -0500375static int nand_imls_legacyimage(struct mtd_info *mtd, int nand_dev,
376 loff_t off, size_t len)
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000377{
378 void *imgdata;
379 int ret;
380
381 imgdata = malloc(len);
382 if (!imgdata) {
383 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
384 nand_dev, off);
385 printf(" Low memory(cannot allocate memory for image)\n");
386 return -ENOMEM;
387 }
388
Scott Wood151c06e2016-05-30 13:57:54 -0500389 ret = nand_read_skip_bad(mtd, off, &len, imgdata);
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000390 if (ret < 0 && ret != -EUCLEAN) {
391 free(imgdata);
392 return ret;
393 }
394
395 if (!image_check_hcrc(imgdata)) {
396 free(imgdata);
397 return 0;
398 }
399
400 printf("Legacy Image at NAND device %d offset %08llX:\n",
401 nand_dev, off);
402 image_print_contents(imgdata);
403
404 puts(" Verifying Checksum ... ");
405 if (!image_check_dcrc(imgdata))
406 puts("Bad Data CRC\n");
407 else
408 puts("OK\n");
409
410 free(imgdata);
411
412 return 0;
413}
414
Scott Wood151c06e2016-05-30 13:57:54 -0500415static int nand_imls_fitimage(struct mtd_info *mtd, int nand_dev, loff_t off,
416 size_t len)
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000417{
418 void *imgdata;
419 int ret;
420
421 imgdata = malloc(len);
422 if (!imgdata) {
423 printf("May be a FIT Image at NAND device %d offset %08llX:\n",
424 nand_dev, off);
425 printf(" Low memory(cannot allocate memory for image)\n");
426 return -ENOMEM;
427 }
428
Scott Wood151c06e2016-05-30 13:57:54 -0500429 ret = nand_read_skip_bad(mtd, off, &len, imgdata);
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000430 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{
Scott Wood151c06e2016-05-30 13:57:54 -0500450 struct mtd_info *mtd;
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000451 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++) {
Scott Woodb616d9b2016-05-30 13:57:55 -0500464 mtd = nand_info[nand_dev];
Scott Wood151c06e2016-05-30 13:57:54 -0500465 if (!mtd->name || !mtd->size)
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000466 continue;
467
Scott Wood151c06e2016-05-30 13:57:54 -0500468 for (off = 0; off < mtd->size; off += mtd->erasesize) {
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000469 const image_header_t *header;
470 int ret;
471
Scott Wood151c06e2016-05-30 13:57:54 -0500472 if (nand_block_isbad(mtd, off))
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000473 continue;
474
475 len = sizeof(buffer);
476
Scott Wood151c06e2016-05-30 13:57:54 -0500477 ret = nand_read(mtd, off, &len, (u8 *)buffer);
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000478 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 Schocher21d29f72014-05-28 11:33:33 +0200485#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Vipin Kumar8fdf1e02012-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);
Scott Wood151c06e2016-05-30 13:57:54 -0500490 nand_imls_legacyimage(mtd, nand_dev, off, len);
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000491 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200492#endif
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000493#if defined(CONFIG_FIT)
494 case IMAGE_FORMAT_FIT:
495 len = fit_get_size(buffer);
Scott Wood151c06e2016-05-30 13:57:54 -0500496 nand_imls_fitimage(mtd, nand_dev, off, len);
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000497 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;
wdenk27b207f2003-07-24 23:38:38 +0000525
526 return (0);
527}
528
529U_BOOT_CMD(
530 imls, 1, 1, do_imls,
Peter Tyser2fb26042009-01-27 18:03:12 -0600531 "list all images found in flash",
wdenk27b207f2003-07-24 23:38:38 +0000532 "\n"
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000533 " - Prints information about all images found at sector/block\n"
534 " boundaries in nor/nand flash."
wdenk27b207f2003-07-24 23:38:38 +0000535);
Jon Loeliger90253172007-07-10 11:02:44 -0500536#endif
wdenk27b207f2003-07-24 23:38:38 +0000537
Marek Vasut44f074c2012-03-14 21:52:45 +0000538#ifdef CONFIG_CMD_BOOTZ
539
Simon Glassa5266d62013-07-04 13:26:10 -0700540int __weak bootz_setup(ulong image, ulong *start, ulong *end)
Marek Vasut44f074c2012-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 Vasut44f074c2012-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 Glassa5266d62013-07-04 13:26:10 -0700555 ulong zi_start, zi_end;
Marek Vasut44f074c2012-03-14 21:52:45 +0000556
Simon Glass35fc84f2013-06-11 11:14:47 -0700557 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
558 images, 1);
Marek Vasut44f074c2012-03-14 21:52:45 +0000559
560 /* Setup Linux kernel zImage entry point */
Tom Rini2b9599e2013-07-09 15:32:34 -0400561 if (!argc) {
Marek Vasut44f074c2012-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 Rini2b9599e2013-07-09 15:32:34 -0400566 images->ep = simple_strtoul(argv[0], NULL, 16);
Marek Vasut44f074c2012-03-14 21:52:45 +0000567 debug("* kernel: cmdline image address = 0x%08lx\n",
568 images->ep);
569 }
570
Simon Glassa5266d62013-07-04 13:26:10 -0700571 ret = bootz_setup(images->ep, &zi_start, &zi_end);
Marek Vasut44f074c2012-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 Rini225fd8c2013-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 */
Karl Apsited52e8572015-05-21 09:52:49 -0400581 if (bootm_find_images(flag, argc, argv))
Tom Rini2b9599e2013-07-09 15:32:34 -0400582 return 1;
Marek Vasut44f074c2012-03-14 21:52:45 +0000583
Tom Rini2b9599e2013-07-09 15:32:34 -0400584 return 0;
Marek Vasut44f074c2012-03-14 21:52:45 +0000585}
586
Rob Herringda620222012-12-02 21:00:23 -0600587int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Marek Vasut44f074c2012-03-14 21:52:45 +0000588{
Simon Glass35fc84f2013-06-11 11:14:47 -0700589 int ret;
Marek Vasut44f074c2012-03-14 21:52:45 +0000590
Tom Rini2b9599e2013-07-09 15:32:34 -0400591 /* Consume 'bootz' */
592 argc--; argv++;
593
Marek Vasut44f074c2012-03-14 21:52:45 +0000594 if (bootz_start(cmdtp, flag, argc, argv, &images))
595 return 1;
596
Simon Glass385501d2013-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 Glassfb1b1392013-07-04 13:26:11 -0700603 images.os.os = IH_OS_LINUX;
Simon Glass35fc84f2013-06-11 11:14:47 -0700604 ret = do_bootm_states(cmdtp, flag, argc, argv,
Simon Glassfb1b1392013-07-04 13:26:11 -0700605 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
606 BOOTM_STATE_OS_GO,
Simon Glassd0ae31e2013-06-11 11:14:48 -0700607 &images, 1);
Marek Vasut44f074c2012-03-14 21:52:45 +0000608
Simon Glass35fc84f2013-06-11 11:14:47 -0700609 return ret;
Marek Vasut44f074c2012-03-14 21:52:45 +0000610}
611
Kim Phillips088f1b12012-10-29 13:34:31 +0000612#ifdef CONFIG_SYS_LONGHELP
613static char bootz_help_text[] =
Marek Vasut017e1f32012-03-18 11:47:58 +0000614 "[addr [initrd[:size]] [fdt]]\n"
615 " - boot Linux zImage stored in memory\n"
Marek Vasut44f074c2012-03-14 21:52:45 +0000616 "\tThe argument 'initrd' is optional and specifies the address\n"
Marek Vasut017e1f32012-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 Vasut44f074c2012-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 Phillips088f1b12012-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 Vasut44f074c2012-03-14 21:52:45 +0000632);
633#endif /* CONFIG_CMD_BOOTZ */
Tom Rinid2b2ffe2014-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;
Andre Przywaraf9a90ac2016-05-25 09:48:14 +0100656 uint64_t image_size;
Tom Rinid2b2ffe2014-08-14 06:42:36 -0400657
658 ih = (struct Image_header *)map_sysmem(images->ep, 0);
659
660 if (ih->magic != le32_to_cpu(LINUX_ARM64_IMAGE_MAGIC)) {
661 puts("Bad Linux ARM64 Image magic!\n");
662 return 1;
663 }
664
665 if (ih->image_size == 0) {
666 puts("Image lacks image_size field, assuming 16MiB\n");
Andre Przywaraf9a90ac2016-05-25 09:48:14 +0100667 image_size = 16 << 20;
668 } else {
669 image_size = le64_to_cpu(ih->image_size);
Tom Rinid2b2ffe2014-08-14 06:42:36 -0400670 }
671
672 /*
673 * If we are not at the correct run-time location, set the new
674 * correct location and then move the image there.
675 */
Andre Przywaraf9a90ac2016-05-25 09:48:14 +0100676 dst = gd->bd->bi_dram[0].start + le64_to_cpu(ih->text_offset);
Masahiro Yamada62022d52016-03-29 19:51:57 +0900677
678 unmap_sysmem(ih);
679
Tom Rinid2b2ffe2014-08-14 06:42:36 -0400680 if (images->ep != dst) {
681 void *src;
682
683 debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
684
685 src = (void *)images->ep;
686 images->ep = dst;
Andre Przywaraf9a90ac2016-05-25 09:48:14 +0100687 memmove((void *)dst, src, image_size);
Tom Rinid2b2ffe2014-08-14 06:42:36 -0400688 }
689
690 return 0;
691}
692
693/*
694 * Image booting support
695 */
696static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
697 char * const argv[], bootm_headers_t *images)
698{
699 int ret;
700 struct Image_header *ih;
701
702 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
703 images, 1);
704
705 /* Setup Linux kernel Image entry point */
706 if (!argc) {
707 images->ep = load_addr;
708 debug("* kernel: default image load address = 0x%08lx\n",
709 load_addr);
710 } else {
711 images->ep = simple_strtoul(argv[0], NULL, 16);
712 debug("* kernel: cmdline image address = 0x%08lx\n",
713 images->ep);
714 }
715
716 ret = booti_setup(images);
717 if (ret != 0)
718 return 1;
719
720 ih = (struct Image_header *)map_sysmem(images->ep, 0);
721
722 lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
723
Masahiro Yamada62022d52016-03-29 19:51:57 +0900724 unmap_sysmem(ih);
725
Tom Rinid2b2ffe2014-08-14 06:42:36 -0400726 /*
727 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
728 * have a header that provide this informaiton.
729 */
Karl Apsited52e8572015-05-21 09:52:49 -0400730 if (bootm_find_images(flag, argc, argv))
Tom Rinid2b2ffe2014-08-14 06:42:36 -0400731 return 1;
732
733 return 0;
734}
735
736int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
737{
738 int ret;
739
740 /* Consume 'booti' */
741 argc--; argv++;
742
743 if (booti_start(cmdtp, flag, argc, argv, &images))
744 return 1;
745
746 /*
747 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
748 * disable interrupts ourselves
749 */
750 bootm_disable_interrupts();
751
752 images.os.os = IH_OS_LINUX;
753 ret = do_bootm_states(cmdtp, flag, argc, argv,
754 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
755 BOOTM_STATE_OS_GO,
756 &images, 1);
757
758 return ret;
759}
760
761#ifdef CONFIG_SYS_LONGHELP
762static char booti_help_text[] =
763 "[addr [initrd[:size]] [fdt]]\n"
Karsten Merker6f6051f2016-02-22 20:59:08 +0100764 " - boot arm64 Linux Image stored in memory\n"
Tom Rinid2b2ffe2014-08-14 06:42:36 -0400765 "\tThe argument 'initrd' is optional and specifies the address\n"
Karsten Merker6f6051f2016-02-22 20:59:08 +0100766 "\tof an initrd in memory. The optional parameter ':size' allows\n"
767 "\tspecifying the size of a RAW initrd.\n"
Tom Rinid2b2ffe2014-08-14 06:42:36 -0400768#if defined(CONFIG_OF_LIBFDT)
Karsten Merker6f6051f2016-02-22 20:59:08 +0100769 "\tSince booting a Linux kernel requires a flat device-tree, a\n"
770 "\tthird argument providing the address of the device-tree blob\n"
771 "\tis required. To boot a kernel with a device-tree blob but\n"
772 "\twithout an initrd image, use a '-' for the initrd argument.\n"
Tom Rinid2b2ffe2014-08-14 06:42:36 -0400773#endif
774 "";
775#endif
776
777U_BOOT_CMD(
778 booti, CONFIG_SYS_MAXARGS, 1, do_booti,
779 "boot arm64 Linux Image image from memory", booti_help_text
780);
781#endif /* CONFIG_CMD_BOOTI */