3 Copyright (C) Dean Camera, 2013.
5 dean [at] fourwalledcubicle [dot] com
10 Copyright 2013 Dean Camera (dean [at] fourwalledcubicle [dot] com)
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.
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
31 #ifndef _VIRTUALFAT_H_
32 #define _VIRTUALFAT_H_
36 #include <avr/pgmspace.h>
38 #include <LUFA/Drivers/USB/USB.h>
40 #include "../BootloaderAPI.h"
43 /** Size of the virtual FIRMWARE.BIN file in bytes. */
44 #define FIRMWARE_FILE_SIZE_BYTES (FLASHEND - (FLASHEND - BOOT_START_ADDR) - AUX_BOOT_SECTION_SIZE)
46 /** Number of sectors that comprise a single logical disk cluster. */
47 #define SECTOR_PER_CLUSTER 4
49 /** Size of a single logical sector on the disk. */
50 #define SECTOR_SIZE_BYTES 512
52 /** Size of a logical cluster on the disk, in bytes */
53 #define CLUSTER_SIZE_BYTES (SECTOR_PER_CLUSTER * SECTOR_SIZE_BYTES)
55 /** Number of sectors required to store a given size in bytes.
57 * \param[in] size Size of the data that needs to be stored
59 * \return Number of sectors required to store the given data on the disk.
61 #define FILE_SECTORS(size) ((size / SECTOR_SIZE_BYTES) + ((size % SECTOR_SIZE_BYTES) ? 1 : 0))
63 /** Number of clusters required to store a given size in bytes.
65 * \param[in] size Size of the data that needs to be stored
67 * \return Number of clusters required to store the given data on the disk.
69 #define FILE_CLUSTERS(size) ((size / CLUSTER_SIZE_BYTES) + ((size % CLUSTER_SIZE_BYTES) ? 1 : 0))
71 /** Total number of logical sectors/blocks on the disk. */
72 #define LUN_MEDIA_BLOCKS (FILE_SECTORS(FIRMWARE_FILE_SIZE_BYTES) + 32)
74 /** Converts a given time in HH:MM:SS format to a FAT filesystem time.
76 * \note The minimum seconds resolution of FAT is 2, thus odd seconds
77 * will be truncated to the previous integer multiple of 2 seconds.
79 * \param[in] hh Hours (0-23)
80 * \param[in] mm Minutes (0-59)
81 * \param[in] ss Seconds (0-59)
83 * \return Given time encoded as a FAT filesystem timestamp
85 #define FAT_TIME(hh, mm, ss) ((hh << 11) | (mm << 5) | (ss >> 1))
87 /** Converts a given date in DD/MM/YYYY format to a FAT filesystem date.
89 * \param[in] dd Days in the month (1-31)
90 * \param[in] mm Months in the year (1-12)
91 * \param[in] yyyy Year (1980 - 2107)
93 * \return Given date encoded as a FAT filesystem datestamp
95 #define FAT_DATE(dd, mm, yyyy) (((yyyy - 1980) << 9) | (mm << 5) | (dd << 0))
97 /* Type Definitions: */
98 /** FAT boot block structure definition, used to identify the core
99 * parameters of a FAT filesystem stored on a disk.
101 * \note This definition is truncated to save space; the magic signature
102 * 0xAA55 must be appended to the very end of the block for it to
103 * be detected by the host as a valid boot block.
107 uint8_t Bootstrap
[3];
108 uint8_t Description
[8];
110 uint8_t SectorsPerCluster
;
111 uint16_t ReservedSectors
;
113 uint16_t RootDirectoryEntries
;
114 uint16_t TotalSectors16
;
115 uint8_t MediaDescriptor
;
116 uint16_t SectorsPerFAT
;
117 uint16_t SectorsPerTrack
;
119 uint32_t HiddenSectors
;
120 uint32_t TotalSectors32
;
121 uint16_t PhysicalDriveNum
;
122 uint8_t ExtendedBootRecordSig
;
123 uint32_t VolumeSerialNumber
;
124 uint8_t VolumeLabel
[11];
125 uint8_t FilesystemIdentifier
[8];
126 /* uint8_t BootstrapProgram[448]; */
127 /* uint16_t MagicSignature; */
130 /** FAT legacy 8.3 style directory entry structure definition, used to
131 * identify the files and folders of FAT filesystem stored on a disk.
136 uint8_t Extension
[3];
138 uint8_t Reserved
[10];
139 uint16_t CreationTime
;
140 uint16_t CreationDate
;
141 uint16_t StartingCluster
;
142 uint32_t FileSizeBytes
;
143 } FATDirectoryEntry_t
;
145 /* Function Prototypes: */
146 #if defined(INCLUDE_FROM_VIRTUAL_FAT_C)
147 static void UpdateFAT12ClusterEntry(uint8_t* const FATTable
,
148 const uint16_t Index
,
149 const uint16_t ChainEntry
) AUX_BOOT_SECTION
;
150 static void WriteVirtualBlock(const uint16_t BlockNumber
) AUX_BOOT_SECTION
;
151 static void ReadVirtualBlock(const uint16_t BlockNumber
) AUX_BOOT_SECTION
;
154 void VirtualFAT_WriteBlocks(const uint16_t BlockAddress
,
155 uint16_t TotalBlocks
) AUX_BOOT_SECTION
;
157 void VirtualFAT_ReadBlocks(const uint16_t BlockAddress
,
158 uint16_t TotalBlocks
) AUX_BOOT_SECTION
;