x86: Fix duplicated UV BAU interrupt vector
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / redboot.c
blob2d600a1bf2aa76e804679e2df284d8b7f9be2742
1 /*
2 * Parse RedBoot-style Flash Image System (FIS) tables and
3 * produce a Linux partition array to match.
4 */
6 #include <linux/kernel.h>
7 #include <linux/slab.h>
8 #include <linux/init.h>
9 #include <linux/vmalloc.h>
11 #include <linux/mtd/mtd.h>
12 #include <linux/mtd/partitions.h>
14 struct fis_image_desc {
15 unsigned char name[16]; // Null terminated name
16 uint32_t flash_base; // Address within FLASH of image
17 uint32_t mem_base; // Address in memory where it executes
18 uint32_t size; // Length of image
19 uint32_t entry_point; // Execution entry point
20 uint32_t data_length; // Length of actual data
21 unsigned char _pad[256-(16+7*sizeof(uint32_t))];
22 uint32_t desc_cksum; // Checksum over image descriptor
23 uint32_t file_cksum; // Checksum over image data
26 struct fis_list {
27 struct fis_image_desc *img;
28 struct fis_list *next;
31 static int directory = CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK;
32 module_param(directory, int, 0);
34 static inline int redboot_checksum(struct fis_image_desc *img)
36 /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
37 return 1;
40 static int parse_redboot_partitions(struct mtd_info *master,
41 struct mtd_partition **pparts,
42 unsigned long fis_origin)
44 int nrparts = 0;
45 struct fis_image_desc *buf;
46 struct mtd_partition *parts;
47 struct fis_list *fl = NULL, *tmp_fl;
48 int ret, i;
49 size_t retlen;
50 char *names;
51 char *nullname;
52 int namelen = 0;
53 int nulllen = 0;
54 int numslots;
55 unsigned long offset;
56 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
57 static char nullstring[] = "unallocated";
58 #endif
60 if ( directory < 0 ) {
61 offset = master->size + directory * master->erasesize;
62 while (master->block_isbad &&
63 master->block_isbad(master, offset)) {
64 if (!offset) {
65 nogood:
66 printk(KERN_NOTICE "Failed to find a non-bad block to check for RedBoot partition table\n");
67 return -EIO;
69 offset -= master->erasesize;
71 } else {
72 offset = directory * master->erasesize;
73 while (master->block_isbad &&
74 master->block_isbad(master, offset)) {
75 offset += master->erasesize;
76 if (offset == master->size)
77 goto nogood;
80 buf = vmalloc(master->erasesize);
82 if (!buf)
83 return -ENOMEM;
85 printk(KERN_NOTICE "Searching for RedBoot partition table in %s at offset 0x%lx\n",
86 master->name, offset);
88 ret = master->read(master, offset,
89 master->erasesize, &retlen, (void *)buf);
91 if (ret)
92 goto out;
94 if (retlen != master->erasesize) {
95 ret = -EIO;
96 goto out;
99 numslots = (master->erasesize / sizeof(struct fis_image_desc));
100 for (i = 0; i < numslots; i++) {
101 if (!memcmp(buf[i].name, "FIS directory", 14)) {
102 /* This is apparently the FIS directory entry for the
103 * FIS directory itself. The FIS directory size is
104 * one erase block; if the buf[i].size field is
105 * swab32(erasesize) then we know we are looking at
106 * a byte swapped FIS directory - swap all the entries!
107 * (NOTE: this is 'size' not 'data_length'; size is
108 * the full size of the entry.)
111 /* RedBoot can combine the FIS directory and
112 config partitions into a single eraseblock;
113 we assume wrong-endian if either the swapped
114 'size' matches the eraseblock size precisely,
115 or if the swapped size actually fits in an
116 eraseblock while the unswapped size doesn't. */
117 if (swab32(buf[i].size) == master->erasesize ||
118 (buf[i].size > master->erasesize
119 && swab32(buf[i].size) < master->erasesize)) {
120 int j;
121 /* Update numslots based on actual FIS directory size */
122 numslots = swab32(buf[i].size) / sizeof (struct fis_image_desc);
123 for (j = 0; j < numslots; ++j) {
125 /* A single 0xff denotes a deleted entry.
126 * Two of them in a row is the end of the table.
128 if (buf[j].name[0] == 0xff) {
129 if (buf[j].name[1] == 0xff) {
130 break;
131 } else {
132 continue;
136 /* The unsigned long fields were written with the
137 * wrong byte sex, name and pad have no byte sex.
139 swab32s(&buf[j].flash_base);
140 swab32s(&buf[j].mem_base);
141 swab32s(&buf[j].size);
142 swab32s(&buf[j].entry_point);
143 swab32s(&buf[j].data_length);
144 swab32s(&buf[j].desc_cksum);
145 swab32s(&buf[j].file_cksum);
147 } else if (buf[i].size < master->erasesize) {
148 /* Update numslots based on actual FIS directory size */
149 numslots = buf[i].size / sizeof(struct fis_image_desc);
151 break;
154 if (i == numslots) {
155 /* Didn't find it */
156 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
157 master->name);
158 ret = 0;
159 goto out;
162 for (i = 0; i < numslots; i++) {
163 struct fis_list *new_fl, **prev;
165 if (buf[i].name[0] == 0xff) {
166 if (buf[i].name[1] == 0xff) {
167 break;
168 } else {
169 continue;
172 if (!redboot_checksum(&buf[i]))
173 break;
175 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
176 namelen += strlen(buf[i].name)+1;
177 if (!new_fl) {
178 ret = -ENOMEM;
179 goto out;
181 new_fl->img = &buf[i];
182 if (fis_origin) {
183 buf[i].flash_base -= fis_origin;
184 } else {
185 buf[i].flash_base &= master->size-1;
188 /* I'm sure the JFFS2 code has done me permanent damage.
189 * I now think the following is _normal_
191 prev = &fl;
192 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
193 prev = &(*prev)->next;
194 new_fl->next = *prev;
195 *prev = new_fl;
197 nrparts++;
199 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
200 if (fl->img->flash_base) {
201 nrparts++;
202 nulllen = sizeof(nullstring);
205 for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
206 if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
207 nrparts++;
208 nulllen = sizeof(nullstring);
211 #endif
212 parts = kzalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
214 if (!parts) {
215 ret = -ENOMEM;
216 goto out;
219 nullname = (char *)&parts[nrparts];
220 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
221 if (nulllen > 0) {
222 strcpy(nullname, nullstring);
224 #endif
225 names = nullname + nulllen;
227 i=0;
229 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
230 if (fl->img->flash_base) {
231 parts[0].name = nullname;
232 parts[0].size = fl->img->flash_base;
233 parts[0].offset = 0;
234 i++;
236 #endif
237 for ( ; i<nrparts; i++) {
238 parts[i].size = fl->img->size;
239 parts[i].offset = fl->img->flash_base;
240 parts[i].name = names;
242 strcpy(names, fl->img->name);
243 #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
244 if (!memcmp(names, "RedBoot", 8) ||
245 !memcmp(names, "RedBoot config", 15) ||
246 !memcmp(names, "FIS directory", 14)) {
247 parts[i].mask_flags = MTD_WRITEABLE;
249 #endif
250 names += strlen(names)+1;
252 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
253 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
254 i++;
255 parts[i].offset = parts[i-1].size + parts[i-1].offset;
256 parts[i].size = fl->next->img->flash_base - parts[i].offset;
257 parts[i].name = nullname;
259 #endif
260 tmp_fl = fl;
261 fl = fl->next;
262 kfree(tmp_fl);
264 ret = nrparts;
265 *pparts = parts;
266 out:
267 while (fl) {
268 struct fis_list *old = fl;
269 fl = fl->next;
270 kfree(old);
272 vfree(buf);
273 return ret;
276 static struct mtd_part_parser redboot_parser = {
277 .owner = THIS_MODULE,
278 .parse_fn = parse_redboot_partitions,
279 .name = "RedBoot",
282 static int __init redboot_parser_init(void)
284 return register_mtd_parser(&redboot_parser);
287 static void __exit redboot_parser_exit(void)
289 deregister_mtd_parser(&redboot_parser);
292 module_init(redboot_parser_init);
293 module_exit(redboot_parser_exit);
295 MODULE_LICENSE("GPL");
296 MODULE_AUTHOR("David Woodhouse <dwmw2@infradead.org>");
297 MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");