blob: bf1ab56afb8ea54dbb3876bc660834e72c590201 [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;
68
Linus Torvalds1da177e2005-04-16 15:20:36 -070069 if (from >= mtd->size)
70 len = 0;
71 else if (from + len > mtd->size)
72 len = mtd->size - from;
Artem Bityutskiy329ad392011-12-23 17:30:16 +020073 res = mtd_read(part->master, from + part->offset, len, retlen, buf);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020074 if (unlikely(res)) {
Brian Norrisd57f40542011-09-20 18:34:25 -070075 if (mtd_is_bitflip(res))
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020076 mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
Brian Norrisd57f40542011-09-20 18:34:25 -070077 if (mtd_is_eccerr(res))
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020078 mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020079 }
80 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{
86 struct mtd_part *part = PART(mtd);
87 if (from >= mtd->size)
88 len = 0;
89 else if (from + len > mtd->size)
90 len = mtd->size - from;
Artem Bityutskiyd35ea202011-12-23 17:00:37 +020091 return mtd_point(part->master, from + part->offset, len, retlen,
92 virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
Thomas Gleixner9223a452006-05-23 17:21:03 +020094
Jared Hulberta98889f2008-04-29 23:26:49 -070095static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070096{
97 struct mtd_part *part = PART(mtd);
98
Artem Bityutskiy72197782011-12-23 17:05:52 +020099 mtd_unpoint(part->master, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100}
101
David Howells402d3262009-02-12 10:40:00 +0000102static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
103 unsigned long len,
104 unsigned long offset,
105 unsigned long flags)
106{
107 struct mtd_part *part = PART(mtd);
108
109 offset += part->offset;
Artem Bityutskiy04c601b2011-12-23 17:10:15 +0200110 return mtd_get_unmapped_area(part->master, len, offset, flags);
David Howells402d3262009-02-12 10:40:00 +0000111}
112
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200113static int part_read_oob(struct mtd_info *mtd, loff_t from,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900114 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
116 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200117 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200118
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 if (from >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200120 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300121 if (ops->datbuf && from + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200122 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200123
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200124 /*
125 * If OOB is also requested, make sure that we do not read past the end
126 * of this partition.
127 */
128 if (ops->oobbuf) {
129 size_t len, pages;
130
Brian Norris0612b9d2011-08-30 18:45:40 -0700131 if (ops->mode == MTD_OPS_AUTO_OOB)
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200132 len = mtd->oobavail;
133 else
134 len = mtd->oobsize;
135 pages = mtd_div_by_ws(mtd->size, mtd);
136 pages -= mtd_div_by_ws(from, mtd);
137 if (ops->ooboffs + ops->ooblen > pages * len)
138 return -EINVAL;
139 }
140
Artem Bityutskiyfd2819b2011-12-23 18:27:05 +0200141 res = mtd_read_oob(part->master, from + part->offset, ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200142 if (unlikely(res)) {
Brian Norrisd57f40542011-09-20 18:34:25 -0700143 if (mtd_is_bitflip(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200144 mtd->ecc_stats.corrected++;
Brian Norrisd57f40542011-09-20 18:34:25 -0700145 if (mtd_is_eccerr(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200146 mtd->ecc_stats.failed++;
147 }
148 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900151static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
152 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900155 return part->master->read_user_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156 len, retlen, buf);
157}
158
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900159static int part_get_user_prot_info(struct mtd_info *mtd,
160 struct otp_info *buf, size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000161{
162 struct mtd_part *part = PART(mtd);
Artem Bityutskiy855e5d82011-12-23 18:45:11 +0200163 return mtd_get_user_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000164}
165
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900166static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
167 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
169 struct mtd_part *part = PART(mtd);
Artem Bityutskiyd264f722011-12-23 18:40:06 +0200170 return mtd_read_fact_prot_reg(part->master, from, len, retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900173static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
174 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000175{
176 struct mtd_part *part = PART(mtd);
Artem Bityutskiya750b5c2011-12-23 18:33:28 +0200177 return mtd_get_fact_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000178}
179
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900180static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
181 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
183 struct mtd_part *part = PART(mtd);
184 if (!(mtd->flags & MTD_WRITEABLE))
185 return -EROFS;
186 if (to >= mtd->size)
187 len = 0;
188 else if (to + len > mtd->size)
189 len = mtd->size - to;
Artem Bityutskiyeda95cb2011-12-23 17:35:41 +0200190 return mtd_write(part->master, to + part->offset, len, retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900193static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
194 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000195{
196 struct mtd_part *part = PART(mtd);
197 if (!(mtd->flags & MTD_WRITEABLE))
198 return -EROFS;
199 if (to >= mtd->size)
200 len = 0;
201 else if (to + len > mtd->size)
202 len = mtd->size - to;
Artem Bityutskiy7ae79d72011-12-23 18:03:17 +0200203 return mtd_panic_write(part->master, to + part->offset, len, retlen,
204 buf);
Richard Purdie388bbb02008-02-06 10:17:15 +0000205}
206
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200207static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900208 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 struct mtd_part *part = PART(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200211
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 if (!(mtd->flags & MTD_WRITEABLE))
213 return -EROFS;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200214
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200216 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300217 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200218 return -EINVAL;
Artem Bityutskiya2cc5ba2011-12-23 18:29:55 +0200219 return mtd_write_oob(part->master, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900222static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
223 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900226 return part->master->write_user_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227 len, retlen, buf);
228}
229
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900230static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
231 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000232{
233 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900234 return part->master->lock_user_prot_reg(part->master, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000235}
236
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900237static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
238 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239{
240 struct mtd_part *part = PART(mtd);
241 if (!(mtd->flags & MTD_WRITEABLE))
242 return -EROFS;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900243 return part->master->writev(part->master, vecs, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245}
246
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900247static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248{
249 struct mtd_part *part = PART(mtd);
250 int ret;
251 if (!(mtd->flags & MTD_WRITEABLE))
252 return -EROFS;
253 if (instr->addr >= mtd->size)
254 return -EINVAL;
255 instr->addr += part->offset;
Artem Bityutskiy7e1f0dc2011-12-23 15:25:39 +0200256 ret = mtd_erase(part->master, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200257 if (ret) {
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300258 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Adrian Hunter74641d72007-03-08 12:20:12 +0200259 instr->fail_addr -= part->offset;
260 instr->addr -= part->offset;
261 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 return ret;
263}
264
265void mtd_erase_callback(struct erase_info *instr)
266{
267 if (instr->mtd->erase == part_erase) {
268 struct mtd_part *part = PART(instr->mtd);
269
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300270 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271 instr->fail_addr -= part->offset;
272 instr->addr -= part->offset;
273 }
274 if (instr->callback)
275 instr->callback(instr);
276}
277EXPORT_SYMBOL_GPL(mtd_erase_callback);
278
Adrian Hunter69423d92008-12-10 13:37:21 +0000279static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280{
281 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000282 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 return -EINVAL;
284 return part->master->lock(part->master, ofs + part->offset, len);
285}
286
Adrian Hunter69423d92008-12-10 13:37:21 +0000287static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000290 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 return -EINVAL;
292 return part->master->unlock(part->master, ofs + part->offset, len);
293}
294
Richard Cochran99384242010-06-14 18:10:33 +0200295static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
296{
297 struct mtd_part *part = PART(mtd);
298 if ((len + ofs) > mtd->size)
299 return -EINVAL;
300 return part->master->is_locked(part->master, ofs + part->offset, len);
301}
302
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303static void part_sync(struct mtd_info *mtd)
304{
305 struct mtd_part *part = PART(mtd);
306 part->master->sync(part->master);
307}
308
309static int part_suspend(struct mtd_info *mtd)
310{
311 struct mtd_part *part = PART(mtd);
312 return part->master->suspend(part->master);
313}
314
315static void part_resume(struct mtd_info *mtd)
316{
317 struct mtd_part *part = PART(mtd);
318 part->master->resume(part->master);
319}
320
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900321static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
323 struct mtd_part *part = PART(mtd);
324 if (ofs >= mtd->size)
325 return -EINVAL;
326 ofs += part->offset;
327 return part->master->block_isbad(part->master, ofs);
328}
329
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900330static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331{
332 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200333 int res;
334
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 if (!(mtd->flags & MTD_WRITEABLE))
336 return -EROFS;
337 if (ofs >= mtd->size)
338 return -EINVAL;
339 ofs += part->offset;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200340 res = part->master->block_markbad(part->master, ofs);
341 if (!res)
342 mtd->ecc_stats.badblocks++;
343 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344}
345
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300346static inline void free_partition(struct mtd_part *p)
347{
348 kfree(p->mtd.name);
349 kfree(p);
350}
351
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000352/*
353 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 * attached to the given master MTD object.
355 */
356
357int del_mtd_partitions(struct mtd_info *master)
358{
Chris Malley71a928c2008-05-19 20:11:50 +0100359 struct mtd_part *slave, *next;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300360 int ret, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300362 mutex_lock(&mtd_partitions_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100363 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 if (slave->master == master) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300365 ret = del_mtd_device(&slave->mtd);
366 if (ret < 0) {
367 err = ret;
368 continue;
369 }
Chris Malley71a928c2008-05-19 20:11:50 +0100370 list_del(&slave->list);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300371 free_partition(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300373 mutex_unlock(&mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300375 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376}
377
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300378static struct mtd_part *allocate_partition(struct mtd_info *master,
379 const struct mtd_partition *part, int partno,
380 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900381{
382 struct mtd_part *slave;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300383 char *name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900384
385 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900386 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300387 name = kstrdup(part->name, GFP_KERNEL);
388 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900389 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300390 master->name);
391 kfree(name);
392 kfree(slave);
393 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900394 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900395
396 /* set up the MTD object for this partition */
397 slave->mtd.type = master->type;
398 slave->mtd.flags = master->flags & ~part->mask_flags;
399 slave->mtd.size = part->size;
400 slave->mtd.writesize = master->writesize;
Anatolij Gustschin7fa33ac2010-12-16 23:42:18 +0100401 slave->mtd.writebufsize = master->writebufsize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900402 slave->mtd.oobsize = master->oobsize;
403 slave->mtd.oobavail = master->oobavail;
404 slave->mtd.subpage_sft = master->subpage_sft;
405
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300406 slave->mtd.name = name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900407 slave->mtd.owner = master->owner;
David Howells402d3262009-02-12 10:40:00 +0000408 slave->mtd.backing_dev_info = master->backing_dev_info;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900409
David Brownell1f24b5a2009-03-26 00:42:41 -0700410 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
411 * to have the same data be in two different partitions.
412 */
413 slave->mtd.dev.parent = master->dev.parent;
414
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900415 slave->mtd.read = part_read;
416 slave->mtd.write = part_write;
417
418 if (master->panic_write)
419 slave->mtd.panic_write = part_panic_write;
420
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900421 if (master->point && master->unpoint) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900422 slave->mtd.point = part_point;
423 slave->mtd.unpoint = part_unpoint;
424 }
425
David Howells402d3262009-02-12 10:40:00 +0000426 if (master->get_unmapped_area)
427 slave->mtd.get_unmapped_area = part_get_unmapped_area;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900428 if (master->read_oob)
429 slave->mtd.read_oob = part_read_oob;
430 if (master->write_oob)
431 slave->mtd.write_oob = part_write_oob;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900432 if (master->read_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900433 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900434 if (master->read_fact_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900435 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900436 if (master->write_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900437 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900438 if (master->lock_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900439 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900440 if (master->get_user_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900441 slave->mtd.get_user_prot_info = part_get_user_prot_info;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900442 if (master->get_fact_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900443 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
444 if (master->sync)
445 slave->mtd.sync = part_sync;
David Woodhouse4704a782009-04-05 07:56:23 -0700446 if (!partno && !master->dev.class && master->suspend && master->resume) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900447 slave->mtd.suspend = part_suspend;
448 slave->mtd.resume = part_resume;
449 }
450 if (master->writev)
451 slave->mtd.writev = part_writev;
452 if (master->lock)
453 slave->mtd.lock = part_lock;
454 if (master->unlock)
455 slave->mtd.unlock = part_unlock;
Richard Cochran99384242010-06-14 18:10:33 +0200456 if (master->is_locked)
457 slave->mtd.is_locked = part_is_locked;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900458 if (master->block_isbad)
459 slave->mtd.block_isbad = part_block_isbad;
460 if (master->block_markbad)
461 slave->mtd.block_markbad = part_block_markbad;
462 slave->mtd.erase = part_erase;
463 slave->master = master;
464 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900465
466 if (slave->offset == MTDPART_OFS_APPEND)
467 slave->offset = cur_offset;
468 if (slave->offset == MTDPART_OFS_NXTBLK) {
469 slave->offset = cur_offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000470 if (mtd_mod_by_eb(cur_offset, master) != 0) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900471 /* Round up to next erasesize */
Adrian Hunter69423d92008-12-10 13:37:21 +0000472 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900473 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000474 "0x%012llx -> 0x%012llx\n", partno,
475 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900476 }
477 }
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400478 if (slave->offset == MTDPART_OFS_RETAIN) {
479 slave->offset = cur_offset;
480 if (master->size - slave->offset >= slave->mtd.size) {
481 slave->mtd.size = master->size - slave->offset
482 - slave->mtd.size;
483 } else {
484 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
485 part->name, master->size - slave->offset,
486 slave->mtd.size);
487 /* register to preserve ordering */
488 goto out_register;
489 }
490 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900491 if (slave->mtd.size == MTDPART_SIZ_FULL)
492 slave->mtd.size = master->size - slave->offset;
493
Adrian Hunter69423d92008-12-10 13:37:21 +0000494 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
495 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900496
497 /* let's do some sanity checks */
498 if (slave->offset >= master->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900499 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900500 slave->offset = 0;
501 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900502 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900503 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900504 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900505 }
506 if (slave->offset + slave->mtd.size > master->size) {
507 slave->mtd.size = master->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000508 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
509 part->name, master->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900510 }
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900511 if (master->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900512 /* Deal with variable erase size stuff */
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900513 int i, max = master->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000514 u64 end = slave->offset + slave->mtd.size;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900515 struct mtd_erase_region_info *regions = master->eraseregions;
516
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900517 /* Find the first erase regions which is part of this
518 * partition. */
519 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900520 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900521 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700522 if (i > 0)
523 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900524
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900525 /* Pick biggest erasesize */
526 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900527 if (slave->mtd.erasesize < regions[i].erasesize) {
528 slave->mtd.erasesize = regions[i].erasesize;
529 }
530 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900531 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900532 } else {
533 /* Single erase size */
534 slave->mtd.erasesize = master->erasesize;
535 }
536
537 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000538 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900539 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900540 /* FIXME: Let it be writable if it is on a boundary of
541 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900542 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900543 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 +0900544 part->name);
545 }
546 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000547 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900548 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900549 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900550 part->name);
551 }
552
553 slave->mtd.ecclayout = master->ecclayout;
554 if (master->block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000555 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900556
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900557 while (offs < slave->mtd.size) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900558 if (master->block_isbad(master,
559 offs + slave->offset))
560 slave->mtd.ecc_stats.badblocks++;
561 offs += slave->mtd.erasesize;
562 }
563 }
564
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900565out_register:
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900566 return slave;
567}
568
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300569int mtd_add_partition(struct mtd_info *master, char *name,
570 long long offset, long long length)
571{
572 struct mtd_partition part;
573 struct mtd_part *p, *new;
574 uint64_t start, end;
575 int ret = 0;
576
577 /* the direct offset is expected */
578 if (offset == MTDPART_OFS_APPEND ||
579 offset == MTDPART_OFS_NXTBLK)
580 return -EINVAL;
581
582 if (length == MTDPART_SIZ_FULL)
583 length = master->size - offset;
584
585 if (length <= 0)
586 return -EINVAL;
587
588 part.name = name;
589 part.size = length;
590 part.offset = offset;
591 part.mask_flags = 0;
592 part.ecclayout = NULL;
593
594 new = allocate_partition(master, &part, -1, offset);
595 if (IS_ERR(new))
596 return PTR_ERR(new);
597
598 start = offset;
599 end = offset + length;
600
601 mutex_lock(&mtd_partitions_mutex);
602 list_for_each_entry(p, &mtd_partitions, list)
603 if (p->master == master) {
604 if ((start >= p->offset) &&
605 (start < (p->offset + p->mtd.size)))
606 goto err_inv;
607
608 if ((end >= p->offset) &&
609 (end < (p->offset + p->mtd.size)))
610 goto err_inv;
611 }
612
613 list_add(&new->list, &mtd_partitions);
614 mutex_unlock(&mtd_partitions_mutex);
615
616 add_mtd_device(&new->mtd);
617
618 return ret;
619err_inv:
620 mutex_unlock(&mtd_partitions_mutex);
621 free_partition(new);
622 return -EINVAL;
623}
624EXPORT_SYMBOL_GPL(mtd_add_partition);
625
626int mtd_del_partition(struct mtd_info *master, int partno)
627{
628 struct mtd_part *slave, *next;
629 int ret = -EINVAL;
630
631 mutex_lock(&mtd_partitions_mutex);
632 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
633 if ((slave->master == master) &&
634 (slave->mtd.index == partno)) {
635 ret = del_mtd_device(&slave->mtd);
636 if (ret < 0)
637 break;
638
639 list_del(&slave->list);
640 free_partition(slave);
641 break;
642 }
643 mutex_unlock(&mtd_partitions_mutex);
644
645 return ret;
646}
647EXPORT_SYMBOL_GPL(mtd_del_partition);
648
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649/*
650 * This function, given a master MTD object and a partition table, creates
651 * and registers slave MTD objects which are bound to the master according to
652 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700653 *
654 * We don't register the master, or expect the caller to have done so,
655 * for reasons of data integrity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
657
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000658int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700659 const struct mtd_partition *parts,
660 int nbparts)
661{
662 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000663 uint64_t cur_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664 int i;
665
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900666 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667
668 for (i = 0; i < nbparts; i++) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300669 slave = allocate_partition(master, parts + i, i, cur_offset);
670 if (IS_ERR(slave))
671 return PTR_ERR(slave);
672
673 mutex_lock(&mtd_partitions_mutex);
674 list_add(&slave->list, &mtd_partitions);
675 mutex_unlock(&mtd_partitions_mutex);
676
677 add_mtd_device(&slave->mtd);
678
Linus Torvalds1da177e2005-04-16 15:20:36 -0700679 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
681
682 return 0;
683}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700684
685static DEFINE_SPINLOCK(part_parser_lock);
686static LIST_HEAD(part_parsers);
687
688static struct mtd_part_parser *get_partition_parser(const char *name)
689{
Chris Malley71a928c2008-05-19 20:11:50 +0100690 struct mtd_part_parser *p, *ret = NULL;
691
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692 spin_lock(&part_parser_lock);
693
Chris Malley71a928c2008-05-19 20:11:50 +0100694 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
696 ret = p;
697 break;
698 }
Chris Malley71a928c2008-05-19 20:11:50 +0100699
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 spin_unlock(&part_parser_lock);
701
702 return ret;
703}
704
Dmitry Eremin-Solenikov953b3bd2011-06-23 15:26:14 +0400705#define put_partition_parser(p) do { module_put((p)->owner); } while (0)
706
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707int register_mtd_parser(struct mtd_part_parser *p)
708{
709 spin_lock(&part_parser_lock);
710 list_add(&p->list, &part_parsers);
711 spin_unlock(&part_parser_lock);
712
713 return 0;
714}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900715EXPORT_SYMBOL_GPL(register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716
717int deregister_mtd_parser(struct mtd_part_parser *p)
718{
719 spin_lock(&part_parser_lock);
720 list_del(&p->list);
721 spin_unlock(&part_parser_lock);
722 return 0;
723}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900724EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300726/*
727 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
728 * are changing this array!
729 */
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400730static const char *default_mtd_part_types[] = {
731 "cmdlinepart",
732 "ofpart",
733 NULL
734};
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400735
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300736/**
737 * parse_mtd_partitions - parse MTD partitions
738 * @master: the master partition (describes whole MTD device)
739 * @types: names of partition parsers to try or %NULL
740 * @pparts: array of partitions found is returned here
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400741 * @data: MTD partition parser-specific data
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300742 *
743 * This function tries to find partition on MTD device @master. It uses MTD
744 * partition parsers, specified in @types. However, if @types is %NULL, then
745 * the default list of parsers is used. The default list contains only the
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400746 * "cmdlinepart" and "ofpart" parsers ATM.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300747 *
748 * This function may return:
749 * o a negative error code in case of failure
750 * o zero if no partitions were found
751 * o a positive number of found partitions, in which case on exit @pparts will
752 * point to an array containing this number of &struct mtd_info objects.
753 */
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000754int parse_mtd_partitions(struct mtd_info *master, const char **types,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400755 struct mtd_partition **pparts,
756 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757{
758 struct mtd_part_parser *parser;
759 int ret = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000760
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400761 if (!types)
762 types = default_mtd_part_types;
763
Linus Torvalds1da177e2005-04-16 15:20:36 -0700764 for ( ; ret <= 0 && *types; types++) {
765 parser = get_partition_parser(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766 if (!parser && !request_module("%s", *types))
767 parser = get_partition_parser(*types);
Artem Bityutskiy7c802fb2011-05-17 11:13:17 +0300768 if (!parser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 continue;
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400770 ret = (*parser->parse_fn)(master, pparts, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000772 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 ret, parser->name, master->name);
774 }
775 put_partition_parser(parser);
776 }
777 return ret;
778}
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300779
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200780int mtd_is_partition(struct mtd_info *mtd)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300781{
782 struct mtd_part *part;
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200783 int ispart = 0;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300784
785 mutex_lock(&mtd_partitions_mutex);
786 list_for_each_entry(part, &mtd_partitions, list)
787 if (&part->mtd == mtd) {
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200788 ispart = 1;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300789 break;
790 }
791 mutex_unlock(&mtd_partitions_mutex);
792
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200793 return ispart;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300794}
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200795EXPORT_SYMBOL_GPL(mtd_is_partition);