Skip to contentSkip to conversation

block/vvfat: introduce "fat-size" option

PATCH In review Clément Chigot 2 participants 12 messages Nov 27, 2025 – Jan 6 Block layer

lore
Share & tools
lore
#0 lore raw

v3 cover block/vvfat: introduce "fat-size" option

The main goal of this series is to introduce a new option "fat-size" within the vvfat backend (patch 5).

This series also includes minor patches:
- patch 1 introduces another option to create unpartitionned disks.
- patch 2-4 are minor improvements easing the introducing of "fat-size" option

This was tested on with a aarch64-linux kernel taken from functional/aarch64/test-virt and on aarch64-qnx over raspi4b with a workaround, not included here (the SD bus must be associated to the EMMC2 port instead of through GPIOs).

Changes since v2:
- patch 1:
- change default: true for hard disk, false for false.
- remove "unpartitioned" keyword within filename
- patch 5
- rename option "fs-size"

Clément Chigot (5):
vvfat: introduce partitioned option
vvfat: move fat_type check prior to size setup
vvfat: add a define for VVFAT_SECTOR_BITS and VVFAT_SECTOR_SIZE
vvfat: move size parameters within driver structure
vvfat: add support for "fs-size" option

 block/vvfat.c        | 350 +++++++++++++++++++++++++++++++------------
 qapi/block-core.json |  16 +-
 2 files changed, 270 insertions(+), 96 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH v3 1/5 vvfat: introduce partitioned option

This option tells whether the volume should be partitioned or not. Its default varies: true for hard disks and false for floppy. Its prime effect is to prevent a master boot record (MBR) to be initialized.

This is useful as some operating system (QNX, Rtems) don't recognized FAT mounted disks (especially SD cards) if a MBR is present.

---
 block/vvfat.c        | 19 ++++++++++++++++---
 qapi/block-core.json | 10 +++++++---
 2 files changed, 23 insertions(+), 6 deletions(-)
block/vvfat.c +16 -3
index 814796d918..dd0b3689c1 100644
@@ -306,7 +306,8 @@ typedef struct BDRVVVFATState {
     array_t fat,directory,mapping;
     char volume_label[11];
 
-    uint32_t offset_to_bootsector; /* 0 for floppy, 0x3f for disk */
+    /* 0x3f for partitioned disk, 0x0 otherwise */
+    uint32_t offset_to_bootsector;
 
     unsigned int cluster_size;
     unsigned int sectors_per_cluster;
@@ -1082,6 +1083,11 @@ static QemuOptsList runtime_opts = {
             .type = QEMU_OPT_BOOL,
             .help = "Make the image writable",
         },
+        {
+            .name = "partitioned",
+            .type = QEMU_OPT_BOOL,
+            .help = "Do not add a Master Boot Record on this disk",
+        },
         { /* end of list */ }
     },
 };
@@ -1138,7 +1144,7 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 {
     BDRVVVFATState *s = bs->opaque;
     int cyls, heads, secs;
-    bool floppy;
+    bool floppy, partitioned;
     const char *dirname, *label;
     QemuOpts *opts;
     int ret;
@@ -1165,6 +1171,9 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
     s->fat_type = qemu_opt_get_number(opts, "fat-type", 0);
     floppy = qemu_opt_get_bool(opts, "floppy", false);
 
+    /* Hard disk are partitioned by default; floppy aren't.  */
+    partitioned = qemu_opt_get_bool(opts, "partitioned", floppy ? false : true);
+
     memset(s->volume_label, ' ', sizeof(s->volume_label));
     label = qemu_opt_get(opts, "label");
     if (label) {
@@ -1196,7 +1205,6 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
         if (!s->fat_type) {
             s->fat_type = 16;
         }
-        s->offset_to_bootsector = 0x3f;
         cyls = s->fat_type == 12 ? 64 : 1024;
         heads = 16;
         secs = 63;
@@ -1215,6 +1223,10 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 
+    /* Reserver space for MBR */
+    if (partitioned) {
+        s->offset_to_bootsector = 0x3f;
+    }
 
     s->bs = bs;
 
@@ -3246,6 +3258,7 @@ static const char *const vvfat_strong_runtime_opts[] = {
     "floppy",
     "label",
     "rw",
+    "partitioned",
 
     NULL
 };
qapi/block-core.json +7 -3
index b82af74256..ca438fba51 100644
@@ -3464,8 +3464,8 @@
 #
 # @fat-type: FAT type: 12, 16 or 32
 #
-# @floppy: whether to export a floppy image (true) or partitioned hard
-#     disk (false; default)
+# @floppy: whether to export a floppy image (true) or hard disk
+#     (false; default)
 #
 # @label: set the volume label, limited to 11 bytes.  FAT16 and FAT32
 #     traditionally have some restrictions on labels, which are
@@ -3474,11 +3474,15 @@
 #
 # @rw: whether to allow write operations (default: false)
 #
+# @partitioned: whether the volume will be partitioned;
+#     (default: true for hard disk, false for floppy)
+#     (since 10.2)
+#
 # Since: 2.9
 ##
 { 'struct': 'BlockdevOptionsVVFAT',
   'data': { 'dir': 'str', '*fat-type': 'int', '*floppy': 'bool',
-            '*label': 'str', '*rw': 'bool' } }
+            '*label': 'str', '*rw': 'bool', '*partitioned': 'bool' } }
 
 ##
 # @BlockdevOptionsGenericFormat:
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH v3 1/5] vvfat: introduce partitioned option

Quoted 28 lines · Clément Chigot

This option tells whether the volume should be partitioned or not. Its default varies: true for hard disks and false for floppy. Its prime effect is to prevent a master boot record (MBR) to be initialized.

This is useful as some operating system (QNX, Rtems) don't recognized FAT mounted disks (especially SD cards) if a MBR is present.

---
block/vvfat.c | 19 ++++++++++++++++---
qapi/block-core.json | 10 +++++++---

2 files changed, 23 insertions(+), 6 deletions(-)
block/vvfat.c +2 -1
index 814796d918..dd0b3689c1 100644
@@ -306,7 +306,8 @@ typedef struct BDRVVVFATState {
    array_t fat,directory,mapping;
    char volume_label[11];
 
-    uint32_t offset_to_bootsector; /* 0 for floppy, 0x3f for disk */
+    /* 0x3f for partitioned disk, 0x0 otherwise */
+    uint32_t offset_to_bootsector;
 
    unsigned int cluster_size;
    unsigned int sectors_per_cluster;

block/vvfat.c static QemuOptsList runtime_opts = { · line 1089

Quoted 7 lines
block/vvfat.c +4 -0
@@ -1082,6 +1083,11 @@ static QemuOptsList runtime_opts = {
            .type = QEMU_OPT_BOOL,
            .help = "Make the image writable",
        },
+        {
+            .name = "partitioned",
+            .type = QEMU_OPT_BOOL,
+            .help = "Do not add a Master Boot Record on this disk",

Maybe should say "Add MBR to disk" and not "Do not add" as that's what true means now.

Quoted 14 lines
block/vvfat.c +2 -1
+        },
        { /* end of list */ }
    },
 };
