tlv_eeprom.c 28 KB

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