| 1 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 2 | // File : fat32.h |
|---|
| 3 | // Date : 01/09/2013 |
|---|
| 4 | // Author : Alain Greiner / Marco Jankovic |
|---|
| 5 | // Copyright (c) UPMC-LIP6 |
|---|
| 6 | //////////////////////////////////////////////////////////////////////////////// |
|---|
| 7 | |
|---|
| 8 | #ifndef _FAT32_H |
|---|
| 9 | #define _FAT32_H |
|---|
| 10 | |
|---|
| 11 | #include "fat32_shared.h" |
|---|
| 12 | #include "giet_config.h" |
|---|
| 13 | #include "kernel_locks.h" |
|---|
| 14 | #include "mapping_info.h" |
|---|
| 15 | |
|---|
| 16 | /*************** Partition Boot Sector Format **********************************/ |
|---|
| 17 | // offset | length |
|---|
| 18 | #define BS_JMPBOOT 0 , 3 |
|---|
| 19 | #define BS_OEMNAME 3 , 8 |
|---|
| 20 | #define BPB_BYTSPERSEC 11 , 2 |
|---|
| 21 | #define BPB_SECPERCLUS 13 , 1 |
|---|
| 22 | #define BPB_RSVDSECCNT 14 , 2 |
|---|
| 23 | #define BPB_NUMFATS 16 , 1 |
|---|
| 24 | #define BPB_ROOTENTCNT 17 , 2 |
|---|
| 25 | #define BPB_TOTSEC16 19 , 2 |
|---|
| 26 | #define BPB_MEDIA 21 , 1 |
|---|
| 27 | #define BPB_FATSZ16 22 , 2 |
|---|
| 28 | #define BPB_SECPERTRK 24 , 2 |
|---|
| 29 | #define BPB_NUMHEADS 26 , 2 |
|---|
| 30 | #define BPB_HIDDSEC 28 , 4 |
|---|
| 31 | #define BPB_TOTSEC32 32 , 4 |
|---|
| 32 | #define BPB_PARTITION_TABLE 446 , 64 |
|---|
| 33 | |
|---|
| 34 | // FAT 32 |
|---|
| 35 | #define BPB_FAT32_FATSZ32 36 , 4 |
|---|
| 36 | #define BPB_FAT32_EXTFLAGS 40 , 2 |
|---|
| 37 | #define BPB_FAT32_FSVER 42 , 2 |
|---|
| 38 | #define BPB_FAT32_ROOTCLUS 44 , 4 |
|---|
| 39 | #define BPB_FAT32_FSINFO 48 , 2 |
|---|
| 40 | #define BPB_FAT32_BKBOOTSEC 50 , 2 |
|---|
| 41 | #define BS_FAT32_DRVNUM 64 , 1 |
|---|
| 42 | #define BS_FAT32_BOOTSIG 66 , 1 |
|---|
| 43 | #define BS_FAT32_VOLID 67 , 4 |
|---|
| 44 | #define BS_FAT32_VOLLAB 71 , 11 |
|---|
| 45 | #define BS_FAT32_FILSYSTYPE 82 , 8 |
|---|
| 46 | |
|---|
| 47 | // Partitions |
|---|
| 48 | #define FIRST_PARTITION_ACTIVE 446 , 8 |
|---|
| 49 | #define FIRST_PARTITION_BEGIN_LBA 454 , 4 |
|---|
| 50 | #define FIRST_PARTITION_SIZE 458 , 4 |
|---|
| 51 | #define SECOND_PARTITION_ACTIVE 462 , 8 |
|---|
| 52 | #define SECOND_PARTITION_BEGIN_LBA 470 , 4 |
|---|
| 53 | #define SECOND_PARTITION_SIZE 474 , 4 |
|---|
| 54 | #define THIRD_PARTITION_ACTIVE 478 , 8 |
|---|
| 55 | #define THIRD_PARTITION_BEGIN_LBA 486 , 4 |
|---|
| 56 | #define THIRD_PARTITION_SIZE 490 , 4 |
|---|
| 57 | #define FOURTH_PARTITION_ACTIVE 494 , 8 |
|---|
| 58 | #define FOURTH_PARTITION_BEGIN_LBA 502 , 4 |
|---|
| 59 | #define FOURTH_PARTITION_SIZE 506 , 4 |
|---|
| 60 | /*******************************************************************************/ |
|---|
| 61 | |
|---|
| 62 | #define MBR_SIGNATURE_POSITION 510 , 2 |
|---|
| 63 | #define MBR_SIGNATURE_VALUE 0xAA55 |
|---|
| 64 | |
|---|
| 65 | /************** FAT_FS_INFO SECTOR ********************************************/ |
|---|
| 66 | #define FS_SIGNATURE_VALUE_1 0x52526141 |
|---|
| 67 | #define FS_SIGNATURE_VALUE_2 0x72724161 |
|---|
| 68 | #define FS_SIGNATURE_VALUE_3 0x000055AA |
|---|
| 69 | #define FS_SIGNATURE_POSITION_1 0 , 4 |
|---|
| 70 | #define FS_SIGNATURE_POSITION_2 484 , 4 |
|---|
| 71 | #define FS_SIGNATURE_POSITION_3 508 , 4 |
|---|
| 72 | #define FS_FREE_CLUSTERS 488 , 4 |
|---|
| 73 | #define FS_FREE_CLUSTER_HINT 492 , 4 |
|---|
| 74 | /*******************************************************************************/ |
|---|
| 75 | |
|---|
| 76 | #define DIR_ENTRY_SIZE 32 |
|---|
| 77 | |
|---|
| 78 | #define NAME_MAX_SIZE 31 |
|---|
| 79 | |
|---|
| 80 | /******* Directory Entry Structure (32 bytes) **********************************/ |
|---|
| 81 | // offset | length |
|---|
| 82 | #define DIR_NAME 0 , 11 // dir_entry name |
|---|
| 83 | #define DIR_ATTR 11 , 1 // attributes |
|---|
| 84 | #define DIR_NTRES 12 , 1 // reserved for the OS |
|---|
| 85 | #define DIR_CRT_TIMES_TENTH 13 , 1 |
|---|
| 86 | #define DIR_FST_CLUS_HI 20 , 2 // cluster index 16 MSB bits |
|---|
| 87 | #define DIR_WRT_TIME 22 , 2 // time of last write |
|---|
| 88 | #define DIR_WRT_DATE 24 , 2 // date of last write |
|---|
| 89 | #define DIR_FST_CLUS_LO 26 , 2 // cluster index 16 LSB bit |
|---|
| 90 | #define DIR_FILE_SIZE 28 , 4 // dir_entry size (up to 4 Gbytes) |
|---|
| 91 | /*******************************************************************************/ |
|---|
| 92 | |
|---|
| 93 | /******* LFN Directory Entry Structure (32 bytes) *****************************/ |
|---|
| 94 | // offset | length |
|---|
| 95 | #define LDIR_ORD 0 , 1 // Sequence number (from 0x01 to 0x0f) |
|---|
| 96 | #define LDIR_NAME_1 1 , 10 // name broken into 3 parts |
|---|
| 97 | #define LDIR_ATTR 11 , 1 // attributes (must be 0x0F) |
|---|
| 98 | #define LDIR_TYPE 12 , 1 // directory type (must be 0x00) |
|---|
| 99 | #define LDIR_CHKSUM 13 , 1 // checksum of name in short dir |
|---|
| 100 | #define LDIR_NAME_2 14 , 12 |
|---|
| 101 | #define LDIR_RSVD 26 , 2 // artifact of previous fat (must be 0) |
|---|
| 102 | #define LDIR_NAME_3 28 , 4 |
|---|
| 103 | /*******************************************************************************/ |
|---|
| 104 | |
|---|
| 105 | /*********************** DIR_ATTR values (attributes) ************************/ |
|---|
| 106 | #define ATTR_READ_ONLY 0x01 |
|---|
| 107 | #define ATTR_HIDDEN 0x02 |
|---|
| 108 | #define ATTR_SYSTEM 0x04 |
|---|
| 109 | #define ATTR_VOLUME_ID 0x08 |
|---|
| 110 | #define ATTR_DIRECTORY 0x10 |
|---|
| 111 | #define ATTR_ARCHIVE 0x20 |
|---|
| 112 | #define ATTR_LONG_NAME_MASK 0x0f // READ_ONLY|HIDDEN|SYSTEM|VOLUME_ID |
|---|
| 113 | /*******************************************************************************/ |
|---|
| 114 | |
|---|
| 115 | /********************* DIR_ORD special values **********************************/ |
|---|
| 116 | #define FREE_ENTRY 0xE5 // this entry is free in the directory |
|---|
| 117 | #define NO_MORE_ENTRY 0x00 // no more entry in the directory |
|---|
| 118 | /*******************************************************************************/ |
|---|
| 119 | |
|---|
| 120 | /******************** CLuster Index Special Values *****************************/ |
|---|
| 121 | #define FREE_CLUSTER 0x00000000 |
|---|
| 122 | #define RESERVED_CLUSTER 0x00000001 |
|---|
| 123 | #define BAD_CLUSTER 0x0FFFFFF7 |
|---|
| 124 | #define END_OF_CHAIN_CLUSTER_MIN 0x0ffffff8 |
|---|
| 125 | #define END_OF_CHAIN_CLUSTER_MAX 0x0fffffff |
|---|
| 126 | /*******************************************************************************/ |
|---|
| 127 | |
|---|
| 128 | #define FAT_INITIALIZED 0xBABEF00D |
|---|
| 129 | #define FAT_PADDR_MODE 0x00000001 // mask for physical address mode |
|---|
| 130 | #define FAT_FORCED_OFFSET 0X00000002 // mask for forced offset mode |
|---|
| 131 | |
|---|
| 132 | /******************************************************************************** |
|---|
| 133 | This struct defines a non terminal node in a 64-tree implementing a File-Cache |
|---|
| 134 | associated to an open file, or the Fat-Cache, associated to the FAT itself. |
|---|
| 135 | Each node has 64 children, and we use the void* type, because the children |
|---|
| 136 | can be non-terminal (fat_cache_node_t) or terminal (fat_cache_cluster_t). |
|---|
| 137 | ********************************************************************************/ |
|---|
| 138 | |
|---|
| 139 | typedef struct fat_cache_node_s |
|---|
| 140 | { |
|---|
| 141 | void* children[64]; // pointers on the 64 children |
|---|
| 142 | } fat_cache_node_t; |
|---|
| 143 | |
|---|
| 144 | |
|---|
| 145 | /******************************************************************************** |
|---|
| 146 | This struct defines a buffer descriptor, that is a leaf cell in a 64-tree. |
|---|
| 147 | Each buffer descriptor contains a pointer on a 4K bytes buffer, and the |
|---|
| 148 | lba on block device. |
|---|
| 149 | ********************************************************************************/ |
|---|
| 150 | |
|---|
| 151 | typedef struct fat_cache_desc_s |
|---|
| 152 | { |
|---|
| 153 | unsigned int lba; // cluster lba on block device |
|---|
| 154 | unsigned int dirty; // modified if non zero |
|---|
| 155 | unsigned char* buffer; // pointer on the 4 Kbytes buffer |
|---|
| 156 | } fat_cache_desc_t; |
|---|
| 157 | |
|---|
| 158 | |
|---|
| 159 | /******************************************************************************** |
|---|
| 160 | This struct defines a file/directory inode / size = 64 bytes |
|---|
| 161 | ********************************************************************************/ |
|---|
| 162 | |
|---|
| 163 | typedef struct fat_inode_s |
|---|
| 164 | { |
|---|
| 165 | struct fat_inode_s* parent; // parent directory inode |
|---|
| 166 | struct fat_inode_s* next; // next inode in same directory |
|---|
| 167 | struct fat_inode_s* child; // first children inode (dir only) |
|---|
| 168 | fat_cache_node_t* cache; // pointer on the file_cache root |
|---|
| 169 | unsigned int cluster; // first cluster index in FAT |
|---|
| 170 | unsigned int size; // number of bytes |
|---|
| 171 | unsigned int count; // reference count / 0 if dir |
|---|
| 172 | unsigned short dentry; // NORMAL dir_entry index in parent |
|---|
| 173 | unsigned char levels; // number of levels in file_cache |
|---|
| 174 | unsigned char is_dir; // directory if non zero |
|---|
| 175 | char name[32]; // file directory name |
|---|
| 176 | } fat_inode_t; |
|---|
| 177 | |
|---|
| 178 | /******************************************************************************** |
|---|
| 179 | This struct defines a file descriptor (handler) / size = 16 bytes |
|---|
| 180 | ********************************************************************************/ |
|---|
| 181 | |
|---|
| 182 | typedef struct fat_file_desc_s |
|---|
| 183 | { |
|---|
| 184 | unsigned int seek; // current offset value (bytes) |
|---|
| 185 | fat_inode_t* inode; // pointer on inode |
|---|
| 186 | char allocated; // file descriptor allocated |
|---|
| 187 | char read_only; // write protected |
|---|
| 188 | char write_only; // read protected |
|---|
| 189 | char reserved[5]; // reserved |
|---|
| 190 | } fat_file_desc_t; |
|---|
| 191 | |
|---|
| 192 | /******************************************************************************** |
|---|
| 193 | This struct defines a FAT32 File system descriptor |
|---|
| 194 | *******************************************************************************/ |
|---|
| 195 | |
|---|
| 196 | typedef struct fat_desc_s |
|---|
| 197 | { |
|---|
| 198 | unsigned char block_buffer[512]; // one block buffer (for FS_INFO) |
|---|
| 199 | fat_file_desc_t fd[GIET_OPEN_FILES_MAX]; // file descriptors array |
|---|
| 200 | spin_lock_t fat_lock; // global lock protecting FAT |
|---|
| 201 | fat_inode_t* inode_tree_root; // Inode-Tree root pointer |
|---|
| 202 | fat_cache_node_t* fat_cache_root; // Fat_Cache root pointer |
|---|
| 203 | unsigned int fat_cache_levels; // number of levels in Fat-Cache |
|---|
| 204 | unsigned int block_buffer_lba; // lba of block in block_buffer |
|---|
| 205 | unsigned int initialized; // 0xBABEF00D when FAT initialized |
|---|
| 206 | unsigned int sector_size; // must be 512 |
|---|
| 207 | unsigned int cluster_size; // must be 4096 |
|---|
| 208 | unsigned int fat_lba; // lba of first FAT sector |
|---|
| 209 | unsigned int fat_sectors; // number of sectors in FAT region |
|---|
| 210 | unsigned int data_lba; // lba of first DATA sector |
|---|
| 211 | unsigned int data_sectors; // number of sectors inf DATA region |
|---|
| 212 | unsigned int fs_info_lba; // lba of fs_info |
|---|
| 213 | unsigned int free_cluster_hint; // start point to search free cluster |
|---|
| 214 | unsigned int free_clusters_number; // total number of free clusters |
|---|
| 215 | } fat_desc_t; |
|---|
| 216 | |
|---|
| 217 | |
|---|
| 218 | /******************************************************************************* |
|---|
| 219 | Extern global variables |
|---|
| 220 | *******************************************************************************/ |
|---|
| 221 | |
|---|
| 222 | extern fat_desc_t _fat; |
|---|
| 223 | |
|---|
| 224 | /******************************************************************************* |
|---|
| 225 | Extern Functions |
|---|
| 226 | *******************************************************************************/ |
|---|
| 227 | |
|---|
| 228 | extern int _fat_ioc_access( unsigned int use_irq, // descheduling |
|---|
| 229 | unsigned int to_mem, // read / write |
|---|
| 230 | unsigned int lba, // first sector |
|---|
| 231 | unsigned int buf_vaddr, // memory buffer vaddr |
|---|
| 232 | unsigned int count ); // number of sectors |
|---|
| 233 | |
|---|
| 234 | extern int _fat_init(); |
|---|
| 235 | |
|---|
| 236 | extern int _fat_open( char* pathname, // path from root |
|---|
| 237 | unsigned int flags ); // O_CREAT/O_RDONLY |
|---|
| 238 | |
|---|
| 239 | extern int _fat_close( unsigned int fd_id ); // file descriptor |
|---|
| 240 | |
|---|
| 241 | extern int _fat_file_info( unsigned int fd_id, // file descriptor |
|---|
| 242 | fat_file_info_t* info ); // file info struct |
|---|
| 243 | |
|---|
| 244 | extern int _fat_read( unsigned int fd_id, // file descriptor |
|---|
| 245 | unsigned int vaddr, // destination buffer |
|---|
| 246 | unsigned int count, // number of bytes |
|---|
| 247 | unsigned int extend, // paddr extension |
|---|
| 248 | unsigned int offset, // forced offset |
|---|
| 249 | unsigned int modes ); // special modes |
|---|
| 250 | |
|---|
| 251 | extern int _fat_write( unsigned int fd_id, // file descriptor |
|---|
| 252 | unsigned int vaddr, // source buffer |
|---|
| 253 | unsigned int count, // number of bytes |
|---|
| 254 | unsigned int extend, // paddr extension |
|---|
| 255 | unsigned int modes ); // special modes |
|---|
| 256 | |
|---|
| 257 | extern int _fat_lseek( unsigned int fd_id, // file descriptor |
|---|
| 258 | unsigned int offset, // new offset value |
|---|
| 259 | unsigned int whence ); // command type |
|---|
| 260 | |
|---|
| 261 | extern int _fat_remove( char* pathname, // path from root |
|---|
| 262 | unsigned int should_be_dir ); // for checking |
|---|
| 263 | |
|---|
| 264 | extern int _fat_rename( char* old_path, // path from root |
|---|
| 265 | char* new_path ); // path from root |
|---|
| 266 | |
|---|
| 267 | extern int _fat_mkdir( char* pathname ); // path from root |
|---|
| 268 | |
|---|
| 269 | extern int _fat_opendir( char* pathname ); // path from root |
|---|
| 270 | |
|---|
| 271 | extern int _fat_closedir( unsigned int fd_id ); // file descriptor |
|---|
| 272 | |
|---|
| 273 | extern int _fat_readdir( unsigned int fd_id, // file descriptor |
|---|
| 274 | fat_dirent_t* entry ); // directory entry |
|---|
| 275 | |
|---|
| 276 | extern int _fat_load_no_cache( char* pathname, // path from root |
|---|
| 277 | unsigned int buffer_vbase, // buffer base |
|---|
| 278 | unsigned int buffer_size ); // buffer size |
|---|
| 279 | |
|---|
| 280 | extern int _get_fat_cache_buffer( unsigned int cluster_id, // cluster index in FAT |
|---|
| 281 | fat_cache_desc_t** desc ); // buffer descriptor |
|---|
| 282 | |
|---|
| 283 | extern int _get_file_cache_buffer( fat_inode_t* inode, // inode pointer |
|---|
| 284 | unsigned int cluster_id, // cluster index in file |
|---|
| 285 | unsigned int writable, // behaviour if miss |
|---|
| 286 | fat_cache_desc_t** desc ); // buffer descriptor |
|---|
| 287 | #endif |
|---|
| 288 | |
|---|
| 289 | |
|---|
| 290 | // Local Variables: |
|---|
| 291 | // tab-width: 4 |
|---|
| 292 | // c-basic-offset: 4 |
|---|
| 293 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
|---|
| 294 | // indent-tabs-mode: nil |
|---|
| 295 | // End: |
|---|
| 296 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
|---|
| 297 | |
|---|