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
33 * Virtualized FAT12 filesystem implementation, to perform self-programming
34 * in response to read and write requests to the virtual filesystem by the
38 #define INCLUDE_FROM_VIRTUAL_FAT_C
39 #include "VirtualFAT.h"
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.
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
51 static const FATBootBlock_t BootBlock
=
53 .Bootstrap
= {0xEB, 0x3C, 0x90},
54 .Description
= "mkdosfs",
55 .SectorSize
= SECTOR_SIZE_BYTES
,
56 .SectorsPerCluster
= SECTOR_PER_CLUSTER
,
59 .RootDirectoryEntries
= (SECTOR_SIZE_BYTES
/ sizeof(FATDirectoryEntry_t
)),
60 .TotalSectors16
= LUN_MEDIA_BLOCKS
,
61 .MediaDescriptor
= 0xF8,
63 .SectorsPerTrack
= (LUN_MEDIA_BLOCKS
% 64),
64 .Heads
= (LUN_MEDIA_BLOCKS
/ 64),
67 .PhysicalDriveNum
= 0,
68 .ExtendedBootRecordSig
= 0x29,
69 .VolumeSerialNumber
= 0x12345678,
70 .VolumeLabel
= "LUFA BOOT ",
71 .FilesystemIdentifier
= "FAT12 ",
74 /** FAT 8.3 style directory entry, for the virtual FLASH contents file. */
75 static FATDirectoryEntry_t FirmwareFileEntries
[] =
77 /* Root volume label entry; disk label is contained in the Filename and
78 * Extension fields (concatenated) with a special attribute flag - other
79 * fields are ignored. Should be the same as the label in the boot block.
84 .Filename
= "LUFA BOO",
86 .Attributes
= FAT_FLAG_VOLUME_NAME
,
95 /* VFAT Long File Name entry for the virtual firmware file; required to
96 * prevent corruption of systems that are unable to detect the device
97 * as being a legacy MSDOS style FAT12 volume to prevent corruption. */
101 .Ordinal
= FAT_ORDINAL_LAST_ENTRY
| 1,
102 .Attribute
= FAT_FLAG_LONG_FILE_NAME
,
124 /* MSDOS file entry for the virtual Firmware image. */
128 .Filename
= "FIRMWARE",
132 .CreationTime
= FAT_TIME(1, 1, 0),
133 .CreationDate
= FAT_DATE(14, 2, 1989),
134 .StartingCluster
= 2,
135 .FileSizeBytes
= FIRMWARE_FILE_SIZE_BYTES
,
141 /** Updates a FAT12 cluster entry in the FAT file table with the specified next
142 * chain index. If the cluster is the last in the file chain, the magic value
145 * \note FAT data cluster indexes are offset by 2, so that cluster 2 is the
146 * first file data cluster on the disk. See the FAT specification.
148 * \param[out] FATTable Pointer to the FAT12 allocation table
149 * \param[in] Index Index of the cluster entry to update
150 * \param[in] ChainEntry Next cluster index in the file chain
152 static void UpdateFAT12ClusterEntry(uint8_t* const FATTable
,
153 const uint16_t Index
,
154 const uint16_t ChainEntry
)
156 /* Calculate the starting offset of the cluster entry in the FAT12 table */
157 uint8_t FATOffset
= (Index
+ (Index
>> 1));
158 bool UpperNibble
= ((Index
& 1) != 0);
160 /* Check if the start of the entry is at an upper nibble of the byte, fill
161 * out FAT12 entry as required */
164 FATTable
[FATOffset
] = (FATTable
[FATOffset
] & 0x0F) | ((ChainEntry
& 0x0F) << 4);
165 FATTable
[FATOffset
+ 1] = (ChainEntry
>> 4);
169 FATTable
[FATOffset
] = ChainEntry
;
170 FATTable
[FATOffset
+ 1] = (FATTable
[FATOffset
] & 0xF0) | (ChainEntry
>> 8);
174 /** Writes a block of data to the virtual FAT filesystem, from the USB Mass
177 * \param[in] BlockNumber Index of the block to write.
179 static void WriteVirtualBlock(const uint16_t BlockNumber
)
181 uint8_t BlockBuffer
[SECTOR_SIZE_BYTES
];
183 /* Buffer the entire block to be written from the host */
184 Endpoint_Read_Stream_LE(BlockBuffer
, sizeof(BlockBuffer
), NULL
);
187 if ((BlockNumber
>= 4) && (BlockNumber
< (4 + FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES
))))
189 #if (FLASHEND > 0xFFFF)
190 uint32_t WriteFlashAddress
= (uint32_t)(BlockNumber
- 4) * SECTOR_SIZE_BYTES
;
192 uint16_t WriteFlashAddress
= (uint16_t)(BlockNumber
- 4) * SECTOR_SIZE_BYTES
;
195 for (uint16_t i
= 0; i
< SECTOR_SIZE_BYTES
; i
+= 2)
197 if ((WriteFlashAddress
% SPM_PAGESIZE
) == 0)
199 /* Erase the given FLASH page, ready to be programmed */
200 BootloaderAPI_ErasePage(WriteFlashAddress
);
203 /* Write the next data word to the FLASH page */
204 BootloaderAPI_FillWord(WriteFlashAddress
, (BlockBuffer
[i
+ 1] << 8) | BlockBuffer
[i
]);
205 WriteFlashAddress
+= 2;
207 if ((WriteFlashAddress
% SPM_PAGESIZE
) == 0)
209 /* Write the filled FLASH page to memory */
210 BootloaderAPI_WritePage(WriteFlashAddress
- SPM_PAGESIZE
);
216 /** Reads a block of data from the virtual FAT filesystem, and sends it to the
217 * host via the USB Mass Storage interface.
219 * \param[in] BlockNumber Index of the block to read.
221 static void ReadVirtualBlock(const uint16_t BlockNumber
)
223 uint8_t BlockBuffer
[SECTOR_SIZE_BYTES
];
224 memset(BlockBuffer
, 0x00, sizeof(BlockBuffer
));
228 case 0: /* Block 0: Boot block sector */
229 memcpy(BlockBuffer
, &BootBlock
, sizeof(FATBootBlock_t
));
231 /* Add the magic signature to the end of the block */
232 BlockBuffer
[SECTOR_SIZE_BYTES
- 2] = 0x55;
233 BlockBuffer
[SECTOR_SIZE_BYTES
- 1] = 0xAA;
236 case 1: /* Block 1: First FAT12 cluster chain copy */
237 case 2: /* Block 2: Second FAT12 cluster chain copy */
238 /* Cluster 0: Media type/Reserved */
239 UpdateFAT12ClusterEntry(BlockBuffer
, 0, 0xF00 | BootBlock
.MediaDescriptor
);
241 /* Cluster 1: Reserved */
242 UpdateFAT12ClusterEntry(BlockBuffer
, 1, 0xFFF);
244 /* Cluster 2 onwards: Cluster chain of FIRMWARE.BIN */
245 for (uint16_t i
= 0; i
< FILE_CLUSTERS(FIRMWARE_FILE_SIZE_BYTES
); i
++)
246 UpdateFAT12ClusterEntry(BlockBuffer
, i
+2, i
+3);
248 /* Mark last cluster as end of file */
249 UpdateFAT12ClusterEntry(BlockBuffer
, FILE_CLUSTERS(FIRMWARE_FILE_SIZE_BYTES
) + 1, 0xFFF);
252 case 3: /* Block 3: Root file entries */
253 memcpy(BlockBuffer
, FirmwareFileEntries
, sizeof(FirmwareFileEntries
));
256 default: /* Blocks 4 onwards: Data allocation section */
257 if ((BlockNumber
>= 4) && (BlockNumber
< (4 + FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES
))))
259 #if (FLASHEND > 0xFFFF)
260 uint32_t ReadFlashAddress
= (uint32_t)(BlockNumber
- 4) * SECTOR_SIZE_BYTES
;
262 for (uint16_t i
= 0; i
< SECTOR_SIZE_BYTES
; i
++)
263 BlockBuffer
[i
] = pgm_read_byte_far(ReadFlashAddress
++);
265 uint16_t ReadFlashAddress
= (uint16_t)(BlockNumber
- 4) * SECTOR_SIZE_BYTES
;
267 for (uint16_t i
= 0; i
< SECTOR_SIZE_BYTES
; i
++)
268 BlockBuffer
[i
] = pgm_read_byte(ReadFlashAddress
++);
275 /* Write the entire read block Buffer to the host */
276 Endpoint_Write_Stream_LE(BlockBuffer
, sizeof(BlockBuffer
), NULL
);
280 /** Writes a number of blocks to the virtual FAT file system, from the host
281 * PC via the USB Mass Storage interface.
283 * \param[in] BlockAddress Data block starting address for the write sequence
284 * \param[in] TotalBlocks Number of blocks of data to write
286 void VirtualFAT_WriteBlocks(const uint16_t BlockAddress
,
287 uint16_t TotalBlocks
)
289 uint16_t CurrentBlock
= (uint16_t)BlockAddress
;
291 /* Emulated FAT is performed per-block, pass each requested block index
292 * to the emulated FAT block write function */
293 while (TotalBlocks
--)
294 WriteVirtualBlock(CurrentBlock
++);
297 /** Reads a number of blocks from the virtual FAT file system, and sends them
298 * to the host PC via the USB Mass Storage interface.
300 * \param[in] BlockAddress Data block starting address for the read sequence
301 * \param[in] TotalBlocks Number of blocks of data to read
303 void VirtualFAT_ReadBlocks(const uint16_t BlockAddress
,
304 uint16_t TotalBlocks
)
306 uint16_t CurrentBlock
= (uint16_t)BlockAddress
;
308 /* Emulated FAT is performed per-block, pass each requested block index
309 * to the emulated FAT block read function */
310 while (TotalBlocks
--)
311 ReadVirtualBlock(CurrentBlock
++);