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 [add myloader.c to patch]
9 Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
11 --- a/drivers/mtd/Kconfig
12 +++ b/drivers/mtd/Kconfig
13 @@ -177,6 +177,22 @@ menu "Partition parsers"
14 source "drivers/mtd/parsers/Kconfig"
17 +config MTD_MYLOADER_PARTS
18 + tristate "MyLoader partition parsing"
19 + depends on ADM5120 || ATH25 || ATH79
21 + MyLoader is a bootloader which allows the user to define partitions
22 + in flash devices, by putting a table in the second erase block
23 + on the device, similar to a partition table. This table gives the
24 + offsets and lengths of the user defined partitions.
26 + If you need code which can detect and parse these tables, and
27 + register MTD 'partitions' corresponding to each image detected,
30 + You will still need the parsing functions to be called by the driver
31 + for your particular device. It won't happen automatically.
33 comment "User Modules And Translation Layers"
36 --- a/drivers/mtd/Makefile
37 +++ b/drivers/mtd/Makefile
38 @@ -16,6 +16,7 @@ obj-$(CONFIG_MTD_AFS_PARTS) += afs.o
39 obj-$(CONFIG_MTD_AR7_PARTS) += ar7part.o
40 obj-$(CONFIG_MTD_BCM63XX_PARTS) += bcm63xxpart.o
41 obj-$(CONFIG_MTD_BCM47XX_PARTS) += bcm47xxpart.o
42 +obj-$(CONFIG_MTD_MYLOADER_PARTS) += myloader.o
45 # 'Users' - code which presents functionality to userspace.
47 +++ b/drivers/mtd/myloader.c
50 + * Parse MyLoader-style flash partition tables and produce a Linux partition
53 + * Copyright (C) 2007-2009 Gabor Juhos <juhosg@openwrt.org>
55 + * This file was based on drivers/mtd/redboot.c
56 + * Author: Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>
58 + * This program is free software; you can redistribute it and/or modify it
59 + * under the terms of the GNU General Public License version 2 as published
60 + * by the Free Software Foundation.
63 +#include <linux/kernel.h>
64 +#include <linux/module.h>
65 +#include <linux/version.h>
66 +#include <linux/slab.h>
67 +#include <linux/init.h>
68 +#include <linux/vmalloc.h>
69 +#include <linux/mtd/mtd.h>
70 +#include <linux/mtd/partitions.h>
71 +#include <linux/byteorder/generic.h>
72 +#include <linux/myloader.h>
74 +#define BLOCK_LEN_MIN 0x10000
75 +#define PART_NAME_LEN 32
78 + struct mylo_partition_table tab;
79 + char names[MYLO_MAX_PARTITIONS][PART_NAME_LEN];
82 +static int myloader_parse_partitions(struct mtd_info *master,
83 + const struct mtd_partition **pparts,
84 + struct mtd_part_parser_data *data)
86 + struct part_data *buf;
87 + struct mylo_partition_table *tab;
88 + struct mylo_partition *part;
89 + struct mtd_partition *mtd_parts;
90 + struct mtd_partition *mtd_part;
95 + unsigned long offset;
96 + unsigned long blocklen;
98 + buf = vmalloc(sizeof(*buf));
105 + blocklen = master->erasesize;
106 + if (blocklen < BLOCK_LEN_MIN)
107 + blocklen = BLOCK_LEN_MIN;
111 + /* Find the partition table */
112 + for (i = 0; i < 4; i++, offset += blocklen) {
113 + printk(KERN_DEBUG "%s: searching for MyLoader partition table"
114 + " at offset 0x%lx\n", master->name, offset);
116 + ret = mtd_read(master, offset, sizeof(*buf), &retlen,
121 + if (retlen != sizeof(*buf)) {
126 + /* Check for Partition Table magic number */
127 + if (tab->magic == le32_to_cpu(MYLO_MAGIC_PARTITIONS))
132 + if (tab->magic != le32_to_cpu(MYLO_MAGIC_PARTITIONS)) {
133 + printk(KERN_DEBUG "%s: no MyLoader partition table found\n",
139 + /* The MyLoader and the Partition Table is always present */
142 + /* Detect number of used partitions */
143 + for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
144 + part = &tab->partitions[i];
146 + if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
152 + mtd_parts = kzalloc((num_parts * sizeof(*mtd_part) +
153 + num_parts * PART_NAME_LEN), GFP_KERNEL);
160 + mtd_part = mtd_parts;
161 + names = (char *)&mtd_parts[num_parts];
163 + strncpy(names, "myloader", PART_NAME_LEN);
164 + mtd_part->name = names;
165 + mtd_part->offset = 0;
166 + mtd_part->size = offset;
167 + mtd_part->mask_flags = MTD_WRITEABLE;
169 + names += PART_NAME_LEN;
171 + strncpy(names, "partition_table", PART_NAME_LEN);
172 + mtd_part->name = names;
173 + mtd_part->offset = offset;
174 + mtd_part->size = blocklen;
175 + mtd_part->mask_flags = MTD_WRITEABLE;
177 + names += PART_NAME_LEN;
179 + for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
180 + part = &tab->partitions[i];
182 + if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
185 + if ((buf->names[i][0]) && (buf->names[i][0] != '\xff'))
186 + strncpy(names, buf->names[i], PART_NAME_LEN);
188 + snprintf(names, PART_NAME_LEN, "partition%d", i);
190 + mtd_part->offset = le32_to_cpu(part->addr);
191 + mtd_part->size = le32_to_cpu(part->size);
192 + mtd_part->name = names;
194 + names += PART_NAME_LEN;
197 + *pparts = mtd_parts;
206 +static struct mtd_part_parser myloader_mtd_parser = {
207 + .owner = THIS_MODULE,
208 + .parse_fn = myloader_parse_partitions,
209 + .name = "MyLoader",
212 +static int __init myloader_mtd_parser_init(void)
214 + register_mtd_parser(&myloader_mtd_parser);
219 +static void __exit myloader_mtd_parser_exit(void)
221 + deregister_mtd_parser(&myloader_mtd_parser);
224 +module_init(myloader_mtd_parser_init);
225 +module_exit(myloader_mtd_parser_exit);
227 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
228 +MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
229 +MODULE_LICENSE("GPL v2");