blob: fe566539232b5518a41bc314e6400d56736aa1a5 [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
Stefan Reinauer13774912011-10-14 15:19:21 -070021#include <arch/byteorder.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000022#include <console/console.h>
Stefan Reinauerde3206a2010-02-22 06:09:43 +000023#include <fallback.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000024#include <boot/elf.h>
25#include <boot/elf_boot.h>
26#include <boot/coreboot_tables.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
Stefan Reinauer19436292011-11-07 12:43:03 -080033/* Maximum physical address we can use for the coreboot bounce buffer. */
Ronald G. Minnichae631262009-04-01 10:48:39 +000034#ifndef MAX_ADDR
35#define MAX_ADDR -1UL
36#endif
37
Stefan Reinauer19436292011-11-07 12:43:03 -080038/* from coreboot_ram.ld: */
Ronald G. Minnichae631262009-04-01 10:48:39 +000039extern unsigned char _ram_seg;
40extern unsigned char _eram_seg;
41
Stefan Reinauer19436292011-11-07 12:43:03 -080042static const unsigned long lb_start = (unsigned long)&_ram_seg;
43static const unsigned long lb_end = (unsigned long)&_eram_seg;
44
Ronald G. Minnichae631262009-04-01 10:48:39 +000045struct segment {
46 struct segment *next;
47 struct segment *prev;
48 struct segment *phdr_next;
49 struct segment *phdr_prev;
50 unsigned long s_dstaddr;
51 unsigned long s_srcaddr;
52 unsigned long s_memsz;
53 unsigned long s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +000054 int compression;
Ronald G. Minnichae631262009-04-01 10:48:39 +000055};
56
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000057/* The problem:
Ronald G. Minnichae631262009-04-01 10:48:39 +000058 * Static executables all want to share the same addresses
59 * in memory because only a few addresses are reliably present on
60 * a machine, and implementing general relocation is hard.
61 *
62 * The solution:
Stefan Reinauerf64893a2009-07-23 22:03:14 +000063 * - Allocate a buffer the size of the coreboot image plus additional
64 * required space.
65 * - Anything that would overwrite coreboot copy into the lower part of
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000066 * the buffer.
Stefan Reinauerf64893a2009-07-23 22:03:14 +000067 * - After loading an ELF image copy coreboot to the top of the buffer.
Ronald G. Minnichae631262009-04-01 10:48:39 +000068 * - Then jump to the loaded image.
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000069 *
Ronald G. Minnichae631262009-04-01 10:48:39 +000070 * Benefits:
71 * - Nearly arbitrary standalone executables can be loaded.
72 * - Coreboot is preserved, so it can be returned to.
73 * - The implementation is still relatively simple,
Zheng Baoba49fb72009-11-01 09:18:23 +000074 * and much simpler than the general case implemented in kexec.
Ronald G. Minnichae631262009-04-01 10:48:39 +000075 */
76
Patrick Georgi5eceb322009-05-13 16:27:25 +000077static unsigned long bounce_size, bounce_buffer;
78
Myles Watson92027982009-09-23 20:32:21 +000079static void get_bounce_buffer(struct lb_memory *mem, unsigned long req_size)
Ronald G. Minnichae631262009-04-01 10:48:39 +000080{
81 unsigned long lb_size;
82 unsigned long mem_entries;
83 unsigned long buffer;
84 int i;
Stefan Reinauer19436292011-11-07 12:43:03 -080085 lb_size = lb_end - lb_start;
86 /* Plus coreboot size so I have somewhere
87 * to place a copy to return to.
88 */
Myles Watson92027982009-09-23 20:32:21 +000089 lb_size = req_size + lb_size;
Stefan Reinauer19436292011-11-07 12:43:03 -080090 mem_entries = (mem->size - sizeof(*mem)) / sizeof(mem->map[0]);
Ronald G. Minnichae631262009-04-01 10:48:39 +000091 buffer = 0;
92 for(i = 0; i < mem_entries; i++) {
93 unsigned long mstart, mend;
94 unsigned long msize;
95 unsigned long tbuffer;
96 if (mem->map[i].type != LB_MEM_RAM)
97 continue;
98 if (unpack_lb64(mem->map[i].start) > MAX_ADDR)
99 continue;
100 if (unpack_lb64(mem->map[i].size) < lb_size)
101 continue;
102 mstart = unpack_lb64(mem->map[i].start);
103 msize = MAX_ADDR - mstart +1;
104 if (msize > unpack_lb64(mem->map[i].size))
105 msize = unpack_lb64(mem->map[i].size);
106 mend = mstart + msize;
107 tbuffer = mend - lb_size;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000108 if (tbuffer < buffer)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000109 continue;
110 buffer = tbuffer;
111 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000112 bounce_buffer = buffer;
Myles Watson92027982009-09-23 20:32:21 +0000113 bounce_size = req_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000114}
115
116static int valid_area(struct lb_memory *mem, unsigned long buffer,
117 unsigned long start, unsigned long len)
118{
119 /* Check through all of the memory segments and ensure
120 * the segment that was passed in is completely contained
121 * in RAM.
122 */
123 int i;
124 unsigned long end = start + len;
Stefan Reinauer19436292011-11-07 12:43:03 -0800125 unsigned long mem_entries = (mem->size - sizeof(*mem)) /
126 sizeof(mem->map[0]);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000127
128 /* See if I conflict with the bounce buffer */
129 if (end >= buffer) {
130 return 0;
131 }
132
133 /* Walk through the table of valid memory ranges and see if I
134 * have a match.
135 */
136 for(i = 0; i < mem_entries; i++) {
137 uint64_t mstart, mend;
138 uint32_t mtype;
139 mtype = mem->map[i].type;
140 mstart = unpack_lb64(mem->map[i].start);
141 mend = mstart + unpack_lb64(mem->map[i].size);
Stefan Reinauerc6b8b7d2011-11-07 12:56:12 -0800142 if ((mtype == LB_MEM_RAM) && (start >= mstart) && (end < mend)) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000143 break;
144 }
Stefan Reinauerc6b8b7d2011-11-07 12:56:12 -0800145 if ((mtype == LB_MEM_TABLE) && (start >= mstart) && (end < mend)) {
Stefan Reinauerd6b4f1c2010-09-23 18:29:40 +0000146 printk(BIOS_ERR, "Payload is overwriting coreboot tables.\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000147 break;
148 }
149 }
150 if (i == mem_entries) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000151 printk(BIOS_ERR, "No matching ram area found for range:\n");
152 printk(BIOS_ERR, " [0x%016lx, 0x%016lx)\n", start, end);
153 printk(BIOS_ERR, "Ram areas\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000154 for(i = 0; i < mem_entries; i++) {
155 uint64_t mstart, mend;
156 uint32_t mtype;
157 mtype = mem->map[i].type;
158 mstart = unpack_lb64(mem->map[i].start);
159 mend = mstart + unpack_lb64(mem->map[i].size);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000160 printk(BIOS_ERR, " [0x%016lx, 0x%016lx) %s\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000161 (unsigned long)mstart,
162 (unsigned long)mend,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000163 (mtype == LB_MEM_RAM)?"RAM":"Reserved");
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000164
Ronald G. Minnichae631262009-04-01 10:48:39 +0000165 }
166 return 0;
167 }
168 return 1;
169}
170
Patrick Georgi5eceb322009-05-13 16:27:25 +0000171
172static int overlaps_coreboot(struct segment *seg)
173{
174 unsigned long start, end;
175 start = seg->s_dstaddr;
176 end = start + seg->s_memsz;
177 return !((end <= lb_start) || (start >= lb_end));
178}
179
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000180static int relocate_segment(unsigned long buffer, struct segment *seg)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000181{
182 /* Modify all segments that want to load onto coreboot
183 * to load onto the bounce buffer instead.
184 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000185 /* ret: 1 : A new segment is inserted before the seg.
Stefan Reinauer19436292011-11-07 12:43:03 -0800186 * 0 : A new segment is inserted after the seg, or no new one.
187 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000188 unsigned long start, middle, end, ret = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000189
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000190 printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000191 lb_start, lb_end);
192
Patrick Georgi5eceb322009-05-13 16:27:25 +0000193 /* I don't conflict with coreboot so get out of here */
194 if (!overlaps_coreboot(seg))
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000195 return 0;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000196
Ronald G. Minnichae631262009-04-01 10:48:39 +0000197 start = seg->s_dstaddr;
198 middle = start + seg->s_filesz;
199 end = start + seg->s_memsz;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000200
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000201 printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000202 start, middle, end);
203
Patrick Georgi369bc782009-04-25 07:32:24 +0000204 if (seg->compression == CBFS_COMPRESS_NONE) {
205 /* Slice off a piece at the beginning
206 * that doesn't conflict with coreboot.
207 */
208 if (start < lb_start) {
209 struct segment *new;
210 unsigned long len = lb_start - start;
211 new = malloc(sizeof(*new));
212 *new = *seg;
213 new->s_memsz = len;
214 seg->s_memsz -= len;
215 seg->s_dstaddr += len;
216 seg->s_srcaddr += len;
217 if (seg->s_filesz > len) {
218 new->s_filesz = len;
219 seg->s_filesz -= len;
220 } else {
221 seg->s_filesz = 0;
222 }
223
224 /* Order by stream offset */
225 new->next = seg;
226 new->prev = seg->prev;
227 seg->prev->next = new;
228 seg->prev = new;
229 /* Order by original program header order */
230 new->phdr_next = seg;
231 new->phdr_prev = seg->phdr_prev;
232 seg->phdr_prev->phdr_next = new;
233 seg->phdr_prev = new;
234
235 /* compute the new value of start */
236 start = seg->s_dstaddr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000237
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000238 printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000239 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000240 new->s_dstaddr + new->s_filesz,
241 new->s_dstaddr + new->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000242
243 ret = 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000244 }
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000245
246 /* Slice off a piece at the end
247 * that doesn't conflict with coreboot
Patrick Georgi369bc782009-04-25 07:32:24 +0000248 */
249 if (end > lb_end) {
250 unsigned long len = lb_end - start;
251 struct segment *new;
252 new = malloc(sizeof(*new));
253 *new = *seg;
254 seg->s_memsz = len;
255 new->s_memsz -= len;
256 new->s_dstaddr += len;
257 new->s_srcaddr += len;
258 if (seg->s_filesz > len) {
259 seg->s_filesz = len;
260 new->s_filesz -= len;
261 } else {
262 new->s_filesz = 0;
263 }
264 /* Order by stream offset */
265 new->next = seg->next;
266 new->prev = seg;
267 seg->next->prev = new;
268 seg->next = new;
269 /* Order by original program header order */
270 new->phdr_next = seg->phdr_next;
271 new->phdr_prev = seg;
272 seg->phdr_next->phdr_prev = new;
273 seg->phdr_next = new;
274
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000275 printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000276 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000277 new->s_dstaddr + new->s_filesz,
278 new->s_dstaddr + new->s_memsz);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000279 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000280 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000281
Ronald G. Minnichae631262009-04-01 10:48:39 +0000282 /* Now retarget this segment onto the bounce buffer */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000283 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
Ronald G. Minnichae631262009-04-01 10:48:39 +0000284 * so you will make the dstaddr be this buffer, and it will get copied
285 * later to where coreboot lives.
286 */
287 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
288
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000289 printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000290 seg->s_dstaddr,
291 seg->s_dstaddr + seg->s_filesz,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000292 seg->s_dstaddr + seg->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000293
294 return ret;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000295}
296
297
298static int build_self_segment_list(
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000299 struct segment *head,
Patrick Georgi5eceb322009-05-13 16:27:25 +0000300 struct lb_memory *mem,
Peter Stuge483b7bb2009-04-14 07:40:01 +0000301 struct cbfs_payload *payload, u32 *entry)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000302{
303 struct segment *new;
304 struct segment *ptr;
Peter Stuge483b7bb2009-04-14 07:40:01 +0000305 struct cbfs_payload_segment *segment, *first_segment;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000306 memset(head, 0, sizeof(*head));
307 head->phdr_next = head->phdr_prev = head;
308 head->next = head->prev = head;
309 first_segment = segment = &payload->segments;
310
311 while(1) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000312 printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000313 switch(segment->type) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000314 case PAYLOAD_SEGMENT_PARAMS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000315 printk(BIOS_DEBUG, " parameter section (skipped)\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000316 segment++;
317 continue;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000318
Ronald G. Minnichae631262009-04-01 10:48:39 +0000319 case PAYLOAD_SEGMENT_CODE:
320 case PAYLOAD_SEGMENT_DATA:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000321 printk(BIOS_DEBUG, " %s (compression=%x)\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000322 segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
323 ntohl(segment->compression));
324 new = malloc(sizeof(*new));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700325 new->s_dstaddr = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000326 new->s_memsz = ntohl(segment->mem_len);
327 new->compression = ntohl(segment->compression);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000328
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700329 new->s_srcaddr = (u32) ((unsigned char *)first_segment)
330 + ntohl(segment->offset);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000331 new->s_filesz = ntohl(segment->len);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000332 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 +0000333 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
334 /* Clean up the values */
335 if (new->s_filesz > new->s_memsz) {
336 new->s_filesz = new->s_memsz;
337 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000338 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 +0000339 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
340 break;
341
Ronald G. Minnichae631262009-04-01 10:48:39 +0000342 case PAYLOAD_SEGMENT_BSS:
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700343 printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
344 (intptr_t)ntohll(segment->load_addr),
345 ntohl(segment->mem_len));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000346 new = malloc(sizeof(*new));
347 new->s_filesz = 0;
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700348 new->s_dstaddr = ntohll(segment->load_addr);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000349 new->s_memsz = ntohl(segment->mem_len);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000350 break;
351
352 case PAYLOAD_SEGMENT_ENTRY:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000353 printk(BIOS_DEBUG, " Entry Point 0x%p\n", (void *) ntohl((u32) segment->load_addr));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700354 *entry = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000355 /* Per definition, a payload always has the entry point
356 * as last segment. Thus, we use the occurence of the
357 * entry point as break condition for the loop.
358 * Can we actually just look at the number of section?
359 */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000360 return 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000361
362 default:
363 /* We found something that we don't know about. Throw
364 * hands into the sky and run away!
365 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000366 printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000367 return -1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000368 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000369
Ronald G. Minnichae631262009-04-01 10:48:39 +0000370 segment++;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000371
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000372 // FIXME: Explain what this is
Ronald G. Minnichae631262009-04-01 10:48:39 +0000373 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700374 if (new->s_srcaddr < ntohll(segment->load_addr))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000375 break;
376 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000377
Ronald G. Minnichae631262009-04-01 10:48:39 +0000378 /* Order by stream offset */
379 new->next = ptr;
380 new->prev = ptr->prev;
381 ptr->prev->next = new;
382 ptr->prev = new;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000383
Ronald G. Minnichae631262009-04-01 10:48:39 +0000384 /* Order by original program header order */
385 new->phdr_next = head;
386 new->phdr_prev = head->phdr_prev;
387 head->phdr_prev->phdr_next = new;
388 head->phdr_prev = new;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000389 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000390
Ronald G. Minnichae631262009-04-01 10:48:39 +0000391 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000392}
393
394static int load_self_segments(
Patrick Georgi5eceb322009-05-13 16:27:25 +0000395 struct segment *head,
396 struct lb_memory *mem,
397 struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000398{
Ronald G. Minnichae631262009-04-01 10:48:39 +0000399 struct segment *ptr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000400
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000401 unsigned long bounce_high = lb_end;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000402 for(ptr = head->next; ptr != head; ptr = ptr->next) {
403 if (!overlaps_coreboot(ptr)) continue;
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000404 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
405 bounce_high = ptr->s_dstaddr + ptr->s_memsz;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000406 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000407 get_bounce_buffer(mem, bounce_high - lb_start);
Patrick Georgi5eceb322009-05-13 16:27:25 +0000408 if (!bounce_buffer) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000409 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
Patrick Georgi5eceb322009-05-13 16:27:25 +0000410 return 0;
411 }
412 for(ptr = head->next; ptr != head; ptr = ptr->next) {
413 /* Verify the memory addresses in the segment are valid */
414 if (!valid_area(mem, bounce_buffer, ptr->s_dstaddr, ptr->s_memsz))
415 return 0;
416 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000417 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000418 unsigned char *dest, *src;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000419 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000420 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000421
Patrick Georgi5eceb322009-05-13 16:27:25 +0000422 /* Modify the segment to load onto the bounce_buffer if necessary.
423 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000424 if (relocate_segment(bounce_buffer, ptr)) {
425 ptr = (ptr->prev)->prev;
426 continue;
427 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000428
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000429 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Patrick Georgi5eceb322009-05-13 16:27:25 +0000430 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
431
Ronald G. Minnichae631262009-04-01 10:48:39 +0000432 /* Compute the boundaries of the segment */
433 dest = (unsigned char *)(ptr->s_dstaddr);
Myles Watsonfa12b672009-04-30 22:45:41 +0000434 src = (unsigned char *)(ptr->s_srcaddr);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000435
Ronald G. Minnichae631262009-04-01 10:48:39 +0000436 /* Copy data from the initial buffer */
437 if (ptr->s_filesz) {
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000438 unsigned char *middle, *end;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000439 size_t len;
440 len = ptr->s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +0000441 switch(ptr->compression) {
Patrick Georgi369bc782009-04-25 07:32:24 +0000442 case CBFS_COMPRESS_LZMA: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000443 printk(BIOS_DEBUG, "using LZMA\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000444 len = ulzma(src, dest);
Myles Watson94de72b2010-06-01 15:19:25 +0000445 if (!len) /* Decompression Error. */
446 return 0;
Patrick Georgi369bc782009-04-25 07:32:24 +0000447 break;
448 }
Stefan Reinauer3e4fb9d2011-04-21 21:26:58 +0000449#if CONFIG_COMPRESSED_PAYLOAD_NRV2B
Patrick Georgi369bc782009-04-25 07:32:24 +0000450 case CBFS_COMPRESS_NRV2B: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000451 printk(BIOS_DEBUG, "using NRV2B\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000452 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
453 unsigned long tmp;
454 len = unrv2b(src, dest, &tmp);
455 break;
456 }
457#endif
458 case CBFS_COMPRESS_NONE: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000459 printk(BIOS_DEBUG, "it's not compressed!\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000460 memcpy(dest, src, len);
461 break;
462 }
463 default:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000464 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression);
Patrick Georgi369bc782009-04-25 07:32:24 +0000465 return -1;
466 }
467 end = dest + ptr->s_memsz;
468 middle = dest + len;
Myles Watson94de72b2010-06-01 15:19:25 +0000469 printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
Patrick Georgi369bc782009-04-25 07:32:24 +0000470 (unsigned long)dest,
471 (unsigned long)middle,
472 (unsigned long)end,
473 (unsigned long)src);
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000474
475 /* Zero the extra bytes between middle & end */
476 if (middle < end) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000477 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000478 (unsigned long)middle, (unsigned long)(end - middle));
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000479
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000480 /* Zero the extra bytes */
481 memset(middle, 0, end - middle);
482 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000483 /* Copy the data that's outside the area that shadows coreboot_ram */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000484 printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000485 if ((unsigned long)end > bounce_buffer) {
486 if ((unsigned long)dest < bounce_buffer) {
Myles Watson7943fe62009-10-30 02:08:07 +0000487 unsigned char *from = dest;
488 unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000489 unsigned long amount = bounce_buffer-(unsigned long)dest;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000490 printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000491 memcpy(to, from, amount);
492 }
493 if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
494 unsigned long from = bounce_buffer + (lb_end - lb_start);
495 unsigned long to = lb_end;
Myles Watson7943fe62009-10-30 02:08:07 +0000496 unsigned long amount = (unsigned long)end - from;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000497 printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
Myles Watson7943fe62009-10-30 02:08:07 +0000498 memcpy((char*)to, (char*)from, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000499 }
500 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000501 }
502 }
503 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000504}
505
Myles Watson92027982009-09-23 20:32:21 +0000506static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000507{
Myles Watsonfa12b672009-04-30 22:45:41 +0000508 u32 entry=0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000509 struct segment head;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000510
511 /* Preprocess the self segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000512 if (!build_self_segment_list(&head, mem, payload, &entry))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000513 goto out;
514
515 /* Load the segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000516 if (!load_self_segments(&head, mem, payload))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000517 goto out;
518
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000519 printk(BIOS_SPEW, "Loaded segments\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000520
521 /* Reset to booting from this image as late as possible */
522 boot_successful();
523
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000524 printk(BIOS_DEBUG, "Jumping to boot code at %x\n", entry);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000525 post_code(POST_ENTER_ELF_BOOT);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000526
527 /* Jump to kernel */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000528 jmp_to_elf_entry((void*)entry, bounce_buffer, bounce_size);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000529 return 1;
530
531 out:
532 return 0;
533}
534
Stefan Reinauer19436292011-11-07 12:43:03 -0800535void *cbfs_load_payload(struct lb_memory *lb_mem, const char *name)
536{
537 struct cbfs_payload *payload;
538
539 payload = (struct cbfs_payload *)cbfs_find_file(name, CBFS_TYPE_PAYLOAD);
540 if (payload == NULL)
541 return (void *) -1;
542 printk(BIOS_DEBUG, "Got a payload\n");
543
544 selfboot(lb_mem, payload);
545 printk(BIOS_EMERG, "SELFBOOT RETURNED!\n");
546
547 return (void *) -1;
548}
549