Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

UBI: add image sequence number to EC header

An image sequence number is added to the UBI erase-counter header
to be able determine if the root file system contains a mixture
of old and new images (because the flashing failed to complete).

A change to nolo is also needed for this to take effect.

Signed-off-by: Adrian Hunter <adrian.hunter@nokia.com>
Signed-off-by: Artem Bityutskiy <Artem.Bityutskiy@nokia.com>

authored by

Adrian Hunter and committed by
Artem Bityutskiy
0c6c7fa1 1398788f

+33 -3
+1
drivers/mtd/ubi/build.c
··· 996 996 ubi_msg("number of PEBs reserved for bad PEB handling: %d", 997 997 ubi->beb_rsvd_pebs); 998 998 ubi_msg("max/mean erase counter: %d/%d", ubi->max_ec, ubi->mean_ec); 999 + ubi_msg("image sequence number: %d", ubi->image_seq); 999 1000 1000 1001 /* 1001 1002 * The below lock makes sure we do not race with 'ubi_thread()' which
+2
drivers/mtd/ubi/debug.c
··· 44 44 be32_to_cpu(ec_hdr->vid_hdr_offset)); 45 45 printk(KERN_DEBUG "\tdata_offset %d\n", 46 46 be32_to_cpu(ec_hdr->data_offset)); 47 + printk(KERN_DEBUG "\timage_seq %d\n", 48 + be32_to_cpu(ec_hdr->image_seq)); 47 49 printk(KERN_DEBUG "\thdr_crc %#08x\n", 48 50 be32_to_cpu(ec_hdr->hdr_crc)); 49 51 printk(KERN_DEBUG "erase counter header hexdump:\n");
+13 -2
drivers/mtd/ubi/io.c
··· 563 563 * This function returns zero if the erase counter header is OK, and %1 if 564 564 * not. 565 565 */ 566 - static int validate_ec_hdr(const struct ubi_device *ubi, 566 + static int validate_ec_hdr(struct ubi_device *ubi, 567 567 const struct ubi_ec_hdr *ec_hdr) 568 568 { 569 569 long long ec; 570 - int vid_hdr_offset, leb_start; 570 + int vid_hdr_offset, leb_start, image_seq; 571 571 572 572 ec = be64_to_cpu(ec_hdr->ec); 573 573 vid_hdr_offset = be32_to_cpu(ec_hdr->vid_hdr_offset); 574 574 leb_start = be32_to_cpu(ec_hdr->data_offset); 575 + image_seq = be32_to_cpu(ec_hdr->image_seq); 575 576 576 577 if (ec_hdr->version != UBI_VERSION) { 577 578 ubi_err("node with incompatible UBI version found: " ··· 595 594 596 595 if (ec < 0 || ec > UBI_MAX_ERASECOUNTER) { 597 596 ubi_err("bad erase counter %lld", ec); 597 + goto bad; 598 + } 599 + 600 + if (!ubi->image_seq_set) { 601 + ubi->image_seq = image_seq; 602 + ubi->image_seq_set = 1; 603 + } else if (ubi->image_seq != image_seq) { 604 + ubi_err("bad image sequence number %d, expected %d", 605 + image_seq, ubi->image_seq); 598 606 goto bad; 599 607 } 600 608 ··· 752 742 ec_hdr->version = UBI_VERSION; 753 743 ec_hdr->vid_hdr_offset = cpu_to_be32(ubi->vid_hdr_offset); 754 744 ec_hdr->data_offset = cpu_to_be32(ubi->leb_start); 745 + ec_hdr->image_seq = cpu_to_be32(ubi->image_seq); 755 746 crc = crc32(UBI_CRC32_INIT, ec_hdr, UBI_EC_HDR_SIZE_CRC); 756 747 ec_hdr->hdr_crc = cpu_to_be32(crc); 757 748
+2
drivers/mtd/ubi/scan.c
··· 910 910 if (si->is_empty) 911 911 ubi_msg("empty MTD device detected"); 912 912 913 + ubi->image_seq_set = 1; 914 + 913 915 /* 914 916 * In case of unknown erase counter we use the mean erase counter 915 917 * value.
+11 -1
drivers/mtd/ubi/ubi-media.h
··· 129 129 * @ec: the erase counter 130 130 * @vid_hdr_offset: where the VID header starts 131 131 * @data_offset: where the user data start 132 + * @image_seq: image sequence number 132 133 * @padding2: reserved for future, zeroes 133 134 * @hdr_crc: erase counter header CRC checksum 134 135 * ··· 145 144 * volume identifier header and user data, relative to the beginning of the 146 145 * physical eraseblock. These values have to be the same for all physical 147 146 * eraseblocks. 147 + * 148 + * The @image_seq field is used to validate a UBI image that has been prepared 149 + * for a UBI device. The @image_seq value can be any value, but it must be the 150 + * same on all eraseblocks. UBI will ensure that all new erase counter headers 151 + * also contain this value, and will check the value when scanning at start-up. 152 + * One way to make use of @image_seq is to increase its value by one every time 153 + * an image is flashed over an existing image, then, if the flashing does not 154 + * complete, UBI will detect the error when scanning. 148 155 */ 149 156 struct ubi_ec_hdr { 150 157 __be32 magic; ··· 161 152 __be64 ec; /* Warning: the current limit is 31-bit anyway! */ 162 153 __be32 vid_hdr_offset; 163 154 __be32 data_offset; 164 - __u8 padding2[36]; 155 + __be32 image_seq; 156 + __u8 padding2[32]; 165 157 __be32 hdr_crc; 166 158 } __attribute__ ((packed)); 167 159
+4
drivers/mtd/ubi/ubi.h
··· 301 301 * @vol->readers, @vol->writers, @vol->exclusive, 302 302 * @vol->ref_count, @vol->mapping and @vol->eba_tbl. 303 303 * @ref_count: count of references on the UBI device 304 + * @image_seq: image sequence number recorded on EC headers 305 + * @image_seq_set: indicates @image_seq is known 304 306 * 305 307 * @rsvd_pebs: count of reserved physical eraseblocks 306 308 * @avail_pebs: count of available physical eraseblocks ··· 392 390 struct ubi_volume *volumes[UBI_MAX_VOLUMES+UBI_INT_VOL_COUNT]; 393 391 spinlock_t volumes_lock; 394 392 int ref_count; 393 + int image_seq; 394 + int image_seq_set; 395 395 396 396 int rsvd_pebs; 397 397 int avail_pebs;