6adac7766959d6e4025792a0e0d9f65cfc3a2194
[openwrt.git] / target / linux / generic / patches-3.18 / 431-mtd-bcm47xxpart-check-for-bad-blocks-when-calculatin.patch
1 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <zajec5@gmail.com>
2 Date: Sat, 2 Jan 2016 01:04:52 +0100
3 Subject: [PATCH] mtd: bcm47xxpart: check for bad blocks when calculating
4  offsets
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
10 ---
11  drivers/mtd/bcm47xxpart.c | 50 +++++++++++++++++++++++++++++++++++++----------
12  1 file changed, 40 insertions(+), 10 deletions(-)
13
14 --- a/drivers/mtd/bcm47xxpart.c
15 +++ b/drivers/mtd/bcm47xxpart.c
16 @@ -61,6 +61,34 @@ static void bcm47xxpart_add_part(struct mtd_partition *part, const char *name,
17         part->mask_flags = mask_flags;
18  }
19  
20 +/*
21 + * Calculate real end offset (address) for a given amount of data. It checks
22 + * all blocks skipping bad ones.
23 + */
24 +static size_t bcm47xxpart_real_offset(struct mtd_info *master, size_t offset,
25 +                                     size_t bytes)
26 +{
27 +       size_t real_offset = offset;
28 +
29 +       if (mtd_block_isbad(master, real_offset))
30 +               pr_warn("Base offset shouldn't be at bad block");
31 +
32 +       while (bytes >= master->erasesize) {
33 +               bytes -= master->erasesize;
34 +               real_offset += master->erasesize;
35 +               while (mtd_block_isbad(master, real_offset)) {
36 +                       real_offset += master->erasesize;
37 +
38 +                       if (real_offset >= master->size)
39 +                               return real_offset - master->erasesize;
40 +               }
41 +       }
42 +
43 +       real_offset += bytes;
44 +
45 +       return real_offset;
46 +}
47 +
48  static const char *bcm47xxpart_trx_data_part_name(struct mtd_info *master,
49                                                   size_t offset)
50  {
51 @@ -182,6 +210,8 @@ static int bcm47xxpart_parse(struct mtd_info *master,
52  
53                 /* TRX */
54                 if (buf[0x000 / 4] == TRX_MAGIC) {
55 +                       uint32_t tmp;
56 +
57                         if (BCM47XXPART_MAX_PARTS - curr_part < 4) {
58                                 pr_warn("Not enough partitions left to register trx, scanning stopped!\n");
59                                 break;
60 @@ -196,18 +226,18 @@ static int bcm47xxpart_parse(struct mtd_info *master,
61                         i = 0;
62                         /* We have LZMA loader if offset[2] points to sth */
63                         if (trx->offset[2]) {
64 +                               tmp = bcm47xxpart_real_offset(master, offset,
65 +                                                             trx->offset[i]);
66                                 bcm47xxpart_add_part(&parts[curr_part++],
67 -                                                    "loader",
68 -                                                    offset + trx->offset[i],
69 -                                                    0);
70 +                                                    "loader", tmp, 0);
71                                 i++;
72                         }
73  
74                         if (trx->offset[i]) {
75 +                               tmp = bcm47xxpart_real_offset(master, offset,
76 +                                                             trx->offset[i]);
77                                 bcm47xxpart_add_part(&parts[curr_part++],
78 -                                                    "linux",
79 -                                                    offset + trx->offset[i],
80 -                                                    0);
81 +                                                    "linux", tmp, 0);
82                                 i++;
83                         }
84  
85 @@ -219,11 +249,11 @@ static int bcm47xxpart_parse(struct mtd_info *master,
86                         if (trx->offset[i]) {
87                                 const char *name;
88  
89 -                               name = bcm47xxpart_trx_data_part_name(master, offset + trx->offset[i]);
90 +                               tmp = bcm47xxpart_real_offset(master, offset,
91 +                                                             trx->offset[i]);
92 +                               name = bcm47xxpart_trx_data_part_name(master, tmp);
93                                 bcm47xxpart_add_part(&parts[curr_part++],
94 -                                                    name,
95 -                                                    offset + trx->offset[i],
96 -                                                    0);
97 +                                                    name, tmp, 0);
98                                 i++;
99                         }
100