Phoenard
PHNSDMinimal_fat.h
Go to the documentation of this file.
1 /*
2 The MIT License (MIT)
3 
4 This file is part of the Phoenard Arduino library
5 Copyright (c) 2014 Phoenard
6 
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE.
24 */
25 
26 #ifndef _SD_MINIMAL_FAT_H_
27 #define _SD_MINIMAL_FAT_H_
28 
29 #include <stdint.h>
30 
31 namespace SDMINFAT {
32 
37 /*
38  * mostly from Microsoft document fatgen103.doc
39  * http://www.microsoft.com/whdc/system/platform/firmware/fatgen.mspx
40  */
41 
42 /* card types */
44 uint8_t const SD_CARD_TYPE_SD1 = 1;
46 uint8_t const SD_CARD_TYPE_SD2 = 2;
48 uint8_t const SD_CARD_TYPE_SDHC = 3;
49 
51 uint16_t const FAT_DEFAULT_DATE = ((2000 - 1980) << 9) | (1 << 5) | 1;
53 uint16_t const FAT_DEFAULT_TIME = (1 << 11);
54 
56 uint8_t const BOOTSIG0 = 0X55;
58 uint8_t const BOOTSIG1 = 0XAA;
59 
73  uint8_t boot;
78  uint8_t beginHead;
83  unsigned beginSector : 6;
85  unsigned beginCylinderHigh : 2;
95  uint8_t type;
100  uint8_t endHead;
105  unsigned endSector : 6;
107  unsigned endCylinderHigh : 2;
112  uint8_t endCylinderLow;
114  uint32_t firstSector;
116  uint32_t totalSectors;
117 };
119 typedef struct partitionTable part_t;
120 
130  uint8_t codeArea[440];
132  uint32_t diskSignature;
134  uint16_t usuallyZero;
138  uint8_t mbrSig0;
140  uint8_t mbrSig1;
141 };
143 typedef struct masterBootRecord mbr_t;
144 
157  uint16_t bytesPerSector;
172  uint8_t fatCount;
191  uint16_t totalSectors16;
198  uint8_t mediaType;
204  uint16_t sectorsPerFat16;
208  uint16_t headCount;
214  uint32_t hidddenSectors;
221  uint32_t totalSectors32;
225  uint32_t sectorsPerFat32;
236  uint16_t fat32Flags;
241  uint16_t fat32Version;
251  uint16_t fat32FSInfo;
262  uint8_t fat32Reserved[12];
263 };
265 typedef struct biosParmBlock bpb_t;
266 
275  uint8_t jmpToBootCode[3];
277  char oemName[8];
281  uint8_t driveNumber;
283  uint8_t reserved1;
285  uint8_t bootSignature;
289  char volumeLabel[11];
291  char fileSystemType[8];
293  uint8_t bootCode[420];
295  uint8_t bootSectorSig0;
297  uint8_t bootSectorSig1;
298 };
299 
300 /* End Of Chain values for FAT entries */
302 uint16_t const FAT16EOC = 0XFFFF;
304 uint16_t const FAT16EOC_MIN = 0XFFF8;
306 uint32_t const FAT32EOC = 0X0FFFFFFF;
308 uint32_t const FAT32EOC_MIN = 0X0FFFFFF8;
310 uint32_t const FAT32MASK = 0X0FFFFFFF;
311 
313 typedef struct fat32BootSector fbs_t;
314 
316 uint8_t const DIR_NAME_DELETED = 0XE5;
318 uint8_t const DIR_NAME_FREE = 0X00;
320 uint8_t const DIR_ATT_READ_ONLY = 0X01;
322 uint8_t const DIR_ATT_HIDDEN = 0X02;
324 uint8_t const DIR_ATT_SYSTEM = 0X04;
326 uint8_t const DIR_ATT_VOLUME_ID = 0X08;
328 uint8_t const DIR_ATT_DIRECTORY = 0X10;
330 uint8_t const DIR_ATT_NOT_FILE_MASK = (DIR_ATT_VOLUME_ID | DIR_ATT_DIRECTORY);
331 
368  uint8_t name[11];
375  uint8_t attributes;
380  uint8_t reservedNT;
388  uint16_t creationTime;
390  uint16_t creationDate;
396  uint16_t lastAccessDate;
403  uint16_t lastWriteTime;
405  uint16_t lastWriteDate;
407  uint16_t firstClusterLow;
409  uint32_t fileSize;
410 
412  uint32_t firstCluster() {
413  return (uint32_t) firstClusterHigh << 16 | (uint32_t) firstClusterLow;
414  }
416  void setFirstCluster(uint32_t firstCluster) {
417  firstClusterLow = firstCluster & 0XFFFF;
418  firstClusterHigh = firstCluster >> 16;
419  }
421  uint8_t isFile() { return !(attributes & DIR_ATT_NOT_FILE_MASK); }
423  uint8_t isReadOnly() { return (attributes & DIR_ATT_READ_ONLY); }
424 };
425 
427 typedef struct directoryEntry dir_t;
428 
429 /* brief Cache for an SD data block */
430 union cache_t {
431  uint8_t data[512]; /* Used to access cached file data blocks. */
432  uint16_t fat16[256]; /* Used to access cached FAT16 entries. */
433  uint32_t fat32[128]; /* Used to access cached FAT32 entries. */
434  dir_t dir[16]; /* Used to access cached directory entries. */
435  mbr_t mbr; /* Used to access a cached MasterBoot Record. */
436  fbs_t fbs; /* Used to access to a cached FAT boot sector. */
437 };
438 
439 /*
440  * Based on the document:
441  *
442  * SD Specifications
443  * Part 1
444  * Physical Layer
445  * Simplified Specification
446  * Version 2.00
447  * September 25, 2006
448  *
449  * www.sdcard.org/developers/tech/sdcard/pls/Simplified_Physical_Layer_Spec.pdf
450  */
451 
452 /* SD card commands */
454 uint8_t const CMD0 = 0X00;
456 uint8_t const CMD8 = 0X08;
458 uint8_t const CMD9 = 0X09;
460 uint8_t const CMD10 = 0X0A;
462 uint8_t const CMD13 = 0X0D;
464 uint8_t const CMD17 = 0X11;
466 uint8_t const CMD24 = 0X18;
468 uint8_t const CMD25 = 0X19;
470 uint8_t const CMD32 = 0X20;
473 uint8_t const CMD33 = 0X21;
475 uint8_t const CMD38 = 0X26;
477 uint8_t const CMD55 = 0X37;
479 uint8_t const CMD58 = 0X3A;
482 uint8_t const ACMD23 = 0X17;
485 uint8_t const ACMD41 = 0X29;
486 
488 uint8_t const R1_READY_STATE = 0X00;
490 uint8_t const R1_IDLE_STATE = 0X01;
492 uint8_t const R1_ILLEGAL_COMMAND = 0X04;
494 uint8_t const DATA_IDLE_BLOCK = 0xFF;
496 uint8_t const DATA_START_BLOCK = 0XFE;
498 uint8_t const STOP_TRAN_TOKEN = 0XFD;
500 uint8_t const WRITE_MULTIPLE_TOKEN = 0XFC;
502 uint8_t const DATA_RES_MASK = 0X1F;
504 uint8_t const DATA_RES_ACCEPTED = 0X05;
505 
506 }
507 
508 #endif /* _SD_MINIMAL_FAT_H_ */
uint8_t driveNumber
Definition: PHNSDMinimal_fat.h:281
uint8_t creationTimeTenths
Definition: PHNSDMinimal_fat.h:386
uint8_t name[11]
Definition: PHNSDMinimal_fat.h:368
uint16_t lastWriteDate
Definition: PHNSDMinimal_fat.h:405
uint16_t lastAccessDate
Definition: PHNSDMinimal_fat.h:396
uint8_t isFile()
Definition: PHNSDMinimal_fat.h:421
uint32_t fat32RootCluster
Definition: PHNSDMinimal_fat.h:246
uint32_t fileSize
Definition: PHNSDMinimal_fat.h:409
Boot sector for a FAT16 or FAT32 volume.
Definition: PHNSDMinimal_fat.h:273
uint8_t attributes
Definition: PHNSDMinimal_fat.h:375
uint16_t creationDate
Definition: PHNSDMinimal_fat.h:390
uint8_t endCylinderLow
Definition: PHNSDMinimal_fat.h:112
uint8_t bootSectorSig1
Definition: PHNSDMinimal_fat.h:297
uint8_t beginCylinderLow
Definition: PHNSDMinimal_fat.h:90
uint8_t mbrSig1
Definition: PHNSDMinimal_fat.h:140
uint16_t sectorsPerTrtack
Definition: PHNSDMinimal_fat.h:206
uint8_t codeArea[440]
Definition: PHNSDMinimal_fat.h:130
FAT short directory entry.
Definition: PHNSDMinimal_fat.h:362
char fileSystemType[8]
Definition: PHNSDMinimal_fat.h:291
uint16_t firstClusterLow
Definition: PHNSDMinimal_fat.h:407
uint8_t boot
Definition: PHNSDMinimal_fat.h:73
uint8_t bootSectorSig0
Definition: PHNSDMinimal_fat.h:295
unsigned endSector
Definition: PHNSDMinimal_fat.h:105
uint8_t jmpToBootCode[3]
Definition: PHNSDMinimal_fat.h:275
uint16_t fat32FSInfo
Definition: PHNSDMinimal_fat.h:251
uint8_t mbrSig0
Definition: PHNSDMinimal_fat.h:138
uint16_t totalSectors16
Definition: PHNSDMinimal_fat.h:191
uint32_t totalSectors32
Definition: PHNSDMinimal_fat.h:221
Definition: PHNSDMinimal_fat.h:430
uint16_t usuallyZero
Definition: PHNSDMinimal_fat.h:134
uint8_t sectorsPerCluster
Definition: PHNSDMinimal_fat.h:163
uint32_t sectorsPerFat32
Definition: PHNSDMinimal_fat.h:225
unsigned endCylinderHigh
Definition: PHNSDMinimal_fat.h:107
uint32_t firstCluster()
Definition: PHNSDMinimal_fat.h:412
uint8_t reserved1
Definition: PHNSDMinimal_fat.h:283
BIOS parameter block.
Definition: PHNSDMinimal_fat.h:152
uint8_t bootSignature
Definition: PHNSDMinimal_fat.h:285
uint16_t lastWriteTime
Definition: PHNSDMinimal_fat.h:403
uint8_t fatCount
Definition: PHNSDMinimal_fat.h:172
uint16_t fat32Flags
Definition: PHNSDMinimal_fat.h:236
uint8_t fat32Reserved[12]
Definition: PHNSDMinimal_fat.h:262
uint16_t sectorsPerFat16
Definition: PHNSDMinimal_fat.h:204
uint16_t rootDirEntryCount
Definition: PHNSDMinimal_fat.h:181
uint32_t firstSector
Definition: PHNSDMinimal_fat.h:114
Definition: PHNSDMinimal_fat.h:31
uint32_t volumeSerialNumber
Definition: PHNSDMinimal_fat.h:287
uint8_t endHead
Definition: PHNSDMinimal_fat.h:100
uint16_t firstClusterHigh
Definition: PHNSDMinimal_fat.h:401
void setFirstCluster(uint32_t firstCluster)
Definition: PHNSDMinimal_fat.h:416
uint8_t bootCode[420]
Definition: PHNSDMinimal_fat.h:293
bpb_t bpb
Definition: PHNSDMinimal_fat.h:279
unsigned beginCylinderHigh
Definition: PHNSDMinimal_fat.h:85
char oemName[8]
Definition: PHNSDMinimal_fat.h:277
Master Boot Record.
Definition: PHNSDMinimal_fat.h:128
uint32_t totalSectors
Definition: PHNSDMinimal_fat.h:116
char volumeLabel[11]
Definition: PHNSDMinimal_fat.h:289
uint32_t diskSignature
Definition: PHNSDMinimal_fat.h:132
uint32_t hidddenSectors
Definition: PHNSDMinimal_fat.h:214
uint16_t reservedSectorCount
Definition: PHNSDMinimal_fat.h:168
uint8_t beginHead
Definition: PHNSDMinimal_fat.h:78
uint8_t isReadOnly()
Definition: PHNSDMinimal_fat.h:423
uint16_t headCount
Definition: PHNSDMinimal_fat.h:208
uint16_t bytesPerSector
Definition: PHNSDMinimal_fat.h:157
uint8_t reservedNT
Definition: PHNSDMinimal_fat.h:380
uint8_t mediaType
Definition: PHNSDMinimal_fat.h:198
uint16_t creationTime
Definition: PHNSDMinimal_fat.h:388
uint16_t fat32Version
Definition: PHNSDMinimal_fat.h:241
uint16_t fat32BackBootBlock
Definition: PHNSDMinimal_fat.h:257
MBR partition table entry.
Definition: PHNSDMinimal_fat.h:67
unsigned beginSector
Definition: PHNSDMinimal_fat.h:83
uint8_t type
Definition: PHNSDMinimal_fat.h:95
part_t part[4]
Definition: PHNSDMinimal_fat.h:136