Skip to contentSkip to conversation

tpm: A fix and a cleanup

PATCH In review Stefan Berger 2 participants 5 messages Feb 1, 2018 – Feb 2, 2018

lore
Share & tools
lore
#0 lore raw

cover tpm: A fix and a cleanup

The following two patches fix the resetting of the CRB interface and wrap calls to st{w,l}_be_p in tpm_cmd_set_XYZ functions.

Stefan

Stefan Berger (2):
tpm: Split off tpm_crb_reset function
tpm: wrap stX_be_p in tpm_cmd_set_XYZ functions

 hw/tpm/tpm_crb.c  |  6 ++++++
 hw/tpm/tpm_util.c |  6 +++---
 hw/tpm/tpm_util.h | 15 +++++++++++++++
 3 files changed, 24 insertions(+), 3 deletions(-)
Signature · 1 line
2.5.5
#1 lore raw

PATCH 1/2 tpm: Split off tpm_crb_reset function

Split off the tpm_crb_reset function part from tpm_crb_realize that we need to run every time the machine resets.

---
 hw/tpm/tpm_crb.c | 6 ++++++
 1 file changed, 6 insertions(+)
hw/tpm/tpm_crb.c +6 -0
index 687d255..624e2e9 100644
@@ -232,6 +232,11 @@ static void tpm_crb_realize(DeviceState *dev, Error **errp)
         TPM_CRB_ADDR_BASE, &s->mmio);
     memory_region_add_subregion(get_system_memory(),
         TPM_CRB_ADDR_BASE + sizeof(s->regs), &s->cmdmem);
+}
+
+static void tpm_crb_reset(DeviceState *dev)
+{
+    CRBState *s = CRB(dev);
 
     tpm_backend_reset(s->tpmbe);
 
@@ -274,6 +279,7 @@ static void tpm_crb_class_init(ObjectClass *klass, void *data)
 
     dc->realize = tpm_crb_realize;
     dc->props = tpm_crb_properties;
+    dc->reset = tpm_crb_reset;
     dc->vmsd  = &vmstate_tpm_crb;
     dc->user_creatable = true;
     tc->model = TPM_MODEL_TPM_CRB;
Signature · 1 line
2.5.5
#2 lore raw

Re: [Qemu-devel] [PATCH 1/2] tpm: Split off tpm_crb_reset function

Quoted 25 lines · Stefan Berger

Split off the tpm_crb_reset function part from tpm_crb_realize that we need to run every time the machine resets.

---
  hw/tpm/tpm_crb.c | 6 ++++++
  1 file changed, 6 insertions(+)
hw/tpm/tpm_crb.c +5 -0
index 687d255..624e2e9 100644
@@ -232,6 +232,11 @@ static void tpm_crb_realize(DeviceState *dev, Error **errp)
          TPM_CRB_ADDR_BASE, &s->mmio);
      memory_region_add_subregion(get_system_memory(),
          TPM_CRB_ADDR_BASE + sizeof(s->regs), &s->cmdmem);
+}
+
+static void tpm_crb_reset(DeviceState *dev)
+{
+    CRBState *s = CRB(dev);
 
      tpm_backend_reset(s->tpmbe);
 

hw/tpm/tpm_crb.c static void tpm_crb_class_init(ObjectClass *klass, void *data) · line 285

Quoted 7 lines
hw/tpm/tpm_crb.c +1 -0
@@ -274,6 +279,7 @@ static void tpm_crb_class_init(ObjectClass *klass, void *data)
 
      dc->realize = tpm_crb_realize;
      dc->props = tpm_crb_properties;
+    dc->reset = tpm_crb_reset;
      dc->vmsd  = &vmstate_tpm_crb;
      dc->user_creatable = true;
      tc->model = TPM_MODEL_TPM_CRB;

So this splt-off unfortunately causes the tpm-crb-test to fail since the reset function is not called. I wonder how to trigger that? Do we need to call tpm_crb_reset now from tpm_crb_realize just to make the tests work ? Or is there a function we can call in the test case to trigger the device reset ?

#3 lore raw

PATCH 2/2 tpm: wrap stX_be_p in tpm_cmd_set_XYZ functions

In reply to Stefan Berger

Wrap the calls to stl_be_p and stw_be_p in tpm_cmd_set_XYZ functions that are similar to existing getters.

---
 hw/tpm/tpm_util.c |  6 +++---
 hw/tpm/tpm_util.h | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)
