📄 fat.h~
字号:
//---------------------------------------------------------------------
//FileName : fat.h
//Created by : nasif
//Modified by : ZhengYanbo
//Last Modified: 19/10/2005
//function : implementation to interface ZIPAMP with FAT filesystem
//Copyright 2003 Nasif Akand (nasif@yifan.net)
//http://go.to/zipamp
//http://zipamp.virtualave.net
//This file is part of ZipAmp MP3 software.
//Now ZipAmp can support MMC/SD Card.
//---------------------------------------------------------------------
#ifndef __FAT_H__
#define __FAT_H__
#include "type.h";
/*
Definitions for initializing and controlling FAT file system. To reduce memory
some "dirty tricks" has been used. Comments are given for any shortcut or dirty methods.
byte = unsigned char
word = unsigned int
dword = unsigned long
*/
#define FAT12 12 /* FAT12 type */
#define FAT16 16 /* FAT16 type */
#define FAT32 32 /* FAT32 type */
#define OTHER 255 /* unknow format */
#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
// Boot signature
#define BOOTSIG0 0x55
#define BOOTSIG1 0xaa
#define EXBOOTSIG 0x29
//For Win9x (DOS 7.10) BPB
#define FATNUM 0xf /* mask for numbering active FAT */
#define FATMIRROR 0x80 /* FAT is mirrored (like it always was) */
#define FSVERS 0 /* currently only 0 is understood */
//For dirEntry
#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 ATTR_NORMAL 0x00 /* normal file */
#define ATTR_READ_ONLY 0x01 /* file is readonly */
#define ATTR_HIDDEN 0x02 /* file is hidden */
#define ATTR_SYSTEM 0x04 /* file is a system file */
#define ATTR_VOLUME_ID 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 */
#define LCASE_BASE 0x08 /* filename base in lower case */
#define LCASE_EXT 0x10 /* filename extension in lower case */
#define ATTR_LONG_NAME 0x0f
#define ATTR_LONG_NAME_MASK 0x3f //ATTR_READ_ONLY | ATTR_HIDDEN | ATTR_SYSTEM | ATTR_VOLUME_ID | ATTR_DIRECTORY | ATTR_ARCHIVE
//For winentry (windows long file name entry)
#define WIN_LAST 0x40
#define WIN_CNT 0x3f
#define ATTR_WIN95 0x0f
#define WIN_CHARS 13 /* Number of chars per winentry */
#define WIN_MAXLEN 255 // Maximum filename length in Win95
//Note: Must be < sizeof(dirent.d_name)
struct driveID {
word res1; //reserved 0
word cylinder; //# of cylinders 1
word res2; //reserved 2
word head; //# of head 3
word obsolete[2];//obsolete data 5
word sector; //# of sectors 6
word skip[16]; //skipping 40 bytes 7-26
byte firmware[8];//firmware number
byte model[40]; //model name string, stored word by word 27-46
//each word has two characters, and they are stored
//in little endian structure. "SEAGATE" would be stored
//as "ESGATA",0,"E" (0 is the null character)
word res3[2]; // 47-48
word detect; // 49
word skip2[10]; // 50-59
word totalSecLo;
word totalSecHi;
// dword totalSec; //total LBA sectors 60-61
//...............
//There are more structures but we don't use them.
};
struct driveSize {
word nothing[60];
word totalSecLo;
word totalSecHi;
};
//Naming shotcuts:
// part = partition
// Sec = Sector
// Cyl = Cylinder
// Rec = Record
//BPB = Bios Parameter Block
struct partRec { //16 byte partition definition
byte isActive; /* 0x80 indicates active partition */
byte startHead; /* starting head for partition */
word startCylSec; /* starting cylinder and sector */
byte type; /* partition type (see above) */
byte endHead; /* ending head for this partition */
word endCylSect; /* ending cylinder and sector */
dword startLBA; /* first LBA sector for this partition */
dword size; /* size of this partition (bytes or sectors ?) */
};
struct partSecRec {
// From a 512 byte sector, 446 bytes must be skipped before entering
// data to this record. This data is boot executable code for PC
// but we do not need it. This will also keep memory requirement at
// low. We are not using any external memroy, therefore only RAM we
// have is 512 byte of AVR internal SRAM. The skipping is done init function.
//byte skip[446] /* Skip 446 bytes.
struct partRec partRecs[4]; /* four partition records (64 bytes) */
byte bootSectSig0; /* two signature bytes (2 bytes) */
byte bootSectSig1;
// #define BOOTSIG0 0x55
// #define BOOTSIG1 0xaa
};
//==========================================================
/*
* Format of a boot sector. This is the first sector on a DOS floppy disk
* or the fist sector of a partition on a hard disk. But, it is not the
* first sector of a partitioned hard disk.
*/
//==============================================================================
// BIOS Parameter Block (BPB) for different DOS versions
//==============================================================================
typedef struct bootSecExt {
char driveNumber; /* drive number (0x80) */
char reserved1; /* reserved */
char bootSignature; /* ext. boot signature (0x29) */
//#define EXBOOTSIG 0x29
dword volumeID; /* volume ID number */
char volumeLabel[11]; /* volume label */
char fileSysType[8]; /* fs type (FAT12 or FAT16) */
} bootSecExtType;
//=======================================================================================
//BPB for Win9x (DOS 7.10)
typedef struct bpb710 {
word bytesPerSec; /* bytes per sector */
byte secPerClust; /* sectors per cluster */
word resSectors; /* number of reserved sectors */
byte FATs; /* number of FATs */
word rootDirEnts; /* number of root directory entries */
word totSec16; /* total number of sectors */
byte media; /* media descriptor */
word FATSz16; /* number of sectors per FAT */
word secPerTrack; /* sectors per track */
word heads; /* number of heads */
dword hiddenSecs; /* # of hidden sectors */
/* DOS 3.3 compatibility ends here */
dword totSec32; /* # of sectors if parition is FAT32 */
/* DOS 5.0 compatibility ends here */
dword FATSz32; /* like bpbFATsecs for FAT32 */
word extFlags; /* extended flags: */
//#define FATNUM 0xf /* mask for numbering active FAT */
//#define FATMIRROR 0x80 /* FAT is mirrored (like it always was) */
word FSVers; /* filesystem version */
//#define FSVERS 0 /* currently only 0 is understood */
dword rootClust; /* start cluster for root directory */
word FSInfo; /* filesystem info structure sector */
word backup; /* backup boot sector */
byte skip[12]; /* This is 12 byte filler, we skip it */
} bpb710Type;
//Windows 9x (DOS 7.10)boot sector(First sector of that partition)
struct bootSec710 {
byte jump[3]; /* jump inst E9xxxx or EBxx90 3 byte*/
char oemName[8]; /* OEM name and version 8 byte */
bpb710Type BPB; /* BIOS parameter block 53 */
bootSecExtType ext; /* Bootsector Extension 6 byte*/
//Must skip 418 bytes, reason is given in struct partSecRec
//byte skip[418]; /* skip bytes */
byte bootSectSig2; /* 2 & 3 are only defined for FAT32? */
byte bootSectSig3;
byte bootSectSig0;
byte bootSectSig1;
};
/*
* FAT32 FSInfo block.
*/
struct FAT32fsInfo {
//skip 484 bytes
//byte sig1[4]; //double word, signatrue is: 0x52 52 61 41
//BYTE skip[480];
dword sig2; //dword, signature is: 0x61 41 72 72
dword freeCluster; //# of free clusters
dword nxtfree; //Cluster # of cluster that was most recentrly allocated
//Following parameters are not used in ZipAmp thus not put into record.
// byte reserved[12]; //reserved 12 bytes
// byte notNeeded[2];
// byte sig3[4]; //Boot record signature, 0xAA 55 00 00
};
//File name in directory entry, 32 byte
struct direntry {
byte name[8]; /* filename, blank filled */
byte extension[3]; /* extension, blank filled */
byte attributes; /* file attributes */
byte lowerCase; /* NT VFAT lower case flags */
byte CHundredth; /* hundredth of seconds in CTime */
byte CTime[2]; /* create time */
byte CDate[2]; /* create date */
byte ADate[2]; /* access date */
word highClust; /* high bytes of cluster number */
byte MTime[2]; /* last update time */
byte MDate[2]; /* last update date */
word startCluster; /* starting cluster of file */
dword fileSize; /* size of file in bytes */
};
//Long file name structure
struct winentry {
byte Cnt; //Start offset of text in name
word Part1[5]; //First 5 bytes of name, size is word because of UNICODE
byte Attr; //Attributes;
byte Reserved1;
byte Chksum;
word Part2[6]; //Next 6 bytes of name
word Reserved2;
word Part3[2]; //Last 2 bytes, total 13 bytes
};
struct configuration { //This structure is saved by ZipAmp in ZIPAMP.CFG file to store player state.
byte Sign1; //If data valid Sign1 = 0xA5
byte SoundMode; //SoundMode
byte PlayMode; //0=Continue, 1=Repeat, 2=Random, 3=Dir
//dword DriveSize; //Drive size
word TotalFiles; //Total mp3 files
word LastFile; //Number of last file played
byte volume; //volume value of the system
byte Sign2; //If data valid Sign2=0x5A
//Total 7 bytes
//These records aren't used yet.
// dword PlayListCount; //Total number of play list files upto 4
// dword PlayListClust[4];//Start cluster address of play list file
// byte PlayListNum; //Last play list used
// word PlayListFileNum; //Last played back file number from last play list used
};
//***************************************************
// function define
//***************************************************
byte ATA_getFileSystemInfo(void);
byte getDirEntry(byte onlyMP3);
dword findNextCluster();
unsigned long clust2LBA(unsigned long clust);
//unsigned long LBA2Cluster(unsigned long LBA);
void saveConfig(); //Saves the configuration record in ZIPAMP.CFG file
byte loadConfig(); //Loads configuration record from ZIPAMP.CFG file
//void fat32PushDir(); //Pushes directory return address into dir stack file ZIPAMP.SYS
//void fat32PopDir(); //Pops directory address from dir stack ZIPAMP.SYS
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -