]>
Commit | Line | Data |
---|---|---|
47d1a6e1 | 1 | /* |
ca95c9df | 2 | * (C) Copyright 2000-2009 |
47d1a6e1 WD |
3 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
4 | * | |
1a459660 | 5 | * SPDX-License-Identifier: GPL-2.0+ |
47d1a6e1 WD |
6 | */ |
7 | ||
8 | /* | |
9 | * Boot support | |
10 | */ | |
11 | #include <common.h> | |
b6396403 | 12 | #include <bootm.h> |
47d1a6e1 | 13 | #include <command.h> |
7f70e853 | 14 | #include <environment.h> |
90268b87 | 15 | #include <errno.h> |
b6396403 | 16 | #include <image.h> |
4ed6552f | 17 | #include <lmb.h> |
b6396403 | 18 | #include <malloc.h> |
0eb25b61 | 19 | #include <mapmem.h> |
b6396403 | 20 | #include <nand.h> |
47d1a6e1 | 21 | #include <asm/byteorder.h> |
5bf2766b | 22 | #include <linux/compiler.h> |
b6396403 SG |
23 | #include <linux/ctype.h> |
24 | #include <linux/err.h> | |
25 | #include <u-boot/zlib.h> | |
20dde48b | 26 | |
1ee1180b | 27 | DECLARE_GLOBAL_DATA_PTR; |
228f29ac | 28 | |
baa26db4 | 29 | #if defined(CONFIG_CMD_IMI) |
712fbcf3 | 30 | static int image_info(unsigned long addr); |
47d1a6e1 | 31 | #endif |
27b207fd | 32 | |
baa26db4 | 33 | #if defined(CONFIG_CMD_IMLS) |
27b207fd | 34 | #include <flash.h> |
ca5def3f | 35 | #include <mtd/cfi_flash.h> |
e6f2e902 | 36 | extern flash_info_t flash_info[]; /* info for FLASH chips */ |
8fdf1e0f VK |
37 | #endif |
38 | ||
39 | #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND) | |
712fbcf3 | 40 | static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]); |
27b207fd WD |
41 | #endif |
42 | ||
dee17768 | 43 | bootm_headers_t images; /* pointers to os/initrd/fdt images */ |
15940c9a | 44 | |
49c3a861 KG |
45 | /* we overload the cmd field with our state machine info instead of a |
46 | * function pointer */ | |
f74d9bd2 | 47 | static cmd_tbl_t cmd_bootm_sub[] = { |
49c3a861 KG |
48 | U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""), |
49 | U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""), | |
fca43cc8 | 50 | #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH |
49c3a861 KG |
51 | 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 | |
49c3a861 | 56 | U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""), |
224c90d1 | 57 | U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""), |
49c3a861 | 58 | U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""), |
d0ae31eb | 59 | U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""), |
49c3a861 KG |
60 | U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""), |
61 | }; | |
62 | ||
088f1b19 | 63 | static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc, |
712fbcf3 | 64 | char * const argv[]) |
49c3a861 KG |
65 | { |
66 | int ret = 0; | |
6d6f1236 | 67 | long state; |
49c3a861 | 68 | cmd_tbl_t *c; |
49c3a861 | 69 | |
983c72f4 SG |
70 | c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub)); |
71 | argc--; argv++; | |
49c3a861 KG |
72 | |
73 | if (c) { | |
6d6f1236 | 74 | state = (long)c->cmd; |
983c72f4 | 75 | if (state == BOOTM_STATE_START) |
35fc84fa | 76 | state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER; |
47e26b1b WD |
77 | } else { |
78 | /* Unrecognized command */ | |
4c12eeb8 | 79 | return CMD_RET_USAGE; |
49c3a861 KG |
80 | } |
81 | ||
ff6c032e HS |
82 | if (((state & BOOTM_STATE_START) != BOOTM_STATE_START) && |
83 | images.state >= state) { | |
712fbcf3 | 84 | printf("Trying to execute a command out of order\n"); |
4c12eeb8 | 85 | return CMD_RET_USAGE; |
49c3a861 KG |
86 | } |
87 | ||
35fc84fa | 88 | ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0); |
49c3a861 KG |
89 | |
90 | return ret; | |
91 | } | |
92 | ||
396f635b KG |
93 | /*******************************************************************/ |
94 | /* bootm - boot application image from image in memory */ | |
95 | /*******************************************************************/ | |
be083159 | 96 | |
712fbcf3 | 97 | int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
396f635b | 98 | { |
2e5167cc | 99 | #ifdef CONFIG_NEEDS_MANUAL_RELOC |
521af04d | 100 | static int relocated = 0; |
be083159 | 101 | |
be083159 KG |
102 | if (!relocated) { |
103 | int i; | |
58bd77db | 104 | |
58bd77db DS |
105 | /* 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 | ||
be083159 KG |
109 | relocated = 1; |
110 | } | |
521af04d | 111 | #endif |
396f635b | 112 | |
49c3a861 | 113 | /* determine if we have a sub command */ |
983c72f4 SG |
114 | argc--; argv++; |
115 | if (argc > 0) { | |
49c3a861 KG |
116 | char *endp; |
117 | ||
983c72f4 SG |
118 | simple_strtoul(argv[0], &endp, 16); |
119 | /* endp pointing to NULL means that argv[0] was just a | |
49c3a861 KG |
120 | * 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 | ||
35fc84fa SG |
131 | return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START | |
132 | BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER | | |
3d187b39 TR |
133 | BOOTM_STATE_LOADOS | |
134 | #if defined(CONFIG_PPC) || defined(CONFIG_MIPS) | |
135 | BOOTM_STATE_OS_CMDLINE | | |
136 | #endif | |
5c427e49 PB |
137 | BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO | |
138 | BOOTM_STATE_OS_GO, &images, 1); | |
47d1a6e1 WD |
139 | } |
140 | ||
67d668bf MF |
141 | int 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 | ||
088f1b19 KP |
156 | #ifdef CONFIG_SYS_LONGHELP |
157 | static char bootm_help_text[] = | |
1ee1180b MB |
158 | "[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" | |
4a2ad5ff | 161 | #if defined(CONFIG_OF_LIBFDT) |
98a9c4d4 | 162 | "\tWhen booting a Linux kernel which requires a flat device-tree\n" |
5441f61a | 163 | "\ta third argument is required which is the address of the\n" |
98a9c4d4 MM |
164 | "\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" | |
98a9c4d4 | 167 | #endif |
6986a385 MB |
168 | #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 | |
49c3a861 KG |
176 | "\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" | |
59af76d9 | 181 | #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH) |
49c3a861 KG |
182 | "\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 | |
49c3a861 | 187 | "\tcmdline - OS specific command line processing/setup\n" |
224c90d1 | 188 | "\tbdt - OS specific bd_t processing\n" |
49c3a861 | 189 | "\tprep - OS specific prep before relocation or go\n" |
e3046ba4 MS |
190 | #if defined(CONFIG_TRACE) |
191 | "\tfake - OS specific fake start without go\n" | |
192 | #endif | |
088f1b19 KP |
193 | "\tgo - start OS"; |
194 | #endif | |
195 | ||
196 | U_BOOT_CMD( | |
197 | bootm, CONFIG_SYS_MAXARGS, 1, do_bootm, | |
198 | "boot application image from memory", bootm_help_text | |
8bde7f77 | 199 | ); |
47d1a6e1 | 200 | |
1ee1180b MB |
201 | /*******************************************************************/ |
202 | /* bootd - boot default image */ | |
203 | /*******************************************************************/ | |
baa26db4 | 204 | #if defined(CONFIG_CMD_BOOTD) |
712fbcf3 | 205 | int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
47d1a6e1 | 206 | { |
73671dad | 207 | return run_command(getenv("bootcmd"), flag); |
47d1a6e1 | 208 | } |
47d1a6e1 | 209 | |
0d498393 | 210 | U_BOOT_CMD( |
1ee1180b | 211 | boot, 1, 1, do_bootd, |
2fb2604d | 212 | "boot default, i.e., run 'bootcmd'", |
a89c33db | 213 | "" |
9d2b18a0 | 214 | ); |
47d1a6e1 | 215 | |
9d2b18a0 | 216 | /* keep old command name "bootd" for backward compatibility */ |
0d498393 | 217 | U_BOOT_CMD( |
1ee1180b | 218 | bootd, 1, 1, do_bootd, |
2fb2604d | 219 | "boot default, i.e., run 'bootcmd'", |
a89c33db | 220 | "" |
8bde7f77 | 221 | ); |
47d1a6e1 | 222 | |
47d1a6e1 | 223 | #endif |
47d1a6e1 | 224 | |
47d1a6e1 | 225 | |
1ee1180b MB |
226 | /*******************************************************************/ |
227 | /* iminfo - print header info for a requested image */ | |
228 | /*******************************************************************/ | |
baa26db4 | 229 | #if defined(CONFIG_CMD_IMI) |
088f1b19 | 230 | static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
47d1a6e1 WD |
231 | { |
232 | int arg; | |
233 | ulong addr; | |
1ee1180b | 234 | int rcode = 0; |
47d1a6e1 | 235 | |
47d1a6e1 | 236 | if (argc < 2) { |
712fbcf3 | 237 | return image_info(load_addr); |
47d1a6e1 | 238 | } |
47d1a6e1 | 239 | |
1ee1180b | 240 | for (arg = 1; arg < argc; ++arg) { |
712fbcf3 SW |
241 | addr = simple_strtoul(argv[arg], NULL, 16); |
242 | if (image_info(addr) != 0) | |
1ee1180b | 243 | rcode = 1; |
47d1a6e1 WD |
244 | } |
245 | return rcode; | |
246 | } | |
47d1a6e1 | 247 | |
712fbcf3 | 248 | static int image_info(ulong addr) |
47d1a6e1 | 249 | { |
d5934ad7 | 250 | void *hdr = (void *)addr; |
47d1a6e1 | 251 | |
712fbcf3 | 252 | printf("\n## Checking Image at %08lx ...\n", addr); |
47d1a6e1 | 253 | |
712fbcf3 | 254 | switch (genimg_get_format(hdr)) { |
21d29f7f | 255 | #if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
d5934ad7 | 256 | case IMAGE_FORMAT_LEGACY: |
712fbcf3 SW |
257 | puts(" Legacy image found\n"); |
258 | if (!image_check_magic(hdr)) { | |
259 | puts(" Bad Magic Number\n"); | |
d5934ad7 MB |
260 | return 1; |
261 | } | |
47d1a6e1 | 262 | |
712fbcf3 SW |
263 | if (!image_check_hcrc(hdr)) { |
264 | puts(" Bad Header Checksum\n"); | |
d5934ad7 | 265 | return 1; |
47d1a6e1 WD |
266 | } |
267 | ||
712fbcf3 | 268 | image_print_contents(hdr); |
47d1a6e1 | 269 | |
712fbcf3 SW |
270 | puts(" Verifying Checksum ... "); |
271 | if (!image_check_dcrc(hdr)) { | |
272 | puts(" Bad Data CRC\n"); | |
d5934ad7 | 273 | return 1; |
47d1a6e1 | 274 | } |
712fbcf3 | 275 | puts("OK\n"); |
d5934ad7 | 276 | return 0; |
21d29f7f | 277 | #endif |
d5934ad7 MB |
278 | #if defined(CONFIG_FIT) |
279 | case IMAGE_FORMAT_FIT: | |
712fbcf3 | 280 | puts(" FIT image found\n"); |
47d1a6e1 | 281 | |
712fbcf3 SW |
282 | if (!fit_check_format(hdr)) { |
283 | puts("Bad FIT image format!\n"); | |
e32fea6a | 284 | return 1; |
47d1a6e1 WD |
285 | } |
286 | ||
712fbcf3 | 287 | fit_print_contents(hdr); |
a4f24345 | 288 | |
b8da8366 | 289 | if (!fit_all_image_verify(hdr)) { |
712fbcf3 | 290 | puts("Bad hash in FIT image!\n"); |
a4f24345 BS |
291 | return 1; |
292 | } | |
293 | ||
d5934ad7 MB |
294 | return 0; |
295 | #endif | |
296 | default: | |
712fbcf3 | 297 | puts("Unknown image format!\n"); |
d5934ad7 | 298 | break; |
47d1a6e1 WD |
299 | } |
300 | ||
d5934ad7 | 301 | return 1; |
47d1a6e1 | 302 | } |
0d498393 WD |
303 | |
304 | U_BOOT_CMD( | |
6d0f6bcf | 305 | iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo, |
2fb2604d | 306 | "print header information for application image", |
8bde7f77 WD |
307 | "addr [addr ...]\n" |
308 | " - print header information for application image starting at\n" | |
309 | " address 'addr' in memory; this includes verification of the\n" | |
a89c33db | 310 | " image contents (magic number, header and payload checksums)" |
8bde7f77 | 311 | ); |
25c751e9 | 312 | #endif |
87a449c8 | 313 | |
25c751e9 | 314 | |
1ee1180b MB |
315 | /*******************************************************************/ |
316 | /* imls - list all images found in flash */ | |
317 | /*******************************************************************/ | |
baa26db4 | 318 | #if defined(CONFIG_CMD_IMLS) |
8fdf1e0f | 319 | static int do_imls_nor(void) |
27b207fd WD |
320 | { |
321 | flash_info_t *info; | |
322 | int i, j; | |
d5934ad7 | 323 | void *hdr; |
25c751e9 | 324 | |
1ee1180b | 325 | for (i = 0, info = &flash_info[0]; |
6d0f6bcf | 326 | i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) { |
25c751e9 | 327 | |
27b207fd WD |
328 | if (info->flash_id == FLASH_UNKNOWN) |
329 | goto next_bank; | |
1ee1180b | 330 | for (j = 0; j < info->sector_count; ++j) { |
25c751e9 | 331 | |
d5934ad7 MB |
332 | hdr = (void *)info->start[j]; |
333 | if (!hdr) | |
b97a2a0a | 334 | goto next_sector; |
27b207fd | 335 | |
712fbcf3 | 336 | switch (genimg_get_format(hdr)) { |
21d29f7f | 337 | #if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
d5934ad7 | 338 | case IMAGE_FORMAT_LEGACY: |
712fbcf3 | 339 | if (!image_check_hcrc(hdr)) |
d5934ad7 MB |
340 | goto next_sector; |
341 | ||
712fbcf3 SW |
342 | printf("Legacy Image at %08lX:\n", (ulong)hdr); |
343 | image_print_contents(hdr); | |
d5934ad7 | 344 | |
712fbcf3 SW |
345 | puts(" Verifying Checksum ... "); |
346 | if (!image_check_dcrc(hdr)) { | |
347 | puts("Bad Data CRC\n"); | |
d5934ad7 | 348 | } else { |
712fbcf3 | 349 | puts("OK\n"); |
d5934ad7 MB |
350 | } |
351 | break; | |
21d29f7f | 352 | #endif |
d5934ad7 MB |
353 | #if defined(CONFIG_FIT) |
354 | case IMAGE_FORMAT_FIT: | |
712fbcf3 | 355 | if (!fit_check_format(hdr)) |
e32fea6a MB |
356 | goto next_sector; |
357 | ||
712fbcf3 SW |
358 | printf("FIT Image at %08lX:\n", (ulong)hdr); |
359 | fit_print_contents(hdr); | |
d5934ad7 | 360 | break; |
213bf8c8 | 361 | #endif |
d5934ad7 | 362 | default: |
27b207fd | 363 | goto next_sector; |
25c751e9 | 364 | } |
87a449c8 | 365 | |
bdccc4fe | 366 | next_sector: ; |
c76f951a | 367 | } |
bdccc4fe | 368 | next_bank: ; |
27b207fd | 369 | } |
8fdf1e0f VK |
370 | return 0; |
371 | } | |
372 | #endif | |
373 | ||
374 | #if defined(CONFIG_CMD_IMLS_NAND) | |
151c06ec SW |
375 | static int nand_imls_legacyimage(struct mtd_info *mtd, int nand_dev, |
376 | loff_t off, size_t len) | |
8fdf1e0f VK |
377 | { |
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 | ||
151c06ec | 389 | ret = nand_read_skip_bad(mtd, off, &len, imgdata); |
8fdf1e0f VK |
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 | ||
151c06ec SW |
415 | static int nand_imls_fitimage(struct mtd_info *mtd, int nand_dev, loff_t off, |
416 | size_t len) | |
8fdf1e0f VK |
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 | ||
151c06ec | 429 | ret = nand_read_skip_bad(mtd, off, &len, imgdata); |
8fdf1e0f VK |
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 | ||
448 | static int do_imls_nand(void) | |
449 | { | |
151c06ec | 450 | struct mtd_info *mtd; |
8fdf1e0f VK |
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++) { | |
b616d9b0 | 464 | mtd = nand_info[nand_dev]; |
151c06ec | 465 | if (!mtd->name || !mtd->size) |
8fdf1e0f VK |
466 | continue; |
467 | ||
151c06ec | 468 | for (off = 0; off < mtd->size; off += mtd->erasesize) { |
8fdf1e0f VK |
469 | const image_header_t *header; |
470 | int ret; | |
471 | ||
151c06ec | 472 | if (nand_block_isbad(mtd, off)) |
8fdf1e0f VK |
473 | continue; |
474 | ||
475 | len = sizeof(buffer); | |
476 | ||
151c06ec | 477 | ret = nand_read(mtd, off, &len, (u8 *)buffer); |
8fdf1e0f VK |
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)) { | |
21d29f7f | 485 | #if defined(CONFIG_IMAGE_FORMAT_LEGACY) |
8fdf1e0f VK |
486 | case IMAGE_FORMAT_LEGACY: |
487 | header = (const image_header_t *)buffer; | |
488 | ||
489 | len = image_get_image_size(header); | |
151c06ec | 490 | nand_imls_legacyimage(mtd, nand_dev, off, len); |
8fdf1e0f | 491 | break; |
21d29f7f | 492 | #endif |
8fdf1e0f VK |
493 | #if defined(CONFIG_FIT) |
494 | case IMAGE_FORMAT_FIT: | |
495 | len = fit_get_size(buffer); | |
151c06ec | 496 | nand_imls_fitimage(mtd, nand_dev, off, len); |
8fdf1e0f VK |
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) | |
508 | static 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; | |
c76f951a | 525 | |
27b207fd WD |
526 | return (0); |
527 | } | |
c76f951a | 528 | |
27b207fd WD |
529 | U_BOOT_CMD( |
530 | imls, 1, 1, do_imls, | |
2fb2604d | 531 | "list all images found in flash", |
27b207fd | 532 | "\n" |
8fdf1e0f VK |
533 | " - Prints information about all images found at sector/block\n" |
534 | " boundaries in nor/nand flash." | |
27b207fd | 535 | ); |
98a9c4d4 | 536 | #endif |
47d1a6e1 | 537 | |
44f074c7 MV |
538 | #ifdef CONFIG_CMD_BOOTZ |
539 | ||
a5266d6b | 540 | int __weak bootz_setup(ulong image, ulong *start, ulong *end) |
44f074c7 MV |
541 | { |
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 | } | |
44f074c7 MV |
547 | |
548 | /* | |
549 | * zImage booting support | |
550 | */ | |
551 | static int bootz_start(cmd_tbl_t *cmdtp, int flag, int argc, | |
552 | char * const argv[], bootm_headers_t *images) | |
553 | { | |
554 | int ret; | |
a5266d6b | 555 | ulong zi_start, zi_end; |
44f074c7 | 556 | |
35fc84fa SG |
557 | ret = do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START, |
558 | images, 1); | |
44f074c7 MV |
559 | |
560 | /* Setup Linux kernel zImage entry point */ | |
2b9599e0 | 561 | if (!argc) { |
44f074c7 MV |
562 | images->ep = load_addr; |
563 | debug("* kernel: default image load address = 0x%08lx\n", | |
564 | load_addr); | |
565 | } else { | |
2b9599e0 | 566 | images->ep = simple_strtoul(argv[0], NULL, 16); |
44f074c7 MV |
567 | debug("* kernel: cmdline image address = 0x%08lx\n", |
568 | images->ep); | |
569 | } | |
570 | ||
a5266d6b | 571 | ret = bootz_setup(images->ep, &zi_start, &zi_end); |
44f074c7 MV |
572 | if (ret != 0) |
573 | return 1; | |
574 | ||
575 | lmb_reserve(&images->lmb, images->ep, zi_end - zi_start); | |
576 | ||
225fd8c5 TR |
577 | /* |
578 | * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not | |
579 | * have a header that provide this informaiton. | |
580 | */ | |
d52e8575 | 581 | if (bootm_find_images(flag, argc, argv)) |
2b9599e0 | 582 | return 1; |
44f074c7 | 583 | |
2b9599e0 | 584 | return 0; |
44f074c7 MV |
585 | } |
586 | ||
da620222 | 587 | int do_bootz(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]) |
44f074c7 | 588 | { |
35fc84fa | 589 | int ret; |
44f074c7 | 590 | |
2b9599e0 TR |
591 | /* Consume 'bootz' */ |
592 | argc--; argv++; | |
593 | ||
44f074c7 MV |
594 | if (bootz_start(cmdtp, flag, argc, argv, &images)) |
595 | return 1; | |
596 | ||
385501d3 SG |
597 | /* |
598 | * We are doing the BOOTM_STATE_LOADOS state ourselves, so must | |
599 | * disable interrupts ourselves | |
600 | */ | |
601 | bootm_disable_interrupts(); | |
602 | ||
fb1b139b | 603 | images.os.os = IH_OS_LINUX; |
35fc84fa | 604 | ret = do_bootm_states(cmdtp, flag, argc, argv, |
fb1b139b SG |
605 | BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO | |
606 | BOOTM_STATE_OS_GO, | |
d0ae31eb | 607 | &images, 1); |
f8be7d65 | 608 | |
35fc84fa | 609 | return ret; |
44f074c7 MV |
610 | } |
611 | ||
088f1b19 KP |
612 | #ifdef CONFIG_SYS_LONGHELP |
613 | static char bootz_help_text[] = | |
017e1f3f MV |
614 | "[addr [initrd[:size]] [fdt]]\n" |
615 | " - boot Linux zImage stored in memory\n" | |
44f074c7 | 616 | "\tThe argument 'initrd' is optional and specifies the address\n" |
017e1f3f MV |
617 | "\tof the initrd in memory. The optional argument ':size' allows\n" |
618 | "\tspecifying the size of RAW initrd.\n" | |
44f074c7 MV |
619 | #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 | |
088f1b19 KP |
626 | ""; |
627 | #endif | |
628 | ||
629 | U_BOOT_CMD( | |
630 | bootz, CONFIG_SYS_MAXARGS, 1, do_bootz, | |
631 | "boot Linux zImage image from memory", bootz_help_text | |
44f074c7 MV |
632 | ); |
633 | #endif /* CONFIG_CMD_BOOTZ */ | |
d2b2ffe3 TR |
634 | |
635 | #ifdef CONFIG_CMD_BOOTI | |
636 | /* See Documentation/arm64/booting.txt in the Linux kernel */ | |
637 | struct 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 | ||
652 | static int booti_setup(bootm_headers_t *images) | |
653 | { | |
654 | struct Image_header *ih; | |
655 | uint64_t dst; | |
f9a90ace | 656 | uint64_t image_size; |
d2b2ffe3 TR |
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"); | |
f9a90ace AP |
667 | image_size = 16 << 20; |
668 | } else { | |
669 | image_size = le64_to_cpu(ih->image_size); | |
d2b2ffe3 TR |
670 | } |
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 | */ | |
f9a90ace | 676 | dst = gd->bd->bi_dram[0].start + le64_to_cpu(ih->text_offset); |
62022d52 MY |
677 | |
678 | unmap_sysmem(ih); | |
679 | ||
d2b2ffe3 TR |
680 | 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; | |
f9a90ace | 687 | memmove((void *)dst, src, image_size); |
d2b2ffe3 TR |
688 | } |
689 | ||
690 | return 0; | |
691 | } | |
692 | ||
693 | /* | |
694 | * Image booting support | |
695 | */ | |
696 | static 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 | ||
62022d52 MY |
724 | unmap_sysmem(ih); |
725 | ||
d2b2ffe3 TR |
726 | /* |
727 | * Handle the BOOTM_STATE_FINDOTHER state ourselves as we do not | |
728 | * have a header that provide this informaiton. | |
729 | */ | |
d52e8575 | 730 | if (bootm_find_images(flag, argc, argv)) |
d2b2ffe3 TR |
731 | return 1; |
732 | ||
733 | return 0; | |
734 | } | |
735 | ||
736 | int 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 | |
762 | static char booti_help_text[] = | |
763 | "[addr [initrd[:size]] [fdt]]\n" | |
6f6051fa | 764 | " - boot arm64 Linux Image stored in memory\n" |
d2b2ffe3 | 765 | "\tThe argument 'initrd' is optional and specifies the address\n" |
6f6051fa KM |
766 | "\tof an initrd in memory. The optional parameter ':size' allows\n" |
767 | "\tspecifying the size of a RAW initrd.\n" | |
d2b2ffe3 | 768 | #if defined(CONFIG_OF_LIBFDT) |
6f6051fa KM |
769 | "\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" | |
d2b2ffe3 TR |
773 | #endif |
774 | ""; | |
775 | #endif | |
776 | ||
777 | U_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 */ |