[530] | 1 | //////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 2 | // File : fat32.h |
---|
| 3 | // Date : 01/09/2013 |
---|
| 4 | // Author : Marco Jankovic / Alain Greiner |
---|
| 5 | // Copyright (c) UPMC-LIP6 |
---|
[530] | 6 | //////////////////////////////////////////////////////////////////////////////// |
---|
[258] | 7 | |
---|
| 8 | #ifndef _FAT32_H |
---|
| 9 | #define _FAT32_H |
---|
| 10 | |
---|
| 11 | #include "giet_config.h" |
---|
[503] | 12 | #include "kernel_locks.h" |
---|
[258] | 13 | |
---|
[530] | 14 | /*************** Partition Boot Sector Format **********************************/ |
---|
[258] | 15 | // offset | length |
---|
| 16 | #define BS_JMPBOOT 0 , 3 |
---|
| 17 | #define BS_OEMNAME 3 , 8 |
---|
| 18 | #define BPB_BYTSPERSEC 11 , 2 |
---|
| 19 | #define BPB_SECPERCLUS 13 , 1 |
---|
| 20 | #define BPB_RSVDSECCNT 14 , 2 |
---|
| 21 | #define BPB_NUMFATS 16 , 1 |
---|
| 22 | #define BPB_ROOTENTCNT 17 , 2 |
---|
| 23 | #define BPB_TOTSEC16 19 , 2 |
---|
| 24 | #define BPB_MEDIA 21 , 1 |
---|
| 25 | #define BPB_FATSZ16 22 , 2 |
---|
| 26 | #define BPB_SECPERTRK 24 , 2 |
---|
| 27 | #define BPB_NUMHEADS 26 , 2 |
---|
| 28 | #define BPB_HIDDSEC 28 , 4 |
---|
| 29 | #define BPB_TOTSEC32 32 , 4 |
---|
| 30 | #define BPB_PARTITION_TABLE 446 , 64 |
---|
| 31 | |
---|
| 32 | // FAT 32 |
---|
| 33 | #define BPB_FAT32_FATSZ32 36 , 4 |
---|
| 34 | #define BPB_FAT32_EXTFLAGS 40 , 2 |
---|
| 35 | #define BPB_FAT32_FSVER 42 , 2 |
---|
| 36 | #define BPB_FAT32_ROOTCLUS 44 , 4 |
---|
| 37 | #define BPB_FAT32_FSINFO 48 , 2 |
---|
| 38 | #define BPB_FAT32_BKBOOTSEC 50 , 2 |
---|
| 39 | #define BS_FAT32_DRVNUM 64 , 1 |
---|
| 40 | #define BS_FAT32_BOOTSIG 66 , 1 |
---|
| 41 | #define BS_FAT32_VOLID 67 , 4 |
---|
| 42 | #define BS_FAT32_VOLLAB 71 , 11 |
---|
| 43 | #define BS_FAT32_FILSYSTYPE 82 , 8 |
---|
| 44 | |
---|
| 45 | // Partitions |
---|
| 46 | #define FIRST_PARTITION_ACTIVE 446 , 8 |
---|
| 47 | #define FIRST_PARTITION_BEGIN_LBA 454 , 4 |
---|
| 48 | #define FIRST_PARTITION_SIZE 458 , 4 |
---|
| 49 | #define SECOND_PARTITION_ACTIVE 462 , 8 |
---|
| 50 | #define SECOND_PARTITION_BEGIN_LBA 470 , 4 |
---|
| 51 | #define SECOND_PARTITION_SIZE 474 , 4 |
---|
| 52 | #define THIRD_PARTITION_ACTIVE 478 , 8 |
---|
| 53 | #define THIRD_PARTITION_BEGIN_LBA 486 , 4 |
---|
| 54 | #define THIRD_PARTITION_SIZE 490 , 4 |
---|
| 55 | #define FOURTH_PARTITION_ACTIVE 494 , 8 |
---|
| 56 | #define FOURTH_PARTITION_BEGIN_LBA 502 , 4 |
---|
| 57 | #define FOURTH_PARTITION_SIZE 506 , 4 |
---|
[530] | 58 | /*******************************************************************************/ |
---|
[258] | 59 | |
---|
| 60 | #define MBR_SIGNATURE_POSITION 510 , 2 |
---|
| 61 | #define MBR_SIGNATURE_VALUE 0xAA55 |
---|
| 62 | |
---|
[530] | 63 | /************** FAT_FS_INFO SECTOR ********************************************/ |
---|
[258] | 64 | #define FS_SIGNATURE_VALUE_1 0x52526141 |
---|
| 65 | #define FS_SIGNATURE_VALUE_2 0x72724161 |
---|
| 66 | #define FS_SIGNATURE_VALUE_3 0x000055AA |
---|
| 67 | #define FS_SIGNATURE_POSITION_1 0 , 4 |
---|
| 68 | #define FS_SIGNATURE_POSITION_2 484 , 4 |
---|
| 69 | #define FS_SIGNATURE_POSITION_3 508 , 4 |
---|
[291] | 70 | #define FS_FREE_CLUSTER 488 , 4 |
---|
| 71 | #define FS_FREE_CLUSTER_HINT 492 , 4 |
---|
[530] | 72 | /*******************************************************************************/ |
---|
[258] | 73 | |
---|
| 74 | #define DIR_ENTRY_SIZE 32 |
---|
| 75 | |
---|
[530] | 76 | /******* Directory Entry Structure (32 bytes) **********************************/ |
---|
[258] | 77 | // offset | length |
---|
| 78 | #define DIR_NAME 0 , 11 // dir_entry name |
---|
| 79 | #define DIR_ATTR 11 , 1 // attributes |
---|
| 80 | #define DIR_NTRES 12 , 1 // reserved for the OS |
---|
| 81 | #define DIR_CRT_TIMES_TENTH 13 , 1 |
---|
| 82 | #define DIR_FST_CLUS_HI 20 , 2 // cluster index 16 MSB bits |
---|
| 83 | #define DIR_WRT_TIME 22 , 2 // time of last write |
---|
| 84 | #define DIR_WRT_DATE 24 , 2 // date of last write |
---|
| 85 | #define DIR_FST_CLUS_LO 26 , 2 // cluster index 16 LSB bit |
---|
| 86 | #define DIR_FILE_SIZE 28 , 4 // file size (up to 4 giga bytes) |
---|
[530] | 87 | /*******************************************************************************/ |
---|
[258] | 88 | |
---|
[530] | 89 | /******* LFN Directory Entry Structure (32 bytes) *****************************/ |
---|
[258] | 90 | // offset | length |
---|
| 91 | #define LDIR_ORD 0 , 1 // Sequence number (from 0x01 to 0x0f) |
---|
| 92 | #define LDIR_NAME_1 1 , 10 // name broken into 3 parts |
---|
| 93 | #define LDIR_ATTR 11 , 1 // attributes (must be 0x0F) |
---|
| 94 | #define LDIR_TYPE 12 , 1 // directory type (must be 0x00) |
---|
| 95 | #define LDIR_CHKSUM 13 , 1 // checksum of name in short dir |
---|
| 96 | #define LDIR_NAME_2 14 , 12 |
---|
| 97 | #define LDIR_RSVD 26 , 2 // artifact of previous fat (must be 0) |
---|
| 98 | #define LDIR_NAME_3 28 , 4 |
---|
[530] | 99 | /*******************************************************************************/ |
---|
[258] | 100 | |
---|
[530] | 101 | /*********************** DIR_ATTR values (attributes) ************************/ |
---|
[258] | 102 | #define ATTR_READ_ONLY 0x01 |
---|
| 103 | #define ATTR_HIDDEN 0x02 |
---|
| 104 | #define ATTR_SYSTEM 0x04 |
---|
| 105 | #define ATTR_VOLUME_ID 0x08 |
---|
| 106 | #define ATTR_DIRECTORY 0x10 |
---|
| 107 | #define ATTR_ARCHIVE 0x20 |
---|
| 108 | #define ATTR_LONG_NAME_MASK 0x0f // READ_ONLY|HIDDEN|SYSTEM|VOLUME_ID |
---|
[530] | 109 | /*******************************************************************************/ |
---|
[258] | 110 | |
---|
[530] | 111 | /********************* DIR_ORD special values **********************************/ |
---|
[258] | 112 | #define FREE_ENTRY 0xE5 // this entry is free in the directory |
---|
| 113 | #define NO_MORE_ENTRY 0x00 // no more entry in the directory |
---|
[530] | 114 | /*******************************************************************************/ |
---|
[258] | 115 | |
---|
[530] | 116 | /******************** CLuster Index Special Values *****************************/ |
---|
[258] | 117 | #define FREE_CLUSTER 0x00000000 |
---|
| 118 | #define RESERVED_CLUSTER 0x00000001 |
---|
| 119 | #define BAD_CLUSTER 0x0FFFFFF7 |
---|
| 120 | #define END_OF_CHAIN_CLUSTER 0x0ffffff8 |
---|
[530] | 121 | /*******************************************************************************/ |
---|
[258] | 122 | |
---|
| 123 | #define FAT_INITIALISED 0xBABEF00D |
---|
| 124 | |
---|
[530] | 125 | /************ This struct defines a file descriptor ****************************/ |
---|
[258] | 126 | typedef struct file_descriptor_s |
---|
| 127 | { |
---|
| 128 | unsigned int used; // descriptor contains an open file |
---|
[300] | 129 | unsigned int first_cluster; // first cluster index on disk |
---|
[258] | 130 | unsigned int file_size; // number of bytes |
---|
[291] | 131 | unsigned int lba_dir_entry; // lba of dir_entry for an open file |
---|
[258] | 132 | char name[244]; // pathname |
---|
| 133 | } file_desc_t; |
---|
[530] | 134 | /*******************************************************************************/ |
---|
[258] | 135 | |
---|
[530] | 136 | /************ This struct describes a FAT32 disk *******************************/ |
---|
[258] | 137 | typedef struct fat32_fs_s |
---|
| 138 | { |
---|
[530] | 139 | char fat_cache[512]; // FAT cache: 1 sector |
---|
[458] | 140 | spin_lock_t fat_lock; // lock protecting exclusive access |
---|
[258] | 141 | file_desc_t fd[GIET_OPEN_FILES_MAX]; // file descriptors array |
---|
[530] | 142 | unsigned int initialised; // 0xBABEF00D when FAT initialised |
---|
[258] | 143 | unsigned int sector_size; // number of bytes (power of 2) |
---|
| 144 | unsigned int sectors_per_cluster; // power of 2 (must be greater than 4) |
---|
[530] | 145 | unsigned int cluster_size; // sector_size * sector_per_cluster |
---|
| 146 | unsigned int fat_sectors; // number of sectors for 1 FAT copy |
---|
[300] | 147 | unsigned int fat_lba; // lba of first FAT sector |
---|
[258] | 148 | unsigned int data_lba; // lba of first data sector |
---|
| 149 | unsigned int cache_lba; // lba of sector loaded in fat_cache |
---|
[291] | 150 | unsigned int last_cluster_allocated; // Last known cluster allocated |
---|
| 151 | unsigned int number_free_cluster; // number of free clusters |
---|
| 152 | unsigned int fs_info_lba; // lba of fs_info |
---|
[258] | 153 | } fat32_fs_t; |
---|
[530] | 154 | /*******************************************************************************/ |
---|
[258] | 155 | |
---|
[530] | 156 | /*********************** Extern Functions *************************************/ |
---|
[258] | 157 | |
---|
[530] | 158 | extern int _fat_init( unsigned int use_irq ); // use IRQ if possible |
---|
[258] | 159 | |
---|
[530] | 160 | extern int _fat_open( unsigned int use_irq, // use IRQ if possible |
---|
| 161 | char* pathname, // file pathname from root |
---|
| 162 | unsigned int create ); // create new file if non zero |
---|
[258] | 163 | |
---|
[530] | 164 | extern int _fat_read( unsigned int use_irq, // use IRQ if possible |
---|
| 165 | unsigned int fd_id, // file descriptor index |
---|
| 166 | void* buffer, // destination buffer |
---|
| 167 | unsigned int count, // number of sectors to read |
---|
| 168 | unsigned int offset ); // offset sector in file |
---|
[258] | 169 | |
---|
[530] | 170 | extern int _fat_write( unsigned int use_irq, // use IRQ if possible |
---|
| 171 | unsigned int fd_id, // file descriptor index |
---|
| 172 | void* buffer, // source buffer |
---|
| 173 | unsigned int count, // number of sectors to write |
---|
| 174 | unsigned int offset ); // offset sector in file |
---|
[258] | 175 | |
---|
[530] | 176 | extern int _fat_fstat( unsigned int fd_id ); // file descriptor index |
---|
[258] | 177 | |
---|
[530] | 178 | extern int _fat_close( unsigned int fd_id ); // file descriptor index |
---|
[258] | 179 | |
---|
[260] | 180 | |
---|
[530] | 181 | extern int _fat_user_open( char* pathname, // file pathname from root |
---|
| 182 | unsigned int flags ); // unused |
---|
[258] | 183 | |
---|
| 184 | extern int _fat_user_read( unsigned int fd_id, // file descriptor index |
---|
| 185 | void* buffer, // destination buffer |
---|
| 186 | unsigned int count, // number of sectors to read |
---|
[530] | 187 | unsigned int offset ); // sectors to skip in file |
---|
[258] | 188 | |
---|
| 189 | extern int _fat_user_write( unsigned int fd_id, // file descriptor index |
---|
| 190 | void* buffer, // source buffer |
---|
| 191 | unsigned int count, // number of sectors to write |
---|
[530] | 192 | unsigned int offset ); // sectors to skip in file |
---|
[258] | 193 | |
---|
| 194 | extern int _fat_user_lseek( unsigned int fd_id, |
---|
| 195 | unsigned int offset, |
---|
| 196 | unsigned int whence ); |
---|
| 197 | |
---|
[530] | 198 | /*******************************************************************************/ |
---|
[258] | 199 | |
---|
| 200 | |
---|
| 201 | #endif |
---|
| 202 | |
---|
| 203 | |
---|
| 204 | // Local Variables: |
---|
| 205 | // tab-width: 4 |
---|
| 206 | // c-basic-offset: 4 |
---|
| 207 | // c-file-offsets:((innamespace . 0)(inline-open . 0)) |
---|
| 208 | // indent-tabs-mode: nil |
---|
| 209 | // End: |
---|
| 210 | // vim: filetype=c:expandtab:shiftwidth=4:tabstop=4:softtabstop=4 |
---|
| 211 | |
---|