xref: /rk3399_rockchip-uboot/include/mtd/ubi-user.h (revision 1a4596601fd395f3afb8f82f3f840c5e00bdd57a)
147ae6693SKyungmin Park /*
247ae6693SKyungmin Park  * Copyright (c) International Business Machines Corp., 2006
347ae6693SKyungmin Park  *
4*1a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
547ae6693SKyungmin Park  *
647ae6693SKyungmin Park  * Author: Artem Bityutskiy (Битюцкий Артём)
747ae6693SKyungmin Park  */
847ae6693SKyungmin Park 
947ae6693SKyungmin Park #ifndef __UBI_USER_H__
1047ae6693SKyungmin Park #define __UBI_USER_H__
1147ae6693SKyungmin Park 
1247ae6693SKyungmin Park /*
1347ae6693SKyungmin Park  * UBI device creation (the same as MTD device attachment)
1447ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
1547ae6693SKyungmin Park  *
1647ae6693SKyungmin Park  * MTD devices may be attached using %UBI_IOCATT ioctl command of the UBI
1747ae6693SKyungmin Park  * control device. The caller has to properly fill and pass
1847ae6693SKyungmin Park  * &struct ubi_attach_req object - UBI will attach the MTD device specified in
1947ae6693SKyungmin Park  * the request and return the newly created UBI device number as the ioctl
2047ae6693SKyungmin Park  * return value.
2147ae6693SKyungmin Park  *
2247ae6693SKyungmin Park  * UBI device deletion (the same as MTD device detachment)
2347ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
2447ae6693SKyungmin Park  *
2547ae6693SKyungmin Park  * An UBI device maybe deleted with %UBI_IOCDET ioctl command of the UBI
2647ae6693SKyungmin Park  * control device.
2747ae6693SKyungmin Park  *
2847ae6693SKyungmin Park  * UBI volume creation
2947ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~~~
3047ae6693SKyungmin Park  *
3147ae6693SKyungmin Park  * UBI volumes are created via the %UBI_IOCMKVOL IOCTL command of UBI character
3247ae6693SKyungmin Park  * device. A &struct ubi_mkvol_req object has to be properly filled and a
3347ae6693SKyungmin Park  * pointer to it has to be passed to the IOCTL.
3447ae6693SKyungmin Park  *
3547ae6693SKyungmin Park  * UBI volume deletion
3647ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~~~
3747ae6693SKyungmin Park  *
3847ae6693SKyungmin Park  * To delete a volume, the %UBI_IOCRMVOL IOCTL command of the UBI character
3947ae6693SKyungmin Park  * device should be used. A pointer to the 32-bit volume ID hast to be passed
4047ae6693SKyungmin Park  * to the IOCTL.
4147ae6693SKyungmin Park  *
4247ae6693SKyungmin Park  * UBI volume re-size
4347ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~~
4447ae6693SKyungmin Park  *
4547ae6693SKyungmin Park  * To re-size a volume, the %UBI_IOCRSVOL IOCTL command of the UBI character
4647ae6693SKyungmin Park  * device should be used. A &struct ubi_rsvol_req object has to be properly
4747ae6693SKyungmin Park  * filled and a pointer to it has to be passed to the IOCTL.
4847ae6693SKyungmin Park  *
4947ae6693SKyungmin Park  * UBI volume update
5047ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~
5147ae6693SKyungmin Park  *
5247ae6693SKyungmin Park  * Volume update should be done via the %UBI_IOCVOLUP IOCTL command of the
5347ae6693SKyungmin Park  * corresponding UBI volume character device. A pointer to a 64-bit update
5447ae6693SKyungmin Park  * size should be passed to the IOCTL. After this, UBI expects user to write
5547ae6693SKyungmin Park  * this number of bytes to the volume character device. The update is finished
5647ae6693SKyungmin Park  * when the claimed number of bytes is passed. So, the volume update sequence
5747ae6693SKyungmin Park  * is something like:
5847ae6693SKyungmin Park  *
5947ae6693SKyungmin Park  * fd = open("/dev/my_volume");
6047ae6693SKyungmin Park  * ioctl(fd, UBI_IOCVOLUP, &image_size);
6147ae6693SKyungmin Park  * write(fd, buf, image_size);
6247ae6693SKyungmin Park  * close(fd);
6347ae6693SKyungmin Park  *
6447ae6693SKyungmin Park  * Atomic eraseblock change
6547ae6693SKyungmin Park  * ~~~~~~~~~~~~~~~~~~~~~~~~
6647ae6693SKyungmin Park  *
6747ae6693SKyungmin Park  * Atomic eraseblock change operation is done via the %UBI_IOCEBCH IOCTL
6847ae6693SKyungmin Park  * command of the corresponding UBI volume character device. A pointer to
6947ae6693SKyungmin Park  * &struct ubi_leb_change_req has to be passed to the IOCTL. Then the user is
7047ae6693SKyungmin Park  * expected to write the requested amount of bytes. This is similar to the
7147ae6693SKyungmin Park  * "volume update" IOCTL.
7247ae6693SKyungmin Park  */
7347ae6693SKyungmin Park 
7447ae6693SKyungmin Park /*
7547ae6693SKyungmin Park  * When a new UBI volume or UBI device is created, users may either specify the
7647ae6693SKyungmin Park  * volume/device number they want to create or to let UBI automatically assign
7747ae6693SKyungmin Park  * the number using these constants.
7847ae6693SKyungmin Park  */
7947ae6693SKyungmin Park #define UBI_VOL_NUM_AUTO (-1)
8047ae6693SKyungmin Park #define UBI_DEV_NUM_AUTO (-1)
8147ae6693SKyungmin Park 
8247ae6693SKyungmin Park /* Maximum volume name length */
8347ae6693SKyungmin Park #define UBI_MAX_VOLUME_NAME 127
8447ae6693SKyungmin Park 
8547ae6693SKyungmin Park /* IOCTL commands of UBI character devices */
8647ae6693SKyungmin Park 
8747ae6693SKyungmin Park #define UBI_IOC_MAGIC 'o'
8847ae6693SKyungmin Park 
8947ae6693SKyungmin Park /* Create an UBI volume */
9047ae6693SKyungmin Park #define UBI_IOCMKVOL _IOW(UBI_IOC_MAGIC, 0, struct ubi_mkvol_req)
9147ae6693SKyungmin Park /* Remove an UBI volume */
9247ae6693SKyungmin Park #define UBI_IOCRMVOL _IOW(UBI_IOC_MAGIC, 1, int32_t)
9347ae6693SKyungmin Park /* Re-size an UBI volume */
9447ae6693SKyungmin Park #define UBI_IOCRSVOL _IOW(UBI_IOC_MAGIC, 2, struct ubi_rsvol_req)
9547ae6693SKyungmin Park 
9647ae6693SKyungmin Park /* IOCTL commands of the UBI control character device */
9747ae6693SKyungmin Park 
9847ae6693SKyungmin Park #define UBI_CTRL_IOC_MAGIC 'o'
9947ae6693SKyungmin Park 
10047ae6693SKyungmin Park /* Attach an MTD device */
10147ae6693SKyungmin Park #define UBI_IOCATT _IOW(UBI_CTRL_IOC_MAGIC, 64, struct ubi_attach_req)
10247ae6693SKyungmin Park /* Detach an MTD device */
10347ae6693SKyungmin Park #define UBI_IOCDET _IOW(UBI_CTRL_IOC_MAGIC, 65, int32_t)
10447ae6693SKyungmin Park 
10547ae6693SKyungmin Park /* IOCTL commands of UBI volume character devices */
10647ae6693SKyungmin Park 
10747ae6693SKyungmin Park #define UBI_VOL_IOC_MAGIC 'O'
10847ae6693SKyungmin Park 
10947ae6693SKyungmin Park /* Start UBI volume update */
11047ae6693SKyungmin Park #define UBI_IOCVOLUP _IOW(UBI_VOL_IOC_MAGIC, 0, int64_t)
11147ae6693SKyungmin Park /* An eraseblock erasure command, used for debugging, disabled by default */
11247ae6693SKyungmin Park #define UBI_IOCEBER _IOW(UBI_VOL_IOC_MAGIC, 1, int32_t)
11347ae6693SKyungmin Park /* An atomic eraseblock change command */
11447ae6693SKyungmin Park #define UBI_IOCEBCH _IOW(UBI_VOL_IOC_MAGIC, 2, int32_t)
11547ae6693SKyungmin Park 
11647ae6693SKyungmin Park /* Maximum MTD device name length supported by UBI */
11747ae6693SKyungmin Park #define MAX_UBI_MTD_NAME_LEN 127
11847ae6693SKyungmin Park 
11947ae6693SKyungmin Park /*
12047ae6693SKyungmin Park  * UBI data type hint constants.
12147ae6693SKyungmin Park  *
12247ae6693SKyungmin Park  * UBI_LONGTERM: long-term data
12347ae6693SKyungmin Park  * UBI_SHORTTERM: short-term data
12447ae6693SKyungmin Park  * UBI_UNKNOWN: data persistence is unknown
12547ae6693SKyungmin Park  *
12647ae6693SKyungmin Park  * These constants are used when data is written to UBI volumes in order to
12747ae6693SKyungmin Park  * help the UBI wear-leveling unit to find more appropriate physical
12847ae6693SKyungmin Park  * eraseblocks.
12947ae6693SKyungmin Park  */
13047ae6693SKyungmin Park enum {
13147ae6693SKyungmin Park 	UBI_LONGTERM  = 1,
13247ae6693SKyungmin Park 	UBI_SHORTTERM = 2,
13347ae6693SKyungmin Park 	UBI_UNKNOWN   = 3,
13447ae6693SKyungmin Park };
13547ae6693SKyungmin Park 
13647ae6693SKyungmin Park /*
13747ae6693SKyungmin Park  * UBI volume type constants.
13847ae6693SKyungmin Park  *
13947ae6693SKyungmin Park  * @UBI_DYNAMIC_VOLUME: dynamic volume
14047ae6693SKyungmin Park  * @UBI_STATIC_VOLUME:  static volume
14147ae6693SKyungmin Park  */
14247ae6693SKyungmin Park enum {
14347ae6693SKyungmin Park 	UBI_DYNAMIC_VOLUME = 3,
14447ae6693SKyungmin Park 	UBI_STATIC_VOLUME  = 4,
14547ae6693SKyungmin Park };
14647ae6693SKyungmin Park 
14747ae6693SKyungmin Park /**
14847ae6693SKyungmin Park  * struct ubi_attach_req - attach MTD device request.
14947ae6693SKyungmin Park  * @ubi_num: UBI device number to create
15047ae6693SKyungmin Park  * @mtd_num: MTD device number to attach
15147ae6693SKyungmin Park  * @vid_hdr_offset: VID header offset (use defaults if %0)
15247ae6693SKyungmin Park  * @padding: reserved for future, not used, has to be zeroed
15347ae6693SKyungmin Park  *
15447ae6693SKyungmin Park  * This data structure is used to specify MTD device UBI has to attach and the
15547ae6693SKyungmin Park  * parameters it has to use. The number which should be assigned to the new UBI
15647ae6693SKyungmin Park  * device is passed in @ubi_num. UBI may automatically assign the number if
15747ae6693SKyungmin Park  * @UBI_DEV_NUM_AUTO is passed. In this case, the device number is returned in
15847ae6693SKyungmin Park  * @ubi_num.
15947ae6693SKyungmin Park  *
16047ae6693SKyungmin Park  * Most applications should pass %0 in @vid_hdr_offset to make UBI use default
16147ae6693SKyungmin Park  * offset of the VID header within physical eraseblocks. The default offset is
16247ae6693SKyungmin Park  * the next min. I/O unit after the EC header. For example, it will be offset
16347ae6693SKyungmin Park  * 512 in case of a 512 bytes page NAND flash with no sub-page support. Or
16447ae6693SKyungmin Park  * it will be 512 in case of a 2KiB page NAND flash with 4 512-byte sub-pages.
16547ae6693SKyungmin Park  *
16647ae6693SKyungmin Park  * But in rare cases, if this optimizes things, the VID header may be placed to
16747ae6693SKyungmin Park  * a different offset. For example, the boot-loader might do things faster if the
16847ae6693SKyungmin Park  * VID header sits at the end of the first 2KiB NAND page with 4 sub-pages. As
16947ae6693SKyungmin Park  * the boot-loader would not normally need to read EC headers (unless it needs
17047ae6693SKyungmin Park  * UBI in RW mode), it might be faster to calculate ECC. This is weird example,
17147ae6693SKyungmin Park  * but it real-life example. So, in this example, @vid_hdr_offer would be
17247ae6693SKyungmin Park  * 2KiB-64 bytes = 1984. Note, that this position is not even 512-bytes
17347ae6693SKyungmin Park  * aligned, which is OK, as UBI is clever enough to realize this is 4th sub-page
17447ae6693SKyungmin Park  * of the first page and add needed padding.
17547ae6693SKyungmin Park  */
17647ae6693SKyungmin Park struct ubi_attach_req {
17747ae6693SKyungmin Park 	int32_t ubi_num;
17847ae6693SKyungmin Park 	int32_t mtd_num;
17947ae6693SKyungmin Park 	int32_t vid_hdr_offset;
18047ae6693SKyungmin Park 	uint8_t padding[12];
18147ae6693SKyungmin Park };
18247ae6693SKyungmin Park 
18347ae6693SKyungmin Park /**
18447ae6693SKyungmin Park  * struct ubi_mkvol_req - volume description data structure used in
18547ae6693SKyungmin Park  *                        volume creation requests.
18647ae6693SKyungmin Park  * @vol_id: volume number
18747ae6693SKyungmin Park  * @alignment: volume alignment
18847ae6693SKyungmin Park  * @bytes: volume size in bytes
18947ae6693SKyungmin Park  * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
19047ae6693SKyungmin Park  * @padding1: reserved for future, not used, has to be zeroed
19147ae6693SKyungmin Park  * @name_len: volume name length
19247ae6693SKyungmin Park  * @padding2: reserved for future, not used, has to be zeroed
19347ae6693SKyungmin Park  * @name: volume name
19447ae6693SKyungmin Park  *
19547ae6693SKyungmin Park  * This structure is used by user-space programs when creating new volumes. The
19647ae6693SKyungmin Park  * @used_bytes field is only necessary when creating static volumes.
19747ae6693SKyungmin Park  *
19847ae6693SKyungmin Park  * The @alignment field specifies the required alignment of the volume logical
19947ae6693SKyungmin Park  * eraseblock. This means, that the size of logical eraseblocks will be aligned
20047ae6693SKyungmin Park  * to this number, i.e.,
20147ae6693SKyungmin Park  *	(UBI device logical eraseblock size) mod (@alignment) = 0.
20247ae6693SKyungmin Park  *
20347ae6693SKyungmin Park  * To put it differently, the logical eraseblock of this volume may be slightly
20447ae6693SKyungmin Park  * shortened in order to make it properly aligned. The alignment has to be
20547ae6693SKyungmin Park  * multiple of the flash minimal input/output unit, or %1 to utilize the entire
20647ae6693SKyungmin Park  * available space of logical eraseblocks.
20747ae6693SKyungmin Park  *
20847ae6693SKyungmin Park  * The @alignment field may be useful, for example, when one wants to maintain
20947ae6693SKyungmin Park  * a block device on top of an UBI volume. In this case, it is desirable to fit
21047ae6693SKyungmin Park  * an integer number of blocks in logical eraseblocks of this UBI volume. With
21147ae6693SKyungmin Park  * alignment it is possible to update this volume using plane UBI volume image
21247ae6693SKyungmin Park  * BLOBs, without caring about how to properly align them.
21347ae6693SKyungmin Park  */
21447ae6693SKyungmin Park struct ubi_mkvol_req {
21547ae6693SKyungmin Park 	int32_t vol_id;
21647ae6693SKyungmin Park 	int32_t alignment;
21747ae6693SKyungmin Park 	int64_t bytes;
21847ae6693SKyungmin Park 	int8_t vol_type;
21947ae6693SKyungmin Park 	int8_t padding1;
22047ae6693SKyungmin Park 	int16_t name_len;
22147ae6693SKyungmin Park 	int8_t padding2[4];
22247ae6693SKyungmin Park 	char name[UBI_MAX_VOLUME_NAME + 1];
22347ae6693SKyungmin Park } __attribute__ ((packed));
22447ae6693SKyungmin Park 
22547ae6693SKyungmin Park /**
22647ae6693SKyungmin Park  * struct ubi_rsvol_req - a data structure used in volume re-size requests.
22747ae6693SKyungmin Park  * @vol_id: ID of the volume to re-size
22847ae6693SKyungmin Park  * @bytes: new size of the volume in bytes
22947ae6693SKyungmin Park  *
23047ae6693SKyungmin Park  * Re-sizing is possible for both dynamic and static volumes. But while dynamic
23147ae6693SKyungmin Park  * volumes may be re-sized arbitrarily, static volumes cannot be made to be
23247ae6693SKyungmin Park  * smaller then the number of bytes they bear. To arbitrarily shrink a static
23347ae6693SKyungmin Park  * volume, it must be wiped out first (by means of volume update operation with
23447ae6693SKyungmin Park  * zero number of bytes).
23547ae6693SKyungmin Park  */
23647ae6693SKyungmin Park struct ubi_rsvol_req {
23747ae6693SKyungmin Park 	int64_t bytes;
23847ae6693SKyungmin Park 	int32_t vol_id;
23947ae6693SKyungmin Park } __attribute__ ((packed));
24047ae6693SKyungmin Park 
24147ae6693SKyungmin Park /**
24247ae6693SKyungmin Park  * struct ubi_leb_change_req - a data structure used in atomic logical
24347ae6693SKyungmin Park  *                             eraseblock change requests.
24447ae6693SKyungmin Park  * @lnum: logical eraseblock number to change
24547ae6693SKyungmin Park  * @bytes: how many bytes will be written to the logical eraseblock
24647ae6693SKyungmin Park  * @dtype: data type (%UBI_LONGTERM, %UBI_SHORTTERM, %UBI_UNKNOWN)
24747ae6693SKyungmin Park  * @padding: reserved for future, not used, has to be zeroed
24847ae6693SKyungmin Park  */
24947ae6693SKyungmin Park struct ubi_leb_change_req {
25047ae6693SKyungmin Park 	int32_t lnum;
25147ae6693SKyungmin Park 	int32_t bytes;
25247ae6693SKyungmin Park 	uint8_t dtype;
25347ae6693SKyungmin Park 	uint8_t padding[7];
25447ae6693SKyungmin Park } __attribute__ ((packed));
25547ae6693SKyungmin Park 
25647ae6693SKyungmin Park #endif /* __UBI_USER_H__ */
257