xref: /openbmc/u-boot/include/mtd/ubi-user.h (revision 47ae6693f54f80455ae32c2e0d995e0e4bdc15b9)
1*47ae6693SKyungmin Park /*
2*47ae6693SKyungmin Park  * Copyright (c) International Business Machines Corp., 2006
3*47ae6693SKyungmin Park  *
4*47ae6693SKyungmin Park  * This program is free software; you can redistribute it and/or modify
5*47ae6693SKyungmin Park  * it under the terms of the GNU General Public License as published by
6*47ae6693SKyungmin Park  * the Free Software Foundation; either version 2 of the License, or
7*47ae6693SKyungmin Park  * (at your option) any later version.
8*47ae6693SKyungmin Park  *
9*47ae6693SKyungmin Park  * This program is distributed in the hope that it will be useful,
10*47ae6693SKyungmin Park  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*47ae6693SKyungmin Park  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12*47ae6693SKyungmin Park  * the GNU General Public License for more details.
13*47ae6693SKyungmin Park  *
14*47ae6693SKyungmin Park  * You should have received a copy of the GNU General Public License
15*47ae6693SKyungmin Park  * along with this program; if not, write to the Free Software
16*47ae6693SKyungmin Park  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17*47ae6693SKyungmin Park  *
18*47ae6693SKyungmin Park  * Author: Artem Bityutskiy (Битюцкий Артём)
19*47ae6693SKyungmin Park  */
20*47ae6693SKyungmin Park 
21*47ae6693SKyungmin Park #ifndef __UBI_USER_H__
22*47ae6693SKyungmin Park #define __UBI_USER_H__
23*47ae6693SKyungmin Park 
24*47ae6693SKyungmin Park /*
25*47ae6693SKyungmin Park  * UBI device creation (the same as MTD device attachment)
26*47ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
27*47ae6693SKyungmin Park  *
28*47ae6693SKyungmin Park  * MTD devices may be attached using %UBI_IOCATT ioctl command of the UBI
29*47ae6693SKyungmin Park  * control device. The caller has to properly fill and pass
30*47ae6693SKyungmin Park  * &struct ubi_attach_req object - UBI will attach the MTD device specified in
31*47ae6693SKyungmin Park  * the request and return the newly created UBI device number as the ioctl
32*47ae6693SKyungmin Park  * return value.
33*47ae6693SKyungmin Park  *
34*47ae6693SKyungmin Park  * UBI device deletion (the same as MTD device detachment)
35*47ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
36*47ae6693SKyungmin Park  *
37*47ae6693SKyungmin Park  * An UBI device maybe deleted with %UBI_IOCDET ioctl command of the UBI
38*47ae6693SKyungmin Park  * control device.
39*47ae6693SKyungmin Park  *
40*47ae6693SKyungmin Park  * UBI volume creation
41*47ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~~~
42*47ae6693SKyungmin Park  *
43*47ae6693SKyungmin Park  * UBI volumes are created via the %UBI_IOCMKVOL IOCTL command of UBI character
44*47ae6693SKyungmin Park  * device. A &struct ubi_mkvol_req object has to be properly filled and a
45*47ae6693SKyungmin Park  * pointer to it has to be passed to the IOCTL.
46*47ae6693SKyungmin Park  *
47*47ae6693SKyungmin Park  * UBI volume deletion
48*47ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~~~
49*47ae6693SKyungmin Park  *
50*47ae6693SKyungmin Park  * To delete a volume, the %UBI_IOCRMVOL IOCTL command of the UBI character
51*47ae6693SKyungmin Park  * device should be used. A pointer to the 32-bit volume ID hast to be passed
52*47ae6693SKyungmin Park  * to the IOCTL.
53*47ae6693SKyungmin Park  *
54*47ae6693SKyungmin Park  * UBI volume re-size
55*47ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~~
56*47ae6693SKyungmin Park  *
57*47ae6693SKyungmin Park  * To re-size a volume, the %UBI_IOCRSVOL IOCTL command of the UBI character
58*47ae6693SKyungmin Park  * device should be used. A &struct ubi_rsvol_req object has to be properly
59*47ae6693SKyungmin Park  * filled and a pointer to it has to be passed to the IOCTL.
60*47ae6693SKyungmin Park  *
61*47ae6693SKyungmin Park  * UBI volume update
62*47ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~
63*47ae6693SKyungmin Park  *
64*47ae6693SKyungmin Park  * Volume update should be done via the %UBI_IOCVOLUP IOCTL command of the
65*47ae6693SKyungmin Park  * corresponding UBI volume character device. A pointer to a 64-bit update
66*47ae6693SKyungmin Park  * size should be passed to the IOCTL. After this, UBI expects user to write
67*47ae6693SKyungmin Park  * this number of bytes to the volume character device. The update is finished
68*47ae6693SKyungmin Park  * when the claimed number of bytes is passed. So, the volume update sequence
69*47ae6693SKyungmin Park  * is something like:
70*47ae6693SKyungmin Park  *
71*47ae6693SKyungmin Park  * fd = open("/dev/my_volume");
72*47ae6693SKyungmin Park  * ioctl(fd, UBI_IOCVOLUP, &image_size);
73*47ae6693SKyungmin Park  * write(fd, buf, image_size);
74*47ae6693SKyungmin Park  * close(fd);
75*47ae6693SKyungmin Park  *
76*47ae6693SKyungmin Park  * Atomic eraseblock change
77*47ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~~~~~~~~
78*47ae6693SKyungmin Park  *
79*47ae6693SKyungmin Park  * Atomic eraseblock change operation is done via the %UBI_IOCEBCH IOCTL
80*47ae6693SKyungmin Park  * command of the corresponding UBI volume character device. A pointer to
81*47ae6693SKyungmin Park  * &struct ubi_leb_change_req has to be passed to the IOCTL. Then the user is
82*47ae6693SKyungmin Park  * expected to write the requested amount of bytes. This is similar to the
83*47ae6693SKyungmin Park  * "volume update" IOCTL.
84*47ae6693SKyungmin Park  */
85*47ae6693SKyungmin Park 
86*47ae6693SKyungmin Park /*
87*47ae6693SKyungmin Park  * When a new UBI volume or UBI device is created, users may either specify the
88*47ae6693SKyungmin Park  * volume/device number they want to create or to let UBI automatically assign
89*47ae6693SKyungmin Park  * the number using these constants.
90*47ae6693SKyungmin Park  */
91*47ae6693SKyungmin Park #define UBI_VOL_NUM_AUTO (-1)
92*47ae6693SKyungmin Park #define UBI_DEV_NUM_AUTO (-1)
93*47ae6693SKyungmin Park 
94*47ae6693SKyungmin Park /* Maximum volume name length */
95*47ae6693SKyungmin Park #define UBI_MAX_VOLUME_NAME 127
96*47ae6693SKyungmin Park 
97*47ae6693SKyungmin Park /* IOCTL commands of UBI character devices */
98*47ae6693SKyungmin Park 
99*47ae6693SKyungmin Park #define UBI_IOC_MAGIC 'o'
100*47ae6693SKyungmin Park 
101*47ae6693SKyungmin Park /* Create an UBI volume */
102*47ae6693SKyungmin Park #define UBI_IOCMKVOL _IOW(UBI_IOC_MAGIC, 0, struct ubi_mkvol_req)
103*47ae6693SKyungmin Park /* Remove an UBI volume */
104*47ae6693SKyungmin Park #define UBI_IOCRMVOL _IOW(UBI_IOC_MAGIC, 1, int32_t)
105*47ae6693SKyungmin Park /* Re-size an UBI volume */
106*47ae6693SKyungmin Park #define UBI_IOCRSVOL _IOW(UBI_IOC_MAGIC, 2, struct ubi_rsvol_req)
107*47ae6693SKyungmin Park 
108*47ae6693SKyungmin Park /* IOCTL commands of the UBI control character device */
109*47ae6693SKyungmin Park 
110*47ae6693SKyungmin Park #define UBI_CTRL_IOC_MAGIC 'o'
111*47ae6693SKyungmin Park 
112*47ae6693SKyungmin Park /* Attach an MTD device */
113*47ae6693SKyungmin Park #define UBI_IOCATT _IOW(UBI_CTRL_IOC_MAGIC, 64, struct ubi_attach_req)
114*47ae6693SKyungmin Park /* Detach an MTD device */
115*47ae6693SKyungmin Park #define UBI_IOCDET _IOW(UBI_CTRL_IOC_MAGIC, 65, int32_t)
116*47ae6693SKyungmin Park 
117*47ae6693SKyungmin Park /* IOCTL commands of UBI volume character devices */
118*47ae6693SKyungmin Park 
119*47ae6693SKyungmin Park #define UBI_VOL_IOC_MAGIC 'O'
120*47ae6693SKyungmin Park 
121*47ae6693SKyungmin Park /* Start UBI volume update */
122*47ae6693SKyungmin Park #define UBI_IOCVOLUP _IOW(UBI_VOL_IOC_MAGIC, 0, int64_t)
123*47ae6693SKyungmin Park /* An eraseblock erasure command, used for debugging, disabled by default */
124*47ae6693SKyungmin Park #define UBI_IOCEBER _IOW(UBI_VOL_IOC_MAGIC, 1, int32_t)
125*47ae6693SKyungmin Park /* An atomic eraseblock change command */
126*47ae6693SKyungmin Park #define UBI_IOCEBCH _IOW(UBI_VOL_IOC_MAGIC, 2, int32_t)
127*47ae6693SKyungmin Park 
128*47ae6693SKyungmin Park /* Maximum MTD device name length supported by UBI */
129*47ae6693SKyungmin Park #define MAX_UBI_MTD_NAME_LEN 127
130*47ae6693SKyungmin Park 
131*47ae6693SKyungmin Park /*
132*47ae6693SKyungmin Park  * UBI data type hint constants.
133*47ae6693SKyungmin Park  *
134*47ae6693SKyungmin Park  * UBI_LONGTERM: long-term data
135*47ae6693SKyungmin Park  * UBI_SHORTTERM: short-term data
136*47ae6693SKyungmin Park  * UBI_UNKNOWN: data persistence is unknown
137*47ae6693SKyungmin Park  *
138*47ae6693SKyungmin Park  * These constants are used when data is written to UBI volumes in order to
139*47ae6693SKyungmin Park  * help the UBI wear-leveling unit to find more appropriate physical
140*47ae6693SKyungmin Park  * eraseblocks.
141*47ae6693SKyungmin Park  */
142*47ae6693SKyungmin Park enum {
143*47ae6693SKyungmin Park 	UBI_LONGTERM  = 1,
144*47ae6693SKyungmin Park 	UBI_SHORTTERM = 2,
145*47ae6693SKyungmin Park 	UBI_UNKNOWN   = 3,
146*47ae6693SKyungmin Park };
147*47ae6693SKyungmin Park 
148*47ae6693SKyungmin Park /*
149*47ae6693SKyungmin Park  * UBI volume type constants.
150*47ae6693SKyungmin Park  *
151*47ae6693SKyungmin Park  * @UBI_DYNAMIC_VOLUME: dynamic volume
152*47ae6693SKyungmin Park  * @UBI_STATIC_VOLUME:  static volume
153*47ae6693SKyungmin Park  */
154*47ae6693SKyungmin Park enum {
155*47ae6693SKyungmin Park 	UBI_DYNAMIC_VOLUME = 3,
156*47ae6693SKyungmin Park 	UBI_STATIC_VOLUME  = 4,
157*47ae6693SKyungmin Park };
158*47ae6693SKyungmin Park 
159*47ae6693SKyungmin Park /**
160*47ae6693SKyungmin Park  * struct ubi_attach_req - attach MTD device request.
161*47ae6693SKyungmin Park  * @ubi_num: UBI device number to create
162*47ae6693SKyungmin Park  * @mtd_num: MTD device number to attach
163*47ae6693SKyungmin Park  * @vid_hdr_offset: VID header offset (use defaults if %0)
164*47ae6693SKyungmin Park  * @padding: reserved for future, not used, has to be zeroed
165*47ae6693SKyungmin Park  *
166*47ae6693SKyungmin Park  * This data structure is used to specify MTD device UBI has to attach and the
167*47ae6693SKyungmin Park  * parameters it has to use. The number which should be assigned to the new UBI
168*47ae6693SKyungmin Park  * device is passed in @ubi_num. UBI may automatically assign the number if
169*47ae6693SKyungmin Park  * @UBI_DEV_NUM_AUTO is passed. In this case, the device number is returned in
170*47ae6693SKyungmin Park  * @ubi_num.
171*47ae6693SKyungmin Park  *
172*47ae6693SKyungmin Park  * Most applications should pass %0 in @vid_hdr_offset to make UBI use default
173*47ae6693SKyungmin Park  * offset of the VID header within physical eraseblocks. The default offset is
174*47ae6693SKyungmin Park  * the next min. I/O unit after the EC header. For example, it will be offset
175*47ae6693SKyungmin Park  * 512 in case of a 512 bytes page NAND flash with no sub-page support. Or
176*47ae6693SKyungmin Park  * it will be 512 in case of a 2KiB page NAND flash with 4 512-byte sub-pages.
177*47ae6693SKyungmin Park  *
178*47ae6693SKyungmin Park  * But in rare cases, if this optimizes things, the VID header may be placed to
179*47ae6693SKyungmin Park  * a different offset. For example, the boot-loader might do things faster if the
180*47ae6693SKyungmin Park  * VID header sits at the end of the first 2KiB NAND page with 4 sub-pages. As
181*47ae6693SKyungmin Park  * the boot-loader would not normally need to read EC headers (unless it needs
182*47ae6693SKyungmin Park  * UBI in RW mode), it might be faster to calculate ECC. This is weird example,
183*47ae6693SKyungmin Park  * but it real-life example. So, in this example, @vid_hdr_offer would be
184*47ae6693SKyungmin Park  * 2KiB-64 bytes = 1984. Note, that this position is not even 512-bytes
185*47ae6693SKyungmin Park  * aligned, which is OK, as UBI is clever enough to realize this is 4th sub-page
186*47ae6693SKyungmin Park  * of the first page and add needed padding.
187*47ae6693SKyungmin Park  */
188*47ae6693SKyungmin Park struct ubi_attach_req {
189*47ae6693SKyungmin Park 	int32_t ubi_num;
190*47ae6693SKyungmin Park 	int32_t mtd_num;
191*47ae6693SKyungmin Park 	int32_t vid_hdr_offset;
192*47ae6693SKyungmin Park 	uint8_t padding[12];
193*47ae6693SKyungmin Park };
194*47ae6693SKyungmin Park 
195*47ae6693SKyungmin Park /**
196*47ae6693SKyungmin Park  * struct ubi_mkvol_req - volume description data structure used in
197*47ae6693SKyungmin Park  *                        volume creation requests.
198*47ae6693SKyungmin Park  * @vol_id: volume number
199*47ae6693SKyungmin Park  * @alignment: volume alignment
200*47ae6693SKyungmin Park  * @bytes: volume size in bytes
201*47ae6693SKyungmin Park  * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
202*47ae6693SKyungmin Park  * @padding1: reserved for future, not used, has to be zeroed
203*47ae6693SKyungmin Park  * @name_len: volume name length
204*47ae6693SKyungmin Park  * @padding2: reserved for future, not used, has to be zeroed
205*47ae6693SKyungmin Park  * @name: volume name
206*47ae6693SKyungmin Park  *
207*47ae6693SKyungmin Park  * This structure is used by user-space programs when creating new volumes. The
208*47ae6693SKyungmin Park  * @used_bytes field is only necessary when creating static volumes.
209*47ae6693SKyungmin Park  *
210*47ae6693SKyungmin Park  * The @alignment field specifies the required alignment of the volume logical
211*47ae6693SKyungmin Park  * eraseblock. This means, that the size of logical eraseblocks will be aligned
212*47ae6693SKyungmin Park  * to this number, i.e.,
213*47ae6693SKyungmin Park  *	(UBI device logical eraseblock size) mod (@alignment) = 0.
214*47ae6693SKyungmin Park  *
215*47ae6693SKyungmin Park  * To put it differently, the logical eraseblock of this volume may be slightly
216*47ae6693SKyungmin Park  * shortened in order to make it properly aligned. The alignment has to be
217*47ae6693SKyungmin Park  * multiple of the flash minimal input/output unit, or %1 to utilize the entire
218*47ae6693SKyungmin Park  * available space of logical eraseblocks.
219*47ae6693SKyungmin Park  *
220*47ae6693SKyungmin Park  * The @alignment field may be useful, for example, when one wants to maintain
221*47ae6693SKyungmin Park  * a block device on top of an UBI volume. In this case, it is desirable to fit
222*47ae6693SKyungmin Park  * an integer number of blocks in logical eraseblocks of this UBI volume. With
223*47ae6693SKyungmin Park  * alignment it is possible to update this volume using plane UBI volume image
224*47ae6693SKyungmin Park  * BLOBs, without caring about how to properly align them.
225*47ae6693SKyungmin Park  */
226*47ae6693SKyungmin Park struct ubi_mkvol_req {
227*47ae6693SKyungmin Park 	int32_t vol_id;
228*47ae6693SKyungmin Park 	int32_t alignment;
229*47ae6693SKyungmin Park 	int64_t bytes;
230*47ae6693SKyungmin Park 	int8_t vol_type;
231*47ae6693SKyungmin Park 	int8_t padding1;
232*47ae6693SKyungmin Park 	int16_t name_len;
233*47ae6693SKyungmin Park 	int8_t padding2[4];
234*47ae6693SKyungmin Park 	char name[UBI_MAX_VOLUME_NAME + 1];
235*47ae6693SKyungmin Park } __attribute__ ((packed));
236*47ae6693SKyungmin Park 
237*47ae6693SKyungmin Park /**
238*47ae6693SKyungmin Park  * struct ubi_rsvol_req - a data structure used in volume re-size requests.
239*47ae6693SKyungmin Park  * @vol_id: ID of the volume to re-size
240*47ae6693SKyungmin Park  * @bytes: new size of the volume in bytes
241*47ae6693SKyungmin Park  *
242*47ae6693SKyungmin Park  * Re-sizing is possible for both dynamic and static volumes. But while dynamic
243*47ae6693SKyungmin Park  * volumes may be re-sized arbitrarily, static volumes cannot be made to be
244*47ae6693SKyungmin Park  * smaller then the number of bytes they bear. To arbitrarily shrink a static
245*47ae6693SKyungmin Park  * volume, it must be wiped out first (by means of volume update operation with
246*47ae6693SKyungmin Park  * zero number of bytes).
247*47ae6693SKyungmin Park  */
248*47ae6693SKyungmin Park struct ubi_rsvol_req {
249*47ae6693SKyungmin Park 	int64_t bytes;
250*47ae6693SKyungmin Park 	int32_t vol_id;
251*47ae6693SKyungmin Park } __attribute__ ((packed));
252*47ae6693SKyungmin Park 
253*47ae6693SKyungmin Park /**
254*47ae6693SKyungmin Park  * struct ubi_leb_change_req - a data structure used in atomic logical
255*47ae6693SKyungmin Park  *                             eraseblock change requests.
256*47ae6693SKyungmin Park  * @lnum: logical eraseblock number to change
257*47ae6693SKyungmin Park  * @bytes: how many bytes will be written to the logical eraseblock
258*47ae6693SKyungmin Park  * @dtype: data type (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
259*47ae6693SKyungmin Park  * @padding: reserved for future, not used, has to be zeroed
260*47ae6693SKyungmin Park  */
261*47ae6693SKyungmin Park struct ubi_leb_change_req {
262*47ae6693SKyungmin Park 	int32_t lnum;
263*47ae6693SKyungmin Park 	int32_t bytes;
264*47ae6693SKyungmin Park 	uint8_t dtype;
265*47ae6693SKyungmin Park 	uint8_t padding[7];
266*47ae6693SKyungmin Park } __attribute__ ((packed));
267*47ae6693SKyungmin Park 
268*47ae6693SKyungmin Park #endif /* __UBI_USER_H__ */
269