int tpm_comm_submit_command()

in src/tpm_comm_emulator.c [279:351]


int tpm_comm_submit_command(TPM_COMM_HANDLE handle, const unsigned char* cmd_bytes, uint32_t bytes_len, unsigned char* response, uint32_t* resp_len)
{
    int result;
    if (handle == NULL || cmd_bytes == NULL || response == NULL || resp_len == NULL)
    {
        LogError("Invalid argument specified handle: %p, cmd_bytes: %p, response: %p, resp_len: %p.", handle, cmd_bytes, response, resp_len);
        result = MU_FAILURE;
    }
    else
    {
        // Send to TPM
        unsigned char locality = 0;
        if (send_sync_cmd(handle, Remote_SendCommand) != 0)
        {
            LogError("Failure preparing sending Remote Command");
            result = MU_FAILURE;
        }
        else if (send_sync_bytes(handle, (const unsigned char*)&locality, 1) != 0)
        {
            LogError("Failure setting locality to TPM");
            result = MU_FAILURE;
        }
        else if (send_sync_cmd(handle, bytes_len) != 0)
        {
            LogError("Failure writing command bit to tpm");
            result = MU_FAILURE;
        }
        else if (send_sync_bytes(handle, cmd_bytes, bytes_len))
        {
            LogError("Failure writing data to tpm");
            result = MU_FAILURE;
        }
        else
        {
            uint32_t length_byte;

            if (read_sync_cmd(handle, &length_byte) != 0)
            {
                LogError("Failure reading length data from tpm");
                result = MU_FAILURE;
            }
            else if (length_byte > *resp_len)
            {
                LogError("Bytes read are greater then bytes expected len_bytes:%u expected: %u", length_byte, *resp_len);
                result = MU_FAILURE;
            }
            else
            {
                *resp_len = length_byte;
                if (read_sync_bytes(handle, response, &length_byte) != 0)
                {
                    LogError("Failure reading bytes");
                    result = MU_FAILURE;
                }
                else
                {
                    // check the Ack
                    uint32_t ack_cmd;
                    if (read_sync_cmd(handle, &ack_cmd) != 0 || ack_cmd != 0)
                    {
                        LogError("Failure reading tpm ack");
                        result = MU_FAILURE;
                    }
                    else
                    {
                        result = 0;
                    }
                }
            }
        }
    }
    return result;
}