1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Simple MTD partitioning layer
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>
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>
24 * MTD methods which simply translate the effective address and pass through
25 * to the _real_ device.
28 static inline void free_partition(struct mtd_info
*mtd
)
34 static struct mtd_info
*allocate_partition(struct mtd_info
*parent
,
35 const struct mtd_partition
*part
,
36 int partno
, uint64_t cur_offset
)
38 int wr_alignment
= (parent
->flags
& MTD_NO_ERASE
) ? parent
->writesize
:
40 struct mtd_info
*child
, *master
= mtd_get_master(parent
);
45 /* allocate the partition structure */
46 child
= kzalloc(sizeof(*child
), GFP_KERNEL
);
47 name
= kstrdup(part
->name
, GFP_KERNEL
);
48 if (!name
|| !child
) {
49 printk(KERN_ERR
"memory allocation error while creating partitions for \"%s
\"\n",
53 return ERR_PTR(-ENOMEM);
56 /* set up the MTD object for this partition */
57 child->type = parent->type;
58 child->part.flags = parent->flags & ~part->mask_flags;
59 child->flags = child->part.flags;
60 child->size = part->size;
61 child->writesize = parent->writesize;
62 child->writebufsize = parent->writebufsize;
63 child->oobsize = parent->oobsize;
64 child->oobavail = parent->oobavail;
65 child->subpage_sft = parent->subpage_sft;
68 child->owner = parent->owner;
70 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
71 * concern for showing the same data in multiple partitions.
72 * However, it is very useful to have the master node present,
73 * so the MTD_PARTITIONED_MASTER option allows that. The master
74 * will have device nodes etc only if this is set, so make the
75 * parent conditional on that option. Note, this is a way to
76 * distinguish between the parent and its partitions in sysfs.
78 child->dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
79 &parent->dev : parent->dev.parent;
80 child->dev.of_node = part->of_node;
81 child->parent = parent;
82 child->part.offset = part->offset;
83 INIT_LIST_HEAD(&child->partitions);
85 if (child->part.offset == MTDPART_OFS_APPEND)
86 child->part.offset = cur_offset;
87 if (child->part.offset == MTDPART_OFS_NXTBLK) {
89 child->part.offset = cur_offset;
90 remainder = do_div(tmp, wr_alignment);
92 child->part.offset += wr_alignment - remainder;
93 printk(KERN_NOTICE "Moving partition
%d
: "
94 "0x
%012llx
-> 0x
%012llx
\n", partno,
95 (unsigned long long)cur_offset,
99 if (child->part.offset == MTDPART_OFS_RETAIN) {
100 child->part.offset = cur_offset;
101 if (parent->size - child->part.offset >= child->size) {
102 child->size = parent->size - child->part.offset -
105 printk(KERN_ERR "mtd partition
\"%s
\" doesn
't have enough space: %#llx < %#llx, disabled\n",
106 part->name, parent->size - child->part.offset,
108 /* register to preserve ordering */
112 if (child->size == MTDPART_SIZ_FULL)
113 child->size = parent->size - child->part.offset;
115 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n",
116 child->part.offset, child->part.offset + child->size,
119 /* let's
do some sanity checks */
120 if (child
->part
.offset
>= parent
->size
) {
121 /* let's register it anyway to preserve ordering */
122 child
->part
.offset
= 0;
125 /* Initialize ->erasesize to make add_mtd_device() happy. */
126 child
->erasesize
= parent
->erasesize
;
127 printk(KERN_ERR
"mtd: partition \"%s
\" is out of reach
-- disabled
\n",
131 if (child->part.offset + child->size > parent->size) {
132 child->size = parent->size - child->part.offset;
133 printk(KERN_WARNING"mtd
: partition
\"%s
\" extends beyond the end of device
\"%s
\" -- size truncated to
%#llx\n",
134 part->name, parent->name, child->size);
136 if (parent->numeraseregions > 1) {
137 /* Deal with variable erase size stuff */
138 int i, max = parent->numeraseregions;
139 u64 end = child->part.offset + child->size;
140 struct mtd_erase_region_info *regions = parent->eraseregions;
142 /* Find the first erase regions which is part of this
144 for (i = 0; i < max && regions[i].offset <= child->part.offset;
147 /* The loop searched for the region _behind_ the first one */
151 /* Pick biggest erasesize */
152 for (; i < max && regions[i].offset < end; i++) {
153 if (child->erasesize < regions[i].erasesize)
154 child->erasesize = regions[i].erasesize;
156 BUG_ON(child->erasesize == 0);
158 /* Single erase size */
159 child->erasesize = parent->erasesize;
163 * Child erasesize might differ from the parent one if the parent
164 * exposes several regions with different erasesize. Adjust
165 * wr_alignment accordingly.
167 if (!(child->flags & MTD_NO_ERASE))
168 wr_alignment = child->erasesize;
170 tmp = mtd_get_master_ofs(child, 0);
171 remainder = do_div(tmp, wr_alignment);
172 if ((child->flags & MTD_WRITEABLE) && remainder) {
173 /* Doesn't start on a boundary of major erase size */
174 /* FIXME: Let it be writable if it is on a boundary of
175 * _minor_ erase size though */
176 child->flags &= ~MTD_WRITEABLE;
177 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
181 tmp = mtd_get_master_ofs(child, 0) + child->size;
182 remainder = do_div(tmp, wr_alignment);
183 if ((child->flags & MTD_WRITEABLE) && remainder) {
184 child->flags &= ~MTD_WRITEABLE;
185 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
189 child->ecc_step_size = parent->ecc_step_size;
190 child->ecc_strength = parent->ecc_strength;
191 child->bitflip_threshold = parent->bitflip_threshold;
193 if (master->_block_isbad) {
196 while (offs < child->size) {
197 if (mtd_block_isreserved(child, offs))
198 child->ecc_stats.bbtblocks++;
199 else if (mtd_block_isbad(child, offs))
200 child->ecc_stats.badblocks++;
201 offs += child->erasesize;
209 static ssize_t mtd_partition_offset_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
212 struct mtd_info *mtd = dev_get_drvdata(dev);
214 return snprintf(buf, PAGE_SIZE, "%lld\n", mtd->part.offset);
217 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
219 static const struct attribute *mtd_partition_attrs[] = {
220 &dev_attr_offset.attr,
224 static int mtd_add_partition_attrs(struct mtd_info *new)
226 int ret = sysfs_create_files(&new->dev.kobj, mtd_partition_attrs);
229 "mtd: failed to create partition attrs, err=%d\n", ret);
233 int mtd_add_partition(struct mtd_info *parent, const char *name,
234 long long offset, long long length)
236 struct mtd_info *master = mtd_get_master(parent);
237 struct mtd_partition part;
238 struct mtd_info *child;
241 /* the direct offset is expected */
242 if (offset == MTDPART_OFS_APPEND ||
243 offset == MTDPART_OFS_NXTBLK)
246 if (length == MTDPART_SIZ_FULL)
247 length = parent->size - offset;
252 memset(&part, 0, sizeof(part));
255 part.offset = offset;
257 child = allocate_partition(parent, &part, -1, offset);
259 return PTR_ERR(child);
261 mutex_lock(&master->master.partitions_lock);
262 list_add_tail(&child->part.node, &parent->partitions);
263 mutex_unlock(&master->master.partitions_lock);
265 ret = add_mtd_device(child);
267 goto err_remove_part;
269 mtd_add_partition_attrs(child);
274 mutex_lock(&master->master.partitions_lock);
275 list_del(&child->part.node);
276 mutex_unlock(&master->master.partitions_lock);
278 free_partition(child);
282 EXPORT_SYMBOL_GPL(mtd_add_partition);
285 * __mtd_del_partition - delete MTD partition
287 * @priv: MTD structure to be deleted
289 * This function must be called with the partitions mutex locked.
291 static int __mtd_del_partition(struct mtd_info *mtd)
293 struct mtd_info *child, *next;
296 list_for_each_entry_safe(child, next, &mtd->partitions, part.node) {
297 err = __mtd_del_partition(child);
302 sysfs_remove_files(&mtd->dev.kobj, mtd_partition_attrs);
304 err = del_mtd_device(mtd);
308 list_del(&child->part.node);
315 * This function unregisters and destroy all slave MTD objects which are
316 * attached to the given MTD object, recursively.
318 static int __del_mtd_partitions(struct mtd_info *mtd)
320 struct mtd_info *child, *next;
324 list_for_each_entry_safe(child, next, &mtd->partitions, part.node) {
325 if (mtd_has_partitions(child))
326 del_mtd_partitions(child);
328 pr_info("Deleting %s MTD partition\n", child->name);
329 ret = del_mtd_device(child);
331 pr_err("Error when deleting partition \"%s\" (%d)\n",
337 list_del(&child->part.node);
338 free_partition(child);
344 int del_mtd_partitions(struct mtd_info *mtd)
346 struct mtd_info *master = mtd_get_master(mtd);
349 pr_info("Deleting MTD partitions on \"%s\":\n", mtd->name);
351 mutex_lock(&master->master.partitions_lock);
352 ret = __del_mtd_partitions(mtd);
353 mutex_unlock(&master->master.partitions_lock);
358 int mtd_del_partition(struct mtd_info *mtd, int partno)
360 struct mtd_info *child, *master = mtd_get_master(mtd);
363 mutex_lock(&master->master.partitions_lock);
364 list_for_each_entry(child, &mtd->partitions, part.node) {
365 if (child->index == partno) {
366 ret = __mtd_del_partition(child);
370 mutex_unlock(&master->master.partitions_lock);
374 EXPORT_SYMBOL_GPL(mtd_del_partition);
377 * This function, given a parent MTD object and a partition table, creates
378 * and registers the child MTD objects which are bound to the parent according
379 * to the partition definitions.
381 * For historical reasons, this function's caller only registers the parent
382 * if the MTD_PARTITIONED_MASTER config option is set.
385 int add_mtd_partitions(struct mtd_info *parent,
386 const struct mtd_partition *parts,
389 struct mtd_info *child, *master = mtd_get_master(parent);
390 uint64_t cur_offset = 0;
393 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n",
394 nbparts, parent->name);
396 for (i = 0; i < nbparts; i++) {
397 child = allocate_partition(parent, parts + i, i, cur_offset);
399 ret = PTR_ERR(child);
400 goto err_del_partitions;
403 mutex_lock(&master->master.partitions_lock);
404 list_add_tail(&child->part.node, &parent->partitions);
405 mutex_unlock(&master->master.partitions_lock);
407 ret = add_mtd_device(child);
409 mutex_lock(&master->master.partitions_lock);
410 list_del(&child->part.node);
411 mutex_unlock(&master->master.partitions_lock);
413 free_partition(child);
414 goto err_del_partitions;
417 mtd_add_partition_attrs(child);
419 /* Look for subpartitions */
420 parse_mtd_partitions(child, parts[i].types, NULL);
422 cur_offset = child->part.offset + child->size;
428 del_mtd_partitions(master);
433 static DEFINE_SPINLOCK(part_parser_lock);
434 static LIST_HEAD(part_parsers);
436 static struct mtd_part_parser *mtd_part_parser_get(const char *name)
438 struct mtd_part_parser *p, *ret = NULL;
440 spin_lock(&part_parser_lock);
442 list_for_each_entry(p, &part_parsers, list)
443 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
448 spin_unlock(&part_parser_lock);
453 static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
455 module_put(p->owner);
459 * Many partition parsers just expected the core to kfree() all their data in
460 * one chunk. Do that by default.
462 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
468 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
473 p->cleanup = &mtd_part_parser_cleanup_default;
475 spin_lock(&part_parser_lock);
476 list_add(&p->list, &part_parsers);
477 spin_unlock(&part_parser_lock);
481 EXPORT_SYMBOL_GPL(__register_mtd_parser);
483 void deregister_mtd_parser(struct mtd_part_parser *p)
485 spin_lock(&part_parser_lock);
487 spin_unlock(&part_parser_lock);
489 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
492 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
493 * are changing this array!
495 static const char * const default_mtd_part_types[] = {
501 /* Check DT only when looking for subpartitions. */
502 static const char * const default_subpartition_types[] = {
507 static int mtd_part_do_parse(struct mtd_part_parser *parser,
508 struct mtd_info *master,
509 struct mtd_partitions *pparts,
510 struct mtd_part_parser_data *data)
514 ret = (*parser->parse_fn)(master, &pparts->parts, data);
515 pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
519 pr_notice("%d %s partitions found on MTD device %s\n", ret,
520 parser->name, master->name);
522 pparts->nr_parts = ret;
523 pparts->parser = parser;
529 * mtd_part_get_compatible_parser - find MTD parser by a compatible string
531 * @compat: compatible string describing partitions in a device tree
533 * MTD parsers can specify supported partitions by providing a table of
534 * compatibility strings. This function finds a parser that advertises support
535 * for a passed value of "compatible".
537 static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
539 struct mtd_part_parser *p, *ret = NULL;
541 spin_lock(&part_parser_lock);
543 list_for_each_entry(p, &part_parsers, list) {
544 const struct of_device_id *matches;
546 matches = p->of_match_table;
550 for (; matches->compatible[0]; matches++) {
551 if (!strcmp(matches->compatible, compat) &&
552 try_module_get(p->owner)) {
562 spin_unlock(&part_parser_lock);
567 static int mtd_part_of_parse(struct mtd_info *master,
568 struct mtd_partitions *pparts)
570 struct mtd_part_parser *parser;
571 struct device_node *np;
572 struct property *prop;
574 const char *fixed = "fixed-partitions";
577 np = mtd_get_of_node(master);
578 if (mtd_is_partition(master))
581 np = of_get_child_by_name(np, "partitions");
583 of_property_for_each_string(np, "compatible", prop, compat) {
584 parser = mtd_part_get_compatible_parser(compat);
587 ret = mtd_part_do_parse(parser, master, pparts, NULL);
592 mtd_part_parser_put(parser);
599 * For backward compatibility we have to try the "fixed-partitions"
600 * parser. It supports old DT format with partitions specified as a
601 * direct subnodes of a flash device DT node without any compatibility
602 * specified we could match.
604 parser = mtd_part_parser_get(fixed);
605 if (!parser && !request_module("%s", fixed))
606 parser = mtd_part_parser_get(fixed);
608 ret = mtd_part_do_parse(parser, master, pparts, NULL);
611 mtd_part_parser_put(parser);
620 * parse_mtd_partitions - parse and register MTD partitions
622 * @master: the master partition (describes whole MTD device)
623 * @types: names of partition parsers to try or %NULL
624 * @data: MTD partition parser-specific data
626 * This function tries to find & register partitions on MTD device @master. It
627 * uses MTD partition parsers, specified in @types. However, if @types is %NULL,
628 * then the default list of parsers is used. The default list contains only the
629 * "cmdlinepart" and "ofpart" parsers ATM.
630 * Note: If there are more then one parser in @types, the kernel only takes the
631 * partitions parsed out by the first parser.
633 * This function may return:
634 * o a negative error code in case of failure
635 * o number of found partitions otherwise
637 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
638 struct mtd_part_parser_data *data)
640 struct mtd_partitions pparts = { };
641 struct mtd_part_parser *parser;
645 types = mtd_is_partition(master) ? default_subpartition_types :
646 default_mtd_part_types;
648 for ( ; *types; types++) {
650 * ofpart is a special type that means OF partitioning info
651 * should be used. It requires a bit different logic so it is
652 * handled in a separated function.
654 if (!strcmp(*types, "ofpart")) {
655 ret = mtd_part_of_parse(master, &pparts);
657 pr_debug("%s: parsing partitions %s\n", master->name,
659 parser = mtd_part_parser_get(*types);
660 if (!parser && !request_module("%s", *types))
661 parser = mtd_part_parser_get(*types);
662 pr_debug("%s: got parser %s\n", master->name,
663 parser ? parser->name : NULL);
666 ret = mtd_part_do_parse(parser, master, &pparts, data);
668 mtd_part_parser_put(parser);
670 /* Found partitions! */
672 err = add_mtd_partitions(master, pparts.parts,
674 mtd_part_parser_cleanup(&pparts);
675 return err ? err : pparts.nr_parts;
678 * Stash the first error we see; only report it if no parser
687 void mtd_part_parser_cleanup(struct mtd_partitions *parts)
689 const struct mtd_part_parser *parser;
694 parser = parts->parser;
697 parser->cleanup(parts->parts, parts->nr_parts);
699 mtd_part_parser_put(parser);
703 /* Returns the size of the entire flash chip */
704 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
706 struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
710 EXPORT_SYMBOL_GPL(mtd_get_device_size);