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