blob: a308e707392d595902b77a03e2078ffe8da97d9e [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
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020040/**
41 * struct mtd_part - our partition node structure
42 *
43 * @mtd: struct holding partition details
44 * @parent: parent mtd - flash device or another partition
45 * @offset: partition offset relative to the *flash device*
46 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070047struct mtd_part {
48 struct mtd_info mtd;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020049 struct mtd_info *parent;
Adrian Hunter69423d92008-12-10 13:37:21 +000050 uint64_t offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 struct list_head list;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052};
53
54/*
55 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
Brian Norris25245342015-11-19 19:28:39 -080056 * the pointer to that structure.
Linus Torvalds1da177e2005-04-16 15:20:36 -070057 */
Brian Norris25245342015-11-19 19:28:39 -080058static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
59{
60 return container_of(mtd, struct mtd_part, mtd);
61}
Linus Torvalds1da177e2005-04-16 15:20:36 -070062
Thomas Gleixner97894cd2005-11-07 11:15:26 +000063
64/*
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 * MTD methods which simply translate the effective address and pass through
66 * to the _real_ device.
67 */
68
Atsushi Nemotob33a2882008-07-19 01:00:33 +090069static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
70 size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Brian Norris25245342015-11-19 19:28:39 -080072 struct mtd_part *part = mtd_to_part(mtd);
Yauhen Kharuzhyd8877f12009-03-27 00:41:09 +020073 struct mtd_ecc_stats stats;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020074 int res;
75
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020076 stats = part->parent->ecc_stats;
77 res = part->parent->_read(part->parent, from + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -080078 retlen, buf);
Mike Dunnedbc45402012-04-25 12:06:11 -070079 if (unlikely(mtd_is_eccerr(res)))
80 mtd->ecc_stats.failed +=
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020081 part->parent->ecc_stats.failed - stats.failed;
Mike Dunnedbc45402012-04-25 12:06:11 -070082 else
83 mtd->ecc_stats.corrected +=
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020084 part->parent->ecc_stats.corrected - stats.corrected;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +020085 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -070086}
87
Atsushi Nemotob33a2882008-07-19 01:00:33 +090088static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
89 size_t *retlen, void **virt, resource_size_t *phys)
Linus Torvalds1da177e2005-04-16 15:20:36 -070090{
Brian Norris25245342015-11-19 19:28:39 -080091 struct mtd_part *part = mtd_to_part(mtd);
Artem Bityutskiy5def4892012-02-03 16:23:52 +020092
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +020093 return part->parent->_point(part->parent, from + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -080094 retlen, virt, phys);
Linus Torvalds1da177e2005-04-16 15:20:36 -070095}
Thomas Gleixner9223a452006-05-23 17:21:03 +020096
Artem Bityutskiy5e4e6e32012-02-03 13:20:43 +020097static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
Brian Norris25245342015-11-19 19:28:39 -080099 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200101 return part->parent->_unpoint(part->parent, from + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102}
103
David Howells402d3262009-02-12 10:40:00 +0000104static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
105 unsigned long len,
106 unsigned long offset,
107 unsigned long flags)
108{
Brian Norris25245342015-11-19 19:28:39 -0800109 struct mtd_part *part = mtd_to_part(mtd);
David Howells402d3262009-02-12 10:40:00 +0000110
111 offset += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200112 return part->parent->_get_unmapped_area(part->parent, len, offset,
Mike Dunn994c8402012-03-03 13:13:06 -0800113 flags);
David Howells402d3262009-02-12 10:40:00 +0000114}
115
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200116static int part_read_oob(struct mtd_info *mtd, loff_t from,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900117 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700118{
Brian Norris25245342015-11-19 19:28:39 -0800119 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200120 int res;
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200121
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122 if (from >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200123 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300124 if (ops->datbuf && from + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200125 return -EINVAL;
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200126
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200127 /*
128 * If OOB is also requested, make sure that we do not read past the end
129 * of this partition.
130 */
131 if (ops->oobbuf) {
132 size_t len, pages;
133
Boris BREZILLON29f10582016-03-07 10:46:52 +0100134 len = mtd_oobavail(mtd, ops);
Artem Bityutskiy154bf892011-01-16 17:50:54 +0200135 pages = mtd_div_by_ws(mtd->size, mtd);
136 pages -= mtd_div_by_ws(from, mtd);
137 if (ops->ooboffs + ops->ooblen > pages * len)
138 return -EINVAL;
139 }
140
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200141 res = part->parent->_read_oob(part->parent, from + part->offset, ops);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200142 if (unlikely(res)) {
Brian Norrisd57f40542011-09-20 18:34:25 -0700143 if (mtd_is_bitflip(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200144 mtd->ecc_stats.corrected++;
Brian Norrisd57f40542011-09-20 18:34:25 -0700145 if (mtd_is_eccerr(res))
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200146 mtd->ecc_stats.failed++;
147 }
148 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900151static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
152 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153{
Brian Norris25245342015-11-19 19:28:39 -0800154 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200155 return part->parent->_read_user_prot_reg(part->parent, from, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800156 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157}
158
Christian Riesch4b78fc42014-01-28 09:29:44 +0100159static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
160 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000161{
Brian Norris25245342015-11-19 19:28:39 -0800162 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200163 return part->parent->_get_user_prot_info(part->parent, len, retlen,
Christian Riesch4b78fc42014-01-28 09:29:44 +0100164 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000165}
166
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900167static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
168 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169{
Brian Norris25245342015-11-19 19:28:39 -0800170 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200171 return part->parent->_read_fact_prot_reg(part->parent, from, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800172 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173}
174
Christian Riesch4b78fc42014-01-28 09:29:44 +0100175static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
176 size_t *retlen, struct otp_info *buf)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000177{
Brian Norris25245342015-11-19 19:28:39 -0800178 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200179 return part->parent->_get_fact_prot_info(part->parent, len, retlen,
Christian Riesch4b78fc42014-01-28 09:29:44 +0100180 buf);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000181}
182
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900183static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
184 size_t *retlen, const u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185{
Brian Norris25245342015-11-19 19:28:39 -0800186 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200187 return part->parent->_write(part->parent, to + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800188 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189}
190
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900191static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
192 size_t *retlen, const u_char *buf)
Richard Purdie388bbb02008-02-06 10:17:15 +0000193{
Brian Norris25245342015-11-19 19:28:39 -0800194 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200195 return part->parent->_panic_write(part->parent, to + part->offset, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800196 retlen, buf);
Richard Purdie388bbb02008-02-06 10:17:15 +0000197}
198
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200199static int part_write_oob(struct mtd_info *mtd, loff_t to,
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900200 struct mtd_oob_ops *ops)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Brian Norris25245342015-11-19 19:28:39 -0800202 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200203
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 if (to >= mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200205 return -EINVAL;
Vitaly Wool70145682006-11-03 18:20:38 +0300206 if (ops->datbuf && to + ops->len > mtd->size)
Thomas Gleixner8593fbc2006-05-29 03:26:58 +0200207 return -EINVAL;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200208 return part->parent->_write_oob(part->parent, to + part->offset, ops);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900211static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
212 size_t len, size_t *retlen, u_char *buf)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213{
Brian Norris25245342015-11-19 19:28:39 -0800214 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200215 return part->parent->_write_user_prot_reg(part->parent, from, len,
Mike Dunn994c8402012-03-03 13:13:06 -0800216 retlen, buf);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900219static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
220 size_t len)
Nicolas Pitref77814d2005-02-08 17:11:19 +0000221{
Brian Norris25245342015-11-19 19:28:39 -0800222 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200223 return part->parent->_lock_user_prot_reg(part->parent, from, len);
Nicolas Pitref77814d2005-02-08 17:11:19 +0000224}
225
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900226static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
227 unsigned long count, loff_t to, size_t *retlen)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Brian Norris25245342015-11-19 19:28:39 -0800229 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200230 return part->parent->_writev(part->parent, vecs, count,
Mike Dunn994c8402012-03-03 13:13:06 -0800231 to + part->offset, retlen);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232}
233
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900234static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235{
Brian Norris25245342015-11-19 19:28:39 -0800236 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 int ret;
Artem Bityutskiy664addc2012-02-03 18:13:23 +0200238
Linus Torvalds1da177e2005-04-16 15:20:36 -0700239 instr->addr += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200240 ret = part->parent->_erase(part->parent, instr);
Adrian Hunter74641d72007-03-08 12:20:12 +0200241 if (ret) {
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300242 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Adrian Hunter74641d72007-03-08 12:20:12 +0200243 instr->fail_addr -= part->offset;
244 instr->addr -= part->offset;
245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 return ret;
247}
248
249void mtd_erase_callback(struct erase_info *instr)
250{
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200251 if (instr->mtd->_erase == part_erase) {
Brian Norris25245342015-11-19 19:28:39 -0800252 struct mtd_part *part = mtd_to_part(instr->mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253
Adrian Hunterbb0eb212008-08-12 12:40:50 +0300254 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 instr->fail_addr -= part->offset;
256 instr->addr -= part->offset;
257 }
258 if (instr->callback)
259 instr->callback(instr);
260}
261EXPORT_SYMBOL_GPL(mtd_erase_callback);
262
Adrian Hunter69423d92008-12-10 13:37:21 +0000263static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264{
Brian Norris25245342015-11-19 19:28:39 -0800265 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200266 return part->parent->_lock(part->parent, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267}
268
Adrian Hunter69423d92008-12-10 13:37:21 +0000269static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270{
Brian Norris25245342015-11-19 19:28:39 -0800271 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200272 return part->parent->_unlock(part->parent, ofs + part->offset, len);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700273}
274
Richard Cochran99384242010-06-14 18:10:33 +0200275static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
276{
Brian Norris25245342015-11-19 19:28:39 -0800277 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200278 return part->parent->_is_locked(part->parent, ofs + part->offset, len);
Richard Cochran99384242010-06-14 18:10:33 +0200279}
280
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281static void part_sync(struct mtd_info *mtd)
282{
Brian Norris25245342015-11-19 19:28:39 -0800283 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200284 part->parent->_sync(part->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
287static int part_suspend(struct mtd_info *mtd)
288{
Brian Norris25245342015-11-19 19:28:39 -0800289 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200290 return part->parent->_suspend(part->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291}
292
293static void part_resume(struct mtd_info *mtd)
294{
Brian Norris25245342015-11-19 19:28:39 -0800295 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200296 part->parent->_resume(part->parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297}
298
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300299static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
300{
Brian Norris25245342015-11-19 19:28:39 -0800301 struct mtd_part *part = mtd_to_part(mtd);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300302 ofs += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200303 return part->parent->_block_isreserved(part->parent, ofs);
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300304}
305
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900306static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
Brian Norris25245342015-11-19 19:28:39 -0800308 struct mtd_part *part = mtd_to_part(mtd);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 ofs += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200310 return part->parent->_block_isbad(part->parent, ofs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311}
312
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900313static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
Brian Norris25245342015-11-19 19:28:39 -0800315 struct mtd_part *part = mtd_to_part(mtd);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200316 int res;
317
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318 ofs += part->offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200319 res = part->parent->_block_markbad(part->parent, ofs);
Thomas Gleixnerf1a28c02006-05-30 00:37:34 +0200320 if (!res)
321 mtd->ecc_stats.badblocks++;
322 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323}
324
Richard Weinberger5e149072016-09-21 11:43:56 +0200325static int part_get_device(struct mtd_info *mtd)
326{
327 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200328 return part->parent->_get_device(part->parent);
Richard Weinberger5e149072016-09-21 11:43:56 +0200329}
330
331static void part_put_device(struct mtd_info *mtd)
332{
333 struct mtd_part *part = mtd_to_part(mtd);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200334 part->parent->_put_device(part->parent);
Richard Weinberger5e149072016-09-21 11:43:56 +0200335}
336
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100337static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
338 struct mtd_oob_region *oobregion)
339{
340 struct mtd_part *part = mtd_to_part(mtd);
341
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200342 return mtd_ooblayout_ecc(part->parent, section, oobregion);
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100343}
344
345static int part_ooblayout_free(struct mtd_info *mtd, int section,
346 struct mtd_oob_region *oobregion)
347{
348 struct mtd_part *part = mtd_to_part(mtd);
349
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200350 return mtd_ooblayout_free(part->parent, section, oobregion);
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100351}
352
353static const struct mtd_ooblayout_ops part_ooblayout_ops = {
354 .ecc = part_ooblayout_ecc,
355 .free = part_ooblayout_free,
356};
357
Jeff Westfahl6080ef62017-01-10 13:30:17 -0600358static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
359{
360 struct mtd_part *part = mtd_to_part(mtd);
361
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200362 return part->parent->_max_bad_blocks(part->parent,
Jeff Westfahl6080ef62017-01-10 13:30:17 -0600363 ofs + part->offset, len);
364}
365
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300366static inline void free_partition(struct mtd_part *p)
367{
368 kfree(p->mtd.name);
369 kfree(p);
370}
371
Rafał Miłecki1a0915b2017-06-21 08:26:46 +0200372/**
373 * mtd_parse_part - parse MTD partition looking for subpartitions
374 *
375 * @slave: part that is supposed to be a container and should be parsed
376 * @types: NULL-terminated array with names of partition parsers to try
377 *
378 * Some partitions are kind of containers with extra subpartitions (volumes).
379 * There can be various formats of such containers. This function tries to use
380 * specified parsers to analyze given partition and registers found
381 * subpartitions on success.
382 */
383static int mtd_parse_part(struct mtd_part *slave, const char *const *types)
384{
385 struct mtd_partitions parsed;
386 int err;
387
388 err = parse_mtd_partitions(&slave->mtd, types, &parsed, NULL);
389 if (err)
390 return err;
391 else if (!parsed.nr_parts)
392 return -ENOENT;
393
394 err = add_mtd_partitions(&slave->mtd, parsed.parts, parsed.nr_parts);
395
396 mtd_part_parser_cleanup(&parsed);
397
398 return err;
399}
400
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200401static struct mtd_part *allocate_partition(struct mtd_info *parent,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300402 const struct mtd_partition *part, int partno,
403 uint64_t cur_offset)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900404{
Brian Norrisc169e3d2017-06-22 14:09:18 -0700405 int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize :
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200406 parent->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900407 struct mtd_part *slave;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200408 u32 remainder;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300409 char *name;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200410 u64 tmp;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900411
412 /* allocate the partition structure */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900413 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300414 name = kstrdup(part->name, GFP_KERNEL);
415 if (!name || !slave) {
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900416 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200417 parent->name);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300418 kfree(name);
419 kfree(slave);
420 return ERR_PTR(-ENOMEM);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900421 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900422
423 /* set up the MTD object for this partition */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200424 slave->mtd.type = parent->type;
425 slave->mtd.flags = parent->flags & ~part->mask_flags;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900426 slave->mtd.size = part->size;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200427 slave->mtd.writesize = parent->writesize;
428 slave->mtd.writebufsize = parent->writebufsize;
429 slave->mtd.oobsize = parent->oobsize;
430 slave->mtd.oobavail = parent->oobavail;
431 slave->mtd.subpage_sft = parent->subpage_sft;
432 slave->mtd.pairing = parent->pairing;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900433
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300434 slave->mtd.name = name;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200435 slave->mtd.owner = parent->owner;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900436
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700437 /* NOTE: Historically, we didn't arrange MTDs as a tree out of
438 * concern for showing the same data in multiple partitions.
439 * However, it is very useful to have the master node present,
440 * so the MTD_PARTITIONED_MASTER option allows that. The master
441 * will have device nodes etc only if this is set, so make the
442 * parent conditional on that option. Note, this is a way to
443 * distinguish between the master and the partition in sysfs.
David Brownell1f24b5a2009-03-26 00:42:41 -0700444 */
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200445 slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200446 &parent->dev :
447 parent->dev.parent;
Sascha Hauer42e94012017-02-09 11:50:24 +0100448 slave->mtd.dev.of_node = part->of_node;
David Brownell1f24b5a2009-03-26 00:42:41 -0700449
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200450 slave->mtd._read = part_read;
451 slave->mtd._write = part_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900452
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200453 if (parent->_panic_write)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200454 slave->mtd._panic_write = part_panic_write;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900455
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200456 if (parent->_point && parent->_unpoint) {
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200457 slave->mtd._point = part_point;
458 slave->mtd._unpoint = part_unpoint;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900459 }
460
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200461 if (parent->_get_unmapped_area)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200462 slave->mtd._get_unmapped_area = part_get_unmapped_area;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200463 if (parent->_read_oob)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200464 slave->mtd._read_oob = part_read_oob;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200465 if (parent->_write_oob)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200466 slave->mtd._write_oob = part_write_oob;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200467 if (parent->_read_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200468 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200469 if (parent->_read_fact_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200470 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200471 if (parent->_write_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200472 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200473 if (parent->_lock_user_prot_reg)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200474 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200475 if (parent->_get_user_prot_info)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200476 slave->mtd._get_user_prot_info = part_get_user_prot_info;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200477 if (parent->_get_fact_prot_info)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200478 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200479 if (parent->_sync)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200480 slave->mtd._sync = part_sync;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200481 if (!partno && !parent->dev.class && parent->_suspend &&
482 parent->_resume) {
Brian Norrisc169e3d2017-06-22 14:09:18 -0700483 slave->mtd._suspend = part_suspend;
484 slave->mtd._resume = part_resume;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900485 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200486 if (parent->_writev)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200487 slave->mtd._writev = part_writev;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200488 if (parent->_lock)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200489 slave->mtd._lock = part_lock;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200490 if (parent->_unlock)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200491 slave->mtd._unlock = part_unlock;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200492 if (parent->_is_locked)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200493 slave->mtd._is_locked = part_is_locked;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200494 if (parent->_block_isreserved)
Ezequiel Garcia8471bb72014-05-21 19:06:12 -0300495 slave->mtd._block_isreserved = part_block_isreserved;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200496 if (parent->_block_isbad)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200497 slave->mtd._block_isbad = part_block_isbad;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200498 if (parent->_block_markbad)
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200499 slave->mtd._block_markbad = part_block_markbad;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200500 if (parent->_max_bad_blocks)
Jeff Westfahl6080ef62017-01-10 13:30:17 -0600501 slave->mtd._max_bad_blocks = part_max_bad_blocks;
Richard Weinberger5e149072016-09-21 11:43:56 +0200502
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200503 if (parent->_get_device)
Richard Weinberger5e149072016-09-21 11:43:56 +0200504 slave->mtd._get_device = part_get_device;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200505 if (parent->_put_device)
Richard Weinberger5e149072016-09-21 11:43:56 +0200506 slave->mtd._put_device = part_put_device;
507
Artem Bityutskiy3c3c10b2012-01-30 14:58:32 +0200508 slave->mtd._erase = part_erase;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200509 slave->parent = parent;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900510 slave->offset = part->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900511
512 if (slave->offset == MTDPART_OFS_APPEND)
513 slave->offset = cur_offset;
514 if (slave->offset == MTDPART_OFS_NXTBLK) {
Chris Packham1eeef2d2017-06-09 15:58:31 +1200515 tmp = cur_offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900516 slave->offset = cur_offset;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200517 remainder = do_div(tmp, wr_alignment);
518 if (remainder) {
519 slave->offset += wr_alignment - remainder;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900520 printk(KERN_NOTICE "Moving partition %d: "
Adrian Hunter69423d92008-12-10 13:37:21 +0000521 "0x%012llx -> 0x%012llx\n", partno,
522 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900523 }
524 }
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400525 if (slave->offset == MTDPART_OFS_RETAIN) {
526 slave->offset = cur_offset;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200527 if (parent->size - slave->offset >= slave->mtd.size) {
528 slave->mtd.size = parent->size - slave->offset
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400529 - slave->mtd.size;
530 } else {
531 printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200532 part->name, parent->size - slave->offset,
Dmitry Eremin-Solenikov1a313682011-06-06 18:04:14 +0400533 slave->mtd.size);
534 /* register to preserve ordering */
535 goto out_register;
536 }
537 }
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900538 if (slave->mtd.size == MTDPART_SIZ_FULL)
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200539 slave->mtd.size = parent->size - slave->offset;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900540
Adrian Hunter69423d92008-12-10 13:37:21 +0000541 printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
542 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900543
544 /* let's do some sanity checks */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200545 if (slave->offset >= parent->size) {
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900546 /* let's register it anyway to preserve ordering */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900547 slave->offset = 0;
548 slave->mtd.size = 0;
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900549 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900550 part->name);
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900551 goto out_register;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900552 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200553 if (slave->offset + slave->mtd.size > parent->size) {
554 slave->mtd.size = parent->size - slave->offset;
Adrian Hunter69423d92008-12-10 13:37:21 +0000555 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200556 part->name, parent->name, (unsigned long long)slave->mtd.size);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900557 }
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200558 if (parent->numeraseregions > 1) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900559 /* Deal with variable erase size stuff */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200560 int i, max = parent->numeraseregions;
Adrian Hunter69423d92008-12-10 13:37:21 +0000561 u64 end = slave->offset + slave->mtd.size;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200562 struct mtd_erase_region_info *regions = parent->eraseregions;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900563
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900564 /* Find the first erase regions which is part of this
565 * partition. */
566 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900567 ;
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900568 /* The loop searched for the region _behind_ the first one */
Roel Kluina57ca042009-09-18 12:51:50 -0700569 if (i > 0)
570 i--;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900571
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900572 /* Pick biggest erasesize */
573 for (; i < max && regions[i].offset < end; i++) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900574 if (slave->mtd.erasesize < regions[i].erasesize) {
575 slave->mtd.erasesize = regions[i].erasesize;
576 }
577 }
Atsushi Nemoto6910c132008-07-19 01:00:57 +0900578 BUG_ON(slave->mtd.erasesize == 0);
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900579 } else {
580 /* Single erase size */
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200581 slave->mtd.erasesize = parent->erasesize;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900582 }
583
Boris Brezillon7e439682017-09-25 10:19:57 +0200584 /*
585 * Slave erasesize might differ from the master one if the master
586 * exposes several regions with different erasesize. Adjust
587 * wr_alignment accordingly.
588 */
589 if (!(slave->mtd.flags & MTD_NO_ERASE))
590 wr_alignment = slave->mtd.erasesize;
591
Chris Packham1eeef2d2017-06-09 15:58:31 +1200592 tmp = slave->offset;
593 remainder = do_div(tmp, wr_alignment);
594 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900595 /* Doesn't start on a boundary of major erase size */
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900596 /* FIXME: Let it be writable if it is on a boundary of
597 * _minor_ erase size though */
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900598 slave->mtd.flags &= ~MTD_WRITEABLE;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200599 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 +0900600 part->name);
601 }
Chris Packham1eeef2d2017-06-09 15:58:31 +1200602
603 tmp = slave->mtd.size;
604 remainder = do_div(tmp, wr_alignment);
605 if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900606 slave->mtd.flags &= ~MTD_WRITEABLE;
Chris Packham1eeef2d2017-06-09 15:58:31 +1200607 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 +0900608 part->name);
609 }
610
Boris Brezillonadbbc3b2016-02-03 19:01:31 +0100611 mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200612 slave->mtd.ecc_step_size = parent->ecc_step_size;
613 slave->mtd.ecc_strength = parent->ecc_strength;
614 slave->mtd.bitflip_threshold = parent->bitflip_threshold;
Mike Dunnd062d4e2012-04-25 12:06:08 -0700615
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200616 if (parent->_block_isbad) {
Adrian Hunter69423d92008-12-10 13:37:21 +0000617 uint64_t offs = 0;
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900618
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900619 while (offs < slave->mtd.size) {
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200620 if (mtd_block_isreserved(parent, offs + slave->offset))
Ezequiel Garciafdf43a42014-03-21 08:57:44 -0300621 slave->mtd.ecc_stats.bbtblocks++;
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200622 else if (mtd_block_isbad(parent, offs + slave->offset))
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900623 slave->mtd.ecc_stats.badblocks++;
624 offs += slave->mtd.erasesize;
625 }
626 }
627
Atsushi Nemotof636ffb2008-07-19 01:01:22 +0900628out_register:
Atsushi Nemoto7788ba72008-07-19 01:00:18 +0900629 return slave;
630}
631
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700632static ssize_t mtd_partition_offset_show(struct device *dev,
633 struct device_attribute *attr, char *buf)
634{
635 struct mtd_info *mtd = dev_get_drvdata(dev);
Brian Norris25245342015-11-19 19:28:39 -0800636 struct mtd_part *part = mtd_to_part(mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700637 return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
638}
639
640static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);
641
642static const struct attribute *mtd_partition_attrs[] = {
643 &dev_attr_offset.attr,
644 NULL
645};
646
647static int mtd_add_partition_attrs(struct mtd_part *new)
648{
649 int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
650 if (ret)
651 printk(KERN_WARNING
652 "mtd: failed to create partition attrs, err=%d\n", ret);
653 return ret;
654}
655
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200656int mtd_add_partition(struct mtd_info *parent, const char *name,
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300657 long long offset, long long length)
658{
659 struct mtd_partition part;
Dan Ehrenberg3a434f62015-04-02 15:15:12 -0700660 struct mtd_part *new;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300661 int ret = 0;
662
663 /* the direct offset is expected */
664 if (offset == MTDPART_OFS_APPEND ||
665 offset == MTDPART_OFS_NXTBLK)
666 return -EINVAL;
667
668 if (length == MTDPART_SIZ_FULL)
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200669 length = parent->size - offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300670
671 if (length <= 0)
672 return -EINVAL;
673
Brian Norris93867232015-11-11 16:47:52 -0800674 memset(&part, 0, sizeof(part));
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300675 part.name = name;
676 part.size = length;
677 part.offset = offset;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300678
Rafał Miłecki0a9d72b2017-06-21 08:26:44 +0200679 new = allocate_partition(parent, &part, -1, offset);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300680 if (IS_ERR(new))
681 return PTR_ERR(new);
682
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300683 mutex_lock(&mtd_partitions_mutex);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300684 list_add(&new->list, &mtd_partitions);
685 mutex_unlock(&mtd_partitions_mutex);
686
687 add_mtd_device(&new->mtd);
688
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700689 mtd_add_partition_attrs(new);
690
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300691 return ret;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300692}
693EXPORT_SYMBOL_GPL(mtd_add_partition);
694
Rafał Miłecki08263a92017-06-21 08:26:42 +0200695/**
696 * __mtd_del_partition - delete MTD partition
697 *
698 * @priv: internal MTD struct for partition to be deleted
699 *
700 * This function must be called with the partitions mutex locked.
701 */
702static int __mtd_del_partition(struct mtd_part *priv)
703{
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200704 struct mtd_part *child, *next;
Rafał Miłecki08263a92017-06-21 08:26:42 +0200705 int err;
706
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200707 list_for_each_entry_safe(child, next, &mtd_partitions, list) {
708 if (child->parent == &priv->mtd) {
709 err = __mtd_del_partition(child);
710 if (err)
711 return err;
712 }
713 }
714
Rafał Miłeckic5ceaba2017-06-21 08:26:43 +0200715 sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);
716
Rafał Miłecki08263a92017-06-21 08:26:42 +0200717 err = del_mtd_device(&priv->mtd);
718 if (err)
719 return err;
720
721 list_del(&priv->list);
722 free_partition(priv);
723
724 return 0;
725}
726
727/*
728 * This function unregisters and destroy all slave MTD objects which are
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200729 * attached to the given MTD object.
Rafał Miłecki08263a92017-06-21 08:26:42 +0200730 */
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200731int del_mtd_partitions(struct mtd_info *mtd)
Rafał Miłecki08263a92017-06-21 08:26:42 +0200732{
733 struct mtd_part *slave, *next;
734 int ret, err = 0;
735
736 mutex_lock(&mtd_partitions_mutex);
737 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200738 if (slave->parent == mtd) {
Rafał Miłecki08263a92017-06-21 08:26:42 +0200739 ret = __mtd_del_partition(slave);
740 if (ret < 0)
741 err = ret;
742 }
743 mutex_unlock(&mtd_partitions_mutex);
744
745 return err;
746}
747
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200748int mtd_del_partition(struct mtd_info *mtd, int partno)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300749{
750 struct mtd_part *slave, *next;
751 int ret = -EINVAL;
752
753 mutex_lock(&mtd_partitions_mutex);
754 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200755 if ((slave->parent == mtd) &&
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300756 (slave->mtd.index == partno)) {
Rafał Miłecki08263a92017-06-21 08:26:42 +0200757 ret = __mtd_del_partition(slave);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300758 break;
759 }
760 mutex_unlock(&mtd_partitions_mutex);
761
762 return ret;
763}
764EXPORT_SYMBOL_GPL(mtd_del_partition);
765
Linus Torvalds1da177e2005-04-16 15:20:36 -0700766/*
767 * This function, given a master MTD object and a partition table, creates
768 * and registers slave MTD objects which are bound to the master according to
769 * the partition definitions.
David Brownell1f24b5a2009-03-26 00:42:41 -0700770 *
Dan Ehrenberg727dc612015-04-02 15:15:10 -0700771 * For historical reasons, this function's caller only registers the master
772 * if the MTD_PARTITIONED_MASTER config option is set.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 */
774
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000775int add_mtd_partitions(struct mtd_info *master,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700776 const struct mtd_partition *parts,
777 int nbparts)
778{
779 struct mtd_part *slave;
Adrian Hunter69423d92008-12-10 13:37:21 +0000780 uint64_t cur_offset = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700781 int i;
782
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900783 printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 for (i = 0; i < nbparts; i++) {
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300786 slave = allocate_partition(master, parts + i, i, cur_offset);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200787 if (IS_ERR(slave)) {
788 del_mtd_partitions(master);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300789 return PTR_ERR(slave);
Boris BREZILLONe5bae862015-07-30 12:18:03 +0200790 }
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300791
792 mutex_lock(&mtd_partitions_mutex);
793 list_add(&slave->list, &mtd_partitions);
794 mutex_unlock(&mtd_partitions_mutex);
795
796 add_mtd_device(&slave->mtd);
Dan Ehrenberga62c24d2015-04-02 15:15:11 -0700797 mtd_add_partition_attrs(slave);
Rafał Miłecki1a0915b2017-06-21 08:26:46 +0200798 if (parts[i].types)
799 mtd_parse_part(slave, parts[i].types);
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300800
Linus Torvalds1da177e2005-04-16 15:20:36 -0700801 cur_offset = slave->offset + slave->mtd.size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802 }
803
804 return 0;
805}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700806
807static DEFINE_SPINLOCK(part_parser_lock);
808static LIST_HEAD(part_parsers);
809
Brian Norris5531ae42015-12-04 15:25:15 -0800810static struct mtd_part_parser *mtd_part_parser_get(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811{
Chris Malley71a928c2008-05-19 20:11:50 +0100812 struct mtd_part_parser *p, *ret = NULL;
813
Linus Torvalds1da177e2005-04-16 15:20:36 -0700814 spin_lock(&part_parser_lock);
815
Chris Malley71a928c2008-05-19 20:11:50 +0100816 list_for_each_entry(p, &part_parsers, list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
818 ret = p;
819 break;
820 }
Chris Malley71a928c2008-05-19 20:11:50 +0100821
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 spin_unlock(&part_parser_lock);
823
824 return ret;
825}
826
Brian Norris5531ae42015-12-04 15:25:15 -0800827static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
828{
829 module_put(p->owner);
830}
Dmitry Eremin-Solenikov953b3bd2011-06-23 15:26:14 +0400831
Brian Norrisadc83bf2015-12-09 10:24:03 -0800832/*
833 * Many partition parsers just expected the core to kfree() all their data in
834 * one chunk. Do that by default.
835 */
836static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
837 int nr_parts)
838{
839 kfree(pparts);
840}
841
Brian Norrisb9eab012015-11-11 19:13:29 -0800842int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843{
Brian Norrisb9eab012015-11-11 19:13:29 -0800844 p->owner = owner;
845
Brian Norrisadc83bf2015-12-09 10:24:03 -0800846 if (!p->cleanup)
847 p->cleanup = &mtd_part_parser_cleanup_default;
848
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 spin_lock(&part_parser_lock);
850 list_add(&p->list, &part_parsers);
851 spin_unlock(&part_parser_lock);
Brian Norrisb9eab012015-11-11 19:13:29 -0800852
853 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700854}
Brian Norrisb9eab012015-11-11 19:13:29 -0800855EXPORT_SYMBOL_GPL(__register_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856
Axel Lincf3b2b12013-12-01 18:59:15 +0800857void deregister_mtd_parser(struct mtd_part_parser *p)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858{
859 spin_lock(&part_parser_lock);
860 list_del(&p->list);
861 spin_unlock(&part_parser_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
Atsushi Nemotob33a2882008-07-19 01:00:33 +0900863EXPORT_SYMBOL_GPL(deregister_mtd_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300865/*
866 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
867 * are changing this array!
868 */
Artem Bityutskiyccef4dc2013-03-12 10:51:35 +0200869static const char * const default_mtd_part_types[] = {
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400870 "cmdlinepart",
871 "ofpart",
872 NULL
873};
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400874
Brian Norris01f9c722017-05-23 07:30:20 +0200875static int mtd_part_do_parse(struct mtd_part_parser *parser,
876 struct mtd_info *master,
877 struct mtd_partitions *pparts,
878 struct mtd_part_parser_data *data)
879{
880 int ret;
881
882 ret = (*parser->parse_fn)(master, &pparts->parts, data);
883 pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
884 if (ret <= 0)
885 return ret;
886
887 pr_notice("%d %s partitions found on MTD device %s\n", ret,
888 parser->name, master->name);
889
890 pparts->nr_parts = ret;
891 pparts->parser = parser;
892
893 return ret;
894}
895
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300896/**
897 * parse_mtd_partitions - parse MTD partitions
898 * @master: the master partition (describes whole MTD device)
899 * @types: names of partition parsers to try or %NULL
Brian Norris07fd2f82015-12-04 15:25:17 -0800900 * @pparts: info about partitions found is returned here
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400901 * @data: MTD partition parser-specific data
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300902 *
903 * This function tries to find partition on MTD device @master. It uses MTD
904 * partition parsers, specified in @types. However, if @types is %NULL, then
905 * the default list of parsers is used. The default list contains only the
Dmitry Eremin-Solenikovd26c87d2011-05-29 21:32:33 +0400906 * "cmdlinepart" and "ofpart" parsers ATM.
Huang Shijiec51803d2012-08-18 13:07:41 -0400907 * Note: If there are more then one parser in @types, the kernel only takes the
908 * partitions parsed out by the first parser.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300909 *
910 * This function may return:
911 * o a negative error code in case of failure
Brian Norris07fd2f82015-12-04 15:25:17 -0800912 * o zero otherwise, and @pparts will describe the partitions, number of
Brian Norrisadc83bf2015-12-09 10:24:03 -0800913 * partitions, and the parser which parsed them. Caller must release
914 * resources with mtd_part_parser_cleanup() when finished with the returned
915 * data.
Artem Bityutskiyad274ce2011-06-08 11:42:27 +0300916 */
Artem Bityutskiy26a47342013-03-11 15:38:48 +0200917int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
Brian Norris07fd2f82015-12-04 15:25:17 -0800918 struct mtd_partitions *pparts,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400919 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700920{
921 struct mtd_part_parser *parser;
Brian Norris5a2415b2015-10-11 13:03:47 -0700922 int ret, err = 0;
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000923
Dmitry Eremin-Solenikov5c4eefb2011-06-02 18:51:16 +0400924 if (!types)
925 types = default_mtd_part_types;
926
Brian Norris5a2415b2015-10-11 13:03:47 -0700927 for ( ; *types; types++) {
Michal Suchanek8e2c9922015-08-18 15:34:07 +0000928 pr_debug("%s: parsing partitions %s\n", master->name, *types);
Brian Norris5531ae42015-12-04 15:25:15 -0800929 parser = mtd_part_parser_get(*types);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700930 if (!parser && !request_module("%s", *types))
Brian Norris5531ae42015-12-04 15:25:15 -0800931 parser = mtd_part_parser_get(*types);
Michal Suchanek8e2c9922015-08-18 15:34:07 +0000932 pr_debug("%s: got parser %s\n", master->name,
933 parser ? parser->name : NULL);
Artem Bityutskiy7c802fb2011-05-17 11:13:17 +0300934 if (!parser)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 continue;
Brian Norris01f9c722017-05-23 07:30:20 +0200936 ret = mtd_part_do_parse(parser, master, pparts, data);
937 /* Found partitions! */
938 if (ret > 0)
Brian Norris07fd2f82015-12-04 15:25:17 -0800939 return 0;
Brian Norrisadc83bf2015-12-09 10:24:03 -0800940 mtd_part_parser_put(parser);
Brian Norris5a2415b2015-10-11 13:03:47 -0700941 /*
942 * Stash the first error we see; only report it if no parser
943 * succeeds
944 */
945 if (ret < 0 && !err)
946 err = ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947 }
Brian Norris5a2415b2015-10-11 13:03:47 -0700948 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949}
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300950
Brian Norrisadc83bf2015-12-09 10:24:03 -0800951void mtd_part_parser_cleanup(struct mtd_partitions *parts)
952{
953 const struct mtd_part_parser *parser;
954
955 if (!parts)
956 return;
957
958 parser = parts->parser;
959 if (parser) {
960 if (parser->cleanup)
961 parser->cleanup(parts->parts, parts->nr_parts);
962
963 mtd_part_parser_put(parser);
964 }
965}
966
Richard Genoud5dee4672012-07-10 18:23:39 +0200967int mtd_is_partition(const struct mtd_info *mtd)
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300968{
969 struct mtd_part *part;
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200970 int ispart = 0;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300971
972 mutex_lock(&mtd_partitions_mutex);
973 list_for_each_entry(part, &mtd_partitions, list)
974 if (&part->mtd == mtd) {
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200975 ispart = 1;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300976 break;
977 }
978 mutex_unlock(&mtd_partitions_mutex);
979
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200980 return ispart;
Roman Tereshonkov5daa7b22010-09-17 13:31:41 +0300981}
Roman Tereshonkova7e93dc2010-11-23 14:17:17 +0200982EXPORT_SYMBOL_GPL(mtd_is_partition);
Richard Genoud62082e52012-07-10 18:23:40 +0200983
984/* Returns the size of the entire flash chip */
985uint64_t mtd_get_device_size(const struct mtd_info *mtd)
986{
987 if (!mtd_is_partition(mtd))
988 return mtd->size;
989
Rafał Miłecki97519dc2017-06-21 08:26:45 +0200990 return mtd_get_device_size(mtd_to_part(mtd)->parent);
Richard Genoud62082e52012-07-10 18:23:40 +0200991}
992EXPORT_SYMBOL_GPL(mtd_get_device_size);