blob: 63d1cd2c17be3f08db82ab2a2c19faf57d891cff [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Simple MTD partitioning layer
3 *
4 * (C) 2000 Nicolas Pitre <nico@cam.org>
5 *
6 * This code is GPL
7 *
Linus Torvalds1da177e2005-04-16 15:20:36 -07008 * 02-21-2002 Thomas Gleixner <gleixner@autronix.de>
9 * added support for read_oob, write_oob
Thomas Gleixner97894cd2005-11-07 11:15:26 +000010 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070011
12#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/kernel.h>
15#include <linux/slab.h>
16#include <linux/list.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/kmod.h>
18#include <linux/mtd/mtd.h>
19#include <linux/mtd/partitions.h>
20#include <linux/mtd/compatmac.h>
21
22/* Our partition linked list */
23static LIST_HEAD(mtd_partitions);
24
25/* Our partition node structure */
26struct mtd_part {
27 struct mtd_info mtd;
28 struct mtd_info *master;
Adrian Hunter69423d92008-12-10 13:37:21 +000029 uint64_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 int index;
31 struct list_head list;
32 int registered;
33};
34
35/*
36 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
37 * the pointer to that structure with this macro.
38 */
39#define PART(x) ((struct mtd_part *)(x))
40
Thomas Gleixner97894cd2005-11-07 11:15:26 +000041
42/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 * MTD methods which simply translate the effective address and pass through
44 * to the _real_ device.
45 */
46
Atsushi Nemotob33a2882008-07-19 01:00:33 +090047static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
48 size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
50 struct mtd_part *part = PART(mtd);
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020051 struct mtd_ecc_stats stats;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020052 int res;
53
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020054 stats = part->master->ecc_stats;
55
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 if (from >= mtd->size)
57 len = 0;
58 else if (from + len > mtd->size)
59 len = mtd->size - from;
Atsushi Nemotob33a2882008-07-19 01:00:33 +090060 res = part->master->read(part->master, from + part->offset,
Thomas Gleixner9223a452006-05-23 17:21:03 +020061 len, retlen, buf);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020062 if (unlikely(res)) {
63 if (res == -EUCLEAN)
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020064 mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020065 if (res == -EBADMSG)
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020066 mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020067 }
68 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069}
70
Atsushi Nemotob33a2882008-07-19 01:00:33 +090071static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
72 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070073{
74 struct mtd_part *part = PART(mtd);
75 if (from >= mtd->size)
76 len = 0;
77 else if (from + len > mtd->size)
78 len = mtd->size - from;
Thomas Gleixner97894cd2005-11-07 11:15:26 +000079 return part->master->point (part->master, from + part->offset,
Jared Hulberta98889f2008-04-29 23:26:49 -070080 len, retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081}
Thomas Gleixner9223a452006-05-23 17:21:03 +020082
Jared Hulberta98889f2008-04-29 23:26:49 -070083static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070084{
85 struct mtd_part *part = PART(mtd);
86
Jared Hulberta98889f2008-04-29 23:26:49 -070087 part->master->unpoint(part->master, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070088}
89
David Howells402d3262009-02-12 10:40:00 +000090static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
91 unsigned long len,
92 unsigned long offset,
93 unsigned long flags)
94{
95 struct mtd_part *part = PART(mtd);
96
97 offset += part->offset;
98 return part->master->get_unmapped_area(part->master, len, offset,
99 flags);
100}
101
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200102static int part_read_oob(struct mtd_info *mtd, loff_t from,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900103 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200106 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200107
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 if (from >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200109 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300110 if (ops->datbuf && from + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200111 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200112 res = part->master->read_oob(part->master, from + part->offset, ops);
113
114 if (unlikely(res)) {
115 if (res == -EUCLEAN)
116 mtd->ecc_stats.corrected++;
117 if (res == -EBADMSG)
118 mtd->ecc_stats.failed++;
119 }
120 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121}
122
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900123static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
124 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
126 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900127 return part->master->read_user_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 len, retlen, buf);
129}
130
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900131static int part_get_user_prot_info(struct mtd_info *mtd,
132 struct otp_info *buf, size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000133{
134 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900135 return part->master->get_user_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000136}
137
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900138static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
139 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140{
141 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900142 return part->master->read_fact_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 len, retlen, buf);
144}
145
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900146static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
147 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000148{
149 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900150 return part->master->get_fact_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000151}
152
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900153static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
154 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155{
156 struct mtd_part *part = PART(mtd);
157 if (!(mtd->flags & MTD_WRITEABLE))
158 return -EROFS;
159 if (to >= mtd->size)
160 len = 0;
161 else if (to + len > mtd->size)
162 len = mtd->size - to;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900163 return part->master->write(part->master, to + part->offset,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200164 len, retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165}
166
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900167static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
168 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000169{
170 struct mtd_part *part = PART(mtd);
171 if (!(mtd->flags & MTD_WRITEABLE))
172 return -EROFS;
173 if (to >= mtd->size)
174 len = 0;
175 else if (to + len > mtd->size)
176 len = mtd->size - to;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900177 return part->master->panic_write(part->master, to + part->offset,
Richard Purdie388bbb02008-02-06 10:17:15 +0000178 len, retlen, buf);
179}
180
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200181static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900182 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
184 struct mtd_part *part = PART(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200185
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 if (!(mtd->flags & MTD_WRITEABLE))
187 return -EROFS;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200188
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200190 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300191 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200192 return -EINVAL;
193 return part->master->write_oob(part->master, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900196static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
197 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198{
199 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900200 return part->master->write_user_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 len, retlen, buf);
202}
203
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900204static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
205 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000206{
207 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900208 return part->master->lock_user_prot_reg(part->master, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000209}
210
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900211static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
212 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
214 struct mtd_part *part = PART(mtd);
215 if (!(mtd->flags & MTD_WRITEABLE))
216 return -EROFS;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900217 return part->master->writev(part->master, vecs, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219}
220
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900221static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 struct mtd_part *part = PART(mtd);
224 int ret;
225 if (!(mtd->flags & MTD_WRITEABLE))
226 return -EROFS;
227 if (instr->addr >= mtd->size)
228 return -EINVAL;
229 instr->addr += part->offset;
230 ret = part->master->erase(part->master, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200231 if (ret) {
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300232 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Adrian Hunter74641d72007-03-08 12:20:12 +0200233 instr->fail_addr -= part->offset;
234 instr->addr -= part->offset;
235 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236 return ret;
237}
238
239void mtd_erase_callback(struct erase_info *instr)
240{
241 if (instr->mtd->erase == part_erase) {
242 struct mtd_part *part = PART(instr->mtd);
243
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300244 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 instr->fail_addr -= part->offset;
246 instr->addr -= part->offset;
247 }
248 if (instr->callback)
249 instr->callback(instr);
250}
251EXPORT_SYMBOL_GPL(mtd_erase_callback);
252
Adrian Hunter69423d92008-12-10 13:37:21 +0000253static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254{
255 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000256 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 return -EINVAL;
258 return part->master->lock(part->master, ofs + part->offset, len);
259}
260
Adrian Hunter69423d92008-12-10 13:37:21 +0000261static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262{
263 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000264 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700265 return -EINVAL;
266 return part->master->unlock(part->master, ofs + part->offset, len);
267}
268
269static void part_sync(struct mtd_info *mtd)
270{
271 struct mtd_part *part = PART(mtd);
272 part->master->sync(part->master);
273}
274
275static int part_suspend(struct mtd_info *mtd)
276{
277 struct mtd_part *part = PART(mtd);
278 return part->master->suspend(part->master);
279}
280
281static void part_resume(struct mtd_info *mtd)
282{
283 struct mtd_part *part = PART(mtd);
284 part->master->resume(part->master);
285}
286
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900287static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700288{
289 struct mtd_part *part = PART(mtd);
290 if (ofs >= mtd->size)
291 return -EINVAL;
292 ofs += part->offset;
293 return part->master->block_isbad(part->master, ofs);
294}
295
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900296static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297{
298 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200299 int res;
300
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301 if (!(mtd->flags & MTD_WRITEABLE))
302 return -EROFS;
303 if (ofs >= mtd->size)
304 return -EINVAL;
305 ofs += part->offset;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200306 res = part->master->block_markbad(part->master, ofs);
307 if (!res)
308 mtd->ecc_stats.badblocks++;
309 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310}
311
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000312/*
313 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * attached to the given master MTD object.
315 */
316
317int del_mtd_partitions(struct mtd_info *master)
318{
Chris Malley71a928c2008-05-19 20:11:50 +0100319 struct mtd_part *slave, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320
Chris Malley71a928c2008-05-19 20:11:50 +0100321 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 if (slave->master == master) {
Chris Malley71a928c2008-05-19 20:11:50 +0100323 list_del(&slave->list);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900324 if (slave->registered)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 del_mtd_device(&slave->mtd);
326 kfree(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 return 0;
330}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900331EXPORT_SYMBOL(del_mtd_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900333static struct mtd_part *add_one_partition(struct mtd_info *master,
334 const struct mtd_partition *part, int partno,
Adrian Hunter69423d92008-12-10 13:37:21 +0000335 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900336{
337 struct mtd_part *slave;
338
339 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900340 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900341 if (!slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900342 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900343 master->name);
344 del_mtd_partitions(master);
345 return NULL;
346 }
347 list_add(&slave->list, &mtd_partitions);
348
349 /* set up the MTD object for this partition */
350 slave->mtd.type = master->type;
351 slave->mtd.flags = master->flags & ~part->mask_flags;
352 slave->mtd.size = part->size;
353 slave->mtd.writesize = master->writesize;
354 slave->mtd.oobsize = master->oobsize;
355 slave->mtd.oobavail = master->oobavail;
356 slave->mtd.subpage_sft = master->subpage_sft;
357
358 slave->mtd.name = part->name;
359 slave->mtd.owner = master->owner;
David Howells402d3262009-02-12 10:40:00 +0000360 slave->mtd.backing_dev_info = master->backing_dev_info;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900361
David Brownell1f24b5a2009-03-26 00:42:41 -0700362 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
363 * to have the same data be in two different partitions.
364 */
365 slave->mtd.dev.parent = master->dev.parent;
366
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900367 slave->mtd.read = part_read;
368 slave->mtd.write = part_write;
369
370 if (master->panic_write)
371 slave->mtd.panic_write = part_panic_write;
372
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900373 if (master->point && master->unpoint) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900374 slave->mtd.point = part_point;
375 slave->mtd.unpoint = part_unpoint;
376 }
377
David Howells402d3262009-02-12 10:40:00 +0000378 if (master->get_unmapped_area)
379 slave->mtd.get_unmapped_area = part_get_unmapped_area;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900380 if (master->read_oob)
381 slave->mtd.read_oob = part_read_oob;
382 if (master->write_oob)
383 slave->mtd.write_oob = part_write_oob;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900384 if (master->read_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900385 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900386 if (master->read_fact_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900387 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900388 if (master->write_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900389 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900390 if (master->lock_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900391 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900392 if (master->get_user_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900393 slave->mtd.get_user_prot_info = part_get_user_prot_info;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900394 if (master->get_fact_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900395 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
396 if (master->sync)
397 slave->mtd.sync = part_sync;
David Woodhouse4704a782009-04-05 07:56:23 -0700398 if (!partno && !master->dev.class && master->suspend && master->resume) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900399 slave->mtd.suspend = part_suspend;
400 slave->mtd.resume = part_resume;
401 }
402 if (master->writev)
403 slave->mtd.writev = part_writev;
404 if (master->lock)
405 slave->mtd.lock = part_lock;
406 if (master->unlock)
407 slave->mtd.unlock = part_unlock;
408 if (master->block_isbad)
409 slave->mtd.block_isbad = part_block_isbad;
410 if (master->block_markbad)
411 slave->mtd.block_markbad = part_block_markbad;
412 slave->mtd.erase = part_erase;
413 slave->master = master;
414 slave->offset = part->offset;
415 slave->index = partno;
416
417 if (slave->offset == MTDPART_OFS_APPEND)
418 slave->offset = cur_offset;
419 if (slave->offset == MTDPART_OFS_NXTBLK) {
420 slave->offset = cur_offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000421 if (mtd_mod_by_eb(cur_offset, master) != 0) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900422 /* Round up to next erasesize */
Adrian Hunter69423d92008-12-10 13:37:21 +0000423 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900424 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000425 "0x%012llx -> 0x%012llx\n", partno,
426 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900427 }
428 }
429 if (slave->mtd.size == MTDPART_SIZ_FULL)
430 slave->mtd.size = master->size - slave->offset;
431
Adrian Hunter69423d92008-12-10 13:37:21 +0000432 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
433 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900434
435 /* let's do some sanity checks */
436 if (slave->offset >= master->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900437 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900438 slave->offset = 0;
439 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900440 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900441 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900442 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900443 }
444 if (slave->offset + slave->mtd.size > master->size) {
445 slave->mtd.size = master->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000446 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
447 part->name, master->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900448 }
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900449 if (master->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900450 /* Deal with variable erase size stuff */
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900451 int i, max = master->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000452 u64 end = slave->offset + slave->mtd.size;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900453 struct mtd_erase_region_info *regions = master->eraseregions;
454
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900455 /* Find the first erase regions which is part of this
456 * partition. */
457 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900458 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900459 /* The loop searched for the region _behind_ the first one */
460 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900461
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900462 /* Pick biggest erasesize */
463 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900464 if (slave->mtd.erasesize < regions[i].erasesize) {
465 slave->mtd.erasesize = regions[i].erasesize;
466 }
467 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900468 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900469 } else {
470 /* Single erase size */
471 slave->mtd.erasesize = master->erasesize;
472 }
473
474 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000475 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900476 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900477 /* FIXME: Let it be writable if it is on a boundary of
478 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900479 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900480 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 +0900481 part->name);
482 }
483 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000484 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900485 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900486 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900487 part->name);
488 }
489
490 slave->mtd.ecclayout = master->ecclayout;
491 if (master->block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000492 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900493
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900494 while (offs < slave->mtd.size) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900495 if (master->block_isbad(master,
496 offs + slave->offset))
497 slave->mtd.ecc_stats.badblocks++;
498 offs += slave->mtd.erasesize;
499 }
500 }
501
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900502out_register:
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900503 if (part->mtdp) {
504 /* store the object pointer (caller may or may not register it*/
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900505 *part->mtdp = &slave->mtd;
506 slave->registered = 0;
507 } else {
508 /* register our partition */
509 add_mtd_device(&slave->mtd);
510 slave->registered = 1;
511 }
512 return slave;
513}
514
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515/*
516 * This function, given a master MTD object and a partition table, creates
517 * and registers slave MTD objects which are bound to the master according to
518 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700519 *
520 * We don't register the master, or expect the caller to have done so,
521 * for reasons of data integrity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522 */
523
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000524int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 const struct mtd_partition *parts,
526 int nbparts)
527{
528 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000529 uint64_t cur_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 int i;
531
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900532 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533
534 for (i = 0; i < nbparts; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900535 slave = add_one_partition(master, parts + i, i, cur_offset);
536 if (!slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700538 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 }
540
541 return 0;
542}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543EXPORT_SYMBOL(add_mtd_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544
545static DEFINE_SPINLOCK(part_parser_lock);
546static LIST_HEAD(part_parsers);
547
548static struct mtd_part_parser *get_partition_parser(const char *name)
549{
Chris Malley71a928c2008-05-19 20:11:50 +0100550 struct mtd_part_parser *p, *ret = NULL;
551
Linus Torvalds1da177e2005-04-16 15:20:36 -0700552 spin_lock(&part_parser_lock);
553
Chris Malley71a928c2008-05-19 20:11:50 +0100554 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
556 ret = p;
557 break;
558 }
Chris Malley71a928c2008-05-19 20:11:50 +0100559
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560 spin_unlock(&part_parser_lock);
561
562 return ret;
563}
564
565int register_mtd_parser(struct mtd_part_parser *p)
566{
567 spin_lock(&part_parser_lock);
568 list_add(&p->list, &part_parsers);
569 spin_unlock(&part_parser_lock);
570
571 return 0;
572}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900573EXPORT_SYMBOL_GPL(register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574
575int deregister_mtd_parser(struct mtd_part_parser *p)
576{
577 spin_lock(&part_parser_lock);
578 list_del(&p->list);
579 spin_unlock(&part_parser_lock);
580 return 0;
581}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900582EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000584int parse_mtd_partitions(struct mtd_info *master, const char **types,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 struct mtd_partition **pparts, unsigned long origin)
586{
587 struct mtd_part_parser *parser;
588 int ret = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000589
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 for ( ; ret <= 0 && *types; types++) {
591 parser = get_partition_parser(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 if (!parser && !request_module("%s", *types))
593 parser = get_partition_parser(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 if (!parser) {
595 printk(KERN_NOTICE "%s partition parsing not available\n",
596 *types);
597 continue;
598 }
599 ret = (*parser->parse_fn)(master, pparts, origin);
600 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000601 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 ret, parser->name, master->name);
603 }
604 put_partition_parser(parser);
605 }
606 return ret;
607}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608EXPORT_SYMBOL_GPL(parse_mtd_partitions);