mirror of
https://github.com/AsahiLinux/u-boot
synced 2024-11-29 16:10:58 +00:00
mtd: Add sanity checks in mtd_write/read_oob()
Unlike what's done in mtd_read/write(), there are no checks to make sure the parameters passed to mtd_read/write_oob() are consistent, which forces implementers of ->_read/write_oob() to do it, which in turn leads to code duplication and possibly errors in the logic. Do general sanity checks, like ops fields consistency and range checking. Signed-off-by: Boris Brezillon <boris.brezillon@free-electrons.com> Cc: Peter Pan <peterpandong@micron.com> Signed-off-by: Richard Weinberger <richard@nod.at> [Miquel: squashed the fix about the chip's size check] Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
This commit is contained in:
parent
5f50d82d89
commit
8fad769f1e
1 changed files with 45 additions and 0 deletions
|
@ -1010,12 +1010,50 @@ int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(mtd_panic_write);
|
EXPORT_SYMBOL_GPL(mtd_panic_write);
|
||||||
|
|
||||||
|
static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
|
||||||
|
struct mtd_oob_ops *ops)
|
||||||
|
{
|
||||||
|
/*
|
||||||
|
* Some users are setting ->datbuf or ->oobbuf to NULL, but are leaving
|
||||||
|
* ->len or ->ooblen uninitialized. Force ->len and ->ooblen to 0 in
|
||||||
|
* this case.
|
||||||
|
*/
|
||||||
|
if (!ops->datbuf)
|
||||||
|
ops->len = 0;
|
||||||
|
|
||||||
|
if (!ops->oobbuf)
|
||||||
|
ops->ooblen = 0;
|
||||||
|
|
||||||
|
if (offs < 0 || offs + ops->len > mtd->size)
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
if (ops->ooblen) {
|
||||||
|
u64 maxooblen;
|
||||||
|
|
||||||
|
if (ops->ooboffs >= mtd_oobavail(mtd, ops))
|
||||||
|
return -EINVAL;
|
||||||
|
|
||||||
|
maxooblen = ((mtd_div_by_ws(mtd->size, mtd) -
|
||||||
|
mtd_div_by_ws(offs, mtd)) *
|
||||||
|
mtd_oobavail(mtd, ops)) - ops->ooboffs;
|
||||||
|
if (ops->ooblen > maxooblen)
|
||||||
|
return -EINVAL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
|
int mtd_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
|
||||||
{
|
{
|
||||||
int ret_code;
|
int ret_code;
|
||||||
ops->retlen = ops->oobretlen = 0;
|
ops->retlen = ops->oobretlen = 0;
|
||||||
if (!mtd->_read_oob)
|
if (!mtd->_read_oob)
|
||||||
return -EOPNOTSUPP;
|
return -EOPNOTSUPP;
|
||||||
|
|
||||||
|
ret_code = mtd_check_oob_ops(mtd, from, ops);
|
||||||
|
if (ret_code)
|
||||||
|
return ret_code;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
|
* In cases where ops->datbuf != NULL, mtd->_read_oob() has semantics
|
||||||
* similar to mtd->_read(), returning a non-negative integer
|
* similar to mtd->_read(), returning a non-negative integer
|
||||||
|
@ -1034,11 +1072,18 @@ EXPORT_SYMBOL_GPL(mtd_read_oob);
|
||||||
int mtd_write_oob(struct mtd_info *mtd, loff_t to,
|
int mtd_write_oob(struct mtd_info *mtd, loff_t to,
|
||||||
struct mtd_oob_ops *ops)
|
struct mtd_oob_ops *ops)
|
||||||
{
|
{
|
||||||
|
int ret;
|
||||||
|
|
||||||
ops->retlen = ops->oobretlen = 0;
|
ops->retlen = ops->oobretlen = 0;
|
||||||
if (!mtd->_write_oob)
|
if (!mtd->_write_oob)
|
||||||
return -EOPNOTSUPP;
|
return -EOPNOTSUPP;
|
||||||
if (!(mtd->flags & MTD_WRITEABLE))
|
if (!(mtd->flags & MTD_WRITEABLE))
|
||||||
return -EROFS;
|
return -EROFS;
|
||||||
|
|
||||||
|
ret = mtd_check_oob_ops(mtd, to, ops);
|
||||||
|
if (ret)
|
||||||
|
return ret;
|
||||||
|
|
||||||
return mtd->_write_oob(mtd, to, ops);
|
return mtd->_write_oob(mtd, to, ops);
|
||||||
}
|
}
|
||||||
EXPORT_SYMBOL_GPL(mtd_write_oob);
|
EXPORT_SYMBOL_GPL(mtd_write_oob);
|
||||||
|
|
Loading…
Reference in a new issue