mtdpart.c 26.7 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 44 45 46
/**
 * struct mtd_part - our partition node structure
 *
 * @mtd: struct holding partition details
 * @parent: parent mtd - flash device or another partition
 * @offset: partition offset relative to the *flash device*
 */
L
Linus Torvalds 已提交
47 48
struct mtd_part {
	struct mtd_info mtd;
49
	struct mtd_info *parent;
50
	uint64_t offset;
L
Linus Torvalds 已提交
51 52 53 54 55
	struct list_head list;
};

/*
 * Given a pointer to the MTD object in the mtd_part structure, we can retrieve
56
 * the pointer to that structure.
L
Linus Torvalds 已提交
57
 */
58 59 60 61
static inline struct mtd_part *mtd_to_part(const struct mtd_info *mtd)
{
	return container_of(mtd, struct mtd_part, mtd);
}
L
Linus Torvalds 已提交
62

63 64

/*
L
Linus Torvalds 已提交
65 66 67 68
 * MTD methods which simply translate the effective address and pass through
 * to the _real_ device.
 */

69 70
static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
		size_t *retlen, u_char *buf)
L
Linus Torvalds 已提交
71
{
72
	struct mtd_part *part = mtd_to_part(mtd);
73
	struct mtd_ecc_stats stats;
74 75
	int res;

76 77
	stats = part->parent->ecc_stats;
	res = part->parent->_read(part->parent, from + part->offset, len,
M
Mike Dunn 已提交
78
				  retlen, buf);
79 80
	if (unlikely(mtd_is_eccerr(res)))
		mtd->ecc_stats.failed +=
81
			part->parent->ecc_stats.failed - stats.failed;
82 83
	else
		mtd->ecc_stats.corrected +=
84
			part->parent->ecc_stats.corrected - stats.corrected;
85
	return res;
L
Linus Torvalds 已提交
86 87
}

88 89
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 已提交
90
{
91
	struct mtd_part *part = mtd_to_part(mtd);
92

93
	return part->parent->_point(part->parent, from + part->offset, len,
M
Mike Dunn 已提交
94
				    retlen, virt, phys);
L
Linus Torvalds 已提交
95
}
96

97
static int part_unpoint(struct mtd_info *mtd, loff_t from, size_t len)
L
Linus Torvalds 已提交
98
{
99
	struct mtd_part *part = mtd_to_part(mtd);
L
Linus Torvalds 已提交
100

101
	return part->parent->_unpoint(part->parent, from + part->offset, len);
L
Linus Torvalds 已提交
102 103
}

104
static int part_read_oob(struct mtd_info *mtd, loff_t from,
105
		struct mtd_oob_ops *ops)
