blob: 1dab3cc78878dcea322d1685f0df0ed388f7a70c [file] [log] [blame]
wdenk2262cfe2002-11-18 00:14:45 +00001/*
Gabe Blackd3a2bc32011-12-05 12:09:23 +00002 * Copyright (c) 2011 The Chromium OS Authors.
wdenk2262cfe2002-11-18 00:14:45 +00003 * (C) Copyright 2002
Albert ARIBAUDfa82f872011-08-04 18:45:45 +02004 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
wdenk8bde7f72003-06-27 21:31:46 +00005 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenk2262cfe2002-11-18 00:14:45 +00007 */
8
wdenk8bde7f72003-06-27 21:31:46 +00009/*
Graeme Russdbf71152011-04-13 19:43:26 +100010 * Linux x86 zImage and bzImage loading
wdenk8bde7f72003-06-27 21:31:46 +000011 *
12 * based on the procdure described in
wdenk2262cfe2002-11-18 00:14:45 +000013 * linux/Documentation/i386/boot.txt
14 */
15
16#include <common.h>
17#include <asm/io.h>
18#include <asm/ptrace.h>
19#include <asm/zimage.h>
wdenk2262cfe2002-11-18 00:14:45 +000020#include <asm/byteorder.h>
Graeme Russ95ffaba2010-04-24 00:05:49 +100021#include <asm/bootparam.h>
Vadim Bendebury3cdc18a2012-10-23 18:04:34 +000022#ifdef CONFIG_SYS_COREBOOT
23#include <asm/arch/timestamp.h>
24#endif
Stefan Reinauer61e0ea92012-10-23 18:04:37 +000025#include <linux/compiler.h>
wdenk2262cfe2002-11-18 00:14:45 +000026
27/*
28 * Memory lay-out:
wdenk8bde7f72003-06-27 21:31:46 +000029 *
wdenk2262cfe2002-11-18 00:14:45 +000030 * relative to setup_base (which is 0x90000 currently)
wdenk8bde7f72003-06-27 21:31:46 +000031 *
32 * 0x0000-0x7FFF Real mode kernel
wdenk2262cfe2002-11-18 00:14:45 +000033 * 0x8000-0x8FFF Stack and heap
34 * 0x9000-0x90FF Kernel command line
35 */
Graeme Russ83088af2011-11-08 02:33:15 +000036#define DEFAULT_SETUP_BASE 0x90000
37#define COMMAND_LINE_OFFSET 0x9000
38#define HEAP_END_OFFSET 0x8e00
wdenk2262cfe2002-11-18 00:14:45 +000039
Graeme Russ83088af2011-11-08 02:33:15 +000040#define COMMAND_LINE_SIZE 2048
wdenk2262cfe2002-11-18 00:14:45 +000041
Gabe Black233dbc12011-12-05 12:09:24 +000042unsigned generic_install_e820_map(unsigned max_entries,
43 struct e820entry *entries)
44{
45 return 0;
46}
47
48unsigned install_e820_map(unsigned max_entries,
49 struct e820entry *entries)
50 __attribute__((weak, alias("generic_install_e820_map")));
51
wdenk2262cfe2002-11-18 00:14:45 +000052static void build_command_line(char *command_line, int auto_boot)
53{
54 char *env_command_line;
wdenk8bde7f72003-06-27 21:31:46 +000055
wdenk2262cfe2002-11-18 00:14:45 +000056 command_line[0] = '\0';
wdenk8bde7f72003-06-27 21:31:46 +000057
wdenk2262cfe2002-11-18 00:14:45 +000058 env_command_line = getenv("bootargs");
wdenk8bde7f72003-06-27 21:31:46 +000059
wdenk2262cfe2002-11-18 00:14:45 +000060 /* set console= argument if we use a serial console */
Graeme Russ83088af2011-11-08 02:33:15 +000061 if (!strstr(env_command_line, "console=")) {
62 if (!strcmp(getenv("stdout"), "serial")) {
wdenk8bde7f72003-06-27 21:31:46 +000063
wdenk2262cfe2002-11-18 00:14:45 +000064 /* We seem to use serial console */
wdenk8bde7f72003-06-27 21:31:46 +000065 sprintf(command_line, "console=ttyS0,%s ",
Graeme Russ83088af2011-11-08 02:33:15 +000066 getenv("baudrate"));
wdenk2262cfe2002-11-18 00:14:45 +000067 }
68 }
wdenk8bde7f72003-06-27 21:31:46 +000069
Graeme Russ83088af2011-11-08 02:33:15 +000070 if (auto_boot)
wdenk2262cfe2002-11-18 00:14:45 +000071 strcat(command_line, "auto ");
wdenk8bde7f72003-06-27 21:31:46 +000072
Graeme Russ83088af2011-11-08 02:33:15 +000073 if (env_command_line)
wdenk2262cfe2002-11-18 00:14:45 +000074 strcat(command_line, env_command_line);
wdenk8bde7f72003-06-27 21:31:46 +000075
wdenk2262cfe2002-11-18 00:14:45 +000076 printf("Kernel command line: \"%s\"\n", command_line);
77}
78
Gabe Black69370d12011-12-05 12:09:26 +000079static int kernel_magic_ok(struct setup_header *hdr)
80{
81 if (KERNEL_MAGIC != hdr->boot_flag) {
82 printf("Error: Invalid Boot Flag "
83 "(found 0x%04x, expected 0x%04x)\n",
84 hdr->boot_flag, KERNEL_MAGIC);
85 return 0;
86 } else {
87 printf("Valid Boot Flag\n");
88 return 1;
89 }
90}
91
92static int get_boot_protocol(struct setup_header *hdr)
93{
94 if (hdr->header == KERNEL_V2_MAGIC) {
95 printf("Magic signature found\n");
96 return hdr->version;
97 } else {
98 /* Very old kernel */
99 printf("Magic signature not found\n");
100 return 0x0100;
101 }
102}
103
104struct boot_params *load_zimage(char *image, unsigned long kernel_size,
105 void **load_address)
wdenk2262cfe2002-11-18 00:14:45 +0000106{
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000107 struct boot_params *setup_base;
wdenk2262cfe2002-11-18 00:14:45 +0000108 int setup_size;
109 int bootproto;
110 int big_image;
wdenk8bde7f72003-06-27 21:31:46 +0000111
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000112 struct boot_params *params = (struct boot_params *)image;
113 struct setup_header *hdr = &params->hdr;
wdenk8bde7f72003-06-27 21:31:46 +0000114
Graeme Russ83088af2011-11-08 02:33:15 +0000115 /* base address for real-mode segment */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000116 setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
wdenk8bde7f72003-06-27 21:31:46 +0000117
Gabe Black69370d12011-12-05 12:09:26 +0000118 if (!kernel_magic_ok(hdr))
wdenk2262cfe2002-11-18 00:14:45 +0000119 return 0;
wdenk8bde7f72003-06-27 21:31:46 +0000120
wdenk2262cfe2002-11-18 00:14:45 +0000121 /* determine size of setup */
Graeme Russ95ffaba2010-04-24 00:05:49 +1000122 if (0 == hdr->setup_sects) {
123 printf("Setup Sectors = 0 (defaulting to 4)\n");
wdenk2262cfe2002-11-18 00:14:45 +0000124 setup_size = 5 * 512;
125 } else {
Graeme Russ95ffaba2010-04-24 00:05:49 +1000126 setup_size = (hdr->setup_sects + 1) * 512;
wdenk2262cfe2002-11-18 00:14:45 +0000127 }
wdenk8bde7f72003-06-27 21:31:46 +0000128
Graeme Russ95ffaba2010-04-24 00:05:49 +1000129 printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
130
Graeme Russ83088af2011-11-08 02:33:15 +0000131 if (setup_size > SETUP_MAX_SIZE)
wdenk2262cfe2002-11-18 00:14:45 +0000132 printf("Error: Setup is too large (%d bytes)\n", setup_size);
wdenk8bde7f72003-06-27 21:31:46 +0000133
Gabe Black69370d12011-12-05 12:09:26 +0000134 /* determine boot protocol version */
135 bootproto = get_boot_protocol(hdr);
136
137 printf("Using boot protocol version %x.%02x\n",
138 (bootproto & 0xff00) >> 8, bootproto & 0xff);
139
140 if (bootproto >= 0x0200) {
141 if (hdr->setup_sects >= 15) {
142 printf("Linux kernel version %s\n",
143 (char *)params +
144 hdr->kernel_version + 0x200);
145 } else {
146 printf("Setup Sectors < 15 - "
147 "Cannot print kernel version.\n");
148 }
149 }
150
wdenk2262cfe2002-11-18 00:14:45 +0000151 /* Determine image type */
Graeme Russ83088af2011-11-08 02:33:15 +0000152 big_image = (bootproto >= 0x0200) &&
153 (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk8bde7f72003-06-27 21:31:46 +0000154
Graeme Russ95ffaba2010-04-24 00:05:49 +1000155 /* Determine load address */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000156 if (big_image)
Gabe Black233dbc12011-12-05 12:09:24 +0000157 *load_address = (void *)BZIMAGE_LOAD_ADDR;
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000158 else
Gabe Black233dbc12011-12-05 12:09:24 +0000159 *load_address = (void *)ZIMAGE_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000160
Gabe Black233dbc12011-12-05 12:09:24 +0000161 printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
162 memset(setup_base, 0, sizeof(*setup_base));
163 setup_base->hdr = params->hdr;
wdenk8bde7f72003-06-27 21:31:46 +0000164
Gabe Black69370d12011-12-05 12:09:26 +0000165 if (bootproto >= 0x0204)
166 kernel_size = hdr->syssize * 16;
167 else
168 kernel_size -= setup_size;
wdenk8bde7f72003-06-27 21:31:46 +0000169
wdenk8bde7f72003-06-27 21:31:46 +0000170 if (bootproto == 0x0100) {
Graeme Russ83088af2011-11-08 02:33:15 +0000171 /*
172 * A very old kernel MUST have its real-mode code
173 * loaded at 0x90000
174 */
wdenk2262cfe2002-11-18 00:14:45 +0000175 if ((u32)setup_base != 0x90000) {
176 /* Copy the real-mode kernel */
Graeme Russ83088af2011-11-08 02:33:15 +0000177 memmove((void *)0x90000, setup_base, setup_size);
wdenk8bde7f72003-06-27 21:31:46 +0000178
Graeme Russ83088af2011-11-08 02:33:15 +0000179 /* Copy the command line */
180 memmove((void *)0x99000,
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000181 (u8 *)setup_base + COMMAND_LINE_OFFSET,
Graeme Russ83088af2011-11-08 02:33:15 +0000182 COMMAND_LINE_SIZE);
183
184 /* Relocated */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000185 setup_base = (struct boot_params *)0x90000;
wdenk2262cfe2002-11-18 00:14:45 +0000186 }
wdenk8bde7f72003-06-27 21:31:46 +0000187
wdenk2262cfe2002-11-18 00:14:45 +0000188 /* It is recommended to clear memory up to the 32K mark */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000189 memset((u8 *)0x90000 + setup_size, 0,
190 SETUP_MAX_SIZE - setup_size);
wdenk2262cfe2002-11-18 00:14:45 +0000191 }
wdenk8bde7f72003-06-27 21:31:46 +0000192
Gabe Black69370d12011-12-05 12:09:26 +0000193 if (big_image) {
194 if (kernel_size > BZIMAGE_MAX_SIZE) {
195 printf("Error: bzImage kernel too big! "
196 "(size: %ld, max: %d)\n",
197 kernel_size, BZIMAGE_MAX_SIZE);
198 return 0;
199 }
200 } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
201 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
202 kernel_size, ZIMAGE_MAX_SIZE);
203 return 0;
204 }
Graeme Russ95ffaba2010-04-24 00:05:49 +1000205
Gabe Black69370d12011-12-05 12:09:26 +0000206 printf("Loading %s at address %p (%ld bytes)\n",
207 big_image ? "bzImage" : "zImage", *load_address, kernel_size);
208
209 memmove(*load_address, image + setup_size, kernel_size);
210
211 return setup_base;
212}
213
214int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
215 unsigned long initrd_addr, unsigned long initrd_size)
216{
217 struct setup_header *hdr = &setup_base->hdr;
218 int bootproto = get_boot_protocol(hdr);
219
Gabe Black69370d12011-12-05 12:09:26 +0000220 setup_base->e820_entries = install_e820_map(
221 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Gabe Black69370d12011-12-05 12:09:26 +0000222
223 if (bootproto == 0x0100) {
224 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
225 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
226 }
wdenk2262cfe2002-11-18 00:14:45 +0000227 if (bootproto >= 0x0200) {
Graeme Russ95ffaba2010-04-24 00:05:49 +1000228 hdr->type_of_loader = 8;
229
wdenk2262cfe2002-11-18 00:14:45 +0000230 if (initrd_addr) {
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000231 printf("Initial RAM disk at linear address "
232 "0x%08lx, size %ld bytes\n",
wdenk2262cfe2002-11-18 00:14:45 +0000233 initrd_addr, initrd_size);
wdenk8bde7f72003-06-27 21:31:46 +0000234
Graeme Russ95ffaba2010-04-24 00:05:49 +1000235 hdr->ramdisk_image = initrd_addr;
236 hdr->ramdisk_size = initrd_size;
wdenk2262cfe2002-11-18 00:14:45 +0000237 }
238 }
wdenk8bde7f72003-06-27 21:31:46 +0000239
wdenk2262cfe2002-11-18 00:14:45 +0000240 if (bootproto >= 0x0201) {
Graeme Russ95ffaba2010-04-24 00:05:49 +1000241 hdr->heap_end_ptr = HEAP_END_OFFSET;
242 hdr->loadflags |= HEAP_FLAG;
wdenk2262cfe2002-11-18 00:14:45 +0000243 }
wdenk8bde7f72003-06-27 21:31:46 +0000244
wdenk2262cfe2002-11-18 00:14:45 +0000245 if (bootproto >= 0x0202) {
Gabe Black69370d12011-12-05 12:09:26 +0000246 hdr->cmd_line_ptr = (uintptr_t)cmd_line;
wdenk2262cfe2002-11-18 00:14:45 +0000247 } else if (bootproto >= 0x0200) {
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000248 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
Gabe Black69370d12011-12-05 12:09:26 +0000249 setup_base->screen_info.cl_offset =
250 (uintptr_t)cmd_line - (uintptr_t)setup_base;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000251
252 hdr->setup_move_size = 0x9100;
wdenk2262cfe2002-11-18 00:14:45 +0000253 }
wdenk2262cfe2002-11-18 00:14:45 +0000254
wdenk2262cfe2002-11-18 00:14:45 +0000255 /* build command line at COMMAND_LINE_OFFSET */
Gabe Black69370d12011-12-05 12:09:26 +0000256 build_command_line(cmd_line, auto_boot);
257 return 0;
wdenk2262cfe2002-11-18 00:14:45 +0000258}
259
Stefan Reinauer61e0ea92012-10-23 18:04:37 +0000260/*
261 * Implement a weak default function for boards that optionally
262 * need to clean up the system before jumping to the kernel.
263 */
264__weak void board_final_cleanup(void)
265{
266}
267
Gabe Black233dbc12011-12-05 12:09:24 +0000268void boot_zimage(void *setup_base, void *load_address)
wdenk2262cfe2002-11-18 00:14:45 +0000269{
Simon Glass79497032013-04-17 16:13:35 +0000270 debug("## Transferring control to Linux (at address %08x) ...\n",
271 (u32)setup_base);
272
273 bootstage_mark_name(BOOTSTAGE_ID_BOOTM_HANDOFF, "start_kernel");
274#ifdef CONFIG_BOOTSTAGE_REPORT
275 bootstage_report();
276#endif
Stefan Reinauer61e0ea92012-10-23 18:04:37 +0000277 board_final_cleanup();
278
Gabe Black233dbc12011-12-05 12:09:24 +0000279 printf("\nStarting kernel ...\n\n");
280
Vadim Bendebury3cdc18a2012-10-23 18:04:34 +0000281#ifdef CONFIG_SYS_COREBOOT
282 timestamp_add_now(TS_U_BOOT_START_KERNEL);
283#endif
Gabe Black233dbc12011-12-05 12:09:24 +0000284 /*
285 * Set %ebx, %ebp, and %edi to 0, %esi to point to the boot_params
286 * structure, and then jump to the kernel. We assume that %cs is
287 * 0x10, 4GB flat, and read/execute, and the data segments are 0x18,
288 * 4GB flat, and read/write. U-boot is setting them up that way for
289 * itself in arch/i386/cpu/cpu.c.
290 */
291 __asm__ __volatile__ (
Simon Glass14843112012-10-23 18:04:40 +0000292 "movl $0, %%ebp\n"
293 "cli\n"
294 "jmp *%[kernel_entry]\n"
Gabe Black233dbc12011-12-05 12:09:24 +0000295 :: [kernel_entry]"a"(load_address),
296 [boot_params] "S"(setup_base),
297 "b"(0), "D"(0)
298 : "%ebp"
299 );
wdenk2262cfe2002-11-18 00:14:45 +0000300}
Graeme Russ95ffaba2010-04-24 00:05:49 +1000301
Graeme Russ8e18e6e2011-12-23 10:20:55 +1100302void setup_pcat_compatibility(void)
303 __attribute__((weak, alias("__setup_pcat_compatibility")));
304
305void __setup_pcat_compatibility(void)
306{
307}
308
Graeme Russ83088af2011-11-08 02:33:15 +0000309int do_zboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
Graeme Russ95ffaba2010-04-24 00:05:49 +1000310{
Gabe Black69370d12011-12-05 12:09:26 +0000311 struct boot_params *base_ptr;
Graeme Russabe98f42010-10-07 20:03:19 +1100312 void *bzImage_addr = NULL;
Gabe Black233dbc12011-12-05 12:09:24 +0000313 void *load_address;
Graeme Russabe98f42010-10-07 20:03:19 +1100314 char *s;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000315 ulong bzImage_size = 0;
Gabe Black6f9d9982011-12-05 12:09:27 +0000316 ulong initrd_addr = 0;
317 ulong initrd_size = 0;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000318
319 disable_interrupts();
320
321 /* Setup board for maximum PC/AT Compatibility */
322 setup_pcat_compatibility();
323
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000324 if (argc >= 2) {
Graeme Russabe98f42010-10-07 20:03:19 +1100325 /* argv[1] holds the address of the bzImage */
326 s = argv[1];
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000327 } else {
Graeme Russabe98f42010-10-07 20:03:19 +1100328 s = getenv("fileaddr");
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000329 }
Graeme Russ95ffaba2010-04-24 00:05:49 +1000330
Graeme Russabe98f42010-10-07 20:03:19 +1100331 if (s)
332 bzImage_addr = (void *)simple_strtoul(s, NULL, 16);
333
Gabe Black6f9d9982011-12-05 12:09:27 +0000334 if (argc >= 3) {
Graeme Russabe98f42010-10-07 20:03:19 +1100335 /* argv[2] holds the size of the bzImage */
Graeme Russ95ffaba2010-04-24 00:05:49 +1000336 bzImage_size = simple_strtoul(argv[2], NULL, 16);
Gabe Black6f9d9982011-12-05 12:09:27 +0000337 }
338
339 if (argc >= 4)
340 initrd_addr = simple_strtoul(argv[3], NULL, 16);
341 if (argc >= 5)
342 initrd_size = simple_strtoul(argv[4], NULL, 16);
Graeme Russ95ffaba2010-04-24 00:05:49 +1000343
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000344 /* Lets look for */
Gabe Black69370d12011-12-05 12:09:26 +0000345 base_ptr = load_zimage(bzImage_addr, bzImage_size, &load_address);
Graeme Russ95ffaba2010-04-24 00:05:49 +1000346
Graeme Russ83088af2011-11-08 02:33:15 +0000347 if (!base_ptr) {
348 printf("## Kernel loading failed ...\n");
Gabe Black69370d12011-12-05 12:09:26 +0000349 return -1;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000350 }
Gabe Black69370d12011-12-05 12:09:26 +0000351 if (setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
Gabe Black6f9d9982011-12-05 12:09:27 +0000352 0, initrd_addr, initrd_size)) {
Gabe Black69370d12011-12-05 12:09:26 +0000353 printf("Setting up boot parameters failed ...\n");
354 return -1;
355 }
356
Gabe Black69370d12011-12-05 12:09:26 +0000357 /* we assume that the kernel is in place */
358 boot_zimage(base_ptr, load_address);
359 /* does not return */
Graeme Russ95ffaba2010-04-24 00:05:49 +1000360
361 return -1;
362}
363
364U_BOOT_CMD(
Gabe Black6f9d9982011-12-05 12:09:27 +0000365 zboot, 5, 0, do_zboot,
Graeme Russ95ffaba2010-04-24 00:05:49 +1000366 "Boot bzImage",
Gabe Black6f9d9982011-12-05 12:09:27 +0000367 "[addr] [size] [initrd addr] [initrd size]\n"
368 " addr - The optional starting address of the bzimage.\n"
369 " If not set it defaults to the environment\n"
370 " variable \"fileaddr\".\n"
371 " size - The optional size of the bzimage. Defaults to\n"
372 " zero.\n"
373 " initrd addr - The address of the initrd image to use, if any.\n"
374 " initrd size - The size of the initrd image to use, if any.\n"
Graeme Russ95ffaba2010-04-24 00:05:49 +1000375);