⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fat_fat.c

📁 fat16文件系统源码。需要的请下
💻 C
📖 第 1 页 / 共 2 页
字号:
//****************************************************************
// FileName:    fat_fat.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"   

#if COMPILE_FAT_WRITE == TRUE

/*F**************************************************************************
* NAME: fat_update_fats
*----------------------------------------------------------------------------
* PARAMS:
*    f_delete: flag of whether the file is deleted
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
*   Update entry and FAT after a writing file session
*----------------------------------------------------------------------------
* NOTE:
*   FAT16 update : random update (direct access)
*   FAT12 update : sequential update
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_update_fats(Byte f_delete,fat_st_clust_chain xdata *chain)
{
	Uint16 nb_cluster;
	  
    nb_cluster = (Uint16)((fat_file_size.l / fat_sector_size) / fat_cluster_size) + 1;
    if(fat_file_size.l % ((Uint32)(fat_sector_size)*fat_cluster_size) == 0)
         nb_cluster -= 1;


    if(nb_cluster == 0)
        return;

  	fat_phy_fats(chain,nb_cluster,f_delete);

	if(f_delete == TRUE)
	{
	  
	   	if(fat_dir_list_last == 1)
	  	{
	  	  	fat_dir_list_last = 0;	
		  	lfn_name[0] = '\n';
	  	}  
	  	else
	  	{
		  	if(fat_dir_list_index == (fat_dir_list_last - 1))
		  	{
	          	fat_dir_list_index--;
	          	fat_dir_list_last--;
		  	}
	      	fat_scan_dir(FALSE);
	      	fat_dir_seek(((Uint32)fat_dir_all_count) * DIR_SIZE);
		  	fat_get_dir_entry(&fat_cache.current);          // update current file info 
	  	}
	}
}


#if COMPILE_FAT_DIR_CREATE == TRUE
/*F**************************************************************************
* NAME: fat_dir_update_fats
*----------------------------------------------------------------------------
* PARAMS:
*    d_delete: flag of whether the dir is deleted
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
*   Update entry and FAT after a writing file session
*----------------------------------------------------------------------------
* NOTE:
*   FAT16 update : random update (direct access)
*   FAT12 update : sequential update
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_dir_update_fats(Byte d_delete,fat_st_clust_chain xdata *chain)
{
	Byte index;	
	Uint16 i;
	Uint16 nb_cluster;
  
   	nb_cluster=0;
   	index=0;
 
    for(i=0;i<MAX_DIR_FRAGMENT_NUMBER;i++)
	{
		if(dclusters[i].number == 0)
		{
		 	nb_cluster++;
			break;
		}
        else
		{
		 	nb_cluster = nb_cluster + dclusters[i].number;
		}
	}

    fat_phy_fats(chain,nb_cluster,d_delete);
	
}
#endif //COMPILE_FAT_DIR_CREATE


#if COMPILE_FAT_WRITE == TRUE
/*F**************************************************************************
* NAME: fat_phy_fats
*----------------------------------------------------------------------------
* PARAMS:
*    d_delete: flag of whether the dir is deleted
* return: none
*----------------------------------------------------------------------------
* PURPOSE:
*   Update entry and FAT after a writing file session
*----------------------------------------------------------------------------
* NOTE:
*   FAT16 update : random update (direct access)
*   FAT12 update : sequential update
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
void fat_phy_fats(fat_st_clust_chain xdata *chain, Uint16 nb_cluster,byte del_flag)
{
	if(fat_type_id == FAT_IS_16)
	{
#if COMPILE_FAT_16 == TRUE
   		fat16_phy_fats(chain,nb_cluster,del_flag);
#endif
	}
	else if(fat_type_id == FAT_IS_12)
	{
#if COMPILE_FAT_12 == TRUE
		fat12_phy_fats(chain,nb_cluster,del_flag);
#endif
	}
	else if(fat_type_id == FAT_IS_32)
	{
#if COMPILE_FAT_32 == TRUE
		fat32_phy_fats(chain,nb_cluster,del_flag);
#endif
	}
////////////////////////////////////////////////////////////////////
	fat_write_sector(fat_ptr_fats+fat_sector_count.w);

	if(fat_2_is_present==TRUE)
	{
		fat_read_sector(fat_ptr_fats+fat_sector_count.w);

	    fat_write_sector(fat_ptr_fats + fat_fat_size + fat_sector_count.w);
    }
}
#endif //#if COMPILE_FAT_WRITE == TRUE

#if COMPILE_FAT_16 == TRUE

void fat16_phy_fats(fat_st_clust_chain xdata *chain, Uint16 nb_cluster,byte del_flag)
{
  	Byte index;
  	Byte chain_index;
//  	Byte sector_number;
//
//  	Uint16 i;
  	Uint16 cluster;


  	Uint16 j;
  	byte  flag;

////////////////////////////////////
  	index=0;
  	j=0;

	/* Start at first chain cluster */
	fat_sector_count.b[1] = (chain[0].cluster.w[1] + 2) / (fat_sector_size >> 1);
	fat_sector_count.b[0] = 0;	
	/* Bufferize fat sector */
	fat_read_sector(fat_ptr_fats + fat_sector_count.b[1]);	
	/* i -> word fat sector position */
	fat_buf_count = ((chain[index].cluster.w[1] + 2) << 1) & (fat_sector_size - 1);  //0x1FF;
	/* init the starting cluster value */
	cluster = chain[index].cluster.w[1];
	chain_index = 1;
	flag = 0;
  
   	do
   	{
      	/* Determinate the value of the next cluster */
      	if (chain[index].number == chain_index)
      	{
		    /* increase index */
		    index++;
		    if (del_flag == TRUE)
		    {
		        gl_buffer[fat_buf_count++] = 0x00;
		        gl_buffer[fat_buf_count++] = 0x00;
		    }
		    else
		    {
		        gl_buffer[fat_buf_count++] = chain[index].cluster.w[1] + 2;
		        gl_buffer[fat_buf_count++] = (chain[index].cluster.w[1] + 2) >> 8;
		    }
		    chain_index = 1;
	
	        if (((chain[index].cluster.w[1] + 2) / (fat_sector_size >> 1)) != fat_sector_count.b[1])
		    { /* Fat change sector */
			    /* FAT 1 update */
				fat_write_sector(fat_ptr_fats + fat_sector_count.b[1]);
			           
			    if (fat_2_is_present == TRUE)
			    {
			        /* FAT 2 update */
					fat_read_sector(fat_ptr_fats + fat_sector_count.b[1]);
					fat_write_sector(fat_ptr_fats + fat_sector_count.b[1] + fat_fat_size);
				}   

			    fat_sector_count.b[1] = (chain[index].cluster.w[1] + 2) / (fat_sector_size >> 1);
				     
	            fat_read_sector(fat_ptr_fats + fat_sector_count.b[1]);
	
			}
			fat_buf_count = ((chain[index].cluster.w[1] + 2) << 1) & (fat_sector_size - 1);//0x1FF;
			cluster = chain[index].cluster.w[1];
			flag = 1;
      	}			
      	else
      	{
		    cluster++;
		    if (del_flag == TRUE)
		    {
		        gl_buffer[fat_buf_count++] = 0x00;
		        gl_buffer[fat_buf_count++] = 0x00;
		    }
		    else
		    {
		        gl_buffer[fat_buf_count++] = cluster + 2 ;
		        gl_buffer[fat_buf_count++] = (cluster + 2) >> 8;
		    }
		    chain_index++;
	        fat_wr_fat();
			flag = 0;
        }
      	j++;

    }
    while (j < nb_cluster);
	/* End of file indicate by 0xFFFF */
	if(flag == 0)
	{
		if(fat_buf_count == 0)
		{
			fat_sector_count.b[1]--;
			fat_read_sector(fat_ptr_fats + fat_sector_count.b[1]);	
			fat_buf_count = fat_sector_size - 2;
		}
		else
		{
		    fat_buf_count = fat_buf_count - 2;
		}
	}

	if (del_flag == TRUE)
	{
		gl_buffer[fat_buf_count++] = 0x00;
		gl_buffer[fat_buf_count] = 0x00;
    }
	else
	{
	    gl_buffer[fat_buf_count++] = 0xFF;
	    gl_buffer[fat_buf_count] = 0xFF;
	}
	//fat_phy_fat_end(sector_number);
}

