tlv_eeprom.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * See file CREDITS for list of people who contributed to this
  4. * project.
  5. *
  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>
  10. */
  11. #include <common.h>
  12. #include <command.h>
  13. #include <dm.h>
  14. #include <i2c.h>
  15. #include <i2c_eeprom.h>
  16. #include <env.h>
  17. #include <init.h>
  18. #include <net.h>
  19. #include <asm/global_data.h>
  20. #include <linux/ctype.h>
  21. #include <u-boot/crc.h>
  22. #include "tlv_eeprom.h"
  23. DECLARE_GLOBAL_DATA_PTR;
  24. #define MAX_TLV_DEVICES 2
  25. /* File scope function prototypes */
  26. static bool is_checksum_valid(u8 *eeprom);
  27. static int read_eeprom(u8 *eeprom);
  28. static void show_eeprom(u8 *eeprom);
  29. static void decode_tlv(struct tlvinfo_tlv *tlv);
  30. static void update_crc(u8 *eeprom);
  31. static int prog_eeprom(u8 *eeprom);
  32. static bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index);
  33. static bool tlvinfo_delete_tlv(u8 *eeprom, u8 code);
  34. static bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval);
  35. static int set_mac(char *buf, const char *string);
  36. static int set_date(char *buf, const char *string);
  37. static int set_bytes(char *buf, const char *string, int *converted_accum);
  38. static void show_tlv_devices(void);
  39. /* Set to 1 if we've read EEPROM into memory */
  40. static int has_been_read;
  41. /* The EERPOM contents after being read into memory */
  42. static u8 eeprom[TLV_INFO_MAX_LEN];
  43. static struct udevice *tlv_devices[MAX_TLV_DEVICES];
  44. static unsigned int current_dev;
  45. #define to_header(p) ((struct tlvinfo_header *)p)
  46. #define to_entry(p) ((struct tlvinfo_tlv *)p)
  47. #define HDR_SIZE sizeof(struct tlvinfo_header)
  48. #define ENT_SIZE sizeof(struct tlvinfo_tlv)
  49. static inline bool is_digit(char c)
  50. {
  51. return (c >= '0' && c <= '9');
  52. }
  53. /**
  54. * is_valid_tlv
  55. *
  56. * Perform basic sanity checks on a TLV field. The TLV is pointed to
  57. * by the parameter provided.
  58. * 1. The type code is not reserved (0x00 or 0xFF)
  59. */
  60. static inline bool is_valid_tlv(struct tlvinfo_tlv *tlv)
  61. {
  62. return((tlv->type != 0x00) && (tlv->type != 0xFF));
  63. }
  64. /**
  65. * is_hex
  66. *
  67. * Tests if character is an ASCII hex digit
  68. */
  69. static inline u8 is_hex(char p)
  70. {
  71. return (((p >= '0') && (p <= '9')) ||
  72. ((p >= 'A') && (p <= 'F')) ||
  73. ((p >= 'a') && (p <= 'f')));
  74. }
  75. /**
  76. * is_checksum_valid
  77. *
  78. * Validate the checksum in the provided TlvInfo EEPROM data. First,
  79. * verify that the TlvInfo header is valid, then make sure the last
  80. * TLV is a CRC-32 TLV. Then calculate the CRC over the EEPROM data
  81. * and compare it to the value stored in the EEPROM CRC-32 TLV.
  82. */
  83. static bool is_checksum_valid(u8 *eeprom)
  84. {
  85. struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
  86. struct tlvinfo_tlv *eeprom_crc;
  87. unsigned int calc_crc;
  88. unsigned int stored_crc;
  89. // Is the eeprom header valid?
  90. if (!is_valid_tlvinfo_header(eeprom_hdr))
  91. return false;
  92. // Is the last TLV a CRC?
  93. eeprom_crc = to_entry(&eeprom[HDR_SIZE +
  94. be16_to_cpu(eeprom_hdr->totallen) - (ENT_SIZE + 4)]);
  95. if (eeprom_crc->type != TLV_CODE_CRC_32 || eeprom_crc->length != 4)
  96. return false;
  97. // Calculate the checksum
  98. calc_crc = crc32(0, (void *)eeprom,
  99. HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen) - 4);
  100. stored_crc = (eeprom_crc->value[0] << 24) |
  101. (eeprom_crc->value[1] << 16) |
  102. (eeprom_crc->value[2] << 8) |
  103. eeprom_crc->value[3];
  104. return calc_crc == stored_crc;
  105. }
  106. /**
  107. * read_eeprom
  108. *
  109. * Read the EEPROM into memory, if it hasn't already been read.
  110. */
  111. static int read_eeprom(u8 *eeprom)
  112. {
  113. int ret;
  114. struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
  115. struct tlvinfo_tlv *eeprom_tlv = to_entry(&eeprom[HDR_SIZE]);
  116. if (has_been_read)
  117. return 0;
  118. /* Read the header */
  119. ret = read_tlv_eeprom((void *)eeprom_hdr, 0, HDR_SIZE, current_dev);
  120. /* If the header was successfully read, read the TLVs */
  121. if (ret == 0 && is_valid_tlvinfo_header(eeprom_hdr))
  122. ret = read_tlv_eeprom((void *)eeprom_tlv, HDR_SIZE,
  123. be16_to_cpu(eeprom_hdr->totallen),
  124. current_dev);
  125. // If the contents are invalid, start over with default contents
  126. if (!is_valid_tlvinfo_header(eeprom_hdr) ||
  127. !is_checksum_valid(eeprom)) {
  128. strcpy(eeprom_hdr->signature, TLV_INFO_ID_STRING);
  129. eeprom_hdr->version = TLV_INFO_VERSION;
  130. eeprom_hdr->totallen = cpu_to_be16(0);
  131. update_crc(eeprom);
  132. }
  133. has_been_read = 1;
  134. #ifdef DEBUG
  135. show_eeprom(eeprom);
  136. #endif
  137. return ret;
  138. }
  139. /**
  140. * show_eeprom
  141. *
  142. * Display the contents of the EEPROM
  143. */
  144. static void show_eeprom(u8 *eeprom)
  145. {
  146. int tlv_end;
  147. int curr_tlv;
  148. struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
  149. struct tlvinfo_tlv *eeprom_tlv;
  150. if (!is_valid_tlvinfo_header(eeprom_hdr)) {
  151. printf("EEPROM does not contain data in a valid TlvInfo format.\n");
  152. return;
  153. }
  154. printf("TLV: %u\n", current_dev);
  155. printf("TlvInfo Header:\n");
  156. printf(" Id String: %s\n", eeprom_hdr->signature);
  157. printf(" Version: %d\n", eeprom_hdr->version);
  158. printf(" Total Length: %d\n", be16_to_cpu(eeprom_hdr->totallen));
  159. printf("TLV Name Code Len Value\n");
  160. printf("-------------------- ---- --- -----\n");
  161. curr_tlv = HDR_SIZE;
  162. tlv_end = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen);
  163. while (curr_tlv < tlv_end) {
  164. eeprom_tlv = to_entry(&eeprom[curr_tlv]);
  165. if (!is_valid_tlv(eeprom_tlv)) {
  166. printf("Invalid TLV field starting at EEPROM offset %d\n",
  167. curr_tlv);
  168. return;
  169. }
  170. decode_tlv(eeprom_tlv);
  171. curr_tlv += ENT_SIZE + eeprom_tlv->length;
  172. }
  173. printf("Checksum is %s.\n",
  174. is_checksum_valid(eeprom) ? "valid" : "invalid");
  175. #ifdef DEBUG
  176. printf("EEPROM dump: (0x%x bytes)", TLV_INFO_MAX_LEN);
  177. for (i = 0; i < TLV_INFO_MAX_LEN; i++) {
  178. if ((i % 16) == 0)
  179. printf("\n%02X: ", i);
  180. printf("%02X ", eeprom[i]);
  181. }
  182. printf("\n");
  183. #endif
  184. }
  185. /**
  186. * Struct for displaying the TLV codes and names.
  187. */
  188. struct tlv_code_desc {
  189. u8 m_code;
  190. char *m_name;
  191. };
  192. /**
  193. * List of TLV codes and names.
  194. */
  195. static struct tlv_code_desc tlv_code_list[] = {
  196. { TLV_CODE_PRODUCT_NAME, "Product Name"},
  197. { TLV_CODE_PART_NUMBER, "Part Number"},
  198. { TLV_CODE_SERIAL_NUMBER, "Serial Number"},
  199. { TLV_CODE_MAC_BASE, "Base MAC Address"},
  200. { TLV_CODE_MANUF_DATE, "Manufacture Date"},
  201. { TLV_CODE_DEVICE_VERSION, "Device Version"},
  202. { TLV_CODE_LABEL_REVISION, "Label Revision"},
  203. { TLV_CODE_PLATFORM_NAME, "Platform Name"},
  204. { TLV_CODE_ONIE_VERSION, "ONIE Version"},
  205. { TLV_CODE_MAC_SIZE, "MAC Addresses"},
  206. { TLV_CODE_MANUF_NAME, "Manufacturer"},
  207. { TLV_CODE_MANUF_COUNTRY, "Country Code"},
  208. { TLV_CODE_VENDOR_NAME, "Vendor Name"},
  209. { TLV_CODE_DIAG_VERSION, "Diag Version"},
  210. { TLV_CODE_SERVICE_TAG, "Service Tag"},
  211. { TLV_CODE_VENDOR_EXT, "Vendor Extension"},
  212. { TLV_CODE_CRC_32, "CRC-32"},
  213. };
  214. /**
  215. * Look up a TLV name by its type.
  216. */
  217. static inline const char *tlv_type2name(u8 type)
  218. {
  219. char *name = "Unknown";
  220. int i;
  221. for (i = 0; i < ARRAY_SIZE(tlv_code_list); i++) {
  222. if (tlv_code_list[i].m_code == type) {
  223. name = tlv_code_list[i].m_name;
  224. break;
  225. }
  226. }
  227. return name;
  228. }
  229. /*
  230. * decode_tlv
  231. *
  232. * Print a string representing the contents of the TLV field. The format of
  233. * the string is:
  234. * 1. The name of the field left justified in 20 characters
  235. * 2. The type code in hex right justified in 5 characters
  236. * 3. The length in decimal right justified in 4 characters
  237. * 4. The value, left justified in however many characters it takes
  238. * The validity of EEPROM contents and the TLV field have been verified
  239. * prior to calling this function.
  240. */
  241. #define DECODE_NAME_MAX 20
  242. /*
  243. * The max decode value is currently for the 'raw' type or the 'vendor
  244. * extension' type, both of which have the same decode format. The
  245. * max decode string size is computed as follows:
  246. *
  247. * strlen(" 0xFF") * TLV_VALUE_MAX_LEN + 1
  248. *
  249. */
  250. #define DECODE_VALUE_MAX ((5 * TLV_VALUE_MAX_LEN) + 1)
  251. static void decode_tlv(struct tlvinfo_tlv *tlv)
  252. {
  253. char name[DECODE_NAME_MAX];
  254. char value[DECODE_VALUE_MAX];
  255. int i;
  256. strncpy(name, tlv_type2name(tlv->type), DECODE_NAME_MAX);
  257. switch (tlv->type) {
  258. case TLV_CODE_PRODUCT_NAME:
  259. case TLV_CODE_PART_NUMBER:
  260. case TLV_CODE_SERIAL_NUMBER:
  261. case TLV_CODE_MANUF_DATE:
  262. case TLV_CODE_LABEL_REVISION:
  263. case TLV_CODE_PLATFORM_NAME:
  264. case TLV_CODE_ONIE_VERSION:
  265. case TLV_CODE_MANUF_NAME:
  266. case TLV_CODE_MANUF_COUNTRY:
  267. case TLV_CODE_VENDOR_NAME:
  268. case TLV_CODE_DIAG_VERSION:
  269. case TLV_CODE_SERVICE_TAG:
  270. memcpy(value, tlv->value, tlv->length);
  271. value[tlv->length] = 0;
  272. break;
  273. case TLV_CODE_MAC_BASE:
  274. sprintf(value, "%02X:%02X:%02X:%02X:%02X:%02X",
  275. tlv->value[0], tlv->value[1], tlv->value[2],
  276. tlv->value[3], tlv->value[4], tlv->value[5]);
  277. break;
  278. case TLV_CODE_DEVICE_VERSION:
  279. sprintf(value, "%u", tlv->value[0]);
  280. break;
  281. case TLV_CODE_MAC_SIZE:
  282. sprintf(value, "%u", (tlv->value[0] << 8) | tlv->value[1]);
  283. break;
  284. case TLV_CODE_VENDOR_EXT:
  285. value[0] = 0;
  286. for (i = 0; (i < (DECODE_VALUE_MAX / 5)) && (i < tlv->length);
  287. i++) {
  288. sprintf(value, "%s 0x%02X", value, tlv->value[i]);
  289. }
  290. break;
  291. case TLV_CODE_CRC_32:
  292. sprintf(value, "0x%02X%02X%02X%02X",
  293. tlv->value[0], tlv->value[1],
  294. tlv->value[2], tlv->value[3]);
  295. break;
  296. default:
  297. value[0] = 0;
  298. for (i = 0; (i < (DECODE_VALUE_MAX / 5)) && (i < tlv->length);
  299. i++) {
  300. sprintf(value, "%s 0x%02X", value, tlv->value[i]);
  301. }
  302. break;
  303. }
  304. name[DECODE_NAME_MAX - 1] = 0;
  305. printf("%-20s 0x%02X %3d %s\n", name, tlv->type, tlv->length, value);
  306. }
  307. /**
  308. * update_crc
  309. *
  310. * This function updates the CRC-32 TLV. If there is no CRC-32 TLV, then
  311. * one is added. This function should be called after each update to the
  312. * EEPROM structure, to make sure the CRC is always correct.
  313. */
  314. static void update_crc(u8 *eeprom)
  315. {
  316. struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
  317. struct tlvinfo_tlv *eeprom_crc;
  318. unsigned int calc_crc;
  319. int eeprom_index;
  320. // Discover the CRC TLV
  321. if (!tlvinfo_find_tlv(eeprom, TLV_CODE_CRC_32, &eeprom_index)) {
  322. unsigned int totallen = be16_to_cpu(eeprom_hdr->totallen);
  323. if ((totallen + ENT_SIZE + 4) > TLV_TOTAL_LEN_MAX)
  324. return;
  325. eeprom_index = HDR_SIZE + totallen;
  326. eeprom_hdr->totallen = cpu_to_be16(totallen + ENT_SIZE + 4);
  327. }
  328. eeprom_crc = to_entry(&eeprom[eeprom_index]);
  329. eeprom_crc->type = TLV_CODE_CRC_32;
  330. eeprom_crc->length = 4;
  331. // Calculate the checksum
  332. calc_crc = crc32(0, (void *)eeprom,
  333. HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen) - 4);
  334. eeprom_crc->value[0] = (calc_crc >> 24) & 0xFF;
  335. eeprom_crc->value[1] = (calc_crc >> 16) & 0xFF;
  336. eeprom_crc->value[2] = (calc_crc >> 8) & 0xFF;
  337. eeprom_crc->value[3] = (calc_crc >> 0) & 0xFF;
  338. }
  339. /**
  340. * prog_eeprom
  341. *
  342. * Write the EEPROM data from CPU memory to the hardware.
  343. */
  344. static int prog_eeprom(u8 *eeprom)
  345. {
  346. int ret = 0;
  347. struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
  348. int eeprom_len;
  349. update_crc(eeprom);
  350. eeprom_len = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen);
  351. ret = write_tlv_eeprom(eeprom, eeprom_len);
  352. if (ret) {
  353. printf("Programming failed.\n");
  354. return -1;
  355. }
  356. printf("Programming passed.\n");
  357. return 0;
  358. }
  359. /**
  360. * show_tlv_code_list - Display the list of TLV codes and names
  361. */
  362. void show_tlv_code_list(void)
  363. {
  364. int i;
  365. printf("TLV Code TLV Name\n");
  366. printf("======== =================\n");
  367. for (i = 0; i < ARRAY_SIZE(tlv_code_list); i++) {
  368. printf("0x%02X %s\n",
  369. tlv_code_list[i].m_code,
  370. tlv_code_list[i].m_name);
  371. }
  372. }
  373. /**
  374. * do_tlv_eeprom
  375. *
  376. * This function implements the tlv_eeprom command.
  377. */
  378. int do_tlv_eeprom(struct cmd_tbl *cmdtp, int flag, int argc, char *const argv[])
  379. {
  380. char cmd;
  381. struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
  382. // If no arguments, read the EERPOM and display its contents
  383. if (argc == 1) {
  384. read_eeprom(eeprom);
  385. show_eeprom(eeprom);
  386. return 0;
  387. }
  388. // We only look at the first character to the command, so "read" and
  389. // "reset" will both be treated as "read".
  390. cmd = argv[1][0];
  391. // Read the EEPROM contents
  392. if (cmd == 'r') {
  393. has_been_read = 0;
  394. if (!read_eeprom(eeprom))
  395. printf("EEPROM data loaded from device to memory.\n");
  396. return 0;
  397. }
  398. // Subsequent commands require that the EEPROM has already been read.
  399. if (!has_been_read) {
  400. printf("Please read the EEPROM data first, using the 'tlv_eeprom read' command.\n");
  401. return 0;
  402. }
  403. // Handle the commands that don't take parameters
  404. if (argc == 2) {
  405. switch (cmd) {
  406. case 'w': /* write */
  407. prog_eeprom(eeprom);
  408. break;
  409. case 'e': /* erase */
  410. strcpy(eeprom_hdr->signature, TLV_INFO_ID_STRING);
  411. eeprom_hdr->version = TLV_INFO_VERSION;
  412. eeprom_hdr->totallen = cpu_to_be16(0);
  413. update_crc(eeprom);
  414. printf("EEPROM data in memory reset.\n");
  415. break;
  416. case 'l': /* list */
  417. show_tlv_code_list();
  418. break;
  419. case 'd': /* dev */
  420. show_tlv_devices();
  421. break;
  422. default:
  423. cmd_usage(cmdtp);
  424. break;
  425. }
  426. return 0;
  427. }
  428. // The set command takes one or two args.
  429. if (argc > 4) {
  430. cmd_usage(cmdtp);
  431. return 0;
  432. }
  433. // Set command. If the TLV exists in the EEPROM, delete it. Then if
  434. // data was supplied for this TLV add the TLV with the new contents at
  435. // the end.
  436. if (cmd == 's') {
  437. int tcode;
  438. tcode = simple_strtoul(argv[2], NULL, 0);
  439. tlvinfo_delete_tlv(eeprom, tcode);
  440. if (argc == 4)
  441. tlvinfo_add_tlv(eeprom, tcode, argv[3]);
  442. } else if (cmd == 'd') { /* 'dev' command */
  443. unsigned int devnum;
  444. devnum = simple_strtoul(argv[2], NULL, 0);
  445. if (devnum > MAX_TLV_DEVICES || !tlv_devices[devnum]) {
  446. printf("Invalid device number\n");
  447. return 0;
  448. }
  449. current_dev = devnum;
  450. has_been_read = 0;
  451. } else {
  452. cmd_usage(cmdtp);
  453. }
  454. return 0;
  455. }
  456. /**
  457. * This macro defines the tlv_eeprom command line command.
  458. */
  459. U_BOOT_CMD(tlv_eeprom, 4, 1, do_tlv_eeprom,
  460. "Display and program the system EEPROM data block.",
  461. "[read|write|set <type_code> <string_value>|erase|list]\n"
  462. "tlv_eeprom\n"
  463. " - With no arguments display the current contents.\n"
  464. "tlv_eeprom dev [dev]\n"
  465. " - List devices or set current EEPROM device.\n"
  466. "tlv_eeprom read\n"
  467. " - Load EEPROM data from device to memory.\n"
  468. "tlv_eeprom write\n"
  469. " - Write the EEPROM data to persistent storage.\n"
  470. "tlv_eeprom set <type_code> <string_value>\n"
  471. " - Set a field to a value.\n"
  472. " - If no string_value, field is deleted.\n"
  473. " - Use 'tlv_eeprom write' to make changes permanent.\n"
  474. "tlv_eeprom erase\n"
  475. " - Reset the in memory EEPROM data.\n"
  476. " - Use 'tlv_eeprom read' to refresh the in memory EEPROM data.\n"
  477. " - Use 'tlv_eeprom write' to make changes permanent.\n"
  478. "tlv_eeprom list\n"
  479. " - List the understood TLV codes and names.\n"
  480. );
  481. /**
  482. * tlvinfo_find_tlv
  483. *
  484. * This function finds the TLV with the supplied code in the EERPOM.
  485. * An offset from the beginning of the EEPROM is returned in the
  486. * eeprom_index parameter if the TLV is found.
  487. */
  488. static bool tlvinfo_find_tlv(u8 *eeprom, u8 tcode, int *eeprom_index)
  489. {
  490. struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
  491. struct tlvinfo_tlv *eeprom_tlv;
  492. int eeprom_end;
  493. // Search through the TLVs, looking for the first one which matches the
  494. // supplied type code.
  495. *eeprom_index = HDR_SIZE;
  496. eeprom_end = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen);
  497. while (*eeprom_index < eeprom_end) {
  498. eeprom_tlv = to_entry(&eeprom[*eeprom_index]);
  499. if (!is_valid_tlv(eeprom_tlv))
  500. return false;
  501. if (eeprom_tlv->type == tcode)
  502. return true;
  503. *eeprom_index += ENT_SIZE + eeprom_tlv->length;
  504. }
  505. return(false);
  506. }
  507. /**
  508. * tlvinfo_delete_tlv
  509. *
  510. * This function deletes the TLV with the specified type code from the
  511. * EEPROM.
  512. */
  513. static bool tlvinfo_delete_tlv(u8 *eeprom, u8 code)
  514. {
  515. int eeprom_index;
  516. int tlength;
  517. struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
  518. struct tlvinfo_tlv *eeprom_tlv;
  519. // Find the TLV and then move all following TLVs "forward"
  520. if (tlvinfo_find_tlv(eeprom, code, &eeprom_index)) {
  521. eeprom_tlv = to_entry(&eeprom[eeprom_index]);
  522. tlength = ENT_SIZE + eeprom_tlv->length;
  523. memcpy(&eeprom[eeprom_index], &eeprom[eeprom_index + tlength],
  524. HDR_SIZE +
  525. be16_to_cpu(eeprom_hdr->totallen) - eeprom_index -
  526. tlength);
  527. eeprom_hdr->totallen =
  528. cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) -
  529. tlength);
  530. update_crc(eeprom);
  531. return true;
  532. }
  533. return false;
  534. }
  535. /**
  536. * tlvinfo_add_tlv
  537. *
  538. * This function adds a TLV to the EEPROM, converting the value (a string) to
  539. * the format in which it will be stored in the EEPROM.
  540. */
  541. #define MAX_TLV_VALUE_LEN 256
  542. static bool tlvinfo_add_tlv(u8 *eeprom, int tcode, char *strval)
  543. {
  544. struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
  545. struct tlvinfo_tlv *eeprom_tlv;
  546. int new_tlv_len = 0;
  547. u32 value;
  548. char data[MAX_TLV_VALUE_LEN];
  549. int eeprom_index;
  550. // Encode each TLV type into the format to be stored in the EERPOM
  551. switch (tcode) {
  552. case TLV_CODE_PRODUCT_NAME:
  553. case TLV_CODE_PART_NUMBER:
  554. case TLV_CODE_SERIAL_NUMBER:
  555. case TLV_CODE_LABEL_REVISION:
  556. case TLV_CODE_PLATFORM_NAME:
  557. case TLV_CODE_ONIE_VERSION:
  558. case TLV_CODE_MANUF_NAME:
  559. case TLV_CODE_MANUF_COUNTRY:
  560. case TLV_CODE_VENDOR_NAME:
  561. case TLV_CODE_DIAG_VERSION:
  562. case TLV_CODE_SERVICE_TAG:
  563. strncpy(data, strval, MAX_TLV_VALUE_LEN);
  564. new_tlv_len = min_t(size_t, MAX_TLV_VALUE_LEN, strlen(strval));
  565. break;
  566. case TLV_CODE_DEVICE_VERSION:
  567. value = simple_strtoul(strval, NULL, 0);
  568. if (value >= 256) {
  569. printf("ERROR: Device version must be 255 or less. Value supplied: %u",
  570. value);
  571. return false;
  572. }
  573. data[0] = value & 0xFF;
  574. new_tlv_len = 1;
  575. break;
  576. case TLV_CODE_MAC_SIZE:
  577. value = simple_strtoul(strval, NULL, 0);
  578. if (value >= 65536) {
  579. printf("ERROR: MAC Size must be 65535 or less. Value supplied: %u",
  580. value);
  581. return false;
  582. }
  583. data[0] = (value >> 8) & 0xFF;
  584. data[1] = value & 0xFF;
  585. new_tlv_len = 2;
  586. break;
  587. case TLV_CODE_MANUF_DATE:
  588. if (set_date(data, strval) != 0)
  589. return false;
  590. new_tlv_len = 19;
  591. break;
  592. case TLV_CODE_MAC_BASE:
  593. if (set_mac(data, strval) != 0)
  594. return false;
  595. new_tlv_len = 6;
  596. break;
  597. case TLV_CODE_CRC_32:
  598. printf("WARNING: The CRC TLV is set automatically and cannot be set manually.\n");
  599. return false;
  600. case TLV_CODE_VENDOR_EXT:
  601. default:
  602. if (set_bytes(data, strval, &new_tlv_len) != 0)
  603. return false;
  604. break;
  605. }
  606. // Is there room for this TLV?
  607. if ((be16_to_cpu(eeprom_hdr->totallen) + ENT_SIZE + new_tlv_len) >
  608. TLV_TOTAL_LEN_MAX) {
  609. printf("ERROR: There is not enough room in the EERPOM to save data.\n");
  610. return false;
  611. }
  612. // Add TLV at the end, overwriting CRC TLV if it exists
  613. if (tlvinfo_find_tlv(eeprom, TLV_CODE_CRC_32, &eeprom_index))
  614. eeprom_hdr->totallen =
  615. cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) -
  616. ENT_SIZE - 4);
  617. else
  618. eeprom_index = HDR_SIZE + be16_to_cpu(eeprom_hdr->totallen);
  619. eeprom_tlv = to_entry(&eeprom[eeprom_index]);
  620. eeprom_tlv->type = tcode;
  621. eeprom_tlv->length = new_tlv_len;
  622. memcpy(eeprom_tlv->value, data, new_tlv_len);
  623. // Update the total length and calculate (add) a new CRC-32 TLV
  624. eeprom_hdr->totallen = cpu_to_be16(be16_to_cpu(eeprom_hdr->totallen) +
  625. ENT_SIZE + new_tlv_len);
  626. update_crc(eeprom);
  627. return true;
  628. }
  629. /**
  630. * set_mac
  631. *
  632. * Converts a string MAC address into a binary buffer.
  633. *
  634. * This function takes a pointer to a MAC address string
  635. * (i.e."XX:XX:XX:XX:XX:XX", where "XX" is a two-digit hex number).
  636. * The string format is verified and then converted to binary and
  637. * stored in a buffer.
  638. */
  639. static int set_mac(char *buf, const char *string)
  640. {
  641. char *p = (char *)string;
  642. int i;
  643. int err = 0;
  644. char *end;
  645. if (!p) {
  646. printf("ERROR: NULL mac addr string passed in.\n");
  647. return -1;
  648. }
  649. if (strlen(p) != 17) {
  650. printf("ERROR: MAC address strlen() != 17 -- %zu\n", strlen(p));
  651. printf("ERROR: Bad MAC address format: %s\n", string);
  652. return -1;
  653. }
  654. for (i = 0; i < 17; i++) {
  655. if ((i % 3) == 2) {
  656. if (p[i] != ':') {
  657. err++;
  658. printf("ERROR: mac: p[%i] != :, found: `%c'\n",
  659. i, p[i]);
  660. break;
  661. }
  662. continue;
  663. } else if (!is_hex(p[i])) {
  664. err++;
  665. printf("ERROR: mac: p[%i] != hex digit, found: `%c'\n",
  666. i, p[i]);
  667. break;
  668. }
  669. }
  670. if (err != 0) {
  671. printf("ERROR: Bad MAC address format: %s\n", string);
  672. return -1;
  673. }
  674. /* Convert string to binary */
  675. for (i = 0, p = (char *)string; i < 6; i++) {
  676. buf[i] = p ? hextoul(p, &end) : 0;
  677. if (p)
  678. p = (*end) ? end + 1 : end;
  679. }
  680. if (!is_valid_ethaddr((u8 *)buf)) {
  681. printf("ERROR: MAC address must not be 00:00:00:00:00:00, a multicast address or FF:FF:FF:FF:FF:FF.\n");
  682. printf("ERROR: Bad MAC address format: %s\n", string);
  683. return -1;
  684. }
  685. return 0;
  686. }
  687. /**
  688. * set_date
  689. *
  690. * Validates the format of the data string
  691. *
  692. * This function takes a pointer to a date string (i.e. MM/DD/YYYY hh:mm:ss)
  693. * and validates that the format is correct. If so the string is copied
  694. * to the supplied buffer.
  695. */
  696. static int set_date(char *buf, const char *string)
  697. {
  698. int i;
  699. if (!string) {
  700. printf("ERROR: NULL date string passed in.\n");
  701. return -1;
  702. }
  703. if (strlen(string) != 19) {
  704. printf("ERROR: Date strlen() != 19 -- %zu\n", strlen(string));
  705. printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n",
  706. string);
  707. return -1;
  708. }
  709. for (i = 0; string[i] != 0; i++) {
  710. switch (i) {
  711. case 2:
  712. case 5:
  713. if (string[i] != '/') {
  714. printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n",
  715. string);
  716. return -1;
  717. }
  718. break;
  719. case 10:
  720. if (string[i] != ' ') {
  721. printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n",
  722. string);
  723. return -1;
  724. }
  725. break;
  726. case 13:
  727. case 16:
  728. if (string[i] != ':') {
  729. printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n",
  730. string);
  731. return -1;
  732. }
  733. break;
  734. default:
  735. if (!is_digit(string[i])) {
  736. printf("ERROR: Bad date format (MM/DD/YYYY hh:mm:ss): %s\n",
  737. string);
  738. return -1;
  739. }
  740. break;
  741. }
  742. }
  743. strcpy(buf, string);
  744. return 0;
  745. }
  746. /**
  747. * set_bytes
  748. *
  749. * Converts a space-separated string of decimal numbers into a
  750. * buffer of bytes.
  751. *
  752. * This function takes a pointer to a space-separated string of decimal
  753. * numbers (i.e. "128 0x55 0321") with "C" standard radix specifiers
  754. * and converts them to an array of bytes.
  755. */
  756. static int set_bytes(char *buf, const char *string, int *converted_accum)
  757. {
  758. char *p = (char *)string;
  759. int i;
  760. uint byte;
  761. if (!p) {
  762. printf("ERROR: NULL string passed in.\n");
  763. return -1;
  764. }
  765. /* Convert string to bytes */
  766. for (i = 0, p = (char *)string; (i < TLV_VALUE_MAX_LEN) && (*p != 0);
  767. i++) {
  768. while ((*p == ' ') || (*p == '\t') || (*p == ',') ||
  769. (*p == ';')) {
  770. p++;
  771. }
  772. if (*p != 0) {
  773. if (!is_digit(*p)) {
  774. printf("ERROR: Non-digit found in byte string: (%s)\n",
  775. string);
  776. return -1;
  777. }
  778. byte = simple_strtoul(p, &p, 0);
  779. if (byte >= 256) {
  780. printf("ERROR: The value specified is greater than 255: (%u) in string: %s\n",
  781. byte, string);
  782. return -1;
  783. }
  784. buf[i] = byte & 0xFF;
  785. }
  786. }
  787. if (i == TLV_VALUE_MAX_LEN && (*p != 0)) {
  788. printf("ERROR: Trying to assign too many bytes (max: %d) in string: %s\n",
  789. TLV_VALUE_MAX_LEN, string);
  790. return -1;
  791. }
  792. *converted_accum = i;
  793. return 0;
  794. }
  795. static void show_tlv_devices(void)
  796. {
  797. unsigned int dev;
  798. for (dev = 0; dev < MAX_TLV_DEVICES; dev++)
  799. if (tlv_devices[dev])
  800. printf("TLV: %u%s\n", dev,
  801. (dev == current_dev) ? " (*)" : "");
  802. }
  803. static int find_tlv_devices(struct udevice **tlv_devices_p)
  804. {
  805. int ret;
  806. int count_dev = 0;
  807. struct udevice *dev;
  808. for (ret = uclass_first_device_check(UCLASS_I2C_EEPROM, &dev);
  809. dev;
  810. ret = uclass_next_device_check(&dev)) {
  811. if (ret == 0)
  812. tlv_devices_p[count_dev++] = dev;
  813. if (count_dev >= MAX_TLV_DEVICES)
  814. break;
  815. }
  816. return (count_dev == 0) ? -ENODEV : 0;
  817. }
  818. static struct udevice *find_tlv_device_by_index(int dev_num)
  819. {
  820. struct udevice *local_tlv_devices[MAX_TLV_DEVICES] = {};
  821. struct udevice **tlv_devices_p;
  822. int ret;
  823. if (gd->flags & (GD_FLG_RELOC | GD_FLG_SPL_INIT)) {
  824. /* Assume BSS is initialized; use static data */
  825. if (tlv_devices[dev_num])
  826. return tlv_devices[dev_num];
  827. tlv_devices_p = tlv_devices;
  828. } else {
  829. tlv_devices_p = local_tlv_devices;
  830. }
  831. ret = find_tlv_devices(tlv_devices_p);
  832. if (ret == 0 && tlv_devices_p[dev_num])
  833. return tlv_devices_p[dev_num];
  834. return NULL;
  835. }
  836. /**
  837. * read_tlv_eeprom - read the hwinfo from i2c EEPROM
  838. */
  839. int read_tlv_eeprom(void *eeprom, int offset, int len, int dev_num)
  840. {
  841. struct udevice *dev;
  842. if (dev_num >= MAX_TLV_DEVICES)
  843. return -EINVAL;
  844. dev = find_tlv_device_by_index(dev_num);
  845. if (!dev)
  846. return -ENODEV;
  847. return i2c_eeprom_read(dev, offset, eeprom, len);
  848. }
  849. /**
  850. * write_tlv_eeprom - write the hwinfo to i2c EEPROM
  851. */
  852. int write_tlv_eeprom(void *eeprom, int len)
  853. {
  854. if (!(gd->flags & GD_FLG_RELOC))
  855. return -ENODEV;
  856. if (!tlv_devices[current_dev])
  857. return -ENODEV;
  858. return i2c_eeprom_write(tlv_devices[current_dev], 0, eeprom, len);
  859. }
  860. int read_tlvinfo_tlv_eeprom(void *eeprom, struct tlvinfo_header **hdr,
  861. struct tlvinfo_tlv **first_entry, int dev_num)
  862. {
  863. int ret;
  864. struct tlvinfo_header *tlv_hdr;
  865. struct tlvinfo_tlv *tlv_ent;
  866. /* Read TLV header */
  867. ret = read_tlv_eeprom(eeprom, 0, HDR_SIZE, dev_num);
  868. if (ret < 0)
  869. return ret;
  870. tlv_hdr = eeprom;
  871. if (!is_valid_tlvinfo_header(tlv_hdr))
  872. return -EINVAL;
  873. /* Read TLV entries */
  874. tlv_ent = to_entry(&tlv_hdr[1]);
  875. ret = read_tlv_eeprom(tlv_ent, HDR_SIZE,
  876. be16_to_cpu(tlv_hdr->totallen), dev_num);
  877. if (ret < 0)
  878. return ret;
  879. if (!is_checksum_valid(eeprom))
  880. return -EINVAL;
  881. *hdr = tlv_hdr;
  882. *first_entry = tlv_ent;
  883. return 0;
  884. }
  885. /**
  886. * mac_read_from_eeprom
  887. *
  888. * Read the MAC addresses from EEPROM
  889. *
  890. * This function reads the MAC addresses from EEPROM and sets the
  891. * appropriate environment variables for each one read.
  892. *
  893. * The environment variables are only set if they haven't been set already.
  894. * This ensures that any user-saved variables are never overwritten.
  895. *
  896. * This function must be called after relocation.
  897. */
  898. int mac_read_from_eeprom(void)
  899. {
  900. unsigned int i;
  901. int eeprom_index;
  902. struct tlvinfo_tlv *eeprom_tlv;
  903. int maccount;
  904. u8 macbase[6];
  905. struct tlvinfo_header *eeprom_hdr = to_header(eeprom);
  906. puts("EEPROM: ");
  907. if (read_eeprom(eeprom)) {
  908. printf("Read failed.\n");
  909. return -1;
  910. }
  911. maccount = 1;
  912. if (tlvinfo_find_tlv(eeprom, TLV_CODE_MAC_SIZE, &eeprom_index)) {
  913. eeprom_tlv = to_entry(&eeprom[eeprom_index]);
  914. maccount = (eeprom_tlv->value[0] << 8) | eeprom_tlv->value[1];
  915. }
  916. memcpy(macbase, "\0\0\0\0\0\0", 6);
  917. if (tlvinfo_find_tlv(eeprom, TLV_CODE_MAC_BASE, &eeprom_index)) {
  918. eeprom_tlv = to_entry(&eeprom[eeprom_index]);
  919. memcpy(macbase, eeprom_tlv->value, 6);
  920. }
  921. for (i = 0; i < maccount; i++) {
  922. if (is_valid_ethaddr(macbase)) {
  923. char ethaddr[18];
  924. char enetvar[11];
  925. sprintf(ethaddr, "%02X:%02X:%02X:%02X:%02X:%02X",
  926. macbase[0], macbase[1], macbase[2],
  927. macbase[3], macbase[4], macbase[5]);
  928. sprintf(enetvar, i ? "eth%daddr" : "ethaddr", i);
  929. /* Only initialize environment variables that are blank
  930. * (i.e. have not yet been set)
  931. */
  932. if (!env_get(enetvar))
  933. env_set(enetvar, ethaddr);
  934. macbase[5]++;
  935. if (macbase[5] == 0) {
  936. macbase[4]++;
  937. if (macbase[4] == 0) {
  938. macbase[3]++;
  939. if (macbase[3] == 0) {
  940. macbase[0] = 0;
  941. macbase[1] = 0;
  942. macbase[2] = 0;
  943. }
  944. }
  945. }
  946. }
  947. }
  948. printf("%s v%u len=%u\n", eeprom_hdr->signature, eeprom_hdr->version,
  949. be16_to_cpu(eeprom_hdr->totallen));
  950. return 0;
  951. }
  952. /**
  953. * populate_serial_number - read the serial number from EEPROM
  954. *
  955. * This function reads the serial number from the EEPROM and sets the
  956. * appropriate environment variable.
  957. *
  958. * The environment variable is only set if it has not been set
  959. * already. This ensures that any user-saved variables are never
  960. * overwritten.
  961. *
  962. * This function must be called after relocation.
  963. */
  964. int populate_serial_number(void)
  965. {
  966. char serialstr[257];
  967. int eeprom_index;
  968. struct tlvinfo_tlv *eeprom_tlv;
  969. if (env_get("serial#"))
  970. return 0;
  971. if (read_eeprom(eeprom)) {
  972. printf("Read failed.\n");
  973. return -1;
  974. }
  975. if (tlvinfo_find_tlv(eeprom, TLV_CODE_SERIAL_NUMBER, &eeprom_index)) {
  976. eeprom_tlv = to_entry(&eeprom[eeprom_index]);
  977. memcpy(serialstr, eeprom_tlv->value, eeprom_tlv->length);
  978. serialstr[eeprom_tlv->length] = 0;
  979. env_set("serial#", serialstr);
  980. }
  981. return 0;
  982. }