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
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
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
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>
18 drivers/mtd/bcm47xxpart.c | 56 ++++++++++++++++++++++++++++++++++++++---------
19 1 file changed, 46 insertions(+), 10 deletions(-)
21 --- a/drivers/mtd/bcm47xxpart.c
22 +++ b/drivers/mtd/bcm47xxpart.c
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
36 + * bcm47xxpart_bootpartition - gets index of TRX partition used by bootloader
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.
42 + * This function provides info about currently used TRX partition. It's the one
43 + * containing kernel started by the bootloader.
45 +static int bcm47xxpart_bootpartition(void)
50 + /* Check CFE environment variable */
51 + if (bcm47xx_nvram_getenv("bootpartition", buf, sizeof(buf)) > 0) {
52 + if (!kstrtoint(buf, 0, &bootpartition))
53 + return bootpartition;
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_
65 uint32_t blocksize = master->erasesize;
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, };
72 @@ -243,7 +269,11 @@ static int bcm47xxpart_parse(struct mtd_
73 if (buf[0x000 / 4] == TRX_MAGIC) {
74 struct trx_header *trx;
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",
81 + trx_parts[trx_num++] = curr_part;
82 bcm47xxpart_add_part(&parts[curr_part++], "firmware",
85 @@ -329,14 +359,20 @@ static int bcm47xxpart_parse(struct mtd_
88 /* If there was TRX parse it now */
89 - if (trx_part >= 0) {
91 + for (i = 0; i < trx_num; i++) {
92 + struct mtd_partition *trx = &parts[trx_parts[i]];
94 - num_parts = bcm47xxpart_parse_trx(master, &parts[trx_part],
96 - BCM47XXPART_MAX_PARTS - curr_part);
98 - curr_part += num_parts;
99 + if (i == bcm47xxpart_bootpartition()) {
102 + num_parts = bcm47xxpart_parse_trx(master, trx,
104 + BCM47XXPART_MAX_PARTS - curr_part);
106 + curr_part += num_parts;
108 + trx->name = "failsafe";