[adm5120] use the global register access macros in the flash driver
[openwrt.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 size_t parts_len;
58 char *names;
59 unsigned long offset;
60 unsigned long blocklen;
61
62 tab = vmalloc(sizeof(*tab));
63 if (!tab) {
64 return -ENOMEM;
65 goto out;
66 }
67
68 blocklen = master->erasesize;
69 if (blocklen < BLOCK_LEN_MIN)
70 blocklen = BLOCK_LEN_MIN;
71
72 /* Partition Table is always located on the second erase block */
73 offset = blocklen;
74 printk(KERN_NOTICE "%s: searching for MyLoader partition table at "
75 "offset 0x%lx\n", master->name, offset);
76
77 ret = master->read(master, offset, sizeof(*tab), &retlen, (void *)tab);
78 if (ret)
79 goto out;
80
81 if (retlen != sizeof(*tab)) {
82 ret = -EIO;
83 goto out_free_buf;
84 }
85
86 /* Check for Partition Table magic number */
87 if (tab->magic != le32_to_cpu(MYLO_MAGIC_PARTITIONS)) {
88 printk(KERN_NOTICE "%s: no MyLoader partition table found\n",
89 master->name);
90 ret = 0;
91 goto out_free_buf;
92 }
93
94 /* The MyLoader and the Partition Table is always present */
95 num_parts = 2;
96
97 /* Detect number of used partitions */
98 for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
99 part = &tab->partitions[i];
100
101 if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
102 continue;
103
104 num_parts++;
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 strncpy(names, NAME_MYLOADER, NAME_LEN_MAX-1);
119 mtd_part->name = names;
120 mtd_part->offset = 0;
121 mtd_part->size = blocklen;
122 mtd_part->mask_flags = MTD_WRITEABLE;
123 mtd_part++;
124 names += NAME_LEN_MAX;
125
126 strncpy(names, NAME_PARTITION_TABLE, NAME_LEN_MAX-1);
127 mtd_part->name = names;
128 mtd_part->offset = blocklen;
129 mtd_part->size = blocklen;
130 mtd_part->mask_flags = MTD_WRITEABLE;
131 mtd_part++;
132 names += NAME_LEN_MAX;
133
134 for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
135 part = &tab->partitions[i];
136
137 if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
138 continue;
139
140 sprintf(names, "partition%d", i);
141 mtd_part->offset = le32_to_cpu(part->addr);
142 mtd_part->size = le32_to_cpu(part->size);
143 mtd_part->name = names;
144 mtd_part++;
145 names += NAME_LEN_MAX;
146 }
147
148 *pparts = mtd_parts;
149 ret = num_parts;
150
151 out_free_buf:
152 vfree(tab);
153 out:
154 return ret;
155 }
156
157 static struct mtd_part_parser mylo_mtd_parser = {
158 .owner = THIS_MODULE,
159 .parse_fn = parse_myloader_partitions,
160 .name = NAME_MYLOADER,
161 };
162
163 static int __init mylo_mtd_parser_init(void)
164 {
165 return register_mtd_parser(&mylo_mtd_parser);
166 }
167
168 static void __exit mylo_mtd_parser_exit(void)
169 {
170 deregister_mtd_parser(&mylo_mtd_parser);
171 }
172
173 module_init(mylo_mtd_parser_init);
174 module_exit(mylo_mtd_parser_exit);
175
176 MODULE_AUTHOR("Gabor Juhos <juhosg at openwrt.org>");
177 MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
178 MODULE_LICENSE("GPL");
This page took 0.057563 seconds and 5 git commands to generate.