blob: a3e3a7d074d5c7b246aed2bebdd2ed25feba16f9 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * Simple MTD partitioning layer
3 *
David Woodhousea1452a32010-08-08 20:58:20 +01004 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
5 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
6 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 *
David Woodhousea1452a32010-08-08 20:58:20 +01008 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 *
David Woodhousea1452a32010-08-08 20:58:20 +010013 * 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 *
Thomas Gleixner97894cd2005-11-07 11:15:26 +000022 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070023
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>
Linus Torvalds1da177e2005-04-16 15:20:36 -070029#include <linux/kmod.h>
30#include <linux/mtd/mtd.h>
31#include <linux/mtd/partitions.h>
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +030032#include <linux/err.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033
Jamie Ileseea72d52011-05-23 10:23:42 +010034#include "mtdcore.h"
35
Linus Torvalds1da177e2005-04-16 15:20:36 -070036/* Our partition linked list */
37static LIST_HEAD(mtd_partitions);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +030038static DEFINE_MUTEX(mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40/* Our partition node structure */
41struct mtd_part {
42 struct mtd_info mtd;
43 struct mtd_info *master;
Adrian Hunter69423d92008-12-10 13:37:21 +000044 uint64_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070045 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
48/*
49 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
50 * the pointer to that structure with this macro.
51 */
52#define PART(x) ((struct mtd_part *)(x))
53
Thomas Gleixner97894cd2005-11-07 11:15:26 +000054
55/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070056 * MTD methods which simply translate the effective address and pass through
57 * to the _real_ device.
58 */
59
Atsushi Nemotob33a2882008-07-19 01:00:33 +090060static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
61 size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070062{
63 struct mtd_part *part = PART(mtd);
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020064 struct mtd_ecc_stats stats;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020065 int res;
66
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020067 stats = part->master->ecc_stats;
Mike Dunn994c8402012-03-03 13:13:06 -080068 res = part->master->_read(part->master, from + part->offset, len,
69 retlen, buf);
Mike Dunnedbc45402012-04-25 12:06:11 -070070 if (unlikely(mtd_is_eccerr(res)))
71 mtd->ecc_stats.failed +=
72 part->master->ecc_stats.failed - stats.failed;
73 else
74 mtd->ecc_stats.corrected +=
75 part->master->ecc_stats.corrected - stats.corrected;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020076 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070077}
78
Atsushi Nemotob33a2882008-07-19 01:00:33 +090079static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
80 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070081{
82 struct mtd_part *part = PART(mtd);
Artem Bityutskiy5def4892012-02-03 16:23:52 +020083
Mike Dunn994c8402012-03-03 13:13:06 -080084 return part->master->_point(part->master, from + part->offset, len,
85 retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
Thomas Gleixner9223a452006-05-23 17:21:03 +020087
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +020088static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070089{
90 struct mtd_part *part = PART(mtd);
91
Mike Dunn994c8402012-03-03 13:13:06 -080092 return part->master->_unpoint(part->master, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093}
94
David Howells402d3262009-02-12 10:40:00 +000095static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
96 unsigned long len,
97 unsigned long offset,
98 unsigned long flags)
99{
100 struct mtd_part *part = PART(mtd);
101
102 offset += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800103 return part->master->_get_unmapped_area(part->master, len, offset,
104 flags);
David Howells402d3262009-02-12 10:40:00 +0000105}
106
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200107static int part_read_oob(struct mtd_info *mtd, loff_t from,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900108 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109{
110 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200111 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200112
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (from >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200114 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300115 if (ops->datbuf && from + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200116 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200117
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200118 /*
119 * If OOB is also requested, make sure that we do not read past the end
120 * of this partition.
121 */
122 if (ops->oobbuf) {
123 size_t len, pages;
124
Brian Norris0612b9d2011-08-30 18:45:40 -0700125 if (ops->mode == MTD_OPS_AUTO_OOB)
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200126 len = mtd->oobavail;
127 else
128 len = mtd->oobsize;
129 pages = mtd_div_by_ws(mtd->size, mtd);
130 pages -= mtd_div_by_ws(from, mtd);
131 if (ops->ooboffs + ops->ooblen > pages * len)
132 return -EINVAL;
133 }
134
Mike Dunn994c8402012-03-03 13:13:06 -0800135 res = part->master->_read_oob(part->master, from + part->offset, ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200136 if (unlikely(res)) {
Brian Norrisd57f40542011-09-20 18:34:25 -0700137 if (mtd_is_bitflip(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200138 mtd->ecc_stats.corrected++;
Brian Norrisd57f40542011-09-20 18:34:25 -0700139 if (mtd_is_eccerr(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200140 mtd->ecc_stats.failed++;
141 }
142 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143}
144
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900145static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
146 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147{
148 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800149 return part->master->_read_user_prot_reg(part->master, from, len,
150 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151}
152
Christian Riesch4b78fc42014-01-28 09:29:44 +0100153static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
154 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000155{
156 struct mtd_part *part = PART(mtd);
Christian Riesch4b78fc42014-01-28 09:29:44 +0100157 return part->master->_get_user_prot_info(part->master, len, retlen,
158 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000159}
160
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900161static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
162 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
164 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800165 return part->master->_read_fact_prot_reg(part->master, from, len,
166 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167}
168
Christian Riesch4b78fc42014-01-28 09:29:44 +0100169static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
170 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000171{
172 struct mtd_part *part = PART(mtd);
Christian Riesch4b78fc42014-01-28 09:29:44 +0100173 return part->master->_get_fact_prot_info(part->master, len, retlen,
174 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000175}
176
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900177static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
178 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179{
180 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800181 return part->master->_write(part->master, to + part->offset, len,
182 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900185static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
186 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000187{
188 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800189 return part->master->_panic_write(part->master, to + part->offset, len,
190 retlen, buf);
Richard Purdie388bbb02008-02-06 10:17:15 +0000191}
192
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200193static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900194 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195{
196 struct mtd_part *part = PART(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200197
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200199 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300200 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200201 return -EINVAL;
Mike Dunn994c8402012-03-03 13:13:06 -0800202 return part->master->_write_oob(part->master, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900205static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
206 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207{
208 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800209 return part->master->_write_user_prot_reg(part->master, from, len,
210 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211}
212
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900213static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
214 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000215{
216 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800217 return part->master->_lock_user_prot_reg(part->master, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000218}
219
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900220static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
221 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222{
223 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800224 return part->master->_writev(part->master, vecs, count,
225 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700226}
227
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900228static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
230 struct mtd_part *part = PART(mtd);
231 int ret;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 instr->addr += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800234 ret = part->master->_erase(part->master, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200235 if (ret) {
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300236 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Adrian Hunter74641d72007-03-08 12:20:12 +0200237 instr->fail_addr -= part->offset;
238 instr->addr -= part->offset;
239 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240 return ret;
241}
242
243void mtd_erase_callback(struct erase_info *instr)
244{
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200245 if (instr->mtd->_erase == part_erase) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 struct mtd_part *part = PART(instr->mtd);
247
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300248 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 instr->fail_addr -= part->offset;
250 instr->addr -= part->offset;
251 }
252 if (instr->callback)
253 instr->callback(instr);
254}
255EXPORT_SYMBOL_GPL(mtd_erase_callback);
256
Adrian Hunter69423d92008-12-10 13:37:21 +0000257static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258{
259 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800260 return part->master->_lock(part->master, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261}
262
Adrian Hunter69423d92008-12-10 13:37:21 +0000263static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
265 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800266 return part->master->_unlock(part->master, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Richard Cochran99384242010-06-14 18:10:33 +0200269static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
270{
271 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800272 return part->master->_is_locked(part->master, ofs + part->offset, len);
Richard Cochran99384242010-06-14 18:10:33 +0200273}
274
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275static void part_sync(struct mtd_info *mtd)
276{
277 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800278 part->master->_sync(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700279}
280
281static int part_suspend(struct mtd_info *mtd)
282{
283 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800284 return part->master->_suspend(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287static void part_resume(struct mtd_info *mtd)
288{
289 struct mtd_part *part = PART(mtd);
Mike Dunn994c8402012-03-03 13:13:06 -0800290 part->master->_resume(part->master);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300293static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
294{
295 struct mtd_part *part = PART(mtd);
296 ofs += part->offset;
297 return part->master->_block_isreserved(part->master, ofs);
298}
299
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900300static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700301{
302 struct mtd_part *part = PART(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303 ofs += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800304 return part->master->_block_isbad(part->master, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700305}
306
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900307static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700308{
309 struct mtd_part *part = PART(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200310 int res;
311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 ofs += part->offset;
Mike Dunn994c8402012-03-03 13:13:06 -0800313 res = part->master->_block_markbad(part->master, ofs);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200314 if (!res)
315 mtd->ecc_stats.badblocks++;
316 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317}
318
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300319static inline void free_partition(struct mtd_part *p)
320{
321 kfree(p->mtd.name);
322 kfree(p);
323}
324
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000325/*
326 * This function unregisters and destroy all slave MTD objects which are
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 * attached to the given master MTD object.
328 */
329
330int del_mtd_partitions(struct mtd_info *master)
331{
Chris Malley71a928c2008-05-19 20:11:50 +0100332 struct mtd_part *slave, *next;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300333 int ret, err = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300335 mutex_lock(&mtd_partitions_mutex);
Chris Malley71a928c2008-05-19 20:11:50 +0100336 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 if (slave->master == master) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300338 ret = del_mtd_device(&slave->mtd);
339 if (ret < 0) {
340 err = ret;
341 continue;
342 }
Chris Malley71a928c2008-05-19 20:11:50 +0100343 list_del(&slave->list);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300344 free_partition(slave);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300346 mutex_unlock(&mtd_partitions_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300348 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349}
350
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300351static struct mtd_part *allocate_partition(struct mtd_info *master,
352 const struct mtd_partition *part, int partno,
353 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900354{
355 struct mtd_part *slave;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300356 char *name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900357
358 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900359 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300360 name = kstrdup(part->name, GFP_KERNEL);
361 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900362 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300363 master->name);
364 kfree(name);
365 kfree(slave);
366 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900367 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900368
369 /* set up the MTD object for this partition */
370 slave->mtd.type = master->type;
371 slave->mtd.flags = master->flags & ~part->mask_flags;
372 slave->mtd.size = part->size;
373 slave->mtd.writesize = master->writesize;
Anatolij Gustschin7fa33ac2010-12-16 23:42:18 +0100374 slave->mtd.writebufsize = master->writebufsize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900375 slave->mtd.oobsize = master->oobsize;
376 slave->mtd.oobavail = master->oobavail;
377 slave->mtd.subpage_sft = master->subpage_sft;
378
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300379 slave->mtd.name = name;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900380 slave->mtd.owner = master->owner;
David Howells402d3262009-02-12 10:40:00 +0000381 slave->mtd.backing_dev_info = master->backing_dev_info;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900382
David Brownell1f24b5a2009-03-26 00:42:41 -0700383 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
384 * to have the same data be in two different partitions.
385 */
386 slave->mtd.dev.parent = master->dev.parent;
387
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200388 slave->mtd._read = part_read;
389 slave->mtd._write = part_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900390
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200391 if (master->_panic_write)
392 slave->mtd._panic_write = part_panic_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900393
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200394 if (master->_point && master->_unpoint) {
395 slave->mtd._point = part_point;
396 slave->mtd._unpoint = part_unpoint;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900397 }
398
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200399 if (master->_get_unmapped_area)
400 slave->mtd._get_unmapped_area = part_get_unmapped_area;
401 if (master->_read_oob)
402 slave->mtd._read_oob = part_read_oob;
403 if (master->_write_oob)
404 slave->mtd._write_oob = part_write_oob;
405 if (master->_read_user_prot_reg)
406 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
407 if (master->_read_fact_prot_reg)
408 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
409 if (master->_write_user_prot_reg)
410 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
411 if (master->_lock_user_prot_reg)
412 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
413 if (master->_get_user_prot_info)
414 slave->mtd._get_user_prot_info = part_get_user_prot_info;
415 if (master->_get_fact_prot_info)
416 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
417 if (master->_sync)
418 slave->mtd._sync = part_sync;
419 if (!partno && !master->dev.class && master->_suspend &&
420 master->_resume) {
421 slave->mtd._suspend = part_suspend;
422 slave->mtd._resume = part_resume;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900423 }
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200424 if (master->_writev)
425 slave->mtd._writev = part_writev;
426 if (master->_lock)
427 slave->mtd._lock = part_lock;
428 if (master->_unlock)
429 slave->mtd._unlock = part_unlock;
430 if (master->_is_locked)
431 slave->mtd._is_locked = part_is_locked;
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300432 if (master->_block_isreserved)
433 slave->mtd._block_isreserved = part_block_isreserved;
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200434 if (master->_block_isbad)
435 slave->mtd._block_isbad = part_block_isbad;
436 if (master->_block_markbad)
437 slave->mtd._block_markbad = part_block_markbad;
438 slave->mtd._erase = part_erase;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900439 slave->master = master;
440 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900441
442 if (slave->offset == MTDPART_OFS_APPEND)
443 slave->offset = cur_offset;
444 if (slave->offset == MTDPART_OFS_NXTBLK) {
445 slave->offset = cur_offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000446 if (mtd_mod_by_eb(cur_offset, master) != 0) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900447 /* Round up to next erasesize */
Adrian Hunter69423d92008-12-10 13:37:21 +0000448 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900449 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000450 "0x%012llx -> 0x%012llx\n", partno,
451 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900452 }
453 }
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400454 if (slave->offset == MTDPART_OFS_RETAIN) {
455 slave->offset = cur_offset;
456 if (master->size - slave->offset >= slave->mtd.size) {
457 slave->mtd.size = master->size - slave->offset
458 - slave->mtd.size;
459 } else {
460 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
461 part->name, master->size - slave->offset,
462 slave->mtd.size);
463 /* register to preserve ordering */
464 goto out_register;
465 }
466 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900467 if (slave->mtd.size == MTDPART_SIZ_FULL)
468 slave->mtd.size = master->size - slave->offset;
469
Adrian Hunter69423d92008-12-10 13:37:21 +0000470 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
471 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900472
473 /* let's do some sanity checks */
474 if (slave->offset >= master->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900475 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900476 slave->offset = 0;
477 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900478 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900479 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900480 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900481 }
482 if (slave->offset + slave->mtd.size > master->size) {
483 slave->mtd.size = master->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000484 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
485 part->name, master->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900486 }
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900487 if (master->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900488 /* Deal with variable erase size stuff */
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900489 int i, max = master->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000490 u64 end = slave->offset + slave->mtd.size;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900491 struct mtd_erase_region_info *regions = master->eraseregions;
492
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900493 /* Find the first erase regions which is part of this
494 * partition. */
495 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900496 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900497 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700498 if (i > 0)
499 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900500
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900501 /* Pick biggest erasesize */
502 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900503 if (slave->mtd.erasesize < regions[i].erasesize) {
504 slave->mtd.erasesize = regions[i].erasesize;
505 }
506 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900507 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900508 } else {
509 /* Single erase size */
510 slave->mtd.erasesize = master->erasesize;
511 }
512
513 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000514 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900515 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900516 /* FIXME: Let it be writable if it is on a boundary of
517 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900518 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900519 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900520 part->name);
521 }
522 if ((slave->mtd.flags & MTD_WRITEABLE) &&
Adrian Hunter69423d92008-12-10 13:37:21 +0000523 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900524 slave->mtd.flags &= ~MTD_WRITEABLE;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900525 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900526 part->name);
527 }
528
529 slave->mtd.ecclayout = master->ecclayout;
Huang Shijiebdf69c42013-08-16 10:10:06 +0800530 slave->mtd.ecc_step_size = master->ecc_step_size;
Mike Dunn6a918ba2012-03-11 14:21:11 -0700531 slave->mtd.ecc_strength = master->ecc_strength;
Mike Dunnd062d4e2012-04-25 12:06:08 -0700532 slave->mtd.bitflip_threshold = master->bitflip_threshold;
533
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200534 if (master->_block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000535 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900536
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900537 while (offs < slave->mtd.size) {
Ezequiel Garciafdf43a42014-03-21 08:57:44 -0300538 if (mtd_block_isreserved(master, offs + slave->offset))
539 slave->mtd.ecc_stats.bbtblocks++;
540 else if (mtd_block_isbad(master, offs + slave->offset))
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900541 slave->mtd.ecc_stats.badblocks++;
542 offs += slave->mtd.erasesize;
543 }
544 }
545
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900546out_register:
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900547 return slave;
548}
549
Geert Uytterhoeven26a6d242013-11-12 20:11:26 +0100550int mtd_add_partition(struct mtd_info *master, const char *name,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300551 long long offset, long long length)
552{
553 struct mtd_partition part;
554 struct mtd_part *p, *new;
555 uint64_t start, end;
556 int ret = 0;
557
558 /* the direct offset is expected */
559 if (offset == MTDPART_OFS_APPEND ||
560 offset == MTDPART_OFS_NXTBLK)
561 return -EINVAL;
562
563 if (length == MTDPART_SIZ_FULL)
564 length = master->size - offset;
565
566 if (length <= 0)
567 return -EINVAL;
568
569 part.name = name;
570 part.size = length;
571 part.offset = offset;
572 part.mask_flags = 0;
573 part.ecclayout = NULL;
574
575 new = allocate_partition(master, &part, -1, offset);
576 if (IS_ERR(new))
577 return PTR_ERR(new);
578
579 start = offset;
580 end = offset + length;
581
582 mutex_lock(&mtd_partitions_mutex);
583 list_for_each_entry(p, &mtd_partitions, list)
584 if (p->master == master) {
585 if ((start >= p->offset) &&
586 (start < (p->offset + p->mtd.size)))
587 goto err_inv;
588
589 if ((end >= p->offset) &&
590 (end < (p->offset + p->mtd.size)))
591 goto err_inv;
592 }
593
594 list_add(&new->list, &mtd_partitions);
595 mutex_unlock(&mtd_partitions_mutex);
596
597 add_mtd_device(&new->mtd);
598
599 return ret;
600err_inv:
601 mutex_unlock(&mtd_partitions_mutex);
602 free_partition(new);
603 return -EINVAL;
604}
605EXPORT_SYMBOL_GPL(mtd_add_partition);
606
607int mtd_del_partition(struct mtd_info *master, int partno)
608{
609 struct mtd_part *slave, *next;
610 int ret = -EINVAL;
611
612 mutex_lock(&mtd_partitions_mutex);
613 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
614 if ((slave->master == master) &&
615 (slave->mtd.index == partno)) {
616 ret = del_mtd_device(&slave->mtd);
617 if (ret < 0)
618 break;
619
620 list_del(&slave->list);
621 free_partition(slave);
622 break;
623 }
624 mutex_unlock(&mtd_partitions_mutex);
625
626 return ret;
627}
628EXPORT_SYMBOL_GPL(mtd_del_partition);
629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630/*
631 * This function, given a master MTD object and a partition table, creates
632 * and registers slave MTD objects which are bound to the master according to
633 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700634 *
635 * We don't register the master, or expect the caller to have done so,
636 * for reasons of data integrity.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 */
638
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000639int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 const struct mtd_partition *parts,
641 int nbparts)
642{
643 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000644 uint64_t cur_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 int i;
646
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900647 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648
649 for (i = 0; i < nbparts; i++) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300650 slave = allocate_partition(master, parts + i, i, cur_offset);
651 if (IS_ERR(slave))
652 return PTR_ERR(slave);
653
654 mutex_lock(&mtd_partitions_mutex);
655 list_add(&slave->list, &mtd_partitions);
656 mutex_unlock(&mtd_partitions_mutex);
657
658 add_mtd_device(&slave->mtd);
659
Linus Torvalds1da177e2005-04-16 15:20:36 -0700660 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 }
662
663 return 0;
664}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666static DEFINE_SPINLOCK(part_parser_lock);
667static LIST_HEAD(part_parsers);
668
669static struct mtd_part_parser *get_partition_parser(const char *name)
670{
Chris Malley71a928c2008-05-19 20:11:50 +0100671 struct mtd_part_parser *p, *ret = NULL;
672
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673 spin_lock(&part_parser_lock);
674
Chris Malley71a928c2008-05-19 20:11:50 +0100675 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700676 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
677 ret = p;
678 break;
679 }
Chris Malley71a928c2008-05-19 20:11:50 +0100680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 spin_unlock(&part_parser_lock);
682
683 return ret;
684}
685
Dmitry Eremin-Solenikov953b3bd2011-06-23 15:26:14 +0400686#define put_partition_parser(p) do { module_put((p)->owner); } while (0)
687
Axel Lin6e14a612013-12-01 19:01:06 +0800688void register_mtd_parser(struct mtd_part_parser *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689{
690 spin_lock(&part_parser_lock);
691 list_add(&p->list, &part_parsers);
692 spin_unlock(&part_parser_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700693}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900694EXPORT_SYMBOL_GPL(register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
Axel Lincf3b2b12013-12-01 18:59:15 +0800696void deregister_mtd_parser(struct mtd_part_parser *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 spin_lock(&part_parser_lock);
699 list_del(&p->list);
700 spin_unlock(&part_parser_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700701}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900702EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300704/*
705 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
706 * are changing this array!
707 */
Artem Bityutskiyccef4dc2013-03-12 10:51:35 +0200708static const char * const default_mtd_part_types[] = {
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400709 "cmdlinepart",
710 "ofpart",
711 NULL
712};
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400713
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300714/**
715 * parse_mtd_partitions - parse MTD partitions
716 * @master: the master partition (describes whole MTD device)
717 * @types: names of partition parsers to try or %NULL
718 * @pparts: array of partitions found is returned here
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400719 * @data: MTD partition parser-specific data
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300720 *
721 * This function tries to find partition on MTD device @master. It uses MTD
722 * partition parsers, specified in @types. However, if @types is %NULL, then
723 * the default list of parsers is used. The default list contains only the
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400724 * "cmdlinepart" and "ofpart" parsers ATM.
Huang Shijiec51803d2012-08-18 13:07:41 -0400725 * Note: If there are more then one parser in @types, the kernel only takes the
726 * partitions parsed out by the first parser.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300727 *
728 * This function may return:
729 * o a negative error code in case of failure
730 * o zero if no partitions were found
731 * o a positive number of found partitions, in which case on exit @pparts will
732 * point to an array containing this number of &struct mtd_info objects.
733 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200734int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400735 struct mtd_partition **pparts,
736 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737{
738 struct mtd_part_parser *parser;
739 int ret = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000740
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400741 if (!types)
742 types = default_mtd_part_types;
743
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 for ( ; ret <= 0 && *types; types++) {
745 parser = get_partition_parser(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700746 if (!parser && !request_module("%s", *types))
Stefan Roese58edc902012-01-25 11:24:36 +0100747 parser = get_partition_parser(*types);
Artem Bityutskiy7c802fb2011-05-17 11:13:17 +0300748 if (!parser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700749 continue;
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400750 ret = (*parser->parse_fn)(master, pparts, data);
Huang Shijiec51803d2012-08-18 13:07:41 -0400751 put_partition_parser(parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 if (ret > 0) {
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000753 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 ret, parser->name, master->name);
Huang Shijiec51803d2012-08-18 13:07:41 -0400755 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700757 }
758 return ret;
759}
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300760
Richard Genoud5dee4672012-07-10 18:23:39 +0200761int mtd_is_partition(const struct mtd_info *mtd)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300762{
763 struct mtd_part *part;
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200764 int ispart = 0;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300765
766 mutex_lock(&mtd_partitions_mutex);
767 list_for_each_entry(part, &mtd_partitions, list)
768 if (&part->mtd == mtd) {
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200769 ispart = 1;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300770 break;
771 }
772 mutex_unlock(&mtd_partitions_mutex);
773
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200774 return ispart;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300775}
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200776EXPORT_SYMBOL_GPL(mtd_is_partition);
Richard Genoud62082e52012-07-10 18:23:40 +0200777
778/* Returns the size of the entire flash chip */
779uint64_t mtd_get_device_size(const struct mtd_info *mtd)
780{
781 if (!mtd_is_partition(mtd))
782 return mtd->size;
783
784 return PART(mtd)->master->size;
785}
786EXPORT_SYMBOL_GPL(mtd_get_device_size);