[PATCH] tpm: Refactor open-coded bitmap 'activePcrBanks' to virBitmap

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Peter Krempa <pkrempa@xxxxxxxxxx>
---

Applies on top of Michal's series to fix 'activePcrBank' handling.

 src/conf/domain_conf.c | 17 +++++++++--------
 src/conf/domain_conf.h |  2 +-
 src/qemu/qemu_tpm.c    | 21 +++++++++------------
 3 files changed, 19 insertions(+), 21 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index ddd6fe1c90..fccb29bc63 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -3255,6 +3255,7 @@ void virDomainTPMDefFree(virDomainTPMDef *def)
         virObjectUnref(def->data.emulator.source);
         g_free(def->data.emulator.storagepath);
         g_free(def->data.emulator.logfile);
+        virBitmapFree(def->data.emulator.activePcrBanks);
         break;
     case VIR_DOMAIN_TPM_TYPE_LAST:
         break;
@@ -10442,6 +10443,8 @@ virDomainTPMDefParseXML(virDomainXMLOption *xmlopt,

         if ((nnodes = virXPathNodeSet("./backend/active_pcr_banks/*", ctxt, &nodes)) < 0)
             break;
+        if (nnodes > 0)
+            def->data.emulator.activePcrBanks = virBitmapNew(0);
         for (i = 0; i < nnodes; i++) {
             if ((bank = virDomainTPMPcrBankTypeFromString((const char *)nodes[i]->name)) < 0) {
                 virReportError(VIR_ERR_CONFIG_UNSUPPORTED,
@@ -10449,7 +10452,7 @@ virDomainTPMDefParseXML(virDomainXMLOption *xmlopt,
                                nodes[i]->name);
                 goto error;
             }
-            def->data.emulator.activePcrBanks |= (1 << bank);
+            virBitmapSetBitExpand(def->data.emulator.activePcrBanks, bank);
         }
         break;
     case VIR_DOMAIN_TPM_TYPE_LAST:
@@ -20671,7 +20674,8 @@ virDomainTPMDefCheckABIStability(virDomainTPMDef *src,
             return false;
         }

-        if (src->data.emulator.activePcrBanks != dst->data.emulator.activePcrBanks) {
+        if (!virBitmapEqual(src->data.emulator.activePcrBanks,
+                            dst->data.emulator.activePcrBanks)) {
             virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s",
                            _("Target active PCR banks doesn't match source"));
             return false;
@@ -24235,13 +24239,10 @@ virDomainTPMDefFormat(virBuffer *buf,
         }
         if (def->data.emulator.activePcrBanks) {
             g_auto(virBuffer) activePcrBanksBuf = VIR_BUFFER_INIT_CHILD(&backendChildBuf);
-            size_t i;
+            ssize_t bank = -1;

-            for (i = VIR_DOMAIN_TPM_PCR_BANK_SHA1; i < VIR_DOMAIN_TPM_PCR_BANK_LAST; i++) {
-                if ((def->data.emulator.activePcrBanks & (1 << i)))
-                    virBufferAsprintf(&activePcrBanksBuf, "<%s/>\n",
-                                      virDomainTPMPcrBankTypeToString(i));
-            }
+            while ((bank = virBitmapNextSetBit(def->data.emulator.activePcrBanks, bank)) > -1)
+                virBufferAsprintf(&activePcrBanksBuf, "<%s/>\n", virDomainTPMPcrBankTypeToString(bank));

             virXMLFormatElement(&backendChildBuf, "active_pcr_banks", NULL, &activePcrBanksBuf);
         }
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 200a75d705..724265b6b5 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1451,7 +1451,7 @@ struct _virDomainTPMDef {
             unsigned char secretuuid[VIR_UUID_BUFLEN];
             bool hassecretuuid;
             bool persistent_state;
-            unsigned int activePcrBanks;
+            virBitmap *activePcrBanks;
         } emulator;
     } data;
 };
diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c
index f28dd2e1e9..c08b0851da 100644
--- a/src/qemu/qemu_tpm.c
+++ b/src/qemu/qemu_tpm.c
@@ -444,19 +444,16 @@ qemuTPMEmulatorRunSetup(const char *storagepath,


 static char *
-qemuTPMPcrBankBitmapToStr(unsigned int pcrBanks)
+qemuTPMPcrBankBitmapToStr(virBitmap *activePcrBanks)
 {
     g_auto(virBuffer) buf = VIR_BUFFER_INITIALIZER;
-    const char *comma = "";
-    size_t i;
-
-    for (i = VIR_DOMAIN_TPM_PCR_BANK_SHA1; i < VIR_DOMAIN_TPM_PCR_BANK_LAST; i++) {
-        if (pcrBanks & (1 << i)) {
-            virBufferAsprintf(&buf, "%s%s",
-                              comma, virDomainTPMPcrBankTypeToString(i));
-            comma = ",";
-        }
-    }
+    ssize_t bank = -1;
+
+    while ((bank = virBitmapNextSetBit(activePcrBanks, bank)) > -1)
+        virBufferAsprintf(&buf, "%s,", virDomainTPMPcrBankTypeToString(bank));
+
+    virBufferTrim(&buf, ",");
+
     return virBufferContentAndReset(&buf);
 }

@@ -481,7 +478,7 @@ static int
 qemuTPMEmulatorReconfigure(const char *storagepath,
                            uid_t swtpm_user,
                            gid_t swtpm_group,
-                           unsigned int activePcrBanks,
+                           virBitmap *activePcrBanks,
                            const char *logfile,
                            const virDomainTPMVersion tpmversion,
                            const unsigned char *secretuuid)
-- 
2.36.1




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux