ivm.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  1. /*
  2. * (C) Copyright 2011
  3. * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
  4. *
  5. * See file CREDITS for list of people who contributed to this
  6. * project.
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  21. * MA 02111-1307 USA
  22. */
  23. #include <common.h>
  24. #include <hush.h>
  25. #include <i2c.h>
  26. #include "common.h"
  27. int ivm_calc_crc(unsigned char *buf, int len)
  28. {
  29. const unsigned short crc_tab[16] = {
  30. 0x0000, 0xCC01, 0xD801, 0x1400,
  31. 0xF001, 0x3C00, 0x2800, 0xE401,
  32. 0xA001, 0x6C00, 0x7800, 0xB401,
  33. 0x5000, 0x9C01, 0x8801, 0x4400};
  34. unsigned short crc = 0; /* final result */
  35. unsigned short r1 = 0; /* temp */
  36. unsigned char byte = 0; /* input buffer */
  37. int i;
  38. /* calculate CRC from array data */
  39. for (i = 0; i < len; i++) {
  40. byte = buf[i];
  41. /* lower 4 bits */
  42. r1 = crc_tab[crc & 0xF];
  43. crc = ((crc) >> 4) & 0x0FFF;
  44. crc = crc ^ r1 ^ crc_tab[byte & 0xF];
  45. /* upper 4 bits */
  46. r1 = crc_tab[crc & 0xF];
  47. crc = (crc >> 4) & 0x0FFF;
  48. crc = crc ^ r1 ^ crc_tab[(byte >> 4) & 0xF];
  49. }
  50. return crc;
  51. }
  52. static int ivm_set_value(char *name, char *value)
  53. {
  54. char tempbuf[256];
  55. if (value != NULL) {
  56. sprintf(tempbuf, "%s=%s", name, value);
  57. return set_local_var(tempbuf, 0);
  58. } else {
  59. unset_local_var(name);
  60. }
  61. return 0;
  62. }
  63. static int ivm_get_value(unsigned char *buf, int len, char *name, int off,
  64. int check)
  65. {
  66. unsigned short val;
  67. unsigned char valbuf[30];
  68. if ((buf[off + 0] != buf[off + 2]) &&
  69. (buf[off + 2] != buf[off + 4])) {
  70. printf("%s Error corrupted %s\n", __func__, name);
  71. val = -1;
  72. } else {
  73. val = buf[off + 0] + (buf[off + 1] << 8);
  74. if ((val == 0) && (check == 1))
  75. val = -1;
  76. }
  77. sprintf((char *)valbuf, "%x", val);
  78. ivm_set_value(name, (char *)valbuf);
  79. return val;
  80. }
  81. #define INV_BLOCKSIZE 0x100
  82. #define INV_DATAADDRESS 0x21
  83. #define INVENTORYDATASIZE (INV_BLOCKSIZE - INV_DATAADDRESS - 3)
  84. #define IVM_POS_SHORT_TEXT 0
  85. #define IVM_POS_MANU_ID 1
  86. #define IVM_POS_MANU_SERIAL 2
  87. #define IVM_POS_PART_NUMBER 3
  88. #define IVM_POS_BUILD_STATE 4
  89. #define IVM_POS_SUPPLIER_PART_NUMBER 5
  90. #define IVM_POS_DELIVERY_DATE 6
  91. #define IVM_POS_SUPPLIER_BUILD_STATE 7
  92. #define IVM_POS_CUSTOMER_ID 8
  93. #define IVM_POS_CUSTOMER_PROD_ID 9
  94. #define IVM_POS_HISTORY 10
  95. #define IVM_POS_SYMBOL_ONLY 11
  96. static char convert_char(char c)
  97. {
  98. return (c < ' ' || c > '~') ? '.' : c;
  99. }
  100. static int ivm_findinventorystring(int type,
  101. unsigned char *const string,
  102. unsigned long maxlen,
  103. unsigned char *buf)
  104. {
  105. int xcode = 0;
  106. unsigned long cr = 0;
  107. unsigned long addr = INV_DATAADDRESS;
  108. unsigned long size = 0;
  109. unsigned long nr = type;
  110. int stop = 0; /* stop on semicolon */
  111. memset(string, '\0', maxlen);
  112. switch (type) {
  113. case IVM_POS_SYMBOL_ONLY:
  114. nr = 0;
  115. stop = 1;
  116. break;
  117. default:
  118. nr = type;
  119. stop = 0;
  120. }
  121. /* Look for the requested number of CR. */
  122. while ((cr != nr) && (addr < INVENTORYDATASIZE)) {
  123. if ((buf[addr] == '\r'))
  124. cr++;
  125. addr++;
  126. }
  127. /*
  128. * the expected number of CR was found until the end of the IVM
  129. * content --> fill string
  130. */
  131. if (addr < INVENTORYDATASIZE) {
  132. /* Copy the IVM string in the corresponding string */
  133. for (; (buf[addr] != '\r') &&
  134. ((buf[addr] != ';') || (!stop)) &&
  135. (size < (maxlen - 1) &&
  136. (addr < INVENTORYDATASIZE)); addr++) {
  137. size += sprintf((char *)string + size, "%c",
  138. convert_char (buf[addr]));
  139. }
  140. /*
  141. * copy phase is done: check if everything is ok. If not,
  142. * the inventory data is most probably corrupted: tell
  143. * the world there is a problem!
  144. */
  145. if (addr == INVENTORYDATASIZE) {
  146. xcode = -1;
  147. printf("Error end of string not found\n");
  148. } else if ((size >= (maxlen - 1)) &&
  149. (buf[addr] != '\r')) {
  150. xcode = -1;
  151. printf("string too long till next CR\n");
  152. }
  153. } else {
  154. /*
  155. * some CR are missing...
  156. * the inventory data is most probably corrupted
  157. */
  158. xcode = -1;
  159. printf("not enough cr found\n");
  160. }
  161. return xcode;
  162. }
  163. #define GET_STRING(name, which, len) \
  164. if (ivm_findinventorystring(which, valbuf, len, buf) == 0) { \
  165. ivm_set_value(name, (char *)valbuf); \
  166. }
  167. static int ivm_check_crc(unsigned char *buf, int block)
  168. {
  169. unsigned long crc;
  170. unsigned long crceeprom;
  171. crc = ivm_calc_crc(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2);
  172. crceeprom = (buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 1] + \
  173. buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2] * 256);
  174. if (crc != crceeprom) {
  175. if (block == 0)
  176. printf("Error CRC Block: %d EEprom: calculated: \
  177. %lx EEprom: %lx\n", block, crc, crceeprom);
  178. return -1;
  179. }
  180. return 0;
  181. }
  182. static int ivm_analyze_block2(unsigned char *buf, int len)
  183. {
  184. unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
  185. unsigned long count;
  186. /* IVM_MAC Adress begins at offset 1 */
  187. sprintf((char *)valbuf, "%pM", buf + 1);
  188. ivm_set_value("IVM_MacAddress", (char *)valbuf);
  189. /* if an offset is defined, add it */
  190. #if defined(CONFIG_PIGGY_MAC_ADRESS_OFFSET)
  191. if (CONFIG_PIGGY_MAC_ADRESS_OFFSET > 0) {
  192. unsigned long val = (buf[4] << 16) + (buf[5] << 8) + buf[6];
  193. val += CONFIG_PIGGY_MAC_ADRESS_OFFSET;
  194. buf[4] = (val >> 16) & 0xff;
  195. buf[5] = (val >> 8) & 0xff;
  196. buf[6] = val & 0xff;
  197. sprintf((char *)valbuf, "%pM", buf);
  198. }
  199. #endif
  200. #ifdef MACH_TYPE_KM_KIRKWOOD
  201. setenv((char *)"ethaddr", (char *)valbuf);
  202. #else
  203. if (getenv("ethaddr") == NULL)
  204. setenv((char *)"ethaddr", (char *)valbuf);
  205. #endif
  206. /* IVM_MacCount */
  207. count = (buf[10] << 24) +
  208. (buf[11] << 16) +
  209. (buf[12] << 8) +
  210. buf[13];
  211. if (count == 0xffffffff)
  212. count = 1;
  213. sprintf((char *)valbuf, "%lx", count);
  214. ivm_set_value("IVM_MacCount", (char *)valbuf);
  215. return 0;
  216. }
  217. int ivm_analyze_eeprom(unsigned char *buf, int len)
  218. {
  219. unsigned short val;
  220. unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
  221. unsigned char *tmp;
  222. if (ivm_check_crc(buf, 0) != 0)
  223. return -1;
  224. ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  225. "IVM_BoardId", 0, 1);
  226. val = ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  227. "IVM_HWKey", 6, 1);
  228. if (val != 0xffff) {
  229. sprintf((char *)valbuf, "%x", ((val / 100) % 10));
  230. ivm_set_value("IVM_HWVariant", (char *)valbuf);
  231. sprintf((char *)valbuf, "%x", (val % 100));
  232. ivm_set_value("IVM_HWVersion", (char *)valbuf);
  233. }
  234. ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
  235. "IVM_Functions", 12, 0);
  236. GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8)
  237. GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64)
  238. tmp = (unsigned char *) getenv("IVM_DeviceName");
  239. if (tmp) {
  240. int len = strlen((char *)tmp);
  241. int i = 0;
  242. while (i < len) {
  243. if (tmp[i] == ';') {
  244. ivm_set_value("IVM_ShortText",
  245. (char *)&tmp[i + 1]);
  246. break;
  247. }
  248. i++;
  249. }
  250. if (i >= len)
  251. ivm_set_value("IVM_ShortText", NULL);
  252. } else {
  253. ivm_set_value("IVM_ShortText", NULL);
  254. }
  255. GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32)
  256. GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20)
  257. GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32)
  258. GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32)
  259. GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32)
  260. GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32)
  261. GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32)
  262. GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32)
  263. GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32)
  264. if (ivm_check_crc(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0)
  265. return 0;
  266. ivm_analyze_block2(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2],
  267. CONFIG_SYS_IVM_EEPROM_PAGE_LEN);
  268. return 0;
  269. }
  270. int ivm_read_eeprom(void)
  271. {
  272. #if defined(CONFIG_I2C_MUX)
  273. I2C_MUX_DEVICE *dev = NULL;
  274. #endif
  275. uchar i2c_buffer[CONFIG_SYS_IVM_EEPROM_MAX_LEN];
  276. uchar *buf;
  277. unsigned long dev_addr = CONFIG_SYS_IVM_EEPROM_ADR;
  278. int ret;
  279. #if defined(CONFIG_I2C_MUX)
  280. /* First init the Bus, select the Bus */
  281. #if defined(CONFIG_SYS_I2C_IVM_BUS)
  282. dev = i2c_mux_ident_muxstring((uchar *)CONFIG_SYS_I2C_IVM_BUS);
  283. #else
  284. buf = (unsigned char *) getenv("EEprom_ivm");
  285. if (buf != NULL)
  286. dev = i2c_mux_ident_muxstring(buf);
  287. #endif
  288. if (dev == NULL) {
  289. printf("Error couldnt add Bus for IVM\n");
  290. return -1;
  291. }
  292. i2c_set_bus_num(dev->busid);
  293. #endif
  294. buf = (unsigned char *) getenv("EEprom_ivm_addr");
  295. if (buf != NULL) {
  296. ret = strict_strtoul((char *)buf, 16, &dev_addr);
  297. if (ret != 0)
  298. return -3;
  299. }
  300. /* add deblocking here */
  301. i2c_make_abort();
  302. ret = i2c_read(dev_addr, 0, 1, i2c_buffer,
  303. CONFIG_SYS_IVM_EEPROM_MAX_LEN);
  304. if (ret != 0) {
  305. printf("Error reading EEprom\n");
  306. return -2;
  307. }
  308. return ivm_analyze_eeprom(i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN);
  309. }