coreboot
coreboot is an Open Source project aimed at replacing the proprietary BIOS found in most computers.
mrc_cache.c File Reference
#include <string.h>
#include <boot_device.h>
#include <bootstate.h>
#include <bootmode.h>
#include <console/console.h>
#include <cbmem.h>
#include <elog.h>
#include <fmap.h>
#include <ip_checksum.h>
#include <region_file.h>
#include <security/vboot/antirollback.h>
#include <security/vboot/mrc_cache_hash_tpm.h>
#include <security/vboot/vboot_common.h>
#include <spi_flash.h>
#include "mrc_cache.h"
Include dependency graph for mrc_cache.c:

Go to the source code of this file.

Data Structures

struct  mrc_metadata
 
struct  cache_region
 

Macros

#define DEFAULT_MRC_CACHE   "RW_MRC_CACHE"
 
#define VARIABLE_MRC_CACHE   "RW_VAR_MRC_CACHE"
 
#define RECOVERY_MRC_CACHE   "RECOVERY_MRC_CACHE"
 
#define UNIFIED_MRC_CACHE   "UNIFIED_MRC_CACHE"
 
#define MRC_DATA_SIGNATURE   (('M'<<0)|('R'<<8)|('C'<<16)|('D'<<24))
 
#define NORMAL_FLAG   (1 << 0)
 
#define RECOVERY_FLAG   (1 << 1)
 

Enumerations

enum  result { UPDATE_FAILURE = -1 , UPDATE_SUCCESS = 0 , ALREADY_UPTODATE = 1 }
 

Functions

 _Static_assert (!CONFIG(MRC_SAVE_HASH_IN_TPM)||CONFIG(VBOOT_STARTS_IN_BOOTBLOCK), "for TPM MRC hash functionality, vboot must start in bootblock")
 
static int lookup_region_by_name (const char *name, struct region *r)
 
static const struct cache_regionlookup_region_type (int type)
 
static const struct cache_regionlookup_region (struct region *r, int type)
 
static int mrc_header_valid (struct region_device *rdev, struct mrc_metadata *md)
 
static int mrc_data_valid (int type, const struct mrc_metadata *md, void *data, size_t data_size)
 
static int mrc_cache_get_latest_slot_info (const char *name, const struct region_device *backing_rdev, struct mrc_metadata *md, struct region_file *cache_file, struct region_device *rdev, bool fail_bad_data)
 
static int mrc_cache_find_current (int type, uint32_t version, struct region_device *rdev, struct mrc_metadata *md)
 
ssize_t mrc_cache_load_current (int type, uint32_t version, void *buffer, size_t buffer_size)
 mrc_cache_load_current More...
 
voidmrc_cache_current_mmap_leak (int type, uint32_t version, size_t *data_size)
 mrc_cache_mmap_leak More...
 
static bool mrc_cache_needs_update (const struct region_device *rdev, const struct mrc_metadata *new_md, const void *new_data, size_t new_data_size)
 
static void log_event_cache_update (uint8_t slot, enum result res)
 
static void update_mrc_cache_by_type (int type, struct mrc_metadata *new_md, const void *new_data, size_t new_data_size)
 
static int nvm_is_write_protected (void)
 
static int nvm_protect (const struct region *r)
 
static int protect_mrc_cache (const char *name)
 
static void protect_mrc_region (void)
 
static void invalidate_normal_cache (void)
 
static void update_mrc_cache_from_cbmem (int type)
 
static void finalize_mrc_cache (void *unused)
 
int mrc_cache_stash_data (int type, uint32_t version, const void *data, size_t size)
 Returns < 0 on error, 0 on success. More...
 
 BOOT_STATE_INIT_ENTRY (BS_DEV_ENUMERATE, BS_ON_EXIT, finalize_mrc_cache, NULL)
 

Variables

struct mrc_metadata __packed
 
static const struct cache_region recovery_training
 
static const struct cache_region normal_training
 
static const struct cache_region variable_data
 
static const struct cache_regioncache_regions []
 

Macro Definition Documentation

◆ DEFAULT_MRC_CACHE

#define DEFAULT_MRC_CACHE   "RW_MRC_CACHE"

