2014-10-08 20:32:23 +02:00
|
|
|
/*
|
|
|
|
* This file is part of the coreboot project.
|
|
|
|
*
|
|
|
|
* Copyright (C) 2014 The ChromiumOS Authors. All rights reserved.
|
|
|
|
*
|
|
|
|
* 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-01-27 01:22:53 +01:00
|
|
|
#include <arch/early_variables.h>
|
2015-09-04 23:32:49 +02:00
|
|
|
#include <assert.h>
|
2016-07-25 20:48:03 +02:00
|
|
|
#include <commonlib/region.h>
|
2014-10-29 19:18:11 +01:00
|
|
|
#include <console/console.h>
|
|
|
|
#include <string.h>
|
2014-12-04 02:35:53 +01:00
|
|
|
#include <vb2_api.h>
|
2014-10-29 19:18:11 +01:00
|
|
|
#include <vboot_nvstorage.h>
|
2016-07-25 20:48:03 +02:00
|
|
|
#include <vboot/vboot_common.h>
|
|
|
|
#include <vboot/vbnv.h>
|
|
|
|
#include <vboot/vbnv_layout.h>
|
2014-10-08 20:32:23 +02:00
|
|
|
|
2014-10-29 19:18:11 +01:00
|
|
|
#define BLOB_SIZE VB2_NVDATA_SIZE
|
|
|
|
|
2016-01-27 01:22:53 +01:00
|
|
|
struct vbnv_flash_ctx {
|
|
|
|
/* VBNV flash is initialized */
|
|
|
|
int initialized;
|
|
|
|
|
2016-08-12 19:48:58 +02:00
|
|
|
/* Offset of the current nvdata in flash */
|
2016-01-27 01:22:53 +01:00
|
|
|
int blob_offset;
|
2014-10-29 19:18:11 +01:00
|
|
|
|
2016-08-12 19:48:58 +02:00
|
|
|
/* Offset of the topmost nvdata blob in flash */
|
2016-01-27 01:22:53 +01:00
|
|
|
int top_offset;
|
2014-10-29 19:18:11 +01:00
|
|
|
|
2016-08-12 19:48:58 +02:00
|
|
|
/* Region to store and retrieve the VBNV contents. */
|
|
|
|
struct region_device vbnv_dev;
|
2014-10-29 19:18:11 +01:00
|
|
|
|
2016-01-27 01:22:53 +01:00
|
|
|
/* Cache of the current nvdata */
|
|
|
|
uint8_t cache[BLOB_SIZE];
|
|
|
|
};
|
|
|
|
static struct vbnv_flash_ctx vbnv_flash CAR_GLOBAL;
|
2014-10-29 19:18:11 +01:00
|
|
|
|
|
|
|
/*
|
|
|
|
* This code assumes that flash is erased to 1-bits, and write operations can
|
|
|
|
* only change 1-bits to 0-bits. So if the new contents only change 1-bits to
|
|
|
|
* 0-bits, we can reuse the current blob.
|
|
|
|
*/
|
|
|
|
static inline uint8_t erase_value(void)
|
|
|
|
{
|
|
|
|
return 0xff;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int can_overwrite(uint8_t current, uint8_t new)
|
|
|
|
{
|
|
|
|
return (current & new) == new;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int init_vbnv(void)
|
|
|
|
{
|
2016-01-27 01:22:53 +01:00
|
|
|
struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
|
2016-08-12 19:48:58 +02:00
|
|
|
struct region_device *rdev = &ctx->vbnv_dev;
|
2014-10-29 19:18:11 +01:00
|
|
|
uint8_t buf[BLOB_SIZE];
|
|
|
|
uint8_t empty_blob[BLOB_SIZE];
|
2016-08-27 01:01:16 +02:00
|
|
|
int used_below, empty_above;
|
2014-10-29 19:18:11 +01:00
|
|
|
int offset;
|
|
|
|
int i;
|
|
|
|
|
2016-08-12 19:48:58 +02:00
|
|
|
if (vboot_named_region_device_rw("RW_NVRAM", rdev) ||
|
|
|
|
region_device_sz(rdev) < BLOB_SIZE) {
|
2014-10-29 19:18:11 +01:00
|
|
|
printk(BIOS_ERR, "%s: failed to locate NVRAM\n", __func__);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Prepare an empty blob to compare against. */
|
|
|
|
for (i = 0; i < BLOB_SIZE; i++)
|
|
|
|
empty_blob[i] = erase_value();
|
|
|
|
|
2016-08-12 19:48:58 +02:00
|
|
|
ctx->top_offset = region_device_sz(rdev) - BLOB_SIZE;
|
2015-01-11 08:11:43 +01:00
|
|
|
|
2016-08-27 01:01:16 +02:00
|
|
|
/* Binary search for the border between used and empty */
|
|
|
|
used_below = 0;
|
|
|
|
empty_above = region_device_sz(rdev) / BLOB_SIZE;
|
|
|
|
|
|
|
|
while (used_below + 1 < empty_above) {
|
|
|
|
int guess = (used_below + empty_above) / 2;
|
|
|
|
if (rdev_readat(rdev, buf, guess * BLOB_SIZE, BLOB_SIZE) < 0) {
|
2014-10-29 19:18:11 +01:00
|
|
|
printk(BIOS_ERR, "failed to read nvdata\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!memcmp(buf, empty_blob, BLOB_SIZE))
|
2016-08-27 01:01:16 +02:00
|
|
|
empty_above = guess;
|
|
|
|
else
|
|
|
|
used_below = guess;
|
2014-10-29 19:18:11 +01:00
|
|
|
}
|
|
|
|
|
2016-08-27 01:01:16 +02:00
|
|
|
/*
|
|
|
|
* Offset points to the last non-empty blob. Or if all blobs are empty
|
|
|
|
* (nvram is totally erased), point to the first blob.
|
|
|
|
*/
|
|
|
|
offset = used_below * BLOB_SIZE;
|
|
|
|
|
2014-10-29 19:18:11 +01:00
|
|
|
/* reread the nvdata and write it to the cache */
|
2016-08-12 19:48:58 +02:00
|
|
|
if (rdev_readat(rdev, ctx->cache, offset, BLOB_SIZE) < 0) {
|
2014-10-29 19:18:11 +01:00
|
|
|
printk(BIOS_ERR, "failed to read nvdata\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2016-01-27 01:22:53 +01:00
|
|
|
ctx->blob_offset = offset;
|
|
|
|
ctx->initialized = 1;
|
2014-10-29 19:18:11 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int erase_nvram(void)
|
|
|
|
{
|
2016-01-27 01:22:53 +01:00
|
|
|
struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
|
2016-08-12 19:48:58 +02:00
|
|
|
const struct region_device *rdev = &ctx->vbnv_dev;
|
2016-01-27 01:22:53 +01:00
|
|
|
|
2016-08-12 19:48:58 +02:00
|
|
|
if (rdev_eraseat(rdev, 0, region_device_sz(rdev)) < 0) {
|
2014-10-29 19:18:11 +01:00
|
|
|
printk(BIOS_ERR, "failed to erase nvram\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printk(BIOS_INFO, "nvram is cleared\n");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-01-26 02:13:27 +01:00
|
|
|
void read_vbnv_flash(uint8_t *vbnv_copy)
|
2014-10-08 20:32:23 +02:00
|
|
|
{
|
2016-01-27 01:22:53 +01:00
|
|
|
struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
|
|
|
|
|
|
|
|
if (!ctx->initialized)
|
2014-10-29 19:18:11 +01:00
|
|
|
if (init_vbnv())
|
|
|
|
return; /* error */
|
2016-01-27 01:22:53 +01:00
|
|
|
|
|
|
|
memcpy(vbnv_copy, ctx->cache, BLOB_SIZE);
|
2014-10-08 20:32:23 +02:00
|
|
|
}
|
|
|
|
|
2016-01-26 02:13:27 +01:00
|
|
|
void save_vbnv_flash(const uint8_t *vbnv_copy)
|
2014-10-08 20:32:23 +02:00
|
|
|
{
|
2016-01-27 01:22:53 +01:00
|
|
|
struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
|
2015-01-11 08:11:43 +01:00
|
|
|
int new_offset;
|
2014-10-29 19:18:11 +01:00
|
|
|
int i;
|
2016-08-12 19:48:58 +02:00
|
|
|
const struct region_device *rdev = &ctx->vbnv_dev;
|
2014-10-29 19:18:11 +01:00
|
|
|
|
2016-01-27 01:22:53 +01:00
|
|
|
if (!ctx->initialized)
|
2014-10-29 19:18:11 +01:00
|
|
|
if (init_vbnv())
|
|
|
|
return; /* error */
|
|
|
|
|
|
|
|
/* Bail out if there have been no changes. */
|
2016-01-27 01:22:53 +01:00
|
|
|
if (!memcmp(vbnv_copy, ctx->cache, BLOB_SIZE))
|
2014-10-29 19:18:11 +01:00
|
|
|
return;
|
|
|
|
|
2016-01-27 01:22:53 +01:00
|
|
|
new_offset = ctx->blob_offset;
|
2015-01-11 08:11:43 +01:00
|
|
|
|
2014-10-29 19:18:11 +01:00
|
|
|
/* See if we can overwrite the current blob with the new one */
|
|
|
|
for (i = 0; i < BLOB_SIZE; i++) {
|
2016-01-27 01:22:53 +01:00
|
|
|
if (!can_overwrite(ctx->cache[i], vbnv_copy[i])) {
|
2014-10-29 19:18:11 +01:00
|
|
|
/* unable to overwrite. need to use the next blob */
|
|
|
|
new_offset += BLOB_SIZE;
|
2016-01-27 01:22:53 +01:00
|
|
|
if (new_offset > ctx->top_offset) {
|
2014-10-29 19:18:11 +01:00
|
|
|
if (erase_nvram())
|
|
|
|
return; /* error */
|
2015-05-16 06:25:46 +02:00
|
|
|
new_offset = 0;
|
2014-10-29 19:18:11 +01:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-23 20:22:17 +02:00
|
|
|
if (rdev_writeat(rdev, vbnv_copy, new_offset, BLOB_SIZE) == BLOB_SIZE) {
|
2015-01-11 08:11:43 +01:00
|
|
|
/* write was successful. safely move pointer forward */
|
2016-01-27 01:22:53 +01:00
|
|
|
ctx->blob_offset = new_offset;
|
|
|
|
memcpy(ctx->cache, vbnv_copy, BLOB_SIZE);
|
2015-01-11 08:11:43 +01:00
|
|
|
} else {
|
|
|
|
printk(BIOS_ERR, "failed to save nvdata\n");
|
2014-10-29 19:18:11 +01:00
|
|
|
}
|
2014-10-08 20:32:23 +02:00
|
|
|
}
|