Skip to contentSkip to conversation

hw/sd/sdcard: Register device ops in drives without media

PATCH In review Alberto Garcia 2 participants 5 messages Jul 23 – Sep 2

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/sd/sdcard: Register device ops in drives without media

Commit a362b19a39 ("hw/sd/sdcard: Fix size check for backing block image") accidentally moved the blk_set_dev_ops() call into the new 'if (blk_size >= 0)' block in sd_realize().

Because of that, if a drive is attached without a medium then the condition is false (blk_size == -ENOMEDIUM) and blk_set_dev_ops() is never called, so the drive is registered as having non-removable media:

$QEMU -device sd-card,drive=sdcard0 -drive if=none,id=sdcard0 (qemu) change sdcard0 sd-card.qcow2
Error: Device 'sdcard0' is not removable

This patch moves the blk_set_perm() and blk_set_dev_ops() calls outside of the 'if (blk_size >= 0)' block so the device ops and permissions are registered when a drive is attached.

Fixes a362b19a39 ("hw/sd/sdcard: Fix size check for backing block image")
---
 hw/sd/sd.c | 47 +++++++++++++++++++++++------------------------
 1 file changed, 23 insertions(+), 24 deletions(-)
hw/sd/sd.c +23 -24
index a30c541df0..780e9f6295 100644
@@ -3100,7 +3100,6 @@ static void sd_blk_size_error(SDState *sd, int64_t blk_size,
 static void sd_realize(DeviceState *dev, Error **errp)
 {
     SDState *sd = SDMMC_COMMON(dev);
-    int64_t blk_size = -ENOMEDIUM;
     int ret;
 
     switch (sd->spec_version) {
@@ -3118,32 +3117,32 @@ static void sd_realize(DeviceState *dev, Error **errp)
             return;
         }
 
-        blk_size = blk_getlength(sd->blk);
-    }
-    if (blk_size >= 0) {
-        blk_size -= sd->boot_part_size * 2 + sd->rpmb_part_size;
-        if (blk_size > SDSC_MAX_CAPACITY) {
-            if (sd_is_emmc(sd) &&
-                !QEMU_IS_ALIGNED(blk_size, 1 << HWBLOCK_SHIFT)) {
-                int64_t blk_size_aligned =
-                    ((blk_size >> HWBLOCK_SHIFT) + 1) << HWBLOCK_SHIFT;
-                sd_blk_size_error(sd, blk_size, blk_size_aligned,
-                                  "multiples of 512", errp);
+        int64_t blk_size = blk_getlength(sd->blk);
+        if (blk_size >= 0) {
+            blk_size -= sd->boot_part_size * 2 + sd->rpmb_part_size;
+            if (blk_size > SDSC_MAX_CAPACITY) {
+                if (sd_is_emmc(sd) &&
+                    !QEMU_IS_ALIGNED(blk_size, 1 << HWBLOCK_SHIFT)) {
+                    int64_t blk_size_aligned =
+                        ((blk_size >> HWBLOCK_SHIFT) + 1) << HWBLOCK_SHIFT;
+                    sd_blk_size_error(sd, blk_size, blk_size_aligned,
+                                      "multiples of 512", errp);
+                    return;
+                } else if (!sd_is_emmc(sd) &&
+                    !QEMU_IS_ALIGNED(blk_size, 512 * KiB)) {
+                    int64_t blk_size_aligned = ((blk_size >> 19) + 1) << 19;
+                    sd_blk_size_error(sd, blk_size, blk_size_aligned,
+                                      "multiples of 512K", errp);
+                    return;
+                }
+            } else if (blk_size > 0 && !is_power_of_2(blk_size)) {
+                sd_blk_size_error(sd, blk_size, pow2ceil(blk_size),
+                                  "a power of 2", errp);
                 return;
-            } else if (!sd_is_emmc(sd) &&
-                !QEMU_IS_ALIGNED(blk_size, 512 * KiB)) {
-                int64_t blk_size_aligned = ((blk_size >> 19) + 1) << 19;
-                sd_blk_size_error(sd, blk_size, blk_size_aligned,
-                                  "multiples of 512K", errp);
+            } else if (blk_size < 0) {
+                error_setg(errp, "eMMC image smaller than boot partitions");
                 return;
             }
-        } else if (blk_size > 0 && !is_power_of_2(blk_size)) {
-            sd_blk_size_error(sd, blk_size, pow2ceil(blk_size), "a power of 2",
-                              errp);
-            return;
-        } else if (blk_size < 0) {
-            error_setg(errp, "eMMC image smaller than boot partitions");
-            return;
         }
 
         ret = blk_set_perm(sd->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
Signature · 1 line
2.47.3
#1 lore raw
Quoted 4 lines · Alberto Garcia
hw/sd/sd.c +0 -4
-    if (blk_size >= 0) {
-        blk_size -= sd->boot_part_size * 2 + sd->rpmb_part_size;
-        if (blk_size > SDSC_MAX_CAPACITY) {
-            if (sd_is_emmc(sd) &&

[...]

hw/sd/sd.c static void sd_realize(DeviceState *dev, Error **errp) · line 3124

Quoted 5 lines
hw/sd/sd.c +5 -0
+        int64_t blk_size = blk_getlength(sd->blk);
+        if (blk_size >= 0) {
+            blk_size -= sd->boot_part_size * 2 + sd->rpmb_part_size;
+            if (blk_size > SDSC_MAX_CAPACITY) {
+                if (sd_is_emmc(sd) &&

[...]

There's a bit of churn here because I'm moving a whole block inside another one, because I think that the resulting code reads better, but we can of course keep the patch simpler if you prefer:

@@ -3145,7 +3145,9 @@ static void sd_realize(DeviceState *dev, Error **errp)

             error_setg(errp, "eMMC image smaller than boot partitions");
             return;
         }

+ }

+    if (sd->blk) {
         ret = blk_set_perm(sd->blk, BLK_PERM_CONSISTENT_READ | BLK_PERM_WRITE,
                            BLK_PERM_ALL, errp);
         if (ret < 0) {

Berto

#2 lore raw
In reply to Alberto Garcia

ping

Quoted 3 lines · Alberto Garcia

This patch moves the blk_set_perm() and blk_set_dev_ops() calls outside of the 'if (blk_size >= 0)' block so the device ops and permissions are registered when a drive is attached.

#3 lore raw

Hi Alberto,

Quoted 7 lines · Alberto Garcia

ping

Quoted 3 lines · Alberto Garcia

This patch moves the blk_set_perm() and blk_set_dev_ops() calls outside of the 'if (blk_size >= 0)' block so the device ops and permissions are registered when a drive is attached.

I have your series tagged for to review and pick for my next sdmmc pull request which 1/ is long due and 2/ is scheduled for this week hopefully.

Sorry to keep you waiting...

Reviewed-by on v1 1/1
#4 lore raw
In reply to Alberto Garcia
Quoted 17 lines · Alberto Garcia

Commit a362b19a39 ("hw/sd/sdcard: Fix size check for backing block image") accidentally moved the blk_set_dev_ops() call into the new 'if (blk_size >= 0)' block in sd_realize().

Because of that, if a drive is attached without a medium then the condition is false (blk_size == -ENOMEDIUM) and blk_set_dev_ops() is never called, so the drive is registered as having non-removable media:

$QEMU -device sd-card,drive=sdcard0 -drive if=none,id=sdcard0 (qemu) change sdcard0 sd-card.qcow2
Error: Device 'sdcard0' is not removable

This patch moves the blk_set_perm() and blk_set_dev_ops() calls outside of the 'if (blk_size >= 0)' block so the device ops and permissions are registered when a drive is attached.

Quoted 5 lines
Fixes a362b19a39 ("hw/sd/sdcard: Fix size check for backing block image")
---
  hw/sd/sd.c | 47 +++++++++++++++++++++++------------------------
  1 file changed, 23 insertions(+), 24 deletions(-)

and queued, thanks!