This repository has been archived by the owner on May 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
read_root.c
87 lines (73 loc) · 2.19 KB
/
read_root.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
#include <stdio.h>
#include <stdlib.h>
typedef struct {
unsigned char first_byte;
unsigned char start_chs[3];
unsigned char partition_type;
unsigned char end_chs[3];
unsigned short starting_cluster;
unsigned int file_size;
} __attribute((packed)) PartitionTable;
typedef struct {
unsigned char jmp[3];
char oem[8];
unsigned short sector_size;
// {...} COMPLETAR
char volume_id[4];
char volume_label[11];
char fs_type[8];
char boot_code[448];
unsigned short boot_sector_signature;
} __attribute((packed)) Fat12BootSector;
typedef struct {
// {...} COMPLETAR
} __attribute((packed)) Fat12Entry;
void print_file_info(Fat12Entry *entry) {
switch(entry->filename[0]) {
case 0x00:
return; // unused entry
case 0xE5:
printf("Archivo borrado: [?%.7s.%.3s]\n", // COMPLETAR
return;
case 0x05:
printf("Archivo que comienza con 0xE5: [%c%.7s.%.3s]\n", 0xE5, // COMPLETAR
break;
case 0x2E:
printf("Directorio: [%.8s.%.3s]\n", // COMPLETAR
break;
default:
printf("Archivo: [%.8s.%.3s]\n", // COMPLETAR
}
}
int main() {
FILE * in = fopen("test.img", "rb");
int i;
PartitionTable pt[4];
Fat12BootSector bs;
Fat12Entry entry;
//{...} Completar
for(i=0; i<4; i++) {
if(pt[i].partition_type == 1) {
printf("Encontrada particion FAT12 %d\n", i);
break;
}
}
if(i == 4) {
printf("No encontrado filesystem FAT12, saliendo...\n");
return -1;
}
fseek(in, 0, SEEK_SET);
//{...} Leo boot sector
printf("En 0x%X, sector size %d, FAT size %d sectors, %d FATs\n\n",
ftell(in), bs.sector_size, bs.fat_size_sectors, bs.number_of_fats);
fseek(in, (bs.reserved_sectors-1 + bs.fat_size_sectors * bs.number_of_fats) *
bs.sector_size, SEEK_CUR);
printf("Root dir_entries %d \n", bs.root_dir_entries);
for(i=0; i<bs.root_dir_entries; i++) {
fread(&entry, sizeof(entry), 1, in);
print_file_info(&entry);
}
printf("\nLeido Root directory, ahora en 0x%X\n", ftell(in));
fclose(in);
return 0;
}