pagetest.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471
  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. * Test page read and write on MTD device.
  18. *
  19. * Author: Adrian Hunter <ext-adrian.hunter@nokia.com>
  20. */
  21. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  22. #include <asm/div64.h>
  23. #include <linux/init.h>
  24. #include <linux/module.h>
  25. #include <linux/moduleparam.h>
  26. #include <linux/err.h>
  27. #include <linux/mtd/mtd.h>
  28. #include <linux/slab.h>
  29. #include <linux/sched.h>
  30. #include <linux/random.h>
  31. #include "mtd_test.h"
  32. static int dev = -EINVAL;
  33. module_param(dev, int, S_IRUGO);
  34. MODULE_PARM_DESC(dev, "MTD device number to use");
  35. static struct mtd_info *mtd;
  36. static unsigned char *twopages;
  37. static unsigned char *writebuf;
  38. static unsigned char *boundary;
  39. static unsigned char *bbt;
  40. static int pgsize;
  41. static int bufsize;
  42. static int ebcnt;
  43. static int pgcnt;
  44. static int errcnt;
  45. static struct rnd_state rnd_state;
  46. static int write_eraseblock(int ebnum)
  47. {
  48. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  49. prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize);
  50. cond_resched();
  51. return mtdtest_write(mtd, addr, mtd->erasesize, writebuf);
  52. }
  53. static int verify_eraseblock(int ebnum)
  54. {
  55. uint32_t j;
  56. int err = 0, i;
  57. loff_t addr0, addrn;
  58. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  59. addr0 = 0;
  60. for (i = 0; i < ebcnt && bbt[i]; ++i)
  61. addr0 += mtd->erasesize;
  62. addrn = mtd->size;
  63. for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
  64. addrn -= mtd->erasesize;
  65. prandom_bytes_state(&rnd_state, writebuf, mtd->erasesize);
  66. for (j = 0; j < pgcnt - 1; ++j, addr += pgsize) {
  67. /* Do a read to set the internal dataRAMs to different data */
  68. err = mtdtest_read(mtd, addr0, bufsize, twopages);
  69. if (err)
  70. return err;
  71. err = mtdtest_read(mtd, addrn - bufsize, bufsize, twopages);
  72. if (err)
  73. return err;
  74. memset(twopages, 0, bufsize);
  75. err = mtdtest_read(mtd, addr, bufsize, twopages);
  76. if (err)
  77. break;
  78. if (memcmp(twopages, writebuf + (j * pgsize), bufsize)) {
  79. pr_err("error: verify failed at %#llx\n",
  80. (long long)addr);
  81. errcnt += 1;
  82. }
  83. }
  84. /* Check boundary between eraseblocks */
  85. if (addr <= addrn - pgsize - pgsize && !bbt[ebnum + 1]) {
  86. struct rnd_state old_state = rnd_state;
  87. /* Do a read to set the internal dataRAMs to different data */
  88. err = mtdtest_read(mtd, addr0, bufsize, twopages);
  89. if (err)
  90. return err;
  91. err = mtdtest_read(mtd, addrn - bufsize, bufsize, twopages);
  92. if (err)
  93. return err;
  94. memset(twopages, 0, bufsize);
  95. err = mtdtest_read(mtd, addr, bufsize, twopages);
  96. if (err)
  97. return err;
  98. memcpy(boundary, writebuf + mtd->erasesize - pgsize, pgsize);
  99. prandom_bytes_state(&rnd_state, boundary + pgsize, pgsize);
  100. if (memcmp(twopages, boundary, bufsize)) {
  101. pr_err("error: verify failed at %#llx\n",
  102. (long long)addr);
  103. errcnt += 1;
  104. }
  105. rnd_state = old_state;
  106. }
  107. return err;
  108. }
  109. static int crosstest(void)
  110. {
  111. int err = 0, i;
  112. loff_t addr, addr0, addrn;
  113. unsigned char *pp1, *pp2, *pp3, *pp4;
  114. pr_info("crosstest\n");
  115. pp1 = kmalloc(pgsize * 4, GFP_KERNEL);
  116. if (!pp1)
  117. return -ENOMEM;
  118. pp2 = pp1 + pgsize;
  119. pp3 = pp2 + pgsize;
  120. pp4 = pp3 + pgsize;
  121. memset(pp1, 0, pgsize * 4);
  122. addr0 = 0;
  123. for (i = 0; i < ebcnt && bbt[i]; ++i)
  124. addr0 += mtd->erasesize;
  125. addrn = mtd->size;
  126. for (i = 0; i < ebcnt && bbt[ebcnt - i - 1]; ++i)
  127. addrn -= mtd->erasesize;
  128. /* Read 2nd-to-last page to pp1 */
  129. addr = addrn - pgsize - pgsize;
  130. err = mtdtest_read(mtd, addr, pgsize, pp1);
  131. if (err) {
  132. kfree(pp1);
  133. return err;
  134. }
  135. /* Read 3rd-to-last page to pp1 */
  136. addr = addrn - pgsize - pgsize - pgsize;
  137. err = mtdtest_read(mtd, addr, pgsize, pp1);
  138. if (err) {
  139. kfree(pp1);
  140. return err;
  141. }
  142. /* Read first page to pp2 */
  143. addr = addr0;
  144. pr_info("reading page at %#llx\n", (long long)addr);
  145. err = mtdtest_read(mtd, addr, pgsize, pp2);
  146. if (err) {
  147. kfree(pp1);
  148. return err;
  149. }
  150. /* Read last page to pp3 */
  151. addr = addrn - pgsize;
  152. pr_info("reading page at %#llx\n", (long long)addr);
  153. err = mtdtest_read(mtd, addr, pgsize, pp3);
  154. if (err) {
  155. kfree(pp1);
  156. return err;
  157. }
  158. /* Read first page again to pp4 */
  159. addr = addr0;
  160. pr_info("reading page at %#llx\n", (long long)addr);
  161. err = mtdtest_read(mtd, addr, pgsize, pp4);
  162. if (err) {
  163. kfree(pp1);
  164. return err;
  165. }
  166. /* pp2 and pp4 should be the same */
  167. pr_info("verifying pages read at %#llx match\n",
  168. (long long)addr0);
  169. if (memcmp(pp2, pp4, pgsize)) {
  170. pr_err("verify failed!\n");
  171. errcnt += 1;
  172. } else if (!err)
  173. pr_info("crosstest ok\n");
  174. kfree(pp1);
  175. return err;
  176. }
  177. static int erasecrosstest(void)
  178. {
  179. int err = 0, i, ebnum, ebnum2;
  180. loff_t addr0;
  181. char *readbuf = twopages;
  182. pr_info("erasecrosstest\n");
  183. ebnum = 0;
  184. addr0 = 0;
  185. for (i = 0; i < ebcnt && bbt[i]; ++i) {
  186. addr0 += mtd->erasesize;
  187. ebnum += 1;
  188. }
  189. ebnum2 = ebcnt - 1;
  190. while (ebnum2 && bbt[ebnum2])
  191. ebnum2 -= 1;
  192. pr_info("erasing block %d\n", ebnum);
  193. err = mtdtest_erase_eraseblock(mtd, ebnum);
  194. if (err)
  195. return err;
  196. pr_info("writing 1st page of block %d\n", ebnum);
  197. prandom_bytes_state(&rnd_state, writebuf, pgsize);
  198. strcpy(writebuf, "There is no data like this!");
  199. err = mtdtest_write(mtd, addr0, pgsize, writebuf);
  200. if (err)
  201. return err;
  202. pr_info("reading 1st page of block %d\n", ebnum);
  203. memset(readbuf, 0, pgsize);
  204. err = mtdtest_read(mtd, addr0, pgsize, readbuf);
  205. if (err)
  206. return err;
  207. pr_info("verifying 1st page of block %d\n", ebnum);
  208. if (memcmp(writebuf, readbuf, pgsize)) {
  209. pr_err("verify failed!\n");
  210. errcnt += 1;
  211. return -1;
  212. }
  213. pr_info("erasing block %d\n", ebnum);
  214. err = mtdtest_erase_eraseblock(mtd, ebnum);
  215. if (err)
  216. return err;
  217. pr_info("writing 1st page of block %d\n", ebnum);
  218. prandom_bytes_state(&rnd_state, writebuf, pgsize);
  219. strcpy(writebuf, "There is no data like this!");
  220. err = mtdtest_write(mtd, addr0, pgsize, writebuf);
  221. if (err)
  222. return err;
  223. pr_info("erasing block %d\n", ebnum2);
  224. err = mtdtest_erase_eraseblock(mtd, ebnum2);
  225. if (err)
  226. return err;
  227. pr_info("reading 1st page of block %d\n", ebnum);
  228. memset(readbuf, 0, pgsize);
  229. err = mtdtest_read(mtd, addr0, pgsize, readbuf);
  230. if (err)
  231. return err;
  232. pr_info("verifying 1st page of block %d\n", ebnum);
  233. if (memcmp(writebuf, readbuf, pgsize)) {
  234. pr_err("verify failed!\n");
  235. errcnt += 1;
  236. return -1;
  237. }
  238. if (!err)
  239. pr_info("erasecrosstest ok\n");
  240. return err;
  241. }
  242. static int erasetest(void)
  243. {
  244. int err = 0, i, ebnum, ok = 1;
  245. loff_t addr0;
  246. pr_info("erasetest\n");
  247. ebnum = 0;
  248. addr0 = 0;
  249. for (i = 0; i < ebcnt && bbt[i]; ++i) {
  250. addr0 += mtd->erasesize;
  251. ebnum += 1;
  252. }
  253. pr_info("erasing block %d\n", ebnum);
  254. err = mtdtest_erase_eraseblock(mtd, ebnum);
  255. if (err)
  256. return err;
  257. pr_info("writing 1st page of block %d\n", ebnum);
  258. prandom_bytes_state(&rnd_state, writebuf, pgsize);
  259. err = mtdtest_write(mtd, addr0, pgsize, writebuf);
  260. if (err)
  261. return err;
  262. pr_info("erasing block %d\n", ebnum);
  263. err = mtdtest_erase_eraseblock(mtd, ebnum);
  264. if (err)
  265. return err;
  266. pr_info("reading 1st page of block %d\n", ebnum);
  267. err = mtdtest_read(mtd, addr0, pgsize, twopages);
  268. if (err)
  269. return err;
  270. pr_info("verifying 1st page of block %d is all 0xff\n",
  271. ebnum);
  272. for (i = 0; i < pgsize; ++i)
  273. if (twopages[i] != 0xff) {
  274. pr_err("verifying all 0xff failed at %d\n",
  275. i);
  276. errcnt += 1;
  277. ok = 0;
  278. break;
  279. }
  280. if (ok && !err)
  281. pr_info("erasetest ok\n");
  282. return err;
  283. }
  284. static int __init mtd_pagetest_init(void)
  285. {
  286. int err = 0;
  287. uint64_t tmp;
  288. uint32_t i;
  289. printk(KERN_INFO "\n");
  290. printk(KERN_INFO "=================================================\n");
  291. if (dev < 0) {
  292. pr_info("Please specify a valid mtd-device via module parameter\n");
  293. pr_crit("CAREFUL: This test wipes all data on the specified MTD device!\n");
  294. return -EINVAL;
  295. }
  296. pr_info("MTD device: %d\n", dev);
  297. mtd = get_mtd_device(NULL, dev);
  298. if (IS_ERR(mtd)) {
  299. err = PTR_ERR(mtd);
  300. pr_err("error: cannot get MTD device\n");
  301. return err;
  302. }
  303. if (!mtd_type_is_nand(mtd)) {
  304. pr_info("this test requires NAND flash\n");
  305. goto out;
  306. }
  307. tmp = mtd->size;
  308. do_div(tmp, mtd->erasesize);
  309. ebcnt = tmp;
  310. pgcnt = mtd->erasesize / mtd->writesize;
  311. pgsize = mtd->writesize;
  312. pr_info("MTD device size %llu, eraseblock size %u, "
  313. "page size %u, count of eraseblocks %u, pages per "
  314. "eraseblock %u, OOB size %u\n",
  315. (unsigned long long)mtd->size, mtd->erasesize,
  316. pgsize, ebcnt, pgcnt, mtd->oobsize);
  317. err = -ENOMEM;
  318. bufsize = pgsize * 2;
  319. writebuf = kmalloc(mtd->erasesize, GFP_KERNEL);
  320. if (!writebuf)
  321. goto out;
  322. twopages = kmalloc(bufsize, GFP_KERNEL);
  323. if (!twopages)
  324. goto out;
  325. boundary = kmalloc(bufsize, GFP_KERNEL);
  326. if (!boundary)
  327. goto out;
  328. bbt = kzalloc(ebcnt, GFP_KERNEL);
  329. if (!bbt)
  330. goto out;
  331. err = mtdtest_scan_for_bad_eraseblocks(mtd, bbt, 0, ebcnt);
  332. if (err)
  333. goto out;
  334. /* Erase all eraseblocks */
  335. pr_info("erasing whole device\n");
  336. err = mtdtest_erase_good_eraseblocks(mtd, bbt, 0, ebcnt);
  337. if (err)
  338. goto out;
  339. pr_info("erased %u eraseblocks\n", ebcnt);
  340. /* Write all eraseblocks */
  341. prandom_seed_state(&rnd_state, 1);
  342. pr_info("writing whole device\n");
  343. for (i = 0; i < ebcnt; ++i) {
  344. if (bbt[i])
  345. continue;
  346. err = write_eraseblock(i);
  347. if (err)
  348. goto out;
  349. if (i % 256 == 0)
  350. pr_info("written up to eraseblock %u\n", i);
  351. err = mtdtest_relax();
  352. if (err)
  353. goto out;
  354. }
  355. pr_info("written %u eraseblocks\n", i);
  356. /* Check all eraseblocks */
  357. prandom_seed_state(&rnd_state, 1);
  358. pr_info("verifying all eraseblocks\n");
  359. for (i = 0; i < ebcnt; ++i) {
  360. if (bbt[i])
  361. continue;
  362. err = verify_eraseblock(i);
  363. if (err)
  364. goto out;
  365. if (i % 256 == 0)
  366. pr_info("verified up to eraseblock %u\n", i);
  367. err = mtdtest_relax();
  368. if (err)
  369. goto out;
  370. }
  371. pr_info("verified %u eraseblocks\n", i);
  372. err = crosstest();
  373. if (err)
  374. goto out;
  375. err = erasecrosstest();
  376. if (err)
  377. goto out;
  378. err = erasetest();
  379. if (err)
  380. goto out;
  381. pr_info("finished with %d errors\n", errcnt);
  382. out:
  383. kfree(bbt);
  384. kfree(boundary);
  385. kfree(twopages);
  386. kfree(writebuf);
  387. put_mtd_device(mtd);
  388. if (err)
  389. pr_info("error %d occurred\n", err);
  390. printk(KERN_INFO "=================================================\n");
  391. return err;
  392. }
  393. module_init(mtd_pagetest_init);
  394. static void __exit mtd_pagetest_exit(void)
  395. {
  396. return;
  397. }
  398. module_exit(mtd_pagetest_exit);
  399. MODULE_DESCRIPTION("NAND page test");
  400. MODULE_AUTHOR("Adrian Hunter");
  401. MODULE_LICENSE("GPL");