📄 nandfile.424
字号:
remains -= curSize;
pfile->fpos += curSize;
readCount += curSize;
#ifdef NAND_FFS_DEBUG
sprintf(NAND_FFS_DebugString, "\t readCount1: %d", readCount);
SprintStringLn(NAND_FFS_DebugString);
#endif
if (remains == 0)
return readCount;
// part 2: read not done yet, go to part two
// part 2 reads the data of the lumps in between the first one and the last one
/*
if (curLump->nextLump == NAND_END_LUMP)
return readCount;
if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
return readCount;
if (curLump == NULL)
return readCount;
// we have to store curLump->actualSize here
// for later part 2 processing in while()
curSize = curLump->actualSize;
nextLumpNo = curLump->nextLump;
*/
if (nextLumpNo == NAND_END_LUMP)
return readCount;
// we have to store curLump->actualSize here
// for later part 2 processing in while()
curSize = found;
if (NAND_FFS_readLump(nextLumpNo, (unsigned char**)&nextLump) == -1)
return readCount;
// continue with next block till the last block
// while (remains > ((struct diskLump *)(curBlock->nextBlock))->actualSize)
while (remains > nextLump->actualSize)
{
// update the file position of the 1st byte of the current lump
//pfile->lumpfpos += curLump->actualSize;
pfile->lumpfpos += curSize;
// get next lump
curLumpNo = nextLumpNo;
/**** added by chilong 01/16/2002 ****/
curLump = nextLump;
/**** added by chilong 01/16/2002 ****/
/* marked by chilong 01/16/2002
if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
return readCount;
marked by chilong 01/16/2002 */
// the new current lump
pfile->currentLump = curLumpNo;
src = (unsigned char *)((unsigned long)curLump + L_HEADER);
// prevLumpNo = curLumpNo;
// we have to store curLump->actualSize here
// for later pfile->fpos, remains, readCount
// nextLumpNo for the next readLump()
curSize = curLump->actualSize;
nextLumpNo = curLump->nextLump;
// if both buffers are 4 byte-aligned
if ((long)buffer % 4 == 0 && (long)src % 4 == 0)
{
// for (i = 0; i < curLump->actualSize / 4; i++)
for (i = 0; i < curSize / 4; i++)
{
*((int*)buffer) = *((int*)src);
buffer += 4;
src += 4;
}
// for (i = 0; i < curLump->actualSize % 4; i++)
for (i = 0; i < curSize % 4; i++)
*buffer++ = *src++;
}
else if ((long)buffer % 2 == 0 && (long)src % 2 == 0)// if both buffers are 2 byte-aligned
{
// for (i = 0; i < curLump->actualSize / 2; i++)
for (i = 0; i < curSize / 2; i++)
{
*((short*)buffer) = *((short*)src);
buffer +=2;
src += 2;
}
// for (i = 0; i < curLump->actualSize % 2; i++)
for (i = 0; i < curSize % 2; i++)
*buffer++ = *src++;
}
else if ((long) buffer % 2 == 1 && (long)src % 2 == 1)
{
// move 1 byte first to make both buffer & src located on even addresses
*buffer++ = *src++;
if ((long)buffer % 4 == 0 && (long)src % 4 == 0)
{
// for (i = 0; i < (curLump->actualSize-1) / 4; i++)
for (i = 0; i < (curSize-1) / 4; i++)
{
*((int*)buffer) = *((int*)src);
buffer += 4;
src += 4;
}
// for (i = 0; i < (curLump->actualSize-1) % 4; i++)
for (i = 0; i < (curSize-1) % 4; i++)
*buffer++ = *src++;
}
else
{
// for (i = 0; i < (curLump->actualSize-1) / 2; i++)
for (i = 0; i < (curSize-1) / 2; i++)
{
*((short*)buffer) = *((short*)src);
buffer +=2;
src += 2;
}
// for (i = 0; i < (curLump->actualSize-1) % 2; i++)
for (i = 0; i < (curSize-1) % 2; i++)
*buffer++ = *src++;
}
}
else
{
// for (i = 0; i < curLump->actualSize; i++)
for (i = 0; i < curSize; i++)
*buffer++ = *src++;
}
// pfile->currentLump = curLumpNo = prevLumpNo;
/*
remains -= curLump->actualSize;
pfile->fpos += curLump->actualSize;
readCount += curLump->actualSize;
*/
remains -= curSize;
pfile->fpos += curSize;
readCount += curSize;
// nextLumpNo = curLump->nextLump;
if (nextLumpNo == NAND_END_LUMP)
return readCount;
if (NAND_FFS_readLump(nextLumpNo, (unsigned char**)&nextLump) == -1)
return readCount;
}
// part 3: transfer data from the last lump to user buffer
/*
if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
return readCount;
pfile->lumpfpos += curLump->actualSize;
if (curLump->nextLump == NAND_END_LUMP)
return readCount;
// get next lump
curLumpNo = curLump->nextLump;
*/
pfile->lumpfpos += curSize;
if (nextLumpNo == NAND_END_LUMP)
return readCount;
// get next lump
curLumpNo = nextLumpNo;
/**** added by chilong 01/16/2002 ****/
curLump = nextLump;
/**** added by chilong 01/16/2002 ****/
// the new current block
pfile->currentLump = curLumpNo;
/* marked by chilong 01/16/2002
if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
return readCount;
marked by chilong 01/16/2002 */
src = (unsigned char *)((unsigned long)curLump + L_HEADER);
// prevLumpNo = curLumpNo;
nextLumpNo = curLump->nextLump;
// if both buffers are 4 byte-aligned
if ((long)buffer % 4 == 0 && (long)src % 4 == 0)
{
for (i = 0; i < remains / 4; i++)
{
*((int*)buffer) = *((int*)src);
buffer += 4;
src += 4;
}
for (i = 0; i < remains % 4; i++)
*buffer++ = *src++;
}
else if ((long)buffer % 2 == 0 && (long)src % 2 == 0)// if both buffers are 2 byte-aligned
{
for (i = 0; i < remains / 2; i++)
{
*((short*)buffer) = *((short*)src);
buffer += 2;
src += 2;
}
for (i = 0; i < remains % 2; i++)
*buffer++ = *src++;
}
else if ((long)buffer % 2 == 1 && (long)src % 2 == 1)
{
// move 1 byte first to make both buffer & src located on even addresses
*buffer++ = *src++;
if ((long)buffer % 4 == 0 && (long)src % 4 == 0)
{
for (i = 0; i < (remains-1) / 4; i++)
{
*((int*)buffer) = *((int*)src);
buffer += 4;
src += 4;
}
for (i = 0; i < (remains-1) % 4; i++)
*buffer++ = *src++;
}
else
{
for (i = 0; i < (remains-1) / 2; i++)
{
*((short*)buffer) = *((short*)src);
buffer += 2;
src += 2;
}
for (i = 0; i < (remains-1) % 2; i++)
*buffer++ = *src++;
}
}
else
{
for (i = 0; i < remains; i++)
*buffer++ = *src++;
}
// pfile->currentLump = curLumpNo = prevLumpNo;
pfile->fpos += remains;
readCount += remains;
#ifdef NAND_FFS_DEBUG
sprintf(NAND_FFS_DebugString, "\t remains: %d", remains);
SprintStringLn(NAND_FFS_DebugString);
sprintf(NAND_FFS_DebugString, "\t readCount2: %d", readCount);
SprintStringLn(NAND_FFS_DebugString);
#endif
// remains = 0;
return readCount;
}
/*************************************************************
Function : NAND_FFS_write
Description:
write data from user buffer to file
Inputs :
fhandle - file handle
buffer - user specified buffer
size - size of data to be written
Outputs :
the size of data written to disk
**************************************************************/
long NAND_FFS_write(int fhandle, void *buffer, unsigned long size)
{
long result;
sysTime now;
if (NAND_FFS_Init_OK == FALSE)
{
NAND_FFS_Errno = ERROR_FILE_SYSTEM_NOT_INIT;
return 0;
}
if ((fhandle < 0) || (fhandle >= MAX_OPEN_FILE) || (NAND_FFS_HandleTable[fhandle] == NULL))
{
NAND_FFS_Errno = ERROR_INVALID_HANDLE;
return 0;
}
if (NAND_FFS_HandleTable[fhandle]->deviceID != NAND_FLASH_DISK_ID)
{
NAND_FFS_Errno = ERROR_INVALID_DEVICE;
return 0;
}
if ((NAND_FFS_HandleTable[fhandle]->fileFlag & O_RDWR) == 0)
{
NAND_FFS_Errno = ERROR_FILE_FLAG;
return 0;
}
NAND_FFS_Errno = 0;
if (size == 0)
return 0;
if (sc_waitSemaphore(NAND_FFS_SemaphoreID) == -1)
{
NAND_FFS_Errno = ERROR_WAIT_SEMAPHORE;
return 0;
}
result = NAND_FFS_write_r(NAND_FFS_HandleTable[fhandle], (unsigned char *)buffer, size);
// get current time
sc_getTime(&now);
NAND_FFS_HandleTable[fhandle]->dirEntry.time = now.hour * 2048 + now.minute * 32 + now.second / 2;
NAND_FFS_HandleTable[fhandle]->dirEntry.date = (now.year-1980) * 512 + now.month * 32 + now.day;
// update file information
if (NAND_FFS_update_file_info(NAND_FFS_HandleTable[fhandle]) == -1)
{
sc_signalSemaphore(NAND_FFS_SemaphoreID);
return -1;
}
// update file handle if there are duplicate handles
NAND_FFS_updateFileHandleTable(NAND_FFS_HandleTable[fhandle], 1);
if (sc_signalSemaphore(NAND_FFS_SemaphoreID) == -1)
{
NAND_FFS_Errno = ERROR_SIGNAL_SEMAPHORE;
return -1;
}
return result;
}
/*************************************************************
Function: NAND_FFS_write_r
Description:
write data from file to user buffer
Input:
pfile - the target file structure
buffer - the user buffer
size - the size of data to be written
Output:
size of data written to the disk
**************************************************************/
long NAND_FFS_write_r(struct NAND_FFS_FILE *pfile, unsigned char *buffer, unsigned long size)
{
struct NAND_diskLump *curLump;
struct NAND_diskLump *prevLump;
struct NAND_diskLump *nextLump;
unsigned char *des;
int found = 0;
long writeCount = 0;
long freeSize = 0;
long remains;
long i;
// sysTime now;
int curLumpNo;
int prevLumpNo;
int nextLumpNo;
/**** added by chilong 01/13/2002 ****/
int bNeed2ReadAgain = 1;
int bCurLumpWritten = 0;
/**** added by chilong 01/13/2002 ****/
#ifdef NAND_FFS_DEBUG
// sprintf(NAND_FFS_DebugString, "[NAND_FFS_write] requited write size: %ld", size);
sprintf(NAND_FFS_DebugString, "[NAND_FFS_write] handle: %d, size: %ld",
pfile->handle, size);
SprintStringLn(NAND_FFS_DebugString);
#endif
if (pfile->currentLump == NAND_END_LUMP)
{
// there is no current lump
curLumpNo = pfile->dirEntry.startLump;
pfile->lumpfpos = 0;
if (curLumpNo == NAND_END_LUMP)
{
// no block in the file, get a new block
pfile->currentLump = NAND_findFreeLump();
if (pfile->currentLump == NAND_END_LUMP)
{
NAND_FFS_Errno = ERROR_ALLOC_LUMP;
return 0;
}
if (NAND_FFS_readLump(pfile->currentLump, (unsigned char **)&curLump) == -1)
return 0;
curLump->type = NAND_LUMP_TYPE_FILE;
//curLump->userCount = 0;
curLump->prevLump = NAND_END_LUMP;
curLump->nextLump = NAND_END_LUMP;
curLump->size = NAND_LUMP_SIZE - sizeof(struct NAND_diskLump);
curLump->actualSize = 0;
/* marked by chilong 01/13/2002
if (NAND_FFS_writeLump(pfile->currentLump, (unsigned char*)curLump) == -1)
return 0;
marked by chilong 01/13/2002 */
/**** added by chilong 01/13/2002 ****/
bNeed2ReadAgain = 0;
/**** added by chilong 01/13/2002 ****/
// this is the starting block of the file
pfile->dirEntry.startLump = pfile->currentLump;
}
else
{
pfile->currentLump = curLumpNo;
found = 1;
}
}
// there is a current block
curLumpNo = pfile->currentLump;
if (NAND_checkLump(curLumpNo) == -1)
{
// invalid block location
NAND_FFS_Errno = ERROR_FILE_SYSTEM;
return 0;
}
prevLumpNo = NAND_END_LUMP;
// check if the file position matches this lump
if (pfile->fpos < pfile->lumpfpos)
{
#ifdef NAND_FFS_DEBUG
SprintStringLn("[NAND_FFS_write] pfile->fpos < pfile->lumpfpos");
#endif
// file position is before the beginning of the current lump
// find the lump that is pointed to by the file position by going backward
if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
return 0;
while (curLumpNo != NAND_END_LUMP)
{
curLumpNo = curLump->prevLump;
if (curLumpNo != NAND_END_LUMP)
{
pfile->lumpfpos -= LUMP_DATA_SIZE;
if ((pfile->fpos >= pfile->lumpfpos) && (pfile->fpos < pfile->lumpfpos + LUMP_DATA_SIZE))
{
found = 1;
break;
}
if (NAND_FFS_readLump(curLumpNo, (unsigned char**) &curLump) == -1)
return 0;
}
}
if (curLumpNo == NAND_END_LUMP)
{
// bad file structure
found = 0;
}
}
else if (pfile->fpos >= pfile->lumpfpos + LUMP_DATA_SIZE)
{
#ifdef NAND_FFS_DEBUG
SprintStringLn("[NAND_FFS_write] pfile->fpos >= pfile->lumpfpos + LUMP_DATA_SIZE");
#endif
// file position is behind the current lump
// find the block that is pointed by the file position by going forward
if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
return 0;
while (curLumpNo != NAND_END_LUMP)
{
prevLumpNo = curLumpNo;
curLumpNo = curLump->nextLump;
if (curLumpNo != NAND_END_LUMP)
{
pfile->lumpfpos += LUMP_DATA_SIZE;
if ((pfile->fpos >= pfile->lumpfpos) && (pfile->fpos < pfile->lumpfpos + LUMP_DATA_SIZE))
{
found = 1;
break;
}
if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
return 0;
}
}
if (curLumpNo == NAND_END_LUMP && found == 1)
{
// the file position is just beyond the last lump
// add a new lump to the file
curLumpNo = NAND_findFreeLump();
if (curLumpNo == NAND_END_LUMP)
{
pfile->currentLump = NAND_END_LUMP;
pfile->lumpfpos = 0;
NAND_FFS_Errno = ERROR_ALLOC_LUMP;
return 0;
}
// write previous lump header back to flash
if (prevLumpNo != NAND_END_LUMP)
{
if (NAND_FFS_readLump(prevLumpNo, (unsigned char**)&prevLump) == -1)
return 0;
prevLump->nextLump = curLumpNo;
if (NAND_FFS_writeLump(prevLumpNo, (unsigned char *)prevLump) == -1)
return 0;
}
else
pfile->dirEntry.startLump = curLumpNo;
// set up the block links
if (NAND_FFS_readLump(curLumpNo, (unsigned char**)&curLump) == -1)
return 0;
// set new lump header
curLump->type = NAND_LUMP_TYPE_FILE;
//curLump->userCount = 0;
curLump->prevLump = prevLumpNo;
curLump->nextLump = NAND_END_LUMP;
curLump->size = NAND_LUMP_SIZE - sizeof(struct NAND_diskLump);
curLump->actualSize = 0;
if (NAND_FFS_writeLump(curLumpNo, (unsigned char*)curLump) == -1)
return 0;
bNeed2ReadAgain = 0;
}
}
if (found == 1)
{
// a lump other than the current lump is found
pfile->currentLump = curLumpNo;
}
else
{
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -