mtd_test.c 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. #define pr_fmt(fmt) "mtd_test: " fmt
  2. #include <linux/module.h>
  3. #include <linux/sched.h>
  4. #include <linux/printk.h>
  5. #include "mtd_test.h"
  6. int mtdtest_erase_eraseblock(struct mtd_info *mtd, unsigned int ebnum)
  7. {
  8. int err;
  9. struct erase_info ei;
  10. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  11. memset(&ei, 0, sizeof(struct erase_info));
  12. ei.mtd = mtd;
  13. ei.addr = addr;
  14. ei.len = mtd->erasesize;
  15. err = mtd_erase(mtd, &ei);
  16. if (err) {
  17. pr_info("error %d while erasing EB %d\n", err, ebnum);
  18. return err;
  19. }
  20. if (ei.state == MTD_ERASE_FAILED) {
  21. pr_info("some erase error occurred at EB %d\n", ebnum);
  22. return -EIO;
  23. }
  24. return 0;
  25. }
  26. static int is_block_bad(struct mtd_info *mtd, unsigned int ebnum)
  27. {
  28. int ret;
  29. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  30. ret = mtd_block_isbad(mtd, addr);
  31. if (ret)
  32. pr_info("block %d is bad\n", ebnum);
  33. return ret;
  34. }
  35. int mtdtest_scan_for_bad_eraseblocks(struct mtd_info *mtd, unsigned char *bbt,
  36. unsigned int eb, int ebcnt)
  37. {
  38. int i, bad = 0;
  39. if (!mtd_can_have_bb(mtd))
  40. return 0;
  41. pr_info("scanning for bad eraseblocks\n");
  42. for (i = 0; i < ebcnt; ++i) {
  43. bbt[i] = is_block_bad(mtd, eb + i) ? 1 : 0;
  44. if (bbt[i])
  45. bad += 1;
  46. cond_resched();
  47. }
  48. pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
  49. return 0;
  50. }
  51. int mtdtest_erase_good_eraseblocks(struct mtd_info *mtd, unsigned char *bbt,
  52. unsigned int eb, int ebcnt)
  53. {
  54. int err;
  55. unsigned int i;
  56. for (i = 0; i < ebcnt; ++i) {
  57. if (bbt[i])
  58. continue;
  59. err = mtdtest_erase_eraseblock(mtd, eb + i);
  60. if (err)
  61. return err;
  62. cond_resched();
  63. }
  64. return 0;
  65. }
  66. int mtdtest_read(struct mtd_info *mtd, loff_t addr, size_t size, void *buf)
  67. {
  68. size_t read;
  69. int err;
  70. err = mtd_read(mtd, addr, size, &read, buf);
  71. /* Ignore corrected ECC errors */
  72. if (mtd_is_bitflip(err))
  73. err = 0;
  74. if (!err && read != size)
  75. err = -EIO;
  76. if (err)
  77. pr_err("error: read failed at %#llx\n", addr);
  78. return err;
  79. }
  80. int mtdtest_write(struct mtd_info *mtd, loff_t addr, size_t size,
  81. const void *buf)
  82. {
  83. size_t written;
  84. int err;
  85. err = mtd_write(mtd, addr, size, &written, buf);
  86. if (!err && written != size)
  87. err = -EIO;
  88. if (err)
  89. pr_err("error: write failed at %#llx\n", addr);
  90. return err;
  91. }