883586c3d6967e67f0bed1d858b39bf879b8f43a
[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 = 32,
46 .Heads = 64,
47 .HiddenSectors = 0,
48 .TotalSectors32 = 0,
49 .PhysicalDriveNum = 0,
50 .ExtendedBootRecordSig = 0x29,
51 .VolumeSerialNumber = 0x12345678,
52 .VolumeLabel = "LUFA BOOT ",
53 .FilesystemIdentifier = "FAT16 ",
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 = 2049,
68 };
69
70
71 static void WriteBlock(uint16_t BlockNumber)
72 {
73 uint8_t BlockBuffer[512];
74
75 /* Wait until endpoint is ready before continuing */
76 if (Endpoint_WaitUntilReady())
77 return;
78
79 Endpoint_Read_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
80 Endpoint_ClearOUT();
81
82 printf("WRITE %d\r\n", BlockNumber);
83 // TODO: Write to FLASH
84 }
85
86 static void ReadBlock(uint16_t BlockNumber)
87 {
88 uint8_t BlockBuffer[512];
89 memset(BlockBuffer, 0x00, sizeof(BlockBuffer));
90
91 printf("READ %d", BlockNumber);
92
93 switch (BlockNumber)
94 {
95 case 0:
96 memcpy(BlockBuffer, &BootBlock, sizeof(FATBootBlock_t));
97 printf(" <B>\r\n");
98 break;
99
100 case 1:
101 case 2:
102 printf(" <F>\r\n");
103
104 /* Cluster 0: Media type/Reserved */
105 ((uint16_t*)&BlockBuffer)[0] = 0xFF00 | BootBlock.MediaDescriptor;
106
107 /* Cluster 1: Reserved */
108 ((uint16_t*)&BlockBuffer)[1] = 0xFFFF;
109
110 /* Cluster 2 onwards: Cluster chain of FIRMWARE.BIN */
111 for (uint16_t i = 0; i < FILE_CLUSTERS(2049); i++)
112 {
113 ((uint16_t*)&BlockBuffer)[i + 2] = i + 3;
114 }
115
116 /* Mark last cluster as end of file */
117 ((uint16_t*)&BlockBuffer)[FILE_CLUSTERS(2049) + 3] = 0xFFFF;
118 break;
119
120 case 3:
121 printf("<R>\r\n");
122 memcpy(BlockBuffer, &FirmwareFileEntry, sizeof(FATDirectoryEntry_t));
123 break;
124
125 default:
126 if ((BlockNumber >= 4) && (BlockNumber < (4 + FILE_CLUSTERS(FIRMWARE_FILE_SIZE))))
127 {
128 printf("<D>\r\n");
129
130 for (uint16_t i = 0; i < 512; i++)
131 BlockBuffer[i] = '0' + BlockNumber; //A' + (i % 26);
132 }
133
134 break;
135 }
136
137 /* Wait until endpoint is ready before continuing */
138 if (Endpoint_WaitUntilReady())
139 return;
140
141 Endpoint_Write_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
142 Endpoint_ClearIN();
143 }
144
145 void VirtualFAT_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
146 const uint32_t BlockAddress,
147 uint16_t TotalBlocks)
148 {
149 uint16_t CurrentBlock = (uint16_t)BlockAddress;
150
151 /* Emulated FAT is performed per-block, pass each requested block index
152 * to the emulation function */
153 while (TotalBlocks--)
154 WriteBlock(CurrentBlock++);
155 }
156
157 void VirtualFAT_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
158 const uint32_t BlockAddress,
159 uint16_t TotalBlocks)
160 {
161 uint16_t CurrentBlock = (uint16_t)BlockAddress;
162
163 /* Emulated FAT is performed per-block, pass each requested block index
164 * to the emulation function */
165 while (TotalBlocks--)
166 ReadBlock(CurrentBlock++);
167 }
168