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