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