2d97be750839d6d33443388d096499bd8b85352e
[librecmc/librecmc.git] /
1 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
2 Subject: [PATCH 2/2] mtd: bcm47xxpart: support layouts with multiple TRX
3  partitions
4 MIME-Version: 1.0
5 Content-Type: text/plain; charset=UTF-8
6 Content-Transfer-Encoding: 8bit
7
8 Some devices may have an extra TRX partition used as failsafe one. If
9 we detect such partition we should set a proper name for it and don't
10 parse it.
11
12 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
13 ---
14  drivers/mtd/bcm47xxpart.c | 56 ++++++++++++++++++++++++++++++++++++++---------
15  1 file changed, 46 insertions(+), 10 deletions(-)
16
17 --- a/drivers/mtd/bcm47xxpart.c
18 +++ b/drivers/mtd/bcm47xxpart.c
19 @@ -9,6 +9,7 @@
20   *
21   */
22  
23 +#include <linux/bcm47xx_nvram.h>
24  #include <linux/module.h>
25  #include <linux/kernel.h>
26  #include <linux/slab.h>
27 @@ -144,6 +145,30 @@ static int bcm47xxpart_parse_trx(struct
28         return curr_part;
29  }
30  
31 +/**
32 + * bcm47xxpart_bootpartition - gets index of TRX partition used by bootloader
33 + *
34 + * Some devices may have more than one TRX partition. In such case one of them
35 + * is the main one and another a failsafe one. Bootloader may fallback to the
36 + * failsafe firmware if it detects corruption of the main image.
37 + *
38 + * This function provides info about currently used TRX partition. It's the one
39 + * containing kernel started by the bootloader.
40 + */
41 +static int bcm47xxpart_bootpartition(void)
42 +{
43 +       char buf[4];
44 +       int bootpartition;
45 +
46 +       /* Check CFE environment variable */
47 +       if (bcm47xx_nvram_getenv("bootpartition", buf, sizeof(buf)) > 0) {
48 +               if (!kstrtoint(buf, 0, &bootpartition))
49 +                       return bootpartition;
50 +       }
51 +
52 +       return 0;
53 +}
54 +
55  static int bcm47xxpart_parse(struct mtd_info *master,
56                              struct mtd_partition **pparts,
57                              struct mtd_part_parser_data *data)
58 @@ -154,7 +179,8 @@ static int bcm47xxpart_parse(struct mtd_
59         size_t bytes_read;
60         uint32_t offset;
61         uint32_t blocksize = master->erasesize;
62 -       int trx_part = -1;
63 +       int trx_parts[2]; /* Array with indexes of TRX partitions */
64 +       int trx_num = 0; /* Number of found TRX partitions */
65         int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
66         int err;
67  
68 @@ -243,7 +269,11 @@ static int bcm47xxpart_parse(struct mtd_
69                 if (buf[0x000 / 4] == TRX_MAGIC) {
70                         struct trx_header *trx;
71  
72 -                       trx_part = curr_part;
73 +                       if (trx_num >= ARRAY_SIZE(trx_parts))
74 +                               pr_warn("No enough space to store another TRX found at 0x%X\n",
75 +                                       offset);
76 +                       else
77 +                               trx_parts[trx_num++] = curr_part;
78                         bcm47xxpart_add_part(&parts[curr_part++], "firmware",
79                                              offset, 0);
80  
81 @@ -329,14 +359,20 @@ static int bcm47xxpart_parse(struct mtd_
82         }
83  
84         /* If there was TRX parse it now */
85 -       if (trx_part >= 0) {
86 -               int num_parts;
87 +       for (i = 0; i < trx_num; i++) {
88 +               struct mtd_partition *trx = &parts[trx_parts[i]];
89  
90 -               num_parts = bcm47xxpart_parse_trx(master, &parts[trx_part],
91 -                                                 parts + curr_part,
92 -                                                 BCM47XXPART_MAX_PARTS - curr_part);
93 -               if (num_parts > 0)
94 -                       curr_part += num_parts;
95 +               if (i == bcm47xxpart_bootpartition()) {
96 +                       int num_parts;
97 +
98 +                       num_parts = bcm47xxpart_parse_trx(master, trx,
99 +                                                         parts + curr_part,
100 +                                                         BCM47XXPART_MAX_PARTS - curr_part);
101 +                       if (num_parts > 0)
102 +                               curr_part += num_parts;
103 +               } else {
104 +                       trx->name = "failsafe";
105 +               }
106         }
107  
108         *pparts = parts;