mtd_test.c 2.2 KB

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