Skip to contentSkip to conversation

hw/arm: allow flash images being smaller than the available space

PATCH In review Gerd Hoffmann 3 participants 5 messages Dec 16, 2022

lore
Share & tools
lore
#0 lore raw

cover hw/arm: allow flash images being smaller than the available space

Gerd Hoffmann (2):
hw/arm: allow flash images being smaller than the available space
docs: add no-padding firmware feature

 hw/arm/virt.c              | 16 ++++++++++++++++
 docs/interop/firmware.json |  5 ++++-
 2 files changed, 20 insertions(+), 1 deletion(-)
Signature · 1 line
2.38.1
#1 lore raw

PATCH 1/2 hw/arm: allow flash images being smaller than the available space

Query block device backing flash for size and use that instead of requiring the block device being exactly 64M in size. This allows to use edk2 firmware builds without padding, i.e. use QEMU_EFI.fd (which is /way/ smaller than 64M) as-is.

-rw-r--r--. 1 root root 67108864 Dec 12 23:45 QEMU_EFI-pflash.raw -rw-r--r--. 1 root root 2097152 Dec 12 23:45 QEMU_EFI.fd

---
 hw/arm/virt.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
hw/arm/virt.c +16 -0
index b87135085610..c71ae2cd73f7 100644
@@ -43,6 +43,7 @@
 #include "hw/vfio/vfio-amd-xgbe.h"
 #include "hw/display/ramfb.h"
 #include "net/net.h"
+#include "sysemu/block-backend.h"
 #include "sysemu/device_tree.h"
 #include "sysemu/numa.h"
 #include "sysemu/runstate.h"
@@ -1134,6 +1135,21 @@ static void virt_flash_map1(PFlashCFI01 *flash,
                             MemoryRegion *sysmem)
 {
     DeviceState *dev = DEVICE(flash);
+    BlockBackend *blk;
+
+    blk = pflash_cfi01_get_blk(flash);
+    if (blk) {
+        hwaddr blksize = blk_getlength(blk);
+
+        if (blksize == 0 || blksize > size ||
+            !QEMU_IS_ALIGNED(size, VIRT_FLASH_SECTOR_SIZE)) {
+            error_report("system firmware block device %s"
+                         " has invalid size %" PRId64,
+                         blk_name(blk), size);
+            exit(1);
+        }
+        size = blksize;
+    }
 
     assert(QEMU_IS_ALIGNED(size, VIRT_FLASH_SECTOR_SIZE));
     assert(size / VIRT_FLASH_SECTOR_SIZE <= UINT32_MAX);
Signature · 1 line
2.38.1
#2 lore raw
Quoted 49 lines · Gerd Hoffmann

Query block device backing flash for size and use that instead of requiring the block device being exactly 64M in size. This allows to use edk2 firmware builds without padding, i.e. use QEMU_EFI.fd (which is /way/ smaller than 64M) as-is.

(quoted diff) +0 -2
-rw-r--r--. 1 root root 67108864 Dec 12 23:45 QEMU_EFI-pflash.raw
-rw-r--r--. 1 root root  2097152 Dec 12 23:45 QEMU_EFI.fd
---
 hw/arm/virt.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
hw/arm/virt.c +16 -1
index b87135085610..c71ae2cd73f7 100644
@@ -43,6 +43,7 @@
 #include "hw/vfio/vfio-amd-xgbe.h"
 #include "hw/display/ramfb.h"
 #include "net/net.h"
+#include "sysemu/block-backend.h"
 #include "sysemu/device_tree.h"
 #include "sysemu/numa.h"
 #include "sysemu/runstate.h"
@@ -1134,6 +1135,21 @@ static void virt_flash_map1(PFlashCFI01 *flash,
                             MemoryRegion *sysmem)
 {
     DeviceState *dev = DEVICE(flash);
+    BlockBackend *blk;
+
+    blk = pflash_cfi01_get_blk(flash);
+    if (blk) {
+        hwaddr blksize = blk_getlength(blk);
+
+        if (blksize == 0 || blksize > size ||
+            !QEMU_IS_ALIGNED(size, VIRT_FLASH_SECTOR_SIZE)) {
+            error_report("system firmware block device %s"
+                         " has invalid size %" PRId64,
+                         blk_name(blk), size);
+            exit(1);
+        }
+        size = blksize;
+    }
 
     assert(QEMU_IS_ALIGNED(size, VIRT_FLASH_SECTOR_SIZE));
     assert(size / VIRT_FLASH_SECTOR_SIZE <= UINT32_MAX);
--
Quoted 1 line

2.38.1

We've had at least three threads about this already, attempting various approaches. Please read up on them and the discussions that ensued from those patches before having another go at it.

The problem with this idea is that the size of the flash device exposed to the guest should not depend on the size of the backing file the user provides -- it's a fact about the machine and also if it varies it's easy for the user to back themselves into a corner where they can't migrate to a destination where the backing file is larger, or they can't add new variables to the EFI store because the backing file is too small.

thanks
-- PMM

#3 lore raw

PATCH 2/2 docs: add no-padding firmware feature

In reply to Gerd Hoffmann
---
 docs/interop/firmware.json | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
docs/interop/firmware.json +4 -1
index 56814f02b3c0..74f404d745b0 100644
@@ -191,6 +191,8 @@
 #                  PL011 UART. @verbose-static is mutually exclusive
 #                  with @verbose-dynamic.
 #
+# @no-padding: The (arm/aarch64) firmware images are not padded to 64M.
+#
 # Since: 3.0
 ##
 { 'enum' : 'FirmwareFeature',
@@ -198,7 +200,8 @@
              'amd-sev', 'amd-sev-es', 'amd-sev-snp',
              'intel-tdx',
              'enrolled-keys', 'requires-smm', 'secure-boot',
-             'verbose-dynamic', 'verbose-static' ] }
+             'verbose-dynamic', 'verbose-static',
+             'no-padding' ] }
 
 ##
 # @FirmwareFlashFile:
