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