WKern
Loading...
Searching...
No Matches
fat16_mnt.c
Go to the documentation of this file.
1/*
2WKern - A Bare Metal OS / Kernel I am maKing (For Fun)
3Copyright (C) 2025 Wdboyes13
4
5This program is free software: you can redistribute it and/or modify
6it under the terms of the GNU General Public License as published by
7the Free Software Foundation, either version 3 of the License, or
8any later version.
9
10This program is distributed in the hope that it will be useful,
11but WITHOUT ANY WARRANTY; without even the implied warranty of
12MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13GNU General Public License for more details.
14
15You should have received a copy of the GNU General Public License
16along with this program. If not, see <https://www.gnu.org/licenses/>.
17*/
18
19#include <fileio/fileio.h>
20#include <io/kio.h>
21
26static u8 fat16_sector[512];
27
38 // Use 32-bit total sectors if 16-bit is zero (fat16 magic)
39 u32 total_sectors = fat->total_sectors_16 != 0 ? fat->total_sectors_16
40 : fat->total_sectors_32;
41
42 // Root directory size in sectors (each entry is 32 bytes)
43 u32 root_dir_sectors =
44 ((fat->root_entry_count * 32) + (fat->bytes_per_sector - 1)) /
46
47 // Calculate data sectors by subtracting reserved, FAT tables, and root dir
48 u32 data_sectors = total_sectors - fat->reserved_sectors -
49 (fat->num_fats * fat->sectors_per_fat) -
50 root_dir_sectors;
51
52 // Total clusters in data region
53 return data_sectors / fat->sectors_per_cluster;
54}
55
67u32 Fat16Mount(u32 partition_lba) {
68 AtaReadSector(partition_lba, fat16_sector);
69
70 struct FaT16Bpb *bpb = (struct FaT16Bpb *)fat16_sector;
71
72 // Check boot sector signature 0xAA55
73 if (*(u16 *)&fat16_sector[510] != 0xAA55) {
74 Kprintf("Invalid FAT16 boot sector signature\n");
75 return 0;
76 }
77
78 // Load BPB values into fat16 struct
79 fat16.bytes_per_sector = bpb->bytes_per_sector;
80 fat16.sectors_per_cluster = bpb->sectors_per_cluster;
81 fat16.reserved_sectors = bpb->reserved_sectors;
82 fat16.num_fats = bpb->num_fats;
83 fat16.root_entry_count = bpb->root_entry_count;
84 fat16.sectors_per_fat = bpb->sectors_per_fat;
85
86 fat16.total_sectors_16 = bpb->total_sectors_short;
87 fat16.total_sectors_32 = bpb->total_sectors_long;
88
89 fat16.fat_start_lba = partition_lba + bpb->reserved_sectors;
90
91 // Calculate root directory sectors and their start LBA
92 u32 root_dir_sectors =
93 ((bpb->root_entry_count * 32) + (bpb->bytes_per_sector - 1)) /
95
96 fat16.root_dir_start_lba =
97 fat16.fat_start_lba + (bpb->num_fats * bpb->sectors_per_fat);
98 fat16.data_start_lba = fat16.root_dir_start_lba + root_dir_sectors;
99
100 fat16.total_cluster = Fat16TotalClusters(&fat16);
101
102 Kprintf("FAT16 Mount Success:\n");
103 Kprintf(" FAT LBA: %x\n", fat16.fat_start_lba);
104 Kprintf(" Root Dir LBA: %x\n", fat16.root_dir_start_lba);
105 Kprintf(" Data LBA: %x\n", fat16.data_start_lba);
106
107 return 1;
108}
void AtaReadSector(u32 lba, u8 *buffer)
Read a single 512-byte sector from the ATA device using LBA addressing.
Definition ATA.c:65
u32 Fat16TotalClusters(struct FaT16Info *fat)
Calculate the total number of data clusters in a FAT16 filesystem.
Definition fat16_mnt.c:37
struct FaT16Info fat16
Holds FAT16 filesystem info and a sector buffer.
Definition fat16_mnt.c:25
u32 Fat16Mount(u32 partition_lba)
Mounts the FAT16 filesystem from a given partition LBA.
Definition fat16_mnt.c:67
Definitions and declarations for FAT16 filesystem and ATA I/O.
void Kprintf(const char *fmt,...)
Formatted output to the screen.
Definition printer.c:152
unsigned int u32
32-Bit Unsigned Int
Definition nums.h:30
unsigned short u16
16-Bit Unsigned Int
Definition nums.h:36
unsigned char u8
8-Bit Unsigned Int
Definition nums.h:32
FAT16 BIOS Parameter Block (BPB) structure — describes volume layout.
Definition fileio.h:81
u16 reserved_sectors
Definition fileio.h:86
u8 num_fats
Definition fileio.h:87
u16 root_entry_count
Definition fileio.h:88
u16 total_sectors_short
Definition fileio.h:89
u16 sectors_per_fat
Definition fileio.h:92
u8 sectors_per_cluster
Definition fileio.h:85
u16 bytes_per_sector
Definition fileio.h:84
u32 total_sectors_long
Definition fileio.h:96
Parsed FAT16 filesystem layout and parameters.
Definition fileio.h:114
u32 total_sectors_32
Definition fileio.h:127
u16 total_sectors_16
Definition fileio.h:126
u16 sectors_per_fat
Definition fileio.h:123
u8 num_fats
Definition fileio.h:122
u16 reserved_sectors
Definition fileio.h:125
u16 bytes_per_sector
Definition fileio.h:120
u8 sectors_per_cluster
Definition fileio.h:121
u16 root_entry_count
Definition fileio.h:119