blob: 9ccb1b3361327f0e2a7acba795d187766387dffc [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>
29
30#include "mtdcore.h"
Kyungmin Parke29c22f2008-11-19 16:20:36 +010031
32/* Our partition linked list */
Heiko Schocherff94bc42014-06-24 10:10:04 +020033static LIST_HEAD(mtd_partitions);
34#ifndef __UBOOT__
35static DEFINE_MUTEX(mtd_partitions_mutex);
36#else
37DEFINE_MUTEX(mtd_partitions_mutex);
38#endif
Kyungmin Parke29c22f2008-11-19 16:20:36 +010039
40/* Our partition node structure */
41struct mtd_part {
42 struct mtd_info mtd;
43 struct mtd_info *master;
Stefan Roese8d2effe2009-05-11 16:03:55 +020044 uint64_t offset;
Kyungmin Parke29c22f2008-11-19 16:20:36 +010045 struct list_head list;
Kyungmin Parke29c22f2008-11-19 16:20:36 +010046};
47
48/*
49 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
50 * the pointer to that structure with this macro.
51 */
52#define PART(x) ((struct mtd_part *)(x))
53
54
Heiko Schocherff94bc42014-06-24 10:10:04 +020055#ifdef __UBOOT__
56/* from mm/util.c */
57
58/**
59 * kstrdup - allocate space for and copy an existing string
60 * @s: the string to duplicate
61 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
62 */
63char *kstrdup(const char *s, gfp_t gfp)
64{
65 size_t len;
66 char *buf;
67
68 if (!s)
69 return NULL;
70
71 len = strlen(s) + 1;
72 buf = kmalloc(len, gfp);
73 if (buf)
74 memcpy(buf, s, len);
75 return buf;
76}
77#endif
78
Kyungmin Parke29c22f2008-11-19 16:20:36 +010079/*
80 * MTD methods which simply translate the effective address and pass through
81 * to the _real_ device.
82 */
83
Stefan Roese8d2effe2009-05-11 16:03:55 +020084static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
85 size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +010086{
87 struct mtd_part *part = PART(mtd);
Stefan Roese8d2effe2009-05-11 16:03:55 +020088 struct mtd_ecc_stats stats;
Kyungmin Parke29c22f2008-11-19 16:20:36 +010089 int res;
90
Stefan Roese8d2effe2009-05-11 16:03:55 +020091 stats = part->master->ecc_stats;
Heiko Schocherff94bc42014-06-24 10:10:04 +020092 res = part->master->_read(part->master, from + part->offset, len,
93 retlen, buf);
Paul Burton40462e52013-09-04 15:16:56 +010094 if (unlikely(mtd_is_eccerr(res)))
95 mtd->ecc_stats.failed +=
96 part->master->ecc_stats.failed - stats.failed;
97 else
98 mtd->ecc_stats.corrected +=
99 part->master->ecc_stats.corrected - stats.corrected;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100100 return res;
101}
102
Heiko Schocherff94bc42014-06-24 10:10:04 +0200103#ifndef __UBOOT__
104static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
105 size_t *retlen, void **virt, resource_size_t *phys)
106{
107 struct mtd_part *part = PART(mtd);
108
109 return part->master->_point(part->master, from + part->offset, len,
110 retlen, virt, phys);
111}
112
113static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
114{
115 struct mtd_part *part = PART(mtd);
116
117 return part->master->_unpoint(part->master, from + part->offset, len);
118}
119#endif
120
121static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
122 unsigned long len,
123 unsigned long offset,
124 unsigned long flags)
125{
126 struct mtd_part *part = PART(mtd);
127
128 offset += part->offset;
129 return part->master->_get_unmapped_area(part->master, len, offset,
130 flags);
131}
132
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100133static int part_read_oob(struct mtd_info *mtd, loff_t from,
Stefan Roese8d2effe2009-05-11 16:03:55 +0200134 struct mtd_oob_ops *ops)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100135{
136 struct mtd_part *part = PART(mtd);
137 int res;
138
139 if (from >= mtd->size)
140 return -EINVAL;
141 if (ops->datbuf && from + ops->len > mtd->size)
142 return -EINVAL;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100143
Heiko Schocherff94bc42014-06-24 10:10:04 +0200144 /*
145 * If OOB is also requested, make sure that we do not read past the end
146 * of this partition.
147 */
148 if (ops->oobbuf) {
149 size_t len, pages;
150
151 if (ops->mode == MTD_OPS_AUTO_OOB)
152 len = mtd->oobavail;
153 else
154 len = mtd->oobsize;
155 pages = mtd_div_by_ws(mtd->size, mtd);
156 pages -= mtd_div_by_ws(from, mtd);
157 if (ops->ooboffs + ops->ooblen > pages * len)
158 return -EINVAL;
159 }
160
161 res = part->master->_read_oob(part->master, from + part->offset, ops);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100162 if (unlikely(res)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000163 if (mtd_is_bitflip(res))
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100164 mtd->ecc_stats.corrected++;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000165 if (mtd_is_eccerr(res))
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100166 mtd->ecc_stats.failed++;
167 }
168 return res;
169}
170
Stefan Roese8d2effe2009-05-11 16:03:55 +0200171static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
172 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100173{
174 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200175 return part->master->_read_user_prot_reg(part->master, from, len,
176 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100177}
178
Heiko Schocher4e67c572014-07-15 16:08:43 +0200179static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
180 size_t *retlen, struct otp_info *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100181{
182 struct mtd_part *part = PART(mtd);
Heiko Schocher4e67c572014-07-15 16:08:43 +0200183 return part->master->_get_user_prot_info(part->master, len, retlen,
184 buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100185}
186
Stefan Roese8d2effe2009-05-11 16:03:55 +0200187static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
188 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100189{
190 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200191 return part->master->_read_fact_prot_reg(part->master, from, len,
192 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100193}
194
Heiko Schocher4e67c572014-07-15 16:08:43 +0200195static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
196 size_t *retlen, struct otp_info *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100197{
198 struct mtd_part *part = PART(mtd);
Heiko Schocher4e67c572014-07-15 16:08:43 +0200199 return part->master->_get_fact_prot_info(part->master, len, retlen,
200 buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100201}
202
Stefan Roese8d2effe2009-05-11 16:03:55 +0200203static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
204 size_t *retlen, const u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100205{
206 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200207 return part->master->_write(part->master, to + part->offset, len,
208 retlen, buf);
209}
210
211static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
212 size_t *retlen, const u_char *buf)
213{
214 struct mtd_part *part = PART(mtd);
215 return part->master->_panic_write(part->master, to + part->offset, len,
216 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100217}
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100218
219static int part_write_oob(struct mtd_info *mtd, loff_t to,
Stefan Roese8d2effe2009-05-11 16:03:55 +0200220 struct mtd_oob_ops *ops)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100221{
222 struct mtd_part *part = PART(mtd);
223
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100224 if (to >= mtd->size)
225 return -EINVAL;
226 if (ops->datbuf && to + ops->len > mtd->size)
227 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200228 return part->master->_write_oob(part->master, to + part->offset, ops);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100229}
230
Stefan Roese8d2effe2009-05-11 16:03:55 +0200231static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
232 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100233{
234 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200235 return part->master->_write_user_prot_reg(part->master, from, len,
236 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100237}
238
Stefan Roese8d2effe2009-05-11 16:03:55 +0200239static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
240 size_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100241{
242 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200243 return part->master->_lock_user_prot_reg(part->master, from, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100244}
245
Heiko Schocherff94bc42014-06-24 10:10:04 +0200246#ifndef __UBOOT__
247static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
248 unsigned long count, loff_t to, size_t *retlen)
249{
250 struct mtd_part *part = PART(mtd);
251 return part->master->_writev(part->master, vecs, count,
252 to + part->offset, retlen);
253}
254#endif
255
Stefan Roese8d2effe2009-05-11 16:03:55 +0200256static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100257{
258 struct mtd_part *part = PART(mtd);
259 int ret;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000260
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100261 instr->addr += part->offset;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200262 ret = part->master->_erase(part->master, instr);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100263 if (ret) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200264 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100265 instr->fail_addr -= part->offset;
266 instr->addr -= part->offset;
267 }
268 return ret;
269}
270
271void mtd_erase_callback(struct erase_info *instr)
272{
Sergey Lapindfe64e22013-01-14 03:46:50 +0000273 if (instr->mtd->_erase == part_erase) {
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100274 struct mtd_part *part = PART(instr->mtd);
275
Stefan Roese8d2effe2009-05-11 16:03:55 +0200276 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100277 instr->fail_addr -= part->offset;
278 instr->addr -= part->offset;
279 }
280 if (instr->callback)
281 instr->callback(instr);
282}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200283EXPORT_SYMBOL_GPL(mtd_erase_callback);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100284
Stefan Roese8d2effe2009-05-11 16:03:55 +0200285static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100286{
287 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200288 return part->master->_lock(part->master, ofs + part->offset, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100289}
290
Stefan Roese8d2effe2009-05-11 16:03:55 +0200291static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100292{
293 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200294 return part->master->_unlock(part->master, ofs + part->offset, len);
295}
296
297static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
298{
299 struct mtd_part *part = PART(mtd);
300 return part->master->_is_locked(part->master, ofs + part->offset, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100301}
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100302
303static void part_sync(struct mtd_info *mtd)
304{
305 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200306 part->master->_sync(part->master);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100307}
308
Heiko Schocherff94bc42014-06-24 10:10:04 +0200309#ifndef __UBOOT__
310static int part_suspend(struct mtd_info *mtd)
311{
312 struct mtd_part *part = PART(mtd);
313 return part->master->_suspend(part->master);
314}
315
316static void part_resume(struct mtd_info *mtd)
317{
318 struct mtd_part *part = PART(mtd);
319 part->master->_resume(part->master);
320}
321#endif
322
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300323static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
324{
325 struct mtd_part *part = PART(mtd);
326 ofs += part->offset;
327 return part->master->_block_isreserved(part->master, ofs);
328}
329
Stefan Roese8d2effe2009-05-11 16:03:55 +0200330static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100331{
332 struct mtd_part *part = PART(mtd);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100333 ofs += part->offset;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200334 return part->master->_block_isbad(part->master, ofs);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100335}
336
Stefan Roese8d2effe2009-05-11 16:03:55 +0200337static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100338{
339 struct mtd_part *part = PART(mtd);
340 int res;
341
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100342 ofs += part->offset;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200343 res = part->master->_block_markbad(part->master, ofs);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100344 if (!res)
345 mtd->ecc_stats.badblocks++;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100346 return res;
347}
348
Heiko Schocherff94bc42014-06-24 10:10:04 +0200349static inline void free_partition(struct mtd_part *p)
350{
351 kfree(p->mtd.name);
352 kfree(p);
353}
354
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100355/*
356 * This function unregisters and destroy all slave MTD objects which are
357 * attached to the given master MTD object.
358 */
359
360int del_mtd_partitions(struct mtd_info *master)
361{
Stefan Roese8d2effe2009-05-11 16:03:55 +0200362 struct mtd_part *slave, *next;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200363 int ret, err = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100364
Miquel Raynalb0036f72018-08-16 17:30:19 +0200365 debug("Deleting MTD partitions on \"%s\":\n", master->name);
366
Heiko Schocherff94bc42014-06-24 10:10:04 +0200367 mutex_lock(&mtd_partitions_mutex);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200368 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100369 if (slave->master == master) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200370 ret = del_mtd_device(&slave->mtd);
371 if (ret < 0) {
372 err = ret;
373 continue;
374 }
Stefan Roese8d2effe2009-05-11 16:03:55 +0200375 list_del(&slave->list);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200376 free_partition(slave);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200377 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200378 mutex_unlock(&mtd_partitions_mutex);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200379
Heiko Schocherff94bc42014-06-24 10:10:04 +0200380 return err;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200381}
382
Heiko Schocherff94bc42014-06-24 10:10:04 +0200383static struct mtd_part *allocate_partition(struct mtd_info *master,
384 const struct mtd_partition *part, int partno,
385 uint64_t cur_offset)
Stefan Roese8d2effe2009-05-11 16:03:55 +0200386{
387 struct mtd_part *slave;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200388 char *name;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200389
390 /* allocate the partition structure */
391 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200392 name = kstrdup(part->name, GFP_KERNEL);
393 if (!name || !slave) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200394 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200395 master->name);
396 kfree(name);
397 kfree(slave);
398 return ERR_PTR(-ENOMEM);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200399 }
Stefan Roese8d2effe2009-05-11 16:03:55 +0200400
401 /* set up the MTD object for this partition */
402 slave->mtd.type = master->type;
403 slave->mtd.flags = master->flags & ~part->mask_flags;
404 slave->mtd.size = part->size;
405 slave->mtd.writesize = master->writesize;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200406 slave->mtd.writebufsize = master->writebufsize;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200407 slave->mtd.oobsize = master->oobsize;
408 slave->mtd.oobavail = master->oobavail;
409 slave->mtd.subpage_sft = master->subpage_sft;
410
Heiko Schocherff94bc42014-06-24 10:10:04 +0200411 slave->mtd.name = name;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200412 slave->mtd.owner = master->owner;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200413#ifndef __UBOOT__
414 slave->mtd.backing_dev_info = master->backing_dev_info;
415
416 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
417 * to have the same data be in two different partitions.
418 */
419 slave->mtd.dev.parent = master->dev.parent;
420#endif
Stefan Roese8d2effe2009-05-11 16:03:55 +0200421
Boris Brezillon596cf082018-08-16 17:29:59 +0200422 if (master->_read)
423 slave->mtd._read = part_read;
424 if (master->_write)
425 slave->mtd._write = part_write;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200426
Heiko Schocherff94bc42014-06-24 10:10:04 +0200427 if (master->_panic_write)
428 slave->mtd._panic_write = part_panic_write;
429
430#ifndef __UBOOT__
431 if (master->_point && master->_unpoint) {
432 slave->mtd._point = part_point;
433 slave->mtd._unpoint = part_unpoint;
434 }
435#endif
436
437 if (master->_get_unmapped_area)
438 slave->mtd._get_unmapped_area = part_get_unmapped_area;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000439 if (master->_read_oob)
440 slave->mtd._read_oob = part_read_oob;
441 if (master->_write_oob)
442 slave->mtd._write_oob = part_write_oob;
443 if (master->_read_user_prot_reg)
444 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
445 if (master->_read_fact_prot_reg)
446 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
447 if (master->_write_user_prot_reg)
448 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
449 if (master->_lock_user_prot_reg)
450 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
451 if (master->_get_user_prot_info)
452 slave->mtd._get_user_prot_info = part_get_user_prot_info;
453 if (master->_get_fact_prot_info)
454 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
455 if (master->_sync)
456 slave->mtd._sync = part_sync;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200457#ifndef __UBOOT__
458 if (!partno && !master->dev.class && master->_suspend &&
459 master->_resume) {
460 slave->mtd._suspend = part_suspend;
461 slave->mtd._resume = part_resume;
462 }
463 if (master->_writev)
464 slave->mtd._writev = part_writev;
465#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +0000466 if (master->_lock)
467 slave->mtd._lock = part_lock;
468 if (master->_unlock)
469 slave->mtd._unlock = part_unlock;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200470 if (master->_is_locked)
471 slave->mtd._is_locked = part_is_locked;
Ezequiel Garcia86a720a2014-05-21 19:06:12 -0300472 if (master->_block_isreserved)
473 slave->mtd._block_isreserved = part_block_isreserved;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000474 if (master->_block_isbad)
475 slave->mtd._block_isbad = part_block_isbad;
476 if (master->_block_markbad)
477 slave->mtd._block_markbad = part_block_markbad;
478 slave->mtd._erase = part_erase;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200479 slave->master = master;
480 slave->offset = part->offset;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200481
482 if (slave->offset == MTDPART_OFS_APPEND)
483 slave->offset = cur_offset;
484 if (slave->offset == MTDPART_OFS_NXTBLK) {
485 slave->offset = cur_offset;
486 if (mtd_mod_by_eb(cur_offset, master) != 0) {
487 /* Round up to next erasesize */
488 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200489 debug("Moving partition %d: "
490 "0x%012llx -> 0x%012llx\n", partno,
491 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
492 }
493 }
494 if (slave->offset == MTDPART_OFS_RETAIN) {
495 slave->offset = cur_offset;
496 if (master->size - slave->offset >= slave->mtd.size) {
497 slave->mtd.size = master->size - slave->offset
498 - slave->mtd.size;
499 } else {
500 debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
501 part->name, master->size - slave->offset,
502 slave->mtd.size);
503 /* register to preserve ordering */
504 goto out_register;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200505 }
506 }
507 if (slave->mtd.size == MTDPART_SIZ_FULL)
508 slave->mtd.size = master->size - slave->offset;
509
Heiko Schocherff94bc42014-06-24 10:10:04 +0200510 debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
511 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200512
513 /* let's do some sanity checks */
514 if (slave->offset >= master->size) {
515 /* let's register it anyway to preserve ordering */
516 slave->offset = 0;
517 slave->mtd.size = 0;
518 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
519 part->name);
520 goto out_register;
521 }
522 if (slave->offset + slave->mtd.size > master->size) {
523 slave->mtd.size = master->size - slave->offset;
524 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
525 part->name, master->name, (unsigned long long)slave->mtd.size);
526 }
527 if (master->numeraseregions > 1) {
528 /* Deal with variable erase size stuff */
529 int i, max = master->numeraseregions;
530 u64 end = slave->offset + slave->mtd.size;
531 struct mtd_erase_region_info *regions = master->eraseregions;
532
533 /* Find the first erase regions which is part of this
534 * partition. */
535 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
536 ;
537 /* The loop searched for the region _behind_ the first one */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200538 if (i > 0)
539 i--;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200540
541 /* Pick biggest erasesize */
542 for (; i < max && regions[i].offset < end; i++) {
543 if (slave->mtd.erasesize < regions[i].erasesize) {
544 slave->mtd.erasesize = regions[i].erasesize;
545 }
546 }
547 BUG_ON(slave->mtd.erasesize == 0);
548 } else {
549 /* Single erase size */
550 slave->mtd.erasesize = master->erasesize;
551 }
552
553 if ((slave->mtd.flags & MTD_WRITEABLE) &&
554 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
555 /* Doesn't start on a boundary of major erase size */
556 /* FIXME: Let it be writable if it is on a boundary of
557 * _minor_ erase size though */
558 slave->mtd.flags &= ~MTD_WRITEABLE;
559 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
560 part->name);
561 }
562 if ((slave->mtd.flags & MTD_WRITEABLE) &&
563 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
564 slave->mtd.flags &= ~MTD_WRITEABLE;
565 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
566 part->name);
567 }
568
569 slave->mtd.ecclayout = master->ecclayout;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200570 slave->mtd.ecc_step_size = master->ecc_step_size;
571 slave->mtd.ecc_strength = master->ecc_strength;
572 slave->mtd.bitflip_threshold = master->bitflip_threshold;
573
Sergey Lapindfe64e22013-01-14 03:46:50 +0000574 if (master->_block_isbad) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200575 uint64_t offs = 0;
576
577 while (offs < slave->mtd.size) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000578 if (mtd_block_isbad(master, offs + slave->offset))
Stefan Roese8d2effe2009-05-11 16:03:55 +0200579 slave->mtd.ecc_stats.badblocks++;
580 offs += slave->mtd.erasesize;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100581 }
582 }
583
Stefan Roese8d2effe2009-05-11 16:03:55 +0200584out_register:
Stefan Roese8d2effe2009-05-11 16:03:55 +0200585 return slave;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100586}
587
Heiko Schocherddf7bcf2014-07-15 16:08:42 +0200588#ifndef __UBOOT__
Heiko Schocherff94bc42014-06-24 10:10:04 +0200589int mtd_add_partition(struct mtd_info *master, const char *name,
590 long long offset, long long length)
591{
592 struct mtd_partition part;
593 struct mtd_part *p, *new;
594 uint64_t start, end;
595 int ret = 0;
596
597 /* the direct offset is expected */
598 if (offset == MTDPART_OFS_APPEND ||
599 offset == MTDPART_OFS_NXTBLK)
600 return -EINVAL;
601
602 if (length == MTDPART_SIZ_FULL)
603 length = master->size - offset;
604
605 if (length <= 0)
606 return -EINVAL;
607
608 part.name = name;
609 part.size = length;
610 part.offset = offset;
611 part.mask_flags = 0;
612 part.ecclayout = NULL;
613
614 new = allocate_partition(master, &part, -1, offset);
615 if (IS_ERR(new))
616 return PTR_ERR(new);
617
618 start = offset;
619 end = offset + length;
620
621 mutex_lock(&mtd_partitions_mutex);
622 list_for_each_entry(p, &mtd_partitions, list)
623 if (p->master == master) {
624 if ((start >= p->offset) &&
625 (start < (p->offset + p->mtd.size)))
626 goto err_inv;
627
628 if ((end >= p->offset) &&
629 (end < (p->offset + p->mtd.size)))
630 goto err_inv;
631 }
632
633 list_add(&new->list, &mtd_partitions);
634 mutex_unlock(&mtd_partitions_mutex);
635
636 add_mtd_device(&new->mtd);
637
638 return ret;
639err_inv:
640 mutex_unlock(&mtd_partitions_mutex);
641 free_partition(new);
642 return -EINVAL;
643}
644EXPORT_SYMBOL_GPL(mtd_add_partition);
645
646int mtd_del_partition(struct mtd_info *master, int partno)
647{
648 struct mtd_part *slave, *next;
649 int ret = -EINVAL;
650
651 mutex_lock(&mtd_partitions_mutex);
652 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
653 if ((slave->master == master) &&
654 (slave->mtd.index == partno)) {
655 ret = del_mtd_device(&slave->mtd);
656 if (ret < 0)
657 break;
658
659 list_del(&slave->list);
660 free_partition(slave);
661 break;
662 }
663 mutex_unlock(&mtd_partitions_mutex);
664
665 return ret;
666}
667EXPORT_SYMBOL_GPL(mtd_del_partition);
Heiko Schocherddf7bcf2014-07-15 16:08:42 +0200668#endif
Heiko Schocherff94bc42014-06-24 10:10:04 +0200669
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100670/*
671 * This function, given a master MTD object and a partition table, creates
672 * and registers slave MTD objects which are bound to the master according to
673 * the partition definitions.
Stefan Roese8d2effe2009-05-11 16:03:55 +0200674 *
675 * We don't register the master, or expect the caller to have done so,
676 * for reasons of data integrity.
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100677 */
678
679int add_mtd_partitions(struct mtd_info *master,
680 const struct mtd_partition *parts,
681 int nbparts)
682{
683 struct mtd_part *slave;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200684 uint64_t cur_offset = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100685 int i;
686
Heiko Schocherff94bc42014-06-24 10:10:04 +0200687#ifdef __UBOOT__
Stefan Roese9def12c2008-11-27 14:05:15 +0100688 /*
689 * Need to init the list here, since LIST_INIT() does not
690 * work on platforms where relocation has problems (like MIPS
691 * & PPC).
692 */
693 if (mtd_partitions.next == NULL)
694 INIT_LIST_HEAD(&mtd_partitions);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200695#endif
Stefan Roese9def12c2008-11-27 14:05:15 +0100696
Joe Hershberger147162d2013-04-08 10:32:49 +0000697 debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100698
699 for (i = 0; i < nbparts; i++) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200700 slave = allocate_partition(master, parts + i, i, cur_offset);
701 if (IS_ERR(slave))
702 return PTR_ERR(slave);
703
704 mutex_lock(&mtd_partitions_mutex);
705 list_add(&slave->list, &mtd_partitions);
706 mutex_unlock(&mtd_partitions_mutex);
707
708 add_mtd_device(&slave->mtd);
709
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100710 cur_offset = slave->offset + slave->mtd.size;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100711 }
712
713 return 0;
714}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200715
716#ifndef __UBOOT__
717static DEFINE_SPINLOCK(part_parser_lock);
718static LIST_HEAD(part_parsers);
719
720static struct mtd_part_parser *get_partition_parser(const char *name)
721{
722 struct mtd_part_parser *p, *ret = NULL;
723
724 spin_lock(&part_parser_lock);
725
726 list_for_each_entry(p, &part_parsers, list)
727 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
728 ret = p;
729 break;
730 }
731
732 spin_unlock(&part_parser_lock);
733
734 return ret;
735}
736
737#define put_partition_parser(p) do { module_put((p)->owner); } while (0)
738
739void register_mtd_parser(struct mtd_part_parser *p)
740{
741 spin_lock(&part_parser_lock);
742 list_add(&p->list, &part_parsers);
743 spin_unlock(&part_parser_lock);
744}
745EXPORT_SYMBOL_GPL(register_mtd_parser);
746
747void deregister_mtd_parser(struct mtd_part_parser *p)
748{
749 spin_lock(&part_parser_lock);
750 list_del(&p->list);
751 spin_unlock(&part_parser_lock);
752}
753EXPORT_SYMBOL_GPL(deregister_mtd_parser);
754
755/*
756 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
757 * are changing this array!
758 */
759static const char * const default_mtd_part_types[] = {
760 "cmdlinepart",
761 "ofpart",
762 NULL
763};
764
765/**
766 * parse_mtd_partitions - parse MTD partitions
767 * @master: the master partition (describes whole MTD device)
768 * @types: names of partition parsers to try or %NULL
769 * @pparts: array of partitions found is returned here
770 * @data: MTD partition parser-specific data
771 *
772 * This function tries to find partition on MTD device @master. It uses MTD
773 * partition parsers, specified in @types. However, if @types is %NULL, then
774 * the default list of parsers is used. The default list contains only the
775 * "cmdlinepart" and "ofpart" parsers ATM.
776 * Note: If there are more then one parser in @types, the kernel only takes the
777 * partitions parsed out by the first parser.
778 *
779 * This function may return:
780 * o a negative error code in case of failure
781 * o zero if no partitions were found
782 * o a positive number of found partitions, in which case on exit @pparts will
783 * point to an array containing this number of &struct mtd_info objects.
784 */
785int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
786 struct mtd_partition **pparts,
787 struct mtd_part_parser_data *data)
788{
789 struct mtd_part_parser *parser;
790 int ret = 0;
791
792 if (!types)
793 types = default_mtd_part_types;
794
795 for ( ; ret <= 0 && *types; types++) {
796 parser = get_partition_parser(*types);
797 if (!parser && !request_module("%s", *types))
798 parser = get_partition_parser(*types);
799 if (!parser)
800 continue;
801 ret = (*parser->parse_fn)(master, pparts, data);
802 put_partition_parser(parser);
803 if (ret > 0) {
804 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
805 ret, parser->name, master->name);
806 break;
807 }
808 }
809 return ret;
810}
811#endif
812
813int mtd_is_partition(const struct mtd_info *mtd)
814{
815 struct mtd_part *part;
816 int ispart = 0;
817
818 mutex_lock(&mtd_partitions_mutex);
819 list_for_each_entry(part, &mtd_partitions, list)
820 if (&part->mtd == mtd) {
821 ispart = 1;
822 break;
823 }
824 mutex_unlock(&mtd_partitions_mutex);
825
826 return ispart;
827}
828EXPORT_SYMBOL_GPL(mtd_is_partition);
829
830/* Returns the size of the entire flash chip */
831uint64_t mtd_get_device_size(const struct mtd_info *mtd)
832{
833 if (!mtd_is_partition(mtd))
834 return mtd->size;
835
836 return PART(mtd)->master->size;
837}
838EXPORT_SYMBOL_GPL(mtd_get_device_size);