NinjaFlight
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
fat_standard.h
Go to the documentation of this file.
1 /*
2  * This file is part of Ninjaflight.
3  *
4  * Ninjaflight 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.
8  *
9  * Ninjaflight 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.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with Ninjaflight. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #pragma once
19 
20 #include <stdint.h>
21 #include <stdbool.h>
22 
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
27 
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
31 
32 #define FAT_DIRECTORY_ENTRY_SIZE 32
33 #define FAT_SMALLEST_LEGAL_CLUSTER_NUMBER 2
34 
35 #define FAT_MAXIMUM_FILESIZE 0xFFFFFFFF
36 
37 #define FAT12_MAX_CLUSTERS 4084
38 #define FAT16_MAX_CLUSTERS 65524
39 
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
46 
47 #define FAT_FILENAME_LENGTH 11
48 #define FAT_DELETED_FILE_MARKER 0xE5
49 
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))
52 
53 typedef enum {
59 
60 typedef struct mbrPartitionEntry_t {
61  uint8_t bootFlag;
62  uint8_t chsBegin[3];
63  uint8_t type;
64  uint8_t chsEnd[3];
65  uint32_t lbaBegin;
66  uint32_t numSectors;
68 
69 typedef struct fat16Descriptor_t {
70  uint8_t driveNumber;
71  uint8_t reserved1;
72  uint8_t bootSignature;
73  uint32_t volumeID;
74  char volumeLabel[11];
75  char fileSystemType[8];
77 
78 typedef struct fat32Descriptor_t {
79  uint32_t FATSize32;
80  uint16_t extFlags;
81  uint16_t fsVer;
82  uint32_t rootCluster;
83  uint16_t fsInfo;
84  uint16_t backupBootSector;
85  uint8_t reserved[12];
86  uint8_t driveNumber;
87  uint8_t reserved1;
88  uint8_t bootSignature;
89  uint32_t volumeID;
90  char volumeLabel[11];
91  char fileSystemType[8];
93 
94 typedef struct fatVolumeID_t {
95  uint8_t jmpBoot[3];
96  char oemName[8];
97  uint16_t bytesPerSector;
100  uint8_t numFATs;
101  uint16_t rootEntryCount;
102  uint16_t totalSectors16;
103  uint8_t media;
104  uint16_t FATSize16;
105  uint16_t sectorsPerTrack;
106  uint16_t numHeads;
107  uint32_t hiddenSectors;
108  uint32_t totalSectors32;
109  union {
111  fat32Descriptor_t fat32;
112  } fatDescriptor;
113 } __attribute__((packed)) fatVolumeID_t;
114 
115 typedef struct fatDirectoryEntry_t {
117  uint8_t attrib;
118  uint8_t ntReserved;
120  uint16_t creationTime;
121  uint16_t creationDate;
122  uint16_t lastAccessDate;
124  uint16_t lastWriteTime;
125  uint16_t lastWriteDate;
126  uint16_t firstClusterLow;
127  uint32_t fileSize;
129 
130 uint32_t fat32_decodeClusterNumber(uint32_t clusterNumber);
131 
132 bool fat32_isEndOfChainMarker(uint32_t clusterNumber);
133 bool fat16_isEndOfChainMarker(uint16_t clusterNumber);
134 
135 bool fat_isFreeSpace(uint32_t clusterNumber);
136 
137 bool fat_isDirectoryEntryTerminator(fatDirectoryEntry_t *entry);
138 bool fat_isDirectoryEntryEmpty(fatDirectoryEntry_t *entry);
139 
140 void fat_convertFilenameToFATStyle(const char *filename, uint8_t *fatFilename);
uint8_t creationTimeTenths
Definition: fat_standard.h:119
uint32_t lbaBegin
Definition: fat_standard.h:65
uint8_t sectorsPerCluster
Definition: fat_standard.h:98
union @6 fatDescriptor
uint16_t lastAccessDate
Definition: fat_standard.h:122
uint16_t rootEntryCount
Definition: fat_standard.h:101
uint16_t firstClusterHigh
Definition: fat_standard.h:123
uint8_t attrib
Definition: fat_standard.h:117
uint16_t fsVer
Definition: fat_standard.h:67
Definition: fat_standard.h:78
bool fat_isDirectoryEntryTerminator(fatDirectoryEntry_t *entry)
Definition: fat_standard.c:49
uint32_t FATSize32
Definition: fat_standard.h:79
uint16_t fsInfo
Definition: fat_standard.h:83
uint16_t reservedSectorCount
Definition: fat_standard.h:99
uint16_t lastWriteTime
Definition: fat_standard.h:124
uint8_t ntReserved
Definition: fat_standard.h:118
uint16_t backupBootSector
Definition: fat_standard.h:84
uint16_t firstClusterLow
Definition: fat_standard.h:126
Definition: fat_standard.h:115
uint16_t totalSectors16
Definition: fat_standard.h:102
Definition: fat_standard.h:94
bool fat32_isEndOfChainMarker(uint32_t clusterNumber)
Definition: fat_standard.c:28
uint8_t bootSignature
Definition: fat_standard.h:67
uint16_t creationTime
Definition: fat_standard.h:120
uint32_t fileSize
Definition: fat_standard.h:127
uint8_t jmpBoot[3]
Definition: fat_standard.h:65
uint8_t type
Definition: fat_standard.h:63
uint32_t rootCluster
Definition: fat_standard.h:82
char fileSystemType[8]
Definition: fat_standard.h:70
uint8_t bootSignature
Definition: fat_standard.h:88
uint16_t bytesPerSector
Definition: fat_standard.h:97
bool fat16_isEndOfChainMarker(uint16_t clusterNumber)
Definition: fat_standard.c:22
char oemName[8]
Definition: fat_standard.h:66
#define FAT_FILENAME_LENGTH
Definition: fat_standard.h:47
uint16_t FATSize16
Definition: fat_standard.h:104
char filename[FAT_FILENAME_LENGTH]
Definition: fat_standard.h:65
uint8_t numFATs
Definition: fat_standard.h:100
uint8_t reserved1
Definition: fat_standard.h:66
char volumeLabel[11]
Definition: fat_standard.h:69
uint32_t totalSectors32
Definition: fat_standard.h:108
uint32_t volumeID
Definition: fat_standard.h:89
uint16_t extFlags
Definition: fat_standard.h:66
fat16Descriptor_t fat16
Definition: fat_standard.h:110
uint16_t lastWriteDate
Definition: fat_standard.h:125
uint8_t reserved[12]
Definition: fat_standard.h:71
uint32_t volumeID
Definition: fat_standard.h:68
bool fat_isFreeSpace(uint32_t clusterNumber)
Definition: fat_standard.c:44
uint16_t sectorsPerTrack
Definition: fat_standard.h:105
uint8_t bootFlag
Definition: fat_standard.h:61
struct mbrPartitionEntry_t __attribute__((packed)) mbrPartitionEntry_t
Definition: fat_standard.h:57
uint8_t media
Definition: fat_standard.h:103
Definition: fat_standard.h:56
uint8_t chsBegin[3]
Definition: fat_standard.h:62
uint16_t numHeads
Definition: fat_standard.h:106
Definition: fat_standard.h:55
uint8_t chsEnd[3]
Definition: fat_standard.h:64
bool fat_isDirectoryEntryEmpty(fatDirectoryEntry_t *entry)
Definition: fat_standard.c:54
uint32_t hiddenSectors
Definition: fat_standard.h:107
fat32Descriptor_t fat32
Definition: fat_standard.h:111
uint32_t numSectors
Definition: fat_standard.h:66
fatFilesystemType_e
Definition: fat_standard.h:53
uint8_t driveNumber
Definition: fat_standard.h:65
uint8_t driveNumber
Definition: fat_standard.h:86
Definition: fat_standard.h:60
uint32_t fat32_decodeClusterNumber(uint32_t clusterNumber)
Definition: fat_standard.c:38
Definition: fat_standard.h:69
uint8_t reserved1
Definition: fat_standard.h:87
void fat_convertFilenameToFATStyle(const char *filename, uint8_t *fatFilename)
Definition: fat_standard.c:64
uint16_t creationDate
Definition: fat_standard.h:121
Definition: fat_standard.h:54