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