readtest.c 5.0 KB

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