?? fat_sys.c
字號:
//****************************************************************
// FileName: fat_dir.c
// Description: file system control
// Version: 1.0
// Histrory:
// 1.0 chanjl 2003.11.10 create the module
//****************************************************************
#include "DataType.h"
#include "FTC32t.h"
#include "Define.h"
#include "ftc32t_m.h"
#include "register.h"
#include "global_variable.h"
#include "fat.h"
#include "fat_h.h"
#include <string.h>
void fat_count_sector(void)
{
if(fat_buf_count == fat_sector_size)
{
fat_buf_count=0;
phy_read_sector();
}
}
/*F**************************************************************************
* NAME: fat_get_clusters
*----------------------------------------------------------------------------
* PARAMS:
* chain: allocation list address
* nb_frag: maximum number of fragment
*
* return:
* - OK: allocation done
* - KO: allocation done but truncated: file too much fragmented
*----------------------------------------------------------------------------
* PURPOSE:
* Prepare a list of the file clusters:
* chain[n].cluster contains the starting cluster number of a fragment
* chain[n].number contains the number of contiguous clusters in fragment
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* File cluster list is limited by the nb_frag parameter.
* If memory is too much fragmented, file may not be fully played.
* Last list item always has single cluster
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
#if COMPILE_FAT_16 == TRUE
byte fat16_get_clusters (fat_st_clust_chain xdata *chain, Byte nb_frag)
{
Byte index;
/* index in chain */
Uint16 new_cluster;
Uint16 old_cluster;
nb_frag = nb_frag - 1; /* set limit (index start at 0) */
/* build the first entry of the allocation list */
chain[0].number = 1;
chain[0].cluster.l = fat_cache.current.start_cluster.l - 2; /* 2 = 1st cluster */
old_cluster = fat_cache.current.start_cluster.l;
index = 0;
fat_buf_count = (old_cluster % (fat_sector_size >> 1)) << 1;
phy_read_open(fat_ptr_fats + (old_cluster / (fat_sector_size >> 1)));
/* read first entry */
phy_read_sector();
((Byte*)&new_cluster)[1] = gl_buffer[fat_buf_count];
((Byte*)&new_cluster)[0] = gl_buffer[fat_buf_count + 1];
while (new_cluster != LAST_CLUSTER16) /* loop until last cluster found */
{
if ((new_cluster == (old_cluster + 1)) && (chain[index].number != MAX_CL_PER_FRAG))
{ /* contiguous cluster up to 255 */
chain[index].number++;
fat_buf_count = fat_buf_count + 2;
fat_count_sector();
}
else
{ /* compute fragmentation */
index++;
chain[index].number = 1;
chain[index].cluster.w[1] = new_cluster - 2; /* 2 = 1st cluster */
fat_buf_count=(new_cluster % (fat_sector_size >> 1)) << 1;
phy_read_close();
phy_read_open(fat_ptr_fats + (new_cluster / (fat_sector_size >> 1)));
phy_read_sector();
}
old_cluster = new_cluster;
if (index == nb_frag)
{ /* end of chain reached */
/* last fragment always contains a single cluster */
chain[nb_frag].number = 0; /* end of chain marker */
fat_last_clust_index = nb_frag;
return KO; /* file too much fragmented */
}
else
{
/* read new entry */
((Byte *)&new_cluster)[1]= gl_buffer[0+fat_buf_count];
((Byte *)&new_cluster)[0]= gl_buffer[1+fat_buf_count];
}
}
fat_last_clust_index = index;
// fat_get_clusters_end(chain,index);
return OK;
}
#endif //COMPILE_FAT_16
#if COMPILE_FAT_12 == TRUE
byte fat12_get_clusters (fat_st_clust_chain xdata *chain, Byte nb_frag)
{
byte fat12_parity;
Byte index;
/* index in chain */
// Byte sector_count;
Uint16 i;
Uint16 new_cluster;
Uint16 old_cluster;
// Uint16 count;
Uint16 fat12_cluster;
nb_frag = nb_frag - 1; /* set limit (index start at 0) */
/* build the first entry of the allocation list */
chain[0].number = 1;
chain[0].cluster.l = fat_cache.current.start_cluster.l - 2; /* 2 = 1st cluster */
old_cluster = fat_cache.current.start_cluster.l;
index = 0;
/* compute offset in sector */
phy_read_open(fat_ptr_fats);
phy_read_sector();
fat_buf_count = 0;
fat12_parity = 0;
i = old_cluster + 1;
do
{
if (fat12_parity == 0)
{
((Byte *)&fat12_cluster)[1]= gl_buffer[fat_buf_count++];
fat_count_sector();
((Byte *)&fat12_cluster)[0]= gl_buffer[fat_buf_count++];
fat_count_sector();
new_cluster = (fat12_cluster & 0x00FF);
new_cluster += (fat12_cluster & 0x0F00);
fat12_parity = 1;
}
else
{
((Byte *)&fat12_cluster)[1]= gl_buffer[fat_buf_count++];
fat_count_sector();
new_cluster = (fat12_cluster & 0xF000) >> 12;
new_cluster += (fat12_cluster & 0x00FF) << 4;
fat12_parity = 0;
}
i--;
}
while (i != 0);
while (new_cluster != LAST_CLUSTER12) /* loop until last cluster found */
{
if ((new_cluster == (old_cluster + 1)) && (chain[index].number != MAX_CL_PER_FRAG))
{ /* contiguous cluster up to 255 */
chain[index].number++;
//if(count>=pageSize)
}
else
{
/* add fragmentation */
index++;
chain[index].number = 1;
chain[index].cluster.w[1] = new_cluster - 2; /* 2 = 1st cluster */
for (i = new_cluster - old_cluster - 1; i != 0; i--)
{ /* dummy FAT read */
if (fat12_parity == 0)
{
((Byte *)&fat12_cluster)[1]= gl_buffer[fat_buf_count++];
fat_count_sector();
((Byte *)&fat12_cluster)[0]= gl_buffer[fat_buf_count++];
fat_count_sector();
fat12_parity = 1;
}
else
{
((Byte *)&fat12_cluster)[1]= gl_buffer[fat_buf_count++];
fat_count_sector();
fat12_parity = 0;
}
}
}
old_cluster=new_cluster;
if(index==nb_frag)
{/* end of chain reached */
/* last fragment always contains a single cluster */
chain[nb_frag].number = 0; /* end of chain marker */
fat_last_clust_index = nb_frag;
return KO; /* file too much fragmented */
}
else
{
/* read new entry */
if(fat12_parity == 0)
{
((Byte *)&fat12_cluster)[1]= gl_buffer[fat_buf_count++];
fat_count_sector();
((Byte *)&fat12_cluster)[0]= gl_buffer[fat_buf_count++];
fat_count_sector();
new_cluster = (fat12_cluster & 0x00FF);
new_cluster += (fat12_cluster & 0x0F00);
fat12_parity = 1;
}
else
{
((Byte *)&fat12_cluster)[1] = gl_buffer[fat_buf_count++];
fat_count_sector();
new_cluster = (fat12_cluster & 0xF000) >> 12;
new_cluster += (fat12_cluster & 0x00FF) << 4;
fat12_parity = 0;
}
}
}//while
fat_last_clust_index = index;
// fat_get_clusters_end(chain,index);
return OK;
}
#endif //COMPILE_FAT_12
#if COMPILE_FAT_32 == TRUE
byte fat32_get_clusters (fat_st_clust_chain xdata *chain, Byte nb_frag)
{
Byte index;
/* index in chain */
Union32 new_cluster;
Union32 old_cluster;
nb_frag = nb_frag - 1; /* set limit (index start at 0) */
/* build the first entry of the allocation list */
chain[0].number = 1;
chain[0].cluster.l = fat_cache.current.start_cluster.l - 2; /* 2 = 1st cluster */
old_cluster.l = fat_cache.current.start_cluster.l;
index = 0;
fat_buf_count = (old_cluster.l % (fat_sector_size >> 2)) << 2;
phy_read_open(fat_ptr_fats + (old_cluster.l / (fat_sector_size >> 2)));
/* read first entry */
phy_read_sector();
new_cluster.b[3] = gl_buffer[0+fat_buf_count];
new_cluster.b[2] = gl_buffer[1+fat_buf_count];
new_cluster.b[1] = gl_buffer[2+fat_buf_count];
new_cluster.b[0] = gl_buffer[3+fat_buf_count];
while(new_cluster.l != LAST_CLUSTER32) /* loop until last cluster found */
{
if ((new_cluster.l == (old_cluster.l + 1)) && (chain[index].number != MAX_CL_PER_FRAG))
{/* contiguous cluster up to 255 */
chain[index].number++;
fat_buf_count=fat_buf_count+4;
fat_count_sector();
}
else
{/* compute fragmentation */
index++;
chain[index].number = 1;
chain[index].cluster.l = new_cluster.l - 2; /* 2 = 1st cluster */
fat_buf_count=(new_cluster.l % (fat_sector_size >> 2))<<2;
phy_read_close();
phy_read_open(fat_ptr_fats + (new_cluster.l / (fat_sector_size >> 2)));
phy_read_sector();
}
old_cluster.l = new_cluster.l;
if (index == nb_frag)
{ /* end of chain reached */
/* last fragment always contains a single cluster */
chain[nb_frag].number = 0; /* end of chain marker */
fat_last_clust_index = nb_frag;
return KO; /* file too much fragmented */
}
else
{
/* read new entry */
new_cluster.b[3] = gl_buffer[0+fat_buf_count];
new_cluster.b[2] = gl_buffer[1+fat_buf_count];
new_cluster.b[1] = gl_buffer[2+fat_buf_count];
new_cluster.b[0] = gl_buffer[3+fat_buf_count];
}
}
fat_last_clust_index = index;
//fat_get_clusters_end(chain,index);
return OK;
}
#endif //COMPILE_FAT_32
byte fat_get_clusters (fat_st_clust_chain xdata *chain, Byte nb_frag)
{
if(fat_type_id == FAT_IS_16)
{
#if COMPILE_FAT_16 == TRUE
if(fat16_get_clusters (chain, nb_frag) == KO)
return KO;
#endif
}
else if(fat_type_id == FAT_IS_12)
{
#if COMPILE_FAT_12 == TRUE
if(fat12_get_clusters (chain, nb_frag) == KO)
return KO;
#endif
}
else if(fat_type_id == FAT_IS_32)
{
#if COMPILE_FAT_32 == TRUE
if(fat32_get_clusters (chain, nb_frag) == KO)
return KO;
#endif
}
phy_read_close();
/* end of file: last fragment must always contain a single cluster */
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 chain marker */
}
else
{
chain[fat_last_clust_index ].number--;
fat_last_clust_index++; // = index + 1;
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 chain marker */
}
return OK;
}
/*F**************************************************************************
* NAME: fat_set_clusters
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* - OK: allocation done
* - KO: allocation cannot be done : no free cluster
*----------------------------------------------------------------------------
* PURPOSE:
* Prepare a list of the free clusters:
* chain[n].cluster contains the starting cluster number of a fragment
* chain[n].number contains the number of contiguous clusters in fragment
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* Free cluster list is limited by the nb_frag parameter.
* If memory is too much fragmented, created file may be limited in size.
* Last list item always has single cluster
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
#if COMPILE_FAT_16 == TRUE
byte fat16_set_clusters (fat_st_clust_chain xdata *chain, Byte nb_frag)
{
Byte cluster_free;//,flag;
Byte index;
// Uint16 count;
// word sector_count; // 2004.2.25 modify
// Uint16 cluster,cluster2start;
Uint16 max_cluster;
Union16 tmp_cluster;
Uint16 cluster,cluster2start;
// Uint16 tmp_cluster;
Uint16 nb_cluster;
Uint16 max,min;
max_cluster = fat_count_of_clusters;
//max_cluster = fat_max_cluster_number;//2004.3.13 modify
index = 0;
nb_cluster = 0;
cluster_free = FALSE;
phy_read_open(fat_ptr_fats);
phy_read_sector();
cluster = 0;
cluster2start = 0;//2;
max = fat_sector_size>>1;
min = 0;
fat_buf_count = fat_sector_size>>2;
do
{
while((max - min) > 1)
{
tmp_cluster.b[1] = gl_buffer[(fat_buf_count<<1)];
tmp_cluster.b[0] = gl_buffer[(fat_buf_count<<1)+1];
if(tmp_cluster.w == 0x0000)
{
max = fat_buf_count;
fat_buf_count = ((fat_buf_count - min) >> 1) + min ;
}
else
{
min = fat_buf_count;
fat_buf_count = ((max - fat_buf_count) >> 1) + fat_buf_count;
}
}
if(max == 1)
{
tmp_cluster.b[1] = gl_buffer[0];
tmp_cluster.b[0] = gl_buffer[1];
if(tmp_cluster.w == 0x0000)
{
max = max - 1;
}
}
if(max == (fat_sector_size>>1))
{
phy_read_sector();
max = fat_sector_size>>1;
min = 0;
fat_buf_count = fat_sector_size>>2;
cluster += max;
//cluster2start += n;
}
else
{
fat_buf_count = max*2;
cluster += (fat_buf_count >> 1);
//cluster2start += count / 2;
cluster_free = TRUE;
break;
}
}while(cluster < max_cluster);
cluster2start = cluster;
cluster -= 2;
if (!cluster_free)
{
return KO; /* no free cluster found */
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -