#include <linux/vmalloc.h>
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
+#include <linux/version.h>
#include <linux/byteorder/generic.h>
+#include <linux/of.h>
+#include <dt-bindings/mtd/partitions/uimage.h>
#include "mtdsplit.h"
-/*
- * uimage_header itself is only 64B, but it may be prepended with another data.
- * Currently the biggest size is for Edimax devices: 20B + 64B
- */
-#define MAX_HEADER_LEN 84
-
-#define IH_MAGIC 0x27051956 /* Image Magic Number */
-#define IH_NMLEN 32 /* Image Name Length */
-
-#define IH_OS_LINUX 5 /* Linux */
-
-#define IH_TYPE_KERNEL 2 /* OS Kernel Image */
-#define IH_TYPE_FILESYSTEM 7 /* Filesystem Image */
-
/*
* Legacy format image header,
* all data in network byte order (aka natural aka bigendian).
return 0;
}
+static void uimage_parse_dt(struct mtd_info *master, int *extralen,
+ u32 *ih_magic, u32 *ih_type,
+ u32 *header_offset, u32 *part_magic)
+{
+ struct device_node *np = mtd_get_of_node(master);
+
+ if (!np || !of_device_is_compatible(np, "openwrt,uimage"))
+ return;
+
+ if (!of_property_read_u32(np, "openwrt,padding", extralen))
+ pr_debug("got openwrt,padding=%d from device-tree\n", *extralen);
+ if (!of_property_read_u32(np, "openwrt,ih-magic", ih_magic))
+ pr_debug("got openwrt,ih-magic=%08x from device-tree\n", *ih_magic);
+ if (!of_property_read_u32(np, "openwrt,ih-type", ih_type))
+ pr_debug("got openwrt,ih-type=%08x from device-tree\n", *ih_type);
+ if (!of_property_read_u32(np, "openwrt,offset", header_offset))
+ pr_debug("got ih-start=%u from device-tree\n", *header_offset);
+ if (!of_property_read_u32(np, "openwrt,partition-magic", part_magic))
+ pr_debug("got openwrt,partition-magic=%08x from device-tree\n", *part_magic);
+}
+
+static ssize_t uimage_verify_default(u_char *buf, u32 ih_magic, u32 ih_type)
+{
+ struct uimage_header *header = (struct uimage_header *)buf;
+
+ /* default sanity checks */
+ if (be32_to_cpu(header->ih_magic) != ih_magic) {
+ pr_debug("invalid uImage magic: %08x != %08x\n",
+ be32_to_cpu(header->ih_magic), ih_magic);
+ return -EINVAL;
+ }
+
+ if (header->ih_os != IH_OS_LINUX) {
+ pr_debug("invalid uImage OS: %08x != %08x\n",
+ be32_to_cpu(header->ih_os), IH_OS_LINUX);
+ return -EINVAL;
+ }
+
+ if (header->ih_type != ih_type) {
+ pr_debug("invalid uImage type: %08x != %08x\n",
+ be32_to_cpu(header->ih_type), ih_type);
+ return -EINVAL;
+ }
+
+ return 0;
+}
+
/**
* __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts
*
* @find_header: function to call for a block of data that will return offset
- * of a valid uImage header if found
+ * and tail padding length of a valid uImage header if found
*/
static int __mtdsplit_parse_uimage(struct mtd_info *master,
- struct mtd_partition **pparts,
- struct mtd_part_parser_data *data,
- ssize_t (*find_header)(u_char *buf, size_t len))
+ const struct mtd_partition **pparts,
+ struct mtd_part_parser_data *data)
{
struct mtd_partition *parts;
u_char *buf;
size_t uimage_size = 0;
size_t rootfs_offset;
size_t rootfs_size = 0;
+ size_t buflen;
int uimage_part, rf_part;
int ret;
+ int extralen = 0;
+ u32 ih_magic = IH_MAGIC;
+ u32 ih_type = IH_TYPE_KERNEL;
+ u32 header_offset = 0;
+ u32 part_magic = 0;
+ enum mtdsplit_part_type type;
nr_parts = 2;
parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
if (!parts)
return -ENOMEM;
- buf = vmalloc(MAX_HEADER_LEN);
+ uimage_parse_dt(master, &extralen, &ih_magic, &ih_type, &header_offset, &part_magic);
+ buflen = sizeof(struct uimage_header) + header_offset;
+ buf = vmalloc(buflen);
if (!buf) {
ret = -ENOMEM;
goto err_free_parts;
uimage_size = 0;
- ret = read_uimage_header(master, offset, buf, MAX_HEADER_LEN);
+ ret = read_uimage_header(master, offset, buf, buflen);
if (ret)
continue;
- ret = find_header(buf, MAX_HEADER_LEN);
+ /* verify optional partition magic before uimage header */
+ if (header_offset && part_magic && (be32_to_cpu(*(u32 *)buf) != part_magic))
+ continue;
+
+ ret = uimage_verify_default(buf + header_offset, ih_magic, ih_type);
if (ret < 0) {
pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
master->name, (unsigned long long) offset);
continue;
}
- header = (struct uimage_header *)(buf + ret);
- uimage_size = sizeof(*header) + be32_to_cpu(header->ih_size);
+ header = (struct uimage_header *)(buf + header_offset);
+
+ uimage_size = sizeof(*header) +
+ be32_to_cpu(header->ih_size) + header_offset + extralen;
+
if ((offset + uimage_size) > master->size) {
pr_debug("uImage exceeds MTD device \"%s\"\n",
master->name);
rf_part = 1;
/* find the roots after the uImage */
- ret = mtd_find_rootfs_from(master,
- uimage_offset + uimage_size,
- master->size,
- &rootfs_offset);
+ ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size,
+ master->size, &rootfs_offset, &type);
if (ret) {
pr_debug("no rootfs after uImage in \"%s\"\n",
master->name);
uimage_part = 1;
/* check rootfs presence at offset 0 */
- ret = mtd_check_rootfs_magic(master, 0);
+ ret = mtd_check_rootfs_magic(master, 0, &type);
if (ret) {
pr_debug("no rootfs before uImage in \"%s\"\n",
master->name);
parts[uimage_part].offset = uimage_offset;
parts[uimage_part].size = uimage_size;
- parts[rf_part].name = ROOTFS_PART_NAME;
+ if (type == MTDSPLIT_PART_TYPE_UBI)
+ parts[rf_part].name = UBI_PART_NAME;
+ else
+ parts[rf_part].name = ROOTFS_PART_NAME;
parts[rf_part].offset = rootfs_offset;
parts[rf_part].size = rootfs_size;
return ret;
}
-static ssize_t uimage_verify_default(u_char *buf, size_t len)
-{
- struct uimage_header *header = (struct uimage_header *)buf;
-
- /* default sanity checks */
- if (be32_to_cpu(header->ih_magic) != IH_MAGIC) {
- pr_debug("invalid uImage magic: %08x\n",
- be32_to_cpu(header->ih_magic));
- return -EINVAL;
- }
-
- if (header->ih_os != IH_OS_LINUX) {
- pr_debug("invalid uImage OS: %08x\n",
- be32_to_cpu(header->ih_os));
- return -EINVAL;
- }
-
- if (header->ih_type != IH_TYPE_KERNEL) {
- pr_debug("invalid uImage type: %08x\n",
- be32_to_cpu(header->ih_type));
- return -EINVAL;
- }
-
- return 0;
-}
-
-static int
-mtdsplit_uimage_parse_generic(struct mtd_info *master,
- struct mtd_partition **pparts,
- struct mtd_part_parser_data *data)
-{
- return __mtdsplit_parse_uimage(master, pparts, data,
- uimage_verify_default);
-}
+static const struct of_device_id mtdsplit_uimage_of_match_table[] = {
+ { .compatible = "denx,uimage" },
+ { .compatible = "openwrt,uimage" },
+ {},
+};
static struct mtd_part_parser uimage_generic_parser = {
.owner = THIS_MODULE,
.name = "uimage-fw",
- .parse_fn = mtdsplit_uimage_parse_generic,
- .type = MTD_PARSER_TYPE_FIRMWARE,
-};
-
-#define FW_MAGIC_WNR2000V3 0x32303033
-#define FW_MAGIC_WNR2000V4 0x32303034
-#define FW_MAGIC_WNR2200 0x32323030
-#define FW_MAGIC_WNR612V2 0x32303631
-#define FW_MAGIC_WNR1000V2 0x31303031
-#define FW_MAGIC_WNR1000V2_VC 0x31303030
-#define FW_MAGIC_WNDR3700 0x33373030
-#define FW_MAGIC_WNDR3700V2 0x33373031
-
-static ssize_t uimage_verify_wndr3700(u_char *buf, size_t len)
-{
- struct uimage_header *header = (struct uimage_header *)buf;
- uint8_t expected_type = IH_TYPE_FILESYSTEM;
-
- switch be32_to_cpu(header->ih_magic) {
- case FW_MAGIC_WNR612V2:
- case FW_MAGIC_WNR1000V2:
- case FW_MAGIC_WNR1000V2_VC:
- case FW_MAGIC_WNR2000V3:
- case FW_MAGIC_WNR2200:
- case FW_MAGIC_WNDR3700:
- case FW_MAGIC_WNDR3700V2:
- break;
- case FW_MAGIC_WNR2000V4:
- expected_type = IH_TYPE_KERNEL;
- break;
- default:
- return -EINVAL;
- }
-
- if (header->ih_os != IH_OS_LINUX ||
- header->ih_type != expected_type)
- return -EINVAL;
-
- return 0;
-}
-
-static int
-mtdsplit_uimage_parse_netgear(struct mtd_info *master,
- struct mtd_partition **pparts,
- struct mtd_part_parser_data *data)
-{
- return __mtdsplit_parse_uimage(master, pparts, data,
- uimage_verify_wndr3700);
-}
-
-static struct mtd_part_parser uimage_netgear_parser = {
- .owner = THIS_MODULE,
- .name = "netgear-fw",
- .parse_fn = mtdsplit_uimage_parse_netgear,
- .type = MTD_PARSER_TYPE_FIRMWARE,
-};
-
-/**************************************************
- * Edimax
- **************************************************/
-
-#define FW_EDIMAX_OFFSET 20
-#define FW_MAGIC_EDIMAX 0x43535953
-
-static ssize_t uimage_find_edimax(u_char *buf, size_t len)
-{
- struct uimage_header *header;
-
- if (len < FW_EDIMAX_OFFSET + sizeof(*header)) {
- pr_err("Buffer too small for checking Edimax header\n");
- return -ENOSPC;
- }
-
- header = (struct uimage_header *)(buf + FW_EDIMAX_OFFSET);
-
- switch be32_to_cpu(header->ih_magic) {
- case FW_MAGIC_EDIMAX:
- break;
- default:
- return -EINVAL;
- }
-
- if (header->ih_os != IH_OS_LINUX ||
- header->ih_type != IH_TYPE_FILESYSTEM)
- return -EINVAL;
-
- return FW_EDIMAX_OFFSET;
-}
-
-static int
-mtdsplit_uimage_parse_edimax(struct mtd_info *master,
- struct mtd_partition **pparts,
- struct mtd_part_parser_data *data)
-{
- return __mtdsplit_parse_uimage(master, pparts, data,
- uimage_find_edimax);
-}
-
-static struct mtd_part_parser uimage_edimax_parser = {
- .owner = THIS_MODULE,
- .name = "edimax-fw",
- .parse_fn = mtdsplit_uimage_parse_edimax,
+ .of_match_table = mtdsplit_uimage_of_match_table,
+ .parse_fn = __mtdsplit_parse_uimage,
.type = MTD_PARSER_TYPE_FIRMWARE,
};
static int __init mtdsplit_uimage_init(void)
{
register_mtd_parser(&uimage_generic_parser);
- register_mtd_parser(&uimage_netgear_parser);
- register_mtd_parser(&uimage_edimax_parser);
return 0;
}