Merge branch '2022-09-29-dm-core-support-multiple-device-trees-in-ofnode' into next
[platform/kernel/u-boot.git] / cmd / nand.c
index d381053..5bb4379 100644 (file)
  * 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>
@@ -24,6 +28,7 @@
 #include <image.h>
 #include <asm/cache.h>
 #include <linux/mtd/mtd.h>
+#include <linux/mtd/rawnand.h>
 #include <command.h>
 #include <console.h>
 #include <env.h>
@@ -43,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)
 {
@@ -732,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
@@ -824,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"
@@ -853,7 +975,7 @@ static int nand_load_image(struct cmd_tbl *cmdtp, struct mtd_info *mtd,
        char *s;
        size_t cnt;
 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
-       image_header_t *hdr;
+       struct legacy_img_hdr *hdr;
 #endif
 #if defined(CONFIG_FIT)
        const void *fit_hdr = NULL;
@@ -882,7 +1004,7 @@ static int nand_load_image(struct cmd_tbl *cmdtp, struct mtd_info *mtd,
        switch (genimg_get_format ((void *)addr)) {
 #if defined(CONFIG_LEGACY_IMAGE_FORMAT)
        case IMAGE_FORMAT_LEGACY:
-               hdr = (image_header_t *)addr;
+               hdr = (struct legacy_img_hdr *)addr;
 
                bootstage_mark(BOOTSTAGE_ID_NAND_TYPE);
                image_print_contents (hdr);