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
87  *
88  * Note: this routine does not validate the EEPROM data.
89  *
90  */
91 int write_tlv_eeprom(void *eeprom, int len);
92 
93 /**
94  * read_tlvinfo_tlv_eeprom - Read the TLV from EEPROM, and validate
95  * @eeprom: Pointer to buffer to hold the binary data. Must point to a buffer
96  *          of size at least TLV_INFO_MAX_LEN.
97  * @hdr   : Points to pointer to TLV header (output)
98  * @first_entry : Points to pointer to first TLV entry (output)
99  * @dev   : EEPROM device to read
100  *
101  * Store the raw EEPROM data from EEPROM @dev in the @eeprom buffer. If TLV is
102  * valid set *@hdr and *@first_entry.
103  *
104  * Returns 0 when read from EEPROM is successful, and the data is valid.
105  * Returns <0 error value when EEPROM read fails. Return -EINVAL when TLV is
106  * invalid.
107  *
108  */
109 
110 int read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr,
111 			    struct tlvinfo_tlv **first_entry, int dev);
112 
113 #else /* !CONFIG_IS_ENABLED(CMD_TLV_EEPROM) */
114 
read_tlv_eeprom(void * eeprom,int offset,int len,int dev)115 static inline int read_tlv_eeprom(void *eeprom, int offset, int len, int dev)
116 {
117 	return -ENOTSUPP;
118 }
119 
write_tlv_eeprom(void * eeprom,int len)120 static inline int write_tlv_eeprom(void *eeprom, int len)
121 {
122 	return -ENOTSUPP;
123 }
124 
125 static inline int
read_tlvinfo_tlv_eeprom(void * eeprom,struct tlvinfo_header ** hdr,struct tlvinfo_tlv ** first_entry,int dev)126 read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr,
127 			struct tlvinfo_tlv **first_entry, int dev)
128 {
129 	return -ENOTSUPP;
130 }
131 
132 #endif /* CONFIG_IS_ENABLED(CMD_TLV_EEPROM) */
133 
134 /**
135  *  is_valid_tlvinfo_header
136  *
137  *  Perform sanity checks on the first 11 bytes of the TlvInfo EEPROM
138  *  data pointed to by the parameter:
139  *      1. First 8 bytes contain null-terminated ASCII string "TlvInfo"
140  *      2. Version byte is 1
141  *      3. Total length bytes contain value which is less than or equal
142  *         to the allowed maximum (2048-11)
143  *
144  */
is_valid_tlvinfo_header(struct tlvinfo_header * hdr)145 static inline bool is_valid_tlvinfo_header(struct tlvinfo_header *hdr)
146 {
147 	return ((strcmp(hdr->signature, TLV_INFO_ID_STRING) == 0) &&
148 		(hdr->version == TLV_INFO_VERSION) &&
149 		(be16_to_cpu(hdr->totallen) <= TLV_TOTAL_LEN_MAX));
150 }
151 
152 #endif /* __TLV_EEPROM_H_ */
153