blob: 023516a632766c42df88c26ec2e504605f669639 [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>
Rafał Miłecki5b644aa2018-03-14 13:10:42 +010033#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
Jamie Ileseea72d52011-05-23 10:23:42 +010035#include "mtdcore.h"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037/* Our partition linked list */
38static LIST_HEAD(mtd_partitions);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +030039static DEFINE_MUTEX(mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020041/**
42 * struct mtd_part - our partition node structure
43 *
44 * @mtd: struct holding partition details
45 * @parent: parent mtd - flash device or another partition
46 * @offset: partition offset relative to the *flash device*
47 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070048struct mtd_part {
49 struct mtd_info mtd;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020050 struct mtd_info *parent;
Adrian Hunter69423d92008-12-10 13:37:21 +000051 uint64_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053};
54
55/*
56 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
Brian Norris25245342015-11-19 19:28:39 -080057 * the pointer to that structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070058 */
Brian Norris25245342015-11-19 19:28:39 -080059static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
60{
61 return container_of(mtd, struct mtd_part, mtd);
62}
Linus Torvalds1da177e2005-04-16 15:20:36 -070063
Thomas Gleixner97894cd2005-11-07 11:15:26 +000064
65/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 * MTD methods which simply translate the effective address and pass through
67 * to the _real_ device.
68 */
69
Atsushi Nemotob33a2882008-07-19 01:00:33 +090070static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
71 size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Brian Norris25245342015-11-19 19:28:39 -080073 struct mtd_part *part = mtd_to_part(mtd);
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020074 struct mtd_ecc_stats stats;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020075 int res;
76
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020077 stats = part->parent->ecc_stats;
78 res = part->parent->_read(part->parent, from + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -080079 retlen, buf);
Mike Dunnedbc45402012-04-25 12:06:11 -070080 if (unlikely(mtd_is_eccerr(res)))
81 mtd->ecc_stats.failed +=
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020082 part->parent->ecc_stats.failed - stats.failed;
Mike Dunnedbc45402012-04-25 12:06:11 -070083 else
84 mtd->ecc_stats.corrected +=
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020085 part->parent->ecc_stats.corrected - stats.corrected;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020086 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087}
88
Atsushi Nemotob33a2882008-07-19 01:00:33 +090089static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
90 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070091{
Brian Norris25245342015-11-19 19:28:39 -080092 struct mtd_part *part = mtd_to_part(mtd);
Artem Bityutskiy5def4892012-02-03 16:23:52 +020093
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020094 return part->parent->_point(part->parent, from + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -080095 retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
Thomas Gleixner9223a452006-05-23 17:21:03 +020097
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +020098static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070099{
Brian Norris25245342015-11-19 19:28:39 -0800100 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200102 return part->parent->_unpoint(part->parent, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103}
104
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200105static int part_read_oob(struct mtd_info *mtd, loff_t from,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900106 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107{
Brian Norris25245342015-11-19 19:28:39 -0800108 struct mtd_part *part = mtd_to_part(mtd);
Boris Brezillond020fc82018-01-09 09:50:33 +0100109 struct mtd_ecc_stats stats;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200110 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200111
Boris Brezillond020fc82018-01-09 09:50:33 +0100112 stats = part->parent->ecc_stats;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200113 res = part->parent->_read_oob(part->parent, from + part->offset, ops);
Boris Brezillond020fc82018-01-09 09:50:33 +0100114 if (unlikely(mtd_is_eccerr(res)))
115 mtd->ecc_stats.failed +=
116 part->parent->ecc_stats.failed - stats.failed;
117 else
118 mtd->ecc_stats.corrected +=
119 part->parent->ecc_stats.corrected - stats.corrected;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200120 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900123static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
124 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Brian Norris25245342015-11-19 19:28:39 -0800126 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200127 return part->parent->_read_user_prot_reg(part->parent, from, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800128 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129}
130
Christian Riesch4b78fc42014-01-28 09:29:44 +0100131static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
132 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000133{
Brian Norris25245342015-11-19 19:28:39 -0800134 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200135 return part->parent->_get_user_prot_info(part->parent, len, retlen,
Christian Riesch4b78fc42014-01-28 09:29:44 +0100136 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000137}
138
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900139static int part_read_fact_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_fact_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_fact_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_fact_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_write(struct mtd_info *mtd, loff_t to, size_t len,
156 size_t *retlen, const 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->_write(part->parent, to + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800160 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900163static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
164 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +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->_panic_write(part->parent, to + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800168 retlen, buf);
Richard Purdie388bbb02008-02-06 10:17:15 +0000169}
170
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200171static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900172 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173{
Brian Norris25245342015-11-19 19:28:39 -0800174 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200175
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200176 return part->parent->_write_oob(part->parent, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900179static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
180 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
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->_write_user_prot_reg(part->parent, from, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800184 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900187static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
188 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000189{
Brian Norris25245342015-11-19 19:28:39 -0800190 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200191 return part->parent->_lock_user_prot_reg(part->parent, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000192}
193
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900194static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
195 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Brian Norris25245342015-11-19 19:28:39 -0800197 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200198 return part->parent->_writev(part->parent, vecs, count,
Mike Dunn994c8402012-03-03 13:13:06 -0800199 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200}
201
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900202static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203{
Brian Norris25245342015-11-19 19:28:39 -0800204 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 int ret;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200206
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 instr->addr += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200208 ret = part->parent->_erase(part->parent, instr);
Boris Brezillon8f347c42018-02-12 22:03:10 +0100209 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
210 instr->fail_addr -= part->offset;
211 instr->addr -= part->offset;
212
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 return ret;
214}
215
Adrian Hunter69423d92008-12-10 13:37:21 +0000216static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217{
Brian Norris25245342015-11-19 19:28:39 -0800218 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200219 return part->parent->_lock(part->parent, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Adrian Hunter69423d92008-12-10 13:37:21 +0000222static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Brian Norris25245342015-11-19 19:28:39 -0800224 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200225 return part->parent->_unlock(part->parent, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Richard Cochran99384242010-06-14 18:10:33 +0200228static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
229{
Brian Norris25245342015-11-19 19:28:39 -0800230 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200231 return part->parent->_is_locked(part->parent, ofs + part->offset, len);
Richard Cochran99384242010-06-14 18:10:33 +0200232}
233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234static void part_sync(struct mtd_info *mtd)
235{
Brian Norris25245342015-11-19 19:28:39 -0800236 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200237 part->parent->_sync(part->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238}
239
240static int part_suspend(struct mtd_info *mtd)
241{
Brian Norris25245342015-11-19 19:28:39 -0800242 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200243 return part->parent->_suspend(part->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244}
245
246static void part_resume(struct mtd_info *mtd)
247{
Brian Norris25245342015-11-19 19:28:39 -0800248 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200249 part->parent->_resume(part->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250}
251
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300252static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
253{
Brian Norris25245342015-11-19 19:28:39 -0800254 struct mtd_part *part = mtd_to_part(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300255 ofs += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200256 return part->parent->_block_isreserved(part->parent, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300257}
258
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900259static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
Brian Norris25245342015-11-19 19:28:39 -0800261 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 ofs += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200263 return part->parent->_block_isbad(part->parent, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900266static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267{
Brian Norris25245342015-11-19 19:28:39 -0800268 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200269 int res;
270
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 ofs += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200272 res = part->parent->_block_markbad(part->parent, ofs);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200273 if (!res)
274 mtd->ecc_stats.badblocks++;
275 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276}
277
Richard Weinberger5e149072016-09-21 11:43:56 +0200278static int part_get_device(struct mtd_info *mtd)
279{
280 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200281 return part->parent->_get_device(part->parent);
Richard Weinberger5e149072016-09-21 11:43:56 +0200282}
283
284static void part_put_device(struct mtd_info *mtd)
285{
286 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200287 part->parent->_put_device(part->parent);
Richard Weinberger5e149072016-09-21 11:43:56 +0200288}
289
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100290static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
291 struct mtd_oob_region *oobregion)
292{
293 struct mtd_part *part = mtd_to_part(mtd);
294
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200295 return mtd_ooblayout_ecc(part->parent, section, oobregion);
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100296}
297
298static int part_ooblayout_free(struct mtd_info *mtd, int section,
299 struct mtd_oob_region *oobregion)
300{
301 struct mtd_part *part = mtd_to_part(mtd);
302
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200303 return mtd_ooblayout_free(part->parent, section, oobregion);
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100304}
305
306static const struct mtd_ooblayout_ops part_ooblayout_ops = {
307 .ecc = part_ooblayout_ecc,
308 .free = part_ooblayout_free,
309};
310
Jeff Westfahl6080ef62017-01-10 13:30:17 -0600311static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
312{
313 struct mtd_part *part = mtd_to_part(mtd);
314
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200315 return part->parent->_max_bad_blocks(part->parent,
Jeff Westfahl6080ef62017-01-10 13:30:17 -0600316 ofs + part->offset, len);
317}
318
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300319static inline void free_partition(struct mtd_part *p)
320{
321 kfree(p->mtd.name);
322 kfree(p);
323}
324
Rafał Miłecki1a0915b2017-06-21 08:26:46 +0200325/**
326 * mtd_parse_part - parse MTD partition looking for subpartitions
327 *
328 * @slave: part that is supposed to be a container and should be parsed
329 * @types: NULL-terminated array with names of partition parsers to try
330 *
331 * Some partitions are kind of containers with extra subpartitions (volumes).
332 * There can be various formats of such containers. This function tries to use
333 * specified parsers to analyze given partition and registers found
334 * subpartitions on success.
335 */
336static int mtd_parse_part(struct mtd_part *slave, const char *const *types)
337{
338 struct mtd_partitions parsed;
339 int err;
340
341 err = parse_mtd_partitions(&slave->mtd, types, &parsed, NULL);
342 if (err)
343 return err;
344 else if (!parsed.nr_parts)
345 return -ENOENT;
346
347 err = add_mtd_partitions(&slave->mtd, parsed.parts, parsed.nr_parts);
348
349 mtd_part_parser_cleanup(&parsed);
350
351 return err;
352}
353
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200354static struct mtd_part *allocate_partition(struct mtd_info *parent,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300355 const struct mtd_partition *part, int partno,
356 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900357{
Brian Norrisc169e3d2017-06-22 14:09:18 -0700358 int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize :
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200359 parent->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900360 struct mtd_part *slave;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200361 u32 remainder;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300362 char *name;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200363 u64 tmp;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900364
365 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900366 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300367 name = kstrdup(part->name, GFP_KERNEL);
368 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900369 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200370 parent->name);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300371 kfree(name);
372 kfree(slave);
373 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900374 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900375
376 /* set up the MTD object for this partition */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200377 slave->mtd.type = parent->type;
378 slave->mtd.flags = parent->flags & ~part->mask_flags;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900379 slave->mtd.size = part->size;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200380 slave->mtd.writesize = parent->writesize;
381 slave->mtd.writebufsize = parent->writebufsize;
382 slave->mtd.oobsize = parent->oobsize;
383 slave->mtd.oobavail = parent->oobavail;
384 slave->mtd.subpage_sft = parent->subpage_sft;
385 slave->mtd.pairing = parent->pairing;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900386
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300387 slave->mtd.name = name;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200388 slave->mtd.owner = parent->owner;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900389
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700390 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
391 * concern for showing the same data in multiple partitions.
392 * However, it is very useful to have the master node present,
393 * so the MTD_PARTITIONED_MASTER option allows that. The master
394 * will have device nodes etc only if this is set, so make the
395 * parent conditional on that option. Note, this is a way to
396 * distinguish between the master and the partition in sysfs.
David Brownell1f24b5a2009-03-26 00:42:41 -0700397 */
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200398 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200399 &parent->dev :
400 parent->dev.parent;
Sascha Hauer42e94012017-02-09 11:50:24 +0100401 slave->mtd.dev.of_node = part->of_node;
David Brownell1f24b5a2009-03-26 00:42:41 -0700402
Boris Brezillon24ff1292018-01-09 09:50:34 +0100403 if (parent->_read)
404 slave->mtd._read = part_read;
405 if (parent->_write)
406 slave->mtd._write = part_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900407
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200408 if (parent->_panic_write)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200409 slave->mtd._panic_write = part_panic_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900410
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200411 if (parent->_point && parent->_unpoint) {
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200412 slave->mtd._point = part_point;
413 slave->mtd._unpoint = part_unpoint;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900414 }
415
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200416 if (parent->_read_oob)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200417 slave->mtd._read_oob = part_read_oob;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200418 if (parent->_write_oob)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200419 slave->mtd._write_oob = part_write_oob;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200420 if (parent->_read_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200421 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200422 if (parent->_read_fact_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200423 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200424 if (parent->_write_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200425 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200426 if (parent->_lock_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200427 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200428 if (parent->_get_user_prot_info)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200429 slave->mtd._get_user_prot_info = part_get_user_prot_info;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200430 if (parent->_get_fact_prot_info)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200431 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200432 if (parent->_sync)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200433 slave->mtd._sync = part_sync;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200434 if (!partno && !parent->dev.class && parent->_suspend &&
435 parent->_resume) {
Brian Norrisc169e3d2017-06-22 14:09:18 -0700436 slave->mtd._suspend = part_suspend;
437 slave->mtd._resume = part_resume;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900438 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200439 if (parent->_writev)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200440 slave->mtd._writev = part_writev;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200441 if (parent->_lock)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200442 slave->mtd._lock = part_lock;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200443 if (parent->_unlock)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200444 slave->mtd._unlock = part_unlock;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200445 if (parent->_is_locked)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200446 slave->mtd._is_locked = part_is_locked;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200447 if (parent->_block_isreserved)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300448 slave->mtd._block_isreserved = part_block_isreserved;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200449 if (parent->_block_isbad)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200450 slave->mtd._block_isbad = part_block_isbad;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200451 if (parent->_block_markbad)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200452 slave->mtd._block_markbad = part_block_markbad;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200453 if (parent->_max_bad_blocks)
Jeff Westfahl6080ef62017-01-10 13:30:17 -0600454 slave->mtd._max_bad_blocks = part_max_bad_blocks;
Richard Weinberger5e149072016-09-21 11:43:56 +0200455
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200456 if (parent->_get_device)
Richard Weinberger5e149072016-09-21 11:43:56 +0200457 slave->mtd._get_device = part_get_device;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200458 if (parent->_put_device)
Richard Weinberger5e149072016-09-21 11:43:56 +0200459 slave->mtd._put_device = part_put_device;
460
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200461 slave->mtd._erase = part_erase;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200462 slave->parent = parent;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900463 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900464
465 if (slave->offset == MTDPART_OFS_APPEND)
466 slave->offset = cur_offset;
467 if (slave->offset == MTDPART_OFS_NXTBLK) {
Chris Packham1eeef2d2017-06-09 15:58:31 +1200468 tmp = cur_offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900469 slave->offset = cur_offset;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200470 remainder = do_div(tmp, wr_alignment);
471 if (remainder) {
472 slave->offset += wr_alignment - remainder;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900473 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000474 "0x%012llx -> 0x%012llx\n", partno,
475 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900476 }
477 }
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400478 if (slave->offset == MTDPART_OFS_RETAIN) {
479 slave->offset = cur_offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200480 if (parent->size - slave->offset >= slave->mtd.size) {
481 slave->mtd.size = parent->size - slave->offset
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400482 - slave->mtd.size;
483 } else {
484 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200485 part->name, parent->size - slave->offset,
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400486 slave->mtd.size);
487 /* register to preserve ordering */
488 goto out_register;
489 }
490 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900491 if (slave->mtd.size == MTDPART_SIZ_FULL)
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200492 slave->mtd.size = parent->size - slave->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900493
Adrian Hunter69423d92008-12-10 13:37:21 +0000494 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
495 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900496
497 /* let's do some sanity checks */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200498 if (slave->offset >= parent->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900499 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900500 slave->offset = 0;
501 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900502 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900503 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900504 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900505 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200506 if (slave->offset + slave->mtd.size > parent->size) {
507 slave->mtd.size = parent->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000508 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 +0200509 part->name, parent->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900510 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200511 if (parent->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900512 /* Deal with variable erase size stuff */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200513 int i, max = parent->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000514 u64 end = slave->offset + slave->mtd.size;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200515 struct mtd_erase_region_info *regions = parent->eraseregions;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900516
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900517 /* Find the first erase regions which is part of this
518 * partition. */
519 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900520 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900521 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700522 if (i > 0)
523 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900524
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900525 /* Pick biggest erasesize */
526 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900527 if (slave->mtd.erasesize < regions[i].erasesize) {
528 slave->mtd.erasesize = regions[i].erasesize;
529 }
530 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900531 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900532 } else {
533 /* Single erase size */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200534 slave->mtd.erasesize = parent->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900535 }
536
Boris Brezillon7e439682017-09-25 10:19:57 +0200537 /*
538 * Slave erasesize might differ from the master one if the master
539 * exposes several regions with different erasesize. Adjust
540 * wr_alignment accordingly.
541 */
542 if (!(slave->mtd.flags & MTD_NO_ERASE))
543 wr_alignment = slave->mtd.erasesize;
544
Chris Packham1eeef2d2017-06-09 15:58:31 +1200545 tmp = slave->offset;
546 remainder = do_div(tmp, wr_alignment);
547 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900548 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900549 /* FIXME: Let it be writable if it is on a boundary of
550 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900551 slave->mtd.flags &= ~MTD_WRITEABLE;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200552 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 +0900553 part->name);
554 }
Chris Packham1eeef2d2017-06-09 15:58:31 +1200555
556 tmp = slave->mtd.size;
557 remainder = do_div(tmp, wr_alignment);
558 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900559 slave->mtd.flags &= ~MTD_WRITEABLE;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200560 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 +0900561 part->name);
562 }
563
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100564 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200565 slave->mtd.ecc_step_size = parent->ecc_step_size;
566 slave->mtd.ecc_strength = parent->ecc_strength;
567 slave->mtd.bitflip_threshold = parent->bitflip_threshold;
Mike Dunnd062d4e2012-04-25 12:06:08 -0700568
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200569 if (parent->_block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000570 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900571
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900572 while (offs < slave->mtd.size) {
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200573 if (mtd_block_isreserved(parent, offs + slave->offset))
Ezequiel Garciafdf43a42014-03-21 08:57:44 -0300574 slave->mtd.ecc_stats.bbtblocks++;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200575 else if (mtd_block_isbad(parent, offs + slave->offset))
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900576 slave->mtd.ecc_stats.badblocks++;
577 offs += slave->mtd.erasesize;
578 }
579 }
580
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900581out_register:
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900582 return slave;
583}
584
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700585static ssize_t mtd_partition_offset_show(struct device *dev,
586 struct device_attribute *attr, char *buf)
587{
588 struct mtd_info *mtd = dev_get_drvdata(dev);
Brian Norris25245342015-11-19 19:28:39 -0800589 struct mtd_part *part = mtd_to_part(mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700590 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
591}
592
593static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
594
595static const struct attribute *mtd_partition_attrs[] = {
596 &dev_attr_offset.attr,
597 NULL
598};
599
600static int mtd_add_partition_attrs(struct mtd_part *new)
601{
602 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
603 if (ret)
604 printk(KERN_WARNING
605 "mtd: failed to create partition attrs, err=%d\n", ret);
606 return ret;
607}
608
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200609int mtd_add_partition(struct mtd_info *parent, const char *name,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300610 long long offset, long long length)
611{
612 struct mtd_partition part;
Dan Ehrenberg3a434f62015-04-02 15:15:12 -0700613 struct mtd_part *new;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300614 int ret = 0;
615
616 /* the direct offset is expected */
617 if (offset == MTDPART_OFS_APPEND ||
618 offset == MTDPART_OFS_NXTBLK)
619 return -EINVAL;
620
621 if (length == MTDPART_SIZ_FULL)
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200622 length = parent->size - offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300623
624 if (length <= 0)
625 return -EINVAL;
626
Brian Norris93867232015-11-11 16:47:52 -0800627 memset(&part, 0, sizeof(part));
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300628 part.name = name;
629 part.size = length;
630 part.offset = offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300631
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200632 new = allocate_partition(parent, &part, -1, offset);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300633 if (IS_ERR(new))
634 return PTR_ERR(new);
635
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300636 mutex_lock(&mtd_partitions_mutex);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300637 list_add(&new->list, &mtd_partitions);
638 mutex_unlock(&mtd_partitions_mutex);
639
640 add_mtd_device(&new->mtd);
641
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700642 mtd_add_partition_attrs(new);
643
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300644 return ret;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300645}
646EXPORT_SYMBOL_GPL(mtd_add_partition);
647
Rafał Miłecki08263a92017-06-21 08:26:42 +0200648/**
649 * __mtd_del_partition - delete MTD partition
650 *
651 * @priv: internal MTD struct for partition to be deleted
652 *
653 * This function must be called with the partitions mutex locked.
654 */
655static int __mtd_del_partition(struct mtd_part *priv)
656{
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200657 struct mtd_part *child, *next;
Rafał Miłecki08263a92017-06-21 08:26:42 +0200658 int err;
659
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200660 list_for_each_entry_safe(child, next, &mtd_partitions, list) {
661 if (child->parent == &priv->mtd) {
662 err = __mtd_del_partition(child);
663 if (err)
664 return err;
665 }
666 }
667
Rafał Miłeckic5ceaba2017-06-21 08:26:43 +0200668 sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);
669
Rafał Miłecki08263a92017-06-21 08:26:42 +0200670 err = del_mtd_device(&priv->mtd);
671 if (err)
672 return err;
673
674 list_del(&priv->list);
675 free_partition(priv);
676
677 return 0;
678}
679
680/*
681 * This function unregisters and destroy all slave MTD objects which are
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200682 * attached to the given MTD object.
Rafał Miłecki08263a92017-06-21 08:26:42 +0200683 */
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200684int del_mtd_partitions(struct mtd_info *mtd)
Rafał Miłecki08263a92017-06-21 08:26:42 +0200685{
686 struct mtd_part *slave, *next;
687 int ret, err = 0;
688
689 mutex_lock(&mtd_partitions_mutex);
690 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200691 if (slave->parent == mtd) {
Rafał Miłecki08263a92017-06-21 08:26:42 +0200692 ret = __mtd_del_partition(slave);
693 if (ret < 0)
694 err = ret;
695 }
696 mutex_unlock(&mtd_partitions_mutex);
697
698 return err;
699}
700
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200701int mtd_del_partition(struct mtd_info *mtd, int partno)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300702{
703 struct mtd_part *slave, *next;
704 int ret = -EINVAL;
705
706 mutex_lock(&mtd_partitions_mutex);
707 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200708 if ((slave->parent == mtd) &&
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300709 (slave->mtd.index == partno)) {
Rafał Miłecki08263a92017-06-21 08:26:42 +0200710 ret = __mtd_del_partition(slave);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300711 break;
712 }
713 mutex_unlock(&mtd_partitions_mutex);
714
715 return ret;
716}
717EXPORT_SYMBOL_GPL(mtd_del_partition);
718
Linus Torvalds1da177e2005-04-16 15:20:36 -0700719/*
720 * This function, given a master MTD object and a partition table, creates
721 * and registers slave MTD objects which are bound to the master according to
722 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700723 *
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700724 * For historical reasons, this function's caller only registers the master
725 * if the MTD_PARTITIONED_MASTER config option is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 */
727
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000728int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 const struct mtd_partition *parts,
730 int nbparts)
731{
732 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000733 uint64_t cur_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 int i;
735
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900736 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737
738 for (i = 0; i < nbparts; i++) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300739 slave = allocate_partition(master, parts + i, i, cur_offset);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200740 if (IS_ERR(slave)) {
741 del_mtd_partitions(master);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300742 return PTR_ERR(slave);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200743 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300744
745 mutex_lock(&mtd_partitions_mutex);
746 list_add(&slave->list, &mtd_partitions);
747 mutex_unlock(&mtd_partitions_mutex);
748
749 add_mtd_device(&slave->mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700750 mtd_add_partition_attrs(slave);
Rafał Miłecki1a0915b2017-06-21 08:26:46 +0200751 if (parts[i].types)
752 mtd_parse_part(slave, parts[i].types);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 }
756
757 return 0;
758}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759
760static DEFINE_SPINLOCK(part_parser_lock);
761static LIST_HEAD(part_parsers);
762
Brian Norris5531ae42015-12-04 15:25:15 -0800763static struct mtd_part_parser *mtd_part_parser_get(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764{
Chris Malley71a928c2008-05-19 20:11:50 +0100765 struct mtd_part_parser *p, *ret = NULL;
766
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 spin_lock(&part_parser_lock);
768
Chris Malley71a928c2008-05-19 20:11:50 +0100769 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700770 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
771 ret = p;
772 break;
773 }
Chris Malley71a928c2008-05-19 20:11:50 +0100774
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 spin_unlock(&part_parser_lock);
776
777 return ret;
778}
779
Brian Norris5531ae42015-12-04 15:25:15 -0800780static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
781{
782 module_put(p->owner);
783}
Dmitry Eremin-Solenikov953b3bd2011-06-23 15:26:14 +0400784
Brian Norrisadc83bf2015-12-09 10:24:03 -0800785/*
786 * Many partition parsers just expected the core to kfree() all their data in
787 * one chunk. Do that by default.
788 */
789static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
790 int nr_parts)
791{
792 kfree(pparts);
793}
794
Brian Norrisb9eab012015-11-11 19:13:29 -0800795int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
Brian Norrisb9eab012015-11-11 19:13:29 -0800797 p->owner = owner;
798
Brian Norrisadc83bf2015-12-09 10:24:03 -0800799 if (!p->cleanup)
800 p->cleanup = &mtd_part_parser_cleanup_default;
801
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 spin_lock(&part_parser_lock);
803 list_add(&p->list, &part_parsers);
804 spin_unlock(&part_parser_lock);
Brian Norrisb9eab012015-11-11 19:13:29 -0800805
806 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807}
Brian Norrisb9eab012015-11-11 19:13:29 -0800808EXPORT_SYMBOL_GPL(__register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809
Axel Lincf3b2b12013-12-01 18:59:15 +0800810void deregister_mtd_parser(struct mtd_part_parser *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
812 spin_lock(&part_parser_lock);
813 list_del(&p->list);
814 spin_unlock(&part_parser_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700815}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900816EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300818/*
819 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
820 * are changing this array!
821 */
Artem Bityutskiyccef4dc2013-03-12 10:51:35 +0200822static const char * const default_mtd_part_types[] = {
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400823 "cmdlinepart",
824 "ofpart",
825 NULL
826};
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400827
Brian Norris01f9c722017-05-23 07:30:20 +0200828static int mtd_part_do_parse(struct mtd_part_parser *parser,
829 struct mtd_info *master,
830 struct mtd_partitions *pparts,
831 struct mtd_part_parser_data *data)
832{
833 int ret;
834
835 ret = (*parser->parse_fn)(master, &pparts->parts, data);
836 pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
837 if (ret <= 0)
838 return ret;
839
840 pr_notice("%d %s partitions found on MTD device %s\n", ret,
841 parser->name, master->name);
842
843 pparts->nr_parts = ret;
844 pparts->parser = parser;
845
846 return ret;
847}
848
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300849/**
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100850 * mtd_part_get_compatible_parser - find MTD parser by a compatible string
851 *
852 * @compat: compatible string describing partitions in a device tree
853 *
854 * MTD parsers can specify supported partitions by providing a table of
855 * compatibility strings. This function finds a parser that advertises support
856 * for a passed value of "compatible".
857 */
858static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
859{
860 struct mtd_part_parser *p, *ret = NULL;
861
862 spin_lock(&part_parser_lock);
863
864 list_for_each_entry(p, &part_parsers, list) {
865 const struct of_device_id *matches;
866
867 matches = p->of_match_table;
868 if (!matches)
869 continue;
870
871 for (; matches->compatible[0]; matches++) {
872 if (!strcmp(matches->compatible, compat) &&
873 try_module_get(p->owner)) {
874 ret = p;
875 break;
876 }
877 }
878
879 if (ret)
880 break;
881 }
882
883 spin_unlock(&part_parser_lock);
884
885 return ret;
886}
887
888static int mtd_part_of_parse(struct mtd_info *master,
889 struct mtd_partitions *pparts)
890{
891 struct mtd_part_parser *parser;
892 struct device_node *np;
893 struct property *prop;
894 const char *compat;
Rafał Miłeckic0faf432018-03-14 13:10:43 +0100895 const char *fixed = "fixed-partitions";
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100896 int ret, err = 0;
897
898 np = of_get_child_by_name(mtd_get_of_node(master), "partitions");
899 of_property_for_each_string(np, "compatible", prop, compat) {
900 parser = mtd_part_get_compatible_parser(compat);
901 if (!parser)
902 continue;
903 ret = mtd_part_do_parse(parser, master, pparts, NULL);
904 if (ret > 0) {
905 of_node_put(np);
906 return ret;
907 }
908 mtd_part_parser_put(parser);
909 if (ret < 0 && !err)
910 err = ret;
911 }
912 of_node_put(np);
913
914 /*
Rafał Miłeckic0faf432018-03-14 13:10:43 +0100915 * For backward compatibility we have to try the "fixed-partitions"
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100916 * parser. It supports old DT format with partitions specified as a
917 * direct subnodes of a flash device DT node without any compatibility
918 * specified we could match.
919 */
920 parser = mtd_part_parser_get(fixed);
921 if (!parser && !request_module("%s", fixed))
922 parser = mtd_part_parser_get(fixed);
923 if (parser) {
924 ret = mtd_part_do_parse(parser, master, pparts, NULL);
925 if (ret > 0)
926 return ret;
927 mtd_part_parser_put(parser);
928 if (ret < 0 && !err)
929 err = ret;
930 }
931
932 return err;
933}
934
935/**
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300936 * parse_mtd_partitions - parse MTD partitions
937 * @master: the master partition (describes whole MTD device)
938 * @types: names of partition parsers to try or %NULL
Brian Norris07fd2f82015-12-04 15:25:17 -0800939 * @pparts: info about partitions found is returned here
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400940 * @data: MTD partition parser-specific data
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300941 *
942 * This function tries to find partition on MTD device @master. It uses MTD
943 * partition parsers, specified in @types. However, if @types is %NULL, then
944 * the default list of parsers is used. The default list contains only the
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400945 * "cmdlinepart" and "ofpart" parsers ATM.
Huang Shijiec51803d2012-08-18 13:07:41 -0400946 * Note: If there are more then one parser in @types, the kernel only takes the
947 * partitions parsed out by the first parser.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300948 *
949 * This function may return:
950 * o a negative error code in case of failure
Brian Norris07fd2f82015-12-04 15:25:17 -0800951 * o zero otherwise, and @pparts will describe the partitions, number of
Brian Norrisadc83bf2015-12-09 10:24:03 -0800952 * partitions, and the parser which parsed them. Caller must release
953 * resources with mtd_part_parser_cleanup() when finished with the returned
954 * data.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300955 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200956int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
Brian Norris07fd2f82015-12-04 15:25:17 -0800957 struct mtd_partitions *pparts,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400958 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700959{
960 struct mtd_part_parser *parser;
Brian Norris5a2415b2015-10-11 13:03:47 -0700961 int ret, err = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000962
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400963 if (!types)
964 types = default_mtd_part_types;
965
Brian Norris5a2415b2015-10-11 13:03:47 -0700966 for ( ; *types; types++) {
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100967 /*
968 * ofpart is a special type that means OF partitioning info
969 * should be used. It requires a bit different logic so it is
970 * handled in a separated function.
971 */
972 if (!strcmp(*types, "ofpart")) {
973 ret = mtd_part_of_parse(master, pparts);
974 } else {
975 pr_debug("%s: parsing partitions %s\n", master->name,
976 *types);
Brian Norris5531ae42015-12-04 15:25:15 -0800977 parser = mtd_part_parser_get(*types);
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100978 if (!parser && !request_module("%s", *types))
979 parser = mtd_part_parser_get(*types);
980 pr_debug("%s: got parser %s\n", master->name,
981 parser ? parser->name : NULL);
982 if (!parser)
983 continue;
984 ret = mtd_part_do_parse(parser, master, pparts, data);
985 if (ret <= 0)
986 mtd_part_parser_put(parser);
987 }
Brian Norris01f9c722017-05-23 07:30:20 +0200988 /* Found partitions! */
989 if (ret > 0)
Brian Norris07fd2f82015-12-04 15:25:17 -0800990 return 0;
Brian Norris5a2415b2015-10-11 13:03:47 -0700991 /*
992 * Stash the first error we see; only report it if no parser
993 * succeeds
994 */
995 if (ret < 0 && !err)
996 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700997 }
Brian Norris5a2415b2015-10-11 13:03:47 -0700998 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999}
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001000
Brian Norrisadc83bf2015-12-09 10:24:03 -08001001void mtd_part_parser_cleanup(struct mtd_partitions *parts)
1002{
1003 const struct mtd_part_parser *parser;
1004
1005 if (!parts)
1006 return;
1007
1008 parser = parts->parser;
1009 if (parser) {
1010 if (parser->cleanup)
1011 parser->cleanup(parts->parts, parts->nr_parts);
1012
1013 mtd_part_parser_put(parser);
1014 }
1015}
1016
Richard Genoud5dee4672012-07-10 18:23:39 +02001017int mtd_is_partition(const struct mtd_info *mtd)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001018{
1019 struct mtd_part *part;
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +02001020 int ispart = 0;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001021
1022 mutex_lock(&mtd_partitions_mutex);
1023 list_for_each_entry(part, &mtd_partitions, list)
1024 if (&part->mtd == mtd) {
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +02001025 ispart = 1;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001026 break;
1027 }
1028 mutex_unlock(&mtd_partitions_mutex);
1029
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +02001030 return ispart;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001031}
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +02001032EXPORT_SYMBOL_GPL(mtd_is_partition);
Richard Genoud62082e52012-07-10 18:23:40 +02001033
1034/* Returns the size of the entire flash chip */
1035uint64_t mtd_get_device_size(const struct mtd_info *mtd)
1036{
1037 if (!mtd_is_partition(mtd))
1038 return mtd->size;
1039
Rafał Miłecki97519dc2017-06-21 08:26:45 +02001040 return mtd_get_device_size(mtd_to_part(mtd)->parent);
Richard Genoud62082e52012-07-10 18:23:40 +02001041}
1042EXPORT_SYMBOL_GPL(mtd_get_device_size);