Skip to content

Commit 0715782

Browse files
Added FAT32 (does not work yet)
1 parent ba97679 commit 0715782

File tree

14 files changed

+1261
-88
lines changed

14 files changed

+1261
-88
lines changed

disk-fat.img

64 MB
Binary file not shown.

disk.img

0 Bytes
Binary file not shown.

locc

21.7 KB
Binary file not shown.

source/includes/filesystems/fat.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* @file fat.h
3+
* @author Pradosh ([email protected])
4+
* @brief The general header which applies to any FAT file system.
5+
* @version 0.1
6+
* @date 2026-01-07
7+
*
8+
* @copyright Copyright (c) Pradosh 2026
9+
*
10+
*/
11+
12+
#ifndef FAT_H
13+
#define FAT_H
14+
15+
typedef enum {
16+
FAT_OK = 0,
17+
18+
// generic
19+
FAT_ERR_IO,
20+
FAT_ERR_INVALID,
21+
FAT_ERR_NOT_FOUND,
22+
FAT_ERR_EXISTS,
23+
FAT_ERR_NOT_DIR,
24+
FAT_ERR_IS_DIR,
25+
FAT_ERR_NO_SPACE,
26+
FAT_ERR_NAME_INVALID,
27+
FAT_ERR_NOT_EMPTY,
28+
FAT_ERR_READ,
29+
30+
// filesystem corruption (meltdown-worthy)
31+
FAT_ERR_CORRUPT,
32+
FAT_ERR_FAT_LOOP,
33+
FAT_ERR_BAD_CLUSTER,
34+
} fat_err_t;
35+
36+
#endif

source/includes/filesystems/fat16.h

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <basics.h>
1616
#include <graphics.h>
1717
#include <ahci.h>
18+
#include <filesystems/fat.h>
1819

1920
#define FAT16_EOC 0xFFF8
2021
#define FAT16_ROOT_CLUSTER 0
@@ -79,26 +80,6 @@ typedef struct {
7980
uint16_t cluster;
8081
} fat16_file_t;
8182

