xref: /openbmc/linux/include/linux/mtd/ubi.h (revision 0e0ee1cc)
1801c135cSArtem B. Bityutskiy /*
2801c135cSArtem B. Bityutskiy  * Copyright (c) International Business Machines Corp., 2006
3801c135cSArtem B. Bityutskiy  *
4801c135cSArtem B. Bityutskiy  * This program is free software; you can redistribute it and/or modify
5801c135cSArtem B. Bityutskiy  * it under the terms of the GNU General Public License as published by
6801c135cSArtem B. Bityutskiy  * the Free Software Foundation; either version 2 of the License, or
7801c135cSArtem B. Bityutskiy  * (at your option) any later version.
8801c135cSArtem B. Bityutskiy  *
9801c135cSArtem B. Bityutskiy  * This program is distributed in the hope that it will be useful,
10801c135cSArtem B. Bityutskiy  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11801c135cSArtem B. Bityutskiy  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12801c135cSArtem B. Bityutskiy  * the GNU General Public License for more details.
13801c135cSArtem B. Bityutskiy  *
14801c135cSArtem B. Bityutskiy  * You should have received a copy of the GNU General Public License
15801c135cSArtem B. Bityutskiy  * along with this program; if not, write to the Free Software
16801c135cSArtem B. Bityutskiy  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17801c135cSArtem B. Bityutskiy  *
18801c135cSArtem B. Bityutskiy  * Author: Artem Bityutskiy (Битюцкий Артём)
19801c135cSArtem B. Bityutskiy  */
20801c135cSArtem B. Bityutskiy 
21801c135cSArtem B. Bityutskiy #ifndef __LINUX_UBI_H__
22801c135cSArtem B. Bityutskiy #define __LINUX_UBI_H__
23801c135cSArtem B. Bityutskiy 
24801c135cSArtem B. Bityutskiy #include <asm/ioctl.h>
25801c135cSArtem B. Bityutskiy #include <linux/types.h>
26801c135cSArtem B. Bityutskiy #include <mtd/ubi-user.h>
27801c135cSArtem B. Bityutskiy 
28801c135cSArtem B. Bityutskiy /*
29801c135cSArtem B. Bityutskiy  * enum ubi_open_mode - UBI volume open mode constants.
30801c135cSArtem B. Bityutskiy  *
31801c135cSArtem B. Bityutskiy  * UBI_READONLY: read-only mode
32801c135cSArtem B. Bityutskiy  * UBI_READWRITE: read-write mode
33801c135cSArtem B. Bityutskiy  * UBI_EXCLUSIVE: exclusive mode
34801c135cSArtem B. Bityutskiy  */
35801c135cSArtem B. Bityutskiy enum {
36801c135cSArtem B. Bityutskiy 	UBI_READONLY = 1,
37801c135cSArtem B. Bityutskiy 	UBI_READWRITE,
38801c135cSArtem B. Bityutskiy 	UBI_EXCLUSIVE
39801c135cSArtem B. Bityutskiy };
40801c135cSArtem B. Bityutskiy 
41801c135cSArtem B. Bityutskiy /**
42801c135cSArtem B. Bityutskiy  * struct ubi_volume_info - UBI volume description data structure.
43801c135cSArtem B. Bityutskiy  * @vol_id: volume ID
44801c135cSArtem B. Bityutskiy  * @ubi_num: UBI device number this volume belongs to
45801c135cSArtem B. Bityutskiy  * @size: how many physical eraseblocks are reserved for this volume
46801c135cSArtem B. Bityutskiy  * @used_bytes: how many bytes of data this volume contains
47801c135cSArtem B. Bityutskiy  * @used_ebs: how many physical eraseblocks of this volume actually contain any
48801c135cSArtem B. Bityutskiy  *            data
49801c135cSArtem B. Bityutskiy  * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
50801c135cSArtem B. Bityutskiy  * @corrupted: non-zero if the volume is corrupted (static volumes only)
51801c135cSArtem B. Bityutskiy  * @upd_marker: non-zero if the volume has update marker set
52801c135cSArtem B. Bityutskiy  * @alignment: volume alignment
53801c135cSArtem B. Bityutskiy  * @usable_leb_size: how many bytes are available in logical eraseblocks of
54801c135cSArtem B. Bityutskiy  *                   this volume
55801c135cSArtem B. Bityutskiy  * @name_len: volume name length
56801c135cSArtem B. Bityutskiy  * @name: volume name
57801c135cSArtem B. Bityutskiy  * @cdev: UBI volume character device major and minor numbers
58801c135cSArtem B. Bityutskiy  *
59801c135cSArtem B. Bityutskiy  * The @corrupted flag is only relevant to static volumes and is always zero
60801c135cSArtem B. Bityutskiy  * for dynamic ones. This is because UBI does not care about dynamic volume
61801c135cSArtem B. Bityutskiy  * data protection and only cares about protecting static volume data.
62801c135cSArtem B. Bityutskiy  *
63801c135cSArtem B. Bityutskiy  * The @upd_marker flag is set if the volume update operation was interrupted.
64801c135cSArtem B. Bityutskiy  * Before touching the volume data during the update operation, UBI first sets
65801c135cSArtem B. Bityutskiy  * the update marker flag for this volume. If the volume update operation was
66801c135cSArtem B. Bityutskiy  * further interrupted, the update marker indicates this. If the update marker
67801c135cSArtem B. Bityutskiy  * is set, the contents of the volume is certainly damaged and a new volume
68801c135cSArtem B. Bityutskiy  * update operation has to be started.
69801c135cSArtem B. Bityutskiy  *
70801c135cSArtem B. Bityutskiy  * To put it differently, @corrupted and @upd_marker fields have different
71801c135cSArtem B. Bityutskiy  * semantics:
72801c135cSArtem B. Bityutskiy  *     o the @corrupted flag means that this static volume is corrupted for some
73801c135cSArtem B. Bityutskiy  *       reasons, but not because an interrupted volume update
74801c135cSArtem B. Bityutskiy  *     o the @upd_marker field means that the volume is damaged because of an
75801c135cSArtem B. Bityutskiy  *       interrupted update operation.
76801c135cSArtem B. Bityutskiy  *
77801c135cSArtem B. Bityutskiy  * I.e., the @corrupted flag is never set if the @upd_marker flag is set.
78801c135cSArtem B. Bityutskiy  *
79801c135cSArtem B. Bityutskiy  * The @used_bytes and @used_ebs fields are only really needed for static
80801c135cSArtem B. Bityutskiy  * volumes and contain the number of bytes stored in this static volume and how
81801c135cSArtem B. Bityutskiy  * many eraseblock this data occupies. In case of dynamic volumes, the
82801c135cSArtem B. Bityutskiy  * @used_bytes field is equivalent to @size*@usable_leb_size, and the @used_ebs
83801c135cSArtem B. Bityutskiy  * field is equivalent to @size.
84801c135cSArtem B. Bityutskiy  *
85801c135cSArtem B. Bityutskiy  * In general, logical eraseblock size is a property of the UBI device, not
86801c135cSArtem B. Bityutskiy  * of the UBI volume. Indeed, the logical eraseblock size depends on the
87801c135cSArtem B. Bityutskiy  * physical eraseblock size and on how much bytes UBI headers consume. But
88801c135cSArtem B. Bityutskiy  * because of the volume alignment (@alignment), the usable size of logical
89801c135cSArtem B. Bityutskiy  * eraseblocks if a volume may be less. The following equation is true:
90801c135cSArtem B. Bityutskiy  * 	@usable_leb_size = LEB size - (LEB size mod @alignment),
91801c135cSArtem B. Bityutskiy  * where LEB size is the logical eraseblock size defined by the UBI device.
92801c135cSArtem B. Bityutskiy  *
93801c135cSArtem B. Bityutskiy  * The alignment is multiple to the minimal flash input/output unit size or %1
94801c135cSArtem B. Bityutskiy  * if all the available space is used.
95801c135cSArtem B. Bityutskiy  *
96801c135cSArtem B. Bityutskiy  * To put this differently, alignment may be considered is a way to change
97801c135cSArtem B. Bityutskiy  * volume logical eraseblock sizes.
98801c135cSArtem B. Bityutskiy  */
99801c135cSArtem B. Bityutskiy struct ubi_volume_info {
100801c135cSArtem B. Bityutskiy 	int ubi_num;
101801c135cSArtem B. Bityutskiy 	int vol_id;
102801c135cSArtem B. Bityutskiy 	int size;
103801c135cSArtem B. Bityutskiy 	long long used_bytes;
104801c135cSArtem B. Bityutskiy 	int used_ebs;
105801c135cSArtem B. Bityutskiy 	int vol_type;
106801c135cSArtem B. Bityutskiy 	int corrupted;
107801c135cSArtem B. Bityutskiy 	int upd_marker;
108801c135cSArtem B. Bityutskiy 	int alignment;
109801c135cSArtem B. Bityutskiy 	int usable_leb_size;
110801c135cSArtem B. Bityutskiy 	int name_len;
111801c135cSArtem B. Bityutskiy 	const char *name;
112801c135cSArtem B. Bityutskiy 	dev_t cdev;
113801c135cSArtem B. Bityutskiy };
114801c135cSArtem B. Bityutskiy 
115801c135cSArtem B. Bityutskiy /**
116801c135cSArtem B. Bityutskiy  * struct ubi_device_info - UBI device description data structure.
117801c135cSArtem B. Bityutskiy  * @ubi_num: ubi device number
118801c135cSArtem B. Bityutskiy  * @leb_size: logical eraseblock size on this UBI device
119801c135cSArtem B. Bityutskiy  * @min_io_size: minimal I/O unit size
120801c135cSArtem B. Bityutskiy  * @ro_mode: if this device is in read-only mode
121801c135cSArtem B. Bityutskiy  * @cdev: UBI character device major and minor numbers
122801c135cSArtem B. Bityutskiy  *
123801c135cSArtem B. Bityutskiy  * Note, @leb_size is the logical eraseblock size offered by the UBI device.
124801c135cSArtem B. Bityutskiy  * Volumes of this UBI device may have smaller logical eraseblock size if their
125801c135cSArtem B. Bityutskiy  * alignment is not equivalent to %1.
126801c135cSArtem B. Bityutskiy  */
127801c135cSArtem B. Bityutskiy struct ubi_device_info {
128801c135cSArtem B. Bityutskiy 	int ubi_num;
129801c135cSArtem B. Bityutskiy 	int leb_size;
130801c135cSArtem B. Bityutskiy 	int min_io_size;
131801c135cSArtem B. Bityutskiy 	int ro_mode;
132801c135cSArtem B. Bityutskiy 	dev_t cdev;
133801c135cSArtem B. Bityutskiy };
134801c135cSArtem B. Bityutskiy 
1350e0ee1ccSDmitry Pervushin /*
1360e0ee1ccSDmitry Pervushin  * enum - volume notification types.
1370e0ee1ccSDmitry Pervushin  * @UBI_VOLUME_ADDED: volume has been added
1380e0ee1ccSDmitry Pervushin  * @UBI_VOLUME_REMOVED: start volume volume
1390e0ee1ccSDmitry Pervushin  * @UBI_VOLUME_RESIZED: volume size has been re-sized
1400e0ee1ccSDmitry Pervushin  * @UBI_VOLUME_RENAMED: volume name has been re-named
1410e0ee1ccSDmitry Pervushin  * @UBI_VOLUME_UPDATED: volume name has been updated
1420e0ee1ccSDmitry Pervushin  *
1430e0ee1ccSDmitry Pervushin  * These constants define which type of event has happened when a volume
1440e0ee1ccSDmitry Pervushin  * notification function is invoked.
1450e0ee1ccSDmitry Pervushin  */
1460e0ee1ccSDmitry Pervushin enum {
1470e0ee1ccSDmitry Pervushin 	UBI_VOLUME_ADDED,
1480e0ee1ccSDmitry Pervushin 	UBI_VOLUME_REMOVED,
1490e0ee1ccSDmitry Pervushin 	UBI_VOLUME_RESIZED,
1500e0ee1ccSDmitry Pervushin 	UBI_VOLUME_RENAMED,
1510e0ee1ccSDmitry Pervushin 	UBI_VOLUME_UPDATED,
1520e0ee1ccSDmitry Pervushin };
1530e0ee1ccSDmitry Pervushin 
1540e0ee1ccSDmitry Pervushin /*
1550e0ee1ccSDmitry Pervushin  * struct ubi_notification - UBI notification description structure.
1560e0ee1ccSDmitry Pervushin  * @di: UBI device description object
1570e0ee1ccSDmitry Pervushin  * @vi: UBI volume description object
1580e0ee1ccSDmitry Pervushin  *
1590e0ee1ccSDmitry Pervushin  * UBI notifiers are called with a pointer to an object of this type. The
1600e0ee1ccSDmitry Pervushin  * object describes the notification. Namely, it provides a description of the
1610e0ee1ccSDmitry Pervushin  * UBI device and UBI volume the notification informs about.
1620e0ee1ccSDmitry Pervushin  */
1630e0ee1ccSDmitry Pervushin struct ubi_notification {
1640e0ee1ccSDmitry Pervushin 	struct ubi_device_info di;
1650e0ee1ccSDmitry Pervushin 	struct ubi_volume_info vi;
1660e0ee1ccSDmitry Pervushin };
1670e0ee1ccSDmitry Pervushin 
168801c135cSArtem B. Bityutskiy /* UBI descriptor given to users when they open UBI volumes */
169801c135cSArtem B. Bityutskiy struct ubi_volume_desc;
170801c135cSArtem B. Bityutskiy 
171801c135cSArtem B. Bityutskiy int ubi_get_device_info(int ubi_num, struct ubi_device_info *di);
172801c135cSArtem B. Bityutskiy void ubi_get_volume_info(struct ubi_volume_desc *desc,
173801c135cSArtem B. Bityutskiy 			 struct ubi_volume_info *vi);
174801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume(int ubi_num, int vol_id, int mode);
175801c135cSArtem B. Bityutskiy struct ubi_volume_desc *ubi_open_volume_nm(int ubi_num, const char *name,
176801c135cSArtem B. Bityutskiy 					   int mode);
1770e0ee1ccSDmitry Pervushin int ubi_register_volume_notifier(struct notifier_block *nb,
1780e0ee1ccSDmitry Pervushin 				 int ignore_existing);
1790e0ee1ccSDmitry Pervushin int ubi_unregister_volume_notifier(struct notifier_block *nb);
1800e0ee1ccSDmitry Pervushin 
181801c135cSArtem B. Bityutskiy void ubi_close_volume(struct ubi_volume_desc *desc);
182801c135cSArtem B. Bityutskiy int ubi_leb_read(struct ubi_volume_desc *desc, int lnum, char *buf, int offset,
183801c135cSArtem B. Bityutskiy 		 int len, int check);
184801c135cSArtem B. Bityutskiy int ubi_leb_write(struct ubi_volume_desc *desc, int lnum, const void *buf,
185801c135cSArtem B. Bityutskiy 		  int offset, int len, int dtype);
186801c135cSArtem B. Bityutskiy int ubi_leb_change(struct ubi_volume_desc *desc, int lnum, const void *buf,
187801c135cSArtem B. Bityutskiy 		   int len, int dtype);
188801c135cSArtem B. Bityutskiy int ubi_leb_erase(struct ubi_volume_desc *desc, int lnum);
189801c135cSArtem B. Bityutskiy int ubi_leb_unmap(struct ubi_volume_desc *desc, int lnum);
190393852ecSArtem Bityutskiy int ubi_leb_map(struct ubi_volume_desc *desc, int lnum, int dtype);
191801c135cSArtem B. Bityutskiy int ubi_is_mapped(struct ubi_volume_desc *desc, int lnum);
192a5bf6190SArtem Bityutskiy int ubi_sync(int ubi_num);
193801c135cSArtem B. Bityutskiy 
194801c135cSArtem B. Bityutskiy /*
195801c135cSArtem B. Bityutskiy  * This function is the same as the 'ubi_leb_read()' function, but it does not
196801c135cSArtem B. Bityutskiy  * provide the checking capability.
197801c135cSArtem B. Bityutskiy  */
198801c135cSArtem B. Bityutskiy static inline int ubi_read(struct ubi_volume_desc *desc, int lnum, char *buf,
199801c135cSArtem B. Bityutskiy 			   int offset, int len)
200801c135cSArtem B. Bityutskiy {
201801c135cSArtem B. Bityutskiy 	return ubi_leb_read(desc, lnum, buf, offset, len, 0);
202801c135cSArtem B. Bityutskiy }
203801c135cSArtem B. Bityutskiy 
204801c135cSArtem B. Bityutskiy /*
205801c135cSArtem B. Bityutskiy  * This function is the same as the 'ubi_leb_write()' functions, but it does
206801c135cSArtem B. Bityutskiy  * not have the data type argument.
207801c135cSArtem B. Bityutskiy  */
208801c135cSArtem B. Bityutskiy static inline int ubi_write(struct ubi_volume_desc *desc, int lnum,
209801c135cSArtem B. Bityutskiy 			    const void *buf, int offset, int len)
210801c135cSArtem B. Bityutskiy {
211801c135cSArtem B. Bityutskiy 	return ubi_leb_write(desc, lnum, buf, offset, len, UBI_UNKNOWN);
212801c135cSArtem B. Bityutskiy }
213801c135cSArtem B. Bityutskiy 
214801c135cSArtem B. Bityutskiy /*
215801c135cSArtem B. Bityutskiy  * This function is the same as the 'ubi_leb_change()' functions, but it does
216801c135cSArtem B. Bityutskiy  * not have the data type argument.
217801c135cSArtem B. Bityutskiy  */
218801c135cSArtem B. Bityutskiy static inline int ubi_change(struct ubi_volume_desc *desc, int lnum,
219801c135cSArtem B. Bityutskiy 				    const void *buf, int len)
220801c135cSArtem B. Bityutskiy {
221801c135cSArtem B. Bityutskiy 	return ubi_leb_change(desc, lnum, buf, len, UBI_UNKNOWN);
222801c135cSArtem B. Bityutskiy }
223801c135cSArtem B. Bityutskiy 
224801c135cSArtem B. Bityutskiy #endif /* !__LINUX_UBI_H__ */
225