Baruch Siach | fad4898 | 2020-01-21 15:44:54 +0200 | [diff] [blame] | 1 | /* SPDX-License-Identifier: GPL-2.0+ */ |
| 2 | /* |
| 3 | * See file CREDITS for list of people who contributed to this |
| 4 | * project. |
| 5 | */ |
| 6 | |
| 7 | #ifndef __TLV_EEPROM_H_ |
| 8 | #define __TLV_EEPROM_H_ |
| 9 | |
| 10 | /* |
| 11 | * The Definition of the TlvInfo EEPROM format can be found at onie.org or |
| 12 | * github.com/onie |
| 13 | */ |
| 14 | |
| 15 | /* |
| 16 | * TlvInfo header: Layout of the header for the TlvInfo format |
| 17 | * |
| 18 | * See the end of this file for details of this eeprom format |
| 19 | */ |
| 20 | struct __attribute__ ((__packed__)) tlvinfo_header { |
| 21 | char signature[8]; /* 0x00 - 0x07 EEPROM Tag "TlvInfo" */ |
| 22 | u8 version; /* 0x08 Structure version */ |
| 23 | u16 totallen; /* 0x09 - 0x0A Length of all data which follows */ |
| 24 | }; |
| 25 | |
| 26 | // Header Field Constants |
| 27 | #define TLV_INFO_ID_STRING "TlvInfo" |
| 28 | #define TLV_INFO_VERSION 0x01 |
| 29 | #define TLV_INFO_MAX_LEN 2048 |
| 30 | #define TLV_TOTAL_LEN_MAX (TLV_INFO_MAX_LEN - \ |
| 31 | sizeof(struct tlvinfo_header)) |
| 32 | |
| 33 | /* |
| 34 | * TlvInfo TLV: Layout of a TLV field |
| 35 | */ |
| 36 | struct __attribute__ ((__packed__)) tlvinfo_tlv { |
| 37 | u8 type; |
| 38 | u8 length; |
| 39 | u8 value[0]; |
| 40 | }; |
| 41 | |
| 42 | /* Maximum length of a TLV value in bytes */ |
| 43 | #define TLV_VALUE_MAX_LEN 255 |
| 44 | |
| 45 | /** |
| 46 | * The TLV Types. |
| 47 | * |
| 48 | * Keep these in sync with tlv_code_list in cmd/tlv_eeprom.c |
| 49 | */ |
| 50 | #define TLV_CODE_PRODUCT_NAME 0x21 |
| 51 | #define TLV_CODE_PART_NUMBER 0x22 |
| 52 | #define TLV_CODE_SERIAL_NUMBER 0x23 |
| 53 | #define TLV_CODE_MAC_BASE 0x24 |
| 54 | #define TLV_CODE_MANUF_DATE 0x25 |
| 55 | #define TLV_CODE_DEVICE_VERSION 0x26 |
| 56 | #define TLV_CODE_LABEL_REVISION 0x27 |
| 57 | #define TLV_CODE_PLATFORM_NAME 0x28 |
| 58 | #define TLV_CODE_ONIE_VERSION 0x29 |
| 59 | #define TLV_CODE_MAC_SIZE 0x2A |
| 60 | #define TLV_CODE_MANUF_NAME 0x2B |
| 61 | #define TLV_CODE_MANUF_COUNTRY 0x2C |
| 62 | #define TLV_CODE_VENDOR_NAME 0x2D |
| 63 | #define TLV_CODE_DIAG_VERSION 0x2E |
| 64 | #define TLV_CODE_SERVICE_TAG 0x2F |
| 65 | #define TLV_CODE_VENDOR_EXT 0xFD |
| 66 | #define TLV_CODE_CRC_32 0xFE |
| 67 | |
| 68 | #if CONFIG_IS_ENABLED(CMD_TLV_EEPROM) |
| 69 | |
| 70 | /** |
| 71 | * read_tlv_eeprom - Read the EEPROM binary data from the hardware |
| 72 | * @eeprom: Pointer to buffer to hold the binary data |
| 73 | * @offset: Offset within EEPROM block to read data from |
| 74 | * @len : Maximum size of buffer |
| 75 | * @dev : EEPROM device to read |
| 76 | * |
| 77 | * Note: this routine does not validate the EEPROM data. |
| 78 | * |
| 79 | */ |
| 80 | |
| 81 | int read_tlv_eeprom(void *eeprom, int offset, int len, int dev); |
| 82 | |
| 83 | /** |
| 84 | * write_tlv_eeprom - Write the entire EEPROM binary data to the hardware |
| 85 | * @eeprom: Pointer to buffer to hold the binary data |
| 86 | * @len : Maximum size of buffer |
Josua Mayer | 4d79c98 | 2023-05-05 11:20:46 +0300 | [diff] [blame] | 87 | * @dev : EEPROM device to write |
Baruch Siach | fad4898 | 2020-01-21 15:44:54 +0200 | [diff] [blame] | 88 | * |
| 89 | * Note: this routine does not validate the EEPROM data. |
| 90 | * |
| 91 | */ |
Josua Mayer | 4d79c98 | 2023-05-05 11:20:46 +0300 | [diff] [blame] | 92 | int write_tlv_eeprom(void *eeprom, int len, int dev); |
Baruch Siach | fad4898 | 2020-01-21 15:44:54 +0200 | [diff] [blame] | 93 | |
| 94 | /** |
| 95 | * read_tlvinfo_tlv_eeprom - Read the TLV from EEPROM, and validate |
| 96 | * @eeprom: Pointer to buffer to hold the binary data. Must point to a buffer |
| 97 | * of size at least TLV_INFO_MAX_LEN. |
| 98 | * @hdr : Points to pointer to TLV header (output) |
| 99 | * @first_entry : Points to pointer to first TLV entry (output) |
| 100 | * @dev : EEPROM device to read |
| 101 | * |
| 102 | * Store the raw EEPROM data from EEPROM @dev in the @eeprom buffer. If TLV is |
| 103 | * valid set *@hdr and *@first_entry. |
| 104 | * |
| 105 | * Returns 0 when read from EEPROM is successful, and the data is valid. |
| 106 | * Returns <0 error value when EEPROM read fails. Return -EINVAL when TLV is |
| 107 | * invalid. |
| 108 | * |
| 109 | */ |
| 110 | |
| 111 | int read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr, |
| 112 | struct tlvinfo_tlv **first_entry, int dev); |
| 113 | |
| 114 | #else /* !CONFIG_IS_ENABLED(CMD_TLV_EEPROM) */ |
| 115 | |
| 116 | static inline int read_tlv_eeprom(void *eeprom, int offset, int len, int dev) |
| 117 | { |
Simon Glass | 62360c1 | 2021-03-25 10:26:07 +1300 | [diff] [blame] | 118 | return -ENOSYS; |
Baruch Siach | fad4898 | 2020-01-21 15:44:54 +0200 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | static inline int write_tlv_eeprom(void *eeprom, int len) |
| 122 | { |
Simon Glass | 62360c1 | 2021-03-25 10:26:07 +1300 | [diff] [blame] | 123 | return -ENOSYS; |
Baruch Siach | fad4898 | 2020-01-21 15:44:54 +0200 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | static inline int |
| 127 | read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr, |
| 128 | struct tlvinfo_tlv **first_entry, int dev) |
| 129 | { |
Simon Glass | 62360c1 | 2021-03-25 10:26:07 +1300 | [diff] [blame] | 130 | return -ENOSYS; |
Baruch Siach | fad4898 | 2020-01-21 15:44:54 +0200 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | #endif /* CONFIG_IS_ENABLED(CMD_TLV_EEPROM) */ |
| 134 | |
| 135 | /** |
| 136 | * is_valid_tlvinfo_header |
| 137 | * |
| 138 | * Perform sanity checks on the first 11 bytes of the TlvInfo EEPROM |
| 139 | * data pointed to by the parameter: |
| 140 | * 1. First 8 bytes contain null-terminated ASCII string "TlvInfo" |
| 141 | * 2. Version byte is 1 |
| 142 | * 3. Total length bytes contain value which is less than or equal |
| 143 | * to the allowed maximum (2048-11) |
| 144 | * |
| 145 | */ |
| 146 | static inline bool is_valid_tlvinfo_header(struct tlvinfo_header *hdr) |
| 147 | { |
| 148 | return ((strcmp(hdr->signature, TLV_INFO_ID_STRING) == 0) && |
| 149 | (hdr->version == TLV_INFO_VERSION) && |
| 150 | (be16_to_cpu(hdr->totallen) <= TLV_TOTAL_LEN_MAX)); |
| 151 | } |
| 152 | |
| 153 | #endif /* __TLV_EEPROM_H_ */ |