aboutsummaryrefslogtreecommitdiffstats
path: root/Bootloaders/Incomplete/MassStorage/Lib/VirtualFAT.c
blob: 7a248d87ef4fc199805eaae02655572954589bb4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
             LUFA Library
     Copyright (C) Dean Camera, 2013.

  dean [at] fourwalledcubicle [dot] com
           www.lufa-lib.org
*/

/*
  Copyright 2013  Dean Camera (dean [at] fourwalledcubicle [dot] com)

  Permission to use, copy, modify, distribute, and sell this
  software and its documentation for any purpose is hereby granted
  without fee, provided that the above copyright notice appear in
  all copies and that both that the copyright notice and this
  permission notice and warranty disclaimer appear in supporting
  documentation, and that the name of the author not be used in
  advertising or publicity pertaining to distribution of the
  software without specific, written prior permission.

  The author disclaims all warranties with regard to this
  software, including all implied warranties of merchantability
  and fitness.  In no event shall the author be liable for any
  special, indirect or consequential damages or any damages
  whatsoever resulting from loss of use, data or profits, whether
  in an action of contract, negligence or other tortious action,
  arising out of or in connection with the use or performance of
  this software.
*/

#include "VirtualFAT.h"

static const FATBootBlock_t BootBlock =
	{
		.Bootstrap               = {0xEB, 0x3C, 0x90},
		.Description             = "mkdosfs",
		.SectorSize              = SECTOR_SIZE_BYTES,
		.SectorsPerCluster       = SECTOR_PER_CLUSTER,
		.ReservedSectors         = 1,
		.FATCopies               = 2,
		.RootDirectoryEntries    = (SECTOR_SIZE_BYTES / sizeof(FATDirectoryEntry_t)),
		.TotalSectors16          = LUN_MEDIA_BLOCKS,
		.MediaDescriptor         = 0xF8,
		.SectorsPerFAT           = 1,
		.SectorsPerTrack         = 32,
		.Heads                   = 64,
		.HiddenSectors           = 0,
		.TotalSectors32          = 0,
		.PhysicalDriveNum        = 0,
		.ExtendedBootRecordSig   = 0x29,
		.VolumeSerialNumber      = 0x12345678,
		.VolumeLabel             = "LUFA BOOT  ",
		.FilesystemIdentifier    = "FAT16   ",
		.BootstrapProgram        = {0},
		.MagicSignature          = 0xAA55,
	};

static FATDirectoryEntry_t FirmwareFileEntry =
	{
		.Filename        = "FIRMWARE",
		.Extension       = "BIN",
		.Attributes      = 0,
		.Reserved        = {0},
		.CreationTime    = FAT_TIME(1, 1, 0),
		.CreationDate    = FAT_DATE(14, 2, 1989),
		.StartingCluster = 2,
		.FileSizeBytes   = 2049,
	};


static void WriteBlock(uint16_t BlockNumber)
{
	uint8_t BlockBuffer[512];

	/* Wait until endpoint is ready before continuing */
	if (Endpoint_WaitUntilReady())
	  return;

	Endpoint_Read_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
	Endpoint_ClearOUT();

	printf("WRITE %d\r\n", BlockNumber);
	// TODO: Write to FLASH
}

static void ReadBlock(uint16_t BlockNumber)
{
	uint8_t BlockBuffer[512];
	memset(BlockBuffer, 0x00, sizeof(BlockBuffer));

	printf("READ %d", BlockNumber);

	switch (BlockNumber)
	{
		case 0:
			memcpy(BlockBuffer, &BootBlock, sizeof(FATBootBlock_t));
			printf(" <B>\r\n");
			break;

		case 1:
		case 2:
			printf(" <F>\r\n");

			/* Cluster 0: Media type/Reserved */
			((uint16_t*)&BlockBuffer)[0] = 0xFF00 | BootBlock.MediaDescriptor;

			/* Cluster 1: Reserved */
			((uint16_t*)&BlockBuffer)[1] = 0xFFFF;

			/* Cluster 2 onwards: Cluster chain of FIRMWARE.BIN */
			for (uint16_t i = 0; i < FILE_CLUSTERS(2049); i++)
			{
				((uint16_t*)&BlockBuffer)[i + 2] = i + 3;
			}

			/* Mark last cluster as end of file */
			((uint16_t*)&BlockBuffer)[FILE_CLUSTERS(2049) + 1] = 0xFFFF;
			break;

		case 3:
			printf("<R>\r\n");
			memcpy(BlockBuffer, &FirmwareFileEntry, sizeof(FATDirectoryEntry_t));
			break;

		default:
			if ((BlockNumber >= 4) && (BlockNumber < (4 + FILE_CLUSTERS(FIRMWARE_FILE_SIZE))))
			{
				printf("<D>\r\n");

				for (uint16_t i = 0; i < 512; i++)
				  BlockBuffer[i] = '0' + BlockNumber; //A' + (i % 26);
			}

			break;
	}

	/* Wait until endpoint is ready before continuing */
	if (Endpoint_WaitUntilReady())
	  return;

	Endpoint_Write_Stream_LE(BlockBuffer, sizeof(BlockBuffer), NULL);
	Endpoint_ClearIN();
}

void VirtualFAT_WriteBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
                            const uint32_t BlockAddress,
                            uint16_t TotalBlocks)
{
	uint16_t CurrentBlock = (uint16_t)BlockAddress;

	/* Emulated FAT is performed per-block, pass each requested block index
	 * to the emulation function */
	while (TotalBlocks--)
	  WriteBlock(CurrentBlock++);
}

void VirtualFAT_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
                           const uint32_t BlockAddress,
                           uint16_t TotalBlocks)
{
	uint16_t CurrentBlock = (uint16_t)BlockAddress;

	/* Emulated FAT is performed per-block, pass each requested block index
	 * to the emulation function */
	while (TotalBlocks--)
	  ReadBlock(CurrentBlock++);
}