blob: be088bccd593142bd063955e29bdd84a4998d087 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Simple MTD partitioning layer
3 *
David Woodhousea1452a32010-08-08 20:58:20 +01004 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
5 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
6 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
David Woodhousea1452a32010-08-08 20:58:20 +01008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
David Woodhousea1452a32010-08-08 20:58:20 +010013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
Thomas Gleixner97894cd2005-11-07 11:15:26 +000022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/kernel.h>
27#include <linux/slab.h>
28#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kmod.h>
30#include <linux/mtd/mtd.h>
31#include <linux/mtd/partitions.h>
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +030032#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jamie Ileseea72d52011-05-23 10:23:42 +010034#include "mtdcore.h"
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* Our partition linked list */
37static LIST_HEAD(mtd_partitions);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +030038static DEFINE_MUTEX(mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020040/**
41 * struct mtd_part - our partition node structure
42 *
43 * @mtd: struct holding partition details
44 * @parent: parent mtd - flash device or another partition
45 * @offset: partition offset relative to the *flash device*
46 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct mtd_part {
48 struct mtd_info mtd;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020049 struct mtd_info *parent;
Adrian Hunter69423d92008-12-10 13:37:21 +000050 uint64_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
54/*
55 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
Brian Norris25245342015-11-19 19:28:39 -080056 * the pointer to that structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 */
Brian Norris25245342015-11-19 19:28:39 -080058static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
59{
60 return container_of(mtd, struct mtd_part, mtd);
61}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Thomas Gleixner97894cd2005-11-07 11:15:26 +000063
64/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * MTD methods which simply translate the effective address and pass through
66 * to the _real_ device.
67 */
68
Atsushi Nemotob33a2882008-07-19 01:00:33 +090069static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
70 size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Brian Norris25245342015-11-19 19:28:39 -080072 struct mtd_part *part = mtd_to_part(mtd);
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020073 struct mtd_ecc_stats stats;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020074 int res;
75
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020076 stats = part->parent->ecc_stats;
77 res = part->parent->_read(part->parent, from + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -080078 retlen, buf);
Mike Dunnedbc45402012-04-25 12:06:11 -070079 if (unlikely(mtd_is_eccerr(res)))
80 mtd->ecc_stats.failed +=
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020081 part->parent->ecc_stats.failed - stats.failed;
Mike Dunnedbc45402012-04-25 12:06:11 -070082 else
83 mtd->ecc_stats.corrected +=
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020084 part->parent->ecc_stats.corrected - stats.corrected;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020085 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Atsushi Nemotob33a2882008-07-19 01:00:33 +090088static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
89 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Brian Norris25245342015-11-19 19:28:39 -080091 struct mtd_part *part = mtd_to_part(mtd);
Artem Bityutskiy5def4892012-02-03 16:23:52 +020092
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020093 return part->parent->_point(part->parent, from + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -080094 retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
Thomas Gleixner9223a452006-05-23 17:21:03 +020096
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +020097static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Brian Norris25245342015-11-19 19:28:39 -080099 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200101 return part->parent->_unpoint(part->parent, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200104static int part_read_oob(struct mtd_info *mtd, loff_t from,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900105 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Brian Norris25245342015-11-19 19:28:39 -0800107 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200108 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200109
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (from >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200111 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300112 if (ops->datbuf && from + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200113 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200114
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200115 /*
116 * If OOB is also requested, make sure that we do not read past the end
117 * of this partition.
118 */
119 if (ops->oobbuf) {
120 size_t len, pages;
121
Boris BREZILLON29f10582016-03-07 10:46:52 +0100122 len = mtd_oobavail(mtd, ops);
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200123 pages = mtd_div_by_ws(mtd->size, mtd);
124 pages -= mtd_div_by_ws(from, mtd);
125 if (ops->ooboffs + ops->ooblen > pages * len)
126 return -EINVAL;
127 }
128
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200129 res = part->parent->_read_oob(part->parent, from + part->offset, ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200130 if (unlikely(res)) {
Brian Norrisd57f40542011-09-20 18:34:25 -0700131 if (mtd_is_bitflip(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200132 mtd->ecc_stats.corrected++;
Brian Norrisd57f40542011-09-20 18:34:25 -0700133 if (mtd_is_eccerr(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200134 mtd->ecc_stats.failed++;
135 }
136 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137}
138
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900139static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
140 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141{
Brian Norris25245342015-11-19 19:28:39 -0800142 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200143 return part->parent->_read_user_prot_reg(part->parent, from, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800144 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145}
146
Christian Riesch4b78fc42014-01-28 09:29:44 +0100147static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
148 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000149{
Brian Norris25245342015-11-19 19:28:39 -0800150 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200151 return part->parent->_get_user_prot_info(part->parent, len, retlen,
Christian Riesch4b78fc42014-01-28 09:29:44 +0100152 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000153}
154
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900155static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
156 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157{
Brian Norris25245342015-11-19 19:28:39 -0800158 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200159 return part->parent->_read_fact_prot_reg(part->parent, from, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800160 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Christian Riesch4b78fc42014-01-28 09:29:44 +0100163static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
164 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000165{
Brian Norris25245342015-11-19 19:28:39 -0800166 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200167 return part->parent->_get_fact_prot_info(part->parent, len, retlen,
Christian Riesch4b78fc42014-01-28 09:29:44 +0100168 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000169}
170
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900171static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
172 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Brian Norris25245342015-11-19 19:28:39 -0800174 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200175 return part->parent->_write(part->parent, to + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800176 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900179static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
180 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000181{
Brian Norris25245342015-11-19 19:28:39 -0800182 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200183 return part->parent->_panic_write(part->parent, to + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800184 retlen, buf);
Richard Purdie388bbb02008-02-06 10:17:15 +0000185}
186
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200187static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900188 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189{
Brian Norris25245342015-11-19 19:28:39 -0800190 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200191
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200193 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300194 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200195 return -EINVAL;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200196 return part->parent->_write_oob(part->parent, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197}
198
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900199static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
200 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Brian Norris25245342015-11-19 19:28:39 -0800202 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200203 return part->parent->_write_user_prot_reg(part->parent, from, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800204 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900207static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
208 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000209{
Brian Norris25245342015-11-19 19:28:39 -0800210 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200211 return part->parent->_lock_user_prot_reg(part->parent, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000212}
213
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900214static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
215 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Brian Norris25245342015-11-19 19:28:39 -0800217 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200218 return part->parent->_writev(part->parent, vecs, count,
Mike Dunn994c8402012-03-03 13:13:06 -0800219 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900222static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Brian Norris25245342015-11-19 19:28:39 -0800224 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225 int ret;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200226
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 instr->addr += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200228 ret = part->parent->_erase(part->parent, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200229 if (ret) {
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300230 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Adrian Hunter74641d72007-03-08 12:20:12 +0200231 instr->fail_addr -= part->offset;
232 instr->addr -= part->offset;
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return ret;
235}
236
237void mtd_erase_callback(struct erase_info *instr)
238{
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200239 if (instr->mtd->_erase == part_erase) {
Brian Norris25245342015-11-19 19:28:39 -0800240 struct mtd_part *part = mtd_to_part(instr->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300242 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 instr->fail_addr -= part->offset;
244 instr->addr -= part->offset;
245 }
246 if (instr->callback)
247 instr->callback(instr);
248}
249EXPORT_SYMBOL_GPL(mtd_erase_callback);
250
Adrian Hunter69423d92008-12-10 13:37:21 +0000251static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
Brian Norris25245342015-11-19 19:28:39 -0800253 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200254 return part->parent->_lock(part->parent, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255}
256
Adrian Hunter69423d92008-12-10 13:37:21 +0000257static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Brian Norris25245342015-11-19 19:28:39 -0800259 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200260 return part->parent->_unlock(part->parent, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Richard Cochran99384242010-06-14 18:10:33 +0200263static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
264{
Brian Norris25245342015-11-19 19:28:39 -0800265 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200266 return part->parent->_is_locked(part->parent, ofs + part->offset, len);
Richard Cochran99384242010-06-14 18:10:33 +0200267}
268
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269static void part_sync(struct mtd_info *mtd)
270{
Brian Norris25245342015-11-19 19:28:39 -0800271 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200272 part->parent->_sync(part->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
275static int part_suspend(struct mtd_info *mtd)
276{
Brian Norris25245342015-11-19 19:28:39 -0800277 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200278 return part->parent->_suspend(part->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281static void part_resume(struct mtd_info *mtd)
282{
Brian Norris25245342015-11-19 19:28:39 -0800283 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200284 part->parent->_resume(part->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300287static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
288{
Brian Norris25245342015-11-19 19:28:39 -0800289 struct mtd_part *part = mtd_to_part(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300290 ofs += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200291 return part->parent->_block_isreserved(part->parent, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300292}
293
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900294static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
Brian Norris25245342015-11-19 19:28:39 -0800296 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 ofs += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200298 return part->parent->_block_isbad(part->parent, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299}
300
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900301static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Brian Norris25245342015-11-19 19:28:39 -0800303 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200304 int res;
305
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 ofs += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200307 res = part->parent->_block_markbad(part->parent, ofs);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200308 if (!res)
309 mtd->ecc_stats.badblocks++;
310 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Richard Weinberger5e149072016-09-21 11:43:56 +0200313static int part_get_device(struct mtd_info *mtd)
314{
315 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200316 return part->parent->_get_device(part->parent);
Richard Weinberger5e149072016-09-21 11:43:56 +0200317}
318
319static void part_put_device(struct mtd_info *mtd)
320{
321 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200322 part->parent->_put_device(part->parent);
Richard Weinberger5e149072016-09-21 11:43:56 +0200323}
324
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100325static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
326 struct mtd_oob_region *oobregion)
327{
328 struct mtd_part *part = mtd_to_part(mtd);
329
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200330 return mtd_ooblayout_ecc(part->parent, section, oobregion);
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100331}
332
333static int part_ooblayout_free(struct mtd_info *mtd, int section,
334 struct mtd_oob_region *oobregion)
335{
336 struct mtd_part *part = mtd_to_part(mtd);
337
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200338 return mtd_ooblayout_free(part->parent, section, oobregion);
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100339}
340
341static const struct mtd_ooblayout_ops part_ooblayout_ops = {
342 .ecc = part_ooblayout_ecc,
343 .free = part_ooblayout_free,
344};
345
Jeff Westfahl6080ef62017-01-10 13:30:17 -0600346static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
347{
348 struct mtd_part *part = mtd_to_part(mtd);
349
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200350 return part->parent->_max_bad_blocks(part->parent,
Jeff Westfahl6080ef62017-01-10 13:30:17 -0600351 ofs + part->offset, len);
352}
353
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300354static inline void free_partition(struct mtd_part *p)
355{
356 kfree(p->mtd.name);
357 kfree(p);
358}
359
Rafał Miłecki1a0915b2017-06-21 08:26:46 +0200360/**
361 * mtd_parse_part - parse MTD partition looking for subpartitions
362 *
363 * @slave: part that is supposed to be a container and should be parsed
364 * @types: NULL-terminated array with names of partition parsers to try
365 *
366 * Some partitions are kind of containers with extra subpartitions (volumes).
367 * There can be various formats of such containers. This function tries to use
368 * specified parsers to analyze given partition and registers found
369 * subpartitions on success.
370 */
371static int mtd_parse_part(struct mtd_part *slave, const char *const *types)
372{
373 struct mtd_partitions parsed;
374 int err;
375
376 err = parse_mtd_partitions(&slave->mtd, types, &parsed, NULL);
377 if (err)
378 return err;
379 else if (!parsed.nr_parts)
380 return -ENOENT;
381
382 err = add_mtd_partitions(&slave->mtd, parsed.parts, parsed.nr_parts);
383
384 mtd_part_parser_cleanup(&parsed);
385
386 return err;
387}
388
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200389static struct mtd_part *allocate_partition(struct mtd_info *parent,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300390 const struct mtd_partition *part, int partno,
391 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900392{
Brian Norrisc169e3d2017-06-22 14:09:18 -0700393 int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize :
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200394 parent->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900395 struct mtd_part *slave;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200396 u32 remainder;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300397 char *name;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200398 u64 tmp;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900399
400 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900401 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300402 name = kstrdup(part->name, GFP_KERNEL);
403 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900404 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200405 parent->name);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300406 kfree(name);
407 kfree(slave);
408 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900409 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900410
411 /* set up the MTD object for this partition */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200412 slave->mtd.type = parent->type;
413 slave->mtd.flags = parent->flags & ~part->mask_flags;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900414 slave->mtd.size = part->size;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200415 slave->mtd.writesize = parent->writesize;
416 slave->mtd.writebufsize = parent->writebufsize;
417 slave->mtd.oobsize = parent->oobsize;
418 slave->mtd.oobavail = parent->oobavail;
419 slave->mtd.subpage_sft = parent->subpage_sft;
420 slave->mtd.pairing = parent->pairing;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900421
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300422 slave->mtd.name = name;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200423 slave->mtd.owner = parent->owner;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900424
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700425 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
426 * concern for showing the same data in multiple partitions.
427 * However, it is very useful to have the master node present,
428 * so the MTD_PARTITIONED_MASTER option allows that. The master
429 * will have device nodes etc only if this is set, so make the
430 * parent conditional on that option. Note, this is a way to
431 * distinguish between the master and the partition in sysfs.
David Brownell1f24b5a2009-03-26 00:42:41 -0700432 */
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200433 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200434 &parent->dev :
435 parent->dev.parent;
Sascha Hauer42e94012017-02-09 11:50:24 +0100436 slave->mtd.dev.of_node = part->of_node;
David Brownell1f24b5a2009-03-26 00:42:41 -0700437
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200438 slave->mtd._read = part_read;
439 slave->mtd._write = part_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900440
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200441 if (parent->_panic_write)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200442 slave->mtd._panic_write = part_panic_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900443
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200444 if (parent->_point && parent->_unpoint) {
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200445 slave->mtd._point = part_point;
446 slave->mtd._unpoint = part_unpoint;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900447 }
448
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200449 if (parent->_read_oob)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200450 slave->mtd._read_oob = part_read_oob;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200451 if (parent->_write_oob)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200452 slave->mtd._write_oob = part_write_oob;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200453 if (parent->_read_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200454 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200455 if (parent->_read_fact_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200456 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200457 if (parent->_write_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200458 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200459 if (parent->_lock_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200460 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200461 if (parent->_get_user_prot_info)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200462 slave->mtd._get_user_prot_info = part_get_user_prot_info;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200463 if (parent->_get_fact_prot_info)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200464 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200465 if (parent->_sync)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200466 slave->mtd._sync = part_sync;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200467 if (!partno && !parent->dev.class && parent->_suspend &&
468 parent->_resume) {
Brian Norrisc169e3d2017-06-22 14:09:18 -0700469 slave->mtd._suspend = part_suspend;
470 slave->mtd._resume = part_resume;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900471 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200472 if (parent->_writev)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200473 slave->mtd._writev = part_writev;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200474 if (parent->_lock)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200475 slave->mtd._lock = part_lock;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200476 if (parent->_unlock)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200477 slave->mtd._unlock = part_unlock;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200478 if (parent->_is_locked)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200479 slave->mtd._is_locked = part_is_locked;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200480 if (parent->_block_isreserved)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300481 slave->mtd._block_isreserved = part_block_isreserved;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200482 if (parent->_block_isbad)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200483 slave->mtd._block_isbad = part_block_isbad;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200484 if (parent->_block_markbad)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200485 slave->mtd._block_markbad = part_block_markbad;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200486 if (parent->_max_bad_blocks)
Jeff Westfahl6080ef62017-01-10 13:30:17 -0600487 slave->mtd._max_bad_blocks = part_max_bad_blocks;
Richard Weinberger5e149072016-09-21 11:43:56 +0200488
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200489 if (parent->_get_device)
Richard Weinberger5e149072016-09-21 11:43:56 +0200490 slave->mtd._get_device = part_get_device;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200491 if (parent->_put_device)
Richard Weinberger5e149072016-09-21 11:43:56 +0200492 slave->mtd._put_device = part_put_device;
493
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200494 slave->mtd._erase = part_erase;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200495 slave->parent = parent;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900496 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900497
498 if (slave->offset == MTDPART_OFS_APPEND)
499 slave->offset = cur_offset;
500 if (slave->offset == MTDPART_OFS_NXTBLK) {
Chris Packham1eeef2d2017-06-09 15:58:31 +1200501 tmp = cur_offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900502 slave->offset = cur_offset;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200503 remainder = do_div(tmp, wr_alignment);
504 if (remainder) {
505 slave->offset += wr_alignment - remainder;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900506 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000507 "0x%012llx -> 0x%012llx\n", partno,
508 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900509 }
510 }
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400511 if (slave->offset == MTDPART_OFS_RETAIN) {
512 slave->offset = cur_offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200513 if (parent->size - slave->offset >= slave->mtd.size) {
514 slave->mtd.size = parent->size - slave->offset
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400515 - slave->mtd.size;
516 } else {
517 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200518 part->name, parent->size - slave->offset,
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400519 slave->mtd.size);
520 /* register to preserve ordering */
521 goto out_register;
522 }
523 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900524 if (slave->mtd.size == MTDPART_SIZ_FULL)
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200525 slave->mtd.size = parent->size - slave->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900526
Adrian Hunter69423d92008-12-10 13:37:21 +0000527 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
528 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900529
530 /* let's do some sanity checks */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200531 if (slave->offset >= parent->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900532 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900533 slave->offset = 0;
534 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900535 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900536 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900537 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900538 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200539 if (slave->offset + slave->mtd.size > parent->size) {
540 slave->mtd.size = parent->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000541 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200542 part->name, parent->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900543 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200544 if (parent->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900545 /* Deal with variable erase size stuff */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200546 int i, max = parent->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000547 u64 end = slave->offset + slave->mtd.size;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200548 struct mtd_erase_region_info *regions = parent->eraseregions;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900549
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900550 /* Find the first erase regions which is part of this
551 * partition. */
552 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900553 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900554 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700555 if (i > 0)
556 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900557
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900558 /* Pick biggest erasesize */
559 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900560 if (slave->mtd.erasesize < regions[i].erasesize) {
561 slave->mtd.erasesize = regions[i].erasesize;
562 }
563 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900564 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900565 } else {
566 /* Single erase size */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200567 slave->mtd.erasesize = parent->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900568 }
569
Boris Brezillon7e439682017-09-25 10:19:57 +0200570 /*
571 * Slave erasesize might differ from the master one if the master
572 * exposes several regions with different erasesize. Adjust
573 * wr_alignment accordingly.
574 */
575 if (!(slave->mtd.flags & MTD_NO_ERASE))
576 wr_alignment = slave->mtd.erasesize;
577
Chris Packham1eeef2d2017-06-09 15:58:31 +1200578 tmp = slave->offset;
579 remainder = do_div(tmp, wr_alignment);
580 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900581 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900582 /* FIXME: Let it be writable if it is on a boundary of
583 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900584 slave->mtd.flags &= ~MTD_WRITEABLE;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200585 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900586 part->name);
587 }
Chris Packham1eeef2d2017-06-09 15:58:31 +1200588
589 tmp = slave->mtd.size;
590 remainder = do_div(tmp, wr_alignment);
591 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900592 slave->mtd.flags &= ~MTD_WRITEABLE;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200593 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900594 part->name);
595 }
596
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100597 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200598 slave->mtd.ecc_step_size = parent->ecc_step_size;
599 slave->mtd.ecc_strength = parent->ecc_strength;
600 slave->mtd.bitflip_threshold = parent->bitflip_threshold;
Mike Dunnd062d4e2012-04-25 12:06:08 -0700601
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200602 if (parent->_block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000603 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900604
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900605 while (offs < slave->mtd.size) {
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200606 if (mtd_block_isreserved(parent, offs + slave->offset))
Ezequiel Garciafdf43a42014-03-21 08:57:44 -0300607 slave->mtd.ecc_stats.bbtblocks++;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200608 else if (mtd_block_isbad(parent, offs + slave->offset))
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900609 slave->mtd.ecc_stats.badblocks++;
610 offs += slave->mtd.erasesize;
611 }
612 }
613
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900614out_register:
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900615 return slave;
616}
617
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700618static ssize_t mtd_partition_offset_show(struct device *dev,
619 struct device_attribute *attr, char *buf)
620{
621 struct mtd_info *mtd = dev_get_drvdata(dev);
Brian Norris25245342015-11-19 19:28:39 -0800622 struct mtd_part *part = mtd_to_part(mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700623 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
624}
625
626static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
627
628static const struct attribute *mtd_partition_attrs[] = {
629 &dev_attr_offset.attr,
630 NULL
631};
632
633static int mtd_add_partition_attrs(struct mtd_part *new)
634{
635 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
636 if (ret)
637 printk(KERN_WARNING
638 "mtd: failed to create partition attrs, err=%d\n", ret);
639 return ret;
640}
641
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200642int mtd_add_partition(struct mtd_info *parent, const char *name,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300643 long long offset, long long length)
644{
645 struct mtd_partition part;
Dan Ehrenberg3a434f62015-04-02 15:15:12 -0700646 struct mtd_part *new;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300647 int ret = 0;
648
649 /* the direct offset is expected */
650 if (offset == MTDPART_OFS_APPEND ||
651 offset == MTDPART_OFS_NXTBLK)
652 return -EINVAL;
653
654 if (length == MTDPART_SIZ_FULL)
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200655 length = parent->size - offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300656
657 if (length <= 0)
658 return -EINVAL;
659
Brian Norris93867232015-11-11 16:47:52 -0800660 memset(&part, 0, sizeof(part));
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300661 part.name = name;
662 part.size = length;
663 part.offset = offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300664
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200665 new = allocate_partition(parent, &part, -1, offset);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300666 if (IS_ERR(new))
667 return PTR_ERR(new);
668
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300669 mutex_lock(&mtd_partitions_mutex);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300670 list_add(&new->list, &mtd_partitions);
671 mutex_unlock(&mtd_partitions_mutex);
672
673 add_mtd_device(&new->mtd);
674
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700675 mtd_add_partition_attrs(new);
676
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300677 return ret;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300678}
679EXPORT_SYMBOL_GPL(mtd_add_partition);
680
Rafał Miłecki08263a92017-06-21 08:26:42 +0200681/**
682 * __mtd_del_partition - delete MTD partition
683 *
684 * @priv: internal MTD struct for partition to be deleted
685 *
686 * This function must be called with the partitions mutex locked.
687 */
688static int __mtd_del_partition(struct mtd_part *priv)
689{
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200690 struct mtd_part *child, *next;
Rafał Miłecki08263a92017-06-21 08:26:42 +0200691 int err;
692
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200693 list_for_each_entry_safe(child, next, &mtd_partitions, list) {
694 if (child->parent == &priv->mtd) {
695 err = __mtd_del_partition(child);
696 if (err)
697 return err;
698 }
699 }
700
Rafał Miłeckic5ceaba2017-06-21 08:26:43 +0200701 sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);
702
Rafał Miłecki08263a92017-06-21 08:26:42 +0200703 err = del_mtd_device(&priv->mtd);
704 if (err)
705 return err;
706
707 list_del(&priv->list);
708 free_partition(priv);
709
710 return 0;
711}
712
713/*
714 * This function unregisters and destroy all slave MTD objects which are
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200715 * attached to the given MTD object.
Rafał Miłecki08263a92017-06-21 08:26:42 +0200716 */
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200717int del_mtd_partitions(struct mtd_info *mtd)
Rafał Miłecki08263a92017-06-21 08:26:42 +0200718{
719 struct mtd_part *slave, *next;
720 int ret, err = 0;
721
722 mutex_lock(&mtd_partitions_mutex);
723 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200724 if (slave->parent == mtd) {
Rafał Miłecki08263a92017-06-21 08:26:42 +0200725 ret = __mtd_del_partition(slave);
726 if (ret < 0)
727 err = ret;
728 }
729 mutex_unlock(&mtd_partitions_mutex);
730
731 return err;
732}
733
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200734int mtd_del_partition(struct mtd_info *mtd, int partno)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300735{
736 struct mtd_part *slave, *next;
737 int ret = -EINVAL;
738
739 mutex_lock(&mtd_partitions_mutex);
740 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200741 if ((slave->parent == mtd) &&
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300742 (slave->mtd.index == partno)) {
Rafał Miłecki08263a92017-06-21 08:26:42 +0200743 ret = __mtd_del_partition(slave);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300744 break;
745 }
746 mutex_unlock(&mtd_partitions_mutex);
747
748 return ret;
749}
750EXPORT_SYMBOL_GPL(mtd_del_partition);
751
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752/*
753 * This function, given a master MTD object and a partition table, creates
754 * and registers slave MTD objects which are bound to the master according to
755 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700756 *
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700757 * For historical reasons, this function's caller only registers the master
758 * if the MTD_PARTITIONED_MASTER config option is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 */
760
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000761int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700762 const struct mtd_partition *parts,
763 int nbparts)
764{
765 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000766 uint64_t cur_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 int i;
768
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900769 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770
771 for (i = 0; i < nbparts; i++) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300772 slave = allocate_partition(master, parts + i, i, cur_offset);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200773 if (IS_ERR(slave)) {
774 del_mtd_partitions(master);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300775 return PTR_ERR(slave);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200776 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300777
778 mutex_lock(&mtd_partitions_mutex);
779 list_add(&slave->list, &mtd_partitions);
780 mutex_unlock(&mtd_partitions_mutex);
781
782 add_mtd_device(&slave->mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700783 mtd_add_partition_attrs(slave);
Rafał Miłecki1a0915b2017-06-21 08:26:46 +0200784 if (parts[i].types)
785 mtd_parse_part(slave, parts[i].types);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788 }
789
790 return 0;
791}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
793static DEFINE_SPINLOCK(part_parser_lock);
794static LIST_HEAD(part_parsers);
795
Brian Norris5531ae42015-12-04 15:25:15 -0800796static struct mtd_part_parser *mtd_part_parser_get(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797{
Chris Malley71a928c2008-05-19 20:11:50 +0100798 struct mtd_part_parser *p, *ret = NULL;
799
Linus Torvalds1da177e2005-04-16 15:20:36 -0700800 spin_lock(&part_parser_lock);
801
Chris Malley71a928c2008-05-19 20:11:50 +0100802 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
804 ret = p;
805 break;
806 }
Chris Malley71a928c2008-05-19 20:11:50 +0100807
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 spin_unlock(&part_parser_lock);
809
810 return ret;
811}
812
Brian Norris5531ae42015-12-04 15:25:15 -0800813static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
814{
815 module_put(p->owner);
816}
Dmitry Eremin-Solenikov953b3bd2011-06-23 15:26:14 +0400817
Brian Norrisadc83bf2015-12-09 10:24:03 -0800818/*
819 * Many partition parsers just expected the core to kfree() all their data in
820 * one chunk. Do that by default.
821 */
822static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
823 int nr_parts)
824{
825 kfree(pparts);
826}
827
Brian Norrisb9eab012015-11-11 19:13:29 -0800828int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829{
Brian Norrisb9eab012015-11-11 19:13:29 -0800830 p->owner = owner;
831
Brian Norrisadc83bf2015-12-09 10:24:03 -0800832 if (!p->cleanup)
833 p->cleanup = &mtd_part_parser_cleanup_default;
834
Linus Torvalds1da177e2005-04-16 15:20:36 -0700835 spin_lock(&part_parser_lock);
836 list_add(&p->list, &part_parsers);
837 spin_unlock(&part_parser_lock);
Brian Norrisb9eab012015-11-11 19:13:29 -0800838
839 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840}
Brian Norrisb9eab012015-11-11 19:13:29 -0800841EXPORT_SYMBOL_GPL(__register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842
Axel Lincf3b2b12013-12-01 18:59:15 +0800843void deregister_mtd_parser(struct mtd_part_parser *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844{
845 spin_lock(&part_parser_lock);
846 list_del(&p->list);
847 spin_unlock(&part_parser_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900849EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300851/*
852 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
853 * are changing this array!
854 */
Artem Bityutskiyccef4dc2013-03-12 10:51:35 +0200855static const char * const default_mtd_part_types[] = {
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400856 "cmdlinepart",
857 "ofpart",
858 NULL
859};
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400860
Brian Norris01f9c722017-05-23 07:30:20 +0200861static int mtd_part_do_parse(struct mtd_part_parser *parser,
862 struct mtd_info *master,
863 struct mtd_partitions *pparts,
864 struct mtd_part_parser_data *data)
865{
866 int ret;
867
868 ret = (*parser->parse_fn)(master, &pparts->parts, data);
869 pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
870 if (ret <= 0)
871 return ret;
872
873 pr_notice("%d %s partitions found on MTD device %s\n", ret,
874 parser->name, master->name);
875
876 pparts->nr_parts = ret;
877 pparts->parser = parser;
878
879 return ret;
880}
881
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300882/**
883 * parse_mtd_partitions - parse MTD partitions
884 * @master: the master partition (describes whole MTD device)
885 * @types: names of partition parsers to try or %NULL
Brian Norris07fd2f82015-12-04 15:25:17 -0800886 * @pparts: info about partitions found is returned here
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400887 * @data: MTD partition parser-specific data
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300888 *
889 * This function tries to find partition on MTD device @master. It uses MTD
890 * partition parsers, specified in @types. However, if @types is %NULL, then
891 * the default list of parsers is used. The default list contains only the
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400892 * "cmdlinepart" and "ofpart" parsers ATM.
Huang Shijiec51803d2012-08-18 13:07:41 -0400893 * Note: If there are more then one parser in @types, the kernel only takes the
894 * partitions parsed out by the first parser.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300895 *
896 * This function may return:
897 * o a negative error code in case of failure
Brian Norris07fd2f82015-12-04 15:25:17 -0800898 * o zero otherwise, and @pparts will describe the partitions, number of
Brian Norrisadc83bf2015-12-09 10:24:03 -0800899 * partitions, and the parser which parsed them. Caller must release
900 * resources with mtd_part_parser_cleanup() when finished with the returned
901 * data.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300902 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200903int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
Brian Norris07fd2f82015-12-04 15:25:17 -0800904 struct mtd_partitions *pparts,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400905 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906{
907 struct mtd_part_parser *parser;
Brian Norris5a2415b2015-10-11 13:03:47 -0700908 int ret, err = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000909
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400910 if (!types)
911 types = default_mtd_part_types;
912
Brian Norris5a2415b2015-10-11 13:03:47 -0700913 for ( ; *types; types++) {
Michal Suchanek8e2c9922015-08-18 15:34:07 +0000914 pr_debug("%s: parsing partitions %s\n", master->name, *types);
Brian Norris5531ae42015-12-04 15:25:15 -0800915 parser = mtd_part_parser_get(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700916 if (!parser && !request_module("%s", *types))
Brian Norris5531ae42015-12-04 15:25:15 -0800917 parser = mtd_part_parser_get(*types);
Michal Suchanek8e2c9922015-08-18 15:34:07 +0000918 pr_debug("%s: got parser %s\n", master->name,
919 parser ? parser->name : NULL);
Artem Bityutskiy7c802fb2011-05-17 11:13:17 +0300920 if (!parser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700921 continue;
Brian Norris01f9c722017-05-23 07:30:20 +0200922 ret = mtd_part_do_parse(parser, master, pparts, data);
923 /* Found partitions! */
924 if (ret > 0)
Brian Norris07fd2f82015-12-04 15:25:17 -0800925 return 0;
Brian Norrisadc83bf2015-12-09 10:24:03 -0800926 mtd_part_parser_put(parser);
Brian Norris5a2415b2015-10-11 13:03:47 -0700927 /*
928 * Stash the first error we see; only report it if no parser
929 * succeeds
930 */
931 if (ret < 0 && !err)
932 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 }
Brian Norris5a2415b2015-10-11 13:03:47 -0700934 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935}
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300936
Brian Norrisadc83bf2015-12-09 10:24:03 -0800937void mtd_part_parser_cleanup(struct mtd_partitions *parts)
938{
939 const struct mtd_part_parser *parser;
940
941 if (!parts)
942 return;
943
944 parser = parts->parser;
945 if (parser) {
946 if (parser->cleanup)
947 parser->cleanup(parts->parts, parts->nr_parts);
948
949 mtd_part_parser_put(parser);
950 }
951}
952
Richard Genoud5dee4672012-07-10 18:23:39 +0200953int mtd_is_partition(const struct mtd_info *mtd)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300954{
955 struct mtd_part *part;
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200956 int ispart = 0;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300957
958 mutex_lock(&mtd_partitions_mutex);
959 list_for_each_entry(part, &mtd_partitions, list)
960 if (&part->mtd == mtd) {
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200961 ispart = 1;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300962 break;
963 }
964 mutex_unlock(&mtd_partitions_mutex);
965
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200966 return ispart;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300967}
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200968EXPORT_SYMBOL_GPL(mtd_is_partition);
Richard Genoud62082e52012-07-10 18:23:40 +0200969
970/* Returns the size of the entire flash chip */
971uint64_t mtd_get_device_size(const struct mtd_info *mtd)
972{
973 if (!mtd_is_partition(mtd))
974 return mtd->size;
975
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200976 return mtd_get_device_size(mtd_to_part(mtd)->parent);
Richard Genoud62082e52012-07-10 18:23:40 +0200977}
978EXPORT_SYMBOL_GPL(mtd_get_device_size);