blob: b5f1aa61cb8dd135ee10165c847fa555f9908a9e [file] [log] [blame]
Nikita Kiryanov82309252012-01-12 03:26:30 +00001/*
2 * (C) Copyright 2011 CompuLab, Ltd. <www.compulab.co.il>
3 *
4 * Authors: Nikita Kiryanov <nikita@compulab.co.il>
5 * Igor Grinberg <grinberg@compulab.co.il>
6 *
Wolfgang Denk1a459662013-07-08 09:37:19 +02007 * SPDX-License-Identifier: GPL-2.0+
Nikita Kiryanov82309252012-01-12 03:26:30 +00008 */
9
10#include <common.h>
11#include <i2c.h>
Nikita Kiryanov8af57342016-04-16 17:55:04 +030012#include <eeprom_layout.h>
13#include <eeprom_field.h>
14#include <linux/kernel.h>
Nikita Kiryanov53af8772015-09-06 11:48:37 +030015#include "eeprom.h"
Nikita Kiryanov82309252012-01-12 03:26:30 +000016
Igor Grinbergd3f041c2014-05-15 11:25:09 +030017#ifndef CONFIG_SYS_I2C_EEPROM_ADDR
18# define CONFIG_SYS_I2C_EEPROM_ADDR 0x50
19# define CONFIG_SYS_I2C_EEPROM_ADDR_LEN 1
20#endif
21
Nikita Kiryanov7d2f6692014-09-17 15:59:25 +030022#ifndef CONFIG_SYS_I2C_EEPROM_BUS
23#define CONFIG_SYS_I2C_EEPROM_BUS 0
24#endif
25
Nikita Kiryanov82309252012-01-12 03:26:30 +000026#define EEPROM_LAYOUT_VER_OFFSET 44
27#define BOARD_SERIAL_OFFSET 20
28#define BOARD_SERIAL_OFFSET_LEGACY 8
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +000029#define BOARD_REV_OFFSET 0
30#define BOARD_REV_OFFSET_LEGACY 6
Nikita Kiryanov6f3b3002012-05-24 04:01:22 +000031#define BOARD_REV_SIZE 2
Nikita Kiryanov53af8772015-09-06 11:48:37 +030032#define PRODUCT_NAME_OFFSET 128
33#define PRODUCT_NAME_SIZE 16
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +000034#define MAC_ADDR_OFFSET 4
35#define MAC_ADDR_OFFSET_LEGACY 0
Nikita Kiryanov82309252012-01-12 03:26:30 +000036
37#define LAYOUT_INVALID 0
38#define LAYOUT_LEGACY 0xff
39
Nikita Kiryanove7a24472015-01-14 10:42:43 +020040static int cl_eeprom_bus;
Igor Grinberg689be5f2013-09-16 21:49:58 +030041static int cl_eeprom_layout; /* Implicitly LAYOUT_INVALID */
Nikita Kiryanov82309252012-01-12 03:26:30 +000042
Igor Grinberg689be5f2013-09-16 21:49:58 +030043static int cl_eeprom_read(uint offset, uchar *buf, int len)
Nikita Kiryanov82309252012-01-12 03:26:30 +000044{
Nikita Kiryanov52658fd2014-08-20 15:08:52 +030045 int res;
46 unsigned int current_i2c_bus = i2c_get_bus_num();
47
Nikita Kiryanove7a24472015-01-14 10:42:43 +020048 res = i2c_set_bus_num(cl_eeprom_bus);
Nikita Kiryanov52658fd2014-08-20 15:08:52 +030049 if (res < 0)
50 return res;
51
52 res = i2c_read(CONFIG_SYS_I2C_EEPROM_ADDR, offset,
Nikita Kiryanov82309252012-01-12 03:26:30 +000053 CONFIG_SYS_I2C_EEPROM_ADDR_LEN, buf, len);
Nikita Kiryanov52658fd2014-08-20 15:08:52 +030054
55 i2c_set_bus_num(current_i2c_bus);
56
57 return res;
Nikita Kiryanov82309252012-01-12 03:26:30 +000058}
59
Nikita Kiryanove7a24472015-01-14 10:42:43 +020060static int cl_eeprom_setup(uint eeprom_bus)
Nikita Kiryanov82309252012-01-12 03:26:30 +000061{
62 int res;
63
Nikita Kiryanove7a24472015-01-14 10:42:43 +020064 /*
65 * We know the setup was already done when the layout is set to a valid
66 * value and we're using the same bus as before.
67 */
68 if (cl_eeprom_layout != LAYOUT_INVALID && eeprom_bus == cl_eeprom_bus)
Nikita Kiryanov82309252012-01-12 03:26:30 +000069 return 0;
70
Nikita Kiryanove7a24472015-01-14 10:42:43 +020071 cl_eeprom_bus = eeprom_bus;
Igor Grinberg689be5f2013-09-16 21:49:58 +030072 res = cl_eeprom_read(EEPROM_LAYOUT_VER_OFFSET,
73 (uchar *)&cl_eeprom_layout, 1);
Nikita Kiryanov82309252012-01-12 03:26:30 +000074 if (res) {
Igor Grinberg689be5f2013-09-16 21:49:58 +030075 cl_eeprom_layout = LAYOUT_INVALID;
Nikita Kiryanov82309252012-01-12 03:26:30 +000076 return res;
77 }
78
Igor Grinberg689be5f2013-09-16 21:49:58 +030079 if (cl_eeprom_layout == 0 || cl_eeprom_layout >= 0x20)
80 cl_eeprom_layout = LAYOUT_LEGACY;
Nikita Kiryanov82309252012-01-12 03:26:30 +000081
82 return 0;
83}
84
85void get_board_serial(struct tag_serialnr *serialnr)
86{
87 u32 serial[2];
88 uint offset;
89
90 memset(serialnr, 0, sizeof(*serialnr));
Igor Grinberg689be5f2013-09-16 21:49:58 +030091
Nikita Kiryanove7a24472015-01-14 10:42:43 +020092 if (cl_eeprom_setup(CONFIG_SYS_I2C_EEPROM_BUS))
Nikita Kiryanov82309252012-01-12 03:26:30 +000093 return;
94
Igor Grinberg689be5f2013-09-16 21:49:58 +030095 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
96 BOARD_SERIAL_OFFSET : BOARD_SERIAL_OFFSET_LEGACY;
97
98 if (cl_eeprom_read(offset, (uchar *)serial, 8))
Nikita Kiryanov82309252012-01-12 03:26:30 +000099 return;
100
101 if (serial[0] != 0xffffffff && serial[1] != 0xffffffff) {
102 serialnr->low = serial[0];
103 serialnr->high = serial[1];
104 }
105}
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000106
107/*
Igor Grinberg689be5f2013-09-16 21:49:58 +0300108 * Routine: cl_eeprom_read_mac_addr
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000109 * Description: read mac address and store it in buf.
110 */
Nikita Kiryanove7a24472015-01-14 10:42:43 +0200111int cl_eeprom_read_mac_addr(uchar *buf, uint eeprom_bus)
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000112{
113 uint offset;
Nikita Kiryanove93e8092015-09-06 11:48:36 +0300114 int err;
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000115
Nikita Kiryanove93e8092015-09-06 11:48:36 +0300116 err = cl_eeprom_setup(eeprom_bus);
117 if (err)
118 return err;
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000119
Igor Grinberg689be5f2013-09-16 21:49:58 +0300120 offset = (cl_eeprom_layout != LAYOUT_LEGACY) ?
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000121 MAC_ADDR_OFFSET : MAC_ADDR_OFFSET_LEGACY;
Igor Grinberg689be5f2013-09-16 21:49:58 +0300122
123 return cl_eeprom_read(offset, buf, 6);
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000124}
125
Igor Grinberga937fd12014-11-03 11:32:18 +0200126static u32 board_rev;
127
Nikita Kiryanove4e2bf52012-01-12 03:28:09 +0000128/*
Igor Grinberg689be5f2013-09-16 21:49:58 +0300129 * Routine: cl_eeprom_get_board_rev
Nikita Kiryanov8c318eb2012-05-24 04:01:24 +0000130 * Description: read system revision from eeprom
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000131 */
Nikita Kiryanov72898ac2015-09-06 11:48:35 +0300132u32 cl_eeprom_get_board_rev(uint eeprom_bus)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000133{
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000134 char str[5]; /* Legacy representation can contain at most 4 digits */
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000135 uint offset = BOARD_REV_OFFSET_LEGACY;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000136
Igor Grinberga937fd12014-11-03 11:32:18 +0200137 if (board_rev)
138 return board_rev;
139
Nikita Kiryanov72898ac2015-09-06 11:48:35 +0300140 if (cl_eeprom_setup(eeprom_bus))
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000141 return 0;
142
Igor Grinberg689be5f2013-09-16 21:49:58 +0300143 if (cl_eeprom_layout != LAYOUT_LEGACY)
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000144 offset = BOARD_REV_OFFSET;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000145
Igor Grinberga937fd12014-11-03 11:32:18 +0200146 if (cl_eeprom_read(offset, (uchar *)&board_rev, BOARD_REV_SIZE))
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000147 return 0;
148
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000149 /*
150 * Convert legacy syntactic representation to semantic
151 * representation. i.e. for rev 1.00: 0x100 --> 0x64
152 */
Igor Grinberg689be5f2013-09-16 21:49:58 +0300153 if (cl_eeprom_layout == LAYOUT_LEGACY) {
Igor Grinberga937fd12014-11-03 11:32:18 +0200154 sprintf(str, "%x", board_rev);
155 board_rev = simple_strtoul(str, NULL, 10);
Nikita Kiryanov2ef63022012-05-24 04:01:23 +0000156 }
157
Igor Grinberga937fd12014-11-03 11:32:18 +0200158 return board_rev;
Nikita Kiryanov7d3c97d2012-01-02 04:01:34 +0000159};
Nikita Kiryanov53af8772015-09-06 11:48:37 +0300160
161/*
162 * Routine: cl_eeprom_get_board_rev
163 * Description: read system revision from eeprom
164 *
165 * @buf: buffer to store the product name
166 * @eeprom_bus: i2c bus num of the eeprom
167 *
168 * @return: 0 on success, < 0 on failure
169 */
170int cl_eeprom_get_product_name(uchar *buf, uint eeprom_bus)
171{
172 int err;
173
174 if (buf == NULL)
175 return -EINVAL;
176
177 err = cl_eeprom_setup(eeprom_bus);
178 if (err)
179 return err;
180
181 err = cl_eeprom_read(PRODUCT_NAME_OFFSET, buf, PRODUCT_NAME_SIZE);
182 if (!err) /* Protect ourselves from invalid data (unterminated str) */
183 buf[PRODUCT_NAME_SIZE - 1] = '\0';
184
185 return err;
186}
Nikita Kiryanov8af57342016-04-16 17:55:04 +0300187
188#ifdef CONFIG_CMD_EEPROM_LAYOUT
189/**
190 * eeprom_field_print_bin_ver() - print a "version field" which contains binary
191 * data
192 *
193 * Treat the field data as simple binary data, and print it formatted as a
194 * version number (2 digits after decimal point).
195 * The field size must be exactly 2 bytes.
196 *
197 * Sample output:
198 * Field Name 123.45
199 *
200 * @field: an initialized field to print
201 */
202void eeprom_field_print_bin_ver(const struct eeprom_field *field)
203{
204 if ((field->buf[0] == 0xff) && (field->buf[1] == 0xff)) {
205 field->buf[0] = 0;
206 field->buf[1] = 0;
207 }
208
209 printf(PRINT_FIELD_SEGMENT, field->name);
210 int major = (field->buf[1] << 8 | field->buf[0]) / 100;
211 int minor = (field->buf[1] << 8 | field->buf[0]) - major * 100;
212 printf("%d.%02d\n", major, minor);
213}
214
215/**
216 * eeprom_field_update_bin_ver() - update a "version field" which contains
217 * binary data
218 *
219 * This function takes a version string in the form of x.y (x and y are both
220 * decimal values, y is limited to two digits), translates it to the binary
221 * form, then writes it to the field. The field size must be exactly 2 bytes.
222 *
223 * This function strictly enforces the data syntax, and will not update the
224 * field if there's any deviation from it. It also protects from overflow.
225 *
226 * @field: an initialized field
227 * @value: a version string
228 *
229 * Returns 0 on success, -1 on failure.
230 */
231int eeprom_field_update_bin_ver(struct eeprom_field *field, char *value)
232{
233 char *endptr;
234 char *tok = strtok(value, ".");
235 if (tok == NULL)
236 return -1;
237
238 int num = simple_strtol(tok, &endptr, 0);
239 if (*endptr != '\0')
240 return -1;
241
242 tok = strtok(NULL, "");
243 if (tok == NULL)
244 return -1;
245
246 int remainder = simple_strtol(tok, &endptr, 0);
247 if (*endptr != '\0')
248 return -1;
249
250 num = num * 100 + remainder;
251 if (num >> 16)
252 return -1;
253
254 field->buf[0] = (unsigned char)num;
255 field->buf[1] = num >> 8;
256
257 return 0;
258}
259
260char *months[12] = {"Jan", "Feb", "Mar", "Apr", "May", "Jun",
261 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};
262
263/**
264 * eeprom_field_print_date() - print a field which contains date data
265 *
266 * Treat the field data as simple binary data, and print it formatted as a date.
267 * Sample output:
268 * Field Name 07/Feb/2014
269 * Field Name 56/BAD/9999
270 *
271 * @field: an initialized field to print
272 */
273void eeprom_field_print_date(const struct eeprom_field *field)
274{
275 printf(PRINT_FIELD_SEGMENT, field->name);
276 printf("%02d/", field->buf[0]);
277 if (field->buf[1] >= 1 && field->buf[1] <= 12)
278 printf("%s", months[field->buf[1] - 1]);
279 else
280 printf("BAD");
281
282 printf("/%d\n", field->buf[3] << 8 | field->buf[2]);
283}
284
285static int validate_date(unsigned char day, unsigned char month,
286 unsigned int year)
287{
288 int days_in_february;
289
290 switch (month) {
291 case 0:
292 case 2:
293 case 4:
294 case 6:
295 case 7:
296 case 9:
297 case 11:
298 if (day > 31)
299 return -1;
300 break;
301 case 3:
302 case 5:
303 case 8:
304 case 10:
305 if (day > 30)
306 return -1;
307 break;
308 case 1:
309 days_in_february = 28;
310 if (year % 4 == 0) {
311 if (year % 100 != 0)
312 days_in_february = 29;
313 else if (year % 400 == 0)
314 days_in_february = 29;
315 }
316
317 if (day > days_in_february)
318 return -1;
319
320 break;
321 default:
322 return -1;
323 }
324
325 return 0;
326}
327
328/**
329 * eeprom_field_update_date() - update a date field which contains binary data
330 *
331 * This function takes a date string in the form of x/Mon/y (x and y are both
332 * decimal values), translates it to the binary representation, then writes it
333 * to the field.
334 *
335 * This function strictly enforces the data syntax, and will not update the
336 * field if there's any deviation from it. It also protects from overflow in the
337 * year value, and checks the validity of the date.
338 *
339 * @field: an initialized field
340 * @value: a date string
341 *
342 * Returns 0 on success, -1 on failure.
343 */
344int eeprom_field_update_date(struct eeprom_field *field, char *value)
345{
346 char *endptr;
347 char *tok1 = strtok(value, "/");
348 char *tok2 = strtok(NULL, "/");
349 char *tok3 = strtok(NULL, "/");
350
351 if (tok1 == NULL || tok2 == NULL || tok3 == NULL) {
352 printf("%s: syntax error\n", field->name);
353 return -1;
354 }
355
356 unsigned char day = (unsigned char)simple_strtol(tok1, &endptr, 0);
357 if (*endptr != '\0' || day == 0) {
358 printf("%s: invalid day\n", field->name);
359 return -1;
360 }
361
362 unsigned char month;
363 for (month = 1; month <= 12; month++)
364 if (!strcmp(tok2, months[month - 1]))
365 break;
366
367 unsigned int year = simple_strtol(tok3, &endptr, 0);
368 if (*endptr != '\0') {
369 printf("%s: invalid year\n", field->name);
370 return -1;
371 }
372
373 if (validate_date(day, month - 1, year)) {
374 printf("%s: invalid date\n", field->name);
375 return -1;
376 }
377
378 if (year >> 16) {
379 printf("%s: year overflow\n", field->name);
380 return -1;
381 }
382
383 field->buf[0] = day;
384 field->buf[1] = month;
385 field->buf[2] = (unsigned char)year;
386 field->buf[3] = (unsigned char)(year >> 8);
387
388 return 0;
389}
390
391#define LAYOUT_VERSION_LEGACY 1
392#define LAYOUT_VERSION_VER1 2
393#define LAYOUT_VERSION_VER2 3
394#define LAYOUT_VERSION_VER3 4
395
396extern struct eeprom_field layout_unknown[1];
397
398#define DEFINE_PRINT_UPDATE(x) eeprom_field_print_##x, eeprom_field_update_##x
399
400#ifdef CONFIG_CM_T3X
401struct eeprom_field layout_legacy[5] = {
402 { "MAC address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
403 { "Board Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin) },
404 { "Serial Number", 8, NULL, DEFINE_PRINT_UPDATE(bin) },
405 { "Board Configuration", 64, NULL, DEFINE_PRINT_UPDATE(ascii) },
406 { RESERVED_FIELDS, 176, NULL, eeprom_field_print_reserved,
407 eeprom_field_update_ascii },
408};
409#else
410#define layout_legacy layout_unknown
411#endif
412
413#if defined(CONFIG_CM_T3X) || defined(CONFIG_CM_T3517)
414struct eeprom_field layout_v1[12] = {
415 { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
416 { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
417 { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
418 { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
419 { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) },
420 { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) },
421 { RESERVED_FIELDS, 96, NULL, DEFINE_PRINT_UPDATE(reserved) },
422 { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
423 { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
424 { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
425 { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
426 { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved,
427 eeprom_field_update_ascii },
428};
429#else
430#define layout_v1 layout_unknown
431#endif
432
433struct eeprom_field layout_v2[15] = {
434 { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
435 { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
436 { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
437 { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
438 { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) },
439 { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) },
440 { "3rd MAC Address (WIFI)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
441 { "4th MAC Address (Bluetooth)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
442 { "Layout Version", 1, NULL, DEFINE_PRINT_UPDATE(bin) },
443 { RESERVED_FIELDS, 83, NULL, DEFINE_PRINT_UPDATE(reserved) },
444 { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
445 { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
446 { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
447 { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
448 { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved,
449 eeprom_field_update_ascii },
450};
451
452struct eeprom_field layout_v3[16] = {
453 { "Major Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
454 { "Minor Revision", 2, NULL, DEFINE_PRINT_UPDATE(bin_ver) },
455 { "1st MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
456 { "2nd MAC Address", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
457 { "Production Date", 4, NULL, DEFINE_PRINT_UPDATE(date) },
458 { "Serial Number", 12, NULL, DEFINE_PRINT_UPDATE(bin_rev) },
459 { "3rd MAC Address (WIFI)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
460 { "4th MAC Address (Bluetooth)", 6, NULL, DEFINE_PRINT_UPDATE(mac) },
461 { "Layout Version", 1, NULL, DEFINE_PRINT_UPDATE(bin) },
462 { "CompuLab EEPROM ID", 3, NULL, DEFINE_PRINT_UPDATE(bin) },
463 { RESERVED_FIELDS, 80, NULL, DEFINE_PRINT_UPDATE(reserved) },
464 { "Product Name", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
465 { "Product Options #1", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
466 { "Product Options #2", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
467 { "Product Options #3", 16, NULL, DEFINE_PRINT_UPDATE(ascii) },
468 { RESERVED_FIELDS, 64, NULL, eeprom_field_print_reserved,
469 eeprom_field_update_ascii },
470};
471
472void eeprom_layout_assign(struct eeprom_layout *layout, int layout_version)
473{
474 switch (layout->layout_version) {
475 case LAYOUT_VERSION_LEGACY:
476 layout->fields = layout_legacy;
477 layout->num_of_fields = ARRAY_SIZE(layout_legacy);
478 break;
479 case LAYOUT_VERSION_VER1:
480 layout->fields = layout_v1;
481 layout->num_of_fields = ARRAY_SIZE(layout_v1);
482 break;
483 case LAYOUT_VERSION_VER2:
484 layout->fields = layout_v2;
485 layout->num_of_fields = ARRAY_SIZE(layout_v2);
486 break;
487 case LAYOUT_VERSION_VER3:
488 layout->fields = layout_v3;
489 layout->num_of_fields = ARRAY_SIZE(layout_v3);
490 break;
491 default:
492 __eeprom_layout_assign(layout, layout_version);
493 }
494}
495
496int eeprom_parse_layout_version(char *str)
497{
498 if (!strcmp(str, "legacy"))
499 return LAYOUT_VERSION_LEGACY;
500 else if (!strcmp(str, "v1"))
501 return LAYOUT_VERSION_VER1;
502 else if (!strcmp(str, "v2"))
503 return LAYOUT_VERSION_VER2;
504 else if (!strcmp(str, "v3"))
505 return LAYOUT_VERSION_VER3;
506 else
507 return LAYOUT_VERSION_UNRECOGNIZED;
508}
509
510int eeprom_layout_detect(unsigned char *data)
511{
512 switch (data[EEPROM_LAYOUT_VER_OFFSET]) {
513 case 0xff:
514 case 0:
515 return LAYOUT_VERSION_VER1;
516 case 2:
517 return LAYOUT_VERSION_VER2;
518 case 3:
519 return LAYOUT_VERSION_VER3;
520 }
521
522 if (data[EEPROM_LAYOUT_VER_OFFSET] >= 0x20)
523 return LAYOUT_VERSION_LEGACY;
524
525 return LAYOUT_VERSION_UNRECOGNIZED;
526}
527#endif