DiskOnChip G3 has eight free bytes in its 16-byte spare area. An OOB-only AUTO write of all eight bytes fails. A 16-byte request succeeds, but the driver stages and reports only the first eight bytes. Require one page's worth of spare bytes in the selected mode for a staged write. MEMWRITE supplies the data and OOB lengths separately: two data pages can have spare bytes for only the first. Require a data page for each supplied spare area, not a spare area for every data page. Reject partial spare areas and nonzero spare offsets instead of using the offset to index the caller's buffer. Fixes: fb50b58e488d ("mtd: docg3: add write functions") Cc: stable@vger.kernel.org Signed-off-by: James Hilliard --- drivers/mtd/devices/docg3.c | 117 +++++++++++++------------------------------- 1 file changed, 35 insertions(+), 82 deletions(-) diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c index 1f0b1b0f4a83..9617b1ebba0f 100644 --- a/drivers/mtd/devices/docg3.c +++ b/drivers/mtd/devices/docg3.c @@ -1288,30 +1288,6 @@ static int doc_write_page(struct docg3 *docg3, loff_t to, const u_char *buf, return ret; } -/** - * doc_guess_autoecc - Guess autoecc mode from mbd_oob_ops - * @ops: the oob operations - * - * Returns 0 or 1 if success, -EINVAL if invalid oob mode - */ -static int doc_guess_autoecc(struct mtd_oob_ops *ops) -{ - int autoecc; - - switch (ops->mode) { - case MTD_OPS_PLACE_OOB: - case MTD_OPS_AUTO_OOB: - autoecc = 1; - break; - case MTD_OPS_RAW: - autoecc = 0; - break; - default: - autoecc = -EINVAL; - } - return autoecc; -} - /** * doc_fill_autooob - Fill a 16 bytes OOB from 8 non-ECC bytes * @dst: the target 16 bytes OOB buffer @@ -1324,9 +1300,26 @@ static void doc_fill_autooob(u8 *dst, u8 *oobsrc) dst[DOC_LAYOUT_OOB_UNUSED_OFS] = oobsrc[DOC_LAYOUT_OOB_PAGEINFO_SZ]; } +static bool doc_oob_write_valid(struct mtd_info *mtd, u64 to, struct mtd_oob_ops *ops) +{ + u32 avail = mtd_oobavail(mtd, ops); + + if (ops->mode != MTD_OPS_PLACE_OOB && ops->mode != MTD_OPS_AUTO_OOB && + ops->mode != MTD_OPS_RAW) + return false; + if (to % DOC_LAYOUT_PAGE_SIZE || ops->len % DOC_LAYOUT_PAGE_SIZE || + (ops->ooblen && (ops->ooboffs || ops->ooblen % avail))) + return false; + + if (!ops->len) + return ops->ooblen == avail; + + return ops->ooblen / avail <= ops->len / DOC_LAYOUT_PAGE_SIZE; +} + /** * doc_backup_oob - Backup OOB into docg3 structure - * @docg3: the device + * @mtd: flash device * @to: the page offset in the chip * @ops: the OOB size and buffer * @@ -1338,30 +1331,18 @@ static void doc_fill_autooob(u8 *dst, u8 *oobsrc) * * The only reliable way would be for userland to call doc_write_oob() with both * the page data _and_ the OOB area. - * - * Returns 0 if success, -EINVAL if ops content invalid */ -static int doc_backup_oob(struct docg3 *docg3, loff_t to, - struct mtd_oob_ops *ops) +static void doc_backup_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops) { - int ooblen = ops->ooblen, autoecc; - - if (ooblen != DOC_LAYOUT_OOB_SIZE) - return -EINVAL; - autoecc = doc_guess_autoecc(ops); - if (autoecc < 0) - return autoecc; + struct docg3 *docg3 = mtd->priv; docg3->oob_write_ofs = to; - docg3->oob_autoecc = autoecc; - if (ops->mode == MTD_OPS_AUTO_OOB) { + docg3->oob_autoecc = ops->mode != MTD_OPS_RAW; + if (ops->mode == MTD_OPS_AUTO_OOB) doc_fill_autooob(docg3->oob_write_buf, ops->oobbuf); - ops->oobretlen = 8; - } else { + else memcpy(docg3->oob_write_buf, ops->oobbuf, DOC_LAYOUT_OOB_SIZE); - ops->oobretlen = DOC_LAYOUT_OOB_SIZE; - } - return 0; + ops->oobretlen = ops->ooblen; } /** @@ -1372,12 +1353,12 @@ static int doc_backup_oob(struct docg3 *docg3, loff_t to, * @ops: the mtd oob structure * * Either write OOB data into a temporary buffer, for the subsequent write - * page. The provided OOB should be 16 bytes long. If a data buffer is provided - * as well, issue the page write. + * page. An OOB-only write supplies one page in the selected mode. If a data + * buffer is provided as well, issue the page write. * Or provide data without OOB, and then a all zeroed OOB will be used (ECC will * still be filled in if asked for). * - * Returns 0 is successful, EINVAL if length is not 14 bytes + * Returns 0 on success or a negative error code. */ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, struct mtd_oob_ops *ops) @@ -1386,52 +1367,24 @@ static int doc_write_oob(struct mtd_info *mtd, loff_t ofs, int ret, autoecc, oobdelta; u8 *oobbuf = ops->oobbuf; u8 *buf = ops->datbuf; - size_t len, ooblen; + size_t len = ops->len, ooblen = ops->ooblen; u8 oob[DOC_LAYOUT_OOB_SIZE]; - if (buf) - len = ops->len; - else - len = 0; - if (oobbuf) - ooblen = ops->ooblen; - else - ooblen = 0; - - if (oobbuf && ops->mode == MTD_OPS_PLACE_OOB) - oobbuf += ops->ooboffs; - doc_dbg("doc_write_oob(from=%lld, mode=%d, data=(%p:%zu), oob=(%p:%zu))\n", ofs, ops->mode, buf, len, oobbuf, ooblen); - switch (ops->mode) { - case MTD_OPS_PLACE_OOB: - case MTD_OPS_RAW: - oobdelta = mtd->oobsize; - break; - case MTD_OPS_AUTO_OOB: - oobdelta = mtd->oobavail; - break; - default: - return -EINVAL; - } - if ((len % DOC_LAYOUT_PAGE_SIZE) || (ooblen % oobdelta) || - (ofs % DOC_LAYOUT_PAGE_SIZE)) - return -EINVAL; - if (len && ooblen && - (len / DOC_LAYOUT_PAGE_SIZE) != (ooblen / oobdelta)) + if (!doc_oob_write_valid(mtd, ofs, ops)) return -EINVAL; ops->oobretlen = 0; ops->retlen = 0; ret = 0; - if (len == 0 && ooblen == 0) - return -EINVAL; - if (len == 0 && ooblen > 0) - return doc_backup_oob(docg3, ofs, ops); + if (!len) { + doc_backup_oob(mtd, ofs, ops); + return 0; + } - autoecc = doc_guess_autoecc(ops); - if (autoecc < 0) - return autoecc; + oobdelta = mtd_oobavail(mtd, ops); + autoecc = ops->mode != MTD_OPS_RAW; mutex_lock(&docg3->cascade->lock); while (!ret && len > 0) { -- 2.53.0