401-partial_eraseblock_write.patch 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. --- a/drivers/mtd/mtdpart.c
  2. +++ b/drivers/mtd/mtdpart.c
  3. @@ -35,6 +35,8 @@
  4. #include "mtdcore.h"
  5. +#define MTD_ERASE_PARTIAL 0x8000 /* partition only covers parts of an erase block */
  6. +
  7. /* Our partition linked list */
  8. static LIST_HEAD(mtd_partitions);
  9. static DEFINE_MUTEX(mtd_partitions_mutex);
  10. @@ -252,13 +254,61 @@ static int part_erase(struct mtd_info *m
  11. return -EROFS;
  12. if (instr->addr >= mtd->size)
  13. return -EINVAL;
  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 = mtd_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. @@ -266,7 +316,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. @@ -537,18 +605,24 @@ 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. + if ((slave->mtd.flags & (MTD_ERASE_PARTIAL|MTD_WRITEABLE)) == MTD_ERASE_PARTIAL)
  125. + printk(KERN_WARNING"mtd: partition \"%s\" must either start or end on erase block boundary or be smaller than an erase block -- forcing read-only\n",
  126. + part->name);
  127. slave->mtd.ecclayout = master->ecclayout;
  128. if (master->block_isbad) {
  129. --- a/include/linux/mtd/mtd.h
  130. +++ b/include/linux/mtd/mtd.h
  131. @@ -58,6 +58,10 @@ struct erase_info {
  132. u_long priv;
  133. u_char state;
  134. struct erase_info *next;
  135. +
  136. + u8 *erase_buf;
  137. + u32 erase_buf_ofs;
  138. + bool partial_start;
  139. };
  140. struct mtd_erase_region_info {