Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Simple MTD partitioning layer |
| 3 | * |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 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. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 12 | * |
David Woodhouse | a1452a3 | 2010-08-08 20:58:20 +0100 | [diff] [blame] | 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 | * |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 22 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 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> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 29 | #include <linux/kmod.h> |
| 30 | #include <linux/mtd/mtd.h> |
| 31 | #include <linux/mtd/partitions.h> |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 32 | #include <linux/err.h> |
Rafał Miłecki | 5b644aa | 2018-03-14 13:10:42 +0100 | [diff] [blame] | 33 | #include <linux/of.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 34 | |
Jamie Iles | eea72d5 | 2011-05-23 10:23:42 +0100 | [diff] [blame] | 35 | #include "mtdcore.h" |
| 36 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | /* Our partition linked list */ |
| 38 | static LIST_HEAD(mtd_partitions); |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 39 | static DEFINE_MUTEX(mtd_partitions_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 40 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 41 | /** |
| 42 | * struct mtd_part - our partition node structure |
| 43 | * |
| 44 | * @mtd: struct holding partition details |
| 45 | * @parent: parent mtd - flash device or another partition |
| 46 | * @offset: partition offset relative to the *flash device* |
| 47 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | struct mtd_part { |
| 49 | struct mtd_info mtd; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 50 | struct mtd_info *parent; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 51 | uint64_t offset; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | struct list_head list; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 53 | }; |
| 54 | |
| 55 | /* |
| 56 | * Given a pointer to the MTD object in the mtd_part structure, we can retrieve |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 57 | * the pointer to that structure. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | */ |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 59 | static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd) |
| 60 | { |
| 61 | return container_of(mtd, struct mtd_part, mtd); |
| 62 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 63 | |
Rafał Miłecki | 6750f61 | 2018-11-20 10:24:09 +0100 | [diff] [blame] | 64 | static u64 part_absolute_offset(struct mtd_info *mtd) |
| 65 | { |
| 66 | struct mtd_part *part = mtd_to_part(mtd); |
| 67 | |
| 68 | if (!mtd_is_partition(mtd)) |
| 69 | return 0; |
| 70 | |
| 71 | return part_absolute_offset(part->parent) + part->offset; |
| 72 | } |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 73 | |
| 74 | /* |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * MTD methods which simply translate the effective address and pass through |
| 76 | * to the _real_ device. |
| 77 | */ |
| 78 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 79 | static int part_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 80 | size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 82 | struct mtd_part *part = mtd_to_part(mtd); |
Yauhen Kharuzhy | d8877f1 | 2009-03-27 00:41:09 +0200 | [diff] [blame] | 83 | struct mtd_ecc_stats stats; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 84 | int res; |
| 85 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 86 | stats = part->parent->ecc_stats; |
| 87 | res = part->parent->_read(part->parent, from + part->offset, len, |
Mike Dunn | 994c840 | 2012-03-03 13:13:06 -0800 | [diff] [blame] | 88 | retlen, buf); |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 89 | if (unlikely(mtd_is_eccerr(res))) |
| 90 | mtd->ecc_stats.failed += |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 91 | part->parent->ecc_stats.failed - stats.failed; |
Mike Dunn | edbc4540 | 2012-04-25 12:06:11 -0700 | [diff] [blame] | 92 | else |
| 93 | mtd->ecc_stats.corrected += |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 94 | part->parent->ecc_stats.corrected - stats.corrected; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 95 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 98 | static int part_point(struct mtd_info *mtd, loff_t from, size_t len, |
| 99 | size_t *retlen, void **virt, resource_size_t *phys) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 100 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 101 | struct mtd_part *part = mtd_to_part(mtd); |
Artem Bityutskiy | 5def489 | 2012-02-03 16:23:52 +0200 | [diff] [blame] | 102 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 103 | return part->parent->_point(part->parent, from + part->offset, len, |
Mike Dunn | 994c840 | 2012-03-03 13:13:06 -0800 | [diff] [blame] | 104 | retlen, virt, phys); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 105 | } |
Thomas Gleixner | 9223a45 | 2006-05-23 17:21:03 +0200 | [diff] [blame] | 106 | |
Artem Bityutskiy | 5e4e6e3 | 2012-02-03 13:20:43 +0200 | [diff] [blame] | 107 | static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 109 | struct mtd_part *part = mtd_to_part(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 111 | return part->parent->_unpoint(part->parent, from + part->offset, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 112 | } |
| 113 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 114 | static int part_read_oob(struct mtd_info *mtd, loff_t from, |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 115 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 116 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 117 | struct mtd_part *part = mtd_to_part(mtd); |
Boris Brezillon | d020fc8 | 2018-01-09 09:50:33 +0100 | [diff] [blame] | 118 | struct mtd_ecc_stats stats; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 119 | int res; |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 120 | |
Boris Brezillon | d020fc8 | 2018-01-09 09:50:33 +0100 | [diff] [blame] | 121 | stats = part->parent->ecc_stats; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 122 | res = part->parent->_read_oob(part->parent, from + part->offset, ops); |
Boris Brezillon | d020fc8 | 2018-01-09 09:50:33 +0100 | [diff] [blame] | 123 | if (unlikely(mtd_is_eccerr(res))) |
| 124 | mtd->ecc_stats.failed += |
| 125 | part->parent->ecc_stats.failed - stats.failed; |
| 126 | else |
| 127 | mtd->ecc_stats.corrected += |
| 128 | part->parent->ecc_stats.corrected - stats.corrected; |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 129 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 130 | } |
| 131 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 132 | static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 133 | size_t len, size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 134 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 135 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 136 | return part->parent->_read_user_prot_reg(part->parent, from, len, |
Mike Dunn | 994c840 | 2012-03-03 13:13:06 -0800 | [diff] [blame] | 137 | retlen, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 140 | static int part_get_user_prot_info(struct mtd_info *mtd, size_t len, |
| 141 | size_t *retlen, struct otp_info *buf) |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 142 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 143 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 144 | return part->parent->_get_user_prot_info(part->parent, len, retlen, |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 145 | buf); |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 146 | } |
| 147 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 148 | static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, |
| 149 | size_t len, size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 150 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 151 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 152 | return part->parent->_read_fact_prot_reg(part->parent, from, len, |
Mike Dunn | 994c840 | 2012-03-03 13:13:06 -0800 | [diff] [blame] | 153 | retlen, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 156 | static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len, |
| 157 | size_t *retlen, struct otp_info *buf) |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 158 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 159 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 160 | return part->parent->_get_fact_prot_info(part->parent, len, retlen, |
Christian Riesch | 4b78fc4 | 2014-01-28 09:29:44 +0100 | [diff] [blame] | 161 | buf); |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 162 | } |
| 163 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 164 | static int part_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 165 | size_t *retlen, const u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 166 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 167 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 168 | return part->parent->_write(part->parent, to + part->offset, len, |
Mike Dunn | 994c840 | 2012-03-03 13:13:06 -0800 | [diff] [blame] | 169 | retlen, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 172 | static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 173 | size_t *retlen, const u_char *buf) |
Richard Purdie | 388bbb0 | 2008-02-06 10:17:15 +0000 | [diff] [blame] | 174 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 175 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 176 | return part->parent->_panic_write(part->parent, to + part->offset, len, |
Mike Dunn | 994c840 | 2012-03-03 13:13:06 -0800 | [diff] [blame] | 177 | retlen, buf); |
Richard Purdie | 388bbb0 | 2008-02-06 10:17:15 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 180 | static int part_write_oob(struct mtd_info *mtd, loff_t to, |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 181 | struct mtd_oob_ops *ops) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 182 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 183 | struct mtd_part *part = mtd_to_part(mtd); |
Thomas Gleixner | 8593fbc | 2006-05-29 03:26:58 +0200 | [diff] [blame] | 184 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 185 | return part->parent->_write_oob(part->parent, to + part->offset, ops); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 188 | static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 189 | size_t len, size_t *retlen, u_char *buf) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 190 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 191 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 192 | return part->parent->_write_user_prot_reg(part->parent, from, len, |
Mike Dunn | 994c840 | 2012-03-03 13:13:06 -0800 | [diff] [blame] | 193 | retlen, buf); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 196 | static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 197 | size_t len) |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 198 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 199 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 200 | return part->parent->_lock_user_prot_reg(part->parent, from, len); |
Nicolas Pitre | f77814d | 2005-02-08 17:11:19 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 203 | static int part_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 204 | unsigned long count, loff_t to, size_t *retlen) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 205 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 206 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 207 | return part->parent->_writev(part->parent, vecs, count, |
Mike Dunn | 994c840 | 2012-03-03 13:13:06 -0800 | [diff] [blame] | 208 | to + part->offset, retlen); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 211 | static int part_erase(struct mtd_info *mtd, struct erase_info *instr) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 213 | struct mtd_part *part = mtd_to_part(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | int ret; |
Artem Bityutskiy | 664addc | 2012-02-03 18:13:23 +0200 | [diff] [blame] | 215 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | instr->addr += part->offset; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 217 | ret = part->parent->_erase(part->parent, instr); |
Boris Brezillon | 8f347c4 | 2018-02-12 22:03:10 +0100 | [diff] [blame] | 218 | if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) |
| 219 | instr->fail_addr -= part->offset; |
| 220 | instr->addr -= part->offset; |
| 221 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 222 | return ret; |
| 223 | } |
| 224 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 225 | static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 227 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 228 | return part->parent->_lock(part->parent, ofs + part->offset, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | } |
| 230 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 231 | static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 232 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 233 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 234 | return part->parent->_unlock(part->parent, ofs + part->offset, len); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
Richard Cochran | 9938424 | 2010-06-14 18:10:33 +0200 | [diff] [blame] | 237 | static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 238 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 239 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 240 | return part->parent->_is_locked(part->parent, ofs + part->offset, len); |
Richard Cochran | 9938424 | 2010-06-14 18:10:33 +0200 | [diff] [blame] | 241 | } |
| 242 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 243 | static void part_sync(struct mtd_info *mtd) |
| 244 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 245 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 246 | part->parent->_sync(part->parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static int part_suspend(struct mtd_info *mtd) |
| 250 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 251 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 252 | return part->parent->_suspend(part->parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | static void part_resume(struct mtd_info *mtd) |
| 256 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 257 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 258 | part->parent->_resume(part->parent); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 259 | } |
| 260 | |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 261 | static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 262 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 263 | struct mtd_part *part = mtd_to_part(mtd); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 264 | ofs += part->offset; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 265 | return part->parent->_block_isreserved(part->parent, ofs); |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 266 | } |
| 267 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 268 | static int part_block_isbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 269 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 270 | struct mtd_part *part = mtd_to_part(mtd); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 271 | ofs += part->offset; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 272 | return part->parent->_block_isbad(part->parent, ofs); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 273 | } |
| 274 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 275 | static int part_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 276 | { |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 277 | struct mtd_part *part = mtd_to_part(mtd); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 278 | int res; |
| 279 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 280 | ofs += part->offset; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 281 | res = part->parent->_block_markbad(part->parent, ofs); |
Thomas Gleixner | f1a28c0 | 2006-05-30 00:37:34 +0200 | [diff] [blame] | 282 | if (!res) |
| 283 | mtd->ecc_stats.badblocks++; |
| 284 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 285 | } |
| 286 | |
Richard Weinberger | 5e14907 | 2016-09-21 11:43:56 +0200 | [diff] [blame] | 287 | static int part_get_device(struct mtd_info *mtd) |
| 288 | { |
| 289 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 290 | return part->parent->_get_device(part->parent); |
Richard Weinberger | 5e14907 | 2016-09-21 11:43:56 +0200 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static void part_put_device(struct mtd_info *mtd) |
| 294 | { |
| 295 | struct mtd_part *part = mtd_to_part(mtd); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 296 | part->parent->_put_device(part->parent); |
Richard Weinberger | 5e14907 | 2016-09-21 11:43:56 +0200 | [diff] [blame] | 297 | } |
| 298 | |
Boris Brezillon | adbbc3b | 2016-02-03 19:01:31 +0100 | [diff] [blame] | 299 | static int part_ooblayout_ecc(struct mtd_info *mtd, int section, |
| 300 | struct mtd_oob_region *oobregion) |
| 301 | { |
| 302 | struct mtd_part *part = mtd_to_part(mtd); |
| 303 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 304 | return mtd_ooblayout_ecc(part->parent, section, oobregion); |
Boris Brezillon | adbbc3b | 2016-02-03 19:01:31 +0100 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | static int part_ooblayout_free(struct mtd_info *mtd, int section, |
| 308 | struct mtd_oob_region *oobregion) |
| 309 | { |
| 310 | struct mtd_part *part = mtd_to_part(mtd); |
| 311 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 312 | return mtd_ooblayout_free(part->parent, section, oobregion); |
Boris Brezillon | adbbc3b | 2016-02-03 19:01:31 +0100 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | static const struct mtd_ooblayout_ops part_ooblayout_ops = { |
| 316 | .ecc = part_ooblayout_ecc, |
| 317 | .free = part_ooblayout_free, |
| 318 | }; |
| 319 | |
Jeff Westfahl | 6080ef6 | 2017-01-10 13:30:17 -0600 | [diff] [blame] | 320 | static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len) |
| 321 | { |
| 322 | struct mtd_part *part = mtd_to_part(mtd); |
| 323 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 324 | return part->parent->_max_bad_blocks(part->parent, |
Jeff Westfahl | 6080ef6 | 2017-01-10 13:30:17 -0600 | [diff] [blame] | 325 | ofs + part->offset, len); |
| 326 | } |
| 327 | |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 328 | static inline void free_partition(struct mtd_part *p) |
| 329 | { |
| 330 | kfree(p->mtd.name); |
| 331 | kfree(p); |
| 332 | } |
| 333 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 334 | static struct mtd_part *allocate_partition(struct mtd_info *parent, |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 335 | const struct mtd_partition *part, int partno, |
| 336 | uint64_t cur_offset) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 337 | { |
Brian Norris | c169e3d | 2017-06-22 14:09:18 -0700 | [diff] [blame] | 338 | int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize : |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 339 | parent->erasesize; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 340 | struct mtd_part *slave; |
Chris Packham | 1eeef2d | 2017-06-09 15:58:31 +1200 | [diff] [blame] | 341 | u32 remainder; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 342 | char *name; |
Chris Packham | 1eeef2d | 2017-06-09 15:58:31 +1200 | [diff] [blame] | 343 | u64 tmp; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 344 | |
| 345 | /* allocate the partition structure */ |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 346 | slave = kzalloc(sizeof(*slave), GFP_KERNEL); |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 347 | name = kstrdup(part->name, GFP_KERNEL); |
| 348 | if (!name || !slave) { |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 349 | printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 350 | parent->name); |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 351 | kfree(name); |
| 352 | kfree(slave); |
| 353 | return ERR_PTR(-ENOMEM); |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 354 | } |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 355 | |
| 356 | /* set up the MTD object for this partition */ |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 357 | slave->mtd.type = parent->type; |
Rafał Miłecki | 1186af4 | 2018-11-20 09:55:45 +0100 | [diff] [blame] | 358 | slave->mtd.flags = parent->orig_flags & ~part->mask_flags; |
| 359 | slave->mtd.orig_flags = slave->mtd.flags; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 360 | slave->mtd.size = part->size; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 361 | slave->mtd.writesize = parent->writesize; |
| 362 | slave->mtd.writebufsize = parent->writebufsize; |
| 363 | slave->mtd.oobsize = parent->oobsize; |
| 364 | slave->mtd.oobavail = parent->oobavail; |
| 365 | slave->mtd.subpage_sft = parent->subpage_sft; |
| 366 | slave->mtd.pairing = parent->pairing; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 367 | |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 368 | slave->mtd.name = name; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 369 | slave->mtd.owner = parent->owner; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 370 | |
Dan Ehrenberg | 727dc61 | 2015-04-02 15:15:10 -0700 | [diff] [blame] | 371 | /* NOTE: Historically, we didn't arrange MTDs as a tree out of |
| 372 | * concern for showing the same data in multiple partitions. |
| 373 | * However, it is very useful to have the master node present, |
| 374 | * so the MTD_PARTITIONED_MASTER option allows that. The master |
| 375 | * will have device nodes etc only if this is set, so make the |
| 376 | * parent conditional on that option. Note, this is a way to |
| 377 | * distinguish between the master and the partition in sysfs. |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 378 | */ |
Rafał Miłecki | 97519dc | 2017-06-21 08:26:45 +0200 | [diff] [blame] | 379 | slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ? |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 380 | &parent->dev : |
| 381 | parent->dev.parent; |
Sascha Hauer | 42e9401 | 2017-02-09 11:50:24 +0100 | [diff] [blame] | 382 | slave->mtd.dev.of_node = part->of_node; |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 383 | |
Boris Brezillon | 24ff129 | 2018-01-09 09:50:34 +0100 | [diff] [blame] | 384 | if (parent->_read) |
| 385 | slave->mtd._read = part_read; |
| 386 | if (parent->_write) |
| 387 | slave->mtd._write = part_write; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 388 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 389 | if (parent->_panic_write) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 390 | slave->mtd._panic_write = part_panic_write; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 391 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 392 | if (parent->_point && parent->_unpoint) { |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 393 | slave->mtd._point = part_point; |
| 394 | slave->mtd._unpoint = part_unpoint; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 395 | } |
| 396 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 397 | if (parent->_read_oob) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 398 | slave->mtd._read_oob = part_read_oob; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 399 | if (parent->_write_oob) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 400 | slave->mtd._write_oob = part_write_oob; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 401 | if (parent->_read_user_prot_reg) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 402 | slave->mtd._read_user_prot_reg = part_read_user_prot_reg; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 403 | if (parent->_read_fact_prot_reg) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 404 | slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 405 | if (parent->_write_user_prot_reg) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 406 | slave->mtd._write_user_prot_reg = part_write_user_prot_reg; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 407 | if (parent->_lock_user_prot_reg) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 408 | slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 409 | if (parent->_get_user_prot_info) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 410 | slave->mtd._get_user_prot_info = part_get_user_prot_info; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 411 | if (parent->_get_fact_prot_info) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 412 | slave->mtd._get_fact_prot_info = part_get_fact_prot_info; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 413 | if (parent->_sync) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 414 | slave->mtd._sync = part_sync; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 415 | if (!partno && !parent->dev.class && parent->_suspend && |
| 416 | parent->_resume) { |
Brian Norris | c169e3d | 2017-06-22 14:09:18 -0700 | [diff] [blame] | 417 | slave->mtd._suspend = part_suspend; |
| 418 | slave->mtd._resume = part_resume; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 419 | } |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 420 | if (parent->_writev) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 421 | slave->mtd._writev = part_writev; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 422 | if (parent->_lock) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 423 | slave->mtd._lock = part_lock; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 424 | if (parent->_unlock) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 425 | slave->mtd._unlock = part_unlock; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 426 | if (parent->_is_locked) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 427 | slave->mtd._is_locked = part_is_locked; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 428 | if (parent->_block_isreserved) |
Ezequiel Garcia | 8471bb7 | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 429 | slave->mtd._block_isreserved = part_block_isreserved; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 430 | if (parent->_block_isbad) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 431 | slave->mtd._block_isbad = part_block_isbad; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 432 | if (parent->_block_markbad) |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 433 | slave->mtd._block_markbad = part_block_markbad; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 434 | if (parent->_max_bad_blocks) |
Jeff Westfahl | 6080ef6 | 2017-01-10 13:30:17 -0600 | [diff] [blame] | 435 | slave->mtd._max_bad_blocks = part_max_bad_blocks; |
Richard Weinberger | 5e14907 | 2016-09-21 11:43:56 +0200 | [diff] [blame] | 436 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 437 | if (parent->_get_device) |
Richard Weinberger | 5e14907 | 2016-09-21 11:43:56 +0200 | [diff] [blame] | 438 | slave->mtd._get_device = part_get_device; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 439 | if (parent->_put_device) |
Richard Weinberger | 5e14907 | 2016-09-21 11:43:56 +0200 | [diff] [blame] | 440 | slave->mtd._put_device = part_put_device; |
| 441 | |
Artem Bityutskiy | 3c3c10b | 2012-01-30 14:58:32 +0200 | [diff] [blame] | 442 | slave->mtd._erase = part_erase; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 443 | slave->parent = parent; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 444 | slave->offset = part->offset; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 445 | |
| 446 | if (slave->offset == MTDPART_OFS_APPEND) |
| 447 | slave->offset = cur_offset; |
| 448 | if (slave->offset == MTDPART_OFS_NXTBLK) { |
Chris Packham | 1eeef2d | 2017-06-09 15:58:31 +1200 | [diff] [blame] | 449 | tmp = cur_offset; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 450 | slave->offset = cur_offset; |
Chris Packham | 1eeef2d | 2017-06-09 15:58:31 +1200 | [diff] [blame] | 451 | remainder = do_div(tmp, wr_alignment); |
| 452 | if (remainder) { |
| 453 | slave->offset += wr_alignment - remainder; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 454 | printk(KERN_NOTICE "Moving partition %d: " |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 455 | "0x%012llx -> 0x%012llx\n", partno, |
| 456 | (unsigned long long)cur_offset, (unsigned long long)slave->offset); |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 457 | } |
| 458 | } |
Dmitry Eremin-Solenikov | 1a31368 | 2011-06-06 18:04:14 +0400 | [diff] [blame] | 459 | if (slave->offset == MTDPART_OFS_RETAIN) { |
| 460 | slave->offset = cur_offset; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 461 | if (parent->size - slave->offset >= slave->mtd.size) { |
| 462 | slave->mtd.size = parent->size - slave->offset |
Dmitry Eremin-Solenikov | 1a31368 | 2011-06-06 18:04:14 +0400 | [diff] [blame] | 463 | - slave->mtd.size; |
| 464 | } else { |
| 465 | printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n", |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 466 | part->name, parent->size - slave->offset, |
Dmitry Eremin-Solenikov | 1a31368 | 2011-06-06 18:04:14 +0400 | [diff] [blame] | 467 | slave->mtd.size); |
| 468 | /* register to preserve ordering */ |
| 469 | goto out_register; |
| 470 | } |
| 471 | } |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 472 | if (slave->mtd.size == MTDPART_SIZ_FULL) |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 473 | slave->mtd.size = parent->size - slave->offset; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 474 | |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 475 | printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset, |
| 476 | (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name); |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 477 | |
| 478 | /* let's do some sanity checks */ |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 479 | if (slave->offset >= parent->size) { |
Atsushi Nemoto | f636ffb | 2008-07-19 01:01:22 +0900 | [diff] [blame] | 480 | /* let's register it anyway to preserve ordering */ |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 481 | slave->offset = 0; |
| 482 | slave->mtd.size = 0; |
Boris Brezillon | ad46351 | 2019-01-30 12:55:52 +0100 | [diff] [blame] | 483 | |
| 484 | /* Initialize ->erasesize to make add_mtd_device() happy. */ |
| 485 | slave->mtd.erasesize = parent->erasesize; |
| 486 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 487 | printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 488 | part->name); |
Atsushi Nemoto | f636ffb | 2008-07-19 01:01:22 +0900 | [diff] [blame] | 489 | goto out_register; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 490 | } |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 491 | if (slave->offset + slave->mtd.size > parent->size) { |
| 492 | slave->mtd.size = parent->size - slave->offset; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 493 | printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n", |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 494 | part->name, parent->name, (unsigned long long)slave->mtd.size); |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 495 | } |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 496 | if (parent->numeraseregions > 1) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 497 | /* Deal with variable erase size stuff */ |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 498 | int i, max = parent->numeraseregions; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 499 | u64 end = slave->offset + slave->mtd.size; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 500 | struct mtd_erase_region_info *regions = parent->eraseregions; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 501 | |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 502 | /* Find the first erase regions which is part of this |
| 503 | * partition. */ |
| 504 | for (i = 0; i < max && regions[i].offset <= slave->offset; i++) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 505 | ; |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 506 | /* The loop searched for the region _behind_ the first one */ |
Roel Kluin | a57ca04 | 2009-09-18 12:51:50 -0700 | [diff] [blame] | 507 | if (i > 0) |
| 508 | i--; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 509 | |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 510 | /* Pick biggest erasesize */ |
| 511 | for (; i < max && regions[i].offset < end; i++) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 512 | if (slave->mtd.erasesize < regions[i].erasesize) { |
| 513 | slave->mtd.erasesize = regions[i].erasesize; |
| 514 | } |
| 515 | } |
Atsushi Nemoto | 6910c13 | 2008-07-19 01:00:57 +0900 | [diff] [blame] | 516 | BUG_ON(slave->mtd.erasesize == 0); |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 517 | } else { |
| 518 | /* Single erase size */ |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 519 | slave->mtd.erasesize = parent->erasesize; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 520 | } |
| 521 | |
Boris Brezillon | 7e43968 | 2017-09-25 10:19:57 +0200 | [diff] [blame] | 522 | /* |
| 523 | * Slave erasesize might differ from the master one if the master |
| 524 | * exposes several regions with different erasesize. Adjust |
| 525 | * wr_alignment accordingly. |
| 526 | */ |
| 527 | if (!(slave->mtd.flags & MTD_NO_ERASE)) |
| 528 | wr_alignment = slave->mtd.erasesize; |
| 529 | |
Rafał Miłecki | 6750f61 | 2018-11-20 10:24:09 +0100 | [diff] [blame] | 530 | tmp = part_absolute_offset(parent) + slave->offset; |
Chris Packham | 1eeef2d | 2017-06-09 15:58:31 +1200 | [diff] [blame] | 531 | remainder = do_div(tmp, wr_alignment); |
| 532 | if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 533 | /* Doesn't start on a boundary of major erase size */ |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 534 | /* FIXME: Let it be writable if it is on a boundary of |
| 535 | * _minor_ erase size though */ |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 536 | slave->mtd.flags &= ~MTD_WRITEABLE; |
Chris Packham | 1eeef2d | 2017-06-09 15:58:31 +1200 | [diff] [blame] | 537 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n", |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 538 | part->name); |
| 539 | } |
Chris Packham | 1eeef2d | 2017-06-09 15:58:31 +1200 | [diff] [blame] | 540 | |
Rafał Miłecki | 6750f61 | 2018-11-20 10:24:09 +0100 | [diff] [blame] | 541 | tmp = part_absolute_offset(parent) + slave->mtd.size; |
Chris Packham | 1eeef2d | 2017-06-09 15:58:31 +1200 | [diff] [blame] | 542 | remainder = do_div(tmp, wr_alignment); |
| 543 | if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) { |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 544 | slave->mtd.flags &= ~MTD_WRITEABLE; |
Chris Packham | 1eeef2d | 2017-06-09 15:58:31 +1200 | [diff] [blame] | 545 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n", |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 546 | part->name); |
| 547 | } |
| 548 | |
Boris Brezillon | adbbc3b | 2016-02-03 19:01:31 +0100 | [diff] [blame] | 549 | mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops); |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 550 | slave->mtd.ecc_step_size = parent->ecc_step_size; |
| 551 | slave->mtd.ecc_strength = parent->ecc_strength; |
| 552 | slave->mtd.bitflip_threshold = parent->bitflip_threshold; |
Mike Dunn | d062d4e | 2012-04-25 12:06:08 -0700 | [diff] [blame] | 553 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 554 | if (parent->_block_isbad) { |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 555 | uint64_t offs = 0; |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 556 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 557 | while (offs < slave->mtd.size) { |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 558 | if (mtd_block_isreserved(parent, offs + slave->offset)) |
Ezequiel Garcia | fdf43a4 | 2014-03-21 08:57:44 -0300 | [diff] [blame] | 559 | slave->mtd.ecc_stats.bbtblocks++; |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 560 | else if (mtd_block_isbad(parent, offs + slave->offset)) |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 561 | slave->mtd.ecc_stats.badblocks++; |
| 562 | offs += slave->mtd.erasesize; |
| 563 | } |
| 564 | } |
| 565 | |
Atsushi Nemoto | f636ffb | 2008-07-19 01:01:22 +0900 | [diff] [blame] | 566 | out_register: |
Atsushi Nemoto | 7788ba7 | 2008-07-19 01:00:18 +0900 | [diff] [blame] | 567 | return slave; |
| 568 | } |
| 569 | |
Dan Ehrenberg | a62c24d | 2015-04-02 15:15:11 -0700 | [diff] [blame] | 570 | static ssize_t mtd_partition_offset_show(struct device *dev, |
| 571 | struct device_attribute *attr, char *buf) |
| 572 | { |
| 573 | struct mtd_info *mtd = dev_get_drvdata(dev); |
Brian Norris | 2524534 | 2015-11-19 19:28:39 -0800 | [diff] [blame] | 574 | struct mtd_part *part = mtd_to_part(mtd); |
Dan Ehrenberg | a62c24d | 2015-04-02 15:15:11 -0700 | [diff] [blame] | 575 | return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset); |
| 576 | } |
| 577 | |
| 578 | static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL); |
| 579 | |
| 580 | static const struct attribute *mtd_partition_attrs[] = { |
| 581 | &dev_attr_offset.attr, |
| 582 | NULL |
| 583 | }; |
| 584 | |
| 585 | static int mtd_add_partition_attrs(struct mtd_part *new) |
| 586 | { |
| 587 | int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs); |
| 588 | if (ret) |
| 589 | printk(KERN_WARNING |
| 590 | "mtd: failed to create partition attrs, err=%d\n", ret); |
| 591 | return ret; |
| 592 | } |
| 593 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 594 | int mtd_add_partition(struct mtd_info *parent, const char *name, |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 595 | long long offset, long long length) |
| 596 | { |
| 597 | struct mtd_partition part; |
Dan Ehrenberg | 3a434f6 | 2015-04-02 15:15:12 -0700 | [diff] [blame] | 598 | struct mtd_part *new; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 599 | int ret = 0; |
| 600 | |
| 601 | /* the direct offset is expected */ |
| 602 | if (offset == MTDPART_OFS_APPEND || |
| 603 | offset == MTDPART_OFS_NXTBLK) |
| 604 | return -EINVAL; |
| 605 | |
| 606 | if (length == MTDPART_SIZ_FULL) |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 607 | length = parent->size - offset; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 608 | |
| 609 | if (length <= 0) |
| 610 | return -EINVAL; |
| 611 | |
Brian Norris | 9386723 | 2015-11-11 16:47:52 -0800 | [diff] [blame] | 612 | memset(&part, 0, sizeof(part)); |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 613 | part.name = name; |
| 614 | part.size = length; |
| 615 | part.offset = offset; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 616 | |
Rafał Miłecki | 0a9d72b | 2017-06-21 08:26:44 +0200 | [diff] [blame] | 617 | new = allocate_partition(parent, &part, -1, offset); |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 618 | if (IS_ERR(new)) |
| 619 | return PTR_ERR(new); |
| 620 | |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 621 | mutex_lock(&mtd_partitions_mutex); |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 622 | list_add(&new->list, &mtd_partitions); |
| 623 | mutex_unlock(&mtd_partitions_mutex); |
| 624 | |
Boris Brezillon | 2b6f009 | 2019-01-02 15:36:54 +0100 | [diff] [blame] | 625 | ret = add_mtd_device(&new->mtd); |
| 626 | if (ret) |
| 627 | goto err_remove_part; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 628 | |
Dan Ehrenberg | a62c24d | 2015-04-02 15:15:11 -0700 | [diff] [blame] | 629 | mtd_add_partition_attrs(new); |
| 630 | |
Boris Brezillon | 2b6f009 | 2019-01-02 15:36:54 +0100 | [diff] [blame] | 631 | return 0; |
| 632 | |
| 633 | err_remove_part: |
| 634 | mutex_lock(&mtd_partitions_mutex); |
| 635 | list_del(&new->list); |
| 636 | mutex_unlock(&mtd_partitions_mutex); |
| 637 | |
| 638 | free_partition(new); |
Boris Brezillon | 2b6f009 | 2019-01-02 15:36:54 +0100 | [diff] [blame] | 639 | |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 640 | return ret; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 641 | } |
| 642 | EXPORT_SYMBOL_GPL(mtd_add_partition); |
| 643 | |
Rafał Miłecki | 08263a9 | 2017-06-21 08:26:42 +0200 | [diff] [blame] | 644 | /** |
| 645 | * __mtd_del_partition - delete MTD partition |
| 646 | * |
| 647 | * @priv: internal MTD struct for partition to be deleted |
| 648 | * |
| 649 | * This function must be called with the partitions mutex locked. |
| 650 | */ |
| 651 | static int __mtd_del_partition(struct mtd_part *priv) |
| 652 | { |
Rafał Miłecki | 97519dc | 2017-06-21 08:26:45 +0200 | [diff] [blame] | 653 | struct mtd_part *child, *next; |
Rafał Miłecki | 08263a9 | 2017-06-21 08:26:42 +0200 | [diff] [blame] | 654 | int err; |
| 655 | |
Rafał Miłecki | 97519dc | 2017-06-21 08:26:45 +0200 | [diff] [blame] | 656 | list_for_each_entry_safe(child, next, &mtd_partitions, list) { |
| 657 | if (child->parent == &priv->mtd) { |
| 658 | err = __mtd_del_partition(child); |
| 659 | if (err) |
| 660 | return err; |
| 661 | } |
| 662 | } |
| 663 | |
Rafał Miłecki | c5ceaba | 2017-06-21 08:26:43 +0200 | [diff] [blame] | 664 | sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs); |
| 665 | |
Rafał Miłecki | 08263a9 | 2017-06-21 08:26:42 +0200 | [diff] [blame] | 666 | err = del_mtd_device(&priv->mtd); |
| 667 | if (err) |
| 668 | return err; |
| 669 | |
| 670 | list_del(&priv->list); |
| 671 | free_partition(priv); |
| 672 | |
| 673 | return 0; |
| 674 | } |
| 675 | |
| 676 | /* |
| 677 | * This function unregisters and destroy all slave MTD objects which are |
Rafał Miłecki | 97519dc | 2017-06-21 08:26:45 +0200 | [diff] [blame] | 678 | * attached to the given MTD object. |
Rafał Miłecki | 08263a9 | 2017-06-21 08:26:42 +0200 | [diff] [blame] | 679 | */ |
Rafał Miłecki | 97519dc | 2017-06-21 08:26:45 +0200 | [diff] [blame] | 680 | int del_mtd_partitions(struct mtd_info *mtd) |
Rafał Miłecki | 08263a9 | 2017-06-21 08:26:42 +0200 | [diff] [blame] | 681 | { |
| 682 | struct mtd_part *slave, *next; |
| 683 | int ret, err = 0; |
| 684 | |
| 685 | mutex_lock(&mtd_partitions_mutex); |
| 686 | list_for_each_entry_safe(slave, next, &mtd_partitions, list) |
Rafał Miłecki | 97519dc | 2017-06-21 08:26:45 +0200 | [diff] [blame] | 687 | if (slave->parent == mtd) { |
Rafał Miłecki | 08263a9 | 2017-06-21 08:26:42 +0200 | [diff] [blame] | 688 | ret = __mtd_del_partition(slave); |
| 689 | if (ret < 0) |
| 690 | err = ret; |
| 691 | } |
| 692 | mutex_unlock(&mtd_partitions_mutex); |
| 693 | |
| 694 | return err; |
| 695 | } |
| 696 | |
Rafał Miłecki | 97519dc | 2017-06-21 08:26:45 +0200 | [diff] [blame] | 697 | int mtd_del_partition(struct mtd_info *mtd, int partno) |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 698 | { |
| 699 | struct mtd_part *slave, *next; |
| 700 | int ret = -EINVAL; |
| 701 | |
| 702 | mutex_lock(&mtd_partitions_mutex); |
| 703 | list_for_each_entry_safe(slave, next, &mtd_partitions, list) |
Rafał Miłecki | 97519dc | 2017-06-21 08:26:45 +0200 | [diff] [blame] | 704 | if ((slave->parent == mtd) && |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 705 | (slave->mtd.index == partno)) { |
Rafał Miłecki | 08263a9 | 2017-06-21 08:26:42 +0200 | [diff] [blame] | 706 | ret = __mtd_del_partition(slave); |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 707 | break; |
| 708 | } |
| 709 | mutex_unlock(&mtd_partitions_mutex); |
| 710 | |
| 711 | return ret; |
| 712 | } |
| 713 | EXPORT_SYMBOL_GPL(mtd_del_partition); |
| 714 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 715 | /* |
| 716 | * This function, given a master MTD object and a partition table, creates |
| 717 | * and registers slave MTD objects which are bound to the master according to |
| 718 | * the partition definitions. |
David Brownell | 1f24b5a | 2009-03-26 00:42:41 -0700 | [diff] [blame] | 719 | * |
Dan Ehrenberg | 727dc61 | 2015-04-02 15:15:10 -0700 | [diff] [blame] | 720 | * For historical reasons, this function's caller only registers the master |
| 721 | * if the MTD_PARTITIONED_MASTER config option is set. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 722 | */ |
| 723 | |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 724 | int add_mtd_partitions(struct mtd_info *master, |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 725 | const struct mtd_partition *parts, |
| 726 | int nbparts) |
| 727 | { |
| 728 | struct mtd_part *slave; |
Adrian Hunter | 69423d9 | 2008-12-10 13:37:21 +0000 | [diff] [blame] | 729 | uint64_t cur_offset = 0; |
Boris Brezillon | 2b6f009 | 2019-01-02 15:36:54 +0100 | [diff] [blame] | 730 | int i, ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 731 | |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 732 | printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 733 | |
| 734 | for (i = 0; i < nbparts; i++) { |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 735 | slave = allocate_partition(master, parts + i, i, cur_offset); |
Boris BREZILLON | e5bae86 | 2015-07-30 12:18:03 +0200 | [diff] [blame] | 736 | if (IS_ERR(slave)) { |
Boris Brezillon | 2b6f009 | 2019-01-02 15:36:54 +0100 | [diff] [blame] | 737 | ret = PTR_ERR(slave); |
| 738 | goto err_del_partitions; |
Boris BREZILLON | e5bae86 | 2015-07-30 12:18:03 +0200 | [diff] [blame] | 739 | } |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 740 | |
| 741 | mutex_lock(&mtd_partitions_mutex); |
| 742 | list_add(&slave->list, &mtd_partitions); |
| 743 | mutex_unlock(&mtd_partitions_mutex); |
| 744 | |
Boris Brezillon | 2b6f009 | 2019-01-02 15:36:54 +0100 | [diff] [blame] | 745 | ret = add_mtd_device(&slave->mtd); |
| 746 | if (ret) { |
| 747 | mutex_lock(&mtd_partitions_mutex); |
| 748 | list_del(&slave->list); |
| 749 | mutex_unlock(&mtd_partitions_mutex); |
| 750 | |
| 751 | free_partition(slave); |
| 752 | goto err_del_partitions; |
| 753 | } |
| 754 | |
Dan Ehrenberg | a62c24d | 2015-04-02 15:15:11 -0700 | [diff] [blame] | 755 | mtd_add_partition_attrs(slave); |
Rafał Miłecki | 76a8322 | 2018-07-13 16:32:21 +0200 | [diff] [blame] | 756 | /* Look for subpartitions */ |
| 757 | parse_mtd_partitions(&slave->mtd, parts[i].types, NULL); |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 758 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 759 | cur_offset = slave->offset + slave->mtd.size; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | return 0; |
Boris Brezillon | 2b6f009 | 2019-01-02 15:36:54 +0100 | [diff] [blame] | 763 | |
| 764 | err_del_partitions: |
| 765 | del_mtd_partitions(master); |
| 766 | |
| 767 | return ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 768 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 769 | |
| 770 | static DEFINE_SPINLOCK(part_parser_lock); |
| 771 | static LIST_HEAD(part_parsers); |
| 772 | |
Brian Norris | 5531ae4 | 2015-12-04 15:25:15 -0800 | [diff] [blame] | 773 | static struct mtd_part_parser *mtd_part_parser_get(const char *name) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 774 | { |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 775 | struct mtd_part_parser *p, *ret = NULL; |
| 776 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 777 | spin_lock(&part_parser_lock); |
| 778 | |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 779 | list_for_each_entry(p, &part_parsers, list) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 780 | if (!strcmp(p->name, name) && try_module_get(p->owner)) { |
| 781 | ret = p; |
| 782 | break; |
| 783 | } |
Chris Malley | 71a928c | 2008-05-19 20:11:50 +0100 | [diff] [blame] | 784 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 785 | spin_unlock(&part_parser_lock); |
| 786 | |
| 787 | return ret; |
| 788 | } |
| 789 | |
Brian Norris | 5531ae4 | 2015-12-04 15:25:15 -0800 | [diff] [blame] | 790 | static inline void mtd_part_parser_put(const struct mtd_part_parser *p) |
| 791 | { |
| 792 | module_put(p->owner); |
| 793 | } |
Dmitry Eremin-Solenikov | 953b3bd | 2011-06-23 15:26:14 +0400 | [diff] [blame] | 794 | |
Brian Norris | adc83bf | 2015-12-09 10:24:03 -0800 | [diff] [blame] | 795 | /* |
| 796 | * Many partition parsers just expected the core to kfree() all their data in |
| 797 | * one chunk. Do that by default. |
| 798 | */ |
| 799 | static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts, |
| 800 | int nr_parts) |
| 801 | { |
| 802 | kfree(pparts); |
| 803 | } |
| 804 | |
Brian Norris | b9eab01 | 2015-11-11 19:13:29 -0800 | [diff] [blame] | 805 | int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 806 | { |
Brian Norris | b9eab01 | 2015-11-11 19:13:29 -0800 | [diff] [blame] | 807 | p->owner = owner; |
| 808 | |
Brian Norris | adc83bf | 2015-12-09 10:24:03 -0800 | [diff] [blame] | 809 | if (!p->cleanup) |
| 810 | p->cleanup = &mtd_part_parser_cleanup_default; |
| 811 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 812 | spin_lock(&part_parser_lock); |
| 813 | list_add(&p->list, &part_parsers); |
| 814 | spin_unlock(&part_parser_lock); |
Brian Norris | b9eab01 | 2015-11-11 19:13:29 -0800 | [diff] [blame] | 815 | |
| 816 | return 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 817 | } |
Brian Norris | b9eab01 | 2015-11-11 19:13:29 -0800 | [diff] [blame] | 818 | EXPORT_SYMBOL_GPL(__register_mtd_parser); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 819 | |
Axel Lin | cf3b2b1 | 2013-12-01 18:59:15 +0800 | [diff] [blame] | 820 | void deregister_mtd_parser(struct mtd_part_parser *p) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 821 | { |
| 822 | spin_lock(&part_parser_lock); |
| 823 | list_del(&p->list); |
| 824 | spin_unlock(&part_parser_lock); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 825 | } |
Atsushi Nemoto | b33a288 | 2008-07-19 01:00:33 +0900 | [diff] [blame] | 826 | EXPORT_SYMBOL_GPL(deregister_mtd_parser); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 827 | |
Artem Bityutskiy | ad274ce | 2011-06-08 11:42:27 +0300 | [diff] [blame] | 828 | /* |
| 829 | * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you |
| 830 | * are changing this array! |
| 831 | */ |
Artem Bityutskiy | ccef4dc | 2013-03-12 10:51:35 +0200 | [diff] [blame] | 832 | static const char * const default_mtd_part_types[] = { |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 833 | "cmdlinepart", |
| 834 | "ofpart", |
| 835 | NULL |
| 836 | }; |
Dmitry Eremin-Solenikov | 5c4eefb | 2011-06-02 18:51:16 +0400 | [diff] [blame] | 837 | |
Rafał Miłecki | 76a8322 | 2018-07-13 16:32:21 +0200 | [diff] [blame] | 838 | /* Check DT only when looking for subpartitions. */ |
| 839 | static const char * const default_subpartition_types[] = { |
| 840 | "ofpart", |
| 841 | NULL |
| 842 | }; |
| 843 | |
Brian Norris | 01f9c72 | 2017-05-23 07:30:20 +0200 | [diff] [blame] | 844 | static int mtd_part_do_parse(struct mtd_part_parser *parser, |
| 845 | struct mtd_info *master, |
| 846 | struct mtd_partitions *pparts, |
| 847 | struct mtd_part_parser_data *data) |
| 848 | { |
| 849 | int ret; |
| 850 | |
| 851 | ret = (*parser->parse_fn)(master, &pparts->parts, data); |
| 852 | pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret); |
| 853 | if (ret <= 0) |
| 854 | return ret; |
| 855 | |
| 856 | pr_notice("%d %s partitions found on MTD device %s\n", ret, |
| 857 | parser->name, master->name); |
| 858 | |
| 859 | pparts->nr_parts = ret; |
| 860 | pparts->parser = parser; |
| 861 | |
| 862 | return ret; |
| 863 | } |
| 864 | |
Artem Bityutskiy | ad274ce | 2011-06-08 11:42:27 +0300 | [diff] [blame] | 865 | /** |
Rafał Miłecki | 5b644aa | 2018-03-14 13:10:42 +0100 | [diff] [blame] | 866 | * mtd_part_get_compatible_parser - find MTD parser by a compatible string |
| 867 | * |
| 868 | * @compat: compatible string describing partitions in a device tree |
| 869 | * |
| 870 | * MTD parsers can specify supported partitions by providing a table of |
| 871 | * compatibility strings. This function finds a parser that advertises support |
| 872 | * for a passed value of "compatible". |
| 873 | */ |
| 874 | static struct mtd_part_parser *mtd_part_get_compatible_parser(const char *compat) |
| 875 | { |
| 876 | struct mtd_part_parser *p, *ret = NULL; |
| 877 | |
| 878 | spin_lock(&part_parser_lock); |
| 879 | |
| 880 | list_for_each_entry(p, &part_parsers, list) { |
| 881 | const struct of_device_id *matches; |
| 882 | |
| 883 | matches = p->of_match_table; |
| 884 | if (!matches) |
| 885 | continue; |
| 886 | |
| 887 | for (; matches->compatible[0]; matches++) { |
| 888 | if (!strcmp(matches->compatible, compat) && |
| 889 | try_module_get(p->owner)) { |
| 890 | ret = p; |
| 891 | break; |
| 892 | } |
| 893 | } |
| 894 | |
| 895 | if (ret) |
| 896 | break; |
| 897 | } |
| 898 | |
| 899 | spin_unlock(&part_parser_lock); |
| 900 | |
| 901 | return ret; |
| 902 | } |
| 903 | |
| 904 | static int mtd_part_of_parse(struct mtd_info *master, |
| 905 | struct mtd_partitions *pparts) |
| 906 | { |
| 907 | struct mtd_part_parser *parser; |
| 908 | struct device_node *np; |
| 909 | struct property *prop; |
| 910 | const char *compat; |
Rafał Miłecki | c0faf43 | 2018-03-14 13:10:43 +0100 | [diff] [blame] | 911 | const char *fixed = "fixed-partitions"; |
Rafał Miłecki | 5b644aa | 2018-03-14 13:10:42 +0100 | [diff] [blame] | 912 | int ret, err = 0; |
| 913 | |
Rafał Miłecki | 76a8322 | 2018-07-13 16:32:21 +0200 | [diff] [blame] | 914 | np = mtd_get_of_node(master); |
Miquel Raynal | 85516a98 | 2018-09-07 16:35:54 +0200 | [diff] [blame] | 915 | if (mtd_is_partition(master)) |
| 916 | of_node_get(np); |
| 917 | else |
Rafał Miłecki | 76a8322 | 2018-07-13 16:32:21 +0200 | [diff] [blame] | 918 | np = of_get_child_by_name(np, "partitions"); |
Miquel Raynal | 85516a98 | 2018-09-07 16:35:54 +0200 | [diff] [blame] | 919 | |
Rafał Miłecki | 5b644aa | 2018-03-14 13:10:42 +0100 | [diff] [blame] | 920 | of_property_for_each_string(np, "compatible", prop, compat) { |
| 921 | parser = mtd_part_get_compatible_parser(compat); |
| 922 | if (!parser) |
| 923 | continue; |
| 924 | ret = mtd_part_do_parse(parser, master, pparts, NULL); |
| 925 | if (ret > 0) { |
| 926 | of_node_put(np); |
| 927 | return ret; |
| 928 | } |
| 929 | mtd_part_parser_put(parser); |
| 930 | if (ret < 0 && !err) |
| 931 | err = ret; |
| 932 | } |
| 933 | of_node_put(np); |
| 934 | |
| 935 | /* |
Rafał Miłecki | c0faf43 | 2018-03-14 13:10:43 +0100 | [diff] [blame] | 936 | * For backward compatibility we have to try the "fixed-partitions" |
Rafał Miłecki | 5b644aa | 2018-03-14 13:10:42 +0100 | [diff] [blame] | 937 | * parser. It supports old DT format with partitions specified as a |
| 938 | * direct subnodes of a flash device DT node without any compatibility |
| 939 | * specified we could match. |
| 940 | */ |
| 941 | parser = mtd_part_parser_get(fixed); |
| 942 | if (!parser && !request_module("%s", fixed)) |
| 943 | parser = mtd_part_parser_get(fixed); |
| 944 | if (parser) { |
| 945 | ret = mtd_part_do_parse(parser, master, pparts, NULL); |
| 946 | if (ret > 0) |
| 947 | return ret; |
| 948 | mtd_part_parser_put(parser); |
| 949 | if (ret < 0 && !err) |
| 950 | err = ret; |
| 951 | } |
| 952 | |
| 953 | return err; |
| 954 | } |
| 955 | |
| 956 | /** |
Rafał Miłecki | 5ac67ce | 2018-03-27 22:35:41 +0200 | [diff] [blame] | 957 | * parse_mtd_partitions - parse and register MTD partitions |
| 958 | * |
Artem Bityutskiy | ad274ce | 2011-06-08 11:42:27 +0300 | [diff] [blame] | 959 | * @master: the master partition (describes whole MTD device) |
| 960 | * @types: names of partition parsers to try or %NULL |
Dmitry Eremin-Solenikov | c797533 | 2011-06-10 18:18:28 +0400 | [diff] [blame] | 961 | * @data: MTD partition parser-specific data |
Artem Bityutskiy | ad274ce | 2011-06-08 11:42:27 +0300 | [diff] [blame] | 962 | * |
Rafał Miłecki | 5ac67ce | 2018-03-27 22:35:41 +0200 | [diff] [blame] | 963 | * This function tries to find & register partitions on MTD device @master. It |
| 964 | * uses MTD partition parsers, specified in @types. However, if @types is %NULL, |
| 965 | * then the default list of parsers is used. The default list contains only the |
Dmitry Eremin-Solenikov | d26c87d | 2011-05-29 21:32:33 +0400 | [diff] [blame] | 966 | * "cmdlinepart" and "ofpart" parsers ATM. |
Huang Shijie | c51803d | 2012-08-18 13:07:41 -0400 | [diff] [blame] | 967 | * Note: If there are more then one parser in @types, the kernel only takes the |
| 968 | * partitions parsed out by the first parser. |
Artem Bityutskiy | ad274ce | 2011-06-08 11:42:27 +0300 | [diff] [blame] | 969 | * |
| 970 | * This function may return: |
| 971 | * o a negative error code in case of failure |
Rafał Miłecki | 5ac67ce | 2018-03-27 22:35:41 +0200 | [diff] [blame] | 972 | * o number of found partitions otherwise |
Artem Bityutskiy | ad274ce | 2011-06-08 11:42:27 +0300 | [diff] [blame] | 973 | */ |
Artem Bityutskiy | 26a4734 | 2013-03-11 15:38:48 +0200 | [diff] [blame] | 974 | int parse_mtd_partitions(struct mtd_info *master, const char *const *types, |
Dmitry Eremin-Solenikov | c797533 | 2011-06-10 18:18:28 +0400 | [diff] [blame] | 975 | struct mtd_part_parser_data *data) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 976 | { |
Rafał Miłecki | 5ac67ce | 2018-03-27 22:35:41 +0200 | [diff] [blame] | 977 | struct mtd_partitions pparts = { }; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 978 | struct mtd_part_parser *parser; |
Brian Norris | 5a2415b | 2015-10-11 13:03:47 -0700 | [diff] [blame] | 979 | int ret, err = 0; |
Thomas Gleixner | 97894cd | 2005-11-07 11:15:26 +0000 | [diff] [blame] | 980 | |
Dmitry Eremin-Solenikov | 5c4eefb | 2011-06-02 18:51:16 +0400 | [diff] [blame] | 981 | if (!types) |
Rafał Miłecki | 76a8322 | 2018-07-13 16:32:21 +0200 | [diff] [blame] | 982 | types = mtd_is_partition(master) ? default_subpartition_types : |
| 983 | default_mtd_part_types; |
Dmitry Eremin-Solenikov | 5c4eefb | 2011-06-02 18:51:16 +0400 | [diff] [blame] | 984 | |
Brian Norris | 5a2415b | 2015-10-11 13:03:47 -0700 | [diff] [blame] | 985 | for ( ; *types; types++) { |
Rafał Miłecki | 5b644aa | 2018-03-14 13:10:42 +0100 | [diff] [blame] | 986 | /* |
| 987 | * ofpart is a special type that means OF partitioning info |
| 988 | * should be used. It requires a bit different logic so it is |
| 989 | * handled in a separated function. |
| 990 | */ |
| 991 | if (!strcmp(*types, "ofpart")) { |
Rafał Miłecki | 5ac67ce | 2018-03-27 22:35:41 +0200 | [diff] [blame] | 992 | ret = mtd_part_of_parse(master, &pparts); |
Rafał Miłecki | 5b644aa | 2018-03-14 13:10:42 +0100 | [diff] [blame] | 993 | } else { |
| 994 | pr_debug("%s: parsing partitions %s\n", master->name, |
| 995 | *types); |
Brian Norris | 5531ae4 | 2015-12-04 15:25:15 -0800 | [diff] [blame] | 996 | parser = mtd_part_parser_get(*types); |
Rafał Miłecki | 5b644aa | 2018-03-14 13:10:42 +0100 | [diff] [blame] | 997 | if (!parser && !request_module("%s", *types)) |
| 998 | parser = mtd_part_parser_get(*types); |
| 999 | pr_debug("%s: got parser %s\n", master->name, |
| 1000 | parser ? parser->name : NULL); |
| 1001 | if (!parser) |
| 1002 | continue; |
Rafał Miłecki | 5ac67ce | 2018-03-27 22:35:41 +0200 | [diff] [blame] | 1003 | ret = mtd_part_do_parse(parser, master, &pparts, data); |
Rafał Miłecki | 5b644aa | 2018-03-14 13:10:42 +0100 | [diff] [blame] | 1004 | if (ret <= 0) |
| 1005 | mtd_part_parser_put(parser); |
| 1006 | } |
Brian Norris | 01f9c72 | 2017-05-23 07:30:20 +0200 | [diff] [blame] | 1007 | /* Found partitions! */ |
Rafał Miłecki | 5ac67ce | 2018-03-27 22:35:41 +0200 | [diff] [blame] | 1008 | if (ret > 0) { |
| 1009 | err = add_mtd_partitions(master, pparts.parts, |
| 1010 | pparts.nr_parts); |
| 1011 | mtd_part_parser_cleanup(&pparts); |
| 1012 | return err ? err : pparts.nr_parts; |
| 1013 | } |
Brian Norris | 5a2415b | 2015-10-11 13:03:47 -0700 | [diff] [blame] | 1014 | /* |
| 1015 | * Stash the first error we see; only report it if no parser |
| 1016 | * succeeds |
| 1017 | */ |
| 1018 | if (ret < 0 && !err) |
| 1019 | err = ret; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1020 | } |
Brian Norris | 5a2415b | 2015-10-11 13:03:47 -0700 | [diff] [blame] | 1021 | return err; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1022 | } |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 1023 | |
Brian Norris | adc83bf | 2015-12-09 10:24:03 -0800 | [diff] [blame] | 1024 | void mtd_part_parser_cleanup(struct mtd_partitions *parts) |
| 1025 | { |
| 1026 | const struct mtd_part_parser *parser; |
| 1027 | |
| 1028 | if (!parts) |
| 1029 | return; |
| 1030 | |
| 1031 | parser = parts->parser; |
| 1032 | if (parser) { |
| 1033 | if (parser->cleanup) |
| 1034 | parser->cleanup(parts->parts, parts->nr_parts); |
| 1035 | |
| 1036 | mtd_part_parser_put(parser); |
| 1037 | } |
| 1038 | } |
| 1039 | |
Richard Genoud | 5dee467 | 2012-07-10 18:23:39 +0200 | [diff] [blame] | 1040 | int mtd_is_partition(const struct mtd_info *mtd) |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 1041 | { |
| 1042 | struct mtd_part *part; |
Roman Tereshonkov | a7e93dc | 2010-11-23 14:17:17 +0200 | [diff] [blame] | 1043 | int ispart = 0; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 1044 | |
| 1045 | mutex_lock(&mtd_partitions_mutex); |
| 1046 | list_for_each_entry(part, &mtd_partitions, list) |
| 1047 | if (&part->mtd == mtd) { |
Roman Tereshonkov | a7e93dc | 2010-11-23 14:17:17 +0200 | [diff] [blame] | 1048 | ispart = 1; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 1049 | break; |
| 1050 | } |
| 1051 | mutex_unlock(&mtd_partitions_mutex); |
| 1052 | |
Roman Tereshonkov | a7e93dc | 2010-11-23 14:17:17 +0200 | [diff] [blame] | 1053 | return ispart; |
Roman Tereshonkov | 5daa7b2 | 2010-09-17 13:31:41 +0300 | [diff] [blame] | 1054 | } |
Roman Tereshonkov | a7e93dc | 2010-11-23 14:17:17 +0200 | [diff] [blame] | 1055 | EXPORT_SYMBOL_GPL(mtd_is_partition); |
Richard Genoud | 62082e56 | 2012-07-10 18:23:40 +0200 | [diff] [blame] | 1056 | |
| 1057 | /* Returns the size of the entire flash chip */ |
| 1058 | uint64_t mtd_get_device_size(const struct mtd_info *mtd) |
| 1059 | { |
| 1060 | if (!mtd_is_partition(mtd)) |
| 1061 | return mtd->size; |
| 1062 | |
Rafał Miłecki | 97519dc | 2017-06-21 08:26:45 +0200 | [diff] [blame] | 1063 | return mtd_get_device_size(mtd_to_part(mtd)->parent); |
Richard Genoud | 62082e56 | 2012-07-10 18:23:40 +0200 | [diff] [blame] | 1064 | } |
| 1065 | EXPORT_SYMBOL_GPL(mtd_get_device_size); |