blob: a00964cc8d9c719b50d809479865c2d88e36c296 [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
15#include <common.h>
Simon Glass09140112020-05-10 11:40:03 -060016#include <command.h>
Simon Glasscdbff9f2019-08-01 09:46:50 -060017#include <env.h>
Simon Glass36bf4462019-11-14 12:57:42 -070018#include <irq_func.h>
Ivan Gorinov5d732922018-03-26 18:06:54 -070019#include <malloc.h>
Simon Glass776cc202020-04-08 16:57:36 -060020#include <acpi/acpi_table.h>
wdenk2262cfe2002-11-18 00:14:45 +000021#include <asm/io.h>
22#include <asm/ptrace.h>
23#include <asm/zimage.h>
wdenk2262cfe2002-11-18 00:14:45 +000024#include <asm/byteorder.h>
Simon Glass0d0ba592014-10-19 21:11:20 -060025#include <asm/bootm.h>
Graeme Russ95ffaba2010-04-24 00:05:49 +100026#include <asm/bootparam.h>
Vadim Bendebury3cdc18a2012-10-23 18:04:34 +000027#ifdef CONFIG_SYS_COREBOOT
28#include <asm/arch/timestamp.h>
29#endif
Stefan Reinauer61e0ea92012-10-23 18:04:37 +000030#include <linux/compiler.h>
Ivan Gorinov5d732922018-03-26 18:06:54 -070031#include <linux/libfdt.h>
wdenk2262cfe2002-11-18 00:14:45 +000032
33/*
34 * Memory lay-out:
wdenk8bde7f72003-06-27 21:31:46 +000035 *
wdenk2262cfe2002-11-18 00:14:45 +000036 * relative to setup_base (which is 0x90000 currently)
wdenk8bde7f72003-06-27 21:31:46 +000037 *
38 * 0x0000-0x7FFF Real mode kernel
wdenk2262cfe2002-11-18 00:14:45 +000039 * 0x8000-0x8FFF Stack and heap
40 * 0x9000-0x90FF Kernel command line
41 */
Graeme Russ83088af2011-11-08 02:33:15 +000042#define DEFAULT_SETUP_BASE 0x90000
43#define COMMAND_LINE_OFFSET 0x9000
44#define HEAP_END_OFFSET 0x8e00
wdenk2262cfe2002-11-18 00:14:45 +000045
Graeme Russ83088af2011-11-08 02:33:15 +000046#define COMMAND_LINE_SIZE 2048
wdenk2262cfe2002-11-18 00:14:45 +000047
Simon Glasse8148372020-09-05 14:50:38 -060048/**
49 * struct zboot_state - Current state of the boot
50 *
51 * @bzimage_addr: Address of the bzImage to boot
52 * @bzimage_size: Size of the bzImage, or 0 to detect this
53 * @initrd_addr: Address of the initial ramdisk, or 0 if none
54 * @initrd_size: Size of the initial ramdisk, or 0 if none
55 * @load_address: Address where the bzImage is moved before booting, either
56 * BZIMAGE_LOAD_ADDR or ZIMAGE_LOAD_ADDR
Simon Glass88f1cd62020-09-05 14:50:44 -060057 * @base_ptr: Pointer to the boot parameters, typically at address
58 * DEFAULT_SETUP_BASE
Simon Glass4f960232020-09-05 14:50:51 -060059 * @cmdline: Address of 'override' command line, or 0 to use the one in the
60 * setup block
Simon Glasse8148372020-09-05 14:50:38 -060061 */
62struct zboot_state {
63 ulong bzimage_addr;
64 ulong bzimage_size;
65 ulong initrd_addr;
66 ulong initrd_size;
67 ulong load_address;
Simon Glass88f1cd62020-09-05 14:50:44 -060068 struct boot_params *base_ptr;
Simon Glass4f960232020-09-05 14:50:51 -060069 ulong cmdline;
Simon Glasse8148372020-09-05 14:50:38 -060070} state;
71
Simon Glass5588e772020-09-05 14:50:43 -060072enum {
73 ZBOOT_STATE_START = BIT(0),
Simon Glass1d9e4bb2020-09-05 14:50:46 -060074 ZBOOT_STATE_LOAD = BIT(1),
Simon Glass3e597592020-09-05 14:50:47 -060075 ZBOOT_STATE_SETUP = BIT(2),
76 ZBOOT_STATE_INFO = BIT(3),
77 ZBOOT_STATE_GO = BIT(4),
Simon Glass5588e772020-09-05 14:50:43 -060078
Simon Glass631c2b92020-09-05 14:50:50 -060079 /* This one doesn't execute automatically, so stop the count before 5 */
80 ZBOOT_STATE_DUMP = BIT(5),
Simon Glass3e597592020-09-05 14:50:47 -060081 ZBOOT_STATE_COUNT = 5,
Simon Glass5588e772020-09-05 14:50:43 -060082};
83
wdenk2262cfe2002-11-18 00:14:45 +000084static void build_command_line(char *command_line, int auto_boot)
85{
86 char *env_command_line;
wdenk8bde7f72003-06-27 21:31:46 +000087
wdenk2262cfe2002-11-18 00:14:45 +000088 command_line[0] = '\0';
wdenk8bde7f72003-06-27 21:31:46 +000089
Simon Glass00caae62017-08-03 12:22:12 -060090 env_command_line = env_get("bootargs");
wdenk8bde7f72003-06-27 21:31:46 +000091
wdenk2262cfe2002-11-18 00:14:45 +000092 /* set console= argument if we use a serial console */
Graeme Russ83088af2011-11-08 02:33:15 +000093 if (!strstr(env_command_line, "console=")) {
Simon Glass00caae62017-08-03 12:22:12 -060094 if (!strcmp(env_get("stdout"), "serial")) {
wdenk8bde7f72003-06-27 21:31:46 +000095
wdenk2262cfe2002-11-18 00:14:45 +000096 /* We seem to use serial console */
wdenk8bde7f72003-06-27 21:31:46 +000097 sprintf(command_line, "console=ttyS0,%s ",
Simon Glass00caae62017-08-03 12:22:12 -060098 env_get("baudrate"));
wdenk2262cfe2002-11-18 00:14:45 +000099 }
100 }
wdenk8bde7f72003-06-27 21:31:46 +0000101
Graeme Russ83088af2011-11-08 02:33:15 +0000102 if (auto_boot)
wdenk2262cfe2002-11-18 00:14:45 +0000103 strcat(command_line, "auto ");
wdenk8bde7f72003-06-27 21:31:46 +0000104
Graeme Russ83088af2011-11-08 02:33:15 +0000105 if (env_command_line)
wdenk2262cfe2002-11-18 00:14:45 +0000106 strcat(command_line, env_command_line);
wdenk8bde7f72003-06-27 21:31:46 +0000107
wdenk2262cfe2002-11-18 00:14:45 +0000108 printf("Kernel command line: \"%s\"\n", command_line);
109}
110
Gabe Black69370d12011-12-05 12:09:26 +0000111static int kernel_magic_ok(struct setup_header *hdr)
112{
113 if (KERNEL_MAGIC != hdr->boot_flag) {
114 printf("Error: Invalid Boot Flag "
115 "(found 0x%04x, expected 0x%04x)\n",
116 hdr->boot_flag, KERNEL_MAGIC);
117 return 0;
118 } else {
119 printf("Valid Boot Flag\n");
120 return 1;
121 }
122}
123
Simon Glassc038f3b2020-09-05 14:50:40 -0600124static int get_boot_protocol(struct setup_header *hdr, bool verbose)
Gabe Black69370d12011-12-05 12:09:26 +0000125{
126 if (hdr->header == KERNEL_V2_MAGIC) {
Simon Glassc038f3b2020-09-05 14:50:40 -0600127 if (verbose)
128 printf("Magic signature found\n");
Gabe Black69370d12011-12-05 12:09:26 +0000129 return hdr->version;
130 } else {
131 /* Very old kernel */
Simon Glassc038f3b2020-09-05 14:50:40 -0600132 if (verbose)
133 printf("Magic signature not found\n");
Gabe Black69370d12011-12-05 12:09:26 +0000134 return 0x0100;
135 }
136}
137
Ivan Gorinov5d732922018-03-26 18:06:54 -0700138static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob)
139{
Simon Glassc038f3b2020-09-05 14:50:40 -0600140 int bootproto = get_boot_protocol(hdr, false);
Ivan Gorinov5d732922018-03-26 18:06:54 -0700141 struct setup_data *sd;
142 int size;
143
144 if (bootproto < 0x0209)
145 return -ENOTSUPP;
146
147 if (!fdt_blob)
148 return 0;
149
150 size = fdt_totalsize(fdt_blob);
151 if (size < 0)
152 return -EINVAL;
153
154 size += sizeof(struct setup_data);
155 sd = (struct setup_data *)malloc(size);
156 if (!sd) {
157 printf("Not enough memory for DTB setup data\n");
158 return -ENOMEM;
159 }
160
161 sd->next = hdr->setup_data;
162 sd->type = SETUP_DTB;
163 sd->len = fdt_totalsize(fdt_blob);
164 memcpy(sd->data, fdt_blob, sd->len);
165 hdr->setup_data = (unsigned long)sd;
166
167 return 0;
168}
169
Simon Glassc038f3b2020-09-05 14:50:40 -0600170static const char *get_kernel_version(struct boot_params *params,
171 void *kernel_base)
172{
173 struct setup_header *hdr = &params->hdr;
174 int bootproto;
175
176 bootproto = get_boot_protocol(hdr, false);
177 if (bootproto < 0x0200 || hdr->setup_sects < 15)
178 return NULL;
179
180 return kernel_base + hdr->kernel_version + 0x200;
181}
182
Gabe Black69370d12011-12-05 12:09:26 +0000183struct boot_params *load_zimage(char *image, unsigned long kernel_size,
Simon Glass76539382014-10-10 08:21:56 -0600184 ulong *load_addressp)
wdenk2262cfe2002-11-18 00:14:45 +0000185{
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000186 struct boot_params *setup_base;
Simon Glassc038f3b2020-09-05 14:50:40 -0600187 const char *version;
wdenk2262cfe2002-11-18 00:14:45 +0000188 int setup_size;
189 int bootproto;
190 int big_image;
wdenk8bde7f72003-06-27 21:31:46 +0000191
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000192 struct boot_params *params = (struct boot_params *)image;
193 struct setup_header *hdr = &params->hdr;
wdenk8bde7f72003-06-27 21:31:46 +0000194
Graeme Russ83088af2011-11-08 02:33:15 +0000195 /* base address for real-mode segment */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000196 setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
wdenk8bde7f72003-06-27 21:31:46 +0000197
Gabe Black69370d12011-12-05 12:09:26 +0000198 if (!kernel_magic_ok(hdr))
wdenk2262cfe2002-11-18 00:14:45 +0000199 return 0;
wdenk8bde7f72003-06-27 21:31:46 +0000200
wdenk2262cfe2002-11-18 00:14:45 +0000201 /* determine size of setup */
Graeme Russ95ffaba2010-04-24 00:05:49 +1000202 if (0 == hdr->setup_sects) {
203 printf("Setup Sectors = 0 (defaulting to 4)\n");
wdenk2262cfe2002-11-18 00:14:45 +0000204 setup_size = 5 * 512;
205 } else {
Graeme Russ95ffaba2010-04-24 00:05:49 +1000206 setup_size = (hdr->setup_sects + 1) * 512;
wdenk2262cfe2002-11-18 00:14:45 +0000207 }
wdenk8bde7f72003-06-27 21:31:46 +0000208
Graeme Russ95ffaba2010-04-24 00:05:49 +1000209 printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
210
Graeme Russ83088af2011-11-08 02:33:15 +0000211 if (setup_size > SETUP_MAX_SIZE)
wdenk2262cfe2002-11-18 00:14:45 +0000212 printf("Error: Setup is too large (%d bytes)\n", setup_size);
wdenk8bde7f72003-06-27 21:31:46 +0000213
Gabe Black69370d12011-12-05 12:09:26 +0000214 /* determine boot protocol version */
Simon Glassc038f3b2020-09-05 14:50:40 -0600215 bootproto = get_boot_protocol(hdr, true);
Gabe Black69370d12011-12-05 12:09:26 +0000216
217 printf("Using boot protocol version %x.%02x\n",
218 (bootproto & 0xff00) >> 8, bootproto & 0xff);
219
Simon Glassc038f3b2020-09-05 14:50:40 -0600220 version = get_kernel_version(params, image);
221 if (version)
222 printf("Linux kernel version %s\n", version);
223 else
224 printf("Setup Sectors < 15 - Cannot print kernel version\n");
Gabe Black69370d12011-12-05 12:09:26 +0000225
wdenk2262cfe2002-11-18 00:14:45 +0000226 /* Determine image type */
Graeme Russ83088af2011-11-08 02:33:15 +0000227 big_image = (bootproto >= 0x0200) &&
228 (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk8bde7f72003-06-27 21:31:46 +0000229
Graeme Russ95ffaba2010-04-24 00:05:49 +1000230 /* Determine load address */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000231 if (big_image)
Simon Glass76539382014-10-10 08:21:56 -0600232 *load_addressp = BZIMAGE_LOAD_ADDR;
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000233 else
Simon Glass76539382014-10-10 08:21:56 -0600234 *load_addressp = ZIMAGE_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000235
Gabe Black233dbc12011-12-05 12:09:24 +0000236 printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
237 memset(setup_base, 0, sizeof(*setup_base));
238 setup_base->hdr = params->hdr;
wdenk8bde7f72003-06-27 21:31:46 +0000239
Gabe Black69370d12011-12-05 12:09:26 +0000240 if (bootproto >= 0x0204)
241 kernel_size = hdr->syssize * 16;
242 else
243 kernel_size -= setup_size;
wdenk8bde7f72003-06-27 21:31:46 +0000244
wdenk8bde7f72003-06-27 21:31:46 +0000245 if (bootproto == 0x0100) {
Graeme Russ83088af2011-11-08 02:33:15 +0000246 /*
247 * A very old kernel MUST have its real-mode code
248 * loaded at 0x90000
249 */
Simon Glass42fd8c12017-01-16 07:03:35 -0700250 if ((ulong)setup_base != 0x90000) {
wdenk2262cfe2002-11-18 00:14:45 +0000251 /* Copy the real-mode kernel */
Graeme Russ83088af2011-11-08 02:33:15 +0000252 memmove((void *)0x90000, setup_base, setup_size);
wdenk8bde7f72003-06-27 21:31:46 +0000253
Graeme Russ83088af2011-11-08 02:33:15 +0000254 /* Copy the command line */
255 memmove((void *)0x99000,
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000256 (u8 *)setup_base + COMMAND_LINE_OFFSET,
Graeme Russ83088af2011-11-08 02:33:15 +0000257 COMMAND_LINE_SIZE);
258
259 /* Relocated */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000260 setup_base = (struct boot_params *)0x90000;
wdenk2262cfe2002-11-18 00:14:45 +0000261 }
wdenk8bde7f72003-06-27 21:31:46 +0000262
wdenk2262cfe2002-11-18 00:14:45 +0000263 /* It is recommended to clear memory up to the 32K mark */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000264 memset((u8 *)0x90000 + setup_size, 0,
265 SETUP_MAX_SIZE - setup_size);
wdenk2262cfe2002-11-18 00:14:45 +0000266 }
wdenk8bde7f72003-06-27 21:31:46 +0000267
Gabe Black69370d12011-12-05 12:09:26 +0000268 if (big_image) {
269 if (kernel_size > BZIMAGE_MAX_SIZE) {
270 printf("Error: bzImage kernel too big! "
271 "(size: %ld, max: %d)\n",
272 kernel_size, BZIMAGE_MAX_SIZE);
273 return 0;
274 }
275 } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
276 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
277 kernel_size, ZIMAGE_MAX_SIZE);
278 return 0;
279 }
Graeme Russ95ffaba2010-04-24 00:05:49 +1000280
Simon Glass76539382014-10-10 08:21:56 -0600281 printf("Loading %s at address %lx (%ld bytes)\n",
282 big_image ? "bzImage" : "zImage", *load_addressp, kernel_size);
Gabe Black69370d12011-12-05 12:09:26 +0000283
Simon Glass76539382014-10-10 08:21:56 -0600284 memmove((void *)*load_addressp, image + setup_size, kernel_size);
Gabe Black69370d12011-12-05 12:09:26 +0000285
286 return setup_base;
287}
288
289int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
Simon Glass4f960232020-09-05 14:50:51 -0600290 ulong initrd_addr, ulong initrd_size, ulong cmdline_force)
Gabe Black69370d12011-12-05 12:09:26 +0000291{
292 struct setup_header *hdr = &setup_base->hdr;
Simon Glassc038f3b2020-09-05 14:50:40 -0600293 int bootproto = get_boot_protocol(hdr, false);
Gabe Black69370d12011-12-05 12:09:26 +0000294
Gabe Black69370d12011-12-05 12:09:26 +0000295 setup_base->e820_entries = install_e820_map(
296 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Gabe Black69370d12011-12-05 12:09:26 +0000297
298 if (bootproto == 0x0100) {
299 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
300 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
301 }
wdenk2262cfe2002-11-18 00:14:45 +0000302 if (bootproto >= 0x0200) {
Simon Glass00630f62020-09-05 14:50:41 -0600303 hdr->type_of_loader = 0x80; /* U-Boot version 0 */
wdenk2262cfe2002-11-18 00:14:45 +0000304 if (initrd_addr) {
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000305 printf("Initial RAM disk at linear address "
306 "0x%08lx, size %ld bytes\n",
wdenk2262cfe2002-11-18 00:14:45 +0000307 initrd_addr, initrd_size);
wdenk8bde7f72003-06-27 21:31:46 +0000308
Graeme Russ95ffaba2010-04-24 00:05:49 +1000309 hdr->ramdisk_image = initrd_addr;
310 hdr->ramdisk_size = initrd_size;
wdenk2262cfe2002-11-18 00:14:45 +0000311 }
312 }
wdenk8bde7f72003-06-27 21:31:46 +0000313
wdenk2262cfe2002-11-18 00:14:45 +0000314 if (bootproto >= 0x0201) {
Graeme Russ95ffaba2010-04-24 00:05:49 +1000315 hdr->heap_end_ptr = HEAP_END_OFFSET;
316 hdr->loadflags |= HEAP_FLAG;
wdenk2262cfe2002-11-18 00:14:45 +0000317 }
wdenk8bde7f72003-06-27 21:31:46 +0000318
Simon Glass97d1e0c2014-10-19 21:11:21 -0600319 if (cmd_line) {
320 if (bootproto >= 0x0202) {
321 hdr->cmd_line_ptr = (uintptr_t)cmd_line;
322 } else if (bootproto >= 0x0200) {
323 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
324 setup_base->screen_info.cl_offset =
325 (uintptr_t)cmd_line - (uintptr_t)setup_base;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000326
Simon Glass97d1e0c2014-10-19 21:11:21 -0600327 hdr->setup_move_size = 0x9100;
328 }
329
330 /* build command line at COMMAND_LINE_OFFSET */
Simon Glass4f960232020-09-05 14:50:51 -0600331 if (cmdline_force)
332 strcpy(cmd_line, (char *)cmdline_force);
333 else
334 build_command_line(cmd_line, auto_boot);
wdenk2262cfe2002-11-18 00:14:45 +0000335 }
wdenk2262cfe2002-11-18 00:14:45 +0000336
Simon Glass30b372d2020-09-05 14:50:39 -0600337 if (IS_ENABLED(CONFIG_INTEL_MID) && bootproto >= 0x0207)
Andy Shevchenko378960d2018-01-10 19:40:14 +0200338 hdr->hardware_subarch = X86_SUBARCH_INTEL_MID;
Andy Shevchenko378960d2018-01-10 19:40:14 +0200339
Simon Glass30b372d2020-09-05 14:50:39 -0600340 if (IS_ENABLED(CONFIG_GENERATE_ACPI_TABLE))
341 setup_base->acpi_rsdp_addr = acpi_get_rsdp_addr();
Andy Shevchenkod905aa82019-09-13 18:42:00 +0300342
Ivan Gorinov5d732922018-03-26 18:06:54 -0700343 setup_device_tree(hdr, (const void *)env_get_hex("fdtaddr", 0));
Bin Menga4520022015-07-06 16:31:36 +0800344 setup_video(&setup_base->screen_info);
345
Simon Glass30b372d2020-09-05 14:50:39 -0600346 if (IS_ENABLED(CONFIG_EFI_STUB))
347 setup_efi_info(&setup_base->efi_info);
Bin Meng1fdeacd2018-08-23 08:24:10 -0700348
Gabe Black69370d12011-12-05 12:09:26 +0000349 return 0;
wdenk2262cfe2002-11-18 00:14:45 +0000350}
351
Simon Glass5588e772020-09-05 14:50:43 -0600352static int do_zboot_start(struct cmd_tbl *cmdtp, int flag, int argc,
353 char *const argv[])
Graeme Russ95ffaba2010-04-24 00:05:49 +1000354{
Simon Glass5588e772020-09-05 14:50:43 -0600355 const char *s;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000356
Simon Glasse8148372020-09-05 14:50:38 -0600357 memset(&state, '\0', sizeof(state));
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000358 if (argc >= 2) {
Graeme Russabe98f42010-10-07 20:03:19 +1100359 /* argv[1] holds the address of the bzImage */
360 s = argv[1];
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000361 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600362 s = env_get("fileaddr");
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000363 }
Graeme Russ95ffaba2010-04-24 00:05:49 +1000364
Graeme Russabe98f42010-10-07 20:03:19 +1100365 if (s)
Simon Glasse8148372020-09-05 14:50:38 -0600366 state.bzimage_addr = simple_strtoul(s, NULL, 16);
Graeme Russabe98f42010-10-07 20:03:19 +1100367
Gabe Black6f9d9982011-12-05 12:09:27 +0000368 if (argc >= 3) {
Graeme Russabe98f42010-10-07 20:03:19 +1100369 /* argv[2] holds the size of the bzImage */
Simon Glasse8148372020-09-05 14:50:38 -0600370 state.bzimage_size = simple_strtoul(argv[2], NULL, 16);
Gabe Black6f9d9982011-12-05 12:09:27 +0000371 }
372
373 if (argc >= 4)
Simon Glasse8148372020-09-05 14:50:38 -0600374 state.initrd_addr = simple_strtoul(argv[3], NULL, 16);
Gabe Black6f9d9982011-12-05 12:09:27 +0000375 if (argc >= 5)
Simon Glasse8148372020-09-05 14:50:38 -0600376 state.initrd_size = simple_strtoul(argv[4], NULL, 16);
Simon Glassf82cd7b2020-09-05 14:50:49 -0600377 if (argc >= 6) {
378 /*
379 * When the base_ptr is passed in, we assume that the image is
380 * already loaded at the address given by argv[1] and therefore
381 * the original bzImage is somewhere else, or not accessible.
382 * In any case, we don't need access to the bzImage since all
383 * the processing is assumed to be done.
384 *
385 * So set the base_ptr to the given address, use this arg as the
386 * load address and set bzimage_addr to 0 so we know that it
387 * cannot be proceesed (or processed again).
388 */
389 state.base_ptr = (void *)simple_strtoul(argv[5], NULL, 16);
390 state.load_address = state.bzimage_addr;
391 state.bzimage_addr = 0;
392 }
Simon Glass4f960232020-09-05 14:50:51 -0600393 if (argc >= 7)
394 state.cmdline = simple_strtoul(argv[6], NULL, 16);
Graeme Russ95ffaba2010-04-24 00:05:49 +1000395
Simon Glass1d9e4bb2020-09-05 14:50:46 -0600396 return 0;
397}
398
399static int do_zboot_load(struct cmd_tbl *cmdtp, int flag, int argc,
400 char *const argv[])
401{
402 struct boot_params *base_ptr;
403
Simon Glassf82cd7b2020-09-05 14:50:49 -0600404 if (state.base_ptr) {
405 struct boot_params *from = (struct boot_params *)state.base_ptr;
406
407 base_ptr = (struct boot_params *)DEFAULT_SETUP_BASE;
408 printf("Building boot_params at 0x%8.8lx\n", (ulong)base_ptr);
409 memset(base_ptr, '\0', sizeof(*base_ptr));
410 base_ptr->hdr = from->hdr;
411 } else {
412 base_ptr = load_zimage((void *)state.bzimage_addr, state.bzimage_size,
413 &state.load_address);
414 if (!base_ptr) {
415 puts("## Kernel loading failed ...\n");
416 return CMD_RET_FAILURE;
417 }
Graeme Russ95ffaba2010-04-24 00:05:49 +1000418 }
Simon Glass88f1cd62020-09-05 14:50:44 -0600419 state.base_ptr = base_ptr;
Simon Glass126f47c2020-09-05 14:50:48 -0600420 if (env_set_hex("zbootbase", (ulong)base_ptr) ||
421 env_set_hex("zbootaddr", state.load_address))
422 return CMD_RET_FAILURE;
Simon Glasse8148372020-09-05 14:50:38 -0600423
Simon Glass3e597592020-09-05 14:50:47 -0600424 return 0;
425}
426
427static int do_zboot_setup(struct cmd_tbl *cmdtp, int flag, int argc,
428 char *const argv[])
429{
430 struct boot_params *base_ptr = state.base_ptr;
431 int ret;
432
433 if (!base_ptr) {
434 printf("base is not set: use 'zboot load' first\n");
435 return CMD_RET_FAILURE;
436 }
437 ret = setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
Simon Glass4f960232020-09-05 14:50:51 -0600438 0, state.initrd_addr, state.initrd_size,
439 state.cmdline);
Simon Glass3e597592020-09-05 14:50:47 -0600440 if (ret) {
Simon Glass2c363cb2014-10-10 08:21:59 -0600441 puts("Setting up boot parameters failed ...\n");
Simon Glass3e597592020-09-05 14:50:47 -0600442 return CMD_RET_FAILURE;
Gabe Black69370d12011-12-05 12:09:26 +0000443 }
444
Simon Glass88f1cd62020-09-05 14:50:44 -0600445 return 0;
446}
447
Simon Glass6f873f52020-09-05 14:50:45 -0600448static int do_zboot_info(struct cmd_tbl *cmdtp, int flag, int argc,
449 char *const argv[])
450{
451 printf("Kernel loaded at %08lx, setup_base=%p\n",
452 state.load_address, state.base_ptr);
453
454 return 0;
455}
456
Simon Glass88f1cd62020-09-05 14:50:44 -0600457static int do_zboot_go(struct cmd_tbl *cmdtp, int flag, int argc,
458 char *const argv[])
459{
460 int ret;
461
Simon Glasse9d31b32020-09-05 14:50:42 -0600462 disable_interrupts();
Simon Glass88f1cd62020-09-05 14:50:44 -0600463
Gabe Black69370d12011-12-05 12:09:26 +0000464 /* we assume that the kernel is in place */
Simon Glass88f1cd62020-09-05 14:50:44 -0600465 ret = boot_linux_kernel((ulong)state.base_ptr, state.load_address,
466 false);
467 printf("Kernel returned! (err=%d)\n", ret);
468
469 return CMD_RET_FAILURE;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000470}
471
Simon Glass631c2b92020-09-05 14:50:50 -0600472static void print_num(const char *name, ulong value)
473{
474 printf("%-20s: %lx\n", name, value);
475}
476
477static void print_num64(const char *name, u64 value)
478{
479 printf("%-20s: %llx\n", name, value);
480}
481
482static const char *const e820_type_name[E820_COUNT] = {
483 [E820_RAM] = "RAM",
484 [E820_RESERVED] = "Reserved",
485 [E820_ACPI] = "ACPI",
486 [E820_NVS] = "ACPI NVS",
487 [E820_UNUSABLE] = "Unusable",
488};
489
490static const char *const bootloader_id[] = {
491 "LILO",
492 "Loadlin",
493 "bootsect-loader",
494 "Syslinux",
495 "Etherboot/gPXE/iPXE",
496 "ELILO",
497 "undefined",
498 "GRUB",
499 "U-Boot",
500 "Xen",
501 "Gujin",
502 "Qemu",
503 "Arcturus Networks uCbootloader",
504 "kexec-tools",
505 "Extended",
506 "Special",
507 "Reserved",
508 "Minimal Linux Bootloader",
509 "OVMF UEFI virtualization stack",
510};
511
512struct flag_info {
513 uint bit;
514 const char *name;
515};
516
517static struct flag_info load_flags[] = {
518 { LOADED_HIGH, "loaded-high" },
519 { QUIET_FLAG, "quiet" },
520 { KEEP_SEGMENTS, "keep-segments" },
521 { CAN_USE_HEAP, "can-use-heap" },
522};
523
524static struct flag_info xload_flags[] = {
525 { XLF_KERNEL_64, "64-bit-entry" },
526 { XLF_CAN_BE_LOADED_ABOVE_4G, "can-load-above-4gb" },
527 { XLF_EFI_HANDOVER_32, "32-efi-handoff" },
528 { XLF_EFI_HANDOVER_64, "64-efi-handoff" },
529 { XLF_EFI_KEXEC, "kexec-efi-runtime" },
530};
531
532static void print_flags(struct flag_info *flags, int count, uint value)
533{
534 int i;
535
536 printf("%-20s:", "");
537 for (i = 0; i < count; i++) {
538 uint mask = flags[i].bit;
539
540 if (value & mask)
541 printf(" %s", flags[i].name);
542 }
543 printf("\n");
544}
545
546static void show_loader(struct setup_header *hdr)
547{
548 bool version_valid = false;
549 int type, version;
550 const char *name;
551
552 type = hdr->type_of_loader >> 4;
553 version = hdr->type_of_loader & 0xf;
554 if (type == 0xe)
555 type = 0x10 + hdr->ext_loader_type;
556 version |= hdr->ext_loader_ver << 4;
557 if (!hdr->type_of_loader) {
558 name = "pre-2.00 bootloader";
559 } else if (hdr->type_of_loader == 0xff) {
560 name = "unknown";
561 } else if (type < ARRAY_SIZE(bootloader_id)) {
562 name = bootloader_id[type];
563 version_valid = true;
564 } else {
565 name = "undefined";
566 }
567 printf("%20s %s", "", name);
568 if (version_valid)
569 printf(", version %x", version);
570 printf("\n");
571}
572
573int do_zboot_dump(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
574{
575 struct boot_params *base_ptr = state.base_ptr;
576 struct setup_header *hdr;
577 const char *version;
578 int i;
579
580 if (argc > 1)
581 base_ptr = (void *)simple_strtoul(argv[1], NULL, 16);
582 if (!base_ptr) {
583 printf("No zboot setup_base\n");
584 return CMD_RET_FAILURE;
585 }
586 printf("Setup located at %p:\n\n", base_ptr);
587 print_num64("ACPI RSDP addr", base_ptr->acpi_rsdp_addr);
588
589 printf("E820: %d entries\n", base_ptr->e820_entries);
590 if (base_ptr->e820_entries) {
591 printf("%18s %16s %s\n", "Addr", "Size", "Type");
592 for (i = 0; i < base_ptr->e820_entries; i++) {
593 struct e820_entry *entry = &base_ptr->e820_map[i];
594
595 printf("%12llx %10llx %s\n", entry->addr, entry->size,
596 entry->type < E820_COUNT ?
597 e820_type_name[entry->type] :
598 simple_itoa(entry->type));
599 }
600 }
601
602 hdr = &base_ptr->hdr;
603 print_num("Setup sectors", hdr->setup_sects);
604 print_num("Root flags", hdr->root_flags);
605 print_num("Sys size", hdr->syssize);
606 print_num("RAM size", hdr->ram_size);
607 print_num("Video mode", hdr->vid_mode);
608 print_num("Root dev", hdr->root_dev);
609 print_num("Boot flag", hdr->boot_flag);
610 print_num("Jump", hdr->jump);
611 print_num("Header", hdr->header);
612 if (hdr->header == KERNEL_V2_MAGIC)
613 printf("%-20s %s\n", "", "Kernel V2");
614 else
615 printf("%-20s %s\n", "", "Ancient kernel, using version 100");
616 print_num("Version", hdr->version);
617 print_num("Real mode switch", hdr->realmode_swtch);
618 print_num("Start sys", hdr->start_sys);
619 print_num("Kernel version", hdr->kernel_version);
620 version = get_kernel_version(base_ptr, (void *)state.bzimage_addr);
621 if (version)
622 printf(" @%p: %s\n", version, version);
623 print_num("Type of loader", hdr->type_of_loader);
624 show_loader(hdr);
625 print_num("Load flags", hdr->loadflags);
626 print_flags(load_flags, ARRAY_SIZE(load_flags), hdr->loadflags);
627 print_num("Setup move size", hdr->setup_move_size);
628 print_num("Code32 start", hdr->code32_start);
629 print_num("Ramdisk image", hdr->ramdisk_image);
630 print_num("Ramdisk size", hdr->ramdisk_size);
631 print_num("Bootsect kludge", hdr->bootsect_kludge);
632 print_num("Heap end ptr", hdr->heap_end_ptr);
633 print_num("Ext loader ver", hdr->ext_loader_ver);
634 print_num("Ext loader type", hdr->ext_loader_type);
635 print_num("Command line ptr", hdr->cmd_line_ptr);
636 if (hdr->cmd_line_ptr) {
637 printf(" ");
638 /* Use puts() to avoid limits from CONFIG_SYS_PBSIZE */
Simon Glass4f960232020-09-05 14:50:51 -0600639 puts((char *)(ulong)hdr->cmd_line_ptr);
Simon Glass631c2b92020-09-05 14:50:50 -0600640 printf("\n");
641 }
642 print_num("Initrd addr max", hdr->initrd_addr_max);
643 print_num("Kernel alignment", hdr->kernel_alignment);
644 print_num("Relocatable kernel", hdr->relocatable_kernel);
645 print_num("Min alignment", hdr->min_alignment);
646 if (hdr->min_alignment)
647 printf("%-20s: %x\n", "", 1 << hdr->min_alignment);
648 print_num("Xload flags", hdr->xloadflags);
649 print_flags(xload_flags, ARRAY_SIZE(xload_flags), hdr->xloadflags);
650 print_num("Cmdline size", hdr->cmdline_size);
651 print_num("Hardware subarch", hdr->hardware_subarch);
652 print_num64("HW subarch data", hdr->hardware_subarch_data);
653 print_num("Payload offset", hdr->payload_offset);
654 print_num("Payload length", hdr->payload_length);
655 print_num64("Setup data", hdr->setup_data);
656 print_num64("Pref address", hdr->pref_address);
657 print_num("Init size", hdr->init_size);
658 print_num("Handover offset", hdr->handover_offset);
659 if (get_boot_protocol(hdr, false) >= 0x215)
660 print_num("Kernel info offset", hdr->kernel_info_offset);
661
662 return 0;
663}
664
Simon Glass5588e772020-09-05 14:50:43 -0600665/* Note: This defines the complete_zboot() function */
666U_BOOT_SUBCMDS(zboot,
Simon Glass4f960232020-09-05 14:50:51 -0600667 U_BOOT_CMD_MKENT(start, 8, 1, do_zboot_start, "", ""),
Simon Glass1d9e4bb2020-09-05 14:50:46 -0600668 U_BOOT_CMD_MKENT(load, 1, 1, do_zboot_load, "", ""),
Simon Glass3e597592020-09-05 14:50:47 -0600669 U_BOOT_CMD_MKENT(setup, 1, 1, do_zboot_setup, "", ""),
Simon Glass6f873f52020-09-05 14:50:45 -0600670 U_BOOT_CMD_MKENT(info, 1, 1, do_zboot_info, "", ""),
Simon Glass88f1cd62020-09-05 14:50:44 -0600671 U_BOOT_CMD_MKENT(go, 1, 1, do_zboot_go, "", ""),
Simon Glass631c2b92020-09-05 14:50:50 -0600672 U_BOOT_CMD_MKENT(dump, 2, 1, do_zboot_dump, "", ""),
Simon Glass5588e772020-09-05 14:50:43 -0600673)
674
675int do_zboot_states(struct cmd_tbl *cmdtp, int flag, int argc,
676 char *const argv[], int state_mask)
677{
678 int i;
679
680 for (i = 0; i < ZBOOT_STATE_COUNT; i++) {
681 struct cmd_tbl *cmd = &zboot_subcmds[i];
682 int mask = 1 << i;
683 int ret;
684
685 if (mask & state_mask) {
686 ret = cmd->cmd(cmd, flag, argc, argv);
687 if (ret)
688 return ret;
689 }
690 }
691
692 return 0;
693}
694
695int do_zboot_parent(struct cmd_tbl *cmdtp, int flag, int argc,
696 char *const argv[], int *repeatable)
697{
698 /* determine if we have a sub command */
699 if (argc > 1) {
700 char *endp;
701
702 simple_strtoul(argv[1], &endp, 16);
703 /*
704 * endp pointing to nul means that argv[1] was just a valid
705 * number, so pass it along to the normal processing
706 */
707 if (*endp)
708 return do_zboot(cmdtp, flag, argc, argv, repeatable);
709 }
710
Simon Glass88f1cd62020-09-05 14:50:44 -0600711 do_zboot_states(cmdtp, flag, argc, argv, ZBOOT_STATE_START |
Simon Glass3e597592020-09-05 14:50:47 -0600712 ZBOOT_STATE_LOAD | ZBOOT_STATE_SETUP |
713 ZBOOT_STATE_INFO | ZBOOT_STATE_GO);
Simon Glass5588e772020-09-05 14:50:43 -0600714
715 return CMD_RET_FAILURE;
716}
717
718U_BOOT_CMDREP_COMPLETE(
Simon Glass4f960232020-09-05 14:50:51 -0600719 zboot, 8, do_zboot_parent, "Boot bzImage",
720 "[addr] [size] [initrd addr] [initrd size] [setup] [cmdline]\n"
Gabe Black6f9d9982011-12-05 12:09:27 +0000721 " addr - The optional starting address of the bzimage.\n"
722 " If not set it defaults to the environment\n"
723 " variable \"fileaddr\".\n"
724 " size - The optional size of the bzimage. Defaults to\n"
725 " zero.\n"
726 " initrd addr - The address of the initrd image to use, if any.\n"
727 " initrd size - The size of the initrd image to use, if any.\n"
Simon Glassf82cd7b2020-09-05 14:50:49 -0600728 " setup - The address of the kernel setup region, if this\n"
729 " is not at addr\n"
Simon Glass4f960232020-09-05 14:50:51 -0600730 " cmdline - The address of the kernel command line, to\n"
731 " override U-Boot's normal cmdline generation\n"
Simon Glass5588e772020-09-05 14:50:43 -0600732 "\n"
733 "Sub-commands to do part of the zboot sequence:\n"
Simon Glass88f1cd62020-09-05 14:50:44 -0600734 "\tstart [addr [arg ...]] - specify arguments\n"
Simon Glass1d9e4bb2020-09-05 14:50:46 -0600735 "\tload - load OS image\n"
Simon Glass3e597592020-09-05 14:50:47 -0600736 "\tsetup - set up table\n"
Simon Glass6f873f52020-09-05 14:50:45 -0600737 "\tinfo - show summary info\n"
Simon Glass631c2b92020-09-05 14:50:50 -0600738 "\tgo - start OS\n"
739 "\tdump [addr] - dump info (optional address of boot params)",
Simon Glass5588e772020-09-05 14:50:43 -0600740 complete_zboot
Graeme Russ95ffaba2010-04-24 00:05:49 +1000741);