Add documentation to the incomplete Mass Storage class bootloader, update the virtual...
[pub/USBasp.git] / Bootloaders / Incomplete / MassStorage / Lib / VirtualFAT.c
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 /** \file
32 *
33 * Virtualized FAT12 filesystem implementation, to perform self-programming
34 * in response to read and write requests to the virtual filesystem by the
35 * host PC.
36 */
37
38 #define INCLUDE_FROM_VIRTUAL_FAT_C
39 #include "VirtualFAT.h"
40
41 /** FAT filesystem boot sector block, must be the first sector on the physical
42 * disk so that the host can identify the presence of a FAT filesystem. This
43 * block is truncated; normally a large bootstrap section is located near the
44 * end of the block for booting purposes however as this is not meant to be a
45 * bootable disk it is omitted for space reasons.
46 *
47 * \note When returning the boot block to the host, the magic signature 0xAA55
48 * must be added to the very end of the block to identify it as a boot
49 * block.
50 */
51 static const FATBootBlock_t BootBlock =
52 {
53 .Bootstrap = {0xEB, 0x3C, 0x90},
54 .Description = "mkdosfs",
55 .SectorSize = SECTOR_SIZE_BYTES,
56 .SectorsPerCluster = SECTOR_PER_CLUSTER,
57 .ReservedSectors = 1,
58 .FATCopies = 2,
59 .RootDirectoryEntries = (SECTOR_SIZE_BYTES / sizeof(FATDirectoryEntry_t)),
60 .TotalSectors16 = LUN_MEDIA_BLOCKS,
61 .MediaDescriptor = 0xF8,
62 .SectorsPerFAT = 1,
63 .SectorsPerTrack = (LUN_MEDIA_BLOCKS % 64),
64 .Heads = (LUN_MEDIA_BLOCKS / 64),
65 .HiddenSectors = 0,
66 .TotalSectors32 = 0,
67 .PhysicalDriveNum = 0,
68 .ExtendedBootRecordSig = 0x29,
69 .VolumeSerialNumber = 0x12345678,
70 .VolumeLabel = "LUFA BOOT ",
71 .FilesystemIdentifier = "FAT12 ",
72 };
73
74 /** FAT 8.3 style directory entry, for the virtual FLASH contents file. */
75 static FATDirectoryEntry_t FirmwareFileEntry =
76 {
77 .Filename = "FIRMWARE",
78 .Extension = "BIN",
79 .Attributes = 0,
80 .Reserved = {0},
81 .CreationTime = FAT_TIME(1, 1, 0),
82 .CreationDate = FAT_DATE(14, 2, 1989),
83 .StartingCluster = 2,
84 .FileSizeBytes = FIRMWARE_FILE_SIZE_BYTES,
85 };
86
87
88 /** Updates a FAT12 cluster entry in the FAT file table with the specified next
89 * chain index. If the cluster is the last in the file chain, the magic value
90 * 0xFFF is used.
91 *
92 * \note FAT data cluster indexes are offset by 2, so that cluster 2 is the
93 * first file data cluster on the disk. See the FAT specification.
94 *
95 * \param[out] FATTable Pointer to the FAT12 allocation table
96 * \param[in] Index Index of the cluster entry to update
97 * \param[in] ChainEntry Next cluster index in the file chain
98 */
99 static void UpdateFAT12ClusterEntry(uint8_t* const FATTable,
100 const uint16_t Index,
101 const uint16_t ChainEntry)
102 {
103 /* Calculate the starting offset of the cluster entry in the FAT12 table */
104 uint8_t FATOffset = (Index * 3) / 2;
105 bool UpperNibble = (((Index * 3) % 2) != 0);
106
107 /* Check if the start of the entry is at an upper nibble of the byte, fill
108 * out FAT12 entry as required */
109 if (UpperNibble)
110 {
111 FATTable[FATOffset] = (FATTable[FATOffset] & 0x0F) | ((ChainEntry & 0x0F) << 4);
112 FATTable[FATOffset + 1] = (ChainEntry >> 4);
113 }
114 else
115 {
116 FATTable[FATOffset] = ChainEntry;
117 FATTable[FATOffset + 1] = (FATTable[FATOffset] & 0xF0) | (ChainEntry >> 8);
118 }
119 }
120
121 /** Writes a block of data to the virtual FAT filesystem, from the USB Mass
122 * Storage interface.
123 *
124 * \param[in] BlockNumber Index of the block to write.
125 */
126 static void WriteVirtualBlock(const uint16_t BlockNumber)
127 {
128 uint8_t BlockBuffer[SECTOR_SIZE_BYTES];
129
130 /* Buffer the entire block to be written from the host */
131 Endpoint_Read_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
132 Endpoint_ClearOUT();
133
134 if ((BlockNumber >= 4) && (BlockNumber < (4 + FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES))))
135 {
136 uint32_t WriteFlashAddress = (uint32_t)(BlockNumber - 4) * SECTOR_SIZE_BYTES;
137
138 for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i += 2)
139 {
140 if ((WriteFlashAddress % SPM_PAGESIZE) == 0)
141 {
142 /* Erase the given FLASH page, ready to be programmed */
143 boot_page_erase(WriteFlashAddress);
144 boot_spm_busy_wait();
145 }
146
147 /* Write the next data word to the FLASH page */
148 boot_page_fill(WriteFlashAddress, (BlockBuffer[i + 1] << 8) | BlockBuffer[i]);
149 WriteFlashAddress += 2;
150
151 if ((WriteFlashAddress % SPM_PAGESIZE) == 0)
152 {
153 /* Write the filled FLASH page to memory */
154 boot_page_write(WriteFlashAddress - SPM_PAGESIZE);
155 boot_spm_busy_wait();
156 }
157 }
158 }
159 }
160
161 /** Reads a block of data from the virtual FAT filesystem, and sends it to the
162 * host via the USB Mass Storage interface.
163 *
164 * \param[in] BlockNumber Index of the block to read.
165 */
166 static void ReadVirtualBlock(const uint16_t BlockNumber)
167 {
168 uint8_t BlockBuffer[SECTOR_SIZE_BYTES];
169 memset(BlockBuffer, 0x00, sizeof(BlockBuffer));
170
171 switch (BlockNumber)
172 {
173 case 0: /* Block 0: Boot block sector */
174 memcpy(BlockBuffer, &BootBlock, sizeof(FATBootBlock_t));
175
176 /* Add the magic signature to the end of the block */
177 BlockBuffer[SECTOR_SIZE_BYTES - 2] = 0x55;
178 BlockBuffer[SECTOR_SIZE_BYTES - 1] = 0xAA;
179 break;
180
181 case 1: /* Block 1: First FAT12 cluster chain copy */
182 case 2: /* Block 2: Second FAT12 cluster chain copy */
183 /* Cluster 0: Media type/Reserved */
184 UpdateFAT12ClusterEntry(BlockBuffer, 0, 0xF00 | BootBlock.MediaDescriptor);
185
186 /* Cluster 1: Reserved */
187 UpdateFAT12ClusterEntry(BlockBuffer, 1, 0xFFF);
188
189 /* Cluster 2 onwards: Cluster chain of FIRMWARE.BIN */
190 for (uint16_t i = 0; i < FILE_CLUSTERS(FIRMWARE_FILE_SIZE_BYTES); i++)
191 UpdateFAT12ClusterEntry(BlockBuffer, i+2, i+3);
192
193 /* Mark last cluster as end of file */
194 UpdateFAT12ClusterEntry(BlockBuffer, FILE_CLUSTERS(FIRMWARE_FILE_SIZE_BYTES) + 1, 0xFFF);
195 break;
196
197 case 3: /* Block 3: Root file entries */
198 memcpy(BlockBuffer, &FirmwareFileEntry, sizeof(FATDirectoryEntry_t));
199 break;
200
201 default: /* Blocks 4 onwards: Data allocation section */
202 if ((BlockNumber >= 4) && (BlockNumber < (4 + FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES))))
203 {
204 uint32_t ReadFlashAddress = (uint32_t)(BlockNumber - 4) * SECTOR_SIZE_BYTES;
205
206 for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i++)
207 BlockBuffer[i] = pgm_read_byte_far(ReadFlashAddress++);
208 }
209
210 break;
211 }
212
213 /* Write the entire read block Buffer to the host */
214 Endpoint_Write_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
215 Endpoint_ClearIN();
216 }
217
218 /** Writes a number of blocks to the virtual FAT file system, from the host
219 * PC via the USB Mass Storage interface.
220 *
221 * \param[in] MSInterfaceInfo Pointer to a structure containing a Mass Storage Class configuration and state
222 * \param[in] BlockAddress Data block starting address for the write sequence
223 * \param[in] TotalBlocks Number of blocks of data to write
224 */
225 void VirtualFAT_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
226 const uint32_t BlockAddress,
227 uint16_t TotalBlocks)
228 {
229 uint16_t CurrentBlock = (uint16_t)BlockAddress;
230
231 /* Emulated FAT is performed per-block, pass each requested block index
232 * to the emulated FAT block write function */
233 while (TotalBlocks--)
234 WriteVirtualBlock(CurrentBlock++);
235 }
236
237 /** Reads a number of blocks from the virtual FAT file system, and sends them
238 * to the host PC via the USB Mass Storage interface.
239 *
240 * \param[in] MSInterfaceInfo Pointer to a structure containing a Mass Storage Class configuration and state
241 * \param[in] BlockAddress Data block starting address for the read sequence
242 * \param[in] TotalBlocks Number of blocks of data to read
243 */
244 void VirtualFAT_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
245 const uint32_t BlockAddress,
246 uint16_t TotalBlocks)
247 {
248 uint16_t CurrentBlock = (uint16_t)BlockAddress;
249
250 /* Emulated FAT is performed per-block, pass each requested block index
251 * to the emulated FAT block read function */
252 while (TotalBlocks--)
253 ReadVirtualBlock(CurrentBlock++);
254 }
255