My Project
Loading...
Searching...
No Matches
fat32.h
Go to the documentation of this file.
1/*
2 * PROJECT: MatanelOS Kernel
3 * LICENSE: NONE
4 * PURPOSE: FAT32 FileSystem Headers.
5 */
6
7#ifndef X86_KERNEL_FILESYSTEM_FAT32_HEADER
8#define X86_KERNEL_FILESYSTEM_FAT32_HEADER
9 // Standard headers, required.
10#include <stddef.h>
11#include <stdbool.h>
12#include <stdint.h>
14#include "../../mtstatus.h"
15
16#define END_OF_DIRECTORY 0x00
17#define DELETED_DIR_ENTRY 0xE5
18
19
20#define FAT32_FAT_MASK 0x0FFFFFFFU
21#define FAT32_FREE_CLUSTER 0x00000000U
22#define FAT32_BAD_CLUSTER 0x0FFFFFF7U
23#define FAT32_EOC_MIN 0x0FFFFFF8U /* inclusive */
24#define FAT32_EOC_MAX 0x0FFFFFFFU /* inclusive */
25
26
27
28#ifdef _MSC_VER
29#pragma pack(push, 1)
30typedef struct _FAT32_BPB {
31#else
32typedef struct __attribute__((packed)) _FAT32_BPB {
33#endif
34 uint8_t jump[3];
35 uint8_t oem[8];
36 uint16_t bytes_per_sector;
37 uint8_t sectors_per_cluster;
38 uint16_t reserved_sector_count;
39 uint8_t num_fats;
40 uint16_t root_entry_count;
41 uint16_t total_sectors_16;
42 uint8_t media;
43 uint16_t fat_size_16;
44 uint16_t sectors_per_track;
45 uint16_t num_heads;
46 uint32_t hidden_sectors;
47 uint32_t total_sectors_32;
48 uint32_t fat_size_32;
49 uint16_t ext_flags;
50 uint16_t fs_version;
51 uint32_t root_cluster;
52 uint16_t fs_info_sector;
53 uint16_t backup_root_sector;
55#ifdef _MSC_VER
56#pragma pack(pop)
57#endif
58
59#ifdef _MSC_VER
60#pragma pack(push, 1)
61typedef struct _FAT32_DIR_ENTRY {
62#else
63typedef struct __attribute__((packed)) _FAT32_DIR_ENTRY {
64#endif
65 char name[11];
66 uint8_t attr;
67 uint8_t nt_res;
68 uint8_t crt_time_tenth;
69 uint16_t crt_time;
70 uint16_t crt_date;
71 uint16_t lst_acc_date;
72 uint16_t fst_clus_hi;
73 uint16_t wrt_time;
74 uint16_t wrt_date;
75 uint16_t fst_clus_lo;
76 uint32_t file_size;
78#ifdef _MSC_VER
79#pragma pack(pop)
80#endif
81
82#pragma pack(push,1)
83typedef struct {
84 uint8_t LDIR_Ord;
85 uint16_t LDIR_Name1[5];
86 uint8_t LDIR_Attr; // always 0x0F
87 uint8_t LDIR_Type;
88 uint8_t LDIR_Chksum;
89 uint16_t LDIR_Name2[6];
91 uint16_t LDIR_Name3[2];
93#pragma pack(pop)
94
106
107// Initialize a FAT32 FileSystem on a given block device.
108MTSTATUS fat32_init(int disk_index);
109
110// List files in root dir
111void fat32_list_root(void);
112
113typedef enum _FAT32_ATTRIBUTES {
114 ATTR_READ_ONLY = 0x01, // File is read-only
115 ATTR_HIDDEN = 0x02, // File is hidden
116 ATTR_SYSTEM = 0x04, // System file
117 ATTR_VOLUME_ID = 0x08, // Volume label
118 ATTR_DIRECTORY = 0x10, // Entry is a directory
119 ATTR_ARCHIVE = 0x20, // File should be archived
120 // Special combination values
121 ATTR_LONG_NAME = 0x0F, // Long File Name entry (ATTR_READ_ONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUME_ID)
123
124static inline uint16_t fat32_encode_date(uint16_t year, uint8_t month, uint8_t day) {
125 // FAT date: YYYYYYYMMMMDDDDD (since 1980)
126 return ((year - 1980) << 9) | (month << 5) | (day);
127}
128
129static inline uint16_t fat32_encode_time(uint8_t hour, uint8_t min, uint8_t sec) {
130 // FAT time: HHHHHMMMMMMSSSSS (seconds / 2)
131 return (hour << 11) | (min << 5) | (sec / 2);
132}
133
134static inline void fat32_decode_date(uint16_t date, uint16_t* year, uint8_t* month, uint8_t* day) {
135 *year = 1980 + ((date >> 9) & 0x7F); // 7 bits for year
136 *month = (date >> 5) & 0x0F; // 4 bits for month
137 *day = date & 0x1F; // 5 bits for day
138}
139
140static inline void fat32_decode_time(uint16_t time, uint8_t* hour, uint8_t* min, uint8_t* sec) {
141 *hour = (time >> 11) & 0x1F; // 5 bits for hours
142 *min = (time >> 5) & 0x3F; // 6 bits for minutes
143 *sec = (time & 0x1F) * 2; // 5 bits for seconds (2-second resolution)
144}
145
153MTSTATUS fat32_read_file(const char* filename, uint32_t* file_size_out, void** buffer_out);
154
160MTSTATUS fat32_create_directory(const char* path);
161
170MTSTATUS fat32_write_file(const char* path, const void* data, uint32_t size, uint32_t file_modification_mode);
171
179MTSTATUS fat32_list_directory(const char* path, char* listings, size_t max_len);
180
186MTSTATUS fat32_delete_directory(const char* path);
187
193MTSTATUS fat32_delete_file(const char* path);
199bool fat32_directory_is_empty(const char* path);
200
201#endif
MTSTATUS fat32_init(int disk_index)
Definition fat32.c:745
MTSTATUS fat32_create_directory(const char *path)
Creates a new directory (/testdir/ or /testdir are both allowed to create 'testdir' inside of 'root')
Definition fat32.c:1047
MTSTATUS fat32_list_directory(const char *path, char *listings, size_t max_len)
Lists the directory given.
Definition fat32.c:1579
MTSTATUS fat32_delete_directory(const char *path)
This function deletes the directory given to the function from the system.
Definition fat32.c:1922
MTSTATUS fat32_read_file(const char *filename, uint32_t *file_size_out, void **buffer_out)
A FAT32 Function that reads the file requested into a dynamically allocated buffer.
Definition fat32.c:921
MTSTATUS fat32_write_file(const char *path, const void *data, uint32_t size, uint32_t mode)
Creates a new file and writes data to it.
Definition fat32.c:1275
MTSTATUS fat32_delete_file(const char *path)
This function deletes the file given to the function from the system.
Definition fat32.c:1954
void fat32_list_root(void)
Definition fat32.c:769
bool fat32_directory_is_empty(const char *path)
This function returns if the directory given to the function is empty (e.g, has only '....
Definition fat32.c:1663
FAT32_BPB
Definition fat32.h:54
_FAT32_ATTRIBUTES
Definition fat32.h:113
@ ATTR_SYSTEM
Definition fat32.h:116
@ ATTR_LONG_NAME
Definition fat32.h:121
@ ATTR_VOLUME_ID
Definition fat32.h:117
@ ATTR_READ_ONLY
Definition fat32.h:114
@ ATTR_DIRECTORY
Definition fat32.h:118
@ ATTR_HIDDEN
Definition fat32.h:115
@ ATTR_ARCHIVE
Definition fat32.h:119
enum _FAT32_ATTRIBUTES FAT32_ATTRIBUTES
struct _FAT32_FSINFO FAT32_FSINFO
FAT32_DIR_ENTRY
Definition fat32.h:77
typedef __attribute__
Definition fat32.h:63
int32_t MTSTATUS
Definition mtstatus.h:12
uint32_t sectors_per_cluster
Definition fat32.h:100
uint32_t fat_start
Definition fat32.h:101
uint32_t total_clusters
Definition fat32.h:104
uint32_t sectors_per_fat
Definition fat32.h:98
uint32_t total_sectors
Definition fat32.h:103
uint32_t first_data_sector
Definition fat32.h:96
uint16_t reserved_sector_count
Definition fat32.h:102
uint32_t root_cluster
Definition fat32.h:97
uint32_t bytes_per_sector
Definition fat32.h:99
uint8_t LDIR_Type
Definition fat32.h:87
uint16_t LDIR_FstClusLO
Definition fat32.h:90
uint8_t LDIR_Chksum
Definition fat32.h:88
uint8_t LDIR_Attr
Definition fat32.h:86
uint16_t LDIR_Name1[5]
Definition fat32.h:85
uint8_t LDIR_Ord
Definition fat32.h:84
uint16_t LDIR_Name2[6]
Definition fat32.h:89
uint16_t LDIR_Name3[2]
Definition fat32.h:91