VirtualFAT.h 10.3 KB
Newer Older
1
2
/*
             LUFA Library
Dean Camera's avatar
Dean Camera committed
3
     Copyright (C) Dean Camera, 2017.
4
5
6
7
8
9

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
Dean Camera's avatar
Dean Camera committed
10
  Copyright 2017  Dean Camera (dean [at] fourwalledcubicle [dot] com)
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaims all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

#ifndef _VIRTUALFAT_H_
#define _VIRTUALFAT_H_

	/* Includes: */
		#include <avr/io.h>
		#include <avr/pgmspace.h>

		#include <LUFA/Drivers/USB/USB.h>

40
41
		#include "../BootloaderAPI.h"

42
	/* Macros: */
43
44
45
46
47
		/** Size of the virtual FLASH.BIN file in bytes. */
		#define FLASH_FILE_SIZE_BYTES     (FLASHEND - (FLASHEND - BOOT_START_ADDR) - AUX_BOOT_SECTION_SIZE)

		/** Size of the virtual EEPROM.BIN file in bytes. */
		#define EEPROM_FILE_SIZE_BYTES    E2END
48

49
50
		/** Number of sectors that comprise a single logical disk cluster. */
		#define SECTOR_PER_CLUSTER        4
51

52
53
		/** Size of a single logical sector on the disk. */
		#define SECTOR_SIZE_BYTES         512
54

55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
		/** Size of a logical cluster on the disk, in bytes */
		#define CLUSTER_SIZE_BYTES        (SECTOR_PER_CLUSTER * SECTOR_SIZE_BYTES)

		/** Number of sectors required to store a given size in bytes.
		 *
		 *  \param[in] size  Size of the data that needs to be stored
		 *
		 *  \return Number of sectors required to store the given data on the disk.
		 */
		#define FILE_SECTORS(size)        ((size / SECTOR_SIZE_BYTES)  + ((size % SECTOR_SIZE_BYTES)  ? 1 : 0))

		/** Number of clusters required to store a given size in bytes.
		 *
		 *  \param[in] size  Size of the data that needs to be stored
		 *
		 *  \return Number of clusters required to store the given data on the disk.
		 */
		#define FILE_CLUSTERS(size)       ((size / CLUSTER_SIZE_BYTES) + ((size % CLUSTER_SIZE_BYTES) ? 1 : 0))

		/** Total number of logical sectors/blocks on the disk. */
75
		#define LUN_MEDIA_BLOCKS          (FILE_SECTORS(FLASH_FILE_SIZE_BYTES) + FILE_SECTORS(EEPROM_FILE_SIZE_BYTES) + 32)
76
77
78
79
80
81
82
83
84
85
86
87

		/** Converts a given time in HH:MM:SS format to a FAT filesystem time.
		 *
		 *  \note The minimum seconds resolution of FAT is 2, thus odd seconds
		 *        will be truncated to the previous integer multiple of 2 seconds.
		 *
		 *  \param[in] hh  Hours (0-23)
		 *  \param[in] mm  Minutes (0-59)
		 *  \param[in] ss  Seconds (0-59)
		 *
		 *  \return Given time encoded as a FAT filesystem timestamp
		 */
88
		#define FAT_TIME(hh, mm, ss)      ((hh << 11) | (mm << 5) | (ss >> 1))
89
90
91
92
93
94
95
96
97

		/** Converts a given date in DD/MM/YYYY format to a FAT filesystem date.
		 *
		 *  \param[in] dd    Days in the month (1-31)
		 *  \param[in] mm    Months in the year (1-12)
		 *  \param[in] yyyy  Year (1980 - 2107)
		 *
		 *  \return Given date encoded as a FAT filesystem datestamp
		 */
98
		#define FAT_DATE(dd, mm, yyyy)    (((yyyy - 1980) << 9) | (mm << 5) | (dd << 0))
99

100
101
		/** Bit-rotates a given 8-bit value once to the right.
		 *
102
		 *  \param[in] x  Value to rotate right once
103
104
105
		 *
		 *  \return Bit-rotated input value, rotated once to the right.
		 */
106
		#define ROT8(x)                   ((((x) & 0xFE) >> 1) | (((x) & 1) ? 0x80 : 0x00))
107
108
109
110

		/** Computes the LFN entry checksum of a MSDOS 8.3 format file entry,
		 *  to associate a LFN entry with its short file entry.
		 *
111
112
113
114
115
116
117
118
119
120
121
		 *  \param[in] n0  MSDOS Filename character 1
		 *  \param[in] n1  MSDOS Filename character 2
		 *  \param[in] n2  MSDOS Filename character 3
		 *  \param[in] n3  MSDOS Filename character 4
		 *  \param[in] n4  MSDOS Filename character 5
		 *  \param[in] n5  MSDOS Filename character 6
		 *  \param[in] n6  MSDOS Filename character 7
		 *  \param[in] n7  MSDOS Filename character 8
		 *  \param[in] e0  MSDOS Extension character 1
		 *  \param[in] e1  MSDOS Extension character 2
		 *  \param[in] e2  MSDOS Extension character 3
122
123
124
125
		 *
		 *  \return LFN checksum of the given MSDOS 8.3 filename.
		 */
		#define FAT_CHECKSUM(n0, n1, n2, n3, n4, n5, n6, n7, e0, e1, e2) \
126
		   (uint8_t)(ROT8(ROT8(ROT8(ROT8(ROT8(ROT8(ROT8(ROT8(ROT8(ROT8(n0)+n1)+n2)+n3)+n4)+n5)+n6)+n7)+e0)+e1)+e2)
127

128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
		/** \name FAT Filesystem Flags */
		//@{
		/** FAT attribute flag to indicate a read-only file. */
		#define FAT_FLAG_READONLY         (1 << 0)

		/** FAT attribute flag to indicate a hidden file. */
		#define FAT_FLAG_HIDDEN           (1 << 1)

		/** FAT attribute flag to indicate a system file. */
		#define FAT_FLAG_SYSTEM           (1 << 2)

		/** FAT attribute flag to indicate a Volume name entry. */
		#define FAT_FLAG_VOLUME_NAME      (1 << 3)

		/** FAT attribute flag to indicate a directory entry. */
		#define FAT_FLAG_DIRECTORY        (1 << 4)

		/** FAT attribute flag to indicate a file ready for archiving. */
		#define FAT_FLAG_ARCHIVE          (1 << 5)

		/** FAT pseudo-attribute flag to indicate a Long File Name entry. */
		#define FAT_FLAG_LONG_FILE_NAME   0x0F

		/** Ordinal flag marker for FAT Long File Name entries to mark the last entry. */
		#define FAT_ORDINAL_LAST_ENTRY    (1 << 6)
		//@}

155
	/* Enums: */
156
157
158
		/** Enum for the Root FAT file entry indexes on the disk. This can be used
		 *  to retrieve the current contents of a known directory entry.
		 */
159
160
		enum
		{
161
			/** Volume ID directory entry, giving the name of the virtual disk. */
162
			DISK_FILE_ENTRY_VolumeID      = 0,
163
164
165
166
167
168
169
170
			/** Long File Name FAT file entry of the virtual FLASH.BIN image file. */
			DISK_FILE_ENTRY_FLASH_LFN     = 1,
			/** Legacy MSDOS FAT file entry of the virtual FLASH.BIN image file. */
			DISK_FILE_ENTRY_FLASH_MSDOS   = 2,
			/** Long File Name FAT file entry of the virtual EEPROM.BIN image file. */
			DISK_FILE_ENTRY_EEPROM_LFN    = 3,
			/** Legacy MSDOS FAT file entry of the virtual EEPROM.BIN image file. */
			DISK_FILE_ENTRY_EEPROM_MSDOS  = 4,
171
172
		};

173
		/** Enum for the physical disk blocks of the virtual disk. */
174
175
		enum
		{
176
			/** Boot sector disk block. */
177
			DISK_BLOCK_BootBlock          = 0,
178
			/** First copy of the FAT table block. */
179
			DISK_BLOCK_FATBlock1          = 1,
180
			/** Second copy of the FAT table block. */
181
			DISK_BLOCK_FATBlock2          = 2,
182
			/** Root file and directory entries block. */
183
			DISK_BLOCK_RootFilesBlock     = 3,
184
			/** Start block of the disk data section. */
185
			DISK_BLOCK_DataStartBlock     = 4,
186
187
		};

188
	/* Type Definitions: */
189
		/** FAT boot block structure definition, used to identify the core
190
		 *  parameters of a FAT file system stored on a disk.
191
192
		 *
		 *  \note This definition is truncated to save space; the magic signature
193
194
		 *        \c 0xAA55 must be appended to the very end of the block for it
		 *        to be detected by the host as a valid boot block.
195
		 */
