LUFA Library - Mass Storage Class Bootloader
VirtualFAT.h File Reference
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <LUFA/Drivers/USB/USB.h>
#include "../BootloaderAPI.h"

Data Structures

struct  FATBootBlock_t
 
union  FATDirectoryEntry_t
 

Macros

#define FLASH_FILE_SIZE_BYTES   (FLASHEND - (FLASHEND - BOOT_START_ADDR) - AUX_BOOT_SECTION_SIZE)
 
#define EEPROM_FILE_SIZE_BYTES   E2END
 
#define SECTOR_PER_CLUSTER   4
 
#define SECTOR_SIZE_BYTES   512
 
#define CLUSTER_SIZE_BYTES   (SECTOR_PER_CLUSTER * SECTOR_SIZE_BYTES)
 
#define FILE_SECTORS(size)   ((size / SECTOR_SIZE_BYTES) + ((size % SECTOR_SIZE_BYTES) ? 1 : 0))
 
#define FILE_CLUSTERS(size)   ((size / CLUSTER_SIZE_BYTES) + ((size % CLUSTER_SIZE_BYTES) ? 1 : 0))
 
#define LUN_MEDIA_BLOCKS   (FILE_SECTORS(FLASH_FILE_SIZE_BYTES) + FILE_SECTORS(EEPROM_FILE_SIZE_BYTES) + 32)
 
#define FAT_TIME(hh, mm, ss)   ((hh << 11) | (mm << 5) | (ss >> 1))
 
#define FAT_DATE(dd, mm, yyyy)   (((yyyy - 1980) << 9) | (mm << 5) | (dd << 0))
 
#define _ROT8(x)   ((((x) & 0xFE) >> 1) | (((x) & 1) ? 0x80 : 0x00))
 
#define FAT_CHECKSUM(n0, n1, n2, n3, n4, n5, n6, n7, e0, e1, e2)   (uint8_t)(_ROT8(_ROT8(_ROT8(_ROT8(_ROT8(_ROT8(_ROT8(_ROT8(_ROT8(_ROT8(n0)+n1)+n2)+n3)+n4)+n5)+n6)+n7)+e0)+e1)+e2)
 
FAT Filesystem Flags
#define FAT_FLAG_READONLY   (1 << 0)
 
#define FAT_FLAG_HIDDEN   (1 << 1)
 
#define FAT_FLAG_SYSTEM   (1 << 2)
 
#define FAT_FLAG_VOLUME_NAME   (1 << 3)
 
#define FAT_FLAG_DIRECTORY   (1 << 4)
 
#define FAT_FLAG_ARCHIVE   (1 << 5)
 
#define FAT_FLAG_LONG_FILE_NAME   0x0F
 
#define FAT_ORDINAL_LAST_ENTRY   (1 << 6)
 

Enumerations

enum  {
  DISK_FILE_ENTRY_VolumeID = 0,
  DISK_FILE_ENTRY_FLASH_LFN = 1,
  DISK_FILE_ENTRY_FLASH_MSDOS = 2,
  DISK_FILE_ENTRY_EEPROM_LFN = 3,
  DISK_FILE_ENTRY_EEPROM_MSDOS = 4
}
 
enum  {
  DISK_BLOCK_BootBlock = 0,
  DISK_BLOCK_FATBlock1 = 1,
  DISK_BLOCK_FATBlock2 = 2,
  DISK_BLOCK_RootFilesBlock = 3,
  DISK_BLOCK_DataStartBlock = 4
}
 

Functions

void VirtualFAT_WriteBlock (const uint16_t BlockNumber) AUX_BOOT_SECTION
 
void VirtualFAT_ReadBlock (const uint16_t BlockNumber) AUX_BOOT_SECTION
 

Macro Definition Documentation

#define _ROT8 (   x)    ((((x) & 0xFE) >> 1) | (((x) & 1) ? 0x80 : 0x00))

Bit-rotates a given 8-bit value once to the right.

Parameters
xValue to rotate right once
Returns
Bit-rotated input value, rotated once to the right.
#define CLUSTER_SIZE_BYTES   (SECTOR_PER_CLUSTER * SECTOR_SIZE_BYTES)

Size of a logical cluster on the disk, in bytes

#define EEPROM_FILE_SIZE_BYTES   E2END

Size of the virtual EEPROM.BIN file in bytes.

#define FAT_CHECKSUM (   n0,
  n1,
  n2,
  n3,
  n4,
  n5,
  n6,
  n7,
  e0,
  e1,
  e2 
)    (uint8_t)(_ROT8(_ROT8(_ROT8(_ROT8(_ROT8(_ROT8(_ROT8(_ROT8(_ROT8(_ROT8(n0)+n1)+n2)+n3)+n4)+n5)+n6)+n7)+e0)+e1)+e2)

Computes the LFN entry checksum of a MSDOS 8.3 format file entry, to associate a LFN entry with its short file entry.

Parameters
n0MSDOS Filename character 1
n1MSDOS Filename character 2
n2MSDOS Filename character 3
n3MSDOS Filename character 4
n4MSDOS Filename character 5
n5MSDOS Filename character 6
n6MSDOS Filename character 7
n7MSDOS Filename character 8
e0MSDOS Extension character 1
e1MSDOS Extension character 2
e2MSDOS Extension character 3
Returns
LFN checksum of the given MSDOS 8.3 filename.
#define FAT_DATE (   dd,
  mm,
  yyyy 
)    (((yyyy - 1980) << 9) | (mm << 5) | (dd << 0))

Converts a given date in DD/MM/YYYY format to a FAT filesystem date.

Parameters
[in]ddDays in the month (1-31)
[in]mmMonths in the year (1-12)
[in]yyyyYear (1980 - 2107)
Returns
Given date encoded as a FAT filesystem datestamp
#define FAT_FLAG_ARCHIVE   (1 << 5)

FAT attribute flag to indicate a file ready for archiving.

#define FAT_FLAG_DIRECTORY   (1 << 4)

FAT attribute flag to indicate a directory entry.

#define FAT_FLAG_HIDDEN   (1 << 1)

FAT attribute flag to indicate a hidden file.

#define FAT_FLAG_LONG_FILE_NAME   0x0F

FAT pseudo-attribute flag to indicate a Long File Name entry.

#define FAT_FLAG_READONLY   (1 << 0)

FAT attribute flag to indicate a read-only file.

#define FAT_FLAG_SYSTEM   (1 << 2)

FAT attribute flag to indicate a system file.

#define FAT_FLAG_VOLUME_NAME   (1 << 3)

FAT attribute flag to indicate a Volume name entry.

#define FAT_ORDINAL_LAST_ENTRY   (1 << 6)

Ordinal flag marker for FAT Long File Name entries to mark the last entry.

#define FAT_TIME (   hh,
  mm,
  ss 
)    ((hh << 11) | (mm << 5) | (ss >> 1))

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.
Parameters
[in]hhHours (0-23)
[in]mmMinutes (0-59)
[in]ssSeconds (0-59)
Returns
Given time encoded as a FAT filesystem timestamp
#define FILE_CLUSTERS (   size)    ((size / CLUSTER_SIZE_BYTES) + ((size % CLUSTER_SIZE_BYTES) ? 1 : 0))

Number of clusters required to store a given size in bytes.

Parameters
[in]sizeSize of the data that needs to be stored
Returns
Number of clusters 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 sectors required to store a given size in bytes.

Parameters
[in]sizeSize of the data that needs to be stored
Returns
Number of sectors required to store the given data on the disk.
#define FLASH_FILE_SIZE_BYTES   (FLASHEND - (FLASHEND - BOOT_START_ADDR) - AUX_BOOT_SECTION_SIZE)

Size of the virtual FLASH.BIN file in bytes.

#define LUN_MEDIA_BLOCKS   (FILE_SECTORS(FLASH_FILE_SIZE_BYTES) + FILE_SECTORS(EEPROM_FILE_SIZE_BYTES) + 32)

Total number of logical sectors/blocks on the disk.

#define SECTOR_PER_CLUSTER   4

Number of sectors that comprise a single logical disk cluster.

#define SECTOR_SIZE_BYTES   512

Size of a single logical sector on the disk.

Enumeration Type Documentation

anonymous enum

Enum for the Root FAT file entry indexes on the disk. This can be used to retrieve the current contents of a known directory entry.

Enumerator
DISK_FILE_ENTRY_VolumeID 

Volume ID directory entry, giving the name of the virtual disk.

DISK_FILE_ENTRY_FLASH_LFN 

Long File Name FAT file entry of the virtual FLASH.BIN image file.

DISK_FILE_ENTRY_FLASH_MSDOS 

Legacy MSDOS FAT file entry of the virtual FLASH.BIN image file.

DISK_FILE_ENTRY_EEPROM_LFN 

Long File Name FAT file entry of the virtual EEPROM.BIN image file.

DISK_FILE_ENTRY_EEPROM_MSDOS 

Legacy MSDOS FAT file entry of the virtual EEPROM.BIN image file.

anonymous enum

Enum for the physical disk blocks of the virtual disk.

Enumerator
DISK_BLOCK_BootBlock 

Boot sector disk block.

DISK_BLOCK_FATBlock1 

First copy of the FAT table block.

DISK_BLOCK_FATBlock2 

Second copy of the FAT table block.

DISK_BLOCK_RootFilesBlock 

Root file and directory entries block.

DISK_BLOCK_DataStartBlock 

Start block of the disk data section.

Function Documentation

void VirtualFAT_ReadBlock ( const uint16_t  BlockNumber)

Reads a block of data from the virtual FAT filesystem, and sends it to the host via the USB Mass Storage interface.

Parameters
[in]BlockNumberIndex of the block to read.
void VirtualFAT_WriteBlock ( const uint16_t  BlockNumber)

Writes a block of data to the virtual FAT filesystem, from the USB Mass Storage interface.

Parameters
[in]BlockNumberIndex of the block to write.