2fd24a420c796f00a36f637be83539786ce8d1f4
[openwrt/staging/luka.git] / target / linux / generic / pending-5.4 / 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 [adjust for kernel 5.4, add myloader.c to patch]
9 Signed-off-by: Adrian Schmutzler <freifunk@adrianschmutzler.de>
10
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.
15
16 endif # MTD_REDBOOT_PARTS
17 +
18 +config MTD_MYLOADER_PARTS
19 + tristate "MyLoader partition parsing"
20 + depends on ADM5120 || ATH25 || ATH79
21 + ---help---
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.
26 +
27 + If you need code which can detect and parse these tables, and
28 + register MTD 'partitions' corresponding to each image detected,
29 + enable this option.
30 +
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
40 --- /dev/null
41 +++ b/drivers/mtd/parsers/myloader.c
42 @@ -0,0 +1,181 @@
43 +/*
44 + * Parse MyLoader-style flash partition tables and produce a Linux partition
45 + * array to match.
46 + *
47 + * Copyright (C) 2007-2009 Gabor Juhos <juhosg@openwrt.org>
48 + *
49 + * This file was based on drivers/mtd/redboot.c
50 + * Author: Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>
51 + *
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.
55 + */
56 +
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>
67 +
68 +#define BLOCK_LEN_MIN 0x10000
69 +#define PART_NAME_LEN 32
70 +
71 +struct part_data {
72 + struct mylo_partition_table tab;
73 + char names[MYLO_MAX_PARTITIONS][PART_NAME_LEN];
74 +};
75 +
76 +static int myloader_parse_partitions(struct mtd_info *master,
77 + const struct mtd_partition **pparts,
78 + struct mtd_part_parser_data *data)
79 +{
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;
85 + int num_parts;
86 + int ret, i;
87 + size_t retlen;
88 + char *names;
89 + unsigned long offset;
90 + unsigned long blocklen;
91 +
92 + buf = vmalloc(sizeof(*buf));
93 + if (!buf) {
94 + return -ENOMEM;
95 + goto out;
96 + }
97 + tab = &buf->tab;
98 +
99 + blocklen = master->erasesize;
100 + if (blocklen < BLOCK_LEN_MIN)
101 + blocklen = BLOCK_LEN_MIN;
102 +
103 + offset = blocklen;
104 +
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);
109 +
110 + ret = mtd_read(master, offset, sizeof(*buf), &retlen,
111 + (void *)buf);
112 + if (ret)
113 + goto out_free_buf;
114 +
115 + if (retlen != sizeof(*buf)) {
116 + ret = -EIO;
117 + goto out_free_buf;
118 + }
119 +
120 + /* Check for Partition Table magic number */
121 + if (tab->magic == le32_to_cpu(MYLO_MAGIC_PARTITIONS))
122 + break;
123 +
124 + }
125 +
126 + if (tab->magic != le32_to_cpu(MYLO_MAGIC_PARTITIONS)) {
127 + printk(KERN_DEBUG "%s: no MyLoader partition table found\n",
128 + master->name);
129 + ret = 0;
130 + goto out_free_buf;
131 + }
132 +
133 + /* The MyLoader and the Partition Table is always present */
134 + num_parts = 2;
135 +
136 + /* Detect number of used partitions */
137 + for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
138 + part = &tab->partitions[i];
139 +
140 + if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
141 + continue;
142 +
143 + num_parts++;
144 + }
145 +
146 + mtd_parts = kzalloc((num_parts * sizeof(*mtd_part) +
147 + num_parts * PART_NAME_LEN), GFP_KERNEL);
148 +
149 + if (!mtd_parts) {
150 + ret = -ENOMEM;
151 + goto out_free_buf;
152 + }
153 +
154 + mtd_part = mtd_parts;
155 + names = (char *)&mtd_parts[num_parts];
156 +
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;
162 + mtd_part++;
163 + names += PART_NAME_LEN;
164 +
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;
170 + mtd_part++;
171 + names += PART_NAME_LEN;
172 +
173 + for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
174 + part = &tab->partitions[i];
175 +
176 + if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
177 + continue;
178 +
179 + if ((buf->names[i][0]) && (buf->names[i][0] != '\xff'))
180 + strncpy(names, buf->names[i], PART_NAME_LEN);
181 + else
182 + snprintf(names, PART_NAME_LEN, "partition%d", i);
183 +
184 + mtd_part->offset = le32_to_cpu(part->addr);
185 + mtd_part->size = le32_to_cpu(part->size);
186 + mtd_part->name = names;
187 + mtd_part++;
188 + names += PART_NAME_LEN;
189 + }
190 +
191 + *pparts = mtd_parts;
192 + ret = num_parts;
193 +
194 + out_free_buf:
195 + vfree(buf);
196 + out:
197 + return ret;
198 +}
199 +
200 +static struct mtd_part_parser myloader_mtd_parser = {
201 + .owner = THIS_MODULE,
202 + .parse_fn = myloader_parse_partitions,
203 + .name = "MyLoader",
204 +};
205 +
206 +static int __init myloader_mtd_parser_init(void)
207 +{
208 + register_mtd_parser(&myloader_mtd_parser);
209 +
210 + return 0;
211 +}
212 +
213 +static void __exit myloader_mtd_parser_exit(void)
214 +{
215 + deregister_mtd_parser(&myloader_mtd_parser);
216 +}
217 +
218 +module_init(myloader_mtd_parser_init);
219 +module_exit(myloader_mtd_parser_exit);
220 +
221 +MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
222 +MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
223 +MODULE_LICENSE("GPL v2");