diff options
author | Timo Teräs <timo.teras@iki.fi> | 2022-01-21 13:17:00 +0200 |
---|---|---|
committer | Denys Vlasenko <vda.linux@googlemail.com> | 2022-01-23 23:07:07 +0100 |
commit | 78fdf4d22d578d5d51cc08c768b35d050a92902a (patch) | |
tree | 5f8f343f582001baaea4fcbbe3dbc115860bee00 /util-linux/mkfs_vfat.c | |
parent | 6dd6a6c42d1465d8cca2539476f6bffd5e1353dd (diff) | |
download | busybox-w32-78fdf4d22d578d5d51cc08c768b35d050a92902a.tar.gz busybox-w32-78fdf4d22d578d5d51cc08c768b35d050a92902a.tar.bz2 busybox-w32-78fdf4d22d578d5d51cc08c768b35d050a92902a.zip |
mkfs.vfat: fix volume label to be padded with space
The specification requires volume label to be space 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 1470 1502 +32
static.NO_NAME_11 - 12 +12
.rodata 104309 104318 +9
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/0 up/down: 53/0) Total: 53 bytes
Signed-off-by: Timo Teräs <timo.teras@iki.fi>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
Diffstat (limited to 'util-linux/mkfs_vfat.c')
-rw-r--r-- | util-linux/mkfs_vfat.c | 25 |
1 files changed, 17 insertions, 8 deletions
diff --git a/util-linux/mkfs_vfat.c b/util-linux/mkfs_vfat.c index 844d965f8..821371953 100644 --- a/util-linux/mkfs_vfat.c +++ b/util-linux/mkfs_vfat.c | |||
@@ -218,8 +218,11 @@ static const char boot_code[] ALIGN1 = | |||
218 | int mkfs_vfat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; | 218 | int mkfs_vfat_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
219 | int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv) | 219 | int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv) |
220 | { | 220 | { |
221 | static const char NO_NAME_11[] = "NO NAME "; | ||
222 | |||
221 | struct stat st; | 223 | struct stat st; |
222 | const char *volume_label = ""; | 224 | const char *arg_volume_label = NO_NAME_11; //default |
225 | char volume_label11[12]; | ||
223 | char *buf; | 226 | char *buf; |
224 | char *device_name; | 227 | char *device_name; |
225 | uoff_t volume_size_bytes; | 228 | uoff_t volume_size_bytes; |
@@ -257,14 +260,17 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv) | |||
257 | opts = getopt32(argv, "^" | 260 | opts = getopt32(argv, "^" |
258 | "Ab:cCf:F:h:Ii:l:m:n:r:R:s:S:v" | 261 | "Ab:cCf:F:h:Ii:l:m:n:r:R:s:S:v" |
259 | "\0" "-1", //:b+:f+:F+:h+:r+:R+:s+:S+:vv:c--l:l--c | 262 | "\0" "-1", //:b+:f+:F+:h+:r+:R+:s+:S+:vv:c--l:l--c |
260 | NULL, NULL, NULL, NULL, NULL, | 263 | /*b*/NULL, /*f*/NULL, /*F*/NULL, /*h*/NULL, /*i*/NULL, |
261 | NULL, NULL, &volume_label, NULL, NULL, NULL, NULL); | 264 | /*l*/NULL, /*m*/NULL, /*n*/&arg_volume_label, |
265 | /*r*/NULL, /*R*/NULL, /*s*/NULL, /*S*/NULL); | ||
262 | argv += optind; | 266 | argv += optind; |
263 | 267 | ||
264 | // cache device name | 268 | // cache device name |
265 | device_name = argv[0]; | 269 | device_name = argv[0]; |
266 | // default volume ID = creation time | 270 | // default volume ID = creation time |
267 | volume_id = time(NULL); | 271 | volume_id = time(NULL); |
272 | // truncate to exactly 11 chars, pad with spaces | ||
273 | sprintf(volume_label11, "%-11.11s", arg_volume_label); | ||
268 | 274 | ||
269 | dev = xopen(device_name, O_RDWR); | 275 | dev = xopen(device_name, O_RDWR); |
270 | xfstat(dev, &st, device_name); | 276 | xfstat(dev, &st, device_name); |
@@ -459,7 +465,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv) | |||
459 | (int)media_byte, | 465 | (int)media_byte, |
460 | volume_size_sect, (int)total_clust, (int)sect_per_clust, | 466 | volume_size_sect, (int)total_clust, (int)sect_per_clust, |
461 | sect_per_fat, | 467 | sect_per_fat, |
462 | (int)volume_id, volume_label | 468 | (int)volume_id, volume_label11 |
463 | ); | 469 | ); |
464 | } | 470 | } |
465 | 471 | ||
@@ -508,7 +514,7 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv) | |||
508 | STORE_LE(boot_blk->vi.ext_boot_sign, 0x29); | 514 | STORE_LE(boot_blk->vi.ext_boot_sign, 0x29); |
509 | STORE_LE(boot_blk->vi.volume_id32, volume_id); | 515 | STORE_LE(boot_blk->vi.volume_id32, volume_id); |
510 | memcpy(boot_blk->vi.fs_type, "FAT32 ", sizeof(boot_blk->vi.fs_type)); | 516 | memcpy(boot_blk->vi.fs_type, "FAT32 ", sizeof(boot_blk->vi.fs_type)); |
511 | strncpy(boot_blk->vi.volume_label, volume_label, sizeof(boot_blk->vi.volume_label)); | 517 | memcpy(boot_blk->vi.volume_label, volume_label11, 11); |
512 | memcpy(boot_blk->boot_code, boot_code, sizeof(boot_code)); | 518 | memcpy(boot_blk->boot_code, boot_code, sizeof(boot_code)); |
513 | STORE_LE(boot_blk->boot_sign, BOOT_SIGN); | 519 | STORE_LE(boot_blk->boot_sign, BOOT_SIGN); |
514 | 520 | ||
@@ -545,15 +551,18 @@ int mkfs_vfat_main(int argc UNUSED_PARAM, char **argv) | |||
545 | // root directory | 551 | // root directory |
546 | // empty directory is just a set of zero bytes | 552 | // empty directory is just a set of zero bytes |
547 | memset(buf, 0, sect_per_clust * bytes_per_sect); | 553 | memset(buf, 0, sect_per_clust * bytes_per_sect); |
548 | if (volume_label[0]) { | 554 | // not "NO NAME", "NO NAME " etc? |
549 | // create dir entry for volume_label | 555 | // (mkfs.fat 4.1 won't create dir entry even with explicit -n 'NO NAME', |
556 | // but will create one with e.g. -n '', -n ' zZz') | ||
557 | if (strcmp(volume_label11, NO_NAME_11) != 0) { | ||
558 | // create dir entry for volume label | ||
550 | struct msdos_dir_entry *de; | 559 | struct msdos_dir_entry *de; |
551 | #if 0 | 560 | #if 0 |
552 | struct tm tm_time; | 561 | struct tm tm_time; |
553 | uint16_t t, d; | 562 | uint16_t t, d; |
554 | #endif | 563 | #endif |
555 | de = (void*)buf; | 564 | de = (void*)buf; |
556 | strncpy(de->name, volume_label, sizeof(de->name)); | 565 | memcpy(de->name, volume_label11, 11); |
557 | STORE_LE(de->attr, ATTR_VOLUME); | 566 | STORE_LE(de->attr, ATTR_VOLUME); |
558 | #if 0 | 567 | #if 0 |
559 | localtime_r(&create_time, &tm_time); | 568 | localtime_r(&create_time, &tm_time); |