Skip to content

Commit 88f8307

Browse files
committed
tpm_emulator: Report an error if chardev is missing
This patch fixes the odd error reporting when trying to send a file descriptor to the TPM emulator if one has not passed a valid chardev. $ x86_64-softmmu/qemu-system-x86_64 -tpmdev emulator,id=tpm0 qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: Failed to send CMD_SET_DATAFD: Success qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: Could not cleanly shutdown the TPM: Success This is the new error report: $ x86_64-softmmu/qemu-system-x86_64 -tpmdev emulator,id=tpm0 qemu-system-x86_64: -tpmdev emulator,id=tpm0: tpm-emulator: parameter 'chardev' is missing This change does not hide the display of supported TPM types if a non-existent type is passed: $ x86_64-softmmu/qemu-system-x86_64 -tpmdev nonexistent,id=tpm0 qemu-system-x86_64: -tpmdev nonexistent,id=tpm0: Parameter 'type' expects a TPM backend type Supported TPM types (choose only one): passthrough Passthrough TPM backend driver emulator TPM emulator backend driver Signed-off-by: Stefan Berger <[email protected]> Reviewed-by: Marc-André Lureau <[email protected]> Reviewed-by: Markus Armbruster <[email protected]>
1 parent 5a6791c commit 88f8307

File tree

1 file changed

+23
-15
lines changed

1 file changed

+23
-15
lines changed

backends/tpm/tpm_emulator.c

Lines changed: 23 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -549,27 +549,30 @@ static int tpm_emulator_prepare_data_fd(TPMEmulator *tpm_emu)
549549
static int tpm_emulator_handle_device_opts(TPMEmulator *tpm_emu, QemuOpts *opts)
550550
{
551551
const char *value;
552+
Error *err = NULL;
553+
Chardev *dev;
552554

553555
value = qemu_opt_get(opts, "chardev");
554-
if (value) {
555-
Error *err = NULL;
556-
Chardev *dev = qemu_chr_find(value);
557-
558-
if (!dev) {
559-
error_report("tpm-emulator: tpm chardev '%s' not found.", value);
560-
goto err;
561-
}
556+
if (!value) {
557+
error_report("tpm-emulator: parameter 'chardev' is missing");
558+
goto err;
559+
}
562560

563-
if (!qemu_chr_fe_init(&tpm_emu->ctrl_chr, dev, &err)) {
564-
error_prepend(&err, "tpm-emulator: No valid chardev found at '%s':",
565-
value);
566-
error_report_err(err);
567-
goto err;
568-
}
561+
dev = qemu_chr_find(value);
562+
if (!dev) {
563+
error_report("tpm-emulator: tpm chardev '%s' not found", value);
564+
goto err;
565+
}
569566

570-
tpm_emu->options->chardev = g_strdup(value);
567+
if (!qemu_chr_fe_init(&tpm_emu->ctrl_chr, dev, &err)) {
568+
error_prepend(&err, "tpm-emulator: No valid chardev found at '%s':",
569+
value);
570+
error_report_err(err);
571+
goto err;
571572
}
572573

574+
tpm_emu->options->chardev = g_strdup(value);
575+
573576
if (tpm_emulator_prepare_data_fd(tpm_emu) < 0) {
574577
goto err;
575578
}
@@ -925,6 +928,11 @@ static void tpm_emulator_shutdown(TPMEmulator *tpm_emu)
925928
{
926929
ptm_res res;
927930

931+
if (!tpm_emu->options->chardev) {
932+
/* was never properly initialized */
933+
return;
934+
}
935+
928936
if (tpm_emulator_ctrlcmd(tpm_emu, CMD_SHUTDOWN, &res, 0, sizeof(res)) < 0) {
929937
error_report("tpm-emulator: Could not cleanly shutdown the TPM: %s",
930938
strerror(errno));

0 commit comments

Comments
 (0)