forked from ipmitool/frugen
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfru.c
1067 lines (922 loc) · 29.4 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
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/** @file
* @brief FRU information encoding functions
*
* Copyright (C) 2016-2021 Alexander Amelkin <[email protected]>
* SPDX-License-Identifier: LGPL-2.0-or-later OR Apache-2.0
*/
#include "fru.h"
#include "smbios.h"
#include <ctype.h>
#include <stdio.h>
#include <stdint.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stddef.h>
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#define _BSD_SOURCE
#include <endian.h>
#ifdef __STANDALONE__
#include <stdio.h>
#endif
#ifdef DEBUG
#undef DEBUG
#include <stdio.h>
#define DEBUG(f, args...) do { printf("%s:%d: ", __func__, __LINE__); printf(f,##args); } while(0)
#else
#define DEBUG(f, args...)
#endif
static bool autodetect = true;
void fru_set_autodetect(bool enable)
{
autodetect = enable;
}
/**
* Strip trailing spaces
*/
static inline void cut_tail(char *s)
{
int i;
for(i = strlen(s) - 1; i >= 0 && ' ' == s[i]; i--) s[i] = 0;
}
/** Copy a FRU area field to a buffer and return the field's size */
static inline uint8_t fru_field_copy(void *dest, const fru_field_t *fieldp)
{
memcpy(dest, (void *)fieldp, FRU_FIELDSIZE(fieldp->typelen));
return FRU_FIELDSIZE(fieldp->typelen);
}
/**
* Detect the most suitable encoding for the string and calculate the length as well
*
* @returns A FRU field type/length byte, as per IPMI FRU Storage Definition, if everything was ok, or an error code.
* @retval FRU_FIELD_EMPTY The \a data argument was NULL
* @retval FRU_FIELD_TERMINATOR The data exceeded the maximum length (63 bytes)
*
*/
static
uint8_t fru_get_typelen(int len, /**< [in] Length of the data or LEN_AUTO for pure text zero-terminated data */
const uint8_t *data) /**< [in] The input data */
{
uint8_t typelen = len;
int i;
if (!data)
return FRU_FIELD_EMPTY;
if (!len) {
len = strlen(data);
if (!len) {
return FRU_FIELD_EMPTY;
}
}
// If the data exceeds the maximum length, return a terminator
if (len > FRU_FIELDDATALEN(len)) {
DEBUG("Data exceeds maximum length\n");
return FRU_FIELD_TERMINATOR;
}
if (typelen) {
// They gave us a non-zero length. The data must be binary. Trust 'em and don't try to optimize.
DEBUG("Binary data due to non-zero length\n");
return FRU_TYPELEN(BINARY, len);
}
// As we reach this point, we know the data must be text.
// We will try to find the encoding that suits best.
if (autodetect) {
typelen = FRU_TYPELEN(BCDPLUS, (len + 1) / 2); // By default - the most range-restricted text type
DEBUG("Assuming BCD plus data...\n");
}
else {
DEBUG("Assuming ASCII data...\n");
typelen = FRU_TYPELEN(TEXT, len);
}
// Go through the data and expand charset as needed
for (i = 0; i < len; i++) {
if (data[i] < ' '
&& data[i] != '\t'
&& data[i] != '\r'
&& data[i] != '\n')
{
// They lied! The data is binary!
// That's the widest range type.
// There is no use in checking any further.
DEBUG("[%#02x] Binary data!\n", data[i]);
typelen = FRU_TYPELEN(BINARY, len);
break;
}
if (autodetect) {
if (typelen < FRU_MAKETYPE(TEXT)
&& (data[i] > '_' || data[i] < ' '))
{ // Do not reduce the range
// The data doesn't fit into 6-bit ASCII, expand to simple text.
DEBUG("[%c] Data is simple text!\n", data[i]);
typelen = FRU_TYPELEN(TEXT, len);
continue;
}
if (typelen < FRU_MAKETYPE(ASCII_6BIT) && // Do not reduce the range
!isdigit(data[i]) && data[i] != ' ' && data[i] != '-' && data[i] != '.')
{
// The data doesn't fit into BCD plus, expand to
DEBUG("[%c] Data is 6-bit ASCII!\n", data[i]);
typelen = FRU_TYPELEN(ASCII_6BIT, FRU_6BIT_LENGTH(len));
}
} /* autodetect */
}
return typelen;
}
/**
* Allocate a buffer and encode the input string into it as 6-bit ASCII
*
* @returns pointer to the newly allocated field buffer if allocation and encoding were successful
* @returns NULL if there was an error, sets errno accordingly (man malloc)
*/
static fru_field_t *fru_encode_6bit(const unsigned char *s /**< [in] Input string */)
{
int len = strlen(s);
int len6bit = FRU_6BIT_LENGTH(len);
int i, i6;
fru_field_t *out = NULL;
size_t outlen = sizeof(fru_field_t) + len6bit + 1; // 1 extra for null-byte
if (len6bit > FRU_FIELDDATALEN(len6bit) ||
!(out = calloc(1, outlen)))
{
return out;
}
out->typelen = FRU_TYPELEN(ASCII_6BIT, len6bit);
for (i = 0, i6 = 0; i < len && i6 < len6bit; i++) {
int base = i / 4; // Four original bytes get encoded into three 6-bit-packed ones
int byte = i % 4;
char c = (s[i] - ' ') & 0x3F; // Space is zero, maximum is 0x3F (6 significant bits)
DEBUG("%d:%d:%d = %c -> %02hhX\n", base, byte, i6, s[i], c);
switch(byte) {
case 0:
out->data[i6] = c;
break;
case 1:
out->data[i6] |= (c & 0x03) << 6; // Lower 2 bits go high into byte 0
out->data[++i6] = c >> 2; // Higher (4) bits go low into byte 1
break;
case 2:
out->data[i6++] |= c << 4; // Lower 4 bits go high into byte 1
out->data[i6] = c >> 4; // Higher 2 bits go low into byte 2
break;
case 3:
out->data[i6++] |= c << 2; // The whole 6-bit char goes high into byte 3
break;
}
}
return out;
}
/**
* Allocate a buffer and decode a 6-bit ASCII string from it
*/
static unsigned char *fru_decode_6bit(const fru_field_t *field)
{
unsigned char *out = NULL;
const unsigned char *s6;
int len, len6bit;
int i, i6;
if (!field) return out;
len6bit = FRU_FIELDDATALEN(field->typelen);
s6 = field->data;
len = FRU_6BIT_FULLLENGTH(len6bit);
if (!(out = calloc(1, len + 1))) {
return out;
}
DEBUG("Allocated a destination buffer at %p\n", out);
for(i = 0, i6 = 0; i6 <= len6bit && i < len && s6[i6]; i++) {
int base = i / 4;
int byte = i % 4;
DEBUG("%d:%d:%d = ", base, byte, i6);
switch(byte) {
case 0:
DEBUG("%02hhX ", s6[i6]);
out[i] = s6[i6] & 0x3F;
break;
case 1:
DEBUG("%02hhX %02hhX ", s6[i6], s6[i6 + 1]);
out[i] = (s6[i6] >> 6) | (s6[++i6] << 2);
break;
case 2:
DEBUG("%02hhX %02hhX ", s6[i6], s6[i6 + 1]);
out[i] = (s6[i6] >> 4) | (s6[++i6] << 4);
break;
case 3:
DEBUG("%02hhX ", s6[i6]);
out[i] = s6[i6++] >> 2;
break;
}
out[i] &= 0x3F;
out[i] += ' ';
DEBUG("-> %02hhx %c\n", out[i], out[i]);
}
// Strip trailing spaces that could emerge when decoding a
// string that was a byte shorter than a multiple of 4.
cut_tail(out);
return out;
}
/**
* Allocate a buffer and encode that data as per FRU specification
*/
fru_field_t * fru_encode_data(int len, const uint8_t *data)
{
int typelen;
fru_field_t *out;
typelen = fru_get_typelen(len, data);
if (FRU_FIELD_TERMINATOR == typelen)
return NULL; // Can't encode this data
if (FRU_ISTYPE(typelen, ASCII_6BIT)) {
out = fru_encode_6bit(data);
}
else {
if (!(out = malloc(FRU_FIELDSIZE(typelen) + 1))) // Plus 1 byte for null-terminator
return NULL;
out->typelen = typelen;
if (FRU_ISTYPE(typelen, BCDPLUS)) {
int i;
uint8_t c[2] = {0};
/* Copy the data and pack it as BCD */
for (i = 0; i < 2 * FRU_FIELDDATALEN(typelen); i++) {
switch(data[i]) {
case 0: // The null-terminator encountered earlier than end of BCD field, encode as space
case ' ':
c[i % 2] = 0xA;
break;
case '-':
c[i % 2] = 0xB;
break;
case '.':
c[i % 2] = 0xC;
break;
default: // Digits
c[i % 2] = data[i] - '0';
}
out->data[i / 2] = c[0] << 4 | c[1];
}
}
else {
memcpy(out->data, data, FRU_FIELDDATALEN(typelen));
}
out->data[FRU_FIELDDATALEN(typelen)] = 0; // Terminate the string (for safety)
}
return out;
}
/**
* Allocate a buffer and decode the data from it.
*
* For binary data use FRU_FIELDDATALEN(field->typelen) to find
* out the size of the returned buffer.
*/
static
unsigned char * fru_decode_data(const fru_field_t *field)
{
unsigned char * out;
if (!field) return NULL;
if (FRU_ISTYPE(field->typelen, ASCII_6BIT)) {
out = fru_decode_6bit(field);
}
else {
out = malloc(FRU_FIELDDATALEN(field->typelen) + 1);
if (!out) return NULL;
if (FRU_ISTYPE(field->typelen, BCDPLUS)) {
int i;
uint8_t c;
/* Copy the data and pack it as BCD */
for (i = 0; i < 2 * FRU_FIELDDATALEN(field->typelen); i++) {
c = (field->data[i / 2] >> ((i % 2) ? 0 : 4)) & 0x0F;
switch(c) {
case 0xA:
out[i] = ' ';
break;
case 0xB:
out[i] = '-';
break;
case 0xC:
out[i] = '.';
break;
default: // Digits
out[i] = c + '0';
}
}
out[2 * FRU_FIELDDATALEN(field->typelen)] = 0; // Terminate the string
// Strip trailing spaces that may have emerged when a string of odd
// length was BCD-encoded.
cut_tail(out);
}
else {
memcpy(out, field->data, FRU_FIELDDATALEN(field->typelen));
out[FRU_FIELDDATALEN(field->typelen)] = 0; // Terminate the string
}
}
return out;
}
#if 0
struct timeval {
time_t tv_sec; /* seconds */
suseconds_t tv_usec; /* microseconds */
};
struct timezone {
int tz_minuteswest; /* minutes west of Greenwich */
int tz_dsttime; /* type of DST correction */
};
gettimeofday time
#endif
/**
* Calculate zero checksum for command header and FRU areas
*/
static
uint8_t calc_checksum(void *blk, size_t blk_bytes)
{
if (!blk || blk_bytes == 0) {
printf("Null pointer or zero buffer length\n");
exit(1);
}
uint8_t *data = (uint8_t *)blk;
uint8_t checksum = 0;
for(int i = 0; i < blk_bytes; i++) {
checksum += data[i];
}
return (uint8_t)( -(int8_t)checksum);
}
/**
* Calculate an area checksum
*
* Calculation includes the checksum byte itself.
* For freshly prepared area this method returns a checksum to be stored in the last byte.
* For a pre-existing area this method returns zero if checksum is ok or non-zero otherwise.
*
*/
uint8_t fru_area_checksum(fru_info_area_t *area)
{
return calc_checksum(area, (area->blocks * FRU_BLOCK_SZ));
}
/**
* Allocate and build a FRU Information Area block of any type.
*
* The function will allocate a buffer of size that is a muliple of 8 bytes
* and is big enough to accomodate the standard area header corresponding to the
* requested area type, as well as all the supplied data fields, the require padding,
* and a checksum byte.
*
* The data fields will be taken as is and should be supplied pre-encoded in
* the standard FRU field format.
*
* It is safe to free (deallocate) the fields supplied to this function
* immediately after the call as all the data is copied to the new buffer.
*
* Don't forget to free() the returned buffer when you don't need it anymore.
*
* @returns fru_info_area_t *area A newly allocated buffer containing the created area
*
*/
static
fru_info_area_t *fru_create_info_area(fru_area_type_t atype, ///< [in] Area type (FRU_[CHASSIS|BOARD|PRODUCT]_INFO)
uint8_t langtype, ///< [in] Language code for areas that use it (board, product) or Chassis Type for chassis info area
const struct timeval *tv, ///< [in] Manufacturing time since the Epoch (1970/01/01 00:00:00 +0000 UTC) for areas that use it (board)
fru_reclist_t *fields, ///< [in] Single-linked list of data fields
size_t nstrings, ///< [in] Number of strings for mandatory fields
const unsigned char *strings[]) ///<[in] Array of strings for mandatory fields
{
int i = 0;
int field_count;
int typelen;
int padding_size;
fru_board_area_t header = { // Allocate the biggest possible header
.ver = FRU_VER_1,
};
int headerlen = FRU_INFO_AREA_HEADER_SZ; // Assume a smallest possible header for a generic info area
void *out = NULL;
uint8_t *outp;
fru_reclist_t *field = fields;
int totalsize = 2; // A generic info area has a custom fields terminator and a checksum
if (!FRU_AREA_IS_GENERIC(atype)) {
errno = EINVAL; // This function doesn't support multirecord or internal use areas
goto err;
}
header.langtype = langtype;
if (FRU_AREA_HAS_DATE(atype)) {
uint32_t fru_time;
struct tm tm_1996 = {
.tm_year = 96,
.tm_mon = 0,
.tm_mday = 1
};
const struct timeval tv_unspecified = { 0 };
struct timeval tv_1996 = { 0 };
if (!tv) {
errno = EFAULT;
goto err;
}
/*
* It's assumed here that UNIX time 0 (Jan 1st of 1970)
* can never actually happen in a FRU file in 2018.
*/
if (!memcmp(&tv_unspecified, tv, sizeof(tv))) {
printf("Using FRU_DATE_UNSPECIFIED\n");
fru_time = FRU_DATE_UNSPECIFIED;
} else {
// The argument to mktime is zoneless
tv_1996.tv_sec = mktime(&tm_1996);
// FRU time is in minutes and we don't care about microseconds
fru_time = (tv->tv_sec - tv_1996.tv_sec) / 60;
}
header.mfgdate[0] = fru_time & 0xFF;
header.mfgdate[1] = (fru_time >> 8) & 0xFF;
header.mfgdate[2] = (fru_time >> 16) & 0xFF;
headerlen = FRU_DATE_AREA_HEADER_SZ; // Expand the header size
}
DEBUG("headerlen is %d\n", headerlen);
totalsize += headerlen;
/* Find uninitialized mandatory fields, allocate and initialize them with provided strings */
for (field_count = 0, field = fields;
field && !field->rec && field_count < nstrings;
field = field->next, field_count++)
{
field->rec = fru_encode_data(LEN_AUTO, strings[field_count]);
if (!field->rec) goto err;
}
/* Now calculate the total size of all initialized (mandatory and custom) fields */
for (field = &fields[0]; field && field->rec; field = field->next) {
totalsize += FRU_FIELDSIZE(field->rec->typelen);
}
header.blocks = FRU_BLOCKS(totalsize); // Round up to multiple of 8 bytes
padding_size = header.blocks * FRU_BLOCK_SZ - totalsize;
out = calloc(1, FRU_BYTES(header.blocks)); // This will be returned and freed by the caller
outp = out;
if (!out) goto err;
// Now fill the output buffer. First copy the header.
memcpy(outp, &header, headerlen);
outp += headerlen;
DEBUG("area size is %d (%d) bytes\n", totalsize, FRU_BYTES(header.blocks));
DEBUG("area size in header is (%d) bytes\n", FRU_BYTES(((fru_info_area_t *)out)->blocks));
// Add the data fields
for (field = fields; field && field->rec; field = field->next) {
outp += fru_field_copy(outp, field->rec);
}
// Terminate the data fields, add padding and checksum
*outp = FRU_FIELD_TERMINATOR;
outp += 1 + padding_size;
*outp = fru_area_checksum(out);
DEBUG("area size is %d (%d) bytes\n", totalsize, FRU_BYTES(header.blocks));
DEBUG("area size in header is (%d) bytes\n", FRU_BYTES(((fru_info_area_t *)out)->blocks));
err:
/*
* Free the allocated mandatory fields. Either an error has occured or the fields
* have already been copied into the output buffer. Anyway, they aren't needed anymore
*/
for (--field_count; field_count >= 0; field_count--) {
free(fields[field_count].rec);
}
return out;
}
/**
* Allocate and build a Chassis Information Area block.
*
* The function will allocate a buffer of size that is a muliple of 8 bytes
* and is big enough to accomodate the standard area header, all the mandatory
* fields, all the supplied custom fields, the required padding and a checksum byte.
*
* The mandatory fields will be encoded as fits best.
* The custom fields will be used as is (pre-encoded).
*
* It is safe to free (deallocate) any arguments supplied to this function
* immediately after the call as all the data is copied to the new buffer.
*
* Don't forget to free() the returned buffer when you don't need it anymore.
*
* @returns fru_info_area_t *area A newly allocated buffer containing the created area
*
*/
fru_chassis_area_t * fru_chassis_info(const fru_exploded_chassis_t *chassis) ///< [in] Exploded chassis info area
{
int i;
if(!chassis) {
errno = EFAULT;
return NULL;
}
fru_reclist_t fields[] = { // List of fields. Mandatory fields are unallocated yet.
[FRU_CHASSIS_PARTNO] = { NULL, &fields[FRU_CHASSIS_SERIAL] },
[FRU_CHASSIS_SERIAL] = { NULL, chassis->cust },
};
const unsigned char *strings[] = { chassis->pn, chassis->serial };
fru_chassis_area_t *out = NULL;
if (!SMBIOS_CHASSIS_IS_VALID(chassis->type)) {
errno = EINVAL;
return NULL;
}
out = fru_create_info_area(FRU_CHASSIS_INFO,
chassis->type, NULL, fields,
ARRAY_SZ(strings), strings);
return out;
}
/**
* Allocate and build a Board Information Area block.
*
* The function will allocate a buffer of size that is a muliple of 8 bytes
* and is big enough to accomodate the standard area header, all the mandatory
* fields, all the supplied custom fields, the required padding and a checksum byte.
*
* The mandatory fields will be encoded as fits best.
* The custom fields will be used as is (pre-encoded).
*
* It is safe to free (deallocate) any arguments supplied to this function
* immediately after the call as all the data is copied to the new buffer.
*
* Don't forget to free() the returned buffer when you don't need it anymore.
*
* @returns fru_info_area_t *area A newly allocated buffer containing the created area
*
*/
fru_board_area_t * fru_board_info(const fru_exploded_board_t *board) ///< [in] Exploded board information area
{
int i;
if(!board) {
errno = EFAULT;
return NULL;
}
fru_reclist_t fields[] = { // List of fields. Mandatory fields are unallocated yet.
[FRU_BOARD_MFG] = { NULL, &fields[FRU_BOARD_PRODNAME] },
[FRU_BOARD_PRODNAME] = { NULL, &fields[FRU_BOARD_SERIAL] },
[FRU_BOARD_SERIAL] = { NULL, &fields[FRU_BOARD_PARTNO] },
[FRU_BOARD_PARTNO] = { NULL, &fields[FRU_BOARD_FILE] },
[FRU_BOARD_FILE] = { NULL, board->cust },
};
const unsigned char *strings[] = { board->mfg, board->pname, board->serial, board->pn, board->file };
fru_board_area_t *out = NULL;
out = (fru_board_area_t *)fru_create_info_area(FRU_BOARD_INFO,
board->lang, &board->tv, fields,
ARRAY_SZ(strings), strings);
return out;
}
/**
* Allocate and build a Product Information Area block.
*
* The function will allocate a buffer of size that is a muliple of 8 bytes
* and is big enough to accomodate the standard area header, all the mandatory
* fields, all the supplied custom fields, the required padding and a checksum byte.
*
* The mandatory fields will be encoded as fits best.
* The custom fields will be used as is (pre-encoded).
*
* It is safe to free (deallocate) any arguments supplied to this function
* immediately after the call as all the data is copied to the new buffer.
*
* Don't forget to free() the returned buffer when you don't need it anymore.
*
* @returns fru_info_area_t *area A newly allocated buffer containing the created area
*
*/
fru_product_area_t * fru_product_info(const fru_exploded_product_t *product) ///< [in] Exploded product information area
{
int i;
if(!product) {
errno = EFAULT;
return NULL;
}
fru_reclist_t fields[] = { // List of fields. Mandatory fields are unallocated yet.
[FRU_PROD_MFG] = { NULL, &fields[FRU_PROD_NAME] },
[FRU_PROD_NAME] = { NULL, &fields[FRU_PROD_MODELPN] },
[FRU_PROD_MODELPN] = { NULL, &fields[FRU_PROD_VERSION] },
[FRU_PROD_VERSION] = { NULL, &fields[FRU_PROD_SERIAL] },
[FRU_PROD_SERIAL] = { NULL, &fields[FRU_PROD_ASSET] },
[FRU_PROD_ASSET] = { NULL, &fields[FRU_PROD_FILE] },
[FRU_PROD_FILE] = { NULL, product->cust },
};
const unsigned char *strings[] = { product->mfg, product->pname,
product->pn, product->ver,
product->serial, product->atag,
product->file };
fru_product_area_t *out = NULL;
out = fru_create_info_area(FRU_PRODUCT_INFO,
product->lang, NULL, fields,
ARRAY_SZ(strings), strings);
return out;
}
/**
* Take an input string, check that it looks like UUID, and pack it into
* an "exploded" multirecord area record in binary form.
*
* @returns An errno-like negative error code
* @retval 0 Success
* @retval EINVAL Invalid UUID string (wrong length, wrong symbols)
* @retval EFAULT Invalid pointer
* @retval >0 any other error that calloc() is allowed to retrun
*/
int fru_mr_uuid2rec(fru_mr_rec_t **rec, const unsigned char *str)
{
size_t len;
fru_mr_mgmt_rec_t *mgmt = NULL;
const int UUID_SIZE = 16;
const int UUID_STRLEN_NONDASHED = UUID_SIZE * 2; // 2 hex digits for byte
const int UUID_STRLEN_DASHED = UUID_STRLEN_NONDASHED + 4;
union __attribute__((packed)) {
uint8_t raw[UUID_SIZE];
// The structure is according to DMTF SMBIOS 3.2 Specification
struct __attribute__((packed)) {
// All words and dwords here must be Little-Endian for SMBIOS
uint32_t time_low;
uint16_t time_mid;
uint16_t time_hi_and_version;
uint8_t clock_seq_hi_and_reserved;
uint8_t clock_seq_low;
uint8_t node[6];
};
} uuid;
// Need a valid non-allocated record pointer and a string
if (!rec || *rec) return -EFAULT;
if (!str) return -EFAULT;
len = strlen(str);
if(UUID_STRLEN_DASHED != len && UUID_STRLEN_NONDASHED != len) {
return -EINVAL;
}
mgmt = calloc(1, sizeof(fru_mr_mgmt_rec_t) + UUID_SIZE);
if (!mgmt) return errno;
mgmt->hdr.type_id = FRU_MR_MGMT_ACCESS;
mgmt->hdr.eol_ver = FRU_MR_VER;
mgmt->hdr.len = UUID_SIZE + 1; // Include the subtype byte
mgmt->subtype = FRU_MR_MGMT_SYS_UUID;
while(*str) {
static size_t i = 0;
int val;
// Skip dashes
if ('-' == *str) {
++str;
continue;
}
if (!isxdigit(*str)) {
free(mgmt);
return -EINVAL;
}
val = toupper(*str);
if (val < 'A')
val = val - '0';
else
val = val - 'A' + 0xA;
if (0 == i % 2)
uuid.raw[i / 2] = val << 4;
else
uuid.raw[i / 2] |= val;
++i;
++str;
}
// Ensure Little-Endian encoding for SMBIOS specification compatibility
uuid.time_low = htole32(be32toh(uuid.time_low));
uuid.time_mid = htole16(be16toh(uuid.time_mid));
uuid.time_hi_and_version = htole16(be16toh(uuid.time_hi_and_version));
memcpy(mgmt->data, uuid.raw, UUID_SIZE);
*rec = (fru_mr_rec_t *)mgmt;
// Checksum the data
mgmt->hdr.rec_checksum = calc_checksum((*rec)->data, mgmt->hdr.len);
// Checksum the header, don't include the checksum byte itself
mgmt->hdr.hdr_checksum = calc_checksum(*rec, sizeof(fru_mr_header_t) - 1);
return 0;
}
/**
* Allocate a new multirecord reclist entry and add it to \a reclist,
* set \a reclist to point to the newly allocated entry if
* \a reclist was NULL.
*
* @returns Pointer to the added entry
*/
fru_mr_reclist_t *add_mr_reclist(fru_mr_reclist_t **reclist)
{
fru_mr_reclist_t *rec;
fru_mr_reclist_t *reclist_ptr = *reclist;
rec = calloc(1, sizeof(*rec));
if(!rec) return NULL;
// If the reclist is empty, update it
if(!reclist_ptr) {
*reclist = rec;
} else {
// If the reclist is not empty, find the last entry and append the new one as next
while(reclist_ptr->next)
reclist_ptr = reclist_ptr->next;
reclist_ptr->next = rec;
}
return rec;
}
/**
* Allocate and build a MultiRecord area block.
*
* The function will allocate a buffer of size that is required to store all
* the provided data and accompanying record headers. It will calculate data
* and header checksums automatically.
*
* All data will be copied as-is, without any additional encoding.
*
* It is safe to free (deallocate) any arguments supplied to this function
* immediately after the call as all the data is copied to the new buffer.
*
* Don't forget to free() the returned buffer when you don't need it anymore.
*
* @returns fru_mr_area_t *area A newly allocated buffer containing the created area
*
*/
fru_mr_area_t *fru_mr_area(fru_mr_reclist_t *reclist, size_t *total)
{
fru_mr_area_t *area = NULL;
fru_mr_rec_t *rec;
fru_mr_reclist_t *listitem = reclist;
// Calculate the cumulative size of all records
while (listitem && listitem->rec && listitem->rec->hdr.len) {
*total += sizeof(fru_mr_header_t);
*total += listitem->rec->hdr.len;
listitem = listitem->next;
}
area = calloc(1, *total);
if (!area) {
*total = 0;
return NULL;
}
// Walk the input records and pack them into an MR area
listitem = reclist;
rec = area;
while (listitem && listitem->rec && listitem->rec->hdr.len) {
size_t rec_sz = sizeof(fru_mr_header_t) + listitem->rec->hdr.len;
memcpy(rec, listitem->rec, rec_sz);
if (!listitem->next) {
// Update the header and its checksum. Don't include the
// checksum byte itself.
size_t checksum_span = sizeof(fru_mr_header_t) - 1;
rec->hdr.eol_ver |= FRU_MR_EOL;
rec->hdr.hdr_checksum = calc_checksum(rec, checksum_span);
}
rec = (void *)rec + rec_sz;
listitem = listitem->next;
}
return area;
}
/**
* Create a FRU information file.
*
* @param[in] area The array of 5 areas, each may be NULL.
* Areas must be given in the FRU order, which is:
* internal use, chassis, board, product, multirecord
* @param[out] size On success, the size of the newly created FRU information buffer, in 8-byte blocks
*
* @returns fru_t * buffer, a newly allocated buffer containing the created FRU information file
*/
fru_t * fru_create(fru_area_t area[FRU_MAX_AREAS], size_t *size)
{
fru_t fruhdr = { .ver = FRU_VER_1 };
int totalblocks = FRU_BLOCKS(sizeof(fru_t)); // Start with just the header size
int area_offsets[FRU_MAX_AREAS] = { // Indices must match values of fru_area_type_t
offsetof(fru_t, internal),
offsetof(fru_t, chassis),
offsetof(fru_t, board),
offsetof(fru_t, product),
offsetof(fru_t, multirec)
};
fru_t *out = NULL;
int i;
// First calculate the total size of the FRU information storage file to be allocated.
for(i = 0; i < FRU_MAX_AREAS; i++) {
uint8_t atype = area[i].atype;
uint8_t blocks = area[i].blocks;
fru_info_area_t *data = area[i].data;
// Area type must be valid and match the index
if (!FRU_IS_ATYPE_VALID(atype) || atype != (uint8_t)FRU_AREA_NOT_PRESENT && atype != i) {
errno = EINVAL;
return NULL;
}
int area_offset_index = area_offsets[atype];
uint8_t *offset = (uint8_t *)&fruhdr + area_offset_index;
if(!data || // No data is provided or
!FRU_AREA_HAS_SIZE(atype) && !blocks || // no size is given for a non-sized area or
!((fru_info_area_t *)data)->blocks // the sized area contains a zero size
) {
// Mark the area as
*offset = 0;
continue;
}
if(!blocks) {
blocks = data->blocks;
area[i].blocks = blocks;
}
*offset = totalblocks;
totalblocks += blocks;
}
// Calcute header checksum
fruhdr.hchecksum = calc_checksum(&fruhdr, sizeof(fruhdr));
out = calloc(1, FRU_BYTES(totalblocks));
DEBUG("alocated a buffer at %p\n", out);
if (!out) return NULL;
memcpy(out, (uint8_t *)&fruhdr, sizeof(fruhdr));
// Now go through the areas again and copy them into the allocated buffer.
// We have all the offsets and sizes set in the previous loop.
for(i = 0; i < FRU_MAX_AREAS; i++) {
uint8_t atype = area[i].atype;
uint8_t blocks = area[i].blocks;
uint8_t *data = area[i].data;
int area_offset_index = area_offsets[atype];
uint8_t *offset = (uint8_t *)&fruhdr + area_offset_index;
uint8_t *dst = (void *)out + FRU_BYTES(*offset);
if (!blocks) continue;
DEBUG("copying %d bytes of area of type %d to offset 0x%03X (0x%03lX)\n",
FRU_BYTES(blocks), atype, FRU_BYTES(*offset), dst - (uint8_t *)out
);
memcpy(dst, data, FRU_BYTES(blocks));
}
*size = totalblocks;
return out;
}
#ifdef __STANDALONE__
void dump(int len, const unsigned char *data)
{
int i;
printf("Data Dump:");
for (i = 0; i < len; i++) {
if (!(i % 16)) printf("\n%04X: ", i);
printf("%02X ", data[i]);
}
printf("\n");
}
void test_encodings(void)
{
int i, len;
uint8_t typelen;
unsigned char *test_strings[] = {
/* 6-bit ASCII */
"IPMI", "OK!",
/* BCD plus */
"1234-56-7.89 01",
/* Simple text */
"This is a simple text, with punctuation & other stuff",
/* Binary */
"\x00\x01\x02\x03\x04\x05 BINARY TEST"
};
unsigned char *test_types[] = {
"6-bit", "6-bit",
"BCPplus",
"Simple text",
"Binary"
};
int test_lengths[] = { LEN_AUTO, LEN_AUTO, LEN_AUTO, LEN_AUTO, 18 };
for(i = 0; i < ARRAY_SZ(test_strings); i++) {
fru_field_t *field;
const unsigned char *out;
printf("Data set %d.\n", i);
printf("Original data ");
if (test_lengths[i]) dump(test_lengths[i], test_strings[i]);
else printf(": [%s]\n", test_strings[i]);