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

📄 flashfslib.c

📁 这是micrel公司宽带路由ARM9芯片的VXWORKS BSP 源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
* the FLASH disk will then be preserved.
*
* These same procedures apply when creating a FLASH disk with rt11Fs using
* rt11FsDevInit() and rt11FsMkfs(), or creating a FLASH disk with rawFs using
* rawFsDevInit().
*
* RETURNS:
* A pointer to a block device structure (BLK_DEV) or NULL if memory cannot
* be allocated for the device structure or for the FLASH disk.
*
* SEE ALSO: dosFsMkfs(), dosFsDevInit(), rt11FsDevInit(), rt11FsMkfs(),
* rawFsDevInit()
*/

LOCAL BLK_DEV *flashDevCreate
    (
    UINT32	flashOffset,    /* where it is in flash */
    FAST int	bytesPerBlk,	/* number of bytes per block */
    int		nBlocks,	/* number of blocks on this device */
    int		blkOffset	/* no. of blks to skip at start of device */
    )
{
    UINT32        largestSect;
    FAST FLASH_DEV	*pFlashDev;	/* ptr to created FLASH_DEV struct */
    FAST BLK_DEV	*pBlkDev;	/* ptr to BLK_DEV struct in FLASH_DEV */
    
    pBlkDev     = NULL;
    pFlashDev   = NULL;	/* ptr to created FLASH_DEV struct */


	FS_DRV_PRINT (FLASHFS_DEBUG_INIT,
	          ("flashDevCreate(): flashOffset=0x%x, bytesPerBlk=%d, nBlocks=%d, blkOffset=0x%x\n",
		                                (int)flashOffset, (int)bytesPerBlk, (int)nBlocks, (int)blkOffset));

    /* Set up defaults for any values not specified */

    if (bytesPerBlk == 0)
	    bytesPerBlk = FLASH_FS_DOS_SECTOR_SIZE;


    /* Scan to flash device sectors to find the area reserved, and note the */
    /* largest sector to allocate the sector write cache */

    largestSect = FLASH_BANK_SECTOR_SIZE;

    /* If the requested device will fit in flash */ 
    if (largestSect)
    {
        /* Allocate a FLASH_DEV structure for device */
        pFlashDev = (FLASH_DEV *) calloc (1, sizeof (FLASH_DEV));
        
        /* If memory available, create device */
        if (pFlashDev != NULL)
        {

           /* Done in flashDrvInit */
            /* Allocate cache for writing to a flash sector */
          /* pFlashDev->flash_cache = (char *)malloc(largestSect); */

            
            /* Initialize BLK_DEV structure (in FLASH_DEV) */
            pBlkDev = &pFlashDev->flash_blkdev;
    
            pBlkDev->bd_nBlocks      = nBlocks;		/* number of blocks */
            pBlkDev->bd_bytesPerBlk  = bytesPerBlk;	/* bytes per block */
            pBlkDev->bd_blksPerTrack = nBlocks;	    /* blocks per track */
    
            pBlkDev->bd_nHeads       = 1;		    /* one "head" */
            pBlkDev->bd_removable    = FALSE;		/* not removable */
            pBlkDev->bd_retry	     = 1;		    /* retry count */
            pBlkDev->bd_mode	     = O_RDWR;		/* initial mode for device */
            pBlkDev->bd_readyChanged = TRUE;		/* new ready status */
    
            pBlkDev->bd_blkRd	     = flashFsBlkRead;	/* read block function */
            pBlkDev->bd_blkWrt	     = flashFsBlkWrite;	/* write block function */
            pBlkDev->bd_ioctl	     = flashFsIoctl;	/* ioctl function */
            pBlkDev->bd_reset	     = NULL;		/* no reset function */
            pBlkDev->bd_statusChk    = NULL;		/* no check-status function */
            
            /* Initialize remainder of device struct */
    
            pFlashDev->flash_offset = flashOffset;  /* Start offset */
            pFlashDev->flash_blkOffset = blkOffset;	/* block offset */
        }
    }
    else
		FS_DRV_PRINT (FLASHFS_DEBUG_ERROR,
                  ("flashFsRfaConfig(): %s:%d Sector Count invalid\n", __FILE__, __LINE__));

    return (pBlkDev);
}



