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

📄 filesys.c

📁 PDA上的CF CARD 文件系统的建立程式
💻 C
📖 第 1 页 / 共 5 页
字号:
/*************************************************************
File Name: FileSys.C
Last Modified Date: 2001/04/17
Programmer: MSC
Compiler:
Platform:
Usage:
	Generic file system API

Other Information:

**************************************************************/
#include <sys/syscall.h>

#include <stdio.h>
#include <string.h>

#include <uiGui_Cfg.h>
//#include "../rDebug/include/rdebug.h"
#include "../include/filesys.h"

#ifdef RAMDISK_ID
  #include "../../RAMDisk4.2/include/ramdisk.h"
#endif

#ifdef FAT_ID
  //#include "../../driver/fat1x/include/fat1x.h"
  #include "fat1x/include/fat1x.h"
#endif

#ifdef NOR_FLASH_DISK_ID
  #include "../../driver/FFS_NOR/include/FFS_NOR.h"
#endif

#ifdef NAND_FLASH_DISK_ID
  //#include "../../driver/FFS_NAND/include/FFS_NAND.h"
	//#include "FFS_NAND/include/FFS_NAND.h"
	#include "../../FFS_NAND/include/FFS_NAND.h"
	#include "../../FFS_NAND/include/nandfind.h"
#endif

#ifdef FILESYS_DEBUG
  char FS_DebugString[80];
#endif

#ifdef __WIN32__
#include "../../DiskEmu/include/nfshEmu.h"
#endif

// the file handle table shared by all devices
// every device has its own file handle structure
//   but the first two bytes of a file handle of any device
//   record the device ID of the device
//   that's why (short *) is used instead of (void *)
//short *FileSysHandleTable[MAX_OPEN_FILE];
short **FileSysHandleTable;

// device table
short FileSysDeviceTable[DEVICE_NUM];

// current drive
int CurrentDrive = -1;

/**** added by chilong 8/15/2001 ****/
// used to restore CurrentDrive while a CF card is in again
int LastDrive = -1;
/**** added by chilong 8/15/2001 ****/

// file system error number
int FileSys_Errno;

// file system initialize
int FileSys_Running = 0;

extern int CF_Format(void);
//******************************** jason
/*#define open		fs_open
#define close		fs_close
#define read		fs_read
#define write		fs_write
#define lseek		fs_lseek
#define filelength	fs_filelength
#define remove		fs_delfile
#define truncate	fs_truncate
#define findfirst	fs_findfirst
#define findnext	fs_findnext
#define eof			fs_eof
*/

//**************************************


// pathname that matches the user-provided pathname but may have a little modification

/* marked by chilong 01/15/2002
unsigned char *Pathname1;
unsigned char *Pathname2;
   marked by chilong 01/15/2002 */

