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