[MTD] NAND: Support for 16-bit bus-width on AT91.
[deliverable/linux.git] / drivers / mtd / redboot.c
CommitLineData
1da177e4 1/*
b020bb7d 2 * $Id: redboot.c,v 1.21 2006/03/30 18:34:37 bjd Exp $
1da177e4
LT
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
16struct fis_image_desc {
17 unsigned char name[16]; // Null terminated name
b020bb7d
BD
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
1da177e4
LT
26};
27
28struct fis_list {
29 struct fis_image_desc *img;
30 struct fis_list *next;
31};
32
33static int directory = CONFIG_MTD_REDBOOT_DIRECTORY_BLOCK;
34module_param(directory, int, 0);
35
36static 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
97894cda 42static int parse_redboot_partitions(struct mtd_info *master,
1da177e4
LT
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++) {
9cff3372
JB
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
77a33135 91 * one erase block; if the buf[i].size field is
9cff3372
JB
92 * swab32(erasesize) then we know we are looking at
93 * a byte swapped FIS directory - swap all the entries!
77a33135 94 * (NOTE: this is 'size' not 'data_length'; size is
9cff3372
JB
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 && buf[j].name[0] != 0xff; ++j) {
100 /* The unsigned long fields were written with the
101 * wrong byte sex, name and pad have no byte sex.
102 */
77a33135
JB
103 swab32s(&buf[j].flash_base);
104 swab32s(&buf[j].mem_base);
105 swab32s(&buf[j].size);
106 swab32s(&buf[j].entry_point);
107 swab32s(&buf[j].data_length);
108 swab32s(&buf[j].desc_cksum);
109 swab32s(&buf[j].file_cksum);
9cff3372
JB
110 }
111 }
1da177e4 112 break;
0b47d654
YS
113 } else {
114 /* re-calculate of real numslots */
115 numslots = buf[i].size / sizeof(struct fis_image_desc);
9cff3372 116 }
1da177e4
LT
117 }
118 if (i == numslots) {
119 /* Didn't find it */
120 printk(KERN_NOTICE "No RedBoot partition table detected in %s\n",
121 master->name);
122 ret = 0;
123 goto out;
124 }
125
126 for (i = 0; i < numslots; i++) {
127 struct fis_list *new_fl, **prev;
128
129 if (buf[i].name[0] == 0xff)
84e699e6 130 continue;
1da177e4
LT
131 if (!redboot_checksum(&buf[i]))
132 break;
133
134 new_fl = kmalloc(sizeof(struct fis_list), GFP_KERNEL);
135 namelen += strlen(buf[i].name)+1;
136 if (!new_fl) {
137 ret = -ENOMEM;
138 goto out;
139 }
140 new_fl->img = &buf[i];
141 if (fis_origin) {
142 buf[i].flash_base -= fis_origin;
143 } else {
144 buf[i].flash_base &= master->size-1;
145 }
146
147 /* I'm sure the JFFS2 code has done me permanent damage.
148 * I now think the following is _normal_
149 */
150 prev = &fl;
151 while(*prev && (*prev)->img->flash_base < new_fl->img->flash_base)
152 prev = &(*prev)->next;
153 new_fl->next = *prev;
154 *prev = new_fl;
155
156 nrparts++;
157 }
158#ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
159 if (fl->img->flash_base) {
160 nrparts++;
161 nulllen = sizeof(nullstring);
162 }
163
164 for (tmp_fl = fl; tmp_fl->next; tmp_fl = tmp_fl->next) {
165 if (tmp_fl->img->flash_base + tmp_fl->img->size + master->erasesize <= tmp_fl->next->img->flash_base) {
166 nrparts++;
167 nulllen = sizeof(nullstring);
168 }
169 }
170#endif
95b93a0c 171 parts = kzalloc(sizeof(*parts)*nrparts + nulllen + namelen, GFP_KERNEL);
1da177e4
LT
172
173 if (!parts) {
174 ret = -ENOMEM;
175 goto out;
176 }
177
1da177e4
LT
178 nullname = (char *)&parts[nrparts];
179#ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
180 if (nulllen > 0) {
181 strcpy(nullname, nullstring);
182 }
183#endif
184 names = nullname + nulllen;
185
186 i=0;
187
188#ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
189 if (fl->img->flash_base) {
190 parts[0].name = nullname;
191 parts[0].size = fl->img->flash_base;
192 parts[0].offset = 0;
193 i++;
194 }
195#endif
196 for ( ; i<nrparts; i++) {
197 parts[i].size = fl->img->size;
198 parts[i].offset = fl->img->flash_base;
199 parts[i].name = names;
200
201 strcpy(names, fl->img->name);
202#ifdef CONFIG_MTD_REDBOOT_PARTS_READONLY
203 if (!memcmp(names, "RedBoot", 8) ||
204 !memcmp(names, "RedBoot config", 15) ||
205 !memcmp(names, "FIS directory", 14)) {
206 parts[i].mask_flags = MTD_WRITEABLE;
207 }
208#endif
209 names += strlen(names)+1;
210
211#ifdef CONFIG_MTD_REDBOOT_PARTS_UNALLOCATED
212 if(fl->next && fl->img->flash_base + fl->img->size + master->erasesize <= fl->next->img->flash_base) {
213 i++;
214 parts[i].offset = parts[i-1].size + parts[i-1].offset;
215 parts[i].size = fl->next->img->flash_base - parts[i].offset;
216 parts[i].name = nullname;
217 }
218#endif
219 tmp_fl = fl;
220 fl = fl->next;
221 kfree(tmp_fl);
222 }
223 ret = nrparts;
224 *pparts = parts;
225 out:
226 while (fl) {
227 struct fis_list *old = fl;
228 fl = fl->next;
229 kfree(old);
230 }
231 vfree(buf);
232 return ret;
233}
234
235static struct mtd_part_parser redboot_parser = {
236 .owner = THIS_MODULE,
237 .parse_fn = parse_redboot_partitions,
238 .name = "RedBoot",
239};
240
241static int __init redboot_parser_init(void)
242{
243 return register_mtd_parser(&redboot_parser);
244}
245
246static void __exit redboot_parser_exit(void)
247{
248 deregister_mtd_parser(&redboot_parser);
249}
250
251module_init(redboot_parser_init);
252module_exit(redboot_parser_exit);
253
254MODULE_LICENSE("GPL");
255MODULE_AUTHOR("Red Hat, Inc. - David Woodhouse <dwmw2@cambridge.redhat.com>");
256MODULE_DESCRIPTION("Parsing code for RedBoot Flash Image System (FIS) tables");
This page took 0.149157 seconds and 5 git commands to generate.