ramips: drop support for kernel 4.14
[openwrt/openwrt.git] / target / linux / generic / pending-4.14 / 430-mtd-add-myloader-partition-parser.patch
1 From: Florian Fainelli <f.fainelli@gmail.com>
2 Subject: Add myloader partition table parser
3
4 [john@phozen.org: shoud be upstreamable]
5
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>
10
11 --- a/drivers/mtd/Kconfig
12 +++ b/drivers/mtd/Kconfig
13 @@ -178,6 +178,22 @@ menu "Partition parsers"
14 source "drivers/mtd/parsers/Kconfig"
15 endmenu
16
17 +config MTD_MYLOADER_PARTS
18 + tristate "MyLoader partition parsing"
19 + depends on ADM5120 || ATH25 || ATH79
20 + ---help---
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.
25 +
26 + If you need code which can detect and parse these tables, and
27 + register MTD 'partitions' corresponding to each image detected,
28 + enable this option.
29 +
30 + You will still need the parsing functions to be called by the driver
31 + for your particular device. It won't happen automatically.
32 +
33 comment "User Modules And Translation Layers"
34
35 #
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
43 obj-y += parsers/
44
45 # 'Users' - code which presents functionality to userspace.
46 --- /dev/null
47 +++ b/drivers/mtd/myloader.c
48 @@ -0,0 +1,181 @@
49 +/*
50 + * Parse MyLoader-style flash partition tables and produce a Linux partition
51 + * array to match.
52 + *
53 + * Copyright (C) 2007-2009 Gabor Juhos <juhosg@openwrt.org>
54 + *
55 + * This file was based on drivers/mtd/redboot.c
56 + * Author: Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>
57 + *
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.
61 + */
62 +
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>
73 +
74 +#define BLOCK_LEN_MIN 0x10000
75 +#define PART_NAME_LEN 32
76 +
77 +struct part_data {
78 + struct mylo_partition_table tab;
79 + char names[MYLO_MAX_PARTITIONS][PART_NAME_LEN];
80 +};
81 +
82 +static int myloader_parse_partitions(struct mtd_info *master,
83 + const struct mtd_partition **pparts,
84 + struct mtd_part_parser_data *data)
85 +{
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;
91 + int num_parts;
92 + int ret, i;
93 + size_t retlen;
94 + char *names;
95 + unsigned long offset;
96 + unsigned long blocklen;
97 +
98 + buf = vmalloc(sizeof(*buf));
99 + if (!buf) {
100 + return -ENOMEM;
101 + goto out;
102 + }
103 + tab = &buf->tab;
104 +
105 + blocklen = master->erasesize;
106 + if (blocklen < BLOCK_LEN_MIN)
107 + blocklen = BLOCK_LEN_MIN;
108 +
109 + offset = blocklen;
110 +
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);
115 +
116 + ret = mtd_read(master, offset, sizeof(*buf), &retlen,
117 + (void *)buf);
118 + if (ret)
119 + goto out_free_buf;
120 +
121 + if (retlen != sizeof(*buf)) {
122 + ret = -EIO;
123 + goto out_free_buf;
124 + }
125 +
126 + /* Check for Partition Table magic number */
127 + if (tab->magic == le32_to_cpu(MYLO_MAGIC_PARTITIONS))
128 + break;
129 +
130 + }
131 +
132 + if (tab->magic != le32_to_cpu(MYLO_MAGIC_PARTITIONS)) {
133 + printk(KERN_DEBUG "%s: no MyLoader partition table found\n",
134 + master->name);
135 + ret = 0;
136 + goto out_free_buf;
137 + }
138 +
139 + /* The MyLoader and the Partition Table is always present */
140 + num_parts = 2;
141 +
142 + /* Detect number of used partitions */
143 + for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
144 + part = &tab->partitions[i];
145 +
146 + if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
147 + continue;
148 +
149 + num_parts++;
150 + }
151 +
152 + mtd_parts = kzalloc((num_parts * sizeof(*mtd_part) +
153 + num_parts * PART_NAME_LEN), GFP_KERNEL);
154 +
155 + if (!mtd_parts) {
156 + ret = -ENOMEM;
157 + goto out_free_buf;
158 + }
159 +
160 + mtd_part = mtd_parts;
161 + names = (char *)&mtd_parts[num_parts];
162 +
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;
168 + mtd_part++;
169 + names += PART_NAME_LEN;
170 +
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;
176 + mtd_part++;
177 + names += PART_NAME_LEN;
178 +
179 + for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
180 + part = &tab->partitions[i];
181 +
182 + if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
183 + continue;
184 +
185 + if ((buf->names[i][0]) && (buf->names[i][0] != '\xff'))
186 + strncpy(names, buf->names[i], PART_NAME_LEN);
187 + else
188 + snprintf(names, PART_NAME_LEN, "partition%d", i);
189 +
190 + mtd_part->offset = le32_to_cpu(part->addr);
191 + mtd_part->size = le32_to_cpu(part->size);
192 + mtd_part->name = names;
193 + mtd_part++;
194 + names += PART_NAME_LEN;
195 + }
196 +
197 + *pparts = mtd_parts;
198 + ret = num_parts;
199 +
200 + out_free_buf:
201 + vfree(buf);
202 + out:
203 + return ret;
204 +}
205 +
206 +static struct mtd_part_parser myloader_mtd_parser = {
207 + .owner = THIS_MODULE,
208 + .parse_fn = myloader_parse_partitions,
209 + .name = "MyLoader",
210 +};
211 +
212 +static int __init myloader_mtd_parser_init(void)
213 +{
214 + register_mtd_parser(&myloader_mtd_parser);
215 +
216 + return 0;
217 +}
218 +
219 +static void __exit myloader_mtd_parser_exit(void)
220 +{
221 + deregister_mtd_parser(&myloader_mtd_parser);
222 +}
223 +
224 +module_init(myloader_mtd_parser_init);
225 +module_exit(myloader_mtd_parser_exit);
226 +
227 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
228 +MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
229 +MODULE_LICENSE("GPL v2");