Skip to content

Commit

Permalink
Apply review edits
Browse files Browse the repository at this point in the history
  • Loading branch information
Sergey Matsievskiy committed Jan 12, 2023
1 parent 457bffe commit 28aad59
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 88 deletions.
139 changes: 64 additions & 75 deletions fru.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,11 @@
static bool autodetect = true;

const char* enc_names[TOTAL_FIELD_TYPES] = {
[FIELD_TYPE_AUTO] = "auto",
[FIELD_TYPE_BINARY] = "binary",
[FIELD_TYPE_BCDPLUS] = "bcdplus",
[FIELD_TYPE_SIXBITASCII] = "6bitascii",
[FIELD_TYPE_TEXT] = "text"
[FIELD_TYPE_AUTO] = "auto",
[FIELD_TYPE_BINARY] = "binary",
[FIELD_TYPE_BCDPLUS] = "bcdplus",
[FIELD_TYPE_SIXBITASCII] = "6bitascii",
[FIELD_TYPE_TEXT] = "text"
};

void fru_set_autodetect(bool enable)
Expand Down Expand Up @@ -100,7 +100,7 @@ static
uint8_t fru_get_typelen(int len, /**< [in] Length of the data,
LEN_AUTO for pure text zero-terminated data or
one of LEN_BCDPLUS, LEN_6BITASCII, LEN_TEXT for explicit text type */
const uint8_t *data) /**< [in] The input data */
const uint8_t *data) /**< [in] The input data */
{
uint8_t typelen = len;
int i;
Expand All @@ -111,16 +111,16 @@ uint8_t fru_get_typelen(int len, /**< [in] Length of the data,
if (len < 0) {
DEBUG("Forcing string '%s' to ...\n", (char *)data);
// Explicit text type
if (len == LEN_BCDPLUS) {
if (len == LEN_BCDPLUS) {
DEBUG("BCDPLUS type\n");
return FRU_TYPELEN(BCDPLUS, (strlen(data) + 1) / 2);
} else if (len == LEN_6BITASCII) {
} else if (len == LEN_6BITASCII) {
DEBUG("6BIT ASCII type\n");
return FRU_TYPELEN(ASCII_6BIT, FRU_6BIT_LENGTH(strlen(data)));
} else if (len == LEN_TEXT) {
} else if (len == LEN_TEXT) {
DEBUG("ASCII type\n");
return FRU_TYPELEN(TEXT, strlen(data));
} else {
} else {
DEBUG("Nothing... Unknown text type\n");
return FRU_FIELD_TERMINATOR;
}
Expand Down Expand Up @@ -211,8 +211,7 @@ static fru_field_t *fru_encode_6bit(const unsigned char *s /**< [in] Input strin
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)))
if (len6bit > FRU_FIELDDATALEN(len6bit) || !(out = calloc(1, outlen)))
{
return out;
}
Expand Down Expand Up @@ -257,8 +256,8 @@ static fru_field_t *fru_encode_6bit(const unsigned char *s /**< [in] Input strin
*/
static
bool fru_decode_6bit(const fru_field_t *field,
uint8_t *out,
size_t out_len)
uint8_t *out,
size_t out_len)
{
const unsigned char *s6;
int len, len6bit;
Expand Down Expand Up @@ -321,8 +320,8 @@ bool fru_decode_6bit(const fru_field_t *field,
*/
static
bool fru_decode_bcdplus(const fru_field_t *field,
uint8_t *out,
size_t out_len)
uint8_t *out,
size_t out_len)
{
int i;
uint8_t c;
Expand All @@ -333,25 +332,21 @@ bool fru_decode_bcdplus(const fru_field_t *field,
c = (field->data[i / 2] >> ((i % 2) ? 0 : 4)) & 0x0F;
switch (c) {
case 0xA:
out[i] = ' ';
break;
out[i] = ' ';
break;
case 0xB:
out[i] = '-';
break;
out[i] = '-';
break;
case 0xC:
out[i] = '.';
break;
out[i] = '.';
break;
case 0xD:
out[i] = '?';
break;
case 0xE:
out[i] = '?';
break;
case 0xF:
out[i] = '?';
break;
out[i] = '?';
break;
default: // Digits
out[i] = c + '0';
out[i] = c + '0';
}
}
out[2 * FRU_FIELDDATALEN(field->typelen)] = 0; // Terminate the string
Expand All @@ -373,8 +368,8 @@ bool fru_decode_bcdplus(const fru_field_t *field,
*/
static
bool fru_decode_binary(const fru_field_t *field,
uint8_t *out,
size_t out_len)
uint8_t *out,
size_t out_len)
{
int i;
uint8_t c;
Expand Down Expand Up @@ -452,8 +447,8 @@ fru_field_t * fru_encode_data(int len, const uint8_t *data)
}

bool fru_decode_data(fru_field_t *field,
typed_field_t *out,
size_t out_len)
typed_field_t *out,
size_t out_len)
{
if (!field) return false;

Expand All @@ -464,16 +459,16 @@ bool fru_decode_data(fru_field_t *field,
if (out_len < (FRU_FIELDDATALEN(field->typelen) + 1))
return false;

if (FRU_ISTYPE(field->typelen, BCDPLUS)) {
if (FRU_ISTYPE(field->typelen, BCDPLUS)) {
out->type = FIELD_TYPE_BCDPLUS;
return fru_decode_bcdplus(field, out->val, out_len);
} else {
} else {
out->type = FIELD_TYPE_TEXT;
memcpy(out->val, field->data, FRU_FIELDDATALEN(field->typelen));
out->val[FRU_FIELDDATALEN(field->typelen)] = 0; // Terminate the string
return true;
}
}
}
}
}

#if 0
Expand Down Expand Up @@ -545,11 +540,11 @@ uint8_t fru_area_checksum(fru_info_area_t *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 typed_field_t strings[]) ///<[in] Array of typed strings for mandatory fields
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 typed_field_t strings[]) ///<[in] Array of typed strings for mandatory fields
{
int i = 0;
int field_count;
Expand Down Expand Up @@ -684,7 +679,7 @@ static bool fru_decode_custom_fields(const uint8_t *data, fru_reclist_t **reclis
field = (fru_field_t*)data;

// end of fields
if (field->typelen == 0xc1)
if (field->typelen == FRU_TYPE_EOF)
break;

fru_reclist_t *custom_field = add_reclist(reclist);
Expand All @@ -695,18 +690,15 @@ static bool fru_decode_custom_fields(const uint8_t *data, fru_reclist_t **reclis
custom_field->rec = calloc(1, FRU_FIELDMAXARRAY);
custom_field->rec->typelen = field->typelen;
switch (FRU_TYPE(field->typelen)) {
case __TYPE_BINARY: {
case __TYPE_BINARY:
fru_decode_binary(field, custom_field->rec->data, FRU_FIELDMAXLEN);
break;
}
case __TYPE_ASCII_6BIT: {
case __TYPE_ASCII_6BIT:
fru_decode_6bit(field, custom_field->rec->data, FRU_FIELDMAXLEN);
break;
}
case __TYPE_BCDPLUS: {
case __TYPE_BCDPLUS:
fru_decode_bcdplus(field, custom_field->rec->data, FRU_FIELDMAXLEN);
break;
}
default:
memcpy(custom_field->rec->data, field->data, length);
custom_field->rec->data[length] = 0; // Terminate the string
Expand Down Expand Up @@ -759,8 +751,8 @@ fru_chassis_area_t * fru_encode_chassis_info(const fru_exploded_chassis_t *chass
}

out = fru_create_info_area(FRU_CHASSIS_INFO,
chassis->type, NULL, fields,
ARRAY_SZ(strings), strings);
chassis->type, NULL, fields,
ARRAY_SZ(strings), strings);

return out;
}
Expand Down Expand Up @@ -829,16 +821,14 @@ fru_board_area_t * fru_encode_board_info(const fru_exploded_board_t *board) ///<
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);
board->lang, &board->tv, fields,
ARRAY_SZ(strings), strings);

