mtdpart.c 24.9 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3
/*
 * Simple MTD partitioning layer
 *
D
David Woodhouse 已提交
4 5 6
 * Copyright © 2000 Nicolas Pitre <nico@fluxnic.net>
 * Copyright © 2002 Thomas Gleixner <gleixner@linutronix.de>
 * Copyright © 2000-2010 David Woodhouse <dwmw2@infradead.org>
L
Linus Torvalds 已提交
7
 *
D
David Woodhouse 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
L
Linus Torvalds 已提交
21
 *
22
 */
L
Linus Torvalds 已提交
23 24 25 26 27 28 29 30 31

#include <linux/module.h>
#include <linux/types.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/list.h>
#include <linux/kmod.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
32
#include <linux/err.h>
L
Linus Torvalds 已提交
33

34 35
#include "mtdcore.h"

L
Linus Torvalds 已提交
36 37
/* Our partition linked list */
static LIST_HEAD(mtd_partitions);
38
static DEFINE_MUTEX(mtd_partitions_mutex);
L
Linus Torvalds 已提交
39 40 41 42 43

/* Our partition node structure */
struct mtd_part {
	struct mtd_info mtd;
	struct mtd_info *master;
44
	uint64_t offset;
L
Linus Torvalds 已提交
45 46 47 48 49
	struct list_head list;
};

/*
 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
50
 * the pointer to that structure.
L
Linus Torvalds 已提交
51
 */
52 53 54 55
static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
{
	return container_of(mtd, struct mtd_part, mtd);
}
L
Linus Torvalds 已提交
56

57 58

/*
L
Linus Torvalds 已提交
59 60 61 62
 * MTD methods which simply translate the effective address and pass through
 * to the _real_ device.
 */

63 64
static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
		size_t *retlen, u_char *buf)
L
Linus Torvalds 已提交
65
{
66
	struct mtd_part *part = mtd_to_part(mtd);
67
	struct mtd_ecc_stats stats;
68 69
	int res;

70
	stats = part->master->ecc_stats;
M
Mike Dunn 已提交
71 72
	res = part->master->_read(part->master, from + part->offset, len,
				  retlen, buf);
73 74 75 76 77 78
	if (unlikely(mtd_is_eccerr(res)))
		mtd->ecc_stats.failed +=
			part->master->ecc_stats.failed - stats.failed;
	else
		mtd->ecc_stats.corrected +=
			part->master->ecc_stats.corrected - stats.corrected;
79
	return res;
L
Linus Torvalds 已提交
80 81
}

82 83
static int part_point(struct mtd_info *mtd, loff_t from, size_t len,
		size_t *retlen, void **virt, resource_size_t *phys)
L
Linus Torvalds 已提交
84
{
85
	struct mtd_part *part = mtd_to_part(mtd);
86

M
Mike Dunn 已提交
87 88
	return part->master->_point(part->master, from + part->offset, len,
				    retlen, virt, phys);
L
Linus Torvalds 已提交
89
}
90

91
static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
L
Linus Torvalds 已提交
92
{
93
	struct mtd_part *part = mtd_to_part(mtd);
L
Linus Torvalds 已提交
94

M
Mike Dunn 已提交
95
	return part->master->_unpoint(part->master, from + part->offset, len);
L
Linus Torvalds 已提交
96 97
}

98 99 100 101 102
static unsigned long part_get_unmapped_area(struct mtd_info *mtd,
					    unsigned long len,
					    unsigned long offset,
					    unsigned long flags)
{
103
	struct mtd_part *part = mtd_to_part(mtd);
104 105

	offset += part->offset;
M
Mike Dunn 已提交
106 107
	return part->master->_get_unmapped_area(part->master, len, offset,
						flags);
108 109
}

110
static int part_read_oob(struct mtd_info *mtd, loff_t from,
111
		struct mtd_oob_ops *ops)
L
Linus Torvalds 已提交
112
{
113
	struct mtd_part *part = mtd_to_part(mtd);
114
	int res;
115

L
Linus Torvalds 已提交
116
	if (from >= mtd->size)
117
		return -EINVAL;
118
	if (ops->datbuf && from + ops->len > mtd->size)
119
		return -EINVAL;
120

121 122 123 124 125 126 127
	/*
	 * If OOB is also requested, make sure that we do not read past the end
	 * of this partition.
	 */
	if (ops->oobbuf) {
		size_t len, pages;

128
		len = mtd_oobavail(mtd, ops);
129 130 131 132 133 134
		pages = mtd_div_by_ws(mtd->size, mtd);
		pages -= mtd_div_by_ws(from, mtd);
		if (ops->ooboffs + ops->ooblen > pages * len)
			return -EINVAL;
	}

M
Mike Dunn 已提交
135
	res = part->master->_read_oob(part->master, from + part->offset, ops);
136
	if (unlikely(res)) {
137
		if (mtd_is_bitflip(res))
138
			mtd->ecc_stats.corrected++;
139
		if (mtd_is_eccerr(res))
140 141 142
			mtd->ecc_stats.failed++;
	}
	return res;
L
Linus Torvalds 已提交
143 144
}

145 146
static int part_read_user_prot_reg(struct mtd_info *mtd, loff_t from,
		size_t len, size_t *retlen, u_char *buf)
L
Linus Torvalds 已提交
147
{
148
	struct mtd_part *part = mtd_to_part(mtd);
M
Mike Dunn 已提交
149 150
	return part->master->_read_user_prot_reg(part->master, from, len,
						 retlen, buf);
L
Linus Torvalds 已提交
151 152
}

153 154
static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
				   size_t *retlen, struct otp_info *buf)
155
{
156
	struct mtd_part *part = mtd_to_part(mtd);
157 158
	return part->master->_get_user_prot_info(part->master, len, retlen,
						 buf);
159 160
}

161 162
static int part_read_fact_prot_reg(struct mtd_info *mtd, loff_t from,
		size_t len, size_t *retlen, u_char *buf)
L
Linus Torvalds 已提交
163
{
164
	struct mtd_part *part = mtd_to_part(mtd);
M
Mike Dunn 已提交
165 166
	return part->master->_read_fact_prot_reg(part->master, from, len,
						 retlen, buf);
L
Linus Torvalds 已提交
167 168
}

169 170
static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
				   size_t *retlen, struct otp_info *buf)
171
{
172
	struct mtd_part *part = mtd_to_part(mtd);
173 174
	return part->master->_get_fact_prot_info(part->master, len, retlen,
						 buf);
175 176
}

177 178
static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
		size_t *retlen, const u_char *buf)
L
Linus Torvalds 已提交
179
{
180
	struct mtd_part *part = mtd_to_part(mtd);
M
Mike Dunn 已提交
181 182
	return part->master->_write(part->master, to + part->offset, len,
				    retlen, buf);
L
Linus Torvalds 已提交
183 184
}

185 186
static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
		size_t *retlen, const u_char *buf)
187
{
188
	struct mtd_part *part = mtd_to_part(mtd);
M
Mike Dunn 已提交
189 190
	return part->master->_panic_write(part->master, to + part->offset, len,
					  retlen, buf);
191 192
}

193
static int part_write_oob(struct mtd_info *mtd, loff_t to,
194
		struct mtd_oob_ops *ops)
L
Linus Torvalds 已提交
195
{
196
	struct mtd_part *part = mtd_to_part(mtd);
197

L
Linus Torvalds 已提交
198
	if (to >= mtd->size)
199
		return -EINVAL;
200
	if (ops->datbuf && to + ops->len > mtd->size)
201
		return -EINVAL;
M
Mike Dunn 已提交
202
	return part->master->_write_oob(part->master, to + part->offset, ops);
L
Linus Torvalds 已提交
203 204
}

205 206
static int part_write_user_prot_reg(struct mtd_info *mtd, loff_t from,
		size_t len, size_t *retlen, u_char *buf)
L
Linus Torvalds 已提交
207
{
208
	struct mtd_part *part = mtd_to_part(mtd);
M
Mike Dunn 已提交
209 210
	return part->master->_write_user_prot_reg(part->master, from, len,
						  retlen, buf);
L
Linus Torvalds 已提交
211 212
}

213 214
static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
		size_t len)
215
{
216
	struct mtd_part *part = mtd_to_part(mtd);
M
Mike Dunn 已提交
217
	return part->master->_lock_user_prot_reg(part->master, from, len);
218 219
}

220 221
static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
		unsigned long count, loff_t to, size_t *retlen)
L
Linus Torvalds 已提交
222
{
223
	struct mtd_part *part = mtd_to_part(mtd);
M
Mike Dunn 已提交
224 225
	return part->master->_writev(part->master, vecs, count,
				     to + part->offset, retlen);
L
Linus Torvalds 已提交
226 227
}

228
static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
L
Linus Torvalds 已提交
229
{
230
	struct mtd_part *part = mtd_to_part(mtd);
L
Linus Torvalds 已提交
231
	int ret;
232

L
Linus Torvalds 已提交
233
	instr->addr += part->offset;
M
Mike Dunn 已提交
234
	ret = part->master->_erase(part->master, instr);
235
	if (ret) {
236
		if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
237 238 239
			instr->fail_addr -= part->offset;
		instr->addr -= part->offset;
	}
L
Linus Torvalds 已提交
240 241 242 243 244
	return ret;
}

void mtd_erase_callback(struct erase_info *instr)
{
245
	if (instr->mtd->_erase == part_erase) {
246
		struct mtd_part *part = mtd_to_part(instr->mtd);
L
Linus Torvalds 已提交
247

248
		if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
L
Linus Torvalds 已提交
249 250 251 252 253 254 255 256
			instr->fail_addr -= part->offset;
		instr->addr -= part->offset;
	}
	if (instr->callback)
		instr->callback(instr);
}
EXPORT_SYMBOL_GPL(mtd_erase_callback);

257
static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
L
Linus Torvalds 已提交
258
{
259
	struct mtd_part *part = mtd_to_part(mtd);
M
Mike Dunn 已提交
260
	return part->master->_lock(part->master, ofs + part->offset, len);
L
Linus Torvalds 已提交
261 262
}

263
static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
L
Linus Torvalds 已提交
264
{
265
	struct mtd_part *part = mtd_to_part(mtd);
M
Mike Dunn 已提交
266
	return part->master->_unlock(part->master, ofs + part->offset, len);
L
Linus Torvalds 已提交
267 268
}

269 270
static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
{
271
	struct mtd_part *part = mtd_to_part(mtd);
M
Mike Dunn 已提交
272
	return part->master->_is_locked(part->master, ofs + part->offset, len);
273 274
}

L
Linus Torvalds 已提交
275 276
static void part_sync(struct mtd_info *mtd)
{
277
	struct mtd_part *part = mtd_to_part(mtd);
M
Mike Dunn 已提交
278
	part->master->_sync(part->master);
L
Linus Torvalds 已提交
279 280 281 282
}

static int part_suspend(struct mtd_info *mtd)
{
283
	struct mtd_part *part = mtd_to_part(mtd);
M
Mike Dunn 已提交
284
	return part->master->_suspend(part->master);
L
Linus Torvalds 已提交
285 286 287 288
}

static void part_resume(struct mtd_info *mtd)
{
289
	struct mtd_part *part = mtd_to_part(mtd);
M
Mike Dunn 已提交
290
	part->master->_resume(part->master);
L
Linus Torvalds 已提交
291 292
}

293 294
static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
{
295
	struct mtd_part *part = mtd_to_part(mtd);
296 297 298 299
	ofs += part->offset;
	return part->master->_block_isreserved(part->master, ofs);
}

300
static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
L
Linus Torvalds 已提交
301
{
302
	struct mtd_part *part = mtd_to_part(mtd);
L
Linus Torvalds 已提交
303
	ofs += part->offset;
M
Mike Dunn 已提交
304
	return part->master->_block_isbad(part->master, ofs);
L
Linus Torvalds 已提交
305 306
}

307
static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
L
Linus Torvalds 已提交
308
{
309
	struct mtd_part *part = mtd_to_part(mtd);
310 311
	int res;

L
Linus Torvalds 已提交
312
	ofs += part->offset;
M
Mike Dunn 已提交
313
	res = part->master->_block_markbad(part->master, ofs);
314 315 316
	if (!res)
		mtd->ecc_stats.badblocks++;
	return res;
L
Linus Torvalds 已提交
317 318
}

319 320 321 322 323 324 325 326 327 328 329 330
static int part_get_device(struct mtd_info *mtd)
{
	struct mtd_part *part = mtd_to_part(mtd);
	return part->master->_get_device(part->master);
}

static void part_put_device(struct mtd_info *mtd)
{
	struct mtd_part *part = mtd_to_part(mtd);
	part->master->_put_device(part->master);
}

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
			      struct mtd_oob_region *oobregion)
{
	struct mtd_part *part = mtd_to_part(mtd);

	return mtd_ooblayout_ecc(part->master, section, oobregion);
}

static int part_ooblayout_free(struct mtd_info *mtd, int section,
			       struct mtd_oob_region *oobregion)
{
	struct mtd_part *part = mtd_to_part(mtd);

	return mtd_ooblayout_free(part->master, section, oobregion);
}

static const struct mtd_ooblayout_ops part_ooblayout_ops = {
	.ecc = part_ooblayout_ecc,
	.free = part_ooblayout_free,
};

352 353 354 355 356 357 358 359
static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
{
	struct mtd_part *part = mtd_to_part(mtd);

	return part->master->_max_bad_blocks(part->master,
					     ofs + part->offset, len);
}

360 361 362 363 364 365
static inline void free_partition(struct mtd_part *p)
{
	kfree(p->mtd.name);
	kfree(p);
}

366 367
/*
 * This function unregisters and destroy all slave MTD objects which are
L
Linus Torvalds 已提交
368 369 370 371 372
 * attached to the given master MTD object.
 */

int del_mtd_partitions(struct mtd_info *master)
{
373
	struct mtd_part *slave, *next;
374
	int ret, err = 0;
L
Linus Torvalds 已提交
375

376
	mutex_lock(&mtd_partitions_mutex);
377
	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
L
Linus Torvalds 已提交
378
		if (slave->master == master) {
379 380 381 382 383
			ret = del_mtd_device(&slave->mtd);
			if (ret < 0) {
				err = ret;
				continue;
			}
384
			list_del(&slave->list);
385
			free_partition(slave);
L
Linus Torvalds 已提交
386
		}
387
	mutex_unlock(&mtd_partitions_mutex);
L
Linus Torvalds 已提交
388

389
	return err;
L
Linus Torvalds 已提交
390 391
}

392 393 394
static struct mtd_part *allocate_partition(struct mtd_info *master,
			const struct mtd_partition *part, int partno,
			uint64_t cur_offset)
395
{
396 397
	int wr_alignment = (master->flags & MTD_NO_ERASE) ? master->writesize:
							    master->erasesize;
398
	struct mtd_part *slave;
399
	u32 remainder;
400
	char *name;
401
	u64 tmp;
402 403

	/* allocate the partition structure */
404
	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
405 406
	name = kstrdup(part->name, GFP_KERNEL);
	if (!name || !slave) {
407
		printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
408 409 410 411
		       master->name);
		kfree(name);
		kfree(slave);
		return ERR_PTR(-ENOMEM);
412 413 414 415 416 417 418
	}

	/* set up the MTD object for this partition */
	slave->mtd.type = master->type;
	slave->mtd.flags = master->flags & ~part->mask_flags;
	slave->mtd.size = part->size;
	slave->mtd.writesize = master->writesize;
419
	slave->mtd.writebufsize = master->writebufsize;
420 421 422
	slave->mtd.oobsize = master->oobsize;
	slave->mtd.oobavail = master->oobavail;
	slave->mtd.subpage_sft = master->subpage_sft;
423
	slave->mtd.pairing = master->pairing;
424

425
	slave->mtd.name = name;
426 427
	slave->mtd.owner = master->owner;

428 429 430 431 432 433 434
	/* NOTE: Historically, we didn't arrange MTDs as a tree out of
	 * concern for showing the same data in multiple partitions.
	 * However, it is very useful to have the master node present,
	 * so the MTD_PARTITIONED_MASTER option allows that. The master
	 * will have device nodes etc only if this is set, so make the
	 * parent conditional on that option. Note, this is a way to
	 * distinguish between the master and the partition in sysfs.
D
David Brownell 已提交
435
	 */
436 437 438
	slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) ?
				&master->dev :
				master->dev.parent;
439
	slave->mtd.dev.of_node = part->of_node;
D
David Brownell 已提交
440

441 442
	slave->mtd._read = part_read;
	slave->mtd._write = part_write;
443

444 445
	if (master->_panic_write)
		slave->mtd._panic_write = part_panic_write;
446

447 448 449
	if (master->_point && master->_unpoint) {
		slave->mtd._point = part_point;
		slave->mtd._unpoint = part_unpoint;
450 451
	}

452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475
	if (master->_get_unmapped_area)
		slave->mtd._get_unmapped_area = part_get_unmapped_area;
	if (master->_read_oob)
		slave->mtd._read_oob = part_read_oob;
	if (master->_write_oob)
		slave->mtd._write_oob = part_write_oob;
	if (master->_read_user_prot_reg)
		slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
	if (master->_read_fact_prot_reg)
		slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
	if (master->_write_user_prot_reg)
		slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
	if (master->_lock_user_prot_reg)
		slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
	if (master->_get_user_prot_info)
		slave->mtd._get_user_prot_info = part_get_user_prot_info;
	if (master->_get_fact_prot_info)
		slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
	if (master->_sync)
		slave->mtd._sync = part_sync;
	if (!partno && !master->dev.class && master->_suspend &&
	    master->_resume) {
			slave->mtd._suspend = part_suspend;
			slave->mtd._resume = part_resume;
476
	}
477 478 479 480 481 482 483 484
	if (master->_writev)
		slave->mtd._writev = part_writev;
	if (master->_lock)
		slave->mtd._lock = part_lock;
	if (master->_unlock)
		slave->mtd._unlock = part_unlock;
	if (master->_is_locked)
		slave->mtd._is_locked = part_is_locked;
485 486
	if (master->_block_isreserved)
		slave->mtd._block_isreserved = part_block_isreserved;
487 488 489 490
	if (master->_block_isbad)
		slave->mtd._block_isbad = part_block_isbad;
	if (master->_block_markbad)
		slave->mtd._block_markbad = part_block_markbad;
491 492
	if (master->_max_bad_blocks)
		slave->mtd._max_bad_blocks = part_max_bad_blocks;
493 494 495 496 497 498

	if (master->_get_device)
		slave->mtd._get_device = part_get_device;
	if (master->_put_device)
		slave->mtd._put_device = part_put_device;

499
	slave->mtd._erase = part_erase;
500 501 502 503 504 505
	slave->master = master;
	slave->offset = part->offset;

	if (slave->offset == MTDPART_OFS_APPEND)
		slave->offset = cur_offset;
	if (slave->offset == MTDPART_OFS_NXTBLK) {
506
		tmp = cur_offset;
507
		slave->offset = cur_offset;
508 509 510
		remainder = do_div(tmp, wr_alignment);
		if (remainder) {
			slave->offset += wr_alignment - remainder;
511
			printk(KERN_NOTICE "Moving partition %d: "
512 513
			       "0x%012llx -> 0x%012llx\n", partno,
			       (unsigned long long)cur_offset, (unsigned long long)slave->offset);
514 515
		}
	}
516 517 518 519 520 521 522 523 524 525 526 527 528
	if (slave->offset == MTDPART_OFS_RETAIN) {
		slave->offset = cur_offset;
		if (master->size - slave->offset >= slave->mtd.size) {
			slave->mtd.size = master->size - slave->offset
							- slave->mtd.size;
		} else {
			printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
				part->name, master->size - slave->offset,
				slave->mtd.size);
			/* register to preserve ordering */
			goto out_register;
		}
	}
529 530 531
	if (slave->mtd.size == MTDPART_SIZ_FULL)
		slave->mtd.size = master->size - slave->offset;

532 533
	printk(KERN_NOTICE "0x%012llx-0x%012llx : \"%s\"\n", (unsigned long long)slave->offset,
		(unsigned long long)(slave->offset + slave->mtd.size), slave->mtd.name);
534 535 536

	/* let's do some sanity checks */
	if (slave->offset >= master->size) {
537
		/* let's register it anyway to preserve ordering */
538 539
		slave->offset = 0;
		slave->mtd.size = 0;
540
		printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
541
			part->name);
542
		goto out_register;
543 544 545
	}
	if (slave->offset + slave->mtd.size > master->size) {
		slave->mtd.size = master->size - slave->offset;
546 547
		printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
			part->name, master->name, (unsigned long long)slave->mtd.size);
548
	}
549
	if (master->numeraseregions > 1) {
550
		/* Deal with variable erase size stuff */
551
		int i, max = master->numeraseregions;
552
		u64 end = slave->offset + slave->mtd.size;
553 554
		struct mtd_erase_region_info *regions = master->eraseregions;

555 556 557
		/* Find the first erase regions which is part of this
		 * partition. */
		for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
558
			;
559
		/* The loop searched for the region _behind_ the first one */
560 561
		if (i > 0)
			i--;
562

563 564
		/* Pick biggest erasesize */
		for (; i < max && regions[i].offset < end; i++) {
565 566 567 568
			if (slave->mtd.erasesize < regions[i].erasesize) {
				slave->mtd.erasesize = regions[i].erasesize;
			}
		}
569
		BUG_ON(slave->mtd.erasesize == 0);
570 571 572 573 574
	} else {
		/* Single erase size */
		slave->mtd.erasesize = master->erasesize;
	}

575 576 577
	tmp = slave->offset;
	remainder = do_div(tmp, wr_alignment);
	if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
578
		/* Doesn't start on a boundary of major erase size */
579 580
		/* FIXME: Let it be writable if it is on a boundary of
		 * _minor_ erase size though */
581
		slave->mtd.flags &= ~MTD_WRITEABLE;
582
		printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
583 584
			part->name);
	}
585 586 587 588

	tmp = slave->mtd.size;
	remainder = do_div(tmp, wr_alignment);
	if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
589
		slave->mtd.flags &= ~MTD_WRITEABLE;
590
		printk(KERN_WARNING"mtd: partition \"%s\" doesn't end on an erase/write block -- force read-only\n",
591 592 593
			part->name);
	}

594
	mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
595
	slave->mtd.ecc_step_size = master->ecc_step_size;
M
Mike Dunn 已提交
596
	slave->mtd.ecc_strength = master->ecc_strength;
597 598
	slave->mtd.bitflip_threshold = master->bitflip_threshold;

599
	if (master->_block_isbad) {
600
		uint64_t offs = 0;
601

602
		while (offs < slave->mtd.size) {
603 604 605
			if (mtd_block_isreserved(master, offs + slave->offset))
				slave->mtd.ecc_stats.bbtblocks++;
			else if (mtd_block_isbad(master, offs + slave->offset))
606 607 608 609 610
				slave->mtd.ecc_stats.badblocks++;
			offs += slave->mtd.erasesize;
		}
	}

611
out_register:
612 613 614
	return slave;
}

615 616 617 618
static ssize_t mtd_partition_offset_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct mtd_info *mtd = dev_get_drvdata(dev);
619
	struct mtd_part *part = mtd_to_part(mtd);
620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638
	return snprintf(buf, PAGE_SIZE, "%lld\n", part->offset);
}

static DEVICE_ATTR(offset, S_IRUGO, mtd_partition_offset_show, NULL);

static const struct attribute *mtd_partition_attrs[] = {
	&dev_attr_offset.attr,
	NULL
};

static int mtd_add_partition_attrs(struct mtd_part *new)
{
	int ret = sysfs_create_files(&new->mtd.dev.kobj, mtd_partition_attrs);
	if (ret)
		printk(KERN_WARNING
		       "mtd: failed to create partition attrs, err=%d\n", ret);
	return ret;
}

639
int mtd_add_partition(struct mtd_info *master, const char *name,
640 641 642
		      long long offset, long long length)
{
	struct mtd_partition part;
643
	struct mtd_part *new;
644 645 646 647 648 649 650 651 652 653 654 655 656
	int ret = 0;

	/* the direct offset is expected */
	if (offset == MTDPART_OFS_APPEND ||
	    offset == MTDPART_OFS_NXTBLK)
		return -EINVAL;

	if (length == MTDPART_SIZ_FULL)
		length = master->size - offset;

	if (length <= 0)
		return -EINVAL;

657
	memset(&part, 0, sizeof(part));
658 659 660 661 662 663 664 665 666 667 668 669 670 671
	part.name = name;
	part.size = length;
	part.offset = offset;

	new = allocate_partition(master, &part, -1, offset);
	if (IS_ERR(new))
		return PTR_ERR(new);

	mutex_lock(&mtd_partitions_mutex);
	list_add(&new->list, &mtd_partitions);
	mutex_unlock(&mtd_partitions_mutex);

	add_mtd_device(&new->mtd);

672 673
	mtd_add_partition_attrs(new);

674 675 676 677 678 679 680 681 682 683 684 685 686
	return ret;
}
EXPORT_SYMBOL_GPL(mtd_add_partition);

