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