Skip to content

Commit

Permalink
Add support to send fdo.download:done and fdo.command:exitcode messag…
Browse files Browse the repository at this point in the history
…e from device to owner (fido-device-onboard#272)

* Add support to send fdo.download:done message from device to owner

fdo.download:done message indicates that the download has completed, returns the length of the target file.
Value of -1 indicates the sha-384 check failed, or other file write error

* Fix memory leaks

* * Add support to send fdo.command:exitcode message from device to owner
* Fix FDO_SIM to work with FDO_SYS

---------

Signed-off-by: Shrikant Temburwar <[email protected]>
Co-authored-by: KiranSukhavasi <[email protected]>
  • Loading branch information
shrikant1407 and KiranSukhavasi authored Mar 1, 2024
1 parent e32ec0d commit 3395708
Show file tree
Hide file tree
Showing 8 changed files with 226 additions and 116 deletions.
108 changes: 92 additions & 16 deletions device_modules/fdo_sim/fdo_sim.c
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,64 @@ static size_t file_seek_pos = 0;
static size_t file_sz = 0;
// EOT value whose value is 0 for 'fetch-data'success, and 1 for failure
static int fetch_data_status = 1;
// local isMore flag that represents whether the module has data/response to
// send in the NEXT messege SHOULD be 'true' if there is data to send, 'false'
// otherwise For simplicity, it is 'false' always (also a valid value)
static bool ismore = false;

/**
* Write CBOR-encoded fdo.download:done content into FDOW with given data.
*/
static bool write_done(fdow_t *fdow, char *module_message, size_t bin_len)
{

if (!module_message || !bin_len) {
LOG(LOG_ERROR,
"Module fdo_sim - Invalid params for fdo.download:done\n");
return false;
}

const char message[] = "done";
if (memcpy_s(module_message, sizeof(message), message,
sizeof(message)) != 0) {
LOG(LOG_ERROR,
"Module fdo_sim - Failed to copy module message data\n");
return false;
}

if (!fdow_signed_int(fdow, bin_len)) {
LOG(LOG_ERROR, "Module fdo_sim - Failed to write "
"fdo.download:done content\n");
return false;
}

return true;
}

/**
* Write CBOR-encoded fdo.command:exitcode content into FDOW with given data.
*/
static bool write_exitcode(fdow_t *fdow, char *module_message, size_t bin_len)
{

if (!module_message) {
LOG(LOG_ERROR, "Module fdo_sim - Invalid params for "
"fdo.command:exitcode\n");
return false;
}

const char message[] = "exitcode";
if (memcpy_s(module_message, sizeof(message), message,
sizeof(message)) != 0) {
LOG(LOG_ERROR,
"Module fdo_sim - Failed to copy module message data\n");
return false;
}

if (!fdow_signed_int(fdow, bin_len)) {
LOG(LOG_ERROR, "Module fdo_sim - Failed to write "
"fdo.command:exitcode content\n");
return false;
}

return true;
}

/**
* List of helper functions used in switch case
Expand All @@ -41,7 +95,7 @@ int fdo_sim_start(fdor_t **fdor, fdow_t **fdow)
// Initialize module's CBOR Reader/Writer objects.
*fdow = FSIMModuleAlloc(sizeof(fdow_t));
if (!fdow_init(*fdow) ||
!fdo_block_alloc_with_size(&(*fdow)->b, 8192)) {
!fdo_block_alloc_with_size(&(*fdow)->b, MOD_MAX_BUFF_SIZE)) {
LOG(LOG_ERROR, "Module fdo_sim - FDOW "
"Initialization/Allocation failed!\n");
result = FDO_SI_CONTENT_ERROR;
Expand All @@ -50,7 +104,7 @@ int fdo_sim_start(fdor_t **fdor, fdow_t **fdow)

*fdor = FSIMModuleAlloc(sizeof(fdor_t));
if (!fdor_init(*fdor) ||
!fdo_block_alloc_with_size(&(*fdor)->b, 8192)) {
!fdo_block_alloc_with_size(&(*fdor)->b, MOD_MAX_BUFF_SIZE)) {
LOG(LOG_ERROR, "Module fdo_sim - FDOR "
"Initialization/Allocation failed!\n");
goto end;
Expand Down Expand Up @@ -97,7 +151,7 @@ int fdo_sim_has_more_dsi(bool *has_more, bool hasmore)
return FDO_SI_SUCCESS;
}

int fdo_sim_is_more_dsi(bool *is_more)
int fdo_sim_is_more_dsi(bool *is_more, bool ismore)
{
// calculate whether there is ServiceInfo to send in the NEXT
// iteration and update 'is_more'.
Expand Down Expand Up @@ -126,9 +180,9 @@ int fdo_sim_get_dsi_count(uint16_t *num_module_messages)
}

int fdo_sim_get_dsi(fdow_t **fdow, size_t mtu, char *module_message,
uint8_t *module_val, size_t *module_val_sz, size_t bin_len,
uint8_t *bin_data, size_t temp_module_val_sz, bool *hasmore,
fdoSimModMsg *write_type, char *filename)
uint8_t *module_val, size_t *module_val_sz, size_t bin_len,
uint8_t *bin_data, size_t temp_module_val_sz, bool *hasmore,
fdoSimModMsg *write_type, char *filename)
{
// write Device ServiceInfo using 'fdow' by partitioning the
// messages as per MTU, here.
Expand All @@ -138,7 +192,6 @@ int fdo_sim_get_dsi(fdow_t **fdow, size_t mtu, char *module_message,

int result = FDO_SI_INTERNAL_ERROR;

(void)bin_len;
(void)filename;

// reset and initialize FDOW's encoder for usage
Expand All @@ -154,7 +207,30 @@ int fdo_sim_get_dsi(fdow_t **fdow, size_t mtu, char *module_message,
goto end;
}

// TO-DO: Imlement functionality
if (*write_type == FDO_SIM_MOD_MSG_DONE) {
if (!write_done(*fdow, module_message, bin_len)) {
LOG(LOG_ERROR, "Module fdo_sim - Failed to "
"respond with fdo.download:done\n");
goto end;
}
*hasmore = false;
LOG(LOG_DEBUG,
"Module fdo_sim - Responded with fdo.download:done\n");
} else if (*write_type == FDO_SIM_MOD_MSG_EXIT_CODE) {
if (!write_exitcode(*fdow, module_message, bin_len)) {
LOG(LOG_ERROR, "Module fdo_sim - Failed to "
"respond with fdo.command:exitcode\n");
goto end;
}
*hasmore = false;
LOG(LOG_DEBUG,
"Module fdo_sim - Responded with fdo.command:exitcode\n");
} else if (*write_type == FDO_SIM_MOD_MSG_NONE) {
// shouldn't reach here, if we do, it might a logical
// error log and fail
LOG(LOG_ERROR, "Module fdo_sim - Invalid module write state\n");
goto end;
}

if (!fdow_encoded_length(*fdow, &temp_module_val_sz)) {
LOG(LOG_ERROR,
Expand All @@ -170,14 +246,14 @@ int fdo_sim_get_dsi(fdow_t **fdow, size_t mtu, char *module_message,
}
result = FDO_SI_SUCCESS;
end:
result =
fdo_sim_end(NULL, fdow, result, bin_data, NULL, 0, hasmore, write_type);
result = fdo_sim_end(NULL, fdow, result, bin_data, NULL, 0, hasmore,
write_type);
return result;
}

int fdo_sim_end(fdor_t **fdor, fdow_t **fdow, int result, uint8_t *bin_data,
uint8_t **exec_instr, size_t total_exec_array_length, bool *hasmore,
fdoSimModMsg *write_type)
uint8_t **exec_instr, size_t total_exec_array_length,
bool *hasmore, fdoSimModMsg *write_type)
{
// End of function, clean-up state variables/objects
if (bin_data) {
Expand Down
27 changes: 14 additions & 13 deletions device_modules/fdo_sim/fdo_sim.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,24 @@ int fdo_sim_command(fdo_sdk_si_type type, char *module_message,
int fdo_sim_start(fdor_t **fdor, fdow_t **fdow);
int fdo_sim_failure(fdor_t **fdor, fdow_t **fdow);
int fdo_sim_has_more_dsi(bool *has_more, bool hasmore);
int fdo_sim_is_more_dsi(bool *is_more);
int fdo_sim_is_more_dsi(bool *is_more, bool ismore);
int fdo_sim_get_dsi_count(uint16_t *num_module_messages);
int fdo_sim_get_dsi(fdow_t **fdow, size_t mtu, char *module_message,
uint8_t *module_val, size_t *module_val_sz, size_t bin_len,
uint8_t *bin_data, size_t temp_module_val_sz, bool *hasmore,
fdoSimModMsg *write_type, char *filename);
uint8_t *module_val, size_t *module_val_sz, size_t bin_len,
uint8_t *bin_data, size_t temp_module_val_sz, bool *hasmore,
fdoSimModMsg *write_type, char *filename);

int fdo_sim_set_osi_download(char *module_message, uint8_t *module_val,
size_t *module_val_sz, int *strcmp_filedesc,
int *strcmp_length, int *strcmp_sha_384,
int *strcmp_write);
size_t *module_val_sz, int *strcmp_filedesc,
int *strcmp_length, int *strcmp_sha_384,
int *strcmp_write);

int fdo_sim_set_osi_command(char *module_message, uint8_t *module_val,
size_t *module_val_sz, int *strcmp_cmd,
int *strcmp_args, int *strcmp_may_fail,
int *strcmp_return_stdout, int *strcmp_return_stderr,
int *strcmp_sig, int *strcmp_exec);
size_t *module_val_sz, int *strcmp_cmd,
int *strcmp_args, int *strcmp_may_fail,
int *strcmp_return_stdout,
int *strcmp_return_stderr, int *strcmp_sig,
int *strcmp_exec);

int fdo_sim_set_osi_strcmp(size_t bin_len, uint8_t *bin_data);
int fdo_sim_set_osi_sha_384(size_t bin_len, uint8_t *bin_data);
Expand All @@ -123,6 +124,6 @@ int fdo_sim_set_osi_exec(uint8_t **exec_instr);
int fdo_sim_set_osi_status_cb(size_t *status_cb_array_length);
int fdo_sim_set_osi_fetch(size_t bin_len);
int fdo_sim_end(fdor_t **fdor, fdow_t **fdow, int result, uint8_t *bin_data,
uint8_t **exec_instr, size_t total_exec_array_length, bool *hasmore,
fdoSimModMsg *write_type);
uint8_t **exec_instr, size_t total_exec_array_length,
bool *hasmore, fdoSimModMsg *write_type);
#endif /* __FDO_SYS_H__ */
37 changes: 23 additions & 14 deletions device_modules/fdo_sim/fdo_sim_command.c
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,15 @@ static size_t total_exec_array_length = 0;
// local hasMore flag that represents whether the module has data/response to
// send NOW 'true' if there is data to send, 'false' otherwise
static bool hasmore = false;
// local isMore flag that represents whether the module has data/response to
// send in the NEXT messege SHOULD be 'true' if there is data to send, 'false'
// otherwise For simplicity, it is 'false' always (also a valid value)
static bool ismore = false;
static fdoSimModMsg write_type = FDO_SIM_MOD_MSG_EXIT;
static uint8_t *fdo_cmd = NULL;
static size_t fdo_cmd_len = 0;
static uint8_t **fdo_exec_instr = NULL;
static int exit_code = -1;

int fdo_sim_command(fdo_sdk_si_type type, char *module_message,
uint8_t *module_val, size_t *module_val_sz,
Expand Down Expand Up @@ -60,16 +65,16 @@ int fdo_sim_command(fdo_sdk_si_type type, char *module_message,
result = fdo_sim_has_more_dsi(has_more, hasmore);
goto end;
case FDO_SI_IS_MORE_DSI:
result = fdo_sim_is_more_dsi(is_more);
result = fdo_sim_is_more_dsi(is_more, ismore);
goto end;
case FDO_SI_GET_DSI_COUNT:
result = fdo_sim_get_dsi_count(num_module_messages);
goto end;
case FDO_SI_GET_DSI:
result = fdo_sim_get_dsi(&fdow, mtu, module_message, module_val,
module_val_sz, bin_len, bin_data,
temp_module_val_sz, &hasmore,
&write_type, filename);
module_val_sz, exit_code, bin_data,
temp_module_val_sz, &hasmore,
&write_type, filename);
goto end;
case FDO_SI_SET_OSI:
result = fdo_sim_set_osi_command(
Expand All @@ -86,7 +91,7 @@ int fdo_sim_command(fdo_sdk_si_type type, char *module_message,
goto end;
} else if (strcmp_args == 0) {
result = fdo_sim_set_osi_args(exec_array_index,
&exec_instructions_sz);
&exec_instructions_sz);
goto end;
} else if (strcmp_may_fail == 0) {
result = fdo_sim_set_osi_may_fail();
Expand All @@ -110,15 +115,16 @@ int fdo_sim_command(fdo_sdk_si_type type, char *module_message,

end:
result = fdo_sim_end(&fdor, &fdow, result, bin_data, exec_instr,
total_exec_array_length, &hasmore, &write_type);
total_exec_array_length, &hasmore, &write_type);
return result;
}

int fdo_sim_set_osi_command(char *module_message, uint8_t *module_val,
size_t *module_val_sz, int *strcmp_cmd,
int *strcmp_args, int *strcmp_may_fail,
int *strcmp_return_stdout, int *strcmp_return_stderr,
int *strcmp_sig, int *strcmp_exec)
size_t *module_val_sz, int *strcmp_cmd,
int *strcmp_args, int *strcmp_may_fail,
int *strcmp_return_stdout,
int *strcmp_return_stderr, int *strcmp_sig,
int *strcmp_exec)
{
if (!module_message || !module_val || !module_val_sz ||
*module_val_sz > MOD_MAX_BUFF_SIZE) {
Expand Down Expand Up @@ -303,7 +309,7 @@ int fdo_sim_set_osi_cmd(size_t bin_len, uint8_t *bin_data)
result = FDO_SI_SUCCESS;
end:
result = fdo_sim_end(&fdor, &fdow, result, bin_data, NULL,
total_exec_array_length, &hasmore, &write_type);
total_exec_array_length, &hasmore, &write_type);
return result;
}

Expand Down Expand Up @@ -433,11 +439,11 @@ int fdo_sim_set_osi_args(int exec_array_index, size_t *exec_instructions_sz)
if (!flag) {
result =
fdo_sim_end(&fdor, &fdow, result, fdo_cmd, fdo_exec_instr,
total_exec_array_length, &hasmore, &write_type);
total_exec_array_length, &hasmore, &write_type);
} else {
result =
fdo_sim_end(&fdor, &fdow, result, fdo_cmd, NULL,
total_exec_array_length, &hasmore, &write_type);
total_exec_array_length, &hasmore, &write_type);
}
return result;
}
Expand All @@ -459,10 +465,13 @@ int fdo_sim_set_osi_exec(uint8_t **exec_instr)
"process fdo.command:execute\n");
goto end;
}
exit_code = 0;
hasmore = true;
write_type = FDO_SIM_MOD_MSG_EXIT_CODE;
}
result = FDO_SI_SUCCESS;
end:
result = fdo_sim_end(&fdor, &fdow, result, NULL, exec_instr,
total_exec_array_length, &hasmore, &write_type);
total_exec_array_length, &hasmore, &write_type);
return result;
}
Loading

0 comments on commit 3395708

Please sign in to comment.