-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdisk.c
68 lines (55 loc) · 2.43 KB
/
disk.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
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <unistd.h>
#include "disk.h"
#include "utils.h"
void display_fsinfo(BootEntry *disk_info) {
printf("Number of FATs = %d\n", disk_info->BPB_NumFATs);
printf("Number of bytes per sector = %d\n", disk_info->BPB_BytsPerSec);
printf("Number of sectors per cluster = %d\n", disk_info->BPB_SecPerClus);
printf("Number of reserved sectors = %d\n", disk_info->BPB_RsvdSecCnt);
}
unsigned int cluster_to_sector(BootEntry *disk_info, unsigned int cluster) {
return disk_info->BPB_RsvdSecCnt + disk_info->BPB_NumFATs * disk_info->BPB_FATSz32 + (cluster - 2) * disk_info->BPB_SecPerClus;
}
unsigned int cluster_to_bytes(BootEntry *disk_info, unsigned int cluster) {
return cluster_to_sector(disk_info, cluster) * disk_info->BPB_BytsPerSec;
}
unsigned int *read_fat(unsigned char *disk, BootEntry *disk_info, unsigned int cluster, int nfat) {
return (unsigned int *) (disk + (disk_info->BPB_RsvdSecCnt + disk_info->BPB_FATSz32 * (nfat - 1)) * disk_info->BPB_BytsPerSec) + cluster;
}
DirEntry *read_directory(unsigned char *disk, BootEntry *disk_info, unsigned int cluster) {
return (DirEntry *) (disk + cluster_to_bytes(disk_info, cluster));
}
unsigned char *read_cluster(unsigned char *disk, BootEntry *disk_info, unsigned int cluster) {
return disk + cluster_to_bytes(disk_info, cluster);
}
unsigned char *map_disk(char *disk_name, int *disk_size, char mode) {
int fd = open(disk_name, mode == 'r' ? O_RDONLY : O_RDWR);
if (fd < 0) {
fprintf(stderr, "Error opening disk\n");
return NULL;
}
*disk_size = get_file_size(fd);
if (*disk_size < 0)
return NULL;
unsigned char *disk = mmap(NULL, *disk_size, mode == 'r' ? PROT_READ : PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (disk == MAP_FAILED) {
fprintf(stderr, "Error reading disk\n");
return NULL;
}
close(fd);
return disk;
}
void unmap_disk(unsigned char *disk, int disk_size) {
munmap(disk, disk_size);
}
void update_disk(unsigned char *disk, BootEntry *disk_info, unsigned int root_cluster, unsigned int cluster_offset, unsigned char data) {
disk[cluster_to_bytes(disk_info, root_cluster) + cluster_offset] = data;
}
void update_fat(unsigned char *disk, BootEntry *disk_info, unsigned int cluster, unsigned int value) {
for(int i = 1; i <= disk_info->BPB_NumFATs; i++) {
*read_fat(disk, disk_info, cluster, i) = value;
}
}