Merge branch 'for-linus' of master.kernel.org:/pub/scm/linux/kernel/git/roland/infiniband
[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         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 (!memcmp(buf[i].name, "FIS directory", 14)) {
89                         /* This is apparently the FIS directory entry for the
90                          * FIS directory itself.  The FIS directory size is
91                          * one erase block; if the buf[i].size field is
92                          * swab32(erasesize) then we know we are looking at
93                          * a byte swapped FIS directory - swap all the entries!
94                          * (NOTE: this is 'size' not 'data_length'; size is
95                          * the full size of the entry.)
96                          */
97                         if (swab32(buf[i].size) == master->erasesize) {
98                                 int j;
99                                 for (j = 0; j < numslots; ++j) {
100
101                                         /* A single 0xff denotes a deleted entry.
102                                          * Two of them in a row is the end of the table.
103                                          */
104                                         if (buf[j].name[0] == 0xff) {
105                                                 if (buf[j].name[1] == 0xff) {
106                                                         break;
107                                                 } else {
108                                                         continue;
109                                                 }
110                                         }
111
112                                         /* The unsigned long fields were written with the
113                                          * wrong byte sex, name and pad have no byte sex.
114                                          */
115                                         swab32s(&buf[j].flash_base);
116                                         swab32s(&buf[j].mem_base);
117                                         swab32s(&buf[j].size);
118                                         swab32s(&buf[j].entry_point);
119                                         swab32s(&buf[j].data_length);
120                                         swab32s(&buf[j].desc_cksum);
121                                         swab32s(&buf[j].file_cksum);
122                                 }
123                         }
124                         break;
125                 } else {
126                         /* re-calculate of real numslots */
127                         numslots = buf[i].size / sizeof(struct fis_image_desc);
128                 }
129         }
130         if (i == numslots) {
131                 /* Didn't find it */
132                 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
133                        master->name);
134                 ret = 0;
135                 goto out;
136         }
137
138         for (i = 0; i < numslots; i++) {
139                 struct fis_list *new_fl, **prev;
140
141                 if (buf[i].name[0] == 0xff) {
142                         if (buf[i].name[1] == 0xff) {
143                                 break;
144                         } else {
145                                 continue;
146                         }
147                 }
148                 if (!redboot_checksum(&buf[i]))
149                         break;
150
151                 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
152                 namelen += strlen(buf[i].name)+1;
153                 if (!new_fl) {
154                         ret = -ENOMEM;
155                         goto out;
156                 }
157                 new_fl->img = &buf[i];
158                 if (fis_origin) {
159                         buf[i].flash_base -= fis_origin;
160                 } else {
161                         buf[i].flash_base &= master->size-1;
162                 }
163
164                 /* I'm sure the JFFS2 code has done me permanent damage.
165                  * I now think the following is _normal_
166                  */
167                 prev = &fl;
168                 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
169                         prev = &(*prev)->next;
170                 new_fl->next = *prev;
171                 *prev = new_fl;
172
173                 nrparts++;
174         }
175 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
176         if (fl->img->flash_base) {
177                 nrparts++;
178                 nulllen = sizeof(nullstring);
179         }
180
181         for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
182                 if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
183                         nrparts++;
184                         nulllen = sizeof(nullstring);
185                 }
186         }
187 #endif
188         parts = kzalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
189
190         if (!parts) {
191                 ret = -ENOMEM;
192                 goto out;
193         }
194
195         nullname = (char *)&parts[nrparts];
196 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
197         if (nulllen > 0) {
198                 strcpy(nullname, nullstring);
199         }
200 #endif
201         names = nullname + nulllen;
202
203         i=0;
204
205 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
206         if (fl->img->flash_base) {
207                parts[0].name = nullname;
208                parts[0].size = fl->img->flash_base;
209                parts[0].offset = 0;
210                 i++;
211         }
212 #endif
213         for ( ; i<nrparts; i++) {
214                 parts[i].size = fl->img->size;
215                 parts[i].offset = fl->img->flash_base;
216                 parts[i].name = names;
217
218                 strcpy(names, fl->img->name);
219 #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
220                 if (!memcmp(names, "RedBoot", 8) ||
221                                 !memcmp(names, "RedBoot config", 15) ||
222                                 !memcmp(names, "FIS directory", 14)) {
223                         parts[i].mask_flags = MTD_WRITEABLE;
224                 }
225 #endif
226                 names += strlen(names)+1;
227
228 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
229                 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
230                         i++;
231                         parts[i].offset = parts[i-1].size + parts[i-1].offset;
232                         parts[i].size = fl->next->img->flash_base - parts[i].offset;
233                         parts[i].name = nullname;
234                 }
235 #endif
236                 tmp_fl = fl;
237                 fl = fl->next;
238                 kfree(tmp_fl);
239         }
240         ret = nrparts;
241         *pparts = parts;
242  out:
243         while (fl) {
244                 struct fis_list *old = fl;
245                 fl = fl->next;
246                 kfree(old);
247         }
248         vfree(buf);
249         return ret;
250 }
251
252 static struct mtd_part_parser redboot_parser = {
253         .owner = THIS_MODULE,
254         .parse_fn = parse_redboot_partitions,
255         .name = "RedBoot",
256 };
257
258 static int __init redboot_parser_init(void)
259 {
260         return register_mtd_parser(&redboot_parser);
261 }
262
263 static void __exit redboot_parser_exit(void)
264 {
265         deregister_mtd_parser(&redboot_parser);
266 }
267
268 module_init(redboot_parser_init);
269 module_exit(redboot_parser_exit);
270
271 MODULE_LICENSE("GPL");
272 MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
273 MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");