1 /*======================================================================
3 drivers/mtd/afs.c: ARM Flash Layout/Partitioning
5 Copyright © 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 ======================================================================*/
26 #include <linux/module.h>
27 #include <linux/types.h>
28 #include <linux/kernel.h>
29 #include <linux/slab.h>
30 #include <linux/string.h>
31 #include <linux/init.h>
33 #include <linux/mtd/mtd.h>
34 #include <linux/mtd/map.h>
35 #include <linux/mtd/partitions.h>
37 #define AFSV1_FOOTER_MAGIC 0xA0FFFF9F
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_v1
{
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) */
60 static u32
word_sum(void *words
, int num
)
72 afs_read_footer_v1(struct mtd_info
*mtd
, u_int
*img_start
, u_int
*iis_start
,
73 u_int off
, u_int mask
)
76 u_int ptr
= off
+ mtd
->erasesize
- sizeof(fs
);
80 ret
= mtd_read(mtd
, ptr
, sizeof(fs
), &sz
, (u_char
*)&fs
);
81 if (ret
>= 0 && sz
!= sizeof(fs
))
85 printk(KERN_ERR
"AFS: mtd read failed at 0x%x: %d\n",
91 * Does it contain the magic number?
93 if (fs
.signature
!= AFSV1_FOOTER_MAGIC
)
99 if (word_sum(&fs
, sizeof(fs
) / sizeof(u32
)) != 0xffffffff)
103 * Don't touch the SIB.
108 *iis_start
= fs
.image_info_base
& mask
;
109 *img_start
= fs
.image_start
& mask
;
112 * Check the image info base. This can not
113 * be located after the footer structure.
115 if (*iis_start
>= ptr
)
119 * Check the start of this image. The image
120 * data can not be located after this block.
122 if (*img_start
> off
)
129 afs_read_iis_v1(struct mtd_info
*mtd
, struct image_info_v1
*iis
, u_int ptr
)
134 memset(iis
, 0, sizeof(*iis
));
135 ret
= mtd_read(mtd
, ptr
, sizeof(*iis
), &sz
, (u_char
*)iis
);
139 if (sz
!= sizeof(*iis
)) {
147 * Validate the name - it must be NUL terminated.
149 for (i
= 0; i
< sizeof(iis
->name
); i
++)
150 if (iis
->name
[i
] == '\0')
153 if (i
< sizeof(iis
->name
))
159 printk(KERN_ERR
"AFS: mtd read failed at 0x%x: %d\n",
164 static int parse_afs_partitions(struct mtd_info
*mtd
,
165 const struct mtd_partition
**pparts
,
166 struct mtd_part_parser_data
*data
)
168 struct mtd_partition
*parts
;
169 u_int mask
, off
, idx
, sz
;
174 * This is the address mask; we use this to mask off out of
175 * range address bits.
177 mask
= mtd
->size
- 1;
180 * First, calculate the size of the array we need for the
181 * partition information. We include in this the size of
184 for (idx
= off
= sz
= 0; off
< mtd
->size
; off
+= mtd
->erasesize
) {
185 struct image_info_v1 iis
;
186 u_int iis_ptr
, img_ptr
;
188 ret
= afs_read_footer_v1(mtd
, &img_ptr
, &iis_ptr
, off
, mask
);
192 ret
= afs_read_iis_v1(mtd
, &iis
, iis_ptr
);
198 sz
+= sizeof(struct mtd_partition
);
199 sz
+= strlen(iis
.name
) + 1;
207 parts
= kzalloc(sz
, GFP_KERNEL
);
211 str
= (char *)(parts
+ idx
);
214 * Identify the partitions
216 for (idx
= off
= 0; off
< mtd
->size
; off
+= mtd
->erasesize
) {
217 struct image_info_v1 iis
;
218 u_int iis_ptr
, img_ptr
;
220 /* Read the footer. */
221 ret
= afs_read_footer_v1(mtd
, &img_ptr
, &iis_ptr
, off
, mask
);
227 /* Read the image info block */
228 ret
= afs_read_iis_v1(mtd
, &iis
, iis_ptr
);
234 strcpy(str
, iis
.name
);
236 parts
[idx
].name
= str
;
237 parts
[idx
].size
= (iis
.length
+ mtd
->erasesize
- 1) & ~(mtd
->erasesize
- 1);
238 parts
[idx
].offset
= img_ptr
;
239 parts
[idx
].mask_flags
= 0;
241 printk(" mtd%d: at 0x%08x, %5lluKiB, %8u, %s\n",
242 idx
, img_ptr
, parts
[idx
].size
/ 1024,
243 iis
.imageNumber
, str
);
246 str
= str
+ strlen(iis
.name
) + 1;
255 return idx
? idx
: ret
;
258 static struct mtd_part_parser afs_parser
= {
259 .parse_fn
= parse_afs_partitions
,
262 module_mtd_part_parser(afs_parser
);
264 MODULE_AUTHOR("ARM Ltd");
265 MODULE_DESCRIPTION("ARM Firmware Suite partition parser");
266 MODULE_LICENSE("GPL");