diff mbox series

[OpenWrt-Devel,2/5] ath79: port cybertan_part from ar71xx

Message ID 84075c16f62e93696f6dab6255fd5c6e03fdcaa6.1533936232.git.chunkeey@gmail.com
State Changes Requested
Delegated to: John Crispin
Headers show
Series [OpenWrt-Devel,1/5] packages: nvram: make it possible to include it for ath79 targets | expand

Commit Message

Christian Lamparter Aug. 10, 2018, 9:24 p.m. UTC
The cybertan_part mtd parser is ported over from ar71xx and converted
to integrate into the "firmware" mtd splitter. It will no longer add
the u-boot, nvram and art partitions, which were never part of the
special Cybertan header.

Instead these partitions have to be specified in the DT, which has the
upside of making it possible to add properties (i.e.: read-only), labels
and references to these important partitions.

Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
---
 target/linux/ath79/config-4.14                |   1 +
 .../ath79/files/drivers/mtd/cybertan_part.c   | 176 ++++++++++++++++++
 .../404-mtd-cybertan-trx-parser.patch         |  25 +++
 .../405-mtd-tp-link-partition-parser.patch    |   6 +-
 4 files changed, 205 insertions(+), 3 deletions(-)
 create mode 100644 target/linux/ath79/files/drivers/mtd/cybertan_part.c
 create mode 100644 target/linux/ath79/patches-4.14/404-mtd-cybertan-trx-parser.patch

Comments

Jonas Gorski Aug. 11, 2018, 8:16 p.m. UTC | #1
On 10 August 2018 at 23:24, Christian Lamparter <chunkeey@gmail.com> wrote:
> The cybertan_part mtd parser is ported over from ar71xx and converted
> to integrate into the "firmware" mtd splitter. It will no longer add
> the u-boot, nvram and art partitions, which were never part of the
> special Cybertan header.
>
> Instead these partitions have to be specified in the DT, which has the
> upside of making it possible to add properties (i.e.: read-only), labels
> and references to these important partitions.

Instead of using the OpenWrt proprietary FIRMWARE_PARSER thingy, there
is now an upstream (device tree) solution for that.

By giving the parser an of_mach_table (with e.g. "foo,cybertan") you
can then define it as

partitions {
                       ...

                       partition@40000 {
                               label = "firmware";
                               reg = <0x40000 0x7a0000>;
                               compatible = "foo,cybertan";
                       };
                       ...
};

and the parser will be called for the firmware partition.

And then we can eventually drop the FIRMWARE_PARSER stuff from OpenWrt.

See http://git.infradead.org/linux-mtd-next.git/blob/HEAD:/drivers/mtd/parsers/parser_trx.c
for an example of such a parser. Should be really not much change
needed for that (also it should then reside in the parsers sub
directory). The hardest part will be figuring out, what the right
compatible name is ;-)


