Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Simple MTD partitioning layer |
| 4 | * |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 5 | * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net> |
| 6 | * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de> |
| 7 | * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org> |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 8 | * |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 9 | */ |
| 10 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 11 | #ifndef __UBOOT__ |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 12 | #include <log.h> |
Simon Glass | 61b29b8 | 2020-02-03 07:36:15 -0700 | [diff] [blame] | 13 | #include <dm/devres.h> |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 14 | #include <linux/module.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/slab.h> |
| 18 | #include <linux/list.h> |
| 19 | #include <linux/kmod.h> |
| 20 | #endif |
| 21 | |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 22 | #include <common.h> |
| 23 | #include <malloc.h> |
Simon Glass | eb41d8a | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 24 | #include <linux/bug.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 25 | #include <linux/errno.h> |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 26 | #include <linux/compat.h> |
| 27 | #include <ubi_uboot.h> |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 28 | |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 29 | #include <linux/mtd/mtd.h> |
| 30 | #include <linux/mtd/partitions.h> |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 31 | #include <linux/err.h> |
Miquel Raynal | 21cc1fb | 2018-09-29 12:58:25 +0200 | [diff] [blame] | 32 | #include <linux/sizes.h> |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 33 | |
| 34 | #include "mtdcore.h" |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 35 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 36 | #ifndef __UBOOT__ |
| 37 | static DEFINE_MUTEX(mtd_partitions_mutex); |
| 38 | #else |
| 39 | DEFINE_MUTEX(mtd_partitions_mutex); |
| 40 | #endif |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 41 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 42 | #ifdef __UBOOT__ |
| 43 | /* from mm/util.c */ |
| 44 | |
| 45 | /** |
| 46 | * kstrdup - allocate space for and copy an existing string |
| 47 | * @s: the string to duplicate |
| 48 | * @gfp: the GFP mask used in the kmalloc() call when allocating memory |
| 49 | */ |
| 50 | char *kstrdup(const char *s, gfp_t gfp) |
| 51 | { |
| 52 | size_t len; |
| 53 | char *buf; |
| 54 | |
| 55 | if (!s) |
| 56 | return NULL; |
| 57 | |
| 58 | len = strlen(s) + 1; |
| 59 | buf = kmalloc(len, gfp); |
| 60 | if (buf) |
| 61 | memcpy(buf, s, len); |
| 62 | return buf; |
| 63 | } |
| 64 | #endif |
| 65 | |
Miquel Raynal | 21cc1fb | 2018-09-29 12:58:25 +0200 | [diff] [blame] | 66 | #define MTD_SIZE_REMAINING (~0LLU) |
| 67 | #define MTD_OFFSET_NOT_SPECIFIED (~0LLU) |
| 68 | |
Boris Brezillon | 4a5594f | 2018-12-02 10:54:30 +0100 | [diff] [blame] | 69 | bool mtd_partitions_used(struct mtd_info *master) |
| 70 | { |
| 71 | struct mtd_info *slave; |
| 72 | |
| 73 | list_for_each_entry(slave, &master->partitions, node) { |
| 74 | if (slave->usecount) |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | return false; |
| 79 | } |
| 80 | |
Miquel Raynal | 21cc1fb | 2018-09-29 12:58:25 +0200 | [diff] [blame] | 81 | /** |
| 82 | * mtd_parse_partition - Parse @mtdparts partition definition, fill @partition |
| 83 | * with it and update the @mtdparts string pointer. |
| 84 | * |
| 85 | * The partition name is allocated and must be freed by the caller. |
| 86 | * |
| 87 | * This function is widely inspired from part_parse (mtdparts.c). |
| 88 | * |
| 89 | * @mtdparts: String describing the partition with mtdparts command syntax |
| 90 | * @partition: MTD partition structure to fill |
| 91 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 92 | * Return: 0 on success, an error otherwise. |
Miquel Raynal | 21cc1fb | 2018-09-29 12:58:25 +0200 | [diff] [blame] | 93 | */ |
| 94 | static int mtd_parse_partition(const char **_mtdparts, |
| 95 | struct mtd_partition *partition) |
| 96 | { |
| 97 | const char *mtdparts = *_mtdparts; |
| 98 | const char *name = NULL; |
| 99 | int name_len; |
| 100 | char *buf; |
| 101 | |
| 102 | /* Ensure the partition structure is empty */ |
| 103 | memset(partition, 0, sizeof(struct mtd_partition)); |
| 104 | |
| 105 | /* Fetch the partition size */ |
| 106 | if (*mtdparts == '-') { |
| 107 | /* Assign all remaining space to this partition */ |
| 108 | partition->size = MTD_SIZE_REMAINING; |
| 109 | mtdparts++; |
| 110 | } else { |
| 111 | partition->size = ustrtoull(mtdparts, (char **)&mtdparts, 0); |
| 112 | if (partition->size < SZ_4K) { |
| 113 | printf("Minimum partition size 4kiB, %lldB requested\n", |
| 114 | partition->size); |
| 115 | return -EINVAL; |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | /* Check for the offset */ |
| 120 | partition->offset = MTD_OFFSET_NOT_SPECIFIED; |
| 121 | if (*mtdparts == '@') { |
| 122 | mtdparts++; |
| 123 | partition->offset = ustrtoull(mtdparts, (char **)&mtdparts, 0); |
| 124 | } |
| 125 | |
| 126 | /* Now look for the name */ |
| 127 | if (*mtdparts == '(') { |
| 128 | name = ++mtdparts; |
| 129 | mtdparts = strchr(name, ')'); |
| 130 | if (!mtdparts) { |
| 131 | printf("No closing ')' found in partition name\n"); |
| 132 | return -EINVAL; |
| 133 | } |
| 134 | name_len = mtdparts - name + 1; |
| 135 | if ((name_len - 1) == 0) { |
| 136 | printf("Empty partition name\n"); |
| 137 | return -EINVAL; |
| 138 | } |
| 139 | mtdparts++; |
| 140 | } else { |
| 141 | /* Name will be of the form size@offset */ |
| 142 | name_len = 22; |
| 143 | } |
| 144 | |
| 145 | /* Check if the partition is read-only */ |
| 146 | if (strncmp(mtdparts, "ro", 2) == 0) { |
| 147 | partition->mask_flags |= MTD_WRITEABLE; |
| 148 | mtdparts += 2; |
| 149 | } |
| 150 | |
| 151 | /* Check for a potential next partition definition */ |
| 152 | if (*mtdparts == ',') { |
| 153 | if (partition->size == MTD_SIZE_REMAINING) { |
| 154 | printf("No partitions allowed after a fill-up\n"); |
| 155 | return -EINVAL; |
| 156 | } |
| 157 | ++mtdparts; |
| 158 | } else if ((*mtdparts == ';') || (*mtdparts == '\0')) { |
| 159 | /* NOP */ |
| 160 | } else { |
| 161 | printf("Unexpected character '%c' in mtdparts\n", *mtdparts); |
| 162 | return -EINVAL; |
| 163 | } |
| 164 | |
| 165 | /* |
| 166 | * Allocate a buffer for the name and either copy the provided name or |
| 167 | * auto-generate it with the form 'size@offset'. |
| 168 | */ |
| 169 | buf = malloc(name_len); |
| 170 | if (!buf) |
| 171 | return -ENOMEM; |
| 172 | |
| 173 | if (name) |
| 174 | strncpy(buf, name, name_len - 1); |
| 175 | else |
| 176 | snprintf(buf, name_len, "0x%08llx@0x%08llx", |
| 177 | partition->size, partition->offset); |
| 178 | |
| 179 | buf[name_len - 1] = '\0'; |
| 180 | partition->name = buf; |
| 181 | |
| 182 | *_mtdparts = mtdparts; |
| 183 | |
| 184 | return 0; |
| 185 | } |
| 186 | |
| 187 | /** |
| 188 | * mtd_parse_partitions - Create a partition array from an mtdparts definition |
| 189 | * |
| 190 | * Stateless function that takes a @parent MTD device, a string @_mtdparts |
| 191 | * describing the partitions (with the "mtdparts" command syntax) and creates |
| 192 | * the corresponding MTD partition structure array @_parts. Both the name and |
| 193 | * the structure partition itself must be freed freed, the caller may use |
| 194 | * @mtd_free_parsed_partitions() for this purpose. |
| 195 | * |
| 196 | * @parent: MTD device which contains the partitions |
| 197 | * @_mtdparts: Pointer to a string describing the partitions with "mtdparts" |
| 198 | * command syntax. |
| 199 | * @_parts: Allocated array containing the partitions, must be freed by the |
| 200 | * caller. |
| 201 | * @_nparts: Size of @_parts array. |
| 202 | * |
Heinrich Schuchardt | 185f812 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 203 | * Return: 0 on success, an error otherwise. |
Miquel Raynal | 21cc1fb | 2018-09-29 12:58:25 +0200 | [diff] [blame] | 204 | */ |
| 205 | int mtd_parse_partitions(struct mtd_info *parent, const char **_mtdparts, |
| 206 | struct mtd_partition **_parts, int *_nparts) |
| 207 | { |
| 208 | struct mtd_partition partition = {}, *parts; |
| 209 | const char *mtdparts = *_mtdparts; |
Martin Kaistra | 892d461 | 2020-07-13 14:40:02 +0200 | [diff] [blame] | 210 | uint64_t cur_off = 0, cur_sz = 0; |
Miquel Raynal | 21cc1fb | 2018-09-29 12:58:25 +0200 | [diff] [blame] | 211 | int nparts = 0; |
| 212 | int ret, idx; |
| 213 | u64 sz; |
| 214 | |
| 215 | /* First, iterate over the partitions until we know their number */ |
| 216 | while (mtdparts[0] != '\0' && mtdparts[0] != ';') { |
| 217 | ret = mtd_parse_partition(&mtdparts, &partition); |
| 218 | if (ret) |
| 219 | return ret; |
| 220 | |
| 221 | free((char *)partition.name); |
| 222 | nparts++; |
| 223 | } |
| 224 | |
| 225 | /* Allocate an array of partitions to give back to the caller */ |
| 226 | parts = malloc(sizeof(*parts) * nparts); |
| 227 | if (!parts) { |
| 228 | printf("Not enough space to save partitions meta-data\n"); |
| 229 | return -ENOMEM; |
| 230 | } |
| 231 | |
| 232 | /* Iterate again over each partition to save the data in our array */ |
| 233 | for (idx = 0; idx < nparts; idx++) { |
| 234 | ret = mtd_parse_partition(_mtdparts, &parts[idx]); |
| 235 | if (ret) |
| 236 | return ret; |
| 237 | |
| 238 | if (parts[idx].size == MTD_SIZE_REMAINING) |
| 239 | parts[idx].size = parent->size - cur_sz; |
| 240 | cur_sz += parts[idx].size; |
| 241 | |
| 242 | sz = parts[idx].size; |
| 243 | if (sz < parent->writesize || do_div(sz, parent->writesize)) { |
| 244 | printf("Partition size must be a multiple of %d\n", |
| 245 | parent->writesize); |
| 246 | return -EINVAL; |
| 247 | } |
| 248 | |
| 249 | if (parts[idx].offset == MTD_OFFSET_NOT_SPECIFIED) |
| 250 | parts[idx].offset = cur_off; |
| 251 | cur_off += parts[idx].size; |
| 252 | |
| 253 | parts[idx].ecclayout = parent->ecclayout; |
| 254 | } |
| 255 | |
| 256 | /* Offset by one mtdparts to point to the next device if any */ |
| 257 | if (*_mtdparts[0] == ';') |
| 258 | (*_mtdparts)++; |
| 259 | |
| 260 | *_parts = parts; |
| 261 | *_nparts = nparts; |
| 262 | |
| 263 | return 0; |
| 264 | } |
| 265 | |
| 266 | /** |
| 267 | * mtd_free_parsed_partitions - Free dynamically allocated partitions |
| 268 | * |
| 269 | * Each successful call to @mtd_parse_partitions must be followed by a call to |
| 270 | * @mtd_free_parsed_partitions to free any allocated array during the parsing |
| 271 | * process. |
| 272 | * |
| 273 | * @parts: Array containing the partitions that will be freed. |
| 274 | * @nparts: Size of @parts array. |
| 275 | */ |
| 276 | void mtd_free_parsed_partitions(struct mtd_partition *parts, |
| 277 | unsigned int nparts) |
| 278 | { |
| 279 | int i; |
| 280 | |
| 281 | for (i = 0; i < nparts; i++) |
| 282 | free((char *)parts[i].name); |
| 283 | |
| 284 | free(parts); |
| 285 | } |
| 286 | |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 287 | /* |
| 288 | * MTD methods which simply translate the effective address and pass through |
| 289 | * to the _real_ device. |
| 290 | */ |
| 291 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 292 | static int part_read(struct mtd_info *mtd, loff_t from, size_t len, |
| 293 | size_t *retlen, u_char *buf) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 294 | { |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 295 | struct mtd_ecc_stats stats; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 296 | int res; |
| 297 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 298 | stats = mtd->parent->ecc_stats; |
| 299 | res = mtd->parent->_read(mtd->parent, from + mtd->offset, len, |
| 300 | retlen, buf); |
Paul Burton | 40462e5 | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 301 | if (unlikely(mtd_is_eccerr(res))) |
| 302 | mtd->ecc_stats.failed += |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 303 | mtd->parent->ecc_stats.failed - stats.failed; |
Paul Burton | 40462e5 | 2013-09-04 15:16:56 +0100 | [diff] [blame] | 304 | else |
| 305 | mtd->ecc_stats.corrected += |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 306 | mtd->parent->ecc_stats.corrected - stats.corrected; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 307 | return res; |
| 308 | } |
| 309 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 310 | #ifndef __UBOOT__ |
| 311 | static int part_point(struct mtd_info *mtd, loff_t from, size_t len, |
| 312 | size_t *retlen, void **virt, resource_size_t *phys) |
| 313 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 314 | return mtd->parent->_point(mtd->parent, from + mtd->offset, len, |
| 315 | retlen, virt, phys); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 316 | } |
| 317 | |
| 318 | static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len) |
| 319 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 320 | return mtd->parent->_unpoint(mtd->parent, from + mtd->offset, len); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 321 | } |
| 322 | #endif |
| 323 | |
| 324 | static unsigned long part_get_unmapped_area(struct mtd_info *mtd, |
| 325 | unsigned long len, |
| 326 | unsigned long offset, |
| 327 | unsigned long flags) |
| 328 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 329 | offset += mtd->offset; |
| 330 | return mtd->parent->_get_unmapped_area(mtd->parent, len, offset, flags); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 331 | } |
| 332 | |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 333 | static int part_read_oob(struct mtd_info *mtd, loff_t from, |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 334 | struct mtd_oob_ops *ops) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 335 | { |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 336 | int res; |
| 337 | |
| 338 | if (from >= mtd->size) |
| 339 | return -EINVAL; |
| 340 | if (ops->datbuf && from + ops->len > mtd->size) |
| 341 | return -EINVAL; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 342 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 343 | /* |
| 344 | * If OOB is also requested, make sure that we do not read past the end |
| 345 | * of this partition. |
| 346 | */ |
| 347 | if (ops->oobbuf) { |
| 348 | size_t len, pages; |
| 349 | |
| 350 | if (ops->mode == MTD_OPS_AUTO_OOB) |
| 351 | len = mtd->oobavail; |
| 352 | else |
| 353 | len = mtd->oobsize; |
| 354 | pages = mtd_div_by_ws(mtd->size, mtd); |
| 355 | pages -= mtd_div_by_ws(from, mtd); |
| 356 | if (ops->ooboffs + ops->ooblen > pages * len) |
| 357 | return -EINVAL; |
| 358 | } |
| 359 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 360 | res = mtd->parent->_read_oob(mtd->parent, from + mtd->offset, ops); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 361 | if (unlikely(res)) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 362 | if (mtd_is_bitflip(res)) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 363 | mtd->ecc_stats.corrected++; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 364 | if (mtd_is_eccerr(res)) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 365 | mtd->ecc_stats.failed++; |
| 366 | } |
| 367 | return res; |
| 368 | } |
| 369 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 370 | static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 371 | size_t len, size_t *retlen, u_char *buf) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 372 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 373 | return mtd->parent->_read_user_prot_reg(mtd->parent, from, len, |
| 374 | retlen, buf); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 375 | } |
| 376 | |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 377 | static int part_get_user_prot_info(struct mtd_info *mtd, size_t len, |
| 378 | size_t *retlen, struct otp_info *buf) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 379 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 380 | return mtd->parent->_get_user_prot_info(mtd->parent, len, retlen, |
| 381 | buf); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 382 | } |
| 383 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 384 | static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from, |
| 385 | size_t len, size_t *retlen, u_char *buf) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 386 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 387 | return mtd->parent->_read_fact_prot_reg(mtd->parent, from, len, |
| 388 | retlen, buf); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 389 | } |
| 390 | |
Heiko Schocher | 4e67c57 | 2014-07-15 16:08:43 +0200 | [diff] [blame] | 391 | static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len, |
| 392 | size_t *retlen, struct otp_info *buf) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 393 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 394 | return mtd->parent->_get_fact_prot_info(mtd->parent, len, retlen, |
| 395 | buf); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 396 | } |
| 397 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 398 | static int part_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 399 | size_t *retlen, const u_char *buf) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 400 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 401 | return mtd->parent->_write(mtd->parent, to + mtd->offset, len, |
| 402 | retlen, buf); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len, |
| 406 | size_t *retlen, const u_char *buf) |
| 407 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 408 | return mtd->parent->_panic_write(mtd->parent, to + mtd->offset, len, |
| 409 | retlen, buf); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 410 | } |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 411 | |
| 412 | static int part_write_oob(struct mtd_info *mtd, loff_t to, |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 413 | struct mtd_oob_ops *ops) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 414 | { |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 415 | if (to >= mtd->size) |
| 416 | return -EINVAL; |
| 417 | if (ops->datbuf && to + ops->len > mtd->size) |
| 418 | return -EINVAL; |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 419 | return mtd->parent->_write_oob(mtd->parent, to + mtd->offset, ops); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 420 | } |
| 421 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 422 | static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 423 | size_t len, size_t *retlen, u_char *buf) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 424 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 425 | return mtd->parent->_write_user_prot_reg(mtd->parent, from, len, |
| 426 | retlen, buf); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 427 | } |
| 428 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 429 | static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from, |
| 430 | size_t len) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 431 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 432 | return mtd->parent->_lock_user_prot_reg(mtd->parent, from, len); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 433 | } |
| 434 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 435 | #ifndef __UBOOT__ |
| 436 | static int part_writev(struct mtd_info *mtd, const struct kvec *vecs, |
| 437 | unsigned long count, loff_t to, size_t *retlen) |
| 438 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 439 | return mtd->parent->_writev(mtd->parent, vecs, count, |
| 440 | to + mtd->offset, retlen); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 441 | } |
| 442 | #endif |
| 443 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 444 | static int part_erase(struct mtd_info *mtd, struct erase_info *instr) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 445 | { |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 446 | int ret; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 447 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 448 | instr->addr += mtd->offset; |
Marek Behún | a60397d | 2021-10-05 15:56:05 +0200 | [diff] [blame] | 449 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 450 | ret = mtd->parent->_erase(mtd->parent, instr); |
Marek Behún | a60397d | 2021-10-05 15:56:05 +0200 | [diff] [blame] | 451 | if (ret && instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN) |
| 452 | instr->fail_addr -= mtd->offset; |
| 453 | |
| 454 | instr->addr -= mtd->offset; |
| 455 | |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 456 | return ret; |
| 457 | } |
| 458 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 459 | static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 460 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 461 | return mtd->parent->_lock(mtd->parent, ofs + mtd->offset, len); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 462 | } |
| 463 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 464 | static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 465 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 466 | return mtd->parent->_unlock(mtd->parent, ofs + mtd->offset, len); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 467 | } |
| 468 | |
| 469 | static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len) |
| 470 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 471 | return mtd->parent->_is_locked(mtd->parent, ofs + mtd->offset, len); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 472 | } |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 473 | |
| 474 | static void part_sync(struct mtd_info *mtd) |
| 475 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 476 | mtd->parent->_sync(mtd->parent); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 477 | } |
| 478 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 479 | #ifndef __UBOOT__ |
| 480 | static int part_suspend(struct mtd_info *mtd) |
| 481 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 482 | return mtd->parent->_suspend(mtd->parent); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 483 | } |
| 484 | |
| 485 | static void part_resume(struct mtd_info *mtd) |
| 486 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 487 | mtd->parent->_resume(mtd->parent); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 488 | } |
| 489 | #endif |
| 490 | |
Ezequiel Garcia | 86a720a | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 491 | static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs) |
| 492 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 493 | ofs += mtd->offset; |
| 494 | return mtd->parent->_block_isreserved(mtd->parent, ofs); |
Ezequiel Garcia | 86a720a | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 495 | } |
| 496 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 497 | static int part_block_isbad(struct mtd_info *mtd, loff_t ofs) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 498 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 499 | ofs += mtd->offset; |
| 500 | return mtd->parent->_block_isbad(mtd->parent, ofs); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 501 | } |
| 502 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 503 | static int part_block_markbad(struct mtd_info *mtd, loff_t ofs) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 504 | { |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 505 | int res; |
| 506 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 507 | ofs += mtd->offset; |
| 508 | res = mtd->parent->_block_markbad(mtd->parent, ofs); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 509 | if (!res) |
| 510 | mtd->ecc_stats.badblocks++; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 511 | return res; |
| 512 | } |
| 513 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 514 | static inline void free_partition(struct mtd_info *p) |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 515 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 516 | kfree(p->name); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 517 | kfree(p); |
| 518 | } |
| 519 | |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 520 | /* |
| 521 | * This function unregisters and destroy all slave MTD objects which are |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 522 | * attached to the given master MTD object, recursively. |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 523 | */ |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 524 | static int do_del_mtd_partitions(struct mtd_info *master) |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 525 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 526 | struct mtd_info *slave, *next; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 527 | int ret, err = 0; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 528 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 529 | list_for_each_entry_safe(slave, next, &master->partitions, node) { |
| 530 | if (mtd_has_partitions(slave)) |
| 531 | del_mtd_partitions(slave); |
Miquel Raynal | b0036f7 | 2018-08-16 17:30:19 +0200 | [diff] [blame] | 532 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 533 | debug("Deleting %s MTD partition\n", slave->name); |
| 534 | ret = del_mtd_device(slave); |
| 535 | if (ret < 0) { |
| 536 | printf("Error when deleting partition \"%s\" (%d)\n", |
| 537 | slave->name, ret); |
| 538 | err = ret; |
| 539 | continue; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 540 | } |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 541 | |
| 542 | list_del(&slave->node); |
| 543 | free_partition(slave); |
| 544 | } |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 545 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 546 | return err; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 547 | } |
| 548 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 549 | int del_mtd_partitions(struct mtd_info *master) |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 550 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 551 | int ret; |
| 552 | |
| 553 | debug("Deleting MTD partitions on \"%s\":\n", master->name); |
| 554 | |
| 555 | mutex_lock(&mtd_partitions_mutex); |
| 556 | ret = do_del_mtd_partitions(master); |
| 557 | mutex_unlock(&mtd_partitions_mutex); |
| 558 | |
| 559 | return ret; |
| 560 | } |
| 561 | |
| 562 | static struct mtd_info *allocate_partition(struct mtd_info *master, |
| 563 | const struct mtd_partition *part, |
| 564 | int partno, uint64_t cur_offset) |
| 565 | { |
| 566 | struct mtd_info *slave; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 567 | char *name; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 568 | |
| 569 | /* allocate the partition structure */ |
| 570 | slave = kzalloc(sizeof(*slave), GFP_KERNEL); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 571 | name = kstrdup(part->name, GFP_KERNEL); |
| 572 | if (!name || !slave) { |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 573 | printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n", |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 574 | master->name); |
| 575 | kfree(name); |
| 576 | kfree(slave); |
| 577 | return ERR_PTR(-ENOMEM); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 578 | } |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 579 | |
| 580 | /* set up the MTD object for this partition */ |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 581 | slave->type = master->type; |
| 582 | slave->flags = master->flags & ~part->mask_flags; |
| 583 | slave->size = part->size; |
| 584 | slave->writesize = master->writesize; |
| 585 | slave->writebufsize = master->writebufsize; |
| 586 | slave->oobsize = master->oobsize; |
| 587 | slave->oobavail = master->oobavail; |
| 588 | slave->subpage_sft = master->subpage_sft; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 589 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 590 | slave->name = name; |
| 591 | slave->owner = master->owner; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 592 | #ifndef __UBOOT__ |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 593 | slave->backing_dev_info = master->backing_dev_info; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 594 | |
| 595 | /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone |
| 596 | * to have the same data be in two different partitions. |
| 597 | */ |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 598 | slave->dev.parent = master->dev.parent; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 599 | #endif |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 600 | |
Boris Brezillon | 596cf08 | 2018-08-16 17:29:59 +0200 | [diff] [blame] | 601 | if (master->_read) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 602 | slave->_read = part_read; |
Boris Brezillon | 596cf08 | 2018-08-16 17:29:59 +0200 | [diff] [blame] | 603 | if (master->_write) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 604 | slave->_write = part_write; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 605 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 606 | if (master->_panic_write) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 607 | slave->_panic_write = part_panic_write; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 608 | |
| 609 | #ifndef __UBOOT__ |
| 610 | if (master->_point && master->_unpoint) { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 611 | slave->_point = part_point; |
| 612 | slave->_unpoint = part_unpoint; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 613 | } |
| 614 | #endif |
| 615 | |
| 616 | if (master->_get_unmapped_area) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 617 | slave->_get_unmapped_area = part_get_unmapped_area; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 618 | if (master->_read_oob) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 619 | slave->_read_oob = part_read_oob; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 620 | if (master->_write_oob) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 621 | slave->_write_oob = part_write_oob; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 622 | if (master->_read_user_prot_reg) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 623 | slave->_read_user_prot_reg = part_read_user_prot_reg; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 624 | if (master->_read_fact_prot_reg) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 625 | slave->_read_fact_prot_reg = part_read_fact_prot_reg; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 626 | if (master->_write_user_prot_reg) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 627 | slave->_write_user_prot_reg = part_write_user_prot_reg; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 628 | if (master->_lock_user_prot_reg) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 629 | slave->_lock_user_prot_reg = part_lock_user_prot_reg; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 630 | if (master->_get_user_prot_info) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 631 | slave->_get_user_prot_info = part_get_user_prot_info; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 632 | if (master->_get_fact_prot_info) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 633 | slave->_get_fact_prot_info = part_get_fact_prot_info; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 634 | if (master->_sync) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 635 | slave->_sync = part_sync; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 636 | #ifndef __UBOOT__ |
| 637 | if (!partno && !master->dev.class && master->_suspend && |
| 638 | master->_resume) { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 639 | slave->_suspend = part_suspend; |
| 640 | slave->_resume = part_resume; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 641 | } |
| 642 | if (master->_writev) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 643 | slave->_writev = part_writev; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 644 | #endif |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 645 | if (master->_lock) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 646 | slave->_lock = part_lock; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 647 | if (master->_unlock) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 648 | slave->_unlock = part_unlock; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 649 | if (master->_is_locked) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 650 | slave->_is_locked = part_is_locked; |
Ezequiel Garcia | 86a720a | 2014-05-21 19:06:12 -0300 | [diff] [blame] | 651 | if (master->_block_isreserved) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 652 | slave->_block_isreserved = part_block_isreserved; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 653 | if (master->_block_isbad) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 654 | slave->_block_isbad = part_block_isbad; |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 655 | if (master->_block_markbad) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 656 | slave->_block_markbad = part_block_markbad; |
| 657 | slave->_erase = part_erase; |
| 658 | slave->parent = master; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 659 | slave->offset = part->offset; |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 660 | INIT_LIST_HEAD(&slave->partitions); |
| 661 | INIT_LIST_HEAD(&slave->node); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 662 | |
| 663 | if (slave->offset == MTDPART_OFS_APPEND) |
| 664 | slave->offset = cur_offset; |
| 665 | if (slave->offset == MTDPART_OFS_NXTBLK) { |
| 666 | slave->offset = cur_offset; |
| 667 | if (mtd_mod_by_eb(cur_offset, master) != 0) { |
| 668 | /* Round up to next erasesize */ |
| 669 | slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 670 | debug("Moving partition %d: " |
| 671 | "0x%012llx -> 0x%012llx\n", partno, |
| 672 | (unsigned long long)cur_offset, (unsigned long long)slave->offset); |
| 673 | } |
| 674 | } |
| 675 | if (slave->offset == MTDPART_OFS_RETAIN) { |
| 676 | slave->offset = cur_offset; |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 677 | if (master->size - slave->offset >= slave->size) { |
| 678 | slave->size = master->size - slave->offset |
| 679 | - slave->size; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 680 | } else { |
| 681 | debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n", |
| 682 | part->name, master->size - slave->offset, |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 683 | slave->size); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 684 | /* register to preserve ordering */ |
| 685 | goto out_register; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 686 | } |
| 687 | } |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 688 | if (slave->size == MTDPART_SIZ_FULL) |
| 689 | slave->size = master->size - slave->offset; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 690 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 691 | debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset, |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 692 | (unsigned long long)(slave->offset + slave->size), slave->name); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 693 | |
| 694 | /* let's do some sanity checks */ |
| 695 | if (slave->offset >= master->size) { |
| 696 | /* let's register it anyway to preserve ordering */ |
| 697 | slave->offset = 0; |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 698 | slave->size = 0; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 699 | printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n", |
| 700 | part->name); |
| 701 | goto out_register; |
| 702 | } |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 703 | if (slave->offset + slave->size > master->size) { |
| 704 | slave->size = master->size - slave->offset; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 705 | printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n", |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 706 | part->name, master->name, slave->size); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 707 | } |
| 708 | if (master->numeraseregions > 1) { |
| 709 | /* Deal with variable erase size stuff */ |
| 710 | int i, max = master->numeraseregions; |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 711 | u64 end = slave->offset + slave->size; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 712 | struct mtd_erase_region_info *regions = master->eraseregions; |
| 713 | |
| 714 | /* Find the first erase regions which is part of this |
| 715 | * partition. */ |
| 716 | for (i = 0; i < max && regions[i].offset <= slave->offset; i++) |
| 717 | ; |
| 718 | /* The loop searched for the region _behind_ the first one */ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 719 | if (i > 0) |
| 720 | i--; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 721 | |
| 722 | /* Pick biggest erasesize */ |
| 723 | for (; i < max && regions[i].offset < end; i++) { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 724 | if (slave->erasesize < regions[i].erasesize) |
| 725 | slave->erasesize = regions[i].erasesize; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 726 | } |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 727 | WARN_ON(slave->erasesize == 0); |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 728 | } else { |
| 729 | /* Single erase size */ |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 730 | slave->erasesize = master->erasesize; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 731 | } |
| 732 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 733 | if ((slave->flags & MTD_WRITEABLE) && |
| 734 | mtd_mod_by_eb(slave->offset, slave)) { |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 735 | /* Doesn't start on a boundary of major erase size */ |
| 736 | /* FIXME: Let it be writable if it is on a boundary of |
| 737 | * _minor_ erase size though */ |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 738 | slave->flags &= ~MTD_WRITEABLE; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 739 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n", |
| 740 | part->name); |
| 741 | } |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 742 | if ((slave->flags & MTD_WRITEABLE) && |
| 743 | mtd_mod_by_eb(slave->size, slave)) { |
| 744 | slave->flags &= ~MTD_WRITEABLE; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 745 | printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n", |
| 746 | part->name); |
| 747 | } |
| 748 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 749 | slave->ecclayout = master->ecclayout; |
| 750 | slave->ecc_step_size = master->ecc_step_size; |
| 751 | slave->ecc_strength = master->ecc_strength; |
| 752 | slave->bitflip_threshold = master->bitflip_threshold; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 753 | |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 754 | if (master->_block_isbad) { |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 755 | uint64_t offs = 0; |
| 756 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 757 | while (offs < slave->size) { |
Sergey Lapin | dfe64e2 | 2013-01-14 03:46:50 +0000 | [diff] [blame] | 758 | if (mtd_block_isbad(master, offs + slave->offset)) |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 759 | slave->ecc_stats.badblocks++; |
| 760 | offs += slave->erasesize; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 761 | } |
| 762 | } |
| 763 | |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 764 | out_register: |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 765 | return slave; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 766 | } |
| 767 | |
Heiko Schocher | ddf7bcf | 2014-07-15 16:08:42 +0200 | [diff] [blame] | 768 | #ifndef __UBOOT__ |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 769 | int mtd_add_partition(struct mtd_info *master, const char *name, |
| 770 | long long offset, long long length) |
| 771 | { |
| 772 | struct mtd_partition part; |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 773 | struct mtd_info *p, *new; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 774 | uint64_t start, end; |
| 775 | int ret = 0; |
| 776 | |
| 777 | /* the direct offset is expected */ |
| 778 | if (offset == MTDPART_OFS_APPEND || |
| 779 | offset == MTDPART_OFS_NXTBLK) |
| 780 | return -EINVAL; |
| 781 | |
| 782 | if (length == MTDPART_SIZ_FULL) |
| 783 | length = master->size - offset; |
| 784 | |
| 785 | if (length <= 0) |
| 786 | return -EINVAL; |
| 787 | |
| 788 | part.name = name; |
| 789 | part.size = length; |
| 790 | part.offset = offset; |
| 791 | part.mask_flags = 0; |
| 792 | part.ecclayout = NULL; |
| 793 | |
| 794 | new = allocate_partition(master, &part, -1, offset); |
| 795 | if (IS_ERR(new)) |
| 796 | return PTR_ERR(new); |
| 797 | |
| 798 | start = offset; |
| 799 | end = offset + length; |
| 800 | |
| 801 | mutex_lock(&mtd_partitions_mutex); |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 802 | list_for_each_entry(p, &master->partitions, node) { |
| 803 | if (start >= p->offset && |
| 804 | (start < (p->offset + p->size))) |
| 805 | goto err_inv; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 806 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 807 | if (end >= p->offset && |
| 808 | (end < (p->offset + p->size))) |
| 809 | goto err_inv; |
| 810 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 811 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 812 | list_add_tail(&new->node, &master->partitions); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 813 | mutex_unlock(&mtd_partitions_mutex); |
| 814 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 815 | add_mtd_device(new); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 816 | |
| 817 | return ret; |
| 818 | err_inv: |
| 819 | mutex_unlock(&mtd_partitions_mutex); |
| 820 | free_partition(new); |
| 821 | return -EINVAL; |
| 822 | } |
| 823 | EXPORT_SYMBOL_GPL(mtd_add_partition); |
| 824 | |
| 825 | int mtd_del_partition(struct mtd_info *master, int partno) |
| 826 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 827 | struct mtd_info *slave, *next; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 828 | int ret = -EINVAL; |
| 829 | |
| 830 | mutex_lock(&mtd_partitions_mutex); |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 831 | list_for_each_entry_safe(slave, next, &master->partitions, node) |
| 832 | if (slave->index == partno) { |
| 833 | ret = del_mtd_device(slave); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 834 | if (ret < 0) |
| 835 | break; |
| 836 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 837 | list_del(&slave->node); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 838 | free_partition(slave); |
| 839 | break; |
| 840 | } |
| 841 | mutex_unlock(&mtd_partitions_mutex); |
| 842 | |
| 843 | return ret; |
| 844 | } |
| 845 | EXPORT_SYMBOL_GPL(mtd_del_partition); |
Heiko Schocher | ddf7bcf | 2014-07-15 16:08:42 +0200 | [diff] [blame] | 846 | #endif |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 847 | |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 848 | /* |
| 849 | * This function, given a master MTD object and a partition table, creates |
| 850 | * and registers slave MTD objects which are bound to the master according to |
| 851 | * the partition definitions. |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 852 | * |
| 853 | * We don't register the master, or expect the caller to have done so, |
| 854 | * for reasons of data integrity. |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 855 | */ |
| 856 | |
| 857 | int add_mtd_partitions(struct mtd_info *master, |
| 858 | const struct mtd_partition *parts, |
| 859 | int nbparts) |
| 860 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 861 | struct mtd_info *slave; |
Stefan Roese | 8d2effe | 2009-05-11 16:03:55 +0200 | [diff] [blame] | 862 | uint64_t cur_offset = 0; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 863 | int i; |
| 864 | |
Joe Hershberger | 147162d | 2013-04-08 10:32:49 +0000 | [diff] [blame] | 865 | debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name); |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 866 | |
| 867 | for (i = 0; i < nbparts; i++) { |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 868 | slave = allocate_partition(master, parts + i, i, cur_offset); |
| 869 | if (IS_ERR(slave)) |
| 870 | return PTR_ERR(slave); |
| 871 | |
| 872 | mutex_lock(&mtd_partitions_mutex); |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 873 | list_add_tail(&slave->node, &master->partitions); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 874 | mutex_unlock(&mtd_partitions_mutex); |
| 875 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 876 | add_mtd_device(slave); |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 877 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 878 | cur_offset = slave->offset + slave->size; |
Kyungmin Park | e29c22f | 2008-11-19 16:20:36 +0100 | [diff] [blame] | 879 | } |
| 880 | |
| 881 | return 0; |
| 882 | } |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 883 | |
Marek Behún | dc339bf | 2021-05-26 14:08:19 +0200 | [diff] [blame] | 884 | #if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(OF_CONTROL) |
| 885 | int add_mtd_partitions_of(struct mtd_info *master) |
| 886 | { |
| 887 | ofnode parts, child; |
| 888 | int i = 0; |
| 889 | |
| 890 | if (!master->dev) |
| 891 | return 0; |
| 892 | |
| 893 | parts = ofnode_find_subnode(mtd_get_ofnode(master), "partitions"); |
| 894 | if (!ofnode_valid(parts) || !ofnode_is_available(parts) || |
| 895 | !ofnode_device_is_compatible(parts, "fixed-partitions")) |
| 896 | return 0; |
| 897 | |
| 898 | ofnode_for_each_subnode(child, parts) { |
| 899 | struct mtd_partition part = { 0 }; |
| 900 | struct mtd_info *slave; |
| 901 | fdt_addr_t offset, size; |
| 902 | |
| 903 | if (!ofnode_is_available(child)) |
| 904 | continue; |
| 905 | |
| 906 | offset = ofnode_get_addr_size_index_notrans(child, 0, &size); |
| 907 | if (offset == FDT_ADDR_T_NONE || !size) { |
| 908 | debug("Missing partition offset/size on \"%s\" partition\n", |
| 909 | master->name); |
| 910 | continue; |
| 911 | } |
| 912 | |
| 913 | part.name = ofnode_read_string(child, "label"); |
| 914 | if (!part.name) |
| 915 | part.name = ofnode_read_string(child, "name"); |
| 916 | |
| 917 | /* |
| 918 | * .mask_flags is used to remove flags in allocate_partition(), |
| 919 | * so when "read-only" is present, we add MTD_WRITABLE to the |
| 920 | * mask, and so MTD_WRITABLE will be removed on partition |
| 921 | * allocation |
| 922 | */ |
| 923 | if (ofnode_read_bool(child, "read-only")) |
| 924 | part.mask_flags |= MTD_WRITEABLE; |
| 925 | if (ofnode_read_bool(child, "lock")) |
| 926 | part.mask_flags |= MTD_POWERUP_LOCK; |
| 927 | |
| 928 | part.offset = offset; |
| 929 | part.size = size; |
| 930 | part.ecclayout = master->ecclayout; |
| 931 | |
| 932 | slave = allocate_partition(master, &part, i++, 0); |
| 933 | if (IS_ERR(slave)) |
| 934 | return PTR_ERR(slave); |
| 935 | |
| 936 | mutex_lock(&mtd_partitions_mutex); |
| 937 | list_add_tail(&slave->node, &master->partitions); |
| 938 | mutex_unlock(&mtd_partitions_mutex); |
| 939 | |
| 940 | add_mtd_device(slave); |
| 941 | } |
| 942 | |
| 943 | return 0; |
| 944 | } |
| 945 | #endif /* CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(OF_CONTROL) */ |
| 946 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 947 | #ifndef __UBOOT__ |
| 948 | static DEFINE_SPINLOCK(part_parser_lock); |
| 949 | static LIST_HEAD(part_parsers); |
| 950 | |
| 951 | static struct mtd_part_parser *get_partition_parser(const char *name) |
| 952 | { |
| 953 | struct mtd_part_parser *p, *ret = NULL; |
| 954 | |
| 955 | spin_lock(&part_parser_lock); |
| 956 | |
| 957 | list_for_each_entry(p, &part_parsers, list) |
| 958 | if (!strcmp(p->name, name) && try_module_get(p->owner)) { |
| 959 | ret = p; |
| 960 | break; |
| 961 | } |
| 962 | |
| 963 | spin_unlock(&part_parser_lock); |
| 964 | |
| 965 | return ret; |
| 966 | } |
| 967 | |
| 968 | #define put_partition_parser(p) do { module_put((p)->owner); } while (0) |
| 969 | |
| 970 | void register_mtd_parser(struct mtd_part_parser *p) |
| 971 | { |
| 972 | spin_lock(&part_parser_lock); |
| 973 | list_add(&p->list, &part_parsers); |
| 974 | spin_unlock(&part_parser_lock); |
| 975 | } |
| 976 | EXPORT_SYMBOL_GPL(register_mtd_parser); |
| 977 | |
| 978 | void deregister_mtd_parser(struct mtd_part_parser *p) |
| 979 | { |
| 980 | spin_lock(&part_parser_lock); |
| 981 | list_del(&p->list); |
| 982 | spin_unlock(&part_parser_lock); |
| 983 | } |
| 984 | EXPORT_SYMBOL_GPL(deregister_mtd_parser); |
| 985 | |
| 986 | /* |
| 987 | * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you |
| 988 | * are changing this array! |
| 989 | */ |
| 990 | static const char * const default_mtd_part_types[] = { |
| 991 | "cmdlinepart", |
| 992 | "ofpart", |
| 993 | NULL |
| 994 | }; |
| 995 | |
| 996 | /** |
| 997 | * parse_mtd_partitions - parse MTD partitions |
| 998 | * @master: the master partition (describes whole MTD device) |
| 999 | * @types: names of partition parsers to try or %NULL |
| 1000 | * @pparts: array of partitions found is returned here |
| 1001 | * @data: MTD partition parser-specific data |
| 1002 | * |
| 1003 | * This function tries to find partition on MTD device @master. It uses MTD |
| 1004 | * partition parsers, specified in @types. However, if @types is %NULL, then |
| 1005 | * the default list of parsers is used. The default list contains only the |
| 1006 | * "cmdlinepart" and "ofpart" parsers ATM. |
| 1007 | * Note: If there are more then one parser in @types, the kernel only takes the |
| 1008 | * partitions parsed out by the first parser. |
| 1009 | * |
| 1010 | * This function may return: |
| 1011 | * o a negative error code in case of failure |
| 1012 | * o zero if no partitions were found |
| 1013 | * o a positive number of found partitions, in which case on exit @pparts will |
| 1014 | * point to an array containing this number of &struct mtd_info objects. |
| 1015 | */ |
| 1016 | int parse_mtd_partitions(struct mtd_info *master, const char *const *types, |
| 1017 | struct mtd_partition **pparts, |
| 1018 | struct mtd_part_parser_data *data) |
| 1019 | { |
| 1020 | struct mtd_part_parser *parser; |
| 1021 | int ret = 0; |
| 1022 | |
| 1023 | if (!types) |
| 1024 | types = default_mtd_part_types; |
| 1025 | |
| 1026 | for ( ; ret <= 0 && *types; types++) { |
| 1027 | parser = get_partition_parser(*types); |
| 1028 | if (!parser && !request_module("%s", *types)) |
| 1029 | parser = get_partition_parser(*types); |
| 1030 | if (!parser) |
| 1031 | continue; |
| 1032 | ret = (*parser->parse_fn)(master, pparts, data); |
| 1033 | put_partition_parser(parser); |
| 1034 | if (ret > 0) { |
| 1035 | printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n", |
| 1036 | ret, parser->name, master->name); |
| 1037 | break; |
| 1038 | } |
| 1039 | } |
| 1040 | return ret; |
| 1041 | } |
| 1042 | #endif |
| 1043 | |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1044 | /* Returns the size of the entire flash chip */ |
| 1045 | uint64_t mtd_get_device_size(const struct mtd_info *mtd) |
| 1046 | { |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 1047 | if (mtd_is_partition(mtd)) |
| 1048 | return mtd->parent->size; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1049 | |
Miquel Raynal | 2a74930 | 2018-09-29 12:58:27 +0200 | [diff] [blame] | 1050 | return mtd->size; |
Heiko Schocher | ff94bc4 | 2014-06-24 10:10:04 +0200 | [diff] [blame] | 1051 | } |
| 1052 | EXPORT_SYMBOL_GPL(mtd_get_device_size); |