Skip to contentSkip to conversation

hw/uefi: some bugfixes

PATCH Superseded Gerd Hoffmann 3 participants 15 messages Mar 19, 2025

v2 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

v2 cover hw/uefi: some bugfixes

Gerd Hoffmann (4):
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 docs/firmware: add feature flag for qemu variable store

 hw/uefi/var-service-core.c |  1 +
 hw/uefi/var-service-json.c | 24 +++++++++++++++++++-----
 docs/interop/firmware.json |  5 ++++-
 3 files changed, 24 insertions(+), 6 deletions(-)
Signature · 1 line
2.48.1
#1 lore raw

PATCH v2 1/4 hw/uefi: flush variable store to disk in post load

Makes 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

Re: [PATCH v2 1/4] hw/uefi: flush variable store to disk in post load

Quoted 1 line · Gerd Hoffmann

Makes live migration more robust.

Typo "Make"

Quoted 9 lines

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(+)
#3 lore raw

PATCH v2 2/4 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
#5 lore raw

PATCH v2 3/4 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
#7 lore raw

PATCH v2 4/4 docs/firmware: add feature flag for qemu variable store

In reply to Gerd Hoffmann
---
 docs/interop/firmware.json | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
docs/interop/firmware.json +4 -1
index 57f55f6c5455..76df1043dae9 100644
@@ -214,13 +214,16 @@
 #                  PL011 UART. @verbose-static is mutually exclusive
 #                  with @verbose-dynamic.
 #
+# @qemu-vars: The firmware expects qemu to provide an efi variable
+#             store, via "uefi-vars-sysbus" or "uefi-vars-x64" device.
+#
 # 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', 'qemu-vars',
              'verbose-dynamic', 'verbose-static' ] }
 
 ##
Signature · 1 line
2.48.1
#8 lore raw

Re: [PATCH v2 4/4] docs/firmware: add feature flag for qemu variable store

Quoted 10 lines · Gerd Hoffmann
---
 docs/interop/firmware.json | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
docs/interop/firmware.json +0 -0
index 57f55f6c5455..76df1043dae9 100644

docs/interop/firmware.json line 218

Quoted 5 lines
docs/interop/firmware.json +2 -0
@@ -214,13 +214,16 @@
 #                  PL011 UART. @verbose-static is mutually exclusive
 #                  with @verbose-dynamic.
 #
+# @qemu-vars: The firmware expects qemu to provide an efi variable
+#             store, via "uefi-vars-sysbus" or "uefi-vars-x64" device.

It seems like this would imply mapping.device == memory, as if we had mapping.device == flash, then we would need to extend FirmwareFlashMode with an extra option ? If so, lets document this expectation.

Quoted 15 lines
docs/interop/firmware.json +2 -1
+#
 # 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', 'qemu-vars',
              'verbose-dynamic', 'verbose-static' ] }
 
 ##
Signature · 1 line
2.48.1

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

Re: [PATCH v2 4/4] docs/firmware: add feature flag for qemu variable store

Quoted 6 lines · Daniel P. Berrangé
Quoted 5 lines · Gerd Hoffmann
---
 docs/interop/firmware.json | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 12 lines

docs/interop/firmware.json line 218

Quoted 10 lines
docs/interop/firmware.json +2 -0
index 57f55f6c5455..76df1043dae9 100644
@@ -214,13 +214,16 @@
 #                  PL011 UART. @verbose-static is mutually exclusive
 #                  with @verbose-dynamic.
 #
+# @qemu-vars: The firmware expects qemu to provide an efi variable
+#             store, via "uefi-vars-sysbus" or "uefi-vars-x64" device.

It seems like this would imply mapping.device == memory,

edk2 doesn't care if you load it into flash or memory, both cases will work fine. Using flash if we don't actually need it makes things more complicated for no good reason, so yes, I'd go write config files with mapping.device == memory.

Quoted 3 lines

as if we had
mapping.device == flash, then we would need to extend FirmwareFlashMode with an extra option ?

There is 'stateless' already for 'firmware image in r/o flash'.

take care,
Gerd

#10 lore raw

Re: [PATCH v2 4/4] docs/firmware: add feature flag for qemu variable store

Quoted 7 lines · Gerd Hoffmann
Quoted 6 lines · Daniel P. Berrangé
Quoted 5 lines · Gerd Hoffmann
---
 docs/interop/firmware.json | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
Quoted 10 lines
Quoted 10 lines

docs/interop/firmware.json line 218

Quoted 10 lines
docs/interop/firmware.json +2 -0
index 57f55f6c5455..76df1043dae9 100644
@@ -214,13 +214,16 @@
 #                  PL011 UART. @verbose-static is mutually exclusive
 #                  with @verbose-dynamic.
 #
+# @qemu-vars: The firmware expects qemu to provide an efi variable
+#             store, via "uefi-vars-sysbus" or "uefi-vars-x64" device.

I wonder if 'qemu-vars' is the right name here ? It feels like the specification for such device is effectively defined by UEFI, with any hypervisor providing a impl. Perhaps just call it 'uefi-vars-dev' or some name that's relevant for what EDK2 calls it ?

Quoted 8 lines
Quoted 2 lines

It seems like this would imply mapping.device == memory,

edk2 doesn't care if you load it into flash or memory, both cases will work fine. Using flash if we don't actually need it makes things more complicated for no good reason, so yes, I'd go write config files with mapping.device == memory.

Quoted 5 lines
Quoted 3 lines

as if we had
mapping.device == flash, then we would need to extend FirmwareFlashMode with an extra option ?

