From d58057bd9b2f4a24b874a69975ea186466937360 Mon Sep 17 00:00:00 2001 From: Hyunchul Lee Date: Thu, 9 Dec 2021 08:56:12 +0900 Subject: [PATCH] fsck: introduce the option "b" to repair the main boot sector If the option "b" is given, try to recover the main boot sector even if exfat is not found. otherwise warn it and just exit. Signed-off-by: Hyunchul Lee --- fsck/fsck.c | 17 ++++++++++++++++- fsck/fsck.h | 1 + manpages/fsck.exfat.8 | 5 +++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/fsck/fsck.c b/fsck/fsck.c index 6131d13d..c06197f4 100644 --- a/fsck/fsck.c +++ b/fsck/fsck.c @@ -77,6 +77,7 @@ static struct option opts[] = { {"verbose", no_argument, NULL, 'v' }, {"help", no_argument, NULL, 'h' }, {"?", no_argument, NULL, '?' }, + {"ignore-bad-fs", no_argument, NULL, 'b' }, {NULL, 0, NULL, 0 } }; @@ -88,6 +89,7 @@ static void usage(char *name) fprintf(stderr, "\t-n | --repair-no No repair\n"); fprintf(stderr, "\t-p | --repair-auto Repair automatically\n"); fprintf(stderr, "\t-a Repair automatically\n"); + fprintf(stderr, "\t-b | --ignore-bad-fs Try to recover even if exfat is not found\n"); fprintf(stderr, "\t-V | --version Show version\n"); fprintf(stderr, "\t-v | --verbose Print debug\n"); fprintf(stderr, "\t-h | --help Show help\n"); @@ -796,9 +798,17 @@ static int exfat_boot_region_check(struct exfat *exfat, struct pbr **bs) if (exfat_read(exfat->blk_dev->dev_fd, boot_sect, sizeof(*boot_sect), 0) != (ssize_t)sizeof(*boot_sect)) { exfat_err("failed to read Main boot sector\n"); + free(boot_sect); return -EIO; } + if (memcmp(boot_sect->bpb.oem_name, "EXFAT ", 8) != 0 && + !(exfat->options & FSCK_OPTS_IGNORE_BAD_FS_NAME)) { + exfat_err("Bad fs_name in boot sector, which does not describe a valid exfat filesystem\n"); + free(boot_sect); + return -ENOTSUP; + } + sect_size = 1 << boot_sect->bsx.sect_size_bits; free(boot_sect); @@ -1530,7 +1540,7 @@ int main(int argc, char * const argv[]) exfat_err("failed to init locale/codeset\n"); opterr = 0; - while ((c = getopt_long(argc, argv, "arynpVvh", opts, NULL)) != EOF) { + while ((c = getopt_long(argc, argv, "arynpbVvh", opts, NULL)) != EOF) { switch (c) { case 'n': if (ui.options & FSCK_OPTS_REPAIR_ALL) @@ -1553,6 +1563,9 @@ int main(int argc, char * const argv[]) usage(argv[0]); ui.options |= FSCK_OPTS_REPAIR_AUTO; break; + case 'b': + ui.options |= FSCK_OPTS_IGNORE_BAD_FS_NAME; + break; case 'V': version_only = true; break; @@ -1576,6 +1589,8 @@ int main(int argc, char * const argv[]) if (ui.options & FSCK_OPTS_REPAIR_WRITE) ui.ei.writeable = true; else { + if (ui.options & FSCK_OPTS_IGNORE_BAD_FS_NAME) + usage(argv[0]); ui.options |= FSCK_OPTS_REPAIR_NO; ui.ei.writeable = false; } diff --git a/fsck/fsck.h b/fsck/fsck.h index 6c91face..56d3b3b8 100644 --- a/fsck/fsck.h +++ b/fsck/fsck.h @@ -54,6 +54,7 @@ enum fsck_ui_options { FSCK_OPTS_REPAIR_AUTO = 0x08, FSCK_OPTS_REPAIR_WRITE = 0x0b, FSCK_OPTS_REPAIR_ALL = 0x0f, + FSCK_OPTS_IGNORE_BAD_FS_NAME = 0x10, }; struct exfat { diff --git a/manpages/fsck.exfat.8 b/manpages/fsck.exfat.8 index 83f7815a..b93baaa9 100644 --- a/manpages/fsck.exfat.8 +++ b/manpages/fsck.exfat.8 @@ -14,6 +14,8 @@ fsck.exfat \- check an exFAT filesystem ] [ .B \-y ] [ +.B \-b +] [ .B \-v ] .I device @@ -46,6 +48,9 @@ Prints the version number and exits. .TP .B \-y Repair the filesystem answering yes to all questions. +.TP +.B \-b +Try to repair the filesystem even if the exFAT filesystem is not found. .SH SEE ALSO .BR fsck (8), .BR fstab (5),