blob: 4ad41582b0980999fd2652e0d77a238b61225f3c [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>
Duncan Lauried8c4f2b2014-04-22 10:46:06 -070022#include "mrc_cache.h"
Aaron Durbin4d3de7e2015-09-02 17:34:04 -050023#include "nvm.h"
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050024
25#define MRC_DATA_ALIGN 0x1000
26#define MRC_DATA_SIGNATURE (('M'<<0)|('R'<<8)|('C'<<16)|('D'<<24))
27
28/* The mrc_data_region describes the larger non-volatile area to store
29 * mrc_saved_data objects.*/
30struct mrc_data_region {
31 void *base;
32 uint32_t size;
33};
34
35/* common code */
36static int mrc_cache_get_region(struct mrc_data_region *region)
37{
Alexandru Gagniuc810caa92016-04-12 14:52:29 -070038 bool located_by_fmap = true;
39 struct region_device rdev;
Aaron Durbin0424c952015-03-28 23:56:22 -050040
Alexandru Gagniuc810caa92016-04-12 14:52:29 -070041 if (fmap_locate_area_as_rdev("RW_MRC_CACHE", &rdev))
42 located_by_fmap = false;
Aaron Durbin0424c952015-03-28 23:56:22 -050043
Alexandru Gagniuc810caa92016-04-12 14:52:29 -070044 /* CHROMEOS builds must get their MRC cache from FMAP. */
45 if (IS_ENABLED(CONFIG_CHROMEOS) && !located_by_fmap)
46 return -1;
47
48 if (located_by_fmap) {
Aaron Durbin0424c952015-03-28 23:56:22 -050049 region->size = region_device_sz(&rdev);
50 region->base = rdev_mmap_full(&rdev);
51
52 if (region->base == NULL)
53 return -1;
Aaron Durbin0424c952015-03-28 23:56:22 -050054 } else {
55 region->base = (void *)CONFIG_MRC_SETTINGS_CACHE_BASE;
56 region->size = CONFIG_MRC_SETTINGS_CACHE_SIZE;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050057 }
Alexandru Gagniuc810caa92016-04-12 14:52:29 -070058
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050059 return 0;
60}
61
62static int mrc_cache_in_region(const struct mrc_data_region *region,
63 const struct mrc_saved_data *cache)
64{
65 uintptr_t region_end;
66 uintptr_t cache_end;
67
68 if ((uintptr_t)cache < (uintptr_t)region->base)
69 return 0;
70
71 region_end = (uintptr_t)region->base;
72 region_end += region->size;
73
74 if ((uintptr_t)cache >= region_end)
75 return 0;
76
77 if ((sizeof(*cache) + (uintptr_t)cache) >= region_end)
78 return 0;
79
80 cache_end = (uintptr_t)cache;
81 cache_end += cache->size + sizeof(*cache);
82
83 if (cache_end > region_end)
84 return 0;
85
86 return 1;
87}
88
89static int mrc_cache_valid(const struct mrc_data_region *region,
90 const struct mrc_saved_data *cache)
91{
92 uint32_t checksum;
93
94 if (cache->signature != MRC_DATA_SIGNATURE)
95 return 0;
96
97 if (cache->size > region->size)
98 return 0;
99
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500100 checksum = compute_ip_checksum((void *)&cache->data[0], cache->size);
101
102 if (cache->checksum != checksum)
103 return 0;
104
105 return 1;
106}
107
108static const struct mrc_saved_data *
109next_cache_block(const struct mrc_saved_data *cache)
110{
111 uintptr_t next = (uintptr_t)cache;
112
113 next += ALIGN(cache->size + sizeof(*cache), MRC_DATA_ALIGN);
114
115 return (const struct mrc_saved_data *)next;
116}
117
118/* Locate the most recently saved MRC data. */
119static int __mrc_cache_get_current(const struct mrc_data_region *region,
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600120 const struct mrc_saved_data **cache,
121 uint32_t version)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500122{
123 const struct mrc_saved_data *msd;
124 const struct mrc_saved_data *verified_cache;
Duncan Lauried8c4f2b2014-04-22 10:46:06 -0700125 int slot = 0;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500126
127 msd = region->base;
128
129 verified_cache = NULL;
130
131 while (mrc_cache_in_region(region, msd) &&
132 mrc_cache_valid(region, msd)) {
133 verified_cache = msd;
134 msd = next_cache_block(msd);
Duncan Lauried8c4f2b2014-04-22 10:46:06 -0700135 slot++;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500136 }
137
138 if (verified_cache == NULL)
139 return -1;
140
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600141 if (verified_cache->version != version) {
142 printk(BIOS_DEBUG, "MRC cache version mismatch: %x vs %x\n",
143 verified_cache->version, version);
144 return -1;
145 }
146
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500147 *cache = verified_cache;
Duncan Lauried8c4f2b2014-04-22 10:46:06 -0700148 printk(BIOS_DEBUG, "MRC cache slot %d @ %p\n", slot-1, verified_cache);
149
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500150 return 0;
151}
152
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600153int mrc_cache_get_current_with_version(const struct mrc_saved_data **cache,
154 uint32_t version)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500155{
156 struct mrc_data_region region;
157
158 if (mrc_cache_get_region(&region) < 0)
159 return -1;
160
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600161 return __mrc_cache_get_current(&region, cache, version);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500162}
163
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600164int mrc_cache_get_current(const struct mrc_saved_data **cache)
165{
166 return mrc_cache_get_current_with_version(cache, 0);
167}
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500168/* Fill in mrc_saved_data structure with payload. */
Alexandru Gagniuc1116fa82016-03-04 14:38:19 -0800169static void mrc_cache_fill(struct mrc_saved_data *cache, const void *data,
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600170 size_t size, uint32_t version)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500171{
172 cache->signature = MRC_DATA_SIGNATURE;
173 cache->size = size;
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600174 cache->version = version;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500175 memcpy(&cache->data[0], data, size);
176 cache->checksum = compute_ip_checksum((void *)&cache->data[0],
177 cache->size);
178}
179
Alexandru Gagniuc1116fa82016-03-04 14:38:19 -0800180int mrc_cache_stash_data_with_version(const void *data, size_t size,
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600181 uint32_t version)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500182{
183 int cbmem_size;
184 struct mrc_saved_data *cache;
185
186 cbmem_size = sizeof(*cache) + ALIGN(size, 16);
187
188 cache = cbmem_add(CBMEM_ID_MRCDATA, cbmem_size);
189
190 if (cache == NULL) {
191 printk(BIOS_ERR, "No space in cbmem for MRC data.\n");
192 return -1;
193 }
194
Aaron Durbin4177db52014-02-05 14:55:26 -0600195 /* Clear alignment padding bytes at end of data. */
196 memset(&cache->data[size], 0, cbmem_size - size - sizeof(*cache));
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500197
198 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%zu bytes)\n",
199 data, cache, size);
200
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600201 mrc_cache_fill(cache, data, size, version);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500202
203 return 0;
204}
205
Alexandru Gagniuc1116fa82016-03-04 14:38:19 -0800206int mrc_cache_stash_data(const void *data, size_t size)
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600207{
208 return mrc_cache_stash_data_with_version(data, size, 0);
209}
210
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500211static int mrc_slot_valid(const struct mrc_data_region *region,
212 const struct mrc_saved_data *slot,
213 const struct mrc_saved_data *to_save)
214{
215 uintptr_t region_begin;
216 uintptr_t region_end;
217 uintptr_t slot_end;
218 uintptr_t slot_begin;
219 uint32_t size;
220
221 region_begin = (uintptr_t)region->base;
222 region_end = region_begin + region->size;
223 slot_begin = (uintptr_t)slot;
224 size = to_save->size + sizeof(*to_save);
225 slot_end = slot_begin + size;
226
Aaron Durbin4177db52014-02-05 14:55:26 -0600227 if (slot_begin < region_begin || slot_begin >= region_end)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500228 return 0;
229
230 if (size > region->size)
231 return 0;
232
233 if (slot_end > region_end || slot_end < region_begin)
234 return 0;
235
236 if (!nvm_is_erased(slot, size))
237 return 0;
238
239 return 1;
240}
241
242static const struct mrc_saved_data *
243mrc_cache_next_slot(const struct mrc_data_region *region,
244 const struct mrc_saved_data *current_slot)
245{
246 const struct mrc_saved_data *next_slot;
247
248 if (current_slot == NULL) {
249 next_slot = region->base;
250 } else {
251 next_slot = next_cache_block(current_slot);
252 }
253
254 return next_slot;
255}
256
Duncan Lauriea32b6b92015-01-15 15:49:07 -0800257/* Protect RW_MRC_CACHE region with a Protected Range Register */
258static int protect_mrc_cache(const struct mrc_data_region *region)
259{
260#if IS_ENABLED(CONFIG_MRC_SETTINGS_PROTECT)
261 if (nvm_is_write_protected() <= 0) {
262 printk(BIOS_INFO, "NOT enabling PRR for RW_MRC_CACHE region\n");
263 return 1;
264 }
265
266 if (nvm_protect(region->base, region->size) < 0) {
267 printk(BIOS_ERR, "ERROR setting PRR for RW_MRC_CACHE region\n");
268 return -1;
269 }
270
271 printk(BIOS_INFO, "Enabled Protected Range on RW_MRC_CACHE region\n");
272#endif
273 return 0;
274}
275
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500276static void update_mrc_cache(void *unused)
277{
278 const struct mrc_saved_data *current_boot;
279 const struct mrc_saved_data *current_saved;
280 const struct mrc_saved_data *next_slot;
281 struct mrc_data_region region;
282
283 printk(BIOS_DEBUG, "Updating MRC cache data.\n");
284
285 current_boot = cbmem_find(CBMEM_ID_MRCDATA);
286 if (!current_boot) {
287 printk(BIOS_ERR, "No MRC cache in cbmem.\n");
288 return;
289 }
290
291 if (mrc_cache_get_region(&region)) {
292 printk(BIOS_ERR, "Could not obtain MRC cache region.\n");
293 return;
294 }
295
296 if (!mrc_cache_valid(&region, current_boot)) {
297 printk(BIOS_ERR, "MRC cache data in cbmem invalid.\n");
298 return;
299 }
300
301 current_saved = NULL;
302
Aaron Durbinbc6e7c02015-12-09 15:04:49 -0600303 if (!__mrc_cache_get_current(&region, &current_saved,
304 current_boot->version)) {
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500305 if (current_saved->size == current_boot->size &&
306 !memcmp(&current_saved->data[0], &current_boot->data[0],
307 current_saved->size)) {
308 printk(BIOS_DEBUG, "MRC cache up to date.\n");
Duncan Lauriea32b6b92015-01-15 15:49:07 -0800309 protect_mrc_cache(&region);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500310 return;
311 }
312 }
313
314 next_slot = mrc_cache_next_slot(&region, current_saved);
315
316 if (!mrc_slot_valid(&region, next_slot, current_boot)) {
Aaron Durbin931e5902014-01-13 11:34:51 -0600317 printk(BIOS_DEBUG, "Slot @ %p is invalid.\n", next_slot);
318 if (!nvm_is_erased(region.base, region.size)) {
319 if (nvm_erase(region.base, region.size) < 0) {
320 printk(BIOS_DEBUG, "Failure erasing region.\n");
321 return;
322 }
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500323 }
324 next_slot = region.base;
325 }
326
Aaron Durbin4177db52014-02-05 14:55:26 -0600327 if (nvm_write((void *)next_slot, current_boot,
328 current_boot->size + sizeof(*current_boot))) {
329 printk(BIOS_DEBUG, "Failure writing MRC cache to %p.\n",
330 next_slot);
331 }
Duncan Lauriea32b6b92015-01-15 15:49:07 -0800332 protect_mrc_cache(&region);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500333}
334
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500335BOOT_STATE_INIT_ENTRY(BS_WRITE_TABLES, BS_ON_ENTRY, update_mrc_cache, NULL);