blob: a79971f052ce383fbabb74474a939a9f8046708f [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
57 */
58struct zboot_state {
59 ulong bzimage_addr;
60 ulong bzimage_size;
61 ulong initrd_addr;
62 ulong initrd_size;
63 ulong load_address;
64} state;
65
wdenk2262cfe2002-11-18 00:14:45 +000066static void build_command_line(char *command_line, int auto_boot)
67{
68 char *env_command_line;
wdenk8bde7f72003-06-27 21:31:46 +000069
wdenk2262cfe2002-11-18 00:14:45 +000070 command_line[0] = '\0';
wdenk8bde7f72003-06-27 21:31:46 +000071
Simon Glass00caae62017-08-03 12:22:12 -060072 env_command_line = env_get("bootargs");
wdenk8bde7f72003-06-27 21:31:46 +000073
wdenk2262cfe2002-11-18 00:14:45 +000074 /* set console= argument if we use a serial console */
Graeme Russ83088af2011-11-08 02:33:15 +000075 if (!strstr(env_command_line, "console=")) {
Simon Glass00caae62017-08-03 12:22:12 -060076 if (!strcmp(env_get("stdout"), "serial")) {
wdenk8bde7f72003-06-27 21:31:46 +000077
wdenk2262cfe2002-11-18 00:14:45 +000078 /* We seem to use serial console */
wdenk8bde7f72003-06-27 21:31:46 +000079 sprintf(command_line, "console=ttyS0,%s ",
Simon Glass00caae62017-08-03 12:22:12 -060080 env_get("baudrate"));
wdenk2262cfe2002-11-18 00:14:45 +000081 }
82 }
wdenk8bde7f72003-06-27 21:31:46 +000083
Graeme Russ83088af2011-11-08 02:33:15 +000084 if (auto_boot)
wdenk2262cfe2002-11-18 00:14:45 +000085 strcat(command_line, "auto ");
wdenk8bde7f72003-06-27 21:31:46 +000086
Graeme Russ83088af2011-11-08 02:33:15 +000087 if (env_command_line)
wdenk2262cfe2002-11-18 00:14:45 +000088 strcat(command_line, env_command_line);
wdenk8bde7f72003-06-27 21:31:46 +000089
wdenk2262cfe2002-11-18 00:14:45 +000090 printf("Kernel command line: \"%s\"\n", command_line);
91}
92
Gabe Black69370d12011-12-05 12:09:26 +000093static int kernel_magic_ok(struct setup_header *hdr)
94{
95 if (KERNEL_MAGIC != hdr->boot_flag) {
96 printf("Error: Invalid Boot Flag "
97 "(found 0x%04x, expected 0x%04x)\n",
98 hdr->boot_flag, KERNEL_MAGIC);
99 return 0;
100 } else {
101 printf("Valid Boot Flag\n");
102 return 1;
103 }
104}
105
106static int get_boot_protocol(struct setup_header *hdr)
107{
108 if (hdr->header == KERNEL_V2_MAGIC) {
109 printf("Magic signature found\n");
110 return hdr->version;
111 } else {
112 /* Very old kernel */
113 printf("Magic signature not found\n");
114 return 0x0100;
115 }
116}
117
Ivan Gorinov5d732922018-03-26 18:06:54 -0700118static int setup_device_tree(struct setup_header *hdr, const void *fdt_blob)
119{
120 int bootproto = get_boot_protocol(hdr);
121 struct setup_data *sd;
122 int size;
123
124 if (bootproto < 0x0209)
125 return -ENOTSUPP;
126
127 if (!fdt_blob)
128 return 0;
129
130 size = fdt_totalsize(fdt_blob);
131 if (size < 0)
132 return -EINVAL;
133
134 size += sizeof(struct setup_data);
135 sd = (struct setup_data *)malloc(size);
136 if (!sd) {
137 printf("Not enough memory for DTB setup data\n");
138 return -ENOMEM;
139 }
140
141 sd->next = hdr->setup_data;
142 sd->type = SETUP_DTB;
143 sd->len = fdt_totalsize(fdt_blob);
144 memcpy(sd->data, fdt_blob, sd->len);
145 hdr->setup_data = (unsigned long)sd;
146
147 return 0;
148}
149
Gabe Black69370d12011-12-05 12:09:26 +0000150struct boot_params *load_zimage(char *image, unsigned long kernel_size,
Simon Glass76539382014-10-10 08:21:56 -0600151 ulong *load_addressp)
wdenk2262cfe2002-11-18 00:14:45 +0000152{
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000153 struct boot_params *setup_base;
wdenk2262cfe2002-11-18 00:14:45 +0000154 int setup_size;
155 int bootproto;
156 int big_image;
wdenk8bde7f72003-06-27 21:31:46 +0000157
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000158 struct boot_params *params = (struct boot_params *)image;
159 struct setup_header *hdr = &params->hdr;
wdenk8bde7f72003-06-27 21:31:46 +0000160
Graeme Russ83088af2011-11-08 02:33:15 +0000161 /* base address for real-mode segment */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000162 setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
wdenk8bde7f72003-06-27 21:31:46 +0000163
Gabe Black69370d12011-12-05 12:09:26 +0000164 if (!kernel_magic_ok(hdr))
wdenk2262cfe2002-11-18 00:14:45 +0000165 return 0;
wdenk8bde7f72003-06-27 21:31:46 +0000166
wdenk2262cfe2002-11-18 00:14:45 +0000167 /* determine size of setup */
Graeme Russ95ffaba2010-04-24 00:05:49 +1000168 if (0 == hdr->setup_sects) {
169 printf("Setup Sectors = 0 (defaulting to 4)\n");
wdenk2262cfe2002-11-18 00:14:45 +0000170 setup_size = 5 * 512;
171 } else {
Graeme Russ95ffaba2010-04-24 00:05:49 +1000172 setup_size = (hdr->setup_sects + 1) * 512;
wdenk2262cfe2002-11-18 00:14:45 +0000173 }
wdenk8bde7f72003-06-27 21:31:46 +0000174
Graeme Russ95ffaba2010-04-24 00:05:49 +1000175 printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
176
Graeme Russ83088af2011-11-08 02:33:15 +0000177 if (setup_size > SETUP_MAX_SIZE)
wdenk2262cfe2002-11-18 00:14:45 +0000178 printf("Error: Setup is too large (%d bytes)\n", setup_size);
wdenk8bde7f72003-06-27 21:31:46 +0000179
Gabe Black69370d12011-12-05 12:09:26 +0000180 /* determine boot protocol version */
181 bootproto = get_boot_protocol(hdr);
182
183 printf("Using boot protocol version %x.%02x\n",
184 (bootproto & 0xff00) >> 8, bootproto & 0xff);
185
186 if (bootproto >= 0x0200) {
187 if (hdr->setup_sects >= 15) {
188 printf("Linux kernel version %s\n",
189 (char *)params +
190 hdr->kernel_version + 0x200);
191 } else {
192 printf("Setup Sectors < 15 - "
193 "Cannot print kernel version.\n");
194 }
195 }
196
wdenk2262cfe2002-11-18 00:14:45 +0000197 /* Determine image type */
Graeme Russ83088af2011-11-08 02:33:15 +0000198 big_image = (bootproto >= 0x0200) &&
199 (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk8bde7f72003-06-27 21:31:46 +0000200
Graeme Russ95ffaba2010-04-24 00:05:49 +1000201 /* Determine load address */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000202 if (big_image)
Simon Glass76539382014-10-10 08:21:56 -0600203 *load_addressp = BZIMAGE_LOAD_ADDR;
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000204 else
Simon Glass76539382014-10-10 08:21:56 -0600205 *load_addressp = ZIMAGE_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000206
Gabe Black233dbc12011-12-05 12:09:24 +0000207 printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
208 memset(setup_base, 0, sizeof(*setup_base));
209 setup_base->hdr = params->hdr;
wdenk8bde7f72003-06-27 21:31:46 +0000210
Gabe Black69370d12011-12-05 12:09:26 +0000211 if (bootproto >= 0x0204)
212 kernel_size = hdr->syssize * 16;
213 else
214 kernel_size -= setup_size;
wdenk8bde7f72003-06-27 21:31:46 +0000215
wdenk8bde7f72003-06-27 21:31:46 +0000216 if (bootproto == 0x0100) {
Graeme Russ83088af2011-11-08 02:33:15 +0000217 /*
218 * A very old kernel MUST have its real-mode code
219 * loaded at 0x90000
220 */
Simon Glass42fd8c12017-01-16 07:03:35 -0700221 if ((ulong)setup_base != 0x90000) {
wdenk2262cfe2002-11-18 00:14:45 +0000222 /* Copy the real-mode kernel */
Graeme Russ83088af2011-11-08 02:33:15 +0000223 memmove((void *)0x90000, setup_base, setup_size);
wdenk8bde7f72003-06-27 21:31:46 +0000224
Graeme Russ83088af2011-11-08 02:33:15 +0000225 /* Copy the command line */
226 memmove((void *)0x99000,
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000227 (u8 *)setup_base + COMMAND_LINE_OFFSET,
Graeme Russ83088af2011-11-08 02:33:15 +0000228 COMMAND_LINE_SIZE);
229
230 /* Relocated */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000231 setup_base = (struct boot_params *)0x90000;
wdenk2262cfe2002-11-18 00:14:45 +0000232 }
wdenk8bde7f72003-06-27 21:31:46 +0000233
wdenk2262cfe2002-11-18 00:14:45 +0000234 /* It is recommended to clear memory up to the 32K mark */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000235 memset((u8 *)0x90000 + setup_size, 0,
236 SETUP_MAX_SIZE - setup_size);
wdenk2262cfe2002-11-18 00:14:45 +0000237 }
wdenk8bde7f72003-06-27 21:31:46 +0000238
Gabe Black69370d12011-12-05 12:09:26 +0000239 if (big_image) {
240 if (kernel_size > BZIMAGE_MAX_SIZE) {
241 printf("Error: bzImage kernel too big! "
242 "(size: %ld, max: %d)\n",
243 kernel_size, BZIMAGE_MAX_SIZE);
244 return 0;
245 }
246 } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
247 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
248 kernel_size, ZIMAGE_MAX_SIZE);
249 return 0;
250 }
Graeme Russ95ffaba2010-04-24 00:05:49 +1000251
Simon Glass76539382014-10-10 08:21:56 -0600252 printf("Loading %s at address %lx (%ld bytes)\n",
253 big_image ? "bzImage" : "zImage", *load_addressp, kernel_size);
Gabe Black69370d12011-12-05 12:09:26 +0000254
Simon Glass76539382014-10-10 08:21:56 -0600255 memmove((void *)*load_addressp, image + setup_size, kernel_size);
Gabe Black69370d12011-12-05 12:09:26 +0000256
257 return setup_base;
258}
259
260int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
261 unsigned long initrd_addr, unsigned long initrd_size)
262{
263 struct setup_header *hdr = &setup_base->hdr;
264 int bootproto = get_boot_protocol(hdr);
265
Gabe Black69370d12011-12-05 12:09:26 +0000266 setup_base->e820_entries = install_e820_map(
267 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Gabe Black69370d12011-12-05 12:09:26 +0000268
269 if (bootproto == 0x0100) {
270 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
271 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
272 }
wdenk2262cfe2002-11-18 00:14:45 +0000273 if (bootproto >= 0x0200) {
Graeme Russ95ffaba2010-04-24 00:05:49 +1000274 hdr->type_of_loader = 8;
275
wdenk2262cfe2002-11-18 00:14:45 +0000276 if (initrd_addr) {
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000277 printf("Initial RAM disk at linear address "
278 "0x%08lx, size %ld bytes\n",
wdenk2262cfe2002-11-18 00:14:45 +0000279 initrd_addr, initrd_size);
wdenk8bde7f72003-06-27 21:31:46 +0000280
Graeme Russ95ffaba2010-04-24 00:05:49 +1000281 hdr->ramdisk_image = initrd_addr;
282 hdr->ramdisk_size = initrd_size;
wdenk2262cfe2002-11-18 00:14:45 +0000283 }
284 }
wdenk8bde7f72003-06-27 21:31:46 +0000285
wdenk2262cfe2002-11-18 00:14:45 +0000286 if (bootproto >= 0x0201) {
Graeme Russ95ffaba2010-04-24 00:05:49 +1000287 hdr->heap_end_ptr = HEAP_END_OFFSET;
288 hdr->loadflags |= HEAP_FLAG;
wdenk2262cfe2002-11-18 00:14:45 +0000289 }
wdenk8bde7f72003-06-27 21:31:46 +0000290
Simon Glass97d1e0c2014-10-19 21:11:21 -0600291 if (cmd_line) {
292 if (bootproto >= 0x0202) {
293 hdr->cmd_line_ptr = (uintptr_t)cmd_line;
294 } else if (bootproto >= 0x0200) {
295 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
296 setup_base->screen_info.cl_offset =
297 (uintptr_t)cmd_line - (uintptr_t)setup_base;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000298
Simon Glass97d1e0c2014-10-19 21:11:21 -0600299 hdr->setup_move_size = 0x9100;
300 }
301
302 /* build command line at COMMAND_LINE_OFFSET */
303 build_command_line(cmd_line, auto_boot);
wdenk2262cfe2002-11-18 00:14:45 +0000304 }
wdenk2262cfe2002-11-18 00:14:45 +0000305
Andy Shevchenko378960d2018-01-10 19:40:14 +0200306#ifdef CONFIG_INTEL_MID
307 if (bootproto >= 0x0207)
308 hdr->hardware_subarch = X86_SUBARCH_INTEL_MID;
309#endif
310
Andy Shevchenkod905aa82019-09-13 18:42:00 +0300311#ifdef CONFIG_GENERATE_ACPI_TABLE
312 setup_base->acpi_rsdp_addr = acpi_get_rsdp_addr();
313#endif
314
Ivan Gorinov5d732922018-03-26 18:06:54 -0700315 setup_device_tree(hdr, (const void *)env_get_hex("fdtaddr", 0));
Bin Menga4520022015-07-06 16:31:36 +0800316 setup_video(&setup_base->screen_info);
317
Bin Meng1fdeacd2018-08-23 08:24:10 -0700318#ifdef CONFIG_EFI_STUB
319 setup_efi_info(&setup_base->efi_info);
320#endif
321
Gabe Black69370d12011-12-05 12:09:26 +0000322 return 0;
wdenk2262cfe2002-11-18 00:14:45 +0000323}
324
Simon Glass09140112020-05-10 11:40:03 -0600325int do_zboot(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
Graeme Russ95ffaba2010-04-24 00:05:49 +1000326{
Gabe Black69370d12011-12-05 12:09:26 +0000327 struct boot_params *base_ptr;
Graeme Russabe98f42010-10-07 20:03:19 +1100328 char *s;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000329
330 disable_interrupts();
Simon Glasse8148372020-09-05 14:50:38 -0600331 memset(&state, '\0', sizeof(state));
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000332 if (argc >= 2) {
Graeme Russabe98f42010-10-07 20:03:19 +1100333 /* argv[1] holds the address of the bzImage */
334 s = argv[1];
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000335 } else {
Simon Glass00caae62017-08-03 12:22:12 -0600336 s = env_get("fileaddr");
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000337 }
Graeme Russ95ffaba2010-04-24 00:05:49 +1000338
Graeme Russabe98f42010-10-07 20:03:19 +1100339 if (s)
Simon Glasse8148372020-09-05 14:50:38 -0600340 state.bzimage_addr = simple_strtoul(s, NULL, 16);
Graeme Russabe98f42010-10-07 20:03:19 +1100341
Gabe Black6f9d9982011-12-05 12:09:27 +0000342 if (argc >= 3) {
Graeme Russabe98f42010-10-07 20:03:19 +1100343 /* argv[2] holds the size of the bzImage */
Simon Glasse8148372020-09-05 14:50:38 -0600344 state.bzimage_size = simple_strtoul(argv[2], NULL, 16);
Gabe Black6f9d9982011-12-05 12:09:27 +0000345 }
346
347 if (argc >= 4)
Simon Glasse8148372020-09-05 14:50:38 -0600348 state.initrd_addr = simple_strtoul(argv[3], NULL, 16);
Gabe Black6f9d9982011-12-05 12:09:27 +0000349 if (argc >= 5)
Simon Glasse8148372020-09-05 14:50:38 -0600350 state.initrd_size = simple_strtoul(argv[4], NULL, 16);
Graeme Russ95ffaba2010-04-24 00:05:49 +1000351
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000352 /* Lets look for */
Simon Glasse8148372020-09-05 14:50:38 -0600353 base_ptr = load_zimage((void *)state.bzimage_addr, state.bzimage_size,
354 &state.load_address);
Graeme Russ83088af2011-11-08 02:33:15 +0000355 if (!base_ptr) {
Simon Glass2c363cb2014-10-10 08:21:59 -0600356 puts("## Kernel loading failed ...\n");
Gabe Black69370d12011-12-05 12:09:26 +0000357 return -1;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000358 }
Simon Glasse8148372020-09-05 14:50:38 -0600359
360 if (setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET, 0,
361 state.initrd_addr, state.initrd_size)) {
Simon Glass2c363cb2014-10-10 08:21:59 -0600362 puts("Setting up boot parameters failed ...\n");
Gabe Black69370d12011-12-05 12:09:26 +0000363 return -1;
364 }
365
Gabe Black69370d12011-12-05 12:09:26 +0000366 /* we assume that the kernel is in place */
Simon Glasse8148372020-09-05 14:50:38 -0600367 return boot_linux_kernel((ulong)base_ptr, state.load_address, false);
Graeme Russ95ffaba2010-04-24 00:05:49 +1000368}
369
370U_BOOT_CMD(
Gabe Black6f9d9982011-12-05 12:09:27 +0000371 zboot, 5, 0, do_zboot,
Graeme Russ95ffaba2010-04-24 00:05:49 +1000372 "Boot bzImage",
Gabe Black6f9d9982011-12-05 12:09:27 +0000373 "[addr] [size] [initrd addr] [initrd size]\n"
374 " addr - The optional starting address of the bzimage.\n"
375 " If not set it defaults to the environment\n"
376 " variable \"fileaddr\".\n"
377 " size - The optional size of the bzimage. Defaults to\n"
378 " zero.\n"
379 " initrd addr - The address of the initrd image to use, if any.\n"
380 " initrd size - The size of the initrd image to use, if any.\n"
Graeme Russ95ffaba2010-04-24 00:05:49 +1000381);