summaryrefslogtreecommitdiffstats
path: root/src/vendorcode/google/chromeos/vbnv_flash.c
blob: fe2667ef2d36adfb1c54aa3d6c5c18a62b1df6ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
/*
 * 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.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc.
 *
 * TODO: Make this CAR-friendly in case we use it on x86 some day.
 */

#include <cbfs.h>
#include <cbfs_core.h>
#include <console/console.h>
#include <spi_flash.h>
#include <string.h>
#include <vb2_api.h>
#include <vboot_nvstorage.h>
#include "chromeos.h"
#include "vbnv_layout.h"

#define BLOB_SIZE VB2_NVDATA_SIZE

/* FMAP descriptor of the NVRAM area */
static struct vboot_region nvram_region;

/* offset of the current nvdata in SPI flash */
static int blob_offset = -1;

/* Offset of the topmost nvdata blob in SPI flash */
static int top_offset;

/* cache of the current nvdata */
static uint8_t cache[BLOB_SIZE];

/* spi_flash struct used when saving data */
static struct spi_flash *spi_flash = NULL;

/*
 * 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 inline int is_initialized(void)
{
	return blob_offset >= 0;
}

static int init_vbnv(void)
{
	uint8_t buf[BLOB_SIZE];
	uint8_t empty_blob[BLOB_SIZE];
	int offset;
	int i;

	vboot_locate_region("RW_NVRAM", &nvram_region);
	if (nvram_region.size < BLOB_SIZE) {
		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();

	offset = nvram_region.offset_addr;
	top_offset = nvram_region.offset_addr + nvram_region.size - BLOB_SIZE;

	/*
	 * after the loop, offset is supposed to point the blob right before
	 * the first empty blob, the last blob in the nvram if there is no
	 * empty blob, or the base of the region if the nvram has never been
	 * used.
	 */
	for (i = offset; i <= top_offset; i += BLOB_SIZE) {
		if (vboot_get_region(i, BLOB_SIZE, buf) == NULL) {
			printk(BIOS_ERR, "failed to read nvdata\n");
			return 1;
		}
		if (!memcmp(buf, empty_blob, BLOB_SIZE))
			break;
		offset = i;
	}

	/* reread the nvdata and write it to the cache */
	if (vboot_get_region(offset, BLOB_SIZE, cache) == NULL) {
		printk(BIOS_ERR, "failed to read nvdata\n");
		return 1;
	}

	blob_offset = offset;

	return 0;
}

static int vbnv_flash_probe(void)
{
	if (!spi_flash) {
		spi_flash = spi_flash_probe(CONFIG_BOOT_MEDIA_SPI_BUS, 0);
		if (!spi_flash) {
			printk(BIOS_ERR, "failed to probe spi flash\n");
			return 1;
		}
	}
	return 0;
}

static int erase_nvram(void)
{
	if (vbnv_flash_probe())
		return 1;

	if (spi_flash->erase(spi_flash, nvram_region.offset_addr,
			     nvram_region.size)) {
		printk(BIOS_ERR, "failed to erase nvram\n");
		return 1;
	}

	printk(BIOS_INFO, "nvram is cleared\n");
	return 0;
}

void read_vbnv(uint8_t *vbnv_copy)
{
	if (!is_initialized())
		if (init_vbnv())
			return;  /* error */
	memcpy(vbnv_copy, cache, BLOB_SIZE);
}

void save_vbnv(const uint8_t *vbnv_copy)
{
	int new_offset;
	int i;

	if (!is_initialized())
		if (init_vbnv())
			return;  /* error */

	/* Bail out if there have been no changes. */
	if (!memcmp(vbnv_copy, cache, BLOB_SIZE))
		return;

	new_offset = 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])) {
			/* unable to overwrite. need to use the next blob */
			new_offset += BLOB_SIZE;
			if (new_offset > top_offset) {
				if (erase_nvram())
					return;  /* error */
				new_offset = nvram_region.offset_addr;
			}
			break;
		}
	}

	if (!vbnv_flash_probe() &&
	    !spi_flash->write(spi_flash, new_offset, BLOB_SIZE, vbnv_copy)) {
		/* write was successful. safely move pointer forward */
		blob_offset = new_offset;
		memcpy(cache, vbnv_copy, BLOB_SIZE);
	} else {
		printk(BIOS_ERR, "failed to save nvdata\n");
	}
}

int get_recovery_mode_from_vbnv(void)
{
	if (!is_initialized())
		init_vbnv();
	return cache[RECOVERY_OFFSET];
}