]>
Commit | Line | Data |
---|---|---|
83d290c5 | 1 | // SPDX-License-Identifier: GPL-2.0+ |
47d1a6e1 | 2 | /* |
ca95c9df | 3 | * (C) Copyright 2000-2009 |
47d1a6e1 | 4 | * Wolfgang Denk, DENX Software Engineering, [email protected]. |
47d1a6e1 WD |
5 | */ |
6 | ||
7 | /* | |
8 | * Boot support | |
9 | */ | |
10 | #include <common.h> | |
b6396403 | 11 | #include <bootm.h> |
47d1a6e1 | 12 | #include <command.h> |
7b51b576 | 13 | #include <env.h> |
90268b87 | 14 | #include <errno.h> |
b6396403 | 15 | #include <image.h> |
b6396403 SG |
16 | #include <malloc.h> |
17 | #include <nand.h> | |
47d1a6e1 | 18 | #include <asm/byteorder.h> |
401d1c4f | 19 | #include <asm/global_data.h> |
b6396403 SG |
20 | #include <linux/ctype.h> |
21 | #include <linux/err.h> | |
22 | #include <u-boot/zlib.h> | |
ae0d12f8 | 23 | #include <mapmem.h> |
20dde48b | 24 | |
1ee1180b | 25 | DECLARE_GLOBAL_DATA_PTR; |
228f29ac | 26 | |
baa26db4 | 27 | #if defined(CONFIG_CMD_IMI) |
712fbcf3 | 28 | static int image_info(unsigned long addr); |
47d1a6e1 | 29 | #endif |
27b207fd | 30 | |
baa26db4 | 31 | #if defined(CONFIG_CMD_IMLS) |
27b207fd | 32 | #include <flash.h> |
ca5def3f | 33 | #include <mtd/cfi_flash.h> |
8fdf1e0f VK |
34 | #endif |
35 | ||
36 | #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND) | |
09140113 SG |
37 | static int do_imls(struct cmd_tbl *cmdtp, int flag, int argc, |
38 | char *const argv[]); | |
27b207fd WD |
39 | #endif |
40 | ||
49c3a861 KG |
41 | /* we overload the cmd field with our state machine info instead of a |
42 | * function pointer */ | |
09140113 | 43 | static struct cmd_tbl cmd_bootm_sub[] = { |
49c3a861 KG |
44 | U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""), |
45 | U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""), | |
7bebc11c PR |
46 | #ifdef CONFIG_CMD_BOOTM_PRE_LOAD |
47 | U_BOOT_CMD_MKENT(preload, 0, 1, (void *)BOOTM_STATE_PRE_LOAD, "", ""), | |
48 | #endif | |
fca43cc8 | 49 | #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH |
49c3a861 KG |
50 | 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 | |
49c3a861 | 55 | U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""), |
224c90d1 | 56 | U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""), |
49c3a861 | 57 | U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""), |
d0ae31eb | 58 | U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""), |
49c3a861 KG |
59 | U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""), |
60 | }; | |
61 | ||
7bebc11c PR |
62 | #if defined(CONFIG_CMD_BOOTM_PRE_LOAD) |
63 | static ulong bootm_get_addr(int argc, char *const argv[]) | |
64 | { | |
65 | ulong addr; | |
66 | ||
67 | if (argc > 0) | |
68 | addr = hextoul(argv[0], NULL); | |
69 | else | |
70 | addr = image_load_addr; | |
71 | ||
72 | return addr; | |
73 | } | |
74 | #endif | |
75 | ||
09140113 SG |
76 | static int do_bootm_subcommand(struct cmd_tbl *cmdtp, int flag, int argc, |
77 | char *const argv[]) | |
49c3a861 | 78 | { |
a6c49161 | 79 | struct bootm_info bmi; |
49c3a861 | 80 | int ret = 0; |
6d6f1236 | 81 | long state; |
09140113 | 82 | struct cmd_tbl *c; |
49c3a861 | 83 | |
983c72f4 SG |
84 | c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub)); |
85 | argc--; argv++; | |
49c3a861 KG |
86 | |
87 | if (c) { | |
6d6f1236 | 88 | state = (long)c->cmd; |
983c72f4 | 89 | if (state == BOOTM_STATE_START) |
9d46e63d PR |
90 | state |= BOOTM_STATE_PRE_LOAD | BOOTM_STATE_FINDOS | |
91 | BOOTM_STATE_FINDOTHER; | |
7bebc11c PR |
92 | #if defined(CONFIG_CMD_BOOTM_PRE_LOAD) |
93 | if (state == BOOTM_STATE_PRE_LOAD) | |
94 | state |= BOOTM_STATE_START; | |
95 | #endif | |
47e26b1b WD |
96 | } else { |
97 | /* Unrecognized command */ | |
4c12eeb8 | 98 | return CMD_RET_USAGE; |
49c3a861 KG |
99 | } |
100 | ||
ff6c032e HS |
101 | if (((state & BOOTM_STATE_START) != BOOTM_STATE_START) && |
102 | images.state >= state) { | |
712fbcf3 | 103 | printf("Trying to execute a command out of order\n"); |
4c12eeb8 | 104 | return CMD_RET_USAGE; |
49c3a861 KG |
105 | } |
106 | ||
a6c49161 SG |
107 | bootm_init(&bmi); |
108 | if (argc) | |
109 | bmi.addr_img = argv[0]; | |
110 | if (argc > 1) | |
111 | bmi.conf_ramdisk = argv[1]; | |
112 | if (argc > 2) | |
113 | bmi.conf_fdt = argv[2]; | |
114 | bmi.cmd_name = "bootm"; | |
115 | bmi.boot_progress = false; | |
116 | ||
117 | /* set up argc and argv[] since some OSes use them */ | |
118 | bmi.argc = argc; | |
119 | bmi.argv = argv; | |
120 | ||
121 | ret = do_bootm_states(&bmi, state); | |
49c3a861 | 122 | |
7bebc11c PR |
123 | #if defined(CONFIG_CMD_BOOTM_PRE_LOAD) |
124 | if (!ret && (state & BOOTM_STATE_PRE_LOAD)) | |
125 | env_set_hex("loadaddr_verified", | |
126 | bootm_get_addr(argc, argv) + image_load_offset); | |
127 | #endif | |
128 | ||
f06443f9 | 129 | return ret ? CMD_RET_FAILURE : 0; |
49c3a861 KG |
130 | } |
131 | ||
396f635b KG |
132 | /*******************************************************************/ |
133 | /* bootm - boot application image from image in memory */ | |
134 | /*******************************************************************/ | |
be083159 | 135 | |
09140113 | 136 | int do_bootm(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
396f635b | 137 | { |
a6c49161 | 138 | struct bootm_info bmi; |
2c0b61d5 | 139 | int states; |
f06443f9 SG |
140 | int ret; |
141 | ||
49c3a861 | 142 | /* determine if we have a sub command */ |
983c72f4 SG |
143 | argc--; argv++; |
144 | if (argc > 0) { | |
49c3a861 KG |
145 | char *endp; |
146 | ||
7e5f460e | 147 | hextoul(argv[0], &endp); |
983c72f4 | 148 | /* endp pointing to NULL means that argv[0] was just a |
49c3a861 KG |
149 | * valid number, pass it along to the normal bootm processing |
150 | * | |
151 | * If endp is ':' or '#' assume a FIT identifier so pass | |
152 | * along for normal processing. | |
153 | * | |
154 | * Right now we assume the first arg should never be '-' | |
155 | */ | |
156 | if ((*endp != 0) && (*endp != ':') && (*endp != '#')) | |
157 | return do_bootm_subcommand(cmdtp, flag, argc, argv); | |
158 | } | |
159 | ||
2c0b61d5 SG |
160 | states = BOOTM_STATE_START | BOOTM_STATE_FINDOS | BOOTM_STATE_PRE_LOAD | |
161 | BOOTM_STATE_FINDOTHER | BOOTM_STATE_LOADOS | | |
5c427e49 | 162 | BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO | |
2c0b61d5 SG |
163 | BOOTM_STATE_OS_GO; |
164 | if (IS_ENABLED(CONFIG_SYS_BOOT_RAMDISK_HIGH)) | |
165 | states |= BOOTM_STATE_RAMDISK; | |
dec166d6 EJ |
166 | if (IS_ENABLED(CONFIG_MEASURED_BOOT)) |
167 | states |= BOOTM_STATE_MEASURE; | |
2c0b61d5 SG |
168 | if (IS_ENABLED(CONFIG_PPC) || IS_ENABLED(CONFIG_MIPS)) |
169 | states |= BOOTM_STATE_OS_CMDLINE; | |
a6c49161 SG |
170 | |
171 | bootm_init(&bmi); | |
172 | if (argc) | |
173 | bmi.addr_img = argv[0]; | |
174 | if (argc > 1) | |
175 | bmi.conf_ramdisk = argv[1]; | |
176 | if (argc > 2) | |
177 | bmi.conf_fdt = argv[2]; | |
178 | ||
179 | /* set up argc and argv[] since some OSes use them */ | |
180 | bmi.argc = argc; | |
181 | bmi.argv = argv; | |
182 | ||
183 | ret = do_bootm_states(&bmi, states); | |
f06443f9 SG |
184 | |
185 | return ret ? CMD_RET_FAILURE : 0; | |
47d1a6e1 WD |
186 | } |
187 | ||
09140113 | 188 | int bootm_maybe_autostart(struct cmd_tbl *cmdtp, const char *cmd) |
67d668bf | 189 | { |
78398652 | 190 | if (env_get_autostart()) { |
67d668bf MF |
191 | char *local_args[2]; |
192 | local_args[0] = (char *)cmd; | |
193 | local_args[1] = NULL; | |
bb872dd9 SG |
194 | printf("Automatic boot of image at addr 0x%08lX ...\n", |
195 | image_load_addr); | |
67d668bf MF |
196 | return do_bootm(cmdtp, 0, 1, local_args); |
197 | } | |
198 | ||
199 | return 0; | |
200 | } | |
201 | ||
3616218b | 202 | U_BOOT_LONGHELP(bootm, |
1ee1180b MB |
203 | "[addr [arg ...]]\n - boot application image stored in memory\n" |
204 | "\tpassing arguments 'arg ...'; when booting a Linux kernel,\n" | |
205 | "\t'arg' can be the address of an initrd image\n" | |
4a2ad5ff | 206 | #if defined(CONFIG_OF_LIBFDT) |
98a9c4d4 | 207 | "\tWhen booting a Linux kernel which requires a flat device-tree\n" |
5441f61a | 208 | "\ta third argument is required which is the address of the\n" |
98a9c4d4 MM |
209 | "\tdevice-tree blob. To boot that kernel without an initrd image,\n" |
210 | "\tuse a '-' for the second argument. If you do not pass a third\n" | |
211 | "\ta bd_info struct will be passed instead\n" | |
98a9c4d4 | 212 | #endif |
6986a385 MB |
213 | #if defined(CONFIG_FIT) |
214 | "\t\nFor the new multi component uImage format (FIT) addresses\n" | |
da1a3bd4 | 215 | "\tmust be extended to include component or configuration unit name:\n" |
6986a385 MB |
216 | "\taddr:<subimg_uname> - direct component image specification\n" |
217 | "\taddr#<conf_uname> - configuration specification\n" | |
218 | "\tUse iminfo command to get the list of existing component\n" | |
219 | "\timages and configurations.\n" | |
220 | #endif | |
49c3a861 KG |
221 | "\nSub-commands to do part of the bootm sequence. The sub-commands " |
222 | "must be\n" | |
223 | "issued in the order below (it's ok to not issue all sub-commands):\n" | |
224 | "\tstart [addr [arg ...]]\n" | |
7bebc11c PR |
225 | #if defined(CONFIG_CMD_BOOTM_PRE_LOAD) |
226 | "\tpreload [addr [arg ..]] - run only the preload stage\n" | |
227 | #endif | |
49c3a861 | 228 | "\tloados - load OS image\n" |
59af76d9 | 229 | #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH) |
49c3a861 KG |
230 | "\tramdisk - relocate initrd, set env initrd_start/initrd_end\n" |
231 | #endif | |
232 | #if defined(CONFIG_OF_LIBFDT) | |
233 | "\tfdt - relocate flat device tree\n" | |
234 | #endif | |
49c3a861 | 235 | "\tcmdline - OS specific command line processing/setup\n" |
bb5a2cf9 | 236 | "\tbdt - OS specific bd_info processing\n" |
49c3a861 | 237 | "\tprep - OS specific prep before relocation or go\n" |
e3046ba4 MS |
238 | #if defined(CONFIG_TRACE) |
239 | "\tfake - OS specific fake start without go\n" | |
240 | #endif | |
3616218b | 241 | "\tgo - start OS"); |
088f1b19 KP |
242 | |
243 | U_BOOT_CMD( | |
244 | bootm, CONFIG_SYS_MAXARGS, 1, do_bootm, | |
245 | "boot application image from memory", bootm_help_text | |
8bde7f77 | 246 | ); |
47d1a6e1 | 247 | |
1ee1180b MB |
248 | /*******************************************************************/ |
249 | /* bootd - boot default image */ | |
250 | /*******************************************************************/ | |
baa26db4 | 251 | #if defined(CONFIG_CMD_BOOTD) |
09140113 | 252 | int do_bootd(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[]) |
47d1a6e1 | 253 | { |
00caae6d | 254 | return run_command(env_get("bootcmd"), flag); |
47d1a6e1 | 255 | } |
47d1a6e1 | 256 | |
0d498393 | 257 | U_BOOT_CMD( |
1ee1180b | 258 | boot, 1, 1, do_bootd, |
2fb2604d | 259 | "boot default, i.e., run 'bootcmd'", |
a89c33db | 260 | "" |
9d2b18a0 | 261 | ); |
47d1a6e1 | 262 | |
9d2b18a0 | 263 | /* keep old command name "bootd" for backward compatibility */ |
0d498393 | 264 | U_BOOT_CMD( |
1ee1180b | 265 | bootd, 1, 1, do_bootd, |
2fb2604d | 266 | "boot default, i.e., run 'bootcmd'", |
a89c33db | 267 | "" |
8bde7f77 | 268 | ); |
47d1a6e1 | 269 | |
47d1a6e1 | 270 | #endif |
47d1a6e1 | 271 | |
47d1a6e1 | 272 | |
1ee1180b MB |
273 | /*******************************************************************/ |
274 | /* iminfo - print header info for a requested image */ | |
275 | /*******************************************************************/ | |
baa26db4 | 276 | #if defined(CONFIG_CMD_IMI) |
09140113 SG |
277 | static int do_iminfo(struct cmd_tbl *cmdtp, int flag, int argc, |
278 | char *const argv[]) | |
47d1a6e1 WD |
279 | { |
280 | int arg; | |
281 | ulong addr; | |
1ee1180b | 282 | int rcode = 0; |
47d1a6e1 | 283 | |
47d1a6e1 | 284 | if (argc < 2) { |
bb872dd9 | 285 | return image_info(image_load_addr); |
47d1a6e1 | 286 | } |
47d1a6e1 | 287 | |
1ee1180b | 288 | for (arg = 1; arg < argc; ++arg) { |
7e5f460e | 289 | addr = hextoul(argv[arg], NULL); |
712fbcf3 | 290 | if (image_info(addr) != 0) |
1ee1180b | 291 | rcode = 1; |
47d1a6e1 WD |
292 | } |
293 | return rcode; | |
294 | } | |
47d1a6e1 | 295 | |
712fbcf3 | 296 | static int image_info(ulong addr) |
47d1a6e1 | 297 | { |
ae0d12f8 | 298 | void *hdr = (void *)map_sysmem(addr, 0); |
47d1a6e1 | 299 | |
712fbcf3 | 300 | printf("\n## Checking Image at %08lx ...\n", addr); |
47d1a6e1 | 301 | |
712fbcf3 | 302 | switch (genimg_get_format(hdr)) { |
c76c93a3 | 303 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
d5934ad7 | 304 | case IMAGE_FORMAT_LEGACY: |
712fbcf3 SW |
305 | puts(" Legacy image found\n"); |
306 | if (!image_check_magic(hdr)) { | |
307 | puts(" Bad Magic Number\n"); | |
ae0d12f8 | 308 | unmap_sysmem(hdr); |
d5934ad7 MB |
309 | return 1; |
310 | } | |
47d1a6e1 | 311 | |
712fbcf3 SW |
312 | if (!image_check_hcrc(hdr)) { |
313 | puts(" Bad Header Checksum\n"); | |
ae0d12f8 | 314 | unmap_sysmem(hdr); |
d5934ad7 | 315 | return 1; |
47d1a6e1 WD |
316 | } |
317 | ||
712fbcf3 | 318 | image_print_contents(hdr); |
47d1a6e1 | 319 | |
712fbcf3 SW |
320 | puts(" Verifying Checksum ... "); |
321 | if (!image_check_dcrc(hdr)) { | |
322 | puts(" Bad Data CRC\n"); | |
ae0d12f8 | 323 | unmap_sysmem(hdr); |
d5934ad7 | 324 | return 1; |
47d1a6e1 | 325 | } |
712fbcf3 | 326 | puts("OK\n"); |
ae0d12f8 | 327 | unmap_sysmem(hdr); |
d5934ad7 | 328 | return 0; |
21d29f7f | 329 | #endif |
4f1318b2 MT |
330 | #if defined(CONFIG_ANDROID_BOOT_IMAGE) |
331 | case IMAGE_FORMAT_ANDROID: | |
332 | puts(" Android image found\n"); | |
333 | android_print_contents(hdr); | |
ae0d12f8 | 334 | unmap_sysmem(hdr); |
4f1318b2 MT |
335 | return 0; |
336 | #endif | |
d5934ad7 MB |
337 | #if defined(CONFIG_FIT) |
338 | case IMAGE_FORMAT_FIT: | |
712fbcf3 | 339 | puts(" FIT image found\n"); |
47d1a6e1 | 340 | |
c5819701 | 341 | if (fit_check_format(hdr, IMAGE_SIZE_INVAL)) { |
712fbcf3 | 342 | puts("Bad FIT image format!\n"); |
ae0d12f8 | 343 | unmap_sysmem(hdr); |
e32fea6a | 344 | return 1; |
47d1a6e1 WD |
345 | } |
346 | ||
712fbcf3 | 347 | fit_print_contents(hdr); |
a4f24345 | 348 | |
b8da8366 | 349 | if (!fit_all_image_verify(hdr)) { |
712fbcf3 | 350 | puts("Bad hash in FIT image!\n"); |
ae0d12f8 | 351 | unmap_sysmem(hdr); |
a4f24345 BS |
352 | return 1; |
353 | } | |
354 | ||
ae0d12f8 | 355 | unmap_sysmem(hdr); |
d5934ad7 MB |
356 | return 0; |
357 | #endif | |
358 | default: | |
712fbcf3 | 359 | puts("Unknown image format!\n"); |
d5934ad7 | 360 | break; |
47d1a6e1 WD |
361 | } |
362 | ||
ae0d12f8 | 363 | unmap_sysmem(hdr); |
d5934ad7 | 364 | return 1; |
47d1a6e1 | 365 | } |
0d498393 WD |
366 | |
367 | U_BOOT_CMD( | |
6d0f6bcf | 368 | iminfo, CONFIG_SYS_MAXARGS, 1, do_iminfo, |
2fb2604d | 369 | "print header information for application image", |
8bde7f77 WD |
370 | "addr [addr ...]\n" |
371 | " - print header information for application image starting at\n" | |
372 | " address 'addr' in memory; this includes verification of the\n" | |
a89c33db | 373 | " image contents (magic number, header and payload checksums)" |
8bde7f77 | 374 | ); |
25c751e9 | 375 | #endif |
87a449c8 | 376 | |
25c751e9 | 377 | |
1ee1180b MB |
378 | /*******************************************************************/ |
379 | /* imls - list all images found in flash */ | |
380 | /*******************************************************************/ | |
baa26db4 | 381 | #if defined(CONFIG_CMD_IMLS) |
8fdf1e0f | 382 | static int do_imls_nor(void) |
27b207fd WD |
383 | { |
384 | flash_info_t *info; | |
385 | int i, j; | |
d5934ad7 | 386 | void *hdr; |
25c751e9 | 387 | |
1ee1180b | 388 | for (i = 0, info = &flash_info[0]; |
98150e7e | 389 | i < CFI_FLASH_BANKS; ++i, ++info) { |
25c751e9 | 390 | |
27b207fd WD |
391 | if (info->flash_id == FLASH_UNKNOWN) |
392 | goto next_bank; | |
1ee1180b | 393 | for (j = 0; j < info->sector_count; ++j) { |
25c751e9 | 394 | |
d5934ad7 MB |
395 | hdr = (void *)info->start[j]; |
396 | if (!hdr) | |
b97a2a0a | 397 | goto next_sector; |
27b207fd | 398 | |
712fbcf3 | 399 | switch (genimg_get_format(hdr)) { |
c76c93a3 | 400 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
d5934ad7 | 401 | case IMAGE_FORMAT_LEGACY: |
712fbcf3 | 402 | if (!image_check_hcrc(hdr)) |
d5934ad7 MB |
403 | goto next_sector; |
404 | ||
712fbcf3 SW |
405 | printf("Legacy Image at %08lX:\n", (ulong)hdr); |
406 | image_print_contents(hdr); | |
d5934ad7 | 407 | |
712fbcf3 SW |
408 | puts(" Verifying Checksum ... "); |
409 | if (!image_check_dcrc(hdr)) { | |
410 | puts("Bad Data CRC\n"); | |
d5934ad7 | 411 | } else { |
712fbcf3 | 412 | puts("OK\n"); |
d5934ad7 MB |
413 | } |
414 | break; | |
21d29f7f | 415 | #endif |
d5934ad7 MB |
416 | #if defined(CONFIG_FIT) |
417 | case IMAGE_FORMAT_FIT: | |
c5819701 | 418 | if (fit_check_format(hdr, IMAGE_SIZE_INVAL)) |
e32fea6a MB |
419 | goto next_sector; |
420 | ||
712fbcf3 SW |
421 | printf("FIT Image at %08lX:\n", (ulong)hdr); |
422 | fit_print_contents(hdr); | |
d5934ad7 | 423 | break; |
213bf8c8 | 424 | #endif |
d5934ad7 | 425 | default: |
27b207fd | 426 | goto next_sector; |
25c751e9 | 427 | } |
87a449c8 | 428 | |
bdccc4fe | 429 | next_sector: ; |
c76f951a | 430 | } |
bdccc4fe | 431 | next_bank: ; |
27b207fd | 432 | } |
8fdf1e0f VK |
433 | return 0; |
434 | } | |
435 | #endif | |
436 | ||
437 | #if defined(CONFIG_CMD_IMLS_NAND) | |
151c06ec SW |
438 | static int nand_imls_legacyimage(struct mtd_info *mtd, int nand_dev, |
439 | loff_t off, size_t len) | |
8fdf1e0f VK |
440 | { |
441 | void *imgdata; | |
442 | int ret; | |
443 | ||
444 | imgdata = malloc(len); | |
445 | if (!imgdata) { | |
446 | printf("May be a Legacy Image at NAND device %d offset %08llX:\n", | |
447 | nand_dev, off); | |
448 | printf(" Low memory(cannot allocate memory for image)\n"); | |
449 | return -ENOMEM; | |
450 | } | |
451 | ||
dbe7881d | 452 | ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata); |
8fdf1e0f VK |
453 | if (ret < 0 && ret != -EUCLEAN) { |
454 | free(imgdata); | |
455 | return ret; | |
456 | } | |
457 | ||
458 | if (!image_check_hcrc(imgdata)) { | |
459 | free(imgdata); | |
460 | return 0; | |
461 | } | |
462 | ||
463 | printf("Legacy Image at NAND device %d offset %08llX:\n", | |
464 | nand_dev, off); | |
465 | image_print_contents(imgdata); | |
466 | ||
467 | puts(" Verifying Checksum ... "); | |
468 | if (!image_check_dcrc(imgdata)) | |
469 | puts("Bad Data CRC\n"); | |
470 | else | |
471 | puts("OK\n"); | |
472 | ||
473 | free(imgdata); | |
474 | ||
475 | return 0; | |
476 | } | |
477 | ||
151c06ec SW |
478 | static int nand_imls_fitimage(struct mtd_info *mtd, int nand_dev, loff_t off, |
479 | size_t len) | |
8fdf1e0f VK |
480 | { |
481 | void *imgdata; | |
482 | int ret; | |
483 | ||
484 | imgdata = malloc(len); | |
485 | if (!imgdata) { | |
486 | printf("May be a FIT Image at NAND device %d offset %08llX:\n", | |
487 | nand_dev, off); | |
488 | printf(" Low memory(cannot allocate memory for image)\n"); | |
489 | return -ENOMEM; | |
490 | } | |
491 | ||
dbe7881d | 492 | ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata); |
8fdf1e0f VK |
493 | if (ret < 0 && ret != -EUCLEAN) { |
494 | free(imgdata); | |
495 | return ret; | |
496 | } | |
497 | ||
c5819701 | 498 | if (fit_check_format(imgdata, IMAGE_SIZE_INVAL)) { |
8fdf1e0f VK |
499 | free(imgdata); |
500 | return 0; | |
501 | } | |
502 | ||
503 | printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off); | |
504 | ||
505 | fit_print_contents(imgdata); | |
506 | free(imgdata); | |
507 | ||
508 | return 0; | |
509 | } | |
510 | ||
511 | static int do_imls_nand(void) | |
512 | { | |
151c06ec | 513 | struct mtd_info *mtd; |
8fdf1e0f VK |
514 | int nand_dev = nand_curr_device; |
515 | size_t len; | |
516 | loff_t off; | |
517 | u32 buffer[16]; | |
518 | ||
519 | if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) { | |
520 | puts("\nNo NAND devices available\n"); | |
521 | return -ENODEV; | |
522 | } | |
523 | ||
524 | printf("\n"); | |
525 | ||
526 | for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) { | |
f370b515 | 527 | mtd = get_nand_dev_by_index(nand_dev); |
151c06ec | 528 | if (!mtd->name || !mtd->size) |
8fdf1e0f VK |
529 | continue; |
530 | ||
151c06ec | 531 | for (off = 0; off < mtd->size; off += mtd->erasesize) { |
f3543e69 | 532 | const struct legacy_img_hdr *header; |
8fdf1e0f VK |
533 | int ret; |
534 | ||
151c06ec | 535 | if (nand_block_isbad(mtd, off)) |
8fdf1e0f VK |
536 | continue; |
537 | ||
538 | len = sizeof(buffer); | |
539 | ||
151c06ec | 540 | ret = nand_read(mtd, off, &len, (u8 *)buffer); |
8fdf1e0f VK |
541 | if (ret < 0 && ret != -EUCLEAN) { |
542 | printf("NAND read error %d at offset %08llX\n", | |
543 | ret, off); | |
544 | continue; | |
545 | } | |
546 | ||
547 | switch (genimg_get_format(buffer)) { | |
c76c93a3 | 548 | #if defined(CONFIG_LEGACY_IMAGE_FORMAT) |
8fdf1e0f | 549 | case IMAGE_FORMAT_LEGACY: |
f3543e69 | 550 | header = (const struct legacy_img_hdr *)buffer; |
8fdf1e0f VK |
551 | |
552 | len = image_get_image_size(header); | |
151c06ec | 553 | nand_imls_legacyimage(mtd, nand_dev, off, len); |
8fdf1e0f | 554 | break; |
21d29f7f | 555 | #endif |
8fdf1e0f VK |
556 | #if defined(CONFIG_FIT) |
557 | case IMAGE_FORMAT_FIT: | |
558 | len = fit_get_size(buffer); | |
151c06ec | 559 | nand_imls_fitimage(mtd, nand_dev, off, len); |
8fdf1e0f VK |
560 | break; |
561 | #endif | |
562 | } | |
563 | } | |
564 | } | |
565 | ||
566 | return 0; | |
567 | } | |
568 | #endif | |
569 | ||
570 | #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND) | |
09140113 SG |
571 | static int do_imls(struct cmd_tbl *cmdtp, int flag, int argc, |
572 | char *const argv[]) | |
8fdf1e0f VK |
573 | { |
574 | int ret_nor = 0, ret_nand = 0; | |
575 | ||
576 | #if defined(CONFIG_CMD_IMLS) | |
577 | ret_nor = do_imls_nor(); | |
578 | #endif | |
579 | ||
580 | #if defined(CONFIG_CMD_IMLS_NAND) | |
581 | ret_nand = do_imls_nand(); | |
582 | #endif | |
583 | ||
584 | if (ret_nor) | |
585 | return ret_nor; | |
586 | ||
587 | if (ret_nand) | |
588 | return ret_nand; | |
c76f951a | 589 | |
27b207fd WD |
590 | return (0); |
591 | } | |
c76f951a | 592 | |
27b207fd WD |
593 | U_BOOT_CMD( |
594 | imls, 1, 1, do_imls, | |
2fb2604d | 595 | "list all images found in flash", |
27b207fd | 596 | "\n" |
8fdf1e0f VK |
597 | " - Prints information about all images found at sector/block\n" |
598 | " boundaries in nor/nand flash." | |
27b207fd | 599 | ); |
98a9c4d4 | 600 | #endif |