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