flashFsLib.c
资源名称:bcm4702.rar [点击查看]
上传用户:yuanda199
上传日期:2022-06-26
资源大小:412k
文件大小:6k
源码类别:
VxWorks
开发平台:
C/C++
- #include "types.h"
- #include "vxWorks.h"
- #include "stdio.h"
- #include "stdlib.h"
- #include "errno.h"
- #include "ioLib.h"
- #include "dosFsLib.h"
- #include "fioLib.h"
- #include "blkIo.h"
- #include "string.h"
- #include "flashDrvLib.h"
- #include "flashFsLib.h"
- #include "errnoLib.h"
- BLK_DEV flashBlkDev;
- DOS_VOL_DESC *flashDosVolDesc = NULL;
- int flashDosFormat = 0;
- /*
- * The boot ROM resides at the begining of the 2 MB flash, from 0 MB
- * through 0 MB plus 512 KB.
- *
- * flashFsGetPhys remaps logical into physical sectors in such as way as
- * to skip over the two boot sectors.
- */
- int flashFsVerbose = 0;
- STATUS
- flashFsGetPhys(int blkNum, int *sectorNum, int *offset)
- {
- int sect;
- if (blkNum >= FLASH_FS_SIZE_BLOCKS)
- return (ERROR);
- sect = blkNum / FLASH_FS_BLOCK_PER_SECTOR;
- if (sect >= FLASH_BOOT_START_SECTOR) {
- /* Skip boot sector */
- sect += FLASH_BOOT_SIZE_SECTORS;
- }
- *sectorNum = sect;
- *offset = (blkNum % FLASH_FS_BLOCK_PER_SECTOR) * FLASH_FS_BLOCK_SIZE;
- if (flashFsVerbose > 2)
- printf("flashFsGetPhys(): blkNum = %d, "
- "*sectorNum = %d, *offset = 0x%xn",
- blkNum, *sectorNum, *offset);
- return (OK);
- }
- STATUS
- flashFsBlkRead(BLK_DEV *pDev, int startBlk, int numBlks, char *pBuf)
- {
- int blkIndx, phySectorNum, offset;
- if (flashFsVerbose > 1)
- printf("flashFsBlkRead(): startBlk = %d, numBlks = %dn",
- startBlk, numBlks);
- for (blkIndx = 0; blkIndx < numBlks; blkIndx++) {
- if (flashFsGetPhys((startBlk + blkIndx), &phySectorNum, &offset) ==
- ERROR) {
- if (flashFsVerbose)
- printf("flashFsBlkRead(): flashFsGetPhys() failedn");
- return (ERROR);
- }
- if (flashBlkRead(phySectorNum, pBuf,
- offset, FLASH_FS_BLOCK_SIZE) == ERROR) {
- if (flashFsVerbose)
- printf("flashFsBlkRead(): flashBlkRead() failedn");
- return (ERROR);
- }
- pBuf += FLASH_FS_BLOCK_SIZE;
- }
- return (OK);
- }
- STATUS
- flashFsBlkWrite(BLK_DEV *pDev, int startBlk, int numBlks, char *pBuf)
- {
- int blkIndx, phySectorNum, offset;
- if (flashFsVerbose > 1)
- printf("flashFsBlkWrite(): startBlk = %d, numBlks = %dn",
- startBlk, numBlks);
- for (blkIndx = 0; blkIndx < numBlks; blkIndx++) {
- if (flashFsGetPhys((startBlk + blkIndx), &phySectorNum, &offset) ==
- ERROR) {
- printf("flashFsBlkWrite(): flashFsGetPhys() failedn");
- return (ERROR);
- }
- if (flashBlkWrite(phySectorNum, pBuf,
- offset, FLASH_FS_BLOCK_SIZE) == ERROR) {
- printf("flashFsBlkWrite(): flashBlkWrite() failedn");
- return (ERROR);
- }
- pBuf += FLASH_FS_BLOCK_SIZE;
- }
- return (OK);
- }
- STATUS
- flashSync(void)
- {
- int flashFd = 0;
- STATUS rc;
- if ((flashFd = open(FLASH_FS_NAME, O_RDWR, 0666)) == ERROR) {
- printf("flashSync(): open() failedn");
- }
- rc = ioctl(flashFd, FLASH_FS_SYNC, 0);
- close(flashFd);
- return rc;
- }
- STATUS
- flashFsIoctl(BLK_DEV *pDev, int funcCode, int arg)
- {
- int sectorAfterBoot;
- if (flashFsVerbose > 1)
- printf("flashFsBlkIoctl(): calledn");
- switch (funcCode) {
- case FIODISKFORMAT:
- /* Clear flashDrvLib's cached sector */
- if (flashDrvLibInit() == ERROR) {
- printf("flashFsLibInit(): flashDrvLibInit() failedn");
- return (ERROR);
- }
- /* From beginning of flash to beginning of Boot Area */
- if (flashEraseBank(0, FLASH_BOOT_START_SECTOR) == ERROR) {
- return (ERROR);
- }
- sectorAfterBoot = FLASH_BOOT_START_SECTOR + FLASH_BOOT_SIZE_SECTORS;
- /* From end of Boot Area to end of flash */
- if (flashEraseBank(sectorAfterBoot,
- FLASH_SIZE_SECTORS - sectorAfterBoot) == ERROR) {
- return (ERROR);
- }
- break;
- case FLASH_FS_SYNC:
- flashSyncFilesystem();
- break;
- default:
- errnoSet(S_ioLib_UNKNOWN_REQUEST);
- return (ERROR);
- break;
- }
- return (OK);
- }
- int
- tstFlashFile(void)
- {
- int fh;
- char buff[20];
- printf("creatingn");
- fh = creat("myfile", O_RDWR);
- if (fh < 0) {
- printf("tstFlashFile(): create() failedn");
- return (ERROR);
- }
- printf("writingn");
- write(fh, buff, 10);
- printf("closingn");
- close(fh);
- return (0);
- }
- int
- tstFlashFile_1(void)
- {
- int fh;
- char buff[40];
- printf("openingn");
- fh = open("myfile", O_RDWR, 0664);
- if (fh < 0) {
- printf("tstFlashFile_1(): open() failedn");
- return (ERROR);
- }
- printf("writingn");
- write(fh, buff, 10);
- printf("writingn");
- write(fh, buff, 23);
- printf("closingn");
- close(fh);
- return (0);
- }
- STATUS
- flashFsLibInit(void)
- {
- /*
- * We are considered initialized once flashDosVolDesc is non-NULL.
- */
- if (flashDosVolDesc) {
- return OK;
- }
- if (flashDrvLibInit() == ERROR) {
- printf("flashFsLibInit(): flashDrvLibInit() failedn");
- return (ERROR);
- }
- /*
- * Set up BLK DEV structure
- */
- flashBlkDev.bd_blkRd = flashFsBlkRead;
- flashBlkDev.bd_blkWrt = flashFsBlkWrite;
- flashBlkDev.bd_ioctl = flashFsIoctl;
- flashBlkDev.bd_reset = NULL;
- flashBlkDev.bd_statusChk = NULL;
- flashBlkDev.bd_removable = FALSE;
- flashBlkDev.bd_nBlocks = FLASH_FS_SIZE_BLOCKS;
- flashBlkDev.bd_bytesPerBlk = FLASH_FS_BLOCK_SIZE;
- flashBlkDev.bd_blksPerTrack = 1;
- flashBlkDev.bd_nHeads = 1;
- flashBlkDev.bd_retry = 1;
- flashBlkDev.bd_mode = O_RDWR;
- flashBlkDev.bd_readyChanged = FALSE;
- if (flashFsVerbose) {
- printf("flashFsLibInit: Initializingn");
- }
- if (!flashDosFormat) {
- flashDosVolDesc = dosFsDevInit(FLASH_FS_NAME, &flashBlkDev, NULL);
- }
- if (flashDosVolDesc == NULL) {
- if (flashFsVerbose) {
- printf("nflashFsLibInit: first time initialization...n");
- }
- if (dosFsMkfsOptionsSet(DOS_OPT_LONGNAMES) == ERROR) {
- printf("flashFsLibInit: dosFsMkfsOptionsSet failedn");
- }
- flashDosVolDesc = dosFsMkfs(FLASH_FS_NAME, &flashBlkDev);
- if (flashDosVolDesc == NULL) {
- printf("flashFsLibInit (first init): dosFsMkfs failed!");
- printf("(0x%x)n", errno);
- return ERROR;
- } else {
- if (flashFsVerbose) {
- printf("donen");
- }
- }
- }
- flashFsSync();
- return OK;
- }
- STATUS
- flashFsSync(void)
- {
- if (flashDosVolDesc) {
- if (flashFsVerbose) {
- printf("flashFsSync(): Syncing...");
- }
- flashSyncFilesystem();
- if (flashFsVerbose) {
- printf("donen");
- }
- }
- return OK;
- }