blob: 56acdbf65ba998c9eed092222fe2715d00497c42 [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 Glass61b29b82020-02-03 07:36:15 -070012#include <dm/devres.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020013#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/list.h>
18#include <linux/kmod.h>
19#endif
20
Kyungmin Parke29c22f2008-11-19 16:20:36 +010021#include <common.h>
22#include <malloc.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090023#include <linux/errno.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020024#include <linux/compat.h>
25#include <ubi_uboot.h>
Kyungmin Parke29c22f2008-11-19 16:20:36 +010026
Kyungmin Parke29c22f2008-11-19 16:20:36 +010027#include <linux/mtd/mtd.h>
28#include <linux/mtd/partitions.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020029#include <linux/err.h>
Miquel Raynal21cc1fb2018-09-29 12:58:25 +020030#include <linux/sizes.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020031
32#include "mtdcore.h"
Kyungmin Parke29c22f2008-11-19 16:20:36 +010033
Heiko Schocherff94bc42014-06-24 10:10:04 +020034#ifndef __UBOOT__
35static DEFINE_MUTEX(mtd_partitions_mutex);
36#else
37DEFINE_MUTEX(mtd_partitions_mutex);
38#endif
Kyungmin Parke29c22f2008-11-19 16:20:36 +010039
Heiko Schocherff94bc42014-06-24 10:10:04 +020040#ifdef __UBOOT__
41/* from mm/util.c */
42
43/**
44 * kstrdup - allocate space for and copy an existing string
45 * @s: the string to duplicate
46 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
47 */
48char *kstrdup(const char *s, gfp_t gfp)
49{
50 size_t len;
51 char *buf;
52
53 if (!s)
54 return NULL;
55
56 len = strlen(s) + 1;
57 buf = kmalloc(len, gfp);
58 if (buf)
59 memcpy(buf, s, len);
60 return buf;
61}
62#endif
63
Miquel Raynal21cc1fb2018-09-29 12:58:25 +020064#define MTD_SIZE_REMAINING (~0LLU)
65#define MTD_OFFSET_NOT_SPECIFIED (~0LLU)
66
Boris Brezillon4a5594f2018-12-02 10:54:30 +010067bool mtd_partitions_used(struct mtd_info *master)
68{
69 struct mtd_info *slave;
70
71 list_for_each_entry(slave, &master->partitions, node) {
72 if (slave->usecount)
73 return true;
74 }
75
76 return false;
77}
78
Miquel Raynal21cc1fb2018-09-29 12:58:25 +020079/**
80 * mtd_parse_partition - Parse @mtdparts partition definition, fill @partition
81 * with it and update the @mtdparts string pointer.
82 *
83 * The partition name is allocated and must be freed by the caller.
84 *
85 * This function is widely inspired from part_parse (mtdparts.c).
86 *
87 * @mtdparts: String describing the partition with mtdparts command syntax
88 * @partition: MTD partition structure to fill
89 *
90 * @return 0 on success, an error otherwise.
91 */
92static int mtd_parse_partition(const char **_mtdparts,
93 struct mtd_partition *partition)
94{
95 const char *mtdparts = *_mtdparts;
96 const char *name = NULL;
97 int name_len;
98 char *buf;
99
100 /* Ensure the partition structure is empty */
101 memset(partition, 0, sizeof(struct mtd_partition));
102
103 /* Fetch the partition size */
104 if (*mtdparts == '-') {
105 /* Assign all remaining space to this partition */
106 partition->size = MTD_SIZE_REMAINING;
107 mtdparts++;
108 } else {
109 partition->size = ustrtoull(mtdparts, (char **)&mtdparts, 0);
110 if (partition->size < SZ_4K) {
111 printf("Minimum partition size 4kiB, %lldB requested\n",
112 partition->size);
113 return -EINVAL;
114 }
115 }
116
117 /* Check for the offset */
118 partition->offset = MTD_OFFSET_NOT_SPECIFIED;
119 if (*mtdparts == '@') {
120 mtdparts++;
121 partition->offset = ustrtoull(mtdparts, (char **)&mtdparts, 0);
122 }
123
124 /* Now look for the name */
125 if (*mtdparts == '(') {
126 name = ++mtdparts;
127 mtdparts = strchr(name, ')');
128 if (!mtdparts) {
129 printf("No closing ')' found in partition name\n");
130 return -EINVAL;
131 }
132 name_len = mtdparts - name + 1;
133 if ((name_len - 1) == 0) {
134 printf("Empty partition name\n");
135 return -EINVAL;
136 }
137 mtdparts++;
138 } else {
139 /* Name will be of the form size@offset */
140 name_len = 22;
141 }
142
143 /* Check if the partition is read-only */
144 if (strncmp(mtdparts, "ro", 2) == 0) {
145 partition->mask_flags |= MTD_WRITEABLE;
146 mtdparts += 2;
147 }
148
149 /* Check for a potential next partition definition */
150 if (*mtdparts == ',') {
151 if (partition->size == MTD_SIZE_REMAINING) {
152 printf("No partitions allowed after a fill-up\n");
153 return -EINVAL;
154 }
155 ++mtdparts;
156 } else if ((*mtdparts == ';') || (*mtdparts == '\0')) {
157 /* NOP */
158 } else {
159 printf("Unexpected character '%c' in mtdparts\n", *mtdparts);
160 return -EINVAL;
161 }
162
163 /*
164 * Allocate a buffer for the name and either copy the provided name or
165 * auto-generate it with the form 'size@offset'.
166 */
167 buf = malloc(name_len);
168 if (!buf)
169 return -ENOMEM;
170
171 if (name)
172 strncpy(buf, name, name_len - 1);
173 else
174 snprintf(buf, name_len, "0x%08llx@0x%08llx",
175 partition->size, partition->offset);
176
177 buf[name_len - 1] = '\0';
178 partition->name = buf;
179
180 *_mtdparts = mtdparts;
181
182 return 0;
183}
184
185/**
186 * mtd_parse_partitions - Create a partition array from an mtdparts definition
187 *
188 * Stateless function that takes a @parent MTD device, a string @_mtdparts
189 * describing the partitions (with the "mtdparts" command syntax) and creates
190 * the corresponding MTD partition structure array @_parts. Both the name and
191 * the structure partition itself must be freed freed, the caller may use
192 * @mtd_free_parsed_partitions() for this purpose.
193 *
194 * @parent: MTD device which contains the partitions
195 * @_mtdparts: Pointer to a string describing the partitions with "mtdparts"
196 * command syntax.
197 * @_parts: Allocated array containing the partitions, must be freed by the
198 * caller.
199 * @_nparts: Size of @_parts array.
200 *
201 * @return 0 on success, an error otherwise.
202 */
203int mtd_parse_partitions(struct mtd_info *parent, const char **_mtdparts,
204 struct mtd_partition **_parts, int *_nparts)
205{
206 struct mtd_partition partition = {}, *parts;
207 const char *mtdparts = *_mtdparts;
208 int cur_off = 0, cur_sz = 0;
209 int nparts = 0;
210 int ret, idx;
211 u64 sz;
212
213 /* First, iterate over the partitions until we know their number */
214 while (mtdparts[0] != '\0' && mtdparts[0] != ';') {
215 ret = mtd_parse_partition(&mtdparts, &partition);
216 if (ret)
217 return ret;
218
219 free((char *)partition.name);
220 nparts++;
221 }
222
223 /* Allocate an array of partitions to give back to the caller */
224 parts = malloc(sizeof(*parts) * nparts);
225 if (!parts) {
226 printf("Not enough space to save partitions meta-data\n");
227 return -ENOMEM;
228 }
229
230 /* Iterate again over each partition to save the data in our array */
231 for (idx = 0; idx < nparts; idx++) {
232 ret = mtd_parse_partition(_mtdparts, &parts[idx]);
233 if (ret)
234 return ret;
235
236 if (parts[idx].size == MTD_SIZE_REMAINING)
237 parts[idx].size = parent->size - cur_sz;
238 cur_sz += parts[idx].size;
239
240 sz = parts[idx].size;
241 if (sz < parent->writesize || do_div(sz, parent->writesize)) {
242 printf("Partition size must be a multiple of %d\n",
243 parent->writesize);
244 return -EINVAL;
245 }
246
247 if (parts[idx].offset == MTD_OFFSET_NOT_SPECIFIED)
248 parts[idx].offset = cur_off;
249 cur_off += parts[idx].size;
250
251 parts[idx].ecclayout = parent->ecclayout;
252 }
253
254 /* Offset by one mtdparts to point to the next device if any */
255 if (*_mtdparts[0] == ';')
256 (*_mtdparts)++;
257
258 *_parts = parts;
259 *_nparts = nparts;
260
261 return 0;
262}
263
264/**
265 * mtd_free_parsed_partitions - Free dynamically allocated partitions
266 *
267 * Each successful call to @mtd_parse_partitions must be followed by a call to
268 * @mtd_free_parsed_partitions to free any allocated array during the parsing
269 * process.
270 *
271 * @parts: Array containing the partitions that will be freed.
272 * @nparts: Size of @parts array.
273 */
274void mtd_free_parsed_partitions(struct mtd_partition *parts,
275 unsigned int nparts)
276{
277 int i;
278
279 for (i = 0; i < nparts; i++)
280 free((char *)parts[i].name);
281
282 free(parts);
283}
284
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100285/*
286 * MTD methods which simply translate the effective address and pass through
287 * to the _real_ device.
288 */
289
Stefan Roese8d2effe2009-05-11 16:03:55 +0200290static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
291 size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100292{
Stefan Roese8d2effe2009-05-11 16:03:55 +0200293 struct mtd_ecc_stats stats;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100294 int res;
295
Miquel Raynal2a749302018-09-29 12:58:27 +0200296 stats = mtd->parent->ecc_stats;
297 res = mtd->parent->_read(mtd->parent, from + mtd->offset, len,
298 retlen, buf);
Paul Burton40462e52013-09-04 15:16:56 +0100299 if (unlikely(mtd_is_eccerr(res)))
300 mtd->ecc_stats.failed +=
Miquel Raynal2a749302018-09-29 12:58:27 +0200301 mtd->parent->ecc_stats.failed - stats.failed;
Paul Burton40462e52013-09-04 15:16:56 +0100302 else
303 mtd->ecc_stats.corrected +=
Miquel Raynal2a749302018-09-29 12:58:27 +0200304 mtd->parent->ecc_stats.corrected - stats.corrected;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100305 return res;
306}
307
Heiko Schocherff94bc42014-06-24 10:10:04 +0200308#ifndef __UBOOT__
309static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
310 size_t *retlen, void **virt, resource_size_t *phys)
311{
Miquel Raynal2a749302018-09-29 12:58:27 +0200312 return mtd->parent->_point(mtd->parent, from + mtd->offset, len,
313 retlen, virt, phys);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200314}
315
316static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
317{
Miquel Raynal2a749302018-09-29 12:58:27 +0200318 return mtd->parent->_unpoint(mtd->parent, from + mtd->offset, len);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200319}
320#endif
321
322static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
323 unsigned long len,
324 unsigned long offset,
325 unsigned long flags)
326{
Miquel Raynal2a749302018-09-29 12:58:27 +0200327 offset += mtd->offset;
328 return mtd->parent->_get_unmapped_area(mtd->parent, len, offset, flags);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200329}
330
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100331static int part_read_oob(struct mtd_info *mtd, loff_t from,
Stefan Roese8d2effe2009-05-11 16:03:55 +0200332 struct mtd_oob_ops *ops)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100333{
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100334 int res;
335
336 if (from >= mtd->size)
337 return -EINVAL;
338 if (ops->datbuf && from + ops->len > mtd->size)
339 return -EINVAL;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100340
Heiko Schocherff94bc42014-06-24 10:10:04 +0200341 /*
342 * If OOB is also requested, make sure that we do not read past the end
343 * of this partition.
344 */
345 if (ops->oobbuf) {
346 size_t len, pages;
347
348 if (ops->mode == MTD_OPS_AUTO_OOB)
349 len = mtd->oobavail;
350 else
351 len = mtd->oobsize;
352 pages = mtd_div_by_ws(mtd->size, mtd);
353 pages -= mtd_div_by_ws(from, mtd);
354 if (ops->ooboffs + ops->ooblen > pages * len)
355 return -EINVAL;
356 }
357
Miquel Raynal2a749302018-09-29 12:58:27 +0200358 res = mtd->parent->_read_oob(mtd->parent, from + mtd->offset, ops);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100359 if (unlikely(res)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000360 if (mtd_is_bitflip(res))
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100361 mtd->ecc_stats.corrected++;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000362 if (mtd_is_eccerr(res))
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100363 mtd->ecc_stats.failed++;
364 }
365 return res;
366}
367
Stefan Roese8d2effe2009-05-11 16:03:55 +0200368static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
369 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100370{
Miquel Raynal2a749302018-09-29 12:58:27 +0200371 return mtd->parent->_read_user_prot_reg(mtd->parent, from, len,
372 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100373}
374
Heiko Schocher4e67c572014-07-15 16:08:43 +0200375static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
376 size_t *retlen, struct otp_info *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100377{
Miquel Raynal2a749302018-09-29 12:58:27 +0200378 return mtd->parent->_get_user_prot_info(mtd->parent, len, retlen,
379 buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100380}
381
Stefan Roese8d2effe2009-05-11 16:03:55 +0200382static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
383 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100384{
Miquel Raynal2a749302018-09-29 12:58:27 +0200385 return mtd->parent->_read_fact_prot_reg(mtd->parent, from, len,
386 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100387}
388
Heiko Schocher4e67c572014-07-15 16:08:43 +0200389static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
390 size_t *retlen, struct otp_info *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100391{
Miquel Raynal2a749302018-09-29 12:58:27 +0200392 return mtd->parent->_get_fact_prot_info(mtd->parent, len, retlen,
393 buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100394}
395
Stefan Roese8d2effe2009-05-11 16:03:55 +0200396static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
397 size_t *retlen, const u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100398{
Miquel Raynal2a749302018-09-29 12:58:27 +0200399 return mtd->parent->_write(mtd->parent, to + mtd->offset, len,
400 retlen, buf);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200401}
402
403static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
404 size_t *retlen, const u_char *buf)
405{
Miquel Raynal2a749302018-09-29 12:58:27 +0200406 return mtd->parent->_panic_write(mtd->parent, to + mtd->offset, len,
407 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100408}
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100409
410static int part_write_oob(struct mtd_info *mtd, loff_t to,
Stefan Roese8d2effe2009-05-11 16:03:55 +0200411 struct mtd_oob_ops *ops)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100412{
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100413 if (to >= mtd->size)
414 return -EINVAL;
415 if (ops->datbuf && to + ops->len > mtd->size)
416 return -EINVAL;
Miquel Raynal2a749302018-09-29 12:58:27 +0200417 return mtd->parent->_write_oob(mtd->parent, to + mtd->offset, ops);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100418}
419
Stefan Roese8d2effe2009-05-11 16:03:55 +0200420static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
421 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100422{
Miquel Raynal2a749302018-09-29 12:58:27 +0200423 return mtd->parent->_write_user_prot_reg(mtd->parent, from, len,
424 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100425}
426
Stefan Roese8d2effe2009-05-11 16:03:55 +0200427static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
428 size_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100429{
Miquel Raynal2a749302018-09-29 12:58:27 +0200430 return mtd->parent->_lock_user_prot_reg(mtd->parent, from, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100431}
432
Heiko Schocherff94bc42014-06-24 10:10:04 +0200433#ifndef __UBOOT__
434static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
435 unsigned long count, loff_t to, size_t *retlen)
436{
Miquel Raynal2a749302018-09-29 12:58:27 +0200437 return mtd->parent->_writev(mtd->parent, vecs, count,
438 to + mtd->offset, retlen);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200439}
440#endif
441
Stefan Roese8d2effe2009-05-11 16:03:55 +0200442static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100443{
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100444 int ret;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000445
Miquel Raynal2a749302018-09-29 12:58:27 +0200446 instr->addr += mtd->offset;
447 ret = mtd->parent->_erase(mtd->parent, instr);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100448 if (ret) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200449 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Miquel Raynal2a749302018-09-29 12:58:27 +0200450 instr->fail_addr -= mtd->offset;
451 instr->addr -= mtd->offset;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100452 }
453 return ret;
454}
455
456void mtd_erase_callback(struct erase_info *instr)
457{
Sergey Lapindfe64e22013-01-14 03:46:50 +0000458 if (instr->mtd->_erase == part_erase) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200459 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Miquel Raynal2a749302018-09-29 12:58:27 +0200460 instr->fail_addr -= instr->mtd->offset;
461 instr->addr -= instr->mtd->offset;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100462 }
463 if (instr->callback)
464 instr->callback(instr);
465}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200466EXPORT_SYMBOL_GPL(mtd_erase_callback);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100467
Stefan Roese8d2effe2009-05-11 16:03:55 +0200468static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100469{
Miquel Raynal2a749302018-09-29 12:58:27 +0200470 return mtd->parent->_lock(mtd->parent, ofs + mtd->offset, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100471}
472
Stefan Roese8d2effe2009-05-11 16:03:55 +0200473static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100474{
Miquel Raynal2a749302018-09-29 12:58:27 +0200475 return mtd->parent->_unlock(mtd->parent, ofs + mtd->offset, len);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200476}
477
478static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
479{
Miquel Raynal2a749302018-09-29 12:58:27 +0200480 return mtd->parent->_is_locked(mtd->parent, ofs + mtd->offset, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100481}
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100482
483static void part_sync(struct mtd_info *mtd)
484{
Miquel Raynal2a749302018-09-29 12:58:27 +0200485 mtd->parent->_sync(mtd->parent);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100486}
487
Heiko Schocherff94bc42014-06-24 10:10:04 +0200488#ifndef __UBOOT__
489static int part_suspend(struct mtd_info *mtd)
490{
Miquel Raynal2a749302018-09-29 12:58:27 +0200491 return mtd->parent->_suspend(mtd->parent);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200492}
493
494static void part_resume(struct mtd_info *mtd)
495{
Miquel Raynal2a749302018-09-29 12:58:27 +0200496 mtd->parent->_resume(mtd->parent);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200497}
498#endif
499
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300500static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
501{
Miquel Raynal2a749302018-09-29 12:58:27 +0200502 ofs += mtd->offset;
503 return mtd->parent->_block_isreserved(mtd->parent, ofs);
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300504}
505
Stefan Roese8d2effe2009-05-11 16:03:55 +0200506static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100507{
Miquel Raynal2a749302018-09-29 12:58:27 +0200508 ofs += mtd->offset;
509 return mtd->parent->_block_isbad(mtd->parent, ofs);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100510}
511
Stefan Roese8d2effe2009-05-11 16:03:55 +0200512static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100513{
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100514 int res;
515
Miquel Raynal2a749302018-09-29 12:58:27 +0200516 ofs += mtd->offset;
517 res = mtd->parent->_block_markbad(mtd->parent, ofs);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100518 if (!res)
519 mtd->ecc_stats.badblocks++;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100520 return res;
521}
522
Miquel Raynal2a749302018-09-29 12:58:27 +0200523static inline void free_partition(struct mtd_info *p)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200524{
Miquel Raynal2a749302018-09-29 12:58:27 +0200525 kfree(p->name);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200526 kfree(p);
527}
528
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100529/*
530 * This function unregisters and destroy all slave MTD objects which are
Miquel Raynal2a749302018-09-29 12:58:27 +0200531 * attached to the given master MTD object, recursively.
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100532 */
Miquel Raynal2a749302018-09-29 12:58:27 +0200533static int do_del_mtd_partitions(struct mtd_info *master)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100534{
Miquel Raynal2a749302018-09-29 12:58:27 +0200535 struct mtd_info *slave, *next;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200536 int ret, err = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100537
Miquel Raynal2a749302018-09-29 12:58:27 +0200538 list_for_each_entry_safe(slave, next, &master->partitions, node) {
539 if (mtd_has_partitions(slave))
540 del_mtd_partitions(slave);
Miquel Raynalb0036f72018-08-16 17:30:19 +0200541
Miquel Raynal2a749302018-09-29 12:58:27 +0200542 debug("Deleting %s MTD partition\n", slave->name);
543 ret = del_mtd_device(slave);
544 if (ret < 0) {
545 printf("Error when deleting partition \"%s\" (%d)\n",
546 slave->name, ret);
547 err = ret;
548 continue;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200549 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200550
551 list_del(&slave->node);
552 free_partition(slave);
553 }
Stefan Roese8d2effe2009-05-11 16:03:55 +0200554
Heiko Schocherff94bc42014-06-24 10:10:04 +0200555 return err;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200556}
557
Miquel Raynal2a749302018-09-29 12:58:27 +0200558int del_mtd_partitions(struct mtd_info *master)
Stefan Roese8d2effe2009-05-11 16:03:55 +0200559{
Miquel Raynal2a749302018-09-29 12:58:27 +0200560 int ret;
561
562 debug("Deleting MTD partitions on \"%s\":\n", master->name);
563
564 mutex_lock(&mtd_partitions_mutex);
565 ret = do_del_mtd_partitions(master);
566 mutex_unlock(&mtd_partitions_mutex);
567
568 return ret;
569}
570
571static struct mtd_info *allocate_partition(struct mtd_info *master,
572 const struct mtd_partition *part,
573 int partno, uint64_t cur_offset)
574{
575 struct mtd_info *slave;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200576 char *name;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200577
578 /* allocate the partition structure */
579 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200580 name = kstrdup(part->name, GFP_KERNEL);
581 if (!name || !slave) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200582 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200583 master->name);
584 kfree(name);
585 kfree(slave);
586 return ERR_PTR(-ENOMEM);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200587 }
Stefan Roese8d2effe2009-05-11 16:03:55 +0200588
589 /* set up the MTD object for this partition */
Miquel Raynal2a749302018-09-29 12:58:27 +0200590 slave->type = master->type;
591 slave->flags = master->flags & ~part->mask_flags;
592 slave->size = part->size;
593 slave->writesize = master->writesize;
594 slave->writebufsize = master->writebufsize;
595 slave->oobsize = master->oobsize;
596 slave->oobavail = master->oobavail;
597 slave->subpage_sft = master->subpage_sft;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200598
Miquel Raynal2a749302018-09-29 12:58:27 +0200599 slave->name = name;
600 slave->owner = master->owner;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200601#ifndef __UBOOT__
Miquel Raynal2a749302018-09-29 12:58:27 +0200602 slave->backing_dev_info = master->backing_dev_info;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200603
604 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
605 * to have the same data be in two different partitions.
606 */
Miquel Raynal2a749302018-09-29 12:58:27 +0200607 slave->dev.parent = master->dev.parent;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200608#endif
Stefan Roese8d2effe2009-05-11 16:03:55 +0200609
Boris Brezillon596cf082018-08-16 17:29:59 +0200610 if (master->_read)
Miquel Raynal2a749302018-09-29 12:58:27 +0200611 slave->_read = part_read;
Boris Brezillon596cf082018-08-16 17:29:59 +0200612 if (master->_write)
Miquel Raynal2a749302018-09-29 12:58:27 +0200613 slave->_write = part_write;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200614
Heiko Schocherff94bc42014-06-24 10:10:04 +0200615 if (master->_panic_write)
Miquel Raynal2a749302018-09-29 12:58:27 +0200616 slave->_panic_write = part_panic_write;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200617
618#ifndef __UBOOT__
619 if (master->_point && master->_unpoint) {
Miquel Raynal2a749302018-09-29 12:58:27 +0200620 slave->_point = part_point;
621 slave->_unpoint = part_unpoint;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200622 }
623#endif
624
625 if (master->_get_unmapped_area)
Miquel Raynal2a749302018-09-29 12:58:27 +0200626 slave->_get_unmapped_area = part_get_unmapped_area;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000627 if (master->_read_oob)
Miquel Raynal2a749302018-09-29 12:58:27 +0200628 slave->_read_oob = part_read_oob;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000629 if (master->_write_oob)
Miquel Raynal2a749302018-09-29 12:58:27 +0200630 slave->_write_oob = part_write_oob;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000631 if (master->_read_user_prot_reg)
Miquel Raynal2a749302018-09-29 12:58:27 +0200632 slave->_read_user_prot_reg = part_read_user_prot_reg;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000633 if (master->_read_fact_prot_reg)
Miquel Raynal2a749302018-09-29 12:58:27 +0200634 slave->_read_fact_prot_reg = part_read_fact_prot_reg;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000635 if (master->_write_user_prot_reg)
Miquel Raynal2a749302018-09-29 12:58:27 +0200636 slave->_write_user_prot_reg = part_write_user_prot_reg;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000637 if (master->_lock_user_prot_reg)
Miquel Raynal2a749302018-09-29 12:58:27 +0200638 slave->_lock_user_prot_reg = part_lock_user_prot_reg;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000639 if (master->_get_user_prot_info)
Miquel Raynal2a749302018-09-29 12:58:27 +0200640 slave->_get_user_prot_info = part_get_user_prot_info;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000641 if (master->_get_fact_prot_info)
Miquel Raynal2a749302018-09-29 12:58:27 +0200642 slave->_get_fact_prot_info = part_get_fact_prot_info;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000643 if (master->_sync)
Miquel Raynal2a749302018-09-29 12:58:27 +0200644 slave->_sync = part_sync;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200645#ifndef __UBOOT__
646 if (!partno && !master->dev.class && master->_suspend &&
647 master->_resume) {
Miquel Raynal2a749302018-09-29 12:58:27 +0200648 slave->_suspend = part_suspend;
649 slave->_resume = part_resume;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200650 }
651 if (master->_writev)
Miquel Raynal2a749302018-09-29 12:58:27 +0200652 slave->_writev = part_writev;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200653#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +0000654 if (master->_lock)
Miquel Raynal2a749302018-09-29 12:58:27 +0200655 slave->_lock = part_lock;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000656 if (master->_unlock)
Miquel Raynal2a749302018-09-29 12:58:27 +0200657 slave->_unlock = part_unlock;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200658 if (master->_is_locked)
Miquel Raynal2a749302018-09-29 12:58:27 +0200659 slave->_is_locked = part_is_locked;
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300660 if (master->_block_isreserved)
Miquel Raynal2a749302018-09-29 12:58:27 +0200661 slave->_block_isreserved = part_block_isreserved;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000662 if (master->_block_isbad)
Miquel Raynal2a749302018-09-29 12:58:27 +0200663 slave->_block_isbad = part_block_isbad;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000664 if (master->_block_markbad)
Miquel Raynal2a749302018-09-29 12:58:27 +0200665 slave->_block_markbad = part_block_markbad;
666 slave->_erase = part_erase;
667 slave->parent = master;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200668 slave->offset = part->offset;
Miquel Raynal2a749302018-09-29 12:58:27 +0200669 INIT_LIST_HEAD(&slave->partitions);
670 INIT_LIST_HEAD(&slave->node);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200671
672 if (slave->offset == MTDPART_OFS_APPEND)
673 slave->offset = cur_offset;
674 if (slave->offset == MTDPART_OFS_NXTBLK) {
675 slave->offset = cur_offset;
676 if (mtd_mod_by_eb(cur_offset, master) != 0) {
677 /* Round up to next erasesize */
678 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200679 debug("Moving partition %d: "
680 "0x%012llx -> 0x%012llx\n", partno,
681 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
682 }
683 }
684 if (slave->offset == MTDPART_OFS_RETAIN) {
685 slave->offset = cur_offset;
Miquel Raynal2a749302018-09-29 12:58:27 +0200686 if (master->size - slave->offset >= slave->size) {
687 slave->size = master->size - slave->offset
688 - slave->size;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200689 } else {
690 debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
691 part->name, master->size - slave->offset,
Miquel Raynal2a749302018-09-29 12:58:27 +0200692 slave->size);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200693 /* register to preserve ordering */
694 goto out_register;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200695 }
696 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200697 if (slave->size == MTDPART_SIZ_FULL)
698 slave->size = master->size - slave->offset;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200699
Heiko Schocherff94bc42014-06-24 10:10:04 +0200700 debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
Miquel Raynal2a749302018-09-29 12:58:27 +0200701 (unsigned long long)(slave->offset + slave->size), slave->name);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200702
703 /* let's do some sanity checks */
704 if (slave->offset >= master->size) {
705 /* let's register it anyway to preserve ordering */
706 slave->offset = 0;
Miquel Raynal2a749302018-09-29 12:58:27 +0200707 slave->size = 0;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200708 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
709 part->name);
710 goto out_register;
711 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200712 if (slave->offset + slave->size > master->size) {
713 slave->size = master->size - slave->offset;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200714 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 +0200715 part->name, master->name, slave->size);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200716 }
717 if (master->numeraseregions > 1) {
718 /* Deal with variable erase size stuff */
719 int i, max = master->numeraseregions;
Miquel Raynal2a749302018-09-29 12:58:27 +0200720 u64 end = slave->offset + slave->size;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200721 struct mtd_erase_region_info *regions = master->eraseregions;
722
723 /* Find the first erase regions which is part of this
724 * partition. */
725 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
726 ;
727 /* The loop searched for the region _behind_ the first one */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200728 if (i > 0)
729 i--;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200730
731 /* Pick biggest erasesize */
732 for (; i < max && regions[i].offset < end; i++) {
Miquel Raynal2a749302018-09-29 12:58:27 +0200733 if (slave->erasesize < regions[i].erasesize)
734 slave->erasesize = regions[i].erasesize;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200735 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200736 WARN_ON(slave->erasesize == 0);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200737 } else {
738 /* Single erase size */
Miquel Raynal2a749302018-09-29 12:58:27 +0200739 slave->erasesize = master->erasesize;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200740 }
741
Miquel Raynal2a749302018-09-29 12:58:27 +0200742 if ((slave->flags & MTD_WRITEABLE) &&
743 mtd_mod_by_eb(slave->offset, slave)) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200744 /* Doesn't start on a boundary of major erase size */
745 /* FIXME: Let it be writable if it is on a boundary of
746 * _minor_ erase size though */
Miquel Raynal2a749302018-09-29 12:58:27 +0200747 slave->flags &= ~MTD_WRITEABLE;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200748 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
749 part->name);
750 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200751 if ((slave->flags & MTD_WRITEABLE) &&
752 mtd_mod_by_eb(slave->size, slave)) {
753 slave->flags &= ~MTD_WRITEABLE;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200754 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
755 part->name);
756 }
757
Miquel Raynal2a749302018-09-29 12:58:27 +0200758 slave->ecclayout = master->ecclayout;
759 slave->ecc_step_size = master->ecc_step_size;
760 slave->ecc_strength = master->ecc_strength;
761 slave->bitflip_threshold = master->bitflip_threshold;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200762
Sergey Lapindfe64e22013-01-14 03:46:50 +0000763 if (master->_block_isbad) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200764 uint64_t offs = 0;
765
Miquel Raynal2a749302018-09-29 12:58:27 +0200766 while (offs < slave->size) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000767 if (mtd_block_isbad(master, offs + slave->offset))
Miquel Raynal2a749302018-09-29 12:58:27 +0200768 slave->ecc_stats.badblocks++;
769 offs += slave->erasesize;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100770 }
771 }
772
Stefan Roese8d2effe2009-05-11 16:03:55 +0200773out_register:
Stefan Roese8d2effe2009-05-11 16:03:55 +0200774 return slave;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100775}
776
Heiko Schocherddf7bcf2014-07-15 16:08:42 +0200777#ifndef __UBOOT__
Heiko Schocherff94bc42014-06-24 10:10:04 +0200778int mtd_add_partition(struct mtd_info *master, const char *name,
779 long long offset, long long length)
780{
781 struct mtd_partition part;
Miquel Raynal2a749302018-09-29 12:58:27 +0200782 struct mtd_info *p, *new;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200783 uint64_t start, end;
784 int ret = 0;
785
786 /* the direct offset is expected */
787 if (offset == MTDPART_OFS_APPEND ||
788 offset == MTDPART_OFS_NXTBLK)
789 return -EINVAL;
790
791 if (length == MTDPART_SIZ_FULL)
792 length = master->size - offset;
793
794 if (length <= 0)
795 return -EINVAL;
796
797 part.name = name;
798 part.size = length;
799 part.offset = offset;
800 part.mask_flags = 0;
801 part.ecclayout = NULL;
802
803 new = allocate_partition(master, &part, -1, offset);
804 if (IS_ERR(new))
805 return PTR_ERR(new);
806
807 start = offset;
808 end = offset + length;
809
810 mutex_lock(&mtd_partitions_mutex);
Miquel Raynal2a749302018-09-29 12:58:27 +0200811 list_for_each_entry(p, &master->partitions, node) {
812 if (start >= p->offset &&
813 (start < (p->offset + p->size)))
814 goto err_inv;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200815
Miquel Raynal2a749302018-09-29 12:58:27 +0200816 if (end >= p->offset &&
817 (end < (p->offset + p->size)))
818 goto err_inv;
819 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200820
Miquel Raynal2a749302018-09-29 12:58:27 +0200821 list_add_tail(&new->node, &master->partitions);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200822 mutex_unlock(&mtd_partitions_mutex);
823
Miquel Raynal2a749302018-09-29 12:58:27 +0200824 add_mtd_device(new);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200825
826 return ret;
827err_inv:
828 mutex_unlock(&mtd_partitions_mutex);
829 free_partition(new);
830 return -EINVAL;
831}
832EXPORT_SYMBOL_GPL(mtd_add_partition);
833
834int mtd_del_partition(struct mtd_info *master, int partno)
835{
Miquel Raynal2a749302018-09-29 12:58:27 +0200836 struct mtd_info *slave, *next;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200837 int ret = -EINVAL;
838
839 mutex_lock(&mtd_partitions_mutex);
Miquel Raynal2a749302018-09-29 12:58:27 +0200840 list_for_each_entry_safe(slave, next, &master->partitions, node)
841 if (slave->index == partno) {
842 ret = del_mtd_device(slave);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200843 if (ret < 0)
844 break;
845
Miquel Raynal2a749302018-09-29 12:58:27 +0200846 list_del(&slave->node);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200847 free_partition(slave);
848 break;
849 }
850 mutex_unlock(&mtd_partitions_mutex);
851
852 return ret;
853}
854EXPORT_SYMBOL_GPL(mtd_del_partition);
Heiko Schocherddf7bcf2014-07-15 16:08:42 +0200855#endif
Heiko Schocherff94bc42014-06-24 10:10:04 +0200856
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100857/*
858 * This function, given a master MTD object and a partition table, creates
859 * and registers slave MTD objects which are bound to the master according to
860 * the partition definitions.
Stefan Roese8d2effe2009-05-11 16:03:55 +0200861 *
862 * We don't register the master, or expect the caller to have done so,
863 * for reasons of data integrity.
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100864 */
865
866int add_mtd_partitions(struct mtd_info *master,
867 const struct mtd_partition *parts,
868 int nbparts)
869{
Miquel Raynal2a749302018-09-29 12:58:27 +0200870 struct mtd_info *slave;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200871 uint64_t cur_offset = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100872 int i;
873
Joe Hershberger147162d2013-04-08 10:32:49 +0000874 debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100875
876 for (i = 0; i < nbparts; i++) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200877 slave = allocate_partition(master, parts + i, i, cur_offset);
878 if (IS_ERR(slave))
879 return PTR_ERR(slave);
880
881 mutex_lock(&mtd_partitions_mutex);
Miquel Raynal2a749302018-09-29 12:58:27 +0200882 list_add_tail(&slave->node, &master->partitions);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200883 mutex_unlock(&mtd_partitions_mutex);
884
Miquel Raynal2a749302018-09-29 12:58:27 +0200885 add_mtd_device(slave);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200886
Miquel Raynal2a749302018-09-29 12:58:27 +0200887 cur_offset = slave->offset + slave->size;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100888 }
889
890 return 0;
891}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200892
893#ifndef __UBOOT__
894static DEFINE_SPINLOCK(part_parser_lock);
895static LIST_HEAD(part_parsers);
896
897static struct mtd_part_parser *get_partition_parser(const char *name)
898{
899 struct mtd_part_parser *p, *ret = NULL;
900
901 spin_lock(&part_parser_lock);
902
903 list_for_each_entry(p, &part_parsers, list)
904 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
905 ret = p;
906 break;
907 }
908
909 spin_unlock(&part_parser_lock);
910
911 return ret;
912}
913
914#define put_partition_parser(p) do { module_put((p)->owner); } while (0)
915
916void register_mtd_parser(struct mtd_part_parser *p)
917{
918 spin_lock(&part_parser_lock);
919 list_add(&p->list, &part_parsers);
920 spin_unlock(&part_parser_lock);
921}
922EXPORT_SYMBOL_GPL(register_mtd_parser);
923
924void deregister_mtd_parser(struct mtd_part_parser *p)
925{
926 spin_lock(&part_parser_lock);
927 list_del(&p->list);
928 spin_unlock(&part_parser_lock);
929}
930EXPORT_SYMBOL_GPL(deregister_mtd_parser);
931
932/*
933 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
934 * are changing this array!
935 */
936static const char * const default_mtd_part_types[] = {
937 "cmdlinepart",
938 "ofpart",
939 NULL
940};
941
942/**
943 * parse_mtd_partitions - parse MTD partitions
944 * @master: the master partition (describes whole MTD device)
945 * @types: names of partition parsers to try or %NULL
946 * @pparts: array of partitions found is returned here
947 * @data: MTD partition parser-specific data
948 *
949 * This function tries to find partition on MTD device @master. It uses MTD
950 * partition parsers, specified in @types. However, if @types is %NULL, then
951 * the default list of parsers is used. The default list contains only the
952 * "cmdlinepart" and "ofpart" parsers ATM.
953 * Note: If there are more then one parser in @types, the kernel only takes the
954 * partitions parsed out by the first parser.
955 *
956 * This function may return:
957 * o a negative error code in case of failure
958 * o zero if no partitions were found
959 * o a positive number of found partitions, in which case on exit @pparts will
960 * point to an array containing this number of &struct mtd_info objects.
961 */
962int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
963 struct mtd_partition **pparts,
964 struct mtd_part_parser_data *data)
965{
966 struct mtd_part_parser *parser;
967 int ret = 0;
968
969 if (!types)
970 types = default_mtd_part_types;
971
972 for ( ; ret <= 0 && *types; types++) {
973 parser = get_partition_parser(*types);
974 if (!parser && !request_module("%s", *types))
975 parser = get_partition_parser(*types);
976 if (!parser)
977 continue;
978 ret = (*parser->parse_fn)(master, pparts, data);
979 put_partition_parser(parser);
980 if (ret > 0) {
981 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
982 ret, parser->name, master->name);
983 break;
984 }
985 }
986 return ret;
987}
988#endif
989
Heiko Schocherff94bc42014-06-24 10:10:04 +0200990/* Returns the size of the entire flash chip */
991uint64_t mtd_get_device_size(const struct mtd_info *mtd)
992{
Miquel Raynal2a749302018-09-29 12:58:27 +0200993 if (mtd_is_partition(mtd))
994 return mtd->parent->size;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200995
Miquel Raynal2a749302018-09-29 12:58:27 +0200996 return mtd->size;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200997}
998EXPORT_SYMBOL_GPL(mtd_get_device_size);