1 /* SPDX-License-Identifier: GPL-2.0-or-later */
2 /*
3 * Copyright (C) 2020 Hyunchul Lee <[email protected]>
4 */
5 #include <stdio.h>
6 #include <string.h>
7 #include <stdarg.h>
8 #include <stdlib.h>
9
10 #include "exfat_ondisk.h"
11 #include "libexfat.h"
12 #include "repair.h"
13 #include "exfat_fs.h"
14 #include "exfat_dir.h"
15 #include "fsck.h"
16
17 struct exfat_repair_problem {
18 er_problem_code_t prcode;
19 unsigned int flags;
20 unsigned int prompt_type;
21 unsigned int default_number;
22 unsigned int bypass_number;
23 unsigned int max_number;
24 };
25
26 /* Problem flags */
27 #define ERF_PREEN_YES 0x00000001
28 #define ERF_DEFAULT_YES 0x00000002
29 #define ERF_DEFAULT_NO 0x00000004
30
31 /* Prompt types */
32 #define ERP_FIX 0x00000001
33 #define ERP_TRUNCATE 0x00000002
34 #define ERP_DELETE 0x00000003
35 #define ERP_RENAME 0x00000004
36
37 static const char *prompts[] = {
38 "Repair",
39 "Fix",
40 "Truncate",
41 "Delete",
42 "Select",
43 };
44
45 static struct exfat_repair_problem problems[] = {
46 {ER_BS_CHECKSUM, ERF_PREEN_YES, ERP_FIX, 0, 0, 0},
47 {ER_BS_BOOT_REGION, 0, ERP_FIX, 0, 0, 0},
48 {ER_DE_CHECKSUM, ERF_PREEN_YES, ERP_DELETE, 0, 0, 0},
49 {ER_DE_UNKNOWN, ERF_PREEN_YES, ERP_DELETE, 0, 0, 0},
50 {ER_DE_FILE, ERF_PREEN_YES, ERP_DELETE, 0, 0, 0},
51 {ER_DE_SECONDARY_COUNT, ERF_PREEN_YES, ERP_DELETE, 0, 0, 0},
52 {ER_DE_STREAM, ERF_PREEN_YES, ERP_DELETE, 0, 0, 0},
53 {ER_DE_NAME, ERF_PREEN_YES, ERP_DELETE, 0, 0, 0},
54 {ER_DE_NAME_HASH, ERF_PREEN_YES, ERP_FIX, 0, 0, 0},
55 {ER_DE_NAME_LEN, ERF_PREEN_YES, ERP_FIX, 0, 0, 0},
56 {ER_DE_DOT_NAME, ERF_PREEN_YES, ERP_RENAME, 2, 3, 4},
57 {ER_FILE_VALID_SIZE, ERF_PREEN_YES, ERP_FIX, 0, 0, 0},
58 {ER_FILE_INVALID_CLUS, ERF_PREEN_YES, ERP_TRUNCATE, 0, 0, 0},
59 {ER_FILE_FIRST_CLUS, ERF_PREEN_YES, ERP_TRUNCATE, 0, 0, 0},
60 {ER_FILE_SMALLER_SIZE, ERF_PREEN_YES, ERP_TRUNCATE, 0, 0, 0},
61 {ER_FILE_LARGER_SIZE, ERF_PREEN_YES, ERP_TRUNCATE, 0, 0, 0},
62 {ER_FILE_DUPLICATED_CLUS, ERF_PREEN_YES, ERP_TRUNCATE, 0, 0, 0},
63 {ER_FILE_ZERO_NOFAT, ERF_PREEN_YES, ERP_FIX, 0, 0, 0},
64 {ER_DE_FIRST_CLUS, ERF_PREEN_YES, ERP_FIX, 0, 0, 0}
65 };
66
find_problem(er_problem_code_t prcode)67 static struct exfat_repair_problem *find_problem(er_problem_code_t prcode)
68 {
69 unsigned int i;
70
71 for (i = 0; i < sizeof(problems)/sizeof(problems[0]); i++) {
72 if (problems[i].prcode == prcode) {
73 return &problems[i];
74 }
75 }
76 return NULL;
77 }
78
ask_repair(struct exfat_fsck * fsck,struct exfat_repair_problem * pr)79 static int ask_repair(struct exfat_fsck *fsck, struct exfat_repair_problem *pr)
80 {
81 int repair = 0;
82 char answer[8];
83
84 if (fsck->options & FSCK_OPTS_REPAIR_NO ||
85 pr->flags & ERF_DEFAULT_NO)
86 repair = 0;
87 else if (fsck->options & FSCK_OPTS_REPAIR_YES ||
88 pr->flags & ERF_DEFAULT_YES)
89 repair = 1;
90 else {
91 if (fsck->options & FSCK_OPTS_REPAIR_ASK) {
92 do {
93 if (pr->prompt_type & ERP_RENAME) {
94 printf("%s (Number: ?) ",
95 prompts[pr->prompt_type]);
96 } else {
97 printf(". %s (y/N)? ",
98 prompts[pr->prompt_type]);
99 }
100 fflush(stdout);
101
102 if (!fgets(answer, sizeof(answer), stdin))
103 continue;
104
105 if (pr->prompt_type & ERP_RENAME) {
106 unsigned int number = atoi(answer);
107
108 if (number > 0 && number < pr->max_number)
109 return number;
110 } else {
111 if (strcasecmp(answer, "Y\n") == 0)
112 return 1;
113 else if (strcasecmp(answer, "\n") == 0 ||
114 strcasecmp(answer, "N\n") == 0)
115 return 0;
116 }
117 } while (1);
118 } else if (fsck->options & FSCK_OPTS_REPAIR_AUTO &&
119 pr->flags & ERF_PREEN_YES)
120 repair = 1;
121 }
122
123 if (pr->prompt_type & ERP_RENAME) {
124 int print_num = repair ? pr->default_number : pr->bypass_number;
125
126 printf("%s (Number : %d)\n", prompts[pr->prompt_type],
127 print_num);
128 repair = print_num;
129 } else {
130 printf(". %s (y/N)? %c\n", prompts[pr->prompt_type],
131 repair ? 'y' : 'n');
132 }
133 return repair;
134 }
135
exfat_repair_ask(struct exfat_fsck * fsck,er_problem_code_t prcode,const char * desc,...)136 int exfat_repair_ask(struct exfat_fsck *fsck, er_problem_code_t prcode,
137 const char *desc, ...)
138 {
139 struct exfat_repair_problem *pr = NULL;
140 va_list ap;
141 int repair;
142
143 pr = find_problem(prcode);
144 if (!pr) {
145 exfat_err("unknown problem code. %#x\n", prcode);
146 return 0;
147 }
148
149 va_start(ap, desc);
150 vprintf(desc, ap);
151 va_end(ap);
152
153 repair = ask_repair(fsck, pr);
154 if (repair) {
155 if (pr->prompt_type & ERP_TRUNCATE)
156 fsck->dirty_fat = true;
157 fsck->dirty = true;
158 }
159 return repair;
160 }
161