blob: f8be72c77c98e31abb50682d0d9c6dad358b6125 [file] [log] [blame]
ebiedermc7798892009-04-01 11:03:32 +00001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2003 Eric W. Biederman <ebiederm@xmission.com>
5 * Copyright (C) 2009 Ron Minnich <rminnich@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
19 */
20
Ronald G. Minnichae631262009-04-01 10:48:39 +000021#include <console/console.h>
Stefan Reinauerde3206a2010-02-22 06:09:43 +000022#include <fallback.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000023#include <boot/elf.h>
24#include <boot/elf_boot.h>
25#include <boot/coreboot_tables.h>
26#include <ip_checksum.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000027#include <stdint.h>
28#include <stdlib.h>
29#include <string.h>
Peter Stuge483b7bb2009-04-14 07:40:01 +000030#include <cbfs.h>
Myles Watson58170782009-10-28 16:13:28 +000031#include <lib.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000032
33#ifndef CONFIG_BIG_ENDIAN
34#define ntohl(x) ( ((x&0xff)<<24) | ((x&0xff00)<<8) | \
35 ((x&0xff0000) >> 8) | ((x&0xff000000) >> 24) )
36#else
37#define ntohl(x) (x)
38#endif
39
40/* Maximum physical address we can use for the coreboot bounce buffer.
41 */
42#ifndef MAX_ADDR
43#define MAX_ADDR -1UL
44#endif
45
46extern unsigned char _ram_seg;
47extern unsigned char _eram_seg;
48
49struct segment {
50 struct segment *next;
51 struct segment *prev;
52 struct segment *phdr_next;
53 struct segment *phdr_prev;
54 unsigned long s_dstaddr;
55 unsigned long s_srcaddr;
56 unsigned long s_memsz;
57 unsigned long s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +000058 int compression;
Ronald G. Minnichae631262009-04-01 10:48:39 +000059};
60
61struct verify_callback {
62 struct verify_callback *next;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000063 int (*callback)(struct verify_callback *vcb,
Ronald G. Minnichae631262009-04-01 10:48:39 +000064 Elf_ehdr *ehdr, Elf_phdr *phdr, struct segment *head);
65 unsigned long desc_offset;
66 unsigned long desc_addr;
67};
68
69struct ip_checksum_vcb {
70 struct verify_callback data;
71 unsigned short ip_checksum;
72};
73
Myles Watson92027982009-09-23 20:32:21 +000074static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload);
75
Patrick Georgid1185bf2009-05-26 14:00:49 +000076void * cbfs_load_payload(struct lb_memory *lb_mem, const char *name)
77{
Stefan Reinauerf64893a2009-07-23 22:03:14 +000078 struct cbfs_payload *payload;
Patrick Georgid1185bf2009-05-26 14:00:49 +000079
Stefan Reinauerf64893a2009-07-23 22:03:14 +000080 payload = (struct cbfs_payload *)cbfs_find_file(name, CBFS_TYPE_PAYLOAD);
Patrick Georgid1185bf2009-05-26 14:00:49 +000081 if (payload == NULL)
82 return (void *) -1;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000083 printk(BIOS_DEBUG, "Got a payload\n");
Stefan Reinauerf64893a2009-07-23 22:03:14 +000084
Patrick Georgid1185bf2009-05-26 14:00:49 +000085 selfboot(lb_mem, payload);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000086 printk(BIOS_EMERG, "SELFBOOT RETURNED!\n");
Patrick Georgid1185bf2009-05-26 14:00:49 +000087
88 return (void *) -1;
89}
90
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000091/* The problem:
Ronald G. Minnichae631262009-04-01 10:48:39 +000092 * Static executables all want to share the same addresses
93 * in memory because only a few addresses are reliably present on
94 * a machine, and implementing general relocation is hard.
95 *
96 * The solution:
Stefan Reinauerf64893a2009-07-23 22:03:14 +000097 * - Allocate a buffer the size of the coreboot image plus additional
98 * required space.
99 * - Anything that would overwrite coreboot copy into the lower part of
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000100 * the buffer.
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000101 * - After loading an ELF image copy coreboot to the top of the buffer.
Ronald G. Minnichae631262009-04-01 10:48:39 +0000102 * - Then jump to the loaded image.
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000103 *
Ronald G. Minnichae631262009-04-01 10:48:39 +0000104 * Benefits:
105 * - Nearly arbitrary standalone executables can be loaded.
106 * - Coreboot is preserved, so it can be returned to.
107 * - The implementation is still relatively simple,
Zheng Baoba49fb72009-11-01 09:18:23 +0000108 * and much simpler than the general case implemented in kexec.
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000109 *
Ronald G. Minnichae631262009-04-01 10:48:39 +0000110 */
111
Patrick Georgi5eceb322009-05-13 16:27:25 +0000112static unsigned long bounce_size, bounce_buffer;
113
Myles Watson92027982009-09-23 20:32:21 +0000114static void get_bounce_buffer(struct lb_memory *mem, unsigned long req_size)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000115{
116 unsigned long lb_size;
117 unsigned long mem_entries;
118 unsigned long buffer;
119 int i;
120 lb_size = (unsigned long)(&_eram_seg - &_ram_seg);
121 /* Double coreboot size so I have somewhere to place a copy to return to */
Myles Watson92027982009-09-23 20:32:21 +0000122 lb_size = req_size + lb_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000123 mem_entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
124 buffer = 0;
125 for(i = 0; i < mem_entries; i++) {
126 unsigned long mstart, mend;
127 unsigned long msize;
128 unsigned long tbuffer;
129 if (mem->map[i].type != LB_MEM_RAM)
130 continue;
131 if (unpack_lb64(mem->map[i].start) > MAX_ADDR)
132 continue;
133 if (unpack_lb64(mem->map[i].size) < lb_size)
134 continue;
135 mstart = unpack_lb64(mem->map[i].start);
136 msize = MAX_ADDR - mstart +1;
137 if (msize > unpack_lb64(mem->map[i].size))
138 msize = unpack_lb64(mem->map[i].size);
139 mend = mstart + msize;
140 tbuffer = mend - lb_size;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000141 if (tbuffer < buffer)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000142 continue;
143 buffer = tbuffer;
144 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000145 bounce_buffer = buffer;
Myles Watson92027982009-09-23 20:32:21 +0000146 bounce_size = req_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000147}
148
149static int valid_area(struct lb_memory *mem, unsigned long buffer,
150 unsigned long start, unsigned long len)
151{
152 /* Check through all of the memory segments and ensure
153 * the segment that was passed in is completely contained
154 * in RAM.
155 */
156 int i;
157 unsigned long end = start + len;
158 unsigned long mem_entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
159
160 /* See if I conflict with the bounce buffer */
161 if (end >= buffer) {
162 return 0;
163 }
164
165 /* Walk through the table of valid memory ranges and see if I
166 * have a match.
167 */
168 for(i = 0; i < mem_entries; i++) {
169 uint64_t mstart, mend;
170 uint32_t mtype;
171 mtype = mem->map[i].type;
172 mstart = unpack_lb64(mem->map[i].start);
173 mend = mstart + unpack_lb64(mem->map[i].size);
174 if ((mtype == LB_MEM_RAM) && (start < mend) && (end > mstart)) {
175 break;
176 }
177 if ((mtype == LB_MEM_TABLE) && (start < mend) && (end > mstart)) {
Stefan Reinauerd6b4f1c2010-09-23 18:29:40 +0000178 printk(BIOS_ERR, "Payload is overwriting coreboot tables.\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000179 break;
180 }
181 }
182 if (i == mem_entries) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000183 printk(BIOS_ERR, "No matching ram area found for range:\n");
184 printk(BIOS_ERR, " [0x%016lx, 0x%016lx)\n", start, end);
185 printk(BIOS_ERR, "Ram areas\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000186 for(i = 0; i < mem_entries; i++) {
187 uint64_t mstart, mend;
188 uint32_t mtype;
189 mtype = mem->map[i].type;
190 mstart = unpack_lb64(mem->map[i].start);
191 mend = mstart + unpack_lb64(mem->map[i].size);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000192 printk(BIOS_ERR, " [0x%016lx, 0x%016lx) %s\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000193 (unsigned long)mstart,
194 (unsigned long)mend,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000195 (mtype == LB_MEM_RAM)?"RAM":"Reserved");
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000196
Ronald G. Minnichae631262009-04-01 10:48:39 +0000197 }
198 return 0;
199 }
200 return 1;
201}
202
Patrick Georgi5eceb322009-05-13 16:27:25 +0000203static const unsigned long lb_start = (unsigned long)&_ram_seg;
204static const unsigned long lb_end = (unsigned long)&_eram_seg;
205
206static int overlaps_coreboot(struct segment *seg)
207{
208 unsigned long start, end;
209 start = seg->s_dstaddr;
210 end = start + seg->s_memsz;
211 return !((end <= lb_start) || (start >= lb_end));
212}
213
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000214static int relocate_segment(unsigned long buffer, struct segment *seg)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000215{
216 /* Modify all segments that want to load onto coreboot
217 * to load onto the bounce buffer instead.
218 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000219 /* ret: 1 : A new segment is inserted before the seg.
220 * 0 : A new segment is inserted after the seg, or no new one. */
221 unsigned long start, middle, end, ret = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000222
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000223 printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000224 lb_start, lb_end);
225
Patrick Georgi5eceb322009-05-13 16:27:25 +0000226 /* I don't conflict with coreboot so get out of here */
227 if (!overlaps_coreboot(seg))
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000228 return 0;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000229
Ronald G. Minnichae631262009-04-01 10:48:39 +0000230 start = seg->s_dstaddr;
231 middle = start + seg->s_filesz;
232 end = start + seg->s_memsz;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000233
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000234 printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000235 start, middle, end);
236
Patrick Georgi369bc782009-04-25 07:32:24 +0000237 if (seg->compression == CBFS_COMPRESS_NONE) {
238 /* Slice off a piece at the beginning
239 * that doesn't conflict with coreboot.
240 */
241 if (start < lb_start) {
242 struct segment *new;
243 unsigned long len = lb_start - start;
244 new = malloc(sizeof(*new));
245 *new = *seg;
246 new->s_memsz = len;
247 seg->s_memsz -= len;
248 seg->s_dstaddr += len;
249 seg->s_srcaddr += len;
250 if (seg->s_filesz > len) {
251 new->s_filesz = len;
252 seg->s_filesz -= len;
253 } else {
254 seg->s_filesz = 0;
255 }
256
257 /* Order by stream offset */
258 new->next = seg;
259 new->prev = seg->prev;
260 seg->prev->next = new;
261 seg->prev = new;
262 /* Order by original program header order */
263 new->phdr_next = seg;
264 new->phdr_prev = seg->phdr_prev;
265 seg->phdr_prev->phdr_next = new;
266 seg->phdr_prev = new;
267
268 /* compute the new value of start */
269 start = seg->s_dstaddr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000270
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000271 printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000272 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000273 new->s_dstaddr + new->s_filesz,
274 new->s_dstaddr + new->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000275
276 ret = 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000277 }
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000278
279 /* Slice off a piece at the end
280 * that doesn't conflict with coreboot
Patrick Georgi369bc782009-04-25 07:32:24 +0000281 */
282 if (end > lb_end) {
283 unsigned long len = lb_end - start;
284 struct segment *new;
285 new = malloc(sizeof(*new));
286 *new = *seg;
287 seg->s_memsz = len;
288 new->s_memsz -= len;
289 new->s_dstaddr += len;
290 new->s_srcaddr += len;
291 if (seg->s_filesz > len) {
292 seg->s_filesz = len;
293 new->s_filesz -= len;
294 } else {
295 new->s_filesz = 0;
296 }
297 /* Order by stream offset */
298 new->next = seg->next;
299 new->prev = seg;
300 seg->next->prev = new;
301 seg->next = new;
302 /* Order by original program header order */
303 new->phdr_next = seg->phdr_next;
304 new->phdr_prev = seg;
305 seg->phdr_next->phdr_prev = new;
306 seg->phdr_next = new;
307
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000308 printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000309 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000310 new->s_dstaddr + new->s_filesz,
311 new->s_dstaddr + new->s_memsz);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000312 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000313 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000314
Ronald G. Minnichae631262009-04-01 10:48:39 +0000315 /* Now retarget this segment onto the bounce buffer */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000316 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
Ronald G. Minnichae631262009-04-01 10:48:39 +0000317 * so you will make the dstaddr be this buffer, and it will get copied
318 * later to where coreboot lives.
319 */
320 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
321
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000322 printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000323 seg->s_dstaddr,
324 seg->s_dstaddr + seg->s_filesz,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000325 seg->s_dstaddr + seg->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000326
327 return ret;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000328}
329
330
331static int build_self_segment_list(
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000332 struct segment *head,
Patrick Georgi5eceb322009-05-13 16:27:25 +0000333 struct lb_memory *mem,
Peter Stuge483b7bb2009-04-14 07:40:01 +0000334 struct cbfs_payload *payload, u32 *entry)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000335{
336 struct segment *new;
337 struct segment *ptr;
Peter Stuge483b7bb2009-04-14 07:40:01 +0000338 struct cbfs_payload_segment *segment, *first_segment;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000339 memset(head, 0, sizeof(*head));
340 head->phdr_next = head->phdr_prev = head;
341 head->next = head->prev = head;
342 first_segment = segment = &payload->segments;
343
344 while(1) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000345 printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000346 switch(segment->type) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000347 case PAYLOAD_SEGMENT_PARAMS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000348 printk(BIOS_DEBUG, " parameter section (skipped)\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000349 segment++;
350 continue;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000351
Ronald G. Minnichae631262009-04-01 10:48:39 +0000352 case PAYLOAD_SEGMENT_CODE:
353 case PAYLOAD_SEGMENT_DATA:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000354 printk(BIOS_DEBUG, " %s (compression=%x)\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000355 segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
356 ntohl(segment->compression));
357 new = malloc(sizeof(*new));
358 new->s_dstaddr = ntohl((u32) segment->load_addr);
359 new->s_memsz = ntohl(segment->mem_len);
360 new->compression = ntohl(segment->compression);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000361
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000362 new->s_srcaddr = (u32) ((unsigned char *) first_segment) + ntohl(segment->offset);
363 new->s_filesz = ntohl(segment->len);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000364 printk(BIOS_DEBUG, " New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000365 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
366 /* Clean up the values */
367 if (new->s_filesz > new->s_memsz) {
368 new->s_filesz = new->s_memsz;
369 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000370 printk(BIOS_DEBUG, " (cleaned up) New segment addr 0x%lx size 0x%lx offset 0x%lx filesize 0x%lx\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000371 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
372 break;
373
Ronald G. Minnichae631262009-04-01 10:48:39 +0000374 case PAYLOAD_SEGMENT_BSS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000375 printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *) ntohl((u32) segment->load_addr),
Ronald G. Minnichae631262009-04-01 10:48:39 +0000376 ntohl(segment->mem_len));
377 new = malloc(sizeof(*new));
378 new->s_filesz = 0;
379 new->s_dstaddr = ntohl((u32) segment->load_addr);
380 new->s_memsz = ntohl(segment->mem_len);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000381 break;
382
383 case PAYLOAD_SEGMENT_ENTRY:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000384 printk(BIOS_DEBUG, " Entry Point 0x%p\n", (void *) ntohl((u32) segment->load_addr));
Myles Watsonfa12b672009-04-30 22:45:41 +0000385 *entry = ntohl((u32) segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000386 /* Per definition, a payload always has the entry point
387 * as last segment. Thus, we use the occurence of the
388 * entry point as break condition for the loop.
389 * Can we actually just look at the number of section?
390 */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000391 return 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000392
393 default:
394 /* We found something that we don't know about. Throw
395 * hands into the sky and run away!
396 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000397 printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000398 return -1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000399 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000400
Ronald G. Minnichae631262009-04-01 10:48:39 +0000401 segment++;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000402
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000403 // FIXME: Explain what this is
Ronald G. Minnichae631262009-04-01 10:48:39 +0000404 for(ptr = head->next; ptr != head; ptr = ptr->next) {
405 if (new->s_srcaddr < ntohl((u32) segment->load_addr))
406 break;
407 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000408
Ronald G. Minnichae631262009-04-01 10:48:39 +0000409 /* Order by stream offset */
410 new->next = ptr;
411 new->prev = ptr->prev;
412 ptr->prev->next = new;
413 ptr->prev = new;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000414
Ronald G. Minnichae631262009-04-01 10:48:39 +0000415 /* Order by original program header order */
416 new->phdr_next = head;
417 new->phdr_prev = head->phdr_prev;
418 head->phdr_prev->phdr_next = new;
419 head->phdr_prev = new;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000420 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000421
Ronald G. Minnichae631262009-04-01 10:48:39 +0000422 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000423}
424
425static int load_self_segments(
Patrick Georgi5eceb322009-05-13 16:27:25 +0000426 struct segment *head,
427 struct lb_memory *mem,
428 struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000429{
Ronald G. Minnichae631262009-04-01 10:48:39 +0000430 struct segment *ptr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000431
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000432 unsigned long bounce_high = lb_end;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000433 for(ptr = head->next; ptr != head; ptr = ptr->next) {
434 if (!overlaps_coreboot(ptr)) continue;
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000435 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
436 bounce_high = ptr->s_dstaddr + ptr->s_memsz;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000437 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000438 get_bounce_buffer(mem, bounce_high - lb_start);
Patrick Georgi5eceb322009-05-13 16:27:25 +0000439 if (!bounce_buffer) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000440 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
Patrick Georgi5eceb322009-05-13 16:27:25 +0000441 return 0;
442 }
443 for(ptr = head->next; ptr != head; ptr = ptr->next) {
444 /* Verify the memory addresses in the segment are valid */
445 if (!valid_area(mem, bounce_buffer, ptr->s_dstaddr, ptr->s_memsz))
446 return 0;
447 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000448 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000449 unsigned char *dest, *src;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000450 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000451 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000452
Patrick Georgi5eceb322009-05-13 16:27:25 +0000453 /* Modify the segment to load onto the bounce_buffer if necessary.
454 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000455 if (relocate_segment(bounce_buffer, ptr)) {
456 ptr = (ptr->prev)->prev;
457 continue;
458 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000459
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000460 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Patrick Georgi5eceb322009-05-13 16:27:25 +0000461 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
462
Ronald G. Minnichae631262009-04-01 10:48:39 +0000463 /* Compute the boundaries of the segment */
464 dest = (unsigned char *)(ptr->s_dstaddr);
Myles Watsonfa12b672009-04-30 22:45:41 +0000465 src = (unsigned char *)(ptr->s_srcaddr);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000466
Ronald G. Minnichae631262009-04-01 10:48:39 +0000467 /* Copy data from the initial buffer */
468 if (ptr->s_filesz) {
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000469 unsigned char *middle, *end;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000470 size_t len;
471 len = ptr->s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +0000472 switch(ptr->compression) {
Patrick Georgi369bc782009-04-25 07:32:24 +0000473 case CBFS_COMPRESS_LZMA: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000474 printk(BIOS_DEBUG, "using LZMA\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000475 len = ulzma(src, dest);
Myles Watson94de72b2010-06-01 15:19:25 +0000476 if (!len) /* Decompression Error. */
477 return 0;
Patrick Georgi369bc782009-04-25 07:32:24 +0000478 break;
479 }
Patrick Georgi369bc782009-04-25 07:32:24 +0000480#if CONFIG_COMPRESSED_PAYLOAD_NRV2B==1
481 case CBFS_COMPRESS_NRV2B: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000482 printk(BIOS_DEBUG, "using NRV2B\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000483 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
484 unsigned long tmp;
485 len = unrv2b(src, dest, &tmp);
486 break;
487 }
488#endif
489 case CBFS_COMPRESS_NONE: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000490 printk(BIOS_DEBUG, "it's not compressed!\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000491 memcpy(dest, src, len);
492 break;
493 }
494 default:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000495 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression);
Patrick Georgi369bc782009-04-25 07:32:24 +0000496 return -1;
497 }
498 end = dest + ptr->s_memsz;
499 middle = dest + len;
Myles Watson94de72b2010-06-01 15:19:25 +0000500 printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
Patrick Georgi369bc782009-04-25 07:32:24 +0000501 (unsigned long)dest,
502 (unsigned long)middle,
503 (unsigned long)end,
504 (unsigned long)src);
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000505
506 /* Zero the extra bytes between middle & end */
507 if (middle < end) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000508 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000509 (unsigned long)middle, (unsigned long)(end - middle));
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000510
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000511 /* Zero the extra bytes */
512 memset(middle, 0, end - middle);
513 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000514 /* Copy the data that's outside the area that shadows coreboot_ram */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000515 printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000516 if ((unsigned long)end > bounce_buffer) {
517 if ((unsigned long)dest < bounce_buffer) {
Myles Watson7943fe62009-10-30 02:08:07 +0000518 unsigned char *from = dest;
519 unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000520 unsigned long amount = bounce_buffer-(unsigned long)dest;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000521 printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000522 memcpy(to, from, amount);
523 }
524 if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
525 unsigned long from = bounce_buffer + (lb_end - lb_start);
526 unsigned long to = lb_end;
Myles Watson7943fe62009-10-30 02:08:07 +0000527 unsigned long amount = (unsigned long)end - from;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000528 printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
Myles Watson7943fe62009-10-30 02:08:07 +0000529 memcpy((char*)to, (char*)from, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000530 }
531 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000532 }
533 }
534 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000535}
536
Myles Watson92027982009-09-23 20:32:21 +0000537static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000538{
Myles Watsonfa12b672009-04-30 22:45:41 +0000539 u32 entry=0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000540 struct segment head;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000541
542 /* Preprocess the self segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000543 if (!build_self_segment_list(&head, mem, payload, &entry))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000544 goto out;
545
546 /* Load the segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000547 if (!load_self_segments(&head, mem, payload))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000548 goto out;
549
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000550 printk(BIOS_SPEW, "Loaded segments\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000551
552 /* Reset to booting from this image as late as possible */
553 boot_successful();
554
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000555 printk(BIOS_DEBUG, "Jumping to boot code at %x\n", entry);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000556 post_code(POST_ENTER_ELF_BOOT);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000557
558 /* Jump to kernel */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000559 jmp_to_elf_entry((void*)entry, bounce_buffer, bounce_size);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000560 return 1;
561
562 out:
563 return 0;
564}
565