blob: e32a0ac2298f6c87688f37ead7cd621fb8e608aa [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>
Dan Ehrenberg727dc612015-04-02 15:15:10 -070033#include <linux/kconfig.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
41/* Our partition node structure */
42struct mtd_part {
43 struct mtd_info mtd;
44 struct mtd_info *master;
Adrian Hunter69423d92008-12-10 13:37:21 +000045 uint64_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
49/*
50 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
Brian Norris25245342015-11-19 19:28:39 -080051 * the pointer to that structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070052 */
Brian Norris25245342015-11-19 19:28:39 -080053static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
54{
55 return container_of(mtd, struct mtd_part, mtd);
56}
Linus Torvalds1da177e2005-04-16 15:20:36 -070057
Thomas Gleixner97894cd2005-11-07 11:15:26 +000058
59/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070060 * MTD methods which simply translate the effective address and pass through
61 * to the _real_ device.
62 */
63
Atsushi Nemotob33a2882008-07-19 01:00:33 +090064static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
65 size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070066{
Brian Norris25245342015-11-19 19:28:39 -080067 struct mtd_part *part = mtd_to_part(mtd);
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020068 struct mtd_ecc_stats stats;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020069 int res;
70
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020071 stats = part->master->ecc_stats;
Mike Dunn994c8402012-03-03 13:13:06 -080072 res = part->master->_read(part->master, from + part->offset, len,
73 retlen, buf);
Mike Dunnedbc45402012-04-25 12:06:11 -070074 if (unlikely(mtd_is_eccerr(res)))
75 mtd->ecc_stats.failed +=
76 part->master->ecc_stats.failed - stats.failed;
77 else
78 mtd->ecc_stats.corrected +=
79 part->master->ecc_stats.corrected - stats.corrected;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020080 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
82
Atsushi Nemotob33a2882008-07-19 01:00:33 +090083static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
84 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070085{
Brian Norris25245342015-11-19 19:28:39 -080086 struct mtd_part *part = mtd_to_part(mtd);
Artem Bityutskiy5def4892012-02-03 16:23:52 +020087
Mike Dunn994c8402012-03-03 13:13:06 -080088 return part->master->_point(part->master, from + part->offset, len,
89 retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
Thomas Gleixner9223a452006-05-23 17:21:03 +020091
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +020092static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070093{
Brian Norris25245342015-11-19 19:28:39 -080094 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
Mike Dunn994c8402012-03-03 13:13:06 -080096 return part->master->_unpoint(part->master, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
David Howells402d3262009-02-12 10:40:00 +000099static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
100 unsigned long len,
101 unsigned long offset,
102 unsigned long flags)
103{
Brian Norris25245342015-11-19 19:28:39 -0800104 struct mtd_part *part = mtd_to_part(mtd);
David Howells402d3262009-02-12 10:40:00 +0000105
106 offset += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800107 return part->master->_get_unmapped_area(part->master, len, offset,
108 flags);
David Howells402d3262009-02-12 10:40:00 +0000109}
110
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200111static int part_read_oob(struct mtd_info *mtd, loff_t from,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900112 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113{
Brian Norris25245342015-11-19 19:28:39 -0800114 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200115 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 if (from >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200118 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300119 if (ops->datbuf && from + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200120 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200121
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200122 /*
123 * If OOB is also requested, make sure that we do not read past the end
124 * of this partition.
125 */
126 if (ops->oobbuf) {
127 size_t len, pages;
128
Boris BREZILLON29f10582016-03-07 10:46:52 +0100129 len = mtd_oobavail(mtd, ops);
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200130 pages = mtd_div_by_ws(mtd->size, mtd);
131 pages -= mtd_div_by_ws(from, mtd);
132 if (ops->ooboffs + ops->ooblen > pages * len)
133 return -EINVAL;
134 }
135
Mike Dunn994c8402012-03-03 13:13:06 -0800136 res = part->master->_read_oob(part->master, from + part->offset, ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200137 if (unlikely(res)) {
Brian Norrisd57f40542011-09-20 18:34:25 -0700138 if (mtd_is_bitflip(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200139 mtd->ecc_stats.corrected++;
Brian Norrisd57f40542011-09-20 18:34:25 -0700140 if (mtd_is_eccerr(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200141 mtd->ecc_stats.failed++;
142 }
143 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144}
145
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900146static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
147 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148{
Brian Norris25245342015-11-19 19:28:39 -0800149 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800150 return part->master->_read_user_prot_reg(part->master, from, len,
151 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152}
153
Christian Riesch4b78fc42014-01-28 09:29:44 +0100154static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
155 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000156{
Brian Norris25245342015-11-19 19:28:39 -0800157 struct mtd_part *part = mtd_to_part(mtd);
Christian Riesch4b78fc42014-01-28 09:29:44 +0100158 return part->master->_get_user_prot_info(part->master, len, retlen,
159 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000160}
161
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900162static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
163 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164{
Brian Norris25245342015-11-19 19:28:39 -0800165 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800166 return part->master->_read_fact_prot_reg(part->master, from, len,
167 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168}
169
Christian Riesch4b78fc42014-01-28 09:29:44 +0100170static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
171 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000172{
Brian Norris25245342015-11-19 19:28:39 -0800173 struct mtd_part *part = mtd_to_part(mtd);
Christian Riesch4b78fc42014-01-28 09:29:44 +0100174 return part->master->_get_fact_prot_info(part->master, len, retlen,
175 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000176}
177
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900178static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
179 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Brian Norris25245342015-11-19 19:28:39 -0800181 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800182 return part->master->_write(part->master, to + part->offset, len,
183 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900186static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
187 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000188{
Brian Norris25245342015-11-19 19:28:39 -0800189 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800190 return part->master->_panic_write(part->master, to + part->offset, len,
191 retlen, buf);
Richard Purdie388bbb02008-02-06 10:17:15 +0000192}
193
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200194static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900195 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Brian Norris25245342015-11-19 19:28:39 -0800197 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200200 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300201 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200202 return -EINVAL;
Mike Dunn994c8402012-03-03 13:13:06 -0800203 return part->master->_write_oob(part->master, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}
205
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900206static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
207 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208{
Brian Norris25245342015-11-19 19:28:39 -0800209 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800210 return part->master->_write_user_prot_reg(part->master, from, len,
211 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}
213
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900214static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
215 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000216{
Brian Norris25245342015-11-19 19:28:39 -0800217 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800218 return part->master->_lock_user_prot_reg(part->master, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000219}
220
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900221static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
222 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223{
Brian Norris25245342015-11-19 19:28:39 -0800224 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800225 return part->master->_writev(part->master, vecs, count,
226 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227}
228
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900229static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230{
Brian Norris25245342015-11-19 19:28:39 -0800231 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 int ret;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200233
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 instr->addr += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800235 ret = part->master->_erase(part->master, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200236 if (ret) {
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300237 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Adrian Hunter74641d72007-03-08 12:20:12 +0200238 instr->fail_addr -= part->offset;
239 instr->addr -= part->offset;
240 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 return ret;
242}
243
244void mtd_erase_callback(struct erase_info *instr)
245{
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200246 if (instr->mtd->_erase == part_erase) {
Brian Norris25245342015-11-19 19:28:39 -0800247 struct mtd_part *part = mtd_to_part(instr->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300249 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 instr->fail_addr -= part->offset;
251 instr->addr -= part->offset;
252 }
253 if (instr->callback)
254 instr->callback(instr);
255}
256EXPORT_SYMBOL_GPL(mtd_erase_callback);
257
Adrian Hunter69423d92008-12-10 13:37:21 +0000258static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259{
Brian Norris25245342015-11-19 19:28:39 -0800260 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800261 return part->master->_lock(part->master, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262}
263
Adrian Hunter69423d92008-12-10 13:37:21 +0000264static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
Brian Norris25245342015-11-19 19:28:39 -0800266 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800267 return part->master->_unlock(part->master, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268}
269
Richard Cochran99384242010-06-14 18:10:33 +0200270static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
271{
Brian Norris25245342015-11-19 19:28:39 -0800272 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800273 return part->master->_is_locked(part->master, ofs + part->offset, len);
Richard Cochran99384242010-06-14 18:10:33 +0200274}
275
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276static void part_sync(struct mtd_info *mtd)
277{
Brian Norris25245342015-11-19 19:28:39 -0800278 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800279 part->master->_sync(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280}
281
282static int part_suspend(struct mtd_info *mtd)
283{
Brian Norris25245342015-11-19 19:28:39 -0800284 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800285 return part->master->_suspend(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286}
287
288static void part_resume(struct mtd_info *mtd)
289{
Brian Norris25245342015-11-19 19:28:39 -0800290 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800291 part->master->_resume(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292}
293
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300294static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
295{
Brian Norris25245342015-11-19 19:28:39 -0800296 struct mtd_part *part = mtd_to_part(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300297 ofs += part->offset;
298 return part->master->_block_isreserved(part->master, ofs);
299}
300
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900301static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302{
Brian Norris25245342015-11-19 19:28:39 -0800303 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 ofs += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800305 return part->master->_block_isbad(part->master, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306}
307
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900308static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309{
Brian Norris25245342015-11-19 19:28:39 -0800310 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200311 int res;
312
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 ofs += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800314 res = part->master->_block_markbad(part->master, ofs);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200315 if (!res)
316 mtd->ecc_stats.badblocks++;
317 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318}
319
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100320static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
321 struct mtd_oob_region *oobregion)
322{
323 struct mtd_part *part = mtd_to_part(mtd);
324
325 return mtd_ooblayout_ecc(part->master, section, oobregion);
326}
327
328static int part_ooblayout_free(struct mtd_info *mtd, int section,
329 struct mtd_oob_region *oobregion)
330{
331 struct mtd_part *part = mtd_to_part(mtd);
332
333 return mtd_ooblayout_free(part->master, section, oobregion);
334}
335
336static const struct mtd_ooblayout_ops part_ooblayout_ops = {
337 .ecc = part_ooblayout_ecc,
338 .free = part_ooblayout_free,
339};
340
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300341static inline void free_partition(struct mtd_part *p)
342{
343 kfree(p->mtd.name);
344 kfree(p);
345}
346
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000347/*
348 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 * attached to the given master MTD object.
350 */
351
352int del_mtd_partitions(struct mtd_info *master)
353{
Chris Malley71a928c2008-05-19 20:11:50 +0100354 struct mtd_part *slave, *next;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300355 int ret, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300357 mutex_lock(&mtd_partitions_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100358 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 if (slave->master == master) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300360 ret = del_mtd_device(&slave->mtd);
361 if (ret < 0) {
362 err = ret;
363 continue;
364 }
Chris Malley71a928c2008-05-19 20:11:50 +0100365 list_del(&slave->list);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300366 free_partition(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300368 mutex_unlock(&mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300370 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371}
372
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300373static struct mtd_part *allocate_partition(struct mtd_info *master,
374 const struct mtd_partition *part, int partno,
375 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900376{
377 struct mtd_part *slave;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300378 char *name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900379
380 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900381 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300382 name = kstrdup(part->name, GFP_KERNEL);
383 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900384 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300385 master->name);
386 kfree(name);
387 kfree(slave);
388 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900389 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900390
391 /* set up the MTD object for this partition */
392 slave->mtd.type = master->type;
393 slave->mtd.flags = master->flags & ~part->mask_flags;
394 slave->mtd.size = part->size;
395 slave->mtd.writesize = master->writesize;
Anatolij Gustschin7fa33ac2010-12-16 23:42:18 +0100396 slave->mtd.writebufsize = master->writebufsize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900397 slave->mtd.oobsize = master->oobsize;
398 slave->mtd.oobavail = master->oobavail;
399 slave->mtd.subpage_sft = master->subpage_sft;
Boris Brezillon477b0222015-11-16 15:53:13 +0100400 slave->mtd.pairing = master->pairing;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900401
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300402 slave->mtd.name = name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900403 slave->mtd.owner = master->owner;
404
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700405 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
406 * concern for showing the same data in multiple partitions.
407 * However, it is very useful to have the master node present,
408 * so the MTD_PARTITIONED_MASTER option allows that. The master
409 * will have device nodes etc only if this is set, so make the
410 * parent conditional on that option. Note, this is a way to
411 * distinguish between the master and the partition in sysfs.
David Brownell1f24b5a2009-03-26 00:42:41 -0700412 */
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700413 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) ?
414 &master->dev :
415 master->dev.parent;
David Brownell1f24b5a2009-03-26 00:42:41 -0700416
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200417 slave->mtd._read = part_read;
418 slave->mtd._write = part_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900419
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200420 if (master->_panic_write)
421 slave->mtd._panic_write = part_panic_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900422
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200423 if (master->_point && master->_unpoint) {
424 slave->mtd._point = part_point;
425 slave->mtd._unpoint = part_unpoint;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900426 }
427
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200428 if (master->_get_unmapped_area)
429 slave->mtd._get_unmapped_area = part_get_unmapped_area;
430 if (master->_read_oob)
431 slave->mtd._read_oob = part_read_oob;
432 if (master->_write_oob)
433 slave->mtd._write_oob = part_write_oob;
434 if (master->_read_user_prot_reg)
435 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
436 if (master->_read_fact_prot_reg)
437 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
438 if (master->_write_user_prot_reg)
439 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
440 if (master->_lock_user_prot_reg)
441 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
442 if (master->_get_user_prot_info)
443 slave->mtd._get_user_prot_info = part_get_user_prot_info;
444 if (master->_get_fact_prot_info)
445 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
446 if (master->_sync)
447 slave->mtd._sync = part_sync;
448 if (!partno && !master->dev.class && master->_suspend &&
449 master->_resume) {
450 slave->mtd._suspend = part_suspend;
451 slave->mtd._resume = part_resume;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900452 }
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200453 if (master->_writev)
454 slave->mtd._writev = part_writev;
455 if (master->_lock)
456 slave->mtd._lock = part_lock;
457 if (master->_unlock)
458 slave->mtd._unlock = part_unlock;
459 if (master->_is_locked)
460 slave->mtd._is_locked = part_is_locked;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300461 if (master->_block_isreserved)
462 slave->mtd._block_isreserved = part_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200463 if (master->_block_isbad)
464 slave->mtd._block_isbad = part_block_isbad;
465 if (master->_block_markbad)
466 slave->mtd._block_markbad = part_block_markbad;
467 slave->mtd._erase = part_erase;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900468 slave->master = master;
469 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900470
471 if (slave->offset == MTDPART_OFS_APPEND)
472 slave->offset = cur_offset;
473 if (slave->offset == MTDPART_OFS_NXTBLK) {
474 slave->offset = cur_offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000475 if (mtd_mod_by_eb(cur_offset, master) != 0) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900476 /* Round up to next erasesize */
Adrian Hunter69423d92008-12-10 13:37:21 +0000477 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900478 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000479 "0x%012llx -> 0x%012llx\n", partno,
480 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900481 }
482 }
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400483 if (slave->offset == MTDPART_OFS_RETAIN) {
484 slave->offset = cur_offset;
485 if (master->size - slave->offset >= slave->mtd.size) {
486 slave->mtd.size = master->size - slave->offset
487 - slave->mtd.size;
488 } else {
489 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
490 part->name, master->size - slave->offset,
491 slave->mtd.size);
492 /* register to preserve ordering */
493 goto out_register;
494 }
495 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900496 if (slave->mtd.size == MTDPART_SIZ_FULL)
497 slave->mtd.size = master->size - slave->offset;
498
Adrian Hunter69423d92008-12-10 13:37:21 +0000499 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
500 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900501
502 /* let's do some sanity checks */
503 if (slave->offset >= master->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900504 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900505 slave->offset = 0;
506 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900507 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900508 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900509 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900510 }
511 if (slave->offset + slave->mtd.size > master->size) {
512 slave->mtd.size = master->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000513 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
514 part->name, master->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900515 }
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900516 if (master->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900517 /* Deal with variable erase size stuff */
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900518 int i, max = master->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000519 u64 end = slave->offset + slave->mtd.size;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900520 struct mtd_erase_region_info *regions = master->eraseregions;
521
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900522 /* Find the first erase regions which is part of this
523 * partition. */
524 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900525 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900526 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700527 if (i > 0)
528 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900529
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900530 /* Pick biggest erasesize */
531 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900532 if (slave->mtd.erasesize < regions[i].erasesize) {
533 slave->mtd.erasesize = regions[i].erasesize;
534 }
535 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900536 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900537 } else {
538 /* Single erase size */
539 slave->mtd.erasesize = master->erasesize;
540 }
541
542 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000543 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900544 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900545 /* FIXME: Let it be writable if it is on a boundary of
546 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900547 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900548 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900549 part->name);
550 }
551 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000552 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900553 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900554 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900555 part->name);
556 }
557
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100558 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
Huang Shijiebdf69c42013-08-16 10:10:06 +0800559 slave->mtd.ecc_step_size = master->ecc_step_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700560 slave->mtd.ecc_strength = master->ecc_strength;
Mike Dunnd062d4e2012-04-25 12:06:08 -0700561 slave->mtd.bitflip_threshold = master->bitflip_threshold;
562
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200563 if (master->_block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000564 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900565
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900566 while (offs < slave->mtd.size) {
Ezequiel Garciafdf43a42014-03-21 08:57:44 -0300567 if (mtd_block_isreserved(master, offs + slave->offset))
568 slave->mtd.ecc_stats.bbtblocks++;
569 else if (mtd_block_isbad(master, offs + slave->offset))
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900570 slave->mtd.ecc_stats.badblocks++;
571 offs += slave->mtd.erasesize;
572 }
573 }
574
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900575out_register:
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900576 return slave;
577}
578
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700579static ssize_t mtd_partition_offset_show(struct device *dev,
580 struct device_attribute *attr, char *buf)
581{
582 struct mtd_info *mtd = dev_get_drvdata(dev);
Brian Norris25245342015-11-19 19:28:39 -0800583 struct mtd_part *part = mtd_to_part(mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700584 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
585}
586
587static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
588
589static const struct attribute *mtd_partition_attrs[] = {
590 &dev_attr_offset.attr,
591 NULL
592};
593
594static int mtd_add_partition_attrs(struct mtd_part *new)
595{
596 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
597 if (ret)
598 printk(KERN_WARNING
599 "mtd: failed to create partition attrs, err=%d\n", ret);
600 return ret;
601}
602
Geert Uytterhoeven26a6d242013-11-12 20:11:26 +0100603int mtd_add_partition(struct mtd_info *master, const char *name,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300604 long long offset, long long length)
605{
606 struct mtd_partition part;
Dan Ehrenberg3a434f62015-04-02 15:15:12 -0700607 struct mtd_part *new;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300608 int ret = 0;
609
610 /* the direct offset is expected */
611 if (offset == MTDPART_OFS_APPEND ||
612 offset == MTDPART_OFS_NXTBLK)
613 return -EINVAL;
614
615 if (length == MTDPART_SIZ_FULL)
616 length = master->size - offset;
617
618 if (length <= 0)
619 return -EINVAL;
620
Brian Norris93867232015-11-11 16:47:52 -0800621 memset(&part, 0, sizeof(part));
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300622 part.name = name;
623 part.size = length;
624 part.offset = offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300625
626 new = allocate_partition(master, &part, -1, offset);
627 if (IS_ERR(new))
628 return PTR_ERR(new);
629
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300630 mutex_lock(&mtd_partitions_mutex);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300631 list_add(&new->list, &mtd_partitions);
632 mutex_unlock(&mtd_partitions_mutex);
633
634 add_mtd_device(&new->mtd);
635
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700636 mtd_add_partition_attrs(new);
637
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300638 return ret;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300639}
640EXPORT_SYMBOL_GPL(mtd_add_partition);
641
642int mtd_del_partition(struct mtd_info *master, int partno)
643{
644 struct mtd_part *slave, *next;
645 int ret = -EINVAL;
646
647 mutex_lock(&mtd_partitions_mutex);
648 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
649 if ((slave->master == master) &&
650 (slave->mtd.index == partno)) {
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700651 sysfs_remove_files(&slave->mtd.dev.kobj,
652 mtd_partition_attrs);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300653 ret = del_mtd_device(&slave->mtd);
654 if (ret < 0)
655 break;
656
657 list_del(&slave->list);
658 free_partition(slave);
659 break;
660 }
661 mutex_unlock(&mtd_partitions_mutex);
662
663 return ret;
664}
665EXPORT_SYMBOL_GPL(mtd_del_partition);
666
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667/*
668 * This function, given a master MTD object and a partition table, creates
669 * and registers slave MTD objects which are bound to the master according to
670 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700671 *
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700672 * For historical reasons, this function's caller only registers the master
673 * if the MTD_PARTITIONED_MASTER config option is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700674 */
675
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000676int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700677 const struct mtd_partition *parts,
678 int nbparts)
679{
680 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000681 uint64_t cur_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 int i;
683
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900684 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 for (i = 0; i < nbparts; i++) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300687 slave = allocate_partition(master, parts + i, i, cur_offset);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200688 if (IS_ERR(slave)) {
689 del_mtd_partitions(master);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300690 return PTR_ERR(slave);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200691 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300692
693 mutex_lock(&mtd_partitions_mutex);
694 list_add(&slave->list, &mtd_partitions);
695 mutex_unlock(&mtd_partitions_mutex);
696
697 add_mtd_device(&slave->mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700698 mtd_add_partition_attrs(slave);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701 }
702
703 return 0;
704}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706static DEFINE_SPINLOCK(part_parser_lock);
707static LIST_HEAD(part_parsers);
708
Brian Norris5531ae42015-12-04 15:25:15 -0800709static struct mtd_part_parser *mtd_part_parser_get(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710{
Chris Malley71a928c2008-05-19 20:11:50 +0100711 struct mtd_part_parser *p, *ret = NULL;
712
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713 spin_lock(&part_parser_lock);
714
Chris Malley71a928c2008-05-19 20:11:50 +0100715 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
717 ret = p;
718 break;
719 }
Chris Malley71a928c2008-05-19 20:11:50 +0100720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 spin_unlock(&part_parser_lock);
722
723 return ret;
724}
725
Brian Norris5531ae42015-12-04 15:25:15 -0800726static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
727{
728 module_put(p->owner);
729}
Dmitry Eremin-Solenikov953b3bd2011-06-23 15:26:14 +0400730
Brian Norrisadc83bf2015-12-09 10:24:03 -0800731/*
732 * Many partition parsers just expected the core to kfree() all their data in
733 * one chunk. Do that by default.
734 */
735static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
736 int nr_parts)
737{
738 kfree(pparts);
739}
740
Brian Norrisb9eab012015-11-11 19:13:29 -0800741int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
Brian Norrisb9eab012015-11-11 19:13:29 -0800743 p->owner = owner;
744
Brian Norrisadc83bf2015-12-09 10:24:03 -0800745 if (!p->cleanup)
746 p->cleanup = &mtd_part_parser_cleanup_default;
747
Linus Torvalds1da177e2005-04-16 15:20:36 -0700748 spin_lock(&part_parser_lock);
749 list_add(&p->list, &part_parsers);
750 spin_unlock(&part_parser_lock);
Brian Norrisb9eab012015-11-11 19:13:29 -0800751
752 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753}
Brian Norrisb9eab012015-11-11 19:13:29 -0800754EXPORT_SYMBOL_GPL(__register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755
Axel Lincf3b2b12013-12-01 18:59:15 +0800756void deregister_mtd_parser(struct mtd_part_parser *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 spin_lock(&part_parser_lock);
759 list_del(&p->list);
760 spin_unlock(&part_parser_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900762EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300764/*
765 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
766 * are changing this array!
767 */
Artem Bityutskiyccef4dc2013-03-12 10:51:35 +0200768static const char * const default_mtd_part_types[] = {
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400769 "cmdlinepart",
770 "ofpart",
771 NULL
772};
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400773
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300774/**
775 * parse_mtd_partitions - parse MTD partitions
776 * @master: the master partition (describes whole MTD device)
777 * @types: names of partition parsers to try or %NULL
Brian Norris07fd2f82015-12-04 15:25:17 -0800778 * @pparts: info about partitions found is returned here
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400779 * @data: MTD partition parser-specific data
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300780 *
781 * This function tries to find partition on MTD device @master. It uses MTD
782 * partition parsers, specified in @types. However, if @types is %NULL, then
783 * the default list of parsers is used. The default list contains only the
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400784 * "cmdlinepart" and "ofpart" parsers ATM.
Huang Shijiec51803d2012-08-18 13:07:41 -0400785 * Note: If there are more then one parser in @types, the kernel only takes the
786 * partitions parsed out by the first parser.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300787 *
788 * This function may return:
789 * o a negative error code in case of failure
Brian Norris07fd2f82015-12-04 15:25:17 -0800790 * o zero otherwise, and @pparts will describe the partitions, number of
Brian Norrisadc83bf2015-12-09 10:24:03 -0800791 * partitions, and the parser which parsed them. Caller must release
792 * resources with mtd_part_parser_cleanup() when finished with the returned
793 * data.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300794 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200795int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
Brian Norris07fd2f82015-12-04 15:25:17 -0800796 struct mtd_partitions *pparts,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400797 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798{
799 struct mtd_part_parser *parser;
Brian Norris5a2415b2015-10-11 13:03:47 -0700800 int ret, err = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000801
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400802 if (!types)
803 types = default_mtd_part_types;
804
Brian Norris5a2415b2015-10-11 13:03:47 -0700805 for ( ; *types; types++) {
Michal Suchanek8e2c9922015-08-18 15:34:07 +0000806 pr_debug("%s: parsing partitions %s\n", master->name, *types);
Brian Norris5531ae42015-12-04 15:25:15 -0800807 parser = mtd_part_parser_get(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 if (!parser && !request_module("%s", *types))
Brian Norris5531ae42015-12-04 15:25:15 -0800809 parser = mtd_part_parser_get(*types);
Michal Suchanek8e2c9922015-08-18 15:34:07 +0000810 pr_debug("%s: got parser %s\n", master->name,
811 parser ? parser->name : NULL);
Artem Bityutskiy7c802fb2011-05-17 11:13:17 +0300812 if (!parser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 continue;
Brian Norris07fd2f82015-12-04 15:25:17 -0800814 ret = (*parser->parse_fn)(master, &pparts->parts, data);
Michal Suchanek8e2c9922015-08-18 15:34:07 +0000815 pr_debug("%s: parser %s: %i\n",
816 master->name, parser->name, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000818 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 ret, parser->name, master->name);
Brian Norris07fd2f82015-12-04 15:25:17 -0800820 pparts->nr_parts = ret;
821 pparts->parser = parser;
822 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700823 }
Brian Norrisadc83bf2015-12-09 10:24:03 -0800824 mtd_part_parser_put(parser);
Brian Norris5a2415b2015-10-11 13:03:47 -0700825 /*
826 * Stash the first error we see; only report it if no parser
827 * succeeds
828 */
829 if (ret < 0 && !err)
830 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
Brian Norris5a2415b2015-10-11 13:03:47 -0700832 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833}
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300834
Brian Norrisadc83bf2015-12-09 10:24:03 -0800835void mtd_part_parser_cleanup(struct mtd_partitions *parts)
836{
837 const struct mtd_part_parser *parser;
838
839 if (!parts)
840 return;
841
842 parser = parts->parser;
843 if (parser) {
844 if (parser->cleanup)
845 parser->cleanup(parts->parts, parts->nr_parts);
846
847 mtd_part_parser_put(parser);
848 }
849}
850
Richard Genoud5dee4672012-07-10 18:23:39 +0200851int mtd_is_partition(const struct mtd_info *mtd)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300852{
853 struct mtd_part *part;
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200854 int ispart = 0;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300855
856 mutex_lock(&mtd_partitions_mutex);
857 list_for_each_entry(part, &mtd_partitions, list)
858 if (&part->mtd == mtd) {
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200859 ispart = 1;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300860 break;
861 }
862 mutex_unlock(&mtd_partitions_mutex);
863
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200864 return ispart;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300865}
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200866EXPORT_SYMBOL_GPL(mtd_is_partition);
Richard Genoud62082e562012-07-10 18:23:40 +0200867
868/* Returns the size of the entire flash chip */
869uint64_t mtd_get_device_size(const struct mtd_info *mtd)
870{
871 if (!mtd_is_partition(mtd))
872 return mtd->size;
873
Brian Norris25245342015-11-19 19:28:39 -0800874 return mtd_to_part(mtd)->master->size;
Richard Genoud62082e562012-07-10 18:23:40 +0200875}
876EXPORT_SYMBOL_GPL(mtd_get_device_size);