There is 'stateless' already for 'firmware image in r/o flash'.

What's the behaviour of UEFI if build with JSON vars support, but without QEMU providing any JSON vars backend ? If that happily runs stateless in that case, then we could reuse the existing 'stateless' mode, without having compat trouble with older libvirt that don't know about the 'qemu-vars' feature.

We would want to expand the 'stateless' docs to mention that this feature flag indicates optional support for persistence in that case.

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 :|
#11 lore raw

Re: [PATCH v2 4/4] docs/firmware: add feature flag for qemu variable store

Quoted 7 lines · Daniel P. Berrangé
Quoted 2 lines
Quoted 2 lines

docs/interop/firmware.json line 218

Quoted 2 lines
docs/interop/firmware.json +2 -0
+# @qemu-vars: The firmware expects qemu to provide an efi variable
+#             store, via "uefi-vars-sysbus" or "uefi-vars-x64" device.

I wonder if 'qemu-vars' is the right name here ? It feels like the specification for such device is effectively defined by UEFI, with any hypervisor providing a impl. Perhaps just call it 'uefi-vars-dev' or some name that's relevant for what EDK2 calls it ?

'host-uefi-vars' maybe? Or 'vmm-uefi-vars'?

Quoted 4 lines
Quoted 1 line

There is 'stateless' already for 'firmware image in r/o flash'.

What's the behaviour of UEFI if build with JSON vars support, but without QEMU providing any JSON vars backend ?

It will panic.

Quoted 2 lines

We would want to expand the 'stateless' docs to mention that this feature flag indicates optional support for persistence in that case.

Optional is not possible do due to the way variable store support is organized in edk2. Firmware can't switch between smm and non-smm configuration at runtime for similar reasons.

take care,
Gerd

#12 lore raw

Re: [PATCH v2 4/4] docs/firmware: add feature flag for qemu variable store

Quoted 10 lines · Gerd Hoffmann
Quoted 7 lines · Daniel P. Berrangé
Quoted 2 lines
Quoted 2 lines

docs/interop/firmware.json line 218

Quoted 2 lines
docs/interop/firmware.json +2 -0
+# @qemu-vars: The firmware expects qemu to provide an efi variable
+#             store, via "uefi-vars-sysbus" or "uefi-vars-x64" device.

I wonder if 'qemu-vars' is the right name here ? It feels like the specification for such device is effectively defined by UEFI, with any hypervisor providing a impl. Perhaps just call it 'uefi-vars-dev' or some name that's relevant for what EDK2 calls it ?

Quoted 8 lines

'host-uefi-vars' maybe? Or 'vmm-uefi-vars'?

Quoted 4 lines
Quoted 1 line

There is 'stateless' already for 'firmware image in r/o flash'.

What's the behaviour of UEFI if build with JSON vars support, but without QEMU providing any JSON vars backend ?

It will panic.

In that case, we must not reuse 'stateless' with such builds, as that's quite different semantics & incompatible with current usage.

We would need a new 'uefi-vars' mode, or just declare that we must always use 'memory' not 'flash' for such builds.

Quoted 11 lines
Quoted 3 lines

We would want to expand the 'stateless' docs to mention that this feature flag indicates optional support for persistence in that case.

Optional is not possible do due to the way variable store support is organized in edk2. Firmware can't switch between smm and non-smm configuration at runtime for similar reasons.

take care,
Gerd

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 :|
#13 lore raw

Re: [PATCH v2 4/4] docs/firmware: add feature flag for qemu variable store

Hi,

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

There is 'stateless' already for 'firmware image in r/o flash'.

What's the behaviour of UEFI if build with JSON vars support, but without QEMU providing any JSON vars backend ?

Quoted 7 lines
Quoted 1 line

It will panic.

In that case, we must not reuse 'stateless' with such builds, as that's quite different semantics & incompatible with current usage.

We would need a new 'uefi-vars' mode, or just declare that we must always use 'memory' not 'flash' for such builds.

I don't see how 'flash.stateless' is any different than 'memory' (or 'kernel', or maybe 'igvm' some day). If the 'host-uefi-vars' feature is present '-device uefi-vars-$kind' is required, no matter how you load the firmware.

What exactly will an old libvirt which does not know the 'host-uefi-vars' feature do in case it finds a firmware.json file with that feature? Play safe and ignore the file?

take care,
Gerd

#14 lore raw

Re: [PATCH v2 4/4] docs/firmware: add feature flag for qemu variable store

Quoted 15 lines · Gerd Hoffmann

Hi,

Quoted 5 lines
Quoted 5 lines
Quoted 4 lines
Quoted 1 line

There is 'stateless' already for 'firmware image in r/o flash'.

What's the behaviour of UEFI if build with JSON vars support, but without QEMU providing any JSON vars backend ?

Quoted 7 lines
Quoted 1 line

It will panic.

In that case, we must not reuse 'stateless' with such builds, as that's quite different semantics & incompatible with current usage.

We would need a new 'uefi-vars' mode, or just declare that we must always use 'memory' not 'flash' for such builds.

Quoted 8 lines

I don't see how 'flash.stateless' is any different than 'memory' (or 'kernel', or maybe 'igvm' some day). If the 'host-uefi-vars' feature is present '-device uefi-vars-$kind' is required, no matter how you load the firmware.

What exactly will an old libvirt which does not know the 'host-uefi-vars' feature do in case it finds a firmware.json file with that feature? Play safe and ignore the file?

No, unknown features are simply ignored.

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 :|