readtest.c 5.0 KB

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