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

📄 flashfslib.c

📁 大名鼎鼎的mpc8260的bsp源代码
💻 C
📖 第 1 页 / 共 2 页
字号:
/* flashFsLib.c - Flash File System library */

/*
 * Vooha Mousse Board flash filesystem driver
 * by Curt McDowell, 08-06-99, Broadcom Corp.
 */
/* Copyright 1998-2000 Wind River Systems, Inc. */
/* NOMANUAL */

/* Modification of TPOS
02c,09apr03,lyx   add lightLoop here
02b,04apr03,lyx   add fs for bootrom, and add flashFsRead for debug
02a,27mar03,lyx  Leave rawfs and dosfs 
20021102	删除了若干仅用于调试的printf()  ,并修正了printf  的调用,
printf()  函数内不能再加括号,如
 printf(("enter flashFsBlkWrite1, startBlk=%d, numblks=%d\n", startBlk, numBlks));
的调用会引起死机,估计是因为printf  无法识别出参数.
*/

#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include "vxWorks.h"
#include "dosFsLib.h"
#include "rawFsLib.h"
#include "fioLib.h"
#include "blkIo.h"
#include "flashLib.h"
#include "flashFsLib.h"
#include "errnoLib.h"
#include "config.h"

#if FALSE
#define PRIVATE			static
#else
#define PRIVATE
#endif

/* Configuration for the raw file system used for
   storing vxWorks.z */
BLK_DEV		     flashBlkDev0;
RAW_VOL_DESC  *  flashRawVolDesc0;

/* configuration for the DOS filesystems NVM*/

BLK_DEV		     flashBlkDev1;
DOS_VOL_CONFIG	 flashVolConfig1;
DOS_VOL_DESC  *  flashDosVolDesc1;

/* Configuration for the raw file system used for
   storing bootrom */
BLK_DEV		     flashBlkDev2;
RAW_VOL_DESC  *  flashRawVolDesc2;

/* configuration for the DOS filesystems NVM backup*/

BLK_DEV		     flashBlkDev3;
DOS_VOL_CONFIG	 flashVolConfig3;
DOS_VOL_DESC  *  flashDosVolDesc3;

extern STATUS 	diskInit (char *devName);
extern STATUS 	diskFormat (char *devName);



typedef struct flash_vol_s {
    flash_dev_t	   *   dev;
    int			       blocks;		  /* Number of DOS blocks on volume */
    int			       lgSectorSize;  /* Copied from flash device */
    void	       *   bufData;	      /* Buffered write sector, 0 if none */
    int			       bufSector;	  /* Buffered data sector number */
} flash_vol_t;

/* RawFs used for storing vxworks application */
flash_vol_t flashVol0[] = {
    {	FLASH_DEV_BANK_SA1	}
};





int flashVolCount0 = sizeof (flashVol0) / sizeof (flashVol0[0]);




#define	RAW_SECTOR_SIZE_LG	9
#define	RAW_SECTOR_SIZE		(1 << RAW_SECTOR_SIZE_LG) /* 512 */

#define VERBOSE_BLKFILL		0x0001
#define VERBOSE_BLKFLUSH	0x0002
#define VERBOSE_BLKINVAL	0x0004
#define VERBOSE_FLASHES		0x0008
#define VERBOSE_GETPHYS		0x0010
#define VERBOSE_BLKREAD		0x0020
#define VERBOSE_BLKWRITE	0x0040
#define VERBOSE_IOCTL		0x0080
#define VERBOSE_SYNC		0x0100

int flashFsVerbose = 0; 

int flashFsTotalBlocks0 = 0;






PRIVATE STATUS flashFsBlkInvalAll0(void)
{
    int			volnum;

    if (flashFsVerbose & VERBOSE_BLKINVAL)
	    printf("flashFsBlkInvalAll0: entered\n");

    for (volnum = 0; volnum < flashVolCount0; volnum++) {
	flash_vol_t	*vol = &flashVol0[volnum];

	if (vol->bufData != 0) {
	    free(vol->bufData);
	    vol->bufData = 0;
	}
    }

    return OK;
}


PRIVATE STATUS flashFsBlkFill0(int volnum, int sector)
{
    flash_vol_t		*vol = &flashVol0[volnum];

    if (flashFsVerbose & VERBOSE_BLKFILL)
	    printf("flashFsBlkFill0: vol=%d, sector=%d\n", volnum, sector);

    if (vol->bufData)
	return ERROR;

    vol->bufData = malloc(1 << vol->lgSectorSize);

    if (vol->bufData == 0) {
	    printf("flashFsBlkFill0: Out of memory\n");
	    return ERROR;
    }

    if (flashRead(vol->dev,
		  sector << vol->lgSectorSize,
		  vol->bufData,
		  1 << vol->lgSectorSize) == ERROR)
	    return ERROR;

    vol->bufSector = sector;

    if (flashFsVerbose & VERBOSE_BLKFILL)
	    printf("flashFsBlkFill0: rv=OK\n");

    return OK;
}

PRIVATE STATUS flashFsBlkFlush0(int volnum)
{
    flash_vol_t		*vol = &flashVol0[volnum];

    if (flashFsVerbose & VERBOSE_BLKFLUSH)
	    printf("flashFsBlkFlush0: vol=%d\n", volnum);

    if (vol->bufData != 0) {
	    if (flashFsVerbose & VERBOSE_FLASHES)
	        printf("flashFsBlkFlush0: erase vol=%d sector=%d\n",
		           volnum, vol->bufSector);

	    if (flashEraseSector(vol->dev,
			     vol->bufSector) == ERROR)
	        return ERROR;

	    if (flashWrite(vol->dev,
		       vol->bufSector << vol->lgSectorSize,
		       vol->bufData,
		       1 << vol->lgSectorSize) == ERROR)
	        return ERROR;

	    free(vol->bufData);
	    vol->bufData = 0;
    }

    if (flashFsVerbose & VERBOSE_BLKFLUSH)
	    printf("flashFsBlkFlush0: rv=OK\n");

    return OK;
}



PRIVATE STATUS flashFsBlkFlushAll0(void)
{
    int		volnum;

    if (flashFsVerbose & VERBOSE_BLKFLUSH)
	    printf("flashFsBlkFlushAll0: entered\n");

    for (volnum = 0; volnum < flashVolCount0; volnum++)
	    if (flashFsBlkFlush0(volnum) == ERROR)
	        return ERROR;

    if (flashFsVerbose & VERBOSE_BLKFLUSH)
	    printf("flashFsBlkFlushAll: rv=OK\n");

    return OK;
}


PRIVATE STATUS flashFsBlkEraseAll0(void)
{
    int		volnum;

    flashFsBlkInvalAll0();

    for (volnum = 0; volnum < flashVolCount0; volnum++)
	    if (flashErase(flashVol0[volnum].dev) == ERROR)
	        return ERROR;

    return OK;
}




PRIVATE STATUS flashFsSync0(void)
{
    if (flashFsVerbose & VERBOSE_SYNC)
        printf("flashFsSync0: entered, vol=0x%x\n", (int) flashRawVolDesc0);

    if (flashRawVolDesc0) {
        flashFsBlkFlushAll0();
    }

    return OK;
}


PRIVATE STATUS flashFsGetPhys0(int blkNum, int *volnum_p, int *pos_p)
{
    int		volnum, block = blkNum;

    for (volnum = 0; volnum < flashVolCount0; volnum++) {
	    flash_vol_t	*vol = &flashVol0[volnum];

	    if (block < vol->blocks) {
	        *volnum_p = volnum;
	        break;
	    }

	    block -= vol->blocks;
    }

    if (volnum == flashVolCount0) {
	    printf("flashFsGetPhys0: RAW block %d out of range\n", blkNum);
	    return ERROR;
    }

    *pos_p = block * RAW_SECTOR_SIZE;

    if (flashFsVerbose & VERBOSE_GETPHYS)
	    printf("flashFsGetPhys0: blkNum = %d, volnum = %d, pos = 0x%08x\n",
	           blkNum, volnum, *pos_p);

    return OK;
}


