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