blob: 95d47422bbf2039f9e92bc7d09e08c548d7e3b0a [file] [log] [blame]
Thomas Gleixnerfd534e92019-05-23 11:14:39 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
3 * Simple MTD partitioning layer
4 *
David Woodhousea1452a32010-08-08 20:58:20 +01005 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
6 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
7 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
Thomas Gleixner97894cd2005-11-07 11:15:26 +00008 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07009
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070015#include <linux/kmod.h>
16#include <linux/mtd/mtd.h>
17#include <linux/mtd/partitions.h>
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +030018#include <linux/err.h>
Rafał Miłecki5b644aa2018-03-14 13:10:42 +010019#include <linux/of.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020
Jamie Ileseea72d52011-05-23 10:23:42 +010021#include "mtdcore.h"
22
Thomas Gleixner97894cd2005-11-07 11:15:26 +000023/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 * MTD methods which simply translate the effective address and pass through
25 * to the _real_ device.
26 */
27
Miquel Raynal46b58892020-01-14 10:09:52 +010028static inline void free_partition(struct mtd_info *mtd)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Miquel Raynal46b58892020-01-14 10:09:52 +010030 kfree(mtd->name);
31 kfree(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -070032}
33
Miquel Raynal46b58892020-01-14 10:09:52 +010034static struct mtd_info *allocate_partition(struct mtd_info *parent,
35 const struct mtd_partition *part,
36 int partno, uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +090037{
Boris Brezillon9e3307a2020-05-03 17:53:37 +020038 struct mtd_info *master = mtd_get_master(parent);
39 int wr_alignment = (parent->flags & MTD_NO_ERASE) ?
40 master->writesize : master->erasesize;
41 u64 parent_size = mtd_is_partition(parent) ?
42 parent->part.size : parent->size;
43 struct mtd_info *child;
Chris Packham1eeef2d2017-06-09 15:58:31 +120044 u32 remainder;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +030045 char *name;
Chris Packham1eeef2d2017-06-09 15:58:31 +120046 u64 tmp;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +090047
48 /* allocate the partition structure */
Miquel Raynal46b58892020-01-14 10:09:52 +010049 child = kzalloc(sizeof(*child), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +030050 name = kstrdup(part->name, GFP_KERNEL);
Miquel Raynal46b58892020-01-14 10:09:52 +010051 if (!name || !child) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +090052 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020053 parent->name);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +030054 kfree(name);
Miquel Raynal46b58892020-01-14 10:09:52 +010055 kfree(child);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +030056 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +090057 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +090058
59 /* set up the MTD object for this partition */
Miquel Raynal46b58892020-01-14 10:09:52 +010060 child->type = parent->type;
61 child->part.flags = parent->flags & ~part->mask_flags;
Boris Brezillon9e3307a2020-05-03 17:53:37 +020062 child->part.flags |= part->add_flags;
Miquel Raynal46b58892020-01-14 10:09:52 +010063 child->flags = child->part.flags;
Boris Brezillon9e3307a2020-05-03 17:53:37 +020064 child->part.size = part->size;
Miquel Raynal46b58892020-01-14 10:09:52 +010065 child->writesize = parent->writesize;
66 child->writebufsize = parent->writebufsize;
67 child->oobsize = parent->oobsize;
68 child->oobavail = parent->oobavail;
69 child->subpage_sft = parent->subpage_sft;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +090070
Miquel Raynal46b58892020-01-14 10:09:52 +010071 child->name = name;
72 child->owner = parent->owner;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +090073
Dan Ehrenberg727dc612015-04-02 15:15:10 -070074 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
75 * concern for showing the same data in multiple partitions.
76 * However, it is very useful to have the master node present,
77 * so the MTD_PARTITIONED_MASTER option allows that. The master
78 * will have device nodes etc only if this is set, so make the
79 * parent conditional on that option. Note, this is a way to
Miquel Raynal46b58892020-01-14 10:09:52 +010080 * distinguish between the parent and its partitions in sysfs.
David Brownell1f24b5a2009-03-26 00:42:41 -070081 */
Miquel Raynal46b58892020-01-14 10:09:52 +010082 child->dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
83 &parent->dev : parent->dev.parent;
84 child->dev.of_node = part->of_node;
85 child->parent = parent;
86 child->part.offset = part->offset;
87 INIT_LIST_HEAD(&child->partitions);
David Brownell1f24b5a2009-03-26 00:42:41 -070088
Miquel Raynal46b58892020-01-14 10:09:52 +010089 if (child->part.offset == MTDPART_OFS_APPEND)
90 child->part.offset = cur_offset;
91 if (child->part.offset == MTDPART_OFS_NXTBLK) {
Chris Packham1eeef2d2017-06-09 15:58:31 +120092 tmp = cur_offset;
Miquel Raynal46b58892020-01-14 10:09:52 +010093 child->part.offset = cur_offset;
Chris Packham1eeef2d2017-06-09 15:58:31 +120094 remainder = do_div(tmp, wr_alignment);
95 if (remainder) {
Miquel Raynal46b58892020-01-14 10:09:52 +010096 child->part.offset += wr_alignment - remainder;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +090097 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +000098 "0x%012llx -> 0x%012llx\n", partno,
Miquel Raynal46b58892020-01-14 10:09:52 +010099 (unsigned long long)cur_offset,
100 child->part.offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900101 }
102 }
Miquel Raynal46b58892020-01-14 10:09:52 +0100103 if (child->part.offset == MTDPART_OFS_RETAIN) {
104 child->part.offset = cur_offset;
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200105 if (parent_size - child->part.offset >= child->part.size) {
106 child->part.size = parent_size - child->part.offset -
107 child->part.size;
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400108 } else {
109 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200110 part->name, parent_size - child->part.offset,
111 child->part.size);
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400112 /* register to preserve ordering */
113 goto out_register;
114 }
115 }
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200116 if (child->part.size == MTDPART_SIZ_FULL)
117 child->part.size = parent_size - child->part.offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900118
Miquel Raynal46b58892020-01-14 10:09:52 +0100119 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n",
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200120 child->part.offset, child->part.offset + child->part.size,
Miquel Raynal46b58892020-01-14 10:09:52 +0100121 child->name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900122
123 /* let's do some sanity checks */
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200124 if (child->part.offset >= parent_size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900125 /* let's register it anyway to preserve ordering */
Miquel Raynal46b58892020-01-14 10:09:52 +0100126 child->part.offset = 0;
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200127 child->part.size = 0;
Boris Brezillonad463512019-01-30 12:55:52 +0100128
129 /* Initialize ->erasesize to make add_mtd_device() happy. */
Miquel Raynal46b58892020-01-14 10:09:52 +0100130 child->erasesize = parent->erasesize;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900131 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900132 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900133 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900134 }
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200135 if (child->part.offset + child->part.size > parent->size) {
136 child->part.size = parent_size - child->part.offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000137 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200138 part->name, parent->name, child->part.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900139 }
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200140
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200141 if (parent->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900142 /* Deal with variable erase size stuff */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200143 int i, max = parent->numeraseregions;
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200144 u64 end = child->part.offset + child->part.size;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200145 struct mtd_erase_region_info *regions = parent->eraseregions;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900146
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900147 /* Find the first erase regions which is part of this
148 * partition. */
Miquel Raynal46b58892020-01-14 10:09:52 +0100149 for (i = 0; i < max && regions[i].offset <= child->part.offset;
150 i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900151 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900152 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700153 if (i > 0)
154 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900155
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900156 /* Pick biggest erasesize */
157 for (; i < max && regions[i].offset < end; i++) {
Miquel Raynal46b58892020-01-14 10:09:52 +0100158 if (child->erasesize < regions[i].erasesize)
159 child->erasesize = regions[i].erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900160 }
Miquel Raynal46b58892020-01-14 10:09:52 +0100161 BUG_ON(child->erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900162 } else {
163 /* Single erase size */
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200164 child->erasesize = master->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900165 }
166
Boris Brezillon7e439682017-09-25 10:19:57 +0200167 /*
Miquel Raynal46b58892020-01-14 10:09:52 +0100168 * Child erasesize might differ from the parent one if the parent
Boris Brezillon7e439682017-09-25 10:19:57 +0200169 * exposes several regions with different erasesize. Adjust
170 * wr_alignment accordingly.
171 */
Miquel Raynal46b58892020-01-14 10:09:52 +0100172 if (!(child->flags & MTD_NO_ERASE))
173 wr_alignment = child->erasesize;
Boris Brezillon7e439682017-09-25 10:19:57 +0200174
Miquel Raynal46b58892020-01-14 10:09:52 +0100175 tmp = mtd_get_master_ofs(child, 0);
Chris Packham1eeef2d2017-06-09 15:58:31 +1200176 remainder = do_div(tmp, wr_alignment);
Miquel Raynal46b58892020-01-14 10:09:52 +0100177 if ((child->flags & MTD_WRITEABLE) && remainder) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900178 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900179 /* FIXME: Let it be writable if it is on a boundary of
180 * _minor_ erase size though */
Miquel Raynal46b58892020-01-14 10:09:52 +0100181 child->flags &= ~MTD_WRITEABLE;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200182 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900183 part->name);
184 }
Chris Packham1eeef2d2017-06-09 15:58:31 +1200185
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200186 tmp = mtd_get_master_ofs(child, 0) + child->part.size;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200187 remainder = do_div(tmp, wr_alignment);
Miquel Raynal46b58892020-01-14 10:09:52 +0100188 if ((child->flags & MTD_WRITEABLE) && remainder) {
189 child->flags &= ~MTD_WRITEABLE;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200190 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900191 part->name);
192 }
193
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200194 child->size = child->part.size;
Miquel Raynal46b58892020-01-14 10:09:52 +0100195 child->ecc_step_size = parent->ecc_step_size;
196 child->ecc_strength = parent->ecc_strength;
197 child->bitflip_threshold = parent->bitflip_threshold;
Mike Dunnd062d4e2012-04-25 12:06:08 -0700198
Miquel Raynal46b58892020-01-14 10:09:52 +0100199 if (master->_block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000200 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900201
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200202 while (offs < child->part.size) {
Miquel Raynal46b58892020-01-14 10:09:52 +0100203 if (mtd_block_isreserved(child, offs))
204 child->ecc_stats.bbtblocks++;
205 else if (mtd_block_isbad(child, offs))
206 child->ecc_stats.badblocks++;
207 offs += child->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900208 }
209 }
210
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900211out_register:
Miquel Raynal46b58892020-01-14 10:09:52 +0100212 return child;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900213}
214
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700215static ssize_t mtd_partition_offset_show(struct device *dev,
216 struct device_attribute *attr, char *buf)
217{
218 struct mtd_info *mtd = dev_get_drvdata(dev);
Miquel Raynal46b58892020-01-14 10:09:52 +0100219
220 return snprintf(buf, PAGE_SIZE, "%lld\n", mtd->part.offset);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700221}
222
223static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
224
225static const struct attribute *mtd_partition_attrs[] = {
226 &dev_attr_offset.attr,
227 NULL
228};
229
Miquel Raynal46b58892020-01-14 10:09:52 +0100230static int mtd_add_partition_attrs(struct mtd_info *new)
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700231{
Miquel Raynal46b58892020-01-14 10:09:52 +0100232 int ret = sysfs_create_files(&new->dev.kobj, mtd_partition_attrs);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700233 if (ret)
234 printk(KERN_WARNING
235 "mtd: failed to create partition attrs, err=%d\n", ret);
236 return ret;
237}
238
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200239int mtd_add_partition(struct mtd_info *parent, const char *name,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300240 long long offset, long long length)
241{
Miquel Raynal46b58892020-01-14 10:09:52 +0100242 struct mtd_info *master = mtd_get_master(parent);
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200243 u64 parent_size = mtd_is_partition(parent) ?
244 parent->part.size : parent->size;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300245 struct mtd_partition part;
Miquel Raynal46b58892020-01-14 10:09:52 +0100246 struct mtd_info *child;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300247 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)
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200255 length = parent_size - offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300256
257 if (length <= 0)
258 return -EINVAL;
259
Brian Norris93867232015-11-11 16:47:52 -0800260 memset(&part, 0, sizeof(part));
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300261 part.name = name;
262 part.size = length;
263 part.offset = offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300264
Miquel Raynal46b58892020-01-14 10:09:52 +0100265 child = allocate_partition(parent, &part, -1, offset);
266 if (IS_ERR(child))
267 return PTR_ERR(child);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300268
Miquel Raynal46b58892020-01-14 10:09:52 +0100269 mutex_lock(&master->master.partitions_lock);
270 list_add_tail(&child->part.node, &parent->partitions);
271 mutex_unlock(&master->master.partitions_lock);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300272
Miquel Raynal46b58892020-01-14 10:09:52 +0100273 ret = add_mtd_device(child);
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100274 if (ret)
275 goto err_remove_part;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300276
Miquel Raynal46b58892020-01-14 10:09:52 +0100277 mtd_add_partition_attrs(child);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700278
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100279 return 0;
280
281err_remove_part:
Miquel Raynal46b58892020-01-14 10:09:52 +0100282 mutex_lock(&master->master.partitions_lock);
283 list_del(&child->part.node);
284 mutex_unlock(&master->master.partitions_lock);
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100285
Miquel Raynal46b58892020-01-14 10:09:52 +0100286 free_partition(child);
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100287
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300288 return ret;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300289}
290EXPORT_SYMBOL_GPL(mtd_add_partition);
291
Rafał Miłecki08263a92017-06-21 08:26:42 +0200292/**
293 * __mtd_del_partition - delete MTD partition
294 *
Miquel Raynal46b58892020-01-14 10:09:52 +0100295 * @priv: MTD structure to be deleted
Rafał Miłecki08263a92017-06-21 08:26:42 +0200296 *
297 * This function must be called with the partitions mutex locked.
298 */
Miquel Raynal46b58892020-01-14 10:09:52 +0100299static int __mtd_del_partition(struct mtd_info *mtd)
Rafał Miłecki08263a92017-06-21 08:26:42 +0200300{
Miquel Raynal46b58892020-01-14 10:09:52 +0100301 struct mtd_info *child, *next;
Rafał Miłecki08263a92017-06-21 08:26:42 +0200302 int err;
303
Miquel Raynal46b58892020-01-14 10:09:52 +0100304 list_for_each_entry_safe(child, next, &mtd->partitions, part.node) {
305 err = __mtd_del_partition(child);
306 if (err)
307 return err;
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200308 }
309
Miquel Raynal46b58892020-01-14 10:09:52 +0100310 sysfs_remove_files(&mtd->dev.kobj, mtd_partition_attrs);
Rafał Miłeckic5ceaba2017-06-21 08:26:43 +0200311
Miquel Raynal46b58892020-01-14 10:09:52 +0100312 err = del_mtd_device(mtd);
Rafał Miłecki08263a92017-06-21 08:26:42 +0200313 if (err)
314 return err;
315
Miquel Raynal46b58892020-01-14 10:09:52 +0100316 list_del(&child->part.node);
317 free_partition(mtd);
Rafał Miłecki08263a92017-06-21 08:26:42 +0200318
319 return 0;
320}
321
322/*
323 * This function unregisters and destroy all slave MTD objects which are
Miquel Raynal46b58892020-01-14 10:09:52 +0100324 * attached to the given MTD object, recursively.
Rafał Miłecki08263a92017-06-21 08:26:42 +0200325 */
Miquel Raynal46b58892020-01-14 10:09:52 +0100326static int __del_mtd_partitions(struct mtd_info *mtd)
Rafał Miłecki08263a92017-06-21 08:26:42 +0200327{
Miquel Raynal46b58892020-01-14 10:09:52 +0100328 struct mtd_info *child, *next;
329 LIST_HEAD(tmp_list);
Rafał Miłecki08263a92017-06-21 08:26:42 +0200330 int ret, err = 0;
331
Miquel Raynal46b58892020-01-14 10:09:52 +0100332 list_for_each_entry_safe(child, next, &mtd->partitions, part.node) {
333 if (mtd_has_partitions(child))
David Bauerc55310e2021-02-17 20:53:20 +0100334 __del_mtd_partitions(child);
Miquel Raynal46b58892020-01-14 10:09:52 +0100335
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;
Rafał Miłecki08263a92017-06-21 08:26:42 +0200343 }
Miquel Raynal46b58892020-01-14 10:09:52 +0100344
345 list_del(&child->part.node);
346 free_partition(child);
347 }
Rafał Miłecki08263a92017-06-21 08:26:42 +0200348
349 return err;
350}
351
Miquel Raynal46b58892020-01-14 10:09:52 +0100352int 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
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200366int mtd_del_partition(struct mtd_info *mtd, int partno)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300367{
Miquel Raynal46b58892020-01-14 10:09:52 +0100368 struct mtd_info *child, *master = mtd_get_master(mtd);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300369 int ret = -EINVAL;
370
Miquel Raynal46b58892020-01-14 10:09:52 +0100371 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);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300375 break;
376 }
Miquel Raynal46b58892020-01-14 10:09:52 +0100377 }
378 mutex_unlock(&master->master.partitions_lock);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300379
380 return ret;
381}
382EXPORT_SYMBOL_GPL(mtd_del_partition);
383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384/*
Miquel Raynal46b58892020-01-14 10:09:52 +0100385 * 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.
David Brownell1f24b5a2009-03-26 00:42:41 -0700388 *
Miquel Raynal46b58892020-01-14 10:09:52 +0100389 * For historical reasons, this function's caller only registers the parent
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700390 * if the MTD_PARTITIONED_MASTER config option is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 */
392
Miquel Raynal46b58892020-01-14 10:09:52 +0100393int add_mtd_partitions(struct mtd_info *parent,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394 const struct mtd_partition *parts,
395 int nbparts)
396{
Miquel Raynal46b58892020-01-14 10:09:52 +0100397 struct mtd_info *child, *master = mtd_get_master(parent);
Adrian Hunter69423d92008-12-10 13:37:21 +0000398 uint64_t cur_offset = 0;
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100399 int i, ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400
Miquel Raynal46b58892020-01-14 10:09:52 +0100401 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n",
402 nbparts, parent->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404 for (i = 0; i < nbparts; i++) {
Miquel Raynal46b58892020-01-14 10:09:52 +0100405 child = allocate_partition(parent, parts + i, i, cur_offset);
406 if (IS_ERR(child)) {
407 ret = PTR_ERR(child);
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100408 goto err_del_partitions;
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200409 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300410
Miquel Raynal46b58892020-01-14 10:09:52 +0100411 mutex_lock(&master->master.partitions_lock);
412 list_add_tail(&child->part.node, &parent->partitions);
413 mutex_unlock(&master->master.partitions_lock);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300414
Miquel Raynal46b58892020-01-14 10:09:52 +0100415 ret = add_mtd_device(child);
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100416 if (ret) {
Miquel Raynal46b58892020-01-14 10:09:52 +0100417 mutex_lock(&master->master.partitions_lock);
418 list_del(&child->part.node);
419 mutex_unlock(&master->master.partitions_lock);
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100420
Miquel Raynal46b58892020-01-14 10:09:52 +0100421 free_partition(child);
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100422 goto err_del_partitions;
423 }
424
Miquel Raynal46b58892020-01-14 10:09:52 +0100425 mtd_add_partition_attrs(child);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300426
Miquel Raynal46b58892020-01-14 10:09:52 +0100427 /* Look for subpartitions */
428 parse_mtd_partitions(child, parts[i].types, NULL);
429
Boris Brezillon9e3307a2020-05-03 17:53:37 +0200430 cur_offset = child->part.offset + child->part.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 }
432
433 return 0;
Boris Brezillon2b6f0092019-01-02 15:36:54 +0100434
435err_del_partitions:
436 del_mtd_partitions(master);
437
438 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
441static DEFINE_SPINLOCK(part_parser_lock);
442static LIST_HEAD(part_parsers);
443
Brian Norris5531ae42015-12-04 15:25:15 -0800444static struct mtd_part_parser *mtd_part_parser_get(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445{
Chris Malley71a928c2008-05-19 20:11:50 +0100446 struct mtd_part_parser *p, *ret = NULL;
447
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 spin_lock(&part_parser_lock);
449
Chris Malley71a928c2008-05-19 20:11:50 +0100450 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
452 ret = p;
453 break;
454 }
Chris Malley71a928c2008-05-19 20:11:50 +0100455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 spin_unlock(&part_parser_lock);
457
458 return ret;
459}
460
Brian Norris5531ae42015-12-04 15:25:15 -0800461static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
462{
463 module_put(p->owner);
464}
Dmitry Eremin-Solenikov953b3bd2011-06-23 15:26:14 +0400465
Brian Norrisadc83bf2015-12-09 10:24:03 -0800466/*
467 * Many partition parsers just expected the core to kfree() all their data in
468 * one chunk. Do that by default.
469 */
470static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
471 int nr_parts)
472{
473 kfree(pparts);
474}
475
Brian Norrisb9eab012015-11-11 19:13:29 -0800476int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477{
Brian Norrisb9eab012015-11-11 19:13:29 -0800478 p->owner = owner;
479
Brian Norrisadc83bf2015-12-09 10:24:03 -0800480 if (!p->cleanup)
481 p->cleanup = &mtd_part_parser_cleanup_default;
482
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 spin_lock(&part_parser_lock);
484 list_add(&p->list, &part_parsers);
485 spin_unlock(&part_parser_lock);
Brian Norrisb9eab012015-11-11 19:13:29 -0800486
487 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488}
Brian Norrisb9eab012015-11-11 19:13:29 -0800489EXPORT_SYMBOL_GPL(__register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Axel Lincf3b2b12013-12-01 18:59:15 +0800491void deregister_mtd_parser(struct mtd_part_parser *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492{
493 spin_lock(&part_parser_lock);
494 list_del(&p->list);
495 spin_unlock(&part_parser_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900497EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300499/*
500 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
501 * are changing this array!
502 */
Artem Bityutskiyccef4dc2013-03-12 10:51:35 +0200503static const char * const default_mtd_part_types[] = {
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400504 "cmdlinepart",
505 "ofpart",
506 NULL
507};
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400508
Rafał Miłecki76a83222018-07-13 16:32:21 +0200509/* Check DT only when looking for subpartitions. */
510static const char * const default_subpartition_types[] = {
511 "ofpart",
512 NULL
513};
514
Brian Norris01f9c722017-05-23 07:30:20 +0200515static 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
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300536/**
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100537 * 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 */
545static 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
575static 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 property *prop;
581 const char *compat;
Rafał Miłeckic0faf432018-03-14 13:10:43 +0100582 const char *fixed = "fixed-partitions";
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100583 int ret, err = 0;
584
Rafał Miłecki76a83222018-07-13 16:32:21 +0200585 np = mtd_get_of_node(master);
Miquel Raynal85516a982018-09-07 16:35:54 +0200586 if (mtd_is_partition(master))
587 of_node_get(np);
588 else
Rafał Miłecki76a83222018-07-13 16:32:21 +0200589 np = of_get_child_by_name(np, "partitions");
Miquel Raynal85516a982018-09-07 16:35:54 +0200590
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100591 of_property_for_each_string(np, "compatible", prop, compat) {
592 parser = mtd_part_get_compatible_parser(compat);
593 if (!parser)
594 continue;
595 ret = mtd_part_do_parse(parser, master, pparts, NULL);
596 if (ret > 0) {
597 of_node_put(np);
598 return ret;
599 }
600 mtd_part_parser_put(parser);
601 if (ret < 0 && !err)
602 err = ret;
603 }
604 of_node_put(np);
605
606 /*
Rafał Miłeckic0faf432018-03-14 13:10:43 +0100607 * For backward compatibility we have to try the "fixed-partitions"
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100608 * parser. It supports old DT format with partitions specified as a
609 * direct subnodes of a flash device DT node without any compatibility
610 * specified we could match.
611 */
612 parser = mtd_part_parser_get(fixed);
613 if (!parser && !request_module("%s", fixed))
614 parser = mtd_part_parser_get(fixed);
615 if (parser) {
616 ret = mtd_part_do_parse(parser, master, pparts, NULL);
617 if (ret > 0)
618 return ret;
619 mtd_part_parser_put(parser);
620 if (ret < 0 && !err)
621 err = ret;
622 }
623
624 return err;
625}
626
627/**
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200628 * parse_mtd_partitions - parse and register MTD partitions
629 *
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300630 * @master: the master partition (describes whole MTD device)
631 * @types: names of partition parsers to try or %NULL
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400632 * @data: MTD partition parser-specific data
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300633 *
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200634 * This function tries to find & register partitions on MTD device @master. It
635 * uses MTD partition parsers, specified in @types. However, if @types is %NULL,
636 * then the default list of parsers is used. The default list contains only the
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400637 * "cmdlinepart" and "ofpart" parsers ATM.
Huang Shijiec51803d2012-08-18 13:07:41 -0400638 * Note: If there are more then one parser in @types, the kernel only takes the
639 * partitions parsed out by the first parser.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300640 *
641 * This function may return:
642 * o a negative error code in case of failure
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200643 * o number of found partitions otherwise
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300644 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200645int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400646 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700647{
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200648 struct mtd_partitions pparts = { };
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 struct mtd_part_parser *parser;
Brian Norris5a2415b2015-10-11 13:03:47 -0700650 int ret, err = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000651
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400652 if (!types)
Rafał Miłecki76a83222018-07-13 16:32:21 +0200653 types = mtd_is_partition(master) ? default_subpartition_types :
654 default_mtd_part_types;
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400655
Brian Norris5a2415b2015-10-11 13:03:47 -0700656 for ( ; *types; types++) {
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100657 /*
658 * ofpart is a special type that means OF partitioning info
659 * should be used. It requires a bit different logic so it is
660 * handled in a separated function.
661 */
662 if (!strcmp(*types, "ofpart")) {
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200663 ret = mtd_part_of_parse(master, &pparts);
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100664 } else {
665 pr_debug("%s: parsing partitions %s\n", master->name,
666 *types);
Brian Norris5531ae42015-12-04 15:25:15 -0800667 parser = mtd_part_parser_get(*types);
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100668 if (!parser && !request_module("%s", *types))
669 parser = mtd_part_parser_get(*types);
670 pr_debug("%s: got parser %s\n", master->name,
671 parser ? parser->name : NULL);
672 if (!parser)
673 continue;
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200674 ret = mtd_part_do_parse(parser, master, &pparts, data);
Rafał Miłecki5b644aa2018-03-14 13:10:42 +0100675 if (ret <= 0)
676 mtd_part_parser_put(parser);
677 }
Brian Norris01f9c722017-05-23 07:30:20 +0200678 /* Found partitions! */
Rafał Miłecki5ac67ce2018-03-27 22:35:41 +0200679 if (ret > 0) {
680 err = add_mtd_partitions(master, pparts.parts,
681 pparts.nr_parts);
682 mtd_part_parser_cleanup(&pparts);
683 return err ? err : pparts.nr_parts;
684 }
Brian Norris5a2415b2015-10-11 13:03:47 -0700685 /*
686 * Stash the first error we see; only report it if no parser
687 * succeeds
688 */
689 if (ret < 0 && !err)
690 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 }
Brian Norris5a2415b2015-10-11 13:03:47 -0700692 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300694
Brian Norrisadc83bf2015-12-09 10:24:03 -0800695void mtd_part_parser_cleanup(struct mtd_partitions *parts)
696{
697 const struct mtd_part_parser *parser;
698
699 if (!parts)
700 return;
701
702 parser = parts->parser;
703 if (parser) {
704 if (parser->cleanup)
705 parser->cleanup(parts->parts, parts->nr_parts);
706
707 mtd_part_parser_put(parser);
708 }
709}
710
Richard Genoud62082e562012-07-10 18:23:40 +0200711/* Returns the size of the entire flash chip */
712uint64_t mtd_get_device_size(const struct mtd_info *mtd)
713{
Miquel Raynal46b58892020-01-14 10:09:52 +0100714 struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
Richard Genoud62082e562012-07-10 18:23:40 +0200715
Miquel Raynal46b58892020-01-14 10:09:52 +0100716 return master->size;
Richard Genoud62082e562012-07-10 18:23:40 +0200717}
718EXPORT_SYMBOL_GPL(mtd_get_device_size);