Add documentation to the incomplete Mass Storage class bootloader, update the virtual...
[pub/USBasp.git] / Bootloaders / Incomplete / 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 /* Macros: */
41 /** Size of the virtual FIRMWARE.BIN file in bytes. */
42 #define FIRMWARE_FILE_SIZE_BYTES (FLASHEND - (FLASHEND - BOOT_START_ADDR) + 1UL)
43
44 /** Number of sectors that comprise a single logical disk cluster. */
45 #define SECTOR_PER_CLUSTER 4
46
47 /** Size of a single logical sector on the disk. */
48 #define SECTOR_SIZE_BYTES 512
49
50 /** Size of a logical cluster on the disk, in bytes */
51 #define CLUSTER_SIZE_BYTES (SECTOR_PER_CLUSTER * SECTOR_SIZE_BYTES)
52
53 /** Number of sectors required to store a given size in bytes.
54 *
55 * \param[in] size Size of the data that needs to be stored
56 *
57 * \return Number of sectors required to store the given data on the disk.
58 */
59 #define FILE_SECTORS(size) ((size / SECTOR_SIZE_BYTES) + ((size % SECTOR_SIZE_BYTES) ? 1 : 0))
60
61 /** Number of clusters required to store a given size in bytes.
62 *
63 * \param[in] size Size of the data that needs to be stored
64 *
65 * \return Number of clusters required to store the given data on the disk.
66 */
67 #define FILE_CLUSTERS(size) ((size / CLUSTER_SIZE_BYTES) + ((size % CLUSTER_SIZE_BYTES) ? 1 : 0))
68
69 /** Total number of logical sectors/blocks on the disk. */
70 #define LUN_MEDIA_BLOCKS (FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES) + 32)
71
72 /** Converts a given time in HH:MM:SS format to a FAT filesystem time.
73 *
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.
76 *
77 * \param[in] hh Hours (0-23)
78 * \param[in] mm Minutes (0-59)
79 * \param[in] ss Seconds (0-59)
80 *
81 * \return Given time encoded as a FAT filesystem timestamp
82 */
83 #define FAT_TIME(h, m, s) ((hh << 11) | (mm << 5) | (ss >> 1))
84
85 /** Converts a given date in DD/MM/YYYY format to a FAT filesystem date.
86 *
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)
90 *
91 * \return Given date encoded as a FAT filesystem datestamp
92 */
93 #define FAT_DATE(d, m, y) (((yyyy - 1980) << 9) | (mm << 5) | (dd << 0))
94
95 /* Type Definitions: */
96 /** FAT boot block structure definition, used to identify the core
97 * parameters of a FAT filesystem stored on a disk.
98 *
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.
102 */
103 typedef struct
104 {
105 uint8_t Bootstrap[3];
106 uint8_t Description[8];
107 uint16_t SectorSize;
108 uint8_t SectorsPerCluster;
109 uint16_t ReservedSectors;
110 uint8_t FATCopies;
111 uint16_t RootDirectoryEntries;
112 uint16_t TotalSectors16;
113 uint8_t MediaDescriptor;
114 uint16_t SectorsPerFAT;
115 uint16_t SectorsPerTrack;
116 uint16_t Heads;
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; */
126 } FATBootBlock_t;
127
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.
130 */
131 typedef struct
132 {
133 uint8_t Filename[8];
134 uint8_t Extension[3];
135 uint8_t Attributes;
136 uint8_t Reserved[10];
137 uint16_t CreationTime;
138 uint16_t CreationDate;
139 uint16_t StartingCluster;
140 uint32_t FileSizeBytes;
141 } FATDirectoryEntry_t;
142
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);
150 #endif
151
152 void VirtualFAT_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
153 const uint32_t BlockAddress,
154 uint16_t TotalBlocks);
155
156 void VirtualFAT_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
157 const uint32_t BlockAddress,
158 uint16_t TotalBlocks);
159 #endif