1 From 7134a2d026d942210b4d26d6059c9d979ca7866e Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Fri, 12 Mar 2021 14:49:19 +0100
4 Subject: [PATCH] mtd: parsers: ofpart: support Linksys Northstar partitions
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
9 This allows extending ofpart parser with support for Linksys Northstar
10 devices. That support uses recently added quirks mechanism.
12 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
13 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
14 Link: https://lore.kernel.org/linux-mtd/20210312134919.7767-2-zajec5@gmail.com
16 drivers/mtd/parsers/Kconfig | 10 +++++
17 drivers/mtd/parsers/Makefile | 1 +
18 drivers/mtd/parsers/ofpart_core.c | 6 +++
19 drivers/mtd/parsers/ofpart_linksys_ns.c | 50 +++++++++++++++++++++++++
20 drivers/mtd/parsers/ofpart_linksys_ns.h | 18 +++++++++
21 5 files changed, 85 insertions(+)
22 create mode 100644 drivers/mtd/parsers/ofpart_linksys_ns.c
23 create mode 100644 drivers/mtd/parsers/ofpart_linksys_ns.h
25 --- a/drivers/mtd/parsers/Kconfig
26 +++ b/drivers/mtd/parsers/Kconfig
27 @@ -76,6 +76,16 @@ config MTD_OF_PARTS_BCM4908
28 that can have multiple "firmware" partitions. It takes care of
29 finding currently used one and backup ones.
31 +config MTD_OF_PARTS_LINKSYS_NS
32 + bool "Linksys Northstar partitioning support"
33 + depends on MTD_OF_PARTS && (ARCH_BCM_5301X || ARCH_BCM4908 || COMPILE_TEST)
34 + default ARCH_BCM_5301X
36 + This provides partitions parser for Linksys devices based on Broadcom
37 + Northstar architecture. Linksys commonly uses fixed flash layout with
38 + two "firmware" partitions. Currently used firmware has to be detected
39 + using CFE environment variable.
41 config MTD_PARSER_IMAGETAG
42 tristate "Parser for BCM963XX Image Tag format partitions"
43 depends on BCM63XX || BMIPS_GENERIC || COMPILE_TEST
44 --- a/drivers/mtd/parsers/Makefile
45 +++ b/drivers/mtd/parsers/Makefile
46 @@ -6,6 +6,7 @@ obj-$(CONFIG_MTD_CMDLINE_PARTS) += cmdl
47 obj-$(CONFIG_MTD_OF_PARTS) += ofpart.o
48 ofpart-y += ofpart_core.o
49 ofpart-$(CONFIG_MTD_OF_PARTS_BCM4908) += ofpart_bcm4908.o
50 +ofpart-$(CONFIG_MTD_OF_PARTS_LINKSYS_NS)+= ofpart_linksys_ns.o
51 obj-$(CONFIG_MTD_PARSER_IMAGETAG) += parser_imagetag.o
52 obj-$(CONFIG_MTD_AFS_PARTS) += afs.o
53 obj-$(CONFIG_MTD_PARSER_TRX) += parser_trx.o
54 --- a/drivers/mtd/parsers/ofpart_core.c
55 +++ b/drivers/mtd/parsers/ofpart_core.c
57 #include <linux/mtd/partitions.h>
59 #include "ofpart_bcm4908.h"
60 +#include "ofpart_linksys_ns.h"
62 struct fixed_partitions_quirks {
63 int (*post_parse)(struct mtd_info *mtd, struct mtd_partition *parts, int nr_parts);
64 @@ -26,6 +27,10 @@ static struct fixed_partitions_quirks bc
65 .post_parse = bcm4908_partitions_post_parse,
68 +static struct fixed_partitions_quirks linksys_ns_partitions_quirks = {
69 + .post_parse = linksys_ns_partitions_post_parse,
72 static const struct of_device_id parse_ofpart_match_table[];
74 static bool node_has_compatible(struct device_node *pp)
75 @@ -167,6 +172,7 @@ static const struct of_device_id parse_o
76 { .compatible = "fixed-partitions" },
78 { .compatible = "brcm,bcm4908-partitions", .data = &bcm4908_partitions_quirks, },
79 + { .compatible = "linksys,ns-partitions", .data = &linksys_ns_partitions_quirks, },
82 MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
84 +++ b/drivers/mtd/parsers/ofpart_linksys_ns.c
86 +// SPDX-License-Identifier: GPL-2.0
88 + * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
91 +#include <linux/bcm47xx_nvram.h>
92 +#include <linux/mtd/mtd.h>
93 +#include <linux/mtd/partitions.h>
95 +#include "ofpart_linksys_ns.h"
97 +#define NVRAM_BOOT_PART "bootpartition"
99 +static int ofpart_linksys_ns_bootpartition(void)
104 + /* Check CFE environment variable */
105 + if (bcm47xx_nvram_getenv(NVRAM_BOOT_PART, buf, sizeof(buf)) > 0) {
106 + if (!kstrtoint(buf, 0, &bootpartition))
107 + return bootpartition;
108 + pr_warn("Failed to parse %s value \"%s\"\n", NVRAM_BOOT_PART,
111 + pr_warn("Failed to get NVRAM \"%s\"\n", NVRAM_BOOT_PART);
117 +int linksys_ns_partitions_post_parse(struct mtd_info *mtd,
118 + struct mtd_partition *parts,
121 + int bootpartition = ofpart_linksys_ns_bootpartition();
125 + for (i = 0; i < nr_parts; i++) {
126 + if (of_device_is_compatible(parts[i].of_node, "linksys,ns-firmware")) {
127 + if (trx_idx++ == bootpartition)
128 + parts[i].name = "firmware";
130 + parts[i].name = "backup";
137 +++ b/drivers/mtd/parsers/ofpart_linksys_ns.h
139 +/* SPDX-License-Identifier: GPL-2.0 */
140 +#ifndef __OFPART_LINKSYS_NS_H
141 +#define __OFPART_LINKSYS_NS_H
143 +#ifdef CONFIG_MTD_OF_PARTS_LINKSYS_NS
144 +int linksys_ns_partitions_post_parse(struct mtd_info *mtd,
145 + struct mtd_partition *parts,
148 +static inline int linksys_ns_partitions_post_parse(struct mtd_info *mtd,
149 + struct mtd_partition *parts,
152 + return -EOPNOTSUPP;