1 From 88271cb3ae9c68dc200d627653df96fc557c2a64 Mon Sep 17 00:00:00 2001
2 From: Weijie Gao <weijie.gao@mediatek.com>
3 Date: Mon, 25 Jul 2022 10:55:35 +0800
4 Subject: [PATCH 47/71] cmd: add a new command for NAND flash debugging
6 Add a command 'nand-ext' for NAND flash debugging:
7 - Dump a page with oob, with optional raw read support
8 - Display all bad blocks
9 - Mark a block as bad block
10 - Set a bitflip on a page
12 - Read / write data from/to any offset with any size
13 - Read / write pages with oob
14 - Erase, read and write support skip bad block or forced mode, support
15 raw mode, supporot auto-oob mode
16 - Supports operating on a specific partition
17 - No need to specify NAND device name
19 Signed-off-by: Weijie Gao <weijie.gao@mediatek.com>
23 cmd/nand-ext.c | 1062 ++++++++++++++++++++++++++++++++++++++++++++++++
24 3 files changed, 1071 insertions(+)
25 create mode 100644 cmd/nand-ext.c
29 @@ -1353,6 +1353,14 @@ config CMD_NAND_TORTURE
34 + bool "nand - extended nand utility for debugging"
35 + depends on !CMD_NAND
36 + default y if MTD_RAW_NAND || MTD_SPI_NAND || MTK_SPI_NAND
37 + select MTD_PARTITIONS
39 + NAND flash R/W and debugging support.
46 @@ -125,6 +125,7 @@ obj-y += legacy-mtd-utils.o
48 obj-$(CONFIG_CMD_MUX) += mux.o
49 obj-$(CONFIG_CMD_NAND) += nand.o
50 +obj-$(CONFIG_CMD_NAND_EXT) += nand-ext.o
51 obj-$(CONFIG_CMD_NMBM) += nmbm.o
52 obj-$(CONFIG_CMD_NET) += net.o
53 obj-$(CONFIG_CMD_NVEDIT_EFI) += nvedit_efi.o
57 +// SPDX-License-Identifier: GPL-2.0
59 + * Copyright (C) 2021 MediaTek Inc. All Rights Reserved.
61 + * Author: Weijie Gao <weijie.gao@mediatek.com>
68 +#include <dm/devres.h>
69 +#include <linux/types.h>
70 +#include <linux/mtd/mtd.h>
72 +static struct mtd_info *curr_dev;
74 +static void mtd_show_parts(struct mtd_info *mtd, int level)
76 + struct mtd_info *part;
79 + list_for_each_entry(part, &mtd->partitions, node) {
80 + for (i = 0; i < level; i++)
82 + printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
83 + part->offset, part->offset + part->size, part->name);
85 + mtd_show_parts(part, level + 1);
89 +static void mtd_show_device(struct mtd_info *mtd)
92 + printf("* %s\n", mtd->name);
93 +#if defined(CONFIG_DM)
95 + printf(" - device: %s\n", mtd->dev->name);
96 + printf(" - parent: %s\n", mtd->dev->parent->name);
97 + printf(" - driver: %s\n", mtd->dev->driver->name);
101 + /* MTD device information */
102 + printf(" - type: ");
103 + switch (mtd->type) {
104 + case MTD_NANDFLASH:
105 + printf("NAND flash\n");
107 + case MTD_MLCNANDFLASH:
108 + printf("MLC NAND flash\n");
112 + printf("Not supported\n");
116 + printf(" - block size: 0x%x bytes\n", mtd->erasesize);
117 + printf(" - page size: 0x%x bytes\n", mtd->writesize);
118 + printf(" - OOB size: %u bytes\n", mtd->oobsize);
119 + printf(" - OOB available: %u bytes\n", mtd->oobavail);
121 + if (mtd->ecc_strength) {
122 + printf(" - ECC strength: %u bits\n", mtd->ecc_strength);
123 + printf(" - ECC step size: %u bytes\n", mtd->ecc_step_size);
124 + printf(" - bitflip threshold: %u bits\n",
125 + mtd->bitflip_threshold);
128 + printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
129 + mtd->offset, mtd->offset + mtd->size, mtd->name);
131 + /* MTD partitions, if any */
132 + mtd_show_parts(mtd, 1);
135 +static int do_nand_list(struct cmd_tbl *cmdtp, int flag, int argc,
136 + char *const argv[])
138 + struct mtd_info *mtd;
141 + /* Ensure all devices (and their partitions) are probed */
142 + mtd_probe_devices();
144 + printf("List of NAND devices:\n");
145 + mtd_for_each_device(mtd) {
146 + if (mtd->type != MTD_NANDFLASH && mtd->type != MTD_MLCNANDFLASH)
149 + if (!mtd_is_partition(mtd))
150 + mtd_show_device(mtd);
156 + printf("No NAND MTD device found\n");
158 + return CMD_RET_SUCCESS;
161 +static struct mtd_info *nand_get_curr_dev(void)
163 + struct mtd_info *mtd, *first_dev = NULL;
164 + int err, dev_nb = 0;
167 + mtd = get_mtd_device(curr_dev, -1);
168 + if (!IS_ERR_OR_NULL(mtd)) {
169 + __put_mtd_device(mtd);
176 + /* Ensure all devices (and their partitions) are probed */
177 + mtd_probe_devices();
179 + mtd_for_each_device(mtd) {
180 + if (mtd->type != MTD_NANDFLASH && mtd->type != MTD_MLCNANDFLASH)
183 + if (!mtd_is_partition(mtd)) {
191 + printf("No NAND MTD device found\n");
196 + printf("No active NAND MTD device specified\n");
200 + err = __get_mtd_device(first_dev);
202 + printf("Failed to get MTD device '%s': err %d\n",
203 + first_dev->name, err);
207 + curr_dev = first_dev;
209 + printf("'%s' is now active device\n", first_dev->name);
214 +static struct mtd_info *nand_get_part(struct mtd_info *master,
217 + struct mtd_info *slave;
219 + list_for_each_entry(slave, &master->partitions, node) {
220 + if (!strcmp(slave->name, name))
227 +static int do_nand_info(struct cmd_tbl *cmdtp, int flag, int argc,
228 + char *const argv[])
230 + struct mtd_info *mtd = nand_get_curr_dev();
233 + return CMD_RET_FAILURE;
235 + mtd_show_device(mtd);
240 +static int do_nand_select(struct cmd_tbl *cmdtp, int flag, int argc,
241 + char *const argv[])
243 + struct mtd_info *mtd, *old;
246 + printf("MTD device name must be specified\n");
247 + return CMD_RET_USAGE;
250 + mtd = get_mtd_device_nm(argv[1]);
252 + printf("MTD device '%s' not found\n", argv[1]);
253 + return CMD_RET_FAILURE;
256 + if (mtd_is_partition(mtd)) {
257 + printf("Error: '%s' is a MTD partition\n", argv[1]);
258 + __put_mtd_device(mtd);
259 + return CMD_RET_FAILURE;
262 + if (mtd->type != MTD_NANDFLASH && mtd->type != MTD_MLCNANDFLASH) {
263 + printf("Error: '%s' is not a NAND device\n", argv[1]);
264 + __put_mtd_device(mtd);
265 + return CMD_RET_FAILURE;
268 + if (mtd == curr_dev) {
269 + __put_mtd_device(mtd);
270 + return CMD_RET_SUCCESS;
274 + old = get_mtd_device(curr_dev, -1);
275 + if (!IS_ERR_OR_NULL(old)) {
276 + __put_mtd_device(old);
277 + __put_mtd_device(curr_dev);
285 + printf("'%s' is now active device\n", curr_dev->name);
287 + return CMD_RET_SUCCESS;
290 +static void dump_buf(const u8 *data, size_t size, u64 addr)
292 + const u8 *p = data;
298 + chklen = (u32)size;
300 + printf("%08llx: ", addr);
302 + for (i = 0; i < chklen; i++) {
303 + if (i && (i % 4 == 0))
306 + printf("%02x ", p[i]);
309 + for (i = chklen; i < 16; i++) {
310 + if (i && (i % 4 == 0))
317 + for (i = 0; i < chklen; i++) {
318 + if (p[i] < 32 || p[i] >= 0x7f)
321 + printf("%c", p[i]);
331 +static int do_nand_dump(struct cmd_tbl *cmdtp, int flag, int argc,
332 + char *const argv[])
334 + struct mtd_info *mtd = nand_get_curr_dev();
335 + struct mtd_oob_ops io_op = {};
342 + return CMD_RET_FAILURE;
344 + if (strstr(argv[0], ".raw"))
348 + printf("Dump offset must be specified\n");
349 + return CMD_RET_USAGE;
352 + off = simple_strtoull(argv[1], NULL, 0);
353 + if (off >= mtd->size) {
354 + printf("Offset 0x%llx is larger than flash size\n", off);
355 + return CMD_RET_FAILURE;
358 + off &= ~(u64)mtd->writesize_mask;
360 + buf = malloc(mtd->writesize + mtd->oobsize);
362 + printf("Failed to allocate buffer\n");
363 + return CMD_RET_FAILURE;
366 + io_op.mode = raw ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB;
367 + io_op.len = mtd->writesize;
368 + io_op.datbuf = buf;
369 + io_op.ooblen = mtd->oobsize;
370 + io_op.oobbuf = buf + mtd->writesize;
372 + ret = mtd_read_oob(mtd, off, &io_op);
373 + if (ret < 0 && ret != -EUCLEAN && ret != -EBADMSG) {
374 + printf("Failed to read page at 0x%llx, err %d\n", off, ret);
376 + return CMD_RET_FAILURE;
379 + printf("Dump of %spage at 0x%llx:\n", raw ? "raw " : "", off);
380 + dump_buf(buf, mtd->writesize, off);
384 + dump_buf(buf + mtd->writesize, mtd->oobsize, 0);
388 + return CMD_RET_SUCCESS;
391 +static int do_nand_bad(struct cmd_tbl *cmdtp, int flag, int argc,
392 + char *const argv[])
394 + struct mtd_info *mtd = nand_get_curr_dev();
398 + return CMD_RET_FAILURE;
400 + while (off < mtd->size) {
401 + if (mtd_block_isbad(mtd, off))
402 + printf("\t%08llx\n", off);
404 + off += mtd->erasesize;
410 +static int do_nand_markbad(struct cmd_tbl *cmdtp, int flag, int argc,
411 + char *const argv[])
413 + struct mtd_info *mtd = nand_get_curr_dev();
418 + return CMD_RET_FAILURE;
421 + printf("Missing address within a block to be marked bad\n");
422 + return CMD_RET_USAGE;
425 + off = simple_strtoull(argv[1], NULL, 0);
426 + if (off >= mtd->size) {
427 + printf("Offset 0x%llx is larger than flash size\n", off);
428 + return CMD_RET_FAILURE;
431 + off &= ~(u64)mtd->erasesize_mask;
433 + ret = mtd_block_markbad(mtd, off);
436 + printf("Block at 0x%08llx has been marked bad\n", off);
438 + printf("Failed to mark bad block at 0x%08llx\n", off);
440 + return ret ? CMD_RET_FAILURE : CMD_RET_SUCCESS;
443 +static int do_nand_bitflip(struct cmd_tbl *cmdtp, int flag, int argc,
444 + char *const argv[])
446 + struct mtd_info *mtd = nand_get_curr_dev();
447 + struct mtd_oob_ops io_op = {};
455 + return CMD_RET_FAILURE;
458 + printf("Missing address to generate bitflip\n");
459 + return CMD_RET_USAGE;
462 + off = simple_strtoull(argv[1], NULL, 0);
463 + if (off >= mtd->size) {
464 + printf("Offset 0x%llx is larger than flash size\n", off);
465 + return CMD_RET_FAILURE;
469 + printf("Missing column address\n");
470 + return CMD_RET_USAGE;
473 + col = simple_strtoul(argv[2], NULL, 0);
474 + if (col >= mtd->writesize + mtd->oobsize) {
475 + printf("Column address must be less than %u\n",
476 + mtd->writesize + mtd->oobsize);
477 + return CMD_RET_FAILURE;
481 + printf("Missing bit position\n");
482 + return CMD_RET_USAGE;
485 + bit = simple_strtoul(argv[3], NULL, 0);
487 + printf("Bit position must be less than 8\n");
488 + return CMD_RET_FAILURE;
491 + off &= ~(u64)mtd->writesize_mask;
493 + buf = malloc(mtd->writesize + mtd->oobsize);
495 + printf("Failed to allocate buffer\n");
496 + return CMD_RET_FAILURE;
499 + io_op.mode = MTD_OPS_RAW;
500 + io_op.len = mtd->writesize;
501 + io_op.datbuf = buf;
502 + io_op.ooblen = mtd->oobsize;
503 + io_op.oobbuf = buf + mtd->writesize;
505 + ret = mtd_read_oob(mtd, off, &io_op);
507 + printf("Failed to read page at 0x%llx, err %d\n", off, ret);
509 + return CMD_RET_FAILURE;
512 + if (!(buf[col] & (1 << bit))) {
513 + printf("Bit %u at byte %u is already zero\n", bit, col);
515 + return CMD_RET_FAILURE;
518 + buf[col] &= ~(1 << bit);
520 + memset(&io_op, 0, sizeof(io_op));
521 + io_op.mode = MTD_OPS_RAW;
522 + io_op.len = mtd->writesize;
523 + io_op.datbuf = buf;
524 + io_op.ooblen = mtd->oobsize;
525 + io_op.oobbuf = buf + mtd->writesize;
527 + ret = mtd_write_oob(mtd, off, &io_op);
530 + printf("Failed to write page at 0x%llx, err %d\n", off, ret);
531 + return CMD_RET_FAILURE;
534 + memset(&io_op, 0, sizeof(io_op));
535 + io_op.mode = MTD_OPS_RAW;
536 + io_op.len = mtd->writesize;
537 + io_op.datbuf = buf;
538 + io_op.ooblen = mtd->oobsize;
539 + io_op.oobbuf = buf + mtd->writesize;
541 + ret = mtd_read_oob(mtd, off, &io_op);
543 + printf("Failed to read page at 0x%llx, err %d\n", off, ret);
545 + return CMD_RET_FAILURE;
548 + res = (buf[col] & (1 << bit)) == 0;
552 + printf("Bit %u at byte %u has been changed to 0\n", bit, col);
553 + return CMD_RET_SUCCESS;
556 + printf("Failed to change bit %u at byte %u to 0\n", bit, col);
557 + return CMD_RET_FAILURE;
560 +static int do_nand_erase(struct cmd_tbl *cmdtp, int flag, int argc,
561 + char *const argv[])
563 + struct mtd_info *mtd = nand_get_curr_dev(), *part;
564 + bool spread = false, force = false;
565 + u64 off, size, end, limit;
566 + struct erase_info ei;
571 + return CMD_RET_FAILURE;
573 + if (strstr(argv[0], ".spread"))
576 + if (strstr(argv[0], ".force"))
579 + if (spread && force) {
580 + printf("spread and force must not be set at the same time\n");
581 + return CMD_RET_FAILURE;
585 + printf("Erase start offset/partition must be specified\n");
586 + return CMD_RET_USAGE;
589 + part = nand_get_part(mtd, argv[1]);
591 + off = part->offset;
596 + size = simple_strtoull(argv[2], NULL, 0);
598 + if (size > part->size) {
599 + printf("Erase end offset is larger than partition size\n");
600 + printf("Erase size reduced to 0x%llx\n", part->size);
605 + limit = off + part->size;
607 + off = simple_strtoull(argv[1], &ends, 0);
609 + if (ends == argv[1] || *ends) {
610 + printf("Partition '%s' not found\n", argv[1]);
611 + return CMD_RET_FAILURE;
614 + if (off >= mtd->size) {
615 + printf("Offset 0x%llx is larger than flash size\n", off);
616 + return CMD_RET_FAILURE;
620 + printf("Erase size offset must be specified\n");
621 + return CMD_RET_USAGE;
624 + size = simple_strtoull(argv[2], NULL, 0);
626 + if (off + size > mtd->size) {
627 + printf("Erase end offset is larger than flash size\n");
629 + size = mtd->size - off;
630 + printf("Erase size reduced to 0x%llx\n", size);
637 + off &= ~(u64)mtd->erasesize_mask;
638 + end = (end + mtd->erasesize_mask) & (~(u64)mtd->erasesize_mask);
641 + printf("Erasing from 0x%llx to 0x%llx, size 0x%llx ...\n",
642 + off, end - 1, end - off);
644 + while (size && off < limit) {
645 + if (mtd_block_isbad(mtd, off)) {
646 + printf("Bad block at 0x%llx", off);
649 + printf(" ... skipped\n");
650 + off += mtd->erasesize;
655 + printf(" ... aborted\n");
656 + return CMD_RET_FAILURE;
659 + printf(" ... will be force erased\n");
662 + memset(&ei, 0, sizeof(ei));
666 + ei.len = mtd->erasesize;
669 + ret = mtd_erase(mtd, &ei);
671 + printf("Erase failed at 0x%llx\n", off);
672 + return CMD_RET_FAILURE;
675 + off += mtd->erasesize;
676 + size -= mtd->erasesize;
679 + printf("Succeeded\n");
681 + return CMD_RET_SUCCESS;
684 +static bool is_empty_page(const u8 *buf, size_t size)
688 + for (i = 0; i < size; i++) {
689 + if (buf[i] != 0xff)
696 +static int do_nand_io_normal(int argc, char *const argv[])
698 + struct mtd_info *mtd = nand_get_curr_dev(), *part;
699 + bool spread = false, force = false, raw = false, writeff = false;
700 + bool read = false, checkbad = true;
701 + struct mtd_oob_ops io_op = {};
702 + size_t size, padding, chksz;
710 + return CMD_RET_FAILURE;
712 + if (!strncmp(argv[0], "read", 4))
715 + if (strstr(argv[0], ".spread"))
718 + if (strstr(argv[0], ".force"))
721 + if (strstr(argv[0], ".raw"))
724 + if (strstr(argv[0], ".ff"))
727 + if (spread && force) {
728 + printf("spread and force must not be set at the same time\n");
729 + return CMD_RET_FAILURE;
733 + printf("Data address must be specified\n");
734 + return CMD_RET_USAGE;
737 + addr = simple_strtoul(argv[1], NULL, 0);
740 + printf("Flash address/partition must be specified\n");
741 + return CMD_RET_USAGE;
744 + part = nand_get_part(mtd, argv[2]);
749 + off = simple_strtoull(argv[3], NULL, 0);
750 + if (off + part->offset >= part->size) {
751 + printf("Offset is larger than partition size\n");
752 + return CMD_RET_FAILURE;
757 + size = part->size - off;
759 + size = simple_strtoul(argv[4], NULL, 0);
760 + if (off + size > part->size) {
761 + printf("Data size is too large\n");
762 + return CMD_RET_FAILURE;
766 + off += part->offset;
768 + off = simple_strtoull(argv[2], &ends, 0);
770 + if (ends == argv[1] || *ends) {
771 + printf("Partition '%s' not found\n", argv[2]);
772 + return CMD_RET_FAILURE;
775 + if (off >= mtd->size) {
776 + printf("Offset 0x%llx is larger than flash size\n", off);
777 + return CMD_RET_FAILURE;
781 + printf("Data size must be specified\n");
782 + return CMD_RET_USAGE;
785 + size = simple_strtoul(argv[3], NULL, 0);
786 + if (off + size > mtd->size) {
787 + printf("Data size is too large\n");
788 + return CMD_RET_FAILURE;
792 + buf = malloc(mtd->writesize);
794 + printf("Failed to allocate buffer\n");
795 + return CMD_RET_FAILURE;
798 + printf("%s from 0x%llx to 0x%llx, size 0x%zx ...\n",
799 + read ? "Reading" : "Writing", off, off + size - 1, size);
801 + while (size && off < mtd->size) {
802 + if (checkbad || !(off & mtd->erasesize_mask)) {
803 + offp = off & ~(u64)mtd->erasesize_mask;
805 + if (mtd_block_isbad(mtd, offp)) {
806 + printf("Bad block at 0x%llx", offp);
809 + printf(" ... skipped\n");
810 + off += mtd->erasesize;
816 + printf(" ... aborted\n");
820 + printf(" ... continue\n");
826 + padding = off & mtd->writesize_mask;
827 + chksz = mtd->writesize - padding;
828 + chksz = min_t(size_t, chksz, size);
830 + offp = off & ~(u64)mtd->writesize_mask;
832 + memset(&io_op, 0, sizeof(io_op));
833 + io_op.mode = raw ? MTD_OPS_RAW : MTD_OPS_PLACE_OOB;
834 + io_op.len = mtd->writesize;
836 + if (chksz < mtd->writesize)
837 + io_op.datbuf = buf;
839 + io_op.datbuf = (void *)addr;
842 + ret = mtd_read_oob(mtd, offp, &io_op);
843 + if (ret && ret != -EUCLEAN && ret != -EBADMSG)
846 + if (chksz < mtd->writesize)
847 + memcpy((void *)addr, buf + padding, chksz);
849 + if (chksz < mtd->writesize) {
850 + memset(buf, 0xff, mtd->writesize);
851 + memcpy(buf + padding, (void *)addr, chksz);
854 + if (is_empty_page(io_op.datbuf, io_op.len) && !writeff)
857 + ret = mtd_write_oob(mtd, offp, &io_op);
869 + printf("Succeeded\n");
870 + ret = CMD_RET_SUCCESS;
874 + printf("0x%zx byte%s remained for %s\n", size, size > 1 ? "s" : "",
875 + read ? "read" : "write");
879 + printf("%s error %d at 0x%llx\n", read ? "Read" : "Write", ret, offp);
882 + ret = CMD_RET_FAILURE;
889 +static int do_nand_io_page(int argc, char *const argv[])
891 + struct mtd_info *mtd = nand_get_curr_dev(), *part;
892 + bool spread = false, force = false, raw = false, autooob = false;
893 + bool read = false, checkbad = true, writeff = false;
894 + struct mtd_oob_ops io_op = {};
902 + return CMD_RET_FAILURE;
904 + if (!strncmp(argv[0], "read", 4))
907 + if (strstr(argv[0], ".spread"))
910 + if (strstr(argv[0], ".force"))
913 + if (strstr(argv[0], ".raw"))
916 + if (strstr(argv[0], ".auto"))
919 + if (spread && force) {
920 + printf("spread and force must not be set at the same time\n");
921 + return CMD_RET_FAILURE;
924 + if (raw && autooob) {
925 + printf("raw and auto must not be set at the same time\n");
926 + return CMD_RET_FAILURE;
930 + printf("Data address must be specified\n");
931 + return CMD_RET_USAGE;
934 + addr = simple_strtoul(argv[1], NULL, 0);
937 + printf("Flash address/partition must be specified\n");
938 + return CMD_RET_USAGE;
941 + part = nand_get_part(mtd, argv[2]);
944 + printf("Partition offset / page count must be specified\n");
945 + return CMD_RET_USAGE;
951 + count = simple_strtoul(argv[3], NULL, 0);
952 + if (part->offset + count * mtd->writesize > part->size) {
953 + printf("Page count exceeds partition size\n");
954 + return CMD_RET_FAILURE;
957 + off = simple_strtoull(argv[3], NULL, 0);
958 + if (off >= part->size) {
959 + printf("Offset 0x%llx is larger than partition size\n", off);
960 + return CMD_RET_FAILURE;
963 + off &= ~(u64)mtd->writesize_mask;
965 + count = simple_strtoul(argv[4], NULL, 0);
966 + if (part->offset + off + count * mtd->writesize > part->size) {
967 + printf("Page count exceeds partition size\n");
968 + return CMD_RET_FAILURE;
972 + off += part->offset;
974 + off = simple_strtoull(argv[2], &ends, 0);
976 + if (ends == argv[1] || *ends) {
977 + printf("Partition '%s' not found\n", argv[2]);
978 + return CMD_RET_FAILURE;
981 + if (off >= mtd->size) {
982 + printf("Offset 0x%llx is larger than flash size\n", off);
983 + return CMD_RET_FAILURE;
986 + off &= ~(u64)mtd->writesize_mask;
989 + printf("Page count must be specified\n");
990 + return CMD_RET_USAGE;
993 + count = simple_strtoul(argv[3], NULL, 0);
994 + if (off + count * mtd->writesize > mtd->size) {
995 + printf("Page count exceeds flash size\n");
996 + return CMD_RET_FAILURE;
1000 + printf("%s from 0x%llx to 0x%llx (+%u), count %u ...\n",
1001 + read ? "Reading" : "Writing", off,
1002 + off + count * mtd->writesize - 1, mtd->oobsize, count);
1004 + while (count && off < mtd->size) {
1005 + if (checkbad || !(off & mtd->erasesize_mask)) {
1006 + offp = off & ~(u64)mtd->erasesize_mask;
1008 + if (mtd_block_isbad(mtd, offp)) {
1009 + printf("Bad block at 0x%llx", offp);
1012 + printf(" ... skipped\n");
1013 + off += mtd->erasesize;
1019 + printf(" ... aborted\n");
1020 + return CMD_RET_FAILURE;
1023 + printf(" ... continue\n");
1029 + memset(&io_op, 0, sizeof(io_op));
1032 + io_op.mode = MTD_OPS_RAW;
1034 + io_op.mode = MTD_OPS_AUTO_OOB;
1036 + io_op.mode = MTD_OPS_PLACE_OOB;
1038 + io_op.len = mtd->writesize;
1039 + io_op.ooblen = mtd->oobsize;
1040 + io_op.datbuf = (void *)addr;
1041 + io_op.oobbuf = io_op.datbuf + mtd->writesize;
1044 + ret = mtd_read_oob(mtd, off, &io_op);
1045 + if (ret && ret != -EUCLEAN && ret != -EBADMSG)
1048 + if (is_empty_page((void *)addr, mtd->writesize + mtd->oobsize) && !writeff)
1051 + ret = mtd_write_oob(mtd, off, &io_op);
1058 + addr += mtd->writesize + mtd->oobsize;
1059 + off += mtd->writesize;
1063 + printf("Succeeded\n");
1064 + return CMD_RET_SUCCESS;
1067 + printf("%u page%s remained for %s\n", count, count > 1 ? "s" : "",
1068 + read ? "read" : "write");
1069 + return CMD_RET_FAILURE;
1072 + printf("%s error %d at 0x%llx\n", read ? "Read" : "Write", ret, off);
1073 + return CMD_RET_FAILURE;
1076 +static int do_nand_io(struct cmd_tbl *cmdtp, int flag, int argc,
1077 + char *const argv[])
1079 + if (strstr(argv[0], ".oob"))
1080 + return do_nand_io_page(argc, argv);
1082 + return do_nand_io_normal(argc, argv);
1085 +#ifdef CONFIG_SYS_LONGHELP
1086 +static char nand_help_text[] =
1087 + "- NAND flash R/W and debugging utility\n"
1089 + "nand info - Show active NAND devices\n"
1090 + "nand select <name> - Select active NAND devices\n"
1091 + "nand dump[.raw] <off>\n"
1093 + "nand markbad <off>\n"
1094 + "nand bitflip <off> <col> <bit>\n"
1095 + "nand erase[.spread|.force] [<off> <size>|<part> [<size>]]\n"
1096 + "nand read[.spread|.force][.raw] <addr> <off> <size>\n"
1097 + " <addr> <part> [<off> [<size>]]\n"
1098 + "nand write[.spread|.force][.raw][.ff] <addr> <off> <size>\n"
1099 + " <addr> <part> [<off> [<size>]]\n"
1100 + "nand read.oob[.spread|.force][.raw|.auto] <addr> <off> <count>\n"
1101 + " <addr> <part> [<off>] <count>\n"
1102 + "nand write.oob[.spread|.force][.raw|.auto][.ff] <addr> <off> <count>\n"
1103 + " <addr> <part> [<off>] <count>\n";
1106 +U_BOOT_CMD_WITH_SUBCMDS(nand, "NAND utility",
1108 + U_BOOT_SUBCMD_MKENT(list, 1, 0, do_nand_list),
1109 + U_BOOT_SUBCMD_MKENT(info, 1, 0, do_nand_info),
1110 + U_BOOT_SUBCMD_MKENT(select, 2, 0, do_nand_select),
1111 + U_BOOT_SUBCMD_MKENT(dump, 2, 0, do_nand_dump),
1112 + U_BOOT_SUBCMD_MKENT(bad, 1, 0, do_nand_bad),
1113 + U_BOOT_SUBCMD_MKENT(markbad, 2, 0, do_nand_markbad),
1114 + U_BOOT_SUBCMD_MKENT(bitflip, 4, 0, do_nand_bitflip),
1115 + U_BOOT_SUBCMD_MKENT(erase, 3, 0, do_nand_erase),
1116 + U_BOOT_SUBCMD_MKENT(read, 5, 0, do_nand_io),
1117 + U_BOOT_SUBCMD_MKENT(write, 5, 0, do_nand_io)