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 #include <linux/ctype.h>
20 static struct mtd_info
*get_mtd_by_name(const char *name
)
26 mtd
= get_mtd_device_nm(name
);
27 if (IS_ERR_OR_NULL(mtd
))
28 printf("MTD device %s not found, ret %ld\n", name
,
34 static uint
mtd_len_to_pages(struct mtd_info
*mtd
, u64 len
)
36 do_div(len
, mtd
->writesize
);
41 static bool mtd_is_aligned_with_min_io_size(struct mtd_info
*mtd
, u64 size
)
43 return !do_div(size
, mtd
->writesize
);
46 static bool mtd_is_aligned_with_block_size(struct mtd_info
*mtd
, u64 size
)
48 return !do_div(size
, mtd
->erasesize
);
51 static void mtd_dump_buf(const u8
*buf
, uint len
, uint offset
)
55 for (i
= 0; i
< len
; ) {
56 printf("0x%08x:\t", offset
+ i
);
57 for (j
= 0; j
< 8; j
++)
58 printf("%02x ", buf
[i
+ j
]);
61 for (j
= 0; j
< 8; j
++)
62 printf("%02x ", buf
[i
+ j
]);
68 static void mtd_dump_device_buf(struct mtd_info
*mtd
, u64 start_off
,
69 const u8
*buf
, u64 len
, bool woob
)
71 bool has_pages
= mtd
->type
== MTD_NANDFLASH
||
72 mtd
->type
== MTD_MLCNANDFLASH
;
73 int npages
= mtd_len_to_pages(mtd
, len
);
77 for (page
= 0; page
< npages
; page
++) {
78 u64 data_off
= page
* mtd
->writesize
;
80 printf("\nDump %d data bytes from 0x%08llx:\n",
81 mtd
->writesize
, start_off
+ data_off
);
82 mtd_dump_buf(&buf
[data_off
],
83 mtd
->writesize
, start_off
+ data_off
);
86 u64 oob_off
= page
* mtd
->oobsize
;
88 printf("Dump %d OOB bytes from page at 0x%08llx:\n",
89 mtd
->oobsize
, start_off
+ data_off
);
90 mtd_dump_buf(&buf
[len
+ oob_off
],
95 printf("\nDump %lld data bytes from 0x%llx:\n",
97 mtd_dump_buf(buf
, len
, start_off
);
101 static void mtd_show_parts(struct mtd_info
*mtd
, int level
)
103 struct mtd_info
*part
;
106 list_for_each_entry(part
, &mtd
->partitions
, node
) {
107 for (i
= 0; i
< level
; i
++)
109 printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
110 part
->offset
, part
->offset
+ part
->size
, part
->name
);
112 mtd_show_parts(part
, level
+ 1);
116 static void mtd_show_device(struct mtd_info
*mtd
)
119 printf("* %s\n", mtd
->name
);
120 #if defined(CONFIG_DM)
122 printf(" - device: %s\n", mtd
->dev
->name
);
123 printf(" - parent: %s\n", mtd
->dev
->parent
->name
);
124 printf(" - driver: %s\n", mtd
->dev
->driver
->name
);
128 /* MTD device information */
138 printf("NOR flash\n");
141 printf("NAND flash\n");
144 printf("Data flash\n");
147 printf("UBI volume\n");
149 case MTD_MLCNANDFLASH
:
150 printf("MLC NAND flash\n");
158 printf(" - block size: 0x%x bytes\n", mtd
->erasesize
);
159 printf(" - min I/O: 0x%x bytes\n", mtd
->writesize
);
162 printf(" - OOB size: %u bytes\n", mtd
->oobsize
);
163 printf(" - OOB available: %u bytes\n", mtd
->oobavail
);
166 if (mtd
->ecc_strength
) {
167 printf(" - ECC strength: %u bits\n", mtd
->ecc_strength
);
168 printf(" - ECC step size: %u bytes\n", mtd
->ecc_step_size
);
169 printf(" - bitflip threshold: %u bits\n",
170 mtd
->bitflip_threshold
);
173 printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
174 mtd
->offset
, mtd
->offset
+ mtd
->size
, mtd
->name
);
176 /* MTD partitions, if any */
177 mtd_show_parts(mtd
, 1);
180 /* Logic taken from fs/ubifs/recovery.c:is_empty() */
181 static bool mtd_oob_write_is_empty(struct mtd_oob_ops
*op
)
185 for (i
= 0; i
< op
->len
; i
++)
186 if (op
->datbuf
[i
] != 0xff)
189 for (i
= 0; i
< op
->ooblen
; i
++)
190 if (op
->oobbuf
[i
] != 0xff)
196 static int do_mtd_list(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
199 struct mtd_info
*mtd
;
202 /* Ensure all devices (and their partitions) are probed */
205 printf("List of MTD devices:\n");
206 mtd_for_each_device(mtd
) {
207 if (!mtd_is_partition(mtd
))
208 mtd_show_device(mtd
);
214 printf("No MTD device found\n");
215 return CMD_RET_FAILURE
;
218 return CMD_RET_SUCCESS
;
221 static int mtd_special_write_oob(struct mtd_info
*mtd
, u64 off
,
222 struct mtd_oob_ops
*io_op
,
223 bool write_empty_pages
, bool woob
)
228 * By default, do not write an empty page.
229 * Skip it by simulating a successful write.
231 if (!write_empty_pages
&& mtd_oob_write_is_empty(io_op
)) {
232 io_op
->retlen
= mtd
->writesize
;
233 io_op
->oobretlen
= woob
? mtd
->oobsize
: 0;
235 ret
= mtd_write_oob(mtd
, off
, io_op
);
241 static int do_mtd_io(cmd_tbl_t
*cmdtp
, int flag
, int argc
, char * const argv
[])
243 bool dump
, read
, raw
, woob
, write_empty_pages
, has_pages
= false;
244 u64 start_off
, off
, len
, remaining
, default_len
;
245 struct mtd_oob_ops io_op
= {};
246 uint user_addr
= 0, npages
;
247 const char *cmd
= argv
[0];
248 struct mtd_info
*mtd
;
254 return CMD_RET_USAGE
;
256 mtd
= get_mtd_by_name(argv
[1]);
257 if (IS_ERR_OR_NULL(mtd
))
258 return CMD_RET_FAILURE
;
260 if (mtd
->type
== MTD_NANDFLASH
|| mtd
->type
== MTD_MLCNANDFLASH
)
263 dump
= !strncmp(cmd
, "dump", 4);
264 read
= dump
|| !strncmp(cmd
, "read", 4);
265 raw
= strstr(cmd
, ".raw");
266 woob
= strstr(cmd
, ".oob");
267 write_empty_pages
= !has_pages
|| strstr(cmd
, ".dontskipff");
278 user_addr
= simple_strtoul(argv
[0], NULL
, 16);
283 start_off
= argc
> 0 ? simple_strtoul(argv
[0], NULL
, 16) : 0;
284 if (!mtd_is_aligned_with_min_io_size(mtd
, start_off
)) {
285 printf("Offset not aligned with a page (0x%x)\n",
287 ret
= CMD_RET_FAILURE
;
291 default_len
= dump
? mtd
->writesize
: mtd
->size
;
292 len
= argc
> 1 ? simple_strtoul(argv
[1], NULL
, 16) : default_len
;
293 if (!mtd_is_aligned_with_min_io_size(mtd
, len
)) {
294 len
= round_up(len
, mtd
->writesize
);
295 printf("Size not on a page boundary (0x%x), rounding to 0x%llx\n",
296 mtd
->writesize
, len
);
300 npages
= mtd_len_to_pages(mtd
, len
);
301 oob_len
= woob
? npages
* mtd
->oobsize
: 0;
304 buf
= kmalloc(len
+ oob_len
, GFP_KERNEL
);
306 buf
= map_sysmem(user_addr
, 0);
309 printf("Could not map/allocate the user buffer\n");
310 ret
= CMD_RET_FAILURE
;
315 printf("%s %lld byte(s) (%d page(s)) at offset 0x%08llx%s%s%s\n",
316 read
? "Reading" : "Writing", len
, npages
, start_off
,
317 raw
? " [raw]" : "", woob
? " [oob]" : "",
318 !read
&& write_empty_pages
? " [dontskipff]" : "");
320 printf("%s %lld byte(s) at offset 0x%08llx\n",
321 read
? "Reading" : "Writing", len
, start_off
);
323 io_op
.mode
= raw
? MTD_OPS_RAW
: MTD_OPS_AUTO_OOB
;
324 io_op
.len
= has_pages
? mtd
->writesize
: len
;
325 io_op
.ooblen
= woob
? mtd
->oobsize
: 0;
327 io_op
.oobbuf
= woob
? &buf
[len
] : NULL
;
329 /* Search for the first good block after the given offset */
331 while (mtd_block_isbad(mtd
, off
))
332 off
+= mtd
->erasesize
;
334 /* Loop over the pages to do the actual read/write */
336 /* Skip the block if it is bad */
337 if (mtd_is_aligned_with_block_size(mtd
, off
) &&
338 mtd_block_isbad(mtd
, off
)) {
339 off
+= mtd
->erasesize
;
344 ret
= mtd_read_oob(mtd
, off
, &io_op
);
346 ret
= mtd_special_write_oob(mtd
, off
, &io_op
,
347 write_empty_pages
, woob
);
350 printf("Failure while %s at offset 0x%llx\n",
351 read
? "reading" : "writing", off
);
356 remaining
-= io_op
.retlen
;
357 io_op
.datbuf
+= io_op
.retlen
;
358 io_op
.oobbuf
+= io_op
.oobretlen
;
362 mtd_dump_device_buf(mtd
, start_off
, buf
, len
, woob
);
370 printf("%s on %s failed with error %d\n",
371 read
? "Read" : "Write", mtd
->name
, ret
);
372 ret
= CMD_RET_FAILURE
;
374 ret
= CMD_RET_SUCCESS
;
383 static int do_mtd_erase(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
386 struct erase_info erase_op
= {};
387 struct mtd_info
*mtd
;
393 return CMD_RET_USAGE
;
395 mtd
= get_mtd_by_name(argv
[1]);
396 if (IS_ERR_OR_NULL(mtd
))
397 return CMD_RET_FAILURE
;
399 scrub
= strstr(argv
[0], ".dontskipbad");
404 off
= argc
> 0 ? simple_strtoul(argv
[0], NULL
, 16) : 0;
405 len
= argc
> 1 ? simple_strtoul(argv
[1], NULL
, 16) : mtd
->size
;
407 if (!mtd_is_aligned_with_block_size(mtd
, off
)) {
408 printf("Offset not aligned with a block (0x%x)\n",
410 ret
= CMD_RET_FAILURE
;
414 if (!mtd_is_aligned_with_block_size(mtd
, len
)) {
415 printf("Size not a multiple of a block (0x%x)\n",
417 ret
= CMD_RET_FAILURE
;
421 printf("Erasing 0x%08llx ... 0x%08llx (%d eraseblock(s))\n",
422 off
, off
+ len
- 1, mtd_div_by_eb(len
, mtd
));
427 erase_op
.scrub
= scrub
;
429 while (erase_op
.len
) {
430 ret
= mtd_erase(mtd
, &erase_op
);
432 /* Abort if its not a bad block error */
436 printf("Skipping bad block at 0x%08llx\n", erase_op
.fail_addr
);
438 /* Skip bad block and continue behind it */
439 erase_op
.len
-= erase_op
.fail_addr
- erase_op
.addr
;
440 erase_op
.len
-= mtd
->erasesize
;
441 erase_op
.addr
= erase_op
.fail_addr
+ mtd
->erasesize
;
444 if (ret
&& ret
!= -EIO
)
445 ret
= CMD_RET_FAILURE
;
447 ret
= CMD_RET_SUCCESS
;
455 static int do_mtd_bad(cmd_tbl_t
*cmdtp
, int flag
, int argc
,
458 struct mtd_info
*mtd
;
462 return CMD_RET_USAGE
;
464 mtd
= get_mtd_by_name(argv
[1]);
465 if (IS_ERR_OR_NULL(mtd
))
466 return CMD_RET_FAILURE
;
468 if (!mtd_can_have_bb(mtd
)) {
469 printf("Only NAND-based devices can have bad blocks\n");
473 printf("MTD device %s bad blocks list:\n", mtd
->name
);
474 for (off
= 0; off
< mtd
->size
; off
+= mtd
->erasesize
) {
475 if (mtd_block_isbad(mtd
, off
))
476 printf("\t0x%08llx\n", off
);
482 return CMD_RET_SUCCESS
;
485 #ifdef CONFIG_AUTO_COMPLETE
486 static int mtd_name_complete(int argc
, char * const argv
[], char last_char
,
487 int maxv
, char *cmdv
[])
489 int len
= 0, n_found
= 0;
490 struct mtd_info
*mtd
;
496 (argc
== 1 && (last_char
== '\0' || isblank(last_char
))))
500 len
= strlen(argv
[0]);
502 mtd_for_each_device(mtd
) {
504 (len
> strlen(mtd
->name
) ||
505 strncmp(argv
[0], mtd
->name
, len
)))
508 if (n_found
>= maxv
- 2) {
509 cmdv
[n_found
++] = "...";
513 cmdv
[n_found
++] = mtd
->name
;
516 cmdv
[n_found
] = NULL
;
520 #endif /* CONFIG_AUTO_COMPLETE */
522 #ifdef CONFIG_SYS_LONGHELP
523 static char mtd_help_text
[] =
524 "- generic operations on memory technology devices\n\n"
526 "mtd read[.raw][.oob] <name> <addr> [<off> [<size>]]\n"
527 "mtd dump[.raw][.oob] <name> [<off> [<size>]]\n"
528 "mtd write[.raw][.oob][.dontskipff] <name> <addr> [<off> [<size>]]\n"
529 "mtd erase[.dontskipbad] <name> [<off> [<size>]]\n"
531 "Specific functions:\n"
535 "\t<name>: NAND partition/chip name\n"
536 "\t<addr>: user address from/to which data will be retrieved/stored\n"
537 "\t<off>: offset in <name> in bytes (default: start of the part)\n"
538 "\t\t* must be block-aligned for erase\n"
539 "\t\t* must be page-aligned otherwise\n"
540 "\t<size>: length of the operation in bytes (default: the entire device)\n"
541 "\t\t* must be a multiple of a block for erase\n"
542 "\t\t* must be a multiple of a page otherwise (special case: default is a page with dump)\n"
544 "The .dontskipff option forces writing empty pages, don't use it if unsure.\n";
547 U_BOOT_CMD_WITH_SUBCMDS(mtd
, "MTD utils", mtd_help_text
,
548 U_BOOT_SUBCMD_MKENT(list
, 1, 1, do_mtd_list
),
549 U_BOOT_SUBCMD_MKENT_COMPLETE(read
, 5, 0, do_mtd_io
,
551 U_BOOT_SUBCMD_MKENT_COMPLETE(write
, 5, 0, do_mtd_io
,
553 U_BOOT_SUBCMD_MKENT_COMPLETE(dump
, 4, 0, do_mtd_io
,
555 U_BOOT_SUBCMD_MKENT_COMPLETE(erase
, 4, 0, do_mtd_erase
,
557 U_BOOT_SUBCMD_MKENT_COMPLETE(bad
, 2, 1, do_mtd_bad
,