static URET _BuildValidTreeNode(uffs_Device *dev, TreeNode *node, //!< empty node uffs_BlockInfo *bc, struct BlockTypeStatSt *st) { uffs_Tags *tag; TreeNode *node_alt; u16 block, parent, serial, block_alt, block_save; uffs_BlockInfo *bc_alt; u8 type; int page; UBOOL needToInsertToTree = U_FALSE; uffs_Buf *buf = NULL; uffs_FileInfo *info; u16 data_sum = 0; // check the first page on the block ... uffs_BlockInfoLoad(dev, bc, 0); tag = GET_TAG(bc, 0); //get first page's tag if (!TAG_IS_DIRTY(tag)) { // should never go here ... unless mark dirty page failed ? uffs_Perror(UFFS_MSG_NORMAL, "First page is clean in a non-erased block ?"); return U_FAIL; } if (!TAG_IS_VALID(tag)) { //first page is invalid ? should be erased now! uffs_Perror(UFFS_MSG_NORMAL, "first page in block %d is invalid, will be erased now!", bc->block); goto process_invalid_block; } block = bc->block; parent = TAG_PARENT(tag); serial = TAG_SERIAL(tag); type = TAG_TYPE(tag); // check if there is an 'alternative block' // (node which has the same serial number) in tree ? node_alt = uffs_FindFromTree(dev, type, parent, serial); if (node_alt != NULL) { //find a alternate node ! need to check the timestamp ! block_alt = _GetBlockFromNode(type, node_alt); uffs_Perror(UFFS_MSG_NORMAL, "Process unclean block (%d vs %d)", block, block_alt); if (block_alt == INVALID_UFFS_SERIAL) { uffs_Perror(UFFS_MSG_SERIOUS, "invalid block ?"); return U_FAIL; } bc_alt = uffs_BlockInfoGet(dev, block_alt); if (bc_alt == NULL) { uffs_Perror(UFFS_MSG_SERIOUS, "can't get block info "); return U_FAIL; } uffs_BlockInfoLoad(dev, bc_alt, 0); if (uffs_IsSrcNewerThanObj ( TAG_BLOCK_TS(tag), TAG_BLOCK_TS(GET_TAG(bc_alt, 0))) == U_TRUE) { //the node is newer than node_alt, so keep node_alt, and erase node uffs_FlashEraseBlock(dev, block); node->u.list.block = block; if (HAVE_BADBLOCK(dev)) uffs_BadBlockProcess(dev, node); else uffs_TreeInsertToErasedListTail(dev, node); uffs_BlockInfoPut(dev, bc_alt); //put back bc_alt before we return. return U_SUCC; } else { //the node is older than node_alt, so keep node, and erase node_alt //we use node as erased node to insert to erased list block_save = _GetBlockFromNode(type, node_alt); uffs_FlashEraseBlock(dev, block_save); node->u.list.block = block_save; if (HAVE_BADBLOCK(dev)) uffs_BadBlockProcess(dev, node); else uffs_TreeInsertToErasedListTail(dev, node); //put back bc_alt because we don't need it anymore. uffs_BlockInfoPut(dev, bc_alt); //use node_alt to store new informations in following node = node_alt; needToInsertToTree = U_FALSE; } } else { needToInsertToTree = U_TRUE; } if (type == UFFS_TYPE_DIR || type == UFFS_TYPE_FILE) { buf = uffs_BufClone(dev, NULL); if (buf == NULL) return U_FAIL; uffs_BlockInfoLoad(dev, bc, UFFS_ALL_PAGES); page = uffs_FindPageInBlockWithPageId(dev, bc, 0); if (page == UFFS_INVALID_PAGE) { uffs_BufFreeClone(dev, buf); uffs_Perror(UFFS_MSG_SERIOUS, "Can't find any valid page for page_id=0 ? invalid block !" "this might be caused by the tag layout change.\n"); goto process_invalid_block; } page = uffs_FindBestPageInBlock(dev, bc, page); uffs_FlashReadPage(dev, block, page, buf, U_FALSE); info = (uffs_FileInfo *) (buf->data); data_sum = uffs_MakeSum16(info->name, info->name_len); uffs_BufFreeClone(dev, buf); } switch (type) { case UFFS_TYPE_DIR: node->u.dir.block = bc->block; node->u.dir.checksum = data_sum; node->u.dir.parent = TAG_PARENT(tag); node->u.dir.serial = TAG_SERIAL(tag); st->dir++; break; case UFFS_TYPE_FILE: node->u.file.block = bc->block; node->u.file.checksum = data_sum; node->u.file.parent = TAG_PARENT(tag); node->u.file.serial = TAG_SERIAL(tag); node->u.file.len = uffs_GetBlockFileDataLength(dev, bc, UFFS_TYPE_FILE); st->file++; break; case UFFS_TYPE_DATA: node->u.data.block = bc->block; node->u.data.parent = TAG_PARENT(tag); node->u.data.serial = TAG_SERIAL(tag); node->u.data.len = uffs_GetBlockFileDataLength(dev, bc, UFFS_TYPE_DATA); st->data++; break; } if (needToInsertToTree == U_TRUE) { uffs_InsertNodeToTree(dev, type, node); } return U_SUCC; process_invalid_block: /* erase the invalid block */ uffs_FlashEraseBlock(dev, bc->block); node->u.list.block = bc->block; if (HAVE_BADBLOCK(dev)) uffs_BadBlockProcess(dev, node); else uffs_TreeInsertToErasedListTail(dev, node); return U_SUCC; }
/** * \brief recover bad block * \param[in] dev uffs device */ void uffs_BadBlockRecover(uffs_Device *dev) { TreeNode *good, *bad; uffs_Buf *buf; u16 i; u16 page; uffs_BlockInfo *bc = NULL; uffs_Tags *tag; uffs_Tags newTag; UBOOL succRecov; UBOOL goodBlockIsDirty = U_FALSE; int ret; int region; u8 type; if (dev->bad.block == UFFS_INVALID_BLOCK) return; // pick up an erased good block good = uffs_TreeGetErasedNode(dev); if (good == NULL) { uffs_Perror(UFFS_ERR_SERIOUS, "no free block to replace bad block!"); return; } //recover block bc = uffs_BlockInfoGet(dev, dev->bad.block); if (bc == NULL) { uffs_Perror(UFFS_ERR_SERIOUS, "can't get bad block info"); return; } succRecov = U_TRUE; for (i = 0; i < dev->attr->pages_per_block; i++) { page = uffs_FindPageInBlockWithPageId(dev, bc, i); if(page == UFFS_INVALID_PAGE) { break; //end of last valid page, normal break } page = uffs_FindBestPageInBlock(dev, bc, page); tag = GET_TAG(bc, page); buf = uffs_BufClone(dev, NULL); if (buf == NULL) { uffs_Perror(UFFS_ERR_SERIOUS, "Can't clone a new buf!"); succRecov = U_FALSE; break; } //NOTE: since this is a bad block, we can't guarantee the data is ECC ok, so just load data even ECC is not OK. ret = uffs_LoadPhyDataToBufEccUnCare(dev, buf, bc->block, page); if (ret == U_FAIL) { uffs_Perror(UFFS_ERR_SERIOUS, "I/O error ?"); uffs_BufFreeClone(dev, buf); succRecov = U_FALSE; break; } buf->data_len = TAG_DATA_LEN(tag); if (buf->data_len > dev->com.pg_data_size) { uffs_Perror(UFFS_ERR_NOISY, "data length over flow!!!"); buf->data_len = dev->com.pg_data_size; } buf->parent = TAG_PARENT(tag); buf->serial = TAG_SERIAL(tag); buf->type = TAG_TYPE(tag); buf->page_id = TAG_PAGE_ID(tag); newTag = *tag; TAG_BLOCK_TS(&newTag) = uffs_GetNextBlockTimeStamp(TAG_BLOCK_TS(tag)); ret = uffs_FlashWritePageCombine(dev, good->u.list.block, i, buf, &newTag); goodBlockIsDirty = U_TRUE; uffs_BufFreeClone(dev, buf); if (ret == UFFS_FLASH_IO_ERR) { uffs_Perror(UFFS_ERR_NORMAL, "I/O error ?"); succRecov = U_FALSE; break; } } if (succRecov == U_TRUE) { //successful recover bad block, so need to mark bad block, and replace with good one region = SEARCH_REGION_DIR|SEARCH_REGION_FILE|SEARCH_REGION_DATA; bad = uffs_TreeFindNodeByBlock(dev, dev->bad.block, ®ion); if (bad != NULL) { switch (region) { case SEARCH_REGION_DIR: bad->u.dir.block = good->u.list.block; type = UFFS_TYPE_DIR; break; case SEARCH_REGION_FILE: bad->u.file.block = good->u.list.block; type = UFFS_TYPE_FILE; break; case SEARCH_REGION_DATA: bad->u.data.block = good->u.list.block; type = UFFS_TYPE_DATA; } //from now, the 'bad' is actually good block :))) uffs_Perror(UFFS_ERR_NOISY, "new bad block %d found, and replaced by %d!", dev->bad.block, good->u.list.block); uffs_BlockInfoExpire(dev, bc, UFFS_ALL_PAGES); //we reuse the 'good' node as bad block node, and process the bad block. good->u.list.block = dev->bad.block; uffs_BadBlockProcess(dev, good); } else { uffs_Perror(UFFS_ERR_SERIOUS, "can't find the reported bad block(%d) in the tree???", dev->bad.block); if (goodBlockIsDirty == U_TRUE) dev->ops->EraseBlock(dev, good->u.list.block); uffs_TreeInsertToErasedListTail(dev, good); } } else { if (goodBlockIsDirty == U_TRUE) dev->ops->EraseBlock(dev, good->u.list.block); uffs_TreeInsertToErasedListTail(dev, good); //put back to erased list } uffs_BlockInfoPut(dev, bc); }