ubi.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. *
  6. * Author: Artem Bityutskiy (Битюцкий Артём)
  7. */
  8. #ifndef __LINUX_UBI_H__
  9. #define __LINUX_UBI_H__
  10. #include <linux/types.h>
  11. #ifndef __UBOOT__
  12. #include <linux/ioctl.h>
  13. #include <mtd/ubi-user.h>
  14. #endif
  15. /* All voumes/LEBs */
  16. #define UBI_ALL -1
  17. /*
  18. * enum ubi_open_mode - UBI volume open mode constants.
  19. *
  20. * UBI_READONLY: read-only mode
  21. * UBI_READWRITE: read-write mode
  22. * UBI_EXCLUSIVE: exclusive mode
  23. */
  24. enum {
  25. UBI_READONLY = 1,
  26. UBI_READWRITE,
  27. UBI_EXCLUSIVE
  28. };
  29. /**
  30. * struct ubi_volume_info - UBI volume description data structure.
  31. * @vol_id: volume ID
  32. * @ubi_num: UBI device number this volume belongs to
  33. * @size: how many physical eraseblocks are reserved for this volume
  34. * @used_bytes: how many bytes of data this volume contains
  35. * @used_ebs: how many physical eraseblocks of this volume actually contain any
  36. * data
  37. * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  38. * @corrupted: non-zero if the volume is corrupted (static volumes only)
  39. * @upd_marker: non-zero if the volume has update marker set
  40. * @alignment: volume alignment
  41. * @usable_leb_size: how many bytes are available in logical eraseblocks of
  42. * this volume
  43. * @name_len: volume name length
  44. * @name: volume name
  45. * @cdev: UBI volume character device major and minor numbers
  46. *
  47. * The @corrupted flag is only relevant to static volumes and is always zero
  48. * for dynamic ones. This is because UBI does not care about dynamic volume
  49. * data protection and only cares about protecting static volume data.
  50. *
  51. * The @upd_marker flag is set if the volume update operation was interrupted.
  52. * Before touching the volume data during the update operation, UBI first sets
  53. * the update marker flag for this volume. If the volume update operation was
  54. * further interrupted, the update marker indicates this. If the update marker
  55. * is set, the contents of the volume is certainly damaged and a new volume
  56. * update operation has to be started.
  57. *
  58. * To put it differently, @corrupted and @upd_marker fields have different
  59. * semantics:
  60. * o the @corrupted flag means that this static volume is corrupted for some
  61. * reasons, but not because an interrupted volume update
  62. * o the @upd_marker field means that the volume is damaged because of an
  63. * interrupted update operation.
  64. *
  65. * I.e., the @corrupted flag is never set if the @upd_marker flag is set.
  66. *
  67. * The @used_bytes and @used_ebs fields are only really needed for static
  68. * volumes and contain the number of bytes stored in this static volume and how
  69. * many eraseblock this data occupies. In case of dynamic volumes, the
  70. * @used_bytes field is equivalent to @size*@usable_leb_size, and the @used_ebs
  71. * field is equivalent to @size.
  72. *
  73. * In general, logical eraseblock size is a property of the UBI device, not
  74. * of the UBI volume. Indeed, the logical eraseblock size depends on the
  75. * physical eraseblock size and on how much bytes UBI headers consume. But
  76. * because of the volume alignment (@alignment), the usable size of logical
  77. * eraseblocks if a volume may be less. The following equation is true:
  78. * @usable_leb_size = LEB size - (LEB size mod @alignment),
  79. * where LEB size is the logical eraseblock size defined by the UBI device.
  80. *
  81. * The alignment is multiple to the minimal flash input/output unit size or %1
  82. * if all the available space is used.
  83. *
  84. * To put this differently, alignment may be considered is a way to change
  85. * volume logical eraseblock sizes.
  86. */
  87. struct ubi_volume_info {
  88. int ubi_num;
  89. int vol_id;
  90. int size;
  91. long long used_bytes;
  92. int used_ebs;
  93. int vol_type;
  94. int corrupted;
  95. int upd_marker;
  96. int alignment;
  97. int usable_leb_size;
  98. int name_len;
  99. const char *name;
  100. dev_t cdev;
  101. };
  102. /**
  103. * struct ubi_device_info - UBI device description data structure.
  104. * @ubi_num: ubi device number
  105. * @leb_size: logical eraseblock size on this UBI device
  106. * @leb_start: starting offset of logical eraseblocks within physical
  107. * eraseblocks
  108. * @min_io_size: minimal I/O unit size
  109. * @max_write_size: maximum amount of bytes the underlying flash can write at a
  110. * time (MTD write buffer size)
  111. * @ro_mode: if this device is in read-only mode
  112. * @cdev: UBI character device major and minor numbers
  113. *
  114. * Note, @leb_size is the logical eraseblock size offered by the UBI device.
  115. * Volumes of this UBI device may have smaller logical eraseblock size if their
  116. * alignment is not equivalent to %1.
  117. *
  118. * The @max_write_size field describes flash write maximum write unit. For
  119. * example, NOR flash allows for changing individual bytes, so @min_io_size is
  120. * %1. However, it does not mean than NOR flash has to write data byte-by-byte.
  121. * Instead, CFI NOR flashes have a write-buffer of, e.g., 64 bytes, and when
  122. * writing large chunks of data, they write 64-bytes at a time. Obviously, this
  123. * improves write throughput.
  124. *
  125. * Also, the MTD device may have N interleaved (striped) flash chips
  126. * underneath, in which case @min_io_size can be physical min. I/O size of
  127. * single flash chip, while @max_write_size can be N * @min_io_size.
  128. *
  129. * The @max_write_size field is always greater or equivalent to @min_io_size.
  130. * E.g., some NOR flashes may have (@min_io_size = 1, @max_write_size = 64). In
  131. * contrast, NAND flashes usually have @min_io_size = @max_write_size = NAND
  132. * page size.
  133. */
  134. struct ubi_device_info {
  135. int ubi_num;
  136. int leb_size;
  137. int leb_start;
  138. int min_io_size;
  139. int max_write_size;
  140. int ro_mode;
  141. #ifndef __UBOOT__
  142. dev_t cdev;
  143. #endif
  144. };
  145. /*
  146. * Volume notification types.
  147. * @UBI_VOLUME_ADDED: a volume has been added (an UBI device was attached or a
  148. * volume was created)
  149. * @UBI_VOLUME_REMOVED: a volume has been removed (an UBI device was detached
  150. * or a volume was removed)
  151. * @UBI_VOLUME_RESIZED: a volume has been re-sized
  152. * @UBI_VOLUME_RENAMED: a volume has been re-named
  153. * @UBI_VOLUME_UPDATED: data has been written to a volume
  154. *
  155. * These constants define which type of event has happened when a volume
  156. * notification function is invoked.
  157. */
  158. enum {
  159. UBI_VOLUME_ADDED,
  160. UBI_VOLUME_REMOVED,
  161. UBI_VOLUME_RESIZED,
  162. UBI_VOLUME_RENAMED,
  163. UBI_VOLUME_UPDATED,
  164. };
  165. /*
  166. * struct ubi_notification - UBI notification description structure.
  167. * @di: UBI device description object
  168. * @vi: UBI volume description object
  169. *
  170. * UBI notifiers are called with a pointer to an object of this type. The
  171. * object describes the notification. Namely, it provides a description of the
  172. * UBI device and UBI volume the notification informs about.
  173. */
  174. struct ubi_notification {
  175. struct ubi_device_info di;
  176. struct ubi_volume_info vi;
  177. };
  178. /* UBI descriptor given to users when they open UBI volumes */
  179. struct ubi_volume_desc;
  180. int ubi_get_device_info(int ubi_num, struct ubi_device_info *di);
  181. void ubi_get_volume_info(struct ubi_volume_desc *desc,
  182. struct ubi_volume_info *vi);
  183. struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode);
  184. struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
  185. int mode);
  186. struct ubi_volume_desc *ubi_open_volume_path(const char *pathname, int mode);
  187. #ifndef __UBOOT__
  188. typedef int (*notifier_fn_t)(void *nb,
  189. unsigned long action, void *data);
  190. struct notifier_block {
  191. notifier_fn_t notifier_call;
  192. struct notifier_block *next;
  193. void *next;
  194. int priority;
  195. };
  196. int ubi_register_volume_notifier(struct notifier_block *nb,
  197. int ignore_existing);
  198. int ubi_unregister_volume_notifier(struct notifier_block *nb);
  199. #endif
  200. void ubi_close_volume(struct ubi_volume_desc *desc);
  201. int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
  202. int len, int check);
  203. int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
  204. int offset, int len);
  205. int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
  206. int len);
  207. int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum);
  208. int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum);
  209. int ubi_leb_map(struct ubi_volume_desc *desc, int lnum);
  210. int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum);
  211. int ubi_sync(int ubi_num);
  212. int ubi_flush(int ubi_num, int vol_id, int lnum);
  213. /*
  214. * This function is the same as the 'ubi_leb_read()' function, but it does not
  215. * provide the checking capability.
  216. */
  217. static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf,
  218. int offset, int len)
  219. {
  220. return ubi_leb_read(desc, lnum, buf, offset, len, 0);
  221. }
  222. #endif /* !__LINUX_UBI_H__ */