Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/common_drivers/fru_tools/fru_tools.c

raw symbols SCIP
1 /*
2 * fru_tools.c
3 * Copyright (C) 2012 Analog Devices
4 * Author : Robin Getz <[email protected]>
5 *
6 * This file is maintained as part of:
7 * https://github.com/analogdevicesinc/fru_tools
8 * but is released under this license, so you can use it without having
9 * your software fall under the GPL. If you make improvements to this,
10 * although you are not required, it would be nice if you sent me a patch.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 * - Redistributions of source code must retain the above copyright
16 * notice, this list of conditions and the following disclaimer.
17 * - Redistributions in binary form must reproduce the above copyright
18 * notice, this list of conditions and the following disclaimer in
19 * the documentation and/or other materials provided with the
20 * distribution.
21 * - Neither the name of Analog Devices, Inc. nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 * - The use of this software may or may not infringe the patent rights
25 * of one or more patent holders. This license does not release you
26 * from the requirement that you obtain separate licenses from these
27 * patent holders to use this software.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
34 * BUT NOT LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF
35 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
36 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
37 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
38 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
39 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
40 */
41
42 #include <ctype.h>
43 #include <stdio.h>
44 #include <stdlib.h>
45 #include <string.h>
46 #include <time.h>
47 #include <limits.h>
48
49 #include "fru_tools.h"
50
51 /*
52 * This code is based from:
53 * Platform Management FRU Information
54 * Storage Definition
55 * Document Revision 1.1, September 27, 1999
56 * Available at zero cost at:
57 * http://download.intel.com/design/servers/ipmi/FRU1011.pdf
58 *
59 * ANSI/VITA 57.1
60 * FPGA Mezzanine Card (FMC) Standard
61 * Approved July 2008 (Revised February 2010)
62 * http://www.vita.com/fmc.html
63 * Available for a small fee
64 * https://vita.com/secure/online-store.html
65 * used with permission
66 * Most info is in section 5.5.1: IPMI Support
67 */
68
69 #if CHAR_BIT != 8
70 #error "unsupported char size"
71 #endif
72
73 /*
74 * FRU information uses a zero checksum everywhere.
75 * The modulo 256 sum of the preceding bytes (starting with the first byte
76 * of the header) plus the checksum byte equals zero.
77 * Platform Management FRU Information Storage Definition : section 16.2.[56]
78 */
79 unsigned char calc_zero_checksum (unsigned char *data, size_t len)
80 {
81 size_t i;
82 unsigned char tmp = 0;
83
84 for (i = 0; i <= len; i++)
85 tmp += data[i];
86
87 return tmp;
88 }
89
90 /*
91 * FRU Board Area Mfg. Date / Time is the
92 * number of _minutes_ from 0:00 hrs 01Jan1996
93 * Max is 0xFFFFFF (3 bytes worth) or
94 * 16777215 minutes; or
95 * 279620 hours, 15 minutes; or
96 * 11650 days, 20 hours, 15 minutes; or
97 * 31 years, 328 days, 7 hours, 56 minutes (assuming 525949 minutes in a year); or
98 * up to : Wed Nov 24 07:56 2027
99 * See:
100 * section 11, Platform Management FRU Information Storage Definition
101 */
102 time_t min2date(unsigned int mins)
103 {
104 struct tm tm;
105 time_t tmp;
106
107 /* Set up 01-Jan-1996 , and add the number of minutes to it */
108 memset(&tm, 0, sizeof(struct tm));
109 tm.tm_year = 96;
110 tm.tm_mday = 1;
111 tm.tm_min += mins;
112 tmp = mktime(&tm);
113 return tmp;
114 }
115
116 /*
117 * 6-bit ASCII Packing
118 * Platform Management FRU Information Storage Definition: Section 13.[23]
119 */
120 int ascii2six(unsigned char **dest, unsigned char *src, size_t size)
121 {
122 size_t i = 0;
123 ssize_t j;
124 unsigned int k, m = 0;
125 unsigned char *p, *d, *e;
126
127 if (!src || !size)
128 return 0;
129
130 e = d = calloc(1, size);
131
132 p = src;
133 /*
134 * 6-bit requires uppercase chars, between 0 and 0x3f
135 */
136 while (*p != '\0' && i <= size) {
137 j = toupper(*p) - 0x20;
138 if (j < 0 || j >= 0x40) {
139 printf_warn("%s : trying to convert a string '%s'\n"
140 "\t\twhich includes char '%c (0x%x)', which can't be converted\n",
141 __func__, src, *p, *p);
142 free(e);
143 return -1;
144 }
145 *d = j;
146 ++p, ++d, i++;
147 }
148
149 /* the length of dest, should be 3/4 of size, it's zero padded at the end */
150 *dest = calloc(1, size + 1);
151 p = *dest;
152
153 for (i = 0; i <= size ; i+= 4) {
154 k = e[i];
155 m++;
156 if ((i + 1) < size) {
157 k |= e[i + 1] << 6;
158 m++;
159 }
160 if ((i + 2) < size) {
161 k |= e[i + 2] << 12;
162 m++;
163 }
164 if ((i + 3) < size) {
165 k |= e[i + 3] << 18;
166 }
167 #if __BYTE_ORDER == __BIG_ENDIAN
168 k = __builtin_bswap32(k);
169 #endif
170 memcpy(p, &k, 3);
171 p += 3;
172 }
173
174 free (e);
175
176 return m;
177 }
178
179 /*
180 * * 6-bit ASCII Unpacking
181 * * Platform Management FRU Information Storage Definition: Section 13.[23]
182 */
183 unsigned char * six2ascii(unsigned char *buf, size_t size)
184 {
185 unsigned char *p, *dest;
186 size_t i;
187
188 if (!size)
189 return NULL;
190
191 /* the length of dest, should be 4/3 of size + 1 for null termination char*/
192 dest = calloc(1, ((size * 4) / 3) + 2);
193 p = dest;
194
195 for (i = 0; i < size; i += 3) {
196 *dest = (buf[i] & 0x3F) + 0x20;
197 /* printf("1: %i: 0x%x (%c)\n", i, *dest, *dest); */
198 dest++;
199 if ((i + 1) < size) {
200 *dest = ((buf[i] & 0xC0) >> 6 | (buf[i+1] & 0x0F) << 2) + 0x20;
201 /* printf("2: %i: 0x%x (%c)\n", i, *dest, *dest); */
202 dest++;
203 }
204 if ((i + 2) < size) {
205 *dest = ((buf[i+1] & 0xF0) >> 4 | (buf[i+2] & 0x03) << 4) + 0x20;
206 /* printf("3: %i: 0x%x (%c)\n", i, *dest, *dest); */
207 dest++;
208 }
209 if ((i + 3) < size) {
210 *dest = ((buf[i+2] & 0xFC) >> 2) + 0x20;
211 /* printf("4: %i: 0x%x (%c)\n", i, *dest, *dest); */
212 dest++;
213 }
214 }
215 /* make sure strings are null terminated */
216 *dest = 0;
217
218 /* Drop trailing spaces & null chars */
219 dest--;
220 while ((*dest == 0 || *dest == ' ') && size) {
221 *dest = 0;
222 dest--;
223 size--;
224 }
225
226 return p;
227 }
228
229 /*
230 * Extract strings from fields
231 * Section 13 TYPE/LENGTH BYTE FORMAT
232 * Platform Management FRU Information Storage Definition
233 */
234 unsigned int parse_string(unsigned char *p, unsigned char **str, const char * field)
235 {
236 size_t len, i, j;
237
238 len = p[0] & 0x3F;
239
240 if (!len) {
241 *str = calloc(1, 3);
242 *str[0] = (FRU_STRING_ASCII << 6);
243 return 1;
244 }
245
246 switch((p[0] >> 6) & 0x3 ) {
247 case FRU_STRING_BINARY:
248 /* binary or unspecified */
249 *str = calloc(1, len + 2);
250 memcpy(*str, p, len + 1);
251 break;
252 case FRU_STRING_BCD:
253 /* BCD plus */
254 printf_err("BCD - sorry\n");
255 break;
256 case FRU_STRING_SIXBIT:
257 /* 6-bit ASCII, packed */
258 {
259 unsigned char *tmp1, *tmp2;
260 size_t tlen;
261
262 tmp1 = six2ascii(&p[1], p[0]& 0x3F);
263 tlen = strlen((char *)tmp1);
264 *str = calloc(1, tlen + 2);
265
266 tmp2 = *str;
267 tmp2++;
268 memcpy(tmp2, tmp1, tlen + 1);
269 if (tlen > 0x3F)
270 tlen = 0x3F;
271
272 *str[0] = (FRU_STRING_ASCII << 6) | tlen;
273 free(tmp1);
274 }
275 break;
276 case FRU_STRING_ASCII:
277 /* 8-bit ASCII */
278 *str = calloc(1, len + 2);
279 memcpy(*str, p, len + 1);
280 for (i = 1; i < len; i++) {
281 if (p[i] < 0x20 || p[i] == 0x7F) {
282 printf_warn("Field '%s' marked as ASCII, but contains non-printable "
283 "characters:\n", field);
284 printf_warn(" Length/Type : 0x%x (length:%i; type:ASCII(%i)\n",
285 p[0], p[0] & 0x3F, FRU_STRING_ASCII);
286 printf_warn(" Contents : ");
287 for (j = 1; j < len + 1; j++)
288 printf_warn("0x%02x ", p[j]);
289 printf_warn(" |");
290 for (j = 0; j < len + 1; j++)
291 printf_warn("%c", ((p[j] < 32) || (p[j] >= 127)) ? '.': p[j]);
292 printf_warn("|\n");
293 }
294 }
295 break;
296 }
297 return len + 1;
298 }
299
300 /*
301 * Board Info Area Format
302 * Platform Management FRU Information Storage Definition: Section 11
303 */
304 struct BOARD_INFO * parse_board_area(unsigned char *data)
305 {
306
307 struct BOARD_INFO *fru;
308 unsigned char *p;
309 unsigned int len, i, j;
310
311 fru = calloc(1, sizeof(struct BOARD_INFO));
312
313 if (data[0] != 0x01) {
314 printf_err("Board Area Format Version mismatch: 0x%02x should be 0x01\n", data [0]);
315 goto err;
316 }
317
318 len = (data[1] * 8) - 1;
319 if (calc_zero_checksum(data, len)) {
320 printf_err("Board Area Checksum failed");
321 goto err;
322 }
323
324 if (data[2] != 0 && data[2] != 25) {
325 printf_err("Board Area is non-English - sorry: Lang code = %i\n", data[2]);
326 goto err;
327 }
328
329 len--;
330 while ((data[len] == 0x00) && (len != 0))
331 len--;
332 if (len == 0 || data[len] != 0xC1) {
333 printf_err("BOARD INFO not terminated properly, walking backwards len: "
334 "%i:0x%02x should be 0xC1\n", len, data[len]);
335 goto err;
336 }
337
338 fru->mfg_date = data[3] | (data[4] << 8) | (data[5] << 16);
339
340 p = &data[6];
341 len -= 6;
342
343 i = parse_string(p, &fru->manufacturer, "Manufacture");
344 p += i, len -= i;
345
346 i = parse_string(p, &fru->product_name, "Product Name");
347 p += i, len -= i;
348
349 i = parse_string(p, &fru->serial_number, "Serial Number");
350 p += i, len -= i;
351
352 i = parse_string(p, &fru->part_number, "Part Number");
353 p += i, len -= i;
354
355 i = parse_string(p, &fru->FRU_file_ID, "FRU File ID");
356 p += i, len -= i;
357
358 j = 0;
359 while (len != 0 && j < CUSTOM_FIELDS) {
360 i = parse_string(p, &fru->custom[j], "Custom Field");
361 p += i, len -= i, j++;
362 }
363
364 if (*p != 0xC1) {
365 printf_err("BOARD INFO not terminated properly, "
366 "offset %02i(0x%02x) : %02i(0x%02x) should be 0xC1\n",
367 p - data, p - data, *p, *p);
368 goto err;
369 }
370
371 return fru;
372
373 err:
374 free(fru->manufacturer);
375 free(fru->product_name);
376 free(fru->serial_number);
377 free(fru->part_number);
378 free(fru->FRU_file_ID);
379 for( j = 0; j < CUSTOM_FIELDS; j++)
380 free(fru->custom[j]);
381 free(fru);
382 return NULL;
383 }
384
385 /*
386 * Each record in this area begins with a pre-defined header as specified in the
387 * section 16 in the Platform Management FRU Information Storage Definition.
388 * This header contains a ‘Type’ field that identifies what information is
389 * contained in the record. * There are some FMC specific headers, defined
390 * in section 5.5.1 of the FMC specification "IPMI Support". These FMC specific
391 * sections have a 1 byte sub-type, and a 3 byte Unique Organization Identifier
392 */
393 struct MULTIRECORD_INFO * parse_multiboard_area(unsigned char *data)
394 {
395 int i = 0, tmp, type;
396 unsigned char *p;
397 struct MULTIRECORD_INFO *multi;
398
399 multi = calloc(1, sizeof(struct MULTIRECORD_INFO));
400
401 p = data;
402
403 do {
404 if (i != 0)
405 p += 5 + p[2];
406 if (p[0] >= 0x06 && p[0] <= 0xBF) {
407 printf_err("MultiRecord Area %i: Invalid Record Header\n", i);
408 return NULL;
409 }
410 if (calc_zero_checksum(p, 4)) {
411 printf_err("MultiRecord Area %i (Record Type 0x%x): "
412 "Header Checksum failed\n", i, p[0]);
413 return NULL;
414 }
415
416 if (!p[2] || ((calc_zero_checksum(p+5, p[2] - 1) + p[3]) & 0xFF)) {
417 printf_err("MultiRecord Area %i (Record Type 0x%x): "
418 "Record Checksum failed\n", i, p[0]);
419 return NULL;
420 }
421
422 /*
423 * Record Type ID
424 */
425 switch(p[0]) {
426 case MULTIRECORD_DC_OUTPUT:
427 case MULTIRECORD_DC_INPUT:
428 tmp = p[5] & 0xF;
429 if ((tmp) >= NUM_SUPPLIES)
430 printf_err("Too many Supplies defined in Multirecords\n");
431
432 multi->supplies[tmp] = calloc(1, p[2] + 6);
433 memcpy (multi->supplies[tmp], p, p[2] + 6);
434 multi->supplies[tmp][1] = multi->supplies[tmp][1] & 0x7F;
435 break;
436 case MULTIRECORD_FMC:
437 /*
438 * Use VITA's OUI: 0x0012a2 is specified in the FMC spec - Rule 5.77
439 */
440 if ((p[5] | p[6] << 8 | p[7] << 16) != VITA_OUI) {
441 printf_err("OUI Doesn't match : is 0x%06X, "
442 "should be 0x%06x\n", p[5] | p[6] << 8 | p[7] << 16, VITA_OUI);
443 }
444 /* type field is located: Header + Manufacturer ID = 5 + 3 */
445 type = p[8] >> 4;
446
447 switch (type) {
448 case MULTIRECORD_CONNECTOR:
449 /* see table 7 in FMC spec */
450 multi->connector = calloc(1, p[2] + 6);
451 memcpy (multi->connector, p, p[2] + 6);
452 /* This isn't the end 'til we re-assemble things */
453 multi->connector[1] = multi->connector[1] & 0x7F;
454 break;
455 case MULTIRECORD_I2C:
456 if (p[2] <= 5) {
457 printf_warn("I2C MultiRecord is too short (len:%i)\n"
458 " (at least 4 is needed for OUI and subtype)\n",
459 p[2]);
460 } else {
461 /* see table 9 in FMC spec */
462 unsigned char *foo2;
463 foo2 = six2ascii(&p[9], p[2] - 4);
464
465 multi->i2c_devices = calloc(1, strlen((char *)foo2) + 1);
466 strcpy ((char *)multi->i2c_devices, (char *)foo2);
467 free(foo2);
468 /* This isn't the end 'til we re-assemble things */
469 multi->i2c_devices[1] = multi->i2c_devices[1] & 0x7F;
470 }
471 break;
472 default:
473 printf_err("Unknown multirecord type : %i\n", type);
474 break;
475 }
476
477 if (type == 1) {
478
479 }
480 break;
481 default:
482 printf_err("Unknown MultiRecord Area\n");
483 }
484
485 i++;
486 } while (!(p[1] & 0x80));
487
488 return multi;
489 }
490
491 /*
492 * Common Header Format
493 * Section 8 in the Platform Management FRU Information Storage Definition
494 */
495 struct FRU_DATA * parse_FRU (unsigned char *data)
496 {
497 struct FRU_DATA *fru;
498
499 fru = calloc (1, sizeof(struct FRU_DATA));
500
501 /* Check FRU version */
502 if (data[0] != 0x01) {
503 printf_err("FRU Version number mismatch 0x%02x should be 0x01\n", data[0]);
504 goto err;
505 }
506
507 /* Check Padding */
508 if (data[6] != 0x00) {
509 printf_err("FRU byte 6 should be PAD, and be zero -- but it's not\n");
510 goto err;
511 }
512
513 /* Check header checksum */
514 if (calc_zero_checksum(data, 7)) {
515 printf_err("Common Header Checksum failed\n");
516 goto err;
517 }
518
519 /* Parse Internal Use Area */
520 if (data[1]) {
521 printf_err("Internal Use Area not yet implemented - sorry\n");
522 goto err;
523 }
524
525 /* Parse Chassis Info Area */
526 if (data[2]) {
527 printf_err("Chassis Info Area not yet implmented - sorry\n");
528 goto err;
529 }
530
531 /* Parse Board Area */
532 if (data[3]) {
533 fru->Board_Area = parse_board_area(&data[data[3] * 8]);
534 if (!fru->Board_Area)
535 goto err;
536 }
537
538 /* Parse Chassis Info Area */
539 if (data[4]) {
540 printf_err("Chassis Info Area parsing not yet implemented - sorry\n");
541 goto err;
542 }
543
544 /* Parse MultiRecord Area */
545 if (data[5])
546 fru->MultiRecord_Area = parse_multiboard_area(&data[data[5] * 8]);
547
548 return fru;
549
550 err:
551 free(fru);
552 return NULL;
553
554 }
555
556 void free_FRU(struct FRU_DATA *fru)
557 {
558 int j;
559
560 free(fru->Board_Area->manufacturer);
561 free(fru->Board_Area->product_name);
562 free(fru->Board_Area->serial_number);
563 free(fru->Board_Area->part_number);
564 free(fru->Board_Area->FRU_file_ID);
565 for(j = 0; j < CUSTOM_FIELDS; j++)
566 free(fru->Board_Area->custom[j]);
567 free(fru->Board_Area);
568
569 for(j = 0; j < NUM_SUPPLIES; j++)
570 free(fru->MultiRecord_Area->supplies[j]);
571 free(fru->MultiRecord_Area->i2c_devices);
572
573 free(fru->MultiRecord_Area->connector);
574 free(fru->MultiRecord_Area);
575
576 free(fru);
577
578 }
579
580 /*
581 * take string, and put in into the buffer
582 * return the number of bytes copied
583 */
584 static unsigned int insert_str(unsigned char *buf, const unsigned char * str, bool force)
585 {
586 int tmp;
587
588 if (TYPE_CODE(str) == FRU_STRING_ASCII)
589 tmp = strlen((const char *)&str[1]);
590 else
591 tmp = FIELD_LEN(str);
592
593 /* Turn ASCII into 6 bit if possible */
594 if ((TYPE_CODE(str) != FRU_STRING_ASCII) || (tmp <= 0x3F && !force)) {
595 /* It fits, so just leave it as ASCII/binary/whatever format it's in */
596 buf[0] = tmp | (TYPE_CODE(str) << 6);
597 memcpy(&buf[1], &str[1], tmp);
598 } else {
599 /* turn it into 6-bit ASCII */
600 unsigned char *six = NULL;
601
602 tmp = ascii2six(&six, (unsigned char *)&str[1], tmp);
603 if (tmp < 0) {
604 /* Counldn't encode things */
605 printf_warn("couldn't encode '%s' string\n", &str[1]);
606 tmp = strlen((const char *)&str[1]);
607 if (tmp > 0x3F) {
608 printf_warn("fail : %d\n", tmp);
609 printf_err("String too long to fit\n");
610 }
611 buf[0] = tmp | (FRU_STRING_ASCII << 6);
612 memcpy(&buf[1], &str[1], tmp);
613 return tmp + 1;
614 }
615 if (tmp > 0x3F) {
616 printf_warn("fail : %d\n", tmp);
617 printf_err("String too long to fit\n");
618 }
619
620 buf[0] = tmp | (FRU_STRING_SIXBIT << 6);
621 memcpy(&buf[1], six, tmp);
622 free(six);
623 }
624
625
626 return tmp + 1;
627 }
628
629 /*
630 * Takes the FRU structure, and builds the binary blob
631 */
632 unsigned char * build_FRU_blob (struct FRU_DATA *fru, size_t *length, bool packed)
633 {
634 unsigned char *p, *buf;
635 unsigned int st, len, tmp, last = 0, i = 0, j;
636
637 buf = calloc(1, 1024);
638 len = 256;
639
640 buf[0] = 0x01;
641 i = 8;
642 if (fru->Internal_Area)
643 printf_err("Internal Use Area not yet implemented - sorry\n");
644
645 if (fru->Chassis_Info)
646 printf_err("Chassis Info not yet implemented - sorry\n");
647
648 if (fru->Board_Area) {
649 len = st = i;
650 buf[3] = i / 8;
651 buf[i] = 0x1; /* Magic number */
652 /* buf[i+1] = length, which needs to be determined later */
653 buf[i+2] = 25; /* English */
654 buf[i+3] = (fru->Board_Area->mfg_date) & 0xFF;
655 buf[i+4] = (fru->Board_Area->mfg_date >> 8) & 0xFF;
656 buf[i+5] = (fru->Board_Area->mfg_date >> 16) & 0xFF;
657
658 i += 6;
659 i += insert_str(&buf[i], fru->Board_Area->manufacturer, packed);
660 i += insert_str(&buf[i], fru->Board_Area->product_name, packed);
661 i += insert_str(&buf[i], fru->Board_Area->serial_number, packed);
662 i += insert_str(&buf[i], fru->Board_Area->part_number, packed);
663 i += insert_str(&buf[i], fru->Board_Area->FRU_file_ID, packed);
664 for (j = 0; j < CUSTOM_FIELDS; j++) {
665 if (fru->Board_Area->custom[j]) {
666 i += insert_str(&buf[i], fru->Board_Area->custom[j], packed);
667 }
668 }
669 buf[i] = 0xC1;
670 i++;
671 i = (((i >> 3) + 1) << 3) - 1;
672 len = i - st;
673 buf[st + 1] = len / 8 + 1;
674 buf[i] = 256 - calc_zero_checksum(&buf[st], len);
675 i++;
676 }
677 if (fru->Product_Info) {
678 }
679 if (fru->MultiRecord_Area) {
680 st = i;
681 buf[5] = st / 8;
682 for (tmp = 0; tmp < NUM_SUPPLIES; tmp++) {
683 p = fru->MultiRecord_Area->supplies[tmp];
684 if (!p)
685 continue;
686 memcpy(&buf[i], p, p[2]+ 5);
687 last = i + 1;
688 i += p[2] + 5;
689 }
690 p = fru->MultiRecord_Area->connector;
691 if (p) {
692 memcpy(&buf[i], p, p[2]+ 5);
693 last = i + 1;
694 i += p[2] + 5;
695 }
696 p = fru->MultiRecord_Area->i2c_devices;
697 if (p) {
698 unsigned int len, oui = VITA_OUI;
699 unsigned char *six = NULL;
700 len = ascii2six(&six, p, strlen((char *)p));
701
702 /* Type ID, Record Format version, Length, checksum, checksum */
703 sprintf((char *)&buf[i], "%c%c%c%c%c", MULTIRECORD_FMC, 0x02, len + 4, 0, 0);
704 /* Store OUI */
705 #if __BYTE_ORDER == __BIG_ENDIAN
706 oui = __builtin_bswap32(oui);
707 #endif
708 memcpy(&buf[i+5], &oui, 3);
709 /* Subtype & version - see Table 9 in FMC spec*/
710 sprintf((char *)&buf[i+8], "%c", (0x1 << 4) | (0));
711 /* finally copy over the string */
712 memcpy(&buf[i+9], six, len);
713 /* Record Checksum */
714 buf[i+3] = 0x100 - calc_zero_checksum(&buf[i+5], len + 3);
715 /* Header Checksum */
716 buf[i+4] = 0x100 - calc_zero_checksum(&buf[i], 4);
717
718 last = i + 1;
719 i += len + 9;
720
721 free(six);
722 }
723 if (last) {
724 buf[last] = buf[last] | 0x80;
725 /* redo the header checksum, since we just changed the header :( */
726 buf[last + 3] = 0;
727 buf[last + 3] = 0x100 - calc_zero_checksum(&buf[last - 1], 4);
728 }
729 }
730 buf[7] = 256 - calc_zero_checksum(buf, 6);
731
732 *length = i;
733 return buf;
734 }