Rebased from upstream / out of band repository.
[librecmc/librecmc.git] / target / linux / generic / pending-4.19 / 494-mtd-ubi-add-EOF-marker-support.patch
1 From: Gabor Juhos <juhosg@openwrt.org>
2 Subject: mtd: add EOF marker support to the UBI layer
3
4 Signed-off-by: Gabor Juhos <juhosg@openwrt.org>
5 ---
6  drivers/mtd/ubi/attach.c | 25 ++++++++++++++++++++++---
7  drivers/mtd/ubi/ubi.h    |  1 +
8  2 files changed, 23 insertions(+), 3 deletions(-)
9
10 --- a/drivers/mtd/ubi/attach.c
11 +++ b/drivers/mtd/ubi/attach.c
12 @@ -939,6 +939,13 @@ static bool vol_ignored(int vol_id)
13  #endif
14  }
15  
16 +static bool ec_hdr_has_eof(struct ubi_ec_hdr *ech)
17 +{
18 +       return ech->padding1[0] == 'E' &&
19 +              ech->padding1[1] == 'O' &&
20 +              ech->padding1[2] == 'F';
21 +}
22 +
23  /**
24   * scan_peb - scan and process UBI headers of a PEB.
25   * @ubi: UBI device description object
26 @@ -971,9 +978,21 @@ static int scan_peb(struct ubi_device *u
27                 return 0;
28         }
29  
30 -       err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
31 -       if (err < 0)
32 -               return err;
33 +       if (!ai->eof_found) {
34 +               err = ubi_io_read_ec_hdr(ubi, pnum, ech, 0);
35 +               if (err < 0)
36 +                       return err;
37 +
38 +               if (ec_hdr_has_eof(ech)) {
39 +                       pr_notice("UBI: EOF marker found, PEBs from %d will be erased\n",
40 +                               pnum);
41 +                       ai->eof_found = true;
42 +               }
43 +       }
44 +
45 +       if (ai->eof_found)
46 +               err = UBI_IO_FF_BITFLIPS;
47 +
48         switch (err) {
49         case 0:
50                 break;
51 --- a/drivers/mtd/ubi/ubi.h
52 +++ b/drivers/mtd/ubi/ubi.h
53 @@ -789,6 +789,7 @@ struct ubi_attach_info {
54         int mean_ec;
55         uint64_t ec_sum;
56         int ec_count;
57 +       bool eof_found;
58         struct kmem_cache *aeb_slab_cache;
59         struct ubi_ec_hdr *ech;
60         struct ubi_vid_io_buf *vidb;