2 * Copyright (C) 2008, 2009 Nokia Corporation
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 * Author: Artem Bityutskiy
23 /* Imported from mtd-utils by dehrenberg */
32 // Needed for uint8_t, uint64_t
35 /* Maximum MTD device name length */
36 #define MTD_NAME_MAX 127
37 /* Maximum MTD device type string length */
38 #define MTD_TYPE_MAX 64
40 /* MTD library descriptor */
41 typedef void * libmtd_t;
44 struct region_info_user;
47 * @mtd_dev_cnt: count of MTD devices in system
48 * @lowest_mtd_num: lowest MTD device number in system
49 * @highest_mtd_num: highest MTD device number in system
50 * @sysfs_supported: non-zero if sysfs is supported by MTD
57 unsigned int sysfs_supported:1;
61 * struct mtd_dev_info - information about an MTD device.
62 * @mtd_num: MTD device number
63 * @major: major number of corresponding character device
64 * @minor: minor number of corresponding character device
65 * @type: flash type (constants like %MTD_NANDFLASH defined in mtd-abi.h)
66 * @type_str: static R/O flash type string
68 * @size: device size in bytes
69 * @eb_cnt: count of eraseblocks
70 * @eb_size: eraseblock size
71 * @min_io_size: minimum input/output unit size
72 * @subpage_size: sub-page size
73 * @oob_size: OOB size (zero if the device does not have OOB area)
74 * @region_cnt: count of additional erase regions
75 * @writable: zero if the device is read-only
76 * @bb_allowed: non-zero if the MTD device may have bad eraseblocks
84 char type_str[MTD_TYPE_MAX + 1];
85 char name[MTD_NAME_MAX + 1];
93 unsigned int writable:1;
94 unsigned int bb_allowed:1;
98 * libmtd_open - open MTD library.
100 * This function initializes and opens the MTD library and returns MTD library
101 * descriptor in case of success and %NULL in case of failure. In case of
102 * failure, errno contains zero if MTD is not present in the system, or
103 * contains the error code if a real error happened.
105 libmtd_t libmtd_open(void);
108 * libmtd_close - close MTD library.
109 * @desc: MTD library descriptor
111 void libmtd_close(libmtd_t desc);
114 * mtd_dev_present - check whether a MTD device is present.
115 * @desc: MTD library descriptor
116 * @mtd_num: MTD device number to check
118 * This function returns %1 if MTD device is present and %0 if not.
120 int mtd_dev_present(libmtd_t desc, int mtd_num);
123 * mtd_get_info - get general MTD information.
124 * @desc: MTD library descriptor
125 * @info: the MTD device information is returned here
127 * This function fills the passed @info object with general MTD information and
128 * returns %0 in case of success and %-1 in case of failure. If MTD subsystem is
129 * not present in the system, errno is set to @ENODEV.
131 int mtd_get_info(libmtd_t desc, struct mtd_info *info);
134 * mtd_get_dev_info - get information about an MTD device.
135 * @desc: MTD library descriptor
136 * @node: name of the MTD device node
137 * @mtd: the MTD device information is returned here
139 * This function gets information about MTD device defined by the @node device
140 * node file and saves this information in the @mtd object. Returns %0 in case
141 * of success and %-1 in case of failure. If MTD subsystem is not present in the
142 * system, or the MTD device does not exist, errno is set to @ENODEV.
144 int mtd_get_dev_info(libmtd_t desc, const char *node, struct mtd_dev_info *mtd);
147 * mtd_get_dev_info1 - get information about an MTD device.
148 * @desc: MTD library descriptor
149 * @mtd_num: MTD device number to fetch information about
150 * @mtd: the MTD device information is returned here
152 * This function is identical to 'mtd_get_dev_info()' except that it accepts
153 * MTD device number, not MTD character device.
155 int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd);
158 * mtd_lock - lock eraseblocks.
159 * @desc: MTD library descriptor
160 * @mtd: MTD device description object
161 * @fd: MTD device node file descriptor
162 * @eb: eraseblock to lock
164 * This function locks eraseblock @eb. Returns %0 in case of success and %-1
165 * in case of failure.
167 int mtd_lock(const struct mtd_dev_info *mtd, int fd, int eb);
170 * mtd_unlock - unlock eraseblocks.
171 * @desc: MTD library descriptor
172 * @mtd: MTD device description object
173 * @fd: MTD device node file descriptor
174 * @eb: eraseblock to lock
176 * This function unlocks eraseblock @eb. Returns %0 in case of success and %-1
177 * in case of failure.
179 int mtd_unlock(const struct mtd_dev_info *mtd, int fd, int eb);
182 * mtd_erase - erase an eraseblock.
183 * @desc: MTD library descriptor
184 * @mtd: MTD device description object
185 * @fd: MTD device node file descriptor
186 * @eb: eraseblock to erase
188 * This function erases eraseblock @eb of MTD device described by @fd. Returns
189 * %0 in case of success and %-1 in case of failure.
191 int mtd_erase(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb);
194 * mtd_regioninfo - get information about an erase region.
195 * @fd: MTD device node file descriptor
196 * @regidx: index of region to look up
197 * @reginfo: the region information is returned here
199 * This function gets information about an erase region defined by the
200 * @regidx index and saves this information in the @reginfo object.
201 * Returns %0 in case of success and %-1 in case of failure. If the
202 * @regidx is not valid or unavailable, errno is set to @ENODEV.
204 int mtd_regioninfo(int fd, int regidx, struct region_info_user *reginfo);
207 * mtd_is_locked - see if the specified eraseblock is locked.
208 * @mtd: MTD device description object
209 * @fd: MTD device node file descriptor
210 * @eb: eraseblock to check
212 * This function checks to see if eraseblock @eb of MTD device described
213 * by @fd is locked. Returns %0 if it is unlocked, %1 if it is locked, and
214 * %-1 in case of failure. If the ioctl is not supported (support was added in
215 * Linux kernel 2.6.36) or this particular device does not support it, errno is
218 int mtd_is_locked(const struct mtd_dev_info *mtd, int fd, int eb);
221 * mtd_torture - torture an eraseblock.
222 * @desc: MTD library descriptor
223 * @mtd: MTD device description object
224 * @fd: MTD device node file descriptor
225 * @eb: eraseblock to torture
227 * This function tortures eraseblock @eb. Returns %0 in case of success and %-1
228 * in case of failure.
230 int mtd_torture(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb);
233 * mtd_is_bad - check if eraseblock is bad.
234 * @mtd: MTD device description object
235 * @fd: MTD device node file descriptor
236 * @eb: eraseblock to check
238 * This function checks if eraseblock @eb is bad. Returns %0 if not, %1 if yes,
239 * and %-1 in case of failure.
241 int mtd_is_bad(const struct mtd_dev_info *mtd, int fd, int eb);
244 * mtd_mark_bad - mark an eraseblock as bad.
245 * @mtd: MTD device description object
246 * @fd: MTD device node file descriptor
247 * @eb: eraseblock to mark as bad
249 * This function marks eraseblock @eb as bad. Returns %0 in case of success and
250 * %-1 in case of failure.
252 int mtd_mark_bad(const struct mtd_dev_info *mtd, int fd, int eb);
255 * mtd_read - read data from an MTD device.
256 * @mtd: MTD device description object
257 * @fd: MTD device node file descriptor
258 * @eb: eraseblock to read from
259 * @offs: offset withing the eraseblock to read from
260 * @buf: buffer to read data to
261 * @len: how many bytes to read
263 * This function reads @len bytes of data from eraseblock @eb and offset @offs
264 * of the MTD device defined by @mtd and stores the read data at buffer @buf.
265 * Returns %0 in case of success and %-1 in case of failure.
267 int mtd_read(const struct mtd_dev_info *mtd, int fd, int eb, int offs,
271 * mtd_write - write data to an MTD device.
272 * @desc: MTD library descriptor
273 * @mtd: MTD device description object
274 * @fd: MTD device node file descriptor
275 * @eb: eraseblock to write to
276 * @offs: offset withing the eraseblock to write to
277 * @data: data buffer to write
278 * @len: how many data bytes to write
279 * @oob: OOB buffer to write
280 * @ooblen: how many OOB bytes to write
281 * @mode: write mode (e.g., %MTD_OOB_PLACE, %MTD_OOB_RAW)
283 * This function writes @len bytes of data to eraseblock @eb and offset @offs
284 * of the MTD device defined by @mtd. Returns %0 in case of success and %-1 in
287 * Can only write to a single page at a time if writing to OOB.
289 int mtd_write(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb,
290 int offs, void *data, int len, void *oob, int ooblen,
294 * mtd_read_oob - read out-of-band area.
295 * @desc: MTD library descriptor
296 * @mtd: MTD device description object
297 * @fd: MTD device node file descriptor
298 * @start: page-aligned start address
299 * @length: number of OOB bytes to read
302 * This function reads @length OOB bytes starting from address @start on
303 * MTD device described by @fd. The address is specified as page byte offset
304 * from the beginning of the MTD device. This function returns %0 in case of
305 * success and %-1 in case of failure.
307 int mtd_read_oob(libmtd_t desc, const struct mtd_dev_info *mtd, int fd,
308 uint64_t start, uint64_t length, void *data);
311 * mtd_write_oob - write out-of-band area.
312 * @desc: MTD library descriptor
313 * @mtd: MTD device description object
314 * @fd: MTD device node file descriptor
315 * @start: page-aligned start address
316 * @length: number of OOB bytes to write
317 * @data: write buffer
319 * This function writes @length OOB bytes starting from address @start on
320 * MTD device described by @fd. The address is specified as page byte offset
321 * from the beginning of the MTD device. Returns %0 in case of success and %-1
322 * in case of failure.
324 int mtd_write_oob(libmtd_t desc, const struct mtd_dev_info *mtd, int fd,
325 uint64_t start, uint64_t length, void *data);
328 * mtd_write_img - write a file to MTD device.
329 * @mtd: MTD device description object
330 * @fd: MTD device node file descriptor
331 * @eb: eraseblock to write to
332 * @offs: offset withing the eraseblock to write to
333 * @img_name: the file to write
335 * This function writes an image @img_name the MTD device defined by @mtd. @eb
336 * and @offs are the starting eraseblock and offset on the MTD device. Returns
337 * %0 in case of success and %-1 in case of failure.
339 int mtd_write_img(const struct mtd_dev_info *mtd, int fd, int eb, int offs,
340 const char *img_name);
343 * mtd_probe_node - test MTD node.
344 * @desc: MTD library descriptor
345 * @node: the node to test
347 * This function tests whether @node is an MTD device node and returns %1 if it
348 * is, and %-1 if it is not (errno is %ENODEV in this case) or if an error
351 int mtd_probe_node(libmtd_t desc, const char *node);
357 #endif /* __LIBMTD_H__ */