int mtd_del_partition(struct mtd_info *master, int partno)
{
	struct mtd_part *slave, *next;
	int ret = -EINVAL;

	mutex_lock(&mtd_partitions_mutex);
	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
		if ((slave->master == master) &&
		    (slave->mtd.index == partno)) {
687 688
			sysfs_remove_files(&slave->mtd.dev.kobj,
					   mtd_partition_attrs);
689 690 691 692 693 694 695 696 697 698 699 700 701 702
			ret = del_mtd_device(&slave->mtd);
			if (ret < 0)
				break;

			list_del(&slave->list);
			free_partition(slave);
			break;
		}
	mutex_unlock(&mtd_partitions_mutex);

	return ret;
}
EXPORT_SYMBOL_GPL(mtd_del_partition);

L
Linus Torvalds 已提交
703 704 705 706
/*
 * This function, given a master MTD object and a partition table, creates
 * and registers slave MTD objects which are bound to the master according to
 * the partition definitions.
D
David Brownell 已提交
707
 *
708 709
 * For historical reasons, this function's caller only registers the master
 * if the MTD_PARTITIONED_MASTER config option is set.
L
Linus Torvalds 已提交
710 711
 */

712
int add_mtd_partitions(struct mtd_info *master,
L
Linus Torvalds 已提交
713 714 715 716
		       const struct mtd_partition *parts,
		       int nbparts)
{
	struct mtd_part *slave;
717
	uint64_t cur_offset = 0;
L
Linus Torvalds 已提交
718 719
	int i;

720
	printk(KERN_NOTICE "Creating %d MTD partitions on \"%s\":\n", nbparts, master->name);
L
Linus Torvalds 已提交
721 722

	for (i = 0; i < nbparts; i++) {
723
		slave = allocate_partition(master, parts + i, i, cur_offset);
724 725
		if (IS_ERR(slave)) {
			del_mtd_partitions(master);
726
			return PTR_ERR(slave);
727
		}
728 729 730 731 732 733

		mutex_lock(&mtd_partitions_mutex);
		list_add(&slave->list, &mtd_partitions);
		mutex_unlock(&mtd_partitions_mutex);

		add_mtd_device(&slave->mtd);
734
		mtd_add_partition_attrs(slave);
735

L
Linus Torvalds 已提交
736 737 738 739 740 741 742 743 744
		cur_offset = slave->offset + slave->mtd.size;
	}

	return 0;
}

static DEFINE_SPINLOCK(part_parser_lock);
static LIST_HEAD(part_parsers);

745
static struct mtd_part_parser *mtd_part_parser_get(const char *name)
L
Linus Torvalds 已提交
746
{
747
	struct mtd_part_parser *p, *ret = NULL;
L
Linus Torvalds 已提交
748

749
	spin_lock(&part_parser_lock);
L
Linus Torvalds 已提交
750

751
	list_for_each_entry(p, &part_parsers, list)
L
Linus Torvalds 已提交
752 753 754 755
		if (!strcmp(p->name, name) && try_module_get(p->owner)) {
			ret = p;
			break;
		}
756

L
Linus Torvalds 已提交
757 758 759 760 761
	spin_unlock(&part_parser_lock);

	return ret;
}

762 763 764 765
static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
{
	module_put(p->owner);
}
766

767 768 769 770 771 772 773 774 775 776
/*
 * Many partition parsers just expected the core to kfree() all their data in
 * one chunk. Do that by default.
 */
static void mtd_part_parser_cleanup_default(const struct mtd_partition *pparts,
					    int nr_parts)
{
	kfree(pparts);
}

777
int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
L
Linus Torvalds 已提交
778
{
779 780
	p->owner = owner;

781 782 783
	if (!p->cleanup)
		p->cleanup = &mtd_part_parser_cleanup_default;

L
Linus Torvalds 已提交
784 785 786
	spin_lock(&part_parser_lock);
	list_add(&p->list, &part_parsers);
	spin_unlock(&part_parser_lock);
787 788

	return 0;
L
Linus Torvalds 已提交
789
}
790
EXPORT_SYMBOL_GPL(__register_mtd_parser);
L
Linus Torvalds 已提交
791

792
void deregister_mtd_parser(struct mtd_part_parser *p)
L
Linus Torvalds 已提交
793 794 795 796 797
{
	spin_lock(&part_parser_lock);
	list_del(&p->list);
	spin_unlock(&part_parser_lock);
}
798
EXPORT_SYMBOL_GPL(deregister_mtd_parser);
L
Linus Torvalds 已提交
799