>
> Signed-off-by: Christian Lamparter <chunkeey@gmail.com>
> ---
>  target/linux/ath79/config-4.14                |   1 +
>  .../ath79/files/drivers/mtd/cybertan_part.c   | 176 ++++++++++++++++++
>  .../404-mtd-cybertan-trx-parser.patch         |  25 +++
>  .../405-mtd-tp-link-partition-parser.patch    |   6 +-
>  4 files changed, 205 insertions(+), 3 deletions(-)
>  create mode 100644 target/linux/ath79/files/drivers/mtd/cybertan_part.c
>  create mode 100644 target/linux/ath79/patches-4.14/404-mtd-cybertan-trx-parser.patch
>
> diff --git a/target/linux/ath79/config-4.14 b/target/linux/ath79/config-4.14
> index a8349040a1..312740ed17 100644
> --- a/target/linux/ath79/config-4.14
> +++ b/target/linux/ath79/config-4.14
> @@ -171,6 +171,7 @@ CONFIG_MTD_SPLIT_SEAMA_FW=y
>  CONFIG_MTD_SPLIT_TPLINK_FW=y
>  CONFIG_MTD_SPLIT_UIMAGE_FW=y
>  CONFIG_MTD_SPLIT_WRGG_FW=y
> +CONFIG_MTD_CYBERTAN_PARTS=y
>  CONFIG_MTD_TPLINK_PARTS=y
>  CONFIG_NEED_DMA_MAP_STATE=y
>  CONFIG_NEED_PER_CPU_KM=y
> diff --git a/target/linux/ath79/files/drivers/mtd/cybertan_part.c b/target/linux/ath79/files/drivers/mtd/cybertan_part.c
> new file mode 100644
> index 0000000000..47742b2674
> --- /dev/null
> +++ b/target/linux/ath79/files/drivers/mtd/cybertan_part.c
> @@ -0,0 +1,176 @@
> +/*
> + * Copyright (C) 2009 Christian Daniel <cd@maintech.de>
> + * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
> + *
> + * This program is free software; you can redistribute it and/or modify
> + * it under the terms of the GNU General Public License as published by
> + * the Free Software Foundation; either version 2 of the License, or
> + * (at your option) any later version.
> + *
> + * This program is distributed in the hope that it will be useful,
> + * but WITHOUT ANY WARRANTY; without even the implied warranty of
> + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
> + * GNU General Public License for more details.
> + *
> + * You should have received a copy of the GNU General Public License
> + * along with this program; if not, write to the Free Software
> + * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
> + *
> + * TRX flash partition table.
> + * Based on ar7 map by Felix Fietkau <nbd@nbd.name>
> + *
> + */
> +
> +#include <linux/kernel.h>
> +#include <linux/module.h>
> +#include <linux/slab.h>
> +#include <linux/vmalloc.h>
> +
> +#include <linux/mtd/mtd.h>
> +#include <linux/mtd/partitions.h>
> +#include <linux/version.h>
> +
> +struct cybertan_header {
> +       char    magic[4];
> +       u8      res1[4];
> +       char    fw_date[3];
> +       char    fw_ver[3];
> +       char    id[4];
> +       char    hw_ver;
> +       char    unused;
> +       u8      flags[2];
> +       u8      res2[10];
> +};
> +
> +#define TRX_PARTS      3
> +#define TRX_MAGIC      0x30524448
> +#define TRX_MAX_OFFSET 3
> +
> +struct trx_header {
> +       uint32_t magic;           /* "HDR0" */
> +       uint32_t len;             /* Length of file including header */
> +       uint32_t crc32;           /* 32-bit CRC from flag_version to end of file */
> +       uint32_t flag_version;    /* 0:15 flags, 16:31 version */
> +       uint32_t offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
> +};
> +
> +#define IH_MAGIC       0x27051956      /* Image Magic Number */
> +#define IH_NMLEN       32              /* Image Name Length */
> +
> +struct uimage_header {
> +       uint32_t        ih_magic;       /* Image Header Magic Number */
> +       uint32_t        ih_hcrc;        /* Image Header CRC Checksum */
> +       uint32_t        ih_time;        /* Image Creation Timestamp */
> +       uint32_t        ih_size;        /* Image Data Size */
> +       uint32_t        ih_load;        /* Data» Load  Address */
> +       uint32_t        ih_ep;          /* Entry Point Address */
> +       uint32_t        ih_dcrc;        /* Image Data CRC Checksum */
> +       uint8_t         ih_os;          /* Operating System */
> +       uint8_t         ih_arch;        /* CPU architecture */
> +       uint8_t         ih_type;        /* Image Type */
> +       uint8_t         ih_comp;        /* Compression Type */
> +       uint8_t         ih_name[IH_NMLEN];      /* Image Name */
> +};
> +
> +struct firmware_header {
> +       struct cybertan_header  cybertan;
> +       struct trx_header       trx;
> +       struct uimage_header    uimage;
> +} __packed;
> +
> +static int cybertan_parse_partitions(struct mtd_info *master,
> +                                    const struct mtd_partition **pparts,
> +                                    struct mtd_part_parser_data *data)
> +{
> +       struct firmware_header *header;
> +       struct trx_header *theader;
> +       struct uimage_header *uheader;
> +       struct mtd_partition *trx_parts;
> +       size_t retlen;
> +       unsigned int kernel_len;
> +       int ret;
> +
> +       trx_parts = kzalloc(TRX_PARTS * sizeof(struct mtd_partition),
> +                           GFP_KERNEL);
> +       if (!trx_parts) {
> +               ret = -ENOMEM;
> +               goto out;
> +       }
> +
> +       header = vmalloc(sizeof(*header));
> +       if (!header) {
> +               return -ENOMEM;
> +               goto free_parts;
> +       }
> +
> +       ret = mtd_read(master, 0, sizeof(*header),
> +                      &retlen, (void *) header);
> +       if (ret)
> +               goto free_hdr;
> +
> +       if (retlen != sizeof(*header)) {
> +               ret = -EIO;
> +               goto free_hdr;
> +       }
> +
> +       theader = &header->trx;
> +       if (le32_to_cpu(theader->magic) != TRX_MAGIC) {
> +               printk(KERN_NOTICE "%s: no TRX header found\n", master->name);
> +               goto free_hdr;
> +       }
> +
> +       uheader = &header->uimage;
> +       if (uheader->ih_magic != IH_MAGIC) {
> +               printk(KERN_NOTICE "%s: no uImage found\n", master->name);
> +               goto free_hdr;
> +       }
> +
> +       kernel_len = le32_to_cpu(theader->offsets[1]) +
> +               sizeof(struct cybertan_header);
> +
> +       trx_parts[0].name = "header";
> +       trx_parts[0].offset = 0;
> +       trx_parts[0].size = offsetof(struct firmware_header, uimage);
> +       trx_parts[0].mask_flags = 0;
> +
> +       trx_parts[1].name = "kernel";
> +       trx_parts[1].offset = trx_parts[0].offset + trx_parts[0].size;
> +       trx_parts[1].size = kernel_len - trx_parts[0].size;
> +       trx_parts[1].mask_flags = 0;
> +
> +       trx_parts[2].name = "rootfs";
> +       trx_parts[2].offset = trx_parts[1].offset + trx_parts[1].size;
> +       trx_parts[2].size = master->size - trx_parts[1].size - trx_parts[0].size;
> +       trx_parts[2].mask_flags = 0;
> +
> +       vfree(header);
> +
> +       *pparts = trx_parts;
> +       return TRX_PARTS;
> +
> +free_hdr:
> +       vfree(header);
> +free_parts:
> +       kfree(trx_parts);
> +out:
> +       return ret;
> +}
> +
> +static struct mtd_part_parser cybertan_parser = {
> +       .owner          = THIS_MODULE,
> +       .parse_fn       = cybertan_parse_partitions,
> +       .name           = "cybertan",
> +       .type           = MTD_PARSER_TYPE_FIRMWARE,
> +};
> +
> +static int __init cybertan_parser_init(void)
> +{
> +       register_mtd_parser(&cybertan_parser);
> +
> +       return 0;
> +}
> +
> +module_init(cybertan_parser_init);

