1 | /*
|
---|
2 | * mbadblocks.c
|
---|
3 | * Mark bad blocks on disk
|
---|
4 | *
|
---|
5 | */
|
---|
6 |
|
---|
7 | #include "sysincludes.h"
|
---|
8 | #include "msdos.h"
|
---|
9 | #include "mtools.h"
|
---|
10 | #include "mainloop.h"
|
---|
11 | #include "fsP.h"
|
---|
12 |
|
---|
13 | void mbadblocks(int argc, char **argv, int type)
|
---|
14 | {
|
---|
15 | int i;
|
---|
16 | char *in_buf;
|
---|
17 | int in_len;
|
---|
18 | off_t start;
|
---|
19 | struct MainParam_t mp;
|
---|
20 | Fs_t *Fs;
|
---|
21 | Stream_t *Dir;
|
---|
22 | int ret;
|
---|
23 |
|
---|
24 | if (argc != 2 || skip_drive(argv[1]) == argv[1]) {
|
---|
25 | fprintf(stderr, "Mtools version %s, dated %s\n",
|
---|
26 | mversion, mdate);
|
---|
27 | fprintf(stderr, "Usage: %s drive:\n", argv[0]);
|
---|
28 | exit(1);
|
---|
29 | }
|
---|
30 |
|
---|
31 | init_mp(&mp);
|
---|
32 |
|
---|
33 | Dir = open_root_dir(get_drive(argv[1], NULL), O_RDWR);
|
---|
34 | if (!Dir) {
|
---|
35 | fprintf(stderr,"%s: Cannot initialize drive\n", argv[0]);
|
---|
36 | exit(1);
|
---|
37 | }
|
---|
38 |
|
---|
39 | Fs = (Fs_t *)GetFs(Dir);
|
---|
40 | in_len = Fs->cluster_size * Fs->sector_size;
|
---|
41 | in_buf = malloc(in_len);
|
---|
42 | if(!in_buf) {
|
---|
43 | FREE(&Dir);
|
---|
44 | printOom();
|
---|
45 | exit(1);
|
---|
46 | }
|
---|
47 | for(i=0; i < Fs->clus_start; i++ ){
|
---|
48 | ret = READS(Fs->Next,
|
---|
49 | in_buf, sectorsToBytes((Stream_t*)Fs, i), Fs->sector_size);
|
---|
50 | if( ret < 0 ){
|
---|
51 | perror("early error");
|
---|
52 | exit(1);
|
---|
53 | }
|
---|
54 | if(ret < Fs->sector_size){
|
---|
55 | fprintf(stderr,"end of file in file_read\n");
|
---|
56 | exit(1);
|
---|
57 | }
|
---|
58 | }
|
---|
59 |
|
---|
60 | in_len = Fs->cluster_size * Fs->sector_size;
|
---|
61 | for(i=2; i< Fs->num_clus + 2; i++){
|
---|
62 | if(got_signal)
|
---|
63 | break;
|
---|
64 | if(Fs->fat_decode((Fs_t*)Fs,i))
|
---|
65 | continue;
|
---|
66 | start = (i - 2) * Fs->cluster_size + Fs->clus_start;
|
---|
67 | ret = force_read(Fs->Next, in_buf,
|
---|
68 | sectorsToBytes((Stream_t*)Fs, start), in_len);
|
---|
69 | if(ret < in_len ){
|
---|
70 | printf("Bad cluster %d found\n", i);
|
---|
71 | fatEncode((Fs_t*)Fs, i, 0xfff7);
|
---|
72 | continue;
|
---|
73 | }
|
---|
74 | }
|
---|
75 | FREE(&Dir);
|
---|
76 | exit(0);
|
---|
77 | }
|
---|