📄 fat16avr.h
字号:
//############################################################################
//
// File Name : 'fat.c'
// Title : FAT16 file system driver
// Author : Zoltan Gradwohl
// Date : 28/07/2005
// Revised : 28/07/2005
// Version : 1.0
// Target MCU : ATmega128 (2K Byte Internal SRAM required)
// Editor Tabs : 4
//
//
// NOTE: This code is currently below version 1.0, and therefore is considered
// to be lacking in some functionality or documentation, or may not be fully
// tested. Nonetheless, you can expect most functions to work.
//
// This code is distributed under the GNU Public License
// which can be found at http://www.gnu.org/licenses/gpl.txt
//
//############################################################################
#ifndef FAT_H
#define FAT_H
#include "global.h"
// Some useful cluster numbers
#define MSDOSFSROOT 0 // cluster 0 means the root dir
#define CLUST_FREE 0 // cluster 0 also means a free cluster
#define MSDOSFSFREE CLUST_FREE
#define CLUST_FIRST 2 // first legal cluster number
#define CLUST_RSRVD 0xfffffff6 // reserved cluster range
#define CLUST_BAD 0xfffffff7 // a cluster with a defect
#define CLUST_EOFS 0xfffffff8 // start of eof cluster range
#define CLUST_EOFE 0xffffffff // end of eof cluster range
#define FAT12_MASK 0x00000fff // mask for 12 bit cluster numbers
#define FAT16_MASK 0x0000ffff // mask for 16 bit cluster numbers
#define FAT32_MASK 0x0fffffff // mask for FAT32 cluster numbers
// Partition Type used in the partition record
#define PART_TYPE_UNKNOWN 0x00
#define PART_TYPE_FAT12 0x01
#define PART_TYPE_XENIX 0x02
#define PART_TYPE_DOSFAT16 0x04
#define PART_TYPE_EXTDOS 0x05
#define PART_TYPE_FAT16 0x06
#define PART_TYPE_NTFS 0x07
#define PART_TYPE_FAT32 0x0B
#define PART_TYPE_FAT32LBA 0x0C
#define PART_TYPE_FAT16LBA 0x0E
#define PART_TYPE_EXTDOSLBA 0x0F
#define PART_TYPE_ONTRACK 0x33
#define PART_TYPE_NOVELL 0x40
#define PART_TYPE_PCIX 0x4B
#define PART_TYPE_PHOENIXSAVE 0xA0
#define PART_TYPE_CPM 0xDB
#define PART_TYPE_DBFS 0xE0
#define PART_TYPE_BBT 0xFF
// Maximum filename length in Win95
// Note: Must be < sizeof(dirent.d_name)
#define WIN_MAXLEN 255
// This is the format of the contents of the deTime field in the direntry
// structure.
// We don't use bitfields because we don't know how compilers for
// arbitrary machines will lay them out.
#define DT_2SECONDS_MASK 0x1F // seconds divided by 2
#define DT_2SECONDS_SHIFT 0
#define DT_MINUTES_MASK 0x7E0 // minutes
#define DT_MINUTES_SHIFT 5
#define DT_HOURS_MASK 0xF800 // hours
#define DT_HOURS_SHIFT 11
// This is the format of the contents of the deDate field in the direntry
// structure.
#define DD_DAY_MASK 0x1F // day of month
#define DD_DAY_SHIFT 0
#define DD_MONTH_MASK 0x1E0 // month
#define DD_MONTH_SHIFT 5
#define DD_YEAR_MASK 0xFE00 // year - 1980
#define DD_YEAR_SHIFT 9
#define CONTINUE 0
#define OVERWRITE 1
// Prototypes
uint8_t fatInit(void);
void FAT16_Search ( char *Name2Find, // In: File name to find
int8_t select ); // In: Select operation: overwrite or contiue data
void FAT16_Write_Data ( uint32_t Data ); // In:
void FAT16_Finish_Data ( void ); //
int8_t fatSearch_File ( char *Name2Find_, // In: File name to find
uint16_t *FAT_Entry_, // Return: FAT entry where data starts
uint32_t *FileSize_, // Return: Return filesize
uint32_t *Sector_, // Return: Sector which contains file size inforation
uint8_t *SubSector_, // Return: Return which of the 16 32byte long SubSectors contains the file size
uint32_t *DataSector, // Return: Return sector where data starts
uint8_t *Attributes_, // Return: Return Attribute Dir, archive, hidden, system, volume ...
uint8_t *Buffer_); // In: Buffer to use
int8_t fatGetDirEntry ( char *Name2Find, // In: String with name to find in sector
uint16_t *FAT_Entry, // In/Ret: FAT entry where data starts
uint32_t *FileSize, // Return: Return filesize
uint32_t *Sector, // Return: Sector which contains file size inforation
uint8_t *SubSector, // Return: Return which of the 16 32byte long SubSectors contains the file size
uint32_t *DataSector, // Return: Return sector where data starts
uint8_t *Attributes, // Return: Return Attribute Dir, archive, hidden, system, volume ...
uint8_t *Buffer); // In: Select buffer to use
void fatLoad ( uint16_t *FATEntry, // Angabe Start cluster
uint32_t *Adr_FirstSectorOfCluster,// Rückgabe Adresse des ersten Sectors des gew?hlten Clusters
// Rückgabe des n?chsten FAT- Eintrags auf dem das aktuelle FAT-Eintrag zeigt
uint8_t *Buffer); // In: Select buffer to use
void PrintDOSLine ( char *DirNameAdress,
uint16_t CreateDate,
uint16_t CreateTime,
uint32_t FileSize,
uint8_t Dir_Or_Data);
void fatRead_File ( uint16_t *FATEntry, // In: Start clusters of file
uint32_t *Sector_Adress, // In:
uint32_t *FileSize, // In:
uint8_t *Buffer); // In: Select buffer to use
void fatWrite ( uint16_t *FATEntry, // In/Ret: Angabe Start cluster , Rückgabe des n?chsten FAT- Eintrags auf dem das aktuelle FAT-Eintrag zeigt
uint32_t *Adr_FirstSectorOfCluster,// Return: Rückgabe Adresse des ersten Sectors des gew?hlten Clusters
uint8_t *Buffer ); // In: Select buffer to use
void fatWrite_Data ( uint32_t *data, // In:
uint16_t *FATEntry, // In/Ret:
uint32_t *DataSector, // In/Ret:
uint32_t *FileSize, // In/Ret:
uint8_t *Buffer); // In:
void fatWrite_Continue ( uint16_t *FATEntry, // In/Ret: FAT entry where data starts
uint32_t *SectorAdress, // Return: Current data sector adress
uint32_t *FileSize, // Return: Return filesize
uint8_t *Buffer); // In: Select buffer to use
void fat_Delete ( uint16_t *FATEntry, // In/Ret: FAT entry where data starts
uint32_t *FileSize, // Return: Return filesize
uint32_t *Sector, // In: Sector which contains file size inforation
uint8_t *SubSector, // In: Return which of the 16 32byte long SubSectors contains the file size
uint32_t *DataSector, // Return: Sector where data starts
uint8_t *Buffer); // In: Select buffer to use
void fat_Init_File ( uint16_t *FATEntry, // In/Ret: FAT entry where data starts
uint32_t *FileSize, // In: Filesize
uint32_t *Sector, // In: Sector which contains file size inforation
uint8_t *SubSector, // In: Return which of the 16 32byte long SubSectors contains the file size
uint32_t *DataSector, // Return: Sector where data starts
uint8_t *Buffer); // In: Select buffer to use
void Fat_Test ( void );
#define READROOTDIR 0
// number of directory entries in one sector
#define DIRENTRIES_PER_SECTOR 0x10
//############################################################################
// Structure of a dos directory entry.
typedef struct
//############################################################################
{
uint8_t deName[8]; // filename, blank filled
#define SLOT_EMPTY 0x00 // slot has never been used
#define SLOT_E5 0x05 // the real value is 0xe5
#define SLOT_DELETED 0xe5 // file in this slot deleted
#define SLOT_DNt_Entry 0x2E // 'Dot' entry;
uint8_t deExtension[3]; // extension, blank filled
uint8_t deAttributes; // file attributes
#define ATTR_NORMAL 0x00 // normal file
#define ATTR_READONLY 0x01 // file is readonly
#define ATTR_HIDDEN 0x02 // file is hidden
#define ATTR_SYSTEM 0x04 // file is a system file
#define ATTR_VOLUME 0x08 // entry is a volume label
#define ATTR_LONG_FILENAME 0x0f // this is a long filename entry
#define ATTR_DIRECTORY 0x10 // entry is a directory name
#define ATTR_ARCHIVE 0x20 // file is new or modified
uint8_t deLowerCase; // NT VFAT lower case flags
#define LCASE_BASE 0x08 // filename base in lower case
#define LCASE_EXT 0x10 // filename extension in lower case
uint8_t deCHundredth; // hundredth of seconds in CTime
uint16_t deCTime; // create time
uint16_t deCDate; // create date
uint16_t deADate; // access date
uint16_t deHighClust; // high uint8_ts of cluster number
uint16_t deMTime; // last update time
uint16_t deMDate; // last update date
uint16_t deStartCluster; // starting cluster of file
uint32_t deFileSize; // size of file
}DirEntry;
//############################################################################
// Structure of a Win95 long name directory entry
typedef struct
//############################################################################
{
uint8_t weCnt;
#define WIN_LAST 0x40
#define WIN_CNT 0x3f
uint8_t wePart1[10];
uint8_t weAttributes;
#define ATTR_WIN95 0x0f // ATTR_LONG_FILENAME
uint8_t weReserved1;
uint8_t weChksum;
uint8_t wePart2[12];
uint16_t weReserved2;
uint8_t wePart3[4];
}LongDirEntry;
//############################################################################
typedef struct // The 2-byte numbers are stored little endian (low order byte first).
//############################################################################
{
// Code to jump to the bootstrap code
uint8_t bsjmpBoot[3]; // 0-2 Jump to bootstrap (E.g. eb 3c 90; on i86: JMP 003E NOP. One finds either eb xx 90, or e9 xx xx. The position of the bootstrap varies.)
// OS Name
char bsOEMName[8]; // 3-10 OEM name/version (E.g. "IBM 3.3", "IBM 20.0", "MSDOS5.0", "MSWIN4.0".
// BIOS Parameter Block
uint16_t bsBytesPerSec; // 11-12 Number of bytes per sector (512). Must be one of 512, 1024, 2048, 4096.
uint8_t bsSecPerClus; // 13 Number of sectors per cluster (1). Must be one of 1, 2, 4, 8, 16, 32, 64, 128.
uint16_t bsRsvdSecCnt; // 14-15 Number of reserved sectors (1). FAT12 and FAT16 use 1. FAT32 uses 32.
uint8_t bsNumFATs; // 16 Number of FAT copies (2)
uint16_t bsRootEntCnt; // 17-18 Number of root directory entries (224). 0 for FAT32. 512 is recommended for FAT16.
uint16_t bsTotSec16; // 19-20 Total number of sectors in the filesystem (2880). (in case the partition is not FAT32 and smaller than 32 MB)
uint8_t bsMedia; // 21 Media descriptor type -> For hard disks: Value 0xF8 , DOS version 2.0
uint16_t bsNrSeProFAT; // 22-23 Number of sectors per FAT (9). 0 for FAT32.
uint16_t bsSecPerTrk; // 24-25 Number of sectors per track (12)
uint16_t bsNumHeads; // 26-27 Number of heads (2, for a double-sided diskette)
uint32_t bsHiddSec; // 28-31 Number of hidden sectors (0)
uint32_t bsTotSec32; // 32-35 Number of total sectors (in case the total was not given in bytes 19-20)
// Ext. BIOS Parameter Block
uint8_t bsLogDrvNr; // 36 Logical Drive Number (for use with INT 13, e.g. 0 or 0x80)
uint8_t bsReserved; // 37 Reserved (Earlier: Current Head, the track containing the Boot Record) Used by Windows NT: bit 0: need disk check; bit 1: need surface scan
uint8_t bsExtSign; // 38 Extended signature (0x29) Indicates that the three following fields are present. Windows NT recognizes either 0x28 or 0x29.
uint32_t bsParSerNr; // 39-42 Serial number of partition
uint8_t bsVolLbl[11]; // 43-53 Volume label or "NO NAME "
uint8_t bsFileSysType[8]; // 54-61 Filesystem type (E.g. "FAT12 ", "FAT16 ", "FAT ", or all zero.)
// Bootstrap Code
uint8_t bsBootstrap[448]; // Bootstrap
// Signature
uint16_t bsSignature; // 510-511 Signature 55 aa
}BootSector;
//############################################################################
typedef struct // length 16 uint8_ts
//############################################################################
{
uint8_t prIsActive; // Current State of Partition x80 indicates active partition
uint8_t prStartHead; // Beginning of Partition - Head
uint16_t prStartCylSect; // Beginning of Partition - Cylinder/Sector (See Below)
uint8_t prPartType; // Type of Partition
uint8_t prEndHead; // End of Partition - Head
uint16_t prEndCylSect; // End of Partition - Cylinder/Sector
uint32_t prStartLBA; // Number of Sectors Between the MBR and the First Sector in the Partition
uint32_t prSize; // Number of Sectors in the Partition
}PartitionEntry;
//############################################################################
typedef struct // The 2-byte numbers are stored little endian (low order byte first).
//############################################################################
{
uint8_t mbBootCode[446]; // Code to jump to the bootstrap code
PartitionEntry mbPartitionEntries[4]; // 1st..4th Partition Entry
uint16_t mbExecutableMarker; //Executable Marker (55h AAh) 2 Bytes
#define BOOTSIG0 0xaa55
}MasterBootSector;
//############################################################################
union FAT16_Buffer
//############################################################################
{
// data layer one - (unformated data)
uint8_t FATBuffer[512];
// data layer two - (Master Boot Record)
MasterBootSector MBS;
// data layer three - (Volume Boot Record)
BootSector BootSec;
// data layer four - (512byte partitioned in 20 SubSectors) Type: DirEntry
DirEntry direntries[DIRENTRIES_PER_SECTOR];
// data layer five - (512byte partitioned in 20 SubSectors) Type: Long Name DirEntry
LongDirEntry longdirentries[DIRENTRIES_PER_SECTOR];
// data layer six - (data to write on mmc [32bit]~>128 )
uint32_t FATData[128];
};
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -