2 * This file is part of the coreboot project.
4 * Copyright (C) 2003 Eric W. Biederman <ebiederm@xmission.com>
5 * Copyright (C) 2009 Ron Minnich <rminnich@gmail.com>
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.
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.
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
21 #include <arch/byteorder.h>
22 #include <console/console.h>
25 #include <boot/elf_boot.h>
26 #include <boot/coreboot_tables.h>
33 /* Maximum physical address we can use for the coreboot bounce buffer. */
38 /* from coreboot_ram.ld: */
39 extern unsigned char _ram_seg;
40 extern unsigned char _eram_seg;
42 static const unsigned long lb_start = (unsigned long)&_ram_seg;
43 static const unsigned long lb_end = (unsigned long)&_eram_seg;
48 struct segment *phdr_next;
49 struct segment *phdr_prev;
50 unsigned long s_dstaddr;
51 unsigned long s_srcaddr;
52 unsigned long s_memsz;
53 unsigned long s_filesz;
58 * Static executables all want to share the same addresses
59 * in memory because only a few addresses are reliably present on
60 * a machine, and implementing general relocation is hard.
63 * - Allocate a buffer the size of the coreboot image plus additional
65 * - Anything that would overwrite coreboot copy into the lower part of
67 * - After loading an ELF image copy coreboot to the top of the buffer.
68 * - Then jump to the loaded image.
71 * - Nearly arbitrary standalone executables can be loaded.
72 * - Coreboot is preserved, so it can be returned to.
73 * - The implementation is still relatively simple,
74 * and much simpler than the general case implemented in kexec.
77 static unsigned long bounce_size, bounce_buffer;
79 static void get_bounce_buffer(struct lb_memory *mem, unsigned long req_size)
81 unsigned long lb_size;
82 unsigned long mem_entries;
85 lb_size = lb_end - lb_start;
86 /* Plus coreboot size so I have somewhere
87 * to place a copy to return to.
89 lb_size = req_size + lb_size;
90 mem_entries = (mem->size - sizeof(*mem)) / sizeof(mem->map[0]);
92 for(i = 0; i < mem_entries; i++) {
93 unsigned long mstart, mend;
95 unsigned long tbuffer;
96 if (mem->map[i].type != LB_MEM_RAM)
98 if (unpack_lb64(mem->map[i].start) > MAX_ADDR)
100 if (unpack_lb64(mem->map[i].size) < lb_size)
102 mstart = unpack_lb64(mem->map[i].start);
103 msize = MAX_ADDR - mstart +1;
104 if (msize > unpack_lb64(mem->map[i].size))
105 msize = unpack_lb64(mem->map[i].size);
106 mend = mstart + msize;
107 tbuffer = mend - lb_size;
108 if (tbuffer < buffer)
112 bounce_buffer = buffer;
113 bounce_size = req_size;
116 static int valid_area(struct lb_memory *mem, unsigned long buffer,
117 unsigned long start, unsigned long len)
119 /* Check through all of the memory segments and ensure
120 * the segment that was passed in is completely contained
124 unsigned long end = start + len;
125 unsigned long mem_entries = (mem->size - sizeof(*mem)) /
128 /* See if I conflict with the bounce buffer */
133 /* Walk through the table of valid memory ranges and see if I
136 for(i = 0; i < mem_entries; i++) {
137 uint64_t mstart, mend;
139 mtype = mem->map[i].type;
140 mstart = unpack_lb64(mem->map[i].start);
141 mend = mstart + unpack_lb64(mem->map[i].size);
142 if ((mtype == LB_MEM_RAM) && (start >= mstart) && (end < mend)) {
145 if ((mtype == LB_MEM_TABLE) && (start >= mstart) && (end < mend)) {
146 printk(BIOS_ERR, "Payload is overwriting coreboot tables.\n");
150 if (i == mem_entries) {
151 printk(BIOS_ERR, "No matching ram area found for range:\n");
152 printk(BIOS_ERR, " [0x%016lx, 0x%016lx)\n", start, end);
153 printk(BIOS_ERR, "Ram areas\n");
154 for(i = 0; i < mem_entries; i++) {
155 uint64_t mstart, mend;
157 mtype = mem->map[i].type;
158 mstart = unpack_lb64(mem->map[i].start);
159 mend = mstart + unpack_lb64(mem->map[i].size);
160 printk(BIOS_ERR, " [0x%016lx, 0x%016lx) %s\n",
161 (unsigned long)mstart,
163 (mtype == LB_MEM_RAM)?"RAM":"Reserved");
172 static int overlaps_coreboot(struct segment *seg)
174 unsigned long start, end;
175 start = seg->s_dstaddr;
176 end = start + seg->s_memsz;
177 return !((end <= lb_start) || (start >= lb_end));
180 static int relocate_segment(unsigned long buffer, struct segment *seg)
182 /* Modify all segments that want to load onto coreboot
183 * to load onto the bounce buffer instead.
185 /* ret: 1 : A new segment is inserted before the seg.
186 * 0 : A new segment is inserted after the seg, or no new one.
188 unsigned long start, middle, end, ret = 0;
190 printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
193 /* I don't conflict with coreboot so get out of here */
194 if (!overlaps_coreboot(seg))
197 start = seg->s_dstaddr;
198 middle = start + seg->s_filesz;
199 end = start + seg->s_memsz;
201 printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
204 if (seg->compression == CBFS_COMPRESS_NONE) {
205 /* Slice off a piece at the beginning
206 * that doesn't conflict with coreboot.
208 if (start < lb_start) {
210 unsigned long len = lb_start - start;
211 new = malloc(sizeof(*new));
215 seg->s_dstaddr += len;
216 seg->s_srcaddr += len;
217 if (seg->s_filesz > len) {
219 seg->s_filesz -= len;
224 /* Order by stream offset */
226 new->prev = seg->prev;
227 seg->prev->next = new;
229 /* Order by original program header order */
230 new->phdr_next = seg;
231 new->phdr_prev = seg->phdr_prev;
232 seg->phdr_prev->phdr_next = new;
233 seg->phdr_prev = new;
235 /* compute the new value of start */
236 start = seg->s_dstaddr;
238 printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
240 new->s_dstaddr + new->s_filesz,
241 new->s_dstaddr + new->s_memsz);
246 /* Slice off a piece at the end
247 * that doesn't conflict with coreboot
250 unsigned long len = lb_end - start;
252 new = malloc(sizeof(*new));
256 new->s_dstaddr += len;
257 new->s_srcaddr += len;
258 if (seg->s_filesz > len) {
260 new->s_filesz -= len;
264 /* Order by stream offset */
265 new->next = seg->next;
267 seg->next->prev = new;
269 /* Order by original program header order */
270 new->phdr_next = seg->phdr_next;
271 new->phdr_prev = seg;
272 seg->phdr_next->phdr_prev = new;
273 seg->phdr_next = new;
275 printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
277 new->s_dstaddr + new->s_filesz,
278 new->s_dstaddr + new->s_memsz);
282 /* Now retarget this segment onto the bounce buffer */
283 /* sort of explanation: the buffer is a 1:1 mapping to coreboot.
284 * so you will make the dstaddr be this buffer, and it will get copied
285 * later to where coreboot lives.
287 seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
289 printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
291 seg->s_dstaddr + seg->s_filesz,
292 seg->s_dstaddr + seg->s_memsz);
298 static int build_self_segment_list(
299 struct segment *head,
300 struct lb_memory *mem,
301 struct cbfs_payload *payload, u32 *entry)
305 struct cbfs_payload_segment *segment, *first_segment;
306 memset(head, 0, sizeof(*head));
307 head->phdr_next = head->phdr_prev = head;
308 head->next = head->prev = head;
309 first_segment = segment = &payload->segments;
312 printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
313 switch(segment->type) {
314 case PAYLOAD_SEGMENT_PARAMS:
315 printk(BIOS_DEBUG, " parameter section (skipped)\n");
319 case PAYLOAD_SEGMENT_CODE:
320 case PAYLOAD_SEGMENT_DATA:
321 printk(BIOS_DEBUG, " %s (compression=%x)\n",
322 segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
323 ntohl(segment->compression));
324 new = malloc(sizeof(*new));
325 new->s_dstaddr = ntohll(segment->load_addr);
326 new->s_memsz = ntohl(segment->mem_len);
327 new->compression = ntohl(segment->compression);
329 new->s_srcaddr = (u32) ((unsigned char *)first_segment)
330 + ntohl(segment->offset);
331 new->s_filesz = ntohl(segment->len);
332 printk(BIOS_DEBUG, " New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n",
333 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
334 /* Clean up the values */
335 if (new->s_filesz > new->s_memsz) {
336 new->s_filesz = new->s_memsz;
338 printk(BIOS_DEBUG, " (cleaned up) New segment addr 0x%lx size 0x%lx offset 0x%lx filesize 0x%lx\n",
339 new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
342 case PAYLOAD_SEGMENT_BSS:
343 printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
344 (intptr_t)ntohll(segment->load_addr),
345 ntohl(segment->mem_len));
346 new = malloc(sizeof(*new));
348 new->s_dstaddr = ntohll(segment->load_addr);
349 new->s_memsz = ntohl(segment->mem_len);
352 case PAYLOAD_SEGMENT_ENTRY:
353 printk(BIOS_DEBUG, " Entry Point 0x%p\n", (void *) ntohl((u32) segment->load_addr));
354 *entry = ntohll(segment->load_addr);
355 /* Per definition, a payload always has the entry point
356 * as last segment. Thus, we use the occurence of the
357 * entry point as break condition for the loop.
358 * Can we actually just look at the number of section?
363 /* We found something that we don't know about. Throw
364 * hands into the sky and run away!
366 printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
372 // FIXME: Explain what this is
373 for(ptr = head->next; ptr != head; ptr = ptr->next) {
374 if (new->s_srcaddr < ntohll(segment->load_addr))
378 /* Order by stream offset */
380 new->prev = ptr->prev;
381 ptr->prev->next = new;
384 /* Order by original program header order */
385 new->phdr_next = head;
386 new->phdr_prev = head->phdr_prev;
387 head->phdr_prev->phdr_next = new;
388 head->phdr_prev = new;
394 static int load_self_segments(
395 struct segment *head,
396 struct lb_memory *mem,
397 struct cbfs_payload *payload)
401 unsigned long bounce_high = lb_end;
402 for(ptr = head->next; ptr != head; ptr = ptr->next) {
403 if (!overlaps_coreboot(ptr)) continue;
404 if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
405 bounce_high = ptr->s_dstaddr + ptr->s_memsz;
407 get_bounce_buffer(mem, bounce_high - lb_start);
408 if (!bounce_buffer) {
409 printk(BIOS_ERR, "Could not find a bounce buffer...\n");
412 for(ptr = head->next; ptr != head; ptr = ptr->next) {
413 /* Verify the memory addresses in the segment are valid */
414 if (!valid_area(mem, bounce_buffer, ptr->s_dstaddr, ptr->s_memsz))
417 for(ptr = head->next; ptr != head; ptr = ptr->next) {
418 unsigned char *dest, *src;
419 printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
420 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
422 /* Modify the segment to load onto the bounce_buffer if necessary.
424 if (relocate_segment(bounce_buffer, ptr)) {
425 ptr = (ptr->prev)->prev;
429 printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
430 ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
432 /* Compute the boundaries of the segment */
433 dest = (unsigned char *)(ptr->s_dstaddr);
434 src = (unsigned char *)(ptr->s_srcaddr);
436 /* Copy data from the initial buffer */
438 unsigned char *middle, *end;
441 switch(ptr->compression) {
442 case CBFS_COMPRESS_LZMA: {
443 printk(BIOS_DEBUG, "using LZMA\n");
444 len = ulzma(src, dest);
445 if (!len) /* Decompression Error. */
449 #if CONFIG_COMPRESSED_PAYLOAD_NRV2B
450 case CBFS_COMPRESS_NRV2B: {
451 printk(BIOS_DEBUG, "using NRV2B\n");
452 unsigned long unrv2b(u8 *src, u8 *dst, unsigned long *ilen_p);
454 len = unrv2b(src, dest, &tmp);
458 case CBFS_COMPRESS_NONE: {
459 printk(BIOS_DEBUG, "it's not compressed!\n");
460 memcpy(dest, src, len);
464 printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression);
467 end = dest + ptr->s_memsz;
469 printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
471 (unsigned long)middle,
475 /* Zero the extra bytes between middle & end */
477 printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
478 (unsigned long)middle, (unsigned long)(end - middle));
480 /* Zero the extra bytes */
481 memset(middle, 0, end - middle);
483 /* Copy the data that's outside the area that shadows coreboot_ram */
484 printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
485 if ((unsigned long)end > bounce_buffer) {
486 if ((unsigned long)dest < bounce_buffer) {
487 unsigned char *from = dest;
488 unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
489 unsigned long amount = bounce_buffer-(unsigned long)dest;
490 printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
491 memcpy(to, from, amount);
493 if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
494 unsigned long from = bounce_buffer + (lb_end - lb_start);
495 unsigned long to = lb_end;
496 unsigned long amount = (unsigned long)end - from;
497 printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
498 memcpy((char*)to, (char*)from, amount);
506 static int selfboot(struct lb_memory *mem, struct cbfs_payload *payload)
511 /* Preprocess the self segments */
512 if (!build_self_segment_list(&head, mem, payload, &entry))
515 /* Load the segments */
516 if (!load_self_segments(&head, mem, payload))
519 printk(BIOS_SPEW, "Loaded segments\n");
521 /* Reset to booting from this image as late as possible */
524 printk(BIOS_DEBUG, "Jumping to boot code at %x\n", entry);
525 post_code(POST_ENTER_ELF_BOOT);
528 jmp_to_elf_entry((void*)entry, bounce_buffer, bounce_size);
535 void *cbfs_load_payload(struct lb_memory *lb_mem, const char *name)
537 struct cbfs_payload *payload;
539 payload = (struct cbfs_payload *)cbfs_find_file(name, CBFS_TYPE_PAYLOAD);
542 printk(BIOS_DEBUG, "Got a payload\n");
544 selfboot(lb_mem, payload);
545 printk(BIOS_EMERG, "SELFBOOT RETURNED!\n");