2015-03-29 06:56:22 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the coreboot project.
|
|
|
|
*
|
|
|
|
* Copyright 2012-2015 Google Inc.
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
2016-02-09 18:17:56 +01:00
|
|
|
#include <arch/early_variables.h>
|
2015-03-29 06:56:22 +02:00
|
|
|
#include <boot_device.h>
|
|
|
|
#include <console/console.h>
|
|
|
|
#include <fmap.h>
|
2015-09-08 20:34:43 +02:00
|
|
|
#include <commonlib/fmap_serialized.h>
|
2015-03-29 06:56:22 +02:00
|
|
|
#include <stddef.h>
|
|
|
|
#include <string.h>
|
2019-09-12 13:21:37 +02:00
|
|
|
#include <cbmem.h>
|
2015-03-29 06:56:22 +02:00
|
|
|
|
2016-05-10 22:12:08 +02:00
|
|
|
#include "fmap_config.h"
|
|
|
|
|
2015-03-29 06:56:22 +02:00
|
|
|
/*
|
|
|
|
* See http://code.google.com/p/flashmap/ for more information on FMAP.
|
|
|
|
*/
|
|
|
|
|
2016-02-09 18:17:56 +01:00
|
|
|
static int fmap_print_once CAR_GLOBAL;
|
2019-09-12 13:21:37 +02:00
|
|
|
static struct mem_region_device fmap_cache CAR_GLOBAL;
|
2016-02-09 18:17:56 +01:00
|
|
|
|
2019-09-27 08:51:46 +02:00
|
|
|
uint64_t get_fmap_flash_offset(void)
|
|
|
|
{
|
|
|
|
return FMAP_OFFSET;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int find_fmap_directory(struct region_device *fmrd)
|
2015-03-29 06:56:22 +02:00
|
|
|
{
|
|
|
|
const struct region_device *boot;
|
|
|
|
struct fmap *fmap;
|
|
|
|
size_t fmap_size;
|
2016-05-10 22:12:08 +02:00
|
|
|
size_t offset = FMAP_OFFSET;
|
2015-03-29 06:56:22 +02:00
|
|
|
|
2019-09-12 13:21:37 +02:00
|
|
|
if (cbmem_possibly_online() && !ENV_SMM) {
|
|
|
|
/* Try FMAP cache first */
|
|
|
|
const struct mem_region_device *cache;
|
|
|
|
|
|
|
|
cache = car_get_var_ptr(&fmap_cache);
|
|
|
|
if (region_device_sz(&cache->rdev))
|
|
|
|
return rdev_chain(fmrd, &cache->rdev, 0,
|
|
|
|
region_device_sz(&cache->rdev));
|
|
|
|
}
|
|
|
|
|
2015-03-29 06:56:22 +02:00
|
|
|
boot_device_init();
|
|
|
|
boot = boot_device_ro();
|
|
|
|
|
|
|
|
if (boot == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
fmap_size = sizeof(struct fmap);
|
|
|
|
|
|
|
|
fmap = rdev_mmap(boot, offset, fmap_size);
|
|
|
|
|
|
|
|
if (fmap == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (memcmp(fmap->signature, FMAP_SIGNATURE, sizeof(fmap->signature))) {
|
|
|
|
printk(BIOS_DEBUG, "No FMAP found at %zx offset.\n", offset);
|
|
|
|
rdev_munmap(boot, fmap);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2016-02-09 18:17:56 +01:00
|
|
|
if (!car_get_var(fmap_print_once)) {
|
|
|
|
printk(BIOS_DEBUG, "FMAP: Found \"%s\" version %d.%d at %zx.\n",
|
|
|
|
fmap->name, fmap->ver_major, fmap->ver_minor, offset);
|
|
|
|
printk(BIOS_DEBUG, "FMAP: base = %llx size = %x #areas = %d\n",
|
|
|
|
(long long)fmap->base, fmap->size, fmap->nareas);
|
|
|
|
car_set_var(fmap_print_once, 1);
|
|
|
|
}
|
2015-03-29 06:56:22 +02:00
|
|
|
|
|
|
|
fmap_size += fmap->nareas * sizeof(struct fmap_area);
|
|
|
|
|
|
|
|
rdev_munmap(boot, fmap);
|
|
|
|
|
|
|
|
return rdev_chain(fmrd, boot, offset, fmap_size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int fmap_locate_area_as_rdev(const char *name, struct region_device *area)
|
|
|
|
{
|
|
|
|
struct region ar;
|
|
|
|
|
|
|
|
if (fmap_locate_area(name, &ar))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return boot_device_ro_subregion(&ar, area);
|
|
|
|
}
|
|
|
|
|
2016-08-12 19:42:04 +02:00
|
|
|
int fmap_locate_area_as_rdev_rw(const char *name, struct region_device *area)
|
|
|
|
{
|
|
|
|
struct region ar;
|
|
|
|
|
|
|
|
if (fmap_locate_area(name, &ar))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
return boot_device_rw_subregion(&ar, area);
|
|
|
|
}
|
|
|
|
|
2015-03-29 06:56:22 +02:00
|
|
|
int fmap_locate_area(const char *name, struct region *ar)
|
|
|
|
{
|
|
|
|
struct region_device fmrd;
|
|
|
|
size_t offset;
|
|
|
|
|
|
|
|
if (find_fmap_directory(&fmrd))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Start reading the areas just after fmap header. */
|
|
|
|
offset = sizeof(struct fmap);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
struct fmap_area *area;
|
|
|
|
|
|
|
|
area = rdev_mmap(&fmrd, offset, sizeof(*area));
|
|
|
|
|
|
|
|
if (area == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if (strcmp((const char *)area->name, name)) {
|
|
|
|
rdev_munmap(&fmrd, area);
|
|
|
|
offset += sizeof(struct fmap_area);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2016-02-09 18:17:56 +01:00
|
|
|
printk(BIOS_DEBUG, "FMAP: area %s found @ %x (%d bytes)\n",
|
|
|
|
name, area->offset, area->size);
|
2015-03-29 06:56:22 +02:00
|
|
|
|
|
|
|
ar->offset = area->offset;
|
|
|
|
ar->size = area->size;
|
|
|
|
|
|
|
|
rdev_munmap(&fmrd, area);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
printk(BIOS_DEBUG, "FMAP: area %s not found\n", name);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2015-07-09 11:27:44 +02:00
|
|
|
|
|
|
|
int fmap_find_region_name(const struct region * const ar,
|
|
|
|
char name[FMAP_STRLEN])
|
|
|
|
{
|
|
|
|
struct region_device fmrd;
|
|
|
|
size_t offset;
|
|
|
|
|
|
|
|
if (find_fmap_directory(&fmrd))
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
/* Start reading the areas just after fmap header. */
|
|
|
|
offset = sizeof(struct fmap);
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
struct fmap_area *area;
|
|
|
|
|
|
|
|
area = rdev_mmap(&fmrd, offset, sizeof(*area));
|
|
|
|
|
|
|
|
if (area == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
if ((ar->offset != area->offset) ||
|
|
|
|
(ar->size != area->size)) {
|
|
|
|
rdev_munmap(&fmrd, area);
|
|
|
|
offset += sizeof(struct fmap_area);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) found, named %s\n",
|
|
|
|
ar->offset, ar->size, area->name);
|
|
|
|
|
|
|
|
memcpy(name, area->name, FMAP_STRLEN);
|
|
|
|
|
|
|
|
rdev_munmap(&fmrd, area);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
printk(BIOS_DEBUG, "FMAP: area (%zx, %zx) not found\n",
|
|
|
|
ar->offset, ar->size);
|
|
|
|
|
|
|
|
return -1;
|
|
|
|
}
|
2019-03-20 22:37:34 +01:00
|
|
|
|
|
|
|
ssize_t fmap_read_area(const char *name, void *buffer, size_t size)
|
|
|
|
{
|
|
|
|
struct region_device rdev;
|
|
|
|
if (fmap_locate_area_as_rdev(name, &rdev))
|
|
|
|
return -1;
|
|
|
|
return rdev_readat(&rdev, buffer, 0,
|
|
|
|
MIN(size, region_device_sz(&rdev)));
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t fmap_overwrite_area(const char *name, const void *buffer, size_t size)
|
|
|
|
{
|
|
|
|
struct region_device rdev;
|
|
|
|
|
|
|
|
if (fmap_locate_area_as_rdev_rw(name, &rdev))
|
|
|
|
return -1;
|
|
|
|
if (size > region_device_sz(&rdev))
|
|
|
|
return -1;
|
|
|
|
if (rdev_eraseat(&rdev, 0, region_device_sz(&rdev)) < 0)
|
|
|
|
return -1;
|
|
|
|
return rdev_writeat(&rdev, buffer, 0, size);
|
|
|
|
}
|
2019-09-12 13:21:37 +02:00
|
|
|
|
|
|
|
static void fmap_register_cache(int unused)
|
|
|
|
{
|
|
|
|
const struct cbmem_entry *e;
|
|
|
|
struct mem_region_device *mdev;
|
|
|
|
|
|
|
|
mdev = car_get_var_ptr(&fmap_cache);
|
|
|
|
|
|
|
|
/* Find the FMAP cache installed by previous stage */
|
|
|
|
e = cbmem_entry_find(CBMEM_ID_FMAP);
|
|
|
|
/* Don't set fmap_cache so that find_fmap_directory will use regular path */
|
|
|
|
if (!e)
|
|
|
|
return;
|
|
|
|
|
|
|
|
mem_region_device_ro_init(mdev, cbmem_entry_start(e), cbmem_entry_size(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The main reason to copy the FMAP into CBMEM is to make it available to the
|
|
|
|
* OS on every architecture. As side effect use the CBMEM copy as cache.
|
|
|
|
*/
|
|
|
|
static void fmap_setup_cache(int unused)
|
|
|
|
{
|
|
|
|
struct region_device fmrd;
|
|
|
|
|
|
|
|
if (find_fmap_directory(&fmrd))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* Reloads the FMAP even on ACPI S3 resume */
|
|
|
|
const size_t s = region_device_sz(&fmrd);
|
|
|
|
struct fmap *fmap = cbmem_add(CBMEM_ID_FMAP, s);
|
|
|
|
if (!fmap) {
|
|
|
|
printk(BIOS_ERR, "ERROR: Failed to allocate CBMEM\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ssize_t ret = rdev_readat(&fmrd, fmap, 0, s);
|
|
|
|
if (ret != s) {
|
|
|
|
printk(BIOS_ERR, "ERROR: Failed to read FMAP into CBMEM\n");
|
|
|
|
cbmem_entry_remove(cbmem_entry_find(CBMEM_ID_FMAP));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Finally advertise the cache for the current stage */
|
|
|
|
fmap_register_cache(unused);
|
|
|
|
}
|
|
|
|
|
|
|
|
ROMSTAGE_CBMEM_INIT_HOOK(fmap_setup_cache)
|
|
|
|
RAMSTAGE_CBMEM_INIT_HOOK(fmap_register_cache)
|
|
|
|
POSTCAR_CBMEM_INIT_HOOK(fmap_register_cache)
|