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

Gerd Hoffmann posted 2 patches 3 years, 1 month ago
Maintainers: "Philippe Mathieu-Daudé" <philmd@linaro.org>, "Daniel P. Berrangé" <berrange@redhat.com>, Kashyap Chamarthy <kchamart@redhat.com>, Peter Maydell <peter.maydell@linaro.org>
[PATCH 1/2] hw/arm: allow flash images being smaller than the available space
Posted by Gerd Hoffmann 3 years, 1 month ago
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

Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
---
 hw/arm/virt.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)

diff --git a/hw/arm/virt.c b/hw/arm/virt.c
index b87135085610..c71ae2cd73f7 100644
--- a/hw/arm/virt.c
+++ b/hw/arm/virt.c
@@ -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);
-- 
2.38.1
Re: [PATCH 1/2] hw/arm: allow flash images being smaller than the available space
Posted by Peter Maydell 3 years, 1 month ago
On Fri, 16 Dec 2022 at 10:12, Gerd Hoffmann <kraxel@redhat.com> wrote:
>
> 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
>
> Signed-off-by: Gerd Hoffmann <kraxel@redhat.com>
> ---
>  hw/arm/virt.c | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
>
> diff --git a/hw/arm/virt.c b/hw/arm/virt.c
> index b87135085610..c71ae2cd73f7 100644
> --- a/hw/arm/virt.c
> +++ b/hw/arm/virt.c
> @@ -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);
> --
> 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