blob: 2f0e92f123a5acaa06a6c2f031cec72766d06099 [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>
Simon Glassaa83f2b2014-10-19 21:11:20 -060021#include <asm/bootm.h>
Graeme Russ1bab1042010-04-24 00:05:49 +100022#include <asm/bootparam.h>
Vadim Bendebury8cd22c82012-10-23 18:04:34 +000023#ifdef CONFIG_SYS_COREBOOT
24#include <asm/arch/timestamp.h>
25#endif
Stefan Reinauer2e5225e2012-10-23 18:04:37 +000026#include <linux/compiler.h>
wdenk591dda52002-11-18 00:14:45 +000027
28/*
29 * Memory lay-out:
wdenk57b2d802003-06-27 21:31:46 +000030 *
wdenk591dda52002-11-18 00:14:45 +000031 * relative to setup_base (which is 0x90000 currently)
wdenk57b2d802003-06-27 21:31:46 +000032 *
33 * 0x0000-0x7FFF Real mode kernel
wdenk591dda52002-11-18 00:14:45 +000034 * 0x8000-0x8FFF Stack and heap
35 * 0x9000-0x90FF Kernel command line
36 */
Graeme Russ883c6032011-11-08 02:33:15 +000037#define DEFAULT_SETUP_BASE 0x90000
38#define COMMAND_LINE_OFFSET 0x9000
39#define HEAP_END_OFFSET 0x8e00
wdenk591dda52002-11-18 00:14:45 +000040
Graeme Russ883c6032011-11-08 02:33:15 +000041#define COMMAND_LINE_SIZE 2048
wdenk591dda52002-11-18 00:14:45 +000042
Gabe Blackc67a9482011-12-05 12:09:24 +000043unsigned generic_install_e820_map(unsigned max_entries,
44 struct e820entry *entries)
45{
46 return 0;
47}
48
49unsigned install_e820_map(unsigned max_entries,
50 struct e820entry *entries)
51 __attribute__((weak, alias("generic_install_e820_map")));
52
wdenk591dda52002-11-18 00:14:45 +000053static void build_command_line(char *command_line, int auto_boot)
54{
55 char *env_command_line;
wdenk57b2d802003-06-27 21:31:46 +000056
wdenk591dda52002-11-18 00:14:45 +000057 command_line[0] = '\0';
wdenk57b2d802003-06-27 21:31:46 +000058
wdenk591dda52002-11-18 00:14:45 +000059 env_command_line = getenv("bootargs");
wdenk57b2d802003-06-27 21:31:46 +000060
wdenk591dda52002-11-18 00:14:45 +000061 /* set console= argument if we use a serial console */
Graeme Russ883c6032011-11-08 02:33:15 +000062 if (!strstr(env_command_line, "console=")) {
63 if (!strcmp(getenv("stdout"), "serial")) {
wdenk57b2d802003-06-27 21:31:46 +000064
wdenk591dda52002-11-18 00:14:45 +000065 /* We seem to use serial console */
wdenk57b2d802003-06-27 21:31:46 +000066 sprintf(command_line, "console=ttyS0,%s ",
Graeme Russ883c6032011-11-08 02:33:15 +000067 getenv("baudrate"));
wdenk591dda52002-11-18 00:14:45 +000068 }
69 }
wdenk57b2d802003-06-27 21:31:46 +000070
Graeme Russ883c6032011-11-08 02:33:15 +000071 if (auto_boot)
wdenk591dda52002-11-18 00:14:45 +000072 strcat(command_line, "auto ");
wdenk57b2d802003-06-27 21:31:46 +000073
Graeme Russ883c6032011-11-08 02:33:15 +000074 if (env_command_line)
wdenk591dda52002-11-18 00:14:45 +000075 strcat(command_line, env_command_line);
wdenk57b2d802003-06-27 21:31:46 +000076
wdenk591dda52002-11-18 00:14:45 +000077 printf("Kernel command line: \"%s\"\n", command_line);
78}
79
Gabe Black540c2622011-12-05 12:09:26 +000080static int kernel_magic_ok(struct setup_header *hdr)
wdenk591dda52002-11-18 00:14:45 +000081{
Graeme Russ1bab1042010-04-24 00:05:49 +100082 if (KERNEL_MAGIC != hdr->boot_flag) {
Gabe Black1cf51212011-12-05 12:09:23 +000083 printf("Error: Invalid Boot Flag "
84 "(found 0x%04x, expected 0x%04x)\n",
85 hdr->boot_flag, KERNEL_MAGIC);
wdenk591dda52002-11-18 00:14:45 +000086 return 0;
Graeme Russ1bab1042010-04-24 00:05:49 +100087 } else {
88 printf("Valid Boot Flag\n");
Gabe Black540c2622011-12-05 12:09:26 +000089 return 1;
wdenk591dda52002-11-18 00:14:45 +000090 }
Gabe Black540c2622011-12-05 12:09:26 +000091}
wdenk57b2d802003-06-27 21:31:46 +000092
Gabe Black540c2622011-12-05 12:09:26 +000093static int get_boot_protocol(struct setup_header *hdr)
94{
95 if (hdr->header == KERNEL_V2_MAGIC) {
Graeme Russ1bab1042010-04-24 00:05:49 +100096 printf("Magic signature found\n");
Gabe Black540c2622011-12-05 12:09:26 +000097 return hdr->version;
wdenk591dda52002-11-18 00:14:45 +000098 } else {
99 /* Very old kernel */
Graeme Russ1bab1042010-04-24 00:05:49 +1000100 printf("Magic signature not found\n");
Gabe Black540c2622011-12-05 12:09:26 +0000101 return 0x0100;
wdenk591dda52002-11-18 00:14:45 +0000102 }
Gabe Black540c2622011-12-05 12:09:26 +0000103}
104
105struct boot_params *load_zimage(char *image, unsigned long kernel_size,
106 void **load_address)
107{
108 struct boot_params *setup_base;
109 int setup_size;
110 int bootproto;
111 int big_image;
112
113 struct boot_params *params = (struct boot_params *)image;
114 struct setup_header *hdr = &params->hdr;
115
116 /* base address for real-mode segment */
117 setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
118
119 if (!kernel_magic_ok(hdr))
120 return 0;
wdenk57b2d802003-06-27 21:31:46 +0000121
wdenk591dda52002-11-18 00:14:45 +0000122 /* determine size of setup */
Graeme Russ1bab1042010-04-24 00:05:49 +1000123 if (0 == hdr->setup_sects) {
124 printf("Setup Sectors = 0 (defaulting to 4)\n");
wdenk591dda52002-11-18 00:14:45 +0000125 setup_size = 5 * 512;
126 } else {
Graeme Russ1bab1042010-04-24 00:05:49 +1000127 setup_size = (hdr->setup_sects + 1) * 512;
wdenk591dda52002-11-18 00:14:45 +0000128 }
wdenk57b2d802003-06-27 21:31:46 +0000129
Graeme Russ1bab1042010-04-24 00:05:49 +1000130 printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
131
Graeme Russ883c6032011-11-08 02:33:15 +0000132 if (setup_size > SETUP_MAX_SIZE)
wdenk591dda52002-11-18 00:14:45 +0000133 printf("Error: Setup is too large (%d bytes)\n", setup_size);
wdenk57b2d802003-06-27 21:31:46 +0000134
Gabe Black540c2622011-12-05 12:09:26 +0000135 /* determine boot protocol version */
136 bootproto = get_boot_protocol(hdr);
137
138 printf("Using boot protocol version %x.%02x\n",
139 (bootproto & 0xff00) >> 8, bootproto & 0xff);
140
141 if (bootproto >= 0x0200) {
142 if (hdr->setup_sects >= 15) {
143 printf("Linux kernel version %s\n",
144 (char *)params +
145 hdr->kernel_version + 0x200);
146 } else {
147 printf("Setup Sectors < 15 - "
148 "Cannot print kernel version.\n");
149 }
150 }
151
wdenk591dda52002-11-18 00:14:45 +0000152 /* Determine image type */
Graeme Russ883c6032011-11-08 02:33:15 +0000153 big_image = (bootproto >= 0x0200) &&
154 (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk57b2d802003-06-27 21:31:46 +0000155
Graeme Russ1bab1042010-04-24 00:05:49 +1000156 /* Determine load address */
Gabe Black1cf51212011-12-05 12:09:23 +0000157 if (big_image)
Gabe Blackc67a9482011-12-05 12:09:24 +0000158 *load_address = (void *)BZIMAGE_LOAD_ADDR;
Gabe Black1cf51212011-12-05 12:09:23 +0000159 else
Gabe Blackc67a9482011-12-05 12:09:24 +0000160 *load_address = (void *)ZIMAGE_LOAD_ADDR;
wdenk57b2d802003-06-27 21:31:46 +0000161
Gabe Blackc67a9482011-12-05 12:09:24 +0000162 printf("Building boot_params at 0x%8.8lx\n", (ulong)setup_base);
163 memset(setup_base, 0, sizeof(*setup_base));
164 setup_base->hdr = params->hdr;
wdenk57b2d802003-06-27 21:31:46 +0000165
Gabe Black540c2622011-12-05 12:09:26 +0000166 if (bootproto >= 0x0204)
167 kernel_size = hdr->syssize * 16;
168 else
169 kernel_size -= setup_size;
wdenk57b2d802003-06-27 21:31:46 +0000170
wdenk57b2d802003-06-27 21:31:46 +0000171 if (bootproto == 0x0100) {
Graeme Russ883c6032011-11-08 02:33:15 +0000172 /*
173 * A very old kernel MUST have its real-mode code
174 * loaded at 0x90000
175 */
wdenk591dda52002-11-18 00:14:45 +0000176 if ((u32)setup_base != 0x90000) {
177 /* Copy the real-mode kernel */
Graeme Russ883c6032011-11-08 02:33:15 +0000178 memmove((void *)0x90000, setup_base, setup_size);
179
wdenk591dda52002-11-18 00:14:45 +0000180 /* Copy the command line */
Graeme Russ883c6032011-11-08 02:33:15 +0000181 memmove((void *)0x99000,
Gabe Black1cf51212011-12-05 12:09:23 +0000182 (u8 *)setup_base + COMMAND_LINE_OFFSET,
Graeme Russ883c6032011-11-08 02:33:15 +0000183 COMMAND_LINE_SIZE);
wdenk57b2d802003-06-27 21:31:46 +0000184
Graeme Russ883c6032011-11-08 02:33:15 +0000185 /* Relocated */
Gabe Black1cf51212011-12-05 12:09:23 +0000186 setup_base = (struct boot_params *)0x90000;
wdenk591dda52002-11-18 00:14:45 +0000187 }
wdenk57b2d802003-06-27 21:31:46 +0000188
wdenk591dda52002-11-18 00:14:45 +0000189 /* It is recommended to clear memory up to the 32K mark */
Gabe Black1cf51212011-12-05 12:09:23 +0000190 memset((u8 *)0x90000 + setup_size, 0,
191 SETUP_MAX_SIZE - setup_size);
wdenk591dda52002-11-18 00:14:45 +0000192 }
wdenk57b2d802003-06-27 21:31:46 +0000193
Gabe Black540c2622011-12-05 12:09:26 +0000194 if (big_image) {
195 if (kernel_size > BZIMAGE_MAX_SIZE) {
196 printf("Error: bzImage kernel too big! "
197 "(size: %ld, max: %d)\n",
198 kernel_size, BZIMAGE_MAX_SIZE);
199 return 0;
200 }
201 } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
202 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
203 kernel_size, ZIMAGE_MAX_SIZE);
204 return 0;
205 }
206
207 printf("Loading %s at address %p (%ld bytes)\n",
208 big_image ? "bzImage" : "zImage", *load_address, kernel_size);
209
210 memmove(*load_address, image + setup_size, kernel_size);
211
212 return setup_base;
213}
214
215int setup_zimage(struct boot_params *setup_base, char *cmd_line, int auto_boot,
216 unsigned long initrd_addr, unsigned long initrd_size)
217{
218 struct setup_header *hdr = &setup_base->hdr;
219 int bootproto = get_boot_protocol(hdr);
220
Gabe Black540c2622011-12-05 12:09:26 +0000221 setup_base->e820_entries = install_e820_map(
222 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Graeme Russ1bab1042010-04-24 00:05:49 +1000223
Gabe Black540c2622011-12-05 12:09:26 +0000224 if (bootproto == 0x0100) {
225 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
226 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
227 }
wdenk591dda52002-11-18 00:14:45 +0000228 if (bootproto >= 0x0200) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000229 hdr->type_of_loader = 8;
230
wdenk591dda52002-11-18 00:14:45 +0000231 if (initrd_addr) {
Gabe Black1cf51212011-12-05 12:09:23 +0000232 printf("Initial RAM disk at linear address "
233 "0x%08lx, size %ld bytes\n",
wdenk591dda52002-11-18 00:14:45 +0000234 initrd_addr, initrd_size);
wdenk57b2d802003-06-27 21:31:46 +0000235
Graeme Russ1bab1042010-04-24 00:05:49 +1000236 hdr->ramdisk_image = initrd_addr;
237 hdr->ramdisk_size = initrd_size;
wdenk591dda52002-11-18 00:14:45 +0000238 }
239 }
wdenk57b2d802003-06-27 21:31:46 +0000240
wdenk591dda52002-11-18 00:14:45 +0000241 if (bootproto >= 0x0201) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000242 hdr->heap_end_ptr = HEAP_END_OFFSET;
243 hdr->loadflags |= HEAP_FLAG;
wdenk591dda52002-11-18 00:14:45 +0000244 }
wdenk57b2d802003-06-27 21:31:46 +0000245
Simon Glass4b4f2732014-10-19 21:11:21 -0600246 if (cmd_line) {
247 if (bootproto >= 0x0202) {
248 hdr->cmd_line_ptr = (uintptr_t)cmd_line;
249 } else if (bootproto >= 0x0200) {
250 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
251 setup_base->screen_info.cl_offset =
252 (uintptr_t)cmd_line - (uintptr_t)setup_base;
Graeme Russ1bab1042010-04-24 00:05:49 +1000253
Simon Glass4b4f2732014-10-19 21:11:21 -0600254 hdr->setup_move_size = 0x9100;
255 }
256
257 /* build command line at COMMAND_LINE_OFFSET */
258 build_command_line(cmd_line, auto_boot);
wdenk591dda52002-11-18 00:14:45 +0000259 }
wdenk591dda52002-11-18 00:14:45 +0000260
Gabe Black540c2622011-12-05 12:09:26 +0000261 return 0;
wdenk591dda52002-11-18 00:14:45 +0000262}
263
Gabe Blackc67a9482011-12-05 12:09:24 +0000264void boot_zimage(void *setup_base, void *load_address)
wdenk591dda52002-11-18 00:14:45 +0000265{
Simon Glassaa83f2b2014-10-19 21:11:20 -0600266 bootm_announce_and_cleanup();
Gabe Blackc67a9482011-12-05 12:09:24 +0000267
Vadim Bendebury8cd22c82012-10-23 18:04:34 +0000268#ifdef CONFIG_SYS_COREBOOT
269 timestamp_add_now(TS_U_BOOT_START_KERNEL);
270#endif
Gabe Blackc67a9482011-12-05 12:09:24 +0000271 /*
272 * Set %ebx, %ebp, and %edi to 0, %esi to point to the boot_params
273 * structure, and then jump to the kernel. We assume that %cs is
274 * 0x10, 4GB flat, and read/execute, and the data segments are 0x18,
275 * 4GB flat, and read/write. U-boot is setting them up that way for
276 * itself in arch/i386/cpu/cpu.c.
277 */
278 __asm__ __volatile__ (
Simon Glass923bbf02012-10-23 18:04:40 +0000279 "movl $0, %%ebp\n"
280 "cli\n"
281 "jmp *%[kernel_entry]\n"
Gabe Blackc67a9482011-12-05 12:09:24 +0000282 :: [kernel_entry]"a"(load_address),
283 [boot_params] "S"(setup_base),
284 "b"(0), "D"(0)
285 : "%ebp"
286 );
wdenk591dda52002-11-18 00:14:45 +0000287}
Graeme Russ1bab1042010-04-24 00:05:49 +1000288
Graeme Russ81d90dd2011-12-23 10:20:55 +1100289void setup_pcat_compatibility(void)
290 __attribute__((weak, alias("__setup_pcat_compatibility")));
291
292void __setup_pcat_compatibility(void)
293{
294}
295
Graeme Russ883c6032011-11-08 02:33:15 +0000296int do_zboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
Graeme Russ1bab1042010-04-24 00:05:49 +1000297{
Gabe Black540c2622011-12-05 12:09:26 +0000298 struct boot_params *base_ptr;
Graeme Russ3134be22010-10-07 20:03:19 +1100299 void *bzImage_addr = NULL;
Gabe Blackc67a9482011-12-05 12:09:24 +0000300 void *load_address;
Graeme Russ3134be22010-10-07 20:03:19 +1100301 char *s;
Graeme Russ1bab1042010-04-24 00:05:49 +1000302 ulong bzImage_size = 0;
Gabe Black69b49ee2011-12-05 12:09:27 +0000303 ulong initrd_addr = 0;
304 ulong initrd_size = 0;
Graeme Russ1bab1042010-04-24 00:05:49 +1000305
306 disable_interrupts();
307
308 /* Setup board for maximum PC/AT Compatibility */
309 setup_pcat_compatibility();
310
Gabe Black1cf51212011-12-05 12:09:23 +0000311 if (argc >= 2) {
Graeme Russ3134be22010-10-07 20:03:19 +1100312 /* argv[1] holds the address of the bzImage */
313 s = argv[1];
Gabe Black1cf51212011-12-05 12:09:23 +0000314 } else {
Graeme Russ3134be22010-10-07 20:03:19 +1100315 s = getenv("fileaddr");
Gabe Black1cf51212011-12-05 12:09:23 +0000316 }
Graeme Russ1bab1042010-04-24 00:05:49 +1000317
Graeme Russ3134be22010-10-07 20:03:19 +1100318 if (s)
319 bzImage_addr = (void *)simple_strtoul(s, NULL, 16);
320
Gabe Black69b49ee2011-12-05 12:09:27 +0000321 if (argc >= 3) {
Graeme Russ3134be22010-10-07 20:03:19 +1100322 /* argv[2] holds the size of the bzImage */
Graeme Russ1bab1042010-04-24 00:05:49 +1000323 bzImage_size = simple_strtoul(argv[2], NULL, 16);
Gabe Black69b49ee2011-12-05 12:09:27 +0000324 }
325
326 if (argc >= 4)
327 initrd_addr = simple_strtoul(argv[3], NULL, 16);
328 if (argc >= 5)
329 initrd_size = simple_strtoul(argv[4], NULL, 16);
Graeme Russ1bab1042010-04-24 00:05:49 +1000330
Gabe Black1cf51212011-12-05 12:09:23 +0000331 /* Lets look for */
Gabe Black540c2622011-12-05 12:09:26 +0000332 base_ptr = load_zimage(bzImage_addr, bzImage_size, &load_address);
Graeme Russ1bab1042010-04-24 00:05:49 +1000333
Graeme Russ883c6032011-11-08 02:33:15 +0000334 if (!base_ptr) {
335 printf("## Kernel loading failed ...\n");
Gabe Black540c2622011-12-05 12:09:26 +0000336 return -1;
Graeme Russ1bab1042010-04-24 00:05:49 +1000337 }
Gabe Black540c2622011-12-05 12:09:26 +0000338 if (setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
Gabe Black69b49ee2011-12-05 12:09:27 +0000339 0, initrd_addr, initrd_size)) {
Gabe Black540c2622011-12-05 12:09:26 +0000340 printf("Setting up boot parameters failed ...\n");
341 return -1;
342 }
343
Gabe Black540c2622011-12-05 12:09:26 +0000344 /* we assume that the kernel is in place */
345 boot_zimage(base_ptr, load_address);
346 /* does not return */
Graeme Russ1bab1042010-04-24 00:05:49 +1000347
348 return -1;
349}
350
351U_BOOT_CMD(
Gabe Black69b49ee2011-12-05 12:09:27 +0000352 zboot, 5, 0, do_zboot,
Graeme Russ1bab1042010-04-24 00:05:49 +1000353 "Boot bzImage",
Gabe Black69b49ee2011-12-05 12:09:27 +0000354 "[addr] [size] [initrd addr] [initrd size]\n"
355 " addr - The optional starting address of the bzimage.\n"
356 " If not set it defaults to the environment\n"
357 " variable \"fileaddr\".\n"
358 " size - The optional size of the bzimage. Defaults to\n"
359 " zero.\n"
360 " initrd addr - The address of the initrd image to use, if any.\n"
361 " initrd size - The size of the initrd image to use, if any.\n"
Graeme Russ1bab1042010-04-24 00:05:49 +1000362);