xref: /openbmc/linux/drivers/mtd/ubi/ubi-media.h (revision 4f2c0a4acffbec01079c28f839422e64ddeff004)
1f5a926ddSLionel Debieve /* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
292a74f1cSArtem Bityutskiy /*
3f5a926ddSLionel Debieve  * Copyright (C) International Business Machines Corp., 2006
492a74f1cSArtem Bityutskiy  * Authors: Artem Bityutskiy (Битюцкий Артём)
592a74f1cSArtem Bityutskiy  *          Thomas Gleixner
692a74f1cSArtem Bityutskiy  *          Frank Haverkamp
792a74f1cSArtem Bityutskiy  *          Oliver Lohmann
892a74f1cSArtem Bityutskiy  *          Andreas Arnez
9f5a926ddSLionel Debieve  *
1092a74f1cSArtem Bityutskiy  * This file defines the layout of UBI headers and all the other UBI on-flash
1192a74f1cSArtem Bityutskiy  * data structures.
1292a74f1cSArtem Bityutskiy  */
1392a74f1cSArtem Bityutskiy 
1492a74f1cSArtem Bityutskiy #ifndef __UBI_MEDIA_H__
1592a74f1cSArtem Bityutskiy #define __UBI_MEDIA_H__
1692a74f1cSArtem Bityutskiy 
1792a74f1cSArtem Bityutskiy #include <asm/byteorder.h>
1892a74f1cSArtem Bityutskiy 
1992a74f1cSArtem Bityutskiy /* The version of UBI images supported by this implementation */
2092a74f1cSArtem Bityutskiy #define UBI_VERSION 1
2192a74f1cSArtem Bityutskiy 
2292a74f1cSArtem Bityutskiy /* The highest erase counter value supported by this implementation */
2392a74f1cSArtem Bityutskiy #define UBI_MAX_ERASECOUNTER 0x7FFFFFFF
2492a74f1cSArtem Bityutskiy 
2592a74f1cSArtem Bityutskiy /* The initial CRC32 value used when calculating CRC checksums */
2692a74f1cSArtem Bityutskiy #define UBI_CRC32_INIT 0xFFFFFFFFU
2792a74f1cSArtem Bityutskiy 
2892a74f1cSArtem Bityutskiy /* Erase counter header magic number (ASCII "UBI#") */
2992a74f1cSArtem Bityutskiy #define UBI_EC_HDR_MAGIC  0x55424923
3092a74f1cSArtem Bityutskiy /* Volume identifier header magic number (ASCII "UBI!") */
3192a74f1cSArtem Bityutskiy #define UBI_VID_HDR_MAGIC 0x55424921
3292a74f1cSArtem Bityutskiy 
3392a74f1cSArtem Bityutskiy /*
3492a74f1cSArtem Bityutskiy  * Volume type constants used in the volume identifier header.
3592a74f1cSArtem Bityutskiy  *
3692a74f1cSArtem Bityutskiy  * @UBI_VID_DYNAMIC: dynamic volume
3792a74f1cSArtem Bityutskiy  * @UBI_VID_STATIC: static volume
3892a74f1cSArtem Bityutskiy  */
3992a74f1cSArtem Bityutskiy enum {
4092a74f1cSArtem Bityutskiy 	UBI_VID_DYNAMIC = 1,
4192a74f1cSArtem Bityutskiy 	UBI_VID_STATIC  = 2
4292a74f1cSArtem Bityutskiy };
4392a74f1cSArtem Bityutskiy 
4492a74f1cSArtem Bityutskiy /*
4592a74f1cSArtem Bityutskiy  * Volume flags used in the volume table record.
4692a74f1cSArtem Bityutskiy  *
4792a74f1cSArtem Bityutskiy  * @UBI_VTBL_AUTORESIZE_FLG: auto-resize this volume
4862652517SQuentin Schulz  * @UBI_VTBL_SKIP_CRC_CHECK_FLG: skip the CRC check done on a static volume at
4962652517SQuentin Schulz  *				 open time. Should only be set on volumes that
5062652517SQuentin Schulz  *				 are used by upper layers doing this kind of
5162652517SQuentin Schulz  *				 check. Main use-case for this flag is
5262652517SQuentin Schulz  *				 boot-time reduction
5392a74f1cSArtem Bityutskiy  *
5492a74f1cSArtem Bityutskiy  * %UBI_VTBL_AUTORESIZE_FLG flag can be set only for one volume in the volume
5592a74f1cSArtem Bityutskiy  * table. UBI automatically re-sizes the volume which has this flag and makes
5692a74f1cSArtem Bityutskiy  * the volume to be of largest possible size. This means that if after the
5792a74f1cSArtem Bityutskiy  * initialization UBI finds out that there are available physical eraseblocks
5892a74f1cSArtem Bityutskiy  * present on the device, it automatically appends all of them to the volume
5992a74f1cSArtem Bityutskiy  * (the physical eraseblocks reserved for bad eraseblocks handling and other
6092a74f1cSArtem Bityutskiy  * reserved physical eraseblocks are not taken). So, if there is a volume with
6192a74f1cSArtem Bityutskiy  * the %UBI_VTBL_AUTORESIZE_FLG flag set, the amount of available logical
6292a74f1cSArtem Bityutskiy  * eraseblocks will be zero after UBI is loaded, because all of them will be
6392a74f1cSArtem Bityutskiy  * reserved for this volume. Note, the %UBI_VTBL_AUTORESIZE_FLG bit is cleared
6492a74f1cSArtem Bityutskiy  * after the volume had been initialized.
6592a74f1cSArtem Bityutskiy  *
6692a74f1cSArtem Bityutskiy  * The auto-resize feature is useful for device production purposes. For
6792a74f1cSArtem Bityutskiy  * example, different NAND flash chips may have different amount of initial bad
6892a74f1cSArtem Bityutskiy  * eraseblocks, depending of particular chip instance. Manufacturers of NAND
6992a74f1cSArtem Bityutskiy  * chips usually guarantee that the amount of initial bad eraseblocks does not
7092a74f1cSArtem Bityutskiy  * exceed certain percent, e.g. 2%. When one creates an UBI image which will be
7192a74f1cSArtem Bityutskiy  * flashed to the end devices in production, he does not know the exact amount
7292a74f1cSArtem Bityutskiy  * of good physical eraseblocks the NAND chip on the device will have, but this
7392a74f1cSArtem Bityutskiy  * number is required to calculate the volume sized and put them to the volume
7492a74f1cSArtem Bityutskiy  * table of the UBI image. In this case, one of the volumes (e.g., the one
7592a74f1cSArtem Bityutskiy  * which will store the root file system) is marked as "auto-resizable", and
7692a74f1cSArtem Bityutskiy  * UBI will adjust its size on the first boot if needed.
7792a74f1cSArtem Bityutskiy  *
7892a74f1cSArtem Bityutskiy  * Note, first UBI reserves some amount of physical eraseblocks for bad
7992a74f1cSArtem Bityutskiy  * eraseblock handling, and then re-sizes the volume, not vice-versa. This
8092a74f1cSArtem Bityutskiy  * means that the pool of reserved physical eraseblocks will always be present.
8192a74f1cSArtem Bityutskiy  */
8292a74f1cSArtem Bityutskiy enum {
8392a74f1cSArtem Bityutskiy 	UBI_VTBL_AUTORESIZE_FLG = 0x01,
8462652517SQuentin Schulz 	UBI_VTBL_SKIP_CRC_CHECK_FLG = 0x02,
8592a74f1cSArtem Bityutskiy };
8692a74f1cSArtem Bityutskiy 
8792a74f1cSArtem Bityutskiy /*
8892a74f1cSArtem Bityutskiy  * Compatibility constants used by internal volumes.
8992a74f1cSArtem Bityutskiy  *
9092a74f1cSArtem Bityutskiy  * @UBI_COMPAT_DELETE: delete this internal volume before anything is written
9192a74f1cSArtem Bityutskiy  *                     to the flash
9292a74f1cSArtem Bityutskiy  * @UBI_COMPAT_RO: attach this device in read-only mode
9392a74f1cSArtem Bityutskiy  * @UBI_COMPAT_PRESERVE: preserve this internal volume - do not touch its
9485c6e6e2SArtem Bityutskiy  *                       physical eraseblocks, don't allow the wear-leveling
9585c6e6e2SArtem Bityutskiy  *                       sub-system to move them
9692a74f1cSArtem Bityutskiy  * @UBI_COMPAT_REJECT: reject this UBI image
9792a74f1cSArtem Bityutskiy  */
9892a74f1cSArtem Bityutskiy enum {
9992a74f1cSArtem Bityutskiy 	UBI_COMPAT_DELETE   = 1,
10092a74f1cSArtem Bityutskiy 	UBI_COMPAT_RO       = 2,
10192a74f1cSArtem Bityutskiy 	UBI_COMPAT_PRESERVE = 4,
10292a74f1cSArtem Bityutskiy 	UBI_COMPAT_REJECT   = 5
10392a74f1cSArtem Bityutskiy };
10492a74f1cSArtem Bityutskiy 
10592a74f1cSArtem Bityutskiy /* Sizes of UBI headers */
10692a74f1cSArtem Bityutskiy #define UBI_EC_HDR_SIZE  sizeof(struct ubi_ec_hdr)
10792a74f1cSArtem Bityutskiy #define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr)
10892a74f1cSArtem Bityutskiy 
10992a74f1cSArtem Bityutskiy /* Sizes of UBI headers without the ending CRC */
11092a74f1cSArtem Bityutskiy #define UBI_EC_HDR_SIZE_CRC  (UBI_EC_HDR_SIZE  - sizeof(__be32))
11192a74f1cSArtem Bityutskiy #define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(__be32))
11292a74f1cSArtem Bityutskiy 
11392a74f1cSArtem Bityutskiy /**
11492a74f1cSArtem Bityutskiy  * struct ubi_ec_hdr - UBI erase counter header.
11592a74f1cSArtem Bityutskiy  * @magic: erase counter header magic number (%UBI_EC_HDR_MAGIC)
11692a74f1cSArtem Bityutskiy  * @version: version of UBI implementation which is supposed to accept this
11792a74f1cSArtem Bityutskiy  *           UBI image
11892a74f1cSArtem Bityutskiy  * @padding1: reserved for future, zeroes
11992a74f1cSArtem Bityutskiy  * @ec: the erase counter
12092a74f1cSArtem Bityutskiy  * @vid_hdr_offset: where the VID header starts
12192a74f1cSArtem Bityutskiy  * @data_offset: where the user data start
1220c6c7fa1SAdrian Hunter  * @image_seq: image sequence number
12392a74f1cSArtem Bityutskiy  * @padding2: reserved for future, zeroes
12492a74f1cSArtem Bityutskiy  * @hdr_crc: erase counter header CRC checksum
12592a74f1cSArtem Bityutskiy  *
12692a74f1cSArtem Bityutskiy  * The erase counter header takes 64 bytes and has a plenty of unused space for
12792a74f1cSArtem Bityutskiy  * future usage. The unused fields are zeroed. The @version field is used to
12892a74f1cSArtem Bityutskiy  * indicate the version of UBI implementation which is supposed to be able to
129025dfdafSFrederik Schwarzer  * work with this UBI image. If @version is greater than the current UBI
13092a74f1cSArtem Bityutskiy  * version, the image is rejected. This may be useful in future if something
13192a74f1cSArtem Bityutskiy  * is changed radically. This field is duplicated in the volume identifier
13292a74f1cSArtem Bityutskiy  * header.
13392a74f1cSArtem Bityutskiy  *
134*818f9e83SJason Wang  * The @vid_hdr_offset and @data_offset fields contain the offset of the
13592a74f1cSArtem Bityutskiy  * volume identifier header and user data, relative to the beginning of the
13692a74f1cSArtem Bityutskiy  * physical eraseblock. These values have to be the same for all physical
13792a74f1cSArtem Bityutskiy  * eraseblocks.
1380c6c7fa1SAdrian Hunter  *
1390c6c7fa1SAdrian Hunter  * The @image_seq field is used to validate a UBI image that has been prepared
1400c6c7fa1SAdrian Hunter  * for a UBI device. The @image_seq value can be any value, but it must be the
1410c6c7fa1SAdrian Hunter  * same on all eraseblocks. UBI will ensure that all new erase counter headers
142fbd0107fSArtem Bityutskiy  * also contain this value, and will check the value when attaching the flash.
1430c6c7fa1SAdrian Hunter  * One way to make use of @image_seq is to increase its value by one every time
1440c6c7fa1SAdrian Hunter  * an image is flashed over an existing image, then, if the flashing does not
145fbd0107fSArtem Bityutskiy  * complete, UBI will detect the error when attaching the media.
14692a74f1cSArtem Bityutskiy  */
14792a74f1cSArtem Bityutskiy struct ubi_ec_hdr {
14892a74f1cSArtem Bityutskiy 	__be32  magic;
14992a74f1cSArtem Bityutskiy 	__u8    version;
15092a74f1cSArtem Bityutskiy 	__u8    padding1[3];
15192a74f1cSArtem Bityutskiy 	__be64  ec; /* Warning: the current limit is 31-bit anyway! */
15292a74f1cSArtem Bityutskiy 	__be32  vid_hdr_offset;
15392a74f1cSArtem Bityutskiy 	__be32  data_offset;
1540c6c7fa1SAdrian Hunter 	__be32  image_seq;
1550c6c7fa1SAdrian Hunter 	__u8    padding2[32];
15692a74f1cSArtem Bityutskiy 	__be32  hdr_crc;
1573627924aSArtem Bityutskiy } __packed;
15892a74f1cSArtem Bityutskiy 
15992a74f1cSArtem Bityutskiy /**
16092a74f1cSArtem Bityutskiy  * struct ubi_vid_hdr - on-flash UBI volume identifier header.
16192a74f1cSArtem Bityutskiy  * @magic: volume identifier header magic number (%UBI_VID_HDR_MAGIC)
16292a74f1cSArtem Bityutskiy  * @version: UBI implementation version which is supposed to accept this UBI
16392a74f1cSArtem Bityutskiy  *           image (%UBI_VERSION)
16492a74f1cSArtem Bityutskiy  * @vol_type: volume type (%UBI_VID_DYNAMIC or %UBI_VID_STATIC)
16592a74f1cSArtem Bityutskiy  * @copy_flag: if this logical eraseblock was copied from another physical
16692a74f1cSArtem Bityutskiy  *             eraseblock (for wear-leveling reasons)
16792a74f1cSArtem Bityutskiy  * @compat: compatibility of this volume (%0, %UBI_COMPAT_DELETE,
16892a74f1cSArtem Bityutskiy  *          %UBI_COMPAT_IGNORE, %UBI_COMPAT_PRESERVE, or %UBI_COMPAT_REJECT)
16992a74f1cSArtem Bityutskiy  * @vol_id: ID of this volume
17092a74f1cSArtem Bityutskiy  * @lnum: logical eraseblock number
1719869cd80SArtem Bityutskiy  * @padding1: reserved for future, zeroes
17292a74f1cSArtem Bityutskiy  * @data_size: how many bytes of data this logical eraseblock contains
17392a74f1cSArtem Bityutskiy  * @used_ebs: total number of used logical eraseblocks in this volume
17492a74f1cSArtem Bityutskiy  * @data_pad: how many bytes at the end of this physical eraseblock are not
17592a74f1cSArtem Bityutskiy  *            used
17692a74f1cSArtem Bityutskiy  * @data_crc: CRC checksum of the data stored in this logical eraseblock
17792a74f1cSArtem Bityutskiy  * @padding2: reserved for future, zeroes
1789869cd80SArtem Bityutskiy  * @sqnum: sequence number
1799869cd80SArtem Bityutskiy  * @padding3: reserved for future, zeroes
18092a74f1cSArtem Bityutskiy  * @hdr_crc: volume identifier header CRC checksum
18192a74f1cSArtem Bityutskiy  *
18292a74f1cSArtem Bityutskiy  * The @sqnum is the value of the global sequence counter at the time when this
18392a74f1cSArtem Bityutskiy  * VID header was created. The global sequence counter is incremented each time
18492a74f1cSArtem Bityutskiy  * UBI writes a new VID header to the flash, i.e. when it maps a logical
18592a74f1cSArtem Bityutskiy  * eraseblock to a new physical eraseblock. The global sequence counter is an
18692a74f1cSArtem Bityutskiy  * unsigned 64-bit integer and we assume it never overflows. The @sqnum
18792a74f1cSArtem Bityutskiy  * (sequence number) is used to distinguish between older and newer versions of
18892a74f1cSArtem Bityutskiy  * logical eraseblocks.
18992a74f1cSArtem Bityutskiy  *
190025dfdafSFrederik Schwarzer  * There are 2 situations when there may be more than one physical eraseblock
19192a74f1cSArtem Bityutskiy  * corresponding to the same logical eraseblock, i.e., having the same @vol_id
19292a74f1cSArtem Bityutskiy  * and @lnum values in the volume identifier header. Suppose we have a logical
19392a74f1cSArtem Bityutskiy  * eraseblock L and it is mapped to the physical eraseblock P.
19492a74f1cSArtem Bityutskiy  *
19592a74f1cSArtem Bityutskiy  * 1. Because UBI may erase physical eraseblocks asynchronously, the following
19692a74f1cSArtem Bityutskiy  * situation is possible: L is asynchronously erased, so P is scheduled for
19792a74f1cSArtem Bityutskiy  * erasure, then L is written to,i.e. mapped to another physical eraseblock P1,
19892a74f1cSArtem Bityutskiy  * so P1 is written to, then an unclean reboot happens. Result - there are 2
19992a74f1cSArtem Bityutskiy  * physical eraseblocks P and P1 corresponding to the same logical eraseblock
20092a74f1cSArtem Bityutskiy  * L. But P1 has greater sequence number, so UBI picks P1 when it attaches the
20192a74f1cSArtem Bityutskiy  * flash.
20292a74f1cSArtem Bityutskiy  *
20392a74f1cSArtem Bityutskiy  * 2. From time to time UBI moves logical eraseblocks to other physical
20492a74f1cSArtem Bityutskiy  * eraseblocks for wear-leveling reasons. If, for example, UBI moves L from P
20592a74f1cSArtem Bityutskiy  * to P1, and an unclean reboot happens before P is physically erased, there
20692a74f1cSArtem Bityutskiy  * are two physical eraseblocks P and P1 corresponding to L and UBI has to
20792a74f1cSArtem Bityutskiy  * select one of them when the flash is attached. The @sqnum field says which
20892a74f1cSArtem Bityutskiy  * PEB is the original (obviously P will have lower @sqnum) and the copy. But
20992a74f1cSArtem Bityutskiy  * it is not enough to select the physical eraseblock with the higher sequence
21092a74f1cSArtem Bityutskiy  * number, because the unclean reboot could have happen in the middle of the
21192a74f1cSArtem Bityutskiy  * copying process, so the data in P is corrupted. It is also not enough to
21292a74f1cSArtem Bityutskiy  * just select the physical eraseblock with lower sequence number, because the
21392a74f1cSArtem Bityutskiy  * data there may be old (consider a case if more data was added to P1 after
21492a74f1cSArtem Bityutskiy  * the copying). Moreover, the unclean reboot may happen when the erasure of P
21592a74f1cSArtem Bityutskiy  * was just started, so it result in unstable P, which is "mostly" OK, but
21692a74f1cSArtem Bityutskiy  * still has unstable bits.
21792a74f1cSArtem Bityutskiy  *
21892a74f1cSArtem Bityutskiy  * UBI uses the @copy_flag field to indicate that this logical eraseblock is a
21992a74f1cSArtem Bityutskiy  * copy. UBI also calculates data CRC when the data is moved and stores it at
22092a74f1cSArtem Bityutskiy  * the @data_crc field of the copy (P1). So when UBI needs to pick one physical
22192a74f1cSArtem Bityutskiy  * eraseblock of two (P or P1), the @copy_flag of the newer one (P1) is
22289b68cd9SUwe Kleine-König  * examined. If it is cleared, the situation is simple and the newer one is
22392a74f1cSArtem Bityutskiy  * picked. If it is set, the data CRC of the copy (P1) is examined. If the CRC
22492a74f1cSArtem Bityutskiy  * checksum is correct, this physical eraseblock is selected (P1). Otherwise
22592a74f1cSArtem Bityutskiy  * the older one (P) is selected.
22692a74f1cSArtem Bityutskiy  *
22792a74f1cSArtem Bityutskiy  * There are 2 sorts of volumes in UBI: user volumes and internal volumes.
22892a74f1cSArtem Bityutskiy  * Internal volumes are not seen from outside and are used for various internal
22992a74f1cSArtem Bityutskiy  * UBI purposes. In this implementation there is only one internal volume - the
23092a74f1cSArtem Bityutskiy  * layout volume. Internal volumes are the main mechanism of UBI extensions.
23192a74f1cSArtem Bityutskiy  * For example, in future one may introduce a journal internal volume. Internal
23292a74f1cSArtem Bityutskiy  * volumes have their own reserved range of IDs.
23392a74f1cSArtem Bityutskiy  *
23492a74f1cSArtem Bityutskiy  * The @compat field is only used for internal volumes and contains the "degree
23592a74f1cSArtem Bityutskiy  * of their compatibility". It is always zero for user volumes. This field
23692a74f1cSArtem Bityutskiy  * provides a mechanism to introduce UBI extensions and to be still compatible
23792a74f1cSArtem Bityutskiy  * with older UBI binaries. For example, if someone introduced a journal in
23892a74f1cSArtem Bityutskiy  * future, he would probably use %UBI_COMPAT_DELETE compatibility for the
23992a74f1cSArtem Bityutskiy  * journal volume.  And in this case, older UBI binaries, which know nothing
24092a74f1cSArtem Bityutskiy  * about the journal volume, would just delete this volume and work perfectly
24192a74f1cSArtem Bityutskiy  * fine. This is similar to what Ext2fs does when it is fed by an Ext3fs image
24292a74f1cSArtem Bityutskiy  * - it just ignores the Ext3fs journal.
24392a74f1cSArtem Bityutskiy  *
24492a74f1cSArtem Bityutskiy  * The @data_crc field contains the CRC checksum of the contents of the logical
24592a74f1cSArtem Bityutskiy  * eraseblock if this is a static volume. In case of dynamic volumes, it does
24692a74f1cSArtem Bityutskiy  * not contain the CRC checksum as a rule. The only exception is when the
24785c6e6e2SArtem Bityutskiy  * data of the physical eraseblock was moved by the wear-leveling sub-system,
24885c6e6e2SArtem Bityutskiy  * then the wear-leveling sub-system calculates the data CRC and stores it in
24985c6e6e2SArtem Bityutskiy  * the @data_crc field. And of course, the @copy_flag is %in this case.
25092a74f1cSArtem Bityutskiy  *
25192a74f1cSArtem Bityutskiy  * The @data_size field is used only for static volumes because UBI has to know
25292a74f1cSArtem Bityutskiy  * how many bytes of data are stored in this eraseblock. For dynamic volumes,
25392a74f1cSArtem Bityutskiy  * this field usually contains zero. The only exception is when the data of the
25492a74f1cSArtem Bityutskiy  * physical eraseblock was moved to another physical eraseblock for
25592a74f1cSArtem Bityutskiy  * wear-leveling reasons. In this case, UBI calculates CRC checksum of the
25692a74f1cSArtem Bityutskiy  * contents and uses both @data_crc and @data_size fields. In this case, the
25792a74f1cSArtem Bityutskiy  * @data_size field contains data size.
25892a74f1cSArtem Bityutskiy  *
25992a74f1cSArtem Bityutskiy  * The @used_ebs field is used only for static volumes and indicates how many
26092a74f1cSArtem Bityutskiy  * eraseblocks the data of the volume takes. For dynamic volumes this field is
26192a74f1cSArtem Bityutskiy  * not used and always contains zero.
26292a74f1cSArtem Bityutskiy  *
26392a74f1cSArtem Bityutskiy  * The @data_pad is calculated when volumes are created using the alignment
26492a74f1cSArtem Bityutskiy  * parameter. So, effectively, the @data_pad field reduces the size of logical
26592a74f1cSArtem Bityutskiy  * eraseblocks of this volume. This is very handy when one uses block-oriented
26692a74f1cSArtem Bityutskiy  * software (say, cramfs) on top of the UBI volume.
26792a74f1cSArtem Bityutskiy  */
26892a74f1cSArtem Bityutskiy struct ubi_vid_hdr {
26992a74f1cSArtem Bityutskiy 	__be32  magic;
27092a74f1cSArtem Bityutskiy 	__u8    version;
27192a74f1cSArtem Bityutskiy 	__u8    vol_type;
27292a74f1cSArtem Bityutskiy 	__u8    copy_flag;
27392a74f1cSArtem Bityutskiy 	__u8    compat;
27492a74f1cSArtem Bityutskiy 	__be32  vol_id;
27592a74f1cSArtem Bityutskiy 	__be32  lnum;
2769869cd80SArtem Bityutskiy 	__u8    padding1[4];
27792a74f1cSArtem Bityutskiy 	__be32  data_size;
27892a74f1cSArtem Bityutskiy 	__be32  used_ebs;
27992a74f1cSArtem Bityutskiy 	__be32  data_pad;
28092a74f1cSArtem Bityutskiy 	__be32  data_crc;
2819869cd80SArtem Bityutskiy 	__u8    padding2[4];
28292a74f1cSArtem Bityutskiy 	__be64  sqnum;
2839869cd80SArtem Bityutskiy 	__u8    padding3[12];
28492a74f1cSArtem Bityutskiy 	__be32  hdr_crc;
2853627924aSArtem Bityutskiy } __packed;
28692a74f1cSArtem Bityutskiy 
28792a74f1cSArtem Bityutskiy /* Internal UBI volumes count */
28892a74f1cSArtem Bityutskiy #define UBI_INT_VOL_COUNT 1
28992a74f1cSArtem Bityutskiy 
29092a74f1cSArtem Bityutskiy /*
2915cc09420SJoel Reardon  * Starting ID of internal volumes: 0x7fffefff.
2925cc09420SJoel Reardon  * There is reserved room for 4096 internal volumes.
29392a74f1cSArtem Bityutskiy  */
29492a74f1cSArtem Bityutskiy #define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096)
29592a74f1cSArtem Bityutskiy 
29692a74f1cSArtem Bityutskiy /* The layout volume contains the volume table */
29792a74f1cSArtem Bityutskiy 
29892a74f1cSArtem Bityutskiy #define UBI_LAYOUT_VOLUME_ID     UBI_INTERNAL_VOL_START
29992a74f1cSArtem Bityutskiy #define UBI_LAYOUT_VOLUME_TYPE   UBI_VID_DYNAMIC
30092a74f1cSArtem Bityutskiy #define UBI_LAYOUT_VOLUME_ALIGN  1
30192a74f1cSArtem Bityutskiy #define UBI_LAYOUT_VOLUME_EBS    2
30292a74f1cSArtem Bityutskiy #define UBI_LAYOUT_VOLUME_NAME   "layout volume"
30392a74f1cSArtem Bityutskiy #define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT
30492a74f1cSArtem Bityutskiy 
30592a74f1cSArtem Bityutskiy /* The maximum number of volumes per one UBI device */
30692a74f1cSArtem Bityutskiy #define UBI_MAX_VOLUMES 128
30792a74f1cSArtem Bityutskiy 
30892a74f1cSArtem Bityutskiy /* The maximum volume name length */
30992a74f1cSArtem Bityutskiy #define UBI_VOL_NAME_MAX 127
31092a74f1cSArtem Bityutskiy 
31192a74f1cSArtem Bityutskiy /* Size of the volume table record */
31292a74f1cSArtem Bityutskiy #define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vtbl_record)
31392a74f1cSArtem Bityutskiy 
31492a74f1cSArtem Bityutskiy /* Size of the volume table record without the ending CRC */
31592a74f1cSArtem Bityutskiy #define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(__be32))
31692a74f1cSArtem Bityutskiy 
31792a74f1cSArtem Bityutskiy /**
31892a74f1cSArtem Bityutskiy  * struct ubi_vtbl_record - a record in the volume table.
31992a74f1cSArtem Bityutskiy  * @reserved_pebs: how many physical eraseblocks are reserved for this volume
32092a74f1cSArtem Bityutskiy  * @alignment: volume alignment
32192a74f1cSArtem Bityutskiy  * @data_pad: how many bytes are unused at the end of the each physical
32292a74f1cSArtem Bityutskiy  * eraseblock to satisfy the requested alignment
32392a74f1cSArtem Bityutskiy  * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
32492a74f1cSArtem Bityutskiy  * @upd_marker: if volume update was started but not finished
32592a74f1cSArtem Bityutskiy  * @name_len: volume name length
32692a74f1cSArtem Bityutskiy  * @name: the volume name
32792a74f1cSArtem Bityutskiy  * @flags: volume flags (%UBI_VTBL_AUTORESIZE_FLG)
32892a74f1cSArtem Bityutskiy  * @padding: reserved, zeroes
32992a74f1cSArtem Bityutskiy  * @crc: a CRC32 checksum of the record
33092a74f1cSArtem Bityutskiy  *
33192a74f1cSArtem Bityutskiy  * The volume table records are stored in the volume table, which is stored in
33292a74f1cSArtem Bityutskiy  * the layout volume. The layout volume consists of 2 logical eraseblock, each
33392a74f1cSArtem Bityutskiy  * of which contains a copy of the volume table (i.e., the volume table is
33492a74f1cSArtem Bityutskiy  * duplicated). The volume table is an array of &struct ubi_vtbl_record
33592a74f1cSArtem Bityutskiy  * objects indexed by the volume ID.
33692a74f1cSArtem Bityutskiy  *
33792a74f1cSArtem Bityutskiy  * If the size of the logical eraseblock is large enough to fit
33892a74f1cSArtem Bityutskiy  * %UBI_MAX_VOLUMES records, the volume table contains %UBI_MAX_VOLUMES
33992a74f1cSArtem Bityutskiy  * records. Otherwise, it contains as many records as it can fit (i.e., size of
34092a74f1cSArtem Bityutskiy  * logical eraseblock divided by sizeof(struct ubi_vtbl_record)).
34192a74f1cSArtem Bityutskiy  *
34292a74f1cSArtem Bityutskiy  * The @upd_marker flag is used to implement volume update. It is set to %1
34392a74f1cSArtem Bityutskiy  * before update and set to %0 after the update. So if the update operation was
34492a74f1cSArtem Bityutskiy  * interrupted, UBI knows that the volume is corrupted.
34592a74f1cSArtem Bityutskiy  *
34692a74f1cSArtem Bityutskiy  * The @alignment field is specified when the volume is created and cannot be
34792a74f1cSArtem Bityutskiy  * later changed. It may be useful, for example, when a block-oriented file
34892a74f1cSArtem Bityutskiy  * system works on top of UBI. The @data_pad field is calculated using the
34992a74f1cSArtem Bityutskiy  * logical eraseblock size and @alignment. The alignment must be multiple to the
35092a74f1cSArtem Bityutskiy  * minimal flash I/O unit. If @alignment is 1, all the available space of
35192a74f1cSArtem Bityutskiy  * the physical eraseblocks is used.
35292a74f1cSArtem Bityutskiy  *
35392a74f1cSArtem Bityutskiy  * Empty records contain all zeroes and the CRC checksum of those zeroes.
35492a74f1cSArtem Bityutskiy  */
35592a74f1cSArtem Bityutskiy struct ubi_vtbl_record {
35692a74f1cSArtem Bityutskiy 	__be32  reserved_pebs;
35792a74f1cSArtem Bityutskiy 	__be32  alignment;
35892a74f1cSArtem Bityutskiy 	__be32  data_pad;
35992a74f1cSArtem Bityutskiy 	__u8    vol_type;
36092a74f1cSArtem Bityutskiy 	__u8    upd_marker;
36192a74f1cSArtem Bityutskiy 	__be16  name_len;
36292a74f1cSArtem Bityutskiy 	__u8    name[UBI_VOL_NAME_MAX+1];
36392a74f1cSArtem Bityutskiy 	__u8    flags;
36492a74f1cSArtem Bityutskiy 	__u8    padding[23];
36592a74f1cSArtem Bityutskiy 	__be32  crc;
3663627924aSArtem Bityutskiy } __packed;
36792a74f1cSArtem Bityutskiy 
3681c865749SRichard Weinberger /* UBI fastmap on-flash data structures */
3691c865749SRichard Weinberger 
3701c865749SRichard Weinberger #define UBI_FM_SB_VOLUME_ID	(UBI_LAYOUT_VOLUME_ID + 1)
3711c865749SRichard Weinberger #define UBI_FM_DATA_VOLUME_ID	(UBI_LAYOUT_VOLUME_ID + 2)
3721c865749SRichard Weinberger 
3731c865749SRichard Weinberger /* fastmap on-flash data structure format version */
3741c865749SRichard Weinberger #define UBI_FM_FMT_VERSION	1
3751c865749SRichard Weinberger 
3761c865749SRichard Weinberger #define UBI_FM_SB_MAGIC		0x7B11D69F
3771c865749SRichard Weinberger #define UBI_FM_HDR_MAGIC	0xD4B82EF7
3781c865749SRichard Weinberger #define UBI_FM_VHDR_MAGIC	0xFA370ED1
3791c865749SRichard Weinberger #define UBI_FM_POOL_MAGIC	0x67AF4D08
3801c865749SRichard Weinberger #define UBI_FM_EBA_MAGIC	0xf0c040a8
3811c865749SRichard Weinberger 
38289b68cd9SUwe Kleine-König /* A fastmap super block can be located between PEB 0 and
3831c865749SRichard Weinberger  * UBI_FM_MAX_START */
3841c865749SRichard Weinberger #define UBI_FM_MAX_START	64
3851c865749SRichard Weinberger 
3861c865749SRichard Weinberger /* A fastmap can use up to UBI_FM_MAX_BLOCKS PEBs */
3871c865749SRichard Weinberger #define UBI_FM_MAX_BLOCKS	32
3881c865749SRichard Weinberger 
3891c865749SRichard Weinberger /* 5% of the total number of PEBs have to be scanned while attaching
3901c865749SRichard Weinberger  * from a fastmap.
3911c865749SRichard Weinberger  * But the size of this pool is limited to be between UBI_FM_MIN_POOL_SIZE and
3921c865749SRichard Weinberger  * UBI_FM_MAX_POOL_SIZE */
3931c865749SRichard Weinberger #define UBI_FM_MIN_POOL_SIZE	8
3941c865749SRichard Weinberger #define UBI_FM_MAX_POOL_SIZE	256
3951c865749SRichard Weinberger 
3961c865749SRichard Weinberger /**
3971c865749SRichard Weinberger  * struct ubi_fm_sb - UBI fastmap super block
3981c865749SRichard Weinberger  * @magic: fastmap super block magic number (%UBI_FM_SB_MAGIC)
3991c865749SRichard Weinberger  * @version: format version of this fastmap
4001c865749SRichard Weinberger  * @data_crc: CRC over the fastmap data
4011c865749SRichard Weinberger  * @used_blocks: number of PEBs used by this fastmap
4021c865749SRichard Weinberger  * @block_loc: an array containing the location of all PEBs of the fastmap
4031c865749SRichard Weinberger  * @block_ec: the erase counter of each used PEB
4041c865749SRichard Weinberger  * @sqnum: highest sequence number value at the time while taking the fastmap
4051c865749SRichard Weinberger  *
4061c865749SRichard Weinberger  */
4071c865749SRichard Weinberger struct ubi_fm_sb {
4081c865749SRichard Weinberger 	__be32 magic;
4091c865749SRichard Weinberger 	__u8 version;
4101c865749SRichard Weinberger 	__u8 padding1[3];
4111c865749SRichard Weinberger 	__be32 data_crc;
4121c865749SRichard Weinberger 	__be32 used_blocks;
4131c865749SRichard Weinberger 	__be32 block_loc[UBI_FM_MAX_BLOCKS];
4141c865749SRichard Weinberger 	__be32 block_ec[UBI_FM_MAX_BLOCKS];
4151c865749SRichard Weinberger 	__be64 sqnum;
4161c865749SRichard Weinberger 	__u8 padding2[32];
4171c865749SRichard Weinberger } __packed;
4181c865749SRichard Weinberger 
4191c865749SRichard Weinberger /**
4201c865749SRichard Weinberger  * struct ubi_fm_hdr - header of the fastmap data set
4211c865749SRichard Weinberger  * @magic: fastmap header magic number (%UBI_FM_HDR_MAGIC)
4221c865749SRichard Weinberger  * @free_peb_count: number of free PEBs known by this fastmap
4231c865749SRichard Weinberger  * @used_peb_count: number of used PEBs known by this fastmap
4241c865749SRichard Weinberger  * @scrub_peb_count: number of to be scrubbed PEBs known by this fastmap
4251c865749SRichard Weinberger  * @bad_peb_count: number of bad PEBs known by this fastmap
4261c865749SRichard Weinberger  * @erase_peb_count: number of bad PEBs which have to be erased
4271c865749SRichard Weinberger  * @vol_count: number of UBI volumes known by this fastmap
4281c865749SRichard Weinberger  */
4291c865749SRichard Weinberger struct ubi_fm_hdr {
4301c865749SRichard Weinberger 	__be32 magic;
4311c865749SRichard Weinberger 	__be32 free_peb_count;
4321c865749SRichard Weinberger 	__be32 used_peb_count;
4331c865749SRichard Weinberger 	__be32 scrub_peb_count;
4341c865749SRichard Weinberger 	__be32 bad_peb_count;
4351c865749SRichard Weinberger 	__be32 erase_peb_count;
4361c865749SRichard Weinberger 	__be32 vol_count;
4371c865749SRichard Weinberger 	__u8 padding[4];
4381c865749SRichard Weinberger } __packed;
4391c865749SRichard Weinberger 
4401c865749SRichard Weinberger /* struct ubi_fm_hdr is followed by two struct ubi_fm_scan_pool */
4411c865749SRichard Weinberger 
4421c865749SRichard Weinberger /**
4431c865749SRichard Weinberger  * struct ubi_fm_scan_pool - Fastmap pool PEBs to be scanned while attaching
4441c865749SRichard Weinberger  * @magic: pool magic numer (%UBI_FM_POOL_MAGIC)
4451c865749SRichard Weinberger  * @size: current pool size
4461c865749SRichard Weinberger  * @max_size: maximal pool size
4471c865749SRichard Weinberger  * @pebs: an array containing the location of all PEBs in this pool
4481c865749SRichard Weinberger  */
4491c865749SRichard Weinberger struct ubi_fm_scan_pool {
4501c865749SRichard Weinberger 	__be32 magic;
4511c865749SRichard Weinberger 	__be16 size;
4521c865749SRichard Weinberger 	__be16 max_size;
4531c865749SRichard Weinberger 	__be32 pebs[UBI_FM_MAX_POOL_SIZE];
4541c865749SRichard Weinberger 	__be32 padding[4];
4551c865749SRichard Weinberger } __packed;
4561c865749SRichard Weinberger 
4571c865749SRichard Weinberger /* ubi_fm_scan_pool is followed by nfree+nused struct ubi_fm_ec records */
4581c865749SRichard Weinberger 
4591c865749SRichard Weinberger /**
4601c865749SRichard Weinberger  * struct ubi_fm_ec - stores the erase counter of a PEB
4611c865749SRichard Weinberger  * @pnum: PEB number
4621c865749SRichard Weinberger  * @ec: ec of this PEB
4631c865749SRichard Weinberger  */
4641c865749SRichard Weinberger struct ubi_fm_ec {
4651c865749SRichard Weinberger 	__be32 pnum;
4661c865749SRichard Weinberger 	__be32 ec;
4671c865749SRichard Weinberger } __packed;
4681c865749SRichard Weinberger 
4691c865749SRichard Weinberger /**
4701c865749SRichard Weinberger  * struct ubi_fm_volhdr - Fastmap volume header
4711c865749SRichard Weinberger  * it identifies the start of an eba table
4721c865749SRichard Weinberger  * @magic: Fastmap volume header magic number (%UBI_FM_VHDR_MAGIC)
4731c865749SRichard Weinberger  * @vol_id: volume id of the fastmapped volume
4741c865749SRichard Weinberger  * @vol_type: type of the fastmapped volume
4751c865749SRichard Weinberger  * @data_pad: data_pad value of the fastmapped volume
4761c865749SRichard Weinberger  * @used_ebs: number of used LEBs within this volume
4771c865749SRichard Weinberger  * @last_eb_bytes: number of bytes used in the last LEB
4781c865749SRichard Weinberger  */
4791c865749SRichard Weinberger struct ubi_fm_volhdr {
4801c865749SRichard Weinberger 	__be32 magic;
4811c865749SRichard Weinberger 	__be32 vol_id;
4821c865749SRichard Weinberger 	__u8 vol_type;
4831c865749SRichard Weinberger 	__u8 padding1[3];
4841c865749SRichard Weinberger 	__be32 data_pad;
4851c865749SRichard Weinberger 	__be32 used_ebs;
4861c865749SRichard Weinberger 	__be32 last_eb_bytes;
4871c865749SRichard Weinberger 	__u8 padding2[8];
4881c865749SRichard Weinberger } __packed;
4891c865749SRichard Weinberger 
4901c865749SRichard Weinberger /* struct ubi_fm_volhdr is followed by one struct ubi_fm_eba records */
4911c865749SRichard Weinberger 
4921c865749SRichard Weinberger /**
4934ebb4c9dSRichard Weinberger  * struct ubi_fm_eba - denotes an association between a PEB and LEB
4941c865749SRichard Weinberger  * @magic: EBA table magic number
4951c865749SRichard Weinberger  * @reserved_pebs: number of table entries
4961c865749SRichard Weinberger  * @pnum: PEB number of LEB (LEB is the index)
4971c865749SRichard Weinberger  */
4981c865749SRichard Weinberger struct ubi_fm_eba {
4991c865749SRichard Weinberger 	__be32 magic;
5001c865749SRichard Weinberger 	__be32 reserved_pebs;
5013676f32aSGustavo A. R. Silva 	__be32 pnum[];
5021c865749SRichard Weinberger } __packed;
50392a74f1cSArtem Bityutskiy #endif /* !__UBI_MEDIA_H__ */
504