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
, u32
*ih_magic
)
74 struct device_node
*np
= mtd_get_of_node(master
);
76 if (!np
|| !of_device_is_compatible(np
, "openwrt,uimage"))
79 if (!of_property_read_u32(np
, "openwrt,padding", extralen
))
80 pr_debug("got openwrt,padding=%d from device-tree\n", *extralen
);
81 if (!of_property_read_u32(np
, "openwrt,ih-magic", ih_magic
))
82 pr_debug("got openwrt,ih-magic=%08x from device-tree\n", *ih_magic
);
86 * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
88 * @find_header: function to call for a block of data that will return offset
89 * and tail padding length of a valid uImage header if found
91 static int __mtdsplit_parse_uimage(struct mtd_info
*master
,
92 const struct mtd_partition
**pparts
,
93 struct mtd_part_parser_data
*data
,
94 ssize_t (*find_header
)(u_char
*buf
, size_t len
, u32 ih_magic
))
96 struct mtd_partition
*parts
;
100 size_t uimage_offset
;
101 size_t uimage_size
= 0;
102 size_t rootfs_offset
;
103 size_t rootfs_size
= 0;
104 int uimage_part
, rf_part
;
107 u32 ih_magic
= IH_MAGIC
;
108 enum mtdsplit_part_type type
;
111 parts
= kzalloc(nr_parts
* sizeof(*parts
), GFP_KERNEL
);
115 buf
= vmalloc(MAX_HEADER_LEN
);
121 uimage_parse_dt(master
, &extralen
, &ih_magic
);
123 /* find uImage on erase block boundaries */
124 for (offset
= 0; offset
< master
->size
; offset
+= master
->erasesize
) {
125 struct uimage_header
*header
;
129 ret
= read_uimage_header(master
, offset
, buf
, MAX_HEADER_LEN
);
133 ret
= find_header(buf
, MAX_HEADER_LEN
, ih_magic
);
135 pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
136 master
->name
, (unsigned long long) offset
);
139 header
= (struct uimage_header
*)(buf
+ ret
);
141 uimage_size
= sizeof(*header
) +
142 be32_to_cpu(header
->ih_size
) + ret
+ extralen
;
144 if ((offset
+ uimage_size
) > master
->size
) {
145 pr_debug("uImage exceeds MTD device \"%s\"\n",
152 if (uimage_size
== 0) {
153 pr_debug("no uImage found in \"%s\"\n", master
->name
);
158 uimage_offset
= offset
;
160 if (uimage_offset
== 0) {
164 /* find the roots after the uImage */
165 ret
= mtd_find_rootfs_from(master
, uimage_offset
+ uimage_size
,
166 master
->size
, &rootfs_offset
, &type
);
168 pr_debug("no rootfs after uImage in \"%s\"\n",
173 rootfs_size
= master
->size
- rootfs_offset
;
174 uimage_size
= rootfs_offset
- uimage_offset
;
179 /* check rootfs presence at offset 0 */
180 ret
= mtd_check_rootfs_magic(master
, 0, &type
);
182 pr_debug("no rootfs before uImage in \"%s\"\n",
188 rootfs_size
= uimage_offset
;
191 if (rootfs_size
== 0) {
192 pr_debug("no rootfs found in \"%s\"\n", master
->name
);
197 parts
[uimage_part
].name
= KERNEL_PART_NAME
;
198 parts
[uimage_part
].offset
= uimage_offset
;
199 parts
[uimage_part
].size
= uimage_size
;
201 if (type
== MTDSPLIT_PART_TYPE_UBI
)
202 parts
[rf_part
].name
= UBI_PART_NAME
;
204 parts
[rf_part
].name
= ROOTFS_PART_NAME
;
205 parts
[rf_part
].offset
= rootfs_offset
;
206 parts
[rf_part
].size
= rootfs_size
;
221 static ssize_t
uimage_verify_default(u_char
*buf
, size_t len
, u32 ih_magic
)
223 struct uimage_header
*header
= (struct uimage_header
*)buf
;
225 /* default sanity checks */
226 if (be32_to_cpu(header
->ih_magic
) != ih_magic
) {
227 pr_debug("invalid uImage magic: %08x != %08x\n",
228 be32_to_cpu(header
->ih_magic
), ih_magic
);
232 if (header
->ih_os
!= IH_OS_LINUX
) {
233 pr_debug("invalid uImage OS: %08x != %08x\n",
234 be32_to_cpu(header
->ih_os
), IH_OS_LINUX
);
238 if (header
->ih_type
!= IH_TYPE_KERNEL
) {
239 pr_debug("invalid uImage type: %08x != %08x\n",
240 be32_to_cpu(header
->ih_type
), IH_TYPE_KERNEL
);
248 mtdsplit_uimage_parse_generic(struct mtd_info
*master
,
249 const struct mtd_partition
**pparts
,
250 struct mtd_part_parser_data
*data
)
252 return __mtdsplit_parse_uimage(master
, pparts
, data
,
253 uimage_verify_default
);
256 static const struct of_device_id mtdsplit_uimage_of_match_table
[] = {
257 { .compatible
= "denx,uimage" },
258 { .compatible
= "openwrt,uimage" },
262 static struct mtd_part_parser uimage_generic_parser
= {
263 .owner
= THIS_MODULE
,
265 .of_match_table
= mtdsplit_uimage_of_match_table
,
266 .parse_fn
= mtdsplit_uimage_parse_generic
,
267 .type
= MTD_PARSER_TYPE_FIRMWARE
,
270 #define FW_MAGIC_GS110TPPV1 0x4e474520
271 #define FW_MAGIC_WNR2000V1 0x32303031
272 #define FW_MAGIC_WNR2000V3 0x32303033
273 #define FW_MAGIC_WNR2000V4 0x32303034
274 #define FW_MAGIC_WNR2200 0x32323030
275 #define FW_MAGIC_WNR612V2 0x32303631
276 #define FW_MAGIC_WNR1000V2 0x31303031
277 #define FW_MAGIC_WNR1000V2_VC 0x31303030
278 #define FW_MAGIC_WNDR3700 0x33373030
279 #define FW_MAGIC_WNDR3700V2 0x33373031
280 #define FW_MAGIC_WPN824N 0x31313030
282 static ssize_t
uimage_verify_wndr3700(u_char
*buf
, size_t len
, u32 ih_magic
)
284 struct uimage_header
*header
= (struct uimage_header
*)buf
;
285 uint8_t expected_type
= IH_TYPE_FILESYSTEM
;
287 switch (be32_to_cpu(header
->ih_magic
)) {
288 case FW_MAGIC_GS110TPPV1
:
289 case FW_MAGIC_WNR2000V4
:
290 expected_type
= IH_TYPE_KERNEL
;
292 case FW_MAGIC_WNR612V2
:
293 case FW_MAGIC_WNR1000V2
:
294 case FW_MAGIC_WNR1000V2_VC
:
295 case FW_MAGIC_WNR2000V1
:
296 case FW_MAGIC_WNR2000V3
:
297 case FW_MAGIC_WNR2200
:
298 case FW_MAGIC_WNDR3700
:
299 case FW_MAGIC_WNDR3700V2
:
300 case FW_MAGIC_WPN824N
:
306 if (header
->ih_os
!= IH_OS_LINUX
||
307 header
->ih_type
!= expected_type
)
314 mtdsplit_uimage_parse_netgear(struct mtd_info
*master
,
315 const struct mtd_partition
**pparts
,
316 struct mtd_part_parser_data
*data
)
318 return __mtdsplit_parse_uimage(master
, pparts
, data
,
319 uimage_verify_wndr3700
);
322 static const struct of_device_id mtdsplit_uimage_netgear_of_match_table
[] = {
323 { .compatible
= "netgear,uimage" },
327 static struct mtd_part_parser uimage_netgear_parser
= {
328 .owner
= THIS_MODULE
,
329 .name
= "netgear-fw",
330 .of_match_table
= mtdsplit_uimage_netgear_of_match_table
,
331 .parse_fn
= mtdsplit_uimage_parse_netgear
,
332 .type
= MTD_PARSER_TYPE_FIRMWARE
,
337 /**************************************************
339 **************************************************/
341 #define FW_MAGIC_SG8208M 0x00000006
342 #define FW_MAGIC_SG8310PM 0x83000006
344 static ssize_t
uimage_verify_allnet(u_char
*buf
, size_t len
, u32 ih_magic
)
346 struct uimage_header
*header
= (struct uimage_header
*)buf
;
348 switch (be32_to_cpu(header
->ih_magic
)) {
349 case FW_MAGIC_SG8208M
:
350 case FW_MAGIC_SG8310PM
:
356 if (header
->ih_os
!= IH_OS_LINUX
)
363 mtdsplit_uimage_parse_allnet(struct mtd_info
*master
,
364 const struct mtd_partition
**pparts
,
365 struct mtd_part_parser_data
*data
)
367 return __mtdsplit_parse_uimage(master
, pparts
, data
,
368 uimage_verify_allnet
);
371 static const struct of_device_id mtdsplit_uimage_allnet_of_match_table
[] = {
372 { .compatible
= "allnet,uimage" },
376 static struct mtd_part_parser uimage_allnet_parser
= {
377 .owner
= THIS_MODULE
,
379 .of_match_table
= mtdsplit_uimage_allnet_of_match_table
,
380 .parse_fn
= mtdsplit_uimage_parse_allnet
,
384 /**************************************************
386 **************************************************/
388 #define FW_EDIMAX_OFFSET 20
389 #define FW_MAGIC_EDIMAX 0x43535953
391 static ssize_t
uimage_find_edimax(u_char
*buf
, size_t len
, u32 ih_magic
)
395 if (len
< FW_EDIMAX_OFFSET
+ sizeof(struct uimage_header
)) {
396 pr_err("Buffer too small for checking Edimax header\n");
401 if (be32_to_cpu(*magic
) != FW_MAGIC_EDIMAX
)
404 if (!uimage_verify_default(buf
+ FW_EDIMAX_OFFSET
, len
, ih_magic
))
405 return FW_EDIMAX_OFFSET
;
411 mtdsplit_uimage_parse_edimax(struct mtd_info
*master
,
412 const struct mtd_partition
**pparts
,
413 struct mtd_part_parser_data
*data
)
415 return __mtdsplit_parse_uimage(master
, pparts
, data
,
419 static const struct of_device_id mtdsplit_uimage_edimax_of_match_table
[] = {
420 { .compatible
= "edimax,uimage" },
424 static struct mtd_part_parser uimage_edimax_parser
= {
425 .owner
= THIS_MODULE
,
427 .of_match_table
= mtdsplit_uimage_edimax_of_match_table
,
428 .parse_fn
= mtdsplit_uimage_parse_edimax
,
429 .type
= MTD_PARSER_TYPE_FIRMWARE
,
432 /**************************************************
433 * OKLI (OpenWrt Kernel Loader Image)
434 **************************************************/
436 #define IH_MAGIC_OKLI 0x4f4b4c49
438 static ssize_t
uimage_verify_okli(u_char
*buf
, size_t len
, u32 ih_magic
)
440 struct uimage_header
*header
= (struct uimage_header
*)buf
;
442 /* default sanity checks */
443 if (be32_to_cpu(header
->ih_magic
) != IH_MAGIC_OKLI
) {
444 pr_debug("invalid uImage magic: %08x\n",
445 be32_to_cpu(header
->ih_magic
));
449 if (header
->ih_os
!= IH_OS_LINUX
) {
450 pr_debug("invalid uImage OS: %08x\n",
451 be32_to_cpu(header
->ih_os
));
455 if (header
->ih_type
!= IH_TYPE_KERNEL
) {
456 pr_debug("invalid uImage type: %08x\n",
457 be32_to_cpu(header
->ih_type
));
465 mtdsplit_uimage_parse_okli(struct mtd_info
*master
,
466 const struct mtd_partition
**pparts
,
467 struct mtd_part_parser_data
*data
)
469 return __mtdsplit_parse_uimage(master
, pparts
, data
,
473 static const struct of_device_id mtdsplit_uimage_okli_of_match_table
[] = {
474 { .compatible
= "openwrt,okli" },
478 static struct mtd_part_parser uimage_okli_parser
= {
479 .owner
= THIS_MODULE
,
481 .of_match_table
= mtdsplit_uimage_okli_of_match_table
,
482 .parse_fn
= mtdsplit_uimage_parse_okli
,
485 /**************************************************
487 **************************************************/
489 static int __init
mtdsplit_uimage_init(void)
491 register_mtd_parser(&uimage_generic_parser
);
492 register_mtd_parser(&uimage_netgear_parser
);
493 register_mtd_parser(&uimage_allnet_parser
);
494 register_mtd_parser(&uimage_edimax_parser
);
495 register_mtd_parser(&uimage_okli_parser
);
500 module_init(mtdsplit_uimage_init
);