blob: 7ae9f8cd7d5541969d05e3954aee20ddc2614390 [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
Ronald G. Minnichae631262009-04-01 10:48:39 +000034/* Maximum physical address we can use for the coreboot bounce buffer.
35 */
36#ifndef MAX_ADDR
37#define MAX_ADDR -1UL
38#endif
39
40extern unsigned char _ram_seg;
41extern unsigned char _eram_seg;
42
43struct segment {
44 struct segment *next;
45 struct segment *prev;
46 struct segment *phdr_next;
47 struct segment *phdr_prev;
48 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
55struct verify_callback {
56 struct verify_callback *next;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000057 int (*callback)(struct verify_callback *vcb,
Ronald G. Minnichae631262009-04-01 10:48:39 +000058 Elf_ehdr *ehdr, Elf_phdr *phdr, struct segment *head);
59 unsigned long desc_offset;
60 unsigned long desc_addr;
61};
62
63struct ip_checksum_vcb {
64 struct verify_callback data;
65 unsigned short ip_checksum;
66};
67
Myles Watson92027982009-09-23 20:32:21 +000068static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload);
69
Patrick Georgid1185bf2009-05-26 14:00:49 +000070void * cbfs_load_payload(struct lb_memory *lb_mem, const char *name)
71{
Stefan Reinauerf64893a2009-07-23 22:03:14 +000072 struct cbfs_payload *payload;
Patrick Georgid1185bf2009-05-26 14:00:49 +000073
Stefan Reinauerf64893a2009-07-23 22:03:14 +000074 payload = (struct cbfs_payload *)cbfs_find_file(name, CBFS_TYPE_PAYLOAD);
Patrick Georgid1185bf2009-05-26 14:00:49 +000075 if (payload == NULL)
76 return (void *) -1;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000077 printk(BIOS_DEBUG, "Got a payload\n");
Stefan Reinauerf64893a2009-07-23 22:03:14 +000078
Patrick Georgid1185bf2009-05-26 14:00:49 +000079 selfboot(lb_mem, payload);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +000080 printk(BIOS_EMERG, "SELFBOOT RETURNED!\n");
Patrick Georgid1185bf2009-05-26 14:00:49 +000081
82 return (void *) -1;
83}
84
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000085/* The problem:
Ronald G. Minnichae631262009-04-01 10:48:39 +000086 * Static executables all want to share the same addresses
87 * in memory because only a few addresses are reliably present on
88 * a machine, and implementing general relocation is hard.
89 *
90 * The solution:
Stefan Reinauerf64893a2009-07-23 22:03:14 +000091 * - Allocate a buffer the size of the coreboot image plus additional
92 * required space.
93 * - Anything that would overwrite coreboot copy into the lower part of
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000094 * the buffer.
Stefan Reinauerf64893a2009-07-23 22:03:14 +000095 * - After loading an ELF image copy coreboot to the top of the buffer.
Ronald G. Minnichae631262009-04-01 10:48:39 +000096 * - Then jump to the loaded image.
Zheng Baoe6ad7fa2009-11-05 10:02:59 +000097 *
Ronald G. Minnichae631262009-04-01 10:48:39 +000098 * Benefits:
99 * - Nearly arbitrary standalone executables can be loaded.
100 * - Coreboot is preserved, so it can be returned to.
101 * - The implementation is still relatively simple,
Zheng Baoba49fb72009-11-01 09:18:23 +0000102 * and much simpler than the general case implemented in kexec.
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000103 *
Ronald G. Minnichae631262009-04-01 10:48:39 +0000104 */
105
Patrick Georgi5eceb322009-05-13 16:27:25 +0000106static unsigned long bounce_size, bounce_buffer;
107
Myles Watson92027982009-09-23 20:32:21 +0000108static void get_bounce_buffer(struct lb_memory *mem, unsigned long req_size)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000109{
110 unsigned long lb_size;
111 unsigned long mem_entries;
112 unsigned long buffer;
113 int i;
114 lb_size = (unsigned long)(&_eram_seg - &_ram_seg);
115 /* Double coreboot size so I have somewhere to place a copy to return to */
Myles Watson92027982009-09-23 20:32:21 +0000116 lb_size = req_size + lb_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000117 mem_entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
118 buffer = 0;
119 for(i = 0; i < mem_entries; i++) {
120 unsigned long mstart, mend;
121 unsigned long msize;
122 unsigned long tbuffer;
123 if (mem->map[i].type != LB_MEM_RAM)
124 continue;
125 if (unpack_lb64(mem->map[i].start) > MAX_ADDR)
126 continue;
127 if (unpack_lb64(mem->map[i].size) < lb_size)
128 continue;
129 mstart = unpack_lb64(mem->map[i].start);
130 msize = MAX_ADDR - mstart +1;
131 if (msize > unpack_lb64(mem->map[i].size))
132 msize = unpack_lb64(mem->map[i].size);
133 mend = mstart + msize;
134 tbuffer = mend - lb_size;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000135 if (tbuffer < buffer)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000136 continue;
137 buffer = tbuffer;
138 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000139 bounce_buffer = buffer;
Myles Watson92027982009-09-23 20:32:21 +0000140 bounce_size = req_size;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000141}
142
143static int valid_area(struct lb_memory *mem, unsigned long buffer,
144 unsigned long start, unsigned long len)
145{
146 /* Check through all of the memory segments and ensure
147 * the segment that was passed in is completely contained
148 * in RAM.
149 */
150 int i;
151 unsigned long end = start + len;
152 unsigned long mem_entries = (mem->size - sizeof(*mem))/sizeof(mem->map[0]);
153
154 /* See if I conflict with the bounce buffer */
155 if (end >= buffer) {
156 return 0;
157 }
158
159 /* Walk through the table of valid memory ranges and see if I
160 * have a match.
161 */
162 for(i = 0; i < mem_entries; i++) {
163 uint64_t mstart, mend;
164 uint32_t mtype;
165 mtype = mem->map[i].type;
166 mstart = unpack_lb64(mem->map[i].start);
167 mend = mstart + unpack_lb64(mem->map[i].size);
168 if ((mtype == LB_MEM_RAM) && (start < mend) && (end > mstart)) {
169 break;
170 }
171 if ((mtype == LB_MEM_TABLE) && (start < mend) && (end > mstart)) {
Stefan Reinauerd6b4f1c2010-09-23 18:29:40 +0000172 printk(BIOS_ERR, "Payload is overwriting coreboot tables.\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000173 break;
174 }
175 }
176 if (i == mem_entries) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000177 printk(BIOS_ERR, "No matching ram area found for range:\n");
178 printk(BIOS_ERR, " [0x%016lx, 0x%016lx)\n", start, end);
179 printk(BIOS_ERR, "Ram areas\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000180 for(i = 0; i < mem_entries; i++) {
181 uint64_t mstart, mend;
182 uint32_t mtype;
183 mtype = mem->map[i].type;
184 mstart = unpack_lb64(mem->map[i].start);
185 mend = mstart + unpack_lb64(mem->map[i].size);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000186 printk(BIOS_ERR, " [0x%016lx, 0x%016lx) %s\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000187 (unsigned long)mstart,
188 (unsigned long)mend,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000189 (mtype == LB_MEM_RAM)?"RAM":"Reserved");
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000190
Ronald G. Minnichae631262009-04-01 10:48:39 +0000191 }
192 return 0;
193 }
194 return 1;
195}
196
Patrick Georgi5eceb322009-05-13 16:27:25 +0000197static const unsigned long lb_start = (unsigned long)&_ram_seg;
198static const unsigned long lb_end = (unsigned long)&_eram_seg;
199
200static int overlaps_coreboot(struct segment *seg)
201{
202 unsigned long start, end;
203 start = seg->s_dstaddr;
204 end = start + seg->s_memsz;
205 return !((end <= lb_start) || (start >= lb_end));
206}
207
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000208static int relocate_segment(unsigned long buffer, struct segment *seg)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000209{
210 /* Modify all segments that want to load onto coreboot
211 * to load onto the bounce buffer instead.
212 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000213 /* ret: 1 : A new segment is inserted before the seg.
214 * 0 : A new segment is inserted after the seg, or no new one. */
215 unsigned long start, middle, end, ret = 0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000216
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000217 printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000218 lb_start, lb_end);
219
Patrick Georgi5eceb322009-05-13 16:27:25 +0000220 /* I don't conflict with coreboot so get out of here */
221 if (!overlaps_coreboot(seg))
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000222 return 0;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000223
Ronald G. Minnichae631262009-04-01 10:48:39 +0000224 start = seg->s_dstaddr;
225 middle = start + seg->s_filesz;
226 end = start + seg->s_memsz;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000227
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000228 printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000229 start, middle, end);
230
Patrick Georgi369bc782009-04-25 07:32:24 +0000231 if (seg->compression == CBFS_COMPRESS_NONE) {
232 /* Slice off a piece at the beginning
233 * that doesn't conflict with coreboot.
234 */
235 if (start < lb_start) {
236 struct segment *new;
237 unsigned long len = lb_start - start;
238 new = malloc(sizeof(*new));
239 *new = *seg;
240 new->s_memsz = len;
241 seg->s_memsz -= len;
242 seg->s_dstaddr += len;
243 seg->s_srcaddr += len;
244 if (seg->s_filesz > len) {
245 new->s_filesz = len;
246 seg->s_filesz -= len;
247 } else {
248 seg->s_filesz = 0;
249 }
250
251 /* Order by stream offset */
252 new->next = seg;
253 new->prev = seg->prev;
254 seg->prev->next = new;
255 seg->prev = new;
256 /* Order by original program header order */
257 new->phdr_next = seg;
258 new->phdr_prev = seg->phdr_prev;
259 seg->phdr_prev->phdr_next = new;
260 seg->phdr_prev = new;
261
262 /* compute the new value of start */
263 start = seg->s_dstaddr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000264
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000265 printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000266 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000267 new->s_dstaddr + new->s_filesz,
268 new->s_dstaddr + new->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000269
270 ret = 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000271 }
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000272
273 /* Slice off a piece at the end
274 * that doesn't conflict with coreboot
Patrick Georgi369bc782009-04-25 07:32:24 +0000275 */
276 if (end > lb_end) {
277 unsigned long len = lb_end - start;
278 struct segment *new;
279 new = malloc(sizeof(*new));
280 *new = *seg;
281 seg->s_memsz = len;
282 new->s_memsz -= len;
283 new->s_dstaddr += len;
284 new->s_srcaddr += len;
285 if (seg->s_filesz > len) {
286 seg->s_filesz = len;
287 new->s_filesz -= len;
288 } else {
289 new->s_filesz = 0;
290 }
291 /* Order by stream offset */
292 new->next = seg->next;
293 new->prev = seg;
294 seg->next->prev = new;
295 seg->next = new;
296 /* Order by original program header order */
297 new->phdr_next = seg->phdr_next;
298 new->phdr_prev = seg;
299 seg->phdr_next->phdr_prev = new;
300 seg->phdr_next = new;
301
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000302 printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000303 new->s_dstaddr,
Patrick Georgi369bc782009-04-25 07:32:24 +0000304 new->s_dstaddr + new->s_filesz,
305 new->s_dstaddr + new->s_memsz);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000306 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000307 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000308
Ronald G. Minnichae631262009-04-01 10:48:39 +0000309 /* Now retarget this segment onto the bounce buffer */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000310 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
Ronald G. Minnichae631262009-04-01 10:48:39 +0000311 * so you will make the dstaddr be this buffer, and it will get copied
312 * later to where coreboot lives.
313 */
314 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
315
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000316 printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000317 seg->s_dstaddr,
318 seg->s_dstaddr + seg->s_filesz,
Ronald G. Minnichae631262009-04-01 10:48:39 +0000319 seg->s_dstaddr + seg->s_memsz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000320
321 return ret;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000322}
323
324
325static int build_self_segment_list(
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000326 struct segment *head,
Patrick Georgi5eceb322009-05-13 16:27:25 +0000327 struct lb_memory *mem,
Peter Stuge483b7bb2009-04-14 07:40:01 +0000328 struct cbfs_payload *payload, u32 *entry)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000329{
330 struct segment *new;
331 struct segment *ptr;
Peter Stuge483b7bb2009-04-14 07:40:01 +0000332 struct cbfs_payload_segment *segment, *first_segment;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000333 memset(head, 0, sizeof(*head));
334 head->phdr_next = head->phdr_prev = head;
335 head->next = head->prev = head;
336 first_segment = segment = &payload->segments;
337
338 while(1) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000339 printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000340 switch(segment->type) {
Ronald G. Minnichae631262009-04-01 10:48:39 +0000341 case PAYLOAD_SEGMENT_PARAMS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000342 printk(BIOS_DEBUG, " parameter section (skipped)\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000343 segment++;
344 continue;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000345
Ronald G. Minnichae631262009-04-01 10:48:39 +0000346 case PAYLOAD_SEGMENT_CODE:
347 case PAYLOAD_SEGMENT_DATA:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000348 printk(BIOS_DEBUG, " %s (compression=%x)\n",
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000349 segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
350 ntohl(segment->compression));
351 new = malloc(sizeof(*new));
352 new->s_dstaddr = ntohl((u32) segment->load_addr);
353 new->s_memsz = ntohl(segment->mem_len);
354 new->compression = ntohl(segment->compression);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000355
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000356 new->s_srcaddr = (u32) ((unsigned char *) first_segment) + ntohl(segment->offset);
357 new->s_filesz = ntohl(segment->len);
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000358 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 +0000359 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
360 /* Clean up the values */
361 if (new->s_filesz > new->s_memsz) {
362 new->s_filesz = new->s_memsz;
363 }
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000364 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 +0000365 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
366 break;
367
Ronald G. Minnichae631262009-04-01 10:48:39 +0000368 case PAYLOAD_SEGMENT_BSS:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000369 printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *) ntohl((u32) segment->load_addr),
Ronald G. Minnichae631262009-04-01 10:48:39 +0000370 ntohl(segment->mem_len));
371 new = malloc(sizeof(*new));
372 new->s_filesz = 0;
373 new->s_dstaddr = ntohl((u32) segment->load_addr);
374 new->s_memsz = ntohl(segment->mem_len);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000375 break;
376
377 case PAYLOAD_SEGMENT_ENTRY:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000378 printk(BIOS_DEBUG, " Entry Point 0x%p\n", (void *) ntohl((u32) segment->load_addr));
Myles Watsonfa12b672009-04-30 22:45:41 +0000379 *entry = ntohl((u32) segment->load_addr);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000380 /* Per definition, a payload always has the entry point
381 * as last segment. Thus, we use the occurence of the
382 * entry point as break condition for the loop.
383 * Can we actually just look at the number of section?
384 */
Ronald G. Minnichae631262009-04-01 10:48:39 +0000385 return 1;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000386
387 default:
388 /* We found something that we don't know about. Throw
389 * hands into the sky and run away!
390 */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000391 printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000392 return -1;
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 segment++;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000396
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000397 // FIXME: Explain what this is
Ronald G. Minnichae631262009-04-01 10:48:39 +0000398 for(ptr = head->next; ptr != head; ptr = ptr->next) {
399 if (new->s_srcaddr < ntohl((u32) segment->load_addr))
400 break;
401 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000402
Ronald G. Minnichae631262009-04-01 10:48:39 +0000403 /* Order by stream offset */
404 new->next = ptr;
405 new->prev = ptr->prev;
406 ptr->prev->next = new;
407 ptr->prev = new;
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000408
Ronald G. Minnichae631262009-04-01 10:48:39 +0000409 /* Order by original program header order */
410 new->phdr_next = head;
411 new->phdr_prev = head->phdr_prev;
412 head->phdr_prev->phdr_next = new;
413 head->phdr_prev = new;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000414 }
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000415
Ronald G. Minnichae631262009-04-01 10:48:39 +0000416 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000417}
418
419static int load_self_segments(
Patrick Georgi5eceb322009-05-13 16:27:25 +0000420 struct segment *head,
421 struct lb_memory *mem,
422 struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000423{
Ronald G. Minnichae631262009-04-01 10:48:39 +0000424 struct segment *ptr;
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000425
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000426 unsigned long bounce_high = lb_end;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000427 for(ptr = head->next; ptr != head; ptr = ptr->next) {
428 if (!overlaps_coreboot(ptr)) continue;
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000429 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
430 bounce_high = ptr->s_dstaddr + ptr->s_memsz;
Patrick Georgi5eceb322009-05-13 16:27:25 +0000431 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000432 get_bounce_buffer(mem, bounce_high - lb_start);
Patrick Georgi5eceb322009-05-13 16:27:25 +0000433 if (!bounce_buffer) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000434 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
Patrick Georgi5eceb322009-05-13 16:27:25 +0000435 return 0;
436 }
437 for(ptr = head->next; ptr != head; ptr = ptr->next) {
438 /* Verify the memory addresses in the segment are valid */
439 if (!valid_area(mem, bounce_buffer, ptr->s_dstaddr, ptr->s_memsz))
440 return 0;
441 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000442 for(ptr = head->next; ptr != head; ptr = ptr->next) {
Stefan Reinauerf64893a2009-07-23 22:03:14 +0000443 unsigned char *dest, *src;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000444 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Ronald G. Minnichae631262009-04-01 10:48:39 +0000445 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000446
Patrick Georgi5eceb322009-05-13 16:27:25 +0000447 /* Modify the segment to load onto the bounce_buffer if necessary.
448 */
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000449 if (relocate_segment(bounce_buffer, ptr)) {
450 ptr = (ptr->prev)->prev;
451 continue;
452 }
Patrick Georgi5eceb322009-05-13 16:27:25 +0000453
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000454 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
Patrick Georgi5eceb322009-05-13 16:27:25 +0000455 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
456
Ronald G. Minnichae631262009-04-01 10:48:39 +0000457 /* Compute the boundaries of the segment */
458 dest = (unsigned char *)(ptr->s_dstaddr);
Myles Watsonfa12b672009-04-30 22:45:41 +0000459 src = (unsigned char *)(ptr->s_srcaddr);
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000460
Ronald G. Minnichae631262009-04-01 10:48:39 +0000461 /* Copy data from the initial buffer */
462 if (ptr->s_filesz) {
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000463 unsigned char *middle, *end;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000464 size_t len;
465 len = ptr->s_filesz;
Patrick Georgi369bc782009-04-25 07:32:24 +0000466 switch(ptr->compression) {
Patrick Georgi369bc782009-04-25 07:32:24 +0000467 case CBFS_COMPRESS_LZMA: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000468 printk(BIOS_DEBUG, "using LZMA\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000469 len = ulzma(src, dest);
Myles Watson94de72b2010-06-01 15:19:25 +0000470 if (!len) /* Decompression Error. */
471 return 0;
Patrick Georgi369bc782009-04-25 07:32:24 +0000472 break;
473 }
Stefan Reinauer3e4fb9d2011-04-21 21:26:58 +0000474#if CONFIG_COMPRESSED_PAYLOAD_NRV2B
Patrick Georgi369bc782009-04-25 07:32:24 +0000475 case CBFS_COMPRESS_NRV2B: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000476 printk(BIOS_DEBUG, "using NRV2B\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000477 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
478 unsigned long tmp;
479 len = unrv2b(src, dest, &tmp);
480 break;
481 }
482#endif
483 case CBFS_COMPRESS_NONE: {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000484 printk(BIOS_DEBUG, "it's not compressed!\n");
Patrick Georgi369bc782009-04-25 07:32:24 +0000485 memcpy(dest, src, len);
486 break;
487 }
488 default:
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000489 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression);
Patrick Georgi369bc782009-04-25 07:32:24 +0000490 return -1;
491 }
492 end = dest + ptr->s_memsz;
493 middle = dest + len;
Myles Watson94de72b2010-06-01 15:19:25 +0000494 printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
Patrick Georgi369bc782009-04-25 07:32:24 +0000495 (unsigned long)dest,
496 (unsigned long)middle,
497 (unsigned long)end,
498 (unsigned long)src);
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000499
500 /* Zero the extra bytes between middle & end */
501 if (middle < end) {
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000502 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000503 (unsigned long)middle, (unsigned long)(end - middle));
Zheng Baoe6ad7fa2009-11-05 10:02:59 +0000504
Ronald G. Minnich671cedc2009-05-14 21:26:28 +0000505 /* Zero the extra bytes */
506 memset(middle, 0, end - middle);
507 }
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000508 /* Copy the data that's outside the area that shadows coreboot_ram */
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000509 printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000510 if ((unsigned long)end > bounce_buffer) {
511 if ((unsigned long)dest < bounce_buffer) {
Myles Watson7943fe62009-10-30 02:08:07 +0000512 unsigned char *from = dest;
513 unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000514 unsigned long amount = bounce_buffer-(unsigned long)dest;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000515 printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000516 memcpy(to, from, amount);
517 }
518 if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
519 unsigned long from = bounce_buffer + (lb_end - lb_start);
520 unsigned long to = lb_end;
Myles Watson7943fe62009-10-30 02:08:07 +0000521 unsigned long amount = (unsigned long)end - from;
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000522 printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
Myles Watson7943fe62009-10-30 02:08:07 +0000523 memcpy((char*)to, (char*)from, amount);
Patrick Georgi26dd71c2009-09-30 21:36:38 +0000524 }
525 }
Ronald G. Minnichae631262009-04-01 10:48:39 +0000526 }
527 }
528 return 1;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000529}
530
Myles Watson92027982009-09-23 20:32:21 +0000531static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload)
Ronald G. Minnichae631262009-04-01 10:48:39 +0000532{
Myles Watsonfa12b672009-04-30 22:45:41 +0000533 u32 entry=0;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000534 struct segment head;
Ronald G. Minnichae631262009-04-01 10:48:39 +0000535
536 /* Preprocess the self segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000537 if (!build_self_segment_list(&head, mem, payload, &entry))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000538 goto out;
539
540 /* Load the segments */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000541 if (!load_self_segments(&head, mem, payload))
Ronald G. Minnichae631262009-04-01 10:48:39 +0000542 goto out;
543
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000544 printk(BIOS_SPEW, "Loaded segments\n");
Ronald G. Minnichae631262009-04-01 10:48:39 +0000545
546 /* Reset to booting from this image as late as possible */
547 boot_successful();
548
Stefan Reinauerc02b4fc2010-03-22 11:42:32 +0000549 printk(BIOS_DEBUG, "Jumping to boot code at %x\n", entry);
Alexandru Gagniuc5005bb062011-04-11 20:17:22 +0000550 post_code(POST_ENTER_ELF_BOOT);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000551
552 /* Jump to kernel */
Patrick Georgi5eceb322009-05-13 16:27:25 +0000553 jmp_to_elf_entry((void*)entry, bounce_buffer, bounce_size);
Ronald G. Minnichae631262009-04-01 10:48:39 +0000554 return 1;
555
556 out:
557 return 0;
558}
559