diff options
Diffstat (limited to 'src/drivers/vpd')
-rw-r--r-- | src/drivers/vpd/Kconfig | 28 | ||||
-rw-r--r-- | src/drivers/vpd/Makefile.inc | 2 | ||||
-rw-r--r-- | src/drivers/vpd/lib_vpd.c | 113 | ||||
-rw-r--r-- | src/drivers/vpd/lib_vpd.h | 226 | ||||
-rw-r--r-- | src/drivers/vpd/vpd.c | 233 | ||||
-rw-r--r-- | src/drivers/vpd/vpd.h | 42 | ||||
-rw-r--r-- | src/drivers/vpd/vpd_tables.h | 115 |
7 files changed, 759 insertions, 0 deletions
diff --git a/src/drivers/vpd/Kconfig b/src/drivers/vpd/Kconfig new file mode 100644 index 0000000000..335b9079de --- /dev/null +++ b/src/drivers/vpd/Kconfig @@ -0,0 +1,28 @@ +## +## This file is part of the coreboot project. +## +## Copyright (C) 2012 The Chromium OS Authors. All rights reserved. +## +## 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. +## + +config VPD + bool "Support for Vital Product Data tables" + default n + help + Enable support for flash based vital product data. + +if VPD + +config VPD_DEBUG + bool "Enable VPD debug output" + default n + +endif diff --git a/src/drivers/vpd/Makefile.inc b/src/drivers/vpd/Makefile.inc new file mode 100644 index 0000000000..91c069d6c8 --- /dev/null +++ b/src/drivers/vpd/Makefile.inc @@ -0,0 +1,2 @@ +romstage-$(CONFIG_VPD) += lib_vpd.c +ramstage-$(CONFIG_VPD) += vpd.c lib_vpd.c diff --git a/src/drivers/vpd/lib_vpd.c b/src/drivers/vpd/lib_vpd.c new file mode 100644 index 0000000000..0744a712ab --- /dev/null +++ b/src/drivers/vpd/lib_vpd.c @@ -0,0 +1,113 @@ +/* + * 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 <assert.h> +#include "lib_vpd.h" + +/* Given an encoded string, this functions decodes the length field which varies + * from 1 byte to many bytes. + * + * The in points the actual byte going to be decoded. The *length returns + * the decoded length field. The number of consumed bytes will be stroed in + * decoded_len. + * + * Returns VPD_FAIL if more bit is 1, but actually reaches the end of string. + */ +int decodeLen(const int32_t max_len, + const uint8_t *in, + int32_t *length, + int32_t *decoded_len) +{ + uint8_t more; + int i = 0; + + assert(length); + assert(decoded_len); + + *length = 0; + do { + if (i >= max_len) + return VPD_FAIL; + + more = in[i] & 0x80; + *length <<= 7; + *length |= in[i] & 0x7f; + ++i; + } while (more); + + *decoded_len = i; + + return VPD_OK; +} + +/* Given the encoded string, this function invokes callback with extracted + * (key, value). The *consumed will be plused the number of bytes consumed in + * this function. + * + * The input_buf points to the first byte of the input buffer. + * + * The *consumed starts from 0, which is actually the next byte to be decoded. + * It can be non-zero to be used in multiple calls. + * + * If one entry is successfully decoded, sends it to callback and returns the + * result. + */ +int decodeVpdString(const int32_t max_len, + const uint8_t *input_buf, + int32_t *consumed, + VpdDecodeCallback callback, + void *callback_arg) +{ + int type; + int32_t key_len, value_len; + int32_t decoded_len; + const uint8_t *key, *value; + + /* type */ + if (*consumed >= max_len) + return VPD_FAIL; + + type = input_buf[*consumed]; + switch (type) { + case VPD_TYPE_INFO: + case VPD_TYPE_STRING: + (*consumed)++; + /* key */ + if (VPD_OK != decodeLen(max_len - *consumed, + &input_buf[*consumed], &key_len, + &decoded_len) || + *consumed + decoded_len >= max_len) { + return VPD_FAIL; + } + + *consumed += decoded_len; + key = &input_buf[*consumed]; + *consumed += key_len; + + /* value */ + if (VPD_OK != decodeLen(max_len - *consumed, + &input_buf[*consumed], + &value_len, &decoded_len) || + *consumed + decoded_len > max_len) { + return VPD_FAIL; + } + *consumed += decoded_len; + value = &input_buf[*consumed]; + *consumed += value_len; + + if (type == VPD_TYPE_STRING) + return callback(key, key_len, value, value_len, + callback_arg); + + return VPD_OK; + + default: + return VPD_FAIL; + break; + } + + return VPD_OK; +} diff --git a/src/drivers/vpd/lib_vpd.h b/src/drivers/vpd/lib_vpd.h new file mode 100644 index 0000000000..7a7566b234 --- /dev/null +++ b/src/drivers/vpd/lib_vpd.h @@ -0,0 +1,226 @@ +/* + * Copyright (c) 2013 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. + * + */ + +#ifndef __LIB_VPD__ +#define __LIB_VPD__ + +#include <inttypes.h> + +enum { + VPD_OK = 0, + VPD_FAIL, +}; + +enum { + VPD_TYPE_TERMINATOR = 0, + VPD_TYPE_STRING, + VPD_TYPE_INFO = 0xfe, + VPD_TYPE_IMPLICIT_TERMINATOR = 0xff, +}; + +enum { + VPD_AS_LONG_AS = -1, +}; + +enum { /* export_type */ + VPD_EXPORT_KEY_VALUE = 1, + VPD_EXPORT_VALUE, + VPD_EXPORT_AS_PARAMETER, +}; + +/* Callback for decodeVpdString to invoke. */ +typedef int VpdDecodeCallback(const uint8_t *key, int32_t key_len, + const uint8_t *value, int32_t value_len, + void *arg); + +/* Container data types */ +struct StringPair { + uint8_t *key; + uint8_t *value; + int pad_len; + int filter_out; /* TRUE means not exported. */ + struct StringPair *next; +}; + +struct PairContainer { + struct StringPair *first; +}; + + +/*********************************************************************** + * Encode and decode VPD entries + ***********************************************************************/ + +/* Encodes the len into multiple bytes with the following format. + * + * 7 6 ............ 0 + * +----+------------------+ + * |More| Length | ... + * +----+------------------+ + * + * The encode_buf points to the actual position we are going to store. + * encoded_len will return the exact bytes we encoded in this function. + * Returns fail if the buffer is not long enough. + */ +int encodeLen( + const int32_t len, + uint8_t *encode_buf, + const int32_t max_len, + int32_t *encoded_len); + +/* Given an encoded string, this functions decodes the length field which varies + * from 1 byte to many bytes. + * + * The in points the actual byte going to be decoded. The *length returns + * the decoded length field. The number of consumed bytes will be stroed in + * decoded_len. + * + * Returns VPD_FAIL if more bit is 1, but actually reaches the end of string. + */ +int decodeLen( + const int32_t max_len, + const uint8_t *in, + int32_t *length, + int32_t *decoded_len); + + +/* Encodes the terminator. + * When calling, the output_buf should point to the start of buffer while + * *generated_len should contain how many bytes exist in buffer now. + * After return, *generated_len would be plused the number of bytes generated + * in this function. + */ +int encodeVpdTerminator( + const int max_buffer_len, + uint8_t *output_buf, + int *generated_len); + +/* Encodes the given type/key/value pair into buffer. + * + * The pad_value_len is used to control the output value length. + * When pad_value_len > 0, the value is outputted into fixed length (pad \0 + * if the value is shorter). Truncated if too long. + * pad_value_len == VPD_NO_LIMIT, output the value as long as possible. + * This is useful when we want to fix the structure layout at beginning. + * + * The encoded string will be stored in output_buf. If it is longer than + * max_buffer_len, this function returns fail. If the buffer is long enough, + * the generated_len will be updated. + * + * When calling, the output_buf should point to the start of buffer while + * *generated_len should contain how many bytes exist in buffer now. + * After return, *generated_len would be plused the number of bytes generated + * in this function. + * + * The initial value of *generated_len can be non-zero, so that this value + * can be used between multiple calls to encodeVpd2Pair(). + */ +int encodeVpdString( + const uint8_t *key, + const uint8_t *value, + const int pad_value_len, + const int max_buffer_len, + uint8_t *output_buf, + int *generated_len); + + +/* Given the encoded string, this function invokes callback with extracted + * (key, value). The *consumed will be plused the number of bytes consumed in + * this function. + * + * The input_buf points to the first byte of the input buffer. + * + * The *consumed starts from 0, which is actually the next byte to be decoded. + * It can be non-zero to be used in multiple calls. + * + * If one entry is successfully decoded, sends it to callback and returns the + * result. + */ +int decodeVpdString( + const int32_t max_len, + const uint8_t *input_buf, + int32_t *consumed, + VpdDecodeCallback callback, + void *callback_arg); + +/*********************************************************************** + * Container helpers + ***********************************************************************/ +void initContainer(struct PairContainer *container); + +struct StringPair *findString(struct PairContainer *container, + const uint8_t *key, + struct StringPair ***prev_next); + +/* If key is already existed in container, its value will be replaced. + * If not existed, creates new entry in container. + */ +void setString(struct PairContainer *container, + const uint8_t *key, + const uint8_t *value, + const int pad_len); + +/* merge all entries in src into dst. If key is duplicate, overwrite it. + */ +void mergeContainer(struct PairContainer *dst, + const struct PairContainer *src); + +/* subtract src from dst. +*/ +int subtractContainer(struct PairContainer *dst, + const struct PairContainer *src); + +/* Given a container, encode its all entries into the buffer. + */ +int encodeContainer(const struct PairContainer *container, + const int max_buf_len, + uint8_t *buf, + int *generated); + +/* Given a VPD blob, decode its entries and push into container. + */ +int decodeToContainer(struct PairContainer *container, + const int32_t max_len, + const uint8_t *input_buf, + int32_t *consumed); + +/* Set filter for exporting functions. + * If filter is NULL, resets the filter so that everything can be exported. + */ +int setContainerFilter(struct PairContainer *container, + const uint8_t *filter); + +/* + * Remove a key. + * Returns VPD_OK if deleted successfully. Otherwise, VPD_FAIL. + */ +int deleteKey(struct PairContainer *container, + const uint8_t *key); + +/* + * Returns number of pairs in container. + */ +int lenOfContainer(const struct PairContainer *container); + +/* + * Export the container content with human-readable text. + * + * The buf points to the first byte of buffer and *generated contains the number + * of bytes already existed in buffer. + * + * Afterward, the *generated will be plused on exact bytes this function has + * generated. + */ +int exportContainer(const int export_type, + const struct PairContainer *container, + const int max_buf_len, + uint8_t *buf, + int *generated); + +void destroyContainer(struct PairContainer *container); + +#endif /* __LIB_VPD__ */ diff --git a/src/drivers/vpd/vpd.c b/src/drivers/vpd/vpd.c new file mode 100644 index 0000000000..e620b58b31 --- /dev/null +++ b/src/drivers/vpd/vpd.c @@ -0,0 +1,233 @@ +/* + * 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.h" +#include "lib_vpd.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; + int32_t key_len, value_len; + 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). + */ +}; + +/* returns the size of data in a VPD 2.0 formatted fmap region, or 0 */ +static int32_t get_vpd_size(const char *fmap_name, int32_t *base) +{ + struct google_vpd_info info; + struct region_device vpd; + int32_t size; + + if (fmap_locate_area_as_rdev(fmap_name, &vpd)) { + printk(BIOS_ERR, "%s: No %s FMAP section.\n", __func__, + fmap_name); + return 0; + } + + size = region_device_sz(&vpd); + + if ((size < GOOGLE_VPD_2_0_OFFSET + sizeof(info)) || + rdev_chain(&vpd, &vpd, GOOGLE_VPD_2_0_OFFSET, + size - GOOGLE_VPD_2_0_OFFSET)) { + printk(BIOS_ERR, "%s: Too small (%d) for Google VPD 2.0.\n", + __func__, size); + return 0; + } + + /* Try if we can find a google_vpd_info, otherwise read whole VPD. */ + if (rdev_readat(&vpd, &info, *base, sizeof(info)) != sizeof(info)) { + printk(BIOS_ERR, "ERROR: Failed to read %s header.\n", + fmap_name); + return 0; + } + + if (memcmp(info.header.magic, VPD_INFO_MAGIC, sizeof(info.header.magic)) + == 0 && size >= info.size + sizeof(info)) { + *base += sizeof(info); + size = info.size; + } else if (info.header.tlv.type == VPD_TYPE_TERMINATOR || + info.header.tlv.type == VPD_TYPE_IMPLICIT_TERMINATOR) { + printk(BIOS_WARNING, "WARNING: %s is uninitialized or empty.\n", + fmap_name); + size = 0; + } else { + size -= GOOGLE_VPD_2_0_OFFSET; + } + + return size; +} + +static void cbmem_add_cros_vpd(int is_recovery) +{ + 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)) { + /* shouldn't happen, but let's be extra defensive */ + printk(BIOS_ERR, "%s: No RO_VPD FMAP section.\n", + __func__); + return; + } + 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); + } + + if (rw_vpd_size) { + if (fmap_locate_area_as_rdev("RW_VPD", &vpd)) { + /* shouldn't happen, but let's be extra defensive */ + printk(BIOS_ERR, "%s: No RW_VPD FMAP section.\n", + __func__); + return; + } + 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); + } +} + +static int vpd_gets_callback(const uint8_t *key, int32_t key_len, + const uint8_t *value, int32_t value_len, + void *arg) +{ + struct vpd_gets_arg *result = (struct vpd_gets_arg *)arg; + if (key_len != result->key_len || + memcmp(key, result->key, key_len) != 0) + /* Returns VPD_OK to continue parsing. */ + return VPD_OK; + + result->matched = 1; + result->value = value; + result->value_len = value_len; + /* Returns VPD_FAIL to stop parsing. */ + return VPD_FAIL; +} + +const void *vpd_find(const char *key, int *size, enum vpd_region region) +{ + struct vpd_gets_arg arg = {0}; + int 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_OK == decodeVpdString(vpd->ro_size, vpd->blob, + &consumed, vpd_gets_callback, &arg)) { + /* Iterate until found or no more entries. */ + } + + if (!arg.matched && region != VPD_RO) + while (VPD_OK == decodeVpdString(vpd->rw_size, + vpd->blob + vpd->ro_size, &consumed, + vpd_gets_callback, &arg)) { + /* Iterate until found or no more entries. */ + } + + if (!arg.matched) + return NULL; + + *size = arg.value_len; + return arg.value; +} + +char *vpd_gets(const char *key, char *buffer, int size, enum vpd_region region) +{ + const void *string_address; + int string_size; + + string_address = vpd_find(key, &string_size, 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'; + } + return buffer; +} + +RAMSTAGE_CBMEM_INIT_HOOK(cbmem_add_cros_vpd) diff --git a/src/drivers/vpd/vpd.h b/src/drivers/vpd/vpd.h new file mode 100644 index 0000000000..6009b8bc0d --- /dev/null +++ b/src/drivers/vpd/vpd.h @@ -0,0 +1,42 @@ +/* + * 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. + */ + +#ifndef __VPD_H__ +#define __VPD_H__ + +enum vpd_region { + VPD_ANY = 0, + VPD_RO = 1, + VPD_RW = 2 +}; +/* + * Reads VPD string value by key. + * + * Reads in at most one less than size characters from VPD and stores them + * into buffer. A terminating null byte ('\0') is stored after the last + * character in the buffer. + * + * Returns NULL if key is not found, otherwise buffer. + */ +char *vpd_gets(const char *key, char *buffer, int size, enum vpd_region region); + +/* + * Find VPD value by key. + * + * Searches for a VPD entry in the VPD cache. If found, places the size of the + * entry into '*size' and returns the pointer to the entry data. + * + * This function presumes that VPD is cached in DRAM (which is the case in the + * current implementation) and as such returns the pointer into the cache. The + * user is not supposed to modify the data, and does not have to free the + * memory. + * + * Returns NULL if key is not found. + */ + +const void *vpd_find(const char *key, int *size, enum vpd_region region); + +#endif /* __VPD_H__ */ diff --git a/src/drivers/vpd/vpd_tables.h b/src/drivers/vpd/vpd_tables.h new file mode 100644 index 0000000000..822ed08b0f --- /dev/null +++ b/src/drivers/vpd/vpd_tables.h @@ -0,0 +1,115 @@ +/* + * 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. + * + * Ported from mosys project (http://code.google.com/p/mosys/). + */ + +#ifndef __LIB_VPD_TABLES_H__ +#define __LIB_VPD_TABLES_H__ + +#include <compiler.h> +#include <inttypes.h> + +#define VPD_ENTRY_MAGIC "_SM_" +#define VPD_INFO_MAGIC \ + "\xfe" /* type: VPD header */ \ + "\x09" /* key length, 9 = 1 + 8 */ \ + "\x01" /* info version, 1 */ \ + "gVpdInfo" /* signature, 8 bytes */ \ + "\x04" /* value length */ + +/* Google specific VPD info */ +struct google_vpd_info { + union { + struct { + uint8_t type; + uint8_t key_len; + uint8_t info_ver; + uint8_t signature[8]; + uint8_t value_len; + } tlv; + uint8_t magic[12]; + } header; + uint32_t size; +} __packed; + +/* Entry */ +struct vpd_entry { + uint8_t anchor_string[4]; + uint8_t entry_cksum; + uint8_t entry_length; + uint8_t major_ver; + uint8_t minor_ver; + uint16_t max_size; + uint8_t entry_rev; + uint8_t format_area[5]; + uint8_t inter_anchor_string[5]; + uint8_t inter_anchor_cksum; + uint16_t table_length; + uint32_t table_address; + uint16_t table_entry_count; + uint8_t bcd_revision; +} __packed; + +/* Header */ +struct vpd_header { + uint8_t type; + uint8_t length; + uint16_t handle; +} __packed; + +/* Type 0 - firmware information */ +struct vpd_table_firmware { + uint8_t vendor; + uint8_t version; + uint16_t start_address; + uint8_t release_date; + uint8_t rom_size_64k_blocks; + uint32_t characteristics; + uint8_t extension[2]; /* v2.4+ */ + uint8_t major_ver; /* v2.4+ */ + uint8_t minor_ver; /* v2.4+ */ + uint8_t ec_major_ver; /* v2.4+ */ + uint8_t ec_minor_ver; /* v2.4+ */ +} __packed; + +/* Type 1 - system information */ +struct vpd_table_system { + uint8_t manufacturer; + uint8_t name; + uint8_t version; + uint8_t serial_number; + uint8_t uuid[16]; + uint8_t wakeup_type; + uint8_t sku_number; /* v2.4+ */ + uint8_t family; /* v2.4+ */ +} __packed; + +/* Type 127 - end of table */ +struct vpd_table_eot { + struct vpd_header header; +} __packed; + +/* Type 241 - binary blob pointer */ +struct vpd_table_binary_blob_pointer { + uint8_t struct_major_version; + uint8_t struct_minor_version; + uint8_t vendor; + uint8_t description; + uint8_t major_version; + uint8_t minor_version; + uint8_t variant; + uint8_t reserved[5]; + uint8_t uuid[16]; + uint32_t offset; + uint32_t size; +} __packed; + +/* The length and number of strings defined here is not a limitation of VPD. + * These numbers were deemed good enough during development. */ +#define VPD_MAX_STRINGS 10 +#define VPD_MAX_STRING_LENGTH 64 + +#endif /* __LIB_VPD_TABLES_H__ */ |