1 /*======================================================================
3 drivers/mtd/afs.c: ARM Flash Layout/Partitioning
5 Copyright (C) 2000 ARM Limited
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.
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.
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
21 This is access code for flashes using ARM's flash partitioning
24 $Id: afs.c,v 1.8 2002/05/04 08:49:09 rmk Exp $
26 ======================================================================*/
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>
35 #include <linux/mtd/mtd.h>
36 #include <linux/mtd/map.h>
37 #include <linux/mtd/partitions.h>
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 */
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) */
61 afs_read_footer(struct mtd_info *mtd, u_int *img_start, u_int *iis_start,
62 u_int off, u_int mask)
64 struct footer_struct fs;
65 u_int ptr = off + mtd->erasesize - sizeof(fs);
69 ret = mtd->read(mtd, ptr, sizeof(fs), &sz, (u_char *) &fs);
70 if (ret >= 0 && sz != sizeof(fs))
74 printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n",
80 * Does it contain the magic number?
82 if (fs.signature != 0xa0ffff9f)
86 * Don't touch the SIB.
91 *iis_start = fs.image_info_base & mask;
92 *img_start = fs.image_start & mask;
95 * Check the image info base. This can not
96 * be located after the footer structure.
98 if (*iis_start >= ptr)
102 * Check the start of this image. The image
103 * data can not be located after this block.
105 if (*img_start > off)
112 afs_read_iis(struct mtd_info *mtd, struct image_info_struct *iis, u_int ptr)
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))
122 printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n",
128 int parse_afs_partitions(struct mtd_info *mtd, struct mtd_partition **pparts)
130 struct mtd_partition *parts;
131 u_int mask, off, idx, sz;
136 * This is the address mask; we use this to mask off out of
137 * range address bits.
139 mask = mtd->size - 1;
142 * First, calculate the size of the array we need for the
143 * partition information. We include in this the size of
146 for (idx = off = sz = 0; off < mtd->size; off += mtd->erasesize) {
147 struct image_info_struct iis;
148 u_int iis_ptr, img_ptr;
150 ret = afs_read_footer(mtd, &img_ptr, &iis_ptr, off, mask);
156 ret = afs_read_iis(mtd, &iis, iis_ptr);
160 sz += sizeof(struct mtd_partition);
161 sz += strlen(iis.name) + 1;
168 parts = kmalloc(sz, GFP_KERNEL);
172 memset(parts, 0, sz);
173 str = (char *)(parts + idx);
176 * Identify the partitions
178 for (idx = off = 0; off < mtd->size; off += mtd->erasesize) {
179 struct image_info_struct iis;
180 u_int iis_ptr, img_ptr, size;
182 /* Read the footer. */
183 ret = afs_read_footer(mtd, &img_ptr, &iis_ptr, off, mask);
189 /* Read the image info block */
190 ret = afs_read_iis(mtd, &iis, iis_ptr);
194 strcpy(str, iis.name);
195 size = mtd->erasesize + off - img_ptr;
198 * In order to support JFFS2 partitions on this layout,
199 * we must lie to MTD about the real size of JFFS2
200 * partitions; this ensures that the AFS flash footer
201 * won't be erased by JFFS2. Please ensure that your
202 * JFFS2 partitions are given image numbers between
203 * 1000 and 2000 inclusive.
205 if (iis.imageNumber >= 1000 && iis.imageNumber < 2000)
206 size -= mtd->erasesize;
208 parts[idx].name = str;
209 parts[idx].size = size;
210 parts[idx].offset = img_ptr;
211 parts[idx].mask_flags = 0;
213 printk(" mtd%d: at 0x%08x, %5dKB, %8u, %s\n",
214 idx, img_ptr, parts[idx].size / 1024,
215 iis.imageNumber, str);
218 str = str + strlen(iis.name) + 1;
227 return idx ? idx : ret;
230 EXPORT_SYMBOL(parse_afs_partitions);
232 MODULE_AUTHOR("ARM Ltd");
233 MODULE_DESCRIPTION("ARM Firmware Suite partition parser");
234 MODULE_LICENSE("GPL");