blob: 602788e016dea9e563cf164759187fd1ae67b53e [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 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 Glass858fed12020-04-08 16:57:36 -060024#include <acpi/acpi_table.h>
wdenk591dda52002-11-18 00:14:45 +000025#include <asm/io.h>
26#include <asm/ptrace.h>
27#include <asm/zimage.h>
wdenk591dda52002-11-18 00:14:45 +000028#include <asm/byteorder.h>
Simon Glassaa83f2b2014-10-19 21:11:20 -060029#include <asm/bootm.h>
Graeme Russ1bab1042010-04-24 00:05:49 +100030#include <asm/bootparam.h>
Vadim Bendebury8cd22c82012-10-23 18:04:34 +000031#ifdef CONFIG_SYS_COREBOOT
32#include <asm/arch/timestamp.h>
33#endif
Stefan Reinauer2e5225e2012-10-23 18:04:37 +000034#include <linux/compiler.h>
Simon Glass8ee28542020-11-04 09:59:14 -070035#include <linux/ctype.h>
Ivan Gorinov901bb0b2018-03-26 18:06:54 -070036#include <linux/libfdt.h>
wdenk591dda52002-11-18 00:14:45 +000037
38/*
39 * Memory lay-out:
wdenk57b2d802003-06-27 21:31:46 +000040 *
wdenk591dda52002-11-18 00:14:45 +000041 * relative to setup_base (which is 0x90000 currently)
wdenk57b2d802003-06-27 21:31:46 +000042 *
43 * 0x0000-0x7FFF Real mode kernel
wdenk591dda52002-11-18 00:14:45 +000044 * 0x8000-0x8FFF Stack and heap
45 * 0x9000-0x90FF Kernel command line
46 */
Graeme Russ883c6032011-11-08 02:33:15 +000047#define DEFAULT_SETUP_BASE 0x90000
48#define COMMAND_LINE_OFFSET 0x9000
49#define HEAP_END_OFFSET 0x8e00
wdenk591dda52002-11-18 00:14:45 +000050
Graeme Russ883c6032011-11-08 02:33:15 +000051#define COMMAND_LINE_SIZE 2048
wdenk591dda52002-11-18 00:14:45 +000052
Simon Glass48800f42020-09-05 14:50:38 -060053/**
54 * struct zboot_state - Current state of the boot
55 *
56 * @bzimage_addr: Address of the bzImage to boot
57 * @bzimage_size: Size of the bzImage, or 0 to detect this
58 * @initrd_addr: Address of the initial ramdisk, or 0 if none
59 * @initrd_size: Size of the initial ramdisk, or 0 if none
60 * @load_address: Address where the bzImage is moved before booting, either
61 * BZIMAGE_LOAD_ADDR or ZIMAGE_LOAD_ADDR
Simon Glass1931b192020-09-05 14:50:44 -060062 * @base_ptr: Pointer to the boot parameters, typically at address
63 * DEFAULT_SETUP_BASE
Simon Glass5e956d12020-11-09 07:12:24 -070064 * @cmdline: Environment variable containing the 'override' command line, or
65 * NULL to use the one in the setup block
Simon Glass48800f42020-09-05 14:50:38 -060066 */
67struct zboot_state {
68 ulong bzimage_addr;
69 ulong bzimage_size;
70 ulong initrd_addr;
71 ulong initrd_size;
72 ulong load_address;
Simon Glass1931b192020-09-05 14:50:44 -060073 struct boot_params *base_ptr;
Simon Glass5e956d12020-11-09 07:12:24 -070074 char *cmdline;
Simon Glass48800f42020-09-05 14:50:38 -060075} state;
76
Simon Glassd9b3c982020-09-05 14:50:43 -060077enum {
78 ZBOOT_STATE_START = BIT(0),
Simon Glass24d26f72020-09-05 14:50:46 -060079 ZBOOT_STATE_LOAD = BIT(1),
Simon Glass30634832020-09-05 14:50:47 -060080 ZBOOT_STATE_SETUP = BIT(2),
81 ZBOOT_STATE_INFO = BIT(3),
82 ZBOOT_STATE_GO = BIT(4),
Simon Glassd9b3c982020-09-05 14:50:43 -060083
Simon Glassaad0d762020-09-05 14:50:50 -060084 /* This one doesn't execute automatically, so stop the count before 5 */
85 ZBOOT_STATE_DUMP = BIT(5),
Simon Glass30634832020-09-05 14:50:47 -060086 ZBOOT_STATE_COUNT = 5,
Simon Glassd9b3c982020-09-05 14:50:43 -060087};
88
wdenk591dda52002-11-18 00:14:45 +000089static void build_command_line(char *command_line, int auto_boot)
90{
91 char *env_command_line;
wdenk57b2d802003-06-27 21:31:46 +000092
wdenk591dda52002-11-18 00:14:45 +000093 command_line[0] = '\0';
wdenk57b2d802003-06-27 21:31:46 +000094
Simon Glass64b723f2017-08-03 12:22:12 -060095 env_command_line = env_get("bootargs");
wdenk57b2d802003-06-27 21:31:46 +000096
wdenk591dda52002-11-18 00:14:45 +000097 /* set console= argument if we use a serial console */
Graeme Russ883c6032011-11-08 02:33:15 +000098 if (!strstr(env_command_line, "console=")) {
Simon Glass64b723f2017-08-03 12:22:12 -060099 if (!strcmp(env_get("stdout"), "serial")) {
wdenk57b2d802003-06-27 21:31:46 +0000100
wdenk591dda52002-11-18 00:14:45 +0000101 /* We seem to use serial console */
wdenk57b2d802003-06-27 21:31:46 +0000102 sprintf(command_line, "console=ttyS0,%s ",
Simon Glass64b723f2017-08-03 12:22:12 -0600103 env_get("baudrate"));
wdenk591dda52002-11-18 00:14:45 +0000104 }
105 }
wdenk57b2d802003-06-27 21:31:46 +0000106
Graeme Russ883c6032011-11-08 02:33:15 +0000107 if (auto_boot)
wdenk591dda52002-11-18 00:14:45 +0000108 strcat(command_line, "auto ");
wdenk57b2d802003-06-27 21:31:46 +0000109
Graeme Russ883c6032011-11-08 02:33:15 +0000110 if (env_command_line)
wdenk591dda52002-11-18 00:14:45 +0000111 strcat(command_line, env_command_line);
Simon Glasseeb01222021-01-24 10:06:09 -0700112#ifdef DEBUG
113 printf("Kernel command line:");
114 puts(command_line);
115 printf("\n");
116#endif
wdenk591dda52002-11-18 00:14:45 +0000117}
118
Gabe Black540c2622011-12-05 12:09:26 +0000119static int kernel_magic_ok(struct setup_header *hdr)
wdenk591dda52002-11-18 00:14:45 +0000120{
Graeme Russ1bab1042010-04-24 00:05:49 +1000121 if (KERNEL_MAGIC != hdr->boot_flag) {
Gabe Black1cf51212011-12-05 12:09:23 +0000122 printf("Error: Invalid Boot Flag "
123 "(found 0x%04x, expected 0x%04x)\n",
124 hdr->boot_flag, KERNEL_MAGIC);
wdenk591dda52002-11-18 00:14:45 +0000125 return 0;
Graeme Russ1bab1042010-04-24 00:05:49 +1000126 } else {
127 printf("Valid Boot Flag\n");
Gabe Black540c2622011-12-05 12:09:26 +0000128 return 1;
wdenk591dda52002-11-18 00:14:45 +0000129 }
Gabe Black540c2622011-12-05 12:09:26 +0000130}
wdenk57b2d802003-06-27 21:31:46 +0000131
Simon Glassdf30d412020-09-05 14:50:40 -0600132static int get_boot_protocol(struct setup_header *hdr, bool verbose)
Gabe Black540c2622011-12-05 12:09:26 +0000133{
134 if (hdr->header == KERNEL_V2_MAGIC) {
Simon Glassdf30d412020-09-05 14:50:40 -0600135 if (verbose)
136 printf("Magic signature found\n");
Gabe Black540c2622011-12-05 12:09:26 +0000137 return hdr->version;
wdenk591dda52002-11-18 00:14:45 +0000138 } else {
139 /* Very old kernel */
Simon Glassdf30d412020-09-05 14:50:40 -0600140 if (verbose)
141 printf("Magic signature not found\n");
Gabe Black540c2622011-12-05 12:09:26 +0000142 return 0x0100;
wdenk591dda52002-11-18 00:14:45 +0000143 }
Gabe Black540c2622011-12-05 12:09:26 +0000144}
145
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700146static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob)
147{
Simon Glassdf30d412020-09-05 14:50:40 -0600148 int bootproto = get_boot_protocol(hdr, false);
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700149 struct setup_data *sd;
150 int size;
151
152 if (bootproto < 0x0209)
153 return -ENOTSUPP;
154
155 if (!fdt_blob)
156 return 0;
157
158 size = fdt_totalsize(fdt_blob);
159 if (size < 0)
160 return -EINVAL;
161
162 size += sizeof(struct setup_data);
163 sd = (struct setup_data *)malloc(size);
164 if (!sd) {
165 printf("Not enough memory for DTB setup data\n");
166 return -ENOMEM;
167 }
168
169 sd->next = hdr->setup_data;
170 sd->type = SETUP_DTB;
171 sd->len = fdt_totalsize(fdt_blob);
172 memcpy(sd->data, fdt_blob, sd->len);
173 hdr->setup_data = (unsigned long)sd;
174
175 return 0;
176}
177
Simon Glassdf30d412020-09-05 14:50:40 -0600178static const char *get_kernel_version(struct boot_params *params,
179 void *kernel_base)
180{
181 struct setup_header *hdr = &params->hdr;
182 int bootproto;
Simon Glass8ee28542020-11-04 09:59:14 -0700183 const char *s, *end;
Simon Glassdf30d412020-09-05 14:50:40 -0600184
185 bootproto = get_boot_protocol(hdr, false);
186 if (bootproto < 0x0200 || hdr->setup_sects < 15)
187 return NULL;
188
Simon Glass8ee28542020-11-04 09:59:14 -0700189 /* sanity-check the kernel version in case it is missing */
190 for (s = kernel_base + hdr->kernel_version + 0x200, end = s + 0x100; *s;
191 s++) {
192 if (!isprint(*s))
193 return NULL;
194 }
195
Simon Glassdf30d412020-09-05 14:50:40 -0600196 return kernel_base + hdr->kernel_version + 0x200;
197}
198
Gabe Black540c2622011-12-05 12:09:26 +0000199struct boot_params *load_zimage(char *image, unsigned long kernel_size,
Simon Glassd98a5bd2014-10-10 08:21:56 -0600200 ulong *load_addressp)
Gabe Black540c2622011-12-05 12:09:26 +0000201{
202 struct boot_params *setup_base;
Simon Glassdf30d412020-09-05 14:50:40 -0600203 const char *version;
Gabe Black540c2622011-12-05 12:09:26 +0000204 int setup_size;
205 int bootproto;
206 int big_image;
207
208 struct boot_params *params = (struct boot_params *)image;
209 struct setup_header *hdr = &params->hdr;
210
211 /* base address for real-mode segment */
212 setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
213
214 if (!kernel_magic_ok(hdr))
215 return 0;
wdenk57b2d802003-06-27 21:31:46 +0000216
wdenk591dda52002-11-18 00:14:45 +0000217 /* determine size of setup */
Graeme Russ1bab1042010-04-24 00:05:49 +1000218 if (0 == hdr->setup_sects) {
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700219 log_warning("Setup Sectors = 0 (defaulting to 4)\n");
wdenk591dda52002-11-18 00:14:45 +0000220 setup_size = 5 * 512;
221 } else {
Graeme Russ1bab1042010-04-24 00:05:49 +1000222 setup_size = (hdr->setup_sects + 1) * 512;
wdenk591dda52002-11-18 00:14:45 +0000223 }
wdenk57b2d802003-06-27 21:31:46 +0000224
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700225 log_debug("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
Graeme Russ1bab1042010-04-24 00:05:49 +1000226
Graeme Russ883c6032011-11-08 02:33:15 +0000227 if (setup_size > SETUP_MAX_SIZE)
wdenk591dda52002-11-18 00:14:45 +0000228 printf("Error: Setup is too large (%d bytes)\n", setup_size);
wdenk57b2d802003-06-27 21:31:46 +0000229
Gabe Black540c2622011-12-05 12:09:26 +0000230 /* determine boot protocol version */
Simon Glassdf30d412020-09-05 14:50:40 -0600231 bootproto = get_boot_protocol(hdr, true);
Gabe Black540c2622011-12-05 12:09:26 +0000232
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700233 log_debug("Using boot protocol version %x.%02x\n",
234 (bootproto & 0xff00) >> 8, bootproto & 0xff);
Gabe Black540c2622011-12-05 12:09:26 +0000235
Simon Glassdf30d412020-09-05 14:50:40 -0600236 version = get_kernel_version(params, image);
237 if (version)
238 printf("Linux kernel version %s\n", version);
239 else
240 printf("Setup Sectors < 15 - Cannot print kernel version\n");
Gabe Black540c2622011-12-05 12:09:26 +0000241
wdenk591dda52002-11-18 00:14:45 +0000242 /* Determine image type */
Graeme Russ883c6032011-11-08 02:33:15 +0000243 big_image = (bootproto >= 0x0200) &&
244 (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk57b2d802003-06-27 21:31:46 +0000245
Graeme Russ1bab1042010-04-24 00:05:49 +1000246 /* Determine load address */
Gabe Black1cf51212011-12-05 12:09:23 +0000247 if (big_image)
Simon Glassd98a5bd2014-10-10 08:21:56 -0600248 *load_addressp = BZIMAGE_LOAD_ADDR;
Gabe Black1cf51212011-12-05 12:09:23 +0000249 else
Simon Glassd98a5bd2014-10-10 08:21:56 -0600250 *load_addressp = ZIMAGE_LOAD_ADDR;
wdenk57b2d802003-06-27 21:31:46 +0000251
Gabe Blackc67a9482011-12-05 12:09:24 +0000252 printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
253 memset(setup_base, 0, sizeof(*setup_base));
254 setup_base->hdr = params->hdr;
wdenk57b2d802003-06-27 21:31:46 +0000255
Gabe Black540c2622011-12-05 12:09:26 +0000256 if (bootproto >= 0x0204)
257 kernel_size = hdr->syssize * 16;
258 else
259 kernel_size -= setup_size;
wdenk57b2d802003-06-27 21:31:46 +0000260
wdenk57b2d802003-06-27 21:31:46 +0000261 if (bootproto == 0x0100) {
Graeme Russ883c6032011-11-08 02:33:15 +0000262 /*
263 * A very old kernel MUST have its real-mode code
264 * loaded at 0x90000
265 */
Simon Glassca37a392017-01-16 07:03:35 -0700266 if ((ulong)setup_base != 0x90000) {
wdenk591dda52002-11-18 00:14:45 +0000267 /* Copy the real-mode kernel */
Graeme Russ883c6032011-11-08 02:33:15 +0000268 memmove((void *)0x90000, setup_base, setup_size);
269
wdenk591dda52002-11-18 00:14:45 +0000270 /* Copy the command line */
Graeme Russ883c6032011-11-08 02:33:15 +0000271 memmove((void *)0x99000,
Gabe Black1cf51212011-12-05 12:09:23 +0000272 (u8 *)setup_base + COMMAND_LINE_OFFSET,
Graeme Russ883c6032011-11-08 02:33:15 +0000273 COMMAND_LINE_SIZE);
wdenk57b2d802003-06-27 21:31:46 +0000274
Graeme Russ883c6032011-11-08 02:33:15 +0000275 /* Relocated */
Gabe Black1cf51212011-12-05 12:09:23 +0000276 setup_base = (struct boot_params *)0x90000;
wdenk591dda52002-11-18 00:14:45 +0000277 }
wdenk57b2d802003-06-27 21:31:46 +0000278
wdenk591dda52002-11-18 00:14:45 +0000279 /* It is recommended to clear memory up to the 32K mark */
Gabe Black1cf51212011-12-05 12:09:23 +0000280 memset((u8 *)0x90000 + setup_size, 0,
281 SETUP_MAX_SIZE - setup_size);
wdenk591dda52002-11-18 00:14:45 +0000282 }
wdenk57b2d802003-06-27 21:31:46 +0000283
Gabe Black540c2622011-12-05 12:09:26 +0000284 if (big_image) {
285 if (kernel_size > BZIMAGE_MAX_SIZE) {
286 printf("Error: bzImage kernel too big! "
287 "(size: %ld, max: %d)\n",
288 kernel_size, BZIMAGE_MAX_SIZE);
289 return 0;
290 }
291 } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
292 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
293 kernel_size, ZIMAGE_MAX_SIZE);
294 return 0;
295 }
296
Simon Glassd98a5bd2014-10-10 08:21:56 -0600297 printf("Loading %s at address %lx (%ld bytes)\n",
298 big_image ? "bzImage" : "zImage", *load_addressp, kernel_size);
Gabe Black540c2622011-12-05 12:09:26 +0000299
Simon Glassd98a5bd2014-10-10 08:21:56 -0600300 memmove((void *)*load_addressp, image + setup_size, kernel_size);
Gabe Black540c2622011-12-05 12:09:26 +0000301
302 return setup_base;
303}
304
305int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
Simon Glass12d93ab2020-09-05 14:50:51 -0600306 ulong initrd_addr, ulong initrd_size, ulong cmdline_force)
Gabe Black540c2622011-12-05 12:09:26 +0000307{
308 struct setup_header *hdr = &setup_base->hdr;
Simon Glassdf30d412020-09-05 14:50:40 -0600309 int bootproto = get_boot_protocol(hdr, false);
Gabe Black540c2622011-12-05 12:09:26 +0000310
Simon Glass842ff442020-11-04 09:59:13 -0700311 log_debug("Setup E820 entries\n");
Gabe Black540c2622011-12-05 12:09:26 +0000312 setup_base->e820_entries = install_e820_map(
313 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Graeme Russ1bab1042010-04-24 00:05:49 +1000314
Gabe Black540c2622011-12-05 12:09:26 +0000315 if (bootproto == 0x0100) {
316 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
317 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
318 }
wdenk591dda52002-11-18 00:14:45 +0000319 if (bootproto >= 0x0200) {
Simon Glassd67ff032020-09-05 14:50:41 -0600320 hdr->type_of_loader = 0x80; /* U-Boot version 0 */
wdenk591dda52002-11-18 00:14:45 +0000321 if (initrd_addr) {
Gabe Black1cf51212011-12-05 12:09:23 +0000322 printf("Initial RAM disk at linear address "
323 "0x%08lx, size %ld bytes\n",
wdenk591dda52002-11-18 00:14:45 +0000324 initrd_addr, initrd_size);
wdenk57b2d802003-06-27 21:31:46 +0000325
Graeme Russ1bab1042010-04-24 00:05:49 +1000326 hdr->ramdisk_image = initrd_addr;
327 hdr->ramdisk_size = initrd_size;
wdenk591dda52002-11-18 00:14:45 +0000328 }
329 }
wdenk57b2d802003-06-27 21:31:46 +0000330
wdenk591dda52002-11-18 00:14:45 +0000331 if (bootproto >= 0x0201) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000332 hdr->heap_end_ptr = HEAP_END_OFFSET;
333 hdr->loadflags |= HEAP_FLAG;
wdenk591dda52002-11-18 00:14:45 +0000334 }
wdenk57b2d802003-06-27 21:31:46 +0000335
Simon Glass4b4f2732014-10-19 21:11:21 -0600336 if (cmd_line) {
Simon Glass7b48ce82020-11-05 10:33:46 -0700337 int max_size = 0xff;
338 int ret;
339
Simon Glass842ff442020-11-04 09:59:13 -0700340 log_debug("Setup cmdline\n");
Simon Glass7b48ce82020-11-05 10:33:46 -0700341 if (bootproto >= 0x0206)
342 max_size = hdr->cmdline_size;
Simon Glass4b4f2732014-10-19 21:11:21 -0600343 if (bootproto >= 0x0202) {
344 hdr->cmd_line_ptr = (uintptr_t)cmd_line;
345 } else if (bootproto >= 0x0200) {
346 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
347 setup_base->screen_info.cl_offset =
348 (uintptr_t)cmd_line - (uintptr_t)setup_base;
Graeme Russ1bab1042010-04-24 00:05:49 +1000349
Simon Glass4b4f2732014-10-19 21:11:21 -0600350 hdr->setup_move_size = 0x9100;
351 }
352
353 /* build command line at COMMAND_LINE_OFFSET */
Simon Glass12d93ab2020-09-05 14:50:51 -0600354 if (cmdline_force)
355 strcpy(cmd_line, (char *)cmdline_force);
356 else
357 build_command_line(cmd_line, auto_boot);
Simon Glass7b48ce82020-11-05 10:33:46 -0700358 ret = bootm_process_cmdline(cmd_line, max_size, BOOTM_CL_ALL);
359 if (ret) {
Simon Glasseeb01222021-01-24 10:06:09 -0700360 printf("Cmdline setup failed (max_size=%x, bootproto=%x, err=%d)\n",
361 max_size, bootproto, ret);
Simon Glass7b48ce82020-11-05 10:33:46 -0700362 return ret;
363 }
364 printf("Kernel command line: \"");
365 puts(cmd_line);
366 printf("\"\n");
wdenk591dda52002-11-18 00:14:45 +0000367 }
wdenk591dda52002-11-18 00:14:45 +0000368
Simon Glass3a177172020-09-05 14:50:39 -0600369 if (IS_ENABLED(CONFIG_INTEL_MID) && bootproto >= 0x0207)
Andy Shevchenko8b7adf82018-01-10 19:40:14 +0200370 hdr->hardware_subarch = X86_SUBARCH_INTEL_MID;
Andy Shevchenko8b7adf82018-01-10 19:40:14 +0200371
Simon Glass3a177172020-09-05 14:50:39 -0600372 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
373 setup_base->acpi_rsdp_addr = acpi_get_rsdp_addr();
Andy Shevchenkod87a21d2019-09-13 18:42:00 +0300374
Simon Glass842ff442020-11-04 09:59:13 -0700375 log_debug("Setup devicetree\n");
Ivan Gorinov901bb0b2018-03-26 18:06:54 -0700376 setup_device_tree(hdr, (const void *)env_get_hex("fdtaddr", 0));
Bin Mengf6d504f2015-07-06 16:31:36 +0800377 setup_video(&setup_base->screen_info);
378
Simon Glass3a177172020-09-05 14:50:39 -0600379 if (IS_ENABLED(CONFIG_EFI_STUB))
380 setup_efi_info(&setup_base->efi_info);
Bin Meng106dcfc2018-08-23 08:24:10 -0700381
Gabe Black540c2622011-12-05 12:09:26 +0000382 return 0;
wdenk591dda52002-11-18 00:14:45 +0000383}
384
Simon Glassd9b3c982020-09-05 14:50:43 -0600385static int do_zboot_start(struct cmd_tbl *cmdtp, int flag, int argc,
386 char *const argv[])
Graeme Russ1bab1042010-04-24 00:05:49 +1000387{
Simon Glassd9b3c982020-09-05 14:50:43 -0600388 const char *s;
Graeme Russ1bab1042010-04-24 00:05:49 +1000389
Simon Glass48800f42020-09-05 14:50:38 -0600390 memset(&state, '\0', sizeof(state));
Gabe Black1cf51212011-12-05 12:09:23 +0000391 if (argc >= 2) {
Graeme Russ3134be22010-10-07 20:03:19 +1100392 /* argv[1] holds the address of the bzImage */
393 s = argv[1];
Gabe Black1cf51212011-12-05 12:09:23 +0000394 } else {
Simon Glass64b723f2017-08-03 12:22:12 -0600395 s = env_get("fileaddr");
Gabe Black1cf51212011-12-05 12:09:23 +0000396 }
Graeme Russ1bab1042010-04-24 00:05:49 +1000397
Graeme Russ3134be22010-10-07 20:03:19 +1100398 if (s)
Simon Glass48800f42020-09-05 14:50:38 -0600399 state.bzimage_addr = simple_strtoul(s, NULL, 16);
Graeme Russ3134be22010-10-07 20:03:19 +1100400
Gabe Black69b49ee2011-12-05 12:09:27 +0000401 if (argc >= 3) {
Graeme Russ3134be22010-10-07 20:03:19 +1100402 /* argv[2] holds the size of the bzImage */
Simon Glass48800f42020-09-05 14:50:38 -0600403 state.bzimage_size = simple_strtoul(argv[2], NULL, 16);
Gabe Black69b49ee2011-12-05 12:09:27 +0000404 }
405
406 if (argc >= 4)
Simon Glass48800f42020-09-05 14:50:38 -0600407 state.initrd_addr = simple_strtoul(argv[3], NULL, 16);
Gabe Black69b49ee2011-12-05 12:09:27 +0000408 if (argc >= 5)
Simon Glass48800f42020-09-05 14:50:38 -0600409 state.initrd_size = simple_strtoul(argv[4], NULL, 16);
Simon Glassc55ce682020-09-05 14:50:49 -0600410 if (argc >= 6) {
411 /*
412 * When the base_ptr is passed in, we assume that the image is
413 * already loaded at the address given by argv[1] and therefore
414 * the original bzImage is somewhere else, or not accessible.
415 * In any case, we don't need access to the bzImage since all
416 * the processing is assumed to be done.
417 *
418 * So set the base_ptr to the given address, use this arg as the
419 * load address and set bzimage_addr to 0 so we know that it
420 * cannot be proceesed (or processed again).
421 */
422 state.base_ptr = (void *)simple_strtoul(argv[5], NULL, 16);
423 state.load_address = state.bzimage_addr;
424 state.bzimage_addr = 0;
425 }
Simon Glass12d93ab2020-09-05 14:50:51 -0600426 if (argc >= 7)
Simon Glass5e956d12020-11-09 07:12:24 -0700427 state.cmdline = env_get(argv[6]);
Graeme Russ1bab1042010-04-24 00:05:49 +1000428
Simon Glass24d26f72020-09-05 14:50:46 -0600429 return 0;
430}
431
432static int do_zboot_load(struct cmd_tbl *cmdtp, int flag, int argc,
433 char *const argv[])
434{
435 struct boot_params *base_ptr;
436
Simon Glassc55ce682020-09-05 14:50:49 -0600437 if (state.base_ptr) {
438 struct boot_params *from = (struct boot_params *)state.base_ptr;
439
440 base_ptr = (struct boot_params *)DEFAULT_SETUP_BASE;
Simon Glass9d8ca9f2020-11-04 09:59:15 -0700441 log_debug("Building boot_params at 0x%8.8lx\n",
442 (ulong)base_ptr);
Simon Glassc55ce682020-09-05 14:50:49 -0600443 memset(base_ptr, '\0', sizeof(*base_ptr));
444 base_ptr->hdr = from->hdr;
445 } else {
446 base_ptr = load_zimage((void *)state.bzimage_addr, state.bzimage_size,
447 &state.load_address);
448 if (!base_ptr) {
449 puts("## Kernel loading failed ...\n");
450 return CMD_RET_FAILURE;
451 }
Graeme Russ1bab1042010-04-24 00:05:49 +1000452 }
Simon Glass1931b192020-09-05 14:50:44 -0600453 state.base_ptr = base_ptr;
Simon Glass58a90462020-09-05 14:50:48 -0600454 if (env_set_hex("zbootbase", (ulong)base_ptr) ||
455 env_set_hex("zbootaddr", state.load_address))
456 return CMD_RET_FAILURE;
Simon Glass48800f42020-09-05 14:50:38 -0600457
Simon Glass30634832020-09-05 14:50:47 -0600458 return 0;
459}
460
461static int do_zboot_setup(struct cmd_tbl *cmdtp, int flag, int argc,
462 char *const argv[])
463{
464 struct boot_params *base_ptr = state.base_ptr;
465 int ret;
466
467 if (!base_ptr) {
468 printf("base is not set: use 'zboot load' first\n");
469 return CMD_RET_FAILURE;
470 }
471 ret = setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
Simon Glass12d93ab2020-09-05 14:50:51 -0600472 0, state.initrd_addr, state.initrd_size,
Simon Glass5e956d12020-11-09 07:12:24 -0700473 (ulong)state.cmdline);
Simon Glass30634832020-09-05 14:50:47 -0600474 if (ret) {
Simon Glassddf446b2014-10-10 08:21:59 -0600475 puts("Setting up boot parameters failed ...\n");
Simon Glass30634832020-09-05 14:50:47 -0600476 return CMD_RET_FAILURE;
Gabe Black540c2622011-12-05 12:09:26 +0000477 }
478
Simon Glass1931b192020-09-05 14:50:44 -0600479 return 0;
480}
481
Simon Glass83dd29e2020-09-05 14:50:45 -0600482static int do_zboot_info(struct cmd_tbl *cmdtp, int flag, int argc,
483 char *const argv[])
484{
485 printf("Kernel loaded at %08lx, setup_base=%p\n",
486 state.load_address, state.base_ptr);
487
488 return 0;
489}
490
Simon Glass1931b192020-09-05 14:50:44 -0600491static int do_zboot_go(struct cmd_tbl *cmdtp, int flag, int argc,
492 char *const argv[])
493{
494 int ret;
495
Simon Glass0db800c2020-09-05 14:50:42 -0600496 disable_interrupts();
Simon Glass1931b192020-09-05 14:50:44 -0600497
Gabe Black540c2622011-12-05 12:09:26 +0000498 /* we assume that the kernel is in place */
Simon Glass1931b192020-09-05 14:50:44 -0600499 ret = boot_linux_kernel((ulong)state.base_ptr, state.load_address,
500 false);
501 printf("Kernel returned! (err=%d)\n", ret);
502
503 return CMD_RET_FAILURE;
Graeme Russ1bab1042010-04-24 00:05:49 +1000504}
505
Simon Glassaad0d762020-09-05 14:50:50 -0600506static void print_num(const char *name, ulong value)
507{
508 printf("%-20s: %lx\n", name, value);
509}
510
511static void print_num64(const char *name, u64 value)
512{
513 printf("%-20s: %llx\n", name, value);
514}
515
516static const char *const e820_type_name[E820_COUNT] = {
517 [E820_RAM] = "RAM",
518 [E820_RESERVED] = "Reserved",
519 [E820_ACPI] = "ACPI",
520 [E820_NVS] = "ACPI NVS",
521 [E820_UNUSABLE] = "Unusable",
522};
523
524static const char *const bootloader_id[] = {
525 "LILO",
526 "Loadlin",
527 "bootsect-loader",
528 "Syslinux",
529 "Etherboot/gPXE/iPXE",
530 "ELILO",
531 "undefined",
532 "GRUB",
533 "U-Boot",
534 "Xen",
535 "Gujin",
536 "Qemu",
537 "Arcturus Networks uCbootloader",
538 "kexec-tools",
539 "Extended",
540 "Special",
541 "Reserved",
542 "Minimal Linux Bootloader",
543 "OVMF UEFI virtualization stack",
544};
545
546struct flag_info {
547 uint bit;
548 const char *name;
549};
550
551static struct flag_info load_flags[] = {
552 { LOADED_HIGH, "loaded-high" },
553 { QUIET_FLAG, "quiet" },
554 { KEEP_SEGMENTS, "keep-segments" },
555 { CAN_USE_HEAP, "can-use-heap" },
556};
557
558static struct flag_info xload_flags[] = {
559 { XLF_KERNEL_64, "64-bit-entry" },
560 { XLF_CAN_BE_LOADED_ABOVE_4G, "can-load-above-4gb" },
561 { XLF_EFI_HANDOVER_32, "32-efi-handoff" },
562 { XLF_EFI_HANDOVER_64, "64-efi-handoff" },
563 { XLF_EFI_KEXEC, "kexec-efi-runtime" },
564};
565
566static void print_flags(struct flag_info *flags, int count, uint value)
567{
568 int i;
569
570 printf("%-20s:", "");
571 for (i = 0; i < count; i++) {
572 uint mask = flags[i].bit;
573
574 if (value & mask)
575 printf(" %s", flags[i].name);
576 }
577 printf("\n");
578}
579
580static void show_loader(struct setup_header *hdr)
581{
582 bool version_valid = false;
583 int type, version;
584 const char *name;
585
586 type = hdr->type_of_loader >> 4;
587 version = hdr->type_of_loader & 0xf;
588 if (type == 0xe)
589 type = 0x10 + hdr->ext_loader_type;
590 version |= hdr->ext_loader_ver << 4;
591 if (!hdr->type_of_loader) {
592 name = "pre-2.00 bootloader";
593 } else if (hdr->type_of_loader == 0xff) {
594 name = "unknown";
595 } else if (type < ARRAY_SIZE(bootloader_id)) {
596 name = bootloader_id[type];
597 version_valid = true;
598 } else {
599 name = "undefined";
600 }
601 printf("%20s %s", "", name);
602 if (version_valid)
603 printf(", version %x", version);
604 printf("\n");
605}
606
Simon Glassba094bb2021-01-24 10:06:08 -0700607void zimage_dump(struct boot_params *base_ptr)
Simon Glassaad0d762020-09-05 14:50:50 -0600608{
Simon Glassaad0d762020-09-05 14:50:50 -0600609 struct setup_header *hdr;
610 const char *version;
611 int i;
612
Simon Glassaad0d762020-09-05 14:50:50 -0600613 printf("Setup located at %p:\n\n", base_ptr);
614 print_num64("ACPI RSDP addr", base_ptr->acpi_rsdp_addr);
615
616 printf("E820: %d entries\n", base_ptr->e820_entries);
617 if (base_ptr->e820_entries) {
618 printf("%18s %16s %s\n", "Addr", "Size", "Type");
619 for (i = 0; i < base_ptr->e820_entries; i++) {
620 struct e820_entry *entry = &base_ptr->e820_map[i];
621
622 printf("%12llx %10llx %s\n", entry->addr, entry->size,
623 entry->type < E820_COUNT ?
624 e820_type_name[entry->type] :
625 simple_itoa(entry->type));
626 }
627 }
628
629 hdr = &base_ptr->hdr;
630 print_num("Setup sectors", hdr->setup_sects);
631 print_num("Root flags", hdr->root_flags);
632 print_num("Sys size", hdr->syssize);
633 print_num("RAM size", hdr->ram_size);
634 print_num("Video mode", hdr->vid_mode);
635 print_num("Root dev", hdr->root_dev);
636 print_num("Boot flag", hdr->boot_flag);
637 print_num("Jump", hdr->jump);
638 print_num("Header", hdr->header);
639 if (hdr->header == KERNEL_V2_MAGIC)
640 printf("%-20s %s\n", "", "Kernel V2");
641 else
642 printf("%-20s %s\n", "", "Ancient kernel, using version 100");
643 print_num("Version", hdr->version);
644 print_num("Real mode switch", hdr->realmode_swtch);
645 print_num("Start sys", hdr->start_sys);
646 print_num("Kernel version", hdr->kernel_version);
647 version = get_kernel_version(base_ptr, (void *)state.bzimage_addr);
648 if (version)
649 printf(" @%p: %s\n", version, version);
650 print_num("Type of loader", hdr->type_of_loader);
651 show_loader(hdr);
652 print_num("Load flags", hdr->loadflags);
653 print_flags(load_flags, ARRAY_SIZE(load_flags), hdr->loadflags);
654 print_num("Setup move size", hdr->setup_move_size);
655 print_num("Code32 start", hdr->code32_start);
656 print_num("Ramdisk image", hdr->ramdisk_image);
657 print_num("Ramdisk size", hdr->ramdisk_size);
658 print_num("Bootsect kludge", hdr->bootsect_kludge);
659 print_num("Heap end ptr", hdr->heap_end_ptr);
660 print_num("Ext loader ver", hdr->ext_loader_ver);
661 print_num("Ext loader type", hdr->ext_loader_type);
662 print_num("Command line ptr", hdr->cmd_line_ptr);
663 if (hdr->cmd_line_ptr) {
664 printf(" ");
665 /* Use puts() to avoid limits from CONFIG_SYS_PBSIZE */
Simon Glass12d93ab2020-09-05 14:50:51 -0600666 puts((char *)(ulong)hdr->cmd_line_ptr);
Simon Glassaad0d762020-09-05 14:50:50 -0600667 printf("\n");
668 }
669 print_num("Initrd addr max", hdr->initrd_addr_max);
670 print_num("Kernel alignment", hdr->kernel_alignment);
671 print_num("Relocatable kernel", hdr->relocatable_kernel);
672 print_num("Min alignment", hdr->min_alignment);
673 if (hdr->min_alignment)
674 printf("%-20s: %x\n", "", 1 << hdr->min_alignment);
675 print_num("Xload flags", hdr->xloadflags);
676 print_flags(xload_flags, ARRAY_SIZE(xload_flags), hdr->xloadflags);
677 print_num("Cmdline size", hdr->cmdline_size);
678 print_num("Hardware subarch", hdr->hardware_subarch);
679 print_num64("HW subarch data", hdr->hardware_subarch_data);
680 print_num("Payload offset", hdr->payload_offset);
681 print_num("Payload length", hdr->payload_length);
682 print_num64("Setup data", hdr->setup_data);
683 print_num64("Pref address", hdr->pref_address);
684 print_num("Init size", hdr->init_size);
685 print_num("Handover offset", hdr->handover_offset);
686 if (get_boot_protocol(hdr, false) >= 0x215)
687 print_num("Kernel info offset", hdr->kernel_info_offset);
Simon Glassba094bb2021-01-24 10:06:08 -0700688}
689
690static int do_zboot_dump(struct cmd_tbl *cmdtp, int flag, int argc,
691 char *const argv[])
692{
693 struct boot_params *base_ptr = state.base_ptr;
694
695 if (argc > 1)
696 base_ptr = (void *)simple_strtoul(argv[1], NULL, 16);
697 if (!base_ptr) {
698 printf("No zboot setup_base\n");
699 return CMD_RET_FAILURE;
700 }
701 zimage_dump(base_ptr);
Simon Glassaad0d762020-09-05 14:50:50 -0600702
703 return 0;
704}
705
Simon Glassd9b3c982020-09-05 14:50:43 -0600706/* Note: This defines the complete_zboot() function */
707U_BOOT_SUBCMDS(zboot,
Simon Glass12d93ab2020-09-05 14:50:51 -0600708 U_BOOT_CMD_MKENT(start, 8, 1, do_zboot_start, "", ""),
Simon Glass24d26f72020-09-05 14:50:46 -0600709 U_BOOT_CMD_MKENT(load, 1, 1, do_zboot_load, "", ""),
Simon Glass30634832020-09-05 14:50:47 -0600710 U_BOOT_CMD_MKENT(setup, 1, 1, do_zboot_setup, "", ""),
Simon Glass83dd29e2020-09-05 14:50:45 -0600711 U_BOOT_CMD_MKENT(info, 1, 1, do_zboot_info, "", ""),
Simon Glass1931b192020-09-05 14:50:44 -0600712 U_BOOT_CMD_MKENT(go, 1, 1, do_zboot_go, "", ""),
Simon Glassaad0d762020-09-05 14:50:50 -0600713 U_BOOT_CMD_MKENT(dump, 2, 1, do_zboot_dump, "", ""),
Simon Glassd9b3c982020-09-05 14:50:43 -0600714)
715
716int do_zboot_states(struct cmd_tbl *cmdtp, int flag, int argc,
717 char *const argv[], int state_mask)
718{
719 int i;
720
721 for (i = 0; i < ZBOOT_STATE_COUNT; i++) {
722 struct cmd_tbl *cmd = &zboot_subcmds[i];
723 int mask = 1 << i;
724 int ret;
725
726 if (mask & state_mask) {
727 ret = cmd->cmd(cmd, flag, argc, argv);
728 if (ret)
729 return ret;
730 }
731 }
732
733 return 0;
734}
735
736int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc,
737 char *const argv[], int *repeatable)
738{
739 /* determine if we have a sub command */
740 if (argc > 1) {
741 char *endp;
742
743 simple_strtoul(argv[1], &endp, 16);
744 /*
745 * endp pointing to nul means that argv[1] was just a valid
746 * number, so pass it along to the normal processing
747 */
748 if (*endp)
749 return do_zboot(cmdtp, flag, argc, argv, repeatable);
750 }
751
Simon Glass1931b192020-09-05 14:50:44 -0600752 do_zboot_states(cmdtp, flag, argc, argv, ZBOOT_STATE_START |
Simon Glass30634832020-09-05 14:50:47 -0600753 ZBOOT_STATE_LOAD | ZBOOT_STATE_SETUP |
754 ZBOOT_STATE_INFO | ZBOOT_STATE_GO);
Simon Glassd9b3c982020-09-05 14:50:43 -0600755
756 return CMD_RET_FAILURE;
757}
758
759U_BOOT_CMDREP_COMPLETE(
Simon Glass12d93ab2020-09-05 14:50:51 -0600760 zboot, 8, do_zboot_parent, "Boot bzImage",
761 "[addr] [size] [initrd addr] [initrd size] [setup] [cmdline]\n"
Gabe Black69b49ee2011-12-05 12:09:27 +0000762 " addr - The optional starting address of the bzimage.\n"
763 " If not set it defaults to the environment\n"
764 " variable \"fileaddr\".\n"
765 " size - The optional size of the bzimage. Defaults to\n"
766 " zero.\n"
767 " initrd addr - The address of the initrd image to use, if any.\n"
768 " initrd size - The size of the initrd image to use, if any.\n"
Simon Glassc55ce682020-09-05 14:50:49 -0600769 " setup - The address of the kernel setup region, if this\n"
770 " is not at addr\n"
Simon Glass5e956d12020-11-09 07:12:24 -0700771 " cmdline - Environment variable containing the kernel\n"
772 " command line, to override U-Boot's normal\n"
773 " cmdline generation\n"
Simon Glassd9b3c982020-09-05 14:50:43 -0600774 "\n"
775 "Sub-commands to do part of the zboot sequence:\n"
Simon Glass1931b192020-09-05 14:50:44 -0600776 "\tstart [addr [arg ...]] - specify arguments\n"
Simon Glass24d26f72020-09-05 14:50:46 -0600777 "\tload - load OS image\n"
Simon Glass30634832020-09-05 14:50:47 -0600778 "\tsetup - set up table\n"
Simon Glass83dd29e2020-09-05 14:50:45 -0600779 "\tinfo - show summary info\n"
Simon Glassaad0d762020-09-05 14:50:50 -0600780 "\tgo - start OS\n"
781 "\tdump [addr] - dump info (optional address of boot params)",
Simon Glassd9b3c982020-09-05 14:50:43 -0600782 complete_zboot
Graeme Russ1bab1042010-04-24 00:05:49 +1000783);