blob: 8b42b5cafa1ba39d4b67b205c94b8ce7a6a97c09 [file] [log] [blame]
wdenk591dda52002-11-18 00:14:45 +00001/*
2 * (C) Copyright 2002
Albert ARIBAUD60fbc8d2011-08-04 18:45:45 +02003 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
wdenk57b2d802003-06-27 21:31:46 +00004 *
wdenk591dda52002-11-18 00:14:45 +00005 * See file CREDITS for list of people who contributed to this
6 * project.
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License as
10 * published by the Free Software Foundation; either version 2 of
11 * the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21 * MA 02111-1307 USA
22 */
23
wdenk57b2d802003-06-27 21:31:46 +000024/*
Graeme Russ45fc1d82011-04-13 19:43:26 +100025 * Linux x86 zImage and bzImage loading
wdenk57b2d802003-06-27 21:31:46 +000026 *
27 * based on the procdure described in
wdenk591dda52002-11-18 00:14:45 +000028 * linux/Documentation/i386/boot.txt
29 */
30
31#include <common.h>
32#include <asm/io.h>
33#include <asm/ptrace.h>
34#include <asm/zimage.h>
35#include <asm/realmode.h>
36#include <asm/byteorder.h>
Graeme Russ1bab1042010-04-24 00:05:49 +100037#include <asm/bootparam.h>
wdenk591dda52002-11-18 00:14:45 +000038
39/*
40 * Memory lay-out:
wdenk57b2d802003-06-27 21:31:46 +000041 *
wdenk591dda52002-11-18 00:14:45 +000042 * relative to setup_base (which is 0x90000 currently)
wdenk57b2d802003-06-27 21:31:46 +000043 *
44 * 0x0000-0x7FFF Real mode kernel
wdenk591dda52002-11-18 00:14:45 +000045 * 0x8000-0x8FFF Stack and heap
46 * 0x9000-0x90FF Kernel command line
47 */
Graeme Russ883c6032011-11-08 02:33:15 +000048#define DEFAULT_SETUP_BASE 0x90000
49#define COMMAND_LINE_OFFSET 0x9000
50#define HEAP_END_OFFSET 0x8e00
wdenk591dda52002-11-18 00:14:45 +000051
Graeme Russ883c6032011-11-08 02:33:15 +000052#define COMMAND_LINE_SIZE 2048
wdenk591dda52002-11-18 00:14:45 +000053
54static void build_command_line(char *command_line, int auto_boot)
55{
56 char *env_command_line;
wdenk57b2d802003-06-27 21:31:46 +000057
wdenk591dda52002-11-18 00:14:45 +000058 command_line[0] = '\0';
wdenk57b2d802003-06-27 21:31:46 +000059
wdenk591dda52002-11-18 00:14:45 +000060 env_command_line = getenv("bootargs");
wdenk57b2d802003-06-27 21:31:46 +000061
wdenk591dda52002-11-18 00:14:45 +000062 /* set console= argument if we use a serial console */
Graeme Russ883c6032011-11-08 02:33:15 +000063 if (!strstr(env_command_line, "console=")) {
64 if (!strcmp(getenv("stdout"), "serial")) {
wdenk57b2d802003-06-27 21:31:46 +000065
wdenk591dda52002-11-18 00:14:45 +000066 /* We seem to use serial console */
wdenk57b2d802003-06-27 21:31:46 +000067 sprintf(command_line, "console=ttyS0,%s ",
Graeme Russ883c6032011-11-08 02:33:15 +000068 getenv("baudrate"));
wdenk591dda52002-11-18 00:14:45 +000069 }
70 }
wdenk57b2d802003-06-27 21:31:46 +000071
Graeme Russ883c6032011-11-08 02:33:15 +000072 if (auto_boot)
wdenk591dda52002-11-18 00:14:45 +000073 strcat(command_line, "auto ");
wdenk57b2d802003-06-27 21:31:46 +000074
Graeme Russ883c6032011-11-08 02:33:15 +000075 if (env_command_line)
wdenk591dda52002-11-18 00:14:45 +000076 strcat(command_line, env_command_line);
wdenk57b2d802003-06-27 21:31:46 +000077
wdenk591dda52002-11-18 00:14:45 +000078 printf("Kernel command line: \"%s\"\n", command_line);
79}
80
wdenk57b2d802003-06-27 21:31:46 +000081void *load_zimage(char *image, unsigned long kernel_size,
wdenk591dda52002-11-18 00:14:45 +000082 unsigned long initrd_addr, unsigned long initrd_size,
83 int auto_boot)
84{
wdenk57b2d802003-06-27 21:31:46 +000085 void *setup_base;
wdenk591dda52002-11-18 00:14:45 +000086 int setup_size;
87 int bootproto;
88 int big_image;
89 void *load_address;
Graeme Russ883c6032011-11-08 02:33:15 +000090 struct setup_header *hdr;
wdenk57b2d802003-06-27 21:31:46 +000091
Graeme Russ883c6032011-11-08 02:33:15 +000092 hdr = (struct setup_header *)(image + SETUP_SECTS_OFF);
wdenk57b2d802003-06-27 21:31:46 +000093
Graeme Russ883c6032011-11-08 02:33:15 +000094 /* base address for real-mode segment */
95 setup_base = (void *)DEFAULT_SETUP_BASE;
wdenk57b2d802003-06-27 21:31:46 +000096
Graeme Russ1bab1042010-04-24 00:05:49 +100097 if (KERNEL_MAGIC != hdr->boot_flag) {
98 printf("Error: Invalid Boot Flag (found 0x%04x, expected 0x%04x)\n",
Graeme Russ883c6032011-11-08 02:33:15 +000099 hdr->boot_flag, KERNEL_MAGIC);
wdenk591dda52002-11-18 00:14:45 +0000100 return 0;
Graeme Russ1bab1042010-04-24 00:05:49 +1000101 } else {
102 printf("Valid Boot Flag\n");
wdenk591dda52002-11-18 00:14:45 +0000103 }
wdenk57b2d802003-06-27 21:31:46 +0000104
wdenk591dda52002-11-18 00:14:45 +0000105 /* determine boot protocol version */
Graeme Russ1bab1042010-04-24 00:05:49 +1000106 if (KERNEL_V2_MAGIC == hdr->header) {
107 printf("Magic signature found\n");
108
109 bootproto = hdr->version;
wdenk591dda52002-11-18 00:14:45 +0000110 } else {
111 /* Very old kernel */
Graeme Russ1bab1042010-04-24 00:05:49 +1000112 printf("Magic signature not found\n");
wdenk591dda52002-11-18 00:14:45 +0000113 bootproto = 0x0100;
114 }
wdenk57b2d802003-06-27 21:31:46 +0000115
wdenk591dda52002-11-18 00:14:45 +0000116 /* determine size of setup */
Graeme Russ1bab1042010-04-24 00:05:49 +1000117 if (0 == hdr->setup_sects) {
118 printf("Setup Sectors = 0 (defaulting to 4)\n");
wdenk591dda52002-11-18 00:14:45 +0000119 setup_size = 5 * 512;
120 } else {
Graeme Russ1bab1042010-04-24 00:05:49 +1000121 setup_size = (hdr->setup_sects + 1) * 512;
wdenk591dda52002-11-18 00:14:45 +0000122 }
wdenk57b2d802003-06-27 21:31:46 +0000123
Graeme Russ1bab1042010-04-24 00:05:49 +1000124 printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
125
Graeme Russ883c6032011-11-08 02:33:15 +0000126 if (setup_size > SETUP_MAX_SIZE)
wdenk591dda52002-11-18 00:14:45 +0000127 printf("Error: Setup is too large (%d bytes)\n", setup_size);
wdenk57b2d802003-06-27 21:31:46 +0000128
wdenk591dda52002-11-18 00:14:45 +0000129 /* Determine image type */
Graeme Russ883c6032011-11-08 02:33:15 +0000130 big_image = (bootproto >= 0x0200) &&
131 (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk57b2d802003-06-27 21:31:46 +0000132
Graeme Russ1bab1042010-04-24 00:05:49 +1000133 /* Determine load address */
Graeme Russ883c6032011-11-08 02:33:15 +0000134 load_address = (void *)(big_image ?
135 BZIMAGE_LOAD_ADDR :
136 ZIMAGE_LOAD_ADDR);
wdenk57b2d802003-06-27 21:31:46 +0000137
wdenk591dda52002-11-18 00:14:45 +0000138 /* load setup */
Graeme Russ883c6032011-11-08 02:33:15 +0000139 printf("Moving Real-Mode Code to 0x%8.8lx (%d bytes)\n",
140 (ulong)setup_base, setup_size);
wdenk591dda52002-11-18 00:14:45 +0000141 memmove(setup_base, image, setup_size);
wdenk57b2d802003-06-27 21:31:46 +0000142
143 printf("Using boot protocol version %x.%02x\n",
wdenk591dda52002-11-18 00:14:45 +0000144 (bootproto & 0xff00) >> 8, bootproto & 0xff);
wdenk57b2d802003-06-27 21:31:46 +0000145
wdenk57b2d802003-06-27 21:31:46 +0000146 if (bootproto == 0x0100) {
Graeme Russ883c6032011-11-08 02:33:15 +0000147 *(u16 *)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC;
148 *(u16 *)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET;
wdenk57b2d802003-06-27 21:31:46 +0000149
Graeme Russ883c6032011-11-08 02:33:15 +0000150 /*
151 * A very old kernel MUST have its real-mode code
152 * loaded at 0x90000
153 */
wdenk591dda52002-11-18 00:14:45 +0000154 if ((u32)setup_base != 0x90000) {
155 /* Copy the real-mode kernel */
Graeme Russ883c6032011-11-08 02:33:15 +0000156 memmove((void *)0x90000, setup_base, setup_size);
157
wdenk591dda52002-11-18 00:14:45 +0000158 /* Copy the command line */
Graeme Russ883c6032011-11-08 02:33:15 +0000159 memmove((void *)0x99000,
160 setup_base + COMMAND_LINE_OFFSET,
161 COMMAND_LINE_SIZE);
wdenk57b2d802003-06-27 21:31:46 +0000162
Graeme Russ883c6032011-11-08 02:33:15 +0000163 /* Relocated */
164 setup_base = (void *)0x90000;
wdenk591dda52002-11-18 00:14:45 +0000165 }
wdenk57b2d802003-06-27 21:31:46 +0000166
wdenk591dda52002-11-18 00:14:45 +0000167 /* It is recommended to clear memory up to the 32K mark */
Graeme Russ883c6032011-11-08 02:33:15 +0000168 memset((void *)0x90000 + setup_size, 0,
169 SETUP_MAX_SIZE-setup_size);
wdenk591dda52002-11-18 00:14:45 +0000170 }
wdenk57b2d802003-06-27 21:31:46 +0000171
Graeme Russ1bab1042010-04-24 00:05:49 +1000172 /* We are now setting up the real-mode version of the header */
173 hdr = (struct setup_header *)(setup_base + SETUP_SECTS_OFF);
174
wdenk591dda52002-11-18 00:14:45 +0000175 if (bootproto >= 0x0200) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000176 hdr->type_of_loader = 8;
177
178 if (hdr->setup_sects >= 15)
Graeme Russ883c6032011-11-08 02:33:15 +0000179 printf("Linux kernel version %s\n",
180 (char *)(setup_base +
181 (hdr->kernel_version + 0x200)));
Graeme Russ1bab1042010-04-24 00:05:49 +1000182 else
183 printf("Setup Sectors < 15 - Cannot print kernel version.\n");
wdenk57b2d802003-06-27 21:31:46 +0000184
wdenk591dda52002-11-18 00:14:45 +0000185 if (initrd_addr) {
186 printf("Initial RAM disk at linear address 0x%08lx, size %ld bytes\n",
187 initrd_addr, initrd_size);
wdenk57b2d802003-06-27 21:31:46 +0000188
Graeme Russ1bab1042010-04-24 00:05:49 +1000189 hdr->ramdisk_image = initrd_addr;
190 hdr->ramdisk_size = initrd_size;
wdenk591dda52002-11-18 00:14:45 +0000191 }
192 }
wdenk57b2d802003-06-27 21:31:46 +0000193
wdenk591dda52002-11-18 00:14:45 +0000194 if (bootproto >= 0x0201) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000195 hdr->heap_end_ptr = HEAP_END_OFFSET;
196 hdr->loadflags |= HEAP_FLAG;
wdenk591dda52002-11-18 00:14:45 +0000197 }
wdenk57b2d802003-06-27 21:31:46 +0000198
wdenk591dda52002-11-18 00:14:45 +0000199 if (bootproto >= 0x0202) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000200 hdr->cmd_line_ptr = (u32)setup_base + COMMAND_LINE_OFFSET;
wdenk591dda52002-11-18 00:14:45 +0000201 } else if (bootproto >= 0x0200) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000202
Graeme Russ883c6032011-11-08 02:33:15 +0000203 *(u16 *)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC;
204 *(u16 *)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET;
Graeme Russ1bab1042010-04-24 00:05:49 +1000205
206 hdr->setup_move_size = 0x9100;
wdenk591dda52002-11-18 00:14:45 +0000207 }
wdenk591dda52002-11-18 00:14:45 +0000208
Graeme Russ1bab1042010-04-24 00:05:49 +1000209 if (bootproto >= 0x0204)
210 kernel_size = hdr->syssize * 16;
211 else
212 kernel_size -= setup_size;
213
wdenk57b2d802003-06-27 21:31:46 +0000214
wdenk591dda52002-11-18 00:14:45 +0000215 if (big_image) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000216 if ((kernel_size) > BZIMAGE_MAX_SIZE) {
wdenk591dda52002-11-18 00:14:45 +0000217 printf("Error: bzImage kernel too big! (size: %ld, max: %d)\n",
Graeme Russ1bab1042010-04-24 00:05:49 +1000218 kernel_size, BZIMAGE_MAX_SIZE);
wdenk591dda52002-11-18 00:14:45 +0000219 return 0;
220 }
wdenk57b2d802003-06-27 21:31:46 +0000221
Graeme Russ1bab1042010-04-24 00:05:49 +1000222 } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
wdenk591dda52002-11-18 00:14:45 +0000223 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
Graeme Russ1bab1042010-04-24 00:05:49 +1000224 kernel_size, ZIMAGE_MAX_SIZE);
wdenk591dda52002-11-18 00:14:45 +0000225 return 0;
226 }
wdenk57b2d802003-06-27 21:31:46 +0000227
wdenk591dda52002-11-18 00:14:45 +0000228 /* build command line at COMMAND_LINE_OFFSET */
229 build_command_line(setup_base + COMMAND_LINE_OFFSET, auto_boot);
wdenk57b2d802003-06-27 21:31:46 +0000230
Graeme Russ883c6032011-11-08 02:33:15 +0000231 printf("Loading %czImage at address 0x%08x (%ld bytes)\n",
232 big_image ? 'b' : ' ', (u32)load_address, kernel_size);
wdenk591dda52002-11-18 00:14:45 +0000233
wdenk57b2d802003-06-27 21:31:46 +0000234
Graeme Russ1bab1042010-04-24 00:05:49 +1000235 memmove(load_address, image + setup_size, kernel_size);
wdenk57b2d802003-06-27 21:31:46 +0000236
wdenk591dda52002-11-18 00:14:45 +0000237 /* ready for booting */
238 return setup_base;
239}
240
wdenk591dda52002-11-18 00:14:45 +0000241void boot_zimage(void *setup_base)
242{
243 struct pt_regs regs;
wdenk57b2d802003-06-27 21:31:46 +0000244
wdenk591dda52002-11-18 00:14:45 +0000245 memset(&regs, 0, sizeof(struct pt_regs));
246 regs.xds = (u32)setup_base >> 4;
Graeme Russ1bab1042010-04-24 00:05:49 +1000247 regs.xes = regs.xds;
248 regs.xss = regs.xds;
wdenkabda5ca2003-05-31 18:35:21 +0000249 regs.esp = 0x9000;
wdenk591dda52002-11-18 00:14:45 +0000250 regs.eflags = 0;
Graeme Russ883c6032011-11-08 02:33:15 +0000251 enter_realmode(((u32)setup_base+SETUP_START_OFFSET)>>4, 0, &regs,
252 &regs);
wdenk591dda52002-11-18 00:14:45 +0000253}
Graeme Russ1bab1042010-04-24 00:05:49 +1000254
Graeme Russ883c6032011-11-08 02:33:15 +0000255int do_zboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
Graeme Russ1bab1042010-04-24 00:05:49 +1000256{
257 void *base_ptr;
Graeme Russ3134be22010-10-07 20:03:19 +1100258 void *bzImage_addr = NULL;
259 char *s;
Graeme Russ1bab1042010-04-24 00:05:49 +1000260 ulong bzImage_size = 0;
261
262 disable_interrupts();
263
264 /* Setup board for maximum PC/AT Compatibility */
265 setup_pcat_compatibility();
266
Graeme Russ3134be22010-10-07 20:03:19 +1100267 if (argc >= 2)
268 /* argv[1] holds the address of the bzImage */
269 s = argv[1];
270 else
271 s = getenv("fileaddr");
Graeme Russ1bab1042010-04-24 00:05:49 +1000272
Graeme Russ3134be22010-10-07 20:03:19 +1100273 if (s)
274 bzImage_addr = (void *)simple_strtoul(s, NULL, 16);
275
276 if (argc >= 3)
277 /* argv[2] holds the size of the bzImage */
Graeme Russ1bab1042010-04-24 00:05:49 +1000278 bzImage_size = simple_strtoul(argv[2], NULL, 16);
279
280 /* Lets look for*/
Graeme Russ883c6032011-11-08 02:33:15 +0000281 base_ptr = load_zimage(bzImage_addr, bzImage_size, 0, 0, 0);
Graeme Russ1bab1042010-04-24 00:05:49 +1000282
Graeme Russ883c6032011-11-08 02:33:15 +0000283 if (!base_ptr) {
284 printf("## Kernel loading failed ...\n");
Graeme Russ1bab1042010-04-24 00:05:49 +1000285 } else {
Graeme Russ883c6032011-11-08 02:33:15 +0000286 printf("## Transferring control to Linux (at address %08x) ...\n",
Graeme Russ1bab1042010-04-24 00:05:49 +1000287 (u32)base_ptr);
288
289 /* we assume that the kernel is in place */
290 printf("\nStarting kernel ...\n\n");
291
292 boot_zimage(base_ptr);
293 /* does not return */
294 }
295
296 return -1;
297}
298
299U_BOOT_CMD(
Graeme Russ3134be22010-10-07 20:03:19 +1100300 zboot, 2, 0, do_zboot,
Graeme Russ1bab1042010-04-24 00:05:49 +1000301 "Boot bzImage",
302 ""
303);