18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause) */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright (C) International Business Machines Corp., 2006
48c2ecf20Sopenharmony_ci * Authors: Artem Bityutskiy (Битюцкий Артём)
58c2ecf20Sopenharmony_ci *          Thomas Gleixner
68c2ecf20Sopenharmony_ci *          Frank Haverkamp
78c2ecf20Sopenharmony_ci *          Oliver Lohmann
88c2ecf20Sopenharmony_ci *          Andreas Arnez
98c2ecf20Sopenharmony_ci *
108c2ecf20Sopenharmony_ci * This file defines the layout of UBI headers and all the other UBI on-flash
118c2ecf20Sopenharmony_ci * data structures.
128c2ecf20Sopenharmony_ci */
138c2ecf20Sopenharmony_ci
148c2ecf20Sopenharmony_ci#ifndef __UBI_MEDIA_H__
158c2ecf20Sopenharmony_ci#define __UBI_MEDIA_H__
168c2ecf20Sopenharmony_ci
178c2ecf20Sopenharmony_ci#include <asm/byteorder.h>
188c2ecf20Sopenharmony_ci
198c2ecf20Sopenharmony_ci/* The version of UBI images supported by this implementation */
208c2ecf20Sopenharmony_ci#define UBI_VERSION 1
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci/* The highest erase counter value supported by this implementation */
238c2ecf20Sopenharmony_ci#define UBI_MAX_ERASECOUNTER 0x7FFFFFFF
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci/* The initial CRC32 value used when calculating CRC checksums */
268c2ecf20Sopenharmony_ci#define UBI_CRC32_INIT 0xFFFFFFFFU
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci/* Erase counter header magic number (ASCII "UBI#") */
298c2ecf20Sopenharmony_ci#define UBI_EC_HDR_MAGIC  0x55424923
308c2ecf20Sopenharmony_ci/* Volume identifier header magic number (ASCII "UBI!") */
318c2ecf20Sopenharmony_ci#define UBI_VID_HDR_MAGIC 0x55424921
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_ci/*
348c2ecf20Sopenharmony_ci * Volume type constants used in the volume identifier header.
358c2ecf20Sopenharmony_ci *
368c2ecf20Sopenharmony_ci * @UBI_VID_DYNAMIC: dynamic volume
378c2ecf20Sopenharmony_ci * @UBI_VID_STATIC: static volume
388c2ecf20Sopenharmony_ci */
398c2ecf20Sopenharmony_cienum {
408c2ecf20Sopenharmony_ci	UBI_VID_DYNAMIC = 1,
418c2ecf20Sopenharmony_ci	UBI_VID_STATIC  = 2
428c2ecf20Sopenharmony_ci};
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci/*
458c2ecf20Sopenharmony_ci * Volume flags used in the volume table record.
468c2ecf20Sopenharmony_ci *
478c2ecf20Sopenharmony_ci * @UBI_VTBL_AUTORESIZE_FLG: auto-resize this volume
488c2ecf20Sopenharmony_ci * @UBI_VTBL_SKIP_CRC_CHECK_FLG: skip the CRC check done on a static volume at
498c2ecf20Sopenharmony_ci *				 open time. Should only be set on volumes that
508c2ecf20Sopenharmony_ci *				 are used by upper layers doing this kind of
518c2ecf20Sopenharmony_ci *				 check. Main use-case for this flag is
528c2ecf20Sopenharmony_ci *				 boot-time reduction
538c2ecf20Sopenharmony_ci *
548c2ecf20Sopenharmony_ci * %UBI_VTBL_AUTORESIZE_FLG flag can be set only for one volume in the volume
558c2ecf20Sopenharmony_ci * table. UBI automatically re-sizes the volume which has this flag and makes
568c2ecf20Sopenharmony_ci * the volume to be of largest possible size. This means that if after the
578c2ecf20Sopenharmony_ci * initialization UBI finds out that there are available physical eraseblocks
588c2ecf20Sopenharmony_ci * present on the device, it automatically appends all of them to the volume
598c2ecf20Sopenharmony_ci * (the physical eraseblocks reserved for bad eraseblocks handling and other
608c2ecf20Sopenharmony_ci * reserved physical eraseblocks are not taken). So, if there is a volume with
618c2ecf20Sopenharmony_ci * the %UBI_VTBL_AUTORESIZE_FLG flag set, the amount of available logical
628c2ecf20Sopenharmony_ci * eraseblocks will be zero after UBI is loaded, because all of them will be
638c2ecf20Sopenharmony_ci * reserved for this volume. Note, the %UBI_VTBL_AUTORESIZE_FLG bit is cleared
648c2ecf20Sopenharmony_ci * after the volume had been initialized.
658c2ecf20Sopenharmony_ci *
668c2ecf20Sopenharmony_ci * The auto-resize feature is useful for device production purposes. For
678c2ecf20Sopenharmony_ci * example, different NAND flash chips may have different amount of initial bad
688c2ecf20Sopenharmony_ci * eraseblocks, depending of particular chip instance. Manufacturers of NAND
698c2ecf20Sopenharmony_ci * chips usually guarantee that the amount of initial bad eraseblocks does not
708c2ecf20Sopenharmony_ci * exceed certain percent, e.g. 2%. When one creates an UBI image which will be
718c2ecf20Sopenharmony_ci * flashed to the end devices in production, he does not know the exact amount
728c2ecf20Sopenharmony_ci * of good physical eraseblocks the NAND chip on the device will have, but this
738c2ecf20Sopenharmony_ci * number is required to calculate the volume sized and put them to the volume
748c2ecf20Sopenharmony_ci * table of the UBI image. In this case, one of the volumes (e.g., the one
758c2ecf20Sopenharmony_ci * which will store the root file system) is marked as "auto-resizable", and
768c2ecf20Sopenharmony_ci * UBI will adjust its size on the first boot if needed.
778c2ecf20Sopenharmony_ci *
788c2ecf20Sopenharmony_ci * Note, first UBI reserves some amount of physical eraseblocks for bad
798c2ecf20Sopenharmony_ci * eraseblock handling, and then re-sizes the volume, not vice-versa. This
808c2ecf20Sopenharmony_ci * means that the pool of reserved physical eraseblocks will always be present.
818c2ecf20Sopenharmony_ci */
828c2ecf20Sopenharmony_cienum {
838c2ecf20Sopenharmony_ci	UBI_VTBL_AUTORESIZE_FLG = 0x01,
848c2ecf20Sopenharmony_ci	UBI_VTBL_SKIP_CRC_CHECK_FLG = 0x02,
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_ci/*
888c2ecf20Sopenharmony_ci * Compatibility constants used by internal volumes.
898c2ecf20Sopenharmony_ci *
908c2ecf20Sopenharmony_ci * @UBI_COMPAT_DELETE: delete this internal volume before anything is written
918c2ecf20Sopenharmony_ci *                     to the flash
928c2ecf20Sopenharmony_ci * @UBI_COMPAT_RO: attach this device in read-only mode
938c2ecf20Sopenharmony_ci * @UBI_COMPAT_PRESERVE: preserve this internal volume - do not touch its
948c2ecf20Sopenharmony_ci *                       physical eraseblocks, don't allow the wear-leveling
958c2ecf20Sopenharmony_ci *                       sub-system to move them
968c2ecf20Sopenharmony_ci * @UBI_COMPAT_REJECT: reject this UBI image
978c2ecf20Sopenharmony_ci */
988c2ecf20Sopenharmony_cienum {
998c2ecf20Sopenharmony_ci	UBI_COMPAT_DELETE   = 1,
1008c2ecf20Sopenharmony_ci	UBI_COMPAT_RO       = 2,
1018c2ecf20Sopenharmony_ci	UBI_COMPAT_PRESERVE = 4,
1028c2ecf20Sopenharmony_ci	UBI_COMPAT_REJECT   = 5
1038c2ecf20Sopenharmony_ci};
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci/* Sizes of UBI headers */
1068c2ecf20Sopenharmony_ci#define UBI_EC_HDR_SIZE  sizeof(struct ubi_ec_hdr)
1078c2ecf20Sopenharmony_ci#define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr)
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* Sizes of UBI headers without the ending CRC */
1108c2ecf20Sopenharmony_ci#define UBI_EC_HDR_SIZE_CRC  (UBI_EC_HDR_SIZE  - sizeof(__be32))
1118c2ecf20Sopenharmony_ci#define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(__be32))
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci/**
1148c2ecf20Sopenharmony_ci * struct ubi_ec_hdr - UBI erase counter header.
1158c2ecf20Sopenharmony_ci * @magic: erase counter header magic number (%UBI_EC_HDR_MAGIC)
1168c2ecf20Sopenharmony_ci * @version: version of UBI implementation which is supposed to accept this
1178c2ecf20Sopenharmony_ci *           UBI image
1188c2ecf20Sopenharmony_ci * @padding1: reserved for future, zeroes
1198c2ecf20Sopenharmony_ci * @ec: the erase counter
1208c2ecf20Sopenharmony_ci * @vid_hdr_offset: where the VID header starts
1218c2ecf20Sopenharmony_ci * @data_offset: where the user data start
1228c2ecf20Sopenharmony_ci * @image_seq: image sequence number
1238c2ecf20Sopenharmony_ci * @padding2: reserved for future, zeroes
1248c2ecf20Sopenharmony_ci * @hdr_crc: erase counter header CRC checksum
1258c2ecf20Sopenharmony_ci *
1268c2ecf20Sopenharmony_ci * The erase counter header takes 64 bytes and has a plenty of unused space for
1278c2ecf20Sopenharmony_ci * future usage. The unused fields are zeroed. The @version field is used to
1288c2ecf20Sopenharmony_ci * indicate the version of UBI implementation which is supposed to be able to
1298c2ecf20Sopenharmony_ci * work with this UBI image. If @version is greater than the current UBI
1308c2ecf20Sopenharmony_ci * version, the image is rejected. This may be useful in future if something
1318c2ecf20Sopenharmony_ci * is changed radically. This field is duplicated in the volume identifier
1328c2ecf20Sopenharmony_ci * header.
1338c2ecf20Sopenharmony_ci *
1348c2ecf20Sopenharmony_ci * The @vid_hdr_offset and @data_offset fields contain the offset of the the
1358c2ecf20Sopenharmony_ci * volume identifier header and user data, relative to the beginning of the
1368c2ecf20Sopenharmony_ci * physical eraseblock. These values have to be the same for all physical
1378c2ecf20Sopenharmony_ci * eraseblocks.
1388c2ecf20Sopenharmony_ci *
1398c2ecf20Sopenharmony_ci * The @image_seq field is used to validate a UBI image that has been prepared
1408c2ecf20Sopenharmony_ci * for a UBI device. The @image_seq value can be any value, but it must be the
1418c2ecf20Sopenharmony_ci * same on all eraseblocks. UBI will ensure that all new erase counter headers
1428c2ecf20Sopenharmony_ci * also contain this value, and will check the value when attaching the flash.
1438c2ecf20Sopenharmony_ci * One way to make use of @image_seq is to increase its value by one every time
1448c2ecf20Sopenharmony_ci * an image is flashed over an existing image, then, if the flashing does not
1458c2ecf20Sopenharmony_ci * complete, UBI will detect the error when attaching the media.
1468c2ecf20Sopenharmony_ci */
1478c2ecf20Sopenharmony_cistruct ubi_ec_hdr {
1488c2ecf20Sopenharmony_ci	__be32  magic;
1498c2ecf20Sopenharmony_ci	__u8    version;
1508c2ecf20Sopenharmony_ci	__u8    padding1[3];
1518c2ecf20Sopenharmony_ci	__be64  ec; /* Warning: the current limit is 31-bit anyway! */
1528c2ecf20Sopenharmony_ci	__be32  vid_hdr_offset;
1538c2ecf20Sopenharmony_ci	__be32  data_offset;
1548c2ecf20Sopenharmony_ci	__be32  image_seq;
1558c2ecf20Sopenharmony_ci	__u8    padding2[32];
1568c2ecf20Sopenharmony_ci	__be32  hdr_crc;
1578c2ecf20Sopenharmony_ci} __packed;
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/**
1608c2ecf20Sopenharmony_ci * struct ubi_vid_hdr - on-flash UBI volume identifier header.
1618c2ecf20Sopenharmony_ci * @magic: volume identifier header magic number (%UBI_VID_HDR_MAGIC)
1628c2ecf20Sopenharmony_ci * @version: UBI implementation version which is supposed to accept this UBI
1638c2ecf20Sopenharmony_ci *           image (%UBI_VERSION)
1648c2ecf20Sopenharmony_ci * @vol_type: volume type (%UBI_VID_DYNAMIC or %UBI_VID_STATIC)
1658c2ecf20Sopenharmony_ci * @copy_flag: if this logical eraseblock was copied from another physical
1668c2ecf20Sopenharmony_ci *             eraseblock (for wear-leveling reasons)
1678c2ecf20Sopenharmony_ci * @compat: compatibility of this volume (%0, %UBI_COMPAT_DELETE,
1688c2ecf20Sopenharmony_ci *          %UBI_COMPAT_IGNORE, %UBI_COMPAT_PRESERVE, or %UBI_COMPAT_REJECT)
1698c2ecf20Sopenharmony_ci * @vol_id: ID of this volume
1708c2ecf20Sopenharmony_ci * @lnum: logical eraseblock number
1718c2ecf20Sopenharmony_ci * @padding1: reserved for future, zeroes
1728c2ecf20Sopenharmony_ci * @data_size: how many bytes of data this logical eraseblock contains
1738c2ecf20Sopenharmony_ci * @used_ebs: total number of used logical eraseblocks in this volume
1748c2ecf20Sopenharmony_ci * @data_pad: how many bytes at the end of this physical eraseblock are not
1758c2ecf20Sopenharmony_ci *            used
1768c2ecf20Sopenharmony_ci * @data_crc: CRC checksum of the data stored in this logical eraseblock
1778c2ecf20Sopenharmony_ci * @padding2: reserved for future, zeroes
1788c2ecf20Sopenharmony_ci * @sqnum: sequence number
1798c2ecf20Sopenharmony_ci * @padding3: reserved for future, zeroes
1808c2ecf20Sopenharmony_ci * @hdr_crc: volume identifier header CRC checksum
1818c2ecf20Sopenharmony_ci *
1828c2ecf20Sopenharmony_ci * The @sqnum is the value of the global sequence counter at the time when this
1838c2ecf20Sopenharmony_ci * VID header was created. The global sequence counter is incremented each time
1848c2ecf20Sopenharmony_ci * UBI writes a new VID header to the flash, i.e. when it maps a logical
1858c2ecf20Sopenharmony_ci * eraseblock to a new physical eraseblock. The global sequence counter is an
1868c2ecf20Sopenharmony_ci * unsigned 64-bit integer and we assume it never overflows. The @sqnum
1878c2ecf20Sopenharmony_ci * (sequence number) is used to distinguish between older and newer versions of
1888c2ecf20Sopenharmony_ci * logical eraseblocks.
1898c2ecf20Sopenharmony_ci *
1908c2ecf20Sopenharmony_ci * There are 2 situations when there may be more than one physical eraseblock
1918c2ecf20Sopenharmony_ci * corresponding to the same logical eraseblock, i.e., having the same @vol_id
1928c2ecf20Sopenharmony_ci * and @lnum values in the volume identifier header. Suppose we have a logical
1938c2ecf20Sopenharmony_ci * eraseblock L and it is mapped to the physical eraseblock P.
1948c2ecf20Sopenharmony_ci *
1958c2ecf20Sopenharmony_ci * 1. Because UBI may erase physical eraseblocks asynchronously, the following
1968c2ecf20Sopenharmony_ci * situation is possible: L is asynchronously erased, so P is scheduled for
1978c2ecf20Sopenharmony_ci * erasure, then L is written to,i.e. mapped to another physical eraseblock P1,
1988c2ecf20Sopenharmony_ci * so P1 is written to, then an unclean reboot happens. Result - there are 2
1998c2ecf20Sopenharmony_ci * physical eraseblocks P and P1 corresponding to the same logical eraseblock
2008c2ecf20Sopenharmony_ci * L. But P1 has greater sequence number, so UBI picks P1 when it attaches the
2018c2ecf20Sopenharmony_ci * flash.
2028c2ecf20Sopenharmony_ci *
2038c2ecf20Sopenharmony_ci * 2. From time to time UBI moves logical eraseblocks to other physical
2048c2ecf20Sopenharmony_ci * eraseblocks for wear-leveling reasons. If, for example, UBI moves L from P
2058c2ecf20Sopenharmony_ci * to P1, and an unclean reboot happens before P is physically erased, there
2068c2ecf20Sopenharmony_ci * are two physical eraseblocks P and P1 corresponding to L and UBI has to
2078c2ecf20Sopenharmony_ci * select one of them when the flash is attached. The @sqnum field says which
2088c2ecf20Sopenharmony_ci * PEB is the original (obviously P will have lower @sqnum) and the copy. But
2098c2ecf20Sopenharmony_ci * it is not enough to select the physical eraseblock with the higher sequence
2108c2ecf20Sopenharmony_ci * number, because the unclean reboot could have happen in the middle of the
2118c2ecf20Sopenharmony_ci * copying process, so the data in P is corrupted. It is also not enough to
2128c2ecf20Sopenharmony_ci * just select the physical eraseblock with lower sequence number, because the
2138c2ecf20Sopenharmony_ci * data there may be old (consider a case if more data was added to P1 after
2148c2ecf20Sopenharmony_ci * the copying). Moreover, the unclean reboot may happen when the erasure of P
2158c2ecf20Sopenharmony_ci * was just started, so it result in unstable P, which is "mostly" OK, but
2168c2ecf20Sopenharmony_ci * still has unstable bits.
2178c2ecf20Sopenharmony_ci *
2188c2ecf20Sopenharmony_ci * UBI uses the @copy_flag field to indicate that this logical eraseblock is a
2198c2ecf20Sopenharmony_ci * copy. UBI also calculates data CRC when the data is moved and stores it at
2208c2ecf20Sopenharmony_ci * the @data_crc field of the copy (P1). So when UBI needs to pick one physical
2218c2ecf20Sopenharmony_ci * eraseblock of two (P or P1), the @copy_flag of the newer one (P1) is
2228c2ecf20Sopenharmony_ci * examined. If it is cleared, the situation is simple and the newer one is
2238c2ecf20Sopenharmony_ci * picked. If it is set, the data CRC of the copy (P1) is examined. If the CRC
2248c2ecf20Sopenharmony_ci * checksum is correct, this physical eraseblock is selected (P1). Otherwise
2258c2ecf20Sopenharmony_ci * the older one (P) is selected.
2268c2ecf20Sopenharmony_ci *
2278c2ecf20Sopenharmony_ci * There are 2 sorts of volumes in UBI: user volumes and internal volumes.
2288c2ecf20Sopenharmony_ci * Internal volumes are not seen from outside and are used for various internal
2298c2ecf20Sopenharmony_ci * UBI purposes. In this implementation there is only one internal volume - the
2308c2ecf20Sopenharmony_ci * layout volume. Internal volumes are the main mechanism of UBI extensions.
2318c2ecf20Sopenharmony_ci * For example, in future one may introduce a journal internal volume. Internal
2328c2ecf20Sopenharmony_ci * volumes have their own reserved range of IDs.
2338c2ecf20Sopenharmony_ci *
2348c2ecf20Sopenharmony_ci * The @compat field is only used for internal volumes and contains the "degree
2358c2ecf20Sopenharmony_ci * of their compatibility". It is always zero for user volumes. This field
2368c2ecf20Sopenharmony_ci * provides a mechanism to introduce UBI extensions and to be still compatible
2378c2ecf20Sopenharmony_ci * with older UBI binaries. For example, if someone introduced a journal in
2388c2ecf20Sopenharmony_ci * future, he would probably use %UBI_COMPAT_DELETE compatibility for the
2398c2ecf20Sopenharmony_ci * journal volume.  And in this case, older UBI binaries, which know nothing
2408c2ecf20Sopenharmony_ci * about the journal volume, would just delete this volume and work perfectly
2418c2ecf20Sopenharmony_ci * fine. This is similar to what Ext2fs does when it is fed by an Ext3fs image
2428c2ecf20Sopenharmony_ci * - it just ignores the Ext3fs journal.
2438c2ecf20Sopenharmony_ci *
2448c2ecf20Sopenharmony_ci * The @data_crc field contains the CRC checksum of the contents of the logical
2458c2ecf20Sopenharmony_ci * eraseblock if this is a static volume. In case of dynamic volumes, it does
2468c2ecf20Sopenharmony_ci * not contain the CRC checksum as a rule. The only exception is when the
2478c2ecf20Sopenharmony_ci * data of the physical eraseblock was moved by the wear-leveling sub-system,
2488c2ecf20Sopenharmony_ci * then the wear-leveling sub-system calculates the data CRC and stores it in
2498c2ecf20Sopenharmony_ci * the @data_crc field. And of course, the @copy_flag is %in this case.
2508c2ecf20Sopenharmony_ci *
2518c2ecf20Sopenharmony_ci * The @data_size field is used only for static volumes because UBI has to know
2528c2ecf20Sopenharmony_ci * how many bytes of data are stored in this eraseblock. For dynamic volumes,
2538c2ecf20Sopenharmony_ci * this field usually contains zero. The only exception is when the data of the
2548c2ecf20Sopenharmony_ci * physical eraseblock was moved to another physical eraseblock for
2558c2ecf20Sopenharmony_ci * wear-leveling reasons. In this case, UBI calculates CRC checksum of the
2568c2ecf20Sopenharmony_ci * contents and uses both @data_crc and @data_size fields. In this case, the
2578c2ecf20Sopenharmony_ci * @data_size field contains data size.
2588c2ecf20Sopenharmony_ci *
2598c2ecf20Sopenharmony_ci * The @used_ebs field is used only for static volumes and indicates how many
2608c2ecf20Sopenharmony_ci * eraseblocks the data of the volume takes. For dynamic volumes this field is
2618c2ecf20Sopenharmony_ci * not used and always contains zero.
2628c2ecf20Sopenharmony_ci *
2638c2ecf20Sopenharmony_ci * The @data_pad is calculated when volumes are created using the alignment
2648c2ecf20Sopenharmony_ci * parameter. So, effectively, the @data_pad field reduces the size of logical
2658c2ecf20Sopenharmony_ci * eraseblocks of this volume. This is very handy when one uses block-oriented
2668c2ecf20Sopenharmony_ci * software (say, cramfs) on top of the UBI volume.
2678c2ecf20Sopenharmony_ci */
2688c2ecf20Sopenharmony_cistruct ubi_vid_hdr {
2698c2ecf20Sopenharmony_ci	__be32  magic;
2708c2ecf20Sopenharmony_ci	__u8    version;
2718c2ecf20Sopenharmony_ci	__u8    vol_type;
2728c2ecf20Sopenharmony_ci	__u8    copy_flag;
2738c2ecf20Sopenharmony_ci	__u8    compat;
2748c2ecf20Sopenharmony_ci	__be32  vol_id;
2758c2ecf20Sopenharmony_ci	__be32  lnum;
2768c2ecf20Sopenharmony_ci	__u8    padding1[4];
2778c2ecf20Sopenharmony_ci	__be32  data_size;
2788c2ecf20Sopenharmony_ci	__be32  used_ebs;
2798c2ecf20Sopenharmony_ci	__be32  data_pad;
2808c2ecf20Sopenharmony_ci	__be32  data_crc;
2818c2ecf20Sopenharmony_ci	__u8    padding2[4];
2828c2ecf20Sopenharmony_ci	__be64  sqnum;
2838c2ecf20Sopenharmony_ci	__u8    padding3[12];
2848c2ecf20Sopenharmony_ci	__be32  hdr_crc;
2858c2ecf20Sopenharmony_ci} __packed;
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci/* Internal UBI volumes count */
2888c2ecf20Sopenharmony_ci#define UBI_INT_VOL_COUNT 1
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci/*
2918c2ecf20Sopenharmony_ci * Starting ID of internal volumes: 0x7fffefff.
2928c2ecf20Sopenharmony_ci * There is reserved room for 4096 internal volumes.
2938c2ecf20Sopenharmony_ci */
2948c2ecf20Sopenharmony_ci#define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096)
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci/* The layout volume contains the volume table */
2978c2ecf20Sopenharmony_ci
2988c2ecf20Sopenharmony_ci#define UBI_LAYOUT_VOLUME_ID     UBI_INTERNAL_VOL_START
2998c2ecf20Sopenharmony_ci#define UBI_LAYOUT_VOLUME_TYPE   UBI_VID_DYNAMIC
3008c2ecf20Sopenharmony_ci#define UBI_LAYOUT_VOLUME_ALIGN  1
3018c2ecf20Sopenharmony_ci#define UBI_LAYOUT_VOLUME_EBS    2
3028c2ecf20Sopenharmony_ci#define UBI_LAYOUT_VOLUME_NAME   "layout volume"
3038c2ecf20Sopenharmony_ci#define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci/* The maximum number of volumes per one UBI device */
3068c2ecf20Sopenharmony_ci#define UBI_MAX_VOLUMES 128
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci/* The maximum volume name length */
3098c2ecf20Sopenharmony_ci#define UBI_VOL_NAME_MAX 127
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci/* Size of the volume table record */
3128c2ecf20Sopenharmony_ci#define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vtbl_record)
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci/* Size of the volume table record without the ending CRC */
3158c2ecf20Sopenharmony_ci#define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(__be32))
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci/**
3188c2ecf20Sopenharmony_ci * struct ubi_vtbl_record - a record in the volume table.
3198c2ecf20Sopenharmony_ci * @reserved_pebs: how many physical eraseblocks are reserved for this volume
3208c2ecf20Sopenharmony_ci * @alignment: volume alignment
3218c2ecf20Sopenharmony_ci * @data_pad: how many bytes are unused at the end of the each physical
3228c2ecf20Sopenharmony_ci * eraseblock to satisfy the requested alignment
3238c2ecf20Sopenharmony_ci * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
3248c2ecf20Sopenharmony_ci * @upd_marker: if volume update was started but not finished
3258c2ecf20Sopenharmony_ci * @name_len: volume name length
3268c2ecf20Sopenharmony_ci * @name: the volume name
3278c2ecf20Sopenharmony_ci * @flags: volume flags (%UBI_VTBL_AUTORESIZE_FLG)
3288c2ecf20Sopenharmony_ci * @padding: reserved, zeroes
3298c2ecf20Sopenharmony_ci * @crc: a CRC32 checksum of the record
3308c2ecf20Sopenharmony_ci *
3318c2ecf20Sopenharmony_ci * The volume table records are stored in the volume table, which is stored in
3328c2ecf20Sopenharmony_ci * the layout volume. The layout volume consists of 2 logical eraseblock, each
3338c2ecf20Sopenharmony_ci * of which contains a copy of the volume table (i.e., the volume table is
3348c2ecf20Sopenharmony_ci * duplicated). The volume table is an array of &struct ubi_vtbl_record
3358c2ecf20Sopenharmony_ci * objects indexed by the volume ID.
3368c2ecf20Sopenharmony_ci *
3378c2ecf20Sopenharmony_ci * If the size of the logical eraseblock is large enough to fit
3388c2ecf20Sopenharmony_ci * %UBI_MAX_VOLUMES records, the volume table contains %UBI_MAX_VOLUMES
3398c2ecf20Sopenharmony_ci * records. Otherwise, it contains as many records as it can fit (i.e., size of
3408c2ecf20Sopenharmony_ci * logical eraseblock divided by sizeof(struct ubi_vtbl_record)).
3418c2ecf20Sopenharmony_ci *
3428c2ecf20Sopenharmony_ci * The @upd_marker flag is used to implement volume update. It is set to %1
3438c2ecf20Sopenharmony_ci * before update and set to %0 after the update. So if the update operation was
3448c2ecf20Sopenharmony_ci * interrupted, UBI knows that the volume is corrupted.
3458c2ecf20Sopenharmony_ci *
3468c2ecf20Sopenharmony_ci * The @alignment field is specified when the volume is created and cannot be
3478c2ecf20Sopenharmony_ci * later changed. It may be useful, for example, when a block-oriented file
3488c2ecf20Sopenharmony_ci * system works on top of UBI. The @data_pad field is calculated using the
3498c2ecf20Sopenharmony_ci * logical eraseblock size and @alignment. The alignment must be multiple to the
3508c2ecf20Sopenharmony_ci * minimal flash I/O unit. If @alignment is 1, all the available space of
3518c2ecf20Sopenharmony_ci * the physical eraseblocks is used.
3528c2ecf20Sopenharmony_ci *
3538c2ecf20Sopenharmony_ci * Empty records contain all zeroes and the CRC checksum of those zeroes.
3548c2ecf20Sopenharmony_ci */
3558c2ecf20Sopenharmony_cistruct ubi_vtbl_record {
3568c2ecf20Sopenharmony_ci	__be32  reserved_pebs;
3578c2ecf20Sopenharmony_ci	__be32  alignment;
3588c2ecf20Sopenharmony_ci	__be32  data_pad;
3598c2ecf20Sopenharmony_ci	__u8    vol_type;
3608c2ecf20Sopenharmony_ci	__u8    upd_marker;
3618c2ecf20Sopenharmony_ci	__be16  name_len;
3628c2ecf20Sopenharmony_ci	__u8    name[UBI_VOL_NAME_MAX+1];
3638c2ecf20Sopenharmony_ci	__u8    flags;
3648c2ecf20Sopenharmony_ci	__u8    padding[23];
3658c2ecf20Sopenharmony_ci	__be32  crc;
3668c2ecf20Sopenharmony_ci} __packed;
3678c2ecf20Sopenharmony_ci
3688c2ecf20Sopenharmony_ci/* UBI fastmap on-flash data structures */
3698c2ecf20Sopenharmony_ci
3708c2ecf20Sopenharmony_ci#define UBI_FM_SB_VOLUME_ID	(UBI_LAYOUT_VOLUME_ID + 1)
3718c2ecf20Sopenharmony_ci#define UBI_FM_DATA_VOLUME_ID	(UBI_LAYOUT_VOLUME_ID + 2)
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci/* fastmap on-flash data structure format version */
3748c2ecf20Sopenharmony_ci#define UBI_FM_FMT_VERSION	1
3758c2ecf20Sopenharmony_ci
3768c2ecf20Sopenharmony_ci#define UBI_FM_SB_MAGIC		0x7B11D69F
3778c2ecf20Sopenharmony_ci#define UBI_FM_HDR_MAGIC	0xD4B82EF7
3788c2ecf20Sopenharmony_ci#define UBI_FM_VHDR_MAGIC	0xFA370ED1
3798c2ecf20Sopenharmony_ci#define UBI_FM_POOL_MAGIC	0x67AF4D08
3808c2ecf20Sopenharmony_ci#define UBI_FM_EBA_MAGIC	0xf0c040a8
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_ci/* A fastmap super block can be located between PEB 0 and
3838c2ecf20Sopenharmony_ci * UBI_FM_MAX_START */
3848c2ecf20Sopenharmony_ci#define UBI_FM_MAX_START	64
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci/* A fastmap can use up to UBI_FM_MAX_BLOCKS PEBs */
3878c2ecf20Sopenharmony_ci#define UBI_FM_MAX_BLOCKS	32
3888c2ecf20Sopenharmony_ci
3898c2ecf20Sopenharmony_ci/* 5% of the total number of PEBs have to be scanned while attaching
3908c2ecf20Sopenharmony_ci * from a fastmap.
3918c2ecf20Sopenharmony_ci * But the size of this pool is limited to be between UBI_FM_MIN_POOL_SIZE and
3928c2ecf20Sopenharmony_ci * UBI_FM_MAX_POOL_SIZE */
3938c2ecf20Sopenharmony_ci#define UBI_FM_MIN_POOL_SIZE	8
3948c2ecf20Sopenharmony_ci#define UBI_FM_MAX_POOL_SIZE	256
3958c2ecf20Sopenharmony_ci
3968c2ecf20Sopenharmony_ci/**
3978c2ecf20Sopenharmony_ci * struct ubi_fm_sb - UBI fastmap super block
3988c2ecf20Sopenharmony_ci * @magic: fastmap super block magic number (%UBI_FM_SB_MAGIC)
3998c2ecf20Sopenharmony_ci * @version: format version of this fastmap
4008c2ecf20Sopenharmony_ci * @data_crc: CRC over the fastmap data
4018c2ecf20Sopenharmony_ci * @used_blocks: number of PEBs used by this fastmap
4028c2ecf20Sopenharmony_ci * @block_loc: an array containing the location of all PEBs of the fastmap
4038c2ecf20Sopenharmony_ci * @block_ec: the erase counter of each used PEB
4048c2ecf20Sopenharmony_ci * @sqnum: highest sequence number value at the time while taking the fastmap
4058c2ecf20Sopenharmony_ci *
4068c2ecf20Sopenharmony_ci */
4078c2ecf20Sopenharmony_cistruct ubi_fm_sb {
4088c2ecf20Sopenharmony_ci	__be32 magic;
4098c2ecf20Sopenharmony_ci	__u8 version;
4108c2ecf20Sopenharmony_ci	__u8 padding1[3];
4118c2ecf20Sopenharmony_ci	__be32 data_crc;
4128c2ecf20Sopenharmony_ci	__be32 used_blocks;
4138c2ecf20Sopenharmony_ci	__be32 block_loc[UBI_FM_MAX_BLOCKS];
4148c2ecf20Sopenharmony_ci	__be32 block_ec[UBI_FM_MAX_BLOCKS];
4158c2ecf20Sopenharmony_ci	__be64 sqnum;
4168c2ecf20Sopenharmony_ci	__u8 padding2[32];
4178c2ecf20Sopenharmony_ci} __packed;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci/**
4208c2ecf20Sopenharmony_ci * struct ubi_fm_hdr - header of the fastmap data set
4218c2ecf20Sopenharmony_ci * @magic: fastmap header magic number (%UBI_FM_HDR_MAGIC)
4228c2ecf20Sopenharmony_ci * @free_peb_count: number of free PEBs known by this fastmap
4238c2ecf20Sopenharmony_ci * @used_peb_count: number of used PEBs known by this fastmap
4248c2ecf20Sopenharmony_ci * @scrub_peb_count: number of to be scrubbed PEBs known by this fastmap
4258c2ecf20Sopenharmony_ci * @bad_peb_count: number of bad PEBs known by this fastmap
4268c2ecf20Sopenharmony_ci * @erase_peb_count: number of bad PEBs which have to be erased
4278c2ecf20Sopenharmony_ci * @vol_count: number of UBI volumes known by this fastmap
4288c2ecf20Sopenharmony_ci */
4298c2ecf20Sopenharmony_cistruct ubi_fm_hdr {
4308c2ecf20Sopenharmony_ci	__be32 magic;
4318c2ecf20Sopenharmony_ci	__be32 free_peb_count;
4328c2ecf20Sopenharmony_ci	__be32 used_peb_count;
4338c2ecf20Sopenharmony_ci	__be32 scrub_peb_count;
4348c2ecf20Sopenharmony_ci	__be32 bad_peb_count;
4358c2ecf20Sopenharmony_ci	__be32 erase_peb_count;
4368c2ecf20Sopenharmony_ci	__be32 vol_count;
4378c2ecf20Sopenharmony_ci	__u8 padding[4];
4388c2ecf20Sopenharmony_ci} __packed;
4398c2ecf20Sopenharmony_ci
4408c2ecf20Sopenharmony_ci/* struct ubi_fm_hdr is followed by two struct ubi_fm_scan_pool */
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci/**
4438c2ecf20Sopenharmony_ci * struct ubi_fm_scan_pool - Fastmap pool PEBs to be scanned while attaching
4448c2ecf20Sopenharmony_ci * @magic: pool magic numer (%UBI_FM_POOL_MAGIC)
4458c2ecf20Sopenharmony_ci * @size: current pool size
4468c2ecf20Sopenharmony_ci * @max_size: maximal pool size
4478c2ecf20Sopenharmony_ci * @pebs: an array containing the location of all PEBs in this pool
4488c2ecf20Sopenharmony_ci */
4498c2ecf20Sopenharmony_cistruct ubi_fm_scan_pool {
4508c2ecf20Sopenharmony_ci	__be32 magic;
4518c2ecf20Sopenharmony_ci	__be16 size;
4528c2ecf20Sopenharmony_ci	__be16 max_size;
4538c2ecf20Sopenharmony_ci	__be32 pebs[UBI_FM_MAX_POOL_SIZE];
4548c2ecf20Sopenharmony_ci	__be32 padding[4];
4558c2ecf20Sopenharmony_ci} __packed;
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_ci/* ubi_fm_scan_pool is followed by nfree+nused struct ubi_fm_ec records */
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci/**
4608c2ecf20Sopenharmony_ci * struct ubi_fm_ec - stores the erase counter of a PEB
4618c2ecf20Sopenharmony_ci * @pnum: PEB number
4628c2ecf20Sopenharmony_ci * @ec: ec of this PEB
4638c2ecf20Sopenharmony_ci */
4648c2ecf20Sopenharmony_cistruct ubi_fm_ec {
4658c2ecf20Sopenharmony_ci	__be32 pnum;
4668c2ecf20Sopenharmony_ci	__be32 ec;
4678c2ecf20Sopenharmony_ci} __packed;
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_ci/**
4708c2ecf20Sopenharmony_ci * struct ubi_fm_volhdr - Fastmap volume header
4718c2ecf20Sopenharmony_ci * it identifies the start of an eba table
4728c2ecf20Sopenharmony_ci * @magic: Fastmap volume header magic number (%UBI_FM_VHDR_MAGIC)
4738c2ecf20Sopenharmony_ci * @vol_id: volume id of the fastmapped volume
4748c2ecf20Sopenharmony_ci * @vol_type: type of the fastmapped volume
4758c2ecf20Sopenharmony_ci * @data_pad: data_pad value of the fastmapped volume
4768c2ecf20Sopenharmony_ci * @used_ebs: number of used LEBs within this volume
4778c2ecf20Sopenharmony_ci * @last_eb_bytes: number of bytes used in the last LEB
4788c2ecf20Sopenharmony_ci */
4798c2ecf20Sopenharmony_cistruct ubi_fm_volhdr {
4808c2ecf20Sopenharmony_ci	__be32 magic;
4818c2ecf20Sopenharmony_ci	__be32 vol_id;
4828c2ecf20Sopenharmony_ci	__u8 vol_type;
4838c2ecf20Sopenharmony_ci	__u8 padding1[3];
4848c2ecf20Sopenharmony_ci	__be32 data_pad;
4858c2ecf20Sopenharmony_ci	__be32 used_ebs;
4868c2ecf20Sopenharmony_ci	__be32 last_eb_bytes;
4878c2ecf20Sopenharmony_ci	__u8 padding2[8];
4888c2ecf20Sopenharmony_ci} __packed;
4898c2ecf20Sopenharmony_ci
4908c2ecf20Sopenharmony_ci/* struct ubi_fm_volhdr is followed by one struct ubi_fm_eba records */
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_ci/**
4938c2ecf20Sopenharmony_ci * struct ubi_fm_eba - denotes an association between a PEB and LEB
4948c2ecf20Sopenharmony_ci * @magic: EBA table magic number
4958c2ecf20Sopenharmony_ci * @reserved_pebs: number of table entries
4968c2ecf20Sopenharmony_ci * @pnum: PEB number of LEB (LEB is the index)
4978c2ecf20Sopenharmony_ci */
4988c2ecf20Sopenharmony_cistruct ubi_fm_eba {
4998c2ecf20Sopenharmony_ci	__be32 magic;
5008c2ecf20Sopenharmony_ci	__be32 reserved_pebs;
5018c2ecf20Sopenharmony_ci	__be32 pnum[];
5028c2ecf20Sopenharmony_ci} __packed;
5038c2ecf20Sopenharmony_ci#endif /* !__UBI_MEDIA_H__ */
504