blob: 73a21bc8f03b650180d0fc42c03e30c0ea41118b [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
Simon Glass2bf90bd2023-12-03 17:29:27 -070058/* Current state of the boot */
Simon Glassbf4af112023-12-03 17:29:26 -070059struct zboot_state state;
Simon Glassd9b3c982020-09-05 14:50:43 -060060
wdenk591dda52002-11-18 00:14:45 +000061static void build_command_line(char *command_line, int auto_boot)
62{
63 char *env_command_line;
wdenk57b2d802003-06-27 21:31:46 +000064
wdenk591dda52002-11-18 00:14:45 +000065 command_line[0] = '\0';
wdenk57b2d802003-06-27 21:31:46 +000066
Simon Glass64b723f2017-08-03 12:22:12 -060067 env_command_line = env_get("bootargs");
wdenk57b2d802003-06-27 21:31:46 +000068
wdenk591dda52002-11-18 00:14:45 +000069 /* set console= argument if we use a serial console */
Graeme Russ883c6032011-11-08 02:33:15 +000070 if (!strstr(env_command_line, "console=")) {
Simon Glass64b723f2017-08-03 12:22:12 -060071 if (!strcmp(env_get("stdout"), "serial")) {
wdenk57b2d802003-06-27 21:31:46 +000072
wdenk591dda52002-11-18 00:14:45 +000073 /* We seem to use serial console */
wdenk57b2d802003-06-27 21:31:46 +000074 sprintf(command_line, "console=ttyS0,%s ",
Simon Glass64b723f2017-08-03 12:22:12 -060075 env_get("baudrate"));
wdenk591dda52002-11-18 00:14:45 +000076 }
77 }
wdenk57b2d802003-06-27 21:31:46 +000078
Graeme Russ883c6032011-11-08 02:33:15 +000079 if (auto_boot)
wdenk591dda52002-11-18 00:14:45 +000080 strcat(command_line, "auto ");
wdenk57b2d802003-06-27 21:31:46 +000081
Graeme Russ883c6032011-11-08 02:33:15 +000082 if (env_command_line)
wdenk591dda52002-11-18 00:14:45 +000083 strcat(command_line, env_command_line);
Simon Glasseeb01222021-01-24 10:06:09 -070084#ifdef DEBUG
85 printf("Kernel command line:");
86 puts(command_line);
87 printf("\n");
88#endif
wdenk591dda52002-11-18 00:14:45 +000089}
90
Gabe Black540c2622011-12-05 12:09:26 +000091static int kernel_magic_ok(struct setup_header *hdr)
wdenk591dda52002-11-18 00:14:45 +000092{
Graeme Russ1bab1042010-04-24 00:05:49 +100093 if (KERNEL_MAGIC != hdr->boot_flag) {
Gabe Black1cf51212011-12-05 12:09:23 +000094 printf("Error: Invalid Boot Flag "
95 "(found 0x%04x, expected 0x%04x)\n",
96 hdr->boot_flag, KERNEL_MAGIC);
wdenk591dda52002-11-18 00:14:45 +000097 return 0;
Graeme Russ1bab1042010-04-24 00:05:49 +100098 } else {
99 printf("Valid Boot Flag\n");
Gabe Black540c2622011-12-05 12:09:26 +0000100 return 1;
wdenk591dda52002-11-18 00:14:45 +0000101 }
Gabe Black540c2622011-12-05 12:09:26 +0000102}
wdenk57b2d802003-06-27 21:31:46 +0000103
Simon Glassdf30d412020-09-05 14:50:40 -0600104static int get_boot_protocol(struct setup_header *hdr, bool verbose)
Gabe Black540c2622011-12-05 12:09:26 +0000105{
106 if (hdr->header == KERNEL_V2_MAGIC) {
Simon Glassdf30d412020-09-05 14:50:40 -0600107 if (verbose)
108 printf("Magic signature found\n");
Gabe Black540c2622011-12-05 12:09:26 +0000109 return hdr->version;
wdenk591dda52002-11-18 00:14:45 +0000110 } else {
111 /* Very old kernel */
Simon Glassdf30d412020-09-05 14:50:40 -0600112 if (verbose)
113 printf("Magic signature not found\n");
Gabe Black540c2622011-12-05 12:09:26 +0000114 return 0x0100;
wdenk591dda52002-11-18 00:14:45 +0000115 }
Gabe Black540c2622011-12-05 12:09:26 +0000116}
117
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700118static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob)
119{
Simon Glassdf30d412020-09-05 14:50:40 -0600120 int bootproto = get_boot_protocol(hdr, false);
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700121 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 Glass531efde2023-07-12 09:04:44 -0600150const char *zimage_get_kernel_version(struct boot_params *params,
Simon Glassdf30d412020-09-05 14:50:40 -0600151 void *kernel_base)
152{
153 struct setup_header *hdr = &params->hdr;
154 int bootproto;
Simon Glass8ee28542020-11-04 09:59:14 -0700155 const char *s, *end;
Simon Glassdf30d412020-09-05 14:50:40 -0600156
157 bootproto = get_boot_protocol(hdr, false);
Simon Glass531efde2023-07-12 09:04:44 -0600158 log_debug("bootproto %x, hdr->setup_sects %x\n", bootproto,
159 hdr->setup_sects);
Simon Glassdf30d412020-09-05 14:50:40 -0600160 if (bootproto < 0x0200 || hdr->setup_sects < 15)
161 return NULL;
162
Simon Glass8ee28542020-11-04 09:59:14 -0700163 /* sanity-check the kernel version in case it is missing */
Simon Glass531efde2023-07-12 09:04:44 -0600164 log_debug("hdr->kernel_version %x, str at %p\n", hdr->kernel_version,
165 kernel_base + hdr->kernel_version + 0x200);
Simon Glass8ee28542020-11-04 09:59:14 -0700166 for (s = kernel_base + hdr->kernel_version + 0x200, end = s + 0x100; *s;
167 s++) {
168 if (!isprint(*s))
169 return NULL;
170 }
171
Simon Glassdf30d412020-09-05 14:50:40 -0600172 return kernel_base + hdr->kernel_version + 0x200;
173}
174
Gabe Black540c2622011-12-05 12:09:26 +0000175struct boot_params *load_zimage(char *image, unsigned long kernel_size,
Simon Glassd98a5bd2014-10-10 08:21:56 -0600176 ulong *load_addressp)
Gabe Black540c2622011-12-05 12:09:26 +0000177{
178 struct boot_params *setup_base;
Simon Glassdf30d412020-09-05 14:50:40 -0600179 const char *version;
Gabe Black540c2622011-12-05 12:09:26 +0000180 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 = &params->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;
wdenk57b2d802003-06-27 21:31:46 +0000192
wdenk591dda52002-11-18 00:14:45 +0000193 /* determine size of setup */
Graeme Russ1bab1042010-04-24 00:05:49 +1000194 if (0 == hdr->setup_sects) {
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700195 log_warning("Setup Sectors = 0 (defaulting to 4)\n");
wdenk591dda52002-11-18 00:14:45 +0000196 setup_size = 5 * 512;
197 } else {
Graeme Russ1bab1042010-04-24 00:05:49 +1000198 setup_size = (hdr->setup_sects + 1) * 512;
wdenk591dda52002-11-18 00:14:45 +0000199 }
wdenk57b2d802003-06-27 21:31:46 +0000200
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700201 log_debug("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
Graeme Russ1bab1042010-04-24 00:05:49 +1000202
Graeme Russ883c6032011-11-08 02:33:15 +0000203 if (setup_size > SETUP_MAX_SIZE)
wdenk591dda52002-11-18 00:14:45 +0000204 printf("Error: Setup is too large (%d bytes)\n", setup_size);
wdenk57b2d802003-06-27 21:31:46 +0000205
Gabe Black540c2622011-12-05 12:09:26 +0000206 /* determine boot protocol version */
Simon Glassdf30d412020-09-05 14:50:40 -0600207 bootproto = get_boot_protocol(hdr, true);
Gabe Black540c2622011-12-05 12:09:26 +0000208
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700209 log_debug("Using boot protocol version %x.%02x\n",
210 (bootproto & 0xff00) >> 8, bootproto & 0xff);
Gabe Black540c2622011-12-05 12:09:26 +0000211
Simon Glass531efde2023-07-12 09:04:44 -0600212 version = zimage_get_kernel_version(params, image);
Simon Glassdf30d412020-09-05 14:50:40 -0600213 if (version)
214 printf("Linux kernel version %s\n", version);
215 else
216 printf("Setup Sectors < 15 - Cannot print kernel version\n");
Gabe Black540c2622011-12-05 12:09:26 +0000217
wdenk591dda52002-11-18 00:14:45 +0000218 /* Determine image type */
Graeme Russ883c6032011-11-08 02:33:15 +0000219 big_image = (bootproto >= 0x0200) &&
220 (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk57b2d802003-06-27 21:31:46 +0000221
Graeme Russ1bab1042010-04-24 00:05:49 +1000222 /* Determine load address */
Gabe Black1cf51212011-12-05 12:09:23 +0000223 if (big_image)
Simon Glassd98a5bd2014-10-10 08:21:56 -0600224 *load_addressp = BZIMAGE_LOAD_ADDR;
Gabe Black1cf51212011-12-05 12:09:23 +0000225 else
Simon Glassd98a5bd2014-10-10 08:21:56 -0600226 *load_addressp = ZIMAGE_LOAD_ADDR;
wdenk57b2d802003-06-27 21:31:46 +0000227
Gabe Blackc67a9482011-12-05 12:09:24 +0000228 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;
wdenk57b2d802003-06-27 21:31:46 +0000231
Gabe Black540c2622011-12-05 12:09:26 +0000232 if (bootproto >= 0x0204)
233 kernel_size = hdr->syssize * 16;
234 else
235 kernel_size -= setup_size;
wdenk57b2d802003-06-27 21:31:46 +0000236
wdenk57b2d802003-06-27 21:31:46 +0000237 if (bootproto == 0x0100) {
Graeme Russ883c6032011-11-08 02:33:15 +0000238 /*
239 * A very old kernel MUST have its real-mode code
240 * loaded at 0x90000
241 */
Simon Glassca37a392017-01-16 07:03:35 -0700242 if ((ulong)setup_base != 0x90000) {
wdenk591dda52002-11-18 00:14:45 +0000243 /* Copy the real-mode kernel */
Graeme Russ883c6032011-11-08 02:33:15 +0000244 memmove((void *)0x90000, setup_base, setup_size);
245
wdenk591dda52002-11-18 00:14:45 +0000246 /* Copy the command line */
Graeme Russ883c6032011-11-08 02:33:15 +0000247 memmove((void *)0x99000,
Gabe Black1cf51212011-12-05 12:09:23 +0000248 (u8 *)setup_base + COMMAND_LINE_OFFSET,
Graeme Russ883c6032011-11-08 02:33:15 +0000249 COMMAND_LINE_SIZE);
wdenk57b2d802003-06-27 21:31:46 +0000250
Graeme Russ883c6032011-11-08 02:33:15 +0000251 /* Relocated */
Gabe Black1cf51212011-12-05 12:09:23 +0000252 setup_base = (struct boot_params *)0x90000;
wdenk591dda52002-11-18 00:14:45 +0000253 }
wdenk57b2d802003-06-27 21:31:46 +0000254
wdenk591dda52002-11-18 00:14:45 +0000255 /* It is recommended to clear memory up to the 32K mark */
Gabe Black1cf51212011-12-05 12:09:23 +0000256 memset((u8 *)0x90000 + setup_size, 0,
257 SETUP_MAX_SIZE - setup_size);
wdenk591dda52002-11-18 00:14:45 +0000258 }
wdenk57b2d802003-06-27 21:31:46 +0000259
Gabe Black540c2622011-12-05 12:09:26 +0000260 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 Glassd98a5bd2014-10-10 08:21:56 -0600273 printf("Loading %s at address %lx (%ld bytes)\n",
274 big_image ? "bzImage" : "zImage", *load_addressp, kernel_size);
Gabe Black540c2622011-12-05 12:09:26 +0000275
Simon Glassd98a5bd2014-10-10 08:21:56 -0600276 memmove((void *)*load_addressp, image + setup_size, kernel_size);
Gabe Black540c2622011-12-05 12:09:26 +0000277
278 return setup_base;
279}
280
281int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
Simon Glass12d93ab2020-09-05 14:50:51 -0600282 ulong initrd_addr, ulong initrd_size, ulong cmdline_force)
Gabe Black540c2622011-12-05 12:09:26 +0000283{
284 struct setup_header *hdr = &setup_base->hdr;
Simon Glassdf30d412020-09-05 14:50:40 -0600285 int bootproto = get_boot_protocol(hdr, false);
Gabe Black540c2622011-12-05 12:09:26 +0000286
Simon Glass842ff442020-11-04 09:59:13 -0700287 log_debug("Setup E820 entries\n");
Simon Glass0de9c372021-07-10 21:15:21 -0600288 if (IS_ENABLED(CONFIG_COREBOOT_SYSINFO)) {
Simon Glass02c51202021-03-15 18:00:23 +1300289 setup_base->e820_entries = cb_install_e820_map(
290 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Simon Glass0de9c372021-07-10 21:15:21 -0600291 } else {
292 setup_base->e820_entries = install_e820_map(
293 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Simon Glass02c51202021-03-15 18:00:23 +1300294 }
Graeme Russ1bab1042010-04-24 00:05:49 +1000295
Gabe Black540c2622011-12-05 12:09:26 +0000296 if (bootproto == 0x0100) {
297 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
298 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
299 }
wdenk591dda52002-11-18 00:14:45 +0000300 if (bootproto >= 0x0200) {
Simon Glassd67ff032020-09-05 14:50:41 -0600301 hdr->type_of_loader = 0x80; /* U-Boot version 0 */
wdenk591dda52002-11-18 00:14:45 +0000302 if (initrd_addr) {
Gabe Black1cf51212011-12-05 12:09:23 +0000303 printf("Initial RAM disk at linear address "
304 "0x%08lx, size %ld bytes\n",
wdenk591dda52002-11-18 00:14:45 +0000305 initrd_addr, initrd_size);
wdenk57b2d802003-06-27 21:31:46 +0000306
Graeme Russ1bab1042010-04-24 00:05:49 +1000307 hdr->ramdisk_image = initrd_addr;
308 hdr->ramdisk_size = initrd_size;
wdenk591dda52002-11-18 00:14:45 +0000309 }
310 }
wdenk57b2d802003-06-27 21:31:46 +0000311
wdenk591dda52002-11-18 00:14:45 +0000312 if (bootproto >= 0x0201) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000313 hdr->heap_end_ptr = HEAP_END_OFFSET;
314 hdr->loadflags |= HEAP_FLAG;
wdenk591dda52002-11-18 00:14:45 +0000315 }
wdenk57b2d802003-06-27 21:31:46 +0000316
Simon Glass4b4f2732014-10-19 21:11:21 -0600317 if (cmd_line) {
Simon Glass7b48ce82020-11-05 10:33:46 -0700318 int max_size = 0xff;
319 int ret;
320
Simon Glass842ff442020-11-04 09:59:13 -0700321 log_debug("Setup cmdline\n");
Simon Glass7b48ce82020-11-05 10:33:46 -0700322 if (bootproto >= 0x0206)
323 max_size = hdr->cmdline_size;
Simon Glass4b4f2732014-10-19 21:11:21 -0600324 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 Russ1bab1042010-04-24 00:05:49 +1000330
Simon Glass4b4f2732014-10-19 21:11:21 -0600331 hdr->setup_move_size = 0x9100;
332 }
333
334 /* build command line at COMMAND_LINE_OFFSET */
Simon Glass12d93ab2020-09-05 14:50:51 -0600335 if (cmdline_force)
336 strcpy(cmd_line, (char *)cmdline_force);
337 else
338 build_command_line(cmd_line, auto_boot);
Simon Glasse7506982021-12-29 11:57:39 -0700339 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 Glass7b48ce82020-11-05 10:33:46 -0700347 }
348 printf("Kernel command line: \"");
349 puts(cmd_line);
350 printf("\"\n");
wdenk591dda52002-11-18 00:14:45 +0000351 }
wdenk591dda52002-11-18 00:14:45 +0000352
Simon Glass3a177172020-09-05 14:50:39 -0600353 if (IS_ENABLED(CONFIG_INTEL_MID) && bootproto >= 0x0207)
Andy Shevchenko8b7adf82018-01-10 19:40:14 +0200354 hdr->hardware_subarch = X86_SUBARCH_INTEL_MID;
Andy Shevchenko8b7adf82018-01-10 19:40:14 +0200355
Simon Glass3a177172020-09-05 14:50:39 -0600356 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
357 setup_base->acpi_rsdp_addr = acpi_get_rsdp_addr();
Andy Shevchenkod87a21d2019-09-13 18:42:00 +0300358
Simon Glass842ff442020-11-04 09:59:13 -0700359 log_debug("Setup devicetree\n");
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700360 setup_device_tree(hdr, (const void *)env_get_hex("fdtaddr", 0));
Bin Mengf6d504f2015-07-06 16:31:36 +0800361 setup_video(&setup_base->screen_info);
362
Simon Glass3a177172020-09-05 14:50:39 -0600363 if (IS_ENABLED(CONFIG_EFI_STUB))
364 setup_efi_info(&setup_base->efi_info);
Bin Meng106dcfc2018-08-23 08:24:10 -0700365
Gabe Black540c2622011-12-05 12:09:26 +0000366 return 0;
wdenk591dda52002-11-18 00:14:45 +0000367}
368
Simon Glass2bf90bd2023-12-03 17:29:27 -0700369int zboot_load(void)
Simon Glass24d26f72020-09-05 14:50:46 -0600370{
371 struct boot_params *base_ptr;
Simon Glassf6d7ebd2023-12-03 17:29:31 -0700372 int ret;
Simon Glass24d26f72020-09-05 14:50:46 -0600373
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
Simon Glassf6d7ebd2023-12-03 17:29:31 -0700392 ret = env_set_hex("zbootbase", map_to_sysmem(state.base_ptr));
393 if (!ret)
394 ret = env_set_hex("zbootaddr", state.load_address);
395 if (ret)
396 return ret;
397
Simon Glass84305a12023-07-12 09:04:43 -0600398 return 0;
399}
400
Simon Glass2bf90bd2023-12-03 17:29:27 -0700401int zboot_setup(void)
Simon Glass84305a12023-07-12 09:04:43 -0600402{
403 struct boot_params *base_ptr = state.base_ptr;
404 int ret;
405
406 ret = setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
407 0, state.initrd_addr, state.initrd_size,
408 (ulong)state.cmdline);
409 if (ret)
410 return -EINVAL;
411
412 return 0;
413}
414
Simon Glass2bf90bd2023-12-03 17:29:27 -0700415int zboot_go(void)
Simon Glass1931b192020-09-05 14:50:44 -0600416{
Simon Glass6d496322023-03-20 08:30:08 +1300417 struct boot_params *params = state.base_ptr;
418 struct setup_header *hdr = &params->hdr;
419 bool image_64bit;
420 ulong entry;
Simon Glass1931b192020-09-05 14:50:44 -0600421 int ret;
422
Simon Glass0db800c2020-09-05 14:50:42 -0600423 disable_interrupts();
Simon Glass1931b192020-09-05 14:50:44 -0600424
Simon Glass6d496322023-03-20 08:30:08 +1300425 entry = state.load_address;
426 image_64bit = false;
427 if (IS_ENABLED(CONFIG_X86_RUN_64BIT) &&
428 (hdr->xloadflags & XLF_KERNEL_64)) {
429 entry += 0x200;
430 image_64bit = true;
431 }
432
Gabe Black540c2622011-12-05 12:09:26 +0000433 /* we assume that the kernel is in place */
Simon Glass6d496322023-03-20 08:30:08 +1300434 ret = boot_linux_kernel((ulong)state.base_ptr, entry, image_64bit);
Simon Glass84305a12023-07-12 09:04:43 -0600435
436 return ret;
437}
438
Simon Glass365d17b2023-12-03 17:29:35 -0700439int zboot_run(ulong addr, ulong size, ulong initrd, ulong initrd_size,
440 ulong base, char *cmdline)
Simon Glass84305a12023-07-12 09:04:43 -0600441{
442 int ret;
443
Simon Glass10880b32023-12-03 17:29:37 -0700444 zboot_start(addr, size, initrd, initrd_size, base, cmdline);
Simon Glass84305a12023-07-12 09:04:43 -0600445 ret = zboot_load();
446 if (ret)
447 return log_msg_ret("ld", ret);
448 ret = zboot_setup();
449 if (ret)
450 return log_msg_ret("set", ret);
451 ret = zboot_go();
452 if (ret)
Simon Glass365d17b2023-12-03 17:29:35 -0700453 return log_msg_ret("go", ret);
Simon Glass84305a12023-07-12 09:04:43 -0600454
455 return -EFAULT;
456}
457
Simon Glassaad0d762020-09-05 14:50:50 -0600458static void print_num(const char *name, ulong value)
459{
460 printf("%-20s: %lx\n", name, value);
461}
462
463static void print_num64(const char *name, u64 value)
464{
465 printf("%-20s: %llx\n", name, value);
466}
467
468static const char *const e820_type_name[E820_COUNT] = {
469 [E820_RAM] = "RAM",
470 [E820_RESERVED] = "Reserved",
471 [E820_ACPI] = "ACPI",
472 [E820_NVS] = "ACPI NVS",
473 [E820_UNUSABLE] = "Unusable",
474};
475
476static const char *const bootloader_id[] = {
477 "LILO",
478 "Loadlin",
479 "bootsect-loader",
480 "Syslinux",
481 "Etherboot/gPXE/iPXE",
482 "ELILO",
483 "undefined",
484 "GRUB",
485 "U-Boot",
486 "Xen",
487 "Gujin",
488 "Qemu",
489 "Arcturus Networks uCbootloader",
490 "kexec-tools",
491 "Extended",
492 "Special",
493 "Reserved",
494 "Minimal Linux Bootloader",
495 "OVMF UEFI virtualization stack",
496};
497
498struct flag_info {
499 uint bit;
500 const char *name;
501};
502
503static struct flag_info load_flags[] = {
504 { LOADED_HIGH, "loaded-high" },
505 { QUIET_FLAG, "quiet" },
506 { KEEP_SEGMENTS, "keep-segments" },
507 { CAN_USE_HEAP, "can-use-heap" },
508};
509
510static struct flag_info xload_flags[] = {
511 { XLF_KERNEL_64, "64-bit-entry" },
512 { XLF_CAN_BE_LOADED_ABOVE_4G, "can-load-above-4gb" },
513 { XLF_EFI_HANDOVER_32, "32-efi-handoff" },
514 { XLF_EFI_HANDOVER_64, "64-efi-handoff" },
515 { XLF_EFI_KEXEC, "kexec-efi-runtime" },
516};
517
518static void print_flags(struct flag_info *flags, int count, uint value)
519{
520 int i;
521
522 printf("%-20s:", "");
523 for (i = 0; i < count; i++) {
524 uint mask = flags[i].bit;
525
526 if (value & mask)
527 printf(" %s", flags[i].name);
528 }
529 printf("\n");
530}
531
532static void show_loader(struct setup_header *hdr)
533{
534 bool version_valid = false;
535 int type, version;
536 const char *name;
537
538 type = hdr->type_of_loader >> 4;
539 version = hdr->type_of_loader & 0xf;
540 if (type == 0xe)
541 type = 0x10 + hdr->ext_loader_type;
542 version |= hdr->ext_loader_ver << 4;
543 if (!hdr->type_of_loader) {
544 name = "pre-2.00 bootloader";
545 } else if (hdr->type_of_loader == 0xff) {
546 name = "unknown";
547 } else if (type < ARRAY_SIZE(bootloader_id)) {
548 name = bootloader_id[type];
549 version_valid = true;
550 } else {
551 name = "undefined";
552 }
553 printf("%20s %s", "", name);
554 if (version_valid)
555 printf(", version %x", version);
556 printf("\n");
557}
558
Simon Glass5495aaf2023-07-30 11:17:00 -0600559void zimage_dump(struct boot_params *base_ptr, bool show_cmdline)
Simon Glassaad0d762020-09-05 14:50:50 -0600560{
Simon Glassaad0d762020-09-05 14:50:50 -0600561 struct setup_header *hdr;
562 const char *version;
563 int i;
564
Simon Glassaad0d762020-09-05 14:50:50 -0600565 printf("Setup located at %p:\n\n", base_ptr);
566 print_num64("ACPI RSDP addr", base_ptr->acpi_rsdp_addr);
567
568 printf("E820: %d entries\n", base_ptr->e820_entries);
569 if (base_ptr->e820_entries) {
Simon Glass5495aaf2023-07-30 11:17:00 -0600570 printf("%12s %10s %s\n", "Addr", "Size", "Type");
Simon Glassaad0d762020-09-05 14:50:50 -0600571 for (i = 0; i < base_ptr->e820_entries; i++) {
572 struct e820_entry *entry = &base_ptr->e820_map[i];
573
574 printf("%12llx %10llx %s\n", entry->addr, entry->size,
575 entry->type < E820_COUNT ?
576 e820_type_name[entry->type] :
577 simple_itoa(entry->type));
578 }
579 }
580
581 hdr = &base_ptr->hdr;
582 print_num("Setup sectors", hdr->setup_sects);
583 print_num("Root flags", hdr->root_flags);
584 print_num("Sys size", hdr->syssize);
585 print_num("RAM size", hdr->ram_size);
586 print_num("Video mode", hdr->vid_mode);
587 print_num("Root dev", hdr->root_dev);
588 print_num("Boot flag", hdr->boot_flag);
589 print_num("Jump", hdr->jump);
590 print_num("Header", hdr->header);
591 if (hdr->header == KERNEL_V2_MAGIC)
592 printf("%-20s %s\n", "", "Kernel V2");
593 else
594 printf("%-20s %s\n", "", "Ancient kernel, using version 100");
595 print_num("Version", hdr->version);
596 print_num("Real mode switch", hdr->realmode_swtch);
Simon Glass83ec1ca2023-03-20 08:30:06 +1300597 print_num("Start sys seg", hdr->start_sys_seg);
Simon Glassaad0d762020-09-05 14:50:50 -0600598 print_num("Kernel version", hdr->kernel_version);
Simon Glass531efde2023-07-12 09:04:44 -0600599 version = zimage_get_kernel_version(base_ptr,
600 (void *)state.bzimage_addr);
Simon Glassaad0d762020-09-05 14:50:50 -0600601 if (version)
602 printf(" @%p: %s\n", version, version);
603 print_num("Type of loader", hdr->type_of_loader);
604 show_loader(hdr);
605 print_num("Load flags", hdr->loadflags);
606 print_flags(load_flags, ARRAY_SIZE(load_flags), hdr->loadflags);
607 print_num("Setup move size", hdr->setup_move_size);
608 print_num("Code32 start", hdr->code32_start);
609 print_num("Ramdisk image", hdr->ramdisk_image);
610 print_num("Ramdisk size", hdr->ramdisk_size);
611 print_num("Bootsect kludge", hdr->bootsect_kludge);
612 print_num("Heap end ptr", hdr->heap_end_ptr);
613 print_num("Ext loader ver", hdr->ext_loader_ver);
614 print_num("Ext loader type", hdr->ext_loader_type);
615 print_num("Command line ptr", hdr->cmd_line_ptr);
Simon Glass5495aaf2023-07-30 11:17:00 -0600616 if (show_cmdline && hdr->cmd_line_ptr) {
Simon Glassaad0d762020-09-05 14:50:50 -0600617 printf(" ");
618 /* Use puts() to avoid limits from CONFIG_SYS_PBSIZE */
Simon Glass12d93ab2020-09-05 14:50:51 -0600619 puts((char *)(ulong)hdr->cmd_line_ptr);
Simon Glassaad0d762020-09-05 14:50:50 -0600620 printf("\n");
621 }
622 print_num("Initrd addr max", hdr->initrd_addr_max);
623 print_num("Kernel alignment", hdr->kernel_alignment);
624 print_num("Relocatable kernel", hdr->relocatable_kernel);
625 print_num("Min alignment", hdr->min_alignment);
626 if (hdr->min_alignment)
627 printf("%-20s: %x\n", "", 1 << hdr->min_alignment);
628 print_num("Xload flags", hdr->xloadflags);
629 print_flags(xload_flags, ARRAY_SIZE(xload_flags), hdr->xloadflags);
630 print_num("Cmdline size", hdr->cmdline_size);
631 print_num("Hardware subarch", hdr->hardware_subarch);
632 print_num64("HW subarch data", hdr->hardware_subarch_data);
633 print_num("Payload offset", hdr->payload_offset);
634 print_num("Payload length", hdr->payload_length);
635 print_num64("Setup data", hdr->setup_data);
636 print_num64("Pref address", hdr->pref_address);
637 print_num("Init size", hdr->init_size);
638 print_num("Handover offset", hdr->handover_offset);
639 if (get_boot_protocol(hdr, false) >= 0x215)
640 print_num("Kernel info offset", hdr->kernel_info_offset);
Simon Glassba094bb2021-01-24 10:06:08 -0700641}
Simon Glass14c0fc72023-12-03 17:29:36 -0700642
643void zboot_start(ulong bzimage_addr, ulong bzimage_size, ulong initrd_addr,
644 ulong initrd_size, ulong base_addr, const char *cmdline)
645{
646 memset(&state, '\0', sizeof(state));
647
648 state.bzimage_size = bzimage_size;
649 state.initrd_addr = initrd_addr;
650 state.initrd_size = initrd_size;
651 if (base_addr) {
652 state.base_ptr = map_sysmem(base_addr, 0);
653 state.load_address = bzimage_addr;
654 } else {
655 state.bzimage_addr = bzimage_addr;
656 }
657 state.cmdline = cmdline;
658}
659
660void zboot_info(void)
661{
662 printf("Kernel loaded at %08lx, setup_base=%p\n",
663 state.load_address, state.base_ptr);
664}