3 * Holger Brunck, Keymile GmbH Hannover, holger.brunck@keymile.com
5 * See file CREDITS for list of people who contributed to this
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.
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.
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,
29 int ivm_calc_crc(unsigned char *buf, int len)
31 const unsigned short crc_tab[16] = {
32 0x0000, 0xCC01, 0xD801, 0x1400,
33 0xF001, 0x3C00, 0x2800, 0xE401,
34 0xA001, 0x6C00, 0x7800, 0xB401,
35 0x5000, 0x9C01, 0x8801, 0x4400};
37 unsigned short crc = 0; /* final result */
38 unsigned short r1 = 0; /* temp */
39 unsigned char byte = 0; /* input buffer */
42 /* calculate CRC from array data */
43 for (i = 0; i < len; i++) {
47 r1 = crc_tab[crc & 0xF];
48 crc = ((crc) >> 4) & 0x0FFF;
49 crc = crc ^ r1 ^ crc_tab[byte & 0xF];
52 r1 = crc_tab[crc & 0xF];
53 crc = (crc >> 4) & 0x0FFF;
54 crc = crc ^ r1 ^ crc_tab[(byte >> 4) & 0xF];
59 static int ivm_set_value(char *name, char *value)
64 sprintf(tempbuf, "%s=%s", name, value);
65 return set_local_var(tempbuf, 0);
67 unset_local_var(name);
72 static int ivm_get_value(unsigned char *buf, int len, char *name, int off,
76 unsigned char valbuf[30];
78 if ((buf[off + 0] != buf[off + 2]) &&
79 (buf[off + 2] != buf[off + 4])) {
80 printf("%s Error corrupted %s\n", __func__, name);
83 val = buf[off + 0] + (buf[off + 1] << 8);
84 if ((val == 0) && (check == 1))
87 sprintf((char *)valbuf, "%x", val);
88 ivm_set_value(name, (char *)valbuf);
92 #define INV_BLOCKSIZE 0x100
93 #define INV_DATAADDRESS 0x21
94 #define INVENTORYDATASIZE (INV_BLOCKSIZE - INV_DATAADDRESS - 3)
96 #define IVM_POS_SHORT_TEXT 0
97 #define IVM_POS_MANU_ID 1
98 #define IVM_POS_MANU_SERIAL 2
99 #define IVM_POS_PART_NUMBER 3
100 #define IVM_POS_BUILD_STATE 4
101 #define IVM_POS_SUPPLIER_PART_NUMBER 5
102 #define IVM_POS_DELIVERY_DATE 6
103 #define IVM_POS_SUPPLIER_BUILD_STATE 7
104 #define IVM_POS_CUSTOMER_ID 8
105 #define IVM_POS_CUSTOMER_PROD_ID 9
106 #define IVM_POS_HISTORY 10
107 #define IVM_POS_SYMBOL_ONLY 11
109 static char convert_char(char c)
111 return (c < ' ' || c > '~') ? '.' : c;
114 static int ivm_findinventorystring(int type,
115 unsigned char *const string,
116 unsigned long maxlen,
120 unsigned long cr = 0;
121 unsigned long addr = INV_DATAADDRESS;
122 unsigned long size = 0;
123 unsigned long nr = type;
124 int stop = 0; /* stop on semicolon */
126 memset(string, '\0', maxlen);
128 case IVM_POS_SYMBOL_ONLY:
137 /* Look for the requested number of CR. */
138 while ((cr != nr) && (addr < INVENTORYDATASIZE)) {
139 if ((buf[addr] == '\r'))
145 * the expected number of CR was found until the end of the IVM
146 * content --> fill string
148 if (addr < INVENTORYDATASIZE) {
149 /* Copy the IVM string in the corresponding string */
150 for (; (buf[addr] != '\r') &&
151 ((buf[addr] != ';') || (!stop)) &&
152 (size < (maxlen - 1) &&
153 (addr < INVENTORYDATASIZE)); addr++) {
154 size += sprintf((char *)string + size, "%c",
155 convert_char (buf[addr]));
159 * copy phase is done: check if everything is ok. If not,
160 * the inventory data is most probably corrupted: tell
161 * the world there is a problem!
163 if (addr == INVENTORYDATASIZE) {
165 printf("Error end of string not found\n");
166 } else if ((size > (maxlen - 1)) &&
167 (buf[addr] != '\r')) {
169 printf("string too long till next CR\n");
173 * some CR are missing...
174 * the inventory data is most probably corrupted
177 printf("not enough cr found\n");
182 #define GET_STRING(name, which, len) \
183 if (ivm_findinventorystring(which, valbuf, len, buf) == 0) { \
184 ivm_set_value(name, (char *)valbuf); \
187 static int ivm_check_crc(unsigned char *buf, int block)
190 unsigned long crceeprom;
192 crc = ivm_calc_crc(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2);
193 crceeprom = (buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 1] + \
194 buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN - 2] * 256);
195 if (crc != crceeprom) {
197 printf("Error CRC Block: %d EEprom: calculated: \
198 %lx EEprom: %lx\n", block, crc, crceeprom);
204 static int calculate_mac_offset(unsigned char *valbuf, unsigned char *buf,
207 unsigned long val = (buf[4] << 16) + (buf[5] << 8) + buf[6];
213 buf[4] = (val >> 16) & 0xff;
214 buf[5] = (val >> 8) & 0xff;
216 sprintf((char *)valbuf, "%pM", buf + 1);
220 static int ivm_analyze_block2(unsigned char *buf, int len)
222 unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
225 /* IVM_MAC Adress begins at offset 1 */
226 sprintf((char *)valbuf, "%pM", buf + 1);
227 ivm_set_value("IVM_MacAddress", (char *)valbuf);
228 /* if an offset is defined, add it */
229 calculate_mac_offset(buf, valbuf, CONFIG_PIGGY_MAC_ADRESS_OFFSET);
230 #ifdef MACH_TYPE_KM_KIRKWOOD
231 setenv((char *)"ethaddr", (char *)valbuf);
233 if (getenv("ethaddr") == NULL)
234 setenv((char *)"ethaddr", (char *)valbuf);
236 #ifdef CONFIG_KMVECT1
237 /* KMVECT1 has two ethernet interfaces */
238 if (getenv("eth1addr") == NULL) {
239 calculate_mac_offset(buf, valbuf, 1);
240 setenv((char *)"eth1addr", (char *)valbuf);
244 count = (buf[10] << 24) +
248 if (count == 0xffffffff)
250 sprintf((char *)valbuf, "%lx", count);
251 ivm_set_value("IVM_MacCount", (char *)valbuf);
255 int ivm_analyze_eeprom(unsigned char *buf, int len)
258 unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN];
261 if (ivm_check_crc(buf, 0) != 0)
264 ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
265 "IVM_BoardId", 0, 1);
266 val = ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
269 sprintf((char *)valbuf, "%x", ((val / 100) % 10));
270 ivm_set_value("IVM_HWVariant", (char *)valbuf);
271 sprintf((char *)valbuf, "%x", (val % 100));
272 ivm_set_value("IVM_HWVersion", (char *)valbuf);
274 ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN,
275 "IVM_Functions", 12, 0);
277 GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8)
278 GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64)
279 tmp = (unsigned char *) getenv("IVM_DeviceName");
281 int len = strlen((char *)tmp);
286 ivm_set_value("IVM_ShortText",
287 (char *)&tmp[i + 1]);
293 ivm_set_value("IVM_ShortText", NULL);
295 ivm_set_value("IVM_ShortText", NULL);
297 GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32)
298 GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20)
299 GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32)
300 GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32)
301 GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32)
302 GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32)
303 GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32)
304 GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32)
305 GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32)
307 if (ivm_check_crc(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0)
309 ivm_analyze_block2(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2],
310 CONFIG_SYS_IVM_EEPROM_PAGE_LEN);
315 int ivm_read_eeprom(void)
317 #if defined(CONFIG_I2C_MUX)
318 I2C_MUX_DEVICE *dev = NULL;
320 uchar i2c_buffer[CONFIG_SYS_IVM_EEPROM_MAX_LEN];
322 unsigned long dev_addr = CONFIG_SYS_IVM_EEPROM_ADR;
325 #if defined(CONFIG_I2C_MUX)
326 /* First init the Bus, select the Bus */
327 buf = (unsigned char *) getenv("EEprom_ivm");
329 dev = i2c_mux_ident_muxstring(buf);
331 printf("Error couldnt add Bus for IVM\n");
334 i2c_set_bus_num(dev->busid);
336 /* add deblocking here */
339 ret = i2c_read(dev_addr, 0, 1, i2c_buffer,
340 CONFIG_SYS_IVM_EEPROM_MAX_LEN);
342 printf("Error reading EEprom\n");
346 return ivm_analyze_eeprom(i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN);