]> Git Repo - u-boot.git/blame - cmd/mtd.c
Merge tag 'ti-v2021.01-rc1' of https://gitlab.denx.de/u-boot/custodians/u-boot-ti
[u-boot.git] / cmd / mtd.c
CommitLineData
5db66b3a
MR
1// SPDX-License-Identifier: GPL-2.0+
2/*
3 * mtd.c
4 *
5 * Generic command to handle basic operations on any memory device.
6 *
7 * Copyright: Bootlin, 2018
8 * Author: Miquèl Raynal <[email protected]>
9 */
10
11#include <command.h>
12#include <common.h>
13#include <console.h>
14#include <malloc.h>
15#include <mapmem.h>
16#include <mtd.h>
61b29b82
SG
17#include <dm/devres.h>
18#include <linux/err.h>
5db66b3a 19
9671243e
BB
20#include <linux/ctype.h>
21
22static struct mtd_info *get_mtd_by_name(const char *name)
23{
24 struct mtd_info *mtd;
25
26 mtd_probe_devices();
27
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,
31 PTR_ERR(mtd));
32
33 return mtd;
34}
35
5db66b3a
MR
36static uint mtd_len_to_pages(struct mtd_info *mtd, u64 len)
37{
38 do_div(len, mtd->writesize);
39
40 return len;
41}
42
43static bool mtd_is_aligned_with_min_io_size(struct mtd_info *mtd, u64 size)
44{
45 return !do_div(size, mtd->writesize);
46}
47
48static bool mtd_is_aligned_with_block_size(struct mtd_info *mtd, u64 size)
49{
50 return !do_div(size, mtd->erasesize);
51}
52
53static void mtd_dump_buf(const u8 *buf, uint len, uint offset)
54{
55 int i, j;
56
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]);
61 printf(" ");
62 i += 8;
63 for (j = 0; j < 8; j++)
64 printf("%02x ", buf[i + j]);
65 printf("\n");
66 i += 8;
67 }
68}
69
70static void mtd_dump_device_buf(struct mtd_info *mtd, u64 start_off,
71 const u8 *buf, u64 len, bool woob)
72{
73 bool has_pages = mtd->type == MTD_NANDFLASH ||
74 mtd->type == MTD_MLCNANDFLASH;
75 int npages = mtd_len_to_pages(mtd, len);
76 uint page;
77
78 if (has_pages) {
79 for (page = 0; page < npages; page++) {
80 u64 data_off = page * mtd->writesize;
81
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);
86
87 if (woob) {
88 u64 oob_off = page * mtd->oobsize;
89
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],
93 mtd->oobsize, 0);
94 }
95 }
96 } else {
97 printf("\nDump %lld data bytes from 0x%llx:\n",
98 len, start_off);
99 mtd_dump_buf(buf, len, start_off);
100 }
101}
102
103static void mtd_show_parts(struct mtd_info *mtd, int level)
104{
105 struct mtd_info *part;
106 int i;
107
108 list_for_each_entry(part, &mtd->partitions, node) {
109 for (i = 0; i < level; i++)
110 printf("\t");
111 printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
112 part->offset, part->offset + part->size, part->name);
113
114 mtd_show_parts(part, level + 1);
115 }
116}
117
118static void mtd_show_device(struct mtd_info *mtd)
119{
120 /* Device */
121 printf("* %s\n", mtd->name);
122#if defined(CONFIG_DM)
123 if (mtd->dev) {
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);
127 }
128#endif
129
130 /* MTD device information */
131 printf(" - type: ");
132 switch (mtd->type) {
133 case MTD_RAM:
134 printf("RAM\n");
135 break;
136 case MTD_ROM:
137 printf("ROM\n");
138 break;
139 case MTD_NORFLASH:
140 printf("NOR flash\n");
141 break;
142 case MTD_NANDFLASH:
143 printf("NAND flash\n");
144 break;
145 case MTD_DATAFLASH:
146 printf("Data flash\n");
147 break;
148 case MTD_UBIVOLUME:
149 printf("UBI volume\n");
150 break;
151 case MTD_MLCNANDFLASH:
152 printf("MLC NAND flash\n");
153 break;
154 case MTD_ABSENT:
155 default:
156 printf("Unknown\n");
157 break;
158 }
159
160 printf(" - block size: 0x%x bytes\n", mtd->erasesize);
161 printf(" - min I/O: 0x%x bytes\n", mtd->writesize);
162
163 if (mtd->oobsize) {
164 printf(" - OOB size: %u bytes\n", mtd->oobsize);
165 printf(" - OOB available: %u bytes\n", mtd->oobavail);
166 }
167
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);
173 }
174
175 printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
176 mtd->offset, mtd->offset + mtd->size, mtd->name);
177
178 /* MTD partitions, if any */
179 mtd_show_parts(mtd, 1);
180}
181
182/* Logic taken from fs/ubifs/recovery.c:is_empty() */
183static bool mtd_oob_write_is_empty(struct mtd_oob_ops *op)
184{
185 int i;
186
187 for (i = 0; i < op->len; i++)
188 if (op->datbuf[i] != 0xff)
189 return false;
190
191 for (i = 0; i < op->ooblen; i++)
192 if (op->oobbuf[i] != 0xff)
193 return false;
194
195 return true;
196}
197
09140113
SG
198static int do_mtd_list(struct cmd_tbl *cmdtp, int flag, int argc,
199 char *const argv[])
5db66b3a
MR
200{
201 struct mtd_info *mtd;
202 int dev_nb = 0;
203
204 /* Ensure all devices (and their partitions) are probed */
205 mtd_probe_devices();
206
207 printf("List of MTD devices:\n");
208 mtd_for_each_device(mtd) {
209 if (!mtd_is_partition(mtd))
210 mtd_show_device(mtd);
211
212 dev_nb++;
213 }
214
215 if (!dev_nb) {
216 printf("No MTD device found\n");
217 return CMD_RET_FAILURE;
218 }
219
220 return CMD_RET_SUCCESS;
221}
222
223static 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)
226{
227 int ret = 0;
228
229 /*
230 * By default, do not write an empty page.
231 * Skip it by simulating a successful write.
232 */
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;
236 } else {
237 ret = mtd_write_oob(mtd, off, io_op);
238 }
239
240 return ret;
241}
242
09140113
SG
243static int do_mtd_io(struct cmd_tbl *cmdtp, int flag, int argc,
244 char *const argv[])
5db66b3a 245{
9671243e
BB
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];
5db66b3a 251 struct mtd_info *mtd;
9671243e
BB
252 u32 oob_len;
253 u8 *buf;
254 int ret;
5db66b3a 255
5db66b3a
MR
256 if (argc < 2)
257 return CMD_RET_USAGE;
258
9671243e
BB
259 mtd = get_mtd_by_name(argv[1]);
260 if (IS_ERR_OR_NULL(mtd))
261 return CMD_RET_FAILURE;
5db66b3a 262
9671243e
BB
263 if (mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH)
264 has_pages = true;
5db66b3a 265
9671243e
BB
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");
5db66b3a 271
9671243e
BB
272 argc -= 2;
273 argv += 2;
5db66b3a 274
9671243e
BB
275 if (!dump) {
276 if (!argc) {
277 ret = CMD_RET_USAGE;
278 goto out_put_mtd;
5db66b3a
MR
279 }
280
9671243e
BB
281 user_addr = simple_strtoul(argv[0], NULL, 16);
282 argc--;
283 argv++;
284 }
5db66b3a 285
9671243e
BB
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",
289 mtd->writesize);
290 ret = CMD_RET_FAILURE;
291 goto out_put_mtd;
292 }
5db66b3a 293
9671243e
BB
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);
300 }
5db66b3a 301
9671243e
BB
302 remaining = len;
303 npages = mtd_len_to_pages(mtd, len);
304 oob_len = woob ? npages * mtd->oobsize : 0;
5db66b3a 305
9671243e
BB
306 if (dump)
307 buf = kmalloc(len + oob_len, GFP_KERNEL);
308 else
309 buf = map_sysmem(user_addr, 0);
310
311 if (!buf) {
312 printf("Could not map/allocate the user buffer\n");
313 ret = CMD_RET_FAILURE;
314 goto out_put_mtd;
315 }
316
317 if (has_pages)
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]" : "");
322 else
323 printf("%s %lld byte(s) at offset 0x%08llx\n",
324 read ? "Reading" : "Writing", len, start_off);
325
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;
329 io_op.datbuf = buf;
330 io_op.oobbuf = woob ? &buf[len] : NULL;
331
332 /* Search for the first good block after the given offset */
333 off = start_off;
334 while (mtd_block_isbad(mtd, off))
335 off += mtd->erasesize;
336
337 /* Loop over the pages to do the actual read/write */
338 while (remaining) {
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;
343 continue;
5db66b3a
MR
344 }
345
9671243e
BB
346 if (read)
347 ret = mtd_read_oob(mtd, off, &io_op);
5db66b3a 348 else
9671243e
BB
349 ret = mtd_special_write_oob(mtd, off, &io_op,
350 write_empty_pages, woob);
5db66b3a 351
9671243e
BB
352 if (ret) {
353 printf("Failure while %s at offset 0x%llx\n",
354 read ? "reading" : "writing", off);
355 break;
356 }
5db66b3a 357
9671243e
BB
358 off += io_op.retlen;
359 remaining -= io_op.retlen;
360 io_op.datbuf += io_op.retlen;
361 io_op.oobbuf += io_op.oobretlen;
362 }
5db66b3a 363
9671243e
BB
364 if (!ret && dump)
365 mtd_dump_device_buf(mtd, start_off, buf, len, woob);
5db66b3a 366
9671243e
BB
367 if (dump)
368 kfree(buf);
369 else
370 unmap_sysmem(buf);
5db66b3a 371
9671243e
BB
372 if (ret) {
373 printf("%s on %s failed with error %d\n",
374 read ? "Read" : "Write", mtd->name, ret);
375 ret = CMD_RET_FAILURE;
376 } else {
377 ret = CMD_RET_SUCCESS;
378 }
5db66b3a 379
9671243e
BB
380out_put_mtd:
381 put_mtd_device(mtd);
5db66b3a 382
9671243e
BB
383 return ret;
384}
5db66b3a 385
09140113
SG
386static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc,
387 char *const argv[])
9671243e
BB
388{
389 struct erase_info erase_op = {};
390 struct mtd_info *mtd;
391 u64 off, len;
392 bool scrub;
b1b147f2 393 int ret = 0;
5db66b3a 394
9671243e
BB
395 if (argc < 2)
396 return CMD_RET_USAGE;
5db66b3a 397
9671243e
BB
398 mtd = get_mtd_by_name(argv[1]);
399 if (IS_ERR_OR_NULL(mtd))
400 return CMD_RET_FAILURE;
5db66b3a 401
9671243e 402 scrub = strstr(argv[0], ".dontskipbad");
5db66b3a 403
9671243e
BB
404 argc -= 2;
405 argv += 2;
5db66b3a 406
9671243e
BB
407 off = argc > 0 ? simple_strtoul(argv[0], NULL, 16) : 0;
408 len = argc > 1 ? simple_strtoul(argv[1], NULL, 16) : mtd->size;
5db66b3a 409
9671243e
BB
410 if (!mtd_is_aligned_with_block_size(mtd, off)) {
411 printf("Offset not aligned with a block (0x%x)\n",
412 mtd->erasesize);
413 ret = CMD_RET_FAILURE;
414 goto out_put_mtd;
415 }
5db66b3a 416
9671243e
BB
417 if (!mtd_is_aligned_with_block_size(mtd, len)) {
418 printf("Size not a multiple of a block (0x%x)\n",
419 mtd->erasesize);
420 ret = CMD_RET_FAILURE;
421 goto out_put_mtd;
422 }
5db66b3a 423
9671243e
BB
424 printf("Erasing 0x%08llx ... 0x%08llx (%d eraseblock(s))\n",
425 off, off + len - 1, mtd_div_by_eb(len, mtd));
5db66b3a 426
9671243e
BB
427 erase_op.mtd = mtd;
428 erase_op.addr = off;
b1b147f2 429 erase_op.len = mtd->erasesize;
9671243e 430 erase_op.scrub = scrub;
5db66b3a 431
b1b147f2 432 while (len) {
9671243e 433 ret = mtd_erase(mtd, &erase_op);
5db66b3a 434
b1b147f2
PD
435 if (ret) {
436 /* Abort if its not a bad block error */
437 if (ret != -EIO)
438 break;
439 printf("Skipping bad block at 0x%08llx\n",
440 erase_op.addr);
441 }
9671243e 442
b1b147f2
PD
443 len -= mtd->erasesize;
444 erase_op.addr += mtd->erasesize;
9671243e
BB
445 }
446
447 if (ret && ret != -EIO)
448 ret = CMD_RET_FAILURE;
449 else
450 ret = CMD_RET_SUCCESS;
451
452out_put_mtd:
453 put_mtd_device(mtd);
454
455 return ret;
456}
457
09140113
SG
458static int do_mtd_bad(struct cmd_tbl *cmdtp, int flag, int argc,
459 char *const argv[])
9671243e
BB
460{
461 struct mtd_info *mtd;
462 loff_t off;
463
464 if (argc < 2)
5db66b3a 465 return CMD_RET_USAGE;
9671243e
BB
466
467 mtd = get_mtd_by_name(argv[1]);
468 if (IS_ERR_OR_NULL(mtd))
469 return CMD_RET_FAILURE;
470
471 if (!mtd_can_have_bb(mtd)) {
472 printf("Only NAND-based devices can have bad blocks\n");
473 goto out_put_mtd;
474 }
475
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);
5db66b3a
MR
480 }
481
9671243e
BB
482out_put_mtd:
483 put_mtd_device(mtd);
484
5db66b3a
MR
485 return CMD_RET_SUCCESS;
486}
487
9671243e 488#ifdef CONFIG_AUTO_COMPLETE
09140113 489static int mtd_name_complete(int argc, char *const argv[], char last_char,
9671243e
BB
490 int maxv, char *cmdv[])
491{
492 int len = 0, n_found = 0;
493 struct mtd_info *mtd;
494
495 argc--;
496 argv++;
497
498 if (argc > 1 ||
499 (argc == 1 && (last_char == '\0' || isblank(last_char))))
500 return 0;
501
502 if (argc)
503 len = strlen(argv[0]);
504
505 mtd_for_each_device(mtd) {
506 if (argc &&
507 (len > strlen(mtd->name) ||
508 strncmp(argv[0], mtd->name, len)))
509 continue;
510
511 if (n_found >= maxv - 2) {
512 cmdv[n_found++] = "...";
513 break;
514 }
515
516 cmdv[n_found++] = mtd->name;
517 }
518
519 cmdv[n_found] = NULL;
520
521 return n_found;
522}
523#endif /* CONFIG_AUTO_COMPLETE */
524
5db66b3a 525#ifdef CONFIG_SYS_LONGHELP
a645831c 526static char mtd_help_text[] =
5db66b3a
MR
527 "- generic operations on memory technology devices\n\n"
528 "mtd list\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"
533 "\n"
534 "Specific functions:\n"
535 "mtd bad <name>\n"
536 "\n"
537 "With:\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"
546 "\n"
a645831c 547 "The .dontskipff option forces writing empty pages, don't use it if unsure.\n";
5db66b3a 548#endif
5db66b3a 549
9671243e
BB
550U_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,
553 mtd_name_complete),
554 U_BOOT_SUBCMD_MKENT_COMPLETE(write, 5, 0, do_mtd_io,
555 mtd_name_complete),
556 U_BOOT_SUBCMD_MKENT_COMPLETE(dump, 4, 0, do_mtd_io,
557 mtd_name_complete),
558 U_BOOT_SUBCMD_MKENT_COMPLETE(erase, 4, 0, do_mtd_erase,
559 mtd_name_complete),
560 U_BOOT_SUBCMD_MKENT_COMPLETE(bad, 2, 1, do_mtd_bad,
561 mtd_name_complete));
This page took 0.124191 seconds and 4 git commands to generate.