1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (c) 2011 The Chromium OS Authors.
5 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
9 * Linux x86 zImage and bzImage loading
11 * based on the procdure described in
12 * linux/Documentation/i386/boot.txt
15 #define LOG_CATEGORY LOGC_BOOT
24 #include <acpi/acpi_table.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>
34 #include <linux/compiler.h>
35 #include <linux/ctype.h>
36 #include <linux/libfdt.h>
41 * relative to setup_base (which is 0x90000 currently)
43 * 0x0000-0x7FFF Real mode kernel
44 * 0x8000-0x8FFF Stack and heap
45 * 0x9000-0x90FF Kernel command line
47 #define DEFAULT_SETUP_BASE 0x90000
48 #define COMMAND_LINE_OFFSET 0x9000
49 #define HEAP_END_OFFSET 0x8e00
51 #define COMMAND_LINE_SIZE 2048
54 * struct zboot_state - Current state of the boot
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
64 * @cmdline: Address of 'override' command line, or 0 to use the one in the
73 struct boot_params *base_ptr;
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),
84 /* This one doesn't execute automatically, so stop the count before 5 */
85 ZBOOT_STATE_DUMP = BIT(5),
86 ZBOOT_STATE_COUNT = 5,
89 static void build_command_line(char *command_line, int auto_boot)
91 char *env_command_line;
93 command_line[0] = '\0';
95 env_command_line = env_get("bootargs");
97 /* set console= argument if we use a serial console */
98 if (!strstr(env_command_line, "console=")) {
99 if (!strcmp(env_get("stdout"), "serial")) {
101 /* We seem to use serial console */
102 sprintf(command_line, "console=ttyS0,%s ",
103 env_get("baudrate"));
108 strcat(command_line, "auto ");
110 if (env_command_line)
111 strcat(command_line, env_command_line);
113 printf("Kernel command line: \"%s\"\n", command_line);
116 static int kernel_magic_ok(struct setup_header *hdr)
118 if (KERNEL_MAGIC != hdr->boot_flag) {
119 printf("Error: Invalid Boot Flag "
120 "(found 0x%04x, expected 0x%04x)\n",
121 hdr->boot_flag, KERNEL_MAGIC);
124 printf("Valid Boot Flag\n");
129 static int get_boot_protocol(struct setup_header *hdr, bool verbose)
131 if (hdr->header == KERNEL_V2_MAGIC) {
133 printf("Magic signature found\n");
136 /* Very old kernel */
138 printf("Magic signature not found\n");
143 static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob)
145 int bootproto = get_boot_protocol(hdr, false);
146 struct setup_data *sd;
149 if (bootproto < 0x0209)
155 size = fdt_totalsize(fdt_blob);
159 size += sizeof(struct setup_data);
160 sd = (struct setup_data *)malloc(size);
162 printf("Not enough memory for DTB setup data\n");
166 sd->next = hdr->setup_data;
167 sd->type = SETUP_DTB;
168 sd->len = fdt_totalsize(fdt_blob);
169 memcpy(sd->data, fdt_blob, sd->len);
170 hdr->setup_data = (unsigned long)sd;
175 static const char *get_kernel_version(struct boot_params *params,
178 struct setup_header *hdr = ¶ms->hdr;
182 bootproto = get_boot_protocol(hdr, false);
183 if (bootproto < 0x0200 || hdr->setup_sects < 15)
186 /* sanity-check the kernel version in case it is missing */
187 for (s = kernel_base + hdr->kernel_version + 0x200, end = s + 0x100; *s;
193 return kernel_base + hdr->kernel_version + 0x200;
196 struct boot_params *load_zimage(char *image, unsigned long kernel_size,
197 ulong *load_addressp)
199 struct boot_params *setup_base;
205 struct boot_params *params = (struct boot_params *)image;
206 struct setup_header *hdr = ¶ms->hdr;
208 /* base address for real-mode segment */
209 setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
211 if (!kernel_magic_ok(hdr))
214 /* determine size of setup */
215 if (0 == hdr->setup_sects) {
216 log_warning("Setup Sectors = 0 (defaulting to 4)\n");
217 setup_size = 5 * 512;
219 setup_size = (hdr->setup_sects + 1) * 512;
222 log_debug("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
224 if (setup_size > SETUP_MAX_SIZE)
225 printf("Error: Setup is too large (%d bytes)\n", setup_size);
227 /* determine boot protocol version */
228 bootproto = get_boot_protocol(hdr, true);
230 log_debug("Using boot protocol version %x.%02x\n",
231 (bootproto & 0xff00) >> 8, bootproto & 0xff);
233 version = get_kernel_version(params, image);
235 printf("Linux kernel version %s\n", version);
237 printf("Setup Sectors < 15 - Cannot print kernel version\n");
239 /* Determine image type */
240 big_image = (bootproto >= 0x0200) &&
241 (hdr->loadflags & BIG_KERNEL_FLAG);
243 /* Determine load address */
245 *load_addressp = BZIMAGE_LOAD_ADDR;
247 *load_addressp = ZIMAGE_LOAD_ADDR;
249 printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
250 memset(setup_base, 0, sizeof(*setup_base));
251 setup_base->hdr = params->hdr;
253 if (bootproto >= 0x0204)
254 kernel_size = hdr->syssize * 16;
256 kernel_size -= setup_size;
258 if (bootproto == 0x0100) {
260 * A very old kernel MUST have its real-mode code
263 if ((ulong)setup_base != 0x90000) {
264 /* Copy the real-mode kernel */
265 memmove((void *)0x90000, setup_base, setup_size);
267 /* Copy the command line */
268 memmove((void *)0x99000,
269 (u8 *)setup_base + COMMAND_LINE_OFFSET,
273 setup_base = (struct boot_params *)0x90000;
276 /* It is recommended to clear memory up to the 32K mark */
277 memset((u8 *)0x90000 + setup_size, 0,
278 SETUP_MAX_SIZE - setup_size);
282 if (kernel_size > BZIMAGE_MAX_SIZE) {
283 printf("Error: bzImage kernel too big! "
284 "(size: %ld, max: %d)\n",
285 kernel_size, BZIMAGE_MAX_SIZE);
288 } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
289 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
290 kernel_size, ZIMAGE_MAX_SIZE);
294 printf("Loading %s at address %lx (%ld bytes)\n",
295 big_image ? "bzImage" : "zImage", *load_addressp, kernel_size);
297 memmove((void *)*load_addressp, image + setup_size, kernel_size);
302 int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
303 ulong initrd_addr, ulong initrd_size, ulong cmdline_force)
305 struct setup_header *hdr = &setup_base->hdr;
306 int bootproto = get_boot_protocol(hdr, false);
308 log_debug("Setup E820 entries\n");
309 setup_base->e820_entries = install_e820_map(
310 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
312 if (bootproto == 0x0100) {
313 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
314 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
316 if (bootproto >= 0x0200) {
317 hdr->type_of_loader = 0x80; /* U-Boot version 0 */
319 printf("Initial RAM disk at linear address "
320 "0x%08lx, size %ld bytes\n",
321 initrd_addr, initrd_size);
323 hdr->ramdisk_image = initrd_addr;
324 hdr->ramdisk_size = initrd_size;
328 if (bootproto >= 0x0201) {
329 hdr->heap_end_ptr = HEAP_END_OFFSET;
330 hdr->loadflags |= HEAP_FLAG;
337 log_debug("Setup cmdline\n");
338 if (bootproto >= 0x0206)
339 max_size = hdr->cmdline_size;
340 if (bootproto >= 0x0202) {
341 hdr->cmd_line_ptr = (uintptr_t)cmd_line;
342 } else if (bootproto >= 0x0200) {
343 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
344 setup_base->screen_info.cl_offset =
345 (uintptr_t)cmd_line - (uintptr_t)setup_base;
347 hdr->setup_move_size = 0x9100;
350 /* build command line at COMMAND_LINE_OFFSET */
352 strcpy(cmd_line, (char *)cmdline_force);
354 build_command_line(cmd_line, auto_boot);
355 ret = bootm_process_cmdline(cmd_line, max_size, BOOTM_CL_ALL);
357 printf("Cmdline setup failed (err=%d)\n", ret);
360 printf("Kernel command line: \"");
365 if (IS_ENABLED(CONFIG_INTEL_MID) && bootproto >= 0x0207)
366 hdr->hardware_subarch = X86_SUBARCH_INTEL_MID;
368 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
369 setup_base->acpi_rsdp_addr = acpi_get_rsdp_addr();
371 log_debug("Setup devicetree\n");
372 setup_device_tree(hdr, (const void *)env_get_hex("fdtaddr", 0));
373 setup_video(&setup_base->screen_info);
375 if (IS_ENABLED(CONFIG_EFI_STUB))
376 setup_efi_info(&setup_base->efi_info);
381 static int do_zboot_start(struct cmd_tbl *cmdtp, int flag, int argc,
386 memset(&state, '\0', sizeof(state));
388 /* argv[1] holds the address of the bzImage */
391 s = env_get("fileaddr");
395 state.bzimage_addr = simple_strtoul(s, NULL, 16);
398 /* argv[2] holds the size of the bzImage */
399 state.bzimage_size = simple_strtoul(argv[2], NULL, 16);
403 state.initrd_addr = simple_strtoul(argv[3], NULL, 16);
405 state.initrd_size = simple_strtoul(argv[4], NULL, 16);
408 * When the base_ptr is passed in, we assume that the image is
409 * already loaded at the address given by argv[1] and therefore
410 * the original bzImage is somewhere else, or not accessible.
411 * In any case, we don't need access to the bzImage since all
412 * the processing is assumed to be done.
414 * So set the base_ptr to the given address, use this arg as the
415 * load address and set bzimage_addr to 0 so we know that it
416 * cannot be proceesed (or processed again).
418 state.base_ptr = (void *)simple_strtoul(argv[5], NULL, 16);
419 state.load_address = state.bzimage_addr;
420 state.bzimage_addr = 0;
423 state.cmdline = simple_strtoul(argv[6], NULL, 16);
428 static int do_zboot_load(struct cmd_tbl *cmdtp, int flag, int argc,
431 struct boot_params *base_ptr;
433 if (state.base_ptr) {
434 struct boot_params *from = (struct boot_params *)state.base_ptr;
436 base_ptr = (struct boot_params *)DEFAULT_SETUP_BASE;
437 log_debug("Building boot_params at 0x%8.8lx\n",
439 memset(base_ptr, '\0', sizeof(*base_ptr));
440 base_ptr->hdr = from->hdr;
442 base_ptr = load_zimage((void *)state.bzimage_addr, state.bzimage_size,
443 &state.load_address);
445 puts("## Kernel loading failed ...\n");
446 return CMD_RET_FAILURE;
449 state.base_ptr = base_ptr;
450 if (env_set_hex("zbootbase", (ulong)base_ptr) ||
451 env_set_hex("zbootaddr", state.load_address))
452 return CMD_RET_FAILURE;
457 static int do_zboot_setup(struct cmd_tbl *cmdtp, int flag, int argc,
460 struct boot_params *base_ptr = state.base_ptr;
464 printf("base is not set: use 'zboot load' first\n");
465 return CMD_RET_FAILURE;
467 ret = setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
468 0, state.initrd_addr, state.initrd_size,
471 puts("Setting up boot parameters failed ...\n");
472 return CMD_RET_FAILURE;
478 static int do_zboot_info(struct cmd_tbl *cmdtp, int flag, int argc,
481 printf("Kernel loaded at %08lx, setup_base=%p\n",
482 state.load_address, state.base_ptr);
487 static int do_zboot_go(struct cmd_tbl *cmdtp, int flag, int argc,
492 disable_interrupts();
494 /* we assume that the kernel is in place */
495 ret = boot_linux_kernel((ulong)state.base_ptr, state.load_address,
497 printf("Kernel returned! (err=%d)\n", ret);
499 return CMD_RET_FAILURE;
502 static void print_num(const char *name, ulong value)
504 printf("%-20s: %lx\n", name, value);
507 static void print_num64(const char *name, u64 value)
509 printf("%-20s: %llx\n", name, value);
512 static const char *const e820_type_name[E820_COUNT] = {
514 [E820_RESERVED] = "Reserved",
515 [E820_ACPI] = "ACPI",
516 [E820_NVS] = "ACPI NVS",
517 [E820_UNUSABLE] = "Unusable",
520 static const char *const bootloader_id[] = {
525 "Etherboot/gPXE/iPXE",
533 "Arcturus Networks uCbootloader",
538 "Minimal Linux Bootloader",
539 "OVMF UEFI virtualization stack",
547 static struct flag_info load_flags[] = {
548 { LOADED_HIGH, "loaded-high" },
549 { QUIET_FLAG, "quiet" },
550 { KEEP_SEGMENTS, "keep-segments" },
551 { CAN_USE_HEAP, "can-use-heap" },
554 static struct flag_info xload_flags[] = {
555 { XLF_KERNEL_64, "64-bit-entry" },
556 { XLF_CAN_BE_LOADED_ABOVE_4G, "can-load-above-4gb" },
557 { XLF_EFI_HANDOVER_32, "32-efi-handoff" },
558 { XLF_EFI_HANDOVER_64, "64-efi-handoff" },
559 { XLF_EFI_KEXEC, "kexec-efi-runtime" },
562 static void print_flags(struct flag_info *flags, int count, uint value)
566 printf("%-20s:", "");
567 for (i = 0; i < count; i++) {
568 uint mask = flags[i].bit;
571 printf(" %s", flags[i].name);
576 static void show_loader(struct setup_header *hdr)
578 bool version_valid = false;
582 type = hdr->type_of_loader >> 4;
583 version = hdr->type_of_loader & 0xf;
585 type = 0x10 + hdr->ext_loader_type;
586 version |= hdr->ext_loader_ver << 4;
587 if (!hdr->type_of_loader) {
588 name = "pre-2.00 bootloader";
589 } else if (hdr->type_of_loader == 0xff) {
591 } else if (type < ARRAY_SIZE(bootloader_id)) {
592 name = bootloader_id[type];
593 version_valid = true;
597 printf("%20s %s", "", name);
599 printf(", version %x", version);
603 int do_zboot_dump(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
605 struct boot_params *base_ptr = state.base_ptr;
606 struct setup_header *hdr;
611 base_ptr = (void *)simple_strtoul(argv[1], NULL, 16);
613 printf("No zboot setup_base\n");
614 return CMD_RET_FAILURE;
616 printf("Setup located at %p:\n\n", base_ptr);
617 print_num64("ACPI RSDP addr", base_ptr->acpi_rsdp_addr);
619 printf("E820: %d entries\n", base_ptr->e820_entries);
620 if (base_ptr->e820_entries) {
621 printf("%18s %16s %s\n", "Addr", "Size", "Type");
622 for (i = 0; i < base_ptr->e820_entries; i++) {
623 struct e820_entry *entry = &base_ptr->e820_map[i];
625 printf("%12llx %10llx %s\n", entry->addr, entry->size,
626 entry->type < E820_COUNT ?
627 e820_type_name[entry->type] :
628 simple_itoa(entry->type));
632 hdr = &base_ptr->hdr;
633 print_num("Setup sectors", hdr->setup_sects);
634 print_num("Root flags", hdr->root_flags);
635 print_num("Sys size", hdr->syssize);
636 print_num("RAM size", hdr->ram_size);
637 print_num("Video mode", hdr->vid_mode);
638 print_num("Root dev", hdr->root_dev);
639 print_num("Boot flag", hdr->boot_flag);
640 print_num("Jump", hdr->jump);
641 print_num("Header", hdr->header);
642 if (hdr->header == KERNEL_V2_MAGIC)
643 printf("%-20s %s\n", "", "Kernel V2");
645 printf("%-20s %s\n", "", "Ancient kernel, using version 100");
646 print_num("Version", hdr->version);
647 print_num("Real mode switch", hdr->realmode_swtch);
648 print_num("Start sys", hdr->start_sys);
649 print_num("Kernel version", hdr->kernel_version);
650 version = get_kernel_version(base_ptr, (void *)state.bzimage_addr);
652 printf(" @%p: %s\n", version, version);
653 print_num("Type of loader", hdr->type_of_loader);
655 print_num("Load flags", hdr->loadflags);
656 print_flags(load_flags, ARRAY_SIZE(load_flags), hdr->loadflags);
657 print_num("Setup move size", hdr->setup_move_size);
658 print_num("Code32 start", hdr->code32_start);
659 print_num("Ramdisk image", hdr->ramdisk_image);
660 print_num("Ramdisk size", hdr->ramdisk_size);
661 print_num("Bootsect kludge", hdr->bootsect_kludge);
662 print_num("Heap end ptr", hdr->heap_end_ptr);
663 print_num("Ext loader ver", hdr->ext_loader_ver);
664 print_num("Ext loader type", hdr->ext_loader_type);
665 print_num("Command line ptr", hdr->cmd_line_ptr);
666 if (hdr->cmd_line_ptr) {
668 /* Use puts() to avoid limits from CONFIG_SYS_PBSIZE */
669 puts((char *)(ulong)hdr->cmd_line_ptr);
672 print_num("Initrd addr max", hdr->initrd_addr_max);
673 print_num("Kernel alignment", hdr->kernel_alignment);
674 print_num("Relocatable kernel", hdr->relocatable_kernel);
675 print_num("Min alignment", hdr->min_alignment);
676 if (hdr->min_alignment)
677 printf("%-20s: %x\n", "", 1 << hdr->min_alignment);
678 print_num("Xload flags", hdr->xloadflags);
679 print_flags(xload_flags, ARRAY_SIZE(xload_flags), hdr->xloadflags);
680 print_num("Cmdline size", hdr->cmdline_size);
681 print_num("Hardware subarch", hdr->hardware_subarch);
682 print_num64("HW subarch data", hdr->hardware_subarch_data);
683 print_num("Payload offset", hdr->payload_offset);
684 print_num("Payload length", hdr->payload_length);
685 print_num64("Setup data", hdr->setup_data);
686 print_num64("Pref address", hdr->pref_address);
687 print_num("Init size", hdr->init_size);
688 print_num("Handover offset", hdr->handover_offset);
689 if (get_boot_protocol(hdr, false) >= 0x215)
690 print_num("Kernel info offset", hdr->kernel_info_offset);
695 /* Note: This defines the complete_zboot() function */
696 U_BOOT_SUBCMDS(zboot,
697 U_BOOT_CMD_MKENT(start, 8, 1, do_zboot_start, "", ""),
698 U_BOOT_CMD_MKENT(load, 1, 1, do_zboot_load, "", ""),
699 U_BOOT_CMD_MKENT(setup, 1, 1, do_zboot_setup, "", ""),
700 U_BOOT_CMD_MKENT(info, 1, 1, do_zboot_info, "", ""),
701 U_BOOT_CMD_MKENT(go, 1, 1, do_zboot_go, "", ""),
702 U_BOOT_CMD_MKENT(dump, 2, 1, do_zboot_dump, "", ""),
705 int do_zboot_states(struct cmd_tbl *cmdtp, int flag, int argc,
706 char *const argv[], int state_mask)
710 for (i = 0; i < ZBOOT_STATE_COUNT; i++) {
711 struct cmd_tbl *cmd = &zboot_subcmds[i];
715 if (mask & state_mask) {
716 ret = cmd->cmd(cmd, flag, argc, argv);
725 int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc,
726 char *const argv[], int *repeatable)
728 /* determine if we have a sub command */
732 simple_strtoul(argv[1], &endp, 16);
734 * endp pointing to nul means that argv[1] was just a valid
735 * number, so pass it along to the normal processing
738 return do_zboot(cmdtp, flag, argc, argv, repeatable);
741 do_zboot_states(cmdtp, flag, argc, argv, ZBOOT_STATE_START |
742 ZBOOT_STATE_LOAD | ZBOOT_STATE_SETUP |
743 ZBOOT_STATE_INFO | ZBOOT_STATE_GO);
745 return CMD_RET_FAILURE;
748 U_BOOT_CMDREP_COMPLETE(
749 zboot, 8, do_zboot_parent, "Boot bzImage",
750 "[addr] [size] [initrd addr] [initrd size] [setup] [cmdline]\n"
751 " addr - The optional starting address of the bzimage.\n"
752 " If not set it defaults to the environment\n"
753 " variable \"fileaddr\".\n"
754 " size - The optional size of the bzimage. Defaults to\n"
756 " initrd addr - The address of the initrd image to use, if any.\n"
757 " initrd size - The size of the initrd image to use, if any.\n"
758 " setup - The address of the kernel setup region, if this\n"
760 " cmdline - The address of the kernel command line, to\n"
761 " override U-Boot's normal cmdline generation\n"
763 "Sub-commands to do part of the zboot sequence:\n"
764 "\tstart [addr [arg ...]] - specify arguments\n"
765 "\tload - load OS image\n"
766 "\tsetup - set up table\n"
767 "\tinfo - show summary info\n"
769 "\tdump [addr] - dump info (optional address of boot params)",