/* chkfatflags * (c) Sebastian Wills 2005 */ #include unsigned int read_twobytes(FILE *stream); int main(int argc, char **argv) { FILE *fs; unsigned int sector_size; unsigned int reserved_sectors; unsigned char num_fats; unsigned int fat_size; unsigned int fat_start; unsigned int fatentry; int fattype=16; char clean_shutdown; char no_errors_detected; unsigned char c; int i; if(argc<2) { fprintf(stderr,"Usage: %s \n",argv[0]); exit(3); } if((fs=fopen(argv[1],"rb"))==NULL) { fprintf(stderr,"Error opening %s\n",argv[1]); exit(3); } safe_fseek(fs, 11, SEEK_SET); sector_size=read_twobytes(fs); printf("Sector size: %d\n",sector_size); safe_fseek(fs, 14, SEEK_SET); reserved_sectors=read_twobytes(fs); printf("Number of reserved sectors: %d\n", reserved_sectors); safe_fseek(fs,16, SEEK_SET); fread(&num_fats,1,1,fs); printf("Number of FATs: %d\n",num_fats); safe_fseek(fs,22, SEEK_SET); fat_size=read_twobytes(fs); if(fat_size==0) /* FAT32 */ { fattype=32; safe_fseek(fs,36,SEEK_SET); fread(&fat_size,4,1,fs); } printf("FAT size: %d sectors\n", fat_size); fat_start=sector_size*reserved_sectors; printf("FAT starts at offset %d (%x)\n",fat_start,fat_start); if(fattype & 0xfff != 0) { fprintf(stderr,"Only FAT16 or FAT32 supported, not FAT12\n"); exit(3); } /* seek to start of fat plus skip over first FAT entry (cluster 0) to get to cluster 1. Each entry is fat_type bits) */ fseek(fs, fat_start+fattype/8, SEEK_SET); fread(&fatentry, fattype/8, 1, fs); printf("FAT entry for cluster 1: 0x%2x\n",fatentry); clean_shutdown= (fatentry & (0x8 << (fattype/8-1))) != 0; no_errors_detected=(fatentry & (0x4 << (fattype/8-1))) != 0; printf("Clean shutdown: %s\n",clean_shutdown ? "yes":"no"); printf("Errors detected: %s\n",!no_errors_detected ? "yes":"no"); } unsigned int read_twobytes(FILE *stream) { unsigned char c; unsigned int result; fread(&c,1,1,stream); result=c; fread(&c,1,1,stream); result=result | ((unsigned int) c << 8); return result; } int safe_fseek(FILE *stream, long offset, int whence) { int retval; if(retval=fseek(stream,offset,whence)==-1) { fprintf(stderr,"Error seeking to offset %ld. Exciting.\n",offset); exit(3); } return retval; }