3 * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
5 * SPDX-License-Identifier: GPL-2.0+
15 static int ivm_calc_crc(unsigned char *buf, int len)
17 const unsigned short crc_tab[16] = {
18 0x0000, 0xCC01, 0xD801, 0x1400,
19 0xF001, 0x3C00, 0x2800, 0xE401,
20 0xA001, 0x6C00, 0x7800, 0xB401,
21 0x5000, 0x9C01, 0x8801, 0x4400};
23 unsigned short crc = 0; /* final result */
24 unsigned short r1 = 0; /* temp */
25 unsigned char byte = 0; /* input buffer */
28 /* calculate CRC from array data */
29 for (i = 0; i < len; i++) {
33 r1 = crc_tab[crc & 0xF];
34 crc = ((crc) >> 4) & 0x0FFF;
35 crc = crc ^ r1 ^ crc_tab[byte & 0xF];
38 r1 = crc_tab[crc & 0xF];
39 crc = (crc >> 4) & 0x0FFF;
40 crc = crc ^ r1 ^ crc_tab[(byte >> 4) & 0xF];
45 static int ivm_set_value(char *name, char *value)
50 sprintf(tempbuf, "%s=%s", name, value);
51 return set_local_var(tempbuf, 0);
53 unset_local_var(name);
58 static int ivm_get_value(unsigned char *buf, int len, char *name, int off,
62 unsigned char valbuf[30];
64 if ((buf[off + 0] != buf[off + 2]) &&
65 (buf[off + 2] != buf[off + 4])) {
66 printf("%s Error corrupted %s\n", __func__, name);
69 val = buf[off + 0] + (buf[off + 1] << 8);
70 if ((val == 0) && (check == 1))
73 sprintf((char *)valbuf, "%x", val);
74 ivm_set_value(name, (char *)valbuf);
78 #define INV_BLOCKSIZE 0x100
79 #define INV_DATAADDRESS 0x21
80 #define INVENTORYDATASIZE (INV_BLOCKSIZE - INV_DATAADDRESS - 3)
82 #define IVM_POS_SHORT_TEXT 0
83 #define IVM_POS_MANU_ID 1
84 #define IVM_POS_MANU_SERIAL 2
85 #define IVM_POS_PART_NUMBER 3
86 #define IVM_POS_BUILD_STATE 4
87 #define IVM_POS_SUPPLIER_PART_NUMBER 5
88 #define IVM_POS_DELIVERY_DATE 6
89 #define IVM_POS_SUPPLIER_BUILD_STATE 7
90 #define IVM_POS_CUSTOMER_ID 8
91 #define IVM_POS_CUSTOMER_PROD_ID 9
92 #define IVM_POS_HISTORY 10
93 #define IVM_POS_SYMBOL_ONLY 11
95 static char convert_char(char c)
97 return (c < ' ' || c > '~') ? '.' : c;
100 static int ivm_findinventorystring(int type,
101 unsigned char *const string,
102 unsigned long maxlen,
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 */
112 memset(string, '\0', maxlen);
114 case IVM_POS_SYMBOL_ONLY:
123 /* Look for the requested number of CR. */
124 while ((cr != nr) && (addr < INVENTORYDATASIZE)) {
125 if (buf[addr] == '\r')
131 * the expected number of CR was found until the end of the IVM
132 * content --> fill string
134 if (addr < INVENTORYDATASIZE) {
135 /* Copy the IVM string in the corresponding string */
136 for (; (buf[addr] != '\r') &&
137 ((buf[addr] != ';') || (!stop)) &&
138 (size < (maxlen - 1) &&
139 (addr < INVENTORYDATASIZE)); addr++) {
140 size += sprintf((char *)string + size, "%c",
141 convert_char (buf[addr]));
145 * copy phase is done: check if everything is ok. If not,
146 * the inventory data is most probably corrupted: tell
147 * the world there is a problem!
149 if (addr == INVENTORYDATASIZE) {
151 printf("Error end of string not found\n");
152 } else if ((size > (maxlen - 1)) &&
153 (buf[addr] != '\r')) {
155 printf("string too long till next CR\n");
159 * some CR are missing...
160 * the inventory data is most probably corrupted
163 printf("not enough cr found\n");
168 #define GET_STRING(name, which, len) \
169 if (ivm_findinventorystring(which, valbuf, len, buf) == 0) { \
170 ivm_set_value(name, (char *)valbuf); \
173 static int ivm_check_crc(unsigned char *buf, int block)
176 unsigned long crceeprom;
178 crc = ivm_calc_crc(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2);
179 crceeprom = (buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 1] + \
180 buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2] * 256);
181 if (crc != crceeprom) {
183 printf("Error CRC Block: %d EEprom: calculated: \
184 %lx EEprom: %lx\n", block, crc, crceeprom);
190 /* take care of the possible MAC address offset and the IVM content offset */
191 static int process_mac(unsigned char *valbuf, unsigned char *buf,
194 unsigned char mac[6];
195 unsigned long val = (buf[4] << 16) + (buf[5] << 8) + buf[6];
197 /* use an intermediate buffer, to not change IVM content
198 * MAC address is at offset 1
200 memcpy(mac, buf+1, 6);
204 mac[3] = (val >> 16) & 0xff;
205 mac[4] = (val >> 8) & 0xff;
209 sprintf((char *)valbuf, "%pM", mac);
213 static int ivm_analyze_block2(unsigned char *buf, int len)
215 unsigned char valbuf[MAC_STR_SZ];
218 /* IVM_MAC Adress begins at offset 1 */
219 sprintf((char *)valbuf, "%pM", buf + 1);
220 ivm_set_value("IVM_MacAddress", (char *)valbuf);
222 count = (buf[10] << 24) +
226 if (count == 0xffffffff)
228 sprintf((char *)valbuf, "%lx", count);
229 ivm_set_value("IVM_MacCount", (char *)valbuf);
233 int ivm_analyze_eeprom(unsigned char *buf, int len)
236 unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
239 if (ivm_check_crc(buf, 0) != 0)
242 ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
243 "IVM_BoardId", 0, 1);
244 val = ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
247 sprintf((char *)valbuf, "%x", ((val / 100) % 10));
248 ivm_set_value("IVM_HWVariant", (char *)valbuf);
249 sprintf((char *)valbuf, "%x", (val % 100));
250 ivm_set_value("IVM_HWVersion", (char *)valbuf);
252 ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
253 "IVM_Functions", 12, 0);
255 GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8)
256 GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64)
257 tmp = (unsigned char *) getenv("IVM_DeviceName");
259 int len = strlen((char *)tmp);
264 ivm_set_value("IVM_ShortText",
265 (char *)&tmp[i + 1]);
271 ivm_set_value("IVM_ShortText", NULL);
273 ivm_set_value("IVM_ShortText", NULL);
275 GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32)
276 GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20)
277 GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32)
278 GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32)
279 GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32)
280 GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32)
281 GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32)
282 GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32)
283 GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32)
285 if (ivm_check_crc(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0)
287 ivm_analyze_block2(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2],
288 CONFIG_SYS_IVM_EEPROM_PAGE_LEN);
293 static int ivm_populate_env(unsigned char *buf, int len)
295 unsigned char *page2;
296 unsigned char valbuf[MAC_STR_SZ];
298 /* do we have the page 2 filled ? if not return */
299 if (ivm_check_crc(buf, 2))
301 page2 = &buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN*2];
303 /* if an offset is defined, add it */
304 process_mac(valbuf, page2, CONFIG_PIGGY_MAC_ADRESS_OFFSET);
305 if (getenv("ethaddr") == NULL)
306 setenv((char *)"ethaddr", (char *)valbuf);
307 #ifdef CONFIG_KMVECT1
308 /* KMVECT1 has two ethernet interfaces */
309 if (getenv("eth1addr") == NULL) {
310 process_mac(valbuf, page2, 1);
311 setenv((char *)"eth1addr", (char *)valbuf);
318 int ivm_read_eeprom(unsigned char *buf, int len)
322 i2c_set_bus_num(CONFIG_KM_IVM_BUS);
323 /* add deblocking here */
326 ret = i2c_read(CONFIG_SYS_IVM_EEPROM_ADR, 0, 1, buf, len);
328 printf("Error reading EEprom\n");
332 return ivm_populate_env(buf, len);