blob: 79e3689f1e16901f07e5ee770bcde218814d621d [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
34/* Our partition linked list */
35static LIST_HEAD(mtd_partitions);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +030036static DEFINE_MUTEX(mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* Our partition node structure */
39struct mtd_part {
40 struct mtd_info mtd;
41 struct mtd_info *master;
Adrian Hunter69423d92008-12-10 13:37:21 +000042 uint64_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044};
45
46/*
47 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
48 * the pointer to that structure with this macro.
49 */
50#define PART(x) ((struct mtd_part *)(x))
51
Thomas Gleixner97894cd2005-11-07 11:15:26 +000052
53/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 * MTD methods which simply translate the effective address and pass through
55 * to the _real_ device.
56 */
57
Atsushi Nemotob33a2882008-07-19 01:00:33 +090058static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
59 size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070060{
61 struct mtd_part *part = PART(mtd);
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020062 struct mtd_ecc_stats stats;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020063 int res;
64
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020065 stats = part->master->ecc_stats;
66
Linus Torvalds1da177e2005-04-16 15:20:36 -070067 if (from >= mtd->size)
68 len = 0;
69 else if (from + len > mtd->size)
70 len = mtd->size - from;
Atsushi Nemotob33a2882008-07-19 01:00:33 +090071 res = part->master->read(part->master, from + part->offset,
Thomas Gleixner9223a452006-05-23 17:21:03 +020072 len, retlen, buf);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020073 if (unlikely(res)) {
74 if (res == -EUCLEAN)
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020075 mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020076 if (res == -EBADMSG)
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020077 mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020078 }
79 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070080}
81
Atsushi Nemotob33a2882008-07-19 01:00:33 +090082static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
83 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 struct mtd_part *part = PART(mtd);
86 if (from >= mtd->size)
87 len = 0;
88 else if (from + len > mtd->size)
89 len = mtd->size - from;
Thomas Gleixner97894cd2005-11-07 11:15:26 +000090 return part->master->point (part->master, from + part->offset,
Jared Hulberta98889f2008-04-29 23:26:49 -070091 len, retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092}
Thomas Gleixner9223a452006-05-23 17:21:03 +020093
Jared Hulberta98889f2008-04-29 23:26:49 -070094static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070095{
96 struct mtd_part *part = PART(mtd);
97
Jared Hulberta98889f2008-04-29 23:26:49 -070098 part->master->unpoint(part->master, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070099}
100
David Howells402d3262009-02-12 10:40:00 +0000101static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
102 unsigned long len,
103 unsigned long offset,
104 unsigned long flags)
105{
106 struct mtd_part *part = PART(mtd);
107
108 offset += part->offset;
109 return part->master->get_unmapped_area(part->master, len, offset,
110 flags);
111}
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 res = part->master->read_oob(part->master, from + part->offset, ops);
124
125 if (unlikely(res)) {
126 if (res == -EUCLEAN)
127 mtd->ecc_stats.corrected++;
128 if (res == -EBADMSG)
129 mtd->ecc_stats.failed++;
130 }
131 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900134static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
135 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136{
137 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900138 return part->master->read_user_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 len, retlen, buf);
140}
141
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900142static int part_get_user_prot_info(struct mtd_info *mtd,
143 struct otp_info *buf, size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000144{
145 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900146 return part->master->get_user_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000147}
148
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900149static int part_read_fact_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{
152 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900153 return part->master->read_fact_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 len, retlen, buf);
155}
156
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900157static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
158 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000159{
160 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900161 return part->master->get_fact_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000162}
163
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900164static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
165 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct mtd_part *part = PART(mtd);
168 if (!(mtd->flags & MTD_WRITEABLE))
169 return -EROFS;
170 if (to >= mtd->size)
171 len = 0;
172 else if (to + len > mtd->size)
173 len = mtd->size - to;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900174 return part->master->write(part->master, to + part->offset,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200175 len, retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176}
177
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900178static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
179 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000180{
181 struct mtd_part *part = PART(mtd);
182 if (!(mtd->flags & MTD_WRITEABLE))
183 return -EROFS;
184 if (to >= mtd->size)
185 len = 0;
186 else if (to + len > mtd->size)
187 len = mtd->size - to;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900188 return part->master->panic_write(part->master, to + part->offset,
Richard Purdie388bbb02008-02-06 10:17:15 +0000189 len, retlen, buf);
190}
191
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200192static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900193 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194{
195 struct mtd_part *part = PART(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 if (!(mtd->flags & MTD_WRITEABLE))
198 return -EROFS;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200199
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200201 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300202 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200203 return -EINVAL;
204 return part->master->write_oob(part->master, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205}
206
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900207static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
208 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209{
210 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900211 return part->master->write_user_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 len, retlen, buf);
213}
214
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900215static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
216 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000217{
218 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900219 return part->master->lock_user_prot_reg(part->master, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000220}
221
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900222static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
223 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224{
225 struct mtd_part *part = PART(mtd);
226 if (!(mtd->flags & MTD_WRITEABLE))
227 return -EROFS;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900228 return part->master->writev(part->master, vecs, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 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{
234 struct mtd_part *part = PART(mtd);
235 int ret;
236 if (!(mtd->flags & MTD_WRITEABLE))
237 return -EROFS;
238 if (instr->addr >= mtd->size)
239 return -EINVAL;
240 instr->addr += part->offset;
241 ret = part->master->erase(part->master, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200242 if (ret) {
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300243 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Adrian Hunter74641d72007-03-08 12:20:12 +0200244 instr->fail_addr -= part->offset;
245 instr->addr -= part->offset;
246 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 return ret;
248}
249
250void mtd_erase_callback(struct erase_info *instr)
251{
252 if (instr->mtd->erase == part_erase) {
253 struct mtd_part *part = PART(instr->mtd);
254
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300255 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700256 instr->fail_addr -= part->offset;
257 instr->addr -= part->offset;
258 }
259 if (instr->callback)
260 instr->callback(instr);
261}
262EXPORT_SYMBOL_GPL(mtd_erase_callback);
263
Adrian Hunter69423d92008-12-10 13:37:21 +0000264static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265{
266 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000267 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 return -EINVAL;
269 return part->master->lock(part->master, ofs + part->offset, len);
270}
271
Adrian Hunter69423d92008-12-10 13:37:21 +0000272static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273{
274 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000275 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700276 return -EINVAL;
277 return part->master->unlock(part->master, ofs + part->offset, len);
278}
279
Richard Cochran99384242010-06-14 18:10:33 +0200280static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
281{
282 struct mtd_part *part = PART(mtd);
283 if ((len + ofs) > mtd->size)
284 return -EINVAL;
285 return part->master->is_locked(part->master, ofs + part->offset, len);
286}
287
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288static void part_sync(struct mtd_info *mtd)
289{
290 struct mtd_part *part = PART(mtd);
291 part->master->sync(part->master);
292}
293
294static int part_suspend(struct mtd_info *mtd)
295{
296 struct mtd_part *part = PART(mtd);
297 return part->master->suspend(part->master);
298}
299
300static void part_resume(struct mtd_info *mtd)
301{
302 struct mtd_part *part = PART(mtd);
303 part->master->resume(part->master);
304}
305
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900306static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308 struct mtd_part *part = PART(mtd);
309 if (ofs >= mtd->size)
310 return -EINVAL;
311 ofs += part->offset;
312 return part->master->block_isbad(part->master, ofs);
313}
314
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900315static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316{
317 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200318 int res;
319
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (!(mtd->flags & MTD_WRITEABLE))
321 return -EROFS;
322 if (ofs >= mtd->size)
323 return -EINVAL;
324 ofs += part->offset;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200325 res = part->master->block_markbad(part->master, ofs);
326 if (!res)
327 mtd->ecc_stats.badblocks++;
328 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329}
330
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300331static inline void free_partition(struct mtd_part *p)
332{
333 kfree(p->mtd.name);
334 kfree(p);
335}
336
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000337/*
338 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 * attached to the given master MTD object.
340 */
341
342int del_mtd_partitions(struct mtd_info *master)
343{
Chris Malley71a928c2008-05-19 20:11:50 +0100344 struct mtd_part *slave, *next;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300345 int ret, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300347 mutex_lock(&mtd_partitions_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100348 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 if (slave->master == master) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300350 ret = del_mtd_device(&slave->mtd);
351 if (ret < 0) {
352 err = ret;
353 continue;
354 }
Chris Malley71a928c2008-05-19 20:11:50 +0100355 list_del(&slave->list);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300356 free_partition(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300358 mutex_unlock(&mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300360 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900362EXPORT_SYMBOL(del_mtd_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300364static struct mtd_part *allocate_partition(struct mtd_info *master,
365 const struct mtd_partition *part, int partno,
366 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900367{
368 struct mtd_part *slave;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300369 char *name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900370
371 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900372 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300373 name = kstrdup(part->name, GFP_KERNEL);
374 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900375 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300376 master->name);
377 kfree(name);
378 kfree(slave);
379 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900380 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900381
382 /* set up the MTD object for this partition */
383 slave->mtd.type = master->type;
384 slave->mtd.flags = master->flags & ~part->mask_flags;
385 slave->mtd.size = part->size;
386 slave->mtd.writesize = master->writesize;
387 slave->mtd.oobsize = master->oobsize;
388 slave->mtd.oobavail = master->oobavail;
389 slave->mtd.subpage_sft = master->subpage_sft;
390
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300391 slave->mtd.name = name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900392 slave->mtd.owner = master->owner;
David Howells402d3262009-02-12 10:40:00 +0000393 slave->mtd.backing_dev_info = master->backing_dev_info;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900394
David Brownell1f24b5a2009-03-26 00:42:41 -0700395 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
396 * to have the same data be in two different partitions.
397 */
398 slave->mtd.dev.parent = master->dev.parent;
399
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900400 slave->mtd.read = part_read;
401 slave->mtd.write = part_write;
402
403 if (master->panic_write)
404 slave->mtd.panic_write = part_panic_write;
405
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900406 if (master->point && master->unpoint) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900407 slave->mtd.point = part_point;
408 slave->mtd.unpoint = part_unpoint;
409 }
410
David Howells402d3262009-02-12 10:40:00 +0000411 if (master->get_unmapped_area)
412 slave->mtd.get_unmapped_area = part_get_unmapped_area;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900413 if (master->read_oob)
414 slave->mtd.read_oob = part_read_oob;
415 if (master->write_oob)
416 slave->mtd.write_oob = part_write_oob;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900417 if (master->read_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900418 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900419 if (master->read_fact_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900420 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900421 if (master->write_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900422 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900423 if (master->lock_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900424 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900425 if (master->get_user_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900426 slave->mtd.get_user_prot_info = part_get_user_prot_info;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900427 if (master->get_fact_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900428 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
429 if (master->sync)
430 slave->mtd.sync = part_sync;
David Woodhouse4704a782009-04-05 07:56:23 -0700431 if (!partno && !master->dev.class && master->suspend && master->resume) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900432 slave->mtd.suspend = part_suspend;
433 slave->mtd.resume = part_resume;
434 }
435 if (master->writev)
436 slave->mtd.writev = part_writev;
437 if (master->lock)
438 slave->mtd.lock = part_lock;
439 if (master->unlock)
440 slave->mtd.unlock = part_unlock;
Richard Cochran99384242010-06-14 18:10:33 +0200441 if (master->is_locked)
442 slave->mtd.is_locked = part_is_locked;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900443 if (master->block_isbad)
444 slave->mtd.block_isbad = part_block_isbad;
445 if (master->block_markbad)
446 slave->mtd.block_markbad = part_block_markbad;
447 slave->mtd.erase = part_erase;
448 slave->master = master;
449 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900450
451 if (slave->offset == MTDPART_OFS_APPEND)
452 slave->offset = cur_offset;
453 if (slave->offset == MTDPART_OFS_NXTBLK) {
454 slave->offset = cur_offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000455 if (mtd_mod_by_eb(cur_offset, master) != 0) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900456 /* Round up to next erasesize */
Adrian Hunter69423d92008-12-10 13:37:21 +0000457 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900458 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000459 "0x%012llx -> 0x%012llx\n", partno,
460 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900461 }
462 }
463 if (slave->mtd.size == MTDPART_SIZ_FULL)
464 slave->mtd.size = master->size - slave->offset;
465
Adrian Hunter69423d92008-12-10 13:37:21 +0000466 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
467 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900468
469 /* let's do some sanity checks */
470 if (slave->offset >= master->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900471 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900472 slave->offset = 0;
473 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900474 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900475 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900476 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900477 }
478 if (slave->offset + slave->mtd.size > master->size) {
479 slave->mtd.size = master->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000480 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
481 part->name, master->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900482 }
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900483 if (master->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900484 /* Deal with variable erase size stuff */
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900485 int i, max = master->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000486 u64 end = slave->offset + slave->mtd.size;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900487 struct mtd_erase_region_info *regions = master->eraseregions;
488
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900489 /* Find the first erase regions which is part of this
490 * partition. */
491 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900492 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900493 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700494 if (i > 0)
495 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900496
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900497 /* Pick biggest erasesize */
498 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900499 if (slave->mtd.erasesize < regions[i].erasesize) {
500 slave->mtd.erasesize = regions[i].erasesize;
501 }
502 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900503 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900504 } else {
505 /* Single erase size */
506 slave->mtd.erasesize = master->erasesize;
507 }
508
509 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000510 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900511 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900512 /* FIXME: Let it be writable if it is on a boundary of
513 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900514 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900515 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 +0900516 part->name);
517 }
518 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000519 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900520 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900521 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900522 part->name);
523 }
524
525 slave->mtd.ecclayout = master->ecclayout;
526 if (master->block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000527 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900528
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900529 while (offs < slave->mtd.size) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900530 if (master->block_isbad(master,
531 offs + slave->offset))
532 slave->mtd.ecc_stats.badblocks++;
533 offs += slave->mtd.erasesize;
534 }
535 }
536
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900537out_register:
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900538 return slave;
539}
540
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300541int mtd_add_partition(struct mtd_info *master, char *name,
542 long long offset, long long length)
543{
544 struct mtd_partition part;
545 struct mtd_part *p, *new;
546 uint64_t start, end;
547 int ret = 0;
548
549 /* the direct offset is expected */
550 if (offset == MTDPART_OFS_APPEND ||
551 offset == MTDPART_OFS_NXTBLK)
552 return -EINVAL;
553
554 if (length == MTDPART_SIZ_FULL)
555 length = master->size - offset;
556
557 if (length <= 0)
558 return -EINVAL;
559
560 part.name = name;
561 part.size = length;
562 part.offset = offset;
563 part.mask_flags = 0;
564 part.ecclayout = NULL;
565
566 new = allocate_partition(master, &part, -1, offset);
567 if (IS_ERR(new))
568 return PTR_ERR(new);
569
570 start = offset;
571 end = offset + length;
572
573 mutex_lock(&mtd_partitions_mutex);
574 list_for_each_entry(p, &mtd_partitions, list)
575 if (p->master == master) {
576 if ((start >= p->offset) &&
577 (start < (p->offset + p->mtd.size)))
578 goto err_inv;
579
580 if ((end >= p->offset) &&
581 (end < (p->offset + p->mtd.size)))
582 goto err_inv;
583 }
584
585 list_add(&new->list, &mtd_partitions);
586 mutex_unlock(&mtd_partitions_mutex);
587
588 add_mtd_device(&new->mtd);
589
590 return ret;
591err_inv:
592 mutex_unlock(&mtd_partitions_mutex);
593 free_partition(new);
594 return -EINVAL;
595}
596EXPORT_SYMBOL_GPL(mtd_add_partition);
597
598int mtd_del_partition(struct mtd_info *master, int partno)
599{
600 struct mtd_part *slave, *next;
601 int ret = -EINVAL;
602
603 mutex_lock(&mtd_partitions_mutex);
604 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
605 if ((slave->master == master) &&
606 (slave->mtd.index == partno)) {
607 ret = del_mtd_device(&slave->mtd);
608 if (ret < 0)
609 break;
610
611 list_del(&slave->list);
612 free_partition(slave);
613 break;
614 }
615 mutex_unlock(&mtd_partitions_mutex);
616
617 return ret;
618}
619EXPORT_SYMBOL_GPL(mtd_del_partition);
620
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621/*
622 * This function, given a master MTD object and a partition table, creates
623 * and registers slave MTD objects which are bound to the master according to
624 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700625 *
626 * We don't register the master, or expect the caller to have done so,
627 * for reasons of data integrity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 */
629
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000630int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700631 const struct mtd_partition *parts,
632 int nbparts)
633{
634 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000635 uint64_t cur_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 int i;
637
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900638 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639
640 for (i = 0; i < nbparts; i++) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300641 slave = allocate_partition(master, parts + i, i, cur_offset);
642 if (IS_ERR(slave))
643 return PTR_ERR(slave);
644
645 mutex_lock(&mtd_partitions_mutex);
646 list_add(&slave->list, &mtd_partitions);
647 mutex_unlock(&mtd_partitions_mutex);
648
649 add_mtd_device(&slave->mtd);
650
Linus Torvalds1da177e2005-04-16 15:20:36 -0700651 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 }
653
654 return 0;
655}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656EXPORT_SYMBOL(add_mtd_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658static DEFINE_SPINLOCK(part_parser_lock);
659static LIST_HEAD(part_parsers);
660
661static struct mtd_part_parser *get_partition_parser(const char *name)
662{
Chris Malley71a928c2008-05-19 20:11:50 +0100663 struct mtd_part_parser *p, *ret = NULL;
664
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 spin_lock(&part_parser_lock);
666
Chris Malley71a928c2008-05-19 20:11:50 +0100667 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
669 ret = p;
670 break;
671 }
Chris Malley71a928c2008-05-19 20:11:50 +0100672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 spin_unlock(&part_parser_lock);
674
675 return ret;
676}
677
678int register_mtd_parser(struct mtd_part_parser *p)
679{
680 spin_lock(&part_parser_lock);
681 list_add(&p->list, &part_parsers);
682 spin_unlock(&part_parser_lock);
683
684 return 0;
685}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900686EXPORT_SYMBOL_GPL(register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687
688int deregister_mtd_parser(struct mtd_part_parser *p)
689{
690 spin_lock(&part_parser_lock);
691 list_del(&p->list);
692 spin_unlock(&part_parser_lock);
693 return 0;
694}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900695EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000697int parse_mtd_partitions(struct mtd_info *master, const char **types,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700698 struct mtd_partition **pparts, unsigned long origin)
699{
700 struct mtd_part_parser *parser;
701 int ret = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000702
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 for ( ; ret <= 0 && *types; types++) {
704 parser = get_partition_parser(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705 if (!parser && !request_module("%s", *types))
706 parser = get_partition_parser(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (!parser) {
708 printk(KERN_NOTICE "%s partition parsing not available\n",
709 *types);
710 continue;
711 }
712 ret = (*parser->parse_fn)(master, pparts, origin);
713 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000714 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 ret, parser->name, master->name);
716 }
717 put_partition_parser(parser);
718 }
719 return ret;
720}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721EXPORT_SYMBOL_GPL(parse_mtd_partitions);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300722
723int mtd_is_master(struct mtd_info *mtd)
724{
725 struct mtd_part *part;
726 int nopart = 0;
727
728 mutex_lock(&mtd_partitions_mutex);
729 list_for_each_entry(part, &mtd_partitions, list)
730 if (&part->mtd == mtd) {
731 nopart = 1;
732 break;
733 }
734 mutex_unlock(&mtd_partitions_mutex);
735
736 return nopart;
737}
738EXPORT_SYMBOL_GPL(mtd_is_master);