blob: 74efa77634790f6ab0ad4c1395f543f13211c304 [file] [log] [blame]
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +04001/*
2 * Copyright (c) International Business Machines Corp., 2006
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
12 * the GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 * Authors: Artem Bityutskiy (Битюцкий Артём)
19 * Thomas Gleixner
20 * Frank Haverkamp
21 * Oliver Lohmann
22 * Andreas Arnez
23 */
24
25/*
26 * This file defines the layout of UBI headers and all the other UBI on-flash
27 * data structures. May be included by user-space.
28 */
29
30#ifndef __UBI_HEADER_H__
31#define __UBI_HEADER_H__
32
33#include <asm/byteorder.h>
34
35/* The version of UBI images supported by this implementation */
36#define UBI_VERSION 1
37
38/* The highest erase counter value supported by this implementation */
39#define UBI_MAX_ERASECOUNTER 0x7FFFFFFF
40
41/* The initial CRC32 value used when calculating CRC checksums */
42#define UBI_CRC32_INIT 0xFFFFFFFFU
43
44/* Erase counter header magic number (ASCII "UBI#") */
45#define UBI_EC_HDR_MAGIC 0x55424923
46/* Volume identifier header magic number (ASCII "UBI!") */
47#define UBI_VID_HDR_MAGIC 0x55424921
48
49/*
50 * Volume type constants used in the volume identifier header.
51 *
52 * @UBI_VID_DYNAMIC: dynamic volume
53 * @UBI_VID_STATIC: static volume
54 */
55enum {
56 UBI_VID_DYNAMIC = 1,
57 UBI_VID_STATIC = 2
58};
59
60/*
61 * Compatibility constants used by internal volumes.
62 *
63 * @UBI_COMPAT_DELETE: delete this internal volume before anything is written
64 * to the flash
65 * @UBI_COMPAT_RO: attach this device in read-only mode
66 * @UBI_COMPAT_PRESERVE: preserve this internal volume - do not touch its
67 * physical eraseblocks, don't allow the wear-leveling unit to move them
68 * @UBI_COMPAT_REJECT: reject this UBI image
69 */
70enum {
71 UBI_COMPAT_DELETE = 1,
72 UBI_COMPAT_RO = 2,
73 UBI_COMPAT_PRESERVE = 4,
74 UBI_COMPAT_REJECT = 5
75};
76
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040077/* Sizes of UBI headers */
78#define UBI_EC_HDR_SIZE sizeof(struct ubi_ec_hdr)
79#define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr)
80
81/* Sizes of UBI headers without the ending CRC */
Christoph Hellwig3261ebd2007-05-21 17:41:46 +030082#define UBI_EC_HDR_SIZE_CRC (UBI_EC_HDR_SIZE - sizeof(__be32))
83#define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(__be32))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +040084
85/**
86 * struct ubi_ec_hdr - UBI erase counter header.
87 * @magic: erase counter header magic number (%UBI_EC_HDR_MAGIC)
88 * @version: version of UBI implementation which is supposed to accept this
89 * UBI image
90 * @padding1: reserved for future, zeroes
91 * @ec: the erase counter
92 * @vid_hdr_offset: where the VID header starts
93 * @data_offset: where the user data start
94 * @padding2: reserved for future, zeroes
95 * @hdr_crc: erase counter header CRC checksum
96 *
97 * The erase counter header takes 64 bytes and has a plenty of unused space for
98 * future usage. The unused fields are zeroed. The @version field is used to
99 * indicate the version of UBI implementation which is supposed to be able to
100 * work with this UBI image. If @version is greater then the current UBI
101 * version, the image is rejected. This may be useful in future if something
102 * is changed radically. This field is duplicated in the volume identifier
103 * header.
104 *
105 * The @vid_hdr_offset and @data_offset fields contain the offset of the the
106 * volume identifier header and user data, relative to the beginning of the
107 * physical eraseblock. These values have to be the same for all physical
108 * eraseblocks.
109 */
110struct ubi_ec_hdr {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300111 __be32 magic;
112 __u8 version;
113 __u8 padding1[3];
114 __be64 ec; /* Warning: the current limit is 31-bit anyway! */
115 __be32 vid_hdr_offset;
116 __be32 data_offset;
117 __u8 padding2[36];
118 __be32 hdr_crc;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400119} __attribute__ ((packed));
120
121/**
122 * struct ubi_vid_hdr - on-flash UBI volume identifier header.
123 * @magic: volume identifier header magic number (%UBI_VID_HDR_MAGIC)
124 * @version: UBI implementation version which is supposed to accept this UBI
125 * image (%UBI_VERSION)
126 * @vol_type: volume type (%UBI_VID_DYNAMIC or %UBI_VID_STATIC)
127 * @copy_flag: if this logical eraseblock was copied from another physical
128 * eraseblock (for wear-leveling reasons)
129 * @compat: compatibility of this volume (%0, %UBI_COMPAT_DELETE,
130 * %UBI_COMPAT_IGNORE, %UBI_COMPAT_PRESERVE, or %UBI_COMPAT_REJECT)
131 * @vol_id: ID of this volume
132 * @lnum: logical eraseblock number
133 * @leb_ver: version of this logical eraseblock (IMPORTANT: obsolete, to be
134 * removed, kept only for not breaking older UBI users)
135 * @data_size: how many bytes of data this logical eraseblock contains
136 * @used_ebs: total number of used logical eraseblocks in this volume
137 * @data_pad: how many bytes at the end of this physical eraseblock are not
138 * used
139 * @data_crc: CRC checksum of the data stored in this logical eraseblock
140 * @padding1: reserved for future, zeroes
141 * @sqnum: sequence number
142 * @padding2: reserved for future, zeroes
143 * @hdr_crc: volume identifier header CRC checksum
144 *
145 * The @sqnum is the value of the global sequence counter at the time when this
146 * VID header was created. The global sequence counter is incremented each time
147 * UBI writes a new VID header to the flash, i.e. when it maps a logical
148 * eraseblock to a new physical eraseblock. The global sequence counter is an
149 * unsigned 64-bit integer and we assume it never overflows. The @sqnum
150 * (sequence number) is used to distinguish between older and newer versions of
151 * logical eraseblocks.
152 *
153 * There are 2 situations when there may be more then one physical eraseblock
154 * corresponding to the same logical eraseblock, i.e., having the same @vol_id
155 * and @lnum values in the volume identifier header. Suppose we have a logical
156 * eraseblock L and it is mapped to the physical eraseblock P.
157 *
158 * 1. Because UBI may erase physical eraseblocks asynchronously, the following
159 * situation is possible: L is asynchronously erased, so P is scheduled for
160 * erasure, then L is written to,i.e. mapped to another physical eraseblock P1,
161 * so P1 is written to, then an unclean reboot happens. Result - there are 2
162 * physical eraseblocks P and P1 corresponding to the same logical eraseblock
163 * L. But P1 has greater sequence number, so UBI picks P1 when it attaches the
164 * flash.
165 *
166 * 2. From time to time UBI moves logical eraseblocks to other physical
167 * eraseblocks for wear-leveling reasons. If, for example, UBI moves L from P
168 * to P1, and an unclean reboot happens before P is physically erased, there
169 * are two physical eraseblocks P and P1 corresponding to L and UBI has to
170 * select one of them when the flash is attached. The @sqnum field says which
171 * PEB is the original (obviously P will have lower @sqnum) and the copy. But
172 * it is not enough to select the physical eraseblock with the higher sequence
173 * number, because the unclean reboot could have happen in the middle of the
174 * copying process, so the data in P is corrupted. It is also not enough to
175 * just select the physical eraseblock with lower sequence number, because the
176 * data there may be old (consider a case if more data was added to P1 after
177 * the copying). Moreover, the unclean reboot may happen when the erasure of P
178 * was just started, so it result in unstable P, which is "mostly" OK, but
179 * still has unstable bits.
180 *
181 * UBI uses the @copy_flag field to indicate that this logical eraseblock is a
182 * copy. UBI also calculates data CRC when the data is moved and stores it at
183 * the @data_crc field of the copy (P1). So when UBI needs to pick one physical
184 * eraseblock of two (P or P1), the @copy_flag of the newer one (P1) is
185 * examined. If it is cleared, the situation* is simple and the newer one is
186 * picked. If it is set, the data CRC of the copy (P1) is examined. If the CRC
187 * checksum is correct, this physical eraseblock is selected (P1). Otherwise
188 * the older one (P) is selected.
189 *
190 * Note, there is an obsolete @leb_ver field which was used instead of @sqnum
191 * in the past. But it is not used anymore and we keep it in order to be able
192 * to deal with old UBI images. It will be removed at some point.
193 *
194 * There are 2 sorts of volumes in UBI: user volumes and internal volumes.
195 * Internal volumes are not seen from outside and are used for various internal
196 * UBI purposes. In this implementation there is only one internal volume - the
197 * layout volume. Internal volumes are the main mechanism of UBI extensions.
198 * For example, in future one may introduce a journal internal volume. Internal
199 * volumes have their own reserved range of IDs.
200 *
201 * The @compat field is only used for internal volumes and contains the "degree
202 * of their compatibility". It is always zero for user volumes. This field
203 * provides a mechanism to introduce UBI extensions and to be still compatible
204 * with older UBI binaries. For example, if someone introduced a journal in
205 * future, he would probably use %UBI_COMPAT_DELETE compatibility for the
206 * journal volume. And in this case, older UBI binaries, which know nothing
207 * about the journal volume, would just delete this volume and work perfectly
208 * fine. This is similar to what Ext2fs does when it is fed by an Ext3fs image
209 * - it just ignores the Ext3fs journal.
210 *
211 * The @data_crc field contains the CRC checksum of the contents of the logical
212 * eraseblock if this is a static volume. In case of dynamic volumes, it does
213 * not contain the CRC checksum as a rule. The only exception is when the
214 * data of the physical eraseblock was moved by the wear-leveling unit, then
215 * the wear-leveling unit calculates the data CRC and stores it in the
216 * @data_crc field. And of course, the @copy_flag is %in this case.
217 *
218 * The @data_size field is used only for static volumes because UBI has to know
219 * how many bytes of data are stored in this eraseblock. For dynamic volumes,
220 * this field usually contains zero. The only exception is when the data of the
221 * physical eraseblock was moved to another physical eraseblock for
222 * wear-leveling reasons. In this case, UBI calculates CRC checksum of the
223 * contents and uses both @data_crc and @data_size fields. In this case, the
224 * @data_size field contains data size.
225 *
226 * The @used_ebs field is used only for static volumes and indicates how many
227 * eraseblocks the data of the volume takes. For dynamic volumes this field is
228 * not used and always contains zero.
229 *
230 * The @data_pad is calculated when volumes are created using the alignment
231 * parameter. So, effectively, the @data_pad field reduces the size of logical
232 * eraseblocks of this volume. This is very handy when one uses block-oriented
233 * software (say, cramfs) on top of the UBI volume.
234 */
235struct ubi_vid_hdr {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300236 __be32 magic;
237 __u8 version;
238 __u8 vol_type;
239 __u8 copy_flag;
240 __u8 compat;
241 __be32 vol_id;
242 __be32 lnum;
243 __be32 leb_ver; /* obsolete, to be removed, don't use */
244 __be32 data_size;
245 __be32 used_ebs;
246 __be32 data_pad;
247 __be32 data_crc;
248 __u8 padding1[4];
249 __be64 sqnum;
250 __u8 padding2[12];
251 __be32 hdr_crc;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400252} __attribute__ ((packed));
253
254/* Internal UBI volumes count */
255#define UBI_INT_VOL_COUNT 1
256
257/*
258 * Starting ID of internal volumes. There is reserved room for 4096 internal
259 * volumes.
260 */
261#define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096)
262
263/* The layout volume contains the volume table */
264
265#define UBI_LAYOUT_VOL_ID UBI_INTERNAL_VOL_START
266#define UBI_LAYOUT_VOLUME_EBS 2
267#define UBI_LAYOUT_VOLUME_NAME "layout volume"
268#define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT
269
270/* The maximum number of volumes per one UBI device */
271#define UBI_MAX_VOLUMES 128
272
273/* The maximum volume name length */
274#define UBI_VOL_NAME_MAX 127
275
276/* Size of the volume table record */
277#define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vtbl_record)
278
279/* Size of the volume table record without the ending CRC */
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300280#define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(__be32))
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400281
282/**
283 * struct ubi_vtbl_record - a record in the volume table.
284 * @reserved_pebs: how many physical eraseblocks are reserved for this volume
285 * @alignment: volume alignment
286 * @data_pad: how many bytes are unused at the end of the each physical
287 * eraseblock to satisfy the requested alignment
288 * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
289 * @upd_marker: if volume update was started but not finished
290 * @name_len: volume name length
291 * @name: the volume name
292 * @padding2: reserved, zeroes
293 * @crc: a CRC32 checksum of the record
294 *
295 * The volume table records are stored in the volume table, which is stored in
296 * the layout volume. The layout volume consists of 2 logical eraseblock, each
297 * of which contains a copy of the volume table (i.e., the volume table is
298 * duplicated). The volume table is an array of &struct ubi_vtbl_record
299 * objects indexed by the volume ID.
300 *
301 * If the size of the logical eraseblock is large enough to fit
302 * %UBI_MAX_VOLUMES records, the volume table contains %UBI_MAX_VOLUMES
303 * records. Otherwise, it contains as many records as it can fit (i.e., size of
304 * logical eraseblock divided by sizeof(struct ubi_vtbl_record)).
305 *
306 * The @upd_marker flag is used to implement volume update. It is set to %1
307 * before update and set to %0 after the update. So if the update operation was
308 * interrupted, UBI knows that the volume is corrupted.
309 *
310 * The @alignment field is specified when the volume is created and cannot be
311 * later changed. It may be useful, for example, when a block-oriented file
312 * system works on top of UBI. The @data_pad field is calculated using the
313 * logical eraseblock size and @alignment. The alignment must be multiple to the
314 * minimal flash I/O unit. If @alignment is 1, all the available space of
315 * the physical eraseblocks is used.
316 *
317 * Empty records contain all zeroes and the CRC checksum of those zeroes.
318 */
319struct ubi_vtbl_record {
Christoph Hellwig3261ebd2007-05-21 17:41:46 +0300320 __be32 reserved_pebs;
321 __be32 alignment;
322 __be32 data_pad;
323 __u8 vol_type;
324 __u8 upd_marker;
325 __be16 name_len;
326 __u8 name[UBI_VOL_NAME_MAX+1];
327 __u8 padding2[24];
328 __be32 crc;
Artem B. Bityutskiy801c1352006-06-27 12:22:22 +0400329} __attribute__ ((packed));
330
331#endif /* !__UBI_HEADER_H__ */