ubi-header.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 2 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  12. * the GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. *
  18. * Authors: Artem Bityutskiy (Битюцкий Артём)
  19. * Thomas Gleixner
  20. * Frank Haverkamp
  21. * Oliver Lohmann
  22. * Andreas Arnez
  23. */
  24. /*
  25. * This file defines the layout of UBI headers and all the other UBI on-flash
  26. * data structures. May be included by user-space.
  27. */
  28. #ifndef __UBI_HEADER_H__
  29. #define __UBI_HEADER_H__
  30. #include <asm/byteorder.h>
  31. /* The version of UBI images supported by this implementation */
  32. #define UBI_VERSION 1
  33. /* The highest erase counter value supported by this implementation */
  34. #define UBI_MAX_ERASECOUNTER 0x7FFFFFFF
  35. /* The initial CRC32 value used when calculating CRC checksums */
  36. #define UBI_CRC32_INIT 0xFFFFFFFFU
  37. /* Erase counter header magic number (ASCII "UBI#") */
  38. #define UBI_EC_HDR_MAGIC 0x55424923
  39. /* Volume identifier header magic number (ASCII "UBI!") */
  40. #define UBI_VID_HDR_MAGIC 0x55424921
  41. /*
  42. * Volume type constants used in the volume identifier header.
  43. *
  44. * @UBI_VID_DYNAMIC: dynamic volume
  45. * @UBI_VID_STATIC: static volume
  46. */
  47. enum {
  48. UBI_VID_DYNAMIC = 1,
  49. UBI_VID_STATIC = 2
  50. };
  51. /*
  52. * Compatibility constants used by internal volumes.
  53. *
  54. * @UBI_COMPAT_DELETE: delete this internal volume before anything is written
  55. * to the flash
  56. * @UBI_COMPAT_RO: attach this device in read-only mode
  57. * @UBI_COMPAT_PRESERVE: preserve this internal volume - do not touch its
  58. * physical eraseblocks, don't allow the wear-leveling unit to move them
  59. * @UBI_COMPAT_REJECT: reject this UBI image
  60. */
  61. enum {
  62. UBI_COMPAT_DELETE = 1,
  63. UBI_COMPAT_RO = 2,
  64. UBI_COMPAT_PRESERVE = 4,
  65. UBI_COMPAT_REJECT = 5
  66. };
  67. /* Sizes of UBI headers */
  68. #define UBI_EC_HDR_SIZE sizeof(struct ubi_ec_hdr)
  69. #define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr)
  70. /* Sizes of UBI headers without the ending CRC */
  71. #define UBI_EC_HDR_SIZE_CRC (UBI_EC_HDR_SIZE - sizeof(__be32))
  72. #define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(__be32))
  73. /**
  74. * struct ubi_ec_hdr - UBI erase counter header.
  75. * @magic: erase counter header magic number (%UBI_EC_HDR_MAGIC)
  76. * @version: version of UBI implementation which is supposed to accept this
  77. * UBI image
  78. * @padding1: reserved for future, zeroes
  79. * @ec: the erase counter
  80. * @vid_hdr_offset: where the VID header starts
  81. * @data_offset: where the user data start
  82. * @padding2: reserved for future, zeroes
  83. * @hdr_crc: erase counter header CRC checksum
  84. *
  85. * The erase counter header takes 64 bytes and has a plenty of unused space for
  86. * future usage. The unused fields are zeroed. The @version field is used to
  87. * indicate the version of UBI implementation which is supposed to be able to
  88. * work with this UBI image. If @version is greater then the current UBI
  89. * version, the image is rejected. This may be useful in future if something
  90. * is changed radically. This field is duplicated in the volume identifier
  91. * header.
  92. *
  93. * The @vid_hdr_offset and @data_offset fields contain the offset of the the
  94. * volume identifier header and user data, relative to the beginning of the
  95. * physical eraseblock. These values have to be the same for all physical
  96. * eraseblocks.
  97. */
  98. struct ubi_ec_hdr {
  99. __be32 magic;
  100. __u8 version;
  101. __u8 padding1[3];
  102. __be64 ec; /* Warning: the current limit is 31-bit anyway! */
  103. __be32 vid_hdr_offset;
  104. __be32 data_offset;
  105. __u8 padding2[36];
  106. __be32 hdr_crc;
  107. } __attribute__ ((packed));
  108. /**
  109. * struct ubi_vid_hdr - on-flash UBI volume identifier header.
  110. * @magic: volume identifier header magic number (%UBI_VID_HDR_MAGIC)
  111. * @version: UBI implementation version which is supposed to accept this UBI
  112. * image (%UBI_VERSION)
  113. * @vol_type: volume type (%UBI_VID_DYNAMIC or %UBI_VID_STATIC)
  114. * @copy_flag: if this logical eraseblock was copied from another physical
  115. * eraseblock (for wear-leveling reasons)
  116. * @compat: compatibility of this volume (%0, %UBI_COMPAT_DELETE,
  117. * %UBI_COMPAT_IGNORE, %UBI_COMPAT_PRESERVE, or %UBI_COMPAT_REJECT)
  118. * @vol_id: ID of this volume
  119. * @lnum: logical eraseblock number
  120. * @leb_ver: version of this logical eraseblock (IMPORTANT: obsolete, to be
  121. * removed, kept only for not breaking older UBI users)
  122. * @data_size: how many bytes of data this logical eraseblock contains
  123. * @used_ebs: total number of used logical eraseblocks in this volume
  124. * @data_pad: how many bytes at the end of this physical eraseblock are not
  125. * used
  126. * @data_crc: CRC checksum of the data stored in this logical eraseblock
  127. * @padding1: reserved for future, zeroes
  128. * @sqnum: sequence number
  129. * @padding2: reserved for future, zeroes
  130. * @hdr_crc: volume identifier header CRC checksum
  131. *
  132. * The @sqnum is the value of the global sequence counter at the time when this
  133. * VID header was created. The global sequence counter is incremented each time
  134. * UBI writes a new VID header to the flash, i.e. when it maps a logical
  135. * eraseblock to a new physical eraseblock. The global sequence counter is an
  136. * unsigned 64-bit integer and we assume it never overflows. The @sqnum
  137. * (sequence number) is used to distinguish between older and newer versions of
  138. * logical eraseblocks.
  139. *
  140. * There are 2 situations when there may be more then one physical eraseblock
  141. * corresponding to the same logical eraseblock, i.e., having the same @vol_id
  142. * and @lnum values in the volume identifier header. Suppose we have a logical
  143. * eraseblock L and it is mapped to the physical eraseblock P.
  144. *
  145. * 1. Because UBI may erase physical eraseblocks asynchronously, the following
  146. * situation is possible: L is asynchronously erased, so P is scheduled for
  147. * erasure, then L is written to,i.e. mapped to another physical eraseblock P1,
  148. * so P1 is written to, then an unclean reboot happens. Result - there are 2
  149. * physical eraseblocks P and P1 corresponding to the same logical eraseblock
  150. * L. But P1 has greater sequence number, so UBI picks P1 when it attaches the
  151. * flash.
  152. *
  153. * 2. From time to time UBI moves logical eraseblocks to other physical
  154. * eraseblocks for wear-leveling reasons. If, for example, UBI moves L from P
  155. * to P1, and an unclean reboot happens before P is physically erased, there
  156. * are two physical eraseblocks P and P1 corresponding to L and UBI has to
  157. * select one of them when the flash is attached. The @sqnum field says which
  158. * PEB is the original (obviously P will have lower @sqnum) and the copy. But
  159. * it is not enough to select the physical eraseblock with the higher sequence
  160. * number, because the unclean reboot could have happen in the middle of the
  161. * copying process, so the data in P is corrupted. It is also not enough to
  162. * just select the physical eraseblock with lower sequence number, because the
  163. * data there may be old (consider a case if more data was added to P1 after
  164. * the copying). Moreover, the unclean reboot may happen when the erasure of P
  165. * was just started, so it result in unstable P, which is "mostly" OK, but
  166. * still has unstable bits.
  167. *
  168. * UBI uses the @copy_flag field to indicate that this logical eraseblock is a
  169. * copy. UBI also calculates data CRC when the data is moved and stores it at
  170. * the @data_crc field of the copy (P1). So when UBI needs to pick one physical
  171. * eraseblock of two (P or P1), the @copy_flag of the newer one (P1) is
  172. * examined. If it is cleared, the situation* is simple and the newer one is
  173. * picked. If it is set, the data CRC of the copy (P1) is examined. If the CRC
  174. * checksum is correct, this physical eraseblock is selected (P1). Otherwise
  175. * the older one (P) is selected.
  176. *
  177. * Note, there is an obsolete @leb_ver field which was used instead of @sqnum
  178. * in the past. But it is not used anymore and we keep it in order to be able
  179. * to deal with old UBI images. It will be removed at some point.
  180. *
  181. * There are 2 sorts of volumes in UBI: user volumes and internal volumes.
  182. * Internal volumes are not seen from outside and are used for various internal
  183. * UBI purposes. In this implementation there is only one internal volume - the
  184. * layout volume. Internal volumes are the main mechanism of UBI extensions.
  185. * For example, in future one may introduce a journal internal volume. Internal
  186. * volumes have their own reserved range of IDs.
  187. *
  188. * The @compat field is only used for internal volumes and contains the "degree
  189. * of their compatibility". It is always zero for user volumes. This field
  190. * provides a mechanism to introduce UBI extensions and to be still compatible
  191. * with older UBI binaries. For example, if someone introduced a journal in
  192. * future, he would probably use %UBI_COMPAT_DELETE compatibility for the
  193. * journal volume. And in this case, older UBI binaries, which know nothing
  194. * about the journal volume, would just delete this volume and work perfectly
  195. * fine. This is similar to what Ext2fs does when it is fed by an Ext3fs image
  196. * - it just ignores the Ext3fs journal.
  197. *
  198. * The @data_crc field contains the CRC checksum of the contents of the logical
  199. * eraseblock if this is a static volume. In case of dynamic volumes, it does
  200. * not contain the CRC checksum as a rule. The only exception is when the
  201. * data of the physical eraseblock was moved by the wear-leveling unit, then
  202. * the wear-leveling unit calculates the data CRC and stores it in the
  203. * @data_crc field. And of course, the @copy_flag is %in this case.
  204. *
  205. * The @data_size field is used only for static volumes because UBI has to know
  206. * how many bytes of data are stored in this eraseblock. For dynamic volumes,
  207. * this field usually contains zero. The only exception is when the data of the
  208. * physical eraseblock was moved to another physical eraseblock for
  209. * wear-leveling reasons. In this case, UBI calculates CRC checksum of the
  210. * contents and uses both @data_crc and @data_size fields. In this case, the
  211. * @data_size field contains data size.
  212. *
  213. * The @used_ebs field is used only for static volumes and indicates how many
  214. * eraseblocks the data of the volume takes. For dynamic volumes this field is
  215. * not used and always contains zero.
  216. *
  217. * The @data_pad is calculated when volumes are created using the alignment
  218. * parameter. So, effectively, the @data_pad field reduces the size of logical
  219. * eraseblocks of this volume. This is very handy when one uses block-oriented
  220. * software (say, cramfs) on top of the UBI volume.
  221. */
  222. struct ubi_vid_hdr {
  223. __be32 magic;
  224. __u8 version;
  225. __u8 vol_type;
  226. __u8 copy_flag;
  227. __u8 compat;
  228. __be32 vol_id;
  229. __be32 lnum;
  230. __be32 leb_ver; /* obsolete, to be removed, don't use */
  231. __be32 data_size;
  232. __be32 used_ebs;
  233. __be32 data_pad;
  234. __be32 data_crc;
  235. __u8 padding1[4];
  236. __be64 sqnum;
  237. __u8 padding2[12];
  238. __be32 hdr_crc;
  239. } __attribute__ ((packed));
  240. /* Internal UBI volumes count */
  241. #define UBI_INT_VOL_COUNT 1
  242. /*
  243. * Starting ID of internal volumes. There is reserved room for 4096 internal
  244. * volumes.
  245. */
  246. #define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096)
  247. /* The layout volume contains the volume table */
  248. #define UBI_LAYOUT_VOL_ID UBI_INTERNAL_VOL_START
  249. #define UBI_LAYOUT_VOLUME_EBS 2
  250. #define UBI_LAYOUT_VOLUME_NAME "layout volume"
  251. #define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT
  252. /* The maximum number of volumes per one UBI device */
  253. #define UBI_MAX_VOLUMES 128
  254. /* The maximum volume name length */
  255. #define UBI_VOL_NAME_MAX 127
  256. /* Size of the volume table record */
  257. #define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vtbl_record)
  258. /* Size of the volume table record without the ending CRC */
  259. #define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(__be32))
  260. /**
  261. * struct ubi_vtbl_record - a record in the volume table.
  262. * @reserved_pebs: how many physical eraseblocks are reserved for this volume
  263. * @alignment: volume alignment
  264. * @data_pad: how many bytes are unused at the end of the each physical
  265. * eraseblock to satisfy the requested alignment
  266. * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
  267. * @upd_marker: if volume update was started but not finished
  268. * @name_len: volume name length
  269. * @name: the volume name
  270. * @padding2: reserved, zeroes
  271. * @crc: a CRC32 checksum of the record
  272. *
  273. * The volume table records are stored in the volume table, which is stored in
  274. * the layout volume. The layout volume consists of 2 logical eraseblock, each
  275. * of which contains a copy of the volume table (i.e., the volume table is
  276. * duplicated). The volume table is an array of &struct ubi_vtbl_record
  277. * objects indexed by the volume ID.
  278. *
  279. * If the size of the logical eraseblock is large enough to fit
  280. * %UBI_MAX_VOLUMES records, the volume table contains %UBI_MAX_VOLUMES
  281. * records. Otherwise, it contains as many records as it can fit (i.e., size of
  282. * logical eraseblock divided by sizeof(struct ubi_vtbl_record)).
  283. *
  284. * The @upd_marker flag is used to implement volume update. It is set to %1
  285. * before update and set to %0 after the update. So if the update operation was
  286. * interrupted, UBI knows that the volume is corrupted.
  287. *
  288. * The @alignment field is specified when the volume is created and cannot be
  289. * later changed. It may be useful, for example, when a block-oriented file
  290. * system works on top of UBI. The @data_pad field is calculated using the
  291. * logical eraseblock size and @alignment. The alignment must be multiple to the
  292. * minimal flash I/O unit. If @alignment is 1, all the available space of
  293. * the physical eraseblocks is used.
  294. *
  295. * Empty records contain all zeroes and the CRC checksum of those zeroes.
  296. */
  297. struct ubi_vtbl_record {
  298. __be32 reserved_pebs;
  299. __be32 alignment;
  300. __be32 data_pad;
  301. __u8 vol_type;
  302. __u8 upd_marker;
  303. __be16 name_len;
  304. __u8 name[UBI_VOL_NAME_MAX+1];
  305. __u8 padding2[24];
  306. __be32 crc;
  307. } __attribute__ ((packed));
  308. #endif /* !__UBI_HEADER_H__ */