mtd_test.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. // SPDX-License-Identifier: GPL-2.0
  2. #define pr_fmt(fmt) "mtd_test: " fmt
  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 = (loff_t)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. return 0;
  22. }
  23. static int is_block_bad(struct mtd_info *mtd, unsigned int ebnum)
  24. {
  25. int ret;
  26. loff_t addr = (loff_t)ebnum * mtd->erasesize;
  27. ret = mtd_block_isbad(mtd, addr);
  28. if (ret)
  29. pr_info("block %d is bad\n", ebnum);
  30. return ret;
  31. }
  32. int mtdtest_scan_for_bad_eraseblocks(struct mtd_info *mtd, unsigned char *bbt,
  33. unsigned int eb, int ebcnt)
  34. {
  35. int i, bad = 0;
  36. if (!mtd_can_have_bb(mtd))
  37. return 0;
  38. pr_info("scanning for bad eraseblocks\n");
  39. for (i = 0; i < ebcnt; ++i) {
  40. bbt[i] = is_block_bad(mtd, eb + i) ? 1 : 0;
  41. if (bbt[i])
  42. bad += 1;
  43. cond_resched();
  44. }
  45. pr_info("scanned %d eraseblocks, %d are bad\n", i, bad);
  46. return 0;
  47. }
  48. int mtdtest_erase_good_eraseblocks(struct mtd_info *mtd, unsigned char *bbt,
  49. unsigned int eb, int ebcnt)
  50. {
  51. int err;
  52. unsigned int i;
  53. for (i = 0; i < ebcnt; ++i) {
  54. if (bbt[i])
  55. continue;
  56. err = mtdtest_erase_eraseblock(mtd, eb + i);
  57. if (err)
  58. return err;
  59. cond_resched();
  60. }
  61. return 0;
  62. }
  63. int mtdtest_read(struct mtd_info *mtd, loff_t addr, size_t size, void *buf)
  64. {
  65. size_t read;
  66. int err;
  67. err = mtd_read(mtd, addr, size, &read, buf);
  68. /* Ignore corrected ECC errors */
  69. if (mtd_is_bitflip(err))
  70. err = 0;
  71. if (!err && read != size)
  72. err = -EIO;
  73. if (err)
  74. pr_err("error: read failed at %#llx\n", addr);
  75. return err;
  76. }
  77. int mtdtest_write(struct mtd_info *mtd, loff_t addr, size_t size,
  78. const void *buf)
  79. {
  80. size_t written;
  81. int err;
  82. err = mtd_write(mtd, addr, size, &written, buf);
  83. if (!err && written != size)
  84. err = -EIO;
  85. if (err)
  86. pr_err("error: write failed at %#llx\n", addr);
  87. return err;
  88. }