L
Linus Torvalds 已提交
106
{
107
	struct mtd_part *part = mtd_to_part(mtd);
108
	struct mtd_ecc_stats stats;
109
	int res;
110

L
Linus Torvalds 已提交
111
	if (from >= mtd->size)
112
		return -EINVAL;
113
	if (ops->datbuf && from + ops->len > mtd->size)
114
		return -EINVAL;
115

116 117 118 119 120 121 122
	/*
	 * 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;

123
		len = mtd_oobavail(mtd, ops);
124 125 126 127 128 129
		pages = mtd_div_by_ws(mtd->size, mtd);
		pages -= mtd_div_by_ws(from, mtd);
		if (ops->ooboffs + ops->ooblen > pages * len)
			return -EINVAL;
	}

130
	stats = part->parent->ecc_stats;
131
	res = part->parent->_read_oob(part->parent, from + part->offset, ops);
132 133 134 135 136 137
	if (unlikely(mtd_is_eccerr(res)))
		mtd->ecc_stats.failed +=
			part->parent->ecc_stats.failed - stats.failed;
	else
		mtd->ecc_stats.corrected +=
			part->parent->ecc_stats.corrected - stats.corrected;
138
	return res;
L
Linus Torvalds 已提交
139 140
}

141 142
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 已提交
143
{
144
	struct mtd_part *part = mtd_to_part(mtd);
145
	return part->parent->_read_user_prot_reg(part->parent, from, len,
M
Mike Dunn 已提交
146
						 retlen, buf);
L
Linus Torvalds 已提交
147 148
}

149 150
static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
				   size_t *retlen, struct otp_info *buf)
151
{
152
	struct mtd_part *part = mtd_to_part(mtd);
153
	return part->parent->_get_user_prot_info(part->parent, len, retlen,
154
						 buf);
155 156
}

157 158
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 已提交
159
{
160
	struct mtd_part *part = mtd_to_part(mtd);
161
	return part->parent->_read_fact_prot_reg(part->parent, from, len,
M
Mike Dunn 已提交
162
						 retlen, buf);
L
Linus Torvalds 已提交
163 164
}

165 166
static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
				   size_t *retlen, struct otp_info *buf)
167
{
168
	struct mtd_part *part = mtd_to_part(mtd);
169
	return part->parent->_get_fact_prot_info(part->parent, len, retlen,
170
						 buf);
171 172
}

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

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

189
static int part_write_oob(struct mtd_info *mtd, loff_t to,
190
		struct mtd_oob_ops *ops)
L
Linus Torvalds 已提交
191
{
192
	struct mtd_part *part = mtd_to_part(mtd);
193

L
Linus Torvalds 已提交
194
	if (to >= mtd->size)
195
		return -EINVAL;
196
	if (ops->datbuf && to + ops->len > mtd->size)
197
		return -EINVAL;
198
	return part->parent->_write_oob(part->parent, to + part->offset, ops);
L
Linus Torvalds 已提交
199 200
}

201 202
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 已提交
203
{
204
	struct mtd_part *part = mtd_to_part(mtd);
205
	return part->parent->_write_user_prot_reg(part->parent, from, len,
M
Mike Dunn 已提交
206
						  retlen, buf);
L
Linus Torvalds 已提交
207 208
}

209 210
static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
		size_t len)
211
{
212
	struct mtd_part *part = mtd_to_part(mtd);
213
	return part->parent->_lock_user_prot_reg(part->parent, from, len);
214 215
}

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

224
static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
L
Linus Torvalds 已提交
225
{
226
	struct mtd_part *part = mtd_to_part(mtd);
L
Linus Torvalds 已提交
227
	int ret;
228

L
Linus Torvalds 已提交
229
	instr->addr += part->offset;
230
	ret = part->parent->_erase(part->parent, instr);
231
	if (ret) {
232
		if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
233 234 235
			instr->fail_addr -= part->offset;
		instr->addr -= part->offset;
	}
L
Linus Torvalds 已提交
236 237 238 239 240
	return ret;
}

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

244
		if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
L
Linus Torvalds 已提交
245 246 247 248 249 250 251 252
			instr->fail_addr -= part->offset;
		instr->addr -= part->offset;
	}
	if (instr->callback)
		instr->callback(instr);
}
EXPORT_SYMBOL_GPL(mtd_erase_callback);

253
static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
L
Linus Torvalds 已提交
254
{
255
	struct mtd_part *part = mtd_to_part(mtd);
256
	return part->parent->_lock(part->parent, ofs + part->offset, len);
L
Linus Torvalds 已提交
257 258
}

259
static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
L
Linus Torvalds 已提交
260
{
261
	struct mtd_part *part = mtd_to_part(mtd);
262
	return part->parent->_unlock(part->parent, ofs + part->offset, len);
L
Linus Torvalds 已提交
263 264
}

265 266
static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
{
267
	struct mtd_part *part = mtd_to_part(mtd);
268
	return part->parent->_is_locked(part->parent, ofs + part->offset, len);
269 270
}

L
Linus Torvalds 已提交
271 272
static void part_sync(struct mtd_info *mtd)
{
273
	struct mtd_part *part = mtd_to_part(mtd);
274
	part->parent->_sync(part->parent);
L
Linus Torvalds 已提交
275 276 277 278
}

static int part_suspend(struct mtd_info *mtd)
{
279
	struct mtd_part *part = mtd_to_part(mtd);
280
	return part->parent->_suspend(part->parent);
L
Linus Torvalds 已提交
281 282 283 284
}

static void part_resume(struct mtd_info *mtd)
{
285
	struct mtd_part *part = mtd_to_part(mtd);
286
	part->parent->_resume(part->parent);
L
Linus Torvalds 已提交
287 288
}

289 290
static int part_block_isreserved(struct mtd_info *mtd, loff_t ofs)
{
291
	struct mtd_part *part = mtd_to_part(mtd);
292
	ofs += part->offset;
293
	return part->parent->_block_isreserved(part->parent, ofs);
294 295
}

296
static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
L
Linus Torvalds 已提交
297
{
298
	struct mtd_part *part = mtd_to_part(mtd);
L
Linus Torvalds 已提交
299
	ofs += part->offset;
300
	return part->parent->_block_isbad(part->parent, ofs);
L
Linus Torvalds 已提交
301 302
}

303
static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
L
Linus Torvalds 已提交
304
{
305
	struct mtd_part *part = mtd_to_part(mtd);
306 307
	int res;

L
Linus Torvalds 已提交
308
	ofs += part->offset;
309
	res = part->parent->_block_markbad(part->parent, ofs);
310 311 312
	if (!res)
		mtd->ecc_stats.badblocks++;
	return res;
L
Linus Torvalds 已提交
313 314
}

315 316 317
static int part_get_device(struct mtd_info *mtd)
{
	struct mtd_part *part = mtd_to_part(mtd);
318
	return part->parent->_get_device(part->parent);
319 320 321 322 323
}

static void part_put_device(struct mtd_info *mtd)
{
	struct mtd_part *part = mtd_to_part(mtd);
324
	part->parent->_put_device(part->parent);
325 326
}

327 328 329 330 331
static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
			      struct mtd_oob_region *oobregion)
{
	struct mtd_part *part = mtd_to_part(mtd);

332
	return mtd_ooblayout_ecc(part->parent, section, oobregion);
333 334 335 336 337 338 339
}

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

340
	return mtd_ooblayout_free(part->parent, section, oobregion);
341 342 343 344 345 346 347
}

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

348 349 350 351
static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
{
	struct mtd_part *part = mtd_to_part(mtd);

352
	return part->parent->_max_bad_blocks(part->parent,
353 354 355
					     ofs + part->offset, len);
}

356 357 358 359 360 361
static inline void free_partition(struct mtd_part *p)
{
	kfree(p->mtd.name);
	kfree(p);
}

362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
/**
 * mtd_parse_part - parse MTD partition looking for subpartitions
 *
 * @slave: part that is supposed to be a container and should be parsed
 * @types: NULL-terminated array with names of partition parsers to try
 *
 * Some partitions are kind of containers with extra subpartitions (volumes).
 * There can be various formats of such containers. This function tries to use
 * specified parsers to analyze given partition and registers found
 * subpartitions on success.
 */
static int mtd_parse_part(struct mtd_part *slave, const char *const *types)
{
	struct mtd_partitions parsed;
	int err;

	err = parse_mtd_partitions(&slave->mtd, types, &parsed, NULL);
	if (err)
		return err;
	else if (!parsed.nr_parts)
		return -ENOENT;

	err = add_mtd_partitions(&slave->mtd, parsed.parts, parsed.nr_parts);

	mtd_part_parser_cleanup(&parsed);

	return err;
}

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

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

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

424
	slave->mtd.name = name;
425
	slave->mtd.owner = parent->owner;
426

427 428 429 430 431 432 433
	/* 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 已提交
434
	 */
435
	slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
436 437
				&parent->dev :
				parent->dev.parent;
438
	slave->mtd.dev.of_node = part->of_node;
D
David Brownell 已提交
439

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

443
	if (parent->_panic_write)
444
		slave->mtd._panic_write = part_panic_write;
445

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

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

491
	if (parent->_get_device)
492
		slave->mtd._get_device = part_get_device;
493
	if (parent->_put_device)
494 495
		slave->mtd._put_device = part_put_device;

496
	slave->mtd._erase = part_erase;
497
	slave->parent = parent;
498 499 500 501 502
	slave->offset = part->offset;

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

529 530
	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);
531 532

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

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

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

572 573 574 575 576 577 578 579
	/*
	 * Slave erasesize might differ from the master one if the master
	 * exposes several regions with different erasesize. Adjust
	 * wr_alignment accordingly.
	 */
	if (!(slave->mtd.flags & MTD_NO_ERASE))
		wr_alignment = slave->mtd.erasesize;

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

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

599
	mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
600 601 602
	slave->mtd.ecc_step_size = parent->ecc_step_size;
	slave->mtd.ecc_strength = parent->ecc_strength;
	slave->mtd.bitflip_threshold = parent->bitflip_threshold;
603

604
	if (parent->_block_isbad) {
605
		uint64_t offs = 0;
606

607
		while (offs < slave->mtd.size) {
608
			if (mtd_block_isreserved(parent, offs + slave->offset))
609
				slave->mtd.ecc_stats.bbtblocks++;
610
			else if (mtd_block_isbad(parent, offs + slave->offset))
611 612 613 614 615
				slave->mtd.ecc_stats.badblocks++;
			offs += slave->mtd.erasesize;
		}
	}

616
out_register:
617 618 619
	return slave;
}

620 621 622 623
static ssize_t mtd_partition_offset_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct mtd_info *mtd = dev_get_drvdata(dev);
624
	struct mtd_part *part = mtd_to_part(mtd);
