blob: 58d3aba2a754b42d904dda9ecaafbee1333ab14e [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
Furquan Shaikh2a12e2e2016-07-25 11:48:03 -070016#include <commonlib/region.h>
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070017#include <console/console.h>
Yu-Ping Wu29c8fa42019-11-18 11:25:47 +080018#include <fmap.h>
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070019#include <string.h>
Randall Spangler144c2282014-12-03 17:35:53 -080020#include <vb2_api.h>
Philipp Deppenwiesefea24292017-10-17 17:02:29 +020021#include <security/vboot/vboot_common.h>
22#include <security/vboot/vbnv.h>
23#include <security/vboot/vbnv_layout.h>
Daisuke Nojiri477dd182014-10-08 11:32:23 -070024
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070025#define BLOB_SIZE VB2_NVDATA_SIZE
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070026
Duncan Laurie01650382016-01-26 16:22:53 -080027struct vbnv_flash_ctx {
28 /* VBNV flash is initialized */
29 int initialized;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070030
Aaron Durbinb18a6662016-08-12 12:48:58 -050031 /* Offset of the current nvdata in flash */
Duncan Laurie01650382016-01-26 16:22:53 -080032 int blob_offset;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070033
Aaron Durbinb18a6662016-08-12 12:48:58 -050034 /* Offset of the topmost nvdata blob in flash */
Duncan Laurie01650382016-01-26 16:22:53 -080035 int top_offset;
Vadim Bendebury9e381a12015-01-10 23:11:43 -080036
Aaron Durbinb18a6662016-08-12 12:48:58 -050037 /* Region to store and retrieve the VBNV contents. */
38 struct region_device vbnv_dev;
Duncan Laurie01650382016-01-26 16:22:53 -080039
40 /* Cache of the current nvdata */
41 uint8_t cache[BLOB_SIZE];
42};
Arthur Heymans344e86b2019-11-20 19:47:10 +010043static struct vbnv_flash_ctx vbnv_flash;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070044
45/*
46 * This code assumes that flash is erased to 1-bits, and write operations can
47 * only change 1-bits to 0-bits. So if the new contents only change 1-bits to
48 * 0-bits, we can reuse the current blob.
49 */
50static inline uint8_t erase_value(void)
51{
52 return 0xff;
53}
54
55static inline int can_overwrite(uint8_t current, uint8_t new)
56{
57 return (current & new) == new;
58}
59
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070060static int init_vbnv(void)
61{
Arthur Heymans344e86b2019-11-20 19:47:10 +010062 struct vbnv_flash_ctx *ctx = &vbnv_flash;
Aaron Durbinb18a6662016-08-12 12:48:58 -050063 struct region_device *rdev = &ctx->vbnv_dev;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070064 uint8_t buf[BLOB_SIZE];
65 uint8_t empty_blob[BLOB_SIZE];
Randall Spangleraa1d3142016-08-26 16:01:16 -070066 int used_below, empty_above;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070067 int offset;
68 int i;
69
Yu-Ping Wu29c8fa42019-11-18 11:25:47 +080070 if (fmap_locate_area_as_rdev_rw("RW_NVRAM", rdev) ||
Aaron Durbinb18a6662016-08-12 12:48:58 -050071 region_device_sz(rdev) < BLOB_SIZE) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070072 printk(BIOS_ERR, "%s: failed to locate NVRAM\n", __func__);
73 return 1;
74 }
75
76 /* Prepare an empty blob to compare against. */
77 for (i = 0; i < BLOB_SIZE; i++)
78 empty_blob[i] = erase_value();
79
Aaron Durbinb18a6662016-08-12 12:48:58 -050080 ctx->top_offset = region_device_sz(rdev) - BLOB_SIZE;
Vadim Bendebury9e381a12015-01-10 23:11:43 -080081
Randall Spangleraa1d3142016-08-26 16:01:16 -070082 /* Binary search for the border between used and empty */
83 used_below = 0;
84 empty_above = region_device_sz(rdev) / BLOB_SIZE;
85
86 while (used_below + 1 < empty_above) {
87 int guess = (used_below + empty_above) / 2;
88 if (rdev_readat(rdev, buf, guess * BLOB_SIZE, BLOB_SIZE) < 0) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070089 printk(BIOS_ERR, "failed to read nvdata\n");
90 return 1;
91 }
92 if (!memcmp(buf, empty_blob, BLOB_SIZE))
Randall Spangleraa1d3142016-08-26 16:01:16 -070093 empty_above = guess;
94 else
95 used_below = guess;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -070096 }
97
Randall Spangleraa1d3142016-08-26 16:01:16 -070098 /*
99 * Offset points to the last non-empty blob. Or if all blobs are empty
100 * (nvram is totally erased), point to the first blob.
101 */
102 offset = used_below * BLOB_SIZE;
103
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700104 /* reread the nvdata and write it to the cache */
Aaron Durbinb18a6662016-08-12 12:48:58 -0500105 if (rdev_readat(rdev, ctx->cache, offset, BLOB_SIZE) < 0) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700106 printk(BIOS_ERR, "failed to read nvdata\n");
107 return 1;
108 }
109
Duncan Laurie01650382016-01-26 16:22:53 -0800110 ctx->blob_offset = offset;
111 ctx->initialized = 1;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700112
113 return 0;
114}
115
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700116static int erase_nvram(void)
117{
Arthur Heymans344e86b2019-11-20 19:47:10 +0100118 struct vbnv_flash_ctx *ctx = &vbnv_flash;
Aaron Durbinb18a6662016-08-12 12:48:58 -0500119 const struct region_device *rdev = &ctx->vbnv_dev;
Duncan Laurie01650382016-01-26 16:22:53 -0800120
Aaron Durbinb18a6662016-08-12 12:48:58 -0500121 if (rdev_eraseat(rdev, 0, region_device_sz(rdev)) < 0) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700122 printk(BIOS_ERR, "failed to erase nvram\n");
123 return 1;
124 }
125
126 printk(BIOS_INFO, "nvram is cleared\n");
127 return 0;
128}
129
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800130void read_vbnv_flash(uint8_t *vbnv_copy)
Daisuke Nojiri477dd182014-10-08 11:32:23 -0700131{
Arthur Heymans344e86b2019-11-20 19:47:10 +0100132 struct vbnv_flash_ctx *ctx = &vbnv_flash;
Duncan Laurie01650382016-01-26 16:22:53 -0800133
134 if (!ctx->initialized)
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700135 if (init_vbnv())
136 return; /* error */
Duncan Laurie01650382016-01-26 16:22:53 -0800137
138 memcpy(vbnv_copy, ctx->cache, BLOB_SIZE);
Daisuke Nojiri477dd182014-10-08 11:32:23 -0700139}
140
Duncan Laurie88b28ad2016-01-25 17:13:27 -0800141void save_vbnv_flash(const uint8_t *vbnv_copy)
Daisuke Nojiri477dd182014-10-08 11:32:23 -0700142{
Arthur Heymans344e86b2019-11-20 19:47:10 +0100143 struct vbnv_flash_ctx *ctx = &vbnv_flash;
Vadim Bendebury9e381a12015-01-10 23:11:43 -0800144 int new_offset;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700145 int i;
Aaron Durbinb18a6662016-08-12 12:48:58 -0500146 const struct region_device *rdev = &ctx->vbnv_dev;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700147
Duncan Laurie01650382016-01-26 16:22:53 -0800148 if (!ctx->initialized)
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700149 if (init_vbnv())
150 return; /* error */
151
152 /* Bail out if there have been no changes. */
Duncan Laurie01650382016-01-26 16:22:53 -0800153 if (!memcmp(vbnv_copy, ctx->cache, BLOB_SIZE))
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700154 return;
155
Duncan Laurie01650382016-01-26 16:22:53 -0800156 new_offset = ctx->blob_offset;
Vadim Bendebury9e381a12015-01-10 23:11:43 -0800157
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700158 /* See if we can overwrite the current blob with the new one */
159 for (i = 0; i < BLOB_SIZE; i++) {
Duncan Laurie01650382016-01-26 16:22:53 -0800160 if (!can_overwrite(ctx->cache[i], vbnv_copy[i])) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700161 /* unable to overwrite. need to use the next blob */
162 new_offset += BLOB_SIZE;
Duncan Laurie01650382016-01-26 16:22:53 -0800163 if (new_offset > ctx->top_offset) {
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700164 if (erase_nvram())
165 return; /* error */
Aaron Durbin4e50cdd2015-05-15 23:25:46 -0500166 new_offset = 0;
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700167 }
168 break;
169 }
170 }
171
Aaron Durbin1b5581c2016-08-23 13:22:17 -0500172 if (rdev_writeat(rdev, vbnv_copy, new_offset, BLOB_SIZE) == BLOB_SIZE) {
Vadim Bendebury9e381a12015-01-10 23:11:43 -0800173 /* write was successful. safely move pointer forward */
Duncan Laurie01650382016-01-26 16:22:53 -0800174 ctx->blob_offset = new_offset;
175 memcpy(ctx->cache, vbnv_copy, BLOB_SIZE);
Vadim Bendebury9e381a12015-01-10 23:11:43 -0800176 } else {
177 printk(BIOS_ERR, "failed to save nvdata\n");
Daisuke Nojiri2b59a832014-10-29 11:18:11 -0700178 }
Daisuke Nojiri477dd182014-10-08 11:32:23 -0700179}