Skip to content

Commit

Permalink
Add compile time option to get device serial number from system BIOS …
Browse files Browse the repository at this point in the history
…table

Signed-off-by: Shrikant Temburwar <shrikant.temburwar@intel.com>
  • Loading branch information
shrikant1407 committed Nov 10, 2023
1 parent b7d0ab7 commit f40c225
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 47 deletions.
28 changes: 28 additions & 0 deletions cmake/cli_input.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ set (TPM2_TCTI_TYPE tabrmd)
set (RESALE true)
set (REUSE true)
set (MTLS false)
set (GET_DEV_SERIAL false)

#for CSE
set (CSE_SHUTDOWN true)
Expand Down Expand Up @@ -855,3 +856,30 @@ endif()
set(CACHED_MTLS ${MTLS} CACHE STRING "Selected MTLS")
message("Selected MTLS ${MTLS}")
###########################################
# FOR GET_DEV_SERIAL
get_property(cached_get_dev_serial_value CACHE GET_DEV_SERIAL PROPERTY VALUE)

set(get_dev_serial_cli_arg ${cached_get_dev_serial_value})
if(get_dev_serial_cli_arg STREQUAL CACHED_GET_DEV_SERIAL)
unset(get_dev_serial_cli_arg)
endif()

set(get_dev_serial_app_cmake_lists ${GET_DEV_SERIAL})
if(cached_get_dev_serial_value STREQUAL GET_DEV_SERIAL)
unset(get_dev_serial_app_cmake_lists)
endif()

if(DEFINED CACHED_GET_DEV_SERIAL)
if ((DEFINED get_dev_serial_cli_arg) AND (NOT(CACHED_GET_DEV_SERIAL STREQUAL get_dev_serial_cli_arg)))
message(WARNING "Need to do make pristine before cmake args can change.")
endif()
set(GET_DEV_SERIAL ${CACHED_GET_DEV_SERIAL})
elseif(DEFINED get_dev_serial_cli_arg)
set(GET_DEV_SERIAL ${get_dev_serial_cli_arg})
elseif(DEFINED get_dev_serial_app_cmake_lists)
set(GET_DEV_SERIAL ${get_dev_serial_app_cmake_lists})
endif()

set(CACHED_GET_DEV_SERIAL ${GET_DEV_SERIAL} CACHE STRING "Selected GET_DEV_SERIAL")
message("Selected GET_DEV_SERIAL ${GET_DEV_SERIAL}")
###########################################
4 changes: 4 additions & 0 deletions cmake/extension.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -275,4 +275,8 @@ endif()
if(${MTLS} STREQUAL true)
client_sdk_compile_definitions(-DMTLS)
endif()

if(${GET_DEV_SERIAL} STREQUAL true)
client_sdk_compile_definitions(-DGET_DEV_SERIAL)
endif()
############################################################
2 changes: 2 additions & 0 deletions lib/include/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,8 +158,10 @@ char *strdup_s(const char *str);
/* Print timestamp */
int print_timestamp(void);

#if defined(GET_DEV_SERIAL)
/* Get device serial number */
int get_device_serial(char *str);
#endif

#ifdef __cplusplus
}
Expand Down
104 changes: 57 additions & 47 deletions lib/m-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ static int read_fill_modelserial(void)
uint8_t def_serial_sz = 0;
uint8_t def_model_sz = 0;
size_t fsize = 0;

#if defined(GET_DEV_SERIAL)
int strcmp_res = -1;
char temp_device_serial[MAX_DEV_SERIAL_SZ];
uint8_t temp_serial_sz = 0;
Expand All @@ -77,12 +79,55 @@ static int read_fill_modelserial(void)
goto err;
}

ret = get_device_serial(temp_device_serial);
if (ret) {
LOG(LOG_ERROR, "Failed to get serial no.\n");
}

if (ret || (!strcmp_s((char *)temp_device_serial, MAX_DEV_SERIAL_SZ,
"Not Specified\n", &strcmp_res) &&
!strcmp_res)) {
LOG(LOG_DEBUG, "Defaulting serial num to 'abcdef'\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 = strncpy_s(device_serial, MAX_DEV_SERIAL_SZ, DEF_SERIAL_NO,
def_serial_sz);
if (ret) {
LOG(LOG_ERROR, "Failed to copy 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;
}
}
#else
fsize = fdo_blob_size((const char *)SERIAL_FILE, FDO_SDK_RAW_DATA);
if ((fsize > 0) && (fsize <= MAX_DEV_SERIAL_SZ)) {

if (fdo_blob_read((const char *)SERIAL_FILE, FDO_SDK_RAW_DATA,
(uint8_t *)device_serial, fsize) <= 0) {

LOG(LOG_ERROR, "Failed to get serial no\n");
goto err;
}
Expand All @@ -94,56 +139,21 @@ static int read_fill_modelserial(void)
LOG(LOG_INFO, "No serialno file present!\n");
}

ret = get_device_serial(temp_device_serial);
if (ret) {
LOG(LOG_ERROR, "Failed to get serial no. "
"Defaulting it to 'abcdef'\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;
}

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;
}
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;
}
}
#endif
LOG(LOG_DEBUG, "Device serial = %s\n", device_serial);

fsize = fdo_blob_size((const char *)MODEL_FILE, FDO_SDK_RAW_DATA);
Expand Down
2 changes: 2 additions & 0 deletions storage/util.c
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ int print_timestamp(void)
return 0;
}

#if defined(GET_DEV_SERIAL)
// Get device serial number
int get_device_serial(char *serial_buff)
{
Expand Down Expand Up @@ -374,3 +375,4 @@ int get_device_serial(char *serial_buff)
}
return ret;
}
#endif

0 comments on commit f40c225

Please sign in to comment.