blob: a78c8cb5cdea36e990ef9f974b89c11b7316c8a5 [file] [log] [blame]
rjw1f884582022-01-06 17:20:42 +08001/*
2 * Copyright (C) 2008, 2009 Nokia Corporation
3 *
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.
8 *
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.
13 *
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., 675 Mass Ave, Cambridge, MA 02139, USA.
17 *
18 * Author: Artem Bityutskiy
19 *
20 * MTD library.
21 */
22
23#ifndef __LIBMTD_H__
24#define __LIBMTD_H__
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30/* Maximum MTD device name length */
31#define MTD_NAME_MAX 127
32/* Maximum MTD device type string length */
33#define MTD_TYPE_MAX 64
34
35/* MTD library descriptor */
36typedef void * libmtd_t;
37
38/* Forward decls */
39struct region_info_user;
40
41/**
42 * @mtd_dev_cnt: count of MTD devices in system
43 * @lowest_mtd_num: lowest MTD device number in system
44 * @highest_mtd_num: highest MTD device number in system
45 * @sysfs_supported: non-zero if sysfs is supported by MTD
46 */
47struct mtd_info
48{
49 int mtd_dev_cnt;
50 int lowest_mtd_num;
51 int highest_mtd_num;
52 unsigned int sysfs_supported:1;
53};
54
55/**
56 * struct mtd_dev_info - information about an MTD device.
57 * @mtd_num: MTD device number
58 * @major: major number of corresponding character device
59 * @minor: minor number of corresponding character device
60 * @type: flash type (constants like %MTD_NANDFLASH defined in mtd-abi.h)
61 * @type_str: static R/O flash type string
62 * @name: device name
63 * @size: device size in bytes
64 * @eb_cnt: count of eraseblocks
65 * @eb_size: eraseblock size
66 * @min_io_size: minimum input/output unit size
67 * @subpage_size: sub-page size
68 * @oob_size: OOB size (zero if the device does not have OOB area)
69 * @region_cnt: count of additional erase regions
70 * @writable: zero if the device is read-only
71 * @bb_allowed: non-zero if the MTD device may have bad eraseblocks
72 */
73struct mtd_dev_info
74{
75 int mtd_num;
76 int major;
77 int minor;
78 int type;
79 const char type_str[MTD_TYPE_MAX + 1];
80 const char name[MTD_NAME_MAX + 1];
81 long long size;
82 int eb_cnt;
83 int eb_size;
84 int min_io_size;
85 int subpage_size;
86 int oob_size;
87 int region_cnt;
88 unsigned int writable:1;
89 unsigned int bb_allowed:1;
90};
91
92/**
93 * libmtd_open - open MTD library.
94 *
95 * This function initializes and opens the MTD library and returns MTD library
96 * descriptor in case of success and %NULL in case of failure. In case of
97 * failure, errno contains zero if MTD is not present in the system, or
98 * contains the error code if a real error happened.
99 */
100libmtd_t libmtd_open(void);
101
102/**
103 * libmtd_close - close MTD library.
104 * @desc: MTD library descriptor
105 */
106void libmtd_close(libmtd_t desc);
107
108/**
109 * mtd_dev_present - check whether a MTD device is present.
110 * @desc: MTD library descriptor
111 * @mtd_num: MTD device number to check
112 *
113 * This function returns %1 if MTD device is present and %0 if not.
114 */
115int mtd_dev_present(libmtd_t desc, int mtd_num);
116
117/**
118 * mtd_get_info - get general MTD information.
119 * @desc: MTD library descriptor
120 * @info: the MTD device information is returned here
121 *
122 * This function fills the passed @info object with general MTD information and
123 * returns %0 in case of success and %-1 in case of failure. If MTD subsystem is
124 * not present in the system, errno is set to @ENODEV.
125 */
126int mtd_get_info(libmtd_t desc, struct mtd_info *info);
127
128/**
129 * mtd_get_dev_info - get information about an MTD device.
130 * @desc: MTD library descriptor
131 * @node: name of the MTD device node
132 * @mtd: the MTD device information is returned here
133 *
134 * This function gets information about MTD device defined by the @node device
135 * node file and saves this information in the @mtd object. Returns %0 in case
136 * of success and %-1 in case of failure. If MTD subsystem is not present in the
137 * system, or the MTD device does not exist, errno is set to @ENODEV.
138 */
139int mtd_get_dev_info(libmtd_t desc, const char *node, struct mtd_dev_info *mtd);
140
141/**
142 * mtd_get_dev_info1 - get information about an MTD device.
143 * @desc: MTD library descriptor
144 * @mtd_num: MTD device number to fetch information about
145 * @mtd: the MTD device information is returned here
146 *
147 * This function is identical to 'mtd_get_dev_info()' except that it accepts
148 * MTD device number, not MTD character device.
149 */
150int mtd_get_dev_info1(libmtd_t desc, int mtd_num, struct mtd_dev_info *mtd);
151
152/**
153 * mtd_lock - lock eraseblocks.
154 * @desc: MTD library descriptor
155 * @mtd: MTD device description object
156 * @fd: MTD device node file descriptor
157 * @eb: eraseblock to lock
158 *
159 * This function locks eraseblock @eb. Returns %0 in case of success and %-1
160 * in case of failure.
161 */
162int mtd_lock(const struct mtd_dev_info *mtd, int fd, int eb);
163
164/**
165 * mtd_unlock - unlock eraseblocks.
166 * @desc: MTD library descriptor
167 * @mtd: MTD device description object
168 * @fd: MTD device node file descriptor
169 * @eb: eraseblock to lock
170 *
171 * This function unlocks eraseblock @eb. Returns %0 in case of success and %-1
172 * in case of failure.
173 */
174int mtd_unlock(const struct mtd_dev_info *mtd, int fd, int eb);
175
176/**
177 * mtd_erase - erase an eraseblock.
178 * @desc: MTD library descriptor
179 * @mtd: MTD device description object
180 * @fd: MTD device node file descriptor
181 * @eb: eraseblock to erase
182 *
183 * This function erases eraseblock @eb of MTD device described by @fd. Returns
184 * %0 in case of success and %-1 in case of failure.
185 */
186int mtd_erase(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb);
187
188/**
189 * mtd_regioninfo - get information about an erase region.
190 * @fd: MTD device node file descriptor
191 * @regidx: index of region to look up
192 * @reginfo: the region information is returned here
193 *
194 * This function gets information about an erase region defined by the
195 * @regidx index and saves this information in the @reginfo object.
196 * Returns %0 in case of success and %-1 in case of failure. If the
197 * @regidx is not valid or unavailable, errno is set to @ENODEV.
198 */
199int mtd_regioninfo(int fd, int regidx, struct region_info_user *reginfo);
200
201/**
202 * mtd_is_locked - see if the specified eraseblock is locked.
203 * @mtd: MTD device description object
204 * @fd: MTD device node file descriptor
205 * @eb: eraseblock to check
206 *
207 * This function checks to see if eraseblock @eb of MTD device described
208 * by @fd is locked. Returns %0 if it is unlocked, %1 if it is locked, and
209 * %-1 in case of failure. If the ioctl is not supported (support was added in
210 * Linux kernel 2.6.36) or this particular device does not support it, errno is
211 * set to @ENOTSUPP.
212 */
213int mtd_is_locked(const struct mtd_dev_info *mtd, int fd, int eb);
214
215/**
216 * mtd_torture - torture an eraseblock.
217 * @desc: MTD library descriptor
218 * @mtd: MTD device description object
219 * @fd: MTD device node file descriptor
220 * @eb: eraseblock to torture
221 *
222 * This function tortures eraseblock @eb. Returns %0 in case of success and %-1
223 * in case of failure.
224 */
225int mtd_torture(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb);
226
227/**
228 * mtd_is_bad - check if eraseblock is bad.
229 * @mtd: MTD device description object
230 * @fd: MTD device node file descriptor
231 * @eb: eraseblock to check
232 *
233 * This function checks if eraseblock @eb is bad. Returns %0 if not, %1 if yes,
234 * and %-1 in case of failure.
235 */
236int mtd_is_bad(const struct mtd_dev_info *mtd, int fd, int eb);
237
238/**
239 * mtd_mark_bad - mark an eraseblock as bad.
240 * @mtd: MTD device description object
241 * @fd: MTD device node file descriptor
242 * @eb: eraseblock to mark as bad
243 *
244 * This function marks eraseblock @eb as bad. Returns %0 in case of success and
245 * %-1 in case of failure.
246 */
247int mtd_mark_bad(const struct mtd_dev_info *mtd, int fd, int eb);
248
249/**
250 * mtd_read - read data from an MTD device.
251 * @mtd: MTD device description object
252 * @fd: MTD device node file descriptor
253 * @eb: eraseblock to read from
254 * @offs: offset withing the eraseblock to read from
255 * @buf: buffer to read data to
256 * @len: how many bytes to read
257 *
258 * This function reads @len bytes of data from eraseblock @eb and offset @offs
259 * of the MTD device defined by @mtd and stores the read data at buffer @buf.
260 * Returns %0 in case of success and %-1 in case of failure.
261 */
262int mtd_read(const struct mtd_dev_info *mtd, int fd, int eb, int offs,
263 void *buf, int len);
264
265/**
266 * mtd_write - write data to an MTD device.
267 * @desc: MTD library descriptor
268 * @mtd: MTD device description object
269 * @fd: MTD device node file descriptor
270 * @eb: eraseblock to write to
271 * @offs: offset withing the eraseblock to write to
272 * @data: data buffer to write
273 * @len: how many data bytes to write
274 * @oob: OOB buffer to write
275 * @ooblen: how many OOB bytes to write
276 * @mode: write mode (e.g., %MTD_OOB_PLACE, %MTD_OOB_RAW)
277 *
278 * This function writes @len bytes of data to eraseblock @eb and offset @offs
279 * of the MTD device defined by @mtd. Returns %0 in case of success and %-1 in
280 * case of failure.
281 *
282 * Can only write to a single page at a time if writing to OOB.
283 */
284int mtd_write(libmtd_t desc, const struct mtd_dev_info *mtd, int fd, int eb,
285 int offs, void *data, int len, void *oob, int ooblen,
286 uint8_t mode);
287
288/**
289 * mtd_read_oob - read out-of-band area.
290 * @desc: MTD library descriptor
291 * @mtd: MTD device description object
292 * @fd: MTD device node file descriptor
293 * @start: page-aligned start address
294 * @length: number of OOB bytes to read
295 * @data: read buffer
296 *
297 * This function reads @length OOB bytes starting from address @start on
298 * MTD device described by @fd. The address is specified as page byte offset
299 * from the beginning of the MTD device. This function returns %0 in case of
300 * success and %-1 in case of failure.
301 */
302int mtd_read_oob(libmtd_t desc, const struct mtd_dev_info *mtd, int fd,
303 uint64_t start, uint64_t length, void *data);
304
305/**
306 * mtd_write_oob - write out-of-band area.
307 * @desc: MTD library descriptor
308 * @mtd: MTD device description object
309 * @fd: MTD device node file descriptor
310 * @start: page-aligned start address
311 * @length: number of OOB bytes to write
312 * @data: write buffer
313 *
314 * This function writes @length OOB bytes starting from address @start on
315 * MTD device described by @fd. The address is specified as page byte offset
316 * from the beginning of the MTD device. Returns %0 in case of success and %-1
317 * in case of failure.
318 */
319int mtd_write_oob(libmtd_t desc, const struct mtd_dev_info *mtd, int fd,
320 uint64_t start, uint64_t length, void *data);
321
322/**
323 * mtd_write_img - write a file to MTD device.
324 * @mtd: MTD device description object
325 * @fd: MTD device node file descriptor
326 * @eb: eraseblock to write to
327 * @offs: offset withing the eraseblock to write to
328 * @img_name: the file to write
329 *
330 * This function writes an image @img_name the MTD device defined by @mtd. @eb
331 * and @offs are the starting eraseblock and offset on the MTD device. Returns
332 * %0 in case of success and %-1 in case of failure.
333 */
334int mtd_write_img(const struct mtd_dev_info *mtd, int fd, int eb, int offs,
335 const char *img_name);
336
337/**
338 * mtd_probe_node - test MTD node.
339 * @desc: MTD library descriptor
340 * @node: the node to test
341 *
342 * This function tests whether @node is an MTD device node and returns %1 if it
343 * is, and %-1 if it is not (errno is %ENODEV in this case) or if an error
344 * occurred.
345 */
346int mtd_probe_node(libmtd_t desc, const char *node);
347
348#ifdef __cplusplus
349}
350#endif
351
352#endif /* __LIBMTD_H__ */