/*************************************************************
Function: FileSys_init
Description:
	initialize the file system
Input:
	NONE
Output:
	0	succeeded
	-1	failed
**************************************************************/
int FileSys_init(void)
{
  int i;

  if (FileSys_Running == 1)
	return 0;

  FileSys_Errno = 0;

  #ifdef FILESYS_DEBUG
	sprintf(FS_DebugString, "[FileSys_init]");
	SprintStringLn(FS_DebugString);
  #endif

  #ifdef FILESYS_DEBUG
	sprintf(FS_DebugString, "    clear file handle table");
	SprintStringLn(FS_DebugString);
  #endif

  /* marked by chilong 01/15/2002 
  Pathname1 = (unsigned char *)ap_malloc(MAX_PATHNAME_LEN + 2);
  if (Pathname1 == NULL)
  {
	FileSys_Errno = ERROR_ALLOC_MEM;
	return -1;
  }

  Pathname2 = (unsigned char *)ap_malloc(MAX_PATHNAME_LEN + 2);
  if (Pathname2 == NULL)
  {
	ap_free(Pathname1);
	FileSys_Errno = ERROR_ALLOC_MEM;
	return -1;
  }
     marked by chilong 01/15/2002 */

  FileSysHandleTable = (short **)ap_malloc(MAX_OPEN_FILE * sizeof(short *));
  if (FileSysHandleTable == NULL)
  {
  	/* marked by chilong 01/15/2002
	ap_free(Pathname2);
	ap_free(Pathname1);
  	   marked by chilong 01/15/2002 */
  	   
	FileSys_Errno = ERROR_ALLOC_MEM;
	return -1;
  }

  #ifdef FS_CURRENT_DIRECTORY_ENABLED
	if (initCurrentPath() == -1)
	{
		ap_free(FileSysHandleTable);
		
		/* marked by chilong 01/15/2002
		ap_free(Pathname2);
		ap_free(Pathname1);
		   marked by chilong 01/15/2002 */
  	   
		return -1;
	}
  #endif

  // clear the file handle table
  for (i = 0; i < MAX_OPEN_FILE; i++)
	FileSysHandleTable[i] = NULL;

  #ifdef FILESYS_DEBUG
	sprintf(FS_DebugString, "    setup device table");
	SprintStringLn(FS_DebugString);
  #endif

  // clear up the storage device table
  for (i = 0; i < DEVICE_NUM; i++)
	FileSysDeviceTable[i] = -1;

  // fill in the device IDs
  for (i = 0; i < DEVICE_NUM; i++)
  {
	#ifdef RAMDISK_ID
		if (i == RAMDISK_ID)
		{
			FileSysDeviceTable[i] = i;
			continue;
		}
	#endif	// #ifdef RAMDISK_ID

	#ifdef FAT_ID
		if (i == FAT_ID)
		{
			FileSysDeviceTable[i] = i;
			continue;
		}
	#endif	// #ifdef FAT_ID


	#ifdef NOR_FLASH_DISK_ID
		if (i == NOR_FLASH_DISK_ID)
		{
			FileSysDeviceTable[i] = i;
			continue;
		}
	#endif	// #ifdef NOR_FLASH_DISK_ID


	#ifdef NAND_FLASH_DISK_ID
		if (i == NAND_FLASH_DISK_ID)
		{
			printf("nflash init, size: %d\n\r", nfshInit() );
			FileSysDeviceTable[i] = i;
			continue;
		}
	#endif	// #ifdef NOR_FLASH_DISK_ID
  }

  #ifdef FILESYS_DEBUG
	sprintf(FS_DebugString, "    devices: %d %d %d %d", FileSysDeviceTable[0], FileSysDeviceTable[1], FileSysDeviceTable[2], FileSysDeviceTable[3]);
	SprintStringLn(FS_DebugString);

	sprintf(FS_DebugString, "    initialize devices");
	SprintStringLn(FS_DebugString);
  #endif

  // initialize the individual device
  for (i = 0; i < DEVICE_NUM; i++)
  {
	if (FileSysDeviceTable[i] != -1)
	{
		#ifdef RAMDISK_ID
			if (FileSysDeviceTable[i] == RAMDISK_ID)
			{
				#ifdef FILESYS_DEBUG
					sprintf(FS_DebugString, "    initialize RAMDisk");
					SprintStringLn(FS_DebugString);
				#endif

				// the device is RAMDisk
				if (ramdisk_init((long)GUI_RAM_DISK_SIZE, 1) == -1)
				{
					// initialization failed, remove the device
					#ifdef FILESYS_DEBUG
						sprintf(FS_DebugString, "        ...FAILED!");
						SprintStringLn(FS_DebugString);
					#endif

					FileSysDeviceTable[i] = -1;
					FileSys_Errno = RDerrno;
				}
				else
				{
					// initialization succeeded
					#ifdef FILESYS_DEBUG
						sprintf(FS_DebugString, "        ...SUCCEEDED!");
						SprintStringLn(FS_DebugString);
					#endif

					#ifdef FILESYS_DEBUG
						sprintf(FS_DebugString, "    RDExist = %d", RDExist);
						SprintStringLn(FS_DebugString);
					#endif
				}

				continue;
			}
		#endif	// #ifdef RAMDISK_ID

		#ifdef FAT_ID
			if (FileSysDeviceTable[i] == FAT_ID)
			{
				#ifdef FILESYS_DEBUG
					sprintf(FS_DebugString, "    initialize FAT/ATA");
					SprintStringLn(FS_DebugString);
				#endif

				// the device is FAT/ATA
				if (FAT_init() == -1)
				{
					// initialization failed, remove the device
					#ifdef FILESYS_DEBUG
						sprintf(FS_DebugString, "        ...FAILED!");
						SprintStringLn(FS_DebugString);
					#endif

					FileSysDeviceTable[i] = -1;
					FileSys_Errno = FATErrno;
				}
				else
				{
					// initialization succeeded
					#ifdef FILESYS_DEBUG
						sprintf(FS_DebugString, "        ...SUCCEEDED!");
						SprintStringLn(FS_DebugString);
					#endif
				}

				continue;
			}
		#endif	// #ifdef FAT_ID

		#ifdef NOR_FLASH_DISK_ID
			if (FileSysDeviceTable[i] == NOR_FLASH_DISK_ID)
			{
				#ifdef FILESYS_DEBUG
					sprintf(FS_DebugString, "    initialize NOR Flash disk");
					SprintStringLn(FS_DebugString);
				#endif

				// the device is flash disk
				if (NOR_FFS_init() == -1)
				{
					// initialization failed, remove the device
					#ifdef FILESYS_DEBUG
						sprintf(FS_DebugString, "        ...FAILED!");
						SprintStringLn(FS_DebugString);
					#endif

					FileSysDeviceTable[i] = -1;
					FileSys_Errno = NOR_FFS_Errno;
				}
				else
				{
					// initialization succeeded
					#ifdef FILESYS_DEBUG
						sprintf(FS_DebugString, "        ...SUCCEEDED!");
						SprintStringLn(FS_DebugString);
					#endif
				}

				continue;
			}
		#endif	// #ifdef NOR_FLASH_DISK_ID


		#ifdef NAND_FLASH_DISK_ID
			if (FileSysDeviceTable[i] == NAND_FLASH_DISK_ID)
			{
				#ifdef FILESYS_DEBUG
					sprintf(FS_DebugString, "    initialize NAND Flash disk");
					SprintStringLn(FS_DebugString);
				#endif

				// the device is flash disk
				if (NAND_FFS_init() == -1)
				{
					// initialization failed, remove the device
					#ifdef FILESYS_DEBUG
						sprintf(FS_DebugString, "        ...FAILED!");
						SprintStringLn(FS_DebugString);
					#endif

					FileSysDeviceTable[i] = -1;
					FileSys_Errno = NAND_FFS_Errno;
				}
				else
				{
					// initialization succeeded
					#ifdef FILESYS_DEBUG
						sprintf(FS_DebugString, "        ...SUCCEEDED!");
						SprintStringLn(FS_DebugString);
					#endif
				}

				continue;
			}
		#endif	// #ifdef NAND_FLASH_DISK_ID
	}
  }

  #ifdef FILESYS_DEBUG
	sprintf(FS_DebugString, "    set current drive");
	SprintStringLn(FS_DebugString);
  #endif

  // set current drive
  CurrentDrive = -1;

/* marked by chilong 12/4/2001 
  for (i = 0; i < DEVICE_NUM; i++)
  {
	if (FileSysDeviceTable[i] != -1)
	{
		CurrentDrive = i;
		break;
	}
  }
   marked by chilong 12/4/2001 */

  /**** modified by chilong 12/4/2001 ****/
//Kevin, if no RAMDISK, can't compile
#if 0
#ifdef RAMDISK_ID
  if (FileSysDeviceTable[RAMDISK_ID] != -1)
  	CurrentDrive = RAMDISK_ID;
#else
  if (FileSysDeviceTable[NAND_FLASH_DISK_ID] != -1)
  	CurrentDrive = NAND_FLASH_DISK_ID;
#endif

  else
  {
  	for (i = 0; i < DEVICE_NUM; i++)
  	{
		if (FileSysDeviceTable[i] != -1)
		{
			CurrentDrive = i;
			break;
		}
  	}
  }
#endif
  	CurrentDrive = 0;//Kevin, 2003/5/2, current disk is a: disk
  /**** modified by chilong 12/4/2001 ****/
  
  FileSys_Running = 1;

  #ifdef FILESYS_DEBUG
	sprintf(FS_DebugString, "    DONE");
	SprintStringLn(FS_DebugString);
  #endif

  return 0;
}



/*************************************************************
Function: FileSys_clear
Description:
	clear/format the target drive
Input:
	drive - the target drive name
Output:
	0	succeeded
	-1	failed
**************************************************************/
//int FileSys_clear(unsigned char *drive)
int FileSys_clear(char *drive)
{
  int result;
  short driveNO;
  
  /**** added by chilong 01/15/2002 ****/
  char *Pathname1;
  /**** added by chilong 01/15/2002 ****/
  
  if (FileSys_Running == 0)
  {
	FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }

  /**** added by chilong 12/7/2001 ****/
  if (drive == NULL)
  	return -1;
  	
  /**** added by chilong 12/7/2001 ****/
  
  /**** added by chilong 01/15/2002 ****/
  if ((Pathname1 = (char *)ap_malloc(MAX_PATHNAME_LEN + 2)) == NULL)
  {
  	FileSys_Errno = ERROR_ALLOC_MEM;
  	return -1;
  }
  /**** added by chilong 01/15/2002 ****/
  
  FileSys_Errno = 0;

  driveNO = getDriveNO(drive, Pathname1);
  if (driveNO == -1)
  {
	ap_free(Pathname1);
	  	
	return -1;
  }

  #ifdef NOR_FLASH_DISK_ID
	if (driveNO == NOR_FLASH_DISK_ID)
	{
		result = NOR_FFS_clear();
		FileSys_Errno = NOR_FFS_Errno;

		ap_free(Pathname1);

		return result;
	}
  #endif

  #ifdef NAND_FLASH_DISK_ID
	if (driveNO == NAND_FLASH_DISK_ID)
	{
		result = NAND_FFS_clear();
		FileSys_Errno = NAND_FFS_Errno;

		ap_free(Pathname1);
		
		return result;
	}
  #endif

  #ifdef RAMDISK_ID
	if (driveNO == RAMDISK_ID)
	{
		result = ramdisk_clear();
		FileSys_Errno = RDerrno;

		ap_free(Pathname1);

		return result;
	}
  #endif

  #ifdef FAT_ID
	if (driveNO == FAT_ID)
	{
		result = FAT_clear();
		FileSys_Errno = FATErrno;

		ap_free(Pathname1);

		return result;
	}
  #endif

  FileSys_Errno = ERROR_INVALID_DEVICE;
  
  ap_free(Pathname1);
  
  return -1;
}

/*************************************************************
Function: chdrive
Description:
	change the current drive
Input:
	drive - the target drive number
Output:
	0	succeeded
	-1	failed
Note:
	Current drive number follows device ID => starts from 0.
	Drive number for user starts from 1 (0 = current drive).
**************************************************************/
int fs_chdrive(int drive)
{
  int i;
  int oldDrive;
  //unsigned char *buffer;
  char *buffer;
  
  if (FileSys_Running == 0)
  {
	FileSys_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
	return -1;
  }
  
  /**** added by chilong 12/7/2001 ****/
  if (drive < 0)
  	return -1;
  /**** added by chilong 12/7/2001 ****/

  if ((buffer = (char *)ap_malloc(MAX_PATHNAME_LEN + 2)) == NULL)
  {
	FileSys_Errno = ERROR_ALLOC_MEM;
	
  	return -1;
  }
  
  if (drive == 0)
  {
  	ap_free(buffer);
	return 0;
  }

  drive--;

  for (i = 0; i < DEVICE_NUM; i++)
  {
	if (FileSysDeviceTable[i] == drive)
	{
		oldDrive = CurrentDrive;
		CurrentDrive = drive;
		
  		// if the current path of the specified drive is valid
  		buffer[0] = BEGIN_DRIVE_NAME + drive;
  		buffer[1] = ':';
  		if (fs_getcwd(buffer+2, MAX_PATHNAME_LEN) == NULL)
  		{
  			CurrentDrive = oldDrive;
  			ap_free(buffer);

  			return -1;
  		}
  
  		if (fs_access(buffer, 0x0) == -1)
  		{
			FileSys_Errno = ERROR_INVALID_PATHNAME;
  			CurrentDrive = oldDrive;
  			ap_free(buffer);


  			return -1;
  		}

	  	ap_free(buffer);
		return 0;
	}
  }

  FileSys_Errno = ERROR_INVALID_DEVICE;
  ap_free(buffer);
  return -1;
}



/*************************************************************
Function: getdrive

⌨️ 快捷键说明

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