[libvirt PATCH 24/33] conf: Parse firmware format

Andrea Bolognani abologna at redhat.com
Wed Feb 15 10:42:21 UTC 2023


The default is raw, which corresponds to the historical
behavior and is also the only accepted value.

Signed-off-by: Andrea Bolognani <abologna at redhat.com>
---
 src/conf/domain_conf.c            | 72 +++++++++++++++++++++++++++++--
 src/conf/domain_conf.h            |  1 +
 src/conf/schemas/domaincommon.rng | 14 ++++++
 3 files changed, 84 insertions(+), 3 deletions(-)

diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 30a3261dab..528426511e 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -3717,7 +3717,12 @@ virDomainPanicDefFree(virDomainPanicDef *panic)
 virDomainLoaderDef *
 virDomainLoaderDefNew(void)
 {
-    return g_new0(virDomainLoaderDef, 1);
+    virDomainLoaderDef *def = NULL;
+
+    def = g_new0(virDomainLoaderDef, 1);
+    def->format = VIR_STORAGE_FILE_RAW;
+
+    return def;
 }
 
 void
@@ -16751,6 +16756,7 @@ virDomainLoaderDefParseXMLNvram(virDomainLoaderDef *loader,
                                 unsigned int flags)
 {
     g_autoptr(virStorageSource) src = virStorageSourceNew();
+    unsigned int format = 0;
     int typePresent;
 
     if (!nvramNode)
@@ -16758,7 +16764,18 @@ virDomainLoaderDefParseXMLNvram(virDomainLoaderDef *loader,
 
     loader->nvramTemplate = virXMLPropString(nvramNode, "template");
 
-    src->format = VIR_STORAGE_FILE_RAW;
+    if (virXMLPropEnumDefault(nvramNode, "format",
+                              virStorageFileFormatTypeFromString, VIR_XML_PROP_NONE,
+                              &format, VIR_STORAGE_FILE_RAW) < 0) {
+        return -1;
+    }
+    if (format != VIR_STORAGE_FILE_RAW) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Unsupported nvram format '%s'"),
+                       virStorageFileFormatTypeToString(format));
+        return -1;
+    }
+    src->format = format;
 
     if ((typePresent = virXMLPropEnum(nvramNode, "type",
                                       virStorageTypeFromString, VIR_XML_PROP_NONE,
@@ -16792,8 +16809,26 @@ static int
 virDomainLoaderDefParseXMLLoader(virDomainLoaderDef *loader,
                                  xmlNodePtr loaderNode)
 {
-    if (!loaderNode)
+    unsigned int format = 0;
+
+    if (!loaderNode) {
+        /* If there is no <loader> element but the <nvram> element
+         * was present, copy the format from the latter to the
+         * former.
+         *
+         * This ensures that a configuration such as
+         *
+         *   <os>
+         *     <nvram format='foo'/>
+         *   </os>
+         *
+         * behaves as expected, that is, results in a firmware build
+         * with format 'foo' being selected */
+        if (loader->nvram)
+            loader->format = loader->nvram->format;
+
         return 0;
+    }
 
     if (virXMLPropTristateBool(loaderNode, "readonly", VIR_XML_PROP_NONE,
                                &loader->readonly) < 0)
@@ -16817,6 +16852,19 @@ virDomainLoaderDefParseXMLLoader(virDomainLoaderDef *loader,
                                &loader->stateless) < 0)
         return -1;
 
+    if (virXMLPropEnumDefault(loaderNode, "format",
+                              virStorageFileFormatTypeFromString, VIR_XML_PROP_NONE,
+                              &format, VIR_STORAGE_FILE_RAW) < 0) {
+        return -1;
+    }
+    if (format != VIR_STORAGE_FILE_RAW) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Unsupported loader format '%s'"),
+                       virStorageFileFormatTypeToString(format));
+        return -1;
+    }
+    loader->format = format;
+
     return 0;
 }
 
@@ -16839,6 +16887,14 @@ virDomainLoaderDefParseXML(virDomainLoaderDef *loader,
                                          loaderNode) < 0)
         return -1;
 
+    if (loader->nvram && loader->format != loader->nvram->format) {
+        virReportError(VIR_ERR_XML_ERROR,
+                       _("Format mismatch: loader.format='%s' nvram.format='%s'"),
+                       virStorageFileFormatTypeToString(loader->format),
+                       virStorageFileFormatTypeToString(loader->nvram->format));
+        return -1;
+    }
+
     return 0;
 }
 
@@ -26156,6 +26212,11 @@ virDomainLoaderDefFormatNvram(virBuffer *buf,
                                           false, flags, false, false, xmlopt) < 0)
                 return -1;
         }
+
+        if (src->format != VIR_STORAGE_FILE_RAW) {
+            virBufferEscapeString(&attrBuf, " format='%s'",
+                                  virStorageFileFormatTypeToString(src->format));
+        }
     }
 
     virXMLFormatElementInternal(buf, "nvram", &attrBuf, childBuf, false, childNewline);
@@ -26190,6 +26251,11 @@ virDomainLoaderDefFormat(virBuffer *buf,
                           virTristateBoolTypeToString(loader->stateless));
     }
 
+    if (loader->format != VIR_STORAGE_FILE_RAW) {
+        virBufferEscapeString(&loaderAttrBuf, " format='%s'",
+                              virStorageFileFormatTypeToString(loader->format));
+    }
+
     virBufferEscapeString(&loaderChildBuf, "%s", loader->path);
 
     virXMLFormatElementInternal(buf, "loader", &loaderAttrBuf, &loaderChildBuf, false, false);
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 96121220ea..aa5b90b82d 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -2312,6 +2312,7 @@ struct _virDomainLoaderDef {
     virDomainLoader type;
     virTristateBool secure;
     virTristateBool stateless;
+    virStorageFileFormat format;
     virStorageSource *nvram;
     bool newStyleNVRAM;
     char *nvramTemplate;   /* user override of path to master nvram */
diff --git a/src/conf/schemas/domaincommon.rng b/src/conf/schemas/domaincommon.rng
index a57dd212ab..150543e076 100644
--- a/src/conf/schemas/domaincommon.rng
+++ b/src/conf/schemas/domaincommon.rng
@@ -349,6 +349,9 @@
                   <ref name="virYesNo"/>
                 </attribute>
               </optional>
+              <optional>
+                <ref name="pflashFormat"/>
+              </optional>
               <optional>
                 <ref name="absFilePath"/>
               </optional>
@@ -361,6 +364,9 @@
                   <ref name="absFilePath"/>
                 </attribute>
               </optional>
+              <optional>
+                <ref name="pflashFormat"/>
+              </optional>
               <optional>
                 <choice>
                   <group>
@@ -7538,6 +7544,14 @@
     </element>
   </define>
 
+  <define name="pflashFormat">
+    <attribute name="format">
+      <choice>
+        <value>raw</value>
+      </choice>
+    </attribute>
+  </define>
+
   <!-- Optional HyperV Enlightenment features -->
   <define name="hyperv">
     <element name="hyperv">
-- 
2.39.1



More information about the libvir-list mailing list