📄 fat.c
字号:
/* FAT12 management */
/********************/
else
{
j = 1;
cluster = fclusters[index].cluster + 2;
sector_number = cluster * 3 / 1024;
/* Bufferize fat sector */
fat_load_sector(fat_ptr_fats + sector_number);
temp = cluster;
fat12_parity = cluster & 0x01;
i = (cluster * 3 / 2) & 0x1FF;
chain_index = 1;
while (j < nb_cluster)
{
/* Determinate the value of the next cluster */
if (fclusters[index].number == chain_index)
{
/* increase index */
index++;
cluster = fclusters[index].cluster + 2;
if (fat12_parity == 0)
{
fat_buf_sector[i++] = cluster & 0x00FF;
Fat_check_update(sector_number);
fat_buf_sector[i] &= 0xF0;
fat_buf_sector[i] |= ((cluster & 0x0F00) >> 8);
fat12_parity = 1;
}
else
{
fat_buf_sector[i] &= 0x0F;
fat_buf_sector[i] |= ((cluster & 0x000F) << 4);
i++;
Fat_check_update(sector_number);
fat_buf_sector[i++] = (cluster & 0x0FF0) >> 4;
Fat_check_update(sector_number);
fat12_parity = 0;
}
chain_index = 1;
temp = cluster * 3 / 1024;
if ( temp != sector_number)
{ /* Fat change sector */
fat_update_fat_sector(sector_number);
sector_number = temp;;
fat_load_sector(fat_ptr_fats + sector_number);
}
fat12_parity = cluster & 0x01;
i = (cluster * 3 / 2) & 0x1FF;
}
else
{
cluster++;
if (fat12_parity == 0)
{
fat_buf_sector[i++] = cluster & 0x00FF;
Fat_check_update(sector_number);
fat_buf_sector[i] &= 0xF0;
fat_buf_sector[i] |= ((cluster & 0x0F00) >> 8);
fat12_parity = 1;
}
else
{
fat_buf_sector[i] &= 0x0F;
fat_buf_sector[i] |= ((cluster & 0x000F) << 4);
i++;
Fat_check_update(sector_number);
fat_buf_sector[i++] = (cluster & 0x0FF0) >> 4;
Fat_check_update(sector_number);
fat12_parity = 0;
}
chain_index++;
}
j++;
}
/* End of file marked with 0xFFF */
if (fat12_parity == 0)
{
fat_buf_sector[i++] = 0xFF;
Fat_check_update(sector_number);
fat_buf_sector[i] |= 0x0F;
}
else
{
fat_buf_sector[i++] |= 0xF0;
Fat_check_update(sector_number);
fat_buf_sector[i] = 0xFF;
}
fat_update_fat_sector(sector_number);
}
/* Reconstruct list file */
temp = 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(id); /* create list of entries */
fat_dir_list_index = temp;
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 i;
Uint16 j;
xdata Uint16 temp;
xdata Uint16 index;
xdata Uint32 root_sector;
/* Check file type */
ext[0] = file_name[8];
ext[1] = file_name[9];
ext[2] = file_name[10];
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 (i = DIR_SIZE - 1; i != 0; i--)
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 */
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[12] = 0x00; /* Millisecond stamp at time creation */
gl_buffer[13] = 0x00; /* Time */
gl_buffer[14] = 0x00;
gl_buffer[15] = 0x00; /* Date */
gl_buffer[16] = 0x00;
gl_buffer[18] = 0x00; /* Last access date */
gl_buffer[19] = 0x00;
gl_buffer[20] = 0x00; /* High word First cluster number : 0x0000 for FAT12/16 */
gl_buffer[21] = 0x00;
gl_buffer[22] = 0x00; /* Time of last write */
gl_buffer[23] = 0x00;
gl_buffer[24] = 0x00; /* Date of last write */
gl_buffer[25] = 0x00;
gl_buffer[26] = fclusters[0].cluster + 2; /* Low word first cluster number */
gl_buffer[27] = (fclusters[0].cluster + 2) >> 8;
gl_buffer[28] = 0x00; /* Size */
gl_buffer[29] = 0x00;
gl_buffer[30] = 0x00;
gl_buffer[31] = 0x00;
root_sector = fat_ptr_rdir + (index / 16);/* root sector address */
fat_load_sector(root_sector);
j = (index % 16) * DIR_SIZE ; /* Position of entry in the sector */
for (i = 0; i < DIR_SIZE; i++)
fat_buf_sector[j++] = gl_buffer[i];
if (Hard_write_open(root_sector) == KO)
return KO;
for (i = 0; i < SECTOR_SIZE; i++)
Hard_write_byte(fat_buf_sector[i]);
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;
temp = fat_dir_entry_list[0];
while (temp < 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 */
temp += 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.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -