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>
diff --git a/src/drivers/vpd/vpd.c b/src/drivers/vpd/vpd.c
index c6dd339..10f5703 100644
--- a/src/drivers/vpd/vpd.c
+++ b/src/drivers/vpd/vpd.c
@@ -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 @@
 	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 @@
 	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 @@
 		}
 		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 @@
 		}
 		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 @@
 
 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 @@
 	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;
+}