blob: d71285e71d95bfda80ae7246c3f773b723bfc9c7 [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
Simon Glass7b48ce82020-11-05 10:33:46 -070017#include <bootm.h>
Simon Glassed38aef2020-05-10 11:40:03 -060018#include <command.h>
Simon Glass83c2e492019-08-01 09:46:50 -060019#include <env.h>
Simon Glass02c51202021-03-15 18:00:23 +130020#include <init.h>
Simon Glass8f3f7612019-11-14 12:57:42 -070021#include <irq_func.h>
Simon Glass842ff442020-11-04 09:59:13 -070022#include <log.h>
Ivan Gorinov901bb0b2018-03-26 18:06:54 -070023#include <malloc.h>
Simon Glass84305a12023-07-12 09:04:43 -060024#include <mapmem.h>
Simon Glass858fed12020-04-08 16:57:36 -060025#include <acpi/acpi_table.h>
wdenk591dda52002-11-18 00:14:45 +000026#include <asm/io.h>
27#include <asm/ptrace.h>
28#include <asm/zimage.h>
wdenk591dda52002-11-18 00:14:45 +000029#include <asm/byteorder.h>
Simon Glassaa83f2b2014-10-19 21:11:20 -060030#include <asm/bootm.h>
Graeme Russ1bab1042010-04-24 00:05:49 +100031#include <asm/bootparam.h>
Simon Glassb73d48f2021-09-24 18:30:20 -060032#include <asm/efi.h>
Simon Glass02c51202021-03-15 18:00:23 +130033#include <asm/global_data.h>
Vadim Bendebury8cd22c82012-10-23 18:04:34 +000034#ifdef CONFIG_SYS_COREBOOT
35#include <asm/arch/timestamp.h>
36#endif
Stefan Reinauer2e5225e2012-10-23 18:04:37 +000037#include <linux/compiler.h>
Simon Glass8ee28542020-11-04 09:59:14 -070038#include <linux/ctype.h>
Ivan Gorinov901bb0b2018-03-26 18:06:54 -070039#include <linux/libfdt.h>
wdenk591dda52002-11-18 00:14:45 +000040
Simon Glass02c51202021-03-15 18:00:23 +130041DECLARE_GLOBAL_DATA_PTR;
42
wdenk591dda52002-11-18 00:14:45 +000043/*
44 * Memory lay-out:
wdenk57b2d802003-06-27 21:31:46 +000045 *
wdenk591dda52002-11-18 00:14:45 +000046 * relative to setup_base (which is 0x90000 currently)
wdenk57b2d802003-06-27 21:31:46 +000047 *
48 * 0x0000-0x7FFF Real mode kernel
wdenk591dda52002-11-18 00:14:45 +000049 * 0x8000-0x8FFF Stack and heap
50 * 0x9000-0x90FF Kernel command line
51 */
Graeme Russ883c6032011-11-08 02:33:15 +000052#define DEFAULT_SETUP_BASE 0x90000
53#define COMMAND_LINE_OFFSET 0x9000
54#define HEAP_END_OFFSET 0x8e00
wdenk591dda52002-11-18 00:14:45 +000055
Graeme Russ883c6032011-11-08 02:33:15 +000056#define COMMAND_LINE_SIZE 2048
wdenk591dda52002-11-18 00:14:45 +000057
58static void build_command_line(char *command_line, int auto_boot)
59{
60 char *env_command_line;
wdenk57b2d802003-06-27 21:31:46 +000061
wdenk591dda52002-11-18 00:14:45 +000062 command_line[0] = '\0';
wdenk57b2d802003-06-27 21:31:46 +000063
Simon Glass64b723f2017-08-03 12:22:12 -060064 env_command_line = env_get("bootargs");
wdenk57b2d802003-06-27 21:31:46 +000065
wdenk591dda52002-11-18 00:14:45 +000066 /* set console= argument if we use a serial console */
Graeme Russ883c6032011-11-08 02:33:15 +000067 if (!strstr(env_command_line, "console=")) {
Simon Glass64b723f2017-08-03 12:22:12 -060068 if (!strcmp(env_get("stdout"), "serial")) {
wdenk57b2d802003-06-27 21:31:46 +000069
wdenk591dda52002-11-18 00:14:45 +000070 /* We seem to use serial console */
wdenk57b2d802003-06-27 21:31:46 +000071 sprintf(command_line, "console=ttyS0,%s ",
Simon Glass64b723f2017-08-03 12:22:12 -060072 env_get("baudrate"));
wdenk591dda52002-11-18 00:14:45 +000073 }
74 }
wdenk57b2d802003-06-27 21:31:46 +000075
Graeme Russ883c6032011-11-08 02:33:15 +000076 if (auto_boot)
wdenk591dda52002-11-18 00:14:45 +000077 strcat(command_line, "auto ");
wdenk57b2d802003-06-27 21:31:46 +000078
Graeme Russ883c6032011-11-08 02:33:15 +000079 if (env_command_line)
wdenk591dda52002-11-18 00:14:45 +000080 strcat(command_line, env_command_line);
Simon Glasseeb01222021-01-24 10:06:09 -070081#ifdef DEBUG
82 printf("Kernel command line:");
83 puts(command_line);
84 printf("\n");
85#endif
wdenk591dda52002-11-18 00:14:45 +000086}
87
Gabe Black540c2622011-12-05 12:09:26 +000088static int kernel_magic_ok(struct setup_header *hdr)
wdenk591dda52002-11-18 00:14:45 +000089{
Graeme Russ1bab1042010-04-24 00:05:49 +100090 if (KERNEL_MAGIC != hdr->boot_flag) {
Gabe Black1cf51212011-12-05 12:09:23 +000091 printf("Error: Invalid Boot Flag "
92 "(found 0x%04x, expected 0x%04x)\n",
93 hdr->boot_flag, KERNEL_MAGIC);
wdenk591dda52002-11-18 00:14:45 +000094 return 0;
Graeme Russ1bab1042010-04-24 00:05:49 +100095 } else {
96 printf("Valid Boot Flag\n");
Gabe Black540c2622011-12-05 12:09:26 +000097 return 1;
wdenk591dda52002-11-18 00:14:45 +000098 }
Gabe Black540c2622011-12-05 12:09:26 +000099}
wdenk57b2d802003-06-27 21:31:46 +0000100
Simon Glassdf30d412020-09-05 14:50:40 -0600101static int get_boot_protocol(struct setup_header *hdr, bool verbose)
Gabe Black540c2622011-12-05 12:09:26 +0000102{
103 if (hdr->header == KERNEL_V2_MAGIC) {
Simon Glassdf30d412020-09-05 14:50:40 -0600104 if (verbose)
105 printf("Magic signature found\n");
Gabe Black540c2622011-12-05 12:09:26 +0000106 return hdr->version;
wdenk591dda52002-11-18 00:14:45 +0000107 } else {
108 /* Very old kernel */
Simon Glassdf30d412020-09-05 14:50:40 -0600109 if (verbose)
110 printf("Magic signature not found\n");
Gabe Black540c2622011-12-05 12:09:26 +0000111 return 0x0100;
wdenk591dda52002-11-18 00:14:45 +0000112 }
Gabe Black540c2622011-12-05 12:09:26 +0000113}
114
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700115static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob)
116{
Simon Glassdf30d412020-09-05 14:50:40 -0600117 int bootproto = get_boot_protocol(hdr, false);
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700118 struct setup_data *sd;
119 int size;
120
121 if (bootproto < 0x0209)
122 return -ENOTSUPP;
123
124 if (!fdt_blob)
125 return 0;
126
127 size = fdt_totalsize(fdt_blob);
128 if (size < 0)
129 return -EINVAL;
130
131 size += sizeof(struct setup_data);
132 sd = (struct setup_data *)malloc(size);
133 if (!sd) {
134 printf("Not enough memory for DTB setup data\n");
135 return -ENOMEM;
136 }
137
138 sd->next = hdr->setup_data;
139 sd->type = SETUP_DTB;
140 sd->len = fdt_totalsize(fdt_blob);
141 memcpy(sd->data, fdt_blob, sd->len);
142 hdr->setup_data = (unsigned long)sd;
143
144 return 0;
145}
146
Simon Glass531efde2023-07-12 09:04:44 -0600147const char *zimage_get_kernel_version(struct boot_params *params,
Simon Glassdf30d412020-09-05 14:50:40 -0600148 void *kernel_base)
149{
150 struct setup_header *hdr = &params->hdr;
151 int bootproto;
Simon Glass8ee28542020-11-04 09:59:14 -0700152 const char *s, *end;
Simon Glassdf30d412020-09-05 14:50:40 -0600153
154 bootproto = get_boot_protocol(hdr, false);
Simon Glass531efde2023-07-12 09:04:44 -0600155 log_debug("bootproto %x, hdr->setup_sects %x\n", bootproto,
156 hdr->setup_sects);
Simon Glassdf30d412020-09-05 14:50:40 -0600157 if (bootproto < 0x0200 || hdr->setup_sects < 15)
158 return NULL;
159
Simon Glass8ee28542020-11-04 09:59:14 -0700160 /* sanity-check the kernel version in case it is missing */
Simon Glass531efde2023-07-12 09:04:44 -0600161 log_debug("hdr->kernel_version %x, str at %p\n", hdr->kernel_version,
162 kernel_base + hdr->kernel_version + 0x200);
Simon Glass8ee28542020-11-04 09:59:14 -0700163 for (s = kernel_base + hdr->kernel_version + 0x200, end = s + 0x100; *s;
164 s++) {
165 if (!isprint(*s))
166 return NULL;
167 }
168
Simon Glassdf30d412020-09-05 14:50:40 -0600169 return kernel_base + hdr->kernel_version + 0x200;
170}
171
Gabe Black540c2622011-12-05 12:09:26 +0000172struct boot_params *load_zimage(char *image, unsigned long kernel_size,
Simon Glassd98a5bd2014-10-10 08:21:56 -0600173 ulong *load_addressp)
Gabe Black540c2622011-12-05 12:09:26 +0000174{
175 struct boot_params *setup_base;
Simon Glassdf30d412020-09-05 14:50:40 -0600176 const char *version;
Gabe Black540c2622011-12-05 12:09:26 +0000177 int setup_size;
178 int bootproto;
179 int big_image;
180
181 struct boot_params *params = (struct boot_params *)image;
182 struct setup_header *hdr = &params->hdr;
183
184 /* base address for real-mode segment */
185 setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
186
187 if (!kernel_magic_ok(hdr))
188 return 0;
wdenk57b2d802003-06-27 21:31:46 +0000189
wdenk591dda52002-11-18 00:14:45 +0000190 /* determine size of setup */
Graeme Russ1bab1042010-04-24 00:05:49 +1000191 if (0 == hdr->setup_sects) {
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700192 log_warning("Setup Sectors = 0 (defaulting to 4)\n");
wdenk591dda52002-11-18 00:14:45 +0000193 setup_size = 5 * 512;
194 } else {
Graeme Russ1bab1042010-04-24 00:05:49 +1000195 setup_size = (hdr->setup_sects + 1) * 512;
wdenk591dda52002-11-18 00:14:45 +0000196 }
wdenk57b2d802003-06-27 21:31:46 +0000197
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700198 log_debug("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
Graeme Russ1bab1042010-04-24 00:05:49 +1000199
Graeme Russ883c6032011-11-08 02:33:15 +0000200 if (setup_size > SETUP_MAX_SIZE)
wdenk591dda52002-11-18 00:14:45 +0000201 printf("Error: Setup is too large (%d bytes)\n", setup_size);
wdenk57b2d802003-06-27 21:31:46 +0000202
Gabe Black540c2622011-12-05 12:09:26 +0000203 /* determine boot protocol version */
Simon Glassdf30d412020-09-05 14:50:40 -0600204 bootproto = get_boot_protocol(hdr, true);
Gabe Black540c2622011-12-05 12:09:26 +0000205
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700206 log_debug("Using boot protocol version %x.%02x\n",
207 (bootproto & 0xff00) >> 8, bootproto & 0xff);
Gabe Black540c2622011-12-05 12:09:26 +0000208
Simon Glass531efde2023-07-12 09:04:44 -0600209 version = zimage_get_kernel_version(params, image);
Simon Glassdf30d412020-09-05 14:50:40 -0600210 if (version)
211 printf("Linux kernel version %s\n", version);
212 else
213 printf("Setup Sectors < 15 - Cannot print kernel version\n");
Gabe Black540c2622011-12-05 12:09:26 +0000214
wdenk591dda52002-11-18 00:14:45 +0000215 /* Determine image type */
Graeme Russ883c6032011-11-08 02:33:15 +0000216 big_image = (bootproto >= 0x0200) &&
217 (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk57b2d802003-06-27 21:31:46 +0000218
Graeme Russ1bab1042010-04-24 00:05:49 +1000219 /* Determine load address */
Gabe Black1cf51212011-12-05 12:09:23 +0000220 if (big_image)
Simon Glassd98a5bd2014-10-10 08:21:56 -0600221 *load_addressp = BZIMAGE_LOAD_ADDR;
Gabe Black1cf51212011-12-05 12:09:23 +0000222 else
Simon Glassd98a5bd2014-10-10 08:21:56 -0600223 *load_addressp = ZIMAGE_LOAD_ADDR;
wdenk57b2d802003-06-27 21:31:46 +0000224
Gabe Blackc67a9482011-12-05 12:09:24 +0000225 printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
226 memset(setup_base, 0, sizeof(*setup_base));
227 setup_base->hdr = params->hdr;
wdenk57b2d802003-06-27 21:31:46 +0000228
Gabe Black540c2622011-12-05 12:09:26 +0000229 if (bootproto >= 0x0204)
230 kernel_size = hdr->syssize * 16;
231 else
232 kernel_size -= setup_size;
wdenk57b2d802003-06-27 21:31:46 +0000233
wdenk57b2d802003-06-27 21:31:46 +0000234 if (bootproto == 0x0100) {
Graeme Russ883c6032011-11-08 02:33:15 +0000235 /*
236 * A very old kernel MUST have its real-mode code
237 * loaded at 0x90000
238 */
Simon Glassca37a392017-01-16 07:03:35 -0700239 if ((ulong)setup_base != 0x90000) {
wdenk591dda52002-11-18 00:14:45 +0000240 /* Copy the real-mode kernel */
Graeme Russ883c6032011-11-08 02:33:15 +0000241 memmove((void *)0x90000, setup_base, setup_size);
242
wdenk591dda52002-11-18 00:14:45 +0000243 /* Copy the command line */
Graeme Russ883c6032011-11-08 02:33:15 +0000244 memmove((void *)0x99000,
Gabe Black1cf51212011-12-05 12:09:23 +0000245 (u8 *)setup_base + COMMAND_LINE_OFFSET,
Graeme Russ883c6032011-11-08 02:33:15 +0000246 COMMAND_LINE_SIZE);
wdenk57b2d802003-06-27 21:31:46 +0000247
Graeme Russ883c6032011-11-08 02:33:15 +0000248 /* Relocated */
Gabe Black1cf51212011-12-05 12:09:23 +0000249 setup_base = (struct boot_params *)0x90000;
wdenk591dda52002-11-18 00:14:45 +0000250 }
wdenk57b2d802003-06-27 21:31:46 +0000251
wdenk591dda52002-11-18 00:14:45 +0000252 /* It is recommended to clear memory up to the 32K mark */
Gabe Black1cf51212011-12-05 12:09:23 +0000253 memset((u8 *)0x90000 + setup_size, 0,
254 SETUP_MAX_SIZE - setup_size);
wdenk591dda52002-11-18 00:14:45 +0000255 }
wdenk57b2d802003-06-27 21:31:46 +0000256
Gabe Black540c2622011-12-05 12:09:26 +0000257 if (big_image) {
258 if (kernel_size > BZIMAGE_MAX_SIZE) {
259 printf("Error: bzImage kernel too big! "
260 "(size: %ld, max: %d)\n",
261 kernel_size, BZIMAGE_MAX_SIZE);
262 return 0;
263 }
264 } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
265 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
266 kernel_size, ZIMAGE_MAX_SIZE);
267 return 0;
268 }
269
Simon Glassd98a5bd2014-10-10 08:21:56 -0600270 printf("Loading %s at address %lx (%ld bytes)\n",
271 big_image ? "bzImage" : "zImage", *load_addressp, kernel_size);
Gabe Black540c2622011-12-05 12:09:26 +0000272
Simon Glassd98a5bd2014-10-10 08:21:56 -0600273 memmove((void *)*load_addressp, image + setup_size, kernel_size);
Gabe Black540c2622011-12-05 12:09:26 +0000274
275 return setup_base;
276}
277
278int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
Simon Glass12d93ab2020-09-05 14:50:51 -0600279 ulong initrd_addr, ulong initrd_size, ulong cmdline_force)
Gabe Black540c2622011-12-05 12:09:26 +0000280{
281 struct setup_header *hdr = &setup_base->hdr;
Simon Glassdf30d412020-09-05 14:50:40 -0600282 int bootproto = get_boot_protocol(hdr, false);
Gabe Black540c2622011-12-05 12:09:26 +0000283
Simon Glass842ff442020-11-04 09:59:13 -0700284 log_debug("Setup E820 entries\n");
Simon Glass0de9c372021-07-10 21:15:21 -0600285 if (IS_ENABLED(CONFIG_COREBOOT_SYSINFO)) {
Simon Glass02c51202021-03-15 18:00:23 +1300286 setup_base->e820_entries = cb_install_e820_map(
287 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Simon Glass0de9c372021-07-10 21:15:21 -0600288 } else {
289 setup_base->e820_entries = install_e820_map(
290 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Simon Glass02c51202021-03-15 18:00:23 +1300291 }
Graeme Russ1bab1042010-04-24 00:05:49 +1000292
Gabe Black540c2622011-12-05 12:09:26 +0000293 if (bootproto == 0x0100) {
294 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
295 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
296 }
wdenk591dda52002-11-18 00:14:45 +0000297 if (bootproto >= 0x0200) {
Simon Glassd67ff032020-09-05 14:50:41 -0600298 hdr->type_of_loader = 0x80; /* U-Boot version 0 */
wdenk591dda52002-11-18 00:14:45 +0000299 if (initrd_addr) {
Gabe Black1cf51212011-12-05 12:09:23 +0000300 printf("Initial RAM disk at linear address "
301 "0x%08lx, size %ld bytes\n",
wdenk591dda52002-11-18 00:14:45 +0000302 initrd_addr, initrd_size);
wdenk57b2d802003-06-27 21:31:46 +0000303
Graeme Russ1bab1042010-04-24 00:05:49 +1000304 hdr->ramdisk_image = initrd_addr;
305 hdr->ramdisk_size = initrd_size;
wdenk591dda52002-11-18 00:14:45 +0000306 }
307 }
wdenk57b2d802003-06-27 21:31:46 +0000308
wdenk591dda52002-11-18 00:14:45 +0000309 if (bootproto >= 0x0201) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000310 hdr->heap_end_ptr = HEAP_END_OFFSET;
311 hdr->loadflags |= HEAP_FLAG;
wdenk591dda52002-11-18 00:14:45 +0000312 }
wdenk57b2d802003-06-27 21:31:46 +0000313
Simon Glass4b4f2732014-10-19 21:11:21 -0600314 if (cmd_line) {
Simon Glass7b48ce82020-11-05 10:33:46 -0700315 int max_size = 0xff;
316 int ret;
317
Simon Glass842ff442020-11-04 09:59:13 -0700318 log_debug("Setup cmdline\n");
Simon Glass7b48ce82020-11-05 10:33:46 -0700319 if (bootproto >= 0x0206)
320 max_size = hdr->cmdline_size;
Simon Glass4b4f2732014-10-19 21:11:21 -0600321 if (bootproto >= 0x0202) {
322 hdr->cmd_line_ptr = (uintptr_t)cmd_line;
323 } else if (bootproto >= 0x0200) {
324 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
325 setup_base->screen_info.cl_offset =
326 (uintptr_t)cmd_line - (uintptr_t)setup_base;
Graeme Russ1bab1042010-04-24 00:05:49 +1000327
Simon Glass4b4f2732014-10-19 21:11:21 -0600328 hdr->setup_move_size = 0x9100;
329 }
330
331 /* build command line at COMMAND_LINE_OFFSET */
Simon Glass12d93ab2020-09-05 14:50:51 -0600332 if (cmdline_force)
333 strcpy(cmd_line, (char *)cmdline_force);
334 else
335 build_command_line(cmd_line, auto_boot);
Simon Glasse7506982021-12-29 11:57:39 -0700336 if (IS_ENABLED(CONFIG_CMD_BOOTM)) {
337 ret = bootm_process_cmdline(cmd_line, max_size,
338 BOOTM_CL_ALL);
339 if (ret) {
340 printf("Cmdline setup failed (max_size=%x, bootproto=%x, err=%d)\n",
341 max_size, bootproto, ret);
342 return ret;
343 }
Simon Glass7b48ce82020-11-05 10:33:46 -0700344 }
345 printf("Kernel command line: \"");
346 puts(cmd_line);
347 printf("\"\n");
wdenk591dda52002-11-18 00:14:45 +0000348 }
wdenk591dda52002-11-18 00:14:45 +0000349
Simon Glass3a177172020-09-05 14:50:39 -0600350 if (IS_ENABLED(CONFIG_INTEL_MID) && bootproto >= 0x0207)
Andy Shevchenko8b7adf82018-01-10 19:40:14 +0200351 hdr->hardware_subarch = X86_SUBARCH_INTEL_MID;
Andy Shevchenko8b7adf82018-01-10 19:40:14 +0200352
Simon Glass3a177172020-09-05 14:50:39 -0600353 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
354 setup_base->acpi_rsdp_addr = acpi_get_rsdp_addr();
Andy Shevchenkod87a21d2019-09-13 18:42:00 +0300355
Simon Glass842ff442020-11-04 09:59:13 -0700356 log_debug("Setup devicetree\n");
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700357 setup_device_tree(hdr, (const void *)env_get_hex("fdtaddr", 0));
Bin Mengf6d504f2015-07-06 16:31:36 +0800358 setup_video(&setup_base->screen_info);
359
Simon Glass3a177172020-09-05 14:50:39 -0600360 if (IS_ENABLED(CONFIG_EFI_STUB))
361 setup_efi_info(&setup_base->efi_info);
Bin Meng106dcfc2018-08-23 08:24:10 -0700362
Gabe Black540c2622011-12-05 12:09:26 +0000363 return 0;
wdenk591dda52002-11-18 00:14:45 +0000364}
365
Simon Glass7b15c572025-03-05 17:25:00 -0700366int zboot_load(struct bootm_info *bmi)
Simon Glass24d26f72020-09-05 14:50:46 -0600367{
368 struct boot_params *base_ptr;
Simon Glassf6d7ebd2023-12-03 17:29:31 -0700369 int ret;
Simon Glass24d26f72020-09-05 14:50:46 -0600370
Simon Glass7b15c572025-03-05 17:25:00 -0700371 if (bmi->base_ptr) {
372 struct boot_params *from = (struct boot_params *)bmi->base_ptr;
Simon Glassc55ce682020-09-05 14:50:49 -0600373
374 base_ptr = (struct boot_params *)DEFAULT_SETUP_BASE;
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700375 log_debug("Building boot_params at 0x%8.8lx\n",
376 (ulong)base_ptr);
Simon Glassc55ce682020-09-05 14:50:49 -0600377 memset(base_ptr, '\0', sizeof(*base_ptr));
378 base_ptr->hdr = from->hdr;
379 } else {
Simon Glass7b15c572025-03-05 17:25:00 -0700380 base_ptr = load_zimage((void *)bmi->bzimage_addr,
381 bmi->bzimage_size, &bmi->load_address);
Simon Glassc55ce682020-09-05 14:50:49 -0600382 if (!base_ptr) {
383 puts("## Kernel loading failed ...\n");
Simon Glass84305a12023-07-12 09:04:43 -0600384 return -EINVAL;
Simon Glassc55ce682020-09-05 14:50:49 -0600385 }
Graeme Russ1bab1042010-04-24 00:05:49 +1000386 }
Simon Glass7b15c572025-03-05 17:25:00 -0700387 bmi->base_ptr = base_ptr;
Simon Glass84305a12023-07-12 09:04:43 -0600388
Simon Glass7b15c572025-03-05 17:25:00 -0700389 ret = env_set_hex("zbootbase", map_to_sysmem(bmi->base_ptr));
Simon Glassf6d7ebd2023-12-03 17:29:31 -0700390 if (!ret)
Simon Glass7b15c572025-03-05 17:25:00 -0700391 ret = env_set_hex("zbootaddr", bmi->load_address);
Simon Glassf6d7ebd2023-12-03 17:29:31 -0700392 if (ret)
393 return ret;
394
Simon Glass84305a12023-07-12 09:04:43 -0600395 return 0;
396}
397
Simon Glass7b15c572025-03-05 17:25:00 -0700398int zboot_setup(struct bootm_info *bmi)
Simon Glass84305a12023-07-12 09:04:43 -0600399{
Simon Glass7b15c572025-03-05 17:25:00 -0700400 struct boot_params *base_ptr = bmi->base_ptr;
Simon Glass84305a12023-07-12 09:04:43 -0600401 int ret;
402
403 ret = setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
Simon Glass7b15c572025-03-05 17:25:00 -0700404 0, bmi->initrd_addr, bmi->initrd_size,
405 (ulong)bmi->cmdline);
Simon Glass84305a12023-07-12 09:04:43 -0600406 if (ret)
407 return -EINVAL;
408
409 return 0;
410}
411
Simon Glass7b15c572025-03-05 17:25:00 -0700412int zboot_go(struct bootm_info *bmi)
Simon Glass1931b192020-09-05 14:50:44 -0600413{
Simon Glass7b15c572025-03-05 17:25:00 -0700414 struct boot_params *params = bmi->base_ptr;
Simon Glass6d496322023-03-20 08:30:08 +1300415 struct setup_header *hdr = &params->hdr;
416 bool image_64bit;
417 ulong entry;
Simon Glass1931b192020-09-05 14:50:44 -0600418 int ret;
419
Simon Glass0db800c2020-09-05 14:50:42 -0600420 disable_interrupts();
Simon Glass1931b192020-09-05 14:50:44 -0600421
Simon Glass7b15c572025-03-05 17:25:00 -0700422 entry = bmi->load_address;
Simon Glass6d496322023-03-20 08:30:08 +1300423 image_64bit = false;
424 if (IS_ENABLED(CONFIG_X86_RUN_64BIT) &&
425 (hdr->xloadflags & XLF_KERNEL_64)) {
Simon Glass6d496322023-03-20 08:30:08 +1300426 image_64bit = true;
427 }
428
Gabe Black540c2622011-12-05 12:09:26 +0000429 /* we assume that the kernel is in place */
Simon Glass7b15c572025-03-05 17:25:00 -0700430 ret = boot_linux_kernel((ulong)bmi->base_ptr, entry, image_64bit);
Simon Glass84305a12023-07-12 09:04:43 -0600431
432 return ret;
433}
434
Simon Glass09519372025-03-05 17:24:56 -0700435int zboot_run_args(ulong addr, ulong size, ulong initrd, ulong initrd_size,
436 ulong base, char *cmdline)
Simon Glass84305a12023-07-12 09:04:43 -0600437{
Simon Glass7b15c572025-03-05 17:25:00 -0700438 struct bootm_info bmi;
Simon Glass84305a12023-07-12 09:04:43 -0600439 int ret;
440
Simon Glass7b15c572025-03-05 17:25:00 -0700441 bootm_init(&bmi);
442 zboot_start(&bmi, addr, size, initrd, initrd_size, base, cmdline);
443 ret = zboot_load(&bmi);
Simon Glass84305a12023-07-12 09:04:43 -0600444 if (ret)
445 return log_msg_ret("ld", ret);
Simon Glass7b15c572025-03-05 17:25:00 -0700446 ret = zboot_setup(&bmi);
Simon Glass84305a12023-07-12 09:04:43 -0600447 if (ret)
448 return log_msg_ret("set", ret);
Simon Glass7b15c572025-03-05 17:25:00 -0700449 ret = zboot_go(&bmi);
Simon Glass84305a12023-07-12 09:04:43 -0600450 if (ret)
Simon Glass365d17b2023-12-03 17:29:35 -0700451 return log_msg_ret("go", ret);
Simon Glass84305a12023-07-12 09:04:43 -0600452
453 return -EFAULT;
454}
455
Simon Glassaad0d762020-09-05 14:50:50 -0600456static void print_num(const char *name, ulong value)
457{
458 printf("%-20s: %lx\n", name, value);
459}
460
461static void print_num64(const char *name, u64 value)
462{
463 printf("%-20s: %llx\n", name, value);
464}
465
466static const char *const e820_type_name[E820_COUNT] = {
467 [E820_RAM] = "RAM",
468 [E820_RESERVED] = "Reserved",
469 [E820_ACPI] = "ACPI",
470 [E820_NVS] = "ACPI NVS",
471 [E820_UNUSABLE] = "Unusable",
472};
473
474static const char *const bootloader_id[] = {
475 "LILO",
476 "Loadlin",
477 "bootsect-loader",
478 "Syslinux",
479 "Etherboot/gPXE/iPXE",
480 "ELILO",
481 "undefined",
482 "GRUB",
483 "U-Boot",
484 "Xen",
485 "Gujin",
486 "Qemu",
487 "Arcturus Networks uCbootloader",
488 "kexec-tools",
489 "Extended",
490 "Special",
491 "Reserved",
492 "Minimal Linux Bootloader",
493 "OVMF UEFI virtualization stack",
494};
495
496struct flag_info {
497 uint bit;
498 const char *name;
499};
500
501static struct flag_info load_flags[] = {
502 { LOADED_HIGH, "loaded-high" },
503 { QUIET_FLAG, "quiet" },
504 { KEEP_SEGMENTS, "keep-segments" },
505 { CAN_USE_HEAP, "can-use-heap" },
506};
507
508static struct flag_info xload_flags[] = {
509 { XLF_KERNEL_64, "64-bit-entry" },
510 { XLF_CAN_BE_LOADED_ABOVE_4G, "can-load-above-4gb" },
511 { XLF_EFI_HANDOVER_32, "32-efi-handoff" },
512 { XLF_EFI_HANDOVER_64, "64-efi-handoff" },
513 { XLF_EFI_KEXEC, "kexec-efi-runtime" },
514};
515
516static void print_flags(struct flag_info *flags, int count, uint value)
517{
518 int i;
519
520 printf("%-20s:", "");
521 for (i = 0; i < count; i++) {
522 uint mask = flags[i].bit;
523
524 if (value & mask)
525 printf(" %s", flags[i].name);
526 }
527 printf("\n");
528}
529
530static void show_loader(struct setup_header *hdr)
531{
532 bool version_valid = false;
533 int type, version;
534 const char *name;
535
536 type = hdr->type_of_loader >> 4;
537 version = hdr->type_of_loader & 0xf;
538 if (type == 0xe)
539 type = 0x10 + hdr->ext_loader_type;
540 version |= hdr->ext_loader_ver << 4;
541 if (!hdr->type_of_loader) {
542 name = "pre-2.00 bootloader";
543 } else if (hdr->type_of_loader == 0xff) {
544 name = "unknown";
545 } else if (type < ARRAY_SIZE(bootloader_id)) {
546 name = bootloader_id[type];
547 version_valid = true;
548 } else {
549 name = "undefined";
550 }
551 printf("%20s %s", "", name);
552 if (version_valid)
553 printf(", version %x", version);
554 printf("\n");
555}
556
Simon Glass7b15c572025-03-05 17:25:00 -0700557void zimage_dump(struct bootm_info *bmi, struct boot_params *base_ptr,
558 bool show_cmdline)
Simon Glassaad0d762020-09-05 14:50:50 -0600559{
Simon Glassaad0d762020-09-05 14:50:50 -0600560 struct setup_header *hdr;
Simon Glassaad0d762020-09-05 14:50:50 -0600561 int i;
562
Simon Glassaad0d762020-09-05 14:50:50 -0600563 printf("Setup located at %p:\n\n", base_ptr);
564 print_num64("ACPI RSDP addr", base_ptr->acpi_rsdp_addr);
565
566 printf("E820: %d entries\n", base_ptr->e820_entries);
567 if (base_ptr->e820_entries) {
Simon Glass5495aaf2023-07-30 11:17:00 -0600568 printf("%12s %10s %s\n", "Addr", "Size", "Type");
Simon Glassaad0d762020-09-05 14:50:50 -0600569 for (i = 0; i < base_ptr->e820_entries; i++) {
570 struct e820_entry *entry = &base_ptr->e820_map[i];
571
572 printf("%12llx %10llx %s\n", entry->addr, entry->size,
573 entry->type < E820_COUNT ?
574 e820_type_name[entry->type] :
575 simple_itoa(entry->type));
576 }
577 }
578
579 hdr = &base_ptr->hdr;
580 print_num("Setup sectors", hdr->setup_sects);
581 print_num("Root flags", hdr->root_flags);
582 print_num("Sys size", hdr->syssize);
583 print_num("RAM size", hdr->ram_size);
584 print_num("Video mode", hdr->vid_mode);
585 print_num("Root dev", hdr->root_dev);
586 print_num("Boot flag", hdr->boot_flag);
587 print_num("Jump", hdr->jump);
588 print_num("Header", hdr->header);
589 if (hdr->header == KERNEL_V2_MAGIC)
590 printf("%-20s %s\n", "", "Kernel V2");
591 else
592 printf("%-20s %s\n", "", "Ancient kernel, using version 100");
593 print_num("Version", hdr->version);
594 print_num("Real mode switch", hdr->realmode_swtch);
Simon Glass83ec1ca2023-03-20 08:30:06 +1300595 print_num("Start sys seg", hdr->start_sys_seg);
Simon Glassaad0d762020-09-05 14:50:50 -0600596 print_num("Kernel version", hdr->kernel_version);
Simon Glassc0315912025-03-05 17:25:01 -0700597 if (bmi->bzimage_addr) {
598 const char *version;
599
600 version = zimage_get_kernel_version(base_ptr,
601 (void *)bmi->bzimage_addr);
602 if (version)
603 printf(" @%p: %s\n", version, version);
604 }
Simon Glassaad0d762020-09-05 14:50:50 -0600605 print_num("Type of loader", hdr->type_of_loader);
606 show_loader(hdr);
607 print_num("Load flags", hdr->loadflags);
608 print_flags(load_flags, ARRAY_SIZE(load_flags), hdr->loadflags);
609 print_num("Setup move size", hdr->setup_move_size);
610 print_num("Code32 start", hdr->code32_start);
611 print_num("Ramdisk image", hdr->ramdisk_image);
612 print_num("Ramdisk size", hdr->ramdisk_size);
613 print_num("Bootsect kludge", hdr->bootsect_kludge);
614 print_num("Heap end ptr", hdr->heap_end_ptr);
615 print_num("Ext loader ver", hdr->ext_loader_ver);
616 print_num("Ext loader type", hdr->ext_loader_type);
617 print_num("Command line ptr", hdr->cmd_line_ptr);
Simon Glass5495aaf2023-07-30 11:17:00 -0600618 if (show_cmdline && hdr->cmd_line_ptr) {
Simon Glassaad0d762020-09-05 14:50:50 -0600619 printf(" ");
620 /* Use puts() to avoid limits from CONFIG_SYS_PBSIZE */
Simon Glass12d93ab2020-09-05 14:50:51 -0600621 puts((char *)(ulong)hdr->cmd_line_ptr);
Simon Glassaad0d762020-09-05 14:50:50 -0600622 printf("\n");
623 }
624 print_num("Initrd addr max", hdr->initrd_addr_max);
625 print_num("Kernel alignment", hdr->kernel_alignment);
626 print_num("Relocatable kernel", hdr->relocatable_kernel);
627 print_num("Min alignment", hdr->min_alignment);
628 if (hdr->min_alignment)
629 printf("%-20s: %x\n", "", 1 << hdr->min_alignment);
630 print_num("Xload flags", hdr->xloadflags);
631 print_flags(xload_flags, ARRAY_SIZE(xload_flags), hdr->xloadflags);
632 print_num("Cmdline size", hdr->cmdline_size);
633 print_num("Hardware subarch", hdr->hardware_subarch);
634 print_num64("HW subarch data", hdr->hardware_subarch_data);
635 print_num("Payload offset", hdr->payload_offset);
636 print_num("Payload length", hdr->payload_length);
637 print_num64("Setup data", hdr->setup_data);
638 print_num64("Pref address", hdr->pref_address);
639 print_num("Init size", hdr->init_size);
640 print_num("Handover offset", hdr->handover_offset);
641 if (get_boot_protocol(hdr, false) >= 0x215)
642 print_num("Kernel info offset", hdr->kernel_info_offset);
Simon Glassba094bb2021-01-24 10:06:08 -0700643}
Simon Glass14c0fc72023-12-03 17:29:36 -0700644
Simon Glass7b15c572025-03-05 17:25:00 -0700645void zboot_start(struct bootm_info *bmi, ulong bzimage_addr, ulong bzimage_size,
646 ulong initrd_addr, ulong initrd_size, ulong base_addr,
647 const char *cmdline)
Simon Glass14c0fc72023-12-03 17:29:36 -0700648{
Simon Glass7b15c572025-03-05 17:25:00 -0700649 bmi->bzimage_size = bzimage_size;
650 bmi->initrd_addr = initrd_addr;
651 bmi->initrd_size = initrd_size;
Simon Glass14c0fc72023-12-03 17:29:36 -0700652 if (base_addr) {
Simon Glass7b15c572025-03-05 17:25:00 -0700653 bmi->base_ptr = map_sysmem(base_addr, 0);
654 bmi->load_address = bzimage_addr;
Simon Glass14c0fc72023-12-03 17:29:36 -0700655 } else {
Simon Glass7b15c572025-03-05 17:25:00 -0700656 bmi->bzimage_addr = bzimage_addr;
Simon Glass14c0fc72023-12-03 17:29:36 -0700657 }
Simon Glass7b15c572025-03-05 17:25:00 -0700658 bmi->cmdline = cmdline;
Simon Glass14c0fc72023-12-03 17:29:36 -0700659}
660
Simon Glass7b15c572025-03-05 17:25:00 -0700661void zboot_info(struct bootm_info *bmi)
Simon Glass14c0fc72023-12-03 17:29:36 -0700662{
663 printf("Kernel loaded at %08lx, setup_base=%p\n",
Simon Glass7b15c572025-03-05 17:25:00 -0700664 bmi->load_address, bmi->base_ptr);
Simon Glass14c0fc72023-12-03 17:29:36 -0700665}