3 * Daniel Engström, Omicron Ceti AB, <daniel@omicron.se>
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
25 * Linux x86 zImage and bzImage loading
27 * based on the procdure described in
28 * linux/Documentation/i386/boot.txt
33 #include <asm/ptrace.h>
34 #include <asm/zimage.h>
35 #include <asm/realmode.h>
36 #include <asm/byteorder.h>
37 #include <asm/bootparam.h>
42 * relative to setup_base (which is 0x90000 currently)
44 * 0x0000-0x7FFF Real mode kernel
45 * 0x8000-0x8FFF Stack and heap
46 * 0x9000-0x90FF Kernel command line
48 #define DEFAULT_SETUP_BASE 0x90000
49 #define COMMAND_LINE_OFFSET 0x9000
50 #define HEAP_END_OFFSET 0x8e00
52 #define COMMAND_LINE_SIZE 2048
54 static void build_command_line(char *command_line, int auto_boot)
56 char *env_command_line;
58 command_line[0] = '\0';
60 env_command_line = getenv("bootargs");
62 /* set console= argument if we use a serial console */
63 if (!strstr(env_command_line, "console=")) {
64 if (!strcmp(getenv("stdout"), "serial")) {
66 /* We seem to use serial console */
67 sprintf(command_line, "console=ttyS0,%s ",
73 strcat(command_line, "auto ");
76 strcat(command_line, env_command_line);
78 printf("Kernel command line: \"%s\"\n", command_line);
81 void *load_zimage(char *image, unsigned long kernel_size,
82 unsigned long initrd_addr, unsigned long initrd_size,
90 struct setup_header *hdr;
92 hdr = (struct setup_header *)(image + SETUP_SECTS_OFF);
94 /* base address for real-mode segment */
95 setup_base = (void *)DEFAULT_SETUP_BASE;
97 if (KERNEL_MAGIC != hdr->boot_flag) {
98 printf("Error: Invalid Boot Flag (found 0x%04x, expected 0x%04x)\n",
99 hdr->boot_flag, KERNEL_MAGIC);
102 printf("Valid Boot Flag\n");
105 /* determine boot protocol version */
106 if (KERNEL_V2_MAGIC == hdr->header) {
107 printf("Magic signature found\n");
109 bootproto = hdr->version;
111 /* Very old kernel */
112 printf("Magic signature not found\n");
116 /* determine size of setup */
117 if (0 == hdr->setup_sects) {
118 printf("Setup Sectors = 0 (defaulting to 4)\n");
119 setup_size = 5 * 512;
121 setup_size = (hdr->setup_sects + 1) * 512;
124 printf("Setup Size = 0x%8.8lx\n", (ulong)setup_size);
126 if (setup_size > SETUP_MAX_SIZE)
127 printf("Error: Setup is too large (%d bytes)\n", setup_size);
129 /* Determine image type */
130 big_image = (bootproto >= 0x0200) &&
131 (hdr->loadflags & BIG_KERNEL_FLAG);
133 /* Determine load address */
134 load_address = (void *)(big_image ?
139 printf("Moving Real-Mode Code to 0x%8.8lx (%d bytes)\n",
140 (ulong)setup_base, setup_size);
141 memmove(setup_base, image, setup_size);
143 printf("Using boot protocol version %x.%02x\n",
144 (bootproto & 0xff00) >> 8, bootproto & 0xff);
146 if (bootproto == 0x0100) {
147 *(u16 *)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC;
148 *(u16 *)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET;
151 * A very old kernel MUST have its real-mode code
154 if ((u32)setup_base != 0x90000) {
155 /* Copy the real-mode kernel */
156 memmove((void *)0x90000, setup_base, setup_size);
158 /* Copy the command line */
159 memmove((void *)0x99000,
160 setup_base + COMMAND_LINE_OFFSET,
164 setup_base = (void *)0x90000;
167 /* It is recommended to clear memory up to the 32K mark */
168 memset((void *)0x90000 + setup_size, 0,
169 SETUP_MAX_SIZE-setup_size);
172 /* We are now setting up the real-mode version of the header */
173 hdr = (struct setup_header *)(setup_base + SETUP_SECTS_OFF);
175 if (bootproto >= 0x0200) {
176 hdr->type_of_loader = 8;
178 if (hdr->setup_sects >= 15)
179 printf("Linux kernel version %s\n",
180 (char *)(setup_base +
181 (hdr->kernel_version + 0x200)));
183 printf("Setup Sectors < 15 - Cannot print kernel version.\n");
186 printf("Initial RAM disk at linear address 0x%08lx, size %ld bytes\n",
187 initrd_addr, initrd_size);
189 hdr->ramdisk_image = initrd_addr;
190 hdr->ramdisk_size = initrd_size;
194 if (bootproto >= 0x0201) {
195 hdr->heap_end_ptr = HEAP_END_OFFSET;
196 hdr->loadflags |= HEAP_FLAG;
199 if (bootproto >= 0x0202) {
200 hdr->cmd_line_ptr = (u32)setup_base + COMMAND_LINE_OFFSET;
201 } else if (bootproto >= 0x0200) {
203 *(u16 *)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC;
204 *(u16 *)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET;
206 hdr->setup_move_size = 0x9100;
209 if (bootproto >= 0x0204)
210 kernel_size = hdr->syssize * 16;
212 kernel_size -= setup_size;
216 if ((kernel_size) > BZIMAGE_MAX_SIZE) {
217 printf("Error: bzImage kernel too big! (size: %ld, max: %d)\n",
218 kernel_size, BZIMAGE_MAX_SIZE);
222 } else if ((kernel_size) > ZIMAGE_MAX_SIZE) {
223 printf("Error: zImage kernel too big! (size: %ld, max: %d)\n",
224 kernel_size, ZIMAGE_MAX_SIZE);
228 /* build command line at COMMAND_LINE_OFFSET */
229 build_command_line(setup_base + COMMAND_LINE_OFFSET, auto_boot);
231 printf("Loading %czImage at address 0x%08x (%ld bytes)\n",
232 big_image ? 'b' : ' ', (u32)load_address, kernel_size);
235 memmove(load_address, image + setup_size, kernel_size);
237 /* ready for booting */
241 void boot_zimage(void *setup_base)
245 memset(®s, 0, sizeof(struct pt_regs));
246 regs.xds = (u32)setup_base >> 4;
251 enter_realmode(((u32)setup_base+SETUP_START_OFFSET)>>4, 0, ®s,
255 int do_zboot(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
258 void *bzImage_addr = NULL;
260 ulong bzImage_size = 0;
262 disable_interrupts();
264 /* Setup board for maximum PC/AT Compatibility */
265 setup_pcat_compatibility();
268 /* argv[1] holds the address of the bzImage */
271 s = getenv("fileaddr");
274 bzImage_addr = (void *)simple_strtoul(s, NULL, 16);
277 /* argv[2] holds the size of the bzImage */
278 bzImage_size = simple_strtoul(argv[2], NULL, 16);
281 base_ptr = load_zimage(bzImage_addr, bzImage_size, 0, 0, 0);
284 printf("## Kernel loading failed ...\n");
286 printf("## Transferring control to Linux (at address %08x) ...\n",
289 /* we assume that the kernel is in place */
290 printf("\nStarting kernel ...\n\n");
292 boot_zimage(base_ptr);
293 /* does not return */
300 zboot, 2, 0, do_zboot,