📄 fat.c
字号:
#if F_USE_FAT32 == 1
if (fat_type_sign==F_TYPE_FAT32)
{
temp32 = fat_start_sector + (cluster_index / (SECTOR_SIZE / 4));
temp8 = hard_read_sector(temp32); /*读此簇所在FAT扇区*/
if(temp8 != F_OK)
return READ_DATA_ERR;
while(1)
{
index=(cluster_index % (SECTOR_SIZE / 4))*4; /*计算此簇此扇区里的偏移*/
if(number==1) /*到结尾簇*/
{
if(w_mode==F_DESIGN_FAT)
ulong_write_data(index, last_cluster);
else
ulong_write_data(index, EMPTY_CLUSTER);
if(fat_bpb_num_fats==F_TRUE) /*有两个FAT表*/
{
temp8 =hard_write_sector(temp32 + fat_count_fat_sector);
if(temp8 !=F_OK)
return WRITE_DATA_ERR;
}
temp8 =hard_write_sector(temp32);
if(temp8 !=F_OK)
return WRITE_DATA_ERR;
else
return F_OK;
}
if(w_mode==F_DESIGN_FAT)
ulong_write_data(index, (cluster_index+1));
else
ulong_write_data(index, EMPTY_CLUSTER);
#ifdef FAT_DEBUG
com_prints("index:",0);
com_send_arry(&index,2,1);
com_prints("cluster_index:",0);
com_send_arry(&cluster_index,4,1);
com_prints("number:",0);
com_send_arry(&number,2,1);
com_prints("temp32:",0);
com_send_arry(&temp32,4,1);
#endif
cluster_index++;
if(cluster_index % 128==0) /*到新的FAT扇区*/
{
temp8=hard_write_sector(temp32); /*更新此扇区,并读下一个FAT扇区*/
if(temp8!=F_OK)
return WRITE_DATA_ERR;
if(fat_bpb_num_fats==F_TRUE) /*有两个FAT表*/
{
temp8=hard_write_sector(temp32+fat_count_fat_sector);
if(temp8!=F_OK)
return WRITE_DATA_ERR;
}
temp32++;
temp8=hard_read_sector(temp32);
if(temp8!=F_OK)
return READ_DATA_ERR;
}
number--;
}
}
#endif
#if F_USE_FAT16 == 1
if(fat_type_sign==F_TYPE_FAT16)
{
temp32=fat_start_sector + (cluster_index / (SECTOR_SIZE / 2));
temp8=hard_read_sector(temp32);
if(temp8!=F_OK)
return READ_DATA_ERR;
while(1)
{
index=(cluster_index % (SECTOR_SIZE / 2))*2;
if(number==1)
{
if(w_mode==F_DESIGN_FAT)
ushort_write_data(index,(UINT16)last_cluster);
else
ushort_write_data(index,(UINT16)EMPTY_CLUSTER);
if(fat_bpb_num_fats==F_TRUE)
{
temp8=hard_write_sector(temp32+fat_count_fat_sector);
if(temp8!=F_OK)
return WRITE_DATA_ERR;
}
temp8=hard_write_sector(temp32);
if(temp8!=F_OK)
return WRITE_DATA_ERR;
else
return F_OK;
}
if(w_mode==F_DESIGN_FAT)
ushort_write_data(index,(UINT16)(cluster_index+1));
else
ushort_write_data(index,(UINT16)EMPTY_CLUSTER);
cluster_index++;
if(cluster_index % 256==0) /*到新的FAT扇区*/
{
temp8=hard_write_sector(temp32); /*更新此扇区,并读下一个FAT扇区*/
if(temp8!=F_OK)
return WRITE_DATA_ERR;
if(fat_bpb_num_fats==F_TRUE)
{
temp8=hard_write_sector(temp32+fat_count_fat_sector);
if(temp8!=F_OK)
return WRITE_DATA_ERR;
}
temp32++;
temp8=hard_read_sector(temp32);
if(temp8!=F_OK)
return READ_DATA_ERR;
}
number--;
}
}
#endif
#if F_USE_FAT12 == 1
if(fat_type_sign==F_TYPE_FAT12)
{
UINT16 temp;
UINT16 tc1;
UINT16 tc2;
UINT8 t_sector;
temp = (UINT16)cluster_index + ((UINT16)cluster_index)/2;
t_sector =temp/SECTOR_SIZE;
temp32 = fat_start_sector + (UINT32)t_sector; /*以下计算此簇在FAT的扇区数*/
temp8 = hard_read_sector(temp32);
if(temp8!=F_OK)
return READ_DATA_ERR;
while(1)
{
temp =(UINT16)cluster_index + ((UINT16)cluster_index)/2;
t_sector =temp/SECTOR_SIZE;
temp32 = fat_start_sector + (UINT32)t_sector;
index =temp % SECTOR_SIZE; /*计算此簇在此扇区的偏移*/
#ifdef FAT_DEBUG
com_prints("index:",0);
com_send_arry(&index,2,1);
com_prints("cluster_index:",0);
com_send_arry(&cluster_index,4,1);
com_prints("number:",0);
com_send_arry(&number,2,1);
#endif
if(w_mode==F_DESIGN_FAT)
{
if(number==1)
tc2 =(UINT16)last_cluster&0X0FFF;
else
tc2 =((UINT16)(cluster_index+1)) &0X0FFF;
}
else
{
tc2 = (UINT16)EMPTY_CLUSTER;
}
if((temp+1)% 512 == 0) /*到边界FAT扇区*/
{
UINT8 td1;
#ifdef FAT_DEBUG
com_prints("t_sector:",0);
com_send_arry(&t_sector,1,1);
#endif
if((t_sector==0)||(t_sector==3)||(t_sector==6)||(t_sector==9))
{
td1=uchar_read_data(511); /*读此簇的低4BIT*/
td1 &=0X0F; /*改变4BIT;*/
td1 |=(UINT8)(tc2<<4);
uchar_write_data(511,td1);
temp8=hard_write_sector(temp32); /*更新此扇区,并读下一个FAT扇区*/
if(temp8!=F_OK)
return WRITE_DATA_ERR;
temp32++;
temp8 =hard_read_sector(temp32);
if(temp8!=F_OK)
return READ_DATA_ERR;
uchar_write_data(0,(UINT8)(tc2>>4)); /*写入此簇的高8BIT*/
}
else
{
td1 =uchar_read_data(511); /*得到簇的低1字节*/
td1 =(UINT8)tc2;
uchar_write_data(511,td1);
temp8=hard_write_sector(temp32); /*更新此扇区,并读下一个FAT扇区*/
if(temp8!=F_OK)
return WRITE_DATA_ERR;
temp32++;
temp8=hard_read_sector(temp32);
if(temp8!=F_OK)
return READ_DATA_ERR;
td1 =uchar_read_data(0); /*读簇的高半字节*/
td1 =0XF0;
td1 =(UINT8)(tc2>>8);
uchar_write_data(0,td1); /*写入此簇的高8BIT*/
}
if(fat_bpb_num_fats==F_TRUE)
{
temp8 =hard_write_sector(temp32+fat_count_fat_sector);
if(temp8!=F_OK)
return WRITE_DATA_ERR;
temp8 =hard_write_sector(temp32); /*更新此扇区,并读下一个FAT扇区*/
if(temp8!=F_OK)
return WRITE_DATA_ERR;
}
if(number==1) /*到剩余的最后一个簇*/
goto p_out;
cluster_index++;
number--;
continue; /*继续改写下一个簇*/
}
tc1=ushort_read_data(index); /*读出此簇*/
if((UINT8)cluster_index&0x01) /*判断此簇的奇偶*/
{ /*为奇,改变高12BIT,保留低4BIT*/
tc1 &=0X000F;
tc1 |=tc2<<4;
}
else /*为偶,改变低12BIT,保留高4BIT*/
{
tc1 &=0XF000;
tc1 |=tc2;
}
ushort_write_data(index,tc1);
/*到偏移2 5 8 11最后一簇读新扇区*/
if(((temp+2)%512 ==0)&&((t_sector ==2)||(t_sector ==5)
||(t_sector ==8)||(t_sector ==11)))
{
temp8 =hard_write_sector(temp32);
if(temp8!=F_OK)
return WRITE_DATA_ERR;
if(fat_bpb_num_fats==F_TRUE)
{
temp8=hard_write_sector(temp32+fat_count_fat_sector);
if(temp8!=F_OK)
return WRITE_DATA_ERR;
}
temp8 =hard_read_sector(temp32+1);
if(temp8!=F_OK)
return READ_DATA_ERR;
}
if(number ==1)
{
p_out:
if(fat_bpb_num_fats==F_TRUE)
{
temp8=hard_write_sector(temp32+fat_count_fat_sector);
if(temp8!=F_OK)
return WRITE_DATA_ERR;
}
temp8 =hard_write_sector(temp32);
if(temp8!=F_OK)
return WRITE_DATA_ERR;
return F_OK;
}
number--;
cluster_index++;
}
}
#endif
return NO_SUPPORT_FS_ERR;
}
#endif
/*==============================================================================
函数名: get_free_cluster
-------------------------------------------------------------------------------
参数:c_num只对构建簇链表有效,要求构建c_num个簇
-------------------------------------------------------------------------------
函数返回:
-------------------------------------------------------------------------------
函数作用:得到一个空的簇号/构建一个链表(用于创建文件后写入数据)/收集所有的空簇数
-------------------------------------------------------------------------------
==============================================================================*/
#if F_EN_WRITE ==1
STATIC UINT32 get_free_cluster(UINT8 g_mode, UINT16 c_num)
{
UINT8 temp8;
UINT16 temp16;
UINT32 temp32;
BOOL free_bit;
BOOL f_in12;
BOOL f_inc; /*第一次进入簇收集模块*/
BOOL f_in32;
UINT16 cc; /*计算当前收集到了多少簇*/
UINT32 cluster_number; /*当前所在簇号*/
UINT32 count_free_cluster; /*用于计数空的簇数*/
f_inc=0;
f_in12=0;
f_in32=0;
free_bit=0;
count_free_cluster=0;
cluster_number=0XFFFFFFFF;
if(fat_disk_end_bit==F_DISK_END)
return F_ERROR; /*返回1,而不存在簇号为1的簇,从而检测出得到空簇失败*/
do
{
cluster_number++; /*指向0簇*/
#ifdef FAT_DEBUG
com_prints("cluster_number",0);
com_send_arry(&cluster_number,4,1);
#endif
#if F_USE_FAT32 == 1
if (fat_type_sign==F_TYPE_FAT32)
{
if((cluster_number % (SECTOR_SIZE / 4)==0)||(f_in32==0))
{
if(f_in32==0)
{
f_in32=1;
if(fat_fsi_nxt_free!=0XFFFFFFFF)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -