411-mtd-partial_eraseblock_write.patch 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. --- a/drivers/mtd/mtdpart.c
  2. +++ b/drivers/mtd/mtdpart.c
  3. @@ -36,6 +36,8 @@
  4. #include "mtdcore.h"
  5. #include "mtdsplit/mtdsplit.h"
  6. +#define MTD_ERASE_PARTIAL 0x8000 /* partition only covers parts of an erase block */
  7. +
  8. /* Our partition linked list */
  9. static LIST_HEAD(mtd_partitions);
  10. static DEFINE_MUTEX(mtd_partitions_mutex);
  11. @@ -234,13 +236,61 @@ static int part_erase(struct mtd_info *m
  12. struct mtd_part *part = PART(mtd);
  13. int ret;
  14. +
  15. + instr->partial_start = false;
  16. + if (mtd->flags & MTD_ERASE_PARTIAL) {
  17. + size_t readlen = 0;
  18. + u64 mtd_ofs;
  19. +
  20. + instr->erase_buf = kmalloc(part->master->erasesize, GFP_ATOMIC);
  21. + if (!instr->erase_buf)
  22. + return -ENOMEM;
  23. +
  24. + mtd_ofs = part->offset + instr->addr;
  25. + instr->erase_buf_ofs = do_div(mtd_ofs, part->master->erasesize);
  26. +
  27. + if (instr->erase_buf_ofs > 0) {
  28. + instr->addr -= instr->erase_buf_ofs;
  29. + ret = mtd_read(part->master,
  30. + instr->addr + part->offset,
  31. + part->master->erasesize,
  32. + &readlen, instr->erase_buf);
  33. +
  34. + instr->len += instr->erase_buf_ofs;
  35. + instr->partial_start = true;
  36. + } else {
  37. + mtd_ofs = part->offset + part->mtd.size;
  38. + instr->erase_buf_ofs = part->master->erasesize -
  39. + do_div(mtd_ofs, part->master->erasesize);
  40. +
  41. + if (instr->erase_buf_ofs > 0) {
  42. + instr->len += instr->erase_buf_ofs;
  43. + ret = mtd_read(part->master,
  44. + part->offset + instr->addr +
  45. + instr->len - part->master->erasesize,
  46. + part->master->erasesize, &readlen,
  47. + instr->erase_buf);
  48. + } else {
  49. + ret = 0;
  50. + }
  51. + }
  52. + if (ret < 0) {
  53. + kfree(instr->erase_buf);
  54. + return ret;
  55. + }
  56. +
  57. + }
  58. +
  59. instr->addr += part->offset;
  60. ret = part->master->_erase(part->master, instr);
  61. if (ret) {
  62. if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  63. instr->fail_addr -= part->offset;
  64. instr->addr -= part->offset;
  65. + if (mtd->flags & MTD_ERASE_PARTIAL)
  66. + kfree(instr->erase_buf);
  67. }
  68. +
  69. return ret;
  70. }
  71. @@ -248,7 +298,25 @@ void mtd_erase_callback(struct erase_inf
  72. {
  73. if (instr->mtd->_erase == part_erase) {
  74. struct mtd_part *part = PART(instr->mtd);
  75. + size_t wrlen = 0;
  76. + if (instr->mtd->flags & MTD_ERASE_PARTIAL) {
  77. + if (instr->partial_start) {
  78. + part->master->_write(part->master,
  79. + instr->addr, instr->erase_buf_ofs,
  80. + &wrlen, instr->erase_buf);
  81. + instr->addr += instr->erase_buf_ofs;
  82. + } else {
  83. + instr->len -= instr->erase_buf_ofs;
  84. + part->master->_write(part->master,
  85. + instr->addr + instr->len,
  86. + instr->erase_buf_ofs, &wrlen,
  87. + instr->erase_buf +
  88. + part->master->erasesize -
  89. + instr->erase_buf_ofs);
  90. + }
  91. + kfree(instr->erase_buf);
  92. + }
  93. if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
  94. instr->fail_addr -= part->offset;
  95. instr->addr -= part->offset;
  96. @@ -515,17 +583,20 @@ static struct mtd_part *allocate_partiti
  97. if ((slave->mtd.flags & MTD_WRITEABLE) &&
  98. mtd_mod_by_eb(slave->offset, &slave->mtd)) {
  99. /* Doesn't start on a boundary of major erase size */
  100. - /* FIXME: Let it be writable if it is on a boundary of
  101. - * _minor_ erase size though */
  102. - slave->mtd.flags &= ~MTD_WRITEABLE;
  103. - printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
  104. - part->name);
  105. + slave->mtd.flags |= MTD_ERASE_PARTIAL;
  106. + if (((u32) slave->mtd.size) > master->erasesize)
  107. + slave->mtd.flags &= ~MTD_WRITEABLE;
  108. + else
  109. + slave->mtd.erasesize = slave->mtd.size;
  110. }
  111. if ((slave->mtd.flags & MTD_WRITEABLE) &&
  112. - mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
  113. - slave->mtd.flags &= ~MTD_WRITEABLE;
  114. - printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
  115. - part->name);
  116. + mtd_mod_by_eb(slave->offset + slave->mtd.size, &slave->mtd)) {
  117. + slave->mtd.flags |= MTD_ERASE_PARTIAL;
  118. +
  119. + if ((u32) slave->mtd.size > master->erasesize)
  120. + slave->mtd.flags &= ~MTD_WRITEABLE;
  121. + else
  122. + slave->mtd.erasesize = slave->mtd.size;
  123. }
  124. slave->mtd.ecclayout = master->ecclayout;
  125. --- a/include/linux/mtd/mtd.h
  126. +++ b/include/linux/mtd/mtd.h
  127. @@ -55,6 +55,10 @@ struct erase_info {
  128. u_long priv;
  129. u_char state;
  130. struct erase_info *next;
  131. +
  132. + u8 *erase_buf;
  133. + u32 erase_buf_ofs;
  134. + bool partial_start;
  135. };
  136. struct mtd_erase_region_info {