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
31 #include "VirtualFAT.h"
33 static const FATBootBlock_t BootBlock
=
35 .Bootstrap
= {0xEB, 0x3C, 0x90},
36 .Description
= "mkdosfs",
37 .SectorSize
= SECTOR_SIZE_BYTES
,
38 .SectorsPerCluster
= SECTOR_PER_CLUSTER
,
41 .RootDirectoryEntries
= (SECTOR_SIZE_BYTES
/ sizeof(FATDirectoryEntry_t
)),
42 .TotalSectors16
= LUN_MEDIA_BLOCKS
,
43 .MediaDescriptor
= 0xF8,
45 .SectorsPerTrack
= LUN_MEDIA_BLOCKS
% 64,
46 .Heads
= LUN_MEDIA_BLOCKS
/ 64,
49 .PhysicalDriveNum
= 0,
50 .ExtendedBootRecordSig
= 0x29,
51 .VolumeSerialNumber
= 0x12345678,
52 .VolumeLabel
= "LUFA BOOT ",
53 .FilesystemIdentifier
= "FAT12 ",
54 .BootstrapProgram
= {0},
55 .MagicSignature
= 0xAA55,
58 static FATDirectoryEntry_t FirmwareFileEntry
=
60 .Filename
= "FIRMWARE",
64 .CreationTime
= FAT_TIME(1, 1, 0),
65 .CreationDate
= FAT_DATE(14, 2, 1989),
67 .FileSizeBytes
= FIRMWARE_FILE_SIZE
,
71 static void UpdateFAT12ClusterEntry(uint8_t* FATTable
,
73 const uint16_t ChainEntry
)
75 /* Calculate the starting offset of the cluster entry in the FAT12 table */
76 uint8_t FATOffset
= (Index
* 3) / 2;
77 bool UpperNibble
= (((Index
* 3) % 2) != 0);
79 /* Check if the start of the entry is at an upper nibble of the byte, fill
80 * out FAT12 entry as required */
83 FATTable
[FATOffset
] = (FATTable
[FATOffset
] & 0x0F) | ((ChainEntry
& 0x0F) << 4);
84 FATTable
[FATOffset
+ 1] = (ChainEntry
>> 4);
88 FATTable
[FATOffset
] = ChainEntry
;
89 FATTable
[FATOffset
+ 1] = (FATTable
[FATOffset
] & 0xF0) | (ChainEntry
>> 8);
93 static void WriteBlock(const uint16_t BlockNumber
)
95 uint8_t BlockBuffer
[SECTOR_SIZE_BYTES
];
97 /* Buffer the entire block to be written from the host */
98 Endpoint_Read_Stream_LE(BlockBuffer
, sizeof(BlockBuffer
), NULL
);
101 if ((BlockNumber
>= 4) && (BlockNumber
< (4 + (FIRMWARE_FILE_SIZE
/ SECTOR_SIZE_BYTES
))))
103 uint32_t WriteFlashAddress
= (uint32_t)(BlockNumber
- 4) * SECTOR_SIZE_BYTES
;
105 for (uint16_t i
= 0; i
< SECTOR_SIZE_BYTES
; i
+= 2)
107 /* Disallow writing to the bootloader section */
108 if (WriteFlashAddress
> BOOT_START_ADDR
)
111 if ((WriteFlashAddress
% SPM_PAGESIZE
) == 0)
113 /* Erase the given FLASH page, ready to be programmed */
114 boot_page_erase(WriteFlashAddress
);
115 boot_spm_busy_wait();
118 /* Write the next data word to the FLASH page */
119 boot_page_fill(WriteFlashAddress
, (BlockBuffer
[i
+ 1] << 8) | BlockBuffer
[i
]);
120 WriteFlashAddress
+= 2;
122 if ((WriteFlashAddress
% SPM_PAGESIZE
) == 0)
124 /* Write the filled FLASH page to memory */
125 boot_page_write(WriteFlashAddress
- SPM_PAGESIZE
);
126 boot_spm_busy_wait();
132 static void ReadBlock(const uint16_t BlockNumber
)
134 uint8_t BlockBuffer
[SECTOR_SIZE_BYTES
];
135 memset(BlockBuffer
, 0x00, sizeof(BlockBuffer
));
139 case 0: /* Block 0: Boot block sector */
140 memcpy(BlockBuffer
, &BootBlock
, sizeof(FATBootBlock_t
));
143 case 1: /* Block 1: First FAT12 cluster chain copy */
144 case 2: /* Block 2: Second FAT12 cluster chain copy */
145 /* Cluster 0: Media type/Reserved */
146 UpdateFAT12ClusterEntry(BlockBuffer
, 0, 0xF00 | BootBlock
.MediaDescriptor
);
148 /* Cluster 1: Reserved */
149 UpdateFAT12ClusterEntry(BlockBuffer
, 1, 0xFFF);
151 /* Cluster 2 onwards: Cluster chain of FIRMWARE.BIN */
152 for (uint16_t i
= 0; i
< FILE_CLUSTERS(FIRMWARE_FILE_SIZE
); i
++)
153 UpdateFAT12ClusterEntry(BlockBuffer
, i
+2, i
+3);
155 /* Mark last cluster as end of file */
156 UpdateFAT12ClusterEntry(BlockBuffer
, FILE_CLUSTERS(FIRMWARE_FILE_SIZE
) + 1, 0xFFF);
159 case 3: /* Block 3: Root file entries */
160 memcpy(BlockBuffer
, &FirmwareFileEntry
, sizeof(FATDirectoryEntry_t
));
163 default: /* Blocks 4 onwards: Data allocation section */
164 if ((BlockNumber
>= 4) && (BlockNumber
< (4 + (FIRMWARE_FILE_SIZE
/ SECTOR_SIZE_BYTES
))))
166 uint32_t ReadFlashAddress
= (uint32_t)(BlockNumber
- 4) * SECTOR_SIZE_BYTES
;
168 for (uint16_t i
= 0; i
< SECTOR_SIZE_BYTES
; i
++)
169 BlockBuffer
[i
] = pgm_read_byte_far(ReadFlashAddress
++);
175 /* Write the entire read block Buffer to the host */
176 Endpoint_Write_Stream_LE(BlockBuffer
, sizeof(BlockBuffer
), NULL
);
180 void VirtualFAT_WriteBlocks(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
,
181 const uint32_t BlockAddress
,
182 uint16_t TotalBlocks
)
184 uint16_t CurrentBlock
= (uint16_t)BlockAddress
;
186 /* Emulated FAT is performed per-block, pass each requested block index
187 * to the emulated FAT block write function */
188 while (TotalBlocks
--)
189 WriteBlock(CurrentBlock
++);
192 void VirtualFAT_ReadBlocks(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
,
193 const uint32_t BlockAddress
,
194 uint16_t TotalBlocks
)
196 uint16_t CurrentBlock
= (uint16_t)BlockAddress
;
198 /* Emulated FAT is performed per-block, pass each requested block index
199 * to the emulated FAT block read function */
200 while (TotalBlocks
--)
201 ReadBlock(CurrentBlock
++);