82-
typedef enum {
83-
FAT_OK = 0,
84-
85-
// generic
86-
FAT_ERR_IO,
87-
FAT_ERR_INVALID,
88-
FAT_ERR_NOT_FOUND,
89-
FAT_ERR_EXISTS,
90-
FAT_ERR_NOT_DIR,
91-
FAT_ERR_IS_DIR,
92-
FAT_ERR_NO_SPACE,
93-
FAT_ERR_NAME_INVALID,
94-
FAT_ERR_NOT_EMPTY,
95-
96-
// filesystem corruption (meltdown-worthy)
97-
FAT_ERR_CORRUPT,
98-
FAT_ERR_FAT_LOOP,
99-
FAT_ERR_BAD_CLUSTER,
100-
} fat_err_t;
101-
10283
partition_fs_type_t detect_fat_type_enum(const int8* buf);
10384
int fat16_mount(int portno, uint32_t partition_lba, fat16_fs_t* fs) ;
10485
uint16_t fat16_read_fat_fs(fat16_fs_t* fs, uint16_t cluster);
Lines changed: 184 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
1+
/**
2+
* @file fat32.h
3+
* @author Pradosh ([email protected])
4+
* @brief The implementation of FAT32 FS for FW
5+
* @version 0.1
6+
* @date 2026-01-07
7+
*
8+
* @copyright Copyright (c) Pradosh 2026
9+
*
10+
*/
11+
#ifndef FAT32_H
12+
#define FAT32_H
13+
#include <basics.h>
14+
#include <filesystems/fat.h>
15+
16+
/* ============================= */
17+
/* FAT32 CONSTANTS & MACROS */
18+
/* ============================= */
19+
20+
#define FAT32_SECTOR_SIZE 512
21+
#define FAT32_MAX_LFN_CHARS 255
22+
23+
/* FAT entry values */
24+
#define FAT32_CLUSTER_FREE 0x00000000
25+
#define FAT32_CLUSTER_BAD 0x0FFFFFF7
26+
#define FAT32_CLUSTER_EOC 0x0FFFFFF8
27+
28+
/* Directory entry attributes */
29+
#define FAT_ATTR_READ_ONLY 0x01
30+
#define FAT_ATTR_HIDDEN 0x02
31+
#define FAT_ATTR_SYSTEM 0x04
32+
#define FAT_ATTR_VOLUME_ID 0x08
33+
#define FAT_ATTR_DIRECTORY 0x10
34+
#define FAT_ATTR_ARCHIVE 0x20
35+
#define FAT_ATTR_LFN 0x0F
36+
37+
/* ============================= */
38+
/* BIOS PARAMETER BLOCK (BPB) */
39+
/* ============================= */
40+
41+
typedef struct __attribute__((packed)) {
42+
uint8_t jump[3];
43+
uint8_t oem_name[8];
44+
45+
uint16_t bytes_per_sector;
46+
uint8_t sectors_per_cluster;
47+
uint16_t reserved_sectors;
48+
uint8_t fat_count;
49+
uint16_t root_entry_count; /* Must be 0 for FAT32 */
50+
uint16_t total_sectors_16;
51+
uint8_t media_type;
52+
uint16_t fat_size_16; /* Must be 0 for FAT32 */
53+
uint16_t sectors_per_track;
54+
uint16_t head_count;
55+
uint32_t hidden_sectors;
56+
uint32_t total_sectors_32;
57+
58+
/* FAT32 extended BPB */
59+
uint32_t fat_size_32;
60+
uint16_t ext_flags;
61+
uint16_t fs_version;
62+
uint32_t root_cluster;
63+
uint16_t fs_info_sector;
64+
uint16_t backup_boot_sector;
65+
uint8_t reserved[12];
66+
67+
uint8_t drive_number;
68+
uint8_t reserved1;
69+
uint8_t boot_signature;
70+
uint32_t volume_id;
71+
uint8_t volume_label[11];
72+
uint8_t fs_type[8];
73+
} fat32_bpb_t;
74+
75+
/* ============================= */
76+
/* FAT32 DIRECTORY ENTRY */
77+
/* ============================= */
78+
79+
typedef struct __attribute__((packed)) {
80+
uint8_t name[11];
81+
uint8_t attr;
82+
uint8_t nt_reserved;
83+
uint8_t creation_time_tenths;
84+
uint16_t creation_time;
85+
uint16_t creation_date;
86+
uint16_t access_date;
87+
uint16_t first_cluster_high;
88+
uint16_t write_time;
89+
uint16_t write_date;
90+
uint16_t first_cluster_low;
91+
uint32_t file_size;
92+
} fat32_dir_entry_t;
93+
94+
/* ============================= */
95+
/* LONG FILE NAME ENTRY */
96+
/* ============================= */
97+
98+
typedef struct __attribute__((packed)) {
99+
uint8_t order;
100+
uint16_t name1[5];
101+
uint8_t attr; /* Always 0x0F */
102+
uint8_t type; /* Always 0 */
103+
uint8_t checksum;
104+
uint16_t name2[6];
105+
uint16_t zero; /* Always 0 */
106+
uint16_t name3[2];
107+
} fat32_lfn_entry_t;
108+
109+
/* ============================= */
110+
/* FAT32 FS CONTEXT */
111+
/* ============================= */
112+
113+
typedef struct {
114+
fat32_bpb_t bpb;
115+
116+
int portno;
117+
uint32_t partition_lba;
118+
119+
uint32_t fat_start_lba;
120+
uint32_t data_start_lba;
121+
uint32_t sectors_per_cluster;
122+
123+
uint32_t total_clusters;
124+
uint32_t root_cluster;
125+
} fat32_fs_t;
126+
127+
/* ============================= */
128+
/* FAT32 FILE HANDLE */
129+
/* ============================= */
130+
131+
typedef struct {
132+
fat32_fs_t* fs; // mounted FS
133+
fat32_dir_entry_t entry;
134+
uint32_t start_cluster;
135+
uint32_t current_cluster;
136+
uint32_t pos;
137+
uint32_t size;
138+
139+
uint32_t parent_cluster;
140+
uint8_t is_dir;
141+
} fat32_file_t;
142+
143+
int fat32_find_path(fat32_fs_t* fs, const char* path, fat32_dir_entry_t* out);
144+
void fat32_list_root(fat32_fs_t* fs);
145+
void fat32_list_dir_cluster(fat32_fs_t* fs, uint32_t cluster);
146+
147+
int fat32_create_path(fat32_fs_t* fs, const char* path, fat32_dir_entry_t* out);
148+
int fat32_truncate(fat32_file_t* file, uint32_t new_size);
149+
150+
uint32_t fat32_alloc_cluster(fat32_fs_t* fs);
151+
const char* fat_next_path_component(const char* path, char* out);
152+
153+
void fat32_free_chain_from(
154+
fat32_fs_t* fs,
155+
uint32_t start,
156+
uint32_t keep);
157+
158+
void fat32_extend_chain(
159+
fat32_fs_t* fs,
160+
uint32_t start,
161+
uint32_t count);
162+
163+
uint32_t fat32_clusters_for_size(
164+
fat32_fs_t* fs,
165+
uint32_t size);
166+
167+
void fat32_update_entry(
168+
fat32_fs_t* fs,
169+
fat32_dir_entry_t* entry);
170+
171+
int fat32_create_entry(
172+
fat32_fs_t* fs,
173+
uint32_t dir_cluster,
174+
const char* name,
175+
uint8_t attr,
176+
fat32_dir_entry_t* out);
177+
178+
int fat32_unlink_path(fat32_fs_t* fs, const char* path);
179+
int fat32_rm_recursive(fat32_fs_t* fs, const char* path);
180+
int fat32_mv(fat32_fs_t* fs, const char* src, const char* dst);
181+
int fat32_delete_entry(fat32_fs_t* fs, uint32_t dir_cluster, const char* name);
182+
int fat32_rmdir(fat32_fs_t* fs, fat32_dir_entry_t* entry);
183+
184+
#endif

