blob: 1d1249fc25acc74b92452a3b878297115845c38e [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk591dda52002-11-18 00:14:45 +00002/*
Gabe Black1cf51212011-12-05 12:09:23 +00003 * Copyright (c) 2011 The Chromium OS Authors.
wdenk591dda52002-11-18 00:14:45 +00004 * (C) Copyright 2002
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +02005 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
wdenk591dda52002-11-18 00:14:45 +00006 */
7
wdenk57b2d802003-06-27 21:31:46 +00008/*
Graeme Russ45fc1d82011-04-13 19:43:26 +10009 * Linux x86 zImage and bzImage loading
wdenk57b2d802003-06-27 21:31:46 +000010 *
11 * based on the procdure described in
wdenk591dda52002-11-18 00:14:45 +000012 * linux/Documentation/i386/boot.txt
13 */
14
Simon Glass842ff442020-11-04 09:59:13 -070015#define LOG_CATEGORY LOGC_BOOT
16
wdenk591dda52002-11-18 00:14:45 +000017#include <common.h>
Simon Glass7b48ce82020-11-05 10:33:46 -070018#include <bootm.h>
Simon Glassed38aef2020-05-10 11:40:03 -060019#include <command.h>
Simon Glass83c2e492019-08-01 09:46:50 -060020#include <env.h>
Simon Glass02c51202021-03-15 18:00:23 +130021#include <init.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070022#include <irq_func.h>
Simon Glass842ff442020-11-04 09:59:13 -070023#include <log.h>
Ivan Gorinov901bb0b2018-03-26 18:06:54 -070024#include <malloc.h>
Simon Glass84305a12023-07-12 09:04:43 -060025#include <mapmem.h>
Simon Glass858fed12020-04-08 16:57:36 -060026#include <acpi/acpi_table.h>
wdenk591dda52002-11-18 00:14:45 +000027#include <asm/io.h>
28#include <asm/ptrace.h>
29#include <asm/zimage.h>
wdenk591dda52002-11-18 00:14:45 +000030#include <asm/byteorder.h>
Simon Glassaa83f2b2014-10-19 21:11:20 -060031#include <asm/bootm.h>
Graeme Russ1bab1042010-04-24 00:05:49 +100032#include <asm/bootparam.h>
Simon Glassb73d48f2021-09-24 18:30:20 -060033#include <asm/efi.h>
Simon Glass02c51202021-03-15 18:00:23 +130034#include <asm/global_data.h>
Vadim Bendebury8cd22c82012-10-23 18:04:34 +000035#ifdef CONFIG_SYS_COREBOOT
36#include <asm/arch/timestamp.h>
37#endif
Stefan Reinauer2e5225e2012-10-23 18:04:37 +000038#include <linux/compiler.h>
Simon Glass8ee28542020-11-04 09:59:14 -070039#include <linux/ctype.h>
Ivan Gorinov901bb0b2018-03-26 18:06:54 -070040#include <linux/libfdt.h>
wdenk591dda52002-11-18 00:14:45 +000041
Simon Glass02c51202021-03-15 18:00:23 +130042DECLARE_GLOBAL_DATA_PTR;
43
wdenk591dda52002-11-18 00:14:45 +000044/*
45 * Memory lay-out:
wdenk57b2d802003-06-27 21:31:46 +000046 *
wdenk591dda52002-11-18 00:14:45 +000047 * relative to setup_base (which is 0x90000 currently)
wdenk57b2d802003-06-27 21:31:46 +000048 *
49 * 0x0000-0x7FFF Real mode kernel
wdenk591dda52002-11-18 00:14:45 +000050 * 0x8000-0x8FFF Stack and heap
51 * 0x9000-0x90FF Kernel command line
52 */
Graeme Russ883c6032011-11-08 02:33:15 +000053#define DEFAULT_SETUP_BASE 0x90000
54#define COMMAND_LINE_OFFSET 0x9000
55#define HEAP_END_OFFSET 0x8e00
wdenk591dda52002-11-18 00:14:45 +000056
Graeme Russ883c6032011-11-08 02:33:15 +000057#define COMMAND_LINE_SIZE 2048
wdenk591dda52002-11-18 00:14:45 +000058
Simon Glass2bf90bd2023-12-03 17:29:27 -070059/* Current state of the boot */
Simon Glassbf4af112023-12-03 17:29:26 -070060struct zboot_state state;
Simon Glassd9b3c982020-09-05 14:50:43 -060061
wdenk591dda52002-11-18 00:14:45 +000062static void build_command_line(char *command_line, int auto_boot)
63{
64 char *env_command_line;
wdenk57b2d802003-06-27 21:31:46 +000065
wdenk591dda52002-11-18 00:14:45 +000066 command_line[0] = '\0';
wdenk57b2d802003-06-27 21:31:46 +000067
Simon Glass64b723f2017-08-03 12:22:12 -060068 env_command_line = env_get("bootargs");
wdenk57b2d802003-06-27 21:31:46 +000069
wdenk591dda52002-11-18 00:14:45 +000070 /* set console= argument if we use a serial console */
Graeme Russ883c6032011-11-08 02:33:15 +000071 if (!strstr(env_command_line, "console=")) {
Simon Glass64b723f2017-08-03 12:22:12 -060072 if (!strcmp(env_get("stdout"), "serial")) {
wdenk57b2d802003-06-27 21:31:46 +000073
wdenk591dda52002-11-18 00:14:45 +000074 /* We seem to use serial console */
wdenk57b2d802003-06-27 21:31:46 +000075 sprintf(command_line, "console=ttyS0,%s ",
Simon Glass64b723f2017-08-03 12:22:12 -060076 env_get("baudrate"));
wdenk591dda52002-11-18 00:14:45 +000077 }
78 }
wdenk57b2d802003-06-27 21:31:46 +000079
Graeme Russ883c6032011-11-08 02:33:15 +000080 if (auto_boot)
wdenk591dda52002-11-18 00:14:45 +000081 strcat(command_line, "auto ");
wdenk57b2d802003-06-27 21:31:46 +000082
Graeme Russ883c6032011-11-08 02:33:15 +000083 if (env_command_line)
wdenk591dda52002-11-18 00:14:45 +000084 strcat(command_line, env_command_line);
Simon Glasseeb01222021-01-24 10:06:09 -070085#ifdef DEBUG
86 printf("Kernel command line:");
87 puts(command_line);
88 printf("\n");
89#endif
wdenk591dda52002-11-18 00:14:45 +000090}
91
Gabe Black540c2622011-12-05 12:09:26 +000092static int kernel_magic_ok(struct setup_header *hdr)
wdenk591dda52002-11-18 00:14:45 +000093{
Graeme Russ1bab1042010-04-24 00:05:49 +100094 if (KERNEL_MAGIC != hdr->boot_flag) {
Gabe Black1cf51212011-12-05 12:09:23 +000095 printf("Error: Invalid Boot Flag "
96 "(found 0x%04x, expected 0x%04x)\n",
97 hdr->boot_flag, KERNEL_MAGIC);
wdenk591dda52002-11-18 00:14:45 +000098 return 0;
Graeme Russ1bab1042010-04-24 00:05:49 +100099 } else {
100 printf("Valid Boot Flag\n");
Gabe Black540c2622011-12-05 12:09:26 +0000101 return 1;
wdenk591dda52002-11-18 00:14:45 +0000102 }
Gabe Black540c2622011-12-05 12:09:26 +0000103}
wdenk57b2d802003-06-27 21:31:46 +0000104
Simon Glassdf30d412020-09-05 14:50:40 -0600105static int get_boot_protocol(struct setup_header *hdr, bool verbose)
Gabe Black540c2622011-12-05 12:09:26 +0000106{
107 if (hdr->header == KERNEL_V2_MAGIC) {
Simon Glassdf30d412020-09-05 14:50:40 -0600108 if (verbose)
109 printf("Magic signature found\n");
Gabe Black540c2622011-12-05 12:09:26 +0000110 return hdr->version;
wdenk591dda52002-11-18 00:14:45 +0000111 } else {
112 /* Very old kernel */
Simon Glassdf30d412020-09-05 14:50:40 -0600113 if (verbose)
114 printf("Magic signature not found\n");
Gabe Black540c2622011-12-05 12:09:26 +0000115 return 0x0100;
wdenk591dda52002-11-18 00:14:45 +0000116 }
Gabe Black540c2622011-12-05 12:09:26 +0000117}
118
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700119static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob)
120{
Simon Glassdf30d412020-09-05 14:50:40 -0600121 int bootproto = get_boot_protocol(hdr, false);
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700122 struct setup_data *sd;
123 int size;
124
125 if (bootproto < 0x0209)
126 return -ENOTSUPP;
127
128 if (!fdt_blob)
129 return 0;
130
131 size = fdt_totalsize(fdt_blob);
132 if (size < 0)
133 return -EINVAL;
134
135 size += sizeof(struct setup_data);
136 sd = (struct setup_data *)malloc(size);
137 if (!sd) {
138 printf("Not enough memory for DTB setup data\n");
139 return -ENOMEM;
140 }
141
142 sd->next = hdr->setup_data;
143 sd->type = SETUP_DTB;
144 sd->len = fdt_totalsize(fdt_blob);
145 memcpy(sd->data, fdt_blob, sd->len);
146 hdr->setup_data = (unsigned long)sd;
147
148 return 0;
149}
150
Simon Glass531efde2023-07-12 09:04:44 -0600151const char *zimage_get_kernel_version(struct boot_params *params,
Simon Glassdf30d412020-09-05 14:50:40 -0600152 void *kernel_base)
153{
154 struct setup_header *hdr = &params->hdr;
155 int bootproto;
Simon Glass8ee28542020-11-04 09:59:14 -0700156 const char *s, *end;
Simon Glassdf30d412020-09-05 14:50:40 -0600157
158 bootproto = get_boot_protocol(hdr, false);
Simon Glass531efde2023-07-12 09:04:44 -0600159 log_debug("bootproto %x, hdr->setup_sects %x\n", bootproto,
160 hdr->setup_sects);
Simon Glassdf30d412020-09-05 14:50:40 -0600161 if (bootproto < 0x0200 || hdr->setup_sects < 15)
162 return NULL;
163
Simon Glass8ee28542020-11-04 09:59:14 -0700164 /* sanity-check the kernel version in case it is missing */
Simon Glass531efde2023-07-12 09:04:44 -0600165 log_debug("hdr->kernel_version %x, str at %p\n", hdr->kernel_version,
166 kernel_base + hdr->kernel_version + 0x200);
Simon Glass8ee28542020-11-04 09:59:14 -0700167 for (s = kernel_base + hdr->kernel_version + 0x200, end = s + 0x100; *s;
168 s++) {
169 if (!isprint(*s))
170 return NULL;
171 }
172
Simon Glassdf30d412020-09-05 14:50:40 -0600173 return kernel_base + hdr->kernel_version + 0x200;
174}
175
Gabe Black540c2622011-12-05 12:09:26 +0000176struct boot_params *load_zimage(char *image, unsigned long kernel_size,
Simon Glassd98a5bd2014-10-10 08:21:56 -0600177 ulong *load_addressp)
Gabe Black540c2622011-12-05 12:09:26 +0000178{
179 struct boot_params *setup_base;
Simon Glassdf30d412020-09-05 14:50:40 -0600180 const char *version;
Gabe Black540c2622011-12-05 12:09:26 +0000181 int setup_size;
182 int bootproto;
183 int big_image;
184
185 struct boot_params *params = (struct boot_params *)image;
186 struct setup_header *hdr = &params->hdr;
187
188 /* base address for real-mode segment */
189 setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
190
191 if (!kernel_magic_ok(hdr))
192 return 0;
wdenk57b2d802003-06-27 21:31:46 +0000193
wdenk591dda52002-11-18 00:14:45 +0000194 /* determine size of setup */
Graeme Russ1bab1042010-04-24 00:05:49 +1000195 if (0 == hdr->setup_sects) {
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700196 log_warning("Setup Sectors = 0 (defaulting to 4)\n");
wdenk591dda52002-11-18 00:14:45 +0000197 setup_size = 5 * 512;
198 } else {
Graeme Russ1bab1042010-04-24 00:05:49 +1000199 setup_size = (hdr->setup_sects + 1) * 512;
wdenk591dda52002-11-18 00:14:45 +0000200 }
wdenk57b2d802003-06-27 21:31:46 +0000201
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700202 log_debug("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
Graeme Russ1bab1042010-04-24 00:05:49 +1000203
Graeme Russ883c6032011-11-08 02:33:15 +0000204 if (setup_size > SETUP_MAX_SIZE)
wdenk591dda52002-11-18 00:14:45 +0000205 printf("Error: Setup is too large (%d bytes)\n", setup_size);
wdenk57b2d802003-06-27 21:31:46 +0000206
Gabe Black540c2622011-12-05 12:09:26 +0000207 /* determine boot protocol version */
Simon Glassdf30d412020-09-05 14:50:40 -0600208 bootproto = get_boot_protocol(hdr, true);
Gabe Black540c2622011-12-05 12:09:26 +0000209
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700210 log_debug("Using boot protocol version %x.%02x\n",
211 (bootproto & 0xff00) >> 8, bootproto & 0xff);
Gabe Black540c2622011-12-05 12:09:26 +0000212
Simon Glass531efde2023-07-12 09:04:44 -0600213 version = zimage_get_kernel_version(params, image);
Simon Glassdf30d412020-09-05 14:50:40 -0600214 if (version)
215 printf("Linux kernel version %s\n", version);
216 else
217 printf("Setup Sectors < 15 - Cannot print kernel version\n");
Gabe Black540c2622011-12-05 12:09:26 +0000218
wdenk591dda52002-11-18 00:14:45 +0000219 /* Determine image type */
Graeme Russ883c6032011-11-08 02:33:15 +0000220 big_image = (bootproto >= 0x0200) &&
221 (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk57b2d802003-06-27 21:31:46 +0000222
Graeme Russ1bab1042010-04-24 00:05:49 +1000223 /* Determine load address */
Gabe Black1cf51212011-12-05 12:09:23 +0000224 if (big_image)
Simon Glassd98a5bd2014-10-10 08:21:56 -0600225 *load_addressp = BZIMAGE_LOAD_ADDR;
Gabe Black1cf51212011-12-05 12:09:23 +0000226 else
Simon Glassd98a5bd2014-10-10 08:21:56 -0600227 *load_addressp = ZIMAGE_LOAD_ADDR;
wdenk57b2d802003-06-27 21:31:46 +0000228
Gabe Blackc67a9482011-12-05 12:09:24 +0000229 printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
230 memset(setup_base, 0, sizeof(*setup_base));
231 setup_base->hdr = params->hdr;
wdenk57b2d802003-06-27 21:31:46 +0000232
Gabe Black540c2622011-12-05 12:09:26 +0000233 if (bootproto >= 0x0204)
234 kernel_size = hdr->syssize * 16;
235 else
236 kernel_size -= setup_size;
wdenk57b2d802003-06-27 21:31:46 +0000237
wdenk57b2d802003-06-27 21:31:46 +0000238 if (bootproto == 0x0100) {
Graeme Russ883c6032011-11-08 02:33:15 +0000239 /*
240 * A very old kernel MUST have its real-mode code
241 * loaded at 0x90000
242 */
Simon Glassca37a392017-01-16 07:03:35 -0700243 if ((ulong)setup_base != 0x90000) {
wdenk591dda52002-11-18 00:14:45 +0000244 /* Copy the real-mode kernel */
Graeme Russ883c6032011-11-08 02:33:15 +0000245 memmove((void *)0x90000, setup_base, setup_size);
246
wdenk591dda52002-11-18 00:14:45 +0000247 /* Copy the command line */
Graeme Russ883c6032011-11-08 02:33:15 +0000248 memmove((void *)0x99000,
Gabe Black1cf51212011-12-05 12:09:23 +0000249 (u8 *)setup_base + COMMAND_LINE_OFFSET,
Graeme Russ883c6032011-11-08 02:33:15 +0000250 COMMAND_LINE_SIZE);
wdenk57b2d802003-06-27 21:31:46 +0000251
Graeme Russ883c6032011-11-08 02:33:15 +0000252 /* Relocated */
Gabe Black1cf51212011-12-05 12:09:23 +0000253 setup_base = (struct boot_params *)0x90000;
wdenk591dda52002-11-18 00:14:45 +0000254 }
wdenk57b2d802003-06-27 21:31:46 +0000255
wdenk591dda52002-11-18 00:14:45 +0000256 /* It is recommended to clear memory up to the 32K mark */
Gabe Black1cf51212011-12-05 12:09:23 +0000257 memset((u8 *)0x90000 + setup_size, 0,
258 SETUP_MAX_SIZE - setup_size);
wdenk591dda52002-11-18 00:14:45 +0000259 }
wdenk57b2d802003-06-27 21:31:46 +0000260
Gabe Black540c2622011-12-05 12:09:26 +0000261 if (big_image) {
262 if (kernel_size > BZIMAGE_MAX_SIZE) {
263 printf("Error: bzImage kernel too big! "
264 "(size: %ld, max: %d)\n",
265 kernel_size, BZIMAGE_MAX_SIZE);
266 return 0;
267 }
268 } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
269 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
270 kernel_size, ZIMAGE_MAX_SIZE);
271 return 0;
272 }
273
Simon Glassd98a5bd2014-10-10 08:21:56 -0600274 printf("Loading %s at address %lx (%ld bytes)\n",
275 big_image ? "bzImage" : "zImage", *load_addressp, kernel_size);
Gabe Black540c2622011-12-05 12:09:26 +0000276
Simon Glassd98a5bd2014-10-10 08:21:56 -0600277 memmove((void *)*load_addressp, image + setup_size, kernel_size);
Gabe Black540c2622011-12-05 12:09:26 +0000278
279 return setup_base;
280}
281
282int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
Simon Glass12d93ab2020-09-05 14:50:51 -0600283 ulong initrd_addr, ulong initrd_size, ulong cmdline_force)
Gabe Black540c2622011-12-05 12:09:26 +0000284{
285 struct setup_header *hdr = &setup_base->hdr;
Simon Glassdf30d412020-09-05 14:50:40 -0600286 int bootproto = get_boot_protocol(hdr, false);
Gabe Black540c2622011-12-05 12:09:26 +0000287
Simon Glass842ff442020-11-04 09:59:13 -0700288 log_debug("Setup E820 entries\n");
Simon Glass0de9c372021-07-10 21:15:21 -0600289 if (IS_ENABLED(CONFIG_COREBOOT_SYSINFO)) {
Simon Glass02c51202021-03-15 18:00:23 +1300290 setup_base->e820_entries = cb_install_e820_map(
291 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Simon Glass0de9c372021-07-10 21:15:21 -0600292 } else {
293 setup_base->e820_entries = install_e820_map(
294 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Simon Glass02c51202021-03-15 18:00:23 +1300295 }
Graeme Russ1bab1042010-04-24 00:05:49 +1000296
Gabe Black540c2622011-12-05 12:09:26 +0000297 if (bootproto == 0x0100) {
298 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
299 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
300 }
wdenk591dda52002-11-18 00:14:45 +0000301 if (bootproto >= 0x0200) {
Simon Glassd67ff032020-09-05 14:50:41 -0600302 hdr->type_of_loader = 0x80; /* U-Boot version 0 */
wdenk591dda52002-11-18 00:14:45 +0000303 if (initrd_addr) {
Gabe Black1cf51212011-12-05 12:09:23 +0000304 printf("Initial RAM disk at linear address "
305 "0x%08lx, size %ld bytes\n",
wdenk591dda52002-11-18 00:14:45 +0000306 initrd_addr, initrd_size);
wdenk57b2d802003-06-27 21:31:46 +0000307
Graeme Russ1bab1042010-04-24 00:05:49 +1000308 hdr->ramdisk_image = initrd_addr;
309 hdr->ramdisk_size = initrd_size;
wdenk591dda52002-11-18 00:14:45 +0000310 }
311 }
wdenk57b2d802003-06-27 21:31:46 +0000312
wdenk591dda52002-11-18 00:14:45 +0000313 if (bootproto >= 0x0201) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000314 hdr->heap_end_ptr = HEAP_END_OFFSET;
315 hdr->loadflags |= HEAP_FLAG;
wdenk591dda52002-11-18 00:14:45 +0000316 }
wdenk57b2d802003-06-27 21:31:46 +0000317
Simon Glass4b4f2732014-10-19 21:11:21 -0600318 if (cmd_line) {
Simon Glass7b48ce82020-11-05 10:33:46 -0700319 int max_size = 0xff;
320 int ret;
321
Simon Glass842ff442020-11-04 09:59:13 -0700322 log_debug("Setup cmdline\n");
Simon Glass7b48ce82020-11-05 10:33:46 -0700323 if (bootproto >= 0x0206)
324 max_size = hdr->cmdline_size;
Simon Glass4b4f2732014-10-19 21:11:21 -0600325 if (bootproto >= 0x0202) {
326 hdr->cmd_line_ptr = (uintptr_t)cmd_line;
327 } else if (bootproto >= 0x0200) {
328 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
329 setup_base->screen_info.cl_offset =
330 (uintptr_t)cmd_line - (uintptr_t)setup_base;
Graeme Russ1bab1042010-04-24 00:05:49 +1000331
Simon Glass4b4f2732014-10-19 21:11:21 -0600332 hdr->setup_move_size = 0x9100;
333 }
334
335 /* build command line at COMMAND_LINE_OFFSET */
Simon Glass12d93ab2020-09-05 14:50:51 -0600336 if (cmdline_force)
337 strcpy(cmd_line, (char *)cmdline_force);
338 else
339 build_command_line(cmd_line, auto_boot);
Simon Glasse7506982021-12-29 11:57:39 -0700340 if (IS_ENABLED(CONFIG_CMD_BOOTM)) {
341 ret = bootm_process_cmdline(cmd_line, max_size,
342 BOOTM_CL_ALL);
343 if (ret) {
344 printf("Cmdline setup failed (max_size=%x, bootproto=%x, err=%d)\n",
345 max_size, bootproto, ret);
346 return ret;
347 }
Simon Glass7b48ce82020-11-05 10:33:46 -0700348 }
349 printf("Kernel command line: \"");
350 puts(cmd_line);
351 printf("\"\n");
wdenk591dda52002-11-18 00:14:45 +0000352 }
wdenk591dda52002-11-18 00:14:45 +0000353
Simon Glass3a177172020-09-05 14:50:39 -0600354 if (IS_ENABLED(CONFIG_INTEL_MID) && bootproto >= 0x0207)
Andy Shevchenko8b7adf82018-01-10 19:40:14 +0200355 hdr->hardware_subarch = X86_SUBARCH_INTEL_MID;
Andy Shevchenko8b7adf82018-01-10 19:40:14 +0200356
Simon Glass3a177172020-09-05 14:50:39 -0600357 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
358 setup_base->acpi_rsdp_addr = acpi_get_rsdp_addr();
Andy Shevchenkod87a21d2019-09-13 18:42:00 +0300359
Simon Glass842ff442020-11-04 09:59:13 -0700360 log_debug("Setup devicetree\n");
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700361 setup_device_tree(hdr, (const void *)env_get_hex("fdtaddr", 0));
Bin Mengf6d504f2015-07-06 16:31:36 +0800362 setup_video(&setup_base->screen_info);
363
Simon Glass3a177172020-09-05 14:50:39 -0600364 if (IS_ENABLED(CONFIG_EFI_STUB))
365 setup_efi_info(&setup_base->efi_info);
Bin Meng106dcfc2018-08-23 08:24:10 -0700366
Gabe Black540c2622011-12-05 12:09:26 +0000367 return 0;
wdenk591dda52002-11-18 00:14:45 +0000368}
369
Simon Glass2bf90bd2023-12-03 17:29:27 -0700370int zboot_load(void)
Simon Glass24d26f72020-09-05 14:50:46 -0600371{
372 struct boot_params *base_ptr;
373
Simon Glassc55ce682020-09-05 14:50:49 -0600374 if (state.base_ptr) {
375 struct boot_params *from = (struct boot_params *)state.base_ptr;
376
377 base_ptr = (struct boot_params *)DEFAULT_SETUP_BASE;
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700378 log_debug("Building boot_params at 0x%8.8lx\n",
379 (ulong)base_ptr);
Simon Glassc55ce682020-09-05 14:50:49 -0600380 memset(base_ptr, '\0', sizeof(*base_ptr));
381 base_ptr->hdr = from->hdr;
382 } else {
383 base_ptr = load_zimage((void *)state.bzimage_addr, state.bzimage_size,
384 &state.load_address);
385 if (!base_ptr) {
386 puts("## Kernel loading failed ...\n");
Simon Glass84305a12023-07-12 09:04:43 -0600387 return -EINVAL;
Simon Glassc55ce682020-09-05 14:50:49 -0600388 }
Graeme Russ1bab1042010-04-24 00:05:49 +1000389 }
Simon Glass1931b192020-09-05 14:50:44 -0600390 state.base_ptr = base_ptr;
Simon Glass84305a12023-07-12 09:04:43 -0600391
392 return 0;
393}
394
Simon Glass2bf90bd2023-12-03 17:29:27 -0700395int zboot_setup(void)
Simon Glass84305a12023-07-12 09:04:43 -0600396{
397 struct boot_params *base_ptr = state.base_ptr;
398 int ret;
399
400 ret = setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
401 0, state.initrd_addr, state.initrd_size,
402 (ulong)state.cmdline);
403 if (ret)
404 return -EINVAL;
405
406 return 0;
407}
408
Simon Glass2bf90bd2023-12-03 17:29:27 -0700409int zboot_go(void)
Simon Glass1931b192020-09-05 14:50:44 -0600410{
Simon Glass6d496322023-03-20 08:30:08 +1300411 struct boot_params *params = state.base_ptr;
412 struct setup_header *hdr = &params->hdr;
413 bool image_64bit;
414 ulong entry;
Simon Glass1931b192020-09-05 14:50:44 -0600415 int ret;
416
Simon Glass0db800c2020-09-05 14:50:42 -0600417 disable_interrupts();
Simon Glass1931b192020-09-05 14:50:44 -0600418
Simon Glass6d496322023-03-20 08:30:08 +1300419 entry = state.load_address;
420 image_64bit = false;
421 if (IS_ENABLED(CONFIG_X86_RUN_64BIT) &&
422 (hdr->xloadflags & XLF_KERNEL_64)) {
423 entry += 0x200;
424 image_64bit = true;
425 }
426
Gabe Black540c2622011-12-05 12:09:26 +0000427 /* we assume that the kernel is in place */
Simon Glass6d496322023-03-20 08:30:08 +1300428 ret = boot_linux_kernel((ulong)state.base_ptr, entry, image_64bit);
Simon Glass84305a12023-07-12 09:04:43 -0600429
430 return ret;
431}
432
Simon Glass84305a12023-07-12 09:04:43 -0600433int zboot_start(ulong addr, ulong size, ulong initrd, ulong initrd_size,
434 ulong base, char *cmdline)
435{
436 int ret;
437
438 memset(&state, '\0', sizeof(state));
439
440 if (base) {
441 state.base_ptr = map_sysmem(base, 0);
442 state.load_address = addr;
443 } else {
444 state.bzimage_addr = addr;
445 }
446 state.bzimage_size = size;
447 state.initrd_addr = initrd;
448 state.initrd_size = initrd_size;
449 state.cmdline = cmdline;
450
451 ret = zboot_load();
452 if (ret)
453 return log_msg_ret("ld", ret);
454 ret = zboot_setup();
455 if (ret)
456 return log_msg_ret("set", ret);
457 ret = zboot_go();
458 if (ret)
459 return log_msg_ret("set", ret);
460
461 return -EFAULT;
462}
463
Simon Glassaad0d762020-09-05 14:50:50 -0600464static void print_num(const char *name, ulong value)
465{
466 printf("%-20s: %lx\n", name, value);
467}
468
469static void print_num64(const char *name, u64 value)
470{
471 printf("%-20s: %llx\n", name, value);
472}
473
474static const char *const e820_type_name[E820_COUNT] = {
475 [E820_RAM] = "RAM",
476 [E820_RESERVED] = "Reserved",
477 [E820_ACPI] = "ACPI",
478 [E820_NVS] = "ACPI NVS",
479 [E820_UNUSABLE] = "Unusable",
480};
481
482static const char *const bootloader_id[] = {
483 "LILO",
484 "Loadlin",
485 "bootsect-loader",
486 "Syslinux",
487 "Etherboot/gPXE/iPXE",
488 "ELILO",
489 "undefined",
490 "GRUB",
491 "U-Boot",
492 "Xen",
493 "Gujin",
494 "Qemu",
495 "Arcturus Networks uCbootloader",
496 "kexec-tools",
497 "Extended",
498 "Special",
499 "Reserved",
500 "Minimal Linux Bootloader",
501 "OVMF UEFI virtualization stack",
502};
503
504struct flag_info {
505 uint bit;
506 const char *name;
507};
508
509static struct flag_info load_flags[] = {
510 { LOADED_HIGH, "loaded-high" },
511 { QUIET_FLAG, "quiet" },
512 { KEEP_SEGMENTS, "keep-segments" },
513 { CAN_USE_HEAP, "can-use-heap" },
514};
515
516static struct flag_info xload_flags[] = {
517 { XLF_KERNEL_64, "64-bit-entry" },
518 { XLF_CAN_BE_LOADED_ABOVE_4G, "can-load-above-4gb" },
519 { XLF_EFI_HANDOVER_32, "32-efi-handoff" },
520 { XLF_EFI_HANDOVER_64, "64-efi-handoff" },
521 { XLF_EFI_KEXEC, "kexec-efi-runtime" },
522};
523
524static void print_flags(struct flag_info *flags, int count, uint value)
525{
526 int i;
527
528 printf("%-20s:", "");
529 for (i = 0; i < count; i++) {
530 uint mask = flags[i].bit;
531
532 if (value & mask)
533 printf(" %s", flags[i].name);
534 }
535 printf("\n");
536}
537
538static void show_loader(struct setup_header *hdr)
539{
540 bool version_valid = false;
541 int type, version;
542 const char *name;
543
544 type = hdr->type_of_loader >> 4;
545 version = hdr->type_of_loader & 0xf;
546 if (type == 0xe)
547 type = 0x10 + hdr->ext_loader_type;
548 version |= hdr->ext_loader_ver << 4;
549 if (!hdr->type_of_loader) {
550 name = "pre-2.00 bootloader";
551 } else if (hdr->type_of_loader == 0xff) {
552 name = "unknown";
553 } else if (type < ARRAY_SIZE(bootloader_id)) {
554 name = bootloader_id[type];
555 version_valid = true;
556 } else {
557 name = "undefined";
558 }
559 printf("%20s %s", "", name);
560 if (version_valid)
561 printf(", version %x", version);
562 printf("\n");
563}
564
Simon Glass5495aaf2023-07-30 11:17:00 -0600565void zimage_dump(struct boot_params *base_ptr, bool show_cmdline)
Simon Glassaad0d762020-09-05 14:50:50 -0600566{
Simon Glassaad0d762020-09-05 14:50:50 -0600567 struct setup_header *hdr;
568 const char *version;
569 int i;
570
Simon Glassaad0d762020-09-05 14:50:50 -0600571 printf("Setup located at %p:\n\n", base_ptr);
572 print_num64("ACPI RSDP addr", base_ptr->acpi_rsdp_addr);
573
574 printf("E820: %d entries\n", base_ptr->e820_entries);
575 if (base_ptr->e820_entries) {
Simon Glass5495aaf2023-07-30 11:17:00 -0600576 printf("%12s %10s %s\n", "Addr", "Size", "Type");
Simon Glassaad0d762020-09-05 14:50:50 -0600577 for (i = 0; i < base_ptr->e820_entries; i++) {
578 struct e820_entry *entry = &base_ptr->e820_map[i];
579
580 printf("%12llx %10llx %s\n", entry->addr, entry->size,
581 entry->type < E820_COUNT ?
582 e820_type_name[entry->type] :
583 simple_itoa(entry->type));
584 }
585 }
586
587 hdr = &base_ptr->hdr;
588 print_num("Setup sectors", hdr->setup_sects);
589 print_num("Root flags", hdr->root_flags);
590 print_num("Sys size", hdr->syssize);
591 print_num("RAM size", hdr->ram_size);
592 print_num("Video mode", hdr->vid_mode);
593 print_num("Root dev", hdr->root_dev);
594 print_num("Boot flag", hdr->boot_flag);
595 print_num("Jump", hdr->jump);
596 print_num("Header", hdr->header);
597 if (hdr->header == KERNEL_V2_MAGIC)
598 printf("%-20s %s\n", "", "Kernel V2");
599 else
600 printf("%-20s %s\n", "", "Ancient kernel, using version 100");
601 print_num("Version", hdr->version);
602 print_num("Real mode switch", hdr->realmode_swtch);
Simon Glass83ec1ca2023-03-20 08:30:06 +1300603 print_num("Start sys seg", hdr->start_sys_seg);
Simon Glassaad0d762020-09-05 14:50:50 -0600604 print_num("Kernel version", hdr->kernel_version);
Simon Glass531efde2023-07-12 09:04:44 -0600605 version = zimage_get_kernel_version(base_ptr,
606 (void *)state.bzimage_addr);
Simon Glassaad0d762020-09-05 14:50:50 -0600607 if (version)
608 printf(" @%p: %s\n", version, version);
609 print_num("Type of loader", hdr->type_of_loader);
610 show_loader(hdr);
611 print_num("Load flags", hdr->loadflags);
612 print_flags(load_flags, ARRAY_SIZE(load_flags), hdr->loadflags);
613 print_num("Setup move size", hdr->setup_move_size);
614 print_num("Code32 start", hdr->code32_start);
615 print_num("Ramdisk image", hdr->ramdisk_image);
616 print_num("Ramdisk size", hdr->ramdisk_size);
617 print_num("Bootsect kludge", hdr->bootsect_kludge);
618 print_num("Heap end ptr", hdr->heap_end_ptr);
619 print_num("Ext loader ver", hdr->ext_loader_ver);
620 print_num("Ext loader type", hdr->ext_loader_type);
621 print_num("Command line ptr", hdr->cmd_line_ptr);
Simon Glass5495aaf2023-07-30 11:17:00 -0600622 if (show_cmdline && hdr->cmd_line_ptr) {
Simon Glassaad0d762020-09-05 14:50:50 -0600623 printf(" ");
624 /* Use puts() to avoid limits from CONFIG_SYS_PBSIZE */
Simon Glass12d93ab2020-09-05 14:50:51 -0600625 puts((char *)(ulong)hdr->cmd_line_ptr);
Simon Glassaad0d762020-09-05 14:50:50 -0600626 printf("\n");
627 }
628 print_num("Initrd addr max", hdr->initrd_addr_max);
629 print_num("Kernel alignment", hdr->kernel_alignment);
630 print_num("Relocatable kernel", hdr->relocatable_kernel);
631 print_num("Min alignment", hdr->min_alignment);
632 if (hdr->min_alignment)
633 printf("%-20s: %x\n", "", 1 << hdr->min_alignment);
634 print_num("Xload flags", hdr->xloadflags);
635 print_flags(xload_flags, ARRAY_SIZE(xload_flags), hdr->xloadflags);
636 print_num("Cmdline size", hdr->cmdline_size);
637 print_num("Hardware subarch", hdr->hardware_subarch);
638 print_num64("HW subarch data", hdr->hardware_subarch_data);
639 print_num("Payload offset", hdr->payload_offset);
640 print_num("Payload length", hdr->payload_length);
641 print_num64("Setup data", hdr->setup_data);
642 print_num64("Pref address", hdr->pref_address);
643 print_num("Init size", hdr->init_size);
644 print_num("Handover offset", hdr->handover_offset);
645 if (get_boot_protocol(hdr, false) >= 0x215)
646 print_num("Kernel info offset", hdr->kernel_info_offset);
Simon Glassba094bb2021-01-24 10:06:08 -0700647}