SdFatStructs.h

changeset 0
2c8ba1964db7
equal deleted inserted replaced
-1:000000000000 0:2c8ba1964db7
1 /* Arduino SdFat Library
2 * Copyright (C) 2009 by William Greiman
3 *
4 * This file is part of the Arduino SdFat Library
5 *
6 * This Library is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This Library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with the Arduino SdFat Library. If not, see
18 * <http://www.gnu.org/licenses/>.
19 */
20 #include "Marlin.h"
21 #ifdef SDSUPPORT
22
23 #ifndef SdFatStructs_h
24 #define SdFatStructs_h
25 /**
26 * \file
27 * \brief FAT file structures
28 */
29 /*
30 * mostly from Microsoft document fatgen103.doc
31 * http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
32 */
33 //------------------------------------------------------------------------------
34 /** Value for byte 510 of boot block or MBR */
35 uint8_t const BOOTSIG0 = 0X55;
36 /** Value for byte 511 of boot block or MBR */
37 uint8_t const BOOTSIG1 = 0XAA;
38 /** Value for bootSignature field int FAT/FAT32 boot sector */
39 uint8_t const EXTENDED_BOOT_SIG = 0X29;
40 //------------------------------------------------------------------------------
41 /**
42 * \struct partitionTable
43 * \brief MBR partition table entry
44 *
45 * A partition table entry for a MBR formatted storage device.
46 * The MBR partition table has four entries.
47 */
48 struct partitionTable {
49 /**
50 * Boot Indicator . Indicates whether the volume is the active
51 * partition. Legal values include: 0X00. Do not use for booting.
52 * 0X80 Active partition.
53 */
54 uint8_t boot;
55 /**
56 * Head part of Cylinder-head-sector address of the first block in
57 * the partition. Legal values are 0-255. Only used in old PC BIOS.
58 */
59 uint8_t beginHead;
60 /**
61 * Sector part of Cylinder-head-sector address of the first block in
62 * the partition. Legal values are 1-63. Only used in old PC BIOS.
63 */
64 unsigned beginSector : 6;
65 /** High bits cylinder for first block in partition. */
66 unsigned beginCylinderHigh : 2;
67 /**
68 * Combine beginCylinderLow with beginCylinderHigh. Legal values
69 * are 0-1023. Only used in old PC BIOS.
70 */
71 uint8_t beginCylinderLow;
72 /**
73 * Partition type. See defines that begin with PART_TYPE_ for
74 * some Microsoft partition types.
75 */
76 uint8_t type;
77 /**
78 * head part of cylinder-head-sector address of the last sector in the
79 * partition. Legal values are 0-255. Only used in old PC BIOS.
80 */
81 uint8_t endHead;
82 /**
83 * Sector part of cylinder-head-sector address of the last sector in
84 * the partition. Legal values are 1-63. Only used in old PC BIOS.
85 */
86 unsigned endSector : 6;
87 /** High bits of end cylinder */
88 unsigned endCylinderHigh : 2;
89 /**
90 * Combine endCylinderLow with endCylinderHigh. Legal values
91 * are 0-1023. Only used in old PC BIOS.
92 */
93 uint8_t endCylinderLow;
94 /** Logical block address of the first block in the partition. */
95 uint32_t firstSector;
96 /** Length of the partition, in blocks. */
97 uint32_t totalSectors;
98 };
99 /** Type name for partitionTable */
100 typedef struct partitionTable part_t;
101 //------------------------------------------------------------------------------
102 /**
103 * \struct masterBootRecord
104 *
105 * \brief Master Boot Record
106 *
107 * The first block of a storage device that is formatted with a MBR.
108 */
109 struct masterBootRecord {
110 /** Code Area for master boot program. */
111 uint8_t codeArea[440];
112 /** Optional Windows NT disk signature. May contain boot code. */
113 uint32_t diskSignature;
114 /** Usually zero but may be more boot code. */
115 uint16_t usuallyZero;
116 /** Partition tables. */
117 part_t part[4];
118 /** First MBR signature byte. Must be 0X55 */
119 uint8_t mbrSig0;
120 /** Second MBR signature byte. Must be 0XAA */
121 uint8_t mbrSig1;
122 };
123 /** Type name for masterBootRecord */
124 typedef struct masterBootRecord mbr_t;
125 //------------------------------------------------------------------------------
126 /**
127 * \struct fat_boot
128 *
129 * \brief Boot sector for a FAT12/FAT16 volume.
130 *
131 */
132 struct fat_boot {
133 /**
134 * The first three bytes of the boot sector must be valid,
135 * executable x 86-based CPU instructions. This includes a
136 * jump instruction that skips the next nonexecutable bytes.
137 */
138 uint8_t jump[3];
139 /**
140 * This is typically a string of characters that identifies
141 * the operating system that formatted the volume.
142 */
143 char oemId[8];
144 /**
145 * The size of a hardware sector. Valid decimal values for this
146 * field are 512, 1024, 2048, and 4096. For most disks used in
147 * the United States, the value of this field is 512.
148 */
149 uint16_t bytesPerSector;
150 /**
151 * Number of sectors per allocation unit. This value must be a
152 * power of 2 that is greater than 0. The legal values are
153 * 1, 2, 4, 8, 16, 32, 64, and 128. 128 should be avoided.
154 */
155 uint8_t sectorsPerCluster;
156 /**
157 * The number of sectors preceding the start of the first FAT,
158 * including the boot sector. The value of this field is always 1.
159 */
160 uint16_t reservedSectorCount;
161 /**
162 * The number of copies of the FAT on the volume.
163 * The value of this field is always 2.
164 */
165 uint8_t fatCount;
166 /**
167 * For FAT12 and FAT16 volumes, this field contains the count of
168 * 32-byte directory entries in the root directory. For FAT32 volumes,
169 * this field must be set to 0. For FAT12 and FAT16 volumes, this
170 * value should always specify a count that when multiplied by 32
171 * results in a multiple of bytesPerSector. FAT16 volumes should
172 * use the value 512.
173 */
174 uint16_t rootDirEntryCount;
175 /**
176 * This field is the old 16-bit total count of sectors on the volume.
177 * This count includes the count of all sectors in all four regions
178 * of the volume. This field can be 0; if it is 0, then totalSectors32
179 * must be nonzero. For FAT32 volumes, this field must be 0. For
180 * FAT12 and FAT16 volumes, this field contains the sector count, and
181 * totalSectors32 is 0 if the total sector count fits
182 * (is less than 0x10000).
183 */
184 uint16_t totalSectors16;
185 /**
186 * This dates back to the old MS-DOS 1.x media determination and is
187 * no longer usually used for anything. 0xF8 is the standard value
188 * for fixed (nonremovable) media. For removable media, 0xF0 is
189 * frequently used. Legal values are 0xF0 or 0xF8-0xFF.
190 */
191 uint8_t mediaType;
192 /**
193 * Count of sectors occupied by one FAT on FAT12/FAT16 volumes.
194 * On FAT32 volumes this field must be 0, and sectorsPerFat32
195 * contains the FAT size count.
196 */
197 uint16_t sectorsPerFat16;
198 /** Sectors per track for interrupt 0x13. Not used otherwise. */
199 uint16_t sectorsPerTrack;
200 /** Number of heads for interrupt 0x13. Not used otherwise. */
201 uint16_t headCount;
202 /**
203 * Count of hidden sectors preceding the partition that contains this
204 * FAT volume. This field is generally only relevant for media
205 * visible on interrupt 0x13.
206 */
207 uint32_t hidddenSectors;
208 /**
209 * This field is the new 32-bit total count of sectors on the volume.
210 * This count includes the count of all sectors in all four regions
211 * of the volume. This field can be 0; if it is 0, then
212 * totalSectors16 must be nonzero.
213 */
214 uint32_t totalSectors32;
215 /**
216 * Related to the BIOS physical drive number. Floppy drives are
217 * identified as 0x00 and physical hard disks are identified as
218 * 0x80, regardless of the number of physical disk drives.
219 * Typically, this value is set prior to issuing an INT 13h BIOS
220 * call to specify the device to access. The value is only
221 * relevant if the device is a boot device.
222 */
223 uint8_t driveNumber;
224 /** used by Windows NT - should be zero for FAT */
225 uint8_t reserved1;
226 /** 0X29 if next three fields are valid */
227 uint8_t bootSignature;
228 /**
229 * A random serial number created when formatting a disk,
230 * which helps to distinguish between disks.
231 * Usually generated by combining date and time.
232 */
233 uint32_t volumeSerialNumber;
234 /**
235 * A field once used to store the volume label. The volume label
236 * is now stored as a special file in the root directory.
237 */
238 char volumeLabel[11];
239 /**
240 * A field with a value of either FAT, FAT12 or FAT16,
241 * depending on the disk format.
242 */
243 char fileSystemType[8];
244 /** X86 boot code */
245 uint8_t bootCode[448];
246 /** must be 0X55 */
247 uint8_t bootSectorSig0;
248 /** must be 0XAA */
249 uint8_t bootSectorSig1;
250 };
251 /** Type name for FAT Boot Sector */
252 typedef struct fat_boot fat_boot_t;
253 //------------------------------------------------------------------------------
254 /**
255 * \struct fat32_boot
256 *
257 * \brief Boot sector for a FAT32 volume.
258 *
259 */
260 struct fat32_boot {
261 /**
262 * The first three bytes of the boot sector must be valid,
263 * executable x 86-based CPU instructions. This includes a
264 * jump instruction that skips the next nonexecutable bytes.
265 */
266 uint8_t jump[3];
267 /**
268 * This is typically a string of characters that identifies
269 * the operating system that formatted the volume.
270 */
271 char oemId[8];
272 /**
273 * The size of a hardware sector. Valid decimal values for this
274 * field are 512, 1024, 2048, and 4096. For most disks used in
275 * the United States, the value of this field is 512.
276 */
277 uint16_t bytesPerSector;
278 /**
279 * Number of sectors per allocation unit. This value must be a
280 * power of 2 that is greater than 0. The legal values are
281 * 1, 2, 4, 8, 16, 32, 64, and 128. 128 should be avoided.
282 */
283 uint8_t sectorsPerCluster;
284 /**
285 * The number of sectors preceding the start of the first FAT,
286 * including the boot sector. Must not be zero
287 */
288 uint16_t reservedSectorCount;
289 /**
290 * The number of copies of the FAT on the volume.
291 * The value of this field is always 2.
292 */
293 uint8_t fatCount;
294 /**
295 * FAT12/FAT16 only. For FAT32 volumes, this field must be set to 0.
296 */
297 uint16_t rootDirEntryCount;
298 /**
299 * For FAT32 volumes, this field must be 0.
300 */
301 uint16_t totalSectors16;
302 /**
303 * This dates back to the old MS-DOS 1.x media determination and is
304 * no longer usually used for anything. 0xF8 is the standard value
305 * for fixed (nonremovable) media. For removable media, 0xF0 is
306 * frequently used. Legal values are 0xF0 or 0xF8-0xFF.
307 */
308 uint8_t mediaType;
309 /**
310 * On FAT32 volumes this field must be 0, and sectorsPerFat32
311 * contains the FAT size count.
312 */
313 uint16_t sectorsPerFat16;
314 /** Sectors per track for interrupt 0x13. Not used otherwise. */
315 uint16_t sectorsPerTrack;
316 /** Number of heads for interrupt 0x13. Not used otherwise. */
317 uint16_t headCount;
318 /**
319 * Count of hidden sectors preceding the partition that contains this
320 * FAT volume. This field is generally only relevant for media
321 * visible on interrupt 0x13.
322 */
323 uint32_t hidddenSectors;
324 /**
325 * Contains the total number of sectors in the FAT32 volume.
326 */
327 uint32_t totalSectors32;
328 /**
329 * Count of sectors occupied by one FAT on FAT32 volumes.
330 */
331 uint32_t sectorsPerFat32;
332 /**
333 * This field is only defined for FAT32 media and does not exist on
334 * FAT12 and FAT16 media.
335 * Bits 0-3 -- Zero-based number of active FAT.
336 * Only valid if mirroring is disabled.
337 * Bits 4-6 -- Reserved.
338 * Bit 7 -- 0 means the FAT is mirrored at runtime into all FATs.
339 * -- 1 means only one FAT is active; it is the one referenced
340 * in bits 0-3.
341 * Bits 8-15 -- Reserved.
342 */
343 uint16_t fat32Flags;
344 /**
345 * FAT32 version. High byte is major revision number.
346 * Low byte is minor revision number. Only 0.0 define.
347 */
348 uint16_t fat32Version;
349 /**
350 * Cluster number of the first cluster of the root directory for FAT32.
351 * This usually 2 but not required to be 2.
352 */
353 uint32_t fat32RootCluster;
354 /**
355 * Sector number of FSINFO structure in the reserved area of the
356 * FAT32 volume. Usually 1.
357 */
358 uint16_t fat32FSInfo;
359 /**
360 * If nonzero, indicates the sector number in the reserved area
361 * of the volume of a copy of the boot record. Usually 6.
362 * No value other than 6 is recommended.
363 */
364 uint16_t fat32BackBootBlock;
365 /**
366 * Reserved for future expansion. Code that formats FAT32 volumes
367 * should always set all of the bytes of this field to 0.
368 */
369 uint8_t fat32Reserved[12];
370 /**
371 * Related to the BIOS physical drive number. Floppy drives are
372 * identified as 0x00 and physical hard disks are identified as
373 * 0x80, regardless of the number of physical disk drives.
374 * Typically, this value is set prior to issuing an INT 13h BIOS
375 * call to specify the device to access. The value is only
376 * relevant if the device is a boot device.
377 */
378 uint8_t driveNumber;
379 /** used by Windows NT - should be zero for FAT */
380 uint8_t reserved1;
381 /** 0X29 if next three fields are valid */
382 uint8_t bootSignature;
383 /**
384 * A random serial number created when formatting a disk,
385 * which helps to distinguish between disks.
386 * Usually generated by combining date and time.
387 */
388 uint32_t volumeSerialNumber;
389 /**
390 * A field once used to store the volume label. The volume label
391 * is now stored as a special file in the root directory.
392 */
393 char volumeLabel[11];
394 /**
395 * A text field with a value of FAT32.
396 */
397 char fileSystemType[8];
398 /** X86 boot code */
399 uint8_t bootCode[420];
400 /** must be 0X55 */
401 uint8_t bootSectorSig0;
402 /** must be 0XAA */
403 uint8_t bootSectorSig1;
404 };
405 /** Type name for FAT32 Boot Sector */
406 typedef struct fat32_boot fat32_boot_t;
407 //------------------------------------------------------------------------------
408 /** Lead signature for a FSINFO sector */
409 uint32_t const FSINFO_LEAD_SIG = 0x41615252;
410 /** Struct signature for a FSINFO sector */
411 uint32_t const FSINFO_STRUCT_SIG = 0x61417272;
412 /**
413 * \struct fat32_fsinfo
414 *
415 * \brief FSINFO sector for a FAT32 volume.
416 *
417 */
418 struct fat32_fsinfo {
419 /** must be 0X52, 0X52, 0X61, 0X41 */
420 uint32_t leadSignature;
421 /** must be zero */
422 uint8_t reserved1[480];
423 /** must be 0X72, 0X72, 0X41, 0X61 */
424 uint32_t structSignature;
425 /**
426 * Contains the last known free cluster count on the volume.
427 * If the value is 0xFFFFFFFF, then the free count is unknown
428 * and must be computed. Any other value can be used, but is
429 * not necessarily correct. It should be range checked at least
430 * to make sure it is <= volume cluster count.
431 */
432 uint32_t freeCount;
433 /**
434 * This is a hint for the FAT driver. It indicates the cluster
435 * number at which the driver should start looking for free clusters.
436 * If the value is 0xFFFFFFFF, then there is no hint and the driver
437 * should start looking at cluster 2.
438 */
439 uint32_t nextFree;
440 /** must be zero */
441 uint8_t reserved2[12];
442 /** must be 0X00, 0X00, 0X55, 0XAA */
443 uint8_t tailSignature[4];
444 };
445 /** Type name for FAT32 FSINFO Sector */
446 typedef struct fat32_fsinfo fat32_fsinfo_t;
447 //------------------------------------------------------------------------------
448 // End Of Chain values for FAT entries
449 /** FAT12 end of chain value used by Microsoft. */
450 uint16_t const FAT12EOC = 0XFFF;
451 /** Minimum value for FAT12 EOC. Use to test for EOC. */
452 uint16_t const FAT12EOC_MIN = 0XFF8;
453 /** FAT16 end of chain value used by Microsoft. */
454 uint16_t const FAT16EOC = 0XFFFF;
455 /** Minimum value for FAT16 EOC. Use to test for EOC. */
456 uint16_t const FAT16EOC_MIN = 0XFFF8;
457 /** FAT32 end of chain value used by Microsoft. */
458 uint32_t const FAT32EOC = 0X0FFFFFFF;
459 /** Minimum value for FAT32 EOC. Use to test for EOC. */
460 uint32_t const FAT32EOC_MIN = 0X0FFFFFF8;
461 /** Mask a for FAT32 entry. Entries are 28 bits. */
462 uint32_t const FAT32MASK = 0X0FFFFFFF;
463 //------------------------------------------------------------------------------
464 /**
465 * \struct directoryEntry
466 * \brief FAT short directory entry
467 *
468 * Short means short 8.3 name, not the entry size.
469 *
470 * Date Format. A FAT directory entry date stamp is a 16-bit field that is
471 * basically a date relative to the MS-DOS epoch of 01/01/1980. Here is the
472 * format (bit 0 is the LSB of the 16-bit word, bit 15 is the MSB of the
473 * 16-bit word):
474 *
475 * Bits 9-15: Count of years from 1980, valid value range 0-127
476 * inclusive (1980-2107).
477 *
478 * Bits 5-8: Month of year, 1 = January, valid value range 1-12 inclusive.
479 *
480 * Bits 0-4: Day of month, valid value range 1-31 inclusive.
481 *
482 * Time Format. A FAT directory entry time stamp is a 16-bit field that has
483 * a granularity of 2 seconds. Here is the format (bit 0 is the LSB of the
484 * 16-bit word, bit 15 is the MSB of the 16-bit word).
485 *
486 * Bits 11-15: Hours, valid value range 0-23 inclusive.
487 *
488 * Bits 5-10: Minutes, valid value range 0-59 inclusive.
489 *
490 * Bits 0-4: 2-second count, valid value range 0-29 inclusive (0 - 58 seconds).
491 *
492 * The valid time range is from Midnight 00:00:00 to 23:59:58.
493 */
494 struct directoryEntry {
495 /** Short 8.3 name.
496 *
497 * The first eight bytes contain the file name with blank fill.
498 * The last three bytes contain the file extension with blank fill.
499 */
500 uint8_t name[11];
501 /** Entry attributes.
502 *
503 * The upper two bits of the attribute byte are reserved and should
504 * always be set to 0 when a file is created and never modified or
505 * looked at after that. See defines that begin with DIR_ATT_.
506 */
507 uint8_t attributes;
508 /**
509 * Reserved for use by Windows NT. Set value to 0 when a file is
510 * created and never modify or look at it after that.
511 */
512 uint8_t reservedNT;
513 /**
514 * The granularity of the seconds part of creationTime is 2 seconds
515 * so this field is a count of tenths of a second and its valid
516 * value range is 0-199 inclusive. (WHG note - seems to be hundredths)
517 */
518 uint8_t creationTimeTenths;
519 /** Time file was created. */
520 uint16_t creationTime;
521 /** Date file was created. */
522 uint16_t creationDate;
523 /**
524 * Last access date. Note that there is no last access time, only
525 * a date. This is the date of last read or write. In the case of
526 * a write, this should be set to the same date as lastWriteDate.
527 */
528 uint16_t lastAccessDate;
529 /**
530 * High word of this entry's first cluster number (always 0 for a
531 * FAT12 or FAT16 volume).
532 */
533 uint16_t firstClusterHigh;
534 /** Time of last write. File creation is considered a write. */
535 uint16_t lastWriteTime;
536 /** Date of last write. File creation is considered a write. */
537 uint16_t lastWriteDate;
538 /** Low word of this entry's first cluster number. */
539 uint16_t firstClusterLow;
540 /** 32-bit unsigned holding this file's size in bytes. */
541 uint32_t fileSize;
542 };
543 //------------------------------------------------------------------------------
544 // Definitions for directory entries
545 //
546 /** Type name for directoryEntry */
547 typedef struct directoryEntry dir_t;
548 /** escape for name[0] = 0XE5 */
549 uint8_t const DIR_NAME_0XE5 = 0X05;
550 /** name[0] value for entry that is free after being "deleted" */
551 uint8_t const DIR_NAME_DELETED = 0XE5;
552 /** name[0] value for entry that is free and no allocated entries follow */
553 uint8_t const DIR_NAME_FREE = 0X00;
554 /** file is read-only */
555 uint8_t const DIR_ATT_READ_ONLY = 0X01;
556 /** File should hidden in directory listings */
557 uint8_t const DIR_ATT_HIDDEN = 0X02;
558 /** Entry is for a system file */
559 uint8_t const DIR_ATT_SYSTEM = 0X04;
560 /** Directory entry contains the volume label */
561 uint8_t const DIR_ATT_VOLUME_ID = 0X08;
562 /** Entry is for a directory */
563 uint8_t const DIR_ATT_DIRECTORY = 0X10;
564 /** Old DOS archive bit for backup support */
565 uint8_t const DIR_ATT_ARCHIVE = 0X20;
566 /** Test value for long name entry. Test is
567 (d->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME. */
568 uint8_t const DIR_ATT_LONG_NAME = 0X0F;
569 /** Test mask for long name entry */
570 uint8_t const DIR_ATT_LONG_NAME_MASK = 0X3F;
571 /** defined attribute bits */
572 uint8_t const DIR_ATT_DEFINED_BITS = 0X3F;
573 /** Directory entry is part of a long name
574 * \param[in] dir Pointer to a directory entry.
575 *
576 * \return true if the entry is for part of a long name else false.
577 */
578 static inline uint8_t DIR_IS_LONG_NAME(const dir_t* dir) {
579 return (dir->attributes & DIR_ATT_LONG_NAME_MASK) == DIR_ATT_LONG_NAME;
580 }
581 /** Mask for file/subdirectory tests */
582 uint8_t const DIR_ATT_FILE_TYPE_MASK = (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY);
583 /** Directory entry is for a file
584 * \param[in] dir Pointer to a directory entry.
585 *
586 * \return true if the entry is for a normal file else false.
587 */
588 static inline uint8_t DIR_IS_FILE(const dir_t* dir) {
589 return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == 0;
590 }
591 /** Directory entry is for a subdirectory
592 * \param[in] dir Pointer to a directory entry.
593 *
594 * \return true if the entry is for a subdirectory else false.
595 */
596 static inline uint8_t DIR_IS_SUBDIR(const dir_t* dir) {
597 return (dir->attributes & DIR_ATT_FILE_TYPE_MASK) == DIR_ATT_DIRECTORY;
598 }
599 /** Directory entry is for a file or subdirectory
600 * \param[in] dir Pointer to a directory entry.
601 *
602 * \return true if the entry is for a normal file or subdirectory else false.
603 */
604 static inline uint8_t DIR_IS_FILE_OR_SUBDIR(const dir_t* dir) {
605 return (dir->attributes & DIR_ATT_VOLUME_ID) == 0;
606 }
607 #endif // SdFatStructs_h
608
609
610 #endif

mercurial