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