PRIVATE STATUS flashFsBlkRead0(BLK_DEV *pDev,
			      int startBlk, int numBlks, char *buf)
{
    int			block, pos, volnum;

    if (flashFsVerbose & VERBOSE_BLKREAD)
	    printf("flashFsBlkRead0: startBlk = %d, numBlks = %d\n",
	           startBlk, numBlks);

    for (block = 0; block < numBlks; block++) {
	    int		sector, offset;
	    flash_vol_t	*vol;

	    if (flashFsGetPhys0(startBlk + block, &volnum, &pos) == ERROR)
	        return ERROR;

	    vol = &flashVol0[volnum];

	    sector = (pos >> vol->lgSectorSize);
	    offset = pos - (sector << vol->lgSectorSize);

	    if (vol->bufData && vol->bufSector == sector) {
	        /*
	         * Block is in buffered sector.  Read from there.
	         */

	        memcpy(buf, vol->bufData + offset, RAW_SECTOR_SIZE);
	    } else {
	        /*
	         * Read block directly from PROM.
	         */

	        if (flashRead(vol->dev, pos, buf, RAW_SECTOR_SIZE) == ERROR)
		        return ERROR;
	    }

	    buf += RAW_SECTOR_SIZE;
    }

    return OK;
}



PRIVATE STATUS flashFsBlkWrite0(BLK_DEV *pDev,
                                int startBlk, int numBlks, char *buf)
{
    int         block, pos, volnum;

    if (flashFsVerbose & VERBOSE_BLKWRITE)
        printf("flashFsBlkWrite0(): startBlk = %d, numBlks = %d\n",
               startBlk, numBlks);

    for (block = 0; block < numBlks; block++) {
        int     sector, offset;
        flash_vol_t *vol;

        if (flashFsGetPhys0(startBlk + block, &volnum, &pos) == ERROR)
            return ERROR;

        vol = &flashVol0[volnum];

        sector = (pos >> vol->lgSectorSize);
        offset = pos - (sector << flashVol0[volnum].lgSectorSize);

        if (vol->bufData && vol->bufSector == sector) {
            /*
             * Block is in dirty buffered sector, so write to there.
             */

            memcpy(vol->bufData + offset, buf, RAW_SECTOR_SIZE);
        } else {
            int     rv;

            /*
             * If block is not in buffered sector, but the corresponding
             * block in the PROM contains all F's, write directly to
             * PROM without flashing.
             */

            rv = flashWritable(vol->dev, pos, RAW_SECTOR_SIZE);

            if (rv == ERROR)
                return ERROR;

            if (rv) {
                /*
                 * Safe to write in place
                 */

                if (flashWrite(vol->dev, pos, buf, RAW_SECTOR_SIZE) == ERROR)
                    return ERROR;
            } else {
                /*
                 * Must flush old dirty buffer (if any),
                 * and create a new dirty buffer.
                 */

                if (flashFsBlkFlush0(volnum) == ERROR)
                    return ERROR;

                if (flashFsBlkFill0(volnum, sector) == ERROR)
                    return ERROR;

                memcpy(vol->bufData + offset, buf, RAW_SECTOR_SIZE);
            }
        }

        buf += RAW_SECTOR_SIZE;
    }

    flashFsSync0();
    return OK;
}



PRIVATE STATUS flashFsIoctl0(BLK_DEV *pDev, int funcCode, int arg)
{
    if (flashFsVerbose & VERBOSE_IOCTL)
        printf("flashFsIoctl0(): called, func=%d\n", funcCode);

    switch (funcCode) {
    case FIODISKFORMAT:
        if (flashFsBlkEraseAll0() == ERROR)
            return ERROR;
        flashFsSync0();
        break;

    case FIOFLASHSYNC:
        if (flashFsBlkFlushAll0() == ERROR)
            return ERROR;
        break;

    case FIOFLASHINVAL:
        if (flashFsBlkInvalAll0() == ERROR)
            return ERROR;
        break;

    default:
        errnoSet(S_ioLib_UNKNOWN_REQUEST);
        return ERROR;
        break;
    }

    return OK;
}




/***********************************************************************
 *
 * Block Buffer Diagnostics for FS0
 *
 ***********************************************************************/

PRIVATE STATUS flashFsBlkDiag0(void)

⌨️ 快捷键说明

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