aboutsummaryrefslogtreecommitdiff
path: root/ubi-utils/new-utils/src/libmtd.c
blob: b60ddbdfd08c031aad8cf3c632abdb4b5979e832 (plain)
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
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
/*
 * Copyright (C) 2008 Nokia Corporation
 *
 * 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 * Author: Artem Bityutskiy
 *
 * MTD library.
 */

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <fcntl.h>

#include <mtd/mtd-user.h>
#include <libmtd.h>
#include "common.h"

#define PROGRAM_NAME "libmtd"
#define MTD_DEV_MAJOR 90

/**
 * mtd_get_info - get information about an MTD device.
 * @node: name of the MTD device node
 * @mtd: the MTD device information is returned here
 *
 * This function gets information about MTD device defined by the @node device
 * node file and saves this information in the @mtd object. Returns %0 in case
 * of success and %-1 in case of failure.
 */
int mtd_get_info(const char *node, struct mtd_info *mtd)
{
	struct stat st;
	struct mtd_info_user ui;
	int ret;
	loff_t offs = 0;

	if (stat(node, &st))
		return sys_errmsg("cannot open \"%s\"", node);

	if (!S_ISCHR(st.st_mode)) {
		errno = EINVAL;
		return errmsg("\"%s\" is not a character device", node);
	}

	mtd->major = major(st.st_rdev);
	mtd->minor = minor(st.st_rdev);

	if (mtd->major != MTD_DEV_MAJOR) {
		errno = EINVAL;
		return errmsg("\"%s\" has major number %d, MTD devices have "
			      "major %d", node, mtd->major, MTD_DEV_MAJOR);
	}

	mtd->num = mtd->minor / 2;
	mtd->rdonly = mtd->minor & 1;

	mtd->fd = open(node, O_RDWR);
	if (mtd->fd == -1)
		return sys_errmsg("cannot open \"%s\"", node);

	if (ioctl(mtd->fd, MEMGETINFO, &ui)) {
		sys_errmsg("MEMGETINFO ioctl request failed");
		goto out_close;
	}

	ret = ioctl(mtd->fd, MEMGETBADBLOCK, &offs);
	if (ret == -1) {
		if (errno != EOPNOTSUPP) {
			sys_errmsg("MEMGETBADBLOCK ioctl failed");
			goto out_close;
		}
		errno = 0;
		mtd->allows_bb = 0;
	} else
		mtd->allows_bb = 1;

	mtd->type = ui.type;
	mtd->size = ui.size;
	mtd->eb_size = ui.erasesize;
	mtd->min_io_size = ui.writesize;

	if (mtd->min_io_size <= 0) {
		errmsg("mtd%d (%s) has insane min. I/O unit size %d",
		       mtd->num, node, mtd->min_io_size);
		goto out_close;
	}
	if (mtd->eb_size <= 0 || mtd->eb_size < mtd->min_io_size) {
		errmsg("mtd%d (%s) has insane eraseblock size %d",
		       mtd->num, node, mtd->eb_size);
		goto out_close;
	}
	if (mtd->size <= 0 || mtd->size < mtd->eb_size) {
		errmsg("mtd%d (%s) has insane size %lld",
		       mtd->num, node, mtd->size);
		goto out_close;
	}
	mtd->eb_cnt = mtd->size / mtd->eb_size;

	switch(mtd->type) {
	case MTD_ABSENT:
		errmsg("mtd%d (%s) is removable and is not present",
		       mtd->num, node);
		goto out_close;
	case MTD_RAM:
		mtd->type_str = "RAM-based";
		break;
	case MTD_ROM:
		mtd->type_str = "ROM";
		break;
	case MTD_NORFLASH:
		mtd->type_str = "NOR";
		break;
	case MTD_NANDFLASH:
		mtd->type_str = "NAND";
		break;
	case MTD_DATAFLASH:
		mtd->type_str = "DataFlash";
		break;
	case MTD_UBIVOLUME:
		mtd->type_str = "UBI-emulated MTD";
		break;
	default:
		mtd->type_str = "Unknown flash type";
		break;
	}

	if (!(ui.flags & MTD_WRITEABLE))
		mtd->rdonly = 1;

	return 0;

out_close:
	close(mtd->fd);
	return -1;
}

/**
 * mtd_erase - erase an eraseblock.
 * @mtd: MTD device description object
 * @eb: eraseblock to erase
 *
 * This function erases the eraseblock and returns %0 in case of success and
 * %-1 in case of failure.
 */
int mtd_erase(const struct mtd_info *mtd, int eb)
{
	struct erase_info_user ei;

	ei.start = eb * mtd->eb_size;;
	ei.length = mtd->eb_size;
	return ioctl(mtd->fd, MEMERASE, &ei);
}