625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
	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;
}

644
int mtd_add_partition(struct mtd_info *parent, const char *name,
645 646 647
		      long long offset, long long length)
{
	struct mtd_partition part;
648
	struct mtd_part *new;
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)
657
		length = parent->size - offset;
658 659 660 661

	if (length <= 0)
		return -EINVAL;

662
	memset(&part, 0, sizeof(part));
663 664 665 666
	part.name = name;
	part.size = length;
	part.offset = offset;

667
	new = allocate_partition(parent, &part, -1, offset);
668 669 670 671 672 673 674 675 676
	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);

677 678
	mtd_add_partition_attrs(new);

679 680 681 682
	return ret;
}
EXPORT_SYMBOL_GPL(mtd_add_partition);

683 684 685 686 687 688 689 690 691
/**
 * __mtd_del_partition - delete MTD partition
 *
 * @priv: internal MTD struct for partition to be deleted
 *
 * This function must be called with the partitions mutex locked.
 */
static int __mtd_del_partition(struct mtd_part *priv)
{
692
	struct mtd_part *child, *next;
693 694
	int err;

695 696 697 698 699 700 701 702
	list_for_each_entry_safe(child, next, &mtd_partitions, list) {
		if (child->parent == &priv->mtd) {
			err = __mtd_del_partition(child);
			if (err)
				return err;
		}
	}

703 704
	sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);

705 706 707 708 709 710 711 712 713 714 715 716
	err = del_mtd_device(&priv->mtd);
	if (err)
		return err;

	list_del(&priv->list);
	free_partition(priv);

	return 0;
}

/*
 * This function unregisters and destroy all slave MTD objects which are
717
 * attached to the given MTD object.
718
 */
