kernel: Add missing includes mtdsplit_*.c
[openwrt/staging/blogic.git] / target / linux / generic / files / drivers / mtd / mtdsplit / mtdsplit_uimage.c
index c30ee6ad74113d564738b068e83cd6b219c72cb3..aba1c83ad65cdf6a56c1683366454367c16f3fa0 100644 (file)
@@ -16,7 +16,9 @@
 #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"
 
@@ -81,7 +83,7 @@ read_uimage_header(struct mtd_info *mtd, size_t offset, u_char *buf,
  *      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))
 {
@@ -95,6 +97,7 @@ 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);
@@ -125,7 +128,7 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
                }
                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);
@@ -148,7 +151,7 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
 
                /* find the roots after the uImage */
                ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size,
-                                          master->size, &rootfs_offset, NULL);
+                                          master->size, &rootfs_offset, &type);
                if (ret) {
                        pr_debug("no rootfs after uImage in \"%s\"\n",
                                 master->name);
@@ -162,7 +165,7 @@ 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, NULL);
+               ret = mtd_check_rootfs_magic(master, 0, &type);
                if (ret) {
                        pr_debug("no rootfs before uImage in \"%s\"\n",
                                 master->name);
@@ -183,7 +186,10 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
        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;
 
@@ -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
@@ -257,10 +274,11 @@ 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:
@@ -283,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,
 };
@@ -306,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,
 };