如题:
unsigned char FAT_Init(void)//Initialize of FAT need initialize SD first
{
bootsector710 *bs = 0; //定义DBR结构体,包含BPB部分(12~90字节是BPB)
bpb710 *bpb = 0; //定义BPB结构体
partrecord *pr = 0; //定义硬盘信息 结构休 (16字节)
DWORD hidsec=0;
u32 Capacity,DBR_Sector;
u8 i;
Capacity = SD_GetCapacity(); //得到SD容量,单位为字节
if(Capacity<0xff)return 1;
if(SD_ReadSingleBlock(0,fat_buffer))return 2; //读物理扇区0。即MBR(其中MBR内的硬盘分区表中有DBR的地址信息),如没有MBR,则为DBR。经测试,我的SD卡是有MBR的
bs = (bootsector710 *)fat_buffer; //(bootsector710 *)fat_buffer:把数组fat_buffer转换为指向结构类型bootsector710 的指针,然后赋给结构指针bs
pr = (partrecord *)((partsector *)fat_buffer)->psPart;//first partition 第一分区
hidsec = pr->prStartLBA;//the hidden sectors 得DBR所在的物理扇区偏移扇区数。在DBR之前的扇区数是隐藏的。比如说这里DBR的偏移,为什么没有小端转大端,读出的数据经验证是对的???
if(hidsec >= Capacity/512)hidsec = 0;
else
{
if(SD_ReadSingleBlock(pr->prStartLBA,fat_buffer))return 3;//read the bpb sector 即DBR扇区,里面的(第12~90字节是BPB)
bs = (bootsector710 *)fat_buffer;
if(bs->bsJump[0]!=0xE9 && bs->bsJump[0]!=0xEB)
{
hidsec = 0;
if(SD_ReadSingleBlock(0,fat_buffer))return 4;//read the bpb sector
bs = (bootsector710 *)fat_buffer;
}
}
if(bs->bsJump[0]!=0xE9 && bs->bsJump[0]!=0xEB)return 5;//对付没有bootsect的sd卡 //dead with the card which has no bootsect
bpb = (bpb710 *)bs->bsBPB;
if(bpb->bpbFATsecs)//detemine thd FAT type //do not support FAT12
{
FAT32_Enable=0; //FAT16
FATsectors = bpb->bpbFATsecs;//FAT表占用的扇区数
FirstDirClust = 2;
}
else
{
FAT32_Enable=1; //FAT32
FATsectors = bpb->bpbBigFATsecs;//FAT占用的扇区数
FirstDirClust = bpb->bpbRootClust;
}
BytesPerSector = bpb->bpbBytesPerSec; //每扇区字节数
SectorsPerClust = (BYTE)bpb->bpbSecPerClust;//每簇扇区数
FirstFATSector = bpb->bpbResSectors+hidsec;//第一个FAT表扇区
RootDirCount = bpb->bpbRootDirEnts; //根目录项数
RootDirSectors = (RootDirCount*32)>>9; //根目录占用的扇区数
FirstDirSector = FirstFATSector+bpb->bpbFATs*FATsectors;//第一个目录扇区
FirstDataSector = FirstDirSector+RootDirSectors;//第一个数据扇区
return 0;
}
|