blob: 2b69ac45319a5d25fa8cadf4ed75793f1c7ee6b1 [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
Ronald G. Minnichae631262009-04-01 10:48:39 +000036
Stefan Reinauer19436292011-11-07 12:43:03 -080037/* Maximum physical address we can use for the coreboot bounce buffer. */
Ronald G. Minnichae631262009-04-01 10:48:39 +000038#ifndef MAX_ADDR
39#define MAX_ADDR -1UL
40#endif
41
Stefan Reinauer19436292011-11-07 12:43:03 -080042/* from coreboot_ram.ld: */
Ronald G. Minnichae631262009-04-01 10:48:39 +000043extern unsigned char _ram_seg;
44extern unsigned char _eram_seg;
45
Stefan Reinauer19436292011-11-07 12:43:03 -080046static const unsigned long lb_start = (unsigned long)&_ram_seg;
47static const unsigned long lb_end = (unsigned long)&_eram_seg;
48
Ronald G. Minnichae631262009-04-01 10:48:39 +000049struct segment {
50 struct segment *next;
51 struct segment *prev;
Ronald G. Minnichae631262009-04-01 10:48:39 +000052 unsigned long s_dstaddr;
53 unsigned long s_srcaddr;
54 unsigned long s_memsz;
55 unsigned long s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +000056 int compression;
Ronald G. Minnichae631262009-04-01 10:48:39 +000057};
58
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000059/* The problem:
Ronald G. Minnichae631262009-04-01 10:48:39 +000060 * Static executables all want to share the same addresses
61 * in memory because only a few addresses are reliably present on
62 * a machine, and implementing general relocation is hard.
63 *
64 * The solution:
Stefan Reinauerf64893a2009-07-23 22:03:14 +000065 * - Allocate a buffer the size of the coreboot image plus additional
66 * required space.
67 * - Anything that would overwrite coreboot copy into the lower part of
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000068 * the buffer.
Stefan Reinauerf64893a2009-07-23 22:03:14 +000069 * - After loading an ELF image copy coreboot to the top of the buffer.
Ronald G. Minnichae631262009-04-01 10:48:39 +000070 * - Then jump to the loaded image.
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000071 *
Ronald G. Minnichae631262009-04-01 10:48:39 +000072 * Benefits:
73 * - Nearly arbitrary standalone executables can be loaded.
74 * - Coreboot is preserved, so it can be returned to.
75 * - The implementation is still relatively simple,
Zheng Baoba49fb72009-11-01 09:18:23 +000076 * and much simpler than the general case implemented in kexec.
Ronald G. Minnichae631262009-04-01 10:48:39 +000077 */
78
Patrick Georgi5eceb322009-05-13 16:27:25 +000079static unsigned long bounce_size, bounce_buffer;
80
Aaron Durbin8e4a3552013-02-08 17:28:04 -060081#if CONFIG_RELOCATABLE_RAMSTAGE
82static void get_bounce_buffer(struct lb_memory *mem, unsigned long req_size)
83{
84 /* When the ramstage is relocatable there is no need for a bounce
85 * buffer. All payloads should not overlap the ramstage.
86 */
87 bounce_buffer = ~0UL;
88 bounce_size = 0;
89}
90#else
Myles Watson92027982009-09-23 20:32:21 +000091static void get_bounce_buffer(struct lb_memory *mem, unsigned long req_size)
Ronald G. Minnichae631262009-04-01 10:48:39 +000092{
93 unsigned long lb_size;
94 unsigned long mem_entries;
95 unsigned long buffer;
96 int i;
Stefan Reinauer19436292011-11-07 12:43:03 -080097 lb_size = lb_end - lb_start;
98 /* Plus coreboot size so I have somewhere
99 * to place a copy to return to.
100 */
Myles Watson92027982009-09-23 20:32:21 +0000101 lb_size = req_size + lb_size;
Stefan Reinauer19436292011-11-07 12:43:03 -0800102 mem_entries = (mem->size - sizeof(*mem)) / sizeof(mem->map[0]);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000103 buffer = 0;
104 for(i = 0; i < mem_entries; i++) {
105 unsigned long mstart, mend;
106 unsigned long msize;
107 unsigned long tbuffer;
108 if (mem->map[i].type != LB_MEM_RAM)
109 continue;
110 if (unpack_lb64(mem->map[i].start) > MAX_ADDR)
111 continue;
112 if (unpack_lb64(mem->map[i].size) < lb_size)
113 continue;
114 mstart = unpack_lb64(mem->map[i].start);
115 msize = MAX_ADDR - mstart +1;
116 if (msize > unpack_lb64(mem->map[i].size))
117 msize = unpack_lb64(mem->map[i].size);
118 mend = mstart + msize;
119 tbuffer = mend - lb_size;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000120 if (tbuffer < buffer)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000121 continue;
122 buffer = tbuffer;
123 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000124 bounce_buffer = buffer;
Myles Watson92027982009-09-23 20:32:21 +0000125 bounce_size = req_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000126}
Aaron Durbin8e4a3552013-02-08 17:28:04 -0600127#endif /* CONFIG_RELOCATABLE_RAMSTAGE */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000128
129static int valid_area(struct lb_memory *mem, unsigned long buffer,
130 unsigned long start, unsigned long len)
131{
132 /* Check through all of the memory segments and ensure
133 * the segment that was passed in is completely contained
134 * in RAM.
135 */
136 int i;
137 unsigned long end = start + len;
Stefan Reinauer19436292011-11-07 12:43:03 -0800138 unsigned long mem_entries = (mem->size - sizeof(*mem)) /
139 sizeof(mem->map[0]);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000140
141 /* See if I conflict with the bounce buffer */
142 if (end >= buffer) {
143 return 0;
144 }
145
146 /* Walk through the table of valid memory ranges and see if I
147 * have a match.
148 */
149 for(i = 0; i < mem_entries; i++) {
150 uint64_t mstart, mend;
151 uint32_t mtype;
152 mtype = mem->map[i].type;
153 mstart = unpack_lb64(mem->map[i].start);
154 mend = mstart + unpack_lb64(mem->map[i].size);
Stefan Reinauerc6b8b7d2011-11-07 12:56:12 -0800155 if ((mtype == LB_MEM_RAM) && (start >= mstart) && (end < mend)) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000156 break;
157 }
Stefan Reinauerc6b8b7d2011-11-07 12:56:12 -0800158 if ((mtype == LB_MEM_TABLE) && (start >= mstart) && (end < mend)) {
Stefan Reinauerd6b4f1c2010-09-23 18:29:40 +0000159 printk(BIOS_ERR, "Payload is overwriting coreboot tables.\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000160 break;
161 }
162 }
163 if (i == mem_entries) {
Stefan Reinauer9ec8ed82011-10-18 15:11:04 -0700164 if (start < (1024*1024) && end <=(1024*1024)) {
165 printk(BIOS_DEBUG, "Payload (probably SeaBIOS) loaded"
166 " into a reserved area in the lower 1MB\n");
167 return 1;
168 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000169 printk(BIOS_ERR, "No matching ram area found for range:\n");
170 printk(BIOS_ERR, " [0x%016lx, 0x%016lx)\n", start, end);
171 printk(BIOS_ERR, "Ram areas\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000172 for(i = 0; i < mem_entries; i++) {
173 uint64_t mstart, mend;
174 uint32_t mtype;
175 mtype = mem->map[i].type;
176 mstart = unpack_lb64(mem->map[i].start);
177 mend = mstart + unpack_lb64(mem->map[i].size);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000178 printk(BIOS_ERR, " [0x%016lx, 0x%016lx) %s\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000179 (unsigned long)mstart,
180 (unsigned long)mend,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000181 (mtype == LB_MEM_RAM)?"RAM":"Reserved");
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000182
Ronald G. Minnichae631262009-04-01 10:48:39 +0000183 }
184 return 0;
185 }
186 return 1;
187}
188
Patrick Georgi5eceb322009-05-13 16:27:25 +0000189
190static int overlaps_coreboot(struct segment *seg)
191{
192 unsigned long start, end;
193 start = seg->s_dstaddr;
194 end = start + seg->s_memsz;
195 return !((end <= lb_start) || (start >= lb_end));
196}
197
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000198static int relocate_segment(unsigned long buffer, struct segment *seg)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000199{
200 /* Modify all segments that want to load onto coreboot
201 * to load onto the bounce buffer instead.
202 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000203 /* ret: 1 : A new segment is inserted before the seg.
Stefan Reinauer19436292011-11-07 12:43:03 -0800204 * 0 : A new segment is inserted after the seg, or no new one.
205 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000206 unsigned long start, middle, end, ret = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000207
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000208 printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000209 lb_start, lb_end);
210
Patrick Georgi5eceb322009-05-13 16:27:25 +0000211 /* I don't conflict with coreboot so get out of here */
212 if (!overlaps_coreboot(seg))
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000213 return 0;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000214
Ronald G. Minnichae631262009-04-01 10:48:39 +0000215 start = seg->s_dstaddr;
216 middle = start + seg->s_filesz;
217 end = start + seg->s_memsz;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000218
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000219 printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000220 start, middle, end);
221
Patrick Georgi369bc782009-04-25 07:32:24 +0000222 if (seg->compression == CBFS_COMPRESS_NONE) {
223 /* Slice off a piece at the beginning
224 * that doesn't conflict with coreboot.
225 */
226 if (start < lb_start) {
227 struct segment *new;
228 unsigned long len = lb_start - start;
229 new = malloc(sizeof(*new));
230 *new = *seg;
231 new->s_memsz = len;
232 seg->s_memsz -= len;
233 seg->s_dstaddr += len;
234 seg->s_srcaddr += len;
235 if (seg->s_filesz > len) {
236 new->s_filesz = len;
237 seg->s_filesz -= len;
238 } else {
239 seg->s_filesz = 0;
240 }
241
242 /* Order by stream offset */
243 new->next = seg;
244 new->prev = seg->prev;
245 seg->prev->next = new;
246 seg->prev = new;
Patrick Georgi369bc782009-04-25 07:32:24 +0000247
248 /* compute the new value of start */
249 start = seg->s_dstaddr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000250
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000251 printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000252 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000253 new->s_dstaddr + new->s_filesz,
254 new->s_dstaddr + new->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000255
256 ret = 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000257 }
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000258
259 /* Slice off a piece at the end
260 * that doesn't conflict with coreboot
Patrick Georgi369bc782009-04-25 07:32:24 +0000261 */
262 if (end > lb_end) {
263 unsigned long len = lb_end - start;
264 struct segment *new;
265 new = malloc(sizeof(*new));
266 *new = *seg;
267 seg->s_memsz = len;
268 new->s_memsz -= len;
269 new->s_dstaddr += len;
270 new->s_srcaddr += len;
271 if (seg->s_filesz > len) {
272 seg->s_filesz = len;
273 new->s_filesz -= len;
274 } else {
275 new->s_filesz = 0;
276 }
277 /* Order by stream offset */
278 new->next = seg->next;
279 new->prev = seg;
280 seg->next->prev = new;
281 seg->next = new;
Patrick Georgi369bc782009-04-25 07:32:24 +0000282
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000283 printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000284 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000285 new->s_dstaddr + new->s_filesz,
286 new->s_dstaddr + new->s_memsz);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000287 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000288 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000289
Ronald G. Minnichae631262009-04-01 10:48:39 +0000290 /* Now retarget this segment onto the bounce buffer */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000291 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
Ronald G. Minnichae631262009-04-01 10:48:39 +0000292 * so you will make the dstaddr be this buffer, and it will get copied
293 * later to where coreboot lives.
294 */
295 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
296
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000297 printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000298 seg->s_dstaddr,
299 seg->s_dstaddr + seg->s_filesz,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000300 seg->s_dstaddr + seg->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000301
302 return ret;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000303}
304
305
306static int build_self_segment_list(
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000307 struct segment *head,
Patrick Georgi5eceb322009-05-13 16:27:25 +0000308 struct lb_memory *mem,
Peter Stuge483b7bb2009-04-14 07:40:01 +0000309 struct cbfs_payload *payload, u32 *entry)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000310{
311 struct segment *new;
312 struct segment *ptr;
Peter Stuge483b7bb2009-04-14 07:40:01 +0000313 struct cbfs_payload_segment *segment, *first_segment;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000314 memset(head, 0, sizeof(*head));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000315 head->next = head->prev = head;
316 first_segment = segment = &payload->segments;
317
318 while(1) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000319 printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000320 switch(segment->type) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000321 case PAYLOAD_SEGMENT_PARAMS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000322 printk(BIOS_DEBUG, " parameter section (skipped)\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000323 segment++;
324 continue;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000325
Ronald G. Minnichae631262009-04-01 10:48:39 +0000326 case PAYLOAD_SEGMENT_CODE:
327 case PAYLOAD_SEGMENT_DATA:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000328 printk(BIOS_DEBUG, " %s (compression=%x)\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000329 segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
330 ntohl(segment->compression));
331 new = malloc(sizeof(*new));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700332 new->s_dstaddr = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000333 new->s_memsz = ntohl(segment->mem_len);
334 new->compression = ntohl(segment->compression);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000335
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700336 new->s_srcaddr = (u32) ((unsigned char *)first_segment)
337 + ntohl(segment->offset);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000338 new->s_filesz = ntohl(segment->len);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000339 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 +0000340 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
341 /* Clean up the values */
342 if (new->s_filesz > new->s_memsz) {
343 new->s_filesz = new->s_memsz;
344 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000345 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 +0000346 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
347 break;
348
Ronald G. Minnichae631262009-04-01 10:48:39 +0000349 case PAYLOAD_SEGMENT_BSS:
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700350 printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
351 (intptr_t)ntohll(segment->load_addr),
352 ntohl(segment->mem_len));
Ronald G. Minnichae631262009-04-01 10:48:39 +0000353 new = malloc(sizeof(*new));
354 new->s_filesz = 0;
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700355 new->s_dstaddr = ntohll(segment->load_addr);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000356 new->s_memsz = ntohl(segment->mem_len);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000357 break;
358
359 case PAYLOAD_SEGMENT_ENTRY:
Hung-Te Linfdfd89f2013-02-27 16:38:38 +0800360 printk(BIOS_DEBUG, " Entry Point 0x%p\n",
361 (void *)(intptr_t)ntohll(segment->load_addr));
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700362 *entry = ntohll(segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000363 /* Per definition, a payload always has the entry point
Martin Rothcbf2bd72013-07-09 21:51:14 -0600364 * as last segment. Thus, we use the occurrence of the
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000365 * entry point as break condition for the loop.
366 * Can we actually just look at the number of section?
367 */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000368 return 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000369
370 default:
371 /* We found something that we don't know about. Throw
372 * hands into the sky and run away!
373 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000374 printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000375 return -1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000376 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000377
Stefan Reinauerc44de492012-01-11 14:07:39 -0800378 /* We have found another CODE, DATA or BSS segment */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000379 segment++;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000380
Stefan Reinauerc44de492012-01-11 14:07:39 -0800381 /* Find place where to insert our segment */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000382 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauer02e75b22011-10-17 09:51:15 -0700383 if (new->s_srcaddr < ntohll(segment->load_addr))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000384 break;
385 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000386
Ronald G. Minnichae631262009-04-01 10:48:39 +0000387 /* Order by stream offset */
388 new->next = ptr;
389 new->prev = ptr->prev;
390 ptr->prev->next = new;
391 ptr->prev = new;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000392 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000393
Ronald G. Minnichae631262009-04-01 10:48:39 +0000394 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000395}
396
397static int load_self_segments(
Patrick Georgi5eceb322009-05-13 16:27:25 +0000398 struct segment *head,
399 struct lb_memory *mem,
400 struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000401{
Ronald G. Minnichae631262009-04-01 10:48:39 +0000402 struct segment *ptr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000403
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000404 unsigned long bounce_high = lb_end;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000405 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerc44de492012-01-11 14:07:39 -0800406 if (!overlaps_coreboot(ptr))
407 continue;
Aaron Durbin8e4a3552013-02-08 17:28:04 -0600408#if CONFIG_RELOCATABLE_RAMSTAGE
409 /* payloads are required to not overlap ramstage. */
410 return 0;
411#else
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000412 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
413 bounce_high = ptr->s_dstaddr + ptr->s_memsz;
Aaron Durbin8e4a3552013-02-08 17:28:04 -0600414#endif
Patrick Georgi5eceb322009-05-13 16:27:25 +0000415 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000416 get_bounce_buffer(mem, bounce_high - lb_start);
Patrick Georgi5eceb322009-05-13 16:27:25 +0000417 if (!bounce_buffer) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000418 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
Patrick Georgi5eceb322009-05-13 16:27:25 +0000419 return 0;
420 }
421 for(ptr = head->next; ptr != head; ptr = ptr->next) {
422 /* Verify the memory addresses in the segment are valid */
423 if (!valid_area(mem, bounce_buffer, ptr->s_dstaddr, ptr->s_memsz))
424 return 0;
425 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000426 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000427 unsigned char *dest, *src;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000428 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000429 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000430
Patrick Georgi5eceb322009-05-13 16:27:25 +0000431 /* Modify the segment to load onto the bounce_buffer if necessary.
432 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000433 if (relocate_segment(bounce_buffer, ptr)) {
434 ptr = (ptr->prev)->prev;
435 continue;
436 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000437
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000438 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Patrick Georgi5eceb322009-05-13 16:27:25 +0000439 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
440
Ronald G. Minnichae631262009-04-01 10:48:39 +0000441 /* Compute the boundaries of the segment */
442 dest = (unsigned char *)(ptr->s_dstaddr);
Myles Watsonfa12b672009-04-30 22:45:41 +0000443 src = (unsigned char *)(ptr->s_srcaddr);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000444
Ronald G. Minnichae631262009-04-01 10:48:39 +0000445 /* Copy data from the initial buffer */
446 if (ptr->s_filesz) {
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000447 unsigned char *middle, *end;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000448 size_t len;
449 len = ptr->s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +0000450 switch(ptr->compression) {
Patrick Georgi369bc782009-04-25 07:32:24 +0000451 case CBFS_COMPRESS_LZMA: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000452 printk(BIOS_DEBUG, "using LZMA\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000453 len = ulzma(src, dest);
Myles Watson94de72b2010-06-01 15:19:25 +0000454 if (!len) /* Decompression Error. */
455 return 0;
Patrick Georgi369bc782009-04-25 07:32:24 +0000456 break;
457 }
Stefan Reinauer3e4fb9d2011-04-21 21:26:58 +0000458#if CONFIG_COMPRESSED_PAYLOAD_NRV2B
Patrick Georgi369bc782009-04-25 07:32:24 +0000459 case CBFS_COMPRESS_NRV2B: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000460 printk(BIOS_DEBUG, "using NRV2B\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000461 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
462 unsigned long tmp;
463 len = unrv2b(src, dest, &tmp);
464 break;
465 }
466#endif
467 case CBFS_COMPRESS_NONE: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000468 printk(BIOS_DEBUG, "it's not compressed!\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000469 memcpy(dest, src, len);
470 break;
471 }
472 default:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000473 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression);
Patrick Georgi369bc782009-04-25 07:32:24 +0000474 return -1;
475 }
476 end = dest + ptr->s_memsz;
477 middle = dest + len;
Myles Watson94de72b2010-06-01 15:19:25 +0000478 printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
Patrick Georgi369bc782009-04-25 07:32:24 +0000479 (unsigned long)dest,
480 (unsigned long)middle,
481 (unsigned long)end,
482 (unsigned long)src);
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000483
484 /* Zero the extra bytes between middle & end */
485 if (middle < end) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000486 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000487 (unsigned long)middle, (unsigned long)(end - middle));
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000488
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000489 /* Zero the extra bytes */
490 memset(middle, 0, end - middle);
491 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000492 /* Copy the data that's outside the area that shadows coreboot_ram */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000493 printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000494 if ((unsigned long)end > bounce_buffer) {
495 if ((unsigned long)dest < bounce_buffer) {
Myles Watson7943fe62009-10-30 02:08:07 +0000496 unsigned char *from = dest;
497 unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000498 unsigned long amount = bounce_buffer-(unsigned long)dest;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000499 printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000500 memcpy(to, from, amount);
501 }
502 if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
503 unsigned long from = bounce_buffer + (lb_end - lb_start);
504 unsigned long to = lb_end;
Myles Watson7943fe62009-10-30 02:08:07 +0000505 unsigned long amount = (unsigned long)end - from;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000506 printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
Myles Watson7943fe62009-10-30 02:08:07 +0000507 memcpy((char*)to, (char*)from, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000508 }
509 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000510 }
511 }
512 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000513}
514
Aaron Durbin001de1a2013-04-24 22:59:45 -0500515void *selfload(struct lb_memory *mem, struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000516{
Myles Watsonfa12b672009-04-30 22:45:41 +0000517 u32 entry=0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000518 struct segment head;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000519
520 /* Preprocess the self segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000521 if (!build_self_segment_list(&head, mem, payload, &entry))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000522 goto out;
523
524 /* Load the segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000525 if (!load_self_segments(&head, mem, payload))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000526 goto out;
527
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000528 printk(BIOS_SPEW, "Loaded segments\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000529
Aaron Durbin001de1a2013-04-24 22:59:45 -0500530 return (void *)entry;
531
532out:
533 return NULL;
534}
535
536void selfboot(void *entry)
537{
Ronald G. Minnichae631262009-04-01 10:48:39 +0000538 /* Reset to booting from this image as late as possible */
539 boot_successful();
540
Aaron Durbin001de1a2013-04-24 22:59:45 -0500541 printk(BIOS_DEBUG, "Jumping to boot code at %p\n", entry);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000542 post_code(POST_ENTER_ELF_BOOT);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000543
Duncan Lauriecde78012011-10-19 15:32:39 -0700544#if CONFIG_COLLECT_TIMESTAMPS
545 timestamp_add_now(TS_SELFBOOT_JUMP);
546#endif
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 */
Aaron Durbin001de1a2013-04-24 22:59:45 -0500554 jmp_to_elf_entry(entry, bounce_buffer, bounce_size);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000555}