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