d54c284c343b5c71040d9b76b485ea0d870fd684
[oweals/openwrt.git] / target / linux / generic / pending-5.4 / 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 Subject: [PATCH] mtd: bcm47xxpart: check for bad blocks when calculating offsets
3
4 Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
5 ---
6
7 --- a/drivers/mtd/parsers/parser_trx.c
8 +++ b/drivers/mtd/parsers/parser_trx.c
9 @@ -29,6 +29,33 @@ struct trx_header {
10         uint32_t offset[3];
11  } __packed;
12  
13 +/*
14 + * Calculate real end offset (address) for a given amount of data. It checks
15 + * all blocks skipping bad ones.
16 + */
17 +static size_t parser_trx_real_offset(struct mtd_info *mtd, size_t bytes)
18 +{
19 +       size_t real_offset = 0;
20 +
21 +       if (mtd_block_isbad(mtd, real_offset))
22 +               pr_warn("Base offset shouldn't be at bad block");
23 +
24 +       while (bytes >= mtd->erasesize) {
25 +               bytes -= mtd->erasesize;
26 +               real_offset += mtd->erasesize;
27 +               while (mtd_block_isbad(mtd, real_offset)) {
28 +                       real_offset += mtd->erasesize;
29 +
30 +                       if (real_offset >= mtd->size)
31 +                               return real_offset - mtd->erasesize;
32 +               }
33 +       }
34 +
35 +       real_offset += bytes;
36 +
37 +       return real_offset;
38 +}
39 +
40  static const char *parser_trx_data_part_name(struct mtd_info *master,
41                                              size_t offset)
42  {
43 @@ -83,21 +110,21 @@ static int parser_trx_parse(struct mtd_i
44         if (trx.offset[2]) {
45                 part = &parts[curr_part++];
46                 part->name = "loader";
47 -               part->offset = trx.offset[i];
48 +               part->offset = parser_trx_real_offset(mtd, trx.offset[i]);
49                 i++;
50         }
51  
52         if (trx.offset[i]) {
53                 part = &parts[curr_part++];
54                 part->name = "linux";
55 -               part->offset = trx.offset[i];
56 +               part->offset = parser_trx_real_offset(mtd, trx.offset[i]);
57                 i++;
58         }
59  
60         if (trx.offset[i]) {
61                 part = &parts[curr_part++];
62 -               part->name = parser_trx_data_part_name(mtd, trx.offset[i]);
63 -               part->offset = trx.offset[i];
64 +               part->offset = parser_trx_real_offset(mtd, trx.offset[i]);
65 +               part->name = parser_trx_data_part_name(mtd, part->offset);
66                 i++;
67         }
68