blob: e5e6b6a142bf31cd52d33a4c4f2924b9d069e15f [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.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
Patrick Georgib890a122015-03-26 15:17:45 +010017 * Foundation, Inc.
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050018 */
19
20#include <string.h>
Aaron Durbin4d3de7e2015-09-02 17:34:04 -050021#include <bootstate.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050022#include <console/console.h>
23#include <cbmem.h>
Aaron Durbin0424c952015-03-28 23:56:22 -050024#include <fmap.h>
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050025#include <ip_checksum.h>
Duncan Lauried8c4f2b2014-04-22 10:46:06 -070026#include "mrc_cache.h"
Aaron Durbin4d3de7e2015-09-02 17:34:04 -050027#include "nvm.h"
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050028
29#define MRC_DATA_ALIGN 0x1000
30#define MRC_DATA_SIGNATURE (('M'<<0)|('R'<<8)|('C'<<16)|('D'<<24))
31
32/* The mrc_data_region describes the larger non-volatile area to store
33 * mrc_saved_data objects.*/
34struct mrc_data_region {
35 void *base;
36 uint32_t size;
37};
38
39/* common code */
40static int mrc_cache_get_region(struct mrc_data_region *region)
41{
Aaron Durbin0424c952015-03-28 23:56:22 -050042 if (IS_ENABLED(CONFIG_CHROMEOS)) {
43 struct region_device rdev;
44
45 if (fmap_locate_area_as_rdev("RW_MRC_CACHE", &rdev))
46 return -1;
47
48 region->size = region_device_sz(&rdev);
49 region->base = rdev_mmap_full(&rdev);
50
51 if (region->base == NULL)
52 return -1;
53
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050054 return 0;
Aaron Durbin0424c952015-03-28 23:56:22 -050055 } else {
56 region->base = (void *)CONFIG_MRC_SETTINGS_CACHE_BASE;
57 region->size = CONFIG_MRC_SETTINGS_CACHE_SIZE;
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -050058 }
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
100 if (cache->reserved != 0)
101 return 0;
102
103 checksum = compute_ip_checksum((void *)&cache->data[0], cache->size);
104
105 if (cache->checksum != checksum)
106 return 0;
107
108 return 1;
109}
110
111static const struct mrc_saved_data *
112next_cache_block(const struct mrc_saved_data *cache)
113{
114 uintptr_t next = (uintptr_t)cache;
115
116 next += ALIGN(cache->size + sizeof(*cache), MRC_DATA_ALIGN);
117
118 return (const struct mrc_saved_data *)next;
119}
120
121/* Locate the most recently saved MRC data. */
122static int __mrc_cache_get_current(const struct mrc_data_region *region,
123 const struct mrc_saved_data **cache)
124{
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
140 if (verified_cache == NULL)
141 return -1;
142
143 *cache = verified_cache;
Duncan Lauried8c4f2b2014-04-22 10:46:06 -0700144 printk(BIOS_DEBUG, "MRC cache slot %d @ %p\n", slot-1, verified_cache);
145
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500146 return 0;
147}
148
149int mrc_cache_get_current(const struct mrc_saved_data **cache)
150{
151 struct mrc_data_region region;
152
153 if (mrc_cache_get_region(&region) < 0)
154 return -1;
155
156 return __mrc_cache_get_current(&region, cache);
157}
158
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500159/* Fill in mrc_saved_data structure with payload. */
160static void mrc_cache_fill(struct mrc_saved_data *cache, void *data,
161 size_t size)
162{
163 cache->signature = MRC_DATA_SIGNATURE;
164 cache->size = size;
165 cache->reserved = 0;
166 memcpy(&cache->data[0], data, size);
167 cache->checksum = compute_ip_checksum((void *)&cache->data[0],
168 cache->size);
169}
170
171int mrc_cache_stash_data(void *data, size_t size)
172{
173 int cbmem_size;
174 struct mrc_saved_data *cache;
175
176 cbmem_size = sizeof(*cache) + ALIGN(size, 16);
177
178 cache = cbmem_add(CBMEM_ID_MRCDATA, cbmem_size);
179
180 if (cache == NULL) {
181 printk(BIOS_ERR, "No space in cbmem for MRC data.\n");
182 return -1;
183 }
184
Aaron Durbin4177db52014-02-05 14:55:26 -0600185 /* Clear alignment padding bytes at end of data. */
186 memset(&cache->data[size], 0, cbmem_size - size - sizeof(*cache));
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500187
188 printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%zu bytes)\n",
189 data, cache, size);
190
191 mrc_cache_fill(cache, data, size);
192
193 return 0;
194}
195
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500196static int mrc_slot_valid(const struct mrc_data_region *region,
197 const struct mrc_saved_data *slot,
198 const struct mrc_saved_data *to_save)
199{
200 uintptr_t region_begin;
201 uintptr_t region_end;
202 uintptr_t slot_end;
203 uintptr_t slot_begin;
204 uint32_t size;
205
206 region_begin = (uintptr_t)region->base;
207 region_end = region_begin + region->size;
208 slot_begin = (uintptr_t)slot;
209 size = to_save->size + sizeof(*to_save);
210 slot_end = slot_begin + size;
211
Aaron Durbin4177db52014-02-05 14:55:26 -0600212 if (slot_begin < region_begin || slot_begin >= region_end)
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500213 return 0;
214
215 if (size > region->size)
216 return 0;
217
218 if (slot_end > region_end || slot_end < region_begin)
219 return 0;
220
221 if (!nvm_is_erased(slot, size))
222 return 0;
223
224 return 1;
225}
226
227static const struct mrc_saved_data *
228mrc_cache_next_slot(const struct mrc_data_region *region,
229 const struct mrc_saved_data *current_slot)
230{
231 const struct mrc_saved_data *next_slot;
232
233 if (current_slot == NULL) {
234 next_slot = region->base;
235 } else {
236 next_slot = next_cache_block(current_slot);
237 }
238
239 return next_slot;
240}
241
Duncan Lauriea32b6b92015-01-15 15:49:07 -0800242/* Protect RW_MRC_CACHE region with a Protected Range Register */
243static int protect_mrc_cache(const struct mrc_data_region *region)
244{
245#if IS_ENABLED(CONFIG_MRC_SETTINGS_PROTECT)
246 if (nvm_is_write_protected() <= 0) {
247 printk(BIOS_INFO, "NOT enabling PRR for RW_MRC_CACHE region\n");
248 return 1;
249 }
250
251 if (nvm_protect(region->base, region->size) < 0) {
252 printk(BIOS_ERR, "ERROR setting PRR for RW_MRC_CACHE region\n");
253 return -1;
254 }
255
256 printk(BIOS_INFO, "Enabled Protected Range on RW_MRC_CACHE region\n");
257#endif
258 return 0;
259}
260
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500261static void update_mrc_cache(void *unused)
262{
263 const struct mrc_saved_data *current_boot;
264 const struct mrc_saved_data *current_saved;
265 const struct mrc_saved_data *next_slot;
266 struct mrc_data_region region;
267
268 printk(BIOS_DEBUG, "Updating MRC cache data.\n");
269
270 current_boot = cbmem_find(CBMEM_ID_MRCDATA);
271 if (!current_boot) {
272 printk(BIOS_ERR, "No MRC cache in cbmem.\n");
273 return;
274 }
275
276 if (mrc_cache_get_region(&region)) {
277 printk(BIOS_ERR, "Could not obtain MRC cache region.\n");
278 return;
279 }
280
281 if (!mrc_cache_valid(&region, current_boot)) {
282 printk(BIOS_ERR, "MRC cache data in cbmem invalid.\n");
283 return;
284 }
285
286 current_saved = NULL;
287
288 if (!__mrc_cache_get_current(&region, &current_saved)) {
289 if (current_saved->size == current_boot->size &&
290 !memcmp(&current_saved->data[0], &current_boot->data[0],
291 current_saved->size)) {
292 printk(BIOS_DEBUG, "MRC cache up to date.\n");
Duncan Lauriea32b6b92015-01-15 15:49:07 -0800293 protect_mrc_cache(&region);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500294 return;
295 }
296 }
297
298 next_slot = mrc_cache_next_slot(&region, current_saved);
299
300 if (!mrc_slot_valid(&region, next_slot, current_boot)) {
Aaron Durbin931e5902014-01-13 11:34:51 -0600301 printk(BIOS_DEBUG, "Slot @ %p is invalid.\n", next_slot);
302 if (!nvm_is_erased(region.base, region.size)) {
303 if (nvm_erase(region.base, region.size) < 0) {
304 printk(BIOS_DEBUG, "Failure erasing region.\n");
305 return;
306 }
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500307 }
308 next_slot = region.base;
309 }
310
Aaron Durbin4177db52014-02-05 14:55:26 -0600311 if (nvm_write((void *)next_slot, current_boot,
312 current_boot->size + sizeof(*current_boot))) {
313 printk(BIOS_DEBUG, "Failure writing MRC cache to %p.\n",
314 next_slot);
315 }
Duncan Lauriea32b6b92015-01-15 15:49:07 -0800316 protect_mrc_cache(&region);
Aaron Durbin9a7d7bc2013-09-07 00:41:48 -0500317}
318
Aaron Durbin9ef9d852015-03-16 17:30:09 -0500319BOOT_STATE_INIT_ENTRY(BS_WRITE_TABLES, BS_ON_ENTRY, update_mrc_cache, NULL);