blob: 573dd5ee5a3f745d4bc269e2e4c325439e2f7e94 [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>
27#include <ip_checksum.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000028#include <stdint.h>
29#include <stdlib.h>
30#include <string.h>
Peter Stuge483b7bb2009-04-14 07:40:01 +000031#include <cbfs.h>
Myles Watson58170782009-10-28 16:13:28 +000032#include <lib.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000033
Stefan Reinauer19436292011-11-07 12:43:03 -080034/* Maximum physical address we can use for the coreboot bounce buffer. */
Ronald G. Minnichae631262009-04-01 10:48:39 +000035#ifndef MAX_ADDR
36#define MAX_ADDR -1UL
37#endif
38
Stefan Reinauer19436292011-11-07 12:43:03 -080039/* from coreboot_ram.ld: */
Ronald G. Minnichae631262009-04-01 10:48:39 +000040extern unsigned char _ram_seg;
41extern unsigned char _eram_seg;
42
Stefan Reinauer19436292011-11-07 12:43:03 -080043static const unsigned long lb_start = (unsigned long)&_ram_seg;
44static const unsigned long lb_end = (unsigned long)&_eram_seg;
45
Ronald G. Minnichae631262009-04-01 10:48:39 +000046struct segment {
47 struct segment *next;
48 struct segment *prev;
49 struct segment *phdr_next;
50 struct segment *phdr_prev;
51 unsigned long s_dstaddr;
52 unsigned long s_srcaddr;
53 unsigned long s_memsz;
54 unsigned long s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +000055 int compression;
Ronald G. Minnichae631262009-04-01 10:48:39 +000056};
57
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000058/* The problem:
Ronald G. Minnichae631262009-04-01 10:48:39 +000059 * Static executables all want to share the same addresses
60 * in memory because only a few addresses are reliably present on
61 * a machine, and implementing general relocation is hard.
62 *
63 * The solution:
Stefan Reinauerf64893a2009-07-23 22:03:14 +000064 * - Allocate a buffer the size of the coreboot image plus additional
65 * required space.
66 * - Anything that would overwrite coreboot copy into the lower part of
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000067 * the buffer.
Stefan Reinauerf64893a2009-07-23 22:03:14 +000068 * - After loading an ELF image copy coreboot to the top of the buffer.
Ronald G. Minnichae631262009-04-01 10:48:39 +000069 * - Then jump to the loaded image.
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000070 *
Ronald G. Minnichae631262009-04-01 10:48:39 +000071 * Benefits:
72 * - Nearly arbitrary standalone executables can be loaded.
73 * - Coreboot is preserved, so it can be returned to.
74 * - The implementation is still relatively simple,
Zheng Baoba49fb72009-11-01 09:18:23 +000075 * and much simpler than the general case implemented in kexec.
Ronald G. Minnichae631262009-04-01 10:48:39 +000076 */
77
Patrick Georgi5eceb322009-05-13 16:27:25 +000078static unsigned long bounce_size, bounce_buffer;
79
Myles Watson92027982009-09-23 20:32:21 +000080static void get_bounce_buffer(struct lb_memory *mem, unsigned long req_size)
Ronald G. Minnichae631262009-04-01 10:48:39 +000081{
82 unsigned long lb_size;
83 unsigned long mem_entries;
84 unsigned long buffer;
85 int i;
Stefan Reinauer19436292011-11-07 12:43:03 -080086 lb_size = lb_end - lb_start;
87 /* Plus coreboot size so I have somewhere
88 * to place a copy to return to.
89 */
Myles Watson92027982009-09-23 20:32:21 +000090 lb_size = req_size + lb_size;
Stefan Reinauer19436292011-11-07 12:43:03 -080091 mem_entries = (mem->size - sizeof(*mem)) / sizeof(mem->map[0]);
Ronald G. Minnichae631262009-04-01 10:48:39 +000092 buffer = 0;
93 for(i = 0; i < mem_entries; i++) {
94 unsigned long mstart, mend;
95 unsigned long msize;
96 unsigned long tbuffer;
97 if (mem->map[i].type != LB_MEM_RAM)
98 continue;
99 if (unpack_lb64(mem->map[i].start) > MAX_ADDR)
100 continue;
101 if (unpack_lb64(mem->map[i].size) < lb_size)
102 continue;
103 mstart = unpack_lb64(mem->map[i].start);
104 msize = MAX_ADDR - mstart +1;
105 if (msize > unpack_lb64(mem->map[i].size))
106 msize = unpack_lb64(mem->map[i].size);
107 mend = mstart + msize;
108 tbuffer = mend - lb_size;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000109 if (tbuffer < buffer)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000110 continue;
111 buffer = tbuffer;
112 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000113 bounce_buffer = buffer;
Myles Watson92027982009-09-23 20:32:21 +0000114 bounce_size = req_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000115}
116
117static int valid_area(struct lb_memory *mem, unsigned long buffer,
118 unsigned long start, unsigned long len)
119{
120 /* Check through all of the memory segments and ensure
121 * the segment that was passed in is completely contained
122 * in RAM.
123 */
124 int i;
125 unsigned long end = start + len;
Stefan Reinauer19436292011-11-07 12:43:03 -0800126 unsigned long mem_entries = (mem->size - sizeof(*mem)) /
127 sizeof(mem->map[0]);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000128
129 /* See if I conflict with the bounce buffer */
130 if (end >= buffer) {
131 return 0;
132 }
133
134 /* Walk through the table of valid memory ranges and see if I
135 * have a match.
136 */
137 for(i = 0; i < mem_entries; i++) {
138 uint64_t mstart, mend;
139 uint32_t mtype;
140 mtype = mem->map[i].type;
141 mstart = unpack_lb64(mem->map[i].start);
142 mend = mstart + unpack_lb64(mem->map[i].size);
143 if ((mtype == LB_MEM_RAM) && (start < mend) && (end > mstart)) {
144 break;
145 }
146 if ((mtype == LB_MEM_TABLE) && (start < mend) && (end > mstart)) {
Stefan Reinauerd6b4f1c2010-09-23 18:29:40 +0000147 printk(BIOS_ERR, "Payload is overwriting coreboot tables.\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000148 break;
149 }
150 }
151 if (i == mem_entries) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000152 printk(BIOS_ERR, "No matching ram area found for range:\n");
153 printk(BIOS_ERR, " [0x%016lx, 0x%016lx)\n", start, end);
154 printk(BIOS_ERR, "Ram areas\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000155 for(i = 0; i < mem_entries; i++) {
156 uint64_t mstart, mend;
157 uint32_t mtype;
158 mtype = mem->map[i].type;
159 mstart = unpack_lb64(mem->map[i].start);
160 mend = mstart + unpack_lb64(mem->map[i].size);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000161 printk(BIOS_ERR, " [0x%016lx, 0x%016lx) %s\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000162 (unsigned long)mstart,
163 (unsigned long)mend,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000164 (mtype == LB_MEM_RAM)?"RAM":"Reserved");
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000165
Ronald G. Minnichae631262009-04-01 10:48:39 +0000166 }
167 return 0;
168 }
169 return 1;
170}
171
Patrick Georgi5eceb322009-05-13 16:27:25 +0000172
173static int overlaps_coreboot(struct segment *seg)
174{
175 unsigned long start, end;
176 start = seg->s_dstaddr;
177 end = start + seg->s_memsz;
178 return !((end <= lb_start) || (start >= lb_end));
179}
180
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000181static int relocate_segment(unsigned long buffer, struct segment *seg)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000182{
183 /* Modify all segments that want to load onto coreboot
184 * to load onto the bounce buffer instead.
185 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000186 /* ret: 1 : A new segment is inserted before the seg.
Stefan Reinauer19436292011-11-07 12:43:03 -0800187 * 0 : A new segment is inserted after the seg, or no new one.
188 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000189 unsigned long start, middle, end, ret = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000190
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000191 printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000192 lb_start, lb_end);
193
Patrick Georgi5eceb322009-05-13 16:27:25 +0000194 /* I don't conflict with coreboot so get out of here */
195 if (!overlaps_coreboot(seg))
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000196 return 0;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000197
Ronald G. Minnichae631262009-04-01 10:48:39 +0000198 start = seg->s_dstaddr;
199 middle = start + seg->s_filesz;
200 end = start + seg->s_memsz;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000201
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000202 printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000203 start, middle, end);
204
Patrick Georgi369bc782009-04-25 07:32:24 +0000205 if (seg->compression == CBFS_COMPRESS_NONE) {
206 /* Slice off a piece at the beginning
207 * that doesn't conflict with coreboot.
208 */
209 if (start < lb_start) {
210 struct segment *new;
211 unsigned long len = lb_start - start;
212 new = malloc(sizeof(*new));
213 *new = *seg;
214 new->s_memsz = len;
215 seg->s_memsz -= len;
216 seg->s_dstaddr += len;
217 seg->s_srcaddr += len;
218 if (seg->s_filesz > len) {
219 new->s_filesz = len;
220 seg->s_filesz -= len;
221 } else {
222 seg->s_filesz = 0;
223 }
224
225 /* Order by stream offset */
226 new->next = seg;
227 new->prev = seg->prev;
228 seg->prev->next = new;
229 seg->prev = new;
230 /* Order by original program header order */
231 new->phdr_next = seg;
232 new->phdr_prev = seg->phdr_prev;
233 seg->phdr_prev->phdr_next = new;
234 seg->phdr_prev = new;
235
236 /* compute the new value of start */
237 start = seg->s_dstaddr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000238
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000239 printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000240 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000241 new->s_dstaddr + new->s_filesz,
242 new->s_dstaddr + new->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000243
244 ret = 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000245 }
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000246
247 /* Slice off a piece at the end
248 * that doesn't conflict with coreboot
Patrick Georgi369bc782009-04-25 07:32:24 +0000249 */
250 if (end > lb_end) {
251 unsigned long len = lb_end - start;
252 struct segment *new;
253 new = malloc(sizeof(*new));
254 *new = *seg;
255 seg->s_memsz = len;
256 new->s_memsz -= len;
257 new->s_dstaddr += len;
258 new->s_srcaddr += len;
259 if (seg->s_filesz > len) {
260 seg->s_filesz = len;
261 new->s_filesz -= len;
262 } else {
263 new->s_filesz = 0;
264 }
265 /* Order by stream offset */
266 new->next = seg->next;
267 new->prev = seg;
268 seg->next->prev = new;
269 seg->next = new;
270 /* Order by original program header order */
271 new->phdr_next = seg->phdr_next;
272 new->phdr_prev = seg;
273 seg->phdr_next->phdr_prev = new;
274 seg->phdr_next = new;
275
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000276 printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000277 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000278 new->s_dstaddr + new->s_filesz,
279 new->s_dstaddr + new->s_memsz);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000280 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000281 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000282
Ronald G. Minnichae631262009-04-01 10:48:39 +0000283 /* Now retarget this segment onto the bounce buffer */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000284 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
Ronald G. Minnichae631262009-04-01 10:48:39 +0000285 * so you will make the dstaddr be this buffer, and it will get copied
286 * later to where coreboot lives.
287 */
288 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
289
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000290 printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000291 seg->s_dstaddr,
292 seg->s_dstaddr + seg->s_filesz,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000293 seg->s_dstaddr + seg->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000294
295 return ret;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000296}
297
298
299static int build_self_segment_list(
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000300 struct segment *head,
Patrick Georgi5eceb322009-05-13 16:27:25 +0000301 struct lb_memory *mem,
Peter Stuge483b7bb2009-04-14 07:40:01 +0000302 struct cbfs_payload *payload, u32 *entry)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000303{
304 struct segment *new;
305 struct segment *ptr;
Peter Stuge483b7bb2009-04-14 07:40:01 +0000306 struct cbfs_payload_segment *segment, *first_segment;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000307 memset(head, 0, sizeof(*head));
308 head->phdr_next = head->phdr_prev = head;
309 head->next = head->prev = head;
310 first_segment = segment = &payload->segments;
311
312 while(1) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000313 printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000314 switch(segment->type) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000315 case PAYLOAD_SEGMENT_PARAMS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000316 printk(BIOS_DEBUG, " parameter section (skipped)\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000317 segment++;
318 continue;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000319
Ronald G. Minnichae631262009-04-01 10:48:39 +0000320 case PAYLOAD_SEGMENT_CODE:
321 case PAYLOAD_SEGMENT_DATA:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000322 printk(BIOS_DEBUG, " %s (compression=%x)\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000323 segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
324 ntohl(segment->compression));
325 new = malloc(sizeof(*new));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700326 new->s_dstaddr = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000327 new->s_memsz = ntohl(segment->mem_len);
328 new->compression = ntohl(segment->compression);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000329
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700330 new->s_srcaddr = (u32) ((unsigned char *)first_segment)
331 + ntohl(segment->offset);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000332 new->s_filesz = ntohl(segment->len);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000333 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 +0000334 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
335 /* Clean up the values */
336 if (new->s_filesz > new->s_memsz) {
337 new->s_filesz = new->s_memsz;
338 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000339 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 +0000340 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
341 break;
342
Ronald G. Minnichae631262009-04-01 10:48:39 +0000343 case PAYLOAD_SEGMENT_BSS:
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700344 printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
345 (intptr_t)ntohll(segment->load_addr),
346 ntohl(segment->mem_len));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000347 new = malloc(sizeof(*new));
348 new->s_filesz = 0;
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700349 new->s_dstaddr = ntohll(segment->load_addr);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000350 new->s_memsz = ntohl(segment->mem_len);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000351 break;
352
353 case PAYLOAD_SEGMENT_ENTRY:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000354 printk(BIOS_DEBUG, " Entry Point 0x%p\n", (void *) ntohl((u32) segment->load_addr));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700355 *entry = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000356 /* Per definition, a payload always has the entry point
357 * as last segment. Thus, we use the occurence of the
358 * entry point as break condition for the loop.
359 * Can we actually just look at the number of section?
360 */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000361 return 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000362
363 default:
364 /* We found something that we don't know about. Throw
365 * hands into the sky and run away!
366 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000367 printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000368 return -1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000369 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000370
Ronald G. Minnichae631262009-04-01 10:48:39 +0000371 segment++;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000372
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000373 // FIXME: Explain what this is
Ronald G. Minnichae631262009-04-01 10:48:39 +0000374 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700375 if (new->s_srcaddr < ntohll(segment->load_addr))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000376 break;
377 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000378
Ronald G. Minnichae631262009-04-01 10:48:39 +0000379 /* Order by stream offset */
380 new->next = ptr;
381 new->prev = ptr->prev;
382 ptr->prev->next = new;
383 ptr->prev = new;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000384
Ronald G. Minnichae631262009-04-01 10:48:39 +0000385 /* Order by original program header order */
386 new->phdr_next = head;
387 new->phdr_prev = head->phdr_prev;
388 head->phdr_prev->phdr_next = new;
389 head->phdr_prev = new;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000390 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000391
Ronald G. Minnichae631262009-04-01 10:48:39 +0000392 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000393}
394
395static int load_self_segments(
Patrick Georgi5eceb322009-05-13 16:27:25 +0000396 struct segment *head,
397 struct lb_memory *mem,
398 struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000399{
Ronald G. Minnichae631262009-04-01 10:48:39 +0000400 struct segment *ptr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000401
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000402 unsigned long bounce_high = lb_end;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000403 for(ptr = head->next; ptr != head; ptr = ptr->next) {
404 if (!overlaps_coreboot(ptr)) continue;
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000405 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
406 bounce_high = ptr->s_dstaddr + ptr->s_memsz;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000407 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000408 get_bounce_buffer(mem, bounce_high - lb_start);
Patrick Georgi5eceb322009-05-13 16:27:25 +0000409 if (!bounce_buffer) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000410 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
Patrick Georgi5eceb322009-05-13 16:27:25 +0000411 return 0;
412 }
413 for(ptr = head->next; ptr != head; ptr = ptr->next) {
414 /* Verify the memory addresses in the segment are valid */
415 if (!valid_area(mem, bounce_buffer, ptr->s_dstaddr, ptr->s_memsz))
416 return 0;
417 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000418 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000419 unsigned char *dest, *src;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000420 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000421 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000422
Patrick Georgi5eceb322009-05-13 16:27:25 +0000423 /* Modify the segment to load onto the bounce_buffer if necessary.
424 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000425 if (relocate_segment(bounce_buffer, ptr)) {
426 ptr = (ptr->prev)->prev;
427 continue;
428 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000429
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000430 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Patrick Georgi5eceb322009-05-13 16:27:25 +0000431 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
432
Ronald G. Minnichae631262009-04-01 10:48:39 +0000433 /* Compute the boundaries of the segment */
434 dest = (unsigned char *)(ptr->s_dstaddr);
Myles Watsonfa12b672009-04-30 22:45:41 +0000435 src = (unsigned char *)(ptr->s_srcaddr);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000436
Ronald G. Minnichae631262009-04-01 10:48:39 +0000437 /* Copy data from the initial buffer */
438 if (ptr->s_filesz) {
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000439 unsigned char *middle, *end;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000440 size_t len;
441 len = ptr->s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +0000442 switch(ptr->compression) {
Patrick Georgi369bc782009-04-25 07:32:24 +0000443 case CBFS_COMPRESS_LZMA: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000444 printk(BIOS_DEBUG, "using LZMA\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000445 len = ulzma(src, dest);
Myles Watson94de72b2010-06-01 15:19:25 +0000446 if (!len) /* Decompression Error. */
447 return 0;
Patrick Georgi369bc782009-04-25 07:32:24 +0000448 break;
449 }
Stefan Reinauer3e4fb9d2011-04-21 21:26:58 +0000450#if CONFIG_COMPRESSED_PAYLOAD_NRV2B
Patrick Georgi369bc782009-04-25 07:32:24 +0000451 case CBFS_COMPRESS_NRV2B: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000452 printk(BIOS_DEBUG, "using NRV2B\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000453 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
454 unsigned long tmp;
455 len = unrv2b(src, dest, &tmp);
456 break;
457 }
458#endif
459 case CBFS_COMPRESS_NONE: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000460 printk(BIOS_DEBUG, "it's not compressed!\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000461 memcpy(dest, src, len);
462 break;
463 }
464 default:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000465 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression);
Patrick Georgi369bc782009-04-25 07:32:24 +0000466 return -1;
467 }
468 end = dest + ptr->s_memsz;
469 middle = dest + len;
Myles Watson94de72b2010-06-01 15:19:25 +0000470 printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
Patrick Georgi369bc782009-04-25 07:32:24 +0000471 (unsigned long)dest,
472 (unsigned long)middle,
473 (unsigned long)end,
474 (unsigned long)src);
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000475
476 /* Zero the extra bytes between middle & end */
477 if (middle < end) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000478 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000479 (unsigned long)middle, (unsigned long)(end - middle));
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000480
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000481 /* Zero the extra bytes */
482 memset(middle, 0, end - middle);
483 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000484 /* Copy the data that's outside the area that shadows coreboot_ram */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000485 printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000486 if ((unsigned long)end > bounce_buffer) {
487 if ((unsigned long)dest < bounce_buffer) {
Myles Watson7943fe62009-10-30 02:08:07 +0000488 unsigned char *from = dest;
489 unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000490 unsigned long amount = bounce_buffer-(unsigned long)dest;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000491 printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000492 memcpy(to, from, amount);
493 }
494 if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
495 unsigned long from = bounce_buffer + (lb_end - lb_start);
496 unsigned long to = lb_end;
Myles Watson7943fe62009-10-30 02:08:07 +0000497 unsigned long amount = (unsigned long)end - from;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000498 printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
Myles Watson7943fe62009-10-30 02:08:07 +0000499 memcpy((char*)to, (char*)from, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000500 }
501 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000502 }
503 }
504 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000505}
506
Myles Watson92027982009-09-23 20:32:21 +0000507static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000508{
Myles Watsonfa12b672009-04-30 22:45:41 +0000509 u32 entry=0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000510 struct segment head;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000511
512 /* Preprocess the self segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000513 if (!build_self_segment_list(&head, mem, payload, &entry))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000514 goto out;
515
516 /* Load the segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000517 if (!load_self_segments(&head, mem, payload))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000518 goto out;
519
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000520 printk(BIOS_SPEW, "Loaded segments\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000521
522 /* Reset to booting from this image as late as possible */
523 boot_successful();
524
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000525 printk(BIOS_DEBUG, "Jumping to boot code at %x\n", entry);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000526 post_code(POST_ENTER_ELF_BOOT);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000527
528 /* Jump to kernel */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000529 jmp_to_elf_entry((void*)entry, bounce_buffer, bounce_size);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000530 return 1;
531
532 out:
533 return 0;
534}
535
Stefan Reinauer19436292011-11-07 12:43:03 -0800536void *cbfs_load_payload(struct lb_memory *lb_mem, const char *name)
537{
538 struct cbfs_payload *payload;
539
540 payload = (struct cbfs_payload *)cbfs_find_file(name, CBFS_TYPE_PAYLOAD);
541 if (payload == NULL)
542 return (void *) -1;
543 printk(BIOS_DEBUG, "Got a payload\n");
544
545 selfboot(lb_mem, payload);
546 printk(BIOS_EMERG, "SELFBOOT RETURNED!\n");
547
548 return (void *) -1;
549}
550