blob: 99c460facd5e97702896aae893d4df258320c0f2 [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 Riesch4b78fc42f2014-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 Riesch4b78fc42f2014-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 Riesch4b78fc42f2014-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 Riesch4b78fc42f2014-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łecki0a9d72b2017-06-21 08:26:44 +0200325static struct mtd_part *allocate_partition(struct mtd_info *parent,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300326 const struct mtd_partition *part, int partno,
327 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900328{
Brian Norrisc169e3d2017-06-22 14:09:18 -0700329 int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize :
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200330 parent->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900331 struct mtd_part *slave;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200332 u32 remainder;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300333 char *name;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200334 u64 tmp;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900335
336 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900337 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300338 name = kstrdup(part->name, GFP_KERNEL);
339 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900340 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200341 parent->name);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300342 kfree(name);
343 kfree(slave);
344 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900345 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900346
347 /* set up the MTD object for this partition */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200348 slave->mtd.type = parent->type;
349 slave->mtd.flags = parent->flags & ~part->mask_flags;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900350 slave->mtd.size = part->size;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200351 slave->mtd.writesize = parent->writesize;
352 slave->mtd.writebufsize = parent->writebufsize;
353 slave->mtd.oobsize = parent->oobsize;
354 slave->mtd.oobavail = parent->oobavail;
355 slave->mtd.subpage_sft = parent->subpage_sft;
356 slave->mtd.pairing = parent->pairing;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900357
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300358 slave->mtd.name = name;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200359 slave->mtd.owner = parent->owner;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900360
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700361 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
362 * concern for showing the same data in multiple partitions.
363 * However, it is very useful to have the master node present,
364 * so the MTD_PARTITIONED_MASTER option allows that. The master
365 * will have device nodes etc only if this is set, so make the
366 * parent conditional on that option. Note, this is a way to
367 * distinguish between the master and the partition in sysfs.
David Brownell1f24b5a2009-03-26 00:42:41 -0700368 */
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200369 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200370 &parent->dev :
371 parent->dev.parent;
Sascha Hauer42e94012017-02-09 11:50:24 +0100372 slave->mtd.dev.of_node = part->of_node;
David Brownell1f24b5a2009-03-26 00:42:41 -0700373
Boris Brezillon24ff1292018-01-09 09:50:34 +0100374 if (parent->_read)
375 slave->mtd._read = part_read;
376 if (parent->_write)
377 slave->mtd._write = part_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900378
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200379 if (parent->_panic_write)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200380 slave->mtd._panic_write = part_panic_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900381
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200382 if (parent->_point && parent->_unpoint) {
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200383 slave->mtd._point = part_point;
384 slave->mtd._unpoint = part_unpoint;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900385 }
386
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200387 if (parent->_read_oob)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200388 slave->mtd._read_oob = part_read_oob;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200389 if (parent->_write_oob)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200390 slave->mtd._write_oob = part_write_oob;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200391 if (parent->_read_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200392 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200393 if (parent->_read_fact_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200394 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200395 if (parent->_write_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200396 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200397 if (parent->_lock_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200398 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200399 if (parent->_get_user_prot_info)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200400 slave->mtd._get_user_prot_info = part_get_user_prot_info;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200401 if (parent->_get_fact_prot_info)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200402 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200403 if (parent->_sync)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200404 slave->mtd._sync = part_sync;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200405 if (!partno && !parent->dev.class && parent->_suspend &&
406 parent->_resume) {
Brian Norrisc169e3d2017-06-22 14:09:18 -0700407 slave->mtd._suspend = part_suspend;
408 slave->mtd._resume = part_resume;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900409 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200410 if (parent->_writev)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200411 slave->mtd._writev = part_writev;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200412 if (parent->_lock)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200413 slave->mtd._lock = part_lock;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200414 if (parent->_unlock)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200415 slave->mtd._unlock = part_unlock;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200416 if (parent->_is_locked)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200417 slave->mtd._is_locked = part_is_locked;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200418 if (parent->_block_isreserved)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300419 slave->mtd._block_isreserved = part_block_isreserved;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200420 if (parent->_block_isbad)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200421 slave->mtd._block_isbad = part_block_isbad;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200422 if (parent->_block_markbad)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200423 slave->mtd._block_markbad = part_block_markbad;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200424 if (parent->_max_bad_blocks)
Jeff Westfahl6080ef62017-01-10 13:30:17 -0600425 slave->mtd._max_bad_blocks = part_max_bad_blocks;
Richard Weinberger5e149072016-09-21 11:43:56 +0200426
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200427 if (parent->_get_device)
Richard Weinberger5e149072016-09-21 11:43:56 +0200428 slave->mtd._get_device = part_get_device;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200429 if (parent->_put_device)
Richard Weinberger5e149072016-09-21 11:43:56 +0200430 slave->mtd._put_device = part_put_device;
431
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200432 slave->mtd._erase = part_erase;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200433 slave->parent = parent;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900434 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900435
436 if (slave->offset == MTDPART_OFS_APPEND)
437 slave->offset = cur_offset;
438 if (slave->offset == MTDPART_OFS_NXTBLK) {
Chris Packham1eeef2d2017-06-09 15:58:31 +1200439 tmp = cur_offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900440 slave->offset = cur_offset;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200441 remainder = do_div(tmp, wr_alignment);
442 if (remainder) {
443 slave->offset += wr_alignment - remainder;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900444 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000445 "0x%012llx -> 0x%012llx\n", partno,
446 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900447 }
448 }
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400449 if (slave->offset == MTDPART_OFS_RETAIN) {
450 slave->offset = cur_offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200451 if (parent->size - slave->offset >= slave->mtd.size) {
452 slave->mtd.size = parent->size - slave->offset
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400453 - slave->mtd.size;
454 } else {
455 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200456 part->name, parent->size - slave->offset,
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400457 slave->mtd.size);
458 /* register to preserve ordering */
459 goto out_register;
460 }
461 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900462 if (slave->mtd.size == MTDPART_SIZ_FULL)
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200463 slave->mtd.size = parent->size - slave->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900464
Adrian Hunter69423d92008-12-10 13:37:21 +0000465 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
466 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900467
468 /* let's do some sanity checks */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200469 if (slave->offset >= parent->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900470 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900471 slave->offset = 0;
472 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900473 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900474 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900475 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900476 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200477 if (slave->offset + slave->mtd.size > parent->size) {
478 slave->mtd.size = parent->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000479 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 +0200480 part->name, parent->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900481 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200482 if (parent->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900483 /* Deal with variable erase size stuff */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200484 int i, max = parent->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000485 u64 end = slave->offset + slave->mtd.size;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200486 struct mtd_erase_region_info *regions = parent->eraseregions;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900487
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900488 /* Find the first erase regions which is part of this
489 * partition. */
490 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900491 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900492 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700493 if (i > 0)
494 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900495
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900496 /* Pick biggest erasesize */
497 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900498 if (slave->mtd.erasesize < regions[i].erasesize) {
499 slave->mtd.erasesize = regions[i].erasesize;
500 }
501 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900502 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900503 } else {
504 /* Single erase size */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200505 slave->mtd.erasesize = parent->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900506 }
507
Boris Brezillon7e439682017-09-25 10:19:57 +0200508 /*
509 * Slave erasesize might differ from the master one if the master
510 * exposes several regions with different erasesize. Adjust
511 * wr_alignment accordingly.
512 */
513 if (!(slave->mtd.flags & MTD_NO_ERASE))
514 wr_alignment = slave->mtd.erasesize;
515
Chris Packham1eeef2d2017-06-09 15:58:31 +1200516 tmp = slave->offset;
517 remainder = do_div(tmp, wr_alignment);
518 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900519 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900520 /* FIXME: Let it be writable if it is on a boundary of
521 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900522 slave->mtd.flags &= ~MTD_WRITEABLE;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200523 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 +0900524 part->name);
525 }
Chris Packham1eeef2d2017-06-09 15:58:31 +1200526
527 tmp = slave->mtd.size;
528 remainder = do_div(tmp, wr_alignment);
529 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900530 slave->mtd.flags &= ~MTD_WRITEABLE;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200531 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 +0900532 part->name);
533 }
534
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100535 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200536 slave->mtd.ecc_step_size = parent->ecc_step_size;
537 slave->mtd.ecc_strength = parent->ecc_strength;
538 slave->mtd.bitflip_threshold = parent->bitflip_threshold;
Mike Dunnd062d4e2012-04-25 12:06:08 -0700539
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200540 if (parent->_block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000541 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900542
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900543 while (offs < slave->mtd.size) {
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200544 if (mtd_block_isreserved(parent, offs + slave->offset))
Ezequiel Garciafdf43a42014-03-21 08:57:44 -0300545 slave->mtd.ecc_stats.bbtblocks++;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200546 else if (mtd_block_isbad(parent, offs + slave->offset))
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900547 slave->mtd.ecc_stats.badblocks++;
548 offs += slave->mtd.erasesize;
549 }
550 }
551
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900552out_register:
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900553 return slave;
554}
555
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700556static ssize_t mtd_partition_offset_show(struct device *dev,
557 struct device_attribute *attr, char *buf)
558{
559 struct mtd_info *mtd = dev_get_drvdata(dev);
Brian Norris25245342015-11-19 19:28:39 -0800560 struct mtd_part *part = mtd_to_part(mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700561 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
562}
563
564static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
565
566static const struct attribute *mtd_partition_attrs[] = {
567 &dev_attr_offset.attr,
568 NULL
569};
570
571static int mtd_add_partition_attrs(struct mtd_part *new)
572{
573 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
574 if (ret)
575 printk(KERN_WARNING
576 "mtd: failed to create partition attrs, err=%d\n", ret);
577 return ret;
578}
579
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200580int mtd_add_partition(struct mtd_info *parent, const char *name,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300581 long long offset, long long length)
582{
583 struct mtd_partition part;
Dan Ehrenberg3a434f62015-04-02 15:15:12 -0700584 struct mtd_part *new;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300585 int ret = 0;
586
587 /* the direct offset is expected */
588 if (offset == MTDPART_OFS_APPEND ||
589 offset == MTDPART_OFS_NXTBLK)
590 return -EINVAL;
591
592 if (length == MTDPART_SIZ_FULL)
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200593 length = parent->size - offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300594
595 if (length <= 0)
596 return -EINVAL;
597
Brian Norris93867232015-11-11 16:47:52 -0800598 memset(&part, 0, sizeof(part));
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300599 part.name = name;
600 part.size = length;
601 part.offset = offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300602
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200603 new = allocate_partition(parent, &part, -1, offset);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300604 if (IS_ERR(new))
605 return PTR_ERR(new);
606
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300607 mutex_lock(&mtd_partitions_mutex);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300608 list_add(&new->list, &mtd_partitions);
609 mutex_unlock(&mtd_partitions_mutex);
610
611 add_mtd_device(&new->mtd);
612
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700613 mtd_add_partition_attrs(new);
614
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300615 return ret;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300616}
617EXPORT_SYMBOL_GPL(mtd_add_partition);
618
Rafał Miłecki08263a92017-06-21 08:26:42 +0200619/**
620 * __mtd_del_partition - delete MTD partition
621 *
622 * @priv: internal MTD struct for partition to be deleted
623 *
624 * This function must be called with the partitions mutex locked.
625 */
626static int __mtd_del_partition(struct mtd_part *priv)
627{
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200628 struct mtd_part *child, *next;
Rafał Miłecki08263a92017-06-21 08:26:42 +0200629 int err;
630
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200631 list_for_each_entry_safe(child, next, &mtd_partitions, list) {
632 if (child->parent == &priv->mtd) {
633 err = __mtd_del_partition(child);
634 if (err)
635 return err;
636 }
637 }
638
Rafał Miłeckic5ceaba2017-06-21 08:26:43 +0200639 sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);
640
Rafał Miłecki08263a92017-06-21 08:26:42 +0200641 err = del_mtd_device(&priv->mtd);
642 if (err)
643 return err;
644
645 list_del(&priv->list);
646 free_partition(priv);
647
648 return 0;
649}
650
651/*
652 * This function unregisters and destroy all slave MTD objects which are
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200653 * attached to the given MTD object.
Rafał Miłecki08263a92017-06-21 08:26:42 +0200654 */
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200655int del_mtd_partitions(struct mtd_info *mtd)
Rafał Miłecki08263a92017-06-21 08:26:42 +0200656{
657 struct mtd_part *slave, *next;
658 int ret, err = 0;
659
660 mutex_lock(&mtd_partitions_mutex);
661 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200662 if (slave->parent == mtd) {
Rafał Miłecki08263a92017-06-21 08:26:42 +0200663 ret = __mtd_del_partition(slave);
664 if (ret < 0)
665 err = ret;
666 }
667 mutex_unlock(&mtd_partitions_mutex);
668
669 return err;
670}
671
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200672int mtd_del_partition(struct mtd_info *mtd, int partno)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300673{
674 struct mtd_part *slave, *next;
675 int ret = -EINVAL;
676
677 mutex_lock(&mtd_partitions_mutex);
678 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200679 if ((slave->parent == mtd) &&
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300680 (slave->mtd.index == partno)) {
Rafał Miłecki08263a92017-06-21 08:26:42 +0200681 ret = __mtd_del_partition(slave);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300682 break;
683 }
684 mutex_unlock(&mtd_partitions_mutex);
685
686 return ret;
687}
688EXPORT_SYMBOL_GPL(mtd_del_partition);
689
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690/*
691 * This function, given a master MTD object and a partition table, creates
692 * and registers slave MTD objects which are bound to the master according to
693 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700694 *
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700695 * For historical reasons, this function's caller only registers the master
696 * if the MTD_PARTITIONED_MASTER config option is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 */
698
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000699int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 const struct mtd_partition *parts,
701 int nbparts)
702{
703 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000704 uint64_t cur_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 int i;
706
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900707 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700708
709 for (i = 0; i < nbparts; i++) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300710 slave = allocate_partition(master, parts + i, i, cur_offset);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200711 if (IS_ERR(slave)) {
712 del_mtd_partitions(master);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300713 return PTR_ERR(slave);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200714 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300715
716 mutex_lock(&mtd_partitions_mutex);
717 list_add(&slave->list, &mtd_partitions);
718 mutex_unlock(&mtd_partitions_mutex);
719
720 add_mtd_device(&slave->mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700721 mtd_add_partition_attrs(slave);
Rafał Miłecki76a83222018-07-13 16:32:21 +0200722 /* Look for subpartitions */
723 parse_mtd_partitions(&slave->mtd, parts[i].types, NULL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300724
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
728 return 0;
729}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730
731static DEFINE_SPINLOCK(part_parser_lock);
732static LIST_HEAD(part_parsers);
733
Brian Norris5531ae42015-12-04 15:25:15 -0800734static struct mtd_part_parser *mtd_part_parser_get(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735{
Chris Malley71a928c2008-05-19 20:11:50 +0100736 struct mtd_part_parser *p, *ret = NULL;
737
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 spin_lock(&part_parser_lock);
739
Chris Malley71a928c2008-05-19 20:11:50 +0100740 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
742 ret = p;
743 break;
744 }
Chris Malley71a928c2008-05-19 20:11:50 +0100745
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 spin_unlock(&part_parser_lock);
747
748 return ret;
749}
750
Brian Norris5531ae42015-12-04 15:25:15 -0800751static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
752{
753 module_put(p->owner);
754}
Dmitry Eremin-Solenikov953b3bd2011-06-23 15:26:14 +0400755
Brian Norrisadc83bf2015-12-09 10:24:03 -0800756/*
757 * Many partition parsers just expected the core to kfree() all their data in
758 * one chunk. Do that by default.
759 */
760static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
761 int nr_parts)
762{
763 kfree(pparts);
764}
765
Brian Norrisb9eab012015-11-11 19:13:29 -0800766int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767{
Brian Norrisb9eab012015-11-11 19:13:29 -0800768 p->owner = owner;
769
Brian Norrisadc83bf2015-12-09 10:24:03 -0800770 if (!p->cleanup)
771 p->cleanup = &mtd_part_parser_cleanup_default;
772
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 spin_lock(&part_parser_lock);
774 list_add(&p->list, &part_parsers);
775 spin_unlock(&part_parser_lock);
Brian Norrisb9eab012015-11-11 19:13:29 -0800776
777 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778}
Brian Norrisb9eab012015-11-11 19:13:29 -0800779EXPORT_SYMBOL_GPL(__register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780
Axel Lincf3b2b12013-12-01 18:59:15 +0800781void deregister_mtd_parser(struct mtd_part_parser *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782{
783 spin_lock(&part_parser_lock);
784 list_del(&p->list);
785 spin_unlock(&part_parser_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900787EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300789/*
790 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
791 * are changing this array!
792 */
Artem Bityutskiyccef4dc2013-03-12 10:51:35 +0200793static const char * const default_mtd_part_types[] = {
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400794 "cmdlinepart",
795 "ofpart",
796 NULL
797};
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400798
Rafał Miłecki76a83222018-07-13 16:32:21 +0200799/* Check DT only when looking for subpartitions. */
800static const char * const default_subpartition_types[] = {
801 "ofpart",
802 NULL
803};
804
Brian Norris01f9c722017-05-23 07:30:20 +0200805static int mtd_part_do_parse(struct mtd_part_parser *parser,
806 struct mtd_info *master,
807 struct mtd_partitions *pparts,
808 struct mtd_part_parser_data *data)
809{
810 int ret;
811
812 ret = (*parser->parse_fn)(master, &pparts->parts, data);
813 pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
814 if (ret <= 0)
815 return ret;
816
817 pr_notice("%d %s partitions found on MTD device %s\n", ret,
818 parser->name, master->name);
819
820 pparts->nr_parts = ret;
821 pparts->parser = parser;
822
823 return ret;
824}
825
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300826/**
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100827 * mtd_part_get_compatible_parser - find MTD parser by a compatible string
828 *
829 * @compat: compatible string describing partitions in a device tree
830 *
831 * MTD parsers can specify supported partitions by providing a table of
832 * compatibility strings. This function finds a parser that advertises support
833 * for a passed value of "compatible".
834 */
835static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
836{
837 struct mtd_part_parser *p, *ret = NULL;
838
839 spin_lock(&part_parser_lock);
840
841 list_for_each_entry(p, &part_parsers, list) {
842 const struct of_device_id *matches;
843
844 matches = p->of_match_table;
845 if (!matches)
846 continue;
847
848 for (; matches->compatible[0]; matches++) {
849 if (!strcmp(matches->compatible, compat) &&
850 try_module_get(p->owner)) {
851 ret = p;
852 break;
853 }
854 }
855
856 if (ret)
857 break;
858 }
859
860 spin_unlock(&part_parser_lock);
861
862 return ret;
863}
864
865static int mtd_part_of_parse(struct mtd_info *master,
866 struct mtd_partitions *pparts)
867{
868 struct mtd_part_parser *parser;
869 struct device_node *np;
870 struct property *prop;
871 const char *compat;
Rafał Miłeckic0faf432018-03-14 13:10:43 +0100872 const char *fixed = "fixed-partitions";
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100873 int ret, err = 0;
874
Rafał Miłecki76a83222018-07-13 16:32:21 +0200875 np = mtd_get_of_node(master);
Miquel Raynal85516a982018-09-07 16:35:54 +0200876 if (mtd_is_partition(master))
877 of_node_get(np);
878 else
Rafał Miłecki76a83222018-07-13 16:32:21 +0200879 np = of_get_child_by_name(np, "partitions");
Miquel Raynal85516a982018-09-07 16:35:54 +0200880
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100881 of_property_for_each_string(np, "compatible", prop, compat) {
882 parser = mtd_part_get_compatible_parser(compat);
883 if (!parser)
884 continue;
885 ret = mtd_part_do_parse(parser, master, pparts, NULL);
886 if (ret > 0) {
887 of_node_put(np);
888 return ret;
889 }
890 mtd_part_parser_put(parser);
891 if (ret < 0 && !err)
892 err = ret;
893 }
894 of_node_put(np);
895
896 /*
Rafał Miłeckic0faf432018-03-14 13:10:43 +0100897 * For backward compatibility we have to try the "fixed-partitions"
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100898 * parser. It supports old DT format with partitions specified as a
899 * direct subnodes of a flash device DT node without any compatibility
900 * specified we could match.
901 */
902 parser = mtd_part_parser_get(fixed);
903 if (!parser && !request_module("%s", fixed))
904 parser = mtd_part_parser_get(fixed);
905 if (parser) {
906 ret = mtd_part_do_parse(parser, master, pparts, NULL);
907 if (ret > 0)
908 return ret;
909 mtd_part_parser_put(parser);
910 if (ret < 0 && !err)
911 err = ret;
912 }
913
914 return err;
915}
916
917/**
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200918 * parse_mtd_partitions - parse and register MTD partitions
919 *
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300920 * @master: the master partition (describes whole MTD device)
921 * @types: names of partition parsers to try or %NULL
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400922 * @data: MTD partition parser-specific data
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300923 *
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200924 * This function tries to find & register partitions on MTD device @master. It
925 * uses MTD partition parsers, specified in @types. However, if @types is %NULL,
926 * then the default list of parsers is used. The default list contains only the
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400927 * "cmdlinepart" and "ofpart" parsers ATM.
Huang Shijiec51803d2012-08-18 13:07:41 -0400928 * Note: If there are more then one parser in @types, the kernel only takes the
929 * partitions parsed out by the first parser.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300930 *
931 * This function may return:
932 * o a negative error code in case of failure
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200933 * o number of found partitions otherwise
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300934 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200935int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400936 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700937{
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200938 struct mtd_partitions pparts = { };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700939 struct mtd_part_parser *parser;
Brian Norris5a2415b2015-10-11 13:03:47 -0700940 int ret, err = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000941
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400942 if (!types)
Rafał Miłecki76a83222018-07-13 16:32:21 +0200943 types = mtd_is_partition(master) ? default_subpartition_types :
944 default_mtd_part_types;
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400945
Brian Norris5a2415b2015-10-11 13:03:47 -0700946 for ( ; *types; types++) {
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100947 /*
948 * ofpart is a special type that means OF partitioning info
949 * should be used. It requires a bit different logic so it is
950 * handled in a separated function.
951 */
952 if (!strcmp(*types, "ofpart")) {
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200953 ret = mtd_part_of_parse(master, &pparts);
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100954 } else {
955 pr_debug("%s: parsing partitions %s\n", master->name,
956 *types);
Brian Norris5531ae42015-12-04 15:25:15 -0800957 parser = mtd_part_parser_get(*types);
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100958 if (!parser && !request_module("%s", *types))
959 parser = mtd_part_parser_get(*types);
960 pr_debug("%s: got parser %s\n", master->name,
961 parser ? parser->name : NULL);
962 if (!parser)
963 continue;
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200964 ret = mtd_part_do_parse(parser, master, &pparts, data);
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100965 if (ret <= 0)
966 mtd_part_parser_put(parser);
967 }
Brian Norris01f9c722017-05-23 07:30:20 +0200968 /* Found partitions! */
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200969 if (ret > 0) {
970 err = add_mtd_partitions(master, pparts.parts,
971 pparts.nr_parts);
972 mtd_part_parser_cleanup(&pparts);
973 return err ? err : pparts.nr_parts;
974 }
Brian Norris5a2415b2015-10-11 13:03:47 -0700975 /*
976 * Stash the first error we see; only report it if no parser
977 * succeeds
978 */
979 if (ret < 0 && !err)
980 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 }
Brian Norris5a2415b2015-10-11 13:03:47 -0700982 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983}
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300984
Brian Norrisadc83bf2015-12-09 10:24:03 -0800985void mtd_part_parser_cleanup(struct mtd_partitions *parts)
986{
987 const struct mtd_part_parser *parser;
988
989 if (!parts)
990 return;
991
992 parser = parts->parser;
993 if (parser) {
994 if (parser->cleanup)
995 parser->cleanup(parts->parts, parts->nr_parts);
996
997 mtd_part_parser_put(parser);
998 }
999}
1000
Richard Genoud5dee4672012-07-10 18:23:39 +02001001int mtd_is_partition(const struct mtd_info *mtd)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001002{
1003 struct mtd_part *part;
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +02001004 int ispart = 0;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001005
1006 mutex_lock(&mtd_partitions_mutex);
1007 list_for_each_entry(part, &mtd_partitions, list)
1008 if (&part->mtd == mtd) {
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +02001009 ispart = 1;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001010 break;
1011 }
1012 mutex_unlock(&mtd_partitions_mutex);
1013
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +02001014 return ispart;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001015}
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +02001016EXPORT_SYMBOL_GPL(mtd_is_partition);
Richard Genoud62082e562012-07-10 18:23:40 +02001017
1018/* Returns the size of the entire flash chip */
1019uint64_t mtd_get_device_size(const struct mtd_info *mtd)
1020{
1021 if (!mtd_is_partition(mtd))
1022 return mtd->size;
1023
Rafał Miłecki97519dc2017-06-21 08:26:45 +02001024 return mtd_get_device_size(mtd_to_part(mtd)->parent);
Richard Genoud62082e562012-07-10 18:23:40 +02001025}
1026EXPORT_SYMBOL_GPL(mtd_get_device_size);