mtd_readtest.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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 = kzalloc(ebcnt, GFP_KERNEL);
  128. if (!bbt) {
  129. printk(PRINT_PREF "error: cannot allocate memory\n");
  130. return -ENOMEM;
  131. }
  132. /* NOR flash does not implement block_isbad */
  133. if (mtd->block_isbad == NULL)
  134. return 0;
  135. printk(PRINT_PREF "scanning for bad eraseblocks\n");
  136. for (i = 0; i < ebcnt; ++i) {
  137. bbt[i] = is_block_bad(i) ? 1 : 0;
  138. if (bbt[i])
  139. bad += 1;
  140. cond_resched();
  141. }
  142. printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
  143. return 0;
  144. }
  145. static int __init mtd_readtest_init(void)
  146. {
  147. uint64_t tmp;
  148. int err, i;
  149. printk(KERN_INFO "\n");
  150. printk(KERN_INFO "=================================================\n");
  151. printk(PRINT_PREF "MTD device: %d\n", dev);
  152. mtd = get_mtd_device(NULL, dev);
  153. if (IS_ERR(mtd)) {
  154. err = PTR_ERR(mtd);
  155. printk(PRINT_PREF "error: Cannot get MTD device\n");
  156. return err;
  157. }
  158. if (mtd->writesize == 1) {
  159. printk(PRINT_PREF "not NAND flash, assume page size is 512 "
  160. "bytes.\n");
  161. pgsize = 512;
  162. } else
  163. pgsize = mtd->writesize;
  164. tmp = mtd->size;
  165. do_div(tmp, mtd->erasesize);
  166. ebcnt = tmp;
  167. pgcnt = mtd->erasesize / pgsize;
  168. printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
  169. "page size %u, count of eraseblocks %u, pages per "
  170. "eraseblock %u, OOB size %u\n",
  171. (unsigned long long)mtd->size, mtd->erasesize,
  172. pgsize, ebcnt, pgcnt, mtd->oobsize);
  173. err = -ENOMEM;
  174. iobuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  175. if (!iobuf) {
  176. printk(PRINT_PREF "error: cannot allocate memory\n");
  177. goto out;
  178. }
  179. iobuf1 = kmalloc(mtd->erasesize, GFP_KERNEL);
  180. if (!iobuf1) {
  181. printk(PRINT_PREF "error: cannot allocate memory\n");
  182. goto out;
  183. }
  184. err = scan_for_bad_eraseblocks();
  185. if (err)
  186. goto out;
  187. /* Read all eraseblocks 1 page at a time */
  188. printk(PRINT_PREF "testing page read\n");
  189. for (i = 0; i < ebcnt; ++i) {
  190. int ret;
  191. if (bbt[i])
  192. continue;
  193. ret = read_eraseblock_by_page(i);
  194. if (ret) {
  195. dump_eraseblock(i);
  196. if (!err)
  197. err = ret;
  198. }
  199. cond_resched();
  200. }
  201. if (err)
  202. printk(PRINT_PREF "finished with errors\n");
  203. else
  204. printk(PRINT_PREF "finished\n");
  205. out:
  206. kfree(iobuf);
  207. kfree(iobuf1);
  208. kfree(bbt);
  209. put_mtd_device(mtd);
  210. if (err)
  211. printk(PRINT_PREF "error %d occurred\n", err);
  212. printk(KERN_INFO "=================================================\n");
  213. return err;
  214. }
  215. module_init(mtd_readtest_init);
  216. static void __exit mtd_readtest_exit(void)
  217. {
  218. return;
  219. }
  220. module_exit(mtd_readtest_exit);
  221. MODULE_DESCRIPTION("Read test module");
  222. MODULE_AUTHOR("Adrian Hunter");
  223. MODULE_LICENSE("GPL");