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

📄 fat_fat.c

📁 fat16文件系统源码。需要的请下
💻 C
📖 第 1 页 / 共 2 页
字号:
						k++;
				   	}
				   	else
				   	{
					   	if (del_flag == TRUE)
					   	{
							gl_buffer[fat_buf_count] &= 0x0F;
					   	}
					   	else
					   	{
							gl_buffer[fat_buf_count] &= 0x0F;
							gl_buffer[fat_buf_count] +=(Byte) ((cluster & 0x000F) << 4);
					   	}
					   	fat_buf_count++;
						fat_wr_fat();

					   	if(del_flag==TRUE)
					   	{
						   	gl_buffer[fat_buf_count++]=0x00;
					   	}
					   	else
					   	{
						   	gl_buffer[fat_buf_count++]=(Byte)((cluster & 0x0ff0)>>4);
					   	}
						fat_wr_fat();
					   	fat12_parity=0;
					   	k++;
				   	}
				}// end for
        		//if(k == 0)
           			//k++;
	 		} 
     		else
			{
				k++;
			}

		 	if(k < nb_cluster-1) 
		 	{
			 	cluster++;
			  	index++;
			 	temp=chain[index].cluster.w[1] + 2;
			 	if(fat12_parity == 0)
			 	{
				 	if(del_flag==TRUE)
				 	{
					 	gl_buffer[fat_buf_count++] = 0x00;
				 	}
				 	else
				 	{
					 	gl_buffer[fat_buf_count++]=(Byte)(temp & 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)((temp & 0x0f00)>>8);
				 	}
				 	fat_buf_count++;
				 	fat12_parity=1;
				 	k++;
			 	}
			 	else
			 	{
				 	if(del_flag==TRUE)
				 	{
					 	gl_buffer[fat_buf_count] &=0x0f;
				 	}
				 	else
				 	{
					 	gl_buffer[fat_buf_count] &= 0x0f;
					 	gl_buffer[fat_buf_count] += (Byte)((temp & 0x000f)<<4);
				 	}
				 	fat_buf_count++;
					fat_wr_fat();
				 	if(del_flag==TRUE)
				 	{
					 	gl_buffer[fat_buf_count++]=0x00;//modify 2004.3.8 i++
				 	}
				 	else
				 	{
					 	gl_buffer[fat_buf_count++]=(Byte)((temp & 0x0ff0)>>4);//modify 2004.3.8 i++
				 	}
				 
					fat_wr_fat();
				 
				 	k++;
				 	fat12_parity=0;
			 	}//end of fragmentation
		 	}
		}  //first if...end
		else    //scan fat to find the next starting cluster
		{	
			if(fat12_parity==0)
			{
				fat_buf_count++;
				fat_wr_fat();

				fat_buf_count++;
				cluster++;
				fat12_parity = 1;
			}
			else
			{
				fat_buf_count++;
				cluster++;
				fat12_parity=0;
			}
			fat_wr_fat();
		}
	}
	while((k<nb_cluster-1) || ((cluster<(chain[index].cluster.w[1]+2)) && (chain[index].number != 0)) /*|| ((nb_cluster == 1)&&( k<=0))*/);//2004.3.9 modify

	//while((k<nb_cluster-1) || ((nb_cluster==1) && (cluster<(chain[index].cluster+2))));

	/* end of file: make it with 0xfff */

	if ((cluster == chain[index].cluster.w[1] + 2) && (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(fat12_parity==0)
	{
		if(del_flag==TRUE)
		{
			gl_buffer[fat_buf_count++]=0x00;
		}
		else
		{
			gl_buffer[fat_buf_count++]=0xff;
		}
		fat_wr_fat();
		if(del_flag==TRUE)
		{
			gl_buffer[fat_buf_count] &= 0xf0;
		}
		else
		{
			gl_buffer[fat_buf_count] |= 0x0f;
		}
	}
	else
	{
		if(del_flag==TRUE)
		{
			gl_buffer[fat_buf_count++] &= 0x0f;
		}
		else
		{
			gl_buffer[fat_buf_count++] |= 0xf0;
		}
		fat_wr_fat();
		if(del_flag==TRUE)
		{
			gl_buffer[fat_buf_count]=0x00;
		}
		else
		{
			gl_buffer[fat_buf_count]=0xff;
		}
	}  

//	fat_phy_fat_end(fat_sector_count.b[1]);
}
#endif //COMPILE_FAT_12

#if COMPILE_FAT_32 == TRUE
void fat32_phy_fats(fat_st_clust_chain xdata *chain, Uint16 nb_cluster,byte del_flag)
{

  	Byte index;
  	Byte chain_index;
//  	Byte sector_number;
//  	Uint16 k;
//  	Uint16 i;
  	Union32 cluster;
//  	Uint16 temp;

  	Uint16 j;
  	byte  flag;
	Uint16 sectorNum_bak;
////////////////////////////////////
  	index=0;
  	j=0;
	fat_sector_count.w = (chain[0].cluster.l + 2) / (fat_sector_size >> 2);	
	phy_read_open(fat_sector_count.w + fat_ptr_fats);
	phy_read_sector();	
	 /* i -> word fat sector position */
    fat_buf_count = ((chain[index].cluster.l + 2) << 2) & (fat_sector_size - 1);  //0x1FF;
    /* init the starting cluster value */
    cluster.l = chain[index].cluster.l + 2;;
    chain_index = 1;
	flag = 0;
//	j = 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;
	            gl_buffer[fat_buf_count++] = 0x00;
	            gl_buffer[fat_buf_count++] = 0x00;
	        }
	        else
	        {
	            gl_buffer[fat_buf_count++] = chain[index].cluster.l + 2;
	            gl_buffer[fat_buf_count++] = (chain[index].cluster.l + 2) >> 8;
	            gl_buffer[fat_buf_count++] = (chain[index].cluster.l + 2) >> 16;
	            gl_buffer[fat_buf_count++] = (chain[index].cluster.l + 2) >> 24;
	        }
	        chain_index = 1;
			sectorNum_bak = (chain[index].cluster.l + 2) / (fat_sector_size >> 2);

			if (sectorNum_bak != fat_sector_count.w)
			{/* Fat change sector */
				/* FAT 1 update */
				fat_write_sector(fat_ptr_fats + fat_sector_count.w);
				if (fat_2_is_present == TRUE)
		        {
		            /* FAT 2 update */
					fat_read_sector(fat_ptr_fats + fat_sector_count.w);
					fat_write_sector(fat_ptr_fats + fat_sector_count.w + fat_fat_size);
				}
				fat_sector_count.w = sectorNum_bak;

				fat_read_sector(fat_ptr_fats + fat_sector_count.w);
			}
		    fat_buf_count = ((chain[index].cluster.l + 2) << 2) & (fat_sector_size - 1);//0x1FF;
		    cluster.l = chain[index].cluster.l + 2;
			flag = 1;
		}
		else
		{
			cluster.l++;
		    if (del_flag == TRUE)
	        {
	            gl_buffer[fat_buf_count++] = 0x00;
	            gl_buffer[fat_buf_count++] = 0x00;
	            gl_buffer[fat_buf_count++] = 0x00;
	            gl_buffer[fat_buf_count++] = 0x00;
	        }
	        else
	        {
	            gl_buffer[fat_buf_count++] = cluster.b[3]; 
	            gl_buffer[fat_buf_count++] = cluster.b[2]; 
	            gl_buffer[fat_buf_count++] = cluster.b[1]; 
	            gl_buffer[fat_buf_count++] = cluster.b[0]; 
	        }
			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.w--;
			 fat_read_sector(fat_ptr_fats + fat_sector_count.w);	
		     fat_buf_count = fat_sector_size - 4;
	    }
	    else
	    {
	          fat_buf_count = fat_buf_count - 4;
	    }
	}
	
	if (del_flag == TRUE)
	{
	     gl_buffer[fat_buf_count++] = 0x00;
	     gl_buffer[fat_buf_count++] = 0x00;
	     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;
	     gl_buffer[fat_buf_count++] = 0xff;
	     gl_buffer[fat_buf_count++] = 0x0f;
	}		

//	fat_phy_fat_end(sectorNum);
}
#endif //COMPILE_FAT_32

#endif //COMPILE_FAT_WRITE

void fat_read_sector( Uint32 number)
{
	phy_read_open(number); 
    phy_read_sector();
    phy_read_close();
}


void fat_write_sector( Uint32 number)
{
#if COMPILE_FAT_WRITE == TRUE
	phy_write_open(number,1);
    phy_write_sector();
    phy_write_close();
#else
number = number;
#endif //#if COMPILE_FAT_WRITE == TRUE
}

/*F**************************************************************************
* NAME: fat_wr_fat
*----------------------------------------------------------------------------
* PARAMS: 
*   id: file extension to select
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
*   Go to the parent directory
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/   
void fat_wr_fat()
{
#if COMPILE_FAT_WRITE == TRUE
	if(fat_buf_count==fat_sector_size)
	{
		//fat_wr_fat(fat_sector_count.b[1]);
	    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);
	    } 
	   
		fat_sector_count.w++;
		fat_read_sector(fat_ptr_fats + fat_sector_count.w);

		fat_buf_count=0;
	}
#endif //#if COMPILE_FAT_WRITE == TRUE	
}



⌨️ 快捷键说明

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