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

📄 ffs_nor.c

📁 norflash 代码!
💻 C
字号:
/*************************************************************
File Name: FFS.C
Last Modified Date: 2000/09/15
Programmer: MSC
Compiler:
Platform:
Usage:
	Flash File System

Unfinished functions:
	NOR_FFS_scanDisk()
	NOR_FFS_defrag()
**************************************************************/
//#include <sys/syscall.h>
//#include "../../rDebug/rdebug.h"

#include <ar2kreg.h>
#include <artsm.h>
#include <errno.h>
#include "../../FileSys/include/FileSys.h"

#include "../include/FFS_NOR.h"
#include "../include/NORfblk.h"

#include "../include/NORfdrv.h"
#include "../include/FFSFile.h"


#ifdef NOR_FLASH_DISK_ID


#ifdef NOR_FFS_DEBUG
	#include <ansi_c/stdio.h>
	char NOR_FFS_DebugString[80];
#endif

// the block map
//	0 = cannot be used by FFS
//	1 = free flash block
//	2 = allocated flash block

/* marked by chilong 11/26/2001 
char NOR_FFS_Block[NOR_FFS_BLOCK_NUM] = {
	1, 1, 1, 1, 1, 1, 1, 1,	//  0- 7
	1, 1, 1, 1, 1, 1, 1, 1	//  8-15
};
   marked by chilong 11/26/2001 */

/* modified by chilong 03/08/2002
char NOR_FFS_Block[NOR_FFS_BLOCK_NUM] = {
	1, 1, 1, 1, 1, 1, 1, 1	//  0- 7
};
   modified by chilong 03/08/2002 */

#ifdef MACHINE_TYPE_MIPS_ARTI
unsigned char Nor_ffs_cache_buf[NOR_FFS_CACHE_LUMP_NUM * NOR_LUMP_SIZE + NOR_FFS_CACHE_LUMP_NUM * sizeof(struct NOR_FFS_cacheInfo)];
#endif

char NOR_FFS_Block[NOR_FFS_BLOCK_NUM] = {
	1, 1, 1, 1, 
	1, 1, 1, 1,
	1, 1, 1, 1,
	1, 1, 1, 1,
	1, 1, 1, 1, 
	1, 1, 1, 1
};
// erase counts for the flash blocks
unsigned long *NOR_FFS_EraseCount;

//int FFS_FreeBegin = NOR_FFS_START_BLOCK;

// semaphore to serialize FFS accesses
//int NOR_FFS_SemaphoreID;
int NOR_FFS_Init_OK = FALSE;
int NOR_FFS_Errno;

unsigned long NOR_FFS_AddressLowerBound = 0;
unsigned long NOR_FFS_AddressUpperBound = 0;

// cache info
struct NOR_FFS_cacheInfo *NOR_FFS_CacheStatus[NOR_FFS_CACHE_LUMP_NUM];
unsigned char *NOR_FFS_LumpCache[NOR_FFS_CACHE_LUMP_NUM];
char *NOR_FlashAccessBuffer;
//int FFS_CacheFlushCount;
//char *FlashBlockBuffer;
//int FBlockInBuffer;

// location of flash erase and program function in RAM
NOR_eraseFun NOR_FFS_erase;	// function pointer to the flash sector erase function in RAM
NOR_programFun NOR_FFS_program;	// function pointer to the flash sector program function in RAM



// FileSys.c
//extern short *FileSysHandleTable[MAX_OPEN_FILE];
extern short **FileSysHandleTable;

// file.c
extern struct NOR_FFS_FILE **NOR_FFS_HandleTable;
extern int NOR_FFS_CurrentHandle;

// lump.c
extern unsigned long NOR_LumpNum;


/*************************************************************
Function: NOR_FFS_init
Description:
	initialize the simple flash storage system
Input:
Output:
	0	SUCCESS
	-1	FAILURE
**************************************************************/
int NOR_FFS_init(void)
{
  int i;
  unsigned long erFuncSize, prFuncSize;
  int *src;
  int *des;
  unsigned short *bootStatus;

#ifdef NOR_FFS_DEBUG
  sprintf(NOR_FFS_DebugString, "[NOR_FFS_init] NOR_FFS_Init_OK=%d", NOR_FFS_Init_OK);
  SprintStringLn(NOR_FFS_DebugString);
#endif

  if (NOR_FFS_Init_OK == TRUE)
  {
#ifdef NOR_FFS_DEBUG
	sprintf(NOR_FFS_DebugString, "Flash disk already initialized");
	SprintStringLn(NOR_FFS_DebugString);
#endif
	return 0;
  }

  NOR_FFS_Init_OK = TRUE;

  NOR_FFS_AddressLowerBound = 0;
  NOR_FFS_AddressUpperBound = 0;

  //NOR_FFS_SemaphoreID = sc_createSemaphore(1, 1);
  //if (NOR_FFS_SemaphoreID == -1)
  //{
//	NOR_FFS_Init_OK = FALSE;
//	NOR_FFS_Errno = ERROR_CREATE_SEMAPHORE;
//	return -1;
  //}

#ifndef AR2000
// thhuang, 2002/12/4
// Be care, it's a trap
// Clear write protect bit
*(unsigned int *)0xbfbf0000 &= 0xFFFFFDFF;
#endif

  // prepare the access counts
  NOR_FFS_EraseCount = (unsigned long *)ap_malloc(NOR_FFS_BLOCK_NUM * sizeof(unsigned long));
  if (NOR_FFS_EraseCount == NULL)
  {
	NOR_FFS_Init_OK = FALSE;
	//sc_deleteSemaphore(NOR_FFS_SemaphoreID);
	NOR_FFS_Errno = ERROR_ALLOC_MEM;
	return -1;
  }
  memoryZero(NOR_FFS_EraseCount, NOR_FFS_BLOCK_NUM * sizeof(unsigned long));

/*
  // clear the file handle table
  for (i = 0; i < MAX_OPEN_FILE; i++)
	NOR_FFS_HandleTable[i] = NULL;
*/
  // set the file handle table
  NOR_FFS_HandleTable = (struct NOR_FFS_FILE **)FileSysHandleTable;
  NOR_FFS_CurrentHandle = -1;

  // initialize lump parameters
  NOR_initLump();

//  NOR_FFS_AddressLowerBound = (unsigned long)NOR_FFS_block2Address(0);
//  NOR_FFS_AddressUpperBound = (unsigned long)NOR_FFS_block2Address(NOR_FFS_BLOCK_NUM - 1) + NOR_FLASH_BLOCK_SIZE;
//  NOR_FFS_AddressLowerBound = (unsigned long)fshGetBase() + NOR_FFS_START_BLOCK * NOR_FLASH_BLOCK_SIZE;
//  NOR_FFS_AddressUpperBound = (unsigned long)fshGetBase() + (NOR_FFS_START_BLOCK + NOR_FFS_BLOCK_NUM) * NOR_FLASH_BLOCK_SIZE;
  NOR_FFS_AddressLowerBound = (unsigned long)NOR_FFS_FLASH_BASE_ADDR + NOR_FFS_START_BLOCK * NOR_FLASH_BLOCK_SIZE;
  NOR_FFS_AddressUpperBound = (unsigned long)NOR_FFS_FLASH_BASE_ADDR + (NOR_FFS_START_BLOCK + NOR_FFS_BLOCK_NUM) * NOR_FLASH_BLOCK_SIZE;

//  FBlockInBuffer = -1;

  // load the erase and program functions into RAM
  erFuncSize = (unsigned long)NOR_FFS_fshSectorProgram - (unsigned long)NOR_FFS_fshSectorErase;
  prFuncSize = (unsigned long)NOR_FFS_block2Sector - (unsigned long)NOR_FFS_fshSectorProgram;

#ifdef NOR_FFS_DEBUG
  sprintf(NOR_FFS_DebugString, "erase: %d; program: %d", erFuncSize, prFuncSize);
  SprintStringLn(NOR_FFS_DebugString);
#endif

  erFuncSize = (((erFuncSize - 1) >> 2) + 1) << 2;
  prFuncSize = (((prFuncSize - 1) >> 2) + 1) << 2;

#ifdef NOR_FFS_DEBUG
  sprintf(NOR_FFS_DebugString, "erase: %d; program: %d", erFuncSize, prFuncSize);
  SprintStringLn(NOR_FFS_DebugString);
#endif

/* marked by chilong 12/2/2001
  // chilong: if this part is unmarked one day,
  //	NOR_FFS_fshSectorErase() & NOR_FFS_fshSectorProgram()
  //	need to be taken care of.
  
  NOR_FFS_erase = (NOR_eraseFun)ap_malloc(erFuncSize);
  if (NOR_FFS_erase == NULL)
  {
	NOR_FFS_Init_OK = FALSE;
	sc_free(NOR_FFS_EraseCount);
	sc_deleteSemaphore(NOR_FFS_SemaphoreID);

	NOR_FFS_Errno = ERROR_ALLOC_MEM;
	return -1;
  }

  NOR_FFS_program = (NOR_programFun)ap_malloc(prFuncSize);
  if (NOR_FFS_program == NULL)
  {
	NOR_FFS_Init_OK = FALSE;
	sc_free((void *)NOR_FFS_erase);
	sc_free(NOR_FFS_EraseCount);
	sc_deleteSemaphore(NOR_FFS_SemaphoreID);

	NOR_FFS_Errno = ERROR_ALLOC_MEM;
	return -1;
  }

  // copy erase function to RAM
  src = (int *)NOR_FFS_fshSectorErase;
  des = (int *)NOR_FFS_erase;
  for (i = 0; i < erFuncSize; i += sizeof(int))
	*des++ = *src++;

  // copy program function to RAM
  src = (int *)NOR_FFS_fshSectorProgram;
  des = (int *)NOR_FFS_program;
  for (i = 0; i < prFuncSize; i += sizeof(int))
	*des++ = *src++;

   marked by chilong 12/2/2001 */

  // setup the cache
//  bootStatus = (unsigned short *)STATUS_ADDR;

#ifdef NOR_FFS_DEBUG
  sprintf(NOR_FFS_DebugString, "  boot status: 0x%x", *bootStatus);
  SprintStringLn(NOR_FFS_DebugString);
#endif

  for (i = 0; i < NOR_FFS_CACHE_LUMP_NUM; i++)
	NOR_FFS_CacheStatus[i] = (struct NOR_FFS_cacheInfo *)(NOR_FFS_CACHE_BASE_ADDRESS + i * sizeof(struct NOR_FFS_cacheInfo));

  erFuncSize = NOR_FFS_CACHE_BASE_ADDRESS + NOR_FFS_CACHE_LUMP_NUM * sizeof(struct NOR_FFS_cacheInfo);
  for (i = 0; i < NOR_FFS_CACHE_LUMP_NUM; i++)
	NOR_FFS_LumpCache[i] = (unsigned char *)(erFuncSize + i * NOR_LUMP_SIZE);
/*
#ifdef NOR_FFS_DEBUG
  NOR_FFS_showCacheStatusAddress();
  NOR_FFS_showCacheAddress();
#endif
*/
//  NOR_FFS_CacheStatus = (struct NOR_FFS_cacheInfo *)NOR_FFS_CACHE_BASE_ADDRESS;
//  NOR_FFS_LumpCache = (char *)(NOR_FFS_CACHE_BASE_ADDRESS + sizeof(struct NOR_FFS_cacheInfo));
//  FFS_CacheFlushCount = 0;

  // vertify the cache if warm boot
  NOR_FlashAccessBuffer = (char *)ap_malloc(NOR_FLASH_BLOCK_SIZE);
  if (NOR_FlashAccessBuffer == NULL)
  {
	NOR_FFS_Init_OK = FALSE;
	sc_free((void *)NOR_FFS_program);
	sc_free((void *)NOR_FFS_erase);
	sc_free(NOR_FFS_EraseCount);
	//sc_deleteSemaphore(NOR_FFS_SemaphoreID);

	NOR_FFS_Errno = ERROR_ALLOC_MEM;
	return -1;
  }
#if 0
  if (queryHWStatus() == WARM_RESET)
  {
	// warm reset, vertify the cache
	for (i = 0; i < NOR_FFS_CACHE_LUMP_NUM; i++)
	{
#ifdef NOR_FFS_DEBUG
		sprintf(NOR_FFS_DebugString, "    verify cache %d", i);
		SprintStringLn(NOR_FFS_DebugString);
#endif

		if (NOR_FFS_vertifyCache(i) == -1)
		{
			// cache content incorrect, clear the cache
			NOR_FFS_clearCache(i);
		}
		else
		{
			// cache content ok, flush the cache	
			if (NOR_FFS_flushCache(i) == -1)
			{
				// flush failed, clear the cache
				NOR_FFS_clearCache(i);
			}
			// clear the last modification timestamp
			NOR_FFS_CacheStatus[i]->lastModTime = 0;
			NOR_FFS_CacheStatus[i]->handle = 0;
		}
	}
  }
  else
#endif
  {
	// cold reset, clear the cache
	NOR_FFS_clearAllCache();
  }

/*
#ifdef NOR_FFS_DEBUG
  NOR_FFS_showCacheStatusN(0);
#endif
*/
#ifdef NOR_FFS_DEBUG
  sprintf(NOR_FFS_DebugString, "NOR_FFS_init done NOR_FFS_Init_OK=%d", NOR_FFS_Init_OK);
  SprintStringLn(NOR_FFS_DebugString);
#endif
/*
#ifdef NOR_FFS_DEBUG
  NOR_FFS_showCacheStatusN(0);
#endif
*/
  return 0;
}



/*************************************************************
Function: NOR_FFS_clear
Description:
	Clear all blocks in FFS
Input:
Output:
	0	SUCCESS
	-1	FAILURE
**************************************************************/
int NOR_FFS_clear(void)
{
  int returnValue = 0;

  //sc_waitSemaphore(NOR_FFS_SemaphoreID);

#ifdef NOR_FFS_DEBUG
  sprintf(NOR_FFS_DebugString, "[NOR_FFS_clear] start");
  SprintStringLn(NOR_FFS_DebugString);
#endif

  returnValue = NOR_FFS_clear_r();

#ifdef NOR_FFS_DEBUG
  sprintf(NOR_FFS_DebugString, "[NOR_FFS_clear] done");
  SprintStringLn(NOR_FFS_DebugString);
#endif

  //sc_signalSemaphore(NOR_FFS_SemaphoreID);

  return returnValue;
}



int NOR_FFS_clear_r(void)
{
  int i;

  // all opened files must be closed
  for (i = 0; i < MAX_OPEN_FILE; i++)
  {
	if (NOR_FFS_HandleTable[i] != NULL)
	{
		// handle i in use, check device type
		if (NOR_FFS_HandleTable[i]->deviceID == NOR_FLASH_ID)
		{
			// ah, this file/AP has been opened and is on flash disk
			// close it
			NOR_FFS_close_r(NOR_FFS_HandleTable[i]);
			NOR_FFS_HandleTable[i] = NULL;
		}
	}
  }

  // clear the cache
  for (i = 0; i < NOR_FFS_CACHE_LUMP_NUM; i++)
	NOR_FFS_clearCache(i);

  // clear all blocks
  for (i = 0; i < NOR_FFS_BLOCK_NUM; i++)
  {
	// continue next block if the current one is not usable for AP storage
	if (NOR_FFS_Block[i] == 0)
		continue;

#ifdef NOR_FFS_DEBUG
	sprintf(NOR_FFS_DebugString, "    Erasing block %d", i);
	SprintStringLn(NOR_FFS_DebugString);
#endif

	if (NOR_FFS_eraseFlashBlock(i) == -1)
	{
#ifdef NOR_FFS_DEBUG
		sprintf(NOR_FFS_DebugString, "    Erase block %d error", i);
		SprintStringLn(NOR_FFS_DebugString);
#endif
		return -1;
	}

	NOR_FFS_EraseCount[i]++;
  }	

  return 0;
}


/*
// implemented in NORscan.c
char NOR_FFS_scanDisk(char interactive, char fix)
{
  return -1;
}
*/


int NOR_FFS_defrag(void)
{
  return -1;
}



#endif	// #ifdef NOR_FLASH_DISK_ID


⌨️ 快捷键说明

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