📄 flashfslib.c
字号:
{
int block, nBlocks, sBlock, i;
UINT32 buf[RAW_SECTOR_SIZE / 4];
int iter;
if (flashFsLibInit() == ERROR) {
printf("flashFsBlkDiag0: init failed\n");
return ERROR;
}
/*
* Because of the Grey code we can only test a power of 2 blocks.
* Round down to a power of 2.
*/
nBlocks = flashFsTotalBlocks0;
while (nBlocks & (nBlocks - 1))
nBlocks &= (nBlocks - 1);
/*
* Erase before first iteration.
*/
printf("flashFsBlkDiag0: Erasing\n");
if (flashFsBlkEraseAll0() == ERROR) {
printf("flashDiag0: Erase failed\n");
return ERROR;
}
for (iter = 0; iter < 2; iter++) {
/*
* The first iteration will be fast because it will be writing
* on top of all F's and will therefore avoid the buffer cache
* and avoid flashing.
*
* The second iteration will be slower because it will be
* periodically flashing sectors.
*/
printf("flashFsBlkDiag0: Writing %d blocks in scrambled order\n",
nBlocks);
for (block = 0; block < nBlocks; block++) {
/*
* Write blocks in pseudo-random order (Grey code
* scrambled). Generate data so PROM has linearly
* incrementing words when done. Grey code is a good test
* because it's highly localized but jumps around too.
*/
sBlock = block ^ (block >> 1);
printf("%x ", sBlock);
for (i = 0; i < RAW_SECTOR_SIZE / 4; i++)
buf[i] = sBlock * (RAW_SECTOR_SIZE / 4) + i;
if (flashFsBlkWrite0(0, sBlock, 1, (char *) buf) == ERROR) {
printf("\nflashFsBlkDiag0: Write failed\n");
return ERROR;
}
}
printf("\nflashFsBlkDiag0: Verifying %d blocks in scrambled order\n",
nBlocks);
for (block = 0; block < nBlocks; block++) {
/*
* Verify blocks in same order. Last block should still be
* dirty/unwritten, this would test that.
*/
sBlock = block ^ (block >> 1);
printf("%x ", sBlock);
if (flashFsBlkRead0(0, sBlock, 1, (char *) buf) == ERROR) {
printf("\nflashFsBlkDiag0: Read failed\n");
return ERROR;
}
for (i = 0; i < RAW_SECTOR_SIZE / 4; i++)
if (buf[i] != sBlock * (RAW_SECTOR_SIZE / 4) + i) {
printf("\nflashFsBlkDiag0: Verify error "
"(block: %d, offset: 0x%x)\n",
sBlock, i);
printf("flashFsBlkDiag0: Expected 0x%08x, got 0x%08x\n",
sBlock * (RAW_SECTOR_SIZE / 4) + i, buf[i]);
return ERROR;
}
}
printf("\nflashFsBlkDiag0: Flushing\n");
if (flashFsBlkFlushAll0() == ERROR) {
printf("flashFsBlkDiag0: Flush failed\n");
return ERROR;
}
/*
* Verify again in linear order
*/
printf("flashFsBlkDiag0: Verifying %d blocks in linear order\n",
nBlocks);
for (block = 0; block < nBlocks; block++) {
if (flashFsBlkRead0(0, block, 1, (char *) buf) == ERROR) {
printf("flashFsBlkDiag0: Read failed\n");
return ERROR;
}
for (i = 0; i < RAW_SECTOR_SIZE / 4; i++)
if (buf[i] != block * (RAW_SECTOR_SIZE / 4) + i) {
printf("flashFsBlkDiag0: Verify error "
"(block: %d, offset: 0x%x)\n",
block, i);
printf("flashFsBlkDiag0: Expected 0x%08x, got 0x%08x\n",
block * (RAW_SECTOR_SIZE / 4) + i, buf[i]);
return ERROR;
}
}
}
printf("flashFsBlkDiag0: Erasing\n");
if (flashFsBlkEraseAll0() == ERROR ) {
printf("flashDiag0: Erase failed\n");
return ERROR;
}
printf("flashFsBlkDiag0: Passed\n");
return OK;
}
STATUS flashFsLibInit(void)
{
int volnum;
/*
* We are considered initialized once flashRawVolDesc0 or flashDosVolDesc1 is non-NULL.
*/
if (flashRawVolDesc0 || flashDosVolDesc1||flashRawVolDesc2)
return OK;
#ifdef INCLUDE_RAWFS
/*
* Set up rfa1
*/
flashFsBlkInvalAll0();
flashFsTotalBlocks0 = 0;
for (volnum = 0; volnum < flashVolCount0; volnum++) {
flash_vol_t *vol = &flashVol0[volnum];
if (! vol->dev->found) {
printf("flashFsLibInit: missing volume %d\n", volnum);
return ERROR;
}
vol->blocks = FLASH_MAX_POS(vol->dev) / RAW_SECTOR_SIZE;
vol->lgSectorSize = vol->dev->lgSectorSize;
flashFsTotalBlocks0 += flashVol0[volnum].blocks;
}
flashBlkDev0.bd_blkRd = flashFsBlkRead0;
flashBlkDev0.bd_blkWrt = flashFsBlkWrite0;
flashBlkDev0.bd_ioctl = flashFsIoctl0;
flashBlkDev0.bd_reset = NULL;
flashBlkDev0.bd_statusChk = NULL;
flashBlkDev0.bd_removable = FALSE;
flashBlkDev0.bd_nBlocks = flashFsTotalBlocks0;
flashBlkDev0.bd_bytesPerBlk = RAW_SECTOR_SIZE;
flashBlkDev0.bd_blksPerTrack = flashFsTotalBlocks0;
flashBlkDev0.bd_nHeads = 1;
flashBlkDev0.bd_retry = 1;
flashBlkDev0.bd_mode = O_RDWR;
flashBlkDev0.bd_readyChanged = FALSE;
flashRawVolDesc0 = rawFsDevInit(FLASH_FS_NAME1, &flashBlkDev0);
if ( flashRawVolDesc0 == NULL )
printf("%s:%d rawFsDevInit(%s) failed\n",
__FILE__, __LINE__, FLASH_FS_NAME1);
flashFsSync0();
#endif /*INCLUDE_RAWFS*/
return OK;
}
STATUS flashFsSync(void)
{
return OK;
}
/*
* Function: flashFsSave
*
* Description:
* Program Flash. If file is deflated, there is a head before the actural file.
* Parameters:
* fdSrc:programmed file Description .
* filename:flash filename.
* deflated:if programmed file .
* Return:
* OK if successful, ERROR if error.
*/
STATUS flashFsSave( int fdSrc, char* fileName, int deflated)
{
int fd;
STATUS rc = ERROR;
char * compBuf;
int r, compSize;
flash_app_head_t flash_app_head;
if ((fd=open(fileName, O_WRONLY, 0666))==ERROR)
{
printErr("flash file open error.\n");
goto done1;
}
if ((compBuf = malloc(COMP_BUF_SIZE)) == NULL)
{
printErr("Not enough memory for image buffer\n");
goto done1;
}
compSize = 0;
while ((r = read(fdSrc, /* Read loop required to support network */
compBuf + compSize,
COMP_BUF_SIZE - compSize)) > 0)
compSize += r;
if (r < 0) {
printErr("Read failed: errno = %d\n", errnoGet());
goto done1;
}
if (compSize == COMP_BUF_SIZE) {
printErr("Compressed image too large\n");
goto done1;
}
printf("%d\n", compSize);
printf("Write to flash %s ......................... ", fileName);
if (deflated)
{
memset(&flash_app_head, 0, sizeof(flash_app_head_t));
flash_app_head.filesize=compSize;
strcpy(flash_app_head.filename, fileName);
if ((r=write (fd, (char *)&flash_app_head, sizeof(flash_app_head_t)))!=sizeof(flash_app_head_t))
{
printErr("flash file write error\n");
goto done1;
}
}
if ((r=write (fd, compBuf, compSize))!=compSize)
{
printErr("flash file write error\n");
goto done1;
}
printf("Done\n");
rc=OK;
done1:
if (compBuf) free(compBuf);
if (fd>=0) close(fd);
return rc;
}
/*
* Function: flashFsBackup
*
* Description:
* copy a file to another file.
* Parameters:
* fSrc: file be copied.
* fdes: file be copy to.
* Return:
* total copied bytes.
*/
int flashFsCopy( char * fsrc, char * fdes)
{
int fdsrc, fddes;
char * copyBuf;
int copySize=0, totalSize=0;
if ((fdsrc=open(fsrc, O_RDONLY, 0666))==ERROR)
{
printf("%s:%d open failed Errno %08x\n",
__FILE__, __LINE__, errnoGet());
goto done1;
}
if ((fddes = open(fdes, O_WRONLY | O_CREAT, 0644)) == ERROR)
{
printf("%s:%d open failed Errno %08x\n",
__FILE__, __LINE__, errnoGet());
goto done1;
}
if ((copyBuf = malloc(0x80000)) == NULL)
{
printf("Not enough memory for image buffer\n");
goto done1;
}
while ((copySize = read(fdsrc, copyBuf , 0x80000)) > 0)
{
write(fddes,copyBuf,copySize);
totalSize+=copySize;
/*printf("Copyed %d bytes from %s to %s\n", copySize, fsrc, fdes);*/
}
done1:
if (copyBuf) free(copyBuf);
if (fdsrc>=0) close(fdsrc);
if (fddes>=0) close(fddes);
return totalSize;
}
#if 1
STATUS flashFsPrint(char* filename, int offset, int size)
{
int fd;
char *compBuf;
int compSize, r;
printf("enter flashFileOpen, and open the %s\n", filename);
if ((fd=open(filename, O_RDONLY, 666))==ERROR)
{
printErr("flash file open error.\n");
return ERROR;
}
if ((compBuf = malloc(COMP_BUF_SIZE)) == NULL)
{
printErr("Not enough memory for image buffer 1\n");
return ERROR;
}
compSize = 0;
while ((r = read(fd, /* Read loop required to support network */
compBuf + compSize,
COMP_BUF_SIZE - compSize)) > 0)
{
compSize += r;
}
printf("total %d bytes\n", compSize);
if ((offset+size)<=compSize)
memcat(compBuf+offset, size, 16);
if (compBuf) free(compBuf);
close(fd);
return OK;
}
STATUS flashFsSaveTo(char* hostName, char* fileName, char* usr, char* passwd, char* toFileName, int deflated)
{
int fdSrc, errFd;
printf ("Loading %s .......................... ", fileName);
/*if (!deflated) *(long *)MEM_CTL_BASE=0x20c620c6;*/
if (ftpXfer (hostName, usr, passwd, "", "RETR %s", "", fileName,
&errFd, &fdSrc) == ERROR)
return (ERROR);
if( flashFsSave( fdSrc, toFileName, deflated ) != OK )
{
printf("flashFsSaveTo %s Error\n", toFileName);
return (ERROR);
}
/* if (!deflated) *(long *)MEM_CTL_BASE=MCR_CS0_BS;*/
close (fdSrc);
close (errFd);
return OK;
}
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -