coding style fixes
[openwrt/staging/florian.git] / target / linux / adm5120 / files / drivers / mtd / myloader.c
1 /*
2 * $Id$
3 *
4 * Parse MyLoader-style flash partition tables and produce a Linux partition
5 * array to match.
6 *
7 * Copyright (C) 2007 OpenWrt.org
8 * Copyright (C) 2007 Gabor Juhos <juhosg at openwrt.org>
9 *
10 * This file was based on drivers/mtd/redboot.c
11 * Author: Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation; either version 2
16 * of the License, or (at your option) any later version.
17 *
18 * This program is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, write to the
25 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 * Boston, MA 02110-1301, USA.
27 */
28
29 #include <linux/kernel.h>
30 #include <linux/slab.h>
31 #include <linux/init.h>
32 #include <linux/vmalloc.h>
33
34 #include <linux/mtd/mtd.h>
35 #include <linux/mtd/partitions.h>
36
37 #include <linux/byteorder/generic.h>
38
39 #include <prom/myloader.h>
40
41 #define NAME_LEN_MAX 20
42 #define NAME_MYLOADER "MyLoader"
43 #define NAME_PARTITION_TABLE "Partition Table"
44 #define BLOCK_LEN_MIN 0x10000
45
46 int parse_myloader_partitions(struct mtd_info *master,
47 struct mtd_partition **pparts,
48 unsigned long origin)
49 {
50 struct mylo_partition_table *tab;
51 struct mylo_partition *part;
52 struct mtd_partition *mtd_parts;
53 struct mtd_partition *mtd_part;
54 int num_parts;
55 int ret, i;
56 size_t retlen;
57 char *names;
58 unsigned long offset;
59 unsigned long blocklen;
60
61 tab = vmalloc(sizeof(*tab));
62 if (!tab) {
63 return -ENOMEM;
64 goto out;
65 }
66
67 blocklen = master->erasesize;
68 if (blocklen < BLOCK_LEN_MIN)
69 blocklen = BLOCK_LEN_MIN;
70
71 /* Partition Table is always located on the second erase block */
72 offset = blocklen;
73 printk(KERN_NOTICE "%s: searching for MyLoader partition table at "
74 "offset 0x%lx\n", master->name, offset);
75
76 ret = master->read(master, offset, sizeof(*tab), &retlen, (void *)tab);
77 if (ret)
78 goto out;
79
80 if (retlen != sizeof(*tab)) {
81 ret = -EIO;
82 goto out_free_buf;
83 }
84
85 /* Check for Partition Table magic number */
86 if (tab->magic != le32_to_cpu(MYLO_MAGIC_PARTITIONS)) {
87 printk(KERN_NOTICE "%s: no MyLoader partition table found\n",
88 master->name);
89 ret = 0;
90 goto out_free_buf;
91 }
92
93 /* The MyLoader and the Partition Table is always present */
94 num_parts = 2;
95
96 /* Detect number of used partitions */
97 for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
98 part = &tab->partitions[i];
99
100 if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
101 continue;
102
103 num_parts++;
104 }
105
106 mtd_parts = kzalloc((num_parts * sizeof(*mtd_part) +
107 num_parts * NAME_LEN_MAX), GFP_KERNEL);
108
109 if (!mtd_parts) {
110 ret = -ENOMEM;
111 goto out_free_buf;
112 }
113
114 mtd_part = mtd_parts;
115 names = (char *)&mtd_parts[num_parts];
116
117 strncpy(names, NAME_MYLOADER, NAME_LEN_MAX-1);
118 mtd_part->name = names;
119 mtd_part->offset = 0;
120 mtd_part->size = blocklen;
121 mtd_part->mask_flags = MTD_WRITEABLE;
122 mtd_part++;
123 names += NAME_LEN_MAX;
124
125 strncpy(names, NAME_PARTITION_TABLE, NAME_LEN_MAX-1);
126 mtd_part->name = names;
127 mtd_part->offset = blocklen;
128 mtd_part->size = blocklen;
129 mtd_part->mask_flags = MTD_WRITEABLE;
130 mtd_part++;
131 names += NAME_LEN_MAX;
132
133 for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
134 part = &tab->partitions[i];
135
136 if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
137 continue;
138
139 sprintf(names, "partition%d", i);
140 mtd_part->offset = le32_to_cpu(part->addr);
141 mtd_part->size = le32_to_cpu(part->size);
142 mtd_part->name = names;
143 mtd_part++;
144 names += NAME_LEN_MAX;
145 }
146
147 *pparts = mtd_parts;
148 ret = num_parts;
149
150 out_free_buf:
151 vfree(tab);
152 out:
153 return ret;
154 }
155
156 static struct mtd_part_parser mylo_mtd_parser = {
157 .owner = THIS_MODULE,
158 .parse_fn = parse_myloader_partitions,
159 .name = NAME_MYLOADER,
160 };
161
162 static int __init mylo_mtd_parser_init(void)
163 {
164 return register_mtd_parser(&mylo_mtd_parser);
165 }
166
167 static void __exit mylo_mtd_parser_exit(void)
168 {
169 deregister_mtd_parser(&mylo_mtd_parser);
170 }
171
172 module_init(mylo_mtd_parser_init);
173 module_exit(mylo_mtd_parser_exit);
174
175 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
176 MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
177 MODULE_LICENSE("GPL");