f5e48c661e8fc8a05ba26da44675b97e13fc3947
[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 * 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 {
74 struct device_node *np = mtd_get_of_node(master);
75
76 if (!np || !of_device_is_compatible(np, "openwrt,uimage"))
77 return;
78 of_property_read_u32(np, "openwrt,padding", extralen);
79 }
80
81 /**
82 * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
83 *
84 * @find_header: function to call for a block of data that will return offset
85 * and tail padding length of a valid uImage header if found
86 */
87 static int __mtdsplit_parse_uimage(struct mtd_info *master,
88 const struct mtd_partition **pparts,
89 struct mtd_part_parser_data *data,
90 ssize_t (*find_header)(u_char *buf, size_t len))
91 {
92 struct mtd_partition *parts;
93 u_char *buf;
94 int nr_parts;
95 size_t offset;
96 size_t uimage_offset;
97 size_t uimage_size = 0;
98 size_t rootfs_offset;
99 size_t rootfs_size = 0;
100 int uimage_part, rf_part;
101 int ret;
102 int extralen = 0;
103 enum mtdsplit_part_type type;
104
105 nr_parts = 2;
106 parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
107 if (!parts)
108 return -ENOMEM;
109
110 buf = vmalloc(MAX_HEADER_LEN);
111 if (!buf) {
112 ret = -ENOMEM;
113 goto err_free_parts;
114 }
115
116 uimage_parse_dt(master, &extralen);
117
118 /* find uImage on erase block boundaries */
119 for (offset = 0; offset < master->size; offset += master->erasesize) {
120 struct uimage_header *header;
121
122 uimage_size = 0;
123
124 ret = read_uimage_header(master, offset, buf, MAX_HEADER_LEN);
125 if (ret)
126 continue;
127
128 ret = find_header(buf, MAX_HEADER_LEN);
129 if (ret < 0) {
130 pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
131 master->name, (unsigned long long) offset);
132 continue;
133 }
134 header = (struct uimage_header *)(buf + ret);
135
136 uimage_size = sizeof(*header) +
137 be32_to_cpu(header->ih_size) + ret + extralen;
138
139 if ((offset + uimage_size) > master->size) {
140 pr_debug("uImage exceeds MTD device \"%s\"\n",
141 master->name);
142 continue;
143 }
144 break;
145 }
146
147 if (uimage_size == 0) {
148 pr_debug("no uImage found in \"%s\"\n", master->name);
149 ret = -ENODEV;
150 goto err_free_buf;
151 }
152
153 uimage_offset = offset;
154
155 if (uimage_offset == 0) {
156 uimage_part = 0;
157 rf_part = 1;
158
159 /* find the roots after the uImage */
160 ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size,
161 master->size, &rootfs_offset, &type);
162 if (ret) {
163 pr_debug("no rootfs after uImage in \"%s\"\n",
164 master->name);
165 goto err_free_buf;
166 }
167
168 rootfs_size = master->size - rootfs_offset;
169 uimage_size = rootfs_offset - uimage_offset;
170 } else {
171 rf_part = 0;
172 uimage_part = 1;
173
174 /* check rootfs presence at offset 0 */
175 ret = mtd_check_rootfs_magic(master, 0, &type);
176 if (ret) {
177 pr_debug("no rootfs before uImage in \"%s\"\n",
178 master->name);
179 goto err_free_buf;
180 }
181
182 rootfs_offset = 0;
183 rootfs_size = uimage_offset;
184 }
185
186 if (rootfs_size == 0) {
187 pr_debug("no rootfs found in \"%s\"\n", master->name);
188 ret = -ENODEV;
189 goto err_free_buf;
190 }
191
192 parts[uimage_part].name = KERNEL_PART_NAME;
193 parts[uimage_part].offset = uimage_offset;
194 parts[uimage_part].size = uimage_size;
195
196 if (type == MTDSPLIT_PART_TYPE_UBI)
197 parts[rf_part].name = UBI_PART_NAME;
198 else
199 parts[rf_part].name = ROOTFS_PART_NAME;
200 parts[rf_part].offset = rootfs_offset;
201 parts[rf_part].size = rootfs_size;
202
203 vfree(buf);
204
205 *pparts = parts;
206 return nr_parts;
207
208 err_free_buf:
209 vfree(buf);
210
211 err_free_parts:
212 kfree(parts);
213 return ret;
214 }
215
216 static ssize_t uimage_verify_default(u_char *buf, size_t len)
217 {
218 struct uimage_header *header = (struct uimage_header *)buf;
219
220 /* default sanity checks */
221 if (be32_to_cpu(header->ih_magic) != IH_MAGIC) {
222 pr_debug("invalid uImage magic: %08x\n",
223 be32_to_cpu(header->ih_magic));
224 return -EINVAL;
225 }
226
227 if (header->ih_os != IH_OS_LINUX) {
228 pr_debug("invalid uImage OS: %08x\n",
229 be32_to_cpu(header->ih_os));
230 return -EINVAL;
231 }
232
233 if (header->ih_type != IH_TYPE_KERNEL) {
234 pr_debug("invalid uImage type: %08x\n",
235 be32_to_cpu(header->ih_type));
236 return -EINVAL;
237 }
238
239 return 0;
240 }
241
242 static int
243 mtdsplit_uimage_parse_generic(struct mtd_info *master,
244 const struct mtd_partition **pparts,
245 struct mtd_part_parser_data *data)
246 {
247 return __mtdsplit_parse_uimage(master, pparts, data,
248 uimage_verify_default);
249 }
250
251 static const struct of_device_id mtdsplit_uimage_of_match_table[] = {
252 { .compatible = "denx,uimage" },
253 { .compatible = "openwrt,uimage" },
254 {},
255 };
256
257 static struct mtd_part_parser uimage_generic_parser = {
258 .owner = THIS_MODULE,
259 .name = "uimage-fw",
260 .of_match_table = mtdsplit_uimage_of_match_table,
261 .parse_fn = mtdsplit_uimage_parse_generic,
262 .type = MTD_PARSER_TYPE_FIRMWARE,
263 };
264
265 #define FW_MAGIC_GS110TPPV1 0x4e474520
266 #define FW_MAGIC_WNR2000V1 0x32303031
267 #define FW_MAGIC_WNR2000V3 0x32303033
268 #define FW_MAGIC_WNR2000V4 0x32303034
269 #define FW_MAGIC_WNR2200 0x32323030
270 #define FW_MAGIC_WNR612V2 0x32303631
271 #define FW_MAGIC_WNR1000V2 0x31303031
272 #define FW_MAGIC_WNR1000V2_VC 0x31303030
273 #define FW_MAGIC_WNDR3700 0x33373030
274 #define FW_MAGIC_WNDR3700V2 0x33373031
275 #define FW_MAGIC_WPN824N 0x31313030
276
277 static ssize_t uimage_verify_wndr3700(u_char *buf, size_t len)
278 {
279 struct uimage_header *header = (struct uimage_header *)buf;
280 uint8_t expected_type = IH_TYPE_FILESYSTEM;
281
282 switch (be32_to_cpu(header->ih_magic)) {
283 case FW_MAGIC_GS110TPPV1:
284 case FW_MAGIC_WNR2000V4:
285 expected_type = IH_TYPE_KERNEL;
286 break;
287 case FW_MAGIC_WNR612V2:
288 case FW_MAGIC_WNR1000V2:
289 case FW_MAGIC_WNR1000V2_VC:
290 case FW_MAGIC_WNR2000V1:
291 case FW_MAGIC_WNR2000V3:
292 case FW_MAGIC_WNR2200:
293 case FW_MAGIC_WNDR3700:
294 case FW_MAGIC_WNDR3700V2:
295 case FW_MAGIC_WPN824N:
296 break;
297 default:
298 return -EINVAL;
299 }
300
301 if (header->ih_os != IH_OS_LINUX ||
302 header->ih_type != expected_type)
303 return -EINVAL;
304
305 return 0;
306 }
307
308 static int
309 mtdsplit_uimage_parse_netgear(struct mtd_info *master,
310 const struct mtd_partition **pparts,
311 struct mtd_part_parser_data *data)
312 {
313 return __mtdsplit_parse_uimage(master, pparts, data,
314 uimage_verify_wndr3700);
315 }
316
317 static const struct of_device_id mtdsplit_uimage_netgear_of_match_table[] = {
318 { .compatible = "netgear,uimage" },
319 {},
320 };
321
322 static struct mtd_part_parser uimage_netgear_parser = {
323 .owner = THIS_MODULE,
324 .name = "netgear-fw",
325 .of_match_table = mtdsplit_uimage_netgear_of_match_table,
326 .parse_fn = mtdsplit_uimage_parse_netgear,
327 .type = MTD_PARSER_TYPE_FIRMWARE,
328
329 };
330
331
332 /**************************************************
333 * ALLNET
334 **************************************************/
335
336 #define FW_MAGIC_SG8208M 0x00000006
337 #define FW_MAGIC_SG8310PM 0x83000006
338
339 static ssize_t uimage_verify_allnet(u_char *buf, size_t len)
340 {
341 struct uimage_header *header = (struct uimage_header *)buf;
342
343 switch (be32_to_cpu(header->ih_magic)) {
344 case FW_MAGIC_SG8208M:
345 case FW_MAGIC_SG8310PM:
346 break;
347 default:
348 return -EINVAL;
349 }
350
351 if (header->ih_os != IH_OS_LINUX)
352 return -EINVAL;
353
354 return 0;
355 }
356
357 static int
358 mtdsplit_uimage_parse_allnet(struct mtd_info *master,
359 const struct mtd_partition **pparts,
360 struct mtd_part_parser_data *data)
361 {
362 return __mtdsplit_parse_uimage(master, pparts, data,
363 uimage_verify_allnet);
364 }
365
366 static const struct of_device_id mtdsplit_uimage_allnet_of_match_table[] = {
367 { .compatible = "allnet,uimage" },
368 {},
369 };
370
371 static struct mtd_part_parser uimage_allnet_parser = {
372 .owner = THIS_MODULE,
373 .name = "allnet-fw",
374 .of_match_table = mtdsplit_uimage_allnet_of_match_table,
375 .parse_fn = mtdsplit_uimage_parse_allnet,
376 };
377
378
379 /**************************************************
380 * Edimax
381 **************************************************/
382
383 #define FW_EDIMAX_OFFSET 20
384 #define FW_MAGIC_EDIMAX 0x43535953
385
386 static ssize_t uimage_find_edimax(u_char *buf, size_t len)
387 {
388 u32 *magic;
389
390 if (len < FW_EDIMAX_OFFSET + sizeof(struct uimage_header)) {
391 pr_err("Buffer too small for checking Edimax header\n");
392 return -ENOSPC;
393 }
394
395 magic = (u32 *)buf;
396 if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
397 return -EINVAL;
398
399 if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len))
400 return FW_EDIMAX_OFFSET;
401
402 return -EINVAL;
403 }
404
405 static int
406 mtdsplit_uimage_parse_edimax(struct mtd_info *master,
407 const struct mtd_partition **pparts,
408 struct mtd_part_parser_data *data)
409 {
410 return __mtdsplit_parse_uimage(master, pparts, data,
411 uimage_find_edimax);
412 }
413
414 static const struct of_device_id mtdsplit_uimage_edimax_of_match_table[] = {
415 { .compatible = "edimax,uimage" },
416 {},
417 };
418
419 static struct mtd_part_parser uimage_edimax_parser = {
420 .owner = THIS_MODULE,
421 .name = "edimax-fw",
422 .of_match_table = mtdsplit_uimage_edimax_of_match_table,
423 .parse_fn = mtdsplit_uimage_parse_edimax,
424 .type = MTD_PARSER_TYPE_FIRMWARE,
425 };
426
427 /**************************************************
428 * OKLI (OpenWrt Kernel Loader Image)
429 **************************************************/
430
431 #define IH_MAGIC_OKLI 0x4f4b4c49
432
433 static ssize_t uimage_verify_okli(u_char *buf, size_t len)
434 {
435 struct uimage_header *header = (struct uimage_header *)buf;
436
437 /* default sanity checks */
438 if (be32_to_cpu(header->ih_magic) != IH_MAGIC_OKLI) {
439 pr_debug("invalid uImage magic: %08x\n",
440 be32_to_cpu(header->ih_magic));
441 return -EINVAL;
442 }
443
444 if (header->ih_os != IH_OS_LINUX) {
445 pr_debug("invalid uImage OS: %08x\n",
446 be32_to_cpu(header->ih_os));
447 return -EINVAL;
448 }
449
450 if (header->ih_type != IH_TYPE_KERNEL) {
451 pr_debug("invalid uImage type: %08x\n",
452 be32_to_cpu(header->ih_type));
453 return -EINVAL;
454 }
455
456 return 0;
457 }
458
459 static int
460 mtdsplit_uimage_parse_okli(struct mtd_info *master,
461 const struct mtd_partition **pparts,
462 struct mtd_part_parser_data *data)
463 {
464 return __mtdsplit_parse_uimage(master, pparts, data,
465 uimage_verify_okli);
466 }
467
468 static const struct of_device_id mtdsplit_uimage_okli_of_match_table[] = {
469 { .compatible = "openwrt,okli" },
470 {},
471 };
472
473 static struct mtd_part_parser uimage_okli_parser = {
474 .owner = THIS_MODULE,
475 .name = "okli-fw",
476 .of_match_table = mtdsplit_uimage_okli_of_match_table,
477 .parse_fn = mtdsplit_uimage_parse_okli,
478 };
479
480 /**************************************************
481 * Init
482 **************************************************/
483
484 static int __init mtdsplit_uimage_init(void)
485 {
486 register_mtd_parser(&uimage_generic_parser);
487 register_mtd_parser(&uimage_netgear_parser);
488 register_mtd_parser(&uimage_allnet_parser);
489 register_mtd_parser(&uimage_edimax_parser);
490 register_mtd_parser(&uimage_okli_parser);
491
492 return 0;
493 }
494
495 module_init(mtdsplit_uimage_init);