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/config.h>
20 #include <linux/kmod.h>
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/partitions.h>
23 #include <linux/mtd/compatmac.h>
25 /* Our partition linked list */
26 static LIST_HEAD(mtd_partitions);
28 /* Our partition node structure */
31 struct mtd_info *master;
34 struct list_head list;
39 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
40 * the pointer to that structure with this macro.
42 #define PART(x) ((struct mtd_part *)(x))
46 * MTD methods which simply translate the effective address and pass through
47 * to the _real_ device.
50 static int part_read (struct mtd_info *mtd, loff_t from, size_t len,
51 size_t *retlen, u_char *buf)
53 struct mtd_part *part = PART(mtd);
56 if (from >= mtd->size)
58 else if (from + len > mtd->size)
59 len = mtd->size - from;
60 res = part->master->read (part->master, from + part->offset,
64 mtd->ecc_stats.corrected++;
66 mtd->ecc_stats.failed++;
71 static int part_point (struct mtd_info *mtd, loff_t from, size_t len,
72 size_t *retlen, u_char **buf)
74 struct mtd_part *part = PART(mtd);
75 if (from >= mtd->size)
77 else if (from + len > mtd->size)
78 len = mtd->size - from;
79 return part->master->point (part->master, from + part->offset,
83 static void part_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
85 struct mtd_part *part = PART(mtd);
87 part->master->unpoint (part->master, addr, from + part->offset, len);
90 static int part_read_oob(struct mtd_info *mtd, loff_t from,
91 struct mtd_oob_ops *ops)
93 struct mtd_part *part = PART(mtd);
96 if (from >= mtd->size)
98 if (from + ops->len > mtd->size)
100 res = part->master->read_oob(part->master, from + part->offset, ops);
104 mtd->ecc_stats.corrected++;
106 mtd->ecc_stats.failed++;
111 static int part_read_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
112 size_t *retlen, u_char *buf)
114 struct mtd_part *part = PART(mtd);
115 return part->master->read_user_prot_reg (part->master, from,
119 static int part_get_user_prot_info (struct mtd_info *mtd,
120 struct otp_info *buf, size_t len)
122 struct mtd_part *part = PART(mtd);
123 return part->master->get_user_prot_info (part->master, buf, len);
126 static int part_read_fact_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
127 size_t *retlen, u_char *buf)
129 struct mtd_part *part = PART(mtd);
130 return part->master->read_fact_prot_reg (part->master, from,
134 static int part_get_fact_prot_info (struct mtd_info *mtd,
135 struct otp_info *buf, size_t len)
137 struct mtd_part *part = PART(mtd);
138 return part->master->get_fact_prot_info (part->master, buf, len);
141 static int part_write (struct mtd_info *mtd, loff_t to, size_t len,
142 size_t *retlen, const u_char *buf)
144 struct mtd_part *part = PART(mtd);
145 if (!(mtd->flags & MTD_WRITEABLE))
149 else if (to + len > mtd->size)
150 len = mtd->size - to;
151 return part->master->write (part->master, to + part->offset,
155 static int part_write_oob(struct mtd_info *mtd, loff_t to,
156 struct mtd_oob_ops *ops)
158 struct mtd_part *part = PART(mtd);
160 if (!(mtd->flags & MTD_WRITEABLE))
165 if (to + ops->len > mtd->size)
167 return part->master->write_oob(part->master, to + part->offset, ops);
170 static int part_write_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
171 size_t *retlen, u_char *buf)
173 struct mtd_part *part = PART(mtd);
174 return part->master->write_user_prot_reg (part->master, from,
178 static int part_lock_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len)
180 struct mtd_part *part = PART(mtd);
181 return part->master->lock_user_prot_reg (part->master, from, len);
184 static int part_writev (struct mtd_info *mtd, const struct kvec *vecs,
185 unsigned long count, loff_t to, size_t *retlen)
187 struct mtd_part *part = PART(mtd);
188 if (!(mtd->flags & MTD_WRITEABLE))
190 return part->master->writev (part->master, vecs, count,
191 to + part->offset, retlen);
194 static int part_erase (struct mtd_info *mtd, struct erase_info *instr)
196 struct mtd_part *part = PART(mtd);
198 if (!(mtd->flags & MTD_WRITEABLE))
200 if (instr->addr >= mtd->size)
202 instr->addr += part->offset;
203 ret = part->master->erase(part->master, instr);
207 void mtd_erase_callback(struct erase_info *instr)
209 if (instr->mtd->erase == part_erase) {
210 struct mtd_part *part = PART(instr->mtd);
212 if (instr->fail_addr != 0xffffffff)
213 instr->fail_addr -= part->offset;
214 instr->addr -= part->offset;
217 instr->callback(instr);
219 EXPORT_SYMBOL_GPL(mtd_erase_callback);
221 static int part_lock (struct mtd_info *mtd, loff_t ofs, size_t len)
223 struct mtd_part *part = PART(mtd);
224 if ((len + ofs) > mtd->size)
226 return part->master->lock(part->master, ofs + part->offset, len);
229 static int part_unlock (struct mtd_info *mtd, loff_t ofs, size_t len)
231 struct mtd_part *part = PART(mtd);
232 if ((len + ofs) > mtd->size)
234 return part->master->unlock(part->master, ofs + part->offset, len);
237 static void part_sync(struct mtd_info *mtd)
239 struct mtd_part *part = PART(mtd);
240 part->master->sync(part->master);
243 static int part_suspend(struct mtd_info *mtd)
245 struct mtd_part *part = PART(mtd);
246 return part->master->suspend(part->master);
249 static void part_resume(struct mtd_info *mtd)
251 struct mtd_part *part = PART(mtd);
252 part->master->resume(part->master);
255 static int part_block_isbad (struct mtd_info *mtd, loff_t ofs)
257 struct mtd_part *part = PART(mtd);
258 if (ofs >= mtd->size)
261 return part->master->block_isbad(part->master, ofs);
264 static int part_block_markbad (struct mtd_info *mtd, loff_t ofs)
266 struct mtd_part *part = PART(mtd);
269 if (!(mtd->flags & MTD_WRITEABLE))
271 if (ofs >= mtd->size)
274 res = part->master->block_markbad(part->master, ofs);
276 mtd->ecc_stats.badblocks++;
281 * This function unregisters and destroy all slave MTD objects which are
282 * attached to the given master MTD object.
285 int del_mtd_partitions(struct mtd_info *master)
287 struct list_head *node;
288 struct mtd_part *slave;
290 for (node = mtd_partitions.next;
291 node != &mtd_partitions;
293 slave = list_entry(node, struct mtd_part, list);
294 if (slave->master == master) {
295 struct list_head *prev = node->prev;
296 __list_del(prev, node->next);
297 if(slave->registered)
298 del_mtd_device(&slave->mtd);
308 * This function, given a master MTD object and a partition table, creates
309 * and registers slave MTD objects which are bound to the master according to
310 * the partition definitions.
311 * (Q: should we register the master MTD object as well?)
314 int add_mtd_partitions(struct mtd_info *master,
315 const struct mtd_partition *parts,
318 struct mtd_part *slave;
319 u_int32_t cur_offset = 0;
322 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
324 for (i = 0; i < nbparts; i++) {
326 /* allocate the partition structure */
327 slave = kmalloc (sizeof(*slave), GFP_KERNEL);
329 printk ("memory allocation error while creating partitions for \"%s\"\n",
331 del_mtd_partitions(master);
334 memset(slave, 0, sizeof(*slave));
335 list_add(&slave->list, &mtd_partitions);
337 /* set up the MTD object for this partition */
338 slave->mtd.type = master->type;
339 slave->mtd.flags = master->flags & ~parts[i].mask_flags;
340 slave->mtd.size = parts[i].size;
341 slave->mtd.writesize = master->writesize;
342 slave->mtd.oobsize = master->oobsize;
343 slave->mtd.ecctype = master->ecctype;
344 slave->mtd.eccsize = master->eccsize;
346 slave->mtd.name = parts[i].name;
347 slave->mtd.bank_size = master->bank_size;
348 slave->mtd.owner = master->owner;
350 slave->mtd.read = part_read;
351 slave->mtd.write = part_write;
353 if(master->point && master->unpoint){
354 slave->mtd.point = part_point;
355 slave->mtd.unpoint = part_unpoint;
358 if (master->read_oob)
359 slave->mtd.read_oob = part_read_oob;
360 if (master->write_oob)
361 slave->mtd.write_oob = part_write_oob;
362 if(master->read_user_prot_reg)
363 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
364 if(master->read_fact_prot_reg)
365 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
366 if(master->write_user_prot_reg)
367 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
368 if(master->lock_user_prot_reg)
369 slave->mtd.lock_user_prot_reg = part_lock_user_prot_reg;
370 if(master->get_user_prot_info)
371 slave->mtd.get_user_prot_info = part_get_user_prot_info;
372 if(master->get_fact_prot_info)
373 slave->mtd.get_fact_prot_info = part_get_fact_prot_info;
375 slave->mtd.sync = part_sync;
376 if (!i && master->suspend && master->resume) {
377 slave->mtd.suspend = part_suspend;
378 slave->mtd.resume = part_resume;
381 slave->mtd.writev = part_writev;
383 slave->mtd.lock = part_lock;
385 slave->mtd.unlock = part_unlock;
386 if (master->block_isbad)
387 slave->mtd.block_isbad = part_block_isbad;
388 if (master->block_markbad)
389 slave->mtd.block_markbad = part_block_markbad;
390 slave->mtd.erase = part_erase;
391 slave->master = master;
392 slave->offset = parts[i].offset;
395 if (slave->offset == MTDPART_OFS_APPEND)
396 slave->offset = cur_offset;
397 if (slave->offset == MTDPART_OFS_NXTBLK) {
398 slave->offset = cur_offset;
399 if ((cur_offset % master->erasesize) != 0) {
400 /* Round up to next erasesize */
401 slave->offset = ((cur_offset / master->erasesize) + 1) * master->erasesize;
402 printk(KERN_NOTICE "Moving partition %d: "
403 "0x%08x -> 0x%08x\n", i,
404 cur_offset, slave->offset);
407 if (slave->mtd.size == MTDPART_SIZ_FULL)
408 slave->mtd.size = master->size - slave->offset;
409 cur_offset = slave->offset + slave->mtd.size;
411 printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
412 slave->offset + slave->mtd.size, slave->mtd.name);
414 /* let's do some sanity checks */
415 if (slave->offset >= master->size) {
416 /* let's register it anyway to preserve ordering */
419 printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
422 if (slave->offset + slave->mtd.size > master->size) {
423 slave->mtd.size = master->size - slave->offset;
424 printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
425 parts[i].name, master->name, slave->mtd.size);
427 if (master->numeraseregions>1) {
428 /* Deal with variable erase size stuff */
430 struct mtd_erase_region_info *regions = master->eraseregions;
432 /* Find the first erase regions which is part of this partition. */
433 for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
436 for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
437 if (slave->mtd.erasesize < regions[i].erasesize) {
438 slave->mtd.erasesize = regions[i].erasesize;
442 /* Single erase size */
443 slave->mtd.erasesize = master->erasesize;
446 if ((slave->mtd.flags & MTD_WRITEABLE) &&
447 (slave->offset % slave->mtd.erasesize)) {
448 /* Doesn't start on a boundary of major erase size */
449 /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
450 slave->mtd.flags &= ~MTD_WRITEABLE;
451 printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
454 if ((slave->mtd.flags & MTD_WRITEABLE) &&
455 (slave->mtd.size % slave->mtd.erasesize)) {
456 slave->mtd.flags &= ~MTD_WRITEABLE;
457 printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
461 slave->mtd.ecclayout = master->ecclayout;
462 if (master->block_isbad) {
465 while(offs < slave->mtd.size) {
466 if (master->block_isbad(master,
467 offs + slave->offset))
468 slave->mtd.ecc_stats.badblocks++;
469 offs += slave->mtd.erasesize;
474 { /* store the object pointer (caller may or may not register it */
475 *parts[i].mtdp = &slave->mtd;
476 slave->registered = 0;
480 /* register our partition */
481 add_mtd_device(&slave->mtd);
482 slave->registered = 1;
489 EXPORT_SYMBOL(add_mtd_partitions);
490 EXPORT_SYMBOL(del_mtd_partitions);
492 static DEFINE_SPINLOCK(part_parser_lock);
493 static LIST_HEAD(part_parsers);
495 static struct mtd_part_parser *get_partition_parser(const char *name)
497 struct list_head *this;
499 spin_lock(&part_parser_lock);
501 list_for_each(this, &part_parsers) {
502 struct mtd_part_parser *p = list_entry(this, struct mtd_part_parser, list);
504 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
509 spin_unlock(&part_parser_lock);
514 int register_mtd_parser(struct mtd_part_parser *p)
516 spin_lock(&part_parser_lock);
517 list_add(&p->list, &part_parsers);
518 spin_unlock(&part_parser_lock);
523 int deregister_mtd_parser(struct mtd_part_parser *p)
525 spin_lock(&part_parser_lock);
527 spin_unlock(&part_parser_lock);
531 int parse_mtd_partitions(struct mtd_info *master, const char **types,
532 struct mtd_partition **pparts, unsigned long origin)
534 struct mtd_part_parser *parser;
537 for ( ; ret <= 0 && *types; types++) {
538 parser = get_partition_parser(*types);
540 if (!parser && !request_module("%s", *types))
541 parser = get_partition_parser(*types);
544 printk(KERN_NOTICE "%s partition parsing not available\n",
548 ret = (*parser->parse_fn)(master, pparts, origin);
550 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
551 ret, parser->name, master->name);
553 put_partition_parser(parser);
558 EXPORT_SYMBOL_GPL(parse_mtd_partitions);
559 EXPORT_SYMBOL_GPL(register_mtd_parser);
560 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
562 MODULE_LICENSE("GPL");
563 MODULE_AUTHOR("Nicolas Pitre <nico@cam.org>");
564 MODULE_DESCRIPTION("Generic support for partitioning of MTD devices");