kernel: Add missing includes mtdsplit_*.c
[openwrt/staging/blogic.git] / target / linux / generic / files / drivers / mtd / mtdsplit / mtdsplit_uimage.c
index 04a0eb47d7daa5450d217e0b02b47f2a70cc0b2f..aba1c83ad65cdf6a56c1683366454367c16f3fa0 100644 (file)
 #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 "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            */
 
@@ -48,16 +56,13 @@ struct uimage_header {
 };
 
 static int
-read_uimage_header(struct mtd_info *mtd, size_t offset,
-                  struct uimage_header *header)
+read_uimage_header(struct mtd_info *mtd, size_t offset, u_char *buf,
+                  size_t header_len)
 {
-       size_t header_len;
        size_t retlen;
        int ret;
 
-       header_len = sizeof(*header);
-       ret = mtd_read(mtd, offset, header_len, &retlen,
-                      (unsigned char *) header);
+       ret = mtd_read(mtd, offset, header_len, &retlen, buf);
        if (ret) {
                pr_debug("read error in \"%s\"\n", mtd->name);
                return ret;
@@ -78,12 +83,12 @@ read_uimage_header(struct mtd_info *mtd, size_t offset,
  *      of a valid uImage header if found
  */
 static int __mtdsplit_parse_uimage(struct mtd_info *master,
-                                  struct mtd_partition **pparts,
+                                  const struct mtd_partition **pparts,
                                   struct mtd_part_parser_data *data,
                                   ssize_t (*find_header)(u_char *buf, size_t len))
 {
        struct mtd_partition *parts;
-       struct uimage_header *header;
+       u_char *buf;
        int nr_parts;
        size_t offset;
        size_t uimage_offset;
@@ -92,38 +97,38 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
        size_t rootfs_size = 0;
        int uimage_part, rf_part;
        int ret;
+       enum mtdsplit_part_type type;
 
        nr_parts = 2;
        parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL);
        if (!parts)
                return -ENOMEM;
 
-       header = vmalloc(sizeof(*header));
-       if (!header) {
+       buf = vmalloc(MAX_HEADER_LEN);
+       if (!buf) {
                ret = -ENOMEM;
                goto err_free_parts;
        }
 
        /* find uImage on erase block boundaries */
        for (offset = 0; offset < master->size; offset += master->erasesize) {
+               struct uimage_header *header;
+
                uimage_size = 0;
 
-               ret = read_uimage_header(master, offset, header);
+               ret = read_uimage_header(master, offset, buf, MAX_HEADER_LEN);
                if (ret)
                        continue;
 
-               ret = find_header((u_char *)header, sizeof(*header));
+               ret = find_header(buf, MAX_HEADER_LEN);
                if (ret < 0) {
                        pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
                                 master->name, (unsigned long long) offset);
                        continue;
                }
-               if (ret > 0) {
-                       pr_warn("extra header offsets are not supported yet\n");
-                       continue;
-               }
+               header = (struct uimage_header *)(buf + ret);
 
-               uimage_size = sizeof(*header) + be32_to_cpu(header->ih_size);
+               uimage_size = sizeof(*header) + be32_to_cpu(header->ih_size) + ret;
                if ((offset + uimage_size) > master->size) {
                        pr_debug("uImage exceeds MTD device \"%s\"\n",
                                 master->name);
@@ -135,7 +140,7 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
        if (uimage_size == 0) {
                pr_debug("no uImage found in \"%s\"\n", master->name);
                ret = -ENODEV;
-               goto err_free_header;
+               goto err_free_buf;
        }
 
        uimage_offset = offset;
@@ -145,14 +150,12 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
                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);
-                       goto err_free_header;
+                       goto err_free_buf;
                }
 
                rootfs_size = master->size - rootfs_offset;
@@ -162,11 +165,11 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
                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);
-                       goto err_free_header;
+                       goto err_free_buf;
                }
 
                rootfs_offset = 0;
@@ -176,24 +179,27 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
        if (rootfs_size == 0) {
                pr_debug("no rootfs found in \"%s\"\n", master->name);
                ret = -ENODEV;
-               goto err_free_header;
+               goto err_free_buf;
        }
 
        parts[uimage_part].name = KERNEL_PART_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;
 
-       vfree(header);
+       vfree(buf);
 
        *pparts = parts;
        return nr_parts;
 
-err_free_header:
-       vfree(header);
+err_free_buf:
+       vfree(buf);
 
 err_free_parts:
        kfree(parts);
@@ -228,20 +234,31 @@ static ssize_t uimage_verify_default(u_char *buf, size_t len)
 
 static int
 mtdsplit_uimage_parse_generic(struct mtd_info *master,
-                             struct mtd_partition **pparts,
+                             const struct mtd_partition **pparts,
                              struct mtd_part_parser_data *data)
 {
        return __mtdsplit_parse_uimage(master, pparts, data,
                                      uimage_verify_default);
 }
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
+static const struct of_device_id mtdsplit_uimage_of_match_table[] = {
+       { .compatible = "denx,uimage" },
+       {},
+};
+#endif
+
 static struct mtd_part_parser uimage_generic_parser = {
        .owner = THIS_MODULE,
        .name = "uimage-fw",
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
+       .of_match_table = mtdsplit_uimage_of_match_table,
+#endif
        .parse_fn = mtdsplit_uimage_parse_generic,
        .type = MTD_PARSER_TYPE_FIRMWARE,
 };
 
+#define FW_MAGIC_WNR2000V1     0x32303031
 #define FW_MAGIC_WNR2000V3     0x32303033
 #define FW_MAGIC_WNR2000V4     0x32303034
 #define FW_MAGIC_WNR2200       0x32323030
@@ -250,20 +267,23 @@ static struct mtd_part_parser uimage_generic_parser = {
 #define FW_MAGIC_WNR1000V2_VC  0x31303030
 #define FW_MAGIC_WNDR3700      0x33373030
 #define FW_MAGIC_WNDR3700V2    0x33373031
+#define FW_MAGIC_WPN824N       0x31313030
 
 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) {
+       switch (be32_to_cpu(header->ih_magic)) {
        case FW_MAGIC_WNR612V2:
        case FW_MAGIC_WNR1000V2:
        case FW_MAGIC_WNR1000V2_VC:
+       case FW_MAGIC_WNR2000V1:
        case FW_MAGIC_WNR2000V3:
        case FW_MAGIC_WNR2200:
        case FW_MAGIC_WNDR3700:
        case FW_MAGIC_WNDR3700V2:
+       case FW_MAGIC_WPN824N:
                break;
        case FW_MAGIC_WNR2000V4:
                expected_type = IH_TYPE_KERNEL;
@@ -281,16 +301,26 @@ static ssize_t uimage_verify_wndr3700(u_char *buf, size_t len)
 
 static int
 mtdsplit_uimage_parse_netgear(struct mtd_info *master,
-                             struct mtd_partition **pparts,
+                             const struct mtd_partition **pparts,
                              struct mtd_part_parser_data *data)
 {
        return __mtdsplit_parse_uimage(master, pparts, data,
                                      uimage_verify_wndr3700);
 }
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
+static const struct of_device_id mtdsplit_uimage_netgear_of_match_table[] = {
+       { .compatible = "netgear,uimage" },
+       {},
+};
+#endif
+
 static struct mtd_part_parser uimage_netgear_parser = {
        .owner = THIS_MODULE,
        .name = "netgear-fw",
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
+       .of_match_table = mtdsplit_uimage_netgear_of_match_table,
+#endif
        .parse_fn = mtdsplit_uimage_parse_netgear,
        .type = MTD_PARSER_TYPE_FIRMWARE,
 };
@@ -304,41 +334,45 @@ static struct mtd_part_parser uimage_netgear_parser = {
 
 static ssize_t uimage_find_edimax(u_char *buf, size_t len)
 {
-       struct uimage_header *header;
+       u32 *magic;
 
-       if (len < FW_EDIMAX_OFFSET + sizeof(*header)) {
+       if (len < FW_EDIMAX_OFFSET + sizeof(struct uimage_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:
+       magic = (u32 *)buf;
+       if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
                return -EINVAL;
-       }
 
-       if (header->ih_os != IH_OS_LINUX ||
-           header->ih_type != IH_TYPE_FILESYSTEM)
-               return -EINVAL;
+       if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len))
+               return FW_EDIMAX_OFFSET;
 
-       return FW_EDIMAX_OFFSET;
+       return -EINVAL;
 }
 
 static int
 mtdsplit_uimage_parse_edimax(struct mtd_info *master,
-                             struct mtd_partition **pparts,
+                             const struct mtd_partition **pparts,
                              struct mtd_part_parser_data *data)
 {
        return __mtdsplit_parse_uimage(master, pparts, data,
                                       uimage_find_edimax);
 }
 
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
+static const struct of_device_id mtdsplit_uimage_edimax_of_match_table[] = {
+       { .compatible = "edimax,uimage" },
+       {},
+};
+#endif
+
 static struct mtd_part_parser uimage_edimax_parser = {
        .owner = THIS_MODULE,
        .name = "edimax-fw",
+#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 9, 0)
+       .of_match_table = mtdsplit_uimage_edimax_of_match_table,
+#endif
        .parse_fn = mtdsplit_uimage_parse_edimax,
        .type = MTD_PARSER_TYPE_FIRMWARE,
 };