2 * Simple MTD partitioning layer
4 * (C) 2000 Nicolas Pitre <nico@cam.org>
8 * $Id: mtdpart.c,v 1.55 2005/11/07 11:14:20 gleixner Exp $
10 * 02-21-2002 Thomas Gleixner <gleixner@autronix.de>
11 * added support for read_oob, write_oob
14 #include <linux/module.h>
15 #include <linux/types.h>
16 #include <linux/kernel.h>
17 #include <linux/slab.h>
18 #include <linux/list.h>
19 #include <linux/kmod.h>
20 #include <linux/mtd/mtd.h>
21 #include <linux/mtd/partitions.h>
22 #include <linux/mtd/compatmac.h>
24 /* Our partition linked list */
25 static LIST_HEAD(mtd_partitions);
27 /* Our partition node structure */
30 struct mtd_info *master;
33 struct list_head list;
38 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
39 * the pointer to that structure with this macro.
41 #define PART(x) ((struct mtd_part *)(x))
45 * MTD methods which simply translate the effective address and pass through
46 * to the _real_ device.
49 static int part_read (struct mtd_info *mtd, loff_t from, size_t len,
50 size_t *retlen, u_char *buf)
52 struct mtd_part *part = PART(mtd);
55 if (from >= mtd->size)
57 else if (from + len > mtd->size)
58 len = mtd->size - from;
59 res = part->master->read (part->master, from + part->offset,
63 mtd->ecc_stats.corrected++;
65 mtd->ecc_stats.failed++;
70 static int part_point (struct mtd_info *mtd, loff_t from, size_t len,
71 size_t *retlen, void **virt, resource_size_t *phys)
73 struct mtd_part *part = PART(mtd);
74 if (from >= mtd->size)
76 else if (from + len > mtd->size)
77 len = mtd->size - from;
78 return part->master->point (part->master, from + part->offset,
79 len, retlen, virt, phys);
82 static void part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
84 struct mtd_part *part = PART(mtd);
86 part->master->unpoint(part->master, from + part->offset, len);
89 static int part_read_oob(struct mtd_info *mtd, loff_t from,
90 struct mtd_oob_ops *ops)
92 struct mtd_part *part = PART(mtd);
95 if (from >= mtd->size)
97 if (ops->datbuf && from + ops->len > mtd->size)
99 res = part->master->read_oob(part->master, from + part->offset, ops);
103 mtd->ecc_stats.corrected++;
105 mtd->ecc_stats.failed++;
110 static int part_read_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
111 size_t *retlen, u_char *buf)
113 struct mtd_part *part = PART(mtd);
114 return part->master->read_user_prot_reg (part->master, from,
118 static int part_get_user_prot_info (struct mtd_info *mtd,
119 struct otp_info *buf, size_t len)
121 struct mtd_part *part = PART(mtd);
122 return part->master->get_user_prot_info (part->master, buf, len);
125 static int part_read_fact_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
126 size_t *retlen, u_char *buf)
128 struct mtd_part *part = PART(mtd);
129 return part->master->read_fact_prot_reg (part->master, from,
133 static int part_get_fact_prot_info (struct mtd_info *mtd,
134 struct otp_info *buf, size_t len)
136 struct mtd_part *part = PART(mtd);
137 return part->master->get_fact_prot_info (part->master, buf, len);
140 static int part_write (struct mtd_info *mtd, loff_t to, size_t len,
141 size_t *retlen, const u_char *buf)
143 struct mtd_part *part = PART(mtd);
144 if (!(mtd->flags & MTD_WRITEABLE))
148 else if (to + len > mtd->size)
149 len = mtd->size - to;
150 return part->master->write (part->master, to + part->offset,
154 static int part_panic_write (struct mtd_info *mtd, loff_t to, size_t len,
155 size_t *retlen, const u_char *buf)
157 struct mtd_part *part = PART(mtd);
158 if (!(mtd->flags & MTD_WRITEABLE))
162 else if (to + len > mtd->size)
163 len = mtd->size - to;
164 return part->master->panic_write (part->master, to + part->offset,
168 static int part_write_oob(struct mtd_info *mtd, loff_t to,
169 struct mtd_oob_ops *ops)
171 struct mtd_part *part = PART(mtd);
173 if (!(mtd->flags & MTD_WRITEABLE))
178 if (ops->datbuf && to + ops->len > mtd->size)
180 return part->master->write_oob(part->master, to + part->offset, ops);
183 static int part_write_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
184 size_t *retlen, u_char *buf)
186 struct mtd_part *part = PART(mtd);
187 return part->master->write_user_prot_reg (part->master, from,
191 static int part_lock_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len)
193 struct mtd_part *part = PART(mtd);
194 return part->master->lock_user_prot_reg (part->master, from, len);
197 static int part_writev (struct mtd_info *mtd, const struct kvec *vecs,
198 unsigned long count, loff_t to, size_t *retlen)
200 struct mtd_part *part = PART(mtd);
201 if (!(mtd->flags & MTD_WRITEABLE))
203 return part->master->writev (part->master, vecs, count,
204 to + part->offset, retlen);
207 static int part_erase (struct mtd_info *mtd, struct erase_info *instr)
209 struct mtd_part *part = PART(mtd);
211 if (!(mtd->flags & MTD_WRITEABLE))
213 if (instr->addr >= mtd->size)
215 instr->addr += part->offset;
216 ret = part->master->erase(part->master, instr);
218 if (instr->fail_addr != 0xffffffff)
219 instr->fail_addr -= part->offset;
220 instr->addr -= part->offset;
225 void mtd_erase_callback(struct erase_info *instr)
227 if (instr->mtd->erase == part_erase) {
228 struct mtd_part *part = PART(instr->mtd);
230 if (instr->fail_addr != 0xffffffff)
231 instr->fail_addr -= part->offset;
232 instr->addr -= part->offset;
235 instr->callback(instr);
237 EXPORT_SYMBOL_GPL(mtd_erase_callback);
239 static int part_lock (struct mtd_info *mtd, loff_t ofs, size_t len)
241 struct mtd_part *part = PART(mtd);
242 if ((len + ofs) > mtd->size)
244 return part->master->lock(part->master, ofs + part->offset, len);
247 static int part_unlock (struct mtd_info *mtd, loff_t ofs, size_t len)
249 struct mtd_part *part = PART(mtd);
250 if ((len + ofs) > mtd->size)
252 return part->master->unlock(part->master, ofs + part->offset, len);
255 static void part_sync(struct mtd_info *mtd)
257 struct mtd_part *part = PART(mtd);
258 part->master->sync(part->master);
261 static int part_suspend(struct mtd_info *mtd)
263 struct mtd_part *part = PART(mtd);
264 return part->master->suspend(part->master);
267 static void part_resume(struct mtd_info *mtd)
269 struct mtd_part *part = PART(mtd);
270 part->master->resume(part->master);
273 static int part_block_isbad (struct mtd_info *mtd, loff_t ofs)
275 struct mtd_part *part = PART(mtd);
276 if (ofs >= mtd->size)
279 return part->master->block_isbad(part->master, ofs);
282 static int part_block_markbad (struct mtd_info *mtd, loff_t ofs)
284 struct mtd_part *part = PART(mtd);
287 if (!(mtd->flags & MTD_WRITEABLE))
289 if (ofs >= mtd->size)
292 res = part->master->block_markbad(part->master, ofs);
294 mtd->ecc_stats.badblocks++;
299 * This function unregisters and destroy all slave MTD objects which are
300 * attached to the given master MTD object.
303 int del_mtd_partitions(struct mtd_info *master)
305 struct list_head *node;
306 struct mtd_part *slave;
308 for (node = mtd_partitions.next;
309 node != &mtd_partitions;
311 slave = list_entry(node, struct mtd_part, list);
312 if (slave->master == master) {
313 struct list_head *prev = node->prev;
314 __list_del(prev, node->next);
315 if(slave->registered)
316 del_mtd_device(&slave->mtd);
326 * This function, given a master MTD object and a partition table, creates
327 * and registers slave MTD objects which are bound to the master according to
328 * the partition definitions.
329 * (Q: should we register the master MTD object as well?)
332 int add_mtd_partitions(struct mtd_info *master,
333 const struct mtd_partition *parts,
336 struct mtd_part *slave;
337 u_int32_t cur_offset = 0;
340 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
342 for (i = 0; i < nbparts; i++) {
344 /* allocate the partition structure */
345 slave = kzalloc (sizeof(*slave), GFP_KERNEL);
347 printk ("memory allocation error while creating partitions for \"%s\"\n",
349 del_mtd_partitions(master);
352 list_add(&slave->list, &mtd_partitions);
354 /* set up the MTD object for this partition */
355 slave->mtd.type = master->type;
356 slave->mtd.flags = master->flags & ~parts[i].mask_flags;
357 slave->mtd.size = parts[i].size;
358 slave->mtd.writesize = master->writesize;
359 slave->mtd.oobsize = master->oobsize;
360 slave->mtd.oobavail = master->oobavail;
361 slave->mtd.subpage_sft = master->subpage_sft;
363 slave->mtd.name = parts[i].name;
364 slave->mtd.owner = master->owner;
366 slave->mtd.read = part_read;
367 slave->mtd.write = part_write;
369 if (master->panic_write)
370 slave->mtd.panic_write = part_panic_write;
372 if(master->point && master->unpoint){
373 slave->mtd.point = part_point;
374 slave->mtd.unpoint = part_unpoint;
377 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;
381 if(master->read_user_prot_reg)
382 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
383 if(master->read_fact_prot_reg)
384 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
385 if(master->write_user_prot_reg)
386 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
387 if(master->lock_user_prot_reg)
388 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
389 if(master->get_user_prot_info)
390 slave->mtd.get_user_prot_info = part_get_user_prot_info;
391 if(master->get_fact_prot_info)
392 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
394 slave->mtd.sync = part_sync;
395 if (!i && master->suspend && master->resume) {
396 slave->mtd.suspend = part_suspend;
397 slave->mtd.resume = part_resume;
400 slave->mtd.writev = part_writev;
402 slave->mtd.lock = part_lock;
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 = parts[i].offset;
414 if (slave->offset == MTDPART_OFS_APPEND)
415 slave->offset = cur_offset;
416 if (slave->offset == MTDPART_OFS_NXTBLK) {
417 slave->offset = cur_offset;
418 if ((cur_offset % master->erasesize) != 0) {
419 /* Round up to next erasesize */
420 slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
421 printk(KERN_NOTICE "Moving partition %d: "
422 "0x%08x -> 0x%08x\n", i,
423 cur_offset, slave->offset);
426 if (slave->mtd.size == MTDPART_SIZ_FULL)
427 slave->mtd.size = master->size - slave->offset;
428 cur_offset = slave->offset + slave->mtd.size;
430 printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
431 slave->offset + slave->mtd.size, slave->mtd.name);
433 /* let's do some sanity checks */
434 if (slave->offset >= master->size) {
435 /* let's register it anyway to preserve ordering */
438 printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
441 if (slave->offset + slave->mtd.size > master->size) {
442 slave->mtd.size = master->size - slave->offset;
443 printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
444 parts[i].name, master->name, slave->mtd.size);
446 if (master->numeraseregions>1) {
447 /* Deal with variable erase size stuff */
449 struct mtd_erase_region_info *regions = master->eraseregions;
451 /* Find the first erase regions which is part of this partition. */
452 for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
455 for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
456 if (slave->mtd.erasesize < regions[i].erasesize) {
457 slave->mtd.erasesize = regions[i].erasesize;
461 /* Single erase size */
462 slave->mtd.erasesize = master->erasesize;
465 if ((slave->mtd.flags & MTD_WRITEABLE) &&
466 (slave->offset % slave->mtd.erasesize)) {
467 /* Doesn't start on a boundary of major erase size */
468 /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
469 slave->mtd.flags &= ~MTD_WRITEABLE;
470 printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
473 if ((slave->mtd.flags & MTD_WRITEABLE) &&
474 (slave->mtd.size % slave->mtd.erasesize)) {
475 slave->mtd.flags &= ~MTD_WRITEABLE;
476 printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
480 slave->mtd.ecclayout = master->ecclayout;
481 if (master->block_isbad) {
484 while(offs < slave->mtd.size) {
485 if (master->block_isbad(master,
486 offs + slave->offset))
487 slave->mtd.ecc_stats.badblocks++;
488 offs += slave->mtd.erasesize;
493 { /* store the object pointer (caller may or may not register it */
494 *parts[i].mtdp = &slave->mtd;
495 slave->registered = 0;
499 /* register our partition */
500 add_mtd_device(&slave->mtd);
501 slave->registered = 1;
508 EXPORT_SYMBOL(add_mtd_partitions);
509 EXPORT_SYMBOL(del_mtd_partitions);
511 static DEFINE_SPINLOCK(part_parser_lock);
512 static LIST_HEAD(part_parsers);
514 static struct mtd_part_parser *get_partition_parser(const char *name)
516 struct list_head *this;
518 spin_lock(&part_parser_lock);
520 list_for_each(this, &part_parsers) {
521 struct mtd_part_parser *p = list_entry(this, struct mtd_part_parser, list);
523 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
528 spin_unlock(&part_parser_lock);
533 int register_mtd_parser(struct mtd_part_parser *p)
535 spin_lock(&part_parser_lock);
536 list_add(&p->list, &part_parsers);
537 spin_unlock(&part_parser_lock);
542 int deregister_mtd_parser(struct mtd_part_parser *p)
544 spin_lock(&part_parser_lock);
546 spin_unlock(&part_parser_lock);
550 int parse_mtd_partitions(struct mtd_info *master, const char **types,
551 struct mtd_partition **pparts, unsigned long origin)
553 struct mtd_part_parser *parser;
556 for ( ; ret <= 0 && *types; types++) {
557 parser = get_partition_parser(*types);
559 if (!parser && !request_module("%s", *types))
560 parser = get_partition_parser(*types);
563 printk(KERN_NOTICE "%s partition parsing not available\n",
567 ret = (*parser->parse_fn)(master, pparts, origin);
569 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
570 ret, parser->name, master->name);
572 put_partition_parser(parser);
577 EXPORT_SYMBOL_GPL(parse_mtd_partitions);
578 EXPORT_SYMBOL_GPL(register_mtd_parser);
579 EXPORT_SYMBOL_GPL(deregister_mtd_parser);