📄 fat_sys.c
字号:
tmp_cluster.b[3] = gl_buffer[fat_buf_count + 0];
tmp_cluster.b[2] = gl_buffer[fat_buf_count + 1];
tmp_cluster.b[1] = gl_buffer[fat_buf_count + 2];
tmp_cluster.b[0] = gl_buffer[fat_buf_count + 3];
if (tmp_cluster.l == 0x00000000)
cluster_free = TRUE;
if(cluster_free)
{/* free cluster: add it to the list */
if (chain[index].number == MAX_CL_PER_FRAG)
{
index++;
chain[index].number = 1;
chain[index].cluster.l = cluster.l;
}
else
{
chain[index].number++;
}
cluster.l++;
cluster2start.l++;
fat_buf_count += 4;
}
else
{/* cluster already used */
do
{
cluster.l++;
cluster2start.l++;
fat_buf_count += 4;
fat_count_sector();
/*
if(fat_buf_count == fat_sector_size)
{
fat_buf_count = 0;
phy_read_sector();
}
*/
tmp_cluster.b[3] = gl_buffer[fat_buf_count + 0];
tmp_cluster.b[2] = gl_buffer[fat_buf_count + 1];
tmp_cluster.b[1] = gl_buffer[fat_buf_count + 2];
tmp_cluster.b[0] = gl_buffer[fat_buf_count + 3];
if (tmp_cluster.l == 0x00000000)
{
cluster_free = TRUE;
break;
}
}
while(cluster.l < fat_count_of_clusters);
if(!cluster_free)
{
if (chain[index].number == 1)
{ /* last cluster is the current one */
fat_last_clust_index = index;
chain[fat_last_clust_index].number = 0; /* end of list marker */
}
else
{
fat_last_clust_index = index + 1;
chain[index].number--;
chain[fat_last_clust_index].cluster.l = chain[index].cluster.l + chain[index].number;
chain[fat_last_clust_index].number = 0; /* end of list marker */
}
return OK; /* end of partition reached */
}
index++;
chain[index].number = 1;
chain[index].cluster.l = cluster.l;
cluster.l++;
cluster2start.l++;
fat_buf_count +=4;
}
nb_cluster++;
}
while ((index != (nb_frag - 1)) && (cluster.l < fat_count_of_clusters) && (nb_cluster < fat_max_cluster_number));
fat_last_clust_index = index;
return OK;
}
#endif //COMPILE_FAT_32
byte fat_set_clusters (fat_st_clust_chain xdata *chain, Byte nb_frag)
{
if(fat_type_id == FAT_IS_16)
{
#if COMPILE_FAT_16 == TRUE
if(fat16_set_clusters(chain,nb_frag) == KO)
return KO;
#endif
}
else if(fat_type_id == FAT_IS_12)
{
#if COMPILE_FAT_12 == TRUE
if(fat12_set_clusters(chain,nb_frag) == KO)
return KO;
#endif
}
else if(fat_type_id == FAT_IS_32)
{
#if COMPILE_FAT_32 == TRUE
if(fat32_set_clusters(chain,nb_frag) == KO)
return KO;
#endif
}
phy_read_close();
/* Mark end of list -> last record number = 0 */
/* Be careful : 0 value is just a marker */
/* Real value is in fact 1!!! */
if (fat_last_clust_index == nb_frag - 1) /* End of chain cluster -> maximum file size */
{
/* The last index must have the end of list marker */
// fat_last_clust_index = index;
chain[fat_last_clust_index-1].number--;
chain[fat_last_clust_index].cluster.l = chain[fat_last_clust_index - 1].cluster.l + chain[fat_last_clust_index-1].number;
chain[fat_last_clust_index].number = 0; /* end of list marker */
}
else /* max cluster -> end of media */
{
if (chain[fat_last_clust_index].number == 1)
{ /* last cluster is the current one */
// fat_last_clust_index = index;
chain[fat_last_clust_index].number = 0; /* end of list marker */
}
else
{
// fat_last_clust_index = index + 1;
chain[fat_last_clust_index].number--;
fat_last_clust_index++;
chain[fat_last_clust_index].cluster.l = chain[fat_last_clust_index-1].cluster.l + chain[fat_last_clust_index-1].number;
chain[fat_last_clust_index].number = 0; /* end of list marker */
}
}
return OK;
}
/*F**************************************************************************
* NAME: fat_create_entry
*----------------------------------------------------------------------------
* PARAMS:
* cluster: starting cluster
* return:
* - OK : new root entry created
* - KO : no more space in file list
*----------------------------------------------------------------------------
* PURPOSE:
* Create an entry in parent directory
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*
*
* Index of file is determinated:
* if deleted dir like DIRVEXXX exist, then index = min index
* else indice = max index read + 1
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
#if COMPILE_FAT_WRITE == TRUE
Byte fat_create_entry(Uint32 cluster)
{
Uint16 i;
Uint16 j,number=0;
Uint16 count=0;
Uint16 indice;
Uint16 indice_min;
Uint16 indice_max;
Uint16 pos;
Uint16 temp;
Uint16 index;
// Uint16 k;
Uint32 root_sector;
Byte flag;
Byte exist,byExist;
Byte deleted_flag;
// bit del;
Uint16 offset;
Uint16 dir_index;
Uint16 dir_last;
// Uint16 dir_count;
// Uint16 cluster_number;
flag=FALSE;
exist = FALSE;
deleted_flag = FALSE;
indice = 0x00;
index = 0;
dir_index = 0;
dir_last = 0;
/* determinate file index */
indice_min = 0xFFFF;
indice_max = 0x00;
if((dir_is_root == TRUE) && (fat_type_id != FAT_IS_32))
{
phy_read_open(fat_ptr_rdir);
}
else
{
fat_dclust_byte_count = 0;
fat_dchain_index = 0;
fat_dchain_nb_clust = 0;
}
do
{
if(number % fat_sector_size==0)
{
if((dir_is_root == TRUE)&&(fat_type_id != FAT_IS_32))
{
phy_read_sector();
}
else
{
fat_dgetsector();
}
number = 0;
}
j = number;
number += DIR_SIZE ;
if(exist == FALSE)
{
if(gl_buffer[j] == FILE_DELETED)
{
offset = index;
exist = TRUE;
}
}
/* check if it's SUBDIR?? directory */
//if((gl_buffer[11+j] & ATTR_DIRECTORY )!= ATTR_DIRECTORY)
// {
byExist = 0;
if(fat_global_id == FILE_DIR)
{
/*
if((gl_buffer[1 + j] == 'R')&&
(gl_buffer[2 + j] == 'I')&&
(gl_buffer[3 + j] == 'V')&&
(gl_buffer[4 + j] == 'E'))
{
byExist = 1;
}
*/
}
else if(fat_global_id == FILE_MP3)
{
if((gl_buffer[1 + j] == 'O')&&
(gl_buffer[2 + j] == 'I')&&
(gl_buffer[3 + j] == 'C')&&
(gl_buffer[4 + j] == 'E')&&
(gl_buffer[8 + j] == 'M')&&
(gl_buffer[9 + j] == 'P')&&
(gl_buffer[10 + j] == '3'))
{
byExist = 1;
}
}
else if(fat_global_id == FILE_AV6)
{
if((gl_buffer[1 + j] == 'O')&&
(gl_buffer[2 + j] == 'I')&&
(gl_buffer[3 + j] == 'C')&&
(gl_buffer[4 + j] == 'E')&&
(gl_buffer[8 + j] == 'A')&&
(gl_buffer[9 + j] == 'V')&&
(gl_buffer[10 + j] == '6'))
{
byExist = 1;
}
}
else if(fat_global_id == FILE_IMA)
{
if((gl_buffer[1 + j] == 'O')&&
(gl_buffer[2 + j] == 'I')&&
(gl_buffer[3 + j] == 'C')&&
(gl_buffer[4 + j] == 'E')&&
(gl_buffer[8 + j] == 'I')&&
(gl_buffer[9 + j] == 'M')&&
(gl_buffer[10 + j] == 'A'))
{
byExist = 1;
}
}
if(byExist)
{
/* caclulate actual index */
temp = (gl_buffer[5 + j] - '0')*100;
temp += (gl_buffer[6 + j] - '0')*10;
temp += (gl_buffer[7 + j] - '0');
if(gl_buffer[0 + j] == FILE_DELETED) /* if directory is deleted */
{
if(temp <= indice_min)
{
indice_min = temp;
pos = index; /* if index is the lower, take it */
}
deleted_flag = 1;
}
else /* if file not deleted, then save max value */
{
if(temp > indice_max)
{
indice_max = temp;
}
if((exist == FALSE) && (deleted_flag == 0))
{
dir_index++;
}
dir_last++;
}
flag=TRUE;
}
index++;
if(dir_is_root == TRUE)
{
if(index == fat_root_entries)
{
if((gl_buffer[j] !=FILE_NOT_EXIST) && (gl_buffer[j] != FILE_DELETED))
return KO;
break;
}
}
}
while(gl_buffer[0 + j] != FILE_NOT_EXIST);
phy_read_close();
if(indice_min == 0xffff) // don't find an earsed dir //
{
if(flag == TRUE) // subdir exist //
{
indice = indice_max + 1;
}
else // subdir no exist //
{
indice = 0x00;
}
index--;
if(exist == TRUE)
{
index = offset;
}
}
else
{
indice = indice_min;
index = pos;
}
/* construct the entry */
/*
if(exist == TRUE)
{
index = offset;
}
*/
if((dir_is_root == TRUE)&&(fat_type_id != FAT_IS_32))
{
root_sector = fat_ptr_rdir + (index / (fat_sector_size >> 5));/* root sector address */
phy_read_open(root_sector);
j = (index % (fat_sector_size >> 5)) * DIR_SIZE ; /* Position of entry in the sector */
}
else
{
//fat_dir_current_offs = 0;
fat_dir_seek(((Uint32)index) * DIR_SIZE);
root_sector = fat_dir_current_sect;
j = fat_dir_sector_offset;// % fat_sector_size;
}
phy_read_sector();
phy_read_close();
/*
if(fat_global_id == FILE_DIR)
{
gl_buffer[0+j] = 'D';
gl_buffer[1+j] = 'R';
gl_buffer[2+j] = 'I';
gl_buffer[3+j] = 'V';
gl_buffer[4+j] = 'E';
gl_buffer[8+j] = ' ';
gl_buffer[9+j] = ' ';
gl_buffer[10+j] = ' ';
gl_buffer[11+j] = ATTR_DIRECTORY;
}
else
{
*/
gl_buffer[0+j] = 'V';
gl_buffer[1+j] = 'O';
gl_buffer[2+j] = 'I';
gl_buffer[3+j] = 'C';
gl_buffer[4+j] = 'E';
if(fat_global_id == FILE_MP3)
{
gl_buffer[8+j] = 'M';
gl_buffer[9+j] = 'P';
gl_buffer[10+j] = '3';
}
else
{
gl_buffer[8+j] = 'A';
gl_buffer[9+j] = 'V';
gl_buffer[10+j] = '6';
}
gl_buffer[11+j] = 0x00;
// }
gl_buffer[5+j] = '0'+ (indice / 100);
gl_buffer[6+j] = '0' + ((indice % 100) / 10);
gl_buffer[7+j] = '0' + ((indice % 100) % 10);
if(fat_global_id == FILE_DIR)
{
memcpy(gl_buffer+j,lfn_name,8);
gl_buffer[8+j] = ' ';
gl_buffer[9+j] = ' ';
gl_buffer[10+j] = ' ';
gl_buffer[11+j] = ATTR_DIRECTORY;
}
for(i=12;i<32;i++)
gl_buffer[i+j] = 0x00;
if(fat_type_id == FAT_IS_32)
{
gl_buffer[20+j] = (Byte)(cluster >> 16);
gl_buffer[21+j] = (Byte)(cluster >> 24);
}
gl_buffer[26+j] = (Byte)(cluster); /* Low word first cluster number */
gl_buffer[27+j] = (Byte)(cluster >> 8);
//j = (index % 16) * DIR_SIZE ; /* Position of entry in the sector */
if(fat_global_id != FILE_DIR)
{
gl_buffer[j + 28] = fat_file_size.b[3];
gl_buffer[j + 29] = fat_file_size.b[2];
gl_buffer[j + 30] = fat_file_size.b[1];
gl_buffer[j + 31] = fat_file_size.b[0];
}
fat_write_sector(root_sector);
fat_dir_list_index = dir_index;
fat_dir_list_last = dir_last + 1;
fat_dir_seek(index * DIR_SIZE);
return OK;
}
#endif //COMPILE_FAT_WRITE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -