kernel: create firmware partition from MyLoader partition parser
[openwrt/openwrt.git] / target / linux / generic / files / drivers / mtd / myloader.c
1 /*
2 * Parse MyLoader-style flash partition tables and produce a Linux partition
3 * array to match.
4 *
5 * Copyright (C) 2007-2009 Gabor Juhos <juhosg@openwrt.org>
6 *
7 * This file was based on drivers/mtd/redboot.c
8 * Author: Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>
9 *
10 * This program is free software; you can redistribute it and/or modify it
11 * under the terms of the GNU General Public License version 2 as published
12 * by the Free Software Foundation.
13 *
14 */
15
16 #include <linux/kernel.h>
17 #include <linux/module.h>
18 #include <linux/version.h>
19 #include <linux/slab.h>
20 #include <linux/init.h>
21 #include <linux/vmalloc.h>
22 #include <linux/mtd/mtd.h>
23 #include <linux/mtd/partitions.h>
24 #include <linux/byteorder/generic.h>
25 #include <linux/myloader.h>
26
27 #define BLOCK_LEN_MIN 0x10000
28 #define PART_NAME_LEN 32
29
30 struct part_data {
31 struct mylo_partition_table tab;
32 char names[MYLO_MAX_PARTITIONS][PART_NAME_LEN];
33 };
34
35 static int myloader_parse_partitions(struct mtd_info *master,
36 struct mtd_partition **pparts,
37 struct mtd_part_parser_data *data)
38 {
39 struct part_data *buf;
40 struct mylo_partition_table *tab;
41 struct mylo_partition *part;
42 struct mtd_partition *mtd_parts;
43 struct mtd_partition *mtd_part;
44 int num_parts;
45 int ret, i;
46 size_t retlen;
47 char *names;
48 unsigned long offset;
49 unsigned long blocklen;
50
51 buf = vmalloc(sizeof(*buf));
52 if (!buf) {
53 return -ENOMEM;
54 goto out;
55 }
56 tab = &buf->tab;
57
58 blocklen = master->erasesize;
59 if (blocklen < BLOCK_LEN_MIN)
60 blocklen = BLOCK_LEN_MIN;
61
62 offset = blocklen;
63
64 /* Find the partition table */
65 for (i = 0; i < 4; i++, offset += blocklen) {
66 printk(KERN_DEBUG "%s: searching for MyLoader partition table"
67 " at offset 0x%lx\n", master->name, offset);
68
69 ret = mtd_read(master, offset, sizeof(*buf), &retlen,
70 (void *)buf);
71 if (ret)
72 goto out_free_buf;
73
74 if (retlen != sizeof(*buf)) {
75 ret = -EIO;
76 goto out_free_buf;
77 }
78
79 /* Check for Partition Table magic number */
80 if (tab->magic == le32_to_cpu(MYLO_MAGIC_PARTITIONS))
81 break;
82
83 }
84
85 if (tab->magic != le32_to_cpu(MYLO_MAGIC_PARTITIONS)) {
86 printk(KERN_DEBUG "%s: no MyLoader partition table found\n",
87 master->name);
88 ret = 0;
89 goto out_free_buf;
90 }
91
92 /*
93 * The MyLoader and the Partition Table is always present.
94 * Additionally, an extra partition is generated to cover
95 * everything after the bootloader.
96 */
97 num_parts = 3;
98
99 /* Detect number of used partitions */
100 for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
101 part = &tab->partitions[i];
102
103 if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
104 continue;
105
106 num_parts++;
107 }
108
109 mtd_parts = kzalloc((num_parts * sizeof(*mtd_part) +
110 num_parts * PART_NAME_LEN), GFP_KERNEL);
111
112 if (!mtd_parts) {
113 ret = -ENOMEM;
114 goto out_free_buf;
115 }
116
117 mtd_part = mtd_parts;
118 names = (char *)&mtd_parts[num_parts];
119
120 strncpy(names, "myloader", PART_NAME_LEN);
121 mtd_part->name = names;
122 mtd_part->offset = 0;
123 mtd_part->size = offset;
124 mtd_part->mask_flags = MTD_WRITEABLE;
125 mtd_part++;
126 names += PART_NAME_LEN;
127
128 strncpy(names, "firmware", PART_NAME_LEN);
129 mtd_part->name = names;
130 mtd_part->offset = offset;
131 mtd_part->size = master->size - offset;
132 mtd_part++;
133 names += PART_NAME_LEN;
134
135 strncpy(names, "partition_table", PART_NAME_LEN);
136 mtd_part->name = names;
137 mtd_part->offset = offset;
138 mtd_part->size = blocklen;
139 mtd_part->mask_flags = MTD_WRITEABLE;
140 mtd_part++;
141 names += PART_NAME_LEN;
142
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 if ((buf->names[i][0]) && (buf->names[i][0] != '\xff'))
150 strncpy(names, buf->names[i], PART_NAME_LEN);
151 else
152 snprintf(names, PART_NAME_LEN, "partition%d", i);
153
154 mtd_part->offset = le32_to_cpu(part->addr);
155 mtd_part->size = le32_to_cpu(part->size);
156 mtd_part->name = names;
157 mtd_part++;
158 names += PART_NAME_LEN;
159 }
160
161 *pparts = mtd_parts;
162 ret = num_parts;
163
164 out_free_buf:
165 vfree(buf);
166 out:
167 return ret;
168 }
169
170 static struct mtd_part_parser myloader_mtd_parser = {
171 .owner = THIS_MODULE,
172 .parse_fn = myloader_parse_partitions,
173 .name = "MyLoader",
174 };
175
176 static int __init myloader_mtd_parser_init(void)
177 {
178 return register_mtd_parser(&myloader_mtd_parser);
179 }
180
181 static void __exit myloader_mtd_parser_exit(void)
182 {
183 deregister_mtd_parser(&myloader_mtd_parser);
184 }
185
186 module_init(myloader_mtd_parser_init);
187 module_exit(myloader_mtd_parser_exit);
188
189 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
190 MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
191 MODULE_LICENSE("GPL v2");