blob: e5ea5129c1e948528ab8842bb952bc11845b4279 [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 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 Glass48800f42020-09-05 14:50:38 -060058/**
59 * struct zboot_state - Current state of the boot
60 *
61 * @bzimage_addr: Address of the bzImage to boot
62 * @bzimage_size: Size of the bzImage, or 0 to detect this
63 * @initrd_addr: Address of the initial ramdisk, or 0 if none
64 * @initrd_size: Size of the initial ramdisk, or 0 if none
65 * @load_address: Address where the bzImage is moved before booting, either
66 * BZIMAGE_LOAD_ADDR or ZIMAGE_LOAD_ADDR
Simon Glass1931b192020-09-05 14:50:44 -060067 * @base_ptr: Pointer to the boot parameters, typically at address
68 * DEFAULT_SETUP_BASE
Simon Glass5e956d12020-11-09 07:12:24 -070069 * @cmdline: Environment variable containing the 'override' command line, or
70 * NULL to use the one in the setup block
Simon Glass48800f42020-09-05 14:50:38 -060071 */
72struct zboot_state {
73 ulong bzimage_addr;
74 ulong bzimage_size;
75 ulong initrd_addr;
76 ulong initrd_size;
77 ulong load_address;
Simon Glass1931b192020-09-05 14:50:44 -060078 struct boot_params *base_ptr;
Simon Glass5e956d12020-11-09 07:12:24 -070079 char *cmdline;
Simon Glass48800f42020-09-05 14:50:38 -060080} state;
81
Simon Glassd9b3c982020-09-05 14:50:43 -060082enum {
83 ZBOOT_STATE_START = BIT(0),
Simon Glass24d26f72020-09-05 14:50:46 -060084 ZBOOT_STATE_LOAD = BIT(1),
Simon Glass30634832020-09-05 14:50:47 -060085 ZBOOT_STATE_SETUP = BIT(2),
86 ZBOOT_STATE_INFO = BIT(3),
87 ZBOOT_STATE_GO = BIT(4),
Simon Glassd9b3c982020-09-05 14:50:43 -060088
Simon Glassaad0d762020-09-05 14:50:50 -060089 /* This one doesn't execute automatically, so stop the count before 5 */
90 ZBOOT_STATE_DUMP = BIT(5),
Simon Glass30634832020-09-05 14:50:47 -060091 ZBOOT_STATE_COUNT = 5,
Simon Glassd9b3c982020-09-05 14:50:43 -060092};
93
wdenk591dda52002-11-18 00:14:45 +000094static void build_command_line(char *command_line, int auto_boot)
95{
96 char *env_command_line;
wdenk57b2d802003-06-27 21:31:46 +000097
wdenk591dda52002-11-18 00:14:45 +000098 command_line[0] = '\0';
wdenk57b2d802003-06-27 21:31:46 +000099
Simon Glass64b723f2017-08-03 12:22:12 -0600100 env_command_line = env_get("bootargs");
wdenk57b2d802003-06-27 21:31:46 +0000101
wdenk591dda52002-11-18 00:14:45 +0000102 /* set console= argument if we use a serial console */
Graeme Russ883c6032011-11-08 02:33:15 +0000103 if (!strstr(env_command_line, "console=")) {
Simon Glass64b723f2017-08-03 12:22:12 -0600104 if (!strcmp(env_get("stdout"), "serial")) {
wdenk57b2d802003-06-27 21:31:46 +0000105
wdenk591dda52002-11-18 00:14:45 +0000106 /* We seem to use serial console */
wdenk57b2d802003-06-27 21:31:46 +0000107 sprintf(command_line, "console=ttyS0,%s ",
Simon Glass64b723f2017-08-03 12:22:12 -0600108 env_get("baudrate"));
wdenk591dda52002-11-18 00:14:45 +0000109 }
110 }
wdenk57b2d802003-06-27 21:31:46 +0000111
Graeme Russ883c6032011-11-08 02:33:15 +0000112 if (auto_boot)
wdenk591dda52002-11-18 00:14:45 +0000113 strcat(command_line, "auto ");
wdenk57b2d802003-06-27 21:31:46 +0000114
Graeme Russ883c6032011-11-08 02:33:15 +0000115 if (env_command_line)
wdenk591dda52002-11-18 00:14:45 +0000116 strcat(command_line, env_command_line);
Simon Glasseeb01222021-01-24 10:06:09 -0700117#ifdef DEBUG
118 printf("Kernel command line:");
119 puts(command_line);
120 printf("\n");
121#endif
wdenk591dda52002-11-18 00:14:45 +0000122}
123
Gabe Black540c2622011-12-05 12:09:26 +0000124static int kernel_magic_ok(struct setup_header *hdr)
wdenk591dda52002-11-18 00:14:45 +0000125{
Graeme Russ1bab1042010-04-24 00:05:49 +1000126 if (KERNEL_MAGIC != hdr->boot_flag) {
Gabe Black1cf51212011-12-05 12:09:23 +0000127 printf("Error: Invalid Boot Flag "
128 "(found 0x%04x, expected 0x%04x)\n",
129 hdr->boot_flag, KERNEL_MAGIC);
wdenk591dda52002-11-18 00:14:45 +0000130 return 0;
Graeme Russ1bab1042010-04-24 00:05:49 +1000131 } else {
132 printf("Valid Boot Flag\n");
Gabe Black540c2622011-12-05 12:09:26 +0000133 return 1;
wdenk591dda52002-11-18 00:14:45 +0000134 }
Gabe Black540c2622011-12-05 12:09:26 +0000135}
wdenk57b2d802003-06-27 21:31:46 +0000136
Simon Glassdf30d412020-09-05 14:50:40 -0600137static int get_boot_protocol(struct setup_header *hdr, bool verbose)
Gabe Black540c2622011-12-05 12:09:26 +0000138{
139 if (hdr->header == KERNEL_V2_MAGIC) {
Simon Glassdf30d412020-09-05 14:50:40 -0600140 if (verbose)
141 printf("Magic signature found\n");
Gabe Black540c2622011-12-05 12:09:26 +0000142 return hdr->version;
wdenk591dda52002-11-18 00:14:45 +0000143 } else {
144 /* Very old kernel */
Simon Glassdf30d412020-09-05 14:50:40 -0600145 if (verbose)
146 printf("Magic signature not found\n");
Gabe Black540c2622011-12-05 12:09:26 +0000147 return 0x0100;
wdenk591dda52002-11-18 00:14:45 +0000148 }
Gabe Black540c2622011-12-05 12:09:26 +0000149}
150
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700151static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob)
152{
Simon Glassdf30d412020-09-05 14:50:40 -0600153 int bootproto = get_boot_protocol(hdr, false);
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700154 struct setup_data *sd;
155 int size;
156
157 if (bootproto < 0x0209)
158 return -ENOTSUPP;
159
160 if (!fdt_blob)
161 return 0;
162
163 size = fdt_totalsize(fdt_blob);
164 if (size < 0)
165 return -EINVAL;
166
167 size += sizeof(struct setup_data);
168 sd = (struct setup_data *)malloc(size);
169 if (!sd) {
170 printf("Not enough memory for DTB setup data\n");
171 return -ENOMEM;
172 }
173
174 sd->next = hdr->setup_data;
175 sd->type = SETUP_DTB;
176 sd->len = fdt_totalsize(fdt_blob);
177 memcpy(sd->data, fdt_blob, sd->len);
178 hdr->setup_data = (unsigned long)sd;
179
180 return 0;
181}
182
Simon Glassdf30d412020-09-05 14:50:40 -0600183static const char *get_kernel_version(struct boot_params *params,
184 void *kernel_base)
185{
186 struct setup_header *hdr = &params->hdr;
187 int bootproto;
Simon Glass8ee28542020-11-04 09:59:14 -0700188 const char *s, *end;
Simon Glassdf30d412020-09-05 14:50:40 -0600189
190 bootproto = get_boot_protocol(hdr, false);
191 if (bootproto < 0x0200 || hdr->setup_sects < 15)
192 return NULL;
193
Simon Glass8ee28542020-11-04 09:59:14 -0700194 /* sanity-check the kernel version in case it is missing */
195 for (s = kernel_base + hdr->kernel_version + 0x200, end = s + 0x100; *s;
196 s++) {
197 if (!isprint(*s))
198 return NULL;
199 }
200
Simon Glassdf30d412020-09-05 14:50:40 -0600201 return kernel_base + hdr->kernel_version + 0x200;
202}
203
Gabe Black540c2622011-12-05 12:09:26 +0000204struct boot_params *load_zimage(char *image, unsigned long kernel_size,
Simon Glassd98a5bd2014-10-10 08:21:56 -0600205 ulong *load_addressp)
Gabe Black540c2622011-12-05 12:09:26 +0000206{
207 struct boot_params *setup_base;
Simon Glassdf30d412020-09-05 14:50:40 -0600208 const char *version;
Gabe Black540c2622011-12-05 12:09:26 +0000209 int setup_size;
210 int bootproto;
211 int big_image;
212
213 struct boot_params *params = (struct boot_params *)image;
214 struct setup_header *hdr = &params->hdr;
215
216 /* base address for real-mode segment */
217 setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
218
219 if (!kernel_magic_ok(hdr))
220 return 0;
wdenk57b2d802003-06-27 21:31:46 +0000221
wdenk591dda52002-11-18 00:14:45 +0000222 /* determine size of setup */
Graeme Russ1bab1042010-04-24 00:05:49 +1000223 if (0 == hdr->setup_sects) {
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700224 log_warning("Setup Sectors = 0 (defaulting to 4)\n");
wdenk591dda52002-11-18 00:14:45 +0000225 setup_size = 5 * 512;
226 } else {
Graeme Russ1bab1042010-04-24 00:05:49 +1000227 setup_size = (hdr->setup_sects + 1) * 512;
wdenk591dda52002-11-18 00:14:45 +0000228 }
wdenk57b2d802003-06-27 21:31:46 +0000229
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700230 log_debug("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
Graeme Russ1bab1042010-04-24 00:05:49 +1000231
Graeme Russ883c6032011-11-08 02:33:15 +0000232 if (setup_size > SETUP_MAX_SIZE)
wdenk591dda52002-11-18 00:14:45 +0000233 printf("Error: Setup is too large (%d bytes)\n", setup_size);
wdenk57b2d802003-06-27 21:31:46 +0000234
Gabe Black540c2622011-12-05 12:09:26 +0000235 /* determine boot protocol version */
Simon Glassdf30d412020-09-05 14:50:40 -0600236 bootproto = get_boot_protocol(hdr, true);
Gabe Black540c2622011-12-05 12:09:26 +0000237
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700238 log_debug("Using boot protocol version %x.%02x\n",
239 (bootproto & 0xff00) >> 8, bootproto & 0xff);
Gabe Black540c2622011-12-05 12:09:26 +0000240
Simon Glassdf30d412020-09-05 14:50:40 -0600241 version = get_kernel_version(params, image);
242 if (version)
243 printf("Linux kernel version %s\n", version);
244 else
245 printf("Setup Sectors < 15 - Cannot print kernel version\n");
Gabe Black540c2622011-12-05 12:09:26 +0000246
wdenk591dda52002-11-18 00:14:45 +0000247 /* Determine image type */
Graeme Russ883c6032011-11-08 02:33:15 +0000248 big_image = (bootproto >= 0x0200) &&
249 (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk57b2d802003-06-27 21:31:46 +0000250
Graeme Russ1bab1042010-04-24 00:05:49 +1000251 /* Determine load address */
Gabe Black1cf51212011-12-05 12:09:23 +0000252 if (big_image)
Simon Glassd98a5bd2014-10-10 08:21:56 -0600253 *load_addressp = BZIMAGE_LOAD_ADDR;
Gabe Black1cf51212011-12-05 12:09:23 +0000254 else
Simon Glassd98a5bd2014-10-10 08:21:56 -0600255 *load_addressp = ZIMAGE_LOAD_ADDR;
wdenk57b2d802003-06-27 21:31:46 +0000256
Gabe Blackc67a9482011-12-05 12:09:24 +0000257 printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
258 memset(setup_base, 0, sizeof(*setup_base));
259 setup_base->hdr = params->hdr;
wdenk57b2d802003-06-27 21:31:46 +0000260
Gabe Black540c2622011-12-05 12:09:26 +0000261 if (bootproto >= 0x0204)
262 kernel_size = hdr->syssize * 16;
263 else
264 kernel_size -= setup_size;
wdenk57b2d802003-06-27 21:31:46 +0000265
wdenk57b2d802003-06-27 21:31:46 +0000266 if (bootproto == 0x0100) {
Graeme Russ883c6032011-11-08 02:33:15 +0000267 /*
268 * A very old kernel MUST have its real-mode code
269 * loaded at 0x90000
270 */
Simon Glassca37a392017-01-16 07:03:35 -0700271 if ((ulong)setup_base != 0x90000) {
wdenk591dda52002-11-18 00:14:45 +0000272 /* Copy the real-mode kernel */
Graeme Russ883c6032011-11-08 02:33:15 +0000273 memmove((void *)0x90000, setup_base, setup_size);
274
wdenk591dda52002-11-18 00:14:45 +0000275 /* Copy the command line */
Graeme Russ883c6032011-11-08 02:33:15 +0000276 memmove((void *)0x99000,
Gabe Black1cf51212011-12-05 12:09:23 +0000277 (u8 *)setup_base + COMMAND_LINE_OFFSET,
Graeme Russ883c6032011-11-08 02:33:15 +0000278 COMMAND_LINE_SIZE);
wdenk57b2d802003-06-27 21:31:46 +0000279
Graeme Russ883c6032011-11-08 02:33:15 +0000280 /* Relocated */
Gabe Black1cf51212011-12-05 12:09:23 +0000281 setup_base = (struct boot_params *)0x90000;
wdenk591dda52002-11-18 00:14:45 +0000282 }
wdenk57b2d802003-06-27 21:31:46 +0000283
wdenk591dda52002-11-18 00:14:45 +0000284 /* It is recommended to clear memory up to the 32K mark */
Gabe Black1cf51212011-12-05 12:09:23 +0000285 memset((u8 *)0x90000 + setup_size, 0,
286 SETUP_MAX_SIZE - setup_size);
wdenk591dda52002-11-18 00:14:45 +0000287 }
wdenk57b2d802003-06-27 21:31:46 +0000288
Gabe Black540c2622011-12-05 12:09:26 +0000289 if (big_image) {
290 if (kernel_size > BZIMAGE_MAX_SIZE) {
291 printf("Error: bzImage kernel too big! "
292 "(size: %ld, max: %d)\n",
293 kernel_size, BZIMAGE_MAX_SIZE);
294 return 0;
295 }
296 } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
297 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
298 kernel_size, ZIMAGE_MAX_SIZE);
299 return 0;
300 }
301
Simon Glassd98a5bd2014-10-10 08:21:56 -0600302 printf("Loading %s at address %lx (%ld bytes)\n",
303 big_image ? "bzImage" : "zImage", *load_addressp, kernel_size);
Gabe Black540c2622011-12-05 12:09:26 +0000304
Simon Glassd98a5bd2014-10-10 08:21:56 -0600305 memmove((void *)*load_addressp, image + setup_size, kernel_size);
Gabe Black540c2622011-12-05 12:09:26 +0000306
307 return setup_base;
308}
309
310int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
Simon Glass12d93ab2020-09-05 14:50:51 -0600311 ulong initrd_addr, ulong initrd_size, ulong cmdline_force)
Gabe Black540c2622011-12-05 12:09:26 +0000312{
313 struct setup_header *hdr = &setup_base->hdr;
Simon Glassdf30d412020-09-05 14:50:40 -0600314 int bootproto = get_boot_protocol(hdr, false);
Gabe Black540c2622011-12-05 12:09:26 +0000315
Simon Glass842ff442020-11-04 09:59:13 -0700316 log_debug("Setup E820 entries\n");
Simon Glass0de9c372021-07-10 21:15:21 -0600317 if (IS_ENABLED(CONFIG_COREBOOT_SYSINFO)) {
Simon Glass02c51202021-03-15 18:00:23 +1300318 setup_base->e820_entries = cb_install_e820_map(
319 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Simon Glass0de9c372021-07-10 21:15:21 -0600320 } else {
321 setup_base->e820_entries = install_e820_map(
322 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Simon Glass02c51202021-03-15 18:00:23 +1300323 }
Graeme Russ1bab1042010-04-24 00:05:49 +1000324
Gabe Black540c2622011-12-05 12:09:26 +0000325 if (bootproto == 0x0100) {
326 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
327 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
328 }
wdenk591dda52002-11-18 00:14:45 +0000329 if (bootproto >= 0x0200) {
Simon Glassd67ff032020-09-05 14:50:41 -0600330 hdr->type_of_loader = 0x80; /* U-Boot version 0 */
wdenk591dda52002-11-18 00:14:45 +0000331 if (initrd_addr) {
Gabe Black1cf51212011-12-05 12:09:23 +0000332 printf("Initial RAM disk at linear address "
333 "0x%08lx, size %ld bytes\n",
wdenk591dda52002-11-18 00:14:45 +0000334 initrd_addr, initrd_size);
wdenk57b2d802003-06-27 21:31:46 +0000335
Graeme Russ1bab1042010-04-24 00:05:49 +1000336 hdr->ramdisk_image = initrd_addr;
337 hdr->ramdisk_size = initrd_size;
wdenk591dda52002-11-18 00:14:45 +0000338 }
339 }
wdenk57b2d802003-06-27 21:31:46 +0000340
wdenk591dda52002-11-18 00:14:45 +0000341 if (bootproto >= 0x0201) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000342 hdr->heap_end_ptr = HEAP_END_OFFSET;
343 hdr->loadflags |= HEAP_FLAG;
wdenk591dda52002-11-18 00:14:45 +0000344 }
wdenk57b2d802003-06-27 21:31:46 +0000345
Simon Glass4b4f2732014-10-19 21:11:21 -0600346 if (cmd_line) {
Simon Glass7b48ce82020-11-05 10:33:46 -0700347 int max_size = 0xff;
348 int ret;
349
Simon Glass842ff442020-11-04 09:59:13 -0700350 log_debug("Setup cmdline\n");
Simon Glass7b48ce82020-11-05 10:33:46 -0700351 if (bootproto >= 0x0206)
352 max_size = hdr->cmdline_size;
Simon Glass4b4f2732014-10-19 21:11:21 -0600353 if (bootproto >= 0x0202) {
354 hdr->cmd_line_ptr = (uintptr_t)cmd_line;
355 } else if (bootproto >= 0x0200) {
356 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
357 setup_base->screen_info.cl_offset =
358 (uintptr_t)cmd_line - (uintptr_t)setup_base;
Graeme Russ1bab1042010-04-24 00:05:49 +1000359
Simon Glass4b4f2732014-10-19 21:11:21 -0600360 hdr->setup_move_size = 0x9100;
361 }
362
363 /* build command line at COMMAND_LINE_OFFSET */
Simon Glass12d93ab2020-09-05 14:50:51 -0600364 if (cmdline_force)
365 strcpy(cmd_line, (char *)cmdline_force);
366 else
367 build_command_line(cmd_line, auto_boot);
Simon Glasse7506982021-12-29 11:57:39 -0700368 if (IS_ENABLED(CONFIG_CMD_BOOTM)) {
369 ret = bootm_process_cmdline(cmd_line, max_size,
370 BOOTM_CL_ALL);
371 if (ret) {
372 printf("Cmdline setup failed (max_size=%x, bootproto=%x, err=%d)\n",
373 max_size, bootproto, ret);
374 return ret;
375 }
Simon Glass7b48ce82020-11-05 10:33:46 -0700376 }
377 printf("Kernel command line: \"");
378 puts(cmd_line);
379 printf("\"\n");
wdenk591dda52002-11-18 00:14:45 +0000380 }
wdenk591dda52002-11-18 00:14:45 +0000381
Simon Glass3a177172020-09-05 14:50:39 -0600382 if (IS_ENABLED(CONFIG_INTEL_MID) && bootproto >= 0x0207)
Andy Shevchenko8b7adf82018-01-10 19:40:14 +0200383 hdr->hardware_subarch = X86_SUBARCH_INTEL_MID;
Andy Shevchenko8b7adf82018-01-10 19:40:14 +0200384
Simon Glass3a177172020-09-05 14:50:39 -0600385 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
386 setup_base->acpi_rsdp_addr = acpi_get_rsdp_addr();
Andy Shevchenkod87a21d2019-09-13 18:42:00 +0300387
Simon Glass842ff442020-11-04 09:59:13 -0700388 log_debug("Setup devicetree\n");
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700389 setup_device_tree(hdr, (const void *)env_get_hex("fdtaddr", 0));
Bin Mengf6d504f2015-07-06 16:31:36 +0800390 setup_video(&setup_base->screen_info);
391
Simon Glass3a177172020-09-05 14:50:39 -0600392 if (IS_ENABLED(CONFIG_EFI_STUB))
393 setup_efi_info(&setup_base->efi_info);
Bin Meng106dcfc2018-08-23 08:24:10 -0700394
Gabe Black540c2622011-12-05 12:09:26 +0000395 return 0;
wdenk591dda52002-11-18 00:14:45 +0000396}
397
Simon Glassd9b3c982020-09-05 14:50:43 -0600398static int do_zboot_start(struct cmd_tbl *cmdtp, int flag, int argc,
399 char *const argv[])
Graeme Russ1bab1042010-04-24 00:05:49 +1000400{
Simon Glassd9b3c982020-09-05 14:50:43 -0600401 const char *s;
Graeme Russ1bab1042010-04-24 00:05:49 +1000402
Simon Glass48800f42020-09-05 14:50:38 -0600403 memset(&state, '\0', sizeof(state));
Gabe Black1cf51212011-12-05 12:09:23 +0000404 if (argc >= 2) {
Graeme Russ3134be22010-10-07 20:03:19 +1100405 /* argv[1] holds the address of the bzImage */
406 s = argv[1];
Gabe Black1cf51212011-12-05 12:09:23 +0000407 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600408 s = env_get("fileaddr");
Gabe Black1cf51212011-12-05 12:09:23 +0000409 }
Graeme Russ1bab1042010-04-24 00:05:49 +1000410
Graeme Russ3134be22010-10-07 20:03:19 +1100411 if (s)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600412 state.bzimage_addr = hextoul(s, NULL);
Graeme Russ3134be22010-10-07 20:03:19 +1100413
Gabe Black69b49ee2011-12-05 12:09:27 +0000414 if (argc >= 3) {
Graeme Russ3134be22010-10-07 20:03:19 +1100415 /* argv[2] holds the size of the bzImage */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600416 state.bzimage_size = hextoul(argv[2], NULL);
Gabe Black69b49ee2011-12-05 12:09:27 +0000417 }
418
419 if (argc >= 4)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600420 state.initrd_addr = hextoul(argv[3], NULL);
Gabe Black69b49ee2011-12-05 12:09:27 +0000421 if (argc >= 5)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600422 state.initrd_size = hextoul(argv[4], NULL);
Simon Glassc55ce682020-09-05 14:50:49 -0600423 if (argc >= 6) {
424 /*
425 * When the base_ptr is passed in, we assume that the image is
426 * already loaded at the address given by argv[1] and therefore
427 * the original bzImage is somewhere else, or not accessible.
428 * In any case, we don't need access to the bzImage since all
429 * the processing is assumed to be done.
430 *
431 * So set the base_ptr to the given address, use this arg as the
432 * load address and set bzimage_addr to 0 so we know that it
433 * cannot be proceesed (or processed again).
434 */
Simon Glass3ff49ec2021-07-24 09:03:29 -0600435 state.base_ptr = (void *)hextoul(argv[5], NULL);
Simon Glassc55ce682020-09-05 14:50:49 -0600436 state.load_address = state.bzimage_addr;
437 state.bzimage_addr = 0;
438 }
Simon Glass12d93ab2020-09-05 14:50:51 -0600439 if (argc >= 7)
Simon Glass5e956d12020-11-09 07:12:24 -0700440 state.cmdline = env_get(argv[6]);
Graeme Russ1bab1042010-04-24 00:05:49 +1000441
Simon Glass24d26f72020-09-05 14:50:46 -0600442 return 0;
443}
444
445static int do_zboot_load(struct cmd_tbl *cmdtp, int flag, int argc,
446 char *const argv[])
447{
448 struct boot_params *base_ptr;
449
Simon Glassc55ce682020-09-05 14:50:49 -0600450 if (state.base_ptr) {
451 struct boot_params *from = (struct boot_params *)state.base_ptr;
452
453 base_ptr = (struct boot_params *)DEFAULT_SETUP_BASE;
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700454 log_debug("Building boot_params at 0x%8.8lx\n",
455 (ulong)base_ptr);
Simon Glassc55ce682020-09-05 14:50:49 -0600456 memset(base_ptr, '\0', sizeof(*base_ptr));
457 base_ptr->hdr = from->hdr;
458 } else {
459 base_ptr = load_zimage((void *)state.bzimage_addr, state.bzimage_size,
460 &state.load_address);
461 if (!base_ptr) {
462 puts("## Kernel loading failed ...\n");
463 return CMD_RET_FAILURE;
464 }
Graeme Russ1bab1042010-04-24 00:05:49 +1000465 }
Simon Glass1931b192020-09-05 14:50:44 -0600466 state.base_ptr = base_ptr;
Simon Glass58a90462020-09-05 14:50:48 -0600467 if (env_set_hex("zbootbase", (ulong)base_ptr) ||
468 env_set_hex("zbootaddr", state.load_address))
469 return CMD_RET_FAILURE;
Simon Glass48800f42020-09-05 14:50:38 -0600470
Simon Glass30634832020-09-05 14:50:47 -0600471 return 0;
472}
473
474static int do_zboot_setup(struct cmd_tbl *cmdtp, int flag, int argc,
475 char *const argv[])
476{
477 struct boot_params *base_ptr = state.base_ptr;
478 int ret;
479
480 if (!base_ptr) {
481 printf("base is not set: use 'zboot load' first\n");
482 return CMD_RET_FAILURE;
483 }
484 ret = setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
Simon Glass12d93ab2020-09-05 14:50:51 -0600485 0, state.initrd_addr, state.initrd_size,
Simon Glass5e956d12020-11-09 07:12:24 -0700486 (ulong)state.cmdline);
Simon Glass30634832020-09-05 14:50:47 -0600487 if (ret) {
Simon Glassddf446b2014-10-10 08:21:59 -0600488 puts("Setting up boot parameters failed ...\n");
Simon Glass30634832020-09-05 14:50:47 -0600489 return CMD_RET_FAILURE;
Gabe Black540c2622011-12-05 12:09:26 +0000490 }
491
Simon Glass1931b192020-09-05 14:50:44 -0600492 return 0;
493}
494
Simon Glass83dd29e2020-09-05 14:50:45 -0600495static int do_zboot_info(struct cmd_tbl *cmdtp, int flag, int argc,
496 char *const argv[])
497{
498 printf("Kernel loaded at %08lx, setup_base=%p\n",
499 state.load_address, state.base_ptr);
500
501 return 0;
502}
503
Simon Glass1931b192020-09-05 14:50:44 -0600504static int do_zboot_go(struct cmd_tbl *cmdtp, int flag, int argc,
505 char *const argv[])
506{
Simon Glass6d496322023-03-20 08:30:08 +1300507 struct boot_params *params = state.base_ptr;
508 struct setup_header *hdr = &params->hdr;
509 bool image_64bit;
510 ulong entry;
Simon Glass1931b192020-09-05 14:50:44 -0600511 int ret;
512
Simon Glass0db800c2020-09-05 14:50:42 -0600513 disable_interrupts();
Simon Glass1931b192020-09-05 14:50:44 -0600514
Simon Glass6d496322023-03-20 08:30:08 +1300515 entry = state.load_address;
516 image_64bit = false;
517 if (IS_ENABLED(CONFIG_X86_RUN_64BIT) &&
518 (hdr->xloadflags & XLF_KERNEL_64)) {
519 entry += 0x200;
520 image_64bit = true;
521 }
522
Gabe Black540c2622011-12-05 12:09:26 +0000523 /* we assume that the kernel is in place */
Simon Glass6d496322023-03-20 08:30:08 +1300524 ret = boot_linux_kernel((ulong)state.base_ptr, entry, image_64bit);
Simon Glass1931b192020-09-05 14:50:44 -0600525 printf("Kernel returned! (err=%d)\n", ret);
526
527 return CMD_RET_FAILURE;
Graeme Russ1bab1042010-04-24 00:05:49 +1000528}
529
Simon Glassaad0d762020-09-05 14:50:50 -0600530static void print_num(const char *name, ulong value)
531{
532 printf("%-20s: %lx\n", name, value);
533}
534
535static void print_num64(const char *name, u64 value)
536{
537 printf("%-20s: %llx\n", name, value);
538}
539
540static const char *const e820_type_name[E820_COUNT] = {
541 [E820_RAM] = "RAM",
542 [E820_RESERVED] = "Reserved",
543 [E820_ACPI] = "ACPI",
544 [E820_NVS] = "ACPI NVS",
545 [E820_UNUSABLE] = "Unusable",
546};
547
548static const char *const bootloader_id[] = {
549 "LILO",
550 "Loadlin",
551 "bootsect-loader",
552 "Syslinux",
553 "Etherboot/gPXE/iPXE",
554 "ELILO",
555 "undefined",
556 "GRUB",
557 "U-Boot",
558 "Xen",
559 "Gujin",
560 "Qemu",
561 "Arcturus Networks uCbootloader",
562 "kexec-tools",
563 "Extended",
564 "Special",
565 "Reserved",
566 "Minimal Linux Bootloader",
567 "OVMF UEFI virtualization stack",
568};
569
570struct flag_info {
571 uint bit;
572 const char *name;
573};
574
575static struct flag_info load_flags[] = {
576 { LOADED_HIGH, "loaded-high" },
577 { QUIET_FLAG, "quiet" },
578 { KEEP_SEGMENTS, "keep-segments" },
579 { CAN_USE_HEAP, "can-use-heap" },
580};
581
582static struct flag_info xload_flags[] = {
583 { XLF_KERNEL_64, "64-bit-entry" },
584 { XLF_CAN_BE_LOADED_ABOVE_4G, "can-load-above-4gb" },
585 { XLF_EFI_HANDOVER_32, "32-efi-handoff" },
586 { XLF_EFI_HANDOVER_64, "64-efi-handoff" },
587 { XLF_EFI_KEXEC, "kexec-efi-runtime" },
588};
589
590static void print_flags(struct flag_info *flags, int count, uint value)
591{
592 int i;
593
594 printf("%-20s:", "");
595 for (i = 0; i < count; i++) {
596 uint mask = flags[i].bit;
597
598 if (value & mask)
599 printf(" %s", flags[i].name);
600 }
601 printf("\n");
602}
603
604static void show_loader(struct setup_header *hdr)
605{
606 bool version_valid = false;
607 int type, version;
608 const char *name;
609
610 type = hdr->type_of_loader >> 4;
611 version = hdr->type_of_loader & 0xf;
612 if (type == 0xe)
613 type = 0x10 + hdr->ext_loader_type;
614 version |= hdr->ext_loader_ver << 4;
615 if (!hdr->type_of_loader) {
616 name = "pre-2.00 bootloader";
617 } else if (hdr->type_of_loader == 0xff) {
618 name = "unknown";
619 } else if (type < ARRAY_SIZE(bootloader_id)) {
620 name = bootloader_id[type];
621 version_valid = true;
622 } else {
623 name = "undefined";
624 }
625 printf("%20s %s", "", name);
626 if (version_valid)
627 printf(", version %x", version);
628 printf("\n");
629}
630
Simon Glassba094bb2021-01-24 10:06:08 -0700631void zimage_dump(struct boot_params *base_ptr)
Simon Glassaad0d762020-09-05 14:50:50 -0600632{
Simon Glassaad0d762020-09-05 14:50:50 -0600633 struct setup_header *hdr;
634 const char *version;
635 int i;
636
Simon Glassaad0d762020-09-05 14:50:50 -0600637 printf("Setup located at %p:\n\n", base_ptr);
638 print_num64("ACPI RSDP addr", base_ptr->acpi_rsdp_addr);
639
640 printf("E820: %d entries\n", base_ptr->e820_entries);
641 if (base_ptr->e820_entries) {
642 printf("%18s %16s %s\n", "Addr", "Size", "Type");
643 for (i = 0; i < base_ptr->e820_entries; i++) {
644 struct e820_entry *entry = &base_ptr->e820_map[i];
645
646 printf("%12llx %10llx %s\n", entry->addr, entry->size,
647 entry->type < E820_COUNT ?
648 e820_type_name[entry->type] :
649 simple_itoa(entry->type));
650 }
651 }
652
653 hdr = &base_ptr->hdr;
654 print_num("Setup sectors", hdr->setup_sects);
655 print_num("Root flags", hdr->root_flags);
656 print_num("Sys size", hdr->syssize);
657 print_num("RAM size", hdr->ram_size);
658 print_num("Video mode", hdr->vid_mode);
659 print_num("Root dev", hdr->root_dev);
660 print_num("Boot flag", hdr->boot_flag);
661 print_num("Jump", hdr->jump);
662 print_num("Header", hdr->header);
663 if (hdr->header == KERNEL_V2_MAGIC)
664 printf("%-20s %s\n", "", "Kernel V2");
665 else
666 printf("%-20s %s\n", "", "Ancient kernel, using version 100");
667 print_num("Version", hdr->version);
668 print_num("Real mode switch", hdr->realmode_swtch);
Simon Glass83ec1ca2023-03-20 08:30:06 +1300669 print_num("Start sys seg", hdr->start_sys_seg);
Simon Glassaad0d762020-09-05 14:50:50 -0600670 print_num("Kernel version", hdr->kernel_version);
671 version = get_kernel_version(base_ptr, (void *)state.bzimage_addr);
672 if (version)
673 printf(" @%p: %s\n", version, version);
674 print_num("Type of loader", hdr->type_of_loader);
675 show_loader(hdr);
676 print_num("Load flags", hdr->loadflags);
677 print_flags(load_flags, ARRAY_SIZE(load_flags), hdr->loadflags);
678 print_num("Setup move size", hdr->setup_move_size);
679 print_num("Code32 start", hdr->code32_start);
680 print_num("Ramdisk image", hdr->ramdisk_image);
681 print_num("Ramdisk size", hdr->ramdisk_size);
682 print_num("Bootsect kludge", hdr->bootsect_kludge);
683 print_num("Heap end ptr", hdr->heap_end_ptr);
684 print_num("Ext loader ver", hdr->ext_loader_ver);
685 print_num("Ext loader type", hdr->ext_loader_type);
686 print_num("Command line ptr", hdr->cmd_line_ptr);
687 if (hdr->cmd_line_ptr) {
688 printf(" ");
689 /* Use puts() to avoid limits from CONFIG_SYS_PBSIZE */
Simon Glass12d93ab2020-09-05 14:50:51 -0600690 puts((char *)(ulong)hdr->cmd_line_ptr);
Simon Glassaad0d762020-09-05 14:50:50 -0600691 printf("\n");
692 }
693 print_num("Initrd addr max", hdr->initrd_addr_max);
694 print_num("Kernel alignment", hdr->kernel_alignment);
695 print_num("Relocatable kernel", hdr->relocatable_kernel);
696 print_num("Min alignment", hdr->min_alignment);
697 if (hdr->min_alignment)
698 printf("%-20s: %x\n", "", 1 << hdr->min_alignment);
699 print_num("Xload flags", hdr->xloadflags);
700 print_flags(xload_flags, ARRAY_SIZE(xload_flags), hdr->xloadflags);
701 print_num("Cmdline size", hdr->cmdline_size);
702 print_num("Hardware subarch", hdr->hardware_subarch);
703 print_num64("HW subarch data", hdr->hardware_subarch_data);
704 print_num("Payload offset", hdr->payload_offset);
705 print_num("Payload length", hdr->payload_length);
706 print_num64("Setup data", hdr->setup_data);
707 print_num64("Pref address", hdr->pref_address);
708 print_num("Init size", hdr->init_size);
709 print_num("Handover offset", hdr->handover_offset);
710 if (get_boot_protocol(hdr, false) >= 0x215)
711 print_num("Kernel info offset", hdr->kernel_info_offset);
Simon Glassba094bb2021-01-24 10:06:08 -0700712}
713
714static int do_zboot_dump(struct cmd_tbl *cmdtp, int flag, int argc,
715 char *const argv[])
716{
717 struct boot_params *base_ptr = state.base_ptr;
718
719 if (argc > 1)
Simon Glass3ff49ec2021-07-24 09:03:29 -0600720 base_ptr = (void *)hextoul(argv[1], NULL);
Simon Glassba094bb2021-01-24 10:06:08 -0700721 if (!base_ptr) {
722 printf("No zboot setup_base\n");
723 return CMD_RET_FAILURE;
724 }
725 zimage_dump(base_ptr);
Simon Glassaad0d762020-09-05 14:50:50 -0600726
727 return 0;
728}
729
Simon Glassd9b3c982020-09-05 14:50:43 -0600730/* Note: This defines the complete_zboot() function */
731U_BOOT_SUBCMDS(zboot,
Simon Glass12d93ab2020-09-05 14:50:51 -0600732 U_BOOT_CMD_MKENT(start, 8, 1, do_zboot_start, "", ""),
Simon Glass24d26f72020-09-05 14:50:46 -0600733 U_BOOT_CMD_MKENT(load, 1, 1, do_zboot_load, "", ""),
Simon Glass30634832020-09-05 14:50:47 -0600734 U_BOOT_CMD_MKENT(setup, 1, 1, do_zboot_setup, "", ""),
Simon Glass83dd29e2020-09-05 14:50:45 -0600735 U_BOOT_CMD_MKENT(info, 1, 1, do_zboot_info, "", ""),
Simon Glass1931b192020-09-05 14:50:44 -0600736 U_BOOT_CMD_MKENT(go, 1, 1, do_zboot_go, "", ""),
Simon Glassaad0d762020-09-05 14:50:50 -0600737 U_BOOT_CMD_MKENT(dump, 2, 1, do_zboot_dump, "", ""),
Simon Glassd9b3c982020-09-05 14:50:43 -0600738)
739
740int do_zboot_states(struct cmd_tbl *cmdtp, int flag, int argc,
741 char *const argv[], int state_mask)
742{
743 int i;
744
745 for (i = 0; i < ZBOOT_STATE_COUNT; i++) {
746 struct cmd_tbl *cmd = &zboot_subcmds[i];
747 int mask = 1 << i;
748 int ret;
749
750 if (mask & state_mask) {
751 ret = cmd->cmd(cmd, flag, argc, argv);
752 if (ret)
753 return ret;
754 }
755 }
756
757 return 0;
758}
759
760int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc,
761 char *const argv[], int *repeatable)
762{
763 /* determine if we have a sub command */
764 if (argc > 1) {
765 char *endp;
766
Simon Glass3ff49ec2021-07-24 09:03:29 -0600767 hextoul(argv[1], &endp);
Simon Glassd9b3c982020-09-05 14:50:43 -0600768 /*
769 * endp pointing to nul means that argv[1] was just a valid
770 * number, so pass it along to the normal processing
771 */
772 if (*endp)
773 return do_zboot(cmdtp, flag, argc, argv, repeatable);
774 }
775
Simon Glass1931b192020-09-05 14:50:44 -0600776 do_zboot_states(cmdtp, flag, argc, argv, ZBOOT_STATE_START |
Simon Glass30634832020-09-05 14:50:47 -0600777 ZBOOT_STATE_LOAD | ZBOOT_STATE_SETUP |
778 ZBOOT_STATE_INFO | ZBOOT_STATE_GO);
Simon Glassd9b3c982020-09-05 14:50:43 -0600779
780 return CMD_RET_FAILURE;
781}
782
783U_BOOT_CMDREP_COMPLETE(
Simon Glass12d93ab2020-09-05 14:50:51 -0600784 zboot, 8, do_zboot_parent, "Boot bzImage",
785 "[addr] [size] [initrd addr] [initrd size] [setup] [cmdline]\n"
Gabe Black69b49ee2011-12-05 12:09:27 +0000786 " addr - The optional starting address of the bzimage.\n"
787 " If not set it defaults to the environment\n"
788 " variable \"fileaddr\".\n"
789 " size - The optional size of the bzimage. Defaults to\n"
790 " zero.\n"
791 " initrd addr - The address of the initrd image to use, if any.\n"
792 " initrd size - The size of the initrd image to use, if any.\n"
Simon Glassc55ce682020-09-05 14:50:49 -0600793 " setup - The address of the kernel setup region, if this\n"
794 " is not at addr\n"
Simon Glass5e956d12020-11-09 07:12:24 -0700795 " cmdline - Environment variable containing the kernel\n"
796 " command line, to override U-Boot's normal\n"
797 " cmdline generation\n"
Simon Glassd9b3c982020-09-05 14:50:43 -0600798 "\n"
799 "Sub-commands to do part of the zboot sequence:\n"
Simon Glass1931b192020-09-05 14:50:44 -0600800 "\tstart [addr [arg ...]] - specify arguments\n"
Simon Glass24d26f72020-09-05 14:50:46 -0600801 "\tload - load OS image\n"
Simon Glass30634832020-09-05 14:50:47 -0600802 "\tsetup - set up table\n"
Simon Glass83dd29e2020-09-05 14:50:45 -0600803 "\tinfo - show summary info\n"
Simon Glassaad0d762020-09-05 14:50:50 -0600804 "\tgo - start OS\n"
805 "\tdump [addr] - dump info (optional address of boot params)",
Simon Glassd9b3c982020-09-05 14:50:43 -0600806 complete_zboot
Graeme Russ1bab1042010-04-24 00:05:49 +1000807);