FWIW, you can just use module_mtd_part_parser(cypertan_parser); and
drop the _init function.


Regards
Jonas
Christian Lamparter Aug. 12, 2018, 12:53 p.m. UTC | #2
On Saturday, August 11, 2018 10:16:06 PM CEST Jonas Gorski wrote:
> On 10 August 2018 at 23:24, Christian Lamparter <chunkeey@gmail.com> wrote:
> > The cybertan_part mtd parser is ported over from ar71xx and converted
> > to integrate into the "firmware" mtd splitter. It will no longer add
> > the u-boot, nvram and art partitions, which were never part of the
> > special Cybertan header.
> >
> > Instead these partitions have to be specified in the DT, which has the
> > upside of making it possible to add properties (i.e.: read-only), labels
> > and references to these important partitions.
> 
> Instead of using the OpenWrt proprietary FIRMWARE_PARSER thingy, there
> is now an upstream (device tree) solution for that.
> 
> By giving the parser an of_mach_table (with e.g. "foo,cybertan") you
> can then define it as
> 
> partitions {
>                        ...
> 
>                        partition@40000 {
>                                label = "firmware";
>                                reg = <0x40000 0x7a0000>;
>                                compatible = "foo,cybertan";
>                        };
>                        ...
> };
> 
> and the parser will be called for the firmware partition.
> 
> And then we can eventually drop the FIRMWARE_PARSER stuff from OpenWrt.
> 
> See http://git.infradead.org/linux-mtd-next.git/blob/HEAD:/drivers/mtd/parsers/parser_trx.c
> for an example of such a parser. Should be really not much change
> needed for that (also it should then reside in the parsers sub
> directory). The hardest part will be figuring out, what the right
> compatible name is ;-)
> 
Thank you, I made all the requested changes and moved the driver to
mtd/parsers/parser_cybertan.c . I'll wait a few days until I post
v2 though.