return out;
}

bool fru_decode_board_info(
const fru_board_area_t *area,
fru_exploded_board_t *board_out
)
bool fru_decode_board_info(const fru_board_area_t *area,
fru_exploded_board_t *board_out)
{
fru_field_t *field;
const uint8_t *data = area->data;
Expand All @@ -849,8 +839,7 @@ bool fru_decode_board_info(
union {
uint32_t val;
uint8_t arr[4];
} min_since_1996_big_endian = {0};
min_since_1996_big_endian.val = 0;
} min_since_1996_big_endian = { 0 };
min_since_1996_big_endian.arr[1] = area->mfgdate[2];
min_since_1996_big_endian.arr[2] = area->mfgdate[1];
min_since_1996_big_endian.arr[3] = area->mfgdate[0];
Expand Down Expand Up @@ -930,15 +919,16 @@ fru_product_area_t * fru_encode_product_info(const fru_exploded_product_t *produ
[FRU_PROD_FILE] = { NULL, product->cust },
};

const typed_field_t strings[] = { product->mfg, product->pname,
product->pn, product->ver,
product->serial, product->atag,
product->file };
const typed_field_t 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);
product->lang, NULL, fields,
ARRAY_SZ(strings), strings);

return out;
}
Expand Down Expand Up @@ -1134,43 +1124,43 @@ bool fru_decode_product_info(

field = (fru_field_t*)data;
if (!fru_decode_data(field, &product_out->mfg,
sizeof(product_out->mfg.val)))
sizeof(product_out->mfg.val)))
return false;
data += FRU_FIELDSIZE(field->typelen);

