[][OpenWrt Dev][nmbm: add extra bad block check before erasing]

[Description]
Add extra bad block check before erasing to make sure bad block(s) marked
by user after NMBM initialization can be detected.

[Release-log]
N/A

Change-Id: I1dcacb37210c710db0ab259c5068a120004b1f28
Reviewed-on: https://gerrit.mediatek.inc/c/openwrt/feeds/mtk_openwrt_feeds/+/4649340
diff --git a/target/linux/generic/files-5.4/drivers/mtd/nmbm/nmbm-core.c b/target/linux/generic/files-5.4/drivers/mtd/nmbm/nmbm-core.c
index 9419832..fe8dae9 100644
--- a/target/linux/generic/files-5.4/drivers/mtd/nmbm/nmbm-core.c
+++ b/target/linux/generic/files-5.4/drivers/mtd/nmbm/nmbm-core.c
@@ -639,6 +639,12 @@
 		if (nmbm_get_block_state(ni, ba) != BLOCK_ST_GOOD)
 			goto next_block;
 
+		/* Insurance to detect unexpected bad block marked by user */
+		if (nmbm_check_bad_phys_block(ni, ba)) {
+			nmbm_set_block_state(ni, ba, BLOCK_ST_BAD);
+			goto next_block;
+		}
+
 		success = nmbm_erase_phys_block(ni, ba2addr(ni, ba));
 		if (success)
 			goto next_block;
@@ -727,6 +733,12 @@
 
 		if (nmbm_get_block_state(ni, ba) != BLOCK_ST_GOOD)
 			goto next_block;
+
+		/* Insurance to detect unexpected bad block marked by user */
+		if (nmbm_check_bad_phys_block(ni, ba)) {
+			nmbm_set_block_state(ni, ba, BLOCK_ST_BAD);
+			goto next_block;
+		}
 
 		success = nmbm_erase_phys_block(ni, ba2addr(ni, ba));
 		if (!success)
@@ -887,6 +899,12 @@
 		if (nmbm_get_block_state(ni, ba) != BLOCK_ST_GOOD)
 			goto next_block;
 
+		/* Insurance to detect unexpected bad block marked by user */
+		if (nmbm_check_bad_phys_block(ni, ba)) {
+			nmbm_set_block_state(ni, ba, BLOCK_ST_BAD);
+			goto next_block;
+		}
+
 		success = nmbm_erase_phys_block(ni, ba2addr(ni, ba));
 		if (!success)
 			goto skip_bad_block;
@@ -2322,6 +2340,13 @@
 	if (nmbm_get_block_state(ni, pb) == BLOCK_ST_BAD ||
 	    nmbm_get_block_state(ni, pb) == BLOCK_ST_NEED_REMAP)
 		goto remap_logic_block;
+
+	/* Insurance to detect unexpected bad block marked by user */
+	if (nmbm_check_bad_phys_block(ni, pb)) {
+		nlog_warn(ni, "Found unexpected bad block possibly marked by user\n");
+		nmbm_set_block_state(ni, pb, BLOCK_ST_BAD);
+		goto remap_logic_block;
+	}
 
 	success = nmbm_erase_phys_block(ni, ba2addr(ni, pb));
 	if (success)