drivers/vpd: add framework to search VPD in romstage

Summary:
Added a framework to search VPD in romstage before memory is
avilable. vpd_cbmem.c and vpd_premem.c are added for
code specific for premem environment and for environment that
cbmem can be used.

Since global variable is forbidden in romstage. A CAR_GLOBAL
variable is defined in vpd.c. This variable holds VPD binary
blobs' base address and size from memory mapped flash.

The overall flow is:
* The CAR variable g_vpd_blob is initialized if it was not,
either at romstage (before FSP-M execution in case of FSP UPD
customization), or at ramstage.
* At ramstage, during CBMEM_INIT, the VPD binary blob contents
are copied into CBMEM.
* At vpd_find() which may be called at romstage or at ramstage,
it sets storage for a local struct vpd_blob variable.
  * The variable gets contents duplicated from g_vpd_blob, if
vpd_find() is called at romstage.
  * The variable gets contents obtained from CBMEM, if vpd_find()
is called at ramstage.

Added a call vpd_get_bool(). Given a key/value pair in VPD
binary blob, and name of a bool type variable, set the variable
value if there is a match.
Several checks are in place:
* The key/value length needs to be correct.
* The key name needs to match.
* THe value is either '1' or '0'.

Test Plan:
* Build an OCP MonoLake coreboot image, flash and run.

Tags:
Signed-off-by: Jonathan Zhang <jonzhang@fb.com>
Change-Id: Iebdba59419a555147fc40391cf17cc6879d9e1b2
Reviewed-on: https://review.coreboot.org/c/coreboot/+/34634
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Hung-Te Lin <hungte@chromium.org>
This commit is contained in:
Jonathan Zhang 2019-07-16 14:37:23 -07:00 committed by Patrick Georgi
parent 4d9d964276
commit b5392f930d
5 changed files with 243 additions and 93 deletions

View File

@ -1,2 +1,2 @@
romstage-$(CONFIG_VPD) += vpd_decode.c
ramstage-$(CONFIG_VPD) += vpd.c vpd_decode.c
romstage-$(CONFIG_VPD) += vpd_decode.c vpd_premem.c vpd.c
ramstage-$(CONFIG_VPD) += vpd_decode.c vpd_cbmem.c vpd.c

View File

