blob: 4f77f22f94c41935adbc694e167de17e3df694ec [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>
19#include <nand.h>
wdenk47d1a6e2002-11-03 00:01:44 +000020#include <asm/byteorder.h>
Anatolij Gustschin5bf27662011-11-19 13:12:18 +000021#include <linux/compiler.h>
Simon Glassb6396402014-06-12 07:24:46 -060022#include <linux/ctype.h>
23#include <linux/err.h>
24#include <u-boot/zlib.h>
Peter Korsgaard20dde482009-11-19 11:37:51 +010025
Marian Balakowicz1ee11802008-01-08 18:17:10 +010026DECLARE_GLOBAL_DATA_PTR;
27
Jon Loeligerbaa26db2007-07-08 17:51:39 -050028#if defined(CONFIG_CMD_IMI)
Stephen Warren712fbcf2011-10-18 11:11:49 +000029static int image_info(unsigned long addr);
wdenk47d1a6e2002-11-03 00:01:44 +000030#endif
wdenk27b207f2003-07-24 23:38:38 +000031
Jon Loeligerbaa26db2007-07-08 17:51:39 -050032#if defined(CONFIG_CMD_IMLS)
wdenk27b207f2003-07-24 23:38:38 +000033#include <flash.h>
Stefan Roeseca5def32010-08-31 10:00:10 +020034#include <mtd/cfi_flash.h>
Marian Balakowicze6f2e902005-10-11 19:09:42 +020035extern flash_info_t flash_info[]; /* info for FLASH chips */
Vipin Kumar8fdf1e02012-12-16 22:32:48 +000036#endif
37
38#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
Stephen Warren712fbcf2011-10-18 11:11:49 +000039static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
wdenk27b207f2003-07-24 23:38:38 +000040#endif
41
Simon Schwarzdee17762011-09-02 00:50:31 +000042bootm_headers_t images; /* pointers to os/initrd/fdt images */
Stefan Roese15940c92006-03-13 11:16:36 +010043
Kumar Gala49c3a862008-10-21 17:25:45 -050044/* we overload the cmd field with our state machine info instead of a
45 * function pointer */
Frans Meulenbroeksf74d9bd2010-02-25 10:12:13 +010046static cmd_tbl_t cmd_bootm_sub[] = {
Kumar Gala49c3a862008-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 Rigbyfca43cc2010-10-13 13:57:35 -060049#ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
Kumar Gala49c3a862008-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 Gala49c3a862008-10-21 17:25:45 -050055 U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
Peter Tyser224c90d2009-11-18 19:08:59 -060056 U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
Kumar Gala49c3a862008-10-21 17:25:45 -050057 U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
Simon Glassd0ae31e2013-06-11 11:14:48 -070058 U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
Kumar Gala49c3a862008-10-21 17:25:45 -050059 U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
60};
61
Kim Phillips088f1b12012-10-29 13:34:31 +000062static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
Stephen Warren712fbcf2011-10-18 11:11:49 +000063 char * const argv[])
Kumar Gala49c3a862008-10-21 17:25:45 -050064{
65 int ret = 0;
Simon Glass6d6f1232011-10-07 13:53:40 +000066 long state;
Kumar Gala49c3a862008-10-21 17:25:45 -050067 cmd_tbl_t *c;
Kumar Gala49c3a862008-10-21 17:25:45 -050068
Simon Glass983c72f2013-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 Gala49c3a862008-10-21 17:25:45 -050071
72 if (c) {
Simon Glass6d6f1232011-10-07 13:53:40 +000073 state = (long)c->cmd;
Simon Glass983c72f2013-06-11 11:14:46 -070074 if (state == BOOTM_STATE_START)
Simon Glass35fc84f2013-06-11 11:14:47 -070075 state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
Wolfgang Denk47e26b12010-07-17 01:06:04 +020076 } else {
77 /* Unrecognized command */
Simon Glass4c12eeb2011-12-10 08:44:01 +000078 return CMD_RET_USAGE;
Kumar Gala49c3a862008-10-21 17:25:45 -050079 }
80
Heiko Schocherff6c0322015-02-24 07:04:38 +010081 if (((state & BOOTM_STATE_START) != BOOTM_STATE_START) &&
82 images.state >= state) {
Stephen Warren712fbcf2011-10-18 11:11:49 +000083 printf("Trying to execute a command out of order\n");
Simon Glass4c12eeb2011-12-10 08:44:01 +000084 return CMD_RET_USAGE;
Kumar Gala49c3a862008-10-21 17:25:45 -050085 }
86
Simon Glass35fc84f2013-06-11 11:14:47 -070087 ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
Kumar Gala49c3a862008-10-21 17:25:45 -050088
89 return ret;
90}
91
Kumar Gala396f6352008-08-15 08:24:41 -050092/*******************************************************************/
93/* bootm - boot application image from image in memory */
94/*******************************************************************/
Kumar Galabe083152008-10-21 17:25:44 -050095
Stephen Warren712fbcf2011-10-18 11:11:49 +000096int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Kumar Gala396f6352008-08-15 08:24:41 -050097{
Wolfgang Denk2e5167c2010-10-28 20:00:11 +020098#ifdef CONFIG_NEEDS_MANUAL_RELOC
Peter Tyser521af042009-09-21 11:20:36 -050099 static int relocated = 0;
Kumar Galabe083152008-10-21 17:25:44 -0500100
Kumar Galabe083152008-10-21 17:25:44 -0500101 if (!relocated) {
102 int i;
Daniel Schwierzeck58bd77d2013-01-07 06:54:52 +0000103
Daniel Schwierzeck58bd77d2013-01-07 06:54:52 +0000104 /* relocate names of sub-command table */
105 for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
106 cmd_bootm_sub[i].name += gd->reloc_off;
107
Kumar Galabe083152008-10-21 17:25:44 -0500108 relocated = 1;
109 }
Peter Tyser521af042009-09-21 11:20:36 -0500110#endif
Kumar Gala396f6352008-08-15 08:24:41 -0500111
Kumar Gala49c3a862008-10-21 17:25:45 -0500112 /* determine if we have a sub command */
Simon Glass983c72f2013-06-11 11:14:46 -0700113 argc--; argv++;
114 if (argc > 0) {
Kumar Gala49c3a862008-10-21 17:25:45 -0500115 char *endp;
116
Simon Glass983c72f2013-06-11 11:14:46 -0700117 simple_strtoul(argv[0], &endp, 16);
118 /* endp pointing to NULL means that argv[0] was just a
Kumar Gala49c3a862008-10-21 17:25:45 -0500119 * valid number, pass it along to the normal bootm processing
120 *
121 * If endp is ':' or '#' assume a FIT identifier so pass
122 * along for normal processing.
123 *
124 * Right now we assume the first arg should never be '-'
125 */
126 if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
127 return do_bootm_subcommand(cmdtp, flag, argc, argv);
128 }
129
Simon Glass35fc84f2013-06-11 11:14:47 -0700130 return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
131 BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
Tom Rini3d187b32013-09-23 14:20:37 -0400132 BOOTM_STATE_LOADOS |
133#if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
134 BOOTM_STATE_OS_CMDLINE |
135#endif
Paul Burton5c427e42013-09-06 11:23:55 +0100136 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
137 BOOTM_STATE_OS_GO, &images, 1);
wdenk47d1a6e2002-11-03 00:01:44 +0000138}
139
Mike Frysinger67d668b2011-06-05 13:43:02 +0000140int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
141{
142 const char *ep = getenv("autostart");
143
144 if (ep && !strcmp(ep, "yes")) {
145 char *local_args[2];
146 local_args[0] = (char *)cmd;
147 local_args[1] = NULL;
148 printf("Automatic boot of image at addr 0x%08lX ...\n", load_addr);
149 return do_bootm(cmdtp, 0, 1, local_args);
150 }
151
152 return 0;
153}
154
Kim Phillips088f1b12012-10-29 13:34:31 +0000155#ifdef CONFIG_SYS_LONGHELP
156static char bootm_help_text[] =
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100157 "[addr [arg ...]]\n - boot application image stored in memory\n"
158 "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n"
159 "\t'arg' can be the address of an initrd image\n"
Marian Balakowicz4a2ad5f2008-01-31 13:20:07 +0100160#if defined(CONFIG_OF_LIBFDT)
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500161 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
Detlev Zundel5441f612007-10-19 16:47:26 +0200162 "\ta third argument is required which is the address of the\n"
Matthew McClintock98a9c4d2006-06-28 10:41:37 -0500163 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
164 "\tuse a '-' for the second argument. If you do not pass a third\n"
165 "\ta bd_info struct will be passed instead\n"
166#endif
Marian Balakowicz6986a382008-03-12 10:01:05 +0100167#if defined(CONFIG_FIT)
168 "\t\nFor the new multi component uImage format (FIT) addresses\n"
169 "\tmust be extened to include component or configuration unit name:\n"
170 "\taddr:<subimg_uname> - direct component image specification\n"
171 "\taddr#<conf_uname> - configuration specification\n"
172 "\tUse iminfo command to get the list of existing component\n"
173 "\timages and configurations.\n"
174#endif
Kumar Gala49c3a862008-10-21 17:25:45 -0500175 "\nSub-commands to do part of the bootm sequence. The sub-commands "
176 "must be\n"
177 "issued in the order below (it's ok to not issue all sub-commands):\n"
178 "\tstart [addr [arg ...]]\n"
179 "\tloados - load OS image\n"
Daniel Schwierzeck59af76d2013-02-26 04:54:19 +0000180#if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
Kumar Gala49c3a862008-10-21 17:25:45 -0500181 "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n"
182#endif
183#if defined(CONFIG_OF_LIBFDT)
184 "\tfdt - relocate flat device tree\n"
185#endif
Kumar Gala49c3a862008-10-21 17:25:45 -0500186 "\tcmdline - OS specific command line processing/setup\n"
Peter Tyser224c90d2009-11-18 19:08:59 -0600187 "\tbdt - OS specific bd_t processing\n"
Kumar Gala49c3a862008-10-21 17:25:45 -0500188 "\tprep - OS specific prep before relocation or go\n"
Michal Simeke3046ba2015-01-15 09:53:13 +0100189#if defined(CONFIG_TRACE)
190 "\tfake - OS specific fake start without go\n"
191#endif
Kim Phillips088f1b12012-10-29 13:34:31 +0000192 "\tgo - start OS";
193#endif
194
195U_BOOT_CMD(
196 bootm, CONFIG_SYS_MAXARGS, 1, do_bootm,
197 "boot application image from memory", bootm_help_text
wdenk8bde7f72003-06-27 21:31:46 +0000198);
199
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100200/*******************************************************************/
201/* bootd - boot default image */
202/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500203#if defined(CONFIG_CMD_BOOTD)
Stephen Warren712fbcf2011-10-18 11:11:49 +0000204int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000205{
Thomas Betker73671da2014-06-05 20:07:56 +0200206 return run_command(getenv("bootcmd"), flag);
wdenk47d1a6e2002-11-03 00:01:44 +0000207}
wdenk8bde7f72003-06-27 21:31:46 +0000208
wdenk0d498392003-07-01 21:06:45 +0000209U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100210 boot, 1, 1, do_bootd,
Peter Tyser2fb26042009-01-27 18:03:12 -0600211 "boot default, i.e., run 'bootcmd'",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200212 ""
wdenk9d2b18a2003-06-28 23:11:04 +0000213);
214
215/* keep old command name "bootd" for backward compatibility */
wdenk0d498392003-07-01 21:06:45 +0000216U_BOOT_CMD(
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100217 bootd, 1, 1, do_bootd,
Peter Tyser2fb26042009-01-27 18:03:12 -0600218 "boot default, i.e., run 'bootcmd'",
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200219 ""
wdenk8bde7f72003-06-27 21:31:46 +0000220);
221
wdenk47d1a6e2002-11-03 00:01:44 +0000222#endif
223
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100224
225/*******************************************************************/
226/* iminfo - print header info for a requested image */
227/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500228#if defined(CONFIG_CMD_IMI)
Kim Phillips088f1b12012-10-29 13:34:31 +0000229static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
wdenk47d1a6e2002-11-03 00:01:44 +0000230{
231 int arg;
232 ulong addr;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100233 int rcode = 0;
wdenk47d1a6e2002-11-03 00:01:44 +0000234
235 if (argc < 2) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000236 return image_info(load_addr);
wdenk47d1a6e2002-11-03 00:01:44 +0000237 }
238
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100239 for (arg = 1; arg < argc; ++arg) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000240 addr = simple_strtoul(argv[arg], NULL, 16);
241 if (image_info(addr) != 0)
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100242 rcode = 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000243 }
244 return rcode;
245}
246
Stephen Warren712fbcf2011-10-18 11:11:49 +0000247static int image_info(ulong addr)
wdenk47d1a6e2002-11-03 00:01:44 +0000248{
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100249 void *hdr = (void *)addr;
wdenk47d1a6e2002-11-03 00:01:44 +0000250
Stephen Warren712fbcf2011-10-18 11:11:49 +0000251 printf("\n## Checking Image at %08lx ...\n", addr);
wdenk47d1a6e2002-11-03 00:01:44 +0000252
Stephen Warren712fbcf2011-10-18 11:11:49 +0000253 switch (genimg_get_format(hdr)) {
Heiko Schocher21d29f72014-05-28 11:33:33 +0200254#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100255 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000256 puts(" Legacy image found\n");
257 if (!image_check_magic(hdr)) {
258 puts(" Bad Magic Number\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100259 return 1;
260 }
261
Stephen Warren712fbcf2011-10-18 11:11:49 +0000262 if (!image_check_hcrc(hdr)) {
263 puts(" Bad Header Checksum\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100264 return 1;
265 }
266
Stephen Warren712fbcf2011-10-18 11:11:49 +0000267 image_print_contents(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100268
Stephen Warren712fbcf2011-10-18 11:11:49 +0000269 puts(" Verifying Checksum ... ");
270 if (!image_check_dcrc(hdr)) {
271 puts(" Bad Data CRC\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100272 return 1;
273 }
Stephen Warren712fbcf2011-10-18 11:11:49 +0000274 puts("OK\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100275 return 0;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200276#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100277#if defined(CONFIG_FIT)
278 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000279 puts(" FIT image found\n");
Marian Balakowicze32fea62008-03-11 12:35:20 +0100280
Stephen Warren712fbcf2011-10-18 11:11:49 +0000281 if (!fit_check_format(hdr)) {
282 puts("Bad FIT image format!\n");
Marian Balakowicze32fea62008-03-11 12:35:20 +0100283 return 1;
284 }
285
Stephen Warren712fbcf2011-10-18 11:11:49 +0000286 fit_print_contents(hdr);
Bartlomiej Siekaa4f24342008-09-09 12:58:16 +0200287
Simon Glassb8da8362013-05-07 06:11:57 +0000288 if (!fit_all_image_verify(hdr)) {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000289 puts("Bad hash in FIT image!\n");
Bartlomiej Siekaa4f24342008-09-09 12:58:16 +0200290 return 1;
291 }
292
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100293 return 0;
294#endif
295 default:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000296 puts("Unknown image format!\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100297 break;
wdenk47d1a6e2002-11-03 00:01:44 +0000298 }
299
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100300 return 1;
wdenk47d1a6e2002-11-03 00:01:44 +0000301}
wdenk0d498392003-07-01 21:06:45 +0000302
303U_BOOT_CMD(
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200304 iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo,
Peter Tyser2fb26042009-01-27 18:03:12 -0600305 "print header information for application image",
wdenk8bde7f72003-06-27 21:31:46 +0000306 "addr [addr ...]\n"
307 " - print header information for application image starting at\n"
308 " address 'addr' in memory; this includes verification of the\n"
Wolfgang Denka89c33d2009-05-24 17:06:54 +0200309 " image contents (magic number, header and payload checksums)"
wdenk8bde7f72003-06-27 21:31:46 +0000310);
Jon Loeliger90253172007-07-10 11:02:44 -0500311#endif
wdenk47d1a6e2002-11-03 00:01:44 +0000312
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100313
314/*******************************************************************/
315/* imls - list all images found in flash */
316/*******************************************************************/
Jon Loeligerbaa26db2007-07-08 17:51:39 -0500317#if defined(CONFIG_CMD_IMLS)
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000318static int do_imls_nor(void)
wdenk27b207f2003-07-24 23:38:38 +0000319{
320 flash_info_t *info;
321 int i, j;
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100322 void *hdr;
wdenk27b207f2003-07-24 23:38:38 +0000323
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100324 for (i = 0, info = &flash_info[0];
Jean-Christophe PLAGNIOL-VILLARD6d0f6bc2008-10-16 15:01:15 +0200325 i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100326
wdenk27b207f2003-07-24 23:38:38 +0000327 if (info->flash_id == FLASH_UNKNOWN)
328 goto next_bank;
Marian Balakowicz1ee11802008-01-08 18:17:10 +0100329 for (j = 0; j < info->sector_count; ++j) {
wdenk27b207f2003-07-24 23:38:38 +0000330
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100331 hdr = (void *)info->start[j];
332 if (!hdr)
wdenk27b207f2003-07-24 23:38:38 +0000333 goto next_sector;
334
Stephen Warren712fbcf2011-10-18 11:11:49 +0000335 switch (genimg_get_format(hdr)) {
Heiko Schocher21d29f72014-05-28 11:33:33 +0200336#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100337 case IMAGE_FORMAT_LEGACY:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000338 if (!image_check_hcrc(hdr))
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100339 goto next_sector;
340
Stephen Warren712fbcf2011-10-18 11:11:49 +0000341 printf("Legacy Image at %08lX:\n", (ulong)hdr);
342 image_print_contents(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100343
Stephen Warren712fbcf2011-10-18 11:11:49 +0000344 puts(" Verifying Checksum ... ");
345 if (!image_check_dcrc(hdr)) {
346 puts("Bad Data CRC\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100347 } else {
Stephen Warren712fbcf2011-10-18 11:11:49 +0000348 puts("OK\n");
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100349 }
350 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200351#endif
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100352#if defined(CONFIG_FIT)
353 case IMAGE_FORMAT_FIT:
Stephen Warren712fbcf2011-10-18 11:11:49 +0000354 if (!fit_check_format(hdr))
Marian Balakowicze32fea62008-03-11 12:35:20 +0100355 goto next_sector;
356
Stephen Warren712fbcf2011-10-18 11:11:49 +0000357 printf("FIT Image at %08lX:\n", (ulong)hdr);
358 fit_print_contents(hdr);
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100359 break;
360#endif
361 default:
wdenk27b207f2003-07-24 23:38:38 +0000362 goto next_sector;
wdenk5bb226e2003-11-17 21:14:37 +0000363 }
Marian Balakowiczd5934ad2008-02-04 08:28:09 +0100364
wdenkbdccc4f2003-08-05 17:43:17 +0000365next_sector: ;
wdenk27b207f2003-07-24 23:38:38 +0000366 }
wdenkbdccc4f2003-08-05 17:43:17 +0000367next_bank: ;
wdenk27b207f2003-07-24 23:38:38 +0000368 }
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000369 return 0;
370}
371#endif
372
373#if defined(CONFIG_CMD_IMLS_NAND)
374static int nand_imls_legacyimage(nand_info_t *nand, int nand_dev, loff_t off,
375 size_t len)
376{
377 void *imgdata;
378 int ret;
379
380 imgdata = malloc(len);
381 if (!imgdata) {
382 printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
383 nand_dev, off);
384 printf(" Low memory(cannot allocate memory for image)\n");
385 return -ENOMEM;
386 }
387
388 ret = nand_read_skip_bad(nand, off, &len,
389 imgdata);
390 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
415static int nand_imls_fitimage(nand_info_t *nand, int nand_dev, loff_t off,
416 size_t len)
417{
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
429 ret = nand_read_skip_bad(nand, off, &len,
430 imgdata);
431 if (ret < 0 && ret != -EUCLEAN) {
432 free(imgdata);
433 return ret;
434 }
435
436 if (!fit_check_format(imgdata)) {
437 free(imgdata);
438 return 0;
439 }
440
441 printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
442
443 fit_print_contents(imgdata);
444 free(imgdata);
445
446 return 0;
447}
448
449static int do_imls_nand(void)
450{
451 nand_info_t *nand;
452 int nand_dev = nand_curr_device;
453 size_t len;
454 loff_t off;
455 u32 buffer[16];
456
457 if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
458 puts("\nNo NAND devices available\n");
459 return -ENODEV;
460 }
461
462 printf("\n");
463
464 for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
465 nand = &nand_info[nand_dev];
466 if (!nand->name || !nand->size)
467 continue;
468
469 for (off = 0; off < nand->size; off += nand->erasesize) {
470 const image_header_t *header;
471 int ret;
472
473 if (nand_block_isbad(nand, off))
474 continue;
475
476 len = sizeof(buffer);
477
478 ret = nand_read(nand, off, &len, (u8 *)buffer);
479 if (ret < 0 && ret != -EUCLEAN) {
480 printf("NAND read error %d at offset %08llX\n",
481 ret, off);
482 continue;
483 }
484
485 switch (genimg_get_format(buffer)) {
Heiko Schocher21d29f72014-05-28 11:33:33 +0200486#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000487 case IMAGE_FORMAT_LEGACY:
488 header = (const image_header_t *)buffer;
489
490 len = image_get_image_size(header);
491 nand_imls_legacyimage(nand, nand_dev, off, len);
492 break;
Heiko Schocher21d29f72014-05-28 11:33:33 +0200493#endif
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000494#if defined(CONFIG_FIT)
495 case IMAGE_FORMAT_FIT:
496 len = fit_get_size(buffer);
497 nand_imls_fitimage(nand, nand_dev, off, len);
498 break;
499#endif
500 }
501 }
502 }
503
504 return 0;
505}
506#endif
507
508#if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
509static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
510{
511 int ret_nor = 0, ret_nand = 0;
512
513#if defined(CONFIG_CMD_IMLS)
514 ret_nor = do_imls_nor();
515#endif
516
517#if defined(CONFIG_CMD_IMLS_NAND)
518 ret_nand = do_imls_nand();
519#endif
520
521 if (ret_nor)
522 return ret_nor;
523
524 if (ret_nand)
525 return ret_nand;
wdenk27b207f2003-07-24 23:38:38 +0000526
527 return (0);
528}
529
530U_BOOT_CMD(
531 imls, 1, 1, do_imls,
Peter Tyser2fb26042009-01-27 18:03:12 -0600532 "list all images found in flash",
wdenk27b207f2003-07-24 23:38:38 +0000533 "\n"
Vipin Kumar8fdf1e02012-12-16 22:32:48 +0000534 " - Prints information about all images found at sector/block\n"
535 " boundaries in nor/nand flash."
wdenk27b207f2003-07-24 23:38:38 +0000536);
Jon Loeliger90253172007-07-10 11:02:44 -0500537#endif
wdenk27b207f2003-07-24 23:38:38 +0000538
Marek Vasut44f074c2012-03-14 21:52:45 +0000539#ifdef CONFIG_CMD_BOOTZ
540
Simon Glassa5266d62013-07-04 13:26:10 -0700541int __weak bootz_setup(ulong image, ulong *start, ulong *end)
Marek Vasut44f074c2012-03-14 21:52:45 +0000542{
543 /* Please define bootz_setup() for your platform */
544
545 puts("Your platform's zImage format isn't supported yet!\n");
546 return -1;
547}
Marek Vasut44f074c2012-03-14 21:52:45 +0000548
549/*
550 * zImage booting support
551 */
552static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc,
553 char * const argv[], bootm_headers_t *images)
554{
555 int ret;
Simon Glassa5266d62013-07-04 13:26:10 -0700556 ulong zi_start, zi_end;
Marek Vasut44f074c2012-03-14 21:52:45 +0000557
Simon Glass35fc84f2013-06-11 11:14:47 -0700558 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
559 images, 1);
Marek Vasut44f074c2012-03-14 21:52:45 +0000560
561 /* Setup Linux kernel zImage entry point */
Tom Rini2b9599e2013-07-09 15:32:34 -0400562 if (!argc) {
Marek Vasut44f074c2012-03-14 21:52:45 +0000563 images->ep = load_addr;
564 debug("* kernel: default image load address = 0x%08lx\n",
565 load_addr);
566 } else {
Tom Rini2b9599e2013-07-09 15:32:34 -0400567 images->ep = simple_strtoul(argv[0], NULL, 16);
Marek Vasut44f074c2012-03-14 21:52:45 +0000568 debug("* kernel: cmdline image address = 0x%08lx\n",
569 images->ep);
570 }
571
Simon Glassa5266d62013-07-04 13:26:10 -0700572 ret = bootz_setup(images->ep, &zi_start, &zi_end);
Marek Vasut44f074c2012-03-14 21:52:45 +0000573 if (ret != 0)
574 return 1;
575
576 lmb_reserve(&images->lmb, images->ep, zi_end - zi_start);
577
Tom Rini225fd8c2013-07-09 15:33:25 -0400578 /*
579 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
580 * have a header that provide this informaiton.
581 */
Simon Glassb6396402014-06-12 07:24:46 -0600582 if (bootm_find_ramdisk_fdt(flag, argc, argv))
Tom Rini2b9599e2013-07-09 15:32:34 -0400583 return 1;
Marek Vasut44f074c2012-03-14 21:52:45 +0000584
Tom Rini2b9599e2013-07-09 15:32:34 -0400585 return 0;
Marek Vasut44f074c2012-03-14 21:52:45 +0000586}
587
Rob Herringda620222012-12-02 21:00:23 -0600588int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Marek Vasut44f074c2012-03-14 21:52:45 +0000589{
Simon Glass35fc84f2013-06-11 11:14:47 -0700590 int ret;
Marek Vasut44f074c2012-03-14 21:52:45 +0000591
Tom Rini2b9599e2013-07-09 15:32:34 -0400592 /* Consume 'bootz' */
593 argc--; argv++;
594
Marek Vasut44f074c2012-03-14 21:52:45 +0000595 if (bootz_start(cmdtp, flag, argc, argv, &images))
596 return 1;
597
Simon Glass385501d2013-07-04 13:26:08 -0700598 /*
599 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
600 * disable interrupts ourselves
601 */
602 bootm_disable_interrupts();
603
Simon Glassfb1b1392013-07-04 13:26:11 -0700604 images.os.os = IH_OS_LINUX;
Simon Glass35fc84f2013-06-11 11:14:47 -0700605 ret = do_bootm_states(cmdtp, flag, argc, argv,
Simon Glassfb1b1392013-07-04 13:26:11 -0700606 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
607 BOOTM_STATE_OS_GO,
Simon Glassd0ae31e2013-06-11 11:14:48 -0700608 &images, 1);
Marek Vasut44f074c2012-03-14 21:52:45 +0000609
Simon Glass35fc84f2013-06-11 11:14:47 -0700610 return ret;
Marek Vasut44f074c2012-03-14 21:52:45 +0000611}
612
Kim Phillips088f1b12012-10-29 13:34:31 +0000613#ifdef CONFIG_SYS_LONGHELP
614static char bootz_help_text[] =
Marek Vasut017e1f32012-03-18 11:47:58 +0000615 "[addr [initrd[:size]] [fdt]]\n"
616 " - boot Linux zImage stored in memory\n"
Marek Vasut44f074c2012-03-14 21:52:45 +0000617 "\tThe argument 'initrd' is optional and specifies the address\n"
Marek Vasut017e1f32012-03-18 11:47:58 +0000618 "\tof the initrd in memory. The optional argument ':size' allows\n"
619 "\tspecifying the size of RAW initrd.\n"
Marek Vasut44f074c2012-03-14 21:52:45 +0000620#if defined(CONFIG_OF_LIBFDT)
621 "\tWhen booting a Linux kernel which requires a flat device-tree\n"
622 "\ta third argument is required which is the address of the\n"
623 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
624 "\tuse a '-' for the second argument. If you do not pass a third\n"
625 "\ta bd_info struct will be passed instead\n"
626#endif
Kim Phillips088f1b12012-10-29 13:34:31 +0000627 "";
628#endif
629
630U_BOOT_CMD(
631 bootz, CONFIG_SYS_MAXARGS, 1, do_bootz,
632 "boot Linux zImage image from memory", bootz_help_text
Marek Vasut44f074c2012-03-14 21:52:45 +0000633);
634#endif /* CONFIG_CMD_BOOTZ */
Tom Rinid2b2ffe2014-08-14 06:42:36 -0400635
636#ifdef CONFIG_CMD_BOOTI
637/* See Documentation/arm64/booting.txt in the Linux kernel */
638struct Image_header {
639 uint32_t code0; /* Executable code */
640 uint32_t code1; /* Executable code */
641 uint64_t text_offset; /* Image load offset, LE */
642 uint64_t image_size; /* Effective Image size, LE */
643 uint64_t res1; /* reserved */
644 uint64_t res2; /* reserved */
645 uint64_t res3; /* reserved */
646 uint64_t res4; /* reserved */
647 uint32_t magic; /* Magic number */
648 uint32_t res5;
649};
650
651#define LINUX_ARM64_IMAGE_MAGIC 0x644d5241
652
653static int booti_setup(bootm_headers_t *images)
654{
655 struct Image_header *ih;
656 uint64_t dst;
657
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");
667 ih->image_size = (16 << 20);
668 }
669
670 /*
671 * If we are not at the correct run-time location, set the new
672 * correct location and then move the image there.
673 */
674 dst = gd->bd->bi_dram[0].start + le32_to_cpu(ih->text_offset);
675 if (images->ep != dst) {
676 void *src;
677
678 debug("Moving Image from 0x%lx to 0x%llx\n", images->ep, dst);
679
680 src = (void *)images->ep;
681 images->ep = dst;
682 memmove((void *)dst, src, le32_to_cpu(ih->image_size));
683 }
684
685 return 0;
686}
687
688/*
689 * Image booting support
690 */
691static int booti_start(cmd_tbl_t *cmdtp, int flag, int argc,
692 char * const argv[], bootm_headers_t *images)
693{
694 int ret;
695 struct Image_header *ih;
696
697 ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START,
698 images, 1);
699
700 /* Setup Linux kernel Image entry point */
701 if (!argc) {
702 images->ep = load_addr;
703 debug("* kernel: default image load address = 0x%08lx\n",
704 load_addr);
705 } else {
706 images->ep = simple_strtoul(argv[0], NULL, 16);
707 debug("* kernel: cmdline image address = 0x%08lx\n",
708 images->ep);
709 }
710
711 ret = booti_setup(images);
712 if (ret != 0)
713 return 1;
714
715 ih = (struct Image_header *)map_sysmem(images->ep, 0);
716
717 lmb_reserve(&images->lmb, images->ep, le32_to_cpu(ih->image_size));
718
719 /*
720 * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not
721 * have a header that provide this informaiton.
722 */
723 if (bootm_find_ramdisk_fdt(flag, argc, argv))
724 return 1;
725
726 return 0;
727}
728
729int do_booti(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
730{
731 int ret;
732
733 /* Consume 'booti' */
734 argc--; argv++;
735
736 if (booti_start(cmdtp, flag, argc, argv, &images))
737 return 1;
738
739 /*
740 * We are doing the BOOTM_STATE_LOADOS state ourselves, so must
741 * disable interrupts ourselves
742 */
743 bootm_disable_interrupts();
744
745 images.os.os = IH_OS_LINUX;
746 ret = do_bootm_states(cmdtp, flag, argc, argv,
747 BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
748 BOOTM_STATE_OS_GO,
749 &images, 1);
750
751 return ret;
752}
753
754#ifdef CONFIG_SYS_LONGHELP
755static char booti_help_text[] =
756 "[addr [initrd[:size]] [fdt]]\n"
757 " - boot Linux Image stored in memory\n"
758 "\tThe argument 'initrd' is optional and specifies the address\n"
759 "\tof the initrd in memory. The optional argument ':size' allows\n"
760 "\tspecifying the size of RAW initrd.\n"
761#if defined(CONFIG_OF_LIBFDT)
762 "\tSince booting a Linux kernelrequires a flat device-tree\n"
763 "\ta third argument is required which is the address of the\n"
764 "\tdevice-tree blob. To boot that kernel without an initrd image,\n"
765 "\tuse a '-' for the second argument.\n"
766#endif
767 "";
768#endif
769
770U_BOOT_CMD(
771 booti, CONFIG_SYS_MAXARGS, 1, do_booti,
772 "boot arm64 Linux Image image from memory", booti_help_text
773);
774#endif /* CONFIG_CMD_BOOTI */