/******************************************************************************
*
* flashFsBlkRead - Block read routine.
*
* This routine is shared by the RFAs.  Flash offset is used to distinguish reads  
* between the RFAs. 
*
* RETURNS: OK if operation succeeds, ERROR otherwise.
*/


STATUS flashFsBlkRead (FLASH_DEV *pFlashDev, int startBlk, int numBlks, char *pBuf)
{
    int	blkIndx, phySectorNum, offset;


	FS_DRV_PRINT (FLASHFS_DEBUG_BLKREAD,
		      ( "flashFsBlkRead(): startBlk = %d, numBlks = %d\n", (int)startBlk, (int)numBlks ));

    /* Add in the block offset */
    startBlk += pFlashDev->flash_blkOffset;

    /* Calculate flash offset */
    startBlk += (pFlashDev->flash_offset/FLASH_FS_DOS_SECTOR_SIZE);

	FS_DRV_PRINT (FLASHFS_DEBUG_BLKREAD,
		      ( "flashFsBlkRead(): pFlashDev->flash_blkOffset = 0x%x, pFlashDev->flash_offset = 0x%x, startBlk = 0x%x\n", 
			   (int)pFlashDev->flash_blkOffset, (int)pFlashDev->flash_offset, (int)startBlk ));


    for( blkIndx = 0; blkIndx < numBlks; blkIndx++ ) 
	{
		if( flashFsGetPhys( (startBlk + blkIndx), &phySectorNum, &offset ) == ERROR ) {
			FS_DRV_PRINT (FLASHFS_DEBUG_ERROR,
	              ( "flashFsBlkRead(): flashFsGetPhys() failed\n" ));
			return( ERROR );
		}

	    FS_DRV_PRINT (FLASHFS_DEBUG_BLKREAD,
		      ( "flashFsBlkRead(): phySectorNum = 0x%x, offset = 0x%x\n", (int)phySectorNum, (int)offset ));

		/*
		d ( (void *) (FLASH_BASE_ADDRESS + (phySectorNum * FLASH_BANK_SECTOR_SIZE) + offset), FLASH_FS_DOS_SECTOR_SIZE, 1);
		*/

		if( flashBlkRead( phySectorNum, pBuf, offset, FLASH_FS_DOS_SECTOR_SIZE ) == ERROR ) {
			FS_DRV_PRINT (FLASHFS_DEBUG_ERROR,
	              ( "flashFsBlkRead(): flashBlkRead() failed\n" ));
			return( ERROR );
		}

		/*
		d ( (void *) pBuf, FLASH_FS_DOS_SECTOR_SIZE, 1);
		*/

		pBuf += FLASH_FS_DOS_SECTOR_SIZE;

    }

    return( OK );
   
}


/******************************************************************************
*
* flashFsBlkWrite - Block write routine.
*
* This routine is shared by the RFAs.  Flash offset is used to distinguish writes  
* between the RFAs.  flashSyncFilesystem is called at the exit of the function to 
* flush the flash buffer to flash part. 
*
* RETURNS: OK if operation succeeds, ERROR otherwise.
*/


