2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
26 #define MBR_PARTITION_TYPE_FAT16 0x06
27 #define MBR_PARTITION_TYPE_FAT32 0x0B
28 #define MBR_PARTITION_TYPE_FAT32_LBA 0x0C
29 #define MBR_PARTITION_TYPE_FAT16_LBA 0x0E
31 // Signature bytes found at index 510 and 511 in the volume ID sector
32 #define FAT_VOLUME_ID_SIGNATURE_1 0x55
33 #define FAT_VOLUME_ID_SIGNATURE_2 0xAA
35 #define FAT_DIRECTORY_ENTRY_SIZE 32
36 #define FAT_SMALLEST_LEGAL_CLUSTER_NUMBER 2
38 #define FAT_MAXIMUM_FILESIZE 0xFFFFFFFF
40 #define FAT12_MAX_CLUSTERS 4084
41 #define FAT16_MAX_CLUSTERS 65524
43 #define FAT_FILE_ATTRIBUTE_READ_ONLY 0x01
44 #define FAT_FILE_ATTRIBUTE_HIDDEN 0x02
45 #define FAT_FILE_ATTRIBUTE_SYSTEM 0x04
46 #define FAT_FILE_ATTRIBUTE_VOLUME_ID 0x08
47 #define FAT_FILE_ATTRIBUTE_DIRECTORY 0x10
48 #define FAT_FILE_ATTRIBUTE_ARCHIVE 0x20
50 #define FAT_FILENAME_LENGTH 11
51 #define FAT_DELETED_FILE_MARKER 0xE5
53 #define FAT_MAKE_DATE(year, month, day) (day | (month << 5) | ((year - 1980) << 9))
54 #define FAT_MAKE_TIME(hour, minute, second) ((second / 2) | (minute << 5) | (hour << 11))
57 FAT_FILESYSTEM_TYPE_INVALID
,
58 FAT_FILESYSTEM_TYPE_FAT12
,
59 FAT_FILESYSTEM_TYPE_FAT16
,
60 FAT_FILESYSTEM_TYPE_FAT32
61 } fatFilesystemType_e
;
63 typedef struct mbrPartitionEntry_t
{
70 } __attribute__((packed
)) mbrPartitionEntry_t
;
72 typedef struct fat16Descriptor_t
{
75 uint8_t bootSignature
;
78 char fileSystemType
[8];
79 } __attribute__((packed
)) fat16Descriptor_t
;
81 typedef struct fat32Descriptor_t
{
87 uint16_t backupBootSector
;
91 uint8_t bootSignature
;
94 char fileSystemType
[8];
95 } __attribute__((packed
)) fat32Descriptor_t
;
97 typedef struct fatVolumeID_t
{
100 uint16_t bytesPerSector
;
101 uint8_t sectorsPerCluster
;
102 uint16_t reservedSectorCount
;
104 uint16_t rootEntryCount
;
105 uint16_t totalSectors16
;
108 uint16_t sectorsPerTrack
;
110 uint32_t hiddenSectors
;
111 uint32_t totalSectors32
;
113 fat16Descriptor_t fat16
;
114 fat32Descriptor_t fat32
;
116 } __attribute__((packed
)) fatVolumeID_t
;
118 typedef struct fatDirectoryEntry_t
{
119 char filename
[FAT_FILENAME_LENGTH
];
122 uint8_t creationTimeTenths
;
123 uint16_t creationTime
;
124 uint16_t creationDate
;
125 uint16_t lastAccessDate
;
126 uint16_t firstClusterHigh
;
127 uint16_t lastWriteTime
;
128 uint16_t lastWriteDate
;
129 uint16_t firstClusterLow
;
131 } __attribute__((packed
)) fatDirectoryEntry_t
;
133 uint32_t fat32_decodeClusterNumber(uint32_t clusterNumber
);
135 bool fat32_isEndOfChainMarker(uint32_t clusterNumber
);
136 bool fat16_isEndOfChainMarker(uint16_t clusterNumber
);
138 bool fat_isFreeSpace(uint32_t clusterNumber
);
140 bool fat_isDirectoryEntryTerminator(fatDirectoryEntry_t
*entry
);
141 bool fat_isDirectoryEntryEmpty(fatDirectoryEntry_t
*entry
);
143 void fat_convertFilenameToFATStyle(const char *filename
, uint8_t *fatFilename
);