blob: 6ab481a7b1c01a3be2616e7730bb8dc96432753c [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Parke29c22f2008-11-19 16:20:36 +01002/*
3 * Simple MTD partitioning layer
4 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02005 * 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 Parke29c22f2008-11-19 16:20:36 +01008 *
Kyungmin Parke29c22f2008-11-19 16:20:36 +01009 */
10
Heiko Schocherff94bc42014-06-24 10:10:04 +020011#ifndef __UBOOT__
Simon Glassf7ae49f2020-05-10 11:40:05 -060012#include <log.h>
Simon Glass61b29b82020-02-03 07:36:15 -070013#include <dm/devres.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020014#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 Parke29c22f2008-11-19 16:20:36 +010022#include <common.h>
23#include <malloc.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060024#include <linux/bug.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090025#include <linux/errno.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020026#include <linux/compat.h>
27#include <ubi_uboot.h>
Kyungmin Parke29c22f2008-11-19 16:20:36 +010028
Kyungmin Parke29c22f2008-11-19 16:20:36 +010029#include <linux/mtd/mtd.h>
30#include <linux/mtd/partitions.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020031#include <linux/err.h>
Miquel Raynal21cc1fb2018-09-29 12:58:25 +020032#include <linux/sizes.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020033
34#include "mtdcore.h"
Kyungmin Parke29c22f2008-11-19 16:20:36 +010035
Heiko Schocherff94bc42014-06-24 10:10:04 +020036#ifndef __UBOOT__
37static DEFINE_MUTEX(mtd_partitions_mutex);
38#else
39DEFINE_MUTEX(mtd_partitions_mutex);
40#endif
Kyungmin Parke29c22f2008-11-19 16:20:36 +010041
Heiko Schocherff94bc42014-06-24 10:10:04 +020042#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 */
50char *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 Raynal21cc1fb2018-09-29 12:58:25 +020066#define MTD_SIZE_REMAINING (~0LLU)
67#define MTD_OFFSET_NOT_SPECIFIED (~0LLU)
68
Boris Brezillon4a5594f2018-12-02 10:54:30 +010069bool 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 Raynal21cc1fb2018-09-29 12:58:25 +020081/**
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 *
92 * @return 0 on success, an error otherwise.
93 */
94static 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 *
203 * @return 0 on success, an error otherwise.
204 */
205int 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 Kaistra892d4612020-07-13 14:40:02 +0200210 uint64_t cur_off = 0, cur_sz = 0;
Miquel Raynal21cc1fb2018-09-29 12:58:25 +0200211 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 */
276void 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 Parke29c22f2008-11-19 16:20:36 +0100287/*
288 * MTD methods which simply translate the effective address and pass through
289 * to the _real_ device.
290 */
291
Stefan Roese8d2effe2009-05-11 16:03:55 +0200292static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
293 size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100294{
Stefan Roese8d2effe2009-05-11 16:03:55 +0200295 struct mtd_ecc_stats stats;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100296 int res;
297
Miquel Raynal2a749302018-09-29 12:58:27 +0200298 stats = mtd->parent->ecc_stats;
299 res = mtd->parent->_read(mtd->parent, from + mtd->offset, len,
300 retlen, buf);
Paul Burton40462e52013-09-04 15:16:56 +0100301 if (unlikely(mtd_is_eccerr(res)))
302 mtd->ecc_stats.failed +=
Miquel Raynal2a749302018-09-29 12:58:27 +0200303 mtd->parent->ecc_stats.failed - stats.failed;
Paul Burton40462e52013-09-04 15:16:56 +0100304 else
305 mtd->ecc_stats.corrected +=
Miquel Raynal2a749302018-09-29 12:58:27 +0200306 mtd->parent->ecc_stats.corrected - stats.corrected;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100307 return res;
308}
309
Heiko Schocherff94bc42014-06-24 10:10:04 +0200310#ifndef __UBOOT__
311static 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 Raynal2a749302018-09-29 12:58:27 +0200314 return mtd->parent->_point(mtd->parent, from + mtd->offset, len,
315 retlen, virt, phys);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200316}
317
318static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
319{
Miquel Raynal2a749302018-09-29 12:58:27 +0200320 return mtd->parent->_unpoint(mtd->parent, from + mtd->offset, len);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200321}
322#endif
323
324static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
325 unsigned long len,
326 unsigned long offset,
327 unsigned long flags)
328{
Miquel Raynal2a749302018-09-29 12:58:27 +0200329 offset += mtd->offset;
330 return mtd->parent->_get_unmapped_area(mtd->parent, len, offset, flags);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200331}
332
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100333static int part_read_oob(struct mtd_info *mtd, loff_t from,
Stefan Roese8d2effe2009-05-11 16:03:55 +0200334 struct mtd_oob_ops *ops)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100335{
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100336 int res;
337
338 if (from >= mtd->size)
339 return -EINVAL;
340 if (ops->datbuf && from + ops->len > mtd->size)
341 return -EINVAL;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100342
Heiko Schocherff94bc42014-06-24 10:10:04 +0200343 /*
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 Raynal2a749302018-09-29 12:58:27 +0200360 res = mtd->parent->_read_oob(mtd->parent, from + mtd->offset, ops);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100361 if (unlikely(res)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000362 if (mtd_is_bitflip(res))
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100363 mtd->ecc_stats.corrected++;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000364 if (mtd_is_eccerr(res))
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100365 mtd->ecc_stats.failed++;
366 }
367 return res;
368}
369
Stefan Roese8d2effe2009-05-11 16:03:55 +0200370static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
371 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100372{
Miquel Raynal2a749302018-09-29 12:58:27 +0200373 return mtd->parent->_read_user_prot_reg(mtd->parent, from, len,
374 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100375}
376
Heiko Schocher4e67c572014-07-15 16:08:43 +0200377static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
378 size_t *retlen, struct otp_info *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100379{
Miquel Raynal2a749302018-09-29 12:58:27 +0200380 return mtd->parent->_get_user_prot_info(mtd->parent, len, retlen,
381 buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100382}
383
Stefan Roese8d2effe2009-05-11 16:03:55 +0200384static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
385 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100386{
Miquel Raynal2a749302018-09-29 12:58:27 +0200387 return mtd->parent->_read_fact_prot_reg(mtd->parent, from, len,
388 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100389}
390
Heiko Schocher4e67c572014-07-15 16:08:43 +0200391static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
392 size_t *retlen, struct otp_info *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100393{
Miquel Raynal2a749302018-09-29 12:58:27 +0200394 return mtd->parent->_get_fact_prot_info(mtd->parent, len, retlen,
395 buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100396}
397
Stefan Roese8d2effe2009-05-11 16:03:55 +0200398static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
399 size_t *retlen, const u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100400{
Miquel Raynal2a749302018-09-29 12:58:27 +0200401 return mtd->parent->_write(mtd->parent, to + mtd->offset, len,
402 retlen, buf);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200403}
404
405static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
406 size_t *retlen, const u_char *buf)
407{
Miquel Raynal2a749302018-09-29 12:58:27 +0200408 return mtd->parent->_panic_write(mtd->parent, to + mtd->offset, len,
409 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100410}
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100411
412static int part_write_oob(struct mtd_info *mtd, loff_t to,
Stefan Roese8d2effe2009-05-11 16:03:55 +0200413 struct mtd_oob_ops *ops)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100414{
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100415 if (to >= mtd->size)
416 return -EINVAL;
417 if (ops->datbuf && to + ops->len > mtd->size)
418 return -EINVAL;
Miquel Raynal2a749302018-09-29 12:58:27 +0200419 return mtd->parent->_write_oob(mtd->parent, to + mtd->offset, ops);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100420}
421
Stefan Roese8d2effe2009-05-11 16:03:55 +0200422static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
423 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100424{
Miquel Raynal2a749302018-09-29 12:58:27 +0200425 return mtd->parent->_write_user_prot_reg(mtd->parent, from, len,
426 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100427}
428
Stefan Roese8d2effe2009-05-11 16:03:55 +0200429static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
430 size_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100431{
Miquel Raynal2a749302018-09-29 12:58:27 +0200432 return mtd->parent->_lock_user_prot_reg(mtd->parent, from, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100433}
434
Heiko Schocherff94bc42014-06-24 10:10:04 +0200435#ifndef __UBOOT__
436static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
437 unsigned long count, loff_t to, size_t *retlen)
438{
Miquel Raynal2a749302018-09-29 12:58:27 +0200439 return mtd->parent->_writev(mtd->parent, vecs, count,
440 to + mtd->offset, retlen);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200441}
442#endif
443
Stefan Roese8d2effe2009-05-11 16:03:55 +0200444static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100445{
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100446 int ret;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000447
Miquel Raynal2a749302018-09-29 12:58:27 +0200448 instr->addr += mtd->offset;
Marek Behúna60397d2021-10-05 15:56:05 +0200449
Miquel Raynal2a749302018-09-29 12:58:27 +0200450 ret = mtd->parent->_erase(mtd->parent, instr);
Marek Behúna60397d2021-10-05 15:56:05 +0200451 if (ret && instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
452 instr->fail_addr -= mtd->offset;
453
454 instr->addr -= mtd->offset;
455
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100456 return ret;
457}
458
459void mtd_erase_callback(struct erase_info *instr)
460{
Marek Behúna60397d2021-10-05 15:56:05 +0200461 if (!instr->callback)
462 return;
463
464 if (instr->mtd->_erase == part_erase && instr->len) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200465 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Miquel Raynal2a749302018-09-29 12:58:27 +0200466 instr->fail_addr -= instr->mtd->offset;
467 instr->addr -= instr->mtd->offset;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100468 }
Marek Behúna60397d2021-10-05 15:56:05 +0200469
470 instr->callback(instr);
471
472 if (instr->mtd->_erase == part_erase && instr->len) {
473 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
474 instr->fail_addr += instr->mtd->offset;
475 instr->addr += instr->mtd->offset;
476 }
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100477}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200478EXPORT_SYMBOL_GPL(mtd_erase_callback);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100479
Stefan Roese8d2effe2009-05-11 16:03:55 +0200480static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100481{
Miquel Raynal2a749302018-09-29 12:58:27 +0200482 return mtd->parent->_lock(mtd->parent, ofs + mtd->offset, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100483}
484
Stefan Roese8d2effe2009-05-11 16:03:55 +0200485static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100486{
Miquel Raynal2a749302018-09-29 12:58:27 +0200487 return mtd->parent->_unlock(mtd->parent, ofs + mtd->offset, len);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200488}
489
490static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
491{
Miquel Raynal2a749302018-09-29 12:58:27 +0200492 return mtd->parent->_is_locked(mtd->parent, ofs + mtd->offset, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100493}
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100494
495static void part_sync(struct mtd_info *mtd)
496{
Miquel Raynal2a749302018-09-29 12:58:27 +0200497 mtd->parent->_sync(mtd->parent);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100498}
499
Heiko Schocherff94bc42014-06-24 10:10:04 +0200500#ifndef __UBOOT__
501static int part_suspend(struct mtd_info *mtd)
502{
Miquel Raynal2a749302018-09-29 12:58:27 +0200503 return mtd->parent->_suspend(mtd->parent);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200504}
505
506static void part_resume(struct mtd_info *mtd)
507{
Miquel Raynal2a749302018-09-29 12:58:27 +0200508 mtd->parent->_resume(mtd->parent);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200509}
510#endif
511
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300512static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
513{
Miquel Raynal2a749302018-09-29 12:58:27 +0200514 ofs += mtd->offset;
515 return mtd->parent->_block_isreserved(mtd->parent, ofs);
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300516}
517
Stefan Roese8d2effe2009-05-11 16:03:55 +0200518static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100519{
Miquel Raynal2a749302018-09-29 12:58:27 +0200520 ofs += mtd->offset;
521 return mtd->parent->_block_isbad(mtd->parent, ofs);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100522}
523
Stefan Roese8d2effe2009-05-11 16:03:55 +0200524static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100525{
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100526 int res;
527
Miquel Raynal2a749302018-09-29 12:58:27 +0200528 ofs += mtd->offset;
529 res = mtd->parent->_block_markbad(mtd->parent, ofs);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100530 if (!res)
531 mtd->ecc_stats.badblocks++;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100532 return res;
533}
534
Miquel Raynal2a749302018-09-29 12:58:27 +0200535static inline void free_partition(struct mtd_info *p)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200536{
Miquel Raynal2a749302018-09-29 12:58:27 +0200537 kfree(p->name);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200538 kfree(p);
539}
540
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100541/*
542 * This function unregisters and destroy all slave MTD objects which are
Miquel Raynal2a749302018-09-29 12:58:27 +0200543 * attached to the given master MTD object, recursively.
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100544 */
Miquel Raynal2a749302018-09-29 12:58:27 +0200545static int do_del_mtd_partitions(struct mtd_info *master)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100546{
Miquel Raynal2a749302018-09-29 12:58:27 +0200547 struct mtd_info *slave, *next;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200548 int ret, err = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100549
Miquel Raynal2a749302018-09-29 12:58:27 +0200550 list_for_each_entry_safe(slave, next, &master->partitions, node) {
551 if (mtd_has_partitions(slave))
552 del_mtd_partitions(slave);
Miquel Raynalb0036f72018-08-16 17:30:19 +0200553
Miquel Raynal2a749302018-09-29 12:58:27 +0200554 debug("Deleting %s MTD partition\n", slave->name);
555 ret = del_mtd_device(slave);
556 if (ret < 0) {
557 printf("Error when deleting partition \"%s\" (%d)\n",
558 slave->name, ret);
559 err = ret;
560 continue;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200561 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200562
563 list_del(&slave->node);
564 free_partition(slave);
565 }
Stefan Roese8d2effe2009-05-11 16:03:55 +0200566
Heiko Schocherff94bc42014-06-24 10:10:04 +0200567 return err;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200568}
569
Miquel Raynal2a749302018-09-29 12:58:27 +0200570int del_mtd_partitions(struct mtd_info *master)
Stefan Roese8d2effe2009-05-11 16:03:55 +0200571{
Miquel Raynal2a749302018-09-29 12:58:27 +0200572 int ret;
573
574 debug("Deleting MTD partitions on \"%s\":\n", master->name);
575
576 mutex_lock(&mtd_partitions_mutex);
577 ret = do_del_mtd_partitions(master);
578 mutex_unlock(&mtd_partitions_mutex);
579
580 return ret;
581}
582
583static struct mtd_info *allocate_partition(struct mtd_info *master,
584 const struct mtd_partition *part,
585 int partno, uint64_t cur_offset)
586{
587 struct mtd_info *slave;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200588 char *name;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200589
590 /* allocate the partition structure */
591 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200592 name = kstrdup(part->name, GFP_KERNEL);
593 if (!name || !slave) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200594 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200595 master->name);
596 kfree(name);
597 kfree(slave);
598 return ERR_PTR(-ENOMEM);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200599 }
Stefan Roese8d2effe2009-05-11 16:03:55 +0200600
601 /* set up the MTD object for this partition */
Miquel Raynal2a749302018-09-29 12:58:27 +0200602 slave->type = master->type;
603 slave->flags = master->flags & ~part->mask_flags;
604 slave->size = part->size;
605 slave->writesize = master->writesize;
606 slave->writebufsize = master->writebufsize;
607 slave->oobsize = master->oobsize;
608 slave->oobavail = master->oobavail;
609 slave->subpage_sft = master->subpage_sft;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200610
Miquel Raynal2a749302018-09-29 12:58:27 +0200611 slave->name = name;
612 slave->owner = master->owner;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200613#ifndef __UBOOT__
Miquel Raynal2a749302018-09-29 12:58:27 +0200614 slave->backing_dev_info = master->backing_dev_info;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200615
616 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
617 * to have the same data be in two different partitions.
618 */
Miquel Raynal2a749302018-09-29 12:58:27 +0200619 slave->dev.parent = master->dev.parent;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200620#endif
Stefan Roese8d2effe2009-05-11 16:03:55 +0200621
Boris Brezillon596cf082018-08-16 17:29:59 +0200622 if (master->_read)
Miquel Raynal2a749302018-09-29 12:58:27 +0200623 slave->_read = part_read;
Boris Brezillon596cf082018-08-16 17:29:59 +0200624 if (master->_write)
Miquel Raynal2a749302018-09-29 12:58:27 +0200625 slave->_write = part_write;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200626
Heiko Schocherff94bc42014-06-24 10:10:04 +0200627 if (master->_panic_write)
Miquel Raynal2a749302018-09-29 12:58:27 +0200628 slave->_panic_write = part_panic_write;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200629
630#ifndef __UBOOT__
631 if (master->_point && master->_unpoint) {
Miquel Raynal2a749302018-09-29 12:58:27 +0200632 slave->_point = part_point;
633 slave->_unpoint = part_unpoint;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200634 }
635#endif
636
637 if (master->_get_unmapped_area)
Miquel Raynal2a749302018-09-29 12:58:27 +0200638 slave->_get_unmapped_area = part_get_unmapped_area;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000639 if (master->_read_oob)
Miquel Raynal2a749302018-09-29 12:58:27 +0200640 slave->_read_oob = part_read_oob;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000641 if (master->_write_oob)
Miquel Raynal2a749302018-09-29 12:58:27 +0200642 slave->_write_oob = part_write_oob;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000643 if (master->_read_user_prot_reg)
Miquel Raynal2a749302018-09-29 12:58:27 +0200644 slave->_read_user_prot_reg = part_read_user_prot_reg;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000645 if (master->_read_fact_prot_reg)
Miquel Raynal2a749302018-09-29 12:58:27 +0200646 slave->_read_fact_prot_reg = part_read_fact_prot_reg;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000647 if (master->_write_user_prot_reg)
Miquel Raynal2a749302018-09-29 12:58:27 +0200648 slave->_write_user_prot_reg = part_write_user_prot_reg;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000649 if (master->_lock_user_prot_reg)
Miquel Raynal2a749302018-09-29 12:58:27 +0200650 slave->_lock_user_prot_reg = part_lock_user_prot_reg;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000651 if (master->_get_user_prot_info)
Miquel Raynal2a749302018-09-29 12:58:27 +0200652 slave->_get_user_prot_info = part_get_user_prot_info;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000653 if (master->_get_fact_prot_info)
Miquel Raynal2a749302018-09-29 12:58:27 +0200654 slave->_get_fact_prot_info = part_get_fact_prot_info;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000655 if (master->_sync)
Miquel Raynal2a749302018-09-29 12:58:27 +0200656 slave->_sync = part_sync;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200657#ifndef __UBOOT__
658 if (!partno && !master->dev.class && master->_suspend &&
659 master->_resume) {
Miquel Raynal2a749302018-09-29 12:58:27 +0200660 slave->_suspend = part_suspend;
661 slave->_resume = part_resume;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200662 }
663 if (master->_writev)
Miquel Raynal2a749302018-09-29 12:58:27 +0200664 slave->_writev = part_writev;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200665#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +0000666 if (master->_lock)
Miquel Raynal2a749302018-09-29 12:58:27 +0200667 slave->_lock = part_lock;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000668 if (master->_unlock)
Miquel Raynal2a749302018-09-29 12:58:27 +0200669 slave->_unlock = part_unlock;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200670 if (master->_is_locked)
Miquel Raynal2a749302018-09-29 12:58:27 +0200671 slave->_is_locked = part_is_locked;
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300672 if (master->_block_isreserved)
Miquel Raynal2a749302018-09-29 12:58:27 +0200673 slave->_block_isreserved = part_block_isreserved;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000674 if (master->_block_isbad)
Miquel Raynal2a749302018-09-29 12:58:27 +0200675 slave->_block_isbad = part_block_isbad;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000676 if (master->_block_markbad)
Miquel Raynal2a749302018-09-29 12:58:27 +0200677 slave->_block_markbad = part_block_markbad;
678 slave->_erase = part_erase;
679 slave->parent = master;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200680 slave->offset = part->offset;
Miquel Raynal2a749302018-09-29 12:58:27 +0200681 INIT_LIST_HEAD(&slave->partitions);
682 INIT_LIST_HEAD(&slave->node);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200683
684 if (slave->offset == MTDPART_OFS_APPEND)
685 slave->offset = cur_offset;
686 if (slave->offset == MTDPART_OFS_NXTBLK) {
687 slave->offset = cur_offset;
688 if (mtd_mod_by_eb(cur_offset, master) != 0) {
689 /* Round up to next erasesize */
690 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200691 debug("Moving partition %d: "
692 "0x%012llx -> 0x%012llx\n", partno,
693 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
694 }
695 }
696 if (slave->offset == MTDPART_OFS_RETAIN) {
697 slave->offset = cur_offset;
Miquel Raynal2a749302018-09-29 12:58:27 +0200698 if (master->size - slave->offset >= slave->size) {
699 slave->size = master->size - slave->offset
700 - slave->size;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200701 } else {
702 debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
703 part->name, master->size - slave->offset,
Miquel Raynal2a749302018-09-29 12:58:27 +0200704 slave->size);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200705 /* register to preserve ordering */
706 goto out_register;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200707 }
708 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200709 if (slave->size == MTDPART_SIZ_FULL)
710 slave->size = master->size - slave->offset;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200711
Heiko Schocherff94bc42014-06-24 10:10:04 +0200712 debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
Miquel Raynal2a749302018-09-29 12:58:27 +0200713 (unsigned long long)(slave->offset + slave->size), slave->name);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200714
715 /* let's do some sanity checks */
716 if (slave->offset >= master->size) {
717 /* let's register it anyway to preserve ordering */
718 slave->offset = 0;
Miquel Raynal2a749302018-09-29 12:58:27 +0200719 slave->size = 0;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200720 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
721 part->name);
722 goto out_register;
723 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200724 if (slave->offset + slave->size > master->size) {
725 slave->size = master->size - slave->offset;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200726 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
Miquel Raynal2a749302018-09-29 12:58:27 +0200727 part->name, master->name, slave->size);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200728 }
729 if (master->numeraseregions > 1) {
730 /* Deal with variable erase size stuff */
731 int i, max = master->numeraseregions;
Miquel Raynal2a749302018-09-29 12:58:27 +0200732 u64 end = slave->offset + slave->size;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200733 struct mtd_erase_region_info *regions = master->eraseregions;
734
735 /* Find the first erase regions which is part of this
736 * partition. */
737 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
738 ;
739 /* The loop searched for the region _behind_ the first one */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200740 if (i > 0)
741 i--;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200742
743 /* Pick biggest erasesize */
744 for (; i < max && regions[i].offset < end; i++) {
Miquel Raynal2a749302018-09-29 12:58:27 +0200745 if (slave->erasesize < regions[i].erasesize)
746 slave->erasesize = regions[i].erasesize;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200747 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200748 WARN_ON(slave->erasesize == 0);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200749 } else {
750 /* Single erase size */
Miquel Raynal2a749302018-09-29 12:58:27 +0200751 slave->erasesize = master->erasesize;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200752 }
753
Miquel Raynal2a749302018-09-29 12:58:27 +0200754 if ((slave->flags & MTD_WRITEABLE) &&
755 mtd_mod_by_eb(slave->offset, slave)) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200756 /* Doesn't start on a boundary of major erase size */
757 /* FIXME: Let it be writable if it is on a boundary of
758 * _minor_ erase size though */
Miquel Raynal2a749302018-09-29 12:58:27 +0200759 slave->flags &= ~MTD_WRITEABLE;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200760 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
761 part->name);
762 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200763 if ((slave->flags & MTD_WRITEABLE) &&
764 mtd_mod_by_eb(slave->size, slave)) {
765 slave->flags &= ~MTD_WRITEABLE;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200766 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
767 part->name);
768 }
769
Miquel Raynal2a749302018-09-29 12:58:27 +0200770 slave->ecclayout = master->ecclayout;
771 slave->ecc_step_size = master->ecc_step_size;
772 slave->ecc_strength = master->ecc_strength;
773 slave->bitflip_threshold = master->bitflip_threshold;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200774
Sergey Lapindfe64e22013-01-14 03:46:50 +0000775 if (master->_block_isbad) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200776 uint64_t offs = 0;
777
Miquel Raynal2a749302018-09-29 12:58:27 +0200778 while (offs < slave->size) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000779 if (mtd_block_isbad(master, offs + slave->offset))
Miquel Raynal2a749302018-09-29 12:58:27 +0200780 slave->ecc_stats.badblocks++;
781 offs += slave->erasesize;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100782 }
783 }
784
Stefan Roese8d2effe2009-05-11 16:03:55 +0200785out_register:
Stefan Roese8d2effe2009-05-11 16:03:55 +0200786 return slave;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100787}
788
Heiko Schocherddf7bcf2014-07-15 16:08:42 +0200789#ifndef __UBOOT__
Heiko Schocherff94bc42014-06-24 10:10:04 +0200790int mtd_add_partition(struct mtd_info *master, const char *name,
791 long long offset, long long length)
792{
793 struct mtd_partition part;
Miquel Raynal2a749302018-09-29 12:58:27 +0200794 struct mtd_info *p, *new;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200795 uint64_t start, end;
796 int ret = 0;
797
798 /* the direct offset is expected */
799 if (offset == MTDPART_OFS_APPEND ||
800 offset == MTDPART_OFS_NXTBLK)
801 return -EINVAL;
802
803 if (length == MTDPART_SIZ_FULL)
804 length = master->size - offset;
805
806 if (length <= 0)
807 return -EINVAL;
808
809 part.name = name;
810 part.size = length;
811 part.offset = offset;
812 part.mask_flags = 0;
813 part.ecclayout = NULL;
814
815 new = allocate_partition(master, &part, -1, offset);
816 if (IS_ERR(new))
817 return PTR_ERR(new);
818
819 start = offset;
820 end = offset + length;
821
822 mutex_lock(&mtd_partitions_mutex);
Miquel Raynal2a749302018-09-29 12:58:27 +0200823 list_for_each_entry(p, &master->partitions, node) {
824 if (start >= p->offset &&
825 (start < (p->offset + p->size)))
826 goto err_inv;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200827
Miquel Raynal2a749302018-09-29 12:58:27 +0200828 if (end >= p->offset &&
829 (end < (p->offset + p->size)))
830 goto err_inv;
831 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200832
Miquel Raynal2a749302018-09-29 12:58:27 +0200833 list_add_tail(&new->node, &master->partitions);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200834 mutex_unlock(&mtd_partitions_mutex);
835
Miquel Raynal2a749302018-09-29 12:58:27 +0200836 add_mtd_device(new);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200837
838 return ret;
839err_inv:
840 mutex_unlock(&mtd_partitions_mutex);
841 free_partition(new);
842 return -EINVAL;
843}
844EXPORT_SYMBOL_GPL(mtd_add_partition);
845
846int mtd_del_partition(struct mtd_info *master, int partno)
847{
Miquel Raynal2a749302018-09-29 12:58:27 +0200848 struct mtd_info *slave, *next;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200849 int ret = -EINVAL;
850
851 mutex_lock(&mtd_partitions_mutex);
Miquel Raynal2a749302018-09-29 12:58:27 +0200852 list_for_each_entry_safe(slave, next, &master->partitions, node)
853 if (slave->index == partno) {
854 ret = del_mtd_device(slave);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200855 if (ret < 0)
856 break;
857
Miquel Raynal2a749302018-09-29 12:58:27 +0200858 list_del(&slave->node);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200859 free_partition(slave);
860 break;
861 }
862 mutex_unlock(&mtd_partitions_mutex);
863
864 return ret;
865}
866EXPORT_SYMBOL_GPL(mtd_del_partition);
Heiko Schocherddf7bcf2014-07-15 16:08:42 +0200867#endif
Heiko Schocherff94bc42014-06-24 10:10:04 +0200868
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100869/*
870 * This function, given a master MTD object and a partition table, creates
871 * and registers slave MTD objects which are bound to the master according to
872 * the partition definitions.
Stefan Roese8d2effe2009-05-11 16:03:55 +0200873 *
874 * We don't register the master, or expect the caller to have done so,
875 * for reasons of data integrity.
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100876 */
877
878int add_mtd_partitions(struct mtd_info *master,
879 const struct mtd_partition *parts,
880 int nbparts)
881{
Miquel Raynal2a749302018-09-29 12:58:27 +0200882 struct mtd_info *slave;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200883 uint64_t cur_offset = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100884 int i;
885
Joe Hershberger147162d2013-04-08 10:32:49 +0000886 debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100887
888 for (i = 0; i < nbparts; i++) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200889 slave = allocate_partition(master, parts + i, i, cur_offset);
890 if (IS_ERR(slave))
891 return PTR_ERR(slave);
892
893 mutex_lock(&mtd_partitions_mutex);
Miquel Raynal2a749302018-09-29 12:58:27 +0200894 list_add_tail(&slave->node, &master->partitions);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200895 mutex_unlock(&mtd_partitions_mutex);
896
Miquel Raynal2a749302018-09-29 12:58:27 +0200897 add_mtd_device(slave);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200898
Miquel Raynal2a749302018-09-29 12:58:27 +0200899 cur_offset = slave->offset + slave->size;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100900 }
901
902 return 0;
903}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200904
Marek Behúndc339bf2021-05-26 14:08:19 +0200905#if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(OF_CONTROL)
906int add_mtd_partitions_of(struct mtd_info *master)
907{
908 ofnode parts, child;
909 int i = 0;
910
911 if (!master->dev)
912 return 0;
913
914 parts = ofnode_find_subnode(mtd_get_ofnode(master), "partitions");
915 if (!ofnode_valid(parts) || !ofnode_is_available(parts) ||
916 !ofnode_device_is_compatible(parts, "fixed-partitions"))
917 return 0;
918
919 ofnode_for_each_subnode(child, parts) {
920 struct mtd_partition part = { 0 };
921 struct mtd_info *slave;
922 fdt_addr_t offset, size;
923
924 if (!ofnode_is_available(child))
925 continue;
926
927 offset = ofnode_get_addr_size_index_notrans(child, 0, &size);
928 if (offset == FDT_ADDR_T_NONE || !size) {
929 debug("Missing partition offset/size on \"%s\" partition\n",
930 master->name);
931 continue;
932 }
933
934 part.name = ofnode_read_string(child, "label");
935 if (!part.name)
936 part.name = ofnode_read_string(child, "name");
937
938 /*
939 * .mask_flags is used to remove flags in allocate_partition(),
940 * so when "read-only" is present, we add MTD_WRITABLE to the
941 * mask, and so MTD_WRITABLE will be removed on partition
942 * allocation
943 */
944 if (ofnode_read_bool(child, "read-only"))
945 part.mask_flags |= MTD_WRITEABLE;
946 if (ofnode_read_bool(child, "lock"))
947 part.mask_flags |= MTD_POWERUP_LOCK;
948
949 part.offset = offset;
950 part.size = size;
951 part.ecclayout = master->ecclayout;
952
953 slave = allocate_partition(master, &part, i++, 0);
954 if (IS_ERR(slave))
955 return PTR_ERR(slave);
956
957 mutex_lock(&mtd_partitions_mutex);
958 list_add_tail(&slave->node, &master->partitions);
959 mutex_unlock(&mtd_partitions_mutex);
960
961 add_mtd_device(slave);
962 }
963
964 return 0;
965}
966#endif /* CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(OF_CONTROL) */
967
Heiko Schocherff94bc42014-06-24 10:10:04 +0200968#ifndef __UBOOT__
969static DEFINE_SPINLOCK(part_parser_lock);
970static LIST_HEAD(part_parsers);
971
972static struct mtd_part_parser *get_partition_parser(const char *name)
973{
974 struct mtd_part_parser *p, *ret = NULL;
975
976 spin_lock(&part_parser_lock);
977
978 list_for_each_entry(p, &part_parsers, list)
979 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
980 ret = p;
981 break;
982 }
983
984 spin_unlock(&part_parser_lock);
985
986 return ret;
987}
988
989#define put_partition_parser(p) do { module_put((p)->owner); } while (0)
990
991void register_mtd_parser(struct mtd_part_parser *p)
992{
993 spin_lock(&part_parser_lock);
994 list_add(&p->list, &part_parsers);
995 spin_unlock(&part_parser_lock);
996}
997EXPORT_SYMBOL_GPL(register_mtd_parser);
998
999void deregister_mtd_parser(struct mtd_part_parser *p)
1000{
1001 spin_lock(&part_parser_lock);
1002 list_del(&p->list);
1003 spin_unlock(&part_parser_lock);
1004}
1005EXPORT_SYMBOL_GPL(deregister_mtd_parser);
1006
1007/*
1008 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
1009 * are changing this array!
1010 */
1011static const char * const default_mtd_part_types[] = {
1012 "cmdlinepart",
1013 "ofpart",
1014 NULL
1015};
1016
1017/**
1018 * parse_mtd_partitions - parse MTD partitions
1019 * @master: the master partition (describes whole MTD device)
1020 * @types: names of partition parsers to try or %NULL
1021 * @pparts: array of partitions found is returned here
1022 * @data: MTD partition parser-specific data
1023 *
1024 * This function tries to find partition on MTD device @master. It uses MTD
1025 * partition parsers, specified in @types. However, if @types is %NULL, then
1026 * the default list of parsers is used. The default list contains only the
1027 * "cmdlinepart" and "ofpart" parsers ATM.
1028 * Note: If there are more then one parser in @types, the kernel only takes the
1029 * partitions parsed out by the first parser.
1030 *
1031 * This function may return:
1032 * o a negative error code in case of failure
1033 * o zero if no partitions were found
1034 * o a positive number of found partitions, in which case on exit @pparts will
1035 * point to an array containing this number of &struct mtd_info objects.
1036 */
1037int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
1038 struct mtd_partition **pparts,
1039 struct mtd_part_parser_data *data)
1040{
1041 struct mtd_part_parser *parser;
1042 int ret = 0;
1043
1044 if (!types)
1045 types = default_mtd_part_types;
1046
1047 for ( ; ret <= 0 && *types; types++) {
1048 parser = get_partition_parser(*types);
1049 if (!parser && !request_module("%s", *types))
1050 parser = get_partition_parser(*types);
1051 if (!parser)
1052 continue;
1053 ret = (*parser->parse_fn)(master, pparts, data);
1054 put_partition_parser(parser);
1055 if (ret > 0) {
1056 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
1057 ret, parser->name, master->name);
1058 break;
1059 }
1060 }
1061 return ret;
1062}
1063#endif
1064
Heiko Schocherff94bc42014-06-24 10:10:04 +02001065/* Returns the size of the entire flash chip */
1066uint64_t mtd_get_device_size(const struct mtd_info *mtd)
1067{
Miquel Raynal2a749302018-09-29 12:58:27 +02001068 if (mtd_is_partition(mtd))
1069 return mtd->parent->size;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001070
Miquel Raynal2a749302018-09-29 12:58:27 +02001071 return mtd->size;
Heiko Schocherff94bc42014-06-24 10:10:04 +02001072}
1073EXPORT_SYMBOL_GPL(mtd_get_device_size);