kernel: update mtdsplit for linux 4.9
[librecmc/librecmc.git] / target / linux / generic / files / drivers / mtd / mtdsplit / mtdsplit_wrgg.c
1 /*
2  *  Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
3  *  Copyright (C) 2014 Felix Fietkau <nbd@nbd.name>
4  *  Copyright (C) 2016 Stijn Tintel <stijn@linux-ipv6.be>
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 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/mtd/mtd.h>
17 #include <linux/mtd/partitions.h>
18 #include <linux/byteorder/generic.h>
19
20 #include "mtdsplit.h"
21
22 #define WRGG_NR_PARTS           2
23 #define WRGG_MIN_ROOTFS_OFFS    0x80000 /* 512KiB */
24 #define WRGG03_MAGIC            0x20080321
25
26 struct wrgg03_header {
27         char            signature[32];
28         uint32_t        magic1;
29         uint32_t        magic2;
30         char            version[16];
31         char            model[16];
32         uint32_t        flag[2];
33         uint32_t        reserve[2];
34         char            buildno[16];
35         uint32_t        size;
36         uint32_t        offset;
37         char            devname[32];
38         char            digest[16];
39 } __attribute__ ((packed));
40
41
42 static int mtdsplit_parse_wrgg(struct mtd_info *master,
43                                const struct mtd_partition **pparts,
44                                struct mtd_part_parser_data *data)
45 {
46         struct wrgg03_header hdr;
47         size_t hdr_len, retlen, kernel_ent_size;
48         size_t rootfs_offset;
49         struct mtd_partition *parts;
50         enum mtdsplit_part_type type;
51         int err;
52
53         hdr_len = sizeof(hdr);
54         err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
55         if (err)
56                 return err;
57
58         if (retlen != hdr_len)
59                 return -EIO;
60
61         /* sanity checks */
62         if (le32_to_cpu(hdr.magic1) != WRGG03_MAGIC)
63                 return -EINVAL;
64
65         kernel_ent_size = hdr_len + be32_to_cpu(hdr.size);
66
67         if (kernel_ent_size > master->size)
68                 return -EINVAL;
69
70         /*
71          * The size in the header covers the rootfs as well.
72          * Start the search from an arbitrary offset.
73          */
74         err = mtd_find_rootfs_from(master, WRGG_MIN_ROOTFS_OFFS,
75                                    master->size, &rootfs_offset, &type);
76         if (err)
77                 return err;
78
79         parts = kzalloc(WRGG_NR_PARTS * sizeof(*parts), GFP_KERNEL);
80         if (!parts)
81                 return -ENOMEM;
82
83         parts[0].name = KERNEL_PART_NAME;
84         parts[0].offset = 0;
85         parts[0].size = rootfs_offset;
86
87         parts[1].name = ROOTFS_PART_NAME;
88         parts[1].offset = rootfs_offset;
89         parts[1].size = master->size - rootfs_offset;
90
91         *pparts = parts;
92         return WRGG_NR_PARTS;
93 }
94
95 static struct mtd_part_parser mtdsplit_wrgg_parser = {
96         .owner = THIS_MODULE,
97         .name = "wrgg-fw",
98         .parse_fn = mtdsplit_parse_wrgg,
99         .type = MTD_PARSER_TYPE_FIRMWARE,
100 };
101
102 static int __init mtdsplit_wrgg_init(void)
103 {
104         register_mtd_parser(&mtdsplit_wrgg_parser);
105
106         return 0;
107 }
108
109 subsys_initcall(mtdsplit_wrgg_init);