2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* Simple MTD partitioning layer
|
|
|
|
*
|
2010-08-09 03:58:20 +08:00
|
|
|
* Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
|
|
|
|
* Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
|
|
|
|
* Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
2010-08-09 03:58:20 +08:00
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 2 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
2005-04-17 06:20:36 +08:00
|
|
|
*
|
2005-11-07 19:15:26 +08:00
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/types.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/slab.h>
|
|
|
|
#include <linux/list.h>
|
|
|
|
#include <linux/kmod.h>
|
|
|
|
#include <linux/mtd/mtd.h>
|
|
|
|
#include <linux/mtd/partitions.h>
|
2010-09-17 18:31:41 +08:00
|
|
|
#include <linux/err.h>
|
2018-03-14 20:10:42 +08:00
|
|
|
#include <linux/of.h>
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-05-23 17:23:42 +08:00
|
|
|
#include "mtdcore.h"
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/* Our partition linked list */
|
|
|
|
static LIST_HEAD(mtd_partitions);
|
2010-09-17 18:31:41 +08:00
|
|
|
static DEFINE_MUTEX(mtd_partitions_mutex);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
/**
|
|
|
|
* struct mtd_part - our partition node structure
|
|
|
|
*
|
|
|
|
* @mtd: struct holding partition details
|
|
|
|
* @parent: parent mtd - flash device or another partition
|
|
|
|
* @offset: partition offset relative to the *flash device*
|
|
|
|
*/
|
2005-04-17 06:20:36 +08:00
|
|
|
struct mtd_part {
|
|
|
|
struct mtd_info mtd;
|
2017-06-21 14:26:44 +08:00
|
|
|
struct mtd_info *parent;
|
2008-12-10 21:37:21 +08:00
|
|
|
uint64_t offset;
|
2005-04-17 06:20:36 +08:00
|
|
|
struct list_head list;
|
|
|
|
};
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Given a pointer to the MTD object in the mtd_part structure, we can retrieve
|
2015-11-20 11:28:39 +08:00
|
|
|
* the pointer to that structure.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
2015-11-20 11:28:39 +08:00
|
|
|
static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
return container_of(mtd, struct mtd_part, mtd);
|
|
|
|
}
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2018-11-20 17:24:09 +08:00
|
|
|
static u64 part_absolute_offset(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
|
|
|
|
|
|
|
if (!mtd_is_partition(mtd))
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
return part_absolute_offset(part->parent) + part->offset;
|
|
|
|
}
|
2005-11-07 19:15:26 +08:00
|
|
|
|
|
|
|
/*
|
2005-04-17 06:20:36 +08:00
|
|
|
* MTD methods which simply translate the effective address and pass through
|
|
|
|
* to the _real_ device.
|
|
|
|
*/
|
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
|
|
|
|
size_t *retlen, u_char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2009-03-27 06:41:09 +08:00
|
|
|
struct mtd_ecc_stats stats;
|
2006-05-30 06:37:34 +08:00
|
|
|
int res;
|
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
stats = part->parent->ecc_stats;
|
|
|
|
res = part->parent->_read(part->parent, from + part->offset, len,
|
2012-03-04 05:13:06 +08:00
|
|
|
retlen, buf);
|
2012-04-26 03:06:11 +08:00
|
|
|
if (unlikely(mtd_is_eccerr(res)))
|
|
|
|
mtd->ecc_stats.failed +=
|
2017-06-21 14:26:44 +08:00
|
|
|
part->parent->ecc_stats.failed - stats.failed;
|
2012-04-26 03:06:11 +08:00
|
|
|
else
|
|
|
|
mtd->ecc_stats.corrected +=
|
2017-06-21 14:26:44 +08:00
|
|
|
part->parent->ecc_stats.corrected - stats.corrected;
|
2006-05-30 06:37:34 +08:00
|
|
|
return res;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
|
|
|
|
size_t *retlen, void **virt, resource_size_t *phys)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2012-02-03 22:23:52 +08:00
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_point(part->parent, from + part->offset, len,
|
2012-03-04 05:13:06 +08:00
|
|
|
retlen, virt, phys);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2006-05-23 23:21:03 +08:00
|
|
|
|
2012-02-03 19:20:43 +08:00
|
|
|
static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_unpoint(part->parent, from + part->offset, len);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2006-05-29 09:26:58 +08:00
|
|
|
static int part_read_oob(struct mtd_info *mtd, loff_t from,
|
2008-07-19 00:00:33 +08:00
|
|
|
struct mtd_oob_ops *ops)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2018-01-09 16:50:33 +08:00
|
|
|
struct mtd_ecc_stats stats;
|
2006-05-30 06:37:34 +08:00
|
|
|
int res;
|
2006-05-29 09:26:58 +08:00
|
|
|
|
2018-01-09 16:50:33 +08:00
|
|
|
stats = part->parent->ecc_stats;
|
2017-06-21 14:26:44 +08:00
|
|
|
res = part->parent->_read_oob(part->parent, from + part->offset, ops);
|
2018-01-09 16:50:33 +08:00
|
|
|
if (unlikely(mtd_is_eccerr(res)))
|
|
|
|
mtd->ecc_stats.failed +=
|
|
|
|
part->parent->ecc_stats.failed - stats.failed;
|
|
|
|
else
|
|
|
|
mtd->ecc_stats.corrected +=
|
|
|
|
part->parent->ecc_stats.corrected - stats.corrected;
|
2006-05-30 06:37:34 +08:00
|
|
|
return res;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
|
|
|
|
size_t len, size_t *retlen, u_char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_read_user_prot_reg(part->parent, from, len,
|
2012-03-04 05:13:06 +08:00
|
|
|
retlen, buf);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2014-01-28 16:29:44 +08:00
|
|
|
static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
|
|
|
|
size_t *retlen, struct otp_info *buf)
|
2005-02-09 01:11:19 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_get_user_prot_info(part->parent, len, retlen,
|
2014-01-28 16:29:44 +08:00
|
|
|
buf);
|
2005-02-09 01:11:19 +08:00
|
|
|
}
|
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
|
|
|
|
size_t len, size_t *retlen, u_char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_read_fact_prot_reg(part->parent, from, len,
|
2012-03-04 05:13:06 +08:00
|
|
|
retlen, buf);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2014-01-28 16:29:44 +08:00
|
|
|
static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
|
|
|
|
size_t *retlen, struct otp_info *buf)
|
2005-02-09 01:11:19 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_get_fact_prot_info(part->parent, len, retlen,
|
2014-01-28 16:29:44 +08:00
|
|
|
buf);
|
2005-02-09 01:11:19 +08:00
|
|
|
}
|
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
|
|
|
|
size_t *retlen, const u_char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_write(part->parent, to + part->offset, len,
|
2012-03-04 05:13:06 +08:00
|
|
|
retlen, buf);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
|
|
|
|
size_t *retlen, const u_char *buf)
|
2008-02-06 18:17:15 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_panic_write(part->parent, to + part->offset, len,
|
2012-03-04 05:13:06 +08:00
|
|
|
retlen, buf);
|
2008-02-06 18:17:15 +08:00
|
|
|
}
|
|
|
|
|
2006-05-29 09:26:58 +08:00
|
|
|
static int part_write_oob(struct mtd_info *mtd, loff_t to,
|
2008-07-19 00:00:33 +08:00
|
|
|
struct mtd_oob_ops *ops)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2006-05-29 09:26:58 +08:00
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_write_oob(part->parent, to + part->offset, ops);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
|
|
|
|
size_t len, size_t *retlen, u_char *buf)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_write_user_prot_reg(part->parent, from, len,
|
2012-03-04 05:13:06 +08:00
|
|
|
retlen, buf);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
|
|
|
|
size_t len)
|
2005-02-09 01:11:19 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_lock_user_prot_reg(part->parent, from, len);
|
2005-02-09 01:11:19 +08:00
|
|
|
}
|
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
|
|
|
|
unsigned long count, loff_t to, size_t *retlen)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_writev(part->parent, vecs, count,
|
2012-03-04 05:13:06 +08:00
|
|
|
to + part->offset, retlen);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2005-04-17 06:20:36 +08:00
|
|
|
int ret;
|
2012-02-04 00:13:23 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
instr->addr += part->offset;
|
2017-06-21 14:26:44 +08:00
|
|
|
ret = part->parent->_erase(part->parent, instr);
|
2018-02-13 05:03:10 +08:00
|
|
|
if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
|
|
|
|
instr->fail_addr -= part->offset;
|
|
|
|
instr->addr -= part->offset;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2008-12-10 21:37:21 +08:00
|
|
|
static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_lock(part->parent, ofs + part->offset, len);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-12-10 21:37:21 +08:00
|
|
|
static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_unlock(part->parent, ofs + part->offset, len);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2010-06-15 00:10:33 +08:00
|
|
|
static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
|
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_is_locked(part->parent, ofs + part->offset, len);
|
2010-06-15 00:10:33 +08:00
|
|
|
}
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
static void part_sync(struct mtd_info *mtd)
|
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
part->parent->_sync(part->parent);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int part_suspend(struct mtd_info *mtd)
|
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_suspend(part->parent);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void part_resume(struct mtd_info *mtd)
|
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
part->parent->_resume(part->parent);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2014-05-22 06:06:12 +08:00
|
|
|
static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
|
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2014-05-22 06:06:12 +08:00
|
|
|
ofs += part->offset;
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_block_isreserved(part->parent, ofs);
|
2014-05-22 06:06:12 +08:00
|
|
|
}
|
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2005-04-17 06:20:36 +08:00
|
|
|
ofs += part->offset;
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_block_isbad(part->parent, ofs);
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2006-05-30 06:37:34 +08:00
|
|
|
int res;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
ofs += part->offset;
|
2017-06-21 14:26:44 +08:00
|
|
|
res = part->parent->_block_markbad(part->parent, ofs);
|
2006-05-30 06:37:34 +08:00
|
|
|
if (!res)
|
|
|
|
mtd->ecc_stats.badblocks++;
|
|
|
|
return res;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
|
|
|
|
2016-09-21 17:43:56 +08:00
|
|
|
static int part_get_device(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_get_device(part->parent);
|
2016-09-21 17:43:56 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void part_put_device(struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2017-06-21 14:26:44 +08:00
|
|
|
part->parent->_put_device(part->parent);
|
2016-09-21 17:43:56 +08:00
|
|
|
}
|
|
|
|
|
2016-02-04 02:01:31 +08:00
|
|
|
static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
|
|
|
|
struct mtd_oob_region *oobregion)
|
|
|
|
{
|
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
return mtd_ooblayout_ecc(part->parent, section, oobregion);
|
2016-02-04 02:01:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int part_ooblayout_free(struct mtd_info *mtd, int section,
|
|
|
|
struct mtd_oob_region *oobregion)
|
|
|
|
{
|
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
return mtd_ooblayout_free(part->parent, section, oobregion);
|
2016-02-04 02:01:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static const struct mtd_ooblayout_ops part_ooblayout_ops = {
|
|
|
|
.ecc = part_ooblayout_ecc,
|
|
|
|
.free = part_ooblayout_free,
|
|
|
|
};
|
|
|
|
|
2017-01-11 03:30:17 +08:00
|
|
|
static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
|
|
|
|
{
|
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
return part->parent->_max_bad_blocks(part->parent,
|
2017-01-11 03:30:17 +08:00
|
|
|
ofs + part->offset, len);
|
|
|
|
}
|
|
|
|
|
2010-09-17 18:31:41 +08:00
|
|
|
static inline void free_partition(struct mtd_part *p)
|
|
|
|
{
|
|
|
|
kfree(p->mtd.name);
|
|
|
|
kfree(p);
|
|
|
|
}
|
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
static struct mtd_part *allocate_partition(struct mtd_info *parent,
|
2010-09-17 18:31:41 +08:00
|
|
|
const struct mtd_partition *part, int partno,
|
|
|
|
uint64_t cur_offset)
|
2008-07-19 00:00:18 +08:00
|
|
|
{
|
2017-06-23 05:09:18 +08:00
|
|
|
int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize :
|
2017-06-21 14:26:44 +08:00
|
|
|
parent->erasesize;
|
2008-07-19 00:00:18 +08:00
|
|
|
struct mtd_part *slave;
|
2017-06-09 11:58:31 +08:00
|
|
|
u32 remainder;
|
2010-09-17 18:31:41 +08:00
|
|
|
char *name;
|
2017-06-09 11:58:31 +08:00
|
|
|
u64 tmp;
|
2008-07-19 00:00:18 +08:00
|
|
|
|
|
|
|
/* allocate the partition structure */
|
2008-07-19 00:00:33 +08:00
|
|
|
slave = kzalloc(sizeof(*slave), GFP_KERNEL);
|
2010-09-17 18:31:41 +08:00
|
|
|
name = kstrdup(part->name, GFP_KERNEL);
|
|
|
|
if (!name || !slave) {
|
2008-07-19 00:00:33 +08:00
|
|
|
printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
|
2017-06-21 14:26:44 +08:00
|
|
|
parent->name);
|
2010-09-17 18:31:41 +08:00
|
|
|
kfree(name);
|
|
|
|
kfree(slave);
|
|
|
|
return ERR_PTR(-ENOMEM);
|
2008-07-19 00:00:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* set up the MTD object for this partition */
|
2017-06-21 14:26:44 +08:00
|
|
|
slave->mtd.type = parent->type;
|
2018-11-20 16:55:45 +08:00
|
|
|
slave->mtd.flags = parent->orig_flags & ~part->mask_flags;
|
|
|
|
slave->mtd.orig_flags = slave->mtd.flags;
|
2008-07-19 00:00:18 +08:00
|
|
|
slave->mtd.size = part->size;
|
2017-06-21 14:26:44 +08:00
|
|
|
slave->mtd.writesize = parent->writesize;
|
|
|
|
slave->mtd.writebufsize = parent->writebufsize;
|
|
|
|
slave->mtd.oobsize = parent->oobsize;
|
|
|
|
slave->mtd.oobavail = parent->oobavail;
|
|
|
|
slave->mtd.subpage_sft = parent->subpage_sft;
|
|
|
|
slave->mtd.pairing = parent->pairing;
|
2008-07-19 00:00:18 +08:00
|
|
|
|
2010-09-17 18:31:41 +08:00
|
|
|
slave->mtd.name = name;
|
2017-06-21 14:26:44 +08:00
|
|
|
slave->mtd.owner = parent->owner;
|
2008-07-19 00:00:18 +08:00
|
|
|
|
2015-04-03 06:15:10 +08:00
|
|
|
/* NOTE: Historically, we didn't arrange MTDs as a tree out of
|
|
|
|
* concern for showing the same data in multiple partitions.
|
|
|
|
* However, it is very useful to have the master node present,
|
|
|
|
* so the MTD_PARTITIONED_MASTER option allows that. The master
|
|
|
|
* will have device nodes etc only if this is set, so make the
|
|
|
|
* parent conditional on that option. Note, this is a way to
|
|
|
|
* distinguish between the master and the partition in sysfs.
|
2009-03-26 15:42:41 +08:00
|
|
|
*/
|
2017-06-21 14:26:45 +08:00
|
|
|
slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
|
2017-06-21 14:26:44 +08:00
|
|
|
&parent->dev :
|
|
|
|
parent->dev.parent;
|
2017-02-09 18:50:24 +08:00
|
|
|
slave->mtd.dev.of_node = part->of_node;
|
2009-03-26 15:42:41 +08:00
|
|
|
|
2018-01-09 16:50:34 +08:00
|
|
|
if (parent->_read)
|
|
|
|
slave->mtd._read = part_read;
|
|
|
|
if (parent->_write)
|
|
|
|
slave->mtd._write = part_write;
|
2008-07-19 00:00:18 +08:00
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_panic_write)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._panic_write = part_panic_write;
|
2008-07-19 00:00:18 +08:00
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_point && parent->_unpoint) {
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._point = part_point;
|
|
|
|
slave->mtd._unpoint = part_unpoint;
|
2008-07-19 00:00:18 +08:00
|
|
|
}
|
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_read_oob)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._read_oob = part_read_oob;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_write_oob)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._write_oob = part_write_oob;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_read_user_prot_reg)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_read_fact_prot_reg)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_write_user_prot_reg)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_lock_user_prot_reg)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_get_user_prot_info)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._get_user_prot_info = part_get_user_prot_info;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_get_fact_prot_info)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_sync)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._sync = part_sync;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (!partno && !parent->dev.class && parent->_suspend &&
|
|
|
|
parent->_resume) {
|
2017-06-23 05:09:18 +08:00
|
|
|
slave->mtd._suspend = part_suspend;
|
|
|
|
slave->mtd._resume = part_resume;
|
2008-07-19 00:00:18 +08:00
|
|
|
}
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_writev)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._writev = part_writev;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_lock)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._lock = part_lock;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_unlock)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._unlock = part_unlock;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_is_locked)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._is_locked = part_is_locked;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_block_isreserved)
|
2014-05-22 06:06:12 +08:00
|
|
|
slave->mtd._block_isreserved = part_block_isreserved;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_block_isbad)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._block_isbad = part_block_isbad;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_block_markbad)
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._block_markbad = part_block_markbad;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_max_bad_blocks)
|
2017-01-11 03:30:17 +08:00
|
|
|
slave->mtd._max_bad_blocks = part_max_bad_blocks;
|
2016-09-21 17:43:56 +08:00
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_get_device)
|
2016-09-21 17:43:56 +08:00
|
|
|
slave->mtd._get_device = part_get_device;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_put_device)
|
2016-09-21 17:43:56 +08:00
|
|
|
slave->mtd._put_device = part_put_device;
|
|
|
|
|
2012-01-30 20:58:32 +08:00
|
|
|
slave->mtd._erase = part_erase;
|
2017-06-21 14:26:44 +08:00
|
|
|
slave->parent = parent;
|
2008-07-19 00:00:18 +08:00
|
|
|
slave->offset = part->offset;
|
|
|
|
|
|
|
|
if (slave->offset == MTDPART_OFS_APPEND)
|
|
|
|
slave->offset = cur_offset;
|
|
|
|
if (slave->offset == MTDPART_OFS_NXTBLK) {
|
2017-06-09 11:58:31 +08:00
|
|
|
tmp = cur_offset;
|
2008-07-19 00:00:18 +08:00
|
|
|
slave->offset = cur_offset;
|
2017-06-09 11:58:31 +08:00
|
|
|
remainder = do_div(tmp, wr_alignment);
|
|
|
|
if (remainder) {
|
|
|
|
slave->offset += wr_alignment - remainder;
|
2008-07-19 00:00:18 +08:00
|
|
|
printk(KERN_NOTICE "Moving partition %d: "
|
2008-12-10 21:37:21 +08:00
|
|
|
"0x%012llx -> 0x%012llx\n", partno,
|
|
|
|
(unsigned long long)cur_offset, (unsigned long long)slave->offset);
|
2008-07-19 00:00:18 +08:00
|
|
|
}
|
|
|
|
}
|
2011-06-06 22:04:14 +08:00
|
|
|
if (slave->offset == MTDPART_OFS_RETAIN) {
|
|
|
|
slave->offset = cur_offset;
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->size - slave->offset >= slave->mtd.size) {
|
|
|
|
slave->mtd.size = parent->size - slave->offset
|
2011-06-06 22:04:14 +08:00
|
|
|
- slave->mtd.size;
|
|
|
|
} else {
|
|
|
|
printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
|
2017-06-21 14:26:44 +08:00
|
|
|
part->name, parent->size - slave->offset,
|
2011-06-06 22:04:14 +08:00
|
|
|
slave->mtd.size);
|
|
|
|
/* register to preserve ordering */
|
|
|
|
goto out_register;
|
|
|
|
}
|
|
|
|
}
|
2008-07-19 00:00:18 +08:00
|
|
|
if (slave->mtd.size == MTDPART_SIZ_FULL)
|
2017-06-21 14:26:44 +08:00
|
|
|
slave->mtd.size = parent->size - slave->offset;
|
2008-07-19 00:00:18 +08:00
|
|
|
|
2008-12-10 21:37:21 +08:00
|
|
|
printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
|
|
|
|
(unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
|
2008-07-19 00:00:18 +08:00
|
|
|
|
|
|
|
/* let's do some sanity checks */
|
2017-06-21 14:26:44 +08:00
|
|
|
if (slave->offset >= parent->size) {
|
2008-07-19 00:01:22 +08:00
|
|
|
/* let's register it anyway to preserve ordering */
|
2008-07-19 00:00:18 +08:00
|
|
|
slave->offset = 0;
|
|
|
|
slave->mtd.size = 0;
|
2008-07-19 00:00:33 +08:00
|
|
|
printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
|
2008-07-19 00:00:18 +08:00
|
|
|
part->name);
|
2008-07-19 00:01:22 +08:00
|
|
|
goto out_register;
|
2008-07-19 00:00:18 +08:00
|
|
|
}
|
2017-06-21 14:26:44 +08:00
|
|
|
if (slave->offset + slave->mtd.size > parent->size) {
|
|
|
|
slave->mtd.size = parent->size - slave->offset;
|
2008-12-10 21:37:21 +08:00
|
|
|
printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
|
2017-06-21 14:26:44 +08:00
|
|
|
part->name, parent->name, (unsigned long long)slave->mtd.size);
|
2008-07-19 00:00:18 +08:00
|
|
|
}
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->numeraseregions > 1) {
|
2008-07-19 00:00:18 +08:00
|
|
|
/* Deal with variable erase size stuff */
|
2017-06-21 14:26:44 +08:00
|
|
|
int i, max = parent->numeraseregions;
|
2008-12-10 21:37:21 +08:00
|
|
|
u64 end = slave->offset + slave->mtd.size;
|
2017-06-21 14:26:44 +08:00
|
|
|
struct mtd_erase_region_info *regions = parent->eraseregions;
|
2008-07-19 00:00:18 +08:00
|
|
|
|
2008-07-19 00:00:57 +08:00
|
|
|
/* Find the first erase regions which is part of this
|
|
|
|
* partition. */
|
|
|
|
for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
|
2008-07-19 00:00:18 +08:00
|
|
|
;
|
2008-07-19 00:00:57 +08:00
|
|
|
/* The loop searched for the region _behind_ the first one */
|
2009-09-19 03:51:50 +08:00
|
|
|
if (i > 0)
|
|
|
|
i--;
|
2008-07-19 00:00:18 +08:00
|
|
|
|
2008-07-19 00:00:57 +08:00
|
|
|
/* Pick biggest erasesize */
|
|
|
|
for (; i < max && regions[i].offset < end; i++) {
|
2008-07-19 00:00:18 +08:00
|
|
|
if (slave->mtd.erasesize < regions[i].erasesize) {
|
|
|
|
slave->mtd.erasesize = regions[i].erasesize;
|
|
|
|
}
|
|
|
|
}
|
2008-07-19 00:00:57 +08:00
|
|
|
BUG_ON(slave->mtd.erasesize == 0);
|
2008-07-19 00:00:18 +08:00
|
|
|
} else {
|
|
|
|
/* Single erase size */
|
2017-06-21 14:26:44 +08:00
|
|
|
slave->mtd.erasesize = parent->erasesize;
|
2008-07-19 00:00:18 +08:00
|
|
|
}
|
|
|
|
|
2017-09-25 16:19:57 +08:00
|
|
|
/*
|
|
|
|
* Slave erasesize might differ from the master one if the master
|
|
|
|
* exposes several regions with different erasesize. Adjust
|
|
|
|
* wr_alignment accordingly.
|
|
|
|
*/
|
|
|
|
if (!(slave->mtd.flags & MTD_NO_ERASE))
|
|
|
|
wr_alignment = slave->mtd.erasesize;
|
|
|
|
|
2018-11-20 17:24:09 +08:00
|
|
|
tmp = part_absolute_offset(parent) + slave->offset;
|
2017-06-09 11:58:31 +08:00
|
|
|
remainder = do_div(tmp, wr_alignment);
|
|
|
|
if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
|
2008-07-19 00:00:18 +08:00
|
|
|
/* Doesn't start on a boundary of major erase size */
|
2008-07-19 00:00:33 +08:00
|
|
|
/* FIXME: Let it be writable if it is on a boundary of
|
|
|
|
* _minor_ erase size though */
|
2008-07-19 00:00:18 +08:00
|
|
|
slave->mtd.flags &= ~MTD_WRITEABLE;
|
2017-06-09 11:58:31 +08:00
|
|
|
printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
|
2008-07-19 00:00:18 +08:00
|
|
|
part->name);
|
|
|
|
}
|
2017-06-09 11:58:31 +08:00
|
|
|
|
2018-11-20 17:24:09 +08:00
|
|
|
tmp = part_absolute_offset(parent) + slave->mtd.size;
|
2017-06-09 11:58:31 +08:00
|
|
|
remainder = do_div(tmp, wr_alignment);
|
|
|
|
if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
|
2008-07-19 00:00:18 +08:00
|
|
|
slave->mtd.flags &= ~MTD_WRITEABLE;
|
2017-06-09 11:58:31 +08:00
|
|
|
printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
|
2008-07-19 00:00:18 +08:00
|
|
|
part->name);
|
|
|
|
}
|
|
|
|
|
2016-02-04 02:01:31 +08:00
|
|
|
mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
|
2017-06-21 14:26:44 +08:00
|
|
|
slave->mtd.ecc_step_size = parent->ecc_step_size;
|
|
|
|
slave->mtd.ecc_strength = parent->ecc_strength;
|
|
|
|
slave->mtd.bitflip_threshold = parent->bitflip_threshold;
|
2012-04-26 03:06:08 +08:00
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
if (parent->_block_isbad) {
|
2008-12-10 21:37:21 +08:00
|
|
|
uint64_t offs = 0;
|
2008-07-19 00:00:18 +08:00
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
while (offs < slave->mtd.size) {
|
2017-06-21 14:26:44 +08:00
|
|
|
if (mtd_block_isreserved(parent, offs + slave->offset))
|
2014-03-21 19:57:44 +08:00
|
|
|
slave->mtd.ecc_stats.bbtblocks++;
|
2017-06-21 14:26:44 +08:00
|
|
|
else if (mtd_block_isbad(parent, offs + slave->offset))
|
2008-07-19 00:00:18 +08:00
|
|
|
slave->mtd.ecc_stats.badblocks++;
|
|
|
|
offs += slave->mtd.erasesize;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-07-19 00:01:22 +08:00
|
|
|
out_register:
|
2008-07-19 00:00:18 +08:00
|
|
|
return slave;
|
|
|
|
}
|
|
|
|
|
2015-04-03 06:15:11 +08:00
|
|
|
static ssize_t mtd_partition_offset_show(struct device *dev,
|
|
|
|
struct device_attribute *attr, char *buf)
|
|
|
|
{
|
|
|
|
struct mtd_info *mtd = dev_get_drvdata(dev);
|
2015-11-20 11:28:39 +08:00
|
|
|
struct mtd_part *part = mtd_to_part(mtd);
|
2015-04-03 06:15:11 +08:00
|
|
|
return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
|
|
|
|
|
|
|
|
static const struct attribute *mtd_partition_attrs[] = {
|
|
|
|
&dev_attr_offset.attr,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
static int mtd_add_partition_attrs(struct mtd_part *new)
|
|
|
|
{
|
|
|
|
int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
|
|
|
|
if (ret)
|
|
|
|
printk(KERN_WARNING
|
|
|
|
"mtd: failed to create partition attrs, err=%d\n", ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
int mtd_add_partition(struct mtd_info *parent, const char *name,
|
2010-09-17 18:31:41 +08:00
|
|
|
long long offset, long long length)
|
|
|
|
{
|
|
|
|
struct mtd_partition part;
|
2015-04-03 06:15:12 +08:00
|
|
|
struct mtd_part *new;
|
2010-09-17 18:31:41 +08:00
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
/* the direct offset is expected */
|
|
|
|
if (offset == MTDPART_OFS_APPEND ||
|
|
|
|
offset == MTDPART_OFS_NXTBLK)
|
|
|
|
return -EINVAL;
|
|
|
|
|
|
|
|
if (length == MTDPART_SIZ_FULL)
|
2017-06-21 14:26:44 +08:00
|
|
|
length = parent->size - offset;
|
2010-09-17 18:31:41 +08:00
|
|
|
|
|
|
|
if (length <= 0)
|
|
|
|
return -EINVAL;
|
|
|
|
|
2015-11-12 08:47:52 +08:00
|
|
|
memset(&part, 0, sizeof(part));
|
2010-09-17 18:31:41 +08:00
|
|
|
part.name = name;
|
|
|
|
part.size = length;
|
|
|
|
part.offset = offset;
|
|
|
|
|
2017-06-21 14:26:44 +08:00
|
|
|
new = allocate_partition(parent, &part, -1, offset);
|
2010-09-17 18:31:41 +08:00
|
|
|
if (IS_ERR(new))
|
|
|
|
return PTR_ERR(new);
|
|
|
|
|
|
|
|
mutex_lock(&mtd_partitions_mutex);
|
|
|
|
list_add(&new->list, &mtd_partitions);
|
|
|
|
mutex_unlock(&mtd_partitions_mutex);
|
|
|
|
|
|
|
|
add_mtd_device(&new->mtd);
|
|
|
|
|
2015-04-03 06:15:11 +08:00
|
|
|
mtd_add_partition_attrs(new);
|
|
|
|
|
2010-09-17 18:31:41 +08:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mtd_add_partition);
|
|
|
|
|
2017-06-21 14:26:42 +08:00
|
|
|
/**
|
|
|
|
* __mtd_del_partition - delete MTD partition
|
|
|
|
*
|
|
|
|
* @priv: internal MTD struct for partition to be deleted
|
|
|
|
*
|
|
|
|
* This function must be called with the partitions mutex locked.
|
|
|
|
*/
|
|
|
|
static int __mtd_del_partition(struct mtd_part *priv)
|
|
|
|
{
|
2017-06-21 14:26:45 +08:00
|
|
|
struct mtd_part *child, *next;
|
2017-06-21 14:26:42 +08:00
|
|
|
int err;
|
|
|
|
|
2017-06-21 14:26:45 +08:00
|
|
|
list_for_each_entry_safe(child, next, &mtd_partitions, list) {
|
|
|
|
if (child->parent == &priv->mtd) {
|
|
|
|
err = __mtd_del_partition(child);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-21 14:26:43 +08:00
|
|
|
sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);
|
|
|
|
|
2017-06-21 14:26:42 +08:00
|
|
|
err = del_mtd_device(&priv->mtd);
|
|
|
|
if (err)
|
|
|
|
return err;
|
|
|
|
|
|
|
|
list_del(&priv->list);
|
|
|
|
free_partition(priv);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function unregisters and destroy all slave MTD objects which are
|
2017-06-21 14:26:45 +08:00
|
|
|
* attached to the given MTD object.
|
2017-06-21 14:26:42 +08:00
|
|
|
*/
|
2017-06-21 14:26:45 +08:00
|
|
|
int del_mtd_partitions(struct mtd_info *mtd)
|
2017-06-21 14:26:42 +08:00
|
|
|
{
|
|
|
|
struct mtd_part *slave, *next;
|
|
|
|
int ret, err = 0;
|
|
|
|
|
|
|
|
mutex_lock(&mtd_partitions_mutex);
|
|
|
|
list_for_each_entry_safe(slave, next, &mtd_partitions, list)
|
2017-06-21 14:26:45 +08:00
|
|
|
if (slave->parent == mtd) {
|
2017-06-21 14:26:42 +08:00
|
|
|
ret = __mtd_del_partition(slave);
|
|
|
|
if (ret < 0)
|
|
|
|
err = ret;
|
|
|
|
}
|
|
|
|
mutex_unlock(&mtd_partitions_mutex);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2017-06-21 14:26:45 +08:00
|
|
|
int mtd_del_partition(struct mtd_info *mtd, int partno)
|
2010-09-17 18:31:41 +08:00
|
|
|
{
|
|
|
|
struct mtd_part *slave, *next;
|
|
|
|
int ret = -EINVAL;
|
|
|
|
|
|
|
|
mutex_lock(&mtd_partitions_mutex);
|
|
|
|
list_for_each_entry_safe(slave, next, &mtd_partitions, list)
|
2017-06-21 14:26:45 +08:00
|
|
|
if ((slave->parent == mtd) &&
|
2010-09-17 18:31:41 +08:00
|
|
|
(slave->mtd.index == partno)) {
|
2017-06-21 14:26:42 +08:00
|
|
|
ret = __mtd_del_partition(slave);
|
2010-09-17 18:31:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
mutex_unlock(&mtd_partitions_mutex);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mtd_del_partition);
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
/*
|
|
|
|
* This function, given a master MTD object and a partition table, creates
|
|
|
|
* and registers slave MTD objects which are bound to the master according to
|
|
|
|
* the partition definitions.
|
2009-03-26 15:42:41 +08:00
|
|
|
*
|
2015-04-03 06:15:10 +08:00
|
|
|
* For historical reasons, this function's caller only registers the master
|
|
|
|
* if the MTD_PARTITIONED_MASTER config option is set.
|
2005-04-17 06:20:36 +08:00
|
|
|
*/
|
|
|
|
|
2005-11-07 19:15:26 +08:00
|
|
|
int add_mtd_partitions(struct mtd_info *master,
|
2005-04-17 06:20:36 +08:00
|
|
|
const struct mtd_partition *parts,
|
|
|
|
int nbparts)
|
|
|
|
{
|
|
|
|
struct mtd_part *slave;
|
2008-12-10 21:37:21 +08:00
|
|
|
uint64_t cur_offset = 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
int i;
|
|
|
|
|
2008-07-19 00:00:33 +08:00
|
|
|
printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
|
|
|
for (i = 0; i < nbparts; i++) {
|
2010-09-17 18:31:41 +08:00
|
|
|
slave = allocate_partition(master, parts + i, i, cur_offset);
|
2015-07-30 18:18:03 +08:00
|
|
|
if (IS_ERR(slave)) {
|
|
|
|
del_mtd_partitions(master);
|
2010-09-17 18:31:41 +08:00
|
|
|
return PTR_ERR(slave);
|
2015-07-30 18:18:03 +08:00
|
|
|
}
|
2010-09-17 18:31:41 +08:00
|
|
|
|
|
|
|
mutex_lock(&mtd_partitions_mutex);
|
|
|
|
list_add(&slave->list, &mtd_partitions);
|
|
|
|
mutex_unlock(&mtd_partitions_mutex);
|
|
|
|
|
|
|
|
add_mtd_device(&slave->mtd);
|
2015-04-03 06:15:11 +08:00
|
|
|
mtd_add_partition_attrs(slave);
|
2018-07-13 22:32:21 +08:00
|
|
|
/* Look for subpartitions */
|
|
|
|
parse_mtd_partitions(&slave->mtd, parts[i].types, NULL);
|
2010-09-17 18:31:41 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
cur_offset = slave->offset + slave->mtd.size;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static DEFINE_SPINLOCK(part_parser_lock);
|
|
|
|
static LIST_HEAD(part_parsers);
|
|
|
|
|
2015-12-05 07:25:15 +08:00
|
|
|
static struct mtd_part_parser *mtd_part_parser_get(const char *name)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2008-05-20 03:11:50 +08:00
|
|
|
struct mtd_part_parser *p, *ret = NULL;
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-05-20 03:11:50 +08:00
|
|
|
spin_lock(&part_parser_lock);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2008-05-20 03:11:50 +08:00
|
|
|
list_for_each_entry(p, &part_parsers, list)
|
2005-04-17 06:20:36 +08:00
|
|
|
if (!strcmp(p->name, name) && try_module_get(p->owner)) {
|
|
|
|
ret = p;
|
|
|
|
break;
|
|
|
|
}
|
2008-05-20 03:11:50 +08:00
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_unlock(&part_parser_lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-12-05 07:25:15 +08:00
|
|
|
static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
|
|
|
|
{
|
|
|
|
module_put(p->owner);
|
|
|
|
}
|
2011-06-23 19:26:14 +08:00
|
|
|
|
2015-12-10 02:24:03 +08:00
|
|
|
/*
|
|
|
|
* Many partition parsers just expected the core to kfree() all their data in
|
|
|
|
* one chunk. Do that by default.
|
|
|
|
*/
|
|
|
|
static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
|
|
|
|
int nr_parts)
|
|
|
|
{
|
|
|
|
kfree(pparts);
|
|
|
|
}
|
|
|
|
|
2015-11-12 11:13:29 +08:00
|
|
|
int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2015-11-12 11:13:29 +08:00
|
|
|
p->owner = owner;
|
|
|
|
|
2015-12-10 02:24:03 +08:00
|
|
|
if (!p->cleanup)
|
|
|
|
p->cleanup = &mtd_part_parser_cleanup_default;
|
|
|
|
|
2005-04-17 06:20:36 +08:00
|
|
|
spin_lock(&part_parser_lock);
|
|
|
|
list_add(&p->list, &part_parsers);
|
|
|
|
spin_unlock(&part_parser_lock);
|
2015-11-12 11:13:29 +08:00
|
|
|
|
|
|
|
return 0;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2015-11-12 11:13:29 +08:00
|
|
|
EXPORT_SYMBOL_GPL(__register_mtd_parser);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2013-12-01 18:59:15 +08:00
|
|
|
void deregister_mtd_parser(struct mtd_part_parser *p)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
|
|
|
spin_lock(&part_parser_lock);
|
|
|
|
list_del(&p->list);
|
|
|
|
spin_unlock(&part_parser_lock);
|
|
|
|
}
|
2008-07-19 00:00:33 +08:00
|
|
|
EXPORT_SYMBOL_GPL(deregister_mtd_parser);
|
2005-04-17 06:20:36 +08:00
|
|
|
|
2011-06-08 16:42:27 +08:00
|
|
|
/*
|
|
|
|
* Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
|
|
|
|
* are changing this array!
|
|
|
|
*/
|
2013-03-12 16:51:35 +08:00
|
|
|
static const char * const default_mtd_part_types[] = {
|
2011-05-30 01:32:33 +08:00
|
|
|
"cmdlinepart",
|
|
|
|
"ofpart",
|
|
|
|
NULL
|
|
|
|
};
|
2011-06-02 22:51:16 +08:00
|
|
|
|
2018-07-13 22:32:21 +08:00
|
|
|
/* Check DT only when looking for subpartitions. */
|
|
|
|
static const char * const default_subpartition_types[] = {
|
|
|
|
"ofpart",
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
2017-05-23 13:30:20 +08:00
|
|
|
static int mtd_part_do_parse(struct mtd_part_parser *parser,
|
|
|
|
struct mtd_info *master,
|
|
|
|
struct mtd_partitions *pparts,
|
|
|
|
struct mtd_part_parser_data *data)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = (*parser->parse_fn)(master, &pparts->parts, data);
|
|
|
|
pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
|
|
|
|
if (ret <= 0)
|
|
|
|
return ret;
|
|
|
|
|
|
|
|
pr_notice("%d %s partitions found on MTD device %s\n", ret,
|
|
|
|
parser->name, master->name);
|
|
|
|
|
|
|
|
pparts->nr_parts = ret;
|
|
|
|
pparts->parser = parser;
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2018-03-14 20:10:42 +08:00
|
|
|
/**
|
|
|
|
* mtd_part_get_compatible_parser - find MTD parser by a compatible string
|
|
|
|
*
|
|
|
|
* @compat: compatible string describing partitions in a device tree
|
|
|
|
*
|
|
|
|
* MTD parsers can specify supported partitions by providing a table of
|
|
|
|
* compatibility strings. This function finds a parser that advertises support
|
|
|
|
* for a passed value of "compatible".
|
|
|
|
*/
|
|
|
|
static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
|
|
|
|
{
|
|
|
|
struct mtd_part_parser *p, *ret = NULL;
|
|
|
|
|
|
|
|
spin_lock(&part_parser_lock);
|
|
|
|
|
|
|
|
list_for_each_entry(p, &part_parsers, list) {
|
|
|
|
const struct of_device_id *matches;
|
|
|
|
|
|
|
|
matches = p->of_match_table;
|
|
|
|
if (!matches)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for (; matches->compatible[0]; matches++) {
|
|
|
|
if (!strcmp(matches->compatible, compat) &&
|
|
|
|
try_module_get(p->owner)) {
|
|
|
|
ret = p;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ret)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
spin_unlock(&part_parser_lock);
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int mtd_part_of_parse(struct mtd_info *master,
|
|
|
|
struct mtd_partitions *pparts)
|
|
|
|
{
|
|
|
|
struct mtd_part_parser *parser;
|
|
|
|
struct device_node *np;
|
|
|
|
struct property *prop;
|
|
|
|
const char *compat;
|
2018-03-14 20:10:43 +08:00
|
|
|
const char *fixed = "fixed-partitions";
|
2018-03-14 20:10:42 +08:00
|
|
|
int ret, err = 0;
|
|
|
|
|
2018-07-13 22:32:21 +08:00
|
|
|
np = mtd_get_of_node(master);
|
2018-09-07 22:35:54 +08:00
|
|
|
if (mtd_is_partition(master))
|
|
|
|
of_node_get(np);
|
|
|
|
else
|
2018-07-13 22:32:21 +08:00
|
|
|
np = of_get_child_by_name(np, "partitions");
|
2018-09-07 22:35:54 +08:00
|
|
|
|
2018-03-14 20:10:42 +08:00
|
|
|
of_property_for_each_string(np, "compatible", prop, compat) {
|
|
|
|
parser = mtd_part_get_compatible_parser(compat);
|
|
|
|
if (!parser)
|
|
|
|
continue;
|
|
|
|
ret = mtd_part_do_parse(parser, master, pparts, NULL);
|
|
|
|
if (ret > 0) {
|
|
|
|
of_node_put(np);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
mtd_part_parser_put(parser);
|
|
|
|
if (ret < 0 && !err)
|
|
|
|
err = ret;
|
|
|
|
}
|
|
|
|
of_node_put(np);
|
|
|
|
|
|
|
|
/*
|
2018-03-14 20:10:43 +08:00
|
|
|
* For backward compatibility we have to try the "fixed-partitions"
|
2018-03-14 20:10:42 +08:00
|
|
|
* parser. It supports old DT format with partitions specified as a
|
|
|
|
* direct subnodes of a flash device DT node without any compatibility
|
|
|
|
* specified we could match.
|
|
|
|
*/
|
|
|
|
parser = mtd_part_parser_get(fixed);
|
|
|
|
if (!parser && !request_module("%s", fixed))
|
|
|
|
parser = mtd_part_parser_get(fixed);
|
|
|
|
if (parser) {
|
|
|
|
ret = mtd_part_do_parse(parser, master, pparts, NULL);
|
|
|
|
if (ret > 0)
|
|
|
|
return ret;
|
|
|
|
mtd_part_parser_put(parser);
|
|
|
|
if (ret < 0 && !err)
|
|
|
|
err = ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2011-06-08 16:42:27 +08:00
|
|
|
/**
|
2018-03-28 04:35:41 +08:00
|
|
|
* parse_mtd_partitions - parse and register MTD partitions
|
|
|
|
*
|
2011-06-08 16:42:27 +08:00
|
|
|
* @master: the master partition (describes whole MTD device)
|
|
|
|
* @types: names of partition parsers to try or %NULL
|
2011-06-10 22:18:28 +08:00
|
|
|
* @data: MTD partition parser-specific data
|
2011-06-08 16:42:27 +08:00
|
|
|
*
|
2018-03-28 04:35:41 +08:00
|
|
|
* This function tries to find & register partitions on MTD device @master. It
|
|
|
|
* uses MTD partition parsers, specified in @types. However, if @types is %NULL,
|
|
|
|
* then the default list of parsers is used. The default list contains only the
|
2011-05-30 01:32:33 +08:00
|
|
|
* "cmdlinepart" and "ofpart" parsers ATM.
|
2012-08-19 01:07:41 +08:00
|
|
|
* Note: If there are more then one parser in @types, the kernel only takes the
|
|
|
|
* partitions parsed out by the first parser.
|
2011-06-08 16:42:27 +08:00
|
|
|
*
|
|
|
|
* This function may return:
|
|
|
|
* o a negative error code in case of failure
|
2018-03-28 04:35:41 +08:00
|
|
|
* o number of found partitions otherwise
|
2011-06-08 16:42:27 +08:00
|
|
|
*/
|
2013-03-11 21:38:48 +08:00
|
|
|
int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
|
2011-06-10 22:18:28 +08:00
|
|
|
struct mtd_part_parser_data *data)
|
2005-04-17 06:20:36 +08:00
|
|
|
{
|
2018-03-28 04:35:41 +08:00
|
|
|
struct mtd_partitions pparts = { };
|
2005-04-17 06:20:36 +08:00
|
|
|
struct mtd_part_parser *parser;
|
2015-10-12 04:03:47 +08:00
|
|
|
int ret, err = 0;
|
2005-11-07 19:15:26 +08:00
|
|
|
|
2011-06-02 22:51:16 +08:00
|
|
|
if (!types)
|
2018-07-13 22:32:21 +08:00
|
|
|
types = mtd_is_partition(master) ? default_subpartition_types :
|
|
|
|
default_mtd_part_types;
|
2011-06-02 22:51:16 +08:00
|
|
|
|
2015-10-12 04:03:47 +08:00
|
|
|
for ( ; *types; types++) {
|
2018-03-14 20:10:42 +08:00
|
|
|
/*
|
|
|
|
* ofpart is a special type that means OF partitioning info
|
|
|
|
* should be used. It requires a bit different logic so it is
|
|
|
|
* handled in a separated function.
|
|
|
|
*/
|
|
|
|
if (!strcmp(*types, "ofpart")) {
|
2018-03-28 04:35:41 +08:00
|
|
|
ret = mtd_part_of_parse(master, &pparts);
|
2018-03-14 20:10:42 +08:00
|
|
|
} else {
|
|
|
|
pr_debug("%s: parsing partitions %s\n", master->name,
|
|
|
|
*types);
|
2015-12-05 07:25:15 +08:00
|
|
|
parser = mtd_part_parser_get(*types);
|
2018-03-14 20:10:42 +08:00
|
|
|
if (!parser && !request_module("%s", *types))
|
|
|
|
parser = mtd_part_parser_get(*types);
|
|
|
|
pr_debug("%s: got parser %s\n", master->name,
|
|
|
|
parser ? parser->name : NULL);
|
|
|
|
if (!parser)
|
|
|
|
continue;
|
2018-03-28 04:35:41 +08:00
|
|
|
ret = mtd_part_do_parse(parser, master, &pparts, data);
|
2018-03-14 20:10:42 +08:00
|
|
|
if (ret <= 0)
|
|
|
|
mtd_part_parser_put(parser);
|
|
|
|
}
|
2017-05-23 13:30:20 +08:00
|
|
|
/* Found partitions! */
|
2018-03-28 04:35:41 +08:00
|
|
|
if (ret > 0) {
|
|
|
|
err = add_mtd_partitions(master, pparts.parts,
|
|
|
|
pparts.nr_parts);
|
|
|
|
mtd_part_parser_cleanup(&pparts);
|
|
|
|
return err ? err : pparts.nr_parts;
|
|
|
|
}
|
2015-10-12 04:03:47 +08:00
|
|
|
/*
|
|
|
|
* Stash the first error we see; only report it if no parser
|
|
|
|
* succeeds
|
|
|
|
*/
|
|
|
|
if (ret < 0 && !err)
|
|
|
|
err = ret;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2015-10-12 04:03:47 +08:00
|
|
|
return err;
|
2005-04-17 06:20:36 +08:00
|
|
|
}
|
2010-09-17 18:31:41 +08:00
|
|
|
|
2015-12-10 02:24:03 +08:00
|
|
|
void mtd_part_parser_cleanup(struct mtd_partitions *parts)
|
|
|
|
{
|
|
|
|
const struct mtd_part_parser *parser;
|
|
|
|
|
|
|
|
if (!parts)
|
|
|
|
return;
|
|
|
|
|
|
|
|
parser = parts->parser;
|
|
|
|
if (parser) {
|
|
|
|
if (parser->cleanup)
|
|
|
|
parser->cleanup(parts->parts, parts->nr_parts);
|
|
|
|
|
|
|
|
mtd_part_parser_put(parser);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-07-11 00:23:39 +08:00
|
|
|
int mtd_is_partition(const struct mtd_info *mtd)
|
2010-09-17 18:31:41 +08:00
|
|
|
{
|
|
|
|
struct mtd_part *part;
|
2010-11-23 20:17:17 +08:00
|
|
|
int ispart = 0;
|
2010-09-17 18:31:41 +08:00
|
|
|
|
|
|
|
mutex_lock(&mtd_partitions_mutex);
|
|
|
|
list_for_each_entry(part, &mtd_partitions, list)
|
|
|
|
if (&part->mtd == mtd) {
|
2010-11-23 20:17:17 +08:00
|
|
|
ispart = 1;
|
2010-09-17 18:31:41 +08:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
mutex_unlock(&mtd_partitions_mutex);
|
|
|
|
|
2010-11-23 20:17:17 +08:00
|
|
|
return ispart;
|
2010-09-17 18:31:41 +08:00
|
|
|
}
|
2010-11-23 20:17:17 +08:00
|
|
|
EXPORT_SYMBOL_GPL(mtd_is_partition);
|
2012-07-11 00:23:40 +08:00
|
|
|
|
|
|
|
/* Returns the size of the entire flash chip */
|
|
|
|
uint64_t mtd_get_device_size(const struct mtd_info *mtd)
|
|
|
|
{
|
|
|
|
if (!mtd_is_partition(mtd))
|
|
|
|
return mtd->size;
|
|
|
|
|
2017-06-21 14:26:45 +08:00
|
|
|
return mtd_get_device_size(mtd_to_part(mtd)->parent);
|
2012-07-11 00:23:40 +08:00
|
|
|
}
|
|
|
|
EXPORT_SYMBOL_GPL(mtd_get_device_size);
|