Skip to content

Commit

Permalink
Make suggested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
aneeshdurg committed Aug 14, 2020
1 parent dea002c commit 4c8b4e4
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 39 deletions.
1 change: 0 additions & 1 deletion fatal.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once

#define fatal(fmt, args...) do { \
fprintf(stderr, fmt, ##args); \
fprintf(stderr, "\n"); \
Expand Down
63 changes: 33 additions & 30 deletions fru.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@
#define DEBUG(f, args...)
#endif

static time_t epoch_seconds_1996() {
struct tm tm_1996 = {
.tm_year = 96,
.tm_mon = 0,
.tm_mday = 1
};
// The argument to mktime is zoneless
return mktime(&tm_1996);
}


/**
* Strip trailing spaces
*/
Expand Down Expand Up @@ -434,13 +445,7 @@ fru_info_area_t *fru_create_info_area(fru_area_type_t atype, ///< [in] Area t

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;
Expand All @@ -455,10 +460,8 @@ fru_info_area_t *fru_create_info_area(fru_area_type_t atype, ///< [in] Area t
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;
fru_time = (tv->tv_sec - epoch_seconds_1996()) / 60;
}
header.mfgdate[0] = fru_time & 0xFF;
header.mfgdate[1] = (fru_time >> 8) & 0xFF;
Expand Down Expand Up @@ -534,7 +537,7 @@ static bool fru_decode_custom_fields(const uint8_t *data, fru_reclist_t **reclis

fru_reclist_t *custom_field = add_reclist(reclist);
if (custom_field == NULL)
return false;
return false;

// Create a NUL terminated version of the data for encoding
// TODO pass the length into fru_encode_data instead
Expand All @@ -548,7 +551,7 @@ static bool fru_decode_custom_fields(const uint8_t *data, fru_reclist_t **reclis
data += FRU_FIELDSIZE(field->typelen);
}

return true;
return true;
}

/**
Expand Down Expand Up @@ -673,53 +676,53 @@ bool fru_decode_board_info(
fru_exploded_board_t *board_out //< [out]
)
{
fru_field_t *field;
fru_field_t *field;
const uint8_t *data = area->data;

board_out->lang = area->langtype;
board_out->lang = area->langtype;

uint32_t *min_since_1996 = (uint32_t*)&(area->mfgdate);
struct tm tm_1996 = {
uint32_t *min_since_1996 = (uint32_t*)&(area->mfgdate);
struct tm tm_1996 = {
.tm_year = 96,
.tm_mon = 0,
.tm_mday = 1
};
// The argument to mktime is zoneless
board_out->tv.tv_sec = mktime(&tm_1996) + 60 * (*min_since_1996);
};
// The argument to mktime is zoneless
board_out->tv.tv_sec = mktime(&tm_1996) + 60 * (*min_since_1996);

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

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

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

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

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

fru_decode_custom_fields(data, &board_out->cust);
fru_decode_custom_fields(data, &board_out->cust);

return true;
return true;
}

/**
Expand Down
19 changes: 11 additions & 8 deletions fru_reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@
#include "fru_reader.h"
#include "fatal.h"

static void safe_read(int fd, void *buffer, size_t length) {
static void safe_read(int fd, uint8_t *buffer, size_t length) {
if (!buffer)
fatal("Cannot read into NULL buffer");

size_t total_bytes_read = 0;
while (total_bytes_read != length) {
ssize_t bytes_read = read(
Expand All @@ -22,7 +25,7 @@ fru_t *read_fru_header(int fd) {
fru_t *fru = malloc(sizeof(fru_t));
if (!fru)
return NULL;
safe_read(fd, fru, sizeof(fru_t));
safe_read(fd, (uint8_t*)fru, sizeof(fru_t));
return fru;
}

Expand All @@ -34,12 +37,12 @@ fru_chassis_area_t *read_fru_chassis_area(int fd) {
fru_chassis_area_t *area = malloc(base_len);
if (!area)
return NULL;
safe_read(fd, area, base_len);
safe_read(fd, (uint8_t*)area, base_len);
size_t data_len = 8 * area->blocks;
area = realloc(area, data_len);
if (!area)
return NULL;
safe_read(fd, &area->data, data_len - base_len);
safe_read(fd, (uint8_t*)&area->data, data_len - base_len);

return area;
}
Expand All @@ -52,12 +55,12 @@ fru_board_area_t *read_fru_board_area(int fd) {
fru_board_area_t *area = malloc(base_len);
if (!area)
return NULL;
safe_read(fd, area, base_len);
safe_read(fd, (uint8_t*)area, base_len);
size_t data_len = 8 * area->blocks;
area = realloc(area, data_len);
if (!area)
return NULL;
safe_read(fd, &area->data, data_len - base_len);
safe_read(fd, (uint8_t*)&area->data, data_len - base_len);

return area;
}
Expand All @@ -70,12 +73,12 @@ fru_product_area_t *read_fru_product_area(int fd) {
fru_product_area_t *area = malloc(base_len);
if (!area)
return NULL;
safe_read(fd, area, base_len);
safe_read(fd, (uint8_t*)area, base_len);
size_t data_len = 8 * area->blocks;
area = realloc(area, data_len);
if (!area)
return NULL;
safe_read(fd, &area->data, data_len - base_len);
safe_read(fd, (uint8_t*)&area->data, data_len - base_len);

return area;
}

0 comments on commit 4c8b4e4

Please sign in to comment.