Debian Codegraph @

src/drivers/mrc_cache/mrc_cache.c

raw symbols SCIP
1 /* SPDX-License-Identifier: GPL-2.0-only */
2
3 #include <string.h>
4 #include <boot_device.h>
5 #include <bootstate.h>
6 #include <bootmode.h>
7 #include <console/console.h>
8 #include <cbmem.h>
9 #include <elog.h>
10 #include <fmap.h>
11 #include <region_file.h>
12 #include <security/tpm/tspi.h>
13 #include <security/vboot/antirollback.h>
14 #include <security/vboot/mrc_cache_hash_tpm.h>
15 #include <security/vboot/vboot_common.h>
16 #include <spi_flash.h>
17 #include <xxhash.h>
18
19 #include "mrc_cache.h"
20
21 #define DEFAULT_MRC_CACHE "RW_MRC_CACHE"
22 #define VARIABLE_MRC_CACHE "RW_VAR_MRC_CACHE"
23 #define RECOVERY_MRC_CACHE "RECOVERY_MRC_CACHE"
24 #define UNIFIED_MRC_CACHE "UNIFIED_MRC_CACHE"
25
26 /* Signature "MRCD" was used for older header format before CB:67670. */
27 #define MRC_DATA_SIGNATURE (('M'<<0)|('R'<<8)|('C'<<16)|('d'<<24))
28
29 static const uint32_t mrc_invalid_sig = ~MRC_DATA_SIGNATURE;
30
31 struct mrc_metadata {
32 uint32_t signature;
33 uint32_t data_size;
34 uint32_t data_hash;
35 uint32_t header_hash;
36 uint32_t version;
37 } __packed;
38
39 enum result {
40 UPDATE_FAILURE = -1,
41 UPDATE_SUCCESS = 0,
42 ALREADY_UPTODATE = 1
43 };
44
45 #define NORMAL_FLAG (1 << 0)
46 #define RECOVERY_FLAG (1 << 1)
47
48 struct cache_region {
49 const char *name;
50 uint32_t cbmem_id;
51 int type;
52 int elog_slot;
53 uint32_t tpm_hash_index;
54 int flags;
55 };
56
57 static const struct cache_region recovery_training = {
58 .name = RECOVERY_MRC_CACHE,
59 .cbmem_id = CBMEM_ID_MRCDATA,
60 .type = MRC_TRAINING_DATA,
61 .elog_slot = ELOG_MEM_CACHE_UPDATE_SLOT_RECOVERY,
62 .tpm_hash_index = MRC_REC_HASH_NV_INDEX,
63 #if CONFIG(HAS_RECOVERY_MRC_CACHE)
64 .flags = RECOVERY_FLAG,
65 #else
66 .flags = 0,
67 #endif
68 };
69
70 static const struct cache_region normal_training = {
71 .name = DEFAULT_MRC_CACHE,
72 .cbmem_id = CBMEM_ID_MRCDATA,
73 .type = MRC_TRAINING_DATA,
74 .elog_slot = ELOG_MEM_CACHE_UPDATE_SLOT_NORMAL,
75 .tpm_hash_index = MRC_RW_HASH_NV_INDEX,
76 #if CONFIG(VBOOT_STARTS_IN_ROMSTAGE)
77 /*
78 * If VBOOT_STARTS_IN_ROMSTAGE is selected, this means that
79 * memory training happens before vboot (in RO) and the
80 * mrc_cache data is always safe to use.
81 */
82 .flags = NORMAL_FLAG | RECOVERY_FLAG,
83 #else
84 /*
85 * If !VBOOT_STARTS_IN_ROMSTAGE, this means that memory training happens after
86 * vboot (in RW code) and is never safe to use in recovery.
87 */
88 .flags = NORMAL_FLAG,
89 #endif
90 };
91
92 static const struct cache_region variable_data = {
93 .name = VARIABLE_MRC_CACHE,
94 .cbmem_id = CBMEM_ID_VAR_MRCDATA,
95 .type = MRC_VARIABLE_DATA,
96 .elog_slot = ELOG_MEM_CACHE_UPDATE_SLOT_VARIABLE,
97 .tpm_hash_index = 0,
98 #if CONFIG(VBOOT_STARTS_IN_ROMSTAGE)
99 /*
100 * If VBOOT_STARTS_IN_ROMSTAGE is selected, this means that
101 * memory training happens before vboot (in RO) and the
102 * mrc_cache data is always safe to use.
103 */
104 .flags = NORMAL_FLAG | RECOVERY_FLAG,
105 #else
106 /*
107 * If !VBOOT_STARTS_IN_ROMSTAGE, this means that memory training happens after
108 * vboot (in RW code) and is never safe to use in recovery.
109 */
110 .flags = NORMAL_FLAG,
111 #endif
112 };
113
114 /* Order matters here for priority in matching. */
115 static const struct cache_region *cache_regions[] = {
116 &recovery_training,
117 &normal_training,
118 &variable_data,
119 };
120
121 /* TPM MRC hash functionality depends on vboot starting before memory init. */
122 _Static_assert(!CONFIG(MRC_SAVE_HASH_IN_TPM) ||
123 CONFIG(VBOOT_STARTS_IN_BOOTBLOCK),
124 "for TPM MRC hash functionality, vboot must start in bootblock");
125
126 static int lookup_region_by_name(const char *name, struct region *r)
127 {
128 if (fmap_locate_area(name, r) == 0)
129 return 0;
130 return -1;
131 }
132
133 static const struct cache_region *lookup_region_type(int type)
134 {
135 int i;
136 int flags;
137
138 if (CONFIG(VBOOT_STARTS_IN_BOOTBLOCK) && vboot_recovery_mode_enabled())
139 flags = RECOVERY_FLAG;
140 else
141 flags = NORMAL_FLAG;
142
143 for (i = 0; i < ARRAY_SIZE(cache_regions); i++) {
144 if (cache_regions[i]->type != type)
145 continue;
146 if ((cache_regions[i]->flags & flags) == flags)
147 return cache_regions[i];
148 }
149
150 return NULL;
151 }
152
153 static const struct cache_region *lookup_region(struct region *r, int type)
154 {
155 const struct cache_region *cr;
156
157 cr = lookup_region_type(type);
158
159 if (cr == NULL) {
160 /* There will be no recovery MRC cache region if (!HAS_RECOVERY_MRC_CACHE &&
161 !VBOOT_STARTS_IN_ROMSTAGE). */
162 printk(BIOS_DEBUG, "MRC: failed to locate region type %d\n", type);
163 return NULL;
164 }
165
166 if (lookup_region_by_name(cr->name, r) < 0)
167 return NULL;
168
169 return cr;
170 }
171
172 static int mrc_header_valid(struct region_device *rdev, struct mrc_metadata *md)
173 {
174 uint32_t hash;
175 uint32_t hash_result;
176 size_t size;
177
178 if (rdev_readat(rdev, md, 0, sizeof(*md)) < 0) {
179 /* When the metadata was invalidated intentionally (for example from the
180 previous recovery boot), print a warning instead of an error. */
181 if (rdev_readat(rdev, md, 0, sizeof(mrc_invalid_sig)) >= 0 &&
182 md->signature == mrc_invalid_sig) {
183 printk(BIOS_INFO, "MRC: metadata was invalidated\n");
184 return -1;
185 }
186
187 printk(BIOS_ERR, "MRC: couldn't read metadata\n");
188 return -1;
189 }
190
191 if (md->signature != MRC_DATA_SIGNATURE) {
192 printk(BIOS_ERR, "MRC: invalid header signature\n");
193 return -1;
194 }
195
196 /* Compute hash over header with 0 as the value. */
197 hash = md->header_hash;
198 md->header_hash = 0;
199 hash_result = xxh32(md, sizeof(*md), 0);
200
201 if (hash != hash_result) {
202 printk(BIOS_ERR, "MRC: header hash mismatch: %x vs %x\n",
203 hash, hash_result);
204 return -1;
205 }
206
207 /* Put back original. */
208 md->header_hash = hash;
209
210 /* Re-size the region device according to the metadata as a region_file
211 * does block allocation. */
212 size = sizeof(*md) + md->data_size;
213 if (rdev_chain(rdev, rdev, 0, size) < 0) {
214 printk(BIOS_ERR, "MRC: size exceeds rdev size: %zx vs %zx\n",
215 size, region_device_sz(rdev));
216 return -1;
217 }
218
219 return 0;
220 }
221
222 static int mrc_data_valid(int type, const struct mrc_metadata *md,
223 void *data, size_t data_size)
224 {
225 uint32_t hash;
226 const struct cache_region *cr = lookup_region_type(type);
227 uint32_t hash_idx;
228
229 if (cr == NULL)
230 return -1;
231
232 if (md->data_size != data_size)
233 return -1;
234
235 hash_idx = cr->tpm_hash_index;
236 if (hash_idx && CONFIG(MRC_SAVE_HASH_IN_TPM)) {
237 if (!mrc_cache_verify_hash(hash_idx, data, data_size))
238 return -1;
239 } else {
240 hash = xxh32(data, data_size, 0);
241
242 if (md->data_hash != hash) {
243 printk(BIOS_ERR, "MRC: data hash mismatch: %x vs %x\n",
244 md->data_hash, hash);
245 return -1;
246 }
247 }
248
249 return 0;
250 }
251
252 static tpm_result_t mrc_measure_cache(int type, struct region_device *cache_region)
253 {
254 tpm_result_t rc = TPM_SUCCESS;
255 char rname[TPM_CB_LOG_PCR_HASH_NAME];
256 snprintf(rname, sizeof(rname), "MRC: %s cache",
257 (type == MRC_VARIABLE_DATA) ? "variable" : "training");
258 rc = tpm_measure_region(cache_region, CONFIG_PCR_RUNTIME_DATA, rname);
259 if (rc)
260 printk(BIOS_ERR, "MRC: Couldn't measure %s! rc %#x\n", rname, rc);
261
262 return rc;
263 }
264
265 static int mrc_cache_get_latest_slot_info(const char *name,
266 const struct region_device *backing_rdev,
267 struct mrc_metadata *md,
268 struct region_file *cache_file,
269 struct region_device *rdev,
270 bool fail_bad_data)
271 {
272 /* Init and obtain a handle to the file data. */
273 if (region_file_init(cache_file, backing_rdev) < 0) {
274 printk(BIOS_ERR, "MRC: region file invalid in '%s'\n", name);
275 return -1;
276 }
277
278 /* Provide a 0 sized region_device from here on out so the caller
279 * has a valid yet unusable region_device. */
280 rdev_chain(rdev, backing_rdev, 0, 0);
281
282 /* No data to return. */
283 if (region_file_data(cache_file, rdev) < 0) {
284 printk(BIOS_NOTICE, "MRC: no data in '%s'\n", name);
285 return fail_bad_data ? -1 : 0;
286 }
287
288 /* Validate header and resize region to reflect actual usage on the
289 * saved medium (including metadata and data). */
290 if (mrc_header_valid(rdev, md) < 0)
291 return fail_bad_data ? -1 : 0;
292
293 return 0;
294 }
295
296 static int mrc_cache_find_current(int type, uint32_t version,
297 struct region_device *rdev,
298 struct mrc_metadata *md)
299 {
300 const struct cache_region *cr;
301 struct region region;
302 struct region_device read_rdev;
303 struct region_file cache_file;
304 size_t data_size;
305 const size_t md_size = sizeof(*md);
306 const bool fail_bad_data = true;
307
308 /*
309 * In recovery mode, force retraining if the memory retrain
310 * switch is set.
311 */
312 if (CONFIG(VBOOT_STARTS_IN_BOOTBLOCK) && vboot_recovery_mode_enabled()
313 && get_recovery_mode_retrain_switch())
314 return -1;
315
316 cr = lookup_region(&region, type);
317
318 if (cr == NULL)
319 return -1;
320
321 if (boot_device_ro_subregion(&region, &read_rdev) < 0)
322 return -1;
323
324 if (mrc_cache_get_latest_slot_info(cr->name,
325 &read_rdev,
326 md,
327 &cache_file,
328 rdev,
329 fail_bad_data) < 0)
330 return -1;
331
332 if (version != md->version) {
333 printk(BIOS_INFO, "MRC: version mismatch: %x vs %x\n",
334 md->version, version);
335 return -1;
336 }
337
338 /* Re-size rdev to only contain the data. i.e. remove metadata. */
339 data_size = md->data_size;
340 return rdev_chain(rdev, rdev, md_size, data_size);
341 }
342
343 ssize_t mrc_cache_load_current(int type, uint32_t version, void *buffer,
344 size_t buffer_size)
345 {
346 struct region_device rdev;
347 struct mrc_metadata md;
348 ssize_t data_size;
349
350 if (mrc_cache_find_current(type, version, &rdev, &md) < 0)
351 return -1;
352
353 data_size = region_device_sz(&rdev);
354 if (buffer_size < data_size)
355 return -1;
356
357 if (rdev_readat(&rdev, buffer, 0, data_size) != data_size)
358 return -1;
359
360 if (mrc_data_valid(type, &md, buffer, data_size) < 0)
361 return -1;
362
363 if (CONFIG(TPM_MEASURE_MRC_CACHE))
364 mrc_measure_cache(type, &rdev);
365
366 return data_size;
367 }
368
369 void *mrc_cache_current_mmap_leak(int type, uint32_t version,
370 size_t *data_size)
371 {
372 struct region_device rdev;
373 void *data;
374 size_t region_device_size;
375 struct mrc_metadata md;
376
377 if (mrc_cache_find_current(type, version, &rdev, &md) < 0)
378 return NULL;
379
380 region_device_size = region_device_sz(&rdev);
381 if (data_size)
382 *data_size = region_device_size;
383 data = rdev_mmap_full(&rdev);
384
385 if (data == NULL) {
386 printk(BIOS_INFO, "MRC: mmap failure.\n");
387 return NULL;
388 }
389
390 if (mrc_data_valid(type, &md, data, region_device_size) < 0)
391 return NULL;
392
393 if (CONFIG(TPM_MEASURE_MRC_CACHE))
394 mrc_measure_cache(type, &rdev);
395
396 return data;
397 }
398
399 static bool mrc_cache_needs_update(const struct region_device *rdev,
400 const struct mrc_metadata *new_md,
401 size_t new_data_size)
402 {
403 void *mapping;
404 size_t old_data_size = region_device_sz(rdev) - sizeof(struct mrc_metadata);
405 bool need_update = false;
406
407 if (new_data_size != old_data_size)
408 return true;
409
410 mapping = rdev_mmap_full(rdev);
411 if (mapping == NULL) {
412 printk(BIOS_ERR, "MRC: cannot mmap existing cache.\n");
413 return true;
414 }
415
416 /*
417 * Compare the old and new metadata only. If the data hashes don't
418 * match, the comparison will fail.
419 */
420 if (memcmp(new_md, mapping, sizeof(struct mrc_metadata)))
421 need_update = true;
422
423 rdev_munmap(rdev, mapping);
424
425 return need_update;
426 }
427
428 static void log_event_cache_update(uint8_t slot, enum result res)
429 {
430 const int type = ELOG_TYPE_MEM_CACHE_UPDATE;
431 struct elog_event_mem_cache_update event = {
432 .slot = slot
433 };
434
435 /* Filter through interesting events only */
436 switch (res) {
437 case UPDATE_FAILURE:
438 event.status = ELOG_MEM_CACHE_UPDATE_STATUS_FAIL;
439 break;
440 case UPDATE_SUCCESS:
441 event.status = ELOG_MEM_CACHE_UPDATE_STATUS_SUCCESS;
442 break;
443 default:
444 return;
445 }
446
447 if (elog_add_event_raw(type, &event, sizeof(event)) < 0)
448 printk(BIOS_ERR, "Failed to log mem cache update event.\n");
449 }
450
451 /* During ramstage this code purposefully uses incoherent transactions between
452 * read and write. The read assumes a memory-mapped boot device that can be used
453 * to quickly locate and compare the up-to-date data. However, when an update
454 * is required it uses the writable region access to perform the update. */
455 static void update_mrc_cache_by_type(int type,
456 struct mrc_metadata *new_md,
457 const void *new_data,
458 size_t new_data_size)
459 {
460 const struct cache_region *cr;
461 struct region region;
462 struct region_device read_rdev;
463 struct region_device write_rdev;
464 struct region_file cache_file;
465 struct mrc_metadata md;
466 struct incoherent_rdev backing_irdev;
467 const struct region_device *backing_rdev;
468 struct region_device latest_rdev;
469 const bool fail_bad_data = false;
470 uint32_t hash_idx;
471
472 cr = lookup_region(&region, type);
473
474 if (cr == NULL)
475 return;
476
477 printk(BIOS_DEBUG, "MRC: Checking cached data update for '%s'.\n",
478 cr->name);
479
480 if (boot_device_ro_subregion(&region, &read_rdev) < 0)
481 return;
482
483 if (boot_device_rw_subregion(&region, &write_rdev) < 0)
484 return;
485
486 backing_rdev = incoherent_rdev_init(&backing_irdev, &region, &read_rdev,
487 &write_rdev);
488
489 if (backing_rdev == NULL)
490 return;
491
492 /* Note that mrc_cache_get_latest_slot_info doesn't check the
493 * validity of the current slot. If the slot is invalid,
494 * we'll overwrite it anyway when we update the mrc_cache.
495 */
496 if (mrc_cache_get_latest_slot_info(cr->name,
497 backing_rdev,
498 &md,
499 &cache_file,
500 &latest_rdev,
501 fail_bad_data) < 0)
502
503 return;
504
505 if (!mrc_cache_needs_update(&latest_rdev, new_md, new_data_size)) {
506 printk(BIOS_DEBUG, "MRC: '%s' does not need update.\n", cr->name);
507 log_event_cache_update(cr->elog_slot, ALREADY_UPTODATE);
508 return;
509 }
510
511 printk(BIOS_DEBUG, "MRC: cache data '%s' needs update.\n", cr->name);
512
513 struct update_region_file_entry entries[] = {
514 [0] = {
515 .size = sizeof(*new_md),
516 .data = new_md,
517 },
518 [1] = {
519 .size = new_data_size,
520 .data = new_data,
521 },
522 };
523 if (region_file_update_data_arr(&cache_file, entries, ARRAY_SIZE(entries)) < 0) {
524 printk(BIOS_ERR, "MRC: failed to update '%s'.\n", cr->name);
525 log_event_cache_update(cr->elog_slot, UPDATE_FAILURE);
526 } else {
527 printk(BIOS_DEBUG, "MRC: updated '%s'.\n", cr->name);
528 log_event_cache_update(cr->elog_slot, UPDATE_SUCCESS);
529 hash_idx = cr->tpm_hash_index;
530 if (hash_idx && CONFIG(MRC_SAVE_HASH_IN_TPM))
531 mrc_cache_update_hash(hash_idx, new_data, new_data_size);
532 if (CONFIG(TPM_MEASURE_MRC_CACHE) &&
533 !rdev_chain_mem(&read_rdev, new_data, new_data_size))
534 mrc_measure_cache(type, &read_rdev);
535 }
536 }
537
538 /* Read flash status register to determine if write protect is active */
539 static int nvm_is_write_protected(void)
540 {
541 u8 sr1;
542 u8 wp_gpio;
543 u8 wp_spi;
544
545 if (!CONFIG(CHROMEOS))
546 return 0;
547
548 if (!CONFIG(BOOT_DEVICE_SPI_FLASH))
549 return 0;
550
551 /* Read Write Protect GPIO if available */
552 wp_gpio = get_write_protect_state();
553
554 /* Read Status Register 1 */
555 if (spi_flash_status(boot_device_spi_flash(), &sr1) < 0) {
556 printk(BIOS_ERR, "Failed to read SPI status register 1\n");
557 return -1;
558 }
559 wp_spi = !!(sr1 & 0x80);
560
561 printk(BIOS_DEBUG, "SPI flash protection: WPSW=%d SRP0=%d\n",
562 wp_gpio, wp_spi);
563
564 return wp_gpio && wp_spi;
565 }
566
567 /* Apply protection to a range of flash */
568 static int nvm_protect(const struct region *r)
569 {
570 if (!CONFIG(MRC_SETTINGS_PROTECT))
571 return 0;
572
573 if (!CONFIG(BOOT_DEVICE_SPI_FLASH))
574 return 0;
575
576 return spi_flash_ctrlr_protect_region(boot_device_spi_flash(), r, WRITE_PROTECT);
577 }
578
579 /* Protect mrc region with a Protected Range Register */
580 static int protect_mrc_cache(const char *name)
581 {
582 struct region region;
583
584 if (!CONFIG(MRC_SETTINGS_PROTECT))
585 return 0;
586
587 if (lookup_region_by_name(name, &region) < 0) {
588 printk(BIOS_INFO, "MRC: Could not find region '%s'\n", name);
589 return -1;
590 }
591
592 if (nvm_is_write_protected() <= 0) {
593 printk(BIOS_INFO, "MRC: NOT enabling PRR for '%s'.\n", name);
594 return 0;
595 }
596
597 if (nvm_protect(&region) < 0) {
598 printk(BIOS_ERR, "MRC: ERROR setting PRR for '%s'.\n", name);
599 return -1;
600 }
601
602 printk(BIOS_INFO, "MRC: Enabled Protected Range on '%s'.\n", name);
603 return 0;
604 }
605
606 static void protect_mrc_region(void)
607 {
608 /*
609 * Check if there is a single unified region that encompasses both
610 * RECOVERY_MRC_CACHE and DEFAULT_MRC_CACHE. In that case protect the
611 * entire region using a single PRR.
612 *
613 * If we are not able to protect the entire region, try protecting
614 * individual regions next.
615 */
616 if (protect_mrc_cache(UNIFIED_MRC_CACHE) == 0)
617 return;
618
619 if (CONFIG(HAS_RECOVERY_MRC_CACHE))
620 protect_mrc_cache(RECOVERY_MRC_CACHE);
621
622 protect_mrc_cache(DEFAULT_MRC_CACHE);
623 }
624
625 static void invalidate_normal_cache(void)
626 {
627 struct region_file cache_file;
628 struct region_device rdev;
629 const char *name = DEFAULT_MRC_CACHE;
630
631 /*
632 * If !HAS_RECOVERY_MRC_CACHE and VBOOT_STARTS_IN_ROMSTAGE is
633 * selected, this means that memory training occurs before
634 * verified boot (in RO), so normal mode cache does not need
635 * to be invalidated.
636 */
637 if (!CONFIG(HAS_RECOVERY_MRC_CACHE) && CONFIG(VBOOT_STARTS_IN_ROMSTAGE))
638 return;
639
640 /* We only invalidate the normal cache in recovery mode. */
641 if (!vboot_recovery_mode_enabled())
642 return;
643
644 /*
645 * For platforms with a recovery mrc_cache, no need to
646 * invalidate when retrain switch is not set.
647 */
648 if (CONFIG(HAS_RECOVERY_MRC_CACHE) && !get_recovery_mode_retrain_switch())
649 return;
650
651 if (fmap_locate_area_as_rdev_rw(name, &rdev) < 0) {
652 printk(BIOS_ERR, "MRC: Couldn't find '%s' region. Invalidation failed\n",
653 name);
654 return;
655 }
656
657 if (region_file_init(&cache_file, &rdev) < 0) {
658 printk(BIOS_ERR, "MRC: region file invalid for '%s'. Invalidation failed\n",
659 name);
660 return;
661 }
662
663 /* Push an update that consists of 4 bytes that is smaller than the
664 * MRC metadata as well as an invalid signature. */
665 if (region_file_update_data(&cache_file, &mrc_invalid_sig,
666 sizeof(mrc_invalid_sig)) < 0)
667 printk(BIOS_ERR, "MRC: invalidation failed for '%s'.\n", name);
668 }
669
670 static void update_mrc_cache_from_cbmem(int type)
671 {
672 const struct cache_region *cr;
673 struct region region;
674 const struct cbmem_entry *to_be_updated;
675
676 cr = lookup_region(&region, type);
677
678 if (cr == NULL) {
679 printk(BIOS_INFO, "MRC: could not find cache_region type %d\n", type);
680 return;
681 }
682
683 to_be_updated = cbmem_entry_find(cr->cbmem_id);
684
685 if (to_be_updated == NULL) {
686 printk(BIOS_INFO, "MRC: No data in cbmem for '%s'.\n",
687 cr->name);
688 return;
689 }
690
691 update_mrc_cache_by_type(type,
692 /* pointer to mrc_cache entry metadata header */
693 cbmem_entry_start(to_be_updated),
694 /* pointer to start of mrc_cache entry data */
695 cbmem_entry_start(to_be_updated) +
696 sizeof(struct mrc_metadata),
697 /* size of just data portion of the entry */
698 cbmem_entry_size(to_be_updated) -
699 sizeof(struct mrc_metadata));
700 }
701
702 static void finalize_mrc_cache(void *unused)
703 {
704 if (CONFIG(MRC_STASH_TO_CBMEM)) {
705 update_mrc_cache_from_cbmem(MRC_TRAINING_DATA);
706
707 if (CONFIG(MRC_SETTINGS_VARIABLE_DATA))
708 update_mrc_cache_from_cbmem(MRC_VARIABLE_DATA);
709 }
710
711 invalidate_normal_cache();
712
713 protect_mrc_region();
714 }
715
716 int mrc_cache_stash_data(int type, uint32_t version, const void *data,
717 size_t size)
718 {
719 const struct cache_region *cr;
720
721 struct mrc_metadata md = {
722 .signature = MRC_DATA_SIGNATURE,
723 .data_size = size,
724 .version = version,
725 .data_hash = xxh32(data, size, 0),
726 };
727 md.header_hash = xxh32(&md, sizeof(md), 0);
728
729 if (CONFIG(MRC_STASH_TO_CBMEM)) {
730 /* Store data in cbmem for use in ramstage */
731 struct mrc_metadata *cbmem_md;
732 size_t cbmem_size;
733 cbmem_size = sizeof(*cbmem_md) + size;
734
735 cr = lookup_region_type(type);
736 if (cr == NULL) {
737 printk(BIOS_INFO, "MRC: No region type found. Skip adding to cbmem for type %d.\n",
738 type);
739 return 0;
740 }
741
742 cbmem_md = cbmem_add(cr->cbmem_id, cbmem_size);
743
744 if (cbmem_md == NULL) {
745 printk(BIOS_ERR, "MRC: failed to add '%s' to cbmem.\n",
746 cr->name);
747 return -1;
748 }
749
750 memcpy(cbmem_md, &md, sizeof(*cbmem_md));
751 /* cbmem_md + 1 is the pointer to the mrc_cache data */
752 memcpy(cbmem_md + 1, data, size);
753 } else {
754 /* Otherwise store to mrc_cache right away */
755 update_mrc_cache_by_type(type, &md, data, size);
756 }
757 return 0;
758 }
759
760 /*
761 * Ensures MRC training data is stored into SPI after PCI enumeration is done.
762 * Some implementations may require this to be later than others.
763 */
764 #if CONFIG(MRC_WRITE_NV_LATE)
765 BOOT_STATE_INIT_ENTRY(BS_OS_RESUME_CHECK, BS_ON_ENTRY, finalize_mrc_cache, NULL);
766 #else
767 BOOT_STATE_INIT_ENTRY(BS_DEV_ENUMERATE, BS_ON_EXIT, finalize_mrc_cache, NULL);
768 #endif