-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
132 lines (120 loc) · 4.3 KB
/
main.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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "structs.h"
typedef struct {
uint8_t first_byte;
uint8_t start_chs[3];
uint8_t partition_type;
uint8_t end_chs[3];
uint32_t start_sector;
uint32_t length_sectors;
} __attribute((packed)) partition_table_t;
fat_desc_t* fat_init(FILE* in, unsigned int offset) {
fat_desc_t* fat = (fat_desc_t*) malloc(sizeof(fat_desc_t));
assert(fat);
memset(fat, 0, sizeof(fat_desc_t));
fat->disk_start = offset;
fat->in = in;
fseek(in, offset, SEEK_SET);
bootsector_t* bs = (bootsector_t*) malloc(sizeof(bootsector_t));
memset(bs, 0, sizeof(bootsector_t));
if (fread(bs, sizeof(bootsector_t), 1, in) != 1) {
fprintf(stderr, "Error: reading bootsector failed\n");
exit(1);
}
fat->bs = bs;
fat->sectors_per_fat = bs->fat_size;
fat->sector_size = bs->sector_size;
fat->hidden_sectors = bs->hidden_sectors;
fat->reserved_sectors = bs->reserved_sectors;
fat->clust_sz = bs->clust_sz;
fat->root_entries = bs->root_entries;
fat->fat_cnt = bs->fat_cnt;
fat->sectors_for_root = (fat->root_entries * 32) / fat->sector_size;
fat->reserved_sectors = fat->reserved_sectors;
fat->rootdir_offset = fat->reserved_sectors + fat->fat_cnt * fat->sectors_per_fat;
fat->cluster_offset = fat->rootdir_offset + fat->sectors_for_root;
return fat;
}
void free_fat(fat_desc_t* fat) {
free(fat->bs);
free(fat);
}
void print_filesystem_info(fat_desc_t* fat) {
printf("Label: %.11s\n", fat->bs->volume_label);
printf("Volume id: 0x%x\n", fat->bs->volume_id);
printf("OEM data: %.8s\n", fat->bs->oem_data);
printf("Sector size: %d\n", (int) fat->sector_size);
printf("Cluster size (sectors): %d\n", fat->clust_sz);
printf("Root entries: %d\n", fat->root_entries);
printf("Number of FATs: %d\n", fat->bs->fat_cnt);
printf("Sectors per fat: %d\n", fat->sectors_per_fat);
printf("Reserved sectors: %d\n", fat->reserved_sectors);
printf("Hidden sectors: %d\n", fat->hidden_sectors);
printf("Fat offset in sectors: %d\n", fat->reserved_sectors);
printf("Root directory offset in sectors: %d\n", fat->rootdir_offset);
printf("First cluster offset in sectors: %d\n", fat->cluster_offset);
}
int main(int argc, char *argv[]) {
if (argc != 2) {
fprintf(stderr, "Usage: fatcat <input file>\n");
exit(1);
}
FILE* in = fopen(argv[1], "rb");
if (in == NULL) {
fprintf(stderr, "Error: unable to open file (check permissions)\n");
exit(1);
}
int i;
partition_table_t pt[4];
fseek(in, 0x1BE, SEEK_SET); // go to partition table start
size_t ret = fread(pt, sizeof(partition_table_t), 4, in); // read 4 entries
if (ret != 4) {
fprintf(stderr, "Error: failed to read partition table\n");
exit(1);
}
for (i = 0; i < 4; i++) {
printf("Partition %d, type %02X", i, pt[i].partition_type);
switch (pt[i].partition_type) {
case 0x0:
printf(" (empty)");
break;
case 0x01:
printf(" (FAT12)");
break;
case 0x04:
case 0x06:
printf(" (FAT16)");
break;
case 0x11:
printf (" (FAT32)");
break;
}
printf("\n Start sector %08X, %d sectors long\n===========\n",
pt[i].start_sector, pt[i].length_sectors);
if (pt[i].partition_type != 0x01 && pt[i].partition_type != 0x06)
continue; // not FAT
fat_desc_t* fat = fat_init(in, 512 * pt[i].start_sector);
print_filesystem_info(fat);
puts("----------------------------\n");
dir_t* root = process_root(fat, fat->rootdir_offset);
if (pt[i].partition_type != 0x01) {
free_dir(root);
free_fat(fat);
puts("Skipping content (FAT16 is not supported)...\n");
continue;
}
puts("<-- BEGIN CONTENT -->\n");
for (i = 0; i < (int) root->subdir_cnt; i++)
print_dir(root->subdirs[i]);
for (i = 0; i < (int) root->file_cnt; i++)
print_file(root->files[i]);
puts("<-- END OF FAT FILESYSTEM -->\n");
free_dir(root);
free_fat(fat);
}
fclose(in);
return 0;
}