flashrom.c: Don't check for erase outside of selected region

Originally, select_erase_functions() was intended to be called only
with regions that are erase-block aligned. To prepare for unaligned
regions, we limit the range passed to need_erase() to what should be
written.

Change-Id: Iae38ecbf79bf6fccc0ff6f874056e0c071636f89
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/72559
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
diff --git a/flashrom.c b/flashrom.c
index 4e2db4d..c18ff43 100644
--- a/flashrom.c
+++ b/flashrom.c
@@ -1082,13 +1082,13 @@
 	struct eraseblock_data *ll = &layout[findex].layout_list[block_num];
 	if (!findex) {
 		if (ll->start_addr >= info->region_start && ll->end_addr <= info->region_end) {
-			chipoff_t start_addr = ll->start_addr;
-			chipoff_t end_addr = ll->end_addr;
-			const chipsize_t erase_len = end_addr - start_addr + 1;
-			const uint8_t erased_value = ERASED_VALUE(flashctx);
+			const chipoff_t write_start = MAX(info->region_start, ll->start_addr);
+			const chipoff_t write_end   = MIN(info->region_end, ll->end_addr);
+			const chipsize_t write_len  = write_end - write_start + 1;
+			const uint8_t erased_value  = ERASED_VALUE(flashctx);
 			ll->selected = need_erase(
-				info->curcontents + start_addr, info->newcontents + start_addr,
-				erase_len, flashctx->chip->gran, erased_value);
+				info->curcontents + write_start, info->newcontents + write_start,
+				write_len, flashctx->chip->gran, erased_value);
 		}
 	} else {
 		int count = 0;