kernel: mtdsplit_uimage: replace "edimax, uimage" parser
authorBjørn Mork <bjorn@mork.no>
Wed, 20 Jan 2021 17:36:53 +0000 (18:36 +0100)
committerPetr Štetiar <ynezz@true.cz>
Fri, 22 Jan 2021 20:03:11 +0000 (21:03 +0100)
The "edimax,uimage"" parser can be replaced by the generic
parser using device specific openwrt,partition-magic and
openwrt,offset properties.

Signed-off-by: Bjørn Mork <bjorn@mork.no>
target/linux/generic/files/drivers/mtd/mtdsplit/mtdsplit_uimage.c
target/linux/generic/files/include/dt-bindings/mtd/partitions/uimage.h
target/linux/ramips/dts/mt7620a_edimax_br-6478ac-v2.dts
target/linux/ramips/dts/mt7620a_edimax_ew-7478apc.dts
target/linux/ramips/dts/mt7620a_edimax_ew-747x.dtsi
target/linux/ramips/dts/mt7621_edimax_re23s.dts
target/linux/ramips/dts/rt3050_edimax_3g-6200n.dts
target/linux/ramips/dts/rt3050_edimax_3g-6200nl.dts
target/linux/ramips/dts/rt3662_edimax_br-6475nd.dts

index 7a8ccdf8f59afbfa2839bbc5ccf900c57073070c..a3e55fb1fe38fd034afd8fb4235064202aedbbce 100644 (file)
 
 #include "mtdsplit.h"
 
-/*
- * uimage_header itself is only 64B, but it may be prepended with another data.
- * Currently the biggest size is for Fon(Foxconn) devices: 64B + 32B
- */
-#define MAX_HEADER_LEN         96
-
 /*
  * Legacy format image header,
  * all data in network byte order (aka natural aka bigendian).
@@ -90,6 +84,32 @@ static void uimage_parse_dt(struct mtd_info *master, int *extralen,
                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
  *
@@ -98,8 +118,7 @@ static void uimage_parse_dt(struct mtd_info *master, int *extralen,
  */
 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, u32 ih_magic, u32 ih_type))
+                                  struct mtd_part_parser_data *data)
 {
        struct mtd_partition *parts;
        u_char *buf;
@@ -125,7 +144,7 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
                return -ENOMEM;
 
        uimage_parse_dt(master, &extralen, &ih_magic, &ih_type, &header_offset, &part_magic);
-       buflen = MAX_HEADER_LEN;
+       buflen = sizeof(struct uimage_header) + header_offset;
        buf = vmalloc(buflen);
        if (!buf) {
                ret = -ENOMEM;
@@ -146,16 +165,13 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
                if (header_offset && part_magic && (be32_to_cpu(*(u32 *)buf) != part_magic))
                        continue;
 
-               ret = find_header(buf + header_offset, buflen, ih_magic, ih_type);
+               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;
                }
 
-               /* let uimage_find_edimax override the offset */
-               if (ret > 0)
-                       header_offset = ret;
                header = (struct uimage_header *)(buf + header_offset);
 
                uimage_size = sizeof(*header) +
@@ -238,41 +254,6 @@ err_free_parts:
        return ret;
 }
 
-static ssize_t uimage_verify_default(u_char *buf, size_t len, 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;
-}
-
-static int
-mtdsplit_uimage_parse_generic(struct mtd_info *master,
-                             const 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" },
@@ -283,59 +264,10 @@ static struct mtd_part_parser uimage_generic_parser = {
        .owner = THIS_MODULE,
        .name = "uimage-fw",
        .of_match_table = mtdsplit_uimage_of_match_table,
-       .parse_fn = mtdsplit_uimage_parse_generic,
+       .parse_fn = __mtdsplit_parse_uimage,
        .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, u32 ih_magic, u32 ih_type)
-{
-       u32 *magic;
-
-       if (len < FW_EDIMAX_OFFSET + sizeof(struct uimage_header)) {
-               pr_err("Buffer too small for checking Edimax header\n");
-               return -ENOSPC;
-       }
-
-       magic = (u32 *)buf;
-       if (be32_to_cpu(*magic) != FW_MAGIC_EDIMAX)
-               return -EINVAL;
-
-       if (!uimage_verify_default(buf + FW_EDIMAX_OFFSET, len, ih_magic, ih_type))
-               return FW_EDIMAX_OFFSET;
-
-       return -EINVAL;
-}
-
-static int
-mtdsplit_uimage_parse_edimax(struct mtd_info *master,
-                             const struct mtd_partition **pparts,
-                             struct mtd_part_parser_data *data)
-{
-       return __mtdsplit_parse_uimage(master, pparts, data,
-                                      uimage_find_edimax);
-}
-
-static const struct of_device_id mtdsplit_uimage_edimax_of_match_table[] = {
-       { .compatible = "edimax,uimage" },
-       {},
-};
-
-static struct mtd_part_parser uimage_edimax_parser = {
-       .owner = THIS_MODULE,
-       .name = "edimax-fw",
-       .of_match_table = mtdsplit_uimage_edimax_of_match_table,
-       .parse_fn = mtdsplit_uimage_parse_edimax,
-       .type = MTD_PARSER_TYPE_FIRMWARE,
-};
-
-
 /**************************************************
  * Init
  **************************************************/
@@ -343,7 +275,6 @@ static struct mtd_part_parser uimage_edimax_parser = {
 static int __init mtdsplit_uimage_init(void)
 {
        register_mtd_parser(&uimage_generic_parser);
-       register_mtd_parser(&uimage_edimax_parser);
 
        return 0;
 }
index 407e46ce2c46e917e53f4f2836f6efddc28ff482..43d5f7b5da879850aa43d49e1719688c0de98809 100644 (file)
  * Magic values specific to "openwrt,uimage" partitions
  */
 #define IH_MAGIC_OKLI  0x4f4b4c49      /* 'OKLI'                       */
+#define FW_EDIMAX_OFFSET       20      /* Edimax Firmware Offset       */
+#define FW_MAGIC_EDIMAX        0x43535953      /* Edimax Firmware Magic Number */
 
 #endif /* __UIMAGE_H__ */
index 0b991611b2238ab81c2b6783bc05a60fae078d11..773db22c743e4090130b8beb7923af22f796d620 100644 (file)
@@ -15,6 +15,7 @@
 
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mtd/partitions/uimage.h>
 
 / {
        compatible = "edimax,br-6478ac-v2", "ralink,mt7620a-soc";
                        };
 
                        partition@70000 {
-                               compatible = "edimax,uimage";
+                               compatible = "openwrt,uimage", "denx,uimage";
+                               openwrt,offset = <FW_EDIMAX_OFFSET>;
+                               openwrt,partition-magic = <FW_MAGIC_EDIMAX>;
                                label = "firmware";
                                reg = <0x00070000 0x00790000>;
                        };
index 1e1b2830f02b36146dfa2b13d688a485ced95ea4..77d214e888c1b3c30ec22fc9e14ed26335953c97 100644 (file)
@@ -4,6 +4,7 @@
 
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mtd/partitions/uimage.h>
 
 / {
        compatible = "edimax,ew-7478apc", "ralink,mt7620a-soc";
                        };
 
                        partition@70000 {
-                               compatible = "edimax,uimage";
+                               compatible = "openwrt,uimage", "denx,uimage";
+                               openwrt,offset = <FW_EDIMAX_OFFSET>;
+                               openwrt,partition-magic = <FW_MAGIC_EDIMAX>;
                                label = "firmware";
                                reg = <0x00070000 0x00790000>;
                        };
index 78b481bd2a64193e13f6957cba25588f006e3afa..d17cc090c2d2b170b5883edc7b5e8357273ab597 100644 (file)
@@ -4,6 +4,7 @@
 
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mtd/partitions/uimage.h>
 
 / {
        compatible = "ralink,mt7620a-soc";
                        };
 
                        partition@70000 {
-                               compatible = "edimax,uimage";
+                               compatible = "openwrt,uimage", "denx,uimage";
+                               openwrt,offset = <FW_EDIMAX_OFFSET>;
+                               openwrt,partition-magic = <FW_MAGIC_EDIMAX>;
                                label = "firmware";
                                reg = <0x00070000 0x00790000>;
                        };
index cf44746e8f613588bc80cae9f4afbcc93c7db1a2..9b9657344601f98084f9f280c759a7293884fe12 100644 (file)
@@ -4,6 +4,7 @@
 
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mtd/partitions/uimage.h>
 
 / {
        compatible = "edimax,re23s", "mediatek,mt7621-soc";
@@ -97,7 +98,9 @@
                        };
 
                        partition@70000 {
-                               compatible = "edimax,uimage";
+                               compatible = "openwrt,uimage", "denx,uimage";
+                               openwrt,offset = <FW_EDIMAX_OFFSET>;
+                               openwrt,partition-magic = <FW_MAGIC_EDIMAX>;
                                label = "firmware";
                                reg = <0x70000 0xf50000>;
                        };
index a185e5d5bb8d8e520c7cbe291ef1b05b282e7a08..07ff8bc9bc467ec3363e2c6f09548f352477bf8a 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mtd/partitions/uimage.h>
 
 / {
        compatible = "edimax,3g-6200n", "ralink,rt3050-soc";
@@ -50,7 +51,9 @@
                        };
 
                        partition@50000 {
-                               compatible = "edimax,uimage";
+                               compatible = "openwrt,uimage", "denx,uimage";
+                               openwrt,offset = <FW_EDIMAX_OFFSET>;
+                               openwrt,partition-magic = <FW_MAGIC_EDIMAX>;
                                label = "firmware";
                                reg = <0x50000 0x390000>;
                        };
index 41768078f5fcc4006ec034c1b2f3a11ee37427a9..f339b7ebe8bd9dad8a297c7ef63b36bb89c51518 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mtd/partitions/uimage.h>
 
 / {
        compatible = "edimax,3g-6200nl", "ralink,rt3050-soc";
@@ -50,7 +51,9 @@
                        };
 
                        partition@50000 {
-                               compatible = "edimax,uimage";
+                               compatible = "openwrt,uimage", "denx,uimage";
+                               openwrt,offset = <FW_EDIMAX_OFFSET>;
+                               openwrt,partition-magic = <FW_MAGIC_EDIMAX>;
                                label = "firmware";
                                reg = <0x50000 0x390000>;
                        };
index a1e7c837d1a6d6fb8bfe02513bfea5457093725f..56d9dc08948d930820de5a6f3421ceb4b057da39 100644 (file)
@@ -2,6 +2,7 @@
 
 #include <dt-bindings/gpio/gpio.h>
 #include <dt-bindings/input/input.h>
+#include <dt-bindings/mtd/partitions/uimage.h>
 
 / {
        compatible = "edimax,br-6475nd", "ralink,rt3662-soc", "ralink,rt3883-soc";
@@ -86,7 +87,9 @@
                        };
 
                        partition@70000 {
-                               compatible = "edimax,uimage";
+                               compatible = "openwrt,uimage", "denx,uimage";
+                               openwrt,offset = <FW_EDIMAX_OFFSET>;
+                               openwrt,partition-magic = <FW_MAGIC_EDIMAX>;
                                reg = <0x00070000 0x00790000>;
                                label = "firmware";
                        };