mtdpart.c 26.1 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

111
	stats = part->parent->ecc_stats;
112
	res = part->parent->_read_oob(part->parent, from + part->offset, ops);
113 114 115 116 117 118
	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;
119
	return res;
L
Linus Torvalds 已提交
120 121
}

122 123
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 已提交
124
{
125
	struct mtd_part *part = mtd_to_part(mtd);
126
	return part->parent->_read_user_prot_reg(part->parent, from, len,
M
Mike Dunn 已提交
127
						 retlen, buf);
L
Linus Torvalds 已提交
128 129
}

130 131
static int part_get_user_prot_info(struct mtd_info *mtd, size_t len,
				   size_t *retlen, struct otp_info *buf)
132
{
133
	struct mtd_part *part = mtd_to_part(mtd);
134
	return part->parent->_get_user_prot_info(part->parent, len, retlen,
135
						 buf);
136 137
}

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

146 147
static int part_get_fact_prot_info(struct mtd_info *mtd, size_t len,
				   size_t *retlen, struct otp_info *buf)
148
{
149
	struct mtd_part *part = mtd_to_part(mtd);
150
	return part->parent->_get_fact_prot_info(part->parent, len, retlen,
151
						 buf);
152 153
}

154 155
static int part_write(struct mtd_info *mtd, loff_t to, size_t len,
		size_t *retlen, const u_char *buf)
L
Linus Torvalds 已提交
156
{
157
	struct mtd_part *part = mtd_to_part(mtd);
158
	return part->parent->_write(part->parent, to + part->offset, len,
M
Mike Dunn 已提交
159
				    retlen, buf);
L
Linus Torvalds 已提交
160 161
}

162 163
static int part_panic_write(struct mtd_info *mtd, loff_t to, size_t len,
		size_t *retlen, const u_char *buf)
164
{
165
	struct mtd_part *part = mtd_to_part(mtd);
166
	return part->parent->_panic_write(part->parent, to + part->offset, len,
M
Mike Dunn 已提交
167
					  retlen, buf);
168 169
}

170
static int part_write_oob(struct mtd_info *mtd, loff_t to,
171
		struct mtd_oob_ops *ops)
L
Linus Torvalds 已提交
172
{
173
	struct mtd_part *part = mtd_to_part(mtd);
174

175
	return part->parent->_write_oob(part->parent, to + part->offset, ops);
L
Linus Torvalds 已提交
176 177
}

178 179
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 已提交
180
{
181
	struct mtd_part *part = mtd_to_part(mtd);
182
	return part->parent->_write_user_prot_reg(part->parent, from, len,
M
Mike Dunn 已提交
183
						  retlen, buf);
L
Linus Torvalds 已提交
184 185
}

186 187
static int part_lock_user_prot_reg(struct mtd_info *mtd, loff_t from,
		size_t len)
188
{
189
	struct mtd_part *part = mtd_to_part(mtd);
190
	return part->parent->_lock_user_prot_reg(part->parent, from, len);
191 192
}

193 194
static int part_writev(struct mtd_info *mtd, const struct kvec *vecs,
		unsigned long count, loff_t to, size_t *retlen)
L
Linus Torvalds 已提交
195
{
196
	struct mtd_part *part = mtd_to_part(mtd);
197
	return part->parent->_writev(part->parent, vecs, count,
M
Mike Dunn 已提交
198
				     to + part->offset, retlen);
L
Linus Torvalds 已提交
199 200
}

201
static int part_erase(struct mtd_info *mtd, struct erase_info *instr)
L
Linus Torvalds 已提交
202
{
203
	struct mtd_part *part = mtd_to_part(mtd);
L
Linus Torvalds 已提交
204
	int ret;
205

L
Linus Torvalds 已提交
206
	instr->addr += part->offset;
207
	ret = part->parent->_erase(part->parent, instr);
208
	if (ret) {
209
		if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
210 211 212
			instr->fail_addr -= part->offset;
		instr->addr -= part->offset;
	}
L
Linus Torvalds 已提交
213 214 215 216 217
	return ret;
}

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

221
		if (instr->fail_addr != MTD_FAIL_ADDR_UNKNOWN)
L
Linus Torvalds 已提交
222 223 224 225 226 227
			instr->fail_addr -= part->offset;
		instr->addr -= part->offset;
	}
}
EXPORT_SYMBOL_GPL(mtd_erase_callback);

228
static int part_lock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
L
Linus Torvalds 已提交
229
{
230
	struct mtd_part *part = mtd_to_part(mtd);
231
	return part->parent->_lock(part->parent, ofs + part->offset, len);
L
Linus Torvalds 已提交
232 233
}

234
static int part_unlock(struct mtd_info *mtd, loff_t ofs, uint64_t len)
L
Linus Torvalds 已提交
235
{
236
	struct mtd_part *part = mtd_to_part(mtd);
237
	return part->parent->_unlock(part->parent, ofs + part->offset, len);
L
Linus Torvalds 已提交
238 239
}

240 241
static int part_is_locked(struct mtd_info *mtd, loff_t ofs, uint64_t len)
{
242
	struct mtd_part *part = mtd_to_part(mtd);
243
	return part->parent->_is_locked(part->parent, ofs + part->offset, len);
244 245
}

L
Linus Torvalds 已提交
246 247
static void part_sync(struct mtd_info *mtd)
{
248
	struct mtd_part *part = mtd_to_part(mtd);
249
	part->parent->_sync(part->parent);
L
Linus Torvalds 已提交
250 251 252 253
}

static int part_suspend(struct mtd_info *mtd)
{
254
	struct mtd_part *part = mtd_to_part(mtd);
255
	return part->parent->_suspend(part->parent);
L
Linus Torvalds 已提交
256 257 258 259
}

static void part_resume(struct mtd_info *mtd)
{
260
	struct mtd_part *part = mtd_to_part(mtd);
261
	part->parent->_resume(part->parent);
L
Linus Torvalds 已提交
262 263
}

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

271
static int part_block_isbad(struct mtd_info *mtd, loff_t ofs)
L
Linus Torvalds 已提交
272
{
273
	struct mtd_part *part = mtd_to_part(mtd);
L
Linus Torvalds 已提交
274
	ofs += part->offset;
275
	return part->parent->_block_isbad(part->parent, ofs);
L
Linus Torvalds 已提交
276 277
}

278
static int part_block_markbad(struct mtd_info *mtd, loff_t ofs)
L
Linus Torvalds 已提交
279
{
280
	struct mtd_part *part = mtd_to_part(mtd);
281 282
	int res;

L
Linus Torvalds 已提交
283
	ofs += part->offset;
284
	res = part->parent->_block_markbad(part->parent, ofs);
285 286 287
	if (!res)
		mtd->ecc_stats.badblocks++;
	return res;
L
Linus Torvalds 已提交
288 289
}

290 291 292
static int part_get_device(struct mtd_info *mtd)
{
	struct mtd_part *part = mtd_to_part(mtd);
293
	return part->parent->_get_device(part->parent);
294 295 296 297 298
}

static void part_put_device(struct mtd_info *mtd)
{
	struct mtd_part *part = mtd_to_part(mtd);
299
	part->parent->_put_device(part->parent);
300 301
}

302 303 304 305 306
static int part_ooblayout_ecc(struct mtd_info *mtd, int section,
			      struct mtd_oob_region *oobregion)
{
	struct mtd_part *part = mtd_to_part(mtd);

307
	return mtd_ooblayout_ecc(part->parent, section, oobregion);
308 309 310 311 312 313 314
}

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

315
	return mtd_ooblayout_free(part->parent, section, oobregion);
316 317 318 319 320 321 322
}

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

323 324 325 326
static int part_max_bad_blocks(struct mtd_info *mtd, loff_t ofs, size_t len)
{
	struct mtd_part *part = mtd_to_part(mtd);

327
	return part->parent->_max_bad_blocks(part->parent,
328 329 330
					     ofs + part->offset, len);
}

331 332 333 334 335 336
static inline void free_partition(struct mtd_part *p)
{
	kfree(p->mtd.name);
	kfree(p);
}

337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365
/**
 * 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;
}

366
static struct mtd_part *allocate_partition(struct mtd_info *parent,
367 368
			const struct mtd_partition *part, int partno,
			uint64_t cur_offset)
369
{
370
	int wr_alignment = (parent->flags & MTD_NO_ERASE) ? parent->writesize :
371
							    parent->erasesize;
372
	struct mtd_part *slave;
373
	u32 remainder;
374
	char *name;
375
	u64 tmp;
376 377

	/* allocate the partition structure */
378
	slave = kzalloc(sizeof(*slave), GFP_KERNEL);
