-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdrecover.c
429 lines (377 loc) · 12.4 KB
/
hdrecover.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
/* hdrecover
*
* Copyright (C) 2006-2013 Steven Price
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <malloc.h>
#include <errno.h>
#include <string.h>
#include <time.h>
#include <sys/ioctl.h>
#include <linux/fs.h>
const char *program_name;
int badblocks = 0;
int recovered = 0;
int destroyed = 0;
bool confirm_all = false;
bool shown_big_warning = false;
unsigned int phys_block_size = 0;
char *buf = NULL;
int fd = 0;
const char *dev_name;
bool dd_rewrite = false;
int64_t length = 0;
int correctsector(int64_t sectornum)
{
ssize_t ret = 0;
badblocks++;
printf("Error at sector %ld\n", sectornum);
if (!dd_rewrite)
{
printf("Attempting to pounce on it...\n");
for (int i = 0; i < 20 && ret != phys_block_size; i++)
{
long int b = random();
b = ((double)length / RAND_MAX) * b;
ret = pread(fd, buf, phys_block_size, b * phys_block_size);
if (ret != phys_block_size)
{
printf("WARNING: Failed to read the random sector %ld!\n", b);
}
ret = pread(fd, buf, phys_block_size, sectornum * phys_block_size);
printf("Attempt %d from sector %12ld: %s\n",
i + 1, sectornum, (ret == phys_block_size) ? "SUCCESS!" : "FAILED");
}
}
if (ret != phys_block_size)
{
printf("Couldn't recover sector %ld\n", sectornum);
if (!confirm_all)
{
printf("The data for this sector could not be recovered. "
"However, destroying the\n");
printf("contents of this sector (ie writing zeros to it) "
"should cause the hard disk\n");
printf("to reallocate it making the drive useable again\n");
printf("Do you really want to destroy the data in sector %ld?"
"\n [ (y)es / (n)o / (a)ll / (q)uit ]:", sectornum);
input:
fgets(buf, 10, stdin);
switch (*buf)
{
case 'n':
printf("Not wiping sector %ld, continuing...\n", sectornum);
return 0;
case 'q':
printf("Not wiping sector %ld, exiting.\n", sectornum);
return 1;
case 'a':
printf("You requested to wipe all bad sectors "
"without further confirmation.\n");
confirm_all = true;
break;
case 'y':
break;
default:
printf("Illegal input '%s', please retry: "
"[ (y)es / (n)o / (a)ll / (q)uit ]:\n", buf);
goto input;
}
}
if (!shown_big_warning)
{
printf("\n\n/---------\\\n");
printf("| WARNING |\n");
printf("\\---------/\n\n");
printf("Up until this point you haven't lost any "
"data because of this program\n");
printf("However, if you say yes below YOU WILL LOSE DATA!\n");
printf("Proceed at your own risk!\n\n");
printf("Type 'destroy data' to continue "
"(choice valid for all subsequent errors!)\n");
fgets(buf, 50, stdin);
if (strcmp(buf, "destroy data\n"))
{
printf("You didn't type 'destroy data' so I'm aborting\n");
return 1;
}
shown_big_warning = true;
}
printf("\nWiping sector %ld...\n", sectornum);
memset(buf, 0, phys_block_size);
pwrite(fd, buf, phys_block_size, sectornum * phys_block_size);
if (dd_rewrite)
{
const char* binary_name = "dd if=/dev/zero of=";
char command_name[100];
char argument[80];
char argument2[20]=" count=1 seek=";
sprintf(command_name, "%s", binary_name);
sprintf(argument, "%s", dev_name);
strcat(command_name, argument);
strcat(command_name, argument2);
sprintf(argument, "%lld", sectornum);
strcat(command_name, argument);
printf("\nRun command: %s\n", command_name);
system(command_name);
}
destroyed++;
printf("Checking sector is now readable...\n");
ret = pread(fd, buf, phys_block_size, sectornum * phys_block_size);
if (ret != phys_block_size)
{
printf("I still couldn't read the sector!\n");
printf("I'm sorry, but even writing to the sector hasn't fixed it"
" - there's nothing\n");
printf("more I can do!\n");
sleep(1);
return 1;
}
else
{
printf("Sector is now readable. But you have lost data!\n");
}
}
else
{
recovered++;
}
return 0;
}
void usage()
{
fprintf(stderr,
"Usage: %s [-s <logical start block>] [-e <logical end block>] [-d {DESTROY, DD}] <block device>\n",
program_name);
exit(EXIT_FAILURE);
}
int main(int argc, char **argv, char **envp)
{
printf(PACKAGE " version " VERSION "\n");
printf("By Steven Price\n\n");
if (sizeof(off_t) != sizeof(int64_t))
{
fprintf(stderr, "Offsets are not 64 bit!\n");
fprintf(stderr, "This program must be compiled to have 64 bit file offsets\n");
fprintf(stderr, "Exiting\n");
return 1;
}
program_name = argv[0];
int64_t logical_start_block = 0;
int64_t logical_end_block = 0;
int opt;
while ((opt = getopt(argc, argv, "s:e:d:")) != -1)
{
switch (opt)
{
case 's':
logical_start_block = atoll(optarg);
break;
case 'e':
logical_end_block = atoll(optarg);
break;
case 'd':
if (!strcmp("DESTROY", optarg))
{
confirm_all = true;
shown_big_warning = true;
printf("Destructive mode enabled!\n");
}
if (!strcmp("DD", optarg))
{
dd_rewrite = true;
printf("DD rewrite mode enabled!\n");
}
break;
default:
usage();
}
}
if (optind > argc - 1)
usage();
dev_name = argv[optind];
fd = open(dev_name, O_RDWR | O_DIRECT);
if (fd < 0)
{
fprintf(stderr, "Failed to open device '%s': %s\n", dev_name, strerror(errno));
return 1;
}
srandom(time(NULL));
length = lseek(fd, 0, SEEK_END);
if (length == -1)
{
fprintf(stderr, "Failed to get size of block device: %s\n", strerror(errno));
return 1;
}
if (ioctl(fd, BLKPBSZGET, &phys_block_size) == -1)
{
fprintf(stderr, "Failed to get physical block size of device: %s\n", strerror(errno));
return 1;
}
if (length % phys_block_size)
{
fprintf(stderr, "Block device size isn't a multiple of %d\n", phys_block_size);
fprintf(stderr, "Is it really a block device?\n");
fprintf(stderr, "Or does it have some strange sector size?\n");
fprintf(stderr, "Since this is unexpected we won't continue\n");
return 1;
}
length /= phys_block_size;
unsigned int logical_block_size = 0;
if (ioctl(fd, BLKSSZGET, &logical_block_size) == -1)
{
fprintf(stderr, "Failed to get logical block size of device: %s\n", strerror(errno));
return 1;
}
printf("Logical sector size is %u bytes\n", logical_block_size);
printf("Physical sector size is %u bytes\n", phys_block_size);
printf("Disk is %ld physical sectors big\n", length);
time_t starttime;
time(&starttime);
time_t lasttime;
time(&lasttime);
int64_t sectornum = logical_start_block / (phys_block_size / logical_block_size);
if (logical_end_block)
{
int64_t new_length = logical_end_block / (phys_block_size / logical_block_size);
if (new_length > length)
{
fprintf(stderr, "Logical end block is out of range!\n");
return 1;
}
else
length = new_length;
}
if (sectornum >= length)
{
fprintf(stderr, "Logical start block is out of range!\n");
return 1;
}
int blocksize = phys_block_size * 20;
// Ensure the buffer is block size byte aligned...
if (posix_memalign((void **) &buf, phys_block_size, blocksize))
{
fprintf(stderr, "Failed to allocate buffer!\n");
return 1;
}
while (sectornum < length)
{
ssize_t ret = pread(fd, buf, blocksize, sectornum * phys_block_size);
if (ret == blocksize) // This 20x physical block size block is fine
{
sectornum += blocksize / phys_block_size;
}
else
{
// Somewhere in this block there's something wrong...
// Or we could just be at the end of the disk
int64_t endsectornum = sectornum + blocksize / phys_block_size;
if (endsectornum < length)
{
printf("Failed to read block at sector %ld, "
"investigating further...\n", sectornum);
}
else
{
endsectornum = length;
}
while (sectornum < endsectornum)
{
ret = pread(fd, buf, phys_block_size, sectornum * phys_block_size);
if (ret != phys_block_size)
{
if (correctsector(sectornum))
{
goto summary;
}
}
sectornum++;
}
}
if (time(NULL) != lasttime)
{
time(&lasttime);
char rs[256];
*rs = 0;
if (sectornum > 0)
{
time_t now;
time(&now);
int64_t remaining = now - starttime;
remaining *= length;
remaining /= sectornum;
remaining -= (now - starttime);
char *p = rs;
if (remaining > 3600)
{
sprintf(p, "%ld hours ", remaining / 3600);
p += strlen(p);
remaining %= 3600;
}
if (remaining > 60)
{
sprintf(p, "%ld minutes ", remaining / 60);
p += strlen(p);
remaining %= 60;
}
sprintf(p, "%ld seconds", remaining);
}
printf("Sector %ld (%02d%%) ETR: %s\n",
sectornum, (int) ((sectornum * 100) / length), rs);
}
}
summary:
printf("Summary:\n");
printf(" %d bad sectors found\n", badblocks);
if (badblocks)
{
printf(" of those %d were recovered\n", recovered);
}
if (destroyed)
{
printf(" and %d could not be recovered and were destroyed "
"causing data loss\n\n", destroyed);
printf("*****************************************\n"
"* You have wiped a sector on this disk! *\n"
"*****************************************\n"
"* If you care about the filesystem on *\n"
"* this disk you should run fsck on it *\n"
"* before mounting it to correct any *\n"
"* potential metadata errors. *\n"
"*****************************************\n\n");
return 10;
}
if (recovered)
{
return 9;
}
if (badblocks)
{
return 8;
}
return 0;
}