1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Copyright (C) 2020 Hyunchul Lee <hyc.lee@gmail.com>
4 */
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdarg.h>
8
9 #include "exfat_ondisk.h"
10 #include "libexfat.h"
11 #include "fsck.h"
12 #include "repair.h"
13
14 struct exfat_repair_problem {
15 er_problem_code_t prcode;
16 unsigned int flags;
17 unsigned int prompt_type;
18 };
19
20 /* Problem flags */
21 #define ERF_PREEN_YES 0x00000001
22 #define ERF_DEFAULT_YES 0x00000002
23 #define ERF_DEFAULT_NO 0x00000004
24
25 /* Prompt types */
26 #define ERP_FIX 0x00000001
27 #define ERP_TRUNCATE 0x00000002
28
29 static const char *prompts[] = {
30 "Repair",
31 "Fix",
32 "Truncate",
33 };
34
35 static struct exfat_repair_problem problems[] = {
36 {ER_BS_CHECKSUM, ERF_PREEN_YES, ERP_FIX},
37 {ER_BS_BOOT_REGION, 0, ERP_FIX},
38 {ER_DE_CHECKSUM, ERF_PREEN_YES, ERP_FIX},
39 {ER_FILE_VALID_SIZE, ERF_PREEN_YES, ERP_FIX},
40 {ER_FILE_INVALID_CLUS, ERF_DEFAULT_NO, ERP_TRUNCATE},
41 {ER_FILE_FIRST_CLUS, ERF_DEFAULT_NO, ERP_TRUNCATE},
42 {ER_FILE_SMALLER_SIZE, ERF_DEFAULT_NO, ERP_TRUNCATE},
43 {ER_FILE_LARGER_SIZE, ERF_DEFAULT_NO, ERP_TRUNCATE},
44 {ER_FILE_DUPLICATED_CLUS, ERF_DEFAULT_NO, ERP_TRUNCATE},
45 {ER_FILE_ZERO_NOFAT, ERF_PREEN_YES, ERP_FIX},
46 };
47
find_problem(er_problem_code_t prcode)48 static struct exfat_repair_problem *find_problem(er_problem_code_t prcode)
49 {
50 unsigned int i;
51
52 for (i = 0; i < sizeof(problems)/sizeof(problems[0]); i++) {
53 if (problems[i].prcode == prcode) {
54 return &problems[i];
55 }
56 }
57 return NULL;
58 }
59
ask_repair(struct exfat * exfat,struct exfat_repair_problem * pr)60 static bool ask_repair(struct exfat *exfat, struct exfat_repair_problem *pr)
61 {
62 bool repair = false;
63 char answer[8];
64
65 if (exfat->options & FSCK_OPTS_REPAIR_NO ||
66 pr->flags & ERF_DEFAULT_NO)
67 repair = false;
68 else if (exfat->options & FSCK_OPTS_REPAIR_YES ||
69 pr->flags & ERF_DEFAULT_YES)
70 repair = true;
71 else {
72 if (exfat->options & FSCK_OPTS_REPAIR_ASK) {
73 do {
74 printf(". %s (y/N)? ",
75 prompts[pr->prompt_type]);
76 fflush(stdout);
77
78 if (fgets(answer, sizeof(answer), stdin)) {
79 if (strcasecmp(answer, "Y\n") == 0)
80 return true;
81 else if (strcasecmp(answer, "\n") == 0
82 || strcasecmp(answer, "N\n") == 0)
83 return false;
84 }
85 } while (1);
86 } else if (exfat->options & FSCK_OPTS_REPAIR_AUTO &&
87 pr->flags & ERF_PREEN_YES)
88 repair = true;
89 }
90
91 printf(". %s (y/N)? %c\n", prompts[pr->prompt_type],
92 repair ? 'y' : 'n');
93 return repair;
94 }
95
exfat_repair_ask(struct exfat * exfat,er_problem_code_t prcode,const char * desc,...)96 bool exfat_repair_ask(struct exfat *exfat, er_problem_code_t prcode,
97 const char *desc, ...)
98 {
99 struct exfat_repair_problem *pr = NULL;
100 va_list ap;
101
102 pr = find_problem(prcode);
103 if (!pr) {
104 exfat_err("unknown problem code. %#x\n", prcode);
105 return false;
106 }
107
108 va_start(ap, desc);
109 vprintf(desc, ap);
110 va_end(ap);
111
112 if (ask_repair(exfat, pr)) {
113 if (pr->prompt_type & ERP_TRUNCATE)
114 exfat->dirty_fat = true;
115 exfat->dirty = true;
116 return true;
117 } else
118 return false;
119 }
120