📄 fat.c
字号:
*****************************************************************************/
void fat_clear_fat (void)
{
Uint16 sector_number;
Uint16 i;
Uint16 cluster;
Uint16 temp;
bit parity_bit;
if (fat_is_fat16)
{
/* init the starting cluster value */
cluster = fclusters[0].cluster + 2;
/* Start at first chain cluster */
sector_number = cluster / 256;
/* Bufferize fat sector */
fat_load_sector(fat_ptr_fats + sector_number);
do
{
if ((cluster / 256) != sector_number)
{
fat_update_fat_sector(sector_number);
sector_number = (Uint16)(cluster / 256);
fat_load_sector(fat_ptr_fats + sector_number);
}
i = (cluster * 2) & 0x1FF;
((Byte *)&cluster)[1] = fat_buf_sector[i];
fat_buf_sector[i++] = 0;
((Byte *)&cluster)[0] = fat_buf_sector[i];
fat_buf_sector[i] = 0;
}
while (cluster != 0xFFFF);
fat_update_fat_sector(sector_number);
}
else
{
/* init the starting cluster value */
cluster = fclusters[0].cluster + 2;
/* Start at first chain cluster */
sector_number = cluster * 3 / 1024;
/* Bufferize fat sector */
fat_load_sector(fat_ptr_fats + sector_number);
temp = sector_number;
do
{
temp = cluster * 3 / 1024;
if (temp != sector_number)
{
fat_update_fat_sector(sector_number);
sector_number = temp;
fat_load_sector(fat_ptr_fats + sector_number);
}
parity_bit = cluster & 0x01;
i = (cluster * 3 / 2) & 0x1FF;
if (parity_bit == 0)
{
((Byte *)&cluster)[1] = fat_buf_sector[i];
fat_buf_sector[i] = 0x00;
i++;
if (i == 512)
{
fat_update_fat_sector(sector_number);
sector_number++;
fat_load_sector(fat_ptr_fats + sector_number);
i = 0;
}
((Byte *)&cluster)[0] = fat_buf_sector[i] & 0x0F;
fat_buf_sector[i] &= 0xF0;
}
else
{
cluster = (fat_buf_sector[i] & 0xF0) >> 4;
fat_buf_sector[i] &= 0x0F;
i++;
if (i == 512)
{
fat_update_fat_sector(sector_number);
sector_number++;
fat_load_sector(fat_ptr_fats + sector_number);
i = 0;
}
cluster += (fat_buf_sector[i] << 4);
fat_buf_sector[i] = 0x00;
}
}
while (cluster != 0xFFF);
fat_update_fat_sector(sector_number);
}
}
/*F**************************************************************************
* NAME: fat_refresh_dir_file_info
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*
*----------------------------------------------------------------------------
* PURPOSE:
* Reconstruct the file directory list and seek to the file pointed by
* fat_dir_list_index
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*
*----------------------------------------------------------------------------
* REQUIREMENTS:
*
*****************************************************************************/
void fat_refresh_dir_file_info (Byte id)
{
if (dir_is_root) /* update fat_dir_current_sect with directory starting value */
{
fat_dir_current_sect = fat_ptr_rdir;
}
else
{
fat_dir_current_sect = (((Uint32)(dclusters[0].cluster)) * fat_cluster_size)
+ fat_ptr_data;
}
fat_get_dir_file_list(id); /* Refresh file list */
fat_seek_entry_record(); /* Re-fetch the entry <-> fat_dir_list_index */
fat_get_dir_entry(&fat_cache.current); /* update current file info */
}
/*F**************************************************************************
* NAME: fat_fdelete
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
* Delete status: DEL_RET_OK: delete done & dir is not empty
* DEL_RET_NO_MORE_FILE: delete done & dir is empty
* DEL_RET_ERROR_DIR: dir can not be deleted
*----------------------------------------------------------------------------
* PURPOSE:
* Delete a selected file, in the root directory or in a sub-dir
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*
*----------------------------------------------------------------------------
* REQUIREMENTS:
* File variables must be updated (fat_dclust_byte_count, entry record, ...)
*****************************************************************************/
Byte fat_fdelete (void)
{
Uint16 i;
xdata Uint32 dir_sector;
if (fat_check_ext() != FILE_DIR)
{
fat_seek_entry_record(); /* Re-fetch the entry <-> fat_dir_list_index */
Hard_read_close();
dir_sector = fat_dir_current_sect;
fat_root_entry = fat_dir_current_offs / 32; /* fat_dir_current_offs give the offset in byte starting */
fat_file_size.l = fat_cache.current.size.l; /* at the beginning of directory */
fat_load_sector(dir_sector); /* Load directory sector */
i = (fat_root_entry % 16) * 32 ; /* position of entry in the sector */
while (fat_buf_sector[i+11] == ATTR_LFN_ENTRY)
{
/* mark file as deleted */
fat_buf_sector[i] = FILE_DELETED;
i+=32;
if (!dir_is_root)
fat_dclust_byte_count += 32;
if (i == SECTOR_SIZE)
{
Hard_write_open(dir_sector);
for (i = 0; i < SECTOR_SIZE; i++)
Hard_write_byte(fat_buf_sector[i]);
Hard_write_close();
if (!dir_is_root) /* sub-directory */
{
/* check if we are at the end of a cluster */
if ((((Byte*)&fat_dclust_byte_count)[1] == 0x00) &&
((((Byte*)&fat_dclust_byte_count)[0] & fat_cluster_mask) == 0x00))
{
/* extract if necessary the next cluster from the allocation list */
if (dclusters[fat_dchain_index].number == fat_dchain_nb_clust)
{ /* new fragment */
fat_dchain_index++;
fat_dchain_nb_clust = 1;
dir_sector = (fat_ptr_data + ((Uint32)(dclusters[fat_dchain_index].cluster) * fat_cluster_size));
}
else
{
fat_dchain_nb_clust++; /* one more cluster read */
dir_sector++; /* Contiguous cluster */
}
}
else
{ /* Don't change the cluster */
dir_sector++;
}
}
else
{ /* Root directory is linear */
dir_sector++;
}
fat_load_sector(dir_sector);
i = 0;
}
}
fat_buf_sector[i] = FILE_DELETED;
Hard_write_open(dir_sector);
for (i = 0; i < SECTOR_SIZE; i++)
Hard_write_byte(fat_buf_sector[i]);
Hard_write_close();
/* FAT update */
fat_fclust_byte_count = 0; /* byte 0 of cluster */
/* reset the allocation list variable */
fat_fchain_index = 0;
fat_fchain_nb_clust = 0; /* start on first contiguous cl */
/* get file allocation list */
fat_get_clusters(&fclusters, MAX_FILE_FRAGMENT_NUMBER);
/* Clear fat cluster */
fat_clear_fat();
/* NF correction */
for (i = 0; i < 256; i++)
gl_buffer[i] = 0x00;
if (((dir_is_root == TRUE) && (fat_dir_list_last == 1)) ||
((dir_is_root == FALSE) && (fat_dir_list_last == 3))) /* . and .. directory */
return DEL_RET_NO_MORE_FILE;
if (fat_dir_list_index == (fat_dir_list_last - 1) )
{
fat_dir_list_index--;
}
return DEL_RET_OK;
}
else
{
return DEL_RET_ERROR_DIR;
}
}
/*F**************************************************************************
* NAME: fat_read_cluster12
*----------------------------------------------------------------------------
* PARAMS:
* - init : initialize the parity bit or not
* return:
* - FAT12 cluster value
*----------------------------------------------------------------------------
* PURPOSE:
* - Read in fat12 file system a cluster value
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
Uint16 fat_read_cluster12 (bit init)
{
static bit fat12_parity;
static idata Uint16 cluster;
if (init)
{
fat12_parity = 0;
cluster = 0;
}
if (fat12_parity == 0)
{
((Byte*)&cluster)[1] = Hard_read_byte();
((Byte*)&cluster)[0] = Hard_read_byte();
fat12_parity = 1;
return (cluster & 0x0FFF);
}
else
{
cluster = (cluster & 0xF000) >> 12;
cluster += (Hard_read_byte() << 4);
fat12_parity = 0;
return (cluster);
}
}
/*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:
*****************************************************************************/
bit fat_set_clusters (void)
{
bit cluster_free;
Uint16 cluster;
Union16 tmp_cluster;
cluster = 0;
cluster_free = FALSE;
fat_last_clust_index = 0;
Hard_read_open(fat_ptr_fats);
if (fat_is_fat16) /* FAT16 management */
{
/* search the first free cluster in fat */
Hard_read_byte(); /* skip first 2 clusters */
Hard_read_byte();
Hard_read_byte();
Hard_read_byte();
do /* search for first free cluster */
{
tmp_cluster.b[1] = Hard_read_byte();
tmp_cluster.b[0] = Hard_read_byte();
if (tmp_cluster.w == 0x0000)
cluster_free = TRUE;
else
cluster++;
}
while ((cluster != fat_count_of_clusters) && (!cluster_free));
if (!cluster_free)
{
Hard_read_close();
return KO; /* no free cluster found */
}
fclusters[fat_last_clust_index].number = 1;
fclusters[fat_last_clust_index].cluster = cluster; /* store first cluster */
cluster++;
if (cluster != fat_count_of_clusters)
{
do /* construct the list */
{
cluster_free = FALSE;
tmp_cluster.b[1] = Hard_read_byte();
tmp_cluster.b[0] = Hard_read_byte();
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -