?? fat.c
字號(hào):
{
/* 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);
/* i -> word fat sector position */
i = (cluster * 2) & 0x1FF;
chain_index = 1;
while (nb_cluster != 0)
{
/* Determinate the value of the next cluster */
if (fclusters[index].number == chain_index)
{
/* increase index */
index++;
cluster = fclusters[index].cluster + 2;
fat_buf_sector[i++] = ((Byte*)&cluster)[1];
fat_buf_sector[i] = ((Byte*)&cluster)[0];
chain_index = 1;
if ( (cluster / 256) != sector_number)
{ /* Fat change sector */
fat_update_fat_sector(sector_number);
sector_number = (Uint16)(cluster / 256);
fat_load_sector(fat_ptr_fats + sector_number);
}
i = (cluster * 2) & 0x1FF;
}
else
{
cluster++;
fat_buf_sector[i++] = ((Byte*)&cluster)[1];
fat_buf_sector[i++] = ((Byte*)&cluster)[0];
chain_index++;
if (((Byte*)&i)[0] == 0x02)
{
fat_update_fat_sector(sector_number);
sector_number++;
fat_load_sector(fat_ptr_fats + sector_number);
((Byte*)&i)[0] = 0x00;
}
}
nb_cluster--;
}
/* End of file indicate by 0xFFFF */
fat_buf_sector[i++] = 0xFF;
fat_buf_sector[i] = 0xFF;
fat_update_fat_sector(sector_number);
}
/********************/
/* FAT12 management */
/********************/
else
{
cluster = fclusters[index].cluster + 2;
sector_number = cluster * 3 / 1024;
/* Bufferize fat sector */
fat_load_sector(fat_ptr_fats + sector_number);
i = cluster;
chain_index = 1;
while (nb_cluster != 0)
{
/* Determinate the value of the next cluster */
if (fclusters[index].number == chain_index)
{
/* increase index */
index++;
fat_update_buf_fat(cluster, fclusters[index].cluster + 2, 0);
cluster = fclusters[index].cluster + 2;
chain_index = 1;
i = cluster * 3 / 1024;
if ( i != sector_number)
{ /* Fat change sector */
fat_update_fat_sector(sector_number);
sector_number = i;
fat_load_sector(fat_ptr_fats + sector_number);
}
}
else
{
cluster++;
fat_update_buf_fat(cluster - 1, cluster, 0);
chain_index++;
}
nb_cluster--;
}
fat_update_buf_fat(cluster, cluster, 1);
}
/* Reconstruct list file */
i = fat_dir_list_index;
fat_dir_current_sect = fat_ptr_rdir;
fat_dclust_byte_count = 0;
dir_is_root = TRUE;
fat_get_dir_file_list(fat_check_ext()); /* create list of entries */
fat_dir_list_index = i;
for (i = 0; i <= fat_dir_list_index; i++)
fat_dseek(fat_dir_entry_list[i] * DIR_SIZE);
fat_get_dir_entry(&fat_cache.current); /* update current file info */
}
/*F**************************************************************************
* NAME: fat_fopen
*----------------------------------------------------------------------------
* PARAMS:
* mode: READ: open file for read
* WRITE: open file for write
*
* return:
* - OK: file opened
* - KO: file not opened: - file is empty
* - low level read error
*----------------------------------------------------------------------------
* PURPOSE:
* Open the file in read or write mode
*----------------------------------------------------------------------------
* EXAMPLE:
* if (fat_get_root_directory(FILE_WAV) == OK) // Select first WAV file in root
* {
* fat_fopen(WRITE); // Open this file in WRITE mode
* for (j = 0; j < 10; j++)
* fat_fputc(buff[j]);
* fat_fclose();
* }
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
* For write mode, there must be an entry in the root and entry data must be
* updated.
*****************************************************************************/
bit fat_fopen (bit mode)
{
if (mode == READ)
{
if (fat_cache.current.size.l == 0)
{
return KO; /* file empty */
}
else
{
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);
/* seek to the beginning of the file */
fat_open_mode = READ;
return Hard_read_open(fat_ptr_data + ((Uint32)(fclusters[0].cluster)
* fat_cluster_size));
}
}
else
{
fat_fclust_byte_count = 0; /* byte 0 of cluster */
fat_file_size.l = 0;
flag_end_disk_file = FALSE;
/* reset the allocation list variable */
fat_fchain_index = 0;
fat_fchain_nb_clust = 0; /* start on first contiguous cl */
fat_root_entry = fat_dclust_byte_count / 32;
/* get file allocation list */
fat_get_clusters(&fclusters, MAX_FILE_FRAGMENT_NUMBER);
fat_open_mode = WRITE;
return Hard_write_open(fat_ptr_data + ((Uint32)(fclusters[0].cluster)
* fat_cluster_size));
}
}
/*F**************************************************************************
* NAME: fat_fclose
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Close opened file
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_fclose (void)
{
if (fat_open_mode == READ)
{
Hard_read_close(); /* close reading */
}
else
{
Hard_write_close(); /* close writing */
fat_update_entry_fat(); /* Update entry and fat */
}
}
/*F**************************************************************************
* NAME: fat_fcreate
*----------------------------------------------------------------------------
* PARAMS:
* file_name : file name of the file to be created
* attribute : file attribute (see fat.h)
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Create a new file in the root directory.
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
* This function update the root directory entry
*----------------------------------------------------------------------------
* REQUIREMENTS:
*
*****************************************************************************/
bit fat_fcreate (char *file_name, Byte attribute)
{
Byte temp_byte;
Uint16 j;
Uint16 index;
/* Check file type */
ext[0] = file_name[8];
ext[1] = file_name[9];
ext[2] = file_name[10];
fat_cache.current.attributes = attribute;
if ((fat_check_ext() == FILE_DIR) || (attribute == ATTR_DIRECTORY))
return KO;
/* get free clusters list */
if ( fat_set_clusters() == KO )
return KO;
/* no more place in file liste */
if (fat_dir_list_last == (MAX_DIRECTORY_FILE - 1))
return KO;
/* Find the first free entry in root */
index = 0;
if (Hard_read_open(fat_ptr_rdir) == KO)
return KO;
temp_byte = Hard_read_byte();
while ((temp_byte != FILE_NOT_EXIST) && (temp_byte != FILE_DELETED))
{
for (temp_byte = DIR_SIZE - 1; temp_byte != 0; temp_byte--)
Hard_read_byte();
temp_byte = Hard_read_byte();
index++;
}
Hard_read_close();
if ((dir_is_root == TRUE) && (index >= NB_ROOT_ENTRY)) /* Maximum entries in root directory */
return KO;
/* Construct the entry */
for (temp_byte = 12; temp_byte < 32; temp_byte++)
gl_buffer[temp_byte] = 0x00;
gl_buffer[0] = file_name[0];
gl_buffer[1] = file_name[1];
gl_buffer[2] = file_name[2];
gl_buffer[3] = file_name[3];
gl_buffer[4] = file_name[4];
gl_buffer[5] = file_name[5];
gl_buffer[6] = file_name[6];
gl_buffer[7] = file_name[7];
gl_buffer[8] = file_name[8];
gl_buffer[9] = file_name[9];
gl_buffer[10] = file_name[10];;
gl_buffer[11] = attribute; /* Attribute : archive */
gl_buffer[26] = fclusters[0].cluster + 2; /* Low word first cluster number */
gl_buffer[27] = (fclusters[0].cluster + 2) >> 8;
fat_load_sector(fat_ptr_rdir + (index / 16));
j = (index % 16) * DIR_SIZE ; /* Position of entry in the sector */
for (temp_byte = 0; temp_byte < DIR_SIZE; temp_byte++)
fat_buf_sector[j++] = gl_buffer[temp_byte];
if (Hard_write_open(fat_ptr_rdir + (index / 16)) == KO)
return KO;
for (j = 0; j < SECTOR_SIZE; j++)
Hard_write_byte(fat_buf_sector[j]);
Hard_write_close();
fat_root_entry = index;
/* Reconstruct file list */
fat_dir_current_sect = fat_ptr_rdir;
fat_dclust_byte_count = 0;
dir_is_root = TRUE;
fat_get_dir_file_list(fat_check_ext()); /* create list of entries */
fat_dir_list_index = 0;
j = fat_dir_entry_list[0];
while (j < index)
{
if ( fat_dseek(fat_dir_entry_list[fat_dir_list_index] * DIR_SIZE) == KO)
return KO;
fat_dir_list_index++; /* point on next root entry */
j += fat_dir_entry_list[fat_dir_list_index];
}
if (fat_dseek(fat_dir_entry_list[fat_dir_list_index] * DIR_SIZE) == KO)
return KO;
fat_get_dir_entry(&fat_cache.current); /* update current file info */
/* parent dir is also root */
fat_cache.parent.start_cluster = 0;
fat_cache.parent.attributes = ATTR_ROOT_DIR; /* mark as root dir */
/* open file in write mode */
fat_fclust_byte_count = 0; /* byte 0 of cluster */
fat_file_size.l = 0;
fat_cache.current.size.l = 0;
flag_end_disk_file = FALSE;
fat_fchain_index = 0; /* reset the allocation list variable */
fat_fchain_nb_clust = 0; /* start on first contiguous cl */
fat_open_mode = WRITE;
return Hard_write_open(fat_ptr_data + ((Uint32)(fclusters[0].cluster)
* fat_cluster_size));
}
/*F**************************************************************************
* NAME: fat_clear_fat
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Reset FAT clusters value
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*
*----------------------------------------------------------------------------
* REQUIREMENTS:
* fclusters[] variable must be updated.
*****************************************************************************/
void fat_clear_fat (void)
{
Uint16 sector_number;
Uint16 i;
Uint16 cluster;
Uint16 temp;
bit end;
/* init the starting cluster value */
cluster = fclusters[0].cluster + 2;
/* Start at first chain cluster */
sector_number = (fat_is_fat16 == TRUE) ? cluster / 256 : cluster * 3 / 1024;
/* Bufferize fat sector */
fat_load_sector(fat_ptr_fats + sector_number);
end = FALSE;
do
{
temp = (fat_is_fat16 == TRUE) ? cluster / 256 : cluster * 3 / 1024;
if (temp != sector_number)
{
fat_update_fat_sector(sector_number);
sector_number = temp;
fat_load_sector(fat_ptr_fats + sector_number);
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -