[kernel] 2.6.27: revert case insensitive match support in xt_string while we are...
[openwrt.git] / target / linux / adm5120 / files-2.6.26 / 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-2008 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/slab.h>
18 #include <linux/init.h>
19 #include <linux/vmalloc.h>
20
21 #include <linux/mtd/mtd.h>
22 #include <linux/mtd/partitions.h>
23
24 #include <linux/byteorder/generic.h>
25
26 #include <prom/myloader.h>
27
28 #define NAME_LEN_MAX            20
29 #define NAME_MYLOADER           "MyLoader"
30 #define NAME_PARTITION_TABLE    "Partition Table"
31 #define BLOCK_LEN_MIN           0x10000
32
33 int parse_myloader_partitions(struct mtd_info *master,
34                         struct mtd_partition **pparts,
35                         unsigned long origin)
36 {
37         struct mylo_partition_table *tab;
38         struct mylo_partition *part;
39         struct mtd_partition *mtd_parts;
40         struct mtd_partition *mtd_part;
41         int num_parts;
42         int ret, i;
43         size_t retlen;
44         char *names;
45         unsigned long offset;
46         unsigned long blocklen;
47
48         tab = vmalloc(sizeof(*tab));
49         if (!tab) {
50                 return -ENOMEM;
51                 goto out;
52         }
53
54         blocklen = master->erasesize;
55         if (blocklen < BLOCK_LEN_MIN)
56                 blocklen = BLOCK_LEN_MIN;
57
58         /* Partition Table is always located on the second erase block */
59         offset = blocklen;
60         printk(KERN_NOTICE "%s: searching for MyLoader partition table at "
61                         "offset 0x%lx\n", master->name, offset);
62
63         ret = master->read(master, offset, sizeof(*tab), &retlen, (void *)tab);
64         if (ret)
65                 goto out;
66
67         if (retlen != sizeof(*tab)) {
68                 ret = -EIO;
69                 goto out_free_buf;
70         }
71
72         /* Check for Partition Table magic number */
73         if (tab->magic != le32_to_cpu(MYLO_MAGIC_PARTITIONS)) {
74                 printk(KERN_NOTICE "%s: no MyLoader partition table found\n",
75                         master->name);
76                 ret = 0;
77                 goto out_free_buf;
78         }
79
80         /* The MyLoader and the Partition Table is always present */
81         num_parts = 2;
82
83         /* Detect number of used partitions */
84         for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
85                 part = &tab->partitions[i];
86
87                 if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
88                         continue;
89
90                 num_parts++;
91         }
92
93         mtd_parts = kzalloc((num_parts * sizeof(*mtd_part) +
94                                 num_parts * NAME_LEN_MAX), GFP_KERNEL);
95
96         if (!mtd_parts) {
97                 ret = -ENOMEM;
98                 goto out_free_buf;
99         }
100
101         mtd_part = mtd_parts;
102         names = (char *)&mtd_parts[num_parts];
103
104         strncpy(names, NAME_MYLOADER, NAME_LEN_MAX-1);
105         mtd_part->name = names;
106         mtd_part->offset = 0;
107         mtd_part->size = blocklen;
108         mtd_part->mask_flags = MTD_WRITEABLE;
109         mtd_part++;
110         names += NAME_LEN_MAX;
111
112         strncpy(names, NAME_PARTITION_TABLE, NAME_LEN_MAX-1);
113         mtd_part->name = names;
114         mtd_part->offset = blocklen;
115         mtd_part->size = blocklen;
116         mtd_part->mask_flags = MTD_WRITEABLE;
117         mtd_part++;
118         names += NAME_LEN_MAX;
119
120         for (i = 0; i < MYLO_MAX_PARTITIONS; i++) {
121                 part = &tab->partitions[i];
122
123                 if (le16_to_cpu(part->type) == PARTITION_TYPE_FREE)
124                         continue;
125
126                 sprintf(names, "partition%d", i);
127                 mtd_part->offset = le32_to_cpu(part->addr);
128                 mtd_part->size = le32_to_cpu(part->size);
129                 mtd_part->name = names;
130                 mtd_part++;
131                 names += NAME_LEN_MAX;
132         }
133
134         *pparts = mtd_parts;
135         ret = num_parts;
136
137 out_free_buf:
138         vfree(tab);
139 out:
140         return ret;
141 }
142
143 static struct mtd_part_parser mylo_mtd_parser = {
144         .owner = THIS_MODULE,
145         .parse_fn = parse_myloader_partitions,
146         .name = NAME_MYLOADER,
147 };
148
149 static int __init mylo_mtd_parser_init(void)
150 {
151         return register_mtd_parser(&mylo_mtd_parser);
152 }
153
154 static void __exit mylo_mtd_parser_exit(void)
155 {
156         deregister_mtd_parser(&mylo_mtd_parser);
157 }
158
159 module_init(mylo_mtd_parser_init);
160 module_exit(mylo_mtd_parser_exit);
161
162 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
163 MODULE_DESCRIPTION("Parsing code for MyLoader partition tables");
164 MODULE_LICENSE("GPL v2");