[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox
Subject:    [PATCH] mkfs.vfat: fix volume label to be padded with space
From:       Timo Teräs <timo.teras () iki ! fi>
Date:       2022-01-21 11:17:00
Message-ID: 20220121111700.34182-1-timo.teras () iki ! fi
[Download RAW message or body]

The specification requires volume label to be zero padded.

Latest fsck.vfat will remove the zero padded volume label
as invalid. See also:
https://github.com/dosfstools/dosfstools/issues/172

Make the default label also "NO NAME" which has the special meaning
that label is not set.

function                                             old     new   delta
mkfs_vfat_main                                      1546    1665    +119
.rodata                                            79474   79480      +6
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 125/0)             Total: 125 bytes
---
Also the volume label should not contain certain special character, or
low case alphabet. However, seems lower case is allowed still by default
in fsck.vfat, and the special characters are a bit of corner case. So I
decided to not add checking for these.

 util-linux/mkfs_vfat.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)

diff --git a/util-linux/mkfs_vfat.c b/util-linux/mkfs_vfat.c
index 844d965f8..fc256b903 100644
--- a/util-linux/mkfs_vfat.c
+++ b/util-linux/mkfs_vfat.c
@@ -219,7 +219,8 @@ int mkfs_vfat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
 {
 	struct stat st;
-	const char *volume_label = "";
+	const char *arg_volume_label = "NO NAME    ";
+	char volume_label[12];
 	char *buf;
 	char *device_name;
 	uoff_t volume_size_bytes;
@@ -258,13 +259,14 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
 		"Ab:cCf:F:h:Ii:l:m:n:r:R:s:S:v"
 		"\0" "-1", //:b+:f+:F+:h+:r+:R+:s+:S+:vv:c--l:l--c
 		NULL, NULL, NULL, NULL, NULL,
-		NULL, NULL, &volume_label, NULL, NULL, NULL, NULL);
+		NULL, NULL, &arg_volume_label, NULL, NULL, NULL, NULL);
 	argv += optind;
 
 	// cache device name
 	device_name = argv[0];
 	// default volume ID = creation time
 	volume_id = time(NULL);
+	snprintf(volume_label, sizeof(volume_label), "%-11s", arg_volume_label);
 
 	dev = xopen(device_name, O_RDWR);
 	xfstat(dev, &st, device_name);
@@ -508,7 +510,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
 		STORE_LE(boot_blk->vi.ext_boot_sign, 0x29);
 		STORE_LE(boot_blk->vi.volume_id32, volume_id);
 		memcpy(boot_blk->vi.fs_type, "FAT32   ", sizeof(boot_blk->vi.fs_type));
-		strncpy(boot_blk->vi.volume_label, volume_label, sizeof(boot_blk->vi.volume_label));
+		memcpy(boot_blk->vi.volume_label, volume_label, sizeof(boot_blk->vi.volume_label));
 		memcpy(boot_blk->boot_code, boot_code, sizeof(boot_code));
 		STORE_LE(boot_blk->boot_sign, BOOT_SIGN);
 
@@ -553,7 +555,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv)
 		uint16_t t, d;
 #endif
 		de = (void*)buf;
-		strncpy(de->name, volume_label, sizeof(de->name));
+		memcpy(de->name, volume_label, sizeof(de->name));
 		STORE_LE(de->attr, ATTR_VOLUME);
 #if 0
 		localtime_r(&create_time, &tm_time);
-- 
2.34.1

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic