1 /* 2 * Simple MTD partitioning layer 3 * 4 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net> 5 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de> 6 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org> 7 * 8 * This program is free software; you can redistribute it and/or modify 9 * it under the terms of the GNU General Public License as published by 10 * the Free Software Foundation; either version 2 of the License, or 11 * (at your option) any later version. 12 * 13 * This program is distributed in the hope that it will be useful, 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 * GNU General Public License for more details. 17 * 18 * You should have received a copy of the GNU General Public License 19 * along with this program; if not, write to the Free Software 20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 21 * 22 */ 23 24 #include <linux/module.h> 25 #include <linux/types.h> 26 #include <linux/kernel.h> 27 #include <linux/slab.h> 28 #include <linux/list.h> 29 #include <linux/kmod.h> 30 #include <linux/mtd/mtd.h> 31 #include <linux/mtd/partitions.h> 32 #include <linux/err.h> 33 #include <linux/kconfig.h> 34 35 #include "mtdcore.h" 36 37 /* Our partition linked list */ 38 static LIST_HEAD(mtd_partitions); 39 static DEFINE_MUTEX(mtd_partitions_mutex); 40 41 /* Our partition node structure */ 42 struct mtd_part { 43 struct mtd_info mtd; 44 struct mtd_info *master; 45 uint64_t offset; 46 struct list_head list; 47 }; 48 49 /* 50 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve 51 * the pointer to that structure with this macro. 52 */ 53 #define PART(x) ((struct mtd_part *)(x)) 54 55 56 /* 57 * MTD methods which simply translate the effective address and pass through 58 * to the _real_ device. 59 */ 60 61 static int part_read(struct mtd_info *mtd, loff_t from, size_t len, 62 size_t *retlen, u_char *buf) 63 { 64 struct mtd_part *part = PART(mtd); 65 struct mtd_ecc_stats stats; 66 int res; 67 68 stats = part->master->ecc_stats; 69 res = part->master->_read(part->master, from + part->offset, len, 70 retlen, buf); 71 if (unlikely(mtd_is_eccerr(res))) 72 mtd->ecc_stats.failed += 73 part->master->ecc_stats.failed - stats.failed; 74 else 75 mtd->ecc_stats.corrected += 76 part->master->ecc_stats.corrected - stats.corrected; 77 return res; 78 } 79 80 static int part_point(struct mtd_info *mtd, loff_t from, size_t len, 81 size_t *retlen, void **virt, resource_size_t *phys) 82 { 83 struct mtd_part *part = PART(mtd); 84 85 return part->master->_point(part->master, from + part->offset, len, 86 retlen, virt, phys); 87 } 88 89 static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len) 90 { 91 struct mtd_part *part = PART(mtd); 92 93 return part->master->_unpoint(part->master, from + part->offset, len); 94 } 95 96 static unsigned long part_get_unmapped_area(struct mtd_info *mtd, 97 unsigned long len, 98 unsigned long offset, 99 unsigned long flags) 100 { 101 struct mtd_part *part = PART(mtd); 102 103 offset += part->offset; 104 return part->master->_get_unmapped_area(part->master, len, offset, 105 flags); 106 } 107 108 static int part_read_oob(struct mtd_info *mtd, loff_t from, 109 struct mtd_oob_ops *ops) 110 { 111 struct mtd_part *part = PART(mtd); 112 int res; 113 114 if (from >= mtd->size) 115 return -EINVAL; 116 if (ops->datbuf && from + ops->len > mtd->size) 117 return -EINVAL; 118 119 /* 120 * If OOB is also requested, make sure that we do not read past the end 121 * of this partition. 122 */ 123 if (ops->oobbuf) { 124 size_t len, pages; 125 126 if (ops->mode == MTD_OPS_AUTO_OOB) 127 len = mtd->oobavail; 128 else 129 len = mtd->oobsize; 130 pages = mtd_div_by_ws(mtd->size, mtd); 131 pages -= mtd_div_by_ws(from, mtd); 132 if (ops->ooboffs + ops->ooblen > pages * len) 133 return -EINVAL; 134 } 135 136 res = part->master->_read_oob(part->master, from + part->offset, ops); 137 if (unlikely(res)) { 138 if (mtd_is_bitflip(res)) 139 mtd->ecc_stats.corrected++; 140 if (mtd_is_eccerr(res)) 141 mtd->ecc_stats.failed++; 142 } 143 return res; 144 } 145 146 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from, 147 size_t len, size_t *retlen, u_char *buf) 148 { 149 struct mtd_part *part = PART(mtd); 150 return part->master->_read_user_prot_reg(part->master, from, len, 151 retlen, buf); 152 } 153 154 static int part_get_user_prot_info(struct mtd_info *mtd, size_t len, 155 size_t *retlen, struct otp_info *buf) 156 { 157 struct mtd_part *part = PART(mtd); 158 return part->master->_get_user_prot_info(part->master, len, retlen, 159 buf); 160 } 161 162 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, 163 size_t len, size_t *retlen, u_char *buf) 164 { 165 struct mtd_part *part = PART(mtd); 166 return part->master->_read_fact_prot_reg(part->master, from, len, 167 retlen, buf); 168 } 169 170 static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len, 171 size_t *retlen, struct otp_info *buf) 172 { 173 struct mtd_part *part = PART(mtd); 174 return part->master->_get_fact_prot_info(part->master, len, retlen, 175 buf); 176 } 177 178 static int part_write(struct mtd_info *mtd, loff_t to, size_t len, 179 size_t *retlen, const u_char *buf) 180 { 181 struct mtd_part *part = PART(mtd); 182 return part->master->_write(part->master, to + part->offset, len, 183 retlen, buf); 184 } 185 186 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len, 187 size_t *retlen, const u_char *buf) 188 { 189 struct mtd_part *part = PART(mtd); 190 return part->master->_panic_write(part->master, to + part->offset, len, 191 retlen, buf); 192 } 193 194 static int part_write_oob(struct mtd_info *mtd, loff_t to, 195 struct mtd_oob_ops *ops) 196 { 197 struct mtd_part *part = PART(mtd); 198 199 if (to >= mtd->size) 200 return -EINVAL; 201 if (ops->datbuf && to + ops->len > mtd->size) 202 return -EINVAL; 203 return part->master->_write_oob(part->master, to + part->offset, ops); 204 } 205 206 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from, 207 size_t len, size_t *retlen, u_char *buf) 208 { 209 struct mtd_part *part = PART(mtd); 210 return part->master->_write_user_prot_reg(part->master, from, len, 211 retlen, buf); 212 } 213 214 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, 215 size_t len) 216 { 217 struct mtd_part *part = PART(mtd); 218 return part->master->_lock_user_prot_reg(part->master, from, len); 219 } 220 221 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs, 222 unsigned long count, loff_t to, size_t *retlen) 223 { 224 struct mtd_part *part = PART(mtd); 225 return part->master->_writev(part->master, vecs, count, 226 to + part->offset, retlen); 227 } 228 229 static int part_erase(struct mtd_info *mtd, struct erase_info *instr) 230 { 231 struct mtd_part *part = PART(mtd); 232 int ret; 233 234 instr->addr += part->offset; 235 ret = part->master->_erase(part->master, instr); 236 if (ret) { 237 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) 238 instr->fail_addr -= part->offset; 239 instr->addr -= part->offset; 240 } 241 return ret; 242 } 243 244 void mtd_erase_callback(struct erase_info *instr) 245 { 246 if (instr->mtd->_erase == part_erase) { 247 struct mtd_part *part = PART(instr->mtd); 248 249 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) 250 instr->fail_addr -= part->offset; 251 instr->addr -= part->offset; 252 } 253 if (instr->callback) 254 instr->callback(instr); 255 } 256 EXPORT_SYMBOL_GPL(mtd_erase_callback); 257 258 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 259 { 260 struct mtd_part *part = PART(mtd); 261 return part->master->_lock(part->master, ofs + part->offset, len); 262 } 263 264 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 265 { 266 struct mtd_part *part = PART(mtd); 267 return part->master->_unlock(part->master, ofs + part->offset, len); 268 } 269 270 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) 271 { 272 struct mtd_part *part = PART(mtd); 273 return part->master->_is_locked(part->master, ofs + part->offset, len); 274 } 275 276 static void part_sync(struct mtd_info *mtd) 277 { 278 struct mtd_part *part = PART(mtd); 279 part->master->_sync(part->master); 280 } 281 282 static int part_suspend(struct mtd_info *mtd) 283 { 284 struct mtd_part *part = PART(mtd); 285 return part->master->_suspend(part->master); 286 } 287 288 static void part_resume(struct mtd_info *mtd) 289 { 290 struct mtd_part *part = PART(mtd); 291 part->master->_resume(part->master); 292 } 293 294 static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs) 295 { 296 struct mtd_part *part = PART(mtd); 297 ofs += part->offset; 298 return part->master->_block_isreserved(part->master, ofs); 299 } 300 301 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs) 302 { 303 struct mtd_part *part = PART(mtd); 304 ofs += part->offset; 305 return part->master->_block_isbad(part->master, ofs); 306 } 307 308 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs) 309 { 310 struct mtd_part *part = PART(mtd); 311 int res; 312 313 ofs += part->offset; 314 res = part->master->_block_markbad(part->master, ofs); 315 if (!res) 316 mtd->ecc_stats.badblocks++; 317 return res; 318 } 319 320 static inline void free_partition(struct mtd_part *p) 321 { 322 kfree(p->mtd.name); 323 kfree(p); 324 } 325 326 /* 327 * This function unregisters and destroy all slave MTD objects which are 328 * attached to the given master MTD object. 329 */ 330 331 int del_mtd_partitions(struct mtd_info *master) 332 { 333 struct mtd_part *slave, *next; 334 int ret, err = 0; 335 336 mutex_lock(&mtd_partitions_mutex); 337 list_for_each_entry_safe(slave, next, &mtd_partitions, list) 338 if (slave->master == master) { 339 ret = del_mtd_device(&slave->mtd); 340 if (ret < 0) { 341 err = ret; 342 continue; 343 } 344 list_del(&slave->list); 345 free_partition(slave); 346 } 347 mutex_unlock(&mtd_partitions_mutex); 348 349 return err; 350 } 351 352 static struct mtd_part *allocate_partition(struct mtd_info *master, 353 const struct mtd_partition *part, int partno, 354 uint64_t cur_offset) 355 { 356 struct mtd_part *slave; 357 char *name; 358 359 /* allocate the partition structure */ 360 slave = kzalloc(sizeof(*slave), GFP_KERNEL); 361 name = kstrdup(part->name, GFP_KERNEL); 362 if (!name || !slave) { 363 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", 364 master->name); 365 kfree(name); 366 kfree(slave); 367 return ERR_PTR(-ENOMEM); 368 } 369 370 /* set up the MTD object for this partition */ 371 slave->mtd.type = master->type; 372 slave->mtd.flags = master->flags & ~part->mask_flags; 373 slave->mtd.size = part->size; 374 slave->mtd.writesize = master->writesize; 375 slave->mtd.writebufsize = master->writebufsize; 376 slave->mtd.oobsize = master->oobsize; 377 slave->mtd.oobavail = master->oobavail; 378 slave->mtd.subpage_sft = master->subpage_sft; 379 380 slave->mtd.name = name; 381 slave->mtd.owner = master->owner; 382 383 /* NOTE: Historically, we didn't arrange MTDs as a tree out of 384 * concern for showing the same data in multiple partitions. 385 * However, it is very useful to have the master node present, 386 * so the MTD_PARTITIONED_MASTER option allows that. The master 387 * will have device nodes etc only if this is set, so make the 388 * parent conditional on that option. Note, this is a way to 389 * distinguish between the master and the partition in sysfs. 390 */ 391 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) ? 392 &master->dev : 393 master->dev.parent; 394 395 slave->mtd._read = part_read; 396 slave->mtd._write = part_write; 397 398 if (master->_panic_write) 399 slave->mtd._panic_write = part_panic_write; 400 401 if (master->_point && master->_unpoint) { 402 slave->mtd._point = part_point; 403 slave->mtd._unpoint = part_unpoint; 404 } 405 406 if (master->_get_unmapped_area) 407 slave->mtd._get_unmapped_area = part_get_unmapped_area; 408 if (master->_read_oob) 409 slave->mtd._read_oob = part_read_oob; 410 if (master->_write_oob) 411 slave->mtd._write_oob = part_write_oob; 412 if (master->_read_user_prot_reg) 413 slave->mtd._read_user_prot_reg = part_read_user_prot_reg; 414 if (master->_read_fact_prot_reg) 415 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg; 416 if (master->_write_user_prot_reg) 417 slave->mtd._write_user_prot_reg = part_write_user_prot_reg; 418 if (master->_lock_user_prot_reg) 419 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg; 420 if (master->_get_user_prot_info) 421 slave->mtd._get_user_prot_info = part_get_user_prot_info; 422 if (master->_get_fact_prot_info) 423 slave->mtd._get_fact_prot_info = part_get_fact_prot_info; 424 if (master->_sync) 425 slave->mtd._sync = part_sync; 426 if (!partno && !master->dev.class && master->_suspend && 427 master->_resume) { 428 slave->mtd._suspend = part_suspend; 429 slave->mtd._resume = part_resume; 430 } 431 if (master->_writev) 432 slave->mtd._writev = part_writev; 433 if (master->_lock) 434 slave->mtd._lock = part_lock; 435 if (master->_unlock) 436 slave->mtd._unlock = part_unlock; 437 if (master->_is_locked) 438 slave->mtd._is_locked = part_is_locked; 439 if (master->_block_isreserved) 440 slave->mtd._block_isreserved = part_block_isreserved; 441 if (master->_block_isbad) 442 slave->mtd._block_isbad = part_block_isbad; 443 if (master->_block_markbad) 444 slave->mtd._block_markbad = part_block_markbad; 445 slave->mtd._erase = part_erase; 446 slave->master = master; 447 slave->offset = part->offset; 448 449 if (slave->offset == MTDPART_OFS_APPEND) 450 slave->offset = cur_offset; 451 if (slave->offset == MTDPART_OFS_NXTBLK) { 452 slave->offset = cur_offset; 453 if (mtd_mod_by_eb(cur_offset, master) != 0) { 454 /* Round up to next erasesize */ 455 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize; 456 printk(KERN_NOTICE "Moving partition %d: " 457 "0x%012llx -> 0x%012llx\n", partno, 458 (unsigned long long)cur_offset, (unsigned long long)slave->offset); 459 } 460 } 461 if (slave->offset == MTDPART_OFS_RETAIN) { 462 slave->offset = cur_offset; 463 if (master->size - slave->offset >= slave->mtd.size) { 464 slave->mtd.size = master->size - slave->offset 465 - slave->mtd.size; 466 } else { 467 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n", 468 part->name, master->size - slave->offset, 469 slave->mtd.size); 470 /* register to preserve ordering */ 471 goto out_register; 472 } 473 } 474 if (slave->mtd.size == MTDPART_SIZ_FULL) 475 slave->mtd.size = master->size - slave->offset; 476 477 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset, 478 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name); 479 480 /* let's do some sanity checks */ 481 if (slave->offset >= master->size) { 482 /* let's register it anyway to preserve ordering */ 483 slave->offset = 0; 484 slave->mtd.size = 0; 485 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", 486 part->name); 487 goto out_register; 488 } 489 if (slave->offset + slave->mtd.size > master->size) { 490 slave->mtd.size = master->size - slave->offset; 491 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n", 492 part->name, master->name, (unsigned long long)slave->mtd.size); 493 } 494 if (master->numeraseregions > 1) { 495 /* Deal with variable erase size stuff */ 496 int i, max = master->numeraseregions; 497 u64 end = slave->offset + slave->mtd.size; 498 struct mtd_erase_region_info *regions = master->eraseregions; 499 500 /* Find the first erase regions which is part of this 501 * partition. */ 502 for (i = 0; i < max && regions[i].offset <= slave->offset; i++) 503 ; 504 /* The loop searched for the region _behind_ the first one */ 505 if (i > 0) 506 i--; 507 508 /* Pick biggest erasesize */ 509 for (; i < max && regions[i].offset < end; i++) { 510 if (slave->mtd.erasesize < regions[i].erasesize) { 511 slave->mtd.erasesize = regions[i].erasesize; 512 } 513 } 514 BUG_ON(slave->mtd.erasesize == 0); 515 } else { 516 /* Single erase size */ 517 slave->mtd.erasesize = master->erasesize; 518 } 519 520 if ((slave->mtd.flags & MTD_WRITEABLE) && 521 mtd_mod_by_eb(slave->offset, &slave->mtd)) { 522 /* Doesn't start on a boundary of major erase size */ 523 /* FIXME: Let it be writable if it is on a boundary of 524 * _minor_ erase size though */ 525 slave->mtd.flags &= ~MTD_WRITEABLE; 526 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n", 527 part->name); 528 } 529 if ((slave->mtd.flags & MTD_WRITEABLE) && 530 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) { 531 slave->mtd.flags &= ~MTD_WRITEABLE; 532 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n", 533 part->name); 534 } 535 536 slave->mtd.ecclayout = master->ecclayout; 537 slave->mtd.ecc_step_size = master->ecc_step_size; 538 slave->mtd.ecc_strength = master->ecc_strength; 539 slave->mtd.bitflip_threshold = master->bitflip_threshold; 540 541 if (master->_block_isbad) { 542 uint64_t offs = 0; 543 544 while (offs < slave->mtd.size) { 545 if (mtd_block_isreserved(master, offs + slave->offset)) 546 slave->mtd.ecc_stats.bbtblocks++; 547 else if (mtd_block_isbad(master, offs + slave->offset)) 548 slave->mtd.ecc_stats.badblocks++; 549 offs += slave->mtd.erasesize; 550 } 551 } 552 553 out_register: 554 return slave; 555 } 556 557 static ssize_t mtd_partition_offset_show(struct device *dev, 558 struct device_attribute *attr, char *buf) 559 { 560 struct mtd_info *mtd = dev_get_drvdata(dev); 561 struct mtd_part *part = PART(mtd); 562 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset); 563 } 564 565 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL); 566 567 static const struct attribute *mtd_partition_attrs[] = { 568 &dev_attr_offset.attr, 569 NULL 570 }; 571 572 static int mtd_add_partition_attrs(struct mtd_part *new) 573 { 574 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs); 575 if (ret) 576 printk(KERN_WARNING 577 "mtd: failed to create partition attrs, err=%d\n", ret); 578 return ret; 579 } 580 581 int mtd_add_partition(struct mtd_info *master, const char *name, 582 long long offset, long long length) 583 { 584 struct mtd_partition part; 585 struct mtd_part *new; 586 int ret = 0; 587 588 /* the direct offset is expected */ 589 if (offset == MTDPART_OFS_APPEND || 590 offset == MTDPART_OFS_NXTBLK) 591 return -EINVAL; 592 593 if (length == MTDPART_SIZ_FULL) 594 length = master->size - offset; 595 596 if (length <= 0) 597 return -EINVAL; 598 599 part.name = name; 600 part.size = length; 601 part.offset = offset; 602 part.mask_flags = 0; 603 part.ecclayout = NULL; 604 605 new = allocate_partition(master, &part, -1, offset); 606 if (IS_ERR(new)) 607 return PTR_ERR(new); 608 609 mutex_lock(&mtd_partitions_mutex); 610 list_add(&new->list, &mtd_partitions); 611 mutex_unlock(&mtd_partitions_mutex); 612 613 add_mtd_device(&new->mtd); 614 615 mtd_add_partition_attrs(new); 616 617 return ret; 618 } 619 EXPORT_SYMBOL_GPL(mtd_add_partition); 620 621 int mtd_del_partition(struct mtd_info *master, int partno) 622 { 623 struct mtd_part *slave, *next; 624 int ret = -EINVAL; 625 626 mutex_lock(&mtd_partitions_mutex); 627 list_for_each_entry_safe(slave, next, &mtd_partitions, list) 628 if ((slave->master == master) && 629 (slave->mtd.index == partno)) { 630 sysfs_remove_files(&slave->mtd.dev.kobj, 631 mtd_partition_attrs); 632 ret = del_mtd_device(&slave->mtd); 633 if (ret < 0) 634 break; 635 636 list_del(&slave->list); 637 free_partition(slave); 638 break; 639 } 640 mutex_unlock(&mtd_partitions_mutex); 641 642 return ret; 643 } 644 EXPORT_SYMBOL_GPL(mtd_del_partition); 645 646 /* 647 * This function, given a master MTD object and a partition table, creates 648 * and registers slave MTD objects which are bound to the master according to 649 * the partition definitions. 650 * 651 * For historical reasons, this function's caller only registers the master 652 * if the MTD_PARTITIONED_MASTER config option is set. 653 */ 654 655 int add_mtd_partitions(struct mtd_info *master, 656 const struct mtd_partition *parts, 657 int nbparts) 658 { 659 struct mtd_part *slave; 660 uint64_t cur_offset = 0; 661 int i; 662 663 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); 664 665 for (i = 0; i < nbparts; i++) { 666 slave = allocate_partition(master, parts + i, i, cur_offset); 667 if (IS_ERR(slave)) 668 return PTR_ERR(slave); 669 670 mutex_lock(&mtd_partitions_mutex); 671 list_add(&slave->list, &mtd_partitions); 672 mutex_unlock(&mtd_partitions_mutex); 673 674 add_mtd_device(&slave->mtd); 675 mtd_add_partition_attrs(slave); 676 677 cur_offset = slave->offset + slave->mtd.size; 678 } 679 680 return 0; 681 } 682 683 static DEFINE_SPINLOCK(part_parser_lock); 684 static LIST_HEAD(part_parsers); 685 686 static struct mtd_part_parser *get_partition_parser(const char *name) 687 { 688 struct mtd_part_parser *p, *ret = NULL; 689 690 spin_lock(&part_parser_lock); 691 692 list_for_each_entry(p, &part_parsers, list) 693 if (!strcmp(p->name, name) && try_module_get(p->owner)) { 694 ret = p; 695 break; 696 } 697 698 spin_unlock(&part_parser_lock); 699 700 return ret; 701 } 702 703 #define put_partition_parser(p) do { module_put((p)->owner); } while (0) 704 705 void register_mtd_parser(struct mtd_part_parser *p) 706 { 707 spin_lock(&part_parser_lock); 708 list_add(&p->list, &part_parsers); 709 spin_unlock(&part_parser_lock); 710 } 711 EXPORT_SYMBOL_GPL(register_mtd_parser); 712 713 void deregister_mtd_parser(struct mtd_part_parser *p) 714 { 715 spin_lock(&part_parser_lock); 716 list_del(&p->list); 717 spin_unlock(&part_parser_lock); 718 } 719 EXPORT_SYMBOL_GPL(deregister_mtd_parser); 720 721 /* 722 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you 723 * are changing this array! 724 */ 725 static const char * const default_mtd_part_types[] = { 726 "cmdlinepart", 727 "ofpart", 728 NULL 729 }; 730 731 /** 732 * parse_mtd_partitions - parse MTD partitions 733 * @master: the master partition (describes whole MTD device) 734 * @types: names of partition parsers to try or %NULL 735 * @pparts: array of partitions found is returned here 736 * @data: MTD partition parser-specific data 737 * 738 * This function tries to find partition on MTD device @master. It uses MTD 739 * partition parsers, specified in @types. However, if @types is %NULL, then 740 * the default list of parsers is used. The default list contains only the 741 * "cmdlinepart" and "ofpart" parsers ATM. 742 * Note: If there are more then one parser in @types, the kernel only takes the 743 * partitions parsed out by the first parser. 744 * 745 * This function may return: 746 * o a negative error code in case of failure 747 * o zero if no partitions were found 748 * o a positive number of found partitions, in which case on exit @pparts will 749 * point to an array containing this number of &struct mtd_info objects. 750 */ 751 int parse_mtd_partitions(struct mtd_info *master, const char *const *types, 752 struct mtd_partition **pparts, 753 struct mtd_part_parser_data *data) 754 { 755 struct mtd_part_parser *parser; 756 int ret = 0; 757 758 if (!types) 759 types = default_mtd_part_types; 760 761 for ( ; ret <= 0 && *types; types++) { 762 parser = get_partition_parser(*types); 763 if (!parser && !request_module("%s", *types)) 764 parser = get_partition_parser(*types); 765 if (!parser) 766 continue; 767 ret = (*parser->parse_fn)(master, pparts, data); 768 put_partition_parser(parser); 769 if (ret > 0) { 770 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n", 771 ret, parser->name, master->name); 772 break; 773 } 774 } 775 return ret; 776 } 777 778 int mtd_is_partition(const struct mtd_info *mtd) 779 { 780 struct mtd_part *part; 781 int ispart = 0; 782 783 mutex_lock(&mtd_partitions_mutex); 784 list_for_each_entry(part, &mtd_partitions, list) 785 if (&part->mtd == mtd) { 786 ispart = 1; 787 break; 788 } 789 mutex_unlock(&mtd_partitions_mutex); 790 791 return ispart; 792 } 793 EXPORT_SYMBOL_GPL(mtd_is_partition); 794 795 /* Returns the size of the entire flash chip */ 796 uint64_t mtd_get_device_size(const struct mtd_info *mtd) 797 { 798 if (!mtd_is_partition(mtd)) 799 return mtd->size; 800 801 return PART(mtd)->master->size; 802 } 803 EXPORT_SYMBOL_GPL(mtd_get_device_size); 804