|
| 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 |
0 commit comments