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