Signature · 1 line
2.38.1
Reviewed-by on v1 2/2
#4 lore raw

Re: [PATCH 2/2] docs: add no-padding firmware feature

On Fri, Dec 16, 2022 at 11:12:34AM +0100, Gerd Hoffmann wrote:

Hi,

Quoted 19 lines
---
 docs/interop/firmware.json | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
docs/interop/firmware.json +2 -0
index 56814f02b3c0..74f404d745b0 100644
@@ -191,6 +191,8 @@
 #                  PL011 UART. @verbose-static is mutually exclusive
 #                  with @verbose-dynamic.
 #
+# @no-padding: The (arm/aarch64) firmware images are not padded to 64M.
+#
 # Since: 3.0
 ##
 { 'enum' : 'FirmwareFeature',

docs/interop/firmware.json line 204

Quoted 6 lines
docs/interop/firmware.json +2 -1
@@ -198,7 +200,8 @@
              'amd-sev', 'amd-sev-es', 'amd-sev-snp',
              'intel-tdx',
              'enrolled-keys', 'requires-smm', 'secure-boot',
-             'verbose-dynamic', 'verbose-static' ] }
+             'verbose-dynamic', 'verbose-static',
+             'no-padding' ] }

If you're re-spinning, please consider adding a sentence or two (for those of us who're not familiar) a bit more about the "no-padding" feature to the commit message.

IIUC, I found the use of the padding feature reading an old email response[1] from Dan Berrangé:

(quote)
...
If there's a risk that newer firmware will be larger than old firmware there's only really two options:

- Keep all firmware images forever, each with a unique versioned filename. This ensures target QEMU will always load the original smaller firmware

- Add padding to the firmware images. IOW, if the firmware is 2 MB, add zero-padding to the end of the image to round it upto 4 MB (whatever you anticipate the largest size wil be in future).
...
(/quote)

[1] https://edk2.groups.io/g/devel/message/54758

Regardless:

Quoted 5 lines
docs/interop/firmware.json +0 -0
 ##
 # @FirmwareFlashFile:
Signature · 1 line
2.38.1
Signature · 1 line
/kashyap