1 From: Florian Fainelli <f.fainelli@gmail.com>
2 Subject: Add myloader partition table parser
4 [john@phozen.org: shoud be upstreamable]
6 lede-commit: d8bf22859b51faa09d22c056fe221a45d2f7a3b8
7 Signed-off-by: Florian Fainelli <f.fainelli@gmail.com>
8 [adjust for kernel 5.4, add myloader.c to patch]
9 Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
11 --- a/drivers/mtd/parsers/Kconfig
12 +++ b/drivers/mtd/parsers/Kconfig
13 @@ -160,3 +160,19 @@ config MTD_REDBOOT_PARTS_READONLY
14 'FIS directory' images, enable this option.
16 endif # MTD_REDBOOT_PARTS
18 +config MTD_MYLOADER_PARTS
19 + tristate "MyLoader partition parsing"
20 + depends on ADM5120 || ATH25 || ATH79
22 + MyLoader is a bootloader which allows the user to define partitions
23 + in flash devices, by putting a table in the second erase block
24 + on the device, similar to a partition table. This table gives the
25 + offsets and lengths of the user defined partitions.
27 + If you need code which can detect and parse these tables, and
28 + register MTD 'partitions' corresponding to each image detected,
31 + You will still need the parsing functions to be called by the driver
32 + for your particular device. It won't happen automatically.
33 --- a/drivers/mtd/parsers/Makefile
34 +++ b/drivers/mtd/parsers/Makefile
35 @@ -9,3 +9,4 @@ obj-$(CONFIG_MTD_AFS_PARTS) += afs.o
36 obj-$(CONFIG_MTD_PARSER_TRX) += parser_trx.o
37 obj-$(CONFIG_MTD_SHARPSL_PARTS) += sharpslpart.o
38 obj-$(CONFIG_MTD_REDBOOT_PARTS) += redboot.o
39 +obj-$(CONFIG_MTD_MYLOADER_PARTS) += myloader.o
41 +++ b/drivers/mtd/parsers/myloader.c
44 + * Parse MyLoader-style flash partition tables and produce a Linux partition
47 + * Copyright (C) 2007-2009 Gabor Juhos <juhosg@openwrt.org>
49 + * This file was based on drivers/mtd/redboot.c
50 + * Author: Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>
52 + * This program is free software; you can redistribute it and/or modify it
53 + * under the terms of the GNU General Public License version 2 as published
54 + * by the Free Software Foundation.
57 +#include <linux/kernel.h>
58 +#include <linux/module.h>
59 +#include <linux/version.h>
60 +#include <linux/slab.h>
61 +#include <linux/init.h>
62 +#include <linux/vmalloc.h>
63 +#include <linux/mtd/mtd.h>
64 +#include <linux/mtd/partitions.h>
65 +#include <linux/byteorder/generic.h>
66 +#include <linux/myloader.h>
68 +#define BLOCK_LEN_MIN 0x10000
69 +#define PART_NAME_LEN 32
72 + struct mylo_partition_table tab;
73 + char names[MYLO_MAX_PARTITIONS][PART_NAME_LEN];
76 +static int myloader_parse_partitions(struct mtd_info *master,
77 + const struct mtd_partition **pparts,
78 + struct mtd_part_parser_data *data)
80 + struct part_data *buf;
81 + struct mylo_partition_table *tab;
82 + struct mylo_partition *part;
83 + struct mtd_partition *mtd_parts;
84 + struct mtd_partition *mtd_part;
89 + unsigned long offset;
90 + unsigned long blocklen;
92 + buf = vmalloc(sizeof(*buf));
99 + blocklen = master->erasesize;
100 + if (blocklen < BLOCK_LEN_MIN)
101 + blocklen = BLOCK_LEN_MIN;
105 + /* Find the partition table */
106 + for (i = 0; i < 4; i++, offset += blocklen) {
107 + printk(KERN_DEBUG "%s: searching for MyLoader partition table"
108 + " at offset 0x%lx\n", master->name, offset);
110 + ret = mtd_read(master, offset, sizeof(*buf), &retlen,
115 + if (retlen != sizeof(*buf)) {
120 + /* Check for Partition Table magic number */
121 + if (tab->magic == le32_to_cpu(MYLO_MAGIC_PARTITIONS))
126 + if (tab->magic != le32_to_cpu(MYLO_MAGIC_PARTITIONS)) {
127 + printk(KERN_DEBUG "%s: no MyLoader partition table found\n",
133 + /* The MyLoader and the Partition Table is always present */
136 + /* Detect number of used partitions */
137 + for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
138 + part = &tab->partitions[i];
140 + if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
146 + mtd_parts = kzalloc((num_parts * sizeof(*mtd_part) +
147 + num_parts * PART_NAME_LEN), GFP_KERNEL);
154 + mtd_part = mtd_parts;
155 + names = (char *)&mtd_parts[num_parts];
157 + strncpy(names, "myloader", PART_NAME_LEN);
158 + mtd_part->name = names;
159 + mtd_part->offset = 0;
160 + mtd_part->size = offset;
161 + mtd_part->mask_flags = MTD_WRITEABLE;
163 + names += PART_NAME_LEN;
165 + strncpy(names, "partition_table", PART_NAME_LEN);
166 + mtd_part->name = names;
167 + mtd_part->offset = offset;
168 + mtd_part->size = blocklen;
169 + mtd_part->mask_flags = MTD_WRITEABLE;
171 + names += PART_NAME_LEN;
173 + for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
174 + part = &tab->partitions[i];
176 + if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
179 + if ((buf->names[i][0]) && (buf->names[i][0] != '\xff'))
180 + strncpy(names, buf->names[i], PART_NAME_LEN);
182 + snprintf(names, PART_NAME_LEN, "partition%d", i);
184 + mtd_part->offset = le32_to_cpu(part->addr);
185 + mtd_part->size = le32_to_cpu(part->size);
186 + mtd_part->name = names;
188 + names += PART_NAME_LEN;
191 + *pparts = mtd_parts;
200 +static struct mtd_part_parser myloader_mtd_parser = {
201 + .owner = THIS_MODULE,
202 + .parse_fn = myloader_parse_partitions,
203 + .name = "MyLoader",
206 +static int __init myloader_mtd_parser_init(void)
208 + register_mtd_parser(&myloader_mtd_parser);
213 +static void __exit myloader_mtd_parser_exit(void)
215 + deregister_mtd_parser(&myloader_mtd_parser);
218 +module_init(myloader_mtd_parser_init);
219 +module_exit(myloader_mtd_parser_exit);
221 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
222 +MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
223 +MODULE_LICENSE("GPL v2");