qemu-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Qemu-devel] [RFC PATCH v6 2/4] hw/block: Pad undersized read-only image


From: Markus Armbruster
Subject: [Qemu-devel] [RFC PATCH v6 2/4] hw/block: Pad undersized read-only images with 0xFF
Date: Thu, 7 Mar 2019 10:37:21 +0100

From: Alex Bennée <address@hidden>

We reject undersized images.  As of the previous commit, even with a
decent error message.  Still, this is a potentially confusing
stumbling block when you move from using -bios to using -drive
if=pflash,file=blob,format=raw,readonly for loading your firmware
code. To mitigate that we automatically pad in the read-only case and
warn the user when we have performed magic to enable things to Just
Work (tm).

Signed-off-by: Alex Bennée <address@hidden>
Reviewed-by: Laszlo Ersek <address@hidden>
Signed-off-by: Markus Armbruster <address@hidden>
---
 hw/block/pflash_cfi01.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/hw/block/pflash_cfi01.c b/hw/block/pflash_cfi01.c
index 75ce8ef489..00980316dc 100644
--- a/hw/block/pflash_cfi01.c
+++ b/hw/block/pflash_cfi01.c
@@ -759,8 +759,9 @@ static void pflash_cfi01_realize(DeviceState *dev, Error 
**errp)
     if (pfl->blk) {
         /*
          * Validate the backing store is the right size for pflash
-         * devices. If the user supplies a larger file we ignore the
-         * tail.
+         * devices. If the device is read-only we can elide the check
+         * and just pad the region first. If the user supplies a
+         * larger file we ignore the tail.
          */
         int64_t backing_len = blk_getlength(pfl->blk);
         if (backing_len < 0) {
@@ -769,10 +770,21 @@ static void pflash_cfi01_realize(DeviceState *dev, Error 
**errp)
         }
 
         if (backing_len < total_len) {
-            error_setg(errp, "device needs %" PRIu64 " bytes, "
-                       "backing file provides only %" PRIu64 " bytes",
-                       total_len, backing_len);
-            return;
+            if (pfl->ro) {
+                size_t pad_bytes = total_len - backing_len;
+                /* pad with NOR erase pattern */
+                memset((uint8_t *)pfl->storage + backing_len,
+                       0xff, pad_bytes);
+                warn_report("device needs %" PRIu64
+                            " bytes, padded with %zu 0xff bytes",
+                            total_len, pad_bytes);
+                total_len = backing_len;
+            } else {
+                error_setg(errp, "device needs %" PRIu64 " bytes, "
+                           "backing file provides only %" PRIu64 " bytes",
+                           total_len, backing_len);
+                return;
+            }
         } else if (backing_len > total_len) {
             warn_report("device needs %" PRIu64 " bytes, rest ignored",
                         total_len);
-- 
2.17.2




reply via email to

[Prev in Thread] Current Thread [Next in Thread]