libmtd.h 11 KB

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