@@ -1138,7 +1144,7 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 {
    BDRVVVFATState *s = bs->opaque;
    int cyls, heads, secs;
-    bool floppy;
+    bool floppy, partitioned;
    const char *dirname, *label;
    QemuOpts *opts;
    int ret;

block/vvfat.c static int vvfat_open(BlockDriverState *bs, QDict *options, int flags, · line 1174

Quoted 4 lines
block/vvfat.c +1 -0
@@ -1165,6 +1171,9 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
    s->fat_type = qemu_opt_get_number(opts, "fat-type", 0);
    floppy = qemu_opt_get_bool(opts, "floppy", false);
 
+    /* Hard disk are partitioned by default; floppy aren't.  */

Singular plural mismatch. Either disks/floppies are/aren't or is/isn't when singular.

Quoted 25 lines
block/vvfat.c +6 -1
+    partitioned = qemu_opt_get_bool(opts, "partitioned", floppy ? false : true);
+
    memset(s->volume_label, ' ', sizeof(s->volume_label));
    label = qemu_opt_get(opts, "label");
    if (label) {
@@ -1196,7 +1205,6 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
        if (!s->fat_type) {
            s->fat_type = 16;
        }
-        s->offset_to_bootsector = 0x3f;
        cyls = s->fat_type == 12 ? 64 : 1024;
        heads = 16;
        secs = 63;
@@ -1215,6 +1223,10 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
        goto fail;
    }
 
+    /* Reserver space for MBR */
+    if (partitioned) {
+        s->offset_to_bootsector = 0x3f;
+    }
 
    s->bs = bs;
 

block/vvfat.c static const char *const vvfat_strong_runtime_opts[] = { · line 3261

Quoted 4 lines
block/vvfat.c +1 -0
@@ -3246,6 +3258,7 @@ static const char *const vvfat_strong_runtime_opts[] = {
    "floppy",
    "label",
    "rw",
+    "partitioned",

Does this also needs to be parsed in vvfat_parse_filename like other options seem to be?

Quoted 8 lines
block/vvfat.c +0 -0
    NULL
 };
qapi/block-core.json +0 -0
index b82af74256..ca438fba51 100644

qapi/block-core.json line 3468

Quoted 7 lines
qapi/block-core.json +2 -2
@@ -3464,8 +3464,8 @@
 #
 # @fat-type: FAT type: 12, 16 or 32
 #
-# @floppy: whether to export a floppy image (true) or partitioned hard
-#     disk (false; default)
+# @floppy: whether to export a floppy image (true) or hard disk
+#     (false; default)

I was wondering what does this option do now. It seems to restrict size to different values, maybe that could be explained here.

Regards,
BALATON Zoltan

Quoted 21 lines
qapi/block-core.json +5 -1
 #
 # @label: set the volume label, limited to 11 bytes.  FAT16 and FAT32
 #     traditionally have some restrictions on labels, which are
@@ -3474,11 +3474,15 @@
 #
 # @rw: whether to allow write operations (default: false)
 #
+# @partitioned: whether the volume will be partitioned;
+#     (default: true for hard disk, false for floppy)
+#     (since 10.2)
+#
 # Since: 2.9
 ##
 { 'struct': 'BlockdevOptionsVVFAT',
  'data': { 'dir': 'str', '*fat-type': 'int', '*floppy': 'bool',
-            '*label': 'str', '*rw': 'bool' } }
+            '*label': 'str', '*rw': 'bool', '*partitioned': 'bool' } }
 
 ##
 # @BlockdevOptionsGenericFormat:
#3 lore raw

Re: [PATCH v3 1/5] vvfat: introduce partitioned option

"

Quoted 87 lines · BALATON Zoltan
Quoted 28 lines · Clément Chigot

This option tells whether the volume should be partitioned or not. Its default varies: true for hard disks and false for floppy. Its prime effect is to prevent a master boot record (MBR) to be initialized.

This is useful as some operating system (QNX, Rtems) don't recognized FAT mounted disks (especially SD cards) if a MBR is present.

---
block/vvfat.c | 19 ++++++++++++++++---
qapi/block-core.json | 10 +++++++---

2 files changed, 23 insertions(+), 6 deletions(-)
block/vvfat.c +2 -1
index 814796d918..dd0b3689c1 100644
@@ -306,7 +306,8 @@ typedef struct BDRVVVFATState {
    array_t fat,directory,mapping;
    char volume_label[11];
 
-    uint32_t offset_to_bootsector; /* 0 for floppy, 0x3f for disk */
+    /* 0x3f for partitioned disk, 0x0 otherwise */
+    uint32_t offset_to_bootsector;
 
    unsigned int cluster_size;
    unsigned int sectors_per_cluster;

block/vvfat.c static QemuOptsList runtime_opts = { · line 1089

Quoted 7 lines
block/vvfat.c +4 -0
@@ -1082,6 +1083,11 @@ static QemuOptsList runtime_opts = {
            .type = QEMU_OPT_BOOL,
            .help = "Make the image writable",
        },
+        {
+            .name = "partitioned",
+            .type = QEMU_OPT_BOOL,
+            .help = "Do not add a Master Boot Record on this disk",

Maybe should say "Add MBR to disk" and not "Do not add" as that's what true means now.

Quoted 14 lines
block/vvfat.c +2 -1
+        },
        { /* end of list */ }
    },
 };
@@ -1138,7 +1144,7 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 {
    BDRVVVFATState *s = bs->opaque;
    int cyls, heads, secs;
-    bool floppy;
+    bool floppy, partitioned;
    const char *dirname, *label;
    QemuOpts *opts;
    int ret;

block/vvfat.c static int vvfat_open(BlockDriverState *bs, QDict *options, int flags, · line 1174

Quoted 4 lines
block/vvfat.c +1 -0
@@ -1165,6 +1171,9 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
    s->fat_type = qemu_opt_get_number(opts, "fat-type", 0);
    floppy = qemu_opt_get_bool(opts, "floppy", false);
 
+    /* Hard disk are partitioned by default; floppy aren't.  */

Singular plural mismatch. Either disks/floppies are/aren't or is/isn't when singular.

Quoted 24 lines
block/vvfat.c +6 -1
+    partitioned = qemu_opt_get_bool(opts, "partitioned", floppy ? false : true);
+
    memset(s->volume_label, ' ', sizeof(s->volume_label));
    label = qemu_opt_get(opts, "label");
    if (label) {
@@ -1196,7 +1205,6 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
        if (!s->fat_type) {
            s->fat_type = 16;
        }
-        s->offset_to_bootsector = 0x3f;
        cyls = s->fat_type == 12 ? 64 : 1024;
        heads = 16;
        secs = 63;
@@ -1215,6 +1223,10 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
        goto fail;
    }
 
+    /* Reserver space for MBR */
+    if (partitioned) {
+        s->offset_to_bootsector = 0x3f;
+    }
 
    s->bs = bs;
 
Quoted 8 lines

block/vvfat.c static const char *const vvfat_strong_runtime_opts[] = { · line 3261

Quoted 5 lines
block/vvfat.c +1 -0
@@ -3246,6 +3258,7 @@ static const char *const vvfat_strong_runtime_opts[] = {
    "floppy",
    "label",
    "rw",
+    "partitioned",

Does this also needs to be parsed in vvfat_parse_filename like other options seem to be?

It used to be the case, but I've removed that possibility in v3. This "vvfat_parse_filename" looks like a "hack" to ease setting vvfat options when instantiating such blocks through the format=raw. However, options can still be passed using "file." prefix. For example, "format=raw,file.partitioned=false" will create an unpartitioned vvfat block.
Overall, I think avoiding new options in the filename is a better cleaner approach.

Quoted 43 lines
Quoted 8 lines
block/vvfat.c +0 -0
    NULL
 };
qapi/block-core.json +0 -0
index b82af74256..ca438fba51 100644

qapi/block-core.json line 3468

Quoted 7 lines
qapi/block-core.json +2 -2
@@ -3464,8 +3464,8 @@
 #
 # @fat-type: FAT type: 12, 16 or 32
 #
-# @floppy: whether to export a floppy image (true) or partitioned hard
-#     disk (false; default)
+# @floppy: whether to export a floppy image (true) or hard disk
+#     (false; default)

I was wondering what does this option do now. It seems to restrict size to different values, maybe that could be explained here.

Regards,
BALATON Zoltan

Quoted 21 lines
qapi/block-core.json +5 -1
 #
 # @label: set the volume label, limited to 11 bytes.  FAT16 and FAT32
 #     traditionally have some restrictions on labels, which are
@@ -3474,11 +3474,15 @@
 #
 # @rw: whether to allow write operations (default: false)
 #
+# @partitioned: whether the volume will be partitioned;
+#     (default: true for hard disk, false for floppy)
+#     (since 10.2)
+#
 # Since: 2.9
 ##
 { 'struct': 'BlockdevOptionsVVFAT',
  'data': { 'dir': 'str', '*fat-type': 'int', '*floppy': 'bool',
-            '*label': 'str', '*rw': 'bool' } }
+            '*label': 'str', '*rw': 'bool', '*partitioned': 'bool' } }
 
 ##
 # @BlockdevOptionsGenericFormat:
#4 lore raw

Re: [PATCH v3 1/5] vvfat: introduce partitioned option

Quoted 1 line · Clément Chigot

On Thu, Nov 27, 2025 at 3:40 PM BALATON Zoltan <[email protected]>

Quoted 96 lines · wrote:
Quoted 87 lines · Clément Chigot
Quoted 28 lines

This option tells whether the volume should be partitioned or not. Its default varies: true for hard disks and false for floppy. Its prime effect is to prevent a master boot record (MBR) to be initialized.

This is useful as some operating system (QNX, Rtems) don't recognized FAT mounted disks (especially SD cards) if a MBR is present.

---
block/vvfat.c | 19 ++++++++++++++++---
qapi/block-core.json | 10 +++++++---

2 files changed, 23 insertions(+), 6 deletions(-)
block/vvfat.c +2 -1
index 814796d918..dd0b3689c1 100644
@@ -306,7 +306,8 @@ typedef struct BDRVVVFATState {
    array_t fat,directory,mapping;
    char volume_label[11];
 
-    uint32_t offset_to_bootsector; /* 0 for floppy, 0x3f for disk */
+    /* 0x3f for partitioned disk, 0x0 otherwise */
+    uint32_t offset_to_bootsector;
 
    unsigned int cluster_size;
    unsigned int sectors_per_cluster;

block/vvfat.c static QemuOptsList runtime_opts = { · line 1089

Quoted 7 lines
block/vvfat.c +4 -0
@@ -1082,6 +1083,11 @@ static QemuOptsList runtime_opts = {
            .type = QEMU_OPT_BOOL,
            .help = "Make the image writable",
        },
+        {
+            .name = "partitioned",
+            .type = QEMU_OPT_BOOL,
+            .help = "Do not add a Master Boot Record on this disk",

Maybe should say "Add MBR to disk" and not "Do not add" as that's what true means now.

Quoted 14 lines
block/vvfat.c +2 -1
+        },
        { /* end of list */ }
    },
 };
@@ -1138,7 +1144,7 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 {
    BDRVVVFATState *s = bs->opaque;
    int cyls, heads, secs;
-    bool floppy;
+    bool floppy, partitioned;
    const char *dirname, *label;
    QemuOpts *opts;
    int ret;

block/vvfat.c static int vvfat_open(BlockDriverState *bs, QDict *options, int flags, · line 1174

Quoted 4 lines
block/vvfat.c +1 -0
@@ -1165,6 +1171,9 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
    s->fat_type = qemu_opt_get_number(opts, "fat-type", 0);
    floppy = qemu_opt_get_bool(opts, "floppy", false);
 
+    /* Hard disk are partitioned by default; floppy aren't.  */

Singular plural mismatch. Either disks/floppies are/aren't or is/isn't when singular.

Quoted 24 lines
block/vvfat.c +6 -1
+    partitioned = qemu_opt_get_bool(opts, "partitioned", floppy ? false : true);
+
    memset(s->volume_label, ' ', sizeof(s->volume_label));
    label = qemu_opt_get(opts, "label");
    if (label) {
@@ -1196,7 +1205,6 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
        if (!s->fat_type) {
            s->fat_type = 16;
        }
-        s->offset_to_bootsector = 0x3f;
        cyls = s->fat_type == 12 ? 64 : 1024;
        heads = 16;
        secs = 63;
@@ -1215,6 +1223,10 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
        goto fail;
    }
 
+    /* Reserver space for MBR */
+    if (partitioned) {
+        s->offset_to_bootsector = 0x3f;
+    }
 
    s->bs = bs;
 
Quoted 8 lines

block/vvfat.c static const char *const vvfat_strong_runtime_opts[] = { · line 3261

Quoted 5 lines
block/vvfat.c +1 -0
@@ -3246,6 +3258,7 @@ static const char *const vvfat_strong_runtime_opts[] = {
    "floppy",
    "label",
    "rw",
+    "partitioned",

Does this also needs to be parsed in vvfat_parse_filename like other options seem to be?

Quoted 3 lines

It used to be the case, but I've removed that possibility in v3. This "vvfat_parse_filename" looks like a "hack" to ease setting vvfat options when instantiating such blocks through the format=raw.

If those aren't deprecated then I'd call it convenience than a hack.

Quoted 5 lines

However, options can still be passed using "file." prefix. For example, "format=raw,file.partitioned=false" will create an unpartitioned vvfat block.
Overall, I think avoiding new options in the filename is a better cleaner approach.

If options are possible to pass that way then omitting for some creates inconsistency so better be consistent unless we remove other options too. But I'd let the maintainer decide on this.

Regards,
BALATON Zoltan

#5 lore raw

PATCH v3 2/5 vvfat: move fat_type check prior to size setup

In reply to Clément Chigot

This allows to handle the default FAT size in a single place and make the following part taking care only about size parameters. It will be later moved away in a specific function.

The selection of floppy size was a bit unusual:
- fat-type undefined: a FAT 12 2880 Kib disk (default)
- fat-type=16: a FAT 16 2880 Kib disk
- fat-type=12: a FAT 12 1440 Kib disk

Now, that fat-type undefined means fat-type=12, it's no longer possible to make that size distinction. Therefore, it's being changed for the following:
- fat-type=12: a FAT 12 1440 Kib disk (default)
- fat-type=16: a FAT 16 2880 Kib dis

This has been choosen for two reasons: keep fat-type=12 the default and create a more usual size for it: 1440 Kib.

The possibility to create a FAT 12 2880 KiB floppy will be added back later, through the fs-size parameter.

---
 block/vvfat.c | 43 ++++++++++++++++++++++---------------------
 1 file changed, 22 insertions(+), 21 deletions(-)
block/vvfat.c +22 -21
index dd0b3689c1..baf04e678b 100644
@@ -1188,28 +1188,8 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
         memcpy(s->volume_label, "QEMU VVFAT", 10);
     }
 
-    if (floppy) {
-        /* 1.44MB or 2.88MB floppy.  2.88MB can be FAT12 (default) or FAT16. */
-        if (!s->fat_type) {
-            s->fat_type = 12;
-            secs = 36;
-            s->sectors_per_cluster = 2;
-        } else {
-            secs = s->fat_type == 12 ? 18 : 36;
-            s->sectors_per_cluster = 1;
-        }
-        cyls = 80;
-        heads = 2;
-    } else {
-        /* 32MB or 504MB disk*/
-        if (!s->fat_type) {
-            s->fat_type = 16;
-        }
-        cyls = s->fat_type == 12 ? 64 : 1024;
-        heads = 16;
-        secs = 63;
-    }
 
+    /* Verify FAT type  */
     switch (s->fat_type) {
     case 32:
         warn_report("FAT32 has not been tested. You are welcome to do so!");
@@ -1217,12 +1197,33 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
     case 16:
     case 12:
         break;
+    case 0:
+        /* Set a default type */
+        if (floppy) {
+            s->fat_type = 12;
+        } else {
+            s->fat_type = 16;
+        }
+        break;
     default:
         error_setg(errp, "Valid FAT types are only 12, 16 and 32");
         ret = -EINVAL;
         goto fail;
     }
 
+
+    if (floppy) {
+        /* Choose floppy size. 1440 KiB for FAT 12, 2880 KiB for FAT-16 */
+        secs = s->fat_type == 12 ? 18 : 36;
+        cyls = 80;
+        heads = 2;
+    } else {
+        /* 32MB or 504MB disk*/
+        cyls = s->fat_type == 12 ? 64 : 1024;
+        heads = 16;
+        secs = 63;
+    }
+
     /* Reserver space for MBR */
     if (partitioned) {
         s->offset_to_bootsector = 0x3f;
Signature · 1 line
2.43.0
#6 lore raw

Re: [PATCH v3 2/5] vvfat: move fat_type check prior to size setup

Quoted 82 lines · Clément Chigot

This allows to handle the default FAT size in a single place and make the following part taking care only about size parameters. It will be later moved away in a specific function.

The selection of floppy size was a bit unusual:
- fat-type undefined: a FAT 12 2880 Kib disk (default)
- fat-type=16: a FAT 16 2880 Kib disk
- fat-type=12: a FAT 12 1440 Kib disk

Now, that fat-type undefined means fat-type=12, it's no longer possible to make that size distinction. Therefore, it's being changed for the following:
- fat-type=12: a FAT 12 1440 Kib disk (default)
- fat-type=16: a FAT 16 2880 Kib dis

This has been choosen for two reasons: keep fat-type=12 the default and create a more usual size for it: 1440 Kib.

The possibility to create a FAT 12 2880 KiB floppy will be added back later, through the fs-size parameter.

---
block/vvfat.c | 43 ++++++++++++++++++++++---------------------

1 file changed, 22 insertions(+), 21 deletions(-)
block/vvfat.c +13 -21
index dd0b3689c1..baf04e678b 100644
@@ -1188,28 +1188,8 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
        memcpy(s->volume_label, "QEMU VVFAT", 10);
    }
 
-    if (floppy) {
-        /* 1.44MB or 2.88MB floppy.  2.88MB can be FAT12 (default) or FAT16. */
-        if (!s->fat_type) {
-            s->fat_type = 12;
-            secs = 36;
-            s->sectors_per_cluster = 2;
-        } else {
-            secs = s->fat_type == 12 ? 18 : 36;
-            s->sectors_per_cluster = 1;
-        }
-        cyls = 80;
-        heads = 2;
-    } else {
-        /* 32MB or 504MB disk*/
-        if (!s->fat_type) {
-            s->fat_type = 16;
-        }
-        cyls = s->fat_type == 12 ? 64 : 1024;
-        heads = 16;
-        secs = 63;
-    }
 
+    /* Verify FAT type  */
    switch (s->fat_type) {
    case 32:
        warn_report("FAT32 has not been tested. You are welcome to do so!");
@@ -1217,12 +1197,33 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
    case 16:
    case 12:
        break;
+    case 0:
+        /* Set a default type */
+        if (floppy) {
+            s->fat_type = 12;
+        } else {
+            s->fat_type = 16;
+        }
+        break;
    default:
        error_setg(errp, "Valid FAT types are only 12, 16 and 32");
        ret = -EINVAL;
        goto fail;
    }
 
+
+    if (floppy) {
+        /* Choose floppy size. 1440 KiB for FAT 12, 2880 KiB for FAT-16 */
+        secs = s->fat_type == 12 ? 18 : 36;

block/vvfat.c static int vvfat_open(BlockDriverState *bs, QDict *options, int flags, · line 1227

Quoted 10 lines
block/vvfat.c +9 -0
+        cyls = 80;
+        heads = 2;
+    } else {
+        /* 32MB or 504MB disk*/
+        cyls = s->fat_type == 12 ? 64 : 1024;
+        heads = 16;
+        secs = 63;
+    }
+
    /* Reserver space for MBR */

Previous patch but only noticed here. This may be correct in French but in English to many r-s in Reserve.

Regards,
BALATON Zoltan

Quoted 3 lines
block/vvfat.c +0 -0
    if (partitioned) {
        s->offset_to_bootsector = 0x3f;
#7 lore raw

PATCH v3 3/5 vvfat: add a define for VVFAT_SECTOR_BITS and VVFAT_SECTOR_SIZE

In reply to Clément Chigot

This removes some hardcoded 0x200 making them far clearer.

This also renames some BDRV_SECTOR_* constants that were introduced during the transitions for sector-based to bytes-based block interfaces. While they have the same values, the BDRV_SECTOR_* constants are unrelated to the VVFAT sectors.

---
 block/vvfat.c | 119 ++++++++++++++++++++++++++++----------------------
 1 file changed, 66 insertions(+), 53 deletions(-)
block/vvfat.c +66 -53
index baf04e678b..6b038067a2 100644
@@ -75,6 +75,9 @@ static void checkpoint(void);
  */
 #define BOOTSECTOR_OEM_NAME "MSWIN4.1"
 
+#define VVFAT_SECTOR_BITS 9
+#define VVFAT_SECTOR_SIZE (1ULL << VVFAT_SECTOR_BITS)
+
 #define DIR_DELETED 0xe5
 #define DIR_KANJI DIR_DELETED
 #define DIR_KANJI_FAKE 0x05
@@ -300,7 +303,7 @@ static void print_mapping(const struct mapping_t* mapping);
 typedef struct BDRVVVFATState {
     CoMutex lock;
     BlockDriverState* bs; /* pointer to parent */
-    unsigned char first_sectors[0x40*0x200];
+    unsigned char first_sectors[0x40 * VVFAT_SECTOR_SIZE];
 
     int fat_type; /* 16 or 32 */
     array_t fat,directory,mapping;
@@ -690,11 +693,11 @@ static inline void init_fat(BDRVVVFATState* s)
     if (s->fat_type == 12) {
         array_init(&(s->fat),1);
         array_ensure_allocated(&(s->fat),
-                s->sectors_per_fat * 0x200 * 3 / 2 - 1);
+                s->sectors_per_fat * VVFAT_SECTOR_SIZE * 3 / 2 - 1);
     } else {
         array_init(&(s->fat),(s->fat_type==32?4:2));
         array_ensure_allocated(&(s->fat),
-                s->sectors_per_fat * 0x200 / s->fat.item_size - 1);
+                s->sectors_per_fat * VVFAT_SECTOR_SIZE / s->fat.item_size - 1);
     }
     memset(s->fat.pointer,0,s->fat.size);
 
@@ -902,19 +905,19 @@ static int init_directories(BDRVVVFATState* s,
     unsigned int i;
     unsigned int cluster;
 
-    memset(&(s->first_sectors[0]),0,0x40*0x200);
+    memset(&(s->first_sectors[0]), 0 , 0x40 * VVFAT_SECTOR_SIZE);
 
-    s->cluster_size=s->sectors_per_cluster*0x200;
+    s->cluster_size = s->sectors_per_cluster * VVFAT_SECTOR_SIZE;
     s->cluster_buffer=g_malloc(s->cluster_size);
 
     /*
-     * The formula: sc = spf+1+spf*spc*(512*8/fat_type),
+     * The formula: sc = spf+1+spf*spc*(VVFAT_SECTOR_SIZE*8/fat_type),
      * where sc is sector_count,
      * spf is sectors_per_fat,
      * spc is sectors_per_clusters, and
      * fat_type = 12, 16 or 32.
      */
-    i = 1+s->sectors_per_cluster*0x200*8/s->fat_type;
+    i = 1 + s->sectors_per_cluster * VVFAT_SECTOR_SIZE * 8 / s->fat_type;
     s->sectors_per_fat=(s->sector_count+i)/i; /* round up */
 
     s->offset_to_fat = s->offset_to_bootsector + 1;
@@ -1012,12 +1015,13 @@ static int init_directories(BDRVVVFATState* s,
     s->current_mapping = NULL;
 
     bootsector = (bootsector_t *)(s->first_sectors
-                                  + s->offset_to_bootsector * 0x200);
+                                  + s->offset_to_bootsector
+                                  * VVFAT_SECTOR_SIZE);
     bootsector->jump[0]=0xeb;
     bootsector->jump[1]=0x3e;
     bootsector->jump[2]=0x90;
     memcpy(bootsector->name, BOOTSECTOR_OEM_NAME, 8);
-    bootsector->sector_size=cpu_to_le16(0x200);
+    bootsector->sector_size = cpu_to_le16(VVFAT_SECTOR_SIZE);
     bootsector->sectors_per_cluster=s->sectors_per_cluster;
     bootsector->reserved_sectors=cpu_to_le16(1);
     bootsector->number_of_fats=0x2; /* number of FATs */
@@ -1310,7 +1314,7 @@ fail:
 
 static void vvfat_refresh_limits(BlockDriverState *bs, Error **errp)
 {
-    bs->bl.request_alignment = BDRV_SECTOR_SIZE; /* No sub-sector I/O */
+    bs->bl.request_alignment = VVFAT_SECTOR_SIZE; /* No sub-sector I/O */
 }
 
 static inline void vvfat_close_current_file(BDRVVVFATState *s)
@@ -1496,21 +1500,23 @@ vvfat_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sector
         if (s->qcow) {
             int64_t n;
             int ret;
-            ret = bdrv_co_is_allocated(s->qcow->bs, sector_num * BDRV_SECTOR_SIZE,
-                                       (nb_sectors - i) * BDRV_SECTOR_SIZE, &n);
+            ret = bdrv_co_is_allocated(s->qcow->bs,
+                                       sector_num * VVFAT_SECTOR_SIZE,
+                                       (nb_sectors - i) * VVFAT_SECTOR_SIZE,
+                                       &n);
             if (ret < 0) {
                 return ret;
             }
             if (ret) {
                 DLOG(fprintf(stderr, "sectors %" PRId64 "+%" PRId64
                              " allocated\n", sector_num,
-                             n >> BDRV_SECTOR_BITS));
-                if (bdrv_co_pread(s->qcow, sector_num * BDRV_SECTOR_SIZE, n,
-                                  buf + i * 0x200, 0) < 0) {
+                             n >> VVFAT_SECTOR_BITS));
+                if (bdrv_co_pread(s->qcow, sector_num * VVFAT_SECTOR_SIZE,
+                                  n, buf + i * VVFAT_SECTOR_SIZE, 0) < 0) {
                     return -1;
                 }
-                i += (n >> BDRV_SECTOR_BITS) - 1;
-                sector_num += (n >> BDRV_SECTOR_BITS) - 1;
+                i += (n >> VVFAT_SECTOR_BITS) - 1;
+                sector_num += (n >> VVFAT_SECTOR_BITS) - 1;
                 continue;
             }
             DLOG(fprintf(stderr, "sector %" PRId64 " not allocated\n",
@@ -1518,19 +1524,20 @@ vvfat_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sector
         }
         if (sector_num < s->offset_to_root_dir) {
             if (sector_num < s->offset_to_fat) {
-                memcpy(buf + i * 0x200,
-                       &(s->first_sectors[sector_num * 0x200]),
-                       0x200);
+                memcpy(buf + i * VVFAT_SECTOR_SIZE,
+                       &(s->first_sectors[sector_num * VVFAT_SECTOR_SIZE]),
+                       VVFAT_SECTOR_SIZE);
             } else if (sector_num < s->offset_to_fat + s->sectors_per_fat) {
-                memcpy(buf + i * 0x200,
-                       &(s->fat.pointer[(sector_num
-                                       - s->offset_to_fat) * 0x200]),
-                       0x200);
+                memcpy(buf + i * VVFAT_SECTOR_SIZE,
+                       &(s->fat.pointer[(sector_num - s->offset_to_fat)
+                                        * VVFAT_SECTOR_SIZE]),
+                       VVFAT_SECTOR_SIZE);
             } else if (sector_num < s->offset_to_root_dir) {
-                memcpy(buf + i * 0x200,
+                memcpy(buf + i * VVFAT_SECTOR_SIZE,
                        &(s->fat.pointer[(sector_num - s->offset_to_fat
-                                       - s->sectors_per_fat) * 0x200]),
-                       0x200);
+                                         - s->sectors_per_fat)
+                                        * VVFAT_SECTOR_SIZE]),
+                       VVFAT_SECTOR_SIZE);
             }
         } else {
             uint32_t sector = sector_num - s->offset_to_root_dir,
@@ -1538,10 +1545,12 @@ vvfat_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sector
             cluster_num=sector/s->sectors_per_cluster;
             if(cluster_num > s->cluster_count || read_cluster(s, cluster_num) != 0) {
                 /* LATER TODO: strict: return -1; */
-                memset(buf+i*0x200,0,0x200);
+                memset(buf + i * VVFAT_SECTOR_SIZE, 0, VVFAT_SECTOR_SIZE);
                 continue;
             }
-            memcpy(buf+i*0x200,s->cluster+sector_offset_in_cluster*0x200,0x200);
+            memcpy(buf + i * VVFAT_SECTOR_SIZE,
+                   s->cluster + sector_offset_in_cluster * VVFAT_SECTOR_SIZE,
+                   VVFAT_SECTOR_SIZE);
         }
     }
     return 0;
@@ -1553,12 +1562,12 @@ vvfat_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
 {
     int ret;
     BDRVVVFATState *s = bs->opaque;
-    uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
-    int nb_sectors = bytes >> BDRV_SECTOR_BITS;
+    uint64_t sector_num = offset >> VVFAT_SECTOR_BITS;
+    int nb_sectors = bytes >> VVFAT_SECTOR_BITS;
     void *buf;
 
-    assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE));
-    assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
+    assert(QEMU_IS_ALIGNED(offset, VVFAT_SECTOR_SIZE));
+    assert(QEMU_IS_ALIGNED(bytes, VVFAT_SECTOR_SIZE));
 
     buf = g_try_malloc(bytes);
     if (bytes && buf == NULL) {
@@ -1824,8 +1833,8 @@ cluster_was_modified(BDRVVVFATState *s, uint32_t cluster_num)
     for (i = 0; !was_modified && i < s->sectors_per_cluster; i++) {
         was_modified = bdrv_co_is_allocated(s->qcow->bs,
                                             (cluster2sector(s, cluster_num) +
-                                             i) * BDRV_SECTOR_SIZE,
-                                            BDRV_SECTOR_SIZE, NULL);
+                                             i) * VVFAT_SECTOR_SIZE,
+                                            VVFAT_SECTOR_SIZE, NULL);
     }
 
     /*
@@ -1979,8 +1988,8 @@ get_cluster_count_for_direntry(BDRVVVFATState* s, direntry_t* direntry, const ch
                     int res;
 
                     res = bdrv_co_is_allocated(s->qcow->bs,
-                                               (offs + i) * BDRV_SECTOR_SIZE,
-                                               BDRV_SECTOR_SIZE, NULL);
+                                               (offs + i) * VVFAT_SECTOR_SIZE,
+                                               VVFAT_SECTOR_SIZE, NULL);
                     if (res < 0) {
                         return -1;
                     }
@@ -1989,9 +1998,9 @@ get_cluster_count_for_direntry(BDRVVVFATState* s, direntry_t* direntry, const ch
                         if (res) {
                             return -1;
                         }
-                        res = bdrv_co_pwrite(s->qcow, offs * BDRV_SECTOR_SIZE,
-                                             BDRV_SECTOR_SIZE, s->cluster_buffer,
-                                             0);
+                        res = bdrv_co_pwrite(s->qcow, offs * VVFAT_SECTOR_SIZE,
+                                             VVFAT_SECTOR_SIZE,
+                                             s->cluster_buffer, 0);
                         if (res < 0) {
                             return -2;
                         }
@@ -2169,7 +2178,7 @@ DLOG(checkpoint());
      * - if all is fine, return number of used clusters
      */
     if (s->fat2 == NULL) {
-        int size = 0x200 * s->sectors_per_fat;
+        int size = VVFAT_SECTOR_SIZE * s->sectors_per_fat;
         s->fat2 = g_malloc(size);
         memcpy(s->fat2, s->fat.pointer, size);
     }
@@ -2566,7 +2575,8 @@ commit_one_file(BDRVVVFATState* s, int dir_index, uint32_t offset)
                 (size > offset && c >=2 && !fat_eof(s, c)));
 
         ret = vvfat_read(s->bs, cluster2sector(s, c),
-            (uint8_t*)cluster, DIV_ROUND_UP(rest_size, 0x200));
+                         (uint8_t *)cluster,
+                         DIV_ROUND_UP(rest_size, VVFAT_SECTOR_SIZE));
 
         if (ret < 0) {
             qemu_close(fd);
@@ -2945,7 +2955,7 @@ static int coroutine_fn GRAPH_RDLOCK do_commit(BDRVVVFATState* s)
     }
 
     /* copy FAT (with bdrv_pread) */
