kernel: backport MTD patch extracing TRX code to separated parser
[openwrt/openwrt.git] / target / linux / generic / patches-4.9 / 065-v4.13-0008-mtd-extract-TRX-parser-out-of-bcm47xxpart-into-a-sep.patch
1 From 99352afe8f169c95b294b6b9a8d0e18cd9e3c2a0 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Wed, 21 Jun 2017 08:26:47 +0200
4 Subject: [PATCH] mtd: extract TRX parser out of bcm47xxpart into a separated
5 module
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 This makes TRX parsing code reusable with other platforms and parsers.
11
12 Please note this patch doesn't really change anything in the existing
13 code, just moves it. There is still some place for improvement (e.g.
14 working on non-hacky method of checking rootfs format) but it's not
15 really a subject of this change.
16
17 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
18 Signed-off-by: Brian Norris <computersforpeace@gmail.com>
19 ---
20 drivers/mtd/Kconfig | 4 ++
21 drivers/mtd/Makefile | 1 +
22 drivers/mtd/bcm47xxpart.c | 99 ++----------------------------
23 drivers/mtd/parsers/Kconfig | 8 +++
24 drivers/mtd/parsers/Makefile | 1 +
25 drivers/mtd/parsers/parser_trx.c | 126 +++++++++++++++++++++++++++++++++++++++
26 6 files changed, 145 insertions(+), 94 deletions(-)
27 create mode 100644 drivers/mtd/parsers/Kconfig
28 create mode 100644 drivers/mtd/parsers/Makefile
29 create mode 100644 drivers/mtd/parsers/parser_trx.c
30
31 --- a/drivers/mtd/Kconfig
32 +++ b/drivers/mtd/Kconfig
33 @@ -155,6 +155,10 @@ config MTD_BCM47XX_PARTS
34 This provides partitions parser for devices based on BCM47xx
35 boards.
36
37 +menu "Partition parsers"
38 +source "drivers/mtd/parsers/Kconfig"
39 +endmenu
40 +
41 comment "User Modules And Translation Layers"
42
43 #
44 --- a/drivers/mtd/Makefile
45 +++ b/drivers/mtd/Makefile
46 @@ -13,6 +13,7 @@ obj-$(CONFIG_MTD_AFS_PARTS) += afs.o
47 obj-$(CONFIG_MTD_AR7_PARTS) += ar7part.o
48 obj-$(CONFIG_MTD_BCM63XX_PARTS) += bcm63xxpart.o
49 obj-$(CONFIG_MTD_BCM47XX_PARTS) += bcm47xxpart.o
50 +obj-y += parsers/
51
52 # 'Users' - code which presents functionality to userspace.
53 obj-$(CONFIG_MTD_BLKDEVS) += mtd_blkdevs.o
54 --- a/drivers/mtd/bcm47xxpart.c
55 +++ b/drivers/mtd/bcm47xxpart.c
56 @@ -43,7 +43,8 @@
57 #define ML_MAGIC2 0x26594131
58 #define TRX_MAGIC 0x30524448
59 #define SHSQ_MAGIC 0x71736873 /* shsq (weird ZTE H218N endianness) */
60 -#define UBI_EC_MAGIC 0x23494255 /* UBI# */
61 +
62 +static const char * const trx_types[] = { "trx", NULL };
63
64 struct trx_header {
65 uint32_t magic;
66 @@ -62,89 +63,6 @@ static void bcm47xxpart_add_part(struct
67 part->mask_flags = mask_flags;
68 }
69
70 -static const char *bcm47xxpart_trx_data_part_name(struct mtd_info *master,
71 - size_t offset)
72 -{
73 - uint32_t buf;
74 - size_t bytes_read;
75 - int err;
76 -
77 - err = mtd_read(master, offset, sizeof(buf), &bytes_read,
78 - (uint8_t *)&buf);
79 - if (err && !mtd_is_bitflip(err)) {
80 - pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
81 - offset, err);
82 - goto out_default;
83 - }
84 -
85 - if (buf == UBI_EC_MAGIC)
86 - return "ubi";
87 -
88 -out_default:
89 - return "rootfs";
90 -}
91 -
92 -static int bcm47xxpart_parse_trx(struct mtd_info *master,
93 - struct mtd_partition *trx,
94 - struct mtd_partition *parts,
95 - size_t parts_len)
96 -{
97 - struct trx_header header;
98 - size_t bytes_read;
99 - int curr_part = 0;
100 - int i, err;
101 -
102 - if (parts_len < 3) {
103 - pr_warn("No enough space to add TRX partitions!\n");
104 - return -ENOMEM;
105 - }
106 -
107 - err = mtd_read(master, trx->offset, sizeof(header), &bytes_read,
108 - (uint8_t *)&header);
109 - if (err && !mtd_is_bitflip(err)) {
110 - pr_err("mtd_read error while reading TRX header: %d\n", err);
111 - return err;
112 - }
113 -
114 - i = 0;
115 -
116 - /* We have LZMA loader if offset[2] points to sth */
117 - if (header.offset[2]) {
118 - bcm47xxpart_add_part(&parts[curr_part++], "loader",
119 - trx->offset + header.offset[i], 0);
120 - i++;
121 - }
122 -
123 - if (header.offset[i]) {
124 - bcm47xxpart_add_part(&parts[curr_part++], "linux",
125 - trx->offset + header.offset[i], 0);
126 - i++;
127 - }
128 -
129 - if (header.offset[i]) {
130 - size_t offset = trx->offset + header.offset[i];
131 - const char *name = bcm47xxpart_trx_data_part_name(master,
132 - offset);
133 -
134 - bcm47xxpart_add_part(&parts[curr_part++], name, offset, 0);
135 - i++;
136 - }
137 -
138 - /*
139 - * Assume that every partition ends at the beginning of the one it is
140 - * followed by.
141 - */
142 - for (i = 0; i < curr_part; i++) {
143 - u64 next_part_offset = (i < curr_part - 1) ?
144 - parts[i + 1].offset :
145 - trx->offset + trx->size;
146 -
147 - parts[i].size = next_part_offset - parts[i].offset;
148 - }
149 -
150 - return curr_part;
151 -}
152 -
153 /**
154 * bcm47xxpart_bootpartition - gets index of TRX partition used by bootloader
155 *
156 @@ -362,17 +280,10 @@ static int bcm47xxpart_parse(struct mtd_
157 for (i = 0; i < trx_num; i++) {
158 struct mtd_partition *trx = &parts[trx_parts[i]];
159
160 - if (i == bcm47xxpart_bootpartition()) {
161 - int num_parts;
162 -
163 - num_parts = bcm47xxpart_parse_trx(master, trx,
164 - parts + curr_part,
165 - BCM47XXPART_MAX_PARTS - curr_part);
166 - if (num_parts > 0)
167 - curr_part += num_parts;
168 - } else {
169 + if (i == bcm47xxpart_bootpartition())
170 + trx->types = trx_types;
171 + else
172 trx->name = "failsafe";
173 - }
174 }
175
176 *pparts = parts;
177 --- /dev/null
178 +++ b/drivers/mtd/parsers/Kconfig
179 @@ -0,0 +1,8 @@
180 +config MTD_PARSER_TRX
181 + tristate "Parser for TRX format partitions"
182 + depends on MTD && (BCM47XX || ARCH_BCM_5301X || COMPILE_TEST)
183 + help
184 + TRX is a firmware format used by Broadcom on their devices. It
185 + may contain up to 3/4 partitions (depending on the version).
186 + This driver will parse TRX header and report at least two partitions:
187 + kernel and rootfs.
188 --- /dev/null
189 +++ b/drivers/mtd/parsers/Makefile
190 @@ -0,0 +1 @@
191 +obj-$(CONFIG_MTD_PARSER_TRX) += parser_trx.o
192 --- /dev/null
193 +++ b/drivers/mtd/parsers/parser_trx.c
194 @@ -0,0 +1,126 @@
195 +/*
196 + * Parser for TRX format partitions
197 + *
198 + * Copyright (C) 2012 - 2017 Rafał Miłecki <rafal@milecki.pl>
199 + *
200 + * This program is free software; you can redistribute it and/or modify
201 + * it under the terms of the GNU General Public License version 2 as
202 + * published by the Free Software Foundation.
203 + *
204 + */
205 +
206 +#include <linux/module.h>
207 +#include <linux/slab.h>
208 +#include <linux/mtd/mtd.h>
209 +#include <linux/mtd/partitions.h>
210 +
211 +#define TRX_PARSER_MAX_PARTS 4
212 +
213 +/* Magics */
214 +#define TRX_MAGIC 0x30524448
215 +#define UBI_EC_MAGIC 0x23494255 /* UBI# */
216 +
217 +struct trx_header {
218 + uint32_t magic;
219 + uint32_t length;
220 + uint32_t crc32;
221 + uint16_t flags;
222 + uint16_t version;
223 + uint32_t offset[3];
224 +} __packed;
225 +
226 +static const char *parser_trx_data_part_name(struct mtd_info *master,
227 + size_t offset)
228 +{
229 + uint32_t buf;
230 + size_t bytes_read;
231 + int err;
232 +
233 + err = mtd_read(master, offset, sizeof(buf), &bytes_read,
234 + (uint8_t *)&buf);
235 + if (err && !mtd_is_bitflip(err)) {
236 + pr_err("mtd_read error while parsing (offset: 0x%X): %d\n",
237 + offset, err);
238 + goto out_default;
239 + }
240 +
241 + if (buf == UBI_EC_MAGIC)
242 + return "ubi";
243 +
244 +out_default:
245 + return "rootfs";
246 +}
247 +
248 +static int parser_trx_parse(struct mtd_info *mtd,
249 + const struct mtd_partition **pparts,
250 + struct mtd_part_parser_data *data)
251 +{
252 + struct mtd_partition *parts;
253 + struct mtd_partition *part;
254 + struct trx_header trx;
255 + size_t bytes_read;
256 + uint8_t curr_part = 0, i = 0;
257 + int err;
258 +
259 + parts = kzalloc(sizeof(struct mtd_partition) * TRX_PARSER_MAX_PARTS,
260 + GFP_KERNEL);
261 + if (!parts)
262 + return -ENOMEM;
263 +
264 + err = mtd_read(mtd, 0, sizeof(trx), &bytes_read, (uint8_t *)&trx);
265 + if (err) {
266 + pr_err("MTD reading error: %d\n", err);
267 + kfree(parts);
268 + return err;
269 + }
270 +
271 + if (trx.magic != TRX_MAGIC) {
272 + kfree(parts);
273 + return -ENOENT;
274 + }
275 +
276 + /* We have LZMA loader if there is address in offset[2] */
277 + if (trx.offset[2]) {
278 + part = &parts[curr_part++];
279 + part->name = "loader";
280 + part->offset = trx.offset[i];
281 + i++;
282 + }
283 +
284 + if (trx.offset[i]) {
285 + part = &parts[curr_part++];
286 + part->name = "linux";
287 + part->offset = trx.offset[i];
288 + i++;
289 + }
290 +
291 + if (trx.offset[i]) {
292 + part = &parts[curr_part++];
293 + part->name = parser_trx_data_part_name(mtd, trx.offset[i]);
294 + part->offset = trx.offset[i];
295 + i++;
296 + }
297 +
298 + /*
299 + * Assume that every partition ends at the beginning of the one it is
300 + * followed by.
301 + */
302 + for (i = 0; i < curr_part; i++) {
303 + u64 next_part_offset = (i < curr_part - 1) ?
304 + parts[i + 1].offset : mtd->size;
305 +
306 + parts[i].size = next_part_offset - parts[i].offset;
307 + }
308 +
309 + *pparts = parts;
310 + return i;
311 +};
312 +
313 +static struct mtd_part_parser mtd_parser_trx = {
314 + .parse_fn = parser_trx_parse,
315 + .name = "trx",
316 +};
317 +module_mtd_part_parser(mtd_parser_trx);
318 +
319 +MODULE_LICENSE("GPL v2");
320 +MODULE_DESCRIPTION("Parser for TRX format partitions");