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