kernel: backport MTD patch extracing TRX code to separated parser
[openwrt/openwrt.git] / target / linux / generic / patches-4.9 / 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
12 --- a/drivers/mtd/parsers/parser_trx.c
13 +++ b/drivers/mtd/parsers/parser_trx.c
14 @@ -29,6 +29,33 @@ struct trx_header {
15 uint32_t offset[3];
16 } __packed;
17
18 +/*
19 + * Calculate real end offset (address) for a given amount of data. It checks
20 + * all blocks skipping bad ones.
21 + */
22 +static size_t parser_trx_real_offset(struct mtd_info *mtd, size_t bytes)
23 +{
24 + size_t real_offset = 0;
25 +
26 + if (mtd_block_isbad(mtd, real_offset))
27 + pr_warn("Base offset shouldn't be at bad block");
28 +
29 + while (bytes >= mtd->erasesize) {
30 + bytes -= mtd->erasesize;
31 + real_offset += mtd->erasesize;
32 + while (mtd_block_isbad(mtd, real_offset)) {
33 + real_offset += mtd->erasesize;
34 +
35 + if (real_offset >= mtd->size)
36 + return real_offset - mtd->erasesize;
37 + }
38 + }
39 +
40 + real_offset += bytes;
41 +
42 + return real_offset;
43 +}
44 +
45 static const char *parser_trx_data_part_name(struct mtd_info *master,
46 size_t offset)
47 {
48 @@ -83,21 +110,21 @@ static int parser_trx_parse(struct mtd_i
49 if (trx.offset[2]) {
50 part = &parts[curr_part++];
51 part->name = "loader";
52 - part->offset = trx.offset[i];
53 + part->offset = parser_trx_real_offset(mtd, trx.offset[i]);
54 i++;
55 }
56
57 if (trx.offset[i]) {
58 part = &parts[curr_part++];
59 part->name = "linux";
60 - part->offset = trx.offset[i];
61 + part->offset = parser_trx_real_offset(mtd, trx.offset[i]);
62 i++;
63 }
64
65 if (trx.offset[i]) {
66 part = &parts[curr_part++];
67 part->name = parser_trx_data_part_name(mtd, trx.offset[i]);
68 - part->offset = trx.offset[i];
69 + part->offset = parser_trx_real_offset(mtd, trx.offset[i]);
70 i++;
71 }
72