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" |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 17 | #include "ops.h" |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame^] | 18 | #include "gunzip_util.h" |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 19 | #include "flatdevtree.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
Olaf Hering | decd300 | 2005-08-08 13:24:38 +1000 | [diff] [blame] | 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 | extern char _start[]; |
Olaf Hering | 9b0cbe9 | 2005-10-28 17:46:45 -0700 | [diff] [blame] | 24 | extern char __bss_start[]; |
Mark Bellon | 3cc747e | 2005-09-06 15:50:02 -0700 | [diff] [blame] | 25 | extern char _end[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 26 | extern char _vmlinux_start[]; |
| 27 | extern char _vmlinux_end[]; |
| 28 | extern char _initrd_start[]; |
| 29 | extern char _initrd_end[]; |
Mark A. Greer | c888554 | 2006-10-16 13:49:27 -0700 | [diff] [blame] | 30 | extern char _dtb_start[]; |
| 31 | extern char _dtb_end[]; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame^] | 33 | static struct gunzip_state gzstate; |
| 34 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | struct addr_range { |
| 36 | unsigned long addr; |
| 37 | unsigned long size; |
| 38 | unsigned long memsize; |
| 39 | }; |
Olaf Hering | afbe8c4 | 2005-10-28 17:46:47 -0700 | [diff] [blame] | 40 | static struct addr_range vmlinux; |
| 41 | static struct addr_range vmlinuz; |
| 42 | static struct addr_range initrd; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Paul Mackerras | 94b212c | 2005-11-16 13:38:21 +1100 | [diff] [blame] | 44 | static unsigned long elfoffset; |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 45 | static int is_64bit; |
Paul Mackerras | 94b212c | 2005-11-16 13:38:21 +1100 | [diff] [blame] | 46 | |
Olaf Hering | 8a76baf | 2005-10-28 17:46:40 -0700 | [diff] [blame] | 47 | static char elfheader[256]; |
Olaf Hering | 7054036 | 2005-10-28 17:46:38 -0700 | [diff] [blame] | 48 | |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 49 | typedef void (*kernel_entry_t)(unsigned long, unsigned long, void *); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 50 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | #undef DEBUG |
| 52 | |
Paul Mackerras | 94b212c | 2005-11-16 13:38:21 +1100 | [diff] [blame] | 53 | static int is_elf64(void *hdr) |
| 54 | { |
| 55 | Elf64_Ehdr *elf64 = hdr; |
| 56 | Elf64_Phdr *elf64ph; |
| 57 | unsigned int i; |
| 58 | |
| 59 | if (!(elf64->e_ident[EI_MAG0] == ELFMAG0 && |
| 60 | elf64->e_ident[EI_MAG1] == ELFMAG1 && |
| 61 | elf64->e_ident[EI_MAG2] == ELFMAG2 && |
| 62 | elf64->e_ident[EI_MAG3] == ELFMAG3 && |
| 63 | elf64->e_ident[EI_CLASS] == ELFCLASS64 && |
| 64 | elf64->e_ident[EI_DATA] == ELFDATA2MSB && |
| 65 | elf64->e_type == ET_EXEC && |
| 66 | elf64->e_machine == EM_PPC64)) |
| 67 | return 0; |
| 68 | |
| 69 | elf64ph = (Elf64_Phdr *)((unsigned long)elf64 + |
| 70 | (unsigned long)elf64->e_phoff); |
| 71 | for (i = 0; i < (unsigned int)elf64->e_phnum; i++, elf64ph++) |
Olaf Hering | 158daa4 | 2006-01-30 14:28:03 +0100 | [diff] [blame] | 72 | if (elf64ph->p_type == PT_LOAD) |
Paul Mackerras | 94b212c | 2005-11-16 13:38:21 +1100 | [diff] [blame] | 73 | break; |
| 74 | if (i >= (unsigned int)elf64->e_phnum) |
| 75 | return 0; |
| 76 | |
| 77 | elfoffset = (unsigned long)elf64ph->p_offset; |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame^] | 78 | vmlinux.size = (unsigned long)elf64ph->p_filesz; |
| 79 | vmlinux.memsize = (unsigned long)elf64ph->p_memsz; |
Paul Mackerras | 66a45dd | 2006-01-14 15:04:06 +1100 | [diff] [blame] | 80 | |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 81 | is_64bit = 1; |
Paul Mackerras | 94b212c | 2005-11-16 13:38:21 +1100 | [diff] [blame] | 82 | return 1; |
| 83 | } |
| 84 | |
| 85 | static int is_elf32(void *hdr) |
| 86 | { |
| 87 | Elf32_Ehdr *elf32 = hdr; |
| 88 | Elf32_Phdr *elf32ph; |
| 89 | unsigned int i; |
| 90 | |
| 91 | if (!(elf32->e_ident[EI_MAG0] == ELFMAG0 && |
| 92 | elf32->e_ident[EI_MAG1] == ELFMAG1 && |
| 93 | elf32->e_ident[EI_MAG2] == ELFMAG2 && |
| 94 | elf32->e_ident[EI_MAG3] == ELFMAG3 && |
| 95 | elf32->e_ident[EI_CLASS] == ELFCLASS32 && |
| 96 | elf32->e_ident[EI_DATA] == ELFDATA2MSB && |
| 97 | elf32->e_type == ET_EXEC && |
| 98 | elf32->e_machine == EM_PPC)) |
| 99 | return 0; |
| 100 | |
| 101 | elf32 = (Elf32_Ehdr *)elfheader; |
| 102 | elf32ph = (Elf32_Phdr *) ((unsigned long)elf32 + elf32->e_phoff); |
| 103 | for (i = 0; i < elf32->e_phnum; i++, elf32ph++) |
Olaf Hering | 158daa4 | 2006-01-30 14:28:03 +0100 | [diff] [blame] | 104 | if (elf32ph->p_type == PT_LOAD) |
Paul Mackerras | 94b212c | 2005-11-16 13:38:21 +1100 | [diff] [blame] | 105 | break; |
| 106 | if (i >= elf32->e_phnum) |
| 107 | return 0; |
| 108 | |
| 109 | elfoffset = elf32ph->p_offset; |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame^] | 110 | vmlinux.size = elf32ph->p_filesz; |
| 111 | vmlinux.memsize = elf32ph->p_memsz; |
Paul Mackerras | 94b212c | 2005-11-16 13:38:21 +1100 | [diff] [blame] | 112 | return 1; |
| 113 | } |
| 114 | |
David Gibson | f79e083 | 2006-11-16 15:31:32 +1100 | [diff] [blame] | 115 | static void prep_kernel(unsigned long a1, unsigned long a2) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Olaf Hering | 8a76baf | 2005-10-28 17:46:40 -0700 | [diff] [blame] | 117 | int len; |
Paul Mackerras | 66a45dd | 2006-01-14 15:04:06 +1100 | [diff] [blame] | 118 | |
Paul Mackerras | 94b212c | 2005-11-16 13:38:21 +1100 | [diff] [blame] | 119 | vmlinuz.addr = (unsigned long)_vmlinux_start; |
| 120 | vmlinuz.size = (unsigned long)(_vmlinux_end - _vmlinux_start); |
| 121 | |
| 122 | /* gunzip the ELF header of the kernel */ |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame^] | 123 | gunzip_start(&gzstate, (void *)vmlinuz.addr, vmlinuz.size); |
| 124 | gunzip_exactly(&gzstate, elfheader, sizeof(elfheader)); |
Paul Mackerras | 94b212c | 2005-11-16 13:38:21 +1100 | [diff] [blame] | 125 | |
| 126 | if (!is_elf64(elfheader) && !is_elf32(elfheader)) { |
| 127 | printf("Error: not a valid PPC32 or PPC64 ELF file!\n\r"); |
| 128 | exit(); |
| 129 | } |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 130 | if (platform_ops.image_hdr) |
| 131 | platform_ops.image_hdr(elfheader); |
Paul Mackerras | 94b212c | 2005-11-16 13:38:21 +1100 | [diff] [blame] | 132 | |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame^] | 133 | /* We need to alloc the memsize: gzip will expand the kernel |
| 134 | * text/data, then possible rubbish we don't care about. But |
| 135 | * the kernel bss must be claimed (it will be zero'd by the |
| 136 | * kernel itself) |
Benjamin Herrenschmidt | c8e3c8b | 2005-11-07 00:57:58 -0800 | [diff] [blame] | 137 | */ |
Olaf Hering | 8a76baf | 2005-10-28 17:46:40 -0700 | [diff] [blame] | 138 | printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux.memsize); |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 139 | vmlinux.addr = (unsigned long)malloc(vmlinux.memsize); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 140 | if (vmlinux.addr == 0) { |
| 141 | printf("Can't allocate memory for kernel image !\n\r"); |
| 142 | exit(); |
| 143 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 144 | |
| 145 | /* |
David Gibson | f79e083 | 2006-11-16 15:31:32 +1100 | [diff] [blame] | 146 | * Now find the initrd |
| 147 | * |
| 148 | * First see if we have an image attached to us. If so |
| 149 | * allocate memory for it and copy it there. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | */ |
| 151 | initrd.size = (unsigned long)(_initrd_end - _initrd_start); |
| 152 | initrd.memsize = initrd.size; |
David Gibson | f79e083 | 2006-11-16 15:31:32 +1100 | [diff] [blame] | 153 | if (initrd.size > 0) { |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 154 | printf("Allocating 0x%lx bytes for initrd ...\n\r", |
| 155 | initrd.size); |
| 156 | initrd.addr = (unsigned long)malloc((u32)initrd.size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 157 | if (initrd.addr == 0) { |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 158 | printf("Can't allocate memory for initial " |
| 159 | "ramdisk !\n\r"); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 160 | exit(); |
| 161 | } |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 162 | printf("initial ramdisk moving 0x%lx <- 0x%lx " |
| 163 | "(0x%lx bytes)\n\r", initrd.addr, |
| 164 | (unsigned long)_initrd_start, initrd.size); |
| 165 | memmove((void *)initrd.addr, (void *)_initrd_start, |
| 166 | initrd.size); |
| 167 | printf("initrd head: 0x%lx\n\r", |
| 168 | *((unsigned long *)initrd.addr)); |
David Gibson | f79e083 | 2006-11-16 15:31:32 +1100 | [diff] [blame] | 169 | } else if (a2 != 0) { |
| 170 | /* Otherwise, see if yaboot or another loader gave us an initrd */ |
| 171 | initrd.addr = a1; |
| 172 | initrd.memsize = initrd.size = a2; |
| 173 | printf("Using loader supplied initrd at 0x%lx (0x%lx bytes)\n\r", |
| 174 | initrd.addr, initrd.size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 175 | } |
| 176 | |
| 177 | /* Eventually gunzip the kernel */ |
David Gibson | ad9d271 | 2007-03-05 14:24:52 +1100 | [diff] [blame^] | 178 | printf("gunzipping (0x%lx <- 0x%lx:0x%0lx)...", |
| 179 | vmlinux.addr, vmlinuz.addr, vmlinuz.addr+vmlinuz.size); |
| 180 | /* discard up to the actual load data */ |
| 181 | gunzip_discard(&gzstate, elfoffset - sizeof(elfheader)); |
| 182 | len = gunzip_finish(&gzstate, (void *)vmlinux.addr, |
| 183 | vmlinux.memsize); |
| 184 | printf("done 0x%lx bytes\n\r", len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 185 | |
| 186 | flush_cache((void *)vmlinux.addr, vmlinux.size); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 187 | } |
| 188 | |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 189 | /* A buffer that may be edited by tools operating on a zImage binary so as to |
| 190 | * edit the command line passed to vmlinux (by setting /chosen/bootargs). |
| 191 | * The buffer is put in it's own section so that tools may locate it easier. |
| 192 | */ |
| 193 | static char builtin_cmdline[COMMAND_LINE_SIZE] |
| 194 | __attribute__((__section__("__builtin_cmdline"))); |
| 195 | |
| 196 | static void get_cmdline(char *buf, int size) |
| 197 | { |
| 198 | void *devp; |
| 199 | int len = strlen(builtin_cmdline); |
| 200 | |
| 201 | buf[0] = '\0'; |
| 202 | |
| 203 | if (len > 0) { /* builtin_cmdline overrides dt's /chosen/bootargs */ |
| 204 | len = min(len, size-1); |
| 205 | strncpy(buf, builtin_cmdline, len); |
| 206 | buf[len] = '\0'; |
| 207 | } |
| 208 | else if ((devp = finddevice("/chosen"))) |
| 209 | getprop(devp, "bootargs", buf, size); |
| 210 | } |
| 211 | |
| 212 | static void set_cmdline(char *buf) |
| 213 | { |
| 214 | void *devp; |
| 215 | |
| 216 | if ((devp = finddevice("/chosen"))) |
| 217 | setprop(devp, "bootargs", buf, strlen(buf) + 1); |
| 218 | } |
| 219 | |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 220 | struct platform_ops platform_ops; |
| 221 | struct dt_ops dt_ops; |
| 222 | struct console_ops console_ops; |
| 223 | |
| 224 | void start(unsigned long a1, unsigned long a2, void *promptr, void *sp) |
| 225 | { |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 226 | kernel_entry_t kentry; |
| 227 | char cmdline[COMMAND_LINE_SIZE]; |
David Gibson | 35af89e | 2006-11-21 11:37:37 +1100 | [diff] [blame] | 228 | unsigned long ft_addr = 0; |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 229 | |
| 230 | memset(__bss_start, 0, _end - __bss_start); |
| 231 | memset(&platform_ops, 0, sizeof(platform_ops)); |
| 232 | memset(&dt_ops, 0, sizeof(dt_ops)); |
| 233 | memset(&console_ops, 0, sizeof(console_ops)); |
| 234 | |
Mark A. Greer | c888554 | 2006-10-16 13:49:27 -0700 | [diff] [blame] | 235 | if (platform_init(promptr, _dtb_start, _dtb_end)) |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 236 | exit(); |
| 237 | if (console_ops.open && (console_ops.open() < 0)) |
| 238 | exit(); |
| 239 | if (platform_ops.fixups) |
| 240 | platform_ops.fixups(); |
| 241 | |
| 242 | printf("\n\rzImage starting: loaded at 0x%p (sp: 0x%p)\n\r", |
| 243 | _start, sp); |
| 244 | |
David Gibson | f79e083 | 2006-11-16 15:31:32 +1100 | [diff] [blame] | 245 | prep_kernel(a1, a2); |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 246 | |
| 247 | /* If cmdline came from zimage wrapper or if we can edit the one |
| 248 | * in the dt, print it out and edit it, if possible. |
| 249 | */ |
| 250 | if ((strlen(builtin_cmdline) > 0) || console_ops.edit_cmdline) { |
| 251 | get_cmdline(cmdline, COMMAND_LINE_SIZE); |
| 252 | printf("\n\rLinux/PowerPC load: %s", cmdline); |
| 253 | if (console_ops.edit_cmdline) |
| 254 | console_ops.edit_cmdline(cmdline, COMMAND_LINE_SIZE); |
| 255 | printf("\n\r"); |
| 256 | set_cmdline(cmdline); |
| 257 | } |
| 258 | |
David Gibson | 35af89e | 2006-11-21 11:37:37 +1100 | [diff] [blame] | 259 | printf("Finalizing device tree..."); |
| 260 | if (dt_ops.finalize) |
| 261 | ft_addr = dt_ops.finalize(); |
| 262 | if (ft_addr) |
| 263 | printf(" flat tree at 0x%lx\n\r", ft_addr); |
| 264 | else |
| 265 | printf(" using OF tree (promptr=%p)\n\r", promptr); |
| 266 | |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 267 | if (console_ops.close) |
| 268 | console_ops.close(); |
| 269 | |
| 270 | kentry = (kernel_entry_t) vmlinux.addr; |
David Gibson | 35af89e | 2006-11-21 11:37:37 +1100 | [diff] [blame] | 271 | if (ft_addr) |
| 272 | kentry(ft_addr, 0, NULL); |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 273 | else |
| 274 | /* XXX initrd addr/size should be passed in properties */ |
David Gibson | f79e083 | 2006-11-16 15:31:32 +1100 | [diff] [blame] | 275 | kentry(initrd.addr, initrd.size, promptr); |
Mark A. Greer | b2c5f61 | 2006-09-19 14:05:08 +1000 | [diff] [blame] | 276 | |
| 277 | /* console closed so printf below may not work */ |
| 278 | printf("Error: Linux kernel returned to zImage boot wrapper!\n\r"); |
| 279 | exit(); |
| 280 | } |