blob: cfbaa3d9a0cbbfda3e6dda66dd889a20c95aca31 [file] [log] [blame]
Kyungmin Parke29c22f2008-11-19 16:20:36 +01001/*
2 * Simple MTD partitioning layer
3 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02004 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
5 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
6 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
Kyungmin Parke29c22f2008-11-19 16:20:36 +01007 *
Heiko Schocherff94bc42014-06-24 10:10:04 +02008 * SPDX-License-Identifier: GPL-2.0+
Kyungmin Parke29c22f2008-11-19 16:20:36 +01009 *
Kyungmin Parke29c22f2008-11-19 16:20:36 +010010 */
11
Heiko Schocherff94bc42014-06-24 10:10:04 +020012#ifndef __UBOOT__
13#include <linux/module.h>
14#include <linux/types.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/list.h>
18#include <linux/kmod.h>
19#endif
20
Kyungmin Parke29c22f2008-11-19 16:20:36 +010021#include <common.h>
22#include <malloc.h>
23#include <asm/errno.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020024#include <linux/compat.h>
25#include <ubi_uboot.h>
Kyungmin Parke29c22f2008-11-19 16:20:36 +010026
Kyungmin Parke29c22f2008-11-19 16:20:36 +010027#include <linux/mtd/mtd.h>
28#include <linux/mtd/partitions.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020029#include <linux/err.h>
30
31#include "mtdcore.h"
Kyungmin Parke29c22f2008-11-19 16:20:36 +010032
33/* Our partition linked list */
Heiko Schocherff94bc42014-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 Parke29c22f2008-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 Roese8d2effe2009-05-11 16:03:55 +020045 uint64_t offset;
Kyungmin Parke29c22f2008-11-19 16:20:36 +010046 struct list_head list;
Kyungmin Parke29c22f2008-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 Schocherff94bc42014-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
Kyungmin Parke29c22f2008-11-19 16:20:36 +010080/*
81 * MTD methods which simply translate the effective address and pass through
82 * to the _real_ device.
83 */
84
Stefan Roese8d2effe2009-05-11 16:03:55 +020085static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
86 size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +010087{
88 struct mtd_part *part = PART(mtd);
Stefan Roese8d2effe2009-05-11 16:03:55 +020089 struct mtd_ecc_stats stats;
Kyungmin Parke29c22f2008-11-19 16:20:36 +010090 int res;
91
Stefan Roese8d2effe2009-05-11 16:03:55 +020092 stats = part->master->ecc_stats;
Heiko Schocherff94bc42014-06-24 10:10:04 +020093 res = part->master->_read(part->master, from + part->offset, len,
94 retlen, buf);
Paul Burton40462e52013-09-04 15:16:56 +010095 if (unlikely(mtd_is_eccerr(res)))
96 mtd->ecc_stats.failed +=
97 part->master->ecc_stats.failed - stats.failed;
98 else
99 mtd->ecc_stats.corrected +=
100 part->master->ecc_stats.corrected - stats.corrected;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100101 return res;
102}
103
Heiko Schocherff94bc42014-06-24 10:10:04 +0200104#ifndef __UBOOT__
105static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
106 size_t *retlen, void **virt, resource_size_t *phys)
107{
108 struct mtd_part *part = PART(mtd);
109
110 return part->master->_point(part->master, from + part->offset, len,
111 retlen, virt, phys);
112}
113
114static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
115{
116 struct mtd_part *part = PART(mtd);
117
118 return part->master->_unpoint(part->master, from + part->offset, len);
119}
120#endif
121
122static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
123 unsigned long len,
124 unsigned long offset,
125 unsigned long flags)
126{
127 struct mtd_part *part = PART(mtd);
128
129 offset += part->offset;
130 return part->master->_get_unmapped_area(part->master, len, offset,
131 flags);
132}
133
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100134static int part_read_oob(struct mtd_info *mtd, loff_t from,
Stefan Roese8d2effe2009-05-11 16:03:55 +0200135 struct mtd_oob_ops *ops)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100136{
137 struct mtd_part *part = PART(mtd);
138 int res;
139
140 if (from >= mtd->size)
141 return -EINVAL;
142 if (ops->datbuf && from + ops->len > mtd->size)
143 return -EINVAL;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100144
Heiko Schocherff94bc42014-06-24 10:10:04 +0200145 /*
146 * If OOB is also requested, make sure that we do not read past the end
147 * of this partition.
148 */
149 if (ops->oobbuf) {
150 size_t len, pages;
151
152 if (ops->mode == MTD_OPS_AUTO_OOB)
153 len = mtd->oobavail;
154 else
155 len = mtd->oobsize;
156 pages = mtd_div_by_ws(mtd->size, mtd);
157 pages -= mtd_div_by_ws(from, mtd);
158 if (ops->ooboffs + ops->ooblen > pages * len)
159 return -EINVAL;
160 }
161
162 res = part->master->_read_oob(part->master, from + part->offset, ops);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100163 if (unlikely(res)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000164 if (mtd_is_bitflip(res))
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100165 mtd->ecc_stats.corrected++;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000166 if (mtd_is_eccerr(res))
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100167 mtd->ecc_stats.failed++;
168 }
169 return res;
170}
171
Stefan Roese8d2effe2009-05-11 16:03:55 +0200172static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
173 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100174{
175 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200176 return part->master->_read_user_prot_reg(part->master, from, len,
177 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100178}
179
Heiko Schocher4e67c572014-07-15 16:08:43 +0200180static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
181 size_t *retlen, struct otp_info *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100182{
183 struct mtd_part *part = PART(mtd);
Heiko Schocher4e67c572014-07-15 16:08:43 +0200184 return part->master->_get_user_prot_info(part->master, len, retlen,
185 buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100186}
187
Stefan Roese8d2effe2009-05-11 16:03:55 +0200188static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
189 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100190{
191 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200192 return part->master->_read_fact_prot_reg(part->master, from, len,
193 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100194}
195
Heiko Schocher4e67c572014-07-15 16:08:43 +0200196static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
197 size_t *retlen, struct otp_info *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100198{
199 struct mtd_part *part = PART(mtd);
Heiko Schocher4e67c572014-07-15 16:08:43 +0200200 return part->master->_get_fact_prot_info(part->master, len, retlen,
201 buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100202}
203
Stefan Roese8d2effe2009-05-11 16:03:55 +0200204static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
205 size_t *retlen, const u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100206{
207 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200208 return part->master->_write(part->master, to + part->offset, len,
209 retlen, buf);
210}
211
212static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
213 size_t *retlen, const u_char *buf)
214{
215 struct mtd_part *part = PART(mtd);
216 return part->master->_panic_write(part->master, to + part->offset, len,
217 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100218}
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100219
220static int part_write_oob(struct mtd_info *mtd, loff_t to,
Stefan Roese8d2effe2009-05-11 16:03:55 +0200221 struct mtd_oob_ops *ops)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100222{
223 struct mtd_part *part = PART(mtd);
224
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100225 if (to >= mtd->size)
226 return -EINVAL;
227 if (ops->datbuf && to + ops->len > mtd->size)
228 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200229 return part->master->_write_oob(part->master, to + part->offset, ops);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100230}
231
Stefan Roese8d2effe2009-05-11 16:03:55 +0200232static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
233 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100234{
235 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200236 return part->master->_write_user_prot_reg(part->master, from, len,
237 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100238}
239
Stefan Roese8d2effe2009-05-11 16:03:55 +0200240static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
241 size_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100242{
243 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200244 return part->master->_lock_user_prot_reg(part->master, from, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100245}
246
Heiko Schocherff94bc42014-06-24 10:10:04 +0200247#ifndef __UBOOT__
248static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
249 unsigned long count, loff_t to, size_t *retlen)
250{
251 struct mtd_part *part = PART(mtd);
252 return part->master->_writev(part->master, vecs, count,
253 to + part->offset, retlen);
254}
255#endif
256
Stefan Roese8d2effe2009-05-11 16:03:55 +0200257static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100258{
259 struct mtd_part *part = PART(mtd);
260 int ret;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000261
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100262 instr->addr += part->offset;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200263 ret = part->master->_erase(part->master, instr);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100264 if (ret) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200265 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100266 instr->fail_addr -= part->offset;
267 instr->addr -= part->offset;
268 }
269 return ret;
270}
271
272void mtd_erase_callback(struct erase_info *instr)
273{
Sergey Lapindfe64e22013-01-14 03:46:50 +0000274 if (instr->mtd->_erase == part_erase) {
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100275 struct mtd_part *part = PART(instr->mtd);
276
Stefan Roese8d2effe2009-05-11 16:03:55 +0200277 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100278 instr->fail_addr -= part->offset;
279 instr->addr -= part->offset;
280 }
281 if (instr->callback)
282 instr->callback(instr);
283}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200284EXPORT_SYMBOL_GPL(mtd_erase_callback);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100285
Stefan Roese8d2effe2009-05-11 16:03:55 +0200286static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100287{
288 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200289 return part->master->_lock(part->master, ofs + part->offset, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100290}
291
Stefan Roese8d2effe2009-05-11 16:03:55 +0200292static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100293{
294 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200295 return part->master->_unlock(part->master, ofs + part->offset, len);
296}
297
298static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
299{
300 struct mtd_part *part = PART(mtd);
301 return part->master->_is_locked(part->master, ofs + part->offset, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100302}
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100303
304static void part_sync(struct mtd_info *mtd)
305{
306 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200307 part->master->_sync(part->master);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100308}
309
Heiko Schocherff94bc42014-06-24 10:10:04 +0200310#ifndef __UBOOT__
311static int part_suspend(struct mtd_info *mtd)
312{
313 struct mtd_part *part = PART(mtd);
314 return part->master->_suspend(part->master);
315}
316
317static void part_resume(struct mtd_info *mtd)
318{
319 struct mtd_part *part = PART(mtd);
320 part->master->_resume(part->master);
321}
322#endif
323
Stefan Roese8d2effe2009-05-11 16:03:55 +0200324static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100325{
326 struct mtd_part *part = PART(mtd);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100327 ofs += part->offset;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200328 return part->master->_block_isbad(part->master, ofs);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100329}
330
Stefan Roese8d2effe2009-05-11 16:03:55 +0200331static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100332{
333 struct mtd_part *part = PART(mtd);
334 int res;
335
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100336 ofs += part->offset;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200337 res = part->master->_block_markbad(part->master, ofs);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100338 if (!res)
339 mtd->ecc_stats.badblocks++;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100340 return res;
341}
342
Heiko Schocherff94bc42014-06-24 10:10:04 +0200343static inline void free_partition(struct mtd_part *p)
344{
345 kfree(p->mtd.name);
346 kfree(p);
347}
348
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100349/*
350 * This function unregisters and destroy all slave MTD objects which are
351 * attached to the given master MTD object.
352 */
353
354int del_mtd_partitions(struct mtd_info *master)
355{
Stefan Roese8d2effe2009-05-11 16:03:55 +0200356 struct mtd_part *slave, *next;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200357 int ret, err = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100358
Heiko Schocherff94bc42014-06-24 10:10:04 +0200359 mutex_lock(&mtd_partitions_mutex);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200360 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100361 if (slave->master == master) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200362 ret = del_mtd_device(&slave->mtd);
363 if (ret < 0) {
364 err = ret;
365 continue;
366 }
Stefan Roese8d2effe2009-05-11 16:03:55 +0200367 list_del(&slave->list);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200368 free_partition(slave);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200369 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200370 mutex_unlock(&mtd_partitions_mutex);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200371
Heiko Schocherff94bc42014-06-24 10:10:04 +0200372 return err;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200373}
374
Heiko Schocherff94bc42014-06-24 10:10:04 +0200375static struct mtd_part *allocate_partition(struct mtd_info *master,
376 const struct mtd_partition *part, int partno,
377 uint64_t cur_offset)
Stefan Roese8d2effe2009-05-11 16:03:55 +0200378{
379 struct mtd_part *slave;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200380 char *name;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200381
382 /* allocate the partition structure */
383 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200384 name = kstrdup(part->name, GFP_KERNEL);
385 if (!name || !slave) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200386 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200387 master->name);
388 kfree(name);
389 kfree(slave);
390 return ERR_PTR(-ENOMEM);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200391 }
Stefan Roese8d2effe2009-05-11 16:03:55 +0200392
393 /* set up the MTD object for this partition */
394 slave->mtd.type = master->type;
395 slave->mtd.flags = master->flags & ~part->mask_flags;
396 slave->mtd.size = part->size;
397 slave->mtd.writesize = master->writesize;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200398 slave->mtd.writebufsize = master->writebufsize;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200399 slave->mtd.oobsize = master->oobsize;
400 slave->mtd.oobavail = master->oobavail;
401 slave->mtd.subpage_sft = master->subpage_sft;
402
Heiko Schocherff94bc42014-06-24 10:10:04 +0200403 slave->mtd.name = name;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200404 slave->mtd.owner = master->owner;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200405#ifndef __UBOOT__
406 slave->mtd.backing_dev_info = master->backing_dev_info;
407
408 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
409 * to have the same data be in two different partitions.
410 */
411 slave->mtd.dev.parent = master->dev.parent;
412#endif
Stefan Roese8d2effe2009-05-11 16:03:55 +0200413
Sergey Lapindfe64e22013-01-14 03:46:50 +0000414 slave->mtd._read = part_read;
415 slave->mtd._write = part_write;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200416
Heiko Schocherff94bc42014-06-24 10:10:04 +0200417 if (master->_panic_write)
418 slave->mtd._panic_write = part_panic_write;
419
420#ifndef __UBOOT__
421 if (master->_point && master->_unpoint) {
422 slave->mtd._point = part_point;
423 slave->mtd._unpoint = part_unpoint;
424 }
425#endif
426
427 if (master->_get_unmapped_area)
428 slave->mtd._get_unmapped_area = part_get_unmapped_area;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000429 if (master->_read_oob)
430 slave->mtd._read_oob = part_read_oob;
431 if (master->_write_oob)
432 slave->mtd._write_oob = part_write_oob;
433 if (master->_read_user_prot_reg)
434 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
435 if (master->_read_fact_prot_reg)
436 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
437 if (master->_write_user_prot_reg)
438 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
439 if (master->_lock_user_prot_reg)
440 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
441 if (master->_get_user_prot_info)
442 slave->mtd._get_user_prot_info = part_get_user_prot_info;
443 if (master->_get_fact_prot_info)
444 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
445 if (master->_sync)
446 slave->mtd._sync = part_sync;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200447#ifndef __UBOOT__
448 if (!partno && !master->dev.class && master->_suspend &&
449 master->_resume) {
450 slave->mtd._suspend = part_suspend;
451 slave->mtd._resume = part_resume;
452 }
453 if (master->_writev)
454 slave->mtd._writev = part_writev;
455#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +0000456 if (master->_lock)
457 slave->mtd._lock = part_lock;
458 if (master->_unlock)
459 slave->mtd._unlock = part_unlock;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200460 if (master->_is_locked)
461 slave->mtd._is_locked = part_is_locked;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000462 if (master->_block_isbad)
463 slave->mtd._block_isbad = part_block_isbad;
464 if (master->_block_markbad)
465 slave->mtd._block_markbad = part_block_markbad;
466 slave->mtd._erase = part_erase;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200467 slave->master = master;
468 slave->offset = part->offset;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200469
470 if (slave->offset == MTDPART_OFS_APPEND)
471 slave->offset = cur_offset;
472 if (slave->offset == MTDPART_OFS_NXTBLK) {
473 slave->offset = cur_offset;
474 if (mtd_mod_by_eb(cur_offset, master) != 0) {
475 /* Round up to next erasesize */
476 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200477 debug("Moving partition %d: "
478 "0x%012llx -> 0x%012llx\n", partno,
479 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
480 }
481 }
482 if (slave->offset == MTDPART_OFS_RETAIN) {
483 slave->offset = cur_offset;
484 if (master->size - slave->offset >= slave->mtd.size) {
485 slave->mtd.size = master->size - slave->offset
486 - slave->mtd.size;
487 } else {
488 debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
489 part->name, master->size - slave->offset,
490 slave->mtd.size);
491 /* register to preserve ordering */
492 goto out_register;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200493 }
494 }
495 if (slave->mtd.size == MTDPART_SIZ_FULL)
496 slave->mtd.size = master->size - slave->offset;
497
Heiko Schocherff94bc42014-06-24 10:10:04 +0200498 debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
499 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200500
501 /* let's do some sanity checks */
502 if (slave->offset >= master->size) {
503 /* let's register it anyway to preserve ordering */
504 slave->offset = 0;
505 slave->mtd.size = 0;
506 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
507 part->name);
508 goto out_register;
509 }
510 if (slave->offset + slave->mtd.size > master->size) {
511 slave->mtd.size = master->size - slave->offset;
512 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
513 part->name, master->name, (unsigned long long)slave->mtd.size);
514 }
515 if (master->numeraseregions > 1) {
516 /* Deal with variable erase size stuff */
517 int i, max = master->numeraseregions;
518 u64 end = slave->offset + slave->mtd.size;
519 struct mtd_erase_region_info *regions = master->eraseregions;
520
521 /* Find the first erase regions which is part of this
522 * partition. */
523 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
524 ;
525 /* The loop searched for the region _behind_ the first one */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200526 if (i > 0)
527 i--;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200528
529 /* Pick biggest erasesize */
530 for (; i < max && regions[i].offset < end; i++) {
531 if (slave->mtd.erasesize < regions[i].erasesize) {
532 slave->mtd.erasesize = regions[i].erasesize;
533 }
534 }
535 BUG_ON(slave->mtd.erasesize == 0);
536 } else {
537 /* Single erase size */
538 slave->mtd.erasesize = master->erasesize;
539 }
540
541 if ((slave->mtd.flags & MTD_WRITEABLE) &&
542 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
543 /* Doesn't start on a boundary of major erase size */
544 /* FIXME: Let it be writable if it is on a boundary of
545 * _minor_ erase size though */
546 slave->mtd.flags &= ~MTD_WRITEABLE;
547 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
548 part->name);
549 }
550 if ((slave->mtd.flags & MTD_WRITEABLE) &&
551 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
552 slave->mtd.flags &= ~MTD_WRITEABLE;
553 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
554 part->name);
555 }
556
557 slave->mtd.ecclayout = master->ecclayout;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200558 slave->mtd.ecc_step_size = master->ecc_step_size;
559 slave->mtd.ecc_strength = master->ecc_strength;
560 slave->mtd.bitflip_threshold = master->bitflip_threshold;
561
Sergey Lapindfe64e22013-01-14 03:46:50 +0000562 if (master->_block_isbad) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200563 uint64_t offs = 0;
564
565 while (offs < slave->mtd.size) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000566 if (mtd_block_isbad(master, offs + slave->offset))
Stefan Roese8d2effe2009-05-11 16:03:55 +0200567 slave->mtd.ecc_stats.badblocks++;
568 offs += slave->mtd.erasesize;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100569 }
570 }
571
Stefan Roese8d2effe2009-05-11 16:03:55 +0200572out_register:
Stefan Roese8d2effe2009-05-11 16:03:55 +0200573 return slave;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100574}
575
Heiko Schocherddf7bcf2014-07-15 16:08:42 +0200576#ifndef __UBOOT__
Heiko Schocherff94bc42014-06-24 10:10:04 +0200577int mtd_add_partition(struct mtd_info *master, const char *name,
578 long long offset, long long length)
579{
580 struct mtd_partition part;
581 struct mtd_part *p, *new;
582 uint64_t start, end;
583 int ret = 0;
584
585 /* the direct offset is expected */
586 if (offset == MTDPART_OFS_APPEND ||
587 offset == MTDPART_OFS_NXTBLK)
588 return -EINVAL;
589
590 if (length == MTDPART_SIZ_FULL)
591 length = master->size - offset;
592
593 if (length <= 0)
594 return -EINVAL;
595
596 part.name = name;
597 part.size = length;
598 part.offset = offset;
599 part.mask_flags = 0;
600 part.ecclayout = NULL;
601
602 new = allocate_partition(master, &part, -1, offset);
603 if (IS_ERR(new))
604 return PTR_ERR(new);
605
606 start = offset;
607 end = offset + length;
608
609 mutex_lock(&mtd_partitions_mutex);
610 list_for_each_entry(p, &mtd_partitions, list)
611 if (p->master == master) {
612 if ((start >= p->offset) &&
613 (start < (p->offset + p->mtd.size)))
614 goto err_inv;
615
616 if ((end >= p->offset) &&
617 (end < (p->offset + p->mtd.size)))
618 goto err_inv;
619 }
620
621 list_add(&new->list, &mtd_partitions);
622 mutex_unlock(&mtd_partitions_mutex);
623
624 add_mtd_device(&new->mtd);
625
626 return ret;
627err_inv:
628 mutex_unlock(&mtd_partitions_mutex);
629 free_partition(new);
630 return -EINVAL;
631}
632EXPORT_SYMBOL_GPL(mtd_add_partition);
633
634int mtd_del_partition(struct mtd_info *master, int partno)
635{
636 struct mtd_part *slave, *next;
637 int ret = -EINVAL;
638
639 mutex_lock(&mtd_partitions_mutex);
640 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
641 if ((slave->master == master) &&
642 (slave->mtd.index == partno)) {
643 ret = del_mtd_device(&slave->mtd);
644 if (ret < 0)
645 break;
646
647 list_del(&slave->list);
648 free_partition(slave);
649 break;
650 }
651 mutex_unlock(&mtd_partitions_mutex);
652
653 return ret;
654}
655EXPORT_SYMBOL_GPL(mtd_del_partition);
Heiko Schocherddf7bcf2014-07-15 16:08:42 +0200656#endif
Heiko Schocherff94bc42014-06-24 10:10:04 +0200657
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100658/*
659 * This function, given a master MTD object and a partition table, creates
660 * and registers slave MTD objects which are bound to the master according to
661 * the partition definitions.
Stefan Roese8d2effe2009-05-11 16:03:55 +0200662 *
663 * We don't register the master, or expect the caller to have done so,
664 * for reasons of data integrity.
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100665 */
666
667int add_mtd_partitions(struct mtd_info *master,
668 const struct mtd_partition *parts,
669 int nbparts)
670{
671 struct mtd_part *slave;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200672 uint64_t cur_offset = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100673 int i;
674
Heiko Schocherff94bc42014-06-24 10:10:04 +0200675#ifdef __UBOOT__
Stefan Roese9def12c2008-11-27 14:05:15 +0100676 /*
677 * Need to init the list here, since LIST_INIT() does not
678 * work on platforms where relocation has problems (like MIPS
679 * & PPC).
680 */
681 if (mtd_partitions.next == NULL)
682 INIT_LIST_HEAD(&mtd_partitions);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200683#endif
Stefan Roese9def12c2008-11-27 14:05:15 +0100684
Joe Hershberger147162d2013-04-08 10:32:49 +0000685 debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100686
687 for (i = 0; i < nbparts; i++) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200688 slave = allocate_partition(master, parts + i, i, cur_offset);
689 if (IS_ERR(slave))
690 return PTR_ERR(slave);
691
692 mutex_lock(&mtd_partitions_mutex);
693 list_add(&slave->list, &mtd_partitions);
694 mutex_unlock(&mtd_partitions_mutex);
695
696 add_mtd_device(&slave->mtd);
697
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100698 cur_offset = slave->offset + slave->mtd.size;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100699 }
700
701 return 0;
702}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200703
704#ifndef __UBOOT__
705static DEFINE_SPINLOCK(part_parser_lock);
706static LIST_HEAD(part_parsers);
707
708static struct mtd_part_parser *get_partition_parser(const char *name)
709{
710 struct mtd_part_parser *p, *ret = NULL;
711
712 spin_lock(&part_parser_lock);
713
714 list_for_each_entry(p, &part_parsers, list)
715 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
716 ret = p;
717 break;
718 }
719
720 spin_unlock(&part_parser_lock);
721
722 return ret;
723}
724
725#define put_partition_parser(p) do { module_put((p)->owner); } while (0)
726
727void register_mtd_parser(struct mtd_part_parser *p)
728{
729 spin_lock(&part_parser_lock);
730 list_add(&p->list, &part_parsers);
731 spin_unlock(&part_parser_lock);
732}
733EXPORT_SYMBOL_GPL(register_mtd_parser);
734
735void deregister_mtd_parser(struct mtd_part_parser *p)
736{
737 spin_lock(&part_parser_lock);
738 list_del(&p->list);
739 spin_unlock(&part_parser_lock);
740}
741EXPORT_SYMBOL_GPL(deregister_mtd_parser);
742
743/*
744 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
745 * are changing this array!
746 */
747static const char * const default_mtd_part_types[] = {
748 "cmdlinepart",
749 "ofpart",
750 NULL
751};
752
753/**
754 * parse_mtd_partitions - parse MTD partitions
755 * @master: the master partition (describes whole MTD device)
756 * @types: names of partition parsers to try or %NULL
757 * @pparts: array of partitions found is returned here
758 * @data: MTD partition parser-specific data
759 *
760 * This function tries to find partition on MTD device @master. It uses MTD
761 * partition parsers, specified in @types. However, if @types is %NULL, then
762 * the default list of parsers is used. The default list contains only the
763 * "cmdlinepart" and "ofpart" parsers ATM.
764 * Note: If there are more then one parser in @types, the kernel only takes the
765 * partitions parsed out by the first parser.
766 *
767 * This function may return:
768 * o a negative error code in case of failure
769 * o zero if no partitions were found
770 * o a positive number of found partitions, in which case on exit @pparts will
771 * point to an array containing this number of &struct mtd_info objects.
772 */
773int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
774 struct mtd_partition **pparts,
775 struct mtd_part_parser_data *data)
776{
777 struct mtd_part_parser *parser;
778 int ret = 0;
779
780 if (!types)
781 types = default_mtd_part_types;
782
783 for ( ; ret <= 0 && *types; types++) {
784 parser = get_partition_parser(*types);
785 if (!parser && !request_module("%s", *types))
786 parser = get_partition_parser(*types);
787 if (!parser)
788 continue;
789 ret = (*parser->parse_fn)(master, pparts, data);
790 put_partition_parser(parser);
791 if (ret > 0) {
792 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
793 ret, parser->name, master->name);
794 break;
795 }
796 }
797 return ret;
798}
799#endif
800
801int mtd_is_partition(const struct mtd_info *mtd)
802{
803 struct mtd_part *part;
804 int ispart = 0;
805
806 mutex_lock(&mtd_partitions_mutex);
807 list_for_each_entry(part, &mtd_partitions, list)
808 if (&part->mtd == mtd) {
809 ispart = 1;
810 break;
811 }
812 mutex_unlock(&mtd_partitions_mutex);
813
814 return ispart;
815}
816EXPORT_SYMBOL_GPL(mtd_is_partition);
817
818/* Returns the size of the entire flash chip */
819uint64_t mtd_get_device_size(const struct mtd_info *mtd)
820{
821 if (!mtd_is_partition(mtd))
822 return mtd->size;
823
824 return PART(mtd)->master->size;
825}
826EXPORT_SYMBOL_GPL(mtd_get_device_size);