1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
37#define AFSV1_FOOTER_MAGIC 0xA0FFFF9F
38
39struct footer_v1 {
40 u32 image_info_base;
41 u32 image_start;
42 u32 signature;
43 u32 type;
44 u32 checksum;
45};
46
47struct image_info_v1 {
48 u32 bootFlags;
49 u32 imageNumber;
50 u32 loadAddress;
51 u32 length;
52 u32 address;
53 char name[16];
54 u32 headerBase;
55 u32 header_length;
56 u32 headerType;
57 u32 checksum;
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
72afs_read_footer_v1(struct mtd_info *mtd, u_int *img_start, u_int *iis_start,
73 u_int off, u_int mask)
74{
75 struct footer_v1 fs;
76 u_int ptr = off + mtd->erasesize - sizeof(fs);
77 size_t sz;
78 int ret;
79
80 ret = mtd_read(mtd, ptr, sizeof(fs), &sz, (u_char *)&fs);
81 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
90
91
92
93 if (fs.signature != AFSV1_FOOTER_MAGIC)
94 return 0;
95
96
97
98
99 if (word_sum(&fs, sizeof(fs) / sizeof(u32)) != 0xffffffff)
100 return 0;
101
102
103
104
105 if (fs.type == 2)
106 return 0;
107
108 *iis_start = fs.image_info_base & mask;
109 *img_start = fs.image_start & mask;
110
111
112
113
114
115 if (*iis_start >= ptr)
116 return 0;
117
118
119
120
121
122 if (*img_start > off)
123 return 0;
124
125 return 1;
126}
127
128static int
129afs_read_iis_v1(struct mtd_info *mtd, struct image_info_v1 *iis, u_int ptr)
130{
131 size_t sz;
132 int ret, i;
133
134 memset(iis, 0, sizeof(*iis));
135 ret = mtd_read(mtd, ptr, sizeof(*iis), &sz, (u_char *)iis);
136 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
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
164static int parse_afs_partitions(struct mtd_info *mtd,
165 const struct mtd_partition **pparts,
166 struct mtd_part_parser_data *data)
167{
168 struct mtd_partition *parts;
169 u_int mask, off, idx, sz;
170 int ret = 0;
171 char *str;
172
173
174
175
176
177 mask = mtd->size - 1;
178
179
180
181
182
183
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;
187
188 ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask);
189 if (ret < 0)
190 break;
191 if (ret) {
192 ret = afs_read_iis_v1(mtd, &iis, iis_ptr);
193 if (ret < 0)
194 break;
195 if (ret == 0)
196 continue;
197
198 sz += sizeof(struct mtd_partition);
199 sz += strlen(iis.name) + 1;
200 idx += 1;
201 }
202 }
203
204 if (!sz)
205 return ret;
206
207 parts = kzalloc(sz, GFP_KERNEL);
208 if (!parts)
209 return -ENOMEM;
210
211 str = (char *)(parts + idx);
212
213
214
215
216 for (idx = off = 0; off < mtd->size; off += mtd->erasesize) {
217 struct image_info_v1 iis;
218 u_int iis_ptr, img_ptr;
219
220
221 ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask);
222 if (ret < 0)
223 break;
224 if (ret == 0)
225 continue;
226
227
228 ret = afs_read_iis_v1(mtd, &iis, iis_ptr);
229 if (ret < 0)
230 break;
231 if (ret == 0)
232 continue;
233
234 strcpy(str, iis.name);
235
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;
240
241 printk(" mtd%d: at 0x%08x, %5lluKiB, %8u, %s\n",
242 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 = {
259 .parse_fn = parse_afs_partitions,
260 .name = "afs",
261};
262module_mtd_part_parser(afs_parser);
263
264MODULE_AUTHOR("ARM Ltd");
265MODULE_DESCRIPTION("ARM Firmware Suite partition parser");
266MODULE_LICENSE("GPL");
267