mtd_erasepart.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  1. /*
  2. * Copyright (c) 2010, The Linux Foundation. All rights reserved.
  3. * Copyright (C) 2006-2008 Nokia Corporation
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License version 2 as published by
  7. * the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope that it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. *
  14. * You should have received a copy of the GNU General Public License along with
  15. * this program; see the file COPYING. If not, write to the Free Software
  16. * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  17. *
  18. * Erase the given MTD partition.
  19. *
  20. */
  21. #include <asm/div64.h>
  22. #include <linux/init.h>
  23. #include <linux/module.h>
  24. #include <linux/moduleparam.h>
  25. #include <linux/err.h>
  26. #include <linux/mtd/mtd.h>
  27. #include <linux/sched.h>
  28. #include <linux/slab.h>
  29. #define PRINT_PREF KERN_INFO "mtd_erasepart: "
  30. static int dev;
  31. module_param(dev, int, S_IRUGO);
  32. MODULE_PARM_DESC(dev, "MTD device number to use");
  33. static struct mtd_info *mtd;
  34. static unsigned char *bbt;
  35. static int ebcnt;
  36. static int erase_eraseblock(int ebnum)
  37. {
  38. int err;
  39. struct erase_info ei;
  40. loff_t addr = ebnum * mtd->erasesize;
  41. memset(&ei, 0, sizeof(struct erase_info));
  42. ei.mtd = mtd;
  43. ei.addr = addr;
  44. ei.len = mtd->erasesize;
  45. err = mtd_erase(mtd, &ei);
  46. if (err) {
  47. printk(PRINT_PREF "error %d while erasing EB %d\n", err, ebnum);
  48. return err;
  49. }
  50. if (ei.state == MTD_ERASE_FAILED) {
  51. printk(PRINT_PREF "some erase error occurred at EB %d\n",
  52. ebnum);
  53. return -EIO;
  54. }
  55. return 0;
  56. }
  57. static int erase_whole_device(void)
  58. {
  59. int err;
  60. unsigned int i;
  61. printk(PRINT_PREF "erasing whole device\n");
  62. for (i = 0; i < ebcnt; ++i) {
  63. if (bbt[i])
  64. continue;
  65. err = erase_eraseblock(i);
  66. if (err)
  67. return err;
  68. cond_resched();
  69. }
  70. printk(PRINT_PREF "erased %u eraseblocks\n", i);
  71. return 0;
  72. }
  73. static int is_block_bad(int ebnum)
  74. {
  75. int ret;
  76. loff_t addr = ebnum * mtd->erasesize;
  77. ret = mtd_block_isbad(mtd, addr);
  78. if (ret)
  79. printk(PRINT_PREF "block %d is bad\n", ebnum);
  80. return ret;
  81. }
  82. static int scan_for_bad_eraseblocks(void)
  83. {
  84. int i, bad = 0;
  85. bbt = kmalloc(ebcnt, GFP_KERNEL);
  86. if (!bbt) {
  87. printk(PRINT_PREF "error: cannot allocate memory\n");
  88. return -ENOMEM;
  89. }
  90. memset(bbt, 0 , ebcnt);
  91. printk(PRINT_PREF "scanning for bad eraseblocks\n");
  92. for (i = 0; i < ebcnt; ++i) {
  93. bbt[i] = is_block_bad(i) ? 1 : 0;
  94. if (bbt[i])
  95. bad += 1;
  96. cond_resched();
  97. }
  98. printk(PRINT_PREF "scanned %d eraseblocks, %d are bad\n", i, bad);
  99. return 0;
  100. }
  101. static int __init mtd_erasepart_init(void)
  102. {
  103. int err = 0;
  104. uint64_t tmp;
  105. printk(KERN_INFO "\n");
  106. printk(KERN_INFO "=================================================\n");
  107. printk(PRINT_PREF "MTD device: %d\n", dev);
  108. mtd = get_mtd_device(NULL, dev);
  109. if (IS_ERR(mtd)) {
  110. err = PTR_ERR(mtd);
  111. printk(PRINT_PREF "error: cannot get MTD device\n");
  112. return err;
  113. }
  114. if (mtd->type != MTD_NANDFLASH) {
  115. printk(PRINT_PREF "this test requires NAND flash\n");
  116. err = -ENODEV;
  117. goto out2;
  118. }
  119. tmp = mtd->size;
  120. do_div(tmp, mtd->erasesize);
  121. ebcnt = tmp;
  122. printk(PRINT_PREF "MTD device size %llu, eraseblock size %u, "
  123. "page size %u, count of eraseblocks %u",
  124. (unsigned long long)mtd->size, mtd->erasesize,
  125. mtd->writesize, ebcnt);
  126. err = scan_for_bad_eraseblocks();
  127. if (err)
  128. goto out1;
  129. printk(PRINT_PREF "Erasing the whole mtd partition\n");
  130. err = erase_whole_device();
  131. out1:
  132. kfree(bbt);
  133. out2:
  134. put_mtd_device(mtd);
  135. if (err)
  136. printk(PRINT_PREF "error %d occurred\n", err);
  137. printk(KERN_INFO "=================================================\n");
  138. return err;
  139. }
  140. module_init(mtd_erasepart_init);
  141. static void __exit mtd_erasepart_exit(void)
  142. {
  143. return;
  144. }
  145. module_exit(mtd_erasepart_exit);
  146. MODULE_DESCRIPTION("Erase a given MTD partition");
  147. MODULE_LICENSE("GPL v2");