source/includes/filesystems/vfs.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,14 @@
1414
#include <graphics.h>
1515
#include <ahci.h>
1616
#include <filesystems/fat16.h>
17+
#include <filesystems/fat32.h>
1718

1819
typedef struct {
1920
mount_entry_t* mnt;
20-
fat16_file_t f;
21+
union {
22+
fat16_file_t fat16;
23+
fat32_file_t fat32;
24+
} f;
2125
uint32_t pos; // for virtual files only, must not be used for real fs
2226
int flags;
2327

source/kernel/C/disk/gpt.c

Lines changed: 0 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -122,39 +122,6 @@ void parse_gpt_partitions(int portno, struct GPT_PartTableHeader* hdr) {
122122
);
123123

124124
printf("[AHCI/GPT] identified disk %s", part_name);
125-
if (fs_type == FS_FAT16) {
126-
// fat16_fs_t fs;
127-
// fat16_dir_entry_t file;
128-
129-
// //
130-
// fat16_mount(portno, start, &fs);
131-
132-
// fat16_create(&fs, 0, "FWLOGS.TXT", 0x20);
133-
// fat16_file_t f;
134-
// fat16_open(&fs, "/FWLOGS.TXT", &f);
135-
// const char msg[] = "HELLO FAT16\n";
136-
// fat16_write(&f, (const uint8_t*)msg, sizeof(msg));
137-
138-
// // rewind
139-
// f.pos = 0;
140-
// f.cluster = f.entry.first_cluster;
141-
142-
// uint8_t buf[64];
143-
// fat16_read(&f, buf, sizeof(buf));
144-
// for(int k=0;k<64;k++)
145-
// printfnoln("%c", buf[k]);
146-
147-
// fat16_create_path(&fs, "/a/b/c/text.txt", 0x20); // 0x20 = archive
148-
149-
// fat16_file_t fnew;
150-
// if (fat16_open(&fs, "/a/b/c/text.txt", &fnew) == 0) {
151-
// const char msg1[] = "HELLO FAT16 PATH!\n";
152-
// fat16_write(&fnew, (const uint8_t*)msg1, sizeof(msg1)-1); // -1 to skip null terminator
153-
// fat16_close(&fnew);
154-
// }
155-
156-
// print("\n");
157-
}
158125
}
159126

160127
gpt_disks_count++;

source/kernel/C/disk/mbr.c

Lines changed: 0 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,22 +85,6 @@ void parse_mbr_partitions(int8* mbr, int portno){
8585
partitions[i].partition_type,
8686
null
8787
);
88-
89-
if(fs_type == FS_FAT16){
90-
// fat16_fs_t fs;
91-
// fat16_dir_entry_t file;
92-
93-
// fat16_mount(portno, partitions[i].lba_start, &fs);
94-
// fat16_list_root(&fs);
95-
96-
// fat16_file_t f;
97-
98-
// if (fat16_open(&fs, "/HELLO.TXT", &f) == 0) {
99-
// const char* msg = "HELLO FROM FROSTWING\n";
100-
// fat16_write(&f, (const uint8_t*)msg, strlen(msg));
101-
// fat16_close(&f);
102-
// }
103-
}
10488
}
10589

10690
mbr_disks_count++;

0 commit comments

Comments
 (0)