blob: 99b1493ed7c39d0d6ed3db78a7845bb6bf41ed5c [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;
Ronald G. Minnichae631262009-04-01 10:48:39 +000048 unsigned long s_dstaddr;
49 unsigned long s_srcaddr;
50 unsigned long s_memsz;
51 unsigned long s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +000052 int compression;
Ronald G. Minnichae631262009-04-01 10:48:39 +000053};
54
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000055/* The problem:
Ronald G. Minnichae631262009-04-01 10:48:39 +000056 * Static executables all want to share the same addresses
57 * in memory because only a few addresses are reliably present on
58 * a machine, and implementing general relocation is hard.
59 *
60 * The solution:
Stefan Reinauerf64893a2009-07-23 22:03:14 +000061 * - Allocate a buffer the size of the coreboot image plus additional
62 * required space.
63 * - Anything that would overwrite coreboot copy into the lower part of
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000064 * the buffer.
Stefan Reinauerf64893a2009-07-23 22:03:14 +000065 * - After loading an ELF image copy coreboot to the top of the buffer.
Ronald G. Minnichae631262009-04-01 10:48:39 +000066 * - Then jump to the loaded image.
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000067 *
Ronald G. Minnichae631262009-04-01 10:48:39 +000068 * Benefits:
69 * - Nearly arbitrary standalone executables can be loaded.
70 * - Coreboot is preserved, so it can be returned to.
71 * - The implementation is still relatively simple,
Zheng Baoba49fb72009-11-01 09:18:23 +000072 * and much simpler than the general case implemented in kexec.
Ronald G. Minnichae631262009-04-01 10:48:39 +000073 */
74
Patrick Georgi5eceb322009-05-13 16:27:25 +000075static unsigned long bounce_size, bounce_buffer;
76
Myles Watson92027982009-09-23 20:32:21 +000077static void get_bounce_buffer(struct lb_memory *mem, unsigned long req_size)
Ronald G. Minnichae631262009-04-01 10:48:39 +000078{
79 unsigned long lb_size;
80 unsigned long mem_entries;
81 unsigned long buffer;
82 int i;
Stefan Reinauer19436292011-11-07 12:43:03 -080083 lb_size = lb_end - lb_start;
84 /* Plus coreboot size so I have somewhere
85 * to place a copy to return to.
86 */
Myles Watson92027982009-09-23 20:32:21 +000087 lb_size = req_size + lb_size;
Stefan Reinauer19436292011-11-07 12:43:03 -080088 mem_entries = (mem->size - sizeof(*mem)) / sizeof(mem->map[0]);
Ronald G. Minnichae631262009-04-01 10:48:39 +000089 buffer = 0;
90 for(i = 0; i < mem_entries; i++) {
91 unsigned long mstart, mend;
92 unsigned long msize;
93 unsigned long tbuffer;
94 if (mem->map[i].type != LB_MEM_RAM)
95 continue;
96 if (unpack_lb64(mem->map[i].start) > MAX_ADDR)
97 continue;
98 if (unpack_lb64(mem->map[i].size) < lb_size)
99 continue;
100 mstart = unpack_lb64(mem->map[i].start);
101 msize = MAX_ADDR - mstart +1;
102 if (msize > unpack_lb64(mem->map[i].size))
103 msize = unpack_lb64(mem->map[i].size);
104 mend = mstart + msize;
105 tbuffer = mend - lb_size;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000106 if (tbuffer < buffer)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000107 continue;
108 buffer = tbuffer;
109 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000110 bounce_buffer = buffer;
Myles Watson92027982009-09-23 20:32:21 +0000111 bounce_size = req_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000112}
113
114static int valid_area(struct lb_memory *mem, unsigned long buffer,
115 unsigned long start, unsigned long len)
116{
117 /* Check through all of the memory segments and ensure
118 * the segment that was passed in is completely contained
119 * in RAM.
120 */
121 int i;
122 unsigned long end = start + len;
Stefan Reinauer19436292011-11-07 12:43:03 -0800123 unsigned long mem_entries = (mem->size - sizeof(*mem)) /
124 sizeof(mem->map[0]);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000125
126 /* See if I conflict with the bounce buffer */
127 if (end >= buffer) {
128 return 0;
129 }
130
131 /* Walk through the table of valid memory ranges and see if I
132 * have a match.
133 */
134 for(i = 0; i < mem_entries; i++) {
135 uint64_t mstart, mend;
136 uint32_t mtype;
137 mtype = mem->map[i].type;
138 mstart = unpack_lb64(mem->map[i].start);
139 mend = mstart + unpack_lb64(mem->map[i].size);
Stefan Reinauerc6b8b7d2011-11-07 12:56:12 -0800140 if ((mtype == LB_MEM_RAM) && (start >= mstart) && (end < mend)) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000141 break;
142 }
Stefan Reinauerc6b8b7d2011-11-07 12:56:12 -0800143 if ((mtype == LB_MEM_TABLE) && (start >= mstart) && (end < mend)) {
Stefan Reinauerd6b4f1c2010-09-23 18:29:40 +0000144 printk(BIOS_ERR, "Payload is overwriting coreboot tables.\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000145 break;
146 }
147 }
148 if (i == mem_entries) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000149 printk(BIOS_ERR, "No matching ram area found for range:\n");
150 printk(BIOS_ERR, " [0x%016lx, 0x%016lx)\n", start, end);
151 printk(BIOS_ERR, "Ram areas\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000152 for(i = 0; i < mem_entries; i++) {
153 uint64_t mstart, mend;
154 uint32_t mtype;
155 mtype = mem->map[i].type;
156 mstart = unpack_lb64(mem->map[i].start);
157 mend = mstart + unpack_lb64(mem->map[i].size);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000158 printk(BIOS_ERR, " [0x%016lx, 0x%016lx) %s\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000159 (unsigned long)mstart,
160 (unsigned long)mend,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000161 (mtype == LB_MEM_RAM)?"RAM":"Reserved");
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000162
Ronald G. Minnichae631262009-04-01 10:48:39 +0000163 }
164 return 0;
165 }
166 return 1;
167}
168
Patrick Georgi5eceb322009-05-13 16:27:25 +0000169
170static int overlaps_coreboot(struct segment *seg)
171{
172 unsigned long start, end;
173 start = seg->s_dstaddr;
174 end = start + seg->s_memsz;
175 return !((end <= lb_start) || (start >= lb_end));
176}
177
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000178static int relocate_segment(unsigned long buffer, struct segment *seg)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000179{
180 /* Modify all segments that want to load onto coreboot
181 * to load onto the bounce buffer instead.
182 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000183 /* ret: 1 : A new segment is inserted before the seg.
Stefan Reinauer19436292011-11-07 12:43:03 -0800184 * 0 : A new segment is inserted after the seg, or no new one.
185 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000186 unsigned long start, middle, end, ret = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000187
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000188 printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000189 lb_start, lb_end);
190
Patrick Georgi5eceb322009-05-13 16:27:25 +0000191 /* I don't conflict with coreboot so get out of here */
192 if (!overlaps_coreboot(seg))
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000193 return 0;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000194
Ronald G. Minnichae631262009-04-01 10:48:39 +0000195 start = seg->s_dstaddr;
196 middle = start + seg->s_filesz;
197 end = start + seg->s_memsz;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000198
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000199 printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000200 start, middle, end);
201
Patrick Georgi369bc782009-04-25 07:32:24 +0000202 if (seg->compression == CBFS_COMPRESS_NONE) {
203 /* Slice off a piece at the beginning
204 * that doesn't conflict with coreboot.
205 */
206 if (start < lb_start) {
207 struct segment *new;
208 unsigned long len = lb_start - start;
209 new = malloc(sizeof(*new));
210 *new = *seg;
211 new->s_memsz = len;
212 seg->s_memsz -= len;
213 seg->s_dstaddr += len;
214 seg->s_srcaddr += len;
215 if (seg->s_filesz > len) {
216 new->s_filesz = len;
217 seg->s_filesz -= len;
218 } else {
219 seg->s_filesz = 0;
220 }
221
222 /* Order by stream offset */
223 new->next = seg;
224 new->prev = seg->prev;
225 seg->prev->next = new;
226 seg->prev = new;
Patrick Georgi369bc782009-04-25 07:32:24 +0000227
228 /* compute the new value of start */
229 start = seg->s_dstaddr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000230
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000231 printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000232 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000233 new->s_dstaddr + new->s_filesz,
234 new->s_dstaddr + new->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000235
236 ret = 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000237 }
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000238
239 /* Slice off a piece at the end
240 * that doesn't conflict with coreboot
Patrick Georgi369bc782009-04-25 07:32:24 +0000241 */
242 if (end > lb_end) {
243 unsigned long len = lb_end - start;
244 struct segment *new;
245 new = malloc(sizeof(*new));
246 *new = *seg;
247 seg->s_memsz = len;
248 new->s_memsz -= len;
249 new->s_dstaddr += len;
250 new->s_srcaddr += len;
251 if (seg->s_filesz > len) {
252 seg->s_filesz = len;
253 new->s_filesz -= len;
254 } else {
255 new->s_filesz = 0;
256 }
257 /* Order by stream offset */
258 new->next = seg->next;
259 new->prev = seg;
260 seg->next->prev = new;
261 seg->next = new;
Patrick Georgi369bc782009-04-25 07:32:24 +0000262
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000263 printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000264 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000265 new->s_dstaddr + new->s_filesz,
266 new->s_dstaddr + new->s_memsz);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000267 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000268 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000269
Ronald G. Minnichae631262009-04-01 10:48:39 +0000270 /* Now retarget this segment onto the bounce buffer */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000271 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
Ronald G. Minnichae631262009-04-01 10:48:39 +0000272 * so you will make the dstaddr be this buffer, and it will get copied
273 * later to where coreboot lives.
274 */
275 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
276
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000277 printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000278 seg->s_dstaddr,
279 seg->s_dstaddr + seg->s_filesz,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000280 seg->s_dstaddr + seg->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000281
282 return ret;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000283}
284
285
286static int build_self_segment_list(
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000287 struct segment *head,
Patrick Georgi5eceb322009-05-13 16:27:25 +0000288 struct lb_memory *mem,
Peter Stuge483b7bb2009-04-14 07:40:01 +0000289 struct cbfs_payload *payload, u32 *entry)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000290{
291 struct segment *new;
292 struct segment *ptr;
Peter Stuge483b7bb2009-04-14 07:40:01 +0000293 struct cbfs_payload_segment *segment, *first_segment;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000294 memset(head, 0, sizeof(*head));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000295 head->next = head->prev = head;
296 first_segment = segment = &payload->segments;
297
298 while(1) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000299 printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000300 switch(segment->type) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000301 case PAYLOAD_SEGMENT_PARAMS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000302 printk(BIOS_DEBUG, " parameter section (skipped)\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000303 segment++;
304 continue;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000305
Ronald G. Minnichae631262009-04-01 10:48:39 +0000306 case PAYLOAD_SEGMENT_CODE:
307 case PAYLOAD_SEGMENT_DATA:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000308 printk(BIOS_DEBUG, " %s (compression=%x)\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000309 segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
310 ntohl(segment->compression));
311 new = malloc(sizeof(*new));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700312 new->s_dstaddr = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000313 new->s_memsz = ntohl(segment->mem_len);
314 new->compression = ntohl(segment->compression);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000315
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700316 new->s_srcaddr = (u32) ((unsigned char *)first_segment)
317 + ntohl(segment->offset);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000318 new->s_filesz = ntohl(segment->len);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000319 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 +0000320 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
321 /* Clean up the values */
322 if (new->s_filesz > new->s_memsz) {
323 new->s_filesz = new->s_memsz;
324 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000325 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 +0000326 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
327 break;
328
Ronald G. Minnichae631262009-04-01 10:48:39 +0000329 case PAYLOAD_SEGMENT_BSS:
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700330 printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
331 (intptr_t)ntohll(segment->load_addr),
332 ntohl(segment->mem_len));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000333 new = malloc(sizeof(*new));
334 new->s_filesz = 0;
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700335 new->s_dstaddr = ntohll(segment->load_addr);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000336 new->s_memsz = ntohl(segment->mem_len);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000337 break;
338
339 case PAYLOAD_SEGMENT_ENTRY:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000340 printk(BIOS_DEBUG, " Entry Point 0x%p\n", (void *) ntohl((u32) segment->load_addr));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700341 *entry = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000342 /* Per definition, a payload always has the entry point
343 * as last segment. Thus, we use the occurence of the
344 * entry point as break condition for the loop.
345 * Can we actually just look at the number of section?
346 */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000347 return 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000348
349 default:
350 /* We found something that we don't know about. Throw
351 * hands into the sky and run away!
352 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000353 printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000354 return -1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000355 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000356
Stefan Reinauerc44de492012-01-11 14:07:39 -0800357 /* We have found another CODE, DATA or BSS segment */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000358 segment++;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000359
Stefan Reinauerc44de492012-01-11 14:07:39 -0800360 /* Find place where to insert our segment */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000361 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700362 if (new->s_srcaddr < ntohll(segment->load_addr))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000363 break;
364 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000365
Ronald G. Minnichae631262009-04-01 10:48:39 +0000366 /* Order by stream offset */
367 new->next = ptr;
368 new->prev = ptr->prev;
369 ptr->prev->next = new;
370 ptr->prev = new;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000371 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000372
Ronald G. Minnichae631262009-04-01 10:48:39 +0000373 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000374}
375
376static int load_self_segments(
Patrick Georgi5eceb322009-05-13 16:27:25 +0000377 struct segment *head,
378 struct lb_memory *mem,
379 struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000380{
Ronald G. Minnichae631262009-04-01 10:48:39 +0000381 struct segment *ptr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000382
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000383 unsigned long bounce_high = lb_end;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000384 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerc44de492012-01-11 14:07:39 -0800385 if (!overlaps_coreboot(ptr))
386 continue;
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000387 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
388 bounce_high = ptr->s_dstaddr + ptr->s_memsz;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000389 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000390 get_bounce_buffer(mem, bounce_high - lb_start);
Patrick Georgi5eceb322009-05-13 16:27:25 +0000391 if (!bounce_buffer) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000392 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
Patrick Georgi5eceb322009-05-13 16:27:25 +0000393 return 0;
394 }
395 for(ptr = head->next; ptr != head; ptr = ptr->next) {
396 /* Verify the memory addresses in the segment are valid */
397 if (!valid_area(mem, bounce_buffer, ptr->s_dstaddr, ptr->s_memsz))
398 return 0;
399 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000400 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000401 unsigned char *dest, *src;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000402 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000403 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000404
Patrick Georgi5eceb322009-05-13 16:27:25 +0000405 /* Modify the segment to load onto the bounce_buffer if necessary.
406 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000407 if (relocate_segment(bounce_buffer, ptr)) {
408 ptr = (ptr->prev)->prev;
409 continue;
410 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000411
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000412 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Patrick Georgi5eceb322009-05-13 16:27:25 +0000413 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
414
Ronald G. Minnichae631262009-04-01 10:48:39 +0000415 /* Compute the boundaries of the segment */
416 dest = (unsigned char *)(ptr->s_dstaddr);
Myles Watsonfa12b672009-04-30 22:45:41 +0000417 src = (unsigned char *)(ptr->s_srcaddr);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000418
Ronald G. Minnichae631262009-04-01 10:48:39 +0000419 /* Copy data from the initial buffer */
420 if (ptr->s_filesz) {
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000421 unsigned char *middle, *end;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000422 size_t len;
423 len = ptr->s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +0000424 switch(ptr->compression) {
Patrick Georgi369bc782009-04-25 07:32:24 +0000425 case CBFS_COMPRESS_LZMA: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000426 printk(BIOS_DEBUG, "using LZMA\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000427 len = ulzma(src, dest);
Myles Watson94de72b2010-06-01 15:19:25 +0000428 if (!len) /* Decompression Error. */
429 return 0;
Patrick Georgi369bc782009-04-25 07:32:24 +0000430 break;
431 }
Stefan Reinauer3e4fb9d2011-04-21 21:26:58 +0000432#if CONFIG_COMPRESSED_PAYLOAD_NRV2B
Patrick Georgi369bc782009-04-25 07:32:24 +0000433 case CBFS_COMPRESS_NRV2B: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000434 printk(BIOS_DEBUG, "using NRV2B\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000435 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
436 unsigned long tmp;
437 len = unrv2b(src, dest, &tmp);
438 break;
439 }
440#endif
441 case CBFS_COMPRESS_NONE: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000442 printk(BIOS_DEBUG, "it's not compressed!\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000443 memcpy(dest, src, len);
444 break;
445 }
446 default:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000447 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression);
Patrick Georgi369bc782009-04-25 07:32:24 +0000448 return -1;
449 }
450 end = dest + ptr->s_memsz;
451 middle = dest + len;
Myles Watson94de72b2010-06-01 15:19:25 +0000452 printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
Patrick Georgi369bc782009-04-25 07:32:24 +0000453 (unsigned long)dest,
454 (unsigned long)middle,
455 (unsigned long)end,
456 (unsigned long)src);
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000457
458 /* Zero the extra bytes between middle & end */
459 if (middle < end) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000460 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000461 (unsigned long)middle, (unsigned long)(end - middle));
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000462
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000463 /* Zero the extra bytes */
464 memset(middle, 0, end - middle);
465 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000466 /* Copy the data that's outside the area that shadows coreboot_ram */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000467 printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000468 if ((unsigned long)end > bounce_buffer) {
469 if ((unsigned long)dest < bounce_buffer) {
Myles Watson7943fe62009-10-30 02:08:07 +0000470 unsigned char *from = dest;
471 unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000472 unsigned long amount = bounce_buffer-(unsigned long)dest;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000473 printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000474 memcpy(to, from, amount);
475 }
476 if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
477 unsigned long from = bounce_buffer + (lb_end - lb_start);
478 unsigned long to = lb_end;
Myles Watson7943fe62009-10-30 02:08:07 +0000479 unsigned long amount = (unsigned long)end - from;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000480 printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
Myles Watson7943fe62009-10-30 02:08:07 +0000481 memcpy((char*)to, (char*)from, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000482 }
483 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000484 }
485 }
486 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000487}
488
Myles Watson92027982009-09-23 20:32:21 +0000489static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000490{
Myles Watsonfa12b672009-04-30 22:45:41 +0000491 u32 entry=0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000492 struct segment head;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000493
494 /* Preprocess the self segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000495 if (!build_self_segment_list(&head, mem, payload, &entry))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000496 goto out;
497
498 /* Load the segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000499 if (!load_self_segments(&head, mem, payload))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000500 goto out;
501
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000502 printk(BIOS_SPEW, "Loaded segments\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000503
504 /* Reset to booting from this image as late as possible */
505 boot_successful();
506
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000507 printk(BIOS_DEBUG, "Jumping to boot code at %x\n", entry);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000508 post_code(POST_ENTER_ELF_BOOT);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000509
510 /* Jump to kernel */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000511 jmp_to_elf_entry((void*)entry, bounce_buffer, bounce_size);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000512 return 1;
513
Patrick Georgic5fc7db2012-03-07 15:55:47 +0100514out:
Ronald G. Minnichae631262009-04-01 10:48:39 +0000515 return 0;
516}
517
Stefan Reinauer19436292011-11-07 12:43:03 -0800518void *cbfs_load_payload(struct lb_memory *lb_mem, const char *name)
519{
520 struct cbfs_payload *payload;
521
522 payload = (struct cbfs_payload *)cbfs_find_file(name, CBFS_TYPE_PAYLOAD);
523 if (payload == NULL)
524 return (void *) -1;
525 printk(BIOS_DEBUG, "Got a payload\n");
526
527 selfboot(lb_mem, payload);
528 printk(BIOS_EMERG, "SELFBOOT RETURNED!\n");
529
530 return (void *) -1;
531}
532