379 380
	name = kstrdup(part->name, GFP_KERNEL);
	if (!name || !slave) {
381
		printk(KERN_ERR"memory allocation error while creating partitions for \"%s\"\n",
382
		       parent->name);
383 384 385
		kfree(name);
		kfree(slave);
		return ERR_PTR(-ENOMEM);
386 387 388
	}

	/* set up the MTD object for this partition */
389 390
	slave->mtd.type = parent->type;
	slave->mtd.flags = parent->flags & ~part->mask_flags;
391
	slave->mtd.size = part->size;
392 393 394 395 396 397
	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;
398

399
	slave->mtd.name = name;
400
	slave->mtd.owner = parent->owner;
401

402 403 404 405 406 407 408
	/* 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 已提交
409
	 */
410
	slave->mtd.dev.parent = IS_ENABLED(CONFIG_MTD_PARTITIONED_MASTER) || mtd_is_partition(parent) ?
411 412
				&parent->dev :
				parent->dev.parent;
413
	slave->mtd.dev.of_node = part->of_node;
D
David Brownell 已提交
414

415 416 417 418
	if (parent->_read)
		slave->mtd._read = part_read;
	if (parent->_write)
		slave->mtd._write = part_write;
419

420
	if (parent->_panic_write)
421
		slave->mtd._panic_write = part_panic_write;
422

423
	if (parent->_point && parent->_unpoint) {
424 425
		slave->mtd._point = part_point;
		slave->mtd._unpoint = part_unpoint;
426 427
	}

428
	if (parent->_read_oob)
429
		slave->mtd._read_oob = part_read_oob;
430
	if (parent->_write_oob)
431
		slave->mtd._write_oob = part_write_oob;
432
	if (parent->_read_user_prot_reg)
433
		slave->mtd._read_user_prot_reg = part_read_user_prot_reg;
434
	if (parent->_read_fact_prot_reg)
435
		slave->mtd._read_fact_prot_reg = part_read_fact_prot_reg;
436
	if (parent->_write_user_prot_reg)
437
		slave->mtd._write_user_prot_reg = part_write_user_prot_reg;
438
	if (parent->_lock_user_prot_reg)
439
		slave->mtd._lock_user_prot_reg = part_lock_user_prot_reg;
440
	if (parent->_get_user_prot_info)
441
		slave->mtd._get_user_prot_info = part_get_user_prot_info;
442
	if (parent->_get_fact_prot_info)
443
		slave->mtd._get_fact_prot_info = part_get_fact_prot_info;
444
	if (parent->_sync)
445
		slave->mtd._sync = part_sync;
446 447
	if (!partno && !parent->dev.class && parent->_suspend &&
	    parent->_resume) {
448 449
		slave->mtd._suspend = part_suspend;
		slave->mtd._resume = part_resume;
450
	}
451
	if (parent->_writev)
452
		slave->mtd._writev = part_writev;
453
	if (parent->_lock)
454
		slave->mtd._lock = part_lock;
455
	if (parent->_unlock)
456
		slave->mtd._unlock = part_unlock;
457
	if (parent->_is_locked)
458
		slave->mtd._is_locked = part_is_locked;
459
	if (parent->_block_isreserved)
460
		slave->mtd._block_isreserved = part_block_isreserved;
461
	if (parent->_block_isbad)
462
		slave->mtd._block_isbad = part_block_isbad;
463
	if (parent->_block_markbad)
464
		slave->mtd._block_markbad = part_block_markbad;
465
	if (parent->_max_bad_blocks)
466
		slave->mtd._max_bad_blocks = part_max_bad_blocks;
467

468
	if (parent->_get_device)
469
		slave->mtd._get_device = part_get_device;
470
	if (parent->_put_device)
471 472
		slave->mtd._put_device = part_put_device;

473
	slave->mtd._erase = part_erase;
474
	slave->parent = parent;
475 476 477 478 479
	slave->offset = part->offset;

	if (slave->offset == MTDPART_OFS_APPEND)
		slave->offset = cur_offset;
	if (slave->offset == MTDPART_OFS_NXTBLK) {
480
		tmp = cur_offset;
481
		slave->offset = cur_offset;
482 483 484
		remainder = do_div(tmp, wr_alignment);
		if (remainder) {
			slave->offset += wr_alignment - remainder;
485
			printk(KERN_NOTICE "Moving partition %d: "
486 487
			       "0x%012llx -> 0x%012llx\n", partno,
			       (unsigned long long)cur_offset, (unsigned long long)slave->offset);
488 489
		}
	}
