aboutsummaryrefslogtreecommitdiffstats
path: root/drivers/mtd/ubi
diff options
context:
space:
mode:
authorDavid Woodhouse <dwmw2@infradead.org>2008-04-23 04:57:25 -0400
committerDavid Woodhouse <dwmw2@infradead.org>2008-04-23 04:57:25 -0400
commite43fe686e48835ca027559a068bbe0b6d264a254 (patch)
treedef98b52b111ba384a2fd01955465f813ca299e4 /drivers/mtd/ubi
parent986ee0139a91ab8b6b07d29d7a112c8033b5f8e0 (diff)
parent434b825e1fc9ef7971fc962734278ffbab36a1ab (diff)
Merge git://git.infradead.org/~dedekind/ubi-2.6
Diffstat (limited to 'drivers/mtd/ubi')
-rw-r--r--drivers/mtd/ubi/Kconfig9
-rw-r--r--drivers/mtd/ubi/build.c37
-rw-r--r--drivers/mtd/ubi/debug.h2
-rw-r--r--drivers/mtd/ubi/gluebi.c5
-rw-r--r--drivers/mtd/ubi/io.c4
-rw-r--r--drivers/mtd/ubi/scan.c41
-rw-r--r--drivers/mtd/ubi/scan.h2
-rw-r--r--drivers/mtd/ubi/ubi-media.h372
-rw-r--r--drivers/mtd/ubi/ubi.h3
9 files changed, 416 insertions, 59 deletions
diff --git a/drivers/mtd/ubi/Kconfig b/drivers/mtd/ubi/Kconfig
index b9daf159a4a7..3f063108e95f 100644
--- a/drivers/mtd/ubi/Kconfig
+++ b/drivers/mtd/ubi/Kconfig
@@ -24,8 +24,13 @@ config MTD_UBI_WL_THRESHOLD
24 erase counter value and the lowest erase counter value of eraseblocks 24 erase counter value and the lowest erase counter value of eraseblocks
25 of UBI devices. When this threshold is exceeded, UBI starts performing 25 of UBI devices. When this threshold is exceeded, UBI starts performing
26 wear leveling by means of moving data from eraseblock with low erase 26 wear leveling by means of moving data from eraseblock with low erase
27 counter to eraseblocks with high erase counter. Leave the default 27 counter to eraseblocks with high erase counter.
28 value if unsure. 28
29 The default value should be OK for SLC NAND flashes, NOR flashes and
30 other flashes which have eraseblock life-cycle 100000 or more.
31 However, in case of MLC NAND flashes which typically have eraseblock
32 life-cycle less then 10000, the threshold should be lessened (e.g.,
33 to 128 or 256, although it does not have to be power of 2).
29 34
30config MTD_UBI_BEB_RESERVE 35config MTD_UBI_BEB_RESERVE
31 int "Percentage of reserved eraseblocks for bad eraseblocks handling" 36 int "Percentage of reserved eraseblocks for bad eraseblocks handling"
diff --git a/drivers/mtd/ubi/build.c b/drivers/mtd/ubi/build.c
index 275960462970..e8578ca422ff 100644
--- a/drivers/mtd/ubi/build.c
+++ b/drivers/mtd/ubi/build.c
@@ -606,8 +606,16 @@ static int io_init(struct ubi_device *ubi)
606 ubi->ro_mode = 1; 606 ubi->ro_mode = 1;
607 } 607 }
608 608
609 dbg_msg("leb_size %d", ubi->leb_size); 609 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
610 dbg_msg("ro_mode %d", ubi->ro_mode); 610 ubi->peb_size, ubi->peb_size >> 10);
611 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
612 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
613 if (ubi->hdrs_min_io_size != ubi->min_io_size)
614 ubi_msg("sub-page size: %d",
615 ubi->hdrs_min_io_size);
616 ubi_msg("VID header offset: %d (aligned %d)",
617 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
618 ubi_msg("data offset: %d", ubi->leb_start);
611 619
612 /* 620 /*
613 * Note, ideally, we have to initialize ubi->bad_peb_count here. But 621 * Note, ideally, we have to initialize ubi->bad_peb_count here. But
@@ -804,15 +812,8 @@ int ubi_attach_mtd_dev(struct mtd_info *mtd, int ubi_num, int vid_hdr_offset)
804 ubi_msg("attached mtd%d to ubi%d", mtd->index, ubi_num); 812 ubi_msg("attached mtd%d to ubi%d", mtd->index, ubi_num);
805 ubi_msg("MTD device name: \"%s\"", mtd->name); 813 ubi_msg("MTD device name: \"%s\"", mtd->name);
806 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20); 814 ubi_msg("MTD device size: %llu MiB", ubi->flash_size >> 20);
807 ubi_msg("physical eraseblock size: %d bytes (%d KiB)",
808 ubi->peb_size, ubi->peb_size >> 10);
809 ubi_msg("logical eraseblock size: %d bytes", ubi->leb_size);
810 ubi_msg("number of good PEBs: %d", ubi->good_peb_count); 815 ubi_msg("number of good PEBs: %d", ubi->good_peb_count);
811 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count); 816 ubi_msg("number of bad PEBs: %d", ubi->bad_peb_count);
812 ubi_msg("smallest flash I/O unit: %d", ubi->min_io_size);
813 ubi_msg("VID header offset: %d (aligned %d)",
814 ubi->vid_hdr_offset, ubi->vid_hdr_aloffset);
815 ubi_msg("data offset: %d", ubi->leb_start);
816 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots); 817 ubi_msg("max. allowed volumes: %d", ubi->vtbl_slots);
817 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD); 818 ubi_msg("wear-leveling threshold: %d", CONFIG_MTD_UBI_WL_THRESHOLD);
818 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT); 819 ubi_msg("number of internal volumes: %d", UBI_INT_VOL_COUNT);
@@ -950,8 +951,7 @@ static int __init ubi_init(void)
950 BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64); 951 BUILD_BUG_ON(sizeof(struct ubi_vid_hdr) != 64);
951 952
952 if (mtd_devs > UBI_MAX_DEVICES) { 953 if (mtd_devs > UBI_MAX_DEVICES) {
953 printk(KERN_ERR "UBI error: too many MTD devices, " 954 ubi_err("too many MTD devices, maximum is %d", UBI_MAX_DEVICES);
954 "maximum is %d\n", UBI_MAX_DEVICES);
955 return -EINVAL; 955 return -EINVAL;
956 } 956 }
957 957
@@ -959,25 +959,25 @@ static int __init ubi_init(void)
959 ubi_class = class_create(THIS_MODULE, UBI_NAME_STR); 959 ubi_class = class_create(THIS_MODULE, UBI_NAME_STR);
960 if (IS_ERR(ubi_class)) { 960 if (IS_ERR(ubi_class)) {
961 err = PTR_ERR(ubi_class); 961 err = PTR_ERR(ubi_class);
962 printk(KERN_ERR "UBI error: cannot create UBI class\n"); 962 ubi_err("cannot create UBI class");
963 goto out; 963 goto out;
964 } 964 }
965 965
966 err = class_create_file(ubi_class, &ubi_version); 966 err = class_create_file(ubi_class, &ubi_version);
967 if (err) { 967 if (err) {
968 printk(KERN_ERR "UBI error: cannot create sysfs file\n"); 968 ubi_err("cannot create sysfs file");
969 goto out_class; 969 goto out_class;
970 } 970 }
971 971
972 err = misc_register(&ubi_ctrl_cdev); 972 err = misc_register(&ubi_ctrl_cdev);
973 if (err) { 973 if (err) {
974 printk(KERN_ERR "UBI error: cannot register device\n"); 974 ubi_err("cannot register device");
975 goto out_version; 975 goto out_version;
976 } 976 }
977 977
978 ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab", 978 ubi_wl_entry_slab = kmem_cache_create("ubi_wl_entry_slab",
979 sizeof(struct ubi_wl_entry), 979 sizeof(struct ubi_wl_entry),
980 0, 0, NULL); 980 0, 0, NULL);
981 if (!ubi_wl_entry_slab) 981 if (!ubi_wl_entry_slab)
982 goto out_dev_unreg; 982 goto out_dev_unreg;
983 983
@@ -1000,8 +1000,7 @@ static int __init ubi_init(void)
1000 mutex_unlock(&ubi_devices_mutex); 1000 mutex_unlock(&ubi_devices_mutex);
1001 if (err < 0) { 1001 if (err < 0) {
1002 put_mtd_device(mtd); 1002 put_mtd_device(mtd);
1003 printk(KERN_ERR "UBI error: cannot attach mtd%d\n", 1003 ubi_err("cannot attach mtd%d", mtd->index);
1004 mtd->index);
1005 goto out_detach; 1004 goto out_detach;
1006 } 1005 }
1007 } 1006 }
@@ -1023,7 +1022,7 @@ out_version:
1023out_class: 1022out_class:
1024 class_destroy(ubi_class); 1023 class_destroy(ubi_class);
1025out: 1024out:
1026 printk(KERN_ERR "UBI error: cannot initialize UBI, error %d\n", err); 1025 ubi_err("UBI error: cannot initialize UBI, error %d", err);
1027 return err; 1026 return err;
1028} 1027}
1029module_init(ubi_init); 1028module_init(ubi_init);
diff --git a/drivers/mtd/ubi/debug.h b/drivers/mtd/ubi/debug.h
index 8ac7d87dc85b..8ea99d8c9e1f 100644
--- a/drivers/mtd/ubi/debug.h
+++ b/drivers/mtd/ubi/debug.h
@@ -99,8 +99,10 @@ void ubi_dbg_dump_mkvol_req(const struct ubi_mkvol_req *req);
99#ifdef CONFIG_MTD_UBI_DEBUG_MSG_BLD 99#ifdef CONFIG_MTD_UBI_DEBUG_MSG_BLD
100/* Initialization and build messages */ 100/* Initialization and build messages */
101#define dbg_bld(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__) 101#define dbg_bld(fmt, ...) dbg_msg(fmt, ##__VA_ARGS__)
102#define UBI_IO_DEBUG 1
102#else 103#else
103#define dbg_bld(fmt, ...) ({}) 104#define dbg_bld(fmt, ...) ({})
105#define UBI_IO_DEBUG 0
104#endif 106#endif
105 107
106#ifdef CONFIG_MTD_UBI_DEBUG_EMULATE_BITFLIPS 108#ifdef CONFIG_MTD_UBI_DEBUG_EMULATE_BITFLIPS
diff --git a/drivers/mtd/ubi/gluebi.c b/drivers/mtd/ubi/gluebi.c
index d397219238d3..e909b390069a 100644
--- a/drivers/mtd/ubi/gluebi.c
+++ b/drivers/mtd/ubi/gluebi.c
@@ -291,11 +291,12 @@ int ubi_create_gluebi(struct ubi_device *ubi, struct ubi_volume *vol)
291 /* 291 /*
292 * In case of dynamic volume, MTD device size is just volume size. In 292 * In case of dynamic volume, MTD device size is just volume size. In
293 * case of a static volume the size is equivalent to the amount of data 293 * case of a static volume the size is equivalent to the amount of data
294 * bytes, which is zero at this moment and will be changed after volume 294 * bytes.
295 * update.
296 */ 295 */
297 if (vol->vol_type == UBI_DYNAMIC_VOLUME) 296 if (vol->vol_type == UBI_DYNAMIC_VOLUME)
298 mtd->size = vol->usable_leb_size * vol->reserved_pebs; 297 mtd->size = vol->usable_leb_size * vol->reserved_pebs;
298 else
299 mtd->size = vol->used_bytes;
299 300
300 if (add_mtd_device(mtd)) { 301 if (add_mtd_device(mtd)) {
301 ubi_err("cannot not add MTD device\n"); 302 ubi_err("cannot not add MTD device\n");
diff --git a/drivers/mtd/ubi/io.c b/drivers/mtd/ubi/io.c
index db3efdef2433..4ac11df7b048 100644
--- a/drivers/mtd/ubi/io.c
+++ b/drivers/mtd/ubi/io.c
@@ -631,6 +631,8 @@ int ubi_io_read_ec_hdr(struct ubi_device *ubi, int pnum,
631 631
632 dbg_io("read EC header from PEB %d", pnum); 632 dbg_io("read EC header from PEB %d", pnum);
633 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 633 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
634 if (UBI_IO_DEBUG)
635 verbose = 1;
634 636
635 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE); 637 err = ubi_io_read(ubi, ec_hdr, pnum, 0, UBI_EC_HDR_SIZE);
636 if (err) { 638 if (err) {
@@ -904,6 +906,8 @@ int ubi_io_read_vid_hdr(struct ubi_device *ubi, int pnum,
904 906
905 dbg_io("read VID header from PEB %d", pnum); 907 dbg_io("read VID header from PEB %d", pnum);
906 ubi_assert(pnum >= 0 && pnum < ubi->peb_count); 908 ubi_assert(pnum >= 0 && pnum < ubi->peb_count);
909 if (UBI_IO_DEBUG)
910 verbose = 1;
907 911
908 p = (char *)vid_hdr - ubi->vid_hdr_shift; 912 p = (char *)vid_hdr - ubi->vid_hdr_shift;
909 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset, 913 err = ubi_io_read(ubi, p, pnum, ubi->vid_hdr_aloffset,
diff --git a/drivers/mtd/ubi/scan.c b/drivers/mtd/ubi/scan.c
index 05aa3e7daba1..96d410e106ab 100644
--- a/drivers/mtd/ubi/scan.c
+++ b/drivers/mtd/ubi/scan.c
@@ -42,6 +42,7 @@
42 42
43#include <linux/err.h> 43#include <linux/err.h>
44#include <linux/crc32.h> 44#include <linux/crc32.h>
45#include <asm/div64.h>
45#include "ubi.h" 46#include "ubi.h"
46 47
47#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID 48#ifdef CONFIG_MTD_UBI_DEBUG_PARANOID
@@ -92,27 +93,6 @@ static int add_to_list(struct ubi_scan_info *si, int pnum, int ec,
92} 93}
93 94
94/** 95/**
95 * commit_to_mean_value - commit intermediate results to the final mean erase
96 * counter value.
97 * @si: scanning information
98 *
99 * This is a helper function which calculates partial mean erase counter mean
100 * value and adds it to the resulting mean value. As we can work only in
101 * integer arithmetic and we want to calculate the mean value of erase counter
102 * accurately, we first sum erase counter values in @si->ec_sum variable and
103 * count these components in @si->ec_count. If this temporary @si->ec_sum is
104 * going to overflow, we calculate the partial mean value
105 * (@si->ec_sum/@si->ec_count) and add it to @si->mean_ec.
106 */
107static void commit_to_mean_value(struct ubi_scan_info *si)
108{
109 si->ec_sum /= si->ec_count;
110 if (si->ec_sum % si->ec_count >= si->ec_count / 2)
111 si->mean_ec += 1;
112 si->mean_ec += si->ec_sum;
113}
114
115/**
116 * validate_vid_hdr - check that volume identifier header is correct and 96 * validate_vid_hdr - check that volume identifier header is correct and
117 * consistent. 97 * consistent.
118 * @vid_hdr: the volume identifier header to check 98 * @vid_hdr: the volume identifier header to check
@@ -901,15 +881,8 @@ static int process_eb(struct ubi_device *ubi, struct ubi_scan_info *si, int pnum
901 881
902adjust_mean_ec: 882adjust_mean_ec:
903 if (!ec_corr) { 883 if (!ec_corr) {
904 if (si->ec_sum + ec < ec) { 884 si->ec_sum += ec;
905 commit_to_mean_value(si); 885 si->ec_count += 1;
906 si->ec_sum = 0;
907 si->ec_count = 0;
908 } else {
909 si->ec_sum += ec;
910 si->ec_count += 1;
911 }
912
913 if (ec > si->max_ec) 886 if (ec > si->max_ec)
914 si->max_ec = ec; 887 si->max_ec = ec;
915 if (ec < si->min_ec) 888 if (ec < si->min_ec)
@@ -965,9 +938,11 @@ struct ubi_scan_info *ubi_scan(struct ubi_device *ubi)
965 938
966 dbg_msg("scanning is finished"); 939 dbg_msg("scanning is finished");
967 940
968 /* Finish mean erase counter calculations */ 941 /* Calculate mean erase counter */
969 if (si->ec_count) 942 if (si->ec_count) {
970 commit_to_mean_value(si); 943 do_div(si->ec_sum, si->ec_count);
944 si->mean_ec = si->ec_sum;
945 }
971 946
972 if (si->is_empty) 947 if (si->is_empty)
973 ubi_msg("empty MTD device detected"); 948 ubi_msg("empty MTD device detected");
diff --git a/drivers/mtd/ubi/scan.h b/drivers/mtd/ubi/scan.h
index 46d444af471a..966b9b682a42 100644
--- a/drivers/mtd/ubi/scan.h
+++ b/drivers/mtd/ubi/scan.h
@@ -124,7 +124,7 @@ struct ubi_scan_info {
124 int max_ec; 124 int max_ec;
125 unsigned long long max_sqnum; 125 unsigned long long max_sqnum;
126 int mean_ec; 126 int mean_ec;
127 int ec_sum; 127 uint64_t ec_sum;
128 int ec_count; 128 int ec_count;
129}; 129};
130 130
diff --git a/drivers/mtd/ubi/ubi-media.h b/drivers/mtd/ubi/ubi-media.h
new file mode 100644
index 000000000000..c3185d9fd048
--- /dev/null
+++ b/drivers/mtd/ubi/ubi-media.h
@@ -0,0 +1,372 @@
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/*
26 * This file defines the layout of UBI headers and all the other UBI on-flash
27 * data structures.
28 */
29
30#ifndef __UBI_MEDIA_H__
31#define __UBI_MEDIA_H__
32
33#include <asm/byteorder.h>
34
35/* The version of UBI images supported by this implementation */
36#define UBI_VERSION 1
37
38/* The highest erase counter value supported by this implementation */
39#define UBI_MAX_ERASECOUNTER 0x7FFFFFFF
40
41/* The initial CRC32 value used when calculating CRC checksums */
42#define UBI_CRC32_INIT 0xFFFFFFFFU
43
44/* Erase counter header magic number (ASCII "UBI#") */
45#define UBI_EC_HDR_MAGIC 0x55424923
46/* Volume identifier header magic number (ASCII "UBI!") */
47#define UBI_VID_HDR_MAGIC 0x55424921
48
49/*
50 * Volume type constants used in the volume identifier header.
51 *
52 * @UBI_VID_DYNAMIC: dynamic volume
53 * @UBI_VID_STATIC: static volume
54 */
55enum {
56 UBI_VID_DYNAMIC = 1,
57 UBI_VID_STATIC = 2
58};
59
60/*
61 * Volume flags used in the volume table record.
62 *
63 * @UBI_VTBL_AUTORESIZE_FLG: auto-resize this volume
64 *
65 * %UBI_VTBL_AUTORESIZE_FLG flag can be set only for one volume in the volume
66 * table. UBI automatically re-sizes the volume which has this flag and makes
67 * the volume to be of largest possible size. This means that if after the
68 * initialization UBI finds out that there are available physical eraseblocks
69 * present on the device, it automatically appends all of them to the volume
70 * (the physical eraseblocks reserved for bad eraseblocks handling and other
71 * reserved physical eraseblocks are not taken). So, if there is a volume with
72 * the %UBI_VTBL_AUTORESIZE_FLG flag set, the amount of available logical
73 * eraseblocks will be zero after UBI is loaded, because all of them will be
74 * reserved for this volume. Note, the %UBI_VTBL_AUTORESIZE_FLG bit is cleared
75 * after the volume had been initialized.
76 *
77 * The auto-resize feature is useful for device production purposes. For
78 * example, different NAND flash chips may have different amount of initial bad
79 * eraseblocks, depending of particular chip instance. Manufacturers of NAND
80 * chips usually guarantee that the amount of initial bad eraseblocks does not
81 * exceed certain percent, e.g. 2%. When one creates an UBI image which will be
82 * flashed to the end devices in production, he does not know the exact amount
83 * of good physical eraseblocks the NAND chip on the device will have, but this
84 * number is required to calculate the volume sized and put them to the volume
85 * table of the UBI image. In this case, one of the volumes (e.g., the one
86 * which will store the root file system) is marked as "auto-resizable", and
87 * UBI will adjust its size on the first boot if needed.
88 *
89 * Note, first UBI reserves some amount of physical eraseblocks for bad
90 * eraseblock handling, and then re-sizes the volume, not vice-versa. This
91 * means that the pool of reserved physical eraseblocks will always be present.
92 */
93enum {
94 UBI_VTBL_AUTORESIZE_FLG = 0x01,
95};
96
97/*
98 * Compatibility constants used by internal volumes.
99 *
100 * @UBI_COMPAT_DELETE: delete this internal volume before anything is written
101 * to the flash
102 * @UBI_COMPAT_RO: attach this device in read-only mode
103 * @UBI_COMPAT_PRESERVE: preserve this internal volume - do not touch its
104 * physical eraseblocks, don't allow the wear-leveling unit to move them
105 * @UBI_COMPAT_REJECT: reject this UBI image
106 */
107enum {
108 UBI_COMPAT_DELETE = 1,
109 UBI_COMPAT_RO = 2,
110 UBI_COMPAT_PRESERVE = 4,
111 UBI_COMPAT_REJECT = 5
112};
113
114/* Sizes of UBI headers */
115#define UBI_EC_HDR_SIZE sizeof(struct ubi_ec_hdr)
116#define UBI_VID_HDR_SIZE sizeof(struct ubi_vid_hdr)
117
118/* Sizes of UBI headers without the ending CRC */
119#define UBI_EC_HDR_SIZE_CRC (UBI_EC_HDR_SIZE - sizeof(__be32))
120#define UBI_VID_HDR_SIZE_CRC (UBI_VID_HDR_SIZE - sizeof(__be32))
121
122/**
123 * struct ubi_ec_hdr - UBI erase counter header.
124 * @magic: erase counter header magic number (%UBI_EC_HDR_MAGIC)
125 * @version: version of UBI implementation which is supposed to accept this
126 * UBI image
127 * @padding1: reserved for future, zeroes
128 * @ec: the erase counter
129 * @vid_hdr_offset: where the VID header starts
130 * @data_offset: where the user data start
131 * @padding2: reserved for future, zeroes
132 * @hdr_crc: erase counter header CRC checksum
133 *
134 * The erase counter header takes 64 bytes and has a plenty of unused space for
135 * future usage. The unused fields are zeroed. The @version field is used to
136 * indicate the version of UBI implementation which is supposed to be able to
137 * work with this UBI image. If @version is greater then the current UBI
138 * version, the image is rejected. This may be useful in future if something
139 * is changed radically. This field is duplicated in the volume identifier
140 * header.
141 *
142 * The @vid_hdr_offset and @data_offset fields contain the offset of the the
143 * volume identifier header and user data, relative to the beginning of the
144 * physical eraseblock. These values have to be the same for all physical
145 * eraseblocks.
146 */
147struct ubi_ec_hdr {
148 __be32 magic;
149 __u8 version;
150 __u8 padding1[3];
151 __be64 ec; /* Warning: the current limit is 31-bit anyway! */
152 __be32 vid_hdr_offset;
153 __be32 data_offset;
154 __u8 padding2[36];
155 __be32 hdr_crc;
156} __attribute__ ((packed));
157
158/**
159 * struct ubi_vid_hdr - on-flash UBI volume identifier header.
160 * @magic: volume identifier header magic number (%UBI_VID_HDR_MAGIC)
161 * @version: UBI implementation version which is supposed to accept this UBI
162 * image (%UBI_VERSION)
163 * @vol_type: volume type (%UBI_VID_DYNAMIC or %UBI_VID_STATIC)
164 * @copy_flag: if this logical eraseblock was copied from another physical
165 * eraseblock (for wear-leveling reasons)
166 * @compat: compatibility of this volume (%0, %UBI_COMPAT_DELETE,
167 * %UBI_COMPAT_IGNORE, %UBI_COMPAT_PRESERVE, or %UBI_COMPAT_REJECT)
168 * @vol_id: ID of this volume
169 * @lnum: logical eraseblock number
170 * @leb_ver: version of this logical eraseblock (IMPORTANT: obsolete, to be
171 * removed, kept only for not breaking older UBI users)
172 * @data_size: how many bytes of data this logical eraseblock contains
173 * @used_ebs: total number of used logical eraseblocks in this volume
174 * @data_pad: how many bytes at the end of this physical eraseblock are not
175 * used
176 * @data_crc: CRC checksum of the data stored in this logical eraseblock
177 * @padding1: reserved for future, zeroes
178 * @sqnum: sequence number
179 * @padding2: reserved for future, zeroes
180 * @hdr_crc: volume identifier header CRC checksum
181 *
182 * The @sqnum is the value of the global sequence counter at the time when this
183 * VID header was created. The global sequence counter is incremented each time
184 * UBI writes a new VID header to the flash, i.e. when it maps a logical
185 * eraseblock to a new physical eraseblock. The global sequence counter is an
186 * unsigned 64-bit integer and we assume it never overflows. The @sqnum
187 * (sequence number) is used to distinguish between older and newer versions of
188 * logical eraseblocks.
189 *
190 * There are 2 situations when there may be more then one physical eraseblock
191 * corresponding to the same logical eraseblock, i.e., having the same @vol_id
192 * and @lnum values in the volume identifier header. Suppose we have a logical
193 * eraseblock L and it is mapped to the physical eraseblock P.
194 *
195 * 1. Because UBI may erase physical eraseblocks asynchronously, the following
196 * situation is possible: L is asynchronously erased, so P is scheduled for
197 * erasure, then L is written to,i.e. mapped to another physical eraseblock P1,
198 * so P1 is written to, then an unclean reboot happens. Result - there are 2
199 * physical eraseblocks P and P1 corresponding to the same logical eraseblock
200 * L. But P1 has greater sequence number, so UBI picks P1 when it attaches the
201 * flash.
202 *
203 * 2. From time to time UBI moves logical eraseblocks to other physical
204 * eraseblocks for wear-leveling reasons. If, for example, UBI moves L from P
205 * to P1, and an unclean reboot happens before P is physically erased, there
206 * are two physical eraseblocks P and P1 corresponding to L and UBI has to
207 * select one of them when the flash is attached. The @sqnum field says which
208 * PEB is the original (obviously P will have lower @sqnum) and the copy. But
209 * it is not enough to select the physical eraseblock with the higher sequence
210 * number, because the unclean reboot could have happen in the middle of the
211 * copying process, so the data in P is corrupted. It is also not enough to
212 * just select the physical eraseblock with lower sequence number, because the
213 * data there may be old (consider a case if more data was added to P1 after
214 * the copying). Moreover, the unclean reboot may happen when the erasure of P
215 * was just started, so it result in unstable P, which is "mostly" OK, but
216 * still has unstable bits.
217 *
218 * UBI uses the @copy_flag field to indicate that this logical eraseblock is a
219 * copy. UBI also calculates data CRC when the data is moved and stores it at
220 * the @data_crc field of the copy (P1). So when UBI needs to pick one physical
221 * eraseblock of two (P or P1), the @copy_flag of the newer one (P1) is
222 * examined. If it is cleared, the situation* is simple and the newer one is
223 * picked. If it is set, the data CRC of the copy (P1) is examined. If the CRC
224 * checksum is correct, this physical eraseblock is selected (P1). Otherwise
225 * the older one (P) is selected.
226 *
227 * Note, there is an obsolete @leb_ver field which was used instead of @sqnum
228 * in the past. But it is not used anymore and we keep it in order to be able
229 * to deal with old UBI images. It will be removed at some point.
230 *
231 * There are 2 sorts of volumes in UBI: user volumes and internal volumes.
232 * Internal volumes are not seen from outside and are used for various internal
233 * UBI purposes. In this implementation there is only one internal volume - the
234 * layout volume. Internal volumes are the main mechanism of UBI extensions.
235 * For example, in future one may introduce a journal internal volume. Internal
236 * volumes have their own reserved range of IDs.
237 *
238 * The @compat field is only used for internal volumes and contains the "degree
239 * of their compatibility". It is always zero for user volumes. This field
240 * provides a mechanism to introduce UBI extensions and to be still compatible
241 * with older UBI binaries. For example, if someone introduced a journal in
242 * future, he would probably use %UBI_COMPAT_DELETE compatibility for the
243 * journal volume. And in this case, older UBI binaries, which know nothing
244 * about the journal volume, would just delete this volume and work perfectly
245 * fine. This is similar to what Ext2fs does when it is fed by an Ext3fs image
246 * - it just ignores the Ext3fs journal.
247 *
248 * The @data_crc field contains the CRC checksum of the contents of the logical
249 * eraseblock if this is a static volume. In case of dynamic volumes, it does
250 * not contain the CRC checksum as a rule. The only exception is when the
251 * data of the physical eraseblock was moved by the wear-leveling unit, then
252 * the wear-leveling unit calculates the data CRC and stores it in the
253 * @data_crc field. And of course, the @copy_flag is %in this case.
254 *
255 * The @data_size field is used only for static volumes because UBI has to know
256 * how many bytes of data are stored in this eraseblock. For dynamic volumes,
257 * this field usually contains zero. The only exception is when the data of the
258 * physical eraseblock was moved to another physical eraseblock for
259 * wear-leveling reasons. In this case, UBI calculates CRC checksum of the
260 * contents and uses both @data_crc and @data_size fields. In this case, the
261 * @data_size field contains data size.
262 *
263 * The @used_ebs field is used only for static volumes and indicates how many
264 * eraseblocks the data of the volume takes. For dynamic volumes this field is
265 * not used and always contains zero.
266 *
267 * The @data_pad is calculated when volumes are created using the alignment
268 * parameter. So, effectively, the @data_pad field reduces the size of logical
269 * eraseblocks of this volume. This is very handy when one uses block-oriented
270 * software (say, cramfs) on top of the UBI volume.
271 */
272struct ubi_vid_hdr {
273 __be32 magic;
274 __u8 version;
275 __u8 vol_type;
276 __u8 copy_flag;
277 __u8 compat;
278 __be32 vol_id;
279 __be32 lnum;
280 __be32 leb_ver; /* obsolete, to be removed, don't use */
281 __be32 data_size;
282 __be32 used_ebs;
283 __be32 data_pad;
284 __be32 data_crc;
285 __u8 padding1[4];
286 __be64 sqnum;
287 __u8 padding2[12];
288 __be32 hdr_crc;
289} __attribute__ ((packed));
290
291/* Internal UBI volumes count */
292#define UBI_INT_VOL_COUNT 1
293
294/*
295 * Starting ID of internal volumes. There is reserved room for 4096 internal
296 * volumes.
297 */
298#define UBI_INTERNAL_VOL_START (0x7FFFFFFF - 4096)
299
300/* The layout volume contains the volume table */
301
302#define UBI_LAYOUT_VOLUME_ID UBI_INTERNAL_VOL_START
303#define UBI_LAYOUT_VOLUME_TYPE UBI_VID_DYNAMIC
304#define UBI_LAYOUT_VOLUME_ALIGN 1
305#define UBI_LAYOUT_VOLUME_EBS 2
306#define UBI_LAYOUT_VOLUME_NAME "layout volume"
307#define UBI_LAYOUT_VOLUME_COMPAT UBI_COMPAT_REJECT
308
309/* The maximum number of volumes per one UBI device */
310#define UBI_MAX_VOLUMES 128
311
312/* The maximum volume name length */
313#define UBI_VOL_NAME_MAX 127
314
315/* Size of the volume table record */
316#define UBI_VTBL_RECORD_SIZE sizeof(struct ubi_vtbl_record)
317
318/* Size of the volume table record without the ending CRC */
319#define UBI_VTBL_RECORD_SIZE_CRC (UBI_VTBL_RECORD_SIZE - sizeof(__be32))
320
321/**
322 * struct ubi_vtbl_record - a record in the volume table.
323 * @reserved_pebs: how many physical eraseblocks are reserved for this volume
324 * @alignment: volume alignment
325 * @data_pad: how many bytes are unused at the end of the each physical
326 * eraseblock to satisfy the requested alignment
327 * @vol_type: volume type (%UBI_DYNAMIC_VOLUME or %UBI_STATIC_VOLUME)
328 * @upd_marker: if volume update was started but not finished
329 * @name_len: volume name length
330 * @name: the volume name
331 * @flags: volume flags (%UBI_VTBL_AUTORESIZE_FLG)
332 * @padding: reserved, zeroes
333 * @crc: a CRC32 checksum of the record
334 *
335 * The volume table records are stored in the volume table, which is stored in
336 * the layout volume. The layout volume consists of 2 logical eraseblock, each
337 * of which contains a copy of the volume table (i.e., the volume table is
338 * duplicated). The volume table is an array of &struct ubi_vtbl_record
339 * objects indexed by the volume ID.
340 *
341 * If the size of the logical eraseblock is large enough to fit
342 * %UBI_MAX_VOLUMES records, the volume table contains %UBI_MAX_VOLUMES
343 * records. Otherwise, it contains as many records as it can fit (i.e., size of
344 * logical eraseblock divided by sizeof(struct ubi_vtbl_record)).
345 *
346 * The @upd_marker flag is used to implement volume update. It is set to %1
347 * before update and set to %0 after the update. So if the update operation was
348 * interrupted, UBI knows that the volume is corrupted.
349 *
350 * The @alignment field is specified when the volume is created and cannot be
351 * later changed. It may be useful, for example, when a block-oriented file
352 * system works on top of UBI. The @data_pad field is calculated using the
353 * logical eraseblock size and @alignment. The alignment must be multiple to the
354 * minimal flash I/O unit. If @alignment is 1, all the available space of
355 * the physical eraseblocks is used.
356 *
357 * Empty records contain all zeroes and the CRC checksum of those zeroes.
358 */
359struct ubi_vtbl_record {
360 __be32 reserved_pebs;
361 __be32 alignment;
362 __be32 data_pad;
363 __u8 vol_type;
364 __u8 upd_marker;
365 __be16 name_len;
366 __u8 name[UBI_VOL_NAME_MAX+1];
367 __u8 flags;
368 __u8 padding[23];
369 __be32 crc;
370} __attribute__ ((packed));
371
372#endif /* !__UBI_MEDIA_H__ */
diff --git a/drivers/mtd/ubi/ubi.h b/drivers/mtd/ubi/ubi.h
index 8f095cb87108..67dcbd11c15c 100644
--- a/drivers/mtd/ubi/ubi.h
+++ b/drivers/mtd/ubi/ubi.h
@@ -37,10 +37,9 @@
37#include <linux/string.h> 37#include <linux/string.h>
38#include <linux/vmalloc.h> 38#include <linux/vmalloc.h>
39#include <linux/mtd/mtd.h> 39#include <linux/mtd/mtd.h>
40
41#include <mtd/ubi-header.h>
42#include <linux/mtd/ubi.h> 40#include <linux/mtd/ubi.h>
43 41
42#include "ubi-media.h"
44#include "scan.h" 43#include "scan.h"
45#include "debug.h" 44#include "debug.h"
46 45