]> git.proxmox.com Git - mirror_ubuntu-zesty-kernel.git/blob - drivers/mtd/mtdpart.c
Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/jikos/hid
[mirror_ubuntu-zesty-kernel.git] / drivers / mtd / mtdpart.c
1 /*
2 * Simple MTD partitioning layer
3 *
4 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
5 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
6 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 *
22 */
23
24 #include <linux/module.h>
25 #include <linux/types.h>
26 #include <linux/kernel.h>
27 #include <linux/slab.h>
28 #include <linux/list.h>
29 #include <linux/kmod.h>
30 #include <linux/mtd/mtd.h>
31 #include <linux/mtd/partitions.h>
32 #include <linux/err.h>
33 #include <linux/kconfig.h>
34
35 #include "mtdcore.h"
36
37 /* Our partition linked list */
38 static LIST_HEAD(mtd_partitions);
39 static DEFINE_MUTEX(mtd_partitions_mutex);
40
41 /* Our partition node structure */
42 struct mtd_part {
43 struct mtd_info mtd;
44 struct mtd_info *master;
45 uint64_t offset;
46 struct list_head list;
47 };
48
49 /*
50 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
51 * the pointer to that structure.
52 */
53 static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
54 {
55 return container_of(mtd, struct mtd_part, mtd);
56 }
57
58
59 /*
60 * MTD methods which simply translate the effective address and pass through
61 * to the _real_ device.
62 */
63
64 static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
65 size_t *retlen, u_char *buf)
66 {
67 struct mtd_part *part = mtd_to_part(mtd);
68 struct mtd_ecc_stats stats;
69 int res;
70
71 stats = part->master->ecc_stats;
72 res = part->master->_read(part->master, from + part->offset, len,
73 retlen, buf);
74 if (unlikely(mtd_is_eccerr(res)))
75 mtd->ecc_stats.failed +=
76 part->master->ecc_stats.failed - stats.failed;
77 else
78 mtd->ecc_stats.corrected +=
79 part->master->ecc_stats.corrected - stats.corrected;
80 return res;
81 }
82
83 static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
84 size_t *retlen, void **virt, resource_size_t *phys)
85 {
86 struct mtd_part *part = mtd_to_part(mtd);
87
88 return part->master->_point(part->master, from + part->offset, len,
89 retlen, virt, phys);
90 }
91
92 static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
93 {
94 struct mtd_part *part = mtd_to_part(mtd);
95
96 return part->master->_unpoint(part->master, from + part->offset, len);
97 }
98
99 static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
100 unsigned long len,
101 unsigned long offset,
102 unsigned long flags)
103 {
104 struct mtd_part *part = mtd_to_part(mtd);
105
106 offset += part->offset;
107 return part->master->_get_unmapped_area(part->master, len, offset,
108 flags);
109 }
110
111 static int part_read_oob(struct mtd_info *mtd, loff_t from,
112 struct mtd_oob_ops *ops)
113 {
114 struct mtd_part *part = mtd_to_part(mtd);
115 int res;
116
117 if (from >= mtd->size)
118 return -EINVAL;
119 if (ops->datbuf && from + ops->len > mtd->size)
120 return -EINVAL;
121
122 /*
123 * If OOB is also requested, make sure that we do not read past the end
124 * of this partition.
125 */
126 if (ops->oobbuf) {
127 size_t len, pages;
128
129 len = mtd_oobavail(mtd, ops);
130 pages = mtd_div_by_ws(mtd->size, mtd);
131 pages -= mtd_div_by_ws(from, mtd);
132 if (ops->ooboffs + ops->ooblen > pages * len)
133 return -EINVAL;
134 }
135
136 res = part->master->_read_oob(part->master, from + part->offset, ops);
137 if (unlikely(res)) {
138 if (mtd_is_bitflip(res))
139 mtd->ecc_stats.corrected++;
140 if (mtd_is_eccerr(res))
141 mtd->ecc_stats.failed++;
142 }
143 return res;
144 }
145
146 static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
147 size_t len, size_t *retlen, u_char *buf)
148 {
149 struct mtd_part *part = mtd_to_part(mtd);
150 return part->master->_read_user_prot_reg(part->master, from, len,
151 retlen, buf);
152 }
153
154 static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
155 size_t *retlen, struct otp_info *buf)
156 {
157 struct mtd_part *part = mtd_to_part(mtd);
158 return part->master->_get_user_prot_info(part->master, len, retlen,
159 buf);
160 }
161
162 static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
163 size_t len, size_t *retlen, u_char *buf)
164 {
165 struct mtd_part *part = mtd_to_part(mtd);
166 return part->master->_read_fact_prot_reg(part->master, from, len,
167 retlen, buf);
168 }
169
170 static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
171 size_t *retlen, struct otp_info *buf)
172 {
173 struct mtd_part *part = mtd_to_part(mtd);
174 return part->master->_get_fact_prot_info(part->master, len, retlen,
175 buf);
176 }
177
178 static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
179 size_t *retlen, const u_char *buf)
180 {
181 struct mtd_part *part = mtd_to_part(mtd);
182 return part->master->_write(part->master, to + part->offset, len,
183 retlen, buf);
184 }
185
186 static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
187 size_t *retlen, const u_char *buf)
188 {
189 struct mtd_part *part = mtd_to_part(mtd);
190 return part->master->_panic_write(part->master, to + part->offset, len,
191 retlen, buf);
192 }
193
194 static int part_write_oob(struct mtd_info *mtd, loff_t to,
195 struct mtd_oob_ops *ops)
196 {
197 struct mtd_part *part = mtd_to_part(mtd);
198
199 if (to >= mtd->size)
200 return -EINVAL;
201 if (ops->datbuf && to + ops->len > mtd->size)
202 return -EINVAL;
203 return part->master->_write_oob(part->master, to + part->offset, ops);
204 }
205
206 static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
207 size_t len, size_t *retlen, u_char *buf)
208 {
209 struct mtd_part *part = mtd_to_part(mtd);
210 return part->master->_write_user_prot_reg(part->master, from, len,
211 retlen, buf);
212 }
213
214 static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
215 size_t len)
216 {
217 struct mtd_part *part = mtd_to_part(mtd);
218 return part->master->_lock_user_prot_reg(part->master, from, len);
219 }
220
221 static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
222 unsigned long count, loff_t to, size_t *retlen)
223 {
224 struct mtd_part *part = mtd_to_part(mtd);
225 return part->master->_writev(part->master, vecs, count,
226 to + part->offset, retlen);
227 }
228
229 static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
230 {
231 struct mtd_part *part = mtd_to_part(mtd);
232 int ret;
233
234 instr->addr += part->offset;
235 ret = part->master->_erase(part->master, instr);
236 if (ret) {
237 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
238 instr->fail_addr -= part->offset;
239 instr->addr -= part->offset;
240 }
241 return ret;
242 }
243
244 void mtd_erase_callback(struct erase_info *instr)
245 {
246 if (instr->mtd->_erase == part_erase) {
247 struct mtd_part *part = mtd_to_part(instr->mtd);
248
249 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
250 instr->fail_addr -= part->offset;
251 instr->addr -= part->offset;
252 }
253 if (instr->callback)
254 instr->callback(instr);
255 }
256 EXPORT_SYMBOL_GPL(mtd_erase_callback);
257
258 static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
259 {
260 struct mtd_part *part = mtd_to_part(mtd);
261 return part->master->_lock(part->master, ofs + part->offset, len);
262 }
263
264 static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
265 {
266 struct mtd_part *part = mtd_to_part(mtd);
267 return part->master->_unlock(part->master, ofs + part->offset, len);
268 }
269
270 static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
271 {
272 struct mtd_part *part = mtd_to_part(mtd);
273 return part->master->_is_locked(part->master, ofs + part->offset, len);
274 }
275
276 static void part_sync(struct mtd_info *mtd)
277 {
278 struct mtd_part *part = mtd_to_part(mtd);
279 part->master->_sync(part->master);
280 }
281
282 static int part_suspend(struct mtd_info *mtd)
283 {
284 struct mtd_part *part = mtd_to_part(mtd);
285 return part->master->_suspend(part->master);
286 }
287
288 static void part_resume(struct mtd_info *mtd)
289 {
290 struct mtd_part *part = mtd_to_part(mtd);
291 part->master->_resume(part->master);
292 }
293
294 static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
295 {
296 struct mtd_part *part = mtd_to_part(mtd);
297 ofs += part->offset;
298 return part->master->_block_isreserved(part->master, ofs);
299 }
300
301 static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
302 {
303 struct mtd_part *part = mtd_to_part(mtd);
304 ofs += part->offset;
305 return part->master->_block_isbad(part->master, ofs);
306 }
307
308 static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
309 {
310 struct mtd_part *part = mtd_to_part(mtd);
311 int res;
312
313 ofs += part->offset;
314 res = part->master->_block_markbad(part->master, ofs);
315 if (!res)
316 mtd->ecc_stats.badblocks++;
317 return res;
318 }
319
320 static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
321 struct mtd_oob_region *oobregion)
322 {
323 struct mtd_part *part = mtd_to_part(mtd);
324
325 return mtd_ooblayout_ecc(part->master, section, oobregion);
326 }
327
328 static int part_ooblayout_free(struct mtd_info *mtd, int section,
329 struct mtd_oob_region *oobregion)
330 {
331 struct mtd_part *part = mtd_to_part(mtd);
332
333 return mtd_ooblayout_free(part->master, section, oobregion);
334 }
335
336 static const struct mtd_ooblayout_ops part_ooblayout_ops = {
337 .ecc = part_ooblayout_ecc,
338 .free = part_ooblayout_free,
339 };
340
341 static inline void free_partition(struct mtd_part *p)
342 {
343 kfree(p->mtd.name);
344 kfree(p);
345 }
346
347 /*
348 * This function unregisters and destroy all slave MTD objects which are
349 * attached to the given master MTD object.
350 */
351
352 int del_mtd_partitions(struct mtd_info *master)
353 {
354 struct mtd_part *slave, *next;
355 int ret, err = 0;
356
357 mutex_lock(&mtd_partitions_mutex);
358 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
359 if (slave->master == master) {
360 ret = del_mtd_device(&slave->mtd);
361 if (ret < 0) {
362 err = ret;
363 continue;
364 }
365 list_del(&slave->list);
366 free_partition(slave);
367 }
368 mutex_unlock(&mtd_partitions_mutex);
369
370 return err;
371 }
372
373 static struct mtd_part *allocate_partition(struct mtd_info *master,
374 const struct mtd_partition *part, int partno,
375 uint64_t cur_offset)
376 {
377 struct mtd_part *slave;
378 char *name;
379
380 /* allocate the partition structure */
381 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
382 name = kstrdup(part->name, GFP_KERNEL);
383 if (!name || !slave) {
384 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
385 master->name);
386 kfree(name);
387 kfree(slave);
388 return ERR_PTR(-ENOMEM);
389 }
390
391 /* set up the MTD object for this partition */
392 slave->mtd.type = master->type;
393 slave->mtd.flags = master->flags & ~part->mask_flags;
394 slave->mtd.size = part->size;
395 slave->mtd.writesize = master->writesize;
396 slave->mtd.writebufsize = master->writebufsize;
397 slave->mtd.oobsize = master->oobsize;
398 slave->mtd.oobavail = master->oobavail;
399 slave->mtd.subpage_sft = master->subpage_sft;
400
401 slave->mtd.name = name;
402 slave->mtd.owner = master->owner;
403
404 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
405 * concern for showing the same data in multiple partitions.
406 * However, it is very useful to have the master node present,
407 * so the MTD_PARTITIONED_MASTER option allows that. The master
408 * will have device nodes etc only if this is set, so make the
409 * parent conditional on that option. Note, this is a way to
410 * distinguish between the master and the partition in sysfs.
411 */
412 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) ?
413 &master->dev :
414 master->dev.parent;
415
416 slave->mtd._read = part_read;
417 slave->mtd._write = part_write;
418
419 if (master->_panic_write)
420 slave->mtd._panic_write = part_panic_write;
421
422 if (master->_point && master->_unpoint) {
423 slave->mtd._point = part_point;
424 slave->mtd._unpoint = part_unpoint;
425 }
426
427 if (master->_get_unmapped_area)
428 slave->mtd._get_unmapped_area = part_get_unmapped_area;
429 if (master->_read_oob)
430 slave->mtd._read_oob = part_read_oob;
431 if (master->_write_oob)
432 slave->mtd._write_oob = part_write_oob;
433 if (master->_read_user_prot_reg)
434 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
435 if (master->_read_fact_prot_reg)
436 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
437 if (master->_write_user_prot_reg)
438 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
439 if (master->_lock_user_prot_reg)
440 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
441 if (master->_get_user_prot_info)
442 slave->mtd._get_user_prot_info = part_get_user_prot_info;
443 if (master->_get_fact_prot_info)
444 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
445 if (master->_sync)
446 slave->mtd._sync = part_sync;
447 if (!partno && !master->dev.class && master->_suspend &&
448 master->_resume) {
449 slave->mtd._suspend = part_suspend;
450 slave->mtd._resume = part_resume;
451 }
452 if (master->_writev)
453 slave->mtd._writev = part_writev;
454 if (master->_lock)
455 slave->mtd._lock = part_lock;
456 if (master->_unlock)
457 slave->mtd._unlock = part_unlock;
458 if (master->_is_locked)
459 slave->mtd._is_locked = part_is_locked;
460 if (master->_block_isreserved)
461 slave->mtd._block_isreserved = part_block_isreserved;
462 if (master->_block_isbad)
463 slave->mtd._block_isbad = part_block_isbad;
464 if (master->_block_markbad)
465 slave->mtd._block_markbad = part_block_markbad;
466 slave->mtd._erase = part_erase;
467 slave->master = master;
468 slave->offset = part->offset;
469
470 if (slave->offset == MTDPART_OFS_APPEND)
471 slave->offset = cur_offset;
472 if (slave->offset == MTDPART_OFS_NXTBLK) {
473 slave->offset = cur_offset;
474 if (mtd_mod_by_eb(cur_offset, master) != 0) {
475 /* Round up to next erasesize */
476 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
477 printk(KERN_NOTICE "Moving partition %d: "
478 "0x%012llx -> 0x%012llx\n", partno,
479 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
480 }
481 }
482 if (slave->offset == MTDPART_OFS_RETAIN) {
483 slave->offset = cur_offset;
484 if (master->size - slave->offset >= slave->mtd.size) {
485 slave->mtd.size = master->size - slave->offset
486 - slave->mtd.size;
487 } else {
488 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
489 part->name, master->size - slave->offset,
490 slave->mtd.size);
491 /* register to preserve ordering */
492 goto out_register;
493 }
494 }
495 if (slave->mtd.size == MTDPART_SIZ_FULL)
496 slave->mtd.size = master->size - slave->offset;
497
498 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
499 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
500
501 /* let's do some sanity checks */
502 if (slave->offset >= master->size) {
503 /* let's register it anyway to preserve ordering */
504 slave->offset = 0;
505 slave->mtd.size = 0;
506 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
507 part->name);
508 goto out_register;
509 }
510 if (slave->offset + slave->mtd.size > master->size) {
511 slave->mtd.size = master->size - slave->offset;
512 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
513 part->name, master->name, (unsigned long long)slave->mtd.size);
514 }
515 if (master->numeraseregions > 1) {
516 /* Deal with variable erase size stuff */
517 int i, max = master->numeraseregions;
518 u64 end = slave->offset + slave->mtd.size;
519 struct mtd_erase_region_info *regions = master->eraseregions;
520
521 /* Find the first erase regions which is part of this
522 * partition. */
523 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
524 ;
525 /* The loop searched for the region _behind_ the first one */
526 if (i > 0)
527 i--;
528
529 /* Pick biggest erasesize */
530 for (; i < max && regions[i].offset < end; i++) {
531 if (slave->mtd.erasesize < regions[i].erasesize) {
532 slave->mtd.erasesize = regions[i].erasesize;
533 }
534 }
535 BUG_ON(slave->mtd.erasesize == 0);
536 } else {
537 /* Single erase size */
538 slave->mtd.erasesize = master->erasesize;
539 }
540
541 if ((slave->mtd.flags & MTD_WRITEABLE) &&
542 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
543 /* Doesn't start on a boundary of major erase size */
544 /* FIXME: Let it be writable if it is on a boundary of
545 * _minor_ erase size though */
546 slave->mtd.flags &= ~MTD_WRITEABLE;
547 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
548 part->name);
549 }
550 if ((slave->mtd.flags & MTD_WRITEABLE) &&
551 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
552 slave->mtd.flags &= ~MTD_WRITEABLE;
553 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
554 part->name);
555 }
556
557 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
558 slave->mtd.ecc_step_size = master->ecc_step_size;
559 slave->mtd.ecc_strength = master->ecc_strength;
560 slave->mtd.bitflip_threshold = master->bitflip_threshold;
561
562 if (master->_block_isbad) {
563 uint64_t offs = 0;
564
565 while (offs < slave->mtd.size) {
566 if (mtd_block_isreserved(master, offs + slave->offset))
567 slave->mtd.ecc_stats.bbtblocks++;
568 else if (mtd_block_isbad(master, offs + slave->offset))
569 slave->mtd.ecc_stats.badblocks++;
570 offs += slave->mtd.erasesize;
571 }
572 }
573
574 out_register:
575 return slave;
576 }
577
578 static ssize_t mtd_partition_offset_show(struct device *dev,
579 struct device_attribute *attr, char *buf)
580 {
581 struct mtd_info *mtd = dev_get_drvdata(dev);
582 struct mtd_part *part = mtd_to_part(mtd);
583 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
584 }
585
586 static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
587
588 static const struct attribute *mtd_partition_attrs[] = {
589 &dev_attr_offset.attr,
590 NULL
591 };
592
593 static int mtd_add_partition_attrs(struct mtd_part *new)
594 {
595 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
596 if (ret)
597 printk(KERN_WARNING
598 "mtd: failed to create partition attrs, err=%d\n", ret);
599 return ret;
600 }
601
602 int mtd_add_partition(struct mtd_info *master, const char *name,
603 long long offset, long long length)
604 {
605 struct mtd_partition part;
606 struct mtd_part *new;
607 int ret = 0;
608
609 /* the direct offset is expected */
610 if (offset == MTDPART_OFS_APPEND ||
611 offset == MTDPART_OFS_NXTBLK)
612 return -EINVAL;
613
614 if (length == MTDPART_SIZ_FULL)
615 length = master->size - offset;
616
617 if (length <= 0)
618 return -EINVAL;
619
620 memset(&part, 0, sizeof(part));
621 part.name = name;
622 part.size = length;
623 part.offset = offset;
624
625 new = allocate_partition(master, &part, -1, offset);
626 if (IS_ERR(new))
627 return PTR_ERR(new);
628
629 mutex_lock(&mtd_partitions_mutex);
630 list_add(&new->list, &mtd_partitions);
631 mutex_unlock(&mtd_partitions_mutex);
632
633 add_mtd_device(&new->mtd);
634
635 mtd_add_partition_attrs(new);
636
637 return ret;
638 }
639 EXPORT_SYMBOL_GPL(mtd_add_partition);
640
641 int mtd_del_partition(struct mtd_info *master, int partno)
642 {
643 struct mtd_part *slave, *next;
644 int ret = -EINVAL;
645
646 mutex_lock(&mtd_partitions_mutex);
647 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
648 if ((slave->master == master) &&
649 (slave->mtd.index == partno)) {
650 sysfs_remove_files(&slave->mtd.dev.kobj,
651 mtd_partition_attrs);
652 ret = del_mtd_device(&slave->mtd);
653 if (ret < 0)
654 break;
655
656 list_del(&slave->list);
657 free_partition(slave);
658 break;
659 }
660 mutex_unlock(&mtd_partitions_mutex);
661
662 return ret;
663 }
664 EXPORT_SYMBOL_GPL(mtd_del_partition);
665
666 /*
667 * This function, given a master MTD object and a partition table, creates
668 * and registers slave MTD objects which are bound to the master according to
669 * the partition definitions.
670 *
671 * For historical reasons, this function's caller only registers the master
672 * if the MTD_PARTITIONED_MASTER config option is set.
673 */
674
675 int add_mtd_partitions(struct mtd_info *master,
676 const struct mtd_partition *parts,
677 int nbparts)
678 {
679 struct mtd_part *slave;
680 uint64_t cur_offset = 0;
681 int i;
682
683 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
684
685 for (i = 0; i < nbparts; i++) {
686 slave = allocate_partition(master, parts + i, i, cur_offset);
687 if (IS_ERR(slave)) {
688 del_mtd_partitions(master);
689 return PTR_ERR(slave);
690 }
691
692 mutex_lock(&mtd_partitions_mutex);
693 list_add(&slave->list, &mtd_partitions);
694 mutex_unlock(&mtd_partitions_mutex);
695
696 add_mtd_device(&slave->mtd);
697 mtd_add_partition_attrs(slave);
698
699 cur_offset = slave->offset + slave->mtd.size;
700 }
701
702 return 0;
703 }
704
705 static DEFINE_SPINLOCK(part_parser_lock);
706 static LIST_HEAD(part_parsers);
707
708 static struct mtd_part_parser *mtd_part_parser_get(const char *name)
709 {
710 struct mtd_part_parser *p, *ret = NULL;
711
712 spin_lock(&part_parser_lock);
713
714 list_for_each_entry(p, &part_parsers, list)
715 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
716 ret = p;
717 break;
718 }
719
720 spin_unlock(&part_parser_lock);
721
722 return ret;
723 }
724
725 static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
726 {
727 module_put(p->owner);
728 }
729
730 /*
731 * Many partition parsers just expected the core to kfree() all their data in
732 * one chunk. Do that by default.
733 */
734 static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
735 int nr_parts)
736 {
737 kfree(pparts);
738 }
739
740 int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
741 {
742 p->owner = owner;
743
744 if (!p->cleanup)
745 p->cleanup = &mtd_part_parser_cleanup_default;
746
747 spin_lock(&part_parser_lock);
748 list_add(&p->list, &part_parsers);
749 spin_unlock(&part_parser_lock);
750
751 return 0;
752 }
753 EXPORT_SYMBOL_GPL(__register_mtd_parser);
754
755 void deregister_mtd_parser(struct mtd_part_parser *p)
756 {
757 spin_lock(&part_parser_lock);
758 list_del(&p->list);
759 spin_unlock(&part_parser_lock);
760 }
761 EXPORT_SYMBOL_GPL(deregister_mtd_parser);
762
763 /*
764 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
765 * are changing this array!
766 */
767 static const char * const default_mtd_part_types[] = {
768 "cmdlinepart",
769 "ofpart",
770 NULL
771 };
772
773 /**
774 * parse_mtd_partitions - parse MTD partitions
775 * @master: the master partition (describes whole MTD device)
776 * @types: names of partition parsers to try or %NULL
777 * @pparts: info about partitions found is returned here
778 * @data: MTD partition parser-specific data
779 *
780 * This function tries to find partition on MTD device @master. It uses MTD
781 * partition parsers, specified in @types. However, if @types is %NULL, then
782 * the default list of parsers is used. The default list contains only the
783 * "cmdlinepart" and "ofpart" parsers ATM.
784 * Note: If there are more then one parser in @types, the kernel only takes the
785 * partitions parsed out by the first parser.
786 *
787 * This function may return:
788 * o a negative error code in case of failure
789 * o zero otherwise, and @pparts will describe the partitions, number of
790 * partitions, and the parser which parsed them. Caller must release
791 * resources with mtd_part_parser_cleanup() when finished with the returned
792 * data.
793 */
794 int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
795 struct mtd_partitions *pparts,
796 struct mtd_part_parser_data *data)
797 {
798 struct mtd_part_parser *parser;
799 int ret, err = 0;
800
801 if (!types)
802 types = default_mtd_part_types;
803
804 for ( ; *types; types++) {
805 pr_debug("%s: parsing partitions %s\n", master->name, *types);
806 parser = mtd_part_parser_get(*types);
807 if (!parser && !request_module("%s", *types))
808 parser = mtd_part_parser_get(*types);
809 pr_debug("%s: got parser %s\n", master->name,
810 parser ? parser->name : NULL);
811 if (!parser)
812 continue;
813 ret = (*parser->parse_fn)(master, &pparts->parts, data);
814 pr_debug("%s: parser %s: %i\n",
815 master->name, parser->name, ret);
816 if (ret > 0) {
817 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
818 ret, parser->name, master->name);
819 pparts->nr_parts = ret;
820 pparts->parser = parser;
821 return 0;
822 }
823 mtd_part_parser_put(parser);
824 /*
825 * Stash the first error we see; only report it if no parser
826 * succeeds
827 */
828 if (ret < 0 && !err)
829 err = ret;
830 }
831 return err;
832 }
833
834 void mtd_part_parser_cleanup(struct mtd_partitions *parts)
835 {
836 const struct mtd_part_parser *parser;
837
838 if (!parts)
839 return;
840
841 parser = parts->parser;
842 if (parser) {
843 if (parser->cleanup)
844 parser->cleanup(parts->parts, parts->nr_parts);
845
846 mtd_part_parser_put(parser);
847 }
848 }
849
850 int mtd_is_partition(const struct mtd_info *mtd)
851 {
852 struct mtd_part *part;
853 int ispart = 0;
854
855 mutex_lock(&mtd_partitions_mutex);
856 list_for_each_entry(part, &mtd_partitions, list)
857 if (&part->mtd == mtd) {
858 ispart = 1;
859 break;
860 }
861 mutex_unlock(&mtd_partitions_mutex);
862
863 return ispart;
864 }
865 EXPORT_SYMBOL_GPL(mtd_is_partition);
866
867 /* Returns the size of the entire flash chip */
868 uint64_t mtd_get_device_size(const struct mtd_info *mtd)
869 {
870 if (!mtd_is_partition(mtd))
871 return mtd->size;
872
873 return mtd_to_part(mtd)->master->size;
874 }
875 EXPORT_SYMBOL_GPL(mtd_get_device_size);