Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support to get device serial from system BIOS table #258

Merged
merged 4 commits into from
Nov 13, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions lib/include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,9 @@ char *strdup_s(const char *str);
/* Print timestamp */
int print_timestamp(void);

/* Get device serial number */
int get_device_serial(char *str);

#ifdef __cplusplus
}
#endif
Expand Down
67 changes: 56 additions & 11 deletions lib/m-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ static int read_fill_modelserial(void)
uint8_t def_serial_sz = 0;
uint8_t def_model_sz = 0;
size_t fsize = 0;
int strcmp_res = -1;
char temp_device_serial[MAX_DEV_SERIAL_SZ];
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TO-DO: make it const

uint8_t temp_serial_sz = 0;

if (memset_s(temp_device_serial, sizeof(temp_device_serial), 0) != 0) {
LOG(LOG_ERROR, "Memset() failed!\n");
goto err;
}

fsize = fdo_blob_size((const char *)SERIAL_FILE, FDO_SDK_RAW_DATA);
if ((fsize > 0) && (fsize <= MAX_DEV_SERIAL_SZ)) {
Expand All @@ -82,24 +90,61 @@ static int read_fill_modelserial(void)
if (fsize > MAX_DEV_SERIAL_SZ) {
LOG(LOG_INFO, "Serialno exceeds 255 characters. "
"Defaulting it to 'abcdef'\n");
} else {
} else if (!fsize) {
LOG(LOG_INFO, "No serialno file present!\n");
}

def_serial_sz = strnlen_s(DEF_SERIAL_NO, MAX_DEV_SERIAL_SZ);
if (!def_serial_sz || def_serial_sz == MAX_DEV_SERIAL_SZ) {
LOG(LOG_ERROR, "Default serial number string isn't "
"NULL terminated\n");
goto err;
ret = get_device_serial(temp_device_serial);
if (ret) {
LOG(LOG_ERROR, "Failed to get serial no. "
"Defaulting it to 'abcdef'\n");
}

ret = strncpy_s(device_serial, MAX_DEV_SERIAL_SZ, DEF_SERIAL_NO,
def_serial_sz);
if (ret) {
LOG(LOG_ERROR, "Failed to get serial no\n");
goto err;
if (ret ||
(!strcmp_s((char *)temp_device_serial, MAX_DEV_SERIAL_SZ,
"Not Specified\n", &strcmp_res) &&
!strcmp_res)) {
def_serial_sz =
strnlen_s(DEF_SERIAL_NO, MAX_DEV_SERIAL_SZ);
if (!def_serial_sz ||
def_serial_sz == MAX_DEV_SERIAL_SZ) {
LOG(LOG_ERROR,
"Default serial number string isn't "
"NULL terminated\n");
goto err;
}

ret = strncpy_s(device_serial, MAX_DEV_SERIAL_SZ,
DEF_SERIAL_NO, def_serial_sz);
if (ret) {
LOG(LOG_ERROR, "Failed to get serial no\n");
goto err;
}
} else {
temp_serial_sz =
strnlen_s(temp_device_serial, MAX_DEV_SERIAL_SZ);
if (!temp_serial_sz ||
temp_serial_sz == MAX_DEV_SERIAL_SZ) {
LOG(LOG_ERROR,
"Default serial number string isn't "
"NULL terminated\n");
goto err;
}

if (*temp_device_serial &&
temp_device_serial[temp_serial_sz - 1] == '\n') {
temp_device_serial[temp_serial_sz - 1] = '\0';
}

ret = strncpy_s(device_serial, MAX_DEV_SERIAL_SZ,
temp_device_serial, temp_serial_sz);
if (ret) {
LOG(LOG_ERROR, "Failed to copy serial no!\n");
goto err;
}
}
}
LOG(LOG_DEBUG, "Device serial = %s\n", device_serial);

fsize = fdo_blob_size((const char *)MODEL_FILE, FDO_SDK_RAW_DATA);
if ((fsize > 0) && (fsize <= MAX_MODEL_NO_SZ)) {
Expand Down
57 changes: 57 additions & 0 deletions storage/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
#include "freertos/task.h"
#endif

#ifndef MAX_DEV_SERIAL_SZ
#define MAX_DEV_SERIAL_SZ 255
#endif

bool file_exists(char const *filename)
{
FILE *fp = NULL;
Expand Down Expand Up @@ -317,3 +321,56 @@ int print_timestamp(void)
#endif
return 0;
}

// Get device serial number
int get_device_serial(char *serial_buff)
{
FILE *fp;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do input check for serial_buff before using it. Do it every place

char *cmd = "dmidecode -s system-serial-number";
int out_sz;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add const for cmd variable ..As this value wouldnt change

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check other places also

char out[MAX_DEV_SERIAL_SZ];
int results_sz = 0;
int ret = -1;
char *results = (char *)malloc(MAX_DEV_SERIAL_SZ * sizeof(char));

if (cmd != NULL) {
/* Open the command for reading. */
fp = popen(cmd, "r");
if (fp != NULL) {

/* Read the output a line at a time - output it. */
while (fgets(out, out_sz = sizeof(out), fp) != NULL) {
if (strcat_s(results, MAX_DEV_SERIAL_SZ, out) !=
0) {
LOG(LOG_ERROR, "Strcat() failed!\n");
goto end;
}
}

results_sz = strnlen_s(results, MAX_DEV_SERIAL_SZ);
if (!results_sz) {
goto end;
}

if (memcpy_s(serial_buff, results_sz, results,
results_sz)) {
LOG(LOG_ERROR,
"Failed to copy device serial contents\n");
goto end;
}
} else {
goto end;
}
ret = 0;
}
end:
/* close */
if (fp) {
pclose(fp);
}
if (results) {
free(results);
results = NULL;
}
return ret;
}