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