6640ebc8e9944cf500bd7ae0ca708e19fb79f8b3
[linux-flexiantxendom0-3.2.10.git] / drivers / mtd / afs.c
1 /*======================================================================
2
3     drivers/mtd/afs.c: ARM Flash Layout/Partitioning
4   
5     Copyright (C) 2000 ARM Limited
6   
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11   
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16   
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
20   
21    This is access code for flashes using ARM's flash partitioning 
22    standards.
23
24    $Id: afs.c,v 1.11 2003/05/16 17:08:24 dwmw2 Exp $
25
26 ======================================================================*/
27
28 #include <linux/module.h>
29 #include <linux/types.h>
30 #include <linux/kernel.h>
31 #include <linux/slab.h>
32 #include <linux/string.h>
33 #include <linux/init.h>
34
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/map.h>
37 #include <linux/mtd/partitions.h>
38
39 struct footer_struct {
40         u32 image_info_base;    /* Address of first word of ImageFooter  */
41         u32 image_start;        /* Start of area reserved by this footer */
42         u32 signature;          /* 'Magic' number proves it's a footer   */
43         u32 type;               /* Area type: ARM Image, SIB, customer   */
44         u32 checksum;           /* Just this structure                   */
45 };
46
47 struct image_info_struct {
48         u32 bootFlags;          /* Boot flags, compression etc.          */
49         u32 imageNumber;        /* Unique number, selects for boot etc.  */
50         u32 loadAddress;        /* Address program should be loaded to   */
51         u32 length;             /* Actual size of image                  */
52         u32 address;            /* Image is executed from here           */
53         char name[16];          /* Null terminated                       */
54         u32 headerBase;         /* Flash Address of any stripped header  */
55         u32 header_length;      /* Length of header in memory            */
56         u32 headerType;         /* AIF, RLF, s-record etc.               */
57         u32 checksum;           /* Image checksum (inc. this struct)     */
58 };
59
60 static int
61 afs_read_footer(struct mtd_info *mtd, u_int *img_start, u_int *iis_start,
62                 u_int off, u_int mask)
63 {
64         struct footer_struct fs;
65         u_int ptr = off + mtd->erasesize - sizeof(fs);
66         size_t sz;
67         int ret;
68
69         ret = mtd->read(mtd, ptr, sizeof(fs), &sz, (u_char *) &fs);
70         if (ret >= 0 && sz != sizeof(fs))
71                 ret = -EINVAL;
72
73         if (ret < 0) {
74                 printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n",
75                         ptr, ret);
76                 return ret;
77         }
78
79         /*
80          * Does it contain the magic number?
81          */
82         if (fs.signature != 0xa0ffff9f)
83                 ret = 1;
84
85         /*
86          * Don't touch the SIB.
87          */
88         if (fs.type == 2)
89                 ret = 1;
90
91         *iis_start = fs.image_info_base & mask;
92         *img_start = fs.image_start & mask;
93
94         /*
95          * Check the image info base.  This can not
96          * be located after the footer structure.
97          */
98         if (*iis_start >= ptr)
99                 ret = 1;
100
101         /*
102          * Check the start of this image.  The image
103          * data can not be located after this block.
104          */
105         if (*img_start > off)
106                 ret = 1;
107
108         return ret;
109 }
110
111 static int
112 afs_read_iis(struct mtd_info *mtd, struct image_info_struct *iis, u_int ptr)
113 {
114         size_t sz;
115         int ret;
116
117         memset(iis, 0, sizeof(*iis));
118         ret = mtd->read(mtd, ptr, sizeof(*iis), &sz, (u_char *) iis);
119         if (ret >= 0 && sz != sizeof(*iis))
120                 ret = -EINVAL;
121         if (ret < 0)
122                 printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n",
123                         ptr, ret);
124
125         return ret;
126 }
127
128 static int parse_afs_partitions(struct mtd_info *mtd, 
129                          struct mtd_partition **pparts,
130                          unsigned long origin)
131 {
132         struct mtd_partition *parts;
133         u_int mask, off, idx, sz;
134         int ret = 0;
135         char *str;
136
137         /*
138          * This is the address mask; we use this to mask off out of
139          * range address bits.
140          */
141         mask = mtd->size - 1;
142
143         /*
144          * First, calculate the size of the array we need for the
145          * partition information.  We include in this the size of
146          * the strings.
147          */
148         for (idx = off = sz = 0; off < mtd->size; off += mtd->erasesize) {
149                 struct image_info_struct iis;
150                 u_int iis_ptr, img_ptr;
151
152                 ret = afs_read_footer(mtd, &img_ptr, &iis_ptr, off, mask);
153                 if (ret < 0)
154                         break;
155                 if (ret == 1)
156                         continue;
157
158                 ret = afs_read_iis(mtd, &iis, iis_ptr);
159                 if (ret < 0)
160                         break;
161
162                 sz += sizeof(struct mtd_partition);
163                 sz += strlen(iis.name) + 1;
164                 idx += 1;
165         }
166
167         if (!sz)
168                 return ret;
169
170         parts = kmalloc(sz, GFP_KERNEL);
171         if (!parts)
172                 return -ENOMEM;
173
174         memset(parts, 0, sz);
175         str = (char *)(parts + idx);
176
177         /*
178          * Identify the partitions
179          */
180         for (idx = off = 0; off < mtd->size; off += mtd->erasesize) {
181                 struct image_info_struct iis;
182                 u_int iis_ptr, img_ptr, size;
183
184                 /* Read the footer. */
185                 ret = afs_read_footer(mtd, &img_ptr, &iis_ptr, off, mask);
186                 if (ret < 0)
187                         break;
188                 if (ret == 1)
189                         continue;
190
191                 /* Read the image info block */
192                 ret = afs_read_iis(mtd, &iis, iis_ptr);
193                 if (ret < 0)
194                         break;
195
196                 strcpy(str, iis.name);
197                 size = mtd->erasesize + off - img_ptr;
198
199                 /*
200                  * In order to support JFFS2 partitions on this layout,
201                  * we must lie to MTD about the real size of JFFS2
202                  * partitions; this ensures that the AFS flash footer
203                  * won't be erased by JFFS2.  Please ensure that your
204                  * JFFS2 partitions are given image numbers between
205                  * 1000 and 2000 inclusive.
206                  */
207                 if (iis.imageNumber >= 1000 && iis.imageNumber < 2000)
208                         size -= mtd->erasesize;
209
210                 parts[idx].name         = str;
211                 parts[idx].size         = size;
212                 parts[idx].offset       = img_ptr;
213                 parts[idx].mask_flags   = 0;
214
215                 printk("  mtd%d: at 0x%08x, %5dKB, %8u, %s\n",
216                         idx, img_ptr, parts[idx].size / 1024,
217                         iis.imageNumber, str);
218
219                 idx += 1;
220                 str = str + strlen(iis.name) + 1;
221         }
222
223         if (!idx) {
224                 kfree(parts);
225                 parts = NULL;
226         }
227
228         *pparts = parts;
229         return idx ? idx : ret;
230 }
231
232 static struct mtd_part_parser afs_parser = {
233         .owner = THIS_MODULE,
234         .parse_fn = parse_afs_partitions,
235         .name = "afs",
236 };
237
238 static int __init afs_parser_init(void)
239 {
240         return register_mtd_parser(&afs_parser);
241 }
242
243 static void __exit afs_parser_exit(void)
244 {
245         deregister_mtd_parser(&afs_parser);
246 }
247
248 module_init(afs_parser_init);
249 module_exit(afs_parser_exit);
250
251
252 MODULE_AUTHOR("ARM Ltd");
253 MODULE_DESCRIPTION("ARM Firmware Suite partition parser");
254 MODULE_LICENSE("GPL");