block/vvfat: introduce "size" option
cover block/vvfat: introduce "size" option
The main goal of this series is to introduce a new option "size" within the vvfat backend (patch 5). It allows more control over SD cards' size. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This series also includes minor patches:
- patch 1 introduces another option to remove the Master Boot Record (this is mandatory for QNX)
- patch 2-4 are minor improvements easing the introducing of "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).
Clément Chigot (5):
vvfat: introduce no-mbr option
vvfat: move fat_type check prior to size setup
vvfat: add a define for SECTOR_SIZE
vvfat: move size parameters within driver structure
vvfat: add support for "size" options
block/vvfat.c | 279 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 213 insertions(+), 66 deletions(-)
Signature · 1 line
2.34.1
PATCH 1/5 vvfat: introduce no-mbr option
This option when set prevents a master boot record (MBR) to be initialized. This is mandatory as some operating system don't recognized mounted disks if a MBR is present.
--- block/vvfat.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)
block/vvfat.c +15 -1
@@ -1082,6 +1082,11 @@ static QemuOptsList runtime_opts = { .type = QEMU_OPT_BOOL, .help = "Make the image writable", }, + { + .name = "no-mbr", + .type = QEMU_OPT_BOOL, + .help = "Do not add a Master Boot Record on this disk", + }, {/* end of list */ } }, };
@@ -1092,6 +1097,7 @@ static void vvfat_parse_filename(const char *filename, QDict *options,int fat_type =0 ;bool floppy = false;bool rw = false; +bool no_mbr = false;int i;if (!strstart (filename,"fat:" ,NULL )) {
@@ -1116,6 +1122,10 @@ static void vvfat_parse_filename(const char *filename, QDict *options, rw = true; } +if (strstr (filename,":no-mbr:" )) { + no_mbr = true; + } +/* Get the directory name without options */ i =strrchr (filename,':' ) - filename;assert (i >=3 );
@@ -1131,6 +1141,7 @@ static void vvfat_parse_filename(const char *filename, QDict *options,qdict_put_int (options,"fat-type" , fat_type);qdict_put_bool (options,"floppy" , floppy);qdict_put_bool (options,"rw" , rw); +qdict_put_bool (options,"no-mbr" , no_mbr); } static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,
@@ -1196,7 +1207,10 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,if (!s->fat_type) { s->fat_type =16 ; } - s->offset_to_bootsector =0x3f ; +/* Reserver space for MBR */ +if (!qemu_opt_get_bool (opts,"no-mbr" , false)) { + s->offset_to_bootsector =0x3f ; + } cyls = s->fat_type ==12 ?64 :1024 ; heads =16 ; secs =63 ;
Signature · 1 line
2.34.1
Re: [PATCH 1/5] vvfat: introduce no-mbr option
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 5 lines
This option when set prevents a master boot record (MBR) to be initialized. This is mandatory as some operating system don't recognized mounted disks if a MBR is present.
Signed-off-by Clément Chigot <[email protected]>
Can we actually give an example of such an OS in the commit message?
Quoted 9 lines
--- block/vvfat.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)block/vvfat.c +0
-0
block/vvfat.c static QemuOptsList runtime_opts = { · line 1089
Quoted 8 lines
block/vvfat.c +5
-0@@ -1082,6 +1082,11 @@ static QemuOptsList runtime_opts = { .type = QEMU_OPT_BOOL, .help = "Make the image writable", }, + { + .name = "no-mbr", + .type = QEMU_OPT_BOOL, + .help = "Do not add a Master Boot Record on this disk", + },
Let's keep option names positive to avoid double negations like 'no-mbr=false'. We can have an 'mbr' option that defaults to true. Or in fact, maybe calling it 'partitioned' would be easier to understand.
You need to update BlockdevOptionsVVFAT in qapi/block-core.json, too, to make the new option work with -blockdev. You should update the description for @floppy there, too, because it says that hard disks are always partitioned.
It should also be added to vvfat_strong_runtime_opts because the value of this option changes the data that the guest sees.
Quoted 12 lines
block/vvfat.c +1
-0{/* end of list */ } }, };@@ -1092,6 +1097,7 @@ static void vvfat_parse_filename(const char *filename, QDict *options,int fat_type =0 ;bool floppy = false;bool rw = false; +bool no_mbr = false;int i;if (!strstart (filename,"fat:" ,NULL )) {
block/vvfat.c static void vvfat_parse_filename(const char *filename, QDict *options, · line 1125
Quoted 4 lines
block/vvfat.c +1
-0@@ -1116,6 +1122,10 @@ static void vvfat_parse_filename(const char *filename, QDict *options, rw = true; } +if (strstr (filename,":no-mbr:" )) {
In the string, the negative form can stay (because the positive one doesn't exist here).
Quoted 16 lines
block/vvfat.c +4
-0+ no_mbr = true; + } +/* Get the directory name without options */ i =strrchr (filename,':' ) - filename;assert (i >=3 );@@ -1131,6 +1141,7 @@ static void vvfat_parse_filename(const char *filename, QDict *options,qdict_put_int (options,"fat-type" , fat_type);qdict_put_bool (options,"floppy" , floppy);qdict_put_bool (options,"rw" , rw); +qdict_put_bool (options,"no-mbr" , no_mbr); } static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,@@ -1196,7 +1207,10 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,if (!s->fat_type) {
block/vvfat.c static int vvfat_open(BlockDriverState *bs, QDict *options, int flags, · line 1216
Quoted 10 lines
block/vvfat.c +4
-1s->fat_type =16 ; } - s->offset_to_bootsector =0x3f ; +/* Reserver space for MBR */ +if (!qemu_opt_get_bool (opts,"no-mbr" , false)) { + s->offset_to_bootsector =0x3f ; + } cyls = s->fat_type ==12 ?64 :1024 ; heads =16 ; secs =63 ;
Kevin
Re: [PATCH 1/5] vvfat: introduce no-mbr option
Quoted 32 lines · Kevin Wolf
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 5 lines
This option when set prevents a master boot record (MBR) to be initialized. This is mandatory as some operating system don't recognized mounted disks if a MBR is present.
Signed-off-by Clément Chigot <[email protected]>Can we actually give an example of such an OS in the commit message?
Quoted 9 lines
--- block/vvfat.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)block/vvfat.c +0
-0block/vvfat.c static QemuOptsList runtime_opts = { · line 1089
Quoted 8 lines
block/vvfat.c +5
-0@@ -1082,6 +1082,11 @@ static QemuOptsList runtime_opts = { .type = QEMU_OPT_BOOL, .help = "Make the image writable", }, + { + .name = "no-mbr", + .type = QEMU_OPT_BOOL, + .help = "Do not add a Master Boot Record on this disk", + },Let's keep option names positive to avoid double negations like 'no-mbr=false'. We can have an 'mbr' option that defaults to true. Or in fact, maybe calling it 'partitioned' would be easier to understand.
Quoted 7 lines
You need to update BlockdevOptionsVVFAT in
qapi/block-core.json, too, to make the new option work with -blockdev. You should update the description for @floppy there, too, because it says that hard disks are always partitioned.It should also be added to
vvfat_strong_runtime_optsbecause the value of this option changes the data that the guest sees.
Just to keep you updated, I've seen your comments. I'm just waiting to see where the discussion of patch 5 leads before pushing v2.
Quoted 49 lines
Quoted 12 lines
block/vvfat.c +1
-0{/* end of list */ } }, };@@ -1092,6 +1097,7 @@ static void vvfat_parse_filename(const char *filename, QDict *options,int fat_type =0 ;bool floppy = false;bool rw = false; +bool no_mbr = false;int i;if (!strstart (filename,"fat:" ,NULL )) {block/vvfat.c static void vvfat_parse_filename(const char *filename, QDict *options, · line 1125
Quoted 4 lines
block/vvfat.c +1
-0@@ -1116,6 +1122,10 @@ static void vvfat_parse_filename(const char *filename, QDict *options, rw = true; } +if (strstr (filename,":no-mbr:" )) {In the string, the negative form can stay (because the positive one doesn't exist here).
Quoted 16 lines
block/vvfat.c +4
-0+ no_mbr = true; + } +/* Get the directory name without options */ i =strrchr (filename,':' ) - filename;assert (i >=3 );@@ -1131,6 +1141,7 @@ static void vvfat_parse_filename(const char *filename, QDict *options,qdict_put_int (options,"fat-type" , fat_type);qdict_put_bool (options,"floppy" , floppy);qdict_put_bool (options,"rw" , rw); +qdict_put_bool (options,"no-mbr" , no_mbr); } static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,@@ -1196,7 +1207,10 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,if (!s->fat_type) {block/vvfat.c static int vvfat_open(BlockDriverState *bs, QDict *options, int flags, · line 1216
Quoted 10 lines
block/vvfat.c +4
-1s->fat_type =16 ; } - s->offset_to_bootsector =0x3f ; +/* Reserver space for MBR */ +if (!qemu_opt_get_bool (opts,"no-mbr" , false)) { + s->offset_to_bootsector =0x3f ; + } cyls = s->fat_type ==12 ?64 :1024 ; heads =16 ; secs =63 ;Kevin
Re: [PATCH 1/5] vvfat: introduce no-mbr option
Am 29.10.2025 um 09:37 hat Clément Chigot geschrieben:
Quoted 38 lines · Kevin Wolf
Quoted 37 lines
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 5 lines
This option when set prevents a master boot record (MBR) to be initialized. This is mandatory as some operating system don't recognized mounted disks if a MBR is present.
Signed-off-by Clément Chigot <[email protected]>Can we actually give an example of such an OS in the commit message?
Quoted 9 lines
--- block/vvfat.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)block/vvfat.c +0
-0block/vvfat.c static QemuOptsList runtime_opts = { · line 1089
Quoted 8 lines
block/vvfat.c +5
-0@@ -1082,6 +1082,11 @@ static QemuOptsList runtime_opts = { .type = QEMU_OPT_BOOL, .help = "Make the image writable", }, + { + .name = "no-mbr", + .type = QEMU_OPT_BOOL, + .help = "Do not add a Master Boot Record on this disk", + },Let's keep option names positive to avoid double negations like 'no-mbr=false'. We can have an 'mbr' option that defaults to true. Or in fact, maybe calling it 'partitioned' would be easier to understand.
You need to update BlockdevOptionsVVFAT in
qapi/block-core.json, too, to make the new option work with -blockdev. You should update the description for @floppy there, too, because it says that hard disks are always partitioned.
Quoted 5 lines
Quoted 2 lines
It should also be added to
vvfat_strong_runtime_optsbecause the value of this option changes the data that the guest sees.Just to keep you updated, I've seen your comments. I'm just waiting to see where the discussion of patch 5 leads before pushing v2.
Yes, that makes sense. So this means that for all the other patches, you agree with my comments and it's clear to you what to change in v2?
Kevin
Re: [PATCH 1/5] vvfat: introduce no-mbr option
Quoted 40 lines · Kevin Wolf
Am 29.10.2025 um 09:37 hat Clément Chigot geschrieben:
Quoted 38 lines · Kevin Wolf
Quoted 37 lines
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 5 lines
This option when set prevents a master boot record (MBR) to be initialized. This is mandatory as some operating system don't recognized mounted disks if a MBR is present.
Signed-off-by Clément Chigot <[email protected]>Can we actually give an example of such an OS in the commit message?
Quoted 9 lines
--- block/vvfat.c | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-)block/vvfat.c +0
-0block/vvfat.c static QemuOptsList runtime_opts = { · line 1089
Quoted 8 lines
block/vvfat.c +5
-0@@ -1082,6 +1082,11 @@ static QemuOptsList runtime_opts = { .type = QEMU_OPT_BOOL, .help = "Make the image writable", }, + { + .name = "no-mbr", + .type = QEMU_OPT_BOOL, + .help = "Do not add a Master Boot Record on this disk", + },Let's keep option names positive to avoid double negations like 'no-mbr=false'. We can have an 'mbr' option that defaults to true. Or in fact, maybe calling it 'partitioned' would be easier to understand.
You need to update BlockdevOptionsVVFAT in
qapi/block-core.json, too, to make the new option work with -blockdev. You should update the description for @floppy there, too, because it says that hard disks are always partitioned.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
It should also be added to
vvfat_strong_runtime_optsbecause the value of this option changes the data that the guest sees.Just to keep you updated, I've seen your comments. I'm just waiting to see where the discussion of patch 5 leads before pushing v2.
Yes, that makes sense. So this means that for all the other patches, you agree with my comments and it's clear to you what to change in v2?
Globally yes. I'm still unsure about patch 2 (I'll answer directly on this patch). But for patch 1 and, yes clearly.
PATCH 2/5 vvfat: move fat_type check prior to size setup
In reply to Clément ChigotThis 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.
Setting a floppy disk of 1MB is no longer possible as it was a side effect of passing "fat-type=12". To be precise there were three cases:
- fat-type undefined (aka default): a fat12 2MB disk
- fat-type=16: a fat16 2Mb disk
- fat-type=12: a fat12 1Mb disk
Now, that fat-type undefined means fat-type=2, it's no longer possible to make that size distinction. It will be added back a bit later, through the size parameter.
--- block/vvfat.c | 48 +++++++++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 21 deletions(-)
block/vvfat.c +27 -21
@@ -1190,45 +1190,51 @@ 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) { +/* Verify FAT type */ +switch (s->fat_type) { +case 32 : +warn_report ("FAT32 has not been tested. You are welcome to do so!" ); +break ; +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) { +/* 2.88MB floppy */ +if (s->fat_type ==12 ) { secs =36 ; s->sectors_per_cluster =2 ; }else { - secs = s->fat_type ==12 ?18 :36 ; + secs =36 ; s->sectors_per_cluster =1 ; } cyls =80 ; heads =2 ; }else { -/* 32MB or 504MB disk*/ -if (!s->fat_type) { - s->fat_type =16 ; - }/* Reserver space for MBR */ if (!qemu_opt_get_bool (opts,"no-mbr" , false)) { s->offset_to_bootsector =0x3f ; } +/* 32MB or 504MB disk*/ cyls = s->fat_type ==12 ?64 :1024 ; heads =16 ; secs =63 ; } -switch (s->fat_type) { -case 32 : -warn_report ("FAT32 has not been tested. You are welcome to do so!" ); -break ; -case 16 : -case 12 : -break ; -default : -error_setg (errp,"Valid FAT types are only 12, 16 and 32" ); - ret = -EINVAL ; - goto fail; - } - s->bs = bs;
Signature · 1 line
2.34.1
Re: [PATCH 2/5] vvfat: move fat_type check prior to size setup
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 9 lines
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.
Setting a floppy disk of 1MB is no longer possible as it was a side effect of passing "fat-type=12". To be precise there were three cases:
- fat-type undefined (aka default): a fat12 2MB disk
- fat-type=16: a fat16 2Mb disk
- fat-type=12: a fat12 1Mb disk
That's quite a strange interface!
If we're touching it anyway, I would change it to make the more common format (1.44 MB) the default for FAT12 and make the 2.88 MB FAT12 floppy temporarily unavailable and later require an explicit size. This way both sizes would still be available using the fat-type.
Please say 1.44 MB and 2.88 MB in the commit message rather than 1MB (or even 1Mb, which might mean megabit). There were other sizes like 1.2 MB that are closer to 1 MB, so it's better to avoid that confusion.
Quoted 1 line
Now, that fat-type undefined means fat-type=2, it's no longer possible
s/2/12/
Quoted 4 lines
to make that size distinction. It will be added back a bit later, through the size parameter.
Signed-off-by Clément Chigot <[email protected]>
The code looks good, apart from the change I suggested above (making 1.44 MB the default for FAT12).
Kevin
Re: [PATCH 2/5] vvfat: move fat_type check prior to size setup
Quoted 12 lines · Kevin Wolf
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 9 lines
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.
Setting a floppy disk of 1MB is no longer possible as it was a side effect of passing "fat-type=12". To be precise there were three cases:
- fat-type undefined (aka default): a fat12 2MB disk
- fat-type=16: a fat16 2Mb disk
- fat-type=12: a fat12 1Mb disk
Quoted 6 lines
That's quite a strange interface!
If we're touching it anyway, I would change it to make the more common format (1.44 MB) the default for FAT12 and make the 2.88 MB FAT12 floppy temporarily unavailable and later require an explicit size. This way both sizes would still be available using the fat-type.
I'm a bit hesitant to change the default behavior as people might be using it without clear knowledge of it. True, "floppy" is probably not a widely used feature but still.
Do QEMU have some specific guidelines when changing such default behavior ? Adding a warning ? Or just a comment in the changelog would be enough ?
Quoted 18 lines
Please say 1.44 MB and 2.88 MB in the commit message rather than 1MB (or even 1Mb, which might mean megabit). There were other sizes like 1.2 MB that are closer to 1 MB, so it's better to avoid that confusion.
Quoted 1 line
Now, that fat-type undefined means fat-type=2, it's no longer possible
s/2/12/
Quoted 4 lines
to make that size distinction. It will be added back a bit later, through the size parameter.
Signed-off-by Clément Chigot <[email protected]>The code looks good, apart from the change I suggested above (making 1.44 MB the default for FAT12).
Kevin
Re: [PATCH 2/5] vvfat: move fat_type check prior to size setup
Quoted 20 lines · Clément Chigot
Quoted 12 lines · Kevin Wolf
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 9 lines
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.
Setting a floppy disk of 1MB is no longer possible as it was a side effect of passing "fat-type=12". To be precise there were three cases:
- fat-type undefined (aka default): a fat12 2MB disk
- fat-type=16: a fat16 2Mb disk
- fat-type=12: a fat12 1Mb diskQuoted 6 lines
That's quite a strange interface!
If we're touching it anyway, I would change it to make the more common format (1.44 MB) the default for FAT12 and make the 2.88 MB FAT12 floppy temporarily unavailable and later require an explicit size. This way both sizes would still be available using the fat-type.
Quoted 6 lines
I'm a bit hesitant to change the default behavior as people might be using it without clear knowledge of it. True, "floppy" is probably not a widely used feature but still.
Do QEMU have some specific guidelines when changing such default behavior ? Adding a warning ? Or just a comment in the changelog would be enough ?
https://www.qemu.org/docs/master/about/deprecated.html
Usually you'd add a warning and list it in the docs as deprecated then can make the change after it was deperecated for two releases.
Regards,
BALATON Zoltan
Re: [PATCH 2/5] vvfat: move fat_type check prior to size setup
Am 29.10.2025 um 14:58 hat BALATON Zoltan geschrieben:
Quoted 28 lines · Clément Chigot
Quoted 20 lines · Kevin Wolf
Quoted 12 lines
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 9 lines
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.
Setting a floppy disk of 1MB is no longer possible as it was a side effect of passing "fat-type=12". To be precise there were three cases:
- fat-type undefined (aka default): a fat12 2MB disk
- fat-type=16: a fat16 2Mb disk
- fat-type=12: a fat12 1Mb diskQuoted 6 lines
That's quite a strange interface!
If we're touching it anyway, I would change it to make the more common format (1.44 MB) the default for FAT12 and make the 2.88 MB FAT12 floppy temporarily unavailable and later require an explicit size. This way both sizes would still be available using the fat-type.
Quoted 6 lines
I'm a bit hesitant to change the default behavior as people might be using it without clear knowledge of it. True, "floppy" is probably not a widely used feature but still.
Do QEMU have some specific guidelines when changing such default behavior ? Adding a warning ? Or just a comment in the changelog would be enough ?
Quoted 4 lines
https://www.qemu.org/docs/master/about/deprecated.html
Usually you'd add a warning and list it in the docs as deprecated then can make the change after it was deperecated for two releases.
We're not removing any functionality, just changing the default. So I don't think the deprecation period applies.
In general, our stance is that we can change defaults whenever we want to, and if you don't want to be surprised by changing defaults, you need to specify the option explicitly. What's a bit strange about the vvfat interface is that the default actually represents a configuration that can't even be expressed explicitly at the moment.
So it is a special case in a way, but given that this is vvfat, which is known to be unstable, not widely used outside of the occasional manual use and not supported by libvirt, I'm willing to just make the change.
Kevin
PATCH 3/5 vvfat: add a define for SECTOR_SIZE
In reply to Clément ChigotThis makes those 0x200 far clearer.
--- block/vvfat.c | 60 ++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 27 deletions(-)
block/vvfat.c +33 -27
@@ -75,6 +75,8 @@ static void checkpoint(void);*/ #define BOOTSECTOR_OEM_NAME "MSWIN4.1" +#define SECTOR_SIZE 0x200 +#define DIR_DELETED 0xe5#define DIR_KANJI DIR_DELETED#define DIR_KANJI_FAKE 0x05
@@ -300,7 +302,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 * SECTOR_SIZE]; int fat_type; /* 16 or 32 */ array_t fat,directory,mapping;
@@ -689,11 +691,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 *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 *SECTOR_SIZE / s->fat.item_size -1 ); }memset (s->fat.pointer,0 ,s->fat.size);
@@ -901,19 +903,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 *SECTOR_SIZE ); - s->cluster_size=s->sectors_per_cluster*0x200 ; + s->cluster_size = s->sectors_per_cluster *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*(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 *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 ;
@@ -1011,12 +1013,12 @@ 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 *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 (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 */
@@ -1513,7 +1515,7 @@ vvfat_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sector" 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 ) { + buf + i *SECTOR_SIZE ,0 ) <0 ) {return -1 ; } i += (n >>BDRV_SECTOR_BITS ) -1 ;
@@ -1525,19 +1527,19 @@ 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 *SECTOR_SIZE , + &(s->first_sectors[sector_num *SECTOR_SIZE ]), +SECTOR_SIZE ); }else if (sector_num < s->offset_to_fat + s->sectors_per_fat) { -memcpy (buf + i *0x200 , +memcpy (buf + i *SECTOR_SIZE , &(s->fat.pointer[(sector_num - - s->offset_to_fat) *0x200 ]), -0x200 ); + - s->offset_to_fat) *SECTOR_SIZE ]), +SECTOR_SIZE ); }else if (sector_num < s->offset_to_root_dir) { -memcpy (buf + i *0x200 , +memcpy (buf + i *SECTOR_SIZE , &(s->fat.pointer[(sector_num - s->offset_to_fat - - s->sectors_per_fat) *0x200 ]), -0x200 ); + - s->sectors_per_fat) *SECTOR_SIZE ]), +SECTOR_SIZE ); } }else { uint32_t sector = sector_num - s->offset_to_root_dir,
@@ -1545,10 +1547,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 *SECTOR_SIZE ,0 ,SECTOR_SIZE );continue ; } -memcpy (buf+i*0x200 ,s->cluster+sector_offset_in_cluster*0x200 ,0x200 ); +memcpy (buf + i *SECTOR_SIZE , + s->cluster + sector_offset_in_cluster *SECTOR_SIZE , +SECTOR_SIZE ); } }return 0 ;
@@ -2176,7 +2180,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 =SECTOR_SIZE * s->sectors_per_fat; s->fat2 =g_malloc (size);memcpy (s->fat2, s->fat.pointer, size); }
@@ -2573,7 +2577,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,SECTOR_SIZE ));if (ret <0 ) {qemu_close (fd);
@@ -2952,7 +2957,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,SECTOR_SIZE * s->sectors_per_fat);/* recurse direntries from root (using bs->bdrv_pread) */ ret =commit_direntries (s,0 ,-1 );
@@ -3016,14 +3021,14 @@ DLOG(checkpoint());* used to mark volume dirtiness */ unsigned char *bootsector = s->first_sectors - + s->offset_to_bootsector *0x200 ; + + s->offset_to_bootsector *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 <SECTOR_SIZE ; i++) {if (i != reserved1_offset && bootsector[i] != buf[i]) {fprintf (stderr,"Tried to write to protected bootsector\n" );return -1 ;
@@ -3078,7 +3083,8 @@ 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 +SECTOR_SIZE * (begin - sector_num));for (k =0 ; k < (end - begin) *0x10 ; k++) {/* no access to the direntry of a read-only file */
Signature · 1 line
2.34.1
Re: [PATCH 3/5] vvfat: add a define for SECTOR_SIZE
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
block/vvfat.c vvfat_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sector · line 1518
Quoted 13 lines
This makes those
0x200far clearer.Signed-off-by Clément Chigot <[email protected]>--- block/vvfat.c | 60 ++++++++++++++++++++++++++++----------------------- 1 file changed, 33 insertions(+), 27 deletions(-)block/vvfat.c +1
-1@@ -1513,7 +1515,7 @@ vvfat_read(BlockDriverState *bs, int64_t sector_num, uint8_t *buf, int nb_sector" 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 ) { + buf + i *SECTOR_SIZE ,0 ) <0 ) {
We get a nasty mix of BDRV_SECTOR_SIZE (the QEMU block layer's sector size) and the new SECTOR_SIZE (the FAT file system's sector size) here. I think both of these actually refer to FAT.
Should we also change those instances of BDRV_SECTOR_SIZE that really should be SECTOR_SIZE?
Kevin
PATCH 4/5 vvfat: move size parameters within driver structure
In reply to Clément ChigotAt the same time, rename them to match bootsector fields.
--- block/vvfat.c | 44 ++++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 20 deletions(-)
block/vvfat.c +24 -20
@@ -310,7 +310,10 @@ typedef struct BDRVVVFATState {uint32_t offset_to_bootsector;/* 0 for floppy, 0x3f for disk */ +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;
@@ -364,7 +367,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;
@@ -380,9 +383,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);
@@ -894,8 +897,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;
@@ -1028,8 +1030,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 );
@@ -1150,7 +1152,6 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags, Error **errp) {BDRVVVFATState *s = bs->opaque; -int cyls, heads, secs;bool floppy;const char *dirname, *label;QemuOpts *opts;
@@ -1218,23 +1219,23 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags,if (floppy) {/* 2.88MB floppy */ if (s->fat_type ==12 ) { - secs =36 ; + s->sectors_per_track =36 ; s->sectors_per_cluster =2 ; }else { - secs =36 ; + s->sectors_per_track =36 ; s->sectors_per_cluster =1 ; } - cyls =80 ; - heads =2 ; + s->cylinder =80 ; + s->number_of_heads =2 ; }else {/* Reserver space for MBR */ if (!qemu_opt_get_bool (opts,"no-mbr" , false)) { s->offset_to_bootsector =0x3f ; }/* 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 ; }
@@ -1251,10 +1252,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)) {
@@ -1275,7 +1279,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; }
@@ -1296,7 +1300,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.34.1
PATCH 5/5 vvfat: add support for "size" options
In reply to Clément ChigotThis allows more flexibility to vvfat backend. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
Some limitations remains, the size parameter is recognized only when "format=vvfat" is passed. In particular, "format=raw,size=xxx" is keeping the previously hardcoded value: 504MB for FAT16 and 32 MB for FAT12. FAT32 has not been adjusted and thus still default to 504MB.
Moreover, for flopyy, size=1M is creating a disk 1.44 MB, and size=2M a disk of 2.88 MB. This avoids having to worry about float operations.
--- block/vvfat.c | 165 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 141 insertions(+), 24 deletions(-)
block/vvfat.c +141 -24
@@ -1091,6 +1091,11 @@ static QemuOptsList runtime_opts = { .type = QEMU_OPT_BOOL, .help = "Do not add a Master Boot Record on this disk", }, + { + .name = BLOCK_OPT_SIZE, + .type = QEMU_OPT_SIZE, + .help = "Virtual disk size" + }, {/* end of list */ } }, };
@@ -1148,10 +1153,141 @@ static void vvfat_parse_filename(const char *filename, QDict *options,qdict_put_bool (options,"no-mbr" , no_mbr); } +static void vvfat_get_size_parameters (uint64_t size,BDRVVVFATState *s, +bool floppy,Error **errp) +{ +if (floppy) { +/* +* Floppy emulation only supports 1.44 MB or 2.88 MB (default). +* In order to avoid floating operations ambiguity, 1 MB is +* recognized for 1.44 MB and 2 MB for 2.88 MB. +*/ +if (!size) { + size =2 *1024 *1024 ; + }else { +if (size ==1024 *1024 && s->fat_type ==16 ) { +error_setg (errp, +"floppy FAT16 unsupported size; only support 2M " +"(for an effective size of 2.88 MB)" ); + }else if (size !=2 *1024 *1024 && size !=1024 *1024 ) { +error_setg (errp, +"floppy unsupported size; should be 1MB (for " +"an effective size of 1.44 MB) or 2.88M (for " +"2.88MB)" ); + } + } + +if (s->fat_type ==12 ) { +if (size ==2 *1024 *1024 ) { + s->sectors_per_cluster =2 ; + }else { + s->sectors_per_cluster =1 ; + } + }else { + s->sectors_per_cluster =1 ; + } + + s->sectors_per_track =36 ; + s->cylinders =80 ; + s->number_of_heads =2 ; + }else { +/* LATER TODO: if FAT32, adjust */ + s->sectors_per_cluster =0x10 ; + +switch (s->fat_type) { +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 32Mb" ); + } + + s->cylinders =64 ; + +/* +* Based on CHS Recommandation table: +* Card Capacity | Number of Headers | Sectors per track +* ~ 2 MB | 4 | 16 +* ~ 4 MB | 8 | 16 +* ~ 8 MB | 16 | 16 +* ~ 16 MB | 2 | 32 +* ~ 32 MB | 4 | 32 +* +* For 2 MB, SD is recommending 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 ; + } + +/* +* The formula between the size (in bytes) and the parameters are: +* size = SECTOR_SIZE * sectors_per_track * number_of_headers * +* cylinders +*/ + s->number_of_heads = size / s->sectors_per_track / +SECTOR_SIZE / s->cylinders; +return ; + +case 16 : +/* Default is 504 MB */ +if (!size) { + size =504 *1024 *1024 ; + }else if (size /1024 >4 *1024 *1024 ) { +error_setg (errp,"FAT16 unsupported size; higher than 4Gb" ); + } + + s->cylinders =1024 ; + +/* +* Based on CHS Recommandation table: +* Card Capacity | Number of Headers | Sectors per track +* ~64 MB | 4 | 32 +* ~128 MB | 8 | 32 +* ~256 MB | 16 | 32 +* ~504 MB | 16 | 63 +* ~1008 MB | 32 | 63 +* ~2016 MB | 64 | 63 +*/ +if (size <=256 *1024 *1024 ) { + s->sectors_per_track =32 ; + }else { + s->sectors_per_track =63 ; + } + +/* +* The formula between the size (in bytes) and the parameters are: +* size = SECTOR_SIZE * sectors_per_track * number_of_headers * +* cylinders +*/ + s->number_of_heads = size / s->sectors_per_track / +SECTOR_SIZE / s->cylinders; +return ; + +case 32 : +/* TODO FAT32 adjust */ +if (size) { +warn_report ("size parameters not supported with FAT32;" +"default to 504MB." ); + } + s->cylinders =1024 ; + s->number_of_heads =16 ; + s->sectors_per_track =63 ; +return ; + } + } +} +static int vvfat_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {BDRVVVFATState *s = bs->opaque; +uint64_t size;bool floppy;const char *dirname, *label;QemuOpts *opts;
@@ -1178,6 +1314,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,"size" ,0 );memset (s->volume_label,' ' ,sizeof (s->volume_label)); label =qemu_opt_get (opts,"label" );
@@ -1215,35 +1352,15 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags, goto fail; } +vvfat_get_size_parameters (size, s, floppy, errp); -if (floppy) { -/* 2.88MB floppy */ -if (s->fat_type ==12 ) { - s->sectors_per_track =36 ; - s->sectors_per_cluster =2 ; - }else { - s->sectors_per_track =36 ; - s->sectors_per_cluster =1 ; - } - s->cylinder =80 ; - s->number_of_heads =2 ; - }else { -/* Reserver space for MBR */ -if (!qemu_opt_get_bool (opts,"no-mbr" , false)) { - s->offset_to_bootsector =0x3f ; - } -/* 32MB or 504MB disk*/ - s->cylinders = s->fat_type ==12 ?64 :1024 ; - s->number_of_heads =16 ; - s->sectors_per_track =63 ; +/* Reserver space for MBR */ +if (!floppy && !qemu_opt_get_bool (opts,"no-mbr" , false)) { + s->offset_to_bootsector =0x3f ; } - s->bs = bs; -/* LATER TODO: if FAT32, adjust */ - s->sectors_per_cluster=0x10 ; - s->current_cluster=0xffffffff ; s->qcow =NULL ;
Signature · 1 line
2.34.1
Re: [PATCH 5/5] vvfat: add support for "size" options
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 22 lines
This allows more flexibility to vvfat backend. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
Some limitations remains, the size parameter is recognized only when "format=vvfat" is passed. In particular, "format=raw,size=xxx" is keeping the previously hardcoded value: 504MB for FAT16 and 32 MB for FAT12. FAT32 has not been adjusted and thus still default to 504MB.
Moreover, for flopyy, size=1M is creating a disk 1.44 MB, and size=2M a disk of 2.88 MB. This avoids having to worry about float operations.
Signed-off-by Clément Chigot <[email protected]>--- block/vvfat.c | 165 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 141 insertions(+), 24 deletions(-)block/vvfat.c +0
-0@@ -1091,6 +1091,11 @@ static QemuOptsList runtime_opts = { .type = QEMU_OPT_BOOL,
block/vvfat.c static QemuOptsList runtime_opts = { · line 1101
Quoted 10 lines
block/vvfat.c +5
-0.help = "Do not add a Master Boot Record on this disk", }, + { + .name = BLOCK_OPT_SIZE, + .type = QEMU_OPT_SIZE, + .help = "Virtual disk size" + }, {/* end of list */ } }, };
Like in patch 1, you need additional changes, in particular to add the option to the QAPI schema in qapi/block-core.json.
Quoted 17 lines
block/vvfat.c +13
-0@@ -1148,10 +1153,141 @@ static void vvfat_parse_filename(const char *filename, QDict *options,qdict_put_bool (options,"no-mbr" , no_mbr); } +static void vvfat_get_size_parameters (uint64_t size,BDRVVVFATState *s, +bool floppy,Error **errp) +{ +if (floppy) { +/* +* Floppy emulation only supports 1.44 MB or 2.88 MB (default). +* In order to avoid floating operations ambiguity, 1 MB is +* recognized for 1.44 MB and 2 MB for 2.88 MB. +*/ +if (!size) { + size =2 *1024 *1024 ; + }else { +if (size ==1024 *1024 && s->fat_type ==16 ) {
block/vvfat.c static void vvfat_parse_filename(const char *filename, QDict *options, · line 1178
Quoted 10 lines
block/vvfat.c +10
-0+error_setg (errp, +"floppy FAT16 unsupported size; only support 2M " +"(for an effective size of 2.88 MB)" ); + }else if (size !=2 *1024 *1024 && size !=1024 *1024 ) { +error_setg (errp, +"floppy unsupported size; should be 1MB (for " +"an effective size of 1.44 MB) or 2.88M (for " +"2.88MB)" ); + } + }
This is horrible. To be fair, it's pretty hard to do something not horrible when the usual units to describe floppy sizes are already horrible. :-)
But I'd still like us to do better here.
To me it looks a bit like what we really want is an enum for floppy sizes (though is there any real reason why we have only those two?), but an arbitrary size for hard disks.
Without the enum, obviously, users could specify 1440k and that would do the right thing. Maybe special casing whatever 1.44M and 2.88M result in and translating them into 1440k and 2880k could be more justifiable than special casing 1M and 2M, but it would still be ugly.
Markus, do you have any advice how this should be represented in QAPI?
Kevin
Quoted 166 lines
block/vvfat.c +113
-24+ +if (s->fat_type ==12 ) { +if (size ==2 *1024 *1024 ) { + s->sectors_per_cluster =2 ; + }else { + s->sectors_per_cluster =1 ; + } + }else { + s->sectors_per_cluster =1 ; + } + + s->sectors_per_track =36 ; + s->cylinders =80 ; + s->number_of_heads =2 ; + }else { +/* LATER TODO: if FAT32, adjust */ + s->sectors_per_cluster =0x10 ; + +switch (s->fat_type) { +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 32Mb" ); + } + + s->cylinders =64 ; + +/* +* Based on CHS Recommandation table: +* Card Capacity | Number of Headers | Sectors per track +* ~ 2 MB | 4 | 16 +* ~ 4 MB | 8 | 16 +* ~ 8 MB | 16 | 16 +* ~ 16 MB | 2 | 32 +* ~ 32 MB | 4 | 32 +* +* For 2 MB, SD is recommending 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 ; + } + +/* +* The formula between the size (in bytes) and the parameters are: +* size = SECTOR_SIZE * sectors_per_track * number_of_headers * +* cylinders +*/ + s->number_of_heads = size / s->sectors_per_track / +SECTOR_SIZE / s->cylinders; +return ; + +case 16 : +/* Default is 504 MB */ +if (!size) { + size =504 *1024 *1024 ; + }else if (size /1024 >4 *1024 *1024 ) { +error_setg (errp,"FAT16 unsupported size; higher than 4Gb" ); + } + + s->cylinders =1024 ; + +/* +* Based on CHS Recommandation table: +* Card Capacity | Number of Headers | Sectors per track +* ~64 MB | 4 | 32 +* ~128 MB | 8 | 32 +* ~256 MB | 16 | 32 +* ~504 MB | 16 | 63 +* ~1008 MB | 32 | 63 +* ~2016 MB | 64 | 63 +*/ +if (size <=256 *1024 *1024 ) { + s->sectors_per_track =32 ; + }else { + s->sectors_per_track =63 ; + } + +/* +* The formula between the size (in bytes) and the parameters are: +* size = SECTOR_SIZE * sectors_per_track * number_of_headers * +* cylinders +*/ + s->number_of_heads = size / s->sectors_per_track / +SECTOR_SIZE / s->cylinders; +return ; + +case 32 : +/* TODO FAT32 adjust */ +if (size) { +warn_report ("size parameters not supported with FAT32;" +"default to 504MB." ); + } + s->cylinders =1024 ; + s->number_of_heads =16 ; + s->sectors_per_track =63 ; +return ; + } + } +} +static int vvfat_open (BlockDriverState *bs,QDict *options,int flags,Error **errp) {BDRVVVFATState *s = bs->opaque; +uint64_t size;bool floppy;const char *dirname, *label;QemuOpts *opts;@@ -1178,6 +1314,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,"size" ,0 );memset (s->volume_label,' ' ,sizeof (s->volume_label)); label =qemu_opt_get (opts,"label" );@@ -1215,35 +1352,15 @@ static int vvfat_open(BlockDriverState *bs, QDict *options, int flags, goto fail; } +vvfat_get_size_parameters (size, s, floppy, errp); -if (floppy) { -/* 2.88MB floppy */ -if (s->fat_type ==12 ) { - s->sectors_per_track =36 ; - s->sectors_per_cluster =2 ; - }else { - s->sectors_per_track =36 ; - s->sectors_per_cluster =1 ; - } - s->cylinder =80 ; - s->number_of_heads =2 ; - }else { -/* Reserver space for MBR */ -if (!qemu_opt_get_bool (opts,"no-mbr" , false)) { - s->offset_to_bootsector =0x3f ; - } -/* 32MB or 504MB disk*/ - s->cylinders = s->fat_type ==12 ?64 :1024 ; - s->number_of_heads =16 ; - s->sectors_per_track =63 ; +/* Reserver space for MBR */ +if (!floppy && !qemu_opt_get_bool (opts,"no-mbr" , false)) { + s->offset_to_bootsector =0x3f ; } - s->bs = bs; -/* LATER TODO: if FAT32, adjust */ - s->sectors_per_cluster=0x10 ; - s->current_cluster=0xffffffff ; s->qcow =NULL ;Signature · 1 line
2.34.1
Re: [PATCH 5/5] vvfat: add support for "size" options
Quoted 3 lines · Kevin Wolf
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 2 lines
This allows more flexibility to vvfat backend. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This is too terse to remind me of how vvfat picks cylinders, heads, and sectors before this patch, so I need to go dig through the source code. I figure it depends on configuration parameters @floppy and @fat-type like this:
floppy fat-type cyls heads secs cyls*heads*secs*512
false 12 64 16 63 31.5 MiB
false 16 1024 16 63 504 MiB
false 32 1024 16 63 504 MiB
true 12 80 2 18 1440 KiB
true 16 80 2 36 2880 KiB
true 32 80 2 36 2880 KiB
How exactly does the new parameter @size change this?
Quoted 4 lines
Quoted 4 lines
Some limitations remains, the size parameter is recognized only when "format=vvfat" is passed. In particular, "format=raw,size=xxx" is keeping the previously hardcoded value: 504MB for FAT16 and 32 MB for FAT12. FAT32 has not been adjusted and thus still default to 504MB.
31.5MiB unless I'm mistaken.
I'm not sure what you're trying to convey in this paragraph. As far as I can tell, you're adding a @size parameter to vvfat, so of course it doesn't affect raw.
Quoted 1 line
Quoted 1 line
Moreover, for flopyy, size=1M is creating a disk 1.44 MB, and size=2M a
floppy
Quoted 1 line
Quoted 1 line
disk of 2.88 MB. This avoids having to worry about float operations.
More on this part below.
Quoted 48 lines
Quoted 11 lines
Signed-off-by Clément Chigot <[email protected]>--- block/vvfat.c | 165 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 141 insertions(+), 24 deletions(-)block/vvfat.c +0
-0@@ -1091,6 +1091,11 @@ static QemuOptsList runtime_opts = { .type = QEMU_OPT_BOOL,block/vvfat.c static QemuOptsList runtime_opts = { · line 1101
Quoted 10 lines
block/vvfat.c +5
-0.help = "Do not add a Master Boot Record on this disk", }, + { + .name = BLOCK_OPT_SIZE, + .type = QEMU_OPT_SIZE, + .help = "Virtual disk size" + }, {/* end of list */ } }, };Like in patch 1, you need additional changes, in particular to add the option to the QAPI schema in
qapi/block-core.json.Quoted 23 lines
block/vvfat.c +19
-0@@ -1148,10 +1153,141 @@ static void vvfat_parse_filename(const char *filename, QDict *options,qdict_put_bool (options,"no-mbr" , no_mbr); } +static void vvfat_get_size_parameters (uint64_t size,BDRVVVFATState *s, +bool floppy,Error **errp) +{ +if (floppy) { +/* +* Floppy emulation only supports 1.44 MB or 2.88 MB (default). +* In order to avoid floating operations ambiguity, 1 MB is +* recognized for 1.44 MB and 2 MB for 2.88 MB. +*/ +if (!size) { + size =2 *1024 *1024 ; + }else { +if (size ==1024 *1024 && s->fat_type ==16 ) { +error_setg (errp, +"floppy FAT16 unsupported size; only support 2M " +"(for an effective size of 2.88 MB)" ); + }else if (size !=2 *1024 *1024 && size !=1024 *1024 ) { +error_setg (errp, + "floppy unsupported size; should be 1MB (for "
Quoted 8 lines
block/vvfat.c static void vvfat_parse_filename(const char *filename, QDict *options, · line 1178
Quoted 4 lines
block/vvfat.c +4
-0+"an effective size of 1.44 MB) or 2.88M (for " + "2.88MB)"); + } + }This is horrible. To be fair, it's pretty hard to do something not horrible when the usual units to describe floppy sizes are already horrible. :-)
Yes :)
Quoted 6 lines
But I'd still like us to do better here.
To me it looks a bit like what we really want is an enum for floppy sizes (though is there any real reason why we have only those two?), but an arbitrary size for hard disks.
Quoted 6 lines
Without the enum, obviously, users could specify 1440k and that would do the right thing. Maybe special casing whatever 1.44M and 2.88M result in and translating them into 1440k and 2880k could be more justifiable than special casing 1M and 2M, but it would still be ugly.
Markus, do you have any advice how this should be represented in QAPI?
Maybe, but first I'd like to understand what @size does.
Re: [PATCH 5/5] vvfat: add support for "size" options
Quoted 20 lines · Markus Armbruster
Quoted 3 lines · Kevin Wolf
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 2 lines
This allows more flexibility to vvfat backend. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This is too terse to remind me of how vvfat picks cylinders, heads, and sectors before this patch, so I need to go dig through the source code. I figure it depends on configuration parameters @floppy and @fat-type like this:
floppy fat-type cyls heads secs cyls*heads*secs*512
false 12 64 16 63 31.5 MiB
false 16 1024 16 63 504 MiB
false 32 1024 16 63 504 MiB
true 12 80 2 18 1440 KiB
true 16 80 2 36 2880 KiB
true 32 80 2 36 2880 KiB
Quoted 1 line
How exactly does the new parameter @size change this?
My prime goal was to create a 256 Mib VVFAT disk. As you can see, today for hard-disks there are only two possibilities: 31.5 Mib or 504 Mib. Hence, I've introduced the option size=xxx to allow more granular choices.
This option changes how cyls, heads and secs parameters are computed to be as closed as possible of its value.
I did try to keep it simple. I could have introduced options to select cylinders, heads, etc. But I think "size=xxx" would be more intuitive. There are also approximations made, as not all sizes can be reached. I didn't add errors or warnings for them. I'm fine adding them.
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Some limitations remains, the size parameter is recognized only when "format=vvfat" is passed. In particular, "format=raw,size=xxx" is keeping the previously hardcoded value: 504MB for FAT16 and 32 MB for FAT12. FAT32 has not been adjusted and thus still default to 504MB.
31.5MiB unless I'm mistaken.
True, I will fix it.
Quoted 3 lines
I'm not sure what you're trying to convey in this paragraph. As far as I can tell, you're adding a @size parameter to vvfat, so of course it doesn't affect raw.
Yes, but AFAICT, if=sd,format=raw will result in vvfat backend being called. I didn't manage to make the new option work with if=sd,format=raw,size=256Mb. Thus, when the "size" option is not provided, I keep the previous value (those for your above comment). Hence this paragraph to mostly warn people about the current limitation.
Quoted 65 lines
Quoted 1 line
Quoted 1 line
Moreover, for flopyy, size=1M is creating a disk 1.44 MB, and size=2M a
floppy
Quoted 1 line
Quoted 1 line
disk of 2.88 MB. This avoids having to worry about float operations.
More on this part below.
Quoted 48 lines
Quoted 11 lines
Signed-off-by Clément Chigot <[email protected]>--- block/vvfat.c | 165 ++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 141 insertions(+), 24 deletions(-)block/vvfat.c +0
-0@@ -1091,6 +1091,11 @@ static QemuOptsList runtime_opts = { .type = QEMU_OPT_BOOL,block/vvfat.c static QemuOptsList runtime_opts = { · line 1101
Quoted 10 lines
block/vvfat.c +5
-0.help = "Do not add a Master Boot Record on this disk", }, + { + .name = BLOCK_OPT_SIZE, + .type = QEMU_OPT_SIZE, + .help = "Virtual disk size" + }, {/* end of list */ } }, };Like in patch 1, you need additional changes, in particular to add the option to the QAPI schema in
qapi/block-core.json.Quoted 23 lines
block/vvfat.c +19
-0@@ -1148,10 +1153,141 @@ static void vvfat_parse_filename(const char *filename, QDict *options,qdict_put_bool (options,"no-mbr" , no_mbr); } +static void vvfat_get_size_parameters (uint64_t size,BDRVVVFATState *s, +bool floppy,Error **errp) +{ +if (floppy) { +/* +* Floppy emulation only supports 1.44 MB or 2.88 MB (default). +* In order to avoid floating operations ambiguity, 1 MB is +* recognized for 1.44 MB and 2 MB for 2.88 MB. +*/ +if (!size) { + size =2 *1024 *1024 ; + }else { +if (size ==1024 *1024 && s->fat_type ==16 ) { +error_setg (errp, +"floppy FAT16 unsupported size; only support 2M " +"(for an effective size of 2.88 MB)" ); + }else if (size !=2 *1024 *1024 && size !=1024 *1024 ) { +error_setg (errp, + "floppy unsupported size; should be 1MB (for "Quoted 8 lines
block/vvfat.c static void vvfat_parse_filename(const char *filename, QDict *options, · line 1178
Quoted 4 lines
block/vvfat.c +4
-0+"an effective size of 1.44 MB) or 2.88M (for " + "2.88MB)"); + } + }This is horrible. To be fair, it's pretty hard to do something not horrible when the usual units to describe floppy sizes are already horrible. :-)
Quoted 1 line
Yes :)
I did have a first version that ignored this new size option for floppy. I did extend it because why not. But if you find it will bring too much complexity I can bring it back.
Quoted 15 lines
Quoted 6 lines
But I'd still like us to do better here.
To me it looks a bit like what we really want is an enum for floppy sizes (though is there any real reason why we have only those two?), but an arbitrary size for hard disks.
Quoted 6 lines
Without the enum, obviously, users could specify 1440k and that would do the right thing. Maybe special casing whatever 1.44M and 2.88M result in and translating them into 1440k and 2880k could be more justifiable than special casing 1M and 2M, but it would still be ugly.
Markus, do you have any advice how this should be represented in QAPI?
Maybe, but first I'd like to understand what @size does.
Re: [PATCH 5/5] vvfat: add support for "size" options
Quoted 30 lines · Clément Chigot
Quoted 20 lines · Markus Armbruster
Quoted 3 lines · Kevin Wolf
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 2 lines
This allows more flexibility to vvfat backend. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This is too terse to remind me of how vvfat picks cylinders, heads, and sectors before this patch, so I need to go dig through the source code. I figure it depends on configuration parameters @floppy and @fat-type like this:
floppy fat-type cyls heads secs cyls*heads*secs*512
false 12 64 16 63 31.5 MiB
false 16 1024 16 63 504 MiB
false 32 1024 16 63 504 MiB
true 12 80 2 18 1440 KiB
true 16 80 2 36 2880 KiB
true 32 80 2 36 2880 KiBQuoted 1 line
How exactly does the new parameter @size change this?
My prime goal was to create a 256 Mib VVFAT disk. As you can see, today for hard-disks there are only two possibilities: 31.5 Mib or 504 Mib. Hence, I've introduced the option
size=xxxto allow more granular choices.
This option changes how cyls, heads and secs parameters are computed to be as closed as possible of its value.
Quoted 4 lines
I did try to keep it simple. I could have introduced options to select cylinders, heads, etc. But I think "size=xxx" would be more intuitive. There are also approximations made, as not all sizes can be reached. I didn't add errors or warnings for them. I'm fine adding them.
I don't have an opinion on whether we should support more sizes and/or provide full control over CHS geometry.
Quoted 13 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Some limitations remains, the size parameter is recognized only when "format=vvfat" is passed. In particular, "format=raw,size=xxx" is keeping the previously hardcoded value: 504MB for FAT16 and 32 MB for FAT12. FAT32 has not been adjusted and thus still default to 504MB.
31.5MiB unless I'm mistaken.
True, I will fix it.
Quoted 3 lines
I'm not sure what you're trying to convey in this paragraph. As far as I can tell, you're adding a @size parameter to vvfat, so of course it doesn't affect raw.
Quoted 6 lines
Yes, but AFAICT,
if=sd,format=rawwill result in vvfat backend being called. I didn't manage to make the new option work withif=sd,format=raw,size=256Mb. Thus, when the "size" option is not provided, I keep the previous value (those for your above comment). Hence this paragraph to mostly warn people about the current limitation.
Are you talking about -drive?
Complete examples, please.
I'm confused about the connection between SD (from if=sd here, and "SD specification" above) and vvfat. SD is a frontend. vvfat is a backend.
[...]
Re: [PATCH 5/5] vvfat: add support for "size" options
Quoted 61 lines · Markus Armbruster
Quoted 30 lines · Clément Chigot
Quoted 20 lines · Markus Armbruster
Quoted 3 lines · Kevin Wolf
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 2 lines
This allows more flexibility to vvfat backend. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This is too terse to remind me of how vvfat picks cylinders, heads, and sectors before this patch, so I need to go dig through the source code. I figure it depends on configuration parameters @floppy and @fat-type like this:
floppy fat-type cyls heads secs cyls*heads*secs*512
false 12 64 16 63 31.5 MiB
false 16 1024 16 63 504 MiB
false 32 1024 16 63 504 MiB
true 12 80 2 18 1440 KiB
true 16 80 2 36 2880 KiB
true 32 80 2 36 2880 KiBQuoted 1 line
How exactly does the new parameter @size change this?
My prime goal was to create a 256 Mib VVFAT disk. As you can see, today for hard-disks there are only two possibilities: 31.5 Mib or 504 Mib. Hence, I've introduced the option
size=xxxto allow more granular choices.
This option changes how cyls, heads and secs parameters are computed to be as closed as possible of its value.Quoted 4 lines
I did try to keep it simple. I could have introduced options to select cylinders, heads, etc. But I think "size=xxx" would be more intuitive. There are also approximations made, as not all sizes can be reached. I didn't add errors or warnings for them. I'm fine adding them.
I don't have an opinion on whether we should support more sizes and/or provide full control over CHS geometry.
Quoted 13 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Some limitations remains, the size parameter is recognized only when "format=vvfat" is passed. In particular, "format=raw,size=xxx" is keeping the previously hardcoded value: 504MB for FAT16 and 32 MB for FAT12. FAT32 has not been adjusted and thus still default to 504MB.
31.5MiB unless I'm mistaken.
True, I will fix it.
Quoted 3 lines
I'm not sure what you're trying to convey in this paragraph. As far as I can tell, you're adding a @size parameter to vvfat, so of course it doesn't affect raw.
Quoted 6 lines
Yes, but AFAICT,
if=sd,format=rawwill result in vvfat backend being called. I didn't manage to make the new option work withif=sd,format=raw,size=256Mb. Thus, when the "size" option is not provided, I keep the previous value (those for your above comment). Hence this paragraph to mostly warn people about the current limitation.
Quoted 6 lines
Are you talking about -drive?
Complete examples, please.
I'm confused about the connection between SD (from if=sd here, and "SD specification" above) and vvfat. SD is a frontend. vvfat is a backend.
Alright, I'll try to explain how I came up with this patch. And sorry if it's a bit blurry, I made it some months ago hence I don't remember all the details...
So, first, my prime goal was to access a local folder in a QNX system running on Raspi 4B emulation.
My usual way to pass such a local folder is through -drive file=fat:rw:<host_folder>,format=raw. For virt, it's usually connected to virtio-blk-device: -drive id=disk0,if=none,... -device virtio-blk-device,drive=disk0. For the Raspi 4b, adding the virtio-blk-device is not possible, hence I have to connect it as a SD
card: -drive if=sd,....
However, without any size= argument, QEMU will complain that the SD card has not a valid size:
| (host) $ qemu-system-aarch64 -M raspi4b -kernel raspi4b-kernel
-nographic -no-reboot -append "earlycon=pl011,mmio32,0xfe201000 console=ttyAMA0 noreboot" -dtb bcm2711-rpi-4-b.dtb -initrd rootfs.cpio.gz -drive id=sdcard,file=fat:rw:<host_folder>,format=raw,if=sd
| qemu-system-aarch64: Invalid SD card size: 504 MiB | SD card size has to be a power of 2, e.g. 512 MiB. | You can resize disk images with 'qemu-img resize <imagefile> <new-size>' | (note that this will lose data if you make the image smaller than
it currently is).
("raspi4b-kernel", the dtb and the rootfs come from functional/aarch64/test_raspi4.py)
Hence, I've added size=256M to reduce the size of that SD card and make QEMU happy. This allows me to mount my host folder on Linux:
| (host) $ qemu-system-aarch64 -M raspi4b -kernel raspi4b-kernel
-nographic -no-reboot -append "earlycon=pl011,mmio32,0xfe201000 console=ttyAMA0 noreboot" -dtb bcm2711-rpi-4-b.dtb -initrd rootfs.cpio.gz -drive id=sdcard,file=fat:rw:<host_folder>,format=raw,if=sd,size=256M
| (QEMU) # fdisk -l /dev/mmcblk1 | Disk /dev/mmcblk1: 256 MB, 268435456 bytes, 524288 sectors | 520 cylinders, 16 heads, 63 sectors/track | Units: sectors of 1 * 512 = 512 bytes | | Device Boot StartCHS EndCHS StartLBA EndLBA
Sectors Size Id Type
| /dev/mmcblk1p1 * 0,1,1 1023,15,63 63 1032191
1032129 503M 6 FAT16
As you can see the "Disk" has the right size (256MB) but the partition still has a 503M size. However, Linux doesn't seem to care too much about that as I'm able to mount this partition, and perform IO operations.
| (QEMU) # mount /dev/mmcblk1p1 /mnt | (QEMU) # ls /mnt | file.txt | (QEMU) # cat /mnt/file.txt | Hello World | (QEMU) # echo "OK" > /mnt/test.txt | (host) $ cat <host_folder>/test.txt | OK
Now, QNX comes into play.
First, the SD card must be connected to another bus. That patch is not part of this series as I'm considering it a QNX issue. Just FTR here is the patch:
| --- a/hw/arm/bcm2838_peripherals.c | +++ b/hw/arm/bcm2838_peripherals.c | @@ -190,7 +190,7 @@ static void bcm2838_peripherals_realize(DeviceState *dev, Error **errp) | &s_base->peri_mr, GPIO_OFFSET, | sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gpio), 0)); | | - object_property_add_alias(OBJECT(s), "sd-bus",
OBJECT(&s->gpio), "sd-bus");
| + object_property_add_alias(OBJECT(s), "sd-bus",OBJECT(&s->emmc2), "sd-bus");
|
| /* BCM2838 RPiVid ASB must be mapped to prevent kernel crash */
Afterwards, QNX is able to see the SD card, but not able to mount it. It complains about the filesystem being corrupted. Looking at fdisk output, it shows a mismatch between the "total section" value and the Cylinders/Heads/etc values.
| (QEMU) # fdisk /dev/hd0 info | Physical disk characteristics: (/dev/hd0) | Disk type : Direct Access (0) | Cylinders : 520 | Heads : 16 | Sectors/Track : 63 | Total Sectors : 524288 | Block Size : 512 | | Warning: total sectors field does not agree with | cylinders*sectors/track*heads!! (524288 vs 524160)
The "no-mbr" option introduced in patch 1 is something we (Adacore's QEMU team) have for a long time. I don't remember the details but we are using it for other OSes as well (notably RTEMS).
Once added, and the drive command line updated for -drive id=sdcard,file=fat:rw:no-mbr:<host_folder>,format=raw,if=sd,size=256M, I don't have this warning anymore. Though, I'm still getting the corrupted filesystem error.
Afterwards, it's a bit blurry but I think by trial and errors we ended up removing the SD size error and realize that -drive id=sdcard,file=fat:rw:no-mbr:<host_folder>,format=raw,if=sd was working. However, size=256M still results in a corrupted filesystem. As a comment in vvfat.c states that it either creates a "32MB or 504 MB disk". I decided to check if I can adapt, hence this patch.
I didn't find any VFAT documentation explaining the relation between the size and the cylinders, heads, sector per track values. However, the SD documentation was giving some recommandations, hence I used it as a base.
I was unable to make vvfat.c recognize the "size" argument passed along format=raw, even if hardcoding the value in vvfat.c did make a difference. And that's why I'm adding the warning in the commit message.
I've also realized that following my patch, the mismatch in between the disk and the partition in Linux was going away when using -drive format=vvfat,size=xxx. Making it not just QNX-oriented.
| (host) $ qemu-system-aarch64 -M raspi4b -kernel raspi4b-kernel
-nographic -no-reboot -append "earlycon=pl011,mmio32,0xfe201000 console=ttyAMA0 noreboot" -dtb bcm2711-rpi-4-b.dtb -initrd rootfs.cpio.gz -drive id=sdcard,file=fat:rw:<host_folder>,format=vvfat,if=sd,size=256M
| (QEMU) # fdisk -l /dev/mmcblk1 | Disk /dev/mmcblk1: 256 MB, 268435456 bytes, 524288 sectors | 1024 cylinders, 16 heads, 32 sectors/track | Units: sectors of 1 * 512 = 512 bytes | | Device Boot StartCHS EndCHS StartLBA EndLBA
Sectors Size Id Type
| /dev/mmcblk1p1 * 0,1,32 1023,15,32 63 524287
524225 255M 6 FAT16
I probably missed a few things, but I hope this is clearer to you why and how this series was made.
Re: [PATCH 5/5] vvfat: add support for "size" options
Quoted 88 lines · Clément Chigot
Quoted 61 lines · Markus Armbruster
Quoted 30 lines · Clément Chigot
Quoted 20 lines · Markus Armbruster
Quoted 3 lines · Kevin Wolf
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 2 lines
This allows more flexibility to vvfat backend. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This is too terse to remind me of how vvfat picks cylinders, heads, and sectors before this patch, so I need to go dig through the source code. I figure it depends on configuration parameters @floppy and @fat-type like this:
floppy fat-type cyls heads secs cyls*heads*secs*512
false 12 64 16 63 31.5 MiB
false 16 1024 16 63 504 MiB
false 32 1024 16 63 504 MiB
true 12 80 2 18 1440 KiB
true 16 80 2 36 2880 KiB
true 32 80 2 36 2880 KiBQuoted 1 line
How exactly does the new parameter @size change this?
My prime goal was to create a 256 Mib VVFAT disk. As you can see, today for hard-disks there are only two possibilities: 31.5 Mib or 504 Mib. Hence, I've introduced the option
size=xxxto allow more granular choices.
This option changes how cyls, heads and secs parameters are computed to be as closed as possible of its value.Quoted 4 lines
I did try to keep it simple. I could have introduced options to select cylinders, heads, etc. But I think "size=xxx" would be more intuitive. There are also approximations made, as not all sizes can be reached. I didn't add errors or warnings for them. I'm fine adding them.
I don't have an opinion on whether we should support more sizes and/or provide full control over CHS geometry.
Quoted 13 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Some limitations remains, the size parameter is recognized only when "format=vvfat" is passed. In particular, "format=raw,size=xxx" is keeping the previously hardcoded value: 504MB for FAT16 and 32 MB for FAT12. FAT32 has not been adjusted and thus still default to 504MB.
31.5MiB unless I'm mistaken.
True, I will fix it.
Quoted 3 lines
I'm not sure what you're trying to convey in this paragraph. As far as I can tell, you're adding a @size parameter to vvfat, so of course it doesn't affect raw.
Quoted 6 lines
Yes, but AFAICT,
if=sd,format=rawwill result in vvfat backend being called. I didn't manage to make the new option work withif=sd,format=raw,size=256Mb. Thus, when the "size" option is not provided, I keep the previous value (those for your above comment). Hence this paragraph to mostly warn people about the current limitation.Quoted 6 lines
Are you talking about -drive?
Complete examples, please.
I'm confused about the connection between SD (from if=sd here, and "SD specification" above) and vvfat. SD is a frontend. vvfat is a backend.
Alright, I'll try to explain how I came up with this patch. And sorry if it's a bit blurry, I made it some months ago hence I don't remember all the details...
So, first, my prime goal was to access a local folder in a QNX system running on Raspi 4B emulation.
My usual way to pass such a local folder is through-drive file=fat:rw:<host_folder>,format=raw. For virt, it's usually connected to virtio-blk-device:-drive id=disk0,if=none,... -device virtio-blk-device,drive=disk0. For the Raspi 4b, adding the virtio-blk-device is not possible, hence I have to connect it as a SD
card:-drive if=sd,....However, without any
size=argument, QEMU will complain that the SD card has not a valid size:
| (host) $ qemu-system-aarch64 -M raspi4b -kernel raspi4b-kernel
-nographic -no-reboot -append "earlycon=pl011,mmio32,0xfe201000 console=ttyAMA0 noreboot" -dtb bcm2711-rpi-4-b.dtb -initrd rootfs.cpio.gz -drive id=sdcard,file=fat:rw:<host_folder>,format=raw,if=sd
Quoted 5 lines
| qemu-system-aarch64: Invalid SD card size: 504 MiB | SD card size has to be a power of 2, e.g. 512 MiB. | You can resize disk images with 'qemu-img resize <imagefile> <new-size>' | (note that this will lose data if you make the image smaller thanit currently is).
Fun!
Quoted 10 lines
("raspi4b-kernel", the dtb and the rootfs come from
functional/aarch64/test_raspi4.py)Hence, I've added
size=256Mto reduce the size of that SD card and make QEMU happy. This allows me to mount my host folder on Linux:
| (host) $ qemu-system-aarch64 -M raspi4b -kernel raspi4b-kernel
-nographic -no-reboot -append "earlycon=pl011,mmio32,0xfe201000 console=ttyAMA0 noreboot" -dtb bcm2711-rpi-4-b.dtb -initrd rootfs.cpio.gz -drive id=sdcard,file=fat:rw:<host_folder>,format=raw,if=sd,size=256M
Quoted 4 lines
| (QEMU) # fdisk -l /dev/mmcblk1 | Disk /dev/mmcblk1: 256 MB, 268435456 bytes, 524288 sectors | 520 cylinders, 16 heads, 63 sectors/track | Units: sectors of 1 * 512 = 512 bytes
520 * 16 * 63 is 524160 sectors, 128 less than the 524288 reported. I figure that's harmless. Only ancient software should look at CHS, and losing a few sectors with ancient software is fine.
Quoted 8 lines
|
| Device Boot StartCHS EndCHS StartLBA EndLBA
Sectors Size Id Type
|/dev/mmcblk1p1* 0,1,1 1023,15,63 63 1032191
1032129 503M 6 FAT16As you can see the "Disk" has the right size (256MB) but the partition still has a 503M size.
The partition table's EndLBA is 1032191 even though the disk has only 524288 cylinders. Scary!
Its EndCHS is consistent with its EndLBA: 1024*16*63 = 1032191 + 1.
Quoted 11 lines
However, Linux doesn't seem to care too much
about that as I'm able to mount this partition, and perform IO operations.| (QEMU) # mount /dev/mmcblk1p1 /mnt | (QEMU) # ls /mnt | file.txt | (QEMU) # cat /mnt/file.txt | Hello World | (QEMU) # echo "OK" > /mnt/test.txt | (host) $ cat <host_folder>/test.txt | OK
Have you tried with a host folder containing more than 256MiB? What happens if you try to read all of it?
Quoted 22 lines
Now, QNX comes into play.
First, the SD card must be connected to another bus. That patch is not part of this series as I'm considering it a QNX issue. Just FTR here is the patch:| --- a/hw/arm/bcm2838_peripherals.c | +++ b/hw/arm/bcm2838_peripherals.c | @@ -190,7 +190,7 @@ static void bcm2838_peripherals_realize(DeviceState *dev, Error **errp) | &s_base->peri_mr, GPIO_OFFSET, | sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gpio), 0)); | | - object_property_add_alias(OBJECT(s), "sd-bus",
OBJECT(&s->gpio), "sd-bus");
| + object_property_add_alias(OBJECT(s), "sd-bus",OBJECT(&s->emmc2), "sd-bus");
|
| /* BCM2838 RPiVid ASB must be mapped to prevent kernel crash */Afterwards, QNX is able to see the SD card, but not able to mount it. It complains about the filesystem being corrupted. Looking at
fdiskoutput, it shows a mismatch between the "total section" value and the Cylinders/Heads/etc values.
Quoted 11 lines
| (QEMU) # fdisk /dev/hd0 info | Physical disk characteristics: (/dev/hd0) | Disk type : Direct Access (0) | Cylinders : 520 | Heads : 16 | Sectors/Track : 63 | Total Sectors : 524288 | Block Size : 512 | | Warning: total sectors field does not agree with | cylinders*sectors/track*heads!! (524288 vs 524160)
I'm not sure this mismatch causes the problem. I suspect the bogus EndLBA does.
Quoted 3 lines
The "no-mbr" option introduced in patch 1 is something we (Adacore's QEMU team) have for a long time. I don't remember the details but we are using it for other OSes as well (notably RTEMS).
Suppressing MBR initialization avoids the partially bogus partition table.
But if we create a partition table, it better make sense, don't you think?
Quoted 17 lines
Once added, and the drive command line updated for
-drive id=sdcard,file=fat:rw:no-mbr:<host_folder>,format=raw,if=sd,size=256M, I don't have this warning anymore. Though, I'm still getting the corrupted filesystem error.Afterwards, it's a bit blurry but I think by trial and errors we ended up removing the SD size error and realize that
-drive id=sdcard,file=fat:rw:no-mbr:<host_folder>,format=raw,if=sdwas working. However,size=256Mstill results in a corrupted filesystem. As a comment invvfat.cstates that it either creates a "32MB or 504 MB disk". I decided to check if I can adapt, hence this patch.I didn't find any VFAT documentation explaining the relation between the size and the cylinders, heads, sector per track values. However, the SD documentation was giving some recommandations, hence I used it as a base.
Quoted 4 lines
I was unable to make
vvfat.crecognize the "size" argument passed alongformat=raw, even if hardcoding the value invvfat.cdid make a difference. And that's why I'm adding the warning in the commit message.
This one:
Some limitations remains, the size parameter is recognized only when "format=vvfat" is passed. In particular, "format=raw,size=xxx" is keeping the previously hardcoded value: 504MB for FAT16 and 32 MB for FAT12. FAT32 has not been adjusted and thus still default to 504MB.
Quoted 14 lines
I've also realized that following my patch, the mismatch in between the disk and the partition in Linux was going away when using
-drive format=vvfat,size=xxx. Making it not just QNX-oriented.
| (host) $ qemu-system-aarch64 -M raspi4b -kernel raspi4b-kernel
-nographic -no-reboot -append "earlycon=pl011,mmio32,0xfe201000 console=ttyAMA0 noreboot" -dtb bcm2711-rpi-4-b.dtb -initrd rootfs.cpio.gz -drive id=sdcard,file=fat:rw:<host_folder>,format=vvfat,if=sd,size=256M| (QEMU) # fdisk -l /dev/mmcblk1 | Disk /dev/mmcblk1: 256 MB, 268435456 bytes, 524288 sectors | 1024 cylinders, 16 heads, 32 sectors/track | Units: sectors of 1 * 512 = 512 bytes | | Device Boot StartCHS EndCHS StartLBA EndLBA
Quoted 3 lines
Sectors Size Id Type
|/dev/mmcblk1p1* 0,1,32 1023,15,32 63 524287
524225 255M 6 FAT16
EndLBA matches disk size. EndCHS still matches EndLBA. Good.
The difference to the command line you showed above is format=raw (bad) vs. format=vvfat (good).
With format=raw,size=256M you get that size, but a bogus partition table.
With format=vvfat,size=256M you get that size, and a sensible partition table.
Correct?
Quoted 2 lines
I probably missed a few things, but I hope this is clearer to you why and how this series was made.
Re: [PATCH 5/5] vvfat: add support for "size" options
Quoted 144 lines · Markus Armbruster
Quoted 88 lines · Clément Chigot
Quoted 61 lines · Markus Armbruster
Quoted 30 lines · Clément Chigot
Quoted 20 lines · Markus Armbruster
Quoted 3 lines · Kevin Wolf
Am 03.09.2025 um 09:57 hat Clément Chigot geschrieben:
Quoted 2 lines
This allows more flexibility to vvfat backend. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This is too terse to remind me of how vvfat picks cylinders, heads, and sectors before this patch, so I need to go dig through the source code. I figure it depends on configuration parameters @floppy and @fat-type like this:
floppy fat-type cyls heads secs cyls*heads*secs*512
false 12 64 16 63 31.5 MiB
false 16 1024 16 63 504 MiB
false 32 1024 16 63 504 MiB
true 12 80 2 18 1440 KiB
true 16 80 2 36 2880 KiB
true 32 80 2 36 2880 KiBQuoted 1 line
How exactly does the new parameter @size change this?
My prime goal was to create a 256 Mib VVFAT disk. As you can see, today for hard-disks there are only two possibilities: 31.5 Mib or 504 Mib. Hence, I've introduced the option
size=xxxto allow more granular choices.
This option changes how cyls, heads and secs parameters are computed to be as closed as possible of its value.Quoted 4 lines
I did try to keep it simple. I could have introduced options to select cylinders, heads, etc. But I think "size=xxx" would be more intuitive. There are also approximations made, as not all sizes can be reached. I didn't add errors or warnings for them. I'm fine adding them.
I don't have an opinion on whether we should support more sizes and/or provide full control over CHS geometry.
Quoted 13 lines
Quoted 6 lines
Quoted 4 lines
Quoted 4 lines
Some limitations remains, the size parameter is recognized only when "format=vvfat" is passed. In particular, "format=raw,size=xxx" is keeping the previously hardcoded value: 504MB for FAT16 and 32 MB for FAT12. FAT32 has not been adjusted and thus still default to 504MB.
31.5MiB unless I'm mistaken.
True, I will fix it.
Quoted 3 lines
I'm not sure what you're trying to convey in this paragraph. As far as I can tell, you're adding a @size parameter to vvfat, so of course it doesn't affect raw.
Quoted 6 lines
Yes, but AFAICT,
if=sd,format=rawwill result in vvfat backend being called. I didn't manage to make the new option work withif=sd,format=raw,size=256Mb. Thus, when the "size" option is not provided, I keep the previous value (those for your above comment). Hence this paragraph to mostly warn people about the current limitation.Quoted 6 lines
Are you talking about -drive?
Complete examples, please.
I'm confused about the connection between SD (from if=sd here, and "SD specification" above) and vvfat. SD is a frontend. vvfat is a backend.
Alright, I'll try to explain how I came up with this patch. And sorry if it's a bit blurry, I made it some months ago hence I don't remember all the details...
So, first, my prime goal was to access a local folder in a QNX system running on Raspi 4B emulation.
My usual way to pass such a local folder is through-drive file=fat:rw:<host_folder>,format=raw. For virt, it's usually connected to virtio-blk-device:-drive id=disk0,if=none,... -device virtio-blk-device,drive=disk0. For the Raspi 4b, adding the virtio-blk-device is not possible, hence I have to connect it as a SD
card:-drive if=sd,....However, without any
size=argument, QEMU will complain that the SD card has not a valid size:
| (host) $ qemu-system-aarch64 -M raspi4b -kernel raspi4b-kernel
-nographic -no-reboot -append "earlycon=pl011,mmio32,0xfe201000 console=ttyAMA0 noreboot" -dtb bcm2711-rpi-4-b.dtb -initrd rootfs.cpio.gz -drive id=sdcard,file=fat:rw:<host_folder>,format=raw,if=sdQuoted 5 lines
| qemu-system-aarch64: Invalid SD card size: 504 MiB | SD card size has to be a power of 2, e.g. 512 MiB. | You can resize disk images with 'qemu-img resize <imagefile> <new-size>' | (note that this will lose data if you make the image smaller thanit currently is).
Fun!
Quoted 10 lines
("raspi4b-kernel", the dtb and the rootfs come from
functional/aarch64/test_raspi4.py)Hence, I've added
size=256Mto reduce the size of that SD card and make QEMU happy. This allows me to mount my host folder on Linux:
| (host) $ qemu-system-aarch64 -M raspi4b -kernel raspi4b-kernel
-nographic -no-reboot -append "earlycon=pl011,mmio32,0xfe201000 console=ttyAMA0 noreboot" -dtb bcm2711-rpi-4-b.dtb -initrd rootfs.cpio.gz -drive id=sdcard,file=fat:rw:<host_folder>,format=raw,if=sd,size=256MQuoted 4 lines
| (QEMU) # fdisk -l /dev/mmcblk1 | Disk /dev/mmcblk1: 256 MB, 268435456 bytes, 524288 sectors | 520 cylinders, 16 heads, 63 sectors/track | Units: sectors of 1 * 512 = 512 bytes520 * 16 * 63 is 524160 sectors, 128 less than the 524288 reported. I figure that's harmless. Only ancient software should look at CHS, and losing a few sectors with ancient software is fine.
Quoted 8 lines
|
| Device Boot StartCHS EndCHS StartLBA EndLBA
Sectors Size Id Type
|/dev/mmcblk1p1* 0,1,1 1023,15,63 63 1032191
1032129 503M 6 FAT16As you can see the "Disk" has the right size (256MB) but the partition still has a 503M size.
The partition table's EndLBA is 1032191 even though the disk has only 524288 cylinders. Scary!
Its EndCHS is consistent with its EndLBA: 1024*16*63 = 1032191 + 1.
Quoted 11 lines
However, Linux doesn't seem to care too much
about that as I'm able to mount this partition, and perform IO operations.| (QEMU) # mount /dev/mmcblk1p1 /mnt | (QEMU) # ls /mnt | file.txt | (QEMU) # cat /mnt/file.txt | Hello World | (QEMU) # echo "OK" > /mnt/test.txt | (host) $ cat <host_folder>/test.txt | OK
Quoted 2 lines
Have you tried with a host folder containing more than 256MiB? What happens if you try to read all of it?
The access is crashing:
| (host) $ du -sh vvfat-test/huge_file | 251M vvfat-test/huge_file | (host) $ qemu-system ... -drive file=fat:rw:vvfat-test,format=raw,if=sd,size=128M | (QEMU) # tail /mnt/huge_file | [ 29.325885] attempt to access beyond end of device | [ 29.325885] mmcblk1p1: rw=524288, want=510657, limit=262081 | [ 29.337672] attempt to access beyond end of device | [ 29.337672] mmcblk1p1: rw=0, want=510657, limit=262081 | tail: read error: I/O error
Quoted 41 lines
Quoted 22 lines
Now, QNX comes into play.
First, the SD card must be connected to another bus. That patch is not part of this series as I'm considering it a QNX issue. Just FTR here is the patch:| --- a/hw/arm/bcm2838_peripherals.c | +++ b/hw/arm/bcm2838_peripherals.c | @@ -190,7 +190,7 @@ static void bcm2838_peripherals_realize(DeviceState *dev, Error **errp) | &s_base->peri_mr, GPIO_OFFSET, | sysbus_mmio_get_region(SYS_BUS_DEVICE(&s->gpio), 0)); | | - object_property_add_alias(OBJECT(s), "sd-bus",
OBJECT(&s->gpio), "sd-bus");
| + object_property_add_alias(OBJECT(s), "sd-bus",OBJECT(&s->emmc2), "sd-bus");
|
| /* BCM2838 RPiVid ASB must be mapped to prevent kernel crash */Afterwards, QNX is able to see the SD card, but not able to mount it. It complains about the filesystem being corrupted. Looking at
fdiskoutput, it shows a mismatch between the "total section" value and the Cylinders/Heads/etc values.Quoted 11 lines
| (QEMU) # fdisk /dev/hd0 info | Physical disk characteristics: (/dev/hd0) | Disk type : Direct Access (0) | Cylinders : 520 | Heads : 16 | Sectors/Track : 63 | Total Sectors : 524288 | Block Size : 512 | | Warning: total sectors field does not agree with | cylinders*sectors/track*heads!! (524288 vs 524160)I'm not sure this mismatch causes the problem. I suspect the bogus EndLBA does.
Quoted 3 lines
The "no-mbr" option introduced in patch 1 is something we (Adacore's QEMU team) have for a long time. I don't remember the details but we are using it for other OSes as well (notably RTEMS).
Quoted 5 lines
Suppressing MBR initialization avoids the partially bogus partition table.
But if we create a partition table, it better make sense, don't you think?
My understanding is that format=vvfat,size=xxxM results in a valid EndLBA. Is that enough to make a valid partition table ? However, I'm still getting the corrupted error without no-mbr. Though, the warning is gone too (compared to format=raw,size=xxx). So this is something else QNX doesn't like...
Something weird if just noticed, the disk CHS values are changed when passing no-mbr
| (host) $ qemu-system-aarch64 -drive
file=fat:rw:<host_folder>:format=vvfat,size=128M
| (QEMU) # fdisk -l /dev/mmcblk1 | Disk /dev/mmcblk1: 128 MB, 134217728 bytes, 262144 sectors | 1024 cylinders, 8 heads, 32 sectors/track | | (host) $ qemu-system-aarch64 -drive file=fat:rw:no-mbr:<host_folder>:format=vvfat,size=128M | (QEMU) # fdisk -l /dev/mmcblk1 | Disk /dev/mmcblk1: 128 MB, 134217728 bytes, 262144 sectors | 4096 cylinders, 4 heads, 16 sectors/track
Not sure if it could be related.
Quoted 52 lines
Quoted 17 lines
Once added, and the drive command line updated for
-drive id=sdcard,file=fat:rw:no-mbr:<host_folder>,format=raw,if=sd,size=256M, I don't have this warning anymore. Though, I'm still getting the corrupted filesystem error.Afterwards, it's a bit blurry but I think by trial and errors we ended up removing the SD size error and realize that
-drive id=sdcard,file=fat:rw:no-mbr:<host_folder>,format=raw,if=sdwas working. However,size=256Mstill results in a corrupted filesystem. As a comment invvfat.cstates that it either creates a "32MB or 504 MB disk". I decided to check if I can adapt, hence this patch.I didn't find any VFAT documentation explaining the relation between the size and the cylinders, heads, sector per track values. However, the SD documentation was giving some recommandations, hence I used it as a base.
Quoted 4 lines
I was unable to make
vvfat.crecognize the "size" argument passed alongformat=raw, even if hardcoding the value invvfat.cdid make a difference. And that's why I'm adding the warning in the commit message.This one:
Some limitations remains, the size parameter is recognized only when "format=vvfat" is passed. In particular, "format=raw,size=xxx" is keeping the previously hardcoded value: 504MB for FAT16 and 32 MB for FAT12. FAT32 has not been adjusted and thus still default to 504MB.
Quoted 14 lines
I've also realized that following my patch, the mismatch in between the disk and the partition in Linux was going away when using
-drive format=vvfat,size=xxx. Making it not just QNX-oriented.
| (host) $ qemu-system-aarch64 -M raspi4b -kernel raspi4b-kernel
-nographic -no-reboot -append "earlycon=pl011,mmio32,0xfe201000 console=ttyAMA0 noreboot" -dtb bcm2711-rpi-4-b.dtb -initrd rootfs.cpio.gz -drive id=sdcard,file=fat:rw:<host_folder>,format=vvfat,if=sd,size=256M| (QEMU) # fdisk -l /dev/mmcblk1 | Disk /dev/mmcblk1: 256 MB, 268435456 bytes, 524288 sectors | 1024 cylinders, 16 heads, 32 sectors/track | Units: sectors of 1 * 512 = 512 bytes | | Device Boot StartCHS EndCHS StartLBA EndLBAQuoted 3 lines
Sectors Size Id Type
|/dev/mmcblk1p1* 0,1,32 1023,15,32 63 524287
524225 255M 6 FAT16EndLBA matches disk size. EndCHS still matches EndLBA. Good.
The difference to the command line you showed above is format=raw (bad) vs. format=vvfat (good).
Quoted 7 lines
With format=raw,size=256M you get that size, but a bogus partition table.
With format=vvfat,size=256M you get that size, and a sensible partition table.
Correct?
Yes and the main reason for that is because I don't know how to retrieve the size given to "raw" within vvfat_open.
My understanding is that raw-format.c is suppressing that option, through qemu_opts_del in raw_read_options, before calling its block childs (here vvfat). Hence, it assumes the default size 512M.
Quoted 3 lines
Quoted 2 lines
I probably missed a few things, but I hope this is clearer to you why and how this series was made.
Re: [PATCH 5/5] vvfat: add support for "size" options
Am 31.10.2025 um 10:47 hat Clément Chigot geschrieben:
Quoted 11 lines
Quoted 11 lines
Quoted 4 lines
I was unable to make
vvfat.crecognize the "size" argument passed alongformat=raw, even if hardcoding the value invvfat.cdid make a difference. And that's why I'm adding the warning in the commit message.This one:
Some limitations remains, the size parameter is recognized only when "format=vvfat" is passed. In particular, "format=raw,size=xxx" is keeping the previously hardcoded value: 504MB for FAT16 and 32 MB for FAT12. FAT32 has not been adjusted and thus still default to 504MB.
I actually wonder if we should give the vvfat option a different name to make it less error prone. Forgetting format=vvfat is easy and then raw will be autodetected and size will be interpreted as an option for raw.
Quoted 29 lines
Quoted 29 lines
Quoted 14 lines
I've also realized that following my patch, the mismatch in between the disk and the partition in Linux was going away when using
-drive format=vvfat,size=xxx. Making it not just QNX-oriented.
| (host) $ qemu-system-aarch64 -M raspi4b -kernel raspi4b-kernel
-nographic -no-reboot -append "earlycon=pl011,mmio32,0xfe201000 console=ttyAMA0 noreboot" -dtb bcm2711-rpi-4-b.dtb -initrd rootfs.cpio.gz -drive id=sdcard,file=fat:rw:<host_folder>,format=vvfat,if=sd,size=256M| (QEMU) # fdisk -l /dev/mmcblk1 | Disk /dev/mmcblk1: 256 MB, 268435456 bytes, 524288 sectors | 1024 cylinders, 16 heads, 32 sectors/track | Units: sectors of 1 * 512 = 512 bytes | | Device Boot StartCHS EndCHS StartLBA EndLBAQuoted 3 lines
Sectors Size Id Type
|/dev/mmcblk1p1* 0,1,32 1023,15,32 63 524287
524225 255M 6 FAT16EndLBA matches disk size. EndCHS still matches EndLBA. Good.
The difference to the command line you showed above is format=raw (bad) vs. format=vvfat (good).
With format=raw,size=256M you get that size, but a bogus partition table.
With format=vvfat,size=256M you get that size, and a sensible partition table.
Quoted 7 lines
Quoted 1 line
Correct?
Yes and the main reason for that is because I don't know how to retrieve the size given to "raw" within
vvfat_open.
My understanding is thatraw-format.cis suppressing that option, throughqemu_opts_delinraw_read_options, before calling its block childs (here vvfat). Hence, it assumes the default size 512M.
The order is the other way around. You always get the protocol level openen first and only then the image format level.
Imagine the simple case of a qcow2 image file used for the VM. You get things stacked like this:
virtio-blk
|
v
qcow2
|
v
fileYou need to open them from bottom to top. Opening a qcow2 image must be able to read from the file, so first the file layer must be opened. And obvious a virtio-blk device can only use the image after the qcow2 layered has been opened.
In your case, this is raw over vvfat. vvfat gets opened first, and then raw gets instantiated on top of it. (If you use format=vvfat, then the raw layer is left away.)
Top level options you give to -drive go to the topmost block driver. You should be able to still set it on the vvfat level with -drive format=raw,file.size=... Deciding which option goes to which node is part of the (rather complicated) bdrv_open() logic in block.c.
What raw does when a size option is given is that it just truncates the lower level to the given size. So as vvfat doesn't know the size, it still creates a 504 MB image, but raw shows only the first part of it to the guest. This results not only in an invalid partition table, but also means that as soon as vvfat decides to put a cluster after your limited size, you'll see filesystem corruption in the guest.
So your approach to deal with this in vvfat and create a smaller filesystem to start with does look right to me.
Kevin
Re: [PATCH 5/5] vvfat: add support for "size" options
Quoted 9 lines · Kevin Wolf
Am 31.10.2025 um 10:47 hat Clément Chigot geschrieben:
Quoted 7 lines
Quoted 7 lines
Quoted 4 lines
I was unable to make
vvfat.crecognize the "size" argument passed alongformat=raw, even if hardcoding the value invvfat.cdid make a difference. And that's why I'm adding the warning in the commit message.This one:
Quoted 8 lines
Quoted 4 lines
Quoted 4 lines
Some limitations remains, the size parameter is recognized only when "format=vvfat" is passed. In particular, "format=raw,size=xxx" is keeping the previously hardcoded value: 504MB for FAT16 and 32 MB for FAT12. FAT32 has not been adjusted and thus still default to 504MB.
I actually wonder if we should give the vvfat option a different name to make it less error prone. Forgetting format=vvfat is easy and then raw will be autodetected and size will be interpreted as an option for raw.
We can clearly name it "fat-size".
Quoted 72 lines
Quoted 29 lines
Quoted 29 lines
Quoted 14 lines
I've also realized that following my patch, the mismatch in between the disk and the partition in Linux was going away when using
-drive format=vvfat,size=xxx. Making it not just QNX-oriented.
| (host) $ qemu-system-aarch64 -M raspi4b -kernel raspi4b-kernel
-nographic -no-reboot -append "earlycon=pl011,mmio32,0xfe201000 console=ttyAMA0 noreboot" -dtb bcm2711-rpi-4-b.dtb -initrd rootfs.cpio.gz -drive id=sdcard,file=fat:rw:<host_folder>,format=vvfat,if=sd,size=256M| (QEMU) # fdisk -l /dev/mmcblk1 | Disk /dev/mmcblk1: 256 MB, 268435456 bytes, 524288 sectors | 1024 cylinders, 16 heads, 32 sectors/track | Units: sectors of 1 * 512 = 512 bytes | | Device Boot StartCHS EndCHS StartLBA EndLBAQuoted 3 lines
Sectors Size Id Type
|/dev/mmcblk1p1* 0,1,32 1023,15,32 63 524287
524225 255M 6 FAT16EndLBA matches disk size. EndCHS still matches EndLBA. Good.
The difference to the command line you showed above is format=raw (bad) vs. format=vvfat (good).
With format=raw,size=256M you get that size, but a bogus partition table.
With format=vvfat,size=256M you get that size, and a sensible partition table.
Quoted 7 lines
Quoted 1 line
Correct?
Yes and the main reason for that is because I don't know how to retrieve the size given to "raw" within
vvfat_open.
My understanding is thatraw-format.cis suppressing that option, throughqemu_opts_delinraw_read_options, before calling its block childs (here vvfat). Hence, it assumes the default size 512M.The order is the other way around. You always get the protocol level openen first and only then the image format level.
Imagine the simple case of a qcow2 image file used for the VM. You get things stacked like this:
virtio-blk | v qcow2 | v fileYou need to open them from bottom to top. Opening a qcow2 image must be able to read from the file, so first the file layer must be opened. And obvious a virtio-blk device can only use the image after the qcow2 layered has been opened.
In your case, this is raw over vvfat. vvfat gets opened first, and then raw gets instantiated on top of it. (If you use format=vvfat, then the raw layer is left away.)
Top level options you give to -drive go to the topmost block driver. You should be able to still set it on the vvfat level with -drive format=raw,file.size=... Deciding which option goes to which node is part of the (rather complicated)
bdrv_open()logic inblock.c.What raw does when a size option is given is that it just truncates the lower level to the given size. So as vvfat doesn't know the size, it still creates a 504 MB image, but raw shows only the first part of it to the guest. This results not only in an invalid partition table, but also means that as soon as vvfat decides to put a cluster after your limited size, you'll see filesystem corruption in the guest.
Quoted 2 lines
So your approach to deal with this in vvfat and create a smaller filesystem to start with does look right to me.
Ok thanks for the explanation. It's a bit counter-intuitive that "size" does not propagate to lower levels, especially if it generates wrong ones behind the scene. But IIUC, this would be a much more complex patch (i.e. changing bdrv_open logic).
Hence, I'm fine keeping this series narrowed to "format=vvfat".
Quoted 2 lines
Kevin
Re: [PATCH 5/5] vvfat: add support for "size" options
Quoted 1 line · Clément Chigot
On Fri, Oct 31, 2025 at 12:57 PM Kevin Wolf <[email protected]> wrote:
[...]
Quoted 11 lines
Quoted 11 lines
Imagine the simple case of a qcow2 image file used for the VM. You get things stacked like this:
virtio-blk | v qcow2 | v file
Quoted 8 lines
Quoted 8 lines
You need to open them from bottom to top. Opening a qcow2 image must be able to read from the file, so first the file layer must be opened. And obvious a virtio-blk device can only use the image after the qcow2 layered has been opened.
In your case, this is raw over vvfat. vvfat gets opened first, and then raw gets instantiated on top of it. (If you use format=vvfat, then the raw layer is left away.)
Desirable, because it's simpler.
Quoted 12 lines
Quoted 12 lines
Top level options you give to -drive go to the topmost block driver. You should be able to still set it on the vvfat level with -drive format=raw,file.size=... Deciding which option goes to which node is part of the (rather complicated)
bdrv_open()logic inblock.c.What raw does when a size option is given is that it just truncates the lower level to the given size. So as vvfat doesn't know the size, it still creates a 504 MB image, but raw shows only the first part of it to the guest. This results not only in an invalid partition table, but also means that as soon as vvfat decides to put a cluster after your limited size, you'll see filesystem corruption in the guest.
Quoted 6 lines
Quoted 2 lines
So your approach to deal with this in vvfat and create a smaller filesystem to start with does look right to me.
Ok thanks for the explanation. It's a bit counter-intuitive that "size" does not propagate to lower levels, especially if it generates wrong ones behind the scene.
Format "raw" was designed to do nothing, so we have a "do nothing" format for the rigid "format over protocol" system.
We've long acquired the means to use a protocol without a format. This made "raw" redundant. I advocate omitting it, because it only complicates matters.
Except when you want the one feature "raw" provides beyond "do nothing":
carve a slice with options offset and size (commit 2fdc70452a5 "raw_bsd:
add offset and size options", 2016).
Quoted 2 lines
But IIUC, this would be a much more
complex patch (i.e. changingbdrv_openlogic).
"size" is for slicing. Can't see how it could simultaneously be forwarded to the next block driver.
Quoted 5 lines
Hence, I'm fine keeping this series narrowed to "format=vvfat".
Quoted 2 lines
Kevin
Re: [PATCH 5/5] vvfat: add support for "size" options
In reply to Kevin WolfKevin Wolf <[email protected]> writes:
[...]
Quoted 10 lines
To me it looks a bit like what we really want is an enum for floppy sizes (though is there any real reason why we have only those two?), but an arbitrary size for hard disks.
Without the enum, obviously, users could specify 1440k and that would do the right thing. Maybe special casing whatever 1.44M and 2.88M result in and translating them into 1440k and 2880k could be more justifiable than special casing 1M and 2M, but it would still be ugly.
Markus, do you have any advice how this should be represented in QAPI?
Still want answers here?
[...]
Re: [PATCH 5/5] vvfat: add support for "size" options
Am 05.11.2025 um 08:08 hat Markus Armbruster geschrieben:
Quoted 8 lines
Kevin Wolf <[email protected]> writes:
[...]
Quoted 4 lines
To me it looks a bit like what we really want is an enum for floppy sizes (though is there any real reason why we have only those two?), but an arbitrary size for hard disks.
Quoted 8 lines
Quoted 6 lines
Without the enum, obviously, users could specify 1440k and that would do the right thing. Maybe special casing whatever 1.44M and 2.88M result in and translating them into 1440k and 2880k could be more justifiable than special casing 1M and 2M, but it would still be ugly.
Markus, do you have any advice how this should be represented in QAPI?
Still want answers here?
Yes, I'm still not sure how we could best represent both hard disk and floppy sizes in vvfat in a way that isn't completely counterintuitive for users, that also isn't just arbitrary magic and that works on the command line.
Unless the need for different sizes has gone away, but I don't think we found any other solution for the problem that would not require a configurable disk/file system size?
Kevin
Re: [PATCH 5/5] vvfat: add support for "size" options
Quoted 15 lines · Kevin Wolf
Am 05.11.2025 um 08:08 hat Markus Armbruster geschrieben:
Quoted 13 lines
Kevin Wolf <[email protected]> writes:
[...]
Quoted 9 lines
To me it looks a bit like what we really want is an enum for floppy sizes (though is there any real reason why we have only those two?), but an arbitrary size for hard disks.
Without the enum, obviously, users could specify 1440k and that would do the right thing. Maybe special casing whatever 1.44M and 2.88M result in and translating them into 1440k and 2880k could be more justifiable than special casing 1M and 2M, but it would still be ugly.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
Markus, do you have any advice how this should be represented in QAPI?
Still want answers here?
Yes, I'm still not sure how we could best represent both hard disk and floppy sizes in vvfat in a way that isn't completely counterintuitive for users, that also isn't just arbitrary magic and that works on the command line.
For v2 (probably pushed sometimes this week), I've changed the command-line approach to allow only fat-size=1440K and fat-size=2880K. Other values will be rejected (including 1.44M or 2.88M).
I'm not familiar with QAPI to see if that approach would fit properly.
Quoted 6 lines
Unless the need for different sizes has gone away, but I don't think we found any other solution for the problem that would not require a configurable disk/file system size?
Kevin
Re: [PATCH 5/5] vvfat: add support for "size" options
In reply to Kevin WolfQuoted 18 lines · Kevin Wolf
Am 05.11.2025 um 08:08 hat Markus Armbruster geschrieben:
Quoted 8 lines
Kevin Wolf <[email protected]> writes:
[...]
Quoted 4 lines
To me it looks a bit like what we really want is an enum for floppy sizes (though is there any real reason why we have only those two?), but an arbitrary size for hard disks.
Quoted 8 lines
Quoted 6 lines
Without the enum, obviously, users could specify 1440k and that would do the right thing. Maybe special casing whatever 1.44M and 2.88M result in and translating them into 1440k and 2880k could be more justifiable than special casing 1M and 2M, but it would still be ugly.
Markus, do you have any advice how this should be represented in QAPI?
Still want answers here?
Quoted 8 lines
Yes, I'm still not sure how we could best represent both hard disk and floppy sizes in vvfat in a way that isn't completely counterintuitive for users, that also isn't just arbitrary magic and that works on the command line.
Unless the need for different sizes has gone away, but I don't think we found any other solution for the problem that would not require a configurable disk/file system size?
Let me recap the problem. Please correct my misunderstandings, if any.
Hard disks can have almost arbitrary sizes. Almost, because it still needs to be a multiple of the block size.
Floppy disks have one of a small set of well-known sizes.
I vaguely recall that we generally derive the device's actual size from the backend's size.
Some devices reject certain sizes. For instance, SD cards require a power of 2.
Most devices seem to accept anything. I can create an IDE, SCSI, or floppy disk backed by a raw image of one byte. I have no idea how it would behave.
As is, the vvfat backend can only do certain sizes, configurable with parameters @floppy and @fat-type. They work for floppies, but not for SD cards, since they're not powers of two.
Instead of deriving size and CHS from @floppy and @fat-type, Clément proposes to specify the size (and derive fat-type and CHS[*]?).
In QMP, we specify the size in bytes. This is fine regardless of size; management applications don't mind sending things like "size": 1474560.
In HMP and the command line, big byte sizes are inconvenient. That's why we support suffixes there. size=256M is a fine way to pick an SD card's size.
The size suffixes seem inconvenient for floppies, though. For instance, 2 heads * 80 tracks * 18 sectors * 512 bytes = 1474560 bytes = 1440 KiBytes, but size=1.44M does not work: 1.44 MiBytes = 1509949.44 Bytes. However, size=1440K does.
This leads me to suggest to simply stick to numeric size, and use appropriate suffixes. These are obvious enough for anything but floppies. So advise users "use K for floppies"[**].
If this isn't good enough, I can help you explore fancier parts of QAPI, such as alternate types.
[*] I guess we could support specifying an optional fat-type in addition to size, and derive only CHS then.
[**] Even for a hypothetical floppy with an odd number of 512 byte
sectors: .5K works, because .5 * 1024 is an integer.
Quoted 12 lines · Clément Chigot
The main goal of this series is to introduce a new option "size" within the vvfat backend (patch 5). It allows more control over SD cards' size. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This series also includes minor patches:
- patch 1 introduces another option to remove the Master Boot Record (this is mandatory for QNX)
- patch 2-4 are minor improvements easing the introducing of "size" option
Quoted 4 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).
Gentle ping for this series.
TIA
Clément
Quoted 13 lines
Clément Chigot (5):
vvfat: introduce no-mbr option
vvfat: movefat_typecheck prior to size setup
vvfat: add a define forSECTOR_SIZE
vvfat: move size parameters within driver structure
vvfat: add support for "size" optionsblock/vvfat.c | 279 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 213 insertions(+), 66 deletions(-)--
2.34.1
Quoted 12 lines · Clément Chigot
The main goal of this series is to introduce a new option "size" within the vvfat backend (patch 5). It allows more control over SD cards' size. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This series also includes minor patches:
- patch 1 introduces another option to remove the Master Boot Record (this is mandatory for QNX)
- patch 2-4 are minor improvements easing the introducing of "size" option
Quoted 4 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).
Gentle ping for this series.
TIA
Clément
Quoted 13 lines
Clément Chigot (5):
vvfat: introduce no-mbr option
vvfat: movefat_typecheck prior to size setup
vvfat: add a define forSECTOR_SIZE
vvfat: move size parameters within driver structure
vvfat: add support for "size" optionsblock/vvfat.c | 279 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 213 insertions(+), 66 deletions(-)--
2.34.1
Quoted 23 lines · Clément Chigot
The main goal of this series is to introduce a new option "size" within the vvfat backend (patch 5). It allows more control over SD cards' size. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This series also includes minor patches:
- patch 1 introduces another option to remove the Master Boot Record (this is mandatory for QNX)
- patch 2-4 are minor improvements easing the introducing of "size" optionThis 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).
Clément Chigot (5):
vvfat: introduce no-mbr option
vvfat: movefat_typecheck prior to size setup
vvfat: add a define forSECTOR_SIZE
vvfat: move size parameters within driver structure
vvfat: add support for "size" options
Quoted 2 lines
block/vvfat.c | 279 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 213 insertions(+), 66 deletions(-)
(Thanks Markus for bringing this thread up)
I just wanted to say that a long time ago I wrote an nbdkit plugin that was intended as a more sane replacement for vfat. Since then several more nbdkit plugins have been added. They support arbitrary sizes already. The first one is the most direct replacement for vvfat (although it doesn't support writes to the backing directory, because that feature is insane).
https://libguestfs.org/nbdkit-floppy-plugin.1.html
https://libguestfs.org/nbdkit-iso-plugin.1.html
https://libguestfs.org/nbdkit-linuxdisk-plugin.1.html
https://libguestfs.org/nbdkit-tmpdisk-plugin.1.html
Very easy to use from qemu as documented here:
https://libguestfs.org/nbdkit-client.1.html#ATTACHING-NBD-DEVICES-TO-A-VIRTUAL-MACHINE
Rich.
Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
Quoted 28 lines · Richard W.M. Jones
Quoted 23 lines · Clément Chigot
The main goal of this series is to introduce a new option "size" within the vvfat backend (patch 5). It allows more control over SD cards' size. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This series also includes minor patches:
- patch 1 introduces another option to remove the Master Boot Record (this is mandatory for QNX)
- patch 2-4 are minor improvements easing the introducing of "size" optionThis 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).
Clément Chigot (5):
vvfat: introduce no-mbr option
vvfat: movefat_typecheck prior to size setup
vvfat: add a define forSECTOR_SIZE
vvfat: move size parameters within driver structure
vvfat: add support for "size" optionsQuoted 2 lines
block/vvfat.c | 279 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 213 insertions(+), 66 deletions(-)
Quoted 8 lines
(Thanks Markus for bringing this thread up)
I just wanted to say that a long time ago I wrote an nbdkit plugin that was intended as a more sane replacement for vfat. Since then several more nbdkit plugins have been added. They support arbitrary sizes already. The first one is the most direct replacement for vvfat (although it doesn't support writes to the backing directory, because that feature is insane).
Thanks for bringing that solution. I'll take a look though I'm not sure it'll fit our needs. That insane feature is required for us... We need ways to retrieve files generated by the binaries run within QEMU. Moreover, we'll probably soon replace all that setup by some SSH commands once we have implemented a few missing network devices (at least the raspi4b one). But as I think those patches make vvfat block a bit better, I still find them worth merging.
Nonetheless, I appreciate your feedback and will keep it in mind the next time we have to deal with emulations without network devices.
Thanks,
Quoted 17 lines
https://libguestfs.org/nbdkit-floppy-plugin.1.html
https://libguestfs.org/nbdkit-iso-plugin.1.html
https://libguestfs.org/nbdkit-linuxdisk-plugin.1.html
https://libguestfs.org/nbdkit-tmpdisk-plugin.1.htmlVery easy to use from qemu as documented here:
https://libguestfs.org/nbdkit-client.1.html#ATTACHING-NBD-DEVICES-TO-A-VIRTUAL-MACHINE
Rich.
--
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
libguestfs lets you edit virtual machines. Supports shell scripting, bindings from many languages. http://libguestfs.org
Am 12.11.2025 um 13:38 hat Richard W.M. Jones geschrieben:
Quoted 24 lines · Clément Chigot
Quoted 23 lines
The main goal of this series is to introduce a new option "size" within the vvfat backend (patch 5). It allows more control over SD cards' size. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This series also includes minor patches:
- patch 1 introduces another option to remove the Master Boot Record (this is mandatory for QNX)
- patch 2-4 are minor improvements easing the introducing of "size" optionThis 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).
Clément Chigot (5):
vvfat: introduce no-mbr option
vvfat: movefat_typecheck prior to size setup
vvfat: add a define forSECTOR_SIZE
vvfat: move size parameters within driver structure
vvfat: add support for "size" options
Quoted 7 lines
Quoted 2 lines
block/vvfat.c | 279 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 213 insertions(+), 66 deletions(-)(Thanks Markus for bringing this thread up)
I just wanted to say that a long time ago I wrote an nbdkit plugin that was intended as a more sane replacement for vfat.
What does it do differently from vvfat? For the read-only part, I thought that vvfat wasn't too bad. Write support is where all the bugs are hiding. But if you have good input, maybe vvfat could be improved.
Quoted 4 lines
Since then several more nbdkit plugins have been added. They support arbitrary sizes already. The first one is the most direct replacement for vvfat (although it doesn't support writes to the backing directory, because that feature is insane).
The scenario we discussed here was explicitly read-write.
Kevin
Quoted 28 lines · Kevin Wolf
Am 12.11.2025 um 13:38 hat Richard W.M. Jones geschrieben:
Quoted 27 lines · Clément Chigot
Quoted 25 lines
The main goal of this series is to introduce a new option "size" within the vvfat backend (patch 5). It allows more control over SD cards' size. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This series also includes minor patches:
- patch 1 introduces another option to remove the Master Boot Record (this is mandatory for QNX)
- patch 2-4 are minor improvements easing the introducing of "size" optionThis 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).
Clément Chigot (5):
vvfat: introduce no-mbr option
vvfat: movefat_typecheck prior to size setup
vvfat: add a define forSECTOR_SIZE
vvfat: move size parameters within driver structure
vvfat: add support for "size" optionsblock/vvfat.c | 279 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 213 insertions(+), 66 deletions(-)
Quoted 8 lines
Quoted 4 lines
(Thanks Markus for bringing this thread up)
I just wanted to say that a long time ago I wrote an nbdkit plugin that was intended as a more sane replacement for vfat.
What does it do differently from vvfat? For the read-only part, I thought that vvfat wasn't too bad. Write support is where all the bugs are hiding. But if you have good input, maybe vvfat could be improved.
There are a bunch of reasons why you'd want the less common drivers to be running outside qemu, and not be a part of qemu at all:
- It's more secure, not just because it's running in a separate process, but because you can confine the process differently (eg with SELinux).
- The command line is far simpler and more intuitive compared to, say, q-s-d.
- There's a simple nbd:// URI scheme for these endpoints, and a whole suite of tools for querying, copying and modifying these NBD endpoints, like nbdinfo, nbdcopy, nbddump, nbdsh, (and more). This is all based on public standards and interoperability tests, with clients in several programming languages.
- You can consume NBD elsewhere than just from qemu, including over the network.
- Huge array of weird and ugly plugins that wouldn't be desirable or even possible in qemu (because they are proprietary like VDDK, or written in Python or other scripting languages like our AWS support). Also a much wider choice of programming languages - vvfat might be more tractable if written in a scripting language.
- A number of out of tree plugins that companies develop, which we enable because we have hard ABI guarantees, but qemu does not (and I understand why qemu does not do this, and those are good reasons).
Quoted 6 lines
Quoted 4 lines
Since then several more nbdkit plugins have been added. They support arbitrary sizes already. The first one is the most direct replacement for vvfat (although it doesn't support writes to the backing directory, because that feature is insane).
The scenario we discussed here was explicitly read-write.
Indeed, nbdkit-floppy-plugin doesn't support writes to the backing filesystem right now. Some others I listed in the original email do support writes, and you can add a COW layer on top of any of them (which is not the same thing of course).
Rich.
Signature · 4 lines
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-builder quickly builds VMs from scratch http://libguestfs.org/virt-builder.1.html
Am 13.11.2025 um 10:13 hat Richard W.M. Jones geschrieben:
Quoted 63 lines · Kevin Wolf
Quoted 28 lines
Am 12.11.2025 um 13:38 hat Richard W.M. Jones geschrieben:
Quoted 27 lines · Clément Chigot
Quoted 25 lines
The main goal of this series is to introduce a new option "size" within the vvfat backend (patch 5). It allows more control over SD cards' size. The value for "Number of Heads" and "Sectors per track" are based on SD specifications Part 2.
This series also includes minor patches:
- patch 1 introduces another option to remove the Master Boot Record (this is mandatory for QNX)
- patch 2-4 are minor improvements easing the introducing of "size" optionThis 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).
Clément Chigot (5):
vvfat: introduce no-mbr option
vvfat: movefat_typecheck prior to size setup
vvfat: add a define forSECTOR_SIZE
vvfat: move size parameters within driver structure
vvfat: add support for "size" optionsblock/vvfat.c | 279 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 213 insertions(+), 66 deletions(-)Quoted 8 lines
Quoted 4 lines
(Thanks Markus for bringing this thread up)
I just wanted to say that a long time ago I wrote an nbdkit plugin that was intended as a more sane replacement for vfat.
What does it do differently from vvfat? For the read-only part, I thought that vvfat wasn't too bad. Write support is where all the bugs are hiding. But if you have good input, maybe vvfat could be improved.
There are a bunch of reasons why you'd want the less common drivers to be running outside qemu, and not be a part of qemu at all:
- It's more secure, not just because it's running in a separate process, but because you can confine the process differently (eg with SELinux).
- The command line is far simpler and more intuitive compared to, say, q-s-d.
- There's a simple nbd:// URI scheme for these endpoints, and a whole suite of tools for querying, copying and modifying these NBD endpoints, like nbdinfo, nbdcopy, nbddump, nbdsh, (and more). This is all based on public standards and interoperability tests, with clients in several programming languages.
- You can consume NBD elsewhere than just from qemu, including over the network.
- Huge array of weird and ugly plugins that wouldn't be desirable or even possible in qemu (because they are proprietary like VDDK, or written in Python or other scripting languages like our AWS support). Also a much wider choice of programming languages - vvfat might be more tractable if written in a scripting language.
Quoted 3 lines
- A number of out of tree plugins that companies develop, which we enable because we have hard ABI guarantees, but qemu does not (and I understand why qemu does not do this, and those are good reasons).
Ok, so nothing in particular that the nbdkit floppy plugin does better than vvfat that could be worth looking at?
I can kind of get the argument that running things in separate processes is always safer, though in the typical vvfat use case your VM is trusted, so it doesn't really matter much. So I wouldn't argue using QSD for this case either, even though it would provide the same isolation. For human use, the simplicity of use for built-in vvfat is hard to beat.
(Ignoring all the other points that are just generic nbdkit advertising with no relevance for the vvfat use case.)
Quoted 11 lines
Quoted 6 lines
Quoted 4 lines
Since then several more nbdkit plugins have been added. They support arbitrary sizes already. The first one is the most direct replacement for vvfat (although it doesn't support writes to the backing directory, because that feature is insane).
The scenario we discussed here was explicitly read-write.
Indeed, nbdkit-floppy-plugin doesn't support writes to the backing filesystem right now. Some others I listed in the original email do support writes, and you can add a COW layer on top of any of them (which is not the same thing of course).
From that list, it seems to be only tmpdisk, which obviously isn't well suited to be used to copy files between host and guest?
Kevin