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.
81 [DISK_FILE_ENTRY_VolumeID
] =
86 .Attributes
= FAT_FLAG_VOLUME_NAME
,
95 /* VFAT Long File Name entry for the virtual firmware file; required to
96 * prevent corruption from systems that are unable to detect the device
97 * as being a legacy MSDOS style FAT12 volume. */
98 [DISK_FILE_ENTRY_FirmwareLFN
] =
102 .Ordinal
= FAT_ORDINAL_LAST_ENTRY
| 1,
103 .Attribute
= FAT_FLAG_LONG_FILE_NAME
,
125 /* MSDOS file entry for the virtual Firmware image. */
126 [DISK_FILE_ENTRY_FirmwareMSDOS
] =
130 .Filename
= "FIRMWARE",
134 .CreationTime
= FAT_TIME(1, 1, 0),
135 .CreationDate
= FAT_DATE(14, 2, 1989),
136 .StartingCluster
= 2,
137 .FileSizeBytes
= FIRMWARE_FILE_SIZE_BYTES
,
142 /** Starting cluster of the virtual firmware file on disk, tracked so that the
143 * offset from the start of the data sector can be determined. On Windows
144 * systems files are usually replaced using the original file's disk clusters,
145 * while Linux appears to overwrite with an offset which must be compensated for.
147 static uint16_t* FileStartCluster
= &FirmwareFileEntries
[DISK_FILE_ENTRY_FirmwareMSDOS
].MSDOS_File
.StartingCluster
;
150 /** Updates a FAT12 cluster entry in the FAT file table with the specified next
151 * chain index. If the cluster is the last in the file chain, the magic value
154 * \note FAT data cluster indexes are offset by 2, so that cluster 2 is the
155 * first file data cluster on the disk. See the FAT specification.
157 * \param[out] FATTable Pointer to the FAT12 allocation table
158 * \param[in] Index Index of the cluster entry to update
159 * \param[in] ChainEntry Next cluster index in the file chain
161 static void UpdateFAT12ClusterEntry(uint8_t* const FATTable
,
162 const uint16_t Index
,
163 const uint16_t ChainEntry
)
165 /* Calculate the starting offset of the cluster entry in the FAT12 table */
166 uint8_t FATOffset
= (Index
+ (Index
>> 1));
167 bool UpperNibble
= ((Index
& 1) != 0);
169 /* Check if the start of the entry is at an upper nibble of the byte, fill
170 * out FAT12 entry as required */
173 FATTable
[FATOffset
] = (FATTable
[FATOffset
] & 0x0F) | ((ChainEntry
& 0x0F) << 4);
174 FATTable
[FATOffset
+ 1] = (ChainEntry
>> 4);
178 FATTable
[FATOffset
] = ChainEntry
;
179 FATTable
[FATOffset
+ 1] = (FATTable
[FATOffset
] & 0xF0) | (ChainEntry
>> 8);
183 /** Reads or writes a block of data from/to the physical device FLASH using a
184 * block buffer stored in RAM, if the requested block is within the virtual
185 * firmware file's sector ranges in the emulated FAT file system.
187 * \param[in] BlockNumber Physical disk block to read from
188 * \param[in,out] BlockBuffer Pointer to the start of the block buffer in RAM
189 * \param[in] Read If \c true, the requested block is read, if
190 * \c false, the requested block is written
192 static void ReadWriteFirmwareFileBlock(const uint16_t BlockNumber
,
193 uint8_t* BlockBuffer
,
196 uint16_t FileStartBlock
= DISK_BLOCK_DataStartBlock
+ (*FileStartCluster
- 2) * SECTOR_PER_CLUSTER
;
197 uint16_t FileEndBlock
= FileStartBlock
+ (FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES
) - 1);
199 /* Range check the write request - abort if requested block is not within the
200 * virtual firmware file sector range */
201 if (!((BlockNumber
>= FileStartBlock
) && (BlockNumber
<= FileEndBlock
)))
204 #if (FLASHEND > 0xFFFF)
205 uint32_t FlashAddress
= (uint32_t)(BlockNumber
- FileStartBlock
) * SECTOR_SIZE_BYTES
;
207 uint16_t FlashAddress
= (uint16_t)(BlockNumber
- FileStartBlock
) * SECTOR_SIZE_BYTES
;
212 /* Read out the mapped block of data from the device's FLASH */
213 for (uint16_t i
= 0; i
< SECTOR_SIZE_BYTES
; i
++)
215 #if (FLASHEND > 0xFFFF)
216 BlockBuffer
[i
] = pgm_read_byte_far(FlashAddress
++);
218 BlockBuffer
[i
] = pgm_read_byte(FlashAddress
++);
224 /* Write out the mapped block of data to the device's FLASH */
225 for (uint16_t i
= 0; i
< SECTOR_SIZE_BYTES
; i
+= 2)
227 if ((FlashAddress
% SPM_PAGESIZE
) == 0)
229 /* Erase the given FLASH page, ready to be programmed */
230 BootloaderAPI_ErasePage(FlashAddress
);
233 /* Write the next data word to the FLASH page */
234 BootloaderAPI_FillWord(FlashAddress
, (BlockBuffer
[i
+ 1] << 8) | BlockBuffer
[i
]);
237 if ((FlashAddress
% SPM_PAGESIZE
) == 0)
239 /* Write the filled FLASH page to memory */
240 BootloaderAPI_WritePage(FlashAddress
- SPM_PAGESIZE
);
246 /** Writes a block of data to the virtual FAT filesystem, from the USB Mass
249 * \param[in] BlockNumber Index of the block to write.
251 void VirtualFAT_WriteBlock(const uint16_t BlockNumber
)
253 uint8_t BlockBuffer
[SECTOR_SIZE_BYTES
];
255 /* Buffer the entire block to be written from the host */
256 Endpoint_Read_Stream_LE(BlockBuffer
, sizeof(BlockBuffer
), NULL
);
261 case DISK_BLOCK_BootBlock
:
262 case DISK_BLOCK_FATBlock1
:
263 case DISK_BLOCK_FATBlock2
:
264 /* Ignore writes to the boot and FAT blocks */
268 case DISK_BLOCK_RootFilesBlock
:
269 /* Copy over the updated directory entries */
270 memcpy(FirmwareFileEntries
, BlockBuffer
, sizeof(FirmwareFileEntries
));
275 ReadWriteFirmwareFileBlock(BlockNumber
, BlockBuffer
, false);
281 /** Reads a block of data from the virtual FAT filesystem, and sends it to the
282 * host via the USB Mass Storage interface.
284 * \param[in] BlockNumber Index of the block to read.
286 void VirtualFAT_ReadBlock(const uint16_t BlockNumber
)
288 uint8_t BlockBuffer
[SECTOR_SIZE_BYTES
];
289 memset(BlockBuffer
, 0x00, sizeof(BlockBuffer
));
293 case DISK_BLOCK_BootBlock
:
294 memcpy(BlockBuffer
, &BootBlock
, sizeof(FATBootBlock_t
));
296 /* Add the magic signature to the end of the block */
297 BlockBuffer
[SECTOR_SIZE_BYTES
- 2] = 0x55;
298 BlockBuffer
[SECTOR_SIZE_BYTES
- 1] = 0xAA;
302 case DISK_BLOCK_FATBlock1
:
303 case DISK_BLOCK_FATBlock2
:
304 /* Cluster 0: Media type/Reserved */
305 UpdateFAT12ClusterEntry(BlockBuffer
, 0, 0xF00 | BootBlock
.MediaDescriptor
);
307 /* Cluster 1: Reserved */
308 UpdateFAT12ClusterEntry(BlockBuffer
, 1, 0xFFF);
310 /* Cluster 2 onwards: Cluster chain of FIRMWARE.BIN */
311 for (uint16_t i
= 0; i
<= FILE_CLUSTERS(FIRMWARE_FILE_SIZE_BYTES
); i
++)
313 uint16_t CurrentCluster
= *FileStartCluster
+ i
;
314 uint16_t NextCluster
= CurrentCluster
+ 1;
316 /* Mark last cluster as end of file */
317 if (i
== FILE_CLUSTERS(FIRMWARE_FILE_SIZE_BYTES
))
320 UpdateFAT12ClusterEntry(BlockBuffer
, CurrentCluster
, NextCluster
);
325 case DISK_BLOCK_RootFilesBlock
:
326 memcpy(BlockBuffer
, FirmwareFileEntries
, sizeof(FirmwareFileEntries
));
331 ReadWriteFirmwareFileBlock(BlockNumber
, BlockBuffer
, true);
336 /* Write the entire read block Buffer to the host */
337 Endpoint_Write_Stream_LE(BlockBuffer
, sizeof(BlockBuffer
), NULL
);