blob: 349fcbe5cc0ff2718c7372bf3263b17a7af3c8d3 [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 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070031};
32
33/*
34 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
35 * the pointer to that structure with this macro.
36 */
37#define PART(x) ((struct mtd_part *)(x))
38
Thomas Gleixner97894cd2005-11-07 11:15:26 +000039
40/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070041 * MTD methods which simply translate the effective address and pass through
42 * to the _real_ device.
43 */
44
Atsushi Nemotob33a2882008-07-19 01:00:33 +090045static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
46 size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070047{
48 struct mtd_part *part = PART(mtd);
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020049 struct mtd_ecc_stats stats;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020050 int res;
51
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020052 stats = part->master->ecc_stats;
53
Linus Torvalds1da177e2005-04-16 15:20:36 -070054 if (from >= mtd->size)
55 len = 0;
56 else if (from + len > mtd->size)
57 len = mtd->size - from;
Atsushi Nemotob33a2882008-07-19 01:00:33 +090058 res = part->master->read(part->master, from + part->offset,
Thomas Gleixner9223a452006-05-23 17:21:03 +020059 len, retlen, buf);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020060 if (unlikely(res)) {
61 if (res == -EUCLEAN)
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020062 mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020063 if (res == -EBADMSG)
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020064 mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020065 }
66 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070067}
68
Atsushi Nemotob33a2882008-07-19 01:00:33 +090069static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
70 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
72 struct mtd_part *part = PART(mtd);
73 if (from >= mtd->size)
74 len = 0;
75 else if (from + len > mtd->size)
76 len = mtd->size - from;
Thomas Gleixner97894cd2005-11-07 11:15:26 +000077 return part->master->point (part->master, from + part->offset,
Jared Hulberta98889f2008-04-29 23:26:49 -070078 len, retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070079}
Thomas Gleixner9223a452006-05-23 17:21:03 +020080
Jared Hulberta98889f2008-04-29 23:26:49 -070081static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070082{
83 struct mtd_part *part = PART(mtd);
84
Jared Hulberta98889f2008-04-29 23:26:49 -070085 part->master->unpoint(part->master, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
David Howells402d3262009-02-12 10:40:00 +000088static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
89 unsigned long len,
90 unsigned long offset,
91 unsigned long flags)
92{
93 struct mtd_part *part = PART(mtd);
94
95 offset += part->offset;
96 return part->master->get_unmapped_area(part->master, len, offset,
97 flags);
98}
99
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200100static int part_read_oob(struct mtd_info *mtd, loff_t from,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900101 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102{
103 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200104 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200105
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (from >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200107 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300108 if (ops->datbuf && from + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200109 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200110 res = part->master->read_oob(part->master, from + part->offset, ops);
111
112 if (unlikely(res)) {
113 if (res == -EUCLEAN)
114 mtd->ecc_stats.corrected++;
115 if (res == -EBADMSG)
116 mtd->ecc_stats.failed++;
117 }
118 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119}
120
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900121static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
122 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
124 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900125 return part->master->read_user_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126 len, retlen, buf);
127}
128
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900129static int part_get_user_prot_info(struct mtd_info *mtd,
130 struct otp_info *buf, size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000131{
132 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900133 return part->master->get_user_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000134}
135
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900136static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
137 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
139 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900140 return part->master->read_fact_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 len, retlen, buf);
142}
143
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900144static int part_get_fact_prot_info(struct mtd_info *mtd, struct otp_info *buf,
145 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000146{
147 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900148 return part->master->get_fact_prot_info(part->master, buf, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000149}
150
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900151static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
152 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
154 struct mtd_part *part = PART(mtd);
155 if (!(mtd->flags & MTD_WRITEABLE))
156 return -EROFS;
157 if (to >= mtd->size)
158 len = 0;
159 else if (to + len > mtd->size)
160 len = mtd->size - to;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900161 return part->master->write(part->master, to + part->offset,
Thomas Gleixner9223a452006-05-23 17:21:03 +0200162 len, retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163}
164
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900165static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
166 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000167{
168 struct mtd_part *part = PART(mtd);
169 if (!(mtd->flags & MTD_WRITEABLE))
170 return -EROFS;
171 if (to >= mtd->size)
172 len = 0;
173 else if (to + len > mtd->size)
174 len = mtd->size - to;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900175 return part->master->panic_write(part->master, to + part->offset,
Richard Purdie388bbb02008-02-06 10:17:15 +0000176 len, retlen, buf);
177}
178
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200179static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900180 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181{
182 struct mtd_part *part = PART(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200183
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 if (!(mtd->flags & MTD_WRITEABLE))
185 return -EROFS;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200186
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200188 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300189 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200190 return -EINVAL;
191 return part->master->write_oob(part->master, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900194static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
195 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
197 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900198 return part->master->write_user_prot_reg(part->master, from,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 len, retlen, buf);
200}
201
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900202static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
203 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000204{
205 struct mtd_part *part = PART(mtd);
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900206 return part->master->lock_user_prot_reg(part->master, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000207}
208
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900209static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
210 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211{
212 struct mtd_part *part = PART(mtd);
213 if (!(mtd->flags & MTD_WRITEABLE))
214 return -EROFS;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900215 return part->master->writev(part->master, vecs, count,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900219static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220{
221 struct mtd_part *part = PART(mtd);
222 int ret;
223 if (!(mtd->flags & MTD_WRITEABLE))
224 return -EROFS;
225 if (instr->addr >= mtd->size)
226 return -EINVAL;
227 instr->addr += part->offset;
228 ret = part->master->erase(part->master, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200229 if (ret) {
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300230 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Adrian Hunter74641d72007-03-08 12:20:12 +0200231 instr->fail_addr -= part->offset;
232 instr->addr -= part->offset;
233 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 return ret;
235}
236
237void mtd_erase_callback(struct erase_info *instr)
238{
239 if (instr->mtd->erase == part_erase) {
240 struct mtd_part *part = PART(instr->mtd);
241
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300242 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 instr->fail_addr -= part->offset;
244 instr->addr -= part->offset;
245 }
246 if (instr->callback)
247 instr->callback(instr);
248}
249EXPORT_SYMBOL_GPL(mtd_erase_callback);
250
Adrian Hunter69423d92008-12-10 13:37:21 +0000251static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252{
253 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000254 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 return -EINVAL;
256 return part->master->lock(part->master, ofs + part->offset, len);
257}
258
Adrian Hunter69423d92008-12-10 13:37:21 +0000259static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260{
261 struct mtd_part *part = PART(mtd);
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000262 if ((len + ofs) > mtd->size)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 return -EINVAL;
264 return part->master->unlock(part->master, ofs + part->offset, len);
265}
266
267static void part_sync(struct mtd_info *mtd)
268{
269 struct mtd_part *part = PART(mtd);
270 part->master->sync(part->master);
271}
272
273static int part_suspend(struct mtd_info *mtd)
274{
275 struct mtd_part *part = PART(mtd);
276 return part->master->suspend(part->master);
277}
278
279static void part_resume(struct mtd_info *mtd)
280{
281 struct mtd_part *part = PART(mtd);
282 part->master->resume(part->master);
283}
284
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900285static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286{
287 struct mtd_part *part = PART(mtd);
288 if (ofs >= mtd->size)
289 return -EINVAL;
290 ofs += part->offset;
291 return part->master->block_isbad(part->master, ofs);
292}
293
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900294static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295{
296 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200297 int res;
298
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 if (!(mtd->flags & MTD_WRITEABLE))
300 return -EROFS;
301 if (ofs >= mtd->size)
302 return -EINVAL;
303 ofs += part->offset;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200304 res = part->master->block_markbad(part->master, ofs);
305 if (!res)
306 mtd->ecc_stats.badblocks++;
307 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308}
309
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000310/*
311 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 * attached to the given master MTD object.
313 */
314
315int del_mtd_partitions(struct mtd_info *master)
316{
Chris Malley71a928c2008-05-19 20:11:50 +0100317 struct mtd_part *slave, *next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Chris Malley71a928c2008-05-19 20:11:50 +0100319 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 if (slave->master == master) {
Chris Malley71a928c2008-05-19 20:11:50 +0100321 list_del(&slave->list);
David Woodhouseb90cf662009-04-05 08:23:44 -0700322 del_mtd_device(&slave->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 kfree(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700324 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325
326 return 0;
327}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900328EXPORT_SYMBOL(del_mtd_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900330static struct mtd_part *add_one_partition(struct mtd_info *master,
331 const struct mtd_partition *part, int partno,
Adrian Hunter69423d92008-12-10 13:37:21 +0000332 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900333{
334 struct mtd_part *slave;
335
336 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900337 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900338 if (!slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900339 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900340 master->name);
341 del_mtd_partitions(master);
342 return NULL;
343 }
344 list_add(&slave->list, &mtd_partitions);
345
346 /* set up the MTD object for this partition */
347 slave->mtd.type = master->type;
348 slave->mtd.flags = master->flags & ~part->mask_flags;
349 slave->mtd.size = part->size;
350 slave->mtd.writesize = master->writesize;
351 slave->mtd.oobsize = master->oobsize;
352 slave->mtd.oobavail = master->oobavail;
353 slave->mtd.subpage_sft = master->subpage_sft;
354
355 slave->mtd.name = part->name;
356 slave->mtd.owner = master->owner;
David Howells402d3262009-02-12 10:40:00 +0000357 slave->mtd.backing_dev_info = master->backing_dev_info;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900358
David Brownell1f24b5a2009-03-26 00:42:41 -0700359 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
360 * to have the same data be in two different partitions.
361 */
362 slave->mtd.dev.parent = master->dev.parent;
363
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900364 slave->mtd.read = part_read;
365 slave->mtd.write = part_write;
366
367 if (master->panic_write)
368 slave->mtd.panic_write = part_panic_write;
369
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900370 if (master->point && master->unpoint) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900371 slave->mtd.point = part_point;
372 slave->mtd.unpoint = part_unpoint;
373 }
374
David Howells402d3262009-02-12 10:40:00 +0000375 if (master->get_unmapped_area)
376 slave->mtd.get_unmapped_area = part_get_unmapped_area;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900377 if (master->read_oob)
378 slave->mtd.read_oob = part_read_oob;
379 if (master->write_oob)
380 slave->mtd.write_oob = part_write_oob;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900381 if (master->read_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900382 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900383 if (master->read_fact_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900384 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900385 if (master->write_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900386 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900387 if (master->lock_user_prot_reg)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900388 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900389 if (master->get_user_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900390 slave->mtd.get_user_prot_info = part_get_user_prot_info;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900391 if (master->get_fact_prot_info)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900392 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
393 if (master->sync)
394 slave->mtd.sync = part_sync;
David Woodhouse4704a782009-04-05 07:56:23 -0700395 if (!partno && !master->dev.class && master->suspend && master->resume) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900396 slave->mtd.suspend = part_suspend;
397 slave->mtd.resume = part_resume;
398 }
399 if (master->writev)
400 slave->mtd.writev = part_writev;
401 if (master->lock)
402 slave->mtd.lock = part_lock;
403 if (master->unlock)
404 slave->mtd.unlock = part_unlock;
405 if (master->block_isbad)
406 slave->mtd.block_isbad = part_block_isbad;
407 if (master->block_markbad)
408 slave->mtd.block_markbad = part_block_markbad;
409 slave->mtd.erase = part_erase;
410 slave->master = master;
411 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900412
413 if (slave->offset == MTDPART_OFS_APPEND)
414 slave->offset = cur_offset;
415 if (slave->offset == MTDPART_OFS_NXTBLK) {
416 slave->offset = cur_offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000417 if (mtd_mod_by_eb(cur_offset, master) != 0) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900418 /* Round up to next erasesize */
Adrian Hunter69423d92008-12-10 13:37:21 +0000419 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900420 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000421 "0x%012llx -> 0x%012llx\n", partno,
422 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900423 }
424 }
425 if (slave->mtd.size == MTDPART_SIZ_FULL)
426 slave->mtd.size = master->size - slave->offset;
427
Adrian Hunter69423d92008-12-10 13:37:21 +0000428 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
429 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900430
431 /* let's do some sanity checks */
432 if (slave->offset >= master->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900433 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900434 slave->offset = 0;
435 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900436 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900437 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900438 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900439 }
440 if (slave->offset + slave->mtd.size > master->size) {
441 slave->mtd.size = master->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000442 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
443 part->name, master->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900444 }
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900445 if (master->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900446 /* Deal with variable erase size stuff */
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900447 int i, max = master->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000448 u64 end = slave->offset + slave->mtd.size;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900449 struct mtd_erase_region_info *regions = master->eraseregions;
450
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900451 /* Find the first erase regions which is part of this
452 * partition. */
453 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900454 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900455 /* The loop searched for the region _behind_ the first one */
456 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900457
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900458 /* Pick biggest erasesize */
459 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900460 if (slave->mtd.erasesize < regions[i].erasesize) {
461 slave->mtd.erasesize = regions[i].erasesize;
462 }
463 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900464 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900465 } else {
466 /* Single erase size */
467 slave->mtd.erasesize = master->erasesize;
468 }
469
470 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000471 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900472 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900473 /* FIXME: Let it be writable if it is on a boundary of
474 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900475 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900476 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 +0900477 part->name);
478 }
479 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000480 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900481 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900482 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900483 part->name);
484 }
485
486 slave->mtd.ecclayout = master->ecclayout;
487 if (master->block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000488 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900489
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900490 while (offs < slave->mtd.size) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900491 if (master->block_isbad(master,
492 offs + slave->offset))
493 slave->mtd.ecc_stats.badblocks++;
494 offs += slave->mtd.erasesize;
495 }
496 }
497
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900498out_register:
David Woodhouseb90cf662009-04-05 08:23:44 -0700499 /* register our partition */
500 add_mtd_device(&slave->mtd);
501
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900502 return slave;
503}
504
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505/*
506 * This function, given a master MTD object and a partition table, creates
507 * and registers slave MTD objects which are bound to the master according to
508 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700509 *
510 * We don't register the master, or expect the caller to have done so,
511 * for reasons of data integrity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 */
513
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000514int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 const struct mtd_partition *parts,
516 int nbparts)
517{
518 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000519 uint64_t cur_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 int i;
521
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900522 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523
524 for (i = 0; i < nbparts; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900525 slave = add_one_partition(master, parts + i, i, cur_offset);
526 if (!slave)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 return -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 }
530
531 return 0;
532}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533EXPORT_SYMBOL(add_mtd_partitions);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534
535static DEFINE_SPINLOCK(part_parser_lock);
536static LIST_HEAD(part_parsers);
537
538static struct mtd_part_parser *get_partition_parser(const char *name)
539{
Chris Malley71a928c2008-05-19 20:11:50 +0100540 struct mtd_part_parser *p, *ret = NULL;
541
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 spin_lock(&part_parser_lock);
543
Chris Malley71a928c2008-05-19 20:11:50 +0100544 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
546 ret = p;
547 break;
548 }
Chris Malley71a928c2008-05-19 20:11:50 +0100549
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 spin_unlock(&part_parser_lock);
551
552 return ret;
553}
554
555int register_mtd_parser(struct mtd_part_parser *p)
556{
557 spin_lock(&part_parser_lock);
558 list_add(&p->list, &part_parsers);
559 spin_unlock(&part_parser_lock);
560
561 return 0;
562}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900563EXPORT_SYMBOL_GPL(register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564
565int deregister_mtd_parser(struct mtd_part_parser *p)
566{
567 spin_lock(&part_parser_lock);
568 list_del(&p->list);
569 spin_unlock(&part_parser_lock);
570 return 0;
571}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900572EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000574int parse_mtd_partitions(struct mtd_info *master, const char **types,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 struct mtd_partition **pparts, unsigned long origin)
576{
577 struct mtd_part_parser *parser;
578 int ret = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000579
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 for ( ; ret <= 0 && *types; types++) {
581 parser = get_partition_parser(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 if (!parser && !request_module("%s", *types))
583 parser = get_partition_parser(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 if (!parser) {
585 printk(KERN_NOTICE "%s partition parsing not available\n",
586 *types);
587 continue;
588 }
589 ret = (*parser->parse_fn)(master, pparts, origin);
590 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000591 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 ret, parser->name, master->name);
593 }
594 put_partition_parser(parser);
595 }
596 return ret;
597}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598EXPORT_SYMBOL_GPL(parse_mtd_partitions);