kernel: mtdsplit_uimage: use separated buffer for reading data

We shouldn't read data directly into the header struct, as some devices
(e.g. Edimax) need more bytes due to some extra header.

Signed-off-by: Rafał Miłecki <zajec5@gmail.com>

SVN-Revision: 44414
This commit is contained in:
Rafał Miłecki 2015-02-12 07:44:34 +00:00
parent 5673e0b703
commit 072759ff39

View file

@ -49,15 +49,15 @@ struct uimage_header {
static int static int
read_uimage_header(struct mtd_info *mtd, size_t offset, read_uimage_header(struct mtd_info *mtd, size_t offset,
struct uimage_header *header) u_char *buf)
{ {
struct uimage_header *header;
size_t header_len; size_t header_len;
size_t retlen; size_t retlen;
int ret; int ret;
header_len = sizeof(*header); header_len = sizeof(*header);
ret = mtd_read(mtd, offset, header_len, &retlen, ret = mtd_read(mtd, offset, header_len, &retlen, buf);
(unsigned char *) header);
if (ret) { if (ret) {
pr_debug("read error in \"%s\"\n", mtd->name); pr_debug("read error in \"%s\"\n", mtd->name);
return ret; return ret;
@ -83,6 +83,7 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
ssize_t (*find_header)(u_char *buf, size_t len)) ssize_t (*find_header)(u_char *buf, size_t len))
{ {
struct mtd_partition *parts; struct mtd_partition *parts;
u_char *buf;
struct uimage_header *header; struct uimage_header *header;
int nr_parts; int nr_parts;
size_t offset; size_t offset;
@ -98,8 +99,8 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
if (!parts) if (!parts)
return -ENOMEM; return -ENOMEM;
header = vmalloc(sizeof(*header)); buf = vmalloc(sizeof(*header));
if (!header) { if (!buf) {
ret = -ENOMEM; ret = -ENOMEM;
goto err_free_parts; goto err_free_parts;
} }
@ -108,20 +109,17 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
for (offset = 0; offset < master->size; offset += master->erasesize) { for (offset = 0; offset < master->size; offset += master->erasesize) {
uimage_size = 0; uimage_size = 0;
ret = read_uimage_header(master, offset, header); ret = read_uimage_header(master, offset, buf);
if (ret) if (ret)
continue; continue;
ret = find_header((u_char *)header, sizeof(*header)); ret = find_header(buf, sizeof(*buf));
if (ret < 0) { if (ret < 0) {
pr_debug("no valid uImage found in \"%s\" at offset %llx\n", pr_debug("no valid uImage found in \"%s\" at offset %llx\n",
master->name, (unsigned long long) offset); master->name, (unsigned long long) offset);
continue; continue;
} }
if (ret > 0) { header = (struct uimage_header *)(buf + ret);
pr_warn("extra header offsets are not supported yet\n");
continue;
}
uimage_size = sizeof(*header) + be32_to_cpu(header->ih_size); uimage_size = sizeof(*header) + be32_to_cpu(header->ih_size);
if ((offset + uimage_size) > master->size) { if ((offset + uimage_size) > master->size) {
@ -135,7 +133,7 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
if (uimage_size == 0) { if (uimage_size == 0) {
pr_debug("no uImage found in \"%s\"\n", master->name); pr_debug("no uImage found in \"%s\"\n", master->name);
ret = -ENODEV; ret = -ENODEV;
goto err_free_header; goto err_free_buf;
} }
uimage_offset = offset; uimage_offset = offset;
@ -152,7 +150,7 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
if (ret) { if (ret) {
pr_debug("no rootfs after uImage in \"%s\"\n", pr_debug("no rootfs after uImage in \"%s\"\n",
master->name); master->name);
goto err_free_header; goto err_free_buf;
} }
rootfs_size = master->size - rootfs_offset; rootfs_size = master->size - rootfs_offset;
@ -166,7 +164,7 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
if (ret) { if (ret) {
pr_debug("no rootfs before uImage in \"%s\"\n", pr_debug("no rootfs before uImage in \"%s\"\n",
master->name); master->name);
goto err_free_header; goto err_free_buf;
} }
rootfs_offset = 0; rootfs_offset = 0;
@ -176,7 +174,7 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
if (rootfs_size == 0) { if (rootfs_size == 0) {
pr_debug("no rootfs found in \"%s\"\n", master->name); pr_debug("no rootfs found in \"%s\"\n", master->name);
ret = -ENODEV; ret = -ENODEV;
goto err_free_header; goto err_free_buf;
} }
parts[uimage_part].name = KERNEL_PART_NAME; parts[uimage_part].name = KERNEL_PART_NAME;
@ -187,13 +185,13 @@ static int __mtdsplit_parse_uimage(struct mtd_info *master,
parts[rf_part].offset = rootfs_offset; parts[rf_part].offset = rootfs_offset;
parts[rf_part].size = rootfs_size; parts[rf_part].size = rootfs_size;
vfree(header); vfree(buf);
*pparts = parts; *pparts = parts;
return nr_parts; return nr_parts;
err_free_header: err_free_buf:
vfree(header); vfree(buf);
err_free_parts: err_free_parts:
kfree(parts); kfree(parts);