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