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