blob: 2f20b92a8825896f9dc3330453beeb6580e95240 [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#define __UBOOT__
13#ifndef __UBOOT__
14#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 Parke29c22f2008-11-19 16:20:36 +010022#include <common.h>
23#include <malloc.h>
24#include <asm/errno.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020025#include <linux/compat.h>
26#include <ubi_uboot.h>
Kyungmin Parke29c22f2008-11-19 16:20:36 +010027
Kyungmin Parke29c22f2008-11-19 16:20:36 +010028#include <linux/mtd/mtd.h>
29#include <linux/mtd/partitions.h>
Heiko Schocherff94bc42014-06-24 10:10:04 +020030#include <linux/err.h>
31
32#include "mtdcore.h"
Kyungmin Parke29c22f2008-11-19 16:20:36 +010033
34/* Our partition linked list */
Heiko Schocherff94bc42014-06-24 10:10:04 +020035static LIST_HEAD(mtd_partitions);
36#ifndef __UBOOT__
37static DEFINE_MUTEX(mtd_partitions_mutex);
38#else
39DEFINE_MUTEX(mtd_partitions_mutex);
40#endif
Kyungmin Parke29c22f2008-11-19 16:20:36 +010041
42/* Our partition node structure */
43struct mtd_part {
44 struct mtd_info mtd;
45 struct mtd_info *master;
Stefan Roese8d2effe2009-05-11 16:03:55 +020046 uint64_t offset;
Kyungmin Parke29c22f2008-11-19 16:20:36 +010047 struct list_head list;
Kyungmin Parke29c22f2008-11-19 16:20:36 +010048};
49
50/*
51 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
52 * the pointer to that structure with this macro.
53 */
54#define PART(x) ((struct mtd_part *)(x))
55
56
Heiko Schocherff94bc42014-06-24 10:10:04 +020057#ifdef __UBOOT__
58/* from mm/util.c */
59
60/**
61 * kstrdup - allocate space for and copy an existing string
62 * @s: the string to duplicate
63 * @gfp: the GFP mask used in the kmalloc() call when allocating memory
64 */
65char *kstrdup(const char *s, gfp_t gfp)
66{
67 size_t len;
68 char *buf;
69
70 if (!s)
71 return NULL;
72
73 len = strlen(s) + 1;
74 buf = kmalloc(len, gfp);
75 if (buf)
76 memcpy(buf, s, len);
77 return buf;
78}
79#endif
80
Kyungmin Parke29c22f2008-11-19 16:20:36 +010081/*
82 * MTD methods which simply translate the effective address and pass through
83 * to the _real_ device.
84 */
85
Stefan Roese8d2effe2009-05-11 16:03:55 +020086static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
87 size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +010088{
89 struct mtd_part *part = PART(mtd);
Stefan Roese8d2effe2009-05-11 16:03:55 +020090 struct mtd_ecc_stats stats;
Kyungmin Parke29c22f2008-11-19 16:20:36 +010091 int res;
92
Stefan Roese8d2effe2009-05-11 16:03:55 +020093 stats = part->master->ecc_stats;
Heiko Schocherff94bc42014-06-24 10:10:04 +020094 res = part->master->_read(part->master, from + part->offset, len,
95 retlen, buf);
Paul Burton40462e52013-09-04 15:16:56 +010096 if (unlikely(mtd_is_eccerr(res)))
97 mtd->ecc_stats.failed +=
98 part->master->ecc_stats.failed - stats.failed;
99 else
100 mtd->ecc_stats.corrected +=
101 part->master->ecc_stats.corrected - stats.corrected;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100102 return res;
103}
104
Heiko Schocherff94bc42014-06-24 10:10:04 +0200105#ifndef __UBOOT__
106static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
107 size_t *retlen, void **virt, resource_size_t *phys)
108{
109 struct mtd_part *part = PART(mtd);
110
111 return part->master->_point(part->master, from + part->offset, len,
112 retlen, virt, phys);
113}
114
115static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
116{
117 struct mtd_part *part = PART(mtd);
118
119 return part->master->_unpoint(part->master, from + part->offset, len);
120}
121#endif
122
123static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
124 unsigned long len,
125 unsigned long offset,
126 unsigned long flags)
127{
128 struct mtd_part *part = PART(mtd);
129
130 offset += part->offset;
131 return part->master->_get_unmapped_area(part->master, len, offset,
132 flags);
133}
134
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100135static int part_read_oob(struct mtd_info *mtd, loff_t from,
Stefan Roese8d2effe2009-05-11 16:03:55 +0200136 struct mtd_oob_ops *ops)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100137{
138 struct mtd_part *part = PART(mtd);
139 int res;
140
141 if (from >= mtd->size)
142 return -EINVAL;
143 if (ops->datbuf && from + ops->len > mtd->size)
144 return -EINVAL;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100145
Heiko Schocherff94bc42014-06-24 10:10:04 +0200146 /*
147 * If OOB is also requested, make sure that we do not read past the end
148 * of this partition.
149 */
150 if (ops->oobbuf) {
151 size_t len, pages;
152
153 if (ops->mode == MTD_OPS_AUTO_OOB)
154 len = mtd->oobavail;
155 else
156 len = mtd->oobsize;
157 pages = mtd_div_by_ws(mtd->size, mtd);
158 pages -= mtd_div_by_ws(from, mtd);
159 if (ops->ooboffs + ops->ooblen > pages * len)
160 return -EINVAL;
161 }
162
163 res = part->master->_read_oob(part->master, from + part->offset, ops);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100164 if (unlikely(res)) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000165 if (mtd_is_bitflip(res))
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100166 mtd->ecc_stats.corrected++;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000167 if (mtd_is_eccerr(res))
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100168 mtd->ecc_stats.failed++;
169 }
170 return res;
171}
172
Stefan Roese8d2effe2009-05-11 16:03:55 +0200173static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
174 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100175{
176 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200177 return part->master->_read_user_prot_reg(part->master, from, len,
178 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100179}
180
Heiko Schocher4e67c572014-07-15 16:08:43 +0200181static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
182 size_t *retlen, struct otp_info *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100183{
184 struct mtd_part *part = PART(mtd);
Heiko Schocher4e67c572014-07-15 16:08:43 +0200185 return part->master->_get_user_prot_info(part->master, len, retlen,
186 buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100187}
188
Stefan Roese8d2effe2009-05-11 16:03:55 +0200189static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
190 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100191{
192 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200193 return part->master->_read_fact_prot_reg(part->master, from, len,
194 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100195}
196
Heiko Schocher4e67c572014-07-15 16:08:43 +0200197static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
198 size_t *retlen, struct otp_info *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100199{
200 struct mtd_part *part = PART(mtd);
Heiko Schocher4e67c572014-07-15 16:08:43 +0200201 return part->master->_get_fact_prot_info(part->master, len, retlen,
202 buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100203}
204
Stefan Roese8d2effe2009-05-11 16:03:55 +0200205static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
206 size_t *retlen, const u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100207{
208 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200209 return part->master->_write(part->master, to + part->offset, len,
210 retlen, buf);
211}
212
213static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
214 size_t *retlen, const u_char *buf)
215{
216 struct mtd_part *part = PART(mtd);
217 return part->master->_panic_write(part->master, to + part->offset, len,
218 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100219}
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100220
221static int part_write_oob(struct mtd_info *mtd, loff_t to,
Stefan Roese8d2effe2009-05-11 16:03:55 +0200222 struct mtd_oob_ops *ops)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100223{
224 struct mtd_part *part = PART(mtd);
225
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100226 if (to >= mtd->size)
227 return -EINVAL;
228 if (ops->datbuf && to + ops->len > mtd->size)
229 return -EINVAL;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200230 return part->master->_write_oob(part->master, to + part->offset, ops);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100231}
232
Stefan Roese8d2effe2009-05-11 16:03:55 +0200233static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
234 size_t len, size_t *retlen, u_char *buf)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100235{
236 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200237 return part->master->_write_user_prot_reg(part->master, from, len,
238 retlen, buf);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100239}
240
Stefan Roese8d2effe2009-05-11 16:03:55 +0200241static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
242 size_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100243{
244 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200245 return part->master->_lock_user_prot_reg(part->master, from, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100246}
247
Heiko Schocherff94bc42014-06-24 10:10:04 +0200248#ifndef __UBOOT__
249static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
250 unsigned long count, loff_t to, size_t *retlen)
251{
252 struct mtd_part *part = PART(mtd);
253 return part->master->_writev(part->master, vecs, count,
254 to + part->offset, retlen);
255}
256#endif
257
Stefan Roese8d2effe2009-05-11 16:03:55 +0200258static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100259{
260 struct mtd_part *part = PART(mtd);
261 int ret;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000262
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100263 instr->addr += part->offset;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200264 ret = part->master->_erase(part->master, instr);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100265 if (ret) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200266 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100267 instr->fail_addr -= part->offset;
268 instr->addr -= part->offset;
269 }
270 return ret;
271}
272
273void mtd_erase_callback(struct erase_info *instr)
274{
Sergey Lapindfe64e22013-01-14 03:46:50 +0000275 if (instr->mtd->_erase == part_erase) {
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100276 struct mtd_part *part = PART(instr->mtd);
277
Stefan Roese8d2effe2009-05-11 16:03:55 +0200278 if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100279 instr->fail_addr -= part->offset;
280 instr->addr -= part->offset;
281 }
282 if (instr->callback)
283 instr->callback(instr);
284}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200285EXPORT_SYMBOL_GPL(mtd_erase_callback);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100286
Stefan Roese8d2effe2009-05-11 16:03:55 +0200287static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100288{
289 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200290 return part->master->_lock(part->master, ofs + part->offset, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100291}
292
Stefan Roese8d2effe2009-05-11 16:03:55 +0200293static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100294{
295 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200296 return part->master->_unlock(part->master, ofs + part->offset, len);
297}
298
299static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
300{
301 struct mtd_part *part = PART(mtd);
302 return part->master->_is_locked(part->master, ofs + part->offset, len);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100303}
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100304
305static void part_sync(struct mtd_info *mtd)
306{
307 struct mtd_part *part = PART(mtd);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200308 part->master->_sync(part->master);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100309}
310
Heiko Schocherff94bc42014-06-24 10:10:04 +0200311#ifndef __UBOOT__
312static int part_suspend(struct mtd_info *mtd)
313{
314 struct mtd_part *part = PART(mtd);
315 return part->master->_suspend(part->master);
316}
317
318static void part_resume(struct mtd_info *mtd)
319{
320 struct mtd_part *part = PART(mtd);
321 part->master->_resume(part->master);
322}
323#endif
324
Stefan Roese8d2effe2009-05-11 16:03:55 +0200325static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100326{
327 struct mtd_part *part = PART(mtd);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100328 ofs += part->offset;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200329 return part->master->_block_isbad(part->master, ofs);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100330}
331
Stefan Roese8d2effe2009-05-11 16:03:55 +0200332static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100333{
334 struct mtd_part *part = PART(mtd);
335 int res;
336
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100337 ofs += part->offset;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200338 res = part->master->_block_markbad(part->master, ofs);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100339 if (!res)
340 mtd->ecc_stats.badblocks++;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100341 return res;
342}
343
Heiko Schocherff94bc42014-06-24 10:10:04 +0200344static inline void free_partition(struct mtd_part *p)
345{
346 kfree(p->mtd.name);
347 kfree(p);
348}
349
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100350/*
351 * This function unregisters and destroy all slave MTD objects which are
352 * attached to the given master MTD object.
353 */
354
355int del_mtd_partitions(struct mtd_info *master)
356{
Stefan Roese8d2effe2009-05-11 16:03:55 +0200357 struct mtd_part *slave, *next;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200358 int ret, err = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100359
Heiko Schocherff94bc42014-06-24 10:10:04 +0200360 mutex_lock(&mtd_partitions_mutex);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200361 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100362 if (slave->master == master) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200363 ret = del_mtd_device(&slave->mtd);
364 if (ret < 0) {
365 err = ret;
366 continue;
367 }
Stefan Roese8d2effe2009-05-11 16:03:55 +0200368 list_del(&slave->list);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200369 free_partition(slave);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200370 }
Heiko Schocherff94bc42014-06-24 10:10:04 +0200371 mutex_unlock(&mtd_partitions_mutex);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200372
Heiko Schocherff94bc42014-06-24 10:10:04 +0200373 return err;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200374}
375
Heiko Schocherff94bc42014-06-24 10:10:04 +0200376static struct mtd_part *allocate_partition(struct mtd_info *master,
377 const struct mtd_partition *part, int partno,
378 uint64_t cur_offset)
Stefan Roese8d2effe2009-05-11 16:03:55 +0200379{
380 struct mtd_part *slave;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200381 char *name;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200382
383 /* allocate the partition structure */
384 slave = kzalloc(sizeof(*slave), GFP_KERNEL);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200385 name = kstrdup(part->name, GFP_KERNEL);
386 if (!name || !slave) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200387 printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
Heiko Schocherff94bc42014-06-24 10:10:04 +0200388 master->name);
389 kfree(name);
390 kfree(slave);
391 return ERR_PTR(-ENOMEM);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200392 }
Stefan Roese8d2effe2009-05-11 16:03:55 +0200393
394 /* set up the MTD object for this partition */
395 slave->mtd.type = master->type;
396 slave->mtd.flags = master->flags & ~part->mask_flags;
397 slave->mtd.size = part->size;
398 slave->mtd.writesize = master->writesize;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200399 slave->mtd.writebufsize = master->writebufsize;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200400 slave->mtd.oobsize = master->oobsize;
401 slave->mtd.oobavail = master->oobavail;
402 slave->mtd.subpage_sft = master->subpage_sft;
403
Heiko Schocherff94bc42014-06-24 10:10:04 +0200404 slave->mtd.name = name;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200405 slave->mtd.owner = master->owner;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200406#ifndef __UBOOT__
407 slave->mtd.backing_dev_info = master->backing_dev_info;
408
409 /* NOTE: we don't arrange MTDs as a tree; it'd be error-prone
410 * to have the same data be in two different partitions.
411 */
412 slave->mtd.dev.parent = master->dev.parent;
413#endif
Stefan Roese8d2effe2009-05-11 16:03:55 +0200414
Sergey Lapindfe64e22013-01-14 03:46:50 +0000415 slave->mtd._read = part_read;
416 slave->mtd._write = part_write;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200417
Heiko Schocherff94bc42014-06-24 10:10:04 +0200418 if (master->_panic_write)
419 slave->mtd._panic_write = part_panic_write;
420
421#ifndef __UBOOT__
422 if (master->_point && master->_unpoint) {
423 slave->mtd._point = part_point;
424 slave->mtd._unpoint = part_unpoint;
425 }
426#endif
427
428 if (master->_get_unmapped_area)
429 slave->mtd._get_unmapped_area = part_get_unmapped_area;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000430 if (master->_read_oob)
431 slave->mtd._read_oob = part_read_oob;
432 if (master->_write_oob)
433 slave->mtd._write_oob = part_write_oob;
434 if (master->_read_user_prot_reg)
435 slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
436 if (master->_read_fact_prot_reg)
437 slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
438 if (master->_write_user_prot_reg)
439 slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
440 if (master->_lock_user_prot_reg)
441 slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
442 if (master->_get_user_prot_info)
443 slave->mtd._get_user_prot_info = part_get_user_prot_info;
444 if (master->_get_fact_prot_info)
445 slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
446 if (master->_sync)
447 slave->mtd._sync = part_sync;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200448#ifndef __UBOOT__
449 if (!partno && !master->dev.class && master->_suspend &&
450 master->_resume) {
451 slave->mtd._suspend = part_suspend;
452 slave->mtd._resume = part_resume;
453 }
454 if (master->_writev)
455 slave->mtd._writev = part_writev;
456#endif
Sergey Lapindfe64e22013-01-14 03:46:50 +0000457 if (master->_lock)
458 slave->mtd._lock = part_lock;
459 if (master->_unlock)
460 slave->mtd._unlock = part_unlock;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200461 if (master->_is_locked)
462 slave->mtd._is_locked = part_is_locked;
Sergey Lapindfe64e22013-01-14 03:46:50 +0000463 if (master->_block_isbad)
464 slave->mtd._block_isbad = part_block_isbad;
465 if (master->_block_markbad)
466 slave->mtd._block_markbad = part_block_markbad;
467 slave->mtd._erase = part_erase;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200468 slave->master = master;
469 slave->offset = part->offset;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200470
471 if (slave->offset == MTDPART_OFS_APPEND)
472 slave->offset = cur_offset;
473 if (slave->offset == MTDPART_OFS_NXTBLK) {
474 slave->offset = cur_offset;
475 if (mtd_mod_by_eb(cur_offset, master) != 0) {
476 /* Round up to next erasesize */
477 slave->offset = (mtd_div_by_eb(cur_offset, master) + 1) * master->erasesize;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200478 debug("Moving partition %d: "
479 "0x%012llx -> 0x%012llx\n", partno,
480 (unsigned long long)cur_offset, (unsigned long long)slave->offset);
481 }
482 }
483 if (slave->offset == MTDPART_OFS_RETAIN) {
484 slave->offset = cur_offset;
485 if (master->size - slave->offset >= slave->mtd.size) {
486 slave->mtd.size = master->size - slave->offset
487 - slave->mtd.size;
488 } else {
489 debug("mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
490 part->name, master->size - slave->offset,
491 slave->mtd.size);
492 /* register to preserve ordering */
493 goto out_register;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200494 }
495 }
496 if (slave->mtd.size == MTDPART_SIZ_FULL)
497 slave->mtd.size = master->size - slave->offset;
498
Heiko Schocherff94bc42014-06-24 10:10:04 +0200499 debug("0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
500 (unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
Stefan Roese8d2effe2009-05-11 16:03:55 +0200501
502 /* let's do some sanity checks */
503 if (slave->offset >= master->size) {
504 /* let's register it anyway to preserve ordering */
505 slave->offset = 0;
506 slave->mtd.size = 0;
507 printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
508 part->name);
509 goto out_register;
510 }
511 if (slave->offset + slave->mtd.size > master->size) {
512 slave->mtd.size = master->size - slave->offset;
513 printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
514 part->name, master->name, (unsigned long long)slave->mtd.size);
515 }
516 if (master->numeraseregions > 1) {
517 /* Deal with variable erase size stuff */
518 int i, max = master->numeraseregions;
519 u64 end = slave->offset + slave->mtd.size;
520 struct mtd_erase_region_info *regions = master->eraseregions;
521
522 /* Find the first erase regions which is part of this
523 * partition. */
524 for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
525 ;
526 /* The loop searched for the region _behind_ the first one */
Heiko Schocherff94bc42014-06-24 10:10:04 +0200527 if (i > 0)
528 i--;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200529
530 /* Pick biggest erasesize */
531 for (; i < max && regions[i].offset < end; i++) {
532 if (slave->mtd.erasesize < regions[i].erasesize) {
533 slave->mtd.erasesize = regions[i].erasesize;
534 }
535 }
536 BUG_ON(slave->mtd.erasesize == 0);
537 } else {
538 /* Single erase size */
539 slave->mtd.erasesize = master->erasesize;
540 }
541
542 if ((slave->mtd.flags & MTD_WRITEABLE) &&
543 mtd_mod_by_eb(slave->offset, &slave->mtd)) {
544 /* Doesn't start on a boundary of major erase size */
545 /* FIXME: Let it be writable if it is on a boundary of
546 * _minor_ erase size though */
547 slave->mtd.flags &= ~MTD_WRITEABLE;
548 printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase block boundary -- force read-only\n",
549 part->name);
550 }
551 if ((slave->mtd.flags & MTD_WRITEABLE) &&
552 mtd_mod_by_eb(slave->mtd.size, &slave->mtd)) {
553 slave->mtd.flags &= ~MTD_WRITEABLE;
554 printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase block -- force read-only\n",
555 part->name);
556 }
557
558 slave->mtd.ecclayout = master->ecclayout;
Heiko Schocherff94bc42014-06-24 10:10:04 +0200559 slave->mtd.ecc_step_size = master->ecc_step_size;
560 slave->mtd.ecc_strength = master->ecc_strength;
561 slave->mtd.bitflip_threshold = master->bitflip_threshold;
562
Sergey Lapindfe64e22013-01-14 03:46:50 +0000563 if (master->_block_isbad) {
Stefan Roese8d2effe2009-05-11 16:03:55 +0200564 uint64_t offs = 0;
565
566 while (offs < slave->mtd.size) {
Sergey Lapindfe64e22013-01-14 03:46:50 +0000567 if (mtd_block_isbad(master, offs + slave->offset))
Stefan Roese8d2effe2009-05-11 16:03:55 +0200568 slave->mtd.ecc_stats.badblocks++;
569 offs += slave->mtd.erasesize;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100570 }
571 }
572
Stefan Roese8d2effe2009-05-11 16:03:55 +0200573out_register:
Stefan Roese8d2effe2009-05-11 16:03:55 +0200574 return slave;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100575}
576
Heiko Schocherddf7bcf2014-07-15 16:08:42 +0200577#ifndef __UBOOT__
Heiko Schocherff94bc42014-06-24 10:10:04 +0200578int mtd_add_partition(struct mtd_info *master, const char *name,
579 long long offset, long long length)
580{
581 struct mtd_partition part;
582 struct mtd_part *p, *new;
583 uint64_t start, end;
584 int ret = 0;
585
586 /* the direct offset is expected */
587 if (offset == MTDPART_OFS_APPEND ||
588 offset == MTDPART_OFS_NXTBLK)
589 return -EINVAL;
590
591 if (length == MTDPART_SIZ_FULL)
592 length = master->size - offset;
593
594 if (length <= 0)
595 return -EINVAL;
596
597 part.name = name;
598 part.size = length;
599 part.offset = offset;
600 part.mask_flags = 0;
601 part.ecclayout = NULL;
602
603 new = allocate_partition(master, &part, -1, offset);
604 if (IS_ERR(new))
605 return PTR_ERR(new);
606
607 start = offset;
608 end = offset + length;
609
610 mutex_lock(&mtd_partitions_mutex);
611 list_for_each_entry(p, &mtd_partitions, list)
612 if (p->master == master) {
613 if ((start >= p->offset) &&
614 (start < (p->offset + p->mtd.size)))
615 goto err_inv;
616
617 if ((end >= p->offset) &&
618 (end < (p->offset + p->mtd.size)))
619 goto err_inv;
620 }
621
622 list_add(&new->list, &mtd_partitions);
623 mutex_unlock(&mtd_partitions_mutex);
624
625 add_mtd_device(&new->mtd);
626
627 return ret;
628err_inv:
629 mutex_unlock(&mtd_partitions_mutex);
630 free_partition(new);
631 return -EINVAL;
632}
633EXPORT_SYMBOL_GPL(mtd_add_partition);
634
635int mtd_del_partition(struct mtd_info *master, int partno)
636{
637 struct mtd_part *slave, *next;
638 int ret = -EINVAL;
639
640 mutex_lock(&mtd_partitions_mutex);
641 list_for_each_entry_safe(slave, next, &mtd_partitions, list)
642 if ((slave->master == master) &&
643 (slave->mtd.index == partno)) {
644 ret = del_mtd_device(&slave->mtd);
645 if (ret < 0)
646 break;
647
648 list_del(&slave->list);
649 free_partition(slave);
650 break;
651 }
652 mutex_unlock(&mtd_partitions_mutex);
653
654 return ret;
655}
656EXPORT_SYMBOL_GPL(mtd_del_partition);
Heiko Schocherddf7bcf2014-07-15 16:08:42 +0200657#endif
Heiko Schocherff94bc42014-06-24 10:10:04 +0200658
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100659/*
660 * This function, given a master MTD object and a partition table, creates
661 * and registers slave MTD objects which are bound to the master according to
662 * the partition definitions.
Stefan Roese8d2effe2009-05-11 16:03:55 +0200663 *
664 * We don't register the master, or expect the caller to have done so,
665 * for reasons of data integrity.
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100666 */
667
668int add_mtd_partitions(struct mtd_info *master,
669 const struct mtd_partition *parts,
670 int nbparts)
671{
672 struct mtd_part *slave;
Stefan Roese8d2effe2009-05-11 16:03:55 +0200673 uint64_t cur_offset = 0;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100674 int i;
675
Heiko Schocherff94bc42014-06-24 10:10:04 +0200676#ifdef __UBOOT__
Stefan Roese9def12c2008-11-27 14:05:15 +0100677 /*
678 * Need to init the list here, since LIST_INIT() does not
679 * work on platforms where relocation has problems (like MIPS
680 * & PPC).
681 */
682 if (mtd_partitions.next == NULL)
683 INIT_LIST_HEAD(&mtd_partitions);
Heiko Schocherff94bc42014-06-24 10:10:04 +0200684#endif
Stefan Roese9def12c2008-11-27 14:05:15 +0100685
Joe Hershberger147162d2013-04-08 10:32:49 +0000686 debug("Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100687
688 for (i = 0; i < nbparts; i++) {
Heiko Schocherff94bc42014-06-24 10:10:04 +0200689 slave = allocate_partition(master, parts + i, i, cur_offset);
690 if (IS_ERR(slave))
691 return PTR_ERR(slave);
692
693 mutex_lock(&mtd_partitions_mutex);
694 list_add(&slave->list, &mtd_partitions);
695 mutex_unlock(&mtd_partitions_mutex);
696
697 add_mtd_device(&slave->mtd);
698
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100699 cur_offset = slave->offset + slave->mtd.size;
Kyungmin Parke29c22f2008-11-19 16:20:36 +0100700 }
701
702 return 0;
703}
Heiko Schocherff94bc42014-06-24 10:10:04 +0200704
705#ifndef __UBOOT__
706static DEFINE_SPINLOCK(part_parser_lock);
707static LIST_HEAD(part_parsers);
708
709static struct mtd_part_parser *get_partition_parser(const char *name)
710{
711 struct mtd_part_parser *p, *ret = NULL;
712
713 spin_lock(&part_parser_lock);
714
715 list_for_each_entry(p, &part_parsers, list)
716 if (!strcmp(p->name, name) && try_module_get(p->owner)) {
717 ret = p;
718 break;
719 }
720
721 spin_unlock(&part_parser_lock);
722
723 return ret;
724}
725
726#define put_partition_parser(p) do { module_put((p)->owner); } while (0)
727
728void register_mtd_parser(struct mtd_part_parser *p)
729{
730 spin_lock(&part_parser_lock);
731 list_add(&p->list, &part_parsers);
732 spin_unlock(&part_parser_lock);
733}
734EXPORT_SYMBOL_GPL(register_mtd_parser);
735
736void deregister_mtd_parser(struct mtd_part_parser *p)
737{
738 spin_lock(&part_parser_lock);
739 list_del(&p->list);
740 spin_unlock(&part_parser_lock);
741}
742EXPORT_SYMBOL_GPL(deregister_mtd_parser);
743
744/*
745 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
746 * are changing this array!
747 */
748static const char * const default_mtd_part_types[] = {
749 "cmdlinepart",
750 "ofpart",
751 NULL
752};
753
754/**
755 * parse_mtd_partitions - parse MTD partitions
756 * @master: the master partition (describes whole MTD device)
757 * @types: names of partition parsers to try or %NULL
758 * @pparts: array of partitions found is returned here
759 * @data: MTD partition parser-specific data
760 *
761 * This function tries to find partition on MTD device @master. It uses MTD
762 * partition parsers, specified in @types. However, if @types is %NULL, then
763 * the default list of parsers is used. The default list contains only the
764 * "cmdlinepart" and "ofpart" parsers ATM.
765 * Note: If there are more then one parser in @types, the kernel only takes the
766 * partitions parsed out by the first parser.
767 *
768 * This function may return:
769 * o a negative error code in case of failure
770 * o zero if no partitions were found
771 * o a positive number of found partitions, in which case on exit @pparts will
772 * point to an array containing this number of &struct mtd_info objects.
773 */
774int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
775 struct mtd_partition **pparts,
776 struct mtd_part_parser_data *data)
777{
778 struct mtd_part_parser *parser;
779 int ret = 0;
780
781 if (!types)
782 types = default_mtd_part_types;
783
784 for ( ; ret <= 0 && *types; types++) {
785 parser = get_partition_parser(*types);
786 if (!parser && !request_module("%s", *types))
787 parser = get_partition_parser(*types);
788 if (!parser)
789 continue;
790 ret = (*parser->parse_fn)(master, pparts, data);
791 put_partition_parser(parser);
792 if (ret > 0) {
793 printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
794 ret, parser->name, master->name);
795 break;
796 }
797 }
798 return ret;
799}
800#endif
801
802int mtd_is_partition(const struct mtd_info *mtd)
803{
804 struct mtd_part *part;
805 int ispart = 0;
806
807 mutex_lock(&mtd_partitions_mutex);
808 list_for_each_entry(part, &mtd_partitions, list)
809 if (&part->mtd == mtd) {
810 ispart = 1;
811 break;
812 }
813 mutex_unlock(&mtd_partitions_mutex);
814
815 return ispart;
816}
817EXPORT_SYMBOL_GPL(mtd_is_partition);
818
819/* Returns the size of the entire flash chip */
820uint64_t mtd_get_device_size(const struct mtd_info *mtd)
821{
822 if (!mtd_is_partition(mtd))
823 return mtd->size;
824
825 return PART(mtd)->master->size;
826}
827EXPORT_SYMBOL_GPL(mtd_get_device_size);