2 * Copyright (C) 2013 Gabor Juhos <juhosg@openwrt.org>
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.
10 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
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>
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
29 #define MAX_HEADER_LEN 96
31 #define IH_MAGIC 0x27051956 /* Image Magic Number */
32 #define IH_NMLEN 32 /* Image Name Length */
34 #define IH_OS_LINUX 5 /* Linux */
36 #define IH_TYPE_KERNEL 2 /* OS Kernel Image */
37 #define IH_TYPE_FILESYSTEM 7 /* Filesystem Image */
40 * Legacy format image header,
41 * all data in network byte order (aka natural aka bigendian).
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 */
59 read_uimage_header(struct mtd_info
*mtd
, size_t offset
, u_char
*buf
,
65 ret
= mtd_read(mtd
, offset
, header_len
, &retlen
, buf
);
67 pr_debug("read error in \"%s\"\n", mtd
->name
);
71 if (retlen
!= header_len
) {
72 pr_debug("short read in \"%s\"\n", mtd
->name
);
80 * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
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
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
))
90 struct mtd_partition
*parts
;
95 size_t uimage_size
= 0;
97 size_t rootfs_size
= 0;
98 int uimage_part
, rf_part
;
101 enum mtdsplit_part_type type
;
104 parts
= kzalloc(nr_parts
* sizeof(*parts
), GFP_KERNEL
);
108 buf
= vmalloc(MAX_HEADER_LEN
);
114 /* find uImage on erase block boundaries */
115 for (offset
= 0; offset
< master
->size
; offset
+= master
->erasesize
) {
116 struct uimage_header
*header
;
120 ret
= read_uimage_header(master
, offset
, buf
, MAX_HEADER_LEN
);
125 ret
= find_header(buf
, MAX_HEADER_LEN
, &extralen
);
127 pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
128 master
->name
, (unsigned long long) offset
);
131 header
= (struct uimage_header
*)(buf
+ ret
);
133 uimage_size
= sizeof(*header
) +
134 be32_to_cpu(header
->ih_size
) + ret
+ extralen
;
136 if ((offset
+ uimage_size
) > master
->size
) {
137 pr_debug("uImage exceeds MTD device \"%s\"\n",
144 if (uimage_size
== 0) {
145 pr_debug("no uImage found in \"%s\"\n", master
->name
);
150 uimage_offset
= offset
;
152 if (uimage_offset
== 0) {
156 /* find the roots after the uImage */
157 ret
= mtd_find_rootfs_from(master
, uimage_offset
+ uimage_size
,
158 master
->size
, &rootfs_offset
, &type
);
160 pr_debug("no rootfs after uImage in \"%s\"\n",
165 rootfs_size
= master
->size
- rootfs_offset
;
166 uimage_size
= rootfs_offset
- uimage_offset
;
171 /* check rootfs presence at offset 0 */
172 ret
= mtd_check_rootfs_magic(master
, 0, &type
);
174 pr_debug("no rootfs before uImage in \"%s\"\n",
180 rootfs_size
= uimage_offset
;
183 if (rootfs_size
== 0) {
184 pr_debug("no rootfs found in \"%s\"\n", master
->name
);
189 parts
[uimage_part
].name
= KERNEL_PART_NAME
;
190 parts
[uimage_part
].offset
= uimage_offset
;
191 parts
[uimage_part
].size
= uimage_size
;
193 if (type
== MTDSPLIT_PART_TYPE_UBI
)
194 parts
[rf_part
].name
= UBI_PART_NAME
;
196 parts
[rf_part
].name
= ROOTFS_PART_NAME
;
197 parts
[rf_part
].offset
= rootfs_offset
;
198 parts
[rf_part
].size
= rootfs_size
;
213 static ssize_t
uimage_verify_default(u_char
*buf
, size_t len
, int *extralen
)
215 struct uimage_header
*header
= (struct uimage_header
*)buf
;
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
));
224 if (header
->ih_os
!= IH_OS_LINUX
) {
225 pr_debug("invalid uImage OS: %08x\n",
226 be32_to_cpu(header
->ih_os
));
230 if (header
->ih_type
!= IH_TYPE_KERNEL
) {
231 pr_debug("invalid uImage type: %08x\n",
232 be32_to_cpu(header
->ih_type
));
240 mtdsplit_uimage_parse_generic(struct mtd_info
*master
,
241 const struct mtd_partition
**pparts
,
242 struct mtd_part_parser_data
*data
)
244 return __mtdsplit_parse_uimage(master
, pparts
, data
,
245 uimage_verify_default
);
248 static const struct of_device_id mtdsplit_uimage_of_match_table
[] = {
249 { .compatible
= "denx,uimage" },
253 static struct mtd_part_parser uimage_generic_parser
= {
254 .owner
= THIS_MODULE
,
256 .of_match_table
= mtdsplit_uimage_of_match_table
,
257 .parse_fn
= mtdsplit_uimage_parse_generic
,
258 .type
= MTD_PARSER_TYPE_FIRMWARE
,
261 #define FW_MAGIC_GS110TPPV1 0x4e474520
262 #define FW_MAGIC_WNR2000V1 0x32303031
263 #define FW_MAGIC_WNR2000V3 0x32303033
264 #define FW_MAGIC_WNR2000V4 0x32303034
265 #define FW_MAGIC_WNR2200 0x32323030
266 #define FW_MAGIC_WNR612V2 0x32303631
267 #define FW_MAGIC_WNR1000V2 0x31303031
268 #define FW_MAGIC_WNR1000V2_VC 0x31303030
269 #define FW_MAGIC_WNDR3700 0x33373030
270 #define FW_MAGIC_WNDR3700V2 0x33373031
271 #define FW_MAGIC_WPN824N 0x31313030
273 static ssize_t
uimage_verify_wndr3700(u_char
*buf
, size_t len
, int *extralen
)
275 struct uimage_header
*header
= (struct uimage_header
*)buf
;
276 uint8_t expected_type
= IH_TYPE_FILESYSTEM
;
278 switch (be32_to_cpu(header
->ih_magic
)) {
279 case FW_MAGIC_GS110TPPV1
:
280 case FW_MAGIC_WNR2000V4
:
281 expected_type
= IH_TYPE_KERNEL
;
283 case FW_MAGIC_WNR612V2
:
284 case FW_MAGIC_WNR1000V2
:
285 case FW_MAGIC_WNR1000V2_VC
:
286 case FW_MAGIC_WNR2000V1
:
287 case FW_MAGIC_WNR2000V3
:
288 case FW_MAGIC_WNR2200
:
289 case FW_MAGIC_WNDR3700
:
290 case FW_MAGIC_WNDR3700V2
:
291 case FW_MAGIC_WPN824N
:
297 if (header
->ih_os
!= IH_OS_LINUX
||
298 header
->ih_type
!= expected_type
)
305 mtdsplit_uimage_parse_netgear(struct mtd_info
*master
,
306 const struct mtd_partition
**pparts
,
307 struct mtd_part_parser_data
*data
)
309 return __mtdsplit_parse_uimage(master
, pparts
, data
,
310 uimage_verify_wndr3700
);
313 static const struct of_device_id mtdsplit_uimage_netgear_of_match_table
[] = {
314 { .compatible
= "netgear,uimage" },
318 static struct mtd_part_parser uimage_netgear_parser
= {
319 .owner
= THIS_MODULE
,
320 .name
= "netgear-fw",
321 .of_match_table
= mtdsplit_uimage_netgear_of_match_table
,
322 .parse_fn
= mtdsplit_uimage_parse_netgear
,
323 .type
= MTD_PARSER_TYPE_FIRMWARE
,
328 /**************************************************
330 **************************************************/
332 #define FW_MAGIC_SG8208M 0x00000006
333 #define FW_MAGIC_SG8310PM 0x83000006
335 static ssize_t
uimage_verify_allnet(u_char
*buf
, size_t len
, int *extralen
)
337 struct uimage_header
*header
= (struct uimage_header
*)buf
;
339 switch (be32_to_cpu(header
->ih_magic
)) {
340 case FW_MAGIC_SG8208M
:
341 case FW_MAGIC_SG8310PM
:
347 if (header
->ih_os
!= IH_OS_LINUX
)
354 mtdsplit_uimage_parse_allnet(struct mtd_info
*master
,
355 const struct mtd_partition
**pparts
,
356 struct mtd_part_parser_data
*data
)
358 return __mtdsplit_parse_uimage(master
, pparts
, data
,
359 uimage_verify_allnet
);
362 static const struct of_device_id mtdsplit_uimage_allnet_of_match_table
[] = {
363 { .compatible
= "allnet,uimage" },
367 static struct mtd_part_parser uimage_allnet_parser
= {
368 .owner
= THIS_MODULE
,
370 .of_match_table
= mtdsplit_uimage_allnet_of_match_table
,
371 .parse_fn
= mtdsplit_uimage_parse_allnet
,
375 /**************************************************
377 **************************************************/
379 #define FW_EDIMAX_OFFSET 20
380 #define FW_MAGIC_EDIMAX 0x43535953
382 static ssize_t
uimage_find_edimax(u_char
*buf
, size_t len
, int *extralen
)
386 if (len
< FW_EDIMAX_OFFSET
+ sizeof(struct uimage_header
)) {
387 pr_err("Buffer too small for checking Edimax header\n");
392 if (be32_to_cpu(*magic
) != FW_MAGIC_EDIMAX
)
395 if (!uimage_verify_default(buf
+ FW_EDIMAX_OFFSET
, len
, extralen
))
396 return FW_EDIMAX_OFFSET
;
402 mtdsplit_uimage_parse_edimax(struct mtd_info
*master
,
403 const struct mtd_partition
**pparts
,
404 struct mtd_part_parser_data
*data
)
406 return __mtdsplit_parse_uimage(master
, pparts
, data
,
410 static const struct of_device_id mtdsplit_uimage_edimax_of_match_table
[] = {
411 { .compatible
= "edimax,uimage" },
415 static struct mtd_part_parser uimage_edimax_parser
= {
416 .owner
= THIS_MODULE
,
418 .of_match_table
= mtdsplit_uimage_edimax_of_match_table
,
419 .parse_fn
= mtdsplit_uimage_parse_edimax
,
420 .type
= MTD_PARSER_TYPE_FIRMWARE
,
424 /**************************************************
426 **************************************************/
428 #define FONFXC_PAD_LEN 32
430 static ssize_t
uimage_find_fonfxc(u_char
*buf
, size_t len
, int *extralen
)
432 if (uimage_verify_default(buf
, len
, extralen
) < 0)
435 *extralen
= FONFXC_PAD_LEN
;
441 mtdsplit_uimage_parse_fonfxc(struct mtd_info
*master
,
442 const struct mtd_partition
**pparts
,
443 struct mtd_part_parser_data
*data
)
445 return __mtdsplit_parse_uimage(master
, pparts
, data
,
449 static const struct of_device_id mtdsplit_uimage_fonfxc_of_match_table
[] = {
450 { .compatible
= "fonfxc,uimage" },
454 static struct mtd_part_parser uimage_fonfxc_parser
= {
455 .owner
= THIS_MODULE
,
457 .of_match_table
= mtdsplit_uimage_fonfxc_of_match_table
,
458 .parse_fn
= mtdsplit_uimage_parse_fonfxc
,
461 /**************************************************
462 * SGE (T&W) Shenzhen Gongjin Electronics
463 **************************************************/
465 #define SGE_PAD_LEN 96
467 static ssize_t
uimage_find_sge(u_char
*buf
, size_t len
, int *extralen
)
469 if (uimage_verify_default(buf
, len
, extralen
) < 0)
472 *extralen
= SGE_PAD_LEN
;
478 mtdsplit_uimage_parse_sge(struct mtd_info
*master
,
479 const struct mtd_partition
**pparts
,
480 struct mtd_part_parser_data
*data
)
482 return __mtdsplit_parse_uimage(master
, pparts
, data
,
486 static const struct of_device_id mtdsplit_uimage_sge_of_match_table
[] = {
487 { .compatible
= "sge,uimage" },
491 static struct mtd_part_parser uimage_sge_parser
= {
492 .owner
= THIS_MODULE
,
494 .of_match_table
= mtdsplit_uimage_sge_of_match_table
,
495 .parse_fn
= mtdsplit_uimage_parse_sge
,
498 /**************************************************
499 * OKLI (OpenWrt Kernel Loader Image)
500 **************************************************/
502 #define IH_MAGIC_OKLI 0x4f4b4c49
504 static ssize_t
uimage_verify_okli(u_char
*buf
, size_t len
, int *extralen
)
506 struct uimage_header
*header
= (struct uimage_header
*)buf
;
508 /* default sanity checks */
509 if (be32_to_cpu(header
->ih_magic
) != IH_MAGIC_OKLI
) {
510 pr_debug("invalid uImage magic: %08x\n",
511 be32_to_cpu(header
->ih_magic
));
515 if (header
->ih_os
!= IH_OS_LINUX
) {
516 pr_debug("invalid uImage OS: %08x\n",
517 be32_to_cpu(header
->ih_os
));
521 if (header
->ih_type
!= IH_TYPE_KERNEL
) {
522 pr_debug("invalid uImage type: %08x\n",
523 be32_to_cpu(header
->ih_type
));
531 mtdsplit_uimage_parse_okli(struct mtd_info
*master
,
532 const struct mtd_partition
**pparts
,
533 struct mtd_part_parser_data
*data
)
535 return __mtdsplit_parse_uimage(master
, pparts
, data
,
539 static const struct of_device_id mtdsplit_uimage_okli_of_match_table
[] = {
540 { .compatible
= "openwrt,okli" },
544 static struct mtd_part_parser uimage_okli_parser
= {
545 .owner
= THIS_MODULE
,
547 .of_match_table
= mtdsplit_uimage_okli_of_match_table
,
548 .parse_fn
= mtdsplit_uimage_parse_okli
,
551 /**************************************************
553 **************************************************/
555 static int __init
mtdsplit_uimage_init(void)
557 register_mtd_parser(&uimage_generic_parser
);
558 register_mtd_parser(&uimage_netgear_parser
);
559 register_mtd_parser(&uimage_allnet_parser
);
560 register_mtd_parser(&uimage_edimax_parser
);
561 register_mtd_parser(&uimage_fonfxc_parser
);
562 register_mtd_parser(&uimage_sge_parser
);
563 register_mtd_parser(&uimage_okli_parser
);
568 module_init(mtdsplit_uimage_init
);