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:
parent
3cbf8d955f
commit
0165038561
1 changed files with 62 additions and 43 deletions
|
@ -11,10 +11,9 @@
|
|||
* 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.
|
||||
*
|
||||
* TODO: Make this CAR-friendly in case we use it on x86 some day.
|
||||
*/
|
||||
|
||||
#include <arch/early_variables.h>
|
||||
#include <assert.h>
|
||||
#include <console/console.h>
|
||||
#include <spi_flash.h>
|
||||
|
@ -27,20 +26,26 @@
|
|||
|
||||
#define BLOB_SIZE VB2_NVDATA_SIZE
|
||||
|
||||
/* FMAP descriptor of the NVRAM area */
|
||||
static struct region_device nvram_region;
|
||||
struct vbnv_flash_ctx {
|
||||
/* VBNV flash is initialized */
|
||||
int initialized;
|
||||
|
||||
/* offset of the current nvdata in SPI flash */
|
||||
static int blob_offset = -1;
|
||||
/* Offset of the current nvdata in SPI flash */
|
||||
int blob_offset;
|
||||
|
||||
/* Offset of the topmost nvdata blob in SPI flash */
|
||||
static int top_offset;
|
||||
/* Offset of the topmost nvdata blob in SPI flash */
|
||||
int top_offset;
|
||||
|
||||
/* cache of the current nvdata */
|
||||
static uint8_t cache[BLOB_SIZE];
|
||||
/* SPI flash handler used when saving data */
|
||||
struct spi_flash *flash;
|
||||
|
||||
/* spi_flash struct used when saving data */
|
||||
static struct spi_flash *spi_flash = NULL;
|
||||
/* FMAP descriptor of the NVRAM area */
|
||||
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
|
||||
|
@ -57,20 +62,16 @@ static inline int can_overwrite(uint8_t current, uint8_t new)
|
|||
return (current & new) == new;
|
||||
}
|
||||
|
||||
static inline int is_initialized(void)
|
||||
{
|
||||
return blob_offset >= 0;
|
||||
}
|
||||
|
||||
static int init_vbnv(void)
|
||||
{
|
||||
struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
|
||||
uint8_t buf[BLOB_SIZE];
|
||||
uint8_t empty_blob[BLOB_SIZE];
|
||||
int offset;
|
||||
int i;
|
||||
|
||||
if (vboot_named_region_device("RW_NVRAM", &nvram_region) ||
|
||||
region_device_sz(&nvram_region) < BLOB_SIZE) {
|
||||
if (vboot_named_region_device("RW_NVRAM", &ctx->region) ||
|
||||
region_device_sz(&ctx->region) < BLOB_SIZE) {
|
||||
printk(BIOS_ERR, "%s: failed to locate NVRAM\n", __func__);
|
||||
return 1;
|
||||
}
|
||||
|
@ -80,7 +81,7 @@ static int init_vbnv(void)
|
|||
empty_blob[i] = erase_value();
|
||||
|
||||
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
|
||||
|
@ -88,8 +89,8 @@ static int init_vbnv(void)
|
|||
* empty blob, or the base of the region if the nvram has never been
|
||||
* used.
|
||||
*/
|
||||
for (i = 0; i <= top_offset; i += BLOB_SIZE) {
|
||||
if (rdev_readat(&nvram_region, buf, i, BLOB_SIZE) < 0) {
|
||||
for (i = 0; i <= ctx->top_offset; i += BLOB_SIZE) {
|
||||
if (rdev_readat(&ctx->region, buf, i, BLOB_SIZE) < 0) {
|
||||
printk(BIOS_ERR, "failed to read nvdata\n");
|
||||
return 1;
|
||||
}
|
||||
|
@ -99,12 +100,13 @@ static int init_vbnv(void)
|
|||
}
|
||||
|
||||
/* 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");
|
||||
return 1;
|
||||
}
|
||||
|
||||
blob_offset = offset;
|
||||
ctx->blob_offset = offset;
|
||||
ctx->initialized = 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
@ -122,15 +124,19 @@ static void vbnv_is_erasable(void)
|
|||
*
|
||||
* TODO: Check by calling can_erase implemented by each spi flash driver
|
||||
*/
|
||||
assert(!(region_device_offset(&nvram_region) % spi_flash->sector_size));
|
||||
assert(!(region_device_sz(&nvram_region) % spi_flash->sector_size));
|
||||
struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
|
||||
|
||||
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)
|
||||
{
|
||||
if (!spi_flash) {
|
||||
spi_flash = spi_flash_probe(CONFIG_BOOT_MEDIA_SPI_BUS, 0);
|
||||
if (!spi_flash) {
|
||||
struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_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");
|
||||
return 1;
|
||||
}
|
||||
|
@ -140,16 +146,25 @@ static int vbnv_flash_probe(void)
|
|||
*/
|
||||
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;
|
||||
}
|
||||
|
||||
static int erase_nvram(void)
|
||||
{
|
||||
struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
|
||||
|
||||
if (vbnv_flash_probe())
|
||||
return 1;
|
||||
|
||||
if (spi_flash->erase(spi_flash, region_device_offset(&nvram_region),
|
||||
region_device_sz(&nvram_region))) {
|
||||
if (ctx->flash->erase(ctx->flash, region_device_offset(&ctx->region),
|
||||
region_device_sz(&ctx->region))) {
|
||||
printk(BIOS_ERR, "failed to erase nvram\n");
|
||||
return 1;
|
||||
}
|
||||
|
@ -160,33 +175,37 @@ static int erase_nvram(void)
|
|||
|
||||
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())
|
||||
return; /* error */
|
||||
memcpy(vbnv_copy, cache, BLOB_SIZE);
|
||||
|
||||
memcpy(vbnv_copy, ctx->cache, BLOB_SIZE);
|
||||
}
|
||||
|
||||
void save_vbnv_flash(const uint8_t *vbnv_copy)
|
||||
{
|
||||
struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
|
||||
int new_offset;
|
||||
int i;
|
||||
|
||||
if (!is_initialized())
|
||||
if (!ctx->initialized)
|
||||
if (init_vbnv())
|
||||
return; /* error */
|
||||
|
||||
/* Bail out if there have been no changes. */
|
||||
if (!memcmp(vbnv_copy, cache, BLOB_SIZE))
|
||||
if (!memcmp(vbnv_copy, ctx->cache, BLOB_SIZE))
|
||||
return;
|
||||
|
||||
new_offset = blob_offset;
|
||||
new_offset = ctx->blob_offset;
|
||||
|
||||
/* See if we can overwrite the current blob with the new one */
|
||||
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 */
|
||||
new_offset += BLOB_SIZE;
|
||||
if (new_offset > top_offset) {
|
||||
if (new_offset > ctx->top_offset) {
|
||||
if (erase_nvram())
|
||||
return; /* error */
|
||||
new_offset = 0;
|
||||
|
@ -196,12 +215,12 @@ void save_vbnv_flash(const uint8_t *vbnv_copy)
|
|||
}
|
||||
|
||||
if (!vbnv_flash_probe() &&
|
||||
!spi_flash->write(spi_flash,
|
||||
region_device_offset(&nvram_region) + new_offset,
|
||||
BLOB_SIZE, vbnv_copy)) {
|
||||
!ctx->flash->write(ctx->flash,
|
||||
region_device_offset(&ctx->region) + new_offset,
|
||||
BLOB_SIZE, vbnv_copy)) {
|
||||
/* write was successful. safely move pointer forward */
|
||||
blob_offset = new_offset;
|
||||
memcpy(cache, vbnv_copy, BLOB_SIZE);
|
||||
ctx->blob_offset = new_offset;
|
||||
memcpy(ctx->cache, vbnv_copy, BLOB_SIZE);
|
||||
} else {
|
||||
printk(BIOS_ERR, "failed to save nvdata\n");
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue