1 // SPDX-License-Identifier: GPL-2.0+
5 * Generic command to handle basic operations on any memory device.
7 * Copyright: Bootlin, 2018
14 #if CONFIG_IS_ENABLED(CMD_MTD_OTP)
20 #include <dm/devres.h>
21 #include <linux/err.h>
23 #include <linux/ctype.h>
25 static struct mtd_info *get_mtd_by_name(const char *name)
31 mtd = get_mtd_device_nm(name);
32 if (IS_ERR_OR_NULL(mtd))
33 printf("MTD device %s not found, ret %ld\n", name,
39 static uint mtd_len_to_pages(struct mtd_info *mtd, u64 len)
41 do_div(len, mtd->writesize);
46 static bool mtd_is_aligned_with_min_io_size(struct mtd_info *mtd, u64 size)
48 return !do_div(size, mtd->writesize);
51 static bool mtd_is_aligned_with_block_size(struct mtd_info *mtd, u64 size)
53 return !do_div(size, mtd->erasesize);
56 static void mtd_dump_buf(const u8 *buf, uint len, uint offset)
60 for (i = 0; i < len; ) {
61 printf("0x%08x:\t", offset + i);
62 for (j = 0; j < 8; j++)
63 printf("%02x ", buf[i + j]);
66 for (j = 0; j < 8; j++)
67 printf("%02x ", buf[i + j]);
73 static void mtd_dump_device_buf(struct mtd_info *mtd, u64 start_off,
74 const u8 *buf, u64 len, bool woob)
76 bool has_pages = mtd->type == MTD_NANDFLASH ||
77 mtd->type == MTD_MLCNANDFLASH;
78 int npages = mtd_len_to_pages(mtd, len);
82 for (page = 0; page < npages; page++) {
83 u64 data_off = (u64)page * mtd->writesize;
85 printf("\nDump %d data bytes from 0x%08llx:\n",
86 mtd->writesize, start_off + data_off);
87 mtd_dump_buf(&buf[data_off],
88 mtd->writesize, start_off + data_off);
91 u64 oob_off = (u64)page * mtd->oobsize;
93 printf("Dump %d OOB bytes from page at 0x%08llx:\n",
94 mtd->oobsize, start_off + data_off);
95 mtd_dump_buf(&buf[len + oob_off],
100 printf("\nDump %lld data bytes from 0x%llx:\n",
102 mtd_dump_buf(buf, len, start_off);
106 static void mtd_show_parts(struct mtd_info *mtd, int level)
108 struct mtd_info *part;
111 list_for_each_entry(part, &mtd->partitions, node) {
112 for (i = 0; i < level; i++)
114 printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
115 part->offset, part->offset + part->size, part->name);
117 mtd_show_parts(part, level + 1);
121 static void mtd_show_device(struct mtd_info *mtd)
124 printf("* %s\n", mtd->name);
125 #if defined(CONFIG_DM)
127 printf(" - device: %s\n", mtd->dev->name);
128 printf(" - parent: %s\n", mtd->dev->parent->name);
129 printf(" - driver: %s\n", mtd->dev->driver->name);
132 if (IS_ENABLED(CONFIG_OF_CONTROL) && mtd->dev) {
136 res = ofnode_get_path(mtd_get_ofnode(mtd), buf, 256);
137 printf(" - path: %s\n", res == 0 ? buf : "unavailable");
140 /* MTD device information */
150 printf("NOR flash\n");
153 printf("NAND flash\n");
156 printf("Data flash\n");
159 printf("UBI volume\n");
161 case MTD_MLCNANDFLASH:
162 printf("MLC NAND flash\n");
170 printf(" - block size: 0x%x bytes\n", mtd->erasesize);
171 printf(" - min I/O: 0x%x bytes\n", mtd->writesize);
174 printf(" - OOB size: %u bytes\n", mtd->oobsize);
175 printf(" - OOB available: %u bytes\n", mtd->oobavail);
178 if (mtd->ecc_strength) {
179 printf(" - ECC strength: %u bits\n", mtd->ecc_strength);
180 printf(" - ECC step size: %u bytes\n", mtd->ecc_step_size);
181 printf(" - bitflip threshold: %u bits\n",
182 mtd->bitflip_threshold);
185 printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
186 mtd->offset, mtd->offset + mtd->size, mtd->name);
188 /* MTD partitions, if any */
189 mtd_show_parts(mtd, 1);
192 /* Logic taken from fs/ubifs/recovery.c:is_empty() */
193 static bool mtd_oob_write_is_empty(struct mtd_oob_ops *op)
197 for (i = 0; i < op->len; i++)
198 if (op->datbuf[i] != 0xff)
201 for (i = 0; i < op->ooblen; i++)
202 if (op->oobbuf[i] != 0xff)
208 #if CONFIG_IS_ENABLED(CMD_MTD_OTP)
209 static int do_mtd_otp_read(struct cmd_tbl *cmdtp, int flag, int argc,
212 struct mtd_info *mtd;
221 return CMD_RET_USAGE;
223 if (!strcmp(argv[2], "u"))
225 else if (!strcmp(argv[2], "f"))
228 return CMD_RET_USAGE;
230 mtd = get_mtd_by_name(argv[1]);
231 if (IS_ERR_OR_NULL(mtd))
232 return CMD_RET_FAILURE;
234 from = simple_strtoul(argv[3], NULL, 0);
235 len = simple_strtoul(argv[4], NULL, 0);
237 ret = CMD_RET_FAILURE;
243 printf("Reading %s OTP from 0x%lx, %zu bytes\n",
244 user ? "user" : "factory", from, len);
247 ret = mtd_read_user_prot_reg(mtd, from, len, &retlen, buf);
249 ret = mtd_read_fact_prot_reg(mtd, from, len, &retlen, buf);
252 pr_err("OTP read failed: %d\n", ret);
253 ret = CMD_RET_FAILURE;
258 pr_err("OTP read returns %zu, but %zu expected\n",
261 print_hex_dump("", 0, 16, 1, buf, retlen, true);
265 ret = CMD_RET_SUCCESS;
273 static int do_mtd_otp_lock(struct cmd_tbl *cmdtp, int flag, int argc,
276 struct mtd_info *mtd;
282 return CMD_RET_USAGE;
284 mtd = get_mtd_by_name(argv[1]);
285 if (IS_ERR_OR_NULL(mtd))
286 return CMD_RET_FAILURE;
288 from = simple_strtoul(argv[2], NULL, 0);
289 len = simple_strtoul(argv[3], NULL, 0);
291 ret = mtd_lock_user_prot_reg(mtd, from, len);
293 pr_err("OTP lock failed: %d\n", ret);
294 ret = CMD_RET_FAILURE;
298 ret = CMD_RET_SUCCESS;
306 static int do_mtd_otp_write(struct cmd_tbl *cmdtp, int flag, int argc,
309 struct mtd_info *mtd;
317 return CMD_RET_USAGE;
319 mtd = get_mtd_by_name(argv[1]);
320 if (IS_ERR_OR_NULL(mtd))
321 return CMD_RET_FAILURE;
323 from = simple_strtoul(argv[2], NULL, 0);
324 binlen = strlen(argv[3]) / 2;
326 ret = CMD_RET_FAILURE;
327 binbuf = malloc(binlen);
331 hex2bin(binbuf, argv[3], binlen);
333 printf("Will write:\n");
335 print_hex_dump("", 0, 16, 1, binbuf, binlen, true);
337 printf("to 0x%lx\n", from);
339 printf("Continue (y/n)?\n");
341 if (confirm_yesno() != 1) {
342 pr_err("OTP write canceled\n");
343 ret = CMD_RET_SUCCESS;
347 ret = mtd_write_user_prot_reg(mtd, from, binlen, &retlen, binbuf);
349 pr_err("OTP write failed: %d\n", ret);
350 ret = CMD_RET_FAILURE;
354 if (retlen != binlen)
355 pr_err("OTP write returns %zu, but %zu expected\n",
358 ret = CMD_RET_SUCCESS;
367 static int do_mtd_otp_info(struct cmd_tbl *cmdtp, int flag, int argc,
370 struct otp_info otp_info;
371 struct mtd_info *mtd;
377 return CMD_RET_USAGE;
379 if (!strcmp(argv[2], "u"))
381 else if (!strcmp(argv[2], "f"))
384 return CMD_RET_USAGE;
386 mtd = get_mtd_by_name(argv[1]);
387 if (IS_ERR_OR_NULL(mtd))
388 return CMD_RET_FAILURE;
391 ret = mtd_get_user_prot_info(mtd, sizeof(otp_info), &retlen,
394 ret = mtd_get_fact_prot_info(mtd, sizeof(otp_info), &retlen,
397 pr_err("OTP info failed: %d\n", ret);
398 ret = CMD_RET_FAILURE;
402 if (retlen != sizeof(otp_info)) {
403 pr_err("OTP info returns %zu, but %zu expected\n",
404 retlen, sizeof(otp_info));
405 ret = CMD_RET_FAILURE;
409 printf("%s OTP region info:\n", user ? "User" : "Factory");
410 printf("\tstart: %u\n", otp_info.start);
411 printf("\tlength: %u\n", otp_info.length);
412 printf("\tlocked: %u\n", otp_info.locked);
414 ret = CMD_RET_SUCCESS;
423 static int do_mtd_list(struct cmd_tbl *cmdtp, int flag, int argc,
426 struct mtd_info *mtd;
429 /* Ensure all devices (and their partitions) are probed */
432 printf("List of MTD devices:\n");
433 mtd_for_each_device(mtd) {
434 if (!mtd_is_partition(mtd))
435 mtd_show_device(mtd);
441 printf("No MTD device found\n");
442 return CMD_RET_FAILURE;
445 return CMD_RET_SUCCESS;
448 static int mtd_special_write_oob(struct mtd_info *mtd, u64 off,
449 struct mtd_oob_ops *io_op,
450 bool write_empty_pages, bool woob)
455 * By default, do not write an empty page.
456 * Skip it by simulating a successful write.
458 if (!write_empty_pages && mtd_oob_write_is_empty(io_op)) {
459 io_op->retlen = mtd->writesize;
460 io_op->oobretlen = woob ? mtd->oobsize : 0;
462 ret = mtd_write_oob(mtd, off, io_op);
468 static int do_mtd_io(struct cmd_tbl *cmdtp, int flag, int argc,
471 bool dump, read, raw, woob, write_empty_pages, has_pages = false;
472 u64 start_off, off, len, remaining, default_len;
473 struct mtd_oob_ops io_op = {};
474 uint user_addr = 0, npages;
475 const char *cmd = argv[0];
476 struct mtd_info *mtd;
482 return CMD_RET_USAGE;
484 mtd = get_mtd_by_name(argv[1]);
485 if (IS_ERR_OR_NULL(mtd))
486 return CMD_RET_FAILURE;
488 if (mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH)
491 dump = !strncmp(cmd, "dump", 4);
492 read = dump || !strncmp(cmd, "read", 4);
493 raw = strstr(cmd, ".raw");
494 woob = strstr(cmd, ".oob");
495 write_empty_pages = !has_pages || strstr(cmd, ".dontskipff");
506 user_addr = hextoul(argv[0], NULL);
511 start_off = argc > 0 ? hextoul(argv[0], NULL) : 0;
512 if (!mtd_is_aligned_with_min_io_size(mtd, start_off)) {
513 printf("Offset not aligned with a page (0x%x)\n",
515 ret = CMD_RET_FAILURE;
519 default_len = dump ? mtd->writesize : mtd->size;
520 len = argc > 1 ? hextoul(argv[1], NULL) : default_len;
521 if (!mtd_is_aligned_with_min_io_size(mtd, len)) {
522 len = round_up(len, mtd->writesize);
523 printf("Size not on a page boundary (0x%x), rounding to 0x%llx\n",
524 mtd->writesize, len);
528 npages = mtd_len_to_pages(mtd, len);
529 oob_len = woob ? npages * mtd->oobsize : 0;
532 buf = kmalloc(len + oob_len, GFP_KERNEL);
534 buf = map_sysmem(user_addr, 0);
537 printf("Could not map/allocate the user buffer\n");
538 ret = CMD_RET_FAILURE;
543 printf("%s %lld byte(s) (%d page(s)) at offset 0x%08llx%s%s%s\n",
544 read ? "Reading" : "Writing", len, npages, start_off,
545 raw ? " [raw]" : "", woob ? " [oob]" : "",
546 !read && write_empty_pages ? " [dontskipff]" : "");
548 printf("%s %lld byte(s) at offset 0x%08llx\n",
549 read ? "Reading" : "Writing", len, start_off);
551 io_op.mode = raw ? MTD_OPS_RAW : MTD_OPS_AUTO_OOB;
552 io_op.len = has_pages ? mtd->writesize : len;
553 io_op.ooblen = woob ? mtd->oobsize : 0;
555 io_op.oobbuf = woob ? &buf[len] : NULL;
557 /* Search for the first good block after the given offset */
559 while (mtd_block_isbad(mtd, off))
560 off += mtd->erasesize;
562 led_activity_blink();
564 /* Loop over the pages to do the actual read/write */
566 /* Skip the block if it is bad */
567 if (mtd_is_aligned_with_block_size(mtd, off) &&
568 mtd_block_isbad(mtd, off)) {
569 off += mtd->erasesize;
574 ret = mtd_read_oob(mtd, off, &io_op);
576 ret = mtd_special_write_oob(mtd, off, &io_op,
577 write_empty_pages, woob);
580 printf("Failure while %s at offset 0x%llx\n",
581 read ? "reading" : "writing", off);
586 remaining -= io_op.retlen;
587 io_op.datbuf += io_op.retlen;
588 io_op.oobbuf += io_op.oobretlen;
594 mtd_dump_device_buf(mtd, start_off, buf, len, woob);
602 printf("%s on %s failed with error %d\n",
603 read ? "Read" : "Write", mtd->name, ret);
604 ret = CMD_RET_FAILURE;
606 ret = CMD_RET_SUCCESS;
615 static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc,
618 struct erase_info erase_op = {};
619 struct mtd_info *mtd;
625 return CMD_RET_USAGE;
627 mtd = get_mtd_by_name(argv[1]);
628 if (IS_ERR_OR_NULL(mtd))
629 return CMD_RET_FAILURE;
631 scrub = strstr(argv[0], ".dontskipbad");
636 off = argc > 0 ? hextoul(argv[0], NULL) : 0;
637 len = argc > 1 ? hextoul(argv[1], NULL) : mtd->size;
639 if (!mtd_is_aligned_with_block_size(mtd, off)) {
640 printf("Offset not aligned with a block (0x%x)\n",
642 ret = CMD_RET_FAILURE;
646 if (!mtd_is_aligned_with_block_size(mtd, len)) {
647 printf("Size not a multiple of a block (0x%x)\n",
649 ret = CMD_RET_FAILURE;
653 printf("Erasing 0x%08llx ... 0x%08llx (%d eraseblock(s))\n",
654 off, off + len - 1, mtd_div_by_eb(len, mtd));
658 erase_op.len = mtd->erasesize;
660 led_activity_blink();
664 ret = mtd_block_isbad(mtd, erase_op.addr);
666 printf("Failed to get bad block at 0x%08llx\n",
668 ret = CMD_RET_FAILURE;
673 printf("Skipping bad block at 0x%08llx\n",
676 len -= mtd->erasesize;
677 erase_op.addr += mtd->erasesize;
682 ret = mtd_erase(mtd, &erase_op);
683 if (ret && ret != -EIO)
686 len -= mtd->erasesize;
687 erase_op.addr += mtd->erasesize;
692 if (ret && ret != -EIO)
693 ret = CMD_RET_FAILURE;
695 ret = CMD_RET_SUCCESS;
703 static int do_mtd_bad(struct cmd_tbl *cmdtp, int flag, int argc,
706 struct mtd_info *mtd;
710 return CMD_RET_USAGE;
712 mtd = get_mtd_by_name(argv[1]);
713 if (IS_ERR_OR_NULL(mtd))
714 return CMD_RET_FAILURE;
716 if (!mtd_can_have_bb(mtd)) {
717 printf("Only NAND-based devices can have bad blocks\n");
721 printf("MTD device %s bad blocks list:\n", mtd->name);
722 for (off = 0; off < mtd->size; off += mtd->erasesize) {
723 if (mtd_block_isbad(mtd, off))
724 printf("\t0x%08llx\n", off);
730 return CMD_RET_SUCCESS;
733 #ifdef CONFIG_AUTO_COMPLETE
734 static int mtd_name_complete(int argc, char *const argv[], char last_char,
735 int maxv, char *cmdv[])
737 int len = 0, n_found = 0;
738 struct mtd_info *mtd;
744 (argc == 1 && (last_char == '\0' || isblank(last_char))))
748 len = strlen(argv[0]);
750 mtd_for_each_device(mtd) {
752 (len > strlen(mtd->name) ||
753 strncmp(argv[0], mtd->name, len)))
756 if (n_found >= maxv - 2) {
757 cmdv[n_found++] = "...";
761 cmdv[n_found++] = mtd->name;
764 cmdv[n_found] = NULL;
768 #endif /* CONFIG_AUTO_COMPLETE */
771 "- generic operations on memory technology devices\n\n"
773 "mtd read[.raw][.oob] <name> <addr> [<off> [<size>]]\n"
774 "mtd dump[.raw][.oob] <name> [<off> [<size>]]\n"
775 "mtd write[.raw][.oob][.dontskipff] <name> <addr> [<off> [<size>]]\n"
776 "mtd erase[.dontskipbad] <name> [<off> [<size>]]\n"
778 "Specific functions:\n"
780 #if CONFIG_IS_ENABLED(CMD_MTD_OTP)
781 "mtd otpread <name> [u|f] <off> <size>\n"
782 "mtd otpwrite <name> <off> <hex string>\n"
783 "mtd otplock <name> <off> <size>\n"
784 "mtd otpinfo <name> [u|f]\n"
788 "\t<name>: NAND partition/chip name (or corresponding DM device name or OF path)\n"
789 "\t<addr>: user address from/to which data will be retrieved/stored\n"
790 "\t<off>: offset in <name> in bytes (default: start of the part)\n"
791 "\t\t* must be block-aligned for erase\n"
792 "\t\t* must be page-aligned otherwise\n"
793 "\t<size>: length of the operation in bytes (default: the entire device)\n"
794 "\t\t* must be a multiple of a block for erase\n"
795 "\t\t* must be a multiple of a page otherwise (special case: default is a page with dump)\n"
796 #if CONFIG_IS_ENABLED(CMD_MTD_OTP)
797 "\t<hex string>: hex string without '0x' and spaces. Example: ABCD1234\n"
798 "\t[u|f]: user or factory OTP region\n"
801 "The .dontskipff option forces writing empty pages, don't use it if unsure.\n");
803 U_BOOT_CMD_WITH_SUBCMDS(mtd, "MTD utils", mtd_help_text,
804 #if CONFIG_IS_ENABLED(CMD_MTD_OTP)
805 U_BOOT_SUBCMD_MKENT(otpread, 5, 1, do_mtd_otp_read),
806 U_BOOT_SUBCMD_MKENT(otpwrite, 4, 1, do_mtd_otp_write),
807 U_BOOT_SUBCMD_MKENT(otplock, 4, 1, do_mtd_otp_lock),
808 U_BOOT_SUBCMD_MKENT(otpinfo, 3, 1, do_mtd_otp_info),
810 U_BOOT_SUBCMD_MKENT(list, 1, 1, do_mtd_list),
811 U_BOOT_SUBCMD_MKENT_COMPLETE(read, 5, 0, do_mtd_io,
813 U_BOOT_SUBCMD_MKENT_COMPLETE(write, 5, 0, do_mtd_io,
815 U_BOOT_SUBCMD_MKENT_COMPLETE(dump, 4, 0, do_mtd_io,
817 U_BOOT_SUBCMD_MKENT_COMPLETE(erase, 4, 0, do_mtd_erase,
819 U_BOOT_SUBCMD_MKENT_COMPLETE(bad, 2, 1, do_mtd_bad,