719
int del_mtd_partitions(struct mtd_info *mtd)
720 721 722 723 724 725
{
	struct mtd_part *slave, *next;
	int ret, err = 0;

	mutex_lock(&mtd_partitions_mutex);
	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
726
		if (slave->parent == mtd) {
727 728 729 730 731 732 733 734 735
			ret = __mtd_del_partition(slave);
			if (ret < 0)
				err = ret;
		}
	mutex_unlock(&mtd_partitions_mutex);

	return err;
}

736
int mtd_del_partition(struct mtd_info *mtd, int partno)
737 738 739 740 741 742
{
	struct mtd_part *slave, *next;
	int ret = -EINVAL;

	mutex_lock(&mtd_partitions_mutex);
	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
743
		if ((slave->parent == mtd) &&
744
		    (slave->mtd.index == partno)) {
745
			ret = __mtd_del_partition(slave);
746 747 748 749 750 751 752 753
			break;
		}
	mutex_unlock(&mtd_partitions_mutex);

	return ret;
}
EXPORT_SYMBOL_GPL(mtd_del_partition);

L
Linus Torvalds 已提交
754 755 756 757
/*
 * 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 已提交
758
 *
759 760
 * For historical reasons, this function's caller only registers the master
 * if the MTD_PARTITIONED_MASTER config option is set.
L
Linus Torvalds 已提交
761 762
 */

763
int add_mtd_partitions(struct mtd_info *master,
L
Linus Torvalds 已提交
764 765 766 767
		       const struct mtd_partition *parts,
		       int nbparts)
{
	struct mtd_part *slave;
768
	uint64_t cur_offset = 0;
L
Linus Torvalds 已提交
769 770
	int i;

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

	for (i = 0; i < nbparts; i++) {
774
		slave = allocate_partition(master, parts + i, i, cur_offset);
775 776
		if (IS_ERR(slave)) {
			del_mtd_partitions(master);
777
			return PTR_ERR(slave);
778
		}
779 780 781 782 783 784

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

		add_mtd_device(&slave->mtd);
785
		mtd_add_partition_attrs(slave);
786 787
		if (parts[i].types)
			mtd_parse_part(slave, parts[i].types);
788

L
Linus Torvalds 已提交
789 790 791 792 793 794 795 796 797
		cur_offset = slave->offset + slave->mtd.size;
	}

	return 0;
}

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

798
static struct mtd_part_parser *mtd_part_parser_get(const char *name)
L
Linus Torvalds 已提交
799
{
800
	struct mtd_part_parser *p, *ret = NULL;
L
Linus Torvalds 已提交
801

802
	spin_lock(&part_parser_lock);
L
Linus Torvalds 已提交
803

804
	list_for_each_entry(p, &part_parsers, list)
L
Linus Torvalds 已提交
805 806 807 808
		if (!strcmp(p->name, name) && try_module_get(p->owner)) {
			ret = p;
			break;
		}
809

L
Linus Torvalds 已提交
810 811 812 813 814
	spin_unlock(&part_parser_lock);

	return ret;
}

815 816 817 818
static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
{
	module_put(p->owner);
}
819

820 821 822 823 824 825 826 827 828 829
/*
 * 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);
}

830
int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
L
Linus Torvalds 已提交
831
{
832 833
	p->owner = owner;

834 835 836
	if (!p->cleanup)
		p->cleanup = &mtd_part_parser_cleanup_default;

L
Linus Torvalds 已提交
837 838 839
	spin_lock(&part_parser_lock);
	list_add(&p->list, &part_parsers);
	spin_unlock(&part_parser_lock);
840 841

	return 0;
L
Linus Torvalds 已提交
842
}
843
EXPORT_SYMBOL_GPL(__register_mtd_parser);
L
Linus Torvalds 已提交
844

845
void deregister_mtd_parser(struct mtd_part_parser *p)
L
Linus Torvalds 已提交
846 847 848 849 850
{
	spin_lock(&part_parser_lock);
	list_del(&p->list);
	spin_unlock(&part_parser_lock);
}
851
EXPORT_SYMBOL_GPL(deregister_mtd_parser);
L
Linus Torvalds 已提交
852

853 854 855 856
/*
 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
 * are changing this array!
 */