490 491
	if (slave->offset == MTDPART_OFS_RETAIN) {
		slave->offset = cur_offset;
492 493
		if (parent->size - slave->offset >= slave->mtd.size) {
			slave->mtd.size = parent->size - slave->offset
494 495 496
							- slave->mtd.size;
		} else {
			printk(KERN_ERR "mtd partition \"%s\" doesn't have enough space: %#llx < %#llx, disabled\n",
497
				part->name, parent->size - slave->offset,
498 499 500 501 502
				slave->mtd.size);
			/* register to preserve ordering */
			goto out_register;
		}
	}
503
	if (slave->mtd.size == MTDPART_SIZ_FULL)
504
		slave->mtd.size = parent->size - slave->offset;
505

506 507
	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);
508 509

	/* let's do some sanity checks */
510
	if (slave->offset >= parent->size) {
511
		/* let's register it anyway to preserve ordering */
512 513
		slave->offset = 0;
		slave->mtd.size = 0;
514
		printk(KERN_ERR"mtd: partition \"%s\" is out of reach -- disabled\n",
515
			part->name);
516
		goto out_register;
517
	}
518 519
	if (slave->offset + slave->mtd.size > parent->size) {
		slave->mtd.size = parent->size - slave->offset;
520
		printk(KERN_WARNING"mtd: partition \"%s\" extends beyond the end of device \"%s\" -- size truncated to %#llx\n",
521
			part->name, parent->name, (unsigned long long)slave->mtd.size);
522
	}
523
	if (parent->numeraseregions > 1) {
524
		/* Deal with variable erase size stuff */
525
		int i, max = parent->numeraseregions;
526
		u64 end = slave->offset + slave->mtd.size;
527
		struct mtd_erase_region_info *regions = parent->eraseregions;
528

529 530 531
		/* Find the first erase regions which is part of this
		 * partition. */
		for (i = 0; i < max && regions[i].offset <= slave->offset; i++)
532
			;
533
		/* The loop searched for the region _behind_ the first one */
534 535
		if (i > 0)
			i--;
536

537 538
		/* Pick biggest erasesize */
		for (; i < max && regions[i].offset < end; i++) {
539 540 541 542
			if (slave->mtd.erasesize < regions[i].erasesize) {
				slave->mtd.erasesize = regions[i].erasesize;
			}
		}
543
		BUG_ON(slave->mtd.erasesize == 0);
544 545
	} else {
		/* Single erase size */
546
		slave->mtd.erasesize = parent->erasesize;
547 548
	}

549 550 551 552 553 554 555 556
	/*
	 * 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;

557 558 559
	tmp = slave->offset;
	remainder = do_div(tmp, wr_alignment);
	if ((slave->mtd.flags & MTD_WRITEABLE) && remainder) {
560
		/* Doesn't start on a boundary of major erase size */
561 562
		/* FIXME: Let it be writable if it is on a boundary of
		 * _minor_ erase size though */
563
		slave->mtd.flags &= ~MTD_WRITEABLE;
564
		printk(KERN_WARNING"mtd: partition \"%s\" doesn't start on an erase/write block boundary -- force read-only\n",
565 566
			part->name);
	}
567 568 569 570

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

576
	mtd_set_ooblayout(&slave->mtd, &part_ooblayout_ops);
577 578 579
	slave->mtd.ecc_step_size = parent->ecc_step_size;
	slave->mtd.ecc_strength = parent->ecc_strength;
	slave->mtd.bitflip_threshold = parent->bitflip_threshold;
580

581
	if (parent->_block_isbad) {
582
		uint64_t offs = 0;
583

584
		while (offs < slave->mtd.size) {
585
			if (mtd_block_isreserved(parent, offs + slave->offset))
586
				slave->mtd.ecc_stats.bbtblocks++;
587
			else if (mtd_block_isbad(parent, offs + slave->offset))
588 589 590 591 592
				slave->mtd.ecc_stats.badblocks++;
			offs += slave->mtd.erasesize;
		}
	}

593
out_register:
594 595 596
	return slave;
}

597 598 599 600
static ssize_t mtd_partition_offset_show(struct device *dev,
		struct device_attribute *attr, char *buf)
{
	struct mtd_info *mtd = dev_get_drvdata(dev);
601
	struct mtd_part *part = mtd_to_part(mtd);
602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620
	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;
}

