mtd_readtest.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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;
  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_OOB_PLACE;
  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 || ops.oobretlen != mtd->oobsize) {
  71. printk(PRINT_PREF "error: read oob failed at "
  72. "%#llx\n", (long long)addr);
  73. if (!err)
  74. err = ret;
  75. if (!err)
  76. err = -EINVAL;
  77. }
  78. oobbuf += mtd->oobsize;
  79. }
  80. addr += pgsize;
  81. buf += pgsize;
  82. }
  83. return err;
  84. }
  85. static void dump_eraseblock(int ebnum)
  86. {
  87. int i, j, n;
  88. char line[128];
  89. int pg, oob;
  90. printk(PRINT_PREF "dumping eraseblock %d\n", ebnum);
  91. n = mtd->erasesize;
  92. for (i = 0; i < n;) {
  93. char *p = line;
  94. p += sprintf(p, "%05x: ", i);
  95. for (j = 0; j < 32 && i < n; j++, i++)
  96. p += sprintf(p, "%02x", (unsigned int)iobuf[i]);
  97. printk(KERN_CRIT "%s\n", line);
  98. cond_resched();
  99. }
  100. if (!mtd->oobsize)
  101. return;
  102. printk(PRINT_PREF "dumping oob from eraseblock %d\n", ebnum);
  103. n = mtd->oobsize;
  104. for (pg = 0, i = 0; pg < pgcnt; pg++)
  105. for (oob = 0; oob < n;) {
  106. char *p = line;
  107. p += sprintf(p, "%05x: ", i);
  108. for (j = 0; j < 32 && oob < n; j++, oob++, i++)
  109. p += sprintf(p, "%02x",
  110. (unsigned int)iobuf1[i]);
  111. printk(KERN_CRIT "%s\n", line);
  112. cond_resched();
  113. }
  114. }
  115. static int is_block_bad(int ebnum)
  116. {
  117. loff_t addr = ebnum * mtd->erasesize;
  118. int ret;
  119. ret = mtd->block_isbad(mtd, addr);
  120. if (ret)
  121. printk(PRINT_PREF "block %d is bad\n", ebnum);
  122. return ret;
  123. }
  124. static int scan_for_bad_eraseblocks(void)
  125. {
  126. int i, bad = 0;
  127. bbt = kmalloc(ebcnt, GFP_KERNEL);
  128. if (!bbt) {
  129. printk(PRINT_PREF "error: cannot allocate memory\n");
  130. return -ENOMEM;
  131. }
  132. memset(bbt, 0 , ebcnt);
  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. printk(PRINT_PREF "MTD device: %d\n", dev);
  153. mtd = get_mtd_device(NULL, dev);
  154. if (IS_ERR(mtd)) {
  155. err = PTR_ERR(mtd);
  156. printk(PRINT_PREF "error: Cannot get MTD device\n");
  157. return err;
  158. }
  159. if (mtd->writesize == 1) {
  160. printk(PRINT_PREF "not NAND flash, assume page size is 512 "
  161. "bytes.\n");
  162. pgsize = 512;
  163. } else
  164. pgsize = mtd->writesize;
  165. tmp = mtd->size;
  166. do_div(tmp, mtd->erasesize);
  167. ebcnt = tmp;
  168. pgcnt = mtd->erasesize / pgsize;
  169. printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
  170. "page size %u, count of eraseblocks %u, pages per "
  171. "eraseblock %u, OOB size %u\n",
  172. (unsigned long long)mtd->size, mtd->erasesize,
  173. pgsize, ebcnt, pgcnt, mtd->oobsize);
  174. err = -ENOMEM;
  175. iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  176. if (!iobuf) {
  177. printk(PRINT_PREF "error: cannot allocate memory\n");
  178. goto out;
  179. }
  180. iobuf1 = kmalloc(mtd->erasesize, GFP_KERNEL);
  181. if (!iobuf1) {
  182. printk(PRINT_PREF "error: cannot allocate memory\n");
  183. goto out;
  184. }
  185. err = scan_for_bad_eraseblocks();
  186. if (err)
  187. goto out;
  188. /* Read all eraseblocks 1 page at a time */
  189. printk(PRINT_PREF "testing page read\n");
  190. for (i = 0; i < ebcnt; ++i) {
  191. int ret;
  192. if (bbt[i])
  193. continue;
  194. ret = read_eraseblock_by_page(i);
  195. if (ret) {
  196. dump_eraseblock(i);
  197. if (!err)
  198. err = ret;
  199. }
  200. cond_resched();
  201. }
  202. if (err)
  203. printk(PRINT_PREF "finished with errors\n");
  204. else
  205. printk(PRINT_PREF "finished\n");
  206. out:
  207. kfree(iobuf);
  208. kfree(iobuf1);
  209. kfree(bbt);
  210. put_mtd_device(mtd);
  211. if (err)
  212. printk(PRINT_PREF "error %d occurred\n", err);
  213. printk(KERN_INFO "=================================================\n");
  214. return err;
  215. }
  216. module_init(mtd_readtest_init);
  217. static void __exit mtd_readtest_exit(void)
  218. {
  219. return;
  220. }
  221. module_exit(mtd_readtest_exit);
  222. MODULE_DESCRIPTION("Read test module");
  223. MODULE_AUTHOR("Adrian Hunter");
  224. MODULE_LICENSE("GPL");