#endif //COMPILE_FAT_16


#if COMPILE_FAT_12 == TRUE
void fat12_phy_fats(fat_st_clust_chain xdata *chain, Uint16 nb_cluster,byte del_flag)
{
  	Byte index;

  	Uint16 k;

  	Uint16 cluster;
  	Uint16 temp;
	byte fat12_parity;

  	Uint16 j;

///////////////////////////////////
  	index=0;
  	j=0;

	cluster = chain[0].cluster.w[1] + 2;
	fat_buf_count =(cluster + (cluster >> 1)) & (fat_sector_size - 1);    //% fat_sector_size;
	fat_sector_count.b[1] = (cluster + (cluster >> 1))/fat_sector_size;
	fat_sector_count.b[0] = 0;
	
	if(cluster%2 == 0)
	{
		fat12_parity = 0;
	}
	else
	{
		fat12_parity = 1;
	    fat_buf_count++;
	}
	
	index = 0;
	k =0;
	
	fat_read_sector(fat_ptr_fats + fat_sector_count.b[1]);

	do    /* do while (k < nb_cluster) */
	{
		if (cluster == (chain[index].cluster.w[1] + 2))  /* start of a fragment */
	    {
	        if (fat12_parity == 1)
	        {
				if (fat_buf_count == 0)
				{
					/* go back to previous fat sector */
					fat_sector_count.b[1]--;
					fat_read_sector(fat_ptr_fats + fat_sector_count.b[1]);
	
					/* Last sector byte */
					fat_buf_count = fat_sector_size - 1;
				}
	            else
				{
					fat_buf_count--;
				}
	       }
	       if (chain[index].number != 0)   /* end of chain marker  */
	       {
				for (j = 0; (j < chain[index].number - 1) && (k < nb_cluster - 1); j++)
	            { /* Compute chain cluster */
				    cluster++;
					if (fat12_parity == 0)
					{
						if (del_flag == TRUE)
						{
							gl_buffer[fat_buf_count++] = 0x00;
						}
						else
						{
							gl_buffer[fat_buf_count++] =(Byte)(cluster & 0x00FF);
						}
						fat_wr_fat();
	
						if (del_flag == TRUE)
						{
							gl_buffer[fat_buf_count] &= 0xF0;
						}
						else
						{
							gl_buffer[fat_buf_count] &= 0xF0;
							gl_buffer[fat_buf_count] +=(Byte)((cluster & 0x0F00) >> 8);
						}
						fat12_parity = 1;
	                    

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -