blob: 9651c06de0a9298f4db58265524ecaebe2c2b9b4 [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
50 * the pointer to that structure with this macro.
51 */
52#define PART(x) ((struct mtd_part *)(x))
53
Thomas Gleixner97894cd2005-11-07 11:15:26 +000054
55/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * MTD methods which simply translate the effective address and pass through
57 * to the _real_ device.
58 */
59
Atsushi Nemotob33a2882008-07-19 01:00:33 +090060static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
61 size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 struct mtd_part *part = PART(mtd);
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020064 struct mtd_ecc_stats stats;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020065 int res;
66
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020067 stats = part->master->ecc_stats;
Mike Dunn994c8402012-03-03 13:13:06 -080068 res = part->master->_read(part->master, from + part->offset, len,
69 retlen, buf);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020070 if (unlikely(res)) {
Brian Norrisd57f40542011-09-20 18:34:25 -070071 if (mtd_is_bitflip(res))
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020072 mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
Brian Norrisd57f40542011-09-20 18:34:25 -070073 if (mtd_is_eccerr(res))
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020074 mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020075 }
76 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Atsushi Nemotob33a2882008-07-19 01:00:33 +090079static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
80 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 struct mtd_part *part = PART(mtd);
Artem Bityutskiy5def4892012-02-03 16:23:52 +020083
Mike Dunn994c8402012-03-03 13:13:06 -080084 return part->master->_point(part->master, from + part->offset, len,
85 retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
Thomas Gleixner9223a452006-05-23 17:21:03 +020087
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +020088static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 struct mtd_part *part = PART(mtd);
91
Mike Dunn994c8402012-03-03 13:13:06 -080092 return part->master->_unpoint(part->master, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
David Howells402d3262009-02-12 10:40:00 +000095static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
96 unsigned long len,
97 unsigned long offset,
98 unsigned long flags)
99{
100 struct mtd_part *part = PART(mtd);
101
102 offset += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800103 return part->master->_get_unmapped_area(part->master, len, offset,
104 flags);
David Howells402d3262009-02-12 10:40:00 +0000105}
106
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200107static int part_read_oob(struct mtd_info *mtd, loff_t from,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900108 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200111 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (from >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200114 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300115 if (ops->datbuf && from + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200116 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200117
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200118 /*
119 * If OOB is also requested, make sure that we do not read past the end
120 * of this partition.
121 */
122 if (ops->oobbuf) {
123 size_t len, pages;
124
Brian Norris0612b9d2011-08-30 18:45:40 -0700125 if (ops->mode == MTD_OPS_AUTO_OOB)
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200126 len = mtd->oobavail;
127 else
128 len = mtd->oobsize;
129 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{
148 struct mtd_part *part = 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
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900153static int part_get_user_prot_info(struct mtd_info *mtd,
154 struct otp_info *buf, size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000155{
156 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800157 return part->master->_get_user_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000158}
159
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900160static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
161 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800164 return part->master->_read_fact_prot_reg(part->master, from, len,
165 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166}
167
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900168static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
169 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000170{
171 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800172 return part->master->_get_fact_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000173}
174
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900175static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
176 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177{
178 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800179 return part->master->_write(part->master, to + part->offset, len,
180 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181}
182
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900183static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
184 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000185{
186 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800187 return part->master->_panic_write(part->master, to + part->offset, len,
188 retlen, buf);
Richard Purdie388bbb02008-02-06 10:17:15 +0000189}
190
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200191static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900192 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193{
194 struct mtd_part *part = PART(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200195
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200197 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300198 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200199 return -EINVAL;
Mike Dunn994c8402012-03-03 13:13:06 -0800200 return part->master->_write_oob(part->master, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201}
202
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900203static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
204 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
206 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800207 return part->master->_write_user_prot_reg(part->master, from, len,
208 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900211static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
212 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000213{
214 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800215 return part->master->_lock_user_prot_reg(part->master, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000216}
217
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900218static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
219 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800222 return part->master->_writev(part->master, vecs, count,
223 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900226static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
228 struct mtd_part *part = PART(mtd);
229 int ret;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200230
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 instr->addr += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800232 ret = part->master->_erase(part->master, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200233 if (ret) {
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300234 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Adrian Hunter74641d72007-03-08 12:20:12 +0200235 instr->fail_addr -= part->offset;
236 instr->addr -= part->offset;
237 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 return ret;
239}
240
241void mtd_erase_callback(struct erase_info *instr)
242{
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200243 if (instr->mtd->_erase == part_erase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 struct mtd_part *part = PART(instr->mtd);
245
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300246 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 instr->fail_addr -= part->offset;
248 instr->addr -= part->offset;
249 }
250 if (instr->callback)
251 instr->callback(instr);
252}
253EXPORT_SYMBOL_GPL(mtd_erase_callback);
254
Adrian Hunter69423d92008-12-10 13:37:21 +0000255static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256{
257 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800258 return part->master->_lock(part->master, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259}
260
Adrian Hunter69423d92008-12-10 13:37:21 +0000261static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800264 return part->master->_unlock(part->master, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265}
266
Richard Cochran99384242010-06-14 18:10:33 +0200267static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
268{
269 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800270 return part->master->_is_locked(part->master, ofs + part->offset, len);
Richard Cochran99384242010-06-14 18:10:33 +0200271}
272
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273static void part_sync(struct mtd_info *mtd)
274{
275 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800276 part->master->_sync(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277}
278
279static int part_suspend(struct mtd_info *mtd)
280{
281 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800282 return part->master->_suspend(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283}
284
285static void part_resume(struct mtd_info *mtd)
286{
287 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800288 part->master->_resume(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289}
290
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900291static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct mtd_part *part = PART(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 ofs += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800295 return part->master->_block_isbad(part->master, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296}
297
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900298static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299{
300 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200301 int res;
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 ofs += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800304 res = part->master->_block_markbad(part->master, ofs);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200305 if (!res)
306 mtd->ecc_stats.badblocks++;
307 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300310static inline void free_partition(struct mtd_part *p)
311{
312 kfree(p->mtd.name);
313 kfree(p);
314}
315
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000316/*
317 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 * attached to the given master MTD object.
319 */
320
321int del_mtd_partitions(struct mtd_info *master)
322{
Chris Malley71a928c2008-05-19 20:11:50 +0100323 struct mtd_part *slave, *next;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300324 int ret, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300326 mutex_lock(&mtd_partitions_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100327 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (slave->master == master) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300329 ret = del_mtd_device(&slave->mtd);
330 if (ret < 0) {
331 err = ret;
332 continue;
333 }
Chris Malley71a928c2008-05-19 20:11:50 +0100334 list_del(&slave->list);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300335 free_partition(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300337 mutex_unlock(&mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300339 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300342static struct mtd_part *allocate_partition(struct mtd_info *master,
343 const struct mtd_partition *part, int partno,
344 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900345{
346 struct mtd_part *slave;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300347 char *name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900348
349 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900350 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300351 name = kstrdup(part->name, GFP_KERNEL);
352 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900353 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300354 master->name);
355 kfree(name);
356 kfree(slave);
357 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900358 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900359
360 /* set up the MTD object for this partition */
361 slave->mtd.type = master->type;
362 slave->mtd.flags = master->flags & ~part->mask_flags;
363 slave->mtd.size = part->size;
364 slave->mtd.writesize = master->writesize;
Anatolij Gustschin7fa33ac2010-12-16 23:42:18 +0100365 slave->mtd.writebufsize = master->writebufsize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900366 slave->mtd.oobsize = master->oobsize;
367 slave->mtd.oobavail = master->oobavail;
368 slave->mtd.subpage_sft = master->subpage_sft;
369
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300370 slave->mtd.name = name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900371 slave->mtd.owner = master->owner;
David Howells402d3262009-02-12 10:40:00 +0000372 slave->mtd.backing_dev_info = master->backing_dev_info;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900373
David Brownell1f24b5a2009-03-26 00:42:41 -0700374 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
375 * to have the same data be in two different partitions.
376 */
377 slave->mtd.dev.parent = master->dev.parent;
378
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200379 slave->mtd._read = part_read;
380 slave->mtd._write = part_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900381
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200382 if (master->_panic_write)
383 slave->mtd._panic_write = part_panic_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900384
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200385 if (master->_point && master->_unpoint) {
386 slave->mtd._point = part_point;
387 slave->mtd._unpoint = part_unpoint;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900388 }
389
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200390 if (master->_get_unmapped_area)
391 slave->mtd._get_unmapped_area = part_get_unmapped_area;
392 if (master->_read_oob)
393 slave->mtd._read_oob = part_read_oob;
394 if (master->_write_oob)
395 slave->mtd._write_oob = part_write_oob;
396 if (master->_read_user_prot_reg)
397 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
398 if (master->_read_fact_prot_reg)
399 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
400 if (master->_write_user_prot_reg)
401 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
402 if (master->_lock_user_prot_reg)
403 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
404 if (master->_get_user_prot_info)
405 slave->mtd._get_user_prot_info = part_get_user_prot_info;
406 if (master->_get_fact_prot_info)
407 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
408 if (master->_sync)
409 slave->mtd._sync = part_sync;
410 if (!partno && !master->dev.class && master->_suspend &&
411 master->_resume) {
412 slave->mtd._suspend = part_suspend;
413 slave->mtd._resume = part_resume;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900414 }
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200415 if (master->_writev)
416 slave->mtd._writev = part_writev;
417 if (master->_lock)
418 slave->mtd._lock = part_lock;
419 if (master->_unlock)
420 slave->mtd._unlock = part_unlock;
421 if (master->_is_locked)
422 slave->mtd._is_locked = part_is_locked;
423 if (master->_block_isbad)
424 slave->mtd._block_isbad = part_block_isbad;
425 if (master->_block_markbad)
426 slave->mtd._block_markbad = part_block_markbad;
427 slave->mtd._erase = part_erase;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900428 slave->master = master;
429 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900430
431 if (slave->offset == MTDPART_OFS_APPEND)
432 slave->offset = cur_offset;
433 if (slave->offset == MTDPART_OFS_NXTBLK) {
434 slave->offset = cur_offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000435 if (mtd_mod_by_eb(cur_offset, master) != 0) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900436 /* Round up to next erasesize */
Adrian Hunter69423d92008-12-10 13:37:21 +0000437 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900438 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000439 "0x%012llx -> 0x%012llx\n", partno,
440 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900441 }
442 }
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400443 if (slave->offset == MTDPART_OFS_RETAIN) {
444 slave->offset = cur_offset;
445 if (master->size - slave->offset >= slave->mtd.size) {
446 slave->mtd.size = master->size - slave->offset
447 - slave->mtd.size;
448 } else {
449 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
450 part->name, master->size - slave->offset,
451 slave->mtd.size);
452 /* register to preserve ordering */
453 goto out_register;
454 }
455 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900456 if (slave->mtd.size == MTDPART_SIZ_FULL)
457 slave->mtd.size = master->size - slave->offset;
458
Adrian Hunter69423d92008-12-10 13:37:21 +0000459 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
460 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900461
462 /* let's do some sanity checks */
463 if (slave->offset >= master->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900464 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900465 slave->offset = 0;
466 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900467 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900468 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900469 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900470 }
471 if (slave->offset + slave->mtd.size > master->size) {
472 slave->mtd.size = master->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000473 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
474 part->name, master->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900475 }
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900476 if (master->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900477 /* Deal with variable erase size stuff */
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900478 int i, max = master->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000479 u64 end = slave->offset + slave->mtd.size;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900480 struct mtd_erase_region_info *regions = master->eraseregions;
481
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900482 /* Find the first erase regions which is part of this
483 * partition. */
484 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900485 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900486 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700487 if (i > 0)
488 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900489
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900490 /* Pick biggest erasesize */
491 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900492 if (slave->mtd.erasesize < regions[i].erasesize) {
493 slave->mtd.erasesize = regions[i].erasesize;
494 }
495 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900496 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900497 } else {
498 /* Single erase size */
499 slave->mtd.erasesize = master->erasesize;
500 }
501
502 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000503 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900504 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900505 /* FIXME: Let it be writable if it is on a boundary of
506 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900507 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900508 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 +0900509 part->name);
510 }
511 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000512 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900513 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900514 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900515 part->name);
516 }
517
518 slave->mtd.ecclayout = master->ecclayout;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700519 slave->mtd.ecc_strength = master->ecc_strength;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200520 if (master->_block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000521 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900522
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900523 while (offs < slave->mtd.size) {
Artem Bityutskiy7086c192011-12-23 19:35:30 +0200524 if (mtd_block_isbad(master, offs + slave->offset))
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900525 slave->mtd.ecc_stats.badblocks++;
526 offs += slave->mtd.erasesize;
527 }
528 }
529
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900530out_register:
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900531 return slave;
532}
533
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300534int mtd_add_partition(struct mtd_info *master, char *name,
535 long long offset, long long length)
536{
537 struct mtd_partition part;
538 struct mtd_part *p, *new;
539 uint64_t start, end;
540 int ret = 0;
541
542 /* the direct offset is expected */
543 if (offset == MTDPART_OFS_APPEND ||
544 offset == MTDPART_OFS_NXTBLK)
545 return -EINVAL;
546
547 if (length == MTDPART_SIZ_FULL)
548 length = master->size - offset;
549
550 if (length <= 0)
551 return -EINVAL;
552
553 part.name = name;
554 part.size = length;
555 part.offset = offset;
556 part.mask_flags = 0;
557 part.ecclayout = NULL;
558
559 new = allocate_partition(master, &part, -1, offset);
560 if (IS_ERR(new))
561 return PTR_ERR(new);
562
563 start = offset;
564 end = offset + length;
565
566 mutex_lock(&mtd_partitions_mutex);
567 list_for_each_entry(p, &mtd_partitions, list)
568 if (p->master == master) {
569 if ((start >= p->offset) &&
570 (start < (p->offset + p->mtd.size)))
571 goto err_inv;
572
573 if ((end >= p->offset) &&
574 (end < (p->offset + p->mtd.size)))
575 goto err_inv;
576 }
577
578 list_add(&new->list, &mtd_partitions);
579 mutex_unlock(&mtd_partitions_mutex);
580
581 add_mtd_device(&new->mtd);
582
583 return ret;
584err_inv:
585 mutex_unlock(&mtd_partitions_mutex);
586 free_partition(new);
587 return -EINVAL;
588}
589EXPORT_SYMBOL_GPL(mtd_add_partition);
590
591int mtd_del_partition(struct mtd_info *master, int partno)
592{
593 struct mtd_part *slave, *next;
594 int ret = -EINVAL;
595
596 mutex_lock(&mtd_partitions_mutex);
597 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
598 if ((slave->master == master) &&
599 (slave->mtd.index == partno)) {
600 ret = del_mtd_device(&slave->mtd);
601 if (ret < 0)
602 break;
603
604 list_del(&slave->list);
605 free_partition(slave);
606 break;
607 }
608 mutex_unlock(&mtd_partitions_mutex);
609
610 return ret;
611}
612EXPORT_SYMBOL_GPL(mtd_del_partition);
613
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614/*
615 * This function, given a master MTD object and a partition table, creates
616 * and registers slave MTD objects which are bound to the master according to
617 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700618 *
619 * We don't register the master, or expect the caller to have done so,
620 * for reasons of data integrity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
622
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000623int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 const struct mtd_partition *parts,
625 int nbparts)
626{
627 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000628 uint64_t cur_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 int i;
630
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900631 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 for (i = 0; i < nbparts; i++) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300634 slave = allocate_partition(master, parts + i, i, cur_offset);
635 if (IS_ERR(slave))
636 return PTR_ERR(slave);
637
638 mutex_lock(&mtd_partitions_mutex);
639 list_add(&slave->list, &mtd_partitions);
640 mutex_unlock(&mtd_partitions_mutex);
641
642 add_mtd_device(&slave->mtd);
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
646
647 return 0;
648}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649
650static DEFINE_SPINLOCK(part_parser_lock);
651static LIST_HEAD(part_parsers);
652
653static struct mtd_part_parser *get_partition_parser(const char *name)
654{
Chris Malley71a928c2008-05-19 20:11:50 +0100655 struct mtd_part_parser *p, *ret = NULL;
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 spin_lock(&part_parser_lock);
658
Chris Malley71a928c2008-05-19 20:11:50 +0100659 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
661 ret = p;
662 break;
663 }
Chris Malley71a928c2008-05-19 20:11:50 +0100664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 spin_unlock(&part_parser_lock);
666
667 return ret;
668}
669
Dmitry Eremin-Solenikov953b3bd2011-06-23 15:26:14 +0400670#define put_partition_parser(p) do { module_put((p)->owner); } while (0)
671
Linus Torvalds1da177e2005-04-16 15:20:36 -0700672int register_mtd_parser(struct mtd_part_parser *p)
673{
674 spin_lock(&part_parser_lock);
675 list_add(&p->list, &part_parsers);
676 spin_unlock(&part_parser_lock);
677
678 return 0;
679}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900680EXPORT_SYMBOL_GPL(register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
682int deregister_mtd_parser(struct mtd_part_parser *p)
683{
684 spin_lock(&part_parser_lock);
685 list_del(&p->list);
686 spin_unlock(&part_parser_lock);
687 return 0;
688}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900689EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700690
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300691/*
692 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
693 * are changing this array!
694 */
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400695static const char *default_mtd_part_types[] = {
696 "cmdlinepart",
697 "ofpart",
698 NULL
699};
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400700
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300701/**
702 * parse_mtd_partitions - parse MTD partitions
703 * @master: the master partition (describes whole MTD device)
704 * @types: names of partition parsers to try or %NULL
705 * @pparts: array of partitions found is returned here
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400706 * @data: MTD partition parser-specific data
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300707 *
708 * This function tries to find partition on MTD device @master. It uses MTD
709 * partition parsers, specified in @types. However, if @types is %NULL, then
710 * the default list of parsers is used. The default list contains only the
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400711 * "cmdlinepart" and "ofpart" parsers ATM.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300712 *
713 * This function may return:
714 * o a negative error code in case of failure
715 * o zero if no partitions were found
716 * o a positive number of found partitions, in which case on exit @pparts will
717 * point to an array containing this number of &struct mtd_info objects.
718 */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000719int parse_mtd_partitions(struct mtd_info *master, const char **types,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400720 struct mtd_partition **pparts,
721 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700722{
723 struct mtd_part_parser *parser;
724 int ret = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000725
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400726 if (!types)
727 types = default_mtd_part_types;
728
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729 for ( ; ret <= 0 && *types; types++) {
730 parser = get_partition_parser(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731 if (!parser && !request_module("%s", *types))
Stefan Roese58edc902012-01-25 11:24:36 +0100732 parser = get_partition_parser(*types);
Artem Bityutskiy7c802fb2011-05-17 11:13:17 +0300733 if (!parser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700734 continue;
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400735 ret = (*parser->parse_fn)(master, pparts, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000737 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 ret, parser->name, master->name);
739 }
740 put_partition_parser(parser);
741 }
742 return ret;
743}
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300744
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200745int mtd_is_partition(struct mtd_info *mtd)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300746{
747 struct mtd_part *part;
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200748 int ispart = 0;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300749
750 mutex_lock(&mtd_partitions_mutex);
751 list_for_each_entry(part, &mtd_partitions, list)
752 if (&part->mtd == mtd) {
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200753 ispart = 1;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300754 break;
755 }
756 mutex_unlock(&mtd_partitions_mutex);
757
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200758 return ispart;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300759}
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200760EXPORT_SYMBOL_GPL(mtd_is_partition);