blob: d61b7edfc9382383edbd0cbb6374790a889ab444 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*======================================================================
2
3 drivers/mtd/afs.c: ARM Flash Layout/Partitioning
Thomas Gleixner97894cd2005-11-07 11:15:26 +00004
David Woodhousea1452a32010-08-08 20:58:20 +01005 Copyright © 2000 ARM Limited
Thomas Gleixner97894cd2005-11-07 11:15:26 +00006
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 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.
Thomas Gleixner97894cd2005-11-07 11:15:26 +000011
Linus Torvalds1da177e2005-04-16 15:20:36 -070012 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.
Thomas Gleixner97894cd2005-11-07 11:15:26 +000016
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 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
Thomas Gleixner97894cd2005-11-07 11:15:26 +000020
21 This is access code for flashes using ARM's flash partitioning
Linus Torvalds1da177e2005-04-16 15:20:36 -070022 standards.
23
Linus Torvalds1da177e2005-04-16 15:20:36 -070024======================================================================*/
25
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>
32
33#include <linux/mtd/mtd.h>
34#include <linux/mtd/map.h>
35#include <linux/mtd/partitions.h>
36
Linus Walleij94984402015-10-15 15:08:46 +020037#define AFSV1_FOOTER_MAGIC 0xA0FFFF9F
38
Linus Walleija486d112015-10-15 15:08:44 +020039struct footer_v1 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070040 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
Linus Walleija486d112015-10-15 15:08:44 +020047struct image_info_v1 {
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 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
60static u32 word_sum(void *words, int num)
61{
62 u32 *p = words;
63 u32 sum = 0;
64
65 while (num--)
66 sum += *p++;
67
68 return sum;
69}
70
71static int
Linus Walleija486d112015-10-15 15:08:44 +020072afs_read_footer_v1(struct mtd_info *mtd, u_int *img_start, u_int *iis_start,
73 u_int off, u_int mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -070074{
Linus Walleija486d112015-10-15 15:08:44 +020075 struct footer_v1 fs;
Linus Torvalds1da177e2005-04-16 15:20:36 -070076 u_int ptr = off + mtd->erasesize - sizeof(fs);
77 size_t sz;
78 int ret;
79
Artem Bityutskiy329ad392011-12-23 17:30:16 +020080 ret = mtd_read(mtd, ptr, sizeof(fs), &sz, (u_char *)&fs);
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 if (ret >= 0 && sz != sizeof(fs))
82 ret = -EINVAL;
83
84 if (ret < 0) {
85 printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n",
86 ptr, ret);
87 return ret;
88 }
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 /*
91 * Does it contain the magic number?
92 */
Linus Walleij94984402015-10-15 15:08:46 +020093 if (fs.signature != AFSV1_FOOTER_MAGIC)
Linus Walleijd2fd05b2015-10-15 15:08:47 +020094 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -070095
96 /*
97 * Check the checksum.
98 */
99 if (word_sum(&fs, sizeof(fs) / sizeof(u32)) != 0xffffffff)
Linus Walleijd2fd05b2015-10-15 15:08:47 +0200100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101
102 /*
103 * Don't touch the SIB.
104 */
105 if (fs.type == 2)
Linus Walleijd2fd05b2015-10-15 15:08:47 +0200106 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 *iis_start = fs.image_info_base & mask;
109 *img_start = fs.image_start & mask;
110
111 /*
112 * Check the image info base. This can not
113 * be located after the footer structure.
114 */
115 if (*iis_start >= ptr)
Linus Walleijd2fd05b2015-10-15 15:08:47 +0200116 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 /*
119 * Check the start of this image. The image
120 * data can not be located after this block.
121 */
122 if (*img_start > off)
Linus Walleijd2fd05b2015-10-15 15:08:47 +0200123 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Linus Walleijd2fd05b2015-10-15 15:08:47 +0200125 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126}
127
128static int
Linus Walleija486d112015-10-15 15:08:44 +0200129afs_read_iis_v1(struct mtd_info *mtd, struct image_info_v1 *iis, u_int ptr)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130{
131 size_t sz;
132 int ret, i;
133
134 memset(iis, 0, sizeof(*iis));
Artem Bityutskiy329ad392011-12-23 17:30:16 +0200135 ret = mtd_read(mtd, ptr, sizeof(*iis), &sz, (u_char *)iis);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (ret < 0)
137 goto failed;
138
139 if (sz != sizeof(*iis)) {
140 ret = -EINVAL;
141 goto failed;
142 }
143
144 ret = 0;
145
146 /*
147 * Validate the name - it must be NUL terminated.
148 */
149 for (i = 0; i < sizeof(iis->name); i++)
150 if (iis->name[i] == '\0')
151 break;
152
153 if (i < sizeof(iis->name))
154 ret = 1;
155
156 return ret;
157
158 failed:
159 printk(KERN_ERR "AFS: mtd read failed at 0x%x: %d\n",
160 ptr, ret);
161 return ret;
162}
163
Thomas Gleixner97894cd2005-11-07 11:15:26 +0000164static int parse_afs_partitions(struct mtd_info *mtd,
Brian Norrisb9adf462015-12-04 15:25:14 -0800165 const struct mtd_partition **pparts,
Dmitry Eremin-Solenikovc7975332011-06-10 18:18:28 +0400166 struct mtd_part_parser_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167{
168 struct mtd_partition *parts;
169 u_int mask, off, idx, sz;
170 int ret = 0;
171 char *str;
172
173 /*
174 * This is the address mask; we use this to mask off out of
175 * range address bits.
176 */
177 mask = mtd->size - 1;
178
179 /*
180 * First, calculate the size of the array we need for the
181 * partition information. We include in this the size of
182 * the strings.
183 */
184 for (idx = off = sz = 0; off < mtd->size; off += mtd->erasesize) {
Linus Walleija486d112015-10-15 15:08:44 +0200185 struct image_info_v1 iis;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186 u_int iis_ptr, img_ptr;
187
Linus Walleija486d112015-10-15 15:08:44 +0200188 ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 if (ret < 0)
190 break;
Linus Walleijd2fd05b2015-10-15 15:08:47 +0200191 if (ret) {
192 ret = afs_read_iis_v1(mtd, &iis, iis_ptr);
193 if (ret < 0)
194 break;
195 if (ret == 0)
196 continue;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197
Linus Walleijd2fd05b2015-10-15 15:08:47 +0200198 sz += sizeof(struct mtd_partition);
199 sz += strlen(iis.name) + 1;
200 idx += 1;
201 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 }
203
204 if (!sz)
205 return ret;
206
Burman Yan95b93a02006-11-15 21:10:29 +0200207 parts = kzalloc(sz, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 if (!parts)
209 return -ENOMEM;
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 str = (char *)(parts + idx);
212
213 /*
214 * Identify the partitions
215 */
216 for (idx = off = 0; off < mtd->size; off += mtd->erasesize) {
Linus Walleija486d112015-10-15 15:08:44 +0200217 struct image_info_v1 iis;
Catalin Marinas747aead2005-06-30 23:01:09 +0100218 u_int iis_ptr, img_ptr;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
220 /* Read the footer. */
Linus Walleija486d112015-10-15 15:08:44 +0200221 ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222 if (ret < 0)
223 break;
224 if (ret == 0)
225 continue;
226
227 /* Read the image info block */
Linus Walleija486d112015-10-15 15:08:44 +0200228 ret = afs_read_iis_v1(mtd, &iis, iis_ptr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 if (ret < 0)
230 break;
231 if (ret == 0)
232 continue;
233
234 strcpy(str, iis.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235
236 parts[idx].name = str;
Catalin Marinas747aead2005-06-30 23:01:09 +0100237 parts[idx].size = (iis.length + mtd->erasesize - 1) & ~(mtd->erasesize - 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 parts[idx].offset = img_ptr;
239 parts[idx].mask_flags = 0;
240
Russell King0ffd24f2009-07-18 14:10:44 +0100241 printk(" mtd%d: at 0x%08x, %5lluKiB, %8u, %s\n",
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242 idx, img_ptr, parts[idx].size / 1024,
243 iis.imageNumber, str);
244
245 idx += 1;
246 str = str + strlen(iis.name) + 1;
247 }
248
249 if (!idx) {
250 kfree(parts);
251 parts = NULL;
252 }
253
254 *pparts = parts;
255 return idx ? idx : ret;
256}
257
258static struct mtd_part_parser afs_parser = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 .parse_fn = parse_afs_partitions,
260 .name = "afs",
261};
Brian Norrisb8f70ba2015-11-11 19:13:30 -0800262module_mtd_part_parser(afs_parser);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264MODULE_AUTHOR("ARM Ltd");
265MODULE_DESCRIPTION("ARM Firmware Suite partition parser");
266MODULE_LICENSE("GPL");