mtd: bcm47xxpart: support layouts with multiple TRX partitions

Linux-MTD Mailing List linux-mtd at lists.infradead.org
Sun Feb 12 18:59:06 PST 2017


Gitweb:     http://git.infradead.org/?p=mtd-2.6.git;a=commit;h=89a0d9a9f1941a086a82bc7cd73d275cec98ba14
Commit:     89a0d9a9f1941a086a82bc7cd73d275cec98ba14
Parent:     b522d7b0ebe3539340c2a6d46d787ae3d33bcb92
Author:     Rafał Miłecki <rafal at milecki.pl>
AuthorDate: Tue Jan 10 23:15:25 2017 +0100
Committer:  Brian Norris <computersforpeace at gmail.com>
CommitDate: Thu Feb 9 18:59:45 2017 -0800

    mtd: bcm47xxpart: support layouts with multiple TRX partitions
    
    Some devices may have an extra TRX partition used as failsafe one. If
    we detect such partition we should set a proper name for it and don't
    parse it.
    
    Signed-off-by: Rafał Miłecki <rafal at milecki.pl>
    Acked-by: Marek Vasut <marek.vasut at gmail.com>
    Signed-off-by: Brian Norris <computersforpeace at gmail.com>
---
 drivers/mtd/bcm47xxpart.c | 56 ++++++++++++++++++++++++++++++++++++++---------
 1 file changed, 46 insertions(+), 10 deletions(-)

diff --git a/drivers/mtd/bcm47xxpart.c b/drivers/mtd/bcm47xxpart.c
index 1093025..d10fa6c 100644
--- a/drivers/mtd/bcm47xxpart.c
+++ b/drivers/mtd/bcm47xxpart.c
@@ -9,6 +9,7 @@
  *
  */
 
+#include <linux/bcm47xx_nvram.h>
 #include <linux/module.h>
 #include <linux/kernel.h>
 #include <linux/slab.h>
@@ -144,6 +145,30 @@ static int bcm47xxpart_parse_trx(struct mtd_info *master,
 	return curr_part;
 }
 
+/**
+ * bcm47xxpart_bootpartition - gets index of TRX partition used by bootloader
+ *
+ * Some devices may have more than one TRX partition. In such case one of them
+ * is the main one and another a failsafe one. Bootloader may fallback to the
+ * failsafe firmware if it detects corruption of the main image.
+ *
+ * This function provides info about currently used TRX partition. It's the one
+ * containing kernel started by the bootloader.
+ */
+static int bcm47xxpart_bootpartition(void)
+{
+	char buf[4];
+	int bootpartition;
+
+	/* Check CFE environment variable */
+	if (bcm47xx_nvram_getenv("bootpartition", buf, sizeof(buf)) > 0) {
+		if (!kstrtoint(buf, 0, &bootpartition))
+			return bootpartition;
+	}
+
+	return 0;
+}
+
 static int bcm47xxpart_parse(struct mtd_info *master,
 			     const struct mtd_partition **pparts,
 			     struct mtd_part_parser_data *data)
@@ -154,7 +179,8 @@ static int bcm47xxpart_parse(struct mtd_info *master,
 	size_t bytes_read;
 	uint32_t offset;
 	uint32_t blocksize = master->erasesize;
-	int trx_part = -1;
+	int trx_parts[2]; /* Array with indexes of TRX partitions */
+	int trx_num = 0; /* Number of found TRX partitions */
 	int possible_nvram_sizes[] = { 0x8000, 0xF000, 0x10000, };
 	int err;
 
@@ -243,7 +269,11 @@ static int bcm47xxpart_parse(struct mtd_info *master,
 		if (buf[0x000 / 4] == TRX_MAGIC) {
 			struct trx_header *trx;
 
-			trx_part = curr_part;
+			if (trx_num >= ARRAY_SIZE(trx_parts))
+				pr_warn("No enough space to store another TRX found at 0x%X\n",
+					offset);
+			else
+				trx_parts[trx_num++] = curr_part;
 			bcm47xxpart_add_part(&parts[curr_part++], "firmware",
 					     offset, 0);
 
@@ -329,14 +359,20 @@ static int bcm47xxpart_parse(struct mtd_info *master,
 	}
 
 	/* If there was TRX parse it now */
-	if (trx_part >= 0) {
-		int num_parts;
-
-		num_parts = bcm47xxpart_parse_trx(master, &parts[trx_part],
-						  parts + curr_part,
-						  BCM47XXPART_MAX_PARTS - curr_part);
-		if (num_parts > 0)
-			curr_part += num_parts;
+	for (i = 0; i < trx_num; i++) {
+		struct mtd_partition *trx = &parts[trx_parts[i]];
+
+		if (i == bcm47xxpart_bootpartition()) {
+			int num_parts;
+
+			num_parts = bcm47xxpart_parse_trx(master, trx,
+							  parts + curr_part,
+							  BCM47XXPART_MAX_PARTS - curr_part);
+			if (num_parts > 0)
+				curr_part += num_parts;
+		} else {
+			trx->name = "failsafe";
+		}
 	}
 
 	*pparts = parts;



More information about the linux-mtd-cvs mailing list