Skip to contentSkip to conversation

hw/uefi: some bugfixes

PATCH In review Gerd Hoffmann 3 participants 11 messages Mar 19, 2025 RISC-V

lore
Share & tools
lore
#0 lore raw

v3 cover hw/uefi: some bugfixes

v3 changes:
- rename firmware.json flag, more verbose documentation.
- add support for riscv and loongarch.

Gerd Hoffmann (6):
hw/uefi: flush variable store to disk in post load hw/uefi: fix error handling in uefi_vars_json_save hw/uefi: fix error handling in uefi_vars_json_load hw/uefi-vars-sysbus: allow for riscv virt hw/uefi-vars-sysbus: allow for loongarch virt docs/firmware: add feature flag for host uefi variable store

 hw/loongarch/virt.c        |  2 ++
 hw/riscv/virt.c            |  2 ++
 hw/uefi/var-service-core.c |  1 +
 hw/uefi/var-service-json.c | 24 +++++++++++++++++++-----
 docs/interop/firmware.json | 12 +++++++++++-
 hw/uefi/Kconfig            |  2 +-
 6 files changed, 36 insertions(+), 7 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v3 1/6 hw/uefi: flush variable store to disk in post load

Make live migration more robust. Commit 4c0cfc72b31a ("pflash_cfi01:
write flash contents to bdrv on incoming migration") elaborates in detail on the motivation.

---
 hw/uefi/var-service-core.c | 1 +
 1 file changed, 1 insertion(+)
hw/uefi/var-service-core.c +1 -0
index 8ed8378ab991..4836a0cb8116 100644
@@ -29,6 +29,7 @@ static int uefi_vars_post_load(void *opaque, int version_id)
     uefi_vars_state *uv = opaque;
 
     uefi_vars_update_storage(uv);
+    uefi_vars_json_save(uv);
     uv->buffer = g_malloc(uv->buf_size);
     return 0;
 }
Signature · 1 line
2.48.1
#2 lore raw

PATCH v3 2/6 hw/uefi: fix error handling in uefi_vars_json_save

In reply to Gerd Hoffmann

Catch lseek errors. Return on errors.
Use autoptr for the GString to simplify cleanup.

---
 hw/uefi/var-service-json.c | 15 +++++++++++----
 1 file changed, 11 insertions(+), 4 deletions(-)
