/* Verify a region of NAND flash. Reads NAND in page-sized chunks and verifies the contents against the contents of a buffer. The offset into the NAND must be page-aligned, and the function doesn't handle skipping bad blocks. */
int nand_verify(struct mtd_info *mtd, loff_t ofs, size_t len, u_char *buf)
/* Verify a region of NAND flash. Reads NAND in page-sized chunks and verifies the contents against the contents of a buffer. The offset into the NAND must be page-aligned, and the function doesn't handle skipping bad blocks. */
int nand_verify(struct mtd_info *mtd, loff_t ofs, size_t len, u_char *buf)
{
	int rval = 0;
	size_t verofs;
	size_t verlen = mtd->writesize;
	uint8_t *verbuf = memalign(ARCH_DMA_MINALIGN, verlen);
	if (!verbuf)
		return -ENOMEM;
	for (verofs = ofs; verofs < ofs + len;
	     verofs += verlen, buf += verlen) {
		verlen = min(mtd->writesize, (uint32_t)(ofs + len - verofs));
		rval = nand_read(mtd, verofs, &verlen, verbuf);
		if (!rval || (rval == -EUCLEAN))
			rval = memcmp(buf, verbuf, verlen);
		if (rval)
			break;
	}
	free(verbuf);
	return rval ? -EIO : 0;
}