blob: 37f174ccbcec4b1f10d0af717707a38fa46d4499 [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
Rafał Miłecki6750f612018-11-20 10:24:09 +010064static u64 part_absolute_offset(struct mtd_info *mtd)
65{
66 struct mtd_part *part = mtd_to_part(mtd);
67
68 if (!mtd_is_partition(mtd))
69 return 0;
70
71 return part_absolute_offset(part->parent) + part->offset;
72}
Thomas Gleixner97894cd2005-11-07 11:15:26 +000073
74/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 * MTD methods which simply translate the effective address and pass through
76 * to the _real_ device.
77 */
78
Atsushi Nemotob33a2882008-07-19 01:00:33 +090079static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
80 size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
Brian Norris25245342015-11-19 19:28:39 -080082 struct mtd_part *part = mtd_to_part(mtd);
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020083 struct mtd_ecc_stats stats;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020084 int res;
85
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020086 stats = part->parent->ecc_stats;
87 res = part->parent->_read(part->parent, from + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -080088 retlen, buf);
Mike Dunnedbc45402012-04-25 12:06:11 -070089 if (unlikely(mtd_is_eccerr(res)))
90 mtd->ecc_stats.failed +=
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020091 part->parent->ecc_stats.failed - stats.failed;
Mike Dunnedbc45402012-04-25 12:06:11 -070092 else
93 mtd->ecc_stats.corrected +=
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020094 part->parent->ecc_stats.corrected - stats.corrected;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020095 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
Atsushi Nemotob33a2882008-07-19 01:00:33 +090098static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
99 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100{
Brian Norris25245342015-11-19 19:28:39 -0800101 struct mtd_part *part = mtd_to_part(mtd);
Artem Bityutskiy5def4892012-02-03 16:23:52 +0200102
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200103 return part->parent->_point(part->parent, from + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800104 retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105}
Thomas Gleixner9223a452006-05-23 17:21:03 +0200106
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +0200107static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108{
Brian Norris25245342015-11-19 19:28:39 -0800109 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200111 return part->parent->_unpoint(part->parent, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112}
113
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200114static int part_read_oob(struct mtd_info *mtd, loff_t from,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900115 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116{
Brian Norris25245342015-11-19 19:28:39 -0800117 struct mtd_part *part = mtd_to_part(mtd);
Boris Brezillond020fc82018-01-09 09:50:33 +0100118 struct mtd_ecc_stats stats;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200119 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200120
Boris Brezillond020fc82018-01-09 09:50:33 +0100121 stats = part->parent->ecc_stats;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200122 res = part->parent->_read_oob(part->parent, from + part->offset, ops);
Boris Brezillond020fc82018-01-09 09:50:33 +0100123 if (unlikely(mtd_is_eccerr(res)))
124 mtd->ecc_stats.failed +=
125 part->parent->ecc_stats.failed - stats.failed;
126 else
127 mtd->ecc_stats.corrected +=
128 part->parent->ecc_stats.corrected - stats.corrected;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200129 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130}
131
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900132static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
133 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Brian Norris25245342015-11-19 19:28:39 -0800135 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200136 return part->parent->_read_user_prot_reg(part->parent, from, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800137 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138}
139
Christian Riesch4b78fc42014-01-28 09:29:44 +0100140static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
141 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000142{
Brian Norris25245342015-11-19 19:28:39 -0800143 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200144 return part->parent->_get_user_prot_info(part->parent, len, retlen,
Christian Riesch4b78fc42014-01-28 09:29:44 +0100145 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000146}
147
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900148static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
149 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150{
Brian Norris25245342015-11-19 19:28:39 -0800151 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200152 return part->parent->_read_fact_prot_reg(part->parent, from, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800153 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154}
155
Christian Riesch4b78fc42014-01-28 09:29:44 +0100156static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
157 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000158{
Brian Norris25245342015-11-19 19:28:39 -0800159 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200160 return part->parent->_get_fact_prot_info(part->parent, len, retlen,
Christian Riesch4b78fc42014-01-28 09:29:44 +0100161 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000162}
163
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900164static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
165 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Brian Norris25245342015-11-19 19:28:39 -0800167 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200168 return part->parent->_write(part->parent, to + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800169 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170}
171
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900172static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
173 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000174{
Brian Norris25245342015-11-19 19:28:39 -0800175 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200176 return part->parent->_panic_write(part->parent, to + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800177 retlen, buf);
Richard Purdie388bbb02008-02-06 10:17:15 +0000178}
179
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200180static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900181 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Brian Norris25245342015-11-19 19:28:39 -0800183 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200184
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200185 return part->parent->_write_oob(part->parent, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186}
187
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900188static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
189 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190{
Brian Norris25245342015-11-19 19:28:39 -0800191 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200192 return part->parent->_write_user_prot_reg(part->parent, from, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800193 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900196static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
197 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000198{
Brian Norris25245342015-11-19 19:28:39 -0800199 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200200 return part->parent->_lock_user_prot_reg(part->parent, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000201}
202
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900203static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
204 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Brian Norris25245342015-11-19 19:28:39 -0800206 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200207 return part->parent->_writev(part->parent, vecs, count,
Mike Dunn994c8402012-03-03 13:13:06 -0800208 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900211static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212{
Brian Norris25245342015-11-19 19:28:39 -0800213 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 int ret;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 instr->addr += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200217 ret = part->parent->_erase(part->parent, instr);
Boris Brezillon8f347c42018-02-12 22:03:10 +0100218 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
219 instr->fail_addr -= part->offset;
220 instr->addr -= part->offset;
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 return ret;
223}
224
Adrian Hunter69423d92008-12-10 13:37:21 +0000225static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Brian Norris25245342015-11-19 19:28:39 -0800227 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200228 return part->parent->_lock(part->parent, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229}
230
Adrian Hunter69423d92008-12-10 13:37:21 +0000231static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232{
Brian Norris25245342015-11-19 19:28:39 -0800233 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200234 return part->parent->_unlock(part->parent, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235}
236
Richard Cochran99384242010-06-14 18:10:33 +0200237static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
238{
Brian Norris25245342015-11-19 19:28:39 -0800239 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200240 return part->parent->_is_locked(part->parent, ofs + part->offset, len);
Richard Cochran99384242010-06-14 18:10:33 +0200241}
242
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243static void part_sync(struct mtd_info *mtd)
244{
Brian Norris25245342015-11-19 19:28:39 -0800245 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200246 part->parent->_sync(part->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247}
248
249static int part_suspend(struct mtd_info *mtd)
250{
Brian Norris25245342015-11-19 19:28:39 -0800251 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200252 return part->parent->_suspend(part->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253}
254
255static void part_resume(struct mtd_info *mtd)
256{
Brian Norris25245342015-11-19 19:28:39 -0800257 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200258 part->parent->_resume(part->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300261static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
262{
Brian Norris25245342015-11-19 19:28:39 -0800263 struct mtd_part *part = mtd_to_part(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300264 ofs += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200265 return part->parent->_block_isreserved(part->parent, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300266}
267
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900268static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269{
Brian Norris25245342015-11-19 19:28:39 -0800270 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 ofs += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200272 return part->parent->_block_isbad(part->parent, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900275static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276{
Brian Norris25245342015-11-19 19:28:39 -0800277 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200278 int res;
279
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 ofs += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200281 res = part->parent->_block_markbad(part->parent, ofs);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200282 if (!res)
283 mtd->ecc_stats.badblocks++;
284 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Richard Weinberger5e149072016-09-21 11:43:56 +0200287static int part_get_device(struct mtd_info *mtd)
288{
289 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200290 return part->parent->_get_device(part->parent);
Richard Weinberger5e149072016-09-21 11:43:56 +0200291}
292
293static void part_put_device(struct mtd_info *mtd)
294{
295 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200296 part->parent->_put_device(part->parent);
Richard Weinberger5e149072016-09-21 11:43:56 +0200297}
298
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100299static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
300 struct mtd_oob_region *oobregion)
301{
302 struct mtd_part *part = mtd_to_part(mtd);
303
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200304 return mtd_ooblayout_ecc(part->parent, section, oobregion);
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100305}
306
307static int part_ooblayout_free(struct mtd_info *mtd, int section,
308 struct mtd_oob_region *oobregion)
309{
310 struct mtd_part *part = mtd_to_part(mtd);
311
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200312 return mtd_ooblayout_free(part->parent, section, oobregion);
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100313}
314
315static const struct mtd_ooblayout_ops part_ooblayout_ops = {
316 .ecc = part_ooblayout_ecc,
317 .free = part_ooblayout_free,
318};
319
Jeff Westfahl6080ef62017-01-10 13:30:17 -0600320static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
321{
322 struct mtd_part *part = mtd_to_part(mtd);
323
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200324 return part->parent->_max_bad_blocks(part->parent,
Jeff Westfahl6080ef62017-01-10 13:30:17 -0600325 ofs + part->offset, len);
326}
327
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300328static inline void free_partition(struct mtd_part *p)
329{
330 kfree(p->mtd.name);
331 kfree(p);
332}
333
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200334static struct mtd_part *allocate_partition(struct mtd_info *parent,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300335 const struct mtd_partition *part, int partno,
336 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900337{
Brian Norrisc169e3d2017-06-22 14:09:18 -0700338 int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize :
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200339 parent->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900340 struct mtd_part *slave;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200341 u32 remainder;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300342 char *name;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200343 u64 tmp;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900344
345 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900346 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300347 name = kstrdup(part->name, GFP_KERNEL);
348 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900349 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200350 parent->name);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300351 kfree(name);
352 kfree(slave);
353 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900354 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900355
356 /* set up the MTD object for this partition */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200357 slave->mtd.type = parent->type;
Rafał Miłecki1186af42018-11-20 09:55:45 +0100358 slave->mtd.flags = parent->orig_flags & ~part->mask_flags;
359 slave->mtd.orig_flags = slave->mtd.flags;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900360 slave->mtd.size = part->size;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200361 slave->mtd.writesize = parent->writesize;
362 slave->mtd.writebufsize = parent->writebufsize;
363 slave->mtd.oobsize = parent->oobsize;
364 slave->mtd.oobavail = parent->oobavail;
365 slave->mtd.subpage_sft = parent->subpage_sft;
366 slave->mtd.pairing = parent->pairing;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900367
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300368 slave->mtd.name = name;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200369 slave->mtd.owner = parent->owner;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900370
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700371 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
372 * concern for showing the same data in multiple partitions.
373 * However, it is very useful to have the master node present,
374 * so the MTD_PARTITIONED_MASTER option allows that. The master
375 * will have device nodes etc only if this is set, so make the
376 * parent conditional on that option. Note, this is a way to
377 * distinguish between the master and the partition in sysfs.
David Brownell1f24b5a2009-03-26 00:42:41 -0700378 */
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200379 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200380 &parent->dev :
381 parent->dev.parent;
Sascha Hauer42e94012017-02-09 11:50:24 +0100382 slave->mtd.dev.of_node = part->of_node;
David Brownell1f24b5a2009-03-26 00:42:41 -0700383
Boris Brezillon24ff1292018-01-09 09:50:34 +0100384 if (parent->_read)
385 slave->mtd._read = part_read;
386 if (parent->_write)
387 slave->mtd._write = part_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900388
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200389 if (parent->_panic_write)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200390 slave->mtd._panic_write = part_panic_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900391
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200392 if (parent->_point && parent->_unpoint) {
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200393 slave->mtd._point = part_point;
394 slave->mtd._unpoint = part_unpoint;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900395 }
396
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200397 if (parent->_read_oob)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200398 slave->mtd._read_oob = part_read_oob;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200399 if (parent->_write_oob)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200400 slave->mtd._write_oob = part_write_oob;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200401 if (parent->_read_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200402 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200403 if (parent->_read_fact_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200404 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200405 if (parent->_write_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200406 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200407 if (parent->_lock_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200408 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200409 if (parent->_get_user_prot_info)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200410 slave->mtd._get_user_prot_info = part_get_user_prot_info;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200411 if (parent->_get_fact_prot_info)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200412 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200413 if (parent->_sync)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200414 slave->mtd._sync = part_sync;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200415 if (!partno && !parent->dev.class && parent->_suspend &&
416 parent->_resume) {
Brian Norrisc169e3d2017-06-22 14:09:18 -0700417 slave->mtd._suspend = part_suspend;
418 slave->mtd._resume = part_resume;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900419 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200420 if (parent->_writev)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200421 slave->mtd._writev = part_writev;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200422 if (parent->_lock)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200423 slave->mtd._lock = part_lock;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200424 if (parent->_unlock)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200425 slave->mtd._unlock = part_unlock;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200426 if (parent->_is_locked)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200427 slave->mtd._is_locked = part_is_locked;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200428 if (parent->_block_isreserved)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300429 slave->mtd._block_isreserved = part_block_isreserved;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200430 if (parent->_block_isbad)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200431 slave->mtd._block_isbad = part_block_isbad;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200432 if (parent->_block_markbad)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200433 slave->mtd._block_markbad = part_block_markbad;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200434 if (parent->_max_bad_blocks)
Jeff Westfahl6080ef62017-01-10 13:30:17 -0600435 slave->mtd._max_bad_blocks = part_max_bad_blocks;
Richard Weinberger5e149072016-09-21 11:43:56 +0200436
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200437 if (parent->_get_device)
Richard Weinberger5e149072016-09-21 11:43:56 +0200438 slave->mtd._get_device = part_get_device;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200439 if (parent->_put_device)
Richard Weinberger5e149072016-09-21 11:43:56 +0200440 slave->mtd._put_device = part_put_device;
441
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200442 slave->mtd._erase = part_erase;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200443 slave->parent = parent;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900444 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900445
446 if (slave->offset == MTDPART_OFS_APPEND)
447 slave->offset = cur_offset;
448 if (slave->offset == MTDPART_OFS_NXTBLK) {
Chris Packham1eeef2d2017-06-09 15:58:31 +1200449 tmp = cur_offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900450 slave->offset = cur_offset;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200451 remainder = do_div(tmp, wr_alignment);
452 if (remainder) {
453 slave->offset += wr_alignment - remainder;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900454 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000455 "0x%012llx -> 0x%012llx\n", partno,
456 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900457 }
458 }
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400459 if (slave->offset == MTDPART_OFS_RETAIN) {
460 slave->offset = cur_offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200461 if (parent->size - slave->offset >= slave->mtd.size) {
462 slave->mtd.size = parent->size - slave->offset
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400463 - slave->mtd.size;
464 } else {
465 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200466 part->name, parent->size - slave->offset,
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400467 slave->mtd.size);
468 /* register to preserve ordering */
469 goto out_register;
470 }
471 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900472 if (slave->mtd.size == MTDPART_SIZ_FULL)
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200473 slave->mtd.size = parent->size - slave->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900474
Adrian Hunter69423d92008-12-10 13:37:21 +0000475 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
476 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900477
478 /* let's do some sanity checks */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200479 if (slave->offset >= parent->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900480 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900481 slave->offset = 0;
482 slave->mtd.size = 0;
Boris Brezillonad463512019-01-30 12:55:52 +0100483
484 /* Initialize ->erasesize to make add_mtd_device() happy. */
485 slave->mtd.erasesize = parent->erasesize;
486
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900487 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900488 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900489 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900490 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200491 if (slave->offset + slave->mtd.size > parent->size) {
492 slave->mtd.size = parent->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000493 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 +0200494 part->name, parent->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900495 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200496 if (parent->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900497 /* Deal with variable erase size stuff */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200498 int i, max = parent->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000499 u64 end = slave->offset + slave->mtd.size;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200500 struct mtd_erase_region_info *regions = parent->eraseregions;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900501
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900502 /* Find the first erase regions which is part of this
503 * partition. */
504 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900505 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900506 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700507 if (i > 0)
508 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900509
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900510 /* Pick biggest erasesize */
511 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900512 if (slave->mtd.erasesize < regions[i].erasesize) {
513 slave->mtd.erasesize = regions[i].erasesize;
514 }
515 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900516 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900517 } else {
518 /* Single erase size */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200519 slave->mtd.erasesize = parent->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900520 }
521
Boris Brezillon7e439682017-09-25 10:19:57 +0200522 /*
523 * Slave erasesize might differ from the master one if the master
524 * exposes several regions with different erasesize. Adjust
525 * wr_alignment accordingly.
526 */
527 if (!(slave->mtd.flags & MTD_NO_ERASE))
528 wr_alignment = slave->mtd.erasesize;
529
Rafał Miłecki6750f612018-11-20 10:24:09 +0100530 tmp = part_absolute_offset(parent) + slave->offset;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200531 remainder = do_div(tmp, wr_alignment);
532 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900533 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900534 /* FIXME: Let it be writable if it is on a boundary of
535 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900536 slave->mtd.flags &= ~MTD_WRITEABLE;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200537 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 +0900538 part->name);
539 }
Chris Packham1eeef2d2017-06-09 15:58:31 +1200540
Rafał Miłecki6750f612018-11-20 10:24:09 +0100541 tmp = part_absolute_offset(parent) + slave->mtd.size;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200542 remainder = do_div(tmp, wr_alignment);
543 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900544 slave->mtd.flags &= ~MTD_WRITEABLE;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200545 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 +0900546 part->name);
547 }
548
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100549 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200550 slave->mtd.ecc_step_size = parent->ecc_step_size;
551 slave->mtd.ecc_strength = parent->ecc_strength;
552 slave->mtd.bitflip_threshold = parent->bitflip_threshold;
Mike Dunnd062d4e2012-04-25 12:06:08 -0700553
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200554 if (parent->_block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000555 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900556
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900557 while (offs < slave->mtd.size) {
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200558 if (mtd_block_isreserved(parent, offs + slave->offset))
Ezequiel Garciafdf43a42014-03-21 08:57:44 -0300559 slave->mtd.ecc_stats.bbtblocks++;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200560 else if (mtd_block_isbad(parent, offs + slave->offset))
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900561 slave->mtd.ecc_stats.badblocks++;
562 offs += slave->mtd.erasesize;
563 }
564 }
565
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900566out_register:
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900567 return slave;
568}
569
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700570static ssize_t mtd_partition_offset_show(struct device *dev,
571 struct device_attribute *attr, char *buf)
572{
573 struct mtd_info *mtd = dev_get_drvdata(dev);
Brian Norris25245342015-11-19 19:28:39 -0800574 struct mtd_part *part = mtd_to_part(mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700575 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
576}
577
578static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
579
580static const struct attribute *mtd_partition_attrs[] = {
581 &dev_attr_offset.attr,
582 NULL
583};
584
585static int mtd_add_partition_attrs(struct mtd_part *new)
586{
587 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
588 if (ret)
589 printk(KERN_WARNING
590 "mtd: failed to create partition attrs, err=%d\n", ret);
591 return ret;
592}
593
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200594int mtd_add_partition(struct mtd_info *parent, const char *name,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300595 long long offset, long long length)
596{
597 struct mtd_partition part;
Dan Ehrenberg3a434f62015-04-02 15:15:12 -0700598 struct mtd_part *new;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300599 int ret = 0;
600
601 /* the direct offset is expected */
602 if (offset == MTDPART_OFS_APPEND ||
603 offset == MTDPART_OFS_NXTBLK)
604 return -EINVAL;
605
606 if (length == MTDPART_SIZ_FULL)
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200607 length = parent->size - offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300608
609 if (length <= 0)
610 return -EINVAL;
611
Brian Norris93867232015-11-11 16:47:52 -0800612 memset(&part, 0, sizeof(part));
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300613 part.name = name;
614 part.size = length;
615 part.offset = offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300616
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200617 new = allocate_partition(parent, &part, -1, offset);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300618 if (IS_ERR(new))
619 return PTR_ERR(new);
620
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300621 mutex_lock(&mtd_partitions_mutex);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300622 list_add(&new->list, &mtd_partitions);
623 mutex_unlock(&mtd_partitions_mutex);
624
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100625 ret = add_mtd_device(&new->mtd);
626 if (ret)
627 goto err_remove_part;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300628
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700629 mtd_add_partition_attrs(new);
630
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100631 return 0;
632
633err_remove_part:
634 mutex_lock(&mtd_partitions_mutex);
635 list_del(&new->list);
636 mutex_unlock(&mtd_partitions_mutex);
637
638 free_partition(new);
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100639
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300640 return ret;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300641}
642EXPORT_SYMBOL_GPL(mtd_add_partition);
643
Rafał Miłecki08263a92017-06-21 08:26:42 +0200644/**
645 * __mtd_del_partition - delete MTD partition
646 *
647 * @priv: internal MTD struct for partition to be deleted
648 *
649 * This function must be called with the partitions mutex locked.
650 */
651static int __mtd_del_partition(struct mtd_part *priv)
652{
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200653 struct mtd_part *child, *next;
Rafał Miłecki08263a92017-06-21 08:26:42 +0200654 int err;
655
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200656 list_for_each_entry_safe(child, next, &mtd_partitions, list) {
657 if (child->parent == &priv->mtd) {
658 err = __mtd_del_partition(child);
659 if (err)
660 return err;
661 }
662 }
663
Rafał Miłeckic5ceaba2017-06-21 08:26:43 +0200664 sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);
665
Rafał Miłecki08263a92017-06-21 08:26:42 +0200666 err = del_mtd_device(&priv->mtd);
667 if (err)
668 return err;
669
670 list_del(&priv->list);
671 free_partition(priv);
672
673 return 0;
674}
675
676/*
677 * This function unregisters and destroy all slave MTD objects which are
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200678 * attached to the given MTD object.
Rafał Miłecki08263a92017-06-21 08:26:42 +0200679 */
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200680int del_mtd_partitions(struct mtd_info *mtd)
Rafał Miłecki08263a92017-06-21 08:26:42 +0200681{
682 struct mtd_part *slave, *next;
683 int ret, err = 0;
684
685 mutex_lock(&mtd_partitions_mutex);
686 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200687 if (slave->parent == mtd) {
Rafał Miłecki08263a92017-06-21 08:26:42 +0200688 ret = __mtd_del_partition(slave);
689 if (ret < 0)
690 err = ret;
691 }
692 mutex_unlock(&mtd_partitions_mutex);
693
694 return err;
695}
696
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200697int mtd_del_partition(struct mtd_info *mtd, int partno)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300698{
699 struct mtd_part *slave, *next;
700 int ret = -EINVAL;
701
702 mutex_lock(&mtd_partitions_mutex);
703 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200704 if ((slave->parent == mtd) &&
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300705 (slave->mtd.index == partno)) {
Rafał Miłecki08263a92017-06-21 08:26:42 +0200706 ret = __mtd_del_partition(slave);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300707 break;
708 }
709 mutex_unlock(&mtd_partitions_mutex);
710
711 return ret;
712}
713EXPORT_SYMBOL_GPL(mtd_del_partition);
714
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715/*
716 * This function, given a master MTD object and a partition table, creates
717 * and registers slave MTD objects which are bound to the master according to
718 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700719 *
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700720 * For historical reasons, this function's caller only registers the master
721 * if the MTD_PARTITIONED_MASTER config option is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722 */
723
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000724int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 const struct mtd_partition *parts,
726 int nbparts)
727{
728 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000729 uint64_t cur_offset = 0;
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100730 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900732 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
734 for (i = 0; i < nbparts; i++) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300735 slave = allocate_partition(master, parts + i, i, cur_offset);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200736 if (IS_ERR(slave)) {
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100737 ret = PTR_ERR(slave);
738 goto err_del_partitions;
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200739 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300740
741 mutex_lock(&mtd_partitions_mutex);
742 list_add(&slave->list, &mtd_partitions);
743 mutex_unlock(&mtd_partitions_mutex);
744
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100745 ret = add_mtd_device(&slave->mtd);
746 if (ret) {
747 mutex_lock(&mtd_partitions_mutex);
748 list_del(&slave->list);
749 mutex_unlock(&mtd_partitions_mutex);
750
751 free_partition(slave);
752 goto err_del_partitions;
753 }
754
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700755 mtd_add_partition_attrs(slave);
Rafał Miłecki76a83222018-07-13 16:32:21 +0200756 /* Look for subpartitions */
757 parse_mtd_partitions(&slave->mtd, parts[i].types, NULL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300758
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 }
761
762 return 0;
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100763
764err_del_partitions:
765 del_mtd_partitions(master);
766
767 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769
770static DEFINE_SPINLOCK(part_parser_lock);
771static LIST_HEAD(part_parsers);
772
Brian Norris5531ae42015-12-04 15:25:15 -0800773static struct mtd_part_parser *mtd_part_parser_get(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700774{
Chris Malley71a928c2008-05-19 20:11:50 +0100775 struct mtd_part_parser *p, *ret = NULL;
776
Linus Torvalds1da177e2005-04-16 15:20:36 -0700777 spin_lock(&part_parser_lock);
778
Chris Malley71a928c2008-05-19 20:11:50 +0100779 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
781 ret = p;
782 break;
783 }
Chris Malley71a928c2008-05-19 20:11:50 +0100784
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 spin_unlock(&part_parser_lock);
786
787 return ret;
788}
789
Brian Norris5531ae42015-12-04 15:25:15 -0800790static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
791{
792 module_put(p->owner);
793}
Dmitry Eremin-Solenikov953b3bd2011-06-23 15:26:14 +0400794
Brian Norrisadc83bf2015-12-09 10:24:03 -0800795/*
796 * Many partition parsers just expected the core to kfree() all their data in
797 * one chunk. Do that by default.
798 */
799static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
800 int nr_parts)
801{
802 kfree(pparts);
803}
804
Brian Norrisb9eab012015-11-11 19:13:29 -0800805int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806{
Brian Norrisb9eab012015-11-11 19:13:29 -0800807 p->owner = owner;
808
Brian Norrisadc83bf2015-12-09 10:24:03 -0800809 if (!p->cleanup)
810 p->cleanup = &mtd_part_parser_cleanup_default;
811
Linus Torvalds1da177e2005-04-16 15:20:36 -0700812 spin_lock(&part_parser_lock);
813 list_add(&p->list, &part_parsers);
814 spin_unlock(&part_parser_lock);
Brian Norrisb9eab012015-11-11 19:13:29 -0800815
816 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817}
Brian Norrisb9eab012015-11-11 19:13:29 -0800818EXPORT_SYMBOL_GPL(__register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819
Axel Lincf3b2b12013-12-01 18:59:15 +0800820void deregister_mtd_parser(struct mtd_part_parser *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700821{
822 spin_lock(&part_parser_lock);
823 list_del(&p->list);
824 spin_unlock(&part_parser_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900826EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700827
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300828/*
829 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
830 * are changing this array!
831 */
Artem Bityutskiyccef4dc2013-03-12 10:51:35 +0200832static const char * const default_mtd_part_types[] = {
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400833 "cmdlinepart",
834 "ofpart",
835 NULL
836};
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400837
Rafał Miłecki76a83222018-07-13 16:32:21 +0200838/* Check DT only when looking for subpartitions. */
839static const char * const default_subpartition_types[] = {
840 "ofpart",
841 NULL
842};
843
Brian Norris01f9c722017-05-23 07:30:20 +0200844static int mtd_part_do_parse(struct mtd_part_parser *parser,
845 struct mtd_info *master,
846 struct mtd_partitions *pparts,
847 struct mtd_part_parser_data *data)
848{
849 int ret;
850
851 ret = (*parser->parse_fn)(master, &pparts->parts, data);
852 pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
853 if (ret <= 0)
854 return ret;
855
856 pr_notice("%d %s partitions found on MTD device %s\n", ret,
857 parser->name, master->name);
858
859 pparts->nr_parts = ret;
860 pparts->parser = parser;
861
862 return ret;
863}
864
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300865/**
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100866 * mtd_part_get_compatible_parser - find MTD parser by a compatible string
867 *
868 * @compat: compatible string describing partitions in a device tree
869 *
870 * MTD parsers can specify supported partitions by providing a table of
871 * compatibility strings. This function finds a parser that advertises support
872 * for a passed value of "compatible".
873 */
874static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
875{
876 struct mtd_part_parser *p, *ret = NULL;
877
878 spin_lock(&part_parser_lock);
879
880 list_for_each_entry(p, &part_parsers, list) {
881 const struct of_device_id *matches;
882
883 matches = p->of_match_table;
884 if (!matches)
885 continue;
886
887 for (; matches->compatible[0]; matches++) {
888 if (!strcmp(matches->compatible, compat) &&
889 try_module_get(p->owner)) {
890 ret = p;
891 break;
892 }
893 }
894
895 if (ret)
896 break;
897 }
898
899 spin_unlock(&part_parser_lock);
900
901 return ret;
902}
903
904static int mtd_part_of_parse(struct mtd_info *master,
905 struct mtd_partitions *pparts)
906{
907 struct mtd_part_parser *parser;
908 struct device_node *np;
909 struct property *prop;
910 const char *compat;
Rafał Miłeckic0faf432018-03-14 13:10:43 +0100911 const char *fixed = "fixed-partitions";
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100912 int ret, err = 0;
913
Rafał Miłecki76a83222018-07-13 16:32:21 +0200914 np = mtd_get_of_node(master);
Miquel Raynal85516a982018-09-07 16:35:54 +0200915 if (mtd_is_partition(master))
916 of_node_get(np);
917 else
Rafał Miłecki76a83222018-07-13 16:32:21 +0200918 np = of_get_child_by_name(np, "partitions");
Miquel Raynal85516a982018-09-07 16:35:54 +0200919
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100920 of_property_for_each_string(np, "compatible", prop, compat) {
921 parser = mtd_part_get_compatible_parser(compat);
922 if (!parser)
923 continue;
924 ret = mtd_part_do_parse(parser, master, pparts, NULL);
925 if (ret > 0) {
926 of_node_put(np);
927 return ret;
928 }
929 mtd_part_parser_put(parser);
930 if (ret < 0 && !err)
931 err = ret;
932 }
933 of_node_put(np);
934
935 /*
Rafał Miłeckic0faf432018-03-14 13:10:43 +0100936 * For backward compatibility we have to try the "fixed-partitions"
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100937 * parser. It supports old DT format with partitions specified as a
938 * direct subnodes of a flash device DT node without any compatibility
939 * specified we could match.
940 */
941 parser = mtd_part_parser_get(fixed);
942 if (!parser && !request_module("%s", fixed))
943 parser = mtd_part_parser_get(fixed);
944 if (parser) {
945 ret = mtd_part_do_parse(parser, master, pparts, NULL);
946 if (ret > 0)
947 return ret;
948 mtd_part_parser_put(parser);
949 if (ret < 0 && !err)
950 err = ret;
951 }
952
953 return err;
954}
955
956/**
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200957 * parse_mtd_partitions - parse and register MTD partitions
958 *
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300959 * @master: the master partition (describes whole MTD device)
960 * @types: names of partition parsers to try or %NULL
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400961 * @data: MTD partition parser-specific data
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300962 *
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200963 * This function tries to find & register partitions on MTD device @master. It
964 * uses MTD partition parsers, specified in @types. However, if @types is %NULL,
965 * then the default list of parsers is used. The default list contains only the
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400966 * "cmdlinepart" and "ofpart" parsers ATM.
Huang Shijiec51803d2012-08-18 13:07:41 -0400967 * Note: If there are more then one parser in @types, the kernel only takes the
968 * partitions parsed out by the first parser.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300969 *
970 * This function may return:
971 * o a negative error code in case of failure
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200972 * o number of found partitions otherwise
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300973 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200974int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400975 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976{
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200977 struct mtd_partitions pparts = { };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700978 struct mtd_part_parser *parser;
Brian Norris5a2415b2015-10-11 13:03:47 -0700979 int ret, err = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000980
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400981 if (!types)
Rafał Miłecki76a83222018-07-13 16:32:21 +0200982 types = mtd_is_partition(master) ? default_subpartition_types :
983 default_mtd_part_types;
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400984
Brian Norris5a2415b2015-10-11 13:03:47 -0700985 for ( ; *types; types++) {
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100986 /*
987 * ofpart is a special type that means OF partitioning info
988 * should be used. It requires a bit different logic so it is
989 * handled in a separated function.
990 */
991 if (!strcmp(*types, "ofpart")) {
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200992 ret = mtd_part_of_parse(master, &pparts);
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100993 } else {
994 pr_debug("%s: parsing partitions %s\n", master->name,
995 *types);
Brian Norris5531ae42015-12-04 15:25:15 -0800996 parser = mtd_part_parser_get(*types);
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100997 if (!parser && !request_module("%s", *types))
998 parser = mtd_part_parser_get(*types);
999 pr_debug("%s: got parser %s\n", master->name,
1000 parser ? parser->name : NULL);
1001 if (!parser)
1002 continue;
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +02001003 ret = mtd_part_do_parse(parser, master, &pparts, data);
Rafał Miłecki5b644aa2018-03-14 13:10:42 +01001004 if (ret <= 0)
1005 mtd_part_parser_put(parser);
1006 }
Brian Norris01f9c722017-05-23 07:30:20 +02001007 /* Found partitions! */
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +02001008 if (ret > 0) {
1009 err = add_mtd_partitions(master, pparts.parts,
1010 pparts.nr_parts);
1011 mtd_part_parser_cleanup(&pparts);
1012 return err ? err : pparts.nr_parts;
1013 }
Brian Norris5a2415b2015-10-11 13:03:47 -07001014 /*
1015 * Stash the first error we see; only report it if no parser
1016 * succeeds
1017 */
1018 if (ret < 0 && !err)
1019 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 }
Brian Norris5a2415b2015-10-11 13:03:47 -07001021 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022}
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001023
Brian Norrisadc83bf2015-12-09 10:24:03 -08001024void mtd_part_parser_cleanup(struct mtd_partitions *parts)
1025{
1026 const struct mtd_part_parser *parser;
1027
1028 if (!parts)
1029 return;
1030
1031 parser = parts->parser;
1032 if (parser) {
1033 if (parser->cleanup)
1034 parser->cleanup(parts->parts, parts->nr_parts);
1035
1036 mtd_part_parser_put(parser);
1037 }
1038}
1039
Richard Genoud5dee4672012-07-10 18:23:39 +02001040int mtd_is_partition(const struct mtd_info *mtd)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001041{
1042 struct mtd_part *part;
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +02001043 int ispart = 0;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001044
1045 mutex_lock(&mtd_partitions_mutex);
1046 list_for_each_entry(part, &mtd_partitions, list)
1047 if (&part->mtd == mtd) {
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +02001048 ispart = 1;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001049 break;
1050 }
1051 mutex_unlock(&mtd_partitions_mutex);
1052
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +02001053 return ispart;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +03001054}
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +02001055EXPORT_SYMBOL_GPL(mtd_is_partition);
Richard Genoud62082e562012-07-10 18:23:40 +02001056
1057/* Returns the size of the entire flash chip */
1058uint64_t mtd_get_device_size(const struct mtd_info *mtd)
1059{
1060 if (!mtd_is_partition(mtd))
1061 return mtd->size;
1062
Rafał Miłecki97519dc2017-06-21 08:26:45 +02001063 return mtd_get_device_size(mtd_to_part(mtd)->parent);
Richard Genoud62082e562012-07-10 18:23:40 +02001064}
1065EXPORT_SYMBOL_GPL(mtd_get_device_size);