[PATCH] drivers/mtd/redboot.c: recognise a foreign byte sex partition table
[linux-2.6] / drivers / mtd / redboot.c
1 /*
2  * $Id: redboot.c,v 1.18 2005/11/07 11:14:21 gleixner Exp $
3  *
4  * Parse RedBoot-style Flash Image System (FIS) tables and
5  * produce a Linux partition array to match.
6  */
7
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/init.h>
11 #include <linux/vmalloc.h>
12
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/partitions.h>
15
16 struct fis_image_desc {
17     unsigned char name[16];      // Null terminated name
18     unsigned long flash_base;    // Address within FLASH of image
19     unsigned long mem_base;      // Address in memory where it executes
20     unsigned long size;          // Length of image
21     unsigned long entry_point;   // Execution entry point
22     unsigned long data_length;   // Length of actual data
23     unsigned char _pad[256-(16+7*sizeof(unsigned long))];
24     unsigned long desc_cksum;    // Checksum over image descriptor
25     unsigned long file_cksum;    // Checksum over image data
26 };
27
28 struct fis_list {
29         struct fis_image_desc *img;
30         struct fis_list *next;
31 };
32
33 static int directory = CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK;
34 module_param(directory, int, 0);
35
36 static inline int redboot_checksum(struct fis_image_desc *img)
37 {
38         /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
39         return 1;
40 }
41
42 static int parse_redboot_partitions(struct mtd_info *master,
43                              struct mtd_partition **pparts,
44                              unsigned long fis_origin)
45 {
46         int nrparts = 0;
47         struct fis_image_desc *buf;
48         struct mtd_partition *parts;
49         struct fis_list *fl = NULL, *tmp_fl;
50         int ret, i;
51         size_t retlen;
52         char *names;
53         char *nullname;
54         int namelen = 0;
55         int nulllen = 0;
56         int numslots;
57         unsigned long offset;
58 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
59         static char nullstring[] = "unallocated";
60 #endif
61
62         buf = vmalloc(master->erasesize);
63
64         if (!buf)
65                 return -ENOMEM;
66
67         if ( directory < 0 )
68                 offset = master->size + directory*master->erasesize;
69         else
70                 offset = directory*master->erasesize;
71
72         printk(KERN_NOTICE "Searching for RedBoot partition table in %s at offset 0x%lx\n",
73                master->name, offset);
74
75         ret = master->read(master, offset,
76                            master->erasesize, &retlen, (void *)buf);
77
78         if (ret)
79                 goto out;
80
81         if (retlen != master->erasesize) {
82                 ret = -EIO;
83                 goto out;
84         }
85
86         numslots = (master->erasesize / sizeof(struct fis_image_desc));
87         for (i = 0; i < numslots; i++) {
88                 if (buf[i].name[0] == 0xff) {
89                         i = numslots;
90                         break;
91                 }
92                 if (!memcmp(buf[i].name, "FIS directory", 14)) {
93                         /* This is apparently the FIS directory entry for the
94                          * FIS directory itself.  The FIS directory size is
95                          * one erase block, if the buf[i].size field is
96                          * swab32(erasesize) then we know we are looking at
97                          * a byte swapped FIS directory - swap all the entries!
98                          * (NOTE: this is 'size' not 'data_length', size is
99                          * the full size of the entry.)
100                          */
101                         if (swab32(buf[i].size) == master->erasesize) {
102                                 int j;
103                                 for (j = 0; j < numslots && buf[j].name[0] != 0xff; ++j) {
104                                         /* The unsigned long fields were written with the
105                                          * wrong byte sex, name and pad have no byte sex.
106                                          */
107 #                                       define do_swab32(x) (x) = swab32(x)
108                                         do_swab32(buf[j].flash_base);
109                                         do_swab32(buf[j].mem_base);
110                                         do_swab32(buf[j].size);
111                                         do_swab32(buf[j].entry_point);
112                                         do_swab32(buf[j].data_length);
113                                         do_swab32(buf[j].desc_cksum);
114                                         do_swab32(buf[j].file_cksum);
115 #                                       undef do_swab32
116                                 }
117                         }
118                         break;
119                 }
120         }
121         if (i == numslots) {
122                 /* Didn't find it */
123                 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
124                        master->name);
125                 ret = 0;
126                 goto out;
127         }
128
129         for (i = 0; i < numslots; i++) {
130                 struct fis_list *new_fl, **prev;
131
132                 if (buf[i].name[0] == 0xff)
133                         break;
134                 if (!redboot_checksum(&buf[i]))
135                         break;
136
137                 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
138                 namelen += strlen(buf[i].name)+1;
139                 if (!new_fl) {
140                         ret = -ENOMEM;
141                         goto out;
142                 }
143                 new_fl->img = &buf[i];
144                 if (fis_origin) {
145                         buf[i].flash_base -= fis_origin;
146                 } else {
147                         buf[i].flash_base &= master->size-1;
148                 }
149
150                 /* I'm sure the JFFS2 code has done me permanent damage.
151                  * I now think the following is _normal_
152                  */
153                 prev = &fl;
154                 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
155                         prev = &(*prev)->next;
156                 new_fl->next = *prev;
157                 *prev = new_fl;
158
159                 nrparts++;
160         }
161 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
162         if (fl->img->flash_base) {
163                 nrparts++;
164                 nulllen = sizeof(nullstring);
165         }
166
167         for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
168                 if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
169                         nrparts++;
170                         nulllen = sizeof(nullstring);
171                 }
172         }
173 #endif
174         parts = kmalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
175
176         if (!parts) {
177                 ret = -ENOMEM;
178                 goto out;
179         }
180
181         memset(parts, 0, sizeof(*parts)*nrparts + nulllen + namelen);
182
183         nullname = (char *)&parts[nrparts];
184 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
185         if (nulllen > 0) {
186                 strcpy(nullname, nullstring);
187         }
188 #endif
189         names = nullname + nulllen;
190
191         i=0;
192
193 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
194         if (fl->img->flash_base) {
195                parts[0].name = nullname;
196                parts[0].size = fl->img->flash_base;
197                parts[0].offset = 0;
198                 i++;
199         }
200 #endif
201         for ( ; i<nrparts; i++) {
202                 parts[i].size = fl->img->size;
203                 parts[i].offset = fl->img->flash_base;
204                 parts[i].name = names;
205
206                 strcpy(names, fl->img->name);
207 #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
208                 if (!memcmp(names, "RedBoot", 8) ||
209                                 !memcmp(names, "RedBoot config", 15) ||
210                                 !memcmp(names, "FIS directory", 14)) {
211                         parts[i].mask_flags = MTD_WRITEABLE;
212                 }
213 #endif
214                 names += strlen(names)+1;
215
216 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
217                 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
218                         i++;
219                         parts[i].offset = parts[i-1].size + parts[i-1].offset;
220                         parts[i].size = fl->next->img->flash_base - parts[i].offset;
221                         parts[i].name = nullname;
222                 }
223 #endif
224                 tmp_fl = fl;
225                 fl = fl->next;
226                 kfree(tmp_fl);
227         }
228         ret = nrparts;
229         *pparts = parts;
230  out:
231         while (fl) {
232                 struct fis_list *old = fl;
233                 fl = fl->next;
234                 kfree(old);
235         }
236         vfree(buf);
237         return ret;
238 }
239
240 static struct mtd_part_parser redboot_parser = {
241         .owner = THIS_MODULE,
242         .parse_fn = parse_redboot_partitions,
243         .name = "RedBoot",
244 };
245
246 static int __init redboot_parser_init(void)
247 {
248         return register_mtd_parser(&redboot_parser);
249 }
250
251 static void __exit redboot_parser_exit(void)
252 {
253         deregister_mtd_parser(&redboot_parser);
254 }
255
256 module_init(redboot_parser_init);
257 module_exit(redboot_parser_exit);
258
259 MODULE_LICENSE("GPL");
260 MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
261 MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");