2018-11-01 16:57:55 +00:00
|
|
|
From: Felix Fietkau <nbd@nbd.name>
|
|
|
|
Subject: mtd: implement write support for partitions covering only a part of an eraseblock (buffer data that would otherwise be erased)
|
|
|
|
|
|
|
|
lede-commit: 87a8e8ac1067f58ba831c4aae443f3655c31cd80
|
|
|
|
Signed-off-by: Felix Fietkau <nbd@nbd.name>
|
|
|
|
---
|
|
|
|
drivers/mtd/mtdpart.c | 90 ++++++++++++++++++++++++++++++++++++++++++++-----
|
|
|
|
include/linux/mtd/mtd.h | 4 +++
|
|
|
|
2 files changed, 85 insertions(+), 9 deletions(-)
|
|
|
|
|
|
|
|
--- a/drivers/mtd/mtdpart.c
|
|
|
|
+++ b/drivers/mtd/mtdpart.c
|
|
|
|
@@ -37,6 +37,8 @@
|
|
|
|
#include "mtdcore.h"
|
|
|
|
#include "mtdsplit/mtdsplit.h"
|
|
|
|
|
|
|
|
+#define MTD_ERASE_PARTIAL 0x8000 /* partition only covers parts of an erase block */
|
|
|
|
+
|
|
|
|
/* Our partition linked list */
|
|
|
|
static LIST_HEAD(mtd_partitions);
|
|
|
|
static DEFINE_MUTEX(mtd_partitions_mutex);
|
2018-11-01 17:01:44 +00:00
|
|
|
@@ -221,6 +223,53 @@ static int part_erase(struct mtd_info *m
|
|
|
|
{
|
2018-11-01 16:57:55 +00:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
|
|
|
int ret;
|
2018-11-01 17:01:44 +00:00
|
|
|
+ size_t wrlen = 0;
|
|
|
|
+ u8 *erase_buf = NULL;
|
|
|
|
+ u32 erase_buf_ofs = 0;
|
|
|
|
+ bool partial_start = false;
|
2018-11-01 16:57:55 +00:00
|
|
|
+
|
|
|
|
+ if (mtd->flags & MTD_ERASE_PARTIAL) {
|
|
|
|
+ size_t readlen = 0;
|
|
|
|
+ u64 mtd_ofs;
|
|
|
|
+
|
2018-11-01 17:01:44 +00:00
|
|
|
+ erase_buf = kmalloc(part->parent->erasesize, GFP_ATOMIC);
|
|
|
|
+ if (!erase_buf)
|
2018-11-01 16:57:55 +00:00
|
|
|
+ return -ENOMEM;
|
|
|
|
+
|
|
|
|
+ mtd_ofs = part->offset + instr->addr;
|
2018-11-01 17:01:44 +00:00
|
|
|
+ erase_buf_ofs = do_div(mtd_ofs, part->parent->erasesize);
|
2018-11-01 16:57:55 +00:00
|
|
|
+
|
2018-11-01 17:01:44 +00:00
|
|
|
+ if (erase_buf_ofs > 0) {
|
|
|
|
+ instr->addr -= erase_buf_ofs;
|
2018-11-01 16:57:55 +00:00
|
|
|
+ ret = mtd_read(part->parent,
|
|
|
|
+ instr->addr + part->offset,
|
|
|
|
+ part->parent->erasesize,
|
2018-11-01 17:01:44 +00:00
|
|
|
+ &readlen, erase_buf);
|
2018-11-01 16:57:55 +00:00
|
|
|
+
|
2018-11-01 17:01:44 +00:00
|
|
|
+ instr->len += erase_buf_ofs;
|
|
|
|
+ partial_start = true;
|
2018-11-01 16:57:55 +00:00
|
|
|
+ } else {
|
|
|
|
+ mtd_ofs = part->offset + part->mtd.size;
|
2018-11-01 17:01:44 +00:00
|
|
|
+ erase_buf_ofs = part->parent->erasesize -
|
2018-11-01 16:57:55 +00:00
|
|
|
+ do_div(mtd_ofs, part->parent->erasesize);
|
|
|
|
+
|
2018-11-01 17:01:44 +00:00
|
|
|
+ if (erase_buf_ofs > 0) {
|
|
|
|
+ instr->len += erase_buf_ofs;
|
2018-11-01 16:57:55 +00:00
|
|
|
+ ret = mtd_read(part->parent,
|
|
|
|
+ part->offset + instr->addr +
|
|
|
|
+ instr->len - part->parent->erasesize,
|
|
|
|
+ part->parent->erasesize, &readlen,
|
2018-11-01 17:01:44 +00:00
|
|
|
+ erase_buf);
|
2018-11-01 16:57:55 +00:00
|
|
|
+ } else {
|
|
|
|
+ ret = 0;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ if (ret < 0) {
|
2018-11-01 17:01:44 +00:00
|
|
|
+ kfree(erase_buf);
|
2018-11-01 16:57:55 +00:00
|
|
|
+ return ret;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
2018-11-01 17:01:44 +00:00
|
|
|
|
2018-11-01 16:57:55 +00:00
|
|
|
instr->addr += part->offset;
|
|
|
|
ret = part->parent->_erase(part->parent, instr);
|
2018-11-01 17:01:44 +00:00
|
|
|
@@ -228,6 +277,24 @@ static int part_erase(struct mtd_info *m
|
|
|
|
instr->fail_addr -= part->offset;
|
|
|
|
instr->addr -= part->offset;
|
|
|
|
|
|
|
|
+ if (mtd->flags & MTD_ERASE_PARTIAL) {
|
|
|
|
+ if (partial_start) {
|
|
|
|
+ part->parent->_write(part->parent,
|
|
|
|
+ instr->addr, erase_buf_ofs,
|
|
|
|
+ &wrlen, erase_buf);
|
|
|
|
+ instr->addr += erase_buf_ofs;
|
|
|
|
+ } else {
|
|
|
|
+ instr->len -= erase_buf_ofs;
|
|
|
|
+ part->parent->_write(part->parent,
|
|
|
|
+ instr->addr + instr->len,
|
|
|
|
+ erase_buf_ofs, &wrlen,
|
|
|
|
+ erase_buf +
|
|
|
|
+ part->parent->erasesize -
|
|
|
|
+ erase_buf_ofs);
|
|
|
|
+ }
|
|
|
|
+ kfree(erase_buf);
|
|
|
|
+ }
|
2018-11-01 16:57:55 +00:00
|
|
|
+
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-11-01 17:01:44 +00:00
|
|
|
@@ -536,19 +603,22 @@ static struct mtd_part *allocate_partiti
|
2018-11-01 16:57:55 +00:00
|
|
|
remainder = do_div(tmp, wr_alignment);
|
|
|
|
if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
|
|
|
|
/* Doesn't start on a boundary of major erase size */
|
|
|
|
- /* FIXME: Let it be writable if it is on a boundary of
|
|
|
|
- * _minor_ erase size though */
|
|
|
|
- slave->mtd.flags &= ~MTD_WRITEABLE;
|
|
|
|
- printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
|
|
|
|
- part->name);
|
|
|
|
+ slave->mtd.flags |= MTD_ERASE_PARTIAL;
|
|
|
|
+ if (((u32)slave->mtd.size) > parent->erasesize)
|
|
|
|
+ slave->mtd.flags &= ~MTD_WRITEABLE;
|
|
|
|
+ else
|
|
|
|
+ slave->mtd.erasesize = slave->mtd.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
- tmp = part_absolute_offset(parent) + slave->mtd.size;
|
|
|
|
+ tmp = part_absolute_offset(parent) + slave->offset + slave->mtd.size;
|
|
|
|
remainder = do_div(tmp, wr_alignment);
|
|
|
|
if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
|
|
|
|
- slave->mtd.flags &= ~MTD_WRITEABLE;
|
|
|
|
- printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
|
|
|
|
- part->name);
|
|
|
|
+ slave->mtd.flags |= MTD_ERASE_PARTIAL;
|
|
|
|
+
|
|
|
|
+ if ((u32)slave->mtd.size > parent->erasesize)
|
|
|
|
+ slave->mtd.flags &= ~MTD_WRITEABLE;
|
|
|
|
+ else
|
|
|
|
+ slave->mtd.erasesize = slave->mtd.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
|