blob: be03b853a6e417b25240a126dfe187800320d87c [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>
Aaron Durbinebf142a2013-03-29 16:23:23 -050023#include <cpu/cpu.h>
Stefan Reinauerde3206a2010-02-22 06:09:43 +000024#include <fallback.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000025#include <boot/elf.h>
26#include <boot/elf_boot.h>
27#include <boot/coreboot_tables.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>
Duncan Lauriecde78012011-10-19 15:32:39 -070033#if CONFIG_COLLECT_TIMESTAMPS
34#include <timestamp.h>
35#endif
Stefan Reinauerd37ab452012-12-18 16:23:28 -080036#include <coverage.h>
Ronald G. Minnichae631262009-04-01 10:48:39 +000037
Stefan Reinauer19436292011-11-07 12:43:03 -080038/* Maximum physical address we can use for the coreboot bounce buffer. */
Ronald G. Minnichae631262009-04-01 10:48:39 +000039#ifndef MAX_ADDR
40#define MAX_ADDR -1UL
41#endif
42
Stefan Reinauer19436292011-11-07 12:43:03 -080043/* from coreboot_ram.ld: */
Ronald G. Minnichae631262009-04-01 10:48:39 +000044extern unsigned char _ram_seg;
45extern unsigned char _eram_seg;
46
Stefan Reinauer19436292011-11-07 12:43:03 -080047static const unsigned long lb_start = (unsigned long)&_ram_seg;
48static const unsigned long lb_end = (unsigned long)&_eram_seg;
49
Ronald G. Minnichae631262009-04-01 10:48:39 +000050struct segment {
51 struct segment *next;
52 struct segment *prev;
Ronald G. Minnichae631262009-04-01 10:48:39 +000053 unsigned long s_dstaddr;
54 unsigned long s_srcaddr;
55 unsigned long s_memsz;
56 unsigned long s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +000057 int compression;
Ronald G. Minnichae631262009-04-01 10:48:39 +000058};
59
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000060/* The problem:
Ronald G. Minnichae631262009-04-01 10:48:39 +000061 * Static executables all want to share the same addresses
62 * in memory because only a few addresses are reliably present on
63 * a machine, and implementing general relocation is hard.
64 *
65 * The solution:
Stefan Reinauerf64893a2009-07-23 22:03:14 +000066 * - Allocate a buffer the size of the coreboot image plus additional
67 * required space.
68 * - Anything that would overwrite coreboot copy into the lower part of
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000069 * the buffer.
Stefan Reinauerf64893a2009-07-23 22:03:14 +000070 * - After loading an ELF image copy coreboot to the top of the buffer.
Ronald G. Minnichae631262009-04-01 10:48:39 +000071 * - Then jump to the loaded image.
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000072 *
Ronald G. Minnichae631262009-04-01 10:48:39 +000073 * Benefits:
74 * - Nearly arbitrary standalone executables can be loaded.
75 * - Coreboot is preserved, so it can be returned to.
76 * - The implementation is still relatively simple,
Zheng Baoba49fb72009-11-01 09:18:23 +000077 * and much simpler than the general case implemented in kexec.
Ronald G. Minnichae631262009-04-01 10:48:39 +000078 */
79
Patrick Georgi5eceb322009-05-13 16:27:25 +000080static unsigned long bounce_size, bounce_buffer;
81
Aaron Durbin8e4a3552013-02-08 17:28:04 -060082#if CONFIG_RELOCATABLE_RAMSTAGE
83static void get_bounce_buffer(struct lb_memory *mem, unsigned long req_size)
84{
85 /* When the ramstage is relocatable there is no need for a bounce
86 * buffer. All payloads should not overlap the ramstage.
87 */
88 bounce_buffer = ~0UL;
89 bounce_size = 0;
90}
91#else
Myles Watson92027982009-09-23 20:32:21 +000092static void get_bounce_buffer(struct lb_memory *mem, unsigned long req_size)
Ronald G. Minnichae631262009-04-01 10:48:39 +000093{
94 unsigned long lb_size;
95 unsigned long mem_entries;
96 unsigned long buffer;
97 int i;
Stefan Reinauer19436292011-11-07 12:43:03 -080098 lb_size = lb_end - lb_start;
99 /* Plus coreboot size so I have somewhere
100 * to place a copy to return to.
101 */
Myles Watson92027982009-09-23 20:32:21 +0000102 lb_size = req_size + lb_size;
Stefan Reinauer19436292011-11-07 12:43:03 -0800103 mem_entries = (mem->size - sizeof(*mem)) / sizeof(mem->map[0]);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000104 buffer = 0;
105 for(i = 0; i < mem_entries; i++) {
106 unsigned long mstart, mend;
107 unsigned long msize;
108 unsigned long tbuffer;
109 if (mem->map[i].type != LB_MEM_RAM)
110 continue;
111 if (unpack_lb64(mem->map[i].start) > MAX_ADDR)
112 continue;
113 if (unpack_lb64(mem->map[i].size) < lb_size)
114 continue;
115 mstart = unpack_lb64(mem->map[i].start);
116 msize = MAX_ADDR - mstart +1;
117 if (msize > unpack_lb64(mem->map[i].size))
118 msize = unpack_lb64(mem->map[i].size);
119 mend = mstart + msize;
120 tbuffer = mend - lb_size;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000121 if (tbuffer < buffer)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000122 continue;
123 buffer = tbuffer;
124 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000125 bounce_buffer = buffer;
Myles Watson92027982009-09-23 20:32:21 +0000126 bounce_size = req_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000127}
Aaron Durbin8e4a3552013-02-08 17:28:04 -0600128#endif /* CONFIG_RELOCATABLE_RAMSTAGE */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000129
130static int valid_area(struct lb_memory *mem, unsigned long buffer,
131 unsigned long start, unsigned long len)
132{
133 /* Check through all of the memory segments and ensure
134 * the segment that was passed in is completely contained
135 * in RAM.
136 */
137 int i;
138 unsigned long end = start + len;
Stefan Reinauer19436292011-11-07 12:43:03 -0800139 unsigned long mem_entries = (mem->size - sizeof(*mem)) /
140 sizeof(mem->map[0]);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000141
142 /* See if I conflict with the bounce buffer */
143 if (end >= buffer) {
144 return 0;
145 }
146
147 /* Walk through the table of valid memory ranges and see if I
148 * have a match.
149 */
150 for(i = 0; i < mem_entries; i++) {
151 uint64_t mstart, mend;
152 uint32_t mtype;
153 mtype = mem->map[i].type;
154 mstart = unpack_lb64(mem->map[i].start);
155 mend = mstart + unpack_lb64(mem->map[i].size);
Stefan Reinauerc6b8b7d2011-11-07 12:56:12 -0800156 if ((mtype == LB_MEM_RAM) && (start >= mstart) && (end < mend)) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000157 break;
158 }
Stefan Reinauerc6b8b7d2011-11-07 12:56:12 -0800159 if ((mtype == LB_MEM_TABLE) && (start >= mstart) && (end < mend)) {
Stefan Reinauerd6b4f1c2010-09-23 18:29:40 +0000160 printk(BIOS_ERR, "Payload is overwriting coreboot tables.\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000161 break;
162 }
163 }
164 if (i == mem_entries) {
Stefan Reinauer9ec8ed82011-10-18 15:11:04 -0700165 if (start < (1024*1024) && end <=(1024*1024)) {
166 printk(BIOS_DEBUG, "Payload (probably SeaBIOS) loaded"
167 " into a reserved area in the lower 1MB\n");
168 return 1;
169 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000170 printk(BIOS_ERR, "No matching ram area found for range:\n");
171 printk(BIOS_ERR, " [0x%016lx, 0x%016lx)\n", start, end);
172 printk(BIOS_ERR, "Ram areas\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000173 for(i = 0; i < mem_entries; i++) {
174 uint64_t mstart, mend;
175 uint32_t mtype;
176 mtype = mem->map[i].type;
177 mstart = unpack_lb64(mem->map[i].start);
178 mend = mstart + unpack_lb64(mem->map[i].size);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000179 printk(BIOS_ERR, " [0x%016lx, 0x%016lx) %s\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000180 (unsigned long)mstart,
181 (unsigned long)mend,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000182 (mtype == LB_MEM_RAM)?"RAM":"Reserved");
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000183
Ronald G. Minnichae631262009-04-01 10:48:39 +0000184 }
185 return 0;
186 }
187 return 1;
188}
189
Patrick Georgi5eceb322009-05-13 16:27:25 +0000190
191static int overlaps_coreboot(struct segment *seg)
192{
193 unsigned long start, end;
194 start = seg->s_dstaddr;
195 end = start + seg->s_memsz;
196 return !((end <= lb_start) || (start >= lb_end));
197}
198
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000199static int relocate_segment(unsigned long buffer, struct segment *seg)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000200{
201 /* Modify all segments that want to load onto coreboot
202 * to load onto the bounce buffer instead.
203 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000204 /* ret: 1 : A new segment is inserted before the seg.
Stefan Reinauer19436292011-11-07 12:43:03 -0800205 * 0 : A new segment is inserted after the seg, or no new one.
206 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000207 unsigned long start, middle, end, ret = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000208
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000209 printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000210 lb_start, lb_end);
211
Patrick Georgi5eceb322009-05-13 16:27:25 +0000212 /* I don't conflict with coreboot so get out of here */
213 if (!overlaps_coreboot(seg))
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000214 return 0;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000215
Ronald G. Minnichae631262009-04-01 10:48:39 +0000216 start = seg->s_dstaddr;
217 middle = start + seg->s_filesz;
218 end = start + seg->s_memsz;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000219
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000220 printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000221 start, middle, end);
222
Patrick Georgi369bc782009-04-25 07:32:24 +0000223 if (seg->compression == CBFS_COMPRESS_NONE) {
224 /* Slice off a piece at the beginning
225 * that doesn't conflict with coreboot.
226 */
227 if (start < lb_start) {
228 struct segment *new;
229 unsigned long len = lb_start - start;
230 new = malloc(sizeof(*new));
231 *new = *seg;
232 new->s_memsz = len;
233 seg->s_memsz -= len;
234 seg->s_dstaddr += len;
235 seg->s_srcaddr += len;
236 if (seg->s_filesz > len) {
237 new->s_filesz = len;
238 seg->s_filesz -= len;
239 } else {
240 seg->s_filesz = 0;
241 }
242
243 /* Order by stream offset */
244 new->next = seg;
245 new->prev = seg->prev;
246 seg->prev->next = new;
247 seg->prev = new;
Patrick Georgi369bc782009-04-25 07:32:24 +0000248
249 /* compute the new value of start */
250 start = seg->s_dstaddr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000251
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000252 printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000253 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000254 new->s_dstaddr + new->s_filesz,
255 new->s_dstaddr + new->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000256
257 ret = 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000258 }
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000259
260 /* Slice off a piece at the end
261 * that doesn't conflict with coreboot
Patrick Georgi369bc782009-04-25 07:32:24 +0000262 */
263 if (end > lb_end) {
264 unsigned long len = lb_end - start;
265 struct segment *new;
266 new = malloc(sizeof(*new));
267 *new = *seg;
268 seg->s_memsz = len;
269 new->s_memsz -= len;
270 new->s_dstaddr += len;
271 new->s_srcaddr += len;
272 if (seg->s_filesz > len) {
273 seg->s_filesz = len;
274 new->s_filesz -= len;
275 } else {
276 new->s_filesz = 0;
277 }
278 /* Order by stream offset */
279 new->next = seg->next;
280 new->prev = seg;
281 seg->next->prev = new;
282 seg->next = new;
Patrick Georgi369bc782009-04-25 07:32:24 +0000283
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000284 printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000285 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000286 new->s_dstaddr + new->s_filesz,
287 new->s_dstaddr + new->s_memsz);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000288 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000289 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000290
Ronald G. Minnichae631262009-04-01 10:48:39 +0000291 /* Now retarget this segment onto the bounce buffer */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000292 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
Ronald G. Minnichae631262009-04-01 10:48:39 +0000293 * so you will make the dstaddr be this buffer, and it will get copied
294 * later to where coreboot lives.
295 */
296 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
297
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000298 printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000299 seg->s_dstaddr,
300 seg->s_dstaddr + seg->s_filesz,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000301 seg->s_dstaddr + seg->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000302
303 return ret;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000304}
305
306
307static int build_self_segment_list(
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000308 struct segment *head,
Patrick Georgi5eceb322009-05-13 16:27:25 +0000309 struct lb_memory *mem,
Peter Stuge483b7bb2009-04-14 07:40:01 +0000310 struct cbfs_payload *payload, u32 *entry)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000311{
312 struct segment *new;
313 struct segment *ptr;
Peter Stuge483b7bb2009-04-14 07:40:01 +0000314 struct cbfs_payload_segment *segment, *first_segment;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000315 memset(head, 0, sizeof(*head));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000316 head->next = head->prev = head;
317 first_segment = segment = &payload->segments;
318
319 while(1) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000320 printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000321 switch(segment->type) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000322 case PAYLOAD_SEGMENT_PARAMS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000323 printk(BIOS_DEBUG, " parameter section (skipped)\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000324 segment++;
325 continue;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000326
Ronald G. Minnichae631262009-04-01 10:48:39 +0000327 case PAYLOAD_SEGMENT_CODE:
328 case PAYLOAD_SEGMENT_DATA:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000329 printk(BIOS_DEBUG, " %s (compression=%x)\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000330 segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
331 ntohl(segment->compression));
332 new = malloc(sizeof(*new));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700333 new->s_dstaddr = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000334 new->s_memsz = ntohl(segment->mem_len);
335 new->compression = ntohl(segment->compression);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000336
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700337 new->s_srcaddr = (u32) ((unsigned char *)first_segment)
338 + ntohl(segment->offset);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000339 new->s_filesz = ntohl(segment->len);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000340 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 +0000341 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
342 /* Clean up the values */
343 if (new->s_filesz > new->s_memsz) {
344 new->s_filesz = new->s_memsz;
345 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000346 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 +0000347 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
348 break;
349
Ronald G. Minnichae631262009-04-01 10:48:39 +0000350 case PAYLOAD_SEGMENT_BSS:
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700351 printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
352 (intptr_t)ntohll(segment->load_addr),
353 ntohl(segment->mem_len));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000354 new = malloc(sizeof(*new));
355 new->s_filesz = 0;
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700356 new->s_dstaddr = ntohll(segment->load_addr);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000357 new->s_memsz = ntohl(segment->mem_len);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000358 break;
359
360 case PAYLOAD_SEGMENT_ENTRY:
Hung-Te Linfdfd89f2013-02-27 16:38:38 +0800361 printk(BIOS_DEBUG, " Entry Point 0x%p\n",
362 (void *)(intptr_t)ntohll(segment->load_addr));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700363 *entry = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000364 /* Per definition, a payload always has the entry point
365 * as last segment. Thus, we use the occurence of the
366 * entry point as break condition for the loop.
367 * Can we actually just look at the number of section?
368 */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000369 return 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000370
371 default:
372 /* We found something that we don't know about. Throw
373 * hands into the sky and run away!
374 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000375 printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000376 return -1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000377 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000378
Stefan Reinauerc44de492012-01-11 14:07:39 -0800379 /* We have found another CODE, DATA or BSS segment */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000380 segment++;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000381
Stefan Reinauerc44de492012-01-11 14:07:39 -0800382 /* Find place where to insert our segment */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000383 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700384 if (new->s_srcaddr < ntohll(segment->load_addr))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000385 break;
386 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000387
Ronald G. Minnichae631262009-04-01 10:48:39 +0000388 /* Order by stream offset */
389 new->next = ptr;
390 new->prev = ptr->prev;
391 ptr->prev->next = new;
392 ptr->prev = new;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000393 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000394
Ronald G. Minnichae631262009-04-01 10:48:39 +0000395 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000396}
397
398static int load_self_segments(
Patrick Georgi5eceb322009-05-13 16:27:25 +0000399 struct segment *head,
400 struct lb_memory *mem,
401 struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000402{
Ronald G. Minnichae631262009-04-01 10:48:39 +0000403 struct segment *ptr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000404
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000405 unsigned long bounce_high = lb_end;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000406 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerc44de492012-01-11 14:07:39 -0800407 if (!overlaps_coreboot(ptr))
408 continue;
Aaron Durbin8e4a3552013-02-08 17:28:04 -0600409#if CONFIG_RELOCATABLE_RAMSTAGE
410 /* payloads are required to not overlap ramstage. */
411 return 0;
412#else
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000413 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
414 bounce_high = ptr->s_dstaddr + ptr->s_memsz;
Aaron Durbin8e4a3552013-02-08 17:28:04 -0600415#endif
Patrick Georgi5eceb322009-05-13 16:27:25 +0000416 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000417 get_bounce_buffer(mem, bounce_high - lb_start);
Patrick Georgi5eceb322009-05-13 16:27:25 +0000418 if (!bounce_buffer) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000419 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
Patrick Georgi5eceb322009-05-13 16:27:25 +0000420 return 0;
421 }
422 for(ptr = head->next; ptr != head; ptr = ptr->next) {
423 /* Verify the memory addresses in the segment are valid */
424 if (!valid_area(mem, bounce_buffer, ptr->s_dstaddr, ptr->s_memsz))
425 return 0;
426 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000427 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000428 unsigned char *dest, *src;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000429 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000430 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000431
Patrick Georgi5eceb322009-05-13 16:27:25 +0000432 /* Modify the segment to load onto the bounce_buffer if necessary.
433 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000434 if (relocate_segment(bounce_buffer, ptr)) {
435 ptr = (ptr->prev)->prev;
436 continue;
437 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000438
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000439 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Patrick Georgi5eceb322009-05-13 16:27:25 +0000440 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
441
Ronald G. Minnichae631262009-04-01 10:48:39 +0000442 /* Compute the boundaries of the segment */
443 dest = (unsigned char *)(ptr->s_dstaddr);
Myles Watsonfa12b672009-04-30 22:45:41 +0000444 src = (unsigned char *)(ptr->s_srcaddr);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000445
Ronald G. Minnichae631262009-04-01 10:48:39 +0000446 /* Copy data from the initial buffer */
447 if (ptr->s_filesz) {
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000448 unsigned char *middle, *end;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000449 size_t len;
450 len = ptr->s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +0000451 switch(ptr->compression) {
Patrick Georgi369bc782009-04-25 07:32:24 +0000452 case CBFS_COMPRESS_LZMA: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000453 printk(BIOS_DEBUG, "using LZMA\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000454 len = ulzma(src, dest);
Myles Watson94de72b2010-06-01 15:19:25 +0000455 if (!len) /* Decompression Error. */
456 return 0;
Patrick Georgi369bc782009-04-25 07:32:24 +0000457 break;
458 }
Stefan Reinauer3e4fb9d2011-04-21 21:26:58 +0000459#if CONFIG_COMPRESSED_PAYLOAD_NRV2B
Patrick Georgi369bc782009-04-25 07:32:24 +0000460 case CBFS_COMPRESS_NRV2B: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000461 printk(BIOS_DEBUG, "using NRV2B\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000462 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
463 unsigned long tmp;
464 len = unrv2b(src, dest, &tmp);
465 break;
466 }
467#endif
468 case CBFS_COMPRESS_NONE: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000469 printk(BIOS_DEBUG, "it's not compressed!\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000470 memcpy(dest, src, len);
471 break;
472 }
473 default:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000474 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression);
Patrick Georgi369bc782009-04-25 07:32:24 +0000475 return -1;
476 }
477 end = dest + ptr->s_memsz;
478 middle = dest + len;
Myles Watson94de72b2010-06-01 15:19:25 +0000479 printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
Patrick Georgi369bc782009-04-25 07:32:24 +0000480 (unsigned long)dest,
481 (unsigned long)middle,
482 (unsigned long)end,
483 (unsigned long)src);
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000484
485 /* Zero the extra bytes between middle & end */
486 if (middle < end) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000487 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000488 (unsigned long)middle, (unsigned long)(end - middle));
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000489
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000490 /* Zero the extra bytes */
491 memset(middle, 0, end - middle);
492 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000493 /* Copy the data that's outside the area that shadows coreboot_ram */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000494 printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000495 if ((unsigned long)end > bounce_buffer) {
496 if ((unsigned long)dest < bounce_buffer) {
Myles Watson7943fe62009-10-30 02:08:07 +0000497 unsigned char *from = dest;
498 unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000499 unsigned long amount = bounce_buffer-(unsigned long)dest;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000500 printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000501 memcpy(to, from, amount);
502 }
503 if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
504 unsigned long from = bounce_buffer + (lb_end - lb_start);
505 unsigned long to = lb_end;
Myles Watson7943fe62009-10-30 02:08:07 +0000506 unsigned long amount = (unsigned long)end - from;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000507 printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
Myles Watson7943fe62009-10-30 02:08:07 +0000508 memcpy((char*)to, (char*)from, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000509 }
510 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000511 }
512 }
513 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000514}
515
Ronald G. Minnich9764d4c2012-06-12 16:29:32 -0700516int selfboot(struct lb_memory *mem, struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000517{
Myles Watsonfa12b672009-04-30 22:45:41 +0000518 u32 entry=0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000519 struct segment head;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000520
521 /* Preprocess the self segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000522 if (!build_self_segment_list(&head, mem, payload, &entry))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000523 goto out;
524
525 /* Load the segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000526 if (!load_self_segments(&head, mem, payload))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000527 goto out;
528
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000529 printk(BIOS_SPEW, "Loaded segments\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000530
531 /* Reset to booting from this image as late as possible */
532 boot_successful();
533
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000534 printk(BIOS_DEBUG, "Jumping to boot code at %x\n", entry);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000535 post_code(POST_ENTER_ELF_BOOT);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000536
Duncan Lauriecde78012011-10-19 15:32:39 -0700537#if CONFIG_COLLECT_TIMESTAMPS
538 timestamp_add_now(TS_SELFBOOT_JUMP);
539#endif
Stefan Reinauerd37ab452012-12-18 16:23:28 -0800540#if CONFIG_COVERAGE
541 coverage_exit();
542#endif
Duncan Lauriecde78012011-10-19 15:32:39 -0700543
Aaron Durbinebf142a2013-03-29 16:23:23 -0500544 /* Tear down the caching of the ROM. */
545 if (disable_cache_rom)
546 disable_cache_rom();
547
Stefan Reinauer75dbc382012-10-15 15:19:43 -0700548 /* Before we go off to run the payload, see if
549 * we stayed within our bounds.
550 */
551 checkstack(_estack, 0);
552
Ronald G. Minnichae631262009-04-01 10:48:39 +0000553 /* Jump to kernel */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000554 jmp_to_elf_entry((void*)entry, bounce_buffer, bounce_size);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000555 return 1;
556
Patrick Georgic5fc7db2012-03-07 15:55:47 +0100557out:
Ronald G. Minnichae631262009-04-01 10:48:39 +0000558 return 0;
559}