1 // SPDX-License-Identifier: GPL-2.0+
5 * Generic command to handle basic operations on any memory device.
7 * Copyright: Bootlin, 2018
13 #if CONFIG_IS_ENABLED(CMD_MTD_OTP)
19 #include <dm/devres.h>
20 #include <linux/err.h>
22 #include <linux/ctype.h>
24 static struct mtd_info *get_mtd_by_name(const char *name)
30 mtd = get_mtd_device_nm(name);
31 if (IS_ERR_OR_NULL(mtd))
32 printf("MTD device %s not found, ret %ld\n", name,
38 static uint mtd_len_to_pages(struct mtd_info *mtd, u64 len)
40 do_div(len, mtd->writesize);
45 static bool mtd_is_aligned_with_min_io_size(struct mtd_info *mtd, u64 size)
47 return !do_div(size, mtd->writesize);
50 static bool mtd_is_aligned_with_block_size(struct mtd_info *mtd, u64 size)
52 return !do_div(size, mtd->erasesize);
55 static void mtd_dump_buf(const u8 *buf, uint len, uint offset)
59 for (i = 0; i < len; ) {
60 printf("0x%08x:\t", offset + i);
61 for (j = 0; j < 8; j++)
62 printf("%02x ", buf[i + j]);
65 for (j = 0; j < 8; j++)
66 printf("%02x ", buf[i + j]);
72 static void mtd_dump_device_buf(struct mtd_info *mtd, u64 start_off,
73 const u8 *buf, u64 len, bool woob)
75 bool has_pages = mtd->type == MTD_NANDFLASH ||
76 mtd->type == MTD_MLCNANDFLASH;
77 int npages = mtd_len_to_pages(mtd, len);
81 for (page = 0; page < npages; page++) {
82 u64 data_off = (u64)page * mtd->writesize;
84 printf("\nDump %d data bytes from 0x%08llx:\n",
85 mtd->writesize, start_off + data_off);
86 mtd_dump_buf(&buf[data_off],
87 mtd->writesize, start_off + data_off);
90 u64 oob_off = (u64)page * mtd->oobsize;
92 printf("Dump %d OOB bytes from page at 0x%08llx:\n",
93 mtd->oobsize, start_off + data_off);
94 mtd_dump_buf(&buf[len + oob_off],
99 printf("\nDump %lld data bytes from 0x%llx:\n",
101 mtd_dump_buf(buf, len, start_off);
105 static void mtd_show_parts(struct mtd_info *mtd, int level)
107 struct mtd_info *part;
110 list_for_each_entry(part, &mtd->partitions, node) {
111 for (i = 0; i < level; i++)
113 printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
114 part->offset, part->offset + part->size, part->name);
116 mtd_show_parts(part, level + 1);
120 static void mtd_show_device(struct mtd_info *mtd)
123 printf("* %s\n", mtd->name);
124 #if defined(CONFIG_DM)
126 printf(" - device: %s\n", mtd->dev->name);
127 printf(" - parent: %s\n", mtd->dev->parent->name);
128 printf(" - driver: %s\n", mtd->dev->driver->name);
131 if (IS_ENABLED(CONFIG_OF_CONTROL) && mtd->dev) {
135 res = ofnode_get_path(mtd_get_ofnode(mtd), buf, 256);
136 printf(" - path: %s\n", res == 0 ? buf : "unavailable");
139 /* MTD device information */
149 printf("NOR flash\n");
152 printf("NAND flash\n");
155 printf("Data flash\n");
158 printf("UBI volume\n");
160 case MTD_MLCNANDFLASH:
161 printf("MLC NAND flash\n");
169 printf(" - block size: 0x%x bytes\n", mtd->erasesize);
170 printf(" - min I/O: 0x%x bytes\n", mtd->writesize);
173 printf(" - OOB size: %u bytes\n", mtd->oobsize);
174 printf(" - OOB available: %u bytes\n", mtd->oobavail);
177 if (mtd->ecc_strength) {
178 printf(" - ECC strength: %u bits\n", mtd->ecc_strength);
179 printf(" - ECC step size: %u bytes\n", mtd->ecc_step_size);
180 printf(" - bitflip threshold: %u bits\n",
181 mtd->bitflip_threshold);
184 printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
185 mtd->offset, mtd->offset + mtd->size, mtd->name);
187 /* MTD partitions, if any */
188 mtd_show_parts(mtd, 1);
191 /* Logic taken from fs/ubifs/recovery.c:is_empty() */
192 static bool mtd_oob_write_is_empty(struct mtd_oob_ops *op)
196 for (i = 0; i < op->len; i++)
197 if (op->datbuf[i] != 0xff)
200 for (i = 0; i < op->ooblen; i++)
201 if (op->oobbuf[i] != 0xff)
207 #if CONFIG_IS_ENABLED(CMD_MTD_OTP)
208 static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
211 struct mtd_info *mtd;
220 return CMD_RET_USAGE;
222 if (!strcmp(argv[2], "u"))
224 else if (!strcmp(argv[2], "f"))
227 return CMD_RET_USAGE;
229 mtd = get_mtd_by_name(argv[1]);
230 if (IS_ERR_OR_NULL(mtd))
231 return CMD_RET_FAILURE;
233 from = simple_strtoul(argv[3], NULL, 0);
234 len = simple_strtoul(argv[4], NULL, 0);
236 ret = CMD_RET_FAILURE;
242 printf("Reading %s OTP from 0x%lx, %zu bytes\n",
243 user ? "user" : "factory", from, len);
246 ret = mtd_read_user_prot_reg(mtd, from, len, &retlen, buf);
248 ret = mtd_read_fact_prot_reg(mtd, from, len, &retlen, buf);
251 pr_err("OTP read failed: %d\n", ret);
252 ret = CMD_RET_FAILURE;
257 pr_err("OTP read returns %zu, but %zu expected\n",
260 print_hex_dump("", 0, 16, 1, buf, retlen, true);
264 ret = CMD_RET_SUCCESS;
272 static int do_mtd_otp_lock(struct cmd_tbl *cmdtp, int flag, int argc,
275 struct mtd_info *mtd;
281 return CMD_RET_USAGE;
283 mtd = get_mtd_by_name(argv[1]);
284 if (IS_ERR_OR_NULL(mtd))
285 return CMD_RET_FAILURE;
287 from = simple_strtoul(argv[2], NULL, 0);
288 len = simple_strtoul(argv[3], NULL, 0);
290 ret = mtd_lock_user_prot_reg(mtd, from, len);
292 pr_err("OTP lock failed: %d\n", ret);
293 ret = CMD_RET_FAILURE;
297 ret = CMD_RET_SUCCESS;
305 static int do_mtd_otp_write(struct cmd_tbl *cmdtp, int flag, int argc,
308 struct mtd_info *mtd;
316 return CMD_RET_USAGE;
318 mtd = get_mtd_by_name(argv[1]);
319 if (IS_ERR_OR_NULL(mtd))
320 return CMD_RET_FAILURE;
322 from = simple_strtoul(argv[2], NULL, 0);
323 binlen = strlen(argv[3]) / 2;
325 ret = CMD_RET_FAILURE;
326 binbuf = malloc(binlen);
330 hex2bin(binbuf, argv[3], binlen);
332 printf("Will write:\n");
334 print_hex_dump("", 0, 16, 1, binbuf, binlen, true);
336 printf("to 0x%lx\n", from);
338 printf("Continue (y/n)?\n");
340 if (confirm_yesno() != 1) {
341 pr_err("OTP write canceled\n");
342 ret = CMD_RET_SUCCESS;
346 ret = mtd_write_user_prot_reg(mtd, from, binlen, &retlen, binbuf);
348 pr_err("OTP write failed: %d\n", ret);
349 ret = CMD_RET_FAILURE;
353 if (retlen != binlen)
354 pr_err("OTP write returns %zu, but %zu expected\n",
357 ret = CMD_RET_SUCCESS;
366 static int do_mtd_otp_info(struct cmd_tbl *cmdtp, int flag, int argc,
369 struct otp_info otp_info;
370 struct mtd_info *mtd;
376 return CMD_RET_USAGE;
378 if (!strcmp(argv[2], "u"))
380 else if (!strcmp(argv[2], "f"))
383 return CMD_RET_USAGE;
385 mtd = get_mtd_by_name(argv[1]);
386 if (IS_ERR_OR_NULL(mtd))
387 return CMD_RET_FAILURE;
390 ret = mtd_get_user_prot_info(mtd, sizeof(otp_info), &retlen,
393 ret = mtd_get_fact_prot_info(mtd, sizeof(otp_info), &retlen,
396 pr_err("OTP info failed: %d\n", ret);
397 ret = CMD_RET_FAILURE;
401 if (retlen != sizeof(otp_info)) {
402 pr_err("OTP info returns %zu, but %zu expected\n",
403 retlen, sizeof(otp_info));
404 ret = CMD_RET_FAILURE;
408 printf("%s OTP region info:\n", user ? "User" : "Factory");
409 printf("\tstart: %u\n", otp_info.start);
410 printf("\tlength: %u\n", otp_info.length);
411 printf("\tlocked: %u\n", otp_info.locked);
413 ret = CMD_RET_SUCCESS;
422 static int do_mtd_list(struct cmd_tbl *cmdtp, int flag, int argc,
425 struct mtd_info *mtd;
428 /* Ensure all devices (and their partitions) are probed */
431 printf("List of MTD devices:\n");
432 mtd_for_each_device(mtd) {
433 if (!mtd_is_partition(mtd))
434 mtd_show_device(mtd);
440 printf("No MTD device found\n");
441 return CMD_RET_FAILURE;
444 return CMD_RET_SUCCESS;
447 static int mtd_special_write_oob(struct mtd_info *mtd, u64 off,
448 struct mtd_oob_ops *io_op,
449 bool write_empty_pages, bool woob)
454 * By default, do not write an empty page.
455 * Skip it by simulating a successful write.
457 if (!write_empty_pages && mtd_oob_write_is_empty(io_op)) {
458 io_op->retlen = mtd->writesize;
459 io_op->oobretlen = woob ? mtd->oobsize : 0;
461 ret = mtd_write_oob(mtd, off, io_op);
467 static int do_mtd_io(struct cmd_tbl *cmdtp, int flag, int argc,
470 bool dump, read, raw, woob, write_empty_pages, has_pages = false;
471 u64 start_off, off, len, remaining, default_len;
472 struct mtd_oob_ops io_op = {};
473 uint user_addr = 0, npages;
474 const char *cmd = argv[0];
475 struct mtd_info *mtd;
481 return CMD_RET_USAGE;
483 mtd = get_mtd_by_name(argv[1]);
484 if (IS_ERR_OR_NULL(mtd))
485 return CMD_RET_FAILURE;
487 if (mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH)
490 dump = !strncmp(cmd, "dump", 4);
491 read = dump || !strncmp(cmd, "read", 4);
492 raw = strstr(cmd, ".raw");
493 woob = strstr(cmd, ".oob");
494 write_empty_pages = !has_pages || strstr(cmd, ".dontskipff");
505 user_addr = hextoul(argv[0], NULL);
510 start_off = argc > 0 ? hextoul(argv[0], NULL) : 0;
511 if (!mtd_is_aligned_with_min_io_size(mtd, start_off)) {
512 printf("Offset not aligned with a page (0x%x)\n",
514 ret = CMD_RET_FAILURE;
518 default_len = dump ? mtd->writesize : mtd->size;
519 len = argc > 1 ? hextoul(argv[1], NULL) : default_len;
520 if (!mtd_is_aligned_with_min_io_size(mtd, len)) {
521 len = round_up(len, mtd->writesize);
522 printf("Size not on a page boundary (0x%x), rounding to 0x%llx\n",
523 mtd->writesize, len);
527 npages = mtd_len_to_pages(mtd, len);
528 oob_len = woob ? npages * mtd->oobsize : 0;
531 buf = kmalloc(len + oob_len, GFP_KERNEL);
533 buf = map_sysmem(user_addr, 0);
536 printf("Could not map/allocate the user buffer\n");
537 ret = CMD_RET_FAILURE;
542 printf("%s %lld byte(s) (%d page(s)) at offset 0x%08llx%s%s%s\n",
543 read ? "Reading" : "Writing", len, npages, start_off,
544 raw ? " [raw]" : "", woob ? " [oob]" : "",
545 !read && write_empty_pages ? " [dontskipff]" : "");
547 printf("%s %lld byte(s) at offset 0x%08llx\n",
548 read ? "Reading" : "Writing", len, start_off);
550 io_op.mode = raw ? MTD_OPS_RAW : MTD_OPS_AUTO_OOB;
551 io_op.len = has_pages ? mtd->writesize : len;
552 io_op.ooblen = woob ? mtd->oobsize : 0;
554 io_op.oobbuf = woob ? &buf[len] : NULL;
556 /* Search for the first good block after the given offset */
558 while (mtd_block_isbad(mtd, off))
559 off += mtd->erasesize;
561 /* Loop over the pages to do the actual read/write */
563 /* Skip the block if it is bad */
564 if (mtd_is_aligned_with_block_size(mtd, off) &&
565 mtd_block_isbad(mtd, off)) {
566 off += mtd->erasesize;
571 ret = mtd_read_oob(mtd, off, &io_op);
573 ret = mtd_special_write_oob(mtd, off, &io_op,
574 write_empty_pages, woob);
577 printf("Failure while %s at offset 0x%llx\n",
578 read ? "reading" : "writing", off);
583 remaining -= io_op.retlen;
584 io_op.datbuf += io_op.retlen;
585 io_op.oobbuf += io_op.oobretlen;
589 mtd_dump_device_buf(mtd, start_off, buf, len, woob);
597 printf("%s on %s failed with error %d\n",
598 read ? "Read" : "Write", mtd->name, ret);
599 ret = CMD_RET_FAILURE;
601 ret = CMD_RET_SUCCESS;
610 static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc,
613 struct erase_info erase_op = {};
614 struct mtd_info *mtd;
620 return CMD_RET_USAGE;
622 mtd = get_mtd_by_name(argv[1]);
623 if (IS_ERR_OR_NULL(mtd))
624 return CMD_RET_FAILURE;
626 scrub = strstr(argv[0], ".dontskipbad");
631 off = argc > 0 ? hextoul(argv[0], NULL) : 0;
632 len = argc > 1 ? hextoul(argv[1], NULL) : mtd->size;
634 if (!mtd_is_aligned_with_block_size(mtd, off)) {
635 printf("Offset not aligned with a block (0x%x)\n",
637 ret = CMD_RET_FAILURE;
641 if (!mtd_is_aligned_with_block_size(mtd, len)) {
642 printf("Size not a multiple of a block (0x%x)\n",
644 ret = CMD_RET_FAILURE;
648 printf("Erasing 0x%08llx ... 0x%08llx (%d eraseblock(s))\n",
649 off, off + len - 1, mtd_div_by_eb(len, mtd));
653 erase_op.len = mtd->erasesize;
657 ret = mtd_block_isbad(mtd, erase_op.addr);
659 printf("Failed to get bad block at 0x%08llx\n",
661 ret = CMD_RET_FAILURE;
666 printf("Skipping bad block at 0x%08llx\n",
669 len -= mtd->erasesize;
670 erase_op.addr += mtd->erasesize;
675 ret = mtd_erase(mtd, &erase_op);
676 if (ret && ret != -EIO)
679 len -= mtd->erasesize;
680 erase_op.addr += mtd->erasesize;
683 if (ret && ret != -EIO)
684 ret = CMD_RET_FAILURE;
686 ret = CMD_RET_SUCCESS;
694 static int do_mtd_bad(struct cmd_tbl *cmdtp, int flag, int argc,
697 struct mtd_info *mtd;
701 return CMD_RET_USAGE;
703 mtd = get_mtd_by_name(argv[1]);
704 if (IS_ERR_OR_NULL(mtd))
705 return CMD_RET_FAILURE;
707 if (!mtd_can_have_bb(mtd)) {
708 printf("Only NAND-based devices can have bad blocks\n");
712 printf("MTD device %s bad blocks list:\n", mtd->name);
713 for (off = 0; off < mtd->size; off += mtd->erasesize) {
714 if (mtd_block_isbad(mtd, off))
715 printf("\t0x%08llx\n", off);
721 return CMD_RET_SUCCESS;
724 #ifdef CONFIG_AUTO_COMPLETE
725 static int mtd_name_complete(int argc, char *const argv[], char last_char,
726 int maxv, char *cmdv[])
728 int len = 0, n_found = 0;
729 struct mtd_info *mtd;
735 (argc == 1 && (last_char == '\0' || isblank(last_char))))
739 len = strlen(argv[0]);
741 mtd_for_each_device(mtd) {
743 (len > strlen(mtd->name) ||
744 strncmp(argv[0], mtd->name, len)))
747 if (n_found >= maxv - 2) {
748 cmdv[n_found++] = "...";
752 cmdv[n_found++] = mtd->name;
755 cmdv[n_found] = NULL;
759 #endif /* CONFIG_AUTO_COMPLETE */
762 "- generic operations on memory technology devices\n\n"
764 "mtd read[.raw][.oob] <name> <addr> [<off> [<size>]]\n"
765 "mtd dump[.raw][.oob] <name> [<off> [<size>]]\n"
766 "mtd write[.raw][.oob][.dontskipff] <name> <addr> [<off> [<size>]]\n"
767 "mtd erase[.dontskipbad] <name> [<off> [<size>]]\n"
769 "Specific functions:\n"
771 #if CONFIG_IS_ENABLED(CMD_MTD_OTP)
772 "mtd otpread <name> [u|f] <off> <size>\n"
773 "mtd otpwrite <name> <off> <hex string>\n"
774 "mtd otplock <name> <off> <size>\n"
775 "mtd otpinfo <name> [u|f]\n"
779 "\t<name>: NAND partition/chip name (or corresponding DM device name or OF path)\n"
780 "\t<addr>: user address from/to which data will be retrieved/stored\n"
781 "\t<off>: offset in <name> in bytes (default: start of the part)\n"
782 "\t\t* must be block-aligned for erase\n"
783 "\t\t* must be page-aligned otherwise\n"
784 "\t<size>: length of the operation in bytes (default: the entire device)\n"
785 "\t\t* must be a multiple of a block for erase\n"
786 "\t\t* must be a multiple of a page otherwise (special case: default is a page with dump)\n"
787 #if CONFIG_IS_ENABLED(CMD_MTD_OTP)
788 "\t<hex string>: hex string without '0x' and spaces. Example: ABCD1234\n"
789 "\t[u|f]: user or factory OTP region\n"
792 "The .dontskipff option forces writing empty pages, don't use it if unsure.\n");
794 U_BOOT_CMD_WITH_SUBCMDS(mtd, "MTD utils", mtd_help_text,
795 #if CONFIG_IS_ENABLED(CMD_MTD_OTP)
796 U_BOOT_SUBCMD_MKENT(otpread, 5, 1, do_mtd_otp_read),
797 U_BOOT_SUBCMD_MKENT(otpwrite, 4, 1, do_mtd_otp_write),
798 U_BOOT_SUBCMD_MKENT(otplock, 4, 1, do_mtd_otp_lock),
799 U_BOOT_SUBCMD_MKENT(otpinfo, 3, 1, do_mtd_otp_info),
801 U_BOOT_SUBCMD_MKENT(list, 1, 1, do_mtd_list),
802 U_BOOT_SUBCMD_MKENT_COMPLETE(read, 5, 0, do_mtd_io,
804 U_BOOT_SUBCMD_MKENT_COMPLETE(write, 5, 0, do_mtd_io,
806 U_BOOT_SUBCMD_MKENT_COMPLETE(dump, 4, 0, do_mtd_io,
808 U_BOOT_SUBCMD_MKENT_COMPLETE(erase, 4, 0, do_mtd_erase,
810 U_BOOT_SUBCMD_MKENT_COMPLETE(bad, 2, 1, do_mtd_bad,