d01f9be53eb744dab189024d9f2e0765a2e1e8de
[pub/USBasp.git] / Bootloaders / MassStorage / Lib / VirtualFAT.h
1 /*
2 LUFA Library
3 Copyright (C) Dean Camera, 2013.
4
5 dean [at] fourwalledcubicle [dot] com
6 www.lufa-lib.org
7 */
8
9 /*
10 Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
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.
20
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
28 this software.
29 */
30
31 #ifndef _VIRTUALFAT_H_
32 #define _VIRTUALFAT_H_
33
34 /* Includes: */
35 #include <avr/io.h>
36 #include <avr/pgmspace.h>
37
38 #include <LUFA/Drivers/USB/USB.h>
39
40 #include "../BootloaderAPI.h"
41
42 /* Macros: */
43 /** Size of the virtual FIRMWARE.BIN file in bytes. */
44 #define FIRMWARE_FILE_SIZE_BYTES (FLASHEND - (FLASHEND - BOOT_START_ADDR) - AUX_BOOT_SECTION_SIZE)
45
46 /** Number of sectors that comprise a single logical disk cluster. */
47 #define SECTOR_PER_CLUSTER 4
48
49 /** Size of a single logical sector on the disk. */
50 #define SECTOR_SIZE_BYTES 512
51
52 /** Size of a logical cluster on the disk, in bytes */
53 #define CLUSTER_SIZE_BYTES (SECTOR_PER_CLUSTER * SECTOR_SIZE_BYTES)
54
55 /** Number of sectors required to store a given size in bytes.
56 *
57 * \param[in] size Size of the data that needs to be stored
58 *
59 * \return Number of sectors required to store the given data on the disk.
60 */
61 #define FILE_SECTORS(size) ((size / SECTOR_SIZE_BYTES) + ((size % SECTOR_SIZE_BYTES) ? 1 : 0))
62
63 /** Number of clusters required to store a given size in bytes.
64 *
65 * \param[in] size Size of the data that needs to be stored
66 *
67 * \return Number of clusters required to store the given data on the disk.
68 */
69 #define FILE_CLUSTERS(size) ((size / CLUSTER_SIZE_BYTES) + ((size % CLUSTER_SIZE_BYTES) ? 1 : 0))
70
71 /** Total number of logical sectors/blocks on the disk. */
72 #define LUN_MEDIA_BLOCKS (FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES) + 32)
73
74 /** Converts a given time in HH:MM:SS format to a FAT filesystem time.
75 *
76 * \note The minimum seconds resolution of FAT is 2, thus odd seconds
77 * will be truncated to the previous integer multiple of 2 seconds.
78 *
79 * \param[in] hh Hours (0-23)
80 * \param[in] mm Minutes (0-59)
81 * \param[in] ss Seconds (0-59)
82 *
83 * \return Given time encoded as a FAT filesystem timestamp
84 */
85 #define FAT_TIME(hh, mm, ss) ((hh << 11) | (mm << 5) | (ss >> 1))
86
87 /** Converts a given date in DD/MM/YYYY format to a FAT filesystem date.
88 *
89 * \param[in] dd Days in the month (1-31)
90 * \param[in] mm Months in the year (1-12)
91 * \param[in] yyyy Year (1980 - 2107)
92 *
93 * \return Given date encoded as a FAT filesystem datestamp
94 */
95 #define FAT_DATE(dd, mm, yyyy) (((yyyy - 1980) << 9) | (mm << 5) | (dd << 0))
96
97 /** \name FAT Filesystem Flags */
98 //@{
99 /** FAT attribute flag to indicate a read-only file. */
100 #define FAT_FLAG_READONLY (1 << 0)
101
102 /** FAT attribute flag to indicate a hidden file. */
103 #define FAT_FLAG_HIDDEN (1 << 1)
104
105 /** FAT attribute flag to indicate a system file. */
106 #define FAT_FLAG_SYSTEM (1 << 2)
107
108 /** FAT attribute flag to indicate a Volume name entry. */
109 #define FAT_FLAG_VOLUME_NAME (1 << 3)
110
111 /** FAT attribute flag to indicate a directory entry. */
112 #define FAT_FLAG_DIRECTORY (1 << 4)
113
114 /** FAT attribute flag to indicate a file ready for archiving. */
115 #define FAT_FLAG_ARCHIVE (1 << 5)
116
117 /** FAT pseudo-attribute flag to indicate a Long File Name entry. */
118 #define FAT_FLAG_LONG_FILE_NAME 0x0F
119
120 /** Ordinal flag marker for FAT Long File Name entries to mark the last entry. */
121 #define FAT_ORDINAL_LAST_ENTRY (1 << 6)
122 //@}
123
124 /* Enums: */
125 /** Enum for the Root FAT file entry indexes on the disk. This can be used
126 * to retrieve the current contents of a known directory entry.
127 */
128 enum
129 {
130 /** Volume ID directory entry, giving the name of the virtual disk. */
131 DISK_FILE_ENTRY_VolumeID = 0,
132 /** Long File Name FAT file entry of the virtual firmware image file. */
133 DISK_FILE_ENTRY_FirmwareLFN = 1,
134 /** Legacy MSDOS FAT file entry of the virtual firmware image file. */
135 DISK_FILE_ENTRY_FirmwareMSDOS = 2,
136 };
137
138 /** Enum for the physical disk blocks of the virtual disk. */
139 enum
140 {
141 /** Boot sector disk block. */
142 DISK_BLOCK_BootBlock = 0,
143 /** First copy of the FAT table block. */
144 DISK_BLOCK_FATBlock1 = 1,
145 /** Second copy of the FAT table block. */
146 DISK_BLOCK_FATBlock2 = 2,
147 /** Root file and directory entries block. */
148 DISK_BLOCK_RootFilesBlock = 3,
149 /** Start block of the disk data section. */
150 DISK_BLOCK_DataStartBlock = 4,
151 };
152
153 /* Type Definitions: */
154 /** FAT boot block structure definition, used to identify the core
155 * parameters of a FAT file system stored on a disk.
156 *
157 * \note This definition is truncated to save space; the magic signature
158 * \c 0xAA55 must be appended to the very end of the block for it
159 * to be detected by the host as a valid boot block.
160 */
161 typedef struct
162 {
163 uint8_t Bootstrap[3];
164 uint8_t Description[8];
165 uint16_t SectorSize;
166 uint8_t SectorsPerCluster;
167 uint16_t ReservedSectors;
168 uint8_t FATCopies;
169 uint16_t RootDirectoryEntries;
170 uint16_t TotalSectors16;
171 uint8_t MediaDescriptor;
172 uint16_t SectorsPerFAT;
173 uint16_t SectorsPerTrack;
174 uint16_t Heads;
175 uint32_t HiddenSectors;
176 uint32_t TotalSectors32;
177 uint16_t PhysicalDriveNum;
178 uint8_t ExtendedBootRecordSig;
179 uint32_t VolumeSerialNumber;
180 uint8_t VolumeLabel[11];
181 uint8_t FilesystemIdentifier[8];
182 /* uint8_t BootstrapProgram[448]; */
183 /* uint16_t MagicSignature; */
184 } FATBootBlock_t;
185
186 /** FAT directory entry structure, for the various kinds of File and
187 * directory descriptors on a FAT disk.
188 */
189 typedef union
190 {
191 /** VFAT Long File Name file entry. */
192 struct
193 {
194 uint8_t Ordinal;
195 uint16_t Unicode1;
196 uint16_t Unicode2;
197 uint16_t Unicode3;
198 uint16_t Unicode4;
199 uint16_t Unicode5;
200 uint8_t Attribute;
201 uint8_t Reserved1;
202 uint8_t Checksum;
203 uint16_t Unicode6;
204 uint16_t Unicode7;
205 uint16_t Unicode8;
206 uint16_t Unicode9;
207 uint16_t Unicode10;
208 uint16_t Unicode11;
209 uint16_t Reserved2;
210 uint16_t Unicode12;
211 uint16_t Unicode13;
212 } VFAT_LongFileName;
213
214 /** Legacy FAT MSDOS 8.3 file entry. */
215 struct
216 {
217 uint8_t Filename[8];
218 uint8_t Extension[3];
219 uint8_t Attributes;
220 uint8_t Reserved[10];
221 uint16_t CreationTime;
222 uint16_t CreationDate;
223 uint16_t StartingCluster;
224 uint32_t FileSizeBytes;
225 } MSDOS_File;
226
227 /** Legacy FAT MSDOS (sub-)directory entry. */
228 struct
229 {
230 uint8_t Name[11];
231 uint8_t Attributes;
232 uint8_t Reserved[10];
233 uint16_t CreationTime;
234 uint16_t CreationDate;
235 uint16_t StartingCluster;
236 uint32_t Reserved2;
237 } MSDOS_Directory;
238 } FATDirectoryEntry_t;
239
240 /* Function Prototypes: */
241 #if defined(INCLUDE_FROM_VIRTUAL_FAT_C)
242 static void UpdateFAT12ClusterEntry(uint8_t* const FATTable,
243 const uint16_t Index,
244 const uint16_t ChainEntry) AUX_BOOT_SECTION;
245
246 static void ReadWriteFirmwareFileBlock(const uint16_t BlockNumber,
247 uint8_t* BlockBuffer,
248 const bool Read) AUX_BOOT_SECTION;
249 #endif
250
251 void VirtualFAT_WriteBlock(const uint16_t BlockNumber) AUX_BOOT_SECTION;
252 void VirtualFAT_ReadBlock(const uint16_t BlockNumber) AUX_BOOT_SECTION;
253
254 #endif