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

📄 extfiltr.c

📁 H3 M-system NAND flash driver in Linux OS, M-DOC driver
💻 C
📖 第 1 页 / 共 2 页
字号:
				return;			memset(pPart,0,sizeof(SectInfo));			pPart->pParent=pPT;			pPart->dwSector=*(pData+11);			pPart->dwSector<<=8;			pPart->dwSector+=*(pData+10);			pPart->dwSector<<=8;			pPart->dwSector+=*(pData+9);			pPart->dwSector<<=8;			pPart->dwSector+=*(pData+8);	#ifdef TFFS_DEBUG_DRIVER			if(pPT->no==0)				pPart->no=bPart+1;			else				if(pPT->no<4)					pPart->no=5;				else					pPart->no=pPT->no+1;	#endif			switch(*(pData+4))			{			case 5:			case 0xf:			case 0x85:				/* extended partition */				pPart->dwSector+=pPart->pParent->dwOffset;				pNextPT=pPart;			/* save next extended partition */				pPart->bSectors=1;		/* partition table */				pPart->bFlag=EXTF_PT;				pPart->dwOffset=pPT->dwOffset;				if(pPart->dwOffset==0)					pPart->dwOffset=pPart->dwSector;				AddSect(pDevice,pPart);				break;			default:				/* data partition */				pPart->dwSector+=pPart->pParent->dwSector+2;	/* offset from PT + superblock */				pPart->bSectors=1;		/* only first sector of superblock */				pPart->bFlag=EXTF_SB;				AddSect(pDevice,pPart);				/* add superblock */				PrintkDebug("AddPT %d: read sector %lu for SB",pPart->no,pPart->dwSector);				ExtReadSector(pDevice,pPart->dwSector,pPartData);				AddSB(pDevice,pPart,(struct ext2_super_block*)pPartData);				/* add group descriptors */				if(pPart->bFlag&EXTF_HAS_SB)				{					unsigned short wGroup;					for(wGroup=0;wGroup<pPart->wGroups;wGroup+=16)					{						PrintkDebug("AddPT %d: read sector %lu for GD",pPT->no,pPart->dwSector+2+(wGroup>>4));						ExtReadSector(pDevice,pPart->dwSector+(pPart->bSect2BlockShift==3?6:2)+(wGroup>>4),pPartData);						AddGD(pDevice,pPart,(struct ext2_group_desc*)pPartData,(wGroup>>4));					}				}			}			PrintkDebug("AddPT: part %d added, %lu",pPart->no,pPart->dwSector);		}		pPT=pNextPT;		if(pPT!=NULL)		{			PrintkDebug("AddPT %d: read sector %lu for next PT",pPT->no,pPT->dwSector);			ExtReadSector(pDevice,pPT->dwSector,pPTData);		}	}	kfree(pPTData);}void DelPT(DeviceInfo*pDevice,SectInfo*pPT){	SectInfo*pCurPT=pPT;	while(pCurPT!=NULL)	{		SectInfo*pCurSect=pCurPT->pNext,*pNextPT=NULL;;		PrintkDebug("DelPT: part %d",pCurPT->no);		while(pCurSect!=pCurPT)		{			if(pCurSect->pParent==pCurPT)			{				PrintkDebug("DelPT: part %d flag %d",pCurSect->no,pCurSect->bFlag);				switch(pCurSect->bFlag)				{				case EXTF_PT:				case EXTF_SB:					pCurSect=DelSect(pDevice,pCurSect);					break;				case EXTF_HAS_PT:					pNextPT=pCurSect;	/* save next PT */					pCurSect=pCurSect->pNext;					break;				case EXTF_HAS_SB:				case EXTF_HAS_GD:					DelSB(pDevice,pCurSect);					pCurSect=DelSect(pDevice,pCurSect);					break;#ifdef TFFS_DEBUG_DRIVER				default:					PrintkDebug("DelPT: wrong case");#endif				}			}			else				pCurSect=pCurSect->pNext;		}		if(pCurPT!=pPT)			DelSect(pDevice,pCurPT);		pCurPT=pNextPT;	}	pPT->bFlag=EXTF_PT;}void AddSB(DeviceInfo*pDevice,SectInfo*pSect,struct ext2_super_block*pSB){	unsigned short wGroup;	SectInfo*pBBSect;	PrintkDebug("AddSB %d: sector %lu",pSect->no,pSect->dwSector);	/* check that is superblock */	if( (le16_to_cpu(pSB->s_magic)!=0xef53) || (le32_to_cpu(pSB->s_creator_os)!=0) || (le32_to_cpu(pSB->s_rev_level)!=0 && le32_to_cpu(pSB->s_rev_level)!=1) )	{		PrintkDebug("AddSB: no superblock: magic 0x%x os %d, rev %d",le16_to_cpu(pSB->s_magic),le32_to_cpu(pSB->s_creator_os),le32_to_cpu(pSB->s_rev_level));		pSect->bFlag=EXTF_SB;		return;	}	pSect->bFlag=EXTF_HAS_SB;	pSect->dwExt3Flags=pSB->s_feature_compat;	pSect->bSect2BlockShift=le32_to_cpu(pSB->s_log_block_size)+1;	pSect->dwBlocksPerBitmap=le32_to_cpu(pSB->s_blocks_per_group);	/* calc no of groups */	pSect->wGroups=(le32_to_cpu(pSB->s_blocks_count)-le32_to_cpu(pSB->s_first_data_block))/le32_to_cpu(pSB->s_blocks_per_group);	if(pSect->wGroups*le32_to_cpu(pSB->s_blocks_per_group) < le32_to_cpu(pSB->s_blocks_count)-le32_to_cpu(pSB->s_first_data_block))		pSect->wGroups++;	PrintkDebug("AddSB %d: %u groups",pSect->no,pSect->wGroups);	pSect->bSectors=pSect->wGroups>>4;	if(((unsigned short)pSect->bSectors)<<4 < pSect->wGroups)		pSect->bSectors++;	pSect->bSectors+=pSect->bSect2BlockShift==3?6:2;	/* group descriptors sectors + 2 or 6 sect. of superblock */	pBBSect=KMalloc(pSect->wGroups*sizeof(SectInfo));	if(pBBSect==NULL)	{		PrintkDebug("AddSB: kmalloc failed");		return;	}	memset(pBBSect,0,pSect->wGroups*sizeof(SectInfo));	pSect->bFlag=EXTF_HAS_SB;	for(wGroup=0;wGroup<pSect->wGroups;wGroup++,pBBSect++)	{		pBBSect->pParent=pSect;		pBBSect->bFlag=EXTF_BB;		pBBSect->dwSector=pSect->dwSector+wGroup*((le32_to_cpu(pSB->s_blocks_per_group))<<pSect->bSect2BlockShift)+1;	/* the value shoul be updated in AddGD */#ifdef TFFS_DEBUG_DRIVER		pBBSect->no=pSect->no;#endif		if(wGroup<pSect->wGroups-1)		{			pBBSect->dwBlocksPerBitmap=le32_to_cpu(pSB->s_blocks_per_group);		}		else	/* last group */		{			pBBSect->dwBlocksPerBitmap=le32_to_cpu(pSB->s_blocks_count)-le32_to_cpu(pSB->s_first_data_block)-le32_to_cpu(pSB->s_blocks_per_group)*(pSect->wGroups-1);		}		pBBSect->bSectors=pBBSect->dwBlocksPerBitmap>>12;		if( ((pBBSect->dwBlocksPerBitmap>>12)<<12) != pBBSect->dwBlocksPerBitmap)			pBBSect->bSectors++;		pBBSect->dwFirstSectorOfBitmap=pSect->dwSector-2+((le32_to_cpu(pSB->s_first_data_block)+le32_to_cpu(pSB->s_blocks_per_group)*wGroup)<<pSect->bSect2BlockShift);		AddSect(pDevice,pBBSect);	}	PrintkDebug("SB %d added: sector %lu sectors %u",pSect->no,pSect->dwSector,pSect->bSectors);}void DelSB(DeviceInfo*pDevice,SectInfo*pPart){	SectInfo*pTmp=pPart->pNext;	PrintkDebug("DelSB part %d",pPart->no);	while(pTmp->bFlag&EXTF_HAS_BB || pTmp->bFlag&EXTF_BB)	{		UncacheSect(pDevice,pTmp);		pTmp=pTmp->pNext;	}	kfree(pPart->pNext);	pPart->pNext=pTmp;	pPart->bFlag=EXTF_SB;}void AddGD(DeviceInfo*pDevice,SectInfo*pSect,struct ext2_group_desc*pGD,unsigned char bOffset){	unsigned short wGroup,wGroups;	SectInfo*pCurSect=pSect->pNext;	PrintkDebug("AddGD: part %d offset %u",pSect->no,bOffset);	wGroups=(((unsigned short)bOffset)<<4);	while(wGroups)	{		pCurSect=pCurSect->pNext;		wGroups--;	}	wGroups=pSect->wGroups-(((unsigned short)bOffset)<<4);	if(wGroups>16)		wGroups=16;	for(wGroup=0;wGroup<wGroups;wGroup++,pCurSect=pCurSect->pNext)	{		UncacheSect(pDevice,pCurSect);		pCurSect->dwSector=((unsigned long)le32_to_cpu(pGD[wGroup].bg_block_bitmap)<<pSect->bSect2BlockShift)+pSect->dwSector-2;		CacheSect(pDevice,pCurSect);		pCurSect->bFlag=EXTF_HAS_BB;		PrintkDebug("AddGD: added group %d (%d) sect %lu",wGroup,wGroup+(bOffset<<4),pCurSect->dwSector);		PrintkDebug("AddGD: 0x%lx 0x%x 0x%lx",(unsigned long)le32_to_cpu(pGD[wGroup].bg_block_bitmap),pSect->bSect2BlockShift,pSect->dwSector);	}	/* set EXTF_HAS_GD flag to the parent sector, if it's last GD sector */	if(((bOffset+1)<<4)>=pSect->wGroups)	{		PrintkDebug("AddGD: end group descriptors");		pSect->bFlag=EXTF_HAS_GD;		pSect->bSectors=1;	}}void ProcessBB(DeviceInfo*pDevice,SectInfo*pSect,unsigned char*pOld,unsigned char*pNew,unsigned char bOffset){	unsigned long dwSectorShift=pSect->dwFirstSectorOfBitmap+(((unsigned long)bOffset)<<(pSect->pParent->bSect2BlockShift+12));	unsigned short wBlock;	unsigned short wLastBlockInSector=pSect->dwBlocksPerBitmap-(((unsigned short)bOffset)<<12);	/*	PrintkDebug("ProcessBB %d: %lu",pSect->no,pSect->dwSector); */	for(wBlock=0;wBlock<wLastBlockInSector;wBlock+=32)	{		unsigned long o=((unsigned long*)pOld)[wBlock>>5],n=((unsigned long*)pNew)[wBlock>>5];		if(o!=n)		{			unsigned char b;			for(b=0;b<32 && wBlock+b<wLastBlockInSector;b++)				if( (o&(1L<<b)) > (n&(1L<<b)) )				{					ExtDelSector(pDevice,((b+wBlock)<<pSect->pParent->bSect2BlockShift)+dwSectorShift,1<<pSect->pParent->bSect2BlockShift);				}		}	}}unsigned char ExtReadSector(DeviceInfo*pDevice,unsigned long dwSector,unsigned char*p){	IOreq ioreq;	ioreq.irHandle=pDevice->bHandle;	ioreq.irData=p;	ioreq.irLength=dwSector;	ioreq.irCount=1;	if(flAbsRead(&ioreq)==flOK)		return 1;	else	{		PrintkDebug("ExtReadSector->flAbsRead error");		return 0;	}}unsigned char ExtDelSector(DeviceInfo*pDevice,unsigned long dwSector,unsigned char bSectors){	IOreq ioreq;	PrintkDebug("ExtDelSector: del %lu %u",dwSector,bSectors);	ioreq.irHandle=pDevice->bHandle;	ioreq.irLength=dwSector;	ioreq.irCount=bSectors;#ifdef DRIVER_DEBUG	if(dwSector>=pDevice->dwSize)		PrintkDebug("ExtDelSector: achtung: wrong sector 0x%lx of 0x%lx",dwSector,pDevice->dwSize);#endif	if(flAbsDelete(&ioreq)!=flOK)	{		PrintkDebug("ExtDelSector:flAbsDelete fails");		return 0;	}	return 1;}void AddSect(DeviceInfo*pDevice,SectInfo*pSect){	SectInfo*pCurSect=&pDevice->sect[1];/*	PrintSect(pSect,"AddSect"); */	while(pCurSect->pNext!=&pDevice->sect[1] && pCurSect->pNext->dwSector<pSect->dwSector)		pCurSect=pCurSect->pNext;	pSect->pNext=pCurSect->pNext;	pSect->pPrev=pCurSect;	pCurSect->pNext->pPrev=pSect;	pCurSect->pNext=pSect;	CacheSect(pDevice,pSect);}SectInfo*DelSect(DeviceInfo*pDevice,SectInfo*pSect){	SectInfo*p=pSect->pNext;	PrintkDebug("DelSect: %lu flag 0x%x",pSect->dwSector,pSect->bFlag);	UncacheSect(pDevice,pSect);	pSect->pPrev->pNext=pSect->pNext;	pSect->pNext->pPrev=pSect->pPrev;	kfree(pSect);	return p;}void CacheSect(DeviceInfo*pDevice,SectInfo*pSect){	unsigned short wCache=pSect->dwSector>>TFFS_EXTF_CACHE_STEP;/*	PrintkDebug("CacheSect %lu to %lu",pSect->dwSector,pSect->dwSector>>TFFS_EXTF_CACHE_STEP); */		while(pDevice->pCache[wCache]==pSect->pNext)	{/*		PrintkDebug("CacheSect: pCache[%u]=%lu",wCache,pSect->dwSector); */		pDevice->pCache[wCache]=pSect;		wCache--;	}}void UncacheSect(DeviceInfo*pDevice,SectInfo*pSect){	unsigned short wCache=pSect->dwSector>>TFFS_EXTF_CACHE_STEP;/*	PrintkDebug("UncacheSect %lu to %lu",pSect->dwSector,pSect->dwSector>>TFFS_EXTF_CACHE_STEP); */	while(pDevice->pCache[wCache]==pSect)	{/*		PrintkDebug("UncacheSect: pCache[%u]=%lu",wCache,pSect->pNext->dwSector); */		pDevice->pCache[wCache]=pSect->pNext;		wCache--;	}}

⌨️ 快捷键说明

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