embkernel
 All Classes Functions Variables Typedefs Groups Pages
FsDefs.hpp
1 //------------------------------------------------------------------------------
2 //This file is part of embKernel.
3 //See license.txt for the full license governing this code.
4 //------------------------------------------------------------------------------
5 
6 #ifndef FS_DEFS_HPP_
7 #define FS_DEFS_HPP_
8 
9 #include <stdint.h>
10 #include <stddef.h>
11 
12 class FsDefs {
13  friend class Fs;
14  friend class FsFile;
15  friend class FsDir;
16  friend class FsPartition;
17  friend class FsDrive;
18 
19 public:
20  const static size_t MAX_LFN_CHARS = 255;
21 
22  typedef enum {
23  RES_SUCCESS = 0x00,
24  RES_EOF = 0x01,
25  RES_DISK_ERROR = 0x02,
26  RES_NOT_FORMATTED = 0x03,
27  RES_OUT_OF_MEMORY = 0x04,
28  RES_INVALID_PATH = 0x05,
29  RES_INVALID_PARTITION = 0x06,
30  RES_ENTRY_NOT_FOUND = 0x07,
31  RES_FILE_ALREADY_OPEN = 0x08,
32  RES_DENIED = 0x09,
33  RES_DISK_FULL = 0x0A,
34  RES_ENTRY_NOT_OPEN = 0x0B,
35  RES_FOLDER_NOT_EMPTY = 0x0C,
36  RES_NOT_IMPLEMENTED = 0x0D,
37  RES_DRIVE_ID_INVALID = 0x0E,
38  RES_NO_MORE_SFN_AVAILABLE = 0x0F,
39  } RESULT;
40 
41  static const char* getResultString(RESULT result);
42 
43  const static size_t SECTOR_BYTES = 512;
44  const static size_t SECTOR_DWORDS = 512 / 4;
45  const static size_t DIRECTORIES_TABLES_PER_SECTOR = 16;
46  const static size_t MAX_LFN_LENGTH = 255;
47  const static uint32_t FAT_FREE_CLUSTER = 0;
48  const static uint32_t FAT_LAST_CLUSTER = 0xFFFFFFFF;
49  const static uint32_t FAT_MAX_CLUSTER_VALUE = 0xFFFFFFF6;
50  const static uint8_t ENTRY_LAST = 0;
51  const static uint8_t ENTRY_UNUSED = 0xE5;
52 
53 private:
54  typedef enum
55  :uint8_t {
56  PT_UNUSED = 0x00,
57  PT_FAT12 = 0x01,
58  PT_FAT16 = 0x04,
59  PT_EXTENDED_CHS = 0x05,
60  PT_FAT16B = 0x06,
61  PT_NTFS = 0x07,
62  PT_FAT32 = 0x0B,
63  PT_FAT32X = 0x0C,
64  PT_FAT16X = 0x0E,
65  PT_EXTENDED_LBA = 0x0F,
66  PT_PARTITION_EISA = 0x12,
67  PT_DYNAMIC_DISK_VOLUME = 0x42,
68  PT_LEGACY_FAT16 = 0x86,
69  PT_LEGACY_NTFS = 0x87,
70  PT_LEGACY_FAT32 = 0x8B,
71  PT_LEGACY_PARTITION_EISA = 0x8C,
72  } PARTITION_TYPE;
73 
74  typedef struct {
75  uint8_t bootFlag;
76  uint8_t chsBegin[3];
77  PARTITION_TYPE partitionType;
78  uint8_t chsEnd[3];
79  uint32_t lbaBegin;
80  uint32_t numberOfSectors;
81  }__attribute__((packed)) PARTITION_ENTRY;
82 
83  typedef struct {
84  uint8_t jmpBoot[3];
85  uint8_t oemName[8];
86  uint16_t bytsPerSec;
87  uint8_t secPerClus;
88  uint16_t rsvdSecCnt;
89  uint8_t numFats;
90  uint16_t rootEntCnt;
91  uint16_t totSec16;
92  uint8_t media;
93  uint16_t fatSz16;
94  uint16_t secPerTrk;
95  uint16_t numHeads;
96  uint32_t hiddSec;
97  uint32_t totSec32;
98  union {
99  struct {
100  uint8_t drvNum;
101  uint8_t reserved1;
102  uint8_t bootSig;
103  uint8_t volId[4];
104  uint8_t volLab[11];
105  uint8_t filSysType[8];
106  uint8_t reserved2[448];
107  uint16_t checkAA55;
108  } fat16;
109  struct {
110  uint32_t fatSz32;
111  uint16_t extFlags;
112  uint16_t fsVer;
113  uint32_t rootClus;
114  uint16_t fsInfo;
115  uint16_t bkBootSec;
116  uint8_t reserved1[12];
117  uint8_t drvNum;
118  uint8_t reserved2;
119  uint8_t bootSig;
120  uint8_t volId[4];
121  uint8_t volLab[11];
122  uint8_t filSysType[8];
123  uint8_t reserved3[420];
124  uint16_t checkAA55;
125  } fat32;
126  } type;
127  }__attribute__((packed)) BOOT_SECTOR;
128 
129  typedef struct {
130  uint32_t leadSig; //0x41615252
131  uint8_t reserverd1[480];
132  uint32_t structSig; //0x61417272
133  uint32_t freeCount;
134  uint32_t nextFree;
135  uint8_t reserved2[14];
136  uint16_t checkAA55; //0xAA55
137  }__attribute__((packed)) FS_INFO;
138 
139  typedef union {
140  struct {
141  uint16_t seconds :5; //2-seconds count
142  uint16_t minutes :6;
143  uint16_t hours :5;
144  } bits;
145  uint16_t value;
146  } TIME;
147 
148  typedef union {
149  struct {
150  uint16_t day :5;
151  uint16_t month :4;
152  uint16_t year :7;
153  } bits;
154  uint16_t value;
155  } DATE;
156 
157  typedef union {
158  struct {
159  uint8_t readOnly :1;
160  uint8_t hidden :1;
161  uint8_t system :1;
162  uint8_t volumeId :1;
163  uint8_t directory :1;
164  uint8_t archive :1;
165  uint8_t zero2 :1;
166  uint8_t zero1 :1;
167  } bits;
168  uint8_t value;
169  } ATTRIBUTES;
170 
171  typedef struct {
172  char name[8];
173  char ext[3];
174  ATTRIBUTES attributes;
175  uint8_t reserved;
176  uint8_t createTimeMs;
177  TIME createTime;
178  DATE createDate;
179  DATE lastAccess;
180  uint16_t firstClusterHi;
181  TIME lastModifiedTime;
182  DATE lastModifiedDate;
183  uint16_t firstClusterLo;
184  uint32_t fileSize;
185  }__attribute__((packed)) SHORT_DIRECTORY_ENTRY;
186 
187  typedef struct {
188  uint8_t order;
189  uint8_t name1[10];
190  ATTRIBUTES attributes;
191  uint8_t type;
192  uint8_t checksum;
193  uint8_t name2[12];
194  uint16_t firstCluster;
195  uint8_t name3[4];
196  }__attribute__((packed)) LONG_DIRECTORY_ENTRY;
197 
198  const static size_t LFN_MAX_CHARS_PER_ENTRY = 13;
199 
200  typedef union {
201  SHORT_DIRECTORY_ENTRY shortEntry;
202  LONG_DIRECTORY_ENTRY longEntry;
203  } DIRECTORY_ENTRY;
204 
205  typedef struct {
206  uint8_t boot[446];
207  PARTITION_ENTRY partitionEntries[4];
208  uint16_t checkAA55;
209  }__attribute__((packed)) MBR;
210 
211 public:
212  typedef union {
213  uint8_t bytes[SECTOR_BYTES];
214  uint32_t dwords[SECTOR_DWORDS];
215  DIRECTORY_ENTRY dirEntries[DIRECTORIES_TABLES_PER_SECTOR];
216  } SECTOR;
217 };
218 
219 #endif /* FS_DEFS_HPP_ */