tlv_eeprom.c 28 KB

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