196
197
198
199
		typedef struct
		{
			uint8_t  Bootstrap[3];
			uint8_t  Description[8];
200
201
202
			uint16_t SectorSize;
			uint8_t  SectorsPerCluster;
			uint16_t ReservedSectors;
203
204
			uint8_t  FATCopies;
			uint16_t RootDirectoryEntries;
205
			uint16_t TotalSectors16;
206
			uint8_t  MediaDescriptor;
207
208
			uint16_t SectorsPerFAT;
			uint16_t SectorsPerTrack;
209
			uint16_t Heads;
210
211
			uint32_t HiddenSectors;
			uint32_t TotalSectors32;
212
213
214
215
216
			uint16_t PhysicalDriveNum;
			uint8_t  ExtendedBootRecordSig;
			uint32_t VolumeSerialNumber;
			uint8_t  VolumeLabel[11];
			uint8_t  FilesystemIdentifier[8];
217
218
			/* uint8_t  BootstrapProgram[448]; */
			/* uint16_t MagicSignature; */
219
220
		} FATBootBlock_t;

221
222
		/** FAT directory entry structure, for the various kinds of File and
		 *  directory descriptors on a FAT disk.
223
		 */
224
		typedef union
225
		{
226
			/** VFAT Long File Name file entry. */
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
			struct
			{
				uint8_t  Ordinal;
				uint16_t Unicode1;
				uint16_t Unicode2;
				uint16_t Unicode3;
				uint16_t Unicode4;
				uint16_t Unicode5;
				uint8_t  Attribute;
				uint8_t  Reserved1;
				uint8_t  Checksum;
				uint16_t Unicode6;
				uint16_t Unicode7;
				uint16_t Unicode8;
				uint16_t Unicode9;
				uint16_t Unicode10;
				uint16_t Unicode11;
				uint16_t Reserved2;
				uint16_t Unicode12;
				uint16_t Unicode13;
247
			} VFAT_LongFileName;
248

249
			/** Legacy FAT MSDOS 8.3 file entry. */
250
251
252
253
254
255
256
257
258
259
			struct
			{
				uint8_t  Filename[8];
				uint8_t  Extension[3];
				uint8_t  Attributes;
				uint8_t  Reserved[10];
				uint16_t CreationTime;
				uint16_t CreationDate;
				uint16_t StartingCluster;
				uint32_t FileSizeBytes;
260
261
			} MSDOS_File;

262
			/** Legacy FAT MSDOS (sub-)directory entry. */
263
264
265
266
267
268
269
270
271
272
			struct
			{
				uint8_t  Name[11];
				uint8_t  Attributes;
				uint8_t  Reserved[10];
				uint16_t CreationTime;
				uint16_t CreationDate;
				uint16_t StartingCluster;
				uint32_t Reserved2;
			} MSDOS_Directory;
273
274
275
		} FATDirectoryEntry_t;

	/* Function Prototypes: */
276
		#if defined(INCLUDE_FROM_VIRTUAL_FAT_C)
277
			static uint8_t ReadEEPROMByte(const uint8_t* const Address) ATTR_NO_INLINE;
278
279

			static void WriteEEPROMByte(uint8_t* const Address,
280
			                            const uint8_t Data) ATTR_NO_INLINE;
281

282
283
			static void UpdateFAT12ClusterEntry(uint8_t* const FATTable,
			                                    const uint16_t Index,
284
			                                    const uint16_t ChainEntry) AUX_BOOT_SECTION;
285

286
287
			static void UpdateFAT12ClusterChain(uint8_t* const FATTable,
			                                    const uint16_t StartIndex,
288
			                                    const uint8_t ChainLength) AUX_BOOT_SECTION;
289
290
291
292
293
294
295
296

			static void ReadWriteFLASHFileBlock(const uint16_t BlockNumber,
			                                    uint8_t* BlockBuffer,
			                                    const bool Read) AUX_BOOT_SECTION;

			static void ReadWriteEEPROMFileBlock(const uint16_t BlockNumber,
			                                     uint8_t* BlockBuffer,
			                                     const bool Read) AUX_BOOT_SECTION;
297
298
		#endif

299
300
		void VirtualFAT_WriteBlock(const uint16_t BlockNumber) AUX_BOOT_SECTION;
		void VirtualFAT_ReadBlock(const uint16_t BlockNumber) AUX_BOOT_SECTION;
301
302

#endif