summaryrefslogtreecommitdiff
path: root/src/vendorcode
diff options
context:
space:
mode:
authorDuncan Laurie <dlaurie@chromium.org>2016-01-26 16:22:53 -0800
committerPatrick Georgi <pgeorgi@google.com>2016-02-09 13:20:41 +0100
commit0165038561c5cfe7165445a56143aa6df3eae4f8 (patch)
tree85e1e6a8d013608eb8ea2af286b5822599184b6e /src/vendorcode
parent3cbf8d955f48fc39d5b3087934cbc5739ba07c5a (diff)
downloadcoreboot-0165038561c5cfe7165445a56143aa6df3eae4f8.tar.xz
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>
Diffstat (limited to 'src/vendorcode')
-rw-r--r--src/vendorcode/google/chromeos/vbnv_flash.c105
1 files changed, 62 insertions, 43 deletions
diff --git a/src/vendorcode/google/chromeos/vbnv_flash.c b/src/vendorcode/google/chromeos/vbnv_flash.c
index ea5d9f3ff9..88f39b08f1 100644
--- a/src/vendorcode/google/chromeos/vbnv_flash.c
+++ b/src/vendorcode/google/chromeos/vbnv_flash.c
@@ -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 */
+ int blob_offset;
-/* offset of the current nvdata in SPI flash */
-static int blob_offset = -1;
+ /* Offset of the topmost nvdata blob in SPI flash */
+ int top_offset;
-/* Offset of the topmost nvdata blob in SPI flash */
-static int top_offset;
+ /* SPI flash handler used when saving data */
+ struct spi_flash *flash;
-/* cache of the current nvdata */
-static uint8_t cache[BLOB_SIZE];
+ /* FMAP descriptor of the NVRAM area */
+ struct region_device region;
-/* spi_flash struct used when saving data */
-static struct spi_flash *spi_flash = NULL;
+ /* 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");
}