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