blob: 630be3e7da04f2eed1ab628c21004ba077d4522c [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
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000356/*
357 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 * attached to the given master MTD object.
359 */
360
361int del_mtd_partitions(struct mtd_info *master)
362{
Chris Malley71a928c2008-05-19 20:11:50 +0100363 struct mtd_part *slave, *next;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300364 int ret, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300366 mutex_lock(&mtd_partitions_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100367 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 if (slave->master == master) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300369 ret = del_mtd_device(&slave->mtd);
370 if (ret < 0) {
371 err = ret;
372 continue;
373 }
Chris Malley71a928c2008-05-19 20:11:50 +0100374 list_del(&slave->list);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300375 free_partition(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300377 mutex_unlock(&mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300379 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380}
381
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300382static struct mtd_part *allocate_partition(struct mtd_info *master,
383 const struct mtd_partition *part, int partno,
384 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900385{
386 struct mtd_part *slave;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300387 char *name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900388
389 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900390 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300391 name = kstrdup(part->name, GFP_KERNEL);
392 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900393 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300394 master->name);
395 kfree(name);
396 kfree(slave);
397 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900398 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900399
400 /* set up the MTD object for this partition */
401 slave->mtd.type = master->type;
402 slave->mtd.flags = master->flags & ~part->mask_flags;
403 slave->mtd.size = part->size;
404 slave->mtd.writesize = master->writesize;
Anatolij Gustschin7fa33ac2010-12-16 23:42:18 +0100405 slave->mtd.writebufsize = master->writebufsize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900406 slave->mtd.oobsize = master->oobsize;
407 slave->mtd.oobavail = master->oobavail;
408 slave->mtd.subpage_sft = master->subpage_sft;
409
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300410 slave->mtd.name = name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900411 slave->mtd.owner = master->owner;
David Howells402d3262009-02-12 10:40:00 +0000412 slave->mtd.backing_dev_info = master->backing_dev_info;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900413
David Brownell1f24b5a2009-03-26 00:42:41 -0700414 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
415 * to have the same data be in two different partitions.
416 */
417 slave->mtd.dev.parent = master->dev.parent;
418
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900419 slave->mtd.read = part_read;
420 slave->mtd.write = part_write;
421
422 if (master->panic_write)
423 slave->mtd.panic_write = part_panic_write;
424
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900425 if (master->point && master->unpoint) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900426 slave->mtd.point = part_point;
427 slave->mtd.unpoint = part_unpoint;
428 }
429
David Howells402d3262009-02-12 10:40:00 +0000430 if (master->get_unmapped_area)
431 slave->mtd.get_unmapped_area = part_get_unmapped_area;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900432 if (master->read_oob)
433 slave->mtd.read_oob = part_read_oob;
434 if (master->write_oob)
435 slave->mtd.write_oob = part_write_oob;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900436 if (master->read_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900437 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900438 if (master->read_fact_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900439 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900440 if (master->write_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900441 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900442 if (master->lock_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900443 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900444 if (master->get_user_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900445 slave->mtd.get_user_prot_info = part_get_user_prot_info;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900446 if (master->get_fact_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900447 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
448 if (master->sync)
449 slave->mtd.sync = part_sync;
David Woodhouse4704a782009-04-05 07:56:23 -0700450 if (!partno && !master->dev.class && master->suspend && master->resume) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900451 slave->mtd.suspend = part_suspend;
452 slave->mtd.resume = part_resume;
453 }
454 if (master->writev)
455 slave->mtd.writev = part_writev;
456 if (master->lock)
457 slave->mtd.lock = part_lock;
458 if (master->unlock)
459 slave->mtd.unlock = part_unlock;
Richard Cochran99384242010-06-14 18:10:33 +0200460 if (master->is_locked)
461 slave->mtd.is_locked = part_is_locked;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900462 if (master->block_isbad)
463 slave->mtd.block_isbad = part_block_isbad;
464 if (master->block_markbad)
465 slave->mtd.block_markbad = part_block_markbad;
466 slave->mtd.erase = part_erase;
467 slave->master = master;
468 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900469
470 if (slave->offset == MTDPART_OFS_APPEND)
471 slave->offset = cur_offset;
472 if (slave->offset == MTDPART_OFS_NXTBLK) {
473 slave->offset = cur_offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000474 if (mtd_mod_by_eb(cur_offset, master) != 0) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900475 /* Round up to next erasesize */
Adrian Hunter69423d92008-12-10 13:37:21 +0000476 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900477 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000478 "0x%012llx -> 0x%012llx\n", partno,
479 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900480 }
481 }
482 if (slave->mtd.size == MTDPART_SIZ_FULL)
483 slave->mtd.size = master->size - slave->offset;
484
Adrian Hunter69423d92008-12-10 13:37:21 +0000485 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
486 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900487
488 /* let's do some sanity checks */
489 if (slave->offset >= master->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900490 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900491 slave->offset = 0;
492 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900493 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900494 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900495 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900496 }
497 if (slave->offset + slave->mtd.size > master->size) {
498 slave->mtd.size = master->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000499 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
500 part->name, master->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900501 }
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900502 if (master->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900503 /* Deal with variable erase size stuff */
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900504 int i, max = master->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000505 u64 end = slave->offset + slave->mtd.size;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900506 struct mtd_erase_region_info *regions = master->eraseregions;
507
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900508 /* Find the first erase regions which is part of this
509 * partition. */
510 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900511 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900512 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700513 if (i > 0)
514 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900515
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900516 /* Pick biggest erasesize */
517 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900518 if (slave->mtd.erasesize < regions[i].erasesize) {
519 slave->mtd.erasesize = regions[i].erasesize;
520 }
521 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900522 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900523 } else {
524 /* Single erase size */
525 slave->mtd.erasesize = master->erasesize;
526 }
527
528 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000529 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900530 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900531 /* FIXME: Let it be writable if it is on a boundary of
532 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900533 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900534 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 +0900535 part->name);
536 }
537 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000538 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900539 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900540 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900541 part->name);
542 }
543
544 slave->mtd.ecclayout = master->ecclayout;
545 if (master->block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000546 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900547
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900548 while (offs < slave->mtd.size) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900549 if (master->block_isbad(master,
550 offs + slave->offset))
551 slave->mtd.ecc_stats.badblocks++;
552 offs += slave->mtd.erasesize;
553 }
554 }
555
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900556out_register:
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900557 return slave;
558}
559
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300560int mtd_add_partition(struct mtd_info *master, char *name,
561 long long offset, long long length)
562{
563 struct mtd_partition part;
564 struct mtd_part *p, *new;
565 uint64_t start, end;
566 int ret = 0;
567
568 /* the direct offset is expected */
569 if (offset == MTDPART_OFS_APPEND ||
570 offset == MTDPART_OFS_NXTBLK)
571 return -EINVAL;
572
573 if (length == MTDPART_SIZ_FULL)
574 length = master->size - offset;
575
576 if (length <= 0)
577 return -EINVAL;
578
579 part.name = name;
580 part.size = length;
581 part.offset = offset;
582 part.mask_flags = 0;
583 part.ecclayout = NULL;
584
585 new = allocate_partition(master, &part, -1, offset);
586 if (IS_ERR(new))
587 return PTR_ERR(new);
588
589 start = offset;
590 end = offset + length;
591
592 mutex_lock(&mtd_partitions_mutex);
593 list_for_each_entry(p, &mtd_partitions, list)
594 if (p->master == master) {
595 if ((start >= p->offset) &&
596 (start < (p->offset + p->mtd.size)))
597 goto err_inv;
598
599 if ((end >= p->offset) &&
600 (end < (p->offset + p->mtd.size)))
601 goto err_inv;
602 }
603
604 list_add(&new->list, &mtd_partitions);
605 mutex_unlock(&mtd_partitions_mutex);
606
607 add_mtd_device(&new->mtd);
608
609 return ret;
610err_inv:
611 mutex_unlock(&mtd_partitions_mutex);
612 free_partition(new);
613 return -EINVAL;
614}
615EXPORT_SYMBOL_GPL(mtd_add_partition);
616
617int mtd_del_partition(struct mtd_info *master, int partno)
618{
619 struct mtd_part *slave, *next;
620 int ret = -EINVAL;
621
622 mutex_lock(&mtd_partitions_mutex);
623 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
624 if ((slave->master == master) &&
625 (slave->mtd.index == partno)) {
626 ret = del_mtd_device(&slave->mtd);
627 if (ret < 0)
628 break;
629
630 list_del(&slave->list);
631 free_partition(slave);
632 break;
633 }
634 mutex_unlock(&mtd_partitions_mutex);
635
636 return ret;
637}
638EXPORT_SYMBOL_GPL(mtd_del_partition);
639
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640/*
641 * This function, given a master MTD object and a partition table, creates
642 * and registers slave MTD objects which are bound to the master according to
643 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700644 *
645 * We don't register the master, or expect the caller to have done so,
646 * for reasons of data integrity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647 */
648
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000649int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700650 const struct mtd_partition *parts,
651 int nbparts)
652{
653 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000654 uint64_t cur_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655 int i;
656
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900657 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 for (i = 0; i < nbparts; i++) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300660 slave = allocate_partition(master, parts + i, i, cur_offset);
661 if (IS_ERR(slave))
662 return PTR_ERR(slave);
663
664 mutex_lock(&mtd_partitions_mutex);
665 list_add(&slave->list, &mtd_partitions);
666 mutex_unlock(&mtd_partitions_mutex);
667
668 add_mtd_device(&slave->mtd);
669
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671 }
672
673 return 0;
674}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676static DEFINE_SPINLOCK(part_parser_lock);
677static LIST_HEAD(part_parsers);
678
679static struct mtd_part_parser *get_partition_parser(const char *name)
680{
Chris Malley71a928c2008-05-19 20:11:50 +0100681 struct mtd_part_parser *p, *ret = NULL;
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 spin_lock(&part_parser_lock);
684
Chris Malley71a928c2008-05-19 20:11:50 +0100685 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
687 ret = p;
688 break;
689 }
Chris Malley71a928c2008-05-19 20:11:50 +0100690
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 spin_unlock(&part_parser_lock);
692
693 return ret;
694}
695
696int register_mtd_parser(struct mtd_part_parser *p)
697{
698 spin_lock(&part_parser_lock);
699 list_add(&p->list, &part_parsers);
700 spin_unlock(&part_parser_lock);
701
702 return 0;
703}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900704EXPORT_SYMBOL_GPL(register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705
706int deregister_mtd_parser(struct mtd_part_parser *p)
707{
708 spin_lock(&part_parser_lock);
709 list_del(&p->list);
710 spin_unlock(&part_parser_lock);
711 return 0;
712}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900713EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000715int parse_mtd_partitions(struct mtd_info *master, const char **types,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 struct mtd_partition **pparts, unsigned long origin)
717{
718 struct mtd_part_parser *parser;
719 int ret = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000720
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721 for ( ; ret <= 0 && *types; types++) {
722 parser = get_partition_parser(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 if (!parser && !request_module("%s", *types))
724 parser = get_partition_parser(*types);
Artem Bityutskiy7c802fb2011-05-17 11:13:17 +0300725 if (!parser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 ret = (*parser->parse_fn)(master, pparts, origin);
728 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000729 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700730 ret, parser->name, master->name);
731 }
732 put_partition_parser(parser);
733 }
734 return ret;
735}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700736EXPORT_SYMBOL_GPL(parse_mtd_partitions);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300737
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200738int mtd_is_partition(struct mtd_info *mtd)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300739{
740 struct mtd_part *part;
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200741 int ispart = 0;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300742
743 mutex_lock(&mtd_partitions_mutex);
744 list_for_each_entry(part, &mtd_partitions, list)
745 if (&part->mtd == mtd) {
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200746 ispart = 1;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300747 break;
748 }
749 mutex_unlock(&mtd_partitions_mutex);
750
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200751 return ispart;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300752}
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200753EXPORT_SYMBOL_GPL(mtd_is_partition);