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
,
73 u32
*ih_magic
, u32
*ih_type
)
75 struct device_node
*np
= mtd_get_of_node(master
);
77 if (!np
|| !of_device_is_compatible(np
, "openwrt,uimage"))
80 if (!of_property_read_u32(np
, "openwrt,padding", extralen
))
81 pr_debug("got openwrt,padding=%d from device-tree\n", *extralen
);
82 if (!of_property_read_u32(np
, "openwrt,ih-magic", ih_magic
))
83 pr_debug("got openwrt,ih-magic=%08x from device-tree\n", *ih_magic
);
84 if (!of_property_read_u32(np
, "openwrt,ih-type", ih_type
))
85 pr_debug("got openwrt,ih-type=%08x from device-tree\n", *ih_type
);
89 * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
91 * @find_header: function to call for a block of data that will return offset
92 * and tail padding length of a valid uImage header if found
94 static int __mtdsplit_parse_uimage(struct mtd_info
*master
,
95 const struct mtd_partition
**pparts
,
96 struct mtd_part_parser_data
*data
,
97 ssize_t (*find_header
)(u_char
*buf
, size_t len
, u32 ih_magic
, u32 ih_type
))
99 struct mtd_partition
*parts
;
103 size_t uimage_offset
;
104 size_t uimage_size
= 0;
105 size_t rootfs_offset
;
106 size_t rootfs_size
= 0;
107 int uimage_part
, rf_part
;
110 u32 ih_magic
= IH_MAGIC
;
111 u32 ih_type
= IH_TYPE_KERNEL
;
112 enum mtdsplit_part_type type
;
115 parts
= kzalloc(nr_parts
* sizeof(*parts
), GFP_KERNEL
);
119 buf
= vmalloc(MAX_HEADER_LEN
);
125 uimage_parse_dt(master
, &extralen
, &ih_magic
, &ih_type
);
127 /* find uImage on erase block boundaries */
128 for (offset
= 0; offset
< master
->size
; offset
+= master
->erasesize
) {
129 struct uimage_header
*header
;
133 ret
= read_uimage_header(master
, offset
, buf
, MAX_HEADER_LEN
);
137 ret
= find_header(buf
, MAX_HEADER_LEN
, ih_magic
, ih_type
);
139 pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
140 master
->name
, (unsigned long long) offset
);
143 header
= (struct uimage_header
*)(buf
+ ret
);
145 uimage_size
= sizeof(*header
) +
146 be32_to_cpu(header
->ih_size
) + ret
+ extralen
;
148 if ((offset
+ uimage_size
) > master
->size
) {
149 pr_debug("uImage exceeds MTD device \"%s\"\n",
156 if (uimage_size
== 0) {
157 pr_debug("no uImage found in \"%s\"\n", master
->name
);
162 uimage_offset
= offset
;
164 if (uimage_offset
== 0) {
168 /* find the roots after the uImage */
169 ret
= mtd_find_rootfs_from(master
, uimage_offset
+ uimage_size
,
170 master
->size
, &rootfs_offset
, &type
);
172 pr_debug("no rootfs after uImage in \"%s\"\n",
177 rootfs_size
= master
->size
- rootfs_offset
;
178 uimage_size
= rootfs_offset
- uimage_offset
;
183 /* check rootfs presence at offset 0 */
184 ret
= mtd_check_rootfs_magic(master
, 0, &type
);
186 pr_debug("no rootfs before uImage in \"%s\"\n",
192 rootfs_size
= uimage_offset
;
195 if (rootfs_size
== 0) {
196 pr_debug("no rootfs found in \"%s\"\n", master
->name
);
201 parts
[uimage_part
].name
= KERNEL_PART_NAME
;
202 parts
[uimage_part
].offset
= uimage_offset
;
203 parts
[uimage_part
].size
= uimage_size
;
205 if (type
== MTDSPLIT_PART_TYPE_UBI
)
206 parts
[rf_part
].name
= UBI_PART_NAME
;
208 parts
[rf_part
].name
= ROOTFS_PART_NAME
;
209 parts
[rf_part
].offset
= rootfs_offset
;
210 parts
[rf_part
].size
= rootfs_size
;
225 static ssize_t
uimage_verify_default(u_char
*buf
, size_t len
, u32 ih_magic
, u32 ih_type
)
227 struct uimage_header
*header
= (struct uimage_header
*)buf
;
229 /* default sanity checks */
230 if (be32_to_cpu(header
->ih_magic
) != ih_magic
) {
231 pr_debug("invalid uImage magic: %08x != %08x\n",
232 be32_to_cpu(header
->ih_magic
), ih_magic
);
236 if (header
->ih_os
!= IH_OS_LINUX
) {
237 pr_debug("invalid uImage OS: %08x != %08x\n",
238 be32_to_cpu(header
->ih_os
), IH_OS_LINUX
);
242 if (header
->ih_type
!= ih_type
) {
243 pr_debug("invalid uImage type: %08x != %08x\n",
244 be32_to_cpu(header
->ih_type
), ih_type
);
252 mtdsplit_uimage_parse_generic(struct mtd_info
*master
,
253 const struct mtd_partition
**pparts
,
254 struct mtd_part_parser_data
*data
)
256 return __mtdsplit_parse_uimage(master
, pparts
, data
,
257 uimage_verify_default
);
260 static const struct of_device_id mtdsplit_uimage_of_match_table
[] = {
261 { .compatible
= "denx,uimage" },
262 { .compatible
= "openwrt,uimage" },
266 static struct mtd_part_parser uimage_generic_parser
= {
267 .owner
= THIS_MODULE
,
269 .of_match_table
= mtdsplit_uimage_of_match_table
,
270 .parse_fn
= mtdsplit_uimage_parse_generic
,
271 .type
= MTD_PARSER_TYPE_FIRMWARE
,
274 /**************************************************
276 **************************************************/
278 #define FW_EDIMAX_OFFSET 20
279 #define FW_MAGIC_EDIMAX 0x43535953
281 static ssize_t
uimage_find_edimax(u_char
*buf
, size_t len
, u32 ih_magic
, u32 ih_type
)
285 if (len
< FW_EDIMAX_OFFSET
+ sizeof(struct uimage_header
)) {
286 pr_err("Buffer too small for checking Edimax header\n");
291 if (be32_to_cpu(*magic
) != FW_MAGIC_EDIMAX
)
294 if (!uimage_verify_default(buf
+ FW_EDIMAX_OFFSET
, len
, ih_magic
, ih_type
))
295 return FW_EDIMAX_OFFSET
;
301 mtdsplit_uimage_parse_edimax(struct mtd_info
*master
,
302 const struct mtd_partition
**pparts
,
303 struct mtd_part_parser_data
*data
)
305 return __mtdsplit_parse_uimage(master
, pparts
, data
,
309 static const struct of_device_id mtdsplit_uimage_edimax_of_match_table
[] = {
310 { .compatible
= "edimax,uimage" },
314 static struct mtd_part_parser uimage_edimax_parser
= {
315 .owner
= THIS_MODULE
,
317 .of_match_table
= mtdsplit_uimage_edimax_of_match_table
,
318 .parse_fn
= mtdsplit_uimage_parse_edimax
,
319 .type
= MTD_PARSER_TYPE_FIRMWARE
,
323 /**************************************************
325 **************************************************/
327 static int __init
mtdsplit_uimage_init(void)
329 register_mtd_parser(&uimage_generic_parser
);
330 register_mtd_parser(&uimage_edimax_parser
);
335 module_init(mtdsplit_uimage_init
);