1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (c) 2011 The Chromium OS Authors.
4  * (C) Copyright 2002
5  * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
6  */
7 
8 /*
9  * Linux x86 zImage and bzImage loading
10  *
11  * based on the procdure described in
12  * linux/Documentation/i386/boot.txt
13  */
14 
15 #define LOG_CATEGORY	LOGC_BOOT
16 
17 #include <common.h>
18 #include <bootm.h>
19 #include <command.h>
20 #include <env.h>
21 #include <irq_func.h>
22 #include <log.h>
23 #include <malloc.h>
24 #include <acpi/acpi_table.h>
25 #include <asm/io.h>
26 #include <asm/ptrace.h>
27 #include <asm/zimage.h>
28 #include <asm/byteorder.h>
29 #include <asm/bootm.h>
30 #include <asm/bootparam.h>
31 #ifdef CONFIG_SYS_COREBOOT
32 #include <asm/arch/timestamp.h>
33 #endif
34 #include <linux/compiler.h>
35 #include <linux/ctype.h>
36 #include <linux/libfdt.h>
37 
38 /*
39  * Memory lay-out:
40  *
41  * relative to setup_base (which is 0x90000 currently)
42  *
43  *	0x0000-0x7FFF	Real mode kernel
44  *	0x8000-0x8FFF	Stack and heap
45  *	0x9000-0x90FF	Kernel command line
46  */
47 #define DEFAULT_SETUP_BASE	0x90000
48 #define COMMAND_LINE_OFFSET	0x9000
49 #define HEAP_END_OFFSET		0x8e00
50 
51 #define COMMAND_LINE_SIZE	2048
52 
53 /**
54  * struct zboot_state - Current state of the boot
55  *
56  * @bzimage_addr: Address of the bzImage to boot
57  * @bzimage_size: Size of the bzImage, or 0 to detect this
58  * @initrd_addr: Address of the initial ramdisk, or 0 if none
59  * @initrd_size: Size of the initial ramdisk, or 0 if none
60  * @load_address: Address where the bzImage is moved before booting, either
61  *	BZIMAGE_LOAD_ADDR or ZIMAGE_LOAD_ADDR
62  * @base_ptr: Pointer to the boot parameters, typically at address
63  *	DEFAULT_SETUP_BASE
64  * @cmdline: Environment variable containing the 'override' command line, or
65  *	NULL to use the one in the setup block
66  */
67 struct zboot_state {
68 	ulong bzimage_addr;
69 	ulong bzimage_size;
70 	ulong initrd_addr;
71 	ulong initrd_size;
72 	ulong load_address;
73 	struct boot_params *base_ptr;
74 	char *cmdline;
75 } state;
76 
77 enum {
78 	ZBOOT_STATE_START	= BIT(0),
79 	ZBOOT_STATE_LOAD	= BIT(1),
80 	ZBOOT_STATE_SETUP	= BIT(2),
81 	ZBOOT_STATE_INFO	= BIT(3),
82 	ZBOOT_STATE_GO		= BIT(4),
83 
84 	/* This one doesn't execute automatically, so stop the count before 5 */
85 	ZBOOT_STATE_DUMP	= BIT(5),
86 	ZBOOT_STATE_COUNT	= 5,
87 };
88 
build_command_line(char * command_line,int auto_boot)89 static void build_command_line(char *command_line, int auto_boot)
90 {
91 	char *env_command_line;
92 
93 	command_line[0] = '\0';
94 
95 	env_command_line =  env_get("bootargs");
96 
97 	/* set console= argument if we use a serial console */
98 	if (!strstr(env_command_line, "console=")) {
99 		if (!strcmp(env_get("stdout"), "serial")) {
100 
101 			/* We seem to use serial console */
102 			sprintf(command_line, "console=ttyS0,%s ",
103 				env_get("baudrate"));
104 		}
105 	}
106 
107 	if (auto_boot)
108 		strcat(command_line, "auto ");
109 
110 	if (env_command_line)
111 		strcat(command_line, env_command_line);
112 #ifdef DEBUG
113 	printf("Kernel command line:");
114 	puts(command_line);
115 	printf("\n");
116 #endif
117 }
118 
kernel_magic_ok(struct setup_header * hdr)119 static int kernel_magic_ok(struct setup_header *hdr)
120 {
121 	if (KERNEL_MAGIC != hdr->boot_flag) {
122 		printf("Error: Invalid Boot Flag "
123 			"(found 0x%04x, expected 0x%04x)\n",
124 			hdr->boot_flag, KERNEL_MAGIC);
125 		return 0;
126 	} else {
127 		printf("Valid Boot Flag\n");
128 		return 1;
129 	}
130 }
131 
get_boot_protocol(struct setup_header * hdr,bool verbose)132 static int get_boot_protocol(struct setup_header *hdr, bool verbose)
133 {
134 	if (hdr->header == KERNEL_V2_MAGIC) {
135 		if (verbose)
136 			printf("Magic signature found\n");
137 		return hdr->version;
138 	} else {
139 		/* Very old kernel */
140 		if (verbose)
141 			printf("Magic signature not found\n");
142 		return 0x0100;
143 	}
144 }
145 
setup_device_tree(struct setup_header * hdr,const void * fdt_blob)146 static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob)
147 {
148 	int bootproto = get_boot_protocol(hdr, false);
149 	struct setup_data *sd;
150 	int size;
151 
152 	if (bootproto < 0x0209)
153 		return -ENOTSUPP;
154 
155 	if (!fdt_blob)
156 		return 0;
157 
158 	size = fdt_totalsize(fdt_blob);
159 	if (size < 0)
160 		return -EINVAL;
161 
162 	size += sizeof(struct setup_data);
163 	sd = (struct setup_data *)malloc(size);
164 	if (!sd) {
165 		printf("Not enough memory for DTB setup data\n");
166 		return -ENOMEM;
167 	}
168 
169 	sd->next = hdr->setup_data;
170 	sd->type = SETUP_DTB;
171 	sd->len = fdt_totalsize(fdt_blob);
172 	memcpy(sd->data, fdt_blob, sd->len);
173 	hdr->setup_data = (unsigned long)sd;
174 
175 	return 0;
176 }
177 
get_kernel_version(struct boot_params * params,void * kernel_base)178 static const char *get_kernel_version(struct boot_params *params,
179 				      void *kernel_base)
180 {
181 	struct setup_header *hdr = &params->hdr;
182 	int bootproto;
183 	const char *s, *end;
184 
185 	bootproto = get_boot_protocol(hdr, false);
186 	if (bootproto < 0x0200 || hdr->setup_sects < 15)
187 		return NULL;
188 
189 	/* sanity-check the kernel version in case it is missing */
190 	for (s = kernel_base + hdr->kernel_version + 0x200, end = s + 0x100; *s;
191 	     s++) {
192 		if (!isprint(*s))
193 			return NULL;
194 	}
195 
196 	return kernel_base + hdr->kernel_version + 0x200;
197 }
198 
load_zimage(char * image,unsigned long kernel_size,ulong * load_addressp)199 struct boot_params *load_zimage(char *image, unsigned long kernel_size,
200 				ulong *load_addressp)
201 {
202 	struct boot_params *setup_base;
203 	const char *version;
204 	int setup_size;
205 	int bootproto;
206 	int big_image;
207 
208 	struct boot_params *params = (struct boot_params *)image;
209 	struct setup_header *hdr = &params->hdr;
210 
211 	/* base address for real-mode segment */
212 	setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
213 
214 	if (!kernel_magic_ok(hdr))
215 		return 0;
216 
217 	/* determine size of setup */
218 	if (0 == hdr->setup_sects) {
219 		log_warning("Setup Sectors = 0 (defaulting to 4)\n");
220 		setup_size = 5 * 512;
221 	} else {
222 		setup_size = (hdr->setup_sects + 1) * 512;
223 	}
224 
225 	log_debug("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
226 
227 	if (setup_size > SETUP_MAX_SIZE)
228 		printf("Error: Setup is too large (%d bytes)\n", setup_size);
229 
230 	/* determine boot protocol version */
231 	bootproto = get_boot_protocol(hdr, true);
232 
233 	log_debug("Using boot protocol version %x.%02x\n",
234 		  (bootproto & 0xff00) >> 8, bootproto & 0xff);
235 
236 	version = get_kernel_version(params, image);
237 	if (version)
238 		printf("Linux kernel version %s\n", version);
239 	else
240 		printf("Setup Sectors < 15 - Cannot print kernel version\n");
241 
242 	/* Determine image type */
243 	big_image = (bootproto >= 0x0200) &&
244 		    (hdr->loadflags & BIG_KERNEL_FLAG);
245 
246 	/* Determine load address */
247 	if (big_image)
248 		*load_addressp = BZIMAGE_LOAD_ADDR;
249 	else
250 		*load_addressp = ZIMAGE_LOAD_ADDR;
251 
252 	printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
253 	memset(setup_base, 0, sizeof(*setup_base));
254 	setup_base->hdr = params->hdr;
255 
256 	if (bootproto >= 0x0204)
257 		kernel_size = hdr->syssize * 16;
258 	else
259 		kernel_size -= setup_size;
260 
261 	if (bootproto == 0x0100) {
262 		/*
263 		 * A very old kernel MUST have its real-mode code
264 		 * loaded at 0x90000
265 		 */
266 		if ((ulong)setup_base != 0x90000) {
267 			/* Copy the real-mode kernel */
268 			memmove((void *)0x90000, setup_base, setup_size);
269 
270 			/* Copy the command line */
271 			memmove((void *)0x99000,
272 				(u8 *)setup_base + COMMAND_LINE_OFFSET,
273 				COMMAND_LINE_SIZE);
274 
275 			 /* Relocated */
276 			setup_base = (struct boot_params *)0x90000;
277 		}
278 
279 		/* It is recommended to clear memory up to the 32K mark */
280 		memset((u8 *)0x90000 + setup_size, 0,
281 		       SETUP_MAX_SIZE - setup_size);
282 	}
283 
284 	if (big_image) {
285 		if (kernel_size > BZIMAGE_MAX_SIZE) {
286 			printf("Error: bzImage kernel too big! "
287 				"(size: %ld, max: %d)\n",
288 				kernel_size, BZIMAGE_MAX_SIZE);
289 			return 0;
290 		}
291 	} else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
292 		printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
293 		       kernel_size, ZIMAGE_MAX_SIZE);
294 		return 0;
295 	}
296 
297 	printf("Loading %s at address %lx (%ld bytes)\n",
298 	       big_image ? "bzImage" : "zImage", *load_addressp, kernel_size);
299 
300 	memmove((void *)*load_addressp, image + setup_size, kernel_size);
301 
302 	return setup_base;
303 }
304 
setup_zimage(struct boot_params * setup_base,char * cmd_line,int auto_boot,ulong initrd_addr,ulong initrd_size,ulong cmdline_force)305 int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
306 		 ulong initrd_addr, ulong initrd_size, ulong cmdline_force)
307 {
308 	struct setup_header *hdr = &setup_base->hdr;
309 	int bootproto = get_boot_protocol(hdr, false);
310 
311 	log_debug("Setup E820 entries\n");
312 	setup_base->e820_entries = install_e820_map(
313 		ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
314 
315 	if (bootproto == 0x0100) {
316 		setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
317 		setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
318 	}
319 	if (bootproto >= 0x0200) {
320 		hdr->type_of_loader = 0x80;	/* U-Boot version 0 */
321 		if (initrd_addr) {
322 			printf("Initial RAM disk at linear address "
323 			       "0x%08lx, size %ld bytes\n",
324 			       initrd_addr, initrd_size);
325 
326 			hdr->ramdisk_image = initrd_addr;
327 			hdr->ramdisk_size = initrd_size;
328 		}
329 	}
330 
331 	if (bootproto >= 0x0201) {
332 		hdr->heap_end_ptr = HEAP_END_OFFSET;
333 		hdr->loadflags |= HEAP_FLAG;
334 	}
335 
336 	if (cmd_line) {
337 		int max_size = 0xff;
338 		int ret;
339 
340 		log_debug("Setup cmdline\n");
341 		if (bootproto >= 0x0206)
342 			max_size = hdr->cmdline_size;
343 		if (bootproto >= 0x0202) {
344 			hdr->cmd_line_ptr = (uintptr_t)cmd_line;
345 		} else if (bootproto >= 0x0200) {
346 			setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
347 			setup_base->screen_info.cl_offset =
348 				(uintptr_t)cmd_line - (uintptr_t)setup_base;
349 
350 			hdr->setup_move_size = 0x9100;
351 		}
352 
353 		/* build command line at COMMAND_LINE_OFFSET */
354 		if (cmdline_force)
355 			strcpy(cmd_line, (char *)cmdline_force);
356 		else
357 			build_command_line(cmd_line, auto_boot);
358 		ret = bootm_process_cmdline(cmd_line, max_size, BOOTM_CL_ALL);
359 		if (ret) {
360 			printf("Cmdline setup failed (max_size=%x, bootproto=%x, err=%d)\n",
361 			       max_size, bootproto, ret);
362 			return ret;
363 		}
364 		printf("Kernel command line: \"");
365 		puts(cmd_line);
366 		printf("\"\n");
367 	}
368 
369 	if (IS_ENABLED(CONFIG_INTEL_MID) && bootproto >= 0x0207)
370 		hdr->hardware_subarch = X86_SUBARCH_INTEL_MID;
371 
372 	if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
373 		setup_base->acpi_rsdp_addr = acpi_get_rsdp_addr();
374 
375 	log_debug("Setup devicetree\n");
376 	setup_device_tree(hdr, (const void *)env_get_hex("fdtaddr", 0));
377 	setup_video(&setup_base->screen_info);
378 
379 	if (IS_ENABLED(CONFIG_EFI_STUB))
380 		setup_efi_info(&setup_base->efi_info);
381 
382 	return 0;
383 }
384 
do_zboot_start(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])385 static int do_zboot_start(struct cmd_tbl *cmdtp, int flag, int argc,
386 			  char *const argv[])
387 {
388 	const char *s;
389 
390 	memset(&state, '\0', sizeof(state));
391 	if (argc >= 2) {
392 		/* argv[1] holds the address of the bzImage */
393 		s = argv[1];
394 	} else {
395 		s = env_get("fileaddr");
396 	}
397 
398 	if (s)
399 		state.bzimage_addr = simple_strtoul(s, NULL, 16);
400 
401 	if (argc >= 3) {
402 		/* argv[2] holds the size of the bzImage */
403 		state.bzimage_size = simple_strtoul(argv[2], NULL, 16);
404 	}
405 
406 	if (argc >= 4)
407 		state.initrd_addr = simple_strtoul(argv[3], NULL, 16);
408 	if (argc >= 5)
409 		state.initrd_size = simple_strtoul(argv[4], NULL, 16);
410 	if (argc >= 6) {
411 		/*
412 		 * When the base_ptr is passed in, we assume that the image is
413 		 * already loaded at the address given by argv[1] and therefore
414 		 * the original bzImage is somewhere else, or not accessible.
415 		 * In any case, we don't need access to the bzImage since all
416 		 * the processing is assumed to be done.
417 		 *
418 		 * So set the base_ptr to the given address, use this arg as the
419 		 * load address and set bzimage_addr to 0 so we know that it
420 		 * cannot be proceesed (or processed again).
421 		 */
422 		state.base_ptr = (void *)simple_strtoul(argv[5], NULL, 16);
423 		state.load_address = state.bzimage_addr;
424 		state.bzimage_addr = 0;
425 	}
426 	if (argc >= 7)
427 		state.cmdline = env_get(argv[6]);
428 
429 	return 0;
430 }
431 
do_zboot_load(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])432 static int do_zboot_load(struct cmd_tbl *cmdtp, int flag, int argc,
433 			 char *const argv[])
434 {
435 	struct boot_params *base_ptr;
436 
437 	if (state.base_ptr) {
438 		struct boot_params *from = (struct boot_params *)state.base_ptr;
439 
440 		base_ptr = (struct boot_params *)DEFAULT_SETUP_BASE;
441 		log_debug("Building boot_params at 0x%8.8lx\n",
442 			  (ulong)base_ptr);
443 		memset(base_ptr, '\0', sizeof(*base_ptr));
444 		base_ptr->hdr = from->hdr;
445 	} else {
446 		base_ptr = load_zimage((void *)state.bzimage_addr, state.bzimage_size,
447 				       &state.load_address);
448 		if (!base_ptr) {
449 			puts("## Kernel loading failed ...\n");
450 			return CMD_RET_FAILURE;
451 		}
452 	}
453 	state.base_ptr = base_ptr;
454 	if (env_set_hex("zbootbase", (ulong)base_ptr) ||
455 	    env_set_hex("zbootaddr", state.load_address))
456 		return CMD_RET_FAILURE;
457 
458 	return 0;
459 }
460 
do_zboot_setup(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])461 static int do_zboot_setup(struct cmd_tbl *cmdtp, int flag, int argc,
462 			  char *const argv[])
463 {
464 	struct boot_params *base_ptr = state.base_ptr;
465 	int ret;
466 
467 	if (!base_ptr) {
468 		printf("base is not set: use 'zboot load' first\n");
469 		return CMD_RET_FAILURE;
470 	}
471 	ret = setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
472 			   0, state.initrd_addr, state.initrd_size,
473 			   (ulong)state.cmdline);
474 	if (ret) {
475 		puts("Setting up boot parameters failed ...\n");
476 		return CMD_RET_FAILURE;
477 	}
478 
479 	return 0;
480 }
481 
do_zboot_info(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])482 static int do_zboot_info(struct cmd_tbl *cmdtp, int flag, int argc,
483 			 char *const argv[])
484 {
485 	printf("Kernel loaded at %08lx, setup_base=%p\n",
486 	       state.load_address, state.base_ptr);
487 
488 	return 0;
489 }
490 
do_zboot_go(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])491 static int do_zboot_go(struct cmd_tbl *cmdtp, int flag, int argc,
492 		       char *const argv[])
493 {
494 	int ret;
495 
496 	disable_interrupts();
497 
498 	/* we assume that the kernel is in place */
499 	ret = boot_linux_kernel((ulong)state.base_ptr, state.load_address,
500 				false);
501 	printf("Kernel returned! (err=%d)\n", ret);
502 
503 	return CMD_RET_FAILURE;
504 }
505 
print_num(const char * name,ulong value)506 static void print_num(const char *name, ulong value)
507 {
508 	printf("%-20s: %lx\n", name, value);
509 }
510 
print_num64(const char * name,u64 value)511 static void print_num64(const char *name, u64 value)
512 {
513 	printf("%-20s: %llx\n", name, value);
514 }
515 
516 static const char *const e820_type_name[E820_COUNT] = {
517 	[E820_RAM] = "RAM",
518 	[E820_RESERVED] = "Reserved",
519 	[E820_ACPI] = "ACPI",
520 	[E820_NVS] = "ACPI NVS",
521 	[E820_UNUSABLE] = "Unusable",
522 };
523 
524 static const char *const bootloader_id[] = {
525 	"LILO",
526 	"Loadlin",
527 	"bootsect-loader",
528 	"Syslinux",
529 	"Etherboot/gPXE/iPXE",
530 	"ELILO",
531 	"undefined",
532 	"GRUB",
533 	"U-Boot",
534 	"Xen",
535 	"Gujin",
536 	"Qemu",
537 	"Arcturus Networks uCbootloader",
538 	"kexec-tools",
539 	"Extended",
540 	"Special",
541 	"Reserved",
542 	"Minimal Linux Bootloader",
543 	"OVMF UEFI virtualization stack",
544 };
545 
546 struct flag_info {
547 	uint bit;
548 	const char *name;
549 };
550 
551 static struct flag_info load_flags[] = {
552 	{ LOADED_HIGH, "loaded-high" },
553 	{ QUIET_FLAG, "quiet" },
554 	{ KEEP_SEGMENTS, "keep-segments" },
555 	{ CAN_USE_HEAP, "can-use-heap" },
556 };
557 
558 static struct flag_info xload_flags[] = {
559 	{ XLF_KERNEL_64, "64-bit-entry" },
560 	{ XLF_CAN_BE_LOADED_ABOVE_4G, "can-load-above-4gb" },
561 	{ XLF_EFI_HANDOVER_32, "32-efi-handoff" },
562 	{ XLF_EFI_HANDOVER_64, "64-efi-handoff" },
563 	{ XLF_EFI_KEXEC, "kexec-efi-runtime" },
564 };
565 
print_flags(struct flag_info * flags,int count,uint value)566 static void print_flags(struct flag_info *flags, int count, uint value)
567 {
568 	int i;
569 
570 	printf("%-20s:", "");
571 	for (i = 0; i < count; i++) {
572 		uint mask = flags[i].bit;
573 
574 		if (value & mask)
575 			printf(" %s", flags[i].name);
576 	}
577 	printf("\n");
578 }
579 
show_loader(struct setup_header * hdr)580 static void show_loader(struct setup_header *hdr)
581 {
582 	bool version_valid = false;
583 	int type, version;
584 	const char *name;
585 
586 	type = hdr->type_of_loader >> 4;
587 	version = hdr->type_of_loader & 0xf;
588 	if (type == 0xe)
589 		type = 0x10 + hdr->ext_loader_type;
590 	version |= hdr->ext_loader_ver << 4;
591 	if (!hdr->type_of_loader) {
592 		name = "pre-2.00 bootloader";
593 	} else if (hdr->type_of_loader == 0xff) {
594 		name = "unknown";
595 	} else if (type < ARRAY_SIZE(bootloader_id)) {
596 		name = bootloader_id[type];
597 		version_valid = true;
598 	} else {
599 		name = "undefined";
600 	}
601 	printf("%20s  %s", "", name);
602 	if (version_valid)
603 		printf(", version %x", version);
604 	printf("\n");
605 }
606 
zimage_dump(struct boot_params * base_ptr)607 void zimage_dump(struct boot_params *base_ptr)
608 {
609 	struct setup_header *hdr;
610 	const char *version;
611 	int i;
612 
613 	printf("Setup located at %p:\n\n", base_ptr);
614 	print_num64("ACPI RSDP addr", base_ptr->acpi_rsdp_addr);
615 
616 	printf("E820: %d entries\n", base_ptr->e820_entries);
617 	if (base_ptr->e820_entries) {
618 		printf("%18s  %16s  %s\n", "Addr", "Size", "Type");
619 		for (i = 0; i < base_ptr->e820_entries; i++) {
620 			struct e820_entry *entry = &base_ptr->e820_map[i];
621 
622 			printf("%12llx  %10llx  %s\n", entry->addr, entry->size,
623 			       entry->type < E820_COUNT ?
624 			       e820_type_name[entry->type] :
625 			       simple_itoa(entry->type));
626 		}
627 	}
628 
629 	hdr = &base_ptr->hdr;
630 	print_num("Setup sectors", hdr->setup_sects);
631 	print_num("Root flags", hdr->root_flags);
632 	print_num("Sys size", hdr->syssize);
633 	print_num("RAM size", hdr->ram_size);
634 	print_num("Video mode", hdr->vid_mode);
635 	print_num("Root dev", hdr->root_dev);
636 	print_num("Boot flag", hdr->boot_flag);
637 	print_num("Jump", hdr->jump);
638 	print_num("Header", hdr->header);
639 	if (hdr->header == KERNEL_V2_MAGIC)
640 		printf("%-20s  %s\n", "", "Kernel V2");
641 	else
642 		printf("%-20s  %s\n", "", "Ancient kernel, using version 100");
643 	print_num("Version", hdr->version);
644 	print_num("Real mode switch", hdr->realmode_swtch);
645 	print_num("Start sys", hdr->start_sys);
646 	print_num("Kernel version", hdr->kernel_version);
647 	version = get_kernel_version(base_ptr, (void *)state.bzimage_addr);
648 	if (version)
649 		printf("   @%p: %s\n", version, version);
650 	print_num("Type of loader", hdr->type_of_loader);
651 	show_loader(hdr);
652 	print_num("Load flags", hdr->loadflags);
653 	print_flags(load_flags, ARRAY_SIZE(load_flags), hdr->loadflags);
654 	print_num("Setup move size", hdr->setup_move_size);
655 	print_num("Code32 start", hdr->code32_start);
656 	print_num("Ramdisk image", hdr->ramdisk_image);
657 	print_num("Ramdisk size", hdr->ramdisk_size);
658 	print_num("Bootsect kludge", hdr->bootsect_kludge);
659 	print_num("Heap end ptr", hdr->heap_end_ptr);
660 	print_num("Ext loader ver", hdr->ext_loader_ver);
661 	print_num("Ext loader type", hdr->ext_loader_type);
662 	print_num("Command line ptr", hdr->cmd_line_ptr);
663 	if (hdr->cmd_line_ptr) {
664 		printf("   ");
665 		/* Use puts() to avoid limits from CONFIG_SYS_PBSIZE */
666 		puts((char *)(ulong)hdr->cmd_line_ptr);
667 		printf("\n");
668 	}
669 	print_num("Initrd addr max", hdr->initrd_addr_max);
670 	print_num("Kernel alignment", hdr->kernel_alignment);
671 	print_num("Relocatable kernel", hdr->relocatable_kernel);
672 	print_num("Min alignment", hdr->min_alignment);
673 	if (hdr->min_alignment)
674 		printf("%-20s: %x\n", "", 1 << hdr->min_alignment);
675 	print_num("Xload flags", hdr->xloadflags);
676 	print_flags(xload_flags, ARRAY_SIZE(xload_flags), hdr->xloadflags);
677 	print_num("Cmdline size", hdr->cmdline_size);
678 	print_num("Hardware subarch", hdr->hardware_subarch);
679 	print_num64("HW subarch data", hdr->hardware_subarch_data);
680 	print_num("Payload offset", hdr->payload_offset);
681 	print_num("Payload length", hdr->payload_length);
682 	print_num64("Setup data", hdr->setup_data);
683 	print_num64("Pref address", hdr->pref_address);
684 	print_num("Init size", hdr->init_size);
685 	print_num("Handover offset", hdr->handover_offset);
686 	if (get_boot_protocol(hdr, false) >= 0x215)
687 		print_num("Kernel info offset", hdr->kernel_info_offset);
688 }
689 
do_zboot_dump(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[])690 static int do_zboot_dump(struct cmd_tbl *cmdtp, int flag, int argc,
691 			 char *const argv[])
692 {
693 	struct boot_params *base_ptr = state.base_ptr;
694 
695 	if (argc > 1)
696 		base_ptr = (void *)simple_strtoul(argv[1], NULL, 16);
697 	if (!base_ptr) {
698 		printf("No zboot setup_base\n");
699 		return CMD_RET_FAILURE;
700 	}
701 	zimage_dump(base_ptr);
702 
703 	return 0;
704 }
705 
706 /* Note: This defines the complete_zboot() function */
707 U_BOOT_SUBCMDS(zboot,
708 	U_BOOT_CMD_MKENT(start, 8, 1, do_zboot_start, "", ""),
709 	U_BOOT_CMD_MKENT(load, 1, 1, do_zboot_load, "", ""),
710 	U_BOOT_CMD_MKENT(setup, 1, 1, do_zboot_setup, "", ""),
711 	U_BOOT_CMD_MKENT(info, 1, 1, do_zboot_info, "", ""),
712 	U_BOOT_CMD_MKENT(go, 1, 1, do_zboot_go, "", ""),
713 	U_BOOT_CMD_MKENT(dump, 2, 1, do_zboot_dump, "", ""),
714 )
715 
do_zboot_states(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[],int state_mask)716 int do_zboot_states(struct cmd_tbl *cmdtp, int flag, int argc,
717 		    char *const argv[], int state_mask)
718 {
719 	int i;
720 
721 	for (i = 0; i < ZBOOT_STATE_COUNT; i++) {
722 		struct cmd_tbl *cmd = &zboot_subcmds[i];
723 		int mask = 1 << i;
724 		int ret;
725 
726 		if (mask & state_mask) {
727 			ret = cmd->cmd(cmd, flag, argc, argv);
728 			if (ret)
729 				return ret;
730 		}
731 	}
732 
733 	return 0;
734 }
735 
do_zboot_parent(struct cmd_tbl * cmdtp,int flag,int argc,char * const argv[],int * repeatable)736 int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc,
737 		    char *const argv[], int *repeatable)
738 {
739 	/* determine if we have a sub command */
740 	if (argc > 1) {
741 		char *endp;
742 
743 		simple_strtoul(argv[1], &endp, 16);
744 		/*
745 		 * endp pointing to nul means that argv[1] was just a valid
746 		 * number, so pass it along to the normal processing
747 		 */
748 		if (*endp)
749 			return do_zboot(cmdtp, flag, argc, argv, repeatable);
750 	}
751 
752 	do_zboot_states(cmdtp, flag, argc, argv, ZBOOT_STATE_START |
753 			ZBOOT_STATE_LOAD | ZBOOT_STATE_SETUP |
754 			ZBOOT_STATE_INFO | ZBOOT_STATE_GO);
755 
756 	return CMD_RET_FAILURE;
757 }
758 
759 U_BOOT_CMDREP_COMPLETE(
760 	zboot, 8, do_zboot_parent, "Boot bzImage",
761 	"[addr] [size] [initrd addr] [initrd size] [setup] [cmdline]\n"
762 	"      addr -        The optional starting address of the bzimage.\n"
763 	"                    If not set it defaults to the environment\n"
764 	"                    variable \"fileaddr\".\n"
765 	"      size -        The optional size of the bzimage. Defaults to\n"
766 	"                    zero.\n"
767 	"      initrd addr - The address of the initrd image to use, if any.\n"
768 	"      initrd size - The size of the initrd image to use, if any.\n"
769 	"      setup -       The address of the kernel setup region, if this\n"
770 	"                    is not at addr\n"
771 	"      cmdline -     Environment variable containing the kernel\n"
772 	"                    command line, to override U-Boot's normal\n"
773 	"                    cmdline generation\n"
774 	"\n"
775 	"Sub-commands to do part of the zboot sequence:\n"
776 	"\tstart [addr [arg ...]] - specify arguments\n"
777 	"\tload   - load OS image\n"
778 	"\tsetup  - set up table\n"
779 	"\tinfo   - show summary info\n"
780 	"\tgo     - start OS\n"
781 	"\tdump [addr]    - dump info (optional address of boot params)",
782 	complete_zboot
783 );
784