2009-04-14 09:40:01 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the coreboot project.
|
|
|
|
*
|
2015-05-16 06:39:23 +02:00
|
|
|
* Copyright (C) 2011 secunet Security Networks AG
|
|
|
|
* Copyright 2015 Google Inc.
|
2009-04-14 09:40:01 +02: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.
|
|
|
|
*/
|
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
#include <assert.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <boot_device.h>
|
|
|
|
#include <cbfs.h>
|
2015-09-29 22:51:35 +02:00
|
|
|
#include <commonlib/compression.h>
|
2015-05-16 06:39:23 +02:00
|
|
|
#include <endian.h>
|
|
|
|
#include <lib.h>
|
|
|
|
#include <symbols.h>
|
2015-09-29 22:51:35 +02:00
|
|
|
#include <timestamp.h>
|
2016-12-01 02:29:10 +01:00
|
|
|
#include <fmap.h>
|
2016-05-02 11:22:29 +02:00
|
|
|
#include "fmap_config.h"
|
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
#define ERROR(x...) printk(BIOS_ERR, "CBFS: " x)
|
|
|
|
#define LOG(x...) printk(BIOS_INFO, "CBFS: " x)
|
|
|
|
#if IS_ENABLED(CONFIG_DEBUG_CBFS)
|
|
|
|
#define DEBUG(x...) printk(BIOS_SPEW, "CBFS: " x)
|
|
|
|
#else
|
|
|
|
#define DEBUG(x...)
|
|
|
|
#endif
|
2014-11-11 02:22:04 +01:00
|
|
|
|
2015-09-17 23:09:30 +02:00
|
|
|
int cbfs_boot_locate(struct cbfsf *fh, const char *name, uint32_t *type)
|
2009-08-20 16:48:03 +02:00
|
|
|
{
|
2015-05-16 06:39:23 +02:00
|
|
|
struct region_device rdev;
|
|
|
|
const struct region_device *boot_dev;
|
|
|
|
struct cbfs_props props;
|
2009-08-20 16:48:03 +02:00
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
if (cbfs_boot_region_properties(&props))
|
|
|
|
return -1;
|
2009-04-14 09:40:01 +02:00
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
/* All boot CBFS operations are performed using the RO devie. */
|
|
|
|
boot_dev = boot_device_ro();
|
2009-04-14 09:40:01 +02:00
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
if (boot_dev == NULL)
|
|
|
|
return -1;
|
2009-04-14 09:40:01 +02:00
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
if (rdev_chain(&rdev, boot_dev, props.offset, props.size))
|
|
|
|
return -1;
|
2009-04-14 09:40:01 +02:00
|
|
|
|
2015-09-17 23:09:30 +02:00
|
|
|
return cbfs_locate(fh, &rdev, name, type);
|
2015-05-16 06:39:23 +02:00
|
|
|
}
|
2009-04-14 09:40:01 +02:00
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
void *cbfs_boot_map_with_leak(const char *name, uint32_t type, size_t *size)
|
|
|
|
{
|
2015-09-17 23:09:30 +02:00
|
|
|
struct cbfsf fh;
|
2015-05-16 06:39:23 +02:00
|
|
|
size_t fsize;
|
2009-04-14 09:40:01 +02:00
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
if (cbfs_boot_locate(&fh, name, &type))
|
2009-04-14 09:40:01 +02:00
|
|
|
return NULL;
|
|
|
|
|
2015-09-17 23:09:30 +02:00
|
|
|
fsize = region_device_sz(&fh.data);
|
2015-05-16 06:39:23 +02:00
|
|
|
|
|
|
|
if (size != NULL)
|
|
|
|
*size = fsize;
|
|
|
|
|
2015-09-17 23:09:30 +02:00
|
|
|
return rdev_mmap(&fh.data, 0, fsize);
|
2009-04-14 09:40:01 +02:00
|
|
|
}
|
|
|
|
|
2016-12-01 02:29:10 +01:00
|
|
|
int cbfs_locate_file_in_region(struct cbfsf *fh, const char *region_name,
|
|
|
|
const char *name, uint32_t *type)
|
|
|
|
{
|
|
|
|
struct region_device rdev;
|
|
|
|
|
|
|
|
if (fmap_locate_area_as_rdev(region_name, &rdev)) {
|
|
|
|
LOG("%s region not found while looking for %s\n",
|
|
|
|
region_name, name);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cbfs_locate(fh, &rdev, name, type);
|
|
|
|
}
|
|
|
|
|
2015-09-29 22:51:35 +02:00
|
|
|
size_t cbfs_load_and_decompress(const struct region_device *rdev, size_t offset,
|
|
|
|
size_t in_size, void *buffer, size_t buffer_size, uint32_t compression)
|
2014-09-19 18:56:15 +02:00
|
|
|
{
|
2015-09-29 22:51:35 +02:00
|
|
|
size_t out_size;
|
|
|
|
|
|
|
|
switch (compression) {
|
|
|
|
case CBFS_COMPRESS_NONE:
|
2016-08-20 00:43:06 +02:00
|
|
|
if (buffer_size < in_size)
|
|
|
|
return 0;
|
2015-09-29 22:51:35 +02:00
|
|
|
if (rdev_readat(rdev, buffer, offset, in_size) != in_size)
|
|
|
|
return 0;
|
|
|
|
return in_size;
|
|
|
|
|
|
|
|
case CBFS_COMPRESS_LZ4:
|
|
|
|
if ((ENV_BOOTBLOCK || ENV_VERSTAGE) &&
|
|
|
|
!IS_ENABLED(CONFIG_COMPRESS_PRERAM_STAGES))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Load the compressed image to the end of the available memory
|
|
|
|
* area for in-place decompression. It is the responsibility of
|
|
|
|
* the caller to ensure that buffer_size is large enough
|
|
|
|
* (see compression.h, guaranteed by cbfstool for stages). */
|
|
|
|
void *compr_start = buffer + buffer_size - in_size;
|
|
|
|
if (rdev_readat(rdev, compr_start, offset, in_size) != in_size)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
timestamp_add_now(TS_START_ULZ4F);
|
|
|
|
out_size = ulz4fn(compr_start, in_size, buffer, buffer_size);
|
|
|
|
timestamp_add_now(TS_END_ULZ4F);
|
|
|
|
return out_size;
|
|
|
|
|
|
|
|
case CBFS_COMPRESS_LZMA:
|
|
|
|
if (ENV_BOOTBLOCK || ENV_VERSTAGE)
|
|
|
|
return 0;
|
2016-07-25 18:53:35 +02:00
|
|
|
if ((ENV_ROMSTAGE || ENV_POSTCAR)
|
|
|
|
&& !IS_ENABLED(CONFIG_COMPRESS_RAMSTAGE))
|
2015-09-29 22:51:35 +02:00
|
|
|
return 0;
|
|
|
|
void *map = rdev_mmap(rdev, offset, in_size);
|
|
|
|
if (map == NULL)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Note: timestamp not useful for memory-mapped media (x86) */
|
|
|
|
timestamp_add_now(TS_START_ULZMA);
|
|
|
|
out_size = ulzman(map, in_size, buffer, buffer_size);
|
|
|
|
timestamp_add_now(TS_END_ULZMA);
|
|
|
|
|
|
|
|
rdev_munmap(rdev, map);
|
|
|
|
|
|
|
|
return out_size;
|
|
|
|
|
|
|
|
default:
|
2015-05-16 06:39:23 +02:00
|
|
|
return 0;
|
2015-09-29 22:51:35 +02:00
|
|
|
}
|
2015-05-16 06:39:23 +02:00
|
|
|
}
|
2015-03-20 19:00:20 +01:00
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
static inline int tohex4(unsigned int c)
|
|
|
|
{
|
|
|
|
return (c <= 9) ? (c + '0') : (c - 10 + 'a');
|
|
|
|
}
|
2015-03-20 19:00:20 +01:00
|
|
|
|
2017-03-09 01:52:22 +01:00
|
|
|
static void tohex16(unsigned int val, char *dest)
|
2015-05-16 06:39:23 +02:00
|
|
|
{
|
|
|
|
dest[0] = tohex4(val>>12);
|
|
|
|
dest[1] = tohex4((val>>8) & 0xf);
|
|
|
|
dest[2] = tohex4((val>>4) & 0xf);
|
|
|
|
dest[3] = tohex4(val & 0xf);
|
2015-03-20 19:00:20 +01:00
|
|
|
}
|
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
void *cbfs_boot_map_optionrom(uint16_t vendor, uint16_t device)
|
2015-03-20 19:00:20 +01:00
|
|
|
{
|
2015-05-16 06:39:23 +02:00
|
|
|
char name[17] = "pciXXXX,XXXX.rom";
|
2015-03-20 19:00:20 +01:00
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
tohex16(vendor, name+3);
|
|
|
|
tohex16(device, name+8);
|
2015-03-20 19:00:20 +01:00
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
return cbfs_boot_map_with_leak(name, CBFS_TYPE_OPTIONROM, NULL);
|
2015-03-20 19:00:20 +01:00
|
|
|
}
|
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
void *cbfs_boot_load_stage_by_name(const char *name)
|
2015-03-20 19:00:20 +01:00
|
|
|
{
|
2015-09-17 23:09:30 +02:00
|
|
|
struct cbfsf fh;
|
2015-12-08 21:34:35 +01:00
|
|
|
struct prog stage = PROG_INIT(PROG_UNKNOWN, name);
|
2015-05-16 06:39:23 +02:00
|
|
|
uint32_t type = CBFS_TYPE_STAGE;
|
2015-03-20 19:00:20 +01:00
|
|
|
|
2015-09-17 23:09:30 +02:00
|
|
|
if (cbfs_boot_locate(&fh, name, &type))
|
2015-05-16 06:39:23 +02:00
|
|
|
return NULL;
|
2009-10-26 18:15:53 +01:00
|
|
|
|
2015-09-17 23:09:30 +02:00
|
|
|
/* Chain data portion in the prog. */
|
|
|
|
cbfs_file_data(prog_rdev(&stage), &fh);
|
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
if (cbfs_prog_stage_load(&stage))
|
|
|
|
return NULL;
|
2009-04-14 09:40:01 +02:00
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
return prog_entry(&stage);
|
Extend CBFS to support arbitrary ROM source media.
Summary:
Isolate CBFS underlying I/O to board/arch-specific implementations as
"media stream", to allow loading and booting romstage on non-x86.
CBFS functions now all take a new "media source" parameter; use
CBFS_DEFAULT_MEDIA if you simply want to load from main firmware.
API Changes:
cbfs_find => cbfs_get_file.
cbfs_find_file => cbfs_get_file_content.
cbfs_get_file => cbfs_get_file_content with correct type.
CBFS used to work only on memory-mapped ROM (all x86). For platforms like ARM,
the ROM may come from USB, UART, or SPI -- any serial devices and not available
for memory mapping.
To support these devices (and allowing CBFS to read from multiple source
at the same time), CBFS operations are now virtual-ized into "cbfs_media". To
simplify porting existing code, every media source must support both "reading
into pre-allocated memory (read)" and "read and return an allocated buffer
(map)". For devices without native memory-mapped ROM, "cbfs_simple_buffer*"
provides simple memory mapping simulation.
Every CBFS function now takes a cbfs_media* as parameter. CBFS_DEFAULT_MEDIA
is defined for CBFS functions to automatically initialize a per-board default
media (CBFS will internally calls init_default_cbfs_media). Also revised CBFS
function names relying on memory mapped backend (ex, "cbfs_find" => actually
loads files). Now we only have two getters:
struct cbfs_file *entry = cbfs_get_file(media, name);
void *data = cbfs_get_file_content(CBFS_DEFAULT_MEDIA, name, type);
Test results:
- Verified to work on x86/qemu.
- Compiles on ARM, and follow up commit will provide working SPI driver.
Change-Id: Iac911ded25a6f2feffbf3101a81364625bb07746
Signed-off-by: Hung-Te Lin <hungte@chromium.org>
Reviewed-on: http://review.coreboot.org/2182
Tested-by: build bot (Jenkins)
Reviewed-by: Ronald G. Minnich <rminnich@gmail.com>
2013-01-22 11:57:56 +01:00
|
|
|
}
|
|
|
|
|
2016-08-20 00:43:06 +02:00
|
|
|
size_t cbfs_boot_load_struct(const char *name, void *buf, size_t buf_size)
|
|
|
|
{
|
|
|
|
struct cbfsf fh;
|
|
|
|
uint32_t compression_algo;
|
|
|
|
size_t decompressed_size;
|
|
|
|
uint32_t type = CBFS_TYPE_STRUCT;
|
|
|
|
|
|
|
|
if (cbfs_boot_locate(&fh, name, &type) < 0)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
if (cbfsf_decompression_info(&fh, &compression_algo,
|
|
|
|
&decompressed_size) < 0
|
|
|
|
|| decompressed_size > buf_size)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return cbfs_load_and_decompress(&fh.data, 0, region_device_sz(&fh.data),
|
|
|
|
buf, buf_size, compression_algo);
|
|
|
|
}
|
|
|
|
|
2016-06-28 06:38:46 +02:00
|
|
|
size_t cbfs_prog_stage_section(struct prog *pstage, uintptr_t *base)
|
|
|
|
{
|
|
|
|
struct cbfs_stage stage;
|
|
|
|
const struct region_device *fh = prog_rdev(pstage);
|
|
|
|
|
|
|
|
if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
*base = (uintptr_t)stage.load;
|
|
|
|
return stage.memlen;
|
|
|
|
}
|
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
int cbfs_prog_stage_load(struct prog *pstage)
|
|
|
|
{
|
|
|
|
struct cbfs_stage stage;
|
|
|
|
uint8_t *load;
|
|
|
|
void *entry;
|
|
|
|
size_t fsize;
|
|
|
|
size_t foffset;
|
2015-09-17 23:09:30 +02:00
|
|
|
const struct region_device *fh = prog_rdev(pstage);
|
2009-04-14 09:40:01 +02:00
|
|
|
|
2015-05-16 06:39:23 +02:00
|
|
|
if (rdev_readat(fh, &stage, 0, sizeof(stage)) != sizeof(stage))
|
2015-12-03 20:29:12 +01:00
|
|
|
return -1;
|
2015-05-16 06:39:23 +02:00
|
|
|
|
|
|
|
fsize = region_device_sz(fh);
|
|
|
|
fsize -= sizeof(stage);
|
|
|
|
foffset = 0;
|
|
|
|
foffset += sizeof(stage);
|
|
|
|
|
|
|
|
assert(fsize == stage.len);
|
|
|
|
|
|
|
|
/* Note: cbfs_stage fields are currently in the endianness of the
|
|
|
|
* running processor. */
|
|
|
|
load = (void *)(uintptr_t)stage.load;
|
|
|
|
entry = (void *)(uintptr_t)stage.entry;
|
|
|
|
|
2015-10-08 00:22:42 +02:00
|
|
|
/* Hacky way to not load programs over read only media. The stages
|
|
|
|
* that would hit this path initialize themselves. */
|
2016-06-01 10:53:18 +02:00
|
|
|
if (ENV_VERSTAGE && !IS_ENABLED(CONFIG_NO_XIP_EARLY_STAGES) &&
|
2016-08-11 21:04:10 +02:00
|
|
|
IS_ENABLED(CONFIG_BOOT_DEVICE_MEMORY_MAPPED)) {
|
2015-10-08 00:22:42 +02:00
|
|
|
void *mapping = rdev_mmap(fh, foffset, fsize);
|
|
|
|
rdev_munmap(fh, mapping);
|
|
|
|
if (mapping == load)
|
|
|
|
goto out;
|
|
|
|
}
|
|
|
|
|
2015-09-29 22:51:35 +02:00
|
|
|
fsize = cbfs_load_and_decompress(fh, foffset, fsize, load,
|
|
|
|
stage.memlen, stage.compression);
|
|
|
|
if (!fsize)
|
2015-05-16 06:39:23 +02:00
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Clear area not covered by file. */
|
|
|
|
memset(&load[fsize], 0, stage.memlen - fsize);
|
|
|
|
|
2016-03-31 20:49:00 +02:00
|
|
|
prog_segment_loaded((uintptr_t)load, stage.memlen, SEG_FINAL);
|
2015-10-08 00:22:42 +02:00
|
|
|
|
|
|
|
out:
|
2015-05-16 06:39:23 +02:00
|
|
|
prog_set_area(pstage, load, stage.memlen);
|
|
|
|
prog_set_entry(pstage, entry, NULL);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-12-09 00:00:23 +01:00
|
|
|
|
2016-05-02 11:22:29 +02:00
|
|
|
/* This only supports the "COREBOOT" fmap region. */
|
2015-12-09 00:00:23 +01:00
|
|
|
static int cbfs_master_header_props(struct cbfs_props *props)
|
|
|
|
{
|
|
|
|
struct cbfs_header header;
|
|
|
|
const struct region_device *bdev;
|
|
|
|
int32_t rel_offset;
|
|
|
|
size_t offset;
|
|
|
|
|
|
|
|
bdev = boot_device_ro();
|
|
|
|
|
|
|
|
if (bdev == NULL)
|
|
|
|
return -1;
|
|
|
|
|
2016-05-02 11:22:29 +02:00
|
|
|
size_t fmap_top = ___FMAP__COREBOOT_BASE + ___FMAP__COREBOOT_SIZE;
|
|
|
|
|
2015-12-09 00:00:23 +01:00
|
|
|
/* Find location of header using signed 32-bit offset from
|
|
|
|
* end of CBFS region. */
|
2016-05-02 11:22:29 +02:00
|
|
|
offset = fmap_top - sizeof(int32_t);
|
2015-12-09 00:00:23 +01:00
|
|
|
if (rdev_readat(bdev, &rel_offset, offset, sizeof(int32_t)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
2016-05-02 11:22:29 +02:00
|
|
|
offset = fmap_top + rel_offset;
|
2015-12-09 00:00:23 +01:00
|
|
|
if (rdev_readat(bdev, &header, offset, sizeof(header)) < 0)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
header.magic = ntohl(header.magic);
|
|
|
|
header.romsize = ntohl(header.romsize);
|
|
|
|
header.offset = ntohl(header.offset);
|
|
|
|
|
|
|
|
if (header.magic != CBFS_HEADER_MAGIC)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
props->offset = header.offset;
|
|
|
|
props->size = header.romsize;
|
|
|
|
props->size -= props->offset;
|
|
|
|
|
|
|
|
printk(BIOS_SPEW, "CBFS @ %zx size %zx\n", props->offset, props->size);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This struct is marked as weak to allow a particular platform to
|
|
|
|
* override the master header logic. This implementation should work for most
|
|
|
|
* devices. */
|
|
|
|
const struct cbfs_locator __attribute__((weak)) cbfs_master_header_locator = {
|
|
|
|
.name = "Master Header Locator",
|
|
|
|
.locate = cbfs_master_header_props,
|
|
|
|
};
|
|
|
|
|
|
|
|
extern const struct cbfs_locator vboot_locator;
|
|
|
|
|
|
|
|
static const struct cbfs_locator *locators[] = {
|
2017-06-24 22:16:38 +02:00
|
|
|
#if IS_ENABLED(CONFIG_VBOOT)
|
2015-12-09 00:00:23 +01:00
|
|
|
&vboot_locator,
|
|
|
|
#endif
|
|
|
|
&cbfs_master_header_locator,
|
|
|
|
};
|
|
|
|
|
|
|
|
int cbfs_boot_region_properties(struct cbfs_props *props)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
boot_device_init();
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(locators); i++) {
|
|
|
|
const struct cbfs_locator *ops;
|
|
|
|
|
|
|
|
ops = locators[i];
|
|
|
|
|
|
|
|
if (ops->locate == NULL)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (ops->locate(props))
|
|
|
|
continue;
|
|
|
|
|
|
|
|
LOG("'%s' located CBFS at [%zx:%zx)\n",
|
|
|
|
ops->name, props->offset, props->offset + props->size);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void cbfs_prepare_program_locate(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
boot_device_init();
|
|
|
|
|
|
|
|
for (i = 0; i < ARRAY_SIZE(locators); i++) {
|
|
|
|
if (locators[i]->prepare == NULL)
|
|
|
|
continue;
|
|
|
|
locators[i]->prepare();
|
|
|
|
}
|
|
|
|
}
|