162306a36Sopenharmony_ci/* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */ 262306a36Sopenharmony_ci/* 362306a36Sopenharmony_ci * Copyright (C) International Business Machines Corp., 2006 462306a36Sopenharmony_ci * Authors: Artem Bityutskiy (Битюцкий Артём) 562306a36Sopenharmony_ci * Thomas Gleixner 662306a36Sopenharmony_ci * Frank Haverkamp 762306a36Sopenharmony_ci * Oliver Lohmann 862306a36Sopenharmony_ci * Andreas Arnez 962306a36Sopenharmony_ci * 1062306a36Sopenharmony_ci * This file defines the layout of UBI headers and all the other UBI on-flash 1162306a36Sopenharmony_ci * data structures. 1262306a36Sopenharmony_ci */ 1362306a36Sopenharmony_ci 1462306a36Sopenharmony_ci#ifndef __UBI_MEDIA_H__ 1562306a36Sopenharmony_ci#define __UBI_MEDIA_H__ 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci#include <asm/byteorder.h> 1862306a36Sopenharmony_ci 1962306a36Sopenharmony_ci/* The version of UBI images supported by this implementation */ 2062306a36Sopenharmony_ci#define UBI_VERSION 1 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ci/* The highest erase counter value supported by this implementation */ 2362306a36Sopenharmony_ci#define UBI_MAX_ERASECOUNTER 0x7FFFFFFF 2462306a36Sopenharmony_ci 2562306a36Sopenharmony_ci/* The initial CRC32 value used when calculating CRC checksums */ 2662306a36Sopenharmony_ci#define UBI_CRC32_INIT 0xFFFFFFFFU 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci/* Erase counter header magic number (ASCII "UBI#") */ 2962306a36Sopenharmony_ci#define UBI_EC_HDR_MAGIC 0x55424923 3062306a36Sopenharmony_ci/* Volume identifier header magic number (ASCII "UBI!") */ 3162306a36Sopenharmony_ci#define UBI_VID_HDR_MAGIC 0x55424921 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci/* 3462306a36Sopenharmony_ci * Volume type constants used in the volume identifier header. 3562306a36Sopenharmony_ci * 3662306a36Sopenharmony_ci * @UBI_VID_DYNAMIC: dynamic volume 3762306a36Sopenharmony_ci * @UBI_VID_STATIC: static volume 3862306a36Sopenharmony_ci */ 3962306a36Sopenharmony_cienum { 4062306a36Sopenharmony_ci UBI_VID_DYNAMIC = 1, 4162306a36Sopenharmony_ci UBI_VID_STATIC = 2 4262306a36Sopenharmony_ci}; 4362306a36Sopenharmony_ci 4462306a36Sopenharmony_ci/* 4562306a36Sopenharmony_ci * Volume flags used in the volume table record. 4662306a36Sopenharmony_ci * 4762306a36Sopenharmony_ci * @UBI_VTBL_AUTORESIZE_FLG: auto-resize this volume 4862306a36Sopenharmony_ci * @UBI_VTBL_SKIP_CRC_CHECK_FLG: skip the CRC check done on a static volume at 4962306a36Sopenharmony_ci * open time. Should only be set on volumes that 5062306a36Sopenharmony_ci * are used by upper layers doing this kind of 5162306a36Sopenharmony_ci * check. Main use-case for this flag is 5262306a36Sopenharmony_ci * boot-time reduction 5362306a36Sopenharmony_ci * 5462306a36Sopenharmony_ci * %UBI_VTBL_AUTORESIZE_FLG flag can be set only for one volume in the volume 5562306a36Sopenharmony_ci * table. UBI automatically re-sizes the volume which has this flag and makes 5662306a36Sopenharmony_ci * the volume to be of largest possible size. This means that if after the 5762306a36Sopenharmony_ci * initialization UBI finds out that there are available physical eraseblocks 5862306a36Sopenharmony_ci * present on the device, it automatically appends all of them to the volume 5962306a36Sopenharmony_ci * (the physical eraseblocks reserved for bad eraseblocks handling and other 6062306a36Sopenharmony_ci * reserved physical eraseblocks are not taken). So, if there is a volume with 6162306a36Sopenharmony_ci * the %UBI_VTBL_AUTORESIZE_FLG flag set, the amount of available logical 6262306a36Sopenharmony_ci * eraseblocks will be zero after UBI is loaded, because all of them will be 6362306a36Sopenharmony_ci * reserved for this volume. Note, the %UBI_VTBL_AUTORESIZE_FLG bit is cleared 6462306a36Sopenharmony_ci * after the volume had been initialized. 6562306a36Sopenharmony_ci * 6662306a36Sopenharmony_ci * The auto-resize feature is useful for device production purposes. For 6762306a36Sopenharmony_ci * example, different NAND flash chips may have different amount of initial bad 6862306a36Sopenharmony_ci * eraseblocks, depending of particular chip instance. Manufacturers of NAND 6962306a36Sopenharmony_ci * chips usually guarantee that the amount of initial bad eraseblocks does not 7062306a36Sopenharmony_ci * exceed certain percent, e.g. 2%. When one creates an UBI image which will be 7162306a36Sopenharmony_ci * flashed to the end devices in production, he does not know the exact amount 7262306a36Sopenharmony_ci * of good physical eraseblocks the NAND chip on the device will have, but this 7362306a36Sopenharmony_ci * number is required to calculate the volume sized and put them to the volume 7462306a36Sopenharmony_ci * table of the UBI image. In this case, one of the volumes (e.g., the one 7562306a36Sopenharmony_ci * which will store the root file system) is marked as "auto-resizable", and 7662306a36Sopenharmony_ci * UBI will adjust its size on the first boot if needed. 7762306a36Sopenharmony_ci * 7862306a36Sopenharmony_ci * Note, first UBI reserves some amount of physical eraseblocks for bad 7962306a36Sopenharmony_ci * eraseblock handling, and then re-sizes the volume, not vice-versa. This 8062306a36Sopenharmony_ci * means that the pool of reserved physical eraseblocks will always be present. 8162306a36Sopenharmony_ci */ 8262306a36Sopenharmony_cienum { 8362306a36Sopenharmony_ci UBI_VTBL_AUTORESIZE_FLG = 0x01, 8462306a36Sopenharmony_ci UBI_VTBL_SKIP_CRC_CHECK_FLG = 0x02, 8562306a36Sopenharmony_ci}; 8662306a36Sopenharmony_ci 8762306a36Sopenharmony_ci/* 8862306a36Sopenharmony_ci * Compatibility constants used by internal volumes. 8962306a36Sopenharmony_ci * 9062306a36Sopenharmony_ci * @UBI_COMPAT_DELETE: delete this internal volume before anything is written 9162306a36Sopenharmony_ci * to the flash 9262306a36Sopenharmony_ci * @UBI_COMPAT_RO: attach this device in read-only mode 9362306a36Sopenharmony_ci * @UBI_COMPAT_PRESERVE: preserve this internal volume - do not touch its 9462306a36Sopenharmony_ci * physical eraseblocks, don't allow the wear-leveling 9562306a36Sopenharmony_ci * sub-system to move them 9662306a36Sopenharmony_ci * @UBI_COMPAT_REJECT: reject this UBI image 9762306a36Sopenharmony_ci */ 9862306a36Sopenharmony_cienum { 9962306a36Sopenharmony_ci UBI_COMPAT_DELETE = 1, 10062306a36Sopenharmony_ci UBI_COMPAT_RO = 2, 10162306a36Sopenharmony_ci UBI_COMPAT_PRESERVE = 4, 10262306a36Sopenharmony_ci UBI_COMPAT_REJECT = 5 10362306a36Sopenharmony_ci}; 10462306a36Sopenharmony_ci 10562306a36Sopenharmony_ci/* Sizes of UBI headers */ 10662306a36Sopenharmony_ci#define UBI_EC_HDR_SIZE sizeof(struct ubi_ec_hdr) 10762306a36Sopenharmony_ci#define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr) 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ci/* Sizes of UBI headers without the ending CRC */ 11062306a36Sopenharmony_ci#define UBI_EC_HDR_SIZE_CRC (UBI_EC_HDR_SIZE - sizeof(__be32)) 11162306a36Sopenharmony_ci#define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(__be32)) 11262306a36Sopenharmony_ci 11362306a36Sopenharmony_ci/** 11462306a36Sopenharmony_ci * struct ubi_ec_hdr - UBI erase counter header. 11562306a36Sopenharmony_ci * @magic: erase counter header magic number (%UBI_EC_HDR_MAGIC) 11662306a36Sopenharmony_ci * @version: version of UBI implementation which is supposed to accept this 11762306a36Sopenharmony_ci * UBI image 11862306a36Sopenharmony_ci * @padding1: reserved for future, zeroes 11962306a36Sopenharmony_ci * @ec: the erase counter 12062306a36Sopenharmony_ci * @vid_hdr_offset: where the VID header starts 12162306a36Sopenharmony_ci * @data_offset: where the user data start 12262306a36Sopenharmony_ci * @image_seq: image sequence number 12362306a36Sopenharmony_ci * @padding2: reserved for future, zeroes 12462306a36Sopenharmony_ci * @hdr_crc: erase counter header CRC checksum 12562306a36Sopenharmony_ci * 12662306a36Sopenharmony_ci * The erase counter header takes 64 bytes and has a plenty of unused space for 12762306a36Sopenharmony_ci * future usage. The unused fields are zeroed. The @version field is used to 12862306a36Sopenharmony_ci * indicate the version of UBI implementation which is supposed to be able to 12962306a36Sopenharmony_ci * work with this UBI image. If @version is greater than the current UBI 13062306a36Sopenharmony_ci * version, the image is rejected. This may be useful in future if something 13162306a36Sopenharmony_ci * is changed radically. This field is duplicated in the volume identifier 13262306a36Sopenharmony_ci * header. 13362306a36Sopenharmony_ci * 13462306a36Sopenharmony_ci * The @vid_hdr_offset and @data_offset fields contain the offset of the 13562306a36Sopenharmony_ci * volume identifier header and user data, relative to the beginning of the 13662306a36Sopenharmony_ci * physical eraseblock. These values have to be the same for all physical 13762306a36Sopenharmony_ci * eraseblocks. 13862306a36Sopenharmony_ci * 13962306a36Sopenharmony_ci * The @image_seq field is used to validate a UBI image that has been prepared 14062306a36Sopenharmony_ci * for a UBI device. The @image_seq value can be any value, but it must be the 14162306a36Sopenharmony_ci * same on all eraseblocks. UBI will ensure that all new erase counter headers 14262306a36Sopenharmony_ci * also contain this value, and will check the value when attaching the flash. 14362306a36Sopenharmony_ci * One way to make use of @image_seq is to increase its value by one every time 14462306a36Sopenharmony_ci * an image is flashed over an existing image, then, if the flashing does not 14562306a36Sopenharmony_ci * complete, UBI will detect the error when attaching the media. 14662306a36Sopenharmony_ci */ 14762306a36Sopenharmony_cistruct ubi_ec_hdr { 14862306a36Sopenharmony_ci __be32 magic; 14962306a36Sopenharmony_ci __u8 version; 15062306a36Sopenharmony_ci __u8 padding1[3]; 15162306a36Sopenharmony_ci __be64 ec; /* Warning: the current limit is 31-bit anyway! */ 15262306a36Sopenharmony_ci __be32 vid_hdr_offset; 15362306a36Sopenharmony_ci __be32 data_offset; 15462306a36Sopenharmony_ci __be32 image_seq; 15562306a36Sopenharmony_ci __u8 padding2[32]; 15662306a36Sopenharmony_ci __be32 hdr_crc; 15762306a36Sopenharmony_ci} __packed; 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci/** 16062306a36Sopenharmony_ci * struct ubi_vid_hdr - on-flash UBI volume identifier header. 16162306a36Sopenharmony_ci * @magic: volume identifier header magic number (%UBI_VID_HDR_MAGIC) 16262306a36Sopenharmony_ci * @version: UBI implementation version which is supposed to accept this UBI 16362306a36Sopenharmony_ci * image (%UBI_VERSION) 16462306a36Sopenharmony_ci * @vol_type: volume type (%UBI_VID_DYNAMIC or %UBI_VID_STATIC) 16562306a36Sopenharmony_ci * @copy_flag: if this logical eraseblock was copied from another physical 16662306a36Sopenharmony_ci * eraseblock (for wear-leveling reasons) 16762306a36Sopenharmony_ci * @compat: compatibility of this volume (%0, %UBI_COMPAT_DELETE, 16862306a36Sopenharmony_ci * %UBI_COMPAT_IGNORE, %UBI_COMPAT_PRESERVE, or %UBI_COMPAT_REJECT) 16962306a36Sopenharmony_ci * @vol_id: ID of this volume 17062306a36Sopenharmony_ci * @lnum: logical eraseblock number 17162306a36Sopenharmony_ci * @padding1: reserved for future, zeroes 17262306a36Sopenharmony_ci * @data_size: how many bytes of data this logical eraseblock contains 17362306a36Sopenharmony_ci * @used_ebs: total number of used logical eraseblocks in this volume 17462306a36Sopenharmony_ci * @data_pad: how many bytes at the end of this physical eraseblock are not 17562306a36Sopenharmony_ci * used 17662306a36Sopenharmony_ci * @data_crc: CRC checksum of the data stored in this logical eraseblock 17762306a36Sopenharmony_ci * @padding2: reserved for future, zeroes 17862306a36Sopenharmony_ci * @sqnum: sequence number 17962306a36Sopenharmony_ci * @padding3: reserved for future, zeroes 18062306a36Sopenharmony_ci * @hdr_crc: volume identifier header CRC checksum 18162306a36Sopenharmony_ci * 18262306a36Sopenharmony_ci * The @sqnum is the value of the global sequence counter at the time when this 18362306a36Sopenharmony_ci * VID header was created. The global sequence counter is incremented each time 18462306a36Sopenharmony_ci * UBI writes a new VID header to the flash, i.e. when it maps a logical 18562306a36Sopenharmony_ci * eraseblock to a new physical eraseblock. The global sequence counter is an 18662306a36Sopenharmony_ci * unsigned 64-bit integer and we assume it never overflows. The @sqnum 18762306a36Sopenharmony_ci * (sequence number) is used to distinguish between older and newer versions of 18862306a36Sopenharmony_ci * logical eraseblocks. 18962306a36Sopenharmony_ci * 19062306a36Sopenharmony_ci * There are 2 situations when there may be more than one physical eraseblock 19162306a36Sopenharmony_ci * corresponding to the same logical eraseblock, i.e., having the same @vol_id 19262306a36Sopenharmony_ci * and @lnum values in the volume identifier header. Suppose we have a logical 19362306a36Sopenharmony_ci * eraseblock L and it is mapped to the physical eraseblock P. 19462306a36Sopenharmony_ci * 19562306a36Sopenharmony_ci * 1. Because UBI may erase physical eraseblocks asynchronously, the following 19662306a36Sopenharmony_ci * situation is possible: L is asynchronously erased, so P is scheduled for 19762306a36Sopenharmony_ci * erasure, then L is written to,i.e. mapped to another physical eraseblock P1, 19862306a36Sopenharmony_ci * so P1 is written to, then an unclean reboot happens. Result - there are 2 19962306a36Sopenharmony_ci * physical eraseblocks P and P1 corresponding to the same logical eraseblock 20062306a36Sopenharmony_ci * L. But P1 has greater sequence number, so UBI picks P1 when it attaches the 20162306a36Sopenharmony_ci * flash. 20262306a36Sopenharmony_ci * 20362306a36Sopenharmony_ci * 2. From time to time UBI moves logical eraseblocks to other physical 20462306a36Sopenharmony_ci * eraseblocks for wear-leveling reasons. If, for example, UBI moves L from P 20562306a36Sopenharmony_ci * to P1, and an unclean reboot happens before P is physically erased, there 20662306a36Sopenharmony_ci * are two physical eraseblocks P and P1 corresponding to L and UBI has to 20762306a36Sopenharmony_ci * select one of them when the flash is attached. The @sqnum field says which 20862306a36Sopenharmony_ci * PEB is the original (obviously P will have lower @sqnum) and the copy. But 20962306a36Sopenharmony_ci * it is not enough to select the physical eraseblock with the higher sequence 21062306a36Sopenharmony_ci * number, because the unclean reboot could have happen in the middle of the 21162306a36Sopenharmony_ci * copying process, so the data in P is corrupted. It is also not enough to 21262306a36Sopenharmony_ci * just select the physical eraseblock with lower sequence number, because the 21362306a36Sopenharmony_ci * data there may be old (consider a case if more data was added to P1 after 21462306a36Sopenharmony_ci * the copying). Moreover, the unclean reboot may happen when the erasure of P 21562306a36Sopenharmony_ci * was just started, so it result in unstable P, which is "mostly" OK, but 21662306a36Sopenharmony_ci * still has unstable bits. 21762306a36Sopenharmony_ci * 21862306a36Sopenharmony_ci * UBI uses the @copy_flag field to indicate that this logical eraseblock is a 21962306a36Sopenharmony_ci * copy. UBI also calculates data CRC when the data is moved and stores it at 22062306a36Sopenharmony_ci * the @data_crc field of the copy (P1). So when UBI needs to pick one physical 22162306a36Sopenharmony_ci * eraseblock of two (P or P1), the @copy_flag of the newer one (P1) is 22262306a36Sopenharmony_ci * examined. If it is cleared, the situation is simple and the newer one is 22362306a36Sopenharmony_ci * picked. If it is set, the data CRC of the copy (P1) is examined. If the CRC 22462306a36Sopenharmony_ci * checksum is correct, this physical eraseblock is selected (P1). Otherwise 22562306a36Sopenharmony_ci * the older one (P) is selected. 22662306a36Sopenharmony_ci * 22762306a36Sopenharmony_ci * There are 2 sorts of volumes in UBI: user volumes and internal volumes. 22862306a36Sopenharmony_ci * Internal volumes are not seen from outside and are used for various internal 22962306a36Sopenharmony_ci * UBI purposes. In this implementation there is only one internal volume - the 23062306a36Sopenharmony_ci * layout volume. Internal volumes are the main mechanism of UBI extensions. 23162306a36Sopenharmony_ci * For example, in future one may introduce a journal internal volume. Internal 23262306a36Sopenharmony_ci * volumes have their own reserved range of IDs. 23362306a36Sopenharmony_ci * 23462306a36Sopenharmony_ci * The @compat field is only used for internal volumes and contains the "degree 23562306a36Sopenharmony_ci * of their compatibility". It is always zero for user volumes. This field 23662306a36Sopenharmony_ci * provides a mechanism to introduce UBI extensions and to be still compatible 23762306a36Sopenharmony_ci * with older UBI binaries. For example, if someone introduced a journal in 23862306a36Sopenharmony_ci * future, he would probably use %UBI_COMPAT_DELETE compatibility for the 23962306a36Sopenharmony_ci * journal volume. And in this case, older UBI binaries, which know nothing 24062306a36Sopenharmony_ci * about the journal volume, would just delete this volume and work perfectly 24162306a36Sopenharmony_ci * fine. This is similar to what Ext2fs does when it is fed by an Ext3fs image 24262306a36Sopenharmony_ci * - it just ignores the Ext3fs journal. 24362306a36Sopenharmony_ci * 24462306a36Sopenharmony_ci * The @data_crc field contains the CRC checksum of the contents of the logical 24562306a36Sopenharmony_ci * eraseblock if this is a static volume. In case of dynamic volumes, it does 24662306a36Sopenharmony_ci * not contain the CRC checksum as a rule. The only exception is when the 24762306a36Sopenharmony_ci * data of the physical eraseblock was moved by the wear-leveling sub-system, 24862306a36Sopenharmony_ci * then the wear-leveling sub-system calculates the data CRC and stores it in 24962306a36Sopenharmony_ci * the @data_crc field. And of course, the @copy_flag is %in this case. 25062306a36Sopenharmony_ci * 25162306a36Sopenharmony_ci * The @data_size field is used only for static volumes because UBI has to know 25262306a36Sopenharmony_ci * how many bytes of data are stored in this eraseblock. For dynamic volumes, 25362306a36Sopenharmony_ci * this field usually contains zero. The only exception is when the data of the 25462306a36Sopenharmony_ci * physical eraseblock was moved to another physical eraseblock for 25562306a36Sopenharmony_ci * wear-leveling reasons. In this case, UBI calculates CRC checksum of the 25662306a36Sopenharmony_ci * contents and uses both @data_crc and @data_size fields. In this case, the 25762306a36Sopenharmony_ci * @data_size field contains data size. 25862306a36Sopenharmony_ci * 25962306a36Sopenharmony_ci * The @used_ebs field is used only for static volumes and indicates how many 26062306a36Sopenharmony_ci * eraseblocks the data of the volume takes. For dynamic volumes this field is 26162306a36Sopenharmony_ci * not used and always contains zero. 26262306a36Sopenharmony_ci * 26362306a36Sopenharmony_ci * The @data_pad is calculated when volumes are created using the alignment 26462306a36Sopenharmony_ci * parameter. So, effectively, the @data_pad field reduces the size of logical 26562306a36Sopenharmony_ci * eraseblocks of this volume. This is very handy when one uses block-oriented 26662306a36Sopenharmony_ci * software (say, cramfs) on top of the UBI volume. 26762306a36Sopenharmony_ci */ 26862306a36Sopenharmony_cistruct ubi_vid_hdr { 26962306a36Sopenharmony_ci __be32 magic; 27062306a36Sopenharmony_ci __u8 version; 27162306a36Sopenharmony_ci __u8 vol_type; 27262306a36Sopenharmony_ci __u8 copy_flag; 27362306a36Sopenharmony_ci __u8 compat; 27462306a36Sopenharmony_ci __be32 vol_id; 27562306a36Sopenharmony_ci __be32 lnum; 27662306a36Sopenharmony_ci __u8 padding1[4]; 27762306a36Sopenharmony_ci __be32 data_size; 27862306a36Sopenharmony_ci __be32 used_ebs; 27962306a36Sopenharmony_ci __be32 data_pad; 28062306a36Sopenharmony_ci __be32 data_crc; 28162306a36Sopenharmony_ci __u8 padding2[4]; 28262306a36Sopenharmony_ci __be64 sqnum; 28362306a36Sopenharmony_ci __u8 padding3[12]; 28462306a36Sopenharmony_ci __be32 hdr_crc; 28562306a36Sopenharmony_ci} __packed; 28662306a36Sopenharmony_ci 28762306a36Sopenharmony_ci/* Internal UBI volumes count */ 28862306a36Sopenharmony_ci#define UBI_INT_VOL_COUNT 1 28962306a36Sopenharmony_ci 29062306a36Sopenharmony_ci/* 29162306a36Sopenharmony_ci * Starting ID of internal volumes: 0x7fffefff. 29262306a36Sopenharmony_ci * There is reserved room for 4096 internal volumes. 29362306a36Sopenharmony_ci */ 29462306a36Sopenharmony_ci#define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096) 29562306a36Sopenharmony_ci 29662306a36Sopenharmony_ci/* The layout volume contains the volume table */ 29762306a36Sopenharmony_ci 29862306a36Sopenharmony_ci#define UBI_LAYOUT_VOLUME_ID UBI_INTERNAL_VOL_START 29962306a36Sopenharmony_ci#define UBI_LAYOUT_VOLUME_TYPE UBI_VID_DYNAMIC 30062306a36Sopenharmony_ci#define UBI_LAYOUT_VOLUME_ALIGN 1 30162306a36Sopenharmony_ci#define UBI_LAYOUT_VOLUME_EBS 2 30262306a36Sopenharmony_ci#define UBI_LAYOUT_VOLUME_NAME "layout volume" 30362306a36Sopenharmony_ci#define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT 30462306a36Sopenharmony_ci 30562306a36Sopenharmony_ci/* The maximum number of volumes per one UBI device */ 30662306a36Sopenharmony_ci#define UBI_MAX_VOLUMES 128 30762306a36Sopenharmony_ci 30862306a36Sopenharmony_ci/* The maximum volume name length */ 30962306a36Sopenharmony_ci#define UBI_VOL_NAME_MAX 127 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ci/* Size of the volume table record */ 31262306a36Sopenharmony_ci#define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vtbl_record) 31362306a36Sopenharmony_ci 31462306a36Sopenharmony_ci/* Size of the volume table record without the ending CRC */ 31562306a36Sopenharmony_ci#define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(__be32)) 31662306a36Sopenharmony_ci 31762306a36Sopenharmony_ci/** 31862306a36Sopenharmony_ci * struct ubi_vtbl_record - a record in the volume table. 31962306a36Sopenharmony_ci * @reserved_pebs: how many physical eraseblocks are reserved for this volume 32062306a36Sopenharmony_ci * @alignment: volume alignment 32162306a36Sopenharmony_ci * @data_pad: how many bytes are unused at the end of the each physical 32262306a36Sopenharmony_ci * eraseblock to satisfy the requested alignment 32362306a36Sopenharmony_ci * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME) 32462306a36Sopenharmony_ci * @upd_marker: if volume update was started but not finished 32562306a36Sopenharmony_ci * @name_len: volume name length 32662306a36Sopenharmony_ci * @name: the volume name 32762306a36Sopenharmony_ci * @flags: volume flags (%UBI_VTBL_AUTORESIZE_FLG) 32862306a36Sopenharmony_ci * @padding: reserved, zeroes 32962306a36Sopenharmony_ci * @crc: a CRC32 checksum of the record 33062306a36Sopenharmony_ci * 33162306a36Sopenharmony_ci * The volume table records are stored in the volume table, which is stored in 33262306a36Sopenharmony_ci * the layout volume. The layout volume consists of 2 logical eraseblock, each 33362306a36Sopenharmony_ci * of which contains a copy of the volume table (i.e., the volume table is 33462306a36Sopenharmony_ci * duplicated). The volume table is an array of &struct ubi_vtbl_record 33562306a36Sopenharmony_ci * objects indexed by the volume ID. 33662306a36Sopenharmony_ci * 33762306a36Sopenharmony_ci * If the size of the logical eraseblock is large enough to fit 33862306a36Sopenharmony_ci * %UBI_MAX_VOLUMES records, the volume table contains %UBI_MAX_VOLUMES 33962306a36Sopenharmony_ci * records. Otherwise, it contains as many records as it can fit (i.e., size of 34062306a36Sopenharmony_ci * logical eraseblock divided by sizeof(struct ubi_vtbl_record)). 34162306a36Sopenharmony_ci * 34262306a36Sopenharmony_ci * The @upd_marker flag is used to implement volume update. It is set to %1 34362306a36Sopenharmony_ci * before update and set to %0 after the update. So if the update operation was 34462306a36Sopenharmony_ci * interrupted, UBI knows that the volume is corrupted. 34562306a36Sopenharmony_ci * 34662306a36Sopenharmony_ci * The @alignment field is specified when the volume is created and cannot be 34762306a36Sopenharmony_ci * later changed. It may be useful, for example, when a block-oriented file 34862306a36Sopenharmony_ci * system works on top of UBI. The @data_pad field is calculated using the 34962306a36Sopenharmony_ci * logical eraseblock size and @alignment. The alignment must be multiple to the 35062306a36Sopenharmony_ci * minimal flash I/O unit. If @alignment is 1, all the available space of 35162306a36Sopenharmony_ci * the physical eraseblocks is used. 35262306a36Sopenharmony_ci * 35362306a36Sopenharmony_ci * Empty records contain all zeroes and the CRC checksum of those zeroes. 35462306a36Sopenharmony_ci */ 35562306a36Sopenharmony_cistruct ubi_vtbl_record { 35662306a36Sopenharmony_ci __be32 reserved_pebs; 35762306a36Sopenharmony_ci __be32 alignment; 35862306a36Sopenharmony_ci __be32 data_pad; 35962306a36Sopenharmony_ci __u8 vol_type; 36062306a36Sopenharmony_ci __u8 upd_marker; 36162306a36Sopenharmony_ci __be16 name_len; 36262306a36Sopenharmony_ci __u8 name[UBI_VOL_NAME_MAX+1]; 36362306a36Sopenharmony_ci __u8 flags; 36462306a36Sopenharmony_ci __u8 padding[23]; 36562306a36Sopenharmony_ci __be32 crc; 36662306a36Sopenharmony_ci} __packed; 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ci/* UBI fastmap on-flash data structures */ 36962306a36Sopenharmony_ci 37062306a36Sopenharmony_ci#define UBI_FM_SB_VOLUME_ID (UBI_LAYOUT_VOLUME_ID + 1) 37162306a36Sopenharmony_ci#define UBI_FM_DATA_VOLUME_ID (UBI_LAYOUT_VOLUME_ID + 2) 37262306a36Sopenharmony_ci 37362306a36Sopenharmony_ci/* fastmap on-flash data structure format version */ 37462306a36Sopenharmony_ci#define UBI_FM_FMT_VERSION 1 37562306a36Sopenharmony_ci 37662306a36Sopenharmony_ci#define UBI_FM_SB_MAGIC 0x7B11D69F 37762306a36Sopenharmony_ci#define UBI_FM_HDR_MAGIC 0xD4B82EF7 37862306a36Sopenharmony_ci#define UBI_FM_VHDR_MAGIC 0xFA370ED1 37962306a36Sopenharmony_ci#define UBI_FM_POOL_MAGIC 0x67AF4D08 38062306a36Sopenharmony_ci#define UBI_FM_EBA_MAGIC 0xf0c040a8 38162306a36Sopenharmony_ci 38262306a36Sopenharmony_ci/* A fastmap super block can be located between PEB 0 and 38362306a36Sopenharmony_ci * UBI_FM_MAX_START */ 38462306a36Sopenharmony_ci#define UBI_FM_MAX_START 64 38562306a36Sopenharmony_ci 38662306a36Sopenharmony_ci/* A fastmap can use up to UBI_FM_MAX_BLOCKS PEBs */ 38762306a36Sopenharmony_ci#define UBI_FM_MAX_BLOCKS 32 38862306a36Sopenharmony_ci 38962306a36Sopenharmony_ci/* 5% of the total number of PEBs have to be scanned while attaching 39062306a36Sopenharmony_ci * from a fastmap. 39162306a36Sopenharmony_ci * But the size of this pool is limited to be between UBI_FM_MIN_POOL_SIZE and 39262306a36Sopenharmony_ci * UBI_FM_MAX_POOL_SIZE */ 39362306a36Sopenharmony_ci#define UBI_FM_MIN_POOL_SIZE 8 39462306a36Sopenharmony_ci#define UBI_FM_MAX_POOL_SIZE 256 39562306a36Sopenharmony_ci 39662306a36Sopenharmony_ci/** 39762306a36Sopenharmony_ci * struct ubi_fm_sb - UBI fastmap super block 39862306a36Sopenharmony_ci * @magic: fastmap super block magic number (%UBI_FM_SB_MAGIC) 39962306a36Sopenharmony_ci * @version: format version of this fastmap 40062306a36Sopenharmony_ci * @data_crc: CRC over the fastmap data 40162306a36Sopenharmony_ci * @used_blocks: number of PEBs used by this fastmap 40262306a36Sopenharmony_ci * @block_loc: an array containing the location of all PEBs of the fastmap 40362306a36Sopenharmony_ci * @block_ec: the erase counter of each used PEB 40462306a36Sopenharmony_ci * @sqnum: highest sequence number value at the time while taking the fastmap 40562306a36Sopenharmony_ci * 40662306a36Sopenharmony_ci */ 40762306a36Sopenharmony_cistruct ubi_fm_sb { 40862306a36Sopenharmony_ci __be32 magic; 40962306a36Sopenharmony_ci __u8 version; 41062306a36Sopenharmony_ci __u8 padding1[3]; 41162306a36Sopenharmony_ci __be32 data_crc; 41262306a36Sopenharmony_ci __be32 used_blocks; 41362306a36Sopenharmony_ci __be32 block_loc[UBI_FM_MAX_BLOCKS]; 41462306a36Sopenharmony_ci __be32 block_ec[UBI_FM_MAX_BLOCKS]; 41562306a36Sopenharmony_ci __be64 sqnum; 41662306a36Sopenharmony_ci __u8 padding2[32]; 41762306a36Sopenharmony_ci} __packed; 41862306a36Sopenharmony_ci 41962306a36Sopenharmony_ci/** 42062306a36Sopenharmony_ci * struct ubi_fm_hdr - header of the fastmap data set 42162306a36Sopenharmony_ci * @magic: fastmap header magic number (%UBI_FM_HDR_MAGIC) 42262306a36Sopenharmony_ci * @free_peb_count: number of free PEBs known by this fastmap 42362306a36Sopenharmony_ci * @used_peb_count: number of used PEBs known by this fastmap 42462306a36Sopenharmony_ci * @scrub_peb_count: number of to be scrubbed PEBs known by this fastmap 42562306a36Sopenharmony_ci * @bad_peb_count: number of bad PEBs known by this fastmap 42662306a36Sopenharmony_ci * @erase_peb_count: number of bad PEBs which have to be erased 42762306a36Sopenharmony_ci * @vol_count: number of UBI volumes known by this fastmap 42862306a36Sopenharmony_ci */ 42962306a36Sopenharmony_cistruct ubi_fm_hdr { 43062306a36Sopenharmony_ci __be32 magic; 43162306a36Sopenharmony_ci __be32 free_peb_count; 43262306a36Sopenharmony_ci __be32 used_peb_count; 43362306a36Sopenharmony_ci __be32 scrub_peb_count; 43462306a36Sopenharmony_ci __be32 bad_peb_count; 43562306a36Sopenharmony_ci __be32 erase_peb_count; 43662306a36Sopenharmony_ci __be32 vol_count; 43762306a36Sopenharmony_ci __u8 padding[4]; 43862306a36Sopenharmony_ci} __packed; 43962306a36Sopenharmony_ci 44062306a36Sopenharmony_ci/* struct ubi_fm_hdr is followed by two struct ubi_fm_scan_pool */ 44162306a36Sopenharmony_ci 44262306a36Sopenharmony_ci/** 44362306a36Sopenharmony_ci * struct ubi_fm_scan_pool - Fastmap pool PEBs to be scanned while attaching 44462306a36Sopenharmony_ci * @magic: pool magic numer (%UBI_FM_POOL_MAGIC) 44562306a36Sopenharmony_ci * @size: current pool size 44662306a36Sopenharmony_ci * @max_size: maximal pool size 44762306a36Sopenharmony_ci * @pebs: an array containing the location of all PEBs in this pool 44862306a36Sopenharmony_ci */ 44962306a36Sopenharmony_cistruct ubi_fm_scan_pool { 45062306a36Sopenharmony_ci __be32 magic; 45162306a36Sopenharmony_ci __be16 size; 45262306a36Sopenharmony_ci __be16 max_size; 45362306a36Sopenharmony_ci __be32 pebs[UBI_FM_MAX_POOL_SIZE]; 45462306a36Sopenharmony_ci __be32 padding[4]; 45562306a36Sopenharmony_ci} __packed; 45662306a36Sopenharmony_ci 45762306a36Sopenharmony_ci/* ubi_fm_scan_pool is followed by nfree+nused struct ubi_fm_ec records */ 45862306a36Sopenharmony_ci 45962306a36Sopenharmony_ci/** 46062306a36Sopenharmony_ci * struct ubi_fm_ec - stores the erase counter of a PEB 46162306a36Sopenharmony_ci * @pnum: PEB number 46262306a36Sopenharmony_ci * @ec: ec of this PEB 46362306a36Sopenharmony_ci */ 46462306a36Sopenharmony_cistruct ubi_fm_ec { 46562306a36Sopenharmony_ci __be32 pnum; 46662306a36Sopenharmony_ci __be32 ec; 46762306a36Sopenharmony_ci} __packed; 46862306a36Sopenharmony_ci 46962306a36Sopenharmony_ci/** 47062306a36Sopenharmony_ci * struct ubi_fm_volhdr - Fastmap volume header 47162306a36Sopenharmony_ci * it identifies the start of an eba table 47262306a36Sopenharmony_ci * @magic: Fastmap volume header magic number (%UBI_FM_VHDR_MAGIC) 47362306a36Sopenharmony_ci * @vol_id: volume id of the fastmapped volume 47462306a36Sopenharmony_ci * @vol_type: type of the fastmapped volume 47562306a36Sopenharmony_ci * @data_pad: data_pad value of the fastmapped volume 47662306a36Sopenharmony_ci * @used_ebs: number of used LEBs within this volume 47762306a36Sopenharmony_ci * @last_eb_bytes: number of bytes used in the last LEB 47862306a36Sopenharmony_ci */ 47962306a36Sopenharmony_cistruct ubi_fm_volhdr { 48062306a36Sopenharmony_ci __be32 magic; 48162306a36Sopenharmony_ci __be32 vol_id; 48262306a36Sopenharmony_ci __u8 vol_type; 48362306a36Sopenharmony_ci __u8 padding1[3]; 48462306a36Sopenharmony_ci __be32 data_pad; 48562306a36Sopenharmony_ci __be32 used_ebs; 48662306a36Sopenharmony_ci __be32 last_eb_bytes; 48762306a36Sopenharmony_ci __u8 padding2[8]; 48862306a36Sopenharmony_ci} __packed; 48962306a36Sopenharmony_ci 49062306a36Sopenharmony_ci/* struct ubi_fm_volhdr is followed by one struct ubi_fm_eba records */ 49162306a36Sopenharmony_ci 49262306a36Sopenharmony_ci/** 49362306a36Sopenharmony_ci * struct ubi_fm_eba - denotes an association between a PEB and LEB 49462306a36Sopenharmony_ci * @magic: EBA table magic number 49562306a36Sopenharmony_ci * @reserved_pebs: number of table entries 49662306a36Sopenharmony_ci * @pnum: PEB number of LEB (LEB is the index) 49762306a36Sopenharmony_ci */ 49862306a36Sopenharmony_cistruct ubi_fm_eba { 49962306a36Sopenharmony_ci __be32 magic; 50062306a36Sopenharmony_ci __be32 reserved_pebs; 50162306a36Sopenharmony_ci __be32 pnum[]; 50262306a36Sopenharmony_ci} __packed; 50362306a36Sopenharmony_ci#endif /* !__UBI_MEDIA_H__ */ 504