Regards,
Christian
diff mbox series

Patch

diff --git a/target/linux/ath79/config-4.14 b/target/linux/ath79/config-4.14
index a8349040a1..312740ed17 100644
--- a/target/linux/ath79/config-4.14
+++ b/target/linux/ath79/config-4.14
@@ -171,6 +171,7 @@  CONFIG_MTD_SPLIT_SEAMA_FW=y
 CONFIG_MTD_SPLIT_TPLINK_FW=y
 CONFIG_MTD_SPLIT_UIMAGE_FW=y
 CONFIG_MTD_SPLIT_WRGG_FW=y
+CONFIG_MTD_CYBERTAN_PARTS=y
 CONFIG_MTD_TPLINK_PARTS=y
 CONFIG_NEED_DMA_MAP_STATE=y
 CONFIG_NEED_PER_CPU_KM=y
diff --git a/target/linux/ath79/files/drivers/mtd/cybertan_part.c b/target/linux/ath79/files/drivers/mtd/cybertan_part.c
new file mode 100644
index 0000000000..47742b2674
--- /dev/null
+++ b/target/linux/ath79/files/drivers/mtd/cybertan_part.c
@@ -0,0 +1,176 @@ 
+/*
+ * Copyright (C) 2009 Christian Daniel <cd@maintech.de>
+ * Copyright (C) 2009 Gabor Juhos <juhosg@openwrt.org>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * TRX flash partition table.
+ * Based on ar7 map by Felix Fietkau <nbd@nbd.name>
+ *
+ */
+
+#include <linux/kernel.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+#include <linux/vmalloc.h>
+
+#include <linux/mtd/mtd.h>
+#include <linux/mtd/partitions.h>
+#include <linux/version.h>
+
+struct cybertan_header {
+	char	magic[4];
+	u8	res1[4];
+	char	fw_date[3];
+	char	fw_ver[3];
+	char	id[4];
+	char	hw_ver;
+	char	unused;
+	u8	flags[2];
+	u8	res2[10];
+};
+
+#define TRX_PARTS	3
+#define TRX_MAGIC	0x30524448
+#define TRX_MAX_OFFSET	3
+
+struct trx_header {
+	uint32_t magic;           /* "HDR0" */
+	uint32_t len;             /* Length of file including header */
+	uint32_t crc32;           /* 32-bit CRC from flag_version to end of file */
+	uint32_t flag_version;    /* 0:15 flags, 16:31 version */
+	uint32_t offsets[TRX_MAX_OFFSET]; /* Offsets of partitions from start of header */
+};
+
+#define IH_MAGIC	0x27051956	/* Image Magic Number */
+#define IH_NMLEN	32		/* Image Name Length */
+
+struct uimage_header {
+	uint32_t	ih_magic;	/* Image Header Magic Number */
+	uint32_t	ih_hcrc;	/* Image Header CRC Checksum */
+	uint32_t	ih_time;	/* Image Creation Timestamp */
+	uint32_t	ih_size;	/* Image Data Size */
+	uint32_t	ih_load;	/* Data» Load  Address */
+	uint32_t	ih_ep;		/* Entry Point Address */
+	uint32_t	ih_dcrc;	/* Image Data CRC Checksum */
+	uint8_t		ih_os;		/* Operating System */
+	uint8_t		ih_arch;	/* CPU architecture */
+	uint8_t		ih_type;	/* Image Type */
+	uint8_t		ih_comp;	/* Compression Type */
+	uint8_t		ih_name[IH_NMLEN];	/* Image Name */
+};
+
+struct firmware_header {
+	struct cybertan_header	cybertan;
+	struct trx_header	trx;
+	struct uimage_header	uimage;
+} __packed;
+
+static int cybertan_parse_partitions(struct mtd_info *master,
+				     const struct mtd_partition **pparts,
+				     struct mtd_part_parser_data *data)
+{
+	struct firmware_header *header;
+	struct trx_header *theader;
+	struct uimage_header *uheader;
+	struct mtd_partition *trx_parts;
+	size_t retlen;
+	unsigned int kernel_len;
+	int ret;
+
+	trx_parts = kzalloc(TRX_PARTS * sizeof(struct mtd_partition),
+			    GFP_KERNEL);
+	if (!trx_parts) {
+		ret = -ENOMEM;
+		goto out;
+	}
+
+	header = vmalloc(sizeof(*header));
+	if (!header) {
+		return -ENOMEM;
+		goto free_parts;
+	}
+
+	ret = mtd_read(master, 0, sizeof(*header),
+		       &retlen, (void *) header);
+	if (ret)
+		goto free_hdr;
+
+	if (retlen != sizeof(*header)) {
+		ret = -EIO;
+		goto free_hdr;
+	}
+
+	theader = &header->trx;
+	if (le32_to_cpu(theader->magic) != TRX_MAGIC) {
+		printk(KERN_NOTICE "%s: no TRX header found\n", master->name);
+		goto free_hdr;
+	}
+
+	uheader = &header->uimage;
+	if (uheader->ih_magic != IH_MAGIC) {
+		printk(KERN_NOTICE "%s: no uImage found\n", master->name);
+		goto free_hdr;
+	}
+
+	kernel_len = le32_to_cpu(theader->offsets[1]) +
+		sizeof(struct cybertan_header);
+
+	trx_parts[0].name = "header";
+	trx_parts[0].offset = 0;
+	trx_parts[0].size = offsetof(struct firmware_header, uimage);
+	trx_parts[0].mask_flags = 0;
+
+	trx_parts[1].name = "kernel";
+	trx_parts[1].offset = trx_parts[0].offset + trx_parts[0].size;
+	trx_parts[1].size = kernel_len - trx_parts[0].size;
+	trx_parts[1].mask_flags = 0;
+
+	trx_parts[2].name = "rootfs";
+	trx_parts[2].offset = trx_parts[1].offset + trx_parts[1].size;
+	trx_parts[2].size = master->size - trx_parts[1].size - trx_parts[0].size;
+	trx_parts[2].mask_flags = 0;
+
+	vfree(header);
+
+	*pparts = trx_parts;
+	return TRX_PARTS;
+
+free_hdr:
+	vfree(header);
+free_parts:
+	kfree(trx_parts);
+out:
+	return ret;
+}
+
+static struct mtd_part_parser cybertan_parser = {
+	.owner		= THIS_MODULE,
+	.parse_fn	= cybertan_parse_partitions,
+	.name		= "cybertan",
+	.type		= MTD_PARSER_TYPE_FIRMWARE,
+};
+
+static int __init cybertan_parser_init(void)
+{
+	register_mtd_parser(&cybertan_parser);
+
+	return 0;
+}
+
+module_init(cybertan_parser_init);
+
+MODULE_LICENSE("GPL");
+MODULE_AUTHOR("Christian Daniel <cd@maintech.de>");
diff --git a/target/linux/ath79/patches-4.14/404-mtd-cybertan-trx-parser.patch b/target/linux/ath79/patches-4.14/404-mtd-cybertan-trx-parser.patch
new file mode 100644
index 0000000000..4b59abe16b
--- /dev/null
+++ b/target/linux/ath79/patches-4.14/404-mtd-cybertan-trx-parser.patch
@@ -0,0 +1,25 @@ 
+--- a/drivers/mtd/Kconfig
++++ b/drivers/mtd/Kconfig
+@@ -178,6 +178,12 @@ menu "Partition parsers"
+ source "drivers/mtd/parsers/Kconfig"
+ endmenu
+ 
++config MTD_CYBERTAN_PARTS
++	tristate "Cybertan partitioning support"
++	depends on ATH79
++	---help---
++	   Cybertan partitioning support
++
+ config MTD_MYLOADER_PARTS
+ 	tristate "MyLoader partition parsing"
+ 	depends on ADM5120 || ATH25 || ATH79
+--- a/drivers/mtd/Makefile
++++ b/drivers/mtd/Makefile
+@@ -18,6 +18,7 @@ obj-$(CONFIG_MTD_BCM63XX_PARTS)	+= bcm63
+ obj-$(CONFIG_MTD_BCM47XX_PARTS)	+= bcm47xxpart.o
+ obj-$(CONFIG_MTD_MYLOADER_PARTS) += myloader.o
+ obj-y				+= parsers/
++obj-$(CONFIG_MTD_CYBERTAN_PARTS) += cybertan_part.o
+ 
+ # 'Users' - code which presents functionality to userspace.
+ obj-$(CONFIG_MTD_BLKDEVS)	+= mtd_blkdevs.o
diff --git a/target/linux/ath79/patches-4.14/405-mtd-tp-link-partition-parser.patch b/target/linux/ath79/patches-4.14/405-mtd-tp-link-partition-parser.patch
index 846b7f0a0c..97f0e567bc 100644
--- a/target/linux/ath79/patches-4.14/405-mtd-tp-link-partition-parser.patch
+++ b/target/linux/ath79/patches-4.14/405-mtd-tp-link-partition-parser.patch
@@ -1,6 +1,6 @@ 
 --- a/drivers/mtd/Kconfig
 +++ b/drivers/mtd/Kconfig
-@@ -194,6 +194,12 @@ config MTD_MYLOADER_PARTS
+@@ -200,6 +200,12 @@ config MTD_MYLOADER_PARTS
  	  You will still need the parsing functions to be called by the driver
  	  for your particular device. It won't happen automatically.
  
@@ -15,10 +15,10 @@ 
  #
 --- a/drivers/mtd/Makefile
 +++ b/drivers/mtd/Makefile
-@@ -18,6 +18,7 @@ obj-$(CONFIG_MTD_BCM63XX_PARTS)	+= bcm63
- obj-$(CONFIG_MTD_BCM47XX_PARTS)	+= bcm47xxpart.o
+@@ -19,6 +19,7 @@ obj-$(CONFIG_MTD_BCM47XX_PARTS)	+= bcm47
  obj-$(CONFIG_MTD_MYLOADER_PARTS) += myloader.o
  obj-y				+= parsers/
+ obj-$(CONFIG_MTD_CYBERTAN_PARTS) += cybertan_part.o
 +obj-$(CONFIG_MTD_TPLINK_PARTS)	+= tplinkpart.o
  
  # 'Users' - code which presents functionality to userspace.