Definition at line 20 of file mrc_cache.c.

◆ MRC_DATA_SIGNATURE

#define MRC_DATA_SIGNATURE   (('M'<<0)|('R'<<8)|('C'<<16)|('D'<<24))

Definition at line 25 of file mrc_cache.c.

◆ NORMAL_FLAG

#define NORMAL_FLAG   (1 << 0)

Definition at line 41 of file mrc_cache.c.

◆ RECOVERY_FLAG

#define RECOVERY_FLAG   (1 << 1)

Definition at line 42 of file mrc_cache.c.

◆ RECOVERY_MRC_CACHE

#define RECOVERY_MRC_CACHE   "RECOVERY_MRC_CACHE"

Definition at line 22 of file mrc_cache.c.

◆ UNIFIED_MRC_CACHE

#define UNIFIED_MRC_CACHE   "UNIFIED_MRC_CACHE"

Definition at line 23 of file mrc_cache.c.

◆ VARIABLE_MRC_CACHE

#define VARIABLE_MRC_CACHE   "RW_VAR_MRC_CACHE"

Definition at line 21 of file mrc_cache.c.

Enumeration Type Documentation

◆ result

enum result
Enumerator
UPDATE_FAILURE 
UPDATE_SUCCESS 
ALREADY_UPTODATE 

Definition at line 35 of file mrc_cache.c.

Function Documentation

◆ _Static_assert()

_Static_assert ( CONFIGMRC_SAVE_HASH_IN_TPM)||CONFIG(VBOOT_STARTS_IN_BOOTBLOCK,
"for TPM MRC hash  functionality,
vboot must start in bootblock"   
)

◆ BOOT_STATE_INIT_ENTRY()

BOOT_STATE_INIT_ENTRY ( BS_DEV_ENUMERATE  ,
BS_ON_EXIT  ,
finalize_mrc_cache  ,
NULL   
)

◆ finalize_mrc_cache()

static void finalize_mrc_cache ( void unused)
static

Definition at line 673 of file mrc_cache.c.

References CONFIG, invalidate_normal_cache(), MRC_TRAINING_DATA, MRC_VARIABLE_DATA, protect_mrc_region(), and update_mrc_cache_from_cbmem().

Here is the call graph for this function:

◆ invalidate_normal_cache()

static void invalidate_normal_cache ( void  )
static

Definition at line 596 of file mrc_cache.c.

References BIOS_ERR, CONFIG, DEFAULT_MRC_CACHE, fmap_locate_area_as_rdev_rw(), get_recovery_mode_retrain_switch(), MRC_DATA_SIGNATURE, name, printk, rdev, region_file_init(), region_file_update_data(), and vboot_recovery_mode_enabled().

Referenced by finalize_mrc_cache().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ log_event_cache_update()

static void log_event_cache_update ( uint8_t  slot,
enum result  res 
)
static

Definition at line 401 of file mrc_cache.c.

References BIOS_ERR, elog_add_event_raw(), ELOG_MEM_CACHE_UPDATE_STATUS_FAIL, ELOG_MEM_CACHE_UPDATE_STATUS_SUCCESS, ELOG_TYPE_MEM_CACHE_UPDATE, printk, elog_event_mem_cache_update::slot, type, UPDATE_FAILURE, and UPDATE_SUCCESS.

Referenced by update_mrc_cache_by_type().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lookup_region()

static const struct cache_region* lookup_region ( struct region r,
int  type 
)
static

Definition at line 149 of file mrc_cache.c.

References BIOS_ERR, lookup_region_by_name(), lookup_region_type(), cache_region::name, NULL, printk, and type.

Referenced by mrc_cache_find_current(), update_mrc_cache_by_type(), and update_mrc_cache_from_cbmem().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lookup_region_by_name()

static int lookup_region_by_name ( const char *  name,
struct region r 
)
static

Definition at line 122 of file mrc_cache.c.

References fmap_locate_area(), and name.

Referenced by lookup_region(), and protect_mrc_cache().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ lookup_region_type()

static const struct cache_region* lookup_region_type ( int  type)
static

Definition at line 129 of file mrc_cache.c.

