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