281175bae74ebb11981afea2d8672874abe34b6a
[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
23 #include "mtdsplit.h"
24
25 /*
26 * uimage_header itself is only 64B, but it may be prepended with another data.
27 * Currently the biggest size is for Fon(Foxconn) devices: 64B + 32B
28 */
29 #define MAX_HEADER_LEN 96
30
31 #define IH_MAGIC 0x27051956 /* Image Magic Number */
32 #define IH_NMLEN 32 /* Image Name Length */
33
34 #define IH_OS_LINUX 5 /* Linux */
35
36 #define IH_TYPE_KERNEL 2 /* OS Kernel Image */
37 #define IH_TYPE_FILESYSTEM 7 /* Filesystem Image */
38
39 /*
40 * Legacy format image header,
41 * all data in network byte order (aka natural aka bigendian).
42 */
43 struct uimage_header {
44 uint32_t ih_magic; /* Image Header Magic Number */
45 uint32_t ih_hcrc; /* Image Header CRC Checksum */
46 uint32_t ih_time; /* Image Creation Timestamp */
47 uint32_t ih_size; /* Image Data Size */
48 uint32_t ih_load; /* Data Load Address */
49 uint32_t ih_ep; /* Entry Point Address */
50 uint32_t ih_dcrc; /* Image Data CRC Checksum */
51 uint8_t ih_os; /* Operating System */
52 uint8_t ih_arch; /* CPU architecture */
53 uint8_t ih_type; /* Image Type */
54 uint8_t ih_comp; /* Compression Type */
55 uint8_t ih_name[IH_NMLEN]; /* Image Name */
56 };
57
58 static int
59 read_uimage_header(struct mtd_info *mtd, size_t offset, u_char *buf,
60 size_t header_len)
61 {
62 size_t retlen;
63 int ret;
64
65 ret = mtd_read(mtd, offset, header_len, &retlen, buf);
66 if (ret) {
67 pr_debug("read error in \"%s\"\n", mtd->name);
68 return ret;
69 }
70
71 if (retlen != header_len) {
72 pr_debug("short read in \"%s\"\n", mtd->name);
73 return -EIO;
74 }
75
76 return 0;
77 }
78
79 /**
80 * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
81 *
82 * @find_header: function to call for a block of data that will return offset
83 * and tail padding length of a valid uImage header if found
84 */
85 static int __mtdsplit_parse_uimage(struct mtd_info *master,
86 const struct mtd_partition **pparts,
87 struct mtd_part_parser_data *data,
88 ssize_t (*find_header)(u_char *buf, size_t len, int *extralen))
89 {
90 struct mtd_partition *parts;
91 u_char *buf;
92 int nr_parts;
93 size_t offset;
94 size_t uimage_offset;
95 size_t uimage_size = 0;
96 size_t rootfs_offset;
97 size_t rootfs_size = 0;
98 int uimage_part, rf_part;
99 int ret;
100 int extralen;
101 enum mtdsplit_part_type type;
102
103 nr_parts = 2;
104 parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
105 if (!parts)
106 return -ENOMEM;
107
108 buf = vmalloc(MAX_HEADER_LEN);
109 if (!buf) {
110 ret = -ENOMEM;
111 goto err_free_parts;
112 }
113
114 /* find uImage on erase block boundaries */
115 for (offset = 0; offset < master->size; offset += master->erasesize) {
116 struct uimage_header *header;
117
118 uimage_size = 0;
119
120 ret = read_uimage_header(master, offset, buf, MAX_HEADER_LEN);
121 if (ret)
122 continue;
123
124 extralen = 0;
125 ret = find_header(buf, MAX_HEADER_LEN, &extralen);
126 if (ret < 0) {
127 pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
128 master->name, (unsigned long long) offset);
129 continue;
130 }
131 header = (struct uimage_header *)(buf + ret);
132
133 uimage_size = sizeof(*header) +
134 be32_to_cpu(header->ih_size) + ret + extralen;
135
136 if ((offset + uimage_size) > master->size) {
137 pr_debug("uImage exceeds MTD device \"%s\"\n",
138 master->name);
139 continue;
140 }
141 break;
142 }
143
144 if (uimage_size == 0) {
145 pr_debug("no uImage found in \"%s\"\n", master->name);
146 ret = -ENODEV;
147 goto err_free_buf;
148 }
149
150 uimage_offset = offset;
151
152 if (uimage_offset == 0) {
153 uimage_part = 0;
154 rf_part = 1;
155
156 /* find the roots after the uImage */
157 ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size,
158 master->size, &rootfs_offset, &type);
159 if (ret) {
160 pr_debug("no rootfs after uImage in \"%s\"\n",
161 master->name);
162 goto err_free_buf;
163 }
164
165 rootfs_size = master->size - rootfs_offset;
166 uimage_size = rootfs_offset - uimage_offset;
167 } else {
168 rf_part = 0;
169 uimage_part = 1;
170
171 /* check rootfs presence at offset 0 */
172 ret = mtd_check_rootfs_magic(master, 0, &type);
173 if (ret) {
174 pr_debug("no rootfs before uImage in \"%s\"\n",
175 master->name);
176 goto err_free_buf;
177 }
178
179 rootfs_offset = 0;
180 rootfs_size = uimage_offset;
181 }
182
183 if (rootfs_size == 0) {
184 pr_debug("no rootfs found in \"%s\"\n", master->name);
185 ret = -ENODEV;
186 goto err_free_buf;
187 }
188
189 parts[uimage_part].name = KERNEL_PART_NAME;
190 parts[uimage_part].offset = uimage_offset;
191 parts[uimage_part].size = uimage_size;
192
193 if (type == MTDSPLIT_PART_TYPE_UBI)
194 parts[rf_part].name = UBI_PART_NAME;
195 else
196 parts[rf_part].name = ROOTFS_PART_NAME;
197 parts[rf_part].offset = rootfs_offset;
198 parts[rf_part].size = rootfs_size;
199
200 vfree(buf);
201
202 *pparts = parts;
203 return nr_parts;
204
205 err_free_buf:
206 vfree(buf);
207
208 err_free_parts:
209 kfree(parts);
210 return ret;
211 }
212
213 static ssize_t uimage_verify_default(u_char *buf, size_t len, int *extralen)
214 {
215 struct uimage_header *header = (struct uimage_header *)buf;
216
217 /* default sanity checks */
218 if (be32_to_cpu(header->ih_magic) != IH_MAGIC) {
219 pr_debug("invalid uImage magic: %08x\n",
220 be32_to_cpu(header->ih_magic));
221 return -EINVAL;
222 }
223
224 if (header->ih_os != IH_OS_LINUX) {
225 pr_debug("invalid uImage OS: %08x\n",
226 be32_to_cpu(header->ih_os));
227 return -EINVAL;
228 }
229
230 if (header->ih_type != IH_TYPE_KERNEL) {
231 pr_debug("invalid uImage type: %08x\n",
232 be32_to_cpu(header->ih_type));
233 return -EINVAL;
234 }
235
236 return 0;
237 }
238
239 static int
240 mtdsplit_uimage_parse_generic(struct mtd_info *master,
241 const struct mtd_partition **pparts,
242 struct mtd_part_parser_data *data)
243 {
244 return __mtdsplit_parse_uimage(master, pparts, data,
245 uimage_verify_default);
246 }
247
248 static const struct of_device_id mtdsplit_uimage_of_match_table[] = {
249 { .compatible = "denx,uimage" },
250 {},
251 };
252
253 static struct mtd_part_parser uimage_generic_parser = {
254 .owner = THIS_MODULE,
255 .name = "uimage-fw",
256 .of_match_table = mtdsplit_uimage_of_match_table,
257 .parse_fn = mtdsplit_uimage_parse_generic,
258 .type = MTD_PARSER_TYPE_FIRMWARE,
259 };
260
261 #define FW_MAGIC_WNR2000V1 0x32303031
262 #define FW_MAGIC_WNR2000V3 0x32303033
263 #define FW_MAGIC_WNR2000V4 0x32303034
264 #define FW_MAGIC_WNR2200 0x32323030
265 #define FW_MAGIC_WNR612V2 0x32303631
266 #define FW_MAGIC_WNR1000V2 0x31303031
267 #define FW_MAGIC_WNR1000V2_VC 0x31303030
268 #define FW_MAGIC_WNDR3700 0x33373030
269 #define FW_MAGIC_WNDR3700V2 0x33373031
270 #define FW_MAGIC_WPN824N 0x31313030
271
272 static ssize_t uimage_verify_wndr3700(u_char *buf, size_t len, int *extralen)
273 {
274 struct uimage_header *header = (struct uimage_header *)buf;
275 uint8_t expected_type = IH_TYPE_FILESYSTEM;
276
277 switch (be32_to_cpu(header->ih_magic)) {
278 case FW_MAGIC_WNR612V2:
279 case FW_MAGIC_WNR1000V2:
280 case FW_MAGIC_WNR1000V2_VC:
281 case FW_MAGIC_WNR2000V1:
282 case FW_MAGIC_WNR2000V3:
283 case FW_MAGIC_WNR2200:
284 case FW_MAGIC_WNDR3700:
285 case FW_MAGIC_WNDR3700V2:
286 case FW_MAGIC_WPN824N:
287 break;
288 case FW_MAGIC_WNR2000V4:
289 expected_type = IH_TYPE_KERNEL;
290 break;
291 default:
292 return -EINVAL;
293 }
294
295 if (header->ih_os != IH_OS_LINUX ||
296 header->ih_type != expected_type)
297 return -EINVAL;
298
299 return 0;
300 }
301
302 static int
303 mtdsplit_uimage_parse_netgear(struct mtd_info *master,
304 const struct mtd_partition **pparts,
305 struct mtd_part_parser_data *data)
306 {
307 return __mtdsplit_parse_uimage(master, pparts, data,
308 uimage_verify_wndr3700);
309 }
310
311 static const struct of_device_id mtdsplit_uimage_netgear_of_match_table[] = {
312 { .compatible = "netgear,uimage" },
313 {},
314 };
315
316 static struct mtd_part_parser uimage_netgear_parser = {
317 .owner = THIS_MODULE,
318 .name = "netgear-fw",
319 .of_match_table = mtdsplit_uimage_netgear_of_match_table,
320 .parse_fn = mtdsplit_uimage_parse_netgear,
321 .type = MTD_PARSER_TYPE_FIRMWARE,
322
323 };
324
325
326 /**************************************************
327 * ALLNET
328 **************************************************/
329
330 #define FW_MAGIC_SG8208M 0x00000006
331 #define FW_MAGIC_SG8310PM 0x83000006
332
333 static ssize_t uimage_verify_allnet(u_char *buf, size_t len, int *extralen)
334 {
335 struct uimage_header *header = (struct uimage_header *)buf;
336
337 switch (be32_to_cpu(header->ih_magic)) {
338 case FW_MAGIC_SG8208M:
339 case FW_MAGIC_SG8310PM:
340 break;
341 default:
342 return -EINVAL;
343 }
344
345 if (header->ih_os != IH_OS_LINUX)
346 return -EINVAL;
347
348 return 0;
349 }
350
351 static int
352 mtdsplit_uimage_parse_allnet(struct mtd_info *master,
353 const struct mtd_partition **pparts,
354 struct mtd_part_parser_data *data)
355 {
356 return __mtdsplit_parse_uimage(master, pparts, data,
357 uimage_verify_allnet);
358 }
359
360 static const struct of_device_id mtdsplit_uimage_allnet_of_match_table[] = {
361 { .compatible = "allnet,uimage" },
362 {},
363 };
364
365 static struct mtd_part_parser uimage_allnet_parser = {
366 .owner = THIS_MODULE,
367 .name = "allnet-fw",
368 .of_match_table = mtdsplit_uimage_allnet_of_match_table,
369 .parse_fn = mtdsplit_uimage_parse_allnet,
370 };
371
372
373 /**************************************************
374 * Edimax
375 **************************************************/
376
377 #define FW_EDIMAX_OFFSET 20
378 #define FW_MAGIC_EDIMAX 0x43535953
379
380 static ssize_t uimage_find_edimax(u_char *buf, size_t len, int *extralen)
381 {
382 u32 *magic;
383
384 if (len < FW_EDIMAX_OFFSET + sizeof(struct uimage_header)) {
385 pr_err("Buffer too small for checking Edimax header\n");
386 return -ENOSPC;
387 }
388
389 magic = (u32 *)buf;
390 if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
391 return -EINVAL;
392
393 if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len, extralen))
394 return FW_EDIMAX_OFFSET;
395
396 return -EINVAL;
397 }
398
399 static int
400 mtdsplit_uimage_parse_edimax(struct mtd_info *master,
401 const struct mtd_partition **pparts,
402 struct mtd_part_parser_data *data)
403 {
404 return __mtdsplit_parse_uimage(master, pparts, data,
405 uimage_find_edimax);
406 }
407
408 static const struct of_device_id mtdsplit_uimage_edimax_of_match_table[] = {
409 { .compatible = "edimax,uimage" },
410 {},
411 };
412
413 static struct mtd_part_parser uimage_edimax_parser = {
414 .owner = THIS_MODULE,
415 .name = "edimax-fw",
416 .of_match_table = mtdsplit_uimage_edimax_of_match_table,
417 .parse_fn = mtdsplit_uimage_parse_edimax,
418 .type = MTD_PARSER_TYPE_FIRMWARE,
419 };
420
421
422 /**************************************************
423 * Fon(Foxconn)
424 **************************************************/
425
426 #define FONFXC_PAD_LEN 32
427
428 static ssize_t uimage_find_fonfxc(u_char *buf, size_t len, int *extralen)
429 {
430 if (uimage_verify_default(buf, len, extralen) < 0)
431 return -EINVAL;
432
433 *extralen = FONFXC_PAD_LEN;
434
435 return 0;
436 }
437
438 static int
439 mtdsplit_uimage_parse_fonfxc(struct mtd_info *master,
440 const struct mtd_partition **pparts,
441 struct mtd_part_parser_data *data)
442 {
443 return __mtdsplit_parse_uimage(master, pparts, data,
444 uimage_find_fonfxc);
445 }
446
447 static const struct of_device_id mtdsplit_uimage_fonfxc_of_match_table[] = {
448 { .compatible = "fonfxc,uimage" },
449 {},
450 };
451
452 static struct mtd_part_parser uimage_fonfxc_parser = {
453 .owner = THIS_MODULE,
454 .name = "fonfxc-fw",
455 .of_match_table = mtdsplit_uimage_fonfxc_of_match_table,
456 .parse_fn = mtdsplit_uimage_parse_fonfxc,
457 };
458
459 /**************************************************
460 * SGE (T&W) Shenzhen Gongjin Electronics
461 **************************************************/
462
463 #define SGE_PAD_LEN 96
464
465 static ssize_t uimage_find_sge(u_char *buf, size_t len, int *extralen)
466 {
467 if (uimage_verify_default(buf, len, extralen) < 0)
468 return -EINVAL;
469
470 *extralen = SGE_PAD_LEN;
471
472 return 0;
473 }
474
475 static int
476 mtdsplit_uimage_parse_sge(struct mtd_info *master,
477 const struct mtd_partition **pparts,
478 struct mtd_part_parser_data *data)
479 {
480 return __mtdsplit_parse_uimage(master, pparts, data,
481 uimage_find_sge);
482 }
483
484 static const struct of_device_id mtdsplit_uimage_sge_of_match_table[] = {
485 { .compatible = "sge,uimage" },
486 {},
487 };
488
489 static struct mtd_part_parser uimage_sge_parser = {
490 .owner = THIS_MODULE,
491 .name = "sge-fw",
492 .of_match_table = mtdsplit_uimage_sge_of_match_table,
493 .parse_fn = mtdsplit_uimage_parse_sge,
494 };
495
496 /**************************************************
497 * OKLI (OpenWrt Kernel Loader Image)
498 **************************************************/
499
500 #define IH_MAGIC_OKLI 0x4f4b4c49
501
502 static ssize_t uimage_verify_okli(u_char *buf, size_t len, int *extralen)
503 {
504 struct uimage_header *header = (struct uimage_header *)buf;
505
506 /* default sanity checks */
507 if (be32_to_cpu(header->ih_magic) != IH_MAGIC_OKLI) {
508 pr_debug("invalid uImage magic: %08x\n",
509 be32_to_cpu(header->ih_magic));
510 return -EINVAL;
511 }
512
513 if (header->ih_os != IH_OS_LINUX) {
514 pr_debug("invalid uImage OS: %08x\n",
515 be32_to_cpu(header->ih_os));
516 return -EINVAL;
517 }
518
519 if (header->ih_type != IH_TYPE_KERNEL) {
520 pr_debug("invalid uImage type: %08x\n",
521 be32_to_cpu(header->ih_type));
522 return -EINVAL;
523 }
524
525 return 0;
526 }
527
528 static int
529 mtdsplit_uimage_parse_okli(struct mtd_info *master,
530 const struct mtd_partition **pparts,
531 struct mtd_part_parser_data *data)
532 {
533 return __mtdsplit_parse_uimage(master, pparts, data,
534 uimage_verify_okli);
535 }
536
537 static const struct of_device_id mtdsplit_uimage_okli_of_match_table[] = {
538 { .compatible = "openwrt,okli" },
539 {},
540 };
541
542 static struct mtd_part_parser uimage_okli_parser = {
543 .owner = THIS_MODULE,
544 .name = "okli-fw",
545 .of_match_table = mtdsplit_uimage_okli_of_match_table,
546 .parse_fn = mtdsplit_uimage_parse_okli,
547 };
548
549 /**************************************************
550 * Init
551 **************************************************/
552
553 static int __init mtdsplit_uimage_init(void)
554 {
555 register_mtd_parser(&uimage_generic_parser);
556 register_mtd_parser(&uimage_netgear_parser);
557 register_mtd_parser(&uimage_allnet_parser);
558 register_mtd_parser(&uimage_edimax_parser);
559 register_mtd_parser(&uimage_fonfxc_parser);
560 register_mtd_parser(&uimage_sge_parser);
561 register_mtd_parser(&uimage_okli_parser);
562
563 return 0;
564 }
565
566 module_init(mtdsplit_uimage_init);