blob: c123da9dc7cb37ab2e888915622478a9dbe9cef6 [file] [log] [blame]
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05001/*
2 * This file is part of the coreboot project.
3 *
Duncan Lauried8c4f2b2014-04-22 10:46:06 -07004 * Copyright (C) 2014 Google Inc.
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -05005 *
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.
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050014 */
15
16#include <string.h>
Aaron Durbin4d3de7e2015-09-02 17:34:04 -050017#include <bootstate.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050018#include <console/console.h>
19#include <cbmem.h>
Aaron Durbin0424c952015-03-28 23:56:22 -050020#include <fmap.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050021#include <ip_checksum.h>
Furquan Shaikh0325dc62016-07-25 13:02:36 -070022#include <vboot/vboot_common.h>
23
Duncan Lauried8c4f2b2014-04-22 10:46:06 -070024#include "mrc_cache.h"
Aaron Durbin4d3de7e2015-09-02 17:34:04 -050025#include "nvm.h"
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050026
27#define MRC_DATA_ALIGN 0x1000
28#define MRC_DATA_SIGNATURE (('M'<<0)|('R'<<8)|('C'<<16)|('D'<<24))
29
30/* The mrc_data_region describes the larger non-volatile area to store
31 * mrc_saved_data objects.*/
32struct mrc_data_region {
33 void *base;
34 uint32_t size;
35};
36
37/* common code */
38static int mrc_cache_get_region(struct mrc_data_region *region)
39{
Alexandru Gagniuc810caa92016-04-12 14:52:29 -070040 bool located_by_fmap = true;
41 struct region_device rdev;
Aaron Durbin0424c952015-03-28 23:56:22 -050042
Alexandru Gagniuc810caa92016-04-12 14:52:29 -070043 if (fmap_locate_area_as_rdev("RW_MRC_CACHE", &rdev))
44 located_by_fmap = false;
Aaron Durbin0424c952015-03-28 23:56:22 -050045
Alexandru Gagniuc810caa92016-04-12 14:52:29 -070046 /* CHROMEOS builds must get their MRC cache from FMAP. */
47 if (IS_ENABLED(CONFIG_CHROMEOS) && !located_by_fmap)
48 return -1;
49
50 if (located_by_fmap) {
Aaron Durbin0424c952015-03-28 23:56:22 -050051 region->size = region_device_sz(&rdev);
52 region->base = rdev_mmap_full(&rdev);
53
54 if (region->base == NULL)
55 return -1;
Aaron Durbin0424c952015-03-28 23:56:22 -050056 } else {
57 region->base = (void *)CONFIG_MRC_SETTINGS_CACHE_BASE;
58 region->size = CONFIG_MRC_SETTINGS_CACHE_SIZE;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050059 }
Alexandru Gagniuc810caa92016-04-12 14:52:29 -070060
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050061 return 0;
62}
63
64static int mrc_cache_in_region(const struct mrc_data_region *region,
65 const struct mrc_saved_data *cache)
66{
67 uintptr_t region_end;
68 uintptr_t cache_end;
69
70 if ((uintptr_t)cache < (uintptr_t)region->base)
71 return 0;
72
73 region_end = (uintptr_t)region->base;
74 region_end += region->size;
75
76 if ((uintptr_t)cache >= region_end)
77 return 0;
78
79 if ((sizeof(*cache) + (uintptr_t)cache) >= region_end)
80 return 0;
81
82 cache_end = (uintptr_t)cache;
83 cache_end += cache->size + sizeof(*cache);
84
85 if (cache_end > region_end)
86 return 0;
87
88 return 1;
89}
90
91static int mrc_cache_valid(const struct mrc_data_region *region,
92 const struct mrc_saved_data *cache)
93{
94 uint32_t checksum;
95
96 if (cache->signature != MRC_DATA_SIGNATURE)
97 return 0;
98
99 if (cache->size > region->size)
100 return 0;
101
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500102 checksum = compute_ip_checksum((void *)&cache->data[0], cache->size);
103
104 if (cache->checksum != checksum)
105 return 0;
106
107 return 1;
108}
109
110static const struct mrc_saved_data *
111next_cache_block(const struct mrc_saved_data *cache)
112{
113 uintptr_t next = (uintptr_t)cache;
114
115 next += ALIGN(cache->size + sizeof(*cache), MRC_DATA_ALIGN);
116
117 return (const struct mrc_saved_data *)next;
118}
119
120/* Locate the most recently saved MRC data. */
121static int __mrc_cache_get_current(const struct mrc_data_region *region,
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600122 const struct mrc_saved_data **cache,
123 uint32_t version)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500124{
125 const struct mrc_saved_data *msd;
126 const struct mrc_saved_data *verified_cache;
Duncan Lauried8c4f2b2014-04-22 10:46:06 -0700127 int slot = 0;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500128
129 msd = region->base;
130
131 verified_cache = NULL;
132
133 while (mrc_cache_in_region(region, msd) &&
134 mrc_cache_valid(region, msd)) {
135 verified_cache = msd;
136 msd = next_cache_block(msd);
Duncan Lauried8c4f2b2014-04-22 10:46:06 -0700137 slot++;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500138 }
139
Furquan Shaikhc3197372016-07-27 08:04:54 -0700140 /*
141 * Update pointer to the most recently saved MRC data before returning
142 * any error. This ensures that the caller can use next available slot
143 * if required.
144 */
145 *cache = verified_cache;
146
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500147 if (verified_cache == NULL)
148 return -1;
149
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600150 if (verified_cache->version != version) {
151 printk(BIOS_DEBUG, "MRC cache version mismatch: %x vs %x\n",
152 verified_cache->version, version);
153 return -1;
154 }
155
Duncan Lauried8c4f2b2014-04-22 10:46:06 -0700156 printk(BIOS_DEBUG, "MRC cache slot %d @ %p\n", slot-1, verified_cache);
157
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500158 return 0;
159}
160
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600161int mrc_cache_get_current_with_version(const struct mrc_saved_data **cache,
162 uint32_t version)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500163{
164 struct mrc_data_region region;
165
166 if (mrc_cache_get_region(&region) < 0)
167 return -1;
168
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600169 return __mrc_cache_get_current(&region, cache, version);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500170}
171
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600172int mrc_cache_get_current(const struct mrc_saved_data **cache)
173{
174 return mrc_cache_get_current_with_version(cache, 0);
175}
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500176/* Fill in mrc_saved_data structure with payload. */
Alexandru Gagniuc1116fa82016-03-04 14:38:19 -0800177static void mrc_cache_fill(struct mrc_saved_data *cache, const void *data,
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600178 size_t size, uint32_t version)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500179{
180 cache->signature = MRC_DATA_SIGNATURE;
181 cache->size = size;
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600182 cache->version = version;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500183 memcpy(&cache->data[0], data, size);
184 cache->checksum = compute_ip_checksum((void *)&cache->data[0],
185 cache->size);
186}
187
Alexandru Gagniuc1116fa82016-03-04 14:38:19 -0800188int mrc_cache_stash_data_with_version(const void *data, size_t size,
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600189 uint32_t version)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500190{
191 int cbmem_size;
192 struct mrc_saved_data *cache;
193
194 cbmem_size = sizeof(*cache) + ALIGN(size, 16);
195
196 cache = cbmem_add(CBMEM_ID_MRCDATA, cbmem_size);
197
198 if (cache == NULL) {
199 printk(BIOS_ERR, "No space in cbmem for MRC data.\n");
200 return -1;
201 }
202
Aaron Durbin4177db52014-02-05 14:55:26 -0600203 /* Clear alignment padding bytes at end of data. */
204 memset(&cache->data[size], 0, cbmem_size - size - sizeof(*cache));
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500205
206 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%zu bytes)\n",
207 data, cache, size);
208
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600209 mrc_cache_fill(cache, data, size, version);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500210
211 return 0;
212}
213
Alexandru Gagniuc1116fa82016-03-04 14:38:19 -0800214int mrc_cache_stash_data(const void *data, size_t size)
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600215{
216 return mrc_cache_stash_data_with_version(data, size, 0);
217}
218
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500219static int mrc_slot_valid(const struct mrc_data_region *region,
220 const struct mrc_saved_data *slot,
221 const struct mrc_saved_data *to_save)
222{
223 uintptr_t region_begin;
224 uintptr_t region_end;
225 uintptr_t slot_end;
226 uintptr_t slot_begin;
227 uint32_t size;
228
229 region_begin = (uintptr_t)region->base;
230 region_end = region_begin + region->size;
231 slot_begin = (uintptr_t)slot;
232 size = to_save->size + sizeof(*to_save);
233 slot_end = slot_begin + size;
234
Aaron Durbin4177db52014-02-05 14:55:26 -0600235 if (slot_begin < region_begin || slot_begin >= region_end)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500236 return 0;
237
238 if (size > region->size)
239 return 0;
240
241 if (slot_end > region_end || slot_end < region_begin)
242 return 0;
243
244 if (!nvm_is_erased(slot, size))
245 return 0;
246
247 return 1;
248}
249
250static const struct mrc_saved_data *
251mrc_cache_next_slot(const struct mrc_data_region *region,
252 const struct mrc_saved_data *current_slot)
253{
254 const struct mrc_saved_data *next_slot;
255
256 if (current_slot == NULL) {
257 next_slot = region->base;
258 } else {
259 next_slot = next_cache_block(current_slot);
260 }
261
262 return next_slot;
263}
264
Duncan Lauriea32b6b92015-01-15 15:49:07 -0800265/* Protect RW_MRC_CACHE region with a Protected Range Register */
266static int protect_mrc_cache(const struct mrc_data_region *region)
267{
268#if IS_ENABLED(CONFIG_MRC_SETTINGS_PROTECT)
269 if (nvm_is_write_protected() <= 0) {
270 printk(BIOS_INFO, "NOT enabling PRR for RW_MRC_CACHE region\n");
271 return 1;
272 }
273
274 if (nvm_protect(region->base, region->size) < 0) {
275 printk(BIOS_ERR, "ERROR setting PRR for RW_MRC_CACHE region\n");
276 return -1;
277 }
278
279 printk(BIOS_INFO, "Enabled Protected Range on RW_MRC_CACHE region\n");
280#endif
281 return 0;
282}
283
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500284static void update_mrc_cache(void *unused)
285{
286 const struct mrc_saved_data *current_boot;
287 const struct mrc_saved_data *current_saved;
288 const struct mrc_saved_data *next_slot;
289 struct mrc_data_region region;
290
291 printk(BIOS_DEBUG, "Updating MRC cache data.\n");
292
293 current_boot = cbmem_find(CBMEM_ID_MRCDATA);
294 if (!current_boot) {
295 printk(BIOS_ERR, "No MRC cache in cbmem.\n");
296 return;
297 }
298
299 if (mrc_cache_get_region(&region)) {
300 printk(BIOS_ERR, "Could not obtain MRC cache region.\n");
301 return;
302 }
303
304 if (!mrc_cache_valid(&region, current_boot)) {
305 printk(BIOS_ERR, "MRC cache data in cbmem invalid.\n");
306 return;
307 }
308
309 current_saved = NULL;
310
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600311 if (!__mrc_cache_get_current(&region, &current_saved,
312 current_boot->version)) {
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500313 if (current_saved->size == current_boot->size &&
314 !memcmp(&current_saved->data[0], &current_boot->data[0],
315 current_saved->size)) {
316 printk(BIOS_DEBUG, "MRC cache up to date.\n");
Duncan Lauriea32b6b92015-01-15 15:49:07 -0800317 protect_mrc_cache(&region);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500318 return;
319 }
320 }
321
322 next_slot = mrc_cache_next_slot(&region, current_saved);
323
324 if (!mrc_slot_valid(&region, next_slot, current_boot)) {
Aaron Durbin931e5902014-01-13 11:34:51 -0600325 printk(BIOS_DEBUG, "Slot @ %p is invalid.\n", next_slot);
326 if (!nvm_is_erased(region.base, region.size)) {
327 if (nvm_erase(region.base, region.size) < 0) {
328 printk(BIOS_DEBUG, "Failure erasing region.\n");
329 return;
330 }
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500331 }
332 next_slot = region.base;
333 }
334
Aaron Durbin4177db52014-02-05 14:55:26 -0600335 if (nvm_write((void *)next_slot, current_boot,
336 current_boot->size + sizeof(*current_boot))) {
337 printk(BIOS_DEBUG, "Failure writing MRC cache to %p.\n",
338 next_slot);
339 }
Duncan Lauriea32b6b92015-01-15 15:49:07 -0800340 protect_mrc_cache(&region);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500341}
342
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500343BOOT_STATE_INIT_ENTRY(BS_WRITE_TABLES, BS_ON_ENTRY, update_mrc_cache, NULL);