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>
18 static uint mtd_len_to_pages(struct mtd_info *mtd, u64 len)
20 do_div(len, mtd->writesize);
25 static bool mtd_is_aligned_with_min_io_size(struct mtd_info *mtd, u64 size)
27 return !do_div(size, mtd->writesize);
30 static bool mtd_is_aligned_with_block_size(struct mtd_info *mtd, u64 size)
32 return !do_div(size, mtd->erasesize);
35 static void mtd_dump_buf(const u8 *buf, uint len, uint offset)
39 for (i = 0; i < len; ) {
40 printf("0x%08x:\t", offset + i);
41 for (j = 0; j < 8; j++)
42 printf("%02x ", buf[i + j]);
45 for (j = 0; j < 8; j++)
46 printf("%02x ", buf[i + j]);
52 static void mtd_dump_device_buf(struct mtd_info *mtd, u64 start_off,
53 const u8 *buf, u64 len, bool woob)
55 bool has_pages = mtd->type == MTD_NANDFLASH ||
56 mtd->type == MTD_MLCNANDFLASH;
57 int npages = mtd_len_to_pages(mtd, len);
61 for (page = 0; page < npages; page++) {
62 u64 data_off = page * mtd->writesize;
64 printf("\nDump %d data bytes from 0x%08llx:\n",
65 mtd->writesize, start_off + data_off);
66 mtd_dump_buf(&buf[data_off],
67 mtd->writesize, start_off + data_off);
70 u64 oob_off = page * mtd->oobsize;
72 printf("Dump %d OOB bytes from page at 0x%08llx:\n",
73 mtd->oobsize, start_off + data_off);
74 mtd_dump_buf(&buf[len + oob_off],
79 printf("\nDump %lld data bytes from 0x%llx:\n",
81 mtd_dump_buf(buf, len, start_off);
85 static void mtd_show_parts(struct mtd_info *mtd, int level)
87 struct mtd_info *part;
90 list_for_each_entry(part, &mtd->partitions, node) {
91 for (i = 0; i < level; i++)
93 printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
94 part->offset, part->offset + part->size, part->name);
96 mtd_show_parts(part, level + 1);
100 static void mtd_show_device(struct mtd_info *mtd)
103 printf("* %s\n", mtd->name);
104 #if defined(CONFIG_DM)
106 printf(" - device: %s\n", mtd->dev->name);
107 printf(" - parent: %s\n", mtd->dev->parent->name);
108 printf(" - driver: %s\n", mtd->dev->driver->name);
112 /* MTD device information */
122 printf("NOR flash\n");
125 printf("NAND flash\n");
128 printf("Data flash\n");
131 printf("UBI volume\n");
133 case MTD_MLCNANDFLASH:
134 printf("MLC NAND flash\n");
142 printf(" - block size: 0x%x bytes\n", mtd->erasesize);
143 printf(" - min I/O: 0x%x bytes\n", mtd->writesize);
146 printf(" - OOB size: %u bytes\n", mtd->oobsize);
147 printf(" - OOB available: %u bytes\n", mtd->oobavail);
150 if (mtd->ecc_strength) {
151 printf(" - ECC strength: %u bits\n", mtd->ecc_strength);
152 printf(" - ECC step size: %u bytes\n", mtd->ecc_step_size);
153 printf(" - bitflip threshold: %u bits\n",
154 mtd->bitflip_threshold);
157 printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
158 mtd->offset, mtd->offset + mtd->size, mtd->name);
160 /* MTD partitions, if any */
161 mtd_show_parts(mtd, 1);
164 /* Logic taken from fs/ubifs/recovery.c:is_empty() */
165 static bool mtd_oob_write_is_empty(struct mtd_oob_ops *op)
169 for (i = 0; i < op->len; i++)
170 if (op->datbuf[i] != 0xff)
173 for (i = 0; i < op->ooblen; i++)
174 if (op->oobbuf[i] != 0xff)
180 static int do_mtd_list(void)
182 struct mtd_info *mtd;
185 /* Ensure all devices (and their partitions) are probed */
188 printf("List of MTD devices:\n");
189 mtd_for_each_device(mtd) {
190 if (!mtd_is_partition(mtd))
191 mtd_show_device(mtd);
197 printf("No MTD device found\n");
198 return CMD_RET_FAILURE;
201 return CMD_RET_SUCCESS;
204 static int mtd_special_write_oob(struct mtd_info *mtd, u64 off,
205 struct mtd_oob_ops *io_op,
206 bool write_empty_pages, bool woob)
211 * By default, do not write an empty page.
212 * Skip it by simulating a successful write.
214 if (!write_empty_pages && mtd_oob_write_is_empty(io_op)) {
215 io_op->retlen = mtd->writesize;
216 io_op->oobretlen = woob ? mtd->oobsize : 0;
218 ret = mtd_write_oob(mtd, off, io_op);
224 static int do_mtd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
226 struct mtd_info *mtd;
230 /* All MTD commands need at least two arguments */
232 return CMD_RET_USAGE;
234 /* Parse the command name and its optional suffixes */
237 /* List the MTD devices if that is what the user wants */
238 if (strcmp(cmd, "list") == 0)
239 return do_mtd_list();
242 * The remaining commands require also at least a device ID.
243 * Check the selected device is valid. Ensure it is probed.
246 return CMD_RET_USAGE;
250 mtd = get_mtd_device_nm(mtd_name);
251 if (IS_ERR_OR_NULL(mtd)) {
252 printf("MTD device %s not found, ret %ld\n",
253 mtd_name, PTR_ERR(mtd));
254 return CMD_RET_FAILURE;
262 if (!strncmp(cmd, "read", 4) || !strncmp(cmd, "dump", 4) ||
263 !strncmp(cmd, "write", 5)) {
264 bool has_pages = mtd->type == MTD_NANDFLASH ||
265 mtd->type == MTD_MLCNANDFLASH;
266 bool dump, read, raw, woob, write_empty_pages;
267 struct mtd_oob_ops io_op = {};
268 uint user_addr = 0, npages;
269 u64 start_off, off, len, remaining, default_len;
274 dump = !strncmp(cmd, "dump", 4);
275 read = dump || !strncmp(cmd, "read", 4);
276 raw = strstr(cmd, ".raw");
277 woob = strstr(cmd, ".oob");
278 write_empty_pages = !has_pages || strstr(cmd, ".dontskipff");
282 return CMD_RET_USAGE;
284 user_addr = simple_strtoul(argv[0], NULL, 16);
289 start_off = argc > 0 ? simple_strtoul(argv[0], NULL, 16) : 0;
290 if (!mtd_is_aligned_with_min_io_size(mtd, start_off)) {
291 printf("Offset not aligned with a page (0x%x)\n",
293 return CMD_RET_FAILURE;
296 default_len = dump ? mtd->writesize : mtd->size;
297 len = argc > 1 ? simple_strtoul(argv[1], NULL, 16) :
299 if (!mtd_is_aligned_with_min_io_size(mtd, len)) {
300 len = round_up(len, mtd->writesize);
301 printf("Size not on a page boundary (0x%x), rounding to 0x%llx\n",
302 mtd->writesize, len);
306 npages = mtd_len_to_pages(mtd, len);
307 oob_len = woob ? npages * mtd->oobsize : 0;
310 buf = kmalloc(len + oob_len, GFP_KERNEL);
312 buf = map_sysmem(user_addr, 0);
315 printf("Could not map/allocate the user buffer\n");
316 return CMD_RET_FAILURE;
320 printf("%s %lld byte(s) (%d page(s)) at offset 0x%08llx%s%s%s\n",
321 read ? "Reading" : "Writing", len, npages, start_off,
322 raw ? " [raw]" : "", woob ? " [oob]" : "",
323 !read && write_empty_pages ? " [dontskipff]" : "");
325 printf("%s %lld byte(s) at offset 0x%08llx\n",
326 read ? "Reading" : "Writing", len, start_off);
328 io_op.mode = raw ? MTD_OPS_RAW : MTD_OPS_AUTO_OOB;
329 io_op.len = has_pages ? mtd->writesize : len;
330 io_op.ooblen = woob ? mtd->oobsize : 0;
332 io_op.oobbuf = woob ? &buf[len] : NULL;
334 /* Search for the first good block after the given offset */
336 while (mtd_block_isbad(mtd, off))
337 off += mtd->erasesize;
339 /* Loop over the pages to do the actual read/write */
341 /* Skip the block if it is bad */
342 if (mtd_is_aligned_with_block_size(mtd, off) &&
343 mtd_block_isbad(mtd, off)) {
344 off += mtd->erasesize;
349 ret = mtd_read_oob(mtd, off, &io_op);
351 ret = mtd_special_write_oob(mtd, off, &io_op,
356 printf("Failure while %s at offset 0x%llx\n",
357 read ? "reading" : "writing", off);
358 return CMD_RET_FAILURE;
362 remaining -= io_op.retlen;
363 io_op.datbuf += io_op.retlen;
364 io_op.oobbuf += io_op.oobretlen;
368 mtd_dump_device_buf(mtd, start_off, buf, len, woob);
376 printf("%s on %s failed with error %d\n",
377 read ? "Read" : "Write", mtd->name, ret);
378 return CMD_RET_FAILURE;
381 } else if (!strcmp(cmd, "erase")) {
382 bool scrub = strstr(cmd, ".dontskipbad");
383 struct erase_info erase_op = {};
387 off = argc > 0 ? simple_strtoul(argv[0], NULL, 16) : 0;
388 len = argc > 1 ? simple_strtoul(argv[1], NULL, 16) : mtd->size;
390 if (!mtd_is_aligned_with_block_size(mtd, off)) {
391 printf("Offset not aligned with a block (0x%x)\n",
393 return CMD_RET_FAILURE;
396 if (!mtd_is_aligned_with_block_size(mtd, len)) {
397 printf("Size not a multiple of a block (0x%x)\n",
399 return CMD_RET_FAILURE;
402 printf("Erasing 0x%08llx ... 0x%08llx (%d eraseblock(s))\n",
403 off, off + len - 1, mtd_div_by_eb(len, mtd));
408 erase_op.scrub = scrub;
410 while (erase_op.len) {
411 ret = mtd_erase(mtd, &erase_op);
413 /* Abort if its not a bad block error */
417 printf("Skipping bad block at 0x%08llx\n",
420 /* Skip bad block and continue behind it */
421 erase_op.len -= erase_op.fail_addr - erase_op.addr;
422 erase_op.len -= mtd->erasesize;
423 erase_op.addr = erase_op.fail_addr + mtd->erasesize;
426 if (ret && ret != -EIO)
427 return CMD_RET_FAILURE;
428 } else if (!strcmp(cmd, "bad")) {
431 if (!mtd_can_have_bb(mtd)) {
432 printf("Only NAND-based devices can have bad blocks\n");
433 return CMD_RET_SUCCESS;
436 printf("MTD device %s bad blocks list:\n", mtd->name);
437 for (off = 0; off < mtd->size; off += mtd->erasesize)
438 if (mtd_block_isbad(mtd, off))
439 printf("\t0x%08llx\n", off);
441 return CMD_RET_USAGE;
444 return CMD_RET_SUCCESS;
447 static char mtd_help_text[] =
448 #ifdef CONFIG_SYS_LONGHELP
449 "- generic operations on memory technology devices\n\n"
451 "mtd read[.raw][.oob] <name> <addr> [<off> [<size>]]\n"
452 "mtd dump[.raw][.oob] <name> [<off> [<size>]]\n"
453 "mtd write[.raw][.oob][.dontskipff] <name> <addr> [<off> [<size>]]\n"
454 "mtd erase[.dontskipbad] <name> [<off> [<size>]]\n"
456 "Specific functions:\n"
460 "\t<name>: NAND partition/chip name\n"
461 "\t<addr>: user address from/to which data will be retrieved/stored\n"
462 "\t<off>: offset in <name> in bytes (default: start of the part)\n"
463 "\t\t* must be block-aligned for erase\n"
464 "\t\t* must be page-aligned otherwise\n"
465 "\t<size>: length of the operation in bytes (default: the entire device)\n"
466 "\t\t* must be a multiple of a block for erase\n"
467 "\t\t* must be a multiple of a page otherwise (special case: default is a page with dump)\n"
469 "The .dontskipff option forces writing empty pages, don't use it if unsure.\n"
473 U_BOOT_CMD(mtd, 10, 1, do_mtd, "MTD utils", mtd_help_text);