Merge branch '2022-09-29-dm-core-support-multiple-device-trees-in-ofnode' into next
[platform/kernel/u-boot.git] / cmd / nand.c
index a2152ec..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>
+#include <bootstage.h>
+#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>
 #include <watchdog.h>
 #include <malloc.h>
 #include <asm/byteorder.h>
 #include <jffs2/jffs2.h>
 #include <nand.h>
 
+#include "legacy-mtd-utils.h"
+
 #if defined(CONFIG_CMD_MTDPARTS)
 
 /* partition handling routines */
 int mtdparts_init(void);
-int id_parse(const char *id, const char **ret_id, u8 *dev_type, u8 *dev_num);
 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)
 {
@@ -184,7 +304,7 @@ static void do_nand_status(struct mtd_info *mtd)
 #ifdef CONFIG_ENV_OFFSET_OOB
 unsigned long nand_env_oob_offset;
 
-int do_nand_env_oob(cmd_tbl_t *cmdtp, int argc, char *const argv[])
+int do_nand_env_oob(struct cmd_tbl *cmdtp, int argc, char *const argv[])
 {
        int ret;
        uint32_t oob_buf[ENV_OFFSET_SIZE/sizeof(uint32_t)];
@@ -305,9 +425,9 @@ static void nand_print_and_set_info(int idx)
        printf("  bbt options 0x%08x\n", chip->bbt_options);
 
        /* Set geometry info */
-       setenv_hex("nand_writesize", mtd->writesize);
-       setenv_hex("nand_oobsize", mtd->oobsize);
-       setenv_hex("nand_erasesize", mtd->erasesize);
+       env_set_hex("nand_writesize", mtd->writesize);
+       env_set_hex("nand_oobsize", mtd->oobsize);
+       env_set_hex("nand_erasesize", mtd->erasesize);
 }
 
 static int raw_access(struct mtd_info *mtd, ulong addr, loff_t off,
@@ -371,7 +491,8 @@ static void adjust_size_for_badblocks(loff_t *size, loff_t offset, int dev)
        }
 }
 
-static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
+static int do_nand(struct cmd_tbl *cmdtp, int flag, int argc,
+                  char *const argv[])
 {
        int i, ret = 0;
        ulong addr;
@@ -383,7 +504,7 @@ static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
 #else
        int quiet = 0;
 #endif
-       const char *quiet_str = getenv("quiet");
+       const char *quiet_str = env_get("quiet");
        int dev = nand_curr_device;
        int repeat = flag & CMD_FLAG_REPEAT;
 
@@ -418,7 +539,7 @@ static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                        return 0;
                }
 
-               dev = (int)simple_strtoul(argv[2], NULL, 10);
+               dev = (int)dectoul(argv[2], NULL);
                set_dev(dev);
 
                return 0;
@@ -537,7 +658,7 @@ static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                if (argc < 3)
                        goto usage;
 
-               off = (int)simple_strtoul(argv[2], NULL, 16);
+               off = (int)hextoul(argv[2], NULL);
                ret = nand_dump(mtd, off, !strcmp(&cmd[4], ".oob"), repeat);
 
                return ret == 0 ? 1 : 0;
@@ -553,7 +674,7 @@ static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                if (argc < 4)
                        goto usage;
 
-               addr = (ulong)simple_strtoul(argv[2], NULL, 16);
+               addr = (ulong)hextoul(argv[2], NULL);
 
                read = strncmp(cmd, "read", 4) == 0; /* 1 = read, 0 = write */
                printf("\nNAND %s: ", read ? "read" : "write");
