cmd: add tlv_eeprom command

Add support for read/write of ONIE "Tlvinfo" EEPROM data format. TLV
stands for Type-Length-Value. The data format is described here:

  https://opencomputeproject.github.io/onie/design-spec/hw_requirements.html#board-eeprom-information-format

Based on U-Boot patch from the Open Compute project:

  ec87e872d4/patches/u-boot/common/feature-sys-eeprom-tlv-common.patch

Keep only I2C EEPROM support. Use the generic eeprom driver. Fix
checkpatch issues.

Add support for multiple EEPROM TLV stores on the same system. This is
useful in case of SOM and carrier that both provide ID and hardware
configuration information.

Add option to enable for SPL. This allows selection of RAM configuration
based on EEPROM stored board identification.

Signed-off-by: Baruch Siach <baruch@tkos.co.il>
This commit is contained in:
Baruch Siach 2020-01-21 15:44:54 +02:00 committed by Stefan Roese
parent ae7ba525a5
commit 1c79f2ff88
4 changed files with 1273 additions and 0 deletions

View File

@ -242,6 +242,20 @@ config CMD_REGINFO
help
Register dump
config CMD_TLV_EEPROM
bool "tlv_eeprom"
depends on I2C_EEPROM
help
Display and program the system EEPROM data block in ONIE Tlvinfo
format. TLV stands for Type-Length-Value.
config SPL_CMD_TLV_EEPROM
bool "tlv_eeprom for SPL"
depends on SPL_I2C_EEPROM
select SPL_DRIVERS_MISC_SUPPORT
help
Read system EEPROM data block in ONIE Tlvinfo format from SPL.
endmenu
menu "Boot commands"

View File

@ -183,6 +183,8 @@ obj-$(CONFIG_X86) += x86/
obj-$(CONFIG_ARCH_MVEBU) += mvebu/
endif # !CONFIG_SPL_BUILD
obj-$(CONFIG_$(SPL_)CMD_TLV_EEPROM) += tlv_eeprom.o
# core command
obj-y += nvedit.o

1105
cmd/tlv_eeprom.c Normal file

File diff suppressed because it is too large Load Diff

152
include/tlv_eeprom.h Normal file
View File

@ -0,0 +1,152 @@
/* SPDX-License-Identifier: GPL-2.0+ */
/*
* See file CREDITS for list of people who contributed to this
* project.
*/
#ifndef __TLV_EEPROM_H_
#define __TLV_EEPROM_H_
/*
* The Definition of the TlvInfo EEPROM format can be found at onie.org or
* github.com/onie
*/
/*
* TlvInfo header: Layout of the header for the TlvInfo format
*
* See the end of this file for details of this eeprom format
*/
struct __attribute__ ((__packed__)) tlvinfo_header {
char signature[8]; /* 0x00 - 0x07 EEPROM Tag "TlvInfo" */
u8 version; /* 0x08 Structure version */
u16 totallen; /* 0x09 - 0x0A Length of all data which follows */
};
// Header Field Constants
#define TLV_INFO_ID_STRING "TlvInfo"
#define TLV_INFO_VERSION 0x01
#define TLV_INFO_MAX_LEN 2048
#define TLV_TOTAL_LEN_MAX (TLV_INFO_MAX_LEN - \
sizeof(struct tlvinfo_header))
/*
* TlvInfo TLV: Layout of a TLV field
*/
struct __attribute__ ((__packed__)) tlvinfo_tlv {
u8 type;
u8 length;
u8 value[0];
};
/* Maximum length of a TLV value in bytes */
#define TLV_VALUE_MAX_LEN 255
/**
* The TLV Types.
*
* Keep these in sync with tlv_code_list in cmd/tlv_eeprom.c
*/
#define TLV_CODE_PRODUCT_NAME 0x21
#define TLV_CODE_PART_NUMBER 0x22
#define TLV_CODE_SERIAL_NUMBER 0x23
#define TLV_CODE_MAC_BASE 0x24
#define TLV_CODE_MANUF_DATE 0x25
#define TLV_CODE_DEVICE_VERSION 0x26
#define TLV_CODE_LABEL_REVISION 0x27
#define TLV_CODE_PLATFORM_NAME 0x28
#define TLV_CODE_ONIE_VERSION 0x29
#define TLV_CODE_MAC_SIZE 0x2A
#define TLV_CODE_MANUF_NAME 0x2B
#define TLV_CODE_MANUF_COUNTRY 0x2C
#define TLV_CODE_VENDOR_NAME 0x2D
#define TLV_CODE_DIAG_VERSION 0x2E
#define TLV_CODE_SERVICE_TAG 0x2F
#define TLV_CODE_VENDOR_EXT 0xFD
#define TLV_CODE_CRC_32 0xFE
#if CONFIG_IS_ENABLED(CMD_TLV_EEPROM)
/**
* read_tlv_eeprom - Read the EEPROM binary data from the hardware
* @eeprom: Pointer to buffer to hold the binary data
* @offset: Offset within EEPROM block to read data from
* @len : Maximum size of buffer
* @dev : EEPROM device to read
*
* Note: this routine does not validate the EEPROM data.
*
*/
int read_tlv_eeprom(void *eeprom, int offset, int len, int dev);
/**
* write_tlv_eeprom - Write the entire EEPROM binary data to the hardware
* @eeprom: Pointer to buffer to hold the binary data
* @len : Maximum size of buffer
*
* Note: this routine does not validate the EEPROM data.
*
*/
int write_tlv_eeprom(void *eeprom, int len);
/**
* read_tlvinfo_tlv_eeprom - Read the TLV from EEPROM, and validate
* @eeprom: Pointer to buffer to hold the binary data. Must point to a buffer
* of size at least TLV_INFO_MAX_LEN.
* @hdr : Points to pointer to TLV header (output)
* @first_entry : Points to pointer to first TLV entry (output)
* @dev : EEPROM device to read
*
* Store the raw EEPROM data from EEPROM @dev in the @eeprom buffer. If TLV is
* valid set *@hdr and *@first_entry.
*
* Returns 0 when read from EEPROM is successful, and the data is valid.
* Returns <0 error value when EEPROM read fails. Return -EINVAL when TLV is
* invalid.
*
*/
int read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr,
struct tlvinfo_tlv **first_entry, int dev);
#else /* !CONFIG_IS_ENABLED(CMD_TLV_EEPROM) */
static inline int read_tlv_eeprom(void *eeprom, int offset, int len, int dev)
{
return -ENOTSUPP;
}
static inline int write_tlv_eeprom(void *eeprom, int len)
{
return -ENOTSUPP;
}
static inline int
read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr,
struct tlvinfo_tlv **first_entry, int dev)
{
return -ENOTSUPP;
}
#endif /* CONFIG_IS_ENABLED(CMD_TLV_EEPROM) */
/**
* is_valid_tlvinfo_header
*
* Perform sanity checks on the first 11 bytes of the TlvInfo EEPROM
* data pointed to by the parameter:
* 1. First 8 bytes contain null-terminated ASCII string "TlvInfo"
* 2. Version byte is 1
* 3. Total length bytes contain value which is less than or equal
* to the allowed maximum (2048-11)
*
*/
static inline bool is_valid_tlvinfo_header(struct tlvinfo_header *hdr)
{
return ((strcmp(hdr->signature, TLV_INFO_ID_STRING) == 0) &&
(hdr->version == TLV_INFO_VERSION) &&
(be16_to_cpu(hdr->totallen) <= TLV_TOTAL_LEN_MAX));
}
#endif /* __TLV_EEPROM_H_ */