]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/blame - drivers/mtd/mtdpart.c
mtd: rawnand: toshiba: Set the pairing scheme for TC58TEG5DCLTA00
[mirror_ubuntu-hirsute-kernel.git] / drivers / mtd / mtdpart.c
CommitLineData
fd534e9b 1// SPDX-License-Identifier: GPL-2.0-or-later
1da177e4
LT
2/*
3 * Simple MTD partitioning layer
4 *
a1452a37
DW
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>
97894cda 8 */
1da177e4
LT
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>
1da177e4
LT
15#include <linux/kmod.h>
16#include <linux/mtd/mtd.h>
17#include <linux/mtd/partitions.h>
5daa7b21 18#include <linux/err.h>
5b644aa0 19#include <linux/of.h>
1da177e4 20
eea72d5f
JI
21#include "mtdcore.h"
22
97894cda 23/*
1da177e4
LT
24 * MTD methods which simply translate the effective address and pass through
25 * to the _real_ device.
26 */
27
46b5889c 28static inline void free_partition(struct mtd_info *mtd)
5daa7b21 29{
46b5889c
MR
30 kfree(mtd->name);
31 kfree(mtd);
5daa7b21
RT
32}
33
46b5889c
MR
34static struct mtd_info *allocate_partition(struct mtd_info *parent,
35 const struct mtd_partition *part,
36 int partno, uint64_t cur_offset)
7788ba71 37{
c169e3d3 38 int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize :
0a9d72b6 39 parent->erasesize;
46b5889c 40 struct mtd_info *child, *master = mtd_get_master(parent);
1eeef2d7 41 u32 remainder;
5daa7b21 42 char *name;
1eeef2d7 43 u64 tmp;
7788ba71
AN
44
45 /* allocate the partition structure */
46b5889c 46 child = kzalloc(sizeof(*child), GFP_KERNEL);
5daa7b21 47 name = kstrdup(part->name, GFP_KERNEL);
46b5889c 48 if (!name || !child) {
b33a2887 49 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
0a9d72b6 50 parent->name);
5daa7b21 51 kfree(name);
46b5889c 52 kfree(child);
5daa7b21 53 return ERR_PTR(-ENOMEM);
7788ba71 54 }
7788ba71
AN
55
56 /* set up the MTD object for this partition */
46b5889c
MR
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;
66
67 child->name = name;
68 child->owner = parent->owner;
7788ba71 69
727dc612
DE
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
46b5889c 76 * distinguish between the parent and its partitions in sysfs.
1f24b5a8 77 */
46b5889c
MR
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);
84
85 if (child->part.offset == MTDPART_OFS_APPEND)
86 child->part.offset = cur_offset;
87 if (child->part.offset == MTDPART_OFS_NXTBLK) {
1eeef2d7 88 tmp = cur_offset;
46b5889c 89 child->part.offset = cur_offset;
1eeef2d7
CP
90 remainder = do_div(tmp, wr_alignment);
91 if (remainder) {
46b5889c 92 child->part.offset += wr_alignment - remainder;
7788ba71 93 printk(KERN_NOTICE "Moving partition %d: "
69423d99 94 "0x%012llx -> 0x%012llx\n", partno,
46b5889c
MR
95 (unsigned long long)cur_offset,
96 child->part.offset);
7788ba71
AN
97 }
98 }
46b5889c
MR
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 -
103 child->size;
1a31368b
DB
104 } else {
105 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
46b5889c
MR
106 part->name, parent->size - child->part.offset,
107 child->size);
1a31368b
DB
108 /* register to preserve ordering */
109 goto out_register;
110 }
111 }
46b5889c
MR
112 if (child->size == MTDPART_SIZ_FULL)
113 child->size = parent->size - child->part.offset;
7788ba71 114
46b5889c
MR
115 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n",
116 child->part.offset, child->part.offset + child->size,
117 child->name);
7788ba71
AN
118
119 /* let's do some sanity checks */
46b5889c 120 if (child->part.offset >= parent->size) {
f636ffb4 121 /* let's register it anyway to preserve ordering */
46b5889c
MR
122 child->part.offset = 0;
123 child->size = 0;
ad463515
BB
124
125 /* Initialize ->erasesize to make add_mtd_device() happy. */
46b5889c 126 child->erasesize = parent->erasesize;
b33a2887 127 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
7788ba71 128 part->name);
f636ffb4 129 goto out_register;
7788ba71 130 }
46b5889c
MR
131 if (child->part.offset + child->size > parent->size) {
132 child->size = parent->size - child->part.offset;
69423d99 133 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
46b5889c 134 part->name, parent->name, child->size);
7788ba71 135 }
0a9d72b6 136 if (parent->numeraseregions > 1) {
7788ba71 137 /* Deal with variable erase size stuff */
0a9d72b6 138 int i, max = parent->numeraseregions;
46b5889c 139 u64 end = child->part.offset + child->size;
0a9d72b6 140 struct mtd_erase_region_info *regions = parent->eraseregions;
7788ba71 141
6910c136
AN
142 /* Find the first erase regions which is part of this
143 * partition. */
46b5889c
MR
144 for (i = 0; i < max && regions[i].offset <= child->part.offset;
145 i++)
7788ba71 146 ;
6910c136 147 /* The loop searched for the region _behind_ the first one */
a57ca046
RK
148 if (i > 0)
149 i--;
7788ba71 150
6910c136
AN
151 /* Pick biggest erasesize */
152 for (; i < max && regions[i].offset < end; i++) {
46b5889c
MR
153 if (child->erasesize < regions[i].erasesize)
154 child->erasesize = regions[i].erasesize;
7788ba71 155 }
46b5889c 156 BUG_ON(child->erasesize == 0);
7788ba71
AN
157 } else {
158 /* Single erase size */
46b5889c 159 child->erasesize = parent->erasesize;
7788ba71
AN
160 }
161
7e439681 162 /*
46b5889c 163 * Child erasesize might differ from the parent one if the parent
7e439681
BB
164 * exposes several regions with different erasesize. Adjust
165 * wr_alignment accordingly.
166 */
46b5889c
MR
167 if (!(child->flags & MTD_NO_ERASE))
168 wr_alignment = child->erasesize;
7e439681 169
46b5889c 170 tmp = mtd_get_master_ofs(child, 0);
1eeef2d7 171 remainder = do_div(tmp, wr_alignment);
46b5889c 172 if ((child->flags & MTD_WRITEABLE) && remainder) {
7788ba71 173 /* Doesn't start on a boundary of major erase size */
b33a2887
AN
174 /* FIXME: Let it be writable if it is on a boundary of
175 * _minor_ erase size though */
46b5889c 176 child->flags &= ~MTD_WRITEABLE;
1eeef2d7 177 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
7788ba71
AN
178 part->name);
179 }
1eeef2d7 180
46b5889c 181 tmp = mtd_get_master_ofs(child, 0) + child->size;
1eeef2d7 182 remainder = do_div(tmp, wr_alignment);
46b5889c
MR
183 if ((child->flags & MTD_WRITEABLE) && remainder) {
184 child->flags &= ~MTD_WRITEABLE;
1eeef2d7 185 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
7788ba71
AN
186 part->name);
187 }
188
46b5889c
MR
189 child->ecc_step_size = parent->ecc_step_size;
190 child->ecc_strength = parent->ecc_strength;
191 child->bitflip_threshold = parent->bitflip_threshold;
d062d4ed 192
46b5889c 193 if (master->_block_isbad) {
69423d99 194 uint64_t offs = 0;
7788ba71 195
46b5889c
MR
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;
7788ba71
AN
202 }
203 }
204
f636ffb4 205out_register:
46b5889c 206 return child;
7788ba71
AN
207}
208
a62c24d7
DE
209static ssize_t mtd_partition_offset_show(struct device *dev,
210 struct device_attribute *attr, char *buf)
211{
212 struct mtd_info *mtd = dev_get_drvdata(dev);
46b5889c
MR
213
214 return snprintf(buf, PAGE_SIZE, "%lld\n", mtd->part.offset);
a62c24d7
DE
215}
216
217static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
218
219static const struct attribute *mtd_partition_attrs[] = {
220 &dev_attr_offset.attr,
221 NULL
222};
223
46b5889c 224static int mtd_add_partition_attrs(struct mtd_info *new)
a62c24d7 225{
46b5889c 226 int ret = sysfs_create_files(&new->dev.kobj, mtd_partition_attrs);
a62c24d7
DE
227 if (ret)
228 printk(KERN_WARNING
229 "mtd: failed to create partition attrs, err=%d\n", ret);
230 return ret;
231}
232
0a9d72b6 233int mtd_add_partition(struct mtd_info *parent, const char *name,
5daa7b21
RT
234 long long offset, long long length)
235{
46b5889c 236 struct mtd_info *master = mtd_get_master(parent);
5daa7b21 237 struct mtd_partition part;
46b5889c 238 struct mtd_info *child;
5daa7b21
RT
239 int ret = 0;
240
241 /* the direct offset is expected */
242 if (offset == MTDPART_OFS_APPEND ||
243 offset == MTDPART_OFS_NXTBLK)
244 return -EINVAL;
245
246 if (length == MTDPART_SIZ_FULL)
0a9d72b6 247 length = parent->size - offset;
5daa7b21
RT
248
249 if (length <= 0)
250 return -EINVAL;
251
93867233 252 memset(&part, 0, sizeof(part));
5daa7b21
RT
253 part.name = name;
254 part.size = length;
255 part.offset = offset;
5daa7b21 256
46b5889c
MR
257 child = allocate_partition(parent, &part, -1, offset);
258 if (IS_ERR(child))
259 return PTR_ERR(child);
5daa7b21 260
46b5889c
MR
261 mutex_lock(&master->master.partitions_lock);
262 list_add_tail(&child->part.node, &parent->partitions);
263 mutex_unlock(&master->master.partitions_lock);
5daa7b21 264
46b5889c 265 ret = add_mtd_device(child);
2b6f0090
BB
266 if (ret)
267 goto err_remove_part;
5daa7b21 268
46b5889c 269 mtd_add_partition_attrs(child);
a62c24d7 270
2b6f0090
BB
271 return 0;
272
273err_remove_part:
46b5889c
MR
274 mutex_lock(&master->master.partitions_lock);
275 list_del(&child->part.node);
276 mutex_unlock(&master->master.partitions_lock);
2b6f0090 277
46b5889c 278 free_partition(child);
2b6f0090 279
5daa7b21 280 return ret;
5daa7b21
RT
281}
282EXPORT_SYMBOL_GPL(mtd_add_partition);
283
08263a9a
RM
284/**
285 * __mtd_del_partition - delete MTD partition
286 *
46b5889c 287 * @priv: MTD structure to be deleted
08263a9a
RM
288 *
289 * This function must be called with the partitions mutex locked.
290 */
46b5889c 291static int __mtd_del_partition(struct mtd_info *mtd)
08263a9a 292{
46b5889c 293 struct mtd_info *child, *next;
08263a9a
RM
294 int err;
295
46b5889c
MR
296 list_for_each_entry_safe(child, next, &mtd->partitions, part.node) {
297 err = __mtd_del_partition(child);
298 if (err)
299 return err;
97519dc5
RM
300 }
301
46b5889c 302 sysfs_remove_files(&mtd->dev.kobj, mtd_partition_attrs);
c5ceaba7 303
46b5889c 304 err = del_mtd_device(mtd);
08263a9a
RM
305 if (err)
306 return err;
307
46b5889c
MR
308 list_del(&child->part.node);
309 free_partition(mtd);
08263a9a
RM
310
311 return 0;
312}
313
314/*
315 * This function unregisters and destroy all slave MTD objects which are
46b5889c 316 * attached to the given MTD object, recursively.
08263a9a 317 */
46b5889c 318static int __del_mtd_partitions(struct mtd_info *mtd)
08263a9a 319{
46b5889c
MR
320 struct mtd_info *child, *next;
321 LIST_HEAD(tmp_list);
08263a9a
RM
322 int ret, err = 0;
323
46b5889c
MR
324 list_for_each_entry_safe(child, next, &mtd->partitions, part.node) {
325 if (mtd_has_partitions(child))
326 del_mtd_partitions(child);
327
328 pr_info("Deleting %s MTD partition\n", child->name);
329 ret = del_mtd_device(child);
330 if (ret < 0) {
331 pr_err("Error when deleting partition \"%s\" (%d)\n",
332 child->name, ret);
333 err = ret;
334 continue;
08263a9a 335 }
46b5889c
MR
336
337 list_del(&child->part.node);
338 free_partition(child);
339 }
08263a9a
RM
340
341 return err;
342}
343
46b5889c
MR
344int del_mtd_partitions(struct mtd_info *mtd)
345{
346 struct mtd_info *master = mtd_get_master(mtd);
347 int ret;
348
349 pr_info("Deleting MTD partitions on \"%s\":\n", mtd->name);
350
351 mutex_lock(&master->master.partitions_lock);
352 ret = __del_mtd_partitions(mtd);
353 mutex_unlock(&master->master.partitions_lock);
354
355 return ret;
356}
357
97519dc5 358int mtd_del_partition(struct mtd_info *mtd, int partno)
5daa7b21 359{
46b5889c 360 struct mtd_info *child, *master = mtd_get_master(mtd);
5daa7b21
RT
361 int ret = -EINVAL;
362
46b5889c
MR
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);
5daa7b21
RT
367 break;
368 }
46b5889c
MR
369 }
370 mutex_unlock(&master->master.partitions_lock);
5daa7b21
RT
371
372 return ret;
373}
374EXPORT_SYMBOL_GPL(mtd_del_partition);
375
1da177e4 376/*
46b5889c
MR
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.
1f24b5a8 380 *
46b5889c 381 * For historical reasons, this function's caller only registers the parent
727dc612 382 * if the MTD_PARTITIONED_MASTER config option is set.
1da177e4
LT
383 */
384
46b5889c 385int add_mtd_partitions(struct mtd_info *parent,
1da177e4
LT
386 const struct mtd_partition *parts,
387 int nbparts)
388{
46b5889c 389 struct mtd_info *child, *master = mtd_get_master(parent);
69423d99 390 uint64_t cur_offset = 0;
2b6f0090 391 int i, ret;
1da177e4 392
46b5889c
MR
393 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n",
394 nbparts, parent->name);
1da177e4
LT
395
396 for (i = 0; i < nbparts; i++) {
46b5889c
MR
397 child = allocate_partition(parent, parts + i, i, cur_offset);
398 if (IS_ERR(child)) {
399 ret = PTR_ERR(child);
2b6f0090 400 goto err_del_partitions;
e5bae867 401 }
5daa7b21 402
46b5889c
MR
403 mutex_lock(&master->master.partitions_lock);
404 list_add_tail(&child->part.node, &parent->partitions);
405 mutex_unlock(&master->master.partitions_lock);
5daa7b21 406
46b5889c 407 ret = add_mtd_device(child);
2b6f0090 408 if (ret) {
46b5889c
MR
409 mutex_lock(&master->master.partitions_lock);
410 list_del(&child->part.node);
411 mutex_unlock(&master->master.partitions_lock);
2b6f0090 412
46b5889c 413 free_partition(child);
2b6f0090
BB
414 goto err_del_partitions;
415 }
416
46b5889c
MR
417 mtd_add_partition_attrs(child);
418
76a83225 419 /* Look for subpartitions */
46b5889c 420 parse_mtd_partitions(child, parts[i].types, NULL);
5daa7b21 421
46b5889c 422 cur_offset = child->part.offset + child->size;
1da177e4
LT
423 }
424
425 return 0;
2b6f0090
BB
426
427err_del_partitions:
428 del_mtd_partitions(master);
429
430 return ret;
1da177e4 431}
1da177e4
LT
432
433static DEFINE_SPINLOCK(part_parser_lock);
434static LIST_HEAD(part_parsers);
435
5531ae48 436static struct mtd_part_parser *mtd_part_parser_get(const char *name)
1da177e4 437{
71a928c0 438 struct mtd_part_parser *p, *ret = NULL;
1da177e4 439
71a928c0 440 spin_lock(&part_parser_lock);
1da177e4 441
71a928c0 442 list_for_each_entry(p, &part_parsers, list)
1da177e4
LT
443 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
444 ret = p;
445 break;
446 }
71a928c0 447
1da177e4
LT
448 spin_unlock(&part_parser_lock);
449
450 return ret;
451}
452
5531ae48
BN
453static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
454{
455 module_put(p->owner);
456}
953b3bd1 457
adc83bf8
BN
458/*
459 * Many partition parsers just expected the core to kfree() all their data in
460 * one chunk. Do that by default.
461 */
462static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
463 int nr_parts)
464{
465 kfree(pparts);
466}
467
b9eab011 468int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
1da177e4 469{
b9eab011
BN
470 p->owner = owner;
471
adc83bf8
BN
472 if (!p->cleanup)
473 p->cleanup = &mtd_part_parser_cleanup_default;
474
1da177e4
LT
475 spin_lock(&part_parser_lock);
476 list_add(&p->list, &part_parsers);
477 spin_unlock(&part_parser_lock);
b9eab011
BN
478
479 return 0;
1da177e4 480}
b9eab011 481EXPORT_SYMBOL_GPL(__register_mtd_parser);
1da177e4 482
cf3b2b1e 483void deregister_mtd_parser(struct mtd_part_parser *p)
1da177e4
LT
484{
485 spin_lock(&part_parser_lock);
486 list_del(&p->list);
487 spin_unlock(&part_parser_lock);
1da177e4 488}
b33a2887 489EXPORT_SYMBOL_GPL(deregister_mtd_parser);
1da177e4 490
ad274cec
AB
491/*
492 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
493 * are changing this array!
494 */
ccef4dcc 495static const char * const default_mtd_part_types[] = {
d26c87d6
DB
496 "cmdlinepart",
497 "ofpart",
498 NULL
499};
5c4eefbd 500
76a83225
RM
501/* Check DT only when looking for subpartitions. */
502static const char * const default_subpartition_types[] = {
503 "ofpart",
504 NULL
505};
506
01f9c724
BN
507static 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)
511{
512 int ret;
513
514 ret = (*parser->parse_fn)(master, &pparts->parts, data);
515 pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
516 if (ret <= 0)
517 return ret;
518
519 pr_notice("%d %s partitions found on MTD device %s\n", ret,
520 parser->name, master->name);
521
522 pparts->nr_parts = ret;
523 pparts->parser = parser;
524
525 return ret;
526}
527
5b644aa0
RM
528/**
529 * mtd_part_get_compatible_parser - find MTD parser by a compatible string
530 *
531 * @compat: compatible string describing partitions in a device tree
532 *
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".
536 */
537static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat)
538{
539 struct mtd_part_parser *p, *ret = NULL;
540
541 spin_lock(&part_parser_lock);
542
543 list_for_each_entry(p, &part_parsers, list) {
544 const struct of_device_id *matches;
545
546 matches = p->of_match_table;
547 if (!matches)
548 continue;
549
550 for (; matches->compatible[0]; matches++) {
551 if (!strcmp(matches->compatible, compat) &&
552 try_module_get(p->owner)) {
553 ret = p;
554 break;
555 }
556 }
557
558 if (ret)
559 break;
560 }
561
562 spin_unlock(&part_parser_lock);
563
564 return ret;
565}
566
567static int mtd_part_of_parse(struct mtd_info *master,
568 struct mtd_partitions *pparts)
569{
570 struct mtd_part_parser *parser;
571 struct device_node *np;
572 struct property *prop;
573 const char *compat;
c0faf434 574 const char *fixed = "fixed-partitions";
5b644aa0
RM
575 int ret, err = 0;
576
76a83225 577 np = mtd_get_of_node(master);
85516a98
MR
578 if (mtd_is_partition(master))
579 of_node_get(np);
580 else
76a83225 581 np = of_get_child_by_name(np, "partitions");
85516a98 582
5b644aa0
RM
583 of_property_for_each_string(np, "compatible", prop, compat) {
584 parser = mtd_part_get_compatible_parser(compat);
585 if (!parser)
586 continue;
587 ret = mtd_part_do_parse(parser, master, pparts, NULL);
588 if (ret > 0) {
589 of_node_put(np);
590 return ret;
591 }
592 mtd_part_parser_put(parser);
593 if (ret < 0 && !err)
594 err = ret;
595 }
596 of_node_put(np);
597
598 /*
c0faf434 599 * For backward compatibility we have to try the "fixed-partitions"
5b644aa0
RM
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.
603 */
604 parser = mtd_part_parser_get(fixed);
605 if (!parser && !request_module("%s", fixed))
606 parser = mtd_part_parser_get(fixed);
607 if (parser) {
608 ret = mtd_part_do_parse(parser, master, pparts, NULL);
609 if (ret > 0)
610 return ret;
611 mtd_part_parser_put(parser);
612 if (ret < 0 && !err)
613 err = ret;
614 }
615
616 return err;
617}
618
ad274cec 619/**
5ac67ce3
RM
620 * parse_mtd_partitions - parse and register MTD partitions
621 *
ad274cec
AB
622 * @master: the master partition (describes whole MTD device)
623 * @types: names of partition parsers to try or %NULL
c7975330 624 * @data: MTD partition parser-specific data
ad274cec 625 *
5ac67ce3
RM
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
d26c87d6 629 * "cmdlinepart" and "ofpart" parsers ATM.
c51803dd
HS
630 * Note: If there are more then one parser in @types, the kernel only takes the
631 * partitions parsed out by the first parser.
ad274cec
AB
632 *
633 * This function may return:
634 * o a negative error code in case of failure
5ac67ce3 635 * o number of found partitions otherwise
ad274cec 636 */
26a47346 637int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
c7975330 638 struct mtd_part_parser_data *data)
1da177e4 639{
5ac67ce3 640 struct mtd_partitions pparts = { };
1da177e4 641 struct mtd_part_parser *parser;
5a2415b0 642 int ret, err = 0;
97894cda 643
5c4eefbd 644 if (!types)
76a83225
RM
645 types = mtd_is_partition(master) ? default_subpartition_types :
646 default_mtd_part_types;
5c4eefbd 647
5a2415b0 648 for ( ; *types; types++) {
5b644aa0
RM
649 /*
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.
653 */
654 if (!strcmp(*types, "ofpart")) {
5ac67ce3 655 ret = mtd_part_of_parse(master, &pparts);
5b644aa0
RM
656 } else {
657 pr_debug("%s: parsing partitions %s\n", master->name,
658 *types);
5531ae48 659 parser = mtd_part_parser_get(*types);
5b644aa0
RM
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);
664 if (!parser)
665 continue;
5ac67ce3 666 ret = mtd_part_do_parse(parser, master, &pparts, data);
5b644aa0
RM
667 if (ret <= 0)
668 mtd_part_parser_put(parser);
669 }
01f9c724 670 /* Found partitions! */
5ac67ce3
RM
671 if (ret > 0) {
672 err = add_mtd_partitions(master, pparts.parts,
673 pparts.nr_parts);
674 mtd_part_parser_cleanup(&pparts);
675 return err ? err : pparts.nr_parts;
676 }
5a2415b0
BN
677 /*
678 * Stash the first error we see; only report it if no parser
679 * succeeds
680 */
681 if (ret < 0 && !err)
682 err = ret;
1da177e4 683 }
5a2415b0 684 return err;
1da177e4 685}
5daa7b21 686
adc83bf8
BN
687void mtd_part_parser_cleanup(struct mtd_partitions *parts)
688{
689 const struct mtd_part_parser *parser;
690
691 if (!parts)
692 return;
693
694 parser = parts->parser;
695 if (parser) {
696 if (parser->cleanup)
697 parser->cleanup(parts->parts, parts->nr_parts);
698
699 mtd_part_parser_put(parser);
700 }
701}
702
62082e56
RG
703/* Returns the size of the entire flash chip */
704uint64_t mtd_get_device_size(const struct mtd_info *mtd)
705{
46b5889c 706 struct mtd_info *master = mtd_get_master((struct mtd_info *)mtd);
62082e56 707
46b5889c 708 return master->size;
62082e56
RG
709}
710EXPORT_SYMBOL_GPL(mtd_get_device_size);