blob: 49353b038a18c9ebb03192a3070261321195b987 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Kyungmin Parkf6d5e252008-11-19 16:20:36 +01002/*
3 * Simple MTD partitioning layer
4 *
Heiko Schocherf5895d12014-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 Parkf6d5e252008-11-19 16:20:36 +01008 *
Kyungmin Parkf6d5e252008-11-19 16:20:36 +01009 */
10
Heiko Schocherf5895d12014-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 Parkf6d5e252008-11-19 16:20:36 +010020#include <common.h>
21#include <malloc.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090022#include <linux/errno.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020023#include <linux/compat.h>
24#include <ubi_uboot.h>
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010025
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010026#include <linux/mtd/mtd.h>
27#include <linux/mtd/partitions.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020028#include <linux/err.h>
Miquel Raynal6f381d52018-09-29 12:58:25 +020029#include <linux/sizes.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020030
31#include "mtdcore.h"
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010032
33/* Our partition linked list */
Heiko Schocherf5895d12014-06-24 10:10:04 +020034static LIST_HEAD(mtd_partitions);
35#ifndef __UBOOT__
36static DEFINE_MUTEX(mtd_partitions_mutex);
37#else
38DEFINE_MUTEX(mtd_partitions_mutex);
39#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010040
41/* Our partition node structure */
42struct mtd_part {
43 struct mtd_info mtd;
44 struct mtd_info *master;
Stefan Roese586b3a62009-05-11 16:03:55 +020045 uint64_t offset;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010046 struct list_head list;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010047};
48
49/*
50 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
51 * the pointer to that structure with this macro.
52 */
53#define PART(x) ((struct mtd_part *)(x))
54
55
Heiko Schocherf5895d12014-06-24 10:10:04 +020056#ifdef __UBOOT__
57/* from mm/util.c */
58
59/**
60 * kstrdup - allocate space for and copy an existing string
61 * @s: the string to duplicate
62 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
63 */
64char *kstrdup(const char *s, gfp_t gfp)
65{
66 size_t len;
67 char *buf;
68
69 if (!s)
70 return NULL;
71
72 len = strlen(s) + 1;
73 buf = kmalloc(len, gfp);
74 if (buf)
75 memcpy(buf, s, len);
76 return buf;
77}
78#endif
79
Miquel Raynal6f381d52018-09-29 12:58:25 +020080#define MTD_SIZE_REMAINING (~0LLU)
81#define MTD_OFFSET_NOT_SPECIFIED (~0LLU)
82
83/**
84 * mtd_parse_partition - Parse @mtdparts partition definition, fill @partition
85 * with it and update the @mtdparts string pointer.
86 *
87 * The partition name is allocated and must be freed by the caller.
88 *
89 * This function is widely inspired from part_parse (mtdparts.c).
90 *
91 * @mtdparts: String describing the partition with mtdparts command syntax
92 * @partition: MTD partition structure to fill
93 *
94 * @return 0 on success, an error otherwise.
95 */
96static int mtd_parse_partition(const char **_mtdparts,
97 struct mtd_partition *partition)
98{
99 const char *mtdparts = *_mtdparts;
100 const char *name = NULL;
101 int name_len;
102 char *buf;
103
104 /* Ensure the partition structure is empty */
105 memset(partition, 0, sizeof(struct mtd_partition));
106
107 /* Fetch the partition size */
108 if (*mtdparts == '-') {
109 /* Assign all remaining space to this partition */
110 partition->size = MTD_SIZE_REMAINING;
111 mtdparts++;
112 } else {
113 partition->size = ustrtoull(mtdparts, (char **)&mtdparts, 0);
114 if (partition->size < SZ_4K) {
115 printf("Minimum partition size 4kiB, %lldB requested\n",
116 partition->size);
117 return -EINVAL;
118 }
119 }
120
121 /* Check for the offset */
122 partition->offset = MTD_OFFSET_NOT_SPECIFIED;
123 if (*mtdparts == '@') {
124 mtdparts++;
125 partition->offset = ustrtoull(mtdparts, (char **)&mtdparts, 0);
126 }
127
128 /* Now look for the name */
129 if (*mtdparts == '(') {
130 name = ++mtdparts;
131 mtdparts = strchr(name, ')');
132 if (!mtdparts) {
133 printf("No closing ')' found in partition name\n");
134 return -EINVAL;
135 }
136 name_len = mtdparts - name + 1;
137 if ((name_len - 1) == 0) {
138 printf("Empty partition name\n");
139 return -EINVAL;
140 }
141 mtdparts++;
142 } else {
143 /* Name will be of the form size@offset */
144 name_len = 22;
145 }
146
147 /* Check if the partition is read-only */
148 if (strncmp(mtdparts, "ro", 2) == 0) {
149 partition->mask_flags |= MTD_WRITEABLE;
150 mtdparts += 2;
151 }
152
153 /* Check for a potential next partition definition */
154 if (*mtdparts == ',') {
155 if (partition->size == MTD_SIZE_REMAINING) {
156 printf("No partitions allowed after a fill-up\n");
157 return -EINVAL;
158 }
159 ++mtdparts;
160 } else if ((*mtdparts == ';') || (*mtdparts == '\0')) {
161 /* NOP */
162 } else {
163 printf("Unexpected character '%c' in mtdparts\n", *mtdparts);
164 return -EINVAL;
165 }
166
167 /*
168 * Allocate a buffer for the name and either copy the provided name or
169 * auto-generate it with the form 'size@offset'.
170 */
171 buf = malloc(name_len);
172 if (!buf)
173 return -ENOMEM;
174
175 if (name)
176 strncpy(buf, name, name_len - 1);
177 else
178 snprintf(buf, name_len, "0x%08llx@0x%08llx",
179 partition->size, partition->offset);
180
181 buf[name_len - 1] = '\0';
182 partition->name = buf;
183
184 *_mtdparts = mtdparts;
185
186 return 0;
187}
188
189/**
190 * mtd_parse_partitions - Create a partition array from an mtdparts definition
191 *
192 * Stateless function that takes a @parent MTD device, a string @_mtdparts
193 * describing the partitions (with the "mtdparts" command syntax) and creates
194 * the corresponding MTD partition structure array @_parts. Both the name and
195 * the structure partition itself must be freed freed, the caller may use
196 * @mtd_free_parsed_partitions() for this purpose.
197 *
198 * @parent: MTD device which contains the partitions
199 * @_mtdparts: Pointer to a string describing the partitions with "mtdparts"
200 * command syntax.
201 * @_parts: Allocated array containing the partitions, must be freed by the
202 * caller.
203 * @_nparts: Size of @_parts array.
204 *
205 * @return 0 on success, an error otherwise.
206 */
207int mtd_parse_partitions(struct mtd_info *parent, const char **_mtdparts,
208 struct mtd_partition **_parts, int *_nparts)
209{
210 struct mtd_partition partition = {}, *parts;
211 const char *mtdparts = *_mtdparts;
212 int cur_off = 0, cur_sz = 0;
213 int nparts = 0;
214 int ret, idx;
215 u64 sz;
216
217 /* First, iterate over the partitions until we know their number */
218 while (mtdparts[0] != '\0' && mtdparts[0] != ';') {
219 ret = mtd_parse_partition(&mtdparts, &partition);
220 if (ret)
221 return ret;
222
223 free((char *)partition.name);
224 nparts++;
225 }
226
227 /* Allocate an array of partitions to give back to the caller */
228 parts = malloc(sizeof(*parts) * nparts);
229 if (!parts) {
230 printf("Not enough space to save partitions meta-data\n");
231 return -ENOMEM;
232 }
233
234 /* Iterate again over each partition to save the data in our array */
235 for (idx = 0; idx < nparts; idx++) {
236 ret = mtd_parse_partition(_mtdparts, &parts[idx]);
237 if (ret)
238 return ret;
239
240 if (parts[idx].size == MTD_SIZE_REMAINING)
241 parts[idx].size = parent->size - cur_sz;
242 cur_sz += parts[idx].size;
243
244 sz = parts[idx].size;
245 if (sz < parent->writesize || do_div(sz, parent->writesize)) {
246 printf("Partition size must be a multiple of %d\n",
247 parent->writesize);
248 return -EINVAL;
249 }
250
251 if (parts[idx].offset == MTD_OFFSET_NOT_SPECIFIED)
252 parts[idx].offset = cur_off;
253 cur_off += parts[idx].size;
254
255 parts[idx].ecclayout = parent->ecclayout;
256 }
257
258 /* Offset by one mtdparts to point to the next device if any */
259 if (*_mtdparts[0] == ';')
260 (*_mtdparts)++;
261
262 *_parts = parts;
263 *_nparts = nparts;
264
265 return 0;
266}
267
268/**
269 * mtd_free_parsed_partitions - Free dynamically allocated partitions
270 *
271 * Each successful call to @mtd_parse_partitions must be followed by a call to
272 * @mtd_free_parsed_partitions to free any allocated array during the parsing
273 * process.
274 *
275 * @parts: Array containing the partitions that will be freed.
276 * @nparts: Size of @parts array.
277 */
278void mtd_free_parsed_partitions(struct mtd_partition *parts,
279 unsigned int nparts)
280{
281 int i;
282
283 for (i = 0; i < nparts; i++)
284 free((char *)parts[i].name);
285
286 free(parts);
287}
288
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100289/*
290 * MTD methods which simply translate the effective address and pass through
291 * to the _real_ device.
292 */
293
Stefan Roese586b3a62009-05-11 16:03:55 +0200294static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
295 size_t *retlen, u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100296{
297 struct mtd_part *part = PART(mtd);
Stefan Roese586b3a62009-05-11 16:03:55 +0200298 struct mtd_ecc_stats stats;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100299 int res;
300
Stefan Roese586b3a62009-05-11 16:03:55 +0200301 stats = part->master->ecc_stats;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200302 res = part->master->_read(part->master, from + part->offset, len,
303 retlen, buf);
Paul Burton700a76c2013-09-04 15:16:56 +0100304 if (unlikely(mtd_is_eccerr(res)))
305 mtd->ecc_stats.failed +=
306 part->master->ecc_stats.failed - stats.failed;
307 else
308 mtd->ecc_stats.corrected +=
309 part->master->ecc_stats.corrected - stats.corrected;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100310 return res;
311}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200312
313#ifndef __UBOOT__
314static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
315 size_t *retlen, void **virt, resource_size_t *phys)
316{
317 struct mtd_part *part = PART(mtd);
318
319 return part->master->_point(part->master, from + part->offset, len,
320 retlen, virt, phys);
321}
322
323static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
324{
325 struct mtd_part *part = PART(mtd);
326
327 return part->master->_unpoint(part->master, from + part->offset, len);
328}
329#endif
330
331static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
332 unsigned long len,
333 unsigned long offset,
334 unsigned long flags)
335{
336 struct mtd_part *part = PART(mtd);
337
338 offset += part->offset;
339 return part->master->_get_unmapped_area(part->master, len, offset,
340 flags);
341}
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100342
343static int part_read_oob(struct mtd_info *mtd, loff_t from,
Stefan Roese586b3a62009-05-11 16:03:55 +0200344 struct mtd_oob_ops *ops)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100345{
346 struct mtd_part *part = PART(mtd);
347 int res;
348
349 if (from >= mtd->size)
350 return -EINVAL;
351 if (ops->datbuf && from + ops->len > mtd->size)
352 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200353
354 /*
355 * If OOB is also requested, make sure that we do not read past the end
356 * of this partition.
357 */
358 if (ops->oobbuf) {
359 size_t len, pages;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100360
Heiko Schocherf5895d12014-06-24 10:10:04 +0200361 if (ops->mode == MTD_OPS_AUTO_OOB)
362 len = mtd->oobavail;
363 else
364 len = mtd->oobsize;
365 pages = mtd_div_by_ws(mtd->size, mtd);
366 pages -= mtd_div_by_ws(from, mtd);
367 if (ops->ooboffs + ops->ooblen > pages * len)
368 return -EINVAL;
369 }
370
371 res = part->master->_read_oob(part->master, from + part->offset, ops);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100372 if (unlikely(res)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000373 if (mtd_is_bitflip(res))
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100374 mtd->ecc_stats.corrected++;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000375 if (mtd_is_eccerr(res))
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100376 mtd->ecc_stats.failed++;
377 }
378 return res;
379}
380
Stefan Roese586b3a62009-05-11 16:03:55 +0200381static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
382 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100383{
384 struct mtd_part *part = PART(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200385 return part->master->_read_user_prot_reg(part->master, from, len,
386 retlen, buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100387}
388
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200389static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
390 size_t *retlen, struct otp_info *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100391{
392 struct mtd_part *part = PART(mtd);
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200393 return part->master->_get_user_prot_info(part->master, len, retlen,
394 buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100395}
396
Stefan Roese586b3a62009-05-11 16:03:55 +0200397static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
398 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100399{
400 struct mtd_part *part = PART(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200401 return part->master->_read_fact_prot_reg(part->master, from, len,
402 retlen, buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100403}
404
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200405static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
406 size_t *retlen, struct otp_info *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100407{
408 struct mtd_part *part = PART(mtd);
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200409 return part->master->_get_fact_prot_info(part->master, len, retlen,
410 buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100411}
412
Stefan Roese586b3a62009-05-11 16:03:55 +0200413static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
414 size_t *retlen, const u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100415{
416 struct mtd_part *part = PART(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200417 return part->master->_write(part->master, to + part->offset, len,
418 retlen, buf);
419}
420
421static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
422 size_t *retlen, const u_char *buf)
423{
424 struct mtd_part *part = PART(mtd);
425 return part->master->_panic_write(part->master, to + part->offset, len,
426 retlen, buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100427}
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100428
429static int part_write_oob(struct mtd_info *mtd, loff_t to,
Stefan Roese586b3a62009-05-11 16:03:55 +0200430 struct mtd_oob_ops *ops)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100431{
432 struct mtd_part *part = PART(mtd);
433
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100434 if (to >= mtd->size)
435 return -EINVAL;
436 if (ops->datbuf && to + ops->len > mtd->size)
437 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200438 return part->master->_write_oob(part->master, to + part->offset, ops);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100439}
440
Stefan Roese586b3a62009-05-11 16:03:55 +0200441static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
442 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100443{
444 struct mtd_part *part = PART(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200445 return part->master->_write_user_prot_reg(part->master, from, len,
446 retlen, buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100447}
448
Stefan Roese586b3a62009-05-11 16:03:55 +0200449static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
450 size_t len)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100451{
452 struct mtd_part *part = PART(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200453 return part->master->_lock_user_prot_reg(part->master, from, len);
454}
455
456#ifndef __UBOOT__
457static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
458 unsigned long count, loff_t to, size_t *retlen)
459{
460 struct mtd_part *part = PART(mtd);
461 return part->master->_writev(part->master, vecs, count,
462 to + part->offset, retlen);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100463}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200464#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100465
Stefan Roese586b3a62009-05-11 16:03:55 +0200466static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100467{
468 struct mtd_part *part = PART(mtd);
469 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000470
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100471 instr->addr += part->offset;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200472 ret = part->master->_erase(part->master, instr);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100473 if (ret) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200474 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100475 instr->fail_addr -= part->offset;
476 instr->addr -= part->offset;
477 }
478 return ret;
479}
480
481void mtd_erase_callback(struct erase_info *instr)
482{
Sergey Lapin3a38a552013-01-14 03:46:50 +0000483 if (instr->mtd->_erase == part_erase) {
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100484 struct mtd_part *part = PART(instr->mtd);
485
Stefan Roese586b3a62009-05-11 16:03:55 +0200486 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100487 instr->fail_addr -= part->offset;
488 instr->addr -= part->offset;
489 }
490 if (instr->callback)
491 instr->callback(instr);
492}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200493EXPORT_SYMBOL_GPL(mtd_erase_callback);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100494
Stefan Roese586b3a62009-05-11 16:03:55 +0200495static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100496{
497 struct mtd_part *part = PART(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200498 return part->master->_lock(part->master, ofs + part->offset, len);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100499}
500
Stefan Roese586b3a62009-05-11 16:03:55 +0200501static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100502{
503 struct mtd_part *part = PART(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200504 return part->master->_unlock(part->master, ofs + part->offset, len);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100505}
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100506
Heiko Schocherf5895d12014-06-24 10:10:04 +0200507static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
508{
509 struct mtd_part *part = PART(mtd);
510 return part->master->_is_locked(part->master, ofs + part->offset, len);
511}
512
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100513static void part_sync(struct mtd_info *mtd)
514{
515 struct mtd_part *part = PART(mtd);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200516 part->master->_sync(part->master);
517}
518
519#ifndef __UBOOT__
520static int part_suspend(struct mtd_info *mtd)
521{
522 struct mtd_part *part = PART(mtd);
523 return part->master->_suspend(part->master);
524}
525
526static void part_resume(struct mtd_info *mtd)
527{
528 struct mtd_part *part = PART(mtd);
529 part->master->_resume(part->master);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100530}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200531#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100532
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300533static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
534{
535 struct mtd_part *part = PART(mtd);
536 ofs += part->offset;
537 return part->master->_block_isreserved(part->master, ofs);
538}
539
Stefan Roese586b3a62009-05-11 16:03:55 +0200540static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100541{
542 struct mtd_part *part = PART(mtd);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100543 ofs += part->offset;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200544 return part->master->_block_isbad(part->master, ofs);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100545}
546
Stefan Roese586b3a62009-05-11 16:03:55 +0200547static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100548{
549 struct mtd_part *part = PART(mtd);
550 int res;
551
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100552 ofs += part->offset;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200553 res = part->master->_block_markbad(part->master, ofs);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100554 if (!res)
555 mtd->ecc_stats.badblocks++;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100556 return res;
557}
558
Heiko Schocherf5895d12014-06-24 10:10:04 +0200559static inline void free_partition(struct mtd_part *p)
560{
561 kfree(p->mtd.name);
562 kfree(p);
563}
564
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100565/*
566 * This function unregisters and destroy all slave MTD objects which are
567 * attached to the given master MTD object.
568 */
569
570int del_mtd_partitions(struct mtd_info *master)
571{
Stefan Roese586b3a62009-05-11 16:03:55 +0200572 struct mtd_part *slave, *next;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200573 int ret, err = 0;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100574
Miquel Raynalf78a12b2018-08-16 17:30:19 +0200575 debug("Deleting MTD partitions on \"%s\":\n", master->name);
576
Heiko Schocherf5895d12014-06-24 10:10:04 +0200577 mutex_lock(&mtd_partitions_mutex);
Stefan Roese586b3a62009-05-11 16:03:55 +0200578 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100579 if (slave->master == master) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200580 ret = del_mtd_device(&slave->mtd);
581 if (ret < 0) {
582 err = ret;
583 continue;
584 }
Stefan Roese586b3a62009-05-11 16:03:55 +0200585 list_del(&slave->list);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200586 free_partition(slave);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100587 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200588 mutex_unlock(&mtd_partitions_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100589
Heiko Schocherf5895d12014-06-24 10:10:04 +0200590 return err;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100591}
592
Heiko Schocherf5895d12014-06-24 10:10:04 +0200593static struct mtd_part *allocate_partition(struct mtd_info *master,
594 const struct mtd_partition *part, int partno,
595 uint64_t cur_offset)
Stefan Roese586b3a62009-05-11 16:03:55 +0200596{
597 struct mtd_part *slave;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200598 char *name;
Stefan Roese586b3a62009-05-11 16:03:55 +0200599
600 /* allocate the partition structure */
601 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200602 name = kstrdup(part->name, GFP_KERNEL);
603 if (!name || !slave) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200604 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200605 master->name);
606 kfree(name);
607 kfree(slave);
608 return ERR_PTR(-ENOMEM);
Stefan Roese586b3a62009-05-11 16:03:55 +0200609 }
Stefan Roese586b3a62009-05-11 16:03:55 +0200610
611 /* set up the MTD object for this partition */
612 slave->mtd.type = master->type;
613 slave->mtd.flags = master->flags & ~part->mask_flags;
614 slave->mtd.size = part->size;
615 slave->mtd.writesize = master->writesize;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200616 slave->mtd.writebufsize = master->writebufsize;
Stefan Roese586b3a62009-05-11 16:03:55 +0200617 slave->mtd.oobsize = master->oobsize;
618 slave->mtd.oobavail = master->oobavail;
619 slave->mtd.subpage_sft = master->subpage_sft;
620
Heiko Schocherf5895d12014-06-24 10:10:04 +0200621 slave->mtd.name = name;
Stefan Roese586b3a62009-05-11 16:03:55 +0200622 slave->mtd.owner = master->owner;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200623#ifndef __UBOOT__
624 slave->mtd.backing_dev_info = master->backing_dev_info;
625
626 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
627 * to have the same data be in two different partitions.
628 */
629 slave->mtd.dev.parent = master->dev.parent;
630#endif
Stefan Roese586b3a62009-05-11 16:03:55 +0200631
Boris Brezillon6c20df72018-08-16 17:29:59 +0200632 if (master->_read)
633 slave->mtd._read = part_read;
634 if (master->_write)
635 slave->mtd._write = part_write;
Stefan Roese586b3a62009-05-11 16:03:55 +0200636
Heiko Schocherf5895d12014-06-24 10:10:04 +0200637 if (master->_panic_write)
638 slave->mtd._panic_write = part_panic_write;
639
640#ifndef __UBOOT__
641 if (master->_point && master->_unpoint) {
642 slave->mtd._point = part_point;
643 slave->mtd._unpoint = part_unpoint;
644 }
645#endif
646
647 if (master->_get_unmapped_area)
648 slave->mtd._get_unmapped_area = part_get_unmapped_area;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000649 if (master->_read_oob)
650 slave->mtd._read_oob = part_read_oob;
651 if (master->_write_oob)
652 slave->mtd._write_oob = part_write_oob;
653 if (master->_read_user_prot_reg)
654 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
655 if (master->_read_fact_prot_reg)
656 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
657 if (master->_write_user_prot_reg)
658 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
659 if (master->_lock_user_prot_reg)
660 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
661 if (master->_get_user_prot_info)
662 slave->mtd._get_user_prot_info = part_get_user_prot_info;
663 if (master->_get_fact_prot_info)
664 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
665 if (master->_sync)
666 slave->mtd._sync = part_sync;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200667#ifndef __UBOOT__
668 if (!partno && !master->dev.class && master->_suspend &&
669 master->_resume) {
670 slave->mtd._suspend = part_suspend;
671 slave->mtd._resume = part_resume;
672 }
673 if (master->_writev)
674 slave->mtd._writev = part_writev;
675#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +0000676 if (master->_lock)
677 slave->mtd._lock = part_lock;
678 if (master->_unlock)
679 slave->mtd._unlock = part_unlock;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200680 if (master->_is_locked)
681 slave->mtd._is_locked = part_is_locked;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300682 if (master->_block_isreserved)
683 slave->mtd._block_isreserved = part_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000684 if (master->_block_isbad)
685 slave->mtd._block_isbad = part_block_isbad;
686 if (master->_block_markbad)
687 slave->mtd._block_markbad = part_block_markbad;
688 slave->mtd._erase = part_erase;
Stefan Roese586b3a62009-05-11 16:03:55 +0200689 slave->master = master;
690 slave->offset = part->offset;
Stefan Roese586b3a62009-05-11 16:03:55 +0200691
692 if (slave->offset == MTDPART_OFS_APPEND)
693 slave->offset = cur_offset;
694 if (slave->offset == MTDPART_OFS_NXTBLK) {
695 slave->offset = cur_offset;
696 if (mtd_mod_by_eb(cur_offset, master) != 0) {
697 /* Round up to next erasesize */
698 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200699 debug("Moving partition %d: "
700 "0x%012llx -> 0x%012llx\n", partno,
701 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
702 }
703 }
704 if (slave->offset == MTDPART_OFS_RETAIN) {
705 slave->offset = cur_offset;
706 if (master->size - slave->offset >= slave->mtd.size) {
707 slave->mtd.size = master->size - slave->offset
708 - slave->mtd.size;
709 } else {
710 debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
711 part->name, master->size - slave->offset,
712 slave->mtd.size);
713 /* register to preserve ordering */
714 goto out_register;
Stefan Roese586b3a62009-05-11 16:03:55 +0200715 }
716 }
717 if (slave->mtd.size == MTDPART_SIZ_FULL)
718 slave->mtd.size = master->size - slave->offset;
719
Heiko Schocherf5895d12014-06-24 10:10:04 +0200720 debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
721 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Stefan Roese586b3a62009-05-11 16:03:55 +0200722
723 /* let's do some sanity checks */
724 if (slave->offset >= master->size) {
725 /* let's register it anyway to preserve ordering */
726 slave->offset = 0;
727 slave->mtd.size = 0;
728 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
729 part->name);
730 goto out_register;
731 }
732 if (slave->offset + slave->mtd.size > master->size) {
733 slave->mtd.size = master->size - slave->offset;
734 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
735 part->name, master->name, (unsigned long long)slave->mtd.size);
736 }
737 if (master->numeraseregions > 1) {
738 /* Deal with variable erase size stuff */
739 int i, max = master->numeraseregions;
740 u64 end = slave->offset + slave->mtd.size;
741 struct mtd_erase_region_info *regions = master->eraseregions;
742
743 /* Find the first erase regions which is part of this
744 * partition. */
745 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
746 ;
747 /* The loop searched for the region _behind_ the first one */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200748 if (i > 0)
749 i--;
Stefan Roese586b3a62009-05-11 16:03:55 +0200750
751 /* Pick biggest erasesize */
752 for (; i < max && regions[i].offset < end; i++) {
753 if (slave->mtd.erasesize < regions[i].erasesize) {
754 slave->mtd.erasesize = regions[i].erasesize;
755 }
756 }
757 BUG_ON(slave->mtd.erasesize == 0);
758 } else {
759 /* Single erase size */
760 slave->mtd.erasesize = master->erasesize;
761 }
762
763 if ((slave->mtd.flags & MTD_WRITEABLE) &&
764 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
765 /* Doesn't start on a boundary of major erase size */
766 /* FIXME: Let it be writable if it is on a boundary of
767 * _minor_ erase size though */
768 slave->mtd.flags &= ~MTD_WRITEABLE;
769 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
770 part->name);
771 }
772 if ((slave->mtd.flags & MTD_WRITEABLE) &&
773 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
774 slave->mtd.flags &= ~MTD_WRITEABLE;
775 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
776 part->name);
777 }
778
779 slave->mtd.ecclayout = master->ecclayout;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200780 slave->mtd.ecc_step_size = master->ecc_step_size;
781 slave->mtd.ecc_strength = master->ecc_strength;
782 slave->mtd.bitflip_threshold = master->bitflip_threshold;
783
Sergey Lapin3a38a552013-01-14 03:46:50 +0000784 if (master->_block_isbad) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200785 uint64_t offs = 0;
786
787 while (offs < slave->mtd.size) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000788 if (mtd_block_isbad(master, offs + slave->offset))
Stefan Roese586b3a62009-05-11 16:03:55 +0200789 slave->mtd.ecc_stats.badblocks++;
790 offs += slave->mtd.erasesize;
791 }
792 }
793
794out_register:
Stefan Roese586b3a62009-05-11 16:03:55 +0200795 return slave;
796}
797
Heiko Schocherb24c4272014-07-15 16:08:42 +0200798#ifndef __UBOOT__
Heiko Schocherf5895d12014-06-24 10:10:04 +0200799int mtd_add_partition(struct mtd_info *master, const char *name,
800 long long offset, long long length)
801{
802 struct mtd_partition part;
803 struct mtd_part *p, *new;
804 uint64_t start, end;
805 int ret = 0;
806
807 /* the direct offset is expected */
808 if (offset == MTDPART_OFS_APPEND ||
809 offset == MTDPART_OFS_NXTBLK)
810 return -EINVAL;
811
812 if (length == MTDPART_SIZ_FULL)
813 length = master->size - offset;
814
815 if (length <= 0)
816 return -EINVAL;
817
818 part.name = name;
819 part.size = length;
820 part.offset = offset;
821 part.mask_flags = 0;
822 part.ecclayout = NULL;
823
824 new = allocate_partition(master, &part, -1, offset);
825 if (IS_ERR(new))
826 return PTR_ERR(new);
827
828 start = offset;
829 end = offset + length;
830
831 mutex_lock(&mtd_partitions_mutex);
832 list_for_each_entry(p, &mtd_partitions, list)
833 if (p->master == master) {
834 if ((start >= p->offset) &&
835 (start < (p->offset + p->mtd.size)))
836 goto err_inv;
837
838 if ((end >= p->offset) &&
839 (end < (p->offset + p->mtd.size)))
840 goto err_inv;
841 }
842
843 list_add(&new->list, &mtd_partitions);
844 mutex_unlock(&mtd_partitions_mutex);
845
846 add_mtd_device(&new->mtd);
847
848 return ret;
849err_inv:
850 mutex_unlock(&mtd_partitions_mutex);
851 free_partition(new);
852 return -EINVAL;
853}
854EXPORT_SYMBOL_GPL(mtd_add_partition);
855
856int mtd_del_partition(struct mtd_info *master, int partno)
857{
858 struct mtd_part *slave, *next;
859 int ret = -EINVAL;
860
861 mutex_lock(&mtd_partitions_mutex);
862 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
863 if ((slave->master == master) &&
864 (slave->mtd.index == partno)) {
865 ret = del_mtd_device(&slave->mtd);
866 if (ret < 0)
867 break;
868
869 list_del(&slave->list);
870 free_partition(slave);
871 break;
872 }
873 mutex_unlock(&mtd_partitions_mutex);
874
875 return ret;
876}
877EXPORT_SYMBOL_GPL(mtd_del_partition);
Heiko Schocherb24c4272014-07-15 16:08:42 +0200878#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200879
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100880/*
881 * This function, given a master MTD object and a partition table, creates
882 * and registers slave MTD objects which are bound to the master according to
883 * the partition definitions.
Stefan Roese586b3a62009-05-11 16:03:55 +0200884 *
885 * We don't register the master, or expect the caller to have done so,
886 * for reasons of data integrity.
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100887 */
888
889int add_mtd_partitions(struct mtd_info *master,
890 const struct mtd_partition *parts,
891 int nbparts)
892{
893 struct mtd_part *slave;
Stefan Roese586b3a62009-05-11 16:03:55 +0200894 uint64_t cur_offset = 0;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100895 int i;
896
Heiko Schocherf5895d12014-06-24 10:10:04 +0200897#ifdef __UBOOT__
Stefan Roese61379202008-11-27 14:05:15 +0100898 /*
899 * Need to init the list here, since LIST_INIT() does not
900 * work on platforms where relocation has problems (like MIPS
901 * & PPC).
902 */
903 if (mtd_partitions.next == NULL)
904 INIT_LIST_HEAD(&mtd_partitions);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200905#endif
Stefan Roese61379202008-11-27 14:05:15 +0100906
Joe Hershberger47550fc2013-04-08 10:32:49 +0000907 debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100908
909 for (i = 0; i < nbparts; i++) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200910 slave = allocate_partition(master, parts + i, i, cur_offset);
911 if (IS_ERR(slave))
912 return PTR_ERR(slave);
913
914 mutex_lock(&mtd_partitions_mutex);
915 list_add(&slave->list, &mtd_partitions);
916 mutex_unlock(&mtd_partitions_mutex);
917
918 add_mtd_device(&slave->mtd);
919
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100920 cur_offset = slave->offset + slave->mtd.size;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100921 }
922
923 return 0;
924}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200925
926#ifndef __UBOOT__
927static DEFINE_SPINLOCK(part_parser_lock);
928static LIST_HEAD(part_parsers);
929
930static struct mtd_part_parser *get_partition_parser(const char *name)
931{
932 struct mtd_part_parser *p, *ret = NULL;
933
934 spin_lock(&part_parser_lock);
935
936 list_for_each_entry(p, &part_parsers, list)
937 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
938 ret = p;
939 break;
940 }
941
942 spin_unlock(&part_parser_lock);
943
944 return ret;
945}
946
947#define put_partition_parser(p) do { module_put((p)->owner); } while (0)
948
949void register_mtd_parser(struct mtd_part_parser *p)
950{
951 spin_lock(&part_parser_lock);
952 list_add(&p->list, &part_parsers);
953 spin_unlock(&part_parser_lock);
954}
955EXPORT_SYMBOL_GPL(register_mtd_parser);
956
957void deregister_mtd_parser(struct mtd_part_parser *p)
958{
959 spin_lock(&part_parser_lock);
960 list_del(&p->list);
961 spin_unlock(&part_parser_lock);
962}
963EXPORT_SYMBOL_GPL(deregister_mtd_parser);
964
965/*
966 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
967 * are changing this array!
968 */
969static const char * const default_mtd_part_types[] = {
970 "cmdlinepart",
971 "ofpart",
972 NULL
973};
974
975/**
976 * parse_mtd_partitions - parse MTD partitions
977 * @master: the master partition (describes whole MTD device)
978 * @types: names of partition parsers to try or %NULL
979 * @pparts: array of partitions found is returned here
980 * @data: MTD partition parser-specific data
981 *
982 * This function tries to find partition on MTD device @master. It uses MTD
983 * partition parsers, specified in @types. However, if @types is %NULL, then
984 * the default list of parsers is used. The default list contains only the
985 * "cmdlinepart" and "ofpart" parsers ATM.
986 * Note: If there are more then one parser in @types, the kernel only takes the
987 * partitions parsed out by the first parser.
988 *
989 * This function may return:
990 * o a negative error code in case of failure
991 * o zero if no partitions were found
992 * o a positive number of found partitions, in which case on exit @pparts will
993 * point to an array containing this number of &struct mtd_info objects.
994 */
995int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
996 struct mtd_partition **pparts,
997 struct mtd_part_parser_data *data)
998{
999 struct mtd_part_parser *parser;
1000 int ret = 0;
1001
1002 if (!types)
1003 types = default_mtd_part_types;
1004
1005 for ( ; ret <= 0 && *types; types++) {
1006 parser = get_partition_parser(*types);
1007 if (!parser && !request_module("%s", *types))
1008 parser = get_partition_parser(*types);
1009 if (!parser)
1010 continue;
1011 ret = (*parser->parse_fn)(master, pparts, data);
1012 put_partition_parser(parser);
1013 if (ret > 0) {
1014 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
1015 ret, parser->name, master->name);
1016 break;
1017 }
1018 }
1019 return ret;
1020}
1021#endif
1022
1023int mtd_is_partition(const struct mtd_info *mtd)
1024{
1025 struct mtd_part *part;
1026 int ispart = 0;
1027
1028 mutex_lock(&mtd_partitions_mutex);
1029 list_for_each_entry(part, &mtd_partitions, list)
1030 if (&part->mtd == mtd) {
1031 ispart = 1;
1032 break;
1033 }
1034 mutex_unlock(&mtd_partitions_mutex);
1035
1036 return ispart;
1037}
1038EXPORT_SYMBOL_GPL(mtd_is_partition);
1039
1040/* Returns the size of the entire flash chip */
1041uint64_t mtd_get_device_size(const struct mtd_info *mtd)
1042{
1043 if (!mtd_is_partition(mtd))
1044 return mtd->size;
1045
1046 return PART(mtd)->master->size;
1047}
1048EXPORT_SYMBOL_GPL(mtd_get_device_size);