layout: Verify that regions to be written are granularity aligned

This will be important with the new erase/write strategy when we don't
write per erase block anymore.

Change-Id: Ie3c74ff4313c9d72ac92d3226804e0407088c17d
Signed-off-by: Nico Huber <nico.h@gmx.de>
Reviewed-on: https://review.coreboot.org/c/flashrom-stable/+/72546
Reviewed-by: Arthur Heymans <arthur@aheymans.xyz>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
diff --git a/layout.c b/layout.c
index d645078..4a5c276 100644
--- a/layout.c
+++ b/layout.c
@@ -197,10 +197,11 @@
 	}
 }
 
-int layout_sanity_checks(const struct flashrom_flashctx *const flash)
+int layout_sanity_checks(const struct flashrom_flashctx *const flash, const bool write_it)
 {
 	const struct flashrom_layout *const layout = get_layout(flash);
 	const chipsize_t total_size = flash->chip->total_size * 1024;
+	const size_t gran = gran_to_bytes(flash->chip->gran);
 	int ret = 0;
 
 	const struct romentry *entry = NULL;
@@ -216,6 +217,11 @@
 				  entry->name);
 			ret = 1;
 		}
+		if (write_it && entry->included && (entry->start % gran || (entry->end + 1) % gran)) {
+			msg_gerr("Error: Region \"%s\" is not aligned with write granularity (%zuB).\n",
+				 entry->name, gran);
+			ret = 1;
+		}
 	}
 
 	return ret;