tlv_eeprom.h 4.3 KB

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