References ARRAY_SIZE, cache_regions, CONFIG, cache_region::flags, NORMAL_FLAG, NULL, RECOVERY_FLAG, type, and vboot_recovery_mode_enabled().

Referenced by lookup_region(), mrc_cache_stash_data(), and mrc_data_valid().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrc_cache_current_mmap_leak()

void* mrc_cache_current_mmap_leak ( int  type,
uint32_t  version,
size_t data_size 
)

mrc_cache_mmap_leak

Return a pointer to a buffer with the latest slot data. An mmap will be executed (without a matching unmap). This will be a common entry point for platforms where mmap is considered a noop, like x86

Definition at line 342 of file mrc_cache.c.

References BIOS_INFO, mrc_metadata::data_size, mrc_cache_find_current(), mrc_data_valid(), NULL, printk, rdev, rdev_mmap_full(), region_device_sz(), type, and version.

Referenced by check_region_overlap(), get_cached_training(), init_dram_ddr3(), OemInitResume(), platform_fsp_memory_init_params_cb(), prepare_mrc_cache(), raminit(), raminit_common(), and sdram_initialize().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrc_cache_find_current()

static int mrc_cache_find_current ( int  type,
uint32_t  version,
struct region_device rdev,
struct mrc_metadata md 
)
static

◆ mrc_cache_get_latest_slot_info()

static int mrc_cache_get_latest_slot_info ( const char *  name,
const struct region_device backing_rdev,
struct mrc_metadata md,
struct region_file cache_file,
struct region_device rdev,
bool  fail_bad_data 
)
static

Definition at line 239 of file mrc_cache.c.

References BIOS_ERR, BIOS_NOTICE, mrc_header_valid(), name, printk, rdev, rdev_chain(), region_file_data(), and region_file_init().

Referenced by mrc_cache_find_current(), and update_mrc_cache_by_type().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrc_cache_load_current()

ssize_t mrc_cache_load_current ( int  type,
uint32_t  version,
void buffer,
size_t  buffer_size 
)

mrc_cache_load_current

Fill in the buffer with the latest slot data. This will be a common entry point for ARM platforms. Returns < 0 on error, size of the returned data on success.

Definition at line 319 of file mrc_cache.c.

References buffer, mrc_metadata::data_size, mrc_cache_find_current(), mrc_data_valid(), rdev, rdev_readat(), region_device_sz(), type, and version.

Referenced by mt_mem_init_run(), and qclib_load_and_run().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrc_cache_needs_update()

static bool mrc_cache_needs_update ( const struct region_device rdev,
const struct mrc_metadata new_md,
const void new_data,
size_t  new_data_size 
)
static

Definition at line 369 of file mrc_cache.c.

References BIOS_ERR, memcmp(), NULL, printk, rdev, rdev_mmap_full(), rdev_munmap(), and region_device_sz().

Referenced by update_mrc_cache_by_type().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrc_cache_stash_data()

int mrc_cache_stash_data ( int  type,
uint32_t  version,
const void data,
size_t  size 
)

◆ mrc_data_valid()

static int mrc_data_valid ( int  type,
const struct mrc_metadata md,
void data,
size_t  data_size 
)
static

Definition at line 209 of file mrc_cache.c.

References BIOS_ERR, checksum(), compute_ip_checksum(), CONFIG, mrc_metadata::data_checksum, mrc_metadata::data_size, lookup_region_type(), mrc_cache_verify_hash(), NULL, printk, cache_region::tpm_hash_index, and type.

Referenced by mrc_cache_current_mmap_leak(), and mrc_cache_load_current().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ mrc_header_valid()

static int mrc_header_valid ( struct region_device rdev,
struct mrc_metadata md 
)
static

Definition at line 167 of file mrc_cache.c.

References BIOS_ERR, checksum(), compute_ip_checksum(), mrc_metadata::data_size, mrc_metadata::header_checksum, MRC_DATA_SIGNATURE, printk, rdev, rdev_chain(), rdev_readat(), region_device_sz(), and mrc_metadata::signature.

Referenced by mrc_cache_get_latest_slot_info().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nvm_is_write_protected()

static int nvm_is_write_protected ( void  )
static

Definition at line 510 of file mrc_cache.c.

