kernel: mtdsplit: wrgg: Support big and little endian
[openwrt/staging/nbd.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 #define WRG_MAGIC 0x20040220
26
27 struct wrgg03_header {
28 char signature[32];
29 uint32_t magic1;
30 uint32_t magic2;
31 char version[16];
32 char model[16];
33 uint32_t flag[2];
34 uint32_t reserve[2];
35 char buildno[16];
36 uint32_t size;
37 uint32_t offset;
38 char devname[32];
39 char digest[16];
40 } __attribute__ ((packed));
41
42 struct wrg_header {
43 char signature[32];
44 uint32_t magic1;
45 uint32_t magic2;
46 uint32_t size;
47 uint32_t offset;
48 char devname[32];
49 char digest[16];
50 } __attribute__ ((packed));
51
52
53 static int mtdsplit_parse_wrgg(struct mtd_info *master,
54 const struct mtd_partition **pparts,
55 struct mtd_part_parser_data *data)
56 {
57 struct wrgg03_header hdr;
58 size_t hdr_len, retlen, kernel_ent_size;
59 size_t rootfs_offset;
60 struct mtd_partition *parts;
61 enum mtdsplit_part_type type;
62 int err;
63
64 hdr_len = sizeof(hdr);
65 err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
66 if (err)
67 return err;
68
69 if (retlen != hdr_len)
70 return -EIO;
71
72 /* sanity checks */
73 if (le32_to_cpu(hdr.magic1) == WRGG03_MAGIC) {
74 kernel_ent_size = hdr_len + be32_to_cpu(hdr.size);
75 /*
76 * If this becomes silly big it's probably because the
77 * WRGG image is little-endian.
78 */
79 if (kernel_ent_size > master->size)
80 kernel_ent_size = hdr_len + le32_to_cpu(hdr.size);
81
82 /* Now what ?! It's neither */
83 if (kernel_ent_size > master->size)
84 return -EINVAL;
85 } else if (le32_to_cpu(hdr.magic1) == WRG_MAGIC) {
86 kernel_ent_size = sizeof(struct wrg_header) + le32_to_cpu(
87 ((struct wrg_header*)&hdr)->size);
88 } else {
89 return -EINVAL;
90 }
91
92 if (kernel_ent_size > master->size)
93 return -EINVAL;
94
95 /*
96 * The size in the header covers the rootfs as well.
97 * Start the search from an arbitrary offset.
98 */
99 err = mtd_find_rootfs_from(master, WRGG_MIN_ROOTFS_OFFS,
100 master->size, &rootfs_offset, &type);
101 if (err)
102 return err;
103
104 parts = kzalloc(WRGG_NR_PARTS * sizeof(*parts), GFP_KERNEL);
105 if (!parts)
106 return -ENOMEM;
107
108 parts[0].name = KERNEL_PART_NAME;
109 parts[0].offset = 0;
110 parts[0].size = rootfs_offset;
111
112 parts[1].name = ROOTFS_PART_NAME;
113 parts[1].offset = rootfs_offset;
114 parts[1].size = master->size - rootfs_offset;
115
116 *pparts = parts;
117 return WRGG_NR_PARTS;
118 }
119
120 static const struct of_device_id mtdsplit_wrgg_of_match_table[] = {
121 { .compatible = "wrg" },
122 {},
123 };
124 MODULE_DEVICE_TABLE(of, mtdsplit_wrgg_of_match_table);
125
126 static struct mtd_part_parser mtdsplit_wrgg_parser = {
127 .owner = THIS_MODULE,
128 .name = "wrgg-fw",
129 .of_match_table = mtdsplit_wrgg_of_match_table,
130 .parse_fn = mtdsplit_parse_wrgg,
131 .type = MTD_PARSER_TYPE_FIRMWARE,
132 };
133
134 static int __init mtdsplit_wrgg_init(void)
135 {
136 register_mtd_parser(&mtdsplit_wrgg_parser);
137
138 return 0;
139 }
140
141 subsys_initcall(mtdsplit_wrgg_init);