1 // SPDX-License-Identifier: GPL-2.0+
3 * See file CREDITS for list of people who contributed to this
6 * Copyright (C) 2013 Curt Brune <curt@cumulusnetworks.com>
7 * Copyright (C) 2014 Srideep <srideep_devireddy@dell.com>
8 * Copyright (C) 2013 Miles Tseng <miles_tseng@accton.com>
9 * Copyright (C) 2014,2016 david_yang <david_yang@accton.com>
16 #include <i2c_eeprom.h>
19 #include <linux/ctype.h>
20 #include <u-boot/crc.h>
22 #include "tlv_eeprom.h"
24 DECLARE_GLOBAL_DATA_PTR;
26 #define MAX_TLV_DEVICES 2
28 /* File scope function prototypes */
29 static bool is_checksum_valid(u8 *eeprom);
30 static int read_eeprom(u8 *eeprom);
31 static void show_eeprom(u8 *eeprom);
32 static void decode_tlv(struct tlvinfo_tlv *tlv);
33 static void update_crc(u8 *eeprom);
34 static int prog_eeprom(u8 *eeprom);
35 static bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index);
36 static bool tlvinfo_delete_tlv(u8 *eeprom, u8 code);
37 static bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval);
38 static int set_mac(char *buf, const char *string);
39 static int set_date(char *buf, const char *string);
40 static int set_bytes(char *buf, const char *string, int *converted_accum);
41 static void show_tlv_devices(void);
43 /* Set to 1 if we've read EEPROM into memory */
44 static int has_been_read;
45 /* The EERPOM contents after being read into memory */
46 static u8 eeprom[TLV_INFO_MAX_LEN];
48 static struct udevice *tlv_devices[MAX_TLV_DEVICES];
49 static unsigned int current_dev;
51 #define to_header(p) ((struct tlvinfo_header *)p)
52 #define to_entry(p) ((struct tlvinfo_tlv *)p)
54 #define HDR_SIZE sizeof(struct tlvinfo_header)
55 #define ENT_SIZE sizeof(struct tlvinfo_tlv)
57 static inline bool is_digit(char c)
59 return (c >= '0' && c <= '9');
65 * Perform basic sanity checks on a TLV field. The TLV is pointed to
66 * by the parameter provided.
67 * 1. The type code is not reserved (0x00 or 0xFF)
69 static inline bool is_valid_tlv(struct tlvinfo_tlv *tlv)
71 return((tlv->type != 0x00) && (tlv->type != 0xFF));
77 * Tests if character is an ASCII hex digit
79 static inline u8 is_hex(char p)
81 return (((p >= '0') && (p <= '9')) ||
82 ((p >= 'A') && (p <= 'F')) ||
83 ((p >= 'a') && (p <= 'f')));
89 * Validate the checksum in the provided TlvInfo EEPROM data. First,
90 * verify that the TlvInfo header is valid, then make sure the last
91 * TLV is a CRC-32 TLV. Then calculate the CRC over the EEPROM data
92 * and compare it to the value stored in the EEPROM CRC-32 TLV.
94 static bool is_checksum_valid(u8 *eeprom)
96 struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
97 struct tlvinfo_tlv *eeprom_crc;
98 unsigned int calc_crc;
99 unsigned int stored_crc;
101 // Is the eeprom header valid?
102 if (!is_valid_tlvinfo_header(eeprom_hdr))
105 // Is the last TLV a CRC?
106 eeprom_crc = to_entry(&eeprom[HDR_SIZE +
107 be16_to_cpu(eeprom_hdr->totallen) - (ENT_SIZE + 4)]);
108 if (eeprom_crc->type != TLV_CODE_CRC_32 || eeprom_crc->length != 4)
111 // Calculate the checksum
112 calc_crc = crc32(0, (void *)eeprom,
113 HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen) - 4);
114 stored_crc = (eeprom_crc->value[0] << 24) |
115 (eeprom_crc->value[1] << 16) |
116 (eeprom_crc->value[2] << 8) |
117 eeprom_crc->value[3];
118 return calc_crc == stored_crc;
124 * Read the EEPROM into memory, if it hasn't already been read.
126 static int read_eeprom(u8 *eeprom)
129 struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
130 struct tlvinfo_tlv *eeprom_tlv = to_entry(&eeprom[HDR_SIZE]);
135 /* Read the header */
136 ret = read_tlv_eeprom((void *)eeprom_hdr, 0, HDR_SIZE, current_dev);
137 /* If the header was successfully read, read the TLVs */
138 if (ret == 0 && is_valid_tlvinfo_header(eeprom_hdr))
139 ret = read_tlv_eeprom((void *)eeprom_tlv, HDR_SIZE,
140 be16_to_cpu(eeprom_hdr->totallen),
143 // If the contents are invalid, start over with default contents
144 if (!is_valid_tlvinfo_header(eeprom_hdr) ||
145 !is_checksum_valid(eeprom)) {
146 strcpy(eeprom_hdr->signature, TLV_INFO_ID_STRING);
147 eeprom_hdr->version = TLV_INFO_VERSION;
148 eeprom_hdr->totallen = cpu_to_be16(0);
164 * Display the contents of the EEPROM
166 static void show_eeprom(u8 *eeprom)
170 struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
171 struct tlvinfo_tlv *eeprom_tlv;
173 if (!is_valid_tlvinfo_header(eeprom_hdr)) {
174 printf("EEPROM does not contain data in a valid TlvInfo format.\n");
178 printf("TLV: %u\n", current_dev);
179 printf("TlvInfo Header:\n");
180 printf(" Id String: %s\n", eeprom_hdr->signature);
181 printf(" Version: %d\n", eeprom_hdr->version);
182 printf(" Total Length: %d\n", be16_to_cpu(eeprom_hdr->totallen));
184 printf("TLV Name Code Len Value\n");
185 printf("-------------------- ---- --- -----\n");
187 tlv_end = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen);
188 while (curr_tlv < tlv_end) {
189 eeprom_tlv = to_entry(&eeprom[curr_tlv]);
190 if (!is_valid_tlv(eeprom_tlv)) {
191 printf("Invalid TLV field starting at EEPROM offset %d\n",
195 decode_tlv(eeprom_tlv);
196 curr_tlv += ENT_SIZE + eeprom_tlv->length;
199 printf("Checksum is %s.\n",
200 is_checksum_valid(eeprom) ? "valid" : "invalid");
203 printf("EEPROM dump: (0x%x bytes)", TLV_INFO_MAX_LEN);
204 for (i = 0; i < TLV_INFO_MAX_LEN; i++) {
206 printf("\n%02X: ", i);
207 printf("%02X ", eeprom[i]);
214 * Struct for displaying the TLV codes and names.
216 struct tlv_code_desc {
222 * List of TLV codes and names.
224 static struct tlv_code_desc tlv_code_list[] = {
225 { TLV_CODE_PRODUCT_NAME, "Product Name"},
226 { TLV_CODE_PART_NUMBER, "Part Number"},
227 { TLV_CODE_SERIAL_NUMBER, "Serial Number"},
228 { TLV_CODE_MAC_BASE, "Base MAC Address"},
229 { TLV_CODE_MANUF_DATE, "Manufacture Date"},
230 { TLV_CODE_DEVICE_VERSION, "Device Version"},
231 { TLV_CODE_LABEL_REVISION, "Label Revision"},
232 { TLV_CODE_PLATFORM_NAME, "Platform Name"},
233 { TLV_CODE_ONIE_VERSION, "ONIE Version"},
234 { TLV_CODE_MAC_SIZE, "MAC Addresses"},
235 { TLV_CODE_MANUF_NAME, "Manufacturer"},
236 { TLV_CODE_MANUF_COUNTRY, "Country Code"},
237 { TLV_CODE_VENDOR_NAME, "Vendor Name"},
238 { TLV_CODE_DIAG_VERSION, "Diag Version"},
239 { TLV_CODE_SERVICE_TAG, "Service Tag"},
240 { TLV_CODE_VENDOR_EXT, "Vendor Extension"},
241 { TLV_CODE_CRC_32, "CRC-32"},
245 * Look up a TLV name by its type.
247 static inline const char *tlv_type2name(u8 type)
249 char *name = "Unknown";
252 for (i = 0; i < ARRAY_SIZE(tlv_code_list); i++) {
253 if (tlv_code_list[i].m_code == type) {
254 name = tlv_code_list[i].m_name;
265 * Print a string representing the contents of the TLV field. The format of
267 * 1. The name of the field left justified in 20 characters
268 * 2. The type code in hex right justified in 5 characters
269 * 3. The length in decimal right justified in 4 characters
270 * 4. The value, left justified in however many characters it takes
271 * The validity of EEPROM contents and the TLV field have been verified
272 * prior to calling this function.
274 #define DECODE_NAME_MAX 20
277 * The max decode value is currently for the 'raw' type or the 'vendor
278 * extension' type, both of which have the same decode format. The
279 * max decode string size is computed as follows:
281 * strlen(" 0xFF") * TLV_VALUE_MAX_LEN + 1
284 #define DECODE_VALUE_MAX ((5 * TLV_VALUE_MAX_LEN) + 1)
286 static void decode_tlv(struct tlvinfo_tlv *tlv)
288 char name[DECODE_NAME_MAX];
289 char value[DECODE_VALUE_MAX];
292 strncpy(name, tlv_type2name(tlv->type), DECODE_NAME_MAX);
295 case TLV_CODE_PRODUCT_NAME:
296 case TLV_CODE_PART_NUMBER:
297 case TLV_CODE_SERIAL_NUMBER:
298 case TLV_CODE_MANUF_DATE:
299 case TLV_CODE_LABEL_REVISION:
300 case TLV_CODE_PLATFORM_NAME:
301 case TLV_CODE_ONIE_VERSION:
302 case TLV_CODE_MANUF_NAME:
303 case TLV_CODE_MANUF_COUNTRY:
304 case TLV_CODE_VENDOR_NAME:
305 case TLV_CODE_DIAG_VERSION:
306 case TLV_CODE_SERVICE_TAG:
307 memcpy(value, tlv->value, tlv->length);
308 value[tlv->length] = 0;
310 case TLV_CODE_MAC_BASE:
311 sprintf(value, "%02X:%02X:%02X:%02X:%02X:%02X",
312 tlv->value[0], tlv->value[1], tlv->value[2],
313 tlv->value[3], tlv->value[4], tlv->value[5]);
315 case TLV_CODE_DEVICE_VERSION:
316 sprintf(value, "%u", tlv->value[0]);
318 case TLV_CODE_MAC_SIZE:
319 sprintf(value, "%u", (tlv->value[0] << 8) | tlv->value[1]);
321 case TLV_CODE_VENDOR_EXT:
323 for (i = 0; (i < (DECODE_VALUE_MAX / 5)) && (i < tlv->length);
325 sprintf(value, "%s 0x%02X", value, tlv->value[i]);
328 case TLV_CODE_CRC_32:
329 sprintf(value, "0x%02X%02X%02X%02X",
330 tlv->value[0], tlv->value[1],
331 tlv->value[2], tlv->value[3]);
335 for (i = 0; (i < (DECODE_VALUE_MAX / 5)) && (i < tlv->length);
337 sprintf(value, "%s 0x%02X", value, tlv->value[i]);
342 name[DECODE_NAME_MAX - 1] = 0;
343 printf("%-20s 0x%02X %3d %s\n", name, tlv->type, tlv->length, value);
349 * This function updates the CRC-32 TLV. If there is no CRC-32 TLV, then
350 * one is added. This function should be called after each update to the
351 * EEPROM structure, to make sure the CRC is always correct.
353 static void update_crc(u8 *eeprom)
355 struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
356 struct tlvinfo_tlv *eeprom_crc;
357 unsigned int calc_crc;
360 // Discover the CRC TLV
361 if (!tlvinfo_find_tlv(eeprom, TLV_CODE_CRC_32, &eeprom_index)) {
362 unsigned int totallen = be16_to_cpu(eeprom_hdr->totallen);
364 if ((totallen + ENT_SIZE + 4) > TLV_TOTAL_LEN_MAX)
366 eeprom_index = HDR_SIZE + totallen;
367 eeprom_hdr->totallen = cpu_to_be16(totallen + ENT_SIZE + 4);
369 eeprom_crc = to_entry(&eeprom[eeprom_index]);
370 eeprom_crc->type = TLV_CODE_CRC_32;
371 eeprom_crc->length = 4;
373 // Calculate the checksum
374 calc_crc = crc32(0, (void *)eeprom,
375 HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen) - 4);
376 eeprom_crc->value[0] = (calc_crc >> 24) & 0xFF;
377 eeprom_crc->value[1] = (calc_crc >> 16) & 0xFF;
378 eeprom_crc->value[2] = (calc_crc >> 8) & 0xFF;
379 eeprom_crc->value[3] = (calc_crc >> 0) & 0xFF;
385 * Write the EEPROM data from CPU memory to the hardware.
387 static int prog_eeprom(u8 *eeprom)
390 struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
395 eeprom_len = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen);
396 ret = write_tlv_eeprom(eeprom, eeprom_len);
398 printf("Programming failed.\n");
402 printf("Programming passed.\n");
407 * show_tlv_code_list - Display the list of TLV codes and names
409 void show_tlv_code_list(void)
413 printf("TLV Code TLV Name\n");
414 printf("======== =================\n");
415 for (i = 0; i < ARRAY_SIZE(tlv_code_list); i++) {
416 printf("0x%02X %s\n",
417 tlv_code_list[i].m_code,
418 tlv_code_list[i].m_name);
425 * This function implements the tlv_eeprom command.
427 int do_tlv_eeprom(cmd_tbl_t *cmdtp, int flag, int argc, char *const argv[])
430 struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
432 // If no arguments, read the EERPOM and display its contents
439 // We only look at the first character to the command, so "read" and
440 // "reset" will both be treated as "read".
443 // Read the EEPROM contents
446 if (!read_eeprom(eeprom))
447 printf("EEPROM data loaded from device to memory.\n");
451 // Subsequent commands require that the EEPROM has already been read.
452 if (!has_been_read) {
453 printf("Please read the EEPROM data first, using the 'tlv_eeprom read' command.\n");
457 // Handle the commands that don't take parameters
460 case 'w': /* write */
463 case 'e': /* erase */
464 strcpy(eeprom_hdr->signature, TLV_INFO_ID_STRING);
465 eeprom_hdr->version = TLV_INFO_VERSION;
466 eeprom_hdr->totallen = cpu_to_be16(0);
468 printf("EEPROM data in memory reset.\n");
471 show_tlv_code_list();
483 // The set command takes one or two args.
489 // Set command. If the TLV exists in the EEPROM, delete it. Then if
490 // data was supplied for this TLV add the TLV with the new contents at
495 tcode = simple_strtoul(argv[2], NULL, 0);
496 tlvinfo_delete_tlv(eeprom, tcode);
498 tlvinfo_add_tlv(eeprom, tcode, argv[3]);
499 } else if (cmd == 'd') { /* 'dev' command */
502 devnum = simple_strtoul(argv[2], NULL, 0);
503 if (devnum > MAX_TLV_DEVICES || !tlv_devices[devnum]) {
504 printf("Invalid device number\n");
507 current_dev = devnum;
517 * This macro defines the tlv_eeprom command line command.
519 U_BOOT_CMD(tlv_eeprom, 4, 1, do_tlv_eeprom,
520 "Display and program the system EEPROM data block.",
521 "[read|write|set <type_code> <string_value>|erase|list]\n"
523 " - With no arguments display the current contents.\n"
524 "tlv_eeprom dev [dev]\n"
525 " - List devices or set current EEPROM device.\n"
527 " - Load EEPROM data from device to memory.\n"
529 " - Write the EEPROM data to persistent storage.\n"
530 "tlv_eeprom set <type_code> <string_value>\n"
531 " - Set a field to a value.\n"
532 " - If no string_value, field is deleted.\n"
533 " - Use 'tlv_eeprom write' to make changes permanent.\n"
535 " - Reset the in memory EEPROM data.\n"
536 " - Use 'tlv_eeprom read' to refresh the in memory EEPROM data.\n"
537 " - Use 'tlv_eeprom write' to make changes permanent.\n"
539 " - List the understood TLV codes and names.\n"
545 * This function finds the TLV with the supplied code in the EERPOM.
546 * An offset from the beginning of the EEPROM is returned in the
547 * eeprom_index parameter if the TLV is found.
549 static bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index)
551 struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
552 struct tlvinfo_tlv *eeprom_tlv;
555 // Search through the TLVs, looking for the first one which matches the
556 // supplied type code.
557 *eeprom_index = HDR_SIZE;
558 eeprom_end = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen);
559 while (*eeprom_index < eeprom_end) {
560 eeprom_tlv = to_entry(&eeprom[*eeprom_index]);
561 if (!is_valid_tlv(eeprom_tlv))
563 if (eeprom_tlv->type == tcode)
565 *eeprom_index += ENT_SIZE + eeprom_tlv->length;
573 * This function deletes the TLV with the specified type code from the
576 static bool tlvinfo_delete_tlv(u8 *eeprom, u8 code)
580 struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
581 struct tlvinfo_tlv *eeprom_tlv;
583 // Find the TLV and then move all following TLVs "forward"
584 if (tlvinfo_find_tlv(eeprom, code, &eeprom_index)) {
585 eeprom_tlv = to_entry(&eeprom[eeprom_index]);
586 tlength = ENT_SIZE + eeprom_tlv->length;
587 memcpy(&eeprom[eeprom_index], &eeprom[eeprom_index + tlength],
589 be16_to_cpu(eeprom_hdr->totallen) - eeprom_index -
591 eeprom_hdr->totallen =
592 cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) -
603 * This function adds a TLV to the EEPROM, converting the value (a string) to
604 * the format in which it will be stored in the EEPROM.
606 #define MAX_TLV_VALUE_LEN 256
607 static bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval)
609 struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
610 struct tlvinfo_tlv *eeprom_tlv;
613 char data[MAX_TLV_VALUE_LEN];
616 // Encode each TLV type into the format to be stored in the EERPOM
618 case TLV_CODE_PRODUCT_NAME:
619 case TLV_CODE_PART_NUMBER:
620 case TLV_CODE_SERIAL_NUMBER:
621 case TLV_CODE_LABEL_REVISION:
622 case TLV_CODE_PLATFORM_NAME:
623 case TLV_CODE_ONIE_VERSION:
624 case TLV_CODE_MANUF_NAME:
625 case TLV_CODE_MANUF_COUNTRY:
626 case TLV_CODE_VENDOR_NAME:
627 case TLV_CODE_DIAG_VERSION:
628 case TLV_CODE_SERVICE_TAG:
629 strncpy(data, strval, MAX_TLV_VALUE_LEN);
630 new_tlv_len = min_t(size_t, MAX_TLV_VALUE_LEN, strlen(strval));
632 case TLV_CODE_DEVICE_VERSION:
633 value = simple_strtoul(strval, NULL, 0);
635 printf("ERROR: Device version must be 255 or less. Value supplied: %u",
639 data[0] = value & 0xFF;
642 case TLV_CODE_MAC_SIZE:
643 value = simple_strtoul(strval, NULL, 0);
644 if (value >= 65536) {
645 printf("ERROR: MAC Size must be 65535 or less. Value supplied: %u",
649 data[0] = (value >> 8) & 0xFF;
650 data[1] = value & 0xFF;
653 case TLV_CODE_MANUF_DATE:
654 if (set_date(data, strval) != 0)
658 case TLV_CODE_MAC_BASE:
659 if (set_mac(data, strval) != 0)
663 case TLV_CODE_CRC_32:
664 printf("WARNING: The CRC TLV is set automatically and cannot be set manually.\n");
666 case TLV_CODE_VENDOR_EXT:
668 if (set_bytes(data, strval, &new_tlv_len) != 0)
673 // Is there room for this TLV?
674 if ((be16_to_cpu(eeprom_hdr->totallen) + ENT_SIZE + new_tlv_len) >
676 printf("ERROR: There is not enough room in the EERPOM to save data.\n");
680 // Add TLV at the end, overwriting CRC TLV if it exists
681 if (tlvinfo_find_tlv(eeprom, TLV_CODE_CRC_32, &eeprom_index))
682 eeprom_hdr->totallen =
683 cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) -
686 eeprom_index = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen);
687 eeprom_tlv = to_entry(&eeprom[eeprom_index]);
688 eeprom_tlv->type = tcode;
689 eeprom_tlv->length = new_tlv_len;
690 memcpy(eeprom_tlv->value, data, new_tlv_len);
692 // Update the total length and calculate (add) a new CRC-32 TLV
693 eeprom_hdr->totallen = cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) +
694 ENT_SIZE + new_tlv_len);
703 * Converts a string MAC address into a binary buffer.
705 * This function takes a pointer to a MAC address string
706 * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number).
707 * The string format is verified and then converted to binary and
708 * stored in a buffer.
710 static int set_mac(char *buf, const char *string)
712 char *p = (char *)string;
718 printf("ERROR: NULL mac addr string passed in.\n");
722 if (strlen(p) != 17) {
723 printf("ERROR: MAC address strlen() != 17 -- %zu\n", strlen(p));
724 printf("ERROR: Bad MAC address format: %s\n", string);
728 for (i = 0; i < 17; i++) {
732 printf("ERROR: mac: p[%i] != :, found: `%c'\n",
737 } else if (!is_hex(p[i])) {
739 printf("ERROR: mac: p[%i] != hex digit, found: `%c'\n",
746 printf("ERROR: Bad MAC address format: %s\n", string);
750 /* Convert string to binary */
751 for (i = 0, p = (char *)string; i < 6; i++) {
752 buf[i] = p ? simple_strtoul(p, &end, 16) : 0;
754 p = (*end) ? end + 1 : end;
757 if (!is_valid_ethaddr((u8 *)buf)) {
758 printf("ERROR: MAC address must not be 00:00:00:00:00:00, a multicast address or FF:FF:FF:FF:FF:FF.\n");
759 printf("ERROR: Bad MAC address format: %s\n", string);
769 * Validates the format of the data string
771 * This function takes a pointer to a date string (i.e. MM/DD/YYYY hh:mm:ss)
772 * and validates that the format is correct. If so the string is copied
773 * to the supplied buffer.
775 static int set_date(char *buf, const char *string)
780 printf("ERROR: NULL date string passed in.\n");
784 if (strlen(string) != 19) {
785 printf("ERROR: Date strlen() != 19 -- %zu\n", strlen(string));
786 printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n",
791 for (i = 0; string[i] != 0; i++) {
795 if (string[i] != '/') {
796 printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n",
802 if (string[i] != ' ') {
803 printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n",
810 if (string[i] != ':') {
811 printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n",
817 if (!is_digit(string[i])) {
818 printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n",
833 * Converts a space-separated string of decimal numbers into a
836 * This function takes a pointer to a space-separated string of decimal
837 * numbers (i.e. "128 0x55 0321") with "C" standard radix specifiers
838 * and converts them to an array of bytes.
840 static int set_bytes(char *buf, const char *string, int *converted_accum)
842 char *p = (char *)string;
847 printf("ERROR: NULL string passed in.\n");
851 /* Convert string to bytes */
852 for (i = 0, p = (char *)string; (i < TLV_VALUE_MAX_LEN) && (*p != 0);
854 while ((*p == ' ') || (*p == '\t') || (*p == ',') ||
860 printf("ERROR: Non-digit found in byte string: (%s)\n",
864 byte = simple_strtoul(p, &p, 0);
866 printf("ERROR: The value specified is greater than 255: (%u) in string: %s\n",
870 buf[i] = byte & 0xFF;
874 if (i == TLV_VALUE_MAX_LEN && (*p != 0)) {
875 printf("ERROR: Trying to assign too many bytes (max: %d) in string: %s\n",
876 TLV_VALUE_MAX_LEN, string);
880 *converted_accum = i;
884 static void show_tlv_devices(void)
888 for (dev = 0; dev < MAX_TLV_DEVICES; dev++)
889 if (tlv_devices[dev])
890 printf("TLV: %u%s\n", dev,
891 (dev == current_dev) ? " (*)" : "");
894 static int find_tlv_devices(struct udevice **tlv_devices_p)
900 for (ret = uclass_first_device_check(UCLASS_I2C_EEPROM, &dev);
902 ret = uclass_next_device_check(&dev)) {
904 tlv_devices_p[count_dev++] = dev;
905 if (count_dev >= MAX_TLV_DEVICES)
909 return (count_dev == 0) ? -ENODEV : 0;
912 static struct udevice *find_tlv_device_by_index(int dev_num)
914 struct udevice *local_tlv_devices[MAX_TLV_DEVICES] = {};
915 struct udevice **tlv_devices_p;
918 if (gd->flags & (GD_FLG_RELOC | GD_FLG_SPL_INIT)) {
919 /* Assume BSS is initialized; use static data */
920 if (tlv_devices[dev_num])
921 return tlv_devices[dev_num];
922 tlv_devices_p = tlv_devices;
924 tlv_devices_p = local_tlv_devices;
927 ret = find_tlv_devices(tlv_devices_p);
928 if (ret == 0 && tlv_devices_p[dev_num])
929 return tlv_devices_p[dev_num];
935 * read_tlv_eeprom - read the hwinfo from i2c EEPROM
937 int read_tlv_eeprom(void *eeprom, int offset, int len, int dev_num)
941 if (dev_num >= MAX_TLV_DEVICES)
944 dev = find_tlv_device_by_index(dev_num);
948 return i2c_eeprom_read(dev, offset, eeprom, len);
952 * write_tlv_eeprom - write the hwinfo to i2c EEPROM
954 int write_tlv_eeprom(void *eeprom, int len)
956 if (!(gd->flags & GD_FLG_RELOC))
958 if (!tlv_devices[current_dev])
961 return i2c_eeprom_write(tlv_devices[current_dev], 0, eeprom, len);
964 int read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr,
965 struct tlvinfo_tlv **first_entry, int dev_num)
968 struct tlvinfo_header *tlv_hdr;
969 struct tlvinfo_tlv *tlv_ent;
971 /* Read TLV header */
972 ret = read_tlv_eeprom(eeprom, 0, HDR_SIZE, dev_num);
977 if (!is_valid_tlvinfo_header(tlv_hdr))
980 /* Read TLV entries */
981 tlv_ent = to_entry(&tlv_hdr[1]);
982 ret = read_tlv_eeprom(tlv_ent, HDR_SIZE,
983 be16_to_cpu(tlv_hdr->totallen), dev_num);
986 if (!is_checksum_valid(eeprom))
990 *first_entry = tlv_ent;
996 * mac_read_from_eeprom
998 * Read the MAC addresses from EEPROM
1000 * This function reads the MAC addresses from EEPROM and sets the
1001 * appropriate environment variables for each one read.
1003 * The environment variables are only set if they haven't been set already.
1004 * This ensures that any user-saved variables are never overwritten.
1006 * This function must be called after relocation.
1008 int mac_read_from_eeprom(void)
1012 struct tlvinfo_tlv *eeprom_tlv;
1015 struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
1019 if (read_eeprom(eeprom)) {
1020 printf("Read failed.\n");
1025 if (tlvinfo_find_tlv(eeprom, TLV_CODE_MAC_SIZE, &eeprom_index)) {
1026 eeprom_tlv = to_entry(&eeprom[eeprom_index]);
1027 maccount = (eeprom_tlv->value[0] << 8) | eeprom_tlv->value[1];
1030 memcpy(macbase, "\0\0\0\0\0\0", 6);
1031 if (tlvinfo_find_tlv(eeprom, TLV_CODE_MAC_BASE, &eeprom_index)) {
1032 eeprom_tlv = to_entry(&eeprom[eeprom_index]);
1033 memcpy(macbase, eeprom_tlv->value, 6);
1036 for (i = 0; i < maccount; i++) {
1037 if (is_valid_ethaddr(macbase)) {
1041 sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
1042 macbase[0], macbase[1], macbase[2],
1043 macbase[3], macbase[4], macbase[5]);
1044 sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
1045 /* Only initialize environment variables that are blank
1046 * (i.e. have not yet been set)
1048 if (!env_get(enetvar))
1049 env_set(enetvar, ethaddr);
1052 if (macbase[5] == 0) {
1054 if (macbase[4] == 0) {
1056 if (macbase[3] == 0) {
1066 printf("%s v%u len=%u\n", eeprom_hdr->signature, eeprom_hdr->version,
1067 be16_to_cpu(eeprom_hdr->totallen));
1073 * populate_serial_number - read the serial number from EEPROM
1075 * This function reads the serial number from the EEPROM and sets the
1076 * appropriate environment variable.
1078 * The environment variable is only set if it has not been set
1079 * already. This ensures that any user-saved variables are never
1082 * This function must be called after relocation.
1084 int populate_serial_number(void)
1086 char serialstr[257];
1088 struct tlvinfo_tlv *eeprom_tlv;
1090 if (env_get("serial#"))
1093 if (read_eeprom(eeprom)) {
1094 printf("Read failed.\n");
1098 if (tlvinfo_find_tlv(eeprom, TLV_CODE_SERIAL_NUMBER, &eeprom_index)) {
1099 eeprom_tlv = to_entry(&eeprom[eeprom_index]);
1100 memcpy(serialstr, eeprom_tlv->value, eeprom_tlv->length);
1101 serialstr[eeprom_tlv->length] = 0;
1102 env_set("serial#", serialstr);