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