blob: dd128a923527cc87a4d4cb3d38bdf47e932685b6 [file] [log] [blame]
Daisuke Nojiri477dd182014-10-08 11:32:23 -07001/*
2 * This file is part of the coreboot project.
3 *
4 * Copyright (C) 2014 The ChromiumOS Authors. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; version 2 of the License.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
Daisuke Nojiri477dd182014-10-08 11:32:23 -070014 */
15
Duncan Laurie01650382016-01-26 16:22:53 -080016#include <arch/early_variables.h>
Daisuke Nojiri914a21e2015-09-04 14:32:49 -070017#include <assert.h>
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070018#include <commonlib/region.h>
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070019#include <console/console.h>
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070020#include <string.h>
Randall Spangler144c2282014-12-03 17:35:53 -080021#include <vb2_api.h>
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070022#include <vboot_nvstorage.h>
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070023#include <vboot/vboot_common.h>
24#include <vboot/vbnv.h>
25#include <vboot/vbnv_layout.h>
Daisuke Nojiri477dd182014-10-08 11:32:23 -070026
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070027#define BLOB_SIZE VB2_NVDATA_SIZE
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070028
Duncan Laurie01650382016-01-26 16:22:53 -080029struct vbnv_flash_ctx {
30 /* VBNV flash is initialized */
31 int initialized;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070032
Aaron Durbinb18a6662016-08-12 12:48:58 -050033 /* Offset of the current nvdata in flash */
Duncan Laurie01650382016-01-26 16:22:53 -080034 int blob_offset;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070035
Aaron Durbinb18a6662016-08-12 12:48:58 -050036 /* Offset of the topmost nvdata blob in flash */
Duncan Laurie01650382016-01-26 16:22:53 -080037 int top_offset;
Vadim Bendebury9e381a12015-01-10 23:11:43 -080038
Aaron Durbinb18a6662016-08-12 12:48:58 -050039 /* Region to store and retrieve the VBNV contents. */
40 struct region_device vbnv_dev;
Duncan Laurie01650382016-01-26 16:22:53 -080041
42 /* Cache of the current nvdata */
43 uint8_t cache[BLOB_SIZE];
44};
45static struct vbnv_flash_ctx vbnv_flash CAR_GLOBAL;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070046
47/*
48 * This code assumes that flash is erased to 1-bits, and write operations can
49 * only change 1-bits to 0-bits. So if the new contents only change 1-bits to
50 * 0-bits, we can reuse the current blob.
51 */
52static inline uint8_t erase_value(void)
53{
54 return 0xff;
55}
56
57static inline int can_overwrite(uint8_t current, uint8_t new)
58{
59 return (current & new) == new;
60}
61
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070062static int init_vbnv(void)
63{
Duncan Laurie01650382016-01-26 16:22:53 -080064 struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
Aaron Durbinb18a6662016-08-12 12:48:58 -050065 struct region_device *rdev = &ctx->vbnv_dev;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070066 uint8_t buf[BLOB_SIZE];
67 uint8_t empty_blob[BLOB_SIZE];
Randall Spangleraa1d3142016-08-26 16:01:16 -070068 int used_below, empty_above;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070069 int offset;
70 int i;
71
Aaron Durbinb18a6662016-08-12 12:48:58 -050072 if (vboot_named_region_device_rw("RW_NVRAM", rdev) ||
73 region_device_sz(rdev) < BLOB_SIZE) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070074 printk(BIOS_ERR, "%s: failed to locate NVRAM\n", __func__);
75 return 1;
76 }
77
78 /* Prepare an empty blob to compare against. */
79 for (i = 0; i < BLOB_SIZE; i++)
80 empty_blob[i] = erase_value();
81
Aaron Durbinb18a6662016-08-12 12:48:58 -050082 ctx->top_offset = region_device_sz(rdev) - BLOB_SIZE;
Vadim Bendebury9e381a12015-01-10 23:11:43 -080083
Randall Spangleraa1d3142016-08-26 16:01:16 -070084 /* Binary search for the border between used and empty */
85 used_below = 0;
86 empty_above = region_device_sz(rdev) / BLOB_SIZE;
87
88 while (used_below + 1 < empty_above) {
89 int guess = (used_below + empty_above) / 2;
90 if (rdev_readat(rdev, buf, guess * BLOB_SIZE, BLOB_SIZE) < 0) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070091 printk(BIOS_ERR, "failed to read nvdata\n");
92 return 1;
93 }
94 if (!memcmp(buf, empty_blob, BLOB_SIZE))
Randall Spangleraa1d3142016-08-26 16:01:16 -070095 empty_above = guess;
96 else
97 used_below = guess;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070098 }
99
Randall Spangleraa1d3142016-08-26 16:01:16 -0700100 /*
101 * Offset points to the last non-empty blob. Or if all blobs are empty
102 * (nvram is totally erased), point to the first blob.
103 */
104 offset = used_below * BLOB_SIZE;
105
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700106 /* reread the nvdata and write it to the cache */
Aaron Durbinb18a6662016-08-12 12:48:58 -0500107 if (rdev_readat(rdev, ctx->cache, offset, BLOB_SIZE) < 0) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700108 printk(BIOS_ERR, "failed to read nvdata\n");
109 return 1;
110 }
111
Duncan Laurie01650382016-01-26 16:22:53 -0800112 ctx->blob_offset = offset;
113 ctx->initialized = 1;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700114
115 return 0;
116}
117
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700118static int erase_nvram(void)
119{
Duncan Laurie01650382016-01-26 16:22:53 -0800120 struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
Aaron Durbinb18a6662016-08-12 12:48:58 -0500121 const struct region_device *rdev = &ctx->vbnv_dev;
Duncan Laurie01650382016-01-26 16:22:53 -0800122
Aaron Durbinb18a6662016-08-12 12:48:58 -0500123 if (rdev_eraseat(rdev, 0, region_device_sz(rdev)) < 0) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700124 printk(BIOS_ERR, "failed to erase nvram\n");
125 return 1;
126 }
127
128 printk(BIOS_INFO, "nvram is cleared\n");
129 return 0;
130}
131
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800132void read_vbnv_flash(uint8_t *vbnv_copy)
Daisuke Nojiri477dd182014-10-08 11:32:23 -0700133{
Duncan Laurie01650382016-01-26 16:22:53 -0800134 struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
135
136 if (!ctx->initialized)
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700137 if (init_vbnv())
138 return; /* error */
Duncan Laurie01650382016-01-26 16:22:53 -0800139
140 memcpy(vbnv_copy, ctx->cache, BLOB_SIZE);
Daisuke Nojiri477dd182014-10-08 11:32:23 -0700141}
142
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800143void save_vbnv_flash(const uint8_t *vbnv_copy)
Daisuke Nojiri477dd182014-10-08 11:32:23 -0700144{
Duncan Laurie01650382016-01-26 16:22:53 -0800145 struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
Vadim Bendebury9e381a12015-01-10 23:11:43 -0800146 int new_offset;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700147 int i;
Aaron Durbinb18a6662016-08-12 12:48:58 -0500148 const struct region_device *rdev = &ctx->vbnv_dev;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700149
Duncan Laurie01650382016-01-26 16:22:53 -0800150 if (!ctx->initialized)
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700151 if (init_vbnv())
152 return; /* error */
153
154 /* Bail out if there have been no changes. */
Duncan Laurie01650382016-01-26 16:22:53 -0800155 if (!memcmp(vbnv_copy, ctx->cache, BLOB_SIZE))
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700156 return;
157
Duncan Laurie01650382016-01-26 16:22:53 -0800158 new_offset = ctx->blob_offset;
Vadim Bendebury9e381a12015-01-10 23:11:43 -0800159
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700160 /* See if we can overwrite the current blob with the new one */
161 for (i = 0; i < BLOB_SIZE; i++) {
Duncan Laurie01650382016-01-26 16:22:53 -0800162 if (!can_overwrite(ctx->cache[i], vbnv_copy[i])) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700163 /* unable to overwrite. need to use the next blob */
164 new_offset += BLOB_SIZE;
Duncan Laurie01650382016-01-26 16:22:53 -0800165 if (new_offset > ctx->top_offset) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700166 if (erase_nvram())
167 return; /* error */
Aaron Durbin4e50cdd2015-05-15 23:25:46 -0500168 new_offset = 0;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700169 }
170 break;
171 }
172 }
173
Aaron Durbin1b5581c2016-08-23 13:22:17 -0500174 if (rdev_writeat(rdev, vbnv_copy, new_offset, BLOB_SIZE) == BLOB_SIZE) {
Vadim Bendebury9e381a12015-01-10 23:11:43 -0800175 /* write was successful. safely move pointer forward */
Duncan Laurie01650382016-01-26 16:22:53 -0800176 ctx->blob_offset = new_offset;
177 memcpy(ctx->cache, vbnv_copy, BLOB_SIZE);
Vadim Bendebury9e381a12015-01-10 23:11:43 -0800178 } else {
179 printk(BIOS_ERR, "failed to save nvdata\n");
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700180 }
Daisuke Nojiri477dd182014-10-08 11:32:23 -0700181}