summaryrefslogtreecommitdiffstats
path: root/drivers/mtd/partition.c
blob: 013697732db3abda654ebcfc4d975fd7de65cdb8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#include <common.h>
#include <errno.h>
#include <malloc.h>
#include <linux/err.h>
#include <linux/mtd/mtd.h>

static int mtd_part_read(struct mtd_info *mtd, loff_t from, size_t len,
                size_t *retlen, u_char *buf)
{
	int res;

	if (from >= mtd->size)
		len = 0;
	else if (from + len > mtd->size)
		len = mtd->size - from;
	res = mtd->master->read(mtd->master, from + mtd->master_offset,
				len, retlen, buf);
	return res;
}

static int mtd_part_read_oob(struct mtd_info *mtd, loff_t from,
		struct mtd_oob_ops *ops)
{
	int res;

	if (from >= mtd->size)
		return -EINVAL;
	if (ops->datbuf && from + ops->len > mtd->size)
		return -EINVAL;

	res = mtd->master->read_oob(mtd->master, from + mtd->master_offset, ops);
	if (unlikely(res)) {
		if (mtd_is_bitflip(res))
			mtd->ecc_stats.corrected++;
		if (mtd_is_eccerr(res))
			mtd->ecc_stats.failed++;
	}
	return res;
}

static int mtd_part_write(struct mtd_info *mtd, loff_t to, size_t len,
                size_t *retlen, const u_char *buf)
{
	if (!(mtd->flags & MTD_WRITEABLE))
		return -EROFS;
	if (to >= mtd->size)
		len = 0;
	else if (to + len > mtd->size)
		len = mtd->size - to;
	return mtd->master->write(mtd->master, to + mtd->master_offset,
					len, retlen, buf);
}

static int mtd_part_write_oob(struct mtd_info *mtd, loff_t to,
		struct mtd_oob_ops *ops)
{
	if (to >= mtd->size)
		return -EINVAL;
	if (ops->datbuf && to + ops->len > mtd->size)
		return -EINVAL;
	return mtd->master->write_oob(mtd->master, to + mtd->master_offset, ops);
}

static int mtd_part_erase(struct mtd_info *mtd, struct erase_info *instr)
{
	int ret;

	if (!(mtd->flags & MTD_WRITEABLE))
		return -EROFS;
	if (instr->addr >= mtd->size)
		return -EINVAL;
	instr->addr += mtd->master_offset;
	ret = mtd->master->erase(mtd->master, instr);
	if (ret) {
		if (instr->fail_addr != 0xffffffff)
			instr->fail_addr -= mtd->master_offset;
		instr->addr -= mtd->master_offset;
	}
	return ret;
}

static int mtd_part_lock(struct mtd_info *mtd, loff_t offset, size_t len)
{
	if (!mtd->master->lock)
		return -ENOSYS;

	if (!(mtd->flags & MTD_WRITEABLE))
		return -EROFS;

	if (offset + len > mtd->size)
		return -EINVAL;

	offset += mtd->master_offset;

	return mtd->master->lock(mtd->master, offset, len);
}

static int mtd_part_unlock(struct mtd_info *mtd, loff_t offset, size_t len)
{
	if (!mtd->master->unlock)
		return -ENOSYS;

	if (!(mtd->flags & MTD_WRITEABLE))
		return -EROFS;

	if (offset + len > mtd->size)
		return -EINVAL;

	offset += mtd->master_offset;

	return mtd->master->unlock(mtd->master, offset, len);
}

static int mtd_part_block_isbad(struct mtd_info *mtd, loff_t ofs)
{
	if (ofs >= mtd->size)
		return -EINVAL;
	ofs += mtd->master_offset;
	return mtd_block_isbad(mtd->master, ofs);
}

static int mtd_part_block_markbad(struct mtd_info *mtd, loff_t ofs)
{
	int res;

	if (!(mtd->flags & MTD_WRITEABLE))
		return -EROFS;
	if (ofs >= mtd->size)
		return -EINVAL;
	ofs += mtd->master_offset;
	res = mtd->master->block_markbad(mtd->master, ofs);
	if (!res)
		mtd->ecc_stats.badblocks++;
	return res;
}

static int mtd_part_block_markgood(struct mtd_info *mtd, loff_t ofs)
{
	int res;

	if (!(mtd->flags & MTD_WRITEABLE))
		return -EROFS;
	if (ofs >= mtd->size)
		return -EINVAL;
	ofs += mtd->master_offset;
	res = mtd->master->block_markgood(mtd->master, ofs);
	if (!res)
		mtd->ecc_stats.badblocks--;
	return res;
}

struct mtd_info *mtd_add_partition(struct mtd_info *mtd, off_t offset,
		uint64_t size, unsigned long flags, const char *name)
{
	struct mtd_info *part;
	int ret;

	part = xzalloc(sizeof(*part));

	part->type = mtd->type;
	part->flags = mtd->flags;
	part->parent = &mtd->class_dev;
	part->writesize = mtd->writesize;
	part->writebufsize = mtd->writebufsize;
	part->oobsize = mtd->oobsize;
	part->oobavail = mtd->oobavail;
	part->bitflip_threshold = mtd->bitflip_threshold;
	part->ecclayout = mtd->ecclayout;
	part->ecc_strength = mtd->ecc_strength;
	part->subpage_sft = mtd->subpage_sft;
	part->cdev.flags = flags;

	if (mtd->numeraseregions > 1) {
		/* Deal with variable erase size stuff */
		int i, max = mtd->numeraseregions;
		u64 end = offset + size;
		struct mtd_erase_region_info *regions = mtd->eraseregions;

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

		/* Pick biggest erasesize */
		for (; i < max && regions[i].offset < end; i++) {
			if (part->erasesize < regions[i].erasesize) {
				part->erasesize = regions[i].erasesize;
			}
		}
		BUG_ON(part->erasesize == 0);
	} else {
		/* Single erase size */
		part->erasesize = mtd->erasesize;
	}

	part->read = mtd_part_read;
	if (IS_ENABLED(CONFIG_MTD_WRITE)) {
		part->write = mtd_part_write;
		part->erase = mtd_part_erase;
		part->lock = mtd_part_lock;
		part->unlock = mtd_part_unlock;
		part->block_markbad = mtd->block_markbad ? mtd_part_block_markbad : NULL;
		part->block_markgood = mtd->block_markgood ? mtd_part_block_markgood : NULL;
	}

	if (mtd->write_oob)
		part->write_oob = mtd_part_write_oob;
	if (mtd->read_oob)
		part->read_oob = mtd_part_read_oob;

	part->block_isbad = mtd->block_isbad ? mtd_part_block_isbad : NULL;
	part->size = size;
	part->name = xstrdup(name);

	part->master_offset = offset;
	part->master = mtd;

	if (!strncmp(mtd->cdev.name, name, strlen(mtd->cdev.name)))
		part->cdev.partname = xstrdup(name + strlen(mtd->cdev.name) + 1);

	ret = add_mtd_device(part, part->name, DEVICE_ID_SINGLE);
	if (ret)
		goto err;

	part->cdev.master = &part->master->cdev;

	return part;
err:
	free(part->cdev.partname);
	free(part->name);
	free(part);

	return ERR_PTR(ret);
}

int mtd_del_partition(struct mtd_info *part)
{
	if (!part->master)
		return -EINVAL;

	del_mtd_device(part);

	free(part->cdev.partname);
	free(part->name);
	free(part);

	return 0;
}