7b0c5dda5298e8073cc78557eca76c4a28164dc6
[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 .BlockSize = VIRTUAL_MEMORY_BLOCK_SIZE,
38 .BlocksPerAllocationUnit = ALLOCATION_UNIT_BLOCKS,
39 .ReservedBlocks = 1,
40 .FATCopies = 2,
41 .RootDirectoryEntries = 512,
42 .TotalBlocks16 = LUN_MEDIA_BLOCKS,
43 .MediaDescriptor = 0xF8,
44 .BlocksPerFAT = 1,
45 .BlocksPerTrack = 32,
46 .Heads = 64,
47 .HiddenBlocks = 0,
48 .TotalBlocks32 = 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 = (1 << 11) | (1 << 5),
65 .CreationDate = (9 << 9) | (2 << 5) | (14 << 0),
66 .StartingCluster = 4,
67 .FileSize = (FLASHEND + 1UL),
68 };
69
70 static void WriteBlock(uint16_t BlockNumber)
71 {
72 uint8_t BlockBuffer[512];
73
74 /* Wait until endpoint is ready before continuing */
75 if (Endpoint_WaitUntilReady())
76 return;
77
78 Endpoint_Read_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
79 Endpoint_ClearOUT();
80
81 // TODO: Write to FLASH
82 }
83
84 static void ReadBlock(uint16_t BlockNumber)
85 {
86 uint8_t BlockBuffer[512];
87 memset(BlockBuffer, 0x00, sizeof(BlockBuffer));
88
89 switch (BlockNumber)
90 {
91 case 0:
92 memcpy(BlockBuffer, &BootBlock, sizeof(FATBootBlock_t));
93 break;
94
95 case 1:
96 case 2:
97 /* Cluster 0: Media type/Reserved */
98 ((uint16_t*)&BlockBuffer)[0] = 0xFF00 | BootBlock.MediaDescriptor;
99
100 /* Cluster 1: Reserved */
101 ((uint16_t*)&BlockBuffer)[1] = 0xFFFF;
102
103 /* Cluster 2: Reserved */
104 ((uint16_t*)&BlockBuffer)[2] = 0xFFFF;
105
106 /* Cluster 3: FIRMWARE.BIN File Entry */
107 ((uint16_t*)&BlockBuffer)[3] = 0xFFFF;
108
109 /* Cluster 4 onwards: Cluster chain of FIRMWARE.BIN */
110 for (uint16_t i = 0; i < ((FLASHEND + 1) / (VIRTUAL_MEMORY_BLOCK_SIZE * ALLOCATION_UNIT_BLOCKS)); i++)
111 {
112 ((uint16_t*)&BlockBuffer)[i + 4] = i + 5;
113 }
114
115 /* Mark last cluster as end of file */
116 ((uint16_t*)&BlockBuffer)[((FLASHEND + 1) / (VIRTUAL_MEMORY_BLOCK_SIZE * ALLOCATION_UNIT_BLOCKS)) + 4] = 0xFFFF;
117 break;
118
119 case 3:
120 memcpy(BlockBuffer, &FirmwareFileEntry, sizeof(FATDirectoryEntry_t));
121 break;
122
123 default:
124 break;
125 }
126
127 /* Wait until endpoint is ready before continuing */
128 if (Endpoint_WaitUntilReady())
129 return;
130
131 Endpoint_Write_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
132 Endpoint_ClearIN();
133 }
134
135 void VirtualFAT_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
136 const uint32_t BlockAddress,
137 uint16_t TotalBlocks)
138 {
139 uint16_t CurrentBlock = (uint16_t)BlockAddress;
140
141 while (TotalBlocks--)
142 WriteBlock(CurrentBlock++);
143 }
144
145 void VirtualFAT_ReadBlocks(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 while (TotalBlocks--)
152 ReadBlock(CurrentBlock++);
153 }
154