1 // SPDX-License-Identifier: GPL-2.0+
5 * Generic command to handle basic operations on any memory device.
7 * Copyright: Bootlin, 2018
8 * Author: Miquèl Raynal <miquel.raynal@bootlin.com>
17 #include <dm/devres.h>
18 #include <linux/err.h>
20 #include <linux/ctype.h>
22 static struct mtd_info *get_mtd_by_name(const char *name)
28 mtd = get_mtd_device_nm(name);
29 if (IS_ERR_OR_NULL(mtd))
30 printf("MTD device %s not found, ret %ld\n", name,
36 static uint mtd_len_to_pages(struct mtd_info *mtd, u64 len)
38 do_div(len, mtd->writesize);
43 static bool mtd_is_aligned_with_min_io_size(struct mtd_info *mtd, u64 size)
45 return !do_div(size, mtd->writesize);
48 static bool mtd_is_aligned_with_block_size(struct mtd_info *mtd, u64 size)
50 return !do_div(size, mtd->erasesize);
53 static void mtd_dump_buf(const u8 *buf, uint len, uint offset)
57 for (i = 0; i < len; ) {
58 printf("0x%08x:\t", offset + i);
59 for (j = 0; j < 8; j++)
60 printf("%02x ", buf[i + j]);
63 for (j = 0; j < 8; j++)
64 printf("%02x ", buf[i + j]);
70 static void mtd_dump_device_buf(struct mtd_info *mtd, u64 start_off,
71 const u8 *buf, u64 len, bool woob)
73 bool has_pages = mtd->type == MTD_NANDFLASH ||
74 mtd->type == MTD_MLCNANDFLASH;
75 int npages = mtd_len_to_pages(mtd, len);
79 for (page = 0; page < npages; page++) {
80 u64 data_off = page * mtd->writesize;
82 printf("\nDump %d data bytes from 0x%08llx:\n",
83 mtd->writesize, start_off + data_off);
84 mtd_dump_buf(&buf[data_off],
85 mtd->writesize, start_off + data_off);
88 u64 oob_off = page * mtd->oobsize;
90 printf("Dump %d OOB bytes from page at 0x%08llx:\n",
91 mtd->oobsize, start_off + data_off);
92 mtd_dump_buf(&buf[len + oob_off],
97 printf("\nDump %lld data bytes from 0x%llx:\n",
99 mtd_dump_buf(buf, len, start_off);
103 static void mtd_show_parts(struct mtd_info *mtd, int level)
105 struct mtd_info *part;
108 list_for_each_entry(part, &mtd->partitions, node) {
109 for (i = 0; i < level; i++)
111 printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
112 part->offset, part->offset + part->size, part->name);
114 mtd_show_parts(part, level + 1);
118 static void mtd_show_device(struct mtd_info *mtd)
121 printf("* %s\n", mtd->name);
122 #if defined(CONFIG_DM)
124 printf(" - device: %s\n", mtd->dev->name);
125 printf(" - parent: %s\n", mtd->dev->parent->name);
126 printf(" - driver: %s\n", mtd->dev->driver->name);
130 /* MTD device information */
140 printf("NOR flash\n");
143 printf("NAND flash\n");
146 printf("Data flash\n");
149 printf("UBI volume\n");
151 case MTD_MLCNANDFLASH:
152 printf("MLC NAND flash\n");
160 printf(" - block size: 0x%x bytes\n", mtd->erasesize);
161 printf(" - min I/O: 0x%x bytes\n", mtd->writesize);
164 printf(" - OOB size: %u bytes\n", mtd->oobsize);
165 printf(" - OOB available: %u bytes\n", mtd->oobavail);
168 if (mtd->ecc_strength) {
169 printf(" - ECC strength: %u bits\n", mtd->ecc_strength);
170 printf(" - ECC step size: %u bytes\n", mtd->ecc_step_size);
171 printf(" - bitflip threshold: %u bits\n",
172 mtd->bitflip_threshold);
175 printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
176 mtd->offset, mtd->offset + mtd->size, mtd->name);
178 /* MTD partitions, if any */
179 mtd_show_parts(mtd, 1);
182 /* Logic taken from fs/ubifs/recovery.c:is_empty() */
183 static bool mtd_oob_write_is_empty(struct mtd_oob_ops *op)
187 for (i = 0; i < op->len; i++)
188 if (op->datbuf[i] != 0xff)
191 for (i = 0; i < op->ooblen; i++)
192 if (op->oobbuf[i] != 0xff)
198 static int do_mtd_list(struct cmd_tbl *cmdtp, int flag, int argc,
201 struct mtd_info *mtd;
204 /* Ensure all devices (and their partitions) are probed */
207 printf("List of MTD devices:\n");
208 mtd_for_each_device(mtd) {
209 if (!mtd_is_partition(mtd))
210 mtd_show_device(mtd);
216 printf("No MTD device found\n");
217 return CMD_RET_FAILURE;
220 return CMD_RET_SUCCESS;
223 static int mtd_special_write_oob(struct mtd_info *mtd, u64 off,
224 struct mtd_oob_ops *io_op,
225 bool write_empty_pages, bool woob)
230 * By default, do not write an empty page.
231 * Skip it by simulating a successful write.
233 if (!write_empty_pages && mtd_oob_write_is_empty(io_op)) {
234 io_op->retlen = mtd->writesize;
235 io_op->oobretlen = woob ? mtd->oobsize : 0;
237 ret = mtd_write_oob(mtd, off, io_op);
243 static int do_mtd_io(struct cmd_tbl *cmdtp, int flag, int argc,
246 bool dump, read, raw, woob, write_empty_pages, has_pages = false;
247 u64 start_off, off, len, remaining, default_len;
248 struct mtd_oob_ops io_op = {};
249 uint user_addr = 0, npages;
250 const char *cmd = argv[0];
251 struct mtd_info *mtd;
257 return CMD_RET_USAGE;
259 mtd = get_mtd_by_name(argv[1]);
260 if (IS_ERR_OR_NULL(mtd))
261 return CMD_RET_FAILURE;
263 if (mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH)
266 dump = !strncmp(cmd, "dump", 4);
267 read = dump || !strncmp(cmd, "read", 4);
268 raw = strstr(cmd, ".raw");
269 woob = strstr(cmd, ".oob");
270 write_empty_pages = !has_pages || strstr(cmd, ".dontskipff");
281 user_addr = simple_strtoul(argv[0], NULL, 16);
286 start_off = argc > 0 ? simple_strtoul(argv[0], NULL, 16) : 0;
287 if (!mtd_is_aligned_with_min_io_size(mtd, start_off)) {
288 printf("Offset not aligned with a page (0x%x)\n",
290 ret = CMD_RET_FAILURE;
294 default_len = dump ? mtd->writesize : mtd->size;
295 len = argc > 1 ? simple_strtoul(argv[1], NULL, 16) : default_len;
296 if (!mtd_is_aligned_with_min_io_size(mtd, len)) {
297 len = round_up(len, mtd->writesize);
298 printf("Size not on a page boundary (0x%x), rounding to 0x%llx\n",
299 mtd->writesize, len);
303 npages = mtd_len_to_pages(mtd, len);
304 oob_len = woob ? npages * mtd->oobsize : 0;
307 buf = kmalloc(len + oob_len, GFP_KERNEL);
309 buf = map_sysmem(user_addr, 0);
312 printf("Could not map/allocate the user buffer\n");
313 ret = CMD_RET_FAILURE;
318 printf("%s %lld byte(s) (%d page(s)) at offset 0x%08llx%s%s%s\n",
319 read ? "Reading" : "Writing", len, npages, start_off,
320 raw ? " [raw]" : "", woob ? " [oob]" : "",
321 !read && write_empty_pages ? " [dontskipff]" : "");
323 printf("%s %lld byte(s) at offset 0x%08llx\n",
324 read ? "Reading" : "Writing", len, start_off);
326 io_op.mode = raw ? MTD_OPS_RAW : MTD_OPS_AUTO_OOB;
327 io_op.len = has_pages ? mtd->writesize : len;
328 io_op.ooblen = woob ? mtd->oobsize : 0;
330 io_op.oobbuf = woob ? &buf[len] : NULL;
332 /* Search for the first good block after the given offset */
334 while (mtd_block_isbad(mtd, off))
335 off += mtd->erasesize;
337 /* Loop over the pages to do the actual read/write */
339 /* Skip the block if it is bad */
340 if (mtd_is_aligned_with_block_size(mtd, off) &&
341 mtd_block_isbad(mtd, off)) {
342 off += mtd->erasesize;
347 ret = mtd_read_oob(mtd, off, &io_op);
349 ret = mtd_special_write_oob(mtd, off, &io_op,
350 write_empty_pages, woob);
353 printf("Failure while %s at offset 0x%llx\n",
354 read ? "reading" : "writing", off);
359 remaining -= io_op.retlen;
360 io_op.datbuf += io_op.retlen;
361 io_op.oobbuf += io_op.oobretlen;
365 mtd_dump_device_buf(mtd, start_off, buf, len, woob);
373 printf("%s on %s failed with error %d\n",
374 read ? "Read" : "Write", mtd->name, ret);
375 ret = CMD_RET_FAILURE;
377 ret = CMD_RET_SUCCESS;
386 static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc,
389 struct erase_info erase_op = {};
390 struct mtd_info *mtd;
396 return CMD_RET_USAGE;
398 mtd = get_mtd_by_name(argv[1]);
399 if (IS_ERR_OR_NULL(mtd))
400 return CMD_RET_FAILURE;
402 scrub = strstr(argv[0], ".dontskipbad");
407 off = argc > 0 ? simple_strtoul(argv[0], NULL, 16) : 0;
408 len = argc > 1 ? simple_strtoul(argv[1], NULL, 16) : mtd->size;
410 if (!mtd_is_aligned_with_block_size(mtd, off)) {
411 printf("Offset not aligned with a block (0x%x)\n",
413 ret = CMD_RET_FAILURE;
417 if (!mtd_is_aligned_with_block_size(mtd, len)) {
418 printf("Size not a multiple of a block (0x%x)\n",
420 ret = CMD_RET_FAILURE;
424 printf("Erasing 0x%08llx ... 0x%08llx (%d eraseblock(s))\n",
425 off, off + len - 1, mtd_div_by_eb(len, mtd));
429 erase_op.len = mtd->erasesize;
430 erase_op.scrub = scrub;
433 ret = mtd_erase(mtd, &erase_op);
436 /* Abort if its not a bad block error */
439 printf("Skipping bad block at 0x%08llx\n",
443 len -= mtd->erasesize;
444 erase_op.addr += mtd->erasesize;
447 if (ret && ret != -EIO)
448 ret = CMD_RET_FAILURE;
450 ret = CMD_RET_SUCCESS;
458 static int do_mtd_bad(struct cmd_tbl *cmdtp, int flag, int argc,
461 struct mtd_info *mtd;
465 return CMD_RET_USAGE;
467 mtd = get_mtd_by_name(argv[1]);
468 if (IS_ERR_OR_NULL(mtd))
469 return CMD_RET_FAILURE;
471 if (!mtd_can_have_bb(mtd)) {
472 printf("Only NAND-based devices can have bad blocks\n");
476 printf("MTD device %s bad blocks list:\n", mtd->name);
477 for (off = 0; off < mtd->size; off += mtd->erasesize) {
478 if (mtd_block_isbad(mtd, off))
479 printf("\t0x%08llx\n", off);
485 return CMD_RET_SUCCESS;
488 #ifdef CONFIG_AUTO_COMPLETE
489 static int mtd_name_complete(int argc, char *const argv[], char last_char,
490 int maxv, char *cmdv[])
492 int len = 0, n_found = 0;
493 struct mtd_info *mtd;
499 (argc == 1 && (last_char == '\0' || isblank(last_char))))
503 len = strlen(argv[0]);
505 mtd_for_each_device(mtd) {
507 (len > strlen(mtd->name) ||
508 strncmp(argv[0], mtd->name, len)))
511 if (n_found >= maxv - 2) {
512 cmdv[n_found++] = "...";
516 cmdv[n_found++] = mtd->name;
519 cmdv[n_found] = NULL;
523 #endif /* CONFIG_AUTO_COMPLETE */
525 #ifdef CONFIG_SYS_LONGHELP
526 static char mtd_help_text[] =
527 "- generic operations on memory technology devices\n\n"
529 "mtd read[.raw][.oob] <name> <addr> [<off> [<size>]]\n"
530 "mtd dump[.raw][.oob] <name> [<off> [<size>]]\n"
531 "mtd write[.raw][.oob][.dontskipff] <name> <addr> [<off> [<size>]]\n"
532 "mtd erase[.dontskipbad] <name> [<off> [<size>]]\n"
534 "Specific functions:\n"
538 "\t<name>: NAND partition/chip name\n"
539 "\t<addr>: user address from/to which data will be retrieved/stored\n"
540 "\t<off>: offset in <name> in bytes (default: start of the part)\n"
541 "\t\t* must be block-aligned for erase\n"
542 "\t\t* must be page-aligned otherwise\n"
543 "\t<size>: length of the operation in bytes (default: the entire device)\n"
544 "\t\t* must be a multiple of a block for erase\n"
545 "\t\t* must be a multiple of a page otherwise (special case: default is a page with dump)\n"
547 "The .dontskipff option forces writing empty pages, don't use it if unsure.\n";
550 U_BOOT_CMD_WITH_SUBCMDS(mtd, "MTD utils", mtd_help_text,
551 U_BOOT_SUBCMD_MKENT(list, 1, 1, do_mtd_list),
552 U_BOOT_SUBCMD_MKENT_COMPLETE(read, 5, 0, do_mtd_io,
554 U_BOOT_SUBCMD_MKENT_COMPLETE(write, 5, 0, do_mtd_io,
556 U_BOOT_SUBCMD_MKENT_COMPLETE(dump, 4, 0, do_mtd_io,
558 U_BOOT_SUBCMD_MKENT_COMPLETE(erase, 4, 0, do_mtd_erase,
560 U_BOOT_SUBCMD_MKENT_COMPLETE(bad, 2, 1, do_mtd_bad,