Add FLASH writing routine to the VirtualFAT layer of the incomplete Mass Storage...
[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 #include "VirtualFAT.h"
32
33 static const FATBootBlock_t BootBlock =
34 {
35 .Bootstrap = {0xEB, 0x3C, 0x90},
36 .Description = "mkdosfs",
37 .SectorSize = SECTOR_SIZE_BYTES,
38 .SectorsPerCluster = SECTOR_PER_CLUSTER,
39 .ReservedSectors = 1,
40 .FATCopies = 2,
41 .RootDirectoryEntries = (SECTOR_SIZE_BYTES / sizeof(FATDirectoryEntry_t)),
42 .TotalSectors16 = LUN_MEDIA_BLOCKS,
43 .MediaDescriptor = 0xF8,
44 .SectorsPerFAT = 1,
45 .SectorsPerTrack = LUN_MEDIA_BLOCKS % 64,
46 .Heads = LUN_MEDIA_BLOCKS / 64,
47 .HiddenSectors = 0,
48 .TotalSectors32 = 0,
49 .PhysicalDriveNum = 0,
50 .ExtendedBootRecordSig = 0x29,
51 .VolumeSerialNumber = 0x12345678,
52 .VolumeLabel = "LUFA BOOT ",
53 .FilesystemIdentifier = "FAT12 ",
54 .BootstrapProgram = {0},
55 .MagicSignature = 0xAA55,
56 };
57
58 static FATDirectoryEntry_t FirmwareFileEntry =
59 {
60 .Filename = "FIRMWARE",
61 .Extension = "BIN",
62 .Attributes = 0,
63 .Reserved = {0},
64 .CreationTime = FAT_TIME(1, 1, 0),
65 .CreationDate = FAT_DATE(14, 2, 1989),
66 .StartingCluster = 2,
67 .FileSizeBytes = FIRMWARE_FILE_SIZE,
68 };
69
70
71 static void UpdateFAT12ClusterEntry(uint8_t* FATTable,
72 const uint16_t Index,
73 const uint16_t ChainEntry)
74 {
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);
78
79 /* Check if the start of the entry is at an upper nibble of the byte, fill
80 * out FAT12 entry as required */
81 if (UpperNibble)
82 {
83 FATTable[FATOffset] = (FATTable[FATOffset] & 0x0F) | ((ChainEntry & 0x0F) << 4);
84 FATTable[FATOffset + 1] = (ChainEntry >> 4);
85 }
86 else
87 {
88 FATTable[FATOffset] = ChainEntry;
89 FATTable[FATOffset + 1] = (FATTable[FATOffset] & 0xF0) | (ChainEntry >> 8);
90 }
91 }
92
93 static void WriteBlock(const uint16_t BlockNumber)
94 {
95 uint8_t BlockBuffer[SECTOR_SIZE_BYTES];
96
97 /* Wait until endpoint is ready before continuing */
98 if (Endpoint_WaitUntilReady())
99 return;
100
101 Endpoint_Read_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
102 Endpoint_ClearOUT();
103
104 if ((BlockNumber >= 4) && (BlockNumber < (4 + (FIRMWARE_FILE_SIZE / SECTOR_SIZE_BYTES))))
105 {
106 uint32_t WriteFlashAddress = (uint32_t)(BlockNumber - 4) * SECTOR_SIZE_BYTES;
107
108 for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i += 2)
109 {
110 /* Disallow writing to the bootloader section */
111 if (WriteFlashAddress > BOOT_START_ADDR)
112 continue;
113
114 if ((WriteFlashAddress % SPM_PAGESIZE) == 0)
115 {
116 /* Erase the given FLASH page, ready to be programmed */
117 boot_page_erase(WriteFlashAddress);
118 boot_spm_busy_wait();
119 }
120
121 /* Write the next data word to the FLASH page */
122 boot_page_fill(WriteFlashAddress, (BlockBuffer[i + 1] << 8) | BlockBuffer[i]);
123 WriteFlashAddress += 2;
124
125 if ((WriteFlashAddress % SPM_PAGESIZE) == 0)
126 {
127 /* Write the filled FLASH page to memory */
128 boot_page_write(WriteFlashAddress - SPM_PAGESIZE);
129 boot_spm_busy_wait();
130 }
131 }
132 }
133 }
134
135 static void ReadBlock(const uint16_t BlockNumber)
136 {
137 uint8_t BlockBuffer[SECTOR_SIZE_BYTES];
138 memset(BlockBuffer, 0x00, sizeof(BlockBuffer));
139
140 switch (BlockNumber)
141 {
142 case 0:
143 memcpy(BlockBuffer, &BootBlock, sizeof(FATBootBlock_t));
144 break;
145
146 case 1:
147 case 2:
148 /* Cluster 0: Media type/Reserved */
149 UpdateFAT12ClusterEntry(BlockBuffer, 0, 0xF00 | BootBlock.MediaDescriptor);
150
151 /* Cluster 1: Reserved */
152 UpdateFAT12ClusterEntry(BlockBuffer, 1, 0xFFF);
153
154 /* Cluster 2 onwards: Cluster chain of FIRMWARE.BIN */
155 for (uint16_t i = 0; i < FILE_CLUSTERS(FIRMWARE_FILE_SIZE); i++)
156 UpdateFAT12ClusterEntry(BlockBuffer, i+2, i+3);
157
158 /* Mark last cluster as end of file */
159 UpdateFAT12ClusterEntry(BlockBuffer, FILE_CLUSTERS(FIRMWARE_FILE_SIZE) + 1, 0xFFF);
160 break;
161
162 case 3:
163 memcpy(BlockBuffer, &FirmwareFileEntry, sizeof(FATDirectoryEntry_t));
164 break;
165
166 default:
167 if ((BlockNumber >= 4) && (BlockNumber < (4 + (FIRMWARE_FILE_SIZE / SECTOR_SIZE_BYTES))))
168 {
169 uint32_t ReadFlashAddress = (uint32_t)(BlockNumber - 4) * SECTOR_SIZE_BYTES;
170
171 for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i++)
172 BlockBuffer[i] = pgm_read_byte_far(ReadFlashAddress++);
173 }
174
175 break;
176 }
177
178 /* Wait until endpoint is ready before continuing */
179 if (Endpoint_WaitUntilReady())
180 return;
181
182 Endpoint_Write_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
183 Endpoint_ClearIN();
184 }
185
186 void VirtualFAT_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
187 const uint32_t BlockAddress,
188 uint16_t TotalBlocks)
189 {
190 uint16_t CurrentBlock = (uint16_t)BlockAddress;
191
192 /* Emulated FAT is performed per-block, pass each requested block index
193 * to the emulation function */
194 while (TotalBlocks--)
195 WriteBlock(CurrentBlock++);
196 }
197
198 void VirtualFAT_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
199 const uint32_t BlockAddress,
200 uint16_t TotalBlocks)
201 {
202 uint16_t CurrentBlock = (uint16_t)BlockAddress;
203
204 /* Emulated FAT is performed per-block, pass each requested block index
205 * to the emulation function */
206 while (TotalBlocks--)
207 ReadBlock(CurrentBlock++);
208 }
209