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
| // read MBR and mount partition
int sd_mount() {
// read MBR
char buf[BLOCK_SIZE];
readblock(0, buf);
// check boot signature
if (buf[510] != 0x55 || buf[511] != 0xAA) {
return -1;
}
// parse first partition only
struct mbr_partition p1;
memcpy(&p1, buf + 446, sizeof(struct mbr_partition));
// mount partition
readblock(p1.starting_sector, buf);
// route each filesystem
if (p1.partition_type == 0x0b) { // FAT32 with CHS addressing
// create FAT32's root directory object
char mountpoint[8] = "/sdp1";
vfs_mkdir(mountpoint);
vfs_mount("sdcard", mountpoint, "fat32");
// store metadata
struct fat32_boot_sector* boot_sector = (struct fat32_boot_sector*)buf;
fat32_metadata.data_region_blk_idx = p1.starting_sector +
boot_sector->n_sectors_per_fat_32 * boot_sector->n_file_alloc_tabs +
boot_sector->n_reserved_sectors;
fat32_metadata.fat_region_blk_idx = p1.starting_sector + boot_sector->n_reserved_sectors;
fat32_metadata.n_fat = boot_sector->n_file_alloc_tabs;
fat32_metadata.sector_per_fat = boot_sector->n_sectors_per_fat_32;
fat32_metadata.first_cluster = boot_sector->root_dir_start_cluster_num;
fat32_metadata.sector_per_cluster = boot_sector->logical_sector_per_cluster;
// get mount node
struct vnode* mount_dir;
char path_remain[128];
traversal(mountpoint, &mount_dir, path_remain);
// fill internal data of mount node
struct fat32_internal* root_internal = (struct fat32_internal*)kmalloc(sizeof(struct fat32_internal));
root_internal->first_cluster = boot_sector->root_dir_start_cluster_num;
mount_dir->internal = root_internal;
}
return 0;
}
|