-    memcpy(s->fat.pointer, s->fat2, 0x200 * s->sectors_per_fat);
+    memcpy(s->fat.pointer, s->fat2, VVFAT_SECTOR_SIZE * s->sectors_per_fat);
 
     /* recurse direntries from root (using bs->bdrv_pread) */
     ret = commit_direntries(s, 0, -1);
@@ -3009,14 +3019,15 @@ DLOG(checkpoint());
          * used to mark volume dirtiness
          */
         unsigned char *bootsector = s->first_sectors
-                                    + s->offset_to_bootsector * 0x200;
+                                    + s->offset_to_bootsector
+                                    * VVFAT_SECTOR_SIZE;
         /*
          * LATER TODO: if FAT32, this is wrong (see init_directories(),
          * which always creates a FAT16 bootsector)
          */
         const int reserved1_offset = offsetof(bootsector_t, u.fat16.reserved1);
 
-        for (i = 0; i < 0x200; i++) {
+        for (i = 0; i < VVFAT_SECTOR_SIZE; i++) {
             if (i != reserved1_offset && bootsector[i] != buf[i]) {
                 fprintf(stderr, "Tried to write to protected bootsector\n");
                 return -1;
@@ -3071,7 +3082,9 @@ DLOG(checkpoint());
                     end = sector_num + nb_sectors;
                 dir_index  = mapping->dir_index +
                     0x10 * (begin - mapping->begin * s->sectors_per_cluster);
-                direntries = (direntry_t*)(buf + 0x200 * (begin - sector_num));
+                direntries =
+                    (direntry_t *)(buf + VVFAT_SECTOR_SIZE
+                                   * (begin - sector_num));
 
                 for (k = 0; k < (end - begin) * 0x10; k++) {
                     /* no access to the direntry of a read-only file */
@@ -3097,8 +3110,8 @@ DLOG(checkpoint());
      * Use qcow backend. Commit later.
      */
 DLOG(fprintf(stderr, "Write to qcow backend: %d + %d\n", (int)sector_num, nb_sectors));
-    ret = bdrv_co_pwrite(s->qcow, sector_num * BDRV_SECTOR_SIZE,
-                         nb_sectors * BDRV_SECTOR_SIZE, buf, 0);
+    ret = bdrv_co_pwrite(s->qcow, sector_num * VVFAT_SECTOR_SIZE,
+                         nb_sectors * VVFAT_SECTOR_SIZE, buf, 0);
     if (ret < 0) {
         fprintf(stderr, "Error writing to qcow backend\n");
         return ret;
@@ -3124,12 +3137,12 @@ vvfat_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
 {
     int ret;
     BDRVVVFATState *s = bs->opaque;
-    uint64_t sector_num = offset >> BDRV_SECTOR_BITS;
-    int nb_sectors = bytes >> BDRV_SECTOR_BITS;
+    uint64_t sector_num = offset >> VVFAT_SECTOR_BITS;
+    int nb_sectors = bytes >> VVFAT_SECTOR_BITS;
     void *buf;
 
-    assert(QEMU_IS_ALIGNED(offset, BDRV_SECTOR_SIZE));
-    assert(QEMU_IS_ALIGNED(bytes, BDRV_SECTOR_SIZE));
+    assert(QEMU_IS_ALIGNED(offset, VVFAT_SECTOR_SIZE));
+    assert(QEMU_IS_ALIGNED(bytes, VVFAT_SECTOR_SIZE));
 
     buf = g_try_malloc(bytes);
     if (bytes && buf == NULL) {
@@ -3195,7 +3208,7 @@ static int enable_write_target(BlockDriverState *bs, Error **errp)
 
     opts = qemu_opts_create(bdrv_qcow->create_opts, NULL, 0, &error_abort);
     qemu_opt_set_number(opts, BLOCK_OPT_SIZE,
-                        bs->total_sectors * BDRV_SECTOR_SIZE, &error_abort);
+                        bs->total_sectors * VVFAT_SECTOR_SIZE, &error_abort);
     qemu_opt_set(opts, BLOCK_OPT_BACKING_FILE, "fat:", &error_abort);
 
     ret = bdrv_create(bdrv_qcow, s->qcow_filename, opts, errp);
Signature · 1 line
2.43.0
#8 lore raw

PATCH v3 4/5 vvfat: move size parameters within driver structure

In reply to Clément Chigot

At the same time, rename them to match bootsector fields.

---
 block/vvfat.c | 42 +++++++++++++++++++++++-------------------
 1 file changed, 23 insertions(+), 19 deletions(-)
block/vvfat.c +23 -19
index 6b038067a2..4e698f1ca7 100644
@@ -312,7 +312,10 @@ typedef struct BDRVVVFATState {
     /* 0x3f for partitioned disk, 0x0 otherwise */
     uint32_t offset_to_bootsector;
 
+    unsigned int cylinders;
     unsigned int cluster_size;
+    unsigned int number_of_heads;
+    unsigned int sectors_per_track;
     unsigned int sectors_per_cluster;
     unsigned int sectors_per_fat;
     uint32_t last_cluster_of_root_directory;
@@ -366,7 +369,7 @@ static int sector2CHS(mbr_chs_t *chs, int spos, int cyls, int heads, int secs)
     return 0;
 }
 
-static void init_mbr(BDRVVVFATState *s, int cyls, int heads, int secs)
+static void init_mbr(BDRVVVFATState *s)
 {
     /* TODO: if the files mbr.img and bootsect.img exist, use them */
     mbr_t* real_mbr=(mbr_t*)s->first_sectors;
@@ -382,9 +385,9 @@ static void init_mbr(BDRVVVFATState *s, int cyls, int heads, int secs)
 
     /* LBA is used when partition is outside the CHS geometry */
     lba  = sector2CHS(&partition->start_CHS, s->offset_to_bootsector,
-                     cyls, heads, secs);
+                      s->cylinders, s->number_of_heads, s->sectors_per_track);
     lba |= sector2CHS(&partition->end_CHS,   s->bs->total_sectors - 1,
-                     cyls, heads, secs);
+                      s->cylinders, s->number_of_heads, s->sectors_per_track);
 
     /*LBA partitions are identified only by start/length_sector_long not by CHS*/
     partition->start_sector_long  = cpu_to_le32(s->offset_to_bootsector);
@@ -896,8 +899,7 @@ static inline off_t cluster2sector(BDRVVVFATState* s, uint32_t cluster_num)
     return s->offset_to_root_dir + s->sectors_per_cluster * cluster_num;
 }
 
-static int init_directories(BDRVVVFATState* s,
-                            const char *dirname, int heads, int secs,
+static int init_directories(BDRVVVFATState *s, const char *dirname,
                             Error **errp)
 {
     bootsector_t* bootsector;
@@ -1031,8 +1033,8 @@ static int init_directories(BDRVVVFATState* s,
     bootsector->media_type = (s->offset_to_bootsector > 0 ? 0xf8 : 0xf0);
     s->fat.pointer[0] = bootsector->media_type;
     bootsector->sectors_per_fat=cpu_to_le16(s->sectors_per_fat);
-    bootsector->sectors_per_track = cpu_to_le16(secs);
-    bootsector->number_of_heads = cpu_to_le16(heads);
+    bootsector->sectors_per_track = cpu_to_le16(s->sectors_per_track);
+    bootsector->number_of_heads = cpu_to_le16(s->number_of_heads);
     bootsector->hidden_sectors = cpu_to_le32(s->offset_to_bootsector);
     bootsector->total_sectors=cpu_to_le32(s->sector_count>0xffff?s->sector_count:0);
 
@@ -1147,7 +1149,6 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
                       Error **errp)
 {
     BDRVVVFATState *s = bs->opaque;
-    int cyls, heads, secs;
     bool floppy, partitioned;
     const char *dirname, *label;
     QemuOpts *opts;
@@ -1218,14 +1219,14 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 
     if (floppy) {
         /* Choose floppy size. 1440 KiB for FAT 12, 2880 KiB for FAT-16 */
-        secs = s->fat_type == 12 ? 18 : 36;
-        cyls = 80;
-        heads = 2;
+        s->sectors_per_track = s->fat_type == 12 ? 18 : 36;
+        s->cylinders = 80;
+        s->number_of_heads = 2;
     } else {
         /* 32MB or 504MB disk*/
-        cyls = s->fat_type == 12 ? 64 : 1024;
-        heads = 16;
-        secs = 63;
+        s->cylinders = s->fat_type == 12 ? 64 : 1024;
+        s->number_of_heads = 16;
+        s->sectors_per_track = 63;
     }
 
     /* Reserver space for MBR */
@@ -1246,10 +1247,13 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
     s->downcase_short_names = 1;
 
     DLOG(fprintf(stderr, "vvfat %s chs %d,%d,%d\n",
-                 dirname, cyls, heads, secs));
+                 dirname, s->cylinders, s->number_of_heads,
+                 s->sectors_per_track));
 
-    s->sector_count = cyls * heads * secs - s->offset_to_bootsector;
-    bs->total_sectors = cyls * heads * secs;
+    s->sector_count = s->cylinders * s->number_of_heads *
+        s->sectors_per_track - s->offset_to_bootsector;
+    bs->total_sectors = s->cylinders * s->number_of_heads *
+        s->sectors_per_track;
 
     if (qemu_opt_get_bool(opts, "rw", false)) {
         if (!bdrv_is_read_only(bs)) {
@@ -1270,7 +1274,7 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
         }
     }
 
-    if (init_directories(s, dirname, heads, secs, errp)) {
+    if (init_directories(s, dirname, errp)) {
         ret = -EIO;
         goto fail;
     }
@@ -1291,7 +1295,7 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
     }
 
     if (s->offset_to_bootsector > 0) {
-        init_mbr(s, cyls, heads, secs);
+        init_mbr(s);
     }
 
     qemu_co_mutex_init(&s->lock);
Signature · 1 line
2.43.0
#9 lore raw

PATCH v3 5/5 vvfat: add support for "fs-size" option

In reply to Clément Chigot

This allows more flexibility to vvfat backend. The values of "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.

Due to the FAT architecture, not all sizes are reachable. Therefore, it could be round up to the closest available size.

FAT32 has not been adjusted and thus still default to 504 Mib.

For floppy, only 1440 Kib and 2880 Kib are supported.

The name of the option was made different from the classical "size" on purpose. This would avoid hardly-detactable errors when "format=vvfat" is forgotten; "size" being recognized by the default format "raw".

---
 block/vvfat.c        | 163 +++++++++++++++++++++++++++++++++++++++----
 qapi/block-core.json |   8 ++-
 2 files changed, 155 insertions(+), 16 deletions(-)
block/vvfat.c +148 -15
index 4e698f1ca7..f763ba6423 100644
@@ -1094,6 +1094,11 @@ static QemuOptsList runtime_opts = {
             .type = QEMU_OPT_BOOL,
             .help = "Do not add a Master Boot Record on this disk",
         },
+        {
+            .name = "fs-size",
+            .type = QEMU_OPT_SIZE,
+            .help = "Virtual disk size"
+        },
         { /* end of list */ }
     },
 };
@@ -1145,10 +1150,150 @@ static void vvfat_parse_filename(const char *filename, QDict *options,
     qdict_put_bool(options, "rw", rw);
 }
 
+static void vvfat_get_size_parameters(uint64_t size, BDRVVVFATState *s,
+                                      bool floppy, Error **errp)
+{
+    if (floppy) {
+        if (s->fat_type == 16) {
+            /* The default for floppy FAT 16 is 2880 KiB */
+            if (!size) {
+                size = 2880 * 1024;
+            }
+
+            if (size != 2880 * 1024) {
+                error_setg(errp,
+                           "floppy FAT16 unsupported size; only support "
+                           "2880 KiB");
+            }
+
+        } else {
+            /* The default for floppy FAT 12 is 1440 KiB */
+            if (!size) {
+                size = 1440 * 1024;
+            }
+
+            if (size != 2880 * 1024 && size != 1440 * 1024) {
+                error_setg(errp,
+                           "floppy FAT12 unsupported size; only support "
+                           "1440 KiB or 2880 KiB");
+            }
+
+        }
+
+        if (s->fat_type == 12) {
+            if (size == 2880 * 1024) {
+                s->sectors_per_track = 36;
+            } else {
+                s->sectors_per_track = 18;
+            }
+        } else {
+            s->sectors_per_track = 36;
+        }
+
+        s->sectors_per_cluster = 0x10;
+        s->cylinders = 80;
+        s->number_of_heads = 2;
+    } else {
+
+        switch (s->fat_type) {
+        case 32:
+            /* TODO FAT32 adjust  */
+            if (size) {
+                warn_report("size parameters not supported with FAT32;"
+                            "default to 504 MiB.");
+            }
+            s->cylinders = 1024;
+            s->number_of_heads = 16;
+            s->sectors_per_cluster = 0x10;
+            s->sectors_per_track = 63;
+            return;
+
+        case 12:
+
+            /* Default is 32 MB */
+            if (!size) {
+                size = 32 * 1024 * 1024;
+            } else if (size > 32 * 1024 * 1024) {
+                error_setg(errp, "FAT12 unsupported size; higher than 32 MiB");
+            }
+
+            s->sectors_per_cluster = 0x10;
+            s->cylinders = 64;
+
+            /*
+             * Based on CHS Recommandation table:
+             *  Card Capacity | Number of Headers | Sectors per track
+             *      2 MiB     |         4         |       16
+             *      4 MiB     |         8         |       16
+             *      8 MiB     |        16         |       16
+             *      16 MiB    |         2         |       32
+             *      32 MiB    |         4         |       32
+             *
+             * For 2 MiB, the recommendation is heads = 2 and sectors = 16, but
+             * this requires a different number of cylinders. Thus, it was
+             * adjusted to keep this number constant.
+             */
+            if (size <= 8 * 1024 * 1024) {
+                s->sectors_per_track = 16;
+            } else {
+                s->sectors_per_track = 32;
+            }
+
+            break;
+
+        case 16:
+            /* Default is 504 MiB */
+            if (!size) {
+                size = 504 * 1024 * 1024;
+            } else if (size / 1024 > 4 * 1024 * 1024) {
+                error_setg(errp, "FAT16 unsupported size; higher than 4 GiB");
+            }
+
+            s->sectors_per_cluster = 0x10;
+            s->cylinders = 1024;
+
+            /*
+             * Based on CHS Recommandation table:
+             *  Card Capacity | Number of Headers | Sectors per track
+             *      64 MiB    |         4         |       32
+             *     128 MiB    |         8         |       32
+             *     256 MiB    |        16         |       32
+             *     504 MiB    |        16         |       63
+             *    1008 MiB    |        32         |       63
+             *    2016 MiB    |        64         |       63
+             */
+            if (size <= 256 * 1024 * 1024) {
+                s->sectors_per_track = 32;
+            } else {
+                s->sectors_per_track = 63;
+            }
+
+            break;
+        }
+
+        /*
+         * The formula between the size (in bytes) and the parameters are:
+         *  size = VVFAT_SECTOR_SIZE * sectors_per_track * number_of_headers *
+         *         cylinders
+         *
+         */
+        s->number_of_heads = size / s->sectors_per_track /
+            VVFAT_SECTOR_SIZE / s->cylinders;
+
+        /* Round up to the closest size if the given one cannot be reached. */
+        if (size %
+            (s->sectors_per_track * VVFAT_SECTOR_SIZE * s->cylinders) != 0) {
+            s->number_of_heads++;
+        }
+
+    }
+}
+
 static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
                       Error **errp)
 {
     BDRVVVFATState *s = bs->opaque;
+    uint64_t size;
     bool floppy, partitioned;
     const char *dirname, *label;
     QemuOpts *opts;
@@ -1175,6 +1320,7 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 
     s->fat_type = qemu_opt_get_number(opts, "fat-type", 0);
     floppy = qemu_opt_get_bool(opts, "floppy", false);
+    size = qemu_opt_get_size_del(opts, "fs-size", 0);
 
     /* Hard disk are partitioned by default; floppy aren't.  */
     partitioned = qemu_opt_get_bool(opts, "partitioned", floppy ? false : true);
@@ -1216,18 +1362,7 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
         goto fail;
     }
 
-
-    if (floppy) {
-        /* Choose floppy size. 1440 KiB for FAT 12, 2880 KiB for FAT-16 */
-        s->sectors_per_track = s->fat_type == 12 ? 18 : 36;
-        s->cylinders = 80;
-        s->number_of_heads = 2;
-    } else {
-        /* 32MB or 504MB disk*/
-        s->cylinders = s->fat_type == 12 ? 64 : 1024;
-        s->number_of_heads = 16;
-        s->sectors_per_track = 63;
-    }
+    vvfat_get_size_parameters(size, s, floppy, errp);
 
     /* Reserver space for MBR */
     if (partitioned) {
@@ -1236,9 +1371,6 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
 
     s->bs = bs;
 
-    /* LATER TODO: if FAT32, adjust */
-    s->sectors_per_cluster=0x10;
-
     s->current_cluster=0xffffffff;
 
     s->qcow = NULL;
@@ -3277,6 +3409,7 @@ static const char *const vvfat_strong_runtime_opts[] = {
     "label",
     "rw",
     "partitioned",
+    "fs-size",
 
     NULL
 };
qapi/block-core.json +7 -1
index ca438fba51..fa4df7b408 100644
@@ -3478,11 +3478,17 @@
 #     (default: true for hard disk, false for floppy)
 #     (since 10.2)
 #
+# @fs-size: size of the device in bytes.  Due to FAT underlying
+#     architecture, this size can be rounded up to the closest valid
+#     size.
+#     (since 10.2)
+#
 # Since: 2.9
 ##
 { 'struct': 'BlockdevOptionsVVFAT',
   'data': { 'dir': 'str', '*fat-type': 'int', '*floppy': 'bool',
-            '*label': 'str', '*rw': 'bool', '*partitioned': 'bool' } }
+            '*label': 'str', '*rw': 'bool', '*partitioned': 'bool',
+            '*fs-size': 'int' } }
 
 ##
 # @BlockdevOptionsGenericFormat:
Signature · 1 line
2.43.0
#10 lore raw
In reply to Clément Chigot
Quoted 8 lines · Clément Chigot

The main goal of this series is to introduce a new option "fat-size" within the vvfat backend (patch 5).

This series also includes minor patches:
- patch 1 introduces another option to create unpartitionned disks.
- patch 2-4 are minor improvements easing the introducing of "fat-size" option

Quoted 8 lines

This was tested on with a aarch64-linux kernel taken from functional/aarch64/test-virt and on aarch64-qnx over raspi4b with a workaround, not included here (the SD bus must be associated to the EMMC2 port instead of through GPIOs).

Changes since v2:
- patch 1:
- change default: true for hard disk, false for false.

Typo: false for floppy

Quoted 3 lines

- remove "unpartitioned" keyword within filename
- patch 5
- rename option "fs-size"

I'm still not sure if it could be done simpler without adding a separate fs-size option and fix size to apply to vvfat instead of raw so it works as expected not how it works now. The idea I proposed was to try to set format to vvfat if file or protocol contains fat: which was discussed here:

https://lists.nongnu.org/archive/html/qemu-devel/2025-11/msg01487.html

but I did not understand Kevin's reply if it's possible or why not.

Regards,
BALATON Zoltan

Quoted 12 lines

Clément Chigot (5):
vvfat: introduce partitioned option
vvfat: move fat_type check prior to size setup
vvfat: add a define for VVFAT_SECTOR_BITS and VVFAT_SECTOR_SIZE
vvfat: move size parameters within driver structure
vvfat: add support for "fs-size" option

block/vvfat.c | 350 +++++++++++++++++++++++++++++++------------
qapi/block-core.json | 16 +-

2 files changed, 270 insertions(+), 96 deletions(-)
#11 lore raw
Quoted 31 lines · BALATON Zoltan
Quoted 8 lines · Clément Chigot

The main goal of this series is to introduce a new option "fat-size" within the vvfat backend (patch 5).

This series also includes minor patches:
- patch 1 introduces another option to create unpartitionned disks.
- patch 2-4 are minor improvements easing the introducing of "fat-size" option

Quoted 8 lines

This was tested on with a aarch64-linux kernel taken from functional/aarch64/test-virt and on aarch64-qnx over raspi4b with a workaround, not included here (the SD bus must be associated to the EMMC2 port instead of through GPIOs).

Changes since v2:
- patch 1:
- change default: true for hard disk, false for false.

Typo: false for floppy

Quoted 3 lines

- remove "unpartitioned" keyword within filename
- patch 5
- rename option "fs-size"

I'm still not sure if it could be done simpler without adding a separate fs-size option and fix size to apply to vvfat instead of raw so it works as expected not how it works now. The idea I proposed was to try to set format to vvfat if file or protocol contains fat: which was discussed here:

Quoted 3 lines

https://lists.nongnu.org/archive/html/qemu-devel/2025-11/msg01487.html

but I did not understand Kevin's reply if it's possible or why not.

My understanding is while it's possible to change the default format dynamically, we'd rather not go in that direction. It's not that trivial, meaning a much wider series of patches, and could change existing behavior, meaning some warnings to be introduced before making such changes.

Overall, I'd rather keep that series small (time you know...). So @Kevin Wolf, would you mind making a review pass on the current V3 ? The V4 has just a few comments/strings updates as per Zoltan feedbacks.

TIA
Clément

Quoted 15 lines

Regards,
BALATON Zoltan

Quoted 12 lines

Clément Chigot (5):
vvfat: introduce partitioned option
vvfat: move fat_type check prior to size setup
vvfat: add a define for VVFAT_SECTOR_BITS and VVFAT_SECTOR_SIZE
vvfat: move size parameters within driver structure
vvfat: add support for "fs-size" option

block/vvfat.c | 350 +++++++++++++++++++++++++++++++------------
qapi/block-core.json | 16 +-

2 files changed, 270 insertions(+), 96 deletions(-)