blob: 0c420726912c120b5a263b7e3b3c165d5af601e6 [file] [log] [blame]
wdenk591dda52002-11-18 00:14:45 +00001/*
2 * (C) Copyright 2002
3 * 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/*
wdenk591dda52002-11-18 00:14:45 +000025 * Linux i386 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>
38#include <asm/ic/sc520.h>
wdenk591dda52002-11-18 00:14:45 +000039
40/*
41 * Memory lay-out:
wdenk57b2d802003-06-27 21:31:46 +000042 *
wdenk591dda52002-11-18 00:14:45 +000043 * relative to setup_base (which is 0x90000 currently)
wdenk57b2d802003-06-27 21:31:46 +000044 *
45 * 0x0000-0x7FFF Real mode kernel
wdenk591dda52002-11-18 00:14:45 +000046 * 0x8000-0x8FFF Stack and heap
47 * 0x9000-0x90FF Kernel command line
48 */
49#define DEFAULT_SETUP_BASE 0x90000
50#define COMMAND_LINE_OFFSET 0x9000
51#define HEAP_END_OFFSET 0x8e00
52
53#define COMMAND_LINE_SIZE 2048
54
55static void build_command_line(char *command_line, int auto_boot)
56{
57 char *env_command_line;
wdenk57b2d802003-06-27 21:31:46 +000058
wdenk591dda52002-11-18 00:14:45 +000059 command_line[0] = '\0';
wdenk57b2d802003-06-27 21:31:46 +000060
wdenk591dda52002-11-18 00:14:45 +000061 env_command_line = getenv("bootargs");
wdenk57b2d802003-06-27 21:31:46 +000062
wdenk591dda52002-11-18 00:14:45 +000063 /* set console= argument if we use a serial console */
64 if (NULL == strstr(env_command_line, "console=")) {
65 if (0==strcmp(getenv("stdout"), "serial")) {
wdenk57b2d802003-06-27 21:31:46 +000066
wdenk591dda52002-11-18 00:14:45 +000067 /* We seem to use serial console */
wdenk57b2d802003-06-27 21:31:46 +000068 sprintf(command_line, "console=ttyS0,%s ",
wdenk591dda52002-11-18 00:14:45 +000069 getenv("baudrate"));
70 }
71 }
wdenk57b2d802003-06-27 21:31:46 +000072
wdenk591dda52002-11-18 00:14:45 +000073 if (auto_boot) {
74 strcat(command_line, "auto ");
75 }
wdenk57b2d802003-06-27 21:31:46 +000076
wdenk591dda52002-11-18 00:14:45 +000077 if (NULL != env_command_line) {
78 strcat(command_line, env_command_line);
wdenk57b2d802003-06-27 21:31:46 +000079 }
80
81
wdenk591dda52002-11-18 00:14:45 +000082 printf("Kernel command line: \"%s\"\n", command_line);
83}
84
wdenk57b2d802003-06-27 21:31:46 +000085void *load_zimage(char *image, unsigned long kernel_size,
wdenk591dda52002-11-18 00:14:45 +000086 unsigned long initrd_addr, unsigned long initrd_size,
87 int auto_boot)
88{
wdenk57b2d802003-06-27 21:31:46 +000089 void *setup_base;
wdenk591dda52002-11-18 00:14:45 +000090 int setup_size;
91 int bootproto;
92 int big_image;
93 void *load_address;
wdenk57b2d802003-06-27 21:31:46 +000094
Graeme Russ1bab1042010-04-24 00:05:49 +100095 struct setup_header *hdr = (struct setup_header *)(image + SETUP_SECTS_OFF);
wdenk57b2d802003-06-27 21:31:46 +000096
wdenk591dda52002-11-18 00:14:45 +000097 setup_base = (void*)DEFAULT_SETUP_BASE; /* base address for real-mode segment */
wdenk57b2d802003-06-27 21:31:46 +000098
Graeme Russ1bab1042010-04-24 00:05:49 +100099 if (KERNEL_MAGIC != hdr->boot_flag) {
100 printf("Error: Invalid Boot Flag (found 0x%04x, expected 0x%04x)\n",
101 hdr->boot_flag, KERNEL_MAGIC);
wdenk591dda52002-11-18 00:14:45 +0000102 return 0;
Graeme Russ1bab1042010-04-24 00:05:49 +1000103 } else {
104 printf("Valid Boot Flag\n");
wdenk591dda52002-11-18 00:14:45 +0000105 }
wdenk57b2d802003-06-27 21:31:46 +0000106
wdenk591dda52002-11-18 00:14:45 +0000107 /* determine boot protocol version */
Graeme Russ1bab1042010-04-24 00:05:49 +1000108 if (KERNEL_V2_MAGIC == hdr->header) {
109 printf("Magic signature found\n");
110
111 bootproto = hdr->version;
wdenk591dda52002-11-18 00:14:45 +0000112 } else {
113 /* Very old kernel */
Graeme Russ1bab1042010-04-24 00:05:49 +1000114 printf("Magic signature not found\n");
wdenk591dda52002-11-18 00:14:45 +0000115 bootproto = 0x0100;
116 }
wdenk57b2d802003-06-27 21:31:46 +0000117
wdenk591dda52002-11-18 00:14:45 +0000118 /* determine size of setup */
Graeme Russ1bab1042010-04-24 00:05:49 +1000119 if (0 == hdr->setup_sects) {
120 printf("Setup Sectors = 0 (defaulting to 4)\n");
wdenk591dda52002-11-18 00:14:45 +0000121 setup_size = 5 * 512;
122 } else {
Graeme Russ1bab1042010-04-24 00:05:49 +1000123 setup_size = (hdr->setup_sects + 1) * 512;
wdenk591dda52002-11-18 00:14:45 +0000124 }
wdenk57b2d802003-06-27 21:31:46 +0000125
Graeme Russ1bab1042010-04-24 00:05:49 +1000126 printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
127
wdenk591dda52002-11-18 00:14:45 +0000128 if (setup_size > SETUP_MAX_SIZE) {
129 printf("Error: Setup is too large (%d bytes)\n", setup_size);
130 }
wdenk57b2d802003-06-27 21:31:46 +0000131
wdenk591dda52002-11-18 00:14:45 +0000132 /* Determine image type */
Graeme Russ1bab1042010-04-24 00:05:49 +1000133 big_image = (bootproto >= 0x0200) && (hdr->loadflags & BIG_KERNEL_FLAG);
wdenk57b2d802003-06-27 21:31:46 +0000134
Graeme Russ1bab1042010-04-24 00:05:49 +1000135 /* Determine load address */
136 load_address = (void*)(big_image ? BZIMAGE_LOAD_ADDR : ZIMAGE_LOAD_ADDR);
wdenk57b2d802003-06-27 21:31:46 +0000137
wdenk591dda52002-11-18 00:14:45 +0000138 /* load setup */
Graeme Russ1bab1042010-04-24 00:05:49 +1000139 printf("Moving Real-Mode Code to 0x%8.8lx (%d bytes)\n", (ulong)setup_base, setup_size);
wdenk591dda52002-11-18 00:14:45 +0000140 memmove(setup_base, image, setup_size);
wdenk57b2d802003-06-27 21:31:46 +0000141
142 printf("Using boot protocol version %x.%02x\n",
wdenk591dda52002-11-18 00:14:45 +0000143 (bootproto & 0xff00) >> 8, bootproto & 0xff);
wdenk57b2d802003-06-27 21:31:46 +0000144
wdenk57b2d802003-06-27 21:31:46 +0000145 if (bootproto == 0x0100) {
146
wdenk591dda52002-11-18 00:14:45 +0000147 *(u16*)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC;
wdenk57b2d802003-06-27 21:31:46 +0000148 *(u16*)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET;
149
wdenk591dda52002-11-18 00:14:45 +0000150 /* A very old kernel MUST have its real-mode code
151 * loaded at 0x90000 */
wdenk57b2d802003-06-27 21:31:46 +0000152
wdenk591dda52002-11-18 00:14:45 +0000153 if ((u32)setup_base != 0x90000) {
154 /* Copy the real-mode kernel */
155 memmove((void*)0x90000, setup_base, setup_size);
156 /* Copy the command line */
wdenk57b2d802003-06-27 21:31:46 +0000157 memmove((void*)0x99000, setup_base+COMMAND_LINE_OFFSET,
wdenk591dda52002-11-18 00:14:45 +0000158 COMMAND_LINE_SIZE);
wdenk57b2d802003-06-27 21:31:46 +0000159
wdenk591dda52002-11-18 00:14:45 +0000160 setup_base = (void*)0x90000; /* Relocated */
161 }
wdenk57b2d802003-06-27 21:31:46 +0000162
wdenk591dda52002-11-18 00:14:45 +0000163 /* It is recommended to clear memory up to the 32K mark */
164 memset((void*)0x90000 + setup_size, 0, SETUP_MAX_SIZE-setup_size);
165 }
wdenk57b2d802003-06-27 21:31:46 +0000166
Graeme Russ1bab1042010-04-24 00:05:49 +1000167 /* We are now setting up the real-mode version of the header */
168 hdr = (struct setup_header *)(setup_base + SETUP_SECTS_OFF);
169
wdenk591dda52002-11-18 00:14:45 +0000170 if (bootproto >= 0x0200) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000171 hdr->type_of_loader = 8;
172
173 if (hdr->setup_sects >= 15)
174 printf("Linux kernel version %s\n", (char *)
175 (setup_base + (hdr->kernel_version + 0x200)));
176 else
177 printf("Setup Sectors < 15 - Cannot print kernel version.\n");
wdenk57b2d802003-06-27 21:31:46 +0000178
wdenk591dda52002-11-18 00:14:45 +0000179 if (initrd_addr) {
180 printf("Initial RAM disk at linear address 0x%08lx, size %ld bytes\n",
181 initrd_addr, initrd_size);
wdenk57b2d802003-06-27 21:31:46 +0000182
Graeme Russ1bab1042010-04-24 00:05:49 +1000183 hdr->ramdisk_image = initrd_addr;
184 hdr->ramdisk_size = initrd_size;
wdenk591dda52002-11-18 00:14:45 +0000185 }
186 }
wdenk57b2d802003-06-27 21:31:46 +0000187
wdenk591dda52002-11-18 00:14:45 +0000188 if (bootproto >= 0x0201) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000189 hdr->heap_end_ptr = HEAP_END_OFFSET;
190 hdr->loadflags |= HEAP_FLAG;
wdenk591dda52002-11-18 00:14:45 +0000191 }
wdenk57b2d802003-06-27 21:31:46 +0000192
wdenk591dda52002-11-18 00:14:45 +0000193 if (bootproto >= 0x0202) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000194 hdr->cmd_line_ptr = (u32)setup_base + COMMAND_LINE_OFFSET;
wdenk591dda52002-11-18 00:14:45 +0000195 } else if (bootproto >= 0x0200) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000196
wdenk591dda52002-11-18 00:14:45 +0000197 *(u16*)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC;
wdenk57b2d802003-06-27 21:31:46 +0000198 *(u16*)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET;
Graeme Russ1bab1042010-04-24 00:05:49 +1000199
200 hdr->setup_move_size = 0x9100;
wdenk591dda52002-11-18 00:14:45 +0000201 }
wdenk591dda52002-11-18 00:14:45 +0000202
Graeme Russ1bab1042010-04-24 00:05:49 +1000203 if (bootproto >= 0x0204)
204 kernel_size = hdr->syssize * 16;
205 else
206 kernel_size -= setup_size;
207
wdenk57b2d802003-06-27 21:31:46 +0000208
wdenk591dda52002-11-18 00:14:45 +0000209 if (big_image) {
Graeme Russ1bab1042010-04-24 00:05:49 +1000210 if ((kernel_size) > BZIMAGE_MAX_SIZE) {
wdenk591dda52002-11-18 00:14:45 +0000211 printf("Error: bzImage kernel too big! (size: %ld, max: %d)\n",
Graeme Russ1bab1042010-04-24 00:05:49 +1000212 kernel_size, BZIMAGE_MAX_SIZE);
wdenk591dda52002-11-18 00:14:45 +0000213 return 0;
214 }
wdenk57b2d802003-06-27 21:31:46 +0000215
Graeme Russ1bab1042010-04-24 00:05:49 +1000216 } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
wdenk591dda52002-11-18 00:14:45 +0000217 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
Graeme Russ1bab1042010-04-24 00:05:49 +1000218 kernel_size, ZIMAGE_MAX_SIZE);
wdenk591dda52002-11-18 00:14:45 +0000219 return 0;
220 }
wdenk57b2d802003-06-27 21:31:46 +0000221
wdenk591dda52002-11-18 00:14:45 +0000222 /* build command line at COMMAND_LINE_OFFSET */
223 build_command_line(setup_base + COMMAND_LINE_OFFSET, auto_boot);
wdenk57b2d802003-06-27 21:31:46 +0000224
225 printf("Loading %czImage at address 0x%08x (%ld bytes)\n", big_image ? 'b' : ' ',
Graeme Russ1bab1042010-04-24 00:05:49 +1000226 (u32)load_address, kernel_size);
wdenk591dda52002-11-18 00:14:45 +0000227
wdenk57b2d802003-06-27 21:31:46 +0000228
Graeme Russ1bab1042010-04-24 00:05:49 +1000229 memmove(load_address, image + setup_size, kernel_size);
wdenk57b2d802003-06-27 21:31:46 +0000230
wdenk591dda52002-11-18 00:14:45 +0000231 /* ready for booting */
232 return setup_base;
233}
234
wdenk591dda52002-11-18 00:14:45 +0000235void boot_zimage(void *setup_base)
236{
237 struct pt_regs regs;
wdenk57b2d802003-06-27 21:31:46 +0000238
wdenk591dda52002-11-18 00:14:45 +0000239 memset(&regs, 0, sizeof(struct pt_regs));
240 regs.xds = (u32)setup_base >> 4;
Graeme Russ1bab1042010-04-24 00:05:49 +1000241 regs.xes = regs.xds;
242 regs.xss = regs.xds;
wdenkabda5ca2003-05-31 18:35:21 +0000243 regs.esp = 0x9000;
wdenk591dda52002-11-18 00:14:45 +0000244 regs.eflags = 0;
245 enter_realmode(((u32)setup_base+SETUP_START_OFFSET)>>4, 0, &regs, &regs);
246}
Graeme Russ1bab1042010-04-24 00:05:49 +1000247
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200248int do_zboot (cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
Graeme Russ1bab1042010-04-24 00:05:49 +1000249{
250 void *base_ptr;
Graeme Russ3134be22010-10-07 20:03:19 +1100251 void *bzImage_addr = NULL;
252 char *s;
Graeme Russ1bab1042010-04-24 00:05:49 +1000253 ulong bzImage_size = 0;
254
255 disable_interrupts();
256
257 /* Setup board for maximum PC/AT Compatibility */
258 setup_pcat_compatibility();
259
Graeme Russ3134be22010-10-07 20:03:19 +1100260 if (argc >= 2)
261 /* argv[1] holds the address of the bzImage */
262 s = argv[1];
263 else
264 s = getenv("fileaddr");
Graeme Russ1bab1042010-04-24 00:05:49 +1000265
Graeme Russ3134be22010-10-07 20:03:19 +1100266 if (s)
267 bzImage_addr = (void *)simple_strtoul(s, NULL, 16);
268
269 if (argc >= 3)
270 /* argv[2] holds the size of the bzImage */
Graeme Russ1bab1042010-04-24 00:05:49 +1000271 bzImage_size = simple_strtoul(argv[2], NULL, 16);
272
273 /* Lets look for*/
274 base_ptr = load_zimage (bzImage_addr, bzImage_size, 0, 0, 0);
275
276 if (NULL == base_ptr) {
277 printf ("## Kernel loading failed ...\n");
278 } else {
279 printf ("## Transferring control to Linux (at address %08x) ...\n",
280 (u32)base_ptr);
281
282 /* we assume that the kernel is in place */
283 printf("\nStarting kernel ...\n\n");
284
285 boot_zimage(base_ptr);
286 /* does not return */
287 }
288
289 return -1;
290}
291
292U_BOOT_CMD(
Graeme Russ3134be22010-10-07 20:03:19 +1100293 zboot, 2, 0, do_zboot,
Graeme Russ1bab1042010-04-24 00:05:49 +1000294 "Boot bzImage",
295 ""
296);