1 // SPDX-License-Identifier: GPL-2.0-only
3 * Copyright (C) 2021 Rafał Miłecki <rafal@milecki.pl>
6 #include <linux/init.h>
7 #include <linux/jffs2.h>
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/mtd/mtd.h>
11 #include <linux/mtd/partitions.h>
13 #include <linux/slab.h>
17 #define je16_to_cpu(x) ((x).v16)
18 #define je32_to_cpu(x) ((x).v32)
22 static int mtdsplit_cfe_bootfs_parse(struct mtd_info
*mtd
,
23 const struct mtd_partition
**pparts
,
24 struct mtd_part_parser_data
*data
)
26 struct jffs2_raw_dirent node
;
27 enum mtdsplit_part_type type
;
28 struct mtd_partition
*parts
;
34 /* Don't parse backup partitions */
35 if (strcmp(mtd
->name
, "firmware"))
38 /* Find the end of JFFS2 bootfs partition */
41 err
= mtd_read(mtd
, offset
, sizeof(node
), &retlen
, (void *)&node
);
42 if (err
|| retlen
!= sizeof(node
))
45 if (je16_to_cpu(node
.magic
) != JFFS2_MAGIC_BITMASK
)
48 offset
+= je32_to_cpu(node
.totlen
);
49 offset
= (offset
+ 0x3) & ~0x3;
50 } while (offset
< mtd
->size
);
52 /* Find rootfs partition that follows the bootfs */
53 err
= mtd_find_rootfs_from(mtd
, mtd
->erasesize
, mtd
->size
, &rootfs_offset
, &type
);
57 parts
= kzalloc(NR_PARTS
* sizeof(*parts
), GFP_KERNEL
);
61 if (type
== MTDSPLIT_PART_TYPE_UBI
)
62 parts
[0].name
= UBI_PART_NAME
;
64 parts
[0].name
= ROOTFS_PART_NAME
;
65 parts
[0].offset
= rootfs_offset
;
66 parts
[0].size
= mtd
->size
- rootfs_offset
;
73 static const struct of_device_id mtdsplit_cfe_bootfs_of_match_table
[] = {
74 { .compatible
= "brcm,bcm4908-firmware" },
77 MODULE_DEVICE_TABLE(of
, mtdsplit_cfe_bootfs_of_match_table
);
79 static struct mtd_part_parser mtdsplit_cfe_bootfs_parser
= {
82 .of_match_table
= mtdsplit_cfe_bootfs_of_match_table
,
83 .parse_fn
= mtdsplit_cfe_bootfs_parse
,
86 module_mtd_part_parser(mtdsplit_cfe_bootfs_parser
);