mtd.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473
  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. #include <command.h>
  11. #include <common.h>
  12. #include <console.h>
  13. #include <malloc.h>
  14. #include <mapmem.h>
  15. #include <mtd.h>
  16. static uint mtd_len_to_pages(struct mtd_info *mtd, u64 len)
  17. {
  18. do_div(len, mtd->writesize);
  19. return len;
  20. }
  21. static bool mtd_is_aligned_with_min_io_size(struct mtd_info *mtd, u64 size)
  22. {
  23. return !do_div(size, mtd->writesize);
  24. }
  25. static bool mtd_is_aligned_with_block_size(struct mtd_info *mtd, u64 size)
  26. {
  27. return !do_div(size, mtd->erasesize);
  28. }
  29. static void mtd_dump_buf(const u8 *buf, uint len, uint offset)
  30. {
  31. int i, j;
  32. for (i = 0; i < len; ) {
  33. printf("0x%08x:\t", offset + i);
  34. for (j = 0; j < 8; j++)
  35. printf("%02x ", buf[i + j]);
  36. printf(" ");
  37. i += 8;
  38. for (j = 0; j < 8; j++)
  39. printf("%02x ", buf[i + j]);
  40. printf("\n");
  41. i += 8;
  42. }
  43. }
  44. static void mtd_dump_device_buf(struct mtd_info *mtd, u64 start_off,
  45. const u8 *buf, u64 len, bool woob)
  46. {
  47. bool has_pages = mtd->type == MTD_NANDFLASH ||
  48. mtd->type == MTD_MLCNANDFLASH;
  49. int npages = mtd_len_to_pages(mtd, len);
  50. uint page;
  51. if (has_pages) {
  52. for (page = 0; page < npages; page++) {
  53. u64 data_off = page * mtd->writesize;
  54. printf("\nDump %d data bytes from 0x%08llx:\n",
  55. mtd->writesize, start_off + data_off);
  56. mtd_dump_buf(&buf[data_off],
  57. mtd->writesize, start_off + data_off);
  58. if (woob) {
  59. u64 oob_off = page * mtd->oobsize;
  60. printf("Dump %d OOB bytes from page at 0x%08llx:\n",
  61. mtd->oobsize, start_off + data_off);
  62. mtd_dump_buf(&buf[len + oob_off],
  63. mtd->oobsize, 0);
  64. }
  65. }
  66. } else {
  67. printf("\nDump %lld data bytes from 0x%llx:\n",
  68. len, start_off);
  69. mtd_dump_buf(buf, len, start_off);
  70. }
  71. }
  72. static void mtd_show_parts(struct mtd_info *mtd, int level)
  73. {
  74. struct mtd_info *part;
  75. int i;
  76. list_for_each_entry(part, &mtd->partitions, node) {
  77. for (i = 0; i < level; i++)
  78. printf("\t");
  79. printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
  80. part->offset, part->offset + part->size, part->name);
  81. mtd_show_parts(part, level + 1);
  82. }
  83. }
  84. static void mtd_show_device(struct mtd_info *mtd)
  85. {
  86. /* Device */
  87. printf("* %s\n", mtd->name);
  88. #if defined(CONFIG_DM)
  89. if (mtd->dev) {
  90. printf(" - device: %s\n", mtd->dev->name);
  91. printf(" - parent: %s\n", mtd->dev->parent->name);
  92. printf(" - driver: %s\n", mtd->dev->driver->name);
  93. }
  94. #endif
  95. /* MTD device information */
  96. printf(" - type: ");
  97. switch (mtd->type) {
  98. case MTD_RAM:
  99. printf("RAM\n");
  100. break;
  101. case MTD_ROM:
  102. printf("ROM\n");
  103. break;
  104. case MTD_NORFLASH:
  105. printf("NOR flash\n");
  106. break;
  107. case MTD_NANDFLASH:
  108. printf("NAND flash\n");
  109. break;
  110. case MTD_DATAFLASH:
  111. printf("Data flash\n");
  112. break;
  113. case MTD_UBIVOLUME:
  114. printf("UBI volume\n");
  115. break;
  116. case MTD_MLCNANDFLASH:
  117. printf("MLC NAND flash\n");
  118. break;
  119. case MTD_ABSENT:
  120. default:
  121. printf("Unknown\n");
  122. break;
  123. }
  124. printf(" - block size: 0x%x bytes\n", mtd->erasesize);
  125. printf(" - min I/O: 0x%x bytes\n", mtd->writesize);
  126. if (mtd->oobsize) {
  127. printf(" - OOB size: %u bytes\n", mtd->oobsize);
  128. printf(" - OOB available: %u bytes\n", mtd->oobavail);
  129. }
  130. if (mtd->ecc_strength) {
  131. printf(" - ECC strength: %u bits\n", mtd->ecc_strength);
  132. printf(" - ECC step size: %u bytes\n", mtd->ecc_step_size);
  133. printf(" - bitflip threshold: %u bits\n",
  134. mtd->bitflip_threshold);
  135. }
  136. printf(" - 0x%012llx-0x%012llx : \"%s\"\n",
  137. mtd->offset, mtd->offset + mtd->size, mtd->name);
  138. /* MTD partitions, if any */
  139. mtd_show_parts(mtd, 1);
  140. }
  141. /* Logic taken from fs/ubifs/recovery.c:is_empty() */
  142. static bool mtd_oob_write_is_empty(struct mtd_oob_ops *op)
  143. {
  144. int i;
  145. for (i = 0; i < op->len; i++)
  146. if (op->datbuf[i] != 0xff)
  147. return false;
  148. for (i = 0; i < op->ooblen; i++)
  149. if (op->oobbuf[i] != 0xff)
  150. return false;
  151. return true;
  152. }
  153. static int do_mtd_list(void)
  154. {
  155. struct mtd_info *mtd;
  156. int dev_nb = 0;
  157. /* Ensure all devices (and their partitions) are probed */
  158. mtd_probe_devices();
  159. printf("List of MTD devices:\n");
  160. mtd_for_each_device(mtd) {
  161. if (!mtd_is_partition(mtd))
  162. mtd_show_device(mtd);
  163. dev_nb++;
  164. }
  165. if (!dev_nb) {
  166. printf("No MTD device found\n");
  167. return CMD_RET_FAILURE;
  168. }
  169. return CMD_RET_SUCCESS;
  170. }
  171. static int mtd_special_write_oob(struct mtd_info *mtd, u64 off,
  172. struct mtd_oob_ops *io_op,
  173. bool write_empty_pages, bool woob)
  174. {
  175. int ret = 0;
  176. /*
  177. * By default, do not write an empty page.
  178. * Skip it by simulating a successful write.
  179. */
  180. if (!write_empty_pages && mtd_oob_write_is_empty(io_op)) {
  181. io_op->retlen = mtd->writesize;
  182. io_op->oobretlen = woob ? mtd->oobsize : 0;
  183. } else {
  184. ret = mtd_write_oob(mtd, off, io_op);
  185. }
  186. return ret;
  187. }
  188. static int do_mtd(cmd_tbl_t *cmdtp, int flag, int argc, char * const argv[])
  189. {
  190. struct mtd_info *mtd;
  191. const char *cmd;
  192. char *mtd_name;
  193. /* All MTD commands need at least two arguments */
  194. if (argc < 2)
  195. return CMD_RET_USAGE;
  196. /* Parse the command name and its optional suffixes */
  197. cmd = argv[1];
  198. /* List the MTD devices if that is what the user wants */
  199. if (strcmp(cmd, "list") == 0)
  200. return do_mtd_list();
  201. /*
  202. * The remaining commands require also at least a device ID.
  203. * Check the selected device is valid. Ensure it is probed.
  204. */
  205. if (argc < 3)
  206. return CMD_RET_USAGE;
  207. mtd_name = argv[2];
  208. mtd_probe_devices();
  209. mtd = get_mtd_device_nm(mtd_name);
  210. if (IS_ERR_OR_NULL(mtd)) {
  211. printf("MTD device %s not found, ret %ld\n",
  212. mtd_name, PTR_ERR(mtd));
  213. return CMD_RET_FAILURE;
  214. }
  215. put_mtd_device(mtd);
  216. argc -= 3;
  217. argv += 3;
  218. /* Do the parsing */
  219. if (!strncmp(cmd, "read", 4) || !strncmp(cmd, "dump", 4) ||
  220. !strncmp(cmd, "write", 5)) {
  221. bool has_pages = mtd->type == MTD_NANDFLASH ||
  222. mtd->type == MTD_MLCNANDFLASH;
  223. bool dump, read, raw, woob, write_empty_pages;
  224. struct mtd_oob_ops io_op = {};
  225. uint user_addr = 0, npages;
  226. u64 start_off, off, len, remaining, default_len;
  227. u32 oob_len;
  228. u8 *buf;
  229. int ret;
  230. dump = !strncmp(cmd, "dump", 4);
  231. read = dump || !strncmp(cmd, "read", 4);
  232. raw = strstr(cmd, ".raw");
  233. woob = strstr(cmd, ".oob");
  234. write_empty_pages = !has_pages || strstr(cmd, ".dontskipff");
  235. if (!dump) {
  236. if (!argc)
  237. return CMD_RET_USAGE;
  238. user_addr = simple_strtoul(argv[0], NULL, 16);
  239. argc--;
  240. argv++;
  241. }
  242. start_off = argc > 0 ? simple_strtoul(argv[0], NULL, 16) : 0;
  243. if (!mtd_is_aligned_with_min_io_size(mtd, start_off)) {
  244. printf("Offset not aligned with a page (0x%x)\n",
  245. mtd->writesize);
  246. return CMD_RET_FAILURE;
  247. }
  248. default_len = dump ? mtd->writesize : mtd->size;
  249. len = argc > 1 ? simple_strtoul(argv[1], NULL, 16) :
  250. default_len;
  251. if (!mtd_is_aligned_with_min_io_size(mtd, len)) {
  252. len = round_up(len, mtd->writesize);
  253. printf("Size not on a page boundary (0x%x), rounding to 0x%llx\n",
  254. mtd->writesize, len);
  255. }
  256. remaining = len;
  257. npages = mtd_len_to_pages(mtd, len);
  258. oob_len = woob ? npages * mtd->oobsize : 0;
  259. if (dump)
  260. buf = kmalloc(len + oob_len, GFP_KERNEL);
  261. else
  262. buf = map_sysmem(user_addr, 0);
  263. if (!buf) {
  264. printf("Could not map/allocate the user buffer\n");
  265. return CMD_RET_FAILURE;
  266. }
  267. if (has_pages)
  268. printf("%s %lld byte(s) (%d page(s)) at offset 0x%08llx%s%s%s\n",
  269. read ? "Reading" : "Writing", len, npages, start_off,
  270. raw ? " [raw]" : "", woob ? " [oob]" : "",
  271. !read && write_empty_pages ? " [dontskipff]" : "");
  272. else
  273. printf("%s %lld byte(s) at offset 0x%08llx\n",
  274. read ? "Reading" : "Writing", len, start_off);
  275. io_op.mode = raw ? MTD_OPS_RAW : MTD_OPS_AUTO_OOB;
  276. io_op.len = has_pages ? mtd->writesize : len;
  277. io_op.ooblen = woob ? mtd->oobsize : 0;
  278. io_op.datbuf = buf;
  279. io_op.oobbuf = woob ? &buf[len] : NULL;
  280. /* Search for the first good block after the given offset */
  281. off = start_off;
  282. while (mtd_block_isbad(mtd, off))
  283. off += mtd->erasesize;
  284. /* Loop over the pages to do the actual read/write */
  285. while (remaining) {
  286. /* Skip the block if it is bad */
  287. if (mtd_is_aligned_with_block_size(mtd, off) &&
  288. mtd_block_isbad(mtd, off)) {
  289. off += mtd->erasesize;
  290. continue;
  291. }
  292. if (read)
  293. ret = mtd_read_oob(mtd, off, &io_op);
  294. else
  295. ret = mtd_special_write_oob(mtd, off, &io_op,
  296. write_empty_pages,
  297. woob);
  298. if (ret) {
  299. printf("Failure while %s at offset 0x%llx\n",
  300. read ? "reading" : "writing", off);
  301. return CMD_RET_FAILURE;
  302. }
  303. off += io_op.retlen;
  304. remaining -= io_op.retlen;
  305. io_op.datbuf += io_op.retlen;
  306. io_op.oobbuf += io_op.oobretlen;
  307. }
  308. if (!ret && dump)
  309. mtd_dump_device_buf(mtd, start_off, buf, len, woob);
  310. if (dump)
  311. kfree(buf);
  312. else
  313. unmap_sysmem(buf);
  314. if (ret) {
  315. printf("%s on %s failed with error %d\n",
  316. read ? "Read" : "Write", mtd->name, ret);
  317. return CMD_RET_FAILURE;
  318. }
  319. } else if (!strcmp(cmd, "erase")) {
  320. bool scrub = strstr(cmd, ".dontskipbad");
  321. struct erase_info erase_op = {};
  322. u64 off, len;
  323. int ret;
  324. off = argc > 0 ? simple_strtoul(argv[0], NULL, 16) : 0;
  325. len = argc > 1 ? simple_strtoul(argv[1], NULL, 16) : mtd->size;
  326. if (!mtd_is_aligned_with_block_size(mtd, off)) {
  327. printf("Offset not aligned with a block (0x%x)\n",
  328. mtd->erasesize);
  329. return CMD_RET_FAILURE;
  330. }
  331. if (!mtd_is_aligned_with_block_size(mtd, len)) {
  332. printf("Size not a multiple of a block (0x%x)\n",
  333. mtd->erasesize);
  334. return CMD_RET_FAILURE;
  335. }
  336. printf("Erasing 0x%08llx ... 0x%08llx (%d eraseblock(s))\n",
  337. off, off + len - 1, mtd_div_by_eb(len, mtd));
  338. erase_op.mtd = mtd;
  339. erase_op.addr = off;
  340. erase_op.len = len;
  341. erase_op.scrub = scrub;
  342. while (erase_op.len) {
  343. ret = mtd_erase(mtd, &erase_op);
  344. /* Abort if its not a bad block error */
  345. if (ret != -EIO)
  346. break;
  347. printf("Skipping bad block at 0x%08llx\n",
  348. erase_op.fail_addr);
  349. /* Skip bad block and continue behind it */
  350. erase_op.len -= erase_op.fail_addr - erase_op.addr;
  351. erase_op.len -= mtd->erasesize;
  352. erase_op.addr = erase_op.fail_addr + mtd->erasesize;
  353. }
  354. if (ret && ret != -EIO)
  355. return CMD_RET_FAILURE;
  356. } else if (!strcmp(cmd, "bad")) {
  357. loff_t off;
  358. if (!mtd_can_have_bb(mtd)) {
  359. printf("Only NAND-based devices can have bad blocks\n");
  360. return CMD_RET_SUCCESS;
  361. }
  362. printf("MTD device %s bad blocks list:\n", mtd->name);
  363. for (off = 0; off < mtd->size; off += mtd->erasesize)
  364. if (mtd_block_isbad(mtd, off))
  365. printf("\t0x%08llx\n", off);
  366. } else {
  367. return CMD_RET_USAGE;
  368. }
  369. return CMD_RET_SUCCESS;
  370. }
  371. static char mtd_help_text[] =
  372. #ifdef CONFIG_SYS_LONGHELP
  373. "- generic operations on memory technology devices\n\n"
  374. "mtd list\n"
  375. "mtd read[.raw][.oob] <name> <addr> [<off> [<size>]]\n"
  376. "mtd dump[.raw][.oob] <name> [<off> [<size>]]\n"
  377. "mtd write[.raw][.oob][.dontskipff] <name> <addr> [<off> [<size>]]\n"
  378. "mtd erase[.dontskipbad] <name> [<off> [<size>]]\n"
  379. "\n"
  380. "Specific functions:\n"
  381. "mtd bad <name>\n"
  382. "\n"
  383. "With:\n"
  384. "\t<name>: NAND partition/chip name\n"
  385. "\t<addr>: user address from/to which data will be retrieved/stored\n"
  386. "\t<off>: offset in <name> in bytes (default: start of the part)\n"
  387. "\t\t* must be block-aligned for erase\n"
  388. "\t\t* must be page-aligned otherwise\n"
  389. "\t<size>: length of the operation in bytes (default: the entire device)\n"
  390. "\t\t* must be a multiple of a block for erase\n"
  391. "\t\t* must be a multiple of a page otherwise (special case: default is a page with dump)\n"
  392. "\n"
  393. "The .dontskipff option forces writing empty pages, don't use it if unsure.\n"
  394. #endif
  395. "";
  396. U_BOOT_CMD(mtd, 10, 1, do_mtd, "MTD utils", mtd_help_text);