3 Copyright (C) Dean Camera, 2013.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com)
12 Permission to use, copy, modify, distribute, and sell this
13 software and its documentation for any purpose is hereby granted
14 without fee, provided that the above copyright notice appear in
15 all copies and that both that the copyright notice and this
16 permission notice and warranty disclaimer appear in supporting
17 documentation, and that the name of the author not be used in
18 advertising or publicity pertaining to distribution of the
19 software without specific, written prior permission.
21 The author disclaims all warranties with regard to this
22 software, including all implied warranties of merchantability
23 and fitness. In no event shall the author be liable for any
24 special, indirect or consequential damages or any damages
25 whatsoever resulting from loss of use, data or profits, whether
26 in an action of contract, negligence or other tortious action,
27 arising out of or in connection with the use or performance of
31 #ifndef _VIRTUALFAT_H_
32 #define _VIRTUALFAT_H_
36 #include <avr/pgmspace.h>
38 #include <LUFA/Drivers/USB/USB.h>
41 /** Size of the virtual FIRMWARE.BIN file in bytes. */
42 #define FIRMWARE_FILE_SIZE_BYTES (FLASHEND - (FLASHEND - BOOT_START_ADDR) + 1UL)
44 /** Number of sectors that comprise a single logical disk cluster. */
45 #define SECTOR_PER_CLUSTER 4
47 /** Size of a single logical sector on the disk. */
48 #define SECTOR_SIZE_BYTES 512
50 /** Size of a logical cluster on the disk, in bytes */
51 #define CLUSTER_SIZE_BYTES (SECTOR_PER_CLUSTER * SECTOR_SIZE_BYTES)
53 /** Number of sectors required to store a given size in bytes.
55 * \param[in] size Size of the data that needs to be stored
57 * \return Number of sectors required to store the given data on the disk.
59 #define FILE_SECTORS(size) ((size / SECTOR_SIZE_BYTES) + ((size % SECTOR_SIZE_BYTES) ? 1 : 0))
61 /** Number of clusters required to store a given size in bytes.
63 * \param[in] size Size of the data that needs to be stored
65 * \return Number of clusters required to store the given data on the disk.
67 #define FILE_CLUSTERS(size) ((size / CLUSTER_SIZE_BYTES) + ((size % CLUSTER_SIZE_BYTES) ? 1 : 0))
69 /** Total number of logical sectors/blocks on the disk. */
70 #define LUN_MEDIA_BLOCKS (FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES) + 32)
72 /** Converts a given time in HH:MM:SS format to a FAT filesystem time.
74 * \note The minimum seconds resolution of FAT is 2, thus odd seconds
75 * will be truncated to the previous integer multiple of 2 seconds.
77 * \param[in] hh Hours (0-23)
78 * \param[in] mm Minutes (0-59)
79 * \param[in] ss Seconds (0-59)
81 * \return Given time encoded as a FAT filesystem timestamp
83 #define FAT_TIME(h, m, s) ((hh << 11) | (mm << 5) | (ss >> 1))
85 /** Converts a given date in DD/MM/YYYY format to a FAT filesystem date.
87 * \param[in] dd Days in the month (1-31)
88 * \param[in] mm Months in the year (1-12)
89 * \param[in] yyyy Year (1980 - 2107)
91 * \return Given date encoded as a FAT filesystem datestamp
93 #define FAT_DATE(d, m, y) (((yyyy - 1980) << 9) | (mm << 5) | (dd << 0))
95 /* Type Definitions: */
96 /** FAT boot block structure definition, used to identify the core
97 * parameters of a FAT filesystem stored on a disk.
99 * \note This definition is truncated to save space; the magic signature
100 * 0xAA55 must be appended to the very end of the block for it to
101 * be detected by the host as a valid boot block.
105 uint8_t Bootstrap
[3];
106 uint8_t Description
[8];
108 uint8_t SectorsPerCluster
;
109 uint16_t ReservedSectors
;
111 uint16_t RootDirectoryEntries
;
112 uint16_t TotalSectors16
;
113 uint8_t MediaDescriptor
;
114 uint16_t SectorsPerFAT
;
115 uint16_t SectorsPerTrack
;
117 uint32_t HiddenSectors
;
118 uint32_t TotalSectors32
;
119 uint16_t PhysicalDriveNum
;
120 uint8_t ExtendedBootRecordSig
;
121 uint32_t VolumeSerialNumber
;
122 uint8_t VolumeLabel
[11];
123 uint8_t FilesystemIdentifier
[8];
124 /* uint8_t BootstrapProgram[448]; */
125 /* uint16_t MagicSignature; */
128 /** FAT legacy 8.3 style directory entry structure definition, used to
129 * identify the files and folders of FAT filesystem stored on a disk.
134 uint8_t Extension
[3];
136 uint8_t Reserved
[10];
137 uint16_t CreationTime
;
138 uint16_t CreationDate
;
139 uint16_t StartingCluster
;
140 uint32_t FileSizeBytes
;
141 } FATDirectoryEntry_t
;
143 /* Function Prototypes: */
144 #if defined(INCLUDE_FROM_VIRTUAL_FAT_C)
145 static void UpdateFAT12ClusterEntry(uint8_t* const FATTable
,
146 const uint16_t Index
,
147 const uint16_t ChainEntry
);
148 static void WriteVirtualBlock(const uint16_t BlockNumber
);
149 static void ReadVirtualBlock(const uint16_t BlockNumber
);
152 void VirtualFAT_WriteBlocks(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
,
153 const uint32_t BlockAddress
,
154 uint16_t TotalBlocks
);
156 void VirtualFAT_ReadBlocks(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
,
157 const uint32_t BlockAddress
,
158 uint16_t TotalBlocks
);