libubi.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  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. * UBI (Unsorted Block Images) library.
  21. */
  22. #ifndef __LIBUBI_H__
  23. #define __LIBUBI_H__
  24. #include <ctype.h>
  25. #include <stdint.h>
  26. #include <mtd/ubi-user.h>
  27. #ifdef __cplusplus
  28. extern "C" {
  29. #endif
  30. /* UBI version libubi is made for */
  31. #define LIBUBI_UBI_VERSION 1
  32. /* Maximum physical eraseblock size in bytes */
  33. #define UBI_MAX_PEB_SZ (2*1024*1024)
  34. /* The maximum volume name length (from Linux's ubi-media.h) */
  35. #define UBI_VOL_NAME_MAX 127
  36. /* UBI library descriptor */
  37. typedef void * libubi_t;
  38. /**
  39. * struct ubi_attach_request - MTD device attachment request.
  40. * @dev_num: number to assign to the newly created UBI device
  41. * (%UBI_DEV_NUM_AUTO should be used to automatically assign the
  42. * number)
  43. * @mtd_num: MTD device number to attach (used if @mtd_dev_node is %NULL)
  44. * @mtd_dev_node: path to MTD device node to attach
  45. * @vid_hdr_offset: VID header offset (%0 means default offset and this is what
  46. * most of the users want)
  47. * @max_beb_per1024: Maximum expected bad eraseblocks per 1024 eraseblocks
  48. */
  49. struct ubi_attach_request
  50. {
  51. int dev_num;
  52. int mtd_num;
  53. const char *mtd_dev_node;
  54. int vid_hdr_offset;
  55. int max_beb_per1024;
  56. };
  57. /**
  58. * struct ubi_mkvol_request - volume creation request.
  59. * @vol_id: ID to assign to the new volume (%UBI_VOL_NUM_AUTO should be used to
  60. * automatically assign ID)
  61. * @alignment: volume alignment
  62. * @bytes: volume size in bytes
  63. * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  64. * @name: volume name
  65. * @flags: volume flags
  66. */
  67. struct ubi_mkvol_request
  68. {
  69. int vol_id;
  70. int alignment;
  71. long long bytes;
  72. int vol_type;
  73. const char *name;
  74. uint8_t flags;
  75. };
  76. /**
  77. * struct ubi_info - general UBI information.
  78. * @dev_count: count of UBI devices in system
  79. * @lowest_dev_num: lowest UBI device number
  80. * @highest_dev_num: highest UBI device number
  81. * @version: UBI version
  82. * @ctrl_major: major number of the UBI control device
  83. * @ctrl_minor: minor number of the UBI control device
  84. */
  85. struct ubi_info
  86. {
  87. int dev_count;
  88. int lowest_dev_num;
  89. int highest_dev_num;
  90. int version;
  91. int ctrl_major;
  92. int ctrl_minor;
  93. };
  94. /**
  95. * struct ubi_dev_info - UBI device information.
  96. * @dev_num: UBI device number
  97. * @mtd_num: MTD device number on top of which this UBI device is working
  98. * @vol_count: count of volumes on this UBI device
  99. * @lowest_vol_id: lowest volume ID
  100. * @highest_vol_id: highest volume ID
  101. * @major: major number of corresponding character device
  102. * @minor: minor number of corresponding character device
  103. * @total_lebs: total number of logical eraseblocks on this UBI device
  104. * @avail_lebs: how many logical eraseblocks are not used and available for new
  105. * volumes
  106. * @total_bytes: @total_lebs * @leb_size
  107. * @avail_bytes: @avail_lebs * @leb_size
  108. * @bad_count: count of bad physical eraseblocks
  109. * @leb_size: logical eraseblock size
  110. * @max_ec: current highest erase counter value
  111. * @bad_rsvd: how many physical eraseblocks of the underlying flash device are
  112. * reserved for bad eraseblocks handling
  113. * @max_vol_count: maximum possible number of volumes on this UBI device
  114. * @min_io_size: minimum input/output unit size of the UBI device
  115. */
  116. struct ubi_dev_info
  117. {
  118. int dev_num;
  119. int mtd_num;
  120. int vol_count;
  121. int lowest_vol_id;
  122. int highest_vol_id;
  123. int major;
  124. int minor;
  125. int total_lebs;
  126. int avail_lebs;
  127. long long total_bytes;
  128. long long avail_bytes;
  129. int bad_count;
  130. int leb_size;
  131. long long max_ec;
  132. int bad_rsvd;
  133. int max_vol_count;
  134. int min_io_size;
  135. };
  136. /**
  137. * struct ubi_vol_info - UBI volume information.
  138. * @dev_num: UBI device number the volume resides on
  139. * @vol_id: ID of this volume
  140. * @major: major number of corresponding volume character device
  141. * @minor: minor number of corresponding volume character device
  142. * @type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  143. * @alignment: alignment of this volume
  144. * @data_bytes: how many data bytes are stored on this volume (equivalent to
  145. * @rsvd_bytes for dynamic volumes)
  146. * @rsvd_bytes: how many bytes are reserved for this volume
  147. * @rsvd_lebs: how many logical eraseblocks are reserved for this volume
  148. * @leb_size: logical eraseblock size of this volume (may be less then
  149. * device's logical eraseblock size due to alignment)
  150. * @corrupted: non-zero if the volume is corrupted
  151. * @name: volume name (null-terminated)
  152. */
  153. struct ubi_vol_info
  154. {
  155. int dev_num;
  156. int vol_id;
  157. int major;
  158. int minor;
  159. int type;
  160. int alignment;
  161. long long data_bytes;
  162. long long rsvd_bytes;
  163. int rsvd_lebs;
  164. int leb_size;
  165. int corrupted;
  166. char name[UBI_VOL_NAME_MAX + 1];
  167. };
  168. /**
  169. * libubi_open - open UBI library.
  170. *
  171. * This function initializes and opens the UBI library and returns UBI library
  172. * descriptor in case of success and %NULL in case of failure. In case of
  173. * failure, errno contains the error code or zero if UBI is not present in the
  174. * system.
  175. */
  176. libubi_t libubi_open(void);
  177. /**
  178. * libubi_close - close UBI library.
  179. * @desc: UBI library descriptor
  180. */
  181. void libubi_close(libubi_t desc);
  182. /**
  183. * ubi_get_info - get general UBI information.
  184. * @desc: UBI library descriptor
  185. * @info: pointer to the &struct ubi_info object to fill
  186. *
  187. * This function fills the passed @info object with general UBI information and
  188. * returns %0 in case of success and %-1 in case of failure.
  189. */
  190. int ubi_get_info(libubi_t desc, struct ubi_info *info);
  191. /**
  192. * mtd_num2ubi_dev - find UBI device by attached MTD device.
  193. * @@desc: UBI library descriptor
  194. * @mtd_num: MTD device number
  195. * @dev_num: UBI device number is returned here
  196. *
  197. * This function finds UBI device to which MTD device @mtd_num is attached.
  198. * Returns %0 if the UBI device was found and %-1 if not.
  199. */
  200. int mtd_num2ubi_dev(libubi_t desc, int mtd_num, int *dev_num);
  201. /**
  202. * ubi_attach - attach an MTD device by its node path or bt MTD device number
  203. * @desc: UBI library descriptor
  204. * @node: name of the UBI control character device node
  205. * @req: MTD attach request
  206. *
  207. * This function creates new UBI device by attaching an MTD device described by
  208. * @req. If @req->mtd_dev_node is given it should contain path to the MTD
  209. * device node. Otherwise @req->mtd_num will be used.
  210. *
  211. * Returns %0 in case of success, %-1 in case of failure (errno is set) and %1
  212. * if parameter @req->max_beb_per1024 was ignored by kernel (because the kernel
  213. * is old and does not support this feature, which was added in 3.7). The newly
  214. * created UBI device number is returned in @req->dev_num. In the MTD device
  215. * was specified by its device node path, the MTD device number is returned in
  216. * @req->mtd_num.
  217. */
  218. int ubi_attach(libubi_t desc, const char *node, struct ubi_attach_request *req);
  219. /**
  220. * ubi_detach_mtd - detach an MTD device.
  221. * @desc: UBI library descriptor
  222. * @node: name of the UBI control character device node
  223. * @mtd_num: MTD device number to detach
  224. *
  225. * This function detaches MTD device number @mtd_num from UBI, which means the
  226. * corresponding UBI device is removed. Returns zero in case of success and %-1
  227. * in case of failure.
  228. */
  229. int ubi_detach_mtd(libubi_t desc, const char *node, int mtd_num);
  230. /**
  231. * ubi_detach - detach an MTD device by its node path.
  232. * @desc: UBI library descriptor
  233. * @node: name of the UBI control character device node
  234. * @mtd_dev_node: path to an MTD device node
  235. *
  236. * This function detaches an MTD device @mtd_dev_node from UBI. Returns zero in
  237. * case of success and %-1 in case of failure.
  238. */
  239. int ubi_detach(libubi_t desc, const char *node, const char *mtd_dev_node);
  240. /**
  241. * ubi_remove_dev - remove an UBI device.
  242. * @desc: UBI library descriptor
  243. * @node: name of the UBI control character device node
  244. * @ubi_dev: UBI device number to remove
  245. *
  246. * This function removes UBI device number @ubi_dev and returns zero in case of
  247. * success and %-1 in case of failure.
  248. */
  249. int ubi_remove_dev(libubi_t desc, const char *node, int ubi_dev);
  250. /**
  251. * ubi_mkvol - create an UBI volume.
  252. * @desc: UBI library descriptor
  253. * @node: name of the UBI character device to create a volume at
  254. * @req: UBI volume creation request
  255. *
  256. * This function creates a UBI volume as described at @req and returns %0 in
  257. * case of success and %-1 in case of failure. The assigned volume ID is
  258. * returned in @req->vol_id.
  259. */
  260. int ubi_mkvol(libubi_t desc, const char *node, struct ubi_mkvol_request *req);
  261. /**
  262. * ubi_rmvol - remove a UBI volume.
  263. * @desc: UBI library descriptor
  264. * @node: name of the UBI character device to remove a volume from
  265. * @vol_id: ID of the volume to remove
  266. *
  267. * This function removes volume @vol_id from UBI device @node and returns %0 in
  268. * case of success and %-1 in case of failure.
  269. */
  270. int ubi_rmvol(libubi_t desc, const char *node, int vol_id);
  271. /**
  272. * ubi_rnvols - rename UBI volumes.
  273. * @desc: UBI library descriptor
  274. * @node: name of the UBI character device to remove a volume from
  275. * @rnvol: description of volumes to rename
  276. *
  277. * This function removes volume @vol_id from UBI device @node and returns %0 in
  278. * case of success and %-1 in case of failure.
  279. */
  280. int ubi_rnvols(libubi_t desc, const char *node, struct ubi_rnvol_req *rnvol);
  281. /**
  282. * ubi_rsvol - re-size UBI volume.
  283. * @desc: UBI library descriptor
  284. * @node: name of the UBI character device owning the volume which should be
  285. * re-sized
  286. * @vol_id: volume ID to re-size
  287. * @bytes: new volume size in bytes
  288. *
  289. * This function returns %0 in case of success and %-1 in case of error.
  290. */
  291. int ubi_rsvol(libubi_t desc, const char *node, int vol_id, long long bytes);
  292. /**
  293. * ubi_probe_node - test UBI node.
  294. * @desc: UBI library descriptor
  295. * @node: the node to test
  296. *
  297. * This function tests whether @node is a UBI device or volume node and returns
  298. * %1 if this is an UBI device node, %2 if this is a volume node, and %-1 if
  299. * this is not an UBI device or volume node (errno is ENODEV in this case) or
  300. * if an error occurred.
  301. */
  302. int ubi_probe_node(libubi_t desc, const char *node);
  303. /**
  304. * ubi_get_dev_info - get UBI device information.
  305. * @desc: UBI library descriptor
  306. * @node: name of the UBI character device to fetch information about
  307. * @info: pointer to the &struct ubi_dev_info object to fill
  308. *
  309. * This function fills the passed @info object with UBI device information and
  310. * returns %0 in case of success and %-1 in case of failure. If the UBI device
  311. * corresponding to @node does not exist, errno is set to @ENODEV.
  312. */
  313. int ubi_get_dev_info(libubi_t desc, const char *node,
  314. struct ubi_dev_info *info);
  315. /**
  316. * ubi_dev_present - check whether an UBI device is present.
  317. * @desc: UBI library descriptor
  318. * @dev_num: UBI device number to check
  319. *
  320. * This function returns %1 if UBI device is present and %0 if not.
  321. */
  322. int ubi_dev_present(libubi_t desc, int dev_num);
  323. /**
  324. * ubi_get_dev_info1 - get UBI device information.
  325. * @desc: UBI library descriptor
  326. * @dev_num: UBI device number to fetch information about
  327. * @info: pointer to the &struct ubi_dev_info object to fill
  328. *
  329. * This function is identical to 'ubi_get_dev_info()' except that it accepts UBI
  330. * device number, not UBI character device. If the UBI device @dev_num does not
  331. * exist, errno is set to @ENODEV.
  332. */
  333. int ubi_get_dev_info1(libubi_t desc, int dev_num, struct ubi_dev_info *info);
  334. /**
  335. * ubi_get_vol_info - get UBI volume information.
  336. * @desc: UBI library descriptor
  337. * @node: name of the UBI volume character device to fetch information about
  338. * @info: pointer to the &struct ubi_vol_info object to fill
  339. *
  340. * This function fills the passed @info object with UBI volume information and
  341. * returns %0 in case of success and %-1 in case of failure. If the UBI volume
  342. * corresponding to @node does not exist, errno is set to @ENODEV.
  343. */
  344. int ubi_get_vol_info(libubi_t desc, const char *node,
  345. struct ubi_vol_info *info);
  346. /**
  347. * ubi_get_vol_info1 - get UBI volume information.
  348. * @desc: UBI library descriptor
  349. * @dev_num: UBI device number
  350. * @vol_id: ID of the UBI volume to fetch information about
  351. * @info: pointer to the &struct ubi_vol_info object to fill
  352. *
  353. * This function is identical to 'ubi_get_vol_info()' except that it accepts UBI
  354. * volume ID, not UBI volume character device. If the UBI device @dev_num does
  355. * not exist, or if the UBI volume @vol_id does not exist, errno is set to
  356. * @ENODEV.
  357. */
  358. int ubi_get_vol_info1(libubi_t desc, int dev_num, int vol_id,
  359. struct ubi_vol_info *info);
  360. /**
  361. * ubi_get_vol_info1_nm - get UBI volume information by volume name.
  362. * @desc: UBI library descriptor
  363. * @dev_num: UBI device number
  364. * @name: name of the UBI volume to fetch information about
  365. * @info: pointer to the &struct ubi_vol_info object to fill
  366. *
  367. * This function is identical to 'ubi_get_vol_info()' except that it accepts UBI
  368. * volume name, not UBI volume ID. If the UBI device @dev_num does not exist,
  369. * or if the UBI volume @name does not exist, errno is set to @ENODEV.
  370. */
  371. int ubi_get_vol_info1_nm(libubi_t desc, int dev_num, const char *name,
  372. struct ubi_vol_info *info);
  373. /**
  374. * ubi_vol_block_create - create a block device on top of an UBI volume.
  375. * @fd: volume character device file descriptor
  376. *
  377. * Returns %0 in case of success and %-1 in case of failure.
  378. */
  379. int ubi_vol_block_create(int fd);
  380. /**
  381. * ubi_vol_block_remove - remove a block device from an UBI volume.
  382. * @fd: volume character device file descriptor
  383. *
  384. * Returns %0 in case of success and %-1 in case of failure.
  385. */
  386. int ubi_vol_block_remove(int fd);
  387. /**
  388. * ubi_update_start - start UBI volume update.
  389. * @desc: UBI library descriptor
  390. * @fd: volume character device file descriptor
  391. * @bytes: how many bytes will be written to the volume
  392. *
  393. * This function initiates UBI volume update and returns %0 in case of success
  394. * and %-1 in case of error. The caller is assumed to write @bytes data to the
  395. * volume @fd afterward.
  396. */
  397. int ubi_update_start(libubi_t desc, int fd, long long bytes);
  398. /**
  399. * ubi_leb_change_start - start atomic LEB change.
  400. * @desc: UBI library descriptor
  401. * @fd: volume character device file descriptor
  402. * @lnum: LEB number to change
  403. * @bytes: how many bytes of new data will be written to the LEB
  404. *
  405. * This function initiates atomic LEB change operation and returns %0 in case
  406. * of success and %-1 in case of error. he caller is assumed to write @bytes
  407. * data to the volume @fd afterward.
  408. */
  409. int ubi_leb_change_start(libubi_t desc, int fd, int lnum, int bytes);
  410. /**
  411. * ubi_set_property - set volume propety.
  412. * @fd: volume character device file descriptor
  413. * @property: the property to change (%UBI_VOL_PROP_DIRECT_WRITE, etc)
  414. * @value: new value of the changed property
  415. *
  416. * This function changes a property of a volume. Returns zero in case of
  417. * success and a negative error code in case of error.
  418. */
  419. int ubi_set_property(int fd, uint8_t property, uint64_t value);
  420. /**
  421. * ubi_leb_unmap - unmap a logical eraseblock.
  422. * @fd: volume character device file descriptor
  423. * @lnum: logical eraseblock to unmap
  424. *
  425. * This function unmaps LEB @lnum and returns zero in case of success and a
  426. * negative error code in case of error.
  427. */
  428. int ubi_leb_unmap(int fd, int lnum);
  429. /**
  430. * ubi_is_mapped - check if logical eraseblock is mapped.
  431. * @fd: volume character device file descriptor
  432. * @lnum: logical eraseblock number
  433. *
  434. * This function checks if logical eraseblock @lnum is mapped to a physical
  435. * eraseblock. If a logical eraseblock is un-mapped, this does not necessarily
  436. * mean it will still be un-mapped after the UBI device is re-attached. The
  437. * logical eraseblock may become mapped to the physical eraseblock it was last
  438. * mapped to.
  439. *
  440. * This function returns %1 if the LEB is mapped, %0 if not, and %-1 in case of
  441. * failure. If the volume is damaged because of an interrupted update errno
  442. * set with %EBADF error code.
  443. */
  444. int ubi_is_mapped(int fd, int lnum);
  445. #ifdef __cplusplus
  446. }
  447. #endif
  448. #endif /* !__LIBUBI_H__ */