blob: 8e634909bcfe995c76bae905a3dae9a654d29562 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Simple MTD partitioning layer
3 *
David Woodhousea1452a32010-08-08 20:58:20 +01004 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
5 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
6 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
David Woodhousea1452a32010-08-08 20:58:20 +01008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
David Woodhousea1452a32010-08-08 20:58:20 +010013 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
Thomas Gleixner97894cd2005-11-07 11:15:26 +000022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
24#include <linux/module.h>
25#include <linux/types.h>
26#include <linux/kernel.h>
27#include <linux/slab.h>
28#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kmod.h>
30#include <linux/mtd/mtd.h>
31#include <linux/mtd/partitions.h>
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +030032#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jamie Ileseea72d52011-05-23 10:23:42 +010034#include "mtdcore.h"
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* Our partition linked list */
37static LIST_HEAD(mtd_partitions);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +030038static DEFINE_MUTEX(mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* Our partition node structure */
41struct mtd_part {
42 struct mtd_info mtd;
43 struct mtd_info *master;
Adrian Hunter69423d92008-12-10 13:37:21 +000044 uint64_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
48/*
49 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
Brian Norris25245342015-11-19 19:28:39 -080050 * the pointer to that structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 */
Brian Norris25245342015-11-19 19:28:39 -080052static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
53{
54 return container_of(mtd, struct mtd_part, mtd);
55}
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
Thomas Gleixner97894cd2005-11-07 11:15:26 +000057
58/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 * MTD methods which simply translate the effective address and pass through
60 * to the _real_ device.
61 */
62
Atsushi Nemotob33a2882008-07-19 01:00:33 +090063static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
64 size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070065{
Brian Norris25245342015-11-19 19:28:39 -080066 struct mtd_part *part = mtd_to_part(mtd);
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020067 struct mtd_ecc_stats stats;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020068 int res;
69
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020070 stats = part->master->ecc_stats;
Mike Dunn994c8402012-03-03 13:13:06 -080071 res = part->master->_read(part->master, from + part->offset, len,
72 retlen, buf);
Mike Dunnedbc45402012-04-25 12:06:11 -070073 if (unlikely(mtd_is_eccerr(res)))
74 mtd->ecc_stats.failed +=
75 part->master->ecc_stats.failed - stats.failed;
76 else
77 mtd->ecc_stats.corrected +=
78 part->master->ecc_stats.corrected - stats.corrected;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020079 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Atsushi Nemotob33a2882008-07-19 01:00:33 +090082static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
83 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
Brian Norris25245342015-11-19 19:28:39 -080085 struct mtd_part *part = mtd_to_part(mtd);
Artem Bityutskiy5def4892012-02-03 16:23:52 +020086
Mike Dunn994c8402012-03-03 13:13:06 -080087 return part->master->_point(part->master, from + part->offset, len,
88 retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
Thomas Gleixner9223a452006-05-23 17:21:03 +020090
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +020091static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070092{
Brian Norris25245342015-11-19 19:28:39 -080093 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094
Mike Dunn994c8402012-03-03 13:13:06 -080095 return part->master->_unpoint(part->master, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
David Howells402d3262009-02-12 10:40:00 +000098static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
99 unsigned long len,
100 unsigned long offset,
101 unsigned long flags)
102{
Brian Norris25245342015-11-19 19:28:39 -0800103 struct mtd_part *part = mtd_to_part(mtd);
David Howells402d3262009-02-12 10:40:00 +0000104
105 offset += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800106 return part->master->_get_unmapped_area(part->master, len, offset,
107 flags);
David Howells402d3262009-02-12 10:40:00 +0000108}
109
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200110static int part_read_oob(struct mtd_info *mtd, loff_t from,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900111 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112{
Brian Norris25245342015-11-19 19:28:39 -0800113 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200114 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200115
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 if (from >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200117 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300118 if (ops->datbuf && from + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200119 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200120
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200121 /*
122 * If OOB is also requested, make sure that we do not read past the end
123 * of this partition.
124 */
125 if (ops->oobbuf) {
126 size_t len, pages;
127
Boris BREZILLON29f10582016-03-07 10:46:52 +0100128 len = mtd_oobavail(mtd, ops);
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200129 pages = mtd_div_by_ws(mtd->size, mtd);
130 pages -= mtd_div_by_ws(from, mtd);
131 if (ops->ooboffs + ops->ooblen > pages * len)
132 return -EINVAL;
133 }
134
Mike Dunn994c8402012-03-03 13:13:06 -0800135 res = part->master->_read_oob(part->master, from + part->offset, ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200136 if (unlikely(res)) {
Brian Norrisd57f40542011-09-20 18:34:25 -0700137 if (mtd_is_bitflip(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200138 mtd->ecc_stats.corrected++;
Brian Norrisd57f40542011-09-20 18:34:25 -0700139 if (mtd_is_eccerr(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200140 mtd->ecc_stats.failed++;
141 }
142 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900145static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
146 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
Brian Norris25245342015-11-19 19:28:39 -0800148 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800149 return part->master->_read_user_prot_reg(part->master, from, len,
150 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Christian Riesch4b78fc42014-01-28 09:29:44 +0100153static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
154 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000155{
Brian Norris25245342015-11-19 19:28:39 -0800156 struct mtd_part *part = mtd_to_part(mtd);
Christian Riesch4b78fc42014-01-28 09:29:44 +0100157 return part->master->_get_user_prot_info(part->master, len, retlen,
158 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000159}
160
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900161static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
162 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Brian Norris25245342015-11-19 19:28:39 -0800164 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800165 return part->master->_read_fact_prot_reg(part->master, from, len,
166 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Christian Riesch4b78fc42014-01-28 09:29:44 +0100169static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
170 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000171{
Brian Norris25245342015-11-19 19:28:39 -0800172 struct mtd_part *part = mtd_to_part(mtd);
Christian Riesch4b78fc42014-01-28 09:29:44 +0100173 return part->master->_get_fact_prot_info(part->master, len, retlen,
174 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000175}
176
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900177static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
178 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
Brian Norris25245342015-11-19 19:28:39 -0800180 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800181 return part->master->_write(part->master, to + part->offset, len,
182 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900185static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
186 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000187{
Brian Norris25245342015-11-19 19:28:39 -0800188 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800189 return part->master->_panic_write(part->master, to + part->offset, len,
190 retlen, buf);
Richard Purdie388bbb02008-02-06 10:17:15 +0000191}
192
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200193static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900194 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
Brian Norris25245342015-11-19 19:28:39 -0800196 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200199 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300200 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200201 return -EINVAL;
Mike Dunn994c8402012-03-03 13:13:06 -0800202 return part->master->_write_oob(part->master, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900205static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
206 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
Brian Norris25245342015-11-19 19:28:39 -0800208 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800209 return part->master->_write_user_prot_reg(part->master, from, len,
210 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900213static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
214 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000215{
Brian Norris25245342015-11-19 19:28:39 -0800216 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800217 return part->master->_lock_user_prot_reg(part->master, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000218}
219
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900220static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
221 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
Brian Norris25245342015-11-19 19:28:39 -0800223 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800224 return part->master->_writev(part->master, vecs, count,
225 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900228static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Brian Norris25245342015-11-19 19:28:39 -0800230 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 int ret;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 instr->addr += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800234 ret = part->master->_erase(part->master, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200235 if (ret) {
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300236 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Adrian Hunter74641d72007-03-08 12:20:12 +0200237 instr->fail_addr -= part->offset;
238 instr->addr -= part->offset;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return ret;
241}
242
243void mtd_erase_callback(struct erase_info *instr)
244{
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200245 if (instr->mtd->_erase == part_erase) {
Brian Norris25245342015-11-19 19:28:39 -0800246 struct mtd_part *part = mtd_to_part(instr->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300248 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 instr->fail_addr -= part->offset;
250 instr->addr -= part->offset;
251 }
252 if (instr->callback)
253 instr->callback(instr);
254}
255EXPORT_SYMBOL_GPL(mtd_erase_callback);
256
Adrian Hunter69423d92008-12-10 13:37:21 +0000257static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
Brian Norris25245342015-11-19 19:28:39 -0800259 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800260 return part->master->_lock(part->master, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Adrian Hunter69423d92008-12-10 13:37:21 +0000263static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Brian Norris25245342015-11-19 19:28:39 -0800265 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800266 return part->master->_unlock(part->master, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Richard Cochran99384242010-06-14 18:10:33 +0200269static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
270{
Brian Norris25245342015-11-19 19:28:39 -0800271 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800272 return part->master->_is_locked(part->master, ofs + part->offset, len);
Richard Cochran99384242010-06-14 18:10:33 +0200273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static void part_sync(struct mtd_info *mtd)
276{
Brian Norris25245342015-11-19 19:28:39 -0800277 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800278 part->master->_sync(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281static int part_suspend(struct mtd_info *mtd)
282{
Brian Norris25245342015-11-19 19:28:39 -0800283 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800284 return part->master->_suspend(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287static void part_resume(struct mtd_info *mtd)
288{
Brian Norris25245342015-11-19 19:28:39 -0800289 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800290 part->master->_resume(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300293static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
294{
Brian Norris25245342015-11-19 19:28:39 -0800295 struct mtd_part *part = mtd_to_part(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300296 ofs += part->offset;
297 return part->master->_block_isreserved(part->master, ofs);
298}
299
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900300static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
Brian Norris25245342015-11-19 19:28:39 -0800302 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 ofs += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800304 return part->master->_block_isbad(part->master, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900307static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
Brian Norris25245342015-11-19 19:28:39 -0800309 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200310 int res;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 ofs += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800313 res = part->master->_block_markbad(part->master, ofs);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200314 if (!res)
315 mtd->ecc_stats.badblocks++;
316 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Richard Weinberger5e149072016-09-21 11:43:56 +0200319static int part_get_device(struct mtd_info *mtd)
320{
321 struct mtd_part *part = mtd_to_part(mtd);
322 return part->master->_get_device(part->master);
323}
324
325static void part_put_device(struct mtd_info *mtd)
326{
327 struct mtd_part *part = mtd_to_part(mtd);
328 part->master->_put_device(part->master);
329}
330
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100331static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
332 struct mtd_oob_region *oobregion)
333{
334 struct mtd_part *part = mtd_to_part(mtd);
335
336 return mtd_ooblayout_ecc(part->master, section, oobregion);
337}
338
339static int part_ooblayout_free(struct mtd_info *mtd, int section,
340 struct mtd_oob_region *oobregion)
341{
342 struct mtd_part *part = mtd_to_part(mtd);
343
344 return mtd_ooblayout_free(part->master, section, oobregion);
345}
346
347static const struct mtd_ooblayout_ops part_ooblayout_ops = {
348 .ecc = part_ooblayout_ecc,
349 .free = part_ooblayout_free,
350};
351
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300352static inline void free_partition(struct mtd_part *p)
353{
354 kfree(p->mtd.name);
355 kfree(p);
356}
357
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000358/*
359 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 * attached to the given master MTD object.
361 */
362
363int del_mtd_partitions(struct mtd_info *master)
364{
Chris Malley71a928c2008-05-19 20:11:50 +0100365 struct mtd_part *slave, *next;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300366 int ret, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300368 mutex_lock(&mtd_partitions_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100369 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 if (slave->master == master) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300371 ret = del_mtd_device(&slave->mtd);
372 if (ret < 0) {
373 err = ret;
374 continue;
375 }
Chris Malley71a928c2008-05-19 20:11:50 +0100376 list_del(&slave->list);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300377 free_partition(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300379 mutex_unlock(&mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300381 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382}
383
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300384static struct mtd_part *allocate_partition(struct mtd_info *master,
385 const struct mtd_partition *part, int partno,
386 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900387{
388 struct mtd_part *slave;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300389 char *name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900390
391 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900392 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300393 name = kstrdup(part->name, GFP_KERNEL);
394 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900395 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300396 master->name);
397 kfree(name);
398 kfree(slave);
399 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900400 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900401
402 /* set up the MTD object for this partition */
403 slave->mtd.type = master->type;
404 slave->mtd.flags = master->flags & ~part->mask_flags;
405 slave->mtd.size = part->size;
406 slave->mtd.writesize = master->writesize;
Anatolij Gustschin7fa33ac2010-12-16 23:42:18 +0100407 slave->mtd.writebufsize = master->writebufsize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900408 slave->mtd.oobsize = master->oobsize;
409 slave->mtd.oobavail = master->oobavail;
410 slave->mtd.subpage_sft = master->subpage_sft;
Boris Brezillon477b0222015-11-16 15:53:13 +0100411 slave->mtd.pairing = master->pairing;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900412
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300413 slave->mtd.name = name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900414 slave->mtd.owner = master->owner;
415
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700416 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
417 * concern for showing the same data in multiple partitions.
418 * However, it is very useful to have the master node present,
419 * so the MTD_PARTITIONED_MASTER option allows that. The master
420 * will have device nodes etc only if this is set, so make the
421 * parent conditional on that option. Note, this is a way to
422 * distinguish between the master and the partition in sysfs.
David Brownell1f24b5a2009-03-26 00:42:41 -0700423 */
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700424 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) ?
425 &master->dev :
426 master->dev.parent;
David Brownell1f24b5a2009-03-26 00:42:41 -0700427
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200428 slave->mtd._read = part_read;
429 slave->mtd._write = part_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900430
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200431 if (master->_panic_write)
432 slave->mtd._panic_write = part_panic_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900433
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200434 if (master->_point && master->_unpoint) {
435 slave->mtd._point = part_point;
436 slave->mtd._unpoint = part_unpoint;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900437 }
438
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200439 if (master->_get_unmapped_area)
440 slave->mtd._get_unmapped_area = part_get_unmapped_area;
441 if (master->_read_oob)
442 slave->mtd._read_oob = part_read_oob;
443 if (master->_write_oob)
444 slave->mtd._write_oob = part_write_oob;
445 if (master->_read_user_prot_reg)
446 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
447 if (master->_read_fact_prot_reg)
448 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
449 if (master->_write_user_prot_reg)
450 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
451 if (master->_lock_user_prot_reg)
452 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
453 if (master->_get_user_prot_info)
454 slave->mtd._get_user_prot_info = part_get_user_prot_info;
455 if (master->_get_fact_prot_info)
456 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
457 if (master->_sync)
458 slave->mtd._sync = part_sync;
459 if (!partno && !master->dev.class && master->_suspend &&
460 master->_resume) {
461 slave->mtd._suspend = part_suspend;
462 slave->mtd._resume = part_resume;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900463 }
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200464 if (master->_writev)
465 slave->mtd._writev = part_writev;
466 if (master->_lock)
467 slave->mtd._lock = part_lock;
468 if (master->_unlock)
469 slave->mtd._unlock = part_unlock;
470 if (master->_is_locked)
471 slave->mtd._is_locked = part_is_locked;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300472 if (master->_block_isreserved)
473 slave->mtd._block_isreserved = part_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200474 if (master->_block_isbad)
475 slave->mtd._block_isbad = part_block_isbad;
476 if (master->_block_markbad)
477 slave->mtd._block_markbad = part_block_markbad;
Richard Weinberger5e149072016-09-21 11:43:56 +0200478
479 if (master->_get_device)
480 slave->mtd._get_device = part_get_device;
481 if (master->_put_device)
482 slave->mtd._put_device = part_put_device;
483
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200484 slave->mtd._erase = part_erase;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900485 slave->master = master;
486 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900487
488 if (slave->offset == MTDPART_OFS_APPEND)
489 slave->offset = cur_offset;
490 if (slave->offset == MTDPART_OFS_NXTBLK) {
491 slave->offset = cur_offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000492 if (mtd_mod_by_eb(cur_offset, master) != 0) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900493 /* Round up to next erasesize */
Adrian Hunter69423d92008-12-10 13:37:21 +0000494 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900495 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000496 "0x%012llx -> 0x%012llx\n", partno,
497 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900498 }
499 }
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400500 if (slave->offset == MTDPART_OFS_RETAIN) {
501 slave->offset = cur_offset;
502 if (master->size - slave->offset >= slave->mtd.size) {
503 slave->mtd.size = master->size - slave->offset
504 - slave->mtd.size;
505 } else {
506 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
507 part->name, master->size - slave->offset,
508 slave->mtd.size);
509 /* register to preserve ordering */
510 goto out_register;
511 }
512 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900513 if (slave->mtd.size == MTDPART_SIZ_FULL)
514 slave->mtd.size = master->size - slave->offset;
515
Adrian Hunter69423d92008-12-10 13:37:21 +0000516 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
517 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900518
519 /* let's do some sanity checks */
520 if (slave->offset >= master->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900521 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900522 slave->offset = 0;
523 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900524 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900525 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900526 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900527 }
528 if (slave->offset + slave->mtd.size > master->size) {
529 slave->mtd.size = master->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000530 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
531 part->name, master->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900532 }
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900533 if (master->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900534 /* Deal with variable erase size stuff */
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900535 int i, max = master->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000536 u64 end = slave->offset + slave->mtd.size;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900537 struct mtd_erase_region_info *regions = master->eraseregions;
538
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900539 /* Find the first erase regions which is part of this
540 * partition. */
541 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900542 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900543 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700544 if (i > 0)
545 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900546
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900547 /* Pick biggest erasesize */
548 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900549 if (slave->mtd.erasesize < regions[i].erasesize) {
550 slave->mtd.erasesize = regions[i].erasesize;
551 }
552 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900553 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900554 } else {
555 /* Single erase size */
556 slave->mtd.erasesize = master->erasesize;
557 }
558
559 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000560 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900561 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900562 /* FIXME: Let it be writable if it is on a boundary of
563 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900564 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900565 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 +0900566 part->name);
567 }
568 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000569 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900570 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900571 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900572 part->name);
573 }
574
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100575 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
Huang Shijiebdf69c42013-08-16 10:10:06 +0800576 slave->mtd.ecc_step_size = master->ecc_step_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700577 slave->mtd.ecc_strength = master->ecc_strength;
Mike Dunnd062d4e2012-04-25 12:06:08 -0700578 slave->mtd.bitflip_threshold = master->bitflip_threshold;
579
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200580 if (master->_block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000581 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900582
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900583 while (offs < slave->mtd.size) {
Ezequiel Garciafdf43a42014-03-21 08:57:44 -0300584 if (mtd_block_isreserved(master, offs + slave->offset))
585 slave->mtd.ecc_stats.bbtblocks++;
586 else if (mtd_block_isbad(master, offs + slave->offset))
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900587 slave->mtd.ecc_stats.badblocks++;
588 offs += slave->mtd.erasesize;
589 }
590 }
591
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900592out_register:
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900593 return slave;
594}
595
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700596static ssize_t mtd_partition_offset_show(struct device *dev,
597 struct device_attribute *attr, char *buf)
598{
599 struct mtd_info *mtd = dev_get_drvdata(dev);
Brian Norris25245342015-11-19 19:28:39 -0800600 struct mtd_part *part = mtd_to_part(mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700601 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
602}
603
604static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
605
606static const struct attribute *mtd_partition_attrs[] = {
607 &dev_attr_offset.attr,
608 NULL
609};
610
611static int mtd_add_partition_attrs(struct mtd_part *new)
612{
613 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
614 if (ret)
615 printk(KERN_WARNING
616 "mtd: failed to create partition attrs, err=%d\n", ret);
617 return ret;
618}
619
Sahitya Tummala88c3522c2017-06-05 15:59:36 +0530620int mtd_add_partition(struct mtd_info *master, char *name,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300621 long long offset, long long length)
622{
623 struct mtd_partition part;
Dan Ehrenberg3a434f62015-04-02 15:15:12 -0700624 struct mtd_part *new;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300625 int ret = 0;
626
627 /* the direct offset is expected */
628 if (offset == MTDPART_OFS_APPEND ||
629 offset == MTDPART_OFS_NXTBLK)
630 return -EINVAL;
631
632 if (length == MTDPART_SIZ_FULL)
633 length = master->size - offset;
634
635 if (length <= 0)
636 return -EINVAL;
637
Brian Norris93867232015-11-11 16:47:52 -0800638 memset(&part, 0, sizeof(part));
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300639 part.name = name;
640 part.size = length;
641 part.offset = offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300642
643 new = allocate_partition(master, &part, -1, offset);
644 if (IS_ERR(new))
645 return PTR_ERR(new);
646
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300647 mutex_lock(&mtd_partitions_mutex);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300648 list_add(&new->list, &mtd_partitions);
649 mutex_unlock(&mtd_partitions_mutex);
650
Boris Brezillond63d0812019-01-02 15:36:54 +0100651 ret = add_mtd_device(&new->mtd);
652 if (ret)
653 goto err_remove_part;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300654
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700655 mtd_add_partition_attrs(new);
656
Boris Brezillond63d0812019-01-02 15:36:54 +0100657 return 0;
658
659err_remove_part:
660 mutex_lock(&mtd_partitions_mutex);
661 list_del(&new->list);
662 mutex_unlock(&mtd_partitions_mutex);
663
664 free_partition(new);
Boris Brezillond63d0812019-01-02 15:36:54 +0100665
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300666 return ret;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300667}
668EXPORT_SYMBOL_GPL(mtd_add_partition);
669
670int mtd_del_partition(struct mtd_info *master, int partno)
671{
672 struct mtd_part *slave, *next;
673 int ret = -EINVAL;
674
675 mutex_lock(&mtd_partitions_mutex);
676 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
677 if ((slave->master == master) &&
678 (slave->mtd.index == partno)) {
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700679 sysfs_remove_files(&slave->mtd.dev.kobj,
680 mtd_partition_attrs);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300681 ret = del_mtd_device(&slave->mtd);
682 if (ret < 0)
683 break;
684
685 list_del(&slave->list);
686 free_partition(slave);
687 break;
688 }
689 mutex_unlock(&mtd_partitions_mutex);
690
691 return ret;
692}
693EXPORT_SYMBOL_GPL(mtd_del_partition);
694
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695/*
696 * This function, given a master MTD object and a partition table, creates
697 * and registers slave MTD objects which are bound to the master according to
698 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700699 *
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700700 * For historical reasons, this function's caller only registers the master
701 * if the MTD_PARTITIONED_MASTER config option is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 */
703
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000704int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 const struct mtd_partition *parts,
706 int nbparts)
707{
708 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000709 uint64_t cur_offset = 0;
Boris Brezillond63d0812019-01-02 15:36:54 +0100710 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900712 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713
714 for (i = 0; i < nbparts; i++) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300715 slave = allocate_partition(master, parts + i, i, cur_offset);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200716 if (IS_ERR(slave)) {
Boris Brezillond63d0812019-01-02 15:36:54 +0100717 ret = PTR_ERR(slave);
718 goto err_del_partitions;
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200719 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300720
721 mutex_lock(&mtd_partitions_mutex);
722 list_add(&slave->list, &mtd_partitions);
723 mutex_unlock(&mtd_partitions_mutex);
724
Boris Brezillond63d0812019-01-02 15:36:54 +0100725 ret = add_mtd_device(&slave->mtd);
726 if (ret) {
727 mutex_lock(&mtd_partitions_mutex);
728 list_del(&slave->list);
729 mutex_unlock(&mtd_partitions_mutex);
730
731 free_partition(slave);
732 goto err_del_partitions;
733 }
734
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700735 mtd_add_partition_attrs(slave);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 }
739
740 return 0;
Boris Brezillond63d0812019-01-02 15:36:54 +0100741
742err_del_partitions:
743 del_mtd_partitions(master);
744
745 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747
748static DEFINE_SPINLOCK(part_parser_lock);
749static LIST_HEAD(part_parsers);
750
Brian Norris5531ae42015-12-04 15:25:15 -0800751static struct mtd_part_parser *mtd_part_parser_get(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
Chris Malley71a928c2008-05-19 20:11:50 +0100753 struct mtd_part_parser *p, *ret = NULL;
754
Linus Torvalds1da177e2005-04-16 15:20:36 -0700755 spin_lock(&part_parser_lock);
756
Chris Malley71a928c2008-05-19 20:11:50 +0100757 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
759 ret = p;
760 break;
761 }
Chris Malley71a928c2008-05-19 20:11:50 +0100762
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 spin_unlock(&part_parser_lock);
764
765 return ret;
766}
767
Brian Norris5531ae42015-12-04 15:25:15 -0800768static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
769{
770 module_put(p->owner);
771}
Dmitry Eremin-Solenikov953b3bd2011-06-23 15:26:14 +0400772
Brian Norrisadc83bf2015-12-09 10:24:03 -0800773/*
774 * Many partition parsers just expected the core to kfree() all their data in
775 * one chunk. Do that by default.
776 */
777static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
778 int nr_parts)
779{
780 kfree(pparts);
781}
782
Brian Norrisb9eab012015-11-11 19:13:29 -0800783int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784{
Brian Norrisb9eab012015-11-11 19:13:29 -0800785 p->owner = owner;
786
Brian Norrisadc83bf2015-12-09 10:24:03 -0800787 if (!p->cleanup)
788 p->cleanup = &mtd_part_parser_cleanup_default;
789
Linus Torvalds1da177e2005-04-16 15:20:36 -0700790 spin_lock(&part_parser_lock);
791 list_add(&p->list, &part_parsers);
792 spin_unlock(&part_parser_lock);
Brian Norrisb9eab012015-11-11 19:13:29 -0800793
794 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795}
Brian Norrisb9eab012015-11-11 19:13:29 -0800796EXPORT_SYMBOL_GPL(__register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
Axel Lincf3b2b12013-12-01 18:59:15 +0800798void deregister_mtd_parser(struct mtd_part_parser *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700799{
800 spin_lock(&part_parser_lock);
801 list_del(&p->list);
802 spin_unlock(&part_parser_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700803}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900804EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300806/*
807 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
808 * are changing this array!
809 */
Artem Bityutskiyccef4dc2013-03-12 10:51:35 +0200810static const char * const default_mtd_part_types[] = {
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400811 "cmdlinepart",
812 "ofpart",
813 NULL
814};
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400815
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300816/**
817 * parse_mtd_partitions - parse MTD partitions
818 * @master: the master partition (describes whole MTD device)
819 * @types: names of partition parsers to try or %NULL
Brian Norris07fd2f82015-12-04 15:25:17 -0800820 * @pparts: info about partitions found is returned here
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400821 * @data: MTD partition parser-specific data
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300822 *
823 * This function tries to find partition on MTD device @master. It uses MTD
824 * partition parsers, specified in @types. However, if @types is %NULL, then
825 * the default list of parsers is used. The default list contains only the
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400826 * "cmdlinepart" and "ofpart" parsers ATM.
Huang Shijiec51803d2012-08-18 13:07:41 -0400827 * Note: If there are more then one parser in @types, the kernel only takes the
828 * partitions parsed out by the first parser.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300829 *
830 * This function may return:
831 * o a negative error code in case of failure
Brian Norris07fd2f82015-12-04 15:25:17 -0800832 * o zero otherwise, and @pparts will describe the partitions, number of
Brian Norrisadc83bf2015-12-09 10:24:03 -0800833 * partitions, and the parser which parsed them. Caller must release
834 * resources with mtd_part_parser_cleanup() when finished with the returned
835 * data.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300836 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200837int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
Brian Norris07fd2f82015-12-04 15:25:17 -0800838 struct mtd_partitions *pparts,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400839 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840{
841 struct mtd_part_parser *parser;
Brian Norris5a2415b2015-10-11 13:03:47 -0700842 int ret, err = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000843
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400844 if (!types)
845 types = default_mtd_part_types;
846
Brian Norris5a2415b2015-10-11 13:03:47 -0700847 for ( ; *types; types++) {
Michal Suchanek8e2c9922015-08-18 15:34:07 +0000848 pr_debug("%s: parsing partitions %s\n", master->name, *types);
Brian Norris5531ae42015-12-04 15:25:15 -0800849 parser = mtd_part_parser_get(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 if (!parser && !request_module("%s", *types))
Brian Norris5531ae42015-12-04 15:25:15 -0800851 parser = mtd_part_parser_get(*types);
Michal Suchanek8e2c9922015-08-18 15:34:07 +0000852 pr_debug("%s: got parser %s\n", master->name,
853 parser ? parser->name : NULL);
Artem Bityutskiy7c802fb2011-05-17 11:13:17 +0300854 if (!parser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 continue;
Brian Norris07fd2f82015-12-04 15:25:17 -0800856 ret = (*parser->parse_fn)(master, &pparts->parts, data);
Michal Suchanek8e2c9922015-08-18 15:34:07 +0000857 pr_debug("%s: parser %s: %i\n",
858 master->name, parser->name, ret);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000860 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700861 ret, parser->name, master->name);
Brian Norris07fd2f82015-12-04 15:25:17 -0800862 pparts->nr_parts = ret;
863 pparts->parser = parser;
864 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865 }
Brian Norrisadc83bf2015-12-09 10:24:03 -0800866 mtd_part_parser_put(parser);
Brian Norris5a2415b2015-10-11 13:03:47 -0700867 /*
868 * Stash the first error we see; only report it if no parser
869 * succeeds
870 */
871 if (ret < 0 && !err)
872 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 }
Brian Norris5a2415b2015-10-11 13:03:47 -0700874 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700875}
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300876
Brian Norrisadc83bf2015-12-09 10:24:03 -0800877void mtd_part_parser_cleanup(struct mtd_partitions *parts)
878{
879 const struct mtd_part_parser *parser;
880
881 if (!parts)
882 return;
883
884 parser = parts->parser;
885 if (parser) {
886 if (parser->cleanup)
887 parser->cleanup(parts->parts, parts->nr_parts);
888
889 mtd_part_parser_put(parser);
890 }
891}
892
Richard Genoud5dee4672012-07-10 18:23:39 +0200893int mtd_is_partition(const struct mtd_info *mtd)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300894{
895 struct mtd_part *part;
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200896 int ispart = 0;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300897
898 mutex_lock(&mtd_partitions_mutex);
899 list_for_each_entry(part, &mtd_partitions, list)
900 if (&part->mtd == mtd) {
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200901 ispart = 1;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300902 break;
903 }
904 mutex_unlock(&mtd_partitions_mutex);
905
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200906 return ispart;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300907}
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200908EXPORT_SYMBOL_GPL(mtd_is_partition);
Richard Genoud62082e52012-07-10 18:23:40 +0200909
910/* Returns the size of the entire flash chip */
911uint64_t mtd_get_device_size(const struct mtd_info *mtd)
912{
913 if (!mtd_is_partition(mtd))
914 return mtd->size;
915
Brian Norris25245342015-11-19 19:28:39 -0800916 return mtd_to_part(mtd)->master->size;
Richard Genoud62082e52012-07-10 18:23:40 +0200917}
918EXPORT_SYMBOL_GPL(mtd_get_device_size);