Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 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 <miquel.raynal@bootlin.com> |
| 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> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 17 | #include <dm/devres.h> |
| 18 | #include <linux/err.h> |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 19 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 20 | #include <linux/ctype.h> |
| 21 | |
| 22 | static 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 | |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 36 | static uint mtd_len_to_pages(struct mtd_info *mtd, u64 len) |
| 37 | { |
| 38 | do_div(len, mtd->writesize); |
| 39 | |
| 40 | return len; |
| 41 | } |
| 42 | |
| 43 | static bool mtd_is_aligned_with_min_io_size(struct mtd_info *mtd, u64 size) |
| 44 | { |
| 45 | return !do_div(size, mtd->writesize); |
| 46 | } |
| 47 | |
| 48 | static bool mtd_is_aligned_with_block_size(struct mtd_info *mtd, u64 size) |
| 49 | { |
| 50 | return !do_div(size, mtd->erasesize); |
| 51 | } |
| 52 | |
| 53 | static 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 | |
| 70 | static 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 | |
| 103 | static 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 | |
| 118 | static 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 |
Marek Behún | 0b6f907 | 2021-05-26 14:08:24 +0200 | [diff] [blame] | 129 | if (IS_ENABLED(CONFIG_OF_CONTROL) && mtd->dev) { |
| 130 | char buf[256]; |
| 131 | int res; |
| 132 | |
| 133 | res = ofnode_get_path(mtd_get_ofnode(mtd), buf, 256); |
| 134 | printf(" - path: %s\n", res == 0 ? buf : "unavailable"); |
| 135 | } |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 136 | |
| 137 | /* MTD device information */ |
| 138 | printf(" - type: "); |
| 139 | switch (mtd->type) { |
| 140 | case MTD_RAM: |
| 141 | printf("RAM\n"); |
| 142 | break; |
| 143 | case MTD_ROM: |
| 144 | printf("ROM\n"); |
| 145 | break; |
| 146 | case MTD_NORFLASH: |
| 147 | printf("NOR flash\n"); |
| 148 | break; |
| 149 | case MTD_NANDFLASH: |
| 150 | printf("NAND flash\n"); |
| 151 | break; |
| 152 | case MTD_DATAFLASH: |
| 153 | printf("Data flash\n"); |
| 154 | break; |
| 155 | case MTD_UBIVOLUME: |
| 156 | printf("UBI volume\n"); |
| 157 | break; |
| 158 | case MTD_MLCNANDFLASH: |
| 159 | printf("MLC NAND flash\n"); |
| 160 | break; |
| 161 | case MTD_ABSENT: |
| 162 | default: |
| 163 | printf("Unknown\n"); |
| 164 | break; |
| 165 | } |
| 166 | |
| 167 | printf(" - block size: 0x%x bytes\n", mtd->erasesize); |
| 168 | printf(" - min I/O: 0x%x bytes\n", mtd->writesize); |
| 169 | |
| 170 | if (mtd->oobsize) { |
| 171 | printf(" - OOB size: %u bytes\n", mtd->oobsize); |
| 172 | printf(" - OOB available: %u bytes\n", mtd->oobavail); |
| 173 | } |
| 174 | |
| 175 | if (mtd->ecc_strength) { |
| 176 | printf(" - ECC strength: %u bits\n", mtd->ecc_strength); |
| 177 | printf(" - ECC step size: %u bytes\n", mtd->ecc_step_size); |
| 178 | printf(" - bitflip threshold: %u bits\n", |
| 179 | mtd->bitflip_threshold); |
| 180 | } |
| 181 | |
| 182 | printf(" - 0x%012llx-0x%012llx : \"%s\"\n", |
| 183 | mtd->offset, mtd->offset + mtd->size, mtd->name); |
| 184 | |
| 185 | /* MTD partitions, if any */ |
| 186 | mtd_show_parts(mtd, 1); |
| 187 | } |
| 188 | |
| 189 | /* Logic taken from fs/ubifs/recovery.c:is_empty() */ |
| 190 | static bool mtd_oob_write_is_empty(struct mtd_oob_ops *op) |
| 191 | { |
| 192 | int i; |
| 193 | |
| 194 | for (i = 0; i < op->len; i++) |
| 195 | if (op->datbuf[i] != 0xff) |
| 196 | return false; |
| 197 | |
| 198 | for (i = 0; i < op->ooblen; i++) |
| 199 | if (op->oobbuf[i] != 0xff) |
| 200 | return false; |
| 201 | |
| 202 | return true; |
| 203 | } |
| 204 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 205 | static int do_mtd_list(struct cmd_tbl *cmdtp, int flag, int argc, |
| 206 | char *const argv[]) |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 207 | { |
| 208 | struct mtd_info *mtd; |
| 209 | int dev_nb = 0; |
| 210 | |
| 211 | /* Ensure all devices (and their partitions) are probed */ |
| 212 | mtd_probe_devices(); |
| 213 | |
| 214 | printf("List of MTD devices:\n"); |
| 215 | mtd_for_each_device(mtd) { |
| 216 | if (!mtd_is_partition(mtd)) |
| 217 | mtd_show_device(mtd); |
| 218 | |
| 219 | dev_nb++; |
| 220 | } |
| 221 | |
| 222 | if (!dev_nb) { |
| 223 | printf("No MTD device found\n"); |
| 224 | return CMD_RET_FAILURE; |
| 225 | } |
| 226 | |
| 227 | return CMD_RET_SUCCESS; |
| 228 | } |
| 229 | |
| 230 | static int mtd_special_write_oob(struct mtd_info *mtd, u64 off, |
| 231 | struct mtd_oob_ops *io_op, |
| 232 | bool write_empty_pages, bool woob) |
| 233 | { |
| 234 | int ret = 0; |
| 235 | |
| 236 | /* |
| 237 | * By default, do not write an empty page. |
| 238 | * Skip it by simulating a successful write. |
| 239 | */ |
| 240 | if (!write_empty_pages && mtd_oob_write_is_empty(io_op)) { |
| 241 | io_op->retlen = mtd->writesize; |
| 242 | io_op->oobretlen = woob ? mtd->oobsize : 0; |
| 243 | } else { |
| 244 | ret = mtd_write_oob(mtd, off, io_op); |
| 245 | } |
| 246 | |
| 247 | return ret; |
| 248 | } |
| 249 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 250 | static int do_mtd_io(struct cmd_tbl *cmdtp, int flag, int argc, |
| 251 | char *const argv[]) |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 252 | { |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 253 | bool dump, read, raw, woob, write_empty_pages, has_pages = false; |
| 254 | u64 start_off, off, len, remaining, default_len; |
| 255 | struct mtd_oob_ops io_op = {}; |
| 256 | uint user_addr = 0, npages; |
| 257 | const char *cmd = argv[0]; |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 258 | struct mtd_info *mtd; |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 259 | u32 oob_len; |
| 260 | u8 *buf; |
| 261 | int ret; |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 262 | |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 263 | if (argc < 2) |
| 264 | return CMD_RET_USAGE; |
| 265 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 266 | mtd = get_mtd_by_name(argv[1]); |
| 267 | if (IS_ERR_OR_NULL(mtd)) |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 268 | return CMD_RET_FAILURE; |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 269 | |
| 270 | if (mtd->type == MTD_NANDFLASH || mtd->type == MTD_MLCNANDFLASH) |
| 271 | has_pages = true; |
| 272 | |
| 273 | dump = !strncmp(cmd, "dump", 4); |
| 274 | read = dump || !strncmp(cmd, "read", 4); |
| 275 | raw = strstr(cmd, ".raw"); |
| 276 | woob = strstr(cmd, ".oob"); |
| 277 | write_empty_pages = !has_pages || strstr(cmd, ".dontskipff"); |
| 278 | |
| 279 | argc -= 2; |
| 280 | argv += 2; |
| 281 | |
| 282 | if (!dump) { |
| 283 | if (!argc) { |
| 284 | ret = CMD_RET_USAGE; |
| 285 | goto out_put_mtd; |
| 286 | } |
| 287 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 288 | user_addr = hextoul(argv[0], NULL); |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 289 | argc--; |
| 290 | argv++; |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 291 | } |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 292 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 293 | start_off = argc > 0 ? hextoul(argv[0], NULL) : 0; |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 294 | if (!mtd_is_aligned_with_min_io_size(mtd, start_off)) { |
| 295 | printf("Offset not aligned with a page (0x%x)\n", |
| 296 | mtd->writesize); |
| 297 | ret = CMD_RET_FAILURE; |
| 298 | goto out_put_mtd; |
| 299 | } |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 300 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 301 | default_len = dump ? mtd->writesize : mtd->size; |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 302 | len = argc > 1 ? hextoul(argv[1], NULL) : default_len; |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 303 | if (!mtd_is_aligned_with_min_io_size(mtd, len)) { |
| 304 | len = round_up(len, mtd->writesize); |
| 305 | printf("Size not on a page boundary (0x%x), rounding to 0x%llx\n", |
| 306 | mtd->writesize, len); |
| 307 | } |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 308 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 309 | remaining = len; |
| 310 | npages = mtd_len_to_pages(mtd, len); |
| 311 | oob_len = woob ? npages * mtd->oobsize : 0; |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 312 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 313 | if (dump) |
| 314 | buf = kmalloc(len + oob_len, GFP_KERNEL); |
| 315 | else |
| 316 | buf = map_sysmem(user_addr, 0); |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 317 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 318 | if (!buf) { |
| 319 | printf("Could not map/allocate the user buffer\n"); |
| 320 | ret = CMD_RET_FAILURE; |
| 321 | goto out_put_mtd; |
| 322 | } |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 323 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 324 | if (has_pages) |
| 325 | printf("%s %lld byte(s) (%d page(s)) at offset 0x%08llx%s%s%s\n", |
| 326 | read ? "Reading" : "Writing", len, npages, start_off, |
| 327 | raw ? " [raw]" : "", woob ? " [oob]" : "", |
| 328 | !read && write_empty_pages ? " [dontskipff]" : ""); |
| 329 | else |
| 330 | printf("%s %lld byte(s) at offset 0x%08llx\n", |
| 331 | read ? "Reading" : "Writing", len, start_off); |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 332 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 333 | io_op.mode = raw ? MTD_OPS_RAW : MTD_OPS_AUTO_OOB; |
| 334 | io_op.len = has_pages ? mtd->writesize : len; |
| 335 | io_op.ooblen = woob ? mtd->oobsize : 0; |
| 336 | io_op.datbuf = buf; |
| 337 | io_op.oobbuf = woob ? &buf[len] : NULL; |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 338 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 339 | /* Search for the first good block after the given offset */ |
| 340 | off = start_off; |
| 341 | while (mtd_block_isbad(mtd, off)) |
| 342 | off += mtd->erasesize; |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 343 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 344 | /* Loop over the pages to do the actual read/write */ |
| 345 | while (remaining) { |
| 346 | /* Skip the block if it is bad */ |
| 347 | if (mtd_is_aligned_with_block_size(mtd, off) && |
| 348 | mtd_block_isbad(mtd, off)) { |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 349 | off += mtd->erasesize; |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 350 | continue; |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 351 | } |
| 352 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 353 | if (read) |
| 354 | ret = mtd_read_oob(mtd, off, &io_op); |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 355 | else |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 356 | ret = mtd_special_write_oob(mtd, off, &io_op, |
| 357 | write_empty_pages, woob); |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 358 | |
| 359 | if (ret) { |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 360 | printf("Failure while %s at offset 0x%llx\n", |
| 361 | read ? "reading" : "writing", off); |
| 362 | break; |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 363 | } |
| 364 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 365 | off += io_op.retlen; |
| 366 | remaining -= io_op.retlen; |
| 367 | io_op.datbuf += io_op.retlen; |
| 368 | io_op.oobbuf += io_op.oobretlen; |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 369 | } |
| 370 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 371 | if (!ret && dump) |
| 372 | mtd_dump_device_buf(mtd, start_off, buf, len, woob); |
| 373 | |
| 374 | if (dump) |
| 375 | kfree(buf); |
| 376 | else |
| 377 | unmap_sysmem(buf); |
| 378 | |
| 379 | if (ret) { |
| 380 | printf("%s on %s failed with error %d\n", |
| 381 | read ? "Read" : "Write", mtd->name, ret); |
| 382 | ret = CMD_RET_FAILURE; |
| 383 | } else { |
| 384 | ret = CMD_RET_SUCCESS; |
| 385 | } |
| 386 | |
| 387 | out_put_mtd: |
| 388 | put_mtd_device(mtd); |
| 389 | |
| 390 | return ret; |
| 391 | } |
| 392 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 393 | static int do_mtd_erase(struct cmd_tbl *cmdtp, int flag, int argc, |
| 394 | char *const argv[]) |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 395 | { |
| 396 | struct erase_info erase_op = {}; |
| 397 | struct mtd_info *mtd; |
| 398 | u64 off, len; |
| 399 | bool scrub; |
Patrick Delaunay | b1b147f | 2019-09-20 09:20:12 +0200 | [diff] [blame] | 400 | int ret = 0; |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 401 | |
| 402 | if (argc < 2) |
| 403 | return CMD_RET_USAGE; |
| 404 | |
| 405 | mtd = get_mtd_by_name(argv[1]); |
| 406 | if (IS_ERR_OR_NULL(mtd)) |
| 407 | return CMD_RET_FAILURE; |
| 408 | |
| 409 | scrub = strstr(argv[0], ".dontskipbad"); |
| 410 | |
| 411 | argc -= 2; |
| 412 | argv += 2; |
| 413 | |
Simon Glass | 7e5f460 | 2021-07-24 09:03:29 -0600 | [diff] [blame] | 414 | off = argc > 0 ? hextoul(argv[0], NULL) : 0; |
| 415 | len = argc > 1 ? hextoul(argv[1], NULL) : mtd->size; |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 416 | |
| 417 | if (!mtd_is_aligned_with_block_size(mtd, off)) { |
| 418 | printf("Offset not aligned with a block (0x%x)\n", |
| 419 | mtd->erasesize); |
| 420 | ret = CMD_RET_FAILURE; |
| 421 | goto out_put_mtd; |
| 422 | } |
| 423 | |
| 424 | if (!mtd_is_aligned_with_block_size(mtd, len)) { |
| 425 | printf("Size not a multiple of a block (0x%x)\n", |
| 426 | mtd->erasesize); |
| 427 | ret = CMD_RET_FAILURE; |
| 428 | goto out_put_mtd; |
| 429 | } |
| 430 | |
| 431 | printf("Erasing 0x%08llx ... 0x%08llx (%d eraseblock(s))\n", |
| 432 | off, off + len - 1, mtd_div_by_eb(len, mtd)); |
| 433 | |
| 434 | erase_op.mtd = mtd; |
| 435 | erase_op.addr = off; |
Patrick Delaunay | b1b147f | 2019-09-20 09:20:12 +0200 | [diff] [blame] | 436 | erase_op.len = mtd->erasesize; |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 437 | |
Patrick Delaunay | b1b147f | 2019-09-20 09:20:12 +0200 | [diff] [blame] | 438 | while (len) { |
Dario Binacchi | d09807a | 2022-10-30 15:14:13 +0100 | [diff] [blame] | 439 | if (!scrub) { |
| 440 | ret = mtd_block_isbad(mtd, erase_op.addr); |
| 441 | if (ret < 0) { |
| 442 | printf("Failed to get bad block at 0x%08llx\n", |
| 443 | erase_op.addr); |
| 444 | ret = CMD_RET_FAILURE; |
| 445 | goto out_put_mtd; |
| 446 | } |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 447 | |
Dario Binacchi | d09807a | 2022-10-30 15:14:13 +0100 | [diff] [blame] | 448 | if (ret > 0) { |
| 449 | printf("Skipping bad block at 0x%08llx\n", |
| 450 | erase_op.addr); |
| 451 | ret = 0; |
| 452 | len -= mtd->erasesize; |
| 453 | erase_op.addr += mtd->erasesize; |
| 454 | continue; |
| 455 | } |
Patrick Delaunay | b1b147f | 2019-09-20 09:20:12 +0200 | [diff] [blame] | 456 | } |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 457 | |
Dario Binacchi | d09807a | 2022-10-30 15:14:13 +0100 | [diff] [blame] | 458 | ret = mtd_erase(mtd, &erase_op); |
| 459 | if (ret && ret != -EIO) |
| 460 | break; |
| 461 | |
Patrick Delaunay | b1b147f | 2019-09-20 09:20:12 +0200 | [diff] [blame] | 462 | len -= mtd->erasesize; |
| 463 | erase_op.addr += mtd->erasesize; |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 464 | } |
| 465 | |
| 466 | if (ret && ret != -EIO) |
| 467 | ret = CMD_RET_FAILURE; |
| 468 | else |
| 469 | ret = CMD_RET_SUCCESS; |
| 470 | |
| 471 | out_put_mtd: |
| 472 | put_mtd_device(mtd); |
| 473 | |
| 474 | return ret; |
| 475 | } |
| 476 | |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 477 | static int do_mtd_bad(struct cmd_tbl *cmdtp, int flag, int argc, |
| 478 | char *const argv[]) |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 479 | { |
| 480 | struct mtd_info *mtd; |
| 481 | loff_t off; |
| 482 | |
| 483 | if (argc < 2) |
| 484 | return CMD_RET_USAGE; |
| 485 | |
| 486 | mtd = get_mtd_by_name(argv[1]); |
| 487 | if (IS_ERR_OR_NULL(mtd)) |
| 488 | return CMD_RET_FAILURE; |
| 489 | |
| 490 | if (!mtd_can_have_bb(mtd)) { |
| 491 | printf("Only NAND-based devices can have bad blocks\n"); |
| 492 | goto out_put_mtd; |
| 493 | } |
| 494 | |
| 495 | printf("MTD device %s bad blocks list:\n", mtd->name); |
| 496 | for (off = 0; off < mtd->size; off += mtd->erasesize) { |
| 497 | if (mtd_block_isbad(mtd, off)) |
| 498 | printf("\t0x%08llx\n", off); |
| 499 | } |
| 500 | |
| 501 | out_put_mtd: |
| 502 | put_mtd_device(mtd); |
| 503 | |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 504 | return CMD_RET_SUCCESS; |
| 505 | } |
| 506 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 507 | #ifdef CONFIG_AUTO_COMPLETE |
Simon Glass | 0914011 | 2020-05-10 11:40:03 -0600 | [diff] [blame] | 508 | static int mtd_name_complete(int argc, char *const argv[], char last_char, |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 509 | int maxv, char *cmdv[]) |
| 510 | { |
| 511 | int len = 0, n_found = 0; |
| 512 | struct mtd_info *mtd; |
| 513 | |
| 514 | argc--; |
| 515 | argv++; |
| 516 | |
| 517 | if (argc > 1 || |
| 518 | (argc == 1 && (last_char == '\0' || isblank(last_char)))) |
| 519 | return 0; |
| 520 | |
| 521 | if (argc) |
| 522 | len = strlen(argv[0]); |
| 523 | |
| 524 | mtd_for_each_device(mtd) { |
| 525 | if (argc && |
| 526 | (len > strlen(mtd->name) || |
| 527 | strncmp(argv[0], mtd->name, len))) |
| 528 | continue; |
| 529 | |
| 530 | if (n_found >= maxv - 2) { |
| 531 | cmdv[n_found++] = "..."; |
| 532 | break; |
| 533 | } |
| 534 | |
| 535 | cmdv[n_found++] = mtd->name; |
| 536 | } |
| 537 | |
| 538 | cmdv[n_found] = NULL; |
| 539 | |
| 540 | return n_found; |
| 541 | } |
| 542 | #endif /* CONFIG_AUTO_COMPLETE */ |
| 543 | |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 544 | U_BOOT_LONGHELP(mtd, |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 545 | "- generic operations on memory technology devices\n\n" |
| 546 | "mtd list\n" |
| 547 | "mtd read[.raw][.oob] <name> <addr> [<off> [<size>]]\n" |
| 548 | "mtd dump[.raw][.oob] <name> [<off> [<size>]]\n" |
| 549 | "mtd write[.raw][.oob][.dontskipff] <name> <addr> [<off> [<size>]]\n" |
| 550 | "mtd erase[.dontskipbad] <name> [<off> [<size>]]\n" |
| 551 | "\n" |
| 552 | "Specific functions:\n" |
| 553 | "mtd bad <name>\n" |
| 554 | "\n" |
| 555 | "With:\n" |
Marek Behún | e41a2bc | 2021-05-26 14:08:26 +0200 | [diff] [blame] | 556 | "\t<name>: NAND partition/chip name (or corresponding DM device name or OF path)\n" |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 557 | "\t<addr>: user address from/to which data will be retrieved/stored\n" |
| 558 | "\t<off>: offset in <name> in bytes (default: start of the part)\n" |
| 559 | "\t\t* must be block-aligned for erase\n" |
| 560 | "\t\t* must be page-aligned otherwise\n" |
| 561 | "\t<size>: length of the operation in bytes (default: the entire device)\n" |
| 562 | "\t\t* must be a multiple of a block for erase\n" |
| 563 | "\t\t* must be a multiple of a page otherwise (special case: default is a page with dump)\n" |
| 564 | "\n" |
Tom Rini | 3616218 | 2023-10-07 15:13:08 -0400 | [diff] [blame] | 565 | "The .dontskipff option forces writing empty pages, don't use it if unsure.\n"); |
Miquel Raynal | 5db66b3 | 2018-09-29 12:58:28 +0200 | [diff] [blame] | 566 | |
Boris Brezillon | 9671243 | 2018-12-03 22:54:22 +0100 | [diff] [blame] | 567 | U_BOOT_CMD_WITH_SUBCMDS(mtd, "MTD utils", mtd_help_text, |
| 568 | U_BOOT_SUBCMD_MKENT(list, 1, 1, do_mtd_list), |
| 569 | U_BOOT_SUBCMD_MKENT_COMPLETE(read, 5, 0, do_mtd_io, |
| 570 | mtd_name_complete), |
| 571 | U_BOOT_SUBCMD_MKENT_COMPLETE(write, 5, 0, do_mtd_io, |
| 572 | mtd_name_complete), |
| 573 | U_BOOT_SUBCMD_MKENT_COMPLETE(dump, 4, 0, do_mtd_io, |
| 574 | mtd_name_complete), |
| 575 | U_BOOT_SUBCMD_MKENT_COMPLETE(erase, 4, 0, do_mtd_erase, |
| 576 | mtd_name_complete), |
| 577 | U_BOOT_SUBCMD_MKENT_COMPLETE(bad, 2, 1, do_mtd_bad, |
| 578 | mtd_name_complete)); |