STATUS flashFsBlkWrite(FLASH_DEV  *pFlashDev, int startBlk, int numBlks, char *pBuf)
{
    int	blkIndx, phySectorNum, offset;


	FS_DRV_PRINT (FLASHFS_DEBUG_BLKWRITE,
	          ( "flashFsBlkWrite(): startBlk = %d, numBlks = %d\n",	(int)startBlk, (int)numBlks ));

    /* Add in the block offset */
    startBlk += pFlashDev->flash_blkOffset;
    
    /* Calculate flash offset */
    startBlk += (pFlashDev->flash_offset/FLASH_FS_DOS_SECTOR_SIZE);

	FS_DRV_PRINT (FLASHFS_DEBUG_BLKWRITE,
		      ( "flashFsBlkWrite(): pFlashDev->flash_blkOffset = 0x%x, pFlashDev->flash_offset = 0x%x, startBlk = 0x%x\n", 
			   (int)pFlashDev->flash_blkOffset, (int)pFlashDev->flash_offset, (int)startBlk ));

 
    for( blkIndx = 0; blkIndx < numBlks; blkIndx++ ) 
	{
		if( flashFsGetPhys( (startBlk + blkIndx), &phySectorNum, &offset ) == ERROR ) {

			FS_DRV_PRINT (FLASHFS_DEBUG_ERROR,
	              ( "flashFsBlkWrite(): flashFsGetPhys() failed. the startblk is %x, the blkindex is %x\n", (int)startBlk, (int)blkIndx));
			return( ERROR );
		}

		/*
		d ( (void *) pBuf, FLASH_FS_DOS_SECTOR_SIZE, 1);
		*/

		if( flashBlkWrite( phySectorNum, pBuf, offset, FLASH_FS_DOS_SECTOR_SIZE ) == ERROR ) {
			FS_DRV_PRINT (FLASHFS_DEBUG_ERROR,
	              ( "flashFsBlkWrite(): flashBlkWrite() failed\n" ));
			return( ERROR );
		}

	    FS_DRV_PRINT (FLASHFS_DEBUG_BLKWRITE,
		      ( "flashFsBlkWrite(): phySectorNum = 0x%x, offset = 0x%x\n", (int)phySectorNum, (int)offset ));

		/*
		d ( (void *) (FLASH_BASE_ADDRESS + (phySectorNum * FLASH_BANK_SECTOR_SIZE) + offset), FLASH_FS_DOS_SECTOR_SIZE, 1);
		*/

		pBuf += FLASH_FS_DOS_SECTOR_SIZE;

    }

	flashSyncFilesystem ();
    return( OK );
}



/******************************************************************************
*
* flashFsIoctl - Ioctl routines.
*  
* Currently disk format and files sytem sync are supported. 
*
* RETURNS: OK.
*/


STATUS flashFsIoctl(BLK_DEV *pDev, int funcCode, int arg)
{

	FS_DRV_PRINT (FLASHFS_DEBUG_IOCTL,
	          ( "flashFsBlkIoctl(): called, funCode=%d\n",  (int)funcCode));

    switch (funcCode)
    {
		case FIODISKFORMAT:
			flashEraseBank();
			FS_DRV_PRINT (FLASHFS_DEBUG_IOCTL,
                  ("flashFsBlkIoctl(): I am here erasing awayt\n"));
			break;
   
		case FLASH_FS_SYNC:
			flashSyncFilesystem();
			break;

		default:
			FS_DRV_PRINT (FLASHFS_DEBUG_ERROR,
                  ("flashFsBlkIoctl(): UNKNOW function code\n"));
			errnoSet( S_ioLib_UNKNOWN_REQUEST );
			return( ERROR );
			break;
    }

    return( OK );
}




/* sal lib needs it */

STATUS flashFsSync()
{
 flashSyncFilesystem();
 return 0;
}



/******************************************************************************
*
* flashFsGetPhys - Determines the physical sector and offset.
*
* This routine takes the blknum from dosFs and translates it into physical sector
* number and offset.  The returned value is used by flashBlkWrite.
*
* RETURNS: OK.
*/


STATUS flashFsGetPhys( int blkNum, int *sectorNum, int *offset )
{

    *sectorNum = (blkNum / (FLASH_BANK_SECTOR_SIZE / FLASH_FS_DOS_SECTOR_SIZE));
    *offset = ((blkNum % (FLASH_BANK_SECTOR_SIZE / FLASH_FS_DOS_SECTOR_SIZE)) \
				* FLASH_FS_DOS_SECTOR_SIZE);

	FS_DRV_PRINT (FLASHFS_DEBUG_GETPHYS,
		      ("flashFsGetPhys(): blkNum = %d, *sectorNum = %d, *offset = 0x%08X\n",
				(int)blkNum, (int)*sectorNum, (int)*offset ));

    return( OK );
}

⌨️ 快捷键说明

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