kernel: add GS110TPPV1 support to mtdsplit_uimage
[openwrt/staging/chunkeey.git] / target / linux / generic / files / drivers / mtd / mtdsplit / mtdsplit_uimage.c
index bc2705ffad59f7e845edbb3990ec9b07ba239206..57d8b9f420d769712493cd6579875cc7d8f52ec3 100644 (file)
 #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
+ * Currently the biggest size is for Fon(Foxconn) devices: 64B + 32B
  */
-#define MAX_HEADER_LEN         84
+#define MAX_HEADER_LEN         96
 
 #define IH_MAGIC       0x27051956      /* Image Magic Number           */
 #define IH_NMLEN               32      /* Image Name Length            */
@@ -79,12 +80,12 @@ read_uimage_header(struct mtd_info *mtd, size_t offset, u_char *buf,
  * __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,
-                                  const 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,
+                  ssize_t (*find_header)(u_char *buf, size_t len, int *extralen))
 {
        struct mtd_partition *parts;
        u_char *buf;
@@ -96,6 +97,7 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
        size_t rootfs_size = 0;
        int uimage_part, rf_part;
        int ret;
+       int extralen;
        enum mtdsplit_part_type type;
 
        nr_parts = 2;
@@ -119,7 +121,8 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
                if (ret)
                        continue;
 
-               ret = find_header(buf, MAX_HEADER_LEN);
+               extralen = 0;
+               ret = find_header(buf, MAX_HEADER_LEN, &extralen);
                if (ret < 0) {
                        pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
                                 master->name, (unsigned long long) offset);
@@ -127,7 +130,9 @@ 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) + ret;
+               uimage_size = sizeof(*header) +
+                               be32_to_cpu(header->ih_size) + ret + extralen;
+
                if ((offset + uimage_size) > master->size) {
                        pr_debug("uImage exceeds MTD device \"%s\"\n",
                                 master->name);
@@ -205,7 +210,7 @@ err_free_parts:
        return ret;
 }
 
