2 * Copyright (C) 2009-2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2009-2013 Gabor Juhos <juhosg@openwrt.org>
4 * Copyright (C) 2012 Jonas Gorski <jogo@openwrt.org>
5 * Copyright (C) 2013 Hauke Mehrtens <hauke@hauke-m.de>
7 * This program is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License version 2 as published
9 * by the Free Software Foundation.
13 #define pr_fmt(fmt) "mtdsplit: " fmt
15 #include <linux/export.h>
16 #include <linux/init.h>
17 #include <linux/kernel.h>
18 #include <linux/magic.h>
19 #include <linux/mtd/mtd.h>
20 #include <linux/mtd/partitions.h>
21 #include <linux/byteorder/generic.h>
25 struct squashfs_super_block
{
31 int mtd_get_squashfs_len(struct mtd_info
*master
,
35 struct squashfs_super_block sb
;
39 err
= mtd_read(master
, offset
, sizeof(sb
), &retlen
, (void *)&sb
);
40 if (err
|| (retlen
!= sizeof(sb
))) {
41 pr_alert("error occured while reading from \"%s\"\n",
46 if (le32_to_cpu(sb
.s_magic
) != SQUASHFS_MAGIC
) {
47 pr_alert("no squashfs found in \"%s\"\n", master
->name
);
51 retlen
= le64_to_cpu(sb
.bytes_used
);
53 pr_alert("squashfs is empty in \"%s\"\n", master
->name
);
57 if (offset
+ retlen
> master
->size
) {
58 pr_alert("squashfs has invalid size in \"%s\"\n",
63 *squashfs_len
= retlen
;
66 EXPORT_SYMBOL_GPL(mtd_get_squashfs_len
);
68 static ssize_t
mtd_next_eb(struct mtd_info
*mtd
, size_t offset
)
70 return mtd_rounddown_to_eb(offset
, mtd
) + mtd
->erasesize
;
73 int mtd_check_rootfs_magic(struct mtd_info
*mtd
, size_t offset
,
74 enum mtdsplit_part_type
*type
)
80 ret
= mtd_read(mtd
, offset
, sizeof(magic
), &retlen
,
81 (unsigned char *) &magic
);
85 if (retlen
!= sizeof(magic
))
88 if (le32_to_cpu(magic
) == SQUASHFS_MAGIC
) {
90 *type
= MTDSPLIT_PART_TYPE_SQUASHFS
;
92 } else if (magic
== 0x19852003) {
94 *type
= MTDSPLIT_PART_TYPE_JFFS2
;
100 EXPORT_SYMBOL_GPL(mtd_check_rootfs_magic
);
102 int mtd_find_rootfs_from(struct mtd_info
*mtd
,
106 enum mtdsplit_part_type
*type
)
111 for (offset
= from
; offset
< limit
;
112 offset
= mtd_next_eb(mtd
, offset
)) {
113 err
= mtd_check_rootfs_magic(mtd
, offset
, type
);
117 *ret_offset
= offset
;
123 EXPORT_SYMBOL_GPL(mtd_find_rootfs_from
);