Skip to content

Commit 57cdde4

Browse files
committed
Merge remote-tracking branch 'remotes/stefanberger/tags/pull-tpm-2020-07-24-1' into staging
Merge tpm 2020/07/24 v1 # gpg: Signature made Sat 25 Jul 2020 01:13:22 BST # gpg: using RSA key B818B9CADF9089C2D5CEC66B75AD65802A0B4211 # gpg: Good signature from "Stefan Berger <[email protected]>" [unknown] # gpg: WARNING: This key is not certified with a trusted signature! # gpg: There is no indication that the signature belongs to the owner. # Primary key fingerprint: B818 B9CA DF90 89C2 D5CE C66B 75AD 6580 2A0B 4211 * remotes/stefanberger/tags/pull-tpm-2020-07-24-1: tpm_emulator: Report an error if chardev is missing tpm: Improve help on TPM types when none are available Revert "tpm: Clean up error reporting in tpm_init_tpmdev()" Signed-off-by: Peter Maydell <[email protected]>
2 parents b0ce3f0 + 88f8307 commit 57cdde4

File tree

5 files changed

+59
-31
lines changed

5 files changed

+59
-31
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));

include/sysemu/tpm.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include "qom/object.h"
1717

1818
int tpm_config_parse(QemuOptsList *opts_list, const char *optarg);
19-
void tpm_init(void);
19+
int tpm_init(void);
2020
void tpm_cleanup(void);
2121

2222
typedef enum TPMVersion {

softmmu/vl.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4258,7 +4258,9 @@ void qemu_init(int argc, char **argv, char **envp)
42584258
user_creatable_add_opts_foreach,
42594259
object_create_delayed, &error_fatal);
42604260

4261-
tpm_init();
4261+
if (tpm_init() < 0) {
4262+
exit(1);
4263+
}
42624264

42634265
blk_mig_init();
42644266
ram_mig_init();

stubs/tpm.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
#include "sysemu/tpm.h"
1111
#include "hw/acpi/tpm.h"
1212

13-
void tpm_init(void)
13+
int tpm_init(void)
1414
{
15+
return 0;
1516
}
1617

1718
void tpm_cleanup(void)

tpm.c

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,23 @@ tpm_be_find_by_type(enum TpmType type)
4747
*/
4848
static void tpm_display_backend_drivers(void)
4949
{
50+
bool got_one = false;
5051
int i;
5152

52-
fprintf(stderr, "Supported TPM types (choose only one):\n");
53-
5453
for (i = 0; i < TPM_TYPE__MAX; i++) {
5554
const TPMBackendClass *bc = tpm_be_find_by_type(i);
5655
if (!bc) {
5756
continue;
5857
}
59-
fprintf(stderr, "%12s %s\n", TpmType_str(i), bc->desc);
58+
if (!got_one) {
59+
error_printf("Supported TPM types (choose only one):\n");
60+
got_one = true;
61+
}
62+
error_printf("%12s %s\n", TpmType_str(i), bc->desc);
63+
}
64+
if (!got_one) {
65+
error_printf("No TPM backend types are available\n");
6066
}
61-
fprintf(stderr, "\n");
6267
}
6368

6469
/*
@@ -81,41 +86,49 @@ TPMBackend *qemu_find_tpm_be(const char *id)
8186

8287
static int tpm_init_tpmdev(void *dummy, QemuOpts *opts, Error **errp)
8388
{
89+
/*
90+
* Use of error_report() in a function with an Error ** parameter
91+
* is suspicious. It is okay here. The parameter only exists to
92+
* make the function usable with qemu_opts_foreach(). It is not
93+
* actually used.
94+
*/
8495
const char *value;
8596
const char *id;
8697
const TPMBackendClass *be;
8798
TPMBackend *drv;
99+
Error *local_err = NULL;
88100
int i;
89101

90102
if (!QLIST_EMPTY(&tpm_backends)) {
91-
error_setg(errp, "Only one TPM is allowed.");
103+
error_report("Only one TPM is allowed.");
92104
return 1;
93105
}
94106

95107
id = qemu_opts_id(opts);
96108
if (id == NULL) {
97-
error_setg(errp, QERR_MISSING_PARAMETER, "id");
109+
error_report(QERR_MISSING_PARAMETER, "id");
98110
return 1;
99111
}
100112

101113
value = qemu_opt_get(opts, "type");
102114
if (!value) {
103-
error_setg(errp, QERR_MISSING_PARAMETER, "type");
115+
error_report(QERR_MISSING_PARAMETER, "type");
104116
tpm_display_backend_drivers();
105117
return 1;
106118
}
107119

108120
i = qapi_enum_parse(&TpmType_lookup, value, -1, NULL);
109121
be = i >= 0 ? tpm_be_find_by_type(i) : NULL;
110122
if (be == NULL) {
111-
error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "type",
112-
"a TPM backend type");
123+
error_report(QERR_INVALID_PARAMETER_VALUE,
124+
"type", "a TPM backend type");
113125
tpm_display_backend_drivers();
114126
return 1;
115127
}
116128

117129
/* validate backend specific opts */
118-
if (!qemu_opts_validate(opts, be->opts, errp)) {
130+
if (!qemu_opts_validate(opts, be->opts, &local_err)) {
131+
error_report_err(local_err);
119132
return 1;
120133
}
121134

@@ -148,10 +161,14 @@ void tpm_cleanup(void)
148161
* Initialize the TPM. Process the tpmdev command line options describing the
149162
* TPM backend.
150163
*/
151-
void tpm_init(void)
164+
int tpm_init(void)
152165
{
153-
qemu_opts_foreach(qemu_find_opts("tpmdev"),
154-
tpm_init_tpmdev, NULL, &error_fatal);
166+
if (qemu_opts_foreach(qemu_find_opts("tpmdev"),
167+
tpm_init_tpmdev, NULL, NULL)) {
168+
return -1;
169+
}
170+
171+
return 0;
155172
}
156173

157174
/*

0 commit comments

Comments
 (0)