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

📄 fat1x.c

📁 PDA上的CF CARD 文件系统的建立程式
💻 C
📖 第 1 页 / 共 2 页
字号:
		{
			FATClusterCache[i][j].drive = -1;
			FATClusterCache[i][j].cluster_no = 0;
			FATClusterCache[i][j].luCount = 0;
			FATClusterCache[i][j].dirty = FALSE;
			
			if (FATClusterCache[i][j].diskBuffer != NULL)
			{
				ap_free(FATClusterCache[i][j].diskBuffer);
				FATClusterCache[i][j].diskBuffer = NULL;
			}
		}
  #endif

  #ifdef FAT_ROOT_CACHE_ON
  	for (i = 0; i < FAT_ROOT_CACHE_BLOCK; i++)
  	{
		FATRootCache[i].drive = -1;
		FATRootCache[i].sec_num = 0;
		FATRootCache[i].lu = 0;
		FATRootCache[i].DirtySec = FALSE;
		if (FATRootCache[i].Entry != NULL)
		{
			ap_free(FATRootCache[i].Entry);
			FATRootCache[i].Entry = NULL;
		}
	}
  #endif
  	
  }
  else
  {
  	// free all FAT semaphores except FATDirSemaphoreID because
  	//    we have to stop other threads from invoking any FAT-related functions
  	//    we have to signal all semaphores so that no threads will wait forever
  
  	if (FATAtaSemaphoreID > 0)
  	{
  		sc_signalSemaphore(FATAtaSemaphoreID);
  		sc_deleteSemaphore(FATAtaSemaphoreID);
  		FATAtaSemaphoreID = -1;
  	}
  
  	if (FATFatSemaphoreID > 0)
  	{
  		sc_signalSemaphore(FATFatSemaphoreID);
  		sc_deleteSemaphore(FATFatSemaphoreID);
  		FATFatSemaphoreID = -1;
  	}
  
  	if (FATFtableSemaphoreID > 0)
  	{
  		sc_signalSemaphore(FATFtableSemaphoreID);
  		sc_deleteSemaphore(FATFtableSemaphoreID);
  		FATFtableSemaphoreID = -1;
  	}
  
  	if (FATDirSemaphoreID > 0)
  	{
  		sc_signalSemaphore(FATDirSemaphoreID);
  		sc_deleteSemaphore(FATDirSemaphoreID);
  		FATDirSemaphoreID = -1;
  	}
  
  	// 3. availDrive has to be minus 1
  	if (FATAvailDrive > 0)
  		FATAvailDrive--;
  }
  
  return;
  /**** modified by chilong 03/18/2002 ****/
  
/* marked by chilong 03/18/2002
  int i;
  
  if (FATHandleTable == NULL)
  {
	FATErrno = ERROR_FILE_SYSTEM_NOT_INIT;
  	return;
  }

  FATErrno = 0;

  // 1. free all resources from the file handle table
  for (i = 0; i < MAX_OPEN_FILE; i++)
  {
	if (FATHandleTable[i] != NULL)
	{
		if ((FATHandleTable[i])->deviceID == FAT_ID)
		{
  			ap_free(FATHandleTable[i]->buffer);
  			ap_free(FATHandleTable[i]);
			FATHandleTable[i] = NULL;
		}
	}
  }
  
  // 2. free all FAT semaphores except FATDirSemaphoreID because
  //    we have to stop other threads from invoking any FAT-related functions
  //    we have to signal all semaphores so that no threads will wait forever
  
  // !!NOTE: we put this part in FAT_checkCFCard()
  if (FATAtaSemaphoreID > 0)
  {
  	sc_signalSemaphore(FATAtaSemaphoreID);
  	sc_deleteSemaphore(FATAtaSemaphoreID);
  	FATAtaSemaphoreID = -1;
  }
  
  if (FATFatSemaphoreID > 0)
  {
  	sc_signalSemaphore(FATFatSemaphoreID);
  	sc_deleteSemaphore(FATFatSemaphoreID);
  	FATFatSemaphoreID = -1;
  }
  
  if (FATFtableSemaphoreID > 0)
  {
  	sc_signalSemaphore(FATFtableSemaphoreID);
  	sc_deleteSemaphore(FATFtableSemaphoreID);
  	FATFtableSemaphoreID = -1;
  }
  
  if (FATDirSemaphoreID > 0)
  {
  	sc_signalSemaphore(FATDirSemaphoreID);
  	sc_deleteSemaphore(FATDirSemaphoreID);
  	FATDirSemaphoreID = -1;
  }
  
  // 3. availDrive has to be minus 1
//  FATAvailDrive--;

   marked by chilong 03/18/2002 */

}

/*************************************************************
Function: FAT_checkCFCard()
Description:
Input:
	isCardOut
Output:
	// ISR version
	0: CF Card Out
	1: Card In
	-1: Other errors, see FATErrno
	
	// non-ISR version
	0: CF Card Out
	1: Same CF Card In
	2: Different CF Card In
	-1: Other errors, see FATErrno
	
Note: The current version is only invoked by CF_handleCardInOrOut in FileSys
      and CF_handleCardInOrOut is invoked by CF's ISR.
**************************************************************/
int FAT_checkCFCard(int isCardOut)
{
#ifdef FAT_USE_PCMCIA
  unsigned short *sectorBuffer;
  
#ifdef FAT_WITH_INTERRUPT
#else
  unsigned short tmpDriveSerialNum[10];
  int 		 i;
  int 		 isTheSameCFCard = 1;
#endif
 
  /* marked by chilong 02/01/2002
  if ((sectorBuffer = (unsigned short *)ap_malloc(SECTOR_SIZE)) == NULL)
  {
  	FATErrno = ERROR_ALLOC_MEM;
  	return -1;
  }
     marked by chilong 02/01/2002 */

#ifdef FAT_WITH_INTERRUPT
  if (isCardOut == 0)
  {
  	/**** modified by chilong 02/01/2002 ****/
  	if ((sectorBuffer = (unsigned short *)ap_malloc(SECTOR_SIZE)) == NULL)
  	{
  		FATErrno = ERROR_ALLOC_MEM;
  		return -1;
  	}
  	/**** modified by chilong 02/01/2002 ****/
  	
  	
  	// Now the card status is in, but we need to double check it here
  	// for fear that the card might be out at the moment
  	if (CFCardInCheck() == CARD_IN)
  	{
  		if (FAT_get_hd_chs(sectorBuffer) != ATA_OK)
  		{
			FATErrno = ERROR_HD_CHS;
		
  			InitFAT = FALSE;

			//FAT_releaseCFResources();
			// modified by chilong 
			FAT_releaseCFResources(0);
  			
  			ap_free(sectorBuffer);
  		
	  		return -1;
  		}
  		
		/* marked by chilong 03/18/2002
		FAT_releaseCFResources();
		InitFAT = FALSE;
		   marked by chilong 03/18/2002 */

		/**** modified by chilong 03/18/2002 ****/
  		ap_free(sectorBuffer);
  		
		InitFAT = FALSE;
		FAT_releaseCFResources(0);
		/**** added by chilong 03/18/2002 ****/

		if (FAT_init() == -1)
		{
	  		// if FAT_Init() fails, let's suppose the CF card is Out and
  			// let FileSys release relative resources from FAT disks
  			return 0;
	  	}
  	
  		return 1;
  	}
  	else
  	{
//  		FAT_releaseCFResources();
		InitFAT = FALSE;
  		ap_free(sectorBuffer);
  	
  		return 0;
  	}
  }
  else
  {
  	/* marked by chilong 03/18/2002
//  	FAT_releaseCFResources();
	InitFAT = FALSE;
	
	if (FATAvailDrive > 0)
		FATAvailDrive--;
  	   marked by chilong 03/18/2002 */
  	   
  	/**** modified by chilong 03/18/2002 ****/
	InitFAT = FALSE;
  	FAT_releaseCFResources(1);
  	/**** modified by chilong 03/18/2002 ****/
		
  	ap_free(sectorBuffer);
  	return 0;
  }
#else // non ISR-version

  /**** modified by chilong 02/01/2002 ****/
  if ((sectorBuffer = (unsigned short *)ap_malloc(SECTOR_SIZE)) == NULL)
  {
  	FATErrno = ERROR_ALLOC_MEM;
  	return -1;
  }
  /**** modified by chilong 02/01/2002 ****/

  // save the drive serial number temporarily
  for (i = 0; i < 10; i++)
  	tmpDriveSerialNum[i] = FATDiskInfo.driveSerialNum[i];
   
  if (CFCardInCheck() == CARD_IN)
  {
  	if (FAT_get_hd_chs(sectorBuffer) != ATA_OK)
  	{
		FATErrno = ERROR_HD_CHS;
		
		FAT_releaseCFResources();
  		InitFAT = FALSE;
  		ap_free(sectorBuffer);
  		
  		return -1;
  	}
  	
  	// 1.check if serial number is the same as the current one
  	//   If not, reinitialize CF
  	//   If yes, return 1
  	for (i = 0; i < 10; i++)
  	{
  		if (tmpDriveSerialNum[i] != FATDiskInfo.driveSerialNum[i])
  		{
  			isTheSameCFCard = 0;
  			break;
  		}
  	}
  	
  	if (isTheSameCFCard == 1 && InitFAT == TRUE)
  	{
		ap_free(sectorBuffer);
		
  		return 1;
  	}
  	
	FAT_releaseCFResources();
	InitFAT = FALSE;

  	// we delete FATDirSemaphoreID here because we have
  	// to stop other threads from accessing FAT disk
	if (FAT_init() == -1)
	{
  		// if FAT_Init() fails, let's suppose the CF card is Out and
  		// let FileSys release relative resources from FAT disks
  		return 0;
  	}
  	
  	if (isTheSameCFCard == 1)
  		return 1;
  	else
  	 	return 2;
  		
  }
  else
  {
  	FAT_releaseCFResources();
	InitFAT = FALSE;
  	ap_free(sectorBuffer);
  	
  	return 0;
  }
#endif  
#else // FAT_USE_PCMCIA
	return 1;//thhuang
#endif
}


/*************************************************************
Function: FAT_open
Description:
	Open a file, it will:
	(1) Construct a file structure
	(2) Set up an file table entry pointing to the above
	structure
Input:
	filename: may contains directories in path flag:
        flag: /Open Only/Create/Truncate/Append
Output:
	file handle is returned
	-1 if the file is not exist, and O_CREATE flag is not set
**************************************************************/
/* 虫

⌨️ 快捷键说明

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