@@ -707,7 +828,7 @@ static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
                        goto usage;
 
                while (argc > 0) {
-                       addr = simple_strtoul(*argv, NULL, 16);
+                       addr = hextoul(*argv, NULL);
 
                        if (mtd_block_markbad(mtd, addr)) {
                                printf("block 0x%08lx NOT marked "
@@ -726,8 +847,15 @@ static int do_nand(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
        }
 
        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
@@ -818,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"
@@ -840,14 +968,14 @@ U_BOOT_CMD(
        "NAND sub-system", nand_help_text
 );
 
-static int nand_load_image(cmd_tbl_t *cmdtp, struct mtd_info *mtd,
+static int nand_load_image(struct cmd_tbl *cmdtp, struct mtd_info *mtd,
                           ulong offset, ulong addr, char *cmd)
 {
        int r;
        char *s;
        size_t cnt;
-#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
-       image_header_t *hdr;
+#if defined(CONFIG_LEGACY_IMAGE_FORMAT)
+       struct legacy_img_hdr *hdr;
 #endif
 #if defined(CONFIG_FIT)
        const void *fit_hdr = NULL;
@@ -874,9 +1002,9 @@ static int nand_load_image(cmd_tbl_t *cmdtp, struct mtd_info *mtd,
        bootstage_mark(BOOTSTAGE_ID_NAND_HDR_READ);
 
        switch (genimg_get_format ((void *)addr)) {
-#if defined(CONFIG_IMAGE_FORMAT_LEGACY)
+#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);
@@ -911,7 +1039,7 @@ static int nand_load_image(cmd_tbl_t *cmdtp, struct mtd_info *mtd,
 #if defined(CONFIG_FIT)
        /* This cannot be done earlier, we need complete FIT image in RAM first */
        if (genimg_get_format ((void *)addr) == IMAGE_FORMAT_FIT) {
-               if (!fit_check_format (fit_hdr)) {
+               if (fit_check_format(fit_hdr, IMAGE_SIZE_INVAL)) {
                        bootstage_error(BOOTSTAGE_ID_NAND_FIT_READ);
                        puts ("** Bad FIT image format\n");
                        return 1;
@@ -923,13 +1051,13 @@ static int nand_load_image(cmd_tbl_t *cmdtp, struct mtd_info *mtd,
 
        /* Loading ok, update default load address */
 
-       load_addr = addr;
+       image_load_addr = addr;
 
        return bootm_maybe_autostart(cmdtp, cmd);
 }
 
-static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc,
-                      char * const argv[])
+static int do_nandboot(struct cmd_tbl *cmdtp, int flag, int argc,
+                      char *const argv[])
 {
        char *boot_device = NULL;
        int idx;
@@ -951,7 +1079,7 @@ static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc,
                        if (argc > 3)
                                goto usage;
                        if (argc == 3)
-                               addr = simple_strtoul(argv[1], NULL, 16);
+                               addr = hextoul(argv[1], NULL);
                        else
                                addr = CONFIG_SYS_LOAD_ADDR;
 
@@ -966,20 +1094,20 @@ static int do_nandboot(cmd_tbl_t *cmdtp, int flag, int argc,
        switch (argc) {
        case 1:
                addr = CONFIG_SYS_LOAD_ADDR;
-               boot_device = getenv("bootdevice");
+               boot_device = env_get("bootdevice");
                break;
        case 2:
-               addr = simple_strtoul(argv[1], NULL, 16);
-               boot_device = getenv("bootdevice");
+               addr = hextoul(argv[1], NULL);
+               boot_device = env_get("bootdevice");
                break;
        case 3:
-               addr = simple_strtoul(argv[1], NULL, 16);
+               addr = hextoul(argv[1], NULL);
                boot_device = argv[2];
                break;
        case 4:
-               addr = simple_strtoul(argv[1], NULL, 16);
+               addr = hextoul(argv[1], NULL);
                boot_device = argv[2];
-               offset = simple_strtoul(argv[3], NULL, 16);
+               offset = hextoul(argv[3], NULL);
                break;
        default:
 #if defined(CONFIG_CMD_MTDPARTS)
@@ -997,7 +1125,7 @@ usage:
        }
        bootstage_mark(BOOTSTAGE_ID_NAND_BOOT_DEVICE);
 
-       idx = simple_strtoul(boot_device, NULL, 16);
+       idx = hextoul(boot_device, NULL);
 
        mtd = get_nand_dev_by_index(idx);
        if (!mtd) {