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
|
Remove address from GPLv2 headers
As per discussion with lawyers[tm], it's not a good idea to
shorten the license header too much - not for legal reasons
but because there are tools that look for them, and giving
them a standard pattern simplifies things.
However, we got confirmation that we don't have to update
every file ever added to coreboot whenever the FSF gets a
new lease, but can drop the address instead.
util/kconfig is excluded because that's imported code that
we may want to synchronize every now and then.
$ find * -type f -exec sed -i "s:Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, *MA[, ]*02110-1301[, ]*USA:Foundation, Inc.:" {} +
$ find * -type f -exec sed -i "s:Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA:Foundation, Inc.:" {} +
$ find * -type f -exec sed -i "s:Foundation, Inc., 59 Temple Place[-, ]*Suite 330, Boston, MA *02111-1307[, ]*USA:Foundation, Inc.:" {} +
$ find * -type f -exec sed -i "s:Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.:Foundation, Inc.:" {} +
$ find * -type f
-a \! -name \*.patch \
-a \! -name \*_shipped \
-a \! -name LICENSE_GPL \
-a \! -name LGPL.txt \
-a \! -name COPYING \
-a \! -name DISCLAIMER \
-exec sed -i "/Foundation, Inc./ N;s:Foundation, Inc.* USA\.* *:Foundation, Inc. :;s:Foundation, Inc. $:Foundation, Inc.:" {} +
Change-Id: Icc968a5a5f3a5df8d32b940f9cdb35350654bef9
Signed-off-by: Patrick Georgi <pgeorgi@chromium.org>
Reviewed-on: http://review.coreboot.org/9233
Tested-by: build bot (Jenkins)
Reviewed-by: Vladimir Serbinenko <phcoder@gmail.com>
2015-03-26 15:17:45 +01:00
|
|
|
* Foundation, Inc.
|
2009-04-01 13:03:32 +02:00
|
|
|
*/
|
|
|
|
|
2009-04-01 12:48:39 +02:00
|
|
|
#include <console/console.h>
|
2013-03-29 22:23:23 +01:00
|
|
|
#include <cpu/cpu.h>
|
Unify byte order macros and clrsetbits
This patch removes quite a bit of code duplication between cpu_to_le32()
and clrsetbits_le32() style macros on the different architectures. This
also syncs those macros back up to the new write32(a, v) style IO
accessor macros that are now used on ARM and ARM64.
CQ-DEPEND=CL:254862
BRANCH=none
BUG=chromium:444723
TEST=Compiled Cosmos, Daisy, Blaze, Falco, Pinky, Pit, Rambi, Ryu,
Storm and Urara. Booted on Jerry. Tried to compare binary images...
unfortunately something about the new macro notation makes the compiler
evaluate it more efficiently (not recalculating the address between the
read and the write), so this was of limited value.
Change-Id: If8ab62912c952d68a67a0f71e82b038732cd1317
Signed-off-by: Patrick Georgi <pgeorgi@chromium.org>
Original-Commit-Id: fd43bf446581bfb84bec4f2ebb56b5de95971c3b
Original-Change-Id: I7d301b5bb5ac0db7f5ff39e3adc2b28a1f402a72
Original-Signed-off-by: Julius Werner <jwerner@chromium.org>
Original-Reviewed-on: https://chromium-review.googlesource.com/254866
Original-Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-on: http://review.coreboot.org/9838
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Reinauer <stefan.reinauer@coreboot.org>
2015-02-23 23:31:09 +01:00
|
|
|
#include <endian.h>
|
2009-04-01 12:48:39 +02:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
New mechanism to define SRAM/memory map with automatic bounds checking
This patch creates a new mechanism to define the static memory layout
(primarily in SRAM) for a given board, superseding the brittle mass of
Kconfigs that we were using before. The core part is a memlayout.ld file
in the mainboard directory (although boards are expected to just include
the SoC default in most cases), which is the primary linker script for
all stages (though not rmodules for now). It uses preprocessor macros
from <memlayout.h> to form a different valid linker script for all
stages while looking like a declarative, boilerplate-free map of memory
addresses to the programmer. Linker asserts will automatically guarantee
that the defined regions cannot overlap. Stages are defined with a
maximum size that will be enforced by the linker. The file serves to
both define and document the memory layout, so that the documentation
cannot go missing or out of date.
The mechanism is implemented for all boards in the ARM, ARM64 and MIPS
architectures, and should be extended onto all systems using SRAM in the
future. The CAR/XIP environment on x86 has very different requirements
and the layout is generally not as static, so it will stay like it is
and be unaffected by this patch (save for aligning some symbol names for
consistency and sharing the new common ramstage linker script include).
BUG=None
TEST=Booted normally and in recovery mode, checked suspend/resume and
the CBMEM console on Falco, Blaze (both normal and vboot2), Pinky and
Pit. Compiled Ryu, Storm and Urara, manually compared the disassemblies
with ToT and looked for red flags.
Change-Id: Ifd2276417f2036cbe9c056f17e42f051bcd20e81
Signed-off-by: Patrick Georgi <pgeorgi@chromium.org>
Original-Commit-Id: f1e2028e7ebceeb2d71ff366150a37564595e614
Original-Change-Id: I005506add4e8fcdb74db6d5e6cb2d4cb1bd3cda5
Original-Signed-off-by: Julius Werner <jwerner@chromium.org>
Original-Reviewed-on: https://chromium-review.googlesource.com/213370
Reviewed-on: http://review.coreboot.org/9283
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Tauner <stefan.tauner@gmx.at>
Reviewed-by: Aaron Durbin <adurbin@google.com>
2014-08-21 00:29:56 +02:00
|
|
|
#include <symbols.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>
|
2015-03-17 17:43:44 +01:00
|
|
|
#include <program_loading.h>
|
2009-04-01 12:48:39 +02:00
|
|
|
|
New mechanism to define SRAM/memory map with automatic bounds checking
This patch creates a new mechanism to define the static memory layout
(primarily in SRAM) for a given board, superseding the brittle mass of
Kconfigs that we were using before. The core part is a memlayout.ld file
in the mainboard directory (although boards are expected to just include
the SoC default in most cases), which is the primary linker script for
all stages (though not rmodules for now). It uses preprocessor macros
from <memlayout.h> to form a different valid linker script for all
stages while looking like a declarative, boilerplate-free map of memory
addresses to the programmer. Linker asserts will automatically guarantee
that the defined regions cannot overlap. Stages are defined with a
maximum size that will be enforced by the linker. The file serves to
both define and document the memory layout, so that the documentation
cannot go missing or out of date.
The mechanism is implemented for all boards in the ARM, ARM64 and MIPS
architectures, and should be extended onto all systems using SRAM in the
future. The CAR/XIP environment on x86 has very different requirements
and the layout is generally not as static, so it will stay like it is
and be unaffected by this patch (save for aligning some symbol names for
consistency and sharing the new common ramstage linker script include).
BUG=None
TEST=Booted normally and in recovery mode, checked suspend/resume and
the CBMEM console on Falco, Blaze (both normal and vboot2), Pinky and
Pit. Compiled Ryu, Storm and Urara, manually compared the disassemblies
with ToT and looked for red flags.
Change-Id: Ifd2276417f2036cbe9c056f17e42f051bcd20e81
Signed-off-by: Patrick Georgi <pgeorgi@chromium.org>
Original-Commit-Id: f1e2028e7ebceeb2d71ff366150a37564595e614
Original-Change-Id: I005506add4e8fcdb74db6d5e6cb2d4cb1bd3cda5
Original-Signed-off-by: Julius Werner <jwerner@chromium.org>
Original-Reviewed-on: https://chromium-review.googlesource.com/213370
Reviewed-on: http://review.coreboot.org/9283
Tested-by: build bot (Jenkins)
Reviewed-by: Stefan Tauner <stefan.tauner@gmx.at>
Reviewed-by: Aaron Durbin <adurbin@google.com>
2014-08-21 00:29:56 +02:00
|
|
|
static const unsigned long lb_start = (unsigned long)&_program;
|
|
|
|
static const unsigned long lb_end = (unsigned long)&_eprogram;
|
2011-11-07 21:43:03 +01:00
|
|
|
|
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,
|
2015-05-16 06:39:23 +02:00
|
|
|
struct cbfs_payload *cbfs_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;
|
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;
|
2014-05-14 00:43:58 +02:00
|
|
|
printk(BIOS_DEBUG,
|
|
|
|
" cleaned up filesize 0x%lx\n",
|
|
|
|
new->s_filesz);
|
2009-07-24 00:03:14 +02:00
|
|
|
}
|
|
|
|
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;
|
2014-02-28 23:27:37 +01:00
|
|
|
new->s_srcaddr = (uintptr_t)
|
|
|
|
((unsigned char *)first_segment)
|
|
|
|
+ ntohl(segment->offset);
|
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,
|
2015-03-20 22:37:12 +01:00
|
|
|
struct prog *payload)
|
2009-04-01 12:48:39 +02:00
|
|
|
{
|
|
|
|
struct segment *ptr;
|
2015-03-20 15:42:05 +01:00
|
|
|
struct segment *last_non_empty;
|
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
|
|
|
|
2015-03-20 15:42:05 +01:00
|
|
|
/* Determine last non-empty loaded segment. */
|
|
|
|
last_non_empty = NULL;
|
|
|
|
for(ptr = head->next; ptr != head; ptr = ptr->next)
|
|
|
|
if (ptr->s_filesz != 0)
|
|
|
|
last_non_empty = ptr;
|
|
|
|
|
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
|
|
|
|
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);
|
|
|
|
}
|
2014-04-22 19:41:05 +02:00
|
|
|
/* Copy the data that's outside the area that shadows ramstage */
|
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
|
|
|
}
|
|
|
|
}
|
2015-01-09 14:14:20 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Each architecture can perform additonal operations
|
|
|
|
* on the loaded segment
|
|
|
|
*/
|
2015-03-20 15:42:05 +01:00
|
|
|
arch_segment_loaded((uintptr_t)dest, ptr->s_memsz,
|
|
|
|
last_non_empty == ptr ? SEG_FINAL : 0);
|
2009-04-01 12:48:39 +02:00
|
|
|
}
|
|
|
|
}
|
2015-01-09 14:14:20 +01:00
|
|
|
|
2009-04-01 12:48:39 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-03-20 22:37:12 +01:00
|
|
|
void *selfload(struct prog *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;
|
2015-05-16 06:39:23 +02:00
|
|
|
void *data;
|
|
|
|
|
|
|
|
data = rdev_mmap_full(&payload->rdev);
|
|
|
|
|
|
|
|
if (data == NULL)
|
|
|
|
return NULL;
|
2009-04-01 12:48:39 +02:00
|
|
|
|
|
|
|
/* Preprocess the self segments */
|
2015-05-16 06:39:23 +02:00
|
|
|
if (!build_self_segment_list(&head, data, &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
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
rdev_munmap(&payload->rdev, data);
|
|
|
|
|
|
|
|
/* Update the payload's area with the bounce buffer information. */
|
|
|
|
prog_set_area(payload, (void *)(uintptr_t)bounce_buffer, bounce_size);
|
|
|
|
|
2013-04-25 05:59:45 +02:00
|
|
|
return (void *)entry;
|
|
|
|
|
|
|
|
out:
|
2015-05-16 06:39:23 +02:00
|
|
|
rdev_munmap(&payload->rdev, data);
|
2013-04-25 05:59:45 +02:00
|
|
|
return NULL;
|
|
|
|
}
|