857
static const char * const default_mtd_part_types[] = {
858 859 860 861
	"cmdlinepart",
	"ofpart",
	NULL
};
862

863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883
static int mtd_part_do_parse(struct mtd_part_parser *parser,
			     struct mtd_info *master,
			     struct mtd_partitions *pparts,
			     struct mtd_part_parser_data *data)
{
	int ret;

	ret = (*parser->parse_fn)(master, &pparts->parts, data);
	pr_debug("%s: parser %s: %i\n", master->name, parser->name, ret);
	if (ret <= 0)
		return ret;

	pr_notice("%d %s partitions found on MTD device %s\n", ret,
		  parser->name, master->name);

	pparts->nr_parts = ret;
	pparts->parser = parser;

	return ret;
}

884 885 886 887
/**
 * parse_mtd_partitions - parse MTD partitions
 * @master: the master partition (describes whole MTD device)
 * @types: names of partition parsers to try or %NULL
888
 * @pparts: info about partitions found is returned here
889
 * @data: MTD partition parser-specific data
890 891 892 893
 *
 * 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
894
 * "cmdlinepart" and "ofpart" parsers ATM.
895 896
 * Note: If there are more then one parser in @types, the kernel only takes the
 * partitions parsed out by the first parser.
897 898 899
 *
 * This function may return:
 * o a negative error code in case of failure
900
 * o zero otherwise, and @pparts will describe the partitions, number of
901 902 903
 *   partitions, and the parser which parsed them. Caller must release
 *   resources with mtd_part_parser_cleanup() when finished with the returned
 *   data.
904
 */
905
int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
906
			 struct mtd_partitions *pparts,
907
			 struct mtd_part_parser_data *data)
L
Linus Torvalds 已提交
908 909
{
	struct mtd_part_parser *parser;
910
	int ret, err = 0;
911

912 913 914
	if (!types)
		types = default_mtd_part_types;

915
	for ( ; *types; types++) {
916
		pr_debug("%s: parsing partitions %s\n", master->name, *types);
917
		parser = mtd_part_parser_get(*types);
L
Linus Torvalds 已提交
918
		if (!parser && !request_module("%s", *types))
919
			parser = mtd_part_parser_get(*types);
920 921
		pr_debug("%s: got parser %s\n", master->name,
			 parser ? parser->name : NULL);
922
		if (!parser)
L
Linus Torvalds 已提交
923
			continue;
924 925 926
		ret = mtd_part_do_parse(parser, master, pparts, data);
		/* Found partitions! */
		if (ret > 0)
927
			return 0;
928
		mtd_part_parser_put(parser);
929 930 931 932 933 934
		/*
		 * Stash the first error we see; only report it if no parser
		 * succeeds
		 */
		if (ret < 0 && !err)
			err = ret;
L
Linus Torvalds 已提交
935
	}
936
	return err;
L
Linus Torvalds 已提交
937
}
938

939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954
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);
	}
}

955
int mtd_is_partition(const struct mtd_info *mtd)
956 957
{
	struct mtd_part *part;
958
	int ispart = 0;
959 960 961 962

	mutex_lock(&mtd_partitions_mutex);
	list_for_each_entry(part, &mtd_partitions, list)
		if (&part->mtd == mtd) {
963
			ispart = 1;
964 965 966 967
			break;
		}
	mutex_unlock(&mtd_partitions_mutex);

968
	return ispart;
969
}
970
EXPORT_SYMBOL_GPL(mtd_is_partition);
971 972 973 974 975 976 977

/* 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;

978
	return mtd_get_device_size(mtd_to_part(mtd)->parent);
979 980
}
EXPORT_SYMBOL_GPL(mtd_get_device_size);