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