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 24 #include <common.h> 25 #include <hush.h> 26 #include <i2c.h> 27 #include "common.h" 28 29 int ivm_calc_crc(unsigned char *buf, int len) 30 { 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}; 36 37 unsigned short crc = 0; /* final result */ 38 unsigned short r1 = 0; /* temp */ 39 unsigned char byte = 0; /* input buffer */ 40 int i; 41 42 /* calculate CRC from array data */ 43 for (i = 0; i < len; i++) { 44 byte = buf[i]; 45 46 /* lower 4 bits */ 47 r1 = crc_tab[crc & 0xF]; 48 crc = ((crc) >> 4) & 0x0FFF; 49 crc = crc ^ r1 ^ crc_tab[byte & 0xF]; 50 51 /* upper 4 bits */ 52 r1 = crc_tab[crc & 0xF]; 53 crc = (crc >> 4) & 0x0FFF; 54 crc = crc ^ r1 ^ crc_tab[(byte >> 4) & 0xF]; 55 } 56 return crc; 57 } 58 59 static int ivm_set_value(char *name, char *value) 60 { 61 char tempbuf[256]; 62 63 if (value != NULL) { 64 sprintf(tempbuf, "%s=%s", name, value); 65 return set_local_var(tempbuf, 0); 66 } else { 67 unset_local_var(name); 68 } 69 return 0; 70 } 71 72 static int ivm_get_value(unsigned char *buf, int len, char *name, int off, 73 int check) 74 { 75 unsigned short val; 76 unsigned char valbuf[30]; 77 78 if ((buf[off + 0] != buf[off + 2]) && 79 (buf[off + 2] != buf[off + 4])) { 80 printf("%s Error corrupted %s\n", __func__, name); 81 val = -1; 82 } else { 83 val = buf[off + 0] + (buf[off + 1] << 8); 84 if ((val == 0) && (check == 1)) 85 val = -1; 86 } 87 sprintf((char *)valbuf, "%x", val); 88 ivm_set_value(name, (char *)valbuf); 89 return val; 90 } 91 92 #define INV_BLOCKSIZE 0x100 93 #define INV_DATAADDRESS 0x21 94 #define INVENTORYDATASIZE (INV_BLOCKSIZE - INV_DATAADDRESS - 3) 95 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 108 109 static char convert_char(char c) 110 { 111 return (c < ' ' || c > '~') ? '.' : c; 112 } 113 114 static int ivm_findinventorystring(int type, 115 unsigned char *const string, 116 unsigned long maxlen, 117 unsigned char *buf) 118 { 119 int xcode = 0; 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 */ 125 126 memset(string, '\0', maxlen); 127 switch (type) { 128 case IVM_POS_SYMBOL_ONLY: 129 nr = 0; 130 stop = 1; 131 break; 132 default: 133 nr = type; 134 stop = 0; 135 } 136 137 /* Look for the requested number of CR. */ 138 while ((cr != nr) && (addr < INVENTORYDATASIZE)) { 139 if ((buf[addr] == '\r')) 140 cr++; 141 addr++; 142 } 143 144 /* 145 * the expected number of CR was found until the end of the IVM 146 * content --> fill string 147 */ 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])); 156 } 157 158 /* 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! 162 */ 163 if (addr == INVENTORYDATASIZE) { 164 xcode = -1; 165 printf("Error end of string not found\n"); 166 } else if ((size >= (maxlen - 1)) && 167 (buf[addr] != '\r')) { 168 xcode = -1; 169 printf("string too long till next CR\n"); 170 } 171 } else { 172 /* 173 * some CR are missing... 174 * the inventory data is most probably corrupted 175 */ 176 xcode = -1; 177 printf("not enough cr found\n"); 178 } 179 return xcode; 180 } 181 182 #define GET_STRING(name, which, len) \ 183 if (ivm_findinventorystring(which, valbuf, len, buf) == 0) { \ 184 ivm_set_value(name, (char *)valbuf); \ 185 } 186 187 static int ivm_check_crc(unsigned char *buf, int block) 188 { 189 unsigned long crc; 190 unsigned long crceeprom; 191 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) { 196 if (block == 0) 197 printf("Error CRC Block: %d EEprom: calculated: \ 198 %lx EEprom: %lx\n", block, crc, crceeprom); 199 return -1; 200 } 201 return 0; 202 } 203 204 static int ivm_analyze_block2(unsigned char *buf, int len) 205 { 206 unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN]; 207 unsigned long count; 208 209 /* IVM_MAC Adress begins at offset 1 */ 210 sprintf((char *)valbuf, "%pM", buf + 1); 211 ivm_set_value("IVM_MacAddress", (char *)valbuf); 212 /* if an offset is defined, add it */ 213 #if defined(CONFIG_PIGGY_MAC_ADRESS_OFFSET) 214 if (CONFIG_PIGGY_MAC_ADRESS_OFFSET > 0) { 215 unsigned long val = (buf[4] << 16) + (buf[5] << 8) + buf[6]; 216 217 val += CONFIG_PIGGY_MAC_ADRESS_OFFSET; 218 buf[4] = (val >> 16) & 0xff; 219 buf[5] = (val >> 8) & 0xff; 220 buf[6] = val & 0xff; 221 sprintf((char *)valbuf, "%pM", buf); 222 } 223 #endif 224 setenv((char *)"ethaddr", (char *)valbuf); 225 226 /* IVM_MacCount */ 227 count = (buf[10] << 24) + 228 (buf[11] << 16) + 229 (buf[12] << 8) + 230 buf[13]; 231 if (count == 0xffffffff) 232 count = 1; 233 sprintf((char *)valbuf, "%lx", count); 234 ivm_set_value("IVM_MacCount", (char *)valbuf); 235 return 0; 236 } 237 238 int ivm_analyze_eeprom(unsigned char *buf, int len) 239 { 240 unsigned short val; 241 unsigned char valbuf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN]; 242 unsigned char *tmp; 243 244 if (ivm_check_crc(buf, 0) != 0) 245 return -1; 246 247 ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN, 248 "IVM_BoardId", 0, 1); 249 val = ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN, 250 "IVM_HWKey", 6, 1); 251 if (val != 0xffff) { 252 sprintf((char *)valbuf, "%x", ((val / 100) % 10)); 253 ivm_set_value("IVM_HWVariant", (char *)valbuf); 254 sprintf((char *)valbuf, "%x", (val % 100)); 255 ivm_set_value("IVM_HWVersion", (char *)valbuf); 256 } 257 ivm_get_value(buf, CONFIG_SYS_IVM_EEPROM_PAGE_LEN, 258 "IVM_Functions", 12, 0); 259 260 GET_STRING("IVM_Symbol", IVM_POS_SYMBOL_ONLY, 8) 261 GET_STRING("IVM_DeviceName", IVM_POS_SHORT_TEXT, 64) 262 tmp = (unsigned char *) getenv("IVM_DeviceName"); 263 if (tmp) { 264 int len = strlen((char *)tmp); 265 int i = 0; 266 267 while (i < len) { 268 if (tmp[i] == ';') { 269 ivm_set_value("IVM_ShortText", 270 (char *)&tmp[i + 1]); 271 break; 272 } 273 i++; 274 } 275 if (i >= len) 276 ivm_set_value("IVM_ShortText", NULL); 277 } else { 278 ivm_set_value("IVM_ShortText", NULL); 279 } 280 GET_STRING("IVM_ManufacturerID", IVM_POS_MANU_ID, 32) 281 GET_STRING("IVM_ManufacturerSerialNumber", IVM_POS_MANU_SERIAL, 20) 282 GET_STRING("IVM_ManufacturerPartNumber", IVM_POS_PART_NUMBER, 32) 283 GET_STRING("IVM_ManufacturerBuildState", IVM_POS_BUILD_STATE, 32) 284 GET_STRING("IVM_SupplierPartNumber", IVM_POS_SUPPLIER_PART_NUMBER, 32) 285 GET_STRING("IVM_DelieveryDate", IVM_POS_DELIVERY_DATE, 32) 286 GET_STRING("IVM_SupplierBuildState", IVM_POS_SUPPLIER_BUILD_STATE, 32) 287 GET_STRING("IVM_CustomerID", IVM_POS_CUSTOMER_ID, 32) 288 GET_STRING("IVM_CustomerProductID", IVM_POS_CUSTOMER_PROD_ID, 32) 289 290 if (ivm_check_crc(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 2) != 0) 291 return 0; 292 ivm_analyze_block2(&buf[CONFIG_SYS_IVM_EEPROM_PAGE_LEN * 2], 293 CONFIG_SYS_IVM_EEPROM_PAGE_LEN); 294 295 return 0; 296 } 297 298 int ivm_read_eeprom(void) 299 { 300 #if defined(CONFIG_I2C_MUX) 301 I2C_MUX_DEVICE *dev = NULL; 302 #endif 303 uchar i2c_buffer[CONFIG_SYS_IVM_EEPROM_MAX_LEN]; 304 uchar *buf; 305 unsigned long dev_addr = CONFIG_SYS_IVM_EEPROM_ADR; 306 int ret; 307 308 #if defined(CONFIG_I2C_MUX) 309 /* First init the Bus, select the Bus */ 310 #if defined(CONFIG_SYS_I2C_IVM_BUS) 311 dev = i2c_mux_ident_muxstring((uchar *)CONFIG_SYS_I2C_IVM_BUS); 312 #else 313 buf = (unsigned char *) getenv("EEprom_ivm"); 314 if (buf != NULL) 315 dev = i2c_mux_ident_muxstring(buf); 316 #endif 317 if (dev == NULL) { 318 printf("Error couldnt add Bus for IVM\n"); 319 return -1; 320 } 321 i2c_set_bus_num(dev->busid); 322 #endif 323 324 buf = (unsigned char *) getenv("EEprom_ivm_addr"); 325 if (buf != NULL) { 326 ret = strict_strtoul((char *)buf, 16, &dev_addr); 327 if (ret != 0) 328 return -3; 329 } 330 331 /* add deblocking here */ 332 i2c_make_abort(); 333 334 ret = i2c_read(dev_addr, 0, 1, i2c_buffer, 335 CONFIG_SYS_IVM_EEPROM_MAX_LEN); 336 if (ret != 0) { 337 printf("Error reading EEprom\n"); 338 return -2; 339 } 340 341 return ivm_analyze_eeprom(i2c_buffer, CONFIG_SYS_IVM_EEPROM_MAX_LEN); 342 } 343