chromeos: Make vbnv_flash driver safe for CAR usage

This modifies the vbnv_flash driver to make it safe for use
in cache-as-ram by handling the global variables safely.

To make this cleaner all of the variables were moved into
one structure and referenced from there.

BUG=chrome-os-partner:47915
BRANCH=glados
TEST=build and boot on chell using following patches to
test backup and restore of vbnv_cmos into flash

Change-Id: I3a17fa51cfd754455502ac2e5f181dae35967f2a
Signed-off-by: Patrick Georgi <pgeorgi@chromium.org>
Original-Commit-Id: 48876561fa4fb61e1ec8f92596c5610d97135201
Original-Change-Id: Id9fda8467edcc55e5ed760ddab197ab97d1f3d25
Original-Signed-off-by: Duncan Laurie <dlaurie@chromium.org>
Original-Reviewed-on: https://chromium-review.googlesource.com/324121
Original-Reviewed-by: Aaron Durbin <adurbin@chromium.org>
Reviewed-on: https://review.coreboot.org/13599
Tested-by: build bot (Jenkins)
Reviewed-by: Martin Roth <martinroth@google.com>
This commit is contained in:
Duncan Laurie
2016-01-26 16:22:53 -08:00
committed by Patrick Georgi
parent 3cbf8d955f
commit 0165038561

View File

@ -11,10 +11,9 @@
* but WITHOUT ANY WARRANTY; without even the implied warranty of * but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details. * GNU General Public License for more details.
*
* TODO: Make this CAR-friendly in case we use it on x86 some day.
*/ */
#include <arch/early_variables.h>
#include <assert.h> #include <assert.h>
#include <console/console.h> #include <console/console.h>
#include <spi_flash.h> #include <spi_flash.h>
@ -27,20 +26,26 @@
#define BLOB_SIZE VB2_NVDATA_SIZE #define BLOB_SIZE VB2_NVDATA_SIZE
/* FMAP descriptor of the NVRAM area */ struct vbnv_flash_ctx {
static struct region_device nvram_region; /* VBNV flash is initialized */
int initialized;
/* offset of the current nvdata in SPI flash */ /* Offset of the current nvdata in SPI flash */
static int blob_offset = -1; int blob_offset;
/* Offset of the topmost nvdata blob in SPI flash */ /* Offset of the topmost nvdata blob in SPI flash */
static int top_offset; int top_offset;
/* cache of the current nvdata */ /* SPI flash handler used when saving data */
static uint8_t cache[BLOB_SIZE]; struct spi_flash *flash;
/* spi_flash struct used when saving data */ /* FMAP descriptor of the NVRAM area */
static struct spi_flash *spi_flash = NULL; struct region_device region;
/* Cache of the current nvdata */
uint8_t cache[BLOB_SIZE];
};
static struct vbnv_flash_ctx vbnv_flash CAR_GLOBAL;
/* /*
* This code assumes that flash is erased to 1-bits, and write operations can * This code assumes that flash is erased to 1-bits, and write operations can
@ -57,20 +62,16 @@ static inline int can_overwrite(uint8_t current, uint8_t new)
return (current & new) == new; return (current & new) == new;
} }
static inline int is_initialized(void)
{
return blob_offset >= 0;
}
static int init_vbnv(void) static int init_vbnv(void)
{ {
struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
uint8_t buf[BLOB_SIZE]; uint8_t buf[BLOB_SIZE];
uint8_t empty_blob[BLOB_SIZE]; uint8_t empty_blob[BLOB_SIZE];
int offset; int offset;
int i; int i;
if (vboot_named_region_device("RW_NVRAM", &nvram_region) || if (vboot_named_region_device("RW_NVRAM", &ctx->region) ||
region_device_sz(&nvram_region) < BLOB_SIZE) { region_device_sz(&ctx->region) < BLOB_SIZE) {
printk(BIOS_ERR, "%s: failed to locate NVRAM\n", __func__); printk(BIOS_ERR, "%s: failed to locate NVRAM\n", __func__);
return 1; return 1;
} }
@ -80,7 +81,7 @@ static int init_vbnv(void)
empty_blob[i] = erase_value(); empty_blob[i] = erase_value();
offset = 0; offset = 0;
top_offset = region_device_sz(&nvram_region) - BLOB_SIZE; ctx->top_offset = region_device_sz(&ctx->region) - BLOB_SIZE;
/* /*
* after the loop, offset is supposed to point the blob right before * after the loop, offset is supposed to point the blob right before
@ -88,8 +89,8 @@ static int init_vbnv(void)
* empty blob, or the base of the region if the nvram has never been * empty blob, or the base of the region if the nvram has never been
* used. * used.
*/ */
for (i = 0; i <= top_offset; i += BLOB_SIZE) { for (i = 0; i <= ctx->top_offset; i += BLOB_SIZE) {
if (rdev_readat(&nvram_region, buf, i, BLOB_SIZE) < 0) { if (rdev_readat(&ctx->region, buf, i, BLOB_SIZE) < 0) {
printk(BIOS_ERR, "failed to read nvdata\n"); printk(BIOS_ERR, "failed to read nvdata\n");
return 1; return 1;
} }
@ -99,12 +100,13 @@ static int init_vbnv(void)
} }
/* reread the nvdata and write it to the cache */ /* reread the nvdata and write it to the cache */
if (rdev_readat(&nvram_region, cache, offset, BLOB_SIZE) < 0) { if (rdev_readat(&ctx->region, ctx->cache, offset, BLOB_SIZE) < 0) {
printk(BIOS_ERR, "failed to read nvdata\n"); printk(BIOS_ERR, "failed to read nvdata\n");
return 1; return 1;
} }
blob_offset = offset; ctx->blob_offset = offset;
ctx->initialized = 1;
return 0; return 0;
} }
@ -122,15 +124,19 @@ static void vbnv_is_erasable(void)
* *
* TODO: Check by calling can_erase implemented by each spi flash driver * TODO: Check by calling can_erase implemented by each spi flash driver
*/ */
assert(!(region_device_offset(&nvram_region) % spi_flash->sector_size)); struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
assert(!(region_device_sz(&nvram_region) % spi_flash->sector_size));
assert(!(region_device_offset(&ctx->region) % ctx->flash->sector_size));
assert(!(region_device_sz(&ctx->region) % ctx->flash->sector_size));
} }
static int vbnv_flash_probe(void) static int vbnv_flash_probe(void)
{ {
if (!spi_flash) { struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
spi_flash = spi_flash_probe(CONFIG_BOOT_MEDIA_SPI_BUS, 0);
if (!spi_flash) { if (!ctx->flash) {
ctx->flash = spi_flash_probe(CONFIG_BOOT_MEDIA_SPI_BUS, 0);
if (!ctx->flash) {
printk(BIOS_ERR, "failed to probe spi flash\n"); printk(BIOS_ERR, "failed to probe spi flash\n");
return 1; return 1;
} }
@ -140,16 +146,25 @@ static int vbnv_flash_probe(void)
*/ */
vbnv_is_erasable(); vbnv_is_erasable();
} }
/*
* Handle the case where spi_flash_probe returns a CAR_GLOBAL
* in early execution on x86 but then later is moved to RAM.
*/
ctx->flash = car_get_var_ptr(ctx->flash);
return 0; return 0;
} }
static int erase_nvram(void) static int erase_nvram(void)
{ {
struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
if (vbnv_flash_probe()) if (vbnv_flash_probe())
return 1; return 1;
if (spi_flash->erase(spi_flash, region_device_offset(&nvram_region), if (ctx->flash->erase(ctx->flash, region_device_offset(&ctx->region),
region_device_sz(&nvram_region))) { region_device_sz(&ctx->region))) {
printk(BIOS_ERR, "failed to erase nvram\n"); printk(BIOS_ERR, "failed to erase nvram\n");
return 1; return 1;
} }
@ -160,33 +175,37 @@ static int erase_nvram(void)
void read_vbnv_flash(uint8_t *vbnv_copy) void read_vbnv_flash(uint8_t *vbnv_copy)
{ {
if (!is_initialized()) struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
if (!ctx->initialized)
if (init_vbnv()) if (init_vbnv())
return; /* error */ return; /* error */
memcpy(vbnv_copy, cache, BLOB_SIZE);
memcpy(vbnv_copy, ctx->cache, BLOB_SIZE);
} }
void save_vbnv_flash(const uint8_t *vbnv_copy) void save_vbnv_flash(const uint8_t *vbnv_copy)
{ {
struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
int new_offset; int new_offset;
int i; int i;
if (!is_initialized()) if (!ctx->initialized)
if (init_vbnv()) if (init_vbnv())
return; /* error */ return; /* error */
/* Bail out if there have been no changes. */ /* Bail out if there have been no changes. */
if (!memcmp(vbnv_copy, cache, BLOB_SIZE)) if (!memcmp(vbnv_copy, ctx->cache, BLOB_SIZE))
return; return;
new_offset = blob_offset; new_offset = ctx->blob_offset;
/* See if we can overwrite the current blob with the new one */ /* See if we can overwrite the current blob with the new one */
for (i = 0; i < BLOB_SIZE; i++) { for (i = 0; i < BLOB_SIZE; i++) {
if (!can_overwrite(cache[i], vbnv_copy[i])) { if (!can_overwrite(ctx->cache[i], vbnv_copy[i])) {
/* unable to overwrite. need to use the next blob */ /* unable to overwrite. need to use the next blob */
new_offset += BLOB_SIZE; new_offset += BLOB_SIZE;
if (new_offset > top_offset) { if (new_offset > ctx->top_offset) {
if (erase_nvram()) if (erase_nvram())
return; /* error */ return; /* error */
new_offset = 0; new_offset = 0;
@ -196,12 +215,12 @@ void save_vbnv_flash(const uint8_t *vbnv_copy)
} }
if (!vbnv_flash_probe() && if (!vbnv_flash_probe() &&
!spi_flash->write(spi_flash, !ctx->flash->write(ctx->flash,
region_device_offset(&nvram_region) + new_offset, region_device_offset(&ctx->region) + new_offset,
BLOB_SIZE, vbnv_copy)) { BLOB_SIZE, vbnv_copy)) {
/* write was successful. safely move pointer forward */ /* write was successful. safely move pointer forward */
blob_offset = new_offset; ctx->blob_offset = new_offset;
memcpy(cache, vbnv_copy, BLOB_SIZE); memcpy(ctx->cache, vbnv_copy, BLOB_SIZE);
} else { } else {
printk(BIOS_ERR, "failed to save nvdata\n"); printk(BIOS_ERR, "failed to save nvdata\n");
} }