Skip to content

Commit 1fca1f6

Browse files
Linus Walleijrichardweinberger
authored andcommitted
mtd: afs: simplify partition parsing
This simplifies the AFS partition parsing to make the code more straight-forward and readable. Before this patch the code tried to calculate the memory required to hold the partition info by adding up the sizes of the strings of the names and adding that to a single memory allocation, indexing the name pointers in front of the struct mtd_partition allocations so all allocated data was in one chunk. This is overzealous. Instead use kstrdup and bail out, kfree():ing the memory used for MTD partitions and names alike on the errorpath. In the process rename the index variable from idx to i. Cc: Ryan Harkin <ryan.harkin@linaro.org> Acked-by: Liviu Dudau <liviu.dudau@arm.com> Signed-off-by: Linus Walleij <linus.walleij@linaro.org> Signed-off-by: Richard Weinberger <richard@nod.at>
1 parent 22749bf commit 1fca1f6

File tree

1 file changed

+32
-35
lines changed

1 file changed

+32
-35
lines changed

drivers/mtd/parsers/afs.c

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -166,9 +166,9 @@ static int parse_afs_partitions(struct mtd_info *mtd,
166166
struct mtd_part_parser_data *data)
167167
{
168168
struct mtd_partition *parts;
169-
u_int mask, off, idx, sz;
169+
u_int mask, off, sz;
170170
int ret = 0;
171-
char *str;
171+
int i;
172172

173173
/*
174174
* This is the address mask; we use this to mask off out of
@@ -181,78 +181,75 @@ static int parse_afs_partitions(struct mtd_info *mtd,
181181
* partition information. We include in this the size of
182182
* the strings.
183183
*/
184-
for (idx = off = sz = 0; off < mtd->size; off += mtd->erasesize) {
185-
struct image_info_v1 iis;
184+
for (i = off = sz = 0; off < mtd->size; off += mtd->erasesize) {
186185
u_int iis_ptr, img_ptr;
187186

188187
ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask);
189188
if (ret < 0)
190-
break;
189+
return ret;
191190
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-
198191
sz += sizeof(struct mtd_partition);
199-
sz += strlen(iis.name) + 1;
200-
idx += 1;
192+
i += 1;
201193
}
202194
}
203195

204-
if (!sz)
205-
return ret;
196+
if (!i)
197+
return 0;
206198

207199
parts = kzalloc(sz, GFP_KERNEL);
208200
if (!parts)
209201
return -ENOMEM;
210202

211-
str = (char *)(parts + idx);
212-
213203
/*
214204
* Identify the partitions
215205
*/
216-
for (idx = off = 0; off < mtd->size; off += mtd->erasesize) {
206+
for (i = off = 0; off < mtd->size; off += mtd->erasesize) {
217207
struct image_info_v1 iis;
218208
u_int iis_ptr, img_ptr;
219209

220210
/* Read the footer. */
221211
ret = afs_read_footer_v1(mtd, &img_ptr, &iis_ptr, off, mask);
222212
if (ret < 0)
223-
break;
213+
goto out_free_parts;
224214
if (ret == 0)
225215
continue;
226216

227217
/* Read the image info block */
228218
ret = afs_read_iis_v1(mtd, &iis, iis_ptr);
229219
if (ret < 0)
230-
break;
220+
goto out_free_parts;
231221
if (ret == 0)
232222
continue;
233223

234-
strcpy(str, iis.name);
224+
parts[i].name = kstrdup(iis.name, GFP_KERNEL);
225+
if (!parts[i].name) {
226+
ret = -ENOMEM;
227+
goto out_free_parts;
228+
}
235229

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;
230+
parts[i].size = (iis.length + mtd->erasesize - 1) & ~(mtd->erasesize - 1);
231+
parts[i].offset = img_ptr;
232+
parts[i].mask_flags = 0;
240233

241234
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-
}
235+
i, img_ptr, parts[i].size / 1024,
236+
iis.imageNumber, parts[i].name);
248237

249-
if (!idx) {
250-
kfree(parts);
251-
parts = NULL;
238+
i += 1;
252239
}
253240

254241
*pparts = parts;
255-
return idx ? idx : ret;
242+
return i;
243+
244+
out_free_parts:
245+
while (i >= 0) {
246+
if (parts[i].name)
247+
kfree(parts[i].name);
248+
i--;
249+
}
250+
kfree(parts);
251+
*pparts = NULL;
252+
return ret;
256253
}
257254

258255
static const struct of_device_id mtd_parser_afs_of_match_table[] = {

0 commit comments

Comments
 (0)