Minor optimizations and corrections to the incomplete Mass Storage class bootloader.
[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 };
55
56 static FATDirectoryEntry_t FirmwareFileEntry =
57 {
58 .Filename = "FIRMWARE",
59 .Extension = "BIN",
60 .Attributes = 0,
61 .Reserved = {0},
62 .CreationTime = FAT_TIME(1, 1, 0),
63 .CreationDate = FAT_DATE(14, 2, 1989),
64 .StartingCluster = 2,
65 .FileSizeBytes = FIRMWARE_FILE_SIZE,
66 };
67
68
69 static void UpdateFAT12ClusterEntry(uint8_t* FATTable,
70 const uint16_t Index,
71 const uint16_t ChainEntry)
72 {
73 /* Calculate the starting offset of the cluster entry in the FAT12 table */
74 uint8_t FATOffset = (Index * 3) / 2;
75 bool UpperNibble = (((Index * 3) % 2) != 0);
76
77 /* Check if the start of the entry is at an upper nibble of the byte, fill
78 * out FAT12 entry as required */
79 if (UpperNibble)
80 {
81 FATTable[FATOffset] = (FATTable[FATOffset] & 0x0F) | ((ChainEntry & 0x0F) << 4);
82 FATTable[FATOffset + 1] = (ChainEntry >> 4);
83 }
84 else
85 {
86 FATTable[FATOffset] = ChainEntry;
87 FATTable[FATOffset + 1] = (FATTable[FATOffset] & 0xF0) | (ChainEntry >> 8);
88 }
89 }
90
91 static void WriteBlock(const uint16_t BlockNumber)
92 {
93 uint8_t BlockBuffer[SECTOR_SIZE_BYTES];
94
95 /* Buffer the entire block to be written from the host */
96 Endpoint_Read_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
97 Endpoint_ClearOUT();
98
99 if ((BlockNumber >= 4) && (BlockNumber < (4 + (FIRMWARE_FILE_SIZE / SECTOR_SIZE_BYTES))))
100 {
101 uint32_t WriteFlashAddress = (uint32_t)(BlockNumber - 4) * SECTOR_SIZE_BYTES;
102
103 for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i += 2)
104 {
105 /* Disallow writing to the bootloader section */
106 if (WriteFlashAddress > BOOT_START_ADDR)
107 continue;
108
109 if ((WriteFlashAddress % SPM_PAGESIZE) == 0)
110 {
111 /* Erase the given FLASH page, ready to be programmed */
112 boot_page_erase(WriteFlashAddress);
113 boot_spm_busy_wait();
114 }
115
116 /* Write the next data word to the FLASH page */
117 boot_page_fill(WriteFlashAddress, (BlockBuffer[i + 1] << 8) | BlockBuffer[i]);
118 WriteFlashAddress += 2;
119
120 if ((WriteFlashAddress % SPM_PAGESIZE) == 0)
121 {
122 /* Write the filled FLASH page to memory */
123 boot_page_write(WriteFlashAddress - SPM_PAGESIZE);
124 boot_spm_busy_wait();
125 }
126 }
127 }
128 }
129
130 static void ReadBlock(const uint16_t BlockNumber)
131 {
132 uint8_t BlockBuffer[SECTOR_SIZE_BYTES];
133 memset(BlockBuffer, 0x00, sizeof(BlockBuffer));
134
135 switch (BlockNumber)
136 {
137 case 0: /* Block 0: Boot block sector */
138 memcpy(BlockBuffer, &BootBlock, sizeof(FATBootBlock_t));
139
140 /* Add the magic signature to the end of the block */
141 BlockBuffer[SECTOR_SIZE_BYTES - 2] = 0x55;
142 BlockBuffer[SECTOR_SIZE_BYTES - 1] = 0xAA;
143 break;
144
145 case 1: /* Block 1: First FAT12 cluster chain copy */
146 case 2: /* Block 2: Second FAT12 cluster chain copy */
147 /* Cluster 0: Media type/Reserved */
148 UpdateFAT12ClusterEntry(BlockBuffer, 0, 0xF00 | BootBlock.MediaDescriptor);
149
150 /* Cluster 1: Reserved */
151 UpdateFAT12ClusterEntry(BlockBuffer, 1, 0xFFF);
152
153 /* Cluster 2 onwards: Cluster chain of FIRMWARE.BIN */
154 for (uint16_t i = 0; i < FILE_CLUSTERS(FIRMWARE_FILE_SIZE); i++)
155 UpdateFAT12ClusterEntry(BlockBuffer, i+2, i+3);
156
157 /* Mark last cluster as end of file */
158 UpdateFAT12ClusterEntry(BlockBuffer, FILE_CLUSTERS(FIRMWARE_FILE_SIZE) + 1, 0xFFF);
159 break;
160
161 case 3: /* Block 3: Root file entries */
162 memcpy(BlockBuffer, &FirmwareFileEntry, sizeof(FATDirectoryEntry_t));
163 break;
164
165 default: /* Blocks 4 onwards: Data allocation section */
166 if ((BlockNumber >= 4) && (BlockNumber < (4 + (FIRMWARE_FILE_SIZE / SECTOR_SIZE_BYTES))))
167 {
168 uint32_t ReadFlashAddress = (uint32_t)(BlockNumber - 4) * SECTOR_SIZE_BYTES;
169
170 for (uint16_t i = 0; i < SECTOR_SIZE_BYTES; i++)
171 BlockBuffer[i] = pgm_read_byte_far(ReadFlashAddress++);
172 }
173
174 break;
175 }
176
177 /* Write the entire read block Buffer to the host */
178 Endpoint_Write_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
179 Endpoint_ClearIN();
180 }
181
182 void VirtualFAT_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
183 const uint32_t BlockAddress,
184 uint16_t TotalBlocks)
185 {
186 uint16_t CurrentBlock = (uint16_t)BlockAddress;
187
188 /* Emulated FAT is performed per-block, pass each requested block index
189 * to the emulated FAT block write function */
190 while (TotalBlocks--)
191 WriteBlock(CurrentBlock++);
192 }
193
194 void VirtualFAT_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
195 const uint32_t BlockAddress,
196 uint16_t TotalBlocks)
197 {
198 uint16_t CurrentBlock = (uint16_t)BlockAddress;
199
200 /* Emulated FAT is performed per-block, pass each requested block index
201 * to the emulated FAT block read function */
202 while (TotalBlocks--)
203 ReadBlock(CurrentBlock++);
204 }
205