field = (fru_field_t*)data;
if (!fru_decode_data(field, &product_out->pname,
sizeof(product_out->pname.val)))
sizeof(product_out->pname.val)))
return false;
data += FRU_FIELDSIZE(field->typelen);

field = (fru_field_t*)data;
if (!fru_decode_data(field, &product_out->pn,
sizeof(product_out->pn.val)))
sizeof(product_out->pn.val)))
return false;
data += FRU_FIELDSIZE(field->typelen);

field = (fru_field_t*)data;
if (!fru_decode_data(field, &product_out->ver,
sizeof(product_out->ver.val)))
sizeof(product_out->ver.val)))
return false;
data += FRU_FIELDSIZE(field->typelen);

field = (fru_field_t*)data;
if (!fru_decode_data(field, &product_out->serial,
sizeof(product_out->serial.val)))
sizeof(product_out->serial.val)))
return false;
data += FRU_FIELDSIZE(field->typelen);

field = (fru_field_t*)data;
if (!fru_decode_data(field, &product_out->atag,
sizeof(product_out->atag.val)))
sizeof(product_out->atag.val)))
return false;
data += FRU_FIELDSIZE(field->typelen);

field = (fru_field_t*)data;
if (!fru_decode_data(field, &product_out->file,
sizeof(product_out->file.val)))
sizeof(product_out->file.val)))
return false;
data += FRU_FIELDSIZE(field->typelen);

Expand Down Expand Up @@ -1258,8 +1248,7 @@ fru_t * fru_create(fru_area_t area[FRU_MAX_AREAS], size_t *size)
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
);
FRU_BYTES(blocks), atype, FRU_BYTES(*offset), dst - (uint8_t *)out);
memcpy(dst, data, FRU_BYTES(blocks));
}

Expand Down Expand Up @@ -1389,7 +1378,7 @@ void test_encodings(void)
printf("Decoding... ");

if (!fru_decode_data(field->&typelen, &field->data,,
FRU_FIELDMAXARRAY)) {
FRU_FIELDMAXARRAY)) {
printf("FAIL!");
goto next;
}
Expand Down
1 change: 1 addition & 0 deletions fru.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,7 @@ static inline fru_reclist_t *add_reclist(fru_reclist_t **reclist)

#define LANG_DEFAULT 0
#define LANG_ENGLISH 25
#define FRU_TYPE_EOF 0xc1

typedef struct fru_info_area_s { // The generic info area structure
FRU_INFO_AREA_HEADER;
Expand Down
Loading

0 comments on commit 28aad59

Please sign in to comment.