blob: 85cd04a8e5d89a9654afd4047a184bf5c4fb6faa [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>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020022#include <security/vboot/vboot_common.h>
23#include <security/vboot/vbnv.h>
24#include <security/vboot/vbnv_layout.h>
Daisuke Nojiri477dd182014-10-08 11:32:23 -070025
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070026#define BLOB_SIZE VB2_NVDATA_SIZE
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070027
Duncan Laurie01650382016-01-26 16:22:53 -080028struct vbnv_flash_ctx {
29 /* VBNV flash is initialized */
30 int initialized;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070031
Aaron Durbinb18a6662016-08-12 12:48:58 -050032 /* Offset of the current nvdata in flash */
Duncan Laurie01650382016-01-26 16:22:53 -080033 int blob_offset;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070034
Aaron Durbinb18a6662016-08-12 12:48:58 -050035 /* Offset of the topmost nvdata blob in flash */
Duncan Laurie01650382016-01-26 16:22:53 -080036 int top_offset;
Vadim Bendebury9e381a12015-01-10 23:11:43 -080037
Aaron Durbinb18a6662016-08-12 12:48:58 -050038 /* Region to store and retrieve the VBNV contents. */
39 struct region_device vbnv_dev;
Duncan Laurie01650382016-01-26 16:22:53 -080040
41 /* Cache of the current nvdata */
42 uint8_t cache[BLOB_SIZE];
43};
44static struct vbnv_flash_ctx vbnv_flash CAR_GLOBAL;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070045
46/*
47 * This code assumes that flash is erased to 1-bits, and write operations can
48 * only change 1-bits to 0-bits. So if the new contents only change 1-bits to
49 * 0-bits, we can reuse the current blob.
50 */
51static inline uint8_t erase_value(void)
52{
53 return 0xff;
54}
55
56static inline int can_overwrite(uint8_t current, uint8_t new)
57{
58 return (current & new) == new;
59}
60
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070061static int init_vbnv(void)
62{
Duncan Laurie01650382016-01-26 16:22:53 -080063 struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
Aaron Durbinb18a6662016-08-12 12:48:58 -050064 struct region_device *rdev = &ctx->vbnv_dev;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070065 uint8_t buf[BLOB_SIZE];
66 uint8_t empty_blob[BLOB_SIZE];
Randall Spangleraa1d3142016-08-26 16:01:16 -070067 int used_below, empty_above;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070068 int offset;
69 int i;
70
Aaron Durbinb18a6662016-08-12 12:48:58 -050071 if (vboot_named_region_device_rw("RW_NVRAM", rdev) ||
72 region_device_sz(rdev) < BLOB_SIZE) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070073 printk(BIOS_ERR, "%s: failed to locate NVRAM\n", __func__);
74 return 1;
75 }
76
77 /* Prepare an empty blob to compare against. */
78 for (i = 0; i < BLOB_SIZE; i++)
79 empty_blob[i] = erase_value();
80
Aaron Durbinb18a6662016-08-12 12:48:58 -050081 ctx->top_offset = region_device_sz(rdev) - BLOB_SIZE;
Vadim Bendebury9e381a12015-01-10 23:11:43 -080082
Randall Spangleraa1d3142016-08-26 16:01:16 -070083 /* Binary search for the border between used and empty */
84 used_below = 0;
85 empty_above = region_device_sz(rdev) / BLOB_SIZE;
86
87 while (used_below + 1 < empty_above) {
88 int guess = (used_below + empty_above) / 2;
89 if (rdev_readat(rdev, buf, guess * BLOB_SIZE, BLOB_SIZE) < 0) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070090 printk(BIOS_ERR, "failed to read nvdata\n");
91 return 1;
92 }
93 if (!memcmp(buf, empty_blob, BLOB_SIZE))
Randall Spangleraa1d3142016-08-26 16:01:16 -070094 empty_above = guess;
95 else
96 used_below = guess;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070097 }
98
Randall Spangleraa1d3142016-08-26 16:01:16 -070099 /*
100 * Offset points to the last non-empty blob. Or if all blobs are empty
101 * (nvram is totally erased), point to the first blob.
102 */
103 offset = used_below * BLOB_SIZE;
104
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700105 /* reread the nvdata and write it to the cache */
Aaron Durbinb18a6662016-08-12 12:48:58 -0500106 if (rdev_readat(rdev, ctx->cache, offset, BLOB_SIZE) < 0) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700107 printk(BIOS_ERR, "failed to read nvdata\n");
108 return 1;
109 }
110
Duncan Laurie01650382016-01-26 16:22:53 -0800111 ctx->blob_offset = offset;
112 ctx->initialized = 1;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700113
114 return 0;
115}
116
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700117static int erase_nvram(void)
118{
Duncan Laurie01650382016-01-26 16:22:53 -0800119 struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
Aaron Durbinb18a6662016-08-12 12:48:58 -0500120 const struct region_device *rdev = &ctx->vbnv_dev;
Duncan Laurie01650382016-01-26 16:22:53 -0800121
Aaron Durbinb18a6662016-08-12 12:48:58 -0500122 if (rdev_eraseat(rdev, 0, region_device_sz(rdev)) < 0) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700123 printk(BIOS_ERR, "failed to erase nvram\n");
124 return 1;
125 }
126
127 printk(BIOS_INFO, "nvram is cleared\n");
128 return 0;
129}
130
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800131void read_vbnv_flash(uint8_t *vbnv_copy)
Daisuke Nojiri477dd182014-10-08 11:32:23 -0700132{
Duncan Laurie01650382016-01-26 16:22:53 -0800133 struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
134
135 if (!ctx->initialized)
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700136 if (init_vbnv())
137 return; /* error */
Duncan Laurie01650382016-01-26 16:22:53 -0800138
139 memcpy(vbnv_copy, ctx->cache, BLOB_SIZE);
Daisuke Nojiri477dd182014-10-08 11:32:23 -0700140}
141
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800142void save_vbnv_flash(const uint8_t *vbnv_copy)
Daisuke Nojiri477dd182014-10-08 11:32:23 -0700143{
Duncan Laurie01650382016-01-26 16:22:53 -0800144 struct vbnv_flash_ctx *ctx = car_get_var_ptr(&vbnv_flash);
Vadim Bendebury9e381a12015-01-10 23:11:43 -0800145 int new_offset;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700146 int i;
Aaron Durbinb18a6662016-08-12 12:48:58 -0500147 const struct region_device *rdev = &ctx->vbnv_dev;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700148
Duncan Laurie01650382016-01-26 16:22:53 -0800149 if (!ctx->initialized)
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700150 if (init_vbnv())
151 return; /* error */
152
153 /* Bail out if there have been no changes. */
Duncan Laurie01650382016-01-26 16:22:53 -0800154 if (!memcmp(vbnv_copy, ctx->cache, BLOB_SIZE))
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700155 return;
156
Duncan Laurie01650382016-01-26 16:22:53 -0800157 new_offset = ctx->blob_offset;
Vadim Bendebury9e381a12015-01-10 23:11:43 -0800158
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700159 /* See if we can overwrite the current blob with the new one */
160 for (i = 0; i < BLOB_SIZE; i++) {
Duncan Laurie01650382016-01-26 16:22:53 -0800161 if (!can_overwrite(ctx->cache[i], vbnv_copy[i])) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700162 /* unable to overwrite. need to use the next blob */
163 new_offset += BLOB_SIZE;
Duncan Laurie01650382016-01-26 16:22:53 -0800164 if (new_offset > ctx->top_offset) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700165 if (erase_nvram())
166 return; /* error */
Aaron Durbin4e50cdd2015-05-15 23:25:46 -0500167 new_offset = 0;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700168 }
169 break;
170 }
171 }
172
Aaron Durbin1b5581c2016-08-23 13:22:17 -0500173 if (rdev_writeat(rdev, vbnv_copy, new_offset, BLOB_SIZE) == BLOB_SIZE) {
Vadim Bendebury9e381a12015-01-10 23:11:43 -0800174 /* write was successful. safely move pointer forward */
Duncan Laurie01650382016-01-26 16:22:53 -0800175 ctx->blob_offset = new_offset;
176 memcpy(ctx->cache, vbnv_copy, BLOB_SIZE);
Vadim Bendebury9e381a12015-01-10 23:11:43 -0800177 } else {
178 printk(BIOS_ERR, "failed to save nvdata\n");
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700179 }
Daisuke Nojiri477dd182014-10-08 11:32:23 -0700180}