wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 1 | /* |
| 2 | * (C) Copyright 2002 |
| 3 | * Daniel Engström, Omicron Ceti AB, daniel@omicron.se |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 4 | * |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 5 | * 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 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 24 | /* |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 25 | * Linux i386 zImage and bzImage loading |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 26 | * |
| 27 | * based on the procdure described in |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 28 | * 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> |
| 37 | |
| 38 | /* |
| 39 | * Memory lay-out: |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 40 | * |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 41 | * relative to setup_base (which is 0x90000 currently) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 42 | * |
| 43 | * 0x0000-0x7FFF Real mode kernel |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 44 | * 0x8000-0x8FFF Stack and heap |
| 45 | * 0x9000-0x90FF Kernel command line |
| 46 | */ |
| 47 | #define DEFAULT_SETUP_BASE 0x90000 |
| 48 | #define COMMAND_LINE_OFFSET 0x9000 |
| 49 | #define HEAP_END_OFFSET 0x8e00 |
| 50 | |
| 51 | #define COMMAND_LINE_SIZE 2048 |
| 52 | |
| 53 | static void build_command_line(char *command_line, int auto_boot) |
| 54 | { |
| 55 | char *env_command_line; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 56 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 57 | command_line[0] = '\0'; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 58 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 59 | env_command_line = getenv("bootargs"); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 60 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 61 | /* set console= argument if we use a serial console */ |
| 62 | if (NULL == strstr(env_command_line, "console=")) { |
| 63 | if (0==strcmp(getenv("stdout"), "serial")) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 64 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 65 | /* We seem to use serial console */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 66 | sprintf(command_line, "console=ttyS0,%s ", |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 67 | getenv("baudrate")); |
| 68 | } |
| 69 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 70 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 71 | if (auto_boot) { |
| 72 | strcat(command_line, "auto "); |
| 73 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 74 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 75 | if (NULL != env_command_line) { |
| 76 | strcat(command_line, env_command_line); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 80 | printf("Kernel command line: \"%s\"\n", command_line); |
| 81 | } |
| 82 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 83 | void *load_zimage(char *image, unsigned long kernel_size, |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 84 | unsigned long initrd_addr, unsigned long initrd_size, |
| 85 | int auto_boot) |
| 86 | { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 87 | void *setup_base; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 88 | int setup_size; |
| 89 | int bootproto; |
| 90 | int big_image; |
| 91 | void *load_address; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 92 | |
| 93 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 94 | setup_base = (void*)DEFAULT_SETUP_BASE; /* base address for real-mode segment */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 95 | |
| 96 | if (KERNEL_MAGIC != *(u16*)(image + BOOT_FLAG_OFF)) { |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 97 | printf("Error: Invalid kernel magic (found 0x%04x, expected 0xaa55)\n", |
| 98 | *(u16*)(image + BOOT_FLAG_OFF)); |
| 99 | return 0; |
| 100 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 101 | |
| 102 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 103 | /* determine boot protocol version */ |
| 104 | if (KERNEL_V2_MAGIC == *(u32*)(image+HEADER_OFF)) { |
| 105 | bootproto = *(u16*)(image+VERSION_OFF); |
| 106 | } else { |
| 107 | /* Very old kernel */ |
| 108 | bootproto = 0x0100; |
| 109 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 110 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 111 | /* determine size of setup */ |
| 112 | if (0 == *(u8*)(image + SETUP_SECTS_OFF)) { |
| 113 | setup_size = 5 * 512; |
| 114 | } else { |
| 115 | setup_size = (*(u8*)(image + SETUP_SECTS_OFF) + 1) * 512; |
| 116 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 117 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 118 | if (setup_size > SETUP_MAX_SIZE) { |
| 119 | printf("Error: Setup is too large (%d bytes)\n", setup_size); |
| 120 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 121 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 122 | /* Determine image type */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 123 | big_image = (bootproto >= 0x0200) && (*(u8*)(image + LOADFLAGS_OFF) & BIG_KERNEL_FLAG); |
| 124 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 125 | /* Derermine load address */ |
| 126 | load_address = (void*)(big_image ? BZIMAGE_LOAD_ADDR:ZIMAGE_LOAD_ADDR); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 127 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 128 | /* load setup */ |
| 129 | memmove(setup_base, image, setup_size); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 130 | |
| 131 | printf("Using boot protocol version %x.%02x\n", |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 132 | (bootproto & 0xff00) >> 8, bootproto & 0xff); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 133 | |
| 134 | |
| 135 | if (bootproto == 0x0100) { |
| 136 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 137 | *(u16*)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 138 | *(u16*)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET; |
| 139 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 140 | /* A very old kernel MUST have its real-mode code |
| 141 | * loaded at 0x90000 */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 142 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 143 | if ((u32)setup_base != 0x90000) { |
| 144 | /* Copy the real-mode kernel */ |
| 145 | memmove((void*)0x90000, setup_base, setup_size); |
| 146 | /* Copy the command line */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 147 | memmove((void*)0x99000, setup_base+COMMAND_LINE_OFFSET, |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 148 | COMMAND_LINE_SIZE); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 149 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 150 | setup_base = (void*)0x90000; /* Relocated */ |
| 151 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 152 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 153 | /* It is recommended to clear memory up to the 32K mark */ |
| 154 | memset((void*)0x90000 + setup_size, 0, SETUP_MAX_SIZE-setup_size); |
| 155 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 156 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 157 | if (bootproto >= 0x0200) { |
| 158 | *(u8*)(setup_base + TYPE_OF_LOADER_OFF) = 0xff; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 159 | printf("Linux kernel version %s\n", |
| 160 | (char*)(setup_base + SETUP_START_OFFSET + |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 161 | *(u16*)(setup_base + START_SYS_OFF + 2))); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 162 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 163 | if (initrd_addr) { |
| 164 | printf("Initial RAM disk at linear address 0x%08lx, size %ld bytes\n", |
| 165 | initrd_addr, initrd_size); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 166 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 167 | *(u32*)(setup_base + RAMDISK_IMAGE_OFF) = initrd_addr; |
| 168 | *(u32*)(setup_base + RAMDISK_SIZE_OFF)=initrd_size; |
| 169 | } |
| 170 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 171 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 172 | if (bootproto >= 0x0201) { |
| 173 | *(u16*)(setup_base + HEAP_END_PTR_OFF) = HEAP_END_OFFSET; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 174 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 175 | /* CAN_USE_HEAP */ |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 176 | *(u8*)(setup_base + LOADFLAGS_OFF) = |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 177 | *(u8*)(setup_base + LOADFLAGS_OFF) | HEAP_FLAG; |
| 178 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 179 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 180 | if (bootproto >= 0x0202) { |
| 181 | *(u32*)(setup_base + CMD_LINE_PTR_OFF) = (u32)setup_base + COMMAND_LINE_OFFSET; |
| 182 | } else if (bootproto >= 0x0200) { |
| 183 | *(u16*)(setup_base + CMD_LINE_MAGIC_OFF) = COMMAND_LINE_MAGIC; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 184 | *(u16*)(setup_base + CMD_LINE_OFFSET_OFF) = COMMAND_LINE_OFFSET; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 185 | *(u16*)(setup_base + SETUP_MOVE_SIZE_OFF) = 0x9100; |
| 186 | } |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 187 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 188 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 189 | if (big_image) { |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 190 | if ((kernel_size - setup_size) > BZIMAGE_MAX_SIZE) { |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 191 | printf("Error: bzImage kernel too big! (size: %ld, max: %d)\n", |
| 192 | kernel_size - setup_size, BZIMAGE_MAX_SIZE); |
| 193 | return 0; |
| 194 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 195 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 196 | } else if ((kernel_size - setup_size) > ZIMAGE_MAX_SIZE) { |
| 197 | printf("Error: zImage kernel too big! (size: %ld, max: %d)\n", |
| 198 | kernel_size - setup_size, ZIMAGE_MAX_SIZE); |
| 199 | return 0; |
| 200 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 201 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 202 | /* build command line at COMMAND_LINE_OFFSET */ |
| 203 | build_command_line(setup_base + COMMAND_LINE_OFFSET, auto_boot); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 204 | |
| 205 | printf("Loading %czImage at address 0x%08x (%ld bytes)\n", big_image ? 'b' : ' ', |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 206 | (u32)load_address, kernel_size - setup_size); |
| 207 | |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 208 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 209 | memmove(load_address, image + setup_size, kernel_size - setup_size); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 210 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 211 | /* ready for booting */ |
| 212 | return setup_base; |
| 213 | } |
| 214 | |
| 215 | |
| 216 | void boot_zimage(void *setup_base) |
| 217 | { |
| 218 | struct pt_regs regs; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 219 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 220 | memset(®s, 0, sizeof(struct pt_regs)); |
| 221 | regs.xds = (u32)setup_base >> 4; |
wdenk | abda5ca | 2003-05-31 18:35:21 +0000 | [diff] [blame] | 222 | regs.xss = 0x9000; |
| 223 | regs.esp = 0x9000; |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 224 | regs.eflags = 0; |
| 225 | enter_realmode(((u32)setup_base+SETUP_START_OFFSET)>>4, 0, ®s, ®s); |
| 226 | } |
| 227 | |
| 228 | |
| 229 | image_header_t *fake_zimage_header(image_header_t *hdr, void *ptr, int size) |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 230 | { |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 231 | /* There is no way to know the size of a zImage ... * |
| 232 | * so we assume that 2MB will be enough for now */ |
| 233 | #define ZIMAGE_SIZE 0x200000 |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 234 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 235 | /* load a 1MB, the loaded will have to be moved to its final |
| 236 | * position again later... */ |
| 237 | #define ZIMAGE_LOAD 0x100000 |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 238 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 239 | ulong checksum; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 240 | |
| 241 | if (KERNEL_MAGIC != *(u16*)(ptr + BOOT_FLAG_OFF)) { |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 242 | /* not a zImage or bzImage */ |
| 243 | return NULL; |
| 244 | } |
| 245 | |
| 246 | if (-1 == size) { |
| 247 | size = ZIMAGE_SIZE; |
| 248 | } |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 249 | #if 0 |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 250 | checksum = crc32 (0, ptr, size); |
| 251 | #else |
| 252 | checksum = 0; |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 253 | #endif |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 254 | memset(hdr, 0, sizeof(image_header_t)); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 255 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 256 | /* Build new header */ |
| 257 | hdr->ih_magic = htonl(IH_MAGIC); |
| 258 | hdr->ih_time = 0; |
| 259 | hdr->ih_size = htonl(size); |
| 260 | hdr->ih_load = htonl(ZIMAGE_LOAD); |
| 261 | hdr->ih_ep = 0; |
| 262 | hdr->ih_dcrc = htonl(checksum); |
| 263 | hdr->ih_os = IH_OS_LINUX; |
| 264 | hdr->ih_arch = IH_CPU_I386; |
| 265 | hdr->ih_type = IH_TYPE_KERNEL; |
| 266 | hdr->ih_comp = IH_COMP_NONE; |
| 267 | |
| 268 | strncpy((char *)hdr->ih_name, "(none)", IH_NMLEN); |
| 269 | |
| 270 | checksum = crc32(0,(const char *)hdr,sizeof(image_header_t)); |
| 271 | |
| 272 | hdr->ih_hcrc = htonl(checksum); |
wdenk | 57b2d80 | 2003-06-27 21:31:46 +0000 | [diff] [blame] | 273 | |
wdenk | 591dda5 | 2002-11-18 00:14:45 +0000 | [diff] [blame] | 274 | return hdr; |
| 275 | } |