aboutsummaryrefslogtreecommitdiffstats
path: root/Bootloaders/MassStorage/Lib/VirtualFAT.h
blob: af45488981e0ce6bb0ccf8e039c48562e1c3ae1f (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
/*
             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.
*/

#ifndef _VIRTUALFAT_H_
#define _VIRTUALFAT_H_

	/* Includes: */
		#include <avr/io.h>
		#include <avr/pgmspace.h>

		#include <LUFA/Drivers/USB/USB.h>

	/* Macros: */
		/** Size of the virtual FIRMWARE.BIN file in bytes. */
		#define FIRMWARE_FILE_SIZE_BYTES  (FLASHEND - (FLASHEND - BOOT_START_ADDR) + 1UL)

		/** Number of sectors that comprise a single logical disk cluster. */
		#define SECTOR_PER_CLUSTER        4

		/** Size of a single logical sector on the disk. */
		#define SECTOR_SIZE_BYTES         512

		/** Size of a logical cluster on the disk, in bytes */
		#define CLUSTER_SIZE_BYTES        (SECTOR_PER_CLUSTER * SECTOR_SIZE_BYTES)

		/** Number of sectors required to store a given size in bytes.
		 *
		 *  \param[in] size  Size of the data that needs to be stored
		 *
		 *  \return Number of sectors required to store the given data on the disk.
		 */
		#define FILE_SECTORS(size)        ((size / SECTOR_SIZE_BYTES)  + ((size % SECTOR_SIZE_BYTES)  ? 1 : 0))

		/** Number of clusters required to store a given size in bytes.
		 *
		 *  \param[in] size  Size of the data that needs to be stored
		 *
		 *  \return Number of clusters required to store the given data on the disk.
		 */
		#define FILE_CLUSTERS(size)       ((size / CLUSTER_SIZE_BYTES) + ((size % CLUSTER_SIZE_BYTES) ? 1 : 0))

		/** Total number of logical sectors/blocks on the disk. */
		#define LUN_MEDIA_BLOCKS          (FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES) + 32)

		/** Converts a given time in HH:MM:SS format to a FAT filesystem time.
		 *
		 *  \note The minimum seconds resolution of FAT is 2, thus odd seconds
		 *        will be truncated to the previous integer multiple of 2 seconds.
		 *
		 *  \param[in] hh  Hours (0-23)
		 *  \param[in] mm  Minutes (0-59)
		 *  \param[in] ss  Seconds (0-59)
		 *
		 *  \return Given time encoded as a FAT filesystem timestamp
		 */
		#define FAT_TIME(hh, mm, ss)      ((hh << 11) | (mm << 5) | (ss >> 1))

		/** Converts a given date in DD/MM/YYYY format to a FAT filesystem date.
		 *
		 *  \param[in] dd    Days in the month (1-31)
		 *  \param[in] mm    Months in the year (1-12)
		 *  \param[in] yyyy  Year (1980 - 2107)
		 *
		 *  \return Given date encoded as a FAT filesystem datestamp
		 */
		#define FAT_DATE(dd, mm, yyyy)    (((yyyy - 1980) << 9) | (mm << 5) | (dd << 0))

	/* Type Definitions: */
		/** FAT boot block structure definition, used to identify the core
		 *  parameters of a FAT filesystem stored on a disk.
		 *
		 *  \note This definition is truncated to save space; the magic signature
		 *        0xAA55 must be appended to the very end of the block for it to
		 *        be detected by the host as a valid boot block.
		 */
		typedef struct
		{
			uint8_t  Bootstrap[3];
			uint8_t  Description[8];
			uint16_t SectorSize;
			uint8_t  SectorsPerCluster;
			uint16_t ReservedSectors;
			uint8_t  FATCopies;
			uint16_t RootDirectoryEntries;
			uint16_t TotalSectors16;
			uint8_t  MediaDescriptor;
			uint16_t SectorsPerFAT;
			uint16_t SectorsPerTrack;
			uint16_t Heads;
			uint32_t HiddenSectors;
			uint32_t TotalSectors32;
			uint16_t PhysicalDriveNum;
			uint8_t  ExtendedBootRecordSig;
			uint32_t VolumeSerialNumber;
			uint8_t  VolumeLabel[11];
			uint8_t  FilesystemIdentifier[8];
			/* uint8_t  BootstrapProgram[448]; */
			/* uint16_t MagicSignature; */
		} FATBootBlock_t;

		/** FAT legacy 8.3 style directory entry structure definition, used to
		 *  identify the files and folders of FAT filesystem stored on a disk.
		 */
		typedef struct
		{
			uint8_t  Filename[8];
			uint8_t  Extension[3];
			uint8_t  Attributes;
			uint8_t  Reserved[10];
			uint16_t CreationTime;
			uint16_t CreationDate;
			uint16_t StartingCluster;
			uint32_t FileSizeBytes;
		} FATDirectoryEntry_t;

	/* Function Prototypes: */
		#if defined(INCLUDE_FROM_VIRTUAL_FAT_C)
			static void UpdateFAT12ClusterEntry(uint8_t* const FATTable,
			                                    const uint16_t Index,
			                                    const uint16_t ChainEntry);
			static void WriteVirtualBlock(const uint16_t BlockNumber);
			static void ReadVirtualBlock(const uint16_t BlockNumber);
		#endif

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

		void VirtualFAT_ReadBlocks(USB_ClassInfo_MS_Device_t* const MSInterfaceInfo,
		                           const uint32_t BlockAddress,
		                           uint16_t TotalBlocks);
#endif