xref: /openbmc/u-boot/cmd/bootm.c (revision 2502bfc4)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * (C) Copyright 2000-2009
4  * Wolfgang Denk, DENX Software Engineering, wd@denx.de.
5  */
6 
7 /*
8  * Boot support
9  */
10 #include <common.h>
11 #include <bootm.h>
12 #include <command.h>
13 #include <environment.h>
14 #include <errno.h>
15 #include <image.h>
16 #include <malloc.h>
17 #include <nand.h>
18 #include <asm/byteorder.h>
19 #include <linux/ctype.h>
20 #include <linux/err.h>
21 #include <u-boot/zlib.h>
22 
23 DECLARE_GLOBAL_DATA_PTR;
24 
25 #if defined(CONFIG_CMD_IMI)
26 static int image_info(unsigned long addr);
27 #endif
28 
29 #if defined(CONFIG_CMD_IMLS)
30 #include <flash.h>
31 #include <mtd/cfi_flash.h>
32 extern flash_info_t flash_info[]; /* info for FLASH chips */
33 #endif
34 
35 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
36 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[]);
37 #endif
38 
39 /* we overload the cmd field with our state machine info instead of a
40  * function pointer */
41 static cmd_tbl_t cmd_bootm_sub[] = {
42 	U_BOOT_CMD_MKENT(start, 0, 1, (void *)BOOTM_STATE_START, "", ""),
43 	U_BOOT_CMD_MKENT(loados, 0, 1, (void *)BOOTM_STATE_LOADOS, "", ""),
44 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
45 	U_BOOT_CMD_MKENT(ramdisk, 0, 1, (void *)BOOTM_STATE_RAMDISK, "", ""),
46 #endif
47 #ifdef CONFIG_OF_LIBFDT
48 	U_BOOT_CMD_MKENT(fdt, 0, 1, (void *)BOOTM_STATE_FDT, "", ""),
49 #endif
50 	U_BOOT_CMD_MKENT(cmdline, 0, 1, (void *)BOOTM_STATE_OS_CMDLINE, "", ""),
51 	U_BOOT_CMD_MKENT(bdt, 0, 1, (void *)BOOTM_STATE_OS_BD_T, "", ""),
52 	U_BOOT_CMD_MKENT(prep, 0, 1, (void *)BOOTM_STATE_OS_PREP, "", ""),
53 	U_BOOT_CMD_MKENT(fake, 0, 1, (void *)BOOTM_STATE_OS_FAKE_GO, "", ""),
54 	U_BOOT_CMD_MKENT(go, 0, 1, (void *)BOOTM_STATE_OS_GO, "", ""),
55 };
56 
do_bootm_subcommand(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])57 static int do_bootm_subcommand(cmd_tbl_t *cmdtp, int flag, int argc,
58 			char * const argv[])
59 {
60 	int ret = 0;
61 	long state;
62 	cmd_tbl_t *c;
63 
64 	c = find_cmd_tbl(argv[0], &cmd_bootm_sub[0], ARRAY_SIZE(cmd_bootm_sub));
65 	argc--; argv++;
66 
67 	if (c) {
68 		state = (long)c->cmd;
69 		if (state == BOOTM_STATE_START)
70 			state |= BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER;
71 	} else {
72 		/* Unrecognized command */
73 		return CMD_RET_USAGE;
74 	}
75 
76 	if (((state & BOOTM_STATE_START) != BOOTM_STATE_START) &&
77 	    images.state >= state) {
78 		printf("Trying to execute a command out of order\n");
79 		return CMD_RET_USAGE;
80 	}
81 
82 	ret = do_bootm_states(cmdtp, flag, argc, argv, state, &images, 0);
83 
84 	return ret;
85 }
86 
87 /*******************************************************************/
88 /* bootm - boot application image from image in memory */
89 /*******************************************************************/
90 
do_bootm(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])91 int do_bootm(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
92 {
93 #ifdef CONFIG_NEEDS_MANUAL_RELOC
94 	static int relocated = 0;
95 
96 	if (!relocated) {
97 		int i;
98 
99 		/* relocate names of sub-command table */
100 		for (i = 0; i < ARRAY_SIZE(cmd_bootm_sub); i++)
101 			cmd_bootm_sub[i].name += gd->reloc_off;
102 
103 		relocated = 1;
104 	}
105 #endif
106 
107 	/* determine if we have a sub command */
108 	argc--; argv++;
109 	if (argc > 0) {
110 		char *endp;
111 
112 		simple_strtoul(argv[0], &endp, 16);
113 		/* endp pointing to NULL means that argv[0] was just a
114 		 * valid number, pass it along to the normal bootm processing
115 		 *
116 		 * If endp is ':' or '#' assume a FIT identifier so pass
117 		 * along for normal processing.
118 		 *
119 		 * Right now we assume the first arg should never be '-'
120 		 */
121 		if ((*endp != 0) && (*endp != ':') && (*endp != '#'))
122 			return do_bootm_subcommand(cmdtp, flag, argc, argv);
123 	}
124 
125 	return do_bootm_states(cmdtp, flag, argc, argv, BOOTM_STATE_START |
126 		BOOTM_STATE_FINDOS | BOOTM_STATE_FINDOTHER |
127 		BOOTM_STATE_LOADOS |
128 #ifdef CONFIG_SYS_BOOT_RAMDISK_HIGH
129 		BOOTM_STATE_RAMDISK |
130 #endif
131 #ifdef CONFIG_MEASURED_BOOT
132 		BOOTM_STATE_MEASURE |
133 #endif
134 #if defined(CONFIG_PPC) || defined(CONFIG_MIPS)
135 		BOOTM_STATE_OS_CMDLINE |
136 #endif
137 		BOOTM_STATE_OS_PREP | BOOTM_STATE_OS_FAKE_GO |
138 		BOOTM_STATE_OS_GO, &images, 1);
139 }
140 
bootm_maybe_autostart(cmd_tbl_t * cmdtp,const char * cmd)141 int bootm_maybe_autostart(cmd_tbl_t *cmdtp, const char *cmd)
142 {
143 	const char *ep = env_get("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 
156 #ifdef CONFIG_SYS_LONGHELP
157 static char bootm_help_text[] =
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"
161 #if defined(CONFIG_OF_LIBFDT)
162 	"\tWhen booting a Linux kernel which requires a flat device-tree\n"
163 	"\ta third argument is required which is the address of the\n"
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"
167 #endif
168 #if defined(CONFIG_FIT)
169 	"\t\nFor the new multi component uImage format (FIT) addresses\n"
170 	"\tmust be extended 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
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"
181 #if defined(CONFIG_SYS_BOOT_RAMDISK_HIGH)
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
187 	"\tcmdline - OS specific command line processing/setup\n"
188 	"\tbdt     - OS specific bd_t processing\n"
189 	"\tprep    - OS specific prep before relocation or go\n"
190 #if defined(CONFIG_TRACE)
191 	"\tfake    - OS specific fake start without go\n"
192 #endif
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
199 );
200 
201 /*******************************************************************/
202 /* bootd - boot default image */
203 /*******************************************************************/
204 #if defined(CONFIG_CMD_BOOTD)
do_bootd(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])205 int do_bootd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
206 {
207 	return run_command(env_get("bootcmd"), flag);
208 }
209 
210 U_BOOT_CMD(
211 	boot,	1,	1,	do_bootd,
212 	"boot default, i.e., run 'bootcmd'",
213 	""
214 );
215 
216 /* keep old command name "bootd" for backward compatibility */
217 U_BOOT_CMD(
218 	bootd, 1,	1,	do_bootd,
219 	"boot default, i.e., run 'bootcmd'",
220 	""
221 );
222 
223 #endif
224 
225 
226 /*******************************************************************/
227 /* iminfo - print header info for a requested image */
228 /*******************************************************************/
229 #if defined(CONFIG_CMD_IMI)
do_iminfo(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])230 static int do_iminfo(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
231 {
232 	int	arg;
233 	ulong	addr;
234 	int	rcode = 0;
235 
236 	if (argc < 2) {
237 		return image_info(load_addr);
238 	}
239 
240 	for (arg = 1; arg < argc; ++arg) {
241 		addr = simple_strtoul(argv[arg], NULL, 16);
242 		if (image_info(addr) != 0)
243 			rcode = 1;
244 	}
245 	return rcode;
246 }
247 
image_info(ulong addr)248 static int image_info(ulong addr)
249 {
250 	void *hdr = (void *)addr;
251 
252 	printf("\n## Checking Image at %08lx ...\n", addr);
253 
254 	switch (genimg_get_format(hdr)) {
255 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
256 	case IMAGE_FORMAT_LEGACY:
257 		puts("   Legacy image found\n");
258 		if (!image_check_magic(hdr)) {
259 			puts("   Bad Magic Number\n");
260 			return 1;
261 		}
262 
263 		if (!image_check_hcrc(hdr)) {
264 			puts("   Bad Header Checksum\n");
265 			return 1;
266 		}
267 
268 		image_print_contents(hdr);
269 
270 		puts("   Verifying Checksum ... ");
271 		if (!image_check_dcrc(hdr)) {
272 			puts("   Bad Data CRC\n");
273 			return 1;
274 		}
275 		puts("OK\n");
276 		return 0;
277 #endif
278 #if defined(CONFIG_ANDROID_BOOT_IMAGE)
279 	case IMAGE_FORMAT_ANDROID:
280 		puts("   Android image found\n");
281 		android_print_contents(hdr);
282 		return 0;
283 #endif
284 #if defined(CONFIG_FIT)
285 	case IMAGE_FORMAT_FIT:
286 		puts("   FIT image found\n");
287 
288 		if (!fit_check_format(hdr)) {
289 			puts("Bad FIT image format!\n");
290 			return 1;
291 		}
292 
293 		fit_print_contents(hdr);
294 
295 		if (!fit_all_image_verify(hdr)) {
296 			puts("Bad hash in FIT image!\n");
297 			return 1;
298 		}
299 
300 		return 0;
301 #endif
302 	default:
303 		puts("Unknown image format!\n");
304 		break;
305 	}
306 
307 	return 1;
308 }
309 
310 U_BOOT_CMD(
311 	iminfo,	CONFIG_SYS_MAXARGS,	1,	do_iminfo,
312 	"print header information for application image",
313 	"addr [addr ...]\n"
314 	"    - print header information for application image starting at\n"
315 	"      address 'addr' in memory; this includes verification of the\n"
316 	"      image contents (magic number, header and payload checksums)"
317 );
318 #endif
319 
320 
321 /*******************************************************************/
322 /* imls - list all images found in flash */
323 /*******************************************************************/
324 #if defined(CONFIG_CMD_IMLS)
do_imls_nor(void)325 static int do_imls_nor(void)
326 {
327 	flash_info_t *info;
328 	int i, j;
329 	void *hdr;
330 
331 	for (i = 0, info = &flash_info[0];
332 		i < CONFIG_SYS_MAX_FLASH_BANKS; ++i, ++info) {
333 
334 		if (info->flash_id == FLASH_UNKNOWN)
335 			goto next_bank;
336 		for (j = 0; j < info->sector_count; ++j) {
337 
338 			hdr = (void *)info->start[j];
339 			if (!hdr)
340 				goto next_sector;
341 
342 			switch (genimg_get_format(hdr)) {
343 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
344 			case IMAGE_FORMAT_LEGACY:
345 				if (!image_check_hcrc(hdr))
346 					goto next_sector;
347 
348 				printf("Legacy Image at %08lX:\n", (ulong)hdr);
349 				image_print_contents(hdr);
350 
351 				puts("   Verifying Checksum ... ");
352 				if (!image_check_dcrc(hdr)) {
353 					puts("Bad Data CRC\n");
354 				} else {
355 					puts("OK\n");
356 				}
357 				break;
358 #endif
359 #if defined(CONFIG_FIT)
360 			case IMAGE_FORMAT_FIT:
361 				if (!fit_check_format(hdr))
362 					goto next_sector;
363 
364 				printf("FIT Image at %08lX:\n", (ulong)hdr);
365 				fit_print_contents(hdr);
366 				break;
367 #endif
368 			default:
369 				goto next_sector;
370 			}
371 
372 next_sector:		;
373 		}
374 next_bank:	;
375 	}
376 	return 0;
377 }
378 #endif
379 
380 #if defined(CONFIG_CMD_IMLS_NAND)
nand_imls_legacyimage(struct mtd_info * mtd,int nand_dev,loff_t off,size_t len)381 static int nand_imls_legacyimage(struct mtd_info *mtd, int nand_dev,
382 				 loff_t off, size_t len)
383 {
384 	void *imgdata;
385 	int ret;
386 
387 	imgdata = malloc(len);
388 	if (!imgdata) {
389 		printf("May be a Legacy Image at NAND device %d offset %08llX:\n",
390 				nand_dev, off);
391 		printf("   Low memory(cannot allocate memory for image)\n");
392 		return -ENOMEM;
393 	}
394 
395 	ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
396 	if (ret < 0 && ret != -EUCLEAN) {
397 		free(imgdata);
398 		return ret;
399 	}
400 
401 	if (!image_check_hcrc(imgdata)) {
402 		free(imgdata);
403 		return 0;
404 	}
405 
406 	printf("Legacy Image at NAND device %d offset %08llX:\n",
407 			nand_dev, off);
408 	image_print_contents(imgdata);
409 
410 	puts("   Verifying Checksum ... ");
411 	if (!image_check_dcrc(imgdata))
412 		puts("Bad Data CRC\n");
413 	else
414 		puts("OK\n");
415 
416 	free(imgdata);
417 
418 	return 0;
419 }
420 
nand_imls_fitimage(struct mtd_info * mtd,int nand_dev,loff_t off,size_t len)421 static int nand_imls_fitimage(struct mtd_info *mtd, int nand_dev, loff_t off,
422 			      size_t len)
423 {
424 	void *imgdata;
425 	int ret;
426 
427 	imgdata = malloc(len);
428 	if (!imgdata) {
429 		printf("May be a FIT Image at NAND device %d offset %08llX:\n",
430 				nand_dev, off);
431 		printf("   Low memory(cannot allocate memory for image)\n");
432 		return -ENOMEM;
433 	}
434 
435 	ret = nand_read_skip_bad(mtd, off, &len, NULL, mtd->size, imgdata);
436 	if (ret < 0 && ret != -EUCLEAN) {
437 		free(imgdata);
438 		return ret;
439 	}
440 
441 	if (!fit_check_format(imgdata)) {
442 		free(imgdata);
443 		return 0;
444 	}
445 
446 	printf("FIT Image at NAND device %d offset %08llX:\n", nand_dev, off);
447 
448 	fit_print_contents(imgdata);
449 	free(imgdata);
450 
451 	return 0;
452 }
453 
do_imls_nand(void)454 static int do_imls_nand(void)
455 {
456 	struct mtd_info *mtd;
457 	int nand_dev = nand_curr_device;
458 	size_t len;
459 	loff_t off;
460 	u32 buffer[16];
461 
462 	if (nand_dev < 0 || nand_dev >= CONFIG_SYS_MAX_NAND_DEVICE) {
463 		puts("\nNo NAND devices available\n");
464 		return -ENODEV;
465 	}
466 
467 	printf("\n");
468 
469 	for (nand_dev = 0; nand_dev < CONFIG_SYS_MAX_NAND_DEVICE; nand_dev++) {
470 		mtd = get_nand_dev_by_index(nand_dev);
471 		if (!mtd->name || !mtd->size)
472 			continue;
473 
474 		for (off = 0; off < mtd->size; off += mtd->erasesize) {
475 			const image_header_t *header;
476 			int ret;
477 
478 			if (nand_block_isbad(mtd, off))
479 				continue;
480 
481 			len = sizeof(buffer);
482 
483 			ret = nand_read(mtd, off, &len, (u8 *)buffer);
484 			if (ret < 0 && ret != -EUCLEAN) {
485 				printf("NAND read error %d at offset %08llX\n",
486 						ret, off);
487 				continue;
488 			}
489 
490 			switch (genimg_get_format(buffer)) {
491 #if defined(CONFIG_IMAGE_FORMAT_LEGACY)
492 			case IMAGE_FORMAT_LEGACY:
493 				header = (const image_header_t *)buffer;
494 
495 				len = image_get_image_size(header);
496 				nand_imls_legacyimage(mtd, nand_dev, off, len);
497 				break;
498 #endif
499 #if defined(CONFIG_FIT)
500 			case IMAGE_FORMAT_FIT:
501 				len = fit_get_size(buffer);
502 				nand_imls_fitimage(mtd, nand_dev, off, len);
503 				break;
504 #endif
505 			}
506 		}
507 	}
508 
509 	return 0;
510 }
511 #endif
512 
513 #if defined(CONFIG_CMD_IMLS) || defined(CONFIG_CMD_IMLS_NAND)
do_imls(cmd_tbl_t * cmdtp,int flag,int argc,char * const argv[])514 static int do_imls(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
515 {
516 	int ret_nor = 0, ret_nand = 0;
517 
518 #if defined(CONFIG_CMD_IMLS)
519 	ret_nor = do_imls_nor();
520 #endif
521 
522 #if defined(CONFIG_CMD_IMLS_NAND)
523 	ret_nand = do_imls_nand();
524 #endif
525 
526 	if (ret_nor)
527 		return ret_nor;
528 
529 	if (ret_nand)
530 		return ret_nand;
531 
532 	return (0);
533 }
534 
535 U_BOOT_CMD(
536 	imls,	1,		1,	do_imls,
537 	"list all images found in flash",
538 	"\n"
539 	"    - Prints information about all images found at sector/block\n"
540 	"      boundaries in nor/nand flash."
541 );
542 #endif
543