kernel: mtdsplit_uimage: replace "edimax, uimage" parser
[openwrt/openwrt.git] / target / linux / generic / files / drivers / mtd / mtdsplit / mtdsplit_uimage.c
1 /*
2 * Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 as published
6 * by the Free Software Foundation.
7 *
8 */
9
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
11
12 #include <linux/module.h>
13 #include <linux/init.h>
14 #include <linux/kernel.h>
15 #include <linux/slab.h>
16 #include <linux/vmalloc.h>
17 #include <linux/mtd/mtd.h>
18 #include <linux/mtd/partitions.h>
19 #include <linux/version.h>
20 #include <linux/byteorder/generic.h>
21 #include <linux/of.h>
22 #include <dt-bindings/mtd/partitions/uimage.h>
23
24 #include "mtdsplit.h"
25
26 /*
27 * Legacy format image header,
28 * all data in network byte order (aka natural aka bigendian).
29 */
30 struct uimage_header {
31 uint32_t ih_magic; /* Image Header Magic Number */
32 uint32_t ih_hcrc; /* Image Header CRC Checksum */
33 uint32_t ih_time; /* Image Creation Timestamp */
34 uint32_t ih_size; /* Image Data Size */
35 uint32_t ih_load; /* Data Load Address */
36 uint32_t ih_ep; /* Entry Point Address */
37 uint32_t ih_dcrc; /* Image Data CRC Checksum */
38 uint8_t ih_os; /* Operating System */
39 uint8_t ih_arch; /* CPU architecture */
40 uint8_t ih_type; /* Image Type */
41 uint8_t ih_comp; /* Compression Type */
42 uint8_t ih_name[IH_NMLEN]; /* Image Name */
43 };
44
45 static int
46 read_uimage_header(struct mtd_info *mtd, size_t offset, u_char *buf,
47 size_t header_len)
48 {
49 size_t retlen;
50 int ret;
51
52 ret = mtd_read(mtd, offset, header_len, &retlen, buf);
53 if (ret) {
54 pr_debug("read error in \"%s\"\n", mtd->name);
55 return ret;
56 }
57
58 if (retlen != header_len) {
59 pr_debug("short read in \"%s\"\n", mtd->name);
60 return -EIO;
61 }
62
63 return 0;
64 }
65
66 static void uimage_parse_dt(struct mtd_info *master, int *extralen,
67 u32 *ih_magic, u32 *ih_type,
68 u32 *header_offset, u32 *part_magic)
69 {
70 struct device_node *np = mtd_get_of_node(master);
71
72 if (!np || !of_device_is_compatible(np, "openwrt,uimage"))
73 return;
74
75 if (!of_property_read_u32(np, "openwrt,padding", extralen))
76 pr_debug("got openwrt,padding=%d from device-tree\n", *extralen);
77 if (!of_property_read_u32(np, "openwrt,ih-magic", ih_magic))
78 pr_debug("got openwrt,ih-magic=%08x from device-tree\n", *ih_magic);
79 if (!of_property_read_u32(np, "openwrt,ih-type", ih_type))
80 pr_debug("got openwrt,ih-type=%08x from device-tree\n", *ih_type);
81 if (!of_property_read_u32(np, "openwrt,offset", header_offset))
82 pr_debug("got ih-start=%u from device-tree\n", *header_offset);
83 if (!of_property_read_u32(np, "openwrt,partition-magic", part_magic))
84 pr_debug("got openwrt,partition-magic=%08x from device-tree\n", *part_magic);
85 }
86
87 static ssize_t uimage_verify_default(u_char *buf, u32 ih_magic, u32 ih_type)
88 {
89 struct uimage_header *header = (struct uimage_header *)buf;
90
91 /* default sanity checks */
92 if (be32_to_cpu(header->ih_magic) != ih_magic) {
93 pr_debug("invalid uImage magic: %08x != %08x\n",
94 be32_to_cpu(header->ih_magic), ih_magic);
95 return -EINVAL;
96 }
97
98 if (header->ih_os != IH_OS_LINUX) {
99 pr_debug("invalid uImage OS: %08x != %08x\n",
100 be32_to_cpu(header->ih_os), IH_OS_LINUX);
101 return -EINVAL;
102 }
103
104 if (header->ih_type != ih_type) {
105 pr_debug("invalid uImage type: %08x != %08x\n",
106 be32_to_cpu(header->ih_type), ih_type);
107 return -EINVAL;
108 }
109
110 return 0;
111 }
112
113 /**
114 * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
115 *
116 * @find_header: function to call for a block of data that will return offset
117 * and tail padding length of a valid uImage header if found
118 */
119 static int __mtdsplit_parse_uimage(struct mtd_info *master,
120 const struct mtd_partition **pparts,
121 struct mtd_part_parser_data *data)
122 {
123 struct mtd_partition *parts;
124 u_char *buf;
125 int nr_parts;
126 size_t offset;
127 size_t uimage_offset;
128 size_t uimage_size = 0;
129 size_t rootfs_offset;
130 size_t rootfs_size = 0;
131 size_t buflen;
132 int uimage_part, rf_part;
133 int ret;
134 int extralen = 0;
135 u32 ih_magic = IH_MAGIC;
136 u32 ih_type = IH_TYPE_KERNEL;
137 u32 header_offset = 0;
138 u32 part_magic = 0;
139 enum mtdsplit_part_type type;
140
141 nr_parts = 2;
142 parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
143 if (!parts)
144 return -ENOMEM;
145
146 uimage_parse_dt(master, &extralen, &ih_magic, &ih_type, &header_offset, &part_magic);
147 buflen = sizeof(struct uimage_header) + header_offset;
148 buf = vmalloc(buflen);
149 if (!buf) {
150 ret = -ENOMEM;
151 goto err_free_parts;
152 }
153
154 /* find uImage on erase block boundaries */
155 for (offset = 0; offset < master->size; offset += master->erasesize) {
156 struct uimage_header *header;
157
158 uimage_size = 0;
159
160 ret = read_uimage_header(master, offset, buf, buflen);
161 if (ret)
162 continue;
163
164 /* verify optional partition magic before uimage header */
165 if (header_offset && part_magic && (be32_to_cpu(*(u32 *)buf) != part_magic))
166 continue;
167
168 ret = uimage_verify_default(buf + header_offset, ih_magic, ih_type);
169 if (ret < 0) {
170 pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
171 master->name, (unsigned long long) offset);
172 continue;
173 }
174
175 header = (struct uimage_header *)(buf + header_offset);
176
177 uimage_size = sizeof(*header) +
178 be32_to_cpu(header->ih_size) + header_offset + extralen;
179
180 if ((offset + uimage_size) > master->size) {
181 pr_debug("uImage exceeds MTD device \"%s\"\n",
182 master->name);
183 continue;
184 }
185 break;
186 }
187
188 if (uimage_size == 0) {
189 pr_debug("no uImage found in \"%s\"\n", master->name);
190 ret = -ENODEV;
191 goto err_free_buf;
192 }
193
194 uimage_offset = offset;
195
196 if (uimage_offset == 0) {
197 uimage_part = 0;
198 rf_part = 1;
199
200 /* find the roots after the uImage */
201 ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size,
202 master->size, &rootfs_offset, &type);
203 if (ret) {
204 pr_debug("no rootfs after uImage in \"%s\"\n",
205 master->name);
206 goto err_free_buf;
207 }
208
209 rootfs_size = master->size - rootfs_offset;
210 uimage_size = rootfs_offset - uimage_offset;
211 } else {
212 rf_part = 0;
213 uimage_part = 1;
214
215 /* check rootfs presence at offset 0 */
216 ret = mtd_check_rootfs_magic(master, 0, &type);
217 if (ret) {
218 pr_debug("no rootfs before uImage in \"%s\"\n",
219 master->name);
220 goto err_free_buf;
221 }
222
223 rootfs_offset = 0;
224 rootfs_size = uimage_offset;
225 }
226
227 if (rootfs_size == 0) {
228 pr_debug("no rootfs found in \"%s\"\n", master->name);
229 ret = -ENODEV;
230 goto err_free_buf;
231 }
232
233 parts[uimage_part].name = KERNEL_PART_NAME;
234 parts[uimage_part].offset = uimage_offset;
235 parts[uimage_part].size = uimage_size;
236
237 if (type == MTDSPLIT_PART_TYPE_UBI)
238 parts[rf_part].name = UBI_PART_NAME;
239 else
240 parts[rf_part].name = ROOTFS_PART_NAME;
241 parts[rf_part].offset = rootfs_offset;
242 parts[rf_part].size = rootfs_size;
243
244 vfree(buf);
245
246 *pparts = parts;
247 return nr_parts;
248
249 err_free_buf:
250 vfree(buf);
251
252 err_free_parts:
253 kfree(parts);
254 return ret;
255 }
256
257 static const struct of_device_id mtdsplit_uimage_of_match_table[] = {
258 { .compatible = "denx,uimage" },
259 { .compatible = "openwrt,uimage" },
260 {},
261 };
262
263 static struct mtd_part_parser uimage_generic_parser = {
264 .owner = THIS_MODULE,
265 .name = "uimage-fw",
266 .of_match_table = mtdsplit_uimage_of_match_table,
267 .parse_fn = __mtdsplit_parse_uimage,
268 .type = MTD_PARSER_TYPE_FIRMWARE,
269 };
270
271 /**************************************************
272 * Init
273 **************************************************/
274
275 static int __init mtdsplit_uimage_init(void)
276 {
277 register_mtd_parser(&uimage_generic_parser);
278
279 return 0;
280 }
281
282 module_init(mtdsplit_uimage_init);