Detecting and repairing fs
This commit is contained in:
parent
e51de87fd2
commit
3d148f75ea
|
|
@ -55,7 +55,10 @@ typedef struct{
|
||||||
uint32_t *file_table;
|
uint32_t *file_table;
|
||||||
}fat_info;
|
}fat_info;
|
||||||
|
|
||||||
|
int verbose = 0;
|
||||||
|
|
||||||
int read_sector(FILE *file, uint32_t sector_start, uint32_t count, buffer_t buf){
|
int read_sector(FILE *file, uint32_t sector_start, uint32_t count, buffer_t buf){
|
||||||
|
verbose && printf("Attempting to read bytes 0x%x - 0x%x\n", sector_start * 512, sector_start * 512 + count * 512);
|
||||||
int result = fseek(file, sector_start * 512, SEEK_SET);
|
int result = fseek(file, sector_start * 512, SEEK_SET);
|
||||||
if(result){
|
if(result){
|
||||||
return result;
|
return result;
|
||||||
|
|
@ -69,35 +72,57 @@ int read_sector(FILE *file, uint32_t sector_start, uint32_t count, buffer_t buf)
|
||||||
int write_sector(FILE *file, uint32_t sector_start, uint32_t count, buffer_t buf){
|
int write_sector(FILE *file, uint32_t sector_start, uint32_t count, buffer_t buf){
|
||||||
int result = fseek(file, sector_start * 512, SEEK_SET);
|
int result = fseek(file, sector_start * 512, SEEK_SET);
|
||||||
if(result){
|
if(result){
|
||||||
|
printf("Error during seek\n");
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
result = fwrite(buf, 512, count, file);
|
result = fwrite(buf, 512, count, file);
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
int write_file(char *path, fat_info *info, FILE *file){
|
||||||
|
FILE *ifile = fopen(path, "r");
|
||||||
|
if(!ifile){
|
||||||
|
printf("Failed to open file: %s\nAborting...\n", path);
|
||||||
|
exit(-1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int read_file(){
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
void write_cluster_value(fat_info *info, uint32_t value, uint32_t cluster){
|
||||||
|
for(int i = 0; i < info->bpb.fat_count; i++){
|
||||||
|
info->file_table[cluster + i * info->bpb.sectors_per_fat * (info->bpb.bytes_per_sector/4)] = value;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
int fat_write_back(FILE *file, fat_info *info){
|
||||||
|
int res = 0;
|
||||||
|
if(res = write_sector(file, info->bpb.reserved_sectors, info->bpb.sectors_per_fat * info->bpb.fat_count, (buffer_t)info->file_table) == 0){
|
||||||
|
verbose && printf("Error during write: %d\n", res);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
int detect_repair_fs(FILE *file, fat_info *info){
|
int detect_repair_fs(FILE *file, fat_info *info){
|
||||||
buffer_t boot_sector = malloc(512);
|
buffer_t boot_sector = malloc(512);
|
||||||
read_sector(file, 0, 1, boot_sector);
|
read_sector(file, 0, 1, boot_sector);
|
||||||
memcpy(&(info->bpb), boot_sector, 90);
|
memcpy(&(info->bpb), boot_sector, 90);
|
||||||
buffer_t fat = malloc(info->bpb.sectors_per_fat * info->bpb.fat_count * info->bpb.bytes_per_sector);
|
buffer_t fat = malloc(info->bpb.sectors_per_fat * info->bpb.fat_count * info->bpb.bytes_per_sector);
|
||||||
printf("allocated 0x%x bytes for fat\n", (info->bpb.sectors_per_fat * info->bpb.fat_count * info->bpb.bytes_per_sector));
|
verbose && printf("allocated 0x%x bytes for fat\n", (info->bpb.sectors_per_fat * info->bpb.fat_count * info->bpb.bytes_per_sector));
|
||||||
read_sector(file, info->bpb.reserved_sectors, info->bpb.fat_count * info->bpb.sectors_per_fat, fat);
|
int result = read_sector(file, info->bpb.reserved_sectors, info->bpb.fat_count * info->bpb.sectors_per_fat, fat);
|
||||||
info->file_table = fat;
|
if(result == 0){
|
||||||
if(!fat[info->bpb.root_dir_cluster]){
|
printf("Error: Could not read from disk\n");
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
info->file_table = (uint32_t *)fat;
|
||||||
|
verbose && printf("FAT assigned\n");
|
||||||
|
if(!info->file_table[info->bpb.root_dir_cluster]){
|
||||||
|
verbose && printf("Root directory not assigned, assigning cluster at cluster %d\n", info->bpb.root_dir_cluster);
|
||||||
write_cluster_value(info, FILE_END, info->bpb.root_dir_cluster);
|
write_cluster_value(info, FILE_END, info->bpb.root_dir_cluster);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void write_cluster_value(fat_info *info, uint32_t value, uint32_t cluster){
|
|
||||||
for(int i = 0; i < info->bpb.fat_count; i++){
|
|
||||||
info->file_table[cluster + i * info->bpb.sectors_per_fat * info->bpb.bytes_per_sector] = value;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
int fat_write_back(FILE *file, fat_info *info){
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
int main(int argc, char **argv){
|
int main(int argc, char **argv){
|
||||||
char **files = malloc(512);
|
char **files = malloc(512);
|
||||||
int filec = 0;
|
int filec = 0;
|
||||||
|
|
@ -105,11 +130,12 @@ int main(int argc, char **argv){
|
||||||
|
|
||||||
fat_info info = {};
|
fat_info info = {};
|
||||||
|
|
||||||
|
if(argc == 1 || !strcmp(argv[1], "-h")){
|
||||||
printf("write files to virtual disk\n");
|
printf("write files to virtual disk\n");
|
||||||
if(argc == 1 || !strcmp(argv[1], "help")){
|
|
||||||
printf("Usage:\ndiskwrite <input files> -o <output file>");
|
printf("Usage:\ndiskwrite <input files> -o <output file>");
|
||||||
printf("\n-h: Help");
|
printf("\n-h: Help");
|
||||||
printf("\n-o: Specify output file\n");
|
printf("\n-o: Specify output file\n");
|
||||||
|
printf("-v: Verbose\n");
|
||||||
}
|
}
|
||||||
for(int i = 1; i < argc; i++){
|
for(int i = 1; i < argc; i++){
|
||||||
if(!strcmp(argv[i], "-o")){
|
if(!strcmp(argv[i], "-o")){
|
||||||
|
|
@ -117,18 +143,26 @@ int main(int argc, char **argv){
|
||||||
if(i >= argc){
|
if(i >= argc){
|
||||||
printf("Error: No output file specified!\n");
|
printf("Error: No output file specified!\n");
|
||||||
}
|
}
|
||||||
printf("OF: %s\n", argv[i]);
|
verbose && printf("OF: %s\n", argv[i]);
|
||||||
output_file = fopen(argv[i], "r+");
|
output_file = fopen(argv[i], "r+");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
if(!strcmp(argv[i], "-v")){
|
||||||
|
verbose = 1;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
files[filec++] = argv[i];
|
files[filec++] = argv[i];
|
||||||
printf("IF: %s\n", argv[i]);
|
verbose && printf("IF: %s\n", argv[i]);
|
||||||
}
|
}
|
||||||
if(filec == 0){
|
if(filec == 0){
|
||||||
printf("Nothing to do, no files to write\n");
|
printf("Nothing to do, no files to write\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
detect_repair_fs(output_file, &info);
|
detect_repair_fs(output_file, &info);
|
||||||
printf("Finished. Exiting...\n");
|
verbose && printf("Finished. Exiting...\n");
|
||||||
|
for(int i = 0; i < filec; i++){
|
||||||
|
write_file(files[i], &info, output_file);
|
||||||
|
}
|
||||||
|
fat_write_back(output_file, &info);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
Loading…
Reference in New Issue