blob: fd8d8e5ea7295ac44c2bb25677d2e3df06f6d1cc [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__
12#include <linux/module.h>
13#include <linux/types.h>
14#include <linux/kernel.h>
15#include <linux/slab.h>
16#include <linux/list.h>
17#include <linux/kmod.h>
18#endif
19
Kyungmin Parke29c22f2008-11-19 16:20:36 +010020#include <common.h>
21#include <malloc.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090022#include <linux/errno.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020023#include <linux/compat.h>
24#include <ubi_uboot.h>
Kyungmin Parke29c22f2008-11-19 16:20:36 +010025
Kyungmin Parke29c22f2008-11-19 16:20:36 +010026#include <linux/mtd/mtd.h>
27#include <linux/mtd/partitions.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020028#include <linux/err.h>
Miquel Raynal21cc1fb2018-09-29 12:58:25 +020029#include <linux/sizes.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020030
31#include "mtdcore.h"
Kyungmin Parke29c22f2008-11-19 16:20:36 +010032
Heiko Schocherff94bc42014-06-24 10:10:04 +020033#ifndef __UBOOT__
34static DEFINE_MUTEX(mtd_partitions_mutex);
35#else
36DEFINE_MUTEX(mtd_partitions_mutex);
37#endif
Kyungmin Parke29c22f2008-11-19 16:20:36 +010038
Heiko Schocherff94bc42014-06-24 10:10:04 +020039#ifdef __UBOOT__
40/* from mm/util.c */
41
42/**
43 * kstrdup - allocate space for and copy an existing string
44 * @s: the string to duplicate
45 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
46 */
47char *kstrdup(const char *s, gfp_t gfp)
48{
49 size_t len;
50 char *buf;
51
52 if (!s)
53 return NULL;
54
55 len = strlen(s) + 1;
56 buf = kmalloc(len, gfp);
57 if (buf)
58 memcpy(buf, s, len);
59 return buf;
60}
61#endif
62
Miquel Raynal21cc1fb2018-09-29 12:58:25 +020063#define MTD_SIZE_REMAINING (~0LLU)
64#define MTD_OFFSET_NOT_SPECIFIED (~0LLU)
65
Boris Brezillon4a5594f2018-12-02 10:54:30 +010066bool mtd_partitions_used(struct mtd_info *master)
67{
68 struct mtd_info *slave;
69
70 list_for_each_entry(slave, &master->partitions, node) {
71 if (slave->usecount)
72 return true;
73 }
74
75 return false;
76}
77
Miquel Raynal21cc1fb2018-09-29 12:58:25 +020078/**
79 * mtd_parse_partition - Parse @mtdparts partition definition, fill @partition
80 * with it and update the @mtdparts string pointer.
81 *
82 * The partition name is allocated and must be freed by the caller.
83 *
84 * This function is widely inspired from part_parse (mtdparts.c).
85 *
86 * @mtdparts: String describing the partition with mtdparts command syntax
87 * @partition: MTD partition structure to fill
88 *
89 * @return 0 on success, an error otherwise.
90 */
91static int mtd_parse_partition(const char **_mtdparts,
92 struct mtd_partition *partition)
93{
94 const char *mtdparts = *_mtdparts;
95 const char *name = NULL;
96 int name_len;
97 char *buf;
98
99 /* Ensure the partition structure is empty */
100 memset(partition, 0, sizeof(struct mtd_partition));
101
102 /* Fetch the partition size */
103 if (*mtdparts == '-') {
104 /* Assign all remaining space to this partition */
105 partition->size = MTD_SIZE_REMAINING;
106 mtdparts++;
107 } else {
108 partition->size = ustrtoull(mtdparts, (char **)&mtdparts, 0);
109 if (partition->size < SZ_4K) {
110 printf("Minimum partition size 4kiB, %lldB requested\n",
111 partition->size);
112 return -EINVAL;
113 }
114 }
115
116 /* Check for the offset */
117 partition->offset = MTD_OFFSET_NOT_SPECIFIED;
118 if (*mtdparts == '@') {
119 mtdparts++;
120 partition->offset = ustrtoull(mtdparts, (char **)&mtdparts, 0);
121 }
122
123 /* Now look for the name */
124 if (*mtdparts == '(') {
125 name = ++mtdparts;
126 mtdparts = strchr(name, ')');
127 if (!mtdparts) {
128 printf("No closing ')' found in partition name\n");
129 return -EINVAL;
130 }
131 name_len = mtdparts - name + 1;
132 if ((name_len - 1) == 0) {
133 printf("Empty partition name\n");
134 return -EINVAL;
135 }
136 mtdparts++;
137 } else {
138 /* Name will be of the form size@offset */
139 name_len = 22;
140 }
141
142 /* Check if the partition is read-only */
143 if (strncmp(mtdparts, "ro", 2) == 0) {
144 partition->mask_flags |= MTD_WRITEABLE;
145 mtdparts += 2;
146 }
147
148 /* Check for a potential next partition definition */
149 if (*mtdparts == ',') {
150 if (partition->size == MTD_SIZE_REMAINING) {
151 printf("No partitions allowed after a fill-up\n");
152 return -EINVAL;
153 }
154 ++mtdparts;
155 } else if ((*mtdparts == ';') || (*mtdparts == '\0')) {
156 /* NOP */
157 } else {
158 printf("Unexpected character '%c' in mtdparts\n", *mtdparts);
159 return -EINVAL;
160 }
161
162 /*
163 * Allocate a buffer for the name and either copy the provided name or
164 * auto-generate it with the form 'size@offset'.
165 */
166 buf = malloc(name_len);
167 if (!buf)
168 return -ENOMEM;
169
170 if (name)
171 strncpy(buf, name, name_len - 1);
172 else
173 snprintf(buf, name_len, "0x%08llx@0x%08llx",
174 partition->size, partition->offset);
175
176 buf[name_len - 1] = '\0';
177 partition->name = buf;
178
179 *_mtdparts = mtdparts;
180
181 return 0;
182}
183
184/**
185 * mtd_parse_partitions - Create a partition array from an mtdparts definition
186 *
187 * Stateless function that takes a @parent MTD device, a string @_mtdparts
188 * describing the partitions (with the "mtdparts" command syntax) and creates
189 * the corresponding MTD partition structure array @_parts. Both the name and
190 * the structure partition itself must be freed freed, the caller may use
191 * @mtd_free_parsed_partitions() for this purpose.
192 *
193 * @parent: MTD device which contains the partitions
194 * @_mtdparts: Pointer to a string describing the partitions with "mtdparts"
195 * command syntax.
196 * @_parts: Allocated array containing the partitions, must be freed by the
197 * caller.
198 * @_nparts: Size of @_parts array.
199 *
200 * @return 0 on success, an error otherwise.
201 */
202int mtd_parse_partitions(struct mtd_info *parent, const char **_mtdparts,
203 struct mtd_partition **_parts, int *_nparts)
204{
205 struct mtd_partition partition = {}, *parts;
206 const char *mtdparts = *_mtdparts;
207 int cur_off = 0, cur_sz = 0;
208 int nparts = 0;
209 int ret, idx;
210 u64 sz;
211
212 /* First, iterate over the partitions until we know their number */
213 while (mtdparts[0] != '\0' && mtdparts[0] != ';') {
214 ret = mtd_parse_partition(&mtdparts, &partition);
215 if (ret)
216 return ret;
217
218 free((char *)partition.name);
219 nparts++;
220 }
221
222 /* Allocate an array of partitions to give back to the caller */
223 parts = malloc(sizeof(*parts) * nparts);
224 if (!parts) {
225 printf("Not enough space to save partitions meta-data\n");
226 return -ENOMEM;
227 }
228
229 /* Iterate again over each partition to save the data in our array */
230 for (idx = 0; idx < nparts; idx++) {
231 ret = mtd_parse_partition(_mtdparts, &parts[idx]);
232 if (ret)
233 return ret;
234
235 if (parts[idx].size == MTD_SIZE_REMAINING)
236 parts[idx].size = parent->size - cur_sz;
237 cur_sz += parts[idx].size;
238
239 sz = parts[idx].size;
240 if (sz < parent->writesize || do_div(sz, parent->writesize)) {
241 printf("Partition size must be a multiple of %d\n",
242 parent->writesize);
243 return -EINVAL;
244 }
245
246 if (parts[idx].offset == MTD_OFFSET_NOT_SPECIFIED)
247 parts[idx].offset = cur_off;
248 cur_off += parts[idx].size;
249
250 parts[idx].ecclayout = parent->ecclayout;
251 }
252
253 /* Offset by one mtdparts to point to the next device if any */
254 if (*_mtdparts[0] == ';')
255 (*_mtdparts)++;
256
257 *_parts = parts;
258 *_nparts = nparts;
259
260 return 0;
261}
262
263/**
264 * mtd_free_parsed_partitions - Free dynamically allocated partitions
265 *
266 * Each successful call to @mtd_parse_partitions must be followed by a call to
267 * @mtd_free_parsed_partitions to free any allocated array during the parsing
268 * process.
269 *
270 * @parts: Array containing the partitions that will be freed.
271 * @nparts: Size of @parts array.
272 */
273void mtd_free_parsed_partitions(struct mtd_partition *parts,
274 unsigned int nparts)
275{
276 int i;
277
278 for (i = 0; i < nparts; i++)
279 free((char *)parts[i].name);
280
281 free(parts);
282}
283
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100284/*
285 * MTD methods which simply translate the effective address and pass through
286 * to the _real_ device.
287 */
288
Stefan Roese8d2effe2009-05-11 16:03:55 +0200289static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
290 size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100291{
Stefan Roese8d2effe2009-05-11 16:03:55 +0200292 struct mtd_ecc_stats stats;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100293 int res;
294
Miquel Raynal2a749302018-09-29 12:58:27 +0200295 stats = mtd->parent->ecc_stats;
296 res = mtd->parent->_read(mtd->parent, from + mtd->offset, len,
297 retlen, buf);
Paul Burton40462e52013-09-04 15:16:56 +0100298 if (unlikely(mtd_is_eccerr(res)))
299 mtd->ecc_stats.failed +=
Miquel Raynal2a749302018-09-29 12:58:27 +0200300 mtd->parent->ecc_stats.failed - stats.failed;
Paul Burton40462e52013-09-04 15:16:56 +0100301 else
302 mtd->ecc_stats.corrected +=
Miquel Raynal2a749302018-09-29 12:58:27 +0200303 mtd->parent->ecc_stats.corrected - stats.corrected;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100304 return res;
305}
306
Heiko Schocherff94bc42014-06-24 10:10:04 +0200307#ifndef __UBOOT__
308static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
309 size_t *retlen, void **virt, resource_size_t *phys)
310{
Miquel Raynal2a749302018-09-29 12:58:27 +0200311 return mtd->parent->_point(mtd->parent, from + mtd->offset, len,
312 retlen, virt, phys);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200313}
314
315static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
316{
Miquel Raynal2a749302018-09-29 12:58:27 +0200317 return mtd->parent->_unpoint(mtd->parent, from + mtd->offset, len);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200318}
319#endif
320
321static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
322 unsigned long len,
323 unsigned long offset,
324 unsigned long flags)
325{
Miquel Raynal2a749302018-09-29 12:58:27 +0200326 offset += mtd->offset;
327 return mtd->parent->_get_unmapped_area(mtd->parent, len, offset, flags);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200328}
329
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100330static int part_read_oob(struct mtd_info *mtd, loff_t from,
Stefan Roese8d2effe2009-05-11 16:03:55 +0200331 struct mtd_oob_ops *ops)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100332{
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100333 int res;
334
335 if (from >= mtd->size)
336 return -EINVAL;
337 if (ops->datbuf && from + ops->len > mtd->size)
338 return -EINVAL;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100339
Heiko Schocherff94bc42014-06-24 10:10:04 +0200340 /*
341 * If OOB is also requested, make sure that we do not read past the end
342 * of this partition.
343 */
344 if (ops->oobbuf) {
345 size_t len, pages;
346
347 if (ops->mode == MTD_OPS_AUTO_OOB)
348 len = mtd->oobavail;
349 else
350 len = mtd->oobsize;
351 pages = mtd_div_by_ws(mtd->size, mtd);
352 pages -= mtd_div_by_ws(from, mtd);
353 if (ops->ooboffs + ops->ooblen > pages * len)
354 return -EINVAL;
355 }
356
Miquel Raynal2a749302018-09-29 12:58:27 +0200357 res = mtd->parent->_read_oob(mtd->parent, from + mtd->offset, ops);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100358 if (unlikely(res)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000359 if (mtd_is_bitflip(res))
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100360 mtd->ecc_stats.corrected++;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000361 if (mtd_is_eccerr(res))
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100362 mtd->ecc_stats.failed++;
363 }
364 return res;
365}
366
Stefan Roese8d2effe2009-05-11 16:03:55 +0200367static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
368 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100369{
Miquel Raynal2a749302018-09-29 12:58:27 +0200370 return mtd->parent->_read_user_prot_reg(mtd->parent, from, len,
371 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100372}
373
Heiko Schocher4e67c572014-07-15 16:08:43 +0200374static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
375 size_t *retlen, struct otp_info *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100376{
Miquel Raynal2a749302018-09-29 12:58:27 +0200377 return mtd->parent->_get_user_prot_info(mtd->parent, len, retlen,
378 buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100379}
380
Stefan Roese8d2effe2009-05-11 16:03:55 +0200381static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
382 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100383{
Miquel Raynal2a749302018-09-29 12:58:27 +0200384 return mtd->parent->_read_fact_prot_reg(mtd->parent, from, len,
385 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100386}
387
Heiko Schocher4e67c572014-07-15 16:08:43 +0200388static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
389 size_t *retlen, struct otp_info *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100390{
Miquel Raynal2a749302018-09-29 12:58:27 +0200391 return mtd->parent->_get_fact_prot_info(mtd->parent, len, retlen,
392 buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100393}
394
Stefan Roese8d2effe2009-05-11 16:03:55 +0200395static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
396 size_t *retlen, const u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100397{
Miquel Raynal2a749302018-09-29 12:58:27 +0200398 return mtd->parent->_write(mtd->parent, to + mtd->offset, len,
399 retlen, buf);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200400}
401
402static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
403 size_t *retlen, const u_char *buf)
404{
Miquel Raynal2a749302018-09-29 12:58:27 +0200405 return mtd->parent->_panic_write(mtd->parent, to + mtd->offset, len,
406 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100407}
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100408
409static int part_write_oob(struct mtd_info *mtd, loff_t to,
Stefan Roese8d2effe2009-05-11 16:03:55 +0200410 struct mtd_oob_ops *ops)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100411{
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100412 if (to >= mtd->size)
413 return -EINVAL;
414 if (ops->datbuf && to + ops->len > mtd->size)
415 return -EINVAL;
Miquel Raynal2a749302018-09-29 12:58:27 +0200416 return mtd->parent->_write_oob(mtd->parent, to + mtd->offset, ops);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100417}
418
Stefan Roese8d2effe2009-05-11 16:03:55 +0200419static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
420 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100421{
Miquel Raynal2a749302018-09-29 12:58:27 +0200422 return mtd->parent->_write_user_prot_reg(mtd->parent, from, len,
423 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100424}
425
Stefan Roese8d2effe2009-05-11 16:03:55 +0200426static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
427 size_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100428{
Miquel Raynal2a749302018-09-29 12:58:27 +0200429 return mtd->parent->_lock_user_prot_reg(mtd->parent, from, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100430}
431
Heiko Schocherff94bc42014-06-24 10:10:04 +0200432#ifndef __UBOOT__
433static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
434 unsigned long count, loff_t to, size_t *retlen)
435{
Miquel Raynal2a749302018-09-29 12:58:27 +0200436 return mtd->parent->_writev(mtd->parent, vecs, count,
437 to + mtd->offset, retlen);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200438}
439#endif
440
Stefan Roese8d2effe2009-05-11 16:03:55 +0200441static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100442{
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100443 int ret;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000444
Miquel Raynal2a749302018-09-29 12:58:27 +0200445 instr->addr += mtd->offset;
446 ret = mtd->parent->_erase(mtd->parent, instr);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100447 if (ret) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200448 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Miquel Raynal2a749302018-09-29 12:58:27 +0200449 instr->fail_addr -= mtd->offset;
450 instr->addr -= mtd->offset;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100451 }
452 return ret;
453}
454
455void mtd_erase_callback(struct erase_info *instr)
456{
Sergey Lapindfe64e22013-01-14 03:46:50 +0000457 if (instr->mtd->_erase == part_erase) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200458 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Miquel Raynal2a749302018-09-29 12:58:27 +0200459 instr->fail_addr -= instr->mtd->offset;
460 instr->addr -= instr->mtd->offset;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100461 }
462 if (instr->callback)
463 instr->callback(instr);
464}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200465EXPORT_SYMBOL_GPL(mtd_erase_callback);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100466
Stefan Roese8d2effe2009-05-11 16:03:55 +0200467static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100468{
Miquel Raynal2a749302018-09-29 12:58:27 +0200469 return mtd->parent->_lock(mtd->parent, ofs + mtd->offset, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100470}
471
Stefan Roese8d2effe2009-05-11 16:03:55 +0200472static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100473{
Miquel Raynal2a749302018-09-29 12:58:27 +0200474 return mtd->parent->_unlock(mtd->parent, ofs + mtd->offset, len);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200475}
476
477static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
478{
Miquel Raynal2a749302018-09-29 12:58:27 +0200479 return mtd->parent->_is_locked(mtd->parent, ofs + mtd->offset, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100480}
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100481
482static void part_sync(struct mtd_info *mtd)
483{
Miquel Raynal2a749302018-09-29 12:58:27 +0200484 mtd->parent->_sync(mtd->parent);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100485}
486
Heiko Schocherff94bc42014-06-24 10:10:04 +0200487#ifndef __UBOOT__
488static int part_suspend(struct mtd_info *mtd)
489{
Miquel Raynal2a749302018-09-29 12:58:27 +0200490 return mtd->parent->_suspend(mtd->parent);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200491}
492
493static void part_resume(struct mtd_info *mtd)
494{
Miquel Raynal2a749302018-09-29 12:58:27 +0200495 mtd->parent->_resume(mtd->parent);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200496}
497#endif
498
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300499static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
500{
Miquel Raynal2a749302018-09-29 12:58:27 +0200501 ofs += mtd->offset;
502 return mtd->parent->_block_isreserved(mtd->parent, ofs);
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300503}
504
Stefan Roese8d2effe2009-05-11 16:03:55 +0200505static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100506{
Miquel Raynal2a749302018-09-29 12:58:27 +0200507 ofs += mtd->offset;
508 return mtd->parent->_block_isbad(mtd->parent, ofs);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100509}
510
Stefan Roese8d2effe2009-05-11 16:03:55 +0200511static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100512{
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100513 int res;
514
Miquel Raynal2a749302018-09-29 12:58:27 +0200515 ofs += mtd->offset;
516 res = mtd->parent->_block_markbad(mtd->parent, ofs);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100517 if (!res)
518 mtd->ecc_stats.badblocks++;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100519 return res;
520}
521
Miquel Raynal2a749302018-09-29 12:58:27 +0200522static inline void free_partition(struct mtd_info *p)
Heiko Schocherff94bc42014-06-24 10:10:04 +0200523{
Miquel Raynal2a749302018-09-29 12:58:27 +0200524 kfree(p->name);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200525 kfree(p);
526}
527
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100528/*
529 * This function unregisters and destroy all slave MTD objects which are
Miquel Raynal2a749302018-09-29 12:58:27 +0200530 * attached to the given master MTD object, recursively.
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100531 */
Miquel Raynal2a749302018-09-29 12:58:27 +0200532static int do_del_mtd_partitions(struct mtd_info *master)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100533{
Miquel Raynal2a749302018-09-29 12:58:27 +0200534 struct mtd_info *slave, *next;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200535 int ret, err = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100536
Miquel Raynal2a749302018-09-29 12:58:27 +0200537 list_for_each_entry_safe(slave, next, &master->partitions, node) {
538 if (mtd_has_partitions(slave))
539 del_mtd_partitions(slave);
Miquel Raynalb0036f72018-08-16 17:30:19 +0200540
Miquel Raynal2a749302018-09-29 12:58:27 +0200541 debug("Deleting %s MTD partition\n", slave->name);
542 ret = del_mtd_device(slave);
543 if (ret < 0) {
544 printf("Error when deleting partition \"%s\" (%d)\n",
545 slave->name, ret);
546 err = ret;
547 continue;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200548 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200549
550 list_del(&slave->node);
551 free_partition(slave);
552 }
Stefan Roese8d2effe2009-05-11 16:03:55 +0200553
Heiko Schocherff94bc42014-06-24 10:10:04 +0200554 return err;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200555}
556
Miquel Raynal2a749302018-09-29 12:58:27 +0200557int del_mtd_partitions(struct mtd_info *master)
Stefan Roese8d2effe2009-05-11 16:03:55 +0200558{
Miquel Raynal2a749302018-09-29 12:58:27 +0200559 int ret;
560
561 debug("Deleting MTD partitions on \"%s\":\n", master->name);
562
563 mutex_lock(&mtd_partitions_mutex);
564 ret = do_del_mtd_partitions(master);
565 mutex_unlock(&mtd_partitions_mutex);
566
567 return ret;
568}
569
570static struct mtd_info *allocate_partition(struct mtd_info *master,
571 const struct mtd_partition *part,
572 int partno, uint64_t cur_offset)
573{
574 struct mtd_info *slave;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200575 char *name;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200576
577 /* allocate the partition structure */
578 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200579 name = kstrdup(part->name, GFP_KERNEL);
580 if (!name || !slave) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200581 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200582 master->name);
583 kfree(name);
584 kfree(slave);
585 return ERR_PTR(-ENOMEM);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200586 }
Stefan Roese8d2effe2009-05-11 16:03:55 +0200587
588 /* set up the MTD object for this partition */
Miquel Raynal2a749302018-09-29 12:58:27 +0200589 slave->type = master->type;
590 slave->flags = master->flags & ~part->mask_flags;
591 slave->size = part->size;
592 slave->writesize = master->writesize;
593 slave->writebufsize = master->writebufsize;
594 slave->oobsize = master->oobsize;
595 slave->oobavail = master->oobavail;
596 slave->subpage_sft = master->subpage_sft;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200597
Miquel Raynal2a749302018-09-29 12:58:27 +0200598 slave->name = name;
599 slave->owner = master->owner;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200600#ifndef __UBOOT__
Miquel Raynal2a749302018-09-29 12:58:27 +0200601 slave->backing_dev_info = master->backing_dev_info;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200602
603 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
604 * to have the same data be in two different partitions.
605 */
Miquel Raynal2a749302018-09-29 12:58:27 +0200606 slave->dev.parent = master->dev.parent;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200607#endif
Stefan Roese8d2effe2009-05-11 16:03:55 +0200608
Boris Brezillon596cf082018-08-16 17:29:59 +0200609 if (master->_read)
Miquel Raynal2a749302018-09-29 12:58:27 +0200610 slave->_read = part_read;
Boris Brezillon596cf082018-08-16 17:29:59 +0200611 if (master->_write)
Miquel Raynal2a749302018-09-29 12:58:27 +0200612 slave->_write = part_write;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200613
Heiko Schocherff94bc42014-06-24 10:10:04 +0200614 if (master->_panic_write)
Miquel Raynal2a749302018-09-29 12:58:27 +0200615 slave->_panic_write = part_panic_write;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200616
617#ifndef __UBOOT__
618 if (master->_point && master->_unpoint) {
Miquel Raynal2a749302018-09-29 12:58:27 +0200619 slave->_point = part_point;
620 slave->_unpoint = part_unpoint;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200621 }
622#endif
623
624 if (master->_get_unmapped_area)
Miquel Raynal2a749302018-09-29 12:58:27 +0200625 slave->_get_unmapped_area = part_get_unmapped_area;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000626 if (master->_read_oob)
Miquel Raynal2a749302018-09-29 12:58:27 +0200627 slave->_read_oob = part_read_oob;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000628 if (master->_write_oob)
Miquel Raynal2a749302018-09-29 12:58:27 +0200629 slave->_write_oob = part_write_oob;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000630 if (master->_read_user_prot_reg)
Miquel Raynal2a749302018-09-29 12:58:27 +0200631 slave->_read_user_prot_reg = part_read_user_prot_reg;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000632 if (master->_read_fact_prot_reg)
Miquel Raynal2a749302018-09-29 12:58:27 +0200633 slave->_read_fact_prot_reg = part_read_fact_prot_reg;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000634 if (master->_write_user_prot_reg)
Miquel Raynal2a749302018-09-29 12:58:27 +0200635 slave->_write_user_prot_reg = part_write_user_prot_reg;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000636 if (master->_lock_user_prot_reg)
Miquel Raynal2a749302018-09-29 12:58:27 +0200637 slave->_lock_user_prot_reg = part_lock_user_prot_reg;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000638 if (master->_get_user_prot_info)
Miquel Raynal2a749302018-09-29 12:58:27 +0200639 slave->_get_user_prot_info = part_get_user_prot_info;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000640 if (master->_get_fact_prot_info)
Miquel Raynal2a749302018-09-29 12:58:27 +0200641 slave->_get_fact_prot_info = part_get_fact_prot_info;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000642 if (master->_sync)
Miquel Raynal2a749302018-09-29 12:58:27 +0200643 slave->_sync = part_sync;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200644#ifndef __UBOOT__
645 if (!partno && !master->dev.class && master->_suspend &&
646 master->_resume) {
Miquel Raynal2a749302018-09-29 12:58:27 +0200647 slave->_suspend = part_suspend;
648 slave->_resume = part_resume;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200649 }
650 if (master->_writev)
Miquel Raynal2a749302018-09-29 12:58:27 +0200651 slave->_writev = part_writev;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200652#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +0000653 if (master->_lock)
Miquel Raynal2a749302018-09-29 12:58:27 +0200654 slave->_lock = part_lock;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000655 if (master->_unlock)
Miquel Raynal2a749302018-09-29 12:58:27 +0200656 slave->_unlock = part_unlock;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200657 if (master->_is_locked)
Miquel Raynal2a749302018-09-29 12:58:27 +0200658 slave->_is_locked = part_is_locked;
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300659 if (master->_block_isreserved)
Miquel Raynal2a749302018-09-29 12:58:27 +0200660 slave->_block_isreserved = part_block_isreserved;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000661 if (master->_block_isbad)
Miquel Raynal2a749302018-09-29 12:58:27 +0200662 slave->_block_isbad = part_block_isbad;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000663 if (master->_block_markbad)
Miquel Raynal2a749302018-09-29 12:58:27 +0200664 slave->_block_markbad = part_block_markbad;
665 slave->_erase = part_erase;
666 slave->parent = master;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200667 slave->offset = part->offset;
Miquel Raynal2a749302018-09-29 12:58:27 +0200668 INIT_LIST_HEAD(&slave->partitions);
669 INIT_LIST_HEAD(&slave->node);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200670
671 if (slave->offset == MTDPART_OFS_APPEND)
672 slave->offset = cur_offset;
673 if (slave->offset == MTDPART_OFS_NXTBLK) {
674 slave->offset = cur_offset;
675 if (mtd_mod_by_eb(cur_offset, master) != 0) {
676 /* Round up to next erasesize */
677 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200678 debug("Moving partition %d: "
679 "0x%012llx -> 0x%012llx\n", partno,
680 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
681 }
682 }
683 if (slave->offset == MTDPART_OFS_RETAIN) {
684 slave->offset = cur_offset;
Miquel Raynal2a749302018-09-29 12:58:27 +0200685 if (master->size - slave->offset >= slave->size) {
686 slave->size = master->size - slave->offset
687 - slave->size;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200688 } else {
689 debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
690 part->name, master->size - slave->offset,
Miquel Raynal2a749302018-09-29 12:58:27 +0200691 slave->size);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200692 /* register to preserve ordering */
693 goto out_register;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200694 }
695 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200696 if (slave->size == MTDPART_SIZ_FULL)
697 slave->size = master->size - slave->offset;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200698
Heiko Schocherff94bc42014-06-24 10:10:04 +0200699 debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
Miquel Raynal2a749302018-09-29 12:58:27 +0200700 (unsigned long long)(slave->offset + slave->size), slave->name);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200701
702 /* let's do some sanity checks */
703 if (slave->offset >= master->size) {
704 /* let's register it anyway to preserve ordering */
705 slave->offset = 0;
Miquel Raynal2a749302018-09-29 12:58:27 +0200706 slave->size = 0;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200707 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
708 part->name);
709 goto out_register;
710 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200711 if (slave->offset + slave->size > master->size) {
712 slave->size = master->size - slave->offset;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200713 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 +0200714 part->name, master->name, slave->size);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200715 }
716 if (master->numeraseregions > 1) {
717 /* Deal with variable erase size stuff */
718 int i, max = master->numeraseregions;
Miquel Raynal2a749302018-09-29 12:58:27 +0200719 u64 end = slave->offset + slave->size;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200720 struct mtd_erase_region_info *regions = master->eraseregions;
721
722 /* Find the first erase regions which is part of this
723 * partition. */
724 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
725 ;
726 /* The loop searched for the region _behind_ the first one */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200727 if (i > 0)
728 i--;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200729
730 /* Pick biggest erasesize */
731 for (; i < max && regions[i].offset < end; i++) {
Miquel Raynal2a749302018-09-29 12:58:27 +0200732 if (slave->erasesize < regions[i].erasesize)
733 slave->erasesize = regions[i].erasesize;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200734 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200735 WARN_ON(slave->erasesize == 0);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200736 } else {
737 /* Single erase size */
Miquel Raynal2a749302018-09-29 12:58:27 +0200738 slave->erasesize = master->erasesize;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200739 }
740
Miquel Raynal2a749302018-09-29 12:58:27 +0200741 if ((slave->flags & MTD_WRITEABLE) &&
742 mtd_mod_by_eb(slave->offset, slave)) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200743 /* Doesn't start on a boundary of major erase size */
744 /* FIXME: Let it be writable if it is on a boundary of
745 * _minor_ erase size though */
Miquel Raynal2a749302018-09-29 12:58:27 +0200746 slave->flags &= ~MTD_WRITEABLE;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200747 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
748 part->name);
749 }
Miquel Raynal2a749302018-09-29 12:58:27 +0200750 if ((slave->flags & MTD_WRITEABLE) &&
751 mtd_mod_by_eb(slave->size, slave)) {
752 slave->flags &= ~MTD_WRITEABLE;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200753 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
754 part->name);
755 }
756
Miquel Raynal2a749302018-09-29 12:58:27 +0200757 slave->ecclayout = master->ecclayout;
758 slave->ecc_step_size = master->ecc_step_size;
759 slave->ecc_strength = master->ecc_strength;
760 slave->bitflip_threshold = master->bitflip_threshold;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200761
Sergey Lapindfe64e22013-01-14 03:46:50 +0000762 if (master->_block_isbad) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200763 uint64_t offs = 0;
764
Miquel Raynal2a749302018-09-29 12:58:27 +0200765 while (offs < slave->size) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000766 if (mtd_block_isbad(master, offs + slave->offset))
Miquel Raynal2a749302018-09-29 12:58:27 +0200767 slave->ecc_stats.badblocks++;
768 offs += slave->erasesize;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100769 }
770 }
771
Stefan Roese8d2effe2009-05-11 16:03:55 +0200772out_register:
Stefan Roese8d2effe2009-05-11 16:03:55 +0200773 return slave;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100774}
775
Heiko Schocherddf7bcf2014-07-15 16:08:42 +0200776#ifndef __UBOOT__
Heiko Schocherff94bc42014-06-24 10:10:04 +0200777int mtd_add_partition(struct mtd_info *master, const char *name,
778 long long offset, long long length)
779{
780 struct mtd_partition part;
Miquel Raynal2a749302018-09-29 12:58:27 +0200781 struct mtd_info *p, *new;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200782 uint64_t start, end;
783 int ret = 0;
784
785 /* the direct offset is expected */
786 if (offset == MTDPART_OFS_APPEND ||
787 offset == MTDPART_OFS_NXTBLK)
788 return -EINVAL;
789
790 if (length == MTDPART_SIZ_FULL)
791 length = master->size - offset;
792
793 if (length <= 0)
794 return -EINVAL;
795
796 part.name = name;
797 part.size = length;
798 part.offset = offset;
799 part.mask_flags = 0;
800 part.ecclayout = NULL;
801
802 new = allocate_partition(master, &part, -1, offset);
803 if (IS_ERR(new))
804 return PTR_ERR(new);
805
806 start = offset;
807 end = offset + length;
808
809 mutex_lock(&mtd_partitions_mutex);
Miquel Raynal2a749302018-09-29 12:58:27 +0200810 list_for_each_entry(p, &master->partitions, node) {
811 if (start >= p->offset &&
812 (start < (p->offset + p->size)))
813 goto err_inv;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200814
Miquel Raynal2a749302018-09-29 12:58:27 +0200815 if (end >= p->offset &&
816 (end < (p->offset + p->size)))
817 goto err_inv;
818 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200819
Miquel Raynal2a749302018-09-29 12:58:27 +0200820 list_add_tail(&new->node, &master->partitions);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200821 mutex_unlock(&mtd_partitions_mutex);
822
Miquel Raynal2a749302018-09-29 12:58:27 +0200823 add_mtd_device(new);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200824
825 return ret;
826err_inv:
827 mutex_unlock(&mtd_partitions_mutex);
828 free_partition(new);
829 return -EINVAL;
830}
831EXPORT_SYMBOL_GPL(mtd_add_partition);
832
833int mtd_del_partition(struct mtd_info *master, int partno)
834{
Miquel Raynal2a749302018-09-29 12:58:27 +0200835 struct mtd_info *slave, *next;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200836 int ret = -EINVAL;
837
838 mutex_lock(&mtd_partitions_mutex);
Miquel Raynal2a749302018-09-29 12:58:27 +0200839 list_for_each_entry_safe(slave, next, &master->partitions, node)
840 if (slave->index == partno) {
841 ret = del_mtd_device(slave);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200842 if (ret < 0)
843 break;
844
Miquel Raynal2a749302018-09-29 12:58:27 +0200845 list_del(&slave->node);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200846 free_partition(slave);
847 break;
848 }
849 mutex_unlock(&mtd_partitions_mutex);
850
851 return ret;
852}
853EXPORT_SYMBOL_GPL(mtd_del_partition);
Heiko Schocherddf7bcf2014-07-15 16:08:42 +0200854#endif
Heiko Schocherff94bc42014-06-24 10:10:04 +0200855
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100856/*
857 * This function, given a master MTD object and a partition table, creates
858 * and registers slave MTD objects which are bound to the master according to
859 * the partition definitions.
Stefan Roese8d2effe2009-05-11 16:03:55 +0200860 *
861 * We don't register the master, or expect the caller to have done so,
862 * for reasons of data integrity.
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100863 */
864
865int add_mtd_partitions(struct mtd_info *master,
866 const struct mtd_partition *parts,
867 int nbparts)
868{
Miquel Raynal2a749302018-09-29 12:58:27 +0200869 struct mtd_info *slave;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200870 uint64_t cur_offset = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100871 int i;
872
Joe Hershberger147162d2013-04-08 10:32:49 +0000873 debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100874
875 for (i = 0; i < nbparts; i++) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200876 slave = allocate_partition(master, parts + i, i, cur_offset);
877 if (IS_ERR(slave))
878 return PTR_ERR(slave);
879
880 mutex_lock(&mtd_partitions_mutex);
Miquel Raynal2a749302018-09-29 12:58:27 +0200881 list_add_tail(&slave->node, &master->partitions);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200882 mutex_unlock(&mtd_partitions_mutex);
883
Miquel Raynal2a749302018-09-29 12:58:27 +0200884 add_mtd_device(slave);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200885
Miquel Raynal2a749302018-09-29 12:58:27 +0200886 cur_offset = slave->offset + slave->size;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100887 }
888
889 return 0;
890}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200891
892#ifndef __UBOOT__
893static DEFINE_SPINLOCK(part_parser_lock);
894static LIST_HEAD(part_parsers);
895
896static struct mtd_part_parser *get_partition_parser(const char *name)
897{
898 struct mtd_part_parser *p, *ret = NULL;
899
900 spin_lock(&part_parser_lock);
901
902 list_for_each_entry(p, &part_parsers, list)
903 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
904 ret = p;
905 break;
906 }
907
908 spin_unlock(&part_parser_lock);
909
910 return ret;
911}
912
913#define put_partition_parser(p) do { module_put((p)->owner); } while (0)
914
915void register_mtd_parser(struct mtd_part_parser *p)
916{
917 spin_lock(&part_parser_lock);
918 list_add(&p->list, &part_parsers);
919 spin_unlock(&part_parser_lock);
920}
921EXPORT_SYMBOL_GPL(register_mtd_parser);
922
923void deregister_mtd_parser(struct mtd_part_parser *p)
924{
925 spin_lock(&part_parser_lock);
926 list_del(&p->list);
927 spin_unlock(&part_parser_lock);
928}
929EXPORT_SYMBOL_GPL(deregister_mtd_parser);
930
931/*
932 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
933 * are changing this array!
934 */
935static const char * const default_mtd_part_types[] = {
936 "cmdlinepart",
937 "ofpart",
938 NULL
939};
940
941/**
942 * parse_mtd_partitions - parse MTD partitions
943 * @master: the master partition (describes whole MTD device)
944 * @types: names of partition parsers to try or %NULL
945 * @pparts: array of partitions found is returned here
946 * @data: MTD partition parser-specific data
947 *
948 * This function tries to find partition on MTD device @master. It uses MTD
949 * partition parsers, specified in @types. However, if @types is %NULL, then
950 * the default list of parsers is used. The default list contains only the
951 * "cmdlinepart" and "ofpart" parsers ATM.
952 * Note: If there are more then one parser in @types, the kernel only takes the
953 * partitions parsed out by the first parser.
954 *
955 * This function may return:
956 * o a negative error code in case of failure
957 * o zero if no partitions were found
958 * o a positive number of found partitions, in which case on exit @pparts will
959 * point to an array containing this number of &struct mtd_info objects.
960 */
961int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
962 struct mtd_partition **pparts,
963 struct mtd_part_parser_data *data)
964{
965 struct mtd_part_parser *parser;
966 int ret = 0;
967
968 if (!types)
969 types = default_mtd_part_types;
970
971 for ( ; ret <= 0 && *types; types++) {
972 parser = get_partition_parser(*types);
973 if (!parser && !request_module("%s", *types))
974 parser = get_partition_parser(*types);
975 if (!parser)
976 continue;
977 ret = (*parser->parse_fn)(master, pparts, data);
978 put_partition_parser(parser);
979 if (ret > 0) {
980 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
981 ret, parser->name, master->name);
982 break;
983 }
984 }
985 return ret;
986}
987#endif
988
Heiko Schocherff94bc42014-06-24 10:10:04 +0200989/* Returns the size of the entire flash chip */
990uint64_t mtd_get_device_size(const struct mtd_info *mtd)
991{
Miquel Raynal2a749302018-09-29 12:58:27 +0200992 if (mtd_is_partition(mtd))
993 return mtd->parent->size;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200994
Miquel Raynal2a749302018-09-29 12:58:27 +0200995 return mtd->size;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200996}
997EXPORT_SYMBOL_GPL(mtd_get_device_size);