x86: Clean up the x86 zimage code in preparation to extend it
[platform/kernel/u-boot.git] / arch / x86 / lib / zimage.c
1 /*
2  * Copyright (c) 2011 The Chromium OS Authors.
3  * (C) Copyright 2002
4  * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
5  *
6  * See file CREDITS for list of people who contributed to this
7  * project.
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License as
11  * published by the Free Software Foundation; either version 2 of
12  * the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
22  * MA 02111-1307 USA
23  */
24
25 /*
26  * Linux x86 zImage and bzImage loading
27  *
28  * based on the procdure described in
29  * linux/Documentation/i386/boot.txt
30  */
31
32 #include <common.h>
33 #include <asm/io.h>
34 #include <asm/ptrace.h>
35 #include <asm/zimage.h>
36 #include <asm/realmode.h>
37 #include <asm/byteorder.h>
38 #include <asm/bootparam.h>
39
40 /*
41  * Memory lay-out:
42  *
43  * relative to setup_base (which is 0x90000 currently)
44  *
45  *      0x0000-0x7FFF   Real mode kernel
46  *      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
55 static void build_command_line(char *command_line, int auto_boot)
56 {
57         char *env_command_line;
58
59         command_line[0] = '\0';
60
61         env_command_line =  getenv("bootargs");
62
63         /* set console= argument if we use a serial console */
64         if (!strstr(env_command_line, "console=")) {
65                 if (!strcmp(getenv("stdout"), "serial")) {
66
67                         /* We seem to use serial console */
68                         sprintf(command_line, "console=ttyS0,%s ",
69                                 getenv("baudrate"));
70                 }
71         }
72
73         if (auto_boot)
74                 strcat(command_line, "auto ");
75
76         if (env_command_line)
77                 strcat(command_line, env_command_line);
78
79         printf("Kernel command line: \"%s\"\n", command_line);
80 }
81
82 void *load_zimage(char *image, unsigned long kernel_size,
83                   unsigned long initrd_addr, unsigned long initrd_size,
84                   int auto_boot)
85 {
86         struct boot_params *setup_base;
87         int setup_size;
88         int bootproto;
89         int big_image;
90         void *load_address;
91
92         struct boot_params *params = (struct boot_params *)image;
93         struct setup_header *hdr = &params->hdr;
94
95         /* base address for real-mode segment */
96         setup_base = (struct boot_params *)DEFAULT_SETUP_BASE;
97
98         if (KERNEL_MAGIC != hdr->boot_flag) {
99                 printf("Error: Invalid Boot Flag "
100                         "(found 0x%04x, expected 0x%04x)\n",
101                         hdr->boot_flag, KERNEL_MAGIC);
102                 return 0;
103         } else {
104                 printf("Valid Boot Flag\n");
105         }
106
107         /* determine boot protocol version */
108         if (KERNEL_V2_MAGIC == hdr->header) {
109                 printf("Magic signature found\n");
110
111                 bootproto = hdr->version;
112         } else {
113                 /* Very old kernel */
114                 printf("Magic signature not found\n");
115                 bootproto = 0x0100;
116         }
117
118         /* determine size of setup */
119         if (0 == hdr->setup_sects) {
120                 printf("Setup Sectors = 0 (defaulting to 4)\n");
121                 setup_size = 5 * 512;
122         } else {
123                 setup_size = (hdr->setup_sects + 1) * 512;
124         }
125
126         printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
127
128         if (setup_size > SETUP_MAX_SIZE)
129                 printf("Error: Setup is too large (%d bytes)\n", setup_size);
130
131         /* Determine image type */
132         big_image = (bootproto >= 0x0200) &&
133                     (hdr->loadflags & BIG_KERNEL_FLAG);
134
135         /* Determine load address */
136         if (big_image)
137                 load_address = (void *)BZIMAGE_LOAD_ADDR;
138         else
139                 load_address = (void *)ZIMAGE_LOAD_ADDR;
140
141         /* load setup */
142         printf("Moving Real-Mode Code to 0x%8.8lx (%d bytes)\n",
143                (ulong)setup_base, setup_size);
144         memmove(setup_base, image, setup_size);
145
146         printf("Using boot protocol version %x.%02x\n",
147                (bootproto & 0xff00) >> 8, bootproto & 0xff);
148
149         if (bootproto == 0x0100) {
150                 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
151                 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
152
153                 /*
154                  * A very old kernel MUST have its real-mode code
155                  * loaded at 0x90000
156                  */
157                 if ((u32)setup_base != 0x90000) {
158                         /* Copy the real-mode kernel */
159                         memmove((void *)0x90000, setup_base, setup_size);
160
161                         /* Copy the command line */
162                         memmove((void *)0x99000,
163                                 (u8 *)setup_base + COMMAND_LINE_OFFSET,
164                                 COMMAND_LINE_SIZE);
165
166                          /* Relocated */
167                         setup_base = (struct boot_params *)0x90000;
168                 }
169
170                 /* It is recommended to clear memory up to the 32K mark */
171                 memset((u8 *)0x90000 + setup_size, 0,
172                        SETUP_MAX_SIZE - setup_size);
173         }
174
175         /* We are now setting up the real-mode version of the header */
176         hdr = &setup_base->hdr;
177
178         if (bootproto >= 0x0200) {
179                 hdr->type_of_loader = 8;
180
181                 if (hdr->setup_sects >= 15) {
182                         printf("Linux kernel version %s\n",
183                                (char *)setup_base +
184                                hdr->kernel_version + 0x200);
185                 } else {
186                         printf("Setup Sectors < 15 - "
187                                "Cannot print kernel version.\n");
188                 }
189
190                 if (initrd_addr) {
191                         printf("Initial RAM disk at linear address "
192                                "0x%08lx, size %ld bytes\n",
193                                initrd_addr, initrd_size);
194
195                         hdr->ramdisk_image = initrd_addr;
196                         hdr->ramdisk_size = initrd_size;
197                 }
198         }
199
200         if (bootproto >= 0x0201) {
201                 hdr->heap_end_ptr = HEAP_END_OFFSET;
202                 hdr->loadflags |= HEAP_FLAG;
203         }
204
205         if (bootproto >= 0x0202) {
206                 hdr->cmd_line_ptr =
207                         (uintptr_t)setup_base + COMMAND_LINE_OFFSET;
208         } else if (bootproto >= 0x0200) {
209                 setup_base->screen_info.cl_magic = COMMAND_LINE_MAGIC;
210                 setup_base->screen_info.cl_offset = COMMAND_LINE_OFFSET;
211
212                 hdr->setup_move_size = 0x9100;
213         }
214
215         if (bootproto >= 0x0204)
216                 kernel_size = hdr->syssize * 16;
217         else
218                 kernel_size -= setup_size;
219
220
221         if (big_image) {
222                 if ((kernel_size) > BZIMAGE_MAX_SIZE) {
223                         printf("Error: bzImage kernel too big! "
224                                 "(size: %ld, max: %d)\n",
225                                 kernel_size, BZIMAGE_MAX_SIZE);
226                         return 0;
227                 }
228         } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
229                 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
230                        kernel_size, ZIMAGE_MAX_SIZE);
231                 return 0;
232         }
233
234         /* build command line at COMMAND_LINE_OFFSET */
235         build_command_line((char *)setup_base + COMMAND_LINE_OFFSET, auto_boot);
236
237         printf("Loading %czImage at address 0x%08x (%ld bytes)\n",
238                big_image ? 'b' : ' ', (u32)load_address, kernel_size);
239
240
241         memmove(load_address, image + setup_size, kernel_size);
242
243         /* ready for booting */
244         return setup_base;
245 }
246
247 void boot_zimage(void *setup_base)
248 {
249         struct pt_regs regs;
250
251         memset(&regs, 0, sizeof(struct pt_regs));
252         regs.xds = (u32)setup_base >> 4;
253         regs.xes = regs.xds;
254         regs.xss = regs.xds;
255         regs.esp = 0x9000;
256         regs.eflags = 0;
257         enter_realmode(((u32)setup_base + SETUP_START_OFFSET) >> 4, 0,
258                        &regs, &regs);
259 }
260
261 int do_zboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
262 {
263         void *base_ptr;
264         void *bzImage_addr = NULL;
265         char *s;
266         ulong bzImage_size = 0;
267
268         disable_interrupts();
269
270         /* Setup board for maximum PC/AT Compatibility */
271         setup_pcat_compatibility();
272
273         if (argc >= 2) {
274                 /* argv[1] holds the address of the bzImage */
275                 s = argv[1];
276         } else {
277                 s = getenv("fileaddr");
278         }
279
280         if (s)
281                 bzImage_addr = (void *)simple_strtoul(s, NULL, 16);
282
283         if (argc >= 3)
284                 /* argv[2] holds the size of the bzImage */
285                 bzImage_size = simple_strtoul(argv[2], NULL, 16);
286
287         /* Lets look for */
288         base_ptr = load_zimage(bzImage_addr, bzImage_size, 0, 0, 0);
289
290         if (!base_ptr) {
291                 printf("## Kernel loading failed ...\n");
292         } else {
293                 printf("## Transferring control to Linux "
294                        "(at address %08x) ...\n",
295                        (u32)base_ptr);
296
297                 /* we assume that the kernel is in place */
298                 printf("\nStarting kernel ...\n\n");
299
300                 boot_zimage(base_ptr);
301                 /* does not return */
302         }
303
304         return -1;
305 }
306
307 U_BOOT_CMD(
308         zboot, 2, 0,    do_zboot,
309         "Boot bzImage",
310         ""
311 );