References BIOS_DEBUG, BIOS_ERR, boot_device_spi_flash(), CONFIG, get_write_protect_state(), printk, and spi_flash_status().

Referenced by protect_mrc_cache().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nvm_protect()

static int nvm_protect ( const struct region r)
static

Definition at line 539 of file mrc_cache.c.

References boot_device_spi_flash(), CONFIG, spi_flash_ctrlr_protect_region(), and WRITE_PROTECT.

Referenced by protect_mrc_cache().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ protect_mrc_cache()

static int protect_mrc_cache ( const char *  name)
static

Definition at line 551 of file mrc_cache.c.

References BIOS_ERR, BIOS_INFO, CONFIG, lookup_region_by_name(), name, nvm_is_write_protected(), nvm_protect(), and printk.

Referenced by protect_mrc_region().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ protect_mrc_region()

static void protect_mrc_region ( void  )
static

Definition at line 577 of file mrc_cache.c.

References CONFIG, DEFAULT_MRC_CACHE, protect_mrc_cache(), RECOVERY_MRC_CACHE, and UNIFIED_MRC_CACHE.

Referenced by finalize_mrc_cache().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ update_mrc_cache_by_type()

◆ update_mrc_cache_from_cbmem()

static void update_mrc_cache_from_cbmem ( int  type)
static

Definition at line 641 of file mrc_cache.c.

References BIOS_INFO, cbmem_entry_find(), cbmem_entry_size(), cbmem_entry_start(), cache_region::cbmem_id, lookup_region(), cache_region::name, NULL, printk, type, and update_mrc_cache_by_type().

Referenced by finalize_mrc_cache().

Here is the call graph for this function:
Here is the caller graph for this function:

Variable Documentation

◆ __packed

◆ cache_regions

const struct cache_region* cache_regions[]
static
Initial value:
= {
}
static const struct cache_region normal_training
Definition: mrc_cache.c:66
static const struct cache_region recovery_training
Definition: mrc_cache.c:53
static const struct cache_region variable_data
Definition: mrc_cache.c:88

Definition at line 111 of file mrc_cache.c.

Referenced by lookup_region_type().

◆ normal_training

const struct cache_region normal_training
static
Initial value:
= {
.cbmem_id = CBMEM_ID_MRCDATA,
.tpm_hash_index = MRC_RW_HASH_NV_INDEX,
.flags = NORMAL_FLAG,
}
#define MRC_RW_HASH_NV_INDEX
Definition: antirollback.h:29
#define CBMEM_ID_MRCDATA
Definition: cbmem_id.h:37
#define ELOG_MEM_CACHE_UPDATE_SLOT_NORMAL
Definition: elog.h:266
#define NORMAL_FLAG
Definition: mrc_cache.c:41
#define DEFAULT_MRC_CACHE
Definition: mrc_cache.c:20
@ MRC_TRAINING_DATA
Definition: mrc_cache.h:11

Definition at line 1 of file mrc_cache.c.

◆ recovery_training

const struct cache_region recovery_training
static
Initial value:
= {
.cbmem_id = CBMEM_ID_MRCDATA,
.tpm_hash_index = MRC_REC_HASH_NV_INDEX,
.flags = 0,
}
#define MRC_REC_HASH_NV_INDEX
Definition: antirollback.h:26
#define ELOG_MEM_CACHE_UPDATE_SLOT_RECOVERY
Definition: elog.h:267
#define RECOVERY_MRC_CACHE
Definition: mrc_cache.c:22

Definition at line 1 of file mrc_cache.c.

◆ variable_data

const struct cache_region variable_data
static
Initial value:
= {
.cbmem_id = CBMEM_ID_VAR_MRCDATA,
.tpm_hash_index = 0,
.flags = NORMAL_FLAG,
}
#define CBMEM_ID_VAR_MRCDATA
Definition: cbmem_id.h:39
#define ELOG_MEM_CACHE_UPDATE_SLOT_VARIABLE
Definition: elog.h:268
#define VARIABLE_MRC_CACHE
Definition: mrc_cache.c:21
@ MRC_VARIABLE_DATA
Definition: mrc_cache.h:12

Definition at line 1 of file mrc_cache.c.