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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/*
* This file is part of the coreboot project.
*
* Copyright (C) 2014 Google Inc.
*
* 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.
*/
#include <string.h>
#include <bootstate.h>
#include <console/console.h>
#include <cbmem.h>
#include <elog.h>
#include <fmap.h>
#include <ip_checksum.h>
#include <vboot/vboot_common.h>
#include "mrc_cache.h"
#include "nvm.h"
#define MRC_DATA_ALIGN 0x1000
#define MRC_DATA_SIGNATURE (('M'<<0)|('R'<<8)|('C'<<16)|('D'<<24))
/* The mrc_data_region describes the larger non-volatile area to store
* mrc_saved_data objects.*/
struct mrc_data_region {
void *base;
uint32_t size;
};
/* common code */
static int mrc_cache_get_region(struct mrc_data_region *region)
{
bool located_by_fmap = true;
struct region_device rdev;
if (fmap_locate_area_as_rdev("RW_MRC_CACHE", &rdev))
located_by_fmap = false;
/* CHROMEOS builds must get their MRC cache from FMAP. */
if (IS_ENABLED(CONFIG_CHROMEOS) && !located_by_fmap)
return -1;
if (located_by_fmap) {
region->size = region_device_sz(&rdev);
region->base = rdev_mmap_full(&rdev);
if (region->base == NULL)
return -1;
} else {
region->base = (void *)CONFIG_MRC_SETTINGS_CACHE_BASE;
region->size = CONFIG_MRC_SETTINGS_CACHE_SIZE;
}
return 0;
}
static int mrc_cache_in_region(const struct mrc_data_region *region,
const struct mrc_saved_data *cache)
{
uintptr_t region_end;
uintptr_t cache_end;
if ((uintptr_t)cache < (uintptr_t)region->base)
return 0;
region_end = (uintptr_t)region->base;
region_end += region->size;
if ((uintptr_t)cache >= region_end)
return 0;
if ((sizeof(*cache) + (uintptr_t)cache) >= region_end)
return 0;
cache_end = (uintptr_t)cache;
cache_end += cache->size + sizeof(*cache);
if (cache_end > region_end)
return 0;
return 1;
}
static int mrc_cache_valid(const struct mrc_data_region *region,
const struct mrc_saved_data *cache)
{
uint32_t checksum;
if (cache->signature != MRC_DATA_SIGNATURE)
return 0;
if (cache->size > region->size)
return 0;
checksum = compute_ip_checksum((void *)&cache->data[0], cache->size);
if (cache->checksum != checksum)
return 0;
return 1;
}
static const struct mrc_saved_data *
next_cache_block(const struct mrc_saved_data *cache)
{
uintptr_t next = (uintptr_t)cache;
next += ALIGN(cache->size + sizeof(*cache), MRC_DATA_ALIGN);
return (const struct mrc_saved_data *)next;
}
/* Locate the most recently saved MRC data. */
static int __mrc_cache_get_current(const struct mrc_data_region *region,
const struct mrc_saved_data **cache,
uint32_t version)
{
const struct mrc_saved_data *msd;
const struct mrc_saved_data *verified_cache;
int slot = 0;
msd = region->base;
verified_cache = NULL;
while (mrc_cache_in_region(region, msd) &&
mrc_cache_valid(region, msd)) {
verified_cache = msd;
msd = next_cache_block(msd);
slot++;
}
/*
* Update pointer to the most recently saved MRC data before returning
* any error. This ensures that the caller can use next available slot
* if required.
*/
*cache = verified_cache;
if (verified_cache == NULL)
return -1;
if (verified_cache->version != version) {
printk(BIOS_DEBUG, "MRC cache version mismatch: %x vs %x\n",
verified_cache->version, version);
return -1;
}
printk(BIOS_DEBUG, "MRC cache slot %d @ %p\n", slot-1, verified_cache);
return 0;
}
int mrc_cache_get_current_with_version(const struct mrc_saved_data **cache,
uint32_t version)
{
struct mrc_data_region region;
if (mrc_cache_get_region(®ion) < 0)
return -1;
return __mrc_cache_get_current(®ion, cache, version);
}
int mrc_cache_get_current(const struct mrc_saved_data **cache)
{
return mrc_cache_get_current_with_version(cache, 0);
}
/* Fill in mrc_saved_data structure with payload. */
static void mrc_cache_fill(struct mrc_saved_data *cache, const void *data,
size_t size, uint32_t version)
{
cache->signature = MRC_DATA_SIGNATURE;
cache->size = size;
cache->version = version;
memcpy(&cache->data[0], data, size);
cache->checksum = compute_ip_checksum((void *)&cache->data[0],
cache->size);
}
int mrc_cache_stash_data_with_version(const void *data, size_t size,
uint32_t version)
{
int cbmem_size;
struct mrc_saved_data *cache;
cbmem_size = sizeof(*cache) + ALIGN(size, 16);
cache = cbmem_add(CBMEM_ID_MRCDATA, cbmem_size);
if (cache == NULL) {
printk(BIOS_ERR, "No space in cbmem for MRC data.\n");
return -1;
}
/* Clear alignment padding bytes at end of data. */
memset(&cache->data[size], 0, cbmem_size - size - sizeof(*cache));
printk(BIOS_DEBUG, "Relocate MRC DATA from %p to %p (%zu bytes)\n",
data, cache, size);
mrc_cache_fill(cache, data, size, version);
return 0;
}
int mrc_cache_stash_data(const void *data, size_t size)
{
return mrc_cache_stash_data_with_version(data, size, 0);
}
static int mrc_slot_valid(const struct mrc_data_region *region,
const struct mrc_saved_data *slot,
const struct mrc_saved_data *to_save)
{
uintptr_t region_begin;
uintptr_t region_end;
uintptr_t slot_end;
uintptr_t slot_begin;
uint32_t size;
region_begin = (uintptr_t)region->base;
region_end = region_begin + region->size;
slot_begin = (uintptr_t)slot;
size = to_save->size + sizeof(*to_save);
slot_end = slot_begin + size;
if (slot_begin < region_begin || slot_begin >= region_end)
return 0;
if (size > region->size)
return 0;
if (slot_end > region_end || slot_end < region_begin)
return 0;
if (!nvm_is_erased(slot, size))
return 0;
return 1;
}
static const struct mrc_saved_data *
mrc_cache_next_slot(const struct mrc_data_region *region,
const struct mrc_saved_data *current_slot)
{
const struct mrc_saved_data *next_slot;
if (current_slot == NULL) {
next_slot = region->base;
} else {
next_slot = next_cache_block(current_slot);
}
return next_slot;
}
/* Protect RW_MRC_CACHE region with a Protected Range Register */
static int protect_mrc_cache(const struct mrc_data_region *region)
{
#if IS_ENABLED(CONFIG_MRC_SETTINGS_PROTECT)
if (nvm_is_write_protected() <= 0) {
printk(BIOS_INFO, "NOT enabling PRR for RW_MRC_CACHE region\n");
return 1;
}
if (nvm_protect(region->base, region->size) < 0) {
printk(BIOS_ERR, "ERROR setting PRR for RW_MRC_CACHE region\n");
return -1;
}
printk(BIOS_INFO, "Enabled Protected Range on RW_MRC_CACHE region\n");
#endif
return 0;
}
static void log_event_cache_update(uint8_t slot, uint8_t status)
{
const int type = ELOG_TYPE_MEM_CACHE_UPDATE;
struct elog_event_mem_cache_update event = {
.slot = slot,
.status = status,
};
if (elog_add_event_raw(type, &event, sizeof(event)) < 0)
printk(BIOS_ERR, "Failed to log mem cache update event.\n");
}
static void update_mrc_cache(void *unused)
{
const struct mrc_saved_data *current_boot;
const struct mrc_saved_data *current_saved;
const struct mrc_saved_data *next_slot;
struct mrc_data_region region;
printk(BIOS_DEBUG, "Updating MRC cache data.\n");
current_boot = cbmem_find(CBMEM_ID_MRCDATA);
if (!current_boot) {
printk(BIOS_ERR, "No MRC cache in cbmem.\n");
return;
}
if (mrc_cache_get_region(®ion)) {
printk(BIOS_ERR, "Could not obtain MRC cache region.\n");
return;
}
if (!mrc_cache_valid(®ion, current_boot)) {
printk(BIOS_ERR, "MRC cache data in cbmem invalid.\n");
return;
}
current_saved = NULL;
if (!__mrc_cache_get_current(®ion, ¤t_saved,
current_boot->version)) {
if (current_saved->size == current_boot->size &&
!memcmp(¤t_saved->data[0], ¤t_boot->data[0],
current_saved->size)) {
printk(BIOS_DEBUG, "MRC cache up to date.\n");
protect_mrc_cache(®ion);
return;
}
}
next_slot = mrc_cache_next_slot(®ion, current_saved);
if (!mrc_slot_valid(®ion, next_slot, current_boot)) {
printk(BIOS_DEBUG, "Slot @ %p is invalid.\n", next_slot);
if (!nvm_is_erased(region.base, region.size)) {
if (nvm_erase(region.base, region.size) < 0) {
printk(BIOS_DEBUG, "Failure erasing region.\n");
return;
}
}
next_slot = region.base;
}
if (nvm_write((void *)next_slot, current_boot,
current_boot->size + sizeof(*current_boot))) {
printk(BIOS_DEBUG, "Failure writing MRC cache to %p.\n",
next_slot);
log_event_cache_update(ELOG_MEM_CACHE_UPDATE_SLOT_NORMAL,
ELOG_MEM_CACHE_UPDATE_STATUS_FAIL);
} else {
log_event_cache_update(ELOG_MEM_CACHE_UPDATE_SLOT_NORMAL,
ELOG_MEM_CACHE_UPDATE_STATUS_SUCCESS);
}
protect_mrc_cache(®ion);
}
BOOT_STATE_INIT_ENTRY(BS_WRITE_TABLES, BS_ON_ENTRY, update_mrc_cache, NULL);
|