blob: 4c75f32785e32681e7d10a284a571daf0bf39f23 [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;
Atsushi Nemotob33a2882008-07-19 01:00:33 +090073 res = part->master->read(part->master, from + part->offset,
Thomas Gleixner9223a452006-05-23 17:21:03 +020074 len, retlen, buf);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020075 if (unlikely(res)) {
76 if (res == -EUCLEAN)
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020077 mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020078 if (res == -EBADMSG)
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020079 mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020080 }
81 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070082}
83
Atsushi Nemotob33a2882008-07-19 01:00:33 +090084static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
85 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
87 struct mtd_part *part = PART(mtd);
88 if (from >= mtd->size)
89 len = 0;
90 else if (from + len > mtd->size)
91 len = mtd->size - from;
Thomas Gleixner97894cd2005-11-07 11:15:26 +000092 return part->master->point (part->master, from + part->offset,
Jared Hulberta98889f2008-04-29 23:26:49 -070093 len, retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094}
Thomas Gleixner9223a452006-05-23 17:21:03 +020095
Jared Hulberta98889f2008-04-29 23:26:49 -070096static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct mtd_part *part = PART(mtd);
99
Jared Hulberta98889f2008-04-29 23:26:49 -0700100 part->master->unpoint(part->master, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101}
102
David Howells402d3262009-02-12 10:40:00 +0000103static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
104 unsigned long len,
105 unsigned long offset,
106 unsigned long flags)
107{
108 struct mtd_part *part = PART(mtd);
109
110 offset += part->offset;
111 return part->master->get_unmapped_area(part->master, len, offset,
112 flags);
113}
114
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200115static int part_read_oob(struct mtd_info *mtd, loff_t from,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900116 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117{
118 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200119 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200120
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121 if (from >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200122 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300123 if (ops->datbuf && from + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200124 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200125
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200126 /*
127 * If OOB is also requested, make sure that we do not read past the end
128 * of this partition.
129 */
130 if (ops->oobbuf) {
131 size_t len, pages;
132
133 if (ops->mode == MTD_OOB_AUTO)
134 len = mtd->oobavail;
135 else
136 len = mtd->oobsize;
137 pages = mtd_div_by_ws(mtd->size, mtd);
138 pages -= mtd_div_by_ws(from, mtd);
139 if (ops->ooboffs + ops->ooblen > pages * len)
140 return -EINVAL;
141 }
142
143 res = part->master->read_oob(part->master, from + part->offset, ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200144 if (unlikely(res)) {
145 if (res == -EUCLEAN)
146 mtd->ecc_stats.corrected++;
147 if (res == -EBADMSG)
148 mtd->ecc_stats.failed++;
149 }
150 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900153static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
154 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900157 return part->master->read_user_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 len, retlen, buf);
159}
160
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900161static int part_get_user_prot_info(struct mtd_info *mtd,
162 struct otp_info *buf, size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000163{
164 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900165 return part->master->get_user_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000166}
167
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900168static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
169 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170{
171 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900172 return part->master->read_fact_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 len, retlen, buf);
174}
175
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900176static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
177 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000178{
179 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900180 return part->master->get_fact_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000181}
182
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900183static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
184 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
186 struct mtd_part *part = PART(mtd);
187 if (!(mtd->flags & MTD_WRITEABLE))
188 return -EROFS;
189 if (to >= mtd->size)
190 len = 0;
191 else if (to + len > mtd->size)
192 len = mtd->size - to;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900193 return part->master->write(part->master, to + part->offset,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200194 len, retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195}
196
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900197static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
198 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000199{
200 struct mtd_part *part = PART(mtd);
201 if (!(mtd->flags & MTD_WRITEABLE))
202 return -EROFS;
203 if (to >= mtd->size)
204 len = 0;
205 else if (to + len > mtd->size)
206 len = mtd->size - to;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900207 return part->master->panic_write(part->master, to + part->offset,
Richard Purdie388bbb02008-02-06 10:17:15 +0000208 len, retlen, buf);
209}
210
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200211static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900212 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct mtd_part *part = PART(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200215
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (!(mtd->flags & MTD_WRITEABLE))
217 return -EROFS;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200218
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200220 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300221 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200222 return -EINVAL;
223 return part->master->write_oob(part->master, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900226static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
227 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
229 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900230 return part->master->write_user_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231 len, retlen, buf);
232}
233
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900234static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
235 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000236{
237 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900238 return part->master->lock_user_prot_reg(part->master, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000239}
240
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900241static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
242 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243{
244 struct mtd_part *part = PART(mtd);
245 if (!(mtd->flags & MTD_WRITEABLE))
246 return -EROFS;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900247 return part->master->writev(part->master, vecs, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249}
250
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900251static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 struct mtd_part *part = PART(mtd);
254 int ret;
255 if (!(mtd->flags & MTD_WRITEABLE))
256 return -EROFS;
257 if (instr->addr >= mtd->size)
258 return -EINVAL;
259 instr->addr += part->offset;
260 ret = part->master->erase(part->master, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200261 if (ret) {
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300262 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Adrian Hunter74641d72007-03-08 12:20:12 +0200263 instr->fail_addr -= part->offset;
264 instr->addr -= part->offset;
265 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 return ret;
267}
268
269void mtd_erase_callback(struct erase_info *instr)
270{
271 if (instr->mtd->erase == part_erase) {
272 struct mtd_part *part = PART(instr->mtd);
273
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300274 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 instr->fail_addr -= part->offset;
276 instr->addr -= part->offset;
277 }
278 if (instr->callback)
279 instr->callback(instr);
280}
281EXPORT_SYMBOL_GPL(mtd_erase_callback);
282
Adrian Hunter69423d92008-12-10 13:37:21 +0000283static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
285 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000286 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287 return -EINVAL;
288 return part->master->lock(part->master, ofs + part->offset, len);
289}
290
Adrian Hunter69423d92008-12-10 13:37:21 +0000291static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000294 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 return -EINVAL;
296 return part->master->unlock(part->master, ofs + part->offset, len);
297}
298
Richard Cochran99384242010-06-14 18:10:33 +0200299static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
300{
301 struct mtd_part *part = PART(mtd);
302 if ((len + ofs) > mtd->size)
303 return -EINVAL;
304 return part->master->is_locked(part->master, ofs + part->offset, len);
305}
306
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307static void part_sync(struct mtd_info *mtd)
308{
309 struct mtd_part *part = PART(mtd);
310 part->master->sync(part->master);
311}
312
313static int part_suspend(struct mtd_info *mtd)
314{
315 struct mtd_part *part = PART(mtd);
316 return part->master->suspend(part->master);
317}
318
319static void part_resume(struct mtd_info *mtd)
320{
321 struct mtd_part *part = PART(mtd);
322 part->master->resume(part->master);
323}
324
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900325static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326{
327 struct mtd_part *part = PART(mtd);
328 if (ofs >= mtd->size)
329 return -EINVAL;
330 ofs += part->offset;
331 return part->master->block_isbad(part->master, ofs);
332}
333
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900334static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335{
336 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200337 int res;
338
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 if (!(mtd->flags & MTD_WRITEABLE))
340 return -EROFS;
341 if (ofs >= mtd->size)
342 return -EINVAL;
343 ofs += part->offset;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200344 res = part->master->block_markbad(part->master, ofs);
345 if (!res)
346 mtd->ecc_stats.badblocks++;
347 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348}
349
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300350static inline void free_partition(struct mtd_part *p)
351{
352 kfree(p->mtd.name);
353 kfree(p);
354}
355
Murali Palnati3e8c3bf2012-02-15 16:51:17 +0530356void part_fill_badblockstats(struct mtd_info *mtd)
357{
358 struct mtd_part *part = PART(mtd);
359 if (part->master->block_isbad) {
360 uint64_t offs = 0;
361 mtd->ecc_stats.badblocks = 0;
362 while (offs < mtd->size) {
363 if (part->master->block_isbad(part->master,
364 offs + part->offset))
365 mtd->ecc_stats.badblocks++;
366 offs += mtd->erasesize;
367 }
368 }
369}
370
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000371/*
372 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 * attached to the given master MTD object.
374 */
375
376int del_mtd_partitions(struct mtd_info *master)
377{
Chris Malley71a928c2008-05-19 20:11:50 +0100378 struct mtd_part *slave, *next;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300379 int ret, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300381 mutex_lock(&mtd_partitions_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100382 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 if (slave->master == master) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300384 ret = del_mtd_device(&slave->mtd);
385 if (ret < 0) {
386 err = ret;
387 continue;
388 }
Chris Malley71a928c2008-05-19 20:11:50 +0100389 list_del(&slave->list);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300390 free_partition(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300392 mutex_unlock(&mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300394 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395}
396
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300397static struct mtd_part *allocate_partition(struct mtd_info *master,
398 const struct mtd_partition *part, int partno,
399 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900400{
401 struct mtd_part *slave;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300402 char *name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900403
404 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900405 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300406 name = kstrdup(part->name, GFP_KERNEL);
407 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900408 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300409 master->name);
410 kfree(name);
411 kfree(slave);
412 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900413 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900414
415 /* set up the MTD object for this partition */
416 slave->mtd.type = master->type;
417 slave->mtd.flags = master->flags & ~part->mask_flags;
418 slave->mtd.size = part->size;
419 slave->mtd.writesize = master->writesize;
Anatolij Gustschin7fa33ac2010-12-16 23:42:18 +0100420 slave->mtd.writebufsize = master->writebufsize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900421 slave->mtd.oobsize = master->oobsize;
422 slave->mtd.oobavail = master->oobavail;
423 slave->mtd.subpage_sft = master->subpage_sft;
424
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300425 slave->mtd.name = name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900426 slave->mtd.owner = master->owner;
David Howells402d3262009-02-12 10:40:00 +0000427 slave->mtd.backing_dev_info = master->backing_dev_info;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900428
David Brownell1f24b5a2009-03-26 00:42:41 -0700429 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
430 * to have the same data be in two different partitions.
431 */
432 slave->mtd.dev.parent = master->dev.parent;
433
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900434 slave->mtd.read = part_read;
435 slave->mtd.write = part_write;
436
437 if (master->panic_write)
438 slave->mtd.panic_write = part_panic_write;
439
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900440 if (master->point && master->unpoint) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900441 slave->mtd.point = part_point;
442 slave->mtd.unpoint = part_unpoint;
443 }
444
David Howells402d3262009-02-12 10:40:00 +0000445 if (master->get_unmapped_area)
446 slave->mtd.get_unmapped_area = part_get_unmapped_area;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900447 if (master->read_oob)
448 slave->mtd.read_oob = part_read_oob;
449 if (master->write_oob)
450 slave->mtd.write_oob = part_write_oob;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900451 if (master->read_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900452 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900453 if (master->read_fact_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900454 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900455 if (master->write_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900456 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900457 if (master->lock_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900458 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900459 if (master->get_user_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900460 slave->mtd.get_user_prot_info = part_get_user_prot_info;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900461 if (master->get_fact_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900462 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
463 if (master->sync)
464 slave->mtd.sync = part_sync;
David Woodhouse4704a782009-04-05 07:56:23 -0700465 if (!partno && !master->dev.class && master->suspend && master->resume) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900466 slave->mtd.suspend = part_suspend;
467 slave->mtd.resume = part_resume;
468 }
469 if (master->writev)
470 slave->mtd.writev = part_writev;
471 if (master->lock)
472 slave->mtd.lock = part_lock;
473 if (master->unlock)
474 slave->mtd.unlock = part_unlock;
Richard Cochran99384242010-06-14 18:10:33 +0200475 if (master->is_locked)
476 slave->mtd.is_locked = part_is_locked;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900477 if (master->block_isbad)
478 slave->mtd.block_isbad = part_block_isbad;
479 if (master->block_markbad)
480 slave->mtd.block_markbad = part_block_markbad;
481 slave->mtd.erase = part_erase;
482 slave->master = master;
483 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900484
485 if (slave->offset == MTDPART_OFS_APPEND)
486 slave->offset = cur_offset;
487 if (slave->offset == MTDPART_OFS_NXTBLK) {
488 slave->offset = cur_offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000489 if (mtd_mod_by_eb(cur_offset, master) != 0) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900490 /* Round up to next erasesize */
Adrian Hunter69423d92008-12-10 13:37:21 +0000491 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900492 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000493 "0x%012llx -> 0x%012llx\n", partno,
494 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900495 }
496 }
497 if (slave->mtd.size == MTDPART_SIZ_FULL)
498 slave->mtd.size = master->size - slave->offset;
499
Adrian Hunter69423d92008-12-10 13:37:21 +0000500 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
501 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900502
503 /* let's do some sanity checks */
504 if (slave->offset >= master->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900505 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900506 slave->offset = 0;
507 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900508 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900509 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900510 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900511 }
512 if (slave->offset + slave->mtd.size > master->size) {
513 slave->mtd.size = master->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000514 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
515 part->name, master->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900516 }
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900517 if (master->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900518 /* Deal with variable erase size stuff */
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900519 int i, max = master->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000520 u64 end = slave->offset + slave->mtd.size;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900521 struct mtd_erase_region_info *regions = master->eraseregions;
522
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900523 /* Find the first erase regions which is part of this
524 * partition. */
525 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900526 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900527 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700528 if (i > 0)
529 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900530
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900531 /* Pick biggest erasesize */
532 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900533 if (slave->mtd.erasesize < regions[i].erasesize) {
534 slave->mtd.erasesize = regions[i].erasesize;
535 }
536 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900537 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900538 } else {
539 /* Single erase size */
540 slave->mtd.erasesize = master->erasesize;
541 }
542
543 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000544 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900545 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900546 /* FIXME: Let it be writable if it is on a boundary of
547 * _minor_ erase size though */
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 start on an erase block boundary -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900550 part->name);
551 }
552 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000553 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900554 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900555 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900556 part->name);
557 }
558
559 slave->mtd.ecclayout = master->ecclayout;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900560
Murali Palnati3e8c3bf2012-02-15 16:51:17 +0530561#ifndef CONFIG_MTD_LAZYECCSTATS
562 part_fill_badblockstats(&(slave->mtd));
563#endif
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900564
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
705int register_mtd_parser(struct mtd_part_parser *p)
706{
707 spin_lock(&part_parser_lock);
708 list_add(&p->list, &part_parsers);
709 spin_unlock(&part_parser_lock);
710
711 return 0;
712}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900713EXPORT_SYMBOL_GPL(register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715int deregister_mtd_parser(struct mtd_part_parser *p)
716{
717 spin_lock(&part_parser_lock);
718 list_del(&p->list);
719 spin_unlock(&part_parser_lock);
720 return 0;
721}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900722EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000724int parse_mtd_partitions(struct mtd_info *master, const char **types,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725 struct mtd_partition **pparts, unsigned long origin)
726{
727 struct mtd_part_parser *parser;
728 int ret = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000729
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 for ( ; ret <= 0 && *types; types++) {
731 parser = get_partition_parser(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700732 if (!parser && !request_module("%s", *types))
733 parser = get_partition_parser(*types);
Artem Bityutskiy7c802fb2011-05-17 11:13:17 +0300734 if (!parser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736 ret = (*parser->parse_fn)(master, pparts, origin);
737 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000738 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 ret, parser->name, master->name);
740 }
741 put_partition_parser(parser);
742 }
743 return ret;
744}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745EXPORT_SYMBOL_GPL(parse_mtd_partitions);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300746
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200747int mtd_is_partition(struct mtd_info *mtd)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300748{
749 struct mtd_part *part;
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200750 int ispart = 0;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300751
752 mutex_lock(&mtd_partitions_mutex);
753 list_for_each_entry(part, &mtd_partitions, list)
754 if (&part->mtd == mtd) {
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200755 ispart = 1;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300756 break;
757 }
758 mutex_unlock(&mtd_partitions_mutex);
759
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200760 return ispart;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300761}
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200762EXPORT_SYMBOL_GPL(mtd_is_partition);