1/*
2 *  Copyright (C) 2013 Felix Fietkau <nbd@nbd.name>
3 *  Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
4 *
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.
8 *
9 */
10
11#define pr_fmt(fmt)	KBUILD_MODNAME ": " fmt
12
13#include <linux/module.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/slab.h>
17#include <linux/magic.h>
18#include <linux/mtd/mtd.h>
19#include <linux/mtd/partitions.h>
20#include <linux/byteorder/generic.h>
21
22#include "mtdsplit.h"
23
24static int
25mtdsplit_parse_squashfs(struct mtd_info *master,
26			struct mtd_partition **pparts,
27			struct mtd_part_parser_data *data)
28{
29	struct mtd_partition *part;
30	struct mtd_info *parent_mtd;
31	size_t part_offset;
32	size_t squashfs_len;
33	int err;
34
35	err = mtd_get_squashfs_len(master, 0, &squashfs_len);
36	if (err)
37		return err;
38
39	parent_mtd = mtdpart_get_master(master);
40	part_offset = mtdpart_get_offset(master);
41
42	part = kzalloc(sizeof(*part), GFP_KERNEL);
43	if (!part) {
44		pr_alert("unable to allocate memory for \"%s\" partition\n",
45			 ROOTFS_SPLIT_NAME);
46		return -ENOMEM;
47	}
48
49	part->name = ROOTFS_SPLIT_NAME;
50	part->offset = mtd_roundup_to_eb(part_offset + squashfs_len,
51					 parent_mtd) - part_offset;
52	part->size = mtd_rounddown_to_eb(master->size - part->offset, master);
53
54	*pparts = part;
55	return 1;
56}
57
58static struct mtd_part_parser mtdsplit_squashfs_parser = {
59	.owner = THIS_MODULE,
60	.name = "squashfs-split",
61	.parse_fn = mtdsplit_parse_squashfs,
62	.type = MTD_PARSER_TYPE_ROOTFS,
63};
64
65static int __init mtdsplit_squashfs_init(void)
66{
67	register_mtd_parser(&mtdsplit_squashfs_parser);
68
69	return 0;
70}
71
72subsys_initcall(mtdsplit_squashfs_init);
73