kernel: mtdsplit_uimage: replace "netgear, uimage" parser
[openwrt/staging/rmilecki.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 * uimage_header itself is only 64B, but it may be prepended with another data.
28 * Currently the biggest size is for Fon(Foxconn) devices: 64B + 32B
29 */
30 #define MAX_HEADER_LEN 96
31
32 /*
33 * Legacy format image header,
34 * all data in network byte order (aka natural aka bigendian).
35 */
36 struct uimage_header {
37 uint32_t ih_magic; /* Image Header Magic Number */
38 uint32_t ih_hcrc; /* Image Header CRC Checksum */
39 uint32_t ih_time; /* Image Creation Timestamp */
40 uint32_t ih_size; /* Image Data Size */
41 uint32_t ih_load; /* Data Load Address */
42 uint32_t ih_ep; /* Entry Point Address */
43 uint32_t ih_dcrc; /* Image Data CRC Checksum */
44 uint8_t ih_os; /* Operating System */
45 uint8_t ih_arch; /* CPU architecture */
46 uint8_t ih_type; /* Image Type */
47 uint8_t ih_comp; /* Compression Type */
48 uint8_t ih_name[IH_NMLEN]; /* Image Name */
49 };
50
51 static int
52 read_uimage_header(struct mtd_info *mtd, size_t offset, u_char *buf,
53 size_t header_len)
54 {
55 size_t retlen;
56 int ret;
57
58 ret = mtd_read(mtd, offset, header_len, &retlen, buf);
59 if (ret) {
60 pr_debug("read error in \"%s\"\n", mtd->name);
61 return ret;
62 }
63
64 if (retlen != header_len) {
65 pr_debug("short read in \"%s\"\n", mtd->name);
66 return -EIO;
67 }
68
69 return 0;
70 }
71
72 static void uimage_parse_dt(struct mtd_info *master, int *extralen,
73 u32 *ih_magic, u32 *ih_type)
74 {
75 struct device_node *np = mtd_get_of_node(master);
76
77 if (!np || !of_device_is_compatible(np, "openwrt,uimage"))
78 return;
79
80 if (!of_property_read_u32(np, "openwrt,padding", extralen))
81 pr_debug("got openwrt,padding=%d from device-tree\n", *extralen);
82 if (!of_property_read_u32(np, "openwrt,ih-magic", ih_magic))
83 pr_debug("got openwrt,ih-magic=%08x from device-tree\n", *ih_magic);
84 if (!of_property_read_u32(np, "openwrt,ih-type", ih_type))
85 pr_debug("got openwrt,ih-type=%08x from device-tree\n", *ih_type);
86 }
87
88 /**
89 * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
90 *
91 * @find_header: function to call for a block of data that will return offset
92 * and tail padding length of a valid uImage header if found
93 */
94 static int __mtdsplit_parse_uimage(struct mtd_info *master,
95 const struct mtd_partition **pparts,
96 struct mtd_part_parser_data *data,
97 ssize_t (*find_header)(u_char *buf, size_t len, u32 ih_magic, u32 ih_type))
98 {
99 struct mtd_partition *parts;
100 u_char *buf;
101 int nr_parts;
102 size_t offset;
103 size_t uimage_offset;
104 size_t uimage_size = 0;
105 size_t rootfs_offset;
106 size_t rootfs_size = 0;
107 int uimage_part, rf_part;
108 int ret;
109 int extralen = 0;
110 u32 ih_magic = IH_MAGIC;
111 u32 ih_type = IH_TYPE_KERNEL;
112 enum mtdsplit_part_type type;
113
114 nr_parts = 2;
115 parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
116 if (!parts)
117 return -ENOMEM;
118
119 buf = vmalloc(MAX_HEADER_LEN);
120 if (!buf) {
121 ret = -ENOMEM;
122 goto err_free_parts;
123 }
124
125 uimage_parse_dt(master, &extralen, &ih_magic, &ih_type);
126
127 /* find uImage on erase block boundaries */
128 for (offset = 0; offset < master->size; offset += master->erasesize) {
129 struct uimage_header *header;
130
131 uimage_size = 0;
132
133 ret = read_uimage_header(master, offset, buf, MAX_HEADER_LEN);
134 if (ret)
135 continue;
136
137 ret = find_header(buf, MAX_HEADER_LEN, ih_magic, ih_type);
138 if (ret < 0) {
139 pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
140 master->name, (unsigned long long) offset);
141 continue;
142 }
143 header = (struct uimage_header *)(buf + ret);
144
145 uimage_size = sizeof(*header) +
146 be32_to_cpu(header->ih_size) + ret + extralen;
147
148 if ((offset + uimage_size) > master->size) {
149 pr_debug("uImage exceeds MTD device \"%s\"\n",
150 master->name);
151 continue;
152 }
153 break;
154 }
155
156 if (uimage_size == 0) {
157 pr_debug("no uImage found in \"%s\"\n", master->name);
158 ret = -ENODEV;
159 goto err_free_buf;
160 }
161
162 uimage_offset = offset;
163
164 if (uimage_offset == 0) {
165 uimage_part = 0;
166 rf_part = 1;
167
168 /* find the roots after the uImage */
169 ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size,
170 master->size, &rootfs_offset, &type);
171 if (ret) {
172 pr_debug("no rootfs after uImage in \"%s\"\n",
173 master->name);
174 goto err_free_buf;
175 }
176
177 rootfs_size = master->size - rootfs_offset;
178 uimage_size = rootfs_offset - uimage_offset;
179 } else {
180 rf_part = 0;
181 uimage_part = 1;
182
183 /* check rootfs presence at offset 0 */
184 ret = mtd_check_rootfs_magic(master, 0, &type);
185 if (ret) {
186 pr_debug("no rootfs before uImage in \"%s\"\n",
187 master->name);
188 goto err_free_buf;
189 }
190
191 rootfs_offset = 0;
192 rootfs_size = uimage_offset;
193 }
194
195 if (rootfs_size == 0) {
196 pr_debug("no rootfs found in \"%s\"\n", master->name);
197 ret = -ENODEV;
198 goto err_free_buf;
199 }
200
201 parts[uimage_part].name = KERNEL_PART_NAME;
202 parts[uimage_part].offset = uimage_offset;
203 parts[uimage_part].size = uimage_size;
204
205 if (type == MTDSPLIT_PART_TYPE_UBI)
206 parts[rf_part].name = UBI_PART_NAME;
207 else
208 parts[rf_part].name = ROOTFS_PART_NAME;
209 parts[rf_part].offset = rootfs_offset;
210 parts[rf_part].size = rootfs_size;
211
212 vfree(buf);
213
214 *pparts = parts;
215 return nr_parts;
216
217 err_free_buf:
218 vfree(buf);
219
220 err_free_parts:
221 kfree(parts);
222 return ret;
223 }
224
225 static ssize_t uimage_verify_default(u_char *buf, size_t len, u32 ih_magic, u32 ih_type)
226 {
227 struct uimage_header *header = (struct uimage_header *)buf;
228
229 /* default sanity checks */
230 if (be32_to_cpu(header->ih_magic) != ih_magic) {
231 pr_debug("invalid uImage magic: %08x != %08x\n",
232 be32_to_cpu(header->ih_magic), ih_magic);
233 return -EINVAL;
234 }
235
236 if (header->ih_os != IH_OS_LINUX) {
237 pr_debug("invalid uImage OS: %08x != %08x\n",
238 be32_to_cpu(header->ih_os), IH_OS_LINUX);
239 return -EINVAL;
240 }
241
242 if (header->ih_type != ih_type) {
243 pr_debug("invalid uImage type: %08x != %08x\n",
244 be32_to_cpu(header->ih_type), ih_type);
245 return -EINVAL;
246 }
247
248 return 0;
249 }
250
251 static int
252 mtdsplit_uimage_parse_generic(struct mtd_info *master,
253 const struct mtd_partition **pparts,
254 struct mtd_part_parser_data *data)
255 {
256 return __mtdsplit_parse_uimage(master, pparts, data,
257 uimage_verify_default);
258 }
259
260 static const struct of_device_id mtdsplit_uimage_of_match_table[] = {
261 { .compatible = "denx,uimage" },
262 { .compatible = "openwrt,uimage" },
263 {},
264 };
265
266 static struct mtd_part_parser uimage_generic_parser = {
267 .owner = THIS_MODULE,
268 .name = "uimage-fw",
269 .of_match_table = mtdsplit_uimage_of_match_table,
270 .parse_fn = mtdsplit_uimage_parse_generic,
271 .type = MTD_PARSER_TYPE_FIRMWARE,
272 };
273
274 /**************************************************
275 * Edimax
276 **************************************************/
277
278 #define FW_EDIMAX_OFFSET 20
279 #define FW_MAGIC_EDIMAX 0x43535953
280
281 static ssize_t uimage_find_edimax(u_char *buf, size_t len, u32 ih_magic, u32 ih_type)
282 {
283 u32 *magic;
284
285 if (len < FW_EDIMAX_OFFSET + sizeof(struct uimage_header)) {
286 pr_err("Buffer too small for checking Edimax header\n");
287 return -ENOSPC;
288 }
289
290 magic = (u32 *)buf;
291 if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
292 return -EINVAL;
293
294 if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len, ih_magic, ih_type))
295 return FW_EDIMAX_OFFSET;
296
297 return -EINVAL;
298 }
299
300 static int
301 mtdsplit_uimage_parse_edimax(struct mtd_info *master,
302 const struct mtd_partition **pparts,
303 struct mtd_part_parser_data *data)
304 {
305 return __mtdsplit_parse_uimage(master, pparts, data,
306 uimage_find_edimax);
307 }
308
309 static const struct of_device_id mtdsplit_uimage_edimax_of_match_table[] = {
310 { .compatible = "edimax,uimage" },
311 {},
312 };
313
314 static struct mtd_part_parser uimage_edimax_parser = {
315 .owner = THIS_MODULE,
316 .name = "edimax-fw",
317 .of_match_table = mtdsplit_uimage_edimax_of_match_table,
318 .parse_fn = mtdsplit_uimage_parse_edimax,
319 .type = MTD_PARSER_TYPE_FIRMWARE,
320 };
321
322
323 /**************************************************
324 * Init
325 **************************************************/
326
327 static int __init mtdsplit_uimage_init(void)
328 {
329 register_mtd_parser(&uimage_generic_parser);
330 register_mtd_parser(&uimage_edimax_parser);
331
332 return 0;
333 }
334
335 module_init(mtdsplit_uimage_init);