[1] | 1 | /* |
---|
| 2 | * fatfs.h - FATFS file system API definition. |
---|
| 3 | * |
---|
[23] | 4 | * Author Mohamed Lamine Karaoui (2014,2015) |
---|
| 5 | * Alain Greiner (2016,2017) |
---|
[1] | 6 | * |
---|
| 7 | * Copyright (c) UPMC Sorbonne Universites |
---|
| 8 | * |
---|
| 9 | * This file is part of ALMOS-MKH. |
---|
| 10 | * |
---|
| 11 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
| 12 | * under the terms of the GNU General Public License as published by |
---|
| 13 | * the Free Software Foundation; version 2.0 of the License. |
---|
| 14 | * |
---|
| 15 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
| 16 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
| 18 | * General Public License for more details. |
---|
| 19 | * |
---|
| 20 | * You should have received a copy of the GNU General Public License |
---|
| 21 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
| 22 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
| 23 | */ |
---|
| 24 | |
---|
| 25 | #ifndef _FATFS_H_ |
---|
| 26 | #define _FATFS_H_ |
---|
| 27 | |
---|
| 28 | #include <hal_types.h> |
---|
| 29 | #include <rwlock.h> |
---|
[23] | 30 | #include <vfs.h> |
---|
[1] | 31 | |
---|
[23] | 32 | |
---|
| 33 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 34 | // The FATFS File System implements a FAT32 read/write file system. |
---|
| 35 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 36 | |
---|
| 37 | /*************** Partition Boot Sector Format **********************************/ |
---|
| 38 | // offset | length |
---|
| 39 | #define BS_JMPBOOT 0 , 3 |
---|
| 40 | #define BS_OEMNAME 3 , 8 |
---|
| 41 | #define BPB_BYTSPERSEC 11 , 2 |
---|
| 42 | #define BPB_SECPERCLUS 13 , 1 |
---|
| 43 | #define BPB_RSVDSECCNT 14 , 2 |
---|
| 44 | #define BPB_NUMFATS 16 , 1 |
---|
| 45 | #define BPB_ROOTENTCNT 17 , 2 |
---|
| 46 | #define BPB_TOTSEC16 19 , 2 |
---|
| 47 | #define BPB_MEDIA 21 , 1 |
---|
| 48 | #define BPB_FATSZ16 22 , 2 |
---|
| 49 | #define BPB_SECPERTRK 24 , 2 |
---|
| 50 | #define BPB_NUMHEADS 26 , 2 |
---|
| 51 | #define BPB_HIDDSEC 28 , 4 |
---|
| 52 | #define BPB_TOTSEC32 32 , 4 |
---|
| 53 | #define BPB_PARTITION_TABLE 446 , 64 |
---|
| 54 | |
---|
| 55 | // FAT 32 |
---|
| 56 | #define BPB_FAT32_FATSZ32 36 , 4 |
---|
| 57 | #define BPB_FAT32_EXTFLAGS 40 , 2 |
---|
| 58 | #define BPB_FAT32_FSVER 42 , 2 |
---|
| 59 | #define BPB_FAT32_ROOTCLUS 44 , 4 |
---|
| 60 | #define BPB_FAT32_FSINFO 48 , 2 |
---|
| 61 | #define BPB_FAT32_BKBOOTSEC 50 , 2 |
---|
| 62 | #define BS_FAT32_DRVNUM 64 , 1 |
---|
| 63 | #define BS_FAT32_BOOTSIG 66 , 1 |
---|
| 64 | #define BS_FAT32_VOLID 67 , 4 |
---|
| 65 | #define BS_FAT32_VOLLAB 71 , 11 |
---|
| 66 | #define BS_FAT32_FILSYSTYPE 82 , 8 |
---|
| 67 | |
---|
| 68 | // Partitions |
---|
| 69 | #define FIRST_PARTITION_ACTIVE 446 , 8 |
---|
| 70 | #define FIRST_PARTITION_BEGIN_LBA 454 , 4 |
---|
| 71 | #define FIRST_PARTITION_SIZE 458 , 4 |
---|
| 72 | #define SECOND_PARTITION_ACTIVE 462 , 8 |
---|
| 73 | #define SECOND_PARTITION_BEGIN_LBA 470 , 4 |
---|
| 74 | #define SECOND_PARTITION_SIZE 474 , 4 |
---|
| 75 | #define THIRD_PARTITION_ACTIVE 478 , 8 |
---|
| 76 | #define THIRD_PARTITION_BEGIN_LBA 486 , 4 |
---|
| 77 | #define THIRD_PARTITION_SIZE 490 , 4 |
---|
| 78 | #define FOURTH_PARTITION_ACTIVE 494 , 8 |
---|
| 79 | #define FOURTH_PARTITION_BEGIN_LBA 502 , 4 |
---|
| 80 | #define FOURTH_PARTITION_SIZE 506 , 4 |
---|
| 81 | /*******************************************************************************/ |
---|
| 82 | |
---|
| 83 | #define MBR_SIGNATURE_POSITION 510 , 2 |
---|
| 84 | #define MBR_SIGNATURE_VALUE 0xAA55 |
---|
| 85 | |
---|
| 86 | /************** FAT_FS_INFO SECTOR ********************************************/ |
---|
| 87 | #define FS_SIGNATURE_VALUE_1 0x52526141 |
---|
| 88 | #define FS_SIGNATURE_VALUE_2 0x72724161 |
---|
| 89 | #define FS_SIGNATURE_VALUE_3 0x000055AA |
---|
| 90 | #define FS_SIGNATURE_POSITION_1 0 , 4 |
---|
| 91 | #define FS_SIGNATURE_POSITION_2 484 , 4 |
---|
| 92 | #define FS_SIGNATURE_POSITION_3 508 , 4 |
---|
| 93 | #define FS_FREE_CLUSTERS 488 , 4 |
---|
| 94 | #define FS_FREE_CLUSTER_HINT 492 , 4 |
---|
| 95 | /*******************************************************************************/ |
---|
| 96 | |
---|
| 97 | #define DIR_ENTRY_SIZE 32 |
---|
| 98 | |
---|
| 99 | #define NAME_MAX_SIZE 31 |
---|
| 100 | |
---|
| 101 | /******* Directory Entry Structure (32 bytes) **********************************/ |
---|
| 102 | // offset | length |
---|
| 103 | #define DIR_NAME 0 , 11 // dir_entry name |
---|
| 104 | #define DIR_ATTR 11 , 1 // attributes |
---|
| 105 | #define DIR_NTRES 12 , 1 // reserved for the OS |
---|
| 106 | #define DIR_CRT_TIMES_TENTH 13 , 1 |
---|
| 107 | #define DIR_FST_CLUS_HI 20 , 2 // cluster index 16 MSB bits |
---|
| 108 | #define DIR_WRT_TIME 22 , 2 // time of last write |
---|
| 109 | #define DIR_WRT_DATE 24 , 2 // date of last write |
---|
| 110 | #define DIR_FST_CLUS_LO 26 , 2 // cluster index 16 LSB bit |
---|
| 111 | #define DIR_FILE_SIZE 28 , 4 // dir_entry size (up to 4 Gbytes) |
---|
| 112 | /*******************************************************************************/ |
---|
| 113 | |
---|
| 114 | /******* LFN Directory Entry Structure (32 bytes) *****************************/ |
---|
| 115 | // offset | length |
---|
| 116 | #define LDIR_ORD 0 , 1 // Sequence number (from 0x01 to 0x0f) |
---|
| 117 | #define LDIR_NAME_1 1 , 10 // name broken into 3 parts |
---|
| 118 | #define LDIR_ATTR 11 , 1 // attributes (must be 0x0F) |
---|
| 119 | #define LDIR_TYPE 12 , 1 // directory type (must be 0x00) |
---|
| 120 | #define LDIR_CHKSUM 13 , 1 // checksum of name in short dir |
---|
| 121 | #define LDIR_NAME_2 14 , 12 |
---|
| 122 | #define LDIR_RSVD 26 , 2 // artifact of previous fat (must be 0) |
---|
| 123 | #define LDIR_NAME_3 28 , 4 |
---|
| 124 | /*******************************************************************************/ |
---|
| 125 | |
---|
| 126 | /*********************** DIR_ATTR values (attributes) ************************/ |
---|
| 127 | #define ATTR_READ_ONLY 0x01 |
---|
| 128 | #define ATTR_HIDDEN 0x02 |
---|
| 129 | #define ATTR_SYSTEM 0x04 |
---|
| 130 | #define ATTR_VOLUME_ID 0x08 |
---|
| 131 | #define ATTR_DIRECTORY 0x10 |
---|
| 132 | #define ATTR_ARCHIVE 0x20 |
---|
| 133 | #define ATTR_LONG_NAME_MASK 0x0f // READ_ONLY|HIDDEN|SYSTEM|VOLUME_ID |
---|
| 134 | /*******************************************************************************/ |
---|
| 135 | |
---|
| 136 | /********************* DIR_ORD special values **********************************/ |
---|
| 137 | #define FREE_ENTRY 0xE5 // this entry is free in the directory |
---|
| 138 | #define NO_MORE_ENTRY 0x00 // no more entry in the directory |
---|
| 139 | /*******************************************************************************/ |
---|
| 140 | |
---|
| 141 | /******************** CLuster Index Special Values *****************************/ |
---|
| 142 | #define FREE_CLUSTER 0x00000000 |
---|
| 143 | #define RESERVED_CLUSTER 0x00000001 |
---|
| 144 | #define BAD_CLUSTER 0x0FFFFFF7 |
---|
| 145 | #define END_OF_CHAIN_CLUSTER_MIN 0x0ffffff8 |
---|
| 146 | #define END_OF_CHAIN_CLUSTER_MAX 0x0fffffff |
---|
| 147 | /*******************************************************************************/ |
---|
| 148 | |
---|
[1] | 149 | /**** Forward declarations ****/ |
---|
| 150 | |
---|
| 151 | struct mapper_s; |
---|
[23] | 152 | struct page_s; |
---|
| 153 | struct vfs_ctx_s; |
---|
[1] | 154 | struct vfs_inode_s; |
---|
| 155 | |
---|
| 156 | /***************************************************************************************** |
---|
[23] | 157 | * This structure defines a FATFS specific context (extension to VFS context). |
---|
[1] | 158 | ****************************************************************************************/ |
---|
| 159 | |
---|
| 160 | typedef struct fatfs_ctx_s |
---|
| 161 | { |
---|
[188] | 162 | uint32_t fat_sectors_count; /*! number of sectors in FAT region */ |
---|
| 163 | uint32_t bytes_per_sector; /*! number of bytes per sector */ |
---|
| 164 | uint32_t sectors_per_cluster; /*! number of sectors per cluster */ |
---|
[23] | 165 | uint32_t fat_begin_lba; /*! lba of FAT region */ |
---|
| 166 | uint32_t cluster_begin_lba; /*! lba of data region */ |
---|
[188] | 167 | uint32_t root_dir_cluster; /*! cluster index for the root directory */ |
---|
[23] | 168 | uint32_t last_allocated_sector; /*! TODO ??? */ |
---|
| 169 | uint32_t last_allocated_index; /*! TODO ??? */ |
---|
| 170 | xptr_t fat_mapper_xp; /*! FAT mapper (in IO cluster) */ |
---|
[1] | 171 | } |
---|
| 172 | fatfs_ctx_t; |
---|
| 173 | |
---|
[23] | 174 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
[188] | 175 | // Generic API: These functions are called by the kernel, |
---|
| 176 | // and must be implemented by all File Systems. |
---|
[23] | 177 | ////////////////////////////////////////////////////////////////////////////////////////// |
---|
| 178 | |
---|
| 179 | /****************************************************************************************** |
---|
[188] | 180 | * This fuction allocates memory from local cluster for a FATFS context descriptor. |
---|
[23] | 181 | ****************************************************************************************** |
---|
[188] | 182 | * @ return a pointer on the created context / return NULL if failure. |
---|
[23] | 183 | *****************************************************************************************/ |
---|
[188] | 184 | fatfs_ctx_t * fatfs_ctx_alloc(); |
---|
[23] | 185 | |
---|
[1] | 186 | /***************************************************************************************** |
---|
[188] | 187 | * This function access the external boot device, and initialises it |
---|
| 188 | * from informations contained in the boot record. |
---|
[1] | 189 | ***************************************************************************************** |
---|
[188] | 190 | * @ vfs_ctx : local pointer on VFS context for FATFS. |
---|
[1] | 191 | ****************************************************************************************/ |
---|
[188] | 192 | void fatfs_ctx_init( fatfs_ctx_t * fatfs_ctx ); |
---|
[1] | 193 | |
---|
| 194 | /***************************************************************************************** |
---|
[23] | 195 | * This function releases memory dynamically allocated for the FATFS context extension. |
---|
[1] | 196 | ***************************************************************************************** |
---|
[23] | 197 | * @ vfs_ctx : local pointer on VFS context. |
---|
[1] | 198 | ****************************************************************************************/ |
---|
[188] | 199 | void fatfs_ctx_destroy( fatfs_ctx_t * fatfs_ctx ); |
---|
[1] | 200 | |
---|
| 201 | /***************************************************************************************** |
---|
[23] | 202 | * This function moves a page from the mapper to the FATFS file system on device. |
---|
[1] | 203 | * It must be called by a thread running in cluster containing the mapper. |
---|
[23] | 204 | * The pointer on the mapper and the page index in file are registered |
---|
[1] | 205 | * in the page descriptor. |
---|
| 206 | ***************************************************************************************** |
---|
| 207 | * @ page : local pointer on page descriptor. |
---|
| 208 | * @ return 0 if success / return EIO if error. |
---|
| 209 | ****************************************************************************************/ |
---|
| 210 | error_t fatfs_write_page( struct page_s * page ); |
---|
| 211 | |
---|
| 212 | /***************************************************************************************** |
---|
| 213 | * This function moves a page from the FATFS file system on device to the mapper. |
---|
| 214 | * It must be called by a thread running in cluster containing the mapper. |
---|
[23] | 215 | * The pointer on the mapper and the page index in file are registered |
---|
[1] | 216 | * in the page descriptor. |
---|
| 217 | ***************************************************************************************** |
---|
| 218 | * @ page : local pointer on page descriptor. |
---|
| 219 | * @ return 0 if success / return EIO if error. |
---|
| 220 | ****************************************************************************************/ |
---|
| 221 | error_t fatfs_read_page( struct page_s * page ); |
---|
| 222 | |
---|
[188] | 223 | /***************************************************************************************** |
---|
| 224 | * This function scan the FAT (File Allocation Table), stored in the FAT mapper, |
---|
| 225 | * and return the FATFS cluster index for a given page of a given file. |
---|
| 226 | * It must be called by a thread running in the cluster containing the FAT mapper |
---|
| 227 | * (can be a local thread or a RPC thread). |
---|
| 228 | * The FAT is actually an array of uint32_t slots. Each slot in this array contains the |
---|
| 229 | * index of another slot in this array, to form one linked list for each file stored on |
---|
| 230 | * device in the RAMFS file system. This index in the FAT array is also the index of the |
---|
| 231 | * FATFS cluster on the device. One FATFS cluster is supposed to contain one PPM page. |
---|
| 232 | * For a given file, the entry point in the FAT is simply the index of the FATFS cluster |
---|
| 233 | * containing the first page of the file. The mapper being a cache, this function |
---|
| 234 | * automatically updates the FAT mapper from informations stored on device in case of miss. |
---|
| 235 | ***************************************************************************************** |
---|
| 236 | * @ mapper : local pointer on the FAT mapper. |
---|
| 237 | * @ first_cluster : index of the first FATFS cluster allocated to the file. |
---|
| 238 | * @ searched_page : index of searched page in the file. |
---|
| 239 | * @ cluster : [out] pointer on buffer for the found FATFS cluster index. |
---|
| 240 | * @ return 0 if success / return EIO if FAT mapper miss cannot be solved. |
---|
| 241 | ****************************************************************************************/ |
---|
| 242 | error_t fatfs_get_cluster( struct mapper_s * mapper, |
---|
| 243 | uint32_t first_cluster, |
---|
| 244 | uint32_t searched_page, |
---|
| 245 | uint32_t * cluster ); |
---|
[1] | 246 | |
---|
| 247 | #endif /* _FATFS_H_ */ |
---|