+/*
+ * Check if the given block is marked bad: ONFI standard mandates
+ * that bad block are marked with "00" bytes on the spare area of the
+ * first page in block.
+ */
+static bool blockIsGood(Mt29f *chip, uint16_t blk)
+{
+ uint8_t *first_byte = (uint8_t *)NFC_SRAM_BASE_ADDR;
+ bool good;
+
+ // Check first byte in spare area of first page in block
+ mt29f_readPage(chip, blk * MT29F_PAGES_PER_BLOCK, MT29F_DATA_SIZE);
+ good = *first_byte == 0xFF;
+
+ if (!good)
+ LOG_INFO("mt29f: bad block %d\n", blk);
+
+ return good;
+}
+
+
+/*
+ * Return the main partition block remapped on given block in the remap
+ * partition (dest_blk).
+ */
+static int getBadBlockFromRemapBlock(Mt29f *chip, uint16_t dest_blk)
+{
+ struct RemapInfo *remap_info = (struct RemapInfo *)NFC_SRAM_BASE_ADDR;
+
+ if (!mt29f_readPage(chip, dest_blk * MT29F_PAGES_PER_BLOCK, MT29F_DATA_SIZE + MT29F_REMAP_TAG_OFFSET))
+ return -1;
+
+ if (remap_info->tag == MT29F_REMAP_TAG)
+ return remap_info->mapped_blk;
+ else
+ return -1;
+}
+
+
+/*
+ * Set a block remapping: src_blk (a block in main data partition) is remappend
+ * on dest_blk (block in reserved remapped blocks partition).
+ */
+static bool setMapping(Mt29f *chip, uint32_t src_blk, uint32_t dest_blk)
+{
+ struct RemapInfo *remap_info = (struct RemapInfo *)NFC_SRAM_BASE_ADDR;
+
+ LOG_INFO("mt29f, setMapping(): src=%ld dst=%ld\n", src_blk, dest_blk);
+
+ if (!mt29f_readPage(chip, dest_blk * MT29F_PAGES_PER_BLOCK, MT29F_DATA_SIZE + MT29F_REMAP_TAG_OFFSET))
+ return false;
+
+ remap_info->tag = MT29F_REMAP_TAG;
+ remap_info->mapped_blk = src_blk;
+
+ return mt29f_writePage(chip, dest_blk * MT29F_PAGES_PER_BLOCK, MT29F_DATA_SIZE + MT29F_REMAP_TAG_OFFSET);
+}
+
+
+/*
+ * Get a new block from the remap partition to use as a substitute
+ * for a bad block.
+ */
+static uint16_t getFreeRemapBlock(Mt29f *chip)
+{
+ int blk;
+
+ for (blk = chip->remap_start; blk < MT29F_NUM_BLOCKS; blk++)
+ {
+ if (blockIsGood(chip, blk))
+ {
+ chip->remap_start = blk + 1;
+ return blk;
+ }
+ }
+
+ LOG_ERR("mt29f: reserved blocks for bad block remapping exhausted!\n");
+ return 0;
+}
+
+
+/*
+ * Check if NAND is initialized.
+ */
+static bool chipIsMarked(Mt29f *chip)
+{
+ return getBadBlockFromRemapBlock(chip, MT29F_NUM_USER_BLOCKS) != -1;
+}
+
+
+/*
+ * Initialize NAND (format). Scan NAND for factory marked bad blocks.
+ * All bad blocks found are remapped to the remap partition: each
+ * block in the remap partition used to remap bad blocks is marked.
+ */
+static void initBlockMap(Mt29f *chip)
+{
+ unsigned b, last;
+
+ // Default is for each block to not be remapped
+ for (b = 0; b < MT29F_NUM_USER_BLOCKS; b++)
+ chip->block_map[b] = b;
+ chip->remap_start = MT29F_NUM_USER_BLOCKS;
+
+ if (chipIsMarked(chip))
+ {
+ LOG_INFO("mt29f: found initialized NAND, searching for remapped blocks\n");
+
+ // Scan for assigned blocks in remap area
+ for (b = last = MT29F_NUM_USER_BLOCKS; b < MT29F_NUM_BLOCKS; b++)
+ {
+ if (blockIsGood(chip, b))
+ {
+ int remapped_blk = getBadBlockFromRemapBlock(chip, b);
+ if (remapped_blk != -1 && remapped_blk != MT29F_NULL_REMAP)
+ {
+ LOG_INFO("mt29f: found remapped block %d->%d\n", remapped_blk, b);
+ chip->block_map[remapped_blk] = b;
+ last = b + 1;
+ }
+ }
+ }
+ chip->remap_start = last;
+ }
+ else
+ {
+ bool remapped_anything = false;
+
+ LOG_INFO("mt29f: found new NAND, searching for bad blocks\n");
+
+ for (b = 0; b < MT29F_NUM_USER_BLOCKS; b++)
+ {
+ if (!blockIsGood(chip, b))
+ {
+ chip->block_map[b] = getFreeRemapBlock(chip);
+ setMapping(chip, b, chip->block_map[b]);
+ remapped_anything = true;
+ LOG_INFO("mt29f: found new bad block %d, remapped to %d\n", b, chip->block_map[b]);
+ }
+ }
+
+ /*
+ * If no bad blocks are found (we're lucky!) write a dummy
+ * remap to mark NAND and detect we already scanned it next time.
+ */
+ if (!remapped_anything)
+ {
+ setMapping(chip, MT29F_NULL_REMAP, MT29F_NUM_USER_BLOCKS);
+ LOG_INFO("mt29f: no bad block founds, marked NAND\n");
+ }
+ }
+}
+
+
+#ifdef _DEBUG
+
+/*
+ * Erase all blocks.
+ * DON'T USE on production chips: this function will try to erase
+ * factory marked bad blocks too.
+ */
+static void mt29f_wipe(Mt29f *chip)
+{
+ int b;
+ for (b = 0; b < MT29F_NUM_BLOCKS; b++)
+ {
+ LOG_INFO("mt29f: erasing block %d\n", b);
+ mt29f_blockErase(chip, b);
+ }
+}
+
+/*
+ * Create some bad blocks, erasing them and writing the bad block mark.
+ */
+static void mt29f_ruinSomeBlocks(Mt29f *chip)
+{
+ int bads[] = { 7, 99, 555, 1003, 1004, 1432 };
+ unsigned i;
+
+ LOG_INFO("mt29f: erasing mark\n");
+ mt29f_blockErase(chip, MT29F_NUM_USER_BLOCKS);
+
+ for (i = 0; i < countof(bads); i++)
+ {
+ LOG_INFO("mt29f: erasing block %d\n", bads[i]);
+ mt29f_blockErase(chip, bads[i]);
+
+ LOG_INFO("mt29f: marking page %d as bad\n", bads[i] * MT29F_PAGES_PER_BLOCK);
+ memset((void *)NFC_SRAM_BASE_ADDR, 0, MT29F_SPARE_SIZE);
+ mt29f_writePage(chip, bads[i] * MT29F_PAGES_PER_BLOCK, MT29F_DATA_SIZE);
+ }
+}
+
+#endif
+
+