/**
 * mtd_is_bad - check if eraseblock is bad.
 * @mtd: MTD device description object
 * @eb: eraseblock to check
 *
 * This function checks if eraseblock @eb is bad. Returns %0 if not, %1 if yes,
 * and %-1 in case of failure.
 */
int mtd_is_bad(const struct mtd_info *mtd, int eb)
{
	int ret;
	loff_t seek;

	if (eb < 0 || eb >= mtd->eb_cnt) {
		errmsg("bad eraseblock number %d, mtd%d has %d eraseblocks",
		       eb, mtd->num, mtd->eb_cnt);
		errno = EINVAL;
		return -1;
	}

	if (!mtd->allows_bb)
		return 0;

	seek = (loff_t)eb * mtd->eb_size;
	ret = ioctl(mtd->fd, MEMGETBADBLOCK, &seek);
	if (ret == -1) {
		sys_errmsg("MEMGETBADBLOCK ioctl failed for "
			   "eraseblock %d (mtd%d)", eb, mtd->num);
		return -1;
	}

	return ret;
}

/**
 * mtd_read - read data from an MTD device.
 * @mtd: MTD device description object
 * @eb: eraseblock to read from
 * @offs: offset withing the eraseblock to read from
 * @buf: buffer to read data to
 * @len: how many bytes to read
 *
 * This function reads @len bytes of data from eraseblock @eb and offset @offs
 * of the MTD device defined by @mtd and stores the read data at buffer @buf.
 * Returns %0 in case of success and %-1 in case of failure.
 */
int mtd_read(const struct mtd_info *mtd, int eb, int offs, void *buf, int len)
{
	int ret, rd = 0;
	off_t seek;

	if (eb < 0 || eb >= mtd->eb_cnt) {
		errmsg("bad eraseblock number %d, mtd%d has %d eraseblocks",
		       eb, mtd->num, mtd->eb_cnt);
		errno = EINVAL;
		return -1;
	}
	if (offs < 0 || offs + len > mtd->eb_size) {
		errmsg("bad offset %d or length %d, mtd%d eraseblock size is %d",
		       offs, len, mtd->num, mtd->eb_size);
		errno = EINVAL;
		return -1;
	}

	/* Seek to the beginning of the eraseblock */
	seek = (off_t)eb * mtd->eb_size + offs;
	if (lseek(mtd->fd, seek, SEEK_SET) != seek) {
		sys_errmsg("cannot seek mtd%d to offset %llu",
			   mtd->num, (unsigned long long)seek);
		return -1;
	}

	while (rd < len) {
		ret = read(mtd->fd, buf, len);
		if (ret < 0) {
			sys_errmsg("cannot read %d bytes from mtd%d (eraseblock %d, offset %d)",
				   len, mtd->num, eb, offs);
			return -1;
		}
		rd += ret;
	}

	return 0;
}

/**
 * mtd_write - write data to an MTD device.
 * @mtd: MTD device description object
 * @eb: eraseblock to write to
 * @offs: offset withing the eraseblock to write to
 * @buf: buffer to write
 * @len: how many bytes to write
 *
 * This function writes @len bytes of data to eraseblock @eb and offset @offs
 * of the MTD device defined by @mtd. Returns %0 in case of success and %-1 in
 * case of failure.
 */
int mtd_write(const struct mtd_info *mtd, int eb, int offs, void *buf, int len)
{
	int ret;
	off_t seek;

	if (eb < 0 || eb >= mtd->eb_cnt) {
		errmsg("bad eraseblock number %d, mtd%d has %d eraseblocks",
		       eb, mtd->num, mtd->eb_cnt);
		errno = EINVAL;
		return -1;
	}
	if (offs < 0 || offs + len > mtd->eb_size) {
		errmsg("bad offset %d or length %d, mtd%d eraseblock size is %d",
		       offs, len, mtd->num, mtd->eb_size);
		errno = EINVAL;
		return -1;
	}
#if 0
	if (offs % mtd->subpage_size) {
		errmsg("write offset %d is not aligned to mtd%d min. I/O size %d",
		       offs, mtd->num, mtd->subpage_size);
		errno = EINVAL;
		return -1;
	}
	if (len % mtd->subpage_size) {
		errmsg("write length %d is not aligned to mtd%d min. I/O size %d",
		       len, mtd->num, mtd->subpage_size);
		errno = EINVAL;
		return -1;
	}
#endif

	/* Seek to the beginning of the eraseblock */
	seek = (off_t)eb * mtd->eb_size + offs;
	if (lseek(mtd->fd, seek, SEEK_SET) != seek) {
		sys_errmsg("cannot seek mtd%d to offset %llu",
			   mtd->num, (unsigned long long)seek);
		return -1;
	}

	ret = write(mtd->fd, buf, len);
	if (ret != len) {
		sys_errmsg("cannot write %d bytes to mtd%d (eraseblock %d, offset %d)",
			   len, mtd->num, eb, offs);
		return -1;
	}

	return 0;
}