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