mkfs.vfat: fix creation of 2.5 kbyte FAT12 image

function                                             old     new   delta
mkfs_vfat_main                                      2463    2454      -9
.rodata                                           105036  105007     -29
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-38)             Total: -38 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko committed 2026-11-16 13:56:51 +01:00
1 parent 8d79a6335c
commit d724d7d292
2 files changed
+83 -27

No files matched your search

+56 -9
View File
@@ -8,13 +8,15 @@
# testing "test name" "options" "expected result" "file input" "stdin"
optional BLKID
# This creates 32MB FAT32 image, and tests that blkid does recognize it.
# This is technically incorrect: at this size, image should be FAT16
# according to Microsoft rules (it says that FAT12/16/32-ness of the volume
# is _solely_ determined by cluster count).
# But Linux kernel happily mounts these small FAT32 images.
testing "mkfs.vfat 32MB image" \
"dd if=/dev/zero of=input bs=512 count=65536 2>/dev/null; mkfs.vfat input 2>&1 && blkid input | sed 's/\"....-....\"/\"xxxx-xxxx\"/' " \
"dd if=/dev/zero of=input bs=512 count=65536 2>/dev/null
mkfs.vfat input 2>&1 && blkid input | sed 's/\"....-....\"/\"xxxx-xxxx\"/' " \
'input: UUID="xxxx-xxxx" TYPE="vfat"
' '' ''
@@ -24,12 +26,52 @@ testing "mkfs.vfat 18KB image" \
'input: UUID="xxxx-xxxx" TYPE="vfat"
' '' ''
# The minimal image which can still be mounted and can have non-zero-length files
testing "mkfs.vfat -b0 -R2 3KB image" \
"mkfs.vfat -b0 -R2 input 3 2>&1 && blkid input | sed 's/\"....-....\"/\"xxxx-xxxx\"/' " \
# The minimal FAT32 image which can still be mounted and can have non-zero-length files
# FAT32: boot sector, fsinfo sector, FAT#1, FAT#2, data sector#1 (contains rootdir), data sector#2
testing "mkfs.vfat -F32 -b0 -R2 3KB image" \
"mkfs.vfat -F32 -b0 -R2 input 3 2>&1 && blkid input | sed 's/\"....-....\"/\"xxxx-xxxx\"/' " \
'input: UUID="xxxx-xxxx" TYPE="vfat"
' '' ''
# The minimal FAT12 image which can still be mounted and can have non-zero-length files
# FAT12: boot sector, FAT#1, FAT#2, rootdir, data sector#1
testing "mkfs.vfat -b0 -R1 2.5KB image" \
"dd if=/dev/zero of=input bs=512 count=5 2>/dev/null
mkfs.vfat -b0 -R1 -n FSLABEL input 2>&1 && hexdump -C input \
| sed 's/....FSLAB.\$/xxxxFSLAB|/;s/.. .. .. .. 46 53 4c 41 42 /xx xx xx xx 46 53 4c 41 42 /'" \
'00000000 eb 58 90 6d 6b 64 6f 73 66 73 00 00 02 01 01 00 |.X.mkdosfs......|
00000010 02 10 00 05 00 f8 01 00 3f 00 ff 00 00 00 00 00 |........?.......|
00000020 05 00 00 00 00 00 29 xx xx xx xx 46 53 4c 41 42 |......)xxxxFSLAB|
00000030 45 4c 20 20 20 20 46 41 54 31 32 20 20 20 00 00 |EL FAT12 ..|
00000040 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000050 00 00 00 00 00 00 00 00 00 00 0e 1f be 77 7c ac |.............w|.|
00000060 22 c0 74 0b 56 b4 0e bb 07 00 cd 10 5e eb f0 32 |".t.V.......^..2|
00000070 e4 cd 16 cd 19 eb fe 54 68 69 73 20 69 73 20 6e |.......This is n|
00000080 6f 74 20 61 20 62 6f 6f 74 61 62 6c 65 20 64 69 |ot a bootable di|
00000090 73 6b 0d 0a 00 00 00 00 00 00 00 00 00 00 00 00 |sk..............|
000000a0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
000001f0 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 aa |..............U.|
00000200 f8 ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000210 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00000400 f8 ff ff 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
00000410 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00000600 46 53 4c 41 42 45 4c 20 20 20 20 08 00 00 00 00 |FSLABEL .....|
00000610 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 |................|
*
00000a00
' '' ''
# The minimal FAT12 one-FAT image which can still be mounted and can have non-zero-length files
# FAT12: boot sector, FAT#1, rootdir, data sector#1
#TODO# needs -f 1|2 support
#TODO#testing "mkfs.vfat -b0 -R1 -f1 2KB image" \
#TODO# "mkfs.vfat -b0 -R1 -f1 input 2 2>&1 && blkid input | sed 's/\"....-....\"/\"xxxx-xxxx\"/' " \
#TODO#'input: UUID="xxxx-xxxx" TYPE="vfat"
#TODO#' '' ''
# Create maximal possible one-sector-per-cluster FAT32:
# FATs should have precisely 2097152 sectors: this holds enough entries for 2097152*512/4 = 0x10000000 clusters.
# Actual number of clusters should be 268435445 = 0x0ffffff5 (the maximum cluster# is 0x0ffffff6).
@@ -44,7 +86,8 @@ input: UUID="xxxx-xxxx" TYPE="vfat"
' '' ''
testing "mkfs.vfat 360" \
"mkfs.vfat -n FSLABEL input 360 2>&1 && hexdump -C input | sed 's/....FSLAB.\$/xxxxFSLAB|/;s/.. .. .. .. 46 53 4c 41 42 /xx xx xx xx 46 53 4c 41 42 /'" \
"mkfs.vfat -n FSLABEL input 360 2>&1 && hexdump -C input \
| sed 's/....FSLAB.\$/xxxxFSLAB|/;s/.. .. .. .. 46 53 4c 41 42 /xx xx xx xx 46 53 4c 41 42 /'" \
'00000000 eb 58 90 6d 6b 64 6f 73 66 73 00 00 02 02 01 00 |.X.mkdosfs......|
00000010 02 70 00 d0 02 fd 02 00 09 00 02 00 00 00 00 00 |.p..............|
00000020 d0 02 00 00 00 00 29 xx xx xx xx 46 53 4c 41 42 |......)xxxxFSLAB|
@@ -71,7 +114,8 @@ testing "mkfs.vfat 360" \
' '' ''
testing "mkfs.vfat 720" \
"mkfs.vfat -n FSLABEL input 720 2>&1 && hexdump -C input | sed 's/....FSLAB.\$/xxxxFSLAB|/;s/.. .. .. .. 46 53 4c 41 42 /xx xx xx xx 46 53 4c 41 42 /'" \
"mkfs.vfat -n FSLABEL input 720 2>&1 && hexdump -C input \
| sed 's/....FSLAB.\$/xxxxFSLAB|/;s/.. .. .. .. 46 53 4c 41 42 /xx xx xx xx 46 53 4c 41 42 /'" \
'00000000 eb 58 90 6d 6b 64 6f 73 66 73 00 00 02 02 01 00 |.X.mkdosfs......|
00000010 02 70 00 a0 05 f9 03 00 09 00 02 00 00 00 00 00 |.p..............|
00000020 a0 05 00 00 00 00 29 xx xx xx xx 46 53 4c 41 42 |......)xxxxFSLAB|
@@ -98,7 +142,8 @@ testing "mkfs.vfat 720" \
' '' ''
testing "mkfs.vfat 1200" \
"mkfs.vfat -n FSLABEL input 1200 2>&1 && hexdump -C input | sed 's/....FSLAB.\$/xxxxFSLAB|/;s/.. .. .. .. 46 53 4c 41 42 /xx xx xx xx 46 53 4c 41 42 /'" \
"mkfs.vfat -n FSLABEL input 1200 2>&1 && hexdump -C input \
| sed 's/....FSLAB.\$/xxxxFSLAB|/;s/.. .. .. .. 46 53 4c 41 42 /xx xx xx xx 46 53 4c 41 42 /'" \
'00000000 eb 58 90 6d 6b 64 6f 73 66 73 00 00 02 01 01 00 |.X.mkdosfs......|
00000010 02 e0 00 60 09 f9 07 00 0f 00 02 00 00 00 00 00 |...`............|
00000020 60 09 00 00 00 00 29 xx xx xx xx 46 53 4c 41 42 |`.....)xxxxFSLAB|
@@ -125,7 +170,8 @@ testing "mkfs.vfat 1200" \
' '' ''
testing "mkfs.vfat 1440" \
"mkfs.vfat -n FSLABEL input 1440 2>&1 && hexdump -C input | sed 's/....FSLAB.\$/xxxxFSLAB|/;s/.. .. .. .. 46 53 4c 41 42 /xx xx xx xx 46 53 4c 41 42 /'" \
"mkfs.vfat -n FSLABEL input 1440 2>&1 && hexdump -C input \
| sed 's/....FSLAB.\$/xxxxFSLAB|/;s/.. .. .. .. 46 53 4c 41 42 /xx xx xx xx 46 53 4c 41 42 /'" \
'00000000 eb 58 90 6d 6b 64 6f 73 66 73 00 00 02 01 01 00 |.X.mkdosfs......|
00000010 02 e0 00 40 0b f0 09 00 12 00 02 00 00 00 00 00 |...@............|
00000020 40 0b 00 00 00 00 29 xx xx xx xx 46 53 4c 41 42 |@.....)xxxxFSLAB|
@@ -152,7 +198,8 @@ testing "mkfs.vfat 1440" \
' '' ''
testing "mkfs.vfat 2880" \
"mkfs.vfat -n FSLABEL input 2880 2>&1 && hexdump -C input | sed 's/....FSLAB.\$/xxxxFSLAB|/;s/.. .. .. .. 46 53 4c 41 42 /xx xx xx xx 46 53 4c 41 42 /'" \
"mkfs.vfat -n FSLABEL input 2880 2>&1 && hexdump -C input \
| sed 's/....FSLAB.\$/xxxxFSLAB|/;s/.. .. .. .. 46 53 4c 41 42 /xx xx xx xx 46 53 4c 41 42 /'" \
'00000000 eb 58 90 6d 6b 64 6f 73 66 73 00 00 02 02 01 00 |.X.mkdosfs......|
00000010 02 e0 00 80 16 f0 09 00 24 00 02 00 00 00 00 00 |........$.......|
00000020 80 16 00 00 00 00 29 xx xx xx xx 46 53 4c 41 42 |......)xxxxFSLAB|
+27 -18
View File
@@ -228,6 +228,20 @@ static const char boot_code[] ALIGN1 =
// 077: message_txt:
"This is not a bootable disk\r\n";
// -S, -s accept hex too, require power-of-2
static unsigned parse_power2(const char *str, unsigned min_max)
{
unsigned n;
unsigned min = min_max & 0x3ff;
unsigned max = min_max >> 12;
n = bb_strtou(str, NULL, 0);
if (n < min || n > max || (n & (n-1)))
return 0;
return n;
}
#define parse_power2(str, min, max) \
parse_power2((str), ((min) | ((max) << 12)))
//compat:
//mkdosfs 2.11 (12 Mar 2005)
//Usage: mkdosfs [-A] [-c] [-C] [-v] [-I] [-l bad-block-file]
@@ -261,7 +275,6 @@ static const char boot_code[] ALIGN1 =
////usage: "\n -I Allow to use entire disk device (e.g. /dev/hda)"
//usage: "\n -n LBL Volume label"
//usage: "\n -v Verbose"
int mkfs_vfat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
{
@@ -342,7 +355,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
if (!S_ISBLK(st_mode)) {
if (!S_ISREG(st_mode)) {
if (!argv[1])
bb_simple_error_msg_and_die("image size must be specified");
bb_simple_error_msg_and_die("size must be given");
}
// not a block device, skip bad sectors check
// opts &= ~OPT_c;
@@ -369,24 +382,21 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
xioctl(dev, BLKSSZGET, &min_bytes_per_sect);
if (min_bytes_per_sect > SECTOR_SIZE) {
bytes_per_sect = min_bytes_per_sect;
bb_error_msg("for this device sector size is %u", min_bytes_per_sect);
bb_error_msg("sector size: %u", min_bytes_per_sect);
}
}
if (opts & OPT_S) {
// -S: upstream accepts hex too, accepts any power-of-2 from 512 to 0x8000, but sectors >4k
// emit "Warning: sector size N > 4096 is non-standard, filesystem may not be usable"
// -S must be from 512 to 0x8000, but sectors >4k emit
// "Warning: sector size N > 4096 is non-standard, filesystem may not be usable"
// and it's correct: Linux kernel does not support >4k
bytes_per_sect = bb_strtou(arg_sector_size, NULL, 0);
if (bytes_per_sect < 0x200 || bytes_per_sect > 0x8000
|| (bytes_per_sect & (bytes_per_sect-1))
) {
bb_error_msg_and_die("bad -S '%s'", arg_sector_size);
}
bytes_per_sect = parse_power2(arg_sector_size, 0x200, 0x8000);
if (!bytes_per_sect)
bb_error_msg_and_die("bad -%c '%s'", 'S', arg_sector_size);
}
volume_size_bytes = get_volume_size_in_bytes(dev, argv[1], 1024,
/*minsize:*/ 6 * bytes_per_sect | /*extend:*/ 1
/*minsize:*/ 4 * bytes_per_sect | /*extend:*/ 1
);
volume_size_sect = volume_size_bytes / bytes_per_sect;
dbg("volume_size_sect:%llu", ULL(volume_size_sect));
@@ -493,7 +503,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
}
if (reserved_sct < 1 || reserved_sct > 0xffff /* won't fit in header field */)
bb_error_msg_and_die("-R %u is invalid", reserved_sct);
bb_error_msg_and_die("bad -R %u", reserved_sct);
if (volume_size_sect > 0xFFFFFFFFu) {
// FAT32 can't store this (modulo sparsely supported DR-DOS extension).
@@ -511,11 +521,10 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
#define volume_size_sect ((uint32_t)volume_size_sect)
if (opts & OPT_s) {
// -s: upstream accepts hex too, accepts any power-of-2 from 1 to 0x80
unsigned n = bb_strtou(arg_sect_per_clust, NULL, 0);
if (n == 0 || n > 0x80 || (n & (n-1)))
bb_error_msg_and_die("bad -s '%s'", arg_sect_per_clust);
sect_per_clust = n; // uint8_t
// -s must be from 1 to 0x80
sect_per_clust = parse_power2(arg_sect_per_clust, 1, 0x80);
if (!sect_per_clust)
bb_error_msg_and_die("bad -%c '%s'", 's', arg_sect_per_clust);
}
if (opts & OPT_F) {