dfu_mtd: Add provision to unlock mtd device
authorSughosh Ganu <sughosh.ganu@linaro.org>
Wed, 30 Dec 2020 13:57:06 +0000 (19:27 +0530)
committerHeinrich Schuchardt <xypron.glpk@gmx.de>
Thu, 31 Dec 2020 13:41:31 +0000 (14:41 +0100)
Prior to writing to an mtd device, mtd_erase is called. This call
fails in case the sector being erased is locked. Call mtd_unlock to
unlock the region which is to be erased and later written to. Lock the
region once the write to the region has completed.

Signed-off-by: Sughosh Ganu <sughosh.ganu@linaro.org>
drivers/dfu/dfu_mtd.c

index 36cd4e9..b34975d 100644 (file)
@@ -21,7 +21,7 @@ static bool mtd_is_aligned_with_block_size(struct mtd_info *mtd, u64 size)
 static int mtd_block_op(enum dfu_op op, struct dfu_entity *dfu,
                        u64 offset, void *buf, long *len)
 {
-       u64 off, lim, remaining;
+       u64 off, lim, remaining, lock_ofs, lock_len;
        struct mtd_info *mtd = dfu->data.mtd.info;
        struct mtd_oob_ops io_op = {};
        int ret = 0;
@@ -34,7 +34,7 @@ static int mtd_block_op(enum dfu_op op, struct dfu_entity *dfu,
                return 0;
        }
 
-       off = dfu->data.mtd.start + offset + dfu->bad_skip;
+       off = lock_ofs = dfu->data.mtd.start + offset + dfu->bad_skip;
        lim = dfu->data.mtd.start + dfu->data.mtd.size;
 
        if (off >= lim) {
@@ -56,12 +56,19 @@ static int mtd_block_op(enum dfu_op op, struct dfu_entity *dfu,
        if (op == DFU_OP_WRITE) {
                struct erase_info erase_op = {};
 
-               remaining = round_up(*len, mtd->erasesize);
+               remaining = lock_len = round_up(*len, mtd->erasesize);
                erase_op.mtd = mtd;
                erase_op.addr = off;
                erase_op.len = mtd->erasesize;
                erase_op.scrub = 0;
 
+               debug("Unlocking the mtd device\n");
+               ret = mtd_unlock(mtd, lock_ofs, lock_len);
+               if (ret && ret != -EOPNOTSUPP) {
+                       printf("MTD device unlock failed\n");
+                       return 0;
+               }
+
                while (remaining) {
                        if (erase_op.addr + remaining > lim) {
                                printf("Limit reached 0x%llx while erasing at offset 0x%llx\n",
@@ -139,6 +146,13 @@ static int mtd_block_op(enum dfu_op op, struct dfu_entity *dfu,
                        io_op.len = mtd->writesize;
        }
 
+       if (op == DFU_OP_WRITE) {
+               /* Write done, lock again */
+               debug("Locking the mtd device\n");
+               ret = mtd_lock(mtd, lock_ofs, lock_len);
+               if (ret && ret != -EOPNOTSUPP)
+                       printf("MTD device lock failed\n");
+       }
        return ret;
 }