/** * __mtdsplit_parse_uimage - scan partition and create kernel + rootfs parts * * @find_header: function to call for a block of data that will return offset * of a valid uImage header if found */ static int __mtdsplit_parse_uimage(struct mtd_info *master, struct mtd_partition **pparts, struct mtd_part_parser_data *data, ssize_t (*find_header)(u_char *buf, size_t len)) { struct mtd_partition *parts; u_char *buf; int nr_parts; size_t offset; size_t uimage_offset; size_t uimage_size = 0; size_t rootfs_offset; size_t rootfs_size = 0; int uimage_part, rf_part; int ret; enum mtdsplit_part_type type; nr_parts = 2; parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL); if (!parts) return -ENOMEM; buf = vmalloc(MAX_HEADER_LEN); if (!buf) { ret = -ENOMEM; goto err_free_parts; } /* find uImage on erase block boundaries */ for (offset = 0; offset < master->size; offset += master->erasesize) { struct uimage_header *header; uimage_size = 0; ret = read_uimage_header(master, offset, buf, MAX_HEADER_LEN); if (ret) continue; ret = find_header(buf, MAX_HEADER_LEN); if (ret < 0) { pr_debug("no valid uImage found in \"%s\" at offset %llx\n", master->name, (unsigned long long) offset); continue; } header = (struct uimage_header *)(buf + ret); uimage_size = sizeof(*header) + be32_to_cpu(header->ih_size) + ret; if ((offset + uimage_size) > master->size) { pr_debug("uImage exceeds MTD device \"%s\"\n", master->name); continue; } break; } if (uimage_size == 0) { pr_debug("no uImage found in \"%s\"\n", master->name); ret = -ENODEV; goto err_free_buf; } uimage_offset = offset; if (uimage_offset == 0) { uimage_part = 0; rf_part = 1; /* find the roots after the uImage */ ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size, master->size, &rootfs_offset, &type); if (ret) { pr_debug("no rootfs after uImage in \"%s\"\n", master->name); goto err_free_buf; } rootfs_size = master->size - rootfs_offset; uimage_size = rootfs_offset - uimage_offset; } else { rf_part = 0; uimage_part = 1; /* check rootfs presence at offset 0 */ ret = mtd_check_rootfs_magic(master, 0, &type); if (ret) { pr_debug("no rootfs before uImage in \"%s\"\n", master->name); goto err_free_buf; } rootfs_offset = 0; rootfs_size = uimage_offset; } if (rootfs_size == 0) { pr_debug("no rootfs found in \"%s\"\n", master->name); ret = -ENODEV; goto err_free_buf; } parts[uimage_part].name = KERNEL_PART_NAME; parts[uimage_part].offset = uimage_offset; parts[uimage_part].size = uimage_size; if (type == MTDSPLIT_PART_TYPE_UBI) parts[rf_part].name = UBI_PART_NAME; else parts[rf_part].name = ROOTFS_PART_NAME; parts[rf_part].offset = rootfs_offset; parts[rf_part].size = rootfs_size; vfree(buf); *pparts = parts; return nr_parts; err_free_buf: vfree(buf); err_free_parts: kfree(parts); return ret; }
static int __mtdsplit_parse_uimage(struct mtd_info *master, struct mtd_partition **pparts, struct mtd_part_parser_data *data, bool (*verify)(struct uimage_header *hdr)) { struct mtd_partition *parts; struct uimage_header *header; int nr_parts; size_t offset; size_t uimage_offset; size_t uimage_size = 0; size_t rootfs_offset; size_t rootfs_size = 0; int uimage_part, rf_part; int ret; nr_parts = 2; parts = kzalloc(nr_parts * sizeof(*parts), GFP_KERNEL); if (!parts) return -ENOMEM; header = vmalloc(sizeof(*header)); if (!header) { ret = -ENOMEM; goto err_free_parts; } /* find uImage on erase block boundaries */ for (offset = 0; offset < master->size; offset += master->erasesize) { uimage_size = 0; ret = read_uimage_header(master, offset, header); if (ret) continue; if (!verify(header)) { pr_debug("no valid uImage found in \"%s\" at offset %llx\n", master->name, (unsigned long long) offset); continue; } uimage_size = sizeof(*header) + be32_to_cpu(header->ih_size); if ((offset + uimage_size) > master->size) { pr_debug("uImage exceeds MTD device \"%s\"\n", master->name); continue; } break; } if (uimage_size == 0) { pr_debug("no uImage found in \"%s\"\n", master->name); ret = -ENODEV; goto err_free_header; } uimage_offset = offset; if (uimage_offset == 0) { uimage_part = 0; rf_part = 1; /* find the roots after the uImage */ ret = mtd_find_rootfs_from(master, uimage_offset + uimage_size, master->size, &rootfs_offset); if (ret) { pr_debug("no rootfs after uImage in \"%s\"\n", master->name); goto err_free_header; } rootfs_size = master->size - rootfs_offset; uimage_size = rootfs_offset - uimage_offset; } else { rf_part = 0; uimage_part = 1; /* check rootfs presence at offset 0 */ ret = mtd_check_rootfs_magic(master, 0); if (ret) { pr_debug("no rootfs before uImage in \"%s\"\n", master->name); goto err_free_header; } rootfs_offset = 0; rootfs_size = uimage_offset; } if (rootfs_size == 0) { pr_debug("no rootfs found in \"%s\"\n", master->name); ret = -ENODEV; goto err_free_header; } parts[uimage_part].name = KERNEL_PART_NAME; parts[uimage_part].offset = uimage_offset; parts[uimage_part].size = uimage_size; parts[rf_part].name = ROOTFS_PART_NAME; parts[rf_part].offset = rootfs_offset; parts[rf_part].size = rootfs_size; vfree(header); *pparts = parts; return nr_parts; err_free_header: vfree(header); err_free_parts: kfree(parts); return ret; }