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
= 2049,
70 static void WriteBlock(uint16_t BlockNumber
)
72 uint8_t BlockBuffer
[512];
74 /* Wait until endpoint is ready before continuing */
75 if (Endpoint_WaitUntilReady())
78 Endpoint_Read_Stream_LE(BlockBuffer
, sizeof(BlockBuffer
), NULL
);
81 printf("WRITE %d\r\n", BlockNumber
);
82 // TODO: Write to FLASH
85 void WriteFAT12ClusterEntry(uint8_t* FATTable
, uint8_t Index
, uint16_t NextEntry
)
87 uint8_t StartOffset
= ((uint16_t)Index
* 3) / 2;
89 /* Check if the start of the entry is at an upper nibble of the byte */
90 if (((uint16_t)Index
* 3) % 2)
92 FATTable
[StartOffset
] |= ((NextEntry
& 0x0F) << 4);
93 FATTable
[StartOffset
+ 1] = (NextEntry
>> 4);
97 FATTable
[StartOffset
] = NextEntry
;
98 FATTable
[StartOffset
+ 1] = (NextEntry
>> 8);
102 static void ReadBlock(uint16_t BlockNumber
)
104 uint8_t BlockBuffer
[512];
105 memset(BlockBuffer
, 0x00, sizeof(BlockBuffer
));
107 printf("READ %d", BlockNumber
);
112 memcpy(BlockBuffer
, &BootBlock
, sizeof(FATBootBlock_t
));
120 /* Cluster 0: Media type/Reserved */
121 WriteFAT12ClusterEntry(BlockBuffer
, 0, 0xF00 | BootBlock
.MediaDescriptor
);
123 /* Cluster 1: Reserved */
124 WriteFAT12ClusterEntry(BlockBuffer
, 1, 0xFFF);
126 /* Cluster 2 onwards: Cluster chain of FIRMWARE.BIN */
127 for (uint16_t i
= 0; i
< FILE_CLUSTERS(2049); i
++)
128 WriteFAT12ClusterEntry(BlockBuffer
, i
+2, i
+3);
130 /* Mark last cluster as end of file */
131 WriteFAT12ClusterEntry(BlockBuffer
, FILE_CLUSTERS(2049) + 1, 0xFFF);
136 memcpy(BlockBuffer
, &FirmwareFileEntry
, sizeof(FATDirectoryEntry_t
));
140 if ((BlockNumber
>= 4) && (BlockNumber
< (4 + FILE_CLUSTERS(FIRMWARE_FILE_SIZE
))))
144 for (uint16_t i
= 0; i
< 512; i
++)
145 BlockBuffer
[i
] = '0' + BlockNumber
; //A' + (i % 26);
151 /* Wait until endpoint is ready before continuing */
152 if (Endpoint_WaitUntilReady())
155 Endpoint_Write_Stream_LE(BlockBuffer
, sizeof(BlockBuffer
), NULL
);
159 void VirtualFAT_WriteBlocks(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
,
160 const uint32_t BlockAddress
,
161 uint16_t TotalBlocks
)
163 uint16_t CurrentBlock
= (uint16_t)BlockAddress
;
165 /* Emulated FAT is performed per-block, pass each requested block index
166 * to the emulation function */
167 while (TotalBlocks
--)
168 WriteBlock(CurrentBlock
++);
171 void VirtualFAT_ReadBlocks(USB_ClassInfo_MS_Device_t
* const MSInterfaceInfo
,
172 const uint32_t BlockAddress
,
173 uint16_t TotalBlocks
)
175 uint16_t CurrentBlock
= (uint16_t)BlockAddress
;
177 /* Emulated FAT is performed per-block, pass each requested block index
178 * to the emulation function */
179 while (TotalBlocks
--)
180 ReadBlock(CurrentBlock
++);