blob: 404620a9e733221b959bfa990f52ab172db34bbf [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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 Heringdecd3002005-08-08 13:24:38 +100011#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. Greerb2c5f612006-09-19 14:05:08 +100017#include "ops.h"
David Gibsonad9d2712007-03-05 14:24:52 +110018#include "gunzip_util.h"
Mark A. Greerb2c5f612006-09-19 14:05:08 +100019#include "flatdevtree.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Olaf Heringdecd3002005-08-08 13:24:38 +100021extern void flush_cache(void *, unsigned long);
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
Linus Torvalds1da177e2005-04-16 15:20:36 -070023extern char _start[];
Olaf Hering9b0cbe92005-10-28 17:46:45 -070024extern char __bss_start[];
Mark Bellon3cc747e2005-09-06 15:50:02 -070025extern char _end[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070026extern char _vmlinux_start[];
27extern char _vmlinux_end[];
28extern char _initrd_start[];
29extern char _initrd_end[];
Mark A. Greerc8885542006-10-16 13:49:27 -070030extern char _dtb_start[];
31extern char _dtb_end[];
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
David Gibsonad9d2712007-03-05 14:24:52 +110033static struct gunzip_state gzstate;
34
Linus Torvalds1da177e2005-04-16 15:20:36 -070035struct addr_range {
36 unsigned long addr;
37 unsigned long size;
38 unsigned long memsize;
39};
Olaf Heringafbe8c42005-10-28 17:46:47 -070040static struct addr_range vmlinux;
41static struct addr_range vmlinuz;
42static struct addr_range initrd;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
Paul Mackerras94b212c2005-11-16 13:38:21 +110044static unsigned long elfoffset;
Mark A. Greerb2c5f612006-09-19 14:05:08 +100045static int is_64bit;
Paul Mackerras94b212c2005-11-16 13:38:21 +110046
Olaf Hering8a76baf2005-10-28 17:46:40 -070047static char elfheader[256];
Olaf Hering70540362005-10-28 17:46:38 -070048
Mark A. Greerb2c5f612006-09-19 14:05:08 +100049typedef void (*kernel_entry_t)(unsigned long, unsigned long, void *);
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#undef DEBUG
52
Paul Mackerras94b212c2005-11-16 13:38:21 +110053static 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 Hering158daa42006-01-30 14:28:03 +010072 if (elf64ph->p_type == PT_LOAD)
Paul Mackerras94b212c2005-11-16 13:38:21 +110073 break;
74 if (i >= (unsigned int)elf64->e_phnum)
75 return 0;
76
77 elfoffset = (unsigned long)elf64ph->p_offset;
David Gibsonad9d2712007-03-05 14:24:52 +110078 vmlinux.size = (unsigned long)elf64ph->p_filesz;
79 vmlinux.memsize = (unsigned long)elf64ph->p_memsz;
Paul Mackerras66a45dd2006-01-14 15:04:06 +110080
Mark A. Greerb2c5f612006-09-19 14:05:08 +100081 is_64bit = 1;
Paul Mackerras94b212c2005-11-16 13:38:21 +110082 return 1;
83}
84
85static 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 Hering158daa42006-01-30 14:28:03 +0100104 if (elf32ph->p_type == PT_LOAD)
Paul Mackerras94b212c2005-11-16 13:38:21 +1100105 break;
106 if (i >= elf32->e_phnum)
107 return 0;
108
109 elfoffset = elf32ph->p_offset;
David Gibsonad9d2712007-03-05 14:24:52 +1100110 vmlinux.size = elf32ph->p_filesz;
111 vmlinux.memsize = elf32ph->p_memsz;
Paul Mackerras94b212c2005-11-16 13:38:21 +1100112 return 1;
113}
114
David Gibsonf79e0832006-11-16 15:31:32 +1100115static void prep_kernel(unsigned long a1, unsigned long a2)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Olaf Hering8a76baf2005-10-28 17:46:40 -0700117 int len;
Paul Mackerras66a45dd2006-01-14 15:04:06 +1100118
Paul Mackerras94b212c2005-11-16 13:38:21 +1100119 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 Gibsonad9d2712007-03-05 14:24:52 +1100123 gunzip_start(&gzstate, (void *)vmlinuz.addr, vmlinuz.size);
124 gunzip_exactly(&gzstate, elfheader, sizeof(elfheader));
Paul Mackerras94b212c2005-11-16 13:38:21 +1100125
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. Greerb2c5f612006-09-19 14:05:08 +1000130 if (platform_ops.image_hdr)
131 platform_ops.image_hdr(elfheader);
Paul Mackerras94b212c2005-11-16 13:38:21 +1100132
David Gibsonad9d2712007-03-05 14:24:52 +1100133 /* 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 Herrenschmidtc8e3c8b2005-11-07 00:57:58 -0800137 */
Olaf Hering8a76baf2005-10-28 17:46:40 -0700138 printf("Allocating 0x%lx bytes for kernel ...\n\r", vmlinux.memsize);
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000139 vmlinux.addr = (unsigned long)malloc(vmlinux.memsize);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 if (vmlinux.addr == 0) {
141 printf("Can't allocate memory for kernel image !\n\r");
142 exit();
143 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144
145 /*
David Gibsonf79e0832006-11-16 15:31:32 +1100146 * 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 Torvalds1da177e2005-04-16 15:20:36 -0700150 */
151 initrd.size = (unsigned long)(_initrd_end - _initrd_start);
152 initrd.memsize = initrd.size;
David Gibsonf79e0832006-11-16 15:31:32 +1100153 if (initrd.size > 0) {
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000154 printf("Allocating 0x%lx bytes for initrd ...\n\r",
155 initrd.size);
156 initrd.addr = (unsigned long)malloc((u32)initrd.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157 if (initrd.addr == 0) {
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000158 printf("Can't allocate memory for initial "
159 "ramdisk !\n\r");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 exit();
161 }
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000162 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 Gibsonf79e0832006-11-16 15:31:32 +1100169 } 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 Torvalds1da177e2005-04-16 15:20:36 -0700175 }
176
177 /* Eventually gunzip the kernel */
David Gibsonad9d2712007-03-05 14:24:52 +1100178 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 Torvalds1da177e2005-04-16 15:20:36 -0700185
186 flush_cache((void *)vmlinux.addr, vmlinux.size);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000189/* 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 */
193static char builtin_cmdline[COMMAND_LINE_SIZE]
194 __attribute__((__section__("__builtin_cmdline")));
195
196static 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
212static 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. Greerb2c5f612006-09-19 14:05:08 +1000220struct platform_ops platform_ops;
221struct dt_ops dt_ops;
222struct console_ops console_ops;
223
224void start(unsigned long a1, unsigned long a2, void *promptr, void *sp)
225{
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000226 kernel_entry_t kentry;
227 char cmdline[COMMAND_LINE_SIZE];
David Gibson35af89e2006-11-21 11:37:37 +1100228 unsigned long ft_addr = 0;
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000229
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. Greerc8885542006-10-16 13:49:27 -0700235 if (platform_init(promptr, _dtb_start, _dtb_end))
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000236 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 Gibsonf79e0832006-11-16 15:31:32 +1100245 prep_kernel(a1, a2);
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000246
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 Gibson35af89e2006-11-21 11:37:37 +1100259 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. Greerb2c5f612006-09-19 14:05:08 +1000267 if (console_ops.close)
268 console_ops.close();
269
270 kentry = (kernel_entry_t) vmlinux.addr;
David Gibson35af89e2006-11-21 11:37:37 +1100271 if (ft_addr)
272 kentry(ft_addr, 0, NULL);
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000273 else
274 /* XXX initrd addr/size should be passed in properties */
David Gibsonf79e0832006-11-16 15:31:32 +1100275 kentry(initrd.addr, initrd.size, promptr);
Mark A. Greerb2c5f612006-09-19 14:05:08 +1000276
277 /* console closed so printf below may not work */
278 printf("Error: Linux kernel returned to zImage boot wrapper!\n\r");
279 exit();
280}