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>
22 #include <dt-bindings/mtd/partitions/uimage.h>
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
30 #define MAX_HEADER_LEN 96
33 * Legacy format image header,
34 * all data in network byte order (aka natural aka bigendian).
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 */
52 read_uimage_header(struct mtd_info
*mtd
, size_t offset
, u_char
*buf
,
58 ret
= mtd_read(mtd
, offset
, header_len
, &retlen
, buf
);
60 pr_debug("read error in \"%s\"\n", mtd
->name
);
64 if (retlen
!= header_len
) {
65 pr_debug("short read in \"%s\"\n", mtd
->name
);
72 static void uimage_parse_dt(struct mtd_info
*master
, int *extralen
)
74 struct device_node
*np
= mtd_get_of_node(master
);
76 if (!np
|| !of_device_is_compatible(np
, "openwrt,uimage"))
78 of_property_read_u32(np
, "openwrt,padding", extralen
);
82 * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
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
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
))
92 struct mtd_partition
*parts
;
97 size_t uimage_size
= 0;
99 size_t rootfs_size
= 0;
100 int uimage_part
, rf_part
;
103 enum mtdsplit_part_type type
;
106 parts
= kzalloc(nr_parts
* sizeof(*parts
), GFP_KERNEL
);
110 buf
= vmalloc(MAX_HEADER_LEN
);
116 uimage_parse_dt(master
, &extralen
);
118 /* find uImage on erase block boundaries */
119 for (offset
= 0; offset
< master
->size
; offset
+= master
->erasesize
) {
120 struct uimage_header
*header
;
124 ret
= read_uimage_header(master
, offset
, buf
, MAX_HEADER_LEN
);
128 ret
= find_header(buf
, MAX_HEADER_LEN
);
130 pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
131 master
->name
, (unsigned long long) offset
);
134 header
= (struct uimage_header
*)(buf
+ ret
);
136 uimage_size
= sizeof(*header
) +
137 be32_to_cpu(header
->ih_size
) + ret
+ extralen
;
139 if ((offset
+ uimage_size
) > master
->size
) {
140 pr_debug("uImage exceeds MTD device \"%s\"\n",
147 if (uimage_size
== 0) {
148 pr_debug("no uImage found in \"%s\"\n", master
->name
);
153 uimage_offset
= offset
;
155 if (uimage_offset
== 0) {
159 /* find the roots after the uImage */
160 ret
= mtd_find_rootfs_from(master
, uimage_offset
+ uimage_size
,
161 master
->size
, &rootfs_offset
, &type
);
163 pr_debug("no rootfs after uImage in \"%s\"\n",
168 rootfs_size
= master
->size
- rootfs_offset
;
169 uimage_size
= rootfs_offset
- uimage_offset
;
174 /* check rootfs presence at offset 0 */
175 ret
= mtd_check_rootfs_magic(master
, 0, &type
);
177 pr_debug("no rootfs before uImage in \"%s\"\n",
183 rootfs_size
= uimage_offset
;
186 if (rootfs_size
== 0) {
187 pr_debug("no rootfs found in \"%s\"\n", master
->name
);
192 parts
[uimage_part
].name
= KERNEL_PART_NAME
;
193 parts
[uimage_part
].offset
= uimage_offset
;
194 parts
[uimage_part
].size
= uimage_size
;
196 if (type
== MTDSPLIT_PART_TYPE_UBI
)
197 parts
[rf_part
].name
= UBI_PART_NAME
;
199 parts
[rf_part
].name
= ROOTFS_PART_NAME
;
200 parts
[rf_part
].offset
= rootfs_offset
;
201 parts
[rf_part
].size
= rootfs_size
;
216 static ssize_t
uimage_verify_default(u_char
*buf
, size_t len
)
218 struct uimage_header
*header
= (struct uimage_header
*)buf
;
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
));
227 if (header
->ih_os
!= IH_OS_LINUX
) {
228 pr_debug("invalid uImage OS: %08x\n",
229 be32_to_cpu(header
->ih_os
));
233 if (header
->ih_type
!= IH_TYPE_KERNEL
) {
234 pr_debug("invalid uImage type: %08x\n",
235 be32_to_cpu(header
->ih_type
));
243 mtdsplit_uimage_parse_generic(struct mtd_info
*master
,
244 const struct mtd_partition
**pparts
,
245 struct mtd_part_parser_data
*data
)
247 return __mtdsplit_parse_uimage(master
, pparts
, data
,
248 uimage_verify_default
);
251 static const struct of_device_id mtdsplit_uimage_of_match_table
[] = {
252 { .compatible
= "denx,uimage" },
253 { .compatible
= "openwrt,uimage" },
257 static struct mtd_part_parser uimage_generic_parser
= {
258 .owner
= THIS_MODULE
,
260 .of_match_table
= mtdsplit_uimage_of_match_table
,
261 .parse_fn
= mtdsplit_uimage_parse_generic
,
262 .type
= MTD_PARSER_TYPE_FIRMWARE
,
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
277 static ssize_t
uimage_verify_wndr3700(u_char
*buf
, size_t len
)
279 struct uimage_header
*header
= (struct uimage_header
*)buf
;
280 uint8_t expected_type
= IH_TYPE_FILESYSTEM
;
282 switch (be32_to_cpu(header
->ih_magic
)) {
283 case FW_MAGIC_GS110TPPV1
:
284 case FW_MAGIC_WNR2000V4
:
285 expected_type
= IH_TYPE_KERNEL
;
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
:
301 if (header
->ih_os
!= IH_OS_LINUX
||
302 header
->ih_type
!= expected_type
)
309 mtdsplit_uimage_parse_netgear(struct mtd_info
*master
,
310 const struct mtd_partition
**pparts
,
311 struct mtd_part_parser_data
*data
)
313 return __mtdsplit_parse_uimage(master
, pparts
, data
,
314 uimage_verify_wndr3700
);
317 static const struct of_device_id mtdsplit_uimage_netgear_of_match_table
[] = {
318 { .compatible
= "netgear,uimage" },
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
,
332 /**************************************************
334 **************************************************/
336 #define FW_MAGIC_SG8208M 0x00000006
337 #define FW_MAGIC_SG8310PM 0x83000006
339 static ssize_t
uimage_verify_allnet(u_char
*buf
, size_t len
)
341 struct uimage_header
*header
= (struct uimage_header
*)buf
;
343 switch (be32_to_cpu(header
->ih_magic
)) {
344 case FW_MAGIC_SG8208M
:
345 case FW_MAGIC_SG8310PM
:
351 if (header
->ih_os
!= IH_OS_LINUX
)
358 mtdsplit_uimage_parse_allnet(struct mtd_info
*master
,
359 const struct mtd_partition
**pparts
,
360 struct mtd_part_parser_data
*data
)
362 return __mtdsplit_parse_uimage(master
, pparts
, data
,
363 uimage_verify_allnet
);
366 static const struct of_device_id mtdsplit_uimage_allnet_of_match_table
[] = {
367 { .compatible
= "allnet,uimage" },
371 static struct mtd_part_parser uimage_allnet_parser
= {
372 .owner
= THIS_MODULE
,
374 .of_match_table
= mtdsplit_uimage_allnet_of_match_table
,
375 .parse_fn
= mtdsplit_uimage_parse_allnet
,
379 /**************************************************
381 **************************************************/
383 #define FW_EDIMAX_OFFSET 20
384 #define FW_MAGIC_EDIMAX 0x43535953
386 static ssize_t
uimage_find_edimax(u_char
*buf
, size_t len
)
390 if (len
< FW_EDIMAX_OFFSET
+ sizeof(struct uimage_header
)) {
391 pr_err("Buffer too small for checking Edimax header\n");
396 if (be32_to_cpu(*magic
) != FW_MAGIC_EDIMAX
)
399 if (!uimage_verify_default(buf
+ FW_EDIMAX_OFFSET
, len
))
400 return FW_EDIMAX_OFFSET
;
406 mtdsplit_uimage_parse_edimax(struct mtd_info
*master
,
407 const struct mtd_partition
**pparts
,
408 struct mtd_part_parser_data
*data
)
410 return __mtdsplit_parse_uimage(master
, pparts
, data
,
414 static const struct of_device_id mtdsplit_uimage_edimax_of_match_table
[] = {
415 { .compatible
= "edimax,uimage" },
419 static struct mtd_part_parser uimage_edimax_parser
= {
420 .owner
= THIS_MODULE
,
422 .of_match_table
= mtdsplit_uimage_edimax_of_match_table
,
423 .parse_fn
= mtdsplit_uimage_parse_edimax
,
424 .type
= MTD_PARSER_TYPE_FIRMWARE
,
427 /**************************************************
428 * OKLI (OpenWrt Kernel Loader Image)
429 **************************************************/
431 #define IH_MAGIC_OKLI 0x4f4b4c49
433 static ssize_t
uimage_verify_okli(u_char
*buf
, size_t len
)
435 struct uimage_header
*header
= (struct uimage_header
*)buf
;
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
));
444 if (header
->ih_os
!= IH_OS_LINUX
) {
445 pr_debug("invalid uImage OS: %08x\n",
446 be32_to_cpu(header
->ih_os
));
450 if (header
->ih_type
!= IH_TYPE_KERNEL
) {
451 pr_debug("invalid uImage type: %08x\n",
452 be32_to_cpu(header
->ih_type
));
460 mtdsplit_uimage_parse_okli(struct mtd_info
*master
,
461 const struct mtd_partition
**pparts
,
462 struct mtd_part_parser_data
*data
)
464 return __mtdsplit_parse_uimage(master
, pparts
, data
,
468 static const struct of_device_id mtdsplit_uimage_okli_of_match_table
[] = {
469 { .compatible
= "openwrt,okli" },
473 static struct mtd_part_parser uimage_okli_parser
= {
474 .owner
= THIS_MODULE
,
476 .of_match_table
= mtdsplit_uimage_okli_of_match_table
,
477 .parse_fn
= mtdsplit_uimage_parse_okli
,
480 /**************************************************
482 **************************************************/
484 static int __init
mtdsplit_uimage_init(void)
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
);
495 module_init(mtdsplit_uimage_init
);