readtest.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2006-2008 Nokia Corporation
  4. *
  5. * Check MTD device read.
  6. *
  7. * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  8. */
  9. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  10. #include <linux/init.h>
  11. #include <linux/module.h>
  12. #include <linux/moduleparam.h>
  13. #include <linux/err.h>
  14. #include <linux/mtd/mtd.h>
  15. #include <linux/slab.h>
  16. #include <linux/sched.h>
  17. #include "mtd_test.h"
  18. static int dev = -EINVAL;
  19. module_param(dev, int, S_IRUGO);
  20. MODULE_PARM_DESC(dev, "MTD device number to use");
  21. static struct mtd_info *mtd;
  22. static unsigned char *iobuf;
  23. static unsigned char *iobuf1;
  24. static unsigned char *bbt;
  25. static int pgsize;
  26. static int ebcnt;
  27. static int pgcnt;
  28. static int read_eraseblock_by_page(int ebnum)
  29. {
  30. int i, ret, err = 0;
  31. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  32. void *buf = iobuf;
  33. void *oobbuf = iobuf1;
  34. for (i = 0; i < pgcnt; i++) {
  35. memset(buf, 0 , pgsize);
  36. ret = mtdtest_read(mtd, addr, pgsize, buf);
  37. if (ret) {
  38. if (!err)
  39. err = ret;
  40. }
  41. if (mtd->oobsize) {
  42. struct mtd_oob_ops ops = { };
  43. ops.mode = MTD_OPS_PLACE_OOB;
  44. ops.len = 0;
  45. ops.retlen = 0;
  46. ops.ooblen = mtd->oobsize;
  47. ops.oobretlen = 0;
  48. ops.ooboffs = 0;
  49. ops.datbuf = NULL;
  50. ops.oobbuf = oobbuf;
  51. ret = mtd_read_oob(mtd, addr, &ops);
  52. if ((ret && !mtd_is_bitflip(ret)) ||
  53. ops.oobretlen != mtd->oobsize) {
  54. pr_err("error: read oob failed at "
  55. "%#llx\n", (long long)addr);
  56. if (!err)
  57. err = ret;
  58. if (!err)
  59. err = -EINVAL;
  60. }
  61. oobbuf += mtd->oobsize;
  62. }
  63. addr += pgsize;
  64. buf += pgsize;
  65. }
  66. return err;
  67. }
  68. static void dump_eraseblock(int ebnum)
  69. {
  70. int i, j, n;
  71. char line[128];
  72. int pg, oob;
  73. pr_info("dumping eraseblock %d\n", ebnum);
  74. n = mtd->erasesize;
  75. for (i = 0; i < n;) {
  76. char *p = line;
  77. p += sprintf(p, "%05x: ", i);
  78. for (j = 0; j < 32 && i < n; j++, i++)
  79. p += sprintf(p, "%02x", (unsigned int)iobuf[i]);
  80. printk(KERN_CRIT "%s\n", line);
  81. cond_resched();
  82. }
  83. if (!mtd->oobsize)
  84. return;
  85. pr_info("dumping oob from eraseblock %d\n", ebnum);
  86. n = mtd->oobsize;
  87. for (pg = 0, i = 0; pg < pgcnt; pg++)
  88. for (oob = 0; oob < n;) {
  89. char *p = line;
  90. p += sprintf(p, "%05x: ", i);
  91. for (j = 0; j < 32 && oob < n; j++, oob++, i++)
  92. p += sprintf(p, "%02x",
  93. (unsigned int)iobuf1[i]);
  94. printk(KERN_CRIT "%s\n", line);
  95. cond_resched();
  96. }
  97. }
  98. static int __init mtd_readtest_init(void)
  99. {
  100. uint64_t tmp;
  101. int err, i;
  102. printk(KERN_INFO "\n");
  103. printk(KERN_INFO "=================================================\n");
  104. if (dev < 0) {
  105. pr_info("Please specify a valid mtd-device via module parameter\n");
  106. return -EINVAL;
  107. }
  108. pr_info("MTD device: %d\n", dev);
  109. mtd = get_mtd_device(NULL, dev);
  110. if (IS_ERR(mtd)) {
  111. err = PTR_ERR(mtd);
  112. pr_err("error: Cannot get MTD device\n");
  113. return err;
  114. }
  115. if (mtd->writesize == 1) {
  116. pr_info("not NAND flash, assume page size is 512 "
  117. "bytes.\n");
  118. pgsize = 512;
  119. } else
  120. pgsize = mtd->writesize;
  121. tmp = mtd->size;
  122. do_div(tmp, mtd->erasesize);
  123. ebcnt = tmp;
  124. pgcnt = mtd->erasesize / pgsize;
  125. pr_info("MTD device size %llu, eraseblock size %u, "
  126. "page size %u, count of eraseblocks %u, pages per "
  127. "eraseblock %u, OOB size %u\n",
  128. (unsigned long long)mtd->size, mtd->erasesize,
  129. pgsize, ebcnt, pgcnt, mtd->oobsize);
  130. err = -ENOMEM;
  131. iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  132. if (!iobuf)
  133. goto out;
  134. iobuf1 = kmalloc(mtd->erasesize, GFP_KERNEL);
  135. if (!iobuf1)
  136. goto out;
  137. bbt = kzalloc(ebcnt, GFP_KERNEL);
  138. if (!bbt)
  139. goto out;
  140. err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
  141. if (err)
  142. goto out;
  143. /* Read all eraseblocks 1 page at a time */
  144. pr_info("testing page read\n");
  145. for (i = 0; i < ebcnt; ++i) {
  146. int ret;
  147. if (bbt[i])
  148. continue;
  149. ret = read_eraseblock_by_page(i);
  150. if (ret) {
  151. dump_eraseblock(i);
  152. if (!err)
  153. err = ret;
  154. }
  155. ret = mtdtest_relax();
  156. if (ret) {
  157. err = ret;
  158. goto out;
  159. }
  160. }
  161. if (err)
  162. pr_info("finished with errors\n");
  163. else
  164. pr_info("finished\n");
  165. out:
  166. kfree(iobuf);
  167. kfree(iobuf1);
  168. kfree(bbt);
  169. put_mtd_device(mtd);
  170. if (err)
  171. pr_info("error %d occurred\n", err);
  172. printk(KERN_INFO "=================================================\n");
  173. return err;
  174. }
  175. module_init(mtd_readtest_init);
  176. static void __exit mtd_readtest_exit(void)
  177. {
  178. return;
  179. }
  180. module_exit(mtd_readtest_exit);
  181. MODULE_DESCRIPTION("Read test module");
  182. MODULE_AUTHOR("Adrian Hunter");
  183. MODULE_LICENSE("GPL");