blob: 7418c9a5fed0845475c99295c1a7702ba398e456 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
wdenk2262cfe2002-11-18 00:14:45 +00002/*
Gabe Blackd3a2bc32011-12-05 12:09:23 +00003 * Copyright (c) 2011 The Chromium OS Authors.
wdenk2262cfe2002-11-18 00:14:45 +00004 * (C) Copyright 2002
Albert ARIBAUDfa82f872011-08-04 18:45:45 +02005 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
wdenk2262cfe2002-11-18 00:14:45 +00006 */
7
wdenk8bde7f72003-06-27 21:31:46 +00008/*
Graeme Russdbf71152011-04-13 19:43:26 +10009 * Linux x86 zImage and bzImage loading
wdenk8bde7f72003-06-27 21:31:46 +000010 *
11 * based on the procdure described in
wdenk2262cfe2002-11-18 00:14:45 +000012 * linux/Documentation/i386/boot.txt
13 */
14
Simon Glassb73d61a2020-11-04 09:59:13 -070015#define LOG_CATEGORY LOGC_BOOT
16
wdenk2262cfe2002-11-18 00:14:45 +000017#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060018#include <command.h>
Simon Glasscdbff9f2019-08-01 09:46:50 -060019#include <env.h>
Simon Glass36bf4462019-11-14 12:57:42 -070020#include <irq_func.h>
Simon Glassb73d61a2020-11-04 09:59:13 -070021#include <log.h>
Ivan Gorinov5d732922018-03-26 18:06:54 -070022#include <malloc.h>
Simon Glass776cc202020-04-08 16:57:36 -060023#include <acpi/acpi_table.h>
wdenk2262cfe2002-11-18 00:14:45 +000024#include <asm/io.h>
25#include <asm/ptrace.h>
26#include <asm/zimage.h>
wdenk2262cfe2002-11-18 00:14:45 +000027#include <asm/byteorder.h>
Simon Glass0d0ba592014-10-19 21:11:20 -060028#include <asm/bootm.h>
Graeme Russ95ffaba2010-04-24 00:05:49 +100029#include <asm/bootparam.h>
Vadim Bendebury3cdc18a2012-10-23 18:04:34 +000030#ifdef CONFIG_SYS_COREBOOT
31#include <asm/arch/timestamp.h>
32#endif
Stefan Reinauer61e0ea92012-10-23 18:04:37 +000033#include <linux/compiler.h>
Ivan Gorinov5d732922018-03-26 18:06:54 -070034#include <linux/libfdt.h>
wdenk2262cfe2002-11-18 00:14:45 +000035
36/*
37 * Memory lay-out:
wdenk8bde7f72003-06-27 21:31:46 +000038 *
wdenk2262cfe2002-11-18 00:14:45 +000039 * relative to setup_base (which is 0x90000 currently)
wdenk8bde7f72003-06-27 21:31:46 +000040 *
41 * 0x0000-0x7FFF Real mode kernel
wdenk2262cfe2002-11-18 00:14:45 +000042 * 0x8000-0x8FFF Stack and heap
43 * 0x9000-0x90FF Kernel command line
44 */
Graeme Russ83088af2011-11-08 02:33:15 +000045#define DEFAULT_SETUP_BASE 0x90000
46#define COMMAND_LINE_OFFSET 0x9000
47#define HEAP_END_OFFSET 0x8e00
wdenk2262cfe2002-11-18 00:14:45 +000048
Graeme Russ83088af2011-11-08 02:33:15 +000049#define COMMAND_LINE_SIZE 2048
wdenk2262cfe2002-11-18 00:14:45 +000050
Simon Glasse8148372020-09-05 14:50:38 -060051/**
52 * struct zboot_state - Current state of the boot
53 *
54 * @bzimage_addr: Address of the bzImage to boot
55 * @bzimage_size: Size of the bzImage, or 0 to detect this
56 * @initrd_addr: Address of the initial ramdisk, or 0 if none
57 * @initrd_size: Size of the initial ramdisk, or 0 if none
58 * @load_address: Address where the bzImage is moved before booting, either
59 * BZIMAGE_LOAD_ADDR or ZIMAGE_LOAD_ADDR
Simon Glass88f1cd62020-09-05 14:50:44 -060060 * @base_ptr: Pointer to the boot parameters, typically at address
61 * DEFAULT_SETUP_BASE
Simon Glass4f960232020-09-05 14:50:51 -060062 * @cmdline: Address of 'override' command line, or 0 to use the one in the
63 * setup block
Simon Glasse8148372020-09-05 14:50:38 -060064 */
65struct zboot_state {
66 ulong bzimage_addr;
67 ulong bzimage_size;
68 ulong initrd_addr;
69 ulong initrd_size;
70 ulong load_address;
Simon Glass88f1cd62020-09-05 14:50:44 -060071 struct boot_params *base_ptr;
Simon Glass4f960232020-09-05 14:50:51 -060072 ulong cmdline;
Simon Glasse8148372020-09-05 14:50:38 -060073} state;
74
Simon Glass5588e772020-09-05 14:50:43 -060075enum {
76 ZBOOT_STATE_START = BIT(0),
Simon Glass1d9e4bb2020-09-05 14:50:46 -060077 ZBOOT_STATE_LOAD = BIT(1),
Simon Glass3e597592020-09-05 14:50:47 -060078 ZBOOT_STATE_SETUP = BIT(2),
79 ZBOOT_STATE_INFO = BIT(3),
80 ZBOOT_STATE_GO = BIT(4),
Simon Glass5588e772020-09-05 14:50:43 -060081
Simon Glass631c2b92020-09-05 14:50:50 -060082 /* This one doesn't execute automatically, so stop the count before 5 */
83 ZBOOT_STATE_DUMP = BIT(5),
Simon Glass3e597592020-09-05 14:50:47 -060084 ZBOOT_STATE_COUNT = 5,
Simon Glass5588e772020-09-05 14:50:43 -060085};
86
wdenk2262cfe2002-11-18 00:14:45 +000087static void build_command_line(char *command_line, int auto_boot)
88{
89 char *env_command_line;
wdenk8bde7f72003-06-27 21:31:46 +000090
wdenk2262cfe2002-11-18 00:14:45 +000091 command_line[0] = '\0';
wdenk8bde7f72003-06-27 21:31:46 +000092
Simon Glass00caae62017-08-03 12:22:12 -060093 env_command_line = env_get("bootargs");
wdenk8bde7f72003-06-27 21:31:46 +000094
wdenk2262cfe2002-11-18 00:14:45 +000095 /* set console= argument if we use a serial console */
Graeme Russ83088af2011-11-08 02:33:15 +000096 if (!strstr(env_command_line, "console=")) {
Simon Glass00caae62017-08-03 12:22:12 -060097 if (!strcmp(env_get("stdout"), "serial")) {
wdenk8bde7f72003-06-27 21:31:46 +000098
wdenk2262cfe2002-11-18 00:14:45 +000099 /* We seem to use serial console */
wdenk8bde7f72003-06-27 21:31:46 +0000100 sprintf(command_line, "console=ttyS0,%s ",
Simon Glass00caae62017-08-03 12:22:12 -0600101 env_get("baudrate"));
wdenk2262cfe2002-11-18 00:14:45 +0000102 }
103 }
wdenk8bde7f72003-06-27 21:31:46 +0000104
Graeme Russ83088af2011-11-08 02:33:15 +0000105 if (auto_boot)
wdenk2262cfe2002-11-18 00:14:45 +0000106 strcat(command_line, "auto ");
wdenk8bde7f72003-06-27 21:31:46 +0000107
Graeme Russ83088af2011-11-08 02:33:15 +0000108 if (env_command_line)
wdenk2262cfe2002-11-18 00:14:45 +0000109 strcat(command_line, env_command_line);
wdenk8bde7f72003-06-27 21:31:46 +0000110
wdenk2262cfe2002-11-18 00:14:45 +0000111 printf("Kernel command line: \"%s\"\n", command_line);
112}
113
Gabe Black69370d12011-12-05 12:09:26 +0000114static int kernel_magic_ok(struct setup_header *hdr)
115{
116 if (KERNEL_MAGIC != hdr->boot_flag) {
117 printf("Error: Invalid Boot Flag "
118 "(found 0x%04x, expected 0x%04x)\n",
119 hdr->boot_flag, KERNEL_MAGIC);
120 return 0;
121 } else {
122 printf("Valid Boot Flag\n");
123 return 1;
124 }
125}
126
Simon Glassc038f3b2020-09-05 14:50:40 -0600127static int get_boot_protocol(struct setup_header *hdr, bool verbose)
Gabe Black69370d12011-12-05 12:09:26 +0000128{
129 if (hdr->header == KERNEL_V2_MAGIC) {
Simon Glassc038f3b2020-09-05 14:50:40 -0600130 if (verbose)
131 printf("Magic signature found\n");
Gabe Black69370d12011-12-05 12:09:26 +0000132 return hdr->version;
133 } else {
134 /* Very old kernel */
Simon Glassc038f3b2020-09-05 14:50:40 -0600135 if (verbose)
136 printf("Magic signature not found\n");
Gabe Black69370d12011-12-05 12:09:26 +0000137 return 0x0100;
138 }
139}
140
Ivan Gorinov5d732922018-03-26 18:06:54 -0700141static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob)
142{
Simon Glassc038f3b2020-09-05 14:50:40 -0600143 int bootproto = get_boot_protocol(hdr, false);
Ivan Gorinov5d732922018-03-26 18:06:54 -0700144 struct setup_data *sd;
145 int size;
146
147 if (bootproto < 0x0209)
148 return -ENOTSUPP;
149
150 if (!fdt_blob)
151 return 0;
152
153 size = fdt_totalsize(fdt_blob);
154 if (size < 0)
155 return -EINVAL;
156
157 size += sizeof(struct setup_data);
158 sd = (struct setup_data *)malloc(size);
159 if (!sd) {
160 printf("Not enough memory for DTB setup data\n");
161 return -ENOMEM;
162 }
163
164 sd->next = hdr->setup_data;
165 sd->type = SETUP_DTB;
166 sd->len = fdt_totalsize(fdt_blob);
167 memcpy(sd->data, fdt_blob, sd->len);
168 hdr->setup_data = (unsigned long)sd;
169
170 return 0;
171}
172
Simon Glassc038f3b2020-09-05 14:50:40 -0600173static const char *get_kernel_version(struct boot_params *params,
174 void *kernel_base)
175{
176 struct setup_header *hdr = &params->hdr;
177 int bootproto;
178
179 bootproto = get_boot_protocol(hdr, false);
180 if (bootproto < 0x0200 || hdr->setup_sects < 15)
181 return NULL;
182
183 return kernel_base + hdr->kernel_version + 0x200;
184}
185
Gabe Black69370d12011-12-05 12:09:26 +0000186struct boot_params *load_zimage(char *image, unsigned long kernel_size,
Simon Glass76539382014-10-10 08:21:56 -0600187 ulong *load_addressp)
wdenk2262cfe2002-11-18 00:14:45 +0000188{
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000189 struct boot_params *setup_base;
Simon Glassc038f3b2020-09-05 14:50:40 -0600190 const char *version;
wdenk2262cfe2002-11-18 00:14:45 +0000191 int setup_size;
192 int bootproto;
193 int big_image;
wdenk8bde7f72003-06-27 21:31:46 +0000194
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000195 struct boot_params *params = (struct boot_params *)image;
196 struct setup_header *hdr = &params->hdr;
wdenk8bde7f72003-06-27 21:31:46 +0000197
Graeme Russ83088af2011-11-08 02:33:15 +0000198 /* base address for real-mode segment */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000199 setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
wdenk8bde7f72003-06-27 21:31:46 +0000200
Gabe Black69370d12011-12-05 12:09:26 +0000201 if (!kernel_magic_ok(hdr))
wdenk2262cfe2002-11-18 00:14:45 +0000202 return 0;
wdenk8bde7f72003-06-27 21:31:46 +0000203
wdenk2262cfe2002-11-18 00:14:45 +0000204 /* determine size of setup */
Graeme Russ95ffaba2010-04-24 00:05:49 +1000205 if (0 == hdr->setup_sects) {
206 printf("Setup Sectors = 0 (defaulting to 4)\n");
wdenk2262cfe2002-11-18 00:14:45 +0000207 setup_size = 5 * 512;
208 } else {
Graeme Russ95ffaba2010-04-24 00:05:49 +1000209 setup_size = (hdr->setup_sects + 1) * 512;
wdenk2262cfe2002-11-18 00:14:45 +0000210 }
wdenk8bde7f72003-06-27 21:31:46 +0000211
Graeme Russ95ffaba2010-04-24 00:05:49 +1000212 printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
213
Graeme Russ83088af2011-11-08 02:33:15 +0000214 if (setup_size > SETUP_MAX_SIZE)
wdenk2262cfe2002-11-18 00:14:45 +0000215 printf("Error: Setup is too large (%d bytes)\n", setup_size);
wdenk8bde7f72003-06-27 21:31:46 +0000216
Gabe Black69370d12011-12-05 12:09:26 +0000217 /* determine boot protocol version */
Simon Glassc038f3b2020-09-05 14:50:40 -0600218 bootproto = get_boot_protocol(hdr, true);
Gabe Black69370d12011-12-05 12:09:26 +0000219
220 printf("Using boot protocol version %x.%02x\n",
221 (bootproto & 0xff00) >> 8, bootproto & 0xff);
222
Simon Glassc038f3b2020-09-05 14:50:40 -0600223 version = get_kernel_version(params, image);
224 if (version)
225 printf("Linux kernel version %s\n", version);
226 else
227 printf("Setup Sectors < 15 - Cannot print kernel version\n");
Gabe Black69370d12011-12-05 12:09:26 +0000228
wdenk2262cfe2002-11-18 00:14:45 +0000229 /* Determine image type */
Graeme Russ83088af2011-11-08 02:33:15 +0000230 big_image = (bootproto >= 0x0200) &&
231 (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk8bde7f72003-06-27 21:31:46 +0000232
Graeme Russ95ffaba2010-04-24 00:05:49 +1000233 /* Determine load address */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000234 if (big_image)
Simon Glass76539382014-10-10 08:21:56 -0600235 *load_addressp = BZIMAGE_LOAD_ADDR;
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000236 else
Simon Glass76539382014-10-10 08:21:56 -0600237 *load_addressp = ZIMAGE_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000238
Gabe Black233dbc12011-12-05 12:09:24 +0000239 printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
240 memset(setup_base, 0, sizeof(*setup_base));
241 setup_base->hdr = params->hdr;
wdenk8bde7f72003-06-27 21:31:46 +0000242
Gabe Black69370d12011-12-05 12:09:26 +0000243 if (bootproto >= 0x0204)
244 kernel_size = hdr->syssize * 16;
245 else
246 kernel_size -= setup_size;
wdenk8bde7f72003-06-27 21:31:46 +0000247
wdenk8bde7f72003-06-27 21:31:46 +0000248 if (bootproto == 0x0100) {
Graeme Russ83088af2011-11-08 02:33:15 +0000249 /*
250 * A very old kernel MUST have its real-mode code
251 * loaded at 0x90000
252 */
Simon Glass42fd8c12017-01-16 07:03:35 -0700253 if ((ulong)setup_base != 0x90000) {
wdenk2262cfe2002-11-18 00:14:45 +0000254 /* Copy the real-mode kernel */
Graeme Russ83088af2011-11-08 02:33:15 +0000255 memmove((void *)0x90000, setup_base, setup_size);
wdenk8bde7f72003-06-27 21:31:46 +0000256
Graeme Russ83088af2011-11-08 02:33:15 +0000257 /* Copy the command line */
258 memmove((void *)0x99000,
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000259 (u8 *)setup_base + COMMAND_LINE_OFFSET,
Graeme Russ83088af2011-11-08 02:33:15 +0000260 COMMAND_LINE_SIZE);
261
262 /* Relocated */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000263 setup_base = (struct boot_params *)0x90000;
wdenk2262cfe2002-11-18 00:14:45 +0000264 }
wdenk8bde7f72003-06-27 21:31:46 +0000265
wdenk2262cfe2002-11-18 00:14:45 +0000266 /* It is recommended to clear memory up to the 32K mark */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000267 memset((u8 *)0x90000 + setup_size, 0,
268 SETUP_MAX_SIZE - setup_size);
wdenk2262cfe2002-11-18 00:14:45 +0000269 }
wdenk8bde7f72003-06-27 21:31:46 +0000270
Gabe Black69370d12011-12-05 12:09:26 +0000271 if (big_image) {
272 if (kernel_size > BZIMAGE_MAX_SIZE) {
273 printf("Error: bzImage kernel too big! "
274 "(size: %ld, max: %d)\n",
275 kernel_size, BZIMAGE_MAX_SIZE);
276 return 0;
277 }
278 } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
279 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
280 kernel_size, ZIMAGE_MAX_SIZE);
281 return 0;
282 }
Graeme Russ95ffaba2010-04-24 00:05:49 +1000283
Simon Glass76539382014-10-10 08:21:56 -0600284 printf("Loading %s at address %lx (%ld bytes)\n",
285 big_image ? "bzImage" : "zImage", *load_addressp, kernel_size);
Gabe Black69370d12011-12-05 12:09:26 +0000286
Simon Glass76539382014-10-10 08:21:56 -0600287 memmove((void *)*load_addressp, image + setup_size, kernel_size);
Gabe Black69370d12011-12-05 12:09:26 +0000288
289 return setup_base;
290}
291
292int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
Simon Glass4f960232020-09-05 14:50:51 -0600293 ulong initrd_addr, ulong initrd_size, ulong cmdline_force)
Gabe Black69370d12011-12-05 12:09:26 +0000294{
295 struct setup_header *hdr = &setup_base->hdr;
Simon Glassc038f3b2020-09-05 14:50:40 -0600296 int bootproto = get_boot_protocol(hdr, false);
Gabe Black69370d12011-12-05 12:09:26 +0000297
Simon Glassb73d61a2020-11-04 09:59:13 -0700298 log_debug("Setup E820 entries\n");
Gabe Black69370d12011-12-05 12:09:26 +0000299 setup_base->e820_entries = install_e820_map(
300 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Gabe Black69370d12011-12-05 12:09:26 +0000301
302 if (bootproto == 0x0100) {
303 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
304 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
305 }
wdenk2262cfe2002-11-18 00:14:45 +0000306 if (bootproto >= 0x0200) {
Simon Glass00630f62020-09-05 14:50:41 -0600307 hdr->type_of_loader = 0x80; /* U-Boot version 0 */
wdenk2262cfe2002-11-18 00:14:45 +0000308 if (initrd_addr) {
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000309 printf("Initial RAM disk at linear address "
310 "0x%08lx, size %ld bytes\n",
wdenk2262cfe2002-11-18 00:14:45 +0000311 initrd_addr, initrd_size);
wdenk8bde7f72003-06-27 21:31:46 +0000312
Graeme Russ95ffaba2010-04-24 00:05:49 +1000313 hdr->ramdisk_image = initrd_addr;
314 hdr->ramdisk_size = initrd_size;
wdenk2262cfe2002-11-18 00:14:45 +0000315 }
316 }
wdenk8bde7f72003-06-27 21:31:46 +0000317
wdenk2262cfe2002-11-18 00:14:45 +0000318 if (bootproto >= 0x0201) {
Graeme Russ95ffaba2010-04-24 00:05:49 +1000319 hdr->heap_end_ptr = HEAP_END_OFFSET;
320 hdr->loadflags |= HEAP_FLAG;
wdenk2262cfe2002-11-18 00:14:45 +0000321 }
wdenk8bde7f72003-06-27 21:31:46 +0000322
Simon Glass97d1e0c2014-10-19 21:11:21 -0600323 if (cmd_line) {
Simon Glassb73d61a2020-11-04 09:59:13 -0700324 log_debug("Setup cmdline\n");
Simon Glass97d1e0c2014-10-19 21:11:21 -0600325 if (bootproto >= 0x0202) {
326 hdr->cmd_line_ptr = (uintptr_t)cmd_line;
327 } else if (bootproto >= 0x0200) {
328 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
329 setup_base->screen_info.cl_offset =
330 (uintptr_t)cmd_line - (uintptr_t)setup_base;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000331
Simon Glass97d1e0c2014-10-19 21:11:21 -0600332 hdr->setup_move_size = 0x9100;
333 }
334
335 /* build command line at COMMAND_LINE_OFFSET */
Simon Glass4f960232020-09-05 14:50:51 -0600336 if (cmdline_force)
337 strcpy(cmd_line, (char *)cmdline_force);
338 else
339 build_command_line(cmd_line, auto_boot);
wdenk2262cfe2002-11-18 00:14:45 +0000340 }
wdenk2262cfe2002-11-18 00:14:45 +0000341
Simon Glass30b372d2020-09-05 14:50:39 -0600342 if (IS_ENABLED(CONFIG_INTEL_MID) && bootproto >= 0x0207)
Andy Shevchenko378960d2018-01-10 19:40:14 +0200343 hdr->hardware_subarch = X86_SUBARCH_INTEL_MID;
Andy Shevchenko378960d2018-01-10 19:40:14 +0200344
Simon Glass30b372d2020-09-05 14:50:39 -0600345 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
346 setup_base->acpi_rsdp_addr = acpi_get_rsdp_addr();
Andy Shevchenkod905aa82019-09-13 18:42:00 +0300347
Simon Glassb73d61a2020-11-04 09:59:13 -0700348 log_debug("Setup devicetree\n");
Ivan Gorinov5d732922018-03-26 18:06:54 -0700349 setup_device_tree(hdr, (const void *)env_get_hex("fdtaddr", 0));
Bin Menga4520022015-07-06 16:31:36 +0800350 setup_video(&setup_base->screen_info);
351
Simon Glass30b372d2020-09-05 14:50:39 -0600352 if (IS_ENABLED(CONFIG_EFI_STUB))
353 setup_efi_info(&setup_base->efi_info);
Bin Meng1fdeacd2018-08-23 08:24:10 -0700354
Gabe Black69370d12011-12-05 12:09:26 +0000355 return 0;
wdenk2262cfe2002-11-18 00:14:45 +0000356}
357
Simon Glass5588e772020-09-05 14:50:43 -0600358static int do_zboot_start(struct cmd_tbl *cmdtp, int flag, int argc,
359 char *const argv[])
Graeme Russ95ffaba2010-04-24 00:05:49 +1000360{
Simon Glass5588e772020-09-05 14:50:43 -0600361 const char *s;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000362
Simon Glasse8148372020-09-05 14:50:38 -0600363 memset(&state, '\0', sizeof(state));
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000364 if (argc >= 2) {
Graeme Russabe98f42010-10-07 20:03:19 +1100365 /* argv[1] holds the address of the bzImage */
366 s = argv[1];
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000367 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600368 s = env_get("fileaddr");
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000369 }
Graeme Russ95ffaba2010-04-24 00:05:49 +1000370
Graeme Russabe98f42010-10-07 20:03:19 +1100371 if (s)
Simon Glasse8148372020-09-05 14:50:38 -0600372 state.bzimage_addr = simple_strtoul(s, NULL, 16);
Graeme Russabe98f42010-10-07 20:03:19 +1100373
Gabe Black6f9d9982011-12-05 12:09:27 +0000374 if (argc >= 3) {
Graeme Russabe98f42010-10-07 20:03:19 +1100375 /* argv[2] holds the size of the bzImage */
Simon Glasse8148372020-09-05 14:50:38 -0600376 state.bzimage_size = simple_strtoul(argv[2], NULL, 16);
Gabe Black6f9d9982011-12-05 12:09:27 +0000377 }
378
379 if (argc >= 4)
Simon Glasse8148372020-09-05 14:50:38 -0600380 state.initrd_addr = simple_strtoul(argv[3], NULL, 16);
Gabe Black6f9d9982011-12-05 12:09:27 +0000381 if (argc >= 5)
Simon Glasse8148372020-09-05 14:50:38 -0600382 state.initrd_size = simple_strtoul(argv[4], NULL, 16);
Simon Glassf82cd7b2020-09-05 14:50:49 -0600383 if (argc >= 6) {
384 /*
385 * When the base_ptr is passed in, we assume that the image is
386 * already loaded at the address given by argv[1] and therefore
387 * the original bzImage is somewhere else, or not accessible.
388 * In any case, we don't need access to the bzImage since all
389 * the processing is assumed to be done.
390 *
391 * So set the base_ptr to the given address, use this arg as the
392 * load address and set bzimage_addr to 0 so we know that it
393 * cannot be proceesed (or processed again).
394 */
395 state.base_ptr = (void *)simple_strtoul(argv[5], NULL, 16);
396 state.load_address = state.bzimage_addr;
397 state.bzimage_addr = 0;
398 }
Simon Glass4f960232020-09-05 14:50:51 -0600399 if (argc >= 7)
400 state.cmdline = simple_strtoul(argv[6], NULL, 16);
Graeme Russ95ffaba2010-04-24 00:05:49 +1000401
Simon Glass1d9e4bb2020-09-05 14:50:46 -0600402 return 0;
403}
404
405static int do_zboot_load(struct cmd_tbl *cmdtp, int flag, int argc,
406 char *const argv[])
407{
408 struct boot_params *base_ptr;
409
Simon Glassf82cd7b2020-09-05 14:50:49 -0600410 if (state.base_ptr) {
411 struct boot_params *from = (struct boot_params *)state.base_ptr;
412
413 base_ptr = (struct boot_params *)DEFAULT_SETUP_BASE;
414 printf("Building boot_params at 0x%8.8lx\n", (ulong)base_ptr);
415 memset(base_ptr, '\0', sizeof(*base_ptr));
416 base_ptr->hdr = from->hdr;
417 } else {
418 base_ptr = load_zimage((void *)state.bzimage_addr, state.bzimage_size,
419 &state.load_address);
420 if (!base_ptr) {
421 puts("## Kernel loading failed ...\n");
422 return CMD_RET_FAILURE;
423 }
Graeme Russ95ffaba2010-04-24 00:05:49 +1000424 }
Simon Glass88f1cd62020-09-05 14:50:44 -0600425 state.base_ptr = base_ptr;
Simon Glass126f47c2020-09-05 14:50:48 -0600426 if (env_set_hex("zbootbase", (ulong)base_ptr) ||
427 env_set_hex("zbootaddr", state.load_address))
428 return CMD_RET_FAILURE;
Simon Glasse8148372020-09-05 14:50:38 -0600429
Simon Glass3e597592020-09-05 14:50:47 -0600430 return 0;
431}
432
433static int do_zboot_setup(struct cmd_tbl *cmdtp, int flag, int argc,
434 char *const argv[])
435{
436 struct boot_params *base_ptr = state.base_ptr;
437 int ret;
438
439 if (!base_ptr) {
440 printf("base is not set: use 'zboot load' first\n");
441 return CMD_RET_FAILURE;
442 }
443 ret = setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
Simon Glass4f960232020-09-05 14:50:51 -0600444 0, state.initrd_addr, state.initrd_size,
445 state.cmdline);
Simon Glass3e597592020-09-05 14:50:47 -0600446 if (ret) {
Simon Glass2c363cb2014-10-10 08:21:59 -0600447 puts("Setting up boot parameters failed ...\n");
Simon Glass3e597592020-09-05 14:50:47 -0600448 return CMD_RET_FAILURE;
Gabe Black69370d12011-12-05 12:09:26 +0000449 }
450
Simon Glass88f1cd62020-09-05 14:50:44 -0600451 return 0;
452}
453
Simon Glass6f873f52020-09-05 14:50:45 -0600454static int do_zboot_info(struct cmd_tbl *cmdtp, int flag, int argc,
455 char *const argv[])
456{
457 printf("Kernel loaded at %08lx, setup_base=%p\n",
458 state.load_address, state.base_ptr);
459
460 return 0;
461}
462
Simon Glass88f1cd62020-09-05 14:50:44 -0600463static int do_zboot_go(struct cmd_tbl *cmdtp, int flag, int argc,
464 char *const argv[])
465{
466 int ret;
467
Simon Glasse9d31b32020-09-05 14:50:42 -0600468 disable_interrupts();
Simon Glass88f1cd62020-09-05 14:50:44 -0600469
Gabe Black69370d12011-12-05 12:09:26 +0000470 /* we assume that the kernel is in place */
Simon Glass88f1cd62020-09-05 14:50:44 -0600471 ret = boot_linux_kernel((ulong)state.base_ptr, state.load_address,
472 false);
473 printf("Kernel returned! (err=%d)\n", ret);
474
475 return CMD_RET_FAILURE;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000476}
477
Simon Glass631c2b92020-09-05 14:50:50 -0600478static void print_num(const char *name, ulong value)
479{
480 printf("%-20s: %lx\n", name, value);
481}
482
483static void print_num64(const char *name, u64 value)
484{
485 printf("%-20s: %llx\n", name, value);
486}
487
488static const char *const e820_type_name[E820_COUNT] = {
489 [E820_RAM] = "RAM",
490 [E820_RESERVED] = "Reserved",
491 [E820_ACPI] = "ACPI",
492 [E820_NVS] = "ACPI NVS",
493 [E820_UNUSABLE] = "Unusable",
494};
495
496static const char *const bootloader_id[] = {
497 "LILO",
498 "Loadlin",
499 "bootsect-loader",
500 "Syslinux",
501 "Etherboot/gPXE/iPXE",
502 "ELILO",
503 "undefined",
504 "GRUB",
505 "U-Boot",
506 "Xen",
507 "Gujin",
508 "Qemu",
509 "Arcturus Networks uCbootloader",
510 "kexec-tools",
511 "Extended",
512 "Special",
513 "Reserved",
514 "Minimal Linux Bootloader",
515 "OVMF UEFI virtualization stack",
516};
517
518struct flag_info {
519 uint bit;
520 const char *name;
521};
522
523static struct flag_info load_flags[] = {
524 { LOADED_HIGH, "loaded-high" },
525 { QUIET_FLAG, "quiet" },
526 { KEEP_SEGMENTS, "keep-segments" },
527 { CAN_USE_HEAP, "can-use-heap" },
528};
529
530static struct flag_info xload_flags[] = {
531 { XLF_KERNEL_64, "64-bit-entry" },
532 { XLF_CAN_BE_LOADED_ABOVE_4G, "can-load-above-4gb" },
533 { XLF_EFI_HANDOVER_32, "32-efi-handoff" },
534 { XLF_EFI_HANDOVER_64, "64-efi-handoff" },
535 { XLF_EFI_KEXEC, "kexec-efi-runtime" },
536};
537
538static void print_flags(struct flag_info *flags, int count, uint value)
539{
540 int i;
541
542 printf("%-20s:", "");
543 for (i = 0; i < count; i++) {
544 uint mask = flags[i].bit;
545
546 if (value & mask)
547 printf(" %s", flags[i].name);
548 }
549 printf("\n");
550}
551
552static void show_loader(struct setup_header *hdr)
553{
554 bool version_valid = false;
555 int type, version;
556 const char *name;
557
558 type = hdr->type_of_loader >> 4;
559 version = hdr->type_of_loader & 0xf;
560 if (type == 0xe)
561 type = 0x10 + hdr->ext_loader_type;
562 version |= hdr->ext_loader_ver << 4;
563 if (!hdr->type_of_loader) {
564 name = "pre-2.00 bootloader";
565 } else if (hdr->type_of_loader == 0xff) {
566 name = "unknown";
567 } else if (type < ARRAY_SIZE(bootloader_id)) {
568 name = bootloader_id[type];
569 version_valid = true;
570 } else {
571 name = "undefined";
572 }
573 printf("%20s %s", "", name);
574 if (version_valid)
575 printf(", version %x", version);
576 printf("\n");
577}
578
579int do_zboot_dump(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
580{
581 struct boot_params *base_ptr = state.base_ptr;
582 struct setup_header *hdr;
583 const char *version;
584 int i;
585
586 if (argc > 1)
587 base_ptr = (void *)simple_strtoul(argv[1], NULL, 16);
588 if (!base_ptr) {
589 printf("No zboot setup_base\n");
590 return CMD_RET_FAILURE;
591 }
592 printf("Setup located at %p:\n\n", base_ptr);
593 print_num64("ACPI RSDP addr", base_ptr->acpi_rsdp_addr);
594
595 printf("E820: %d entries\n", base_ptr->e820_entries);
596 if (base_ptr->e820_entries) {
597 printf("%18s %16s %s\n", "Addr", "Size", "Type");
598 for (i = 0; i < base_ptr->e820_entries; i++) {
599 struct e820_entry *entry = &base_ptr->e820_map[i];
600
601 printf("%12llx %10llx %s\n", entry->addr, entry->size,
602 entry->type < E820_COUNT ?
603 e820_type_name[entry->type] :
604 simple_itoa(entry->type));
605 }
606 }
607
608 hdr = &base_ptr->hdr;
609 print_num("Setup sectors", hdr->setup_sects);
610 print_num("Root flags", hdr->root_flags);
611 print_num("Sys size", hdr->syssize);
612 print_num("RAM size", hdr->ram_size);
613 print_num("Video mode", hdr->vid_mode);
614 print_num("Root dev", hdr->root_dev);
615 print_num("Boot flag", hdr->boot_flag);
616 print_num("Jump", hdr->jump);
617 print_num("Header", hdr->header);
618 if (hdr->header == KERNEL_V2_MAGIC)
619 printf("%-20s %s\n", "", "Kernel V2");
620 else
621 printf("%-20s %s\n", "", "Ancient kernel, using version 100");
622 print_num("Version", hdr->version);
623 print_num("Real mode switch", hdr->realmode_swtch);
624 print_num("Start sys", hdr->start_sys);
625 print_num("Kernel version", hdr->kernel_version);
626 version = get_kernel_version(base_ptr, (void *)state.bzimage_addr);
627 if (version)
628 printf(" @%p: %s\n", version, version);
629 print_num("Type of loader", hdr->type_of_loader);
630 show_loader(hdr);
631 print_num("Load flags", hdr->loadflags);
632 print_flags(load_flags, ARRAY_SIZE(load_flags), hdr->loadflags);
633 print_num("Setup move size", hdr->setup_move_size);
634 print_num("Code32 start", hdr->code32_start);
635 print_num("Ramdisk image", hdr->ramdisk_image);
636 print_num("Ramdisk size", hdr->ramdisk_size);
637 print_num("Bootsect kludge", hdr->bootsect_kludge);
638 print_num("Heap end ptr", hdr->heap_end_ptr);
639 print_num("Ext loader ver", hdr->ext_loader_ver);
640 print_num("Ext loader type", hdr->ext_loader_type);
641 print_num("Command line ptr", hdr->cmd_line_ptr);
642 if (hdr->cmd_line_ptr) {
643 printf(" ");
644 /* Use puts() to avoid limits from CONFIG_SYS_PBSIZE */
Simon Glass4f960232020-09-05 14:50:51 -0600645 puts((char *)(ulong)hdr->cmd_line_ptr);
Simon Glass631c2b92020-09-05 14:50:50 -0600646 printf("\n");
647 }
648 print_num("Initrd addr max", hdr->initrd_addr_max);
649 print_num("Kernel alignment", hdr->kernel_alignment);
650 print_num("Relocatable kernel", hdr->relocatable_kernel);
651 print_num("Min alignment", hdr->min_alignment);
652 if (hdr->min_alignment)
653 printf("%-20s: %x\n", "", 1 << hdr->min_alignment);
654 print_num("Xload flags", hdr->xloadflags);
655 print_flags(xload_flags, ARRAY_SIZE(xload_flags), hdr->xloadflags);
656 print_num("Cmdline size", hdr->cmdline_size);
657 print_num("Hardware subarch", hdr->hardware_subarch);
658 print_num64("HW subarch data", hdr->hardware_subarch_data);
659 print_num("Payload offset", hdr->payload_offset);
660 print_num("Payload length", hdr->payload_length);
661 print_num64("Setup data", hdr->setup_data);
662 print_num64("Pref address", hdr->pref_address);
663 print_num("Init size", hdr->init_size);
664 print_num("Handover offset", hdr->handover_offset);
665 if (get_boot_protocol(hdr, false) >= 0x215)
666 print_num("Kernel info offset", hdr->kernel_info_offset);
667
668 return 0;
669}
670
Simon Glass5588e772020-09-05 14:50:43 -0600671/* Note: This defines the complete_zboot() function */
672U_BOOT_SUBCMDS(zboot,
Simon Glass4f960232020-09-05 14:50:51 -0600673 U_BOOT_CMD_MKENT(start, 8, 1, do_zboot_start, "", ""),
Simon Glass1d9e4bb2020-09-05 14:50:46 -0600674 U_BOOT_CMD_MKENT(load, 1, 1, do_zboot_load, "", ""),
Simon Glass3e597592020-09-05 14:50:47 -0600675 U_BOOT_CMD_MKENT(setup, 1, 1, do_zboot_setup, "", ""),
Simon Glass6f873f52020-09-05 14:50:45 -0600676 U_BOOT_CMD_MKENT(info, 1, 1, do_zboot_info, "", ""),
Simon Glass88f1cd62020-09-05 14:50:44 -0600677 U_BOOT_CMD_MKENT(go, 1, 1, do_zboot_go, "", ""),
Simon Glass631c2b92020-09-05 14:50:50 -0600678 U_BOOT_CMD_MKENT(dump, 2, 1, do_zboot_dump, "", ""),
Simon Glass5588e772020-09-05 14:50:43 -0600679)
680
681int do_zboot_states(struct cmd_tbl *cmdtp, int flag, int argc,
682 char *const argv[], int state_mask)
683{
684 int i;
685
686 for (i = 0; i < ZBOOT_STATE_COUNT; i++) {
687 struct cmd_tbl *cmd = &zboot_subcmds[i];
688 int mask = 1 << i;
689 int ret;
690
691 if (mask & state_mask) {
692 ret = cmd->cmd(cmd, flag, argc, argv);
693 if (ret)
694 return ret;
695 }
696 }
697
698 return 0;
699}
700
701int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc,
702 char *const argv[], int *repeatable)
703{
704 /* determine if we have a sub command */
705 if (argc > 1) {
706 char *endp;
707
708 simple_strtoul(argv[1], &endp, 16);
709 /*
710 * endp pointing to nul means that argv[1] was just a valid
711 * number, so pass it along to the normal processing
712 */
713 if (*endp)
714 return do_zboot(cmdtp, flag, argc, argv, repeatable);
715 }
716
Simon Glass88f1cd62020-09-05 14:50:44 -0600717 do_zboot_states(cmdtp, flag, argc, argv, ZBOOT_STATE_START |
Simon Glass3e597592020-09-05 14:50:47 -0600718 ZBOOT_STATE_LOAD | ZBOOT_STATE_SETUP |
719 ZBOOT_STATE_INFO | ZBOOT_STATE_GO);
Simon Glass5588e772020-09-05 14:50:43 -0600720
721 return CMD_RET_FAILURE;
722}
723
724U_BOOT_CMDREP_COMPLETE(
Simon Glass4f960232020-09-05 14:50:51 -0600725 zboot, 8, do_zboot_parent, "Boot bzImage",
726 "[addr] [size] [initrd addr] [initrd size] [setup] [cmdline]\n"
Gabe Black6f9d9982011-12-05 12:09:27 +0000727 " addr - The optional starting address of the bzimage.\n"
728 " If not set it defaults to the environment\n"
729 " variable \"fileaddr\".\n"
730 " size - The optional size of the bzimage. Defaults to\n"
731 " zero.\n"
732 " initrd addr - The address of the initrd image to use, if any.\n"
733 " initrd size - The size of the initrd image to use, if any.\n"
Simon Glassf82cd7b2020-09-05 14:50:49 -0600734 " setup - The address of the kernel setup region, if this\n"
735 " is not at addr\n"
Simon Glass4f960232020-09-05 14:50:51 -0600736 " cmdline - The address of the kernel command line, to\n"
737 " override U-Boot's normal cmdline generation\n"
Simon Glass5588e772020-09-05 14:50:43 -0600738 "\n"
739 "Sub-commands to do part of the zboot sequence:\n"
Simon Glass88f1cd62020-09-05 14:50:44 -0600740 "\tstart [addr [arg ...]] - specify arguments\n"
Simon Glass1d9e4bb2020-09-05 14:50:46 -0600741 "\tload - load OS image\n"
Simon Glass3e597592020-09-05 14:50:47 -0600742 "\tsetup - set up table\n"
Simon Glass6f873f52020-09-05 14:50:45 -0600743 "\tinfo - show summary info\n"
Simon Glass631c2b92020-09-05 14:50:50 -0600744 "\tgo - start OS\n"
745 "\tdump [addr] - dump info (optional address of boot params)",
Simon Glass5588e772020-09-05 14:50:43 -0600746 complete_zboot
Graeme Russ95ffaba2010-04-24 00:05:49 +1000747);