/* Check if there are any bad blocks, and whether length including bad blocks fits into device */
static int check_skip_len(struct mtd_info *mtd, loff_t offset, size_t length, size_t *used)
/* Check if there are any bad blocks, and whether length including bad blocks fits into device */
static int check_skip_len(struct mtd_info *mtd, loff_t offset, size_t length, size_t *used)
{
	size_t len_excl_bad = 0;
	int ret = 0;
	while (len_excl_bad < length) {
		size_t block_len, block_off;
		loff_t block_start;
		if (offset >= mtd->size)
			return -1;
		block_start = offset & ~(loff_t)(mtd->erasesize - 1);
		block_off = offset & (mtd->erasesize - 1);
		block_len = mtd->erasesize - block_off;
		if (!nand_block_isbad(mtd, block_start))
			len_excl_bad += block_len;
		else
			ret = 1;
		offset += block_len;
		*used += block_len;
	}
	if (len_excl_bad > length)
		*used -= (len_excl_bad - length);
	return ret;
}