summaryrefslogtreecommitdiff
path: root/cmd/nand.c
diff options
context:
space:
mode:
authorBalamanikandan Gunasundar <balamanikandan.gunasundar@microchip.com>2021-06-29 12:46:11 +0530
committerTom Rini <trini@konsulko.com>2021-10-26 15:26:45 -0400
commit4ea84c5dce5a96a4ed9f8c410974425ff2167721 (patch)
treeb45baeac49148ce62d0d62181cdcbc40d93841f3 /cmd/nand.c
parent75e33b378b3c81f1be1a8fa3815390b39fddda57 (diff)
cmd: nand biterr - Add support for nand biterr command
The command shall be used to induce bit errors in the nand page manually. The code flips a bit in the specified offset without changing the ECC. This helps to see how the software handles the error. The patch is ported from https://patchwork.ozlabs.org/project/uboot/patch/\ 1325691123-19565-1-git-send-email-holger.brunck@keymile.com The implementation is inspired from 'mtd-utils/nand-utils/nandflipbits.c' Signed-off-by: Balamanikandan Gunasundar <balamanikandan.gunasundar@microchip.com>
Diffstat (limited to 'cmd/nand.c')
-rw-r--r--cmd/nand.c127
1 files changed, 124 insertions, 3 deletions
diff --git a/cmd/nand.c b/cmd/nand.c
index df5a4b1db1..e730484d0b 100644
--- a/cmd/nand.c
+++ b/cmd/nand.c
@@ -17,6 +17,10 @@
* and/or modified under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of the
* License, or (at your option) any later version.
+ *
+ * The function nand_biterror() in this file is inspired from
+ * mtd-utils/nand-utils/nandflipbits.c which was released under GPLv2
+ * only
*/
#include <common.h>
@@ -44,6 +48,116 @@ int find_dev_and_part(const char *id, struct mtd_device **dev,
u8 *part_num, struct part_info **part);
#endif
+#define MAX_NUM_PAGES 64
+
+static int nand_biterror(struct mtd_info *mtd, ulong off, int bit)
+{
+ int ret = 0;
+ int page = 0;
+ ulong block_off;
+ u_char *datbuf[MAX_NUM_PAGES]; /* Data and OOB */
+ u_char data;
+ int pages_per_blk = mtd->erasesize / mtd->writesize;
+ struct erase_info einfo;
+
+ if (pages_per_blk > MAX_NUM_PAGES) {
+ printf("Too many pages in one erase block\n");
+ return 1;
+ }
+
+ if (bit < 0 || bit > 7) {
+ printf("bit position 0 to 7 is allowed\n");
+ return 1;
+ }
+
+ /* Allocate memory */
+ memset(datbuf, 0, sizeof(datbuf));
+ for (page = 0; page < pages_per_blk ; page++) {
+ datbuf[page] = malloc(mtd->writesize + mtd->oobsize);
+ if (!datbuf[page]) {
+ printf("No memory for page buffer\n");
+ ret = -ENOMEM;
+ goto free_memory;
+ }
+ }
+
+ /* Align to erase block boundary */
+ block_off = off & (~(mtd->erasesize - 1));
+
+ /* Read out memory as first step */
+ for (page = 0; page < pages_per_blk ; page++) {
+ struct mtd_oob_ops ops;
+ loff_t addr = (loff_t)block_off;
+
+ memset(&ops, 0, sizeof(ops));
+ ops.datbuf = datbuf[page];
+ ops.oobbuf = datbuf[page] + mtd->writesize;
+ ops.len = mtd->writesize;
+ ops.ooblen = mtd->oobsize;
+ ops.mode = MTD_OPS_RAW;
+ ret = mtd_read_oob(mtd, addr, &ops);
+ if (ret < 0) {
+ printf("Error (%d) reading page %08lx\n",
+ ret, block_off);
+ ret = 1;
+ goto free_memory;
+ }
+ block_off += mtd->writesize;
+ }
+
+ /* Erase the block */
+ memset(&einfo, 0, sizeof(einfo));
+ einfo.mtd = mtd;
+ /* Align to erase block boundary */
+ einfo.addr = (loff_t)(off & (~(mtd->erasesize - 1)));
+ einfo.len = mtd->erasesize;
+ ret = mtd_erase(mtd, &einfo);
+ if (ret < 0) {
+ printf("Error (%d) nand_erase_nand page %08llx\n",
+ ret, einfo.addr);
+ ret = 1;
+ goto free_memory;
+ }
+
+ /* Twist a bit in data part */
+ block_off = off & (mtd->erasesize - 1);
+ data = datbuf[block_off / mtd->writesize][block_off % mtd->writesize];
+ data ^= (1 << bit);
+ datbuf[block_off / mtd->writesize][block_off % mtd->writesize] = data;
+
+ printf("Flip data at 0x%lx with xor 0x%02x (bit=%d) to value=0x%02x\n",
+ off, (1 << bit), bit, data);
+
+ /* Write back twisted data and unmodified OOB */
+ /* Align to erase block boundary */
+ block_off = off & (~(mtd->erasesize - 1));
+ for (page = 0; page < pages_per_blk; page++) {
+ struct mtd_oob_ops ops;
+ loff_t addr = (loff_t)block_off;
+
+ memset(&ops, 0, sizeof(ops));
+ ops.datbuf = datbuf[page];
+ ops.oobbuf = datbuf[page] + mtd->writesize;
+ ops.len = mtd->writesize;
+ ops.ooblen = mtd->oobsize;
+ ops.mode = MTD_OPS_RAW;
+ ret = mtd_write_oob(mtd, addr, &ops);
+ if (ret < 0) {
+ printf("Error (%d) write page %08lx\n", ret, block_off);
+ ret = 1;
+ goto free_memory;
+ }
+ block_off += mtd->writesize;
+ }
+
+free_memory:
+ for (page = 0; page < pages_per_blk ; page++) {
+ if (datbuf[page])
+ free(datbuf[page]);
+ }
+ return ret;
+}
+
static int nand_dump(struct mtd_info *mtd, ulong off, int only_oob,
int repeat)
{
@@ -733,8 +847,15 @@ static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc,
}
if (strcmp(cmd, "biterr") == 0) {
- /* todo */
- return 1;
+ int bit;
+
+ if (argc != 4)
+ goto usage;
+
+ off = (int)simple_strtoul(argv[2], NULL, 16);
+ bit = (int)simple_strtoul(argv[3], NULL, 10);
+ ret = nand_biterror(mtd, off, bit);
+ return ret;
}
#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
@@ -825,7 +946,7 @@ static char nand_help_text[] =
"nand scrub [-y] off size | scrub.part partition | scrub.chip\n"
" really clean NAND erasing bad blocks (UNSAFE)\n"
"nand markbad off [...] - mark bad block(s) at offset (UNSAFE)\n"
- "nand biterr off - make a bit error at offset (UNSAFE)"
+ "nand biterr off bit - make a bit error at offset and bit position (UNSAFE)"
#ifdef CONFIG_CMD_NAND_LOCK_UNLOCK
"\n"
"nand lock [tight] [status]\n"