blob: 1dab3cc78878dcea322d1685f0df0ed388f7a70c [file] [log] [blame]
wdenk591dda52002-11-18 00:14:45 +00001/*
Gabe Black1cf51212011-12-05 12:09:23 +00002 * Copyright (c) 2011 The Chromium OS Authors.
wdenk591dda52002-11-18 00:14:45 +00003 * (C) Copyright 2002
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +02004 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
wdenk57b2d802003-06-27 21:31:46 +00005 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02006 * SPDX-License-Identifier: GPL-2.0+
wdenk591dda52002-11-18 00:14:45 +00007 */
8
wdenk57b2d802003-06-27 21:31:46 +00009/*
Graeme Russ45fc1d82011-04-13 19:43:26 +100010 * Linux x86 zImage and bzImage loading
wdenk57b2d802003-06-27 21:31:46 +000011 *
12 * based on the procdure described in
wdenk591dda52002-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>
wdenk591dda52002-11-18 00:14:45 +000020#include <asm/byteorder.h>
Graeme Russ1bab1042010-04-24 00:05:49 +100021#include <asm/bootparam.h>
Vadim Bendebury8cd22c82012-10-23 18:04:34 +000022#ifdef CONFIG_SYS_COREBOOT
23#include <asm/arch/timestamp.h>
24#endif
Stefan Reinauer2e5225e2012-10-23 18:04:37 +000025#include <linux/compiler.h>
wdenk591dda52002-11-18 00:14:45 +000026
27/*
28 * Memory lay-out:
wdenk57b2d802003-06-27 21:31:46 +000029 *
wdenk591dda52002-11-18 00:14:45 +000030 * relative to setup_base (which is 0x90000 currently)
wdenk57b2d802003-06-27 21:31:46 +000031 *
32 * 0x0000-0x7FFF Real mode kernel
wdenk591dda52002-11-18 00:14:45 +000033 * 0x8000-0x8FFF Stack and heap
34 * 0x9000-0x90FF Kernel command line
35 */
Graeme Russ883c6032011-11-08 02:33:15 +000036#define DEFAULT_SETUP_BASE 0x90000
37#define COMMAND_LINE_OFFSET 0x9000
38#define HEAP_END_OFFSET 0x8e00
wdenk591dda52002-11-18 00:14:45 +000039
Graeme Russ883c6032011-11-08 02:33:15 +000040#define COMMAND_LINE_SIZE 2048
wdenk591dda52002-11-18 00:14:45 +000041
Gabe Blackc67a9482011-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
wdenk591dda52002-11-18 00:14:45 +000052static void build_command_line(char *command_line, int auto_boot)
53{
54 char *env_command_line;
wdenk57b2d802003-06-27 21:31:46 +000055
wdenk591dda52002-11-18 00:14:45 +000056 command_line[0] = '\0';
wdenk57b2d802003-06-27 21:31:46 +000057
wdenk591dda52002-11-18 00:14:45 +000058 env_command_line = getenv("bootargs");
wdenk57b2d802003-06-27 21:31:46 +000059
wdenk591dda52002-11-18 00:14:45 +000060 /* set console= argument if we use a serial console */
Graeme Russ883c6032011-11-08 02:33:15 +000061 if (!strstr(env_command_line, "console=")) {
62 if (!strcmp(getenv("stdout"), "serial")) {
wdenk57b2d802003-06-27 21:31:46 +000063
wdenk591dda52002-11-18 00:14:45 +000064 /* We seem to use serial console */
wdenk57b2d802003-06-27 21:31:46 +000065 sprintf(command_line, "console=ttyS0,%s ",
Graeme Russ883c6032011-11-08 02:33:15 +000066 getenv("baudrate"));
wdenk591dda52002-11-18 00:14:45 +000067 }
68 }
wdenk57b2d802003-06-27 21:31:46 +000069
Graeme Russ883c6032011-11-08 02:33:15 +000070 if (auto_boot)
wdenk591dda52002-11-18 00:14:45 +000071 strcat(command_line, "auto ");
wdenk57b2d802003-06-27 21:31:46 +000072
Graeme Russ883c6032011-11-08 02:33:15 +000073 if (env_command_line)
wdenk591dda52002-11-18 00:14:45 +000074 strcat(command_line, env_command_line);
wdenk57b2d802003-06-27 21:31:46 +000075
wdenk591dda52002-11-18 00:14:45 +000076 printf("Kernel command line: \"%s\"\n", command_line);
77}
78
Gabe Black540c2622011-12-05 12:09:26 +000079static int kernel_magic_ok(struct setup_header *hdr)
wdenk591dda52002-11-18 00:14:45 +000080{
Graeme Russ1bab1042010-04-24 00:05:49 +100081 if (KERNEL_MAGIC != hdr->boot_flag) {
Gabe Black1cf51212011-12-05 12:09:23 +000082 printf("Error: Invalid Boot Flag "
83 "(found 0x%04x, expected 0x%04x)\n",
84 hdr->boot_flag, KERNEL_MAGIC);
wdenk591dda52002-11-18 00:14:45 +000085 return 0;
Graeme Russ1bab1042010-04-24 00:05:49 +100086 } else {
87 printf("Valid Boot Flag\n");
Gabe Black540c2622011-12-05 12:09:26 +000088 return 1;
wdenk591dda52002-11-18 00:14:45 +000089 }
Gabe Black540c2622011-12-05 12:09:26 +000090}
wdenk57b2d802003-06-27 21:31:46 +000091
Gabe Black540c2622011-12-05 12:09:26 +000092static int get_boot_protocol(struct setup_header *hdr)
93{
94 if (hdr->header == KERNEL_V2_MAGIC) {
Graeme Russ1bab1042010-04-24 00:05:49 +100095 printf("Magic signature found\n");
Gabe Black540c2622011-12-05 12:09:26 +000096 return hdr->version;
wdenk591dda52002-11-18 00:14:45 +000097 } else {
98 /* Very old kernel */
Graeme Russ1bab1042010-04-24 00:05:49 +100099 printf("Magic signature not found\n");
Gabe Black540c2622011-12-05 12:09:26 +0000100 return 0x0100;
wdenk591dda52002-11-18 00:14:45 +0000101 }
Gabe Black540c2622011-12-05 12:09:26 +0000102}
103
104struct boot_params *load_zimage(char *image, unsigned long kernel_size,
105 void **load_address)
106{
107 struct boot_params *setup_base;
108 int setup_size;
109 int bootproto;
110 int big_image;
111
112 struct boot_params *params = (struct boot_params *)image;
113 struct setup_header *hdr = &params->hdr;
114
115 /* base address for real-mode segment */
116 setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
117
118 if (!kernel_magic_ok(hdr))
119 return 0;
wdenk57b2d802003-06-27 21:31:46 +0000120
wdenk591dda52002-11-18 00:14:45 +0000121 /* determine size of setup */
Graeme Russ1bab1042010-04-24 00:05:49 +1000122 if (0 == hdr->setup_sects) {
123 printf("Setup Sectors = 0 (defaulting to 4)\n");
wdenk591dda52002-11-18 00:14:45 +0000124 setup_size = 5 * 512;
125 } else {
Graeme Russ1bab1042010-04-24 00:05:49 +1000126 setup_size = (hdr->setup_sects + 1) * 512;
wdenk591dda52002-11-18 00:14:45 +0000127 }
wdenk57b2d802003-06-27 21:31:46 +0000128
Graeme Russ1bab1042010-04-24 00:05:49 +1000129 printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
130
Graeme Russ883c6032011-11-08 02:33:15 +0000131 if (setup_size > SETUP_MAX_SIZE)
wdenk591dda52002-11-18 00:14:45 +0000132 printf("Error: Setup is too large (%d bytes)\n", setup_size);
wdenk57b2d802003-06-27 21:31:46 +0000133
Gabe Black540c2622011-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
wdenk591dda52002-11-18 00:14:45 +0000151 /* Determine image type */
Graeme Russ883c6032011-11-08 02:33:15 +0000152 big_image = (bootproto >= 0x0200) &&
153 (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk57b2d802003-06-27 21:31:46 +0000154
Graeme Russ1bab1042010-04-24 00:05:49 +1000155 /* Determine load address */
Gabe Black1cf51212011-12-05 12:09:23 +0000156 if (big_image)
Gabe Blackc67a9482011-12-05 12:09:24 +0000157 *load_address = (void *)BZIMAGE_LOAD_ADDR;
Gabe Black1cf51212011-12-05 12:09:23 +0000158 else
Gabe Blackc67a9482011-12-05 12:09:24 +0000159 *load_address = (void *)ZIMAGE_LOAD_ADDR;
wdenk57b2d802003-06-27 21:31:46 +0000160
Gabe Blackc67a9482011-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;
wdenk57b2d802003-06-27 21:31:46 +0000164
Gabe Black540c2622011-12-05 12:09:26 +0000165 if (bootproto >= 0x0204)
166 kernel_size = hdr->syssize * 16;
167 else
168 kernel_size -= setup_size;
wdenk57b2d802003-06-27 21:31:46 +0000169
wdenk57b2d802003-06-27 21:31:46 +0000170 if (bootproto == 0x0100) {
Graeme Russ883c6032011-11-08 02:33:15 +0000171 /*
172 * A very old kernel MUST have its real-mode code
173 * loaded at 0x90000
174 */
wdenk591dda52002-11-18 00:14:45 +0000175 if ((u32)setup_base != 0x90000) {
176 /* Copy the real-mode kernel */
Graeme Russ883c6032011-11-08 02:33:15 +0000177 memmove((void *)0x90000, setup_base, setup_size);
178
wdenk591dda52002-11-18 00:14:45 +0000179 /* Copy the command line */
Graeme Russ883c6032011-11-08 02:33:15 +0000180 memmove((void *)0x99000,
Gabe Black1cf51212011-12-05 12:09:23 +0000181 (u8 *)setup_base + COMMAND_LINE_OFFSET,
Graeme Russ883c6032011-11-08 02:33:15 +0000182 COMMAND_LINE_SIZE);
wdenk57b2d802003-06-27 21:31:46 +0000183
Graeme Russ883c6032011-11-08 02:33:15 +0000184 /* Relocated */
Gabe Black1cf51212011-12-05 12:09:23 +0000185 setup_base = (struct boot_params *)0x90000;
wdenk591dda52002-11-18 00:14:45 +0000186 }
wdenk57b2d802003-06-27 21:31:46 +0000187
wdenk591dda52002-11-18 00:14:45 +0000188 /* It is recommended to clear memory up to the 32K mark */
Gabe Black1cf51212011-12-05 12:09:23 +0000189 memset((u8 *)0x90000 + setup_size, 0,
190 SETUP_MAX_SIZE - setup_size);
wdenk591dda52002-11-18 00:14:45 +0000191 }
wdenk57b2d802003-06-27 21:31:46 +0000192
Gabe Black540c2622011-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 }
205
206 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 Black540c2622011-12-05 12:09:26 +0000220 setup_base->e820_entries = install_e820_map(
221 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Graeme Russ1bab1042010-04-24 00:05:49 +1000222
Gabe Black540c2622011-12-05 12:09:26 +0000223 if (bootproto == 0x0100) {
224 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
225 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
226 }
wdenk591dda52002-11-18 00:14:45 +0000227 if (bootproto >= 0x0200) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000228 hdr->type_of_loader = 8;
229
wdenk591dda52002-11-18 00:14:45 +0000230 if (initrd_addr) {
Gabe Black1cf51212011-12-05 12:09:23 +0000231 printf("Initial RAM disk at linear address "
232 "0x%08lx, size %ld bytes\n",
wdenk591dda52002-11-18 00:14:45 +0000233 initrd_addr, initrd_size);
wdenk57b2d802003-06-27 21:31:46 +0000234
Graeme Russ1bab1042010-04-24 00:05:49 +1000235 hdr->ramdisk_image = initrd_addr;
236 hdr->ramdisk_size = initrd_size;
wdenk591dda52002-11-18 00:14:45 +0000237 }
238 }
wdenk57b2d802003-06-27 21:31:46 +0000239
wdenk591dda52002-11-18 00:14:45 +0000240 if (bootproto >= 0x0201) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000241 hdr->heap_end_ptr = HEAP_END_OFFSET;
242 hdr->loadflags |= HEAP_FLAG;
wdenk591dda52002-11-18 00:14:45 +0000243 }
wdenk57b2d802003-06-27 21:31:46 +0000244
wdenk591dda52002-11-18 00:14:45 +0000245 if (bootproto >= 0x0202) {
Gabe Black540c2622011-12-05 12:09:26 +0000246 hdr->cmd_line_ptr = (uintptr_t)cmd_line;
wdenk591dda52002-11-18 00:14:45 +0000247 } else if (bootproto >= 0x0200) {
Gabe Black1cf51212011-12-05 12:09:23 +0000248 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
Gabe Black540c2622011-12-05 12:09:26 +0000249 setup_base->screen_info.cl_offset =
250 (uintptr_t)cmd_line - (uintptr_t)setup_base;
Graeme Russ1bab1042010-04-24 00:05:49 +1000251
252 hdr->setup_move_size = 0x9100;
wdenk591dda52002-11-18 00:14:45 +0000253 }
wdenk591dda52002-11-18 00:14:45 +0000254
wdenk591dda52002-11-18 00:14:45 +0000255 /* build command line at COMMAND_LINE_OFFSET */
Gabe Black540c2622011-12-05 12:09:26 +0000256 build_command_line(cmd_line, auto_boot);
257 return 0;
wdenk591dda52002-11-18 00:14:45 +0000258}
259
Stefan Reinauer2e5225e2012-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 Blackc67a9482011-12-05 12:09:24 +0000268void boot_zimage(void *setup_base, void *load_address)
wdenk591dda52002-11-18 00:14:45 +0000269{
Simon Glassbcc28da2013-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 Reinauer2e5225e2012-10-23 18:04:37 +0000277 board_final_cleanup();
278
Gabe Blackc67a9482011-12-05 12:09:24 +0000279 printf("\nStarting kernel ...\n\n");
280
Vadim Bendebury8cd22c82012-10-23 18:04:34 +0000281#ifdef CONFIG_SYS_COREBOOT
282 timestamp_add_now(TS_U_BOOT_START_KERNEL);
283#endif
Gabe Blackc67a9482011-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 Glass923bbf02012-10-23 18:04:40 +0000292 "movl $0, %%ebp\n"
293 "cli\n"
294 "jmp *%[kernel_entry]\n"
Gabe Blackc67a9482011-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 );
wdenk591dda52002-11-18 00:14:45 +0000300}
Graeme Russ1bab1042010-04-24 00:05:49 +1000301
Graeme Russ81d90dd2011-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 Russ883c6032011-11-08 02:33:15 +0000309int do_zboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
Graeme Russ1bab1042010-04-24 00:05:49 +1000310{
Gabe Black540c2622011-12-05 12:09:26 +0000311 struct boot_params *base_ptr;
Graeme Russ3134be22010-10-07 20:03:19 +1100312 void *bzImage_addr = NULL;
Gabe Blackc67a9482011-12-05 12:09:24 +0000313 void *load_address;
Graeme Russ3134be22010-10-07 20:03:19 +1100314 char *s;
Graeme Russ1bab1042010-04-24 00:05:49 +1000315 ulong bzImage_size = 0;
Gabe Black69b49ee2011-12-05 12:09:27 +0000316 ulong initrd_addr = 0;
317 ulong initrd_size = 0;
Graeme Russ1bab1042010-04-24 00:05:49 +1000318
319 disable_interrupts();
320
321 /* Setup board for maximum PC/AT Compatibility */
322 setup_pcat_compatibility();
323
Gabe Black1cf51212011-12-05 12:09:23 +0000324 if (argc >= 2) {
Graeme Russ3134be22010-10-07 20:03:19 +1100325 /* argv[1] holds the address of the bzImage */
326 s = argv[1];
Gabe Black1cf51212011-12-05 12:09:23 +0000327 } else {
Graeme Russ3134be22010-10-07 20:03:19 +1100328 s = getenv("fileaddr");
Gabe Black1cf51212011-12-05 12:09:23 +0000329 }
Graeme Russ1bab1042010-04-24 00:05:49 +1000330
Graeme Russ3134be22010-10-07 20:03:19 +1100331 if (s)
332 bzImage_addr = (void *)simple_strtoul(s, NULL, 16);
333
Gabe Black69b49ee2011-12-05 12:09:27 +0000334 if (argc >= 3) {
Graeme Russ3134be22010-10-07 20:03:19 +1100335 /* argv[2] holds the size of the bzImage */
Graeme Russ1bab1042010-04-24 00:05:49 +1000336 bzImage_size = simple_strtoul(argv[2], NULL, 16);
Gabe Black69b49ee2011-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 Russ1bab1042010-04-24 00:05:49 +1000343
Gabe Black1cf51212011-12-05 12:09:23 +0000344 /* Lets look for */
Gabe Black540c2622011-12-05 12:09:26 +0000345 base_ptr = load_zimage(bzImage_addr, bzImage_size, &load_address);
Graeme Russ1bab1042010-04-24 00:05:49 +1000346
Graeme Russ883c6032011-11-08 02:33:15 +0000347 if (!base_ptr) {
348 printf("## Kernel loading failed ...\n");
Gabe Black540c2622011-12-05 12:09:26 +0000349 return -1;
Graeme Russ1bab1042010-04-24 00:05:49 +1000350 }
Gabe Black540c2622011-12-05 12:09:26 +0000351 if (setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
Gabe Black69b49ee2011-12-05 12:09:27 +0000352 0, initrd_addr, initrd_size)) {
Gabe Black540c2622011-12-05 12:09:26 +0000353 printf("Setting up boot parameters failed ...\n");
354 return -1;
355 }
356
Gabe Black540c2622011-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 Russ1bab1042010-04-24 00:05:49 +1000360
361 return -1;
362}
363
364U_BOOT_CMD(
Gabe Black69b49ee2011-12-05 12:09:27 +0000365 zboot, 5, 0, do_zboot,
Graeme Russ1bab1042010-04-24 00:05:49 +1000366 "Boot bzImage",
Gabe Black69b49ee2011-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 Russ1bab1042010-04-24 00:05:49 +1000375);