blob: 4a660ae27bb2e2f2c7abd3c08b6dfc143e354876 [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
Brian Norris0612b9d2011-08-30 18:45:40 -0700129 if (ops->mode == MTD_OPS_AUTO_OOB)
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200130 len = mtd->oobavail;
131 else
132 len = mtd->oobsize;
133 pages = mtd_div_by_ws(mtd->size, mtd);
134 pages -= mtd_div_by_ws(from, mtd);
135 if (ops->ooboffs + ops->ooblen > pages * len)
136 return -EINVAL;
137 }
138
Mike Dunn994c8402012-03-03 13:13:06 -0800139 res = part->master->_read_oob(part->master, from + part->offset, ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200140 if (unlikely(res)) {
Brian Norrisd57f40542011-09-20 18:34:25 -0700141 if (mtd_is_bitflip(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200142 mtd->ecc_stats.corrected++;
Brian Norrisd57f40542011-09-20 18:34:25 -0700143 if (mtd_is_eccerr(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200144 mtd->ecc_stats.failed++;
145 }
146 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900149static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
150 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151{
Brian Norris25245342015-11-19 19:28:39 -0800152 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800153 return part->master->_read_user_prot_reg(part->master, from, len,
154 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
Christian Riesch4b78fc42014-01-28 09:29:44 +0100157static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
158 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000159{
Brian Norris25245342015-11-19 19:28:39 -0800160 struct mtd_part *part = mtd_to_part(mtd);
Christian Riesch4b78fc42014-01-28 09:29:44 +0100161 return part->master->_get_user_prot_info(part->master, len, retlen,
162 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000163}
164
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900165static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
166 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
Brian Norris25245342015-11-19 19:28:39 -0800168 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800169 return part->master->_read_fact_prot_reg(part->master, from, len,
170 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Christian Riesch4b78fc42014-01-28 09:29:44 +0100173static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
174 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000175{
Brian Norris25245342015-11-19 19:28:39 -0800176 struct mtd_part *part = mtd_to_part(mtd);
Christian Riesch4b78fc42014-01-28 09:29:44 +0100177 return part->master->_get_fact_prot_info(part->master, len, retlen,
178 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000179}
180
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900181static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
182 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Brian Norris25245342015-11-19 19:28:39 -0800184 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800185 return part->master->_write(part->master, to + part->offset, len,
186 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187}
188
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900189static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
190 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000191{
Brian Norris25245342015-11-19 19:28:39 -0800192 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800193 return part->master->_panic_write(part->master, to + part->offset, len,
194 retlen, buf);
Richard Purdie388bbb02008-02-06 10:17:15 +0000195}
196
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200197static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900198 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199{
Brian Norris25245342015-11-19 19:28:39 -0800200 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200203 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300204 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200205 return -EINVAL;
Mike Dunn994c8402012-03-03 13:13:06 -0800206 return part->master->_write_oob(part->master, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900209static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
210 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
Brian Norris25245342015-11-19 19:28:39 -0800212 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800213 return part->master->_write_user_prot_reg(part->master, from, len,
214 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215}
216
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900217static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
218 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000219{
Brian Norris25245342015-11-19 19:28:39 -0800220 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800221 return part->master->_lock_user_prot_reg(part->master, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000222}
223
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900224static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
225 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226{
Brian Norris25245342015-11-19 19:28:39 -0800227 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800228 return part->master->_writev(part->master, vecs, count,
229 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230}
231
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900232static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233{
Brian Norris25245342015-11-19 19:28:39 -0800234 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 int ret;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200236
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 instr->addr += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800238 ret = part->master->_erase(part->master, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200239 if (ret) {
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300240 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Adrian Hunter74641d72007-03-08 12:20:12 +0200241 instr->fail_addr -= part->offset;
242 instr->addr -= part->offset;
243 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 return ret;
245}
246
247void mtd_erase_callback(struct erase_info *instr)
248{
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200249 if (instr->mtd->_erase == part_erase) {
Brian Norris25245342015-11-19 19:28:39 -0800250 struct mtd_part *part = mtd_to_part(instr->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300252 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 instr->fail_addr -= part->offset;
254 instr->addr -= part->offset;
255 }
256 if (instr->callback)
257 instr->callback(instr);
258}
259EXPORT_SYMBOL_GPL(mtd_erase_callback);
260
Adrian Hunter69423d92008-12-10 13:37:21 +0000261static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
Brian Norris25245342015-11-19 19:28:39 -0800263 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800264 return part->master->_lock(part->master, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Adrian Hunter69423d92008-12-10 13:37:21 +0000267static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268{
Brian Norris25245342015-11-19 19:28:39 -0800269 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800270 return part->master->_unlock(part->master, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271}
272
Richard Cochran99384242010-06-14 18:10:33 +0200273static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
274{
Brian Norris25245342015-11-19 19:28:39 -0800275 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800276 return part->master->_is_locked(part->master, ofs + part->offset, len);
Richard Cochran99384242010-06-14 18:10:33 +0200277}
278
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279static void part_sync(struct mtd_info *mtd)
280{
Brian Norris25245342015-11-19 19:28:39 -0800281 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800282 part->master->_sync(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285static int part_suspend(struct mtd_info *mtd)
286{
Brian Norris25245342015-11-19 19:28:39 -0800287 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800288 return part->master->_suspend(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
291static void part_resume(struct mtd_info *mtd)
292{
Brian Norris25245342015-11-19 19:28:39 -0800293 struct mtd_part *part = mtd_to_part(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800294 part->master->_resume(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295}
296
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300297static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
298{
Brian Norris25245342015-11-19 19:28:39 -0800299 struct mtd_part *part = mtd_to_part(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300300 ofs += part->offset;
301 return part->master->_block_isreserved(part->master, ofs);
302}
303
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900304static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305{
Brian Norris25245342015-11-19 19:28:39 -0800306 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 ofs += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800308 return part->master->_block_isbad(part->master, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309}
310
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900311static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312{
Brian Norris25245342015-11-19 19:28:39 -0800313 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200314 int res;
315
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 ofs += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800317 res = part->master->_block_markbad(part->master, ofs);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200318 if (!res)
319 mtd->ecc_stats.badblocks++;
320 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321}
322
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300323static inline void free_partition(struct mtd_part *p)
324{
325 kfree(p->mtd.name);
326 kfree(p);
327}
328
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000329/*
330 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 * attached to the given master MTD object.
332 */
333
334int del_mtd_partitions(struct mtd_info *master)
335{
Chris Malley71a928c2008-05-19 20:11:50 +0100336 struct mtd_part *slave, *next;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300337 int ret, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300339 mutex_lock(&mtd_partitions_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100340 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341 if (slave->master == master) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300342 ret = del_mtd_device(&slave->mtd);
343 if (ret < 0) {
344 err = ret;
345 continue;
346 }
Chris Malley71a928c2008-05-19 20:11:50 +0100347 list_del(&slave->list);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300348 free_partition(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300350 mutex_unlock(&mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300352 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353}
354
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300355static struct mtd_part *allocate_partition(struct mtd_info *master,
356 const struct mtd_partition *part, int partno,
357 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900358{
359 struct mtd_part *slave;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300360 char *name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900361
362 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900363 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300364 name = kstrdup(part->name, GFP_KERNEL);
365 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900366 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300367 master->name);
368 kfree(name);
369 kfree(slave);
370 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900371 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900372
373 /* set up the MTD object for this partition */
374 slave->mtd.type = master->type;
375 slave->mtd.flags = master->flags & ~part->mask_flags;
376 slave->mtd.size = part->size;
377 slave->mtd.writesize = master->writesize;
Anatolij Gustschin7fa33ac2010-12-16 23:42:18 +0100378 slave->mtd.writebufsize = master->writebufsize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900379 slave->mtd.oobsize = master->oobsize;
380 slave->mtd.oobavail = master->oobavail;
381 slave->mtd.subpage_sft = master->subpage_sft;
382
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300383 slave->mtd.name = name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900384 slave->mtd.owner = master->owner;
385
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700386 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
387 * concern for showing the same data in multiple partitions.
388 * However, it is very useful to have the master node present,
389 * so the MTD_PARTITIONED_MASTER option allows that. The master
390 * will have device nodes etc only if this is set, so make the
391 * parent conditional on that option. Note, this is a way to
392 * distinguish between the master and the partition in sysfs.
David Brownell1f24b5a2009-03-26 00:42:41 -0700393 */
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700394 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) ?
395 &master->dev :
396 master->dev.parent;
David Brownell1f24b5a2009-03-26 00:42:41 -0700397
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200398 slave->mtd._read = part_read;
399 slave->mtd._write = part_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900400
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200401 if (master->_panic_write)
402 slave->mtd._panic_write = part_panic_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900403
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200404 if (master->_point && master->_unpoint) {
405 slave->mtd._point = part_point;
406 slave->mtd._unpoint = part_unpoint;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900407 }
408
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200409 if (master->_get_unmapped_area)
410 slave->mtd._get_unmapped_area = part_get_unmapped_area;
411 if (master->_read_oob)
412 slave->mtd._read_oob = part_read_oob;
413 if (master->_write_oob)
414 slave->mtd._write_oob = part_write_oob;
415 if (master->_read_user_prot_reg)
416 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
417 if (master->_read_fact_prot_reg)
418 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
419 if (master->_write_user_prot_reg)
420 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
421 if (master->_lock_user_prot_reg)
422 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
423 if (master->_get_user_prot_info)
424 slave->mtd._get_user_prot_info = part_get_user_prot_info;
425 if (master->_get_fact_prot_info)
426 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
427 if (master->_sync)
428 slave->mtd._sync = part_sync;
429 if (!partno && !master->dev.class && master->_suspend &&
430 master->_resume) {
431 slave->mtd._suspend = part_suspend;
432 slave->mtd._resume = part_resume;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900433 }
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200434 if (master->_writev)
435 slave->mtd._writev = part_writev;
436 if (master->_lock)
437 slave->mtd._lock = part_lock;
438 if (master->_unlock)
439 slave->mtd._unlock = part_unlock;
440 if (master->_is_locked)
441 slave->mtd._is_locked = part_is_locked;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300442 if (master->_block_isreserved)
443 slave->mtd._block_isreserved = part_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200444 if (master->_block_isbad)
445 slave->mtd._block_isbad = part_block_isbad;
446 if (master->_block_markbad)
447 slave->mtd._block_markbad = part_block_markbad;
448 slave->mtd._erase = part_erase;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900449 slave->master = master;
450 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900451
452 if (slave->offset == MTDPART_OFS_APPEND)
453 slave->offset = cur_offset;
454 if (slave->offset == MTDPART_OFS_NXTBLK) {
455 slave->offset = cur_offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000456 if (mtd_mod_by_eb(cur_offset, master) != 0) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900457 /* Round up to next erasesize */
Adrian Hunter69423d92008-12-10 13:37:21 +0000458 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900459 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000460 "0x%012llx -> 0x%012llx\n", partno,
461 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900462 }
463 }
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400464 if (slave->offset == MTDPART_OFS_RETAIN) {
465 slave->offset = cur_offset;
466 if (master->size - slave->offset >= slave->mtd.size) {
467 slave->mtd.size = master->size - slave->offset
468 - slave->mtd.size;
469 } else {
470 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
471 part->name, master->size - slave->offset,
472 slave->mtd.size);
473 /* register to preserve ordering */
474 goto out_register;
475 }
476 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900477 if (slave->mtd.size == MTDPART_SIZ_FULL)
478 slave->mtd.size = master->size - slave->offset;
479
Adrian Hunter69423d92008-12-10 13:37:21 +0000480 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
481 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900482
483 /* let's do some sanity checks */
484 if (slave->offset >= master->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900485 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900486 slave->offset = 0;
487 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900488 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900489 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900490 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900491 }
492 if (slave->offset + slave->mtd.size > master->size) {
493 slave->mtd.size = master->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000494 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
495 part->name, master->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900496 }
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900497 if (master->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900498 /* Deal with variable erase size stuff */
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900499 int i, max = master->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000500 u64 end = slave->offset + slave->mtd.size;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900501 struct mtd_erase_region_info *regions = master->eraseregions;
502
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900503 /* Find the first erase regions which is part of this
504 * partition. */
505 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900506 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900507 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700508 if (i > 0)
509 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900510
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900511 /* Pick biggest erasesize */
512 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900513 if (slave->mtd.erasesize < regions[i].erasesize) {
514 slave->mtd.erasesize = regions[i].erasesize;
515 }
516 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900517 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900518 } else {
519 /* Single erase size */
520 slave->mtd.erasesize = master->erasesize;
521 }
522
523 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000524 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900525 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900526 /* FIXME: Let it be writable if it is on a boundary of
527 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900528 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900529 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 +0900530 part->name);
531 }
532 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000533 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900534 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900535 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900536 part->name);
537 }
538
539 slave->mtd.ecclayout = master->ecclayout;
Huang Shijiebdf69c42013-08-16 10:10:06 +0800540 slave->mtd.ecc_step_size = master->ecc_step_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700541 slave->mtd.ecc_strength = master->ecc_strength;
Mike Dunnd062d4e2012-04-25 12:06:08 -0700542 slave->mtd.bitflip_threshold = master->bitflip_threshold;
543
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200544 if (master->_block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000545 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900546
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900547 while (offs < slave->mtd.size) {
Ezequiel Garciafdf43a42014-03-21 08:57:44 -0300548 if (mtd_block_isreserved(master, offs + slave->offset))
549 slave->mtd.ecc_stats.bbtblocks++;
550 else if (mtd_block_isbad(master, offs + slave->offset))
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900551 slave->mtd.ecc_stats.badblocks++;
552 offs += slave->mtd.erasesize;
553 }
554 }
555
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900556out_register:
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900557 return slave;
558}
559
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700560static ssize_t mtd_partition_offset_show(struct device *dev,
561 struct device_attribute *attr, char *buf)
562{
563 struct mtd_info *mtd = dev_get_drvdata(dev);
Brian Norris25245342015-11-19 19:28:39 -0800564 struct mtd_part *part = mtd_to_part(mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700565 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
566}
567
568static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
569
570static const struct attribute *mtd_partition_attrs[] = {
571 &dev_attr_offset.attr,
572 NULL
573};
574
575static int mtd_add_partition_attrs(struct mtd_part *new)
576{
577 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
578 if (ret)
579 printk(KERN_WARNING
580 "mtd: failed to create partition attrs, err=%d\n", ret);
581 return ret;
582}
583
Geert Uytterhoeven26a6d242013-11-12 20:11:26 +0100584int mtd_add_partition(struct mtd_info *master, const char *name,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300585 long long offset, long long length)
586{
587 struct mtd_partition part;
Dan Ehrenberg3a434f62015-04-02 15:15:12 -0700588 struct mtd_part *new;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300589 int ret = 0;
590
591 /* the direct offset is expected */
592 if (offset == MTDPART_OFS_APPEND ||
593 offset == MTDPART_OFS_NXTBLK)
594 return -EINVAL;
595
596 if (length == MTDPART_SIZ_FULL)
597 length = master->size - offset;
598
599 if (length <= 0)
600 return -EINVAL;
601
Brian Norris93867232015-11-11 16:47:52 -0800602 memset(&part, 0, sizeof(part));
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300603 part.name = name;
604 part.size = length;
605 part.offset = offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300606
607 new = allocate_partition(master, &part, -1, offset);
608 if (IS_ERR(new))
609 return PTR_ERR(new);
610
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300611 mutex_lock(&mtd_partitions_mutex);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300612 list_add(&new->list, &mtd_partitions);
613 mutex_unlock(&mtd_partitions_mutex);
614
615 add_mtd_device(&new->mtd);
616
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700617 mtd_add_partition_attrs(new);
618
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300619 return ret;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300620}
621EXPORT_SYMBOL_GPL(mtd_add_partition);
622
623int mtd_del_partition(struct mtd_info *master, int partno)
624{
625 struct mtd_part *slave, *next;
626 int ret = -EINVAL;
627
628 mutex_lock(&mtd_partitions_mutex);
629 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
630 if ((slave->master == master) &&
631 (slave->mtd.index == partno)) {
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700632 sysfs_remove_files(&slave->mtd.dev.kobj,
633 mtd_partition_attrs);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300634 ret = del_mtd_device(&slave->mtd);
635 if (ret < 0)
636 break;
637
638 list_del(&slave->list);
639 free_partition(slave);
640 break;
641 }
642 mutex_unlock(&mtd_partitions_mutex);
643
644 return ret;
645}
646EXPORT_SYMBOL_GPL(mtd_del_partition);
647
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648/*
649 * This function, given a master MTD object and a partition table, creates
650 * and registers slave MTD objects which are bound to the master according to
651 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700652 *
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700653 * For historical reasons, this function's caller only registers the master
654 * if the MTD_PARTITIONED_MASTER config option is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 */
656
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000657int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 const struct mtd_partition *parts,
659 int nbparts)
660{
661 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000662 uint64_t cur_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 int i;
664
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900665 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666
667 for (i = 0; i < nbparts; i++) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300668 slave = allocate_partition(master, parts + i, i, cur_offset);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200669 if (IS_ERR(slave)) {
670 del_mtd_partitions(master);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300671 return PTR_ERR(slave);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200672 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300673
674 mutex_lock(&mtd_partitions_mutex);
675 list_add(&slave->list, &mtd_partitions);
676 mutex_unlock(&mtd_partitions_mutex);
677
678 add_mtd_device(&slave->mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700679 mtd_add_partition_attrs(slave);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
683
684 return 0;
685}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687static DEFINE_SPINLOCK(part_parser_lock);
688static LIST_HEAD(part_parsers);
689
Brian Norris5531ae42015-12-04 15:25:15 -0800690static struct mtd_part_parser *mtd_part_parser_get(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691{
Chris Malley71a928c2008-05-19 20:11:50 +0100692 struct mtd_part_parser *p, *ret = NULL;
693
Linus Torvalds1da177e2005-04-16 15:20:36 -0700694 spin_lock(&part_parser_lock);
695
Chris Malley71a928c2008-05-19 20:11:50 +0100696 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
698 ret = p;
699 break;
700 }
Chris Malley71a928c2008-05-19 20:11:50 +0100701
Linus Torvalds1da177e2005-04-16 15:20:36 -0700702 spin_unlock(&part_parser_lock);
703
704 return ret;
705}
706
Brian Norris5531ae42015-12-04 15:25:15 -0800707static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
708{
709 module_put(p->owner);
710}
Dmitry Eremin-Solenikov953b3bd2011-06-23 15:26:14 +0400711
Brian Norrisb9eab012015-11-11 19:13:29 -0800712int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700713{
Brian Norrisb9eab012015-11-11 19:13:29 -0800714 p->owner = owner;
715
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 spin_lock(&part_parser_lock);
717 list_add(&p->list, &part_parsers);
718 spin_unlock(&part_parser_lock);
Brian Norrisb9eab012015-11-11 19:13:29 -0800719
720 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721}
Brian Norrisb9eab012015-11-11 19:13:29 -0800722EXPORT_SYMBOL_GPL(__register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Axel Lincf3b2b12013-12-01 18:59:15 +0800724void deregister_mtd_parser(struct mtd_part_parser *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725{
726 spin_lock(&part_parser_lock);
727 list_del(&p->list);
728 spin_unlock(&part_parser_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900730EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300732/*
733 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
734 * are changing this array!
735 */
Artem Bityutskiyccef4dc2013-03-12 10:51:35 +0200736static const char * const default_mtd_part_types[] = {
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400737 "cmdlinepart",
738 "ofpart",
739 NULL
740};
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400741
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300742/**
743 * parse_mtd_partitions - parse MTD partitions
744 * @master: the master partition (describes whole MTD device)
745 * @types: names of partition parsers to try or %NULL
746 * @pparts: array of partitions found is returned here
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400747 * @data: MTD partition parser-specific data
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300748 *
749 * This function tries to find partition on MTD device @master. It uses MTD
750 * partition parsers, specified in @types. However, if @types is %NULL, then
751 * the default list of parsers is used. The default list contains only the
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400752 * "cmdlinepart" and "ofpart" parsers ATM.
Huang Shijiec51803d2012-08-18 13:07:41 -0400753 * Note: If there are more then one parser in @types, the kernel only takes the
754 * partitions parsed out by the first parser.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300755 *
756 * This function may return:
757 * o a negative error code in case of failure
758 * o zero if no partitions were found
759 * o a positive number of found partitions, in which case on exit @pparts will
760 * point to an array containing this number of &struct mtd_info objects.
761 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200762int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400763 struct mtd_partition **pparts,
764 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765{
766 struct mtd_part_parser *parser;
Brian Norris5a2415b2015-10-11 13:03:47 -0700767 int ret, err = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000768
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400769 if (!types)
770 types = default_mtd_part_types;
771
Brian Norris5a2415b2015-10-11 13:03:47 -0700772 for ( ; *types; types++) {
Michal Suchanek8e2c9922015-08-18 15:34:07 +0000773 pr_debug("%s: parsing partitions %s\n", master->name, *types);
Brian Norris5531ae42015-12-04 15:25:15 -0800774 parser = mtd_part_parser_get(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 if (!parser && !request_module("%s", *types))
Brian Norris5531ae42015-12-04 15:25:15 -0800776 parser = mtd_part_parser_get(*types);
Michal Suchanek8e2c9922015-08-18 15:34:07 +0000777 pr_debug("%s: got parser %s\n", master->name,
778 parser ? parser->name : NULL);
Artem Bityutskiy7c802fb2011-05-17 11:13:17 +0300779 if (!parser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 continue;
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400781 ret = (*parser->parse_fn)(master, pparts, data);
Michal Suchanek8e2c9922015-08-18 15:34:07 +0000782 pr_debug("%s: parser %s: %i\n",
783 master->name, parser->name, ret);
Brian Norris5531ae42015-12-04 15:25:15 -0800784 mtd_part_parser_put(parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000786 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 ret, parser->name, master->name);
Brian Norris5a2415b2015-10-11 13:03:47 -0700788 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 }
Brian Norris5a2415b2015-10-11 13:03:47 -0700790 /*
791 * Stash the first error we see; only report it if no parser
792 * succeeds
793 */
794 if (ret < 0 && !err)
795 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 }
Brian Norris5a2415b2015-10-11 13:03:47 -0700797 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798}
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300799
Richard Genoud5dee4672012-07-10 18:23:39 +0200800int mtd_is_partition(const struct mtd_info *mtd)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300801{
802 struct mtd_part *part;
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200803 int ispart = 0;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300804
805 mutex_lock(&mtd_partitions_mutex);
806 list_for_each_entry(part, &mtd_partitions, list)
807 if (&part->mtd == mtd) {
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200808 ispart = 1;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300809 break;
810 }
811 mutex_unlock(&mtd_partitions_mutex);
812
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200813 return ispart;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300814}
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200815EXPORT_SYMBOL_GPL(mtd_is_partition);
Richard Genoud62082e52012-07-10 18:23:40 +0200816
817/* Returns the size of the entire flash chip */
818uint64_t mtd_get_device_size(const struct mtd_info *mtd)
819{
820 if (!mtd_is_partition(mtd))
821 return mtd->size;
822
Brian Norris25245342015-11-19 19:28:39 -0800823 return mtd_to_part(mtd)->master->size;
Richard Genoud62082e52012-07-10 18:23:40 +0200824}
825EXPORT_SYMBOL_GPL(mtd_get_device_size);