Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) Paul Mackerras 1997. |
| 3 | * |
| 4 | * Updates for PPC64 by Todd Inglett, Dave Engebretsen & Peter Bergner. |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | */ |
Olaf Hering | decd300 | 2005-08-08 13:24:38 +1000 | [diff] [blame] | 11 | #include <stdarg.h> |
| 12 | #include <stddef.h> |
| 13 | #include "elf.h" |
| 14 | #include "page.h" |
| 15 | #include "string.h" |
| 16 | #include "stdio.h" |
| 17 | #include "prom.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 18 | #include "zlib.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 19 | |
Olaf Hering | decd300 | 2005-08-08 13:24:38 +1000 | [diff] [blame] | 20 | static void gunzip(void *, int, unsigned char *, int *); |
| 21 | extern void flush_cache(void *, unsigned long); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | |
| 24 | /* Value picked to match that used by yaboot */ |
| 25 | #define PROG_START 0x01400000 |
Mark Bellon | 3cc747e | 2005-09-06 15:50:02 -0700 | [diff] [blame] | 26 | #define RAM_END (512<<20) // Fixme: use OF */ |
| 27 | #define ONE_MB 0x100000 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 28 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | extern char _start[]; |
Mark Bellon | 3cc747e | 2005-09-06 15:50:02 -0700 | [diff] [blame] | 30 | extern char _end[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 31 | extern char _vmlinux_start[]; |
| 32 | extern char _vmlinux_end[]; |
| 33 | extern char _initrd_start[]; |
| 34 | extern char _initrd_end[]; |
| 35 | extern unsigned long vmlinux_filesize; |
| 36 | extern unsigned long vmlinux_memsize; |
| 37 | |
| 38 | struct addr_range { |
| 39 | unsigned long addr; |
| 40 | unsigned long size; |
| 41 | unsigned long memsize; |
| 42 | }; |
Olaf Hering | decd300 | 2005-08-08 13:24:38 +1000 | [diff] [blame] | 43 | static struct addr_range vmlinux = {0, 0, 0}; |
| 44 | static struct addr_range vmlinuz = {0, 0, 0}; |
| 45 | static struct addr_range initrd = {0, 0, 0}; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 46 | |
Olaf Hering | 7054036 | 2005-10-28 17:46:38 -0700 | [diff] [blame^] | 47 | static char scratch[46912]; /* scratch space for gunzip, from zlib_inflate_workspacesize() */ |
| 48 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 49 | |
| 50 | typedef void (*kernel_entry_t)( unsigned long, |
| 51 | unsigned long, |
| 52 | void *, |
| 53 | void *); |
| 54 | |
| 55 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | #undef DEBUG |
| 57 | |
Mark Bellon | 3cc747e | 2005-09-06 15:50:02 -0700 | [diff] [blame] | 58 | static unsigned long claim_base; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 59 | |
| 60 | static unsigned long try_claim(unsigned long size) |
| 61 | { |
| 62 | unsigned long addr = 0; |
| 63 | |
Mark Bellon | 3cc747e | 2005-09-06 15:50:02 -0700 | [diff] [blame] | 64 | for(; claim_base < RAM_END; claim_base += ONE_MB) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | #ifdef DEBUG |
| 66 | printf(" trying: 0x%08lx\n\r", claim_base); |
| 67 | #endif |
| 68 | addr = (unsigned long)claim(claim_base, size, 0); |
| 69 | if ((void *)addr != (void *)-1) |
| 70 | break; |
| 71 | } |
| 72 | if (addr == 0) |
| 73 | return 0; |
| 74 | claim_base = PAGE_ALIGN(claim_base + size); |
| 75 | return addr; |
| 76 | } |
| 77 | |
| 78 | void start(unsigned long a1, unsigned long a2, void *promptr) |
| 79 | { |
| 80 | unsigned long i; |
| 81 | kernel_entry_t kernel_entry; |
| 82 | Elf64_Ehdr *elf64; |
| 83 | Elf64_Phdr *elf64ph; |
| 84 | |
| 85 | prom = (int (*)(void *)) promptr; |
| 86 | chosen_handle = finddevice("/chosen"); |
| 87 | if (chosen_handle == (void *) -1) |
| 88 | exit(); |
| 89 | if (getprop(chosen_handle, "stdout", &stdout, sizeof(stdout)) != 4) |
| 90 | exit(); |
| 91 | stderr = stdout; |
| 92 | if (getprop(chosen_handle, "stdin", &stdin, sizeof(stdin)) != 4) |
| 93 | exit(); |
| 94 | |
Mark Bellon | 3cc747e | 2005-09-06 15:50:02 -0700 | [diff] [blame] | 95 | printf("\n\rzImage starting: loaded at 0x%lx\n\r", (unsigned long) _start); |
| 96 | |
| 97 | /* |
| 98 | * The first available claim_base must be above the end of the |
| 99 | * the loaded kernel wrapper file (_start to _end includes the |
| 100 | * initrd image if it is present) and rounded up to a nice |
| 101 | * 1 MB boundary for good measure. |
| 102 | */ |
| 103 | |
| 104 | claim_base = _ALIGN_UP((unsigned long)_end, ONE_MB); |
| 105 | |
| 106 | #if defined(PROG_START) |
| 107 | /* |
| 108 | * Maintain a "magic" minimum address. This keeps some older |
| 109 | * firmware platforms running. |
| 110 | */ |
| 111 | |
| 112 | if (claim_base < PROG_START) |
| 113 | claim_base = PROG_START; |
| 114 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 115 | |
| 116 | /* |
| 117 | * Now we try to claim some memory for the kernel itself |
| 118 | * our "vmlinux_memsize" is the memory footprint in RAM, _HOWEVER_, what |
| 119 | * our Makefile stuffs in is an image containing all sort of junk including |
| 120 | * an ELF header. We need to do some calculations here to find the right |
| 121 | * size... In practice we add 1Mb, that is enough, but we should really |
| 122 | * consider fixing the Makefile to put a _raw_ kernel in there ! |
| 123 | */ |
Mark Bellon | 3cc747e | 2005-09-06 15:50:02 -0700 | [diff] [blame] | 124 | vmlinux_memsize += ONE_MB; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 125 | printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux_memsize); |
| 126 | vmlinux.addr = try_claim(vmlinux_memsize); |
| 127 | if (vmlinux.addr == 0) { |
| 128 | printf("Can't allocate memory for kernel image !\n\r"); |
| 129 | exit(); |
| 130 | } |
| 131 | vmlinuz.addr = (unsigned long)_vmlinux_start; |
| 132 | vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start); |
| 133 | vmlinux.size = PAGE_ALIGN(vmlinux_filesize); |
| 134 | vmlinux.memsize = vmlinux_memsize; |
| 135 | |
| 136 | /* |
| 137 | * Now we try to claim memory for the initrd (and copy it there) |
| 138 | */ |
| 139 | initrd.size = (unsigned long)(_initrd_end - _initrd_start); |
| 140 | initrd.memsize = initrd.size; |
| 141 | if ( initrd.size > 0 ) { |
| 142 | printf("Allocating 0x%lx bytes for initrd ...\n\r", initrd.size); |
| 143 | initrd.addr = try_claim(initrd.size); |
| 144 | if (initrd.addr == 0) { |
| 145 | printf("Can't allocate memory for initial ramdisk !\n\r"); |
| 146 | exit(); |
| 147 | } |
| 148 | a1 = initrd.addr; |
| 149 | a2 = initrd.size; |
Olaf Hering | b1bdfbd | 2005-06-28 21:01:46 +1000 | [diff] [blame] | 150 | printf("initial ramdisk moving 0x%lx <- 0x%lx (0x%lx bytes)\n\r", |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 151 | initrd.addr, (unsigned long)_initrd_start, initrd.size); |
| 152 | memmove((void *)initrd.addr, (void *)_initrd_start, initrd.size); |
Olaf Hering | b1bdfbd | 2005-06-28 21:01:46 +1000 | [diff] [blame] | 153 | printf("initrd head: 0x%lx\n\r", *((unsigned long *)initrd.addr)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | /* Eventually gunzip the kernel */ |
| 157 | if (*(unsigned short *)vmlinuz.addr == 0x1f8b) { |
| 158 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 159 | printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...", |
| 160 | vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size); |
| 161 | len = vmlinuz.size; |
| 162 | gunzip((void *)vmlinux.addr, vmlinux.size, |
| 163 | (unsigned char *)vmlinuz.addr, &len); |
| 164 | printf("done 0x%lx bytes\n\r", len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 165 | } else { |
| 166 | memmove((void *)vmlinux.addr,(void *)vmlinuz.addr,vmlinuz.size); |
| 167 | } |
| 168 | |
| 169 | /* Skip over the ELF header */ |
| 170 | elf64 = (Elf64_Ehdr *)vmlinux.addr; |
| 171 | if ( elf64->e_ident[EI_MAG0] != ELFMAG0 || |
| 172 | elf64->e_ident[EI_MAG1] != ELFMAG1 || |
| 173 | elf64->e_ident[EI_MAG2] != ELFMAG2 || |
| 174 | elf64->e_ident[EI_MAG3] != ELFMAG3 || |
| 175 | elf64->e_ident[EI_CLASS] != ELFCLASS64 || |
| 176 | elf64->e_ident[EI_DATA] != ELFDATA2MSB || |
| 177 | elf64->e_type != ET_EXEC || |
| 178 | elf64->e_machine != EM_PPC64 ) |
| 179 | { |
| 180 | printf("Error: not a valid PPC64 ELF file!\n\r"); |
| 181 | exit(); |
| 182 | } |
| 183 | |
| 184 | elf64ph = (Elf64_Phdr *)((unsigned long)elf64 + |
| 185 | (unsigned long)elf64->e_phoff); |
| 186 | for(i=0; i < (unsigned int)elf64->e_phnum ;i++,elf64ph++) { |
| 187 | if (elf64ph->p_type == PT_LOAD && elf64ph->p_offset != 0) |
| 188 | break; |
| 189 | } |
| 190 | #ifdef DEBUG |
| 191 | printf("... skipping 0x%lx bytes of ELF header\n\r", |
| 192 | (unsigned long)elf64ph->p_offset); |
| 193 | #endif |
| 194 | vmlinux.addr += (unsigned long)elf64ph->p_offset; |
| 195 | vmlinux.size -= (unsigned long)elf64ph->p_offset; |
| 196 | |
| 197 | flush_cache((void *)vmlinux.addr, vmlinux.size); |
| 198 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 199 | kernel_entry = (kernel_entry_t)vmlinux.addr; |
| 200 | #ifdef DEBUG |
| 201 | printf( "kernel:\n\r" |
| 202 | " entry addr = 0x%lx\n\r" |
| 203 | " a1 = 0x%lx,\n\r" |
| 204 | " a2 = 0x%lx,\n\r" |
| 205 | " prom = 0x%lx,\n\r" |
| 206 | " bi_recs = 0x%lx,\n\r", |
| 207 | (unsigned long)kernel_entry, a1, a2, |
| 208 | (unsigned long)prom, NULL); |
| 209 | #endif |
| 210 | |
| 211 | kernel_entry( a1, a2, prom, NULL ); |
| 212 | |
| 213 | printf("Error: Linux kernel returned to zImage bootloader!\n\r"); |
| 214 | |
| 215 | exit(); |
| 216 | } |
| 217 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | #define HEAD_CRC 2 |
| 219 | #define EXTRA_FIELD 4 |
| 220 | #define ORIG_NAME 8 |
| 221 | #define COMMENT 0x10 |
| 222 | #define RESERVED 0xe0 |
| 223 | |
Olaf Hering | decd300 | 2005-08-08 13:24:38 +1000 | [diff] [blame] | 224 | static void gunzip(void *dst, int dstlen, unsigned char *src, int *lenp) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 225 | { |
| 226 | z_stream s; |
| 227 | int r, i, flags; |
| 228 | |
| 229 | /* skip header */ |
| 230 | i = 10; |
| 231 | flags = src[3]; |
Olaf Hering | 7054036 | 2005-10-28 17:46:38 -0700 | [diff] [blame^] | 232 | if (src[2] != Z_DEFLATED || (flags & RESERVED) != 0) { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 233 | printf("bad gzipped data\n\r"); |
| 234 | exit(); |
| 235 | } |
| 236 | if ((flags & EXTRA_FIELD) != 0) |
| 237 | i = 12 + src[10] + (src[11] << 8); |
| 238 | if ((flags & ORIG_NAME) != 0) |
| 239 | while (src[i++] != 0) |
| 240 | ; |
| 241 | if ((flags & COMMENT) != 0) |
| 242 | while (src[i++] != 0) |
| 243 | ; |
| 244 | if ((flags & HEAD_CRC) != 0) |
| 245 | i += 2; |
| 246 | if (i >= *lenp) { |
| 247 | printf("gunzip: ran out of data in header\n\r"); |
| 248 | exit(); |
| 249 | } |
| 250 | |
Olaf Hering | 7054036 | 2005-10-28 17:46:38 -0700 | [diff] [blame^] | 251 | if (zlib_inflate_workspacesize() > sizeof(scratch)) { |
| 252 | printf("gunzip needs more mem\n"); |
| 253 | exit(); |
| 254 | } |
| 255 | memset(&s, 0, sizeof(s)); |
| 256 | s.workspace = scratch; |
| 257 | r = zlib_inflateInit2(&s, -MAX_WBITS); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 258 | if (r != Z_OK) { |
| 259 | printf("inflateInit2 returned %d\n\r", r); |
| 260 | exit(); |
| 261 | } |
| 262 | s.next_in = src + i; |
| 263 | s.avail_in = *lenp - i; |
| 264 | s.next_out = dst; |
| 265 | s.avail_out = dstlen; |
Olaf Hering | 7054036 | 2005-10-28 17:46:38 -0700 | [diff] [blame^] | 266 | r = zlib_inflate(&s, Z_FINISH); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 267 | if (r != Z_OK && r != Z_STREAM_END) { |
| 268 | printf("inflate returned %d msg: %s\n\r", r, s.msg); |
| 269 | exit(); |
| 270 | } |
| 271 | *lenp = s.next_out - (unsigned char *) dst; |
Olaf Hering | 7054036 | 2005-10-28 17:46:38 -0700 | [diff] [blame^] | 272 | zlib_inflateEnd(&s); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |