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

📄 fsmplr.c

📁 norflash的文件系统。 用于中低端手机开发的参考
💻 C
📖 第 1 页 / 共 5 页
字号:
		if(find_end == 0)
			return ERR_FULL;			
	}		

	#ifdef DEBUG_FSM
	MonPrintf("Exit Interface DfsPlrScanSector successfully!\n");
	#endif

	return ERR_NONE;
}

/*#############################################################################
  ### DfsPlrSecUpdating
  ###
  ### DESCRIPTION:
  ###   Search and invalidate old sector with the same lsn.
  ###	Modify corresponding value in Psn2Lsn map.
  ###	Validate the sector in SECTOR_UPDATING status.
  ###
  ### PARAMETERS:
  ###    IN:  Psn - psn of the sector in SECTOR_UPDATING status.
  ###	      Psn2LsnP - pointer to the Psn2Lsn map.
  ###	      pDev - poiter to designated FsmDevObjHdrT of data file system.
  ###	      block_size - flash block size.
  ###	      block_num - flash block number.
  ###    OUT: Psn2Lsn - corresponding lsn number of old sector psn is DIRTYLSN
  ###
  ### RETURNS:
  ###   If read operation is not successful,  the function returns ERR_READ.
  ###	If write operation is not successful,  the funciton returns ERR_WRITE.
  ###	If old sector found is not in correct status, the function returns ERR_EXIST.
  ###	If desigated sector is not found, the function returns ERR_NOTEXIST.
  ###   Otherwise, the function returns ERR_NONE.
  ###
 */

static uint32 DfsPlrSecUpdating(
	uint32		Psn, 
	uint32 *	Psn2LsnP,
	FsmDevObjHdrT * pDev,
	uint32		block_size, 
	uint32		block_num
	)
{
	uint16		block_sectors;
	uint32		sector_size;
	uint32		max_psn;

	uint32		Lsn;

	uint32		updating_sector_addr;
	uint32		discard_sector_addr;

	uint32		discard_psn;

	uint32		hw_status;
	uint8		end_flag;
	uint16		sector_status;

	DEV_WRITE_FUNCPTR	DevWrite = pDev->DevDrvP->FsmDevWrite;
	DEV_READ_FUNCPTR	DevRead  = pDev->DevDrvP->FsmDevRead;


	#ifdef DEBUG_FSM
	MonPrintf("Enter Interface DfsPlrSecUpdating!\n");
	#endif

	block_sectors = ((FsmFlashMediaT *)(pDev->MediaObjP))->SectorsPerBlock;
	sector_size   = ((FsmFlashMediaT *)(pDev->MediaObjP))->SectorSize;
	max_psn       = ((FsmFlashMediaT *)(pDev->MediaObjP))->MaxPsn;

	updating_sector_addr = DfsGetAddr(Psn, block_sectors, block_size, sector_size);

	Lsn = Psn2LsnP[Psn];

	end_flag = 0;	

	for(discard_psn = 0; discard_psn < max_psn; discard_psn++)
	{
		if(Psn2LsnP[discard_psn] == Lsn)
		{
			if(discard_psn == Psn)
				continue;
			else
			{
				end_flag = 1;
				break;
			}
		}
	}

	if(end_flag == 0)
		return ERR_NOTEXIST;

	discard_sector_addr = DfsGetAddr(discard_psn, block_sectors, block_size, sector_size);

	hw_status = DevRead(pDev, 
						(uint8 *)&sector_status, 
						discard_sector_addr + FIELD_OFFSET(FsmFlashSectorHdrT, Status), 
						FIELD_SIZE(FsmFlashSectorHdrT, Status)
						);

	if(hw_status != HW_ERR_NONE)
		return ERR_READ;

	if(sector_status == SECTOR_VALID)
	{
		sector_status = SECTOR_DELETING;

		hw_status = DevWrite(pDev, 
							 (uint8 *)&sector_status, 
							 discard_sector_addr + FIELD_OFFSET(FsmFlashSectorHdrT, Status), 
							 FIELD_SIZE(FsmFlashSectorHdrT, Status)
							 );

		if(hw_status != HW_ERR_NONE)
			return ERR_WRITE;
	}
	else if(sector_status != SECTOR_DELETING)
		return ERR_EXIST;

	sector_status = SECTOR_VALID;

	hw_status = DevWrite(pDev, 
						 (uint8 *)&sector_status, 
						 updating_sector_addr + FIELD_OFFSET(FsmFlashSectorHdrT, Status), 
						 FIELD_SIZE(FsmFlashSectorHdrT, Status)
						 );

	if(hw_status != HW_ERR_NONE)
		return ERR_WRITE;

	sector_status = SECTOR_INVALID;

	hw_status = DevWrite(pDev, 
						 (uint8 *)&sector_status, 
						 discard_sector_addr + FIELD_OFFSET(FsmFlashSectorHdrT, Status), 
						 FIELD_SIZE(FsmFlashSectorHdrT, Status)
						 );

	if(hw_status != HW_ERR_NONE)
		return ERR_WRITE;

	Psn2LsnP[discard_psn] = DIRTYLSN;

	#ifdef DEBUG_FSM
	MonPrintf("Exit Interface DfsPlrSecUpdating successfully!\n");
	#endif

	return ERR_NONE;	
}


#if 0

/*#############################################################################
  ### DfsPlrEntrySecAppending
  ###
  ### DESCRIPTION:
  ###   Validate the sector linked to this sector.
  ###	Invalidate the old sector with the same lsn and modify corresponding
  ###	Psn2Lsn member value.
  ###	Change sector status from SECTOR_APPENDING to SECTOR_VALID.
  ###
  ### PARAMETERS:
  ###    IN:  Psn - psn of the sector in SECTOR_APPENDING status and containing dir entries.
  ###	      Psn2LsnP - pointer to the Psn2Lsn map.
  ###	      pDev - poiter to designated FsmDevObjHdrT of data file system.
  ###	      block_size - flash block size.
  ###	      block_num - flash block number.
  ###    OUT: Psn2Lsn - corresponding lsn number of old sector psn is DIRTYLSN
  ###
  ### RETURNS:
  ###   If read operation is not successful,  the function returns ERR_READ.
  ###	If write operation is not successful, the funciton returns ERR_WRITE.
  ###	If the designated sector found is not in correct status, the function returns ERR_EXIST.
  ###	If the designated sector is not found, the function returns ERR_NOTEXIST.
  ###   Otherwise, the function returns ERR_NONE.
  ###
 */

static uint32 DfsPlrEntrySecAppending(
	uint32		Psn, 
	uint32 *	Psn2LsnP, 
	FsmDevObjHdrT * pDev, 
	uint32		block_size, 
	uint32		block_num
	)
{
	uint16		block_sectors;
	uint32		sector_size;
	uint32		max_psn;

	uint32		Lsn;

	uint32		appending_sector_addr;
	uint32		allocate_sector_addr;
	uint32		discard_sectorc_addr;

	uint32		allocate_psn;
	uint32		discard_psn;

	uint32		hw_status;
	uint8		end_flag;
	uint16		sector_status;

	DEV_WRITE_FUNCPTR	DevWrite = pDev->DevDrvP->FsmDevWrite;
	DEV_READ_FUNCPTR	DevRead  = pDev->DevDrvP->FsmDevRead;


	#ifdef DEBUG_FSM
	MonPrintf("Enter Interface DfsPlrEntrySecAppending!\n");
	#endif

	block_sectors = ((FsmFlashMediaT *)(pDev->MediaObjP))->SectorsPerBlock;
	sector_size   = ((FsmFlashMediaT *)(pDev->MediaObjP))->SectorSize;
	max_psn       = ((FsmFlashMediaT *)(pDev->MediaObjP))->MaxPsn;

	appending_sector_addr = DfsGetAddr(Psn, block_sectors, block_size, sector_size);

	Lsn = Psn2LsnP[Psn];

	end_flag = 0;	

	/* search old sector with the same lsn. */
	for(discard_psn = 0; discard_psn < max_psn; discard_psn++)
	{
		if(Psn2LsnP[discard_psn] == Lsn)
		{
			if(discard_psn == Psn)
				continue;
			else
			{
				end_flag = 1;
				break;
			}
		}
	}

	if(end_flag == 0)
		return ERR_NOTEXIST;

	/* change old sector with the same lsn to SECTOR_DELETING status. */
	discard_sectorc_addr = DfsGetAddr(discard_psn, block_sectors, block_size, sector_size);

	hw_status = DevRead(pDev, 
						(uint8 *)&sector_status, 
						discard_sectorc_addr + FIELD_OFFSET(FsmFlashSectorHdrT, Status),
						FIELD_SIZE(FsmFlashSectorHdrT, Status)
						);

	if(hw_status != HW_ERR_NONE)
		return ERR_READ;

	if(sector_status == SECTOR_VALID)
	{
		sector_status = SECTOR_DELETING;

		hw_status = DevWrite(pDev, 
							 (uint8 *)&sector_status, 
							 discard_sectorc_addr + FIELD_OFFSET(FsmFlashSectorHdrT, Status), 
							 FIELD_SIZE(FsmFlashSectorHdrT, Status)
							 );

		if(hw_status != HW_ERR_NONE)
			return ERR_WRITE;
	}
	else if(sector_status != SECTOR_DELETING)
		return ERR_EXIST;

	/* search sector linked to the sector in SECTOR_APPENDING status. */
	hw_status = DevRead(pDev, 
						(uint8 *)&Lsn, 
						appending_sector_addr + FIELD_OFFSET(FsmFlashSectorHdrT, NextLsn), 
						FIELD_SIZE(FsmFlashSectorHdrT, NextLsn)
						);

	if(hw_status != HW_ERR_NONE)
		return ERR_READ;

	if(Lsn != (uint32)(-1))
	{
		end_flag = 0;

		for(allocate_psn = 0; allocate_psn < max_psn; allocate_psn++)
		{
			if(Psn2LsnP[allocate_psn] == Lsn)
			{
				end_flag = 1;
				break;
			}
		}

		if(end_flag == 0)
			return ERR_NOTEXIST;

		/* set the sector found in link to SECTOR_VALID status. */
		allocate_sector_addr = DfsGetAddr(allocate_psn, block_sectors, block_size, sector_size);	

		hw_status = DevRead(pDev, 
							(uint8 *)&sector_status, 
							allocate_sector_addr + FIELD_OFFSET(FsmFlashSectorHdrT, Status), 
							FIELD_SIZE(FsmFlashSectorHdrT, Status)
							);

		if(hw_status != HW_ERR_NONE)
			return ERR_READ;

		if(sector_status == SECTOR_ALLOCATED)
		{
			sector_status = SECTOR_VALID;

			hw_status = DevWrite(pDev, 
								 (uint8 *)&sector_status, 
								 allocate_sector_addr + FIELD_OFFSET(FsmFlashSectorHdrT, Status), 
								 FIELD_SIZE(FsmFlashSectorHdrT, Status)
								 );

			if(hw_status != HW_ERR_NONE)
				return ERR_WRITE;
		}
		else if(sector_status != SECTOR_VALID)
			return ERR_EXIST;	
	}

	/* change sector status from SECTOR_APPENDING to SECTOR_VALID. */
	sector_status = SECTOR_VALID;

	hw_status = DevWrite(pDev, 
						 (uint8 *)&sector_status, 
						 appending_sector_addr + FIELD_OFFSET(FsmFlashSectorHdrT, Status), 
						 FIELD_SIZE(FsmFlashSectorHdrT, Status)
						 );

	if(hw_status != HW_ERR_NONE)
		return ERR_WRITE;

	/* set old sector status to SECTOR_INVALID. */
	sector_status = SECTOR_INVALID;

	hw_status = DevWrite(pDev, 
						 (uint8 *)&sector_status, 
						 discard_sectorc_addr + FIELD_OFFSET(FsmFlashSectorHdrT, Status), 
						 FIELD_SIZE(FsmFlashSectorHdrT, Status)
						 );

	if(hw_status != HW_ERR_NONE)
		return ERR_WRITE;

	Psn2LsnP[discard_psn] = DIRTYLSN;	

	#ifdef DEBUG_FSM
	MonPrintf("Exit Interface DfsPlrEntrySecAppending successfully!\n");
	#endif

	return ERR_NONE;	
}

#endif


/*#############################################################################
  ### DfsPlrEntryAllocated
  ###
  ### DESCRIPTION:
  ###   Validate all the sectors linked to this entry.
  ###
  ### PARAMETERS:
  ###    IN:  first_lsn - first sector lsn linked to the directory entry in 
  ###			ENTRY_ALLOCATED status.
  ###	      Psn2LsnP - pointer to the Psn2Lsn map.
  ###	      pDev - poiter to designated FsmDevObjHdrT of data file system.
  ###	      block_size - flash block size.
  ###	      block_num - flash block number.
  ###    OUT: 
  ###
  ### RETURNS:
  ###   If read operation is not successful,  the function returns ERR_READ.
  ###	If write operation is not successful, the funciton returns ERR_WRITE.
  ###	If the designated sector found is not in correct status, the function returns ERR_EXIST.
  ###	If the designated sector is not found, the function returns ERR_NOTEXIST.
  ###   Otherwise, the function returns ERR_NONE.
  ###
 */

static uint32 DfsPlrEntryAllocated(
	uint32		first_lsn, 
	uint32 *	Psn2LsnP, 
	FsmDevObjHdrT* pDev, 
	uint32		block_size, 
	uint32		block_num
	)
{
	uint16		block_sectors;
	uint32		sector_size;
	uint32		max_psn;

	uint32		Psn;
	uint32		Lsn;	
	uint16		sector_status;
	uint32		sector_addr;

	uint8		end_flag;

	uint32		hw_status;

	DEV_WRITE_FUNCPTR	DevWrite = pDev->DevDrvP->FsmDevWrite;
	DEV_READ_FUNCPTR	DevRead  = pDev->DevDrvP->FsmDevRead;


	#ifdef DEBUG_FSM
	MonPrintf("Enter Interface DfsPlrEntryAllocated!\n");
	#endif

	block_sectors = ((FsmFlashMediaT *)(pDev->MediaObjP))->SectorsPerBlock;
	sector_size   = ((FsmFlashMediaT *)(pDev->MediaObjP))->SectorSize;
	max_psn       = ((FsmFlashMediaT *)(pDev->MediaObjP))->MaxPsn;

	Lsn = first_lsn;

	end_flag = 0;

	while(Lsn != (uint32)(-1))
	{
		end_flag = 0;

		/* search the sector in link. */
		for(Psn = 0; Psn < max_psn; Psn++)
		{
			if(Psn2LsnP[Psn] == Lsn)
			{
				end_flag = 1;
				break;
			}
		}

		if(end_flag == 0)
			return ERR_NOTEXIST;

		/* read sector status. */
		sector_addr = DfsGetAddr(Psn, block_sectors, block_size, sector_size);

		hw_status = DevRead(pDev, 
							(uint8 *)&sector_status, 
							sector_addr + FIELD_OFFSET(FsmFlashSectorHdrT, Status), 
							FIELD_SIZE(FsmFlashSectorHdrT, Status)
							);

		if(hw_status != HW_ERR_NONE)
			return ERR_READ;

		if(sector_status == SECTOR_ALLOCATED)
		{
			/* modify sector status to SECTOR_VALID. */
			sector_status = SECTOR_VALID;

			hw_status = DevWrite(pDev, 
								 (uint8 *)&sector_status, 
								 sector_addr + FIELD_OFFSET(FsmFlashSectorHdrT, Status), 
								 FIELD_SIZE(FsmFlashSectorHdrT, Status)
								 );

			if(hw_status != HW_ERR_NONE)
				return ERR_WRITE;
		}
		else if(sector_status != SECTOR_VALID)
			return ERR_EXIST;

		/* read nextlsn for searching the next sector in link. */
		hw_status = DevRead(pDev, 
							(uint8 *)&Lsn, 
							sector_addr + FIELD_OFFSET(FsmFlashSectorHdrT, NextLsn), 
							FIELD_SIZE(FsmFlashSectorHdrT, NextLsn)
							);

		if(hw_status != HW_ERR_NONE)
			return ERR_READ;
	}

	#ifdef DEBUG_FSM
	MonPrintf("Exit Interface DfsPlrEntryAllocated successfully!\n");
	#endif

	return ERR_NONE;
}

/*#############################################################################
  ### DfsPlrEntryDeleting
  ###
  ### DESCRIPTION:
  ###   Invalidate all sectors linked to this entry.
  ###	Change entry status to ENTRY_INVALID.
  ###
  ### PARAMETERS:
  ###    IN:  first_lsn - first sector lsn linked to the directory entry in 
  ###			ENTRY_ALLOCATED status.
  ###	      entry_sector_psn - psn of the sector containing the entry in ENTRY_DELETING
  ###			status.
  ###	      entry_num - the number of the entry in sector.
  ###	      Psn2LsnP - pointer to the Psn2Lsn map.
  ###	      pDev - poiter to designated FsmDevObjHdrT of data file system.
  ###	      block_size - flash block size.
  ###	      block_num - flash block number.
  ###    OUT: 
  ###
  ### RETURNS:
  ###   If read operation is not successful,  the function returns ERR_READ.
  ###	If write operation is not successful, the funciton returns ERR_WRITE.
  ###	If the designated sector found is not in correct status, the function returns ERR_EXIST.

⌨️ 快捷键说明

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