1/* Copyright 1995-1999,2001-2003,2007,2009 Alain Knaff. 2 * This file is part of mtools. 3 * 4 * Mtools is free software: you can redistribute it and/or modify 5 * it under the terms of the GNU General Public License as published by 6 * the Free Software Foundation, either version 3 of the License, or 7 * (at your option) any later version. 8 * 9 * Mtools is distributed in the hope that it will be useful, 10 * but WITHOUT ANY WARRANTY; without even the implied warranty of 11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 * GNU General Public License for more details. 13 * 14 * You should have received a copy of the GNU General Public License 15 * along with Mtools. If not, see <http://www.gnu.org/licenses/>. 16 * 17 * mbadblocks.c 18 * Mark bad blocks on disk 19 * 20 */ 21 22#include "sysincludes.h" 23#include "msdos.h" 24#include "mtools.h" 25#include "mainloop.h" 26#include "fsP.h" 27 28void mbadblocks(int argc, char **argv, int type) 29{ 30 unsigned int i; 31 char *in_buf; 32 int in_len; 33 off_t start; 34 struct MainParam_t mp; 35 Fs_t *Fs; 36 Stream_t *Dir; 37 int ret; 38 39 if (argc != 2 || !argv[1][0] || argv[1][1] != ':' || argv[1][2]) { 40 fprintf(stderr, "Mtools version %s, dated %s\n", 41 mversion, mdate); 42 fprintf(stderr, "Usage: %s [-V] device\n", argv[0]); 43 exit(1); 44 } 45 46 init_mp(&mp); 47 48 Dir = open_root_dir(argv[1][0], O_RDWR, NULL); 49 if (!Dir) { 50 fprintf(stderr,"%s: Cannot initialize drive\n", argv[0]); 51 exit(1); 52 } 53 54 Fs = (Fs_t *)GetFs(Dir); 55 in_len = Fs->cluster_size * Fs->sector_size; 56 in_buf = malloc(in_len); 57 if(!in_buf) { 58 FREE(&Dir); 59 printOom(); 60 exit(1); 61 } 62 for(i=0; i < Fs->clus_start; i++ ){ 63 ret = READS(Fs->Next, in_buf, 64 sectorsToBytes((Stream_t*)Fs, i), Fs->sector_size); 65 if( ret < 0 ){ 66 perror("early error"); 67 exit(1); 68 } 69 if(ret < (signed int) Fs->sector_size){ 70 fprintf(stderr,"end of file in file_read\n"); 71 exit(1); 72 } 73 } 74 75 in_len = Fs->cluster_size * Fs->sector_size; 76 for(i=2; i< Fs->num_clus + 2; i++){ 77 if(got_signal) 78 break; 79 if(Fs->fat_decode((Fs_t*)Fs,i)) 80 continue; 81 start = (i - 2) * Fs->cluster_size + Fs->clus_start; 82 ret = force_read(Fs->Next, in_buf, 83 sectorsToBytes((Stream_t*)Fs, start), in_len); 84 if(ret < in_len ){ 85 printf("Bad cluster %d found\n", i); 86 fatEncode((Fs_t*)Fs, i, 0xfff7); 87 continue; 88 } 89 } 90 FREE(&Dir); 91 exit(0); 92} 93