2012-12-24 21:28:37 +01:00
|
|
|
/*
|
|
|
|
* This file is part of the coreboot project.
|
|
|
|
*
|
2018-09-26 20:46:04 +02:00
|
|
|
* Copyright (C) 2012 Google LLC
|
2012-12-24 21:28:37 +01:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2013-02-28 05:50:12 +01:00
|
|
|
#include <assert.h>
|
2014-11-28 10:24:19 +01:00
|
|
|
#include <cbmem.h>
|
2015-03-27 03:04:18 +01:00
|
|
|
#include <cbfs.h>
|
2012-12-24 21:28:37 +01:00
|
|
|
#include <stdint.h>
|
2013-02-09 00:05:36 +01:00
|
|
|
#include <stdlib.h>
|
2012-12-24 21:28:37 +01:00
|
|
|
#include <string.h>
|
|
|
|
#include <console/console.h>
|
2015-01-09 14:14:20 +01:00
|
|
|
#include <program_loading.h>
|
2012-12-24 21:28:37 +01:00
|
|
|
#include <rmodule.h>
|
|
|
|
|
|
|
|
/* Change this define to get more verbose debugging for module loading. */
|
|
|
|
#define PK_ADJ_LEVEL BIOS_NEVER
|
|
|
|
|
|
|
|
static inline int rmodule_is_loaded(const struct rmodule *module)
|
|
|
|
{
|
|
|
|
return module->location != NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Calculate a loaded program address based on the blob address. */
|
|
|
|
static inline void *rmodule_load_addr(const struct rmodule *module,
|
2017-03-10 01:21:34 +01:00
|
|
|
uintptr_t blob_addr)
|
2012-12-24 21:28:37 +01:00
|
|
|
{
|
|
|
|
char *loc = module->location;
|
|
|
|
return &loc[blob_addr - module->header->module_link_start_address];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Initialize a rmodule structure based on raw data. */
|
|
|
|
int rmodule_parse(void *ptr, struct rmodule *module)
|
|
|
|
{
|
|
|
|
char *base;
|
|
|
|
struct rmodule_header *rhdr;
|
|
|
|
|
|
|
|
base = ptr;
|
|
|
|
rhdr = ptr;
|
|
|
|
|
|
|
|
if (rhdr == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Sanity check the raw data. */
|
|
|
|
if (rhdr->magic != RMODULE_MAGIC)
|
|
|
|
return -1;
|
|
|
|
if (rhdr->version != RMODULE_VERSION_1)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Indicate the module hasn't been loaded yet. */
|
|
|
|
module->location = NULL;
|
|
|
|
|
|
|
|
/* The rmodule only needs a reference to the reloc_header. */
|
|
|
|
module->header = rhdr;
|
|
|
|
|
|
|
|
/* The payload lives after the header. */
|
|
|
|
module->payload = &base[rhdr->payload_begin_offset];
|
|
|
|
module->payload_size = rhdr->payload_end_offset -
|
2017-03-10 01:21:34 +01:00
|
|
|
rhdr->payload_begin_offset;
|
2012-12-24 21:28:37 +01:00
|
|
|
module->relocations = &base[rhdr->relocations_begin_offset];
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rmodule_memory_size(const struct rmodule *module)
|
|
|
|
{
|
|
|
|
return module->header->module_program_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *rmodule_parameters(const struct rmodule *module)
|
|
|
|
{
|
|
|
|
if (!rmodule_is_loaded(module))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Indicate if there are no parameters. */
|
|
|
|
if (module->header->parameters_begin == module->header->parameters_end)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return rmodule_load_addr(module, module->header->parameters_begin);
|
|
|
|
}
|
|
|
|
|
|
|
|
int rmodule_entry_offset(const struct rmodule *module)
|
|
|
|
{
|
|
|
|
return module->header->module_entry_point -
|
|
|
|
module->header->module_link_start_address;
|
|
|
|
}
|
|
|
|
|
|
|
|
void *rmodule_entry(const struct rmodule *module)
|
|
|
|
{
|
|
|
|
if (!rmodule_is_loaded(module))
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return rmodule_load_addr(module, module->header->module_entry_point);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rmodule_clear_bss(struct rmodule *module)
|
|
|
|
{
|
|
|
|
char *begin;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
begin = rmodule_load_addr(module, module->header->bss_begin);
|
|
|
|
size = module->header->bss_end - module->header->bss_begin;
|
|
|
|
memset(begin, 0, size);
|
|
|
|
}
|
|
|
|
|
2014-03-10 22:13:58 +01:00
|
|
|
static inline size_t rmodule_number_relocations(const struct rmodule *module)
|
2012-12-24 21:28:37 +01:00
|
|
|
{
|
2014-03-10 22:13:58 +01:00
|
|
|
size_t r;
|
2012-12-24 21:28:37 +01:00
|
|
|
|
|
|
|
r = module->header->relocations_end_offset;
|
|
|
|
r -= module->header->relocations_begin_offset;
|
2014-03-10 22:13:58 +01:00
|
|
|
r /= sizeof(uintptr_t);
|
2012-12-24 21:28:37 +01:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void rmodule_copy_payload(const struct rmodule *module)
|
|
|
|
{
|
|
|
|
printk(BIOS_DEBUG, "Loading module at %p with entry %p. "
|
|
|
|
"filesize: 0x%x memsize: 0x%x\n",
|
|
|
|
module->location, rmodule_entry(module),
|
|
|
|
module->payload_size, rmodule_memory_size(module));
|
2013-02-09 00:05:36 +01:00
|
|
|
|
|
|
|
/* No need to copy the payload if the load location and the
|
|
|
|
* payload location are the same. */
|
|
|
|
if (module->location == module->payload)
|
|
|
|
return;
|
|
|
|
|
2012-12-24 21:28:37 +01:00
|
|
|
memcpy(module->location, module->payload, module->payload_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int rmodule_relocate(const struct rmodule *module)
|
|
|
|
{
|
2014-03-10 22:13:58 +01:00
|
|
|
size_t num_relocations;
|
|
|
|
const uintptr_t *reloc;
|
|
|
|
uintptr_t adjustment;
|
2012-12-24 21:28:37 +01:00
|
|
|
|
|
|
|
/* Each relocation needs to be adjusted relative to the beginning of
|
|
|
|
* the loaded program. */
|
2014-03-10 22:13:58 +01:00
|
|
|
adjustment = (uintptr_t)rmodule_load_addr(module, 0);
|
2012-12-24 21:28:37 +01:00
|
|
|
|
|
|
|
reloc = module->relocations;
|
|
|
|
num_relocations = rmodule_number_relocations(module);
|
|
|
|
|
2014-07-22 19:59:28 +02:00
|
|
|
printk(BIOS_DEBUG, "Processing %zu relocs. Offset value of 0x%08lx\n",
|
|
|
|
num_relocations, (unsigned long)adjustment);
|
2012-12-24 21:28:37 +01:00
|
|
|
|
|
|
|
while (num_relocations > 0) {
|
2014-03-10 22:13:58 +01:00
|
|
|
uintptr_t *adjust_loc;
|
2012-12-24 21:28:37 +01:00
|
|
|
|
|
|
|
/* If the adjustment location is non-NULL adjust it. */
|
2014-03-10 22:13:58 +01:00
|
|
|
adjust_loc = rmodule_load_addr(module, *reloc);
|
2014-07-22 19:59:28 +02:00
|
|
|
printk(PK_ADJ_LEVEL, "Adjusting %p: 0x%08lx -> 0x%08lx\n",
|
|
|
|
adjust_loc, (unsigned long) *adjust_loc,
|
2014-07-23 13:44:09 +02:00
|
|
|
(unsigned long) (*adjust_loc + adjustment));
|
2018-06-25 17:51:05 +02:00
|
|
|
*adjust_loc += adjustment;
|
2012-12-24 21:28:37 +01:00
|
|
|
|
2014-03-10 22:13:58 +01:00
|
|
|
reloc++;
|
2012-12-24 21:28:37 +01:00
|
|
|
num_relocations--;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int rmodule_load_alignment(const struct rmodule *module)
|
|
|
|
{
|
|
|
|
/* The load alignment is the start of the program's linked address.
|
|
|
|
* The base address where the program is loaded needs to be a multiple
|
|
|
|
* of the program's starting link address. That way all data alignment
|
2014-03-10 22:13:58 +01:00
|
|
|
* in the program is preserved. Default to 4KiB. */
|
|
|
|
return 4096;
|
2012-12-24 21:28:37 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int rmodule_load(void *base, struct rmodule *module)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* In order to load the module at a given address, the following steps
|
|
|
|
* take place:
|
|
|
|
* 1. Copy payload to base address.
|
2013-03-02 00:00:39 +01:00
|
|
|
* 2. Adjust relocations within the module to new base address.
|
|
|
|
* 3. Clear the bss segment last since the relocations live where
|
|
|
|
* the bss is. If an rmodule is being loaded from its load
|
|
|
|
* address the relocations need to be processed before the bss.
|
2012-12-24 21:28:37 +01:00
|
|
|
*/
|
|
|
|
module->location = base;
|
|
|
|
rmodule_copy_payload(module);
|
2013-03-02 00:00:39 +01:00
|
|
|
if (rmodule_relocate(module))
|
|
|
|
return -1;
|
2012-12-24 21:28:37 +01:00
|
|
|
rmodule_clear_bss(module);
|
2014-03-25 21:31:00 +01:00
|
|
|
|
2016-03-31 20:49:00 +02:00
|
|
|
prog_segment_loaded((uintptr_t)module->location,
|
2015-03-20 15:42:05 +01:00
|
|
|
rmodule_memory_size(module), SEG_FINAL);
|
2014-03-25 21:31:00 +01:00
|
|
|
|
2013-03-02 00:00:39 +01:00
|
|
|
return 0;
|
2012-12-24 21:28:37 +01:00
|
|
|
}
|
|
|
|
|
2013-02-28 05:50:12 +01:00
|
|
|
int rmodule_calc_region(unsigned int region_alignment, size_t rmodule_size,
|
2017-03-10 01:21:34 +01:00
|
|
|
size_t *region_size, int *load_offset)
|
2013-02-09 00:05:36 +01:00
|
|
|
{
|
2013-02-28 05:50:12 +01:00
|
|
|
/* region_alignment must be a power of 2. */
|
|
|
|
if (region_alignment & (region_alignment - 1))
|
|
|
|
BUG();
|
2013-02-09 00:05:36 +01:00
|
|
|
|
2013-02-28 05:50:12 +01:00
|
|
|
if (region_alignment < 4096)
|
|
|
|
region_alignment = 4096;
|
|
|
|
|
|
|
|
/* Sanity check rmodule_header size. The code below assumes it is less
|
|
|
|
* than the minimum alignment required. */
|
|
|
|
if (region_alignment < sizeof(struct rmodule_header))
|
|
|
|
BUG();
|
|
|
|
|
|
|
|
/* Place the rmodule according to alignment. The rmodule files
|
2013-02-09 00:05:36 +01:00
|
|
|
* themselves are packed as a header and a payload, however the rmodule
|
|
|
|
* itself is linked along with the header. The header starts at address
|
|
|
|
* 0. Immediately following the header in the file is the program,
|
|
|
|
* however its starting address is determined by the rmodule linker
|
|
|
|
* script. In short, sizeof(struct rmodule_header) can be less than
|
|
|
|
* or equal to the linked address of the program. Therefore we want
|
|
|
|
* to place the rmodule so that the program falls on the aligned
|
|
|
|
* address with the header just before it. Therefore, we need at least
|
|
|
|
* a page to account for the size of the header. */
|
2013-02-28 05:50:12 +01:00
|
|
|
*region_size = ALIGN(rmodule_size + region_alignment, 4096);
|
2013-02-09 00:05:36 +01:00
|
|
|
/* The program starts immediately after the header. However,
|
|
|
|
* it needs to be aligned to a 4KiB boundary. Therefore, adjust the
|
|
|
|
* program location so that the program lands on a page boundary. The
|
|
|
|
* layout looks like the following:
|
|
|
|
*
|
2013-02-28 05:50:12 +01:00
|
|
|
* +--------------------------------+ region_alignment + region_size
|
2013-02-09 00:05:36 +01:00
|
|
|
* | >= 0 bytes from alignment |
|
|
|
|
* +--------------------------------+ program end (4KiB aligned)
|
|
|
|
* | program size |
|
|
|
|
* +--------------------------------+ program_begin (4KiB aligned)
|
|
|
|
* | sizeof(struct rmodule_header) |
|
|
|
|
* +--------------------------------+ rmodule header start
|
|
|
|
* | >= 0 bytes from alignment |
|
2013-02-28 05:50:12 +01:00
|
|
|
* +--------------------------------+ region_alignment
|
2013-02-09 00:05:36 +01:00
|
|
|
*/
|
2013-02-28 05:50:12 +01:00
|
|
|
*load_offset = region_alignment;
|
2013-02-09 00:05:36 +01:00
|
|
|
|
2013-02-28 05:50:12 +01:00
|
|
|
return region_alignment - sizeof(struct rmodule_header);
|
2013-02-09 00:05:36 +01:00
|
|
|
}
|
2013-10-24 17:14:06 +02:00
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
int rmodule_stage_load(struct rmod_stage_load *rsl)
|
2013-10-24 17:14:06 +02:00
|
|
|
{
|
|
|
|
struct rmodule rmod_stage;
|
|
|
|
size_t region_size;
|
|
|
|
char *stage_region;
|
|
|
|
int rmodule_offset;
|
|
|
|
int load_offset;
|
2015-05-16 06:39:23 +02:00
|
|
|
struct cbfs_stage stage;
|
|
|
|
void *rmod_loc;
|
|
|
|
struct region_device *fh;
|
2013-10-24 17:14:06 +02:00
|
|
|
|
2015-05-20 19:08:55 +02:00
|
|
|
if (rsl->prog == NULL || prog_name(rsl->prog) == NULL)
|
2015-05-16 06:39:23 +02:00
|
|
|
return -1;
|
|
|
|
|
2015-05-20 19:08:55 +02:00
|
|
|
fh = prog_rdev(rsl->prog);
|
2015-05-16 06:39:23 +02:00
|
|
|
|
|
|
|
if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
|
2013-10-24 17:14:06 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
rmodule_offset =
|
|
|
|
rmodule_calc_region(DYN_CBMEM_ALIGN_SIZE,
|
2017-03-10 01:21:34 +01:00
|
|
|
stage.memlen, ®ion_size, &load_offset);
|
2013-10-24 17:14:06 +02:00
|
|
|
|
2015-04-07 00:18:18 +02:00
|
|
|
stage_region = cbmem_add(rsl->cbmem_id, region_size);
|
2013-10-24 17:14:06 +02:00
|
|
|
|
2015-04-07 00:18:18 +02:00
|
|
|
if (stage_region == NULL)
|
2013-10-24 17:14:06 +02:00
|
|
|
return -1;
|
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
rmod_loc = &stage_region[rmodule_offset];
|
|
|
|
|
2013-10-24 17:14:06 +02:00
|
|
|
printk(BIOS_INFO, "Decompressing stage %s @ 0x%p (%d bytes)\n",
|
2015-05-20 19:08:55 +02:00
|
|
|
prog_name(rsl->prog), rmod_loc, stage.memlen);
|
2015-05-16 06:39:23 +02:00
|
|
|
|
2015-09-29 22:51:35 +02:00
|
|
|
if (!cbfs_load_and_decompress(fh, sizeof(stage), stage.len, rmod_loc,
|
|
|
|
stage.memlen, stage.compression))
|
2013-10-24 17:14:06 +02:00
|
|
|
return -1;
|
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
if (rmodule_parse(rmod_loc, &rmod_stage))
|
2013-10-24 17:14:06 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (rmodule_load(&stage_region[load_offset], &rmod_stage))
|
|
|
|
return -1;
|
|
|
|
|
2015-04-07 00:18:18 +02:00
|
|
|
prog_set_area(rsl->prog, rmod_stage.location,
|
|
|
|
rmodule_memory_size(&rmod_stage));
|
2013-10-24 17:14:06 +02:00
|
|
|
|
2016-03-18 05:13:34 +01:00
|
|
|
/* Allow caller to pick up parameters, if available. */
|
|
|
|
rsl->params = rmodule_parameters(&rmod_stage);
|
|
|
|
|
2017-09-05 21:43:05 +02:00
|
|
|
prog_set_entry(rsl->prog, rmodule_entry(&rmod_stage), rsl->params);
|
|
|
|
|
2013-10-24 17:14:06 +02:00
|
|
|
return 0;
|
|
|
|
}
|