Skip to content

Commit

Permalink
fsck: introduce the option "b" to repair the main boot sector
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
hclee committed Dec 9, 2021
1 parent 9c164ba commit d58057b
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
17 changes: 16 additions & 1 deletion fsck/fsck.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
};

Expand All @@ -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");
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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)
Expand 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;
Expand All @@ -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;
}
Expand Down
1 change: 1 addition & 0 deletions fsck/fsck.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 5 additions & 0 deletions manpages/fsck.exfat.8
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ fsck.exfat \- check an exFAT filesystem
] [
.B \-y
] [
.B \-b
] [
.B \-v
]
.I device
Expand Down Expand Up @@ -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),

0 comments on commit d58057b

Please sign in to comment.