621
int mtd_add_partition(struct mtd_info *parent, const char *name,
622 623 624
		      long long offset, long long length)
{
	struct mtd_partition part;
625
	struct mtd_part *new;
626 627 628 629 630 631 632 633
	int ret = 0;

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

	if (length == MTDPART_SIZ_FULL)
634
		length = parent->size - offset;
635 636 637 638

	if (length <= 0)
		return -EINVAL;

639
	memset(&part, 0, sizeof(part));
640 641 642 643
	part.name = name;
	part.size = length;
	part.offset = offset;

644
	new = allocate_partition(parent, &part, -1, offset);
645 646 647 648 649 650 651 652 653
	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);

654 655
	mtd_add_partition_attrs(new);

656 657 658 659
	return ret;
}
EXPORT_SYMBOL_GPL(mtd_add_partition);

660 661 662 663 664 665 666 667 668
/**
 * __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)
{
669
	struct mtd_part *child, *next;
670 671
	int err;

672 673 674 675 676 677 678 679
	list_for_each_entry_safe(child, next, &mtd_partitions, list) {
		if (child->parent == &priv->mtd) {
			err = __mtd_del_partition(child);
			if (err)
				return err;
		}
	}

680 681
	sysfs_remove_files(&priv->mtd.dev.kobj, mtd_partition_attrs);

682 683 684 685 686 687 688 689 690 691 692 693
	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
694
 * attached to the given MTD object.
695
 */
696
int del_mtd_partitions(struct mtd_info *mtd)
697 698 699 700 701 702
{
	struct mtd_part *slave, *next;
	int ret, err = 0;

	mutex_lock(&mtd_partitions_mutex);
	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
703
		if (slave->parent == mtd) {
704 705 706 707 708 709 710 711 712
			ret = __mtd_del_partition(slave);
			if (ret < 0)
				err = ret;
		}
	mutex_unlock(&mtd_partitions_mutex);

	return err;
}

713
int mtd_del_partition(struct mtd_info *mtd, int partno)
714 715 716 717 718 719
{
	struct mtd_part *slave, *next;
	int ret = -EINVAL;

	mutex_lock(&mtd_partitions_mutex);
	list_for_each_entry_safe(slave, next, &mtd_partitions, list)
720
		if ((slave->parent == mtd) &&
721
		    (slave->mtd.index == partno)) {
722
			ret = __mtd_del_partition(slave);
723 724 725 726 727 728 729 730
			break;
		}
	mutex_unlock(&mtd_partitions_mutex);

	return ret;
}
EXPORT_SYMBOL_GPL(mtd_del_partition);

L
Linus Torvalds 已提交
731 732 733 734
/*
 * 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 已提交
735
 *
736 737
 * For historical reasons, this function's caller only registers the master
 * if the MTD_PARTITIONED_MASTER config option is set.
L
Linus Torvalds 已提交
738 739
 */

740
int add_mtd_partitions(struct mtd_info *master,
L
Linus Torvalds 已提交
741 742 743 744
		       const struct mtd_partition *parts,
		       int nbparts)
{
	struct mtd_part *slave;
745
	uint64_t cur_offset = 0;
L
Linus Torvalds 已提交
746 747
	int i;

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

	for (i = 0; i < nbparts; i++) {
751
		slave = allocate_partition(master, parts + i, i, cur_offset);
752 753
		if (IS_ERR(slave)) {
			del_mtd_partitions(master);
754
			return PTR_ERR(slave);
755
		}
756 757 758 759 760 761

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

		add_mtd_device(&slave->mtd);
762
		mtd_add_partition_attrs(slave);
763 764
		if (parts[i].types)
			mtd_parse_part(slave, parts[i].types);
765

L
Linus Torvalds 已提交
766 767 768 769 770 771 772 773 774
		cur_offset = slave->offset + slave->mtd.size;
	}

	return 0;
}

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

775
static struct mtd_part_parser *mtd_part_parser_get(const char *name)
L
Linus Torvalds 已提交
776
{
777
	struct mtd_part_parser *p, *ret = NULL;
L
Linus Torvalds 已提交
778

779
	spin_lock(&part_parser_lock);
L
Linus Torvalds 已提交
780

781
	list_for_each_entry(p, &part_parsers, list)
L
Linus Torvalds 已提交
782 783 784 785
		if (!strcmp(p->name, name) && try_module_get(p->owner)) {
			ret = p;
			break;
		}
786

L
Linus Torvalds 已提交
787 788 789 790 791
	spin_unlock(&part_parser_lock);

	return ret;
}

792 793 794 795
static inline void mtd_part_parser_put(const struct mtd_part_parser *p)
{
	module_put(p->owner);
}
796

797 798 799 800 801 802 803 804 805 806
/*
 * 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);
}

807
int __register_mtd_parser(struct mtd_part_parser *p, struct module *owner)
L
Linus Torvalds 已提交
808
{
809 810
	p->owner = owner;

811 812 813
	if (!p->cleanup)
		p->cleanup = &mtd_part_parser_cleanup_default;

L
Linus Torvalds 已提交
814 815 816
	spin_lock(&part_parser_lock);
	list_add(&p->list, &part_parsers);
	spin_unlock(&part_parser_lock);
817 818

	return 0;
L
Linus Torvalds 已提交
819
}
820
EXPORT_SYMBOL_GPL(__register_mtd_parser);
L
Linus Torvalds 已提交
821

822
void deregister_mtd_parser(struct mtd_part_parser *p)
L
Linus Torvalds 已提交
823 824 825 826 827
{
	spin_lock(&part_parser_lock);
	list_del(&p->list);
	spin_unlock(&part_parser_lock);
}
828
EXPORT_SYMBOL_GPL(deregister_mtd_parser);
L
Linus Torvalds 已提交
829

830 831 832 833
/*
 * Do not forget to update 'parse_mtd_partitions()' kerneldoc comment if you
 * are changing this array!
 */
834
static const char * const default_mtd_part_types[] = {
835 836 837 838
	"cmdlinepart",
	"ofpart",
	NULL
};
839

840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860
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;
}

861 862 863 864
/**
 * parse_mtd_partitions - parse MTD partitions
 * @master: the master partition (describes whole MTD device)
 * @types: names of partition parsers to try or %NULL
865
 * @pparts: info about partitions found is returned here
866
 * @data: MTD partition parser-specific data
867 868 869 870
 *
 * 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
871
 * "cmdlinepart" and "ofpart" parsers ATM.
872 873
 * Note: If there are more then one parser in @types, the kernel only takes the
 * partitions parsed out by the first parser.
874 875 876
 *
 * This function may return:
 * o a negative error code in case of failure
877
 * o zero otherwise, and @pparts will describe the partitions, number of
878 879 880
 *   partitions, and the parser which parsed them. Caller must release
 *   resources with mtd_part_parser_cleanup() when finished with the returned
 *   data.
881
 */
882
int parse_mtd_partitions(struct mtd_info *master, const char *const *types,
883
			 struct mtd_partitions *pparts,
884
			 struct mtd_part_parser_data *data)
L
Linus Torvalds 已提交
885 886
{
	struct mtd_part_parser *parser;
887
	int ret, err = 0;
888

889 890 891
	if (!types)
		types = default_mtd_part_types;

892
	for ( ; *types; types++) {
893
		pr_debug("%s: parsing partitions %s\n", master->name, *types);
894
		parser = mtd_part_parser_get(*types);
L
Linus Torvalds 已提交
895
		if (!parser && !request_module("%s", *types))
896
			parser = mtd_part_parser_get(*types);
897 898
		pr_debug("%s: got parser %s\n", master->name,
			 parser ? parser->name : NULL);
899
		if (!parser)
L
Linus Torvalds 已提交
900
			continue;
901 902 903
		ret = mtd_part_do_parse(parser, master, pparts, data);
		/* Found partitions! */
		if (ret > 0)
904
			return 0;
905
		mtd_part_parser_put(parser);
906 907 908 909 910 911
		/*
		 * Stash the first error we see; only report it if no parser
		 * succeeds
		 */
		if (ret < 0 && !err)
			err = ret;
L
Linus Torvalds 已提交
912
	}
913
	return err;
L
Linus Torvalds 已提交
914
}
915

916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931
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);
	}
}

932
int mtd_is_partition(const struct mtd_info *mtd)
933 934
{
	struct mtd_part *part;
935
	int ispart = 0;
936 937 938 939

	mutex_lock(&mtd_partitions_mutex);
	list_for_each_entry(part, &mtd_partitions, list)
		if (&part->mtd == mtd) {
940
			ispart = 1;
941 942 943 944
			break;
		}
	mutex_unlock(&mtd_partitions_mutex);

945
	return ispart;
946
}
947
EXPORT_SYMBOL_GPL(mtd_is_partition);
948 949 950 951 952 953 954

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

955
	return mtd_get_device_size(mtd_to_part(mtd)->parent);
956 957
}
EXPORT_SYMBOL_GPL(mtd_get_device_size);