By the way, just in case, here is my current corrected routine, which so far passed my nightly testings:
static int _ScanCleanBlock(uffs_Device *dev, int block)
{
int page;
int size = dev->com.pg_size;
u8* page_content = MY_ALLOC_FCTN(u8, size*sizeof(u8));
int ret = UFFS_FLASH_UNKNOWN_ERR;
for (page = dev->attr->pages_per_block - 1; page >= 0; page--) {
if (dev->ops->ReadPageWithLayout) {
ret = dev->ops->ReadPageWithLayout(dev, block, page, page_content, size, NULL, NULL, NULL);
}
else {
ret = dev->ops->ReadPage(dev, block, page, page_content, size, NULL, NULL, 0);
}
if(ret == UFFS_FLASH_IO_ERR){
uffs_Perror(UFFS_MSG_SERIOUS,
"IO error reading block %d page %d",
block, page);
goto ext;
}
u8 *check_FF = page_content, *check_limit = page_content + size;
for(; check_FF < check_limit; check_FF++){
if (*check_FF != 0xFF){
uffs_Perror(UFFS_MSG_NORMAL,
"unclean page found, block %d page %d, ",
block, page);
ret = UFFS_FLASH_BAD_BLK;
goto ext;
}
}
}
ret = UFFS_FLASH_NO_ERR;
ext:
MY_FREE_FCTN(page_content);
return ret;
}
static URET _BuildTreeStepOne(uffs_Device *dev)
{
(...)
}
else if (uffs_IsPageErased(dev, bc, 0) == U_TRUE) { //@ read one spare: 0
// page 0 tag shows it's an erased block, we need to check the mini header status to make sure it is clean.
int ret = _ScanCleanBlock(dev, block_lt);
if (ret == UFFS_FLASH_IO_ERR) {
uffs_Perror(UFFS_MSG_SERIOUS,
"I/O error when reading page !"
"block %d page %d",
block_lt, 0);
ret = U_FAIL;
break;
}
if (ret == UFFS_FLASH_BAD_BLK) {
// page 0 tag is clean but page data is dirty ???
// this block should be erased immediately !
uffs_Perror(UFFS_MSG_NORMAL,
"block %d dirty, erasing it",
block_lt);
uffs_FlashEraseBlock(dev, block_lt);
}
node->u.list.block = block_lt;
if (HAVE_BADBLOCK(dev)) {
uffs_Perror(UFFS_MSG_NORMAL,
"New bad block (%d) discovered.", block_lt);
uffs_BadBlockProcess(dev, node);
}
else {
uffs_TreeInsertToErasedListTail(dev, node);
}
}
else {
// this block have valid data page(s).
(...)
}
Best regards,
Fred