blob: 842d3e7274e2386d1193ddf19f412b7d61793a79 [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__
Simon Glass0f2af882020-05-10 11:40:05 -060012#include <log.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070013#include <dm/devres.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020014#include <linux/module.h>
15#include <linux/types.h>
16#include <linux/kernel.h>
17#include <linux/slab.h>
18#include <linux/list.h>
19#include <linux/kmod.h>
20#endif
21
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010022#include <malloc.h>
Alexey Romanovd5119672024-07-18 08:46:04 +030023#include <memalign.h>
24#include <part.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060025#include <linux/bug.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090026#include <linux/errno.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020027#include <linux/compat.h>
28#include <ubi_uboot.h>
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010029
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010030#include <linux/mtd/mtd.h>
31#include <linux/mtd/partitions.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020032#include <linux/err.h>
Miquel Raynal6f381d52018-09-29 12:58:25 +020033#include <linux/sizes.h>
Heiko Schocherf5895d12014-06-24 10:10:04 +020034
35#include "mtdcore.h"
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010036
Heiko Schocherf5895d12014-06-24 10:10:04 +020037#ifndef __UBOOT__
38static DEFINE_MUTEX(mtd_partitions_mutex);
39#else
40DEFINE_MUTEX(mtd_partitions_mutex);
41#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +010042
Heiko Schocherf5895d12014-06-24 10:10:04 +020043#ifdef __UBOOT__
44/* from mm/util.c */
45
46/**
47 * kstrdup - allocate space for and copy an existing string
48 * @s: the string to duplicate
49 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
50 */
51char *kstrdup(const char *s, gfp_t gfp)
52{
53 size_t len;
54 char *buf;
55
56 if (!s)
57 return NULL;
58
59 len = strlen(s) + 1;
60 buf = kmalloc(len, gfp);
61 if (buf)
62 memcpy(buf, s, len);
63 return buf;
64}
65#endif
66
Miquel Raynal6f381d52018-09-29 12:58:25 +020067#define MTD_SIZE_REMAINING (~0LLU)
68#define MTD_OFFSET_NOT_SPECIFIED (~0LLU)
69
Boris Brezillon7dfad312018-12-02 10:54:30 +010070bool mtd_partitions_used(struct mtd_info *master)
71{
72 struct mtd_info *slave;
73
74 list_for_each_entry(slave, &master->partitions, node) {
75 if (slave->usecount)
76 return true;
77 }
78
79 return false;
80}
81
Miquel Raynal6f381d52018-09-29 12:58:25 +020082/**
83 * mtd_parse_partition - Parse @mtdparts partition definition, fill @partition
84 * with it and update the @mtdparts string pointer.
85 *
86 * The partition name is allocated and must be freed by the caller.
87 *
88 * This function is widely inspired from part_parse (mtdparts.c).
89 *
90 * @mtdparts: String describing the partition with mtdparts command syntax
91 * @partition: MTD partition structure to fill
92 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +010093 * Return: 0 on success, an error otherwise.
Miquel Raynal6f381d52018-09-29 12:58:25 +020094 */
95static int mtd_parse_partition(const char **_mtdparts,
96 struct mtd_partition *partition)
97{
98 const char *mtdparts = *_mtdparts;
99 const char *name = NULL;
100 int name_len;
101 char *buf;
102
103 /* Ensure the partition structure is empty */
104 memset(partition, 0, sizeof(struct mtd_partition));
105
106 /* Fetch the partition size */
107 if (*mtdparts == '-') {
108 /* Assign all remaining space to this partition */
109 partition->size = MTD_SIZE_REMAINING;
110 mtdparts++;
111 } else {
112 partition->size = ustrtoull(mtdparts, (char **)&mtdparts, 0);
113 if (partition->size < SZ_4K) {
114 printf("Minimum partition size 4kiB, %lldB requested\n",
115 partition->size);
116 return -EINVAL;
117 }
118 }
119
120 /* Check for the offset */
121 partition->offset = MTD_OFFSET_NOT_SPECIFIED;
122 if (*mtdparts == '@') {
123 mtdparts++;
124 partition->offset = ustrtoull(mtdparts, (char **)&mtdparts, 0);
125 }
126
127 /* Now look for the name */
128 if (*mtdparts == '(') {
129 name = ++mtdparts;
130 mtdparts = strchr(name, ')');
131 if (!mtdparts) {
132 printf("No closing ')' found in partition name\n");
133 return -EINVAL;
134 }
135 name_len = mtdparts - name + 1;
136 if ((name_len - 1) == 0) {
137 printf("Empty partition name\n");
138 return -EINVAL;
139 }
140 mtdparts++;
141 } else {
142 /* Name will be of the form size@offset */
143 name_len = 22;
144 }
145
146 /* Check if the partition is read-only */
147 if (strncmp(mtdparts, "ro", 2) == 0) {
148 partition->mask_flags |= MTD_WRITEABLE;
149 mtdparts += 2;
150 }
151
152 /* Check for a potential next partition definition */
153 if (*mtdparts == ',') {
154 if (partition->size == MTD_SIZE_REMAINING) {
155 printf("No partitions allowed after a fill-up\n");
156 return -EINVAL;
157 }
158 ++mtdparts;
159 } else if ((*mtdparts == ';') || (*mtdparts == '\0')) {
160 /* NOP */
161 } else {
162 printf("Unexpected character '%c' in mtdparts\n", *mtdparts);
163 return -EINVAL;
164 }
165
166 /*
167 * Allocate a buffer for the name and either copy the provided name or
168 * auto-generate it with the form 'size@offset'.
169 */
170 buf = malloc(name_len);
171 if (!buf)
172 return -ENOMEM;
173
174 if (name)
175 strncpy(buf, name, name_len - 1);
176 else
177 snprintf(buf, name_len, "0x%08llx@0x%08llx",
178 partition->size, partition->offset);
179
180 buf[name_len - 1] = '\0';
181 partition->name = buf;
182
183 *_mtdparts = mtdparts;
184
185 return 0;
186}
187
188/**
189 * mtd_parse_partitions - Create a partition array from an mtdparts definition
190 *
191 * Stateless function that takes a @parent MTD device, a string @_mtdparts
192 * describing the partitions (with the "mtdparts" command syntax) and creates
193 * the corresponding MTD partition structure array @_parts. Both the name and
194 * the structure partition itself must be freed freed, the caller may use
195 * @mtd_free_parsed_partitions() for this purpose.
196 *
197 * @parent: MTD device which contains the partitions
198 * @_mtdparts: Pointer to a string describing the partitions with "mtdparts"
199 * command syntax.
200 * @_parts: Allocated array containing the partitions, must be freed by the
201 * caller.
202 * @_nparts: Size of @_parts array.
203 *
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100204 * Return: 0 on success, an error otherwise.
Miquel Raynal6f381d52018-09-29 12:58:25 +0200205 */
206int mtd_parse_partitions(struct mtd_info *parent, const char **_mtdparts,
207 struct mtd_partition **_parts, int *_nparts)
208{
209 struct mtd_partition partition = {}, *parts;
210 const char *mtdparts = *_mtdparts;
Alexander Stein12aead02025-02-20 15:58:07 +0100211 uint64_t cur_off = 0;
Miquel Raynal6f381d52018-09-29 12:58:25 +0200212 int nparts = 0;
213 int ret, idx;
214 u64 sz;
215
216 /* First, iterate over the partitions until we know their number */
217 while (mtdparts[0] != '\0' && mtdparts[0] != ';') {
218 ret = mtd_parse_partition(&mtdparts, &partition);
219 if (ret)
220 return ret;
221
222 free((char *)partition.name);
223 nparts++;
224 }
225
226 /* Allocate an array of partitions to give back to the caller */
227 parts = malloc(sizeof(*parts) * nparts);
228 if (!parts) {
229 printf("Not enough space to save partitions meta-data\n");
230 return -ENOMEM;
231 }
232
233 /* Iterate again over each partition to save the data in our array */
234 for (idx = 0; idx < nparts; idx++) {
235 ret = mtd_parse_partition(_mtdparts, &parts[idx]);
236 if (ret)
237 return ret;
238
developer5e19d382025-04-25 11:43:03 +0800239 if (parts[idx].offset == MTD_OFFSET_NOT_SPECIFIED)
240 parts[idx].offset = cur_off;
241 cur_off += parts[idx].size;
242
Miquel Raynal6f381d52018-09-29 12:58:25 +0200243 if (parts[idx].size == MTD_SIZE_REMAINING)
Alexander Stein12aead02025-02-20 15:58:07 +0100244 parts[idx].size = parent->size - parts[idx].offset;
Miquel Raynal6f381d52018-09-29 12:58:25 +0200245
246 sz = parts[idx].size;
247 if (sz < parent->writesize || do_div(sz, parent->writesize)) {
248 printf("Partition size must be a multiple of %d\n",
249 parent->writesize);
250 return -EINVAL;
251 }
252
Miquel Raynal6f381d52018-09-29 12:58:25 +0200253 parts[idx].ecclayout = parent->ecclayout;
254 }
255
256 /* Offset by one mtdparts to point to the next device if any */
257 if (*_mtdparts[0] == ';')
258 (*_mtdparts)++;
259
260 *_parts = parts;
261 *_nparts = nparts;
262
263 return 0;
264}
265
266/**
267 * mtd_free_parsed_partitions - Free dynamically allocated partitions
268 *
269 * Each successful call to @mtd_parse_partitions must be followed by a call to
270 * @mtd_free_parsed_partitions to free any allocated array during the parsing
271 * process.
272 *
273 * @parts: Array containing the partitions that will be freed.
274 * @nparts: Size of @parts array.
275 */
276void mtd_free_parsed_partitions(struct mtd_partition *parts,
277 unsigned int nparts)
278{
279 int i;
280
281 for (i = 0; i < nparts; i++)
282 free((char *)parts[i].name);
283
284 free(parts);
285}
286
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100287/*
288 * MTD methods which simply translate the effective address and pass through
289 * to the _real_ device.
290 */
291
Stefan Roese586b3a62009-05-11 16:03:55 +0200292static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
293 size_t *retlen, u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100294{
Stefan Roese586b3a62009-05-11 16:03:55 +0200295 struct mtd_ecc_stats stats;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100296 int res;
297
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200298 stats = mtd->parent->ecc_stats;
299 res = mtd->parent->_read(mtd->parent, from + mtd->offset, len,
300 retlen, buf);
Paul Burton700a76c2013-09-04 15:16:56 +0100301 if (unlikely(mtd_is_eccerr(res)))
302 mtd->ecc_stats.failed +=
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200303 mtd->parent->ecc_stats.failed - stats.failed;
Paul Burton700a76c2013-09-04 15:16:56 +0100304 else
305 mtd->ecc_stats.corrected +=
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200306 mtd->parent->ecc_stats.corrected - stats.corrected;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100307 return res;
308}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200309
310#ifndef __UBOOT__
311static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
312 size_t *retlen, void **virt, resource_size_t *phys)
313{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200314 return mtd->parent->_point(mtd->parent, from + mtd->offset, len,
315 retlen, virt, phys);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200316}
317
318static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
319{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200320 return mtd->parent->_unpoint(mtd->parent, from + mtd->offset, len);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200321}
322#endif
323
324static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
325 unsigned long len,
326 unsigned long offset,
327 unsigned long flags)
328{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200329 offset += mtd->offset;
330 return mtd->parent->_get_unmapped_area(mtd->parent, len, offset, flags);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200331}
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100332
333static int part_read_oob(struct mtd_info *mtd, loff_t from,
Stefan Roese586b3a62009-05-11 16:03:55 +0200334 struct mtd_oob_ops *ops)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100335{
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100336 int res;
337
338 if (from >= mtd->size)
339 return -EINVAL;
340 if (ops->datbuf && from + ops->len > mtd->size)
341 return -EINVAL;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200342
343 /*
344 * If OOB is also requested, make sure that we do not read past the end
345 * of this partition.
346 */
347 if (ops->oobbuf) {
348 size_t len, pages;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100349
Heiko Schocherf5895d12014-06-24 10:10:04 +0200350 if (ops->mode == MTD_OPS_AUTO_OOB)
351 len = mtd->oobavail;
352 else
353 len = mtd->oobsize;
354 pages = mtd_div_by_ws(mtd->size, mtd);
355 pages -= mtd_div_by_ws(from, mtd);
356 if (ops->ooboffs + ops->ooblen > pages * len)
357 return -EINVAL;
358 }
359
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200360 res = mtd->parent->_read_oob(mtd->parent, from + mtd->offset, ops);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100361 if (unlikely(res)) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000362 if (mtd_is_bitflip(res))
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100363 mtd->ecc_stats.corrected++;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000364 if (mtd_is_eccerr(res))
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100365 mtd->ecc_stats.failed++;
366 }
367 return res;
368}
369
Stefan Roese586b3a62009-05-11 16:03:55 +0200370static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
371 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100372{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200373 return mtd->parent->_read_user_prot_reg(mtd->parent, from, len,
374 retlen, buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100375}
376
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200377static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
378 size_t *retlen, struct otp_info *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100379{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200380 return mtd->parent->_get_user_prot_info(mtd->parent, len, retlen,
381 buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100382}
383
Stefan Roese586b3a62009-05-11 16:03:55 +0200384static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
385 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100386{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200387 return mtd->parent->_read_fact_prot_reg(mtd->parent, from, len,
388 retlen, buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100389}
390
Heiko Schocher081fe9e2014-07-15 16:08:43 +0200391static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
392 size_t *retlen, struct otp_info *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100393{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200394 return mtd->parent->_get_fact_prot_info(mtd->parent, len, retlen,
395 buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100396}
397
Stefan Roese586b3a62009-05-11 16:03:55 +0200398static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
399 size_t *retlen, const u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100400{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200401 return mtd->parent->_write(mtd->parent, to + mtd->offset, len,
402 retlen, buf);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200403}
404
405static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
406 size_t *retlen, const u_char *buf)
407{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200408 return mtd->parent->_panic_write(mtd->parent, to + mtd->offset, len,
409 retlen, buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100410}
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100411
412static int part_write_oob(struct mtd_info *mtd, loff_t to,
Stefan Roese586b3a62009-05-11 16:03:55 +0200413 struct mtd_oob_ops *ops)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100414{
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100415 if (to >= mtd->size)
416 return -EINVAL;
417 if (ops->datbuf && to + ops->len > mtd->size)
418 return -EINVAL;
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200419 return mtd->parent->_write_oob(mtd->parent, to + mtd->offset, ops);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100420}
421
Stefan Roese586b3a62009-05-11 16:03:55 +0200422static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
423 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100424{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200425 return mtd->parent->_write_user_prot_reg(mtd->parent, from, len,
426 retlen, buf);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100427}
428
Stefan Roese586b3a62009-05-11 16:03:55 +0200429static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
430 size_t len)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100431{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200432 return mtd->parent->_lock_user_prot_reg(mtd->parent, from, len);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200433}
434
435#ifndef __UBOOT__
436static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
437 unsigned long count, loff_t to, size_t *retlen)
438{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200439 return mtd->parent->_writev(mtd->parent, vecs, count,
440 to + mtd->offset, retlen);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100441}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200442#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100443
Stefan Roese586b3a62009-05-11 16:03:55 +0200444static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100445{
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100446 int ret;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000447
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200448 instr->addr += mtd->offset;
Marek Behún2e7d7622021-10-05 15:56:05 +0200449
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200450 ret = mtd->parent->_erase(mtd->parent, instr);
Marek Behún2e7d7622021-10-05 15:56:05 +0200451 if (ret && instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
452 instr->fail_addr -= mtd->offset;
453
454 instr->addr -= mtd->offset;
455
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100456 return ret;
457}
458
Stefan Roese586b3a62009-05-11 16:03:55 +0200459static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100460{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200461 return mtd->parent->_lock(mtd->parent, ofs + mtd->offset, len);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100462}
463
Stefan Roese586b3a62009-05-11 16:03:55 +0200464static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100465{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200466 return mtd->parent->_unlock(mtd->parent, ofs + mtd->offset, len);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100467}
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100468
Heiko Schocherf5895d12014-06-24 10:10:04 +0200469static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
470{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200471 return mtd->parent->_is_locked(mtd->parent, ofs + mtd->offset, len);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200472}
473
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100474static void part_sync(struct mtd_info *mtd)
475{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200476 mtd->parent->_sync(mtd->parent);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200477}
478
479#ifndef __UBOOT__
480static int part_suspend(struct mtd_info *mtd)
481{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200482 return mtd->parent->_suspend(mtd->parent);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200483}
484
485static void part_resume(struct mtd_info *mtd)
486{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200487 mtd->parent->_resume(mtd->parent);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100488}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200489#endif
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100490
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300491static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
492{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200493 ofs += mtd->offset;
494 return mtd->parent->_block_isreserved(mtd->parent, ofs);
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300495}
496
Stefan Roese586b3a62009-05-11 16:03:55 +0200497static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100498{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200499 ofs += mtd->offset;
500 return mtd->parent->_block_isbad(mtd->parent, ofs);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100501}
502
Stefan Roese586b3a62009-05-11 16:03:55 +0200503static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100504{
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100505 int res;
506
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200507 ofs += mtd->offset;
508 res = mtd->parent->_block_markbad(mtd->parent, ofs);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100509 if (!res)
510 mtd->ecc_stats.badblocks++;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100511 return res;
512}
513
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200514static inline void free_partition(struct mtd_info *p)
Heiko Schocherf5895d12014-06-24 10:10:04 +0200515{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200516 kfree(p->name);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200517 kfree(p);
518}
519
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100520/*
521 * This function unregisters and destroy all slave MTD objects which are
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200522 * attached to the given master MTD object, recursively.
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100523 */
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200524static int do_del_mtd_partitions(struct mtd_info *master)
525{
526 struct mtd_info *slave, *next;
527 int ret, err = 0;
528
529 list_for_each_entry_safe(slave, next, &master->partitions, node) {
530 if (mtd_has_partitions(slave))
531 del_mtd_partitions(slave);
532
533 debug("Deleting %s MTD partition\n", slave->name);
534 ret = del_mtd_device(slave);
535 if (ret < 0) {
536 printf("Error when deleting partition \"%s\" (%d)\n",
537 slave->name, ret);
538 err = ret;
539 continue;
540 }
541
542 list_del(&slave->node);
543 free_partition(slave);
544 }
545
546 return err;
547}
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100548
549int del_mtd_partitions(struct mtd_info *master)
550{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200551 int ret;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100552
Miquel Raynalf78a12b2018-08-16 17:30:19 +0200553 debug("Deleting MTD partitions on \"%s\":\n", master->name);
554
Heiko Schocherf5895d12014-06-24 10:10:04 +0200555 mutex_lock(&mtd_partitions_mutex);
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200556 ret = do_del_mtd_partitions(master);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200557 mutex_unlock(&mtd_partitions_mutex);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100558
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200559 return ret;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100560}
561
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200562static struct mtd_info *allocate_partition(struct mtd_info *master,
563 const struct mtd_partition *part,
564 int partno, uint64_t cur_offset)
Stefan Roese586b3a62009-05-11 16:03:55 +0200565{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200566 struct mtd_info *slave;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200567 char *name;
Stefan Roese586b3a62009-05-11 16:03:55 +0200568
569 /* allocate the partition structure */
570 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200571 name = kstrdup(part->name, GFP_KERNEL);
572 if (!name || !slave) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200573 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Heiko Schocherf5895d12014-06-24 10:10:04 +0200574 master->name);
575 kfree(name);
576 kfree(slave);
577 return ERR_PTR(-ENOMEM);
Stefan Roese586b3a62009-05-11 16:03:55 +0200578 }
Stefan Roese586b3a62009-05-11 16:03:55 +0200579
580 /* set up the MTD object for this partition */
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200581 slave->type = master->type;
582 slave->flags = master->flags & ~part->mask_flags;
583 slave->size = part->size;
584 slave->writesize = master->writesize;
585 slave->writebufsize = master->writebufsize;
586 slave->oobsize = master->oobsize;
587 slave->oobavail = master->oobavail;
588 slave->subpage_sft = master->subpage_sft;
Stefan Roese586b3a62009-05-11 16:03:55 +0200589
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200590 slave->name = name;
591 slave->owner = master->owner;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200592#ifndef __UBOOT__
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200593 slave->backing_dev_info = master->backing_dev_info;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200594
595 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
596 * to have the same data be in two different partitions.
597 */
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200598 slave->dev.parent = master->dev.parent;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200599#endif
Stefan Roese586b3a62009-05-11 16:03:55 +0200600
Boris Brezillon6c20df72018-08-16 17:29:59 +0200601 if (master->_read)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200602 slave->_read = part_read;
Boris Brezillon6c20df72018-08-16 17:29:59 +0200603 if (master->_write)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200604 slave->_write = part_write;
Stefan Roese586b3a62009-05-11 16:03:55 +0200605
Heiko Schocherf5895d12014-06-24 10:10:04 +0200606 if (master->_panic_write)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200607 slave->_panic_write = part_panic_write;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200608
609#ifndef __UBOOT__
610 if (master->_point && master->_unpoint) {
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200611 slave->_point = part_point;
612 slave->_unpoint = part_unpoint;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200613 }
614#endif
615
616 if (master->_get_unmapped_area)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200617 slave->_get_unmapped_area = part_get_unmapped_area;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000618 if (master->_read_oob)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200619 slave->_read_oob = part_read_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000620 if (master->_write_oob)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200621 slave->_write_oob = part_write_oob;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000622 if (master->_read_user_prot_reg)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200623 slave->_read_user_prot_reg = part_read_user_prot_reg;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000624 if (master->_read_fact_prot_reg)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200625 slave->_read_fact_prot_reg = part_read_fact_prot_reg;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000626 if (master->_write_user_prot_reg)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200627 slave->_write_user_prot_reg = part_write_user_prot_reg;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000628 if (master->_lock_user_prot_reg)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200629 slave->_lock_user_prot_reg = part_lock_user_prot_reg;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000630 if (master->_get_user_prot_info)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200631 slave->_get_user_prot_info = part_get_user_prot_info;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000632 if (master->_get_fact_prot_info)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200633 slave->_get_fact_prot_info = part_get_fact_prot_info;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000634 if (master->_sync)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200635 slave->_sync = part_sync;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200636#ifndef __UBOOT__
637 if (!partno && !master->dev.class && master->_suspend &&
638 master->_resume) {
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200639 slave->_suspend = part_suspend;
640 slave->_resume = part_resume;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200641 }
642 if (master->_writev)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200643 slave->_writev = part_writev;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200644#endif
Sergey Lapin3a38a552013-01-14 03:46:50 +0000645 if (master->_lock)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200646 slave->_lock = part_lock;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000647 if (master->_unlock)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200648 slave->_unlock = part_unlock;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200649 if (master->_is_locked)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200650 slave->_is_locked = part_is_locked;
Ezequiel Garciafc9d57c2014-05-21 19:06:12 -0300651 if (master->_block_isreserved)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200652 slave->_block_isreserved = part_block_isreserved;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000653 if (master->_block_isbad)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200654 slave->_block_isbad = part_block_isbad;
Sergey Lapin3a38a552013-01-14 03:46:50 +0000655 if (master->_block_markbad)
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200656 slave->_block_markbad = part_block_markbad;
657 slave->_erase = part_erase;
658 slave->parent = master;
Stefan Roese586b3a62009-05-11 16:03:55 +0200659 slave->offset = part->offset;
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200660 INIT_LIST_HEAD(&slave->partitions);
661 INIT_LIST_HEAD(&slave->node);
Stefan Roese586b3a62009-05-11 16:03:55 +0200662
663 if (slave->offset == MTDPART_OFS_APPEND)
664 slave->offset = cur_offset;
665 if (slave->offset == MTDPART_OFS_NXTBLK) {
666 slave->offset = cur_offset;
667 if (mtd_mod_by_eb(cur_offset, master) != 0) {
668 /* Round up to next erasesize */
669 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200670 debug("Moving partition %d: "
671 "0x%012llx -> 0x%012llx\n", partno,
672 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
673 }
674 }
675 if (slave->offset == MTDPART_OFS_RETAIN) {
676 slave->offset = cur_offset;
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200677 if (master->size - slave->offset >= slave->size) {
678 slave->size = master->size - slave->offset
679 - slave->size;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200680 } else {
681 debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
682 part->name, master->size - slave->offset,
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200683 slave->size);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200684 /* register to preserve ordering */
685 goto out_register;
Stefan Roese586b3a62009-05-11 16:03:55 +0200686 }
687 }
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200688 if (slave->size == MTDPART_SIZ_FULL)
689 slave->size = master->size - slave->offset;
Stefan Roese586b3a62009-05-11 16:03:55 +0200690
Heiko Schocherf5895d12014-06-24 10:10:04 +0200691 debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200692 (unsigned long long)(slave->offset + slave->size), slave->name);
Stefan Roese586b3a62009-05-11 16:03:55 +0200693
694 /* let's do some sanity checks */
695 if (slave->offset >= master->size) {
696 /* let's register it anyway to preserve ordering */
697 slave->offset = 0;
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200698 slave->size = 0;
Stefan Roese586b3a62009-05-11 16:03:55 +0200699 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
700 part->name);
701 goto out_register;
702 }
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200703 if (slave->offset + slave->size > master->size) {
704 slave->size = master->size - slave->offset;
Stefan Roese586b3a62009-05-11 16:03:55 +0200705 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200706 part->name, master->name, slave->size);
Stefan Roese586b3a62009-05-11 16:03:55 +0200707 }
708 if (master->numeraseregions > 1) {
709 /* Deal with variable erase size stuff */
710 int i, max = master->numeraseregions;
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200711 u64 end = slave->offset + slave->size;
Stefan Roese586b3a62009-05-11 16:03:55 +0200712 struct mtd_erase_region_info *regions = master->eraseregions;
713
714 /* Find the first erase regions which is part of this
715 * partition. */
716 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
717 ;
718 /* The loop searched for the region _behind_ the first one */
Heiko Schocherf5895d12014-06-24 10:10:04 +0200719 if (i > 0)
720 i--;
Stefan Roese586b3a62009-05-11 16:03:55 +0200721
722 /* Pick biggest erasesize */
723 for (; i < max && regions[i].offset < end; i++) {
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200724 if (slave->erasesize < regions[i].erasesize)
725 slave->erasesize = regions[i].erasesize;
Stefan Roese586b3a62009-05-11 16:03:55 +0200726 }
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200727 WARN_ON(slave->erasesize == 0);
Stefan Roese586b3a62009-05-11 16:03:55 +0200728 } else {
729 /* Single erase size */
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200730 slave->erasesize = master->erasesize;
Stefan Roese586b3a62009-05-11 16:03:55 +0200731 }
732
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200733 if ((slave->flags & MTD_WRITEABLE) &&
734 mtd_mod_by_eb(slave->offset, slave)) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200735 /* Doesn't start on a boundary of major erase size */
736 /* FIXME: Let it be writable if it is on a boundary of
737 * _minor_ erase size though */
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200738 slave->flags &= ~MTD_WRITEABLE;
Stefan Roese586b3a62009-05-11 16:03:55 +0200739 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
740 part->name);
741 }
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200742 if ((slave->flags & MTD_WRITEABLE) &&
743 mtd_mod_by_eb(slave->size, slave)) {
744 slave->flags &= ~MTD_WRITEABLE;
Stefan Roese586b3a62009-05-11 16:03:55 +0200745 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
746 part->name);
747 }
748
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200749 slave->ecclayout = master->ecclayout;
750 slave->ecc_step_size = master->ecc_step_size;
751 slave->ecc_strength = master->ecc_strength;
752 slave->bitflip_threshold = master->bitflip_threshold;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200753
Sergey Lapin3a38a552013-01-14 03:46:50 +0000754 if (master->_block_isbad) {
Stefan Roese586b3a62009-05-11 16:03:55 +0200755 uint64_t offs = 0;
756
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200757 while (offs < slave->size) {
Sergey Lapin3a38a552013-01-14 03:46:50 +0000758 if (mtd_block_isbad(master, offs + slave->offset))
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200759 slave->ecc_stats.badblocks++;
760 offs += slave->erasesize;
Stefan Roese586b3a62009-05-11 16:03:55 +0200761 }
762 }
763
764out_register:
Stefan Roese586b3a62009-05-11 16:03:55 +0200765 return slave;
766}
767
Heiko Schocherb24c4272014-07-15 16:08:42 +0200768#ifndef __UBOOT__
Heiko Schocherf5895d12014-06-24 10:10:04 +0200769int mtd_add_partition(struct mtd_info *master, const char *name,
770 long long offset, long long length)
771{
772 struct mtd_partition part;
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200773 struct mtd_info *p, *new;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200774 uint64_t start, end;
775 int ret = 0;
776
777 /* the direct offset is expected */
778 if (offset == MTDPART_OFS_APPEND ||
779 offset == MTDPART_OFS_NXTBLK)
780 return -EINVAL;
781
782 if (length == MTDPART_SIZ_FULL)
783 length = master->size - offset;
784
785 if (length <= 0)
786 return -EINVAL;
787
788 part.name = name;
789 part.size = length;
790 part.offset = offset;
791 part.mask_flags = 0;
792 part.ecclayout = NULL;
793
794 new = allocate_partition(master, &part, -1, offset);
795 if (IS_ERR(new))
796 return PTR_ERR(new);
797
798 start = offset;
799 end = offset + length;
800
801 mutex_lock(&mtd_partitions_mutex);
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200802 list_for_each_entry(p, &master->partitions, node) {
803 if (start >= p->offset &&
804 (start < (p->offset + p->size)))
805 goto err_inv;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200806
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200807 if (end >= p->offset &&
808 (end < (p->offset + p->size)))
809 goto err_inv;
810 }
Heiko Schocherf5895d12014-06-24 10:10:04 +0200811
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200812 list_add_tail(&new->node, &master->partitions);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200813 mutex_unlock(&mtd_partitions_mutex);
814
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200815 add_mtd_device(new);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200816
817 return ret;
818err_inv:
819 mutex_unlock(&mtd_partitions_mutex);
820 free_partition(new);
821 return -EINVAL;
822}
823EXPORT_SYMBOL_GPL(mtd_add_partition);
824
825int mtd_del_partition(struct mtd_info *master, int partno)
826{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200827 struct mtd_info *slave, *next;
Heiko Schocherf5895d12014-06-24 10:10:04 +0200828 int ret = -EINVAL;
829
830 mutex_lock(&mtd_partitions_mutex);
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200831 list_for_each_entry_safe(slave, next, &master->partitions, node)
832 if (slave->index == partno) {
833 ret = del_mtd_device(slave);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200834 if (ret < 0)
835 break;
836
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200837 list_del(&slave->node);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200838 free_partition(slave);
839 break;
840 }
841 mutex_unlock(&mtd_partitions_mutex);
842
843 return ret;
844}
845EXPORT_SYMBOL_GPL(mtd_del_partition);
Heiko Schocherb24c4272014-07-15 16:08:42 +0200846#endif
Heiko Schocherf5895d12014-06-24 10:10:04 +0200847
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100848/*
849 * This function, given a master MTD object and a partition table, creates
850 * and registers slave MTD objects which are bound to the master according to
851 * the partition definitions.
Stefan Roese586b3a62009-05-11 16:03:55 +0200852 *
853 * We don't register the master, or expect the caller to have done so,
854 * for reasons of data integrity.
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100855 */
856
857int add_mtd_partitions(struct mtd_info *master,
858 const struct mtd_partition *parts,
859 int nbparts)
860{
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200861 struct mtd_info *slave;
Stefan Roese586b3a62009-05-11 16:03:55 +0200862 uint64_t cur_offset = 0;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100863 int i;
864
Joe Hershberger47550fc2013-04-08 10:32:49 +0000865 debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100866
867 for (i = 0; i < nbparts; i++) {
Heiko Schocherf5895d12014-06-24 10:10:04 +0200868 slave = allocate_partition(master, parts + i, i, cur_offset);
869 if (IS_ERR(slave))
870 return PTR_ERR(slave);
871
872 mutex_lock(&mtd_partitions_mutex);
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200873 list_add_tail(&slave->node, &master->partitions);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200874 mutex_unlock(&mtd_partitions_mutex);
875
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200876 add_mtd_device(slave);
Heiko Schocherf5895d12014-06-24 10:10:04 +0200877
Miquel Raynal6382e0c2018-09-29 12:58:27 +0200878 cur_offset = slave->offset + slave->size;
Kyungmin Parkf6d5e252008-11-19 16:20:36 +0100879 }
880
881 return 0;
882}
Heiko Schocherf5895d12014-06-24 10:10:04 +0200883
Marek Behún544a8c92021-05-26 14:08:19 +0200884#if CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(OF_CONTROL)
885int add_mtd_partitions_of(struct mtd_info *master)
886{
887 ofnode parts, child;
888 int i = 0;
889
Patrice Chotardc1315f02022-03-21 09:13:37 +0100890 if (!master->dev && !ofnode_valid(master->flash_node))
Marek Behún544a8c92021-05-26 14:08:19 +0200891 return 0;
892
Patrice Chotardc1315f02022-03-21 09:13:37 +0100893 if (master->dev)
894 parts = ofnode_find_subnode(mtd_get_ofnode(master), "partitions");
895 else
896 parts = ofnode_find_subnode(master->flash_node, "partitions");
897
Simon Glass2e4938b2022-09-06 20:27:17 -0600898 if (!ofnode_valid(parts) || !ofnode_is_enabled(parts) ||
Marek Behún544a8c92021-05-26 14:08:19 +0200899 !ofnode_device_is_compatible(parts, "fixed-partitions"))
900 return 0;
901
902 ofnode_for_each_subnode(child, parts) {
903 struct mtd_partition part = { 0 };
904 struct mtd_info *slave;
Pali Rohár004ab832022-05-13 22:24:51 +0200905 fdt_addr_t offset;
906 fdt_size_t size;
Marek Behún544a8c92021-05-26 14:08:19 +0200907
Simon Glass2e4938b2022-09-06 20:27:17 -0600908 if (!ofnode_is_enabled(child))
Marek Behún544a8c92021-05-26 14:08:19 +0200909 continue;
910
911 offset = ofnode_get_addr_size_index_notrans(child, 0, &size);
Mike Looijmans4913b382025-04-08 07:46:38 +0200912 if (offset == FDT_ADDR_T_NONE) {
913 debug("Missing partition offset on \"%s\" partition\n",
Marek Behún544a8c92021-05-26 14:08:19 +0200914 master->name);
915 continue;
916 }
Mike Looijmans4913b382025-04-08 07:46:38 +0200917 if (size == MTDPART_SIZ_FULL)
918 size = master->size - offset;
Marek Behún544a8c92021-05-26 14:08:19 +0200919
920 part.name = ofnode_read_string(child, "label");
921 if (!part.name)
922 part.name = ofnode_read_string(child, "name");
923
924 /*
925 * .mask_flags is used to remove flags in allocate_partition(),
926 * so when "read-only" is present, we add MTD_WRITABLE to the
927 * mask, and so MTD_WRITABLE will be removed on partition
928 * allocation
929 */
930 if (ofnode_read_bool(child, "read-only"))
931 part.mask_flags |= MTD_WRITEABLE;
932 if (ofnode_read_bool(child, "lock"))
933 part.mask_flags |= MTD_POWERUP_LOCK;
934
935 part.offset = offset;
936 part.size = size;
937 part.ecclayout = master->ecclayout;
938
939 slave = allocate_partition(master, &part, i++, 0);
940 if (IS_ERR(slave))
941 return PTR_ERR(slave);
942
943 mutex_lock(&mtd_partitions_mutex);
944 list_add_tail(&slave->node, &master->partitions);
945 mutex_unlock(&mtd_partitions_mutex);
946
947 add_mtd_device(slave);
948 }
949
950 return 0;
951}
952#endif /* CONFIG_IS_ENABLED(DM) && CONFIG_IS_ENABLED(OF_CONTROL) */
953
Heiko Schocherf5895d12014-06-24 10:10:04 +0200954#ifndef __UBOOT__
955static DEFINE_SPINLOCK(part_parser_lock);
956static LIST_HEAD(part_parsers);
957
958static struct mtd_part_parser *get_partition_parser(const char *name)
959{
960 struct mtd_part_parser *p, *ret = NULL;
961
962 spin_lock(&part_parser_lock);
963
964 list_for_each_entry(p, &part_parsers, list)
965 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
966 ret = p;
967 break;
968 }
969
970 spin_unlock(&part_parser_lock);
971
972 return ret;
973}
974
975#define put_partition_parser(p) do { module_put((p)->owner); } while (0)
976
977void register_mtd_parser(struct mtd_part_parser *p)
978{
979 spin_lock(&part_parser_lock);
980 list_add(&p->list, &part_parsers);
981 spin_unlock(&part_parser_lock);
982}
983EXPORT_SYMBOL_GPL(register_mtd_parser);
984
985void deregister_mtd_parser(struct mtd_part_parser *p)
986{
987 spin_lock(&part_parser_lock);
988 list_del(&p->list);
989 spin_unlock(&part_parser_lock);
990}
991EXPORT_SYMBOL_GPL(deregister_mtd_parser);
992
993/*
994 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
995 * are changing this array!
996 */
997static const char * const default_mtd_part_types[] = {
998 "cmdlinepart",
999 "ofpart",
1000 NULL
1001};
1002
1003/**
1004 * parse_mtd_partitions - parse MTD partitions
1005 * @master: the master partition (describes whole MTD device)
1006 * @types: names of partition parsers to try or %NULL
1007 * @pparts: array of partitions found is returned here
1008 * @data: MTD partition parser-specific data
1009 *
1010 * This function tries to find partition on MTD device @master. It uses MTD
1011 * partition parsers, specified in @types. However, if @types is %NULL, then
1012 * the default list of parsers is used. The default list contains only the
1013 * "cmdlinepart" and "ofpart" parsers ATM.
1014 * Note: If there are more then one parser in @types, the kernel only takes the
1015 * partitions parsed out by the first parser.
1016 *
1017 * This function may return:
1018 * o a negative error code in case of failure
1019 * o zero if no partitions were found
1020 * o a positive number of found partitions, in which case on exit @pparts will
1021 * point to an array containing this number of &struct mtd_info objects.
1022 */
1023int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
1024 struct mtd_partition **pparts,
1025 struct mtd_part_parser_data *data)
1026{
1027 struct mtd_part_parser *parser;
1028 int ret = 0;
1029
1030 if (!types)
1031 types = default_mtd_part_types;
1032
1033 for ( ; ret <= 0 && *types; types++) {
1034 parser = get_partition_parser(*types);
1035 if (!parser && !request_module("%s", *types))
1036 parser = get_partition_parser(*types);
1037 if (!parser)
1038 continue;
1039 ret = (*parser->parse_fn)(master, pparts, data);
1040 put_partition_parser(parser);
1041 if (ret > 0) {
1042 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
1043 ret, parser->name, master->name);
1044 break;
1045 }
1046 }
1047 return ret;
1048}
1049#endif
1050
Heiko Schocherf5895d12014-06-24 10:10:04 +02001051/* Returns the size of the entire flash chip */
1052uint64_t mtd_get_device_size(const struct mtd_info *mtd)
1053{
Miquel Raynal6382e0c2018-09-29 12:58:27 +02001054 if (mtd_is_partition(mtd))
1055 return mtd->parent->size;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001056
Miquel Raynal6382e0c2018-09-29 12:58:27 +02001057 return mtd->size;
Heiko Schocherf5895d12014-06-24 10:10:04 +02001058}
1059EXPORT_SYMBOL_GPL(mtd_get_device_size);
Alexey Romanovd5119672024-07-18 08:46:04 +03001060
1061static struct mtd_info *mtd_get_partition_by_index(struct mtd_info *mtd, int index)
1062{
1063 struct mtd_info *part;
1064 int i = 0;
1065
1066 list_for_each_entry(part, &mtd->partitions, node)
1067 if (i++ == index)
1068 return part;
1069
1070 debug("Partition with idx=%d not found on MTD device %s\n", index, mtd->name);
1071 return NULL;
1072}
1073
1074static int __maybe_unused part_get_info_mtd(struct blk_desc *dev_desc, int part_idx,
1075 struct disk_partition *info)
1076{
1077 struct mtd_info *master = blk_desc_to_mtd(dev_desc);
1078 struct mtd_info *part;
1079
1080 if (!master) {
1081 debug("MTD device is NULL\n");
1082 return -EINVAL;
1083 }
1084
1085 part = mtd_get_partition_by_index(master, part_idx);
1086 if (!part) {
1087 debug("Failed to find partition with idx=%d\n", part_idx);
1088 return -EINVAL;
1089 }
1090
1091 snprintf(info->name, PART_NAME_LEN, part->name);
1092 info->start = part->offset / dev_desc->blksz;
1093 info->size = part->size / dev_desc->blksz;
1094 info->blksz = dev_desc->blksz;
1095
1096 return 0;
1097}
1098
1099static void __maybe_unused part_print_mtd(struct blk_desc *dev_desc)
1100{
1101 struct mtd_info *master = blk_desc_to_mtd(dev_desc);
1102 struct mtd_info *part;
1103
1104 if (!master)
1105 return;
1106
1107 list_for_each_entry(part, &master->partitions, node)
1108 printf("- 0x%012llx-0x%012llx : \"%s\"\n",
1109 part->offset, part->offset + part->size, part->name);
1110}
1111
1112static int part_test_mtd(struct blk_desc *dev_desc)
1113{
1114 struct mtd_info *master = blk_desc_to_mtd(dev_desc);
1115 ALLOC_CACHE_ALIGN_BUFFER(unsigned char, buffer, dev_desc->blksz);
1116
1117 if (!master)
1118 return -1;
1119
1120 if (blk_dread(dev_desc, 0, 1, (ulong *)buffer) != 1)
1121 return -1;
1122
1123 return 0;
1124}
1125
1126U_BOOT_PART_TYPE(mtd) = {
1127 .name = "MTD",
1128 .part_type = PART_TYPE_MTD,
1129 .max_entries = MTD_ENTRY_NUMBERS,
1130 .get_info = part_get_info_ptr(part_get_info_mtd),
1131 .print = part_print_ptr(part_print_mtd),
1132 .test = part_test_mtd,
1133};