4e46ded587ff8f40e2cce6c8abafe5b552010c1c
[openwrt/openwrt.git] / target / linux / generic / files / drivers / mtd / mtdsplit / mtdsplit_bcm_wfi.c
1 /*
2 * MTD split for Broadcom Whole Flash Image
3 *
4 * Copyright (C) 2020 Álvaro Fernández Rojas <noltari@gmail.com>
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License version 2 as published
8 * by the Free Software Foundation.
9 *
10 */
11
12 #define je16_to_cpu(x) ((x).v16)
13 #define je32_to_cpu(x) ((x).v32)
14
15 #include <linux/crc32.h>
16 #include <linux/init.h>
17 #include <linux/jffs2.h>
18 #include <linux/kernel.h>
19 #include <linux/module.h>
20 #include <linux/slab.h>
21 #include <linux/byteorder/generic.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/mtd/partitions.h>
24
25 #include "mtdsplit.h"
26
27 #define char_to_num(c) ((c >= '0' && c <= '9') ? (c - '0') : (0))
28
29 #define BCM_WFI_PARTS 3
30
31 #define CFERAM_NAME "cferam"
32 #define CFERAM_NAME_LEN (sizeof(CFERAM_NAME) - 1)
33 #define KERNEL_NAME "vmlinux.lz"
34 #define KERNEL_NAME_LEN (sizeof(KERNEL_NAME) - 1)
35 #define OPENWRT_NAME "1-openwrt"
36 #define OPENWRT_NAME_LEN (sizeof(OPENWRT_NAME) - 1)
37
38 #define UBI_MAGIC 0x55424923
39
40 #define SERCOMM_MAGIC_PFX "eRcOmM."
41 #define SERCOMM_MAGIC_PFX_LEN (sizeof(SERCOMM_MAGIC_PFX) - 1)
42 #define SERCOMM_MAGIC "eRcOmM.000"
43 #define SERCOMM_MAGIC_LEN (sizeof(SERCOMM_MAGIC) - 1)
44
45 static u32 jffs2_dirent_crc(struct jffs2_raw_dirent *node)
46 {
47 return crc32(0, node, sizeof(struct jffs2_raw_dirent) - 8);
48 }
49
50 static bool jffs2_dirent_valid(struct jffs2_raw_dirent *node)
51 {
52 return ((je16_to_cpu(node->magic) == JFFS2_MAGIC_BITMASK) &&
53 (je16_to_cpu(node->nodetype) == JFFS2_NODETYPE_DIRENT) &&
54 je32_to_cpu(node->ino) &&
55 je32_to_cpu(node->node_crc) == jffs2_dirent_crc(node));
56 }
57
58 static int jffs2_find_file(struct mtd_info *mtd, uint8_t *buf,
59 const char *name, size_t name_len,
60 loff_t *offs, loff_t size)
61 {
62 const loff_t end = *offs + size;
63 struct jffs2_raw_dirent *node;
64 bool valid = false;
65 size_t retlen;
66 uint16_t magic;
67 int rc;
68
69 for (; *offs < end; *offs += mtd->erasesize) {
70 unsigned int block_offs = 0;
71
72 /* Skip CFE erased blocks */
73 rc = mtd_read(mtd, *offs, sizeof(magic), &retlen,
74 (void *) &magic);
75 if (rc || retlen != sizeof(magic)) {
76 continue;
77 }
78
79 /* Skip blocks not starting with JFFS2 magic */
80 if (magic != JFFS2_MAGIC_BITMASK)
81 continue;
82
83 /* Read full block */
84 rc = mtd_read(mtd, *offs, mtd->erasesize, &retlen,
85 (void *) buf);
86 if (rc)
87 return rc;
88 if (retlen != mtd->erasesize)
89 return -EINVAL;
90
91 while (block_offs < mtd->erasesize) {
92 node = (struct jffs2_raw_dirent *) &buf[block_offs];
93
94 if (!jffs2_dirent_valid(node)) {
95 block_offs += 4;
96 continue;
97 }
98
99 if (!memcmp(node->name, OPENWRT_NAME,
100 OPENWRT_NAME_LEN))
101 valid = true;
102 else if (!memcmp(node->name, name, name_len))
103 return valid ? 0 : -EINVAL;
104
105 block_offs += je32_to_cpu(node->totlen);
106 block_offs = (block_offs + 0x3) & ~0x3;
107 }
108 }
109
110 return -ENOENT;
111 }
112
113 static int ubifs_find(struct mtd_info *mtd, loff_t *offs, loff_t size)
114 {
115 const loff_t end = *offs + size;
116 uint32_t magic;
117 size_t retlen;
118 int rc;
119
120 for (; *offs < end; *offs += mtd->erasesize) {
121 rc = mtd_read(mtd, *offs, sizeof(magic), &retlen,
122 (unsigned char *) &magic);
123 if (rc || retlen != sizeof(magic))
124 continue;
125
126 if (be32_to_cpu(magic) == UBI_MAGIC)
127 return 0;
128 }
129
130 return -ENOENT;
131 }
132
133 static int mtdsplit_parse_bcm_wfi(struct mtd_info *master,
134 const struct mtd_partition **pparts,
135 struct mtd_part_parser_data *data)
136 {
137 struct mtd_partition *parts;
138 struct device_node *mtd_node;
139 loff_t cfe_off, kernel_off, rootfs_off;
140 bool cfe_part = true;
141 unsigned int num_parts = BCM_WFI_PARTS, cur_part = 0;
142 uint8_t *buf;
143 int ret;
144
145 buf = kzalloc(master->erasesize, GFP_KERNEL);
146 if (!buf)
147 return -ENOMEM;
148
149 mtd_node = mtd_get_of_node(master);
150 if (!mtd_node)
151 return -EINVAL;
152
153 if (of_property_read_bool(mtd_node, "brcm,no-cferam"))
154 cfe_part = false;
155
156 if (cfe_part) {
157 num_parts++;
158 cfe_off = 0;
159
160 ret = jffs2_find_file(master, buf, CFERAM_NAME,
161 CFERAM_NAME_LEN, &cfe_off,
162 master->size);
163 if (ret) {
164 kfree(buf);
165 return ret;
166 }
167
168 kernel_off = cfe_off + master->erasesize;
169 } else {
170 kernel_off = 0;
171 }
172
173 ret = jffs2_find_file(master, buf, KERNEL_NAME, KERNEL_NAME_LEN,
174 &kernel_off, master->size);
175 kfree(buf);
176 if (ret)
177 return ret;
178
179 rootfs_off = kernel_off + master->erasesize;
180 ret = ubifs_find(master, &rootfs_off, master->size);
181 if (ret)
182 return ret;
183
184 parts = kzalloc(num_parts * sizeof(*parts), GFP_KERNEL);
185 if (!parts)
186 return -ENOMEM;
187
188 if (cfe_part) {
189 parts[cur_part].name = "cferam";
190 parts[cur_part].mask_flags = MTD_WRITEABLE;
191 parts[cur_part].offset = 0;
192 parts[cur_part].size = kernel_off;
193 cur_part++;
194 }
195
196 parts[cur_part].name = "firmware";
197 parts[cur_part].offset = kernel_off;
198 parts[cur_part].size = master->size - kernel_off;
199 cur_part++;
200
201 parts[cur_part].name = KERNEL_PART_NAME;
202 parts[cur_part].offset = kernel_off;
203 parts[cur_part].size = rootfs_off - kernel_off;
204 cur_part++;
205
206 parts[cur_part].name = UBI_PART_NAME;
207 parts[cur_part].offset = rootfs_off;
208 parts[cur_part].size = master->size - rootfs_off;
209 cur_part++;
210
211 *pparts = parts;
212
213 return num_parts;
214 }
215
216 static const struct of_device_id mtdsplit_bcm_wfi_of_match[] = {
217 { .compatible = "brcm,wfi" },
218 { },
219 };
220
221 static struct mtd_part_parser mtdsplit_bcm_wfi_parser = {
222 .owner = THIS_MODULE,
223 .name = "bcm-wfi-fw",
224 .of_match_table = mtdsplit_bcm_wfi_of_match,
225 .parse_fn = mtdsplit_parse_bcm_wfi,
226 .type = MTD_PARSER_TYPE_FIRMWARE,
227 };
228
229 static int sercomm_bootflag_value(struct mtd_info *mtd, uint8_t *buf)
230 {
231 size_t retlen;
232 loff_t offs;
233 int rc;
234
235 for (offs = 0; offs < mtd->size; offs += mtd->erasesize) {
236 rc = mtd_read(mtd, offs, SERCOMM_MAGIC_LEN, &retlen, buf);
237 if (rc || retlen != SERCOMM_MAGIC_LEN)
238 continue;
239
240 if (memcmp(buf, SERCOMM_MAGIC_PFX, SERCOMM_MAGIC_PFX_LEN))
241 continue;
242
243 rc = char_to_num(buf[SERCOMM_MAGIC_PFX_LEN + 0]) * 100;
244 rc += char_to_num(buf[SERCOMM_MAGIC_PFX_LEN + 1]) * 10;
245 rc += char_to_num(buf[SERCOMM_MAGIC_PFX_LEN + 2]) * 1;
246
247 return rc;
248 }
249
250 return -ENOENT;
251 }
252
253 static int mtdsplit_parse_ser_wfi(struct mtd_info *master,
254 const struct mtd_partition **pparts,
255 struct mtd_part_parser_data *data)
256 {
257 struct mtd_info *mtd_bf1, *mtd_bf2;
258 struct erase_info bf_erase;
259 struct mtd_partition *parts;
260 loff_t kernel_off, rootfs_off, img_size;
261 loff_t img2_off, img2_size = 0;
262 unsigned int num_parts = BCM_WFI_PARTS, cur_part = 0;
263 uint8_t *buf;
264 int bf1, bf2;
265 int ret;
266
267 mtd_bf1 = get_mtd_device_nm("bootflag1");
268 if (IS_ERR(mtd_bf1))
269 return -ENOENT;
270
271 mtd_bf2 = get_mtd_device_nm("bootflag2");
272 if (IS_ERR(mtd_bf2))
273 return -ENOENT;
274
275 buf = kzalloc(master->erasesize, GFP_KERNEL);
276 if (!buf)
277 return -ENOMEM;
278
279 bf1 = sercomm_bootflag_value(mtd_bf1, buf);
280 if (bf1 >= 0)
281 printk("sercomm: bootflag1=%d\n", bf1);
282
283 bf2 = sercomm_bootflag_value(mtd_bf2, buf);
284 if (bf2 >= 0)
285 printk("sercomm: bootflag2=%d\n", bf2);
286
287 if (bf1 == bf2 && bf2 >= 0) {
288 bf2 = -ENOENT;
289 bf_erase.addr = 0;
290 bf_erase.len = mtd_bf2->size;
291 mtd_erase(mtd_bf2, &bf_erase);
292 }
293
294 if (bf1 >= bf2) {
295 kernel_off = 0;
296 if (bf2 >= 0) {
297 img_size = master->size / 2;
298
299 img2_off = img_size;
300 img2_size = master->size - img2_off;
301 num_parts++;
302 } else {
303 img_size = master->size;
304 }
305 } else {
306 kernel_off = master->size / 2;
307 img_size = master->size;
308
309 img2_off = 0;
310 img2_size = kernel_off;
311 num_parts++;
312 }
313
314 ret = jffs2_find_file(master, buf, KERNEL_NAME, KERNEL_NAME_LEN,
315 &kernel_off, img_size);
316 kfree(buf);
317 if (ret)
318 return ret;
319
320 rootfs_off = kernel_off + master->erasesize;
321 ret = ubifs_find(master, &rootfs_off, img_size);
322 if (ret)
323 return ret;
324
325 parts = kzalloc(num_parts * sizeof(*parts), GFP_KERNEL);
326 if (!parts)
327 return -ENOMEM;
328
329 parts[cur_part].name = "firmware";
330 parts[cur_part].offset = kernel_off;
331 parts[cur_part].size = img_size - kernel_off;
332 cur_part++;
333
334 parts[cur_part].name = KERNEL_PART_NAME;
335 parts[cur_part].offset = kernel_off;
336 parts[cur_part].size = rootfs_off - kernel_off;
337 cur_part++;
338
339 parts[cur_part].name = UBI_PART_NAME;
340 parts[cur_part].offset = rootfs_off;
341 parts[cur_part].size = img_size - rootfs_off;
342 cur_part++;
343
344 if (img2_size) {
345 parts[cur_part].name = "img2";
346 parts[cur_part].offset = img2_off;
347 parts[cur_part].size = img2_size;
348 cur_part++;
349 }
350
351 *pparts = parts;
352
353 return num_parts;
354 }
355
356 static const struct of_device_id mtdsplit_ser_wfi_of_match[] = {
357 { .compatible = "sercomm,wfi" },
358 { },
359 };
360
361 static struct mtd_part_parser mtdsplit_ser_wfi_parser = {
362 .owner = THIS_MODULE,
363 .name = "ser-wfi-fw",
364 .of_match_table = mtdsplit_ser_wfi_of_match,
365 .parse_fn = mtdsplit_parse_ser_wfi,
366 .type = MTD_PARSER_TYPE_FIRMWARE,
367 };
368
369 static int __init mtdsplit_bcm_wfi_init(void)
370 {
371 register_mtd_parser(&mtdsplit_bcm_wfi_parser);
372 register_mtd_parser(&mtdsplit_ser_wfi_parser);
373
374 return 0;
375 }
376
377 module_init(mtdsplit_bcm_wfi_init);