hw/tpm/tpm_util.c +3 -3
index 8abde59..2de52a0 100644
@@ -106,9 +106,9 @@ const PropertyInfo qdev_prop_tpm = {
 void tpm_util_write_fatal_error_response(uint8_t *out, uint32_t out_len)
 {
     if (out_len >= sizeof(struct tpm_resp_hdr)) {
-        stw_be_p(out, TPM_TAG_RSP_COMMAND);
-        stl_be_p(out + 2, sizeof(struct tpm_resp_hdr));
-        stl_be_p(out + 6, TPM_FAIL);
+        tpm_cmd_set_tag(out, TPM_TAG_RSP_COMMAND);
+        tpm_cmd_set_size(out, sizeof(struct tpm_resp_hdr));
+        tpm_cmd_set_error(out, TPM_FAIL);
     }
 }
 
hw/tpm/tpm_util.h +15 -0
index f003d15..f397ac2 100644
@@ -36,11 +36,21 @@ static inline uint16_t tpm_cmd_get_tag(const void *b)
     return lduw_be_p(b);
 }
 
+static inline void tpm_cmd_set_tag(void *b, uint16_t tag)
+{
+    stw_be_p(b, tag);
+}
+
 static inline uint32_t tpm_cmd_get_size(const void *b)
 {
     return ldl_be_p(b + 2);
 }
 
+static inline void tpm_cmd_set_size(void *b, uint32_t size)
+{
+    stl_be_p(b + 2, size);
+}
+
 static inline uint32_t tpm_cmd_get_ordinal(const void *b)
 {
     return ldl_be_p(b + 6);
@@ -51,6 +61,11 @@ static inline uint32_t tpm_cmd_get_errcode(const void *b)
     return ldl_be_p(b + 6);
 }
 
+static inline void tpm_cmd_set_error(void *b, uint32_t error)
+{
+    stl_be_p(b + 6, error);
+}
+
 int tpm_util_get_buffer_size(int tpm_fd, TPMVersion tpm_version,
                              size_t *buffersize);
 
Signature · 1 line
2.5.5
Reviewed-by Marc-André Lureau on v1 2/2
#4 lore raw

Re: [Qemu-devel] [PATCH 2/2] tpm: wrap stX_be_p in tpm_cmd_set_XYZ functions

Hi

Quoted 2 lines · Stefan Berger

Wrap the calls to stl_be_p and stw_be_p in tpm_cmd_set_XYZ functions that are similar to existing getters.

why not,

Quoted 65 lines
---
 hw/tpm/tpm_util.c |  6 +++---
 hw/tpm/tpm_util.h | 15 +++++++++++++++
 2 files changed, 18 insertions(+), 3 deletions(-)
hw/tpm/tpm_util.c +3 -3
index 8abde59..2de52a0 100644
@@ -106,9 +106,9 @@ const PropertyInfo qdev_prop_tpm = {
 void tpm_util_write_fatal_error_response(uint8_t *out, uint32_t out_len)
 {
     if (out_len >= sizeof(struct tpm_resp_hdr)) {
-        stw_be_p(out, TPM_TAG_RSP_COMMAND);
-        stl_be_p(out + 2, sizeof(struct tpm_resp_hdr));
-        stl_be_p(out + 6, TPM_FAIL);
+        tpm_cmd_set_tag(out, TPM_TAG_RSP_COMMAND);
+        tpm_cmd_set_size(out, sizeof(struct tpm_resp_hdr));
+        tpm_cmd_set_error(out, TPM_FAIL);
     }
 }
 
hw/tpm/tpm_util.h +15 -1
index f003d15..f397ac2 100644
@@ -36,11 +36,21 @@ static inline uint16_t tpm_cmd_get_tag(const void *b)
     return lduw_be_p(b);
 }
 
+static inline void tpm_cmd_set_tag(void *b, uint16_t tag)
+{
+    stw_be_p(b, tag);
+}
+
 static inline uint32_t tpm_cmd_get_size(const void *b)
 {
     return ldl_be_p(b + 2);
 }
 
+static inline void tpm_cmd_set_size(void *b, uint32_t size)
+{
+    stl_be_p(b + 2, size);
+}
+
 static inline uint32_t tpm_cmd_get_ordinal(const void *b)
 {
     return ldl_be_p(b + 6);
@@ -51,6 +61,11 @@ static inline uint32_t tpm_cmd_get_errcode(const void *b)
     return ldl_be_p(b + 6);
 }
 
+static inline void tpm_cmd_set_error(void *b, uint32_t error)
+{
+    stl_be_p(b + 6, error);
+}
+
 int tpm_util_get_buffer_size(int tpm_fd, TPMVersion tpm_version,
                              size_t *buffersize);
 
--

2.5.5