On 10 August 2018 at 23:24, Christian Lamparter <chunk...@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 <chunk...@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 <c...@maintech.de> > + * Copyright (C) 2009 Gabor Juhos <juh...@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 <n...@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 _______________________________________________ openwrt-devel mailing list openwrt-devel@lists.openwrt.org https://lists.openwrt.org/mailman/listinfo/openwrt-devel