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