blob: aa5597c94d68d5acddd5f00908e552855f01175d [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>
Simon Glass0d0ba592014-10-19 21:11:20 -060021#include <asm/bootm.h>
Graeme Russ95ffaba2010-04-24 00:05:49 +100022#include <asm/bootparam.h>
Vadim Bendebury3cdc18a2012-10-23 18:04:34 +000023#ifdef CONFIG_SYS_COREBOOT
24#include <asm/arch/timestamp.h>
25#endif
Stefan Reinauer61e0ea92012-10-23 18:04:37 +000026#include <linux/compiler.h>
wdenk2262cfe2002-11-18 00:14:45 +000027
28/*
29 * Memory lay-out:
wdenk8bde7f72003-06-27 21:31:46 +000030 *
wdenk2262cfe2002-11-18 00:14:45 +000031 * relative to setup_base (which is 0x90000 currently)
wdenk8bde7f72003-06-27 21:31:46 +000032 *
33 * 0x0000-0x7FFF Real mode kernel
wdenk2262cfe2002-11-18 00:14:45 +000034 * 0x8000-0x8FFF Stack and heap
35 * 0x9000-0x90FF Kernel command line
36 */
Graeme Russ83088af2011-11-08 02:33:15 +000037#define DEFAULT_SETUP_BASE 0x90000
38#define COMMAND_LINE_OFFSET 0x9000
39#define HEAP_END_OFFSET 0x8e00
wdenk2262cfe2002-11-18 00:14:45 +000040
Graeme Russ83088af2011-11-08 02:33:15 +000041#define COMMAND_LINE_SIZE 2048
wdenk2262cfe2002-11-18 00:14:45 +000042
Gabe Black233dbc12011-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
wdenk2262cfe2002-11-18 00:14:45 +000053static void build_command_line(char *command_line, int auto_boot)
54{
55 char *env_command_line;
wdenk8bde7f72003-06-27 21:31:46 +000056
wdenk2262cfe2002-11-18 00:14:45 +000057 command_line[0] = '\0';
wdenk8bde7f72003-06-27 21:31:46 +000058
wdenk2262cfe2002-11-18 00:14:45 +000059 env_command_line = getenv("bootargs");
wdenk8bde7f72003-06-27 21:31:46 +000060
wdenk2262cfe2002-11-18 00:14:45 +000061 /* set console= argument if we use a serial console */
Graeme Russ83088af2011-11-08 02:33:15 +000062 if (!strstr(env_command_line, "console=")) {
63 if (!strcmp(getenv("stdout"), "serial")) {
wdenk8bde7f72003-06-27 21:31:46 +000064
wdenk2262cfe2002-11-18 00:14:45 +000065 /* We seem to use serial console */
wdenk8bde7f72003-06-27 21:31:46 +000066 sprintf(command_line, "console=ttyS0,%s ",
Graeme Russ83088af2011-11-08 02:33:15 +000067 getenv("baudrate"));
wdenk2262cfe2002-11-18 00:14:45 +000068 }
69 }
wdenk8bde7f72003-06-27 21:31:46 +000070
Graeme Russ83088af2011-11-08 02:33:15 +000071 if (auto_boot)
wdenk2262cfe2002-11-18 00:14:45 +000072 strcat(command_line, "auto ");
wdenk8bde7f72003-06-27 21:31:46 +000073
Graeme Russ83088af2011-11-08 02:33:15 +000074 if (env_command_line)
wdenk2262cfe2002-11-18 00:14:45 +000075 strcat(command_line, env_command_line);
wdenk8bde7f72003-06-27 21:31:46 +000076
wdenk2262cfe2002-11-18 00:14:45 +000077 printf("Kernel command line: \"%s\"\n", command_line);
78}
79
Gabe Black69370d12011-12-05 12:09:26 +000080static int kernel_magic_ok(struct setup_header *hdr)
81{
82 if (KERNEL_MAGIC != hdr->boot_flag) {
83 printf("Error: Invalid Boot Flag "
84 "(found 0x%04x, expected 0x%04x)\n",
85 hdr->boot_flag, KERNEL_MAGIC);
86 return 0;
87 } else {
88 printf("Valid Boot Flag\n");
89 return 1;
90 }
91}
92
93static int get_boot_protocol(struct setup_header *hdr)
94{
95 if (hdr->header == KERNEL_V2_MAGIC) {
96 printf("Magic signature found\n");
97 return hdr->version;
98 } else {
99 /* Very old kernel */
100 printf("Magic signature not found\n");
101 return 0x0100;
102 }
103}
104
105struct boot_params *load_zimage(char *image, unsigned long kernel_size,
Simon Glass76539382014-10-10 08:21:56 -0600106 ulong *load_addressp)
wdenk2262cfe2002-11-18 00:14:45 +0000107{
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000108 struct boot_params *setup_base;
wdenk2262cfe2002-11-18 00:14:45 +0000109 int setup_size;
110 int bootproto;
111 int big_image;
wdenk8bde7f72003-06-27 21:31:46 +0000112
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000113 struct boot_params *params = (struct boot_params *)image;
114 struct setup_header *hdr = &params->hdr;
wdenk8bde7f72003-06-27 21:31:46 +0000115
Graeme Russ83088af2011-11-08 02:33:15 +0000116 /* base address for real-mode segment */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000117 setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
wdenk8bde7f72003-06-27 21:31:46 +0000118
Gabe Black69370d12011-12-05 12:09:26 +0000119 if (!kernel_magic_ok(hdr))
wdenk2262cfe2002-11-18 00:14:45 +0000120 return 0;
wdenk8bde7f72003-06-27 21:31:46 +0000121
wdenk2262cfe2002-11-18 00:14:45 +0000122 /* determine size of setup */
Graeme Russ95ffaba2010-04-24 00:05:49 +1000123 if (0 == hdr->setup_sects) {
124 printf("Setup Sectors = 0 (defaulting to 4)\n");
wdenk2262cfe2002-11-18 00:14:45 +0000125 setup_size = 5 * 512;
126 } else {
Graeme Russ95ffaba2010-04-24 00:05:49 +1000127 setup_size = (hdr->setup_sects + 1) * 512;
wdenk2262cfe2002-11-18 00:14:45 +0000128 }
wdenk8bde7f72003-06-27 21:31:46 +0000129
Graeme Russ95ffaba2010-04-24 00:05:49 +1000130 printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
131
Graeme Russ83088af2011-11-08 02:33:15 +0000132 if (setup_size > SETUP_MAX_SIZE)
wdenk2262cfe2002-11-18 00:14:45 +0000133 printf("Error: Setup is too large (%d bytes)\n", setup_size);
wdenk8bde7f72003-06-27 21:31:46 +0000134
Gabe Black69370d12011-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
wdenk2262cfe2002-11-18 00:14:45 +0000152 /* Determine image type */
Graeme Russ83088af2011-11-08 02:33:15 +0000153 big_image = (bootproto >= 0x0200) &&
154 (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk8bde7f72003-06-27 21:31:46 +0000155
Graeme Russ95ffaba2010-04-24 00:05:49 +1000156 /* Determine load address */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000157 if (big_image)
Simon Glass76539382014-10-10 08:21:56 -0600158 *load_addressp = BZIMAGE_LOAD_ADDR;
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000159 else
Simon Glass76539382014-10-10 08:21:56 -0600160 *load_addressp = ZIMAGE_LOAD_ADDR;
wdenk8bde7f72003-06-27 21:31:46 +0000161
Gabe Black233dbc12011-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;
wdenk8bde7f72003-06-27 21:31:46 +0000165
Gabe Black69370d12011-12-05 12:09:26 +0000166 if (bootproto >= 0x0204)
167 kernel_size = hdr->syssize * 16;
168 else
169 kernel_size -= setup_size;
wdenk8bde7f72003-06-27 21:31:46 +0000170
wdenk8bde7f72003-06-27 21:31:46 +0000171 if (bootproto == 0x0100) {
Graeme Russ83088af2011-11-08 02:33:15 +0000172 /*
173 * A very old kernel MUST have its real-mode code
174 * loaded at 0x90000
175 */
wdenk2262cfe2002-11-18 00:14:45 +0000176 if ((u32)setup_base != 0x90000) {
177 /* Copy the real-mode kernel */
Graeme Russ83088af2011-11-08 02:33:15 +0000178 memmove((void *)0x90000, setup_base, setup_size);
wdenk8bde7f72003-06-27 21:31:46 +0000179
Graeme Russ83088af2011-11-08 02:33:15 +0000180 /* Copy the command line */
181 memmove((void *)0x99000,
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000182 (u8 *)setup_base + COMMAND_LINE_OFFSET,
Graeme Russ83088af2011-11-08 02:33:15 +0000183 COMMAND_LINE_SIZE);
184
185 /* Relocated */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000186 setup_base = (struct boot_params *)0x90000;
wdenk2262cfe2002-11-18 00:14:45 +0000187 }
wdenk8bde7f72003-06-27 21:31:46 +0000188
wdenk2262cfe2002-11-18 00:14:45 +0000189 /* It is recommended to clear memory up to the 32K mark */
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000190 memset((u8 *)0x90000 + setup_size, 0,
191 SETUP_MAX_SIZE - setup_size);
wdenk2262cfe2002-11-18 00:14:45 +0000192 }
wdenk8bde7f72003-06-27 21:31:46 +0000193
Gabe Black69370d12011-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 }
Graeme Russ95ffaba2010-04-24 00:05:49 +1000206
Simon Glass76539382014-10-10 08:21:56 -0600207 printf("Loading %s at address %lx (%ld bytes)\n",
208 big_image ? "bzImage" : "zImage", *load_addressp, kernel_size);
Gabe Black69370d12011-12-05 12:09:26 +0000209
Simon Glass76539382014-10-10 08:21:56 -0600210 memmove((void *)*load_addressp, image + setup_size, kernel_size);
Gabe Black69370d12011-12-05 12:09:26 +0000211
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 Black69370d12011-12-05 12:09:26 +0000221 setup_base->e820_entries = install_e820_map(
222 ARRAY_SIZE(setup_base->e820_map), setup_base->e820_map);
Gabe Black69370d12011-12-05 12:09:26 +0000223
224 if (bootproto == 0x0100) {
225 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
226 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
227 }
wdenk2262cfe2002-11-18 00:14:45 +0000228 if (bootproto >= 0x0200) {
Graeme Russ95ffaba2010-04-24 00:05:49 +1000229 hdr->type_of_loader = 8;
230
wdenk2262cfe2002-11-18 00:14:45 +0000231 if (initrd_addr) {
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000232 printf("Initial RAM disk at linear address "
233 "0x%08lx, size %ld bytes\n",
wdenk2262cfe2002-11-18 00:14:45 +0000234 initrd_addr, initrd_size);
wdenk8bde7f72003-06-27 21:31:46 +0000235
Graeme Russ95ffaba2010-04-24 00:05:49 +1000236 hdr->ramdisk_image = initrd_addr;
237 hdr->ramdisk_size = initrd_size;
wdenk2262cfe2002-11-18 00:14:45 +0000238 }
239 }
wdenk8bde7f72003-06-27 21:31:46 +0000240
wdenk2262cfe2002-11-18 00:14:45 +0000241 if (bootproto >= 0x0201) {
Graeme Russ95ffaba2010-04-24 00:05:49 +1000242 hdr->heap_end_ptr = HEAP_END_OFFSET;
243 hdr->loadflags |= HEAP_FLAG;
wdenk2262cfe2002-11-18 00:14:45 +0000244 }
wdenk8bde7f72003-06-27 21:31:46 +0000245
Simon Glass97d1e0c2014-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 Russ95ffaba2010-04-24 00:05:49 +1000253
Simon Glass97d1e0c2014-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);
wdenk2262cfe2002-11-18 00:14:45 +0000259 }
wdenk2262cfe2002-11-18 00:14:45 +0000260
Gabe Black69370d12011-12-05 12:09:26 +0000261 return 0;
wdenk2262cfe2002-11-18 00:14:45 +0000262}
263
Graeme Russ8e18e6e2011-12-23 10:20:55 +1100264void setup_pcat_compatibility(void)
265 __attribute__((weak, alias("__setup_pcat_compatibility")));
266
267void __setup_pcat_compatibility(void)
268{
269}
270
Graeme Russ83088af2011-11-08 02:33:15 +0000271int do_zboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
Graeme Russ95ffaba2010-04-24 00:05:49 +1000272{
Gabe Black69370d12011-12-05 12:09:26 +0000273 struct boot_params *base_ptr;
Graeme Russabe98f42010-10-07 20:03:19 +1100274 void *bzImage_addr = NULL;
Simon Glass76539382014-10-10 08:21:56 -0600275 ulong load_address;
Graeme Russabe98f42010-10-07 20:03:19 +1100276 char *s;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000277 ulong bzImage_size = 0;
Gabe Black6f9d9982011-12-05 12:09:27 +0000278 ulong initrd_addr = 0;
279 ulong initrd_size = 0;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000280
281 disable_interrupts();
282
283 /* Setup board for maximum PC/AT Compatibility */
284 setup_pcat_compatibility();
285
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000286 if (argc >= 2) {
Graeme Russabe98f42010-10-07 20:03:19 +1100287 /* argv[1] holds the address of the bzImage */
288 s = argv[1];
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000289 } else {
Graeme Russabe98f42010-10-07 20:03:19 +1100290 s = getenv("fileaddr");
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000291 }
Graeme Russ95ffaba2010-04-24 00:05:49 +1000292
Graeme Russabe98f42010-10-07 20:03:19 +1100293 if (s)
294 bzImage_addr = (void *)simple_strtoul(s, NULL, 16);
295
Gabe Black6f9d9982011-12-05 12:09:27 +0000296 if (argc >= 3) {
Graeme Russabe98f42010-10-07 20:03:19 +1100297 /* argv[2] holds the size of the bzImage */
Graeme Russ95ffaba2010-04-24 00:05:49 +1000298 bzImage_size = simple_strtoul(argv[2], NULL, 16);
Gabe Black6f9d9982011-12-05 12:09:27 +0000299 }
300
301 if (argc >= 4)
302 initrd_addr = simple_strtoul(argv[3], NULL, 16);
303 if (argc >= 5)
304 initrd_size = simple_strtoul(argv[4], NULL, 16);
Graeme Russ95ffaba2010-04-24 00:05:49 +1000305
Gabe Blackd3a2bc32011-12-05 12:09:23 +0000306 /* Lets look for */
Gabe Black69370d12011-12-05 12:09:26 +0000307 base_ptr = load_zimage(bzImage_addr, bzImage_size, &load_address);
Graeme Russ95ffaba2010-04-24 00:05:49 +1000308
Graeme Russ83088af2011-11-08 02:33:15 +0000309 if (!base_ptr) {
310 printf("## Kernel loading failed ...\n");
Gabe Black69370d12011-12-05 12:09:26 +0000311 return -1;
Graeme Russ95ffaba2010-04-24 00:05:49 +1000312 }
Gabe Black69370d12011-12-05 12:09:26 +0000313 if (setup_zimage(base_ptr, (char *)base_ptr + COMMAND_LINE_OFFSET,
Gabe Black6f9d9982011-12-05 12:09:27 +0000314 0, initrd_addr, initrd_size)) {
Gabe Black69370d12011-12-05 12:09:26 +0000315 printf("Setting up boot parameters failed ...\n");
316 return -1;
317 }
318
Gabe Black69370d12011-12-05 12:09:26 +0000319 /* we assume that the kernel is in place */
Simon Glass76539382014-10-10 08:21:56 -0600320 return boot_linux_kernel((ulong)base_ptr, load_address, false);
Graeme Russ95ffaba2010-04-24 00:05:49 +1000321}
322
323U_BOOT_CMD(
Gabe Black6f9d9982011-12-05 12:09:27 +0000324 zboot, 5, 0, do_zboot,
Graeme Russ95ffaba2010-04-24 00:05:49 +1000325 "Boot bzImage",
Gabe Black6f9d9982011-12-05 12:09:27 +0000326 "[addr] [size] [initrd addr] [initrd size]\n"
327 " addr - The optional starting address of the bzimage.\n"
328 " If not set it defaults to the environment\n"
329 " variable \"fileaddr\".\n"
330 " size - The optional size of the bzimage. Defaults to\n"
331 " zero.\n"
332 " initrd addr - The address of the initrd image to use, if any.\n"
333 " initrd size - The size of the initrd image to use, if any.\n"
Graeme Russ95ffaba2010-04-24 00:05:49 +1000334);