hw/uefi/var-service-json.c +11 -4
index 761082c11fc1..f1c20a6b8c1e 100644
@@ -178,7 +178,7 @@ void uefi_vars_json_init(uefi_vars_state *uv, Error **errp)
 
 void uefi_vars_json_save(uefi_vars_state *uv)
 {
-    GString *gstr;
+    g_autoptr(GString) gstr = NULL;
     int rc;
 
     if (uv->jsonfd == -1) {
@@ -187,18 +187,25 @@ void uefi_vars_json_save(uefi_vars_state *uv)
 
     gstr = uefi_vars_to_json(uv);
 
-    lseek(uv->jsonfd, 0, SEEK_SET);
+    rc = lseek(uv->jsonfd, 0, SEEK_SET);
+    if (rc < 0) {
+        warn_report("%s: lseek error", __func__);
+        return;
+    }
+
     rc = ftruncate(uv->jsonfd, 0);
     if (rc != 0) {
         warn_report("%s: ftruncate error", __func__);
+        return;
     }
+
     rc = write(uv->jsonfd, gstr->str, gstr->len);
     if (rc != gstr->len) {
         warn_report("%s: write error", __func__);
+        return;
     }
+
     fsync(uv->jsonfd);
-
-    g_string_free(gstr, true);
 }
 
 void uefi_vars_json_load(uefi_vars_state *uv, Error **errp)
Signature · 1 line
2.48.1
#3 lore raw

PATCH v3 3/6 hw/uefi: fix error handling in uefi_vars_json_load

In reply to Gerd Hoffmann

Catch lseek errors. Return on read errors.

Fixes CID 1593154
Fixes CID 1593157
---
 hw/uefi/var-service-json.c | 9 ++++++++-
 1 file changed, 8 insertions(+), 1 deletion(-)
hw/uefi/var-service-json.c +8 -1
index f1c20a6b8c1e..ad3462cd1557 100644
@@ -214,7 +214,7 @@ void uefi_vars_json_load(uefi_vars_state *uv, Error **errp)
     QObject *qobj;
     Visitor *v;
     char *str;
-    size_t len;
+    ssize_t len;
     int rc;
 
     if (uv->jsonfd == -1) {
@@ -222,7 +222,12 @@ void uefi_vars_json_load(uefi_vars_state *uv, Error **errp)
     }
 
     len = lseek(uv->jsonfd, 0, SEEK_END);
+    if (len < 0) {
+        warn_report("%s: lseek error", __func__);
+        return;
+    }
     if (len == 0) {
+        /* empty file */
         return;
     }
 
@@ -231,6 +236,8 @@ void uefi_vars_json_load(uefi_vars_state *uv, Error **errp)
     rc = read(uv->jsonfd, str, len);
     if (rc != len) {
         warn_report("%s: read error", __func__);
+        g_free(str);
+        return;
     }
     str[len] = 0;
 
Signature · 1 line
2.48.1
#4 lore raw

PATCH v3 4/6 hw/uefi-vars-sysbus: allow for riscv virt

In reply to Gerd Hoffmann

Allow the device being added to riscv virt VMs.

---
 hw/riscv/virt.c | 2 ++
 hw/uefi/Kconfig | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
hw/riscv/virt.c +2 -0
index dae46f4733cd..e517002fdfc7 100644
@@ -57,6 +57,7 @@
 #include "hw/acpi/aml-build.h"
 #include "qapi/qapi-visit-common.h"
 #include "hw/virtio/virtio-iommu.h"
+#include "hw/uefi/var-service-api.h"
 
 /* KVM AIA only supports APLIC MSI. APLIC Wired is always emulated by QEMU. */
 static bool virt_use_kvm_aia_aplic_imsic(RISCVVirtAIAType aia_type)
@@ -1935,6 +1936,7 @@ static void virt_machine_class_init(ObjectClass *oc, void *data)
     hc->plug = virt_machine_device_plug_cb;
 
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
+    machine_class_allow_dynamic_sysbus_dev(mc, TYPE_UEFI_VARS_SYSBUS);
 #ifdef CONFIG_TPM
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
 #endif
hw/uefi/Kconfig +1 -1
index ca6c2bc46a96..f139008b632b 100644
@@ -1,3 +1,3 @@
 config UEFI_VARS
 	bool
-        default y if X86_64 || AARCH64
+        default y if X86_64 || AARCH64 || RISCV64
Signature · 1 line
2.48.1
Reviewed-by on v3 4/6
#5 lore raw

Re: [PATCH v3 4/6] hw/uefi-vars-sysbus: allow for riscv virt

Quoted 7 lines · Gerd Hoffmann

Allow the device being added to riscv virt VMs.

---
 hw/riscv/virt.c | 2 ++
 hw/uefi/Kconfig | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
Reviewed-by on v3 4/6
#6 lore raw

Re: [PATCH v3 4/6] hw/uefi-vars-sysbus: allow for riscv virt

In reply to Gerd Hoffmann
Quoted 4 lines · Gerd Hoffmann

Allow the device being added to riscv virt VMs.

---

Quoted 33 lines
  hw/riscv/virt.c | 2 ++
  hw/uefi/Kconfig | 2 +-
  2 files changed, 3 insertions(+), 1 deletion(-)
hw/riscv/virt.c +2 -0
index dae46f4733cd..e517002fdfc7 100644
@@ -57,6 +57,7 @@
  #include "hw/acpi/aml-build.h"
  #include "qapi/qapi-visit-common.h"
  #include "hw/virtio/virtio-iommu.h"
+#include "hw/uefi/var-service-api.h"
 
  /* KVM AIA only supports APLIC MSI. APLIC Wired is always emulated by QEMU. */
  static bool virt_use_kvm_aia_aplic_imsic(RISCVVirtAIAType aia_type)
@@ -1935,6 +1936,7 @@ static void virt_machine_class_init(ObjectClass *oc, void *data)
      hc->plug = virt_machine_device_plug_cb;
 
      machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
+    machine_class_allow_dynamic_sysbus_dev(mc, TYPE_UEFI_VARS_SYSBUS);
  #ifdef CONFIG_TPM
      machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
  #endif
hw/uefi/Kconfig +1 -1
index ca6c2bc46a96..f139008b632b 100644
@@ -1,3 +1,3 @@
  config UEFI_VARS
  	bool
-        default y if X86_64 || AARCH64
+        default y if X86_64 || AARCH64 || RISCV64
#7 lore raw

PATCH v3 5/6 hw/uefi-vars-sysbus: allow for loongarch virt

In reply to Gerd Hoffmann

Allow the device being added to loongarch virt VMs.

---
 hw/loongarch/virt.c | 2 ++
 hw/uefi/Kconfig     | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)
