2 * Copyright (C) 2012 John Crispin <blogic@openwrt.org>
3 * Copyright (C) 2015 Martin Blumenstingl <martin.blumenstingl@googlemail.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 as published
7 * by the Free Software Foundation.
11 #include <linux/module.h>
12 #include <linux/init.h>
13 #include <linux/kernel.h>
14 #include <linux/slab.h>
15 #include <linux/mtd/mtd.h>
16 #include <linux/mtd/partitions.h>
17 #include <linux/byteorder/generic.h>
21 #define BRNIMAGE_NR_PARTS 2
23 #define BRNIMAGE_ALIGN_BYTES 0x400
24 #define BRNIMAGE_FOOTER_SIZE 12
26 #define BRNIMAGE_MIN_OVERHEAD (BRNIMAGE_FOOTER_SIZE)
27 #define BRNIMAGE_MAX_OVERHEAD (BRNIMAGE_ALIGN_BYTES + BRNIMAGE_FOOTER_SIZE)
29 static int mtdsplit_parse_brnimage(struct mtd_info
*master
,
30 struct mtd_partition
**pparts
,
31 struct mtd_part_parser_data
*data
)
33 struct mtd_partition
*parts
;
35 unsigned long rootfs_offset
, rootfs_size
, kernel_size
;
39 for (rootfs_offset
= 0; rootfs_offset
< master
->size
;
40 rootfs_offset
+= BRNIMAGE_ALIGN_BYTES
) {
41 ret
= mtd_check_rootfs_magic(master
, rootfs_offset
, NULL
);
49 if (rootfs_offset
>= master
->size
)
52 ret
= mtd_read(master
, rootfs_offset
- BRNIMAGE_FOOTER_SIZE
, 4, &len
,
60 kernel_size
= le32_to_cpu(buf
);
62 if (kernel_size
> (rootfs_offset
- BRNIMAGE_MIN_OVERHEAD
))
65 if (kernel_size
< (rootfs_offset
- BRNIMAGE_MAX_OVERHEAD
))
69 * The footer must be untouched as it contains the checksum of the
70 * original brnImage (kernel + squashfs)!
72 rootfs_size
= master
->size
- rootfs_offset
- BRNIMAGE_FOOTER_SIZE
;
74 parts
= kzalloc(BRNIMAGE_NR_PARTS
* sizeof(*parts
), GFP_KERNEL
);
78 parts
[0].name
= KERNEL_PART_NAME
;
80 parts
[0].size
= kernel_size
;
82 parts
[1].name
= ROOTFS_PART_NAME
;
83 parts
[1].offset
= rootfs_offset
;
84 parts
[1].size
= rootfs_size
;
87 return BRNIMAGE_NR_PARTS
;
90 static struct mtd_part_parser mtdsplit_brnimage_parser
= {
92 .name
= "brnimage-fw",
93 .parse_fn
= mtdsplit_parse_brnimage
,
94 .type
= MTD_PARSER_TYPE_FIRMWARE
,
97 static int __init
mtdsplit_brnimage_init(void)
99 register_mtd_parser(&mtdsplit_brnimage_parser
);
104 subsys_initcall(mtdsplit_brnimage_init
);