Blame view
lib_i386/zimage.c
6.07 KB
2262cfeef
|
1 2 3 |
/* * (C) Copyright 2002 * Daniel Engström, Omicron Ceti AB, daniel@omicron.se |
8bde7f776
|
4 |
* |
2262cfeef
|
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
* See file CREDITS for list of people who contributed to this * project. * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of * the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307 USA */ |
8bde7f776
|
23 |
/* |
2262cfeef
|
24 |
* Linux i386 zImage and bzImage loading |
8bde7f776
|
25 26 |
* * based on the procdure described in |
2262cfeef
|
27 28 29 30 31 32 33 34 35 36 37 38 |
* linux/Documentation/i386/boot.txt */ #include <common.h> #include <asm/io.h> #include <asm/ptrace.h> #include <asm/zimage.h> #include <asm/realmode.h> #include <asm/byteorder.h> /* * Memory lay-out: |
8bde7f776
|
39 |
* |
2262cfeef
|
40 |
* relative to setup_base (which is 0x90000 currently) |
8bde7f776
|
41 42 |
* * 0x0000-0x7FFF Real mode kernel |
2262cfeef
|
43 44 45 46 47 48 49 50 51 52 53 54 |
* 0x8000-0x8FFF Stack and heap * 0x9000-0x90FF Kernel command line */ #define DEFAULT_SETUP_BASE 0x90000 #define COMMAND_LINE_OFFSET 0x9000 #define HEAP_END_OFFSET 0x8e00 #define COMMAND_LINE_SIZE 2048 static void build_command_line(char *command_line, int auto_boot) { char *env_command_line; |
8bde7f776
|
55 |
|
2262cfeef
|
56 |
command_line[0] = '\0'; |
8bde7f776
|
57 |
|
2262cfeef
|
58 |
env_command_line = getenv("bootargs"); |
8bde7f776
|
59 |
|
2262cfeef
|
60 61 62 |
/* set console= argument if we use a serial console */ if (NULL == strstr(env_command_line, "console=")) { if (0==strcmp(getenv("stdout"), "serial")) { |
8bde7f776
|
63 |
|
2262cfeef
|
64 |
/* We seem to use serial console */ |
8bde7f776
|
65 |
sprintf(command_line, "console=ttyS0,%s ", |
2262cfeef
|
66 67 68 |
getenv("baudrate")); } } |
8bde7f776
|
69 |
|
2262cfeef
|
70 71 72 |
if (auto_boot) { strcat(command_line, "auto "); } |
8bde7f776
|
73 |
|
2262cfeef
|
74 75 |
if (NULL != env_command_line) { strcat(command_line, env_command_line); |
8bde7f776
|
76 |
} |
2262cfeef
|
77 78 79 |
printf("Kernel command line: \"%s\" ", command_line); } |
8bde7f776
|
80 |
void *load_zimage(char *image, unsigned long kernel_size, |
2262cfeef
|
81 82 83 |
unsigned long initrd_addr, unsigned long initrd_size, int auto_boot) { |
8bde7f776
|
84 |
void *setup_base; |
2262cfeef
|
85 86 87 88 |
int setup_size; int bootproto; int big_image; void *load_address; |
8bde7f776
|
89 |
|
2262cfeef
|
90 |
setup_base = (void*)DEFAULT_SETUP_BASE; /* base address for real-mode segment */ |
8bde7f776
|
91 92 |
if (KERNEL_MAGIC != *(u16*)(image + BOOT_FLAG_OFF)) { |
2262cfeef
|
93 94 95 96 97 |
printf("Error: Invalid kernel magic (found 0x%04x, expected 0xaa55) ", *(u16*)(image + BOOT_FLAG_OFF)); return 0; } |
8bde7f776
|
98 |
|
2262cfeef
|
99 100 101 102 103 104 105 |
/* determine boot protocol version */ if (KERNEL_V2_MAGIC == *(u32*)(image+HEADER_OFF)) { bootproto = *(u16*)(image+VERSION_OFF); } else { /* Very old kernel */ bootproto = 0x0100; } |
8bde7f776
|
106 |
|
2262cfeef
|
107 108 109 110 111 112 |
/* determine size of setup */ if (0 == *(u8*)(image + SETUP_SECTS_OFF)) { setup_size = 5 * 512; } else { setup_size = (*(u8*)(image + SETUP_SECTS_OFF) + 1) * 512; } |
8bde7f776
|
113 |
|
2262cfeef
|
114 115 116 117 |
if (setup_size > SETUP_MAX_SIZE) { printf("Error: Setup is too large (%d bytes) ", setup_size); } |
8bde7f776
|
118 |
|
2262cfeef
|
119 |
/* Determine image type */ |
8bde7f776
|
120 |
big_image = (bootproto >= 0x0200) && (*(u8*)(image + LOADFLAGS_OFF) & BIG_KERNEL_FLAG); |
2262cfeef
|
121 122 |
/* Derermine load address */ load_address = (void*)(big_image ? BZIMAGE_LOAD_ADDR:ZIMAGE_LOAD_ADDR); |
8bde7f776
|
123 |
|
2262cfeef
|
124 125 |
/* load setup */ memmove(setup_base, image, setup_size); |
8bde7f776
|
126 127 128 |
printf("Using boot protocol version %x.%02x ", |
2262cfeef
|
129 |
(bootproto & 0xff00) >> 8, bootproto & 0xff); |
8bde7f776
|
130 131 132 |
if (bootproto == 0x0100) { |
2262cfeef
|
133 |
*(u16*)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC; |
8bde7f776
|
134 |
*(u16*)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET; |
2262cfeef
|
135 136 |
/* A very old kernel MUST have its real-mode code * loaded at 0x90000 */ |
8bde7f776
|
137 |
|
2262cfeef
|
138 139 140 141 |
if ((u32)setup_base != 0x90000) { /* Copy the real-mode kernel */ memmove((void*)0x90000, setup_base, setup_size); /* Copy the command line */ |
8bde7f776
|
142 |
memmove((void*)0x99000, setup_base+COMMAND_LINE_OFFSET, |
2262cfeef
|
143 |
COMMAND_LINE_SIZE); |
8bde7f776
|
144 |
|
2262cfeef
|
145 146 |
setup_base = (void*)0x90000; /* Relocated */ } |
8bde7f776
|
147 |
|
2262cfeef
|
148 149 150 |
/* It is recommended to clear memory up to the 32K mark */ memset((void*)0x90000 + setup_size, 0, SETUP_MAX_SIZE-setup_size); } |
8bde7f776
|
151 |
|
2262cfeef
|
152 153 |
if (bootproto >= 0x0200) { *(u8*)(setup_base + TYPE_OF_LOADER_OFF) = 0xff; |
8bde7f776
|
154 155 156 |
printf("Linux kernel version %s ", (char*)(setup_base + SETUP_START_OFFSET + |
2262cfeef
|
157 |
*(u16*)(setup_base + START_SYS_OFF + 2))); |
8bde7f776
|
158 |
|
2262cfeef
|
159 160 161 162 |
if (initrd_addr) { printf("Initial RAM disk at linear address 0x%08lx, size %ld bytes ", initrd_addr, initrd_size); |
8bde7f776
|
163 |
|
2262cfeef
|
164 165 166 167 |
*(u32*)(setup_base + RAMDISK_IMAGE_OFF) = initrd_addr; *(u32*)(setup_base + RAMDISK_SIZE_OFF)=initrd_size; } } |
8bde7f776
|
168 |
|
2262cfeef
|
169 170 |
if (bootproto >= 0x0201) { *(u16*)(setup_base + HEAP_END_PTR_OFF) = HEAP_END_OFFSET; |
8bde7f776
|
171 |
|
2262cfeef
|
172 |
/* CAN_USE_HEAP */ |
8bde7f776
|
173 |
*(u8*)(setup_base + LOADFLAGS_OFF) = |
2262cfeef
|
174 175 |
*(u8*)(setup_base + LOADFLAGS_OFF) | HEAP_FLAG; } |
8bde7f776
|
176 |
|
2262cfeef
|
177 178 179 180 |
if (bootproto >= 0x0202) { *(u32*)(setup_base + CMD_LINE_PTR_OFF) = (u32)setup_base + COMMAND_LINE_OFFSET; } else if (bootproto >= 0x0200) { *(u16*)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC; |
8bde7f776
|
181 |
*(u16*)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET; |
2262cfeef
|
182 183 |
*(u16*)(setup_base + SETUP_MOVE_SIZE_OFF) = 0x9100; } |
2262cfeef
|
184 |
|
8bde7f776
|
185 |
|
2262cfeef
|
186 |
if (big_image) { |
8bde7f776
|
187 |
if ((kernel_size - setup_size) > BZIMAGE_MAX_SIZE) { |
2262cfeef
|
188 189 190 191 192 |
printf("Error: bzImage kernel too big! (size: %ld, max: %d) ", kernel_size - setup_size, BZIMAGE_MAX_SIZE); return 0; } |
8bde7f776
|
193 |
|
2262cfeef
|
194 195 196 197 198 199 |
} else if ((kernel_size - setup_size) > ZIMAGE_MAX_SIZE) { printf("Error: zImage kernel too big! (size: %ld, max: %d) ", kernel_size - setup_size, ZIMAGE_MAX_SIZE); return 0; } |
8bde7f776
|
200 |
|
2262cfeef
|
201 202 |
/* build command line at COMMAND_LINE_OFFSET */ build_command_line(setup_base + COMMAND_LINE_OFFSET, auto_boot); |
8bde7f776
|
203 204 205 |
printf("Loading %czImage at address 0x%08x (%ld bytes) ", big_image ? 'b' : ' ', |
2262cfeef
|
206 |
(u32)load_address, kernel_size - setup_size); |
8bde7f776
|
207 |
|
2262cfeef
|
208 |
memmove(load_address, image + setup_size, kernel_size - setup_size); |
8bde7f776
|
209 |
|
2262cfeef
|
210 211 212 |
/* ready for booting */ return setup_base; } |
2262cfeef
|
213 214 215 |
void boot_zimage(void *setup_base) { struct pt_regs regs; |
8bde7f776
|
216 |
|
2262cfeef
|
217 218 |
memset(®s, 0, sizeof(struct pt_regs)); regs.xds = (u32)setup_base >> 4; |
7a8e9bed1
|
219 220 |
regs.xss = 0x9000; regs.esp = 0x9000; |
2262cfeef
|
221 222 223 |
regs.eflags = 0; enter_realmode(((u32)setup_base+SETUP_START_OFFSET)>>4, 0, ®s, ®s); } |