2009-04-01 13:03:32 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the coreboot project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2003 Eric W. Biederman <ebiederm@xmission.com>
|
|
|
|
* Copyright (C) 2009 Ron Minnich <rminnich@gmail.com>
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; version 2 of the License.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA, 02110-1301 USA
|
|
|
|
*/
|
|
|
|
|
2011-10-15 00:19:21 +02:00
|
|
|
#include <arch/byteorder.h>
|
2009-04-01 12:48:39 +02:00
|
|
|
#include <console/console.h>
|
2013-03-29 22:23:23 +01:00
|
|
|
#include <cpu/cpu.h>
|
2009-04-01 12:48:39 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-04-14 09:40:01 +02:00
|
|
|
#include <cbfs.h>
|
2009-10-28 17:13:28 +01:00
|
|
|
#include <lib.h>
|
2014-02-25 07:21:10 +01:00
|
|
|
#include <bootmem.h>
|
2014-02-24 21:56:34 +01:00
|
|
|
#include <payload_loader.h>
|
2009-04-01 12:48:39 +02:00
|
|
|
|
2011-11-07 21:43:03 +01:00
|
|
|
/* from coreboot_ram.ld: */
|
2009-04-01 12:48:39 +02:00
|
|
|
extern unsigned char _ram_seg;
|
|
|
|
extern unsigned char _eram_seg;
|
|
|
|
|
2011-11-07 21:43:03 +01:00
|
|
|
static const unsigned long lb_start = (unsigned long)&_ram_seg;
|
|
|
|
static const unsigned long lb_end = (unsigned long)&_eram_seg;
|
|
|
|
|
2009-04-01 12:48:39 +02:00
|
|
|
struct segment {
|
|
|
|
struct segment *next;
|
|
|
|
struct segment *prev;
|
|
|
|
unsigned long s_dstaddr;
|
|
|
|
unsigned long s_srcaddr;
|
|
|
|
unsigned long s_memsz;
|
|
|
|
unsigned long s_filesz;
|
2009-04-25 09:32:24 +02:00
|
|
|
int compression;
|
2009-04-01 12:48:39 +02:00
|
|
|
};
|
|
|
|
|
2009-11-05 11:02:59 +01:00
|
|
|
/* The problem:
|
2009-04-01 12:48:39 +02:00
|
|
|
* Static executables all want to share the same addresses
|
|
|
|
* in memory because only a few addresses are reliably present on
|
|
|
|
* a machine, and implementing general relocation is hard.
|
|
|
|
*
|
|
|
|
* The solution:
|
2009-07-24 00:03:14 +02:00
|
|
|
* - Allocate a buffer the size of the coreboot image plus additional
|
|
|
|
* required space.
|
|
|
|
* - Anything that would overwrite coreboot copy into the lower part of
|
2009-11-05 11:02:59 +01:00
|
|
|
* the buffer.
|
2009-07-24 00:03:14 +02:00
|
|
|
* - After loading an ELF image copy coreboot to the top of the buffer.
|
2009-04-01 12:48:39 +02:00
|
|
|
* - Then jump to the loaded image.
|
2009-11-05 11:02:59 +01:00
|
|
|
*
|
2009-04-01 12:48:39 +02:00
|
|
|
* Benefits:
|
|
|
|
* - Nearly arbitrary standalone executables can be loaded.
|
|
|
|
* - Coreboot is preserved, so it can be returned to.
|
|
|
|
* - The implementation is still relatively simple,
|
2009-11-01 10:18:23 +01:00
|
|
|
* and much simpler than the general case implemented in kexec.
|
2009-04-01 12:48:39 +02:00
|
|
|
*/
|
|
|
|
|
2009-05-13 18:27:25 +02:00
|
|
|
static unsigned long bounce_size, bounce_buffer;
|
|
|
|
|
2014-02-25 07:21:10 +01:00
|
|
|
static void get_bounce_buffer(unsigned long req_size)
|
2009-04-01 12:48:39 +02:00
|
|
|
{
|
|
|
|
unsigned long lb_size;
|
2014-02-25 07:21:10 +01:00
|
|
|
void *buffer;
|
2014-02-25 05:11:45 +01:00
|
|
|
|
|
|
|
/* When the ramstage is relocatable there is no need for a bounce
|
|
|
|
* buffer. All payloads should not overlap the ramstage.
|
|
|
|
*/
|
|
|
|
if (IS_ENABLED(CONFIG_RELOCATABLE_RAMSTAGE)) {
|
|
|
|
bounce_buffer = ~0UL;
|
|
|
|
bounce_size = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-11-07 21:43:03 +01:00
|
|
|
lb_size = lb_end - lb_start;
|
|
|
|
/* Plus coreboot size so I have somewhere
|
|
|
|
* to place a copy to return to.
|
|
|
|
*/
|
2009-09-23 22:32:21 +02:00
|
|
|
lb_size = req_size + lb_size;
|
2009-04-01 12:48:39 +02:00
|
|
|
|
2014-02-25 07:21:10 +01:00
|
|
|
buffer = bootmem_allocate_buffer(lb_size);
|
2009-04-01 12:48:39 +02:00
|
|
|
|
2014-02-25 07:21:10 +01:00
|
|
|
printk(BIOS_SPEW, "Bounce Buffer at %p, %lu bytes\n", buffer, lb_size);
|
2009-11-05 11:02:59 +01:00
|
|
|
|
2014-02-25 07:21:10 +01:00
|
|
|
bounce_buffer = (uintptr_t)buffer;
|
|
|
|
bounce_size = req_size;
|
2009-04-01 12:48:39 +02:00
|
|
|
}
|
|
|
|
|
2009-05-13 18:27:25 +02:00
|
|
|
static int overlaps_coreboot(struct segment *seg)
|
|
|
|
{
|
|
|
|
unsigned long start, end;
|
|
|
|
start = seg->s_dstaddr;
|
|
|
|
end = start + seg->s_memsz;
|
|
|
|
return !((end <= lb_start) || (start >= lb_end));
|
|
|
|
}
|
|
|
|
|
2009-11-05 11:02:59 +01:00
|
|
|
static int relocate_segment(unsigned long buffer, struct segment *seg)
|
2009-04-01 12:48:39 +02:00
|
|
|
{
|
|
|
|
/* Modify all segments that want to load onto coreboot
|
|
|
|
* to load onto the bounce buffer instead.
|
|
|
|
*/
|
2009-11-05 11:02:59 +01:00
|
|
|
/* ret: 1 : A new segment is inserted before the seg.
|
2011-11-07 21:43:03 +01:00
|
|
|
* 0 : A new segment is inserted after the seg, or no new one.
|
|
|
|
*/
|
2009-11-05 11:02:59 +01:00
|
|
|
unsigned long start, middle, end, ret = 0;
|
2009-04-01 12:48:39 +02:00
|
|
|
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_SPEW, "lb: [0x%016lx, 0x%016lx)\n",
|
2009-04-01 12:48:39 +02:00
|
|
|
lb_start, lb_end);
|
|
|
|
|
2009-05-13 18:27:25 +02:00
|
|
|
/* I don't conflict with coreboot so get out of here */
|
|
|
|
if (!overlaps_coreboot(seg))
|
2009-11-05 11:02:59 +01:00
|
|
|
return 0;
|
2009-05-13 18:27:25 +02:00
|
|
|
|
2009-04-01 12:48:39 +02:00
|
|
|
start = seg->s_dstaddr;
|
|
|
|
middle = start + seg->s_filesz;
|
|
|
|
end = start + seg->s_memsz;
|
|
|
|
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_SPEW, "segment: [0x%016lx, 0x%016lx, 0x%016lx)\n",
|
2009-04-01 12:48:39 +02:00
|
|
|
start, middle, end);
|
|
|
|
|
2009-04-25 09:32:24 +02:00
|
|
|
if (seg->compression == CBFS_COMPRESS_NONE) {
|
|
|
|
/* Slice off a piece at the beginning
|
|
|
|
* that doesn't conflict with coreboot.
|
|
|
|
*/
|
|
|
|
if (start < lb_start) {
|
|
|
|
struct segment *new;
|
|
|
|
unsigned long len = lb_start - start;
|
|
|
|
new = malloc(sizeof(*new));
|
|
|
|
*new = *seg;
|
|
|
|
new->s_memsz = len;
|
|
|
|
seg->s_memsz -= len;
|
|
|
|
seg->s_dstaddr += len;
|
|
|
|
seg->s_srcaddr += len;
|
|
|
|
if (seg->s_filesz > len) {
|
|
|
|
new->s_filesz = len;
|
|
|
|
seg->s_filesz -= len;
|
|
|
|
} else {
|
|
|
|
seg->s_filesz = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Order by stream offset */
|
|
|
|
new->next = seg;
|
|
|
|
new->prev = seg->prev;
|
|
|
|
seg->prev->next = new;
|
|
|
|
seg->prev = new;
|
|
|
|
|
|
|
|
/* compute the new value of start */
|
|
|
|
start = seg->s_dstaddr;
|
2009-11-05 11:02:59 +01:00
|
|
|
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_SPEW, " early: [0x%016lx, 0x%016lx, 0x%016lx)\n",
|
2009-11-05 11:02:59 +01:00
|
|
|
new->s_dstaddr,
|
2009-04-25 09:32:24 +02:00
|
|
|
new->s_dstaddr + new->s_filesz,
|
|
|
|
new->s_dstaddr + new->s_memsz);
|
2009-11-05 11:02:59 +01:00
|
|
|
|
|
|
|
ret = 1;
|
2009-07-24 00:03:14 +02:00
|
|
|
}
|
2009-11-05 11:02:59 +01:00
|
|
|
|
|
|
|
/* Slice off a piece at the end
|
|
|
|
* that doesn't conflict with coreboot
|
2009-04-25 09:32:24 +02:00
|
|
|
*/
|
|
|
|
if (end > lb_end) {
|
|
|
|
unsigned long len = lb_end - start;
|
|
|
|
struct segment *new;
|
|
|
|
new = malloc(sizeof(*new));
|
|
|
|
*new = *seg;
|
|
|
|
seg->s_memsz = len;
|
|
|
|
new->s_memsz -= len;
|
|
|
|
new->s_dstaddr += len;
|
|
|
|
new->s_srcaddr += len;
|
|
|
|
if (seg->s_filesz > len) {
|
|
|
|
seg->s_filesz = len;
|
|
|
|
new->s_filesz -= len;
|
|
|
|
} else {
|
|
|
|
new->s_filesz = 0;
|
|
|
|
}
|
|
|
|
/* Order by stream offset */
|
|
|
|
new->next = seg->next;
|
|
|
|
new->prev = seg;
|
|
|
|
seg->next->prev = new;
|
|
|
|
seg->next = new;
|
|
|
|
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_SPEW, " late: [0x%016lx, 0x%016lx, 0x%016lx)\n",
|
2009-11-05 11:02:59 +01:00
|
|
|
new->s_dstaddr,
|
2009-04-25 09:32:24 +02:00
|
|
|
new->s_dstaddr + new->s_filesz,
|
|
|
|
new->s_dstaddr + new->s_memsz);
|
2009-04-01 12:48:39 +02:00
|
|
|
}
|
|
|
|
}
|
2009-07-24 00:03:14 +02:00
|
|
|
|
2009-04-01 12:48:39 +02:00
|
|
|
/* Now retarget this segment onto the bounce buffer */
|
2009-11-05 11:02:59 +01:00
|
|
|
/* sort of explanation: the buffer is a 1:1 mapping to coreboot.
|
2009-04-01 12:48:39 +02:00
|
|
|
* so you will make the dstaddr be this buffer, and it will get copied
|
|
|
|
* later to where coreboot lives.
|
|
|
|
*/
|
|
|
|
seg->s_dstaddr = buffer + (seg->s_dstaddr - lb_start);
|
|
|
|
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_SPEW, " bounce: [0x%016lx, 0x%016lx, 0x%016lx)\n",
|
2009-11-05 11:02:59 +01:00
|
|
|
seg->s_dstaddr,
|
|
|
|
seg->s_dstaddr + seg->s_filesz,
|
2009-04-01 12:48:39 +02:00
|
|
|
seg->s_dstaddr + seg->s_memsz);
|
2009-11-05 11:02:59 +01:00
|
|
|
|
|
|
|
return ret;
|
2009-04-01 12:48:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static int build_self_segment_list(
|
2009-11-05 11:02:59 +01:00
|
|
|
struct segment *head,
|
2014-02-25 04:50:24 +01:00
|
|
|
struct payload *payload, uintptr_t *entry)
|
2009-04-01 12:48:39 +02:00
|
|
|
{
|
|
|
|
struct segment *new;
|
|
|
|
struct segment *ptr;
|
2009-04-14 09:40:01 +02:00
|
|
|
struct cbfs_payload_segment *segment, *first_segment;
|
2014-02-25 04:50:24 +01:00
|
|
|
struct cbfs_payload *cbfs_payload;
|
|
|
|
cbfs_payload = payload->backing_store.data;
|
2009-04-01 12:48:39 +02:00
|
|
|
memset(head, 0, sizeof(*head));
|
|
|
|
head->next = head->prev = head;
|
2014-02-25 04:50:24 +01:00
|
|
|
first_segment = segment = &cbfs_payload->segments;
|
2009-04-01 12:48:39 +02:00
|
|
|
|
|
|
|
while(1) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Loading segment from rom address 0x%p\n", segment);
|
2009-04-01 12:48:39 +02:00
|
|
|
switch(segment->type) {
|
|
|
|
case PAYLOAD_SEGMENT_PARAMS:
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, " parameter section (skipped)\n");
|
2009-04-01 12:48:39 +02:00
|
|
|
segment++;
|
|
|
|
continue;
|
2009-07-24 00:03:14 +02:00
|
|
|
|
2009-04-01 12:48:39 +02:00
|
|
|
case PAYLOAD_SEGMENT_CODE:
|
|
|
|
case PAYLOAD_SEGMENT_DATA:
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, " %s (compression=%x)\n",
|
2009-07-24 00:03:14 +02:00
|
|
|
segment->type == PAYLOAD_SEGMENT_CODE ? "code" : "data",
|
|
|
|
ntohl(segment->compression));
|
|
|
|
new = malloc(sizeof(*new));
|
2011-10-17 18:51:15 +02:00
|
|
|
new->s_dstaddr = ntohll(segment->load_addr);
|
2009-07-24 00:03:14 +02:00
|
|
|
new->s_memsz = ntohl(segment->mem_len);
|
|
|
|
new->compression = ntohl(segment->compression);
|
|
|
|
|
2013-11-11 19:36:28 +01:00
|
|
|
new->s_srcaddr = (uintptr_t)
|
|
|
|
((unsigned char *)first_segment)
|
|
|
|
+ ntohl(segment->offset);
|
2009-07-24 00:03:14 +02:00
|
|
|
new->s_filesz = ntohl(segment->len);
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, " New segment dstaddr 0x%lx memsize 0x%lx srcaddr 0x%lx filesize 0x%lx\n",
|
2009-07-24 00:03:14 +02:00
|
|
|
new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
|
|
|
|
/* Clean up the values */
|
|
|
|
if (new->s_filesz > new->s_memsz) {
|
|
|
|
new->s_filesz = new->s_memsz;
|
|
|
|
}
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, " (cleaned up) New segment addr 0x%lx size 0x%lx offset 0x%lx filesize 0x%lx\n",
|
2009-07-24 00:03:14 +02:00
|
|
|
new->s_dstaddr, new->s_memsz, new->s_srcaddr, new->s_filesz);
|
|
|
|
break;
|
|
|
|
|
2009-04-01 12:48:39 +02:00
|
|
|
case PAYLOAD_SEGMENT_BSS:
|
2011-10-17 18:51:15 +02:00
|
|
|
printk(BIOS_DEBUG, " BSS 0x%p (%d byte)\n", (void *)
|
|
|
|
(intptr_t)ntohll(segment->load_addr),
|
|
|
|
ntohl(segment->mem_len));
|
2009-04-01 12:48:39 +02:00
|
|
|
new = malloc(sizeof(*new));
|
|
|
|
new->s_filesz = 0;
|
2011-10-17 18:51:15 +02:00
|
|
|
new->s_dstaddr = ntohll(segment->load_addr);
|
2009-04-01 12:48:39 +02:00
|
|
|
new->s_memsz = ntohl(segment->mem_len);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case PAYLOAD_SEGMENT_ENTRY:
|
2013-02-27 09:38:38 +01:00
|
|
|
printk(BIOS_DEBUG, " Entry Point 0x%p\n",
|
|
|
|
(void *)(intptr_t)ntohll(segment->load_addr));
|
2011-10-17 18:51:15 +02:00
|
|
|
*entry = ntohll(segment->load_addr);
|
2009-07-24 00:03:14 +02:00
|
|
|
/* Per definition, a payload always has the entry point
|
2013-07-10 05:51:14 +02:00
|
|
|
* as last segment. Thus, we use the occurrence of the
|
2009-07-24 00:03:14 +02:00
|
|
|
* entry point as break condition for the loop.
|
|
|
|
* Can we actually just look at the number of section?
|
|
|
|
*/
|
2009-04-01 12:48:39 +02:00
|
|
|
return 1;
|
2009-07-24 00:03:14 +02:00
|
|
|
|
|
|
|
default:
|
|
|
|
/* We found something that we don't know about. Throw
|
|
|
|
* hands into the sky and run away!
|
|
|
|
*/
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_EMERG, "Bad segment type %x\n", segment->type);
|
2009-07-24 00:03:14 +02:00
|
|
|
return -1;
|
2009-04-01 12:48:39 +02:00
|
|
|
}
|
2009-07-24 00:03:14 +02:00
|
|
|
|
2012-01-11 23:07:39 +01:00
|
|
|
/* We have found another CODE, DATA or BSS segment */
|
2009-04-01 12:48:39 +02:00
|
|
|
segment++;
|
2009-07-24 00:03:14 +02:00
|
|
|
|
2012-01-11 23:07:39 +01:00
|
|
|
/* Find place where to insert our segment */
|
2009-04-01 12:48:39 +02:00
|
|
|
for(ptr = head->next; ptr != head; ptr = ptr->next) {
|
2011-10-17 18:51:15 +02:00
|
|
|
if (new->s_srcaddr < ntohll(segment->load_addr))
|
2009-04-01 12:48:39 +02:00
|
|
|
break;
|
|
|
|
}
|
2009-07-24 00:03:14 +02:00
|
|
|
|
2009-04-01 12:48:39 +02:00
|
|
|
/* Order by stream offset */
|
|
|
|
new->next = ptr;
|
|
|
|
new->prev = ptr->prev;
|
|
|
|
ptr->prev->next = new;
|
|
|
|
ptr->prev = new;
|
|
|
|
}
|
2009-07-24 00:03:14 +02:00
|
|
|
|
2009-04-01 12:48:39 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int load_self_segments(
|
2009-05-13 18:27:25 +02:00
|
|
|
struct segment *head,
|
2014-02-25 04:50:24 +01:00
|
|
|
struct payload *payload)
|
2009-04-01 12:48:39 +02:00
|
|
|
{
|
|
|
|
struct segment *ptr;
|
2014-02-25 07:21:10 +01:00
|
|
|
const unsigned long one_meg = (1UL << 20);
|
2009-09-30 23:36:38 +02:00
|
|
|
unsigned long bounce_high = lb_end;
|
2014-02-25 07:21:10 +01:00
|
|
|
|
2009-05-13 18:27:25 +02:00
|
|
|
for(ptr = head->next; ptr != head; ptr = ptr->next) {
|
2014-02-25 07:21:10 +01:00
|
|
|
if (bootmem_region_targets_usable_ram(ptr->s_dstaddr,
|
|
|
|
ptr->s_memsz))
|
2012-01-11 23:07:39 +01:00
|
|
|
continue;
|
2014-02-25 07:21:10 +01:00
|
|
|
|
|
|
|
if (ptr->s_dstaddr < one_meg &&
|
|
|
|
(ptr->s_dstaddr + ptr->s_memsz) <= one_meg) {
|
|
|
|
printk(BIOS_DEBUG,
|
|
|
|
"Payload being loaded below 1MiB "
|
|
|
|
"without region being marked as RAM usable.\n");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Payload segment not targeting RAM. */
|
|
|
|
printk(BIOS_ERR, "SELF Payload doesn't target RAM:\n");
|
|
|
|
printk(BIOS_ERR, "Failed Segment: 0x%lx, %lu bytes\n",
|
|
|
|
ptr->s_dstaddr, ptr->s_memsz);
|
|
|
|
bootmem_dump_ranges();
|
2013-02-09 00:28:04 +01:00
|
|
|
return 0;
|
2014-02-25 07:21:10 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
for(ptr = head->next; ptr != head; ptr = ptr->next) {
|
|
|
|
/*
|
|
|
|
* Add segments to bootmem memory map before a bounce buffer is
|
|
|
|
* allocated so that there aren't conflicts with the actual
|
|
|
|
* payload.
|
|
|
|
*/
|
|
|
|
bootmem_add_range(ptr->s_dstaddr, ptr->s_memsz,
|
|
|
|
LB_MEM_UNUSABLE);
|
|
|
|
|
|
|
|
if (!overlaps_coreboot(ptr))
|
|
|
|
continue;
|
2009-09-30 23:36:38 +02:00
|
|
|
if (ptr->s_dstaddr + ptr->s_memsz > bounce_high)
|
|
|
|
bounce_high = ptr->s_dstaddr + ptr->s_memsz;
|
2009-05-13 18:27:25 +02:00
|
|
|
}
|
2014-02-25 07:21:10 +01:00
|
|
|
get_bounce_buffer(bounce_high - lb_start);
|
2009-05-13 18:27:25 +02:00
|
|
|
if (!bounce_buffer) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_ERR, "Could not find a bounce buffer...\n");
|
2009-05-13 18:27:25 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-02-25 05:11:45 +01:00
|
|
|
|
|
|
|
/* Update the payload's bounce buffer data used when loading. */
|
|
|
|
payload->bounce.data = (void *)(uintptr_t)bounce_buffer;
|
|
|
|
payload->bounce.size = bounce_size;
|
|
|
|
|
2009-04-01 12:48:39 +02:00
|
|
|
for(ptr = head->next; ptr != head; ptr = ptr->next) {
|
2009-07-24 00:03:14 +02:00
|
|
|
unsigned char *dest, *src;
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Loading Segment: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
|
2009-04-01 12:48:39 +02:00
|
|
|
ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
|
2009-11-05 11:02:59 +01:00
|
|
|
|
2009-05-13 18:27:25 +02:00
|
|
|
/* Modify the segment to load onto the bounce_buffer if necessary.
|
|
|
|
*/
|
2009-11-05 11:02:59 +01:00
|
|
|
if (relocate_segment(bounce_buffer, ptr)) {
|
|
|
|
ptr = (ptr->prev)->prev;
|
|
|
|
continue;
|
|
|
|
}
|
2009-05-13 18:27:25 +02:00
|
|
|
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Post relocation: addr: 0x%016lx memsz: 0x%016lx filesz: 0x%016lx\n",
|
2009-05-13 18:27:25 +02:00
|
|
|
ptr->s_dstaddr, ptr->s_memsz, ptr->s_filesz);
|
|
|
|
|
2009-04-01 12:48:39 +02:00
|
|
|
/* Compute the boundaries of the segment */
|
|
|
|
dest = (unsigned char *)(ptr->s_dstaddr);
|
2009-05-01 00:45:41 +02:00
|
|
|
src = (unsigned char *)(ptr->s_srcaddr);
|
2009-11-05 11:02:59 +01:00
|
|
|
|
2009-04-01 12:48:39 +02:00
|
|
|
/* Copy data from the initial buffer */
|
|
|
|
if (ptr->s_filesz) {
|
2009-05-14 23:26:28 +02:00
|
|
|
unsigned char *middle, *end;
|
2009-04-01 12:48:39 +02:00
|
|
|
size_t len;
|
|
|
|
len = ptr->s_filesz;
|
2009-04-25 09:32:24 +02:00
|
|
|
switch(ptr->compression) {
|
|
|
|
case CBFS_COMPRESS_LZMA: {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "using LZMA\n");
|
2009-04-25 09:32:24 +02:00
|
|
|
len = ulzma(src, dest);
|
2010-06-01 17:19:25 +02:00
|
|
|
if (!len) /* Decompression Error. */
|
|
|
|
return 0;
|
2009-04-25 09:32:24 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CBFS_COMPRESS_NONE: {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "it's not compressed!\n");
|
2009-04-25 09:32:24 +02:00
|
|
|
memcpy(dest, src, len);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_INFO, "CBFS: Unknown compression type %d\n", ptr->compression);
|
2009-04-25 09:32:24 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
end = dest + ptr->s_memsz;
|
|
|
|
middle = dest + len;
|
2010-06-01 17:19:25 +02:00
|
|
|
printk(BIOS_SPEW, "[ 0x%08lx, %08lx, 0x%08lx) <- %08lx\n",
|
2009-04-25 09:32:24 +02:00
|
|
|
(unsigned long)dest,
|
|
|
|
(unsigned long)middle,
|
|
|
|
(unsigned long)end,
|
|
|
|
(unsigned long)src);
|
2009-05-14 23:26:28 +02:00
|
|
|
|
|
|
|
/* Zero the extra bytes between middle & end */
|
|
|
|
if (middle < end) {
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "Clearing Segment: addr: 0x%016lx memsz: 0x%016lx\n",
|
2009-05-14 23:26:28 +02:00
|
|
|
(unsigned long)middle, (unsigned long)(end - middle));
|
2009-11-05 11:02:59 +01:00
|
|
|
|
2009-05-14 23:26:28 +02:00
|
|
|
/* Zero the extra bytes */
|
|
|
|
memset(middle, 0, end - middle);
|
|
|
|
}
|
2009-09-30 23:36:38 +02:00
|
|
|
/* Copy the data that's outside the area that shadows coreboot_ram */
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "dest %p, end %p, bouncebuffer %lx\n", dest, end, bounce_buffer);
|
2009-09-30 23:36:38 +02:00
|
|
|
if ((unsigned long)end > bounce_buffer) {
|
|
|
|
if ((unsigned long)dest < bounce_buffer) {
|
2009-10-30 03:08:07 +01:00
|
|
|
unsigned char *from = dest;
|
|
|
|
unsigned char *to = (unsigned char*)(lb_start-(bounce_buffer-(unsigned long)dest));
|
2009-09-30 23:36:38 +02:00
|
|
|
unsigned long amount = bounce_buffer-(unsigned long)dest;
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "move prefix around: from %p, to %p, amount: %lx\n", from, to, amount);
|
2009-09-30 23:36:38 +02:00
|
|
|
memcpy(to, from, amount);
|
|
|
|
}
|
|
|
|
if ((unsigned long)end > bounce_buffer + (lb_end - lb_start)) {
|
|
|
|
unsigned long from = bounce_buffer + (lb_end - lb_start);
|
|
|
|
unsigned long to = lb_end;
|
2009-10-30 03:08:07 +01:00
|
|
|
unsigned long amount = (unsigned long)end - from;
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_DEBUG, "move suffix around: from %lx, to %lx, amount: %lx\n", from, to, amount);
|
2009-10-30 03:08:07 +01:00
|
|
|
memcpy((char*)to, (char*)from, amount);
|
2009-09-30 23:36:38 +02:00
|
|
|
}
|
|
|
|
}
|
2009-04-01 12:48:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-02-25 07:21:10 +01:00
|
|
|
void *selfload(struct payload *payload)
|
2009-04-01 12:48:39 +02:00
|
|
|
{
|
2013-11-11 19:36:28 +01:00
|
|
|
uintptr_t entry = 0;
|
2009-04-01 12:48:39 +02:00
|
|
|
struct segment head;
|
|
|
|
|
|
|
|
/* Preprocess the self segments */
|
2014-02-25 07:21:10 +01:00
|
|
|
if (!build_self_segment_list(&head, payload, &entry))
|
2009-04-01 12:48:39 +02:00
|
|
|
goto out;
|
|
|
|
|
|
|
|
/* Load the segments */
|
2014-02-25 07:21:10 +01:00
|
|
|
if (!load_self_segments(&head, payload))
|
2009-04-01 12:48:39 +02:00
|
|
|
goto out;
|
|
|
|
|
2010-03-22 12:42:32 +01:00
|
|
|
printk(BIOS_SPEW, "Loaded segments\n");
|
2009-04-01 12:48:39 +02:00
|
|
|
|
2013-04-25 05:59:45 +02:00
|
|
|
return (void *)entry;
|
|
|
|
|
|
|
|
out:
|
|
|
|
return NULL;
|
|
|
|
}
|