800 801 802 803
/*
 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
 * are changing this array!
 */
804
static const char * const default_mtd_part_types[] = {
805 806 807 808
	"cmdlinepart",
	"ofpart",
	NULL
};
809

810 811 812 813
/**
 * parse_mtd_partitions - parse MTD partitions
 * @master: the master partition (describes whole MTD device)
 * @types: names of partition parsers to try or %NULL
814
 * @pparts: info about partitions found is returned here
815
 * @data: MTD partition parser-specific data
816 817 818 819
 *
 * This function tries to find partition on MTD device @master. It uses MTD
 * partition parsers, specified in @types. However, if @types is %NULL, then
 * the default list of parsers is used. The default list contains only the
820
 * "cmdlinepart" and "ofpart" parsers ATM.
821 822
 * Note: If there are more then one parser in @types, the kernel only takes the
 * partitions parsed out by the first parser.
823 824 825
 *
 * This function may return:
 * o a negative error code in case of failure
826
 * o zero otherwise, and @pparts will describe the partitions, number of
827 828 829
 *   partitions, and the parser which parsed them. Caller must release
 *   resources with mtd_part_parser_cleanup() when finished with the returned
 *   data.
830
 */
831
int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
832
			 struct mtd_partitions *pparts,
833
			 struct mtd_part_parser_data *data)
L
Linus Torvalds 已提交
834 835
{
	struct mtd_part_parser *parser;
836
	int ret, err = 0;
837

838 839 840
	if (!types)
		types = default_mtd_part_types;

841
	for ( ; *types; types++) {
842
		pr_debug("%s: parsing partitions %s\n", master->name, *types);
843
		parser = mtd_part_parser_get(*types);
L
Linus Torvalds 已提交
844
		if (!parser && !request_module("%s", *types))
845
			parser = mtd_part_parser_get(*types);
846 847
		pr_debug("%s: got parser %s\n", master->name,
			 parser ? parser->name : NULL);
848
		if (!parser)
L
Linus Torvalds 已提交
849
			continue;
850
		ret = (*parser->parse_fn)(master, &pparts->parts, data);
851 852
		pr_debug("%s: parser %s: %i\n",
			 master->name, parser->name, ret);
L
Linus Torvalds 已提交
853
		if (ret > 0) {
854
			printk(KERN_NOTICE "%d %s partitions found on MTD device %s\n",
L
Linus Torvalds 已提交
855
			       ret, parser->name, master->name);
856 857 858
			pparts->nr_parts = ret;
			pparts->parser = parser;
			return 0;
L
Linus Torvalds 已提交
859
		}
860
		mtd_part_parser_put(parser);
861 862 863 864 865 866
		/*
		 * Stash the first error we see; only report it if no parser
		 * succeeds
		 */
		if (ret < 0 && !err)
			err = ret;
L
Linus Torvalds 已提交
867
	}
868
	return err;
L
Linus Torvalds 已提交
869
}
870

871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
void mtd_part_parser_cleanup(struct mtd_partitions *parts)
{
	const struct mtd_part_parser *parser;

	if (!parts)
		return;

	parser = parts->parser;
	if (parser) {
		if (parser->cleanup)
			parser->cleanup(parts->parts, parts->nr_parts);

		mtd_part_parser_put(parser);
	}
}

887
int mtd_is_partition(const struct mtd_info *mtd)
888 889
{
	struct mtd_part *part;
890
	int ispart = 0;
891 892 893 894

	mutex_lock(&mtd_partitions_mutex);
	list_for_each_entry(part, &mtd_partitions, list)
		if (&part->mtd == mtd) {
895
			ispart = 1;
896 897 898 899
			break;
		}
	mutex_unlock(&mtd_partitions_mutex);

900
	return ispart;
901
}
902
EXPORT_SYMBOL_GPL(mtd_is_partition);
903 904 905 906 907 908 909

/* Returns the size of the entire flash chip */
uint64_t mtd_get_device_size(const struct mtd_info *mtd)
{
	if (!mtd_is_partition(mtd))
		return mtd->size;

910
	return mtd_to_part(mtd)->master->size;
911 912
}
EXPORT_SYMBOL_GPL(mtd_get_device_size);