aboutsummaryrefslogtreecommitdiffstats
path: root/target/linux/generic/files/drivers/mtd/mtdsplit/mtdsplit_wrgg.c
blob: 6c137c52ec0e2164d2c2551b11d1212a8588ee65 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
/*
 *  Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
 *  Copyright (C) 2014 Felix Fietkau <nbd@nbd.name>
 *  Copyright (C) 2016 Stijn Tintel <stijn@linux-ipv6.be>
 *
 *  This program is free software; you can redistribute it and/or modify it
 *  under the terms of the GNU General Public License version 2 as published
 *  by the Free Software Foundation.
 *
 */

#include <linux/module.h>
#include <linux/init.h>
#include <linux/kernel.h>
#include <linux/slab.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
#include <linux/byteorder/generic.h>

#include "mtdsplit.h"

#define WRGG_NR_PARTS		2
#define WRGG_MIN_ROOTFS_OFFS	0x80000	/* 512KiB */
#define WRGG03_MAGIC		0x20080321
#define WRG_MAGIC		0x20040220

struct wrgg03_header {
	char		signature[32];
	uint32_t	magic1;
	uint32_t	magic2;
	char		version[16];
	char		model[16];
	uint32_t	flag[2];
	uint32_t	reserve[2];
	char		buildno[16];
	uint32_t	size;
	uint32_t	offset;
	char		devname[32];
	char		digest[16];
} __attribute__ ((packed));

struct wrg_header {
	char		signature[32];
	uint32_t	magic1;
	uint32_t	magic2;
	uint32_t	size;
	uint32_t	offset;
	char		devname[32];
	char		digest[16];
} __attribute__ ((packed));


static int mtdsplit_parse_wrgg(struct mtd_info *master,
			       const struct mtd_partition **pparts,
			       struct mtd_part_parser_data *data)
{
	struct wrgg03_header hdr;
	size_t hdr_len, retlen, kernel_ent_size;
	size_t rootfs_offset;
	struct mtd_partition *parts;
	enum mtdsplit_part_type type;
	int err;

	hdr_len = sizeof(hdr);
	err = mtd_read(master, 0, hdr_len, &retlen, (void *) &hdr);
	if (err)
		return err;

	if (retlen != hdr_len)
		return -EIO;

	/* sanity checks */
	if (le32_to_cpu(hdr.magic1) == WRGG03_MAGIC) {
		kernel_ent_size = hdr_len + be32_to_cpu(hdr.size);
		/*
		 * If this becomes silly big it's probably because the
		 * WRGG image is little-endian.
		 */
		if (kernel_ent_size > master->size)
			kernel_ent_size = hdr_len + le32_to_cpu(hdr.size);

		/* Now what ?! It's neither */
		if (kernel_ent_size > master->size)
			return -EINVAL;
	} else if (le32_to_cpu(hdr.magic1) == WRG_MAGIC) {
		kernel_ent_size = sizeof(struct wrg_header) + le32_to_cpu(
		                  ((struct wrg_header*)&hdr)->size);
	} else {
		return -EINVAL;
	}

	if (kernel_ent_size > master->size)
		return -EINVAL;

	/*
	 * The size in the header covers the rootfs as well.
	 * Start the search from an arbitrary offset.
	 */
	err = mtd_find_rootfs_from(master, WRGG_MIN_ROOTFS_OFFS,
				   master->size, &rootfs_offset, &type);
	if (err)
		return err;

	parts = kzalloc(WRGG_NR_PARTS * sizeof(*parts), GFP_KERNEL);
	if (!parts)
		return -ENOMEM;

	parts[0].name = KERNEL_PART_NAME;
	parts[0].offset = 0;
	parts[0].size = rootfs_offset;

	parts[1].name = ROOTFS_PART_NAME;
	parts[1].offset = rootfs_offset;
	parts[1].size = master->size - rootfs_offset;

	*pparts = parts;
	return WRGG_NR_PARTS;
}

static const struct of_device_id mtdsplit_wrgg_of_match_table[] = {
	{ .compatible = "wrg" },
	{},
};
MODULE_DEVICE_TABLE(of, mtdsplit_wrgg_of_match_table);

static struct mtd_part_parser mtdsplit_wrgg_parser = {
	.owner = THIS_MODULE,
	.name = "wrgg-fw",
	.of_match_table = mtdsplit_wrgg_of_match_table,
	.parse_fn = mtdsplit_parse_wrgg,
	.type = MTD_PARSER_TYPE_FIRMWARE,
};

static int __init mtdsplit_wrgg_init(void)
{
	register_mtd_parser(&mtdsplit_wrgg_parser);

	return 0;
}

subsys_initcall(mtdsplit_wrgg_init);