sis900: come alive after temporary memory shortage
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / drivers / mtd / redboot.c
blob13f9e992bef8bb0bc5b5801a1dd9207bdeb2b85e
1 /*
2 * $Id: redboot.c,v 1.17 2004/11/22 11:33:56 ijc Exp $
4 * Parse RedBoot-style Flash Image System (FIS) tables and
5 * produce a Linux partition array to match.
6 */
8 #include <linux/kernel.h>
9 #include <linux/slab.h>
10 #include <linux/init.h>
11 #include <linux/vmalloc.h>
13 #include <linux/mtd/mtd.h>
14 #include <linux/mtd/partitions.h>
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
28 struct fis_list {
29 struct fis_image_desc *img;
30 struct fis_list *next;
33 static int directory = CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK;
34 module_param(directory, int, 0);
36 static inline int redboot_checksum(struct fis_image_desc *img)
38 /* RedBoot doesn't actually write the desc_cksum field yet AFAICT */
39 return 1;
42 static int parse_redboot_partitions(struct mtd_info *master,
43 struct mtd_partition **pparts,
44 unsigned long fis_origin)
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
62 buf = vmalloc(master->erasesize);
64 if (!buf)
65 return -ENOMEM;
67 if ( directory < 0 )
68 offset = master->size + directory*master->erasesize;
69 else
70 offset = directory*master->erasesize;
72 printk(KERN_NOTICE "Searching for RedBoot partition table in %s at offset 0x%lx\n",
73 master->name, offset);
75 ret = master->read(master, offset,
76 master->erasesize, &retlen, (void *)buf);
78 if (ret)
79 goto out;
81 if (retlen != master->erasesize) {
82 ret = -EIO;
83 goto out;
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;
92 if (!memcmp(buf[i].name, "FIS directory", 14))
93 break;
95 if (i == numslots) {
96 /* Didn't find it */
97 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
98 master->name);
99 ret = 0;
100 goto out;
103 for (i = 0; i < numslots; i++) {
104 struct fis_list *new_fl, **prev;
106 if (buf[i].name[0] == 0xff)
107 break;
108 if (!redboot_checksum(&buf[i]))
109 break;
111 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
112 namelen += strlen(buf[i].name)+1;
113 if (!new_fl) {
114 ret = -ENOMEM;
115 goto out;
117 new_fl->img = &buf[i];
118 if (fis_origin) {
119 buf[i].flash_base -= fis_origin;
120 } else {
121 buf[i].flash_base &= master->size-1;
124 /* I'm sure the JFFS2 code has done me permanent damage.
125 * I now think the following is _normal_
127 prev = &fl;
128 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
129 prev = &(*prev)->next;
130 new_fl->next = *prev;
131 *prev = new_fl;
133 nrparts++;
135 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
136 if (fl->img->flash_base) {
137 nrparts++;
138 nulllen = sizeof(nullstring);
141 for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
142 if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
143 nrparts++;
144 nulllen = sizeof(nullstring);
147 #endif
148 parts = kmalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
150 if (!parts) {
151 ret = -ENOMEM;
152 goto out;
155 memset(parts, 0, sizeof(*parts)*nrparts + nulllen + namelen);
157 nullname = (char *)&parts[nrparts];
158 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
159 if (nulllen > 0) {
160 strcpy(nullname, nullstring);
162 #endif
163 names = nullname + nulllen;
165 i=0;
167 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
168 if (fl->img->flash_base) {
169 parts[0].name = nullname;
170 parts[0].size = fl->img->flash_base;
171 parts[0].offset = 0;
172 i++;
174 #endif
175 for ( ; i<nrparts; i++) {
176 parts[i].size = fl->img->size;
177 parts[i].offset = fl->img->flash_base;
178 parts[i].name = names;
180 strcpy(names, fl->img->name);
181 #ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
182 if (!memcmp(names, "RedBoot", 8) ||
183 !memcmp(names, "RedBoot config", 15) ||
184 !memcmp(names, "FIS directory", 14)) {
185 parts[i].mask_flags = MTD_WRITEABLE;
187 #endif
188 names += strlen(names)+1;
190 #ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
191 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
192 i++;
193 parts[i].offset = parts[i-1].size + parts[i-1].offset;
194 parts[i].size = fl->next->img->flash_base - parts[i].offset;
195 parts[i].name = nullname;
197 #endif
198 tmp_fl = fl;
199 fl = fl->next;
200 kfree(tmp_fl);
202 ret = nrparts;
203 *pparts = parts;
204 out:
205 while (fl) {
206 struct fis_list *old = fl;
207 fl = fl->next;
208 kfree(old);
210 vfree(buf);
211 return ret;
214 static struct mtd_part_parser redboot_parser = {
215 .owner = THIS_MODULE,
216 .parse_fn = parse_redboot_partitions,
217 .name = "RedBoot",
220 static int __init redboot_parser_init(void)
222 return register_mtd_parser(&redboot_parser);
225 static void __exit redboot_parser_exit(void)
227 deregister_mtd_parser(&redboot_parser);
230 module_init(redboot_parser_init);
231 module_exit(redboot_parser_exit);
233 MODULE_LICENSE("GPL");
234 MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
235 MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");