2 * Simple MTD partitioning layer
4 * (C) 2000 Nicolas Pitre <nico@cam.org>
8 * $Id: mtdpart.c,v 1.51 2004/11/16 18:28:59 dwmw2 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);
54 if (from >= mtd->size)
56 else if (from + len > mtd->size)
57 len = mtd->size - from;
58 if (part->master->read_ecc == NULL)
59 return part->master->read (part->master, from + part->offset,
62 return part->master->read_ecc (part->master, from + part->offset,
63 len, retlen, buf, NULL, &mtd->oobinfo);
66 static int part_point (struct mtd_info *mtd, loff_t from, size_t len,
67 size_t *retlen, u_char **buf)
69 struct mtd_part *part = PART(mtd);
70 if (from >= mtd->size)
72 else if (from + len > mtd->size)
73 len = mtd->size - from;
74 return part->master->point (part->master, from + part->offset,
77 static void part_unpoint (struct mtd_info *mtd, u_char *addr, loff_t from, size_t len)
79 struct mtd_part *part = PART(mtd);
81 part->master->unpoint (part->master, addr, from + part->offset, len);
85 static int part_read_ecc (struct mtd_info *mtd, loff_t from, size_t len,
86 size_t *retlen, u_char *buf, u_char *eccbuf, struct nand_oobinfo *oobsel)
88 struct mtd_part *part = PART(mtd);
90 oobsel = &mtd->oobinfo;
91 if (from >= mtd->size)
93 else if (from + len > mtd->size)
94 len = mtd->size - from;
95 return part->master->read_ecc (part->master, from + part->offset,
96 len, retlen, buf, eccbuf, oobsel);
99 static int part_read_oob (struct mtd_info *mtd, loff_t from, size_t len,
100 size_t *retlen, u_char *buf)
102 struct mtd_part *part = PART(mtd);
103 if (from >= mtd->size)
105 else if (from + len > mtd->size)
106 len = mtd->size - from;
107 return part->master->read_oob (part->master, from + part->offset,
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_read_fact_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
120 size_t *retlen, u_char *buf)
122 struct mtd_part *part = PART(mtd);
123 return part->master->read_fact_prot_reg (part->master, from,
127 static int part_write (struct mtd_info *mtd, loff_t to, size_t len,
128 size_t *retlen, const u_char *buf)
130 struct mtd_part *part = PART(mtd);
131 if (!(mtd->flags & MTD_WRITEABLE))
135 else if (to + len > mtd->size)
136 len = mtd->size - to;
137 if (part->master->write_ecc == NULL)
138 return part->master->write (part->master, to + part->offset,
141 return part->master->write_ecc (part->master, to + part->offset,
142 len, retlen, buf, NULL, &mtd->oobinfo);
146 static int part_write_ecc (struct mtd_info *mtd, loff_t to, size_t len,
147 size_t *retlen, const u_char *buf,
148 u_char *eccbuf, struct nand_oobinfo *oobsel)
150 struct mtd_part *part = PART(mtd);
151 if (!(mtd->flags & MTD_WRITEABLE))
154 oobsel = &mtd->oobinfo;
157 else if (to + len > mtd->size)
158 len = mtd->size - to;
159 return part->master->write_ecc (part->master, to + part->offset,
160 len, retlen, buf, eccbuf, oobsel);
163 static int part_write_oob (struct mtd_info *mtd, loff_t to, size_t len,
164 size_t *retlen, const u_char *buf)
166 struct mtd_part *part = PART(mtd);
167 if (!(mtd->flags & MTD_WRITEABLE))
171 else if (to + len > mtd->size)
172 len = mtd->size - to;
173 return part->master->write_oob (part->master, to + part->offset,
177 static int part_write_user_prot_reg (struct mtd_info *mtd, loff_t from, size_t len,
178 size_t *retlen, u_char *buf)
180 struct mtd_part *part = PART(mtd);
181 return part->master->write_user_prot_reg (part->master, from,
185 static int part_writev (struct mtd_info *mtd, const struct kvec *vecs,
186 unsigned long count, loff_t to, size_t *retlen)
188 struct mtd_part *part = PART(mtd);
189 if (!(mtd->flags & MTD_WRITEABLE))
191 if (part->master->writev_ecc == NULL)
192 return part->master->writev (part->master, vecs, count,
193 to + part->offset, retlen);
195 return part->master->writev_ecc (part->master, vecs, count,
196 to + part->offset, retlen,
197 NULL, &mtd->oobinfo);
200 static int part_readv (struct mtd_info *mtd, struct kvec *vecs,
201 unsigned long count, loff_t from, size_t *retlen)
203 struct mtd_part *part = PART(mtd);
204 if (part->master->readv_ecc == NULL)
205 return part->master->readv (part->master, vecs, count,
206 from + part->offset, retlen);
208 return part->master->readv_ecc (part->master, vecs, count,
209 from + part->offset, retlen,
210 NULL, &mtd->oobinfo);
213 static int part_writev_ecc (struct mtd_info *mtd, const struct kvec *vecs,
214 unsigned long count, loff_t to, size_t *retlen,
215 u_char *eccbuf, struct nand_oobinfo *oobsel)
217 struct mtd_part *part = PART(mtd);
218 if (!(mtd->flags & MTD_WRITEABLE))
221 oobsel = &mtd->oobinfo;
222 return part->master->writev_ecc (part->master, vecs, count,
223 to + part->offset, retlen,
227 static int part_readv_ecc (struct mtd_info *mtd, struct kvec *vecs,
228 unsigned long count, loff_t from, size_t *retlen,
229 u_char *eccbuf, struct nand_oobinfo *oobsel)
231 struct mtd_part *part = PART(mtd);
233 oobsel = &mtd->oobinfo;
234 return part->master->readv_ecc (part->master, vecs, count,
235 from + part->offset, retlen,
239 static int part_erase (struct mtd_info *mtd, struct erase_info *instr)
241 struct mtd_part *part = PART(mtd);
243 if (!(mtd->flags & MTD_WRITEABLE))
245 if (instr->addr >= mtd->size)
247 instr->addr += part->offset;
248 ret = part->master->erase(part->master, instr);
252 void mtd_erase_callback(struct erase_info *instr)
254 if (instr->mtd->erase == part_erase) {
255 struct mtd_part *part = PART(instr->mtd);
257 if (instr->fail_addr != 0xffffffff)
258 instr->fail_addr -= part->offset;
259 instr->addr -= part->offset;
262 instr->callback(instr);
264 EXPORT_SYMBOL_GPL(mtd_erase_callback);
266 static int part_lock (struct mtd_info *mtd, loff_t ofs, size_t len)
268 struct mtd_part *part = PART(mtd);
269 if ((len + ofs) > mtd->size)
271 return part->master->lock(part->master, ofs + part->offset, len);
274 static int part_unlock (struct mtd_info *mtd, loff_t ofs, size_t len)
276 struct mtd_part *part = PART(mtd);
277 if ((len + ofs) > mtd->size)
279 return part->master->unlock(part->master, ofs + part->offset, len);
282 static void part_sync(struct mtd_info *mtd)
284 struct mtd_part *part = PART(mtd);
285 part->master->sync(part->master);
288 static int part_suspend(struct mtd_info *mtd)
290 struct mtd_part *part = PART(mtd);
291 return part->master->suspend(part->master);
294 static void part_resume(struct mtd_info *mtd)
296 struct mtd_part *part = PART(mtd);
297 part->master->resume(part->master);
300 static int part_block_isbad (struct mtd_info *mtd, loff_t ofs)
302 struct mtd_part *part = PART(mtd);
303 if (ofs >= mtd->size)
306 return part->master->block_isbad(part->master, ofs);
309 static int part_block_markbad (struct mtd_info *mtd, loff_t ofs)
311 struct mtd_part *part = PART(mtd);
312 if (!(mtd->flags & MTD_WRITEABLE))
314 if (ofs >= mtd->size)
317 return part->master->block_markbad(part->master, ofs);
321 * This function unregisters and destroy all slave MTD objects which are
322 * attached to the given master MTD object.
325 int del_mtd_partitions(struct mtd_info *master)
327 struct list_head *node;
328 struct mtd_part *slave;
330 for (node = mtd_partitions.next;
331 node != &mtd_partitions;
333 slave = list_entry(node, struct mtd_part, list);
334 if (slave->master == master) {
335 struct list_head *prev = node->prev;
336 __list_del(prev, node->next);
337 if(slave->registered)
338 del_mtd_device(&slave->mtd);
348 * This function, given a master MTD object and a partition table, creates
349 * and registers slave MTD objects which are bound to the master according to
350 * the partition definitions.
351 * (Q: should we register the master MTD object as well?)
354 int add_mtd_partitions(struct mtd_info *master,
355 const struct mtd_partition *parts,
358 struct mtd_part *slave;
359 u_int32_t cur_offset = 0;
362 printk (KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
364 for (i = 0; i < nbparts; i++) {
366 /* allocate the partition structure */
367 slave = kmalloc (sizeof(*slave), GFP_KERNEL);
369 printk ("memory allocation error while creating partitions for \"%s\"\n",
371 del_mtd_partitions(master);
374 memset(slave, 0, sizeof(*slave));
375 list_add(&slave->list, &mtd_partitions);
377 /* set up the MTD object for this partition */
378 slave->mtd.type = master->type;
379 slave->mtd.flags = master->flags & ~parts[i].mask_flags;
380 slave->mtd.size = parts[i].size;
381 slave->mtd.oobblock = master->oobblock;
382 slave->mtd.oobsize = master->oobsize;
383 slave->mtd.ecctype = master->ecctype;
384 slave->mtd.eccsize = master->eccsize;
386 slave->mtd.name = parts[i].name;
387 slave->mtd.bank_size = master->bank_size;
388 slave->mtd.owner = master->owner;
390 slave->mtd.read = part_read;
391 slave->mtd.write = part_write;
393 if(master->point && master->unpoint){
394 slave->mtd.point = part_point;
395 slave->mtd.unpoint = part_unpoint;
398 if (master->read_ecc)
399 slave->mtd.read_ecc = part_read_ecc;
400 if (master->write_ecc)
401 slave->mtd.write_ecc = part_write_ecc;
402 if (master->read_oob)
403 slave->mtd.read_oob = part_read_oob;
404 if (master->write_oob)
405 slave->mtd.write_oob = part_write_oob;
406 if(master->read_user_prot_reg)
407 slave->mtd.read_user_prot_reg = part_read_user_prot_reg;
408 if(master->read_fact_prot_reg)
409 slave->mtd.read_fact_prot_reg = part_read_fact_prot_reg;
410 if(master->write_user_prot_reg)
411 slave->mtd.write_user_prot_reg = part_write_user_prot_reg;
413 slave->mtd.sync = part_sync;
414 if (!i && master->suspend && master->resume) {
415 slave->mtd.suspend = part_suspend;
416 slave->mtd.resume = part_resume;
419 slave->mtd.writev = part_writev;
421 slave->mtd.readv = part_readv;
422 if (master->writev_ecc)
423 slave->mtd.writev_ecc = part_writev_ecc;
424 if (master->readv_ecc)
425 slave->mtd.readv_ecc = part_readv_ecc;
427 slave->mtd.lock = part_lock;
429 slave->mtd.unlock = part_unlock;
430 if (master->block_isbad)
431 slave->mtd.block_isbad = part_block_isbad;
432 if (master->block_markbad)
433 slave->mtd.block_markbad = part_block_markbad;
434 slave->mtd.erase = part_erase;
435 slave->master = master;
436 slave->offset = parts[i].offset;
439 if (slave->offset == MTDPART_OFS_APPEND)
440 slave->offset = cur_offset;
441 if (slave->offset == MTDPART_OFS_NXTBLK) {
442 u_int32_t emask = master->erasesize-1;
443 slave->offset = (cur_offset + emask) & ~emask;
444 if (slave->offset != cur_offset) {
445 printk(KERN_NOTICE "Moving partition %d: "
446 "0x%08x -> 0x%08x\n", i,
447 cur_offset, slave->offset);
450 if (slave->mtd.size == MTDPART_SIZ_FULL)
451 slave->mtd.size = master->size - slave->offset;
452 cur_offset = slave->offset + slave->mtd.size;
454 printk (KERN_NOTICE "0x%08x-0x%08x : \"%s\"\n", slave->offset,
455 slave->offset + slave->mtd.size, slave->mtd.name);
457 /* let's do some sanity checks */
458 if (slave->offset >= master->size) {
459 /* let's register it anyway to preserve ordering */
462 printk ("mtd: partition \"%s\" is out of reach -- disabled\n",
465 if (slave->offset + slave->mtd.size > master->size) {
466 slave->mtd.size = master->size - slave->offset;
467 printk ("mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#x\n",
468 parts[i].name, master->name, slave->mtd.size);
470 if (master->numeraseregions>1) {
471 /* Deal with variable erase size stuff */
473 struct mtd_erase_region_info *regions = master->eraseregions;
475 /* Find the first erase regions which is part of this partition. */
476 for (i=0; i < master->numeraseregions && slave->offset >= regions[i].offset; i++)
479 for (i--; i < master->numeraseregions && slave->offset + slave->mtd.size > regions[i].offset; i++) {
480 if (slave->mtd.erasesize < regions[i].erasesize) {
481 slave->mtd.erasesize = regions[i].erasesize;
485 /* Single erase size */
486 slave->mtd.erasesize = master->erasesize;
489 if ((slave->mtd.flags & MTD_WRITEABLE) &&
490 (slave->offset % slave->mtd.erasesize)) {
491 /* Doesn't start on a boundary of major erase size */
492 /* FIXME: Let it be writable if it is on a boundary of _minor_ erase size though */
493 slave->mtd.flags &= ~MTD_WRITEABLE;
494 printk ("mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
497 if ((slave->mtd.flags & MTD_WRITEABLE) &&
498 (slave->mtd.size % slave->mtd.erasesize)) {
499 slave->mtd.flags &= ~MTD_WRITEABLE;
500 printk ("mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
504 /* copy oobinfo from master */
505 memcpy(&slave->mtd.oobinfo, &master->oobinfo, sizeof(slave->mtd.oobinfo));
508 { /* store the object pointer (caller may or may not register it */
509 *parts[i].mtdp = &slave->mtd;
510 slave->registered = 0;
514 /* register our partition */
515 add_mtd_device(&slave->mtd);
516 slave->registered = 1;
523 EXPORT_SYMBOL(add_mtd_partitions);
524 EXPORT_SYMBOL(del_mtd_partitions);
526 static DEFINE_SPINLOCK(part_parser_lock);
527 static LIST_HEAD(part_parsers);
529 static struct mtd_part_parser *get_partition_parser(const char *name)
531 struct list_head *this;
533 spin_lock(&part_parser_lock);
535 list_for_each(this, &part_parsers) {
536 struct mtd_part_parser *p = list_entry(this, struct mtd_part_parser, list);
538 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
543 spin_unlock(&part_parser_lock);
548 int register_mtd_parser(struct mtd_part_parser *p)
550 spin_lock(&part_parser_lock);
551 list_add(&p->list, &part_parsers);
552 spin_unlock(&part_parser_lock);
557 int deregister_mtd_parser(struct mtd_part_parser *p)
559 spin_lock(&part_parser_lock);
561 spin_unlock(&part_parser_lock);
565 int parse_mtd_partitions(struct mtd_info *master, const char **types,
566 struct mtd_partition **pparts, unsigned long origin)
568 struct mtd_part_parser *parser;
571 for ( ; ret <= 0 && *types; types++) {
572 parser = get_partition_parser(*types);
574 if (!parser && !request_module("%s", *types))
575 parser = get_partition_parser(*types);
578 printk(KERN_NOTICE "%s partition parsing not available\n",
582 ret = (*parser->parse_fn)(master, pparts, origin);
584 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
585 ret, parser->name, master->name);
587 put_partition_parser(parser);
592 EXPORT_SYMBOL_GPL(parse_mtd_partitions);
593 EXPORT_SYMBOL_GPL(register_mtd_parser);
594 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
596 MODULE_LICENSE("GPL");
597 MODULE_AUTHOR("Nicolas Pitre <nico@cam.org>");
598 MODULE_DESCRIPTION("Generic support for partitioning of MTD devices");