f3178897857cde5523d34b9fc8ae5e38014f4f33
[openwrt/staging/jow.git] /
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
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This allows extending ofpart parser with support for Linksys Northstar
10 devices. That support uses recently added quirks mechanism.
11
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
15 ---
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
24
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.
30
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
35 + help
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.
40 +
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
56 @@ -17,6 +17,7 @@
57 #include <linux/mtd/partitions.h>
58
59 #include "ofpart_bcm4908.h"
60 +#include "ofpart_linksys_ns.h"
61
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,
66 };
67
68 +static struct fixed_partitions_quirks linksys_ns_partitions_quirks = {
69 + .post_parse = linksys_ns_partitions_post_parse,
70 +};
71 +
72 static const struct of_device_id parse_ofpart_match_table[];
73
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" },
77 /* Customized */
78 { .compatible = "brcm,bcm4908-partitions", .data = &bcm4908_partitions_quirks, },
79 + { .compatible = "linksys,ns-partitions", .data = &linksys_ns_partitions_quirks, },
80 {},
81 };
82 MODULE_DEVICE_TABLE(of, parse_ofpart_match_table);
83 --- /dev/null
84 +++ b/drivers/mtd/parsers/ofpart_linksys_ns.c
85 @@ -0,0 +1,50 @@
86 +// SPDX-License-Identifier: GPL-2.0
87 +/*
88 + * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
89 + */
90 +
91 +#include <linux/bcm47xx_nvram.h>
92 +#include <linux/mtd/mtd.h>
93 +#include <linux/mtd/partitions.h>
94 +
95 +#include "ofpart_linksys_ns.h"
96 +
97 +#define NVRAM_BOOT_PART "bootpartition"
98 +
99 +static int ofpart_linksys_ns_bootpartition(void)
100 +{
101 + char buf[4];
102 + int bootpartition;
103 +
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,
109 + buf);
110 + } else {
111 + pr_warn("Failed to get NVRAM \"%s\"\n", NVRAM_BOOT_PART);
112 + }
113 +
114 + return 0;
115 +}
116 +
117 +int linksys_ns_partitions_post_parse(struct mtd_info *mtd,
118 + struct mtd_partition *parts,
119 + int nr_parts)
120 +{
121 + int bootpartition = ofpart_linksys_ns_bootpartition();
122 + int trx_idx = 0;
123 + int i;
124 +
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";
129 + else
130 + parts[i].name = "backup";
131 + }
132 + }
133 +
134 + return 0;
135 +}
136 --- /dev/null
137 +++ b/drivers/mtd/parsers/ofpart_linksys_ns.h
138 @@ -0,0 +1,18 @@
139 +/* SPDX-License-Identifier: GPL-2.0 */
140 +#ifndef __OFPART_LINKSYS_NS_H
141 +#define __OFPART_LINKSYS_NS_H
142 +
143 +#ifdef CONFIG_MTD_OF_PARTS_LINKSYS_NS
144 +int linksys_ns_partitions_post_parse(struct mtd_info *mtd,
145 + struct mtd_partition *parts,
146 + int nr_parts);
147 +#else
148 +static inline int linksys_ns_partitions_post_parse(struct mtd_info *mtd,
149 + struct mtd_partition *parts,
150 + int nr_parts)
151 +{
152 + return -EOPNOTSUPP;
153 +}
154 +#endif
155 +
156 +#endif