+ 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]);
+ }
+ }