VServer 1.9.2 (patch-2.6.8.1-vs1.9.2.diff)
[linux-2.6.git] / drivers / mtd / redboot.c
1 /*
2  * $Id: redboot.c,v 1.15 2004/08/10 07:55:16 dwmw2 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 inline int redboot_checksum(struct fis_image_desc *img)
34 {
35         /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
36         return 1;
37 }
38
39 static int parse_redboot_partitions(struct mtd_info *master, 
40                              struct mtd_partition **pparts,
41                              unsigned long fis_origin)
42 {
43         int nrparts = 0;
44         struct fis_image_desc *buf;
45         struct mtd_partition *parts;
46         struct fis_list *fl = NULL, *tmp_fl;
47         int ret, i;
48         size_t retlen;
49         char *names;
50         char *nullname;
51         int namelen = 0;
52         int nulllen = 0;
53 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
54         static char nullstring[] = "unallocated";
55 #endif
56
57         buf = vmalloc(master->erasesize);
58
59         if (!buf)
60                 return -ENOMEM;
61
62         /* Read the start of the last erase block */
63         ret = master->read(master, master->size - master->erasesize,
64                            master->erasesize, &retlen, (void *)buf);
65
66         if (ret)
67                 goto out;
68
69         if (retlen != master->erasesize) {
70                 ret = -EIO;
71                 goto out;
72         }
73
74         /* RedBoot image could appear in any of the first three slots */
75         for (i = 0; i < 3; i++) {
76                 if (!memcmp(buf[i].name, "RedBoot", 8))
77                         break;
78         }
79         if (i == 3) {
80                 /* Didn't find it */
81                 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
82                        master->name);
83                 ret = 0;
84                 goto out;
85         }
86
87         for (i = 0; i < master->erasesize / sizeof(struct fis_image_desc); i++) {
88                 struct fis_list *new_fl, **prev;
89
90                 if (buf[i].name[0] == 0xff)
91                         break;
92                 if (!redboot_checksum(&buf[i]))
93                         break;
94
95                 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
96                 namelen += strlen(buf[i].name)+1;
97                 if (!new_fl) {
98                         ret = -ENOMEM;
99                         goto out;
100                 }
101                 new_fl->img = &buf[i];
102                 if (fis_origin) {
103                         buf[i].flash_base -= fis_origin;
104                 } else {
105                         buf[i].flash_base &= master->size-1;
106                 }
107
108                 /* I'm sure the JFFS2 code has done me permanent damage.
109                  * I now think the following is _normal_
110                  */
111                 prev = &fl;
112                 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
113                         prev = &(*prev)->next;
114                 new_fl->next = *prev;
115                 *prev = new_fl;
116
117                 nrparts++;
118         }
119 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
120         if (fl->img->flash_base) {
121                 nrparts++;
122                 nulllen = sizeof(nullstring);
123         }
124
125         for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
126                 if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
127                         nrparts++;
128                         nulllen = sizeof(nullstring);
129                 }
130         }
131 #endif
132         parts = kmalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
133
134         if (!parts) {
135                 ret = -ENOMEM;
136                 goto out;
137         }
138
139         memset(parts, 0, sizeof(*parts)*nrparts + nulllen + namelen);
140
141         nullname = (char *)&parts[nrparts];
142 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
143         if (nulllen > 0) {
144                 strcpy(nullname, nullstring);
145         }
146 #endif
147         names = nullname + nulllen;
148
149         i=0;
150
151 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
152         if (fl->img->flash_base) {
153                parts[0].name = nullname;
154                parts[0].size = fl->img->flash_base;
155                parts[0].offset = 0;
156                 i++;
157         }
158 #endif
159         for ( ; i<nrparts; i++) {
160                 parts[i].size = fl->img->size;
161                 parts[i].offset = fl->img->flash_base;
162                 parts[i].name = names;
163
164                 strcpy(names, fl->img->name);
165 #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
166                 if (!memcmp(names, "RedBoot", 8) ||
167                                 !memcmp(names, "RedBoot config", 15) ||
168                                 !memcmp(names, "FIS directory", 14)) {
169                         parts[i].mask_flags = MTD_WRITEABLE;
170                 }
171 #endif
172                 names += strlen(names)+1;
173
174 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
175                 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
176                         i++;
177                         parts[i].offset = parts[i-1].size + parts[i-1].offset;
178                         parts[i].size = fl->next->img->flash_base - parts[i].offset;
179                         parts[i].name = nullname;
180                 }
181 #endif
182                 tmp_fl = fl;
183                 fl = fl->next;
184                 kfree(tmp_fl);
185         }
186         ret = nrparts;
187         *pparts = parts;
188  out:
189         while (fl) {
190                 struct fis_list *old = fl;
191                 fl = fl->next;
192                 kfree(old);
193         }
194         vfree(buf);
195         return ret;
196 }
197
198 static struct mtd_part_parser redboot_parser = {
199         .owner = THIS_MODULE,
200         .parse_fn = parse_redboot_partitions,
201         .name = "RedBoot",
202 };
203
204 static int __init redboot_parser_init(void)
205 {
206         return register_mtd_parser(&redboot_parser);
207 }
208
209 static void __exit redboot_parser_exit(void)
210 {
211         deregister_mtd_parser(&redboot_parser);
212 }
213
214 module_init(redboot_parser_init);
215 module_exit(redboot_parser_exit);
216
217 MODULE_LICENSE("GPL");
218 MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
219 MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");