Merge branch 'for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/dtor/input
[sfrench/cifs-2.6.git] / drivers / mtd / redboot.c
1 /*
2  * $Id: redboot.c,v 1.21 2006/03/30 18:34:37 bjd 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     uint32_t      flash_base;    // Address within FLASH of image
19     uint32_t      mem_base;      // Address in memory where it executes
20     uint32_t      size;          // Length of image
21     uint32_t      entry_point;   // Execution entry point
22     uint32_t      data_length;   // Length of actual data
23     unsigned char _pad[256-(16+7*sizeof(uint32_t))];
24     uint32_t      desc_cksum;    // Checksum over image descriptor
25     uint32_t      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         if ( directory < 0 ) {
63                 offset = master->size + directory * master->erasesize;
64                 while (master->block_isbad && 
65                        master->block_isbad(master, offset)) {
66                         if (!offset) {
67                         nogood:
68                                 printk(KERN_NOTICE "Failed to find a non-bad block to check for RedBoot partition table\n");
69                                 return -EIO;
70                         }
71                         offset -= master->erasesize;
72                 }
73         } else {
74                 offset = directory * master->erasesize;
75                 while (master->block_isbad && 
76                        master->block_isbad(master, offset)) {
77                         offset += master->erasesize;
78                         if (offset == master->size)
79                                 goto nogood;
80                 }
81         }
82         buf = vmalloc(master->erasesize);
83
84         if (!buf)
85                 return -ENOMEM;
86
87         printk(KERN_NOTICE "Searching for RedBoot partition table in %s at offset 0x%lx\n",
88                master->name, offset);
89
90         ret = master->read(master, offset,
91                            master->erasesize, &retlen, (void *)buf);
92
93         if (ret)
94                 goto out;
95
96         if (retlen != master->erasesize) {
97                 ret = -EIO;
98                 goto out;
99         }
100
101         numslots = (master->erasesize / sizeof(struct fis_image_desc));
102         for (i = 0; i < numslots; i++) {
103                 if (!memcmp(buf[i].name, "FIS directory", 14)) {
104                         /* This is apparently the FIS directory entry for the
105                          * FIS directory itself.  The FIS directory size is
106                          * one erase block; if the buf[i].size field is
107                          * swab32(erasesize) then we know we are looking at
108                          * a byte swapped FIS directory - swap all the entries!
109                          * (NOTE: this is 'size' not 'data_length'; size is
110                          * the full size of the entry.)
111                          */
112
113                         /* RedBoot can combine the FIS directory and
114                            config partitions into a single eraseblock;
115                            we assume wrong-endian if either the swapped
116                            'size' matches the eraseblock size precisely,
117                            or if the swapped size actually fits in an
118                            eraseblock while the unswapped size doesn't. */
119                         if (swab32(buf[i].size) == master->erasesize ||
120                             (buf[i].size > master->erasesize
121                              && swab32(buf[i].size) < master->erasesize)) {
122                                 int j;
123                                 /* Update numslots based on actual FIS directory size */
124                                 numslots = swab32(buf[i].size) / sizeof (struct fis_image_desc);
125                                 for (j = 0; j < numslots; ++j) {
126
127                                         /* A single 0xff denotes a deleted entry.
128                                          * Two of them in a row is the end of the table.
129                                          */
130                                         if (buf[j].name[0] == 0xff) {
131                                                 if (buf[j].name[1] == 0xff) {
132                                                         break;
133                                                 } else {
134                                                         continue;
135                                                 }
136                                         }
137
138                                         /* The unsigned long fields were written with the
139                                          * wrong byte sex, name and pad have no byte sex.
140                                          */
141                                         swab32s(&buf[j].flash_base);
142                                         swab32s(&buf[j].mem_base);
143                                         swab32s(&buf[j].size);
144                                         swab32s(&buf[j].entry_point);
145                                         swab32s(&buf[j].data_length);
146                                         swab32s(&buf[j].desc_cksum);
147                                         swab32s(&buf[j].file_cksum);
148                                 }
149                         } else if (buf[i].size < master->erasesize) {
150                                 /* Update numslots based on actual FIS directory size */
151                                 numslots = buf[i].size / sizeof(struct fis_image_desc);
152                         }
153                         break;
154                 }
155         }
156         if (i == numslots) {
157                 /* Didn't find it */
158                 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
159                        master->name);
160                 ret = 0;
161                 goto out;
162         }
163
164         for (i = 0; i < numslots; i++) {
165                 struct fis_list *new_fl, **prev;
166
167                 if (buf[i].name[0] == 0xff) {
168                         if (buf[i].name[1] == 0xff) {
169                                 break;
170                         } else {
171                                 continue;
172                         }
173                 }
174                 if (!redboot_checksum(&buf[i]))
175                         break;
176
177                 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
178                 namelen += strlen(buf[i].name)+1;
179                 if (!new_fl) {
180                         ret = -ENOMEM;
181                         goto out;
182                 }
183                 new_fl->img = &buf[i];
184                 if (fis_origin) {
185                         buf[i].flash_base -= fis_origin;
186                 } else {
187                         buf[i].flash_base &= master->size-1;
188                 }
189
190                 /* I'm sure the JFFS2 code has done me permanent damage.
191                  * I now think the following is _normal_
192                  */
193                 prev = &fl;
194                 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
195                         prev = &(*prev)->next;
196                 new_fl->next = *prev;
197                 *prev = new_fl;
198
199                 nrparts++;
200         }
201 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
202         if (fl->img->flash_base) {
203                 nrparts++;
204                 nulllen = sizeof(nullstring);
205         }
206
207         for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
208                 if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
209                         nrparts++;
210                         nulllen = sizeof(nullstring);
211                 }
212         }
213 #endif
214         parts = kzalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
215
216         if (!parts) {
217                 ret = -ENOMEM;
218                 goto out;
219         }
220
221         nullname = (char *)&parts[nrparts];
222 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
223         if (nulllen > 0) {
224                 strcpy(nullname, nullstring);
225         }
226 #endif
227         names = nullname + nulllen;
228
229         i=0;
230
231 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
232         if (fl->img->flash_base) {
233                parts[0].name = nullname;
234                parts[0].size = fl->img->flash_base;
235                parts[0].offset = 0;
236                 i++;
237         }
238 #endif
239         for ( ; i<nrparts; i++) {
240                 parts[i].size = fl->img->size;
241                 parts[i].offset = fl->img->flash_base;
242                 parts[i].name = names;
243
244                 strcpy(names, fl->img->name);
245 #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
246                 if (!memcmp(names, "RedBoot", 8) ||
247                                 !memcmp(names, "RedBoot config", 15) ||
248                                 !memcmp(names, "FIS directory", 14)) {
249                         parts[i].mask_flags = MTD_WRITEABLE;
250                 }
251 #endif
252                 names += strlen(names)+1;
253
254 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
255                 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
256                         i++;
257                         parts[i].offset = parts[i-1].size + parts[i-1].offset;
258                         parts[i].size = fl->next->img->flash_base - parts[i].offset;
259                         parts[i].name = nullname;
260                 }
261 #endif
262                 tmp_fl = fl;
263                 fl = fl->next;
264                 kfree(tmp_fl);
265         }
266         ret = nrparts;
267         *pparts = parts;
268  out:
269         while (fl) {
270                 struct fis_list *old = fl;
271                 fl = fl->next;
272                 kfree(old);
273         }
274         vfree(buf);
275         return ret;
276 }
277
278 static struct mtd_part_parser redboot_parser = {
279         .owner = THIS_MODULE,
280         .parse_fn = parse_redboot_partitions,
281         .name = "RedBoot",
282 };
283
284 static int __init redboot_parser_init(void)
285 {
286         return register_mtd_parser(&redboot_parser);
287 }
288
289 static void __exit redboot_parser_exit(void)
290 {
291         deregister_mtd_parser(&redboot_parser);
292 }
293
294 module_init(redboot_parser_init);
295 module_exit(redboot_parser_exit);
296
297 MODULE_LICENSE("GPL");
298 MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
299 MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");