@ -4,6 +4,8 @@
* found in the LICENSE file.
*/
#include <arch/early_variables.h>
#include <assert.h>
#include <console/console.h>
#include <cbmem.h>
#include <fmap.h>
@ -15,13 +17,6 @@
#include "vpd_decode.h"
#include "vpd_tables.h"
/* Currently we only support Google VPD 2.0, which has a fixed offset. */
enum {
GOOGLE_VPD_2_0_OFFSET = 0x600,
CROSVPD_CBMEM_MAGIC = 0x43524f53,
CROSVPD_CBMEM_VERSION = 0x0001,
};
struct vpd_gets_arg {
const uint8_t *key;
const uint8_t *value;
@ -29,18 +24,12 @@ struct vpd_gets_arg {
int matched;
};
struct vpd_cbmem {
uint32_t magic;
uint32_t version;
uint32_t ro_size;
uint32_t rw_size;
uint8_t blob[0];
/* The blob contains both RO and RW data. It starts with RO (0 ..
* ro_size) and then RW (ro_size .. ro_size+rw_size).
*/
};
struct vpd_blob g_vpd_blob CAR_GLOBAL = {0};
/* returns the size of data in a VPD 2.0 formatted fmap region, or 0 */
/*
* returns the size of data in a VPD 2.0 formatted fmap region, or 0.
* Also sets *base as the region's base address.
*/
static int32_t get_vpd_size(const char *fmap_name, int32_t *base)
{
struct google_vpd_info info;
@ -86,34 +75,26 @@ static int32_t get_vpd_size(const char *fmap_name, int32_t *base)
return size;
}
static void cbmem_add_cros_vpd(int is_recovery)
static void vpd_get_blob(void)
{
int32_t ro_vpd_base = 0;
int32_t rw_vpd_base = 0;
int32_t ro_vpd_size = get_vpd_size("RO_VPD", &ro_vpd_base);
int32_t rw_vpd_size = get_vpd_size("RW_VPD", &rw_vpd_base);
/* Return if no VPD at all */
if (ro_vpd_size == 0 && rw_vpd_size == 0)
return;
struct vpd_blob *blob = car_get_var_ptr(&g_vpd_blob);
if (!blob)
return;
blob->ro_base = NULL;
blob->ro_size = 0;
blob->rw_base = NULL;
blob->rw_size = 0;
struct region_device vpd;
struct vpd_cbmem *cbmem;
int32_t ro_vpd_base = 0, rw_vpd_base = 0;
int32_t ro_vpd_size, rw_vpd_size;
timestamp_add_now(TS_START_COPYVPD);
ro_vpd_size = get_vpd_size("RO_VPD", &ro_vpd_base);
rw_vpd_size = get_vpd_size("RW_VPD", &rw_vpd_base);
/* no VPD at all? nothing to do then */
if ((ro_vpd_size == 0) && (rw_vpd_size == 0))
return;
cbmem = cbmem_add(CBMEM_ID_VPD, sizeof(*cbmem) + ro_vpd_size +
rw_vpd_size);
if (!cbmem) {
printk(BIOS_ERR, "%s: Failed to allocate CBMEM (%u+%u).\n",
__func__, ro_vpd_size, rw_vpd_size);
return;
}
cbmem->magic = CROSVPD_CBMEM_MAGIC;
cbmem->version = CROSVPD_CBMEM_VERSION;
cbmem->ro_size = 0;
cbmem->rw_size = 0;
if (ro_vpd_size) {
if (fmap_locate_area_as_rdev("RO_VPD", &vpd)) {
@ -124,20 +105,10 @@ static void cbmem_add_cros_vpd(int is_recovery)
}
rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET,
region_device_sz(&vpd) - GOOGLE_VPD_2_0_OFFSET);
if (rdev_readat(&vpd, cbmem->blob, ro_vpd_base, ro_vpd_size) ==
ro_vpd_size) {
cbmem->ro_size = ro_vpd_size;
} else {
printk(BIOS_ERR,
"%s: Reading RO_VPD FMAP section failed.\n",
__func__);
ro_vpd_size = 0;
}
timestamp_add_now(TS_END_COPYVPD_RO);
blob->ro_base = (uint8_t *)(rdev_mmap_full(&vpd) +
sizeof(struct google_vpd_info));
blob->ro_size = ro_vpd_size;
}
if (rw_vpd_size) {
if (fmap_locate_area_as_rdev("RW_VPD", &vpd)) {
/* shouldn't happen, but let's be extra defensive */
@ -147,17 +118,23 @@ static void cbmem_add_cros_vpd(int is_recovery)
}
rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET,
region_device_sz(&vpd) - GOOGLE_VPD_2_0_OFFSET);
if (rdev_readat(&vpd, cbmem->blob + ro_vpd_size, rw_vpd_base,
rw_vpd_size) == rw_vpd_size) {
cbmem->rw_size = rw_vpd_size;
} else {
printk(BIOS_ERR,
"%s: Reading RW_VPD FMAP section failed.\n",
__func__);
}
timestamp_add_now(TS_END_COPYVPD_RW);
blob->rw_base = (uint8_t *)(rdev_mmap_full(&vpd) +
sizeof(struct google_vpd_info));
blob->rw_size = rw_vpd_size;
}
blob->initialized = true;
}
const struct vpd_blob *vpd_load_blob(void)
{
struct vpd_blob *blob = NULL;
blob = car_get_var_ptr(&g_vpd_blob);
if (blob && blob->initialized == false)
vpd_get_blob();
return blob;
}
static int vpd_gets_callback(const uint8_t *key, uint32_t key_len,
@ -179,30 +156,29 @@ static int vpd_gets_callback(const uint8_t *key, uint32_t key_len,
const void *vpd_find(const char *key, int *size, enum vpd_region region)
{
struct vpd_blob blob = {0};
vpd_get_buffers(&blob);
if (blob.ro_size == 0 && blob.rw_size == 0)
return NULL;
struct vpd_gets_arg arg = {0};
uint32_t consumed = 0;
const struct vpd_cbmem *vpd;
vpd = cbmem_find(CBMEM_ID_VPD);
if (!vpd || !vpd->ro_size)
return NULL;
arg.key = (const uint8_t *)key;
arg.key_len = strlen(key);
if (region == VPD_ANY || region == VPD_RO) {
while (vpd_decode_string(
vpd->ro_size, vpd->blob, &consumed,
vpd_gets_callback, &arg) == VPD_DECODE_OK) {
/* Iterate until found or no more entries. */
if ((region == VPD_ANY || region == VPD_RO) && blob.ro_size != 0) {
while (vpd_decode_string(blob.ro_size, blob.ro_base,
&consumed, vpd_gets_callback, &arg) == VPD_DECODE_OK) {
/* Iterate until found or no more entries. */
}
}
if (!arg.matched && region != VPD_RO) {
while (vpd_decode_string(
vpd->rw_size, vpd->blob + vpd->ro_size,
&consumed, vpd_gets_callback,
&arg) == VPD_DECODE_OK) {
/* Iterate until found or no more entries. */
if ((!arg.matched && region != VPD_RO) && blob.rw_size != 0) {
while (vpd_decode_string(blob.rw_size, blob.rw_base,
&consumed, vpd_gets_callback, &arg) == VPD_DECODE_OK) {
/* Iterate until found or no more entries. */
}
}
@ -223,14 +199,40 @@ char *vpd_gets(const char *key, char *buffer, int size, enum vpd_region region)
if (!string_address)
return NULL;
if (size > (string_size + 1)) {
memcpy(buffer, string_address, string_size);
buffer[string_size] = '\0';
} else {
memcpy(buffer, string_address, size - 1);
buffer[size - 1] = '\0';
}
assert(size > 0);
int copy_size = MIN(size - 1, string_size);
memcpy(buffer, string_address, copy_size);
buffer[copy_size] = '\0';
return buffer;
}
RAMSTAGE_CBMEM_INIT_HOOK(cbmem_add_cros_vpd)
/*
* Find value of boolean type vpd key.
*
* During the process, necessary checking is done, such as making
* sure the value length is 1, and value is either '1' or '0'.
*/
bool vpd_get_bool(const char *key, enum vpd_region region, uint8_t *val)
{
int size;
const char *value;
value = vpd_find(key, &size, region);
if (!value) {
printk(BIOS_CRIT, "problem returning from vpd_find.\n");
return false;
}
if (size != 1)
return false;
/* Make sure the value is either '1' or '0' */
if (*value == '1') {
*val = 1;
return true;
} else if (*value == '0') {
*val = 0;
return true;
} else
return false;
}

View File

@ -7,11 +7,37 @@
#ifndef __VPD_H__
#define __VPD_H__
#define GOOGLE_VPD_2_0_OFFSET 0x600
enum vpd_region {
VPD_ANY = 0,
VPD_RO = 1,
VPD_RW = 2
};
/* VPD 2.0 data blob structure */
struct vpd_blob {
bool initialized;
uint8_t *ro_base;
uint32_t ro_size;
uint8_t *rw_base;
uint32_t rw_size;
};
extern struct vpd_blob g_vpd_blob;
/*
* This function loads g_vpd_blob CAR_GLOBAL variable.
* The variable is initialized if it was not.
*/
const struct vpd_blob *vpd_load_blob(void);
/*
* This function gets the base address and size of
* buffers for RO_VPD/RW_VPD binary blobs, and sets
* the struct.
*/
void vpd_get_buffers(struct vpd_blob *blob);
/*
* Reads VPD string value by key.
*
@ -39,4 +65,13 @@ char *vpd_gets(const char *key, char *buffer, int size, enum vpd_region region);
const void *vpd_find(const char *key, int *size, enum vpd_region region);
/*
* Find value of boolean type vpd key.
*
* During the process, necessary checking is done, such as making
* sure the value length is 1, and value is either '1' or '0'.
*/
bool vpd_get_bool(const char *key, enum vpd_region region,
uint8_t *val);
#endif /* __VPD_H__ */

View File

@ -0,0 +1,86 @@
/*
* Copyright (c) 2014 The Chromium OS Authors. All rights reserved.
* Use of this source code is governed by a BSD-style license that can be
* found in the LICENSE file.
*/
#include <console/console.h>
#include <cbmem.h>
#include <fmap.h>
#include <stdlib.h>
#include <string.h>
#include <timestamp.h>
#include "vpd_tables.h"
#include "vpd.h"
/* Currently we only support Google VPD 2.0, which has a fixed offset. */
enum {
CROSVPD_CBMEM_MAGIC = 0x43524f53,
CROSVPD_CBMEM_VERSION = 0x0001,
};
struct vpd_cbmem {
uint32_t magic;
uint32_t version;
uint32_t ro_size;
uint32_t rw_size;
uint8_t blob[0];
/* The blob contains both RO and RW data. It starts with RO (0 ..
* ro_size) and then RW (ro_size .. ro_size+rw_size).
*/
};
static void cbmem_add_cros_vpd(int is_recovery)
{
struct vpd_cbmem *cbmem;
const struct vpd_blob *blob;
timestamp_add_now(TS_START_COPYVPD);
blob = vpd_load_blob();
/* Return if no VPD at all */
if (blob->ro_size == 0 && blob->rw_size == 0)
return;
cbmem = cbmem_add(CBMEM_ID_VPD, sizeof(*cbmem) + blob->ro_size +
blob->rw_size);
if (!cbmem) {
printk(BIOS_ERR, "%s: Failed to allocate CBMEM (%u+%u).\n",
__func__, blob->ro_size, blob->rw_size);
return;
}
cbmem->magic = CROSVPD_CBMEM_MAGIC;
cbmem->version = CROSVPD_CBMEM_VERSION;
cbmem->ro_size = blob->ro_size;
cbmem->rw_size = blob->rw_size;
if (blob->ro_size) {
memcpy(cbmem->blob, blob->ro_base, blob->ro_size);
timestamp_add_now(TS_END_COPYVPD_RO);
}
if (blob->rw_size) {
memcpy(cbmem->blob + blob->ro_size, blob->rw_base,
blob->rw_size);
timestamp_add_now(TS_END_COPYVPD_RW);
}
}
void vpd_get_buffers(struct vpd_blob *blob)
{
const struct vpd_cbmem *vpd;
vpd = cbmem_find(CBMEM_ID_VPD);
if (!vpd || !vpd->ro_size)
return;
blob->ro_base = (void *)vpd->blob;
blob->ro_size = vpd->ro_size;
blob->rw_base = (void *)vpd->blob + vpd->ro_size;
blob->rw_size = vpd->rw_size;
}
RAMSTAGE_CBMEM_INIT_HOOK(cbmem_add_cros_vpd)

View File

@ -0,0 +1,27 @@
/*
* This file is part of the coreboot project.
*
* Copyright (c) 2019 Facebook, 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 <console/console.h>
#include <string.h>
#include "vpd.h"
void vpd_get_buffers(struct vpd_blob *blob)
{
const struct vpd_blob *b;
b = vpd_load_blob();
memcpy(blob, b, sizeof(*b));
}