mtd_readtest.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. /*
  2. * Copyright (C) 2006-2008 Nokia Corporation
  3. *
  4. * This program is free software; you can redistribute it and/or modify it
  5. * under the terms of the GNU General Public License version 2 as published by
  6. * the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but WITHOUT
  9. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  10. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  11. * more details.
  12. *
  13. * You should have received a copy of the GNU General Public License along with
  14. * this program; see the file COPYING. If not, write to the Free Software
  15. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  16. *
  17. * Check MTD device read.
  18. *
  19. * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  20. */
  21. #include <linux/init.h>
  22. #include <linux/module.h>
  23. #include <linux/moduleparam.h>
  24. #include <linux/err.h>
  25. #include <linux/mtd/mtd.h>
  26. #include <linux/slab.h>
  27. #include <linux/sched.h>
  28. #define PRINT_PREF KERN_INFO "mtd_readtest: "
  29. static int dev = -EINVAL;
  30. module_param(dev, int, S_IRUGO);
  31. MODULE_PARM_DESC(dev, "MTD device number to use");
  32. static struct mtd_info *mtd;
  33. static unsigned char *iobuf;
  34. static unsigned char *iobuf1;
  35. static unsigned char *bbt;
  36. static int pgsize;
  37. static int ebcnt;
  38. static int pgcnt;
  39. static int read_eraseblock_by_page(int ebnum)
  40. {
  41. size_t read = 0;
  42. int i, ret, err = 0;
  43. loff_t addr = ebnum * mtd->erasesize;
  44. void *buf = iobuf;
  45. void *oobbuf = iobuf1;
  46. for (i = 0; i < pgcnt; i++) {
  47. memset(buf, 0 , pgcnt);
  48. ret = mtd->read(mtd, addr, pgsize, &read, buf);
  49. if (ret == -EUCLEAN)
  50. ret = 0;
  51. if (ret || read != pgsize) {
  52. printk(PRINT_PREF "error: read failed at %#llx\n",
  53. (long long)addr);
  54. if (!err)
  55. err = ret;
  56. if (!err)
  57. err = -EINVAL;
  58. }
  59. if (mtd->oobsize) {
  60. struct mtd_oob_ops ops;
  61. ops.mode = MTD_OPS_PLACE_OOB;
  62. ops.len = 0;
  63. ops.retlen = 0;
  64. ops.ooblen = mtd->oobsize;
  65. ops.oobretlen = 0;
  66. ops.ooboffs = 0;
  67. ops.datbuf = NULL;
  68. ops.oobbuf = oobbuf;
  69. ret = mtd->read_oob(mtd, addr, &ops);
  70. if ((ret && !mtd_is_bitflip(ret)) ||
  71. ops.oobretlen != mtd->oobsize) {
  72. printk(PRINT_PREF "error: read oob failed at "
  73. "%#llx\n", (long long)addr);
  74. if (!err)
  75. err = ret;
  76. if (!err)
  77. err = -EINVAL;
  78. }
  79. oobbuf += mtd->oobsize;
  80. }
  81. addr += pgsize;
  82. buf += pgsize;
  83. }
  84. return err;
  85. }
  86. static void dump_eraseblock(int ebnum)
  87. {
  88. int i, j, n;
  89. char line[128];
  90. int pg, oob;
  91. printk(PRINT_PREF "dumping eraseblock %d\n", ebnum);
  92. n = mtd->erasesize;
  93. for (i = 0; i < n;) {
  94. char *p = line;
  95. p += sprintf(p, "%05x: ", i);
  96. for (j = 0; j < 32 && i < n; j++, i++)
  97. p += sprintf(p, "%02x", (unsigned int)iobuf[i]);
  98. printk(KERN_CRIT "%s\n", line);
  99. cond_resched();
  100. }
  101. if (!mtd->oobsize)
  102. return;
  103. printk(PRINT_PREF "dumping oob from eraseblock %d\n", ebnum);
  104. n = mtd->oobsize;
  105. for (pg = 0, i = 0; pg < pgcnt; pg++)
  106. for (oob = 0; oob < n;) {
  107. char *p = line;
  108. p += sprintf(p, "%05x: ", i);
  109. for (j = 0; j < 32 && oob < n; j++, oob++, i++)
  110. p += sprintf(p, "%02x",
  111. (unsigned int)iobuf1[i]);
  112. printk(KERN_CRIT "%s\n", line);
  113. cond_resched();
  114. }
  115. }
  116. static int is_block_bad(int ebnum)
  117. {
  118. loff_t addr = ebnum * mtd->erasesize;
  119. int ret;
  120. ret = mtd->block_isbad(mtd, addr);
  121. if (ret)
  122. printk(PRINT_PREF "block %d is bad\n", ebnum);
  123. return ret;
  124. }
  125. static int scan_for_bad_eraseblocks(void)
  126. {
  127. int i, bad = 0;
  128. bbt = kzalloc(ebcnt, GFP_KERNEL);
  129. if (!bbt) {
  130. printk(PRINT_PREF "error: cannot allocate memory\n");
  131. return -ENOMEM;
  132. }
  133. /* NOR flash does not implement block_isbad */
  134. if (mtd->block_isbad == NULL)
  135. return 0;
  136. printk(PRINT_PREF "scanning for bad eraseblocks\n");
  137. for (i = 0; i < ebcnt; ++i) {
  138. bbt[i] = is_block_bad(i) ? 1 : 0;
  139. if (bbt[i])
  140. bad += 1;
  141. cond_resched();
  142. }
  143. printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
  144. return 0;
  145. }
  146. static int __init mtd_readtest_init(void)
  147. {
  148. uint64_t tmp;
  149. int err, i;
  150. printk(KERN_INFO "\n");
  151. printk(KERN_INFO "=================================================\n");
  152. if (dev < 0) {
  153. printk(PRINT_PREF "Please specify a valid mtd-device via module paramter\n");
  154. return -EINVAL;
  155. }
  156. printk(PRINT_PREF "MTD device: %d\n", dev);
  157. mtd = get_mtd_device(NULL, dev);
  158. if (IS_ERR(mtd)) {
  159. err = PTR_ERR(mtd);
  160. printk(PRINT_PREF "error: Cannot get MTD device\n");
  161. return err;
  162. }
  163. if (mtd->writesize == 1) {
  164. printk(PRINT_PREF "not NAND flash, assume page size is 512 "
  165. "bytes.\n");
  166. pgsize = 512;
  167. } else
  168. pgsize = mtd->writesize;
  169. tmp = mtd->size;
  170. do_div(tmp, mtd->erasesize);
  171. ebcnt = tmp;
  172. pgcnt = mtd->erasesize / pgsize;
  173. printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
  174. "page size %u, count of eraseblocks %u, pages per "
  175. "eraseblock %u, OOB size %u\n",
  176. (unsigned long long)mtd->size, mtd->erasesize,
  177. pgsize, ebcnt, pgcnt, mtd->oobsize);
  178. err = -ENOMEM;
  179. iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  180. if (!iobuf) {
  181. printk(PRINT_PREF "error: cannot allocate memory\n");
  182. goto out;
  183. }
  184. iobuf1 = kmalloc(mtd->erasesize, GFP_KERNEL);
  185. if (!iobuf1) {
  186. printk(PRINT_PREF "error: cannot allocate memory\n");
  187. goto out;
  188. }
  189. err = scan_for_bad_eraseblocks();
  190. if (err)
  191. goto out;
  192. /* Read all eraseblocks 1 page at a time */
  193. printk(PRINT_PREF "testing page read\n");
  194. for (i = 0; i < ebcnt; ++i) {
  195. int ret;
  196. if (bbt[i])
  197. continue;
  198. ret = read_eraseblock_by_page(i);
  199. if (ret) {
  200. dump_eraseblock(i);
  201. if (!err)
  202. err = ret;
  203. }
  204. cond_resched();
  205. }
  206. if (err)
  207. printk(PRINT_PREF "finished with errors\n");
  208. else
  209. printk(PRINT_PREF "finished\n");
  210. out:
  211. kfree(iobuf);
  212. kfree(iobuf1);
  213. kfree(bbt);
  214. put_mtd_device(mtd);
  215. if (err)
  216. printk(PRINT_PREF "error %d occurred\n", err);
  217. printk(KERN_INFO "=================================================\n");
  218. return err;
  219. }
  220. module_init(mtd_readtest_init);
  221. static void __exit mtd_readtest_exit(void)
  222. {
  223. return;
  224. }
  225. module_exit(mtd_readtest_exit);
  226. MODULE_DESCRIPTION("Read test module");
  227. MODULE_AUTHOR("Adrian Hunter");
  228. MODULE_LICENSE("GPL");