xref: /rk3399_rockchip-uboot/drivers/mtd/ubi/ubi-media.h (revision e573bdb324c78fac56655a493bea843842c9d9f8)
1f412fefaSKyungmin Park /*
2f412fefaSKyungmin Park  * Copyright (c) International Business Machines Corp., 2006
3f412fefaSKyungmin Park  *
41a459660SWolfgang Denk  * SPDX-License-Identifier:	GPL-2.0+
5f412fefaSKyungmin Park  *
6f412fefaSKyungmin Park  * Authors: Artem Bityutskiy (Битюцкий Артём)
7f412fefaSKyungmin Park  *          Thomas Gleixner
8f412fefaSKyungmin Park  *          Frank Haverkamp
9f412fefaSKyungmin Park  *          Oliver Lohmann
10f412fefaSKyungmin Park  *          Andreas Arnez
11f412fefaSKyungmin Park  */
12f412fefaSKyungmin Park 
13f412fefaSKyungmin Park /*
14f412fefaSKyungmin Park  * This file defines the layout of UBI headers and all the other UBI on-flash
15f412fefaSKyungmin Park  * data structures.
16f412fefaSKyungmin Park  */
17f412fefaSKyungmin Park 
18f412fefaSKyungmin Park #ifndef __UBI_MEDIA_H__
19f412fefaSKyungmin Park #define __UBI_MEDIA_H__
20f412fefaSKyungmin Park 
21f412fefaSKyungmin Park #include <asm/byteorder.h>
22f412fefaSKyungmin Park 
23f412fefaSKyungmin Park /* The version of UBI images supported by this implementation */
24f412fefaSKyungmin Park #define UBI_VERSION 1
25f412fefaSKyungmin Park 
26f412fefaSKyungmin Park /* The highest erase counter value supported by this implementation */
27f412fefaSKyungmin Park #define UBI_MAX_ERASECOUNTER 0x7FFFFFFF
28f412fefaSKyungmin Park 
29f412fefaSKyungmin Park /* The initial CRC32 value used when calculating CRC checksums */
30f412fefaSKyungmin Park #define UBI_CRC32_INIT 0xFFFFFFFFU
31f412fefaSKyungmin Park 
32f412fefaSKyungmin Park /* Erase counter header magic number (ASCII "UBI#") */
33f412fefaSKyungmin Park #define UBI_EC_HDR_MAGIC  0x55424923
34f412fefaSKyungmin Park /* Volume identifier header magic number (ASCII "UBI!") */
35f412fefaSKyungmin Park #define UBI_VID_HDR_MAGIC 0x55424921
36f412fefaSKyungmin Park 
37f412fefaSKyungmin Park /*
38f412fefaSKyungmin Park  * Volume type constants used in the volume identifier header.
39f412fefaSKyungmin Park  *
40f412fefaSKyungmin Park  * @UBI_VID_DYNAMIC: dynamic volume
41f412fefaSKyungmin Park  * @UBI_VID_STATIC: static volume
42f412fefaSKyungmin Park  */
43f412fefaSKyungmin Park enum {
44f412fefaSKyungmin Park 	UBI_VID_DYNAMIC = 1,
45f412fefaSKyungmin Park 	UBI_VID_STATIC  = 2
46f412fefaSKyungmin Park };
47f412fefaSKyungmin Park 
48f412fefaSKyungmin Park /*
49f412fefaSKyungmin Park  * Volume flags used in the volume table record.
50f412fefaSKyungmin Park  *
51f412fefaSKyungmin Park  * @UBI_VTBL_AUTORESIZE_FLG: auto-resize this volume
52f412fefaSKyungmin Park  *
53f412fefaSKyungmin Park  * %UBI_VTBL_AUTORESIZE_FLG flag can be set only for one volume in the volume
54f412fefaSKyungmin Park  * table. UBI automatically re-sizes the volume which has this flag and makes
55f412fefaSKyungmin Park  * the volume to be of largest possible size. This means that if after the
56f412fefaSKyungmin Park  * initialization UBI finds out that there are available physical eraseblocks
57f412fefaSKyungmin Park  * present on the device, it automatically appends all of them to the volume
58f412fefaSKyungmin Park  * (the physical eraseblocks reserved for bad eraseblocks handling and other
59f412fefaSKyungmin Park  * reserved physical eraseblocks are not taken). So, if there is a volume with
60f412fefaSKyungmin Park  * the %UBI_VTBL_AUTORESIZE_FLG flag set, the amount of available logical
61f412fefaSKyungmin Park  * eraseblocks will be zero after UBI is loaded, because all of them will be
62f412fefaSKyungmin Park  * reserved for this volume. Note, the %UBI_VTBL_AUTORESIZE_FLG bit is cleared
63f412fefaSKyungmin Park  * after the volume had been initialized.
64f412fefaSKyungmin Park  *
65f412fefaSKyungmin Park  * The auto-resize feature is useful for device production purposes. For
66f412fefaSKyungmin Park  * example, different NAND flash chips may have different amount of initial bad
67f412fefaSKyungmin Park  * eraseblocks, depending of particular chip instance. Manufacturers of NAND
68f412fefaSKyungmin Park  * chips usually guarantee that the amount of initial bad eraseblocks does not
69f412fefaSKyungmin Park  * exceed certain percent, e.g. 2%. When one creates an UBI image which will be
70f412fefaSKyungmin Park  * flashed to the end devices in production, he does not know the exact amount
71f412fefaSKyungmin Park  * of good physical eraseblocks the NAND chip on the device will have, but this
72f412fefaSKyungmin Park  * number is required to calculate the volume sized and put them to the volume
73f412fefaSKyungmin Park  * table of the UBI image. In this case, one of the volumes (e.g., the one
74f412fefaSKyungmin Park  * which will store the root file system) is marked as "auto-resizable", and
75f412fefaSKyungmin Park  * UBI will adjust its size on the first boot if needed.
76f412fefaSKyungmin Park  *
77f412fefaSKyungmin Park  * Note, first UBI reserves some amount of physical eraseblocks for bad
78f412fefaSKyungmin Park  * eraseblock handling, and then re-sizes the volume, not vice-versa. This
79f412fefaSKyungmin Park  * means that the pool of reserved physical eraseblocks will always be present.
80f412fefaSKyungmin Park  */
81f412fefaSKyungmin Park enum {
82f412fefaSKyungmin Park 	UBI_VTBL_AUTORESIZE_FLG = 0x01,
83f412fefaSKyungmin Park };
84f412fefaSKyungmin Park 
85f412fefaSKyungmin Park /*
86f412fefaSKyungmin Park  * Compatibility constants used by internal volumes.
87f412fefaSKyungmin Park  *
88f412fefaSKyungmin Park  * @UBI_COMPAT_DELETE: delete this internal volume before anything is written
89f412fefaSKyungmin Park  *                     to the flash
90f412fefaSKyungmin Park  * @UBI_COMPAT_RO: attach this device in read-only mode
91f412fefaSKyungmin Park  * @UBI_COMPAT_PRESERVE: preserve this internal volume - do not touch its
92*ff94bc40SHeiko Schocher  *                       physical eraseblocks, don't allow the wear-leveling
93*ff94bc40SHeiko Schocher  *                       sub-system to move them
94f412fefaSKyungmin Park  * @UBI_COMPAT_REJECT: reject this UBI image
95f412fefaSKyungmin Park  */
96f412fefaSKyungmin Park enum {
97f412fefaSKyungmin Park 	UBI_COMPAT_DELETE   = 1,
98f412fefaSKyungmin Park 	UBI_COMPAT_RO       = 2,
99f412fefaSKyungmin Park 	UBI_COMPAT_PRESERVE = 4,
100f412fefaSKyungmin Park 	UBI_COMPAT_REJECT   = 5
101f412fefaSKyungmin Park };
102f412fefaSKyungmin Park 
103f412fefaSKyungmin Park /* Sizes of UBI headers */
104f412fefaSKyungmin Park #define UBI_EC_HDR_SIZE  sizeof(struct ubi_ec_hdr)
105f412fefaSKyungmin Park #define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr)
106f412fefaSKyungmin Park 
107f412fefaSKyungmin Park /* Sizes of UBI headers without the ending CRC */
108f412fefaSKyungmin Park #define UBI_EC_HDR_SIZE_CRC  (UBI_EC_HDR_SIZE  - sizeof(__be32))
109f412fefaSKyungmin Park #define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(__be32))
110f412fefaSKyungmin Park 
111f412fefaSKyungmin Park /**
112f412fefaSKyungmin Park  * struct ubi_ec_hdr - UBI erase counter header.
113f412fefaSKyungmin Park  * @magic: erase counter header magic number (%UBI_EC_HDR_MAGIC)
114f412fefaSKyungmin Park  * @version: version of UBI implementation which is supposed to accept this
115f412fefaSKyungmin Park  *           UBI image
116f412fefaSKyungmin Park  * @padding1: reserved for future, zeroes
117f412fefaSKyungmin Park  * @ec: the erase counter
118f412fefaSKyungmin Park  * @vid_hdr_offset: where the VID header starts
119f412fefaSKyungmin Park  * @data_offset: where the user data start
120*ff94bc40SHeiko Schocher  * @image_seq: image sequence number
121f412fefaSKyungmin Park  * @padding2: reserved for future, zeroes
122f412fefaSKyungmin Park  * @hdr_crc: erase counter header CRC checksum
123f412fefaSKyungmin Park  *
124f412fefaSKyungmin Park  * The erase counter header takes 64 bytes and has a plenty of unused space for
125f412fefaSKyungmin Park  * future usage. The unused fields are zeroed. The @version field is used to
126f412fefaSKyungmin Park  * indicate the version of UBI implementation which is supposed to be able to
127*ff94bc40SHeiko Schocher  * work with this UBI image. If @version is greater than the current UBI
128f412fefaSKyungmin Park  * version, the image is rejected. This may be useful in future if something
129f412fefaSKyungmin Park  * is changed radically. This field is duplicated in the volume identifier
130f412fefaSKyungmin Park  * header.
131f412fefaSKyungmin Park  *
132f412fefaSKyungmin Park  * The @vid_hdr_offset and @data_offset fields contain the offset of the the
133f412fefaSKyungmin Park  * volume identifier header and user data, relative to the beginning of the
134f412fefaSKyungmin Park  * physical eraseblock. These values have to be the same for all physical
135f412fefaSKyungmin Park  * eraseblocks.
136*ff94bc40SHeiko Schocher  *
137*ff94bc40SHeiko Schocher  * The @image_seq field is used to validate a UBI image that has been prepared
138*ff94bc40SHeiko Schocher  * for a UBI device. The @image_seq value can be any value, but it must be the
139*ff94bc40SHeiko Schocher  * same on all eraseblocks. UBI will ensure that all new erase counter headers
140*ff94bc40SHeiko Schocher  * also contain this value, and will check the value when attaching the flash.
141*ff94bc40SHeiko Schocher  * One way to make use of @image_seq is to increase its value by one every time
142*ff94bc40SHeiko Schocher  * an image is flashed over an existing image, then, if the flashing does not
143*ff94bc40SHeiko Schocher  * complete, UBI will detect the error when attaching the media.
144f412fefaSKyungmin Park  */
145f412fefaSKyungmin Park struct ubi_ec_hdr {
146f412fefaSKyungmin Park 	__be32  magic;
147f412fefaSKyungmin Park 	__u8    version;
148f412fefaSKyungmin Park 	__u8    padding1[3];
149f412fefaSKyungmin Park 	__be64  ec; /* Warning: the current limit is 31-bit anyway! */
150f412fefaSKyungmin Park 	__be32  vid_hdr_offset;
151f412fefaSKyungmin Park 	__be32  data_offset;
152*ff94bc40SHeiko Schocher 	__be32  image_seq;
153*ff94bc40SHeiko Schocher 	__u8    padding2[32];
154f412fefaSKyungmin Park 	__be32  hdr_crc;
155*ff94bc40SHeiko Schocher } __packed;
156f412fefaSKyungmin Park 
157f412fefaSKyungmin Park /**
158f412fefaSKyungmin Park  * struct ubi_vid_hdr - on-flash UBI volume identifier header.
159f412fefaSKyungmin Park  * @magic: volume identifier header magic number (%UBI_VID_HDR_MAGIC)
160f412fefaSKyungmin Park  * @version: UBI implementation version which is supposed to accept this UBI
161f412fefaSKyungmin Park  *           image (%UBI_VERSION)
162f412fefaSKyungmin Park  * @vol_type: volume type (%UBI_VID_DYNAMIC or %UBI_VID_STATIC)
163f412fefaSKyungmin Park  * @copy_flag: if this logical eraseblock was copied from another physical
164f412fefaSKyungmin Park  *             eraseblock (for wear-leveling reasons)
165f412fefaSKyungmin Park  * @compat: compatibility of this volume (%0, %UBI_COMPAT_DELETE,
166f412fefaSKyungmin Park  *          %UBI_COMPAT_IGNORE, %UBI_COMPAT_PRESERVE, or %UBI_COMPAT_REJECT)
167f412fefaSKyungmin Park  * @vol_id: ID of this volume
168f412fefaSKyungmin Park  * @lnum: logical eraseblock number
169*ff94bc40SHeiko Schocher  * @padding1: reserved for future, zeroes
170f412fefaSKyungmin Park  * @data_size: how many bytes of data this logical eraseblock contains
171f412fefaSKyungmin Park  * @used_ebs: total number of used logical eraseblocks in this volume
172f412fefaSKyungmin Park  * @data_pad: how many bytes at the end of this physical eraseblock are not
173f412fefaSKyungmin Park  *            used
174f412fefaSKyungmin Park  * @data_crc: CRC checksum of the data stored in this logical eraseblock
175f412fefaSKyungmin Park  * @padding2: reserved for future, zeroes
176*ff94bc40SHeiko Schocher  * @sqnum: sequence number
177*ff94bc40SHeiko Schocher  * @padding3: reserved for future, zeroes
178f412fefaSKyungmin Park  * @hdr_crc: volume identifier header CRC checksum
179f412fefaSKyungmin Park  *
180f412fefaSKyungmin Park  * The @sqnum is the value of the global sequence counter at the time when this
181f412fefaSKyungmin Park  * VID header was created. The global sequence counter is incremented each time
182f412fefaSKyungmin Park  * UBI writes a new VID header to the flash, i.e. when it maps a logical
183f412fefaSKyungmin Park  * eraseblock to a new physical eraseblock. The global sequence counter is an
184f412fefaSKyungmin Park  * unsigned 64-bit integer and we assume it never overflows. The @sqnum
185f412fefaSKyungmin Park  * (sequence number) is used to distinguish between older and newer versions of
186f412fefaSKyungmin Park  * logical eraseblocks.
187f412fefaSKyungmin Park  *
188*ff94bc40SHeiko Schocher  * There are 2 situations when there may be more than one physical eraseblock
189f412fefaSKyungmin Park  * corresponding to the same logical eraseblock, i.e., having the same @vol_id
190f412fefaSKyungmin Park  * and @lnum values in the volume identifier header. Suppose we have a logical
191f412fefaSKyungmin Park  * eraseblock L and it is mapped to the physical eraseblock P.
192f412fefaSKyungmin Park  *
193f412fefaSKyungmin Park  * 1. Because UBI may erase physical eraseblocks asynchronously, the following
194f412fefaSKyungmin Park  * situation is possible: L is asynchronously erased, so P is scheduled for
195f412fefaSKyungmin Park  * erasure, then L is written to,i.e. mapped to another physical eraseblock P1,
196f412fefaSKyungmin Park  * so P1 is written to, then an unclean reboot happens. Result - there are 2
197f412fefaSKyungmin Park  * physical eraseblocks P and P1 corresponding to the same logical eraseblock
198f412fefaSKyungmin Park  * L. But P1 has greater sequence number, so UBI picks P1 when it attaches the
199f412fefaSKyungmin Park  * flash.
200f412fefaSKyungmin Park  *
201f412fefaSKyungmin Park  * 2. From time to time UBI moves logical eraseblocks to other physical
202f412fefaSKyungmin Park  * eraseblocks for wear-leveling reasons. If, for example, UBI moves L from P
203f412fefaSKyungmin Park  * to P1, and an unclean reboot happens before P is physically erased, there
204f412fefaSKyungmin Park  * are two physical eraseblocks P and P1 corresponding to L and UBI has to
205f412fefaSKyungmin Park  * select one of them when the flash is attached. The @sqnum field says which
206f412fefaSKyungmin Park  * PEB is the original (obviously P will have lower @sqnum) and the copy. But
207f412fefaSKyungmin Park  * it is not enough to select the physical eraseblock with the higher sequence
208f412fefaSKyungmin Park  * number, because the unclean reboot could have happen in the middle of the
209f412fefaSKyungmin Park  * copying process, so the data in P is corrupted. It is also not enough to
210f412fefaSKyungmin Park  * just select the physical eraseblock with lower sequence number, because the
211f412fefaSKyungmin Park  * data there may be old (consider a case if more data was added to P1 after
212f412fefaSKyungmin Park  * the copying). Moreover, the unclean reboot may happen when the erasure of P
213f412fefaSKyungmin Park  * was just started, so it result in unstable P, which is "mostly" OK, but
214f412fefaSKyungmin Park  * still has unstable bits.
215f412fefaSKyungmin Park  *
216f412fefaSKyungmin Park  * UBI uses the @copy_flag field to indicate that this logical eraseblock is a
217f412fefaSKyungmin Park  * copy. UBI also calculates data CRC when the data is moved and stores it at
218f412fefaSKyungmin Park  * the @data_crc field of the copy (P1). So when UBI needs to pick one physical
219f412fefaSKyungmin Park  * eraseblock of two (P or P1), the @copy_flag of the newer one (P1) is
220f412fefaSKyungmin Park  * examined. If it is cleared, the situation* is simple and the newer one is
221f412fefaSKyungmin Park  * picked. If it is set, the data CRC of the copy (P1) is examined. If the CRC
222f412fefaSKyungmin Park  * checksum is correct, this physical eraseblock is selected (P1). Otherwise
223f412fefaSKyungmin Park  * the older one (P) is selected.
224f412fefaSKyungmin Park  *
225f412fefaSKyungmin Park  * There are 2 sorts of volumes in UBI: user volumes and internal volumes.
226f412fefaSKyungmin Park  * Internal volumes are not seen from outside and are used for various internal
227f412fefaSKyungmin Park  * UBI purposes. In this implementation there is only one internal volume - the
228f412fefaSKyungmin Park  * layout volume. Internal volumes are the main mechanism of UBI extensions.
229f412fefaSKyungmin Park  * For example, in future one may introduce a journal internal volume. Internal
230f412fefaSKyungmin Park  * volumes have their own reserved range of IDs.
231f412fefaSKyungmin Park  *
232f412fefaSKyungmin Park  * The @compat field is only used for internal volumes and contains the "degree
233f412fefaSKyungmin Park  * of their compatibility". It is always zero for user volumes. This field
234f412fefaSKyungmin Park  * provides a mechanism to introduce UBI extensions and to be still compatible
235f412fefaSKyungmin Park  * with older UBI binaries. For example, if someone introduced a journal in
236f412fefaSKyungmin Park  * future, he would probably use %UBI_COMPAT_DELETE compatibility for the
237f412fefaSKyungmin Park  * journal volume.  And in this case, older UBI binaries, which know nothing
238f412fefaSKyungmin Park  * about the journal volume, would just delete this volume and work perfectly
239f412fefaSKyungmin Park  * fine. This is similar to what Ext2fs does when it is fed by an Ext3fs image
240f412fefaSKyungmin Park  * - it just ignores the Ext3fs journal.
241f412fefaSKyungmin Park  *
242f412fefaSKyungmin Park  * The @data_crc field contains the CRC checksum of the contents of the logical
243f412fefaSKyungmin Park  * eraseblock if this is a static volume. In case of dynamic volumes, it does
244f412fefaSKyungmin Park  * not contain the CRC checksum as a rule. The only exception is when the
245*ff94bc40SHeiko Schocher  * data of the physical eraseblock was moved by the wear-leveling sub-system,
246*ff94bc40SHeiko Schocher  * then the wear-leveling sub-system calculates the data CRC and stores it in
247*ff94bc40SHeiko Schocher  * the @data_crc field. And of course, the @copy_flag is %in this case.
248f412fefaSKyungmin Park  *
249f412fefaSKyungmin Park  * The @data_size field is used only for static volumes because UBI has to know
250f412fefaSKyungmin Park  * how many bytes of data are stored in this eraseblock. For dynamic volumes,
251f412fefaSKyungmin Park  * this field usually contains zero. The only exception is when the data of the
252f412fefaSKyungmin Park  * physical eraseblock was moved to another physical eraseblock for
253f412fefaSKyungmin Park  * wear-leveling reasons. In this case, UBI calculates CRC checksum of the
254f412fefaSKyungmin Park  * contents and uses both @data_crc and @data_size fields. In this case, the
255f412fefaSKyungmin Park  * @data_size field contains data size.
256f412fefaSKyungmin Park  *
257f412fefaSKyungmin Park  * The @used_ebs field is used only for static volumes and indicates how many
258f412fefaSKyungmin Park  * eraseblocks the data of the volume takes. For dynamic volumes this field is
259f412fefaSKyungmin Park  * not used and always contains zero.
260f412fefaSKyungmin Park  *
261f412fefaSKyungmin Park  * The @data_pad is calculated when volumes are created using the alignment
262f412fefaSKyungmin Park  * parameter. So, effectively, the @data_pad field reduces the size of logical
263f412fefaSKyungmin Park  * eraseblocks of this volume. This is very handy when one uses block-oriented
264f412fefaSKyungmin Park  * software (say, cramfs) on top of the UBI volume.
265f412fefaSKyungmin Park  */
266f412fefaSKyungmin Park struct ubi_vid_hdr {
267f412fefaSKyungmin Park 	__be32  magic;
268f412fefaSKyungmin Park 	__u8    version;
269f412fefaSKyungmin Park 	__u8    vol_type;
270f412fefaSKyungmin Park 	__u8    copy_flag;
271f412fefaSKyungmin Park 	__u8    compat;
272f412fefaSKyungmin Park 	__be32  vol_id;
273f412fefaSKyungmin Park 	__be32  lnum;
274*ff94bc40SHeiko Schocher 	__u8    padding1[4];
275f412fefaSKyungmin Park 	__be32  data_size;
276f412fefaSKyungmin Park 	__be32  used_ebs;
277f412fefaSKyungmin Park 	__be32  data_pad;
278f412fefaSKyungmin Park 	__be32  data_crc;
279*ff94bc40SHeiko Schocher 	__u8    padding2[4];
280f412fefaSKyungmin Park 	__be64  sqnum;
281*ff94bc40SHeiko Schocher 	__u8    padding3[12];
282f412fefaSKyungmin Park 	__be32  hdr_crc;
283*ff94bc40SHeiko Schocher } __packed;
284f412fefaSKyungmin Park 
285f412fefaSKyungmin Park /* Internal UBI volumes count */
286f412fefaSKyungmin Park #define UBI_INT_VOL_COUNT 1
287f412fefaSKyungmin Park 
288f412fefaSKyungmin Park /*
289*ff94bc40SHeiko Schocher  * Starting ID of internal volumes: 0x7fffefff.
290*ff94bc40SHeiko Schocher  * There is reserved room for 4096 internal volumes.
291f412fefaSKyungmin Park  */
292f412fefaSKyungmin Park #define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096)
293f412fefaSKyungmin Park 
294f412fefaSKyungmin Park /* The layout volume contains the volume table */
295f412fefaSKyungmin Park 
296f412fefaSKyungmin Park #define UBI_LAYOUT_VOLUME_ID     UBI_INTERNAL_VOL_START
297f412fefaSKyungmin Park #define UBI_LAYOUT_VOLUME_TYPE   UBI_VID_DYNAMIC
298f412fefaSKyungmin Park #define UBI_LAYOUT_VOLUME_ALIGN  1
299f412fefaSKyungmin Park #define UBI_LAYOUT_VOLUME_EBS    2
300f412fefaSKyungmin Park #define UBI_LAYOUT_VOLUME_NAME   "layout volume"
301f412fefaSKyungmin Park #define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT
302f412fefaSKyungmin Park 
303f412fefaSKyungmin Park /* The maximum number of volumes per one UBI device */
304f412fefaSKyungmin Park #define UBI_MAX_VOLUMES 128
305f412fefaSKyungmin Park 
306f412fefaSKyungmin Park /* The maximum volume name length */
307f412fefaSKyungmin Park #define UBI_VOL_NAME_MAX 127
308f412fefaSKyungmin Park 
309f412fefaSKyungmin Park /* Size of the volume table record */
310f412fefaSKyungmin Park #define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vtbl_record)
311f412fefaSKyungmin Park 
312f412fefaSKyungmin Park /* Size of the volume table record without the ending CRC */
313f412fefaSKyungmin Park #define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(__be32))
314f412fefaSKyungmin Park 
315f412fefaSKyungmin Park /**
316f412fefaSKyungmin Park  * struct ubi_vtbl_record - a record in the volume table.
317f412fefaSKyungmin Park  * @reserved_pebs: how many physical eraseblocks are reserved for this volume
318f412fefaSKyungmin Park  * @alignment: volume alignment
319f412fefaSKyungmin Park  * @data_pad: how many bytes are unused at the end of the each physical
320f412fefaSKyungmin Park  * eraseblock to satisfy the requested alignment
321f412fefaSKyungmin Park  * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
322f412fefaSKyungmin Park  * @upd_marker: if volume update was started but not finished
323f412fefaSKyungmin Park  * @name_len: volume name length
324f412fefaSKyungmin Park  * @name: the volume name
325f412fefaSKyungmin Park  * @flags: volume flags (%UBI_VTBL_AUTORESIZE_FLG)
326f412fefaSKyungmin Park  * @padding: reserved, zeroes
327f412fefaSKyungmin Park  * @crc: a CRC32 checksum of the record
328f412fefaSKyungmin Park  *
329f412fefaSKyungmin Park  * The volume table records are stored in the volume table, which is stored in
330f412fefaSKyungmin Park  * the layout volume. The layout volume consists of 2 logical eraseblock, each
331f412fefaSKyungmin Park  * of which contains a copy of the volume table (i.e., the volume table is
332f412fefaSKyungmin Park  * duplicated). The volume table is an array of &struct ubi_vtbl_record
333f412fefaSKyungmin Park  * objects indexed by the volume ID.
334f412fefaSKyungmin Park  *
335f412fefaSKyungmin Park  * If the size of the logical eraseblock is large enough to fit
336f412fefaSKyungmin Park  * %UBI_MAX_VOLUMES records, the volume table contains %UBI_MAX_VOLUMES
337f412fefaSKyungmin Park  * records. Otherwise, it contains as many records as it can fit (i.e., size of
338f412fefaSKyungmin Park  * logical eraseblock divided by sizeof(struct ubi_vtbl_record)).
339f412fefaSKyungmin Park  *
340f412fefaSKyungmin Park  * The @upd_marker flag is used to implement volume update. It is set to %1
341f412fefaSKyungmin Park  * before update and set to %0 after the update. So if the update operation was
342f412fefaSKyungmin Park  * interrupted, UBI knows that the volume is corrupted.
343f412fefaSKyungmin Park  *
344f412fefaSKyungmin Park  * The @alignment field is specified when the volume is created and cannot be
345f412fefaSKyungmin Park  * later changed. It may be useful, for example, when a block-oriented file
346f412fefaSKyungmin Park  * system works on top of UBI. The @data_pad field is calculated using the
347f412fefaSKyungmin Park  * logical eraseblock size and @alignment. The alignment must be multiple to the
348f412fefaSKyungmin Park  * minimal flash I/O unit. If @alignment is 1, all the available space of
349f412fefaSKyungmin Park  * the physical eraseblocks is used.
350f412fefaSKyungmin Park  *
351f412fefaSKyungmin Park  * Empty records contain all zeroes and the CRC checksum of those zeroes.
352f412fefaSKyungmin Park  */
353f412fefaSKyungmin Park struct ubi_vtbl_record {
354f412fefaSKyungmin Park 	__be32  reserved_pebs;
355f412fefaSKyungmin Park 	__be32  alignment;
356f412fefaSKyungmin Park 	__be32  data_pad;
357f412fefaSKyungmin Park 	__u8    vol_type;
358f412fefaSKyungmin Park 	__u8    upd_marker;
359f412fefaSKyungmin Park 	__be16  name_len;
360*ff94bc40SHeiko Schocher #ifndef __UBOOT__
361f412fefaSKyungmin Park 	__u8    name[UBI_VOL_NAME_MAX+1];
362*ff94bc40SHeiko Schocher #else
363*ff94bc40SHeiko Schocher 	char    name[UBI_VOL_NAME_MAX+1];
364*ff94bc40SHeiko Schocher #endif
365f412fefaSKyungmin Park 	__u8    flags;
366f412fefaSKyungmin Park 	__u8    padding[23];
367f412fefaSKyungmin Park 	__be32  crc;
368*ff94bc40SHeiko Schocher } __packed;
369f412fefaSKyungmin Park 
370*ff94bc40SHeiko Schocher /* UBI fastmap on-flash data structures */
371*ff94bc40SHeiko Schocher 
372*ff94bc40SHeiko Schocher #define UBI_FM_SB_VOLUME_ID	(UBI_LAYOUT_VOLUME_ID + 1)
373*ff94bc40SHeiko Schocher #define UBI_FM_DATA_VOLUME_ID	(UBI_LAYOUT_VOLUME_ID + 2)
374*ff94bc40SHeiko Schocher 
375*ff94bc40SHeiko Schocher /* fastmap on-flash data structure format version */
376*ff94bc40SHeiko Schocher #define UBI_FM_FMT_VERSION	1
377*ff94bc40SHeiko Schocher 
378*ff94bc40SHeiko Schocher #define UBI_FM_SB_MAGIC		0x7B11D69F
379*ff94bc40SHeiko Schocher #define UBI_FM_HDR_MAGIC	0xD4B82EF7
380*ff94bc40SHeiko Schocher #define UBI_FM_VHDR_MAGIC	0xFA370ED1
381*ff94bc40SHeiko Schocher #define UBI_FM_POOL_MAGIC	0x67AF4D08
382*ff94bc40SHeiko Schocher #define UBI_FM_EBA_MAGIC	0xf0c040a8
383*ff94bc40SHeiko Schocher 
384*ff94bc40SHeiko Schocher /* A fastmap supber block can be located between PEB 0 and
385*ff94bc40SHeiko Schocher  * UBI_FM_MAX_START */
386*ff94bc40SHeiko Schocher #define UBI_FM_MAX_START	64
387*ff94bc40SHeiko Schocher 
388*ff94bc40SHeiko Schocher /* A fastmap can use up to UBI_FM_MAX_BLOCKS PEBs */
389*ff94bc40SHeiko Schocher #define UBI_FM_MAX_BLOCKS	32
390*ff94bc40SHeiko Schocher 
391*ff94bc40SHeiko Schocher /* 5% of the total number of PEBs have to be scanned while attaching
392*ff94bc40SHeiko Schocher  * from a fastmap.
393*ff94bc40SHeiko Schocher  * But the size of this pool is limited to be between UBI_FM_MIN_POOL_SIZE and
394*ff94bc40SHeiko Schocher  * UBI_FM_MAX_POOL_SIZE */
395*ff94bc40SHeiko Schocher #define UBI_FM_MIN_POOL_SIZE	8
396*ff94bc40SHeiko Schocher #define UBI_FM_MAX_POOL_SIZE	256
397*ff94bc40SHeiko Schocher 
398*ff94bc40SHeiko Schocher /**
399*ff94bc40SHeiko Schocher  * struct ubi_fm_sb - UBI fastmap super block
400*ff94bc40SHeiko Schocher  * @magic: fastmap super block magic number (%UBI_FM_SB_MAGIC)
401*ff94bc40SHeiko Schocher  * @version: format version of this fastmap
402*ff94bc40SHeiko Schocher  * @data_crc: CRC over the fastmap data
403*ff94bc40SHeiko Schocher  * @used_blocks: number of PEBs used by this fastmap
404*ff94bc40SHeiko Schocher  * @block_loc: an array containing the location of all PEBs of the fastmap
405*ff94bc40SHeiko Schocher  * @block_ec: the erase counter of each used PEB
406*ff94bc40SHeiko Schocher  * @sqnum: highest sequence number value at the time while taking the fastmap
407*ff94bc40SHeiko Schocher  *
408*ff94bc40SHeiko Schocher  */
409*ff94bc40SHeiko Schocher struct ubi_fm_sb {
410*ff94bc40SHeiko Schocher 	__be32 magic;
411*ff94bc40SHeiko Schocher 	__u8 version;
412*ff94bc40SHeiko Schocher 	__u8 padding1[3];
413*ff94bc40SHeiko Schocher 	__be32 data_crc;
414*ff94bc40SHeiko Schocher 	__be32 used_blocks;
415*ff94bc40SHeiko Schocher 	__be32 block_loc[UBI_FM_MAX_BLOCKS];
416*ff94bc40SHeiko Schocher 	__be32 block_ec[UBI_FM_MAX_BLOCKS];
417*ff94bc40SHeiko Schocher 	__be64 sqnum;
418*ff94bc40SHeiko Schocher 	__u8 padding2[32];
419*ff94bc40SHeiko Schocher } __packed;
420*ff94bc40SHeiko Schocher 
421*ff94bc40SHeiko Schocher /**
422*ff94bc40SHeiko Schocher  * struct ubi_fm_hdr - header of the fastmap data set
423*ff94bc40SHeiko Schocher  * @magic: fastmap header magic number (%UBI_FM_HDR_MAGIC)
424*ff94bc40SHeiko Schocher  * @free_peb_count: number of free PEBs known by this fastmap
425*ff94bc40SHeiko Schocher  * @used_peb_count: number of used PEBs known by this fastmap
426*ff94bc40SHeiko Schocher  * @scrub_peb_count: number of to be scrubbed PEBs known by this fastmap
427*ff94bc40SHeiko Schocher  * @bad_peb_count: number of bad PEBs known by this fastmap
428*ff94bc40SHeiko Schocher  * @erase_peb_count: number of bad PEBs which have to be erased
429*ff94bc40SHeiko Schocher  * @vol_count: number of UBI volumes known by this fastmap
430*ff94bc40SHeiko Schocher  */
431*ff94bc40SHeiko Schocher struct ubi_fm_hdr {
432*ff94bc40SHeiko Schocher 	__be32 magic;
433*ff94bc40SHeiko Schocher 	__be32 free_peb_count;
434*ff94bc40SHeiko Schocher 	__be32 used_peb_count;
435*ff94bc40SHeiko Schocher 	__be32 scrub_peb_count;
436*ff94bc40SHeiko Schocher 	__be32 bad_peb_count;
437*ff94bc40SHeiko Schocher 	__be32 erase_peb_count;
438*ff94bc40SHeiko Schocher 	__be32 vol_count;
439*ff94bc40SHeiko Schocher 	__u8 padding[4];
440*ff94bc40SHeiko Schocher } __packed;
441*ff94bc40SHeiko Schocher 
442*ff94bc40SHeiko Schocher /* struct ubi_fm_hdr is followed by two struct ubi_fm_scan_pool */
443*ff94bc40SHeiko Schocher 
444*ff94bc40SHeiko Schocher /**
445*ff94bc40SHeiko Schocher  * struct ubi_fm_scan_pool - Fastmap pool PEBs to be scanned while attaching
446*ff94bc40SHeiko Schocher  * @magic: pool magic numer (%UBI_FM_POOL_MAGIC)
447*ff94bc40SHeiko Schocher  * @size: current pool size
448*ff94bc40SHeiko Schocher  * @max_size: maximal pool size
449*ff94bc40SHeiko Schocher  * @pebs: an array containing the location of all PEBs in this pool
450*ff94bc40SHeiko Schocher  */
451*ff94bc40SHeiko Schocher struct ubi_fm_scan_pool {
452*ff94bc40SHeiko Schocher 	__be32 magic;
453*ff94bc40SHeiko Schocher 	__be16 size;
454*ff94bc40SHeiko Schocher 	__be16 max_size;
455*ff94bc40SHeiko Schocher 	__be32 pebs[UBI_FM_MAX_POOL_SIZE];
456*ff94bc40SHeiko Schocher 	__be32 padding[4];
457*ff94bc40SHeiko Schocher } __packed;
458*ff94bc40SHeiko Schocher 
459*ff94bc40SHeiko Schocher /* ubi_fm_scan_pool is followed by nfree+nused struct ubi_fm_ec records */
460*ff94bc40SHeiko Schocher 
461*ff94bc40SHeiko Schocher /**
462*ff94bc40SHeiko Schocher  * struct ubi_fm_ec - stores the erase counter of a PEB
463*ff94bc40SHeiko Schocher  * @pnum: PEB number
464*ff94bc40SHeiko Schocher  * @ec: ec of this PEB
465*ff94bc40SHeiko Schocher  */
466*ff94bc40SHeiko Schocher struct ubi_fm_ec {
467*ff94bc40SHeiko Schocher 	__be32 pnum;
468*ff94bc40SHeiko Schocher 	__be32 ec;
469*ff94bc40SHeiko Schocher } __packed;
470*ff94bc40SHeiko Schocher 
471*ff94bc40SHeiko Schocher /**
472*ff94bc40SHeiko Schocher  * struct ubi_fm_volhdr - Fastmap volume header
473*ff94bc40SHeiko Schocher  * it identifies the start of an eba table
474*ff94bc40SHeiko Schocher  * @magic: Fastmap volume header magic number (%UBI_FM_VHDR_MAGIC)
475*ff94bc40SHeiko Schocher  * @vol_id: volume id of the fastmapped volume
476*ff94bc40SHeiko Schocher  * @vol_type: type of the fastmapped volume
477*ff94bc40SHeiko Schocher  * @data_pad: data_pad value of the fastmapped volume
478*ff94bc40SHeiko Schocher  * @used_ebs: number of used LEBs within this volume
479*ff94bc40SHeiko Schocher  * @last_eb_bytes: number of bytes used in the last LEB
480*ff94bc40SHeiko Schocher  */
481*ff94bc40SHeiko Schocher struct ubi_fm_volhdr {
482*ff94bc40SHeiko Schocher 	__be32 magic;
483*ff94bc40SHeiko Schocher 	__be32 vol_id;
484*ff94bc40SHeiko Schocher 	__u8 vol_type;
485*ff94bc40SHeiko Schocher 	__u8 padding1[3];
486*ff94bc40SHeiko Schocher 	__be32 data_pad;
487*ff94bc40SHeiko Schocher 	__be32 used_ebs;
488*ff94bc40SHeiko Schocher 	__be32 last_eb_bytes;
489*ff94bc40SHeiko Schocher 	__u8 padding2[8];
490*ff94bc40SHeiko Schocher } __packed;
491*ff94bc40SHeiko Schocher 
492*ff94bc40SHeiko Schocher /* struct ubi_fm_volhdr is followed by one struct ubi_fm_eba records */
493*ff94bc40SHeiko Schocher 
494*ff94bc40SHeiko Schocher /**
495*ff94bc40SHeiko Schocher  * struct ubi_fm_eba - denotes an association beween a PEB and LEB
496*ff94bc40SHeiko Schocher  * @magic: EBA table magic number
497*ff94bc40SHeiko Schocher  * @reserved_pebs: number of table entries
498*ff94bc40SHeiko Schocher  * @pnum: PEB number of LEB (LEB is the index)
499*ff94bc40SHeiko Schocher  */
500*ff94bc40SHeiko Schocher struct ubi_fm_eba {
501*ff94bc40SHeiko Schocher 	__be32 magic;
502*ff94bc40SHeiko Schocher 	__be32 reserved_pebs;
503*ff94bc40SHeiko Schocher 	__be32 pnum[0];
504*ff94bc40SHeiko Schocher } __packed;
505f412fefaSKyungmin Park #endif /* !__UBI_MEDIA_H__ */
506