hw/loongarch/virt.c +2 -0
index a5840ff96857..b6f5f6a3b546 100644
@@ -38,6 +38,7 @@
 #include "hw/mem/nvdimm.h"
 #include "hw/platform-bus.h"
 #include "hw/display/ramfb.h"
+#include "hw/uefi/var-service-api.h"
 #include "hw/mem/pc-dimm.h"
 #include "system/tpm.h"
 #include "system/block-backend.h"
@@ -1207,6 +1208,7 @@ static void virt_class_init(ObjectClass *oc, void *data)
     object_class_property_set_description(oc, "v-eiointc",
                             "Enable Virt Extend I/O Interrupt Controller.");
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_RAMFB_DEVICE);
+    machine_class_allow_dynamic_sysbus_dev(mc, TYPE_UEFI_VARS_SYSBUS);
 #ifdef CONFIG_TPM
     machine_class_allow_dynamic_sysbus_dev(mc, TYPE_TPM_TIS_SYSBUS);
 #endif
hw/uefi/Kconfig +1 -1
index f139008b632b..b55529fca137 100644
@@ -1,3 +1,3 @@
 config UEFI_VARS
 	bool
-        default y if X86_64 || AARCH64 || RISCV64
+        default y if X86_64 || AARCH64 || RISCV64 || LOONGARCH64
\ No newline at end of file
Signature · 1 line
2.48.1
Reviewed-by on v3 5/6
#8 lore raw

Re: [PATCH v3 5/6] hw/uefi-vars-sysbus: allow for loongarch virt

Quoted 7 lines · Gerd Hoffmann

Allow the device being added to loongarch virt VMs.

---
 hw/loongarch/virt.c | 2 ++
 hw/uefi/Kconfig     | 2 +-
 2 files changed, 3 insertions(+), 1 deletion(-)

hw/uefi/Kconfig line 3

Quoted 10 lines
hw/uefi/Kconfig +1 -1
index f139008b632b..b55529fca137 100644
@@ -1,3 +1,3 @@
 config UEFI_VARS
 	bool
-        default y if X86_64 || AARCH64 || RISCV64
+        default y if X86_64 || AARCH64 || RISCV64 || LOONGARCH64
\ No newline at end of file

Could fix the missing newline when sending a PULL

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#9 lore raw

PATCH v3 6/6 docs/firmware: add feature flag for host uefi variable store

In reply to Gerd Hoffmann
---
 docs/interop/firmware.json | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)
docs/interop/firmware.json +11 -1
index 57f55f6c5455..745d21d82232 100644
@@ -214,13 +214,23 @@
 #                  PL011 UART. @verbose-static is mutually exclusive
 #                  with @verbose-dynamic.
 #
+# @host-uefi-vars: The firmware expects the host to provide an uefi
+#                  variable store.  qemu supports that via
+#                  "uefi-vars-sysbus" (aarch64, riscv64, loongarch64)
+#                  or "uefi-vars-x64" (x86_64) devices.  The firmware
+#                  will not use flash for nvram.  When loading the
+#                  firmware into flash the 'stateless' setup should be
+#                  used.  It is recommened to load the firmware into
+#                  memory though.
+#
 # Since: 3.0
 ##
 { 'enum' : 'FirmwareFeature',
   'data' : [ 'acpi-s3', 'acpi-s4',
              'amd-sev', 'amd-sev-es', 'amd-sev-snp',
              'intel-tdx',
-             'enrolled-keys', 'requires-smm', 'secure-boot',
+             'enrolled-keys', 'requires-smm',
+             'secure-boot', 'host-uefi-vars',
              'verbose-dynamic', 'verbose-static' ] }
 
 ##
Signature · 1 line
2.48.1
Reviewed-by on v3 6/6
#10 lore raw

Re: [PATCH v3 6/6] docs/firmware: add feature flag for host uefi variable store

Quoted 4 lines · Gerd Hoffmann
---
 docs/interop/firmware.json | 12 +++++++++++-
 1 file changed, 11 insertions(+), 1 deletion(-)

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|