1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Simple MTD partitioning layer 4 * 5 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net> 6 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de> 7 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org> 8 */ 9 10 #include <linux/module.h> 11 #include <linux/types.h> 12 #include <linux/kernel.h> 13 #include <linux/slab.h> 14 #include <linux/list.h> 15 #include <linux/kmod.h> 16 #include <linux/mtd/mtd.h> 17 #include <linux/mtd/partitions.h> 18 #include <linux/err.h> 19 #include <linux/of.h> 20 #include <linux/of_platform.h> 21 22 #include "mtdcore.h" 23 24 /* 25 * MTD methods which simply translate the effective address and pass through 26 * to the _real_ device. 27 */ 28 29 static inline void free_partition(struct mtd_info *mtd) 30 { 31 kfree(mtd->name); 32 kfree(mtd); 33 } 34 35 static struct mtd_info *allocate_partition(struct mtd_info *parent, 36 const struct mtd_partition *part, 37 int partno, uint64_t cur_offset) 38 { 39 struct mtd_info *master = mtd_get_master(parent); 40 int wr_alignment = (parent->flags & MTD_NO_ERASE) ? 41 master->writesize : master->erasesize; 42 u64 parent_size = mtd_is_partition(parent) ? 43 parent->part.size : parent->size; 44 struct mtd_info *child; 45 u32 remainder; 46 char *name; 47 u64 tmp; 48 49 /* allocate the partition structure */ 50 child = kzalloc(sizeof(*child), GFP_KERNEL); 51 name = kstrdup(part->name, GFP_KERNEL); 52 if (!name || !child) { 53 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", 54 parent->name); 55 kfree(name); 56 kfree(child); 57 return ERR_PTR(-ENOMEM); 58 } 59 60 /* set up the MTD object for this partition */ 61 child->type = parent->type; 62 child->part.flags = parent->flags & ~part->mask_flags; 63 child->part.flags |= part->add_flags; 64 child->flags = child->part.flags; 65 child->part.size = part->size; 66 child->writesize = parent->writesize; 67 child->writebufsize = parent->writebufsize; 68 child->oobsize = parent->oobsize; 69 child->oobavail = parent->oobavail; 70 child->subpage_sft = parent->subpage_sft; 71 72 child->name = name; 73 child->owner = parent->owner; 74 75 /* NOTE: Historically, we didn't arrange MTDs as a tree out of 76 * concern for showing the same data in multiple partitions. 77 * However, it is very useful to have the master node present, 78 * so the MTD_PARTITIONED_MASTER option allows that. The master 79 * will have device nodes etc only if this is set, so make the 80 * parent conditional on that option. Note, this is a way to 81 * distinguish between the parent and its partitions in sysfs. 82 */ 83 child->dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ? 84 &parent->dev : parent->dev.parent; 85 child->dev.of_node = part->of_node; 86 child->parent = parent; 87 child->part.offset = part->offset; 88 INIT_LIST_HEAD(&child->partitions); 89 90 if (child->part.offset == MTDPART_OFS_APPEND) 91 child->part.offset = cur_offset; 92 if (child->part.offset == MTDPART_OFS_NXTBLK) { 93 tmp = cur_offset; 94 child->part.offset = cur_offset; 95 remainder = do_div(tmp, wr_alignment); 96 if (remainder) { 97 child->part.offset += wr_alignment - remainder; 98 printk(KERN_NOTICE "Moving partition %d: " 99 "0x%012llx -> 0x%012llx\n", partno, 100 (unsigned long long)cur_offset, 101 child->part.offset); 102 } 103 } 104 if (child->part.offset == MTDPART_OFS_RETAIN) { 105 child->part.offset = cur_offset; 106 if (parent_size - child->part.offset >= child->part.size) { 107 child->part.size = parent_size - child->part.offset - 108 child->part.size; 109 } else { 110 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n", 111 part->name, parent_size - child->part.offset, 112 child->part.size); 113 /* register to preserve ordering */ 114 goto out_register; 115 } 116 } 117 if (child->part.size == MTDPART_SIZ_FULL) 118 child->part.size = parent_size - child->part.offset; 119 120 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", 121 child->part.offset, child->part.offset + child->part.size, 122 child->name); 123 124 /* let's do some sanity checks */ 125 if (child->part.offset >= parent_size) { 126 /* let's register it anyway to preserve ordering */ 127 child->part.offset = 0; 128 child->part.size = 0; 129 130 /* Initialize ->erasesize to make add_mtd_device() happy. */ 131 child->erasesize = parent->erasesize; 132 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", 133 part->name); 134 goto out_register; 135 } 136 if (child->part.offset + child->part.size > parent->size) { 137 child->part.size = parent_size - child->part.offset; 138 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n", 139 part->name, parent->name, child->part.size); 140 } 141 142 if (parent->numeraseregions > 1) { 143 /* Deal with variable erase size stuff */ 144 int i, max = parent->numeraseregions; 145 u64 end = child->part.offset + child->part.size; 146 struct mtd_erase_region_info *regions = parent->eraseregions; 147 148 /* Find the first erase regions which is part of this 149 * partition. */ 150 for (i = 0; i < max && regions[i].offset <= child->part.offset; 151 i++) 152 ; 153 /* The loop searched for the region _behind_ the first one */ 154 if (i > 0) 155 i--; 156 157 /* Pick biggest erasesize */ 158 for (; i < max && regions[i].offset < end; i++) { 159 if (child->erasesize < regions[i].erasesize) 160 child->erasesize = regions[i].erasesize; 161 } 162 BUG_ON(child->erasesize == 0); 163 } else { 164 /* Single erase size */ 165 child->erasesize = master->erasesize; 166 } 167 168 /* 169 * Child erasesize might differ from the parent one if the parent 170 * exposes several regions with different erasesize. Adjust 171 * wr_alignment accordingly. 172 */ 173 if (!(child->flags & MTD_NO_ERASE)) 174 wr_alignment = child->erasesize; 175 176 tmp = mtd_get_master_ofs(child, 0); 177 remainder = do_div(tmp, wr_alignment); 178 if ((child->flags & MTD_WRITEABLE) && remainder) { 179 /* Doesn't start on a boundary of major erase size */ 180 /* FIXME: Let it be writable if it is on a boundary of 181 * _minor_ erase size though */ 182 child->flags &= ~MTD_WRITEABLE; 183 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n", 184 part->name); 185 } 186 187 tmp = mtd_get_master_ofs(child, 0) + child->part.size; 188 remainder = do_div(tmp, wr_alignment); 189 if ((child->flags & MTD_WRITEABLE) && remainder) { 190 child->flags &= ~MTD_WRITEABLE; 191 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n", 192 part->name); 193 } 194 195 child->size = child->part.size; 196 child->ecc_step_size = parent->ecc_step_size; 197 child->ecc_strength = parent->ecc_strength; 198 child->bitflip_threshold = parent->bitflip_threshold; 199 200 if (master->_block_isbad) { 201 uint64_t offs = 0; 202 203 while (offs < child->part.size) { 204 if (mtd_block_isreserved(child, offs)) 205 child->ecc_stats.bbtblocks++; 206 else if (mtd_block_isbad(child, offs)) 207 child->ecc_stats.badblocks++; 208 offs += child->erasesize; 209 } 210 } 211 212 out_register: 213 return child; 214 } 215 216 static ssize_t offset_show(struct device *dev, 217 struct device_attribute *attr, char *buf) 218 { 219 struct mtd_info *mtd = dev_get_drvdata(dev); 220 221 return sysfs_emit(buf, "%lld\n", mtd->part.offset); 222 } 223 static DEVICE_ATTR_RO(offset); /* mtd partition offset */ 224 225 static const struct attribute *mtd_partition_attrs[] = { 226 &dev_attr_offset.attr, 227 NULL 228 }; 229 230 static int mtd_add_partition_attrs(struct mtd_info *new) 231 { 232 int ret = sysfs_create_files(&new->dev.kobj, mtd_partition_attrs); 233 if (ret) 234 printk(KERN_WARNING 235 "mtd: failed to create partition attrs, err=%d\n", ret); 236 return ret; 237 } 238 239 int mtd_add_partition(struct mtd_info *parent, const char *name, 240 long long offset, long long length) 241 { 242 struct mtd_info *master = mtd_get_master(parent); 243 u64 parent_size = mtd_is_partition(parent) ? 244 parent->part.size : parent->size; 245 struct mtd_partition part; 246 struct mtd_info *child; 247 int ret = 0; 248 249 /* the direct offset is expected */ 250 if (offset == MTDPART_OFS_APPEND || 251 offset == MTDPART_OFS_NXTBLK) 252 return -EINVAL; 253 254 if (length == MTDPART_SIZ_FULL) 255 length = parent_size - offset; 256 257 if (length <= 0) 258 return -EINVAL; 259 260 memset(&part, 0, sizeof(part)); 261 part.name = name; 262 part.size = length; 263 part.offset = offset; 264 265 child = allocate_partition(parent, &part, -1, offset); 266 if (IS_ERR(child)) 267 return PTR_ERR(child); 268 269 mutex_lock(&master->master.partitions_lock); 270 list_add_tail(&child->part.node, &parent->partitions); 271 mutex_unlock(&master->master.partitions_lock); 272 273 ret = add_mtd_device(child); 274 if (ret) 275 goto err_remove_part; 276 277 mtd_add_partition_attrs(child); 278 279 return 0; 280 281 err_remove_part: 282 mutex_lock(&master->master.partitions_lock); 283 list_del(&child->part.node); 284 mutex_unlock(&master->master.partitions_lock); 285 286 free_partition(child); 287 288 return ret; 289 } 290 EXPORT_SYMBOL_GPL(mtd_add_partition); 291 292 /** 293 * __mtd_del_partition - delete MTD partition 294 * 295 * @mtd: MTD structure to be deleted 296 * 297 * This function must be called with the partitions mutex locked. 298 */ 299 static int __mtd_del_partition(struct mtd_info *mtd) 300 { 301 struct mtd_info *child, *next; 302 int err; 303 304 list_for_each_entry_safe(child, next, &mtd->partitions, part.node) { 305 err = __mtd_del_partition(child); 306 if (err) 307 return err; 308 } 309 310 sysfs_remove_files(&mtd->dev.kobj, mtd_partition_attrs); 311 312 err = del_mtd_device(mtd); 313 if (err) 314 return err; 315 316 list_del(&mtd->part.node); 317 free_partition(mtd); 318 319 return 0; 320 } 321 322 /* 323 * This function unregisters and destroy all slave MTD objects which are 324 * attached to the given MTD object, recursively. 325 */ 326 static int __del_mtd_partitions(struct mtd_info *mtd) 327 { 328 struct mtd_info *child, *next; 329 LIST_HEAD(tmp_list); 330 int ret, err = 0; 331 332 list_for_each_entry_safe(child, next, &mtd->partitions, part.node) { 333 if (mtd_has_partitions(child)) 334 __del_mtd_partitions(child); 335 336 pr_info("Deleting %s MTD partition\n", child->name); 337 ret = del_mtd_device(child); 338 if (ret < 0) { 339 pr_err("Error when deleting partition \"%s\" (%d)\n", 340 child->name, ret); 341 err = ret; 342 continue; 343 } 344 345 list_del(&child->part.node); 346 free_partition(child); 347 } 348 349 return err; 350 } 351 352 int del_mtd_partitions(struct mtd_info *mtd) 353 { 354 struct mtd_info *master = mtd_get_master(mtd); 355 int ret; 356 357 pr_info("Deleting MTD partitions on \"%s\":\n", mtd->name); 358 359 mutex_lock(&master->master.partitions_lock); 360 ret = __del_mtd_partitions(mtd); 361 mutex_unlock(&master->master.partitions_lock); 362 363 return ret; 364 } 365 366 int mtd_del_partition(struct mtd_info *mtd, int partno) 367 { 368 struct mtd_info *child, *master = mtd_get_master(mtd); 369 int ret = -EINVAL; 370 371 mutex_lock(&master->master.partitions_lock); 372 list_for_each_entry(child, &mtd->partitions, part.node) { 373 if (child->index == partno) { 374 ret = __mtd_del_partition(child); 375 break; 376 } 377 } 378 mutex_unlock(&master->master.partitions_lock); 379 380 return ret; 381 } 382 EXPORT_SYMBOL_GPL(mtd_del_partition); 383 384 /* 385 * This function, given a parent MTD object and a partition table, creates 386 * and registers the child MTD objects which are bound to the parent according 387 * to the partition definitions. 388 * 389 * For historical reasons, this function's caller only registers the parent 390 * if the MTD_PARTITIONED_MASTER config option is set. 391 */ 392 393 int add_mtd_partitions(struct mtd_info *parent, 394 const struct mtd_partition *parts, 395 int nbparts) 396 { 397 struct mtd_info *child, *master = mtd_get_master(parent); 398 uint64_t cur_offset = 0; 399 int i, ret; 400 401 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", 402 nbparts, parent->name); 403 404 for (i = 0; i < nbparts; i++) { 405 child = allocate_partition(parent, parts + i, i, cur_offset); 406 if (IS_ERR(child)) { 407 ret = PTR_ERR(child); 408 goto err_del_partitions; 409 } 410 411 mutex_lock(&master->master.partitions_lock); 412 list_add_tail(&child->part.node, &parent->partitions); 413 mutex_unlock(&master->master.partitions_lock); 414 415 ret = add_mtd_device(child); 416 if (ret) { 417 mutex_lock(&master->master.partitions_lock); 418 list_del(&child->part.node); 419 mutex_unlock(&master->master.partitions_lock); 420 421 free_partition(child); 422 goto err_del_partitions; 423 } 424 425 mtd_add_partition_attrs(child); 426 427 /* Look for subpartitions */ 428 parse_mtd_partitions(child, parts[i].types, NULL); 429 430 cur_offset = child->part.offset + child->part.size; 431 } 432 433 return 0; 434 435 err_del_partitions: 436 del_mtd_partitions(master); 437 438 return ret; 439 } 440 441 static DEFINE_SPINLOCK(part_parser_lock); 442 static LIST_HEAD(part_parsers); 443 444 static struct mtd_part_parser *mtd_part_parser_get(const char *name) 445 { 446 struct mtd_part_parser *p, *ret = NULL; 447 448 spin_lock(&part_parser_lock); 449 450 list_for_each_entry(p, &part_parsers, list) 451 if (!strcmp(p->name, name) && try_module_get(p->owner)) { 452 ret = p; 453 break; 454 } 455 456 spin_unlock(&part_parser_lock); 457 458 return ret; 459 } 460 461 static inline void mtd_part_parser_put(const struct mtd_part_parser *p) 462 { 463 module_put(p->owner); 464 } 465 466 /* 467 * Many partition parsers just expected the core to kfree() all their data in 468 * one chunk. Do that by default. 469 */ 470 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts, 471 int nr_parts) 472 { 473 kfree(pparts); 474 } 475 476 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner) 477 { 478 p->owner = owner; 479 480 if (!p->cleanup) 481 p->cleanup = &mtd_part_parser_cleanup_default; 482 483 spin_lock(&part_parser_lock); 484 list_add(&p->list, &part_parsers); 485 spin_unlock(&part_parser_lock); 486 487 return 0; 488 } 489 EXPORT_SYMBOL_GPL(__register_mtd_parser); 490 491 void deregister_mtd_parser(struct mtd_part_parser *p) 492 { 493 spin_lock(&part_parser_lock); 494 list_del(&p->list); 495 spin_unlock(&part_parser_lock); 496 } 497 EXPORT_SYMBOL_GPL(deregister_mtd_parser); 498 499 /* 500 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you 501 * are changing this array! 502 */ 503 static const char * const default_mtd_part_types[] = { 504 "cmdlinepart", 505 "ofpart", 506 NULL 507 }; 508 509 /* Check DT only when looking for subpartitions. */ 510 static const char * const default_subpartition_types[] = { 511 "ofpart", 512 NULL 513 }; 514 515 static int mtd_part_do_parse(struct mtd_part_parser *parser, 516 struct mtd_info *master, 517 struct mtd_partitions *pparts, 518 struct mtd_part_parser_data *data) 519 { 520 int ret; 521 522 ret = (*parser->parse_fn)(master, &pparts->parts, data); 523 pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret); 524 if (ret <= 0) 525 return ret; 526 527 pr_notice("%d %s partitions found on MTD device %s\n", ret, 528 parser->name, master->name); 529 530 pparts->nr_parts = ret; 531 pparts->parser = parser; 532 533 return ret; 534 } 535 536 /** 537 * mtd_part_get_compatible_parser - find MTD parser by a compatible string 538 * 539 * @compat: compatible string describing partitions in a device tree 540 * 541 * MTD parsers can specify supported partitions by providing a table of 542 * compatibility strings. This function finds a parser that advertises support 543 * for a passed value of "compatible". 544 */ 545 static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat) 546 { 547 struct mtd_part_parser *p, *ret = NULL; 548 549 spin_lock(&part_parser_lock); 550 551 list_for_each_entry(p, &part_parsers, list) { 552 const struct of_device_id *matches; 553 554 matches = p->of_match_table; 555 if (!matches) 556 continue; 557 558 for (; matches->compatible[0]; matches++) { 559 if (!strcmp(matches->compatible, compat) && 560 try_module_get(p->owner)) { 561 ret = p; 562 break; 563 } 564 } 565 566 if (ret) 567 break; 568 } 569 570 spin_unlock(&part_parser_lock); 571 572 return ret; 573 } 574 575 static int mtd_part_of_parse(struct mtd_info *master, 576 struct mtd_partitions *pparts) 577 { 578 struct mtd_part_parser *parser; 579 struct device_node *np; 580 struct device_node *child; 581 struct property *prop; 582 struct device *dev; 583 const char *compat; 584 const char *fixed = "fixed-partitions"; 585 int ret, err = 0; 586 587 dev = &master->dev; 588 /* Use parent device (controller) if the top level MTD is not registered */ 589 if (!IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) && !mtd_is_partition(master)) 590 dev = master->dev.parent; 591 592 np = mtd_get_of_node(master); 593 if (mtd_is_partition(master)) 594 of_node_get(np); 595 else 596 np = of_get_child_by_name(np, "partitions"); 597 598 /* 599 * Don't create devices that are added to a bus but will never get 600 * probed. That'll cause fw_devlink to block probing of consumers of 601 * this partition until the partition device is probed. 602 */ 603 for_each_child_of_node(np, child) 604 if (of_device_is_compatible(child, "nvmem-cells")) 605 of_node_set_flag(child, OF_POPULATED); 606 607 of_property_for_each_string(np, "compatible", prop, compat) { 608 parser = mtd_part_get_compatible_parser(compat); 609 if (!parser) 610 continue; 611 ret = mtd_part_do_parse(parser, master, pparts, NULL); 612 if (ret > 0) { 613 of_platform_populate(np, NULL, NULL, dev); 614 of_node_put(np); 615 return ret; 616 } 617 mtd_part_parser_put(parser); 618 if (ret < 0 && !err) 619 err = ret; 620 } 621 of_platform_populate(np, NULL, NULL, dev); 622 of_node_put(np); 623 624 /* 625 * For backward compatibility we have to try the "fixed-partitions" 626 * parser. It supports old DT format with partitions specified as a 627 * direct subnodes of a flash device DT node without any compatibility 628 * specified we could match. 629 */ 630 parser = mtd_part_parser_get(fixed); 631 if (!parser && !request_module("%s", fixed)) 632 parser = mtd_part_parser_get(fixed); 633 if (parser) { 634 ret = mtd_part_do_parse(parser, master, pparts, NULL); 635 if (ret > 0) 636 return ret; 637 mtd_part_parser_put(parser); 638 if (ret < 0 && !err) 639 err = ret; 640 } 641 642 return err; 643 } 644 645 /** 646 * parse_mtd_partitions - parse and register MTD partitions 647 * 648 * @master: the master partition (describes whole MTD device) 649 * @types: names of partition parsers to try or %NULL 650 * @data: MTD partition parser-specific data 651 * 652 * This function tries to find & register partitions on MTD device @master. It 653 * uses MTD partition parsers, specified in @types. However, if @types is %NULL, 654 * then the default list of parsers is used. The default list contains only the 655 * "cmdlinepart" and "ofpart" parsers ATM. 656 * Note: If there are more then one parser in @types, the kernel only takes the 657 * partitions parsed out by the first parser. 658 * 659 * This function may return: 660 * o a negative error code in case of failure 661 * o number of found partitions otherwise 662 */ 663 int parse_mtd_partitions(struct mtd_info *master, const char *const *types, 664 struct mtd_part_parser_data *data) 665 { 666 struct mtd_partitions pparts = { }; 667 struct mtd_part_parser *parser; 668 int ret, err = 0; 669 670 if (!types) 671 types = mtd_is_partition(master) ? default_subpartition_types : 672 default_mtd_part_types; 673 674 for ( ; *types; types++) { 675 /* 676 * ofpart is a special type that means OF partitioning info 677 * should be used. It requires a bit different logic so it is 678 * handled in a separated function. 679 */ 680 if (!strcmp(*types, "ofpart")) { 681 ret = mtd_part_of_parse(master, &pparts); 682 } else { 683 pr_debug("%s: parsing partitions %s\n", master->name, 684 *types); 685 parser = mtd_part_parser_get(*types); 686 if (!parser && !request_module("%s", *types)) 687 parser = mtd_part_parser_get(*types); 688 pr_debug("%s: got parser %s\n", master->name, 689 parser ? parser->name : NULL); 690 if (!parser) 691 continue; 692 ret = mtd_part_do_parse(parser, master, &pparts, data); 693 if (ret <= 0) 694 mtd_part_parser_put(parser); 695 } 696 /* Found partitions! */ 697 if (ret > 0) { 698 err = add_mtd_partitions(master, pparts.parts, 699 pparts.nr_parts); 700 mtd_part_parser_cleanup(&pparts); 701 return err ? err : pparts.nr_parts; 702 } 703 /* 704 * Stash the first error we see; only report it if no parser 705 * succeeds 706 */ 707 if (ret < 0 && !err) 708 err = ret; 709 } 710 return err; 711 } 712 713 void mtd_part_parser_cleanup(struct mtd_partitions *parts) 714 { 715 const struct mtd_part_parser *parser; 716 717 if (!parts) 718 return; 719 720 parser = parts->parser; 721 if (parser) { 722 if (parser->cleanup) 723 parser->cleanup(parts->parts, parts->nr_parts); 724 725 mtd_part_parser_put(parser); 726 } 727 } 728 729 /* Returns the size of the entire flash chip */ 730 uint64_t mtd_get_device_size(const struct mtd_info *mtd) 731 { 732 struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd); 733 734 return master->size; 735 } 736 EXPORT_SYMBOL_GPL(mtd_get_device_size); 737