mtd_readtest.c 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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/sched.h>
  27. #define PRINT_PREF KERN_INFO "mtd_readtest: "
  28. static int dev;
  29. module_param(dev, int, S_IRUGO);
  30. MODULE_PARM_DESC(dev, "MTD device number to use");
  31. static struct mtd_info *mtd;
  32. static unsigned char *iobuf;
  33. static unsigned char *iobuf1;
  34. static unsigned char *bbt;
  35. static int pgsize;
  36. static int ebcnt;
  37. static int pgcnt;
  38. static int read_eraseblock_by_page(int ebnum)
  39. {
  40. size_t read = 0;
  41. int i, ret, err = 0;
  42. loff_t addr = ebnum * mtd->erasesize;
  43. void *buf = iobuf;
  44. void *oobbuf = iobuf1;
  45. for (i = 0; i < pgcnt; i++) {
  46. memset(buf, 0 , pgcnt);
  47. ret = mtd->read(mtd, addr, pgsize, &read, buf);
  48. if (ret == -EUCLEAN)
  49. ret = 0;
  50. if (ret || read != pgsize) {
  51. printk(PRINT_PREF "error: read failed at %#llx\n",
  52. (long long)addr);
  53. if (!err)
  54. err = ret;
  55. if (!err)
  56. err = -EINVAL;
  57. }
  58. if (mtd->oobsize) {
  59. struct mtd_oob_ops ops;
  60. ops.mode = MTD_OOB_PLACE;
  61. ops.len = 0;
  62. ops.retlen = 0;
  63. ops.ooblen = mtd->oobsize;
  64. ops.oobretlen = 0;
  65. ops.ooboffs = 0;
  66. ops.datbuf = NULL;
  67. ops.oobbuf = oobbuf;
  68. ret = mtd->read_oob(mtd, addr, &ops);
  69. if (ret || ops.oobretlen != mtd->oobsize) {
  70. printk(PRINT_PREF "error: read oob failed at "
  71. "%#llx\n", (long long)addr);
  72. if (!err)
  73. err = ret;
  74. if (!err)
  75. err = -EINVAL;
  76. }
  77. oobbuf += mtd->oobsize;
  78. }
  79. addr += pgsize;
  80. buf += pgsize;
  81. }
  82. return err;
  83. }
  84. static void dump_eraseblock(int ebnum)
  85. {
  86. int i, j, n;
  87. char line[128];
  88. int pg, oob;
  89. printk(PRINT_PREF "dumping eraseblock %d\n", ebnum);
  90. n = mtd->erasesize;
  91. for (i = 0; i < n;) {
  92. char *p = line;
  93. p += sprintf(p, "%05x: ", i);
  94. for (j = 0; j < 32 && i < n; j++, i++)
  95. p += sprintf(p, "%02x", (unsigned int)iobuf[i]);
  96. printk(KERN_CRIT "%s\n", line);
  97. cond_resched();
  98. }
  99. if (!mtd->oobsize)
  100. return;
  101. printk(PRINT_PREF "dumping oob from eraseblock %d\n", ebnum);
  102. n = mtd->oobsize;
  103. for (pg = 0, i = 0; pg < pgcnt; pg++)
  104. for (oob = 0; oob < n;) {
  105. char *p = line;
  106. p += sprintf(p, "%05x: ", i);
  107. for (j = 0; j < 32 && oob < n; j++, oob++, i++)
  108. p += sprintf(p, "%02x",
  109. (unsigned int)iobuf1[i]);
  110. printk(KERN_CRIT "%s\n", line);
  111. cond_resched();
  112. }
  113. }
  114. static int is_block_bad(int ebnum)
  115. {
  116. loff_t addr = ebnum * mtd->erasesize;
  117. int ret;
  118. ret = mtd->block_isbad(mtd, addr);
  119. if (ret)
  120. printk(PRINT_PREF "block %d is bad\n", ebnum);
  121. return ret;
  122. }
  123. static int scan_for_bad_eraseblocks(void)
  124. {
  125. int i, bad = 0;
  126. bbt = kmalloc(ebcnt, GFP_KERNEL);
  127. if (!bbt) {
  128. printk(PRINT_PREF "error: cannot allocate memory\n");
  129. return -ENOMEM;
  130. }
  131. memset(bbt, 0 , ebcnt);
  132. printk(PRINT_PREF "scanning for bad eraseblocks\n");
  133. for (i = 0; i < ebcnt; ++i) {
  134. bbt[i] = is_block_bad(i) ? 1 : 0;
  135. if (bbt[i])
  136. bad += 1;
  137. cond_resched();
  138. }
  139. printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
  140. return 0;
  141. }
  142. static int __init mtd_readtest_init(void)
  143. {
  144. uint64_t tmp;
  145. int err, i;
  146. printk(KERN_INFO "\n");
  147. printk(KERN_INFO "=================================================\n");
  148. printk(PRINT_PREF "MTD device: %d\n", dev);
  149. mtd = get_mtd_device(NULL, dev);
  150. if (IS_ERR(mtd)) {
  151. err = PTR_ERR(mtd);
  152. printk(PRINT_PREF "error: Cannot get MTD device\n");
  153. return err;
  154. }
  155. if (mtd->writesize == 1) {
  156. printk(PRINT_PREF "not NAND flash, assume page size is 512 "
  157. "bytes.\n");
  158. pgsize = 512;
  159. } else
  160. pgsize = mtd->writesize;
  161. tmp = mtd->size;
  162. do_div(tmp, mtd->erasesize);
  163. ebcnt = tmp;
  164. pgcnt = mtd->erasesize / mtd->writesize;
  165. printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
  166. "page size %u, count of eraseblocks %u, pages per "
  167. "eraseblock %u, OOB size %u\n",
  168. (unsigned long long)mtd->size, mtd->erasesize,
  169. pgsize, ebcnt, pgcnt, mtd->oobsize);
  170. err = -ENOMEM;
  171. iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  172. if (!iobuf) {
  173. printk(PRINT_PREF "error: cannot allocate memory\n");
  174. goto out;
  175. }
  176. iobuf1 = kmalloc(mtd->erasesize, GFP_KERNEL);
  177. if (!iobuf1) {
  178. printk(PRINT_PREF "error: cannot allocate memory\n");
  179. goto out;
  180. }
  181. err = scan_for_bad_eraseblocks();
  182. if (err)
  183. goto out;
  184. /* Read all eraseblocks 1 page at a time */
  185. printk(PRINT_PREF "testing page read\n");
  186. for (i = 0; i < ebcnt; ++i) {
  187. int ret;
  188. if (bbt[i])
  189. continue;
  190. ret = read_eraseblock_by_page(i);
  191. if (ret) {
  192. dump_eraseblock(i);
  193. if (!err)
  194. err = ret;
  195. }
  196. cond_resched();
  197. }
  198. if (err)
  199. printk(PRINT_PREF "finished with errors\n");
  200. else
  201. printk(PRINT_PREF "finished\n");
  202. out:
  203. kfree(iobuf);
  204. kfree(iobuf1);
  205. kfree(bbt);
  206. put_mtd_device(mtd);
  207. if (err)
  208. printk(PRINT_PREF "error %d occurred\n", err);
  209. printk(KERN_INFO "=================================================\n");
  210. return err;
  211. }
  212. module_init(mtd_readtest_init);
  213. static void __exit mtd_readtest_exit(void)
  214. {
  215. return;
  216. }
  217. module_exit(mtd_readtest_exit);
  218. MODULE_DESCRIPTION("Read test module");
  219. MODULE_AUTHOR("Adrian Hunter");
  220. MODULE_LICENSE("GPL");