-static ssize_t uimage_verify_default(u_char *buf, size_t len)
+static ssize_t uimage_verify_default(u_char *buf, size_t len, int *extralen)
 {
        struct uimage_header *header = (struct uimage_header *)buf;
 
@@ -240,23 +245,20 @@ mtdsplit_uimage_parse_generic(struct mtd_info *master,
                                      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_GS110TPPV1    0x4e474520
 #define FW_MAGIC_WNR2000V1     0x32303031
 #define FW_MAGIC_WNR2000V3     0x32303033
 #define FW_MAGIC_WNR2000V4     0x32303034
@@ -268,12 +270,16 @@ static struct mtd_part_parser uimage_generic_parser = {
 #define FW_MAGIC_WNDR3700V2    0x33373031
 #define FW_MAGIC_WPN824N       0x31313030
 
-static ssize_t uimage_verify_wndr3700(u_char *buf, size_t len)
+static ssize_t uimage_verify_wndr3700(u_char *buf, size_t len, int *extralen)
 {
        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_GS110TPPV1:
+       case FW_MAGIC_WNR2000V4:
+               expected_type = IH_TYPE_KERNEL;
+               break;
        case FW_MAGIC_WNR612V2:
        case FW_MAGIC_WNR1000V2:
        case FW_MAGIC_WNR1000V2_VC:
@@ -284,9 +290,6 @@ static ssize_t uimage_verify_wndr3700(u_char *buf, size_t len)
        case FW_MAGIC_WNDR3700V2:
        case FW_MAGIC_WPN824N:
                break;
-       case FW_MAGIC_WNR2000V4:
-               expected_type = IH_TYPE_KERNEL;
-               break;
        default:
                return -EINVAL;
        }
@@ -307,23 +310,68 @@ mtdsplit_uimage_parse_netgear(struct mtd_info *master,
                                      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,
+
+};
+
+
+/**************************************************
+ * ALLNET
+ **************************************************/
+
+#define FW_MAGIC_SG8208M       0x00000006
+#define FW_MAGIC_SG8310PM      0x83000006
+
+static ssize_t uimage_verify_allnet(u_char *buf, size_t len, int *extralen)
+{
+       struct uimage_header *header = (struct uimage_header *)buf;
+
+       switch (be32_to_cpu(header->ih_magic)) {
+       case FW_MAGIC_SG8208M:
+       case FW_MAGIC_SG8310PM:
+               break;
+       default:
+               return -EINVAL;
+       }
+
+       if (header->ih_os != IH_OS_LINUX)
+               return -EINVAL;
+
+       return 0;
+}
+
+static int
+mtdsplit_uimage_parse_allnet(struct mtd_info *master,
+                             const struct mtd_partition **pparts,
+                             struct mtd_part_parser_data *data)
+{
+       return __mtdsplit_parse_uimage(master, pparts, data,
+                                     uimage_verify_allnet);
+}
+
+static const struct of_device_id mtdsplit_uimage_allnet_of_match_table[] = {
+       { .compatible = "allnet,uimage" },
+       {},
 };
 
+static struct mtd_part_parser uimage_allnet_parser = {
+       .owner = THIS_MODULE,
+       .name = "allnet-fw",
+       .of_match_table = mtdsplit_uimage_allnet_of_match_table,
+       .parse_fn = mtdsplit_uimage_parse_allnet,
+};
+
+
 /**************************************************
  * Edimax
  **************************************************/
@@ -331,7 +379,7 @@ static struct mtd_part_parser uimage_netgear_parser = {
 #define FW_EDIMAX_OFFSET       20
 #define FW_MAGIC_EDIMAX                0x43535953
 
-static ssize_t uimage_find_edimax(u_char *buf, size_t len)
+static ssize_t uimage_find_edimax(u_char *buf, size_t len, int *extralen)
 {
        u32 *magic;
 
@@ -344,7 +392,7 @@ static ssize_t uimage_find_edimax(u_char *buf, size_t len)
        if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
                return -EINVAL;
 
-       if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len))
+       if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len, extralen))
                return FW_EDIMAX_OFFSET;
 
        return -EINVAL;
@@ -359,23 +407,147 @@ mtdsplit_uimage_parse_edimax(struct mtd_info *master,
                                       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,
 };
 
+
+/**************************************************
+ * Fon(Foxconn)
+ **************************************************/
+
+#define FONFXC_PAD_LEN         32
+
+static ssize_t uimage_find_fonfxc(u_char *buf, size_t len, int *extralen)
+{
+       if (uimage_verify_default(buf, len, extralen) < 0)
+               return -EINVAL;
+
+       *extralen = FONFXC_PAD_LEN;
+
+       return 0;
+}
+
+static int
+mtdsplit_uimage_parse_fonfxc(struct mtd_info *master,
+                             const struct mtd_partition **pparts,
+                             struct mtd_part_parser_data *data)
+{
+       return __mtdsplit_parse_uimage(master, pparts, data,
+                                      uimage_find_fonfxc);
+}
+
+static const struct of_device_id mtdsplit_uimage_fonfxc_of_match_table[] = {
+       { .compatible = "fonfxc,uimage" },
+       {},
+};
+
+static struct mtd_part_parser uimage_fonfxc_parser = {
+       .owner = THIS_MODULE,
+       .name = "fonfxc-fw",
+       .of_match_table = mtdsplit_uimage_fonfxc_of_match_table,
+       .parse_fn = mtdsplit_uimage_parse_fonfxc,
+};
+
+/**************************************************
+ * SGE (T&W) Shenzhen Gongjin Electronics
+ **************************************************/
+
+#define SGE_PAD_LEN            96
+
+static ssize_t uimage_find_sge(u_char *buf, size_t len, int *extralen)
+{
+       if (uimage_verify_default(buf, len, extralen) < 0)
+               return -EINVAL;
+
+       *extralen = SGE_PAD_LEN;
+
+       return 0;
+}
+
+static int
+mtdsplit_uimage_parse_sge(struct mtd_info *master,
+                             const struct mtd_partition **pparts,
+                             struct mtd_part_parser_data *data)
+{
+       return __mtdsplit_parse_uimage(master, pparts, data,
+                                      uimage_find_sge);
+}
+
+static const struct of_device_id mtdsplit_uimage_sge_of_match_table[] = {
+       { .compatible = "sge,uimage" },
+       {},
+};
+
+static struct mtd_part_parser uimage_sge_parser = {
+       .owner = THIS_MODULE,
+       .name = "sge-fw",
+       .of_match_table = mtdsplit_uimage_sge_of_match_table,
+       .parse_fn = mtdsplit_uimage_parse_sge,
+};
+
+/**************************************************
+ * OKLI (OpenWrt Kernel Loader Image)
+ **************************************************/
+
+#define IH_MAGIC_OKLI  0x4f4b4c49
+
+static ssize_t uimage_verify_okli(u_char *buf, size_t len, int *extralen)
+{
+       struct uimage_header *header = (struct uimage_header *)buf;
+
+       /* default sanity checks */
+       if (be32_to_cpu(header->ih_magic) != IH_MAGIC_OKLI) {
+               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_okli(struct mtd_info *master,
+                             const struct mtd_partition **pparts,
+                             struct mtd_part_parser_data *data)
+{
+       return __mtdsplit_parse_uimage(master, pparts, data,
+                                     uimage_verify_okli);
+}
+
+static const struct of_device_id mtdsplit_uimage_okli_of_match_table[] = {
+       { .compatible = "openwrt,okli" },
+       {},
+};
+
+static struct mtd_part_parser uimage_okli_parser = {
+       .owner = THIS_MODULE,
+       .name = "okli-fw",
+       .of_match_table = mtdsplit_uimage_okli_of_match_table,
+       .parse_fn = mtdsplit_uimage_parse_okli,
+};
+
 /**************************************************
  * Init
  **************************************************/
@@ -384,7 +556,11 @@ static int __init mtdsplit_uimage_init(void)
 {
        register_mtd_parser(&uimage_generic_parser);
        register_mtd_parser(&uimage_netgear_parser);
+       register_mtd_parser(&uimage_allnet_parser);
        register_mtd_parser(&uimage_edimax_parser);
+       register_mtd_parser(&uimage_fonfxc_parser);
+       register_mtd_parser(&uimage_sge_parser);
+       register_mtd_parser(&uimage_okli_parser);
 
        return 0;
 }