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