2019-05-23 09:14:39 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* MTD device concatenation layer
|
|
|
|
*
|
2010-08-08 19:58:20 +00:00
|
|
|
* Copyright © 2002 Robert Kaiser <rkaiser@sysgo.de>
|
|
|
|
* Copyright © 2002-2010 David Woodhouse <dwmw2@infradead.org>
|
2005-04-16 22:20:36 +00:00
|
|
|
*
|
|
|
|
* NAND support by Christian Gan <cgan@iders.ca>
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/kernel.h>
|
2005-11-06 23:14:42 +00:00
|
|
|
#include <linux/module.h>
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/slab.h>
|
2005-11-06 23:14:42 +00:00
|
|
|
#include <linux/sched.h>
|
|
|
|
#include <linux/types.h>
|
2009-02-12 10:40:05 +00:00
|
|
|
#include <linux/backing-dev.h>
|
2005-11-06 23:14:42 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
#include <linux/mtd/mtd.h>
|
|
|
|
#include <linux/mtd/concat.h>
|
|
|
|
|
2006-05-20 09:17:21 +00:00
|
|
|
#include <asm/div64.h>
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* Our storage structure:
|
|
|
|
* Subdev points to an array of pointers to struct mtd_info objects
|
|
|
|
* which is allocated along with this structure
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
struct mtd_concat {
|
|
|
|
struct mtd_info mtd;
|
|
|
|
int num_subdev;
|
|
|
|
struct mtd_info **subdev;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* how to calculate the size required for the above structure,
|
|
|
|
* including the pointer array subdev points to:
|
|
|
|
*/
|
|
|
|
#define SIZEOF_STRUCT_MTD_CONCAT(num_subdev) \
|
|
|
|
((sizeof(struct mtd_concat) + (num_subdev) * sizeof(struct mtd_info *)))
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a pointer to the MTD object in the mtd_concat structure,
|
|
|
|
* we can retrieve the pointer to that structure with this macro.
|
|
|
|
*/
|
|
|
|
#define CONCAT(x) ((struct mtd_concat *)(x))
|
|
|
|
|
2005-11-07 11:15:26 +00:00
|
|
|
/*
|
2005-04-16 22:20:36 +00:00
|
|
|
* MTD methods which look up the relevant subdevice, translate the
|
|
|
|
* effective address and pass through to the subdevice.
|
|
|
|
*/
|
|
|
|
|
|
|
|
static int
|
|
|
|
concat_read(struct mtd_info *mtd, loff_t from, size_t len,
|
|
|
|
size_t * retlen, u_char * buf)
|
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
2006-05-29 22:37:34 +00:00
|
|
|
int ret = 0, err;
|
2005-04-16 22:20:36 +00:00
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
struct mtd_info *subdev = concat->subdev[i];
|
|
|
|
size_t size, retsize;
|
|
|
|
|
|
|
|
if (from >= subdev->size) {
|
|
|
|
/* Not destined for this subdev */
|
|
|
|
size = 0;
|
|
|
|
from -= subdev->size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (from + len > subdev->size)
|
|
|
|
/* First part goes into this subdev */
|
|
|
|
size = subdev->size - from;
|
|
|
|
else
|
|
|
|
/* Entire transaction goes into this subdev */
|
|
|
|
size = len;
|
|
|
|
|
2011-12-23 15:30:16 +00:00
|
|
|
err = mtd_read(subdev, from, size, &retsize, buf);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-05-29 12:56:39 +00:00
|
|
|
/* Save information about bitflips! */
|
2006-05-29 22:37:34 +00:00
|
|
|
if (unlikely(err)) {
|
2011-09-21 01:34:25 +00:00
|
|
|
if (mtd_is_eccerr(err)) {
|
2006-05-29 22:37:34 +00:00
|
|
|
mtd->ecc_stats.failed++;
|
2006-05-29 12:56:39 +00:00
|
|
|
ret = err;
|
2011-09-21 01:34:25 +00:00
|
|
|
} else if (mtd_is_bitflip(err)) {
|
2006-05-29 22:37:34 +00:00
|
|
|
mtd->ecc_stats.corrected++;
|
|
|
|
/* Do not overwrite -EBADMSG !! */
|
|
|
|
if (!ret)
|
|
|
|
ret = err;
|
|
|
|
} else
|
|
|
|
return err;
|
2006-05-29 12:56:39 +00:00
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
*retlen += retsize;
|
|
|
|
len -= size;
|
|
|
|
if (len == 0)
|
2006-05-29 22:37:34 +00:00
|
|
|
return ret;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
buf += size;
|
|
|
|
from = 0;
|
|
|
|
}
|
2006-05-29 22:37:34 +00:00
|
|
|
return -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2020-06-02 14:34:03 +00:00
|
|
|
static int
|
|
|
|
concat_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
|
|
|
|
size_t * retlen, const u_char * buf)
|
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
|
|
|
int err = -EINVAL;
|
|
|
|
int i;
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
struct mtd_info *subdev = concat->subdev[i];
|
|
|
|
size_t size, retsize;
|
|
|
|
|
|
|
|
if (to >= subdev->size) {
|
|
|
|
to -= subdev->size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (to + len > subdev->size)
|
|
|
|
size = subdev->size - to;
|
|
|
|
else
|
|
|
|
size = len;
|
|
|
|
|
|
|
|
err = mtd_panic_write(subdev, to, size, &retsize, buf);
|
|
|
|
if (err == -EOPNOTSUPP) {
|
|
|
|
printk(KERN_ERR "mtdconcat: Cannot write from panic without panic_write\n");
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
|
|
|
|
*retlen += retsize;
|
|
|
|
len -= size;
|
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
err = -EINVAL;
|
|
|
|
buf += size;
|
|
|
|
to = 0;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static int
|
|
|
|
concat_write(struct mtd_info *mtd, loff_t to, size_t len,
|
|
|
|
size_t * retlen, const u_char * buf)
|
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
|
|
|
int err = -EINVAL;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
struct mtd_info *subdev = concat->subdev[i];
|
|
|
|
size_t size, retsize;
|
|
|
|
|
|
|
|
if (to >= subdev->size) {
|
|
|
|
size = 0;
|
|
|
|
to -= subdev->size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (to + len > subdev->size)
|
|
|
|
size = subdev->size - to;
|
|
|
|
else
|
|
|
|
size = len;
|
|
|
|
|
2012-02-03 16:13:23 +00:00
|
|
|
err = mtd_write(subdev, to, size, &retsize, buf);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
|
|
|
|
*retlen += retsize;
|
|
|
|
len -= size;
|
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
err = -EINVAL;
|
|
|
|
buf += size;
|
|
|
|
to = 0;
|
|
|
|
}
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2006-05-17 15:11:16 +00:00
|
|
|
static int
|
2006-05-23 14:06:03 +00:00
|
|
|
concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
|
|
|
|
unsigned long count, loff_t to, size_t * retlen)
|
2006-05-17 15:11:16 +00:00
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
|
|
|
struct kvec *vecs_copy;
|
|
|
|
unsigned long entry_low, entry_high;
|
|
|
|
size_t total_len = 0;
|
|
|
|
int i;
|
|
|
|
int err = -EINVAL;
|
|
|
|
|
|
|
|
/* Calculate total length of data */
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
total_len += vecs[i].iov_len;
|
|
|
|
|
|
|
|
/* Check alignment */
|
2006-05-22 21:18:05 +00:00
|
|
|
if (mtd->writesize > 1) {
|
2007-07-23 12:07:06 +00:00
|
|
|
uint64_t __to = to;
|
2006-05-22 21:18:05 +00:00
|
|
|
if (do_div(__to, mtd->writesize) || (total_len % mtd->writesize))
|
2006-05-17 15:11:16 +00:00
|
|
|
return -EINVAL;
|
2006-05-20 09:17:21 +00:00
|
|
|
}
|
2006-05-17 15:11:16 +00:00
|
|
|
|
|
|
|
/* make a copy of vecs */
|
2024-08-23 11:08:24 +00:00
|
|
|
vecs_copy = kmemdup_array(vecs, count, sizeof(struct kvec), GFP_KERNEL);
|
2006-05-17 15:11:16 +00:00
|
|
|
if (!vecs_copy)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
entry_low = 0;
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
struct mtd_info *subdev = concat->subdev[i];
|
|
|
|
size_t size, wsize, retsize, old_iov_len;
|
|
|
|
|
|
|
|
if (to >= subdev->size) {
|
|
|
|
to -= subdev->size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2008-12-10 13:37:21 +00:00
|
|
|
size = min_t(uint64_t, total_len, subdev->size - to);
|
2006-05-17 15:11:16 +00:00
|
|
|
wsize = size; /* store for future use */
|
|
|
|
|
|
|
|
entry_high = entry_low;
|
|
|
|
while (entry_high < count) {
|
|
|
|
if (size <= vecs_copy[entry_high].iov_len)
|
|
|
|
break;
|
|
|
|
size -= vecs_copy[entry_high++].iov_len;
|
|
|
|
}
|
|
|
|
|
|
|
|
old_iov_len = vecs_copy[entry_high].iov_len;
|
|
|
|
vecs_copy[entry_high].iov_len = size;
|
|
|
|
|
2012-02-03 16:13:23 +00:00
|
|
|
err = mtd_writev(subdev, &vecs_copy[entry_low],
|
|
|
|
entry_high - entry_low + 1, to, &retsize);
|
2006-05-17 15:11:16 +00:00
|
|
|
|
|
|
|
vecs_copy[entry_high].iov_len = old_iov_len - size;
|
|
|
|
vecs_copy[entry_high].iov_base += size;
|
|
|
|
|
|
|
|
entry_low = entry_high;
|
|
|
|
|
|
|
|
if (err)
|
|
|
|
break;
|
|
|
|
|
|
|
|
*retlen += retsize;
|
|
|
|
total_len -= wsize;
|
|
|
|
|
|
|
|
if (total_len == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
err = -EINVAL;
|
|
|
|
to = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
kfree(vecs_copy);
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static int
|
2006-05-29 01:26:58 +00:00
|
|
|
concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
2006-05-29 01:26:58 +00:00
|
|
|
struct mtd_oob_ops devops = *ops;
|
2006-05-29 22:37:34 +00:00
|
|
|
int i, err, ret = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-11-03 15:20:38 +00:00
|
|
|
ops->retlen = ops->oobretlen = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
struct mtd_info *subdev = concat->subdev[i];
|
|
|
|
|
|
|
|
if (from >= subdev->size) {
|
|
|
|
from -= subdev->size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-05-29 01:26:58 +00:00
|
|
|
/* partial read ? */
|
|
|
|
if (from + devops.len > subdev->size)
|
|
|
|
devops.len = subdev->size - from;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-12-23 16:27:05 +00:00
|
|
|
err = mtd_read_oob(subdev, from, &devops);
|
2006-05-29 01:26:58 +00:00
|
|
|
ops->retlen += devops.retlen;
|
2006-11-03 15:20:38 +00:00
|
|
|
ops->oobretlen += devops.oobretlen;
|
2006-05-29 22:37:34 +00:00
|
|
|
|
|
|
|
/* Save information about bitflips! */
|
|
|
|
if (unlikely(err)) {
|
2011-09-21 01:34:25 +00:00
|
|
|
if (mtd_is_eccerr(err)) {
|
2006-05-29 22:37:34 +00:00
|
|
|
mtd->ecc_stats.failed++;
|
|
|
|
ret = err;
|
2011-09-21 01:34:25 +00:00
|
|
|
} else if (mtd_is_bitflip(err)) {
|
2006-05-29 22:37:34 +00:00
|
|
|
mtd->ecc_stats.corrected++;
|
|
|
|
/* Do not overwrite -EBADMSG !! */
|
|
|
|
if (!ret)
|
|
|
|
ret = err;
|
|
|
|
} else
|
|
|
|
return err;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-11-03 15:20:38 +00:00
|
|
|
if (devops.datbuf) {
|
|
|
|
devops.len = ops->len - ops->retlen;
|
|
|
|
if (!devops.len)
|
|
|
|
return ret;
|
2006-05-29 01:26:58 +00:00
|
|
|
devops.datbuf += devops.retlen;
|
2006-11-03 15:20:38 +00:00
|
|
|
}
|
|
|
|
if (devops.oobbuf) {
|
|
|
|
devops.ooblen = ops->ooblen - ops->oobretlen;
|
|
|
|
if (!devops.ooblen)
|
|
|
|
return ret;
|
|
|
|
devops.oobbuf += ops->oobretlen;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
from = 0;
|
|
|
|
}
|
2006-05-29 01:26:58 +00:00
|
|
|
return -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2006-05-29 01:26:58 +00:00
|
|
|
concat_write_oob(struct mtd_info *mtd, loff_t to, struct mtd_oob_ops *ops)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
2006-05-29 01:26:58 +00:00
|
|
|
struct mtd_oob_ops devops = *ops;
|
|
|
|
int i, err;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (!(mtd->flags & MTD_WRITEABLE))
|
|
|
|
return -EROFS;
|
|
|
|
|
2011-04-24 22:57:12 +00:00
|
|
|
ops->retlen = ops->oobretlen = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
struct mtd_info *subdev = concat->subdev[i];
|
|
|
|
|
|
|
|
if (to >= subdev->size) {
|
|
|
|
to -= subdev->size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2006-05-29 01:26:58 +00:00
|
|
|
/* partial write ? */
|
|
|
|
if (to + devops.len > subdev->size)
|
|
|
|
devops.len = subdev->size - to;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2011-12-23 16:29:55 +00:00
|
|
|
err = mtd_write_oob(subdev, to, &devops);
|
2015-01-20 09:35:32 +00:00
|
|
|
ops->retlen += devops.retlen;
|
|
|
|
ops->oobretlen += devops.oobretlen;
|
2005-04-16 22:20:36 +00:00
|
|
|
if (err)
|
2006-05-29 01:26:58 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-11-03 15:20:38 +00:00
|
|
|
if (devops.datbuf) {
|
|
|
|
devops.len = ops->len - ops->retlen;
|
|
|
|
if (!devops.len)
|
|
|
|
return 0;
|
2006-05-29 01:26:58 +00:00
|
|
|
devops.datbuf += devops.retlen;
|
2006-11-03 15:20:38 +00:00
|
|
|
}
|
|
|
|
if (devops.oobbuf) {
|
|
|
|
devops.ooblen = ops->ooblen - ops->oobretlen;
|
|
|
|
if (!devops.ooblen)
|
|
|
|
return 0;
|
|
|
|
devops.oobbuf += devops.oobretlen;
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
to = 0;
|
|
|
|
}
|
2006-05-29 01:26:58 +00:00
|
|
|
return -EINVAL;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int concat_erase(struct mtd_info *mtd, struct erase_info *instr)
|
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
|
|
|
struct mtd_info *subdev;
|
|
|
|
int i, err;
|
2008-12-10 13:37:21 +00:00
|
|
|
uint64_t length, offset = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
struct erase_info *erase;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Check for proper erase block alignment of the to-be-erased area.
|
|
|
|
* It is easier to do this based on the super device's erase
|
|
|
|
* region info rather than looking at each particular sub-device
|
|
|
|
* in turn.
|
|
|
|
*/
|
|
|
|
if (!concat->mtd.numeraseregions) {
|
|
|
|
/* the easy case: device has uniform erase block size */
|
|
|
|
if (instr->addr & (concat->mtd.erasesize - 1))
|
|
|
|
return -EINVAL;
|
|
|
|
if (instr->len & (concat->mtd.erasesize - 1))
|
|
|
|
return -EINVAL;
|
|
|
|
} else {
|
|
|
|
/* device has variable erase size */
|
|
|
|
struct mtd_erase_region_info *erase_regions =
|
|
|
|
concat->mtd.eraseregions;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Find the erase region where the to-be-erased area begins:
|
|
|
|
*/
|
|
|
|
for (i = 0; i < concat->mtd.numeraseregions &&
|
|
|
|
instr->addr >= erase_regions[i].offset; i++) ;
|
|
|
|
--i;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now erase_regions[i] is the region in which the
|
|
|
|
* to-be-erased area begins. Verify that the starting
|
|
|
|
* offset is aligned to this region's erase size:
|
|
|
|
*/
|
2009-09-18 19:51:49 +00:00
|
|
|
if (i < 0 || instr->addr & (erase_regions[i].erasesize - 1))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* now find the erase region where the to-be-erased area ends:
|
|
|
|
*/
|
|
|
|
for (; i < concat->mtd.numeraseregions &&
|
|
|
|
(instr->addr + instr->len) >= erase_regions[i].offset;
|
|
|
|
++i) ;
|
|
|
|
--i;
|
|
|
|
/*
|
|
|
|
* check if the ending offset is aligned to this region's erase size
|
|
|
|
*/
|
2009-09-18 19:51:49 +00:00
|
|
|
if (i < 0 || ((instr->addr + instr->len) &
|
|
|
|
(erase_regions[i].erasesize - 1)))
|
2005-04-16 22:20:36 +00:00
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* make a local copy of instr to avoid modifying the caller's struct */
|
|
|
|
erase = kmalloc(sizeof (struct erase_info), GFP_KERNEL);
|
|
|
|
|
|
|
|
if (!erase)
|
|
|
|
return -ENOMEM;
|
|
|
|
|
|
|
|
*erase = *instr;
|
|
|
|
length = instr->len;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* find the subdevice where the to-be-erased area begins, adjust
|
|
|
|
* starting offset to be relative to the subdevice start
|
|
|
|
*/
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
subdev = concat->subdev[i];
|
|
|
|
if (subdev->size <= erase->addr) {
|
|
|
|
erase->addr -= subdev->size;
|
|
|
|
offset += subdev->size;
|
|
|
|
} else {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* must never happen since size limit has been verified above */
|
2006-03-26 16:15:12 +00:00
|
|
|
BUG_ON(i >= concat->num_subdev);
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/* now do the erase: */
|
|
|
|
err = 0;
|
|
|
|
for (; length > 0; i++) {
|
|
|
|
/* loop for all subdevices affected by this request */
|
|
|
|
subdev = concat->subdev[i]; /* get current subdevice */
|
|
|
|
|
|
|
|
/* limit length to subdevice's size: */
|
|
|
|
if (erase->addr + length > subdev->size)
|
|
|
|
erase->len = subdev->size - erase->addr;
|
|
|
|
else
|
|
|
|
erase->len = length;
|
|
|
|
|
|
|
|
length -= erase->len;
|
2018-02-12 21:03:09 +00:00
|
|
|
if ((err = mtd_erase(subdev, erase))) {
|
2005-04-16 22:20:36 +00:00
|
|
|
/* sanity check: should never happen since
|
|
|
|
* block alignment has been checked above */
|
2006-03-26 16:15:12 +00:00
|
|
|
BUG_ON(err == -EINVAL);
|
2008-08-12 09:40:50 +00:00
|
|
|
if (erase->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
|
2005-04-16 22:20:36 +00:00
|
|
|
instr->fail_addr = erase->fail_addr + offset;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* erase->addr specifies the offset of the area to be
|
|
|
|
* erased *within the current subdevice*. It can be
|
|
|
|
* non-zero only the first time through this loop, i.e.
|
|
|
|
* for the first subdevice where blocks need to be erased.
|
|
|
|
* All the following erases must begin at the start of the
|
|
|
|
* current subdevice, i.e. at offset zero.
|
|
|
|
*/
|
|
|
|
erase->addr = 0;
|
|
|
|
offset += subdev->size;
|
|
|
|
}
|
|
|
|
kfree(erase);
|
|
|
|
|
2018-02-12 21:03:09 +00:00
|
|
|
return err;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 23:19:47 +00:00
|
|
|
static int concat_xxlock(struct mtd_info *mtd, loff_t ofs, uint64_t len,
|
|
|
|
bool is_lock)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
|
|
|
int i, err = -EINVAL;
|
|
|
|
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
struct mtd_info *subdev = concat->subdev[i];
|
2008-12-10 13:37:21 +00:00
|
|
|
uint64_t size;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
if (ofs >= subdev->size) {
|
|
|
|
size = 0;
|
|
|
|
ofs -= subdev->size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (ofs + len > subdev->size)
|
|
|
|
size = subdev->size - ofs;
|
|
|
|
else
|
|
|
|
size = len;
|
|
|
|
|
2019-05-22 23:19:47 +00:00
|
|
|
if (is_lock)
|
|
|
|
err = mtd_lock(subdev, ofs, size);
|
|
|
|
else
|
|
|
|
err = mtd_unlock(subdev, ofs, size);
|
2011-12-30 15:00:35 +00:00
|
|
|
if (err)
|
|
|
|
break;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
len -= size;
|
|
|
|
if (len == 0)
|
|
|
|
break;
|
|
|
|
|
|
|
|
err = -EINVAL;
|
|
|
|
ofs = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2019-05-22 23:19:47 +00:00
|
|
|
static int concat_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
2005-04-16 22:20:36 +00:00
|
|
|
{
|
2019-05-22 23:19:47 +00:00
|
|
|
return concat_xxlock(mtd, ofs, len, true);
|
|
|
|
}
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2019-05-22 23:19:47 +00:00
|
|
|
static int concat_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
|
|
|
{
|
|
|
|
return concat_xxlock(mtd, ofs, len, false);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
2019-05-22 23:19:48 +00:00
|
|
|
static int concat_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
|
|
|
int i, err = -EINVAL;
|
|
|
|
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
struct mtd_info *subdev = concat->subdev[i];
|
|
|
|
|
|
|
|
if (ofs >= subdev->size) {
|
|
|
|
ofs -= subdev->size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ofs + len > subdev->size)
|
|
|
|
break;
|
|
|
|
|
|
|
|
return mtd_is_locked(subdev, ofs, len);
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
static void concat_sync(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
struct mtd_info *subdev = concat->subdev[i];
|
2011-12-23 17:03:12 +00:00
|
|
|
mtd_sync(subdev);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int concat_suspend(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
|
|
|
int i, rc = 0;
|
|
|
|
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
struct mtd_info *subdev = concat->subdev[i];
|
2011-12-23 17:25:16 +00:00
|
|
|
if ((rc = mtd_suspend(subdev)) < 0)
|
2005-04-16 22:20:36 +00:00
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
return rc;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void concat_resume(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
struct mtd_info *subdev = concat->subdev[i];
|
2011-12-23 17:31:25 +00:00
|
|
|
mtd_resume(subdev);
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-05-17 15:11:16 +00:00
|
|
|
static int concat_block_isbad(struct mtd_info *mtd, loff_t ofs)
|
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
|
|
|
int i, res = 0;
|
|
|
|
|
2012-01-02 11:48:54 +00:00
|
|
|
if (!mtd_can_have_bb(concat->subdev[0]))
|
2006-05-17 15:11:16 +00:00
|
|
|
return res;
|
|
|
|
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
struct mtd_info *subdev = concat->subdev[i];
|
|
|
|
|
|
|
|
if (ofs >= subdev->size) {
|
|
|
|
ofs -= subdev->size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-12-23 17:35:30 +00:00
|
|
|
res = mtd_block_isbad(subdev, ofs);
|
2006-05-17 15:11:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int concat_block_markbad(struct mtd_info *mtd, loff_t ofs)
|
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
|
|
|
int i, err = -EINVAL;
|
|
|
|
|
|
|
|
for (i = 0; i < concat->num_subdev; i++) {
|
|
|
|
struct mtd_info *subdev = concat->subdev[i];
|
|
|
|
|
|
|
|
if (ofs >= subdev->size) {
|
|
|
|
ofs -= subdev->size;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2011-12-23 17:37:38 +00:00
|
|
|
err = mtd_block_markbad(subdev, ofs);
|
2006-05-29 22:37:34 +00:00
|
|
|
if (!err)
|
|
|
|
mtd->ecc_stats.badblocks++;
|
2006-05-17 15:11:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* This function constructs a virtual MTD device by concatenating
|
|
|
|
* num_devs MTD devices. A pointer to the new device object is
|
|
|
|
* stored to *new_dev upon success. This function does _not_
|
|
|
|
* register any devices: this is the caller's responsibility.
|
|
|
|
*/
|
|
|
|
struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to concatenate */
|
|
|
|
int num_devs, /* number of subdevices */
|
2008-12-23 10:00:14 +00:00
|
|
|
const char *name)
|
2005-04-16 22:20:36 +00:00
|
|
|
{ /* name for the new device */
|
|
|
|
int i;
|
|
|
|
size_t size;
|
|
|
|
struct mtd_concat *concat;
|
mtd: mtdconcat: Judge callback existence based on the master
Since commit 46b5889cc2c5("mtd: implement proper partition handling")
applied, mtd partition device won't hold some callback functions, such
as _block_isbad, _block_markbad, etc. Besides, function mtd_block_isbad()
will get mtd device's master mtd device, then invokes master mtd device's
callback function. So, following process may result mtd_block_isbad()
always return 0, even though mtd device has bad blocks:
1. Split a mtd device into 3 partitions: PA, PB, PC
[ Each mtd partition device won't has callback function _block_isbad(). ]
2. Concatenate PA and PB as a new mtd device PN
[ mtd_concat_create() finds out each subdev has no callback function
_block_isbad(), so PN won't be assigned callback function
concat_block_isbad(). ]
Then, mtd_block_isbad() checks "!master->_block_isbad" is true, will
always return 0.
Reproducer:
// reproduce.c
static int __init init_diy_module(void)
{
struct mtd_info *mtd[2];
struct mtd_info *mtd_combine = NULL;
mtd[0] = get_mtd_device_nm("NAND simulator partition 0");
if (!mtd[0]) {
pr_err("cannot find mtd1\n");
return -EINVAL;
}
mtd[1] = get_mtd_device_nm("NAND simulator partition 1");
if (!mtd[1]) {
pr_err("cannot find mtd2\n");
return -EINVAL;
}
put_mtd_device(mtd[0]);
put_mtd_device(mtd[1]);
mtd_combine = mtd_concat_create(mtd, 2, "Combine mtd");
if (mtd_combine == NULL) {
pr_err("combine failed\n");
return -EINVAL;
}
mtd_device_register(mtd_combine, NULL, 0);
pr_info("Combine success\n");
return 0;
}
1. ID="0x20,0xac,0x00,0x15"
2. modprobe nandsim id_bytes=$ID parts=50,100 badblocks=100
3. insmod reproduce.ko
4. flash_erase /dev/mtd3 0 0
libmtd: error!: MEMERASE64 ioctl failed for eraseblock 100 (mtd3)
error 5 (Input/output error)
// Should be "flash_erase: Skipping bad block at 00c80000"
Fixes: 46b5889cc2c54bac ("mtd: implement proper partition handling")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20210817114857.2784825-2-chengzhihao1@huawei.com
2021-08-17 11:48:56 +00:00
|
|
|
struct mtd_info *subdev_master = NULL;
|
2008-12-10 14:08:12 +00:00
|
|
|
uint32_t max_erasesize, curr_erasesize;
|
2005-04-16 22:20:36 +00:00
|
|
|
int num_erase_region;
|
2011-01-24 16:45:42 +00:00
|
|
|
int max_writebufsize = 0;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
printk(KERN_NOTICE "Concatenating MTD devices:\n");
|
|
|
|
for (i = 0; i < num_devs; i++)
|
|
|
|
printk(KERN_NOTICE "(%d): \"%s\"\n", i, subdev[i]->name);
|
|
|
|
printk(KERN_NOTICE "into device \"%s\"\n", name);
|
|
|
|
|
|
|
|
/* allocate the device structure */
|
|
|
|
size = SIZEOF_STRUCT_MTD_CONCAT(num_devs);
|
2006-11-15 19:10:29 +00:00
|
|
|
concat = kzalloc(size, GFP_KERNEL);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!concat) {
|
|
|
|
printk
|
|
|
|
("memory allocation error while creating concatenated device \"%s\"\n",
|
|
|
|
name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
concat->subdev = (struct mtd_info **) (concat + 1);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Set up the new "super" device's MTD object structure, check for
|
2011-07-20 16:53:42 +00:00
|
|
|
* incompatibilities between the subdevices.
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
concat->mtd.type = subdev[0]->type;
|
|
|
|
concat->mtd.flags = subdev[0]->flags;
|
|
|
|
concat->mtd.size = subdev[0]->size;
|
|
|
|
concat->mtd.erasesize = subdev[0]->erasesize;
|
2006-05-22 21:18:05 +00:00
|
|
|
concat->mtd.writesize = subdev[0]->writesize;
|
2011-01-24 16:45:42 +00:00
|
|
|
|
|
|
|
for (i = 0; i < num_devs; i++)
|
|
|
|
if (max_writebufsize < subdev[i]->writebufsize)
|
|
|
|
max_writebufsize = subdev[i]->writebufsize;
|
|
|
|
concat->mtd.writebufsize = max_writebufsize;
|
|
|
|
|
2007-10-12 09:54:06 +00:00
|
|
|
concat->mtd.subpage_sft = subdev[0]->subpage_sft;
|
2005-04-16 22:20:36 +00:00
|
|
|
concat->mtd.oobsize = subdev[0]->oobsize;
|
2007-03-06 13:56:34 +00:00
|
|
|
concat->mtd.oobavail = subdev[0]->oobavail;
|
mtd: mtdconcat: Judge callback existence based on the master
Since commit 46b5889cc2c5("mtd: implement proper partition handling")
applied, mtd partition device won't hold some callback functions, such
as _block_isbad, _block_markbad, etc. Besides, function mtd_block_isbad()
will get mtd device's master mtd device, then invokes master mtd device's
callback function. So, following process may result mtd_block_isbad()
always return 0, even though mtd device has bad blocks:
1. Split a mtd device into 3 partitions: PA, PB, PC
[ Each mtd partition device won't has callback function _block_isbad(). ]
2. Concatenate PA and PB as a new mtd device PN
[ mtd_concat_create() finds out each subdev has no callback function
_block_isbad(), so PN won't be assigned callback function
concat_block_isbad(). ]
Then, mtd_block_isbad() checks "!master->_block_isbad" is true, will
always return 0.
Reproducer:
// reproduce.c
static int __init init_diy_module(void)
{
struct mtd_info *mtd[2];
struct mtd_info *mtd_combine = NULL;
mtd[0] = get_mtd_device_nm("NAND simulator partition 0");
if (!mtd[0]) {
pr_err("cannot find mtd1\n");
return -EINVAL;
}
mtd[1] = get_mtd_device_nm("NAND simulator partition 1");
if (!mtd[1]) {
pr_err("cannot find mtd2\n");
return -EINVAL;
}
put_mtd_device(mtd[0]);
put_mtd_device(mtd[1]);
mtd_combine = mtd_concat_create(mtd, 2, "Combine mtd");
if (mtd_combine == NULL) {
pr_err("combine failed\n");
return -EINVAL;
}
mtd_device_register(mtd_combine, NULL, 0);
pr_info("Combine success\n");
return 0;
}
1. ID="0x20,0xac,0x00,0x15"
2. modprobe nandsim id_bytes=$ID parts=50,100 badblocks=100
3. insmod reproduce.ko
4. flash_erase /dev/mtd3 0 0
libmtd: error!: MEMERASE64 ioctl failed for eraseblock 100 (mtd3)
error 5 (Input/output error)
// Should be "flash_erase: Skipping bad block at 00c80000"
Fixes: 46b5889cc2c54bac ("mtd: implement proper partition handling")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20210817114857.2784825-2-chengzhihao1@huawei.com
2021-08-17 11:48:56 +00:00
|
|
|
|
|
|
|
subdev_master = mtd_get_master(subdev[0]);
|
|
|
|
if (subdev_master->_writev)
|
2012-01-30 12:58:32 +00:00
|
|
|
concat->mtd._writev = concat_writev;
|
mtd: mtdconcat: Judge callback existence based on the master
Since commit 46b5889cc2c5("mtd: implement proper partition handling")
applied, mtd partition device won't hold some callback functions, such
as _block_isbad, _block_markbad, etc. Besides, function mtd_block_isbad()
will get mtd device's master mtd device, then invokes master mtd device's
callback function. So, following process may result mtd_block_isbad()
always return 0, even though mtd device has bad blocks:
1. Split a mtd device into 3 partitions: PA, PB, PC
[ Each mtd partition device won't has callback function _block_isbad(). ]
2. Concatenate PA and PB as a new mtd device PN
[ mtd_concat_create() finds out each subdev has no callback function
_block_isbad(), so PN won't be assigned callback function
concat_block_isbad(). ]
Then, mtd_block_isbad() checks "!master->_block_isbad" is true, will
always return 0.
Reproducer:
// reproduce.c
static int __init init_diy_module(void)
{
struct mtd_info *mtd[2];
struct mtd_info *mtd_combine = NULL;
mtd[0] = get_mtd_device_nm("NAND simulator partition 0");
if (!mtd[0]) {
pr_err("cannot find mtd1\n");
return -EINVAL;
}
mtd[1] = get_mtd_device_nm("NAND simulator partition 1");
if (!mtd[1]) {
pr_err("cannot find mtd2\n");
return -EINVAL;
}
put_mtd_device(mtd[0]);
put_mtd_device(mtd[1]);
mtd_combine = mtd_concat_create(mtd, 2, "Combine mtd");
if (mtd_combine == NULL) {
pr_err("combine failed\n");
return -EINVAL;
}
mtd_device_register(mtd_combine, NULL, 0);
pr_info("Combine success\n");
return 0;
}
1. ID="0x20,0xac,0x00,0x15"
2. modprobe nandsim id_bytes=$ID parts=50,100 badblocks=100
3. insmod reproduce.ko
4. flash_erase /dev/mtd3 0 0
libmtd: error!: MEMERASE64 ioctl failed for eraseblock 100 (mtd3)
error 5 (Input/output error)
// Should be "flash_erase: Skipping bad block at 00c80000"
Fixes: 46b5889cc2c54bac ("mtd: implement proper partition handling")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20210817114857.2784825-2-chengzhihao1@huawei.com
2021-08-17 11:48:56 +00:00
|
|
|
if (subdev_master->_read_oob)
|
2012-01-30 12:58:32 +00:00
|
|
|
concat->mtd._read_oob = concat_read_oob;
|
mtd: mtdconcat: Judge callback existence based on the master
Since commit 46b5889cc2c5("mtd: implement proper partition handling")
applied, mtd partition device won't hold some callback functions, such
as _block_isbad, _block_markbad, etc. Besides, function mtd_block_isbad()
will get mtd device's master mtd device, then invokes master mtd device's
callback function. So, following process may result mtd_block_isbad()
always return 0, even though mtd device has bad blocks:
1. Split a mtd device into 3 partitions: PA, PB, PC
[ Each mtd partition device won't has callback function _block_isbad(). ]
2. Concatenate PA and PB as a new mtd device PN
[ mtd_concat_create() finds out each subdev has no callback function
_block_isbad(), so PN won't be assigned callback function
concat_block_isbad(). ]
Then, mtd_block_isbad() checks "!master->_block_isbad" is true, will
always return 0.
Reproducer:
// reproduce.c
static int __init init_diy_module(void)
{
struct mtd_info *mtd[2];
struct mtd_info *mtd_combine = NULL;
mtd[0] = get_mtd_device_nm("NAND simulator partition 0");
if (!mtd[0]) {
pr_err("cannot find mtd1\n");
return -EINVAL;
}
mtd[1] = get_mtd_device_nm("NAND simulator partition 1");
if (!mtd[1]) {
pr_err("cannot find mtd2\n");
return -EINVAL;
}
put_mtd_device(mtd[0]);
put_mtd_device(mtd[1]);
mtd_combine = mtd_concat_create(mtd, 2, "Combine mtd");
if (mtd_combine == NULL) {
pr_err("combine failed\n");
return -EINVAL;
}
mtd_device_register(mtd_combine, NULL, 0);
pr_info("Combine success\n");
return 0;
}
1. ID="0x20,0xac,0x00,0x15"
2. modprobe nandsim id_bytes=$ID parts=50,100 badblocks=100
3. insmod reproduce.ko
4. flash_erase /dev/mtd3 0 0
libmtd: error!: MEMERASE64 ioctl failed for eraseblock 100 (mtd3)
error 5 (Input/output error)
// Should be "flash_erase: Skipping bad block at 00c80000"
Fixes: 46b5889cc2c54bac ("mtd: implement proper partition handling")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20210817114857.2784825-2-chengzhihao1@huawei.com
2021-08-17 11:48:56 +00:00
|
|
|
if (subdev_master->_write_oob)
|
2012-01-30 12:58:32 +00:00
|
|
|
concat->mtd._write_oob = concat_write_oob;
|
mtd: mtdconcat: Judge callback existence based on the master
Since commit 46b5889cc2c5("mtd: implement proper partition handling")
applied, mtd partition device won't hold some callback functions, such
as _block_isbad, _block_markbad, etc. Besides, function mtd_block_isbad()
will get mtd device's master mtd device, then invokes master mtd device's
callback function. So, following process may result mtd_block_isbad()
always return 0, even though mtd device has bad blocks:
1. Split a mtd device into 3 partitions: PA, PB, PC
[ Each mtd partition device won't has callback function _block_isbad(). ]
2. Concatenate PA and PB as a new mtd device PN
[ mtd_concat_create() finds out each subdev has no callback function
_block_isbad(), so PN won't be assigned callback function
concat_block_isbad(). ]
Then, mtd_block_isbad() checks "!master->_block_isbad" is true, will
always return 0.
Reproducer:
// reproduce.c
static int __init init_diy_module(void)
{
struct mtd_info *mtd[2];
struct mtd_info *mtd_combine = NULL;
mtd[0] = get_mtd_device_nm("NAND simulator partition 0");
if (!mtd[0]) {
pr_err("cannot find mtd1\n");
return -EINVAL;
}
mtd[1] = get_mtd_device_nm("NAND simulator partition 1");
if (!mtd[1]) {
pr_err("cannot find mtd2\n");
return -EINVAL;
}
put_mtd_device(mtd[0]);
put_mtd_device(mtd[1]);
mtd_combine = mtd_concat_create(mtd, 2, "Combine mtd");
if (mtd_combine == NULL) {
pr_err("combine failed\n");
return -EINVAL;
}
mtd_device_register(mtd_combine, NULL, 0);
pr_info("Combine success\n");
return 0;
}
1. ID="0x20,0xac,0x00,0x15"
2. modprobe nandsim id_bytes=$ID parts=50,100 badblocks=100
3. insmod reproduce.ko
4. flash_erase /dev/mtd3 0 0
libmtd: error!: MEMERASE64 ioctl failed for eraseblock 100 (mtd3)
error 5 (Input/output error)
// Should be "flash_erase: Skipping bad block at 00c80000"
Fixes: 46b5889cc2c54bac ("mtd: implement proper partition handling")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20210817114857.2784825-2-chengzhihao1@huawei.com
2021-08-17 11:48:56 +00:00
|
|
|
if (subdev_master->_block_isbad)
|
2012-01-30 12:58:32 +00:00
|
|
|
concat->mtd._block_isbad = concat_block_isbad;
|
mtd: mtdconcat: Judge callback existence based on the master
Since commit 46b5889cc2c5("mtd: implement proper partition handling")
applied, mtd partition device won't hold some callback functions, such
as _block_isbad, _block_markbad, etc. Besides, function mtd_block_isbad()
will get mtd device's master mtd device, then invokes master mtd device's
callback function. So, following process may result mtd_block_isbad()
always return 0, even though mtd device has bad blocks:
1. Split a mtd device into 3 partitions: PA, PB, PC
[ Each mtd partition device won't has callback function _block_isbad(). ]
2. Concatenate PA and PB as a new mtd device PN
[ mtd_concat_create() finds out each subdev has no callback function
_block_isbad(), so PN won't be assigned callback function
concat_block_isbad(). ]
Then, mtd_block_isbad() checks "!master->_block_isbad" is true, will
always return 0.
Reproducer:
// reproduce.c
static int __init init_diy_module(void)
{
struct mtd_info *mtd[2];
struct mtd_info *mtd_combine = NULL;
mtd[0] = get_mtd_device_nm("NAND simulator partition 0");
if (!mtd[0]) {
pr_err("cannot find mtd1\n");
return -EINVAL;
}
mtd[1] = get_mtd_device_nm("NAND simulator partition 1");
if (!mtd[1]) {
pr_err("cannot find mtd2\n");
return -EINVAL;
}
put_mtd_device(mtd[0]);
put_mtd_device(mtd[1]);
mtd_combine = mtd_concat_create(mtd, 2, "Combine mtd");
if (mtd_combine == NULL) {
pr_err("combine failed\n");
return -EINVAL;
}
mtd_device_register(mtd_combine, NULL, 0);
pr_info("Combine success\n");
return 0;
}
1. ID="0x20,0xac,0x00,0x15"
2. modprobe nandsim id_bytes=$ID parts=50,100 badblocks=100
3. insmod reproduce.ko
4. flash_erase /dev/mtd3 0 0
libmtd: error!: MEMERASE64 ioctl failed for eraseblock 100 (mtd3)
error 5 (Input/output error)
// Should be "flash_erase: Skipping bad block at 00c80000"
Fixes: 46b5889cc2c54bac ("mtd: implement proper partition handling")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20210817114857.2784825-2-chengzhihao1@huawei.com
2021-08-17 11:48:56 +00:00
|
|
|
if (subdev_master->_block_markbad)
|
2012-01-30 12:58:32 +00:00
|
|
|
concat->mtd._block_markbad = concat_block_markbad;
|
mtd: mtdconcat: Judge callback existence based on the master
Since commit 46b5889cc2c5("mtd: implement proper partition handling")
applied, mtd partition device won't hold some callback functions, such
as _block_isbad, _block_markbad, etc. Besides, function mtd_block_isbad()
will get mtd device's master mtd device, then invokes master mtd device's
callback function. So, following process may result mtd_block_isbad()
always return 0, even though mtd device has bad blocks:
1. Split a mtd device into 3 partitions: PA, PB, PC
[ Each mtd partition device won't has callback function _block_isbad(). ]
2. Concatenate PA and PB as a new mtd device PN
[ mtd_concat_create() finds out each subdev has no callback function
_block_isbad(), so PN won't be assigned callback function
concat_block_isbad(). ]
Then, mtd_block_isbad() checks "!master->_block_isbad" is true, will
always return 0.
Reproducer:
// reproduce.c
static int __init init_diy_module(void)
{
struct mtd_info *mtd[2];
struct mtd_info *mtd_combine = NULL;
mtd[0] = get_mtd_device_nm("NAND simulator partition 0");
if (!mtd[0]) {
pr_err("cannot find mtd1\n");
return -EINVAL;
}
mtd[1] = get_mtd_device_nm("NAND simulator partition 1");
if (!mtd[1]) {
pr_err("cannot find mtd2\n");
return -EINVAL;
}
put_mtd_device(mtd[0]);
put_mtd_device(mtd[1]);
mtd_combine = mtd_concat_create(mtd, 2, "Combine mtd");
if (mtd_combine == NULL) {
pr_err("combine failed\n");
return -EINVAL;
}
mtd_device_register(mtd_combine, NULL, 0);
pr_info("Combine success\n");
return 0;
}
1. ID="0x20,0xac,0x00,0x15"
2. modprobe nandsim id_bytes=$ID parts=50,100 badblocks=100
3. insmod reproduce.ko
4. flash_erase /dev/mtd3 0 0
libmtd: error!: MEMERASE64 ioctl failed for eraseblock 100 (mtd3)
error 5 (Input/output error)
// Should be "flash_erase: Skipping bad block at 00c80000"
Fixes: 46b5889cc2c54bac ("mtd: implement proper partition handling")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20210817114857.2784825-2-chengzhihao1@huawei.com
2021-08-17 11:48:56 +00:00
|
|
|
if (subdev_master->_panic_write)
|
2020-06-02 14:34:03 +00:00
|
|
|
concat->mtd._panic_write = concat_panic_write;
|
2021-08-17 11:48:57 +00:00
|
|
|
if (subdev_master->_read)
|
|
|
|
concat->mtd._read = concat_read;
|
|
|
|
if (subdev_master->_write)
|
|
|
|
concat->mtd._write = concat_write;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
2006-05-29 22:37:34 +00:00
|
|
|
concat->mtd.ecc_stats.badblocks = subdev[0]->ecc_stats.badblocks;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
concat->subdev[0] = subdev[0];
|
|
|
|
|
|
|
|
for (i = 1; i < num_devs; i++) {
|
|
|
|
if (concat->mtd.type != subdev[i]->type) {
|
|
|
|
kfree(concat);
|
|
|
|
printk("Incompatible device type on \"%s\"\n",
|
|
|
|
subdev[i]->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (concat->mtd.flags != subdev[i]->flags) {
|
|
|
|
/*
|
|
|
|
* Expect all flags except MTD_WRITEABLE to be
|
|
|
|
* equal on all subdevices.
|
|
|
|
*/
|
|
|
|
if ((concat->mtd.flags ^ subdev[i]->
|
|
|
|
flags) & ~MTD_WRITEABLE) {
|
|
|
|
kfree(concat);
|
|
|
|
printk("Incompatible device flags on \"%s\"\n",
|
|
|
|
subdev[i]->name);
|
|
|
|
return NULL;
|
|
|
|
} else
|
|
|
|
/* if writeable attribute differs,
|
|
|
|
make super device writeable */
|
|
|
|
concat->mtd.flags |=
|
|
|
|
subdev[i]->flags & MTD_WRITEABLE;
|
|
|
|
}
|
2009-02-12 10:40:05 +00:00
|
|
|
|
mtd: mtdconcat: Judge callback existence based on the master
Since commit 46b5889cc2c5("mtd: implement proper partition handling")
applied, mtd partition device won't hold some callback functions, such
as _block_isbad, _block_markbad, etc. Besides, function mtd_block_isbad()
will get mtd device's master mtd device, then invokes master mtd device's
callback function. So, following process may result mtd_block_isbad()
always return 0, even though mtd device has bad blocks:
1. Split a mtd device into 3 partitions: PA, PB, PC
[ Each mtd partition device won't has callback function _block_isbad(). ]
2. Concatenate PA and PB as a new mtd device PN
[ mtd_concat_create() finds out each subdev has no callback function
_block_isbad(), so PN won't be assigned callback function
concat_block_isbad(). ]
Then, mtd_block_isbad() checks "!master->_block_isbad" is true, will
always return 0.
Reproducer:
// reproduce.c
static int __init init_diy_module(void)
{
struct mtd_info *mtd[2];
struct mtd_info *mtd_combine = NULL;
mtd[0] = get_mtd_device_nm("NAND simulator partition 0");
if (!mtd[0]) {
pr_err("cannot find mtd1\n");
return -EINVAL;
}
mtd[1] = get_mtd_device_nm("NAND simulator partition 1");
if (!mtd[1]) {
pr_err("cannot find mtd2\n");
return -EINVAL;
}
put_mtd_device(mtd[0]);
put_mtd_device(mtd[1]);
mtd_combine = mtd_concat_create(mtd, 2, "Combine mtd");
if (mtd_combine == NULL) {
pr_err("combine failed\n");
return -EINVAL;
}
mtd_device_register(mtd_combine, NULL, 0);
pr_info("Combine success\n");
return 0;
}
1. ID="0x20,0xac,0x00,0x15"
2. modprobe nandsim id_bytes=$ID parts=50,100 badblocks=100
3. insmod reproduce.ko
4. flash_erase /dev/mtd3 0 0
libmtd: error!: MEMERASE64 ioctl failed for eraseblock 100 (mtd3)
error 5 (Input/output error)
// Should be "flash_erase: Skipping bad block at 00c80000"
Fixes: 46b5889cc2c54bac ("mtd: implement proper partition handling")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20210817114857.2784825-2-chengzhihao1@huawei.com
2021-08-17 11:48:56 +00:00
|
|
|
subdev_master = mtd_get_master(subdev[i]);
|
2005-04-16 22:20:36 +00:00
|
|
|
concat->mtd.size += subdev[i]->size;
|
2006-05-29 22:37:34 +00:00
|
|
|
concat->mtd.ecc_stats.badblocks +=
|
|
|
|
subdev[i]->ecc_stats.badblocks;
|
2006-05-22 21:18:05 +00:00
|
|
|
if (concat->mtd.writesize != subdev[i]->writesize ||
|
2006-09-28 13:38:36 +00:00
|
|
|
concat->mtd.subpage_sft != subdev[i]->subpage_sft ||
|
2005-04-16 22:20:36 +00:00
|
|
|
concat->mtd.oobsize != subdev[i]->oobsize ||
|
mtd: mtdconcat: Judge callback existence based on the master
Since commit 46b5889cc2c5("mtd: implement proper partition handling")
applied, mtd partition device won't hold some callback functions, such
as _block_isbad, _block_markbad, etc. Besides, function mtd_block_isbad()
will get mtd device's master mtd device, then invokes master mtd device's
callback function. So, following process may result mtd_block_isbad()
always return 0, even though mtd device has bad blocks:
1. Split a mtd device into 3 partitions: PA, PB, PC
[ Each mtd partition device won't has callback function _block_isbad(). ]
2. Concatenate PA and PB as a new mtd device PN
[ mtd_concat_create() finds out each subdev has no callback function
_block_isbad(), so PN won't be assigned callback function
concat_block_isbad(). ]
Then, mtd_block_isbad() checks "!master->_block_isbad" is true, will
always return 0.
Reproducer:
// reproduce.c
static int __init init_diy_module(void)
{
struct mtd_info *mtd[2];
struct mtd_info *mtd_combine = NULL;
mtd[0] = get_mtd_device_nm("NAND simulator partition 0");
if (!mtd[0]) {
pr_err("cannot find mtd1\n");
return -EINVAL;
}
mtd[1] = get_mtd_device_nm("NAND simulator partition 1");
if (!mtd[1]) {
pr_err("cannot find mtd2\n");
return -EINVAL;
}
put_mtd_device(mtd[0]);
put_mtd_device(mtd[1]);
mtd_combine = mtd_concat_create(mtd, 2, "Combine mtd");
if (mtd_combine == NULL) {
pr_err("combine failed\n");
return -EINVAL;
}
mtd_device_register(mtd_combine, NULL, 0);
pr_info("Combine success\n");
return 0;
}
1. ID="0x20,0xac,0x00,0x15"
2. modprobe nandsim id_bytes=$ID parts=50,100 badblocks=100
3. insmod reproduce.ko
4. flash_erase /dev/mtd3 0 0
libmtd: error!: MEMERASE64 ioctl failed for eraseblock 100 (mtd3)
error 5 (Input/output error)
// Should be "flash_erase: Skipping bad block at 00c80000"
Fixes: 46b5889cc2c54bac ("mtd: implement proper partition handling")
Signed-off-by: Zhihao Cheng <chengzhihao1@huawei.com>
Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
Link: https://lore.kernel.org/linux-mtd/20210817114857.2784825-2-chengzhihao1@huawei.com
2021-08-17 11:48:56 +00:00
|
|
|
!concat->mtd._read_oob != !subdev_master->_read_oob ||
|
|
|
|
!concat->mtd._write_oob != !subdev_master->_write_oob) {
|
|
|
|
/*
|
|
|
|
* Check against subdev[i] for data members, because
|
|
|
|
* subdev's attributes may be different from master
|
|
|
|
* mtd device. Check against subdev's master mtd
|
|
|
|
* device for callbacks, because the existence of
|
|
|
|
* subdev's callbacks is decided by master mtd device.
|
|
|
|
*/
|
2005-04-16 22:20:36 +00:00
|
|
|
kfree(concat);
|
|
|
|
printk("Incompatible OOB or ECC data on \"%s\"\n",
|
|
|
|
subdev[i]->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
concat->subdev[i] = subdev[i];
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2016-02-03 18:01:31 +00:00
|
|
|
mtd_set_ooblayout(&concat->mtd, subdev[0]->ooblayout);
|
2006-05-17 15:11:16 +00:00
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
concat->num_subdev = num_devs;
|
|
|
|
concat->mtd.name = name;
|
|
|
|
|
2012-01-30 12:58:32 +00:00
|
|
|
concat->mtd._erase = concat_erase;
|
|
|
|
concat->mtd._sync = concat_sync;
|
|
|
|
concat->mtd._lock = concat_lock;
|
|
|
|
concat->mtd._unlock = concat_unlock;
|
2019-05-22 23:19:48 +00:00
|
|
|
concat->mtd._is_locked = concat_is_locked;
|
2012-01-30 12:58:32 +00:00
|
|
|
concat->mtd._suspend = concat_suspend;
|
|
|
|
concat->mtd._resume = concat_resume;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Combine the erase block size info of the subdevices:
|
|
|
|
*
|
|
|
|
* first, walk the map of the new device and see how
|
|
|
|
* many changes in erase size we have
|
|
|
|
*/
|
|
|
|
max_erasesize = curr_erasesize = subdev[0]->erasesize;
|
|
|
|
num_erase_region = 1;
|
|
|
|
for (i = 0; i < num_devs; i++) {
|
|
|
|
if (subdev[i]->numeraseregions == 0) {
|
|
|
|
/* current subdevice has uniform erase size */
|
|
|
|
if (subdev[i]->erasesize != curr_erasesize) {
|
|
|
|
/* if it differs from the last subdevice's erase size, count it */
|
|
|
|
++num_erase_region;
|
|
|
|
curr_erasesize = subdev[i]->erasesize;
|
|
|
|
if (curr_erasesize > max_erasesize)
|
|
|
|
max_erasesize = curr_erasesize;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
/* current subdevice has variable erase size */
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < subdev[i]->numeraseregions; j++) {
|
|
|
|
|
|
|
|
/* walk the list of erase regions, count any changes */
|
|
|
|
if (subdev[i]->eraseregions[j].erasesize !=
|
|
|
|
curr_erasesize) {
|
|
|
|
++num_erase_region;
|
|
|
|
curr_erasesize =
|
|
|
|
subdev[i]->eraseregions[j].
|
|
|
|
erasesize;
|
|
|
|
if (curr_erasesize > max_erasesize)
|
|
|
|
max_erasesize = curr_erasesize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (num_erase_region == 1) {
|
|
|
|
/*
|
|
|
|
* All subdevices have the same uniform erase size.
|
|
|
|
* This is easy:
|
|
|
|
*/
|
|
|
|
concat->mtd.erasesize = curr_erasesize;
|
|
|
|
concat->mtd.numeraseregions = 0;
|
|
|
|
} else {
|
2008-12-10 13:37:21 +00:00
|
|
|
uint64_t tmp64;
|
|
|
|
|
2005-04-16 22:20:36 +00:00
|
|
|
/*
|
|
|
|
* erase block size varies across the subdevices: allocate
|
|
|
|
* space to store the data describing the variable erase regions
|
|
|
|
*/
|
|
|
|
struct mtd_erase_region_info *erase_region_p;
|
2008-12-10 13:37:21 +00:00
|
|
|
uint64_t begin, position;
|
2005-04-16 22:20:36 +00:00
|
|
|
|
|
|
|
concat->mtd.erasesize = max_erasesize;
|
|
|
|
concat->mtd.numeraseregions = num_erase_region;
|
|
|
|
concat->mtd.eraseregions = erase_region_p =
|
treewide: kmalloc() -> kmalloc_array()
The kmalloc() function has a 2-factor argument form, kmalloc_array(). This
patch replaces cases of:
kmalloc(a * b, gfp)
with:
kmalloc_array(a * b, gfp)
as well as handling cases of:
kmalloc(a * b * c, gfp)
with:
kmalloc(array3_size(a, b, c), gfp)
as it's slightly less ugly than:
kmalloc_array(array_size(a, b), c, gfp)
This does, however, attempt to ignore constant size factors like:
kmalloc(4 * 1024, gfp)
though any constants defined via macros get caught up in the conversion.
Any factors with a sizeof() of "unsigned char", "char", and "u8" were
dropped, since they're redundant.
The tools/ directory was manually excluded, since it has its own
implementation of kmalloc().
The Coccinelle script used for this was:
// Fix redundant parens around sizeof().
@@
type TYPE;
expression THING, E;
@@
(
kmalloc(
- (sizeof(TYPE)) * E
+ sizeof(TYPE) * E
, ...)
|
kmalloc(
- (sizeof(THING)) * E
+ sizeof(THING) * E
, ...)
)
// Drop single-byte sizes and redundant parens.
@@
expression COUNT;
typedef u8;
typedef __u8;
@@
(
kmalloc(
- sizeof(u8) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(__u8) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(char) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(unsigned char) * (COUNT)
+ COUNT
, ...)
|
kmalloc(
- sizeof(u8) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(__u8) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(char) * COUNT
+ COUNT
, ...)
|
kmalloc(
- sizeof(unsigned char) * COUNT
+ COUNT
, ...)
)
// 2-factor product with sizeof(type/expression) and identifier or constant.
@@
type TYPE;
expression THING;
identifier COUNT_ID;
constant COUNT_CONST;
@@
(
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (COUNT_ID)
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * COUNT_ID
+ COUNT_ID, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (COUNT_CONST)
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * COUNT_CONST
+ COUNT_CONST, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (COUNT_ID)
+ COUNT_ID, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * COUNT_ID
+ COUNT_ID, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (COUNT_CONST)
+ COUNT_CONST, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * COUNT_CONST
+ COUNT_CONST, sizeof(THING)
, ...)
)
// 2-factor product, only identifiers.
@@
identifier SIZE, COUNT;
@@
- kmalloc
+ kmalloc_array
(
- SIZE * COUNT
+ COUNT, SIZE
, ...)
// 3-factor product with 1 sizeof(type) or sizeof(expression), with
// redundant parens removed.
@@
expression THING;
identifier STRIDE, COUNT;
type TYPE;
@@
(
kmalloc(
- sizeof(TYPE) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(TYPE) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(TYPE))
, ...)
|
kmalloc(
- sizeof(THING) * (COUNT) * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * (COUNT) * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * COUNT * (STRIDE)
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
|
kmalloc(
- sizeof(THING) * COUNT * STRIDE
+ array3_size(COUNT, STRIDE, sizeof(THING))
, ...)
)
// 3-factor product with 2 sizeof(variable), with redundant parens removed.
@@
expression THING1, THING2;
identifier COUNT;
type TYPE1, TYPE2;
@@
(
kmalloc(
- sizeof(TYPE1) * sizeof(TYPE2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(TYPE2))
, ...)
|
kmalloc(
- sizeof(THING1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(THING1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(THING1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * COUNT
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
|
kmalloc(
- sizeof(TYPE1) * sizeof(THING2) * (COUNT)
+ array3_size(COUNT, sizeof(TYPE1), sizeof(THING2))
, ...)
)
// 3-factor product, only identifiers, with redundant parens removed.
@@
identifier STRIDE, SIZE, COUNT;
@@
(
kmalloc(
- (COUNT) * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * (STRIDE) * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * STRIDE * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- (COUNT) * (STRIDE) * (SIZE)
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
|
kmalloc(
- COUNT * STRIDE * SIZE
+ array3_size(COUNT, STRIDE, SIZE)
, ...)
)
// Any remaining multi-factor products, first at least 3-factor products,
// when they're not all constants...
@@
expression E1, E2, E3;
constant C1, C2, C3;
@@
(
kmalloc(C1 * C2 * C3, ...)
|
kmalloc(
- (E1) * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- (E1) * (E2) * E3
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- (E1) * (E2) * (E3)
+ array3_size(E1, E2, E3)
, ...)
|
kmalloc(
- E1 * E2 * E3
+ array3_size(E1, E2, E3)
, ...)
)
// And then all remaining 2 factors products when they're not all constants,
// keeping sizeof() as the second factor argument.
@@
expression THING, E1, E2;
type TYPE;
constant C1, C2, C3;
@@
(
kmalloc(sizeof(THING) * C2, ...)
|
kmalloc(sizeof(TYPE) * C2, ...)
|
kmalloc(C1 * C2 * C3, ...)
|
kmalloc(C1 * C2, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * (E2)
+ E2, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(TYPE) * E2
+ E2, sizeof(TYPE)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * (E2)
+ E2, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- sizeof(THING) * E2
+ E2, sizeof(THING)
, ...)
|
- kmalloc
+ kmalloc_array
(
- (E1) * E2
+ E1, E2
, ...)
|
- kmalloc
+ kmalloc_array
(
- (E1) * (E2)
+ E1, E2
, ...)
|
- kmalloc
+ kmalloc_array
(
- E1 * E2
+ E1, E2
, ...)
)
Signed-off-by: Kees Cook <keescook@chromium.org>
2018-06-12 20:55:00 +00:00
|
|
|
kmalloc_array(num_erase_region,
|
|
|
|
sizeof(struct mtd_erase_region_info),
|
|
|
|
GFP_KERNEL);
|
2005-04-16 22:20:36 +00:00
|
|
|
if (!erase_region_p) {
|
|
|
|
kfree(concat);
|
|
|
|
printk
|
|
|
|
("memory allocation error while creating erase region list"
|
|
|
|
" for device \"%s\"\n", name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* walk the map of the new device once more and fill in
|
2022-09-08 12:22:29 +00:00
|
|
|
* erase region info:
|
2005-04-16 22:20:36 +00:00
|
|
|
*/
|
|
|
|
curr_erasesize = subdev[0]->erasesize;
|
|
|
|
begin = position = 0;
|
|
|
|
for (i = 0; i < num_devs; i++) {
|
|
|
|
if (subdev[i]->numeraseregions == 0) {
|
|
|
|
/* current subdevice has uniform erase size */
|
|
|
|
if (subdev[i]->erasesize != curr_erasesize) {
|
|
|
|
/*
|
|
|
|
* fill in an mtd_erase_region_info structure for the area
|
|
|
|
* we have walked so far:
|
|
|
|
*/
|
|
|
|
erase_region_p->offset = begin;
|
|
|
|
erase_region_p->erasesize =
|
|
|
|
curr_erasesize;
|
2008-12-10 13:37:21 +00:00
|
|
|
tmp64 = position - begin;
|
|
|
|
do_div(tmp64, curr_erasesize);
|
|
|
|
erase_region_p->numblocks = tmp64;
|
2005-04-16 22:20:36 +00:00
|
|
|
begin = position;
|
|
|
|
|
|
|
|
curr_erasesize = subdev[i]->erasesize;
|
|
|
|
++erase_region_p;
|
|
|
|
}
|
|
|
|
position += subdev[i]->size;
|
|
|
|
} else {
|
|
|
|
/* current subdevice has variable erase size */
|
|
|
|
int j;
|
|
|
|
for (j = 0; j < subdev[i]->numeraseregions; j++) {
|
|
|
|
/* walk the list of erase regions, count any changes */
|
|
|
|
if (subdev[i]->eraseregions[j].
|
|
|
|
erasesize != curr_erasesize) {
|
|
|
|
erase_region_p->offset = begin;
|
|
|
|
erase_region_p->erasesize =
|
|
|
|
curr_erasesize;
|
2008-12-10 13:37:21 +00:00
|
|
|
tmp64 = position - begin;
|
|
|
|
do_div(tmp64, curr_erasesize);
|
|
|
|
erase_region_p->numblocks = tmp64;
|
2005-04-16 22:20:36 +00:00
|
|
|
begin = position;
|
|
|
|
|
|
|
|
curr_erasesize =
|
|
|
|
subdev[i]->eraseregions[j].
|
|
|
|
erasesize;
|
|
|
|
++erase_region_p;
|
|
|
|
}
|
|
|
|
position +=
|
|
|
|
subdev[i]->eraseregions[j].
|
2008-12-10 13:37:21 +00:00
|
|
|
numblocks * (uint64_t)curr_erasesize;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Now write the final entry */
|
|
|
|
erase_region_p->offset = begin;
|
|
|
|
erase_region_p->erasesize = curr_erasesize;
|
2008-12-10 13:37:21 +00:00
|
|
|
tmp64 = position - begin;
|
|
|
|
do_div(tmp64, curr_erasesize);
|
|
|
|
erase_region_p->numblocks = tmp64;
|
2005-04-16 22:20:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &concat->mtd;
|
|
|
|
}
|
|
|
|
|
2019-11-13 17:15:02 +00:00
|
|
|
/* Cleans the context obtained from mtd_concat_create() */
|
2005-04-16 22:20:36 +00:00
|
|
|
void mtd_concat_destroy(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
struct mtd_concat *concat = CONCAT(mtd);
|
|
|
|
if (concat->mtd.numeraseregions)
|
|
|
|
kfree(concat->mtd.eraseregions);
|
|
|
|
kfree(concat);
|
|
|
|
}
|
|
|
|
|
|
|
|
EXPORT_SYMBOL(mtd_concat_create);
|
|
|
|
EXPORT_SYMBOL(mtd_concat_destroy);
|
|
|
|
|
|
|
|
MODULE_LICENSE("GPL");
|
|
|
|
MODULE_AUTHOR("Robert Kaiser <rkaiser@sysgo.de>");
|
|
|
|
MODULE_DESCRIPTION("Generic support for concatenating of MTD devices");
|