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 34 #include "mtdcore.h" 35 36 /* Our partition linked list */ 37 static LIST_HEAD(mtd_partitions); 38 static DEFINE_MUTEX(mtd_partitions_mutex); 39 40 /** 41 * struct mtd_part - our partition node structure 42 * 43 * @mtd: struct holding partition details 44 * @parent: parent mtd - flash device or another partition 45 * @offset: partition offset relative to the *flash device* 46 */ 47 struct mtd_part { 48 struct mtd_info mtd; 49 struct mtd_info *parent; 50 uint64_t offset; 51 struct list_head list; 52 }; 53 54 /* 55 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve 56 * the pointer to that structure. 57 */ 58 static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd) 59 { 60 return container_of(mtd, struct mtd_part, mtd); 61 } 62 63 64 /* 65 * MTD methods which simply translate the effective address and pass through 66 * to the _real_ device. 67 */ 68 69 static int part_read(struct mtd_info *mtd, loff_t from, size_t len, 70 size_t *retlen, u_char *buf) 71 { 72 struct mtd_part *part = mtd_to_part(mtd); 73 struct mtd_ecc_stats stats; 74 int res; 75 76 stats = part->parent->ecc_stats; 77 res = part->parent->_read(part->parent, from + part->offset, len, 78 retlen, buf); 79 if (unlikely(mtd_is_eccerr(res))) 80 mtd->ecc_stats.failed += 81 part->parent->ecc_stats.failed - stats.failed; 82 else 83 mtd->ecc_stats.corrected += 84 part->parent->ecc_stats.corrected - stats.corrected; 85 return res; 86 } 87 88 static int part_point(struct mtd_info *mtd, loff_t from, size_t len, 89 size_t *retlen, void **virt, resource_size_t *phys) 90 { 91 struct mtd_part *part = mtd_to_part(mtd); 92 93 return part->parent->_point(part->parent, from + part->offset, len, 94 retlen, virt, phys); 95 } 96 97 static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len) 98 { 99 struct mtd_part *part = mtd_to_part(mtd); 100 101 return part->parent->_unpoint(part->parent, from + part->offset, len); 102 } 103 104 static int part_read_oob(struct mtd_info *mtd, loff_t from, 105 struct mtd_oob_ops *ops) 106 { 107 struct mtd_part *part = mtd_to_part(mtd); 108 struct mtd_ecc_stats stats; 109 int res; 110 111 stats = part->parent->ecc_stats; 112 res = part->parent->_read_oob(part->parent, from + part->offset, ops); 113 if (unlikely(mtd_is_eccerr(res))) 114 mtd->ecc_stats.failed += 115 part->parent->ecc_stats.failed - stats.failed; 116 else 117 mtd->ecc_stats.corrected += 118 part->parent->ecc_stats.corrected - stats.corrected; 119 return res; 120 } 121 122 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from, 123 size_t len, size_t *retlen, u_char *buf) 124 { 125 struct mtd_part *part = mtd_to_part(mtd); 126 return part->parent->_read_user_prot_reg(part->parent, from, len, 127 retlen, buf); 128 } 129 130 static int part_get_user_prot_info(struct mtd_info *mtd, size_t len, 131 size_t *retlen, struct otp_info *buf) 132 { 133 struct mtd_part *part = mtd_to_part(mtd); 134 return part->parent->_get_user_prot_info(part->parent, len, retlen, 135 buf); 136 } 137 138 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, 139 size_t len, size_t *retlen, u_char *buf) 140 { 141 struct mtd_part *part = mtd_to_part(mtd); 142 return part->parent->_read_fact_prot_reg(part->parent, from, len, 143 retlen, buf); 144 } 145 146 static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len, 147 size_t *retlen, struct otp_info *buf) 148 { 149 struct mtd_part *part = mtd_to_part(mtd); 150 return part->parent->_get_fact_prot_info(part->parent, len, retlen, 151 buf); 152 } 153 154 static int part_write(struct mtd_info *mtd, loff_t to, size_t len, 155 size_t *retlen, const u_char *buf) 156 { 157 struct mtd_part *part = mtd_to_part(mtd); 158 return part->parent->_write(part->parent, to + part->offset, len, 159 retlen, buf); 160 } 161 162 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len, 163 size_t *retlen, const u_char *buf) 164 { 165 struct mtd_part *part = mtd_to_part(mtd); 166 return part->parent->_panic_write(part->parent, to + part->offset, len, 167 retlen, buf); 168 } 169 170 static int part_write_oob(struct mtd_info *mtd, loff_t to, 171 struct mtd_oob_ops *ops) 172 { 173 struct mtd_part *part = mtd_to_part(mtd); 174 175 return part->parent->_write_oob(part->parent, to + part->offset, ops); 176 } 177 178 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from, 179 size_t len, size_t *retlen, u_char *buf) 180 { 181 struct mtd_part *part = mtd_to_part(mtd); 182 return part->parent->_write_user_prot_reg(part->parent, from, len, 183 retlen, buf); 184 } 185 186 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, 187 size_t len) 188 { 189 struct mtd_part *part = mtd_to_part(mtd); 190 return part->parent->_lock_user_prot_reg(part->parent, from, len); 191 } 192 193 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs, 194 unsigned long count, loff_t to, size_t *retlen) 195 { 196 struct mtd_part *part = mtd_to_part(mtd); 197 return part->parent->_writev(part->parent, vecs, count, 198 to + part->offset, retlen); 199 } 200 201 static int part_erase(struct mtd_info *mtd, struct erase_info *instr) 202 { 203 struct mtd_part *part = mtd_to_part(mtd); 204 int ret; 205 206 instr->addr += part->offset; 207 ret = part->parent->_erase(part->parent, instr); 208 if (ret) { 209 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) 210 instr->fail_addr -= part->offset; 211 instr->addr -= part->offset; 212 } 213 return ret; 214 } 215 216 void mtd_erase_callback(struct erase_info *instr) 217 { 218 if (instr->mtd->_erase == part_erase) { 219 struct mtd_part *part = mtd_to_part(instr->mtd); 220 221 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) 222 instr->fail_addr -= part->offset; 223 instr->addr -= part->offset; 224 } 225 if (instr->callback) 226 instr->callback(instr); 227 } 228 EXPORT_SYMBOL_GPL(mtd_erase_callback); 229 230 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 231 { 232 struct mtd_part *part = mtd_to_part(mtd); 233 return part->parent->_lock(part->parent, ofs + part->offset, len); 234 } 235 236 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) 237 { 238 struct mtd_part *part = mtd_to_part(mtd); 239 return part->parent->_unlock(part->parent, ofs + part->offset, len); 240 } 241 242 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) 243 { 244 struct mtd_part *part = mtd_to_part(mtd); 245 return part->parent->_is_locked(part->parent, ofs + part->offset, len); 246 } 247 248 static void part_sync(struct mtd_info *mtd) 249 { 250 struct mtd_part *part = mtd_to_part(mtd); 251 part->parent->_sync(part->parent); 252 } 253 254 static int part_suspend(struct mtd_info *mtd) 255 { 256 struct mtd_part *part = mtd_to_part(mtd); 257 return part->parent->_suspend(part->parent); 258 } 259 260 static void part_resume(struct mtd_info *mtd) 261 { 262 struct mtd_part *part = mtd_to_part(mtd); 263 part->parent->_resume(part->parent); 264 } 265 266 static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs) 267 { 268 struct mtd_part *part = mtd_to_part(mtd); 269 ofs += part->offset; 270 return part->parent->_block_isreserved(part->parent, ofs); 271 } 272 273 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs) 274 { 275 struct mtd_part *part = mtd_to_part(mtd); 276 ofs += part->offset; 277 return part->parent->_block_isbad(part->parent, ofs); 278 } 279 280 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs) 281 { 282 struct mtd_part *part = mtd_to_part(mtd); 283 int res; 284 285 ofs += part->offset; 286 res = part->parent->_block_markbad(part->parent, ofs); 287 if (!res) 288 mtd->ecc_stats.badblocks++; 289 return res; 290 } 291 292 static int part_get_device(struct mtd_info *mtd) 293 { 294 struct mtd_part *part = mtd_to_part(mtd); 295 return part->parent->_get_device(part->parent); 296 } 297 298 static void part_put_device(struct mtd_info *mtd) 299 { 300 struct mtd_part *part = mtd_to_part(mtd); 301 part->parent->_put_device(part->parent); 302 } 303 304 static int part_ooblayout_ecc(struct mtd_info *mtd, int section, 305 struct mtd_oob_region *oobregion) 306 { 307 struct mtd_part *part = mtd_to_part(mtd); 308 309 return mtd_ooblayout_ecc(part->parent, section, oobregion); 310 } 311 312 static int part_ooblayout_free(struct mtd_info *mtd, int section, 313 struct mtd_oob_region *oobregion) 314 { 315 struct mtd_part *part = mtd_to_part(mtd); 316 317 return mtd_ooblayout_free(part->parent, section, oobregion); 318 } 319 320 static const struct mtd_ooblayout_ops part_ooblayout_ops = { 321 .ecc = part_ooblayout_ecc, 322 .free = part_ooblayout_free, 323 }; 324 325 static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) 326 { 327 struct mtd_part *part = mtd_to_part(mtd); 328 329 return part->parent->_max_bad_blocks(part->parent, 330 ofs + part->offset, len); 331 } 332 333 static inline void free_partition(struct mtd_part *p) 334 { 335 kfree(p->mtd.name); 336 kfree(p); 337 } 338 339 /** 340 * mtd_parse_part - parse MTD partition looking for subpartitions 341 * 342 * @slave: part that is supposed to be a container and should be parsed 343 * @types: NULL-terminated array with names of partition parsers to try 344 * 345 * Some partitions are kind of containers with extra subpartitions (volumes). 346 * There can be various formats of such containers. This function tries to use 347 * specified parsers to analyze given partition and registers found 348 * subpartitions on success. 349 */ 350 static int mtd_parse_part(struct mtd_part *slave, const char *const *types) 351 { 352 struct mtd_partitions parsed; 353 int err; 354 355 err = parse_mtd_partitions(&slave->mtd, types, &parsed, NULL); 356 if (err) 357 return err; 358 else if (!parsed.nr_parts) 359 return -ENOENT; 360 361 err = add_mtd_partitions(&slave->mtd, parsed.parts, parsed.nr_parts); 362 363 mtd_part_parser_cleanup(&parsed); 364 365 return err; 366 } 367 368 static struct mtd_part *allocate_partition(struct mtd_info *parent, 369 const struct mtd_partition *part, int partno, 370 uint64_t cur_offset) 371 { 372 int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize : 373 parent->erasesize; 374 struct mtd_part *slave; 375 u32 remainder; 376 char *name; 377 u64 tmp; 378 379 /* allocate the partition structure */ 380 slave = kzalloc(sizeof(*slave), GFP_KERNEL); 381 name = kstrdup(part->name, GFP_KERNEL); 382 if (!name || !slave) { 383 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", 384 parent->name); 385 kfree(name); 386 kfree(slave); 387 return ERR_PTR(-ENOMEM); 388 } 389 390 /* set up the MTD object for this partition */ 391 slave->mtd.type = parent->type; 392 slave->mtd.flags = parent->flags & ~part->mask_flags; 393 slave->mtd.size = part->size; 394 slave->mtd.writesize = parent->writesize; 395 slave->mtd.writebufsize = parent->writebufsize; 396 slave->mtd.oobsize = parent->oobsize; 397 slave->mtd.oobavail = parent->oobavail; 398 slave->mtd.subpage_sft = parent->subpage_sft; 399 slave->mtd.pairing = parent->pairing; 400 401 slave->mtd.name = name; 402 slave->mtd.owner = parent->owner; 403 404 /* NOTE: Historically, we didn't arrange MTDs as a tree out of 405 * concern for showing the same data in multiple partitions. 406 * However, it is very useful to have the master node present, 407 * so the MTD_PARTITIONED_MASTER option allows that. The master 408 * will have device nodes etc only if this is set, so make the 409 * parent conditional on that option. Note, this is a way to 410 * distinguish between the master and the partition in sysfs. 411 */ 412 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ? 413 &parent->dev : 414 parent->dev.parent; 415 slave->mtd.dev.of_node = part->of_node; 416 417 if (parent->_read) 418 slave->mtd._read = part_read; 419 if (parent->_write) 420 slave->mtd._write = part_write; 421 422 if (parent->_panic_write) 423 slave->mtd._panic_write = part_panic_write; 424 425 if (parent->_point && parent->_unpoint) { 426 slave->mtd._point = part_point; 427 slave->mtd._unpoint = part_unpoint; 428 } 429 430 if (parent->_read_oob) 431 slave->mtd._read_oob = part_read_oob; 432 if (parent->_write_oob) 433 slave->mtd._write_oob = part_write_oob; 434 if (parent->_read_user_prot_reg) 435 slave->mtd._read_user_prot_reg = part_read_user_prot_reg; 436 if (parent->_read_fact_prot_reg) 437 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg; 438 if (parent->_write_user_prot_reg) 439 slave->mtd._write_user_prot_reg = part_write_user_prot_reg; 440 if (parent->_lock_user_prot_reg) 441 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg; 442 if (parent->_get_user_prot_info) 443 slave->mtd._get_user_prot_info = part_get_user_prot_info; 444 if (parent->_get_fact_prot_info) 445 slave->mtd._get_fact_prot_info = part_get_fact_prot_info; 446 if (parent->_sync) 447 slave->mtd._sync = part_sync; 448 if (!partno && !parent->dev.class && parent->_suspend && 449 parent->_resume) { 450 slave->mtd._suspend = part_suspend; 451 slave->mtd._resume = part_resume; 452 } 453 if (parent->_writev) 454 slave->mtd._writev = part_writev; 455 if (parent->_lock) 456 slave->mtd._lock = part_lock; 457 if (parent->_unlock) 458 slave->mtd._unlock = part_unlock; 459 if (parent->_is_locked) 460 slave->mtd._is_locked = part_is_locked; 461 if (parent->_block_isreserved) 462 slave->mtd._block_isreserved = part_block_isreserved; 463 if (parent->_block_isbad) 464 slave->mtd._block_isbad = part_block_isbad; 465 if (parent->_block_markbad) 466 slave->mtd._block_markbad = part_block_markbad; 467 if (parent->_max_bad_blocks) 468 slave->mtd._max_bad_blocks = part_max_bad_blocks; 469 470 if (parent->_get_device) 471 slave->mtd._get_device = part_get_device; 472 if (parent->_put_device) 473 slave->mtd._put_device = part_put_device; 474 475 slave->mtd._erase = part_erase; 476 slave->parent = parent; 477 slave->offset = part->offset; 478 479 if (slave->offset == MTDPART_OFS_APPEND) 480 slave->offset = cur_offset; 481 if (slave->offset == MTDPART_OFS_NXTBLK) { 482 tmp = cur_offset; 483 slave->offset = cur_offset; 484 remainder = do_div(tmp, wr_alignment); 485 if (remainder) { 486 slave->offset += wr_alignment - remainder; 487 printk(KERN_NOTICE "Moving partition %d: " 488 "0x%012llx -> 0x%012llx\n", partno, 489 (unsigned long long)cur_offset, (unsigned long long)slave->offset); 490 } 491 } 492 if (slave->offset == MTDPART_OFS_RETAIN) { 493 slave->offset = cur_offset; 494 if (parent->size - slave->offset >= slave->mtd.size) { 495 slave->mtd.size = parent->size - slave->offset 496 - slave->mtd.size; 497 } else { 498 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n", 499 part->name, parent->size - slave->offset, 500 slave->mtd.size); 501 /* register to preserve ordering */ 502 goto out_register; 503 } 504 } 505 if (slave->mtd.size == MTDPART_SIZ_FULL) 506 slave->mtd.size = parent->size - slave->offset; 507 508 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset, 509 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name); 510 511 /* let's do some sanity checks */ 512 if (slave->offset >= parent->size) { 513 /* let's register it anyway to preserve ordering */ 514 slave->offset = 0; 515 slave->mtd.size = 0; 516 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", 517 part->name); 518 goto out_register; 519 } 520 if (slave->offset + slave->mtd.size > parent->size) { 521 slave->mtd.size = parent->size - slave->offset; 522 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n", 523 part->name, parent->name, (unsigned long long)slave->mtd.size); 524 } 525 if (parent->numeraseregions > 1) { 526 /* Deal with variable erase size stuff */ 527 int i, max = parent->numeraseregions; 528 u64 end = slave->offset + slave->mtd.size; 529 struct mtd_erase_region_info *regions = parent->eraseregions; 530 531 /* Find the first erase regions which is part of this 532 * partition. */ 533 for (i = 0; i < max && regions[i].offset <= slave->offset; i++) 534 ; 535 /* The loop searched for the region _behind_ the first one */ 536 if (i > 0) 537 i--; 538 539 /* Pick biggest erasesize */ 540 for (; i < max && regions[i].offset < end; i++) { 541 if (slave->mtd.erasesize < regions[i].erasesize) { 542 slave->mtd.erasesize = regions[i].erasesize; 543 } 544 } 545 BUG_ON(slave->mtd.erasesize == 0); 546 } else { 547 /* Single erase size */ 548 slave->mtd.erasesize = parent->erasesize; 549 } 550 551 /* 552 * Slave erasesize might differ from the master one if the master 553 * exposes several regions with different erasesize. Adjust 554 * wr_alignment accordingly. 555 */ 556 if (!(slave->mtd.flags & MTD_NO_ERASE)) 557 wr_alignment = slave->mtd.erasesize; 558 559 tmp = slave->offset; 560 remainder = do_div(tmp, wr_alignment); 561 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) { 562 /* Doesn't start on a boundary of major erase size */ 563 /* FIXME: Let it be writable if it is on a boundary of 564 * _minor_ erase size though */ 565 slave->mtd.flags &= ~MTD_WRITEABLE; 566 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n", 567 part->name); 568 } 569 570 tmp = slave->mtd.size; 571 remainder = do_div(tmp, wr_alignment); 572 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) { 573 slave->mtd.flags &= ~MTD_WRITEABLE; 574 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n", 575 part->name); 576 } 577 578 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops); 579 slave->mtd.ecc_step_size = parent->ecc_step_size; 580 slave->mtd.ecc_strength = parent->ecc_strength; 581 slave->mtd.bitflip_threshold = parent->bitflip_threshold; 582 583 if (parent->_block_isbad) { 584 uint64_t offs = 0; 585 586 while (offs < slave->mtd.size) { 587 if (mtd_block_isreserved(parent, offs + slave->offset)) 588 slave->mtd.ecc_stats.bbtblocks++; 589 else if (mtd_block_isbad(parent, offs + slave->offset)) 590 slave->mtd.ecc_stats.badblocks++; 591 offs += slave->mtd.erasesize; 592 } 593 } 594 595 out_register: 596 return slave; 597 } 598 599 static ssize_t mtd_partition_offset_show(struct device *dev, 600 struct device_attribute *attr, char *buf) 601 { 602 struct mtd_info *mtd = dev_get_drvdata(dev); 603 struct mtd_part *part = mtd_to_part(mtd); 604 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset); 605 } 606 607 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL); 608 609 static const struct attribute *mtd_partition_attrs[] = { 610 &dev_attr_offset.attr, 611 NULL 612 }; 613 614 static int mtd_add_partition_attrs(struct mtd_part *new) 615 { 616 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs); 617 if (ret) 618 printk(KERN_WARNING 619 "mtd: failed to create partition attrs, err=%d\n", ret); 620 return ret; 621 } 622 623 int mtd_add_partition(struct mtd_info *parent, const char *name, 624 long long offset, long long length) 625 { 626 struct mtd_partition part; 627 struct mtd_part *new; 628 int ret = 0; 629 630 /* the direct offset is expected */ 631 if (offset == MTDPART_OFS_APPEND || 632 offset == MTDPART_OFS_NXTBLK) 633 return -EINVAL; 634 635 if (length == MTDPART_SIZ_FULL) 636 length = parent->size - offset; 637 638 if (length <= 0) 639 return -EINVAL; 640 641 memset(&part, 0, sizeof(part)); 642 part.name = name; 643 part.size = length; 644 part.offset = offset; 645 646 new = allocate_partition(parent, &part, -1, offset); 647 if (IS_ERR(new)) 648 return PTR_ERR(new); 649 650 mutex_lock(&mtd_partitions_mutex); 651 list_add(&new->list, &mtd_partitions); 652 mutex_unlock(&mtd_partitions_mutex); 653 654 add_mtd_device(&new->mtd); 655 656 mtd_add_partition_attrs(new); 657 658 return ret; 659 } 660 EXPORT_SYMBOL_GPL(mtd_add_partition); 661 662 /** 663 * __mtd_del_partition - delete MTD partition 664 * 665 * @priv: internal MTD struct for partition to be deleted 666 * 667 * This function must be called with the partitions mutex locked. 668 */ 669 static int __mtd_del_partition(struct mtd_part *priv) 670 { 671 struct mtd_part *child, *next; 672 int err; 673 674 list_for_each_entry_safe(child, next, &mtd_partitions, list) { 675 if (child->parent == &priv->mtd) { 676 err = __mtd_del_partition(child); 677 if (err) 678 return err; 679 } 680 } 681 682 sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs); 683 684 err = del_mtd_device(&priv->mtd); 685 if (err) 686 return err; 687 688 list_del(&priv->list); 689 free_partition(priv); 690 691 return 0; 692 } 693 694 /* 695 * This function unregisters and destroy all slave MTD objects which are 696 * attached to the given MTD object. 697 */ 698 int del_mtd_partitions(struct mtd_info *mtd) 699 { 700 struct mtd_part *slave, *next; 701 int ret, err = 0; 702 703 mutex_lock(&mtd_partitions_mutex); 704 list_for_each_entry_safe(slave, next, &mtd_partitions, list) 705 if (slave->parent == mtd) { 706 ret = __mtd_del_partition(slave); 707 if (ret < 0) 708 err = ret; 709 } 710 mutex_unlock(&mtd_partitions_mutex); 711 712 return err; 713 } 714 715 int mtd_del_partition(struct mtd_info *mtd, int partno) 716 { 717 struct mtd_part *slave, *next; 718 int ret = -EINVAL; 719 720 mutex_lock(&mtd_partitions_mutex); 721 list_for_each_entry_safe(slave, next, &mtd_partitions, list) 722 if ((slave->parent == mtd) && 723 (slave->mtd.index == partno)) { 724 ret = __mtd_del_partition(slave); 725 break; 726 } 727 mutex_unlock(&mtd_partitions_mutex); 728 729 return ret; 730 } 731 EXPORT_SYMBOL_GPL(mtd_del_partition); 732 733 /* 734 * This function, given a master MTD object and a partition table, creates 735 * and registers slave MTD objects which are bound to the master according to 736 * the partition definitions. 737 * 738 * For historical reasons, this function's caller only registers the master 739 * if the MTD_PARTITIONED_MASTER config option is set. 740 */ 741 742 int add_mtd_partitions(struct mtd_info *master, 743 const struct mtd_partition *parts, 744 int nbparts) 745 { 746 struct mtd_part *slave; 747 uint64_t cur_offset = 0; 748 int i; 749 750 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); 751 752 for (i = 0; i < nbparts; i++) { 753 slave = allocate_partition(master, parts + i, i, cur_offset); 754 if (IS_ERR(slave)) { 755 del_mtd_partitions(master); 756 return PTR_ERR(slave); 757 } 758 759 mutex_lock(&mtd_partitions_mutex); 760 list_add(&slave->list, &mtd_partitions); 761 mutex_unlock(&mtd_partitions_mutex); 762 763 add_mtd_device(&slave->mtd); 764 mtd_add_partition_attrs(slave); 765 if (parts[i].types) 766 mtd_parse_part(slave, parts[i].types); 767 768 cur_offset = slave->offset + slave->mtd.size; 769 } 770 771 return 0; 772 } 773 774 static DEFINE_SPINLOCK(part_parser_lock); 775 static LIST_HEAD(part_parsers); 776 777 static struct mtd_part_parser *mtd_part_parser_get(const char *name) 778 { 779 struct mtd_part_parser *p, *ret = NULL; 780 781 spin_lock(&part_parser_lock); 782 783 list_for_each_entry(p, &part_parsers, list) 784 if (!strcmp(p->name, name) && try_module_get(p->owner)) { 785 ret = p; 786 break; 787 } 788 789 spin_unlock(&part_parser_lock); 790 791 return ret; 792 } 793 794 static inline void mtd_part_parser_put(const struct mtd_part_parser *p) 795 { 796 module_put(p->owner); 797 } 798 799 /* 800 * Many partition parsers just expected the core to kfree() all their data in 801 * one chunk. Do that by default. 802 */ 803 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts, 804 int nr_parts) 805 { 806 kfree(pparts); 807 } 808 809 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner) 810 { 811 p->owner = owner; 812 813 if (!p->cleanup) 814 p->cleanup = &mtd_part_parser_cleanup_default; 815 816 spin_lock(&part_parser_lock); 817 list_add(&p->list, &part_parsers); 818 spin_unlock(&part_parser_lock); 819 820 return 0; 821 } 822 EXPORT_SYMBOL_GPL(__register_mtd_parser); 823 824 void deregister_mtd_parser(struct mtd_part_parser *p) 825 { 826 spin_lock(&part_parser_lock); 827 list_del(&p->list); 828 spin_unlock(&part_parser_lock); 829 } 830 EXPORT_SYMBOL_GPL(deregister_mtd_parser); 831 832 /* 833 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you 834 * are changing this array! 835 */ 836 static const char * const default_mtd_part_types[] = { 837 "cmdlinepart", 838 "ofpart", 839 NULL 840 }; 841 842 static int mtd_part_do_parse(struct mtd_part_parser *parser, 843 struct mtd_info *master, 844 struct mtd_partitions *pparts, 845 struct mtd_part_parser_data *data) 846 { 847 int ret; 848 849 ret = (*parser->parse_fn)(master, &pparts->parts, data); 850 pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret); 851 if (ret <= 0) 852 return ret; 853 854 pr_notice("%d %s partitions found on MTD device %s\n", ret, 855 parser->name, master->name); 856 857 pparts->nr_parts = ret; 858 pparts->parser = parser; 859 860 return ret; 861 } 862 863 /** 864 * parse_mtd_partitions - parse MTD partitions 865 * @master: the master partition (describes whole MTD device) 866 * @types: names of partition parsers to try or %NULL 867 * @pparts: info about partitions found is returned here 868 * @data: MTD partition parser-specific data 869 * 870 * This function tries to find partition on MTD device @master. It uses MTD 871 * partition parsers, specified in @types. However, if @types is %NULL, then 872 * the default list of parsers is used. The default list contains only the 873 * "cmdlinepart" and "ofpart" parsers ATM. 874 * Note: If there are more then one parser in @types, the kernel only takes the 875 * partitions parsed out by the first parser. 876 * 877 * This function may return: 878 * o a negative error code in case of failure 879 * o zero otherwise, and @pparts will describe the partitions, number of 880 * partitions, and the parser which parsed them. Caller must release 881 * resources with mtd_part_parser_cleanup() when finished with the returned 882 * data. 883 */ 884 int parse_mtd_partitions(struct mtd_info *master, const char *const *types, 885 struct mtd_partitions *pparts, 886 struct mtd_part_parser_data *data) 887 { 888 struct mtd_part_parser *parser; 889 int ret, err = 0; 890 891 if (!types) 892 types = default_mtd_part_types; 893 894 for ( ; *types; types++) { 895 pr_debug("%s: parsing partitions %s\n", master->name, *types); 896 parser = mtd_part_parser_get(*types); 897 if (!parser && !request_module("%s", *types)) 898 parser = mtd_part_parser_get(*types); 899 pr_debug("%s: got parser %s\n", master->name, 900 parser ? parser->name : NULL); 901 if (!parser) 902 continue; 903 ret = mtd_part_do_parse(parser, master, pparts, data); 904 /* Found partitions! */ 905 if (ret > 0) 906 return 0; 907 mtd_part_parser_put(parser); 908 /* 909 * Stash the first error we see; only report it if no parser 910 * succeeds 911 */ 912 if (ret < 0 && !err) 913 err = ret; 914 } 915 return err; 916 } 917 918 void mtd_part_parser_cleanup(struct mtd_partitions *parts) 919 { 920 const struct mtd_part_parser *parser; 921 922 if (!parts) 923 return; 924 925 parser = parts->parser; 926 if (parser) { 927 if (parser->cleanup) 928 parser->cleanup(parts->parts, parts->nr_parts); 929 930 mtd_part_parser_put(parser); 931 } 932 } 933 934 int mtd_is_partition(const struct mtd_info *mtd) 935 { 936 struct mtd_part *part; 937 int ispart = 0; 938 939 mutex_lock(&mtd_partitions_mutex); 940 list_for_each_entry(part, &mtd_partitions, list) 941 if (&part->mtd == mtd) { 942 ispart = 1; 943 break; 944 } 945 mutex_unlock(&mtd_partitions_mutex); 946 947 return ispart; 948 } 949 EXPORT_SYMBOL_GPL(mtd_is_partition); 950 951 /* Returns the size of the entire flash chip */ 952 uint64_t mtd_get_device_size(const struct mtd_info *mtd) 953 { 954 if (!mtd_is_partition(mtd)) 955 return mtd->size; 956 957 return mtd_get_device_size(mtd_to_part(mtd)->parent); 958 } 959 EXPORT_SYMBOL_GPL(mtd_get_device_size); 960