From 6559a90269a75fe29526c3044c3cdec9b8e7f02b Mon Sep 17 00:00:00 2001 From: Stefan Berger Date: Mon, 12 Oct 2020 18:09:27 -0400 Subject: [PATCH] swtpm_cuse: Implement support for --flags startup-xyz options Signed-off-by: Stefan Berger --- src/swtpm/cuse_tpm.c | 40 +++++++++++++++++++++++++++++++++++----- 1 file changed, 35 insertions(+), 5 deletions(-) diff --git a/src/swtpm/cuse_tpm.c b/src/swtpm/cuse_tpm.c index 4130d18..3fcf908 100644 --- a/src/swtpm/cuse_tpm.c +++ b/src/swtpm/cuse_tpm.c @@ -221,9 +221,10 @@ static const char *usage = " instead;\n" " mode allows a user to set the file mode bits of the state\n" " files; the default mode is 0640;\n" -"--flags [not-need-init]\n" +"--flags [not-need-init][,startup-clear|startup-state|startup-deactivated|startup-none]\n" " : not-need-init: commands can be sent without needing to\n" " send an INIT via control channel;\n" +" startup-...: send Startup command with this type;\n" "-r|--runas : after creating the CUSE device, change to the given\n" " user\n" "--tpm2 : choose TPM2 functionality\n" @@ -490,6 +491,35 @@ static void ptm_write_fatal_error_response(TPMLIB_TPMVersion l_tpmversion) l_tpmversion); } +/* + * ptm_send_startup: Send a TPM/TPM2_Startup + */ +static int ptm_send_startup(uint16_t startupType, TPMLIB_TPMVersion l_tpmversion) +{ + uint32_t command_length; + unsigned char command[sizeof(struct tpm_startup)]; + uint32_t max_command_length = sizeof(command); + int ret = 0; + TPM_RESULT rc = TPM_SUCCESS; + + command_length = tpmlib_create_startup_cmd( + startupType, + tpmversion, + command, max_command_length); + if (command_length > 0) + rc = TPMLIB_Process(&ptm_response, &ptm_res_len, &ptm_res_tot, + (unsigned char *)command, command_length); + + if (rc || command_length == 0) { + if (rc) { + logprintf(STDERR_FILENO, "Could not send Startup: 0x%x\n", rc); + ret = -1; + } + } + + return ret; +} + /************************************ read() support ***************************/ /* @@ -1640,10 +1670,10 @@ int swtpm_cuse_main(int argc, char **argv, const char *prgname, const char *ifac } if (param.startupType != _TPM_ST_NONE) { - logprintf(STDERR_FILENO, - "--flags with startup types are not supported\n"); - ret = -1; - goto exit; + if (ptm_send_startup(param.startupType, tpmversion) < 0) { + ret = -1; + goto exit; + } } #if GLIB_MINOR_VERSION >= 32