aboutsummaryrefslogtreecommitdiff
path: root/tpm.c
diff options
context:
space:
mode:
Diffstat (limited to 'tpm.c')
-rw-r--r--tpm.c81
1 files changed, 17 insertions, 64 deletions
diff --git a/tpm.c b/tpm.c
index 9a7c7114d3..2d830d072a 100644
--- a/tpm.c
+++ b/tpm.c
@@ -23,71 +23,28 @@
static QLIST_HEAD(, TPMBackend) tpm_backends =
QLIST_HEAD_INITIALIZER(tpm_backends);
+static TPMDriverOps const *be_drivers[TPM_TYPE__MAX];
+static bool tpm_models[TPM_MODEL__MAX];
-#define TPM_MAX_MODELS 1
-#define TPM_MAX_DRIVERS 1
-
-static TPMDriverOps const *be_drivers[TPM_MAX_DRIVERS] = {
- NULL,
-};
-
-static enum TpmModel tpm_models[TPM_MAX_MODELS] = {
- TPM_MODEL__MAX,
-};
-
-int tpm_register_model(enum TpmModel model)
+void tpm_register_model(enum TpmModel model)
{
- int i;
-
- for (i = 0; i < TPM_MAX_MODELS; i++) {
- if (tpm_models[i] == TPM_MODEL__MAX) {
- tpm_models[i] = model;
- return 0;
- }
- }
- error_report("Could not register TPM model");
- return 1;
-}
-
-static bool tpm_model_is_registered(enum TpmModel model)
-{
- int i;
-
- for (i = 0; i < TPM_MAX_MODELS; i++) {
- if (tpm_models[i] == model) {
- return true;
- }
- }
- return false;
+ tpm_models[model] = true;
}
const TPMDriverOps *tpm_get_backend_driver(const char *type)
{
- int i;
+ int i = qapi_enum_parse(&TpmType_lookup, type, -1, NULL);
- for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
- if (!strcmp(TpmType_lookup[be_drivers[i]->type], type)) {
- return be_drivers[i];
- }
- }
-
- return NULL;
+ return i >= 0 ? be_drivers[i] : NULL;
}
#ifdef CONFIG_TPM
-int tpm_register_driver(const TPMDriverOps *tdo)
+void tpm_register_driver(const TPMDriverOps *tdo)
{
- int i;
+ assert(!be_drivers[tdo->type]);
- for (i = 0; i < TPM_MAX_DRIVERS; i++) {
- if (!be_drivers[i]) {
- be_drivers[i] = tdo;
- return 0;
- }
- }
- error_report("Could not register TPM driver");
- return 1;
+ be_drivers[tdo->type] = tdo;
}
/*
@@ -100,9 +57,12 @@ static void tpm_display_backend_drivers(void)
fprintf(stderr, "Supported TPM types (choose only one):\n");
- for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
+ for (i = 0; i < TPM_TYPE__MAX; i++) {
+ if (be_drivers[i] == NULL) {
+ continue;
+ }
fprintf(stderr, "%12s %s\n",
- TpmType_lookup[be_drivers[i]->type], be_drivers[i]->desc());
+ TpmType_str(i), be_drivers[i]->desc());
}
fprintf(stderr, "\n");
}
@@ -239,14 +199,7 @@ int tpm_config_parse(QemuOptsList *opts_list, const char *optarg)
static const TPMDriverOps *tpm_driver_find_by_type(enum TpmType type)
{
- int i;
-
- for (i = 0; i < TPM_MAX_DRIVERS && be_drivers[i] != NULL; i++) {
- if (be_drivers[i]->type == type) {
- return be_drivers[i];
- }
- }
- return NULL;
+ return be_drivers[type];
}
static TPMInfo *qmp_query_tpm_inst(TPMBackend *drv)
@@ -289,7 +242,7 @@ TPMInfoList *qmp_query_tpm(Error **errp)
TPMInfoList *info, *head = NULL, *cur_item = NULL;
QLIST_FOREACH(drv, &tpm_backends, list) {
- if (!tpm_model_is_registered(drv->fe_model)) {
+ if (!tpm_models[drv->fe_model]) {
continue;
}
info = g_new0(TPMInfoList, 1);
@@ -336,7 +289,7 @@ TpmModelList *qmp_query_tpm_models(Error **errp)
TpmModelList *head = NULL, *prev = NULL, *cur_item;
for (i = 0; i < TPM_MODEL__MAX; i++) {
- if (!tpm_model_is_registered(i)) {
+ if (!tpm_models